atmel_nand.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. /*
  2. * (C) Copyright 2007-2008
  3. * Stelian Pop <stelian@popies.net>
  4. * Lead Tech Design <www.leadtechdesign.com>
  5. *
  6. * (C) Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
  7. *
  8. * Add Programmable Multibit ECC support for various AT91 SoC
  9. * (C) Copyright 2012 ATMEL, Hong Xu
  10. *
  11. * SPDX-License-Identifier: GPL-2.0+
  12. */
  13. #include <common.h>
  14. #include <asm/gpio.h>
  15. #include <asm/arch/gpio.h>
  16. #include <malloc.h>
  17. #include <nand.h>
  18. #include <watchdog.h>
  19. #include <linux/mtd/nand_ecc.h>
  20. #ifdef CONFIG_ATMEL_NAND_HWECC
  21. /* Register access macros */
  22. #define ecc_readl(add, reg) \
  23. readl(AT91_BASE_SYS + add + ATMEL_ECC_##reg)
  24. #define ecc_writel(add, reg, value) \
  25. writel((value), AT91_BASE_SYS + add + ATMEL_ECC_##reg)
  26. #include "atmel_nand_ecc.h" /* Hardware ECC registers */
  27. #ifdef CONFIG_ATMEL_NAND_HW_PMECC
  28. #ifdef CONFIG_SPL_BUILD
  29. #undef CONFIG_SYS_NAND_ONFI_DETECTION
  30. #endif
  31. struct atmel_nand_host {
  32. struct pmecc_regs __iomem *pmecc;
  33. struct pmecc_errloc_regs __iomem *pmerrloc;
  34. void __iomem *pmecc_rom_base;
  35. u8 pmecc_corr_cap;
  36. u16 pmecc_sector_size;
  37. u32 pmecc_index_table_offset;
  38. int pmecc_bytes_per_sector;
  39. int pmecc_sector_number;
  40. int pmecc_degree; /* Degree of remainders */
  41. int pmecc_cw_len; /* Length of codeword */
  42. /* lookup table for alpha_to and index_of */
  43. void __iomem *pmecc_alpha_to;
  44. void __iomem *pmecc_index_of;
  45. /* data for pmecc computation */
  46. int16_t *pmecc_smu;
  47. int16_t *pmecc_partial_syn;
  48. int16_t *pmecc_si;
  49. int16_t *pmecc_lmu; /* polynomal order */
  50. int *pmecc_mu;
  51. int *pmecc_dmu;
  52. int *pmecc_delta;
  53. };
  54. static struct atmel_nand_host pmecc_host;
  55. static struct nand_ecclayout atmel_pmecc_oobinfo;
  56. /*
  57. * Return number of ecc bytes per sector according to sector size and
  58. * correction capability
  59. *
  60. * Following table shows what at91 PMECC supported:
  61. * Correction Capability Sector_512_bytes Sector_1024_bytes
  62. * ===================== ================ =================
  63. * 2-bits 4-bytes 4-bytes
  64. * 4-bits 7-bytes 7-bytes
  65. * 8-bits 13-bytes 14-bytes
  66. * 12-bits 20-bytes 21-bytes
  67. * 24-bits 39-bytes 42-bytes
  68. */
  69. static int pmecc_get_ecc_bytes(int cap, int sector_size)
  70. {
  71. int m = 12 + sector_size / 512;
  72. return (m * cap + 7) / 8;
  73. }
  74. static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
  75. int oobsize, int ecc_len)
  76. {
  77. int i;
  78. layout->eccbytes = ecc_len;
  79. /* ECC will occupy the last ecc_len bytes continuously */
  80. for (i = 0; i < ecc_len; i++)
  81. layout->eccpos[i] = oobsize - ecc_len + i;
  82. layout->oobfree[0].offset = 2;
  83. layout->oobfree[0].length =
  84. oobsize - ecc_len - layout->oobfree[0].offset;
  85. }
  86. static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
  87. {
  88. int table_size;
  89. table_size = host->pmecc_sector_size == 512 ?
  90. PMECC_INDEX_TABLE_SIZE_512 : PMECC_INDEX_TABLE_SIZE_1024;
  91. /* the ALPHA lookup table is right behind the INDEX lookup table. */
  92. return host->pmecc_rom_base + host->pmecc_index_table_offset +
  93. table_size * sizeof(int16_t);
  94. }
  95. static void pmecc_data_free(struct atmel_nand_host *host)
  96. {
  97. free(host->pmecc_partial_syn);
  98. free(host->pmecc_si);
  99. free(host->pmecc_lmu);
  100. free(host->pmecc_smu);
  101. free(host->pmecc_mu);
  102. free(host->pmecc_dmu);
  103. free(host->pmecc_delta);
  104. }
  105. static int pmecc_data_alloc(struct atmel_nand_host *host)
  106. {
  107. const int cap = host->pmecc_corr_cap;
  108. int size;
  109. size = (2 * cap + 1) * sizeof(int16_t);
  110. host->pmecc_partial_syn = malloc(size);
  111. host->pmecc_si = malloc(size);
  112. host->pmecc_lmu = malloc((cap + 1) * sizeof(int16_t));
  113. host->pmecc_smu = malloc((cap + 2) * size);
  114. size = (cap + 1) * sizeof(int);
  115. host->pmecc_mu = malloc(size);
  116. host->pmecc_dmu = malloc(size);
  117. host->pmecc_delta = malloc(size);
  118. if (host->pmecc_partial_syn &&
  119. host->pmecc_si &&
  120. host->pmecc_lmu &&
  121. host->pmecc_smu &&
  122. host->pmecc_mu &&
  123. host->pmecc_dmu &&
  124. host->pmecc_delta)
  125. return 0;
  126. /* error happened */
  127. pmecc_data_free(host);
  128. return -ENOMEM;
  129. }
  130. static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
  131. {
  132. struct nand_chip *nand_chip = mtd->priv;
  133. struct atmel_nand_host *host = nand_chip->priv;
  134. int i;
  135. uint32_t value;
  136. /* Fill odd syndromes */
  137. for (i = 0; i < host->pmecc_corr_cap; i++) {
  138. value = pmecc_readl(host->pmecc, rem_port[sector].rem[i / 2]);
  139. if (i & 1)
  140. value >>= 16;
  141. value &= 0xffff;
  142. host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
  143. }
  144. }
  145. static void pmecc_substitute(struct mtd_info *mtd)
  146. {
  147. struct nand_chip *nand_chip = mtd->priv;
  148. struct atmel_nand_host *host = nand_chip->priv;
  149. int16_t __iomem *alpha_to = host->pmecc_alpha_to;
  150. int16_t __iomem *index_of = host->pmecc_index_of;
  151. int16_t *partial_syn = host->pmecc_partial_syn;
  152. const int cap = host->pmecc_corr_cap;
  153. int16_t *si;
  154. int i, j;
  155. /* si[] is a table that holds the current syndrome value,
  156. * an element of that table belongs to the field
  157. */
  158. si = host->pmecc_si;
  159. memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
  160. /* Computation 2t syndromes based on S(x) */
  161. /* Odd syndromes */
  162. for (i = 1; i < 2 * cap; i += 2) {
  163. for (j = 0; j < host->pmecc_degree; j++) {
  164. if (partial_syn[i] & (0x1 << j))
  165. si[i] = readw(alpha_to + i * j) ^ si[i];
  166. }
  167. }
  168. /* Even syndrome = (Odd syndrome) ** 2 */
  169. for (i = 2, j = 1; j <= cap; i = ++j << 1) {
  170. if (si[j] == 0) {
  171. si[i] = 0;
  172. } else {
  173. int16_t tmp;
  174. tmp = readw(index_of + si[j]);
  175. tmp = (tmp * 2) % host->pmecc_cw_len;
  176. si[i] = readw(alpha_to + tmp);
  177. }
  178. }
  179. }
  180. /*
  181. * This function defines a Berlekamp iterative procedure for
  182. * finding the value of the error location polynomial.
  183. * The input is si[], initialize by pmecc_substitute().
  184. * The output is smu[][].
  185. *
  186. * This function is written according to chip datasheet Chapter:
  187. * Find the Error Location Polynomial Sigma(x) of Section:
  188. * Programmable Multibit ECC Control (PMECC).
  189. */
  190. static void pmecc_get_sigma(struct mtd_info *mtd)
  191. {
  192. struct nand_chip *nand_chip = mtd->priv;
  193. struct atmel_nand_host *host = nand_chip->priv;
  194. int16_t *lmu = host->pmecc_lmu;
  195. int16_t *si = host->pmecc_si;
  196. int *mu = host->pmecc_mu;
  197. int *dmu = host->pmecc_dmu; /* Discrepancy */
  198. int *delta = host->pmecc_delta; /* Delta order */
  199. int cw_len = host->pmecc_cw_len;
  200. const int16_t cap = host->pmecc_corr_cap;
  201. const int num = 2 * cap + 1;
  202. int16_t __iomem *index_of = host->pmecc_index_of;
  203. int16_t __iomem *alpha_to = host->pmecc_alpha_to;
  204. int i, j, k;
  205. uint32_t dmu_0_count, tmp;
  206. int16_t *smu = host->pmecc_smu;
  207. /* index of largest delta */
  208. int ro;
  209. int largest;
  210. int diff;
  211. /* Init the Sigma(x) */
  212. memset(smu, 0, sizeof(int16_t) * ARRAY_SIZE(smu));
  213. dmu_0_count = 0;
  214. /* First Row */
  215. /* Mu */
  216. mu[0] = -1;
  217. smu[0] = 1;
  218. /* discrepancy set to 1 */
  219. dmu[0] = 1;
  220. /* polynom order set to 0 */
  221. lmu[0] = 0;
  222. /* delta[0] = (mu[0] * 2 - lmu[0]) >> 1; */
  223. delta[0] = -1;
  224. /* Second Row */
  225. /* Mu */
  226. mu[1] = 0;
  227. /* Sigma(x) set to 1 */
  228. smu[num] = 1;
  229. /* discrepancy set to S1 */
  230. dmu[1] = si[1];
  231. /* polynom order set to 0 */
  232. lmu[1] = 0;
  233. /* delta[1] = (mu[1] * 2 - lmu[1]) >> 1; */
  234. delta[1] = 0;
  235. for (i = 1; i <= cap; i++) {
  236. mu[i + 1] = i << 1;
  237. /* Begin Computing Sigma (Mu+1) and L(mu) */
  238. /* check if discrepancy is set to 0 */
  239. if (dmu[i] == 0) {
  240. dmu_0_count++;
  241. tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
  242. if ((cap - (lmu[i] >> 1) - 1) & 0x1)
  243. tmp += 2;
  244. else
  245. tmp += 1;
  246. if (dmu_0_count == tmp) {
  247. for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
  248. smu[(cap + 1) * num + j] =
  249. smu[i * num + j];
  250. lmu[cap + 1] = lmu[i];
  251. return;
  252. }
  253. /* copy polynom */
  254. for (j = 0; j <= lmu[i] >> 1; j++)
  255. smu[(i + 1) * num + j] = smu[i * num + j];
  256. /* copy previous polynom order to the next */
  257. lmu[i + 1] = lmu[i];
  258. } else {
  259. ro = 0;
  260. largest = -1;
  261. /* find largest delta with dmu != 0 */
  262. for (j = 0; j < i; j++) {
  263. if ((dmu[j]) && (delta[j] > largest)) {
  264. largest = delta[j];
  265. ro = j;
  266. }
  267. }
  268. /* compute difference */
  269. diff = (mu[i] - mu[ro]);
  270. /* Compute degree of the new smu polynomial */
  271. if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
  272. lmu[i + 1] = lmu[i];
  273. else
  274. lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
  275. /* Init smu[i+1] with 0 */
  276. for (k = 0; k < num; k++)
  277. smu[(i + 1) * num + k] = 0;
  278. /* Compute smu[i+1] */
  279. for (k = 0; k <= lmu[ro] >> 1; k++) {
  280. int16_t a, b, c;
  281. if (!(smu[ro * num + k] && dmu[i]))
  282. continue;
  283. a = readw(index_of + dmu[i]);
  284. b = readw(index_of + dmu[ro]);
  285. c = readw(index_of + smu[ro * num + k]);
  286. tmp = a + (cw_len - b) + c;
  287. a = readw(alpha_to + tmp % cw_len);
  288. smu[(i + 1) * num + (k + diff)] = a;
  289. }
  290. for (k = 0; k <= lmu[i] >> 1; k++)
  291. smu[(i + 1) * num + k] ^= smu[i * num + k];
  292. }
  293. /* End Computing Sigma (Mu+1) and L(mu) */
  294. /* In either case compute delta */
  295. delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
  296. /* Do not compute discrepancy for the last iteration */
  297. if (i >= cap)
  298. continue;
  299. for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
  300. tmp = 2 * (i - 1);
  301. if (k == 0) {
  302. dmu[i + 1] = si[tmp + 3];
  303. } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
  304. int16_t a, b, c;
  305. a = readw(index_of +
  306. smu[(i + 1) * num + k]);
  307. b = si[2 * (i - 1) + 3 - k];
  308. c = readw(index_of + b);
  309. tmp = a + c;
  310. tmp %= cw_len;
  311. dmu[i + 1] = readw(alpha_to + tmp) ^
  312. dmu[i + 1];
  313. }
  314. }
  315. }
  316. }
  317. static int pmecc_err_location(struct mtd_info *mtd)
  318. {
  319. struct nand_chip *nand_chip = mtd->priv;
  320. struct atmel_nand_host *host = nand_chip->priv;
  321. const int cap = host->pmecc_corr_cap;
  322. const int num = 2 * cap + 1;
  323. int sector_size = host->pmecc_sector_size;
  324. int err_nbr = 0; /* number of error */
  325. int roots_nbr; /* number of roots */
  326. int i;
  327. uint32_t val;
  328. int16_t *smu = host->pmecc_smu;
  329. int timeout = PMECC_MAX_TIMEOUT_US;
  330. pmecc_writel(host->pmerrloc, eldis, PMERRLOC_DISABLE);
  331. for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
  332. pmecc_writel(host->pmerrloc, sigma[i],
  333. smu[(cap + 1) * num + i]);
  334. err_nbr++;
  335. }
  336. val = PMERRLOC_ELCFG_NUM_ERRORS(err_nbr - 1);
  337. if (sector_size == 1024)
  338. val |= PMERRLOC_ELCFG_SECTOR_1024;
  339. pmecc_writel(host->pmerrloc, elcfg, val);
  340. pmecc_writel(host->pmerrloc, elen,
  341. sector_size * 8 + host->pmecc_degree * cap);
  342. while (--timeout) {
  343. if (pmecc_readl(host->pmerrloc, elisr) & PMERRLOC_CALC_DONE)
  344. break;
  345. WATCHDOG_RESET();
  346. udelay(1);
  347. }
  348. if (!timeout) {
  349. dev_err(host->dev, "atmel_nand : Timeout to calculate PMECC error location\n");
  350. return -1;
  351. }
  352. roots_nbr = (pmecc_readl(host->pmerrloc, elisr) & PMERRLOC_ERR_NUM_MASK)
  353. >> 8;
  354. /* Number of roots == degree of smu hence <= cap */
  355. if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
  356. return err_nbr - 1;
  357. /* Number of roots does not match the degree of smu
  358. * unable to correct error */
  359. return -1;
  360. }
  361. static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
  362. int sector_num, int extra_bytes, int err_nbr)
  363. {
  364. struct nand_chip *nand_chip = mtd->priv;
  365. struct atmel_nand_host *host = nand_chip->priv;
  366. int i = 0;
  367. int byte_pos, bit_pos, sector_size, pos;
  368. uint32_t tmp;
  369. uint8_t err_byte;
  370. sector_size = host->pmecc_sector_size;
  371. while (err_nbr) {
  372. tmp = pmecc_readl(host->pmerrloc, el[i]) - 1;
  373. byte_pos = tmp / 8;
  374. bit_pos = tmp % 8;
  375. if (byte_pos >= (sector_size + extra_bytes))
  376. BUG(); /* should never happen */
  377. if (byte_pos < sector_size) {
  378. err_byte = *(buf + byte_pos);
  379. *(buf + byte_pos) ^= (1 << bit_pos);
  380. pos = sector_num * host->pmecc_sector_size + byte_pos;
  381. dev_dbg(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
  382. pos, bit_pos, err_byte, *(buf + byte_pos));
  383. } else {
  384. /* Bit flip in OOB area */
  385. tmp = sector_num * host->pmecc_bytes_per_sector
  386. + (byte_pos - sector_size);
  387. err_byte = ecc[tmp];
  388. ecc[tmp] ^= (1 << bit_pos);
  389. pos = tmp + nand_chip->ecc.layout->eccpos[0];
  390. dev_dbg(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
  391. pos, bit_pos, err_byte, ecc[tmp]);
  392. }
  393. i++;
  394. err_nbr--;
  395. }
  396. return;
  397. }
  398. static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
  399. u8 *ecc)
  400. {
  401. struct nand_chip *nand_chip = mtd->priv;
  402. struct atmel_nand_host *host = nand_chip->priv;
  403. int i, err_nbr, eccbytes;
  404. uint8_t *buf_pos;
  405. eccbytes = nand_chip->ecc.bytes;
  406. for (i = 0; i < eccbytes; i++)
  407. if (ecc[i] != 0xff)
  408. goto normal_check;
  409. /* Erased page, return OK */
  410. return 0;
  411. normal_check:
  412. for (i = 0; i < host->pmecc_sector_number; i++) {
  413. err_nbr = 0;
  414. if (pmecc_stat & 0x1) {
  415. buf_pos = buf + i * host->pmecc_sector_size;
  416. pmecc_gen_syndrome(mtd, i);
  417. pmecc_substitute(mtd);
  418. pmecc_get_sigma(mtd);
  419. err_nbr = pmecc_err_location(mtd);
  420. if (err_nbr == -1) {
  421. dev_err(host->dev, "PMECC: Too many errors\n");
  422. mtd->ecc_stats.failed++;
  423. return -EIO;
  424. } else {
  425. pmecc_correct_data(mtd, buf_pos, ecc, i,
  426. host->pmecc_bytes_per_sector, err_nbr);
  427. mtd->ecc_stats.corrected += err_nbr;
  428. }
  429. }
  430. pmecc_stat >>= 1;
  431. }
  432. return 0;
  433. }
  434. static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
  435. struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
  436. {
  437. struct atmel_nand_host *host = chip->priv;
  438. int eccsize = chip->ecc.size;
  439. uint8_t *oob = chip->oob_poi;
  440. uint32_t *eccpos = chip->ecc.layout->eccpos;
  441. uint32_t stat;
  442. int timeout = PMECC_MAX_TIMEOUT_US;
  443. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
  444. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
  445. pmecc_writel(host->pmecc, cfg, ((pmecc_readl(host->pmecc, cfg))
  446. & ~PMECC_CFG_WRITE_OP) | PMECC_CFG_AUTO_ENABLE);
  447. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
  448. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DATA);
  449. chip->read_buf(mtd, buf, eccsize);
  450. chip->read_buf(mtd, oob, mtd->oobsize);
  451. while (--timeout) {
  452. if (!(pmecc_readl(host->pmecc, sr) & PMECC_SR_BUSY))
  453. break;
  454. WATCHDOG_RESET();
  455. udelay(1);
  456. }
  457. if (!timeout) {
  458. dev_err(host->dev, "atmel_nand : Timeout to read PMECC page\n");
  459. return -1;
  460. }
  461. stat = pmecc_readl(host->pmecc, isr);
  462. if (stat != 0)
  463. if (pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]) != 0)
  464. return -EIO;
  465. return 0;
  466. }
  467. static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
  468. struct nand_chip *chip, const uint8_t *buf,
  469. int oob_required)
  470. {
  471. struct atmel_nand_host *host = chip->priv;
  472. uint32_t *eccpos = chip->ecc.layout->eccpos;
  473. int i, j;
  474. int timeout = PMECC_MAX_TIMEOUT_US;
  475. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
  476. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
  477. pmecc_writel(host->pmecc, cfg, (pmecc_readl(host->pmecc, cfg) |
  478. PMECC_CFG_WRITE_OP) & ~PMECC_CFG_AUTO_ENABLE);
  479. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
  480. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DATA);
  481. chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
  482. while (--timeout) {
  483. if (!(pmecc_readl(host->pmecc, sr) & PMECC_SR_BUSY))
  484. break;
  485. WATCHDOG_RESET();
  486. udelay(1);
  487. }
  488. if (!timeout) {
  489. dev_err(host->dev, "atmel_nand : Timeout to read PMECC status, fail to write PMECC in oob\n");
  490. goto out;
  491. }
  492. for (i = 0; i < host->pmecc_sector_number; i++) {
  493. for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
  494. int pos;
  495. pos = i * host->pmecc_bytes_per_sector + j;
  496. chip->oob_poi[eccpos[pos]] =
  497. pmecc_readb(host->pmecc, ecc_port[i].ecc[j]);
  498. }
  499. }
  500. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  501. out:
  502. return 0;
  503. }
  504. static void atmel_pmecc_core_init(struct mtd_info *mtd)
  505. {
  506. struct nand_chip *nand_chip = mtd->priv;
  507. struct atmel_nand_host *host = nand_chip->priv;
  508. uint32_t val = 0;
  509. struct nand_ecclayout *ecc_layout;
  510. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
  511. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
  512. switch (host->pmecc_corr_cap) {
  513. case 2:
  514. val = PMECC_CFG_BCH_ERR2;
  515. break;
  516. case 4:
  517. val = PMECC_CFG_BCH_ERR4;
  518. break;
  519. case 8:
  520. val = PMECC_CFG_BCH_ERR8;
  521. break;
  522. case 12:
  523. val = PMECC_CFG_BCH_ERR12;
  524. break;
  525. case 24:
  526. val = PMECC_CFG_BCH_ERR24;
  527. break;
  528. }
  529. if (host->pmecc_sector_size == 512)
  530. val |= PMECC_CFG_SECTOR512;
  531. else if (host->pmecc_sector_size == 1024)
  532. val |= PMECC_CFG_SECTOR1024;
  533. switch (host->pmecc_sector_number) {
  534. case 1:
  535. val |= PMECC_CFG_PAGE_1SECTOR;
  536. break;
  537. case 2:
  538. val |= PMECC_CFG_PAGE_2SECTORS;
  539. break;
  540. case 4:
  541. val |= PMECC_CFG_PAGE_4SECTORS;
  542. break;
  543. case 8:
  544. val |= PMECC_CFG_PAGE_8SECTORS;
  545. break;
  546. }
  547. val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
  548. | PMECC_CFG_AUTO_DISABLE);
  549. pmecc_writel(host->pmecc, cfg, val);
  550. ecc_layout = nand_chip->ecc.layout;
  551. pmecc_writel(host->pmecc, sarea, mtd->oobsize - 1);
  552. pmecc_writel(host->pmecc, saddr, ecc_layout->eccpos[0]);
  553. pmecc_writel(host->pmecc, eaddr,
  554. ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
  555. /* See datasheet about PMECC Clock Control Register */
  556. pmecc_writel(host->pmecc, clk, PMECC_CLK_133MHZ);
  557. pmecc_writel(host->pmecc, idr, 0xff);
  558. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
  559. }
  560. #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
  561. /*
  562. * get_onfi_ecc_param - Get ECC requirement from ONFI parameters
  563. * @ecc_bits: store the ONFI ECC correct bits capbility
  564. * @sector_size: in how many bytes that ONFI require to correct @ecc_bits
  565. *
  566. * Returns -1 if ONFI parameters is not supported. In this case @ecc_bits,
  567. * @sector_size are initialize to 0.
  568. * Return 0 if success to get the ECC requirement.
  569. */
  570. static int get_onfi_ecc_param(struct nand_chip *chip,
  571. int *ecc_bits, int *sector_size)
  572. {
  573. *ecc_bits = *sector_size = 0;
  574. if (chip->onfi_params.ecc_bits == 0xff)
  575. /* TODO: the sector_size and ecc_bits need to be find in
  576. * extended ecc parameter, currently we don't support it.
  577. */
  578. return -1;
  579. *ecc_bits = chip->onfi_params.ecc_bits;
  580. /* The default sector size (ecc codeword size) is 512 */
  581. *sector_size = 512;
  582. return 0;
  583. }
  584. /*
  585. * pmecc_choose_ecc - Get ecc requirement from ONFI parameters. If
  586. * pmecc_corr_cap or pmecc_sector_size is 0, then set it as
  587. * ONFI ECC parameters.
  588. * @host: point to an atmel_nand_host structure.
  589. * if host->pmecc_corr_cap is 0 then set it as the ONFI ecc_bits.
  590. * if host->pmecc_sector_size is 0 then set it as the ONFI sector_size.
  591. * @chip: point to an nand_chip structure.
  592. * @cap: store the ONFI ECC correct bits capbility
  593. * @sector_size: in how many bytes that ONFI require to correct @ecc_bits
  594. *
  595. * Return 0 if success. otherwise return the error code.
  596. */
  597. static int pmecc_choose_ecc(struct atmel_nand_host *host,
  598. struct nand_chip *chip,
  599. int *cap, int *sector_size)
  600. {
  601. /* Get ECC requirement from ONFI parameters */
  602. *cap = *sector_size = 0;
  603. if (chip->onfi_version) {
  604. if (!get_onfi_ecc_param(chip, cap, sector_size)) {
  605. MTDDEBUG(MTD_DEBUG_LEVEL1, "ONFI params, minimum required ECC: %d bits in %d bytes\n",
  606. *cap, *sector_size);
  607. } else {
  608. dev_info(host->dev, "NAND chip ECC reqirement is in Extended ONFI parameter, we don't support yet.\n");
  609. }
  610. } else {
  611. dev_info(host->dev, "NAND chip is not ONFI compliant, assume ecc_bits is 2 in 512 bytes");
  612. }
  613. if (*cap == 0 && *sector_size == 0) {
  614. /* Non-ONFI compliant or use extended ONFI parameters */
  615. *cap = 2;
  616. *sector_size = 512;
  617. }
  618. /* If head file doesn't specify then use the one in ONFI parameters */
  619. if (host->pmecc_corr_cap == 0) {
  620. /* use the most fitable ecc bits (the near bigger one ) */
  621. if (*cap <= 2)
  622. host->pmecc_corr_cap = 2;
  623. else if (*cap <= 4)
  624. host->pmecc_corr_cap = 4;
  625. else if (*cap <= 8)
  626. host->pmecc_corr_cap = 8;
  627. else if (*cap <= 12)
  628. host->pmecc_corr_cap = 12;
  629. else if (*cap <= 24)
  630. host->pmecc_corr_cap = 24;
  631. else
  632. return -EINVAL;
  633. }
  634. if (host->pmecc_sector_size == 0) {
  635. /* use the most fitable sector size (the near smaller one ) */
  636. if (*sector_size >= 1024)
  637. host->pmecc_sector_size = 1024;
  638. else if (*sector_size >= 512)
  639. host->pmecc_sector_size = 512;
  640. else
  641. return -EINVAL;
  642. }
  643. return 0;
  644. }
  645. #endif
  646. #if defined(NO_GALOIS_TABLE_IN_ROM)
  647. static uint16_t *pmecc_galois_table;
  648. static inline int deg(unsigned int poly)
  649. {
  650. /* polynomial degree is the most-significant bit index */
  651. return fls(poly) - 1;
  652. }
  653. static int build_gf_tables(int mm, unsigned int poly,
  654. int16_t *index_of, int16_t *alpha_to)
  655. {
  656. unsigned int i, x = 1;
  657. const unsigned int k = 1 << deg(poly);
  658. unsigned int nn = (1 << mm) - 1;
  659. /* primitive polynomial must be of degree m */
  660. if (k != (1u << mm))
  661. return -EINVAL;
  662. for (i = 0; i < nn; i++) {
  663. alpha_to[i] = x;
  664. index_of[x] = i;
  665. if (i && (x == 1))
  666. /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
  667. return -EINVAL;
  668. x <<= 1;
  669. if (x & k)
  670. x ^= poly;
  671. }
  672. alpha_to[nn] = 1;
  673. index_of[0] = 0;
  674. return 0;
  675. }
  676. static uint16_t *create_lookup_table(int sector_size)
  677. {
  678. int degree = (sector_size == 512) ?
  679. PMECC_GF_DIMENSION_13 :
  680. PMECC_GF_DIMENSION_14;
  681. unsigned int poly = (sector_size == 512) ?
  682. PMECC_GF_13_PRIMITIVE_POLY :
  683. PMECC_GF_14_PRIMITIVE_POLY;
  684. int table_size = (sector_size == 512) ?
  685. PMECC_INDEX_TABLE_SIZE_512 :
  686. PMECC_INDEX_TABLE_SIZE_1024;
  687. int16_t *addr = kzalloc(2 * table_size * sizeof(uint16_t), GFP_KERNEL);
  688. if (addr && build_gf_tables(degree, poly, addr, addr + table_size))
  689. return NULL;
  690. return (uint16_t *)addr;
  691. }
  692. #endif
  693. static int atmel_pmecc_nand_init_params(struct nand_chip *nand,
  694. struct mtd_info *mtd)
  695. {
  696. struct atmel_nand_host *host;
  697. int cap, sector_size;
  698. host = nand->priv = &pmecc_host;
  699. nand->ecc.mode = NAND_ECC_HW;
  700. nand->ecc.calculate = NULL;
  701. nand->ecc.correct = NULL;
  702. nand->ecc.hwctl = NULL;
  703. #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
  704. host->pmecc_corr_cap = host->pmecc_sector_size = 0;
  705. #ifdef CONFIG_PMECC_CAP
  706. host->pmecc_corr_cap = CONFIG_PMECC_CAP;
  707. #endif
  708. #ifdef CONFIG_PMECC_SECTOR_SIZE
  709. host->pmecc_sector_size = CONFIG_PMECC_SECTOR_SIZE;
  710. #endif
  711. /* Get ECC requirement of ONFI parameters. And if CONFIG_PMECC_CAP or
  712. * CONFIG_PMECC_SECTOR_SIZE not defined, then use ecc_bits, sector_size
  713. * from ONFI.
  714. */
  715. if (pmecc_choose_ecc(host, nand, &cap, &sector_size)) {
  716. dev_err(host->dev, "The NAND flash's ECC requirement(ecc_bits: %d, sector_size: %d) are not support!",
  717. cap, sector_size);
  718. return -EINVAL;
  719. }
  720. if (cap > host->pmecc_corr_cap)
  721. dev_info(host->dev, "WARNING: Using different ecc correct bits(%d bit) from Nand ONFI ECC reqirement (%d bit).\n",
  722. host->pmecc_corr_cap, cap);
  723. if (sector_size < host->pmecc_sector_size)
  724. dev_info(host->dev, "WARNING: Using different ecc correct sector size (%d bytes) from Nand ONFI ECC reqirement (%d bytes).\n",
  725. host->pmecc_sector_size, sector_size);
  726. #else /* CONFIG_SYS_NAND_ONFI_DETECTION */
  727. host->pmecc_corr_cap = CONFIG_PMECC_CAP;
  728. host->pmecc_sector_size = CONFIG_PMECC_SECTOR_SIZE;
  729. #endif
  730. cap = host->pmecc_corr_cap;
  731. sector_size = host->pmecc_sector_size;
  732. /* TODO: need check whether cap & sector_size is validate */
  733. #if defined(NO_GALOIS_TABLE_IN_ROM)
  734. /*
  735. * As pmecc_rom_base is the begin of the gallois field table, So the
  736. * index offset just set as 0.
  737. */
  738. host->pmecc_index_table_offset = 0;
  739. #else
  740. if (host->pmecc_sector_size == 512)
  741. host->pmecc_index_table_offset = ATMEL_PMECC_INDEX_OFFSET_512;
  742. else
  743. host->pmecc_index_table_offset = ATMEL_PMECC_INDEX_OFFSET_1024;
  744. #endif
  745. MTDDEBUG(MTD_DEBUG_LEVEL1,
  746. "Initialize PMECC params, cap: %d, sector: %d\n",
  747. cap, sector_size);
  748. host->pmecc = (struct pmecc_regs __iomem *) ATMEL_BASE_PMECC;
  749. host->pmerrloc = (struct pmecc_errloc_regs __iomem *)
  750. ATMEL_BASE_PMERRLOC;
  751. #if defined(NO_GALOIS_TABLE_IN_ROM)
  752. pmecc_galois_table = create_lookup_table(host->pmecc_sector_size);
  753. if (!pmecc_galois_table) {
  754. dev_err(host->dev, "out of memory\n");
  755. return -ENOMEM;
  756. }
  757. host->pmecc_rom_base = (void __iomem *)pmecc_galois_table;
  758. #else
  759. host->pmecc_rom_base = (void __iomem *) ATMEL_BASE_ROM;
  760. #endif
  761. /* ECC is calculated for the whole page (1 step) */
  762. nand->ecc.size = mtd->writesize;
  763. /* set ECC page size and oob layout */
  764. switch (mtd->writesize) {
  765. case 2048:
  766. case 4096:
  767. case 8192:
  768. host->pmecc_degree = (sector_size == 512) ?
  769. PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
  770. host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
  771. host->pmecc_sector_number = mtd->writesize / sector_size;
  772. host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
  773. cap, sector_size);
  774. host->pmecc_alpha_to = pmecc_get_alpha_to(host);
  775. host->pmecc_index_of = host->pmecc_rom_base +
  776. host->pmecc_index_table_offset;
  777. nand->ecc.steps = 1;
  778. nand->ecc.bytes = host->pmecc_bytes_per_sector *
  779. host->pmecc_sector_number;
  780. if (nand->ecc.bytes > MTD_MAX_ECCPOS_ENTRIES_LARGE) {
  781. dev_err(host->dev, "too large eccpos entries. max support ecc.bytes is %d\n",
  782. MTD_MAX_ECCPOS_ENTRIES_LARGE);
  783. return -EINVAL;
  784. }
  785. if (nand->ecc.bytes > mtd->oobsize - 2) {
  786. dev_err(host->dev, "No room for ECC bytes\n");
  787. return -EINVAL;
  788. }
  789. pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
  790. mtd->oobsize,
  791. nand->ecc.bytes);
  792. nand->ecc.layout = &atmel_pmecc_oobinfo;
  793. break;
  794. case 512:
  795. case 1024:
  796. /* TODO */
  797. dev_err(host->dev, "Unsupported page size for PMECC, use Software ECC\n");
  798. default:
  799. /* page size not handled by HW ECC */
  800. /* switching back to soft ECC */
  801. nand->ecc.mode = NAND_ECC_SOFT;
  802. nand->ecc.read_page = NULL;
  803. nand->ecc.postpad = 0;
  804. nand->ecc.prepad = 0;
  805. nand->ecc.bytes = 0;
  806. return 0;
  807. }
  808. /* Allocate data for PMECC computation */
  809. if (pmecc_data_alloc(host)) {
  810. dev_err(host->dev, "Cannot allocate memory for PMECC computation!\n");
  811. return -ENOMEM;
  812. }
  813. nand->options |= NAND_NO_SUBPAGE_WRITE;
  814. nand->ecc.read_page = atmel_nand_pmecc_read_page;
  815. nand->ecc.write_page = atmel_nand_pmecc_write_page;
  816. nand->ecc.strength = cap;
  817. atmel_pmecc_core_init(mtd);
  818. return 0;
  819. }
  820. #else
  821. /* oob layout for large page size
  822. * bad block info is on bytes 0 and 1
  823. * the bytes have to be consecutives to avoid
  824. * several NAND_CMD_RNDOUT during read
  825. */
  826. static struct nand_ecclayout atmel_oobinfo_large = {
  827. .eccbytes = 4,
  828. .eccpos = {60, 61, 62, 63},
  829. .oobfree = {
  830. {2, 58}
  831. },
  832. };
  833. /* oob layout for small page size
  834. * bad block info is on bytes 4 and 5
  835. * the bytes have to be consecutives to avoid
  836. * several NAND_CMD_RNDOUT during read
  837. */
  838. static struct nand_ecclayout atmel_oobinfo_small = {
  839. .eccbytes = 4,
  840. .eccpos = {0, 1, 2, 3},
  841. .oobfree = {
  842. {6, 10}
  843. },
  844. };
  845. /*
  846. * Calculate HW ECC
  847. *
  848. * function called after a write
  849. *
  850. * mtd: MTD block structure
  851. * dat: raw data (unused)
  852. * ecc_code: buffer for ECC
  853. */
  854. static int atmel_nand_calculate(struct mtd_info *mtd,
  855. const u_char *dat, unsigned char *ecc_code)
  856. {
  857. unsigned int ecc_value;
  858. /* get the first 2 ECC bytes */
  859. ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR);
  860. ecc_code[0] = ecc_value & 0xFF;
  861. ecc_code[1] = (ecc_value >> 8) & 0xFF;
  862. /* get the last 2 ECC bytes */
  863. ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, NPR) & ATMEL_ECC_NPARITY;
  864. ecc_code[2] = ecc_value & 0xFF;
  865. ecc_code[3] = (ecc_value >> 8) & 0xFF;
  866. return 0;
  867. }
  868. /*
  869. * HW ECC read page function
  870. *
  871. * mtd: mtd info structure
  872. * chip: nand chip info structure
  873. * buf: buffer to store read data
  874. * oob_required: caller expects OOB data read to chip->oob_poi
  875. */
  876. static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  877. uint8_t *buf, int oob_required, int page)
  878. {
  879. int eccsize = chip->ecc.size;
  880. int eccbytes = chip->ecc.bytes;
  881. uint32_t *eccpos = chip->ecc.layout->eccpos;
  882. uint8_t *p = buf;
  883. uint8_t *oob = chip->oob_poi;
  884. uint8_t *ecc_pos;
  885. int stat;
  886. /* read the page */
  887. chip->read_buf(mtd, p, eccsize);
  888. /* move to ECC position if needed */
  889. if (eccpos[0] != 0) {
  890. /* This only works on large pages
  891. * because the ECC controller waits for
  892. * NAND_CMD_RNDOUTSTART after the
  893. * NAND_CMD_RNDOUT.
  894. * anyway, for small pages, the eccpos[0] == 0
  895. */
  896. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  897. mtd->writesize + eccpos[0], -1);
  898. }
  899. /* the ECC controller needs to read the ECC just after the data */
  900. ecc_pos = oob + eccpos[0];
  901. chip->read_buf(mtd, ecc_pos, eccbytes);
  902. /* check if there's an error */
  903. stat = chip->ecc.correct(mtd, p, oob, NULL);
  904. if (stat < 0)
  905. mtd->ecc_stats.failed++;
  906. else
  907. mtd->ecc_stats.corrected += stat;
  908. /* get back to oob start (end of page) */
  909. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  910. /* read the oob */
  911. chip->read_buf(mtd, oob, mtd->oobsize);
  912. return 0;
  913. }
  914. /*
  915. * HW ECC Correction
  916. *
  917. * function called after a read
  918. *
  919. * mtd: MTD block structure
  920. * dat: raw data read from the chip
  921. * read_ecc: ECC from the chip (unused)
  922. * isnull: unused
  923. *
  924. * Detect and correct a 1 bit error for a page
  925. */
  926. static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
  927. u_char *read_ecc, u_char *isnull)
  928. {
  929. struct nand_chip *nand_chip = mtd->priv;
  930. unsigned int ecc_status;
  931. unsigned int ecc_word, ecc_bit;
  932. /* get the status from the Status Register */
  933. ecc_status = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, SR);
  934. /* if there's no error */
  935. if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
  936. return 0;
  937. /* get error bit offset (4 bits) */
  938. ecc_bit = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_BITADDR;
  939. /* get word address (12 bits) */
  940. ecc_word = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_WORDADDR;
  941. ecc_word >>= 4;
  942. /* if there are multiple errors */
  943. if (ecc_status & ATMEL_ECC_MULERR) {
  944. /* check if it is a freshly erased block
  945. * (filled with 0xff) */
  946. if ((ecc_bit == ATMEL_ECC_BITADDR)
  947. && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
  948. /* the block has just been erased, return OK */
  949. return 0;
  950. }
  951. /* it doesn't seems to be a freshly
  952. * erased block.
  953. * We can't correct so many errors */
  954. dev_warn(host->dev, "atmel_nand : multiple errors detected."
  955. " Unable to correct.\n");
  956. return -EIO;
  957. }
  958. /* if there's a single bit error : we can correct it */
  959. if (ecc_status & ATMEL_ECC_ECCERR) {
  960. /* there's nothing much to do here.
  961. * the bit error is on the ECC itself.
  962. */
  963. dev_warn(host->dev, "atmel_nand : one bit error on ECC code."
  964. " Nothing to correct\n");
  965. return 0;
  966. }
  967. dev_warn(host->dev, "atmel_nand : one bit error on data."
  968. " (word offset in the page :"
  969. " 0x%x bit offset : 0x%x)\n",
  970. ecc_word, ecc_bit);
  971. /* correct the error */
  972. if (nand_chip->options & NAND_BUSWIDTH_16) {
  973. /* 16 bits words */
  974. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  975. } else {
  976. /* 8 bits words */
  977. dat[ecc_word] ^= (1 << ecc_bit);
  978. }
  979. dev_warn(host->dev, "atmel_nand : error corrected\n");
  980. return 1;
  981. }
  982. /*
  983. * Enable HW ECC : unused on most chips
  984. */
  985. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  986. {
  987. }
  988. int atmel_hwecc_nand_init_param(struct nand_chip *nand, struct mtd_info *mtd)
  989. {
  990. nand->ecc.mode = NAND_ECC_HW;
  991. nand->ecc.calculate = atmel_nand_calculate;
  992. nand->ecc.correct = atmel_nand_correct;
  993. nand->ecc.hwctl = atmel_nand_hwctl;
  994. nand->ecc.read_page = atmel_nand_read_page;
  995. nand->ecc.bytes = 4;
  996. if (nand->ecc.mode == NAND_ECC_HW) {
  997. /* ECC is calculated for the whole page (1 step) */
  998. nand->ecc.size = mtd->writesize;
  999. /* set ECC page size and oob layout */
  1000. switch (mtd->writesize) {
  1001. case 512:
  1002. nand->ecc.layout = &atmel_oobinfo_small;
  1003. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  1004. ATMEL_ECC_PAGESIZE_528);
  1005. break;
  1006. case 1024:
  1007. nand->ecc.layout = &atmel_oobinfo_large;
  1008. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  1009. ATMEL_ECC_PAGESIZE_1056);
  1010. break;
  1011. case 2048:
  1012. nand->ecc.layout = &atmel_oobinfo_large;
  1013. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  1014. ATMEL_ECC_PAGESIZE_2112);
  1015. break;
  1016. case 4096:
  1017. nand->ecc.layout = &atmel_oobinfo_large;
  1018. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  1019. ATMEL_ECC_PAGESIZE_4224);
  1020. break;
  1021. default:
  1022. /* page size not handled by HW ECC */
  1023. /* switching back to soft ECC */
  1024. nand->ecc.mode = NAND_ECC_SOFT;
  1025. nand->ecc.calculate = NULL;
  1026. nand->ecc.correct = NULL;
  1027. nand->ecc.hwctl = NULL;
  1028. nand->ecc.read_page = NULL;
  1029. nand->ecc.postpad = 0;
  1030. nand->ecc.prepad = 0;
  1031. nand->ecc.bytes = 0;
  1032. break;
  1033. }
  1034. }
  1035. return 0;
  1036. }
  1037. #endif /* CONFIG_ATMEL_NAND_HW_PMECC */
  1038. #endif /* CONFIG_ATMEL_NAND_HWECC */
  1039. static void at91_nand_hwcontrol(struct mtd_info *mtd,
  1040. int cmd, unsigned int ctrl)
  1041. {
  1042. struct nand_chip *this = mtd->priv;
  1043. if (ctrl & NAND_CTRL_CHANGE) {
  1044. ulong IO_ADDR_W = (ulong) this->IO_ADDR_W;
  1045. IO_ADDR_W &= ~(CONFIG_SYS_NAND_MASK_ALE
  1046. | CONFIG_SYS_NAND_MASK_CLE);
  1047. if (ctrl & NAND_CLE)
  1048. IO_ADDR_W |= CONFIG_SYS_NAND_MASK_CLE;
  1049. if (ctrl & NAND_ALE)
  1050. IO_ADDR_W |= CONFIG_SYS_NAND_MASK_ALE;
  1051. #ifdef CONFIG_SYS_NAND_ENABLE_PIN
  1052. gpio_set_value(CONFIG_SYS_NAND_ENABLE_PIN, !(ctrl & NAND_NCE));
  1053. #endif
  1054. this->IO_ADDR_W = (void *) IO_ADDR_W;
  1055. }
  1056. if (cmd != NAND_CMD_NONE)
  1057. writeb(cmd, this->IO_ADDR_W);
  1058. }
  1059. #ifdef CONFIG_SYS_NAND_READY_PIN
  1060. static int at91_nand_ready(struct mtd_info *mtd)
  1061. {
  1062. return gpio_get_value(CONFIG_SYS_NAND_READY_PIN);
  1063. }
  1064. #endif
  1065. #ifdef CONFIG_SPL_BUILD
  1066. /* The following code is for SPL */
  1067. static nand_info_t mtd;
  1068. static struct nand_chip nand_chip;
  1069. static int nand_command(int block, int page, uint32_t offs, u8 cmd)
  1070. {
  1071. struct nand_chip *this = mtd.priv;
  1072. int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
  1073. void (*hwctrl)(struct mtd_info *mtd, int cmd,
  1074. unsigned int ctrl) = this->cmd_ctrl;
  1075. while (!this->dev_ready(&mtd))
  1076. ;
  1077. if (cmd == NAND_CMD_READOOB) {
  1078. offs += CONFIG_SYS_NAND_PAGE_SIZE;
  1079. cmd = NAND_CMD_READ0;
  1080. }
  1081. hwctrl(&mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  1082. if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
  1083. offs >>= 1;
  1084. hwctrl(&mtd, offs & 0xff, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
  1085. hwctrl(&mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE);
  1086. hwctrl(&mtd, (page_addr & 0xff), NAND_CTRL_ALE);
  1087. hwctrl(&mtd, ((page_addr >> 8) & 0xff), NAND_CTRL_ALE);
  1088. #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
  1089. hwctrl(&mtd, (page_addr >> 16) & 0x0f, NAND_CTRL_ALE);
  1090. #endif
  1091. hwctrl(&mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  1092. hwctrl(&mtd, NAND_CMD_READSTART, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  1093. hwctrl(&mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
  1094. while (!this->dev_ready(&mtd))
  1095. ;
  1096. return 0;
  1097. }
  1098. static int nand_is_bad_block(int block)
  1099. {
  1100. struct nand_chip *this = mtd.priv;
  1101. nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS, NAND_CMD_READOOB);
  1102. if (this->options & NAND_BUSWIDTH_16) {
  1103. if (readw(this->IO_ADDR_R) != 0xffff)
  1104. return 1;
  1105. } else {
  1106. if (readb(this->IO_ADDR_R) != 0xff)
  1107. return 1;
  1108. }
  1109. return 0;
  1110. }
  1111. #ifdef CONFIG_SPL_NAND_ECC
  1112. static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
  1113. #define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \
  1114. CONFIG_SYS_NAND_ECCSIZE)
  1115. #define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES)
  1116. static int nand_read_page(int block, int page, void *dst)
  1117. {
  1118. struct nand_chip *this = mtd.priv;
  1119. u_char ecc_calc[ECCTOTAL];
  1120. u_char ecc_code[ECCTOTAL];
  1121. u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
  1122. int eccsize = CONFIG_SYS_NAND_ECCSIZE;
  1123. int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
  1124. int eccsteps = ECCSTEPS;
  1125. int i;
  1126. uint8_t *p = dst;
  1127. nand_command(block, page, 0, NAND_CMD_READ0);
  1128. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
  1129. if (this->ecc.mode != NAND_ECC_SOFT)
  1130. this->ecc.hwctl(&mtd, NAND_ECC_READ);
  1131. this->read_buf(&mtd, p, eccsize);
  1132. this->ecc.calculate(&mtd, p, &ecc_calc[i]);
  1133. }
  1134. this->read_buf(&mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
  1135. for (i = 0; i < ECCTOTAL; i++)
  1136. ecc_code[i] = oob_data[nand_ecc_pos[i]];
  1137. eccsteps = ECCSTEPS;
  1138. p = dst;
  1139. for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
  1140. this->ecc.correct(&mtd, p, &ecc_code[i], &ecc_calc[i]);
  1141. return 0;
  1142. }
  1143. int spl_nand_erase_one(int block, int page)
  1144. {
  1145. struct nand_chip *this = mtd.priv;
  1146. void (*hwctrl)(struct mtd_info *mtd, int cmd,
  1147. unsigned int ctrl) = this->cmd_ctrl;
  1148. int page_addr;
  1149. if (nand_chip.select_chip)
  1150. nand_chip.select_chip(&mtd, 0);
  1151. page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
  1152. hwctrl(&mtd, NAND_CMD_ERASE1, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  1153. /* Row address */
  1154. hwctrl(&mtd, (page_addr & 0xff), NAND_CTRL_ALE | NAND_CTRL_CHANGE);
  1155. hwctrl(&mtd, ((page_addr >> 8) & 0xff),
  1156. NAND_CTRL_ALE | NAND_CTRL_CHANGE);
  1157. #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
  1158. /* One more address cycle for devices > 128MiB */
  1159. hwctrl(&mtd, (page_addr >> 16) & 0x0f,
  1160. NAND_CTRL_ALE | NAND_CTRL_CHANGE);
  1161. #endif
  1162. hwctrl(&mtd, NAND_CMD_ERASE2, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
  1163. udelay(2000);
  1164. while (!this->dev_ready(&mtd))
  1165. ;
  1166. nand_deselect();
  1167. return 0;
  1168. }
  1169. #else
  1170. static int nand_read_page(int block, int page, void *dst)
  1171. {
  1172. struct nand_chip *this = mtd.priv;
  1173. nand_command(block, page, 0, NAND_CMD_READ0);
  1174. atmel_nand_pmecc_read_page(&mtd, this, dst, 0, page);
  1175. return 0;
  1176. }
  1177. #endif /* CONFIG_SPL_NAND_ECC */
  1178. int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
  1179. {
  1180. unsigned int block, lastblock;
  1181. unsigned int page;
  1182. block = offs / CONFIG_SYS_NAND_BLOCK_SIZE;
  1183. lastblock = (offs + size - 1) / CONFIG_SYS_NAND_BLOCK_SIZE;
  1184. page = (offs % CONFIG_SYS_NAND_BLOCK_SIZE) / CONFIG_SYS_NAND_PAGE_SIZE;
  1185. while (block <= lastblock) {
  1186. if (!nand_is_bad_block(block)) {
  1187. while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
  1188. nand_read_page(block, page, dst);
  1189. dst += CONFIG_SYS_NAND_PAGE_SIZE;
  1190. page++;
  1191. }
  1192. page = 0;
  1193. } else {
  1194. lastblock++;
  1195. }
  1196. block++;
  1197. }
  1198. return 0;
  1199. }
  1200. int at91_nand_wait_ready(struct mtd_info *mtd)
  1201. {
  1202. struct nand_chip *this = mtd->priv;
  1203. udelay(this->chip_delay);
  1204. return 1;
  1205. }
  1206. int board_nand_init(struct nand_chip *nand)
  1207. {
  1208. int ret = 0;
  1209. nand->ecc.mode = NAND_ECC_SOFT;
  1210. #ifdef CONFIG_SYS_NAND_DBW_16
  1211. nand->options = NAND_BUSWIDTH_16;
  1212. nand->read_buf = nand_read_buf16;
  1213. #else
  1214. nand->read_buf = nand_read_buf;
  1215. #endif
  1216. nand->cmd_ctrl = at91_nand_hwcontrol;
  1217. #ifdef CONFIG_SYS_NAND_READY_PIN
  1218. nand->dev_ready = at91_nand_ready;
  1219. #else
  1220. nand->dev_ready = at91_nand_wait_ready;
  1221. #endif
  1222. nand->chip_delay = 20;
  1223. #ifdef CONFIG_ATMEL_NAND_HWECC
  1224. #ifdef CONFIG_ATMEL_NAND_HW_PMECC
  1225. ret = atmel_pmecc_nand_init_params(nand, &mtd);
  1226. #endif
  1227. #endif
  1228. return ret;
  1229. }
  1230. void nand_init(void)
  1231. {
  1232. mtd.writesize = CONFIG_SYS_NAND_PAGE_SIZE;
  1233. mtd.oobsize = CONFIG_SYS_NAND_OOBSIZE;
  1234. mtd.priv = &nand_chip;
  1235. nand_chip.IO_ADDR_R = (void __iomem *)CONFIG_SYS_NAND_BASE;
  1236. nand_chip.IO_ADDR_W = (void __iomem *)CONFIG_SYS_NAND_BASE;
  1237. board_nand_init(&nand_chip);
  1238. #ifdef CONFIG_SPL_NAND_ECC
  1239. if (nand_chip.ecc.mode == NAND_ECC_SOFT) {
  1240. nand_chip.ecc.calculate = nand_calculate_ecc;
  1241. nand_chip.ecc.correct = nand_correct_data;
  1242. }
  1243. #endif
  1244. if (nand_chip.select_chip)
  1245. nand_chip.select_chip(&mtd, 0);
  1246. }
  1247. void nand_deselect(void)
  1248. {
  1249. if (nand_chip.select_chip)
  1250. nand_chip.select_chip(&mtd, -1);
  1251. }
  1252. #else
  1253. #ifndef CONFIG_SYS_NAND_BASE_LIST
  1254. #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
  1255. #endif
  1256. static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
  1257. static ulong base_addr[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
  1258. int atmel_nand_chip_init(int devnum, ulong base_addr)
  1259. {
  1260. int ret;
  1261. struct mtd_info *mtd = &nand_info[devnum];
  1262. struct nand_chip *nand = &nand_chip[devnum];
  1263. mtd->priv = nand;
  1264. nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
  1265. #ifdef CONFIG_NAND_ECC_BCH
  1266. nand->ecc.mode = NAND_ECC_SOFT_BCH;
  1267. #else
  1268. nand->ecc.mode = NAND_ECC_SOFT;
  1269. #endif
  1270. #ifdef CONFIG_SYS_NAND_DBW_16
  1271. nand->options = NAND_BUSWIDTH_16;
  1272. #endif
  1273. nand->cmd_ctrl = at91_nand_hwcontrol;
  1274. #ifdef CONFIG_SYS_NAND_READY_PIN
  1275. nand->dev_ready = at91_nand_ready;
  1276. #endif
  1277. nand->chip_delay = 75;
  1278. ret = nand_scan_ident(mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
  1279. if (ret)
  1280. return ret;
  1281. #ifdef CONFIG_ATMEL_NAND_HWECC
  1282. #ifdef CONFIG_ATMEL_NAND_HW_PMECC
  1283. ret = atmel_pmecc_nand_init_params(nand, mtd);
  1284. #else
  1285. ret = atmel_hwecc_nand_init_param(nand, mtd);
  1286. #endif
  1287. if (ret)
  1288. return ret;
  1289. #endif
  1290. ret = nand_scan_tail(mtd);
  1291. if (!ret)
  1292. nand_register(devnum);
  1293. return ret;
  1294. }
  1295. void board_nand_init(void)
  1296. {
  1297. int i;
  1298. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
  1299. if (atmel_nand_chip_init(i, base_addr[i]))
  1300. dev_err(host->dev, "atmel_nand: Fail to initialize #%d chip",
  1301. i);
  1302. }
  1303. #endif /* CONFIG_SPL_BUILD */