atmel_nand.c 58 KB

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