sunxi_nand.c 55 KB

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