atmel_nand.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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. * See file CREDITS for list of people who contributed to this
  12. * project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. #include <common.h>
  30. #include <asm/arch/hardware.h>
  31. #include <asm/arch/gpio.h>
  32. #include <asm/arch/at91_pio.h>
  33. #include <nand.h>
  34. #include <watchdog.h>
  35. #ifdef CONFIG_ATMEL_NAND_HWECC
  36. /* Register access macros */
  37. #define ecc_readl(add, reg) \
  38. readl(AT91_BASE_SYS + add + ATMEL_ECC_##reg)
  39. #define ecc_writel(add, reg, value) \
  40. writel((value), AT91_BASE_SYS + add + ATMEL_ECC_##reg)
  41. #include "atmel_nand_ecc.h" /* Hardware ECC registers */
  42. #ifdef CONFIG_ATMEL_NAND_HW_PMECC
  43. struct atmel_nand_host {
  44. struct pmecc_regs __iomem *pmecc;
  45. struct pmecc_errloc_regs __iomem *pmerrloc;
  46. void __iomem *pmecc_rom_base;
  47. u8 pmecc_corr_cap;
  48. u16 pmecc_sector_size;
  49. u32 pmecc_index_table_offset;
  50. int pmecc_bytes_per_sector;
  51. int pmecc_sector_number;
  52. int pmecc_degree; /* Degree of remainders */
  53. int pmecc_cw_len; /* Length of codeword */
  54. /* lookup table for alpha_to and index_of */
  55. void __iomem *pmecc_alpha_to;
  56. void __iomem *pmecc_index_of;
  57. /* data for pmecc computation */
  58. int16_t pmecc_smu[(CONFIG_PMECC_CAP + 2) * (2 * CONFIG_PMECC_CAP + 1)];
  59. int16_t pmecc_partial_syn[2 * CONFIG_PMECC_CAP + 1];
  60. int16_t pmecc_si[2 * CONFIG_PMECC_CAP + 1];
  61. int16_t pmecc_lmu[CONFIG_PMECC_CAP + 1]; /* polynomal order */
  62. int pmecc_mu[CONFIG_PMECC_CAP + 1];
  63. int pmecc_dmu[CONFIG_PMECC_CAP + 1];
  64. int pmecc_delta[CONFIG_PMECC_CAP + 1];
  65. };
  66. static struct atmel_nand_host pmecc_host;
  67. static struct nand_ecclayout atmel_pmecc_oobinfo;
  68. /*
  69. * Return number of ecc bytes per sector according to sector size and
  70. * correction capability
  71. *
  72. * Following table shows what at91 PMECC supported:
  73. * Correction Capability Sector_512_bytes Sector_1024_bytes
  74. * ===================== ================ =================
  75. * 2-bits 4-bytes 4-bytes
  76. * 4-bits 7-bytes 7-bytes
  77. * 8-bits 13-bytes 14-bytes
  78. * 12-bits 20-bytes 21-bytes
  79. * 24-bits 39-bytes 42-bytes
  80. */
  81. static int pmecc_get_ecc_bytes(int cap, int sector_size)
  82. {
  83. int m = 12 + sector_size / 512;
  84. return (m * cap + 7) / 8;
  85. }
  86. static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
  87. int oobsize, int ecc_len)
  88. {
  89. int i;
  90. layout->eccbytes = ecc_len;
  91. /* ECC will occupy the last ecc_len bytes continuously */
  92. for (i = 0; i < ecc_len; i++)
  93. layout->eccpos[i] = oobsize - ecc_len + i;
  94. layout->oobfree[0].offset = 2;
  95. layout->oobfree[0].length =
  96. oobsize - ecc_len - layout->oobfree[0].offset;
  97. }
  98. static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
  99. {
  100. int table_size;
  101. table_size = host->pmecc_sector_size == 512 ?
  102. PMECC_INDEX_TABLE_SIZE_512 : PMECC_INDEX_TABLE_SIZE_1024;
  103. /* the ALPHA lookup table is right behind the INDEX lookup table. */
  104. return host->pmecc_rom_base + host->pmecc_index_table_offset +
  105. table_size * sizeof(int16_t);
  106. }
  107. static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
  108. {
  109. struct nand_chip *nand_chip = mtd->priv;
  110. struct atmel_nand_host *host = nand_chip->priv;
  111. int i;
  112. uint32_t value;
  113. /* Fill odd syndromes */
  114. for (i = 0; i < host->pmecc_corr_cap; i++) {
  115. value = readl(&host->pmecc->rem_port[sector].rem[i / 2]);
  116. if (i & 1)
  117. value >>= 16;
  118. value &= 0xffff;
  119. host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
  120. }
  121. }
  122. static void pmecc_substitute(struct mtd_info *mtd)
  123. {
  124. struct nand_chip *nand_chip = mtd->priv;
  125. struct atmel_nand_host *host = nand_chip->priv;
  126. int16_t __iomem *alpha_to = host->pmecc_alpha_to;
  127. int16_t __iomem *index_of = host->pmecc_index_of;
  128. int16_t *partial_syn = host->pmecc_partial_syn;
  129. const int cap = host->pmecc_corr_cap;
  130. int16_t *si;
  131. int i, j;
  132. /* si[] is a table that holds the current syndrome value,
  133. * an element of that table belongs to the field
  134. */
  135. si = host->pmecc_si;
  136. memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
  137. /* Computation 2t syndromes based on S(x) */
  138. /* Odd syndromes */
  139. for (i = 1; i < 2 * cap; i += 2) {
  140. for (j = 0; j < host->pmecc_degree; j++) {
  141. if (partial_syn[i] & (0x1 << j))
  142. si[i] = readw(alpha_to + i * j) ^ si[i];
  143. }
  144. }
  145. /* Even syndrome = (Odd syndrome) ** 2 */
  146. for (i = 2, j = 1; j <= cap; i = ++j << 1) {
  147. if (si[j] == 0) {
  148. si[i] = 0;
  149. } else {
  150. int16_t tmp;
  151. tmp = readw(index_of + si[j]);
  152. tmp = (tmp * 2) % host->pmecc_cw_len;
  153. si[i] = readw(alpha_to + tmp);
  154. }
  155. }
  156. }
  157. /*
  158. * This function defines a Berlekamp iterative procedure for
  159. * finding the value of the error location polynomial.
  160. * The input is si[], initialize by pmecc_substitute().
  161. * The output is smu[][].
  162. *
  163. * This function is written according to chip datasheet Chapter:
  164. * Find the Error Location Polynomial Sigma(x) of Section:
  165. * Programmable Multibit ECC Control (PMECC).
  166. */
  167. static void pmecc_get_sigma(struct mtd_info *mtd)
  168. {
  169. struct nand_chip *nand_chip = mtd->priv;
  170. struct atmel_nand_host *host = nand_chip->priv;
  171. int16_t *lmu = host->pmecc_lmu;
  172. int16_t *si = host->pmecc_si;
  173. int *mu = host->pmecc_mu;
  174. int *dmu = host->pmecc_dmu; /* Discrepancy */
  175. int *delta = host->pmecc_delta; /* Delta order */
  176. int cw_len = host->pmecc_cw_len;
  177. const int16_t cap = host->pmecc_corr_cap;
  178. const int num = 2 * cap + 1;
  179. int16_t __iomem *index_of = host->pmecc_index_of;
  180. int16_t __iomem *alpha_to = host->pmecc_alpha_to;
  181. int i, j, k;
  182. uint32_t dmu_0_count, tmp;
  183. int16_t *smu = host->pmecc_smu;
  184. /* index of largest delta */
  185. int ro;
  186. int largest;
  187. int diff;
  188. /* Init the Sigma(x) */
  189. memset(smu, 0, sizeof(int16_t) * ARRAY_SIZE(smu));
  190. dmu_0_count = 0;
  191. /* First Row */
  192. /* Mu */
  193. mu[0] = -1;
  194. smu[0] = 1;
  195. /* discrepancy set to 1 */
  196. dmu[0] = 1;
  197. /* polynom order set to 0 */
  198. lmu[0] = 0;
  199. /* delta[0] = (mu[0] * 2 - lmu[0]) >> 1; */
  200. delta[0] = -1;
  201. /* Second Row */
  202. /* Mu */
  203. mu[1] = 0;
  204. /* Sigma(x) set to 1 */
  205. smu[num] = 1;
  206. /* discrepancy set to S1 */
  207. dmu[1] = si[1];
  208. /* polynom order set to 0 */
  209. lmu[1] = 0;
  210. /* delta[1] = (mu[1] * 2 - lmu[1]) >> 1; */
  211. delta[1] = 0;
  212. for (i = 1; i <= cap; i++) {
  213. mu[i + 1] = i << 1;
  214. /* Begin Computing Sigma (Mu+1) and L(mu) */
  215. /* check if discrepancy is set to 0 */
  216. if (dmu[i] == 0) {
  217. dmu_0_count++;
  218. tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
  219. if ((cap - (lmu[i] >> 1) - 1) & 0x1)
  220. tmp += 2;
  221. else
  222. tmp += 1;
  223. if (dmu_0_count == tmp) {
  224. for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
  225. smu[(cap + 1) * num + j] =
  226. smu[i * num + j];
  227. lmu[cap + 1] = lmu[i];
  228. return;
  229. }
  230. /* copy polynom */
  231. for (j = 0; j <= lmu[i] >> 1; j++)
  232. smu[(i + 1) * num + j] = smu[i * num + j];
  233. /* copy previous polynom order to the next */
  234. lmu[i + 1] = lmu[i];
  235. } else {
  236. ro = 0;
  237. largest = -1;
  238. /* find largest delta with dmu != 0 */
  239. for (j = 0; j < i; j++) {
  240. if ((dmu[j]) && (delta[j] > largest)) {
  241. largest = delta[j];
  242. ro = j;
  243. }
  244. }
  245. /* compute difference */
  246. diff = (mu[i] - mu[ro]);
  247. /* Compute degree of the new smu polynomial */
  248. if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
  249. lmu[i + 1] = lmu[i];
  250. else
  251. lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
  252. /* Init smu[i+1] with 0 */
  253. for (k = 0; k < num; k++)
  254. smu[(i + 1) * num + k] = 0;
  255. /* Compute smu[i+1] */
  256. for (k = 0; k <= lmu[ro] >> 1; k++) {
  257. int16_t a, b, c;
  258. if (!(smu[ro * num + k] && dmu[i]))
  259. continue;
  260. a = readw(index_of + dmu[i]);
  261. b = readw(index_of + dmu[ro]);
  262. c = readw(index_of + smu[ro * num + k]);
  263. tmp = a + (cw_len - b) + c;
  264. a = readw(alpha_to + tmp % cw_len);
  265. smu[(i + 1) * num + (k + diff)] = a;
  266. }
  267. for (k = 0; k <= lmu[i] >> 1; k++)
  268. smu[(i + 1) * num + k] ^= smu[i * num + k];
  269. }
  270. /* End Computing Sigma (Mu+1) and L(mu) */
  271. /* In either case compute delta */
  272. delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
  273. /* Do not compute discrepancy for the last iteration */
  274. if (i >= cap)
  275. continue;
  276. for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
  277. tmp = 2 * (i - 1);
  278. if (k == 0) {
  279. dmu[i + 1] = si[tmp + 3];
  280. } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
  281. int16_t a, b, c;
  282. a = readw(index_of +
  283. smu[(i + 1) * num + k]);
  284. b = si[2 * (i - 1) + 3 - k];
  285. c = readw(index_of + b);
  286. tmp = a + c;
  287. tmp %= cw_len;
  288. dmu[i + 1] = readw(alpha_to + tmp) ^
  289. dmu[i + 1];
  290. }
  291. }
  292. }
  293. }
  294. static int pmecc_err_location(struct mtd_info *mtd)
  295. {
  296. struct nand_chip *nand_chip = mtd->priv;
  297. struct atmel_nand_host *host = nand_chip->priv;
  298. const int cap = host->pmecc_corr_cap;
  299. const int num = 2 * cap + 1;
  300. int sector_size = host->pmecc_sector_size;
  301. int err_nbr = 0; /* number of error */
  302. int roots_nbr; /* number of roots */
  303. int i;
  304. uint32_t val;
  305. int16_t *smu = host->pmecc_smu;
  306. int timeout = PMECC_MAX_TIMEOUT_US;
  307. writel(PMERRLOC_DISABLE, &host->pmerrloc->eldis);
  308. for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
  309. writel(smu[(cap + 1) * num + i], &host->pmerrloc->sigma[i]);
  310. err_nbr++;
  311. }
  312. val = PMERRLOC_ELCFG_NUM_ERRORS(err_nbr - 1);
  313. if (sector_size == 1024)
  314. val |= PMERRLOC_ELCFG_SECTOR_1024;
  315. writel(val, &host->pmerrloc->elcfg);
  316. writel(sector_size * 8 + host->pmecc_degree * cap,
  317. &host->pmerrloc->elen);
  318. while (--timeout) {
  319. if (readl(&host->pmerrloc->elisr) & PMERRLOC_CALC_DONE)
  320. break;
  321. WATCHDOG_RESET();
  322. udelay(1);
  323. }
  324. if (!timeout) {
  325. printk(KERN_ERR "atmel_nand : Timeout to calculate PMECC error location\n");
  326. return -1;
  327. }
  328. roots_nbr = (readl(&host->pmerrloc->elisr) & PMERRLOC_ERR_NUM_MASK)
  329. >> 8;
  330. /* Number of roots == degree of smu hence <= cap */
  331. if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
  332. return err_nbr - 1;
  333. /* Number of roots does not match the degree of smu
  334. * unable to correct error */
  335. return -1;
  336. }
  337. static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
  338. int sector_num, int extra_bytes, int err_nbr)
  339. {
  340. struct nand_chip *nand_chip = mtd->priv;
  341. struct atmel_nand_host *host = nand_chip->priv;
  342. int i = 0;
  343. int byte_pos, bit_pos, sector_size, pos;
  344. uint32_t tmp;
  345. uint8_t err_byte;
  346. sector_size = host->pmecc_sector_size;
  347. while (err_nbr) {
  348. tmp = readl(&host->pmerrloc->el[i]) - 1;
  349. byte_pos = tmp / 8;
  350. bit_pos = tmp % 8;
  351. if (byte_pos >= (sector_size + extra_bytes))
  352. BUG(); /* should never happen */
  353. if (byte_pos < sector_size) {
  354. err_byte = *(buf + byte_pos);
  355. *(buf + byte_pos) ^= (1 << bit_pos);
  356. pos = sector_num * host->pmecc_sector_size + byte_pos;
  357. printk(KERN_INFO "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
  358. pos, bit_pos, err_byte, *(buf + byte_pos));
  359. } else {
  360. /* Bit flip in OOB area */
  361. tmp = sector_num * host->pmecc_bytes_per_sector
  362. + (byte_pos - sector_size);
  363. err_byte = ecc[tmp];
  364. ecc[tmp] ^= (1 << bit_pos);
  365. pos = tmp + nand_chip->ecc.layout->eccpos[0];
  366. printk(KERN_INFO "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
  367. pos, bit_pos, err_byte, ecc[tmp]);
  368. }
  369. i++;
  370. err_nbr--;
  371. }
  372. return;
  373. }
  374. static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
  375. u8 *ecc)
  376. {
  377. struct nand_chip *nand_chip = mtd->priv;
  378. struct atmel_nand_host *host = nand_chip->priv;
  379. int i, err_nbr, eccbytes;
  380. uint8_t *buf_pos;
  381. eccbytes = nand_chip->ecc.bytes;
  382. for (i = 0; i < eccbytes; i++)
  383. if (ecc[i] != 0xff)
  384. goto normal_check;
  385. /* Erased page, return OK */
  386. return 0;
  387. normal_check:
  388. for (i = 0; i < host->pmecc_sector_number; i++) {
  389. err_nbr = 0;
  390. if (pmecc_stat & 0x1) {
  391. buf_pos = buf + i * host->pmecc_sector_size;
  392. pmecc_gen_syndrome(mtd, i);
  393. pmecc_substitute(mtd);
  394. pmecc_get_sigma(mtd);
  395. err_nbr = pmecc_err_location(mtd);
  396. if (err_nbr == -1) {
  397. printk(KERN_ERR "PMECC: Too many errors\n");
  398. mtd->ecc_stats.failed++;
  399. return -EIO;
  400. } else {
  401. pmecc_correct_data(mtd, buf_pos, ecc, i,
  402. host->pmecc_bytes_per_sector, err_nbr);
  403. mtd->ecc_stats.corrected += err_nbr;
  404. }
  405. }
  406. pmecc_stat >>= 1;
  407. }
  408. return 0;
  409. }
  410. static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
  411. struct nand_chip *chip, uint8_t *buf, int page)
  412. {
  413. struct atmel_nand_host *host = chip->priv;
  414. int eccsize = chip->ecc.size;
  415. uint8_t *oob = chip->oob_poi;
  416. uint32_t *eccpos = chip->ecc.layout->eccpos;
  417. uint32_t stat;
  418. int timeout = PMECC_MAX_TIMEOUT_US;
  419. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
  420. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
  421. pmecc_writel(host->pmecc, cfg, ((pmecc_readl(host->pmecc, cfg))
  422. & ~PMECC_CFG_WRITE_OP) | PMECC_CFG_AUTO_ENABLE);
  423. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
  424. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DATA);
  425. chip->read_buf(mtd, buf, eccsize);
  426. chip->read_buf(mtd, oob, mtd->oobsize);
  427. while (--timeout) {
  428. if (!(pmecc_readl(host->pmecc, sr) & PMECC_SR_BUSY))
  429. break;
  430. WATCHDOG_RESET();
  431. udelay(1);
  432. }
  433. if (!timeout) {
  434. printk(KERN_ERR "atmel_nand : Timeout to read PMECC page\n");
  435. return -1;
  436. }
  437. stat = pmecc_readl(host->pmecc, isr);
  438. if (stat != 0)
  439. if (pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]) != 0)
  440. return -EIO;
  441. return 0;
  442. }
  443. static void atmel_nand_pmecc_write_page(struct mtd_info *mtd,
  444. struct nand_chip *chip, const uint8_t *buf)
  445. {
  446. struct atmel_nand_host *host = chip->priv;
  447. uint32_t *eccpos = chip->ecc.layout->eccpos;
  448. int i, j;
  449. int timeout = PMECC_MAX_TIMEOUT_US;
  450. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
  451. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
  452. pmecc_writel(host->pmecc, cfg, (pmecc_readl(host->pmecc, cfg) |
  453. PMECC_CFG_WRITE_OP) & ~PMECC_CFG_AUTO_ENABLE);
  454. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
  455. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DATA);
  456. chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
  457. while (--timeout) {
  458. if (!(pmecc_readl(host->pmecc, sr) & PMECC_SR_BUSY))
  459. break;
  460. WATCHDOG_RESET();
  461. udelay(1);
  462. }
  463. if (!timeout) {
  464. printk(KERN_ERR "atmel_nand : Timeout to read PMECC status, fail to write PMECC in oob\n");
  465. return;
  466. }
  467. for (i = 0; i < host->pmecc_sector_number; i++) {
  468. for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
  469. int pos;
  470. pos = i * host->pmecc_bytes_per_sector + j;
  471. chip->oob_poi[eccpos[pos]] =
  472. readb(&host->pmecc->ecc_port[i].ecc[j]);
  473. }
  474. }
  475. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  476. }
  477. static void atmel_pmecc_core_init(struct mtd_info *mtd)
  478. {
  479. struct nand_chip *nand_chip = mtd->priv;
  480. struct atmel_nand_host *host = nand_chip->priv;
  481. uint32_t val = 0;
  482. struct nand_ecclayout *ecc_layout;
  483. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
  484. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
  485. switch (host->pmecc_corr_cap) {
  486. case 2:
  487. val = PMECC_CFG_BCH_ERR2;
  488. break;
  489. case 4:
  490. val = PMECC_CFG_BCH_ERR4;
  491. break;
  492. case 8:
  493. val = PMECC_CFG_BCH_ERR8;
  494. break;
  495. case 12:
  496. val = PMECC_CFG_BCH_ERR12;
  497. break;
  498. case 24:
  499. val = PMECC_CFG_BCH_ERR24;
  500. break;
  501. }
  502. if (host->pmecc_sector_size == 512)
  503. val |= PMECC_CFG_SECTOR512;
  504. else if (host->pmecc_sector_size == 1024)
  505. val |= PMECC_CFG_SECTOR1024;
  506. switch (host->pmecc_sector_number) {
  507. case 1:
  508. val |= PMECC_CFG_PAGE_1SECTOR;
  509. break;
  510. case 2:
  511. val |= PMECC_CFG_PAGE_2SECTORS;
  512. break;
  513. case 4:
  514. val |= PMECC_CFG_PAGE_4SECTORS;
  515. break;
  516. case 8:
  517. val |= PMECC_CFG_PAGE_8SECTORS;
  518. break;
  519. }
  520. val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
  521. | PMECC_CFG_AUTO_DISABLE);
  522. pmecc_writel(host->pmecc, cfg, val);
  523. ecc_layout = nand_chip->ecc.layout;
  524. pmecc_writel(host->pmecc, sarea, mtd->oobsize - 1);
  525. pmecc_writel(host->pmecc, saddr, ecc_layout->eccpos[0]);
  526. pmecc_writel(host->pmecc, eaddr,
  527. ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
  528. /* See datasheet about PMECC Clock Control Register */
  529. pmecc_writel(host->pmecc, clk, PMECC_CLK_133MHZ);
  530. pmecc_writel(host->pmecc, idr, 0xff);
  531. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
  532. }
  533. static int atmel_pmecc_nand_init_params(struct nand_chip *nand,
  534. struct mtd_info *mtd)
  535. {
  536. struct atmel_nand_host *host;
  537. int cap, sector_size;
  538. host = nand->priv = &pmecc_host;
  539. nand->ecc.mode = NAND_ECC_HW;
  540. nand->ecc.calculate = NULL;
  541. nand->ecc.correct = NULL;
  542. nand->ecc.hwctl = NULL;
  543. cap = host->pmecc_corr_cap = CONFIG_PMECC_CAP;
  544. sector_size = host->pmecc_sector_size = CONFIG_PMECC_SECTOR_SIZE;
  545. host->pmecc_index_table_offset = CONFIG_PMECC_INDEX_TABLE_OFFSET;
  546. MTDDEBUG(MTD_DEBUG_LEVEL1,
  547. "Initialize PMECC params, cap: %d, sector: %d\n",
  548. cap, sector_size);
  549. host->pmecc = (struct pmecc_regs __iomem *) ATMEL_BASE_PMECC;
  550. host->pmerrloc = (struct pmecc_errloc_regs __iomem *)
  551. ATMEL_BASE_PMERRLOC;
  552. host->pmecc_rom_base = (void __iomem *) ATMEL_BASE_ROM;
  553. /* ECC is calculated for the whole page (1 step) */
  554. nand->ecc.size = mtd->writesize;
  555. /* set ECC page size and oob layout */
  556. switch (mtd->writesize) {
  557. case 2048:
  558. case 4096:
  559. host->pmecc_degree = PMECC_GF_DIMENSION_13;
  560. host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
  561. host->pmecc_sector_number = mtd->writesize / sector_size;
  562. host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
  563. cap, sector_size);
  564. host->pmecc_alpha_to = pmecc_get_alpha_to(host);
  565. host->pmecc_index_of = host->pmecc_rom_base +
  566. host->pmecc_index_table_offset;
  567. nand->ecc.steps = 1;
  568. nand->ecc.bytes = host->pmecc_bytes_per_sector *
  569. host->pmecc_sector_number;
  570. if (nand->ecc.bytes > mtd->oobsize - 2) {
  571. printk(KERN_ERR "No room for ECC bytes\n");
  572. return -EINVAL;
  573. }
  574. pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
  575. mtd->oobsize,
  576. nand->ecc.bytes);
  577. nand->ecc.layout = &atmel_pmecc_oobinfo;
  578. break;
  579. case 512:
  580. case 1024:
  581. /* TODO */
  582. printk(KERN_ERR "Unsupported page size for PMECC, use Software ECC\n");
  583. default:
  584. /* page size not handled by HW ECC */
  585. /* switching back to soft ECC */
  586. nand->ecc.mode = NAND_ECC_SOFT;
  587. nand->ecc.read_page = NULL;
  588. nand->ecc.postpad = 0;
  589. nand->ecc.prepad = 0;
  590. nand->ecc.bytes = 0;
  591. return 0;
  592. }
  593. nand->ecc.read_page = atmel_nand_pmecc_read_page;
  594. nand->ecc.write_page = atmel_nand_pmecc_write_page;
  595. atmel_pmecc_core_init(mtd);
  596. return 0;
  597. }
  598. #else
  599. /* oob layout for large page size
  600. * bad block info is on bytes 0 and 1
  601. * the bytes have to be consecutives to avoid
  602. * several NAND_CMD_RNDOUT during read
  603. */
  604. static struct nand_ecclayout atmel_oobinfo_large = {
  605. .eccbytes = 4,
  606. .eccpos = {60, 61, 62, 63},
  607. .oobfree = {
  608. {2, 58}
  609. },
  610. };
  611. /* oob layout for small page size
  612. * bad block info is on bytes 4 and 5
  613. * the bytes have to be consecutives to avoid
  614. * several NAND_CMD_RNDOUT during read
  615. */
  616. static struct nand_ecclayout atmel_oobinfo_small = {
  617. .eccbytes = 4,
  618. .eccpos = {0, 1, 2, 3},
  619. .oobfree = {
  620. {6, 10}
  621. },
  622. };
  623. /*
  624. * Calculate HW ECC
  625. *
  626. * function called after a write
  627. *
  628. * mtd: MTD block structure
  629. * dat: raw data (unused)
  630. * ecc_code: buffer for ECC
  631. */
  632. static int atmel_nand_calculate(struct mtd_info *mtd,
  633. const u_char *dat, unsigned char *ecc_code)
  634. {
  635. unsigned int ecc_value;
  636. /* get the first 2 ECC bytes */
  637. ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR);
  638. ecc_code[0] = ecc_value & 0xFF;
  639. ecc_code[1] = (ecc_value >> 8) & 0xFF;
  640. /* get the last 2 ECC bytes */
  641. ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, NPR) & ATMEL_ECC_NPARITY;
  642. ecc_code[2] = ecc_value & 0xFF;
  643. ecc_code[3] = (ecc_value >> 8) & 0xFF;
  644. return 0;
  645. }
  646. /*
  647. * HW ECC read page function
  648. *
  649. * mtd: mtd info structure
  650. * chip: nand chip info structure
  651. * buf: buffer to store read data
  652. */
  653. static int atmel_nand_read_page(struct mtd_info *mtd,
  654. struct nand_chip *chip, uint8_t *buf, int page)
  655. {
  656. int eccsize = chip->ecc.size;
  657. int eccbytes = chip->ecc.bytes;
  658. uint32_t *eccpos = chip->ecc.layout->eccpos;
  659. uint8_t *p = buf;
  660. uint8_t *oob = chip->oob_poi;
  661. uint8_t *ecc_pos;
  662. int stat;
  663. /* read the page */
  664. chip->read_buf(mtd, p, eccsize);
  665. /* move to ECC position if needed */
  666. if (eccpos[0] != 0) {
  667. /* This only works on large pages
  668. * because the ECC controller waits for
  669. * NAND_CMD_RNDOUTSTART after the
  670. * NAND_CMD_RNDOUT.
  671. * anyway, for small pages, the eccpos[0] == 0
  672. */
  673. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  674. mtd->writesize + eccpos[0], -1);
  675. }
  676. /* the ECC controller needs to read the ECC just after the data */
  677. ecc_pos = oob + eccpos[0];
  678. chip->read_buf(mtd, ecc_pos, eccbytes);
  679. /* check if there's an error */
  680. stat = chip->ecc.correct(mtd, p, oob, NULL);
  681. if (stat < 0)
  682. mtd->ecc_stats.failed++;
  683. else
  684. mtd->ecc_stats.corrected += stat;
  685. /* get back to oob start (end of page) */
  686. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  687. /* read the oob */
  688. chip->read_buf(mtd, oob, mtd->oobsize);
  689. return 0;
  690. }
  691. /*
  692. * HW ECC Correction
  693. *
  694. * function called after a read
  695. *
  696. * mtd: MTD block structure
  697. * dat: raw data read from the chip
  698. * read_ecc: ECC from the chip (unused)
  699. * isnull: unused
  700. *
  701. * Detect and correct a 1 bit error for a page
  702. */
  703. static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
  704. u_char *read_ecc, u_char *isnull)
  705. {
  706. struct nand_chip *nand_chip = mtd->priv;
  707. unsigned int ecc_status;
  708. unsigned int ecc_word, ecc_bit;
  709. /* get the status from the Status Register */
  710. ecc_status = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, SR);
  711. /* if there's no error */
  712. if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
  713. return 0;
  714. /* get error bit offset (4 bits) */
  715. ecc_bit = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_BITADDR;
  716. /* get word address (12 bits) */
  717. ecc_word = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_WORDADDR;
  718. ecc_word >>= 4;
  719. /* if there are multiple errors */
  720. if (ecc_status & ATMEL_ECC_MULERR) {
  721. /* check if it is a freshly erased block
  722. * (filled with 0xff) */
  723. if ((ecc_bit == ATMEL_ECC_BITADDR)
  724. && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
  725. /* the block has just been erased, return OK */
  726. return 0;
  727. }
  728. /* it doesn't seems to be a freshly
  729. * erased block.
  730. * We can't correct so many errors */
  731. printk(KERN_WARNING "atmel_nand : multiple errors detected."
  732. " Unable to correct.\n");
  733. return -EIO;
  734. }
  735. /* if there's a single bit error : we can correct it */
  736. if (ecc_status & ATMEL_ECC_ECCERR) {
  737. /* there's nothing much to do here.
  738. * the bit error is on the ECC itself.
  739. */
  740. printk(KERN_WARNING "atmel_nand : one bit error on ECC code."
  741. " Nothing to correct\n");
  742. return 0;
  743. }
  744. printk(KERN_WARNING "atmel_nand : one bit error on data."
  745. " (word offset in the page :"
  746. " 0x%x bit offset : 0x%x)\n",
  747. ecc_word, ecc_bit);
  748. /* correct the error */
  749. if (nand_chip->options & NAND_BUSWIDTH_16) {
  750. /* 16 bits words */
  751. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  752. } else {
  753. /* 8 bits words */
  754. dat[ecc_word] ^= (1 << ecc_bit);
  755. }
  756. printk(KERN_WARNING "atmel_nand : error corrected\n");
  757. return 1;
  758. }
  759. /*
  760. * Enable HW ECC : unused on most chips
  761. */
  762. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  763. {
  764. }
  765. int atmel_hwecc_nand_init_param(struct nand_chip *nand, struct mtd_info *mtd)
  766. {
  767. nand->ecc.mode = NAND_ECC_HW;
  768. nand->ecc.calculate = atmel_nand_calculate;
  769. nand->ecc.correct = atmel_nand_correct;
  770. nand->ecc.hwctl = atmel_nand_hwctl;
  771. nand->ecc.read_page = atmel_nand_read_page;
  772. nand->ecc.bytes = 4;
  773. if (nand->ecc.mode == NAND_ECC_HW) {
  774. /* ECC is calculated for the whole page (1 step) */
  775. nand->ecc.size = mtd->writesize;
  776. /* set ECC page size and oob layout */
  777. switch (mtd->writesize) {
  778. case 512:
  779. nand->ecc.layout = &atmel_oobinfo_small;
  780. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  781. ATMEL_ECC_PAGESIZE_528);
  782. break;
  783. case 1024:
  784. nand->ecc.layout = &atmel_oobinfo_large;
  785. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  786. ATMEL_ECC_PAGESIZE_1056);
  787. break;
  788. case 2048:
  789. nand->ecc.layout = &atmel_oobinfo_large;
  790. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  791. ATMEL_ECC_PAGESIZE_2112);
  792. break;
  793. case 4096:
  794. nand->ecc.layout = &atmel_oobinfo_large;
  795. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  796. ATMEL_ECC_PAGESIZE_4224);
  797. break;
  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.calculate = NULL;
  803. nand->ecc.correct = NULL;
  804. nand->ecc.hwctl = NULL;
  805. nand->ecc.read_page = NULL;
  806. nand->ecc.postpad = 0;
  807. nand->ecc.prepad = 0;
  808. nand->ecc.bytes = 0;
  809. break;
  810. }
  811. }
  812. return 0;
  813. }
  814. #endif /* CONFIG_ATMEL_NAND_HW_PMECC */
  815. #endif /* CONFIG_ATMEL_NAND_HWECC */
  816. static void at91_nand_hwcontrol(struct mtd_info *mtd,
  817. int cmd, unsigned int ctrl)
  818. {
  819. struct nand_chip *this = mtd->priv;
  820. if (ctrl & NAND_CTRL_CHANGE) {
  821. ulong IO_ADDR_W = (ulong) this->IO_ADDR_W;
  822. IO_ADDR_W &= ~(CONFIG_SYS_NAND_MASK_ALE
  823. | CONFIG_SYS_NAND_MASK_CLE);
  824. if (ctrl & NAND_CLE)
  825. IO_ADDR_W |= CONFIG_SYS_NAND_MASK_CLE;
  826. if (ctrl & NAND_ALE)
  827. IO_ADDR_W |= CONFIG_SYS_NAND_MASK_ALE;
  828. #ifdef CONFIG_SYS_NAND_ENABLE_PIN
  829. at91_set_gpio_value(CONFIG_SYS_NAND_ENABLE_PIN,
  830. !(ctrl & NAND_NCE));
  831. #endif
  832. this->IO_ADDR_W = (void *) IO_ADDR_W;
  833. }
  834. if (cmd != NAND_CMD_NONE)
  835. writeb(cmd, this->IO_ADDR_W);
  836. }
  837. #ifdef CONFIG_SYS_NAND_READY_PIN
  838. static int at91_nand_ready(struct mtd_info *mtd)
  839. {
  840. return at91_get_gpio_value(CONFIG_SYS_NAND_READY_PIN);
  841. }
  842. #endif
  843. #ifndef CONFIG_SYS_NAND_BASE_LIST
  844. #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
  845. #endif
  846. static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
  847. static ulong base_addr[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
  848. int atmel_nand_chip_init(int devnum, ulong base_addr)
  849. {
  850. int ret;
  851. struct mtd_info *mtd = &nand_info[devnum];
  852. struct nand_chip *nand = &nand_chip[devnum];
  853. mtd->priv = nand;
  854. nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
  855. nand->ecc.mode = NAND_ECC_SOFT;
  856. #ifdef CONFIG_SYS_NAND_DBW_16
  857. nand->options = NAND_BUSWIDTH_16;
  858. #endif
  859. nand->cmd_ctrl = at91_nand_hwcontrol;
  860. #ifdef CONFIG_SYS_NAND_READY_PIN
  861. nand->dev_ready = at91_nand_ready;
  862. #endif
  863. nand->chip_delay = 20;
  864. ret = nand_scan_ident(mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
  865. if (ret)
  866. return ret;
  867. #ifdef CONFIG_ATMEL_NAND_HWECC
  868. #ifdef CONFIG_ATMEL_NAND_HW_PMECC
  869. ret = atmel_pmecc_nand_init_params(nand, mtd);
  870. #else
  871. ret = atmel_hwecc_nand_init_param(nand, mtd);
  872. #endif
  873. if (ret)
  874. return ret;
  875. #endif
  876. ret = nand_scan_tail(mtd);
  877. if (!ret)
  878. nand_register(devnum);
  879. return ret;
  880. }
  881. void board_nand_init(void)
  882. {
  883. int i;
  884. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
  885. if (atmel_nand_chip_init(i, base_addr[i]))
  886. printk(KERN_ERR "atmel_nand: Fail to initialize #%d chip",
  887. i);
  888. }