atmel_nand.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  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 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 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 __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 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. int total_err = 0;
  606. eccbytes = nand_chip->ecc.bytes;
  607. for (i = 0; i < eccbytes; i++)
  608. if (ecc[i] != 0xff)
  609. goto normal_check;
  610. /* Erased page, return OK */
  611. return 0;
  612. normal_check:
  613. for (i = 0; i < host->pmecc_sector_number; i++) {
  614. err_nbr = 0;
  615. if (pmecc_stat & 0x1) {
  616. buf_pos = buf + i * host->pmecc_sector_size;
  617. pmecc_gen_syndrome(mtd, i);
  618. pmecc_substitute(mtd);
  619. pmecc_get_sigma(mtd);
  620. err_nbr = pmecc_err_location(mtd);
  621. if (err_nbr == -1) {
  622. dev_err(host->dev, "PMECC: Too many errors\n");
  623. mtd->ecc_stats.failed++;
  624. return -EIO;
  625. } else {
  626. pmecc_correct_data(mtd, buf_pos, ecc, i,
  627. host->pmecc_bytes_per_sector, err_nbr);
  628. mtd->ecc_stats.corrected += err_nbr;
  629. total_err += err_nbr;
  630. }
  631. }
  632. pmecc_stat >>= 1;
  633. }
  634. return total_err;
  635. }
  636. static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
  637. struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
  638. {
  639. struct atmel_nand_host *host = chip->priv;
  640. int eccsize = chip->ecc.size;
  641. uint8_t *oob = chip->oob_poi;
  642. uint32_t *eccpos = chip->ecc.layout->eccpos;
  643. uint32_t stat;
  644. unsigned long end_time;
  645. int bitflips = 0;
  646. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
  647. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  648. pmecc_writel(host->ecc, CFG, (pmecc_readl_relaxed(host->ecc, CFG)
  649. & ~PMECC_CFG_WRITE_OP) | PMECC_CFG_AUTO_ENABLE);
  650. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
  651. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
  652. chip->read_buf(mtd, buf, eccsize);
  653. chip->read_buf(mtd, oob, mtd->oobsize);
  654. end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
  655. while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
  656. if (unlikely(time_after(jiffies, end_time))) {
  657. dev_err(host->dev, "PMECC: Timeout to get error status.\n");
  658. return -EIO;
  659. }
  660. cpu_relax();
  661. }
  662. stat = pmecc_readl_relaxed(host->ecc, ISR);
  663. if (stat != 0) {
  664. bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
  665. if (bitflips < 0)
  666. /* uncorrectable errors */
  667. return 0;
  668. }
  669. return bitflips;
  670. }
  671. static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
  672. struct nand_chip *chip, const uint8_t *buf, int oob_required)
  673. {
  674. struct atmel_nand_host *host = chip->priv;
  675. uint32_t *eccpos = chip->ecc.layout->eccpos;
  676. int i, j;
  677. unsigned long end_time;
  678. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
  679. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  680. pmecc_writel(host->ecc, CFG, (pmecc_readl_relaxed(host->ecc, CFG) |
  681. PMECC_CFG_WRITE_OP) & ~PMECC_CFG_AUTO_ENABLE);
  682. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
  683. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
  684. chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
  685. end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
  686. while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
  687. if (unlikely(time_after(jiffies, end_time))) {
  688. dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
  689. return -EIO;
  690. }
  691. cpu_relax();
  692. }
  693. for (i = 0; i < host->pmecc_sector_number; i++) {
  694. for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
  695. int pos;
  696. pos = i * host->pmecc_bytes_per_sector + j;
  697. chip->oob_poi[eccpos[pos]] =
  698. pmecc_readb_ecc_relaxed(host->ecc, i, j);
  699. }
  700. }
  701. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  702. return 0;
  703. }
  704. static void atmel_pmecc_core_init(struct mtd_info *mtd)
  705. {
  706. struct nand_chip *nand_chip = mtd->priv;
  707. struct atmel_nand_host *host = nand_chip->priv;
  708. uint32_t val = 0;
  709. struct nand_ecclayout *ecc_layout;
  710. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
  711. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  712. switch (host->pmecc_corr_cap) {
  713. case 2:
  714. val = PMECC_CFG_BCH_ERR2;
  715. break;
  716. case 4:
  717. val = PMECC_CFG_BCH_ERR4;
  718. break;
  719. case 8:
  720. val = PMECC_CFG_BCH_ERR8;
  721. break;
  722. case 12:
  723. val = PMECC_CFG_BCH_ERR12;
  724. break;
  725. case 24:
  726. val = PMECC_CFG_BCH_ERR24;
  727. break;
  728. }
  729. if (host->pmecc_sector_size == 512)
  730. val |= PMECC_CFG_SECTOR512;
  731. else if (host->pmecc_sector_size == 1024)
  732. val |= PMECC_CFG_SECTOR1024;
  733. switch (host->pmecc_sector_number) {
  734. case 1:
  735. val |= PMECC_CFG_PAGE_1SECTOR;
  736. break;
  737. case 2:
  738. val |= PMECC_CFG_PAGE_2SECTORS;
  739. break;
  740. case 4:
  741. val |= PMECC_CFG_PAGE_4SECTORS;
  742. break;
  743. case 8:
  744. val |= PMECC_CFG_PAGE_8SECTORS;
  745. break;
  746. }
  747. val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
  748. | PMECC_CFG_AUTO_DISABLE);
  749. pmecc_writel(host->ecc, CFG, val);
  750. ecc_layout = nand_chip->ecc.layout;
  751. pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
  752. pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
  753. pmecc_writel(host->ecc, EADDR,
  754. ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
  755. /* See datasheet about PMECC Clock Control Register */
  756. pmecc_writel(host->ecc, CLK, 2);
  757. pmecc_writel(host->ecc, IDR, 0xff);
  758. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
  759. }
  760. static int __init atmel_pmecc_nand_init_params(struct platform_device *pdev,
  761. struct atmel_nand_host *host)
  762. {
  763. struct mtd_info *mtd = &host->mtd;
  764. struct nand_chip *nand_chip = &host->nand_chip;
  765. struct resource *regs, *regs_pmerr, *regs_rom;
  766. int cap, sector_size, err_no;
  767. cap = host->pmecc_corr_cap;
  768. sector_size = host->pmecc_sector_size;
  769. dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
  770. cap, sector_size);
  771. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  772. if (!regs) {
  773. dev_warn(host->dev,
  774. "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
  775. nand_chip->ecc.mode = NAND_ECC_SOFT;
  776. return 0;
  777. }
  778. host->ecc = ioremap(regs->start, resource_size(regs));
  779. if (host->ecc == NULL) {
  780. dev_err(host->dev, "ioremap failed\n");
  781. err_no = -EIO;
  782. goto err_pmecc_ioremap;
  783. }
  784. regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  785. regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
  786. if (regs_pmerr && regs_rom) {
  787. host->pmerrloc_base = ioremap(regs_pmerr->start,
  788. resource_size(regs_pmerr));
  789. host->pmecc_rom_base = ioremap(regs_rom->start,
  790. resource_size(regs_rom));
  791. }
  792. if (!host->pmerrloc_base || !host->pmecc_rom_base) {
  793. dev_err(host->dev,
  794. "Can not get I/O resource for PMECC ERRLOC controller or ROM!\n");
  795. err_no = -EIO;
  796. goto err_pmloc_ioremap;
  797. }
  798. /* ECC is calculated for the whole page (1 step) */
  799. nand_chip->ecc.size = mtd->writesize;
  800. /* set ECC page size and oob layout */
  801. switch (mtd->writesize) {
  802. case 2048:
  803. host->pmecc_degree = PMECC_GF_DIMENSION_13;
  804. host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
  805. host->pmecc_sector_number = mtd->writesize / sector_size;
  806. host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
  807. cap, sector_size);
  808. host->pmecc_alpha_to = pmecc_get_alpha_to(host);
  809. host->pmecc_index_of = host->pmecc_rom_base +
  810. host->pmecc_lookup_table_offset;
  811. nand_chip->ecc.steps = 1;
  812. nand_chip->ecc.strength = cap;
  813. nand_chip->ecc.bytes = host->pmecc_bytes_per_sector *
  814. host->pmecc_sector_number;
  815. if (nand_chip->ecc.bytes > mtd->oobsize - 2) {
  816. dev_err(host->dev, "No room for ECC bytes\n");
  817. err_no = -EINVAL;
  818. goto err_no_ecc_room;
  819. }
  820. pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
  821. mtd->oobsize,
  822. nand_chip->ecc.bytes);
  823. nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
  824. break;
  825. case 512:
  826. case 1024:
  827. case 4096:
  828. /* TODO */
  829. dev_warn(host->dev,
  830. "Unsupported page size for PMECC, use Software ECC\n");
  831. default:
  832. /* page size not handled by HW ECC */
  833. /* switching back to soft ECC */
  834. nand_chip->ecc.mode = NAND_ECC_SOFT;
  835. return 0;
  836. }
  837. /* Allocate data for PMECC computation */
  838. err_no = pmecc_data_alloc(host);
  839. if (err_no) {
  840. dev_err(host->dev,
  841. "Cannot allocate memory for PMECC computation!\n");
  842. goto err_pmecc_data_alloc;
  843. }
  844. nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
  845. nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
  846. atmel_pmecc_core_init(mtd);
  847. return 0;
  848. err_pmecc_data_alloc:
  849. err_no_ecc_room:
  850. err_pmloc_ioremap:
  851. iounmap(host->ecc);
  852. if (host->pmerrloc_base)
  853. iounmap(host->pmerrloc_base);
  854. if (host->pmecc_rom_base)
  855. iounmap(host->pmecc_rom_base);
  856. err_pmecc_ioremap:
  857. return err_no;
  858. }
  859. /*
  860. * Calculate HW ECC
  861. *
  862. * function called after a write
  863. *
  864. * mtd: MTD block structure
  865. * dat: raw data (unused)
  866. * ecc_code: buffer for ECC
  867. */
  868. static int atmel_nand_calculate(struct mtd_info *mtd,
  869. const u_char *dat, unsigned char *ecc_code)
  870. {
  871. struct nand_chip *nand_chip = mtd->priv;
  872. struct atmel_nand_host *host = nand_chip->priv;
  873. unsigned int ecc_value;
  874. /* get the first 2 ECC bytes */
  875. ecc_value = ecc_readl(host->ecc, PR);
  876. ecc_code[0] = ecc_value & 0xFF;
  877. ecc_code[1] = (ecc_value >> 8) & 0xFF;
  878. /* get the last 2 ECC bytes */
  879. ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
  880. ecc_code[2] = ecc_value & 0xFF;
  881. ecc_code[3] = (ecc_value >> 8) & 0xFF;
  882. return 0;
  883. }
  884. /*
  885. * HW ECC read page function
  886. *
  887. * mtd: mtd info structure
  888. * chip: nand chip info structure
  889. * buf: buffer to store read data
  890. * oob_required: caller expects OOB data read to chip->oob_poi
  891. */
  892. static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  893. uint8_t *buf, int oob_required, int page)
  894. {
  895. int eccsize = chip->ecc.size;
  896. int eccbytes = chip->ecc.bytes;
  897. uint32_t *eccpos = chip->ecc.layout->eccpos;
  898. uint8_t *p = buf;
  899. uint8_t *oob = chip->oob_poi;
  900. uint8_t *ecc_pos;
  901. int stat;
  902. unsigned int max_bitflips = 0;
  903. /*
  904. * Errata: ALE is incorrectly wired up to the ECC controller
  905. * on the AP7000, so it will include the address cycles in the
  906. * ECC calculation.
  907. *
  908. * Workaround: Reset the parity registers before reading the
  909. * actual data.
  910. */
  911. if (cpu_is_at32ap7000()) {
  912. struct atmel_nand_host *host = chip->priv;
  913. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  914. }
  915. /* read the page */
  916. chip->read_buf(mtd, p, eccsize);
  917. /* move to ECC position if needed */
  918. if (eccpos[0] != 0) {
  919. /* This only works on large pages
  920. * because the ECC controller waits for
  921. * NAND_CMD_RNDOUTSTART after the
  922. * NAND_CMD_RNDOUT.
  923. * anyway, for small pages, the eccpos[0] == 0
  924. */
  925. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  926. mtd->writesize + eccpos[0], -1);
  927. }
  928. /* the ECC controller needs to read the ECC just after the data */
  929. ecc_pos = oob + eccpos[0];
  930. chip->read_buf(mtd, ecc_pos, eccbytes);
  931. /* check if there's an error */
  932. stat = chip->ecc.correct(mtd, p, oob, NULL);
  933. if (stat < 0) {
  934. mtd->ecc_stats.failed++;
  935. } else {
  936. mtd->ecc_stats.corrected += stat;
  937. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  938. }
  939. /* get back to oob start (end of page) */
  940. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  941. /* read the oob */
  942. chip->read_buf(mtd, oob, mtd->oobsize);
  943. return max_bitflips;
  944. }
  945. /*
  946. * HW ECC Correction
  947. *
  948. * function called after a read
  949. *
  950. * mtd: MTD block structure
  951. * dat: raw data read from the chip
  952. * read_ecc: ECC from the chip (unused)
  953. * isnull: unused
  954. *
  955. * Detect and correct a 1 bit error for a page
  956. */
  957. static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
  958. u_char *read_ecc, u_char *isnull)
  959. {
  960. struct nand_chip *nand_chip = mtd->priv;
  961. struct atmel_nand_host *host = nand_chip->priv;
  962. unsigned int ecc_status;
  963. unsigned int ecc_word, ecc_bit;
  964. /* get the status from the Status Register */
  965. ecc_status = ecc_readl(host->ecc, SR);
  966. /* if there's no error */
  967. if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
  968. return 0;
  969. /* get error bit offset (4 bits) */
  970. ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
  971. /* get word address (12 bits) */
  972. ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
  973. ecc_word >>= 4;
  974. /* if there are multiple errors */
  975. if (ecc_status & ATMEL_ECC_MULERR) {
  976. /* check if it is a freshly erased block
  977. * (filled with 0xff) */
  978. if ((ecc_bit == ATMEL_ECC_BITADDR)
  979. && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
  980. /* the block has just been erased, return OK */
  981. return 0;
  982. }
  983. /* it doesn't seems to be a freshly
  984. * erased block.
  985. * We can't correct so many errors */
  986. dev_dbg(host->dev, "atmel_nand : multiple errors detected."
  987. " Unable to correct.\n");
  988. return -EIO;
  989. }
  990. /* if there's a single bit error : we can correct it */
  991. if (ecc_status & ATMEL_ECC_ECCERR) {
  992. /* there's nothing much to do here.
  993. * the bit error is on the ECC itself.
  994. */
  995. dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
  996. " Nothing to correct\n");
  997. return 0;
  998. }
  999. dev_dbg(host->dev, "atmel_nand : one bit error on data."
  1000. " (word offset in the page :"
  1001. " 0x%x bit offset : 0x%x)\n",
  1002. ecc_word, ecc_bit);
  1003. /* correct the error */
  1004. if (nand_chip->options & NAND_BUSWIDTH_16) {
  1005. /* 16 bits words */
  1006. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  1007. } else {
  1008. /* 8 bits words */
  1009. dat[ecc_word] ^= (1 << ecc_bit);
  1010. }
  1011. dev_dbg(host->dev, "atmel_nand : error corrected\n");
  1012. return 1;
  1013. }
  1014. /*
  1015. * Enable HW ECC : unused on most chips
  1016. */
  1017. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  1018. {
  1019. if (cpu_is_at32ap7000()) {
  1020. struct nand_chip *nand_chip = mtd->priv;
  1021. struct atmel_nand_host *host = nand_chip->priv;
  1022. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  1023. }
  1024. }
  1025. #if defined(CONFIG_OF)
  1026. static int atmel_of_init_port(struct atmel_nand_host *host,
  1027. struct device_node *np)
  1028. {
  1029. u32 val, table_offset;
  1030. u32 offset[2];
  1031. int ecc_mode;
  1032. struct atmel_nand_data *board = &host->board;
  1033. enum of_gpio_flags flags;
  1034. if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
  1035. if (val >= 32) {
  1036. dev_err(host->dev, "invalid addr-offset %u\n", val);
  1037. return -EINVAL;
  1038. }
  1039. board->ale = val;
  1040. }
  1041. if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
  1042. if (val >= 32) {
  1043. dev_err(host->dev, "invalid cmd-offset %u\n", val);
  1044. return -EINVAL;
  1045. }
  1046. board->cle = val;
  1047. }
  1048. ecc_mode = of_get_nand_ecc_mode(np);
  1049. board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
  1050. board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
  1051. if (of_get_nand_bus_width(np) == 16)
  1052. board->bus_width_16 = 1;
  1053. board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
  1054. board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
  1055. board->enable_pin = of_get_gpio(np, 1);
  1056. board->det_pin = of_get_gpio(np, 2);
  1057. host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
  1058. if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
  1059. return 0; /* Not using PMECC */
  1060. /* use PMECC, get correction capability, sector size and lookup
  1061. * table offset.
  1062. */
  1063. if (of_property_read_u32(np, "atmel,pmecc-cap", &val) != 0) {
  1064. dev_err(host->dev, "Cannot decide PMECC Capability\n");
  1065. return -EINVAL;
  1066. } else if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
  1067. (val != 24)) {
  1068. dev_err(host->dev,
  1069. "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
  1070. val);
  1071. return -EINVAL;
  1072. }
  1073. host->pmecc_corr_cap = (u8)val;
  1074. if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) != 0) {
  1075. dev_err(host->dev, "Cannot decide PMECC Sector Size\n");
  1076. return -EINVAL;
  1077. } else if ((val != 512) && (val != 1024)) {
  1078. dev_err(host->dev,
  1079. "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
  1080. val);
  1081. return -EINVAL;
  1082. }
  1083. host->pmecc_sector_size = (u16)val;
  1084. if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
  1085. offset, 2) != 0) {
  1086. dev_err(host->dev, "Cannot get PMECC lookup table offset\n");
  1087. return -EINVAL;
  1088. }
  1089. table_offset = host->pmecc_sector_size == 512 ? offset[0] : offset[1];
  1090. if (!table_offset) {
  1091. dev_err(host->dev, "Invalid PMECC lookup table offset\n");
  1092. return -EINVAL;
  1093. }
  1094. host->pmecc_lookup_table_offset = table_offset;
  1095. return 0;
  1096. }
  1097. #else
  1098. static int atmel_of_init_port(struct atmel_nand_host *host,
  1099. struct device_node *np)
  1100. {
  1101. return -EINVAL;
  1102. }
  1103. #endif
  1104. static int __init atmel_hw_nand_init_params(struct platform_device *pdev,
  1105. struct atmel_nand_host *host)
  1106. {
  1107. struct mtd_info *mtd = &host->mtd;
  1108. struct nand_chip *nand_chip = &host->nand_chip;
  1109. struct resource *regs;
  1110. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1111. if (!regs) {
  1112. dev_err(host->dev,
  1113. "Can't get I/O resource regs, use software ECC\n");
  1114. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1115. return 0;
  1116. }
  1117. host->ecc = ioremap(regs->start, resource_size(regs));
  1118. if (host->ecc == NULL) {
  1119. dev_err(host->dev, "ioremap failed\n");
  1120. return -EIO;
  1121. }
  1122. /* ECC is calculated for the whole page (1 step) */
  1123. nand_chip->ecc.size = mtd->writesize;
  1124. /* set ECC page size and oob layout */
  1125. switch (mtd->writesize) {
  1126. case 512:
  1127. nand_chip->ecc.layout = &atmel_oobinfo_small;
  1128. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
  1129. break;
  1130. case 1024:
  1131. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1132. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
  1133. break;
  1134. case 2048:
  1135. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1136. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
  1137. break;
  1138. case 4096:
  1139. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1140. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
  1141. break;
  1142. default:
  1143. /* page size not handled by HW ECC */
  1144. /* switching back to soft ECC */
  1145. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1146. return 0;
  1147. }
  1148. /* set up for HW ECC */
  1149. nand_chip->ecc.calculate = atmel_nand_calculate;
  1150. nand_chip->ecc.correct = atmel_nand_correct;
  1151. nand_chip->ecc.hwctl = atmel_nand_hwctl;
  1152. nand_chip->ecc.read_page = atmel_nand_read_page;
  1153. nand_chip->ecc.bytes = 4;
  1154. nand_chip->ecc.strength = 1;
  1155. return 0;
  1156. }
  1157. /*
  1158. * Probe for the NAND device.
  1159. */
  1160. static int __init atmel_nand_probe(struct platform_device *pdev)
  1161. {
  1162. struct atmel_nand_host *host;
  1163. struct mtd_info *mtd;
  1164. struct nand_chip *nand_chip;
  1165. struct resource *mem;
  1166. struct mtd_part_parser_data ppdata = {};
  1167. int res;
  1168. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1169. if (!mem) {
  1170. printk(KERN_ERR "atmel_nand: can't get I/O resource mem\n");
  1171. return -ENXIO;
  1172. }
  1173. /* Allocate memory for the device structure (and zero it) */
  1174. host = kzalloc(sizeof(struct atmel_nand_host), GFP_KERNEL);
  1175. if (!host) {
  1176. printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
  1177. return -ENOMEM;
  1178. }
  1179. host->io_phys = (dma_addr_t)mem->start;
  1180. host->io_base = ioremap(mem->start, resource_size(mem));
  1181. if (host->io_base == NULL) {
  1182. printk(KERN_ERR "atmel_nand: ioremap failed\n");
  1183. res = -EIO;
  1184. goto err_nand_ioremap;
  1185. }
  1186. mtd = &host->mtd;
  1187. nand_chip = &host->nand_chip;
  1188. host->dev = &pdev->dev;
  1189. if (pdev->dev.of_node) {
  1190. res = atmel_of_init_port(host, pdev->dev.of_node);
  1191. if (res)
  1192. goto err_ecc_ioremap;
  1193. } else {
  1194. memcpy(&host->board, pdev->dev.platform_data,
  1195. sizeof(struct atmel_nand_data));
  1196. }
  1197. nand_chip->priv = host; /* link the private data structures */
  1198. mtd->priv = nand_chip;
  1199. mtd->owner = THIS_MODULE;
  1200. /* Set address of NAND IO lines */
  1201. nand_chip->IO_ADDR_R = host->io_base;
  1202. nand_chip->IO_ADDR_W = host->io_base;
  1203. nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
  1204. if (gpio_is_valid(host->board.rdy_pin)) {
  1205. res = gpio_request(host->board.rdy_pin, "nand_rdy");
  1206. if (res < 0) {
  1207. dev_err(&pdev->dev,
  1208. "can't request rdy gpio %d\n",
  1209. host->board.rdy_pin);
  1210. goto err_ecc_ioremap;
  1211. }
  1212. res = gpio_direction_input(host->board.rdy_pin);
  1213. if (res < 0) {
  1214. dev_err(&pdev->dev,
  1215. "can't request input direction rdy gpio %d\n",
  1216. host->board.rdy_pin);
  1217. goto err_ecc_ioremap;
  1218. }
  1219. nand_chip->dev_ready = atmel_nand_device_ready;
  1220. }
  1221. if (gpio_is_valid(host->board.enable_pin)) {
  1222. res = gpio_request(host->board.enable_pin, "nand_enable");
  1223. if (res < 0) {
  1224. dev_err(&pdev->dev,
  1225. "can't request enable gpio %d\n",
  1226. host->board.enable_pin);
  1227. goto err_ecc_ioremap;
  1228. }
  1229. res = gpio_direction_output(host->board.enable_pin, 1);
  1230. if (res < 0) {
  1231. dev_err(&pdev->dev,
  1232. "can't request output direction enable gpio %d\n",
  1233. host->board.enable_pin);
  1234. goto err_ecc_ioremap;
  1235. }
  1236. }
  1237. nand_chip->ecc.mode = host->board.ecc_mode;
  1238. nand_chip->chip_delay = 20; /* 20us command delay time */
  1239. if (host->board.bus_width_16) /* 16-bit bus width */
  1240. nand_chip->options |= NAND_BUSWIDTH_16;
  1241. nand_chip->read_buf = atmel_read_buf;
  1242. nand_chip->write_buf = atmel_write_buf;
  1243. platform_set_drvdata(pdev, host);
  1244. atmel_nand_enable(host);
  1245. if (gpio_is_valid(host->board.det_pin)) {
  1246. res = gpio_request(host->board.det_pin, "nand_det");
  1247. if (res < 0) {
  1248. dev_err(&pdev->dev,
  1249. "can't request det gpio %d\n",
  1250. host->board.det_pin);
  1251. goto err_no_card;
  1252. }
  1253. res = gpio_direction_input(host->board.det_pin);
  1254. if (res < 0) {
  1255. dev_err(&pdev->dev,
  1256. "can't request input direction det gpio %d\n",
  1257. host->board.det_pin);
  1258. goto err_no_card;
  1259. }
  1260. if (gpio_get_value(host->board.det_pin)) {
  1261. printk(KERN_INFO "No SmartMedia card inserted.\n");
  1262. res = -ENXIO;
  1263. goto err_no_card;
  1264. }
  1265. }
  1266. if (host->board.on_flash_bbt || on_flash_bbt) {
  1267. printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
  1268. nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
  1269. }
  1270. if (!cpu_has_dma())
  1271. use_dma = 0;
  1272. if (use_dma) {
  1273. dma_cap_mask_t mask;
  1274. dma_cap_zero(mask);
  1275. dma_cap_set(DMA_MEMCPY, mask);
  1276. host->dma_chan = dma_request_channel(mask, NULL, NULL);
  1277. if (!host->dma_chan) {
  1278. dev_err(host->dev, "Failed to request DMA channel\n");
  1279. use_dma = 0;
  1280. }
  1281. }
  1282. if (use_dma)
  1283. dev_info(host->dev, "Using %s for DMA transfers.\n",
  1284. dma_chan_name(host->dma_chan));
  1285. else
  1286. dev_info(host->dev, "No DMA support for NAND access.\n");
  1287. /* first scan to find the device and get the page size */
  1288. if (nand_scan_ident(mtd, 1, NULL)) {
  1289. res = -ENXIO;
  1290. goto err_scan_ident;
  1291. }
  1292. if (nand_chip->ecc.mode == NAND_ECC_HW) {
  1293. if (host->has_pmecc)
  1294. res = atmel_pmecc_nand_init_params(pdev, host);
  1295. else
  1296. res = atmel_hw_nand_init_params(pdev, host);
  1297. if (res != 0)
  1298. goto err_hw_ecc;
  1299. }
  1300. /* second phase scan */
  1301. if (nand_scan_tail(mtd)) {
  1302. res = -ENXIO;
  1303. goto err_scan_tail;
  1304. }
  1305. mtd->name = "atmel_nand";
  1306. ppdata.of_node = pdev->dev.of_node;
  1307. res = mtd_device_parse_register(mtd, NULL, &ppdata,
  1308. host->board.parts, host->board.num_parts);
  1309. if (!res)
  1310. return res;
  1311. err_scan_tail:
  1312. if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
  1313. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  1314. pmecc_data_free(host);
  1315. }
  1316. if (host->ecc)
  1317. iounmap(host->ecc);
  1318. if (host->pmerrloc_base)
  1319. iounmap(host->pmerrloc_base);
  1320. if (host->pmecc_rom_base)
  1321. iounmap(host->pmecc_rom_base);
  1322. err_hw_ecc:
  1323. err_scan_ident:
  1324. err_no_card:
  1325. atmel_nand_disable(host);
  1326. platform_set_drvdata(pdev, NULL);
  1327. if (host->dma_chan)
  1328. dma_release_channel(host->dma_chan);
  1329. err_ecc_ioremap:
  1330. iounmap(host->io_base);
  1331. err_nand_ioremap:
  1332. kfree(host);
  1333. return res;
  1334. }
  1335. /*
  1336. * Remove a NAND device.
  1337. */
  1338. static int __exit atmel_nand_remove(struct platform_device *pdev)
  1339. {
  1340. struct atmel_nand_host *host = platform_get_drvdata(pdev);
  1341. struct mtd_info *mtd = &host->mtd;
  1342. nand_release(mtd);
  1343. atmel_nand_disable(host);
  1344. if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
  1345. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  1346. pmerrloc_writel(host->pmerrloc_base, ELDIS,
  1347. PMERRLOC_DISABLE);
  1348. pmecc_data_free(host);
  1349. }
  1350. if (gpio_is_valid(host->board.det_pin))
  1351. gpio_free(host->board.det_pin);
  1352. if (gpio_is_valid(host->board.enable_pin))
  1353. gpio_free(host->board.enable_pin);
  1354. if (gpio_is_valid(host->board.rdy_pin))
  1355. gpio_free(host->board.rdy_pin);
  1356. if (host->ecc)
  1357. iounmap(host->ecc);
  1358. if (host->pmecc_rom_base)
  1359. iounmap(host->pmecc_rom_base);
  1360. if (host->pmerrloc_base)
  1361. iounmap(host->pmerrloc_base);
  1362. if (host->dma_chan)
  1363. dma_release_channel(host->dma_chan);
  1364. iounmap(host->io_base);
  1365. kfree(host);
  1366. return 0;
  1367. }
  1368. #if defined(CONFIG_OF)
  1369. static const struct of_device_id atmel_nand_dt_ids[] = {
  1370. { .compatible = "atmel,at91rm9200-nand" },
  1371. { /* sentinel */ }
  1372. };
  1373. MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
  1374. #endif
  1375. static struct platform_driver atmel_nand_driver = {
  1376. .remove = __exit_p(atmel_nand_remove),
  1377. .driver = {
  1378. .name = "atmel_nand",
  1379. .owner = THIS_MODULE,
  1380. .of_match_table = of_match_ptr(atmel_nand_dt_ids),
  1381. },
  1382. };
  1383. static int __init atmel_nand_init(void)
  1384. {
  1385. return platform_driver_probe(&atmel_nand_driver, atmel_nand_probe);
  1386. }
  1387. static void __exit atmel_nand_exit(void)
  1388. {
  1389. platform_driver_unregister(&atmel_nand_driver);
  1390. }
  1391. module_init(atmel_nand_init);
  1392. module_exit(atmel_nand_exit);
  1393. MODULE_LICENSE("GPL");
  1394. MODULE_AUTHOR("Rick Bronson");
  1395. MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
  1396. MODULE_ALIAS("platform:atmel_nand");