sunxi_nand.c 59 KB

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