sunxi_nand.c 59 KB

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