atmel_nand.c 42 KB

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