ar934x_nand.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. /*
  2. * Driver for the built-in NAND controller of the Atheros AR934x SoCs
  3. *
  4. * Copyright (C) 2011-2013 Gabor Juhos <[email protected]>
  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. #include <linux/mtd/nand.h>
  17. #include <linux/mtd/rawnand.h>
  18. #include <linux/mtd/partitions.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/delay.h>
  21. #include <linux/slab.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/reset.h>
  25. #define AR934X_NFC_DRIVER_NAME "ar934x-nand"
  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 mtd_info;
  153. struct mtd_partition;
  154. struct ar934x_nfc;
  155. struct ar934x_nfc {
  156. struct nand_chip nand_chip;
  157. struct device *parent;
  158. void __iomem *base;
  159. bool swap_dma;
  160. int irq;
  161. wait_queue_head_t irq_waitq;
  162. bool spurious_irq_expected;
  163. u32 irq_status;
  164. u32 ctrl_reg;
  165. u32 ecc_ctrl_reg;
  166. u32 ecc_offset_reg;
  167. u32 ecc_thres;
  168. u32 ecc_oob_pos;
  169. bool small_page;
  170. unsigned int addr_count0;
  171. unsigned int addr_count1;
  172. u8 *buf;
  173. dma_addr_t buf_dma;
  174. unsigned int buf_size;
  175. int buf_index;
  176. bool read_id;
  177. int erase1_page_addr;
  178. int rndout_page_addr;
  179. int rndout_read_cmd;
  180. int seqin_page_addr;
  181. int seqin_column;
  182. int seqin_read_cmd;
  183. struct reset_control *rst;
  184. };
  185. static inline __printf(2, 3)
  186. void _nfc_dbg(struct ar934x_nfc *nfc, const char *fmt, ...)
  187. {
  188. }
  189. #ifdef AR934X_NFC_DEBUG
  190. #define nfc_dbg(_nfc, fmt, ...) \
  191. dev_info((_nfc)->parent, fmt, ##__VA_ARGS__)
  192. #else
  193. #define nfc_dbg(_nfc, fmt, ...) \
  194. _nfc_dbg((_nfc), fmt, ##__VA_ARGS__)
  195. #endif /* AR934X_NFC_DEBUG */
  196. #ifdef AR934X_NFC_DEBUG_DATA
  197. static void nfc_debug_data(const char *label, void *data, int len)
  198. {
  199. print_hex_dump(KERN_WARNING, label, DUMP_PREFIX_OFFSET, 16, 1,
  200. data, len, 0);
  201. }
  202. #else
  203. static inline void nfc_debug_data(const char *label, void *data, int len) {}
  204. #endif /* AR934X_NFC_DEBUG_DATA */
  205. static void ar934x_nfc_restart(struct ar934x_nfc *nfc);
  206. static inline bool is_all_ff(u8 *buf, int len)
  207. {
  208. while (len--)
  209. if (buf[len] != 0xff)
  210. return false;
  211. return true;
  212. }
  213. static inline void ar934x_nfc_wr(struct ar934x_nfc *nfc, unsigned reg, u32 val)
  214. {
  215. __raw_writel(val, nfc->base + reg);
  216. }
  217. static inline u32 ar934x_nfc_rr(struct ar934x_nfc *nfc, unsigned reg)
  218. {
  219. return __raw_readl(nfc->base + reg);
  220. }
  221. static inline struct ar934x_nfc *mtd_to_ar934x_nfc(struct mtd_info *mtd)
  222. {
  223. struct nand_chip *chip = mtd_to_nand(mtd);
  224. return container_of(chip, struct ar934x_nfc, nand_chip);
  225. }
  226. static struct mtd_info *ar934x_nfc_to_mtd(struct ar934x_nfc *nfc)
  227. {
  228. return nand_to_mtd(&nfc->nand_chip);
  229. }
  230. static inline bool ar934x_nfc_use_irq(struct ar934x_nfc *nfc)
  231. {
  232. return AR934X_NFC_USE_IRQ;
  233. }
  234. static inline void ar934x_nfc_write_cmd_reg(struct ar934x_nfc *nfc, u32 cmd_reg)
  235. {
  236. wmb();
  237. ar934x_nfc_wr(nfc, AR934X_NFC_REG_CMD, cmd_reg);
  238. /* flush write */
  239. ar934x_nfc_rr(nfc, AR934X_NFC_REG_CMD);
  240. }
  241. static bool __ar934x_nfc_dev_ready(struct ar934x_nfc *nfc)
  242. {
  243. u32 status;
  244. status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_STATUS);
  245. return (status & 0xff) == 0xff;
  246. }
  247. static inline bool __ar934x_nfc_is_dma_ready(struct ar934x_nfc *nfc)
  248. {
  249. u32 status;
  250. status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_DMA_CTRL);
  251. return (status & AR934X_NFC_DMA_CTRL_DMA_READY) != 0;
  252. }
  253. static int ar934x_nfc_wait_dev_ready(struct ar934x_nfc *nfc)
  254. {
  255. unsigned long timeout;
  256. timeout = jiffies + msecs_to_jiffies(AR934X_NFC_DEV_READY_TIMEOUT);
  257. do {
  258. if (__ar934x_nfc_dev_ready(nfc))
  259. return 0;
  260. } while time_before(jiffies, timeout);
  261. nfc_dbg(nfc, "timeout waiting for device ready, status:%08x int:%08x\n",
  262. ar934x_nfc_rr(nfc, AR934X_NFC_REG_STATUS),
  263. ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS));
  264. return -ETIMEDOUT;
  265. }
  266. static int ar934x_nfc_wait_dma_ready(struct ar934x_nfc *nfc)
  267. {
  268. unsigned long timeout;
  269. timeout = jiffies + msecs_to_jiffies(AR934X_NFC_DMA_READY_TIMEOUT);
  270. do {
  271. if (__ar934x_nfc_is_dma_ready(nfc))
  272. return 0;
  273. } while time_before(jiffies, timeout);
  274. nfc_dbg(nfc, "timeout waiting for DMA ready, dma_ctrl:%08x\n",
  275. ar934x_nfc_rr(nfc, AR934X_NFC_REG_DMA_CTRL));
  276. return -ETIMEDOUT;
  277. }
  278. static int ar934x_nfc_wait_irq(struct ar934x_nfc *nfc)
  279. {
  280. long timeout;
  281. int ret;
  282. timeout = wait_event_timeout(nfc->irq_waitq,
  283. (nfc->irq_status & AR934X_NFC_IRQ_MASK) != 0,
  284. msecs_to_jiffies(AR934X_NFC_DEV_READY_TIMEOUT));
  285. ret = 0;
  286. if (!timeout) {
  287. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_MASK, 0);
  288. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
  289. /* flush write */
  290. ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS);
  291. nfc_dbg(nfc,
  292. "timeout waiting for interrupt, status:%08x\n",
  293. nfc->irq_status);
  294. ret = -ETIMEDOUT;
  295. }
  296. nfc->irq_status = 0;
  297. return ret;
  298. }
  299. static int ar934x_nfc_wait_done(struct ar934x_nfc *nfc)
  300. {
  301. int ret;
  302. if (ar934x_nfc_use_irq(nfc))
  303. ret = ar934x_nfc_wait_irq(nfc);
  304. else
  305. ret = ar934x_nfc_wait_dev_ready(nfc);
  306. if (ret)
  307. return ret;
  308. return ar934x_nfc_wait_dma_ready(nfc);
  309. }
  310. static int ar934x_nfc_alloc_buf(struct ar934x_nfc *nfc, unsigned size)
  311. {
  312. nfc->buf = dma_alloc_coherent(nfc->parent, size,
  313. &nfc->buf_dma, GFP_KERNEL);
  314. if (nfc->buf == NULL) {
  315. dev_err(nfc->parent, "no memory for DMA buffer\n");
  316. return -ENOMEM;
  317. }
  318. nfc->buf_size = size;
  319. nfc_dbg(nfc, "buf:%p size:%u\n", nfc->buf, nfc->buf_size);
  320. return 0;
  321. }
  322. static void ar934x_nfc_free_buf(struct ar934x_nfc *nfc)
  323. {
  324. dma_free_coherent(nfc->parent, nfc->buf_size, nfc->buf, nfc->buf_dma);
  325. }
  326. static void ar934x_nfc_get_addr(struct ar934x_nfc *nfc, int column,
  327. int page_addr, u32 *addr0, u32 *addr1)
  328. {
  329. u32 a0, a1;
  330. a0 = 0;
  331. a1 = 0;
  332. if (column == -1) {
  333. /* ERASE1 */
  334. a0 = (page_addr & 0xffff) << 16;
  335. a1 = (page_addr >> 16) & 0xf;
  336. } else if (page_addr != -1) {
  337. /* SEQIN, READ0, etc.. */
  338. /* TODO: handle 16bit bus width */
  339. if (nfc->small_page) {
  340. a0 = column & 0xff;
  341. a0 |= (page_addr & 0xff) << 8;
  342. a0 |= ((page_addr >> 8) & 0xff) << 16;
  343. a0 |= ((page_addr >> 16) & 0xff) << 24;
  344. } else {
  345. a0 = column & 0x0FFF;
  346. a0 |= (page_addr & 0xffff) << 16;
  347. if (nfc->addr_count0 > 4)
  348. a1 = (page_addr >> 16) & 0xf;
  349. }
  350. }
  351. *addr0 = a0;
  352. *addr1 = a1;
  353. }
  354. static void ar934x_nfc_send_cmd(struct ar934x_nfc *nfc, unsigned command)
  355. {
  356. u32 cmd_reg;
  357. cmd_reg = AR934X_NFC_CMD_INPUT_SEL_SIU | AR934X_NFC_CMD_ADDR_SEL_0 |
  358. AR934X_NFC_CMD_SEQ_1C;
  359. cmd_reg |= (command & AR934X_NFC_CMD_CMD0_M) << AR934X_NFC_CMD_CMD0_S;
  360. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
  361. ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
  362. ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
  363. ar934x_nfc_wait_dev_ready(nfc);
  364. }
  365. static int ar934x_nfc_do_rw_command(struct ar934x_nfc *nfc, int column,
  366. int page_addr, int len, u32 cmd_reg,
  367. u32 ctrl_reg, bool write)
  368. {
  369. u32 addr0, addr1;
  370. u32 dma_ctrl;
  371. int dir;
  372. int err;
  373. int retries = 0;
  374. WARN_ON(len & 3);
  375. if (WARN_ON(len > nfc->buf_size))
  376. dev_err(nfc->parent, "len=%d > buf_size=%d", len,
  377. nfc->buf_size);
  378. if (write) {
  379. dma_ctrl = AR934X_NFC_DMA_CTRL_DMA_DIR_WRITE;
  380. dir = DMA_TO_DEVICE;
  381. } else {
  382. dma_ctrl = AR934X_NFC_DMA_CTRL_DMA_DIR_READ;
  383. dir = DMA_FROM_DEVICE;
  384. }
  385. ar934x_nfc_get_addr(nfc, column, page_addr, &addr0, &addr1);
  386. dma_ctrl |= AR934X_NFC_DMA_CTRL_DMA_START |
  387. (AR934X_NFC_DMA_CTRL_DMA_BURST_3 <<
  388. AR934X_NFC_DMA_CTRL_DMA_BURST_S);
  389. cmd_reg |= AR934X_NFC_CMD_INPUT_SEL_DMA | AR934X_NFC_CMD_ADDR_SEL_0;
  390. ctrl_reg |= AR934X_NFC_CTRL_INT_EN;
  391. nfc_dbg(nfc, "%s a0:%08x a1:%08x len:%x cmd:%08x dma:%08x ctrl:%08x\n",
  392. (write) ? "write" : "read",
  393. addr0, addr1, len, cmd_reg, dma_ctrl, ctrl_reg);
  394. retry:
  395. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
  396. ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_0, addr0);
  397. ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_1, addr1);
  398. ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_ADDR, nfc->buf_dma);
  399. ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_COUNT, len);
  400. ar934x_nfc_wr(nfc, AR934X_NFC_REG_DATA_SIZE, len);
  401. ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, ctrl_reg);
  402. ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_CTRL, dma_ctrl);
  403. ar934x_nfc_wr(nfc, AR934X_NFC_REG_ECC_CTRL, nfc->ecc_ctrl_reg);
  404. ar934x_nfc_wr(nfc, AR934X_NFC_REG_ECC_OFFSET, nfc->ecc_offset_reg);
  405. if (ar934x_nfc_use_irq(nfc)) {
  406. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_MASK,
  407. AR934X_NFC_IRQ_MASK);
  408. /* flush write */
  409. ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_MASK);
  410. }
  411. ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
  412. err = ar934x_nfc_wait_done(nfc);
  413. if (err) {
  414. dev_dbg(nfc->parent, "%s operation stuck at page %d\n",
  415. (write) ? "write" : "read", page_addr);
  416. ar934x_nfc_restart(nfc);
  417. if (retries++ < AR934X_NFC_DMA_RETRIES)
  418. goto retry;
  419. dev_err(nfc->parent, "%s operation failed on page %d\n",
  420. (write) ? "write" : "read", page_addr);
  421. }
  422. return err;
  423. }
  424. static int ar934x_nfc_send_readid(struct ar934x_nfc *nfc, unsigned command)
  425. {
  426. u32 cmd_reg;
  427. int err;
  428. nfc_dbg(nfc, "readid, cmd:%02x\n", command);
  429. cmd_reg = AR934X_NFC_CMD_SEQ_1C1AXR;
  430. cmd_reg |= (command & AR934X_NFC_CMD_CMD0_M) << AR934X_NFC_CMD_CMD0_S;
  431. err = ar934x_nfc_do_rw_command(nfc, -1, -1, AR934X_NFC_ID_BUF_SIZE,
  432. cmd_reg, nfc->ctrl_reg, false);
  433. nfc_debug_data("[id] ", nfc->buf, AR934X_NFC_ID_BUF_SIZE);
  434. return err;
  435. }
  436. static int ar934x_nfc_send_read(struct ar934x_nfc *nfc, unsigned command,
  437. int column, int page_addr, int len)
  438. {
  439. u32 cmd_reg;
  440. int err;
  441. nfc_dbg(nfc, "read, column=%d page=%d len=%d\n",
  442. column, page_addr, len);
  443. cmd_reg = (command & AR934X_NFC_CMD_CMD0_M) << AR934X_NFC_CMD_CMD0_S;
  444. if (nfc->small_page) {
  445. cmd_reg |= AR934X_NFC_CMD_SEQ_18;
  446. } else {
  447. cmd_reg |= NAND_CMD_READSTART << AR934X_NFC_CMD_CMD1_S;
  448. cmd_reg |= AR934X_NFC_CMD_SEQ_1C5A1CXR;
  449. }
  450. err = ar934x_nfc_do_rw_command(nfc, column, page_addr, len,
  451. cmd_reg, nfc->ctrl_reg, false);
  452. nfc_debug_data("[data] ", nfc->buf, len);
  453. return err;
  454. }
  455. static void ar934x_nfc_send_erase(struct ar934x_nfc *nfc, unsigned command,
  456. int column, int page_addr)
  457. {
  458. u32 addr0, addr1;
  459. u32 ctrl_reg;
  460. u32 cmd_reg;
  461. ar934x_nfc_get_addr(nfc, column, page_addr, &addr0, &addr1);
  462. ctrl_reg = nfc->ctrl_reg;
  463. if (nfc->small_page) {
  464. /* override number of address cycles for the erase command */
  465. ctrl_reg &= ~(AR934X_NFC_CTRL_ADDR_CYCLE0_M <<
  466. AR934X_NFC_CTRL_ADDR_CYCLE0_S);
  467. ctrl_reg &= ~(AR934X_NFC_CTRL_ADDR_CYCLE1_M <<
  468. AR934X_NFC_CTRL_ADDR_CYCLE1_S);
  469. ctrl_reg &= ~(AR934X_NFC_CTRL_SMALL_PAGE);
  470. ctrl_reg |= (nfc->addr_count0 + 1) <<
  471. AR934X_NFC_CTRL_ADDR_CYCLE0_S;
  472. }
  473. cmd_reg = NAND_CMD_ERASE1 << AR934X_NFC_CMD_CMD0_S;
  474. cmd_reg |= command << AR934X_NFC_CMD_CMD1_S;
  475. cmd_reg |= AR934X_NFC_CMD_SEQ_ERASE;
  476. nfc_dbg(nfc, "erase page %d, a0:%08x a1:%08x cmd:%08x ctrl:%08x\n",
  477. page_addr, addr0, addr1, cmd_reg, ctrl_reg);
  478. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
  479. ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, ctrl_reg);
  480. ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_0, addr0);
  481. ar934x_nfc_wr(nfc, AR934X_NFC_REG_ADDR0_1, addr1);
  482. ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
  483. ar934x_nfc_wait_dev_ready(nfc);
  484. }
  485. static int ar934x_nfc_send_write(struct ar934x_nfc *nfc, unsigned command,
  486. int column, int page_addr, int len)
  487. {
  488. u32 cmd_reg;
  489. nfc_dbg(nfc, "write, column=%d page=%d len=%d\n",
  490. column, page_addr, len);
  491. nfc_debug_data("[data] ", nfc->buf, len);
  492. cmd_reg = NAND_CMD_SEQIN << AR934X_NFC_CMD_CMD0_S;
  493. cmd_reg |= command << AR934X_NFC_CMD_CMD1_S;
  494. cmd_reg |= AR934X_NFC_CMD_SEQ_12;
  495. return ar934x_nfc_do_rw_command(nfc, column, page_addr, len,
  496. cmd_reg, nfc->ctrl_reg, true);
  497. }
  498. static void ar934x_nfc_read_status(struct ar934x_nfc *nfc)
  499. {
  500. u32 cmd_reg;
  501. u32 status;
  502. cmd_reg = NAND_CMD_STATUS << AR934X_NFC_CMD_CMD0_S;
  503. cmd_reg |= AR934X_NFC_CMD_SEQ_S;
  504. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
  505. ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
  506. ar934x_nfc_write_cmd_reg(nfc, cmd_reg);
  507. ar934x_nfc_wait_dev_ready(nfc);
  508. status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_READ_STATUS);
  509. nfc_dbg(nfc, "read status, cmd:%08x status:%02x\n",
  510. cmd_reg, (status & 0xff));
  511. if (nfc->swap_dma)
  512. nfc->buf[0 ^ 3] = status;
  513. else
  514. nfc->buf[0] = status;
  515. }
  516. static void ar934x_nfc_cmdfunc(struct nand_chip *nand, unsigned int command,
  517. int column, int page_addr)
  518. {
  519. struct mtd_info *mtd = nand_to_mtd(nand);
  520. struct ar934x_nfc *nfc = nand->priv;
  521. nfc->read_id = false;
  522. if (command != NAND_CMD_PAGEPROG)
  523. nfc->buf_index = 0;
  524. switch (command) {
  525. case NAND_CMD_RESET:
  526. ar934x_nfc_send_cmd(nfc, command);
  527. break;
  528. case NAND_CMD_READID:
  529. nfc->read_id = true;
  530. ar934x_nfc_send_readid(nfc, command);
  531. break;
  532. case NAND_CMD_READ0:
  533. case NAND_CMD_READ1:
  534. if (nfc->small_page) {
  535. ar934x_nfc_send_read(nfc, command, column, page_addr,
  536. mtd->writesize + mtd->oobsize);
  537. } else {
  538. ar934x_nfc_send_read(nfc, command, 0, page_addr,
  539. mtd->writesize + mtd->oobsize);
  540. nfc->buf_index = column;
  541. nfc->rndout_page_addr = page_addr;
  542. nfc->rndout_read_cmd = command;
  543. }
  544. break;
  545. case NAND_CMD_READOOB:
  546. if (nfc->small_page)
  547. ar934x_nfc_send_read(nfc, NAND_CMD_READOOB,
  548. column, page_addr,
  549. mtd->oobsize);
  550. else
  551. ar934x_nfc_send_read(nfc, NAND_CMD_READ0,
  552. mtd->writesize, page_addr,
  553. mtd->oobsize);
  554. break;
  555. case NAND_CMD_RNDOUT:
  556. if (WARN_ON(nfc->small_page))
  557. break;
  558. /* emulate subpage read */
  559. ar934x_nfc_send_read(nfc, nfc->rndout_read_cmd, 0,
  560. nfc->rndout_page_addr,
  561. mtd->writesize + mtd->oobsize);
  562. nfc->buf_index = column;
  563. break;
  564. case NAND_CMD_ERASE1:
  565. nfc->erase1_page_addr = page_addr;
  566. break;
  567. case NAND_CMD_ERASE2:
  568. ar934x_nfc_send_erase(nfc, command, -1, nfc->erase1_page_addr);
  569. break;
  570. case NAND_CMD_STATUS:
  571. ar934x_nfc_read_status(nfc);
  572. break;
  573. case NAND_CMD_SEQIN:
  574. if (nfc->small_page) {
  575. /* output read command */
  576. if (column >= mtd->writesize) {
  577. column -= mtd->writesize;
  578. nfc->seqin_read_cmd = NAND_CMD_READOOB;
  579. } else if (column < 256) {
  580. nfc->seqin_read_cmd = NAND_CMD_READ0;
  581. } else {
  582. column -= 256;
  583. nfc->seqin_read_cmd = NAND_CMD_READ1;
  584. }
  585. } else {
  586. nfc->seqin_read_cmd = NAND_CMD_READ0;
  587. }
  588. nfc->seqin_column = column;
  589. nfc->seqin_page_addr = page_addr;
  590. break;
  591. case NAND_CMD_PAGEPROG:
  592. if (nand->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) {
  593. /* the data is already written */
  594. break;
  595. }
  596. if (nfc->small_page)
  597. ar934x_nfc_send_cmd(nfc, nfc->seqin_read_cmd);
  598. ar934x_nfc_send_write(nfc, command, nfc->seqin_column,
  599. nfc->seqin_page_addr,
  600. nfc->buf_index);
  601. break;
  602. default:
  603. dev_err(nfc->parent,
  604. "unsupported command: %x, column:%d page_addr=%d\n",
  605. command, column, page_addr);
  606. break;
  607. }
  608. }
  609. static int ar934x_nfc_dev_ready(struct nand_chip *chip)
  610. {
  611. struct ar934x_nfc *nfc = chip->priv;
  612. return __ar934x_nfc_dev_ready(nfc);
  613. }
  614. static u8 ar934x_nfc_read_byte(struct nand_chip *chip)
  615. {
  616. struct ar934x_nfc *nfc = chip->priv;
  617. u8 data;
  618. WARN_ON(nfc->buf_index >= nfc->buf_size);
  619. if (nfc->swap_dma || nfc->read_id)
  620. data = nfc->buf[nfc->buf_index ^ 3];
  621. else
  622. data = nfc->buf[nfc->buf_index];
  623. nfc->buf_index++;
  624. return data;
  625. }
  626. static void ar934x_nfc_write_buf(struct nand_chip *chip, const u8 *buf, int len)
  627. {
  628. struct ar934x_nfc *nfc = chip->priv;
  629. int i;
  630. WARN_ON(nfc->buf_index + len > nfc->buf_size);
  631. if (nfc->swap_dma) {
  632. for (i = 0; i < len; i++) {
  633. nfc->buf[nfc->buf_index ^ 3] = buf[i];
  634. nfc->buf_index++;
  635. }
  636. } else {
  637. for (i = 0; i < len; i++) {
  638. nfc->buf[nfc->buf_index] = buf[i];
  639. nfc->buf_index++;
  640. }
  641. }
  642. }
  643. static void ar934x_nfc_read_buf(struct nand_chip *chip, u8 *buf, int len)
  644. {
  645. struct ar934x_nfc *nfc = chip->priv;
  646. int buf_index;
  647. int i;
  648. WARN_ON(nfc->buf_index + len > nfc->buf_size);
  649. buf_index = nfc->buf_index;
  650. if (nfc->swap_dma || nfc->read_id) {
  651. for (i = 0; i < len; i++) {
  652. buf[i] = nfc->buf[buf_index ^ 3];
  653. buf_index++;
  654. }
  655. } else {
  656. for (i = 0; i < len; i++) {
  657. buf[i] = nfc->buf[buf_index];
  658. buf_index++;
  659. }
  660. }
  661. nfc->buf_index = buf_index;
  662. }
  663. static inline void ar934x_nfc_enable_hwecc(struct ar934x_nfc *nfc)
  664. {
  665. nfc->ctrl_reg |= AR934X_NFC_CTRL_ECC_EN;
  666. nfc->ctrl_reg &= ~AR934X_NFC_CTRL_CUSTOM_SIZE_EN;
  667. }
  668. static inline void ar934x_nfc_disable_hwecc(struct ar934x_nfc *nfc)
  669. {
  670. nfc->ctrl_reg &= ~AR934X_NFC_CTRL_ECC_EN;
  671. nfc->ctrl_reg |= AR934X_NFC_CTRL_CUSTOM_SIZE_EN;
  672. }
  673. static int ar934x_nfc_read_oob(struct nand_chip *chip,
  674. int page)
  675. {
  676. struct ar934x_nfc *nfc = chip->priv;
  677. struct mtd_info *mtd = ar934x_nfc_to_mtd(nfc);
  678. int err;
  679. nfc_dbg(nfc, "read_oob: page:%d\n", page);
  680. err = ar934x_nfc_send_read(nfc, NAND_CMD_READ0, mtd->writesize, page,
  681. mtd->oobsize);
  682. if (err)
  683. return err;
  684. memcpy(chip->oob_poi, nfc->buf, mtd->oobsize);
  685. return 0;
  686. }
  687. static int ar934x_nfc_write_oob(struct nand_chip *chip,
  688. int page)
  689. {
  690. struct ar934x_nfc *nfc = chip->priv;
  691. struct mtd_info *mtd = ar934x_nfc_to_mtd(nfc);
  692. nfc_dbg(nfc, "write_oob: page:%d\n", page);
  693. memcpy(nfc->buf, chip->oob_poi, mtd->oobsize);
  694. return ar934x_nfc_send_write(nfc, NAND_CMD_PAGEPROG, mtd->writesize,
  695. page, mtd->oobsize);
  696. }
  697. static int ar934x_nfc_read_page_raw(
  698. struct nand_chip *chip, u8 *buf,
  699. int oob_required, int page)
  700. {
  701. struct ar934x_nfc *nfc = chip->priv;
  702. struct mtd_info *mtd = ar934x_nfc_to_mtd(nfc);
  703. int len;
  704. int err;
  705. nfc_dbg(nfc, "read_page_raw: page:%d oob:%d\n", page, oob_required);
  706. len = mtd->writesize;
  707. if (oob_required)
  708. len += mtd->oobsize;
  709. err = ar934x_nfc_send_read(nfc, NAND_CMD_READ0, 0, page, len);
  710. if (err)
  711. return err;
  712. memcpy(buf, nfc->buf, mtd->writesize);
  713. if (oob_required)
  714. memcpy(chip->oob_poi, &nfc->buf[mtd->writesize], mtd->oobsize);
  715. return 0;
  716. }
  717. static int ar934x_nfc_read_page(struct nand_chip *chip,
  718. u8 *buf, int oob_required, int page)
  719. {
  720. struct ar934x_nfc *nfc = chip->priv;
  721. struct mtd_info *mtd = ar934x_nfc_to_mtd(nfc);
  722. u32 ecc_ctrl;
  723. int max_bitflips = 0;
  724. bool ecc_failed;
  725. bool ecc_corrected;
  726. int err;
  727. nfc_dbg(nfc, "read_page: page:%d oob:%d\n", page, oob_required);
  728. ar934x_nfc_enable_hwecc(nfc);
  729. err = ar934x_nfc_send_read(nfc, NAND_CMD_READ0, 0, page,
  730. mtd->writesize);
  731. ar934x_nfc_disable_hwecc(nfc);
  732. if (err)
  733. return err;
  734. /* TODO: optimize to avoid memcpy */
  735. memcpy(buf, nfc->buf, mtd->writesize);
  736. /* read the ECC status */
  737. ecc_ctrl = ar934x_nfc_rr(nfc, AR934X_NFC_REG_ECC_CTRL);
  738. ecc_failed = ecc_ctrl & AR934X_NFC_ECC_CTRL_ERR_UNCORRECT;
  739. ecc_corrected = ecc_ctrl & AR934X_NFC_ECC_CTRL_ERR_CORRECT;
  740. if (oob_required || ecc_failed) {
  741. err = ar934x_nfc_send_read(nfc, NAND_CMD_READ0, mtd->writesize,
  742. page, mtd->oobsize);
  743. if (err)
  744. return err;
  745. if (oob_required)
  746. memcpy(chip->oob_poi, nfc->buf, mtd->oobsize);
  747. }
  748. if (ecc_failed) {
  749. /*
  750. * The hardware ECC engine reports uncorrectable errors
  751. * on empty pages. Check the ECC bytes and the data. If
  752. * both contains 0xff bytes only, dont report a failure.
  753. *
  754. * TODO: prebuild a buffer with 0xff bytes and use memcmp
  755. * for better performance?
  756. */
  757. if (!is_all_ff(&nfc->buf[nfc->ecc_oob_pos], chip->ecc.total) ||
  758. !is_all_ff(buf, mtd->writesize))
  759. mtd->ecc_stats.failed++;
  760. } else if (ecc_corrected) {
  761. /*
  762. * The hardware does not report the exact count of the
  763. * corrected bitflips, use assumptions based on the
  764. * threshold.
  765. */
  766. if (ecc_ctrl & AR934X_NFC_ECC_CTRL_ERR_OVER) {
  767. /*
  768. * The number of corrected bitflips exceeds the
  769. * threshold. Assume the maximum.
  770. */
  771. max_bitflips = chip->ecc.strength * chip->ecc.steps;
  772. } else {
  773. max_bitflips = nfc->ecc_thres * chip->ecc.steps;
  774. }
  775. mtd->ecc_stats.corrected += max_bitflips;
  776. }
  777. return max_bitflips;
  778. }
  779. static int ar934x_nfc_write_page_raw(
  780. struct nand_chip *chip, const u8 *buf,
  781. int oob_required, int page)
  782. {
  783. struct ar934x_nfc *nfc = chip->priv;
  784. struct mtd_info *mtd = ar934x_nfc_to_mtd(nfc);
  785. int len;
  786. nfc_dbg(nfc, "write_page_raw: page:%d oob:%d\n", page, oob_required);
  787. memcpy(nfc->buf, buf, mtd->writesize);
  788. len = mtd->writesize;
  789. if (oob_required) {
  790. memcpy(&nfc->buf[mtd->writesize], chip->oob_poi, mtd->oobsize);
  791. len += mtd->oobsize;
  792. }
  793. return ar934x_nfc_send_write(nfc, NAND_CMD_PAGEPROG, 0, page, len);
  794. }
  795. static int ar934x_nfc_write_page(struct nand_chip *chip,
  796. const u8 *buf, int oob_required, int page)
  797. {
  798. struct ar934x_nfc *nfc = chip->priv;
  799. struct mtd_info *mtd = ar934x_nfc_to_mtd(nfc);
  800. int err;
  801. nfc_dbg(nfc, "write_page: page:%d oob:%d\n", page, oob_required);
  802. /* write OOB first */
  803. if (oob_required &&
  804. !is_all_ff(chip->oob_poi, mtd->oobsize)) {
  805. err = ar934x_nfc_write_oob(chip, page);
  806. if (err)
  807. return err;
  808. }
  809. /* TODO: optimize to avoid memcopy */
  810. memcpy(nfc->buf, buf, mtd->writesize);
  811. ar934x_nfc_enable_hwecc(nfc);
  812. err = ar934x_nfc_send_write(nfc, NAND_CMD_PAGEPROG, 0, page,
  813. mtd->writesize);
  814. ar934x_nfc_disable_hwecc(nfc);
  815. return err;
  816. }
  817. static int ar934x_nfc_hw_reset_assert(struct ar934x_nfc *nfc)
  818. {
  819. int err;
  820. err = reset_control_assert(nfc->rst);
  821. udelay(250);
  822. return err;
  823. }
  824. static int ar934x_nfc_hw_reset_deassert(struct ar934x_nfc *nfc)
  825. {
  826. int err;
  827. err = reset_control_deassert(nfc->rst);
  828. udelay(250);
  829. return err;
  830. }
  831. static int ar934x_nfc_hw_init(struct ar934x_nfc *nfc)
  832. {
  833. ar934x_nfc_hw_reset_assert(nfc);
  834. ar934x_nfc_hw_reset_deassert(nfc);
  835. /*
  836. * setup timings
  837. * TODO: make it configurable via platform data or DT
  838. */
  839. ar934x_nfc_wr(nfc, AR934X_NFC_REG_TIME_SEQ,
  840. AR934X_NFC_TIME_SEQ_DEFAULT);
  841. ar934x_nfc_wr(nfc, AR934X_NFC_REG_TIMINGS_ASYN,
  842. AR934X_NFC_TIMINGS_ASYN_DEFAULT);
  843. ar934x_nfc_wr(nfc, AR934X_NFC_REG_TIMINGS_SYN,
  844. AR934X_NFC_TIMINGS_SYN_DEFAULT);
  845. /* disable WP on all chips, and select chip 0 */
  846. ar934x_nfc_wr(nfc, AR934X_NFC_REG_MEM_CTRL, 0xff00);
  847. ar934x_nfc_wr(nfc, AR934X_NFC_REG_DMA_ADDR_OFFS, 0);
  848. /* initialize Control register */
  849. nfc->ctrl_reg = AR934X_NFC_CTRL_CUSTOM_SIZE_EN;
  850. ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
  851. if (nfc->small_page) {
  852. /* Setup generic sequence register for small page reads. */
  853. ar934x_nfc_wr(nfc, AR934X_NFC_REG_GEN_SEQ_CTRL,
  854. AR934X_NFC_GENSEQ_SMALL_PAGE_READ);
  855. }
  856. return 0;
  857. }
  858. static void ar934x_nfc_restart(struct ar934x_nfc *nfc)
  859. {
  860. u32 ctrl_reg;
  861. ctrl_reg = nfc->ctrl_reg;
  862. ar934x_nfc_hw_init(nfc);
  863. nfc->ctrl_reg = ctrl_reg;
  864. ar934x_nfc_send_cmd(nfc, NAND_CMD_RESET);
  865. }
  866. static irqreturn_t ar934x_nfc_irq_handler(int irq, void *data)
  867. {
  868. struct ar934x_nfc *nfc = data;
  869. u32 status;
  870. status = ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS);
  871. ar934x_nfc_wr(nfc, AR934X_NFC_REG_INT_STATUS, 0);
  872. /* flush write */
  873. ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_STATUS);
  874. status &= ar934x_nfc_rr(nfc, AR934X_NFC_REG_INT_MASK);
  875. if (status) {
  876. nfc_dbg(nfc, "got IRQ, status:%08x\n", status);
  877. nfc->irq_status = status;
  878. nfc->spurious_irq_expected = true;
  879. wake_up(&nfc->irq_waitq);
  880. } else {
  881. if (nfc->spurious_irq_expected)
  882. nfc->spurious_irq_expected = false;
  883. else
  884. dev_warn(nfc->parent, "spurious interrupt\n");
  885. }
  886. return IRQ_HANDLED;
  887. }
  888. static int ar934x_nfc_init_tail(struct mtd_info *mtd)
  889. {
  890. struct ar934x_nfc *nfc = mtd_to_ar934x_nfc(mtd);
  891. struct nand_chip *chip = &nfc->nand_chip;
  892. u64 chipsize = nanddev_target_size(&chip->base);
  893. u32 ctrl;
  894. u32 t;
  895. int err;
  896. switch (mtd->oobsize) {
  897. case 16:
  898. case 64:
  899. case 128:
  900. ar934x_nfc_wr(nfc, AR934X_NFC_REG_SPARE_SIZE, mtd->oobsize);
  901. break;
  902. default:
  903. dev_err(nfc->parent, "unsupported OOB size: %d bytes\n",
  904. mtd->oobsize);
  905. return -ENXIO;
  906. }
  907. ctrl = AR934X_NFC_CTRL_CUSTOM_SIZE_EN;
  908. switch (mtd->erasesize / mtd->writesize) {
  909. case 32:
  910. t = AR934X_NFC_CTRL_BLOCK_SIZE_32;
  911. break;
  912. case 64:
  913. t = AR934X_NFC_CTRL_BLOCK_SIZE_64;
  914. break;
  915. case 128:
  916. t = AR934X_NFC_CTRL_BLOCK_SIZE_128;
  917. break;
  918. case 256:
  919. t = AR934X_NFC_CTRL_BLOCK_SIZE_256;
  920. break;
  921. default:
  922. dev_err(nfc->parent, "unsupported block size: %u\n",
  923. mtd->erasesize / mtd->writesize);
  924. return -ENXIO;
  925. }
  926. ctrl |= t << AR934X_NFC_CTRL_BLOCK_SIZE_S;
  927. switch (mtd->writesize) {
  928. case 256:
  929. nfc->small_page = 1;
  930. t = AR934X_NFC_CTRL_PAGE_SIZE_256;
  931. break;
  932. case 512:
  933. nfc->small_page = 1;
  934. t = AR934X_NFC_CTRL_PAGE_SIZE_512;
  935. break;
  936. case 1024:
  937. t = AR934X_NFC_CTRL_PAGE_SIZE_1024;
  938. break;
  939. case 2048:
  940. t = AR934X_NFC_CTRL_PAGE_SIZE_2048;
  941. break;
  942. case 4096:
  943. t = AR934X_NFC_CTRL_PAGE_SIZE_4096;
  944. break;
  945. case 8192:
  946. t = AR934X_NFC_CTRL_PAGE_SIZE_8192;
  947. break;
  948. case 16384:
  949. t = AR934X_NFC_CTRL_PAGE_SIZE_16384;
  950. break;
  951. default:
  952. dev_err(nfc->parent, "unsupported write size: %d bytes\n",
  953. mtd->writesize);
  954. return -ENXIO;
  955. }
  956. ctrl |= t << AR934X_NFC_CTRL_PAGE_SIZE_S;
  957. if (nfc->small_page) {
  958. ctrl |= AR934X_NFC_CTRL_SMALL_PAGE;
  959. if (chipsize > (32 << 20)) {
  960. nfc->addr_count0 = 4;
  961. nfc->addr_count1 = 3;
  962. } else if (chipsize > (2 << 16)) {
  963. nfc->addr_count0 = 3;
  964. nfc->addr_count1 = 2;
  965. } else {
  966. nfc->addr_count0 = 2;
  967. nfc->addr_count1 = 1;
  968. }
  969. } else {
  970. if (chipsize > (128 << 20)) {
  971. nfc->addr_count0 = 5;
  972. nfc->addr_count1 = 3;
  973. } else if (chipsize > (8 << 16)) {
  974. nfc->addr_count0 = 4;
  975. nfc->addr_count1 = 2;
  976. } else {
  977. nfc->addr_count0 = 3;
  978. nfc->addr_count1 = 1;
  979. }
  980. }
  981. ctrl |= nfc->addr_count0 << AR934X_NFC_CTRL_ADDR_CYCLE0_S;
  982. ctrl |= nfc->addr_count1 << AR934X_NFC_CTRL_ADDR_CYCLE1_S;
  983. nfc->ctrl_reg = ctrl;
  984. ar934x_nfc_wr(nfc, AR934X_NFC_REG_CTRL, nfc->ctrl_reg);
  985. ar934x_nfc_free_buf(nfc);
  986. err = ar934x_nfc_alloc_buf(nfc, mtd->writesize + mtd->oobsize);
  987. return err;
  988. }
  989. static int ar934x_nfc_ooblayout_ecc(struct mtd_info *mtd, int section,
  990. struct mtd_oob_region *oobregion)
  991. {
  992. if (section)
  993. return -ERANGE;
  994. oobregion->offset = 20;
  995. oobregion->length = 28;
  996. return 0;
  997. }
  998. static int ar934x_nfc_ooblayout_free(struct mtd_info *mtd, int section,
  999. struct mtd_oob_region *oobregion)
  1000. {
  1001. switch (section) {
  1002. case 0:
  1003. oobregion->offset = 4;
  1004. oobregion->length = 16;
  1005. return 0;
  1006. case 1:
  1007. oobregion->offset = 48;
  1008. oobregion->length = 16;
  1009. return 0;
  1010. default:
  1011. return -ERANGE;
  1012. }
  1013. }
  1014. static const struct mtd_ooblayout_ops ar934x_nfc_ecclayout_ops = {
  1015. .ecc = ar934x_nfc_ooblayout_ecc,
  1016. .free = ar934x_nfc_ooblayout_free,
  1017. };
  1018. static int ar934x_nfc_setup_hwecc(struct ar934x_nfc *nfc)
  1019. {
  1020. struct nand_chip *nand = &nfc->nand_chip;
  1021. struct mtd_info *mtd = nand_to_mtd(nand);
  1022. u32 ecc_cap;
  1023. u32 ecc_thres;
  1024. struct mtd_oob_region oobregion;
  1025. switch (mtd->writesize) {
  1026. case 2048:
  1027. /*
  1028. * Writing a subpage separately is not supported, because
  1029. * the controller only does ECC on full-page accesses.
  1030. */
  1031. nand->ecc.size = 512;
  1032. nand->ecc.bytes = 7;
  1033. nand->ecc.strength = 4;
  1034. mtd_set_ooblayout(mtd, &ar934x_nfc_ecclayout_ops);
  1035. break;
  1036. default:
  1037. dev_err(nfc->parent,
  1038. "hardware ECC is not available for %d byte pages\n",
  1039. mtd->writesize);
  1040. return -EINVAL;
  1041. }
  1042. BUG_ON(!mtd->ooblayout->ecc);
  1043. switch (nand->ecc.strength) {
  1044. case 4:
  1045. ecc_cap = AR934X_NFC_ECC_CTRL_ECC_CAP_4;
  1046. ecc_thres = 4;
  1047. break;
  1048. default:
  1049. dev_err(nfc->parent, "unsupported ECC strength %u\n",
  1050. nand->ecc.strength);
  1051. return -EINVAL;
  1052. }
  1053. nfc->ecc_thres = ecc_thres;
  1054. mtd->ooblayout->ecc(mtd, 0, &oobregion);
  1055. nfc->ecc_oob_pos = oobregion.offset;
  1056. nfc->ecc_ctrl_reg = ecc_cap << AR934X_NFC_ECC_CTRL_ECC_CAP_S;
  1057. nfc->ecc_ctrl_reg |= ecc_thres << AR934X_NFC_ECC_CTRL_ERR_THRES_S;
  1058. nfc->ecc_offset_reg = mtd->writesize + nfc->ecc_oob_pos;
  1059. nand->ecc.read_page = ar934x_nfc_read_page;
  1060. nand->ecc.read_page_raw = ar934x_nfc_read_page_raw;
  1061. nand->ecc.write_page = ar934x_nfc_write_page;
  1062. nand->ecc.write_page_raw = ar934x_nfc_write_page_raw;
  1063. nand->ecc.read_oob = ar934x_nfc_read_oob;
  1064. nand->ecc.write_oob = ar934x_nfc_write_oob;
  1065. return 0;
  1066. }
  1067. static int ar934x_nfc_attach_chip(struct nand_chip *nand)
  1068. {
  1069. struct mtd_info *mtd = nand_to_mtd(nand);
  1070. struct ar934x_nfc *nfc = nand_get_controller_data(nand);
  1071. struct device *dev = mtd->dev.parent;
  1072. int ret;
  1073. ret = ar934x_nfc_init_tail(mtd);
  1074. if (ret)
  1075. return ret;
  1076. if (mtd->writesize == 2048)
  1077. nand->options |= NAND_NO_SUBPAGE_WRITE;
  1078. if (nand->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) {
  1079. ret = ar934x_nfc_setup_hwecc(nfc);
  1080. if (ret)
  1081. return ret;
  1082. } else if (nand->ecc.engine_type != NAND_ECC_ENGINE_TYPE_SOFT) {
  1083. dev_err(dev, "unknown ECC mode %d\n", nand->ecc.engine_type);
  1084. return -EINVAL;
  1085. } else if ((nand->ecc.algo != NAND_ECC_ALGO_BCH) &&
  1086. (nand->ecc.algo != NAND_ECC_ALGO_HAMMING)) {
  1087. dev_err(dev, "unknown software ECC algo %d\n", nand->ecc.algo);
  1088. return -EINVAL;
  1089. }
  1090. return 0;
  1091. }
  1092. static u64 ar934x_nfc_dma_mask = DMA_BIT_MASK(32);
  1093. static void ar934x_nfc_cmd_ctrl(struct nand_chip *chip, int dat,
  1094. unsigned int ctrl)
  1095. {
  1096. WARN_ON(dat != NAND_CMD_NONE);
  1097. }
  1098. static const struct nand_controller_ops ar934x_nfc_controller_ops = {
  1099. .attach_chip = ar934x_nfc_attach_chip,
  1100. };
  1101. static int ar934x_nfc_probe(struct platform_device *pdev)
  1102. {
  1103. struct ar934x_nfc *nfc;
  1104. struct resource *res;
  1105. struct mtd_info *mtd;
  1106. struct nand_chip *nand;
  1107. int ret;
  1108. pdev->dev.dma_mask = &ar934x_nfc_dma_mask;
  1109. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  1110. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1111. if (!res) {
  1112. dev_err(&pdev->dev, "failed to get I/O memory\n");
  1113. return -EINVAL;
  1114. }
  1115. nfc = devm_kzalloc(&pdev->dev, sizeof(struct ar934x_nfc), GFP_KERNEL);
  1116. if (!nfc) {
  1117. dev_err(&pdev->dev, "failed to allocate driver data\n");
  1118. return -ENOMEM;
  1119. }
  1120. nfc->base = devm_ioremap_resource(&pdev->dev, res);
  1121. if (IS_ERR(nfc->base)) {
  1122. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  1123. return PTR_ERR(nfc->base);
  1124. }
  1125. nfc->irq = platform_get_irq(pdev, 0);
  1126. if (nfc->irq < 0) {
  1127. dev_err(&pdev->dev, "no IRQ resource specified\n");
  1128. return -EINVAL;
  1129. }
  1130. init_waitqueue_head(&nfc->irq_waitq);
  1131. ret = devm_request_irq(&pdev->dev, nfc->irq, ar934x_nfc_irq_handler,
  1132. 0, AR934X_NFC_DRIVER_NAME, nfc);
  1133. if (ret) {
  1134. dev_err(&pdev->dev, "devm_request_irq failed, err:%d\n", ret);
  1135. return ret;
  1136. }
  1137. nfc->rst = devm_reset_control_get(&pdev->dev, "nand");
  1138. if (IS_ERR(nfc->rst)) {
  1139. dev_err(&pdev->dev, "Failed to get reset\n");
  1140. return PTR_ERR(nfc->rst);
  1141. }
  1142. nfc->parent = &pdev->dev;
  1143. nfc->swap_dma = of_property_read_bool(pdev->dev.of_node,
  1144. "qca,nand-swap-dma");
  1145. nand = &nfc->nand_chip;
  1146. mtd = nand_to_mtd(nand);
  1147. mtd->owner = THIS_MODULE;
  1148. mtd->dev.parent = &pdev->dev;
  1149. mtd->name = AR934X_NFC_DRIVER_NAME;
  1150. nand_set_controller_data(nand, nfc);
  1151. nand_set_flash_node(nand, pdev->dev.of_node);
  1152. nand->legacy.chip_delay = 25;
  1153. nand->legacy.dev_ready = ar934x_nfc_dev_ready;
  1154. nand->legacy.cmdfunc = ar934x_nfc_cmdfunc;
  1155. nand->legacy.cmd_ctrl = ar934x_nfc_cmd_ctrl; /* dummy */
  1156. nand->legacy.read_byte = ar934x_nfc_read_byte;
  1157. nand->legacy.write_buf = ar934x_nfc_write_buf;
  1158. nand->legacy.read_buf = ar934x_nfc_read_buf;
  1159. nand->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST; /* default */
  1160. nand->priv = nfc;
  1161. platform_set_drvdata(pdev, nfc);
  1162. ret = ar934x_nfc_alloc_buf(nfc, AR934X_NFC_ID_BUF_SIZE);
  1163. if (ret)
  1164. return ret;
  1165. ret = ar934x_nfc_hw_init(nfc);
  1166. if (ret) {
  1167. dev_err(&pdev->dev, "hardware init failed, err:%d\n", ret);
  1168. goto err_free_buf;
  1169. }
  1170. nand->legacy.dummy_controller.ops = &ar934x_nfc_controller_ops;
  1171. ret = nand_scan(nand, 1);
  1172. if (ret) {
  1173. dev_err(&pdev->dev, "nand_scan failed, err:%d\n", ret);
  1174. goto err_free_buf;
  1175. }
  1176. ret = mtd_device_register(mtd, NULL, 0);
  1177. if (ret) {
  1178. dev_err(&pdev->dev, "unable to register mtd, err:%d\n", ret);
  1179. goto err_free_buf;
  1180. }
  1181. return 0;
  1182. err_free_buf:
  1183. ar934x_nfc_free_buf(nfc);
  1184. return ret;
  1185. }
  1186. static int ar934x_nfc_remove(struct platform_device *pdev)
  1187. {
  1188. struct ar934x_nfc *nfc;
  1189. nfc = platform_get_drvdata(pdev);
  1190. if (nfc) {
  1191. mtd_device_unregister(nand_to_mtd(&nfc->nand_chip));
  1192. nand_cleanup(&nfc->nand_chip);
  1193. ar934x_nfc_free_buf(nfc);
  1194. }
  1195. return 0;
  1196. }
  1197. static const struct of_device_id ar934x_nfc_match[] = {
  1198. { .compatible = "qca," AR934X_NFC_DRIVER_NAME },
  1199. {},
  1200. };
  1201. MODULE_DEVICE_TABLE(of, ar934x_nfc_match);
  1202. static struct platform_driver ar934x_nfc_driver = {
  1203. .probe = ar934x_nfc_probe,
  1204. .remove = ar934x_nfc_remove,
  1205. .driver = {
  1206. .name = AR934X_NFC_DRIVER_NAME,
  1207. .owner = THIS_MODULE,
  1208. .of_match_table = ar934x_nfc_match,
  1209. },
  1210. };
  1211. module_platform_driver(ar934x_nfc_driver);
  1212. MODULE_LICENSE("GPL v2");
  1213. MODULE_AUTHOR("Gabor Juhos <[email protected]>");
  1214. MODULE_DESCRIPTION("Atheros AR934x NAND Flash Controller driver");
  1215. MODULE_ALIAS("platform:" AR934X_NFC_DRIVER_NAME);