sunxi_nand.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  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/mtd/mtd.h>
  33. #include <linux/mtd/rawnand.h>
  34. #include <linux/mtd/partitions.h>
  35. #include <linux/clk.h>
  36. #include <linux/delay.h>
  37. #include <linux/dmaengine.h>
  38. #include <linux/gpio.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/iopoll.h>
  41. #include <linux/reset.h>
  42. #define NFC_REG_CTL 0x0000
  43. #define NFC_REG_ST 0x0004
  44. #define NFC_REG_INT 0x0008
  45. #define NFC_REG_TIMING_CTL 0x000C
  46. #define NFC_REG_TIMING_CFG 0x0010
  47. #define NFC_REG_ADDR_LOW 0x0014
  48. #define NFC_REG_ADDR_HIGH 0x0018
  49. #define NFC_REG_SECTOR_NUM 0x001C
  50. #define NFC_REG_CNT 0x0020
  51. #define NFC_REG_CMD 0x0024
  52. #define NFC_REG_RCMD_SET 0x0028
  53. #define NFC_REG_WCMD_SET 0x002C
  54. #define NFC_REG_IO_DATA 0x0030
  55. #define NFC_REG_ECC_CTL 0x0034
  56. #define NFC_REG_ECC_ST 0x0038
  57. #define NFC_REG_DEBUG 0x003C
  58. #define NFC_REG_ECC_ERR_CNT(x) ((0x0040 + (x)) & ~0x3)
  59. #define NFC_REG_USER_DATA(x) (0x0050 + ((x) * 4))
  60. #define NFC_REG_SPARE_AREA 0x00A0
  61. #define NFC_REG_PAT_ID 0x00A4
  62. #define NFC_RAM0_BASE 0x0400
  63. #define NFC_RAM1_BASE 0x0800
  64. /* define bit use in NFC_CTL */
  65. #define NFC_EN BIT(0)
  66. #define NFC_RESET BIT(1)
  67. #define NFC_BUS_WIDTH_MSK BIT(2)
  68. #define NFC_BUS_WIDTH_8 (0 << 2)
  69. #define NFC_BUS_WIDTH_16 (1 << 2)
  70. #define NFC_RB_SEL_MSK BIT(3)
  71. #define NFC_RB_SEL(x) ((x) << 3)
  72. #define NFC_CE_SEL_MSK GENMASK(26, 24)
  73. #define NFC_CE_SEL(x) ((x) << 24)
  74. #define NFC_CE_CTL BIT(6)
  75. #define NFC_PAGE_SHIFT_MSK GENMASK(11, 8)
  76. #define NFC_PAGE_SHIFT(x) (((x) < 10 ? 0 : (x) - 10) << 8)
  77. #define NFC_SAM BIT(12)
  78. #define NFC_RAM_METHOD BIT(14)
  79. #define NFC_DEBUG_CTL BIT(31)
  80. /* define bit use in NFC_ST */
  81. #define NFC_RB_B2R BIT(0)
  82. #define NFC_CMD_INT_FLAG BIT(1)
  83. #define NFC_DMA_INT_FLAG BIT(2)
  84. #define NFC_CMD_FIFO_STATUS BIT(3)
  85. #define NFC_STA BIT(4)
  86. #define NFC_NATCH_INT_FLAG BIT(5)
  87. #define NFC_RB_STATE(x) BIT(x + 8)
  88. /* define bit use in NFC_INT */
  89. #define NFC_B2R_INT_ENABLE BIT(0)
  90. #define NFC_CMD_INT_ENABLE BIT(1)
  91. #define NFC_DMA_INT_ENABLE BIT(2)
  92. #define NFC_INT_MASK (NFC_B2R_INT_ENABLE | \
  93. NFC_CMD_INT_ENABLE | \
  94. NFC_DMA_INT_ENABLE)
  95. /* define bit use in NFC_TIMING_CTL */
  96. #define NFC_TIMING_CTL_EDO BIT(8)
  97. /* define NFC_TIMING_CFG register layout */
  98. #define NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD) \
  99. (((tWB) & 0x3) | (((tADL) & 0x3) << 2) | \
  100. (((tWHR) & 0x3) << 4) | (((tRHW) & 0x3) << 6) | \
  101. (((tCAD) & 0x7) << 8))
  102. /* define bit use in NFC_CMD */
  103. #define NFC_CMD_LOW_BYTE_MSK GENMASK(7, 0)
  104. #define NFC_CMD_HIGH_BYTE_MSK GENMASK(15, 8)
  105. #define NFC_CMD(x) (x)
  106. #define NFC_ADR_NUM_MSK GENMASK(18, 16)
  107. #define NFC_ADR_NUM(x) (((x) - 1) << 16)
  108. #define NFC_SEND_ADR BIT(19)
  109. #define NFC_ACCESS_DIR BIT(20)
  110. #define NFC_DATA_TRANS BIT(21)
  111. #define NFC_SEND_CMD1 BIT(22)
  112. #define NFC_WAIT_FLAG BIT(23)
  113. #define NFC_SEND_CMD2 BIT(24)
  114. #define NFC_SEQ BIT(25)
  115. #define NFC_DATA_SWAP_METHOD BIT(26)
  116. #define NFC_ROW_AUTO_INC BIT(27)
  117. #define NFC_SEND_CMD3 BIT(28)
  118. #define NFC_SEND_CMD4 BIT(29)
  119. #define NFC_CMD_TYPE_MSK GENMASK(31, 30)
  120. #define NFC_NORMAL_OP (0 << 30)
  121. #define NFC_ECC_OP (1 << 30)
  122. #define NFC_PAGE_OP (2 << 30)
  123. /* define bit use in NFC_RCMD_SET */
  124. #define NFC_READ_CMD_MSK GENMASK(7, 0)
  125. #define NFC_RND_READ_CMD0_MSK GENMASK(15, 8)
  126. #define NFC_RND_READ_CMD1_MSK GENMASK(23, 16)
  127. /* define bit use in NFC_WCMD_SET */
  128. #define NFC_PROGRAM_CMD_MSK GENMASK(7, 0)
  129. #define NFC_RND_WRITE_CMD_MSK GENMASK(15, 8)
  130. #define NFC_READ_CMD0_MSK GENMASK(23, 16)
  131. #define NFC_READ_CMD1_MSK GENMASK(31, 24)
  132. /* define bit use in NFC_ECC_CTL */
  133. #define NFC_ECC_EN BIT(0)
  134. #define NFC_ECC_PIPELINE BIT(3)
  135. #define NFC_ECC_EXCEPTION BIT(4)
  136. #define NFC_ECC_BLOCK_SIZE_MSK BIT(5)
  137. #define NFC_ECC_BLOCK_512 BIT(5)
  138. #define NFC_RANDOM_EN BIT(9)
  139. #define NFC_RANDOM_DIRECTION BIT(10)
  140. #define NFC_ECC_MODE_MSK GENMASK(15, 12)
  141. #define NFC_ECC_MODE(x) ((x) << 12)
  142. #define NFC_RANDOM_SEED_MSK GENMASK(30, 16)
  143. #define NFC_RANDOM_SEED(x) ((x) << 16)
  144. /* define bit use in NFC_ECC_ST */
  145. #define NFC_ECC_ERR(x) BIT(x)
  146. #define NFC_ECC_ERR_MSK GENMASK(15, 0)
  147. #define NFC_ECC_PAT_FOUND(x) BIT(x + 16)
  148. #define NFC_ECC_ERR_CNT(b, x) (((x) >> (((b) % 4) * 8)) & 0xff)
  149. #define NFC_DEFAULT_TIMEOUT_MS 1000
  150. #define NFC_SRAM_SIZE 1024
  151. #define NFC_MAX_CS 7
  152. /*
  153. * Chip Select structure: stores information related to NAND Chip Select
  154. *
  155. * @cs: the NAND CS id used to communicate with a NAND Chip
  156. * @rb: the Ready/Busy pin ID. -1 means no R/B pin connected to the
  157. * NFC
  158. */
  159. struct sunxi_nand_chip_sel {
  160. u8 cs;
  161. s8 rb;
  162. };
  163. /*
  164. * sunxi HW ECC infos: stores information related to HW ECC support
  165. *
  166. * @mode: the sunxi ECC mode field deduced from ECC requirements
  167. */
  168. struct sunxi_nand_hw_ecc {
  169. int mode;
  170. };
  171. /*
  172. * NAND chip structure: stores NAND chip device related information
  173. *
  174. * @node: used to store NAND chips into a list
  175. * @nand: base NAND chip structure
  176. * @mtd: base MTD structure
  177. * @clk_rate: clk_rate required for this NAND chip
  178. * @timing_cfg TIMING_CFG register value for this NAND chip
  179. * @selected: current active CS
  180. * @nsels: number of CS lines required by the NAND chip
  181. * @sels: array of CS lines descriptions
  182. */
  183. struct sunxi_nand_chip {
  184. struct list_head node;
  185. struct nand_chip nand;
  186. unsigned long clk_rate;
  187. u32 timing_cfg;
  188. u32 timing_ctl;
  189. int selected;
  190. int addr_cycles;
  191. u32 addr[2];
  192. int cmd_cycles;
  193. u8 cmd[2];
  194. int nsels;
  195. struct sunxi_nand_chip_sel sels[0];
  196. };
  197. static inline struct sunxi_nand_chip *to_sunxi_nand(struct nand_chip *nand)
  198. {
  199. return container_of(nand, struct sunxi_nand_chip, nand);
  200. }
  201. /*
  202. * NAND Controller structure: stores sunxi NAND controller information
  203. *
  204. * @controller: base controller structure
  205. * @dev: parent device (used to print error messages)
  206. * @regs: NAND controller registers
  207. * @ahb_clk: NAND Controller AHB clock
  208. * @mod_clk: NAND Controller mod clock
  209. * @assigned_cs: bitmask describing already assigned CS lines
  210. * @clk_rate: NAND controller current clock rate
  211. * @chips: a list containing all the NAND chips attached to
  212. * this NAND controller
  213. * @complete: a completion object used to wait for NAND
  214. * controller events
  215. */
  216. struct sunxi_nfc {
  217. struct nand_hw_control controller;
  218. struct device *dev;
  219. void __iomem *regs;
  220. struct clk *ahb_clk;
  221. struct clk *mod_clk;
  222. struct reset_control *reset;
  223. unsigned long assigned_cs;
  224. unsigned long clk_rate;
  225. struct list_head chips;
  226. struct completion complete;
  227. struct dma_chan *dmac;
  228. };
  229. static inline struct sunxi_nfc *to_sunxi_nfc(struct nand_hw_control *ctrl)
  230. {
  231. return container_of(ctrl, struct sunxi_nfc, controller);
  232. }
  233. static irqreturn_t sunxi_nfc_interrupt(int irq, void *dev_id)
  234. {
  235. struct sunxi_nfc *nfc = dev_id;
  236. u32 st = readl(nfc->regs + NFC_REG_ST);
  237. u32 ien = readl(nfc->regs + NFC_REG_INT);
  238. if (!(ien & st))
  239. return IRQ_NONE;
  240. if ((ien & st) == ien)
  241. complete(&nfc->complete);
  242. writel(st & NFC_INT_MASK, nfc->regs + NFC_REG_ST);
  243. writel(~st & ien & NFC_INT_MASK, nfc->regs + NFC_REG_INT);
  244. return IRQ_HANDLED;
  245. }
  246. static int sunxi_nfc_wait_events(struct sunxi_nfc *nfc, u32 events,
  247. bool use_polling, unsigned int timeout_ms)
  248. {
  249. int ret;
  250. if (events & ~NFC_INT_MASK)
  251. return -EINVAL;
  252. if (!timeout_ms)
  253. timeout_ms = NFC_DEFAULT_TIMEOUT_MS;
  254. if (!use_polling) {
  255. init_completion(&nfc->complete);
  256. writel(events, nfc->regs + NFC_REG_INT);
  257. ret = wait_for_completion_timeout(&nfc->complete,
  258. msecs_to_jiffies(timeout_ms));
  259. if (!ret)
  260. ret = -ETIMEDOUT;
  261. else
  262. ret = 0;
  263. writel(0, nfc->regs + NFC_REG_INT);
  264. } else {
  265. u32 status;
  266. ret = readl_poll_timeout(nfc->regs + NFC_REG_ST, status,
  267. (status & events) == events, 1,
  268. timeout_ms * 1000);
  269. }
  270. writel(events & NFC_INT_MASK, nfc->regs + NFC_REG_ST);
  271. if (ret)
  272. dev_err(nfc->dev, "wait interrupt timedout\n");
  273. return ret;
  274. }
  275. static int sunxi_nfc_wait_cmd_fifo_empty(struct sunxi_nfc *nfc)
  276. {
  277. u32 status;
  278. int ret;
  279. ret = readl_poll_timeout(nfc->regs + NFC_REG_ST, status,
  280. !(status & NFC_CMD_FIFO_STATUS), 1,
  281. NFC_DEFAULT_TIMEOUT_MS * 1000);
  282. if (ret)
  283. dev_err(nfc->dev, "wait for empty cmd FIFO timedout\n");
  284. return ret;
  285. }
  286. static int sunxi_nfc_rst(struct sunxi_nfc *nfc)
  287. {
  288. u32 ctl;
  289. int ret;
  290. writel(0, nfc->regs + NFC_REG_ECC_CTL);
  291. writel(NFC_RESET, nfc->regs + NFC_REG_CTL);
  292. ret = readl_poll_timeout(nfc->regs + NFC_REG_CTL, ctl,
  293. !(ctl & NFC_RESET), 1,
  294. NFC_DEFAULT_TIMEOUT_MS * 1000);
  295. if (ret)
  296. dev_err(nfc->dev, "wait for NAND controller reset timedout\n");
  297. return ret;
  298. }
  299. static int sunxi_nfc_dma_op_prepare(struct mtd_info *mtd, const void *buf,
  300. int chunksize, int nchunks,
  301. enum dma_data_direction ddir,
  302. struct scatterlist *sg)
  303. {
  304. struct nand_chip *nand = mtd_to_nand(mtd);
  305. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  306. struct dma_async_tx_descriptor *dmad;
  307. enum dma_transfer_direction tdir;
  308. dma_cookie_t dmat;
  309. int ret;
  310. if (ddir == DMA_FROM_DEVICE)
  311. tdir = DMA_DEV_TO_MEM;
  312. else
  313. tdir = DMA_MEM_TO_DEV;
  314. sg_init_one(sg, buf, nchunks * chunksize);
  315. ret = dma_map_sg(nfc->dev, sg, 1, ddir);
  316. if (!ret)
  317. return -ENOMEM;
  318. dmad = dmaengine_prep_slave_sg(nfc->dmac, sg, 1, tdir, DMA_CTRL_ACK);
  319. if (!dmad) {
  320. ret = -EINVAL;
  321. goto err_unmap_buf;
  322. }
  323. writel(readl(nfc->regs + NFC_REG_CTL) | NFC_RAM_METHOD,
  324. nfc->regs + NFC_REG_CTL);
  325. writel(nchunks, nfc->regs + NFC_REG_SECTOR_NUM);
  326. writel(chunksize, nfc->regs + NFC_REG_CNT);
  327. dmat = dmaengine_submit(dmad);
  328. ret = dma_submit_error(dmat);
  329. if (ret)
  330. goto err_clr_dma_flag;
  331. return 0;
  332. err_clr_dma_flag:
  333. writel(readl(nfc->regs + NFC_REG_CTL) & ~NFC_RAM_METHOD,
  334. nfc->regs + NFC_REG_CTL);
  335. err_unmap_buf:
  336. dma_unmap_sg(nfc->dev, sg, 1, ddir);
  337. return ret;
  338. }
  339. static void sunxi_nfc_dma_op_cleanup(struct mtd_info *mtd,
  340. enum dma_data_direction ddir,
  341. struct scatterlist *sg)
  342. {
  343. struct nand_chip *nand = mtd_to_nand(mtd);
  344. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  345. dma_unmap_sg(nfc->dev, sg, 1, ddir);
  346. writel(readl(nfc->regs + NFC_REG_CTL) & ~NFC_RAM_METHOD,
  347. nfc->regs + NFC_REG_CTL);
  348. }
  349. static int sunxi_nfc_dev_ready(struct mtd_info *mtd)
  350. {
  351. struct nand_chip *nand = mtd_to_nand(mtd);
  352. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  353. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  354. u32 mask;
  355. if (sunxi_nand->selected < 0)
  356. return 0;
  357. if (sunxi_nand->sels[sunxi_nand->selected].rb < 0) {
  358. dev_err(nfc->dev, "cannot check R/B NAND status!\n");
  359. return 0;
  360. }
  361. mask = NFC_RB_STATE(sunxi_nand->sels[sunxi_nand->selected].rb);
  362. return !!(readl(nfc->regs + NFC_REG_ST) & mask);
  363. }
  364. static void sunxi_nfc_select_chip(struct mtd_info *mtd, int chip)
  365. {
  366. struct nand_chip *nand = mtd_to_nand(mtd);
  367. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  368. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  369. struct sunxi_nand_chip_sel *sel;
  370. u32 ctl;
  371. if (chip > 0 && chip >= sunxi_nand->nsels)
  372. return;
  373. if (chip == sunxi_nand->selected)
  374. return;
  375. ctl = readl(nfc->regs + NFC_REG_CTL) &
  376. ~(NFC_PAGE_SHIFT_MSK | NFC_CE_SEL_MSK | NFC_RB_SEL_MSK | NFC_EN);
  377. if (chip >= 0) {
  378. sel = &sunxi_nand->sels[chip];
  379. ctl |= NFC_CE_SEL(sel->cs) | NFC_EN |
  380. NFC_PAGE_SHIFT(nand->page_shift);
  381. if (sel->rb < 0) {
  382. nand->dev_ready = NULL;
  383. } else {
  384. nand->dev_ready = sunxi_nfc_dev_ready;
  385. ctl |= NFC_RB_SEL(sel->rb);
  386. }
  387. writel(mtd->writesize, nfc->regs + NFC_REG_SPARE_AREA);
  388. if (nfc->clk_rate != sunxi_nand->clk_rate) {
  389. clk_set_rate(nfc->mod_clk, sunxi_nand->clk_rate);
  390. nfc->clk_rate = sunxi_nand->clk_rate;
  391. }
  392. }
  393. writel(sunxi_nand->timing_ctl, nfc->regs + NFC_REG_TIMING_CTL);
  394. writel(sunxi_nand->timing_cfg, nfc->regs + NFC_REG_TIMING_CFG);
  395. writel(ctl, nfc->regs + NFC_REG_CTL);
  396. sunxi_nand->selected = chip;
  397. }
  398. static void sunxi_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  399. {
  400. struct nand_chip *nand = mtd_to_nand(mtd);
  401. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  402. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  403. int ret;
  404. int cnt;
  405. int offs = 0;
  406. u32 tmp;
  407. while (len > offs) {
  408. bool poll = false;
  409. cnt = min(len - offs, NFC_SRAM_SIZE);
  410. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  411. if (ret)
  412. break;
  413. writel(cnt, nfc->regs + NFC_REG_CNT);
  414. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD;
  415. writel(tmp, nfc->regs + NFC_REG_CMD);
  416. /* Arbitrary limit for polling mode */
  417. if (cnt < 64)
  418. poll = true;
  419. ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, poll, 0);
  420. if (ret)
  421. break;
  422. if (buf)
  423. memcpy_fromio(buf + offs, nfc->regs + NFC_RAM0_BASE,
  424. cnt);
  425. offs += cnt;
  426. }
  427. }
  428. static void sunxi_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  429. int len)
  430. {
  431. struct nand_chip *nand = mtd_to_nand(mtd);
  432. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  433. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  434. int ret;
  435. int cnt;
  436. int offs = 0;
  437. u32 tmp;
  438. while (len > offs) {
  439. bool poll = false;
  440. cnt = min(len - offs, NFC_SRAM_SIZE);
  441. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  442. if (ret)
  443. break;
  444. writel(cnt, nfc->regs + NFC_REG_CNT);
  445. memcpy_toio(nfc->regs + NFC_RAM0_BASE, buf + offs, cnt);
  446. tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD |
  447. NFC_ACCESS_DIR;
  448. writel(tmp, nfc->regs + NFC_REG_CMD);
  449. /* Arbitrary limit for polling mode */
  450. if (cnt < 64)
  451. poll = true;
  452. ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, poll, 0);
  453. if (ret)
  454. break;
  455. offs += cnt;
  456. }
  457. }
  458. static uint8_t sunxi_nfc_read_byte(struct mtd_info *mtd)
  459. {
  460. uint8_t ret;
  461. sunxi_nfc_read_buf(mtd, &ret, 1);
  462. return ret;
  463. }
  464. static void sunxi_nfc_cmd_ctrl(struct mtd_info *mtd, int dat,
  465. unsigned int ctrl)
  466. {
  467. struct nand_chip *nand = mtd_to_nand(mtd);
  468. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  469. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  470. int ret;
  471. if (dat == NAND_CMD_NONE && (ctrl & NAND_NCE) &&
  472. !(ctrl & (NAND_CLE | NAND_ALE))) {
  473. u32 cmd = 0;
  474. if (!sunxi_nand->addr_cycles && !sunxi_nand->cmd_cycles)
  475. return;
  476. if (sunxi_nand->cmd_cycles--)
  477. cmd |= NFC_SEND_CMD1 | sunxi_nand->cmd[0];
  478. if (sunxi_nand->cmd_cycles--) {
  479. cmd |= NFC_SEND_CMD2;
  480. writel(sunxi_nand->cmd[1],
  481. nfc->regs + NFC_REG_RCMD_SET);
  482. }
  483. sunxi_nand->cmd_cycles = 0;
  484. if (sunxi_nand->addr_cycles) {
  485. cmd |= NFC_SEND_ADR |
  486. NFC_ADR_NUM(sunxi_nand->addr_cycles);
  487. writel(sunxi_nand->addr[0],
  488. nfc->regs + NFC_REG_ADDR_LOW);
  489. }
  490. if (sunxi_nand->addr_cycles > 4)
  491. writel(sunxi_nand->addr[1],
  492. nfc->regs + NFC_REG_ADDR_HIGH);
  493. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  494. if (ret)
  495. return;
  496. writel(cmd, nfc->regs + NFC_REG_CMD);
  497. sunxi_nand->addr[0] = 0;
  498. sunxi_nand->addr[1] = 0;
  499. sunxi_nand->addr_cycles = 0;
  500. sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, true, 0);
  501. }
  502. if (ctrl & NAND_CLE) {
  503. sunxi_nand->cmd[sunxi_nand->cmd_cycles++] = dat;
  504. } else if (ctrl & NAND_ALE) {
  505. sunxi_nand->addr[sunxi_nand->addr_cycles / 4] |=
  506. dat << ((sunxi_nand->addr_cycles % 4) * 8);
  507. sunxi_nand->addr_cycles++;
  508. }
  509. }
  510. /* These seed values have been extracted from Allwinner's BSP */
  511. static const u16 sunxi_nfc_randomizer_page_seeds[] = {
  512. 0x2b75, 0x0bd0, 0x5ca3, 0x62d1, 0x1c93, 0x07e9, 0x2162, 0x3a72,
  513. 0x0d67, 0x67f9, 0x1be7, 0x077d, 0x032f, 0x0dac, 0x2716, 0x2436,
  514. 0x7922, 0x1510, 0x3860, 0x5287, 0x480f, 0x4252, 0x1789, 0x5a2d,
  515. 0x2a49, 0x5e10, 0x437f, 0x4b4e, 0x2f45, 0x216e, 0x5cb7, 0x7130,
  516. 0x2a3f, 0x60e4, 0x4dc9, 0x0ef0, 0x0f52, 0x1bb9, 0x6211, 0x7a56,
  517. 0x226d, 0x4ea7, 0x6f36, 0x3692, 0x38bf, 0x0c62, 0x05eb, 0x4c55,
  518. 0x60f4, 0x728c, 0x3b6f, 0x2037, 0x7f69, 0x0936, 0x651a, 0x4ceb,
  519. 0x6218, 0x79f3, 0x383f, 0x18d9, 0x4f05, 0x5c82, 0x2912, 0x6f17,
  520. 0x6856, 0x5938, 0x1007, 0x61ab, 0x3e7f, 0x57c2, 0x542f, 0x4f62,
  521. 0x7454, 0x2eac, 0x7739, 0x42d4, 0x2f90, 0x435a, 0x2e52, 0x2064,
  522. 0x637c, 0x66ad, 0x2c90, 0x0bad, 0x759c, 0x0029, 0x0986, 0x7126,
  523. 0x1ca7, 0x1605, 0x386a, 0x27f5, 0x1380, 0x6d75, 0x24c3, 0x0f8e,
  524. 0x2b7a, 0x1418, 0x1fd1, 0x7dc1, 0x2d8e, 0x43af, 0x2267, 0x7da3,
  525. 0x4e3d, 0x1338, 0x50db, 0x454d, 0x764d, 0x40a3, 0x42e6, 0x262b,
  526. 0x2d2e, 0x1aea, 0x2e17, 0x173d, 0x3a6e, 0x71bf, 0x25f9, 0x0a5d,
  527. 0x7c57, 0x0fbe, 0x46ce, 0x4939, 0x6b17, 0x37bb, 0x3e91, 0x76db,
  528. };
  529. /*
  530. * sunxi_nfc_randomizer_ecc512_seeds and sunxi_nfc_randomizer_ecc1024_seeds
  531. * have been generated using
  532. * sunxi_nfc_randomizer_step(seed, (step_size * 8) + 15), which is what
  533. * the randomizer engine does internally before de/scrambling OOB data.
  534. *
  535. * Those tables are statically defined to avoid calculating randomizer state
  536. * at runtime.
  537. */
  538. static const u16 sunxi_nfc_randomizer_ecc512_seeds[] = {
  539. 0x3346, 0x367f, 0x1f18, 0x769a, 0x4f64, 0x068c, 0x2ef1, 0x6b64,
  540. 0x28a9, 0x15d7, 0x30f8, 0x3659, 0x53db, 0x7c5f, 0x71d4, 0x4409,
  541. 0x26eb, 0x03cc, 0x655d, 0x47d4, 0x4daa, 0x0877, 0x712d, 0x3617,
  542. 0x3264, 0x49aa, 0x7f9e, 0x588e, 0x4fbc, 0x7176, 0x7f91, 0x6c6d,
  543. 0x4b95, 0x5fb7, 0x3844, 0x4037, 0x0184, 0x081b, 0x0ee8, 0x5b91,
  544. 0x293d, 0x1f71, 0x0e6f, 0x402b, 0x5122, 0x1e52, 0x22be, 0x3d2d,
  545. 0x75bc, 0x7c60, 0x6291, 0x1a2f, 0x61d4, 0x74aa, 0x4140, 0x29ab,
  546. 0x472d, 0x2852, 0x017e, 0x15e8, 0x5ec2, 0x17cf, 0x7d0f, 0x06b8,
  547. 0x117a, 0x6b94, 0x789b, 0x3126, 0x6ac5, 0x5be7, 0x150f, 0x51f8,
  548. 0x7889, 0x0aa5, 0x663d, 0x77e8, 0x0b87, 0x3dcb, 0x360d, 0x218b,
  549. 0x512f, 0x7dc9, 0x6a4d, 0x630a, 0x3547, 0x1dd2, 0x5aea, 0x69a5,
  550. 0x7bfa, 0x5e4f, 0x1519, 0x6430, 0x3a0e, 0x5eb3, 0x5425, 0x0c7a,
  551. 0x5540, 0x3670, 0x63c1, 0x31e9, 0x5a39, 0x2de7, 0x5979, 0x2891,
  552. 0x1562, 0x014b, 0x5b05, 0x2756, 0x5a34, 0x13aa, 0x6cb5, 0x2c36,
  553. 0x5e72, 0x1306, 0x0861, 0x15ef, 0x1ee8, 0x5a37, 0x7ac4, 0x45dd,
  554. 0x44c4, 0x7266, 0x2f41, 0x3ccc, 0x045e, 0x7d40, 0x7c66, 0x0fa0,
  555. };
  556. static const u16 sunxi_nfc_randomizer_ecc1024_seeds[] = {
  557. 0x2cf5, 0x35f1, 0x63a4, 0x5274, 0x2bd2, 0x778b, 0x7285, 0x32b6,
  558. 0x6a5c, 0x70d6, 0x757d, 0x6769, 0x5375, 0x1e81, 0x0cf3, 0x3982,
  559. 0x6787, 0x042a, 0x6c49, 0x1925, 0x56a8, 0x40a9, 0x063e, 0x7bd9,
  560. 0x4dbf, 0x55ec, 0x672e, 0x7334, 0x5185, 0x4d00, 0x232a, 0x7e07,
  561. 0x445d, 0x6b92, 0x528f, 0x4255, 0x53ba, 0x7d82, 0x2a2e, 0x3a4e,
  562. 0x75eb, 0x450c, 0x6844, 0x1b5d, 0x581a, 0x4cc6, 0x0379, 0x37b2,
  563. 0x419f, 0x0e92, 0x6b27, 0x5624, 0x01e3, 0x07c1, 0x44a5, 0x130c,
  564. 0x13e8, 0x5910, 0x0876, 0x60c5, 0x54e3, 0x5b7f, 0x2269, 0x509f,
  565. 0x7665, 0x36fd, 0x3e9a, 0x0579, 0x6295, 0x14ef, 0x0a81, 0x1bcc,
  566. 0x4b16, 0x64db, 0x0514, 0x4f07, 0x0591, 0x3576, 0x6853, 0x0d9e,
  567. 0x259f, 0x38b7, 0x64fb, 0x3094, 0x4693, 0x6ddd, 0x29bb, 0x0bc8,
  568. 0x3f47, 0x490e, 0x0c0e, 0x7933, 0x3c9e, 0x5840, 0x398d, 0x3e68,
  569. 0x4af1, 0x71f5, 0x57cf, 0x1121, 0x64eb, 0x3579, 0x15ac, 0x584d,
  570. 0x5f2a, 0x47e2, 0x6528, 0x6eac, 0x196e, 0x6b96, 0x0450, 0x0179,
  571. 0x609c, 0x06e1, 0x4626, 0x42c7, 0x273e, 0x486f, 0x0705, 0x1601,
  572. 0x145b, 0x407e, 0x062b, 0x57a5, 0x53f9, 0x5659, 0x4410, 0x3ccd,
  573. };
  574. static u16 sunxi_nfc_randomizer_step(u16 state, int count)
  575. {
  576. state &= 0x7fff;
  577. /*
  578. * This loop is just a simple implementation of a Fibonacci LFSR using
  579. * the x16 + x15 + 1 polynomial.
  580. */
  581. while (count--)
  582. state = ((state >> 1) |
  583. (((state ^ (state >> 1)) & 1) << 14)) & 0x7fff;
  584. return state;
  585. }
  586. static u16 sunxi_nfc_randomizer_state(struct mtd_info *mtd, int page, bool ecc)
  587. {
  588. const u16 *seeds = sunxi_nfc_randomizer_page_seeds;
  589. int mod = mtd_div_by_ws(mtd->erasesize, mtd);
  590. if (mod > ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds))
  591. mod = ARRAY_SIZE(sunxi_nfc_randomizer_page_seeds);
  592. if (ecc) {
  593. if (mtd->ecc_step_size == 512)
  594. seeds = sunxi_nfc_randomizer_ecc512_seeds;
  595. else
  596. seeds = sunxi_nfc_randomizer_ecc1024_seeds;
  597. }
  598. return seeds[page % mod];
  599. }
  600. static void sunxi_nfc_randomizer_config(struct mtd_info *mtd,
  601. int page, bool ecc)
  602. {
  603. struct nand_chip *nand = mtd_to_nand(mtd);
  604. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  605. u32 ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL);
  606. u16 state;
  607. if (!(nand->options & NAND_NEED_SCRAMBLING))
  608. return;
  609. ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL);
  610. state = sunxi_nfc_randomizer_state(mtd, page, ecc);
  611. ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_RANDOM_SEED_MSK;
  612. writel(ecc_ctl | NFC_RANDOM_SEED(state), nfc->regs + NFC_REG_ECC_CTL);
  613. }
  614. static void sunxi_nfc_randomizer_enable(struct mtd_info *mtd)
  615. {
  616. struct nand_chip *nand = mtd_to_nand(mtd);
  617. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  618. if (!(nand->options & NAND_NEED_SCRAMBLING))
  619. return;
  620. writel(readl(nfc->regs + NFC_REG_ECC_CTL) | NFC_RANDOM_EN,
  621. nfc->regs + NFC_REG_ECC_CTL);
  622. }
  623. static void sunxi_nfc_randomizer_disable(struct mtd_info *mtd)
  624. {
  625. struct nand_chip *nand = mtd_to_nand(mtd);
  626. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  627. if (!(nand->options & NAND_NEED_SCRAMBLING))
  628. return;
  629. writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_RANDOM_EN,
  630. nfc->regs + NFC_REG_ECC_CTL);
  631. }
  632. static void sunxi_nfc_randomize_bbm(struct mtd_info *mtd, int page, u8 *bbm)
  633. {
  634. u16 state = sunxi_nfc_randomizer_state(mtd, page, true);
  635. bbm[0] ^= state;
  636. bbm[1] ^= sunxi_nfc_randomizer_step(state, 8);
  637. }
  638. static void sunxi_nfc_randomizer_write_buf(struct mtd_info *mtd,
  639. const uint8_t *buf, int len,
  640. bool ecc, int page)
  641. {
  642. sunxi_nfc_randomizer_config(mtd, page, ecc);
  643. sunxi_nfc_randomizer_enable(mtd);
  644. sunxi_nfc_write_buf(mtd, buf, len);
  645. sunxi_nfc_randomizer_disable(mtd);
  646. }
  647. static void sunxi_nfc_randomizer_read_buf(struct mtd_info *mtd, uint8_t *buf,
  648. int len, bool ecc, int page)
  649. {
  650. sunxi_nfc_randomizer_config(mtd, page, ecc);
  651. sunxi_nfc_randomizer_enable(mtd);
  652. sunxi_nfc_read_buf(mtd, buf, len);
  653. sunxi_nfc_randomizer_disable(mtd);
  654. }
  655. static void sunxi_nfc_hw_ecc_enable(struct mtd_info *mtd)
  656. {
  657. struct nand_chip *nand = mtd_to_nand(mtd);
  658. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  659. struct sunxi_nand_hw_ecc *data = nand->ecc.priv;
  660. u32 ecc_ctl;
  661. ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL);
  662. ecc_ctl &= ~(NFC_ECC_MODE_MSK | NFC_ECC_PIPELINE |
  663. NFC_ECC_BLOCK_SIZE_MSK);
  664. ecc_ctl |= NFC_ECC_EN | NFC_ECC_MODE(data->mode) | NFC_ECC_EXCEPTION |
  665. NFC_ECC_PIPELINE;
  666. if (nand->ecc.size == 512)
  667. ecc_ctl |= NFC_ECC_BLOCK_512;
  668. writel(ecc_ctl, nfc->regs + NFC_REG_ECC_CTL);
  669. }
  670. static void sunxi_nfc_hw_ecc_disable(struct mtd_info *mtd)
  671. {
  672. struct nand_chip *nand = mtd_to_nand(mtd);
  673. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  674. writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_ECC_EN,
  675. nfc->regs + NFC_REG_ECC_CTL);
  676. }
  677. static inline void sunxi_nfc_user_data_to_buf(u32 user_data, u8 *buf)
  678. {
  679. buf[0] = user_data;
  680. buf[1] = user_data >> 8;
  681. buf[2] = user_data >> 16;
  682. buf[3] = user_data >> 24;
  683. }
  684. static inline u32 sunxi_nfc_buf_to_user_data(const u8 *buf)
  685. {
  686. return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  687. }
  688. static void sunxi_nfc_hw_ecc_get_prot_oob_bytes(struct mtd_info *mtd, u8 *oob,
  689. int step, bool bbm, int page)
  690. {
  691. struct nand_chip *nand = mtd_to_nand(mtd);
  692. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  693. sunxi_nfc_user_data_to_buf(readl(nfc->regs + NFC_REG_USER_DATA(step)),
  694. oob);
  695. /* De-randomize the Bad Block Marker. */
  696. if (bbm && (nand->options & NAND_NEED_SCRAMBLING))
  697. sunxi_nfc_randomize_bbm(mtd, page, oob);
  698. }
  699. static void sunxi_nfc_hw_ecc_set_prot_oob_bytes(struct mtd_info *mtd,
  700. const u8 *oob, int step,
  701. bool bbm, int page)
  702. {
  703. struct nand_chip *nand = mtd_to_nand(mtd);
  704. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  705. u8 user_data[4];
  706. /* Randomize the Bad Block Marker. */
  707. if (bbm && (nand->options & NAND_NEED_SCRAMBLING)) {
  708. memcpy(user_data, oob, sizeof(user_data));
  709. sunxi_nfc_randomize_bbm(mtd, page, user_data);
  710. oob = user_data;
  711. }
  712. writel(sunxi_nfc_buf_to_user_data(oob),
  713. nfc->regs + NFC_REG_USER_DATA(step));
  714. }
  715. static void sunxi_nfc_hw_ecc_update_stats(struct mtd_info *mtd,
  716. unsigned int *max_bitflips, int ret)
  717. {
  718. if (ret < 0) {
  719. mtd->ecc_stats.failed++;
  720. } else {
  721. mtd->ecc_stats.corrected += ret;
  722. *max_bitflips = max_t(unsigned int, *max_bitflips, ret);
  723. }
  724. }
  725. static int sunxi_nfc_hw_ecc_correct(struct mtd_info *mtd, u8 *data, u8 *oob,
  726. int step, u32 status, bool *erased)
  727. {
  728. struct nand_chip *nand = mtd_to_nand(mtd);
  729. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  730. struct nand_ecc_ctrl *ecc = &nand->ecc;
  731. u32 tmp;
  732. *erased = false;
  733. if (status & NFC_ECC_ERR(step))
  734. return -EBADMSG;
  735. if (status & NFC_ECC_PAT_FOUND(step)) {
  736. u8 pattern;
  737. if (unlikely(!(readl(nfc->regs + NFC_REG_PAT_ID) & 0x1))) {
  738. pattern = 0x0;
  739. } else {
  740. pattern = 0xff;
  741. *erased = true;
  742. }
  743. if (data)
  744. memset(data, pattern, ecc->size);
  745. if (oob)
  746. memset(oob, pattern, ecc->bytes + 4);
  747. return 0;
  748. }
  749. tmp = readl(nfc->regs + NFC_REG_ECC_ERR_CNT(step));
  750. return NFC_ECC_ERR_CNT(step, tmp);
  751. }
  752. static int sunxi_nfc_hw_ecc_read_chunk(struct mtd_info *mtd,
  753. u8 *data, int data_off,
  754. u8 *oob, int oob_off,
  755. int *cur_off,
  756. unsigned int *max_bitflips,
  757. bool bbm, bool oob_required, int page)
  758. {
  759. struct nand_chip *nand = mtd_to_nand(mtd);
  760. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  761. struct nand_ecc_ctrl *ecc = &nand->ecc;
  762. int raw_mode = 0;
  763. bool erased;
  764. int ret;
  765. if (*cur_off != data_off)
  766. nand_change_read_column_op(nand, data_off, NULL, 0, false);
  767. sunxi_nfc_randomizer_read_buf(mtd, NULL, ecc->size, false, page);
  768. if (data_off + ecc->size != oob_off)
  769. nand_change_read_column_op(nand, oob_off, NULL, 0, false);
  770. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  771. if (ret)
  772. return ret;
  773. sunxi_nfc_randomizer_enable(mtd);
  774. writel(NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | NFC_ECC_OP,
  775. nfc->regs + NFC_REG_CMD);
  776. ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, false, 0);
  777. sunxi_nfc_randomizer_disable(mtd);
  778. if (ret)
  779. return ret;
  780. *cur_off = oob_off + ecc->bytes + 4;
  781. ret = sunxi_nfc_hw_ecc_correct(mtd, data, oob_required ? oob : NULL, 0,
  782. readl(nfc->regs + NFC_REG_ECC_ST),
  783. &erased);
  784. if (erased)
  785. return 1;
  786. if (ret < 0) {
  787. /*
  788. * Re-read the data with the randomizer disabled to identify
  789. * bitflips in erased pages.
  790. */
  791. if (nand->options & NAND_NEED_SCRAMBLING)
  792. nand_change_read_column_op(nand, data_off, data,
  793. ecc->size, false);
  794. else
  795. memcpy_fromio(data, nfc->regs + NFC_RAM0_BASE,
  796. ecc->size);
  797. nand_change_read_column_op(nand, oob_off, oob, ecc->bytes + 4,
  798. false);
  799. ret = nand_check_erased_ecc_chunk(data, ecc->size,
  800. oob, ecc->bytes + 4,
  801. NULL, 0, ecc->strength);
  802. if (ret >= 0)
  803. raw_mode = 1;
  804. } else {
  805. memcpy_fromio(data, nfc->regs + NFC_RAM0_BASE, ecc->size);
  806. if (oob_required) {
  807. nand_change_read_column_op(nand, oob_off, NULL, 0,
  808. false);
  809. sunxi_nfc_randomizer_read_buf(mtd, oob, ecc->bytes + 4,
  810. true, page);
  811. sunxi_nfc_hw_ecc_get_prot_oob_bytes(mtd, oob, 0,
  812. bbm, page);
  813. }
  814. }
  815. sunxi_nfc_hw_ecc_update_stats(mtd, max_bitflips, ret);
  816. return raw_mode;
  817. }
  818. static void sunxi_nfc_hw_ecc_read_extra_oob(struct mtd_info *mtd,
  819. u8 *oob, int *cur_off,
  820. bool randomize, int page)
  821. {
  822. struct nand_chip *nand = mtd_to_nand(mtd);
  823. struct nand_ecc_ctrl *ecc = &nand->ecc;
  824. int offset = ((ecc->bytes + 4) * ecc->steps);
  825. int len = mtd->oobsize - offset;
  826. if (len <= 0)
  827. return;
  828. if (!cur_off || *cur_off != offset)
  829. nand_change_read_column_op(nand, mtd->writesize, NULL, 0,
  830. false);
  831. if (!randomize)
  832. sunxi_nfc_read_buf(mtd, oob + offset, len);
  833. else
  834. sunxi_nfc_randomizer_read_buf(mtd, oob + offset, len,
  835. false, page);
  836. if (cur_off)
  837. *cur_off = mtd->oobsize + mtd->writesize;
  838. }
  839. static int sunxi_nfc_hw_ecc_read_chunks_dma(struct mtd_info *mtd, uint8_t *buf,
  840. int oob_required, int page,
  841. int nchunks)
  842. {
  843. struct nand_chip *nand = mtd_to_nand(mtd);
  844. bool randomized = nand->options & NAND_NEED_SCRAMBLING;
  845. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  846. struct nand_ecc_ctrl *ecc = &nand->ecc;
  847. unsigned int max_bitflips = 0;
  848. int ret, i, raw_mode = 0;
  849. struct scatterlist sg;
  850. u32 status;
  851. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  852. if (ret)
  853. return ret;
  854. ret = sunxi_nfc_dma_op_prepare(mtd, buf, ecc->size, nchunks,
  855. DMA_FROM_DEVICE, &sg);
  856. if (ret)
  857. return ret;
  858. sunxi_nfc_hw_ecc_enable(mtd);
  859. sunxi_nfc_randomizer_config(mtd, page, false);
  860. sunxi_nfc_randomizer_enable(mtd);
  861. writel((NAND_CMD_RNDOUTSTART << 16) | (NAND_CMD_RNDOUT << 8) |
  862. NAND_CMD_READSTART, nfc->regs + NFC_REG_RCMD_SET);
  863. dma_async_issue_pending(nfc->dmac);
  864. writel(NFC_PAGE_OP | NFC_DATA_SWAP_METHOD | NFC_DATA_TRANS,
  865. nfc->regs + NFC_REG_CMD);
  866. ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, false, 0);
  867. if (ret)
  868. dmaengine_terminate_all(nfc->dmac);
  869. sunxi_nfc_randomizer_disable(mtd);
  870. sunxi_nfc_hw_ecc_disable(mtd);
  871. sunxi_nfc_dma_op_cleanup(mtd, DMA_FROM_DEVICE, &sg);
  872. if (ret)
  873. return ret;
  874. status = readl(nfc->regs + NFC_REG_ECC_ST);
  875. for (i = 0; i < nchunks; i++) {
  876. int data_off = i * ecc->size;
  877. int oob_off = i * (ecc->bytes + 4);
  878. u8 *data = buf + data_off;
  879. u8 *oob = nand->oob_poi + oob_off;
  880. bool erased;
  881. ret = sunxi_nfc_hw_ecc_correct(mtd, randomized ? data : NULL,
  882. oob_required ? oob : NULL,
  883. i, status, &erased);
  884. /* ECC errors are handled in the second loop. */
  885. if (ret < 0)
  886. continue;
  887. if (oob_required && !erased) {
  888. /* TODO: use DMA to retrieve OOB */
  889. nand_change_read_column_op(nand,
  890. mtd->writesize + oob_off,
  891. oob, ecc->bytes + 4, false);
  892. sunxi_nfc_hw_ecc_get_prot_oob_bytes(mtd, oob, i,
  893. !i, page);
  894. }
  895. if (erased)
  896. raw_mode = 1;
  897. sunxi_nfc_hw_ecc_update_stats(mtd, &max_bitflips, ret);
  898. }
  899. if (status & NFC_ECC_ERR_MSK) {
  900. for (i = 0; i < nchunks; i++) {
  901. int data_off = i * ecc->size;
  902. int oob_off = i * (ecc->bytes + 4);
  903. u8 *data = buf + data_off;
  904. u8 *oob = nand->oob_poi + oob_off;
  905. if (!(status & NFC_ECC_ERR(i)))
  906. continue;
  907. /*
  908. * Re-read the data with the randomizer disabled to
  909. * identify bitflips in erased pages.
  910. * TODO: use DMA to read page in raw mode
  911. */
  912. if (randomized)
  913. nand_change_read_column_op(nand, data_off,
  914. data, ecc->size,
  915. false);
  916. /* TODO: use DMA to retrieve OOB */
  917. nand_change_read_column_op(nand,
  918. mtd->writesize + oob_off,
  919. oob, ecc->bytes + 4, false);
  920. ret = nand_check_erased_ecc_chunk(data, ecc->size,
  921. oob, ecc->bytes + 4,
  922. NULL, 0,
  923. ecc->strength);
  924. if (ret >= 0)
  925. raw_mode = 1;
  926. sunxi_nfc_hw_ecc_update_stats(mtd, &max_bitflips, ret);
  927. }
  928. }
  929. if (oob_required)
  930. sunxi_nfc_hw_ecc_read_extra_oob(mtd, nand->oob_poi,
  931. NULL, !raw_mode,
  932. page);
  933. return max_bitflips;
  934. }
  935. static int sunxi_nfc_hw_ecc_write_chunk(struct mtd_info *mtd,
  936. const u8 *data, int data_off,
  937. const u8 *oob, int oob_off,
  938. int *cur_off, bool bbm,
  939. int page)
  940. {
  941. struct nand_chip *nand = mtd_to_nand(mtd);
  942. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  943. struct nand_ecc_ctrl *ecc = &nand->ecc;
  944. int ret;
  945. if (data_off != *cur_off)
  946. nand_change_write_column_op(nand, data_off, NULL, 0, false);
  947. sunxi_nfc_randomizer_write_buf(mtd, data, ecc->size, false, page);
  948. if (data_off + ecc->size != oob_off)
  949. nand_change_write_column_op(nand, oob_off, NULL, 0, false);
  950. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  951. if (ret)
  952. return ret;
  953. sunxi_nfc_randomizer_enable(mtd);
  954. sunxi_nfc_hw_ecc_set_prot_oob_bytes(mtd, oob, 0, bbm, page);
  955. writel(NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD |
  956. NFC_ACCESS_DIR | NFC_ECC_OP,
  957. nfc->regs + NFC_REG_CMD);
  958. ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, false, 0);
  959. sunxi_nfc_randomizer_disable(mtd);
  960. if (ret)
  961. return ret;
  962. *cur_off = oob_off + ecc->bytes + 4;
  963. return 0;
  964. }
  965. static void sunxi_nfc_hw_ecc_write_extra_oob(struct mtd_info *mtd,
  966. u8 *oob, int *cur_off,
  967. int page)
  968. {
  969. struct nand_chip *nand = mtd_to_nand(mtd);
  970. struct nand_ecc_ctrl *ecc = &nand->ecc;
  971. int offset = ((ecc->bytes + 4) * ecc->steps);
  972. int len = mtd->oobsize - offset;
  973. if (len <= 0)
  974. return;
  975. if (!cur_off || *cur_off != offset)
  976. nand_change_write_column_op(nand, offset + mtd->writesize,
  977. NULL, 0, false);
  978. sunxi_nfc_randomizer_write_buf(mtd, oob + offset, len, false, page);
  979. if (cur_off)
  980. *cur_off = mtd->oobsize + mtd->writesize;
  981. }
  982. static int sunxi_nfc_hw_ecc_read_page(struct mtd_info *mtd,
  983. struct nand_chip *chip, uint8_t *buf,
  984. int oob_required, int page)
  985. {
  986. struct nand_ecc_ctrl *ecc = &chip->ecc;
  987. unsigned int max_bitflips = 0;
  988. int ret, i, cur_off = 0;
  989. bool raw_mode = false;
  990. nand_read_page_op(chip, page, 0, NULL, 0);
  991. sunxi_nfc_hw_ecc_enable(mtd);
  992. for (i = 0; i < ecc->steps; i++) {
  993. int data_off = i * ecc->size;
  994. int oob_off = i * (ecc->bytes + 4);
  995. u8 *data = buf + data_off;
  996. u8 *oob = chip->oob_poi + oob_off;
  997. ret = sunxi_nfc_hw_ecc_read_chunk(mtd, data, data_off, oob,
  998. oob_off + mtd->writesize,
  999. &cur_off, &max_bitflips,
  1000. !i, oob_required, page);
  1001. if (ret < 0)
  1002. return ret;
  1003. else if (ret)
  1004. raw_mode = true;
  1005. }
  1006. if (oob_required)
  1007. sunxi_nfc_hw_ecc_read_extra_oob(mtd, chip->oob_poi, &cur_off,
  1008. !raw_mode, page);
  1009. sunxi_nfc_hw_ecc_disable(mtd);
  1010. return max_bitflips;
  1011. }
  1012. static int sunxi_nfc_hw_ecc_read_page_dma(struct mtd_info *mtd,
  1013. struct nand_chip *chip, u8 *buf,
  1014. int oob_required, int page)
  1015. {
  1016. int ret;
  1017. nand_read_page_op(chip, page, 0, NULL, 0);
  1018. ret = sunxi_nfc_hw_ecc_read_chunks_dma(mtd, buf, oob_required, page,
  1019. chip->ecc.steps);
  1020. if (ret >= 0)
  1021. return ret;
  1022. /* Fallback to PIO mode */
  1023. return sunxi_nfc_hw_ecc_read_page(mtd, chip, buf, oob_required, page);
  1024. }
  1025. static int sunxi_nfc_hw_ecc_read_subpage(struct mtd_info *mtd,
  1026. struct nand_chip *chip,
  1027. u32 data_offs, u32 readlen,
  1028. u8 *bufpoi, int page)
  1029. {
  1030. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1031. int ret, i, cur_off = 0;
  1032. unsigned int max_bitflips = 0;
  1033. nand_read_page_op(chip, page, 0, NULL, 0);
  1034. sunxi_nfc_hw_ecc_enable(mtd);
  1035. for (i = data_offs / ecc->size;
  1036. i < DIV_ROUND_UP(data_offs + readlen, ecc->size); i++) {
  1037. int data_off = i * ecc->size;
  1038. int oob_off = i * (ecc->bytes + 4);
  1039. u8 *data = bufpoi + data_off;
  1040. u8 *oob = chip->oob_poi + oob_off;
  1041. ret = sunxi_nfc_hw_ecc_read_chunk(mtd, data, data_off,
  1042. oob,
  1043. oob_off + mtd->writesize,
  1044. &cur_off, &max_bitflips, !i,
  1045. false, page);
  1046. if (ret < 0)
  1047. return ret;
  1048. }
  1049. sunxi_nfc_hw_ecc_disable(mtd);
  1050. return max_bitflips;
  1051. }
  1052. static int sunxi_nfc_hw_ecc_read_subpage_dma(struct mtd_info *mtd,
  1053. struct nand_chip *chip,
  1054. u32 data_offs, u32 readlen,
  1055. u8 *buf, int page)
  1056. {
  1057. int nchunks = DIV_ROUND_UP(data_offs + readlen, chip->ecc.size);
  1058. int ret;
  1059. nand_read_page_op(chip, page, 0, NULL, 0);
  1060. ret = sunxi_nfc_hw_ecc_read_chunks_dma(mtd, buf, false, page, nchunks);
  1061. if (ret >= 0)
  1062. return ret;
  1063. /* Fallback to PIO mode */
  1064. return sunxi_nfc_hw_ecc_read_subpage(mtd, chip, data_offs, readlen,
  1065. buf, page);
  1066. }
  1067. static int sunxi_nfc_hw_ecc_write_page(struct mtd_info *mtd,
  1068. struct nand_chip *chip,
  1069. const uint8_t *buf, int oob_required,
  1070. int page)
  1071. {
  1072. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1073. int ret, i, cur_off = 0;
  1074. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  1075. sunxi_nfc_hw_ecc_enable(mtd);
  1076. for (i = 0; i < ecc->steps; i++) {
  1077. int data_off = i * ecc->size;
  1078. int oob_off = i * (ecc->bytes + 4);
  1079. const u8 *data = buf + data_off;
  1080. const u8 *oob = chip->oob_poi + oob_off;
  1081. ret = sunxi_nfc_hw_ecc_write_chunk(mtd, data, data_off, oob,
  1082. oob_off + mtd->writesize,
  1083. &cur_off, !i, page);
  1084. if (ret)
  1085. return ret;
  1086. }
  1087. if (oob_required || (chip->options & NAND_NEED_SCRAMBLING))
  1088. sunxi_nfc_hw_ecc_write_extra_oob(mtd, chip->oob_poi,
  1089. &cur_off, page);
  1090. sunxi_nfc_hw_ecc_disable(mtd);
  1091. return nand_prog_page_end_op(chip);
  1092. }
  1093. static int sunxi_nfc_hw_ecc_write_subpage(struct mtd_info *mtd,
  1094. struct nand_chip *chip,
  1095. u32 data_offs, u32 data_len,
  1096. const u8 *buf, int oob_required,
  1097. int page)
  1098. {
  1099. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1100. int ret, i, cur_off = 0;
  1101. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  1102. sunxi_nfc_hw_ecc_enable(mtd);
  1103. for (i = data_offs / ecc->size;
  1104. i < DIV_ROUND_UP(data_offs + data_len, ecc->size); i++) {
  1105. int data_off = i * ecc->size;
  1106. int oob_off = i * (ecc->bytes + 4);
  1107. const u8 *data = buf + data_off;
  1108. const u8 *oob = chip->oob_poi + oob_off;
  1109. ret = sunxi_nfc_hw_ecc_write_chunk(mtd, data, data_off, oob,
  1110. oob_off + mtd->writesize,
  1111. &cur_off, !i, page);
  1112. if (ret)
  1113. return ret;
  1114. }
  1115. sunxi_nfc_hw_ecc_disable(mtd);
  1116. return nand_prog_page_end_op(chip);
  1117. }
  1118. static int sunxi_nfc_hw_ecc_write_page_dma(struct mtd_info *mtd,
  1119. struct nand_chip *chip,
  1120. const u8 *buf,
  1121. int oob_required,
  1122. int page)
  1123. {
  1124. struct nand_chip *nand = mtd_to_nand(mtd);
  1125. struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
  1126. struct nand_ecc_ctrl *ecc = &nand->ecc;
  1127. struct scatterlist sg;
  1128. int ret, i;
  1129. ret = sunxi_nfc_wait_cmd_fifo_empty(nfc);
  1130. if (ret)
  1131. return ret;
  1132. ret = sunxi_nfc_dma_op_prepare(mtd, buf, ecc->size, ecc->steps,
  1133. DMA_TO_DEVICE, &sg);
  1134. if (ret)
  1135. goto pio_fallback;
  1136. for (i = 0; i < ecc->steps; i++) {
  1137. const u8 *oob = nand->oob_poi + (i * (ecc->bytes + 4));
  1138. sunxi_nfc_hw_ecc_set_prot_oob_bytes(mtd, oob, i, !i, page);
  1139. }
  1140. nand_prog_page_begin_op(chip, page, 0, NULL, 0);
  1141. sunxi_nfc_hw_ecc_enable(mtd);
  1142. sunxi_nfc_randomizer_config(mtd, page, false);
  1143. sunxi_nfc_randomizer_enable(mtd);
  1144. writel((NAND_CMD_RNDIN << 8) | NAND_CMD_PAGEPROG,
  1145. nfc->regs + NFC_REG_RCMD_SET);
  1146. dma_async_issue_pending(nfc->dmac);
  1147. writel(NFC_PAGE_OP | NFC_DATA_SWAP_METHOD |
  1148. NFC_DATA_TRANS | NFC_ACCESS_DIR,
  1149. nfc->regs + NFC_REG_CMD);
  1150. ret = sunxi_nfc_wait_events(nfc, NFC_CMD_INT_FLAG, false, 0);
  1151. if (ret)
  1152. dmaengine_terminate_all(nfc->dmac);
  1153. sunxi_nfc_randomizer_disable(mtd);
  1154. sunxi_nfc_hw_ecc_disable(mtd);
  1155. sunxi_nfc_dma_op_cleanup(mtd, DMA_TO_DEVICE, &sg);
  1156. if (ret)
  1157. return ret;
  1158. if (oob_required || (chip->options & NAND_NEED_SCRAMBLING))
  1159. /* TODO: use DMA to transfer extra OOB bytes ? */
  1160. sunxi_nfc_hw_ecc_write_extra_oob(mtd, chip->oob_poi,
  1161. NULL, page);
  1162. return nand_prog_page_end_op(chip);
  1163. pio_fallback:
  1164. return sunxi_nfc_hw_ecc_write_page(mtd, chip, buf, oob_required, page);
  1165. }
  1166. static int sunxi_nfc_hw_ecc_read_oob(struct mtd_info *mtd,
  1167. struct nand_chip *chip,
  1168. int page)
  1169. {
  1170. chip->pagebuf = -1;
  1171. return chip->ecc.read_page(mtd, chip, chip->data_buf, 1, page);
  1172. }
  1173. static int sunxi_nfc_hw_ecc_write_oob(struct mtd_info *mtd,
  1174. struct nand_chip *chip,
  1175. int page)
  1176. {
  1177. int ret;
  1178. chip->pagebuf = -1;
  1179. memset(chip->data_buf, 0xff, mtd->writesize);
  1180. ret = chip->ecc.write_page(mtd, chip, chip->data_buf, 1, page);
  1181. if (ret)
  1182. return ret;
  1183. /* Send command to program the OOB data */
  1184. return nand_prog_page_end_op(chip);
  1185. }
  1186. static const s32 tWB_lut[] = {6, 12, 16, 20};
  1187. static const s32 tRHW_lut[] = {4, 8, 12, 20};
  1188. static int _sunxi_nand_lookup_timing(const s32 *lut, int lut_size, u32 duration,
  1189. u32 clk_period)
  1190. {
  1191. u32 clk_cycles = DIV_ROUND_UP(duration, clk_period);
  1192. int i;
  1193. for (i = 0; i < lut_size; i++) {
  1194. if (clk_cycles <= lut[i])
  1195. return i;
  1196. }
  1197. /* Doesn't fit */
  1198. return -EINVAL;
  1199. }
  1200. #define sunxi_nand_lookup_timing(l, p, c) \
  1201. _sunxi_nand_lookup_timing(l, ARRAY_SIZE(l), p, c)
  1202. static int sunxi_nfc_setup_data_interface(struct mtd_info *mtd, int csline,
  1203. const struct nand_data_interface *conf)
  1204. {
  1205. struct nand_chip *nand = mtd_to_nand(mtd);
  1206. struct sunxi_nand_chip *chip = to_sunxi_nand(nand);
  1207. struct sunxi_nfc *nfc = to_sunxi_nfc(chip->nand.controller);
  1208. const struct nand_sdr_timings *timings;
  1209. u32 min_clk_period = 0;
  1210. s32 tWB, tADL, tWHR, tRHW, tCAD;
  1211. long real_clk_rate;
  1212. timings = nand_get_sdr_timings(conf);
  1213. if (IS_ERR(timings))
  1214. return -ENOTSUPP;
  1215. /* T1 <=> tCLS */
  1216. if (timings->tCLS_min > min_clk_period)
  1217. min_clk_period = timings->tCLS_min;
  1218. /* T2 <=> tCLH */
  1219. if (timings->tCLH_min > min_clk_period)
  1220. min_clk_period = timings->tCLH_min;
  1221. /* T3 <=> tCS */
  1222. if (timings->tCS_min > min_clk_period)
  1223. min_clk_period = timings->tCS_min;
  1224. /* T4 <=> tCH */
  1225. if (timings->tCH_min > min_clk_period)
  1226. min_clk_period = timings->tCH_min;
  1227. /* T5 <=> tWP */
  1228. if (timings->tWP_min > min_clk_period)
  1229. min_clk_period = timings->tWP_min;
  1230. /* T6 <=> tWH */
  1231. if (timings->tWH_min > min_clk_period)
  1232. min_clk_period = timings->tWH_min;
  1233. /* T7 <=> tALS */
  1234. if (timings->tALS_min > min_clk_period)
  1235. min_clk_period = timings->tALS_min;
  1236. /* T8 <=> tDS */
  1237. if (timings->tDS_min > min_clk_period)
  1238. min_clk_period = timings->tDS_min;
  1239. /* T9 <=> tDH */
  1240. if (timings->tDH_min > min_clk_period)
  1241. min_clk_period = timings->tDH_min;
  1242. /* T10 <=> tRR */
  1243. if (timings->tRR_min > (min_clk_period * 3))
  1244. min_clk_period = DIV_ROUND_UP(timings->tRR_min, 3);
  1245. /* T11 <=> tALH */
  1246. if (timings->tALH_min > min_clk_period)
  1247. min_clk_period = timings->tALH_min;
  1248. /* T12 <=> tRP */
  1249. if (timings->tRP_min > min_clk_period)
  1250. min_clk_period = timings->tRP_min;
  1251. /* T13 <=> tREH */
  1252. if (timings->tREH_min > min_clk_period)
  1253. min_clk_period = timings->tREH_min;
  1254. /* T14 <=> tRC */
  1255. if (timings->tRC_min > (min_clk_period * 2))
  1256. min_clk_period = DIV_ROUND_UP(timings->tRC_min, 2);
  1257. /* T15 <=> tWC */
  1258. if (timings->tWC_min > (min_clk_period * 2))
  1259. min_clk_period = DIV_ROUND_UP(timings->tWC_min, 2);
  1260. /* T16 - T19 + tCAD */
  1261. if (timings->tWB_max > (min_clk_period * 20))
  1262. min_clk_period = DIV_ROUND_UP(timings->tWB_max, 20);
  1263. if (timings->tADL_min > (min_clk_period * 32))
  1264. min_clk_period = DIV_ROUND_UP(timings->tADL_min, 32);
  1265. if (timings->tWHR_min > (min_clk_period * 32))
  1266. min_clk_period = DIV_ROUND_UP(timings->tWHR_min, 32);
  1267. if (timings->tRHW_min > (min_clk_period * 20))
  1268. min_clk_period = DIV_ROUND_UP(timings->tRHW_min, 20);
  1269. tWB = sunxi_nand_lookup_timing(tWB_lut, timings->tWB_max,
  1270. min_clk_period);
  1271. if (tWB < 0) {
  1272. dev_err(nfc->dev, "unsupported tWB\n");
  1273. return tWB;
  1274. }
  1275. tADL = DIV_ROUND_UP(timings->tADL_min, min_clk_period) >> 3;
  1276. if (tADL > 3) {
  1277. dev_err(nfc->dev, "unsupported tADL\n");
  1278. return -EINVAL;
  1279. }
  1280. tWHR = DIV_ROUND_UP(timings->tWHR_min, min_clk_period) >> 3;
  1281. if (tWHR > 3) {
  1282. dev_err(nfc->dev, "unsupported tWHR\n");
  1283. return -EINVAL;
  1284. }
  1285. tRHW = sunxi_nand_lookup_timing(tRHW_lut, timings->tRHW_min,
  1286. min_clk_period);
  1287. if (tRHW < 0) {
  1288. dev_err(nfc->dev, "unsupported tRHW\n");
  1289. return tRHW;
  1290. }
  1291. if (csline == NAND_DATA_IFACE_CHECK_ONLY)
  1292. return 0;
  1293. /*
  1294. * TODO: according to ONFI specs this value only applies for DDR NAND,
  1295. * but Allwinner seems to set this to 0x7. Mimic them for now.
  1296. */
  1297. tCAD = 0x7;
  1298. /* TODO: A83 has some more bits for CDQSS, CS, CLHZ, CCS, WC */
  1299. chip->timing_cfg = NFC_TIMING_CFG(tWB, tADL, tWHR, tRHW, tCAD);
  1300. /* Convert min_clk_period from picoseconds to nanoseconds */
  1301. min_clk_period = DIV_ROUND_UP(min_clk_period, 1000);
  1302. /*
  1303. * Unlike what is stated in Allwinner datasheet, the clk_rate should
  1304. * be set to (1 / min_clk_period), and not (2 / min_clk_period).
  1305. * This new formula was verified with a scope and validated by
  1306. * Allwinner engineers.
  1307. */
  1308. chip->clk_rate = NSEC_PER_SEC / min_clk_period;
  1309. real_clk_rate = clk_round_rate(nfc->mod_clk, chip->clk_rate);
  1310. if (real_clk_rate <= 0) {
  1311. dev_err(nfc->dev, "Unable to round clk %lu\n", chip->clk_rate);
  1312. return -EINVAL;
  1313. }
  1314. /*
  1315. * ONFI specification 3.1, paragraph 4.15.2 dictates that EDO data
  1316. * output cycle timings shall be used if the host drives tRC less than
  1317. * 30 ns.
  1318. */
  1319. min_clk_period = NSEC_PER_SEC / real_clk_rate;
  1320. chip->timing_ctl = ((min_clk_period * 2) < 30) ?
  1321. NFC_TIMING_CTL_EDO : 0;
  1322. return 0;
  1323. }
  1324. static int sunxi_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
  1325. struct mtd_oob_region *oobregion)
  1326. {
  1327. struct nand_chip *nand = mtd_to_nand(mtd);
  1328. struct nand_ecc_ctrl *ecc = &nand->ecc;
  1329. if (section >= ecc->steps)
  1330. return -ERANGE;
  1331. oobregion->offset = section * (ecc->bytes + 4) + 4;
  1332. oobregion->length = ecc->bytes;
  1333. return 0;
  1334. }
  1335. static int sunxi_nand_ooblayout_free(struct mtd_info *mtd, int section,
  1336. struct mtd_oob_region *oobregion)
  1337. {
  1338. struct nand_chip *nand = mtd_to_nand(mtd);
  1339. struct nand_ecc_ctrl *ecc = &nand->ecc;
  1340. if (section > ecc->steps)
  1341. return -ERANGE;
  1342. /*
  1343. * The first 2 bytes are used for BB markers, hence we
  1344. * only have 2 bytes available in the first user data
  1345. * section.
  1346. */
  1347. if (!section && ecc->mode == NAND_ECC_HW) {
  1348. oobregion->offset = 2;
  1349. oobregion->length = 2;
  1350. return 0;
  1351. }
  1352. oobregion->offset = section * (ecc->bytes + 4);
  1353. if (section < ecc->steps)
  1354. oobregion->length = 4;
  1355. else
  1356. oobregion->offset = mtd->oobsize - oobregion->offset;
  1357. return 0;
  1358. }
  1359. static const struct mtd_ooblayout_ops sunxi_nand_ooblayout_ops = {
  1360. .ecc = sunxi_nand_ooblayout_ecc,
  1361. .free = sunxi_nand_ooblayout_free,
  1362. };
  1363. static void sunxi_nand_hw_ecc_ctrl_cleanup(struct nand_ecc_ctrl *ecc)
  1364. {
  1365. kfree(ecc->priv);
  1366. }
  1367. static int sunxi_nand_hw_ecc_ctrl_init(struct mtd_info *mtd,
  1368. struct nand_ecc_ctrl *ecc,
  1369. struct device_node *np)
  1370. {
  1371. static const u8 strengths[] = { 16, 24, 28, 32, 40, 48, 56, 60, 64 };
  1372. struct nand_chip *nand = mtd_to_nand(mtd);
  1373. struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
  1374. struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
  1375. struct sunxi_nand_hw_ecc *data;
  1376. int nsectors;
  1377. int ret;
  1378. int i;
  1379. if (ecc->options & NAND_ECC_MAXIMIZE) {
  1380. int bytes;
  1381. ecc->size = 1024;
  1382. nsectors = mtd->writesize / ecc->size;
  1383. /* Reserve 2 bytes for the BBM */
  1384. bytes = (mtd->oobsize - 2) / nsectors;
  1385. /* 4 non-ECC bytes are added before each ECC bytes section */
  1386. bytes -= 4;
  1387. /* and bytes has to be even. */
  1388. if (bytes % 2)
  1389. bytes--;
  1390. ecc->strength = bytes * 8 / fls(8 * ecc->size);
  1391. for (i = 0; i < ARRAY_SIZE(strengths); i++) {
  1392. if (strengths[i] > ecc->strength)
  1393. break;
  1394. }
  1395. if (!i)
  1396. ecc->strength = 0;
  1397. else
  1398. ecc->strength = strengths[i - 1];
  1399. }
  1400. if (ecc->size != 512 && ecc->size != 1024)
  1401. return -EINVAL;
  1402. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1403. if (!data)
  1404. return -ENOMEM;
  1405. /* Prefer 1k ECC chunk over 512 ones */
  1406. if (ecc->size == 512 && mtd->writesize > 512) {
  1407. ecc->size = 1024;
  1408. ecc->strength *= 2;
  1409. }
  1410. /* Add ECC info retrieval from DT */
  1411. for (i = 0; i < ARRAY_SIZE(strengths); i++) {
  1412. if (ecc->strength <= strengths[i]) {
  1413. /*
  1414. * Update ecc->strength value with the actual strength
  1415. * that will be used by the ECC engine.
  1416. */
  1417. ecc->strength = strengths[i];
  1418. break;
  1419. }
  1420. }
  1421. if (i >= ARRAY_SIZE(strengths)) {
  1422. dev_err(nfc->dev, "unsupported strength\n");
  1423. ret = -ENOTSUPP;
  1424. goto err;
  1425. }
  1426. data->mode = i;
  1427. /* HW ECC always request ECC bytes for 1024 bytes blocks */
  1428. ecc->bytes = DIV_ROUND_UP(ecc->strength * fls(8 * 1024), 8);
  1429. /* HW ECC always work with even numbers of ECC bytes */
  1430. ecc->bytes = ALIGN(ecc->bytes, 2);
  1431. nsectors = mtd->writesize / ecc->size;
  1432. if (mtd->oobsize < ((ecc->bytes + 4) * nsectors)) {
  1433. ret = -EINVAL;
  1434. goto err;
  1435. }
  1436. ecc->read_oob = sunxi_nfc_hw_ecc_read_oob;
  1437. ecc->write_oob = sunxi_nfc_hw_ecc_write_oob;
  1438. mtd_set_ooblayout(mtd, &sunxi_nand_ooblayout_ops);
  1439. ecc->priv = data;
  1440. if (nfc->dmac) {
  1441. ecc->read_page = sunxi_nfc_hw_ecc_read_page_dma;
  1442. ecc->read_subpage = sunxi_nfc_hw_ecc_read_subpage_dma;
  1443. ecc->write_page = sunxi_nfc_hw_ecc_write_page_dma;
  1444. nand->options |= NAND_USE_BOUNCE_BUFFER;
  1445. } else {
  1446. ecc->read_page = sunxi_nfc_hw_ecc_read_page;
  1447. ecc->read_subpage = sunxi_nfc_hw_ecc_read_subpage;
  1448. ecc->write_page = sunxi_nfc_hw_ecc_write_page;
  1449. }
  1450. /* TODO: support DMA for raw accesses and subpage write */
  1451. ecc->write_subpage = sunxi_nfc_hw_ecc_write_subpage;
  1452. ecc->read_oob_raw = nand_read_oob_std;
  1453. ecc->write_oob_raw = nand_write_oob_std;
  1454. return 0;
  1455. err:
  1456. kfree(data);
  1457. return ret;
  1458. }
  1459. static void sunxi_nand_ecc_cleanup(struct nand_ecc_ctrl *ecc)
  1460. {
  1461. switch (ecc->mode) {
  1462. case NAND_ECC_HW:
  1463. sunxi_nand_hw_ecc_ctrl_cleanup(ecc);
  1464. break;
  1465. case NAND_ECC_NONE:
  1466. default:
  1467. break;
  1468. }
  1469. }
  1470. static int sunxi_nand_ecc_init(struct mtd_info *mtd, struct nand_ecc_ctrl *ecc,
  1471. struct device_node *np)
  1472. {
  1473. struct nand_chip *nand = mtd_to_nand(mtd);
  1474. int ret;
  1475. if (!ecc->size) {
  1476. ecc->size = nand->ecc_step_ds;
  1477. ecc->strength = nand->ecc_strength_ds;
  1478. }
  1479. if (!ecc->size || !ecc->strength)
  1480. return -EINVAL;
  1481. switch (ecc->mode) {
  1482. case NAND_ECC_HW:
  1483. ret = sunxi_nand_hw_ecc_ctrl_init(mtd, ecc, np);
  1484. if (ret)
  1485. return ret;
  1486. break;
  1487. case NAND_ECC_NONE:
  1488. case NAND_ECC_SOFT:
  1489. break;
  1490. default:
  1491. return -EINVAL;
  1492. }
  1493. return 0;
  1494. }
  1495. static int sunxi_nand_chip_init(struct device *dev, struct sunxi_nfc *nfc,
  1496. struct device_node *np)
  1497. {
  1498. struct sunxi_nand_chip *chip;
  1499. struct mtd_info *mtd;
  1500. struct nand_chip *nand;
  1501. int nsels;
  1502. int ret;
  1503. int i;
  1504. u32 tmp;
  1505. if (!of_get_property(np, "reg", &nsels))
  1506. return -EINVAL;
  1507. nsels /= sizeof(u32);
  1508. if (!nsels) {
  1509. dev_err(dev, "invalid reg property size\n");
  1510. return -EINVAL;
  1511. }
  1512. chip = devm_kzalloc(dev,
  1513. sizeof(*chip) +
  1514. (nsels * sizeof(struct sunxi_nand_chip_sel)),
  1515. GFP_KERNEL);
  1516. if (!chip) {
  1517. dev_err(dev, "could not allocate chip\n");
  1518. return -ENOMEM;
  1519. }
  1520. chip->nsels = nsels;
  1521. chip->selected = -1;
  1522. for (i = 0; i < nsels; i++) {
  1523. ret = of_property_read_u32_index(np, "reg", i, &tmp);
  1524. if (ret) {
  1525. dev_err(dev, "could not retrieve reg property: %d\n",
  1526. ret);
  1527. return ret;
  1528. }
  1529. if (tmp > NFC_MAX_CS) {
  1530. dev_err(dev,
  1531. "invalid reg value: %u (max CS = 7)\n",
  1532. tmp);
  1533. return -EINVAL;
  1534. }
  1535. if (test_and_set_bit(tmp, &nfc->assigned_cs)) {
  1536. dev_err(dev, "CS %d already assigned\n", tmp);
  1537. return -EINVAL;
  1538. }
  1539. chip->sels[i].cs = tmp;
  1540. if (!of_property_read_u32_index(np, "allwinner,rb", i, &tmp) &&
  1541. tmp < 2)
  1542. chip->sels[i].rb = tmp;
  1543. else
  1544. chip->sels[i].rb = -1;
  1545. }
  1546. nand = &chip->nand;
  1547. /* Default tR value specified in the ONFI spec (chapter 4.15.1) */
  1548. nand->chip_delay = 200;
  1549. nand->controller = &nfc->controller;
  1550. /*
  1551. * Set the ECC mode to the default value in case nothing is specified
  1552. * in the DT.
  1553. */
  1554. nand->ecc.mode = NAND_ECC_HW;
  1555. nand_set_flash_node(nand, np);
  1556. nand->select_chip = sunxi_nfc_select_chip;
  1557. nand->cmd_ctrl = sunxi_nfc_cmd_ctrl;
  1558. nand->read_buf = sunxi_nfc_read_buf;
  1559. nand->write_buf = sunxi_nfc_write_buf;
  1560. nand->read_byte = sunxi_nfc_read_byte;
  1561. nand->setup_data_interface = sunxi_nfc_setup_data_interface;
  1562. mtd = nand_to_mtd(nand);
  1563. mtd->dev.parent = dev;
  1564. ret = nand_scan_ident(mtd, nsels, NULL);
  1565. if (ret)
  1566. return ret;
  1567. if (nand->bbt_options & NAND_BBT_USE_FLASH)
  1568. nand->bbt_options |= NAND_BBT_NO_OOB;
  1569. if (nand->options & NAND_NEED_SCRAMBLING)
  1570. nand->options |= NAND_NO_SUBPAGE_WRITE;
  1571. nand->options |= NAND_SUBPAGE_READ;
  1572. ret = sunxi_nand_ecc_init(mtd, &nand->ecc, np);
  1573. if (ret) {
  1574. dev_err(dev, "ECC init failed: %d\n", ret);
  1575. return ret;
  1576. }
  1577. ret = nand_scan_tail(mtd);
  1578. if (ret) {
  1579. dev_err(dev, "nand_scan_tail failed: %d\n", ret);
  1580. return ret;
  1581. }
  1582. ret = mtd_device_register(mtd, NULL, 0);
  1583. if (ret) {
  1584. dev_err(dev, "failed to register mtd device: %d\n", ret);
  1585. nand_release(mtd);
  1586. return ret;
  1587. }
  1588. list_add_tail(&chip->node, &nfc->chips);
  1589. return 0;
  1590. }
  1591. static int sunxi_nand_chips_init(struct device *dev, struct sunxi_nfc *nfc)
  1592. {
  1593. struct device_node *np = dev->of_node;
  1594. struct device_node *nand_np;
  1595. int nchips = of_get_child_count(np);
  1596. int ret;
  1597. if (nchips > 8) {
  1598. dev_err(dev, "too many NAND chips: %d (max = 8)\n", nchips);
  1599. return -EINVAL;
  1600. }
  1601. for_each_child_of_node(np, nand_np) {
  1602. ret = sunxi_nand_chip_init(dev, nfc, nand_np);
  1603. if (ret) {
  1604. of_node_put(nand_np);
  1605. return ret;
  1606. }
  1607. }
  1608. return 0;
  1609. }
  1610. static void sunxi_nand_chips_cleanup(struct sunxi_nfc *nfc)
  1611. {
  1612. struct sunxi_nand_chip *chip;
  1613. while (!list_empty(&nfc->chips)) {
  1614. chip = list_first_entry(&nfc->chips, struct sunxi_nand_chip,
  1615. node);
  1616. nand_release(nand_to_mtd(&chip->nand));
  1617. sunxi_nand_ecc_cleanup(&chip->nand.ecc);
  1618. list_del(&chip->node);
  1619. }
  1620. }
  1621. static int sunxi_nfc_probe(struct platform_device *pdev)
  1622. {
  1623. struct device *dev = &pdev->dev;
  1624. struct resource *r;
  1625. struct sunxi_nfc *nfc;
  1626. int irq;
  1627. int ret;
  1628. nfc = devm_kzalloc(dev, sizeof(*nfc), GFP_KERNEL);
  1629. if (!nfc)
  1630. return -ENOMEM;
  1631. nfc->dev = dev;
  1632. nand_hw_control_init(&nfc->controller);
  1633. INIT_LIST_HEAD(&nfc->chips);
  1634. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1635. nfc->regs = devm_ioremap_resource(dev, r);
  1636. if (IS_ERR(nfc->regs))
  1637. return PTR_ERR(nfc->regs);
  1638. irq = platform_get_irq(pdev, 0);
  1639. if (irq < 0) {
  1640. dev_err(dev, "failed to retrieve irq\n");
  1641. return irq;
  1642. }
  1643. nfc->ahb_clk = devm_clk_get(dev, "ahb");
  1644. if (IS_ERR(nfc->ahb_clk)) {
  1645. dev_err(dev, "failed to retrieve ahb clk\n");
  1646. return PTR_ERR(nfc->ahb_clk);
  1647. }
  1648. ret = clk_prepare_enable(nfc->ahb_clk);
  1649. if (ret)
  1650. return ret;
  1651. nfc->mod_clk = devm_clk_get(dev, "mod");
  1652. if (IS_ERR(nfc->mod_clk)) {
  1653. dev_err(dev, "failed to retrieve mod clk\n");
  1654. ret = PTR_ERR(nfc->mod_clk);
  1655. goto out_ahb_clk_unprepare;
  1656. }
  1657. ret = clk_prepare_enable(nfc->mod_clk);
  1658. if (ret)
  1659. goto out_ahb_clk_unprepare;
  1660. nfc->reset = devm_reset_control_get_optional_exclusive(dev, "ahb");
  1661. if (IS_ERR(nfc->reset)) {
  1662. ret = PTR_ERR(nfc->reset);
  1663. goto out_mod_clk_unprepare;
  1664. }
  1665. ret = reset_control_deassert(nfc->reset);
  1666. if (ret) {
  1667. dev_err(dev, "reset err %d\n", ret);
  1668. goto out_mod_clk_unprepare;
  1669. }
  1670. ret = sunxi_nfc_rst(nfc);
  1671. if (ret)
  1672. goto out_ahb_reset_reassert;
  1673. writel(0, nfc->regs + NFC_REG_INT);
  1674. ret = devm_request_irq(dev, irq, sunxi_nfc_interrupt,
  1675. 0, "sunxi-nand", nfc);
  1676. if (ret)
  1677. goto out_ahb_reset_reassert;
  1678. nfc->dmac = dma_request_slave_channel(dev, "rxtx");
  1679. if (nfc->dmac) {
  1680. struct dma_slave_config dmac_cfg = { };
  1681. dmac_cfg.src_addr = r->start + NFC_REG_IO_DATA;
  1682. dmac_cfg.dst_addr = dmac_cfg.src_addr;
  1683. dmac_cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1684. dmac_cfg.dst_addr_width = dmac_cfg.src_addr_width;
  1685. dmac_cfg.src_maxburst = 4;
  1686. dmac_cfg.dst_maxburst = 4;
  1687. dmaengine_slave_config(nfc->dmac, &dmac_cfg);
  1688. } else {
  1689. dev_warn(dev, "failed to request rxtx DMA channel\n");
  1690. }
  1691. platform_set_drvdata(pdev, nfc);
  1692. ret = sunxi_nand_chips_init(dev, nfc);
  1693. if (ret) {
  1694. dev_err(dev, "failed to init nand chips\n");
  1695. goto out_release_dmac;
  1696. }
  1697. return 0;
  1698. out_release_dmac:
  1699. if (nfc->dmac)
  1700. dma_release_channel(nfc->dmac);
  1701. out_ahb_reset_reassert:
  1702. reset_control_assert(nfc->reset);
  1703. out_mod_clk_unprepare:
  1704. clk_disable_unprepare(nfc->mod_clk);
  1705. out_ahb_clk_unprepare:
  1706. clk_disable_unprepare(nfc->ahb_clk);
  1707. return ret;
  1708. }
  1709. static int sunxi_nfc_remove(struct platform_device *pdev)
  1710. {
  1711. struct sunxi_nfc *nfc = platform_get_drvdata(pdev);
  1712. sunxi_nand_chips_cleanup(nfc);
  1713. reset_control_assert(nfc->reset);
  1714. if (nfc->dmac)
  1715. dma_release_channel(nfc->dmac);
  1716. clk_disable_unprepare(nfc->mod_clk);
  1717. clk_disable_unprepare(nfc->ahb_clk);
  1718. return 0;
  1719. }
  1720. static const struct of_device_id sunxi_nfc_ids[] = {
  1721. { .compatible = "allwinner,sun4i-a10-nand" },
  1722. { /* sentinel */ }
  1723. };
  1724. MODULE_DEVICE_TABLE(of, sunxi_nfc_ids);
  1725. static struct platform_driver sunxi_nfc_driver = {
  1726. .driver = {
  1727. .name = "sunxi_nand",
  1728. .of_match_table = sunxi_nfc_ids,
  1729. },
  1730. .probe = sunxi_nfc_probe,
  1731. .remove = sunxi_nfc_remove,
  1732. };
  1733. module_platform_driver(sunxi_nfc_driver);
  1734. MODULE_LICENSE("GPL v2");
  1735. MODULE_AUTHOR("Boris BREZILLON");
  1736. MODULE_DESCRIPTION("Allwinner NAND Flash Controller driver");
  1737. MODULE_ALIAS("platform:sunxi_nand");