atmel_nand.c 43 KB

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