ar934x_nfc.c 35 KB

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