atmel_nand.c 40 KB

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