sunxi_nand.c 36 KB

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