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 = PMECC_OOB_RESERVED_BYTES;
  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. if (!host->has_no_lookup_table) {
  1019. regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
  1020. host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev,
  1021. regs_rom);
  1022. if (IS_ERR(host->pmecc_rom_base)) {
  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. }
  1027. if (host->has_no_lookup_table) {
  1028. /* Build the look-up table in runtime */
  1029. galois_table = create_lookup_table(host->dev, sector_size);
  1030. if (!galois_table) {
  1031. dev_err(host->dev, "Failed to build a lookup table in runtime!\n");
  1032. err_no = -EINVAL;
  1033. goto err;
  1034. }
  1035. host->pmecc_rom_base = (void __iomem *)galois_table;
  1036. host->pmecc_lookup_table_offset = 0;
  1037. }
  1038. nand_chip->ecc.size = sector_size;
  1039. /* set ECC page size and oob layout */
  1040. switch (mtd->writesize) {
  1041. case 512:
  1042. case 1024:
  1043. case 2048:
  1044. case 4096:
  1045. case 8192:
  1046. if (sector_size > mtd->writesize) {
  1047. dev_err(host->dev, "pmecc sector size is bigger than the page size!\n");
  1048. err_no = -EINVAL;
  1049. goto err;
  1050. }
  1051. host->pmecc_degree = (sector_size == 512) ?
  1052. PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
  1053. host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
  1054. host->pmecc_alpha_to = pmecc_get_alpha_to(host);
  1055. host->pmecc_index_of = host->pmecc_rom_base +
  1056. host->pmecc_lookup_table_offset;
  1057. nand_chip->ecc.strength = cap;
  1058. nand_chip->ecc.bytes = pmecc_get_ecc_bytes(cap, sector_size);
  1059. nand_chip->ecc.steps = mtd->writesize / sector_size;
  1060. nand_chip->ecc.total = nand_chip->ecc.bytes *
  1061. nand_chip->ecc.steps;
  1062. if (nand_chip->ecc.total >
  1063. mtd->oobsize - PMECC_OOB_RESERVED_BYTES) {
  1064. dev_err(host->dev, "No room for ECC bytes\n");
  1065. err_no = -EINVAL;
  1066. goto err;
  1067. }
  1068. pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
  1069. mtd->oobsize,
  1070. nand_chip->ecc.total);
  1071. nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
  1072. break;
  1073. default:
  1074. dev_warn(host->dev,
  1075. "Unsupported page size for PMECC, use Software ECC\n");
  1076. /* page size not handled by HW ECC */
  1077. /* switching back to soft ECC */
  1078. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1079. return 0;
  1080. }
  1081. /* Allocate data for PMECC computation */
  1082. err_no = pmecc_data_alloc(host);
  1083. if (err_no) {
  1084. dev_err(host->dev,
  1085. "Cannot allocate memory for PMECC computation!\n");
  1086. goto err;
  1087. }
  1088. nand_chip->options |= NAND_NO_SUBPAGE_WRITE;
  1089. nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
  1090. nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
  1091. atmel_pmecc_core_init(mtd);
  1092. return 0;
  1093. err:
  1094. return err_no;
  1095. }
  1096. /*
  1097. * Calculate HW ECC
  1098. *
  1099. * function called after a write
  1100. *
  1101. * mtd: MTD block structure
  1102. * dat: raw data (unused)
  1103. * ecc_code: buffer for ECC
  1104. */
  1105. static int atmel_nand_calculate(struct mtd_info *mtd,
  1106. const u_char *dat, unsigned char *ecc_code)
  1107. {
  1108. struct nand_chip *nand_chip = mtd->priv;
  1109. struct atmel_nand_host *host = nand_chip->priv;
  1110. unsigned int ecc_value;
  1111. /* get the first 2 ECC bytes */
  1112. ecc_value = ecc_readl(host->ecc, PR);
  1113. ecc_code[0] = ecc_value & 0xFF;
  1114. ecc_code[1] = (ecc_value >> 8) & 0xFF;
  1115. /* get the last 2 ECC bytes */
  1116. ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
  1117. ecc_code[2] = ecc_value & 0xFF;
  1118. ecc_code[3] = (ecc_value >> 8) & 0xFF;
  1119. return 0;
  1120. }
  1121. /*
  1122. * HW ECC read page function
  1123. *
  1124. * mtd: mtd info structure
  1125. * chip: nand chip info structure
  1126. * buf: buffer to store read data
  1127. * oob_required: caller expects OOB data read to chip->oob_poi
  1128. */
  1129. static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  1130. uint8_t *buf, int oob_required, int page)
  1131. {
  1132. int eccsize = chip->ecc.size;
  1133. int eccbytes = chip->ecc.bytes;
  1134. uint32_t *eccpos = chip->ecc.layout->eccpos;
  1135. uint8_t *p = buf;
  1136. uint8_t *oob = chip->oob_poi;
  1137. uint8_t *ecc_pos;
  1138. int stat;
  1139. unsigned int max_bitflips = 0;
  1140. /*
  1141. * Errata: ALE is incorrectly wired up to the ECC controller
  1142. * on the AP7000, so it will include the address cycles in the
  1143. * ECC calculation.
  1144. *
  1145. * Workaround: Reset the parity registers before reading the
  1146. * actual data.
  1147. */
  1148. struct atmel_nand_host *host = chip->priv;
  1149. if (host->board.need_reset_workaround)
  1150. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  1151. /* read the page */
  1152. chip->read_buf(mtd, p, eccsize);
  1153. /* move to ECC position if needed */
  1154. if (eccpos[0] != 0) {
  1155. /* This only works on large pages
  1156. * because the ECC controller waits for
  1157. * NAND_CMD_RNDOUTSTART after the
  1158. * NAND_CMD_RNDOUT.
  1159. * anyway, for small pages, the eccpos[0] == 0
  1160. */
  1161. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  1162. mtd->writesize + eccpos[0], -1);
  1163. }
  1164. /* the ECC controller needs to read the ECC just after the data */
  1165. ecc_pos = oob + eccpos[0];
  1166. chip->read_buf(mtd, ecc_pos, eccbytes);
  1167. /* check if there's an error */
  1168. stat = chip->ecc.correct(mtd, p, oob, NULL);
  1169. if (stat < 0) {
  1170. mtd->ecc_stats.failed++;
  1171. } else {
  1172. mtd->ecc_stats.corrected += stat;
  1173. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1174. }
  1175. /* get back to oob start (end of page) */
  1176. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  1177. /* read the oob */
  1178. chip->read_buf(mtd, oob, mtd->oobsize);
  1179. return max_bitflips;
  1180. }
  1181. /*
  1182. * HW ECC Correction
  1183. *
  1184. * function called after a read
  1185. *
  1186. * mtd: MTD block structure
  1187. * dat: raw data read from the chip
  1188. * read_ecc: ECC from the chip (unused)
  1189. * isnull: unused
  1190. *
  1191. * Detect and correct a 1 bit error for a page
  1192. */
  1193. static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
  1194. u_char *read_ecc, u_char *isnull)
  1195. {
  1196. struct nand_chip *nand_chip = mtd->priv;
  1197. struct atmel_nand_host *host = nand_chip->priv;
  1198. unsigned int ecc_status;
  1199. unsigned int ecc_word, ecc_bit;
  1200. /* get the status from the Status Register */
  1201. ecc_status = ecc_readl(host->ecc, SR);
  1202. /* if there's no error */
  1203. if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
  1204. return 0;
  1205. /* get error bit offset (4 bits) */
  1206. ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
  1207. /* get word address (12 bits) */
  1208. ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
  1209. ecc_word >>= 4;
  1210. /* if there are multiple errors */
  1211. if (ecc_status & ATMEL_ECC_MULERR) {
  1212. /* check if it is a freshly erased block
  1213. * (filled with 0xff) */
  1214. if ((ecc_bit == ATMEL_ECC_BITADDR)
  1215. && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
  1216. /* the block has just been erased, return OK */
  1217. return 0;
  1218. }
  1219. /* it doesn't seems to be a freshly
  1220. * erased block.
  1221. * We can't correct so many errors */
  1222. dev_dbg(host->dev, "atmel_nand : multiple errors detected."
  1223. " Unable to correct.\n");
  1224. return -EIO;
  1225. }
  1226. /* if there's a single bit error : we can correct it */
  1227. if (ecc_status & ATMEL_ECC_ECCERR) {
  1228. /* there's nothing much to do here.
  1229. * the bit error is on the ECC itself.
  1230. */
  1231. dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
  1232. " Nothing to correct\n");
  1233. return 0;
  1234. }
  1235. dev_dbg(host->dev, "atmel_nand : one bit error on data."
  1236. " (word offset in the page :"
  1237. " 0x%x bit offset : 0x%x)\n",
  1238. ecc_word, ecc_bit);
  1239. /* correct the error */
  1240. if (nand_chip->options & NAND_BUSWIDTH_16) {
  1241. /* 16 bits words */
  1242. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  1243. } else {
  1244. /* 8 bits words */
  1245. dat[ecc_word] ^= (1 << ecc_bit);
  1246. }
  1247. dev_dbg(host->dev, "atmel_nand : error corrected\n");
  1248. return 1;
  1249. }
  1250. /*
  1251. * Enable HW ECC : unused on most chips
  1252. */
  1253. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  1254. {
  1255. struct nand_chip *nand_chip = mtd->priv;
  1256. struct atmel_nand_host *host = nand_chip->priv;
  1257. if (host->board.need_reset_workaround)
  1258. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  1259. }
  1260. static const struct of_device_id atmel_nand_dt_ids[];
  1261. static int atmel_of_init_port(struct atmel_nand_host *host,
  1262. struct device_node *np)
  1263. {
  1264. u32 val;
  1265. u32 offset[2];
  1266. int ecc_mode;
  1267. struct atmel_nand_data *board = &host->board;
  1268. enum of_gpio_flags flags = 0;
  1269. host->caps = (struct atmel_nand_caps *)
  1270. of_match_device(atmel_nand_dt_ids, host->dev)->data;
  1271. if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
  1272. if (val >= 32) {
  1273. dev_err(host->dev, "invalid addr-offset %u\n", val);
  1274. return -EINVAL;
  1275. }
  1276. board->ale = val;
  1277. }
  1278. if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
  1279. if (val >= 32) {
  1280. dev_err(host->dev, "invalid cmd-offset %u\n", val);
  1281. return -EINVAL;
  1282. }
  1283. board->cle = val;
  1284. }
  1285. ecc_mode = of_get_nand_ecc_mode(np);
  1286. board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
  1287. board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
  1288. board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
  1289. if (of_get_nand_bus_width(np) == 16)
  1290. board->bus_width_16 = 1;
  1291. board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
  1292. board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
  1293. board->enable_pin = of_get_gpio(np, 1);
  1294. board->det_pin = of_get_gpio(np, 2);
  1295. host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
  1296. /* load the nfc driver if there is */
  1297. of_platform_populate(np, NULL, NULL, host->dev);
  1298. if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
  1299. return 0; /* Not using PMECC */
  1300. /* use PMECC, get correction capability, sector size and lookup
  1301. * table offset.
  1302. * If correction bits and sector size are not specified, then find
  1303. * them from NAND ONFI parameters.
  1304. */
  1305. if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
  1306. if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
  1307. (val != 24)) {
  1308. dev_err(host->dev,
  1309. "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
  1310. val);
  1311. return -EINVAL;
  1312. }
  1313. host->pmecc_corr_cap = (u8)val;
  1314. }
  1315. if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
  1316. if ((val != 512) && (val != 1024)) {
  1317. dev_err(host->dev,
  1318. "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
  1319. val);
  1320. return -EINVAL;
  1321. }
  1322. host->pmecc_sector_size = (u16)val;
  1323. }
  1324. if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
  1325. offset, 2) != 0) {
  1326. dev_err(host->dev, "Cannot get PMECC lookup table offset, will build a lookup table in runtime.\n");
  1327. host->has_no_lookup_table = true;
  1328. /* Will build a lookup table and initialize the offset later */
  1329. return 0;
  1330. }
  1331. if (!offset[0] && !offset[1]) {
  1332. dev_err(host->dev, "Invalid PMECC lookup table offset\n");
  1333. return -EINVAL;
  1334. }
  1335. host->pmecc_lookup_table_offset_512 = offset[0];
  1336. host->pmecc_lookup_table_offset_1024 = offset[1];
  1337. return 0;
  1338. }
  1339. static int atmel_hw_nand_init_params(struct platform_device *pdev,
  1340. struct atmel_nand_host *host)
  1341. {
  1342. struct mtd_info *mtd = &host->mtd;
  1343. struct nand_chip *nand_chip = &host->nand_chip;
  1344. struct resource *regs;
  1345. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1346. if (!regs) {
  1347. dev_err(host->dev,
  1348. "Can't get I/O resource regs, use software ECC\n");
  1349. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1350. return 0;
  1351. }
  1352. host->ecc = devm_ioremap_resource(&pdev->dev, regs);
  1353. if (IS_ERR(host->ecc))
  1354. return PTR_ERR(host->ecc);
  1355. /* ECC is calculated for the whole page (1 step) */
  1356. nand_chip->ecc.size = mtd->writesize;
  1357. /* set ECC page size and oob layout */
  1358. switch (mtd->writesize) {
  1359. case 512:
  1360. nand_chip->ecc.layout = &atmel_oobinfo_small;
  1361. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
  1362. break;
  1363. case 1024:
  1364. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1365. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
  1366. break;
  1367. case 2048:
  1368. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1369. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
  1370. break;
  1371. case 4096:
  1372. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1373. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
  1374. break;
  1375. default:
  1376. /* page size not handled by HW ECC */
  1377. /* switching back to soft ECC */
  1378. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1379. return 0;
  1380. }
  1381. /* set up for HW ECC */
  1382. nand_chip->ecc.calculate = atmel_nand_calculate;
  1383. nand_chip->ecc.correct = atmel_nand_correct;
  1384. nand_chip->ecc.hwctl = atmel_nand_hwctl;
  1385. nand_chip->ecc.read_page = atmel_nand_read_page;
  1386. nand_chip->ecc.bytes = 4;
  1387. nand_chip->ecc.strength = 1;
  1388. return 0;
  1389. }
  1390. static inline u32 nfc_read_status(struct atmel_nand_host *host)
  1391. {
  1392. u32 err_flags = NFC_SR_DTOE | NFC_SR_UNDEF | NFC_SR_AWB | NFC_SR_ASE;
  1393. u32 nfc_status = nfc_readl(host->nfc->hsmc_regs, SR);
  1394. if (unlikely(nfc_status & err_flags)) {
  1395. if (nfc_status & NFC_SR_DTOE)
  1396. dev_err(host->dev, "NFC: Waiting Nand R/B Timeout Error\n");
  1397. else if (nfc_status & NFC_SR_UNDEF)
  1398. dev_err(host->dev, "NFC: Access Undefined Area Error\n");
  1399. else if (nfc_status & NFC_SR_AWB)
  1400. dev_err(host->dev, "NFC: Access memory While NFC is busy\n");
  1401. else if (nfc_status & NFC_SR_ASE)
  1402. dev_err(host->dev, "NFC: Access memory Size Error\n");
  1403. }
  1404. return nfc_status;
  1405. }
  1406. /* SMC interrupt service routine */
  1407. static irqreturn_t hsmc_interrupt(int irq, void *dev_id)
  1408. {
  1409. struct atmel_nand_host *host = dev_id;
  1410. u32 status, mask, pending;
  1411. irqreturn_t ret = IRQ_NONE;
  1412. status = nfc_read_status(host);
  1413. mask = nfc_readl(host->nfc->hsmc_regs, IMR);
  1414. pending = status & mask;
  1415. if (pending & NFC_SR_XFR_DONE) {
  1416. complete(&host->nfc->comp_xfer_done);
  1417. nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE);
  1418. ret = IRQ_HANDLED;
  1419. }
  1420. if (pending & NFC_SR_RB_EDGE) {
  1421. complete(&host->nfc->comp_ready);
  1422. nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_RB_EDGE);
  1423. ret = IRQ_HANDLED;
  1424. }
  1425. if (pending & NFC_SR_CMD_DONE) {
  1426. complete(&host->nfc->comp_cmd_done);
  1427. nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE);
  1428. ret = IRQ_HANDLED;
  1429. }
  1430. return ret;
  1431. }
  1432. /* NFC(Nand Flash Controller) related functions */
  1433. static void nfc_prepare_interrupt(struct atmel_nand_host *host, u32 flag)
  1434. {
  1435. if (flag & NFC_SR_XFR_DONE)
  1436. init_completion(&host->nfc->comp_xfer_done);
  1437. if (flag & NFC_SR_RB_EDGE)
  1438. init_completion(&host->nfc->comp_ready);
  1439. if (flag & NFC_SR_CMD_DONE)
  1440. init_completion(&host->nfc->comp_cmd_done);
  1441. /* Enable interrupt that need to wait for */
  1442. nfc_writel(host->nfc->hsmc_regs, IER, flag);
  1443. }
  1444. static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag)
  1445. {
  1446. int i, index = 0;
  1447. struct completion *comp[3]; /* Support 3 interrupt completion */
  1448. if (flag & NFC_SR_XFR_DONE)
  1449. comp[index++] = &host->nfc->comp_xfer_done;
  1450. if (flag & NFC_SR_RB_EDGE)
  1451. comp[index++] = &host->nfc->comp_ready;
  1452. if (flag & NFC_SR_CMD_DONE)
  1453. comp[index++] = &host->nfc->comp_cmd_done;
  1454. if (index == 0) {
  1455. dev_err(host->dev, "Unknown interrupt flag: 0x%08x\n", flag);
  1456. return -EINVAL;
  1457. }
  1458. for (i = 0; i < index; i++) {
  1459. if (wait_for_completion_timeout(comp[i],
  1460. msecs_to_jiffies(NFC_TIME_OUT_MS)))
  1461. continue; /* wait for next completion */
  1462. else
  1463. goto err_timeout;
  1464. }
  1465. return 0;
  1466. err_timeout:
  1467. dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag);
  1468. /* Disable the interrupt as it is not handled by interrupt handler */
  1469. nfc_writel(host->nfc->hsmc_regs, IDR, flag);
  1470. return -ETIMEDOUT;
  1471. }
  1472. static int nfc_send_command(struct atmel_nand_host *host,
  1473. unsigned int cmd, unsigned int addr, unsigned char cycle0)
  1474. {
  1475. unsigned long timeout;
  1476. u32 flag = NFC_SR_CMD_DONE;
  1477. flag |= cmd & NFCADDR_CMD_DATAEN ? NFC_SR_XFR_DONE : 0;
  1478. dev_dbg(host->dev,
  1479. "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n",
  1480. cmd, addr, cycle0);
  1481. timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
  1482. while (nfc_readl(host->nfc->hsmc_regs, SR) & NFC_SR_BUSY) {
  1483. if (time_after(jiffies, timeout)) {
  1484. dev_err(host->dev,
  1485. "Time out to wait for NFC ready!\n");
  1486. return -ETIMEDOUT;
  1487. }
  1488. }
  1489. nfc_prepare_interrupt(host, flag);
  1490. nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0);
  1491. nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs);
  1492. return nfc_wait_interrupt(host, flag);
  1493. }
  1494. static int nfc_device_ready(struct mtd_info *mtd)
  1495. {
  1496. u32 status, mask;
  1497. struct nand_chip *nand_chip = mtd->priv;
  1498. struct atmel_nand_host *host = nand_chip->priv;
  1499. status = nfc_read_status(host);
  1500. mask = nfc_readl(host->nfc->hsmc_regs, IMR);
  1501. /* The mask should be 0. If not we may lost interrupts */
  1502. if (unlikely(mask & status))
  1503. dev_err(host->dev, "Lost the interrupt flags: 0x%08x\n",
  1504. mask & status);
  1505. return status & NFC_SR_RB_EDGE;
  1506. }
  1507. static void nfc_select_chip(struct mtd_info *mtd, int chip)
  1508. {
  1509. struct nand_chip *nand_chip = mtd->priv;
  1510. struct atmel_nand_host *host = nand_chip->priv;
  1511. if (chip == -1)
  1512. nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE);
  1513. else
  1514. nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE);
  1515. }
  1516. static int nfc_make_addr(struct mtd_info *mtd, int command, int column,
  1517. int page_addr, unsigned int *addr1234, unsigned int *cycle0)
  1518. {
  1519. struct nand_chip *chip = mtd->priv;
  1520. int acycle = 0;
  1521. unsigned char addr_bytes[8];
  1522. int index = 0, bit_shift;
  1523. BUG_ON(addr1234 == NULL || cycle0 == NULL);
  1524. *cycle0 = 0;
  1525. *addr1234 = 0;
  1526. if (column != -1) {
  1527. if (chip->options & NAND_BUSWIDTH_16 &&
  1528. !nand_opcode_8bits(command))
  1529. column >>= 1;
  1530. addr_bytes[acycle++] = column & 0xff;
  1531. if (mtd->writesize > 512)
  1532. addr_bytes[acycle++] = (column >> 8) & 0xff;
  1533. }
  1534. if (page_addr != -1) {
  1535. addr_bytes[acycle++] = page_addr & 0xff;
  1536. addr_bytes[acycle++] = (page_addr >> 8) & 0xff;
  1537. if (chip->chipsize > (128 << 20))
  1538. addr_bytes[acycle++] = (page_addr >> 16) & 0xff;
  1539. }
  1540. if (acycle > 4)
  1541. *cycle0 = addr_bytes[index++];
  1542. for (bit_shift = 0; index < acycle; bit_shift += 8)
  1543. *addr1234 += addr_bytes[index++] << bit_shift;
  1544. /* return acycle in cmd register */
  1545. return acycle << NFCADDR_CMD_ACYCLE_BIT_POS;
  1546. }
  1547. static void nfc_nand_command(struct mtd_info *mtd, unsigned int command,
  1548. int column, int page_addr)
  1549. {
  1550. struct nand_chip *chip = mtd->priv;
  1551. struct atmel_nand_host *host = chip->priv;
  1552. unsigned long timeout;
  1553. unsigned int nfc_addr_cmd = 0;
  1554. unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
  1555. /* Set default settings: no cmd2, no addr cycle. read from nand */
  1556. unsigned int cmd2 = 0;
  1557. unsigned int vcmd2 = 0;
  1558. int acycle = NFCADDR_CMD_ACYCLE_NONE;
  1559. int csid = NFCADDR_CMD_CSID_3;
  1560. int dataen = NFCADDR_CMD_DATADIS;
  1561. int nfcwr = NFCADDR_CMD_NFCRD;
  1562. unsigned int addr1234 = 0;
  1563. unsigned int cycle0 = 0;
  1564. bool do_addr = true;
  1565. host->nfc->data_in_sram = NULL;
  1566. dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n",
  1567. __func__, command, column, page_addr);
  1568. switch (command) {
  1569. case NAND_CMD_RESET:
  1570. nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr;
  1571. nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
  1572. udelay(chip->chip_delay);
  1573. nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1);
  1574. timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS);
  1575. while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) {
  1576. if (time_after(jiffies, timeout)) {
  1577. dev_err(host->dev,
  1578. "Time out to wait status ready!\n");
  1579. break;
  1580. }
  1581. }
  1582. return;
  1583. case NAND_CMD_STATUS:
  1584. do_addr = false;
  1585. break;
  1586. case NAND_CMD_PARAM:
  1587. case NAND_CMD_READID:
  1588. do_addr = false;
  1589. acycle = NFCADDR_CMD_ACYCLE_1;
  1590. if (column != -1)
  1591. addr1234 = column;
  1592. break;
  1593. case NAND_CMD_RNDOUT:
  1594. cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS;
  1595. vcmd2 = NFCADDR_CMD_VCMD2;
  1596. break;
  1597. case NAND_CMD_READ0:
  1598. case NAND_CMD_READOOB:
  1599. if (command == NAND_CMD_READOOB) {
  1600. column += mtd->writesize;
  1601. command = NAND_CMD_READ0; /* only READ0 is valid */
  1602. cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS;
  1603. }
  1604. if (host->nfc->use_nfc_sram) {
  1605. /* Enable Data transfer to sram */
  1606. dataen = NFCADDR_CMD_DATAEN;
  1607. /* Need enable PMECC now, since NFC will transfer
  1608. * data in bus after sending nfc read command.
  1609. */
  1610. if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
  1611. pmecc_enable(host, NAND_ECC_READ);
  1612. }
  1613. cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS;
  1614. vcmd2 = NFCADDR_CMD_VCMD2;
  1615. break;
  1616. /* For prgramming command, the cmd need set to write enable */
  1617. case NAND_CMD_PAGEPROG:
  1618. case NAND_CMD_SEQIN:
  1619. case NAND_CMD_RNDIN:
  1620. nfcwr = NFCADDR_CMD_NFCWR;
  1621. if (host->nfc->will_write_sram && command == NAND_CMD_SEQIN)
  1622. dataen = NFCADDR_CMD_DATAEN;
  1623. break;
  1624. default:
  1625. break;
  1626. }
  1627. if (do_addr)
  1628. acycle = nfc_make_addr(mtd, command, column, page_addr,
  1629. &addr1234, &cycle0);
  1630. nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr;
  1631. nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0);
  1632. /*
  1633. * Program and erase have their own busy handlers status, sequential
  1634. * in, and deplete1 need no delay.
  1635. */
  1636. switch (command) {
  1637. case NAND_CMD_CACHEDPROG:
  1638. case NAND_CMD_PAGEPROG:
  1639. case NAND_CMD_ERASE1:
  1640. case NAND_CMD_ERASE2:
  1641. case NAND_CMD_RNDIN:
  1642. case NAND_CMD_STATUS:
  1643. case NAND_CMD_RNDOUT:
  1644. case NAND_CMD_SEQIN:
  1645. case NAND_CMD_READID:
  1646. return;
  1647. case NAND_CMD_READ0:
  1648. if (dataen == NFCADDR_CMD_DATAEN) {
  1649. host->nfc->data_in_sram = host->nfc->sram_bank0 +
  1650. nfc_get_sram_off(host);
  1651. return;
  1652. }
  1653. /* fall through */
  1654. default:
  1655. nfc_prepare_interrupt(host, NFC_SR_RB_EDGE);
  1656. nfc_wait_interrupt(host, NFC_SR_RB_EDGE);
  1657. }
  1658. }
  1659. static int nfc_sram_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1660. uint32_t offset, int data_len, const uint8_t *buf,
  1661. int oob_required, int page, int cached, int raw)
  1662. {
  1663. int cfg, len;
  1664. int status = 0;
  1665. struct atmel_nand_host *host = chip->priv;
  1666. void *sram = host->nfc->sram_bank0 + nfc_get_sram_off(host);
  1667. /* Subpage write is not supported */
  1668. if (offset || (data_len < mtd->writesize))
  1669. return -EINVAL;
  1670. len = mtd->writesize;
  1671. /* Copy page data to sram that will write to nand via NFC */
  1672. if (use_dma) {
  1673. if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) != 0)
  1674. /* Fall back to use cpu copy */
  1675. memcpy(sram, buf, len);
  1676. } else {
  1677. memcpy(sram, buf, len);
  1678. }
  1679. cfg = nfc_readl(host->nfc->hsmc_regs, CFG);
  1680. if (unlikely(raw) && oob_required) {
  1681. memcpy(sram + len, chip->oob_poi, mtd->oobsize);
  1682. len += mtd->oobsize;
  1683. nfc_writel(host->nfc->hsmc_regs, CFG, cfg | NFC_CFG_WSPARE);
  1684. } else {
  1685. nfc_writel(host->nfc->hsmc_regs, CFG, cfg & ~NFC_CFG_WSPARE);
  1686. }
  1687. if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc)
  1688. /*
  1689. * When use NFC sram, need set up PMECC before send
  1690. * NAND_CMD_SEQIN command. Since when the nand command
  1691. * is sent, nfc will do transfer from sram and nand.
  1692. */
  1693. pmecc_enable(host, NAND_ECC_WRITE);
  1694. host->nfc->will_write_sram = true;
  1695. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1696. host->nfc->will_write_sram = false;
  1697. if (likely(!raw))
  1698. /* Need to write ecc into oob */
  1699. status = chip->ecc.write_page(mtd, chip, buf, oob_required);
  1700. if (status < 0)
  1701. return status;
  1702. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1703. status = chip->waitfunc(mtd, chip);
  1704. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  1705. status = chip->errstat(mtd, chip, FL_WRITING, status, page);
  1706. if (status & NAND_STATUS_FAIL)
  1707. return -EIO;
  1708. return 0;
  1709. }
  1710. static int nfc_sram_init(struct mtd_info *mtd)
  1711. {
  1712. struct nand_chip *chip = mtd->priv;
  1713. struct atmel_nand_host *host = chip->priv;
  1714. int res = 0;
  1715. /* Initialize the NFC CFG register */
  1716. unsigned int cfg_nfc = 0;
  1717. /* set page size and oob layout */
  1718. switch (mtd->writesize) {
  1719. case 512:
  1720. cfg_nfc = NFC_CFG_PAGESIZE_512;
  1721. break;
  1722. case 1024:
  1723. cfg_nfc = NFC_CFG_PAGESIZE_1024;
  1724. break;
  1725. case 2048:
  1726. cfg_nfc = NFC_CFG_PAGESIZE_2048;
  1727. break;
  1728. case 4096:
  1729. cfg_nfc = NFC_CFG_PAGESIZE_4096;
  1730. break;
  1731. case 8192:
  1732. cfg_nfc = NFC_CFG_PAGESIZE_8192;
  1733. break;
  1734. default:
  1735. dev_err(host->dev, "Unsupported page size for NFC.\n");
  1736. res = -ENXIO;
  1737. return res;
  1738. }
  1739. /* oob bytes size = (NFCSPARESIZE + 1) * 4
  1740. * Max support spare size is 512 bytes. */
  1741. cfg_nfc |= (((mtd->oobsize / 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS
  1742. & NFC_CFG_NFC_SPARESIZE);
  1743. /* default set a max timeout */
  1744. cfg_nfc |= NFC_CFG_RSPARE |
  1745. NFC_CFG_NFC_DTOCYC | NFC_CFG_NFC_DTOMUL;
  1746. nfc_writel(host->nfc->hsmc_regs, CFG, cfg_nfc);
  1747. host->nfc->will_write_sram = false;
  1748. nfc_set_sram_bank(host, 0);
  1749. /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */
  1750. if (host->nfc->write_by_sram) {
  1751. if ((chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) ||
  1752. chip->ecc.mode == NAND_ECC_NONE)
  1753. chip->write_page = nfc_sram_write_page;
  1754. else
  1755. host->nfc->write_by_sram = false;
  1756. }
  1757. dev_info(host->dev, "Using NFC Sram read %s\n",
  1758. host->nfc->write_by_sram ? "and write" : "");
  1759. return 0;
  1760. }
  1761. static struct platform_driver atmel_nand_nfc_driver;
  1762. /*
  1763. * Probe for the NAND device.
  1764. */
  1765. static int atmel_nand_probe(struct platform_device *pdev)
  1766. {
  1767. struct atmel_nand_host *host;
  1768. struct mtd_info *mtd;
  1769. struct nand_chip *nand_chip;
  1770. struct resource *mem;
  1771. struct mtd_part_parser_data ppdata = {};
  1772. int res, irq;
  1773. /* Allocate memory for the device structure (and zero it) */
  1774. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  1775. if (!host)
  1776. return -ENOMEM;
  1777. res = platform_driver_register(&atmel_nand_nfc_driver);
  1778. if (res)
  1779. dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n");
  1780. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1781. host->io_base = devm_ioremap_resource(&pdev->dev, mem);
  1782. if (IS_ERR(host->io_base)) {
  1783. res = PTR_ERR(host->io_base);
  1784. goto err_nand_ioremap;
  1785. }
  1786. host->io_phys = (dma_addr_t)mem->start;
  1787. mtd = &host->mtd;
  1788. nand_chip = &host->nand_chip;
  1789. host->dev = &pdev->dev;
  1790. if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) {
  1791. /* Only when CONFIG_OF is enabled of_node can be parsed */
  1792. res = atmel_of_init_port(host, pdev->dev.of_node);
  1793. if (res)
  1794. goto err_nand_ioremap;
  1795. } else {
  1796. memcpy(&host->board, dev_get_platdata(&pdev->dev),
  1797. sizeof(struct atmel_nand_data));
  1798. }
  1799. nand_chip->priv = host; /* link the private data structures */
  1800. mtd->priv = nand_chip;
  1801. mtd->owner = THIS_MODULE;
  1802. /* Set address of NAND IO lines */
  1803. nand_chip->IO_ADDR_R = host->io_base;
  1804. nand_chip->IO_ADDR_W = host->io_base;
  1805. if (nand_nfc.is_initialized) {
  1806. /* NFC driver is probed and initialized */
  1807. host->nfc = &nand_nfc;
  1808. nand_chip->select_chip = nfc_select_chip;
  1809. nand_chip->dev_ready = nfc_device_ready;
  1810. nand_chip->cmdfunc = nfc_nand_command;
  1811. /* Initialize the interrupt for NFC */
  1812. irq = platform_get_irq(pdev, 0);
  1813. if (irq < 0) {
  1814. dev_err(host->dev, "Cannot get HSMC irq!\n");
  1815. res = irq;
  1816. goto err_nand_ioremap;
  1817. }
  1818. res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt,
  1819. 0, "hsmc", host);
  1820. if (res) {
  1821. dev_err(&pdev->dev, "Unable to request HSMC irq %d\n",
  1822. irq);
  1823. goto err_nand_ioremap;
  1824. }
  1825. } else {
  1826. res = atmel_nand_set_enable_ready_pins(mtd);
  1827. if (res)
  1828. goto err_nand_ioremap;
  1829. nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
  1830. }
  1831. nand_chip->ecc.mode = host->board.ecc_mode;
  1832. nand_chip->chip_delay = 40; /* 40us command delay time */
  1833. if (host->board.bus_width_16) /* 16-bit bus width */
  1834. nand_chip->options |= NAND_BUSWIDTH_16;
  1835. nand_chip->read_buf = atmel_read_buf;
  1836. nand_chip->write_buf = atmel_write_buf;
  1837. platform_set_drvdata(pdev, host);
  1838. atmel_nand_enable(host);
  1839. if (gpio_is_valid(host->board.det_pin)) {
  1840. res = devm_gpio_request(&pdev->dev,
  1841. host->board.det_pin, "nand_det");
  1842. if (res < 0) {
  1843. dev_err(&pdev->dev,
  1844. "can't request det gpio %d\n",
  1845. host->board.det_pin);
  1846. goto err_no_card;
  1847. }
  1848. res = gpio_direction_input(host->board.det_pin);
  1849. if (res < 0) {
  1850. dev_err(&pdev->dev,
  1851. "can't request input direction det gpio %d\n",
  1852. host->board.det_pin);
  1853. goto err_no_card;
  1854. }
  1855. if (gpio_get_value(host->board.det_pin)) {
  1856. dev_info(&pdev->dev, "No SmartMedia card inserted.\n");
  1857. res = -ENXIO;
  1858. goto err_no_card;
  1859. }
  1860. }
  1861. if (host->board.on_flash_bbt || on_flash_bbt) {
  1862. dev_info(&pdev->dev, "Use On Flash BBT\n");
  1863. nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
  1864. }
  1865. if (!host->board.has_dma)
  1866. use_dma = 0;
  1867. if (use_dma) {
  1868. dma_cap_mask_t mask;
  1869. dma_cap_zero(mask);
  1870. dma_cap_set(DMA_MEMCPY, mask);
  1871. host->dma_chan = dma_request_channel(mask, NULL, NULL);
  1872. if (!host->dma_chan) {
  1873. dev_err(host->dev, "Failed to request DMA channel\n");
  1874. use_dma = 0;
  1875. }
  1876. }
  1877. if (use_dma)
  1878. dev_info(host->dev, "Using %s for DMA transfers.\n",
  1879. dma_chan_name(host->dma_chan));
  1880. else
  1881. dev_info(host->dev, "No DMA support for NAND access.\n");
  1882. /* first scan to find the device and get the page size */
  1883. if (nand_scan_ident(mtd, 1, NULL)) {
  1884. res = -ENXIO;
  1885. goto err_scan_ident;
  1886. }
  1887. if (nand_chip->ecc.mode == NAND_ECC_HW) {
  1888. if (host->has_pmecc)
  1889. res = atmel_pmecc_nand_init_params(pdev, host);
  1890. else
  1891. res = atmel_hw_nand_init_params(pdev, host);
  1892. if (res != 0)
  1893. goto err_hw_ecc;
  1894. }
  1895. /* initialize the nfc configuration register */
  1896. if (host->nfc && host->nfc->use_nfc_sram) {
  1897. res = nfc_sram_init(mtd);
  1898. if (res) {
  1899. host->nfc->use_nfc_sram = false;
  1900. dev_err(host->dev, "Disable use nfc sram for data transfer.\n");
  1901. }
  1902. }
  1903. /* second phase scan */
  1904. if (nand_scan_tail(mtd)) {
  1905. res = -ENXIO;
  1906. goto err_scan_tail;
  1907. }
  1908. mtd->name = "atmel_nand";
  1909. ppdata.of_node = pdev->dev.of_node;
  1910. res = mtd_device_parse_register(mtd, NULL, &ppdata,
  1911. host->board.parts, host->board.num_parts);
  1912. if (!res)
  1913. return res;
  1914. err_scan_tail:
  1915. if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
  1916. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  1917. err_hw_ecc:
  1918. err_scan_ident:
  1919. err_no_card:
  1920. atmel_nand_disable(host);
  1921. if (host->dma_chan)
  1922. dma_release_channel(host->dma_chan);
  1923. err_nand_ioremap:
  1924. return res;
  1925. }
  1926. /*
  1927. * Remove a NAND device.
  1928. */
  1929. static int atmel_nand_remove(struct platform_device *pdev)
  1930. {
  1931. struct atmel_nand_host *host = platform_get_drvdata(pdev);
  1932. struct mtd_info *mtd = &host->mtd;
  1933. nand_release(mtd);
  1934. atmel_nand_disable(host);
  1935. if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
  1936. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  1937. pmerrloc_writel(host->pmerrloc_base, ELDIS,
  1938. PMERRLOC_DISABLE);
  1939. }
  1940. if (host->dma_chan)
  1941. dma_release_channel(host->dma_chan);
  1942. platform_driver_unregister(&atmel_nand_nfc_driver);
  1943. return 0;
  1944. }
  1945. static struct atmel_nand_caps at91rm9200_caps = {
  1946. .pmecc_correct_erase_page = false,
  1947. };
  1948. static struct atmel_nand_caps sama5d4_caps = {
  1949. .pmecc_correct_erase_page = true,
  1950. };
  1951. static const struct of_device_id atmel_nand_dt_ids[] = {
  1952. { .compatible = "atmel,at91rm9200-nand", .data = &at91rm9200_caps },
  1953. { .compatible = "atmel,sama5d4-nand", .data = &sama5d4_caps },
  1954. { /* sentinel */ }
  1955. };
  1956. MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
  1957. static int atmel_nand_nfc_probe(struct platform_device *pdev)
  1958. {
  1959. struct atmel_nfc *nfc = &nand_nfc;
  1960. struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram;
  1961. int ret;
  1962. nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1963. nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs);
  1964. if (IS_ERR(nfc->base_cmd_regs))
  1965. return PTR_ERR(nfc->base_cmd_regs);
  1966. nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1967. nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs);
  1968. if (IS_ERR(nfc->hsmc_regs))
  1969. return PTR_ERR(nfc->hsmc_regs);
  1970. nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  1971. if (nfc_sram) {
  1972. nfc->sram_bank0 = (void * __force)
  1973. devm_ioremap_resource(&pdev->dev, nfc_sram);
  1974. if (IS_ERR(nfc->sram_bank0)) {
  1975. dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n",
  1976. PTR_ERR(nfc->sram_bank0));
  1977. } else {
  1978. nfc->use_nfc_sram = true;
  1979. nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start;
  1980. if (pdev->dev.of_node)
  1981. nfc->write_by_sram = of_property_read_bool(
  1982. pdev->dev.of_node,
  1983. "atmel,write-by-sram");
  1984. }
  1985. }
  1986. nfc_writel(nfc->hsmc_regs, IDR, 0xffffffff);
  1987. nfc_readl(nfc->hsmc_regs, SR); /* clear the NFC_SR */
  1988. nfc->clk = devm_clk_get(&pdev->dev, NULL);
  1989. if (!IS_ERR(nfc->clk)) {
  1990. ret = clk_prepare_enable(nfc->clk);
  1991. if (ret)
  1992. return ret;
  1993. } else {
  1994. dev_warn(&pdev->dev, "NFC clock missing, update your Device Tree");
  1995. }
  1996. nfc->is_initialized = true;
  1997. dev_info(&pdev->dev, "NFC is probed.\n");
  1998. return 0;
  1999. }
  2000. static int atmel_nand_nfc_remove(struct platform_device *pdev)
  2001. {
  2002. struct atmel_nfc *nfc = &nand_nfc;
  2003. if (!IS_ERR(nfc->clk))
  2004. clk_disable_unprepare(nfc->clk);
  2005. return 0;
  2006. }
  2007. static const struct of_device_id atmel_nand_nfc_match[] = {
  2008. { .compatible = "atmel,sama5d3-nfc" },
  2009. { /* sentinel */ }
  2010. };
  2011. MODULE_DEVICE_TABLE(of, atmel_nand_nfc_match);
  2012. static struct platform_driver atmel_nand_nfc_driver = {
  2013. .driver = {
  2014. .name = "atmel_nand_nfc",
  2015. .of_match_table = of_match_ptr(atmel_nand_nfc_match),
  2016. },
  2017. .probe = atmel_nand_nfc_probe,
  2018. .remove = atmel_nand_nfc_remove,
  2019. };
  2020. static struct platform_driver atmel_nand_driver = {
  2021. .probe = atmel_nand_probe,
  2022. .remove = atmel_nand_remove,
  2023. .driver = {
  2024. .name = "atmel_nand",
  2025. .of_match_table = of_match_ptr(atmel_nand_dt_ids),
  2026. },
  2027. };
  2028. module_platform_driver(atmel_nand_driver);
  2029. MODULE_LICENSE("GPL");
  2030. MODULE_AUTHOR("Rick Bronson");
  2031. MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
  2032. MODULE_ALIAS("platform:atmel_nand");