atmel_nand.c 44 KB

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