atmel_nand.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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. printk(KERN_INFO "Initialize PMECC params, cap: %d, sector: %d\n",
  547. cap, sector_size);
  548. host->pmecc = (struct pmecc_regs __iomem *) ATMEL_BASE_PMECC;
  549. host->pmerrloc = (struct pmecc_errloc_regs __iomem *)
  550. ATMEL_BASE_PMERRLOC;
  551. host->pmecc_rom_base = (void __iomem *) ATMEL_BASE_ROM;
  552. /* ECC is calculated for the whole page (1 step) */
  553. nand->ecc.size = mtd->writesize;
  554. /* set ECC page size and oob layout */
  555. switch (mtd->writesize) {
  556. case 2048:
  557. case 4096:
  558. host->pmecc_degree = PMECC_GF_DIMENSION_13;
  559. host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
  560. host->pmecc_sector_number = mtd->writesize / sector_size;
  561. host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
  562. cap, sector_size);
  563. host->pmecc_alpha_to = pmecc_get_alpha_to(host);
  564. host->pmecc_index_of = host->pmecc_rom_base +
  565. host->pmecc_index_table_offset;
  566. nand->ecc.steps = 1;
  567. nand->ecc.bytes = host->pmecc_bytes_per_sector *
  568. host->pmecc_sector_number;
  569. if (nand->ecc.bytes > mtd->oobsize - 2) {
  570. printk(KERN_ERR "No room for ECC bytes\n");
  571. return -EINVAL;
  572. }
  573. pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
  574. mtd->oobsize,
  575. nand->ecc.bytes);
  576. nand->ecc.layout = &atmel_pmecc_oobinfo;
  577. break;
  578. case 512:
  579. case 1024:
  580. /* TODO */
  581. printk(KERN_ERR "Unsupported page size for PMECC, use Software ECC\n");
  582. default:
  583. /* page size not handled by HW ECC */
  584. /* switching back to soft ECC */
  585. nand->ecc.mode = NAND_ECC_SOFT;
  586. nand->ecc.read_page = NULL;
  587. nand->ecc.postpad = 0;
  588. nand->ecc.prepad = 0;
  589. nand->ecc.bytes = 0;
  590. return 0;
  591. }
  592. nand->ecc.read_page = atmel_nand_pmecc_read_page;
  593. nand->ecc.write_page = atmel_nand_pmecc_write_page;
  594. atmel_pmecc_core_init(mtd);
  595. return 0;
  596. }
  597. #else
  598. /* oob layout for large page size
  599. * bad block info is on bytes 0 and 1
  600. * the bytes have to be consecutives to avoid
  601. * several NAND_CMD_RNDOUT during read
  602. */
  603. static struct nand_ecclayout atmel_oobinfo_large = {
  604. .eccbytes = 4,
  605. .eccpos = {60, 61, 62, 63},
  606. .oobfree = {
  607. {2, 58}
  608. },
  609. };
  610. /* oob layout for small page size
  611. * bad block info is on bytes 4 and 5
  612. * the bytes have to be consecutives to avoid
  613. * several NAND_CMD_RNDOUT during read
  614. */
  615. static struct nand_ecclayout atmel_oobinfo_small = {
  616. .eccbytes = 4,
  617. .eccpos = {0, 1, 2, 3},
  618. .oobfree = {
  619. {6, 10}
  620. },
  621. };
  622. /*
  623. * Calculate HW ECC
  624. *
  625. * function called after a write
  626. *
  627. * mtd: MTD block structure
  628. * dat: raw data (unused)
  629. * ecc_code: buffer for ECC
  630. */
  631. static int atmel_nand_calculate(struct mtd_info *mtd,
  632. const u_char *dat, unsigned char *ecc_code)
  633. {
  634. unsigned int ecc_value;
  635. /* get the first 2 ECC bytes */
  636. ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR);
  637. ecc_code[0] = ecc_value & 0xFF;
  638. ecc_code[1] = (ecc_value >> 8) & 0xFF;
  639. /* get the last 2 ECC bytes */
  640. ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, NPR) & ATMEL_ECC_NPARITY;
  641. ecc_code[2] = ecc_value & 0xFF;
  642. ecc_code[3] = (ecc_value >> 8) & 0xFF;
  643. return 0;
  644. }
  645. /*
  646. * HW ECC read page function
  647. *
  648. * mtd: mtd info structure
  649. * chip: nand chip info structure
  650. * buf: buffer to store read data
  651. */
  652. static int atmel_nand_read_page(struct mtd_info *mtd,
  653. struct nand_chip *chip, uint8_t *buf, int page)
  654. {
  655. int eccsize = chip->ecc.size;
  656. int eccbytes = chip->ecc.bytes;
  657. uint32_t *eccpos = chip->ecc.layout->eccpos;
  658. uint8_t *p = buf;
  659. uint8_t *oob = chip->oob_poi;
  660. uint8_t *ecc_pos;
  661. int stat;
  662. /* read the page */
  663. chip->read_buf(mtd, p, eccsize);
  664. /* move to ECC position if needed */
  665. if (eccpos[0] != 0) {
  666. /* This only works on large pages
  667. * because the ECC controller waits for
  668. * NAND_CMD_RNDOUTSTART after the
  669. * NAND_CMD_RNDOUT.
  670. * anyway, for small pages, the eccpos[0] == 0
  671. */
  672. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  673. mtd->writesize + eccpos[0], -1);
  674. }
  675. /* the ECC controller needs to read the ECC just after the data */
  676. ecc_pos = oob + eccpos[0];
  677. chip->read_buf(mtd, ecc_pos, eccbytes);
  678. /* check if there's an error */
  679. stat = chip->ecc.correct(mtd, p, oob, NULL);
  680. if (stat < 0)
  681. mtd->ecc_stats.failed++;
  682. else
  683. mtd->ecc_stats.corrected += stat;
  684. /* get back to oob start (end of page) */
  685. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  686. /* read the oob */
  687. chip->read_buf(mtd, oob, mtd->oobsize);
  688. return 0;
  689. }
  690. /*
  691. * HW ECC Correction
  692. *
  693. * function called after a read
  694. *
  695. * mtd: MTD block structure
  696. * dat: raw data read from the chip
  697. * read_ecc: ECC from the chip (unused)
  698. * isnull: unused
  699. *
  700. * Detect and correct a 1 bit error for a page
  701. */
  702. static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
  703. u_char *read_ecc, u_char *isnull)
  704. {
  705. struct nand_chip *nand_chip = mtd->priv;
  706. unsigned int ecc_status;
  707. unsigned int ecc_word, ecc_bit;
  708. /* get the status from the Status Register */
  709. ecc_status = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, SR);
  710. /* if there's no error */
  711. if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
  712. return 0;
  713. /* get error bit offset (4 bits) */
  714. ecc_bit = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_BITADDR;
  715. /* get word address (12 bits) */
  716. ecc_word = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_WORDADDR;
  717. ecc_word >>= 4;
  718. /* if there are multiple errors */
  719. if (ecc_status & ATMEL_ECC_MULERR) {
  720. /* check if it is a freshly erased block
  721. * (filled with 0xff) */
  722. if ((ecc_bit == ATMEL_ECC_BITADDR)
  723. && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
  724. /* the block has just been erased, return OK */
  725. return 0;
  726. }
  727. /* it doesn't seems to be a freshly
  728. * erased block.
  729. * We can't correct so many errors */
  730. printk(KERN_WARNING "atmel_nand : multiple errors detected."
  731. " Unable to correct.\n");
  732. return -EIO;
  733. }
  734. /* if there's a single bit error : we can correct it */
  735. if (ecc_status & ATMEL_ECC_ECCERR) {
  736. /* there's nothing much to do here.
  737. * the bit error is on the ECC itself.
  738. */
  739. printk(KERN_WARNING "atmel_nand : one bit error on ECC code."
  740. " Nothing to correct\n");
  741. return 0;
  742. }
  743. printk(KERN_WARNING "atmel_nand : one bit error on data."
  744. " (word offset in the page :"
  745. " 0x%x bit offset : 0x%x)\n",
  746. ecc_word, ecc_bit);
  747. /* correct the error */
  748. if (nand_chip->options & NAND_BUSWIDTH_16) {
  749. /* 16 bits words */
  750. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  751. } else {
  752. /* 8 bits words */
  753. dat[ecc_word] ^= (1 << ecc_bit);
  754. }
  755. printk(KERN_WARNING "atmel_nand : error corrected\n");
  756. return 1;
  757. }
  758. /*
  759. * Enable HW ECC : unused on most chips
  760. */
  761. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  762. {
  763. }
  764. int atmel_hwecc_nand_init_param(struct nand_chip *nand, struct mtd_info *mtd)
  765. {
  766. nand->ecc.mode = NAND_ECC_HW;
  767. nand->ecc.calculate = atmel_nand_calculate;
  768. nand->ecc.correct = atmel_nand_correct;
  769. nand->ecc.hwctl = atmel_nand_hwctl;
  770. nand->ecc.read_page = atmel_nand_read_page;
  771. nand->ecc.bytes = 4;
  772. if (nand->ecc.mode == NAND_ECC_HW) {
  773. /* ECC is calculated for the whole page (1 step) */
  774. nand->ecc.size = mtd->writesize;
  775. /* set ECC page size and oob layout */
  776. switch (mtd->writesize) {
  777. case 512:
  778. nand->ecc.layout = &atmel_oobinfo_small;
  779. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  780. ATMEL_ECC_PAGESIZE_528);
  781. break;
  782. case 1024:
  783. nand->ecc.layout = &atmel_oobinfo_large;
  784. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  785. ATMEL_ECC_PAGESIZE_1056);
  786. break;
  787. case 2048:
  788. nand->ecc.layout = &atmel_oobinfo_large;
  789. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  790. ATMEL_ECC_PAGESIZE_2112);
  791. break;
  792. case 4096:
  793. nand->ecc.layout = &atmel_oobinfo_large;
  794. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  795. ATMEL_ECC_PAGESIZE_4224);
  796. break;
  797. default:
  798. /* page size not handled by HW ECC */
  799. /* switching back to soft ECC */
  800. nand->ecc.mode = NAND_ECC_SOFT;
  801. nand->ecc.calculate = NULL;
  802. nand->ecc.correct = NULL;
  803. nand->ecc.hwctl = NULL;
  804. nand->ecc.read_page = NULL;
  805. nand->ecc.postpad = 0;
  806. nand->ecc.prepad = 0;
  807. nand->ecc.bytes = 0;
  808. break;
  809. }
  810. }
  811. return 0;
  812. }
  813. #endif /* CONFIG_ATMEL_NAND_HW_PMECC */
  814. #endif /* CONFIG_ATMEL_NAND_HWECC */
  815. static void at91_nand_hwcontrol(struct mtd_info *mtd,
  816. int cmd, unsigned int ctrl)
  817. {
  818. struct nand_chip *this = mtd->priv;
  819. if (ctrl & NAND_CTRL_CHANGE) {
  820. ulong IO_ADDR_W = (ulong) this->IO_ADDR_W;
  821. IO_ADDR_W &= ~(CONFIG_SYS_NAND_MASK_ALE
  822. | CONFIG_SYS_NAND_MASK_CLE);
  823. if (ctrl & NAND_CLE)
  824. IO_ADDR_W |= CONFIG_SYS_NAND_MASK_CLE;
  825. if (ctrl & NAND_ALE)
  826. IO_ADDR_W |= CONFIG_SYS_NAND_MASK_ALE;
  827. #ifdef CONFIG_SYS_NAND_ENABLE_PIN
  828. at91_set_gpio_value(CONFIG_SYS_NAND_ENABLE_PIN,
  829. !(ctrl & NAND_NCE));
  830. #endif
  831. this->IO_ADDR_W = (void *) IO_ADDR_W;
  832. }
  833. if (cmd != NAND_CMD_NONE)
  834. writeb(cmd, this->IO_ADDR_W);
  835. }
  836. #ifdef CONFIG_SYS_NAND_READY_PIN
  837. static int at91_nand_ready(struct mtd_info *mtd)
  838. {
  839. return at91_get_gpio_value(CONFIG_SYS_NAND_READY_PIN);
  840. }
  841. #endif
  842. #ifndef CONFIG_SYS_NAND_BASE_LIST
  843. #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
  844. #endif
  845. static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
  846. static ulong base_addr[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
  847. int atmel_nand_chip_init(int devnum, ulong base_addr)
  848. {
  849. int ret;
  850. struct mtd_info *mtd = &nand_info[devnum];
  851. struct nand_chip *nand = &nand_chip[devnum];
  852. mtd->priv = nand;
  853. nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
  854. nand->ecc.mode = NAND_ECC_SOFT;
  855. #ifdef CONFIG_SYS_NAND_DBW_16
  856. nand->options = NAND_BUSWIDTH_16;
  857. #endif
  858. nand->cmd_ctrl = at91_nand_hwcontrol;
  859. #ifdef CONFIG_SYS_NAND_READY_PIN
  860. nand->dev_ready = at91_nand_ready;
  861. #endif
  862. nand->chip_delay = 20;
  863. ret = nand_scan_ident(mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
  864. if (ret)
  865. return ret;
  866. #ifdef CONFIG_ATMEL_NAND_HWECC
  867. #ifdef CONFIG_ATMEL_NAND_HW_PMECC
  868. ret = atmel_pmecc_nand_init_params(nand, mtd);
  869. #else
  870. ret = atmel_hwecc_nand_init_param(nand, mtd);
  871. #endif
  872. if (ret)
  873. return ret;
  874. #endif
  875. ret = nand_scan_tail(mtd);
  876. if (!ret)
  877. nand_register(devnum);
  878. return ret;
  879. }
  880. void board_nand_init(void)
  881. {
  882. int i;
  883. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
  884. if (atmel_nand_chip_init(i, base_addr[i]))
  885. printk(KERN_ERR "atmel_nand: Fail to initialize #%d chip",
  886. i);
  887. }