ar934x_nfc.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. /*
  2. * Driver for the built-in NAND controller of the Atheros AR934x SoCs
  3. *
  4. * Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. #include <linux/version.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/module.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/mtd/mtd.h>
  16. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
  17. #include <linux/mtd/nand.h>
  18. #else
  19. #include <linux/mtd/rawnand.h>
  20. #endif
  21. #include <linux/mtd/partitions.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/platform/ar934x_nfc.h>
  26. #define AR934X_NFC_REG_CMD 0x00
  27. #define AR934X_NFC_REG_CTRL 0x04
  28. #define AR934X_NFC_REG_STATUS 0x08
  29. #define AR934X_NFC_REG_INT_MASK 0x0c
  30. #define AR934X_NFC_REG_INT_STATUS 0x10
  31. #define AR934X_NFC_REG_ECC_CTRL 0x14
  32. #define AR934X_NFC_REG_ECC_OFFSET 0x18
  33. #define AR934X_NFC_REG_ADDR0_0 0x1c
  34. #define AR934X_NFC_REG_ADDR0_1 0x24
  35. #define AR934X_NFC_REG_ADDR1_0 0x20
  36. #define AR934X_NFC_REG_ADDR1_1 0x28
  37. #define AR934X_NFC_REG_SPARE_SIZE 0x30
  38. #define AR934X_NFC_REG_PROTECT 0x38
  39. #define AR934X_NFC_REG_LOOKUP_EN 0x40
  40. #define AR934X_NFC_REG_LOOKUP(_x) (0x44 + (_i) * 4)
  41. #define AR934X_NFC_REG_DMA_ADDR 0x64
  42. #define AR934X_NFC_REG_DMA_COUNT 0x68
  43. #define AR934X_NFC_REG_DMA_CTRL 0x6c
  44. #define AR934X_NFC_REG_MEM_CTRL 0x80
  45. #define AR934X_NFC_REG_DATA_SIZE 0x84
  46. #define AR934X_NFC_REG_READ_STATUS 0x88
  47. #define AR934X_NFC_REG_TIME_SEQ 0x8c
  48. #define AR934X_NFC_REG_TIMINGS_ASYN 0x90
  49. #define AR934X_NFC_REG_TIMINGS_SYN 0x94
  50. #define AR934X_NFC_REG_FIFO_DATA 0x98
  51. #define AR934X_NFC_REG_TIME_MODE 0x9c
  52. #define AR934X_NFC_REG_DMA_ADDR_OFFS 0xa0
  53. #define AR934X_NFC_REG_FIFO_INIT 0xb0
  54. #define AR934X_NFC_REG_GEN_SEQ_CTRL 0xb4
  55. #define AR934X_NFC_CMD_CMD_SEQ_S 0
  56. #define AR934X_NFC_CMD_CMD_SEQ_M 0x3f
  57. #define AR934X_NFC_CMD_SEQ_1C 0x00
  58. #define AR934X_NFC_CMD_SEQ_ERASE 0x0e
  59. #define AR934X_NFC_CMD_SEQ_12 0x0c
  60. #define AR934X_NFC_CMD_SEQ_1C1AXR 0x21
  61. #define AR934X_NFC_CMD_SEQ_S 0x24
  62. #define AR934X_NFC_CMD_SEQ_1C3AXR 0x27
  63. #define AR934X_NFC_CMD_SEQ_1C5A1CXR 0x2a
  64. #define AR934X_NFC_CMD_SEQ_18 0x32
  65. #define AR934X_NFC_CMD_INPUT_SEL_SIU 0
  66. #define AR934X_NFC_CMD_INPUT_SEL_DMA BIT(6)
  67. #define AR934X_NFC_CMD_ADDR_SEL_0 0
  68. #define AR934X_NFC_CMD_ADDR_SEL_1 BIT(7)
  69. #define AR934X_NFC_CMD_CMD0_S 8
  70. #define AR934X_NFC_CMD_CMD0_M 0xff
  71. #define AR934X_NFC_CMD_CMD1_S 16
  72. #define AR934X_NFC_CMD_CMD1_M 0xff
  73. #define AR934X_NFC_CMD_CMD2_S 24
  74. #define AR934X_NFC_CMD_CMD2_M 0xff
  75. #define AR934X_NFC_CTRL_ADDR_CYCLE0_M 0x7
  76. #define AR934X_NFC_CTRL_ADDR_CYCLE0_S 0
  77. #define AR934X_NFC_CTRL_SPARE_EN BIT(3)
  78. #define AR934X_NFC_CTRL_INT_EN BIT(4)
  79. #define AR934X_NFC_CTRL_ECC_EN BIT(5)
  80. #define AR934X_NFC_CTRL_BLOCK_SIZE_S 6
  81. #define AR934X_NFC_CTRL_BLOCK_SIZE_M 0x3
  82. #define AR934X_NFC_CTRL_BLOCK_SIZE_32 0
  83. #define AR934X_NFC_CTRL_BLOCK_SIZE_64 1
  84. #define AR934X_NFC_CTRL_BLOCK_SIZE_128 2
  85. #define AR934X_NFC_CTRL_BLOCK_SIZE_256 3
  86. #define AR934X_NFC_CTRL_PAGE_SIZE_S 8
  87. #define AR934X_NFC_CTRL_PAGE_SIZE_M 0x7
  88. #define AR934X_NFC_CTRL_PAGE_SIZE_256 0
  89. #define AR934X_NFC_CTRL_PAGE_SIZE_512 1
  90. #define AR934X_NFC_CTRL_PAGE_SIZE_1024 2
  91. #define AR934X_NFC_CTRL_PAGE_SIZE_2048 3
  92. #define AR934X_NFC_CTRL_PAGE_SIZE_4096 4
  93. #define AR934X_NFC_CTRL_PAGE_SIZE_8192 5
  94. #define AR934X_NFC_CTRL_PAGE_SIZE_16384 6
  95. #define AR934X_NFC_CTRL_CUSTOM_SIZE_EN BIT(11)
  96. #define AR934X_NFC_CTRL_IO_WIDTH_8BITS 0
  97. #define AR934X_NFC_CTRL_IO_WIDTH_16BITS BIT(12)
  98. #define AR934X_NFC_CTRL_LOOKUP_EN BIT(13)
  99. #define AR934X_NFC_CTRL_PROT_EN BIT(14)
  100. #define AR934X_NFC_CTRL_WORK_MODE_ASYNC 0
  101. #define AR934X_NFC_CTRL_WORK_MODE_SYNC BIT(15)
  102. #define AR934X_NFC_CTRL_ADDR0_AUTO_INC BIT(16)
  103. #define AR934X_NFC_CTRL_ADDR1_AUTO_INC BIT(17)
  104. #define AR934X_NFC_CTRL_ADDR_CYCLE1_M 0x7
  105. #define AR934X_NFC_CTRL_ADDR_CYCLE1_S 18
  106. #define AR934X_NFC_CTRL_SMALL_PAGE BIT(21)
  107. #define AR934X_NFC_DMA_CTRL_DMA_START BIT(7)
  108. #define AR934X_NFC_DMA_CTRL_DMA_DIR_WRITE 0
  109. #define AR934X_NFC_DMA_CTRL_DMA_DIR_READ BIT(6)
  110. #define AR934X_NFC_DMA_CTRL_DMA_MODE_SG BIT(5)
  111. #define AR934X_NFC_DMA_CTRL_DMA_BURST_S 2
  112. #define AR934X_NFC_DMA_CTRL_DMA_BURST_0 0
  113. #define AR934X_NFC_DMA_CTRL_DMA_BURST_1 1
  114. #define AR934X_NFC_DMA_CTRL_DMA_BURST_2 2
  115. #define AR934X_NFC_DMA_CTRL_DMA_BURST_3 3
  116. #define AR934X_NFC_DMA_CTRL_DMA_BURST_4 4
  117. #define AR934X_NFC_DMA_CTRL_DMA_BURST_5 5
  118. #define AR934X_NFC_DMA_CTRL_ERR_FLAG BIT(1)
  119. #define AR934X_NFC_DMA_CTRL_DMA_READY BIT(0)
  120. #define AR934X_NFC_INT_DEV_RDY(_x) BIT(4 + (_x))
  121. #define AR934X_NFC_INT_CMD_END BIT(1)
  122. #define AR934X_NFC_ECC_CTRL_ERR_THRES_S 8
  123. #define AR934X_NFC_ECC_CTRL_ERR_THRES_M 0x1f
  124. #define AR934X_NFC_ECC_CTRL_ECC_CAP_S 5
  125. #define AR934X_NFC_ECC_CTRL_ECC_CAP_M 0x7
  126. #define AR934X_NFC_ECC_CTRL_ECC_CAP_2 0
  127. #define AR934X_NFC_ECC_CTRL_ECC_CAP_4 1
  128. #define AR934X_NFC_ECC_CTRL_ECC_CAP_6 2
  129. #define AR934X_NFC_ECC_CTRL_ECC_CAP_8 3
  130. #define AR934X_NFC_ECC_CTRL_ECC_CAP_10 4
  131. #define AR934X_NFC_ECC_CTRL_ECC_CAP_12 5
  132. #define AR934X_NFC_ECC_CTRL_ECC_CAP_14 6
  133. #define AR934X_NFC_ECC_CTRL_ECC_CAP_16 7
  134. #define AR934X_NFC_ECC_CTRL_ERR_OVER BIT(2)
  135. #define AR934X_NFC_ECC_CTRL_ERR_UNCORRECT BIT(1)
  136. #define AR934X_NFC_ECC_CTRL_ERR_CORRECT BIT(0)
  137. #define AR934X_NFC_ECC_OFFS_OFSET_M 0xffff
  138. /* default timing values */
  139. #define AR934X_NFC_TIME_SEQ_DEFAULT 0x7fff
  140. #define AR934X_NFC_TIMINGS_ASYN_DEFAULT 0x22
  141. #define AR934X_NFC_TIMINGS_SYN_DEFAULT 0xf
  142. #define AR934X_NFC_ID_BUF_SIZE 8
  143. #define AR934X_NFC_DEV_READY_TIMEOUT 25 /* msecs */
  144. #define AR934X_NFC_DMA_READY_TIMEOUT 25 /* msecs */
  145. #define AR934X_NFC_DONE_TIMEOUT 1000
  146. #define AR934X_NFC_DMA_RETRIES 20
  147. #define AR934X_NFC_USE_IRQ true
  148. #define AR934X_NFC_IRQ_MASK AR934X_NFC_INT_DEV_RDY(0)
  149. #define AR934X_NFC_GENSEQ_SMALL_PAGE_READ 0x30043
  150. #undef AR934X_NFC_DEBUG_DATA
  151. #undef AR934X_NFC_DEBUG
  152. struct ar934x_nfc;
  153. static inline __attribute__ ((format (printf, 2, 3)))
  154. void _nfc_dbg(struct ar934x_nfc *nfc, const char *fmt, ...)
  155. {
  156. }
  157. #ifdef AR934X_NFC_DEBUG
  158. #define nfc_dbg(_nfc, fmt, ...) \
  159. dev_info((_nfc)->parent, fmt, ##__VA_ARGS__)
  160. #else
  161. #define nfc_dbg(_nfc, fmt, ...) \
  162. _nfc_dbg((_nfc), fmt, ##__VA_ARGS__)
  163. #endif /* AR934X_NFC_DEBUG */
  164. #ifdef AR934X_NFC_DEBUG_DATA
  165. static void
  166. nfc_debug_data(const char *label, void *data, int len)
  167. {
  168. print_hex_dump(KERN_WARNING, label, DUMP_PREFIX_OFFSET, 16, 1,
  169. data, len, 0);
  170. }
  171. #else
  172. static inline void
  173. nfc_debug_data(const char *label, void *data, int len) {}
  174. #endif /* AR934X_NFC_DEBUG_DATA */
  175. struct ar934x_nfc {
  176. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  177. struct mtd_info mtd;
  178. #endif
  179. struct nand_chip nand_chip;
  180. struct device *parent;
  181. void __iomem *base;
  182. void (*select_chip)(int chip_no);
  183. bool swap_dma;
  184. int irq;
  185. wait_queue_head_t irq_waitq;
  186. bool spurious_irq_expected;
  187. u32 irq_status;
  188. u32 ctrl_reg;
  189. u32 ecc_ctrl_reg;
  190. u32 ecc_offset_reg;
  191. u32 ecc_thres;
  192. u32 ecc_oob_pos;
  193. bool small_page;
  194. unsigned int addr_count0;
  195. unsigned int addr_count1;
  196. u8 *buf;
  197. dma_addr_t buf_dma;
  198. unsigned int buf_size;
  199. int buf_index;
  200. bool read_id;
  201. int erase1_page_addr;
  202. int rndout_page_addr;
  203. int rndout_read_cmd;
  204. int seqin_page_addr;
  205. int seqin_column;
  206. int seqin_read_cmd;
  207. };
  208. static void ar934x_nfc_restart(struct ar934x_nfc *nfc);
  209. static inline bool
  210. is_all_ff(u8 *buf, int len)
  211. {
  212. while (len--)
  213. if (buf[len] != 0xff)
  214. return false;
  215. return true;
  216. }
  217. static inline void
  218. ar934x_nfc_wr(struct ar934x_nfc *nfc, unsigned reg, u32 val)
  219. {
  220. __raw_writel(val, nfc->base + reg);
  221. }
  222. static inline u32
  223. ar934x_nfc_rr(struct ar934x_nfc *nfc, unsigned reg)
  224. {
  225. return __raw_readl(nfc->base + reg);
  226. }
  227. static inline struct ar934x_nfc_platform_data *
  228. ar934x_nfc_get_platform_data(struct ar934x_nfc *nfc)
  229. {
  230. return nfc->parent->platform_data;
  231. }
  232. static inline struct
  233. ar934x_nfc *mtd_to_ar934x_nfc(struct mtd_info *mtd)
  234. {
  235. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  236. return container_of(mtd, struct ar934x_nfc, mtd);
  237. #else
  238. struct nand_chip *chip = mtd_to_nand(mtd);
  239. return container_of(chip, struct ar934x_nfc, nand_chip);
  240. #endif
  241. }
  242. static struct mtd_info *ar934x_nfc_to_mtd(struct ar934x_nfc *nfc)
  243. {
  244. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  245. return &nfc->mtd;
  246. #else
  247. return nand_to_mtd(&nfc->nand_chip);
  248. #endif
  249. }
  250. static inline bool ar934x_nfc_use_irq(struct ar934x_nfc *nfc)
  251. {
  252. return AR934X_NFC_USE_IRQ;
  253. }
  254. static inline void ar934x_nfc_write_cmd_reg(struct ar934x_nfc *nfc, u32 cmd_reg)
  255. {
  256. wmb();
  257. ar934x_nfc_wr(nfc, AR934X_NFC_REG_CMD, cmd_reg);
  258. /* flush write */
  259. ar934x_nfc_rr(nfc, AR934X_NFC_REG_CMD);
  260. }
  261. static bool
  262. __ar934x_nfc_dev_ready(struct ar934x_nfc *nfc)
  263. {
  264. u32 status;
  265. status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_STATUS);
  266. return (status & 0xff) == 0xff;
  267. }
  268. static inline bool
  269. __ar934x_nfc_is_dma_ready(struct ar934x_nfc *nfc)
  270. {
  271. u32 status;
  272. status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_DMA_CTRL);
  273. return (status & AR934X_NFC_DMA_CTRL_DMA_READY) != 0;
  274. }
  275. static int
  276. ar934x_nfc_wait_dev_ready(struct ar934x_nfc *nfc)
  277. {
  278. unsigned long timeout;
  279. timeout = jiffies + msecs_to_jiffies(AR934X_NFC_DEV_READY_TIMEOUT);
  280. do {
  281. if (__ar934x_nfc_dev_ready(nfc))
  282. return 0;
  283. } while time_before(jiffies, timeout);
  284. nfc_dbg(nfc, "timeout waiting for device ready, status:%08x int:%08x\n",
  285. ar934x_nfc_rr(nfc, AR934X_NFC_REG_STATUS),
  286. ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS));
  287. return -ETIMEDOUT;
  288. }
  289. static int
  290. ar934x_nfc_wait_dma_ready(struct ar934x_nfc *nfc)
  291. {
  292. unsigned long timeout;
  293. timeout = jiffies + msecs_to_jiffies(AR934X_NFC_DMA_READY_TIMEOUT);
  294. do {
  295. if (__ar934x_nfc_is_dma_ready(nfc))
  296. return 0;
  297. } while time_before(jiffies, timeout);
  298. nfc_dbg(nfc, "timeout waiting for DMA ready, dma_ctrl:%08x\n",
  299. ar934x_nfc_rr(nfc, AR934X_NFC_REG_DMA_CTRL));
  300. return -ETIMEDOUT;
  301. }
  302. static int
  303. ar934x_nfc_wait_irq(struct ar934x_nfc *nfc)
  304. {
  305. long timeout;
  306. int ret;
  307. timeout = wait_event_timeout(nfc->irq_waitq,
  308. (nfc->irq_status & AR934X_NFC_IRQ_MASK) != 0,
  309. msecs_to_jiffies(AR934X_NFC_DEV_READY_TIMEOUT));
  310. ret = 0;
  311. if (!timeout) {
  312. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_MASK, 0);
  313. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
  314. /* flush write */
  315. ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS);
  316. nfc_dbg(nfc,
  317. "timeout waiting for interrupt, status:%08x\n",
  318. nfc->irq_status);
  319. ret = -ETIMEDOUT;
  320. }
  321. nfc->irq_status = 0;
  322. return ret;
  323. }
  324. static int
  325. ar934x_nfc_wait_done(struct ar934x_nfc *nfc)
  326. {
  327. int ret;
  328. if (ar934x_nfc_use_irq(nfc))
  329. ret = ar934x_nfc_wait_irq(nfc);
  330. else
  331. ret = ar934x_nfc_wait_dev_ready(nfc);
  332. if (ret)
  333. return ret;
  334. return ar934x_nfc_wait_dma_ready(nfc);
  335. }
  336. static int
  337. ar934x_nfc_alloc_buf(struct ar934x_nfc *nfc, unsigned size)
  338. {
  339. nfc->buf = dma_alloc_coherent(nfc->parent, size,
  340. &nfc->buf_dma, GFP_KERNEL);
  341. if (nfc->buf == NULL) {
  342. dev_err(nfc->parent, "no memory for DMA buffer\n");
  343. return -ENOMEM;
  344. }
  345. nfc->buf_size = size;
  346. nfc_dbg(nfc, "buf:%p size:%u\n", nfc->buf, nfc->buf_size);
  347. return 0;
  348. }
  349. static void
  350. ar934x_nfc_free_buf(struct ar934x_nfc *nfc)
  351. {
  352. dma_free_coherent(nfc->parent, nfc->buf_size, nfc->buf, nfc->buf_dma);
  353. }
  354. static void
  355. ar934x_nfc_get_addr(struct ar934x_nfc *nfc, int column, int page_addr,
  356. u32 *addr0, u32 *addr1)
  357. {
  358. u32 a0, a1;
  359. a0 = 0;
  360. a1 = 0;
  361. if (column == -1) {
  362. /* ERASE1 */
  363. a0 = (page_addr & 0xffff) << 16;
  364. a1 = (page_addr >> 16) & 0xf;
  365. } else if (page_addr != -1) {
  366. /* SEQIN, READ0, etc.. */
  367. /* TODO: handle 16bit bus width */
  368. if (nfc->small_page) {
  369. a0 = column & 0xff;
  370. a0 |= (page_addr & 0xff) << 8;
  371. a0 |= ((page_addr >> 8) & 0xff) << 16;
  372. a0 |= ((page_addr >> 16) & 0xff) << 24;
  373. } else {
  374. a0 = column & 0x0FFF;
  375. a0 |= (page_addr & 0xffff) << 16;
  376. if (nfc->addr_count0 > 4)
  377. a1 = (page_addr >> 16) & 0xf;
  378. }
  379. }
  380. *addr0 = a0;
  381. *addr1 = a1;
  382. }
  383. static void
  384. ar934x_nfc_send_cmd(struct ar934x_nfc *nfc, unsigned command)
  385. {
  386. u32 cmd_reg;
  387. cmd_reg = AR934X_NFC_CMD_INPUT_SEL_SIU | AR934X_NFC_CMD_ADDR_SEL_0 |
  388. AR934X_NFC_CMD_SEQ_1C;
  389. cmd_reg |= (command & AR934X_NFC_CMD_CMD0_M) << AR934X_NFC_CMD_CMD0_S;
  390. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
  391. ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
  392. ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
  393. ar934x_nfc_wait_dev_ready(nfc);
  394. }
  395. static int
  396. ar934x_nfc_do_rw_command(struct ar934x_nfc *nfc, int column, int page_addr,
  397. int len, u32 cmd_reg, u32 ctrl_reg, bool write)
  398. {
  399. u32 addr0, addr1;
  400. u32 dma_ctrl;
  401. int dir;
  402. int err;
  403. int retries = 0;
  404. WARN_ON(len & 3);
  405. if (WARN_ON(len > nfc->buf_size))
  406. dev_err(nfc->parent, "len=%d > buf_size=%d", len, nfc->buf_size);
  407. if (write) {
  408. dma_ctrl = AR934X_NFC_DMA_CTRL_DMA_DIR_WRITE;
  409. dir = DMA_TO_DEVICE;
  410. } else {
  411. dma_ctrl = AR934X_NFC_DMA_CTRL_DMA_DIR_READ;
  412. dir = DMA_FROM_DEVICE;
  413. }
  414. ar934x_nfc_get_addr(nfc, column, page_addr, &addr0, &addr1);
  415. dma_ctrl |= AR934X_NFC_DMA_CTRL_DMA_START |
  416. (AR934X_NFC_DMA_CTRL_DMA_BURST_3 <<
  417. AR934X_NFC_DMA_CTRL_DMA_BURST_S);
  418. cmd_reg |= AR934X_NFC_CMD_INPUT_SEL_DMA | AR934X_NFC_CMD_ADDR_SEL_0;
  419. ctrl_reg |= AR934X_NFC_CTRL_INT_EN;
  420. nfc_dbg(nfc, "%s a0:%08x a1:%08x len:%x cmd:%08x dma:%08x ctrl:%08x\n",
  421. (write) ? "write" : "read",
  422. addr0, addr1, len, cmd_reg, dma_ctrl, ctrl_reg);
  423. retry:
  424. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
  425. ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_0, addr0);
  426. ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_1, addr1);
  427. ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_ADDR, nfc->buf_dma);
  428. ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_COUNT, len);
  429. ar934x_nfc_wr(nfc, AR934X_NFC_REG_DATA_SIZE, len);
  430. ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, ctrl_reg);
  431. ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_CTRL, dma_ctrl);
  432. ar934x_nfc_wr(nfc, AR934X_NFC_REG_ECC_CTRL, nfc->ecc_ctrl_reg);
  433. ar934x_nfc_wr(nfc, AR934X_NFC_REG_ECC_OFFSET, nfc->ecc_offset_reg);
  434. if (ar934x_nfc_use_irq(nfc)) {
  435. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_MASK, AR934X_NFC_IRQ_MASK);
  436. /* flush write */
  437. ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_MASK);
  438. }
  439. ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
  440. err = ar934x_nfc_wait_done(nfc);
  441. if (err) {
  442. dev_dbg(nfc->parent, "%s operation stuck at page %d\n",
  443. (write) ? "write" : "read", page_addr);
  444. ar934x_nfc_restart(nfc);
  445. if (retries++ < AR934X_NFC_DMA_RETRIES)
  446. goto retry;
  447. dev_err(nfc->parent, "%s operation failed on page %d\n",
  448. (write) ? "write" : "read", page_addr);
  449. }
  450. return err;
  451. }
  452. static int
  453. ar934x_nfc_send_readid(struct ar934x_nfc *nfc, unsigned command)
  454. {
  455. u32 cmd_reg;
  456. int err;
  457. nfc_dbg(nfc, "readid, cmd:%02x\n", command);
  458. cmd_reg = AR934X_NFC_CMD_SEQ_1C1AXR;
  459. cmd_reg |= (command & AR934X_NFC_CMD_CMD0_M) << AR934X_NFC_CMD_CMD0_S;
  460. err = ar934x_nfc_do_rw_command(nfc, -1, -1, AR934X_NFC_ID_BUF_SIZE,
  461. cmd_reg, nfc->ctrl_reg, false);
  462. nfc_debug_data("[id] ", nfc->buf, AR934X_NFC_ID_BUF_SIZE);
  463. return err;
  464. }
  465. static int
  466. ar934x_nfc_send_read(struct ar934x_nfc *nfc, unsigned command, int column,
  467. int page_addr, int len)
  468. {
  469. u32 cmd_reg;
  470. int err;
  471. nfc_dbg(nfc, "read, column=%d page=%d len=%d\n",
  472. column, page_addr, len);
  473. cmd_reg = (command & AR934X_NFC_CMD_CMD0_M) << AR934X_NFC_CMD_CMD0_S;
  474. if (nfc->small_page) {
  475. cmd_reg |= AR934X_NFC_CMD_SEQ_18;
  476. } else {
  477. cmd_reg |= NAND_CMD_READSTART << AR934X_NFC_CMD_CMD1_S;
  478. cmd_reg |= AR934X_NFC_CMD_SEQ_1C5A1CXR;
  479. }
  480. err = ar934x_nfc_do_rw_command(nfc, column, page_addr, len,
  481. cmd_reg, nfc->ctrl_reg, false);
  482. nfc_debug_data("[data] ", nfc->buf, len);
  483. return err;
  484. }
  485. static void
  486. ar934x_nfc_send_erase(struct ar934x_nfc *nfc, unsigned command, int column,
  487. int page_addr)
  488. {
  489. u32 addr0, addr1;
  490. u32 ctrl_reg;
  491. u32 cmd_reg;
  492. ar934x_nfc_get_addr(nfc, column, page_addr, &addr0, &addr1);
  493. ctrl_reg = nfc->ctrl_reg;
  494. if (nfc->small_page) {
  495. /* override number of address cycles for the erase command */
  496. ctrl_reg &= ~(AR934X_NFC_CTRL_ADDR_CYCLE0_M <<
  497. AR934X_NFC_CTRL_ADDR_CYCLE0_S);
  498. ctrl_reg &= ~(AR934X_NFC_CTRL_ADDR_CYCLE1_M <<
  499. AR934X_NFC_CTRL_ADDR_CYCLE1_S);
  500. ctrl_reg &= ~(AR934X_NFC_CTRL_SMALL_PAGE);
  501. ctrl_reg |= (nfc->addr_count0 + 1) <<
  502. AR934X_NFC_CTRL_ADDR_CYCLE0_S;
  503. }
  504. cmd_reg = NAND_CMD_ERASE1 << AR934X_NFC_CMD_CMD0_S;
  505. cmd_reg |= command << AR934X_NFC_CMD_CMD1_S;
  506. cmd_reg |= AR934X_NFC_CMD_SEQ_ERASE;
  507. nfc_dbg(nfc, "erase page %d, a0:%08x a1:%08x cmd:%08x ctrl:%08x\n",
  508. page_addr, addr0, addr1, cmd_reg, ctrl_reg);
  509. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
  510. ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, ctrl_reg);
  511. ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_0, addr0);
  512. ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_1, addr1);
  513. ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
  514. ar934x_nfc_wait_dev_ready(nfc);
  515. }
  516. static int
  517. ar934x_nfc_send_write(struct ar934x_nfc *nfc, unsigned command, int column,
  518. int page_addr, int len)
  519. {
  520. u32 cmd_reg;
  521. nfc_dbg(nfc, "write, column=%d page=%d len=%d\n",
  522. column, page_addr, len);
  523. nfc_debug_data("[data] ", nfc->buf, len);
  524. cmd_reg = NAND_CMD_SEQIN << AR934X_NFC_CMD_CMD0_S;
  525. cmd_reg |= command << AR934X_NFC_CMD_CMD1_S;
  526. cmd_reg |= AR934X_NFC_CMD_SEQ_12;
  527. return ar934x_nfc_do_rw_command(nfc, column, page_addr, len,
  528. cmd_reg, nfc->ctrl_reg, true);
  529. }
  530. static void
  531. ar934x_nfc_read_status(struct ar934x_nfc *nfc)
  532. {
  533. u32 cmd_reg;
  534. u32 status;
  535. cmd_reg = NAND_CMD_STATUS << AR934X_NFC_CMD_CMD0_S;
  536. cmd_reg |= AR934X_NFC_CMD_SEQ_S;
  537. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
  538. ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
  539. ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
  540. ar934x_nfc_wait_dev_ready(nfc);
  541. status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_READ_STATUS);
  542. nfc_dbg(nfc, "read status, cmd:%08x status:%02x\n",
  543. cmd_reg, (status & 0xff));
  544. if (nfc->swap_dma)
  545. nfc->buf[0 ^ 3] = status;
  546. else
  547. nfc->buf[0] = status;
  548. }
  549. static void
  550. ar934x_nfc_cmdfunc(struct mtd_info *mtd, unsigned int command, int column,
  551. int page_addr)
  552. {
  553. struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
  554. struct nand_chip *nand = &nfc->nand_chip;
  555. nfc->read_id = false;
  556. if (command != NAND_CMD_PAGEPROG)
  557. nfc->buf_index = 0;
  558. switch (command) {
  559. case NAND_CMD_RESET:
  560. ar934x_nfc_send_cmd(nfc, command);
  561. break;
  562. case NAND_CMD_READID:
  563. nfc->read_id = true;
  564. ar934x_nfc_send_readid(nfc, command);
  565. break;
  566. case NAND_CMD_READ0:
  567. case NAND_CMD_READ1:
  568. if (nfc->small_page) {
  569. ar934x_nfc_send_read(nfc, command, column, page_addr,
  570. mtd->writesize + mtd->oobsize);
  571. } else {
  572. ar934x_nfc_send_read(nfc, command, 0, page_addr,
  573. mtd->writesize + mtd->oobsize);
  574. nfc->buf_index = column;
  575. nfc->rndout_page_addr = page_addr;
  576. nfc->rndout_read_cmd = command;
  577. }
  578. break;
  579. case NAND_CMD_READOOB:
  580. if (nfc->small_page)
  581. ar934x_nfc_send_read(nfc, NAND_CMD_READOOB,
  582. column, page_addr,
  583. mtd->oobsize);
  584. else
  585. ar934x_nfc_send_read(nfc, NAND_CMD_READ0,
  586. mtd->writesize, page_addr,
  587. mtd->oobsize);
  588. break;
  589. case NAND_CMD_RNDOUT:
  590. if (WARN_ON(nfc->small_page))
  591. break;
  592. /* emulate subpage read */
  593. ar934x_nfc_send_read(nfc, nfc->rndout_read_cmd, 0,
  594. nfc->rndout_page_addr,
  595. mtd->writesize + mtd->oobsize);
  596. nfc->buf_index = column;
  597. break;
  598. case NAND_CMD_ERASE1:
  599. nfc->erase1_page_addr = page_addr;
  600. break;
  601. case NAND_CMD_ERASE2:
  602. ar934x_nfc_send_erase(nfc, command, -1, nfc->erase1_page_addr);
  603. break;
  604. case NAND_CMD_STATUS:
  605. ar934x_nfc_read_status(nfc);
  606. break;
  607. case NAND_CMD_SEQIN:
  608. if (nfc->small_page) {
  609. /* output read command */
  610. if (column >= mtd->writesize) {
  611. column -= mtd->writesize;
  612. nfc->seqin_read_cmd = NAND_CMD_READOOB;
  613. } else if (column < 256) {
  614. nfc->seqin_read_cmd = NAND_CMD_READ0;
  615. } else {
  616. column -= 256;
  617. nfc->seqin_read_cmd = NAND_CMD_READ1;
  618. }
  619. } else {
  620. nfc->seqin_read_cmd = NAND_CMD_READ0;
  621. }
  622. nfc->seqin_column = column;
  623. nfc->seqin_page_addr = page_addr;
  624. break;
  625. case NAND_CMD_PAGEPROG:
  626. if (nand->ecc.mode == NAND_ECC_HW) {
  627. /* the data is already written */
  628. break;
  629. }
  630. if (nfc->small_page)
  631. ar934x_nfc_send_cmd(nfc, nfc->seqin_read_cmd);
  632. ar934x_nfc_send_write(nfc, command, nfc->seqin_column,
  633. nfc->seqin_page_addr,
  634. nfc->buf_index);
  635. break;
  636. default:
  637. dev_err(nfc->parent,
  638. "unsupported command: %x, column:%d page_addr=%d\n",
  639. command, column, page_addr);
  640. break;
  641. }
  642. }
  643. static int
  644. ar934x_nfc_dev_ready(struct mtd_info *mtd)
  645. {
  646. struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
  647. return __ar934x_nfc_dev_ready(nfc);
  648. }
  649. static void
  650. ar934x_nfc_select_chip(struct mtd_info *mtd, int chip_no)
  651. {
  652. struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
  653. if (nfc->select_chip)
  654. nfc->select_chip(chip_no);
  655. }
  656. static u8
  657. ar934x_nfc_read_byte(struct mtd_info *mtd)
  658. {
  659. struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
  660. u8 data;
  661. WARN_ON(nfc->buf_index >= nfc->buf_size);
  662. if (nfc->swap_dma || nfc->read_id)
  663. data = nfc->buf[nfc->buf_index ^ 3];
  664. else
  665. data = nfc->buf[nfc->buf_index];
  666. nfc->buf_index++;
  667. return data;
  668. }
  669. static void
  670. ar934x_nfc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  671. {
  672. struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
  673. int i;
  674. WARN_ON(nfc->buf_index + len > nfc->buf_size);
  675. if (nfc->swap_dma) {
  676. for (i = 0; i < len; i++) {
  677. nfc->buf[nfc->buf_index ^ 3] = buf[i];
  678. nfc->buf_index++;
  679. }
  680. } else {
  681. for (i = 0; i < len; i++) {
  682. nfc->buf[nfc->buf_index] = buf[i];
  683. nfc->buf_index++;
  684. }
  685. }
  686. }
  687. static void
  688. ar934x_nfc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  689. {
  690. struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
  691. int buf_index;
  692. int i;
  693. WARN_ON(nfc->buf_index + len > nfc->buf_size);
  694. buf_index = nfc->buf_index;
  695. if (nfc->swap_dma || nfc->read_id) {
  696. for (i = 0; i < len; i++) {
  697. buf[i] = nfc->buf[buf_index ^ 3];
  698. buf_index++;
  699. }
  700. } else {
  701. for (i = 0; i < len; i++) {
  702. buf[i] = nfc->buf[buf_index];
  703. buf_index++;
  704. }
  705. }
  706. nfc->buf_index = buf_index;
  707. }
  708. static inline void
  709. ar934x_nfc_enable_hwecc(struct ar934x_nfc *nfc)
  710. {
  711. nfc->ctrl_reg |= AR934X_NFC_CTRL_ECC_EN;
  712. nfc->ctrl_reg &= ~AR934X_NFC_CTRL_CUSTOM_SIZE_EN;
  713. }
  714. static inline void
  715. ar934x_nfc_disable_hwecc(struct ar934x_nfc *nfc)
  716. {
  717. nfc->ctrl_reg &= ~AR934X_NFC_CTRL_ECC_EN;
  718. nfc->ctrl_reg |= AR934X_NFC_CTRL_CUSTOM_SIZE_EN;
  719. }
  720. static int
  721. ar934x_nfc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  722. int page)
  723. {
  724. struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
  725. int err;
  726. nfc_dbg(nfc, "read_oob: page:%d\n", page);
  727. err = ar934x_nfc_send_read(nfc, NAND_CMD_READ0, mtd->writesize, page,
  728. mtd->oobsize);
  729. if (err)
  730. return err;
  731. memcpy(chip->oob_poi, nfc->buf, mtd->oobsize);
  732. return 0;
  733. }
  734. static int
  735. ar934x_nfc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  736. int page)
  737. {
  738. struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
  739. nfc_dbg(nfc, "write_oob: page:%d\n", page);
  740. memcpy(nfc->buf, chip->oob_poi, mtd->oobsize);
  741. return ar934x_nfc_send_write(nfc, NAND_CMD_PAGEPROG, mtd->writesize,
  742. page, mtd->oobsize);
  743. }
  744. static int
  745. ar934x_nfc_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  746. u8 *buf, int oob_required, int page)
  747. {
  748. struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
  749. int len;
  750. int err;
  751. nfc_dbg(nfc, "read_page_raw: page:%d oob:%d\n", page, oob_required);
  752. len = mtd->writesize;
  753. if (oob_required)
  754. len += mtd->oobsize;
  755. err = ar934x_nfc_send_read(nfc, NAND_CMD_READ0, 0, page, len);
  756. if (err)
  757. return err;
  758. memcpy(buf, nfc->buf, mtd->writesize);
  759. if (oob_required)
  760. memcpy(chip->oob_poi, &nfc->buf[mtd->writesize], mtd->oobsize);
  761. return 0;
  762. }
  763. static int
  764. ar934x_nfc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  765. u8 *buf, int oob_required, int page)
  766. {
  767. struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
  768. u32 ecc_ctrl;
  769. int max_bitflips = 0;
  770. bool ecc_failed;
  771. bool ecc_corrected;
  772. int err;
  773. nfc_dbg(nfc, "read_page: page:%d oob:%d\n", page, oob_required);
  774. ar934x_nfc_enable_hwecc(nfc);
  775. err = ar934x_nfc_send_read(nfc, NAND_CMD_READ0, 0, page,
  776. mtd->writesize);
  777. ar934x_nfc_disable_hwecc(nfc);
  778. if (err)
  779. return err;
  780. /* TODO: optimize to avoid memcpy */
  781. memcpy(buf, nfc->buf, mtd->writesize);
  782. /* read the ECC status */
  783. ecc_ctrl = ar934x_nfc_rr(nfc, AR934X_NFC_REG_ECC_CTRL);
  784. ecc_failed = ecc_ctrl & AR934X_NFC_ECC_CTRL_ERR_UNCORRECT;
  785. ecc_corrected = ecc_ctrl & AR934X_NFC_ECC_CTRL_ERR_CORRECT;
  786. if (oob_required || ecc_failed) {
  787. err = ar934x_nfc_send_read(nfc, NAND_CMD_READ0, mtd->writesize,
  788. page, mtd->oobsize);
  789. if (err)
  790. return err;
  791. if (oob_required)
  792. memcpy(chip->oob_poi, nfc->buf, mtd->oobsize);
  793. }
  794. if (ecc_failed) {
  795. /*
  796. * The hardware ECC engine reports uncorrectable errors
  797. * on empty pages. Check the ECC bytes and the data. If
  798. * both contains 0xff bytes only, dont report a failure.
  799. *
  800. * TODO: prebuild a buffer with 0xff bytes and use memcmp
  801. * for better performance?
  802. */
  803. if (!is_all_ff(&nfc->buf[nfc->ecc_oob_pos], chip->ecc.total) ||
  804. !is_all_ff(buf, mtd->writesize))
  805. mtd->ecc_stats.failed++;
  806. } else if (ecc_corrected) {
  807. /*
  808. * The hardware does not report the exact count of the
  809. * corrected bitflips, use assumptions based on the
  810. * threshold.
  811. */
  812. if (ecc_ctrl & AR934X_NFC_ECC_CTRL_ERR_OVER) {
  813. /*
  814. * The number of corrected bitflips exceeds the
  815. * threshold. Assume the maximum.
  816. */
  817. max_bitflips = chip->ecc.strength * chip->ecc.steps;
  818. } else {
  819. max_bitflips = nfc->ecc_thres * chip->ecc.steps;
  820. }
  821. mtd->ecc_stats.corrected += max_bitflips;
  822. }
  823. return max_bitflips;
  824. }
  825. static int
  826. ar934x_nfc_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  827. const u8 *buf, int oob_required, int page)
  828. {
  829. struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
  830. int len;
  831. nfc_dbg(nfc, "write_page_raw: page:%d oob:%d\n", page, oob_required);
  832. memcpy(nfc->buf, buf, mtd->writesize);
  833. len = mtd->writesize;
  834. if (oob_required) {
  835. memcpy(&nfc->buf[mtd->writesize], chip->oob_poi, mtd->oobsize);
  836. len += mtd->oobsize;
  837. }
  838. return ar934x_nfc_send_write(nfc, NAND_CMD_PAGEPROG, 0, page, len);
  839. }
  840. static int
  841. ar934x_nfc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  842. const u8 *buf, int oob_required, int page)
  843. {
  844. struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
  845. int err;
  846. nfc_dbg(nfc, "write_page: page:%d oob:%d\n", page, oob_required);
  847. /* write OOB first */
  848. if (oob_required &&
  849. !is_all_ff(chip->oob_poi, mtd->oobsize)) {
  850. err = ar934x_nfc_write_oob(mtd, chip, page);
  851. if (err)
  852. return err;
  853. }
  854. /* TODO: optimize to avoid memcopy */
  855. memcpy(nfc->buf, buf, mtd->writesize);
  856. ar934x_nfc_enable_hwecc(nfc);
  857. err = ar934x_nfc_send_write(nfc, NAND_CMD_PAGEPROG, 0, page,
  858. mtd->writesize);
  859. ar934x_nfc_disable_hwecc(nfc);
  860. return err;
  861. }
  862. static void
  863. ar934x_nfc_hw_init(struct ar934x_nfc *nfc)
  864. {
  865. struct ar934x_nfc_platform_data *pdata;
  866. pdata = ar934x_nfc_get_platform_data(nfc);
  867. if (pdata->hw_reset) {
  868. pdata->hw_reset(true);
  869. pdata->hw_reset(false);
  870. }
  871. /*
  872. * setup timings
  873. * TODO: make it configurable via platform data
  874. */
  875. ar934x_nfc_wr(nfc, AR934X_NFC_REG_TIME_SEQ,
  876. AR934X_NFC_TIME_SEQ_DEFAULT);
  877. ar934x_nfc_wr(nfc, AR934X_NFC_REG_TIMINGS_ASYN,
  878. AR934X_NFC_TIMINGS_ASYN_DEFAULT);
  879. ar934x_nfc_wr(nfc, AR934X_NFC_REG_TIMINGS_SYN,
  880. AR934X_NFC_TIMINGS_SYN_DEFAULT);
  881. /* disable WP on all chips, and select chip 0 */
  882. ar934x_nfc_wr(nfc, AR934X_NFC_REG_MEM_CTRL, 0xff00);
  883. ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_ADDR_OFFS, 0);
  884. /* initialize Control register */
  885. nfc->ctrl_reg = AR934X_NFC_CTRL_CUSTOM_SIZE_EN;
  886. ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
  887. if (nfc->small_page) {
  888. /* Setup generic sequence register for small page reads. */
  889. ar934x_nfc_wr(nfc, AR934X_NFC_REG_GEN_SEQ_CTRL,
  890. AR934X_NFC_GENSEQ_SMALL_PAGE_READ);
  891. }
  892. }
  893. static void
  894. ar934x_nfc_restart(struct ar934x_nfc *nfc)
  895. {
  896. u32 ctrl_reg;
  897. if (nfc->select_chip)
  898. nfc->select_chip(-1);
  899. ctrl_reg = nfc->ctrl_reg;
  900. ar934x_nfc_hw_init(nfc);
  901. nfc->ctrl_reg = ctrl_reg;
  902. if (nfc->select_chip)
  903. nfc->select_chip(0);
  904. ar934x_nfc_send_cmd(nfc, NAND_CMD_RESET);
  905. }
  906. static irqreturn_t
  907. ar934x_nfc_irq_handler(int irq, void *data)
  908. {
  909. struct ar934x_nfc *nfc = data;
  910. u32 status;
  911. status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS);
  912. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
  913. /* flush write */
  914. ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS);
  915. status &= ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_MASK);
  916. if (status) {
  917. nfc_dbg(nfc, "got IRQ, status:%08x\n", status);
  918. nfc->irq_status = status;
  919. nfc->spurious_irq_expected = true;
  920. wake_up(&nfc->irq_waitq);
  921. } else {
  922. if (nfc->spurious_irq_expected) {
  923. nfc->spurious_irq_expected = false;
  924. } else {
  925. dev_warn(nfc->parent, "spurious interrupt\n");
  926. }
  927. }
  928. return IRQ_HANDLED;
  929. }
  930. static int
  931. ar934x_nfc_init_tail(struct mtd_info *mtd)
  932. {
  933. struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
  934. struct nand_chip *chip = &nfc->nand_chip;
  935. u32 ctrl;
  936. u32 t;
  937. int err;
  938. switch (mtd->oobsize) {
  939. case 16:
  940. case 64:
  941. case 128:
  942. ar934x_nfc_wr(nfc, AR934X_NFC_REG_SPARE_SIZE, mtd->oobsize);
  943. break;
  944. default:
  945. dev_err(nfc->parent, "unsupported OOB size: %d bytes\n",
  946. mtd->oobsize);
  947. return -ENXIO;
  948. }
  949. ctrl = AR934X_NFC_CTRL_CUSTOM_SIZE_EN;
  950. switch (mtd->erasesize / mtd->writesize) {
  951. case 32:
  952. t = AR934X_NFC_CTRL_BLOCK_SIZE_32;
  953. break;
  954. case 64:
  955. t = AR934X_NFC_CTRL_BLOCK_SIZE_64;
  956. break;
  957. case 128:
  958. t = AR934X_NFC_CTRL_BLOCK_SIZE_128;
  959. break;
  960. case 256:
  961. t = AR934X_NFC_CTRL_BLOCK_SIZE_256;
  962. break;
  963. default:
  964. dev_err(nfc->parent, "unsupported block size: %u\n",
  965. mtd->erasesize / mtd->writesize);
  966. return -ENXIO;
  967. }
  968. ctrl |= t << AR934X_NFC_CTRL_BLOCK_SIZE_S;
  969. switch (mtd->writesize) {
  970. case 256:
  971. nfc->small_page = 1;
  972. t = AR934X_NFC_CTRL_PAGE_SIZE_256;
  973. break;
  974. case 512:
  975. nfc->small_page = 1;
  976. t = AR934X_NFC_CTRL_PAGE_SIZE_512;
  977. break;
  978. case 1024:
  979. t = AR934X_NFC_CTRL_PAGE_SIZE_1024;
  980. break;
  981. case 2048:
  982. t = AR934X_NFC_CTRL_PAGE_SIZE_2048;
  983. break;
  984. case 4096:
  985. t = AR934X_NFC_CTRL_PAGE_SIZE_4096;
  986. break;
  987. case 8192:
  988. t = AR934X_NFC_CTRL_PAGE_SIZE_8192;
  989. break;
  990. case 16384:
  991. t = AR934X_NFC_CTRL_PAGE_SIZE_16384;
  992. break;
  993. default:
  994. dev_err(nfc->parent, "unsupported write size: %d bytes\n",
  995. mtd->writesize);
  996. return -ENXIO;
  997. }
  998. ctrl |= t << AR934X_NFC_CTRL_PAGE_SIZE_S;
  999. if (nfc->small_page) {
  1000. ctrl |= AR934X_NFC_CTRL_SMALL_PAGE;
  1001. if (chip->chipsize > (32 << 20)) {
  1002. nfc->addr_count0 = 4;
  1003. nfc->addr_count1 = 3;
  1004. } else if (chip->chipsize > (2 << 16)) {
  1005. nfc->addr_count0 = 3;
  1006. nfc->addr_count1 = 2;
  1007. } else {
  1008. nfc->addr_count0 = 2;
  1009. nfc->addr_count1 = 1;
  1010. }
  1011. } else {
  1012. if (chip->chipsize > (128 << 20)) {
  1013. nfc->addr_count0 = 5;
  1014. nfc->addr_count1 = 3;
  1015. } else if (chip->chipsize > (8 << 16)) {
  1016. nfc->addr_count0 = 4;
  1017. nfc->addr_count1 = 2;
  1018. } else {
  1019. nfc->addr_count0 = 3;
  1020. nfc->addr_count1 = 1;
  1021. }
  1022. }
  1023. ctrl |= nfc->addr_count0 << AR934X_NFC_CTRL_ADDR_CYCLE0_S;
  1024. ctrl |= nfc->addr_count1 << AR934X_NFC_CTRL_ADDR_CYCLE1_S;
  1025. nfc->ctrl_reg = ctrl;
  1026. ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
  1027. ar934x_nfc_free_buf(nfc);
  1028. err = ar934x_nfc_alloc_buf(nfc, mtd->writesize + mtd->oobsize);
  1029. return err;
  1030. }
  1031. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  1032. static struct nand_ecclayout ar934x_nfc_oob_64_hwecc = {
  1033. .eccbytes = 28,
  1034. .eccpos = {
  1035. 20, 21, 22, 23, 24, 25, 26,
  1036. 27, 28, 29, 30, 31, 32, 33,
  1037. 34, 35, 36, 37, 38, 39, 40,
  1038. 41, 42, 43, 44, 45, 46, 47,
  1039. },
  1040. .oobfree = {
  1041. {
  1042. .offset = 4,
  1043. .length = 16,
  1044. },
  1045. {
  1046. .offset = 48,
  1047. .length = 16,
  1048. },
  1049. },
  1050. };
  1051. #else
  1052. static int ar934x_nfc_ooblayout_ecc(struct mtd_info *mtd, int section,
  1053. struct mtd_oob_region *oobregion)
  1054. {
  1055. if (section)
  1056. return -ERANGE;
  1057. oobregion->offset = 20;
  1058. oobregion->length = 28;
  1059. return 0;
  1060. }
  1061. static int ar934x_nfc_ooblayout_free(struct mtd_info *mtd, int section,
  1062. struct mtd_oob_region *oobregion)
  1063. {
  1064. switch (section) {
  1065. case 0:
  1066. oobregion->offset = 4;
  1067. oobregion->length = 16;
  1068. return 0;
  1069. case 1:
  1070. oobregion->offset = 48;
  1071. oobregion->length = 16;
  1072. return 0;
  1073. default:
  1074. return -ERANGE;
  1075. }
  1076. }
  1077. static const struct mtd_ooblayout_ops ar934x_nfc_ecclayout_ops = {
  1078. .ecc = ar934x_nfc_ooblayout_ecc,
  1079. .free = ar934x_nfc_ooblayout_free,
  1080. };
  1081. #endif /* < 4.6 */
  1082. static int
  1083. ar934x_nfc_setup_hwecc(struct ar934x_nfc *nfc)
  1084. {
  1085. struct nand_chip *nand = &nfc->nand_chip;
  1086. struct mtd_info *mtd = ar934x_nfc_to_mtd(nfc);
  1087. u32 ecc_cap;
  1088. u32 ecc_thres;
  1089. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0)
  1090. struct mtd_oob_region oobregion;
  1091. #endif
  1092. if (!IS_ENABLED(CONFIG_MTD_NAND_AR934X_HW_ECC)) {
  1093. dev_err(nfc->parent, "hardware ECC support is disabled\n");
  1094. return -EINVAL;
  1095. }
  1096. switch (mtd->writesize) {
  1097. case 2048:
  1098. /*
  1099. * Writing a subpage separately is not supported, because
  1100. * the controller only does ECC on full-page accesses.
  1101. */
  1102. nand->options = NAND_NO_SUBPAGE_WRITE;
  1103. nand->ecc.size = 512;
  1104. nand->ecc.bytes = 7;
  1105. nand->ecc.strength = 4;
  1106. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  1107. nand->ecc.layout = &ar934x_nfc_oob_64_hwecc;
  1108. #else
  1109. mtd_set_ooblayout(mtd, &ar934x_nfc_ecclayout_ops);
  1110. #endif
  1111. break;
  1112. default:
  1113. dev_err(nfc->parent,
  1114. "hardware ECC is not available for %d byte pages\n",
  1115. mtd->writesize);
  1116. return -EINVAL;
  1117. }
  1118. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  1119. BUG_ON(!nand->ecc.layout);
  1120. #else
  1121. BUG_ON(!mtd->ooblayout->ecc);
  1122. #endif
  1123. switch (nand->ecc.strength) {
  1124. case 4:
  1125. ecc_cap = AR934X_NFC_ECC_CTRL_ECC_CAP_4;
  1126. ecc_thres = 4;
  1127. break;
  1128. default:
  1129. dev_err(nfc->parent, "unsupported ECC strength %u\n",
  1130. nand->ecc.strength);
  1131. return -EINVAL;
  1132. }
  1133. nfc->ecc_thres = ecc_thres;
  1134. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  1135. nfc->ecc_oob_pos = nand->ecc.layout->eccpos[0];
  1136. #else
  1137. mtd->ooblayout->ecc(mtd, 0, &oobregion);
  1138. nfc->ecc_oob_pos = oobregion.offset;
  1139. #endif
  1140. nfc->ecc_ctrl_reg = ecc_cap << AR934X_NFC_ECC_CTRL_ECC_CAP_S;
  1141. nfc->ecc_ctrl_reg |= ecc_thres << AR934X_NFC_ECC_CTRL_ERR_THRES_S;
  1142. nfc->ecc_offset_reg = mtd->writesize + nfc->ecc_oob_pos;
  1143. nand->ecc.mode = NAND_ECC_HW;
  1144. nand->ecc.read_page = ar934x_nfc_read_page;
  1145. nand->ecc.read_page_raw = ar934x_nfc_read_page_raw;
  1146. nand->ecc.write_page = ar934x_nfc_write_page;
  1147. nand->ecc.write_page_raw = ar934x_nfc_write_page_raw;
  1148. nand->ecc.read_oob = ar934x_nfc_read_oob;
  1149. nand->ecc.write_oob = ar934x_nfc_write_oob;
  1150. return 0;
  1151. }
  1152. static int
  1153. ar934x_nfc_probe(struct platform_device *pdev)
  1154. {
  1155. static const char *part_probes[] = { "cmdlinepart", NULL, };
  1156. struct ar934x_nfc_platform_data *pdata;
  1157. struct ar934x_nfc *nfc;
  1158. struct resource *res;
  1159. struct mtd_info *mtd;
  1160. struct nand_chip *nand;
  1161. struct mtd_part_parser_data ppdata;
  1162. int ret;
  1163. pdata = pdev->dev.platform_data;
  1164. if (pdata == NULL) {
  1165. dev_err(&pdev->dev, "no platform data defined\n");
  1166. return -EINVAL;
  1167. }
  1168. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1169. if (!res) {
  1170. dev_err(&pdev->dev, "failed to get I/O memory\n");
  1171. return -EINVAL;
  1172. }
  1173. nfc = devm_kzalloc(&pdev->dev, sizeof(struct ar934x_nfc), GFP_KERNEL);
  1174. if (!nfc) {
  1175. dev_err(&pdev->dev, "failed to allocate driver data\n");
  1176. return -ENOMEM;
  1177. }
  1178. nfc->base = devm_ioremap_resource(&pdev->dev, res);
  1179. if (IS_ERR(nfc->base)) {
  1180. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  1181. return PTR_ERR(nfc->base);
  1182. }
  1183. nfc->irq = platform_get_irq(pdev, 0);
  1184. if (nfc->irq < 0) {
  1185. dev_err(&pdev->dev, "no IRQ resource specified\n");
  1186. return -EINVAL;
  1187. }
  1188. init_waitqueue_head(&nfc->irq_waitq);
  1189. ret = request_irq(nfc->irq, ar934x_nfc_irq_handler, 0,
  1190. dev_name(&pdev->dev), nfc);
  1191. if (ret) {
  1192. dev_err(&pdev->dev, "requast_irq failed, err:%d\n", ret);
  1193. return ret;
  1194. }
  1195. nfc->parent = &pdev->dev;
  1196. nfc->select_chip = pdata->select_chip;
  1197. nfc->swap_dma = pdata->swap_dma;
  1198. nand = &nfc->nand_chip;
  1199. mtd = ar934x_nfc_to_mtd(nfc);
  1200. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  1201. mtd->priv = nand;
  1202. #endif
  1203. mtd->owner = THIS_MODULE;
  1204. if (pdata->name)
  1205. mtd->name = pdata->name;
  1206. else
  1207. mtd->name = dev_name(&pdev->dev);
  1208. nand->chip_delay = 25;
  1209. nand->dev_ready = ar934x_nfc_dev_ready;
  1210. nand->cmdfunc = ar934x_nfc_cmdfunc;
  1211. nand->read_byte = ar934x_nfc_read_byte;
  1212. nand->write_buf = ar934x_nfc_write_buf;
  1213. nand->read_buf = ar934x_nfc_read_buf;
  1214. nand->select_chip = ar934x_nfc_select_chip;
  1215. ret = ar934x_nfc_alloc_buf(nfc, AR934X_NFC_ID_BUF_SIZE);
  1216. if (ret)
  1217. goto err_free_irq;
  1218. platform_set_drvdata(pdev, nfc);
  1219. ar934x_nfc_hw_init(nfc);
  1220. ret = nand_scan_ident(mtd, 1, NULL);
  1221. if (ret) {
  1222. dev_err(&pdev->dev, "nand_scan_ident failed, err:%d\n", ret);
  1223. goto err_free_buf;
  1224. }
  1225. ret = ar934x_nfc_init_tail(mtd);
  1226. if (ret) {
  1227. dev_err(&pdev->dev, "init tail failed, err:%d\n", ret);
  1228. goto err_free_buf;
  1229. }
  1230. if (pdata->scan_fixup) {
  1231. ret = pdata->scan_fixup(mtd);
  1232. if (ret)
  1233. goto err_free_buf;
  1234. }
  1235. switch (pdata->ecc_mode) {
  1236. case AR934X_NFC_ECC_SOFT:
  1237. nand->ecc.mode = NAND_ECC_SOFT;
  1238. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0)
  1239. nand->ecc.algo = NAND_ECC_HAMMING;
  1240. #endif
  1241. break;
  1242. case AR934X_NFC_ECC_SOFT_BCH:
  1243. #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
  1244. nand->ecc.mode = NAND_ECC_SOFT_BCH;
  1245. #else
  1246. nand->ecc.mode = NAND_ECC_SOFT;
  1247. nand->ecc.algo = NAND_ECC_BCH;
  1248. #endif
  1249. break;
  1250. case AR934X_NFC_ECC_HW:
  1251. ret = ar934x_nfc_setup_hwecc(nfc);
  1252. if (ret)
  1253. goto err_free_buf;
  1254. break;
  1255. default:
  1256. dev_err(nfc->parent, "unknown ECC mode %d\n", pdata->ecc_mode);
  1257. return -EINVAL;
  1258. }
  1259. ret = nand_scan_tail(mtd);
  1260. if (ret) {
  1261. dev_err(&pdev->dev, "scan tail failed, err:%d\n", ret);
  1262. goto err_free_buf;
  1263. }
  1264. memset(&ppdata, '\0', sizeof(ppdata));
  1265. ret = mtd_device_parse_register(mtd, part_probes, &ppdata,
  1266. pdata->parts, pdata->nr_parts);
  1267. if (ret) {
  1268. dev_err(&pdev->dev, "unable to register mtd, err:%d\n", ret);
  1269. goto err_free_buf;
  1270. }
  1271. return 0;
  1272. err_free_buf:
  1273. ar934x_nfc_free_buf(nfc);
  1274. err_free_irq:
  1275. free_irq(nfc->irq, nfc);
  1276. return ret;
  1277. }
  1278. static int
  1279. ar934x_nfc_remove(struct platform_device *pdev)
  1280. {
  1281. struct ar934x_nfc *nfc;
  1282. struct mtd_info *mtd;
  1283. nfc = platform_get_drvdata(pdev);
  1284. if (nfc) {
  1285. mtd = ar934x_nfc_to_mtd(nfc);
  1286. nand_release(&nfc->nand_chip);
  1287. ar934x_nfc_free_buf(nfc);
  1288. free_irq(nfc->irq, nfc);
  1289. }
  1290. return 0;
  1291. }
  1292. static struct platform_driver ar934x_nfc_driver = {
  1293. .probe = ar934x_nfc_probe,
  1294. .remove = ar934x_nfc_remove,
  1295. .driver = {
  1296. .name = AR934X_NFC_DRIVER_NAME,
  1297. .owner = THIS_MODULE,
  1298. },
  1299. };
  1300. module_platform_driver(ar934x_nfc_driver);
  1301. MODULE_LICENSE("GPL v2");
  1302. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  1303. MODULE_DESCRIPTION("Atheros AR934x NAND Flash Controller driver");
  1304. MODULE_ALIAS("platform:" AR934X_NFC_DRIVER_NAME);