sunxi_nand.c 34 KB

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