sunxi_nand.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  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_CNT0 0x0040
  59. #define NFC_REG_ECC_CNT1 0x0044
  60. #define NFC_REG_ECC_CNT2 0x0048
  61. #define NFC_REG_ECC_CNT3 0x004c
  62. #define NFC_REG_USER_DATA_BASE 0x0050
  63. #define NFC_REG_SPARE_AREA 0x00A0
  64. #define NFC_RAM0_BASE 0x0400
  65. #define NFC_RAM1_BASE 0x0800
  66. /* define bit use in NFC_CTL */
  67. #define NFC_EN BIT(0)
  68. #define NFC_RESET BIT(1)
  69. #define NFC_BUS_WIDYH BIT(2)
  70. #define NFC_RB_SEL BIT(3)
  71. #define NFC_CE_SEL GENMASK(26, 24)
  72. #define NFC_CE_CTL BIT(6)
  73. #define NFC_CE_CTL1 BIT(7)
  74. #define NFC_PAGE_SIZE GENMASK(11, 8)
  75. #define NFC_SAM BIT(12)
  76. #define NFC_RAM_METHOD BIT(14)
  77. #define NFC_DEBUG_CTL BIT(31)
  78. /* define bit use in NFC_ST */
  79. #define NFC_RB_B2R BIT(0)
  80. #define NFC_CMD_INT_FLAG BIT(1)
  81. #define NFC_DMA_INT_FLAG BIT(2)
  82. #define NFC_CMD_FIFO_STATUS BIT(3)
  83. #define NFC_STA BIT(4)
  84. #define NFC_NATCH_INT_FLAG BIT(5)
  85. #define NFC_RB_STATE0 BIT(8)
  86. #define NFC_RB_STATE1 BIT(9)
  87. #define NFC_RB_STATE2 BIT(10)
  88. #define NFC_RB_STATE3 BIT(11)
  89. /* define bit use in NFC_INT */
  90. #define NFC_B2R_INT_ENABLE BIT(0)
  91. #define NFC_CMD_INT_ENABLE BIT(1)
  92. #define NFC_DMA_INT_ENABLE BIT(2)
  93. #define NFC_INT_MASK (NFC_B2R_INT_ENABLE | \
  94. NFC_CMD_INT_ENABLE | \
  95. NFC_DMA_INT_ENABLE)
  96. /* define bit use in NFC_TIMING_CTL */
  97. #define NFC_TIMING_CTL_EDO BIT(8)
  98. /* define NFC_TIMING_CFG register layout */
  99. #define NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD) \
  100. (((tWB) & 0x3) | (((tADL) & 0x3) << 2) | \
  101. (((tWHR) & 0x3) << 4) | (((tRHW) & 0x3) << 6) | \
  102. (((tCAD) & 0x7) << 8))
  103. /* define bit use in NFC_CMD */
  104. #define NFC_CMD_LOW_BYTE GENMASK(7, 0)
  105. #define NFC_CMD_HIGH_BYTE GENMASK(15, 8)
  106. #define NFC_ADR_NUM GENMASK(18, 16)
  107. #define NFC_SEND_ADR BIT(19)
  108. #define NFC_ACCESS_DIR BIT(20)
  109. #define NFC_DATA_TRANS BIT(21)
  110. #define NFC_SEND_CMD1 BIT(22)
  111. #define NFC_WAIT_FLAG BIT(23)
  112. #define NFC_SEND_CMD2 BIT(24)
  113. #define NFC_SEQ BIT(25)
  114. #define NFC_DATA_SWAP_METHOD BIT(26)
  115. #define NFC_ROW_AUTO_INC BIT(27)
  116. #define NFC_SEND_CMD3 BIT(28)
  117. #define NFC_SEND_CMD4 BIT(29)
  118. #define NFC_CMD_TYPE GENMASK(31, 30)
  119. /* define bit use in NFC_RCMD_SET */
  120. #define NFC_READ_CMD GENMASK(7, 0)
  121. #define NFC_RANDOM_READ_CMD0 GENMASK(15, 8)
  122. #define NFC_RANDOM_READ_CMD1 GENMASK(23, 16)
  123. /* define bit use in NFC_WCMD_SET */
  124. #define NFC_PROGRAM_CMD GENMASK(7, 0)
  125. #define NFC_RANDOM_WRITE_CMD GENMASK(15, 8)
  126. #define NFC_READ_CMD0 GENMASK(23, 16)
  127. #define NFC_READ_CMD1 GENMASK(31, 24)
  128. /* define bit use in NFC_ECC_CTL */
  129. #define NFC_ECC_EN BIT(0)
  130. #define NFC_ECC_PIPELINE BIT(3)
  131. #define NFC_ECC_EXCEPTION BIT(4)
  132. #define NFC_ECC_BLOCK_SIZE BIT(5)
  133. #define NFC_RANDOM_EN BIT(9)
  134. #define NFC_RANDOM_DIRECTION BIT(10)
  135. #define NFC_ECC_MODE_SHIFT 12
  136. #define NFC_ECC_MODE GENMASK(15, 12)
  137. #define NFC_RANDOM_SEED GENMASK(30, 16)
  138. /* NFC_USER_DATA helper macros */
  139. #define NFC_BUF_TO_USER_DATA(buf) ((buf)[0] | ((buf)[1] << 8) | \
  140. ((buf)[2] << 16) | ((buf)[3] << 24))
  141. #define NFC_DEFAULT_TIMEOUT_MS 1000
  142. #define NFC_SRAM_SIZE 1024
  143. #define NFC_MAX_CS 7
  144. /*
  145. * Ready/Busy detection type: describes the Ready/Busy detection modes
  146. *
  147. * @RB_NONE: no external detection available, rely on STATUS command
  148. * and software timeouts
  149. * @RB_NATIVE: use sunxi NAND controller Ready/Busy support. The Ready/Busy
  150. * pin of the NAND flash chip must be connected to one of the
  151. * native NAND R/B pins (those which can be muxed to the NAND
  152. * Controller)
  153. * @RB_GPIO: use a simple GPIO to handle Ready/Busy status. The Ready/Busy
  154. * pin of the NAND flash chip must be connected to a GPIO capable
  155. * pin.
  156. */
  157. enum sunxi_nand_rb_type {
  158. RB_NONE,
  159. RB_NATIVE,
  160. RB_GPIO,
  161. };
  162. /*
  163. * Ready/Busy structure: stores information related to Ready/Busy detection
  164. *
  165. * @type: the Ready/Busy detection mode
  166. * @info: information related to the R/B detection mode. Either a gpio
  167. * id or a native R/B id (those supported by the NAND controller).
  168. */
  169. struct sunxi_nand_rb {
  170. enum sunxi_nand_rb_type type;
  171. union {
  172. int gpio;
  173. int nativeid;
  174. } info;
  175. };
  176. /*
  177. * Chip Select structure: stores information related to NAND Chip Select
  178. *
  179. * @cs: the NAND CS id used to communicate with a NAND Chip
  180. * @rb: the Ready/Busy description
  181. */
  182. struct sunxi_nand_chip_sel {
  183. u8 cs;
  184. struct sunxi_nand_rb rb;
  185. };
  186. /*
  187. * sunxi HW ECC infos: stores information related to HW ECC support
  188. *
  189. * @mode: the sunxi ECC mode field deduced from ECC requirements
  190. * @layout: the OOB layout depending on the ECC requirements and the
  191. * selected ECC mode
  192. */
  193. struct sunxi_nand_hw_ecc {
  194. int mode;
  195. struct nand_ecclayout layout;
  196. };
  197. /*
  198. * NAND chip structure: stores NAND chip device related information
  199. *
  200. * @node: used to store NAND chips into a list
  201. * @nand: base NAND chip structure
  202. * @mtd: base MTD structure
  203. * @clk_rate: clk_rate required for this NAND chip
  204. * @timing_cfg TIMING_CFG register value for this NAND chip
  205. * @selected: current active CS
  206. * @nsels: number of CS lines required by the NAND chip
  207. * @sels: array of CS lines descriptions
  208. */
  209. struct sunxi_nand_chip {
  210. struct list_head node;
  211. struct nand_chip nand;
  212. struct mtd_info mtd;
  213. unsigned long clk_rate;
  214. u32 timing_cfg;
  215. u32 timing_ctl;
  216. int selected;
  217. int nsels;
  218. struct sunxi_nand_chip_sel sels[0];
  219. };
  220. static inline struct sunxi_nand_chip *to_sunxi_nand(struct nand_chip *nand)
  221. {
  222. return container_of(nand, struct sunxi_nand_chip, nand);
  223. }
  224. /*
  225. * NAND Controller structure: stores sunxi NAND controller information
  226. *
  227. * @controller: base controller structure
  228. * @dev: parent device (used to print error messages)
  229. * @regs: NAND controller registers
  230. * @ahb_clk: NAND Controller AHB clock
  231. * @mod_clk: NAND Controller mod clock
  232. * @assigned_cs: bitmask describing already assigned CS lines
  233. * @clk_rate: NAND controller current clock rate
  234. * @chips: a list containing all the NAND chips attached to
  235. * this NAND controller
  236. * @complete: a completion object used to wait for NAND
  237. * controller events
  238. */
  239. struct sunxi_nfc {
  240. struct nand_hw_control controller;
  241. struct device *dev;
  242. void __iomem *regs;
  243. struct clk *ahb_clk;
  244. struct clk *mod_clk;
  245. unsigned long assigned_cs;
  246. unsigned long clk_rate;
  247. struct list_head chips;
  248. struct completion complete;
  249. };
  250. static inline struct sunxi_nfc *to_sunxi_nfc(struct nand_hw_control *ctrl)
  251. {
  252. return container_of(ctrl, struct sunxi_nfc, controller);
  253. }
  254. static irqreturn_t sunxi_nfc_interrupt(int irq, void *dev_id)
  255. {
  256. struct sunxi_nfc *nfc = dev_id;
  257. u32 st = readl(nfc->regs + NFC_REG_ST);
  258. u32 ien = readl(nfc->regs + NFC_REG_INT);
  259. if (!(ien & st))
  260. return IRQ_NONE;
  261. if ((ien & st) == ien)
  262. complete(&nfc->complete);
  263. writel(st & NFC_INT_MASK, nfc->regs + NFC_REG_ST);
  264. writel(~st & ien & NFC_INT_MASK, nfc->regs + NFC_REG_INT);
  265. return IRQ_HANDLED;
  266. }
  267. static int sunxi_nfc_wait_int(struct sunxi_nfc *nfc, u32 flags,
  268. unsigned int timeout_ms)
  269. {
  270. init_completion(&nfc->complete);
  271. writel(flags, nfc->regs + NFC_REG_INT);
  272. if (!timeout_ms)
  273. timeout_ms = NFC_DEFAULT_TIMEOUT_MS;
  274. if (!wait_for_completion_timeout(&nfc->complete,
  275. msecs_to_jiffies(timeout_ms))) {
  276. dev_err(nfc->dev, "wait interrupt timedout\n");
  277. return -ETIMEDOUT;
  278. }
  279. return 0;
  280. }
  281. static int sunxi_nfc_wait_cmd_fifo_empty(struct sunxi_nfc *nfc)
  282. {
  283. unsigned long timeout = jiffies +
  284. msecs_to_jiffies(NFC_DEFAULT_TIMEOUT_MS);
  285. do {
  286. if (!(readl(nfc->regs + NFC_REG_ST) & NFC_CMD_FIFO_STATUS))
  287. return 0;
  288. } while (time_before(jiffies, timeout));
  289. dev_err(nfc->dev, "wait for empty cmd FIFO timedout\n");
  290. return -ETIMEDOUT;
  291. }
  292. static int sunxi_nfc_rst(struct sunxi_nfc *nfc)
  293. {
  294. unsigned long timeout = jiffies +
  295. msecs_to_jiffies(NFC_DEFAULT_TIMEOUT_MS);
  296. writel(0, nfc->regs + NFC_REG_ECC_CTL);
  297. writel(NFC_RESET, nfc->regs + NFC_REG_CTL);
  298. do {
  299. if (!(readl(nfc->regs + NFC_REG_CTL) & NFC_RESET))
  300. return 0;
  301. } while (time_before(jiffies, timeout));
  302. dev_err(nfc->dev, "wait for NAND controller reset timedout\n");
  303. return -ETIMEDOUT;
  304. }
  305. static int sunxi_nfc_dev_ready(struct mtd_info *mtd)
  306. {
  307. struct nand_chip *nand = mtd->priv;
  308. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  309. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  310. struct sunxi_nand_rb *rb;
  311. unsigned long timeo = (sunxi_nand->nand.state == FL_ERASING ? 400 : 20);
  312. int ret;
  313. if (sunxi_nand->selected < 0)
  314. return 0;
  315. rb = &sunxi_nand->sels[sunxi_nand->selected].rb;
  316. switch (rb->type) {
  317. case RB_NATIVE:
  318. ret = !!(readl(nfc->regs + NFC_REG_ST) &
  319. (NFC_RB_STATE0 << rb->info.nativeid));
  320. if (ret)
  321. break;
  322. sunxi_nfc_wait_int(nfc, NFC_RB_B2R, timeo);
  323. ret = !!(readl(nfc->regs + NFC_REG_ST) &
  324. (NFC_RB_STATE0 << rb->info.nativeid));
  325. break;
  326. case RB_GPIO:
  327. ret = gpio_get_value(rb->info.gpio);
  328. break;
  329. case RB_NONE:
  330. default:
  331. ret = 0;
  332. dev_err(nfc->dev, "cannot check R/B NAND status!\n");
  333. break;
  334. }
  335. return ret;
  336. }
  337. static void sunxi_nfc_select_chip(struct mtd_info *mtd, int chip)
  338. {
  339. struct nand_chip *nand = mtd->priv;
  340. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  341. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  342. struct sunxi_nand_chip_sel *sel;
  343. u32 ctl;
  344. if (chip > 0 && chip >= sunxi_nand->nsels)
  345. return;
  346. if (chip == sunxi_nand->selected)
  347. return;
  348. ctl = readl(nfc->regs + NFC_REG_CTL) &
  349. ~(NFC_CE_SEL | NFC_RB_SEL | NFC_EN);
  350. if (chip >= 0) {
  351. sel = &sunxi_nand->sels[chip];
  352. ctl |= (sel->cs << 24) | NFC_EN |
  353. (((nand->page_shift - 10) & 0xf) << 8);
  354. if (sel->rb.type == RB_NONE) {
  355. nand->dev_ready = NULL;
  356. } else {
  357. nand->dev_ready = sunxi_nfc_dev_ready;
  358. if (sel->rb.type == RB_NATIVE)
  359. ctl |= (sel->rb.info.nativeid << 3);
  360. }
  361. writel(mtd->writesize, nfc->regs + NFC_REG_SPARE_AREA);
  362. if (nfc->clk_rate != sunxi_nand->clk_rate) {
  363. clk_set_rate(nfc->mod_clk, sunxi_nand->clk_rate);
  364. nfc->clk_rate = sunxi_nand->clk_rate;
  365. }
  366. }
  367. writel(sunxi_nand->timing_ctl, nfc->regs + NFC_REG_TIMING_CTL);
  368. writel(sunxi_nand->timing_cfg, nfc->regs + NFC_REG_TIMING_CFG);
  369. writel(ctl, nfc->regs + NFC_REG_CTL);
  370. sunxi_nand->selected = chip;
  371. }
  372. static void sunxi_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  373. {
  374. struct nand_chip *nand = mtd->priv;
  375. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  376. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  377. int ret;
  378. int cnt;
  379. int offs = 0;
  380. u32 tmp;
  381. while (len > offs) {
  382. cnt = min(len - offs, NFC_SRAM_SIZE);
  383. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  384. if (ret)
  385. break;
  386. writel(cnt, nfc->regs + NFC_REG_CNT);
  387. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD;
  388. writel(tmp, nfc->regs + NFC_REG_CMD);
  389. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  390. if (ret)
  391. break;
  392. if (buf)
  393. memcpy_fromio(buf + offs, nfc->regs + NFC_RAM0_BASE,
  394. cnt);
  395. offs += cnt;
  396. }
  397. }
  398. static void sunxi_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  399. int len)
  400. {
  401. struct nand_chip *nand = mtd->priv;
  402. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  403. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  404. int ret;
  405. int cnt;
  406. int offs = 0;
  407. u32 tmp;
  408. while (len > offs) {
  409. cnt = min(len - offs, NFC_SRAM_SIZE);
  410. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  411. if (ret)
  412. break;
  413. writel(cnt, nfc->regs + NFC_REG_CNT);
  414. memcpy_toio(nfc->regs + NFC_RAM0_BASE, buf + offs, cnt);
  415. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD |
  416. NFC_ACCESS_DIR;
  417. writel(tmp, nfc->regs + NFC_REG_CMD);
  418. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  419. if (ret)
  420. break;
  421. offs += cnt;
  422. }
  423. }
  424. static uint8_t sunxi_nfc_read_byte(struct mtd_info *mtd)
  425. {
  426. uint8_t ret;
  427. sunxi_nfc_read_buf(mtd, &ret, 1);
  428. return ret;
  429. }
  430. static void sunxi_nfc_cmd_ctrl(struct mtd_info *mtd, int dat,
  431. unsigned int ctrl)
  432. {
  433. struct nand_chip *nand = mtd->priv;
  434. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  435. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  436. int ret;
  437. u32 tmp;
  438. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  439. if (ret)
  440. return;
  441. if (ctrl & NAND_CTRL_CHANGE) {
  442. tmp = readl(nfc->regs + NFC_REG_CTL);
  443. if (ctrl & NAND_NCE)
  444. tmp |= NFC_CE_CTL;
  445. else
  446. tmp &= ~NFC_CE_CTL;
  447. writel(tmp, nfc->regs + NFC_REG_CTL);
  448. }
  449. if (dat == NAND_CMD_NONE)
  450. return;
  451. if (ctrl & NAND_CLE) {
  452. writel(NFC_SEND_CMD1 | dat, nfc->regs + NFC_REG_CMD);
  453. } else {
  454. writel(dat, nfc->regs + NFC_REG_ADDR_LOW);
  455. writel(NFC_SEND_ADR, nfc->regs + NFC_REG_CMD);
  456. }
  457. sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  458. }
  459. static int sunxi_nfc_hw_ecc_read_page(struct mtd_info *mtd,
  460. struct nand_chip *chip, uint8_t *buf,
  461. int oob_required, int page)
  462. {
  463. struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
  464. struct nand_ecc_ctrl *ecc = &chip->ecc;
  465. struct nand_ecclayout *layout = ecc->layout;
  466. struct sunxi_nand_hw_ecc *data = ecc->priv;
  467. unsigned int max_bitflips = 0;
  468. int offset;
  469. int ret;
  470. u32 tmp;
  471. int i;
  472. int cnt;
  473. tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  474. tmp &= ~(NFC_ECC_MODE | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE);
  475. tmp |= NFC_ECC_EN | (data->mode << NFC_ECC_MODE_SHIFT) |
  476. NFC_ECC_EXCEPTION;
  477. writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
  478. for (i = 0; i < ecc->steps; i++) {
  479. if (i)
  480. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, i * ecc->size, -1);
  481. offset = mtd->writesize + layout->eccpos[i * ecc->bytes] - 4;
  482. chip->read_buf(mtd, NULL, ecc->size);
  483. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
  484. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  485. if (ret)
  486. return ret;
  487. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | (1 << 30);
  488. writel(tmp, nfc->regs + NFC_REG_CMD);
  489. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  490. if (ret)
  491. return ret;
  492. memcpy_fromio(buf + (i * ecc->size),
  493. nfc->regs + NFC_RAM0_BASE, ecc->size);
  494. if (readl(nfc->regs + NFC_REG_ECC_ST) & 0x1) {
  495. mtd->ecc_stats.failed++;
  496. } else {
  497. tmp = readl(nfc->regs + NFC_REG_ECC_CNT0) & 0xff;
  498. mtd->ecc_stats.corrected += tmp;
  499. max_bitflips = max_t(unsigned int, max_bitflips, tmp);
  500. }
  501. if (oob_required) {
  502. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
  503. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  504. if (ret)
  505. return ret;
  506. offset -= mtd->writesize;
  507. chip->read_buf(mtd, chip->oob_poi + offset,
  508. ecc->bytes + 4);
  509. }
  510. }
  511. if (oob_required) {
  512. cnt = ecc->layout->oobfree[ecc->steps].length;
  513. if (cnt > 0) {
  514. offset = mtd->writesize +
  515. ecc->layout->oobfree[ecc->steps].offset;
  516. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
  517. offset -= mtd->writesize;
  518. chip->read_buf(mtd, chip->oob_poi + offset, cnt);
  519. }
  520. }
  521. tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  522. tmp &= ~NFC_ECC_EN;
  523. writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
  524. return max_bitflips;
  525. }
  526. static int sunxi_nfc_hw_ecc_write_page(struct mtd_info *mtd,
  527. struct nand_chip *chip,
  528. const uint8_t *buf, int oob_required)
  529. {
  530. struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
  531. struct nand_ecc_ctrl *ecc = &chip->ecc;
  532. struct nand_ecclayout *layout = ecc->layout;
  533. struct sunxi_nand_hw_ecc *data = ecc->priv;
  534. int offset;
  535. int ret;
  536. u32 tmp;
  537. int i;
  538. int cnt;
  539. tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  540. tmp &= ~(NFC_ECC_MODE | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE);
  541. tmp |= NFC_ECC_EN | (data->mode << NFC_ECC_MODE_SHIFT) |
  542. NFC_ECC_EXCEPTION;
  543. writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
  544. for (i = 0; i < ecc->steps; i++) {
  545. if (i)
  546. chip->cmdfunc(mtd, NAND_CMD_RNDIN, i * ecc->size, -1);
  547. chip->write_buf(mtd, buf + (i * ecc->size), ecc->size);
  548. offset = layout->eccpos[i * ecc->bytes] - 4 + mtd->writesize;
  549. /* Fill OOB data in */
  550. writel(NFC_BUF_TO_USER_DATA(chip->oob_poi +
  551. layout->oobfree[i].offset),
  552. nfc->regs + NFC_REG_USER_DATA_BASE);
  553. chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset, -1);
  554. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  555. if (ret)
  556. return ret;
  557. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | NFC_ACCESS_DIR |
  558. (1 << 30);
  559. writel(tmp, nfc->regs + NFC_REG_CMD);
  560. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  561. if (ret)
  562. return ret;
  563. }
  564. if (oob_required) {
  565. cnt = ecc->layout->oobfree[i].length;
  566. if (cnt > 0) {
  567. offset = mtd->writesize +
  568. ecc->layout->oobfree[i].offset;
  569. chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset, -1);
  570. offset -= mtd->writesize;
  571. chip->write_buf(mtd, chip->oob_poi + offset, cnt);
  572. }
  573. }
  574. tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  575. tmp &= ~NFC_ECC_EN;
  576. writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
  577. return 0;
  578. }
  579. static int sunxi_nfc_hw_syndrome_ecc_read_page(struct mtd_info *mtd,
  580. struct nand_chip *chip,
  581. uint8_t *buf, int oob_required,
  582. int page)
  583. {
  584. struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
  585. struct nand_ecc_ctrl *ecc = &chip->ecc;
  586. struct sunxi_nand_hw_ecc *data = ecc->priv;
  587. unsigned int max_bitflips = 0;
  588. uint8_t *oob = chip->oob_poi;
  589. int offset = 0;
  590. int ret;
  591. int cnt;
  592. u32 tmp;
  593. int i;
  594. tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  595. tmp &= ~(NFC_ECC_MODE | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE);
  596. tmp |= NFC_ECC_EN | (data->mode << NFC_ECC_MODE_SHIFT) |
  597. NFC_ECC_EXCEPTION;
  598. writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
  599. for (i = 0; i < ecc->steps; i++) {
  600. chip->read_buf(mtd, NULL, ecc->size);
  601. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | (1 << 30);
  602. writel(tmp, nfc->regs + NFC_REG_CMD);
  603. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  604. if (ret)
  605. return ret;
  606. memcpy_fromio(buf, nfc->regs + NFC_RAM0_BASE, ecc->size);
  607. buf += ecc->size;
  608. offset += ecc->size;
  609. if (readl(nfc->regs + NFC_REG_ECC_ST) & 0x1) {
  610. mtd->ecc_stats.failed++;
  611. } else {
  612. tmp = readl(nfc->regs + NFC_REG_ECC_CNT0) & 0xff;
  613. mtd->ecc_stats.corrected += tmp;
  614. max_bitflips = max_t(unsigned int, max_bitflips, tmp);
  615. }
  616. if (oob_required) {
  617. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
  618. chip->read_buf(mtd, oob, ecc->bytes + ecc->prepad);
  619. oob += ecc->bytes + ecc->prepad;
  620. }
  621. offset += ecc->bytes + ecc->prepad;
  622. }
  623. if (oob_required) {
  624. cnt = mtd->oobsize - (oob - chip->oob_poi);
  625. if (cnt > 0) {
  626. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, offset, -1);
  627. chip->read_buf(mtd, oob, cnt);
  628. }
  629. }
  630. writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_ECC_EN,
  631. nfc->regs + NFC_REG_ECC_CTL);
  632. return max_bitflips;
  633. }
  634. static int sunxi_nfc_hw_syndrome_ecc_write_page(struct mtd_info *mtd,
  635. struct nand_chip *chip,
  636. const uint8_t *buf,
  637. int oob_required)
  638. {
  639. struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
  640. struct nand_ecc_ctrl *ecc = &chip->ecc;
  641. struct sunxi_nand_hw_ecc *data = ecc->priv;
  642. uint8_t *oob = chip->oob_poi;
  643. int offset = 0;
  644. int ret;
  645. int cnt;
  646. u32 tmp;
  647. int i;
  648. tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  649. tmp &= ~(NFC_ECC_MODE | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE);
  650. tmp |= NFC_ECC_EN | (data->mode << NFC_ECC_MODE_SHIFT) |
  651. NFC_ECC_EXCEPTION;
  652. writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
  653. for (i = 0; i < ecc->steps; i++) {
  654. chip->write_buf(mtd, buf + (i * ecc->size), ecc->size);
  655. offset += ecc->size;
  656. /* Fill OOB data in */
  657. writel(NFC_BUF_TO_USER_DATA(oob),
  658. nfc->regs + NFC_REG_USER_DATA_BASE);
  659. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | NFC_ACCESS_DIR |
  660. (1 << 30);
  661. writel(tmp, nfc->regs + NFC_REG_CMD);
  662. ret = sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
  663. if (ret)
  664. return ret;
  665. offset += ecc->bytes + ecc->prepad;
  666. oob += ecc->bytes + ecc->prepad;
  667. }
  668. if (oob_required) {
  669. cnt = mtd->oobsize - (oob - chip->oob_poi);
  670. if (cnt > 0) {
  671. chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset, -1);
  672. chip->write_buf(mtd, oob, cnt);
  673. }
  674. }
  675. tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
  676. tmp &= ~NFC_ECC_EN;
  677. writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
  678. return 0;
  679. }
  680. static const s32 tWB_lut[] = {6, 12, 16, 20};
  681. static const s32 tRHW_lut[] = {4, 8, 12, 20};
  682. static int _sunxi_nand_lookup_timing(const s32 *lut, int lut_size, u32 duration,
  683. u32 clk_period)
  684. {
  685. u32 clk_cycles = DIV_ROUND_UP(duration, clk_period);
  686. int i;
  687. for (i = 0; i < lut_size; i++) {
  688. if (clk_cycles <= lut[i])
  689. return i;
  690. }
  691. /* Doesn't fit */
  692. return -EINVAL;
  693. }
  694. #define sunxi_nand_lookup_timing(l, p, c) \
  695. _sunxi_nand_lookup_timing(l, ARRAY_SIZE(l), p, c)
  696. static int sunxi_nand_chip_set_timings(struct sunxi_nand_chip *chip,
  697. const struct nand_sdr_timings *timings)
  698. {
  699. struct sunxi_nfc *nfc = to_sunxi_nfc(chip->nand.controller);
  700. u32 min_clk_period = 0;
  701. s32 tWB, tADL, tWHR, tRHW, tCAD;
  702. /* T1 <=> tCLS */
  703. if (timings->tCLS_min > min_clk_period)
  704. min_clk_period = timings->tCLS_min;
  705. /* T2 <=> tCLH */
  706. if (timings->tCLH_min > min_clk_period)
  707. min_clk_period = timings->tCLH_min;
  708. /* T3 <=> tCS */
  709. if (timings->tCS_min > min_clk_period)
  710. min_clk_period = timings->tCS_min;
  711. /* T4 <=> tCH */
  712. if (timings->tCH_min > min_clk_period)
  713. min_clk_period = timings->tCH_min;
  714. /* T5 <=> tWP */
  715. if (timings->tWP_min > min_clk_period)
  716. min_clk_period = timings->tWP_min;
  717. /* T6 <=> tWH */
  718. if (timings->tWH_min > min_clk_period)
  719. min_clk_period = timings->tWH_min;
  720. /* T7 <=> tALS */
  721. if (timings->tALS_min > min_clk_period)
  722. min_clk_period = timings->tALS_min;
  723. /* T8 <=> tDS */
  724. if (timings->tDS_min > min_clk_period)
  725. min_clk_period = timings->tDS_min;
  726. /* T9 <=> tDH */
  727. if (timings->tDH_min > min_clk_period)
  728. min_clk_period = timings->tDH_min;
  729. /* T10 <=> tRR */
  730. if (timings->tRR_min > (min_clk_period * 3))
  731. min_clk_period = DIV_ROUND_UP(timings->tRR_min, 3);
  732. /* T11 <=> tALH */
  733. if (timings->tALH_min > min_clk_period)
  734. min_clk_period = timings->tALH_min;
  735. /* T12 <=> tRP */
  736. if (timings->tRP_min > min_clk_period)
  737. min_clk_period = timings->tRP_min;
  738. /* T13 <=> tREH */
  739. if (timings->tREH_min > min_clk_period)
  740. min_clk_period = timings->tREH_min;
  741. /* T14 <=> tRC */
  742. if (timings->tRC_min > (min_clk_period * 2))
  743. min_clk_period = DIV_ROUND_UP(timings->tRC_min, 2);
  744. /* T15 <=> tWC */
  745. if (timings->tWC_min > (min_clk_period * 2))
  746. min_clk_period = DIV_ROUND_UP(timings->tWC_min, 2);
  747. /* T16 - T19 + tCAD */
  748. tWB = sunxi_nand_lookup_timing(tWB_lut, timings->tWB_max,
  749. min_clk_period);
  750. if (tWB < 0) {
  751. dev_err(nfc->dev, "unsupported tWB\n");
  752. return tWB;
  753. }
  754. tADL = DIV_ROUND_UP(timings->tADL_min, min_clk_period) >> 3;
  755. if (tADL > 3) {
  756. dev_err(nfc->dev, "unsupported tADL\n");
  757. return -EINVAL;
  758. }
  759. tWHR = DIV_ROUND_UP(timings->tWHR_min, min_clk_period) >> 3;
  760. if (tWHR > 3) {
  761. dev_err(nfc->dev, "unsupported tWHR\n");
  762. return -EINVAL;
  763. }
  764. tRHW = sunxi_nand_lookup_timing(tRHW_lut, timings->tRHW_min,
  765. min_clk_period);
  766. if (tRHW < 0) {
  767. dev_err(nfc->dev, "unsupported tRHW\n");
  768. return tRHW;
  769. }
  770. /*
  771. * TODO: according to ONFI specs this value only applies for DDR NAND,
  772. * but Allwinner seems to set this to 0x7. Mimic them for now.
  773. */
  774. tCAD = 0x7;
  775. /* TODO: A83 has some more bits for CDQSS, CS, CLHZ, CCS, WC */
  776. chip->timing_cfg = NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD);
  777. /*
  778. * ONFI specification 3.1, paragraph 4.15.2 dictates that EDO data
  779. * output cycle timings shall be used if the host drives tRC less than
  780. * 30 ns.
  781. */
  782. chip->timing_ctl = (timings->tRC_min < 30000) ? NFC_TIMING_CTL_EDO : 0;
  783. /* Convert min_clk_period from picoseconds to nanoseconds */
  784. min_clk_period = DIV_ROUND_UP(min_clk_period, 1000);
  785. /*
  786. * Convert min_clk_period into a clk frequency, then get the
  787. * appropriate rate for the NAND controller IP given this formula
  788. * (specified in the datasheet):
  789. * nand clk_rate = 2 * min_clk_rate
  790. */
  791. chip->clk_rate = (2 * NSEC_PER_SEC) / min_clk_period;
  792. return 0;
  793. }
  794. static int sunxi_nand_chip_init_timings(struct sunxi_nand_chip *chip,
  795. struct device_node *np)
  796. {
  797. const struct nand_sdr_timings *timings;
  798. int ret;
  799. int mode;
  800. mode = onfi_get_async_timing_mode(&chip->nand);
  801. if (mode == ONFI_TIMING_MODE_UNKNOWN) {
  802. mode = chip->nand.onfi_timing_mode_default;
  803. } else {
  804. uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {};
  805. mode = fls(mode) - 1;
  806. if (mode < 0)
  807. mode = 0;
  808. feature[0] = mode;
  809. ret = chip->nand.onfi_set_features(&chip->mtd, &chip->nand,
  810. ONFI_FEATURE_ADDR_TIMING_MODE,
  811. feature);
  812. if (ret)
  813. return ret;
  814. }
  815. timings = onfi_async_timing_mode_to_sdr_timings(mode);
  816. if (IS_ERR(timings))
  817. return PTR_ERR(timings);
  818. return sunxi_nand_chip_set_timings(chip, timings);
  819. }
  820. static int sunxi_nand_hw_common_ecc_ctrl_init(struct mtd_info *mtd,
  821. struct nand_ecc_ctrl *ecc,
  822. struct device_node *np)
  823. {
  824. static const u8 strengths[] = { 16, 24, 28, 32, 40, 48, 56, 60, 64 };
  825. struct nand_chip *nand = mtd->priv;
  826. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  827. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  828. struct sunxi_nand_hw_ecc *data;
  829. struct nand_ecclayout *layout;
  830. int nsectors;
  831. int ret;
  832. int i;
  833. data = kzalloc(sizeof(*data), GFP_KERNEL);
  834. if (!data)
  835. return -ENOMEM;
  836. /* Add ECC info retrieval from DT */
  837. for (i = 0; i < ARRAY_SIZE(strengths); i++) {
  838. if (ecc->strength <= strengths[i])
  839. break;
  840. }
  841. if (i >= ARRAY_SIZE(strengths)) {
  842. dev_err(nfc->dev, "unsupported strength\n");
  843. ret = -ENOTSUPP;
  844. goto err;
  845. }
  846. data->mode = i;
  847. /* HW ECC always request ECC bytes for 1024 bytes blocks */
  848. ecc->bytes = DIV_ROUND_UP(ecc->strength * fls(8 * 1024), 8);
  849. /* HW ECC always work with even numbers of ECC bytes */
  850. ecc->bytes = ALIGN(ecc->bytes, 2);
  851. layout = &data->layout;
  852. nsectors = mtd->writesize / ecc->size;
  853. if (mtd->oobsize < ((ecc->bytes + 4) * nsectors)) {
  854. ret = -EINVAL;
  855. goto err;
  856. }
  857. layout->eccbytes = (ecc->bytes * nsectors);
  858. ecc->layout = layout;
  859. ecc->priv = data;
  860. return 0;
  861. err:
  862. kfree(data);
  863. return ret;
  864. }
  865. static void sunxi_nand_hw_common_ecc_ctrl_cleanup(struct nand_ecc_ctrl *ecc)
  866. {
  867. kfree(ecc->priv);
  868. }
  869. static int sunxi_nand_hw_ecc_ctrl_init(struct mtd_info *mtd,
  870. struct nand_ecc_ctrl *ecc,
  871. struct device_node *np)
  872. {
  873. struct nand_ecclayout *layout;
  874. int nsectors;
  875. int i, j;
  876. int ret;
  877. ret = sunxi_nand_hw_common_ecc_ctrl_init(mtd, ecc, np);
  878. if (ret)
  879. return ret;
  880. ecc->read_page = sunxi_nfc_hw_ecc_read_page;
  881. ecc->write_page = sunxi_nfc_hw_ecc_write_page;
  882. layout = ecc->layout;
  883. nsectors = mtd->writesize / ecc->size;
  884. for (i = 0; i < nsectors; i++) {
  885. if (i) {
  886. layout->oobfree[i].offset =
  887. layout->oobfree[i - 1].offset +
  888. layout->oobfree[i - 1].length +
  889. ecc->bytes;
  890. layout->oobfree[i].length = 4;
  891. } else {
  892. /*
  893. * The first 2 bytes are used for BB markers, hence we
  894. * only have 2 bytes available in the first user data
  895. * section.
  896. */
  897. layout->oobfree[i].length = 2;
  898. layout->oobfree[i].offset = 2;
  899. }
  900. for (j = 0; j < ecc->bytes; j++)
  901. layout->eccpos[(ecc->bytes * i) + j] =
  902. layout->oobfree[i].offset +
  903. layout->oobfree[i].length + j;
  904. }
  905. if (mtd->oobsize > (ecc->bytes + 4) * nsectors) {
  906. layout->oobfree[nsectors].offset =
  907. layout->oobfree[nsectors - 1].offset +
  908. layout->oobfree[nsectors - 1].length +
  909. ecc->bytes;
  910. layout->oobfree[nsectors].length = mtd->oobsize -
  911. ((ecc->bytes + 4) * nsectors);
  912. }
  913. return 0;
  914. }
  915. static int sunxi_nand_hw_syndrome_ecc_ctrl_init(struct mtd_info *mtd,
  916. struct nand_ecc_ctrl *ecc,
  917. struct device_node *np)
  918. {
  919. struct nand_ecclayout *layout;
  920. int nsectors;
  921. int i;
  922. int ret;
  923. ret = sunxi_nand_hw_common_ecc_ctrl_init(mtd, ecc, np);
  924. if (ret)
  925. return ret;
  926. ecc->prepad = 4;
  927. ecc->read_page = sunxi_nfc_hw_syndrome_ecc_read_page;
  928. ecc->write_page = sunxi_nfc_hw_syndrome_ecc_write_page;
  929. layout = ecc->layout;
  930. nsectors = mtd->writesize / ecc->size;
  931. for (i = 0; i < (ecc->bytes * nsectors); i++)
  932. layout->eccpos[i] = i;
  933. layout->oobfree[0].length = mtd->oobsize - i;
  934. layout->oobfree[0].offset = i;
  935. return 0;
  936. }
  937. static void sunxi_nand_ecc_cleanup(struct nand_ecc_ctrl *ecc)
  938. {
  939. switch (ecc->mode) {
  940. case NAND_ECC_HW:
  941. case NAND_ECC_HW_SYNDROME:
  942. sunxi_nand_hw_common_ecc_ctrl_cleanup(ecc);
  943. break;
  944. case NAND_ECC_NONE:
  945. kfree(ecc->layout);
  946. default:
  947. break;
  948. }
  949. }
  950. static int sunxi_nand_ecc_init(struct mtd_info *mtd, struct nand_ecc_ctrl *ecc,
  951. struct device_node *np)
  952. {
  953. struct nand_chip *nand = mtd->priv;
  954. int strength;
  955. int blk_size;
  956. int ret;
  957. blk_size = of_get_nand_ecc_step_size(np);
  958. strength = of_get_nand_ecc_strength(np);
  959. if (blk_size > 0 && strength > 0) {
  960. ecc->size = blk_size;
  961. ecc->strength = strength;
  962. } else {
  963. ecc->size = nand->ecc_step_ds;
  964. ecc->strength = nand->ecc_strength_ds;
  965. }
  966. if (!ecc->size || !ecc->strength)
  967. return -EINVAL;
  968. ecc->mode = NAND_ECC_HW;
  969. ret = of_get_nand_ecc_mode(np);
  970. if (ret >= 0)
  971. ecc->mode = ret;
  972. switch (ecc->mode) {
  973. case NAND_ECC_SOFT_BCH:
  974. break;
  975. case NAND_ECC_HW:
  976. ret = sunxi_nand_hw_ecc_ctrl_init(mtd, ecc, np);
  977. if (ret)
  978. return ret;
  979. break;
  980. case NAND_ECC_HW_SYNDROME:
  981. ret = sunxi_nand_hw_syndrome_ecc_ctrl_init(mtd, ecc, np);
  982. if (ret)
  983. return ret;
  984. break;
  985. case NAND_ECC_NONE:
  986. ecc->layout = kzalloc(sizeof(*ecc->layout), GFP_KERNEL);
  987. if (!ecc->layout)
  988. return -ENOMEM;
  989. ecc->layout->oobfree[0].length = mtd->oobsize;
  990. case NAND_ECC_SOFT:
  991. break;
  992. default:
  993. return -EINVAL;
  994. }
  995. return 0;
  996. }
  997. static int sunxi_nand_chip_init(struct device *dev, struct sunxi_nfc *nfc,
  998. struct device_node *np)
  999. {
  1000. const struct nand_sdr_timings *timings;
  1001. struct sunxi_nand_chip *chip;
  1002. struct mtd_part_parser_data ppdata;
  1003. struct mtd_info *mtd;
  1004. struct nand_chip *nand;
  1005. int nsels;
  1006. int ret;
  1007. int i;
  1008. u32 tmp;
  1009. if (!of_get_property(np, "reg", &nsels))
  1010. return -EINVAL;
  1011. nsels /= sizeof(u32);
  1012. if (!nsels) {
  1013. dev_err(dev, "invalid reg property size\n");
  1014. return -EINVAL;
  1015. }
  1016. chip = devm_kzalloc(dev,
  1017. sizeof(*chip) +
  1018. (nsels * sizeof(struct sunxi_nand_chip_sel)),
  1019. GFP_KERNEL);
  1020. if (!chip) {
  1021. dev_err(dev, "could not allocate chip\n");
  1022. return -ENOMEM;
  1023. }
  1024. chip->nsels = nsels;
  1025. chip->selected = -1;
  1026. for (i = 0; i < nsels; i++) {
  1027. ret = of_property_read_u32_index(np, "reg", i, &tmp);
  1028. if (ret) {
  1029. dev_err(dev, "could not retrieve reg property: %d\n",
  1030. ret);
  1031. return ret;
  1032. }
  1033. if (tmp > NFC_MAX_CS) {
  1034. dev_err(dev,
  1035. "invalid reg value: %u (max CS = 7)\n",
  1036. tmp);
  1037. return -EINVAL;
  1038. }
  1039. if (test_and_set_bit(tmp, &nfc->assigned_cs)) {
  1040. dev_err(dev, "CS %d already assigned\n", tmp);
  1041. return -EINVAL;
  1042. }
  1043. chip->sels[i].cs = tmp;
  1044. if (!of_property_read_u32_index(np, "allwinner,rb", i, &tmp) &&
  1045. tmp < 2) {
  1046. chip->sels[i].rb.type = RB_NATIVE;
  1047. chip->sels[i].rb.info.nativeid = tmp;
  1048. } else {
  1049. ret = of_get_named_gpio(np, "rb-gpios", i);
  1050. if (ret >= 0) {
  1051. tmp = ret;
  1052. chip->sels[i].rb.type = RB_GPIO;
  1053. chip->sels[i].rb.info.gpio = tmp;
  1054. ret = devm_gpio_request(dev, tmp, "nand-rb");
  1055. if (ret)
  1056. return ret;
  1057. ret = gpio_direction_input(tmp);
  1058. if (ret)
  1059. return ret;
  1060. } else {
  1061. chip->sels[i].rb.type = RB_NONE;
  1062. }
  1063. }
  1064. }
  1065. timings = onfi_async_timing_mode_to_sdr_timings(0);
  1066. if (IS_ERR(timings)) {
  1067. ret = PTR_ERR(timings);
  1068. dev_err(dev,
  1069. "could not retrieve timings for ONFI mode 0: %d\n",
  1070. ret);
  1071. return ret;
  1072. }
  1073. ret = sunxi_nand_chip_set_timings(chip, timings);
  1074. if (ret) {
  1075. dev_err(dev, "could not configure chip timings: %d\n", ret);
  1076. return ret;
  1077. }
  1078. nand = &chip->nand;
  1079. /* Default tR value specified in the ONFI spec (chapter 4.15.1) */
  1080. nand->chip_delay = 200;
  1081. nand->controller = &nfc->controller;
  1082. nand->select_chip = sunxi_nfc_select_chip;
  1083. nand->cmd_ctrl = sunxi_nfc_cmd_ctrl;
  1084. nand->read_buf = sunxi_nfc_read_buf;
  1085. nand->write_buf = sunxi_nfc_write_buf;
  1086. nand->read_byte = sunxi_nfc_read_byte;
  1087. if (of_get_nand_on_flash_bbt(np))
  1088. nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
  1089. mtd = &chip->mtd;
  1090. mtd->dev.parent = dev;
  1091. mtd->priv = nand;
  1092. mtd->owner = THIS_MODULE;
  1093. ret = nand_scan_ident(mtd, nsels, NULL);
  1094. if (ret)
  1095. return ret;
  1096. ret = sunxi_nand_chip_init_timings(chip, np);
  1097. if (ret) {
  1098. dev_err(dev, "could not configure chip timings: %d\n", ret);
  1099. return ret;
  1100. }
  1101. ret = sunxi_nand_ecc_init(mtd, &nand->ecc, np);
  1102. if (ret) {
  1103. dev_err(dev, "ECC init failed: %d\n", ret);
  1104. return ret;
  1105. }
  1106. ret = nand_scan_tail(mtd);
  1107. if (ret) {
  1108. dev_err(dev, "nand_scan_tail failed: %d\n", ret);
  1109. return ret;
  1110. }
  1111. ppdata.of_node = np;
  1112. ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
  1113. if (ret) {
  1114. dev_err(dev, "failed to register mtd device: %d\n", ret);
  1115. nand_release(mtd);
  1116. return ret;
  1117. }
  1118. list_add_tail(&chip->node, &nfc->chips);
  1119. return 0;
  1120. }
  1121. static int sunxi_nand_chips_init(struct device *dev, struct sunxi_nfc *nfc)
  1122. {
  1123. struct device_node *np = dev->of_node;
  1124. struct device_node *nand_np;
  1125. int nchips = of_get_child_count(np);
  1126. int ret;
  1127. if (nchips > 8) {
  1128. dev_err(dev, "too many NAND chips: %d (max = 8)\n", nchips);
  1129. return -EINVAL;
  1130. }
  1131. for_each_child_of_node(np, nand_np) {
  1132. ret = sunxi_nand_chip_init(dev, nfc, nand_np);
  1133. if (ret)
  1134. return ret;
  1135. }
  1136. return 0;
  1137. }
  1138. static void sunxi_nand_chips_cleanup(struct sunxi_nfc *nfc)
  1139. {
  1140. struct sunxi_nand_chip *chip;
  1141. while (!list_empty(&nfc->chips)) {
  1142. chip = list_first_entry(&nfc->chips, struct sunxi_nand_chip,
  1143. node);
  1144. nand_release(&chip->mtd);
  1145. sunxi_nand_ecc_cleanup(&chip->nand.ecc);
  1146. list_del(&chip->node);
  1147. }
  1148. }
  1149. static int sunxi_nfc_probe(struct platform_device *pdev)
  1150. {
  1151. struct device *dev = &pdev->dev;
  1152. struct resource *r;
  1153. struct sunxi_nfc *nfc;
  1154. int irq;
  1155. int ret;
  1156. nfc = devm_kzalloc(dev, sizeof(*nfc), GFP_KERNEL);
  1157. if (!nfc)
  1158. return -ENOMEM;
  1159. nfc->dev = dev;
  1160. spin_lock_init(&nfc->controller.lock);
  1161. init_waitqueue_head(&nfc->controller.wq);
  1162. INIT_LIST_HEAD(&nfc->chips);
  1163. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1164. nfc->regs = devm_ioremap_resource(dev, r);
  1165. if (IS_ERR(nfc->regs))
  1166. return PTR_ERR(nfc->regs);
  1167. irq = platform_get_irq(pdev, 0);
  1168. if (irq < 0) {
  1169. dev_err(dev, "failed to retrieve irq\n");
  1170. return irq;
  1171. }
  1172. nfc->ahb_clk = devm_clk_get(dev, "ahb");
  1173. if (IS_ERR(nfc->ahb_clk)) {
  1174. dev_err(dev, "failed to retrieve ahb clk\n");
  1175. return PTR_ERR(nfc->ahb_clk);
  1176. }
  1177. ret = clk_prepare_enable(nfc->ahb_clk);
  1178. if (ret)
  1179. return ret;
  1180. nfc->mod_clk = devm_clk_get(dev, "mod");
  1181. if (IS_ERR(nfc->mod_clk)) {
  1182. dev_err(dev, "failed to retrieve mod clk\n");
  1183. ret = PTR_ERR(nfc->mod_clk);
  1184. goto out_ahb_clk_unprepare;
  1185. }
  1186. ret = clk_prepare_enable(nfc->mod_clk);
  1187. if (ret)
  1188. goto out_ahb_clk_unprepare;
  1189. ret = sunxi_nfc_rst(nfc);
  1190. if (ret)
  1191. goto out_mod_clk_unprepare;
  1192. writel(0, nfc->regs + NFC_REG_INT);
  1193. ret = devm_request_irq(dev, irq, sunxi_nfc_interrupt,
  1194. 0, "sunxi-nand", nfc);
  1195. if (ret)
  1196. goto out_mod_clk_unprepare;
  1197. platform_set_drvdata(pdev, nfc);
  1198. ret = sunxi_nand_chips_init(dev, nfc);
  1199. if (ret) {
  1200. dev_err(dev, "failed to init nand chips\n");
  1201. goto out_mod_clk_unprepare;
  1202. }
  1203. return 0;
  1204. out_mod_clk_unprepare:
  1205. clk_disable_unprepare(nfc->mod_clk);
  1206. out_ahb_clk_unprepare:
  1207. clk_disable_unprepare(nfc->ahb_clk);
  1208. return ret;
  1209. }
  1210. static int sunxi_nfc_remove(struct platform_device *pdev)
  1211. {
  1212. struct sunxi_nfc *nfc = platform_get_drvdata(pdev);
  1213. sunxi_nand_chips_cleanup(nfc);
  1214. return 0;
  1215. }
  1216. static const struct of_device_id sunxi_nfc_ids[] = {
  1217. { .compatible = "allwinner,sun4i-a10-nand" },
  1218. { /* sentinel */ }
  1219. };
  1220. MODULE_DEVICE_TABLE(of, sunxi_nfc_ids);
  1221. static struct platform_driver sunxi_nfc_driver = {
  1222. .driver = {
  1223. .name = "sunxi_nand",
  1224. .of_match_table = sunxi_nfc_ids,
  1225. },
  1226. .probe = sunxi_nfc_probe,
  1227. .remove = sunxi_nfc_remove,
  1228. };
  1229. module_platform_driver(sunxi_nfc_driver);
  1230. MODULE_LICENSE("GPL v2");
  1231. MODULE_AUTHOR("Boris BREZILLON");
  1232. MODULE_DESCRIPTION("Allwinner NAND Flash Controller driver");
  1233. MODULE_ALIAS("platform:sunxi_nand");