atmel_nand.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412
  1. /*
  2. * Copyright © 2003 Rick Bronson
  3. *
  4. * Derived from drivers/mtd/nand/autcpu12.c
  5. * Copyright © 2001 Thomas Gleixner (gleixner@autronix.de)
  6. *
  7. * Derived from drivers/mtd/spia.c
  8. * Copyright © 2000 Steven J. Hill (sjhill@cotw.com)
  9. *
  10. *
  11. * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
  12. * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright © 2007
  13. *
  14. * Derived from Das U-Boot source code
  15. * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
  16. * © Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
  17. *
  18. * Add Programmable Multibit ECC support for various AT91 SoC
  19. * © Copyright 2012 ATMEL, Hong Xu
  20. *
  21. * Add Nand Flash Controller support for SAMA5 SoC
  22. * © Copyright 2013 ATMEL, Josh Wu (josh.wu@atmel.com)
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License version 2 as
  26. * published by the Free Software Foundation.
  27. *
  28. */
  29. #include <linux/clk.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/slab.h>
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/of.h>
  36. #include <linux/of_device.h>
  37. #include <linux/of_gpio.h>
  38. #include <linux/of_mtd.h>
  39. #include <linux/mtd/mtd.h>
  40. #include <linux/mtd/nand.h>
  41. #include <linux/mtd/partitions.h>
  42. #include <linux/delay.h>
  43. #include <linux/dmaengine.h>
  44. #include <linux/gpio.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/io.h>
  47. #include <linux/platform_data/atmel.h>
  48. static int use_dma = 1;
  49. module_param(use_dma, int, 0);
  50. static int on_flash_bbt = 0;
  51. module_param(on_flash_bbt, int, 0);
  52. /* Register access macros */
  53. #define ecc_readl(add, reg) \
  54. __raw_readl(add + ATMEL_ECC_##reg)
  55. #define ecc_writel(add, reg, value) \
  56. __raw_writel((value), add + ATMEL_ECC_##reg)
  57. #include "atmel_nand_ecc.h" /* Hardware ECC registers */
  58. #include "atmel_nand_nfc.h" /* Nand Flash Controller definition */
  59. struct atmel_nand_caps {
  60. bool pmecc_correct_erase_page;
  61. };
  62. /* oob layout for large page size
  63. * bad block info is on bytes 0 and 1
  64. * the bytes have to be consecutives to avoid
  65. * several NAND_CMD_RNDOUT during read
  66. */
  67. static struct nand_ecclayout atmel_oobinfo_large = {
  68. .eccbytes = 4,
  69. .eccpos = {60, 61, 62, 63},
  70. .oobfree = {
  71. {2, 58}
  72. },
  73. };
  74. /* oob layout for small page size
  75. * bad block info is on bytes 4 and 5
  76. * the bytes have to be consecutives to avoid
  77. * several NAND_CMD_RNDOUT during read
  78. */
  79. static struct nand_ecclayout atmel_oobinfo_small = {
  80. .eccbytes = 4,
  81. .eccpos = {0, 1, 2, 3},
  82. .oobfree = {
  83. {6, 10}
  84. },
  85. };
  86. struct atmel_nfc {
  87. void __iomem *base_cmd_regs;
  88. void __iomem *hsmc_regs;
  89. void *sram_bank0;
  90. dma_addr_t sram_bank0_phys;
  91. bool use_nfc_sram;
  92. bool write_by_sram;
  93. struct clk *clk;
  94. bool is_initialized;
  95. struct completion comp_ready;
  96. struct completion comp_cmd_done;
  97. struct completion comp_xfer_done;
  98. /* Point to the sram bank which include readed data via NFC */
  99. void *data_in_sram;
  100. bool will_write_sram;
  101. };
  102. static struct atmel_nfc nand_nfc;
  103. struct atmel_nand_host {
  104. struct nand_chip nand_chip;
  105. struct mtd_info mtd;
  106. void __iomem *io_base;
  107. dma_addr_t io_phys;
  108. struct atmel_nand_data board;
  109. struct device *dev;
  110. void __iomem *ecc;
  111. struct completion comp;
  112. struct dma_chan *dma_chan;
  113. struct atmel_nfc *nfc;
  114. struct atmel_nand_caps *caps;
  115. bool has_pmecc;
  116. u8 pmecc_corr_cap;
  117. u16 pmecc_sector_size;
  118. bool has_no_lookup_table;
  119. u32 pmecc_lookup_table_offset;
  120. u32 pmecc_lookup_table_offset_512;
  121. u32 pmecc_lookup_table_offset_1024;
  122. int pmecc_degree; /* Degree of remainders */
  123. int pmecc_cw_len; /* Length of codeword */
  124. void __iomem *pmerrloc_base;
  125. void __iomem *pmecc_rom_base;
  126. /* lookup table for alpha_to and index_of */
  127. void __iomem *pmecc_alpha_to;
  128. void __iomem *pmecc_index_of;
  129. /* data for pmecc computation */
  130. int16_t *pmecc_partial_syn;
  131. int16_t *pmecc_si;
  132. int16_t *pmecc_smu; /* Sigma table */
  133. int16_t *pmecc_lmu; /* polynomal order */
  134. int *pmecc_mu;
  135. int *pmecc_dmu;
  136. int *pmecc_delta;
  137. };
  138. static struct nand_ecclayout atmel_pmecc_oobinfo;
  139. /*
  140. * Enable NAND.
  141. */
  142. static void atmel_nand_enable(struct atmel_nand_host *host)
  143. {
  144. if (gpio_is_valid(host->board.enable_pin))
  145. gpio_set_value(host->board.enable_pin, 0);
  146. }
  147. /*
  148. * Disable NAND.
  149. */
  150. static void atmel_nand_disable(struct atmel_nand_host *host)
  151. {
  152. if (gpio_is_valid(host->board.enable_pin))
  153. gpio_set_value(host->board.enable_pin, 1);
  154. }
  155. /*
  156. * Hardware specific access to control-lines
  157. */
  158. static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  159. {
  160. struct nand_chip *nand_chip = mtd->priv;
  161. struct atmel_nand_host *host = nand_chip->priv;
  162. if (ctrl & NAND_CTRL_CHANGE) {
  163. if (ctrl & NAND_NCE)
  164. atmel_nand_enable(host);
  165. else
  166. atmel_nand_disable(host);
  167. }
  168. if (cmd == NAND_CMD_NONE)
  169. return;
  170. if (ctrl & NAND_CLE)
  171. writeb(cmd, host->io_base + (1 << host->board.cle));
  172. else
  173. writeb(cmd, host->io_base + (1 << host->board.ale));
  174. }
  175. /*
  176. * Read the Device Ready pin.
  177. */
  178. static int atmel_nand_device_ready(struct mtd_info *mtd)
  179. {
  180. struct nand_chip *nand_chip = mtd->priv;
  181. struct atmel_nand_host *host = nand_chip->priv;
  182. return gpio_get_value(host->board.rdy_pin) ^
  183. !!host->board.rdy_pin_active_low;
  184. }
  185. /* Set up for hardware ready pin and enable pin. */
  186. static int atmel_nand_set_enable_ready_pins(struct mtd_info *mtd)
  187. {
  188. struct nand_chip *chip = mtd->priv;
  189. struct atmel_nand_host *host = chip->priv;
  190. int res = 0;
  191. if (gpio_is_valid(host->board.rdy_pin)) {
  192. res = devm_gpio_request(host->dev,
  193. host->board.rdy_pin, "nand_rdy");
  194. if (res < 0) {
  195. dev_err(host->dev,
  196. "can't request rdy gpio %d\n",
  197. host->board.rdy_pin);
  198. return res;
  199. }
  200. res = gpio_direction_input(host->board.rdy_pin);
  201. if (res < 0) {
  202. dev_err(host->dev,
  203. "can't request input direction rdy gpio %d\n",
  204. host->board.rdy_pin);
  205. return res;
  206. }
  207. chip->dev_ready = atmel_nand_device_ready;
  208. }
  209. if (gpio_is_valid(host->board.enable_pin)) {
  210. res = devm_gpio_request(host->dev,
  211. host->board.enable_pin, "nand_enable");
  212. if (res < 0) {
  213. dev_err(host->dev,
  214. "can't request enable gpio %d\n",
  215. host->board.enable_pin);
  216. return res;
  217. }
  218. res = gpio_direction_output(host->board.enable_pin, 1);
  219. if (res < 0) {
  220. dev_err(host->dev,
  221. "can't request output direction enable gpio %d\n",
  222. host->board.enable_pin);
  223. return res;
  224. }
  225. }
  226. return res;
  227. }
  228. /*
  229. * Minimal-overhead PIO for data access.
  230. */
  231. static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
  232. {
  233. struct nand_chip *nand_chip = mtd->priv;
  234. struct atmel_nand_host *host = nand_chip->priv;
  235. if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
  236. memcpy(buf, host->nfc->data_in_sram, len);
  237. host->nfc->data_in_sram += len;
  238. } else {
  239. __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
  240. }
  241. }
  242. static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
  243. {
  244. struct nand_chip *nand_chip = mtd->priv;
  245. struct atmel_nand_host *host = nand_chip->priv;
  246. if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) {
  247. memcpy(buf, host->nfc->data_in_sram, len);
  248. host->nfc->data_in_sram += len;
  249. } else {
  250. __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
  251. }
  252. }
  253. static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
  254. {
  255. struct nand_chip *nand_chip = mtd->priv;
  256. __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
  257. }
  258. static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
  259. {
  260. struct nand_chip *nand_chip = mtd->priv;
  261. __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
  262. }
  263. static void dma_complete_func(void *completion)
  264. {
  265. complete(completion);
  266. }
  267. static int nfc_set_sram_bank(struct atmel_nand_host *host, unsigned int bank)
  268. {
  269. /* NFC only has two banks. Must be 0 or 1 */
  270. if (bank > 1)
  271. return -EINVAL;
  272. if (bank) {
  273. /* Only for a 2k-page or lower flash, NFC can handle 2 banks */
  274. if (host->mtd.writesize > 2048)
  275. return -EINVAL;
  276. nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK1);
  277. } else {
  278. nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK0);
  279. }
  280. return 0;
  281. }
  282. static uint nfc_get_sram_off(struct atmel_nand_host *host)
  283. {
  284. if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
  285. return NFC_SRAM_BANK1_OFFSET;
  286. else
  287. return 0;
  288. }
  289. static dma_addr_t nfc_sram_phys(struct atmel_nand_host *host)
  290. {
  291. if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1)
  292. return host->nfc->sram_bank0_phys + NFC_SRAM_BANK1_OFFSET;
  293. else
  294. return host->nfc->sram_bank0_phys;
  295. }
  296. static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
  297. int is_read)
  298. {
  299. struct dma_device *dma_dev;
  300. enum dma_ctrl_flags flags;
  301. dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
  302. struct dma_async_tx_descriptor *tx = NULL;
  303. dma_cookie_t cookie;
  304. struct nand_chip *chip = mtd->priv;
  305. struct atmel_nand_host *host = chip->priv;
  306. void *p = buf;
  307. int err = -EIO;
  308. enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  309. struct atmel_nfc *nfc = host->nfc;
  310. if (buf >= high_memory)
  311. goto err_buf;
  312. dma_dev = host->dma_chan->device;
  313. flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  314. phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
  315. if (dma_mapping_error(dma_dev->dev, phys_addr)) {
  316. dev_err(host->dev, "Failed to dma_map_single\n");
  317. goto err_buf;
  318. }
  319. if (is_read) {
  320. if (nfc && nfc->data_in_sram)
  321. dma_src_addr = nfc_sram_phys(host) + (nfc->data_in_sram
  322. - (nfc->sram_bank0 + nfc_get_sram_off(host)));
  323. else
  324. dma_src_addr = host->io_phys;
  325. dma_dst_addr = phys_addr;
  326. } else {
  327. dma_src_addr = phys_addr;
  328. if (nfc && nfc->write_by_sram)
  329. dma_dst_addr = nfc_sram_phys(host);
  330. else
  331. dma_dst_addr = host->io_phys;
  332. }
  333. tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
  334. dma_src_addr, len, flags);
  335. if (!tx) {
  336. dev_err(host->dev, "Failed to prepare DMA memcpy\n");
  337. goto err_dma;
  338. }
  339. init_completion(&host->comp);
  340. tx->callback = dma_complete_func;
  341. tx->callback_param = &host->comp;
  342. cookie = tx->tx_submit(tx);
  343. if (dma_submit_error(cookie)) {
  344. dev_err(host->dev, "Failed to do DMA tx_submit\n");
  345. goto err_dma;
  346. }
  347. dma_async_issue_pending(host->dma_chan);
  348. wait_for_completion(&host->comp);
  349. if (is_read && nfc && nfc->data_in_sram)
  350. /* After read data from SRAM, need to increase the position */
  351. nfc->data_in_sram += len;
  352. err = 0;
  353. err_dma:
  354. dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
  355. err_buf:
  356. if (err != 0)
  357. dev_dbg(host->dev, "Fall back to CPU I/O\n");
  358. return err;
  359. }
  360. static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  361. {
  362. struct nand_chip *chip = mtd->priv;
  363. struct atmel_nand_host *host = chip->priv;
  364. if (use_dma && len > mtd->oobsize)
  365. /* only use DMA for bigger than oob size: better performances */
  366. if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
  367. return;
  368. if (host->board.bus_width_16)
  369. atmel_read_buf16(mtd, buf, len);
  370. else
  371. atmel_read_buf8(mtd, buf, len);
  372. }
  373. static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  374. {
  375. struct nand_chip *chip = mtd->priv;
  376. struct atmel_nand_host *host = chip->priv;
  377. if (use_dma && len > mtd->oobsize)
  378. /* only use DMA for bigger than oob size: better performances */
  379. if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
  380. return;
  381. if (host->board.bus_width_16)
  382. atmel_write_buf16(mtd, buf, len);
  383. else
  384. atmel_write_buf8(mtd, buf, len);
  385. }
  386. /*
  387. * Return number of ecc bytes per sector according to sector size and
  388. * correction capability
  389. *
  390. * Following table shows what at91 PMECC supported:
  391. * Correction Capability Sector_512_bytes Sector_1024_bytes
  392. * ===================== ================ =================
  393. * 2-bits 4-bytes 4-bytes
  394. * 4-bits 7-bytes 7-bytes
  395. * 8-bits 13-bytes 14-bytes
  396. * 12-bits 20-bytes 21-bytes
  397. * 24-bits 39-bytes 42-bytes
  398. */
  399. static int pmecc_get_ecc_bytes(int cap, int sector_size)
  400. {
  401. int m = 12 + sector_size / 512;
  402. return (m * cap + 7) / 8;
  403. }
  404. static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
  405. int oobsize, int ecc_len)
  406. {
  407. int i;
  408. layout->eccbytes = ecc_len;
  409. /* ECC will occupy the last ecc_len bytes continuously */
  410. for (i = 0; i < ecc_len; i++)
  411. layout->eccpos[i] = oobsize - ecc_len + i;
  412. layout->oobfree[0].offset = 2;
  413. layout->oobfree[0].length =
  414. oobsize - ecc_len - layout->oobfree[0].offset;
  415. }
  416. static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
  417. {
  418. int table_size;
  419. table_size = host->pmecc_sector_size == 512 ?
  420. PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
  421. return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
  422. table_size * sizeof(int16_t);
  423. }
  424. static int pmecc_data_alloc(struct atmel_nand_host *host)
  425. {
  426. const int cap = host->pmecc_corr_cap;
  427. int size;
  428. size = (2 * cap + 1) * sizeof(int16_t);
  429. host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
  430. host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
  431. host->pmecc_lmu = devm_kzalloc(host->dev,
  432. (cap + 1) * sizeof(int16_t), GFP_KERNEL);
  433. host->pmecc_smu = devm_kzalloc(host->dev,
  434. (cap + 2) * size, GFP_KERNEL);
  435. size = (cap + 1) * sizeof(int);
  436. host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
  437. host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
  438. host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
  439. if (!host->pmecc_partial_syn ||
  440. !host->pmecc_si ||
  441. !host->pmecc_lmu ||
  442. !host->pmecc_smu ||
  443. !host->pmecc_mu ||
  444. !host->pmecc_dmu ||
  445. !host->pmecc_delta)
  446. return -ENOMEM;
  447. return 0;
  448. }
  449. static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
  450. {
  451. struct nand_chip *nand_chip = mtd->priv;
  452. struct atmel_nand_host *host = nand_chip->priv;
  453. int i;
  454. uint32_t value;
  455. /* Fill odd syndromes */
  456. for (i = 0; i < host->pmecc_corr_cap; i++) {
  457. value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
  458. if (i & 1)
  459. value >>= 16;
  460. value &= 0xffff;
  461. host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
  462. }
  463. }
  464. static void pmecc_substitute(struct mtd_info *mtd)
  465. {
  466. struct nand_chip *nand_chip = mtd->priv;
  467. struct atmel_nand_host *host = nand_chip->priv;
  468. int16_t __iomem *alpha_to = host->pmecc_alpha_to;
  469. int16_t __iomem *index_of = host->pmecc_index_of;
  470. int16_t *partial_syn = host->pmecc_partial_syn;
  471. const int cap = host->pmecc_corr_cap;
  472. int16_t *si;
  473. int i, j;
  474. /* si[] is a table that holds the current syndrome value,
  475. * an element of that table belongs to the field
  476. */
  477. si = host->pmecc_si;
  478. memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
  479. /* Computation 2t syndromes based on S(x) */
  480. /* Odd syndromes */
  481. for (i = 1; i < 2 * cap; i += 2) {
  482. for (j = 0; j < host->pmecc_degree; j++) {
  483. if (partial_syn[i] & ((unsigned short)0x1 << j))
  484. si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
  485. }
  486. }
  487. /* Even syndrome = (Odd syndrome) ** 2 */
  488. for (i = 2, j = 1; j <= cap; i = ++j << 1) {
  489. if (si[j] == 0) {
  490. si[i] = 0;
  491. } else {
  492. int16_t tmp;
  493. tmp = readw_relaxed(index_of + si[j]);
  494. tmp = (tmp * 2) % host->pmecc_cw_len;
  495. si[i] = readw_relaxed(alpha_to + tmp);
  496. }
  497. }
  498. return;
  499. }
  500. static void pmecc_get_sigma(struct mtd_info *mtd)
  501. {
  502. struct nand_chip *nand_chip = mtd->priv;
  503. struct atmel_nand_host *host = nand_chip->priv;
  504. int16_t *lmu = host->pmecc_lmu;
  505. int16_t *si = host->pmecc_si;
  506. int *mu = host->pmecc_mu;
  507. int *dmu = host->pmecc_dmu; /* Discrepancy */
  508. int *delta = host->pmecc_delta; /* Delta order */
  509. int cw_len = host->pmecc_cw_len;
  510. const int16_t cap = host->pmecc_corr_cap;
  511. const int num = 2 * cap + 1;
  512. int16_t __iomem *index_of = host->pmecc_index_of;
  513. int16_t __iomem *alpha_to = host->pmecc_alpha_to;
  514. int i, j, k;
  515. uint32_t dmu_0_count, tmp;
  516. int16_t *smu = host->pmecc_smu;
  517. /* index of largest delta */
  518. int ro;
  519. int largest;
  520. int diff;
  521. dmu_0_count = 0;
  522. /* First Row */
  523. /* Mu */
  524. mu[0] = -1;
  525. memset(smu, 0, sizeof(int16_t) * num);
  526. smu[0] = 1;
  527. /* discrepancy set to 1 */
  528. dmu[0] = 1;
  529. /* polynom order set to 0 */
  530. lmu[0] = 0;
  531. delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
  532. /* Second Row */
  533. /* Mu */
  534. mu[1] = 0;
  535. /* Sigma(x) set to 1 */
  536. memset(&smu[num], 0, sizeof(int16_t) * num);
  537. smu[num] = 1;
  538. /* discrepancy set to S1 */
  539. dmu[1] = si[1];
  540. /* polynom order set to 0 */
  541. lmu[1] = 0;
  542. delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
  543. /* Init the Sigma(x) last row */
  544. memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
  545. for (i = 1; i <= cap; i++) {
  546. mu[i + 1] = i << 1;
  547. /* Begin Computing Sigma (Mu+1) and L(mu) */
  548. /* check if discrepancy is set to 0 */
  549. if (dmu[i] == 0) {
  550. dmu_0_count++;
  551. tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
  552. if ((cap - (lmu[i] >> 1) - 1) & 0x1)
  553. tmp += 2;
  554. else
  555. tmp += 1;
  556. if (dmu_0_count == tmp) {
  557. for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
  558. smu[(cap + 1) * num + j] =
  559. smu[i * num + j];
  560. lmu[cap + 1] = lmu[i];
  561. return;
  562. }
  563. /* copy polynom */
  564. for (j = 0; j <= lmu[i] >> 1; j++)
  565. smu[(i + 1) * num + j] = smu[i * num + j];
  566. /* copy previous polynom order to the next */
  567. lmu[i + 1] = lmu[i];
  568. } else {
  569. ro = 0;
  570. largest = -1;
  571. /* find largest delta with dmu != 0 */
  572. for (j = 0; j < i; j++) {
  573. if ((dmu[j]) && (delta[j] > largest)) {
  574. largest = delta[j];
  575. ro = j;
  576. }
  577. }
  578. /* compute difference */
  579. diff = (mu[i] - mu[ro]);
  580. /* Compute degree of the new smu polynomial */
  581. if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
  582. lmu[i + 1] = lmu[i];
  583. else
  584. lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
  585. /* Init smu[i+1] with 0 */
  586. for (k = 0; k < num; k++)
  587. smu[(i + 1) * num + k] = 0;
  588. /* Compute smu[i+1] */
  589. for (k = 0; k <= lmu[ro] >> 1; k++) {
  590. int16_t a, b, c;
  591. if (!(smu[ro * num + k] && dmu[i]))
  592. continue;
  593. a = readw_relaxed(index_of + dmu[i]);
  594. b = readw_relaxed(index_of + dmu[ro]);
  595. c = readw_relaxed(index_of + smu[ro * num + k]);
  596. tmp = a + (cw_len - b) + c;
  597. a = readw_relaxed(alpha_to + tmp % cw_len);
  598. smu[(i + 1) * num + (k + diff)] = a;
  599. }
  600. for (k = 0; k <= lmu[i] >> 1; k++)
  601. smu[(i + 1) * num + k] ^= smu[i * num + k];
  602. }
  603. /* End Computing Sigma (Mu+1) and L(mu) */
  604. /* In either case compute delta */
  605. delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
  606. /* Do not compute discrepancy for the last iteration */
  607. if (i >= cap)
  608. continue;
  609. for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
  610. tmp = 2 * (i - 1);
  611. if (k == 0) {
  612. dmu[i + 1] = si[tmp + 3];
  613. } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
  614. int16_t a, b, c;
  615. a = readw_relaxed(index_of +
  616. smu[(i + 1) * num + k]);
  617. b = si[2 * (i - 1) + 3 - k];
  618. c = readw_relaxed(index_of + b);
  619. tmp = a + c;
  620. tmp %= cw_len;
  621. dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
  622. dmu[i + 1];
  623. }
  624. }
  625. }
  626. return;
  627. }
  628. static int pmecc_err_location(struct mtd_info *mtd)
  629. {
  630. struct nand_chip *nand_chip = mtd->priv;
  631. struct atmel_nand_host *host = nand_chip->priv;
  632. unsigned long end_time;
  633. const int cap = host->pmecc_corr_cap;
  634. const int num = 2 * cap + 1;
  635. int sector_size = host->pmecc_sector_size;
  636. int err_nbr = 0; /* number of error */
  637. int roots_nbr; /* number of roots */
  638. int i;
  639. uint32_t val;
  640. int16_t *smu = host->pmecc_smu;
  641. pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
  642. for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
  643. pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
  644. smu[(cap + 1) * num + i]);
  645. err_nbr++;
  646. }
  647. val = (err_nbr - 1) << 16;
  648. if (sector_size == 1024)
  649. val |= 1;
  650. pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
  651. pmerrloc_writel(host->pmerrloc_base, ELEN,
  652. sector_size * 8 + host->pmecc_degree * cap);
  653. end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
  654. while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
  655. & PMERRLOC_CALC_DONE)) {
  656. if (unlikely(time_after(jiffies, end_time))) {
  657. dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
  658. return -1;
  659. }
  660. cpu_relax();
  661. }
  662. roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
  663. & PMERRLOC_ERR_NUM_MASK) >> 8;
  664. /* Number of roots == degree of smu hence <= cap */
  665. if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
  666. return err_nbr - 1;
  667. /* Number of roots does not match the degree of smu
  668. * unable to correct error */
  669. return -1;
  670. }
  671. static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
  672. int sector_num, int extra_bytes, int err_nbr)
  673. {
  674. struct nand_chip *nand_chip = mtd->priv;
  675. struct atmel_nand_host *host = nand_chip->priv;
  676. int i = 0;
  677. int byte_pos, bit_pos, sector_size, pos;
  678. uint32_t tmp;
  679. uint8_t err_byte;
  680. sector_size = host->pmecc_sector_size;
  681. while (err_nbr) {
  682. tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1;
  683. byte_pos = tmp / 8;
  684. bit_pos = tmp % 8;
  685. if (byte_pos >= (sector_size + extra_bytes))
  686. BUG(); /* should never happen */
  687. if (byte_pos < sector_size) {
  688. err_byte = *(buf + byte_pos);
  689. *(buf + byte_pos) ^= (1 << bit_pos);
  690. pos = sector_num * host->pmecc_sector_size + byte_pos;
  691. dev_info(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
  692. pos, bit_pos, err_byte, *(buf + byte_pos));
  693. } else {
  694. /* Bit flip in OOB area */
  695. tmp = sector_num * nand_chip->ecc.bytes
  696. + (byte_pos - sector_size);
  697. err_byte = ecc[tmp];
  698. ecc[tmp] ^= (1 << bit_pos);
  699. pos = tmp + nand_chip->ecc.layout->eccpos[0];
  700. dev_info(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
  701. pos, bit_pos, err_byte, ecc[tmp]);
  702. }
  703. i++;
  704. err_nbr--;
  705. }
  706. return;
  707. }
  708. static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
  709. u8 *ecc)
  710. {
  711. struct nand_chip *nand_chip = mtd->priv;
  712. struct atmel_nand_host *host = nand_chip->priv;
  713. int i, err_nbr;
  714. uint8_t *buf_pos;
  715. int max_bitflips = 0;
  716. /* If can correct bitfilps from erased page, do the normal check */
  717. if (host->caps->pmecc_correct_erase_page)
  718. goto normal_check;
  719. for (i = 0; i < nand_chip->ecc.total; i++)
  720. if (ecc[i] != 0xff)
  721. goto normal_check;
  722. /* Erased page, return OK */
  723. return 0;
  724. normal_check:
  725. for (i = 0; i < nand_chip->ecc.steps; i++) {
  726. err_nbr = 0;
  727. if (pmecc_stat & 0x1) {
  728. buf_pos = buf + i * host->pmecc_sector_size;
  729. pmecc_gen_syndrome(mtd, i);
  730. pmecc_substitute(mtd);
  731. pmecc_get_sigma(mtd);
  732. err_nbr = pmecc_err_location(mtd);
  733. if (err_nbr == -1) {
  734. dev_err(host->dev, "PMECC: Too many errors\n");
  735. mtd->ecc_stats.failed++;
  736. return -EIO;
  737. } else {
  738. pmecc_correct_data(mtd, buf_pos, ecc, i,
  739. nand_chip->ecc.bytes, err_nbr);
  740. mtd->ecc_stats.corrected += err_nbr;
  741. max_bitflips = max_t(int, max_bitflips, err_nbr);
  742. }
  743. }
  744. pmecc_stat >>= 1;
  745. }
  746. return max_bitflips;
  747. }
  748. static void pmecc_enable(struct atmel_nand_host *host, int ecc_op)
  749. {
  750. u32 val;
  751. if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) {
  752. dev_err(host->dev, "atmel_nand: wrong pmecc operation type!");
  753. return;
  754. }
  755. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
  756. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  757. val = pmecc_readl_relaxed(host->ecc, CFG);
  758. if (ecc_op == NAND_ECC_READ)
  759. pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP)
  760. | PMECC_CFG_AUTO_ENABLE);
  761. else
  762. pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP)
  763. & ~PMECC_CFG_AUTO_ENABLE);
  764. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
  765. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
  766. }
  767. static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
  768. struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
  769. {
  770. struct atmel_nand_host *host = chip->priv;
  771. int eccsize = chip->ecc.size * chip->ecc.steps;
  772. uint8_t *oob = chip->oob_poi;
  773. uint32_t *eccpos = chip->ecc.layout->eccpos;
  774. uint32_t stat;
  775. unsigned long end_time;
  776. int bitflips = 0;
  777. if (!host->nfc || !host->nfc->use_nfc_sram)
  778. pmecc_enable(host, NAND_ECC_READ);
  779. chip->read_buf(mtd, buf, eccsize);
  780. chip->read_buf(mtd, oob, mtd->oobsize);
  781. end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
  782. while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
  783. if (unlikely(time_after(jiffies, end_time))) {
  784. dev_err(host->dev, "PMECC: Timeout to get error status.\n");
  785. return -EIO;
  786. }
  787. cpu_relax();
  788. }
  789. stat = pmecc_readl_relaxed(host->ecc, ISR);
  790. if (stat != 0) {
  791. bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
  792. if (bitflips < 0)
  793. /* uncorrectable errors */
  794. return 0;
  795. }
  796. return bitflips;
  797. }
  798. static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
  799. struct nand_chip *chip, const uint8_t *buf, int oob_required)
  800. {
  801. struct atmel_nand_host *host = chip->priv;
  802. uint32_t *eccpos = chip->ecc.layout->eccpos;
  803. int i, j;
  804. unsigned long end_time;
  805. if (!host->nfc || !host->nfc->write_by_sram) {
  806. pmecc_enable(host, NAND_ECC_WRITE);
  807. chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
  808. }
  809. end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
  810. while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
  811. if (unlikely(time_after(jiffies, end_time))) {
  812. dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
  813. return -EIO;
  814. }
  815. cpu_relax();
  816. }
  817. for (i = 0; i < chip->ecc.steps; i++) {
  818. for (j = 0; j < chip->ecc.bytes; j++) {
  819. int pos;
  820. pos = i * chip->ecc.bytes + j;
  821. chip->oob_poi[eccpos[pos]] =
  822. pmecc_readb_ecc_relaxed(host->ecc, i, j);
  823. }
  824. }
  825. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  826. return 0;
  827. }
  828. static void atmel_pmecc_core_init(struct mtd_info *mtd)
  829. {
  830. struct nand_chip *nand_chip = mtd->priv;
  831. struct atmel_nand_host *host = nand_chip->priv;
  832. uint32_t val = 0;
  833. struct nand_ecclayout *ecc_layout;
  834. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
  835. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  836. switch (host->pmecc_corr_cap) {
  837. case 2:
  838. val = PMECC_CFG_BCH_ERR2;
  839. break;
  840. case 4:
  841. val = PMECC_CFG_BCH_ERR4;
  842. break;
  843. case 8:
  844. val = PMECC_CFG_BCH_ERR8;
  845. break;
  846. case 12:
  847. val = PMECC_CFG_BCH_ERR12;
  848. break;
  849. case 24:
  850. val = PMECC_CFG_BCH_ERR24;
  851. break;
  852. }
  853. if (host->pmecc_sector_size == 512)
  854. val |= PMECC_CFG_SECTOR512;
  855. else if (host->pmecc_sector_size == 1024)
  856. val |= PMECC_CFG_SECTOR1024;
  857. switch (nand_chip->ecc.steps) {
  858. case 1:
  859. val |= PMECC_CFG_PAGE_1SECTOR;
  860. break;
  861. case 2:
  862. val |= PMECC_CFG_PAGE_2SECTORS;
  863. break;
  864. case 4:
  865. val |= PMECC_CFG_PAGE_4SECTORS;
  866. break;
  867. case 8:
  868. val |= PMECC_CFG_PAGE_8SECTORS;
  869. break;
  870. }
  871. val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
  872. | PMECC_CFG_AUTO_DISABLE);
  873. pmecc_writel(host->ecc, CFG, val);
  874. ecc_layout = nand_chip->ecc.layout;
  875. pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
  876. pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
  877. pmecc_writel(host->ecc, EADDR,
  878. ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
  879. /* See datasheet about PMECC Clock Control Register */
  880. pmecc_writel(host->ecc, CLK, 2);
  881. pmecc_writel(host->ecc, IDR, 0xff);
  882. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
  883. }
  884. /*
  885. * Get minimum ecc requirements from NAND.
  886. * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
  887. * will set them according to minimum ecc requirement. Otherwise, use the
  888. * value in DTS file.
  889. * return 0 if success. otherwise return error code.
  890. */
  891. static int pmecc_choose_ecc(struct atmel_nand_host *host,
  892. int *cap, int *sector_size)
  893. {
  894. /* Get minimum ECC requirements */
  895. if (host->nand_chip.ecc_strength_ds) {
  896. *cap = host->nand_chip.ecc_strength_ds;
  897. *sector_size = host->nand_chip.ecc_step_ds;
  898. dev_info(host->dev, "minimum ECC: %d bits in %d bytes\n",
  899. *cap, *sector_size);
  900. } else {
  901. *cap = 2;
  902. *sector_size = 512;
  903. dev_info(host->dev, "can't detect min. ECC, assume 2 bits in 512 bytes\n");
  904. }
  905. /* If device tree doesn't specify, use NAND's minimum ECC parameters */
  906. if (host->pmecc_corr_cap == 0) {
  907. /* use the most fitable ecc bits (the near bigger one ) */
  908. if (*cap <= 2)
  909. host->pmecc_corr_cap = 2;
  910. else if (*cap <= 4)
  911. host->pmecc_corr_cap = 4;
  912. else if (*cap <= 8)
  913. host->pmecc_corr_cap = 8;
  914. else if (*cap <= 12)
  915. host->pmecc_corr_cap = 12;
  916. else if (*cap <= 24)
  917. host->pmecc_corr_cap = 24;
  918. else
  919. return -EINVAL;
  920. }
  921. if (host->pmecc_sector_size == 0) {
  922. /* use the most fitable sector size (the near smaller one ) */
  923. if (*sector_size >= 1024)
  924. host->pmecc_sector_size = 1024;
  925. else if (*sector_size >= 512)
  926. host->pmecc_sector_size = 512;
  927. else
  928. return -EINVAL;
  929. }
  930. return 0;
  931. }
  932. static inline int deg(unsigned int poly)
  933. {
  934. /* polynomial degree is the most-significant bit index */
  935. return fls(poly) - 1;
  936. }
  937. static int build_gf_tables(int mm, unsigned int poly,
  938. int16_t *index_of, int16_t *alpha_to)
  939. {
  940. unsigned int i, x = 1;
  941. const unsigned int k = 1 << deg(poly);
  942. unsigned int nn = (1 << mm) - 1;
  943. /* primitive polynomial must be of degree m */
  944. if (k != (1u << mm))
  945. return -EINVAL;
  946. for (i = 0; i < nn; i++) {
  947. alpha_to[i] = x;
  948. index_of[x] = i;
  949. if (i && (x == 1))
  950. /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
  951. return -EINVAL;
  952. x <<= 1;
  953. if (x & k)
  954. x ^= poly;
  955. }
  956. alpha_to[nn] = 1;
  957. index_of[0] = 0;
  958. return 0;
  959. }
  960. static uint16_t *create_lookup_table(struct device *dev, int sector_size)
  961. {
  962. int degree = (sector_size == 512) ?
  963. PMECC_GF_DIMENSION_13 :
  964. PMECC_GF_DIMENSION_14;
  965. unsigned int poly = (sector_size == 512) ?
  966. PMECC_GF_13_PRIMITIVE_POLY :
  967. PMECC_GF_14_PRIMITIVE_POLY;
  968. int table_size = (sector_size == 512) ?
  969. PMECC_LOOKUP_TABLE_SIZE_512 :
  970. PMECC_LOOKUP_TABLE_SIZE_1024;
  971. int16_t *addr = devm_kzalloc(dev, 2 * table_size * sizeof(uint16_t),
  972. GFP_KERNEL);
  973. if (addr && build_gf_tables(degree, poly, addr, addr + table_size))
  974. return NULL;
  975. return addr;
  976. }
  977. static int atmel_pmecc_nand_init_params(struct platform_device *pdev,
  978. struct atmel_nand_host *host)
  979. {
  980. struct mtd_info *mtd = &host->mtd;
  981. struct nand_chip *nand_chip = &host->nand_chip;
  982. struct resource *regs, *regs_pmerr, *regs_rom;
  983. uint16_t *galois_table;
  984. int cap, sector_size, err_no;
  985. err_no = pmecc_choose_ecc(host, &cap, &sector_size);
  986. if (err_no) {
  987. dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
  988. return err_no;
  989. }
  990. if (cap > host->pmecc_corr_cap ||
  991. sector_size != host->pmecc_sector_size)
  992. dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
  993. cap = host->pmecc_corr_cap;
  994. sector_size = host->pmecc_sector_size;
  995. host->pmecc_lookup_table_offset = (sector_size == 512) ?
  996. host->pmecc_lookup_table_offset_512 :
  997. host->pmecc_lookup_table_offset_1024;
  998. dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
  999. cap, sector_size);
  1000. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1001. if (!regs) {
  1002. dev_warn(host->dev,
  1003. "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
  1004. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1005. return 0;
  1006. }
  1007. host->ecc = devm_ioremap_resource(&pdev->dev, regs);
  1008. if (IS_ERR(host->ecc)) {
  1009. err_no = PTR_ERR(host->ecc);
  1010. goto err;
  1011. }
  1012. regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  1013. host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
  1014. if (IS_ERR(host->pmerrloc_base)) {
  1015. err_no = PTR_ERR(host->pmerrloc_base);
  1016. goto err;
  1017. }
  1018. regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
  1019. host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev, regs_rom);
  1020. if (IS_ERR(host->pmecc_rom_base)) {
  1021. if (!host->has_no_lookup_table)
  1022. /* Don't display the information again */
  1023. dev_err(host->dev, "Can not get I/O resource for ROM, will build a lookup table in runtime!\n");
  1024. host->has_no_lookup_table = true;
  1025. }
  1026. if (host->has_no_lookup_table) {
  1027. /* Build the look-up table in runtime */
  1028. galois_table = create_lookup_table(host->dev, sector_size);
  1029. if (!galois_table) {
  1030. dev_err(host->dev, "Failed to build a lookup table in runtime!\n");
  1031. err_no = -EINVAL;
  1032. goto err;
  1033. }
  1034. host->pmecc_rom_base = (void __iomem *)galois_table;
  1035. host->pmecc_lookup_table_offset = 0;
  1036. }
  1037. nand_chip->ecc.size = sector_size;
  1038. /* set ECC page size and oob layout */
  1039. switch (mtd->writesize) {
  1040. case 512:
  1041. case 1024:
  1042. case 2048:
  1043. case 4096:
  1044. case 8192:
  1045. if (sector_size > mtd->writesize) {
  1046. dev_err(host->dev, "pmecc sector size is bigger than the page size!\n");
  1047. err_no = -EINVAL;
  1048. goto err;
  1049. }
  1050. host->pmecc_degree = (sector_size == 512) ?
  1051. PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
  1052. host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
  1053. host->pmecc_alpha_to = pmecc_get_alpha_to(host);
  1054. host->pmecc_index_of = host->pmecc_rom_base +
  1055. host->pmecc_lookup_table_offset;
  1056. nand_chip->ecc.strength = cap;
  1057. nand_chip->ecc.bytes = pmecc_get_ecc_bytes(cap, sector_size);
  1058. nand_chip->ecc.steps = mtd->writesize / sector_size;
  1059. nand_chip->ecc.total = nand_chip->ecc.bytes *
  1060. nand_chip->ecc.steps;
  1061. if (nand_chip->ecc.total > mtd->oobsize - 2) {
  1062. dev_err(host->dev, "No room for ECC bytes\n");
  1063. err_no = -EINVAL;
  1064. goto err;
  1065. }
  1066. pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
  1067. mtd->oobsize,
  1068. nand_chip->ecc.total);
  1069. nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
  1070. break;
  1071. default:
  1072. dev_warn(host->dev,
  1073. "Unsupported page size for PMECC, use Software ECC\n");
  1074. /* page size not handled by HW ECC */
  1075. /* switching back to soft ECC */
  1076. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1077. return 0;
  1078. }
  1079. /* Allocate data for PMECC computation */
  1080. err_no = pmecc_data_alloc(host);
  1081. if (err_no) {
  1082. dev_err(host->dev,
  1083. "Cannot allocate memory for PMECC computation!\n");
  1084. goto err;
  1085. }
  1086. nand_chip->options |= NAND_NO_SUBPAGE_WRITE;
  1087. nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
  1088. nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
  1089. atmel_pmecc_core_init(mtd);
  1090. return 0;
  1091. err:
  1092. return err_no;
  1093. }
  1094. /*
  1095. * Calculate HW ECC
  1096. *
  1097. * function called after a write
  1098. *
  1099. * mtd: MTD block structure
  1100. * dat: raw data (unused)
  1101. * ecc_code: buffer for ECC
  1102. */
  1103. static int atmel_nand_calculate(struct mtd_info *mtd,
  1104. const u_char *dat, unsigned char *ecc_code)
  1105. {
  1106. struct nand_chip *nand_chip = mtd->priv;
  1107. struct atmel_nand_host *host = nand_chip->priv;
  1108. unsigned int ecc_value;
  1109. /* get the first 2 ECC bytes */
  1110. ecc_value = ecc_readl(host->ecc, PR);
  1111. ecc_code[0] = ecc_value & 0xFF;
  1112. ecc_code[1] = (ecc_value >> 8) & 0xFF;
  1113. /* get the last 2 ECC bytes */
  1114. ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
  1115. ecc_code[2] = ecc_value & 0xFF;
  1116. ecc_code[3] = (ecc_value >> 8) & 0xFF;
  1117. return 0;
  1118. }
  1119. /*
  1120. * HW ECC read page function
  1121. *
  1122. * mtd: mtd info structure
  1123. * chip: nand chip info structure
  1124. * buf: buffer to store read data
  1125. * oob_required: caller expects OOB data read to chip->oob_poi
  1126. */
  1127. static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  1128. uint8_t *buf, int oob_required, int page)
  1129. {
  1130. int eccsize = chip->ecc.size;
  1131. int eccbytes = chip->ecc.bytes;
  1132. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1133. uint8_t *p = buf;
  1134. uint8_t *oob = chip->oob_poi;
  1135. uint8_t *ecc_pos;
  1136. int stat;
  1137. unsigned int max_bitflips = 0;
  1138. /*
  1139. * Errata: ALE is incorrectly wired up to the ECC controller
  1140. * on the AP7000, so it will include the address cycles in the
  1141. * ECC calculation.
  1142. *
  1143. * Workaround: Reset the parity registers before reading the
  1144. * actual data.
  1145. */
  1146. struct atmel_nand_host *host = chip->priv;
  1147. if (host->board.need_reset_workaround)
  1148. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  1149. /* read the page */
  1150. chip->read_buf(mtd, p, eccsize);
  1151. /* move to ECC position if needed */
  1152. if (eccpos[0] != 0) {
  1153. /* This only works on large pages
  1154. * because the ECC controller waits for
  1155. * NAND_CMD_RNDOUTSTART after the
  1156. * NAND_CMD_RNDOUT.
  1157. * anyway, for small pages, the eccpos[0] == 0
  1158. */
  1159. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  1160. mtd->writesize + eccpos[0], -1);
  1161. }
  1162. /* the ECC controller needs to read the ECC just after the data */
  1163. ecc_pos = oob + eccpos[0];
  1164. chip->read_buf(mtd, ecc_pos, eccbytes);
  1165. /* check if there's an error */
  1166. stat = chip->ecc.correct(mtd, p, oob, NULL);
  1167. if (stat < 0) {
  1168. mtd->ecc_stats.failed++;
  1169. } else {
  1170. mtd->ecc_stats.corrected += stat;
  1171. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1172. }
  1173. /* get back to oob start (end of page) */
  1174. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  1175. /* read the oob */
  1176. chip->read_buf(mtd, oob, mtd->oobsize);
  1177. return max_bitflips;
  1178. }
  1179. /*
  1180. * HW ECC Correction
  1181. *
  1182. * function called after a read
  1183. *
  1184. * mtd: MTD block structure
  1185. * dat: raw data read from the chip
  1186. * read_ecc: ECC from the chip (unused)
  1187. * isnull: unused
  1188. *
  1189. * Detect and correct a 1 bit error for a page
  1190. */
  1191. static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
  1192. u_char *read_ecc, u_char *isnull)
  1193. {
  1194. struct nand_chip *nand_chip = mtd->priv;
  1195. struct atmel_nand_host *host = nand_chip->priv;
  1196. unsigned int ecc_status;
  1197. unsigned int ecc_word, ecc_bit;
  1198. /* get the status from the Status Register */
  1199. ecc_status = ecc_readl(host->ecc, SR);
  1200. /* if there's no error */
  1201. if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
  1202. return 0;
  1203. /* get error bit offset (4 bits) */
  1204. ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
  1205. /* get word address (12 bits) */
  1206. ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
  1207. ecc_word >>= 4;
  1208. /* if there are multiple errors */
  1209. if (ecc_status & ATMEL_ECC_MULERR) {
  1210. /* check if it is a freshly erased block
  1211. * (filled with 0xff) */
  1212. if ((ecc_bit == ATMEL_ECC_BITADDR)
  1213. && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
  1214. /* the block has just been erased, return OK */
  1215. return 0;
  1216. }
  1217. /* it doesn't seems to be a freshly
  1218. * erased block.
  1219. * We can't correct so many errors */
  1220. dev_dbg(host->dev, "atmel_nand : multiple errors detected."
  1221. " Unable to correct.\n");
  1222. return -EIO;
  1223. }
  1224. /* if there's a single bit error : we can correct it */
  1225. if (ecc_status & ATMEL_ECC_ECCERR) {
  1226. /* there's nothing much to do here.
  1227. * the bit error is on the ECC itself.
  1228. */
  1229. dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
  1230. " Nothing to correct\n");
  1231. return 0;
  1232. }
  1233. dev_dbg(host->dev, "atmel_nand : one bit error on data."
  1234. " (word offset in the page :"
  1235. " 0x%x bit offset : 0x%x)\n",
  1236. ecc_word, ecc_bit);
  1237. /* correct the error */
  1238. if (nand_chip->options & NAND_BUSWIDTH_16) {
  1239. /* 16 bits words */
  1240. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  1241. } else {
  1242. /* 8 bits words */
  1243. dat[ecc_word] ^= (1 << ecc_bit);
  1244. }
  1245. dev_dbg(host->dev, "atmel_nand : error corrected\n");
  1246. return 1;
  1247. }
  1248. /*
  1249. * Enable HW ECC : unused on most chips
  1250. */
  1251. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  1252. {
  1253. struct nand_chip *nand_chip = mtd->priv;
  1254. struct atmel_nand_host *host = nand_chip->priv;
  1255. if (host->board.need_reset_workaround)
  1256. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  1257. }
  1258. static const struct of_device_id atmel_nand_dt_ids[];
  1259. static int atmel_of_init_port(struct atmel_nand_host *host,
  1260. struct device_node *np)
  1261. {
  1262. u32 val;
  1263. u32 offset[2];
  1264. int ecc_mode;
  1265. struct atmel_nand_data *board = &host->board;
  1266. enum of_gpio_flags flags = 0;
  1267. host->caps = (struct atmel_nand_caps *)
  1268. of_match_device(atmel_nand_dt_ids, host->dev)->data;
  1269. if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
  1270. if (val >= 32) {
  1271. dev_err(host->dev, "invalid addr-offset %u\n", val);
  1272. return -EINVAL;
  1273. }
  1274. board->ale = val;
  1275. }
  1276. if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
  1277. if (val >= 32) {
  1278. dev_err(host->dev, "invalid cmd-offset %u\n", val);
  1279. return -EINVAL;
  1280. }
  1281. board->cle = val;
  1282. }
  1283. ecc_mode = of_get_nand_ecc_mode(np);
  1284. board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
  1285. board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
  1286. board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
  1287. if (of_get_nand_bus_width(np) == 16)
  1288. board->bus_width_16 = 1;
  1289. board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
  1290. board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
  1291. board->enable_pin = of_get_gpio(np, 1);
  1292. board->det_pin = of_get_gpio(np, 2);
  1293. host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
  1294. /* load the nfc driver if there is */
  1295. of_platform_populate(np, NULL, NULL, host->dev);
  1296. if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
  1297. return 0; /* Not using PMECC */
  1298. /* use PMECC, get correction capability, sector size and lookup
  1299. * table offset.
  1300. * If correction bits and sector size are not specified, then find
  1301. * them from NAND ONFI parameters.
  1302. */
  1303. if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
  1304. if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
  1305. (val != 24)) {
  1306. dev_err(host->dev,
  1307. "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
  1308. val);
  1309. return -EINVAL;
  1310. }
  1311. host->pmecc_corr_cap = (u8)val;
  1312. }
  1313. if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
  1314. if ((val != 512) && (val != 1024)) {
  1315. dev_err(host->dev,
  1316. "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
  1317. val);
  1318. return -EINVAL;
  1319. }
  1320. host->pmecc_sector_size = (u16)val;
  1321. }
  1322. if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
  1323. offset, 2) != 0) {
  1324. dev_err(host->dev, "Cannot get PMECC lookup table offset, will build a lookup table in runtime.\n");
  1325. host->has_no_lookup_table = true;
  1326. /* Will build a lookup table and initialize the offset later */
  1327. return 0;
  1328. }
  1329. if (!offset[0] && !offset[1]) {
  1330. dev_err(host->dev, "Invalid PMECC lookup table offset\n");
  1331. return -EINVAL;
  1332. }
  1333. host->pmecc_lookup_table_offset_512 = offset[0];
  1334. host->pmecc_lookup_table_offset_1024 = offset[1];
  1335. return 0;
  1336. }
  1337. static int atmel_hw_nand_init_params(struct platform_device *pdev,
  1338. struct atmel_nand_host *host)
  1339. {
  1340. struct mtd_info *mtd = &host->mtd;
  1341. struct nand_chip *nand_chip = &host->nand_chip;
  1342. struct resource *regs;
  1343. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1344. if (!regs) {
  1345. dev_err(host->dev,
  1346. "Can't get I/O resource regs, use software ECC\n");
  1347. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1348. return 0;
  1349. }
  1350. host->ecc = devm_ioremap_resource(&pdev->dev, regs);
  1351. if (IS_ERR(host->ecc))
  1352. return PTR_ERR(host->ecc);
  1353. /* ECC is calculated for the whole page (1 step) */
  1354. nand_chip->ecc.size = mtd->writesize;
  1355. /* set ECC page size and oob layout */
  1356. switch (mtd->writesize) {
  1357. case 512:
  1358. nand_chip->ecc.layout = &atmel_oobinfo_small;
  1359. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
  1360. break;
  1361. case 1024:
  1362. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1363. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
  1364. break;
  1365. case 2048:
  1366. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1367. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
  1368. break;
  1369. case 4096:
  1370. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1371. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
  1372. break;
  1373. default:
  1374. /* page size not handled by HW ECC */
  1375. /* switching back to soft ECC */
  1376. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1377. return 0;
  1378. }
  1379. /* set up for HW ECC */
  1380. nand_chip->ecc.calculate = atmel_nand_calculate;
  1381. nand_chip->ecc.correct = atmel_nand_correct;
  1382. nand_chip->ecc.hwctl = atmel_nand_hwctl;
  1383. nand_chip->ecc.read_page = atmel_nand_read_page;
  1384. nand_chip->ecc.bytes = 4;
  1385. nand_chip->ecc.strength = 1;
  1386. return 0;
  1387. }
  1388. static inline u32 nfc_read_status(struct atmel_nand_host *host)
  1389. {
  1390. u32 err_flags = NFC_SR_DTOE | NFC_SR_UNDEF | NFC_SR_AWB | NFC_SR_ASE;
  1391. u32 nfc_status = nfc_readl(host->nfc->hsmc_regs, SR);
  1392. if (unlikely(nfc_status & err_flags)) {
  1393. if (nfc_status & NFC_SR_DTOE)
  1394. dev_err(host->dev, "NFC: Waiting Nand R/B Timeout Error\n");
  1395. else if (nfc_status & NFC_SR_UNDEF)
  1396. dev_err(host->dev, "NFC: Access Undefined Area Error\n");
  1397. else if (nfc_status & NFC_SR_AWB)
  1398. dev_err(host->dev, "NFC: Access memory While NFC is busy\n");
  1399. else if (nfc_status & NFC_SR_ASE)
  1400. dev_err(host->dev, "NFC: Access memory Size Error\n");
  1401. }
  1402. return nfc_status;
  1403. }
  1404. /* SMC interrupt service routine */
  1405. static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
  1406. {
  1407. struct atmel_nand_host *host = dev_id;
  1408. u32 status, mask, pending;
  1409. irqreturn_t ret = IRQ_NONE;
  1410. status = nfc_read_status(host);
  1411. mask = nfc_readl(host->nfc->hsmc_regs, IMR);
  1412. pending = status & mask;
  1413. if (pending & NFC_SR_XFR_DONE) {
  1414. complete(&host->nfc->comp_xfer_done);
  1415. nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
  1416. ret = IRQ_HANDLED;
  1417. }
  1418. if (pending & NFC_SR_RB_EDGE) {
  1419. complete(&host->nfc->comp_ready);
  1420. nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_RB_EDGE);
  1421. ret = IRQ_HANDLED;
  1422. }
  1423. if (pending & NFC_SR_CMD_DONE) {
  1424. complete(&host->nfc->comp_cmd_done);
  1425. nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
  1426. ret = IRQ_HANDLED;
  1427. }
  1428. return ret;
  1429. }
  1430. /* NFC(Nand Flash Controller) related functions */
  1431. static void nfc_prepare_interrupt(struct atmel_nand_host *host, u32 flag)
  1432. {
  1433. if (flag & NFC_SR_XFR_DONE)
  1434. init_completion(&host->nfc->comp_xfer_done);
  1435. if (flag & NFC_SR_RB_EDGE)
  1436. init_completion(&host->nfc->comp_ready);
  1437. if (flag & NFC_SR_CMD_DONE)
  1438. init_completion(&host->nfc->comp_cmd_done);
  1439. /* Enable interrupt that need to wait for */
  1440. nfc_writel(host->nfc->hsmc_regs, IER, flag);
  1441. }
  1442. static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
  1443. {
  1444. int i, index = 0;
  1445. struct completion *comp[3]; /* Support 3 interrupt completion */
  1446. if (flag & NFC_SR_XFR_DONE)
  1447. comp[index++] = &host->nfc->comp_xfer_done;
  1448. if (flag & NFC_SR_RB_EDGE)
  1449. comp[index++] = &host->nfc->comp_ready;
  1450. if (flag & NFC_SR_CMD_DONE)
  1451. comp[index++] = &host->nfc->comp_cmd_done;
  1452. if (index == 0) {
  1453. dev_err(host->dev, "Unkown interrupt flag: 0x%08x\n", flag);
  1454. return -EINVAL;
  1455. }
  1456. for (i = 0; i < index; i++) {
  1457. if (wait_for_completion_timeout(comp[i],
  1458. msecs_to_jiffies(NFC_TIME_OUT_MS)))
  1459. continue; /* wait for next completion */
  1460. else
  1461. goto err_timeout;
  1462. }
  1463. return 0;
  1464. err_timeout:
  1465. dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
  1466. /* Disable the interrupt as it is not handled by interrupt handler */
  1467. nfc_writel(host->nfc->hsmc_regs, IDR, flag);
  1468. return -ETIMEDOUT;
  1469. }
  1470. static int nfc_send_command(struct atmel_nand_host *host,
  1471. unsigned int cmd, unsigned int addr, unsigned char cycle0)
  1472. {
  1473. unsigned long timeout;
  1474. u32 flag = NFC_SR_CMD_DONE;
  1475. flag |= cmd & NFCADDR_CMD_DATAEN ? NFC_SR_XFR_DONE : 0;
  1476. dev_dbg(host->dev,
  1477. "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
  1478. cmd, addr, cycle0);
  1479. timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
  1480. while (nfc_cmd_readl(NFCADDR_CMD_NFCBUSY, host->nfc->base_cmd_regs)
  1481. & NFCADDR_CMD_NFCBUSY) {
  1482. if (time_after(jiffies, timeout)) {
  1483. dev_err(host->dev,
  1484. "Time out to wait CMD_NFCBUSY ready!\n");
  1485. return -ETIMEDOUT;
  1486. }
  1487. }
  1488. nfc_prepare_interrupt(host, flag);
  1489. nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
  1490. nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
  1491. return nfc_wait_interrupt(host, flag);
  1492. }
  1493. static int nfc_device_ready(struct mtd_info *mtd)
  1494. {
  1495. u32 status, mask;
  1496. struct nand_chip *nand_chip = mtd->priv;
  1497. struct atmel_nand_host *host = nand_chip->priv;
  1498. status = nfc_read_status(host);
  1499. mask = nfc_readl(host->nfc->hsmc_regs, IMR);
  1500. /* The mask should be 0. If not we may lost interrupts */
  1501. if (unlikely(mask & status))
  1502. dev_err(host->dev, "Lost the interrupt flags: 0x%08x\n",
  1503. mask & status);
  1504. return status & NFC_SR_RB_EDGE;
  1505. }
  1506. static void nfc_select_chip(struct mtd_info *mtd, int chip)
  1507. {
  1508. struct nand_chip *nand_chip = mtd->priv;
  1509. struct atmel_nand_host *host = nand_chip->priv;
  1510. if (chip == -1)
  1511. nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE);
  1512. else
  1513. nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE);
  1514. }
  1515. static int nfc_make_addr(struct mtd_info *mtd, int command, int column,
  1516. int page_addr, unsigned int *addr1234, unsigned int *cycle0)
  1517. {
  1518. struct nand_chip *chip = mtd->priv;
  1519. int acycle = 0;
  1520. unsigned char addr_bytes[8];
  1521. int index = 0, bit_shift;
  1522. BUG_ON(addr1234 == NULL || cycle0 == NULL);
  1523. *cycle0 = 0;
  1524. *addr1234 = 0;
  1525. if (column != -1) {
  1526. if (chip->options & NAND_BUSWIDTH_16 &&
  1527. !nand_opcode_8bits(command))
  1528. column >>= 1;
  1529. addr_bytes[acycle++] = column & 0xff;
  1530. if (mtd->writesize > 512)
  1531. addr_bytes[acycle++] = (column >> 8) & 0xff;
  1532. }
  1533. if (page_addr != -1) {
  1534. addr_bytes[acycle++] = page_addr & 0xff;
  1535. addr_bytes[acycle++] = (page_addr >> 8) & 0xff;
  1536. if (chip->chipsize > (128 << 20))
  1537. addr_bytes[acycle++] = (page_addr >> 16) & 0xff;
  1538. }
  1539. if (acycle > 4)
  1540. *cycle0 = addr_bytes[index++];
  1541. for (bit_shift = 0; index < acycle; bit_shift += 8)
  1542. *addr1234 += addr_bytes[index++] << bit_shift;
  1543. /* return acycle in cmd register */
  1544. return acycle << NFCADDR_CMD_ACYCLE_BIT_POS;
  1545. }
  1546. static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
  1547. int column, int page_addr)
  1548. {
  1549. struct nand_chip *chip = mtd->priv;
  1550. struct atmel_nand_host *host = chip->priv;
  1551. unsigned long timeout;
  1552. unsigned int nfc_addr_cmd = 0;
  1553. unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
  1554. /* Set default settings: no cmd2, no addr cycle. read from nand */
  1555. unsigned int cmd2 = 0;
  1556. unsigned int vcmd2 = 0;
  1557. int acycle = NFCADDR_CMD_ACYCLE_NONE;
  1558. int csid = NFCADDR_CMD_CSID_3;
  1559. int dataen = NFCADDR_CMD_DATADIS;
  1560. int nfcwr = NFCADDR_CMD_NFCRD;
  1561. unsigned int addr1234 = 0;
  1562. unsigned int cycle0 = 0;
  1563. bool do_addr = true;
  1564. host->nfc->data_in_sram = NULL;
  1565. dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
  1566. __func__, command, column, page_addr);
  1567. switch (command) {
  1568. case NAND_CMD_RESET:
  1569. nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr;
  1570. nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
  1571. udelay(chip->chip_delay);
  1572. nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
  1573. timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
  1574. while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) {
  1575. if (time_after(jiffies, timeout)) {
  1576. dev_err(host->dev,
  1577. "Time out to wait status ready!\n");
  1578. break;
  1579. }
  1580. }
  1581. return;
  1582. case NAND_CMD_STATUS:
  1583. do_addr = false;
  1584. break;
  1585. case NAND_CMD_PARAM:
  1586. case NAND_CMD_READID:
  1587. do_addr = false;
  1588. acycle = NFCADDR_CMD_ACYCLE_1;
  1589. if (column != -1)
  1590. addr1234 = column;
  1591. break;
  1592. case NAND_CMD_RNDOUT:
  1593. cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS;
  1594. vcmd2 = NFCADDR_CMD_VCMD2;
  1595. break;
  1596. case NAND_CMD_READ0:
  1597. case NAND_CMD_READOOB:
  1598. if (command == NAND_CMD_READOOB) {
  1599. column += mtd->writesize;
  1600. command = NAND_CMD_READ0; /* only READ0 is valid */
  1601. cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
  1602. }
  1603. if (host->nfc->use_nfc_sram) {
  1604. /* Enable Data transfer to sram */
  1605. dataen = NFCADDR_CMD_DATAEN;
  1606. /* Need enable PMECC now, since NFC will transfer
  1607. * data in bus after sending nfc read command.
  1608. */
  1609. if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
  1610. pmecc_enable(host, NAND_ECC_READ);
  1611. }
  1612. cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS;
  1613. vcmd2 = NFCADDR_CMD_VCMD2;
  1614. break;
  1615. /* For prgramming command, the cmd need set to write enable */
  1616. case NAND_CMD_PAGEPROG:
  1617. case NAND_CMD_SEQIN:
  1618. case NAND_CMD_RNDIN:
  1619. nfcwr = NFCADDR_CMD_NFCWR;
  1620. if (host->nfc->will_write_sram && command == NAND_CMD_SEQIN)
  1621. dataen = NFCADDR_CMD_DATAEN;
  1622. break;
  1623. default:
  1624. break;
  1625. }
  1626. if (do_addr)
  1627. acycle = nfc_make_addr(mtd, command, column, page_addr,
  1628. &addr1234, &cycle0);
  1629. nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
  1630. nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
  1631. /*
  1632. * Program and erase have their own busy handlers status, sequential
  1633. * in, and deplete1 need no delay.
  1634. */
  1635. switch (command) {
  1636. case NAND_CMD_CACHEDPROG:
  1637. case NAND_CMD_PAGEPROG:
  1638. case NAND_CMD_ERASE1:
  1639. case NAND_CMD_ERASE2:
  1640. case NAND_CMD_RNDIN:
  1641. case NAND_CMD_STATUS:
  1642. case NAND_CMD_RNDOUT:
  1643. case NAND_CMD_SEQIN:
  1644. case NAND_CMD_READID:
  1645. return;
  1646. case NAND_CMD_READ0:
  1647. if (dataen == NFCADDR_CMD_DATAEN) {
  1648. host->nfc->data_in_sram = host->nfc->sram_bank0 +
  1649. nfc_get_sram_off(host);
  1650. return;
  1651. }
  1652. /* fall through */
  1653. default:
  1654. nfc_prepare_interrupt(host, NFC_SR_RB_EDGE);
  1655. nfc_wait_interrupt(host, NFC_SR_RB_EDGE);
  1656. }
  1657. }
  1658. static int nfc_sram_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1659. uint32_t offset, int data_len, const uint8_t *buf,
  1660. int oob_required, int page, int cached, int raw)
  1661. {
  1662. int cfg, len;
  1663. int status = 0;
  1664. struct atmel_nand_host *host = chip->priv;
  1665. void *sram = host->nfc->sram_bank0 + nfc_get_sram_off(host);
  1666. /* Subpage write is not supported */
  1667. if (offset || (data_len < mtd->writesize))
  1668. return -EINVAL;
  1669. len = mtd->writesize;
  1670. /* Copy page data to sram that will write to nand via NFC */
  1671. if (use_dma) {
  1672. if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) != 0)
  1673. /* Fall back to use cpu copy */
  1674. memcpy(sram, buf, len);
  1675. } else {
  1676. memcpy(sram, buf, len);
  1677. }
  1678. cfg = nfc_readl(host->nfc->hsmc_regs, CFG);
  1679. if (unlikely(raw) && oob_required) {
  1680. memcpy(sram + len, chip->oob_poi, mtd->oobsize);
  1681. len += mtd->oobsize;
  1682. nfc_writel(host->nfc->hsmc_regs, CFG, cfg | NFC_CFG_WSPARE);
  1683. } else {
  1684. nfc_writel(host->nfc->hsmc_regs, CFG, cfg & ~NFC_CFG_WSPARE);
  1685. }
  1686. if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
  1687. /*
  1688. * When use NFC sram, need set up PMECC before send
  1689. * NAND_CMD_SEQIN command. Since when the nand command
  1690. * is sent, nfc will do transfer from sram and nand.
  1691. */
  1692. pmecc_enable(host, NAND_ECC_WRITE);
  1693. host->nfc->will_write_sram = true;
  1694. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1695. host->nfc->will_write_sram = false;
  1696. if (likely(!raw))
  1697. /* Need to write ecc into oob */
  1698. status = chip->ecc.write_page(mtd, chip, buf, oob_required);
  1699. if (status < 0)
  1700. return status;
  1701. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1702. status = chip->waitfunc(mtd, chip);
  1703. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1704. status = chip->errstat(mtd, chip, FL_WRITING, status, page);
  1705. if (status & NAND_STATUS_FAIL)
  1706. return -EIO;
  1707. return 0;
  1708. }
  1709. static int nfc_sram_init(struct mtd_info *mtd)
  1710. {
  1711. struct nand_chip *chip = mtd->priv;
  1712. struct atmel_nand_host *host = chip->priv;
  1713. int res = 0;
  1714. /* Initialize the NFC CFG register */
  1715. unsigned int cfg_nfc = 0;
  1716. /* set page size and oob layout */
  1717. switch (mtd->writesize) {
  1718. case 512:
  1719. cfg_nfc = NFC_CFG_PAGESIZE_512;
  1720. break;
  1721. case 1024:
  1722. cfg_nfc = NFC_CFG_PAGESIZE_1024;
  1723. break;
  1724. case 2048:
  1725. cfg_nfc = NFC_CFG_PAGESIZE_2048;
  1726. break;
  1727. case 4096:
  1728. cfg_nfc = NFC_CFG_PAGESIZE_4096;
  1729. break;
  1730. case 8192:
  1731. cfg_nfc = NFC_CFG_PAGESIZE_8192;
  1732. break;
  1733. default:
  1734. dev_err(host->dev, "Unsupported page size for NFC.\n");
  1735. res = -ENXIO;
  1736. return res;
  1737. }
  1738. /* oob bytes size = (NFCSPARESIZE + 1) * 4
  1739. * Max support spare size is 512 bytes. */
  1740. cfg_nfc |= (((mtd->oobsize / 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
  1741. & NFC_CFG_NFC_SPARESIZE);
  1742. /* default set a max timeout */
  1743. cfg_nfc |= NFC_CFG_RSPARE |
  1744. NFC_CFG_NFC_DTOCYC | NFC_CFG_NFC_DTOMUL;
  1745. nfc_writel(host->nfc->hsmc_regs, CFG, cfg_nfc);
  1746. host->nfc->will_write_sram = false;
  1747. nfc_set_sram_bank(host, 0);
  1748. /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
  1749. if (host->nfc->write_by_sram) {
  1750. if ((chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) ||
  1751. chip->ecc.mode == NAND_ECC_NONE)
  1752. chip->write_page = nfc_sram_write_page;
  1753. else
  1754. host->nfc->write_by_sram = false;
  1755. }
  1756. dev_info(host->dev, "Using NFC Sram read %s\n",
  1757. host->nfc->write_by_sram ? "and write" : "");
  1758. return 0;
  1759. }
  1760. static struct platform_driver atmel_nand_nfc_driver;
  1761. /*
  1762. * Probe for the NAND device.
  1763. */
  1764. static int atmel_nand_probe(struct platform_device *pdev)
  1765. {
  1766. struct atmel_nand_host *host;
  1767. struct mtd_info *mtd;
  1768. struct nand_chip *nand_chip;
  1769. struct resource *mem;
  1770. struct mtd_part_parser_data ppdata = {};
  1771. int res, irq;
  1772. /* Allocate memory for the device structure (and zero it) */
  1773. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  1774. if (!host)
  1775. return -ENOMEM;
  1776. res = platform_driver_register(&atmel_nand_nfc_driver);
  1777. if (res)
  1778. dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
  1779. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1780. host->io_base = devm_ioremap_resource(&pdev->dev, mem);
  1781. if (IS_ERR(host->io_base)) {
  1782. res = PTR_ERR(host->io_base);
  1783. goto err_nand_ioremap;
  1784. }
  1785. host->io_phys = (dma_addr_t)mem->start;
  1786. mtd = &host->mtd;
  1787. nand_chip = &host->nand_chip;
  1788. host->dev = &pdev->dev;
  1789. if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
  1790. /* Only when CONFIG_OF is enabled of_node can be parsed */
  1791. res = atmel_of_init_port(host, pdev->dev.of_node);
  1792. if (res)
  1793. goto err_nand_ioremap;
  1794. } else {
  1795. memcpy(&host->board, dev_get_platdata(&pdev->dev),
  1796. sizeof(struct atmel_nand_data));
  1797. }
  1798. nand_chip->priv = host; /* link the private data structures */
  1799. mtd->priv = nand_chip;
  1800. mtd->owner = THIS_MODULE;
  1801. /* Set address of NAND IO lines */
  1802. nand_chip->IO_ADDR_R = host->io_base;
  1803. nand_chip->IO_ADDR_W = host->io_base;
  1804. if (nand_nfc.is_initialized) {
  1805. /* NFC driver is probed and initialized */
  1806. host->nfc = &nand_nfc;
  1807. nand_chip->select_chip = nfc_select_chip;
  1808. nand_chip->dev_ready = nfc_device_ready;
  1809. nand_chip->cmdfunc = nfc_nand_command;
  1810. /* Initialize the interrupt for NFC */
  1811. irq = platform_get_irq(pdev, 0);
  1812. if (irq < 0) {
  1813. dev_err(host->dev, "Cannot get HSMC irq!\n");
  1814. res = irq;
  1815. goto err_nand_ioremap;
  1816. }
  1817. res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
  1818. 0, "hsmc", host);
  1819. if (res) {
  1820. dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
  1821. irq);
  1822. goto err_nand_ioremap;
  1823. }
  1824. } else {
  1825. res = atmel_nand_set_enable_ready_pins(mtd);
  1826. if (res)
  1827. goto err_nand_ioremap;
  1828. nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
  1829. }
  1830. nand_chip->ecc.mode = host->board.ecc_mode;
  1831. nand_chip->chip_delay = 40; /* 40us command delay time */
  1832. if (host->board.bus_width_16) /* 16-bit bus width */
  1833. nand_chip->options |= NAND_BUSWIDTH_16;
  1834. nand_chip->read_buf = atmel_read_buf;
  1835. nand_chip->write_buf = atmel_write_buf;
  1836. platform_set_drvdata(pdev, host);
  1837. atmel_nand_enable(host);
  1838. if (gpio_is_valid(host->board.det_pin)) {
  1839. res = devm_gpio_request(&pdev->dev,
  1840. host->board.det_pin, "nand_det");
  1841. if (res < 0) {
  1842. dev_err(&pdev->dev,
  1843. "can't request det gpio %d\n",
  1844. host->board.det_pin);
  1845. goto err_no_card;
  1846. }
  1847. res = gpio_direction_input(host->board.det_pin);
  1848. if (res < 0) {
  1849. dev_err(&pdev->dev,
  1850. "can't request input direction det gpio %d\n",
  1851. host->board.det_pin);
  1852. goto err_no_card;
  1853. }
  1854. if (gpio_get_value(host->board.det_pin)) {
  1855. dev_info(&pdev->dev, "No SmartMedia card inserted.\n");
  1856. res = -ENXIO;
  1857. goto err_no_card;
  1858. }
  1859. }
  1860. if (host->board.on_flash_bbt || on_flash_bbt) {
  1861. dev_info(&pdev->dev, "Use On Flash BBT\n");
  1862. nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
  1863. }
  1864. if (!host->board.has_dma)
  1865. use_dma = 0;
  1866. if (use_dma) {
  1867. dma_cap_mask_t mask;
  1868. dma_cap_zero(mask);
  1869. dma_cap_set(DMA_MEMCPY, mask);
  1870. host->dma_chan = dma_request_channel(mask, NULL, NULL);
  1871. if (!host->dma_chan) {
  1872. dev_err(host->dev, "Failed to request DMA channel\n");
  1873. use_dma = 0;
  1874. }
  1875. }
  1876. if (use_dma)
  1877. dev_info(host->dev, "Using %s for DMA transfers.\n",
  1878. dma_chan_name(host->dma_chan));
  1879. else
  1880. dev_info(host->dev, "No DMA support for NAND access.\n");
  1881. /* first scan to find the device and get the page size */
  1882. if (nand_scan_ident(mtd, 1, NULL)) {
  1883. res = -ENXIO;
  1884. goto err_scan_ident;
  1885. }
  1886. if (nand_chip->ecc.mode == NAND_ECC_HW) {
  1887. if (host->has_pmecc)
  1888. res = atmel_pmecc_nand_init_params(pdev, host);
  1889. else
  1890. res = atmel_hw_nand_init_params(pdev, host);
  1891. if (res != 0)
  1892. goto err_hw_ecc;
  1893. }
  1894. /* initialize the nfc configuration register */
  1895. if (host->nfc && host->nfc->use_nfc_sram) {
  1896. res = nfc_sram_init(mtd);
  1897. if (res) {
  1898. host->nfc->use_nfc_sram = false;
  1899. dev_err(host->dev, "Disable use nfc sram for data transfer.\n");
  1900. }
  1901. }
  1902. /* second phase scan */
  1903. if (nand_scan_tail(mtd)) {
  1904. res = -ENXIO;
  1905. goto err_scan_tail;
  1906. }
  1907. mtd->name = "atmel_nand";
  1908. ppdata.of_node = pdev->dev.of_node;
  1909. res = mtd_device_parse_register(mtd, NULL, &ppdata,
  1910. host->board.parts, host->board.num_parts);
  1911. if (!res)
  1912. return res;
  1913. err_scan_tail:
  1914. if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
  1915. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  1916. err_hw_ecc:
  1917. err_scan_ident:
  1918. err_no_card:
  1919. atmel_nand_disable(host);
  1920. if (host->dma_chan)
  1921. dma_release_channel(host->dma_chan);
  1922. err_nand_ioremap:
  1923. return res;
  1924. }
  1925. /*
  1926. * Remove a NAND device.
  1927. */
  1928. static int atmel_nand_remove(struct platform_device *pdev)
  1929. {
  1930. struct atmel_nand_host *host = platform_get_drvdata(pdev);
  1931. struct mtd_info *mtd = &host->mtd;
  1932. nand_release(mtd);
  1933. atmel_nand_disable(host);
  1934. if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
  1935. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  1936. pmerrloc_writel(host->pmerrloc_base, ELDIS,
  1937. PMERRLOC_DISABLE);
  1938. }
  1939. if (host->dma_chan)
  1940. dma_release_channel(host->dma_chan);
  1941. platform_driver_unregister(&atmel_nand_nfc_driver);
  1942. return 0;
  1943. }
  1944. static struct atmel_nand_caps at91rm9200_caps = {
  1945. .pmecc_correct_erase_page = false,
  1946. };
  1947. static struct atmel_nand_caps sama5d4_caps = {
  1948. .pmecc_correct_erase_page = true,
  1949. };
  1950. static const struct of_device_id atmel_nand_dt_ids[] = {
  1951. { .compatible = "atmel,at91rm9200-nand", .data = &at91rm9200_caps },
  1952. { .compatible = "atmel,sama5d4-nand", .data = &sama5d4_caps },
  1953. { /* sentinel */ }
  1954. };
  1955. MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
  1956. static int atmel_nand_nfc_probe(struct platform_device *pdev)
  1957. {
  1958. struct atmel_nfc *nfc = &nand_nfc;
  1959. struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
  1960. int ret;
  1961. nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1962. nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
  1963. if (IS_ERR(nfc->base_cmd_regs))
  1964. return PTR_ERR(nfc->base_cmd_regs);
  1965. nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1966. nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
  1967. if (IS_ERR(nfc->hsmc_regs))
  1968. return PTR_ERR(nfc->hsmc_regs);
  1969. nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  1970. if (nfc_sram) {
  1971. nfc->sram_bank0 = (void * __force)
  1972. devm_ioremap_resource(&pdev->dev, nfc_sram);
  1973. if (IS_ERR(nfc->sram_bank0)) {
  1974. dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
  1975. PTR_ERR(nfc->sram_bank0));
  1976. } else {
  1977. nfc->use_nfc_sram = true;
  1978. nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
  1979. if (pdev->dev.of_node)
  1980. nfc->write_by_sram = of_property_read_bool(
  1981. pdev->dev.of_node,
  1982. "atmel,write-by-sram");
  1983. }
  1984. }
  1985. nfc_writel(nfc->hsmc_regs, IDR, 0xffffffff);
  1986. nfc_readl(nfc->hsmc_regs, SR); /* clear the NFC_SR */
  1987. nfc->clk = devm_clk_get(&pdev->dev, NULL);
  1988. if (!IS_ERR(nfc->clk)) {
  1989. ret = clk_prepare_enable(nfc->clk);
  1990. if (ret)
  1991. return ret;
  1992. } else {
  1993. dev_warn(&pdev->dev, "NFC clock missing, update your Device Tree");
  1994. }
  1995. nfc->is_initialized = true;
  1996. dev_info(&pdev->dev, "NFC is probed.\n");
  1997. return 0;
  1998. }
  1999. static int atmel_nand_nfc_remove(struct platform_device *pdev)
  2000. {
  2001. struct atmel_nfc *nfc = &nand_nfc;
  2002. if (!IS_ERR(nfc->clk))
  2003. clk_disable_unprepare(nfc->clk);
  2004. return 0;
  2005. }
  2006. static const struct of_device_id atmel_nand_nfc_match[] = {
  2007. { .compatible = "atmel,sama5d3-nfc" },
  2008. { /* sentinel */ }
  2009. };
  2010. MODULE_DEVICE_TABLE(of, atmel_nand_nfc_match);
  2011. static struct platform_driver atmel_nand_nfc_driver = {
  2012. .driver = {
  2013. .name = "atmel_nand_nfc",
  2014. .of_match_table = of_match_ptr(atmel_nand_nfc_match),
  2015. },
  2016. .probe = atmel_nand_nfc_probe,
  2017. .remove = atmel_nand_nfc_remove,
  2018. };
  2019. static struct platform_driver atmel_nand_driver = {
  2020. .probe = atmel_nand_probe,
  2021. .remove = atmel_nand_remove,
  2022. .driver = {
  2023. .name = "atmel_nand",
  2024. .of_match_table = of_match_ptr(atmel_nand_dt_ids),
  2025. },
  2026. };
  2027. module_platform_driver(atmel_nand_driver);
  2028. MODULE_LICENSE("GPL");
  2029. MODULE_AUTHOR("Rick Bronson");
  2030. MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
  2031. MODULE_ALIAS("platform:atmel_nand");