atmel_nand.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  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. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU General Public License version 2 as
  23. * published by the Free Software Foundation.
  24. *
  25. */
  26. #include <linux/dma-mapping.h>
  27. #include <linux/slab.h>
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/of.h>
  32. #include <linux/of_device.h>
  33. #include <linux/of_gpio.h>
  34. #include <linux/of_mtd.h>
  35. #include <linux/mtd/mtd.h>
  36. #include <linux/mtd/nand.h>
  37. #include <linux/mtd/partitions.h>
  38. #include <linux/dmaengine.h>
  39. #include <linux/gpio.h>
  40. #include <linux/io.h>
  41. #include <linux/platform_data/atmel.h>
  42. #include <mach/cpu.h>
  43. static int use_dma = 1;
  44. module_param(use_dma, int, 0);
  45. static int on_flash_bbt = 0;
  46. module_param(on_flash_bbt, int, 0);
  47. /* Register access macros */
  48. #define ecc_readl(add, reg) \
  49. __raw_readl(add + ATMEL_ECC_##reg)
  50. #define ecc_writel(add, reg, value) \
  51. __raw_writel((value), add + ATMEL_ECC_##reg)
  52. #include "atmel_nand_ecc.h" /* Hardware ECC registers */
  53. /* oob layout for large page size
  54. * bad block info is on bytes 0 and 1
  55. * the bytes have to be consecutives to avoid
  56. * several NAND_CMD_RNDOUT during read
  57. */
  58. static struct nand_ecclayout atmel_oobinfo_large = {
  59. .eccbytes = 4,
  60. .eccpos = {60, 61, 62, 63},
  61. .oobfree = {
  62. {2, 58}
  63. },
  64. };
  65. /* oob layout for small page size
  66. * bad block info is on bytes 4 and 5
  67. * the bytes have to be consecutives to avoid
  68. * several NAND_CMD_RNDOUT during read
  69. */
  70. static struct nand_ecclayout atmel_oobinfo_small = {
  71. .eccbytes = 4,
  72. .eccpos = {0, 1, 2, 3},
  73. .oobfree = {
  74. {6, 10}
  75. },
  76. };
  77. struct atmel_nand_host {
  78. struct nand_chip nand_chip;
  79. struct mtd_info mtd;
  80. void __iomem *io_base;
  81. dma_addr_t io_phys;
  82. struct atmel_nand_data board;
  83. struct device *dev;
  84. void __iomem *ecc;
  85. struct completion comp;
  86. struct dma_chan *dma_chan;
  87. bool has_pmecc;
  88. u8 pmecc_corr_cap;
  89. u16 pmecc_sector_size;
  90. u32 pmecc_lookup_table_offset;
  91. int pmecc_bytes_per_sector;
  92. int pmecc_sector_number;
  93. int pmecc_degree; /* Degree of remainders */
  94. int pmecc_cw_len; /* Length of codeword */
  95. void __iomem *pmerrloc_base;
  96. void __iomem *pmecc_rom_base;
  97. /* lookup table for alpha_to and index_of */
  98. void __iomem *pmecc_alpha_to;
  99. void __iomem *pmecc_index_of;
  100. /* data for pmecc computation */
  101. int16_t *pmecc_partial_syn;
  102. int16_t *pmecc_si;
  103. int16_t *pmecc_smu; /* Sigma table */
  104. int16_t *pmecc_lmu; /* polynomal order */
  105. int *pmecc_mu;
  106. int *pmecc_dmu;
  107. int *pmecc_delta;
  108. };
  109. static struct nand_ecclayout atmel_pmecc_oobinfo;
  110. static int cpu_has_dma(void)
  111. {
  112. return cpu_is_at91sam9rl() || cpu_is_at91sam9g45();
  113. }
  114. /*
  115. * Enable NAND.
  116. */
  117. static void atmel_nand_enable(struct atmel_nand_host *host)
  118. {
  119. if (gpio_is_valid(host->board.enable_pin))
  120. gpio_set_value(host->board.enable_pin, 0);
  121. }
  122. /*
  123. * Disable NAND.
  124. */
  125. static void atmel_nand_disable(struct atmel_nand_host *host)
  126. {
  127. if (gpio_is_valid(host->board.enable_pin))
  128. gpio_set_value(host->board.enable_pin, 1);
  129. }
  130. /*
  131. * Hardware specific access to control-lines
  132. */
  133. static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  134. {
  135. struct nand_chip *nand_chip = mtd->priv;
  136. struct atmel_nand_host *host = nand_chip->priv;
  137. if (ctrl & NAND_CTRL_CHANGE) {
  138. if (ctrl & NAND_NCE)
  139. atmel_nand_enable(host);
  140. else
  141. atmel_nand_disable(host);
  142. }
  143. if (cmd == NAND_CMD_NONE)
  144. return;
  145. if (ctrl & NAND_CLE)
  146. writeb(cmd, host->io_base + (1 << host->board.cle));
  147. else
  148. writeb(cmd, host->io_base + (1 << host->board.ale));
  149. }
  150. /*
  151. * Read the Device Ready pin.
  152. */
  153. static int atmel_nand_device_ready(struct mtd_info *mtd)
  154. {
  155. struct nand_chip *nand_chip = mtd->priv;
  156. struct atmel_nand_host *host = nand_chip->priv;
  157. return gpio_get_value(host->board.rdy_pin) ^
  158. !!host->board.rdy_pin_active_low;
  159. }
  160. /*
  161. * Minimal-overhead PIO for data access.
  162. */
  163. static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
  164. {
  165. struct nand_chip *nand_chip = mtd->priv;
  166. __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
  167. }
  168. static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
  169. {
  170. struct nand_chip *nand_chip = mtd->priv;
  171. __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
  172. }
  173. static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
  174. {
  175. struct nand_chip *nand_chip = mtd->priv;
  176. __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
  177. }
  178. static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
  179. {
  180. struct nand_chip *nand_chip = mtd->priv;
  181. __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
  182. }
  183. static void dma_complete_func(void *completion)
  184. {
  185. complete(completion);
  186. }
  187. static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
  188. int is_read)
  189. {
  190. struct dma_device *dma_dev;
  191. enum dma_ctrl_flags flags;
  192. dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
  193. struct dma_async_tx_descriptor *tx = NULL;
  194. dma_cookie_t cookie;
  195. struct nand_chip *chip = mtd->priv;
  196. struct atmel_nand_host *host = chip->priv;
  197. void *p = buf;
  198. int err = -EIO;
  199. enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  200. if (buf >= high_memory)
  201. goto err_buf;
  202. dma_dev = host->dma_chan->device;
  203. flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP |
  204. DMA_COMPL_SKIP_DEST_UNMAP;
  205. phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
  206. if (dma_mapping_error(dma_dev->dev, phys_addr)) {
  207. dev_err(host->dev, "Failed to dma_map_single\n");
  208. goto err_buf;
  209. }
  210. if (is_read) {
  211. dma_src_addr = host->io_phys;
  212. dma_dst_addr = phys_addr;
  213. } else {
  214. dma_src_addr = phys_addr;
  215. dma_dst_addr = host->io_phys;
  216. }
  217. tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
  218. dma_src_addr, len, flags);
  219. if (!tx) {
  220. dev_err(host->dev, "Failed to prepare DMA memcpy\n");
  221. goto err_dma;
  222. }
  223. init_completion(&host->comp);
  224. tx->callback = dma_complete_func;
  225. tx->callback_param = &host->comp;
  226. cookie = tx->tx_submit(tx);
  227. if (dma_submit_error(cookie)) {
  228. dev_err(host->dev, "Failed to do DMA tx_submit\n");
  229. goto err_dma;
  230. }
  231. dma_async_issue_pending(host->dma_chan);
  232. wait_for_completion(&host->comp);
  233. err = 0;
  234. err_dma:
  235. dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
  236. err_buf:
  237. if (err != 0)
  238. dev_warn(host->dev, "Fall back to CPU I/O\n");
  239. return err;
  240. }
  241. static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  242. {
  243. struct nand_chip *chip = mtd->priv;
  244. struct atmel_nand_host *host = chip->priv;
  245. if (use_dma && len > mtd->oobsize)
  246. /* only use DMA for bigger than oob size: better performances */
  247. if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
  248. return;
  249. if (host->board.bus_width_16)
  250. atmel_read_buf16(mtd, buf, len);
  251. else
  252. atmel_read_buf8(mtd, buf, len);
  253. }
  254. static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  255. {
  256. struct nand_chip *chip = mtd->priv;
  257. struct atmel_nand_host *host = chip->priv;
  258. if (use_dma && len > mtd->oobsize)
  259. /* only use DMA for bigger than oob size: better performances */
  260. if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
  261. return;
  262. if (host->board.bus_width_16)
  263. atmel_write_buf16(mtd, buf, len);
  264. else
  265. atmel_write_buf8(mtd, buf, len);
  266. }
  267. /*
  268. * Return number of ecc bytes per sector according to sector size and
  269. * correction capability
  270. *
  271. * Following table shows what at91 PMECC supported:
  272. * Correction Capability Sector_512_bytes Sector_1024_bytes
  273. * ===================== ================ =================
  274. * 2-bits 4-bytes 4-bytes
  275. * 4-bits 7-bytes 7-bytes
  276. * 8-bits 13-bytes 14-bytes
  277. * 12-bits 20-bytes 21-bytes
  278. * 24-bits 39-bytes 42-bytes
  279. */
  280. static int __devinit pmecc_get_ecc_bytes(int cap, int sector_size)
  281. {
  282. int m = 12 + sector_size / 512;
  283. return (m * cap + 7) / 8;
  284. }
  285. static void __devinit pmecc_config_ecc_layout(struct nand_ecclayout *layout,
  286. int oobsize, int ecc_len)
  287. {
  288. int i;
  289. layout->eccbytes = ecc_len;
  290. /* ECC will occupy the last ecc_len bytes continuously */
  291. for (i = 0; i < ecc_len; i++)
  292. layout->eccpos[i] = oobsize - ecc_len + i;
  293. layout->oobfree[0].offset = 2;
  294. layout->oobfree[0].length =
  295. oobsize - ecc_len - layout->oobfree[0].offset;
  296. }
  297. static void __devinit __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
  298. {
  299. int table_size;
  300. table_size = host->pmecc_sector_size == 512 ?
  301. PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
  302. return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
  303. table_size * sizeof(int16_t);
  304. }
  305. static void pmecc_data_free(struct atmel_nand_host *host)
  306. {
  307. kfree(host->pmecc_partial_syn);
  308. kfree(host->pmecc_si);
  309. kfree(host->pmecc_lmu);
  310. kfree(host->pmecc_smu);
  311. kfree(host->pmecc_mu);
  312. kfree(host->pmecc_dmu);
  313. kfree(host->pmecc_delta);
  314. }
  315. static int __devinit pmecc_data_alloc(struct atmel_nand_host *host)
  316. {
  317. const int cap = host->pmecc_corr_cap;
  318. host->pmecc_partial_syn = kzalloc((2 * cap + 1) * sizeof(int16_t),
  319. GFP_KERNEL);
  320. host->pmecc_si = kzalloc((2 * cap + 1) * sizeof(int16_t), GFP_KERNEL);
  321. host->pmecc_lmu = kzalloc((cap + 1) * sizeof(int16_t), GFP_KERNEL);
  322. host->pmecc_smu = kzalloc((cap + 2) * (2 * cap + 1) * sizeof(int16_t),
  323. GFP_KERNEL);
  324. host->pmecc_mu = kzalloc((cap + 1) * sizeof(int), GFP_KERNEL);
  325. host->pmecc_dmu = kzalloc((cap + 1) * sizeof(int), GFP_KERNEL);
  326. host->pmecc_delta = kzalloc((cap + 1) * sizeof(int), GFP_KERNEL);
  327. if (host->pmecc_partial_syn &&
  328. host->pmecc_si &&
  329. host->pmecc_lmu &&
  330. host->pmecc_smu &&
  331. host->pmecc_mu &&
  332. host->pmecc_dmu &&
  333. host->pmecc_delta)
  334. return 0;
  335. /* error happened */
  336. pmecc_data_free(host);
  337. return -ENOMEM;
  338. }
  339. static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
  340. {
  341. struct nand_chip *nand_chip = mtd->priv;
  342. struct atmel_nand_host *host = nand_chip->priv;
  343. int i;
  344. uint32_t value;
  345. /* Fill odd syndromes */
  346. for (i = 0; i < host->pmecc_corr_cap; i++) {
  347. value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
  348. if (i & 1)
  349. value >>= 16;
  350. value &= 0xffff;
  351. host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
  352. }
  353. }
  354. static void pmecc_substitute(struct mtd_info *mtd)
  355. {
  356. struct nand_chip *nand_chip = mtd->priv;
  357. struct atmel_nand_host *host = nand_chip->priv;
  358. int16_t __iomem *alpha_to = host->pmecc_alpha_to;
  359. int16_t __iomem *index_of = host->pmecc_index_of;
  360. int16_t *partial_syn = host->pmecc_partial_syn;
  361. const int cap = host->pmecc_corr_cap;
  362. int16_t *si;
  363. int i, j;
  364. /* si[] is a table that holds the current syndrome value,
  365. * an element of that table belongs to the field
  366. */
  367. si = host->pmecc_si;
  368. memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
  369. /* Computation 2t syndromes based on S(x) */
  370. /* Odd syndromes */
  371. for (i = 1; i < 2 * cap; i += 2) {
  372. for (j = 0; j < host->pmecc_degree; j++) {
  373. if (partial_syn[i] & ((unsigned short)0x1 << j))
  374. si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
  375. }
  376. }
  377. /* Even syndrome = (Odd syndrome) ** 2 */
  378. for (i = 2, j = 1; j <= cap; i = ++j << 1) {
  379. if (si[j] == 0) {
  380. si[i] = 0;
  381. } else {
  382. int16_t tmp;
  383. tmp = readw_relaxed(index_of + si[j]);
  384. tmp = (tmp * 2) % host->pmecc_cw_len;
  385. si[i] = readw_relaxed(alpha_to + tmp);
  386. }
  387. }
  388. return;
  389. }
  390. static void pmecc_get_sigma(struct mtd_info *mtd)
  391. {
  392. struct nand_chip *nand_chip = mtd->priv;
  393. struct atmel_nand_host *host = nand_chip->priv;
  394. int16_t *lmu = host->pmecc_lmu;
  395. int16_t *si = host->pmecc_si;
  396. int *mu = host->pmecc_mu;
  397. int *dmu = host->pmecc_dmu; /* Discrepancy */
  398. int *delta = host->pmecc_delta; /* Delta order */
  399. int cw_len = host->pmecc_cw_len;
  400. const int16_t cap = host->pmecc_corr_cap;
  401. const int num = 2 * cap + 1;
  402. int16_t __iomem *index_of = host->pmecc_index_of;
  403. int16_t __iomem *alpha_to = host->pmecc_alpha_to;
  404. int i, j, k;
  405. uint32_t dmu_0_count, tmp;
  406. int16_t *smu = host->pmecc_smu;
  407. /* index of largest delta */
  408. int ro;
  409. int largest;
  410. int diff;
  411. dmu_0_count = 0;
  412. /* First Row */
  413. /* Mu */
  414. mu[0] = -1;
  415. memset(smu, 0, sizeof(int16_t) * num);
  416. smu[0] = 1;
  417. /* discrepancy set to 1 */
  418. dmu[0] = 1;
  419. /* polynom order set to 0 */
  420. lmu[0] = 0;
  421. delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
  422. /* Second Row */
  423. /* Mu */
  424. mu[1] = 0;
  425. /* Sigma(x) set to 1 */
  426. memset(&smu[num], 0, sizeof(int16_t) * num);
  427. smu[num] = 1;
  428. /* discrepancy set to S1 */
  429. dmu[1] = si[1];
  430. /* polynom order set to 0 */
  431. lmu[1] = 0;
  432. delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
  433. /* Init the Sigma(x) last row */
  434. memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
  435. for (i = 1; i <= cap; i++) {
  436. mu[i + 1] = i << 1;
  437. /* Begin Computing Sigma (Mu+1) and L(mu) */
  438. /* check if discrepancy is set to 0 */
  439. if (dmu[i] == 0) {
  440. dmu_0_count++;
  441. tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
  442. if ((cap - (lmu[i] >> 1) - 1) & 0x1)
  443. tmp += 2;
  444. else
  445. tmp += 1;
  446. if (dmu_0_count == tmp) {
  447. for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
  448. smu[(cap + 1) * num + j] =
  449. smu[i * num + j];
  450. lmu[cap + 1] = lmu[i];
  451. return;
  452. }
  453. /* copy polynom */
  454. for (j = 0; j <= lmu[i] >> 1; j++)
  455. smu[(i + 1) * num + j] = smu[i * num + j];
  456. /* copy previous polynom order to the next */
  457. lmu[i + 1] = lmu[i];
  458. } else {
  459. ro = 0;
  460. largest = -1;
  461. /* find largest delta with dmu != 0 */
  462. for (j = 0; j < i; j++) {
  463. if ((dmu[j]) && (delta[j] > largest)) {
  464. largest = delta[j];
  465. ro = j;
  466. }
  467. }
  468. /* compute difference */
  469. diff = (mu[i] - mu[ro]);
  470. /* Compute degree of the new smu polynomial */
  471. if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
  472. lmu[i + 1] = lmu[i];
  473. else
  474. lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
  475. /* Init smu[i+1] with 0 */
  476. for (k = 0; k < num; k++)
  477. smu[(i + 1) * num + k] = 0;
  478. /* Compute smu[i+1] */
  479. for (k = 0; k <= lmu[ro] >> 1; k++) {
  480. int16_t a, b, c;
  481. if (!(smu[ro * num + k] && dmu[i]))
  482. continue;
  483. a = readw_relaxed(index_of + dmu[i]);
  484. b = readw_relaxed(index_of + dmu[ro]);
  485. c = readw_relaxed(index_of + smu[ro * num + k]);
  486. tmp = a + (cw_len - b) + c;
  487. a = readw_relaxed(alpha_to + tmp % cw_len);
  488. smu[(i + 1) * num + (k + diff)] = a;
  489. }
  490. for (k = 0; k <= lmu[i] >> 1; k++)
  491. smu[(i + 1) * num + k] ^= smu[i * num + k];
  492. }
  493. /* End Computing Sigma (Mu+1) and L(mu) */
  494. /* In either case compute delta */
  495. delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
  496. /* Do not compute discrepancy for the last iteration */
  497. if (i >= cap)
  498. continue;
  499. for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
  500. tmp = 2 * (i - 1);
  501. if (k == 0) {
  502. dmu[i + 1] = si[tmp + 3];
  503. } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
  504. int16_t a, b, c;
  505. a = readw_relaxed(index_of +
  506. smu[(i + 1) * num + k]);
  507. b = si[2 * (i - 1) + 3 - k];
  508. c = readw_relaxed(index_of + b);
  509. tmp = a + c;
  510. tmp %= cw_len;
  511. dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
  512. dmu[i + 1];
  513. }
  514. }
  515. }
  516. return;
  517. }
  518. static int pmecc_err_location(struct mtd_info *mtd)
  519. {
  520. struct nand_chip *nand_chip = mtd->priv;
  521. struct atmel_nand_host *host = nand_chip->priv;
  522. unsigned long end_time;
  523. const int cap = host->pmecc_corr_cap;
  524. const int num = 2 * cap + 1;
  525. int sector_size = host->pmecc_sector_size;
  526. int err_nbr = 0; /* number of error */
  527. int roots_nbr; /* number of roots */
  528. int i;
  529. uint32_t val;
  530. int16_t *smu = host->pmecc_smu;
  531. pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
  532. for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
  533. pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
  534. smu[(cap + 1) * num + i]);
  535. err_nbr++;
  536. }
  537. val = (err_nbr - 1) << 16;
  538. if (sector_size == 1024)
  539. val |= 1;
  540. pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
  541. pmerrloc_writel(host->pmerrloc_base, ELEN,
  542. sector_size * 8 + host->pmecc_degree * cap);
  543. end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
  544. while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
  545. & PMERRLOC_CALC_DONE)) {
  546. if (unlikely(time_after(jiffies, end_time))) {
  547. dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
  548. return -1;
  549. }
  550. cpu_relax();
  551. }
  552. roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
  553. & PMERRLOC_ERR_NUM_MASK) >> 8;
  554. /* Number of roots == degree of smu hence <= cap */
  555. if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
  556. return err_nbr - 1;
  557. /* Number of roots does not match the degree of smu
  558. * unable to correct error */
  559. return -1;
  560. }
  561. static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
  562. int sector_num, int extra_bytes, int err_nbr)
  563. {
  564. struct nand_chip *nand_chip = mtd->priv;
  565. struct atmel_nand_host *host = nand_chip->priv;
  566. int i = 0;
  567. int byte_pos, bit_pos, sector_size, pos;
  568. uint32_t tmp;
  569. uint8_t err_byte;
  570. sector_size = host->pmecc_sector_size;
  571. while (err_nbr) {
  572. tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1;
  573. byte_pos = tmp / 8;
  574. bit_pos = tmp % 8;
  575. if (byte_pos >= (sector_size + extra_bytes))
  576. BUG(); /* should never happen */
  577. if (byte_pos < sector_size) {
  578. err_byte = *(buf + byte_pos);
  579. *(buf + byte_pos) ^= (1 << bit_pos);
  580. pos = sector_num * host->pmecc_sector_size + byte_pos;
  581. dev_info(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
  582. pos, bit_pos, err_byte, *(buf + byte_pos));
  583. } else {
  584. /* Bit flip in OOB area */
  585. tmp = sector_num * host->pmecc_bytes_per_sector
  586. + (byte_pos - sector_size);
  587. err_byte = ecc[tmp];
  588. ecc[tmp] ^= (1 << bit_pos);
  589. pos = tmp + nand_chip->ecc.layout->eccpos[0];
  590. dev_info(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
  591. pos, bit_pos, err_byte, ecc[tmp]);
  592. }
  593. i++;
  594. err_nbr--;
  595. }
  596. return;
  597. }
  598. static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
  599. u8 *ecc)
  600. {
  601. struct nand_chip *nand_chip = mtd->priv;
  602. struct atmel_nand_host *host = nand_chip->priv;
  603. int i, err_nbr, eccbytes;
  604. uint8_t *buf_pos;
  605. eccbytes = nand_chip->ecc.bytes;
  606. for (i = 0; i < eccbytes; i++)
  607. if (ecc[i] != 0xff)
  608. goto normal_check;
  609. /* Erased page, return OK */
  610. return 0;
  611. normal_check:
  612. for (i = 0; i < host->pmecc_sector_number; i++) {
  613. err_nbr = 0;
  614. if (pmecc_stat & 0x1) {
  615. buf_pos = buf + i * host->pmecc_sector_size;
  616. pmecc_gen_syndrome(mtd, i);
  617. pmecc_substitute(mtd);
  618. pmecc_get_sigma(mtd);
  619. err_nbr = pmecc_err_location(mtd);
  620. if (err_nbr == -1) {
  621. dev_err(host->dev, "PMECC: Too many errors\n");
  622. mtd->ecc_stats.failed++;
  623. return -EIO;
  624. } else {
  625. pmecc_correct_data(mtd, buf_pos, ecc, i,
  626. host->pmecc_bytes_per_sector, err_nbr);
  627. mtd->ecc_stats.corrected += err_nbr;
  628. }
  629. }
  630. pmecc_stat >>= 1;
  631. }
  632. return 0;
  633. }
  634. static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
  635. struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
  636. {
  637. struct atmel_nand_host *host = chip->priv;
  638. int eccsize = chip->ecc.size;
  639. uint8_t *oob = chip->oob_poi;
  640. uint32_t *eccpos = chip->ecc.layout->eccpos;
  641. uint32_t stat;
  642. unsigned long end_time;
  643. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
  644. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  645. pmecc_writel(host->ecc, CFG, (pmecc_readl_relaxed(host->ecc, CFG)
  646. & ~PMECC_CFG_WRITE_OP) | PMECC_CFG_AUTO_ENABLE);
  647. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
  648. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
  649. chip->read_buf(mtd, buf, eccsize);
  650. chip->read_buf(mtd, oob, mtd->oobsize);
  651. end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
  652. while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
  653. if (unlikely(time_after(jiffies, end_time))) {
  654. dev_err(host->dev, "PMECC: Timeout to get error status.\n");
  655. return -EIO;
  656. }
  657. cpu_relax();
  658. }
  659. stat = pmecc_readl_relaxed(host->ecc, ISR);
  660. if (stat != 0)
  661. if (pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]) != 0)
  662. return -EIO;
  663. return 0;
  664. }
  665. static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
  666. struct nand_chip *chip, const uint8_t *buf, int oob_required)
  667. {
  668. struct atmel_nand_host *host = chip->priv;
  669. uint32_t *eccpos = chip->ecc.layout->eccpos;
  670. int i, j;
  671. unsigned long end_time;
  672. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
  673. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  674. pmecc_writel(host->ecc, CFG, (pmecc_readl_relaxed(host->ecc, CFG) |
  675. PMECC_CFG_WRITE_OP) & ~PMECC_CFG_AUTO_ENABLE);
  676. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
  677. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
  678. chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
  679. end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
  680. while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
  681. if (unlikely(time_after(jiffies, end_time))) {
  682. dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
  683. return -EIO;
  684. }
  685. cpu_relax();
  686. }
  687. for (i = 0; i < host->pmecc_sector_number; i++) {
  688. for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
  689. int pos;
  690. pos = i * host->pmecc_bytes_per_sector + j;
  691. chip->oob_poi[eccpos[pos]] =
  692. pmecc_readb_ecc_relaxed(host->ecc, i, j);
  693. }
  694. }
  695. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  696. return 0;
  697. }
  698. static void atmel_pmecc_core_init(struct mtd_info *mtd)
  699. {
  700. struct nand_chip *nand_chip = mtd->priv;
  701. struct atmel_nand_host *host = nand_chip->priv;
  702. uint32_t val = 0;
  703. struct nand_ecclayout *ecc_layout;
  704. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
  705. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  706. switch (host->pmecc_corr_cap) {
  707. case 2:
  708. val = PMECC_CFG_BCH_ERR2;
  709. break;
  710. case 4:
  711. val = PMECC_CFG_BCH_ERR4;
  712. break;
  713. case 8:
  714. val = PMECC_CFG_BCH_ERR8;
  715. break;
  716. case 12:
  717. val = PMECC_CFG_BCH_ERR12;
  718. break;
  719. case 24:
  720. val = PMECC_CFG_BCH_ERR24;
  721. break;
  722. }
  723. if (host->pmecc_sector_size == 512)
  724. val |= PMECC_CFG_SECTOR512;
  725. else if (host->pmecc_sector_size == 1024)
  726. val |= PMECC_CFG_SECTOR1024;
  727. switch (host->pmecc_sector_number) {
  728. case 1:
  729. val |= PMECC_CFG_PAGE_1SECTOR;
  730. break;
  731. case 2:
  732. val |= PMECC_CFG_PAGE_2SECTORS;
  733. break;
  734. case 4:
  735. val |= PMECC_CFG_PAGE_4SECTORS;
  736. break;
  737. case 8:
  738. val |= PMECC_CFG_PAGE_8SECTORS;
  739. break;
  740. }
  741. val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
  742. | PMECC_CFG_AUTO_DISABLE);
  743. pmecc_writel(host->ecc, CFG, val);
  744. ecc_layout = nand_chip->ecc.layout;
  745. pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
  746. pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
  747. pmecc_writel(host->ecc, EADDR,
  748. ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
  749. /* See datasheet about PMECC Clock Control Register */
  750. pmecc_writel(host->ecc, CLK, 2);
  751. pmecc_writel(host->ecc, IDR, 0xff);
  752. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
  753. }
  754. static int __init atmel_pmecc_nand_init_params(struct platform_device *pdev,
  755. struct atmel_nand_host *host)
  756. {
  757. struct mtd_info *mtd = &host->mtd;
  758. struct nand_chip *nand_chip = &host->nand_chip;
  759. struct resource *regs, *regs_pmerr, *regs_rom;
  760. int cap, sector_size, err_no;
  761. cap = host->pmecc_corr_cap;
  762. sector_size = host->pmecc_sector_size;
  763. dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
  764. cap, sector_size);
  765. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  766. if (!regs) {
  767. dev_warn(host->dev,
  768. "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
  769. nand_chip->ecc.mode = NAND_ECC_SOFT;
  770. return 0;
  771. }
  772. host->ecc = ioremap(regs->start, resource_size(regs));
  773. if (host->ecc == NULL) {
  774. dev_err(host->dev, "ioremap failed\n");
  775. err_no = -EIO;
  776. goto err_pmecc_ioremap;
  777. }
  778. regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  779. regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
  780. if (regs_pmerr && regs_rom) {
  781. host->pmerrloc_base = ioremap(regs_pmerr->start,
  782. resource_size(regs_pmerr));
  783. host->pmecc_rom_base = ioremap(regs_rom->start,
  784. resource_size(regs_rom));
  785. }
  786. if (!host->pmerrloc_base || !host->pmecc_rom_base) {
  787. dev_err(host->dev,
  788. "Can not get I/O resource for PMECC ERRLOC controller or ROM!\n");
  789. err_no = -EIO;
  790. goto err_pmloc_ioremap;
  791. }
  792. /* ECC is calculated for the whole page (1 step) */
  793. nand_chip->ecc.size = mtd->writesize;
  794. /* set ECC page size and oob layout */
  795. switch (mtd->writesize) {
  796. case 2048:
  797. host->pmecc_degree = PMECC_GF_DIMENSION_13;
  798. host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
  799. host->pmecc_sector_number = mtd->writesize / sector_size;
  800. host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
  801. cap, sector_size);
  802. host->pmecc_alpha_to = pmecc_get_alpha_to(host);
  803. host->pmecc_index_of = host->pmecc_rom_base +
  804. host->pmecc_lookup_table_offset;
  805. nand_chip->ecc.steps = 1;
  806. nand_chip->ecc.strength = cap;
  807. nand_chip->ecc.bytes = host->pmecc_bytes_per_sector *
  808. host->pmecc_sector_number;
  809. if (nand_chip->ecc.bytes > mtd->oobsize - 2) {
  810. dev_err(host->dev, "No room for ECC bytes\n");
  811. err_no = -EINVAL;
  812. goto err_no_ecc_room;
  813. }
  814. pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
  815. mtd->oobsize,
  816. nand_chip->ecc.bytes);
  817. nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
  818. break;
  819. case 512:
  820. case 1024:
  821. case 4096:
  822. /* TODO */
  823. dev_warn(host->dev,
  824. "Unsupported page size for PMECC, use Software ECC\n");
  825. default:
  826. /* page size not handled by HW ECC */
  827. /* switching back to soft ECC */
  828. nand_chip->ecc.mode = NAND_ECC_SOFT;
  829. return 0;
  830. }
  831. /* Allocate data for PMECC computation */
  832. err_no = pmecc_data_alloc(host);
  833. if (err_no) {
  834. dev_err(host->dev,
  835. "Cannot allocate memory for PMECC computation!\n");
  836. goto err_pmecc_data_alloc;
  837. }
  838. nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
  839. nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
  840. atmel_pmecc_core_init(mtd);
  841. return 0;
  842. err_pmecc_data_alloc:
  843. err_no_ecc_room:
  844. err_pmloc_ioremap:
  845. iounmap(host->ecc);
  846. if (host->pmerrloc_base)
  847. iounmap(host->pmerrloc_base);
  848. if (host->pmecc_rom_base)
  849. iounmap(host->pmecc_rom_base);
  850. err_pmecc_ioremap:
  851. return err_no;
  852. }
  853. /*
  854. * Calculate HW ECC
  855. *
  856. * function called after a write
  857. *
  858. * mtd: MTD block structure
  859. * dat: raw data (unused)
  860. * ecc_code: buffer for ECC
  861. */
  862. static int atmel_nand_calculate(struct mtd_info *mtd,
  863. const u_char *dat, unsigned char *ecc_code)
  864. {
  865. struct nand_chip *nand_chip = mtd->priv;
  866. struct atmel_nand_host *host = nand_chip->priv;
  867. unsigned int ecc_value;
  868. /* get the first 2 ECC bytes */
  869. ecc_value = ecc_readl(host->ecc, PR);
  870. ecc_code[0] = ecc_value & 0xFF;
  871. ecc_code[1] = (ecc_value >> 8) & 0xFF;
  872. /* get the last 2 ECC bytes */
  873. ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
  874. ecc_code[2] = ecc_value & 0xFF;
  875. ecc_code[3] = (ecc_value >> 8) & 0xFF;
  876. return 0;
  877. }
  878. /*
  879. * HW ECC read page function
  880. *
  881. * mtd: mtd info structure
  882. * chip: nand chip info structure
  883. * buf: buffer to store read data
  884. * oob_required: caller expects OOB data read to chip->oob_poi
  885. */
  886. static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  887. uint8_t *buf, int oob_required, int page)
  888. {
  889. int eccsize = chip->ecc.size;
  890. int eccbytes = chip->ecc.bytes;
  891. uint32_t *eccpos = chip->ecc.layout->eccpos;
  892. uint8_t *p = buf;
  893. uint8_t *oob = chip->oob_poi;
  894. uint8_t *ecc_pos;
  895. int stat;
  896. unsigned int max_bitflips = 0;
  897. /*
  898. * Errata: ALE is incorrectly wired up to the ECC controller
  899. * on the AP7000, so it will include the address cycles in the
  900. * ECC calculation.
  901. *
  902. * Workaround: Reset the parity registers before reading the
  903. * actual data.
  904. */
  905. if (cpu_is_at32ap7000()) {
  906. struct atmel_nand_host *host = chip->priv;
  907. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  908. }
  909. /* read the page */
  910. chip->read_buf(mtd, p, eccsize);
  911. /* move to ECC position if needed */
  912. if (eccpos[0] != 0) {
  913. /* This only works on large pages
  914. * because the ECC controller waits for
  915. * NAND_CMD_RNDOUTSTART after the
  916. * NAND_CMD_RNDOUT.
  917. * anyway, for small pages, the eccpos[0] == 0
  918. */
  919. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  920. mtd->writesize + eccpos[0], -1);
  921. }
  922. /* the ECC controller needs to read the ECC just after the data */
  923. ecc_pos = oob + eccpos[0];
  924. chip->read_buf(mtd, ecc_pos, eccbytes);
  925. /* check if there's an error */
  926. stat = chip->ecc.correct(mtd, p, oob, NULL);
  927. if (stat < 0) {
  928. mtd->ecc_stats.failed++;
  929. } else {
  930. mtd->ecc_stats.corrected += stat;
  931. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  932. }
  933. /* get back to oob start (end of page) */
  934. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  935. /* read the oob */
  936. chip->read_buf(mtd, oob, mtd->oobsize);
  937. return max_bitflips;
  938. }
  939. /*
  940. * HW ECC Correction
  941. *
  942. * function called after a read
  943. *
  944. * mtd: MTD block structure
  945. * dat: raw data read from the chip
  946. * read_ecc: ECC from the chip (unused)
  947. * isnull: unused
  948. *
  949. * Detect and correct a 1 bit error for a page
  950. */
  951. static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
  952. u_char *read_ecc, u_char *isnull)
  953. {
  954. struct nand_chip *nand_chip = mtd->priv;
  955. struct atmel_nand_host *host = nand_chip->priv;
  956. unsigned int ecc_status;
  957. unsigned int ecc_word, ecc_bit;
  958. /* get the status from the Status Register */
  959. ecc_status = ecc_readl(host->ecc, SR);
  960. /* if there's no error */
  961. if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
  962. return 0;
  963. /* get error bit offset (4 bits) */
  964. ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
  965. /* get word address (12 bits) */
  966. ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
  967. ecc_word >>= 4;
  968. /* if there are multiple errors */
  969. if (ecc_status & ATMEL_ECC_MULERR) {
  970. /* check if it is a freshly erased block
  971. * (filled with 0xff) */
  972. if ((ecc_bit == ATMEL_ECC_BITADDR)
  973. && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
  974. /* the block has just been erased, return OK */
  975. return 0;
  976. }
  977. /* it doesn't seems to be a freshly
  978. * erased block.
  979. * We can't correct so many errors */
  980. dev_dbg(host->dev, "atmel_nand : multiple errors detected."
  981. " Unable to correct.\n");
  982. return -EIO;
  983. }
  984. /* if there's a single bit error : we can correct it */
  985. if (ecc_status & ATMEL_ECC_ECCERR) {
  986. /* there's nothing much to do here.
  987. * the bit error is on the ECC itself.
  988. */
  989. dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
  990. " Nothing to correct\n");
  991. return 0;
  992. }
  993. dev_dbg(host->dev, "atmel_nand : one bit error on data."
  994. " (word offset in the page :"
  995. " 0x%x bit offset : 0x%x)\n",
  996. ecc_word, ecc_bit);
  997. /* correct the error */
  998. if (nand_chip->options & NAND_BUSWIDTH_16) {
  999. /* 16 bits words */
  1000. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  1001. } else {
  1002. /* 8 bits words */
  1003. dat[ecc_word] ^= (1 << ecc_bit);
  1004. }
  1005. dev_dbg(host->dev, "atmel_nand : error corrected\n");
  1006. return 1;
  1007. }
  1008. /*
  1009. * Enable HW ECC : unused on most chips
  1010. */
  1011. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  1012. {
  1013. if (cpu_is_at32ap7000()) {
  1014. struct nand_chip *nand_chip = mtd->priv;
  1015. struct atmel_nand_host *host = nand_chip->priv;
  1016. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  1017. }
  1018. }
  1019. #if defined(CONFIG_OF)
  1020. static int __devinit atmel_of_init_port(struct atmel_nand_host *host,
  1021. struct device_node *np)
  1022. {
  1023. u32 val, table_offset;
  1024. u32 offset[2];
  1025. int ecc_mode;
  1026. struct atmel_nand_data *board = &host->board;
  1027. enum of_gpio_flags flags;
  1028. if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
  1029. if (val >= 32) {
  1030. dev_err(host->dev, "invalid addr-offset %u\n", val);
  1031. return -EINVAL;
  1032. }
  1033. board->ale = val;
  1034. }
  1035. if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
  1036. if (val >= 32) {
  1037. dev_err(host->dev, "invalid cmd-offset %u\n", val);
  1038. return -EINVAL;
  1039. }
  1040. board->cle = val;
  1041. }
  1042. ecc_mode = of_get_nand_ecc_mode(np);
  1043. board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
  1044. board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
  1045. if (of_get_nand_bus_width(np) == 16)
  1046. board->bus_width_16 = 1;
  1047. board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
  1048. board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
  1049. board->enable_pin = of_get_gpio(np, 1);
  1050. board->det_pin = of_get_gpio(np, 2);
  1051. host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
  1052. if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
  1053. return 0; /* Not using PMECC */
  1054. /* use PMECC, get correction capability, sector size and lookup
  1055. * table offset.
  1056. */
  1057. if (of_property_read_u32(np, "atmel,pmecc-cap", &val) != 0) {
  1058. dev_err(host->dev, "Cannot decide PMECC Capability\n");
  1059. return -EINVAL;
  1060. } else if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
  1061. (val != 24)) {
  1062. dev_err(host->dev,
  1063. "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
  1064. val);
  1065. return -EINVAL;
  1066. }
  1067. host->pmecc_corr_cap = (u8)val;
  1068. if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) != 0) {
  1069. dev_err(host->dev, "Cannot decide PMECC Sector Size\n");
  1070. return -EINVAL;
  1071. } else if ((val != 512) && (val != 1024)) {
  1072. dev_err(host->dev,
  1073. "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
  1074. val);
  1075. return -EINVAL;
  1076. }
  1077. host->pmecc_sector_size = (u16)val;
  1078. if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
  1079. offset, 2) != 0) {
  1080. dev_err(host->dev, "Cannot get PMECC lookup table offset\n");
  1081. return -EINVAL;
  1082. }
  1083. table_offset = host->pmecc_sector_size == 512 ? offset[0] : offset[1];
  1084. if (!table_offset) {
  1085. dev_err(host->dev, "Invalid PMECC lookup table offset\n");
  1086. return -EINVAL;
  1087. }
  1088. host->pmecc_lookup_table_offset = table_offset;
  1089. return 0;
  1090. }
  1091. #else
  1092. static int __devinit atmel_of_init_port(struct atmel_nand_host *host,
  1093. struct device_node *np)
  1094. {
  1095. return -EINVAL;
  1096. }
  1097. #endif
  1098. static int __init atmel_hw_nand_init_params(struct platform_device *pdev,
  1099. struct atmel_nand_host *host)
  1100. {
  1101. struct mtd_info *mtd = &host->mtd;
  1102. struct nand_chip *nand_chip = &host->nand_chip;
  1103. struct resource *regs;
  1104. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1105. if (!regs) {
  1106. dev_err(host->dev,
  1107. "Can't get I/O resource regs, use software ECC\n");
  1108. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1109. return 0;
  1110. }
  1111. host->ecc = ioremap(regs->start, resource_size(regs));
  1112. if (host->ecc == NULL) {
  1113. dev_err(host->dev, "ioremap failed\n");
  1114. return -EIO;
  1115. }
  1116. /* ECC is calculated for the whole page (1 step) */
  1117. nand_chip->ecc.size = mtd->writesize;
  1118. /* set ECC page size and oob layout */
  1119. switch (mtd->writesize) {
  1120. case 512:
  1121. nand_chip->ecc.layout = &atmel_oobinfo_small;
  1122. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
  1123. break;
  1124. case 1024:
  1125. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1126. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
  1127. break;
  1128. case 2048:
  1129. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1130. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
  1131. break;
  1132. case 4096:
  1133. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1134. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
  1135. break;
  1136. default:
  1137. /* page size not handled by HW ECC */
  1138. /* switching back to soft ECC */
  1139. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1140. return 0;
  1141. }
  1142. /* set up for HW ECC */
  1143. nand_chip->ecc.calculate = atmel_nand_calculate;
  1144. nand_chip->ecc.correct = atmel_nand_correct;
  1145. nand_chip->ecc.hwctl = atmel_nand_hwctl;
  1146. nand_chip->ecc.read_page = atmel_nand_read_page;
  1147. nand_chip->ecc.bytes = 4;
  1148. nand_chip->ecc.strength = 1;
  1149. return 0;
  1150. }
  1151. /*
  1152. * Probe for the NAND device.
  1153. */
  1154. static int __init atmel_nand_probe(struct platform_device *pdev)
  1155. {
  1156. struct atmel_nand_host *host;
  1157. struct mtd_info *mtd;
  1158. struct nand_chip *nand_chip;
  1159. struct resource *mem;
  1160. struct mtd_part_parser_data ppdata = {};
  1161. int res;
  1162. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1163. if (!mem) {
  1164. printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
  1165. return -ENXIO;
  1166. }
  1167. /* Allocate memory for the device structure (and zero it) */
  1168. host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
  1169. if (!host) {
  1170. printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
  1171. return -ENOMEM;
  1172. }
  1173. host->io_phys = (dma_addr_t)mem->start;
  1174. host->io_base = ioremap(mem->start, resource_size(mem));
  1175. if (host->io_base == NULL) {
  1176. printk(KERN_ERR "atmel_nand: ioremap failed\n");
  1177. res = -EIO;
  1178. goto err_nand_ioremap;
  1179. }
  1180. mtd = &host->mtd;
  1181. nand_chip = &host->nand_chip;
  1182. host->dev = &pdev->dev;
  1183. if (pdev->dev.of_node) {
  1184. res = atmel_of_init_port(host, pdev->dev.of_node);
  1185. if (res)
  1186. goto err_ecc_ioremap;
  1187. } else {
  1188. memcpy(&host->board, pdev->dev.platform_data,
  1189. sizeof(struct atmel_nand_data));
  1190. }
  1191. nand_chip->priv = host; /* link the private data structures */
  1192. mtd->priv = nand_chip;
  1193. mtd->owner = THIS_MODULE;
  1194. /* Set address of NAND IO lines */
  1195. nand_chip->IO_ADDR_R = host->io_base;
  1196. nand_chip->IO_ADDR_W = host->io_base;
  1197. nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
  1198. if (gpio_is_valid(host->board.rdy_pin)) {
  1199. res = gpio_request(host->board.rdy_pin, "nand_rdy");
  1200. if (res < 0) {
  1201. dev_err(&pdev->dev,
  1202. "can't request rdy gpio %d\n",
  1203. host->board.rdy_pin);
  1204. goto err_ecc_ioremap;
  1205. }
  1206. res = gpio_direction_input(host->board.rdy_pin);
  1207. if (res < 0) {
  1208. dev_err(&pdev->dev,
  1209. "can't request input direction rdy gpio %d\n",
  1210. host->board.rdy_pin);
  1211. goto err_ecc_ioremap;
  1212. }
  1213. nand_chip->dev_ready = atmel_nand_device_ready;
  1214. }
  1215. if (gpio_is_valid(host->board.enable_pin)) {
  1216. res = gpio_request(host->board.enable_pin, "nand_enable");
  1217. if (res < 0) {
  1218. dev_err(&pdev->dev,
  1219. "can't request enable gpio %d\n",
  1220. host->board.enable_pin);
  1221. goto err_ecc_ioremap;
  1222. }
  1223. res = gpio_direction_output(host->board.enable_pin, 1);
  1224. if (res < 0) {
  1225. dev_err(&pdev->dev,
  1226. "can't request output direction enable gpio %d\n",
  1227. host->board.enable_pin);
  1228. goto err_ecc_ioremap;
  1229. }
  1230. }
  1231. nand_chip->ecc.mode = host->board.ecc_mode;
  1232. nand_chip->chip_delay = 20; /* 20us command delay time */
  1233. if (host->board.bus_width_16) /* 16-bit bus width */
  1234. nand_chip->options |= NAND_BUSWIDTH_16;
  1235. nand_chip->read_buf = atmel_read_buf;
  1236. nand_chip->write_buf = atmel_write_buf;
  1237. platform_set_drvdata(pdev, host);
  1238. atmel_nand_enable(host);
  1239. if (gpio_is_valid(host->board.det_pin)) {
  1240. res = gpio_request(host->board.det_pin, "nand_det");
  1241. if (res < 0) {
  1242. dev_err(&pdev->dev,
  1243. "can't request det gpio %d\n",
  1244. host->board.det_pin);
  1245. goto err_no_card;
  1246. }
  1247. res = gpio_direction_input(host->board.det_pin);
  1248. if (res < 0) {
  1249. dev_err(&pdev->dev,
  1250. "can't request input direction det gpio %d\n",
  1251. host->board.det_pin);
  1252. goto err_no_card;
  1253. }
  1254. if (gpio_get_value(host->board.det_pin)) {
  1255. printk(KERN_INFO "No SmartMedia card inserted.\n");
  1256. res = -ENXIO;
  1257. goto err_no_card;
  1258. }
  1259. }
  1260. if (host->board.on_flash_bbt || on_flash_bbt) {
  1261. printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
  1262. nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
  1263. }
  1264. if (!cpu_has_dma())
  1265. use_dma = 0;
  1266. if (use_dma) {
  1267. dma_cap_mask_t mask;
  1268. dma_cap_zero(mask);
  1269. dma_cap_set(DMA_MEMCPY, mask);
  1270. host->dma_chan = dma_request_channel(mask, NULL, NULL);
  1271. if (!host->dma_chan) {
  1272. dev_err(host->dev, "Failed to request DMA channel\n");
  1273. use_dma = 0;
  1274. }
  1275. }
  1276. if (use_dma)
  1277. dev_info(host->dev, "Using %s for DMA transfers.\n",
  1278. dma_chan_name(host->dma_chan));
  1279. else
  1280. dev_info(host->dev, "No DMA support for NAND access.\n");
  1281. /* first scan to find the device and get the page size */
  1282. if (nand_scan_ident(mtd, 1, NULL)) {
  1283. res = -ENXIO;
  1284. goto err_scan_ident;
  1285. }
  1286. if (nand_chip->ecc.mode == NAND_ECC_HW) {
  1287. if (host->has_pmecc)
  1288. res = atmel_pmecc_nand_init_params(pdev, host);
  1289. else
  1290. res = atmel_hw_nand_init_params(pdev, host);
  1291. if (res != 0)
  1292. goto err_hw_ecc;
  1293. }
  1294. /* second phase scan */
  1295. if (nand_scan_tail(mtd)) {
  1296. res = -ENXIO;
  1297. goto err_scan_tail;
  1298. }
  1299. mtd->name = "atmel_nand";
  1300. ppdata.of_node = pdev->dev.of_node;
  1301. res = mtd_device_parse_register(mtd, NULL, &ppdata,
  1302. host->board.parts, host->board.num_parts);
  1303. if (!res)
  1304. return res;
  1305. err_scan_tail:
  1306. if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
  1307. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  1308. pmecc_data_free(host);
  1309. }
  1310. if (host->ecc)
  1311. iounmap(host->ecc);
  1312. if (host->pmerrloc_base)
  1313. iounmap(host->pmerrloc_base);
  1314. if (host->pmecc_rom_base)
  1315. iounmap(host->pmecc_rom_base);
  1316. err_hw_ecc:
  1317. err_scan_ident:
  1318. err_no_card:
  1319. atmel_nand_disable(host);
  1320. platform_set_drvdata(pdev, NULL);
  1321. if (host->dma_chan)
  1322. dma_release_channel(host->dma_chan);
  1323. err_ecc_ioremap:
  1324. iounmap(host->io_base);
  1325. err_nand_ioremap:
  1326. kfree(host);
  1327. return res;
  1328. }
  1329. /*
  1330. * Remove a NAND device.
  1331. */
  1332. static int __exit atmel_nand_remove(struct platform_device *pdev)
  1333. {
  1334. struct atmel_nand_host *host = platform_get_drvdata(pdev);
  1335. struct mtd_info *mtd = &host->mtd;
  1336. nand_release(mtd);
  1337. atmel_nand_disable(host);
  1338. if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
  1339. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  1340. pmerrloc_writel(host->pmerrloc_base, ELDIS,
  1341. PMERRLOC_DISABLE);
  1342. pmecc_data_free(host);
  1343. }
  1344. if (gpio_is_valid(host->board.det_pin))
  1345. gpio_free(host->board.det_pin);
  1346. if (gpio_is_valid(host->board.enable_pin))
  1347. gpio_free(host->board.enable_pin);
  1348. if (gpio_is_valid(host->board.rdy_pin))
  1349. gpio_free(host->board.rdy_pin);
  1350. if (host->ecc)
  1351. iounmap(host->ecc);
  1352. if (host->pmecc_rom_base)
  1353. iounmap(host->pmecc_rom_base);
  1354. if (host->pmerrloc_base)
  1355. iounmap(host->pmerrloc_base);
  1356. if (host->dma_chan)
  1357. dma_release_channel(host->dma_chan);
  1358. iounmap(host->io_base);
  1359. kfree(host);
  1360. return 0;
  1361. }
  1362. #if defined(CONFIG_OF)
  1363. static const struct of_device_id atmel_nand_dt_ids[] = {
  1364. { .compatible = "atmel,at91rm9200-nand" },
  1365. { /* sentinel */ }
  1366. };
  1367. MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
  1368. #endif
  1369. static struct platform_driver atmel_nand_driver = {
  1370. .remove = __exit_p(atmel_nand_remove),
  1371. .driver = {
  1372. .name = "atmel_nand",
  1373. .owner = THIS_MODULE,
  1374. .of_match_table = of_match_ptr(atmel_nand_dt_ids),
  1375. },
  1376. };
  1377. static int __init atmel_nand_init(void)
  1378. {
  1379. return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
  1380. }
  1381. static void __exit atmel_nand_exit(void)
  1382. {
  1383. platform_driver_unregister(&atmel_nand_driver);
  1384. }
  1385. module_init(atmel_nand_init);
  1386. module_exit(atmel_nand_exit);
  1387. MODULE_LICENSE("GPL");
  1388. MODULE_AUTHOR("Rick Bronson");
  1389. MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
  1390. MODULE_ALIAS("platform:atmel_nand");