sunxi_nand.c 37 KB

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