sunxi_nand.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138
  1. /*
  2. * Copyright (C) 2013 Boris BREZILLON <b.brezillon.dev@gmail.com>
  3. *
  4. * Derived from:
  5. * https://github.com/yuq/sunxi-nfc-mtd
  6. * Copyright (C) 2013 Qiang Yu <yuq825@gmail.com>
  7. *
  8. * https://github.com/hno/Allwinner-Info
  9. * Copyright (C) 2013 Henrik Nordström <Henrik Nordström>
  10. *
  11. * Copyright (C) 2013 Dmitriy B. <rzk333@gmail.com>
  12. * Copyright (C) 2013 Sergey Lapin <slapin@ossfans.org>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. */
  24. #include <linux/dma-mapping.h>
  25. #include <linux/slab.h>
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/of.h>
  30. #include <linux/of_device.h>
  31. #include <linux/mtd/mtd.h>
  32. #include <linux/mtd/rawnand.h>
  33. #include <linux/mtd/partitions.h>
  34. #include <linux/clk.h>
  35. #include <linux/delay.h>
  36. #include <linux/dmaengine.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/iopoll.h>
  39. #include <linux/reset.h>
  40. #define NFC_REG_CTL 0x0000
  41. #define NFC_REG_ST 0x0004
  42. #define NFC_REG_INT 0x0008
  43. #define NFC_REG_TIMING_CTL 0x000C
  44. #define NFC_REG_TIMING_CFG 0x0010
  45. #define NFC_REG_ADDR_LOW 0x0014
  46. #define NFC_REG_ADDR_HIGH 0x0018
  47. #define NFC_REG_SECTOR_NUM 0x001C
  48. #define NFC_REG_CNT 0x0020
  49. #define NFC_REG_CMD 0x0024
  50. #define NFC_REG_RCMD_SET 0x0028
  51. #define NFC_REG_WCMD_SET 0x002C
  52. #define NFC_REG_IO_DATA 0x0030
  53. #define NFC_REG_ECC_CTL 0x0034
  54. #define NFC_REG_ECC_ST 0x0038
  55. #define NFC_REG_DEBUG 0x003C
  56. #define NFC_REG_ECC_ERR_CNT(x) ((0x0040 + (x)) & ~0x3)
  57. #define NFC_REG_USER_DATA(x) (0x0050 + ((x) * 4))
  58. #define NFC_REG_SPARE_AREA 0x00A0
  59. #define NFC_REG_PAT_ID 0x00A4
  60. #define NFC_RAM0_BASE 0x0400
  61. #define NFC_RAM1_BASE 0x0800
  62. /* define bit use in NFC_CTL */
  63. #define NFC_EN BIT(0)
  64. #define NFC_RESET BIT(1)
  65. #define NFC_BUS_WIDTH_MSK BIT(2)
  66. #define NFC_BUS_WIDTH_8 (0 << 2)
  67. #define NFC_BUS_WIDTH_16 (1 << 2)
  68. #define NFC_RB_SEL_MSK BIT(3)
  69. #define NFC_RB_SEL(x) ((x) << 3)
  70. #define NFC_CE_SEL_MSK GENMASK(26, 24)
  71. #define NFC_CE_SEL(x) ((x) << 24)
  72. #define NFC_CE_CTL BIT(6)
  73. #define NFC_PAGE_SHIFT_MSK GENMASK(11, 8)
  74. #define NFC_PAGE_SHIFT(x) (((x) < 10 ? 0 : (x) - 10) << 8)
  75. #define NFC_SAM BIT(12)
  76. #define NFC_RAM_METHOD BIT(14)
  77. #define NFC_DEBUG_CTL BIT(31)
  78. /* define bit use in NFC_ST */
  79. #define NFC_RB_B2R BIT(0)
  80. #define NFC_CMD_INT_FLAG BIT(1)
  81. #define NFC_DMA_INT_FLAG BIT(2)
  82. #define NFC_CMD_FIFO_STATUS BIT(3)
  83. #define NFC_STA BIT(4)
  84. #define NFC_NATCH_INT_FLAG BIT(5)
  85. #define NFC_RB_STATE(x) BIT(x + 8)
  86. /* define bit use in NFC_INT */
  87. #define NFC_B2R_INT_ENABLE BIT(0)
  88. #define NFC_CMD_INT_ENABLE BIT(1)
  89. #define NFC_DMA_INT_ENABLE BIT(2)
  90. #define NFC_INT_MASK (NFC_B2R_INT_ENABLE | \
  91. NFC_CMD_INT_ENABLE | \
  92. NFC_DMA_INT_ENABLE)
  93. /* define bit use in NFC_TIMING_CTL */
  94. #define NFC_TIMING_CTL_EDO BIT(8)
  95. /* define NFC_TIMING_CFG register layout */
  96. #define NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD) \
  97. (((tWB) & 0x3) | (((tADL) & 0x3) << 2) | \
  98. (((tWHR) & 0x3) << 4) | (((tRHW) & 0x3) << 6) | \
  99. (((tCAD) & 0x7) << 8))
  100. /* define bit use in NFC_CMD */
  101. #define NFC_CMD_LOW_BYTE_MSK GENMASK(7, 0)
  102. #define NFC_CMD_HIGH_BYTE_MSK GENMASK(15, 8)
  103. #define NFC_CMD(x) (x)
  104. #define NFC_ADR_NUM_MSK GENMASK(18, 16)
  105. #define NFC_ADR_NUM(x) (((x) - 1) << 16)
  106. #define NFC_SEND_ADR BIT(19)
  107. #define NFC_ACCESS_DIR BIT(20)
  108. #define NFC_DATA_TRANS BIT(21)
  109. #define NFC_SEND_CMD1 BIT(22)
  110. #define NFC_WAIT_FLAG BIT(23)
  111. #define NFC_SEND_CMD2 BIT(24)
  112. #define NFC_SEQ BIT(25)
  113. #define NFC_DATA_SWAP_METHOD BIT(26)
  114. #define NFC_ROW_AUTO_INC BIT(27)
  115. #define NFC_SEND_CMD3 BIT(28)
  116. #define NFC_SEND_CMD4 BIT(29)
  117. #define NFC_CMD_TYPE_MSK GENMASK(31, 30)
  118. #define NFC_NORMAL_OP (0 << 30)
  119. #define NFC_ECC_OP (1 << 30)
  120. #define NFC_PAGE_OP (2U << 30)
  121. /* define bit use in NFC_RCMD_SET */
  122. #define NFC_READ_CMD_MSK GENMASK(7, 0)
  123. #define NFC_RND_READ_CMD0_MSK GENMASK(15, 8)
  124. #define NFC_RND_READ_CMD1_MSK GENMASK(23, 16)
  125. /* define bit use in NFC_WCMD_SET */
  126. #define NFC_PROGRAM_CMD_MSK GENMASK(7, 0)
  127. #define NFC_RND_WRITE_CMD_MSK GENMASK(15, 8)
  128. #define NFC_READ_CMD0_MSK GENMASK(23, 16)
  129. #define NFC_READ_CMD1_MSK GENMASK(31, 24)
  130. /* define bit use in NFC_ECC_CTL */
  131. #define NFC_ECC_EN BIT(0)
  132. #define NFC_ECC_PIPELINE BIT(3)
  133. #define NFC_ECC_EXCEPTION BIT(4)
  134. #define NFC_ECC_BLOCK_SIZE_MSK BIT(5)
  135. #define NFC_ECC_BLOCK_512 BIT(5)
  136. #define NFC_RANDOM_EN BIT(9)
  137. #define NFC_RANDOM_DIRECTION BIT(10)
  138. #define NFC_ECC_MODE_MSK GENMASK(15, 12)
  139. #define NFC_ECC_MODE(x) ((x) << 12)
  140. #define NFC_RANDOM_SEED_MSK GENMASK(30, 16)
  141. #define NFC_RANDOM_SEED(x) ((x) << 16)
  142. /* define bit use in NFC_ECC_ST */
  143. #define NFC_ECC_ERR(x) BIT(x)
  144. #define NFC_ECC_ERR_MSK GENMASK(15, 0)
  145. #define NFC_ECC_PAT_FOUND(x) BIT(x + 16)
  146. #define NFC_ECC_ERR_CNT(b, x) (((x) >> (((b) % 4) * 8)) & 0xff)
  147. #define NFC_DEFAULT_TIMEOUT_MS 1000
  148. #define NFC_SRAM_SIZE 1024
  149. #define NFC_MAX_CS 7
  150. /*
  151. * Chip Select structure: stores information related to NAND Chip Select
  152. *
  153. * @cs: the NAND CS id used to communicate with a NAND Chip
  154. * @rb: the Ready/Busy pin ID. -1 means no R/B pin connected to the
  155. * NFC
  156. */
  157. struct sunxi_nand_chip_sel {
  158. u8 cs;
  159. s8 rb;
  160. };
  161. /*
  162. * sunxi HW ECC infos: stores information related to HW ECC support
  163. *
  164. * @mode: the sunxi ECC mode field deduced from ECC requirements
  165. */
  166. struct sunxi_nand_hw_ecc {
  167. int mode;
  168. };
  169. /*
  170. * NAND chip structure: stores NAND chip device related information
  171. *
  172. * @node: used to store NAND chips into a list
  173. * @nand: base NAND chip structure
  174. * @mtd: base MTD structure
  175. * @clk_rate: clk_rate required for this NAND chip
  176. * @timing_cfg TIMING_CFG register value for this NAND chip
  177. * @selected: current active CS
  178. * @nsels: number of CS lines required by the NAND chip
  179. * @sels: array of CS lines descriptions
  180. */
  181. struct sunxi_nand_chip {
  182. struct list_head node;
  183. struct nand_chip nand;
  184. unsigned long clk_rate;
  185. u32 timing_cfg;
  186. u32 timing_ctl;
  187. int selected;
  188. int addr_cycles;
  189. u32 addr[2];
  190. int cmd_cycles;
  191. u8 cmd[2];
  192. int nsels;
  193. struct sunxi_nand_chip_sel sels[0];
  194. };
  195. static inline struct sunxi_nand_chip *to_sunxi_nand(struct nand_chip *nand)
  196. {
  197. return container_of(nand, struct sunxi_nand_chip, nand);
  198. }
  199. /*
  200. * NAND Controller structure: stores sunxi NAND controller information
  201. *
  202. * @controller: base controller structure
  203. * @dev: parent device (used to print error messages)
  204. * @regs: NAND controller registers
  205. * @ahb_clk: NAND Controller AHB clock
  206. * @mod_clk: NAND Controller mod clock
  207. * @assigned_cs: bitmask describing already assigned CS lines
  208. * @clk_rate: NAND controller current clock rate
  209. * @chips: a list containing all the NAND chips attached to
  210. * this NAND controller
  211. * @complete: a completion object used to wait for NAND
  212. * controller events
  213. */
  214. struct sunxi_nfc {
  215. struct nand_controller controller;
  216. struct device *dev;
  217. void __iomem *regs;
  218. struct clk *ahb_clk;
  219. struct clk *mod_clk;
  220. struct reset_control *reset;
  221. unsigned long assigned_cs;
  222. unsigned long clk_rate;
  223. struct list_head chips;
  224. struct completion complete;
  225. struct dma_chan *dmac;
  226. };
  227. static inline struct sunxi_nfc *to_sunxi_nfc(struct nand_controller *ctrl)
  228. {
  229. return container_of(ctrl, struct sunxi_nfc, controller);
  230. }
  231. static irqreturn_t sunxi_nfc_interrupt(int irq, void *dev_id)
  232. {
  233. struct sunxi_nfc *nfc = dev_id;
  234. u32 st = readl(nfc->regs + NFC_REG_ST);
  235. u32 ien = readl(nfc->regs + NFC_REG_INT);
  236. if (!(ien & st))
  237. return IRQ_NONE;
  238. if ((ien & st) == ien)
  239. complete(&nfc->complete);
  240. writel(st & NFC_INT_MASK, nfc->regs + NFC_REG_ST);
  241. writel(~st & ien & NFC_INT_MASK, nfc->regs + NFC_REG_INT);
  242. return IRQ_HANDLED;
  243. }
  244. static int sunxi_nfc_wait_events(struct sunxi_nfc *nfc, u32 events,
  245. bool use_polling, unsigned int timeout_ms)
  246. {
  247. int ret;
  248. if (events & ~NFC_INT_MASK)
  249. return -EINVAL;
  250. if (!timeout_ms)
  251. timeout_ms = NFC_DEFAULT_TIMEOUT_MS;
  252. if (!use_polling) {
  253. init_completion(&nfc->complete);
  254. writel(events, nfc->regs + NFC_REG_INT);
  255. ret = wait_for_completion_timeout(&nfc->complete,
  256. msecs_to_jiffies(timeout_ms));
  257. if (!ret)
  258. ret = -ETIMEDOUT;
  259. else
  260. ret = 0;
  261. writel(0, nfc->regs + NFC_REG_INT);
  262. } else {
  263. u32 status;
  264. ret = readl_poll_timeout(nfc->regs + NFC_REG_ST, status,
  265. (status & events) == events, 1,
  266. timeout_ms * 1000);
  267. }
  268. writel(events & NFC_INT_MASK, nfc->regs + NFC_REG_ST);
  269. if (ret)
  270. dev_err(nfc->dev, "wait interrupt timedout\n");
  271. return ret;
  272. }
  273. static int sunxi_nfc_wait_cmd_fifo_empty(struct sunxi_nfc *nfc)
  274. {
  275. u32 status;
  276. int ret;
  277. ret = readl_poll_timeout(nfc->regs + NFC_REG_ST, status,
  278. !(status & NFC_CMD_FIFO_STATUS), 1,
  279. NFC_DEFAULT_TIMEOUT_MS * 1000);
  280. if (ret)
  281. dev_err(nfc->dev, "wait for empty cmd FIFO timedout\n");
  282. return ret;
  283. }
  284. static int sunxi_nfc_rst(struct sunxi_nfc *nfc)
  285. {
  286. u32 ctl;
  287. int ret;
  288. writel(0, nfc->regs + NFC_REG_ECC_CTL);
  289. writel(NFC_RESET, nfc->regs + NFC_REG_CTL);
  290. ret = readl_poll_timeout(nfc->regs + NFC_REG_CTL, ctl,
  291. !(ctl & NFC_RESET), 1,
  292. NFC_DEFAULT_TIMEOUT_MS * 1000);
  293. if (ret)
  294. dev_err(nfc->dev, "wait for NAND controller reset timedout\n");
  295. return ret;
  296. }
  297. static int sunxi_nfc_dma_op_prepare(struct mtd_info *mtd, const void *buf,
  298. int chunksize, int nchunks,
  299. enum dma_data_direction ddir,
  300. struct scatterlist *sg)
  301. {
  302. struct nand_chip *nand = mtd_to_nand(mtd);
  303. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  304. struct dma_async_tx_descriptor *dmad;
  305. enum dma_transfer_direction tdir;
  306. dma_cookie_t dmat;
  307. int ret;
  308. if (ddir == DMA_FROM_DEVICE)
  309. tdir = DMA_DEV_TO_MEM;
  310. else
  311. tdir = DMA_MEM_TO_DEV;
  312. sg_init_one(sg, buf, nchunks * chunksize);
  313. ret = dma_map_sg(nfc->dev, sg, 1, ddir);
  314. if (!ret)
  315. return -ENOMEM;
  316. dmad = dmaengine_prep_slave_sg(nfc->dmac, sg, 1, tdir, DMA_CTRL_ACK);
  317. if (!dmad) {
  318. ret = -EINVAL;
  319. goto err_unmap_buf;
  320. }
  321. writel(readl(nfc->regs + NFC_REG_CTL) | NFC_RAM_METHOD,
  322. nfc->regs + NFC_REG_CTL);
  323. writel(nchunks, nfc->regs + NFC_REG_SECTOR_NUM);
  324. writel(chunksize, nfc->regs + NFC_REG_CNT);
  325. dmat = dmaengine_submit(dmad);
  326. ret = dma_submit_error(dmat);
  327. if (ret)
  328. goto err_clr_dma_flag;
  329. return 0;
  330. err_clr_dma_flag:
  331. writel(readl(nfc->regs + NFC_REG_CTL) & ~NFC_RAM_METHOD,
  332. nfc->regs + NFC_REG_CTL);
  333. err_unmap_buf:
  334. dma_unmap_sg(nfc->dev, sg, 1, ddir);
  335. return ret;
  336. }
  337. static void sunxi_nfc_dma_op_cleanup(struct mtd_info *mtd,
  338. enum dma_data_direction ddir,
  339. struct scatterlist *sg)
  340. {
  341. struct nand_chip *nand = mtd_to_nand(mtd);
  342. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  343. dma_unmap_sg(nfc->dev, sg, 1, ddir);
  344. writel(readl(nfc->regs + NFC_REG_CTL) & ~NFC_RAM_METHOD,
  345. nfc->regs + NFC_REG_CTL);
  346. }
  347. static int sunxi_nfc_dev_ready(struct mtd_info *mtd)
  348. {
  349. struct nand_chip *nand = mtd_to_nand(mtd);
  350. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  351. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  352. u32 mask;
  353. if (sunxi_nand->selected < 0)
  354. return 0;
  355. if (sunxi_nand->sels[sunxi_nand->selected].rb < 0) {
  356. dev_err(nfc->dev, "cannot check R/B NAND status!\n");
  357. return 0;
  358. }
  359. mask = NFC_RB_STATE(sunxi_nand->sels[sunxi_nand->selected].rb);
  360. return !!(readl(nfc->regs + NFC_REG_ST) & mask);
  361. }
  362. static void sunxi_nfc_select_chip(struct mtd_info *mtd, int chip)
  363. {
  364. struct nand_chip *nand = mtd_to_nand(mtd);
  365. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  366. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  367. struct sunxi_nand_chip_sel *sel;
  368. u32 ctl;
  369. if (chip > 0 && chip >= sunxi_nand->nsels)
  370. return;
  371. if (chip == sunxi_nand->selected)
  372. return;
  373. ctl = readl(nfc->regs + NFC_REG_CTL) &
  374. ~(NFC_PAGE_SHIFT_MSK | NFC_CE_SEL_MSK | NFC_RB_SEL_MSK | NFC_EN);
  375. if (chip >= 0) {
  376. sel = &sunxi_nand->sels[chip];
  377. ctl |= NFC_CE_SEL(sel->cs) | NFC_EN |
  378. NFC_PAGE_SHIFT(nand->page_shift);
  379. if (sel->rb < 0) {
  380. nand->dev_ready = NULL;
  381. } else {
  382. nand->dev_ready = sunxi_nfc_dev_ready;
  383. ctl |= NFC_RB_SEL(sel->rb);
  384. }
  385. writel(mtd->writesize, nfc->regs + NFC_REG_SPARE_AREA);
  386. if (nfc->clk_rate != sunxi_nand->clk_rate) {
  387. clk_set_rate(nfc->mod_clk, sunxi_nand->clk_rate);
  388. nfc->clk_rate = sunxi_nand->clk_rate;
  389. }
  390. }
  391. writel(sunxi_nand->timing_ctl, nfc->regs + NFC_REG_TIMING_CTL);
  392. writel(sunxi_nand->timing_cfg, nfc->regs + NFC_REG_TIMING_CFG);
  393. writel(ctl, nfc->regs + NFC_REG_CTL);
  394. sunxi_nand->selected = chip;
  395. }
  396. static void sunxi_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  397. {
  398. struct nand_chip *nand = mtd_to_nand(mtd);
  399. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  400. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  401. int ret;
  402. int cnt;
  403. int offs = 0;
  404. u32 tmp;
  405. while (len > offs) {
  406. bool poll = false;
  407. cnt = min(len - offs, NFC_SRAM_SIZE);
  408. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  409. if (ret)
  410. break;
  411. writel(cnt, nfc->regs + NFC_REG_CNT);
  412. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD;
  413. writel(tmp, nfc->regs + NFC_REG_CMD);
  414. /* Arbitrary limit for polling mode */
  415. if (cnt < 64)
  416. poll = true;
  417. ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, poll, 0);
  418. if (ret)
  419. break;
  420. if (buf)
  421. memcpy_fromio(buf + offs, nfc->regs + NFC_RAM0_BASE,
  422. cnt);
  423. offs += cnt;
  424. }
  425. }
  426. static void sunxi_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  427. int len)
  428. {
  429. struct nand_chip *nand = mtd_to_nand(mtd);
  430. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  431. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  432. int ret;
  433. int cnt;
  434. int offs = 0;
  435. u32 tmp;
  436. while (len > offs) {
  437. bool poll = false;
  438. cnt = min(len - offs, NFC_SRAM_SIZE);
  439. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  440. if (ret)
  441. break;
  442. writel(cnt, nfc->regs + NFC_REG_CNT);
  443. memcpy_toio(nfc->regs + NFC_RAM0_BASE, buf + offs, cnt);
  444. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD |
  445. NFC_ACCESS_DIR;
  446. writel(tmp, nfc->regs + NFC_REG_CMD);
  447. /* Arbitrary limit for polling mode */
  448. if (cnt < 64)
  449. poll = true;
  450. ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, poll, 0);
  451. if (ret)
  452. break;
  453. offs += cnt;
  454. }
  455. }
  456. static uint8_t sunxi_nfc_read_byte(struct mtd_info *mtd)
  457. {
  458. uint8_t ret = 0;
  459. sunxi_nfc_read_buf(mtd, &ret, 1);
  460. return ret;
  461. }
  462. static void sunxi_nfc_cmd_ctrl(struct mtd_info *mtd, int dat,
  463. unsigned int ctrl)
  464. {
  465. struct nand_chip *nand = mtd_to_nand(mtd);
  466. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  467. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  468. int ret;
  469. if (dat == NAND_CMD_NONE && (ctrl & NAND_NCE) &&
  470. !(ctrl & (NAND_CLE | NAND_ALE))) {
  471. u32 cmd = 0;
  472. if (!sunxi_nand->addr_cycles && !sunxi_nand->cmd_cycles)
  473. return;
  474. if (sunxi_nand->cmd_cycles--)
  475. cmd |= NFC_SEND_CMD1 | sunxi_nand->cmd[0];
  476. if (sunxi_nand->cmd_cycles--) {
  477. cmd |= NFC_SEND_CMD2;
  478. writel(sunxi_nand->cmd[1],
  479. nfc->regs + NFC_REG_RCMD_SET);
  480. }
  481. sunxi_nand->cmd_cycles = 0;
  482. if (sunxi_nand->addr_cycles) {
  483. cmd |= NFC_SEND_ADR |
  484. NFC_ADR_NUM(sunxi_nand->addr_cycles);
  485. writel(sunxi_nand->addr[0],
  486. nfc->regs + NFC_REG_ADDR_LOW);
  487. }
  488. if (sunxi_nand->addr_cycles > 4)
  489. writel(sunxi_nand->addr[1],
  490. nfc->regs + NFC_REG_ADDR_HIGH);
  491. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  492. if (ret)
  493. return;
  494. writel(cmd, nfc->regs + NFC_REG_CMD);
  495. sunxi_nand->addr[0] = 0;
  496. sunxi_nand->addr[1] = 0;
  497. sunxi_nand->addr_cycles = 0;
  498. sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, true, 0);
  499. }
  500. if (ctrl & NAND_CLE) {
  501. sunxi_nand->cmd[sunxi_nand->cmd_cycles++] = dat;
  502. } else if (ctrl & NAND_ALE) {
  503. sunxi_nand->addr[sunxi_nand->addr_cycles / 4] |=
  504. dat << ((sunxi_nand->addr_cycles % 4) * 8);
  505. sunxi_nand->addr_cycles++;
  506. }
  507. }
  508. /* These seed values have been extracted from Allwinner's BSP */
  509. static const u16 sunxi_nfc_randomizer_page_seeds[] = {
  510. 0x2b75, 0x0bd0, 0x5ca3, 0x62d1, 0x1c93, 0x07e9, 0x2162, 0x3a72,
  511. 0x0d67, 0x67f9, 0x1be7, 0x077d, 0x032f, 0x0dac, 0x2716, 0x2436,
  512. 0x7922, 0x1510, 0x3860, 0x5287, 0x480f, 0x4252, 0x1789, 0x5a2d,
  513. 0x2a49, 0x5e10, 0x437f, 0x4b4e, 0x2f45, 0x216e, 0x5cb7, 0x7130,
  514. 0x2a3f, 0x60e4, 0x4dc9, 0x0ef0, 0x0f52, 0x1bb9, 0x6211, 0x7a56,
  515. 0x226d, 0x4ea7, 0x6f36, 0x3692, 0x38bf, 0x0c62, 0x05eb, 0x4c55,
  516. 0x60f4, 0x728c, 0x3b6f, 0x2037, 0x7f69, 0x0936, 0x651a, 0x4ceb,
  517. 0x6218, 0x79f3, 0x383f, 0x18d9, 0x4f05, 0x5c82, 0x2912, 0x6f17,
  518. 0x6856, 0x5938, 0x1007, 0x61ab, 0x3e7f, 0x57c2, 0x542f, 0x4f62,
  519. 0x7454, 0x2eac, 0x7739, 0x42d4, 0x2f90, 0x435a, 0x2e52, 0x2064,
  520. 0x637c, 0x66ad, 0x2c90, 0x0bad, 0x759c, 0x0029, 0x0986, 0x7126,
  521. 0x1ca7, 0x1605, 0x386a, 0x27f5, 0x1380, 0x6d75, 0x24c3, 0x0f8e,
  522. 0x2b7a, 0x1418, 0x1fd1, 0x7dc1, 0x2d8e, 0x43af, 0x2267, 0x7da3,
  523. 0x4e3d, 0x1338, 0x50db, 0x454d, 0x764d, 0x40a3, 0x42e6, 0x262b,
  524. 0x2d2e, 0x1aea, 0x2e17, 0x173d, 0x3a6e, 0x71bf, 0x25f9, 0x0a5d,
  525. 0x7c57, 0x0fbe, 0x46ce, 0x4939, 0x6b17, 0x37bb, 0x3e91, 0x76db,
  526. };
  527. /*
  528. * sunxi_nfc_randomizer_ecc512_seeds and sunxi_nfc_randomizer_ecc1024_seeds
  529. * have been generated using
  530. * sunxi_nfc_randomizer_step(seed, (step_size * 8) + 15), which is what
  531. * the randomizer engine does internally before de/scrambling OOB data.
  532. *
  533. * Those tables are statically defined to avoid calculating randomizer state
  534. * at runtime.
  535. */
  536. static const u16 sunxi_nfc_randomizer_ecc512_seeds[] = {
  537. 0x3346, 0x367f, 0x1f18, 0x769a, 0x4f64, 0x068c, 0x2ef1, 0x6b64,
  538. 0x28a9, 0x15d7, 0x30f8, 0x3659, 0x53db, 0x7c5f, 0x71d4, 0x4409,
  539. 0x26eb, 0x03cc, 0x655d, 0x47d4, 0x4daa, 0x0877, 0x712d, 0x3617,
  540. 0x3264, 0x49aa, 0x7f9e, 0x588e, 0x4fbc, 0x7176, 0x7f91, 0x6c6d,
  541. 0x4b95, 0x5fb7, 0x3844, 0x4037, 0x0184, 0x081b, 0x0ee8, 0x5b91,
  542. 0x293d, 0x1f71, 0x0e6f, 0x402b, 0x5122, 0x1e52, 0x22be, 0x3d2d,
  543. 0x75bc, 0x7c60, 0x6291, 0x1a2f, 0x61d4, 0x74aa, 0x4140, 0x29ab,
  544. 0x472d, 0x2852, 0x017e, 0x15e8, 0x5ec2, 0x17cf, 0x7d0f, 0x06b8,
  545. 0x117a, 0x6b94, 0x789b, 0x3126, 0x6ac5, 0x5be7, 0x150f, 0x51f8,
  546. 0x7889, 0x0aa5, 0x663d, 0x77e8, 0x0b87, 0x3dcb, 0x360d, 0x218b,
  547. 0x512f, 0x7dc9, 0x6a4d, 0x630a, 0x3547, 0x1dd2, 0x5aea, 0x69a5,
  548. 0x7bfa, 0x5e4f, 0x1519, 0x6430, 0x3a0e, 0x5eb3, 0x5425, 0x0c7a,
  549. 0x5540, 0x3670, 0x63c1, 0x31e9, 0x5a39, 0x2de7, 0x5979, 0x2891,
  550. 0x1562, 0x014b, 0x5b05, 0x2756, 0x5a34, 0x13aa, 0x6cb5, 0x2c36,
  551. 0x5e72, 0x1306, 0x0861, 0x15ef, 0x1ee8, 0x5a37, 0x7ac4, 0x45dd,
  552. 0x44c4, 0x7266, 0x2f41, 0x3ccc, 0x045e, 0x7d40, 0x7c66, 0x0fa0,
  553. };
  554. static const u16 sunxi_nfc_randomizer_ecc1024_seeds[] = {
  555. 0x2cf5, 0x35f1, 0x63a4, 0x5274, 0x2bd2, 0x778b, 0x7285, 0x32b6,
  556. 0x6a5c, 0x70d6, 0x757d, 0x6769, 0x5375, 0x1e81, 0x0cf3, 0x3982,
  557. 0x6787, 0x042a, 0x6c49, 0x1925, 0x56a8, 0x40a9, 0x063e, 0x7bd9,
  558. 0x4dbf, 0x55ec, 0x672e, 0x7334, 0x5185, 0x4d00, 0x232a, 0x7e07,
  559. 0x445d, 0x6b92, 0x528f, 0x4255, 0x53ba, 0x7d82, 0x2a2e, 0x3a4e,
  560. 0x75eb, 0x450c, 0x6844, 0x1b5d, 0x581a, 0x4cc6, 0x0379, 0x37b2,
  561. 0x419f, 0x0e92, 0x6b27, 0x5624, 0x01e3, 0x07c1, 0x44a5, 0x130c,
  562. 0x13e8, 0x5910, 0x0876, 0x60c5, 0x54e3, 0x5b7f, 0x2269, 0x509f,
  563. 0x7665, 0x36fd, 0x3e9a, 0x0579, 0x6295, 0x14ef, 0x0a81, 0x1bcc,
  564. 0x4b16, 0x64db, 0x0514, 0x4f07, 0x0591, 0x3576, 0x6853, 0x0d9e,
  565. 0x259f, 0x38b7, 0x64fb, 0x3094, 0x4693, 0x6ddd, 0x29bb, 0x0bc8,
  566. 0x3f47, 0x490e, 0x0c0e, 0x7933, 0x3c9e, 0x5840, 0x398d, 0x3e68,
  567. 0x4af1, 0x71f5, 0x57cf, 0x1121, 0x64eb, 0x3579, 0x15ac, 0x584d,
  568. 0x5f2a, 0x47e2, 0x6528, 0x6eac, 0x196e, 0x6b96, 0x0450, 0x0179,
  569. 0x609c, 0x06e1, 0x4626, 0x42c7, 0x273e, 0x486f, 0x0705, 0x1601,
  570. 0x145b, 0x407e, 0x062b, 0x57a5, 0x53f9, 0x5659, 0x4410, 0x3ccd,
  571. };
  572. static u16 sunxi_nfc_randomizer_step(u16 state, int count)
  573. {
  574. state &= 0x7fff;
  575. /*
  576. * This loop is just a simple implementation of a Fibonacci LFSR using
  577. * the x16 + x15 + 1 polynomial.
  578. */
  579. while (count--)
  580. state = ((state >> 1) |
  581. (((state ^ (state >> 1)) & 1) << 14)) & 0x7fff;
  582. return state;
  583. }
  584. static u16 sunxi_nfc_randomizer_state(struct mtd_info *mtd, int page, bool ecc)
  585. {
  586. const u16 *seeds = sunxi_nfc_randomizer_page_seeds;
  587. int mod = mtd_div_by_ws(mtd->erasesize, mtd);
  588. if (mod > ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds))
  589. mod = ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds);
  590. if (ecc) {
  591. if (mtd->ecc_step_size == 512)
  592. seeds = sunxi_nfc_randomizer_ecc512_seeds;
  593. else
  594. seeds = sunxi_nfc_randomizer_ecc1024_seeds;
  595. }
  596. return seeds[page % mod];
  597. }
  598. static void sunxi_nfc_randomizer_config(struct mtd_info *mtd,
  599. int page, bool ecc)
  600. {
  601. struct nand_chip *nand = mtd_to_nand(mtd);
  602. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  603. u32 ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL);
  604. u16 state;
  605. if (!(nand->options & NAND_NEED_SCRAMBLING))
  606. return;
  607. ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL);
  608. state = sunxi_nfc_randomizer_state(mtd, page, ecc);
  609. ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_RANDOM_SEED_MSK;
  610. writel(ecc_ctl | NFC_RANDOM_SEED(state), nfc->regs + NFC_REG_ECC_CTL);
  611. }
  612. static void sunxi_nfc_randomizer_enable(struct mtd_info *mtd)
  613. {
  614. struct nand_chip *nand = mtd_to_nand(mtd);
  615. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  616. if (!(nand->options & NAND_NEED_SCRAMBLING))
  617. return;
  618. writel(readl(nfc->regs + NFC_REG_ECC_CTL) | NFC_RANDOM_EN,
  619. nfc->regs + NFC_REG_ECC_CTL);
  620. }
  621. static void sunxi_nfc_randomizer_disable(struct mtd_info *mtd)
  622. {
  623. struct nand_chip *nand = mtd_to_nand(mtd);
  624. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  625. if (!(nand->options & NAND_NEED_SCRAMBLING))
  626. return;
  627. writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_RANDOM_EN,
  628. nfc->regs + NFC_REG_ECC_CTL);
  629. }
  630. static void sunxi_nfc_randomize_bbm(struct mtd_info *mtd, int page, u8 *bbm)
  631. {
  632. u16 state = sunxi_nfc_randomizer_state(mtd, page, true);
  633. bbm[0] ^= state;
  634. bbm[1] ^= sunxi_nfc_randomizer_step(state, 8);
  635. }
  636. static void sunxi_nfc_randomizer_write_buf(struct mtd_info *mtd,
  637. const uint8_t *buf, int len,
  638. bool ecc, int page)
  639. {
  640. sunxi_nfc_randomizer_config(mtd, page, ecc);
  641. sunxi_nfc_randomizer_enable(mtd);
  642. sunxi_nfc_write_buf(mtd, buf, len);
  643. sunxi_nfc_randomizer_disable(mtd);
  644. }
  645. static void sunxi_nfc_randomizer_read_buf(struct mtd_info *mtd, uint8_t *buf,
  646. int len, bool ecc, int page)
  647. {
  648. sunxi_nfc_randomizer_config(mtd, page, ecc);
  649. sunxi_nfc_randomizer_enable(mtd);
  650. sunxi_nfc_read_buf(mtd, buf, len);
  651. sunxi_nfc_randomizer_disable(mtd);
  652. }
  653. static void sunxi_nfc_hw_ecc_enable(struct mtd_info *mtd)
  654. {
  655. struct nand_chip *nand = mtd_to_nand(mtd);
  656. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  657. struct sunxi_nand_hw_ecc *data = nand->ecc.priv;
  658. u32 ecc_ctl;
  659. ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL);
  660. ecc_ctl &= ~(NFC_ECC_MODE_MSK | NFC_ECC_PIPELINE |
  661. NFC_ECC_BLOCK_SIZE_MSK);
  662. ecc_ctl |= NFC_ECC_EN | NFC_ECC_MODE(data->mode) | NFC_ECC_EXCEPTION |
  663. NFC_ECC_PIPELINE;
  664. if (nand->ecc.size == 512)
  665. ecc_ctl |= NFC_ECC_BLOCK_512;
  666. writel(ecc_ctl, nfc->regs + NFC_REG_ECC_CTL);
  667. }
  668. static void sunxi_nfc_hw_ecc_disable(struct mtd_info *mtd)
  669. {
  670. struct nand_chip *nand = mtd_to_nand(mtd);
  671. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  672. writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_ECC_EN,
  673. nfc->regs + NFC_REG_ECC_CTL);
  674. }
  675. static inline void sunxi_nfc_user_data_to_buf(u32 user_data, u8 *buf)
  676. {
  677. buf[0] = user_data;
  678. buf[1] = user_data >> 8;
  679. buf[2] = user_data >> 16;
  680. buf[3] = user_data >> 24;
  681. }
  682. static inline u32 sunxi_nfc_buf_to_user_data(const u8 *buf)
  683. {
  684. return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  685. }
  686. static void sunxi_nfc_hw_ecc_get_prot_oob_bytes(struct mtd_info *mtd, u8 *oob,
  687. int step, bool bbm, int page)
  688. {
  689. struct nand_chip *nand = mtd_to_nand(mtd);
  690. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  691. sunxi_nfc_user_data_to_buf(readl(nfc->regs + NFC_REG_USER_DATA(step)),
  692. oob);
  693. /* De-randomize the Bad Block Marker. */
  694. if (bbm && (nand->options & NAND_NEED_SCRAMBLING))
  695. sunxi_nfc_randomize_bbm(mtd, page, oob);
  696. }
  697. static void sunxi_nfc_hw_ecc_set_prot_oob_bytes(struct mtd_info *mtd,
  698. const u8 *oob, int step,
  699. bool bbm, int page)
  700. {
  701. struct nand_chip *nand = mtd_to_nand(mtd);
  702. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  703. u8 user_data[4];
  704. /* Randomize the Bad Block Marker. */
  705. if (bbm && (nand->options & NAND_NEED_SCRAMBLING)) {
  706. memcpy(user_data, oob, sizeof(user_data));
  707. sunxi_nfc_randomize_bbm(mtd, page, user_data);
  708. oob = user_data;
  709. }
  710. writel(sunxi_nfc_buf_to_user_data(oob),
  711. nfc->regs + NFC_REG_USER_DATA(step));
  712. }
  713. static void sunxi_nfc_hw_ecc_update_stats(struct mtd_info *mtd,
  714. unsigned int *max_bitflips, int ret)
  715. {
  716. if (ret < 0) {
  717. mtd->ecc_stats.failed++;
  718. } else {
  719. mtd->ecc_stats.corrected += ret;
  720. *max_bitflips = max_t(unsigned int, *max_bitflips, ret);
  721. }
  722. }
  723. static int sunxi_nfc_hw_ecc_correct(struct mtd_info *mtd, u8 *data, u8 *oob,
  724. int step, u32 status, bool *erased)
  725. {
  726. struct nand_chip *nand = mtd_to_nand(mtd);
  727. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  728. struct nand_ecc_ctrl *ecc = &nand->ecc;
  729. u32 tmp;
  730. *erased = false;
  731. if (status & NFC_ECC_ERR(step))
  732. return -EBADMSG;
  733. if (status & NFC_ECC_PAT_FOUND(step)) {
  734. u8 pattern;
  735. if (unlikely(!(readl(nfc->regs + NFC_REG_PAT_ID) & 0x1))) {
  736. pattern = 0x0;
  737. } else {
  738. pattern = 0xff;
  739. *erased = true;
  740. }
  741. if (data)
  742. memset(data, pattern, ecc->size);
  743. if (oob)
  744. memset(oob, pattern, ecc->bytes + 4);
  745. return 0;
  746. }
  747. tmp = readl(nfc->regs + NFC_REG_ECC_ERR_CNT(step));
  748. return NFC_ECC_ERR_CNT(step, tmp);
  749. }
  750. static int sunxi_nfc_hw_ecc_read_chunk(struct mtd_info *mtd,
  751. u8 *data, int data_off,
  752. u8 *oob, int oob_off,
  753. int *cur_off,
  754. unsigned int *max_bitflips,
  755. bool bbm, bool oob_required, int page)
  756. {
  757. struct nand_chip *nand = mtd_to_nand(mtd);
  758. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  759. struct nand_ecc_ctrl *ecc = &nand->ecc;
  760. int raw_mode = 0;
  761. bool erased;
  762. int ret;
  763. if (*cur_off != data_off)
  764. nand_change_read_column_op(nand, data_off, NULL, 0, false);
  765. sunxi_nfc_randomizer_read_buf(mtd, NULL, ecc->size, false, page);
  766. if (data_off + ecc->size != oob_off)
  767. nand_change_read_column_op(nand, oob_off, NULL, 0, false);
  768. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  769. if (ret)
  770. return ret;
  771. sunxi_nfc_randomizer_enable(mtd);
  772. writel(NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | NFC_ECC_OP,
  773. nfc->regs + NFC_REG_CMD);
  774. ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, false, 0);
  775. sunxi_nfc_randomizer_disable(mtd);
  776. if (ret)
  777. return ret;
  778. *cur_off = oob_off + ecc->bytes + 4;
  779. ret = sunxi_nfc_hw_ecc_correct(mtd, data, oob_required ? oob : NULL, 0,
  780. readl(nfc->regs + NFC_REG_ECC_ST),
  781. &erased);
  782. if (erased)
  783. return 1;
  784. if (ret < 0) {
  785. /*
  786. * Re-read the data with the randomizer disabled to identify
  787. * bitflips in erased pages.
  788. */
  789. if (nand->options & NAND_NEED_SCRAMBLING)
  790. nand_change_read_column_op(nand, data_off, data,
  791. ecc->size, false);
  792. else
  793. memcpy_fromio(data, nfc->regs + NFC_RAM0_BASE,
  794. ecc->size);
  795. nand_change_read_column_op(nand, oob_off, oob, ecc->bytes + 4,
  796. false);
  797. ret = nand_check_erased_ecc_chunk(data, ecc->size,
  798. oob, ecc->bytes + 4,
  799. NULL, 0, ecc->strength);
  800. if (ret >= 0)
  801. raw_mode = 1;
  802. } else {
  803. memcpy_fromio(data, nfc->regs + NFC_RAM0_BASE, ecc->size);
  804. if (oob_required) {
  805. nand_change_read_column_op(nand, oob_off, NULL, 0,
  806. false);
  807. sunxi_nfc_randomizer_read_buf(mtd, oob, ecc->bytes + 4,
  808. true, page);
  809. sunxi_nfc_hw_ecc_get_prot_oob_bytes(mtd, oob, 0,
  810. bbm, page);
  811. }
  812. }
  813. sunxi_nfc_hw_ecc_update_stats(mtd, max_bitflips, ret);
  814. return raw_mode;
  815. }
  816. static void sunxi_nfc_hw_ecc_read_extra_oob(struct mtd_info *mtd,
  817. u8 *oob, int *cur_off,
  818. bool randomize, int page)
  819. {
  820. struct nand_chip *nand = mtd_to_nand(mtd);
  821. struct nand_ecc_ctrl *ecc = &nand->ecc;
  822. int offset = ((ecc->bytes + 4) * ecc->steps);
  823. int len = mtd->oobsize - offset;
  824. if (len <= 0)
  825. return;
  826. if (!cur_off || *cur_off != offset)
  827. nand_change_read_column_op(nand, mtd->writesize, NULL, 0,
  828. false);
  829. if (!randomize)
  830. sunxi_nfc_read_buf(mtd, oob + offset, len);
  831. else
  832. sunxi_nfc_randomizer_read_buf(mtd, oob + offset, len,
  833. false, page);
  834. if (cur_off)
  835. *cur_off = mtd->oobsize + mtd->writesize;
  836. }
  837. static int sunxi_nfc_hw_ecc_read_chunks_dma(struct mtd_info *mtd, uint8_t *buf,
  838. int oob_required, int page,
  839. int nchunks)
  840. {
  841. struct nand_chip *nand = mtd_to_nand(mtd);
  842. bool randomized = nand->options & NAND_NEED_SCRAMBLING;
  843. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  844. struct nand_ecc_ctrl *ecc = &nand->ecc;
  845. unsigned int max_bitflips = 0;
  846. int ret, i, raw_mode = 0;
  847. struct scatterlist sg;
  848. u32 status;
  849. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  850. if (ret)
  851. return ret;
  852. ret = sunxi_nfc_dma_op_prepare(mtd, buf, ecc->size, nchunks,
  853. DMA_FROM_DEVICE, &sg);
  854. if (ret)
  855. return ret;
  856. sunxi_nfc_hw_ecc_enable(mtd);
  857. sunxi_nfc_randomizer_config(mtd, page, false);
  858. sunxi_nfc_randomizer_enable(mtd);
  859. writel((NAND_CMD_RNDOUTSTART << 16) | (NAND_CMD_RNDOUT << 8) |
  860. NAND_CMD_READSTART, nfc->regs + NFC_REG_RCMD_SET);
  861. dma_async_issue_pending(nfc->dmac);
  862. writel(NFC_PAGE_OP | NFC_DATA_SWAP_METHOD | NFC_DATA_TRANS,
  863. nfc->regs + NFC_REG_CMD);
  864. ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, false, 0);
  865. if (ret)
  866. dmaengine_terminate_all(nfc->dmac);
  867. sunxi_nfc_randomizer_disable(mtd);
  868. sunxi_nfc_hw_ecc_disable(mtd);
  869. sunxi_nfc_dma_op_cleanup(mtd, DMA_FROM_DEVICE, &sg);
  870. if (ret)
  871. return ret;
  872. status = readl(nfc->regs + NFC_REG_ECC_ST);
  873. for (i = 0; i < nchunks; i++) {
  874. int data_off = i * ecc->size;
  875. int oob_off = i * (ecc->bytes + 4);
  876. u8 *data = buf + data_off;
  877. u8 *oob = nand->oob_poi + oob_off;
  878. bool erased;
  879. ret = sunxi_nfc_hw_ecc_correct(mtd, randomized ? data : NULL,
  880. oob_required ? oob : NULL,
  881. i, status, &erased);
  882. /* ECC errors are handled in the second loop. */
  883. if (ret < 0)
  884. continue;
  885. if (oob_required && !erased) {
  886. /* TODO: use DMA to retrieve OOB */
  887. nand_change_read_column_op(nand,
  888. mtd->writesize + oob_off,
  889. oob, ecc->bytes + 4, false);
  890. sunxi_nfc_hw_ecc_get_prot_oob_bytes(mtd, oob, i,
  891. !i, page);
  892. }
  893. if (erased)
  894. raw_mode = 1;
  895. sunxi_nfc_hw_ecc_update_stats(mtd, &max_bitflips, ret);
  896. }
  897. if (status & NFC_ECC_ERR_MSK) {
  898. for (i = 0; i < nchunks; i++) {
  899. int data_off = i * ecc->size;
  900. int oob_off = i * (ecc->bytes + 4);
  901. u8 *data = buf + data_off;
  902. u8 *oob = nand->oob_poi + oob_off;
  903. if (!(status & NFC_ECC_ERR(i)))
  904. continue;
  905. /*
  906. * Re-read the data with the randomizer disabled to
  907. * identify bitflips in erased pages.
  908. * TODO: use DMA to read page in raw mode
  909. */
  910. if (randomized)
  911. nand_change_read_column_op(nand, data_off,
  912. data, ecc->size,
  913. false);
  914. /* TODO: use DMA to retrieve OOB */
  915. nand_change_read_column_op(nand,
  916. mtd->writesize + oob_off,
  917. oob, ecc->bytes + 4, false);
  918. ret = nand_check_erased_ecc_chunk(data, ecc->size,
  919. oob, ecc->bytes + 4,
  920. NULL, 0,
  921. ecc->strength);
  922. if (ret >= 0)
  923. raw_mode = 1;
  924. sunxi_nfc_hw_ecc_update_stats(mtd, &max_bitflips, ret);
  925. }
  926. }
  927. if (oob_required)
  928. sunxi_nfc_hw_ecc_read_extra_oob(mtd, nand->oob_poi,
  929. NULL, !raw_mode,
  930. page);
  931. return max_bitflips;
  932. }
  933. static int sunxi_nfc_hw_ecc_write_chunk(struct mtd_info *mtd,
  934. const u8 *data, int data_off,
  935. const u8 *oob, int oob_off,
  936. int *cur_off, bool bbm,
  937. int page)
  938. {
  939. struct nand_chip *nand = mtd_to_nand(mtd);
  940. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  941. struct nand_ecc_ctrl *ecc = &nand->ecc;
  942. int ret;
  943. if (data_off != *cur_off)
  944. nand_change_write_column_op(nand, data_off, NULL, 0, false);
  945. sunxi_nfc_randomizer_write_buf(mtd, data, ecc->size, false, page);
  946. if (data_off + ecc->size != oob_off)
  947. nand_change_write_column_op(nand, oob_off, NULL, 0, false);
  948. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  949. if (ret)
  950. return ret;
  951. sunxi_nfc_randomizer_enable(mtd);
  952. sunxi_nfc_hw_ecc_set_prot_oob_bytes(mtd, oob, 0, bbm, page);
  953. writel(NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD |
  954. NFC_ACCESS_DIR | NFC_ECC_OP,
  955. nfc->regs + NFC_REG_CMD);
  956. ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, false, 0);
  957. sunxi_nfc_randomizer_disable(mtd);
  958. if (ret)
  959. return ret;
  960. *cur_off = oob_off + ecc->bytes + 4;
  961. return 0;
  962. }
  963. static void sunxi_nfc_hw_ecc_write_extra_oob(struct mtd_info *mtd,
  964. u8 *oob, int *cur_off,
  965. int page)
  966. {
  967. struct nand_chip *nand = mtd_to_nand(mtd);
  968. struct nand_ecc_ctrl *ecc = &nand->ecc;
  969. int offset = ((ecc->bytes + 4) * ecc->steps);
  970. int len = mtd->oobsize - offset;
  971. if (len <= 0)
  972. return;
  973. if (!cur_off || *cur_off != offset)
  974. nand_change_write_column_op(nand, offset + mtd->writesize,
  975. NULL, 0, false);
  976. sunxi_nfc_randomizer_write_buf(mtd, oob + offset, len, false, page);
  977. if (cur_off)
  978. *cur_off = mtd->oobsize + mtd->writesize;
  979. }
  980. static int sunxi_nfc_hw_ecc_read_page(struct mtd_info *mtd,
  981. struct nand_chip *chip, uint8_t *buf,
  982. int oob_required, int page)
  983. {
  984. struct nand_ecc_ctrl *ecc = &chip->ecc;
  985. unsigned int max_bitflips = 0;
  986. int ret, i, cur_off = 0;
  987. bool raw_mode = false;
  988. nand_read_page_op(chip, page, 0, NULL, 0);
  989. sunxi_nfc_hw_ecc_enable(mtd);
  990. for (i = 0; i < ecc->steps; i++) {
  991. int data_off = i * ecc->size;
  992. int oob_off = i * (ecc->bytes + 4);
  993. u8 *data = buf + data_off;
  994. u8 *oob = chip->oob_poi + oob_off;
  995. ret = sunxi_nfc_hw_ecc_read_chunk(mtd, data, data_off, oob,
  996. oob_off + mtd->writesize,
  997. &cur_off, &max_bitflips,
  998. !i, oob_required, page);
  999. if (ret < 0)
  1000. return ret;
  1001. else if (ret)
  1002. raw_mode = true;
  1003. }
  1004. if (oob_required)
  1005. sunxi_nfc_hw_ecc_read_extra_oob(mtd, chip->oob_poi, &cur_off,
  1006. !raw_mode, page);
  1007. sunxi_nfc_hw_ecc_disable(mtd);
  1008. return max_bitflips;
  1009. }
  1010. static int sunxi_nfc_hw_ecc_read_page_dma(struct mtd_info *mtd,
  1011. struct nand_chip *chip, u8 *buf,
  1012. int oob_required, int page)
  1013. {
  1014. int ret;
  1015. nand_read_page_op(chip, page, 0, NULL, 0);
  1016. ret = sunxi_nfc_hw_ecc_read_chunks_dma(mtd, buf, oob_required, page,
  1017. chip->ecc.steps);
  1018. if (ret >= 0)
  1019. return ret;
  1020. /* Fallback to PIO mode */
  1021. return sunxi_nfc_hw_ecc_read_page(mtd, chip, buf, oob_required, page);
  1022. }
  1023. static int sunxi_nfc_hw_ecc_read_subpage(struct mtd_info *mtd,
  1024. struct nand_chip *chip,
  1025. u32 data_offs, u32 readlen,
  1026. u8 *bufpoi, int page)
  1027. {
  1028. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1029. int ret, i, cur_off = 0;
  1030. unsigned int max_bitflips = 0;
  1031. nand_read_page_op(chip, page, 0, NULL, 0);
  1032. sunxi_nfc_hw_ecc_enable(mtd);
  1033. for (i = data_offs / ecc->size;
  1034. i < DIV_ROUND_UP(data_offs + readlen, ecc->size); i++) {
  1035. int data_off = i * ecc->size;
  1036. int oob_off = i * (ecc->bytes + 4);
  1037. u8 *data = bufpoi + data_off;
  1038. u8 *oob = chip->oob_poi + oob_off;
  1039. ret = sunxi_nfc_hw_ecc_read_chunk(mtd, data, data_off,
  1040. oob,
  1041. oob_off + mtd->writesize,
  1042. &cur_off, &max_bitflips, !i,
  1043. false, page);
  1044. if (ret < 0)
  1045. return ret;
  1046. }
  1047. sunxi_nfc_hw_ecc_disable(mtd);
  1048. return max_bitflips;
  1049. }
  1050. static int sunxi_nfc_hw_ecc_read_subpage_dma(struct mtd_info *mtd,
  1051. struct nand_chip *chip,
  1052. u32 data_offs, u32 readlen,
  1053. u8 *buf, int page)
  1054. {
  1055. int nchunks = DIV_ROUND_UP(data_offs + readlen, chip->ecc.size);
  1056. int ret;
  1057. nand_read_page_op(chip, page, 0, NULL, 0);
  1058. ret = sunxi_nfc_hw_ecc_read_chunks_dma(mtd, buf, false, page, nchunks);
  1059. if (ret >= 0)
  1060. return ret;
  1061. /* Fallback to PIO mode */
  1062. return sunxi_nfc_hw_ecc_read_subpage(mtd, chip, data_offs, readlen,
  1063. buf, page);
  1064. }
  1065. static int sunxi_nfc_hw_ecc_write_page(struct mtd_info *mtd,
  1066. struct nand_chip *chip,
  1067. const uint8_t *buf, int oob_required,
  1068. int page)
  1069. {
  1070. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1071. int ret, i, cur_off = 0;
  1072. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  1073. sunxi_nfc_hw_ecc_enable(mtd);
  1074. for (i = 0; i < ecc->steps; i++) {
  1075. int data_off = i * ecc->size;
  1076. int oob_off = i * (ecc->bytes + 4);
  1077. const u8 *data = buf + data_off;
  1078. const u8 *oob = chip->oob_poi + oob_off;
  1079. ret = sunxi_nfc_hw_ecc_write_chunk(mtd, data, data_off, oob,
  1080. oob_off + mtd->writesize,
  1081. &cur_off, !i, page);
  1082. if (ret)
  1083. return ret;
  1084. }
  1085. if (oob_required || (chip->options & NAND_NEED_SCRAMBLING))
  1086. sunxi_nfc_hw_ecc_write_extra_oob(mtd, chip->oob_poi,
  1087. &cur_off, page);
  1088. sunxi_nfc_hw_ecc_disable(mtd);
  1089. return nand_prog_page_end_op(chip);
  1090. }
  1091. static int sunxi_nfc_hw_ecc_write_subpage(struct mtd_info *mtd,
  1092. struct nand_chip *chip,
  1093. u32 data_offs, u32 data_len,
  1094. const u8 *buf, int oob_required,
  1095. int page)
  1096. {
  1097. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1098. int ret, i, cur_off = 0;
  1099. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  1100. sunxi_nfc_hw_ecc_enable(mtd);
  1101. for (i = data_offs / ecc->size;
  1102. i < DIV_ROUND_UP(data_offs + data_len, ecc->size); i++) {
  1103. int data_off = i * ecc->size;
  1104. int oob_off = i * (ecc->bytes + 4);
  1105. const u8 *data = buf + data_off;
  1106. const u8 *oob = chip->oob_poi + oob_off;
  1107. ret = sunxi_nfc_hw_ecc_write_chunk(mtd, data, data_off, oob,
  1108. oob_off + mtd->writesize,
  1109. &cur_off, !i, page);
  1110. if (ret)
  1111. return ret;
  1112. }
  1113. sunxi_nfc_hw_ecc_disable(mtd);
  1114. return nand_prog_page_end_op(chip);
  1115. }
  1116. static int sunxi_nfc_hw_ecc_write_page_dma(struct mtd_info *mtd,
  1117. struct nand_chip *chip,
  1118. const u8 *buf,
  1119. int oob_required,
  1120. int page)
  1121. {
  1122. struct nand_chip *nand = mtd_to_nand(mtd);
  1123. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  1124. struct nand_ecc_ctrl *ecc = &nand->ecc;
  1125. struct scatterlist sg;
  1126. int ret, i;
  1127. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  1128. if (ret)
  1129. return ret;
  1130. ret = sunxi_nfc_dma_op_prepare(mtd, buf, ecc->size, ecc->steps,
  1131. DMA_TO_DEVICE, &sg);
  1132. if (ret)
  1133. goto pio_fallback;
  1134. for (i = 0; i < ecc->steps; i++) {
  1135. const u8 *oob = nand->oob_poi + (i * (ecc->bytes + 4));
  1136. sunxi_nfc_hw_ecc_set_prot_oob_bytes(mtd, oob, i, !i, page);
  1137. }
  1138. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  1139. sunxi_nfc_hw_ecc_enable(mtd);
  1140. sunxi_nfc_randomizer_config(mtd, page, false);
  1141. sunxi_nfc_randomizer_enable(mtd);
  1142. writel((NAND_CMD_RNDIN << 8) | NAND_CMD_PAGEPROG,
  1143. nfc->regs + NFC_REG_WCMD_SET);
  1144. dma_async_issue_pending(nfc->dmac);
  1145. writel(NFC_PAGE_OP | NFC_DATA_SWAP_METHOD |
  1146. NFC_DATA_TRANS | NFC_ACCESS_DIR,
  1147. nfc->regs + NFC_REG_CMD);
  1148. ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, false, 0);
  1149. if (ret)
  1150. dmaengine_terminate_all(nfc->dmac);
  1151. sunxi_nfc_randomizer_disable(mtd);
  1152. sunxi_nfc_hw_ecc_disable(mtd);
  1153. sunxi_nfc_dma_op_cleanup(mtd, DMA_TO_DEVICE, &sg);
  1154. if (ret)
  1155. return ret;
  1156. if (oob_required || (chip->options & NAND_NEED_SCRAMBLING))
  1157. /* TODO: use DMA to transfer extra OOB bytes ? */
  1158. sunxi_nfc_hw_ecc_write_extra_oob(mtd, chip->oob_poi,
  1159. NULL, page);
  1160. return nand_prog_page_end_op(chip);
  1161. pio_fallback:
  1162. return sunxi_nfc_hw_ecc_write_page(mtd, chip, buf, oob_required, page);
  1163. }
  1164. static int sunxi_nfc_hw_ecc_read_oob(struct mtd_info *mtd,
  1165. struct nand_chip *chip,
  1166. int page)
  1167. {
  1168. chip->pagebuf = -1;
  1169. return chip->ecc.read_page(mtd, chip, chip->data_buf, 1, page);
  1170. }
  1171. static int sunxi_nfc_hw_ecc_write_oob(struct mtd_info *mtd,
  1172. struct nand_chip *chip,
  1173. int page)
  1174. {
  1175. int ret;
  1176. chip->pagebuf = -1;
  1177. memset(chip->data_buf, 0xff, mtd->writesize);
  1178. ret = chip->ecc.write_page(mtd, chip, chip->data_buf, 1, page);
  1179. if (ret)
  1180. return ret;
  1181. /* Send command to program the OOB data */
  1182. return nand_prog_page_end_op(chip);
  1183. }
  1184. static const s32 tWB_lut[] = {6, 12, 16, 20};
  1185. static const s32 tRHW_lut[] = {4, 8, 12, 20};
  1186. static int _sunxi_nand_lookup_timing(const s32 *lut, int lut_size, u32 duration,
  1187. u32 clk_period)
  1188. {
  1189. u32 clk_cycles = DIV_ROUND_UP(duration, clk_period);
  1190. int i;
  1191. for (i = 0; i < lut_size; i++) {
  1192. if (clk_cycles <= lut[i])
  1193. return i;
  1194. }
  1195. /* Doesn't fit */
  1196. return -EINVAL;
  1197. }
  1198. #define sunxi_nand_lookup_timing(l, p, c) \
  1199. _sunxi_nand_lookup_timing(l, ARRAY_SIZE(l), p, c)
  1200. static int sunxi_nfc_setup_data_interface(struct mtd_info *mtd, int csline,
  1201. const struct nand_data_interface *conf)
  1202. {
  1203. struct nand_chip *nand = mtd_to_nand(mtd);
  1204. struct sunxi_nand_chip *chip = to_sunxi_nand(nand);
  1205. struct sunxi_nfc *nfc = to_sunxi_nfc(chip->nand.controller);
  1206. const struct nand_sdr_timings *timings;
  1207. u32 min_clk_period = 0;
  1208. s32 tWB, tADL, tWHR, tRHW, tCAD;
  1209. long real_clk_rate;
  1210. timings = nand_get_sdr_timings(conf);
  1211. if (IS_ERR(timings))
  1212. return -ENOTSUPP;
  1213. /* T1 <=> tCLS */
  1214. if (timings->tCLS_min > min_clk_period)
  1215. min_clk_period = timings->tCLS_min;
  1216. /* T2 <=> tCLH */
  1217. if (timings->tCLH_min > min_clk_period)
  1218. min_clk_period = timings->tCLH_min;
  1219. /* T3 <=> tCS */
  1220. if (timings->tCS_min > min_clk_period)
  1221. min_clk_period = timings->tCS_min;
  1222. /* T4 <=> tCH */
  1223. if (timings->tCH_min > min_clk_period)
  1224. min_clk_period = timings->tCH_min;
  1225. /* T5 <=> tWP */
  1226. if (timings->tWP_min > min_clk_period)
  1227. min_clk_period = timings->tWP_min;
  1228. /* T6 <=> tWH */
  1229. if (timings->tWH_min > min_clk_period)
  1230. min_clk_period = timings->tWH_min;
  1231. /* T7 <=> tALS */
  1232. if (timings->tALS_min > min_clk_period)
  1233. min_clk_period = timings->tALS_min;
  1234. /* T8 <=> tDS */
  1235. if (timings->tDS_min > min_clk_period)
  1236. min_clk_period = timings->tDS_min;
  1237. /* T9 <=> tDH */
  1238. if (timings->tDH_min > min_clk_period)
  1239. min_clk_period = timings->tDH_min;
  1240. /* T10 <=> tRR */
  1241. if (timings->tRR_min > (min_clk_period * 3))
  1242. min_clk_period = DIV_ROUND_UP(timings->tRR_min, 3);
  1243. /* T11 <=> tALH */
  1244. if (timings->tALH_min > min_clk_period)
  1245. min_clk_period = timings->tALH_min;
  1246. /* T12 <=> tRP */
  1247. if (timings->tRP_min > min_clk_period)
  1248. min_clk_period = timings->tRP_min;
  1249. /* T13 <=> tREH */
  1250. if (timings->tREH_min > min_clk_period)
  1251. min_clk_period = timings->tREH_min;
  1252. /* T14 <=> tRC */
  1253. if (timings->tRC_min > (min_clk_period * 2))
  1254. min_clk_period = DIV_ROUND_UP(timings->tRC_min, 2);
  1255. /* T15 <=> tWC */
  1256. if (timings->tWC_min > (min_clk_period * 2))
  1257. min_clk_period = DIV_ROUND_UP(timings->tWC_min, 2);
  1258. /* T16 - T19 + tCAD */
  1259. if (timings->tWB_max > (min_clk_period * 20))
  1260. min_clk_period = DIV_ROUND_UP(timings->tWB_max, 20);
  1261. if (timings->tADL_min > (min_clk_period * 32))
  1262. min_clk_period = DIV_ROUND_UP(timings->tADL_min, 32);
  1263. if (timings->tWHR_min > (min_clk_period * 32))
  1264. min_clk_period = DIV_ROUND_UP(timings->tWHR_min, 32);
  1265. if (timings->tRHW_min > (min_clk_period * 20))
  1266. min_clk_period = DIV_ROUND_UP(timings->tRHW_min, 20);
  1267. tWB = sunxi_nand_lookup_timing(tWB_lut, timings->tWB_max,
  1268. min_clk_period);
  1269. if (tWB < 0) {
  1270. dev_err(nfc->dev, "unsupported tWB\n");
  1271. return tWB;
  1272. }
  1273. tADL = DIV_ROUND_UP(timings->tADL_min, min_clk_period) >> 3;
  1274. if (tADL > 3) {
  1275. dev_err(nfc->dev, "unsupported tADL\n");
  1276. return -EINVAL;
  1277. }
  1278. tWHR = DIV_ROUND_UP(timings->tWHR_min, min_clk_period) >> 3;
  1279. if (tWHR > 3) {
  1280. dev_err(nfc->dev, "unsupported tWHR\n");
  1281. return -EINVAL;
  1282. }
  1283. tRHW = sunxi_nand_lookup_timing(tRHW_lut, timings->tRHW_min,
  1284. min_clk_period);
  1285. if (tRHW < 0) {
  1286. dev_err(nfc->dev, "unsupported tRHW\n");
  1287. return tRHW;
  1288. }
  1289. if (csline == NAND_DATA_IFACE_CHECK_ONLY)
  1290. return 0;
  1291. /*
  1292. * TODO: according to ONFI specs this value only applies for DDR NAND,
  1293. * but Allwinner seems to set this to 0x7. Mimic them for now.
  1294. */
  1295. tCAD = 0x7;
  1296. /* TODO: A83 has some more bits for CDQSS, CS, CLHZ, CCS, WC */
  1297. chip->timing_cfg = NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD);
  1298. /* Convert min_clk_period from picoseconds to nanoseconds */
  1299. min_clk_period = DIV_ROUND_UP(min_clk_period, 1000);
  1300. /*
  1301. * Unlike what is stated in Allwinner datasheet, the clk_rate should
  1302. * be set to (1 / min_clk_period), and not (2 / min_clk_period).
  1303. * This new formula was verified with a scope and validated by
  1304. * Allwinner engineers.
  1305. */
  1306. chip->clk_rate = NSEC_PER_SEC / min_clk_period;
  1307. real_clk_rate = clk_round_rate(nfc->mod_clk, chip->clk_rate);
  1308. if (real_clk_rate <= 0) {
  1309. dev_err(nfc->dev, "Unable to round clk %lu\n", chip->clk_rate);
  1310. return -EINVAL;
  1311. }
  1312. /*
  1313. * ONFI specification 3.1, paragraph 4.15.2 dictates that EDO data
  1314. * output cycle timings shall be used if the host drives tRC less than
  1315. * 30 ns.
  1316. */
  1317. min_clk_period = NSEC_PER_SEC / real_clk_rate;
  1318. chip->timing_ctl = ((min_clk_period * 2) < 30) ?
  1319. NFC_TIMING_CTL_EDO : 0;
  1320. return 0;
  1321. }
  1322. static int sunxi_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
  1323. struct mtd_oob_region *oobregion)
  1324. {
  1325. struct nand_chip *nand = mtd_to_nand(mtd);
  1326. struct nand_ecc_ctrl *ecc = &nand->ecc;
  1327. if (section >= ecc->steps)
  1328. return -ERANGE;
  1329. oobregion->offset = section * (ecc->bytes + 4) + 4;
  1330. oobregion->length = ecc->bytes;
  1331. return 0;
  1332. }
  1333. static int sunxi_nand_ooblayout_free(struct mtd_info *mtd, int section,
  1334. struct mtd_oob_region *oobregion)
  1335. {
  1336. struct nand_chip *nand = mtd_to_nand(mtd);
  1337. struct nand_ecc_ctrl *ecc = &nand->ecc;
  1338. if (section > ecc->steps)
  1339. return -ERANGE;
  1340. /*
  1341. * The first 2 bytes are used for BB markers, hence we
  1342. * only have 2 bytes available in the first user data
  1343. * section.
  1344. */
  1345. if (!section && ecc->mode == NAND_ECC_HW) {
  1346. oobregion->offset = 2;
  1347. oobregion->length = 2;
  1348. return 0;
  1349. }
  1350. oobregion->offset = section * (ecc->bytes + 4);
  1351. if (section < ecc->steps)
  1352. oobregion->length = 4;
  1353. else
  1354. oobregion->offset = mtd->oobsize - oobregion->offset;
  1355. return 0;
  1356. }
  1357. static const struct mtd_ooblayout_ops sunxi_nand_ooblayout_ops = {
  1358. .ecc = sunxi_nand_ooblayout_ecc,
  1359. .free = sunxi_nand_ooblayout_free,
  1360. };
  1361. static void sunxi_nand_hw_ecc_ctrl_cleanup(struct nand_ecc_ctrl *ecc)
  1362. {
  1363. kfree(ecc->priv);
  1364. }
  1365. static int sunxi_nand_hw_ecc_ctrl_init(struct mtd_info *mtd,
  1366. struct nand_ecc_ctrl *ecc,
  1367. struct device_node *np)
  1368. {
  1369. static const u8 strengths[] = { 16, 24, 28, 32, 40, 48, 56, 60, 64 };
  1370. struct nand_chip *nand = mtd_to_nand(mtd);
  1371. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  1372. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  1373. struct sunxi_nand_hw_ecc *data;
  1374. int nsectors;
  1375. int ret;
  1376. int i;
  1377. if (ecc->options & NAND_ECC_MAXIMIZE) {
  1378. int bytes;
  1379. ecc->size = 1024;
  1380. nsectors = mtd->writesize / ecc->size;
  1381. /* Reserve 2 bytes for the BBM */
  1382. bytes = (mtd->oobsize - 2) / nsectors;
  1383. /* 4 non-ECC bytes are added before each ECC bytes section */
  1384. bytes -= 4;
  1385. /* and bytes has to be even. */
  1386. if (bytes % 2)
  1387. bytes--;
  1388. ecc->strength = bytes * 8 / fls(8 * ecc->size);
  1389. for (i = 0; i < ARRAY_SIZE(strengths); i++) {
  1390. if (strengths[i] > ecc->strength)
  1391. break;
  1392. }
  1393. if (!i)
  1394. ecc->strength = 0;
  1395. else
  1396. ecc->strength = strengths[i - 1];
  1397. }
  1398. if (ecc->size != 512 && ecc->size != 1024)
  1399. return -EINVAL;
  1400. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1401. if (!data)
  1402. return -ENOMEM;
  1403. /* Prefer 1k ECC chunk over 512 ones */
  1404. if (ecc->size == 512 && mtd->writesize > 512) {
  1405. ecc->size = 1024;
  1406. ecc->strength *= 2;
  1407. }
  1408. /* Add ECC info retrieval from DT */
  1409. for (i = 0; i < ARRAY_SIZE(strengths); i++) {
  1410. if (ecc->strength <= strengths[i]) {
  1411. /*
  1412. * Update ecc->strength value with the actual strength
  1413. * that will be used by the ECC engine.
  1414. */
  1415. ecc->strength = strengths[i];
  1416. break;
  1417. }
  1418. }
  1419. if (i >= ARRAY_SIZE(strengths)) {
  1420. dev_err(nfc->dev, "unsupported strength\n");
  1421. ret = -ENOTSUPP;
  1422. goto err;
  1423. }
  1424. data->mode = i;
  1425. /* HW ECC always request ECC bytes for 1024 bytes blocks */
  1426. ecc->bytes = DIV_ROUND_UP(ecc->strength * fls(8 * 1024), 8);
  1427. /* HW ECC always work with even numbers of ECC bytes */
  1428. ecc->bytes = ALIGN(ecc->bytes, 2);
  1429. nsectors = mtd->writesize / ecc->size;
  1430. if (mtd->oobsize < ((ecc->bytes + 4) * nsectors)) {
  1431. ret = -EINVAL;
  1432. goto err;
  1433. }
  1434. ecc->read_oob = sunxi_nfc_hw_ecc_read_oob;
  1435. ecc->write_oob = sunxi_nfc_hw_ecc_write_oob;
  1436. mtd_set_ooblayout(mtd, &sunxi_nand_ooblayout_ops);
  1437. ecc->priv = data;
  1438. if (nfc->dmac) {
  1439. ecc->read_page = sunxi_nfc_hw_ecc_read_page_dma;
  1440. ecc->read_subpage = sunxi_nfc_hw_ecc_read_subpage_dma;
  1441. ecc->write_page = sunxi_nfc_hw_ecc_write_page_dma;
  1442. nand->options |= NAND_USE_BOUNCE_BUFFER;
  1443. } else {
  1444. ecc->read_page = sunxi_nfc_hw_ecc_read_page;
  1445. ecc->read_subpage = sunxi_nfc_hw_ecc_read_subpage;
  1446. ecc->write_page = sunxi_nfc_hw_ecc_write_page;
  1447. }
  1448. /* TODO: support DMA for raw accesses and subpage write */
  1449. ecc->write_subpage = sunxi_nfc_hw_ecc_write_subpage;
  1450. ecc->read_oob_raw = nand_read_oob_std;
  1451. ecc->write_oob_raw = nand_write_oob_std;
  1452. return 0;
  1453. err:
  1454. kfree(data);
  1455. return ret;
  1456. }
  1457. static void sunxi_nand_ecc_cleanup(struct nand_ecc_ctrl *ecc)
  1458. {
  1459. switch (ecc->mode) {
  1460. case NAND_ECC_HW:
  1461. sunxi_nand_hw_ecc_ctrl_cleanup(ecc);
  1462. break;
  1463. case NAND_ECC_NONE:
  1464. default:
  1465. break;
  1466. }
  1467. }
  1468. static int sunxi_nand_attach_chip(struct nand_chip *nand)
  1469. {
  1470. struct mtd_info *mtd = nand_to_mtd(nand);
  1471. struct nand_ecc_ctrl *ecc = &nand->ecc;
  1472. struct device_node *np = nand_get_flash_node(nand);
  1473. int ret;
  1474. if (nand->bbt_options & NAND_BBT_USE_FLASH)
  1475. nand->bbt_options |= NAND_BBT_NO_OOB;
  1476. if (nand->options & NAND_NEED_SCRAMBLING)
  1477. nand->options |= NAND_NO_SUBPAGE_WRITE;
  1478. nand->options |= NAND_SUBPAGE_READ;
  1479. if (!ecc->size) {
  1480. ecc->size = nand->ecc_step_ds;
  1481. ecc->strength = nand->ecc_strength_ds;
  1482. }
  1483. if (!ecc->size || !ecc->strength)
  1484. return -EINVAL;
  1485. switch (ecc->mode) {
  1486. case NAND_ECC_HW:
  1487. ret = sunxi_nand_hw_ecc_ctrl_init(mtd, ecc, np);
  1488. if (ret)
  1489. return ret;
  1490. break;
  1491. case NAND_ECC_NONE:
  1492. case NAND_ECC_SOFT:
  1493. break;
  1494. default:
  1495. return -EINVAL;
  1496. }
  1497. return 0;
  1498. }
  1499. static const struct nand_controller_ops sunxi_nand_controller_ops = {
  1500. .attach_chip = sunxi_nand_attach_chip,
  1501. };
  1502. static int sunxi_nand_chip_init(struct device *dev, struct sunxi_nfc *nfc,
  1503. struct device_node *np)
  1504. {
  1505. struct sunxi_nand_chip *chip;
  1506. struct mtd_info *mtd;
  1507. struct nand_chip *nand;
  1508. int nsels;
  1509. int ret;
  1510. int i;
  1511. u32 tmp;
  1512. if (!of_get_property(np, "reg", &nsels))
  1513. return -EINVAL;
  1514. nsels /= sizeof(u32);
  1515. if (!nsels) {
  1516. dev_err(dev, "invalid reg property size\n");
  1517. return -EINVAL;
  1518. }
  1519. chip = devm_kzalloc(dev,
  1520. sizeof(*chip) +
  1521. (nsels * sizeof(struct sunxi_nand_chip_sel)),
  1522. GFP_KERNEL);
  1523. if (!chip) {
  1524. dev_err(dev, "could not allocate chip\n");
  1525. return -ENOMEM;
  1526. }
  1527. chip->nsels = nsels;
  1528. chip->selected = -1;
  1529. for (i = 0; i < nsels; i++) {
  1530. ret = of_property_read_u32_index(np, "reg", i, &tmp);
  1531. if (ret) {
  1532. dev_err(dev, "could not retrieve reg property: %d\n",
  1533. ret);
  1534. return ret;
  1535. }
  1536. if (tmp > NFC_MAX_CS) {
  1537. dev_err(dev,
  1538. "invalid reg value: %u (max CS = 7)\n",
  1539. tmp);
  1540. return -EINVAL;
  1541. }
  1542. if (test_and_set_bit(tmp, &nfc->assigned_cs)) {
  1543. dev_err(dev, "CS %d already assigned\n", tmp);
  1544. return -EINVAL;
  1545. }
  1546. chip->sels[i].cs = tmp;
  1547. if (!of_property_read_u32_index(np, "allwinner,rb", i, &tmp) &&
  1548. tmp < 2)
  1549. chip->sels[i].rb = tmp;
  1550. else
  1551. chip->sels[i].rb = -1;
  1552. }
  1553. nand = &chip->nand;
  1554. /* Default tR value specified in the ONFI spec (chapter 4.15.1) */
  1555. nand->chip_delay = 200;
  1556. nand->controller = &nfc->controller;
  1557. nand->controller->ops = &sunxi_nand_controller_ops;
  1558. /*
  1559. * Set the ECC mode to the default value in case nothing is specified
  1560. * in the DT.
  1561. */
  1562. nand->ecc.mode = NAND_ECC_HW;
  1563. nand_set_flash_node(nand, np);
  1564. nand->select_chip = sunxi_nfc_select_chip;
  1565. nand->cmd_ctrl = sunxi_nfc_cmd_ctrl;
  1566. nand->read_buf = sunxi_nfc_read_buf;
  1567. nand->write_buf = sunxi_nfc_write_buf;
  1568. nand->read_byte = sunxi_nfc_read_byte;
  1569. nand->setup_data_interface = sunxi_nfc_setup_data_interface;
  1570. mtd = nand_to_mtd(nand);
  1571. mtd->dev.parent = dev;
  1572. ret = nand_scan(nand, nsels);
  1573. if (ret)
  1574. return ret;
  1575. ret = mtd_device_register(mtd, NULL, 0);
  1576. if (ret) {
  1577. dev_err(dev, "failed to register mtd device: %d\n", ret);
  1578. nand_cleanup(nand);
  1579. return ret;
  1580. }
  1581. list_add_tail(&chip->node, &nfc->chips);
  1582. return 0;
  1583. }
  1584. static int sunxi_nand_chips_init(struct device *dev, struct sunxi_nfc *nfc)
  1585. {
  1586. struct device_node *np = dev->of_node;
  1587. struct device_node *nand_np;
  1588. int nchips = of_get_child_count(np);
  1589. int ret;
  1590. if (nchips > 8) {
  1591. dev_err(dev, "too many NAND chips: %d (max = 8)\n", nchips);
  1592. return -EINVAL;
  1593. }
  1594. for_each_child_of_node(np, nand_np) {
  1595. ret = sunxi_nand_chip_init(dev, nfc, nand_np);
  1596. if (ret) {
  1597. of_node_put(nand_np);
  1598. return ret;
  1599. }
  1600. }
  1601. return 0;
  1602. }
  1603. static void sunxi_nand_chips_cleanup(struct sunxi_nfc *nfc)
  1604. {
  1605. struct sunxi_nand_chip *chip;
  1606. while (!list_empty(&nfc->chips)) {
  1607. chip = list_first_entry(&nfc->chips, struct sunxi_nand_chip,
  1608. node);
  1609. nand_release(&chip->nand);
  1610. sunxi_nand_ecc_cleanup(&chip->nand.ecc);
  1611. list_del(&chip->node);
  1612. }
  1613. }
  1614. static int sunxi_nfc_probe(struct platform_device *pdev)
  1615. {
  1616. struct device *dev = &pdev->dev;
  1617. struct resource *r;
  1618. struct sunxi_nfc *nfc;
  1619. int irq;
  1620. int ret;
  1621. nfc = devm_kzalloc(dev, sizeof(*nfc), GFP_KERNEL);
  1622. if (!nfc)
  1623. return -ENOMEM;
  1624. nfc->dev = dev;
  1625. nand_controller_init(&nfc->controller);
  1626. INIT_LIST_HEAD(&nfc->chips);
  1627. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1628. nfc->regs = devm_ioremap_resource(dev, r);
  1629. if (IS_ERR(nfc->regs))
  1630. return PTR_ERR(nfc->regs);
  1631. irq = platform_get_irq(pdev, 0);
  1632. if (irq < 0) {
  1633. dev_err(dev, "failed to retrieve irq\n");
  1634. return irq;
  1635. }
  1636. nfc->ahb_clk = devm_clk_get(dev, "ahb");
  1637. if (IS_ERR(nfc->ahb_clk)) {
  1638. dev_err(dev, "failed to retrieve ahb clk\n");
  1639. return PTR_ERR(nfc->ahb_clk);
  1640. }
  1641. ret = clk_prepare_enable(nfc->ahb_clk);
  1642. if (ret)
  1643. return ret;
  1644. nfc->mod_clk = devm_clk_get(dev, "mod");
  1645. if (IS_ERR(nfc->mod_clk)) {
  1646. dev_err(dev, "failed to retrieve mod clk\n");
  1647. ret = PTR_ERR(nfc->mod_clk);
  1648. goto out_ahb_clk_unprepare;
  1649. }
  1650. ret = clk_prepare_enable(nfc->mod_clk);
  1651. if (ret)
  1652. goto out_ahb_clk_unprepare;
  1653. nfc->reset = devm_reset_control_get_optional_exclusive(dev, "ahb");
  1654. if (IS_ERR(nfc->reset)) {
  1655. ret = PTR_ERR(nfc->reset);
  1656. goto out_mod_clk_unprepare;
  1657. }
  1658. ret = reset_control_deassert(nfc->reset);
  1659. if (ret) {
  1660. dev_err(dev, "reset err %d\n", ret);
  1661. goto out_mod_clk_unprepare;
  1662. }
  1663. ret = sunxi_nfc_rst(nfc);
  1664. if (ret)
  1665. goto out_ahb_reset_reassert;
  1666. writel(0, nfc->regs + NFC_REG_INT);
  1667. ret = devm_request_irq(dev, irq, sunxi_nfc_interrupt,
  1668. 0, "sunxi-nand", nfc);
  1669. if (ret)
  1670. goto out_ahb_reset_reassert;
  1671. nfc->dmac = dma_request_slave_channel(dev, "rxtx");
  1672. if (nfc->dmac) {
  1673. struct dma_slave_config dmac_cfg = { };
  1674. dmac_cfg.src_addr = r->start + NFC_REG_IO_DATA;
  1675. dmac_cfg.dst_addr = dmac_cfg.src_addr;
  1676. dmac_cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1677. dmac_cfg.dst_addr_width = dmac_cfg.src_addr_width;
  1678. dmac_cfg.src_maxburst = 4;
  1679. dmac_cfg.dst_maxburst = 4;
  1680. dmaengine_slave_config(nfc->dmac, &dmac_cfg);
  1681. } else {
  1682. dev_warn(dev, "failed to request rxtx DMA channel\n");
  1683. }
  1684. platform_set_drvdata(pdev, nfc);
  1685. ret = sunxi_nand_chips_init(dev, nfc);
  1686. if (ret) {
  1687. dev_err(dev, "failed to init nand chips\n");
  1688. goto out_release_dmac;
  1689. }
  1690. return 0;
  1691. out_release_dmac:
  1692. if (nfc->dmac)
  1693. dma_release_channel(nfc->dmac);
  1694. out_ahb_reset_reassert:
  1695. reset_control_assert(nfc->reset);
  1696. out_mod_clk_unprepare:
  1697. clk_disable_unprepare(nfc->mod_clk);
  1698. out_ahb_clk_unprepare:
  1699. clk_disable_unprepare(nfc->ahb_clk);
  1700. return ret;
  1701. }
  1702. static int sunxi_nfc_remove(struct platform_device *pdev)
  1703. {
  1704. struct sunxi_nfc *nfc = platform_get_drvdata(pdev);
  1705. sunxi_nand_chips_cleanup(nfc);
  1706. reset_control_assert(nfc->reset);
  1707. if (nfc->dmac)
  1708. dma_release_channel(nfc->dmac);
  1709. clk_disable_unprepare(nfc->mod_clk);
  1710. clk_disable_unprepare(nfc->ahb_clk);
  1711. return 0;
  1712. }
  1713. static const struct of_device_id sunxi_nfc_ids[] = {
  1714. { .compatible = "allwinner,sun4i-a10-nand" },
  1715. { /* sentinel */ }
  1716. };
  1717. MODULE_DEVICE_TABLE(of, sunxi_nfc_ids);
  1718. static struct platform_driver sunxi_nfc_driver = {
  1719. .driver = {
  1720. .name = "sunxi_nand",
  1721. .of_match_table = sunxi_nfc_ids,
  1722. },
  1723. .probe = sunxi_nfc_probe,
  1724. .remove = sunxi_nfc_remove,
  1725. };
  1726. module_platform_driver(sunxi_nfc_driver);
  1727. MODULE_LICENSE("GPL v2");
  1728. MODULE_AUTHOR("Boris BREZILLON");
  1729. MODULE_DESCRIPTION("Allwinner NAND Flash Controller driver");
  1730. MODULE_ALIAS("platform:sunxi_nand");