atmel_nand.c 59 KB

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