sunxi_nand.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  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/of_mtd.h>
  33. #include <linux/mtd/mtd.h>
  34. #include <linux/mtd/nand.h>
  35. #include <linux/mtd/partitions.h>
  36. #include <linux/clk.h>
  37. #include <linux/delay.h>
  38. #include <linux/dmaengine.h>
  39. #include <linux/gpio.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/io.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_RANDOM_EN BIT(9)
  138. #define NFC_RANDOM_DIRECTION BIT(10)
  139. #define NFC_ECC_MODE_MSK GENMASK(15, 12)
  140. #define NFC_ECC_MODE(x) ((x) << 12)
  141. #define NFC_RANDOM_SEED_MSK GENMASK(30, 16)
  142. #define NFC_RANDOM_SEED(x) ((x) << 16)
  143. /* define bit use in NFC_ECC_ST */
  144. #define NFC_ECC_ERR(x) BIT(x)
  145. #define NFC_ECC_PAT_FOUND(x) BIT(x + 16)
  146. #define NFC_ECC_ERR_CNT(b, x) (((x) >> ((b) * 8)) & 0xff)
  147. #define NFC_DEFAULT_TIMEOUT_MS 1000
  148. #define NFC_SRAM_SIZE 1024
  149. #define NFC_MAX_CS 7
  150. /*
  151. * Ready/Busy detection type: describes the Ready/Busy detection modes
  152. *
  153. * @RB_NONE: no external detection available, rely on STATUS command
  154. * and software timeouts
  155. * @RB_NATIVE: use sunxi NAND controller Ready/Busy support. The Ready/Busy
  156. * pin of the NAND flash chip must be connected to one of the
  157. * native NAND R/B pins (those which can be muxed to the NAND
  158. * Controller)
  159. * @RB_GPIO: use a simple GPIO to handle Ready/Busy status. The Ready/Busy
  160. * pin of the NAND flash chip must be connected to a GPIO capable
  161. * pin.
  162. */
  163. enum sunxi_nand_rb_type {
  164. RB_NONE,
  165. RB_NATIVE,
  166. RB_GPIO,
  167. };
  168. /*
  169. * Ready/Busy structure: stores information related to Ready/Busy detection
  170. *
  171. * @type: the Ready/Busy detection mode
  172. * @info: information related to the R/B detection mode. Either a gpio
  173. * id or a native R/B id (those supported by the NAND controller).
  174. */
  175. struct sunxi_nand_rb {
  176. enum sunxi_nand_rb_type type;
  177. union {
  178. int gpio;
  179. int nativeid;
  180. } info;
  181. };
  182. /*
  183. * Chip Select structure: stores information related to NAND Chip Select
  184. *
  185. * @cs: the NAND CS id used to communicate with a NAND Chip
  186. * @rb: the Ready/Busy description
  187. */
  188. struct sunxi_nand_chip_sel {
  189. u8 cs;
  190. struct sunxi_nand_rb rb;
  191. };
  192. /*
  193. * sunxi HW ECC infos: stores information related to HW ECC support
  194. *
  195. * @mode: the sunxi ECC mode field deduced from ECC requirements
  196. * @layout: the OOB layout depending on the ECC requirements and the
  197. * selected ECC mode
  198. */
  199. struct sunxi_nand_hw_ecc {
  200. int mode;
  201. struct nand_ecclayout layout;
  202. };
  203. /*
  204. * NAND chip structure: stores NAND chip device related information
  205. *
  206. * @node: used to store NAND chips into a list
  207. * @nand: base NAND chip structure
  208. * @mtd: base MTD structure
  209. * @clk_rate: clk_rate required for this NAND chip
  210. * @timing_cfg TIMING_CFG register value for this NAND chip
  211. * @selected: current active CS
  212. * @nsels: number of CS lines required by the NAND chip
  213. * @sels: array of CS lines descriptions
  214. */
  215. struct sunxi_nand_chip {
  216. struct list_head node;
  217. struct nand_chip nand;
  218. unsigned long clk_rate;
  219. u32 timing_cfg;
  220. u32 timing_ctl;
  221. int selected;
  222. int nsels;
  223. struct sunxi_nand_chip_sel sels[0];
  224. };
  225. static inline struct sunxi_nand_chip *to_sunxi_nand(struct nand_chip *nand)
  226. {
  227. return container_of(nand, struct sunxi_nand_chip, nand);
  228. }
  229. /*
  230. * NAND Controller structure: stores sunxi NAND controller information
  231. *
  232. * @controller: base controller structure
  233. * @dev: parent device (used to print error messages)
  234. * @regs: NAND controller registers
  235. * @ahb_clk: NAND Controller AHB clock
  236. * @mod_clk: NAND Controller mod clock
  237. * @assigned_cs: bitmask describing already assigned CS lines
  238. * @clk_rate: NAND controller current clock rate
  239. * @chips: a list containing all the NAND chips attached to
  240. * this NAND controller
  241. * @complete: a completion object used to wait for NAND
  242. * controller events
  243. */
  244. struct sunxi_nfc {
  245. struct nand_hw_control controller;
  246. struct device *dev;
  247. void __iomem *regs;
  248. struct clk *ahb_clk;
  249. struct clk *mod_clk;
  250. unsigned long assigned_cs;
  251. unsigned long clk_rate;
  252. struct list_head chips;
  253. struct completion complete;
  254. };
  255. static inline struct sunxi_nfc *to_sunxi_nfc(struct nand_hw_control *ctrl)
  256. {
  257. return container_of(ctrl, struct sunxi_nfc, controller);
  258. }
  259. static irqreturn_t sunxi_nfc_interrupt(int irq, void *dev_id)
  260. {
  261. struct sunxi_nfc *nfc = dev_id;
  262. u32 st = readl(nfc->regs + NFC_REG_ST);
  263. u32 ien = readl(nfc->regs + NFC_REG_INT);
  264. if (!(ien & st))
  265. return IRQ_NONE;
  266. if ((ien & st) == ien)
  267. complete(&nfc->complete);
  268. writel(st & NFC_INT_MASK, nfc->regs + NFC_REG_ST);
  269. writel(~st & ien & NFC_INT_MASK, nfc->regs + NFC_REG_INT);
  270. return IRQ_HANDLED;
  271. }
  272. static int sunxi_nfc_wait_int(struct sunxi_nfc *nfc, u32 flags,
  273. unsigned int timeout_ms)
  274. {
  275. init_completion(&nfc->complete);
  276. writel(flags, nfc->regs + NFC_REG_INT);
  277. if (!timeout_ms)
  278. timeout_ms = NFC_DEFAULT_TIMEOUT_MS;
  279. if (!wait_for_completion_timeout(&nfc->complete,
  280. msecs_to_jiffies(timeout_ms))) {
  281. dev_err(nfc->dev, "wait interrupt timedout\n");
  282. return -ETIMEDOUT;
  283. }
  284. return 0;
  285. }
  286. static int sunxi_nfc_wait_cmd_fifo_empty(struct sunxi_nfc *nfc)
  287. {
  288. unsigned long timeout = jiffies +
  289. msecs_to_jiffies(NFC_DEFAULT_TIMEOUT_MS);
  290. do {
  291. if (!(readl(nfc->regs + NFC_REG_ST) & NFC_CMD_FIFO_STATUS))
  292. return 0;
  293. } while (time_before(jiffies, timeout));
  294. dev_err(nfc->dev, "wait for empty cmd FIFO timedout\n");
  295. return -ETIMEDOUT;
  296. }
  297. static int sunxi_nfc_rst(struct sunxi_nfc *nfc)
  298. {
  299. unsigned long timeout = jiffies +
  300. msecs_to_jiffies(NFC_DEFAULT_TIMEOUT_MS);
  301. writel(0, nfc->regs + NFC_REG_ECC_CTL);
  302. writel(NFC_RESET, nfc->regs + NFC_REG_CTL);
  303. do {
  304. if (!(readl(nfc->regs + NFC_REG_CTL) & NFC_RESET))
  305. return 0;
  306. } while (time_before(jiffies, timeout));
  307. dev_err(nfc->dev, "wait for NAND controller reset timedout\n");
  308. return -ETIMEDOUT;
  309. }
  310. static int sunxi_nfc_dev_ready(struct mtd_info *mtd)
  311. {
  312. struct nand_chip *nand = mtd_to_nand(mtd);
  313. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  314. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  315. struct sunxi_nand_rb *rb;
  316. unsigned long timeo = (sunxi_nand->nand.state == FL_ERASING ? 400 : 20);
  317. int ret;
  318. if (sunxi_nand->selected < 0)
  319. return 0;
  320. rb = &sunxi_nand->sels[sunxi_nand->selected].rb;
  321. switch (rb->type) {
  322. case RB_NATIVE:
  323. ret = !!(readl(nfc->regs + NFC_REG_ST) &
  324. NFC_RB_STATE(rb->info.nativeid));
  325. if (ret)
  326. break;
  327. sunxi_nfc_wait_int(nfc, NFC_RB_B2R, timeo);
  328. ret = !!(readl(nfc->regs + NFC_REG_ST) &
  329. NFC_RB_STATE(rb->info.nativeid));
  330. break;
  331. case RB_GPIO:
  332. ret = gpio_get_value(rb->info.gpio);
  333. break;
  334. case RB_NONE:
  335. default:
  336. ret = 0;
  337. dev_err(nfc->dev, "cannot check R/B NAND status!\n");
  338. break;
  339. }
  340. return ret;
  341. }
  342. static void sunxi_nfc_select_chip(struct mtd_info *mtd, int chip)
  343. {
  344. struct nand_chip *nand = mtd_to_nand(mtd);
  345. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  346. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  347. struct sunxi_nand_chip_sel *sel;
  348. u32 ctl;
  349. if (chip > 0 && chip >= sunxi_nand->nsels)
  350. return;
  351. if (chip == sunxi_nand->selected)
  352. return;
  353. ctl = readl(nfc->regs + NFC_REG_CTL) &
  354. ~(NFC_PAGE_SHIFT_MSK | NFC_CE_SEL_MSK | NFC_RB_SEL_MSK | NFC_EN);
  355. if (chip >= 0) {
  356. sel = &sunxi_nand->sels[chip];
  357. ctl |= NFC_CE_SEL(sel->cs) | NFC_EN |
  358. NFC_PAGE_SHIFT(nand->page_shift - 10);
  359. if (sel->rb.type == RB_NONE) {
  360. nand->dev_ready = NULL;
  361. } else {
  362. nand->dev_ready = sunxi_nfc_dev_ready;
  363. if (sel->rb.type == RB_NATIVE)
  364. ctl |= NFC_RB_SEL(sel->rb.info.nativeid);
  365. }
  366. writel(mtd->writesize, nfc->regs + NFC_REG_SPARE_AREA);
  367. if (nfc->clk_rate != sunxi_nand->clk_rate) {
  368. clk_set_rate(nfc->mod_clk, sunxi_nand->clk_rate);
  369. nfc->clk_rate = sunxi_nand->clk_rate;
  370. }
  371. }
  372. writel(sunxi_nand->timing_ctl, nfc->regs + NFC_REG_TIMING_CTL);
  373. writel(sunxi_nand->timing_cfg, nfc->regs + NFC_REG_TIMING_CFG);
  374. writel(ctl, nfc->regs + NFC_REG_CTL);
  375. sunxi_nand->selected = chip;
  376. }
  377. static void sunxi_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  378. {
  379. struct nand_chip *nand = mtd_to_nand(mtd);
  380. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  381. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  382. int ret;
  383. int cnt;
  384. int offs = 0;
  385. u32 tmp;
  386. while (len > offs) {
  387. cnt = min(len - offs, NFC_SRAM_SIZE);
  388. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  389. if (ret)
  390. break;
  391. writel(cnt, nfc->regs + NFC_REG_CNT);
  392. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD;
  393. writel(tmp, nfc->regs + NFC_REG_CMD);
  394. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  395. if (ret)
  396. break;
  397. if (buf)
  398. memcpy_fromio(buf + offs, nfc->regs + NFC_RAM0_BASE,
  399. cnt);
  400. offs += cnt;
  401. }
  402. }
  403. static void sunxi_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  404. int len)
  405. {
  406. struct nand_chip *nand = mtd_to_nand(mtd);
  407. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  408. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  409. int ret;
  410. int cnt;
  411. int offs = 0;
  412. u32 tmp;
  413. while (len > offs) {
  414. cnt = min(len - offs, NFC_SRAM_SIZE);
  415. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  416. if (ret)
  417. break;
  418. writel(cnt, nfc->regs + NFC_REG_CNT);
  419. memcpy_toio(nfc->regs + NFC_RAM0_BASE, buf + offs, cnt);
  420. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD |
  421. NFC_ACCESS_DIR;
  422. writel(tmp, nfc->regs + NFC_REG_CMD);
  423. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  424. if (ret)
  425. break;
  426. offs += cnt;
  427. }
  428. }
  429. static uint8_t sunxi_nfc_read_byte(struct mtd_info *mtd)
  430. {
  431. uint8_t ret;
  432. sunxi_nfc_read_buf(mtd, &ret, 1);
  433. return ret;
  434. }
  435. static void sunxi_nfc_cmd_ctrl(struct mtd_info *mtd, int dat,
  436. unsigned int ctrl)
  437. {
  438. struct nand_chip *nand = mtd_to_nand(mtd);
  439. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  440. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  441. int ret;
  442. u32 tmp;
  443. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  444. if (ret)
  445. return;
  446. if (ctrl & NAND_CTRL_CHANGE) {
  447. tmp = readl(nfc->regs + NFC_REG_CTL);
  448. if (ctrl & NAND_NCE)
  449. tmp |= NFC_CE_CTL;
  450. else
  451. tmp &= ~NFC_CE_CTL;
  452. writel(tmp, nfc->regs + NFC_REG_CTL);
  453. }
  454. if (dat == NAND_CMD_NONE)
  455. return;
  456. if (ctrl & NAND_CLE) {
  457. writel(NFC_SEND_CMD1 | dat, nfc->regs + NFC_REG_CMD);
  458. } else {
  459. writel(dat, nfc->regs + NFC_REG_ADDR_LOW);
  460. writel(NFC_SEND_ADR, nfc->regs + NFC_REG_CMD);
  461. }
  462. sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  463. }
  464. /* These seed values have been extracted from Allwinner's BSP */
  465. static const u16 sunxi_nfc_randomizer_page_seeds[] = {
  466. 0x2b75, 0x0bd0, 0x5ca3, 0x62d1, 0x1c93, 0x07e9, 0x2162, 0x3a72,
  467. 0x0d67, 0x67f9, 0x1be7, 0x077d, 0x032f, 0x0dac, 0x2716, 0x2436,
  468. 0x7922, 0x1510, 0x3860, 0x5287, 0x480f, 0x4252, 0x1789, 0x5a2d,
  469. 0x2a49, 0x5e10, 0x437f, 0x4b4e, 0x2f45, 0x216e, 0x5cb7, 0x7130,
  470. 0x2a3f, 0x60e4, 0x4dc9, 0x0ef0, 0x0f52, 0x1bb9, 0x6211, 0x7a56,
  471. 0x226d, 0x4ea7, 0x6f36, 0x3692, 0x38bf, 0x0c62, 0x05eb, 0x4c55,
  472. 0x60f4, 0x728c, 0x3b6f, 0x2037, 0x7f69, 0x0936, 0x651a, 0x4ceb,
  473. 0x6218, 0x79f3, 0x383f, 0x18d9, 0x4f05, 0x5c82, 0x2912, 0x6f17,
  474. 0x6856, 0x5938, 0x1007, 0x61ab, 0x3e7f, 0x57c2, 0x542f, 0x4f62,
  475. 0x7454, 0x2eac, 0x7739, 0x42d4, 0x2f90, 0x435a, 0x2e52, 0x2064,
  476. 0x637c, 0x66ad, 0x2c90, 0x0bad, 0x759c, 0x0029, 0x0986, 0x7126,
  477. 0x1ca7, 0x1605, 0x386a, 0x27f5, 0x1380, 0x6d75, 0x24c3, 0x0f8e,
  478. 0x2b7a, 0x1418, 0x1fd1, 0x7dc1, 0x2d8e, 0x43af, 0x2267, 0x7da3,
  479. 0x4e3d, 0x1338, 0x50db, 0x454d, 0x764d, 0x40a3, 0x42e6, 0x262b,
  480. 0x2d2e, 0x1aea, 0x2e17, 0x173d, 0x3a6e, 0x71bf, 0x25f9, 0x0a5d,
  481. 0x7c57, 0x0fbe, 0x46ce, 0x4939, 0x6b17, 0x37bb, 0x3e91, 0x76db,
  482. };
  483. /*
  484. * sunxi_nfc_randomizer_ecc512_seeds and sunxi_nfc_randomizer_ecc1024_seeds
  485. * have been generated using
  486. * sunxi_nfc_randomizer_step(seed, (step_size * 8) + 15), which is what
  487. * the randomizer engine does internally before de/scrambling OOB data.
  488. *
  489. * Those tables are statically defined to avoid calculating randomizer state
  490. * at runtime.
  491. */
  492. static const u16 sunxi_nfc_randomizer_ecc512_seeds[] = {
  493. 0x3346, 0x367f, 0x1f18, 0x769a, 0x4f64, 0x068c, 0x2ef1, 0x6b64,
  494. 0x28a9, 0x15d7, 0x30f8, 0x3659, 0x53db, 0x7c5f, 0x71d4, 0x4409,
  495. 0x26eb, 0x03cc, 0x655d, 0x47d4, 0x4daa, 0x0877, 0x712d, 0x3617,
  496. 0x3264, 0x49aa, 0x7f9e, 0x588e, 0x4fbc, 0x7176, 0x7f91, 0x6c6d,
  497. 0x4b95, 0x5fb7, 0x3844, 0x4037, 0x0184, 0x081b, 0x0ee8, 0x5b91,
  498. 0x293d, 0x1f71, 0x0e6f, 0x402b, 0x5122, 0x1e52, 0x22be, 0x3d2d,
  499. 0x75bc, 0x7c60, 0x6291, 0x1a2f, 0x61d4, 0x74aa, 0x4140, 0x29ab,
  500. 0x472d, 0x2852, 0x017e, 0x15e8, 0x5ec2, 0x17cf, 0x7d0f, 0x06b8,
  501. 0x117a, 0x6b94, 0x789b, 0x3126, 0x6ac5, 0x5be7, 0x150f, 0x51f8,
  502. 0x7889, 0x0aa5, 0x663d, 0x77e8, 0x0b87, 0x3dcb, 0x360d, 0x218b,
  503. 0x512f, 0x7dc9, 0x6a4d, 0x630a, 0x3547, 0x1dd2, 0x5aea, 0x69a5,
  504. 0x7bfa, 0x5e4f, 0x1519, 0x6430, 0x3a0e, 0x5eb3, 0x5425, 0x0c7a,
  505. 0x5540, 0x3670, 0x63c1, 0x31e9, 0x5a39, 0x2de7, 0x5979, 0x2891,
  506. 0x1562, 0x014b, 0x5b05, 0x2756, 0x5a34, 0x13aa, 0x6cb5, 0x2c36,
  507. 0x5e72, 0x1306, 0x0861, 0x15ef, 0x1ee8, 0x5a37, 0x7ac4, 0x45dd,
  508. 0x44c4, 0x7266, 0x2f41, 0x3ccc, 0x045e, 0x7d40, 0x7c66, 0x0fa0,
  509. };
  510. static const u16 sunxi_nfc_randomizer_ecc1024_seeds[] = {
  511. 0x2cf5, 0x35f1, 0x63a4, 0x5274, 0x2bd2, 0x778b, 0x7285, 0x32b6,
  512. 0x6a5c, 0x70d6, 0x757d, 0x6769, 0x5375, 0x1e81, 0x0cf3, 0x3982,
  513. 0x6787, 0x042a, 0x6c49, 0x1925, 0x56a8, 0x40a9, 0x063e, 0x7bd9,
  514. 0x4dbf, 0x55ec, 0x672e, 0x7334, 0x5185, 0x4d00, 0x232a, 0x7e07,
  515. 0x445d, 0x6b92, 0x528f, 0x4255, 0x53ba, 0x7d82, 0x2a2e, 0x3a4e,
  516. 0x75eb, 0x450c, 0x6844, 0x1b5d, 0x581a, 0x4cc6, 0x0379, 0x37b2,
  517. 0x419f, 0x0e92, 0x6b27, 0x5624, 0x01e3, 0x07c1, 0x44a5, 0x130c,
  518. 0x13e8, 0x5910, 0x0876, 0x60c5, 0x54e3, 0x5b7f, 0x2269, 0x509f,
  519. 0x7665, 0x36fd, 0x3e9a, 0x0579, 0x6295, 0x14ef, 0x0a81, 0x1bcc,
  520. 0x4b16, 0x64db, 0x0514, 0x4f07, 0x0591, 0x3576, 0x6853, 0x0d9e,
  521. 0x259f, 0x38b7, 0x64fb, 0x3094, 0x4693, 0x6ddd, 0x29bb, 0x0bc8,
  522. 0x3f47, 0x490e, 0x0c0e, 0x7933, 0x3c9e, 0x5840, 0x398d, 0x3e68,
  523. 0x4af1, 0x71f5, 0x57cf, 0x1121, 0x64eb, 0x3579, 0x15ac, 0x584d,
  524. 0x5f2a, 0x47e2, 0x6528, 0x6eac, 0x196e, 0x6b96, 0x0450, 0x0179,
  525. 0x609c, 0x06e1, 0x4626, 0x42c7, 0x273e, 0x486f, 0x0705, 0x1601,
  526. 0x145b, 0x407e, 0x062b, 0x57a5, 0x53f9, 0x5659, 0x4410, 0x3ccd,
  527. };
  528. static u16 sunxi_nfc_randomizer_step(u16 state, int count)
  529. {
  530. state &= 0x7fff;
  531. /*
  532. * This loop is just a simple implementation of a Fibonacci LFSR using
  533. * the x16 + x15 + 1 polynomial.
  534. */
  535. while (count--)
  536. state = ((state >> 1) |
  537. (((state ^ (state >> 1)) & 1) << 14)) & 0x7fff;
  538. return state;
  539. }
  540. static u16 sunxi_nfc_randomizer_state(struct mtd_info *mtd, int page, bool ecc)
  541. {
  542. const u16 *seeds = sunxi_nfc_randomizer_page_seeds;
  543. int mod = mtd_div_by_ws(mtd->erasesize, mtd);
  544. if (mod > ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds))
  545. mod = ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds);
  546. if (ecc) {
  547. if (mtd->ecc_step_size == 512)
  548. seeds = sunxi_nfc_randomizer_ecc512_seeds;
  549. else
  550. seeds = sunxi_nfc_randomizer_ecc1024_seeds;
  551. }
  552. return seeds[page % mod];
  553. }
  554. static void sunxi_nfc_randomizer_config(struct mtd_info *mtd,
  555. int page, bool ecc)
  556. {
  557. struct nand_chip *nand = mtd_to_nand(mtd);
  558. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  559. u32 ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL);
  560. u16 state;
  561. if (!(nand->options & NAND_NEED_SCRAMBLING))
  562. return;
  563. ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL);
  564. state = sunxi_nfc_randomizer_state(mtd, page, ecc);
  565. ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_RANDOM_SEED_MSK;
  566. writel(ecc_ctl | NFC_RANDOM_SEED(state), nfc->regs + NFC_REG_ECC_CTL);
  567. }
  568. static void sunxi_nfc_randomizer_enable(struct mtd_info *mtd)
  569. {
  570. struct nand_chip *nand = mtd_to_nand(mtd);
  571. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  572. if (!(nand->options & NAND_NEED_SCRAMBLING))
  573. return;
  574. writel(readl(nfc->regs + NFC_REG_ECC_CTL) | NFC_RANDOM_EN,
  575. nfc->regs + NFC_REG_ECC_CTL);
  576. }
  577. static void sunxi_nfc_randomizer_disable(struct mtd_info *mtd)
  578. {
  579. struct nand_chip *nand = mtd_to_nand(mtd);
  580. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  581. if (!(nand->options & NAND_NEED_SCRAMBLING))
  582. return;
  583. writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_RANDOM_EN,
  584. nfc->regs + NFC_REG_ECC_CTL);
  585. }
  586. static void sunxi_nfc_randomize_bbm(struct mtd_info *mtd, int page, u8 *bbm)
  587. {
  588. u16 state = sunxi_nfc_randomizer_state(mtd, page, true);
  589. bbm[0] ^= state;
  590. bbm[1] ^= sunxi_nfc_randomizer_step(state, 8);
  591. }
  592. static void sunxi_nfc_randomizer_write_buf(struct mtd_info *mtd,
  593. const uint8_t *buf, int len,
  594. bool ecc, int page)
  595. {
  596. sunxi_nfc_randomizer_config(mtd, page, ecc);
  597. sunxi_nfc_randomizer_enable(mtd);
  598. sunxi_nfc_write_buf(mtd, buf, len);
  599. sunxi_nfc_randomizer_disable(mtd);
  600. }
  601. static void sunxi_nfc_randomizer_read_buf(struct mtd_info *mtd, uint8_t *buf,
  602. int len, bool ecc, int page)
  603. {
  604. sunxi_nfc_randomizer_config(mtd, page, ecc);
  605. sunxi_nfc_randomizer_enable(mtd);
  606. sunxi_nfc_read_buf(mtd, buf, len);
  607. sunxi_nfc_randomizer_disable(mtd);
  608. }
  609. static void sunxi_nfc_hw_ecc_enable(struct mtd_info *mtd)
  610. {
  611. struct nand_chip *nand = mtd_to_nand(mtd);
  612. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  613. struct sunxi_nand_hw_ecc *data = nand->ecc.priv;
  614. u32 ecc_ctl;
  615. ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL);
  616. ecc_ctl &= ~(NFC_ECC_MODE_MSK | NFC_ECC_PIPELINE |
  617. NFC_ECC_BLOCK_SIZE_MSK);
  618. ecc_ctl |= NFC_ECC_EN | NFC_ECC_MODE(data->mode) | NFC_ECC_EXCEPTION;
  619. writel(ecc_ctl, nfc->regs + NFC_REG_ECC_CTL);
  620. }
  621. static void sunxi_nfc_hw_ecc_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. writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_ECC_EN,
  626. nfc->regs + NFC_REG_ECC_CTL);
  627. }
  628. static inline void sunxi_nfc_user_data_to_buf(u32 user_data, u8 *buf)
  629. {
  630. buf[0] = user_data;
  631. buf[1] = user_data >> 8;
  632. buf[2] = user_data >> 16;
  633. buf[3] = user_data >> 24;
  634. }
  635. static int sunxi_nfc_hw_ecc_read_chunk(struct mtd_info *mtd,
  636. u8 *data, int data_off,
  637. u8 *oob, int oob_off,
  638. int *cur_off,
  639. unsigned int *max_bitflips,
  640. bool bbm, int page)
  641. {
  642. struct nand_chip *nand = mtd_to_nand(mtd);
  643. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  644. struct nand_ecc_ctrl *ecc = &nand->ecc;
  645. int raw_mode = 0;
  646. u32 status;
  647. int ret;
  648. if (*cur_off != data_off)
  649. nand->cmdfunc(mtd, NAND_CMD_RNDOUT, data_off, -1);
  650. sunxi_nfc_randomizer_read_buf(mtd, NULL, ecc->size, false, page);
  651. if (data_off + ecc->size != oob_off)
  652. nand->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_off, -1);
  653. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  654. if (ret)
  655. return ret;
  656. sunxi_nfc_randomizer_enable(mtd);
  657. writel(NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | NFC_ECC_OP,
  658. nfc->regs + NFC_REG_CMD);
  659. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  660. sunxi_nfc_randomizer_disable(mtd);
  661. if (ret)
  662. return ret;
  663. *cur_off = oob_off + ecc->bytes + 4;
  664. status = readl(nfc->regs + NFC_REG_ECC_ST);
  665. if (status & NFC_ECC_PAT_FOUND(0)) {
  666. u8 pattern = 0xff;
  667. if (unlikely(!(readl(nfc->regs + NFC_REG_PAT_ID) & 0x1)))
  668. pattern = 0x0;
  669. memset(data, pattern, ecc->size);
  670. memset(oob, pattern, ecc->bytes + 4);
  671. return 1;
  672. }
  673. ret = NFC_ECC_ERR_CNT(0, readl(nfc->regs + NFC_REG_ECC_ERR_CNT(0)));
  674. memcpy_fromio(data, nfc->regs + NFC_RAM0_BASE, ecc->size);
  675. nand->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_off, -1);
  676. sunxi_nfc_randomizer_read_buf(mtd, oob, ecc->bytes + 4, true, page);
  677. if (status & NFC_ECC_ERR(0)) {
  678. /*
  679. * Re-read the data with the randomizer disabled to identify
  680. * bitflips in erased pages.
  681. */
  682. if (nand->options & NAND_NEED_SCRAMBLING) {
  683. nand->cmdfunc(mtd, NAND_CMD_RNDOUT, data_off, -1);
  684. nand->read_buf(mtd, data, ecc->size);
  685. nand->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_off, -1);
  686. nand->read_buf(mtd, oob, ecc->bytes + 4);
  687. }
  688. ret = nand_check_erased_ecc_chunk(data, ecc->size,
  689. oob, ecc->bytes + 4,
  690. NULL, 0, ecc->strength);
  691. if (ret >= 0)
  692. raw_mode = 1;
  693. } else {
  694. /*
  695. * The engine protects 4 bytes of OOB data per chunk.
  696. * Retrieve the corrected OOB bytes.
  697. */
  698. sunxi_nfc_user_data_to_buf(readl(nfc->regs + NFC_REG_USER_DATA(0)),
  699. oob);
  700. /* De-randomize the Bad Block Marker. */
  701. if (bbm && nand->options & NAND_NEED_SCRAMBLING)
  702. sunxi_nfc_randomize_bbm(mtd, page, oob);
  703. }
  704. if (ret < 0) {
  705. mtd->ecc_stats.failed++;
  706. } else {
  707. mtd->ecc_stats.corrected += ret;
  708. *max_bitflips = max_t(unsigned int, *max_bitflips, ret);
  709. }
  710. return raw_mode;
  711. }
  712. static void sunxi_nfc_hw_ecc_read_extra_oob(struct mtd_info *mtd,
  713. u8 *oob, int *cur_off,
  714. bool randomize, int page)
  715. {
  716. struct nand_chip *nand = mtd_to_nand(mtd);
  717. struct nand_ecc_ctrl *ecc = &nand->ecc;
  718. int offset = ((ecc->bytes + 4) * ecc->steps);
  719. int len = mtd->oobsize - offset;
  720. if (len <= 0)
  721. return;
  722. if (*cur_off != offset)
  723. nand->cmdfunc(mtd, NAND_CMD_RNDOUT,
  724. offset + mtd->writesize, -1);
  725. if (!randomize)
  726. sunxi_nfc_read_buf(mtd, oob + offset, len);
  727. else
  728. sunxi_nfc_randomizer_read_buf(mtd, oob + offset, len,
  729. false, page);
  730. *cur_off = mtd->oobsize + mtd->writesize;
  731. }
  732. static inline u32 sunxi_nfc_buf_to_user_data(const u8 *buf)
  733. {
  734. return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  735. }
  736. static int sunxi_nfc_hw_ecc_write_chunk(struct mtd_info *mtd,
  737. const u8 *data, int data_off,
  738. const u8 *oob, int oob_off,
  739. int *cur_off, bool bbm,
  740. int page)
  741. {
  742. struct nand_chip *nand = mtd_to_nand(mtd);
  743. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  744. struct nand_ecc_ctrl *ecc = &nand->ecc;
  745. int ret;
  746. if (data_off != *cur_off)
  747. nand->cmdfunc(mtd, NAND_CMD_RNDIN, data_off, -1);
  748. sunxi_nfc_randomizer_write_buf(mtd, data, ecc->size, false, page);
  749. /* Fill OOB data in */
  750. if ((nand->options & NAND_NEED_SCRAMBLING) && bbm) {
  751. u8 user_data[4];
  752. memcpy(user_data, oob, 4);
  753. sunxi_nfc_randomize_bbm(mtd, page, user_data);
  754. writel(sunxi_nfc_buf_to_user_data(user_data),
  755. nfc->regs + NFC_REG_USER_DATA(0));
  756. } else {
  757. writel(sunxi_nfc_buf_to_user_data(oob),
  758. nfc->regs + NFC_REG_USER_DATA(0));
  759. }
  760. if (data_off + ecc->size != oob_off)
  761. nand->cmdfunc(mtd, NAND_CMD_RNDIN, oob_off, -1);
  762. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  763. if (ret)
  764. return ret;
  765. sunxi_nfc_randomizer_enable(mtd);
  766. writel(NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD |
  767. NFC_ACCESS_DIR | NFC_ECC_OP,
  768. nfc->regs + NFC_REG_CMD);
  769. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  770. sunxi_nfc_randomizer_disable(mtd);
  771. if (ret)
  772. return ret;
  773. *cur_off = oob_off + ecc->bytes + 4;
  774. return 0;
  775. }
  776. static void sunxi_nfc_hw_ecc_write_extra_oob(struct mtd_info *mtd,
  777. u8 *oob, int *cur_off,
  778. int page)
  779. {
  780. struct nand_chip *nand = mtd_to_nand(mtd);
  781. struct nand_ecc_ctrl *ecc = &nand->ecc;
  782. int offset = ((ecc->bytes + 4) * ecc->steps);
  783. int len = mtd->oobsize - offset;
  784. if (len <= 0)
  785. return;
  786. if (*cur_off != offset)
  787. nand->cmdfunc(mtd, NAND_CMD_RNDIN,
  788. offset + mtd->writesize, -1);
  789. sunxi_nfc_randomizer_write_buf(mtd, oob + offset, len, false, page);
  790. *cur_off = mtd->oobsize + mtd->writesize;
  791. }
  792. static int sunxi_nfc_hw_ecc_read_page(struct mtd_info *mtd,
  793. struct nand_chip *chip, uint8_t *buf,
  794. int oob_required, int page)
  795. {
  796. struct nand_ecc_ctrl *ecc = &chip->ecc;
  797. unsigned int max_bitflips = 0;
  798. int ret, i, cur_off = 0;
  799. bool raw_mode = false;
  800. sunxi_nfc_hw_ecc_enable(mtd);
  801. for (i = 0; i < ecc->steps; i++) {
  802. int data_off = i * ecc->size;
  803. int oob_off = i * (ecc->bytes + 4);
  804. u8 *data = buf + data_off;
  805. u8 *oob = chip->oob_poi + oob_off;
  806. ret = sunxi_nfc_hw_ecc_read_chunk(mtd, data, data_off, oob,
  807. oob_off + mtd->writesize,
  808. &cur_off, &max_bitflips,
  809. !i, page);
  810. if (ret < 0)
  811. return ret;
  812. else if (ret)
  813. raw_mode = true;
  814. }
  815. if (oob_required)
  816. sunxi_nfc_hw_ecc_read_extra_oob(mtd, chip->oob_poi, &cur_off,
  817. !raw_mode, page);
  818. sunxi_nfc_hw_ecc_disable(mtd);
  819. return max_bitflips;
  820. }
  821. static int sunxi_nfc_hw_ecc_write_page(struct mtd_info *mtd,
  822. struct nand_chip *chip,
  823. const uint8_t *buf, int oob_required,
  824. int page)
  825. {
  826. struct nand_ecc_ctrl *ecc = &chip->ecc;
  827. int ret, i, cur_off = 0;
  828. sunxi_nfc_hw_ecc_enable(mtd);
  829. for (i = 0; i < ecc->steps; i++) {
  830. int data_off = i * ecc->size;
  831. int oob_off = i * (ecc->bytes + 4);
  832. const u8 *data = buf + data_off;
  833. const u8 *oob = chip->oob_poi + oob_off;
  834. ret = sunxi_nfc_hw_ecc_write_chunk(mtd, data, data_off, oob,
  835. oob_off + mtd->writesize,
  836. &cur_off, !i, page);
  837. if (ret)
  838. return ret;
  839. }
  840. if (oob_required || (chip->options & NAND_NEED_SCRAMBLING))
  841. sunxi_nfc_hw_ecc_write_extra_oob(mtd, chip->oob_poi,
  842. &cur_off, page);
  843. sunxi_nfc_hw_ecc_disable(mtd);
  844. return 0;
  845. }
  846. static int sunxi_nfc_hw_syndrome_ecc_read_page(struct mtd_info *mtd,
  847. struct nand_chip *chip,
  848. uint8_t *buf, int oob_required,
  849. int page)
  850. {
  851. struct nand_ecc_ctrl *ecc = &chip->ecc;
  852. unsigned int max_bitflips = 0;
  853. int ret, i, cur_off = 0;
  854. bool raw_mode = false;
  855. sunxi_nfc_hw_ecc_enable(mtd);
  856. for (i = 0; i < ecc->steps; i++) {
  857. int data_off = i * (ecc->size + ecc->bytes + 4);
  858. int oob_off = data_off + ecc->size;
  859. u8 *data = buf + (i * ecc->size);
  860. u8 *oob = chip->oob_poi + (i * (ecc->bytes + 4));
  861. ret = sunxi_nfc_hw_ecc_read_chunk(mtd, data, data_off, oob,
  862. oob_off, &cur_off,
  863. &max_bitflips, !i, page);
  864. if (ret < 0)
  865. return ret;
  866. else if (ret)
  867. raw_mode = true;
  868. }
  869. if (oob_required)
  870. sunxi_nfc_hw_ecc_read_extra_oob(mtd, chip->oob_poi, &cur_off,
  871. !raw_mode, page);
  872. sunxi_nfc_hw_ecc_disable(mtd);
  873. return max_bitflips;
  874. }
  875. static int sunxi_nfc_hw_syndrome_ecc_write_page(struct mtd_info *mtd,
  876. struct nand_chip *chip,
  877. const uint8_t *buf,
  878. int oob_required, int page)
  879. {
  880. struct nand_ecc_ctrl *ecc = &chip->ecc;
  881. int ret, i, cur_off = 0;
  882. sunxi_nfc_hw_ecc_enable(mtd);
  883. for (i = 0; i < ecc->steps; i++) {
  884. int data_off = i * (ecc->size + ecc->bytes + 4);
  885. int oob_off = data_off + ecc->size;
  886. const u8 *data = buf + (i * ecc->size);
  887. const u8 *oob = chip->oob_poi + (i * (ecc->bytes + 4));
  888. ret = sunxi_nfc_hw_ecc_write_chunk(mtd, data, data_off,
  889. oob, oob_off, &cur_off,
  890. false, page);
  891. if (ret)
  892. return ret;
  893. }
  894. if (oob_required || (chip->options & NAND_NEED_SCRAMBLING))
  895. sunxi_nfc_hw_ecc_write_extra_oob(mtd, chip->oob_poi,
  896. &cur_off, page);
  897. sunxi_nfc_hw_ecc_disable(mtd);
  898. return 0;
  899. }
  900. static const s32 tWB_lut[] = {6, 12, 16, 20};
  901. static const s32 tRHW_lut[] = {4, 8, 12, 20};
  902. static int _sunxi_nand_lookup_timing(const s32 *lut, int lut_size, u32 duration,
  903. u32 clk_period)
  904. {
  905. u32 clk_cycles = DIV_ROUND_UP(duration, clk_period);
  906. int i;
  907. for (i = 0; i < lut_size; i++) {
  908. if (clk_cycles <= lut[i])
  909. return i;
  910. }
  911. /* Doesn't fit */
  912. return -EINVAL;
  913. }
  914. #define sunxi_nand_lookup_timing(l, p, c) \
  915. _sunxi_nand_lookup_timing(l, ARRAY_SIZE(l), p, c)
  916. static int sunxi_nand_chip_set_timings(struct sunxi_nand_chip *chip,
  917. const struct nand_sdr_timings *timings)
  918. {
  919. struct sunxi_nfc *nfc = to_sunxi_nfc(chip->nand.controller);
  920. u32 min_clk_period = 0;
  921. s32 tWB, tADL, tWHR, tRHW, tCAD;
  922. /* T1 <=> tCLS */
  923. if (timings->tCLS_min > min_clk_period)
  924. min_clk_period = timings->tCLS_min;
  925. /* T2 <=> tCLH */
  926. if (timings->tCLH_min > min_clk_period)
  927. min_clk_period = timings->tCLH_min;
  928. /* T3 <=> tCS */
  929. if (timings->tCS_min > min_clk_period)
  930. min_clk_period = timings->tCS_min;
  931. /* T4 <=> tCH */
  932. if (timings->tCH_min > min_clk_period)
  933. min_clk_period = timings->tCH_min;
  934. /* T5 <=> tWP */
  935. if (timings->tWP_min > min_clk_period)
  936. min_clk_period = timings->tWP_min;
  937. /* T6 <=> tWH */
  938. if (timings->tWH_min > min_clk_period)
  939. min_clk_period = timings->tWH_min;
  940. /* T7 <=> tALS */
  941. if (timings->tALS_min > min_clk_period)
  942. min_clk_period = timings->tALS_min;
  943. /* T8 <=> tDS */
  944. if (timings->tDS_min > min_clk_period)
  945. min_clk_period = timings->tDS_min;
  946. /* T9 <=> tDH */
  947. if (timings->tDH_min > min_clk_period)
  948. min_clk_period = timings->tDH_min;
  949. /* T10 <=> tRR */
  950. if (timings->tRR_min > (min_clk_period * 3))
  951. min_clk_period = DIV_ROUND_UP(timings->tRR_min, 3);
  952. /* T11 <=> tALH */
  953. if (timings->tALH_min > min_clk_period)
  954. min_clk_period = timings->tALH_min;
  955. /* T12 <=> tRP */
  956. if (timings->tRP_min > min_clk_period)
  957. min_clk_period = timings->tRP_min;
  958. /* T13 <=> tREH */
  959. if (timings->tREH_min > min_clk_period)
  960. min_clk_period = timings->tREH_min;
  961. /* T14 <=> tRC */
  962. if (timings->tRC_min > (min_clk_period * 2))
  963. min_clk_period = DIV_ROUND_UP(timings->tRC_min, 2);
  964. /* T15 <=> tWC */
  965. if (timings->tWC_min > (min_clk_period * 2))
  966. min_clk_period = DIV_ROUND_UP(timings->tWC_min, 2);
  967. /* T16 - T19 + tCAD */
  968. tWB = sunxi_nand_lookup_timing(tWB_lut, timings->tWB_max,
  969. min_clk_period);
  970. if (tWB < 0) {
  971. dev_err(nfc->dev, "unsupported tWB\n");
  972. return tWB;
  973. }
  974. tADL = DIV_ROUND_UP(timings->tADL_min, min_clk_period) >> 3;
  975. if (tADL > 3) {
  976. dev_err(nfc->dev, "unsupported tADL\n");
  977. return -EINVAL;
  978. }
  979. tWHR = DIV_ROUND_UP(timings->tWHR_min, min_clk_period) >> 3;
  980. if (tWHR > 3) {
  981. dev_err(nfc->dev, "unsupported tWHR\n");
  982. return -EINVAL;
  983. }
  984. tRHW = sunxi_nand_lookup_timing(tRHW_lut, timings->tRHW_min,
  985. min_clk_period);
  986. if (tRHW < 0) {
  987. dev_err(nfc->dev, "unsupported tRHW\n");
  988. return tRHW;
  989. }
  990. /*
  991. * TODO: according to ONFI specs this value only applies for DDR NAND,
  992. * but Allwinner seems to set this to 0x7. Mimic them for now.
  993. */
  994. tCAD = 0x7;
  995. /* TODO: A83 has some more bits for CDQSS, CS, CLHZ, CCS, WC */
  996. chip->timing_cfg = NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD);
  997. /*
  998. * ONFI specification 3.1, paragraph 4.15.2 dictates that EDO data
  999. * output cycle timings shall be used if the host drives tRC less than
  1000. * 30 ns.
  1001. */
  1002. chip->timing_ctl = (timings->tRC_min < 30000) ? NFC_TIMING_CTL_EDO : 0;
  1003. /* Convert min_clk_period from picoseconds to nanoseconds */
  1004. min_clk_period = DIV_ROUND_UP(min_clk_period, 1000);
  1005. /*
  1006. * Convert min_clk_period into a clk frequency, then get the
  1007. * appropriate rate for the NAND controller IP given this formula
  1008. * (specified in the datasheet):
  1009. * nand clk_rate = 2 * min_clk_rate
  1010. */
  1011. chip->clk_rate = (2 * NSEC_PER_SEC) / min_clk_period;
  1012. return 0;
  1013. }
  1014. static int sunxi_nand_chip_init_timings(struct sunxi_nand_chip *chip,
  1015. struct device_node *np)
  1016. {
  1017. struct mtd_info *mtd = nand_to_mtd(&chip->nand);
  1018. const struct nand_sdr_timings *timings;
  1019. int ret;
  1020. int mode;
  1021. mode = onfi_get_async_timing_mode(&chip->nand);
  1022. if (mode == ONFI_TIMING_MODE_UNKNOWN) {
  1023. mode = chip->nand.onfi_timing_mode_default;
  1024. } else {
  1025. uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {};
  1026. int i;
  1027. mode = fls(mode) - 1;
  1028. if (mode < 0)
  1029. mode = 0;
  1030. feature[0] = mode;
  1031. for (i = 0; i < chip->nsels; i++) {
  1032. chip->nand.select_chip(mtd, i);
  1033. ret = chip->nand.onfi_set_features(mtd, &chip->nand,
  1034. ONFI_FEATURE_ADDR_TIMING_MODE,
  1035. feature);
  1036. chip->nand.select_chip(mtd, -1);
  1037. if (ret)
  1038. return ret;
  1039. }
  1040. }
  1041. timings = onfi_async_timing_mode_to_sdr_timings(mode);
  1042. if (IS_ERR(timings))
  1043. return PTR_ERR(timings);
  1044. return sunxi_nand_chip_set_timings(chip, timings);
  1045. }
  1046. static int sunxi_nand_hw_common_ecc_ctrl_init(struct mtd_info *mtd,
  1047. struct nand_ecc_ctrl *ecc,
  1048. struct device_node *np)
  1049. {
  1050. static const u8 strengths[] = { 16, 24, 28, 32, 40, 48, 56, 60, 64 };
  1051. struct nand_chip *nand = mtd_to_nand(mtd);
  1052. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  1053. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  1054. struct sunxi_nand_hw_ecc *data;
  1055. struct nand_ecclayout *layout;
  1056. int nsectors;
  1057. int ret;
  1058. int i;
  1059. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1060. if (!data)
  1061. return -ENOMEM;
  1062. /* Add ECC info retrieval from DT */
  1063. for (i = 0; i < ARRAY_SIZE(strengths); i++) {
  1064. if (ecc->strength <= strengths[i])
  1065. break;
  1066. }
  1067. if (i >= ARRAY_SIZE(strengths)) {
  1068. dev_err(nfc->dev, "unsupported strength\n");
  1069. ret = -ENOTSUPP;
  1070. goto err;
  1071. }
  1072. data->mode = i;
  1073. /* HW ECC always request ECC bytes for 1024 bytes blocks */
  1074. ecc->bytes = DIV_ROUND_UP(ecc->strength * fls(8 * 1024), 8);
  1075. /* HW ECC always work with even numbers of ECC bytes */
  1076. ecc->bytes = ALIGN(ecc->bytes, 2);
  1077. layout = &data->layout;
  1078. nsectors = mtd->writesize / ecc->size;
  1079. if (mtd->oobsize < ((ecc->bytes + 4) * nsectors)) {
  1080. ret = -EINVAL;
  1081. goto err;
  1082. }
  1083. layout->eccbytes = (ecc->bytes * nsectors);
  1084. ecc->layout = layout;
  1085. ecc->priv = data;
  1086. return 0;
  1087. err:
  1088. kfree(data);
  1089. return ret;
  1090. }
  1091. static void sunxi_nand_hw_common_ecc_ctrl_cleanup(struct nand_ecc_ctrl *ecc)
  1092. {
  1093. kfree(ecc->priv);
  1094. }
  1095. static int sunxi_nand_hw_ecc_ctrl_init(struct mtd_info *mtd,
  1096. struct nand_ecc_ctrl *ecc,
  1097. struct device_node *np)
  1098. {
  1099. struct nand_ecclayout *layout;
  1100. int nsectors;
  1101. int i, j;
  1102. int ret;
  1103. ret = sunxi_nand_hw_common_ecc_ctrl_init(mtd, ecc, np);
  1104. if (ret)
  1105. return ret;
  1106. ecc->read_page = sunxi_nfc_hw_ecc_read_page;
  1107. ecc->write_page = sunxi_nfc_hw_ecc_write_page;
  1108. layout = ecc->layout;
  1109. nsectors = mtd->writesize / ecc->size;
  1110. for (i = 0; i < nsectors; i++) {
  1111. if (i) {
  1112. layout->oobfree[i].offset =
  1113. layout->oobfree[i - 1].offset +
  1114. layout->oobfree[i - 1].length +
  1115. ecc->bytes;
  1116. layout->oobfree[i].length = 4;
  1117. } else {
  1118. /*
  1119. * The first 2 bytes are used for BB markers, hence we
  1120. * only have 2 bytes available in the first user data
  1121. * section.
  1122. */
  1123. layout->oobfree[i].length = 2;
  1124. layout->oobfree[i].offset = 2;
  1125. }
  1126. for (j = 0; j < ecc->bytes; j++)
  1127. layout->eccpos[(ecc->bytes * i) + j] =
  1128. layout->oobfree[i].offset +
  1129. layout->oobfree[i].length + j;
  1130. }
  1131. if (mtd->oobsize > (ecc->bytes + 4) * nsectors) {
  1132. layout->oobfree[nsectors].offset =
  1133. layout->oobfree[nsectors - 1].offset +
  1134. layout->oobfree[nsectors - 1].length +
  1135. ecc->bytes;
  1136. layout->oobfree[nsectors].length = mtd->oobsize -
  1137. ((ecc->bytes + 4) * nsectors);
  1138. }
  1139. return 0;
  1140. }
  1141. static int sunxi_nand_hw_syndrome_ecc_ctrl_init(struct mtd_info *mtd,
  1142. struct nand_ecc_ctrl *ecc,
  1143. struct device_node *np)
  1144. {
  1145. struct nand_ecclayout *layout;
  1146. int nsectors;
  1147. int i;
  1148. int ret;
  1149. ret = sunxi_nand_hw_common_ecc_ctrl_init(mtd, ecc, np);
  1150. if (ret)
  1151. return ret;
  1152. ecc->prepad = 4;
  1153. ecc->read_page = sunxi_nfc_hw_syndrome_ecc_read_page;
  1154. ecc->write_page = sunxi_nfc_hw_syndrome_ecc_write_page;
  1155. layout = ecc->layout;
  1156. nsectors = mtd->writesize / ecc->size;
  1157. for (i = 0; i < (ecc->bytes * nsectors); i++)
  1158. layout->eccpos[i] = i;
  1159. layout->oobfree[0].length = mtd->oobsize - i;
  1160. layout->oobfree[0].offset = i;
  1161. return 0;
  1162. }
  1163. static void sunxi_nand_ecc_cleanup(struct nand_ecc_ctrl *ecc)
  1164. {
  1165. switch (ecc->mode) {
  1166. case NAND_ECC_HW:
  1167. case NAND_ECC_HW_SYNDROME:
  1168. sunxi_nand_hw_common_ecc_ctrl_cleanup(ecc);
  1169. break;
  1170. case NAND_ECC_NONE:
  1171. kfree(ecc->layout);
  1172. default:
  1173. break;
  1174. }
  1175. }
  1176. static int sunxi_nand_ecc_init(struct mtd_info *mtd, struct nand_ecc_ctrl *ecc,
  1177. struct device_node *np)
  1178. {
  1179. struct nand_chip *nand = mtd_to_nand(mtd);
  1180. int ret;
  1181. if (!ecc->size) {
  1182. ecc->size = nand->ecc_step_ds;
  1183. ecc->strength = nand->ecc_strength_ds;
  1184. }
  1185. if (!ecc->size || !ecc->strength)
  1186. return -EINVAL;
  1187. switch (ecc->mode) {
  1188. case NAND_ECC_SOFT_BCH:
  1189. break;
  1190. case NAND_ECC_HW:
  1191. ret = sunxi_nand_hw_ecc_ctrl_init(mtd, ecc, np);
  1192. if (ret)
  1193. return ret;
  1194. break;
  1195. case NAND_ECC_HW_SYNDROME:
  1196. ret = sunxi_nand_hw_syndrome_ecc_ctrl_init(mtd, ecc, np);
  1197. if (ret)
  1198. return ret;
  1199. break;
  1200. case NAND_ECC_NONE:
  1201. ecc->layout = kzalloc(sizeof(*ecc->layout), GFP_KERNEL);
  1202. if (!ecc->layout)
  1203. return -ENOMEM;
  1204. ecc->layout->oobfree[0].length = mtd->oobsize;
  1205. case NAND_ECC_SOFT:
  1206. break;
  1207. default:
  1208. return -EINVAL;
  1209. }
  1210. return 0;
  1211. }
  1212. static int sunxi_nand_chip_init(struct device *dev, struct sunxi_nfc *nfc,
  1213. struct device_node *np)
  1214. {
  1215. const struct nand_sdr_timings *timings;
  1216. struct sunxi_nand_chip *chip;
  1217. struct mtd_info *mtd;
  1218. struct nand_chip *nand;
  1219. int nsels;
  1220. int ret;
  1221. int i;
  1222. u32 tmp;
  1223. if (!of_get_property(np, "reg", &nsels))
  1224. return -EINVAL;
  1225. nsels /= sizeof(u32);
  1226. if (!nsels) {
  1227. dev_err(dev, "invalid reg property size\n");
  1228. return -EINVAL;
  1229. }
  1230. chip = devm_kzalloc(dev,
  1231. sizeof(*chip) +
  1232. (nsels * sizeof(struct sunxi_nand_chip_sel)),
  1233. GFP_KERNEL);
  1234. if (!chip) {
  1235. dev_err(dev, "could not allocate chip\n");
  1236. return -ENOMEM;
  1237. }
  1238. chip->nsels = nsels;
  1239. chip->selected = -1;
  1240. for (i = 0; i < nsels; i++) {
  1241. ret = of_property_read_u32_index(np, "reg", i, &tmp);
  1242. if (ret) {
  1243. dev_err(dev, "could not retrieve reg property: %d\n",
  1244. ret);
  1245. return ret;
  1246. }
  1247. if (tmp > NFC_MAX_CS) {
  1248. dev_err(dev,
  1249. "invalid reg value: %u (max CS = 7)\n",
  1250. tmp);
  1251. return -EINVAL;
  1252. }
  1253. if (test_and_set_bit(tmp, &nfc->assigned_cs)) {
  1254. dev_err(dev, "CS %d already assigned\n", tmp);
  1255. return -EINVAL;
  1256. }
  1257. chip->sels[i].cs = tmp;
  1258. if (!of_property_read_u32_index(np, "allwinner,rb", i, &tmp) &&
  1259. tmp < 2) {
  1260. chip->sels[i].rb.type = RB_NATIVE;
  1261. chip->sels[i].rb.info.nativeid = tmp;
  1262. } else {
  1263. ret = of_get_named_gpio(np, "rb-gpios", i);
  1264. if (ret >= 0) {
  1265. tmp = ret;
  1266. chip->sels[i].rb.type = RB_GPIO;
  1267. chip->sels[i].rb.info.gpio = tmp;
  1268. ret = devm_gpio_request(dev, tmp, "nand-rb");
  1269. if (ret)
  1270. return ret;
  1271. ret = gpio_direction_input(tmp);
  1272. if (ret)
  1273. return ret;
  1274. } else {
  1275. chip->sels[i].rb.type = RB_NONE;
  1276. }
  1277. }
  1278. }
  1279. timings = onfi_async_timing_mode_to_sdr_timings(0);
  1280. if (IS_ERR(timings)) {
  1281. ret = PTR_ERR(timings);
  1282. dev_err(dev,
  1283. "could not retrieve timings for ONFI mode 0: %d\n",
  1284. ret);
  1285. return ret;
  1286. }
  1287. ret = sunxi_nand_chip_set_timings(chip, timings);
  1288. if (ret) {
  1289. dev_err(dev, "could not configure chip timings: %d\n", ret);
  1290. return ret;
  1291. }
  1292. nand = &chip->nand;
  1293. /* Default tR value specified in the ONFI spec (chapter 4.15.1) */
  1294. nand->chip_delay = 200;
  1295. nand->controller = &nfc->controller;
  1296. /*
  1297. * Set the ECC mode to the default value in case nothing is specified
  1298. * in the DT.
  1299. */
  1300. nand->ecc.mode = NAND_ECC_HW;
  1301. nand_set_flash_node(nand, np);
  1302. nand->select_chip = sunxi_nfc_select_chip;
  1303. nand->cmd_ctrl = sunxi_nfc_cmd_ctrl;
  1304. nand->read_buf = sunxi_nfc_read_buf;
  1305. nand->write_buf = sunxi_nfc_write_buf;
  1306. nand->read_byte = sunxi_nfc_read_byte;
  1307. mtd = nand_to_mtd(nand);
  1308. mtd->dev.parent = dev;
  1309. ret = nand_scan_ident(mtd, nsels, NULL);
  1310. if (ret)
  1311. return ret;
  1312. if (nand->bbt_options & NAND_BBT_USE_FLASH)
  1313. nand->bbt_options |= NAND_BBT_NO_OOB;
  1314. if (nand->options & NAND_NEED_SCRAMBLING)
  1315. nand->options |= NAND_NO_SUBPAGE_WRITE;
  1316. ret = sunxi_nand_chip_init_timings(chip, np);
  1317. if (ret) {
  1318. dev_err(dev, "could not configure chip timings: %d\n", ret);
  1319. return ret;
  1320. }
  1321. ret = sunxi_nand_ecc_init(mtd, &nand->ecc, np);
  1322. if (ret) {
  1323. dev_err(dev, "ECC init failed: %d\n", ret);
  1324. return ret;
  1325. }
  1326. ret = nand_scan_tail(mtd);
  1327. if (ret) {
  1328. dev_err(dev, "nand_scan_tail failed: %d\n", ret);
  1329. return ret;
  1330. }
  1331. ret = mtd_device_register(mtd, NULL, 0);
  1332. if (ret) {
  1333. dev_err(dev, "failed to register mtd device: %d\n", ret);
  1334. nand_release(mtd);
  1335. return ret;
  1336. }
  1337. list_add_tail(&chip->node, &nfc->chips);
  1338. return 0;
  1339. }
  1340. static int sunxi_nand_chips_init(struct device *dev, struct sunxi_nfc *nfc)
  1341. {
  1342. struct device_node *np = dev->of_node;
  1343. struct device_node *nand_np;
  1344. int nchips = of_get_child_count(np);
  1345. int ret;
  1346. if (nchips > 8) {
  1347. dev_err(dev, "too many NAND chips: %d (max = 8)\n", nchips);
  1348. return -EINVAL;
  1349. }
  1350. for_each_child_of_node(np, nand_np) {
  1351. ret = sunxi_nand_chip_init(dev, nfc, nand_np);
  1352. if (ret) {
  1353. of_node_put(nand_np);
  1354. return ret;
  1355. }
  1356. }
  1357. return 0;
  1358. }
  1359. static void sunxi_nand_chips_cleanup(struct sunxi_nfc *nfc)
  1360. {
  1361. struct sunxi_nand_chip *chip;
  1362. while (!list_empty(&nfc->chips)) {
  1363. chip = list_first_entry(&nfc->chips, struct sunxi_nand_chip,
  1364. node);
  1365. nand_release(nand_to_mtd(&chip->nand));
  1366. sunxi_nand_ecc_cleanup(&chip->nand.ecc);
  1367. list_del(&chip->node);
  1368. }
  1369. }
  1370. static int sunxi_nfc_probe(struct platform_device *pdev)
  1371. {
  1372. struct device *dev = &pdev->dev;
  1373. struct resource *r;
  1374. struct sunxi_nfc *nfc;
  1375. int irq;
  1376. int ret;
  1377. nfc = devm_kzalloc(dev, sizeof(*nfc), GFP_KERNEL);
  1378. if (!nfc)
  1379. return -ENOMEM;
  1380. nfc->dev = dev;
  1381. spin_lock_init(&nfc->controller.lock);
  1382. init_waitqueue_head(&nfc->controller.wq);
  1383. INIT_LIST_HEAD(&nfc->chips);
  1384. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1385. nfc->regs = devm_ioremap_resource(dev, r);
  1386. if (IS_ERR(nfc->regs))
  1387. return PTR_ERR(nfc->regs);
  1388. irq = platform_get_irq(pdev, 0);
  1389. if (irq < 0) {
  1390. dev_err(dev, "failed to retrieve irq\n");
  1391. return irq;
  1392. }
  1393. nfc->ahb_clk = devm_clk_get(dev, "ahb");
  1394. if (IS_ERR(nfc->ahb_clk)) {
  1395. dev_err(dev, "failed to retrieve ahb clk\n");
  1396. return PTR_ERR(nfc->ahb_clk);
  1397. }
  1398. ret = clk_prepare_enable(nfc->ahb_clk);
  1399. if (ret)
  1400. return ret;
  1401. nfc->mod_clk = devm_clk_get(dev, "mod");
  1402. if (IS_ERR(nfc->mod_clk)) {
  1403. dev_err(dev, "failed to retrieve mod clk\n");
  1404. ret = PTR_ERR(nfc->mod_clk);
  1405. goto out_ahb_clk_unprepare;
  1406. }
  1407. ret = clk_prepare_enable(nfc->mod_clk);
  1408. if (ret)
  1409. goto out_ahb_clk_unprepare;
  1410. ret = sunxi_nfc_rst(nfc);
  1411. if (ret)
  1412. goto out_mod_clk_unprepare;
  1413. writel(0, nfc->regs + NFC_REG_INT);
  1414. ret = devm_request_irq(dev, irq, sunxi_nfc_interrupt,
  1415. 0, "sunxi-nand", nfc);
  1416. if (ret)
  1417. goto out_mod_clk_unprepare;
  1418. platform_set_drvdata(pdev, nfc);
  1419. ret = sunxi_nand_chips_init(dev, nfc);
  1420. if (ret) {
  1421. dev_err(dev, "failed to init nand chips\n");
  1422. goto out_mod_clk_unprepare;
  1423. }
  1424. return 0;
  1425. out_mod_clk_unprepare:
  1426. clk_disable_unprepare(nfc->mod_clk);
  1427. out_ahb_clk_unprepare:
  1428. clk_disable_unprepare(nfc->ahb_clk);
  1429. return ret;
  1430. }
  1431. static int sunxi_nfc_remove(struct platform_device *pdev)
  1432. {
  1433. struct sunxi_nfc *nfc = platform_get_drvdata(pdev);
  1434. sunxi_nand_chips_cleanup(nfc);
  1435. return 0;
  1436. }
  1437. static const struct of_device_id sunxi_nfc_ids[] = {
  1438. { .compatible = "allwinner,sun4i-a10-nand" },
  1439. { /* sentinel */ }
  1440. };
  1441. MODULE_DEVICE_TABLE(of, sunxi_nfc_ids);
  1442. static struct platform_driver sunxi_nfc_driver = {
  1443. .driver = {
  1444. .name = "sunxi_nand",
  1445. .of_match_table = sunxi_nfc_ids,
  1446. },
  1447. .probe = sunxi_nfc_probe,
  1448. .remove = sunxi_nfc_remove,
  1449. };
  1450. module_platform_driver(sunxi_nfc_driver);
  1451. MODULE_LICENSE("GPL v2");
  1452. MODULE_AUTHOR("Boris BREZILLON");
  1453. MODULE_DESCRIPTION("Allwinner NAND Flash Controller driver");
  1454. MODULE_ALIAS("platform:sunxi_nand");