atmel_nand.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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 oob_required, 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 int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
  444. struct nand_chip *chip, const uint8_t *buf,
  445. int oob_required)
  446. {
  447. struct atmel_nand_host *host = chip->priv;
  448. uint32_t *eccpos = chip->ecc.layout->eccpos;
  449. int i, j;
  450. int timeout = PMECC_MAX_TIMEOUT_US;
  451. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
  452. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
  453. pmecc_writel(host->pmecc, cfg, (pmecc_readl(host->pmecc, cfg) |
  454. PMECC_CFG_WRITE_OP) & ~PMECC_CFG_AUTO_ENABLE);
  455. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
  456. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DATA);
  457. chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
  458. while (--timeout) {
  459. if (!(pmecc_readl(host->pmecc, sr) & PMECC_SR_BUSY))
  460. break;
  461. WATCHDOG_RESET();
  462. udelay(1);
  463. }
  464. if (!timeout) {
  465. printk(KERN_ERR "atmel_nand : Timeout to read PMECC status, fail to write PMECC in oob\n");
  466. goto out;
  467. }
  468. for (i = 0; i < host->pmecc_sector_number; i++) {
  469. for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
  470. int pos;
  471. pos = i * host->pmecc_bytes_per_sector + j;
  472. chip->oob_poi[eccpos[pos]] =
  473. readb(&host->pmecc->ecc_port[i].ecc[j]);
  474. }
  475. }
  476. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  477. out:
  478. return 0;
  479. }
  480. static void atmel_pmecc_core_init(struct mtd_info *mtd)
  481. {
  482. struct nand_chip *nand_chip = mtd->priv;
  483. struct atmel_nand_host *host = nand_chip->priv;
  484. uint32_t val = 0;
  485. struct nand_ecclayout *ecc_layout;
  486. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
  487. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
  488. switch (host->pmecc_corr_cap) {
  489. case 2:
  490. val = PMECC_CFG_BCH_ERR2;
  491. break;
  492. case 4:
  493. val = PMECC_CFG_BCH_ERR4;
  494. break;
  495. case 8:
  496. val = PMECC_CFG_BCH_ERR8;
  497. break;
  498. case 12:
  499. val = PMECC_CFG_BCH_ERR12;
  500. break;
  501. case 24:
  502. val = PMECC_CFG_BCH_ERR24;
  503. break;
  504. }
  505. if (host->pmecc_sector_size == 512)
  506. val |= PMECC_CFG_SECTOR512;
  507. else if (host->pmecc_sector_size == 1024)
  508. val |= PMECC_CFG_SECTOR1024;
  509. switch (host->pmecc_sector_number) {
  510. case 1:
  511. val |= PMECC_CFG_PAGE_1SECTOR;
  512. break;
  513. case 2:
  514. val |= PMECC_CFG_PAGE_2SECTORS;
  515. break;
  516. case 4:
  517. val |= PMECC_CFG_PAGE_4SECTORS;
  518. break;
  519. case 8:
  520. val |= PMECC_CFG_PAGE_8SECTORS;
  521. break;
  522. }
  523. val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
  524. | PMECC_CFG_AUTO_DISABLE);
  525. pmecc_writel(host->pmecc, cfg, val);
  526. ecc_layout = nand_chip->ecc.layout;
  527. pmecc_writel(host->pmecc, sarea, mtd->oobsize - 1);
  528. pmecc_writel(host->pmecc, saddr, ecc_layout->eccpos[0]);
  529. pmecc_writel(host->pmecc, eaddr,
  530. ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
  531. /* See datasheet about PMECC Clock Control Register */
  532. pmecc_writel(host->pmecc, clk, PMECC_CLK_133MHZ);
  533. pmecc_writel(host->pmecc, idr, 0xff);
  534. pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
  535. }
  536. static int atmel_pmecc_nand_init_params(struct nand_chip *nand,
  537. struct mtd_info *mtd)
  538. {
  539. struct atmel_nand_host *host;
  540. int cap, sector_size;
  541. host = nand->priv = &pmecc_host;
  542. nand->ecc.mode = NAND_ECC_HW;
  543. nand->ecc.calculate = NULL;
  544. nand->ecc.correct = NULL;
  545. nand->ecc.hwctl = NULL;
  546. cap = host->pmecc_corr_cap = CONFIG_PMECC_CAP;
  547. sector_size = host->pmecc_sector_size = CONFIG_PMECC_SECTOR_SIZE;
  548. host->pmecc_index_table_offset = CONFIG_PMECC_INDEX_TABLE_OFFSET;
  549. MTDDEBUG(MTD_DEBUG_LEVEL1,
  550. "Initialize PMECC params, cap: %d, sector: %d\n",
  551. cap, sector_size);
  552. host->pmecc = (struct pmecc_regs __iomem *) ATMEL_BASE_PMECC;
  553. host->pmerrloc = (struct pmecc_errloc_regs __iomem *)
  554. ATMEL_BASE_PMERRLOC;
  555. host->pmecc_rom_base = (void __iomem *) ATMEL_BASE_ROM;
  556. /* ECC is calculated for the whole page (1 step) */
  557. nand->ecc.size = mtd->writesize;
  558. /* set ECC page size and oob layout */
  559. switch (mtd->writesize) {
  560. case 2048:
  561. case 4096:
  562. host->pmecc_degree = PMECC_GF_DIMENSION_13;
  563. host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
  564. host->pmecc_sector_number = mtd->writesize / sector_size;
  565. host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
  566. cap, sector_size);
  567. host->pmecc_alpha_to = pmecc_get_alpha_to(host);
  568. host->pmecc_index_of = host->pmecc_rom_base +
  569. host->pmecc_index_table_offset;
  570. nand->ecc.steps = 1;
  571. nand->ecc.bytes = host->pmecc_bytes_per_sector *
  572. host->pmecc_sector_number;
  573. if (nand->ecc.bytes > mtd->oobsize - 2) {
  574. printk(KERN_ERR "No room for ECC bytes\n");
  575. return -EINVAL;
  576. }
  577. pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
  578. mtd->oobsize,
  579. nand->ecc.bytes);
  580. nand->ecc.layout = &atmel_pmecc_oobinfo;
  581. break;
  582. case 512:
  583. case 1024:
  584. /* TODO */
  585. printk(KERN_ERR "Unsupported page size for PMECC, use Software ECC\n");
  586. default:
  587. /* page size not handled by HW ECC */
  588. /* switching back to soft ECC */
  589. nand->ecc.mode = NAND_ECC_SOFT;
  590. nand->ecc.read_page = NULL;
  591. nand->ecc.postpad = 0;
  592. nand->ecc.prepad = 0;
  593. nand->ecc.bytes = 0;
  594. return 0;
  595. }
  596. nand->ecc.read_page = atmel_nand_pmecc_read_page;
  597. nand->ecc.write_page = atmel_nand_pmecc_write_page;
  598. nand->ecc.strength = cap;
  599. atmel_pmecc_core_init(mtd);
  600. return 0;
  601. }
  602. #else
  603. /* oob layout for large page size
  604. * bad block info is on bytes 0 and 1
  605. * the bytes have to be consecutives to avoid
  606. * several NAND_CMD_RNDOUT during read
  607. */
  608. static struct nand_ecclayout atmel_oobinfo_large = {
  609. .eccbytes = 4,
  610. .eccpos = {60, 61, 62, 63},
  611. .oobfree = {
  612. {2, 58}
  613. },
  614. };
  615. /* oob layout for small page size
  616. * bad block info is on bytes 4 and 5
  617. * the bytes have to be consecutives to avoid
  618. * several NAND_CMD_RNDOUT during read
  619. */
  620. static struct nand_ecclayout atmel_oobinfo_small = {
  621. .eccbytes = 4,
  622. .eccpos = {0, 1, 2, 3},
  623. .oobfree = {
  624. {6, 10}
  625. },
  626. };
  627. /*
  628. * Calculate HW ECC
  629. *
  630. * function called after a write
  631. *
  632. * mtd: MTD block structure
  633. * dat: raw data (unused)
  634. * ecc_code: buffer for ECC
  635. */
  636. static int atmel_nand_calculate(struct mtd_info *mtd,
  637. const u_char *dat, unsigned char *ecc_code)
  638. {
  639. unsigned int ecc_value;
  640. /* get the first 2 ECC bytes */
  641. ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR);
  642. ecc_code[0] = ecc_value & 0xFF;
  643. ecc_code[1] = (ecc_value >> 8) & 0xFF;
  644. /* get the last 2 ECC bytes */
  645. ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, NPR) & ATMEL_ECC_NPARITY;
  646. ecc_code[2] = ecc_value & 0xFF;
  647. ecc_code[3] = (ecc_value >> 8) & 0xFF;
  648. return 0;
  649. }
  650. /*
  651. * HW ECC read page function
  652. *
  653. * mtd: mtd info structure
  654. * chip: nand chip info structure
  655. * buf: buffer to store read data
  656. * oob_required: caller expects OOB data read to chip->oob_poi
  657. */
  658. static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  659. uint8_t *buf, int oob_required, int page)
  660. {
  661. int eccsize = chip->ecc.size;
  662. int eccbytes = chip->ecc.bytes;
  663. uint32_t *eccpos = chip->ecc.layout->eccpos;
  664. uint8_t *p = buf;
  665. uint8_t *oob = chip->oob_poi;
  666. uint8_t *ecc_pos;
  667. int stat;
  668. /* read the page */
  669. chip->read_buf(mtd, p, eccsize);
  670. /* move to ECC position if needed */
  671. if (eccpos[0] != 0) {
  672. /* This only works on large pages
  673. * because the ECC controller waits for
  674. * NAND_CMD_RNDOUTSTART after the
  675. * NAND_CMD_RNDOUT.
  676. * anyway, for small pages, the eccpos[0] == 0
  677. */
  678. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  679. mtd->writesize + eccpos[0], -1);
  680. }
  681. /* the ECC controller needs to read the ECC just after the data */
  682. ecc_pos = oob + eccpos[0];
  683. chip->read_buf(mtd, ecc_pos, eccbytes);
  684. /* check if there's an error */
  685. stat = chip->ecc.correct(mtd, p, oob, NULL);
  686. if (stat < 0)
  687. mtd->ecc_stats.failed++;
  688. else
  689. mtd->ecc_stats.corrected += stat;
  690. /* get back to oob start (end of page) */
  691. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  692. /* read the oob */
  693. chip->read_buf(mtd, oob, mtd->oobsize);
  694. return 0;
  695. }
  696. /*
  697. * HW ECC Correction
  698. *
  699. * function called after a read
  700. *
  701. * mtd: MTD block structure
  702. * dat: raw data read from the chip
  703. * read_ecc: ECC from the chip (unused)
  704. * isnull: unused
  705. *
  706. * Detect and correct a 1 bit error for a page
  707. */
  708. static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
  709. u_char *read_ecc, u_char *isnull)
  710. {
  711. struct nand_chip *nand_chip = mtd->priv;
  712. unsigned int ecc_status;
  713. unsigned int ecc_word, ecc_bit;
  714. /* get the status from the Status Register */
  715. ecc_status = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, SR);
  716. /* if there's no error */
  717. if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
  718. return 0;
  719. /* get error bit offset (4 bits) */
  720. ecc_bit = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_BITADDR;
  721. /* get word address (12 bits) */
  722. ecc_word = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_WORDADDR;
  723. ecc_word >>= 4;
  724. /* if there are multiple errors */
  725. if (ecc_status & ATMEL_ECC_MULERR) {
  726. /* check if it is a freshly erased block
  727. * (filled with 0xff) */
  728. if ((ecc_bit == ATMEL_ECC_BITADDR)
  729. && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
  730. /* the block has just been erased, return OK */
  731. return 0;
  732. }
  733. /* it doesn't seems to be a freshly
  734. * erased block.
  735. * We can't correct so many errors */
  736. printk(KERN_WARNING "atmel_nand : multiple errors detected."
  737. " Unable to correct.\n");
  738. return -EIO;
  739. }
  740. /* if there's a single bit error : we can correct it */
  741. if (ecc_status & ATMEL_ECC_ECCERR) {
  742. /* there's nothing much to do here.
  743. * the bit error is on the ECC itself.
  744. */
  745. printk(KERN_WARNING "atmel_nand : one bit error on ECC code."
  746. " Nothing to correct\n");
  747. return 0;
  748. }
  749. printk(KERN_WARNING "atmel_nand : one bit error on data."
  750. " (word offset in the page :"
  751. " 0x%x bit offset : 0x%x)\n",
  752. ecc_word, ecc_bit);
  753. /* correct the error */
  754. if (nand_chip->options & NAND_BUSWIDTH_16) {
  755. /* 16 bits words */
  756. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  757. } else {
  758. /* 8 bits words */
  759. dat[ecc_word] ^= (1 << ecc_bit);
  760. }
  761. printk(KERN_WARNING "atmel_nand : error corrected\n");
  762. return 1;
  763. }
  764. /*
  765. * Enable HW ECC : unused on most chips
  766. */
  767. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  768. {
  769. }
  770. int atmel_hwecc_nand_init_param(struct nand_chip *nand, struct mtd_info *mtd)
  771. {
  772. nand->ecc.mode = NAND_ECC_HW;
  773. nand->ecc.calculate = atmel_nand_calculate;
  774. nand->ecc.correct = atmel_nand_correct;
  775. nand->ecc.hwctl = atmel_nand_hwctl;
  776. nand->ecc.read_page = atmel_nand_read_page;
  777. nand->ecc.bytes = 4;
  778. if (nand->ecc.mode == NAND_ECC_HW) {
  779. /* ECC is calculated for the whole page (1 step) */
  780. nand->ecc.size = mtd->writesize;
  781. /* set ECC page size and oob layout */
  782. switch (mtd->writesize) {
  783. case 512:
  784. nand->ecc.layout = &atmel_oobinfo_small;
  785. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  786. ATMEL_ECC_PAGESIZE_528);
  787. break;
  788. case 1024:
  789. nand->ecc.layout = &atmel_oobinfo_large;
  790. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  791. ATMEL_ECC_PAGESIZE_1056);
  792. break;
  793. case 2048:
  794. nand->ecc.layout = &atmel_oobinfo_large;
  795. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  796. ATMEL_ECC_PAGESIZE_2112);
  797. break;
  798. case 4096:
  799. nand->ecc.layout = &atmel_oobinfo_large;
  800. ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
  801. ATMEL_ECC_PAGESIZE_4224);
  802. break;
  803. default:
  804. /* page size not handled by HW ECC */
  805. /* switching back to soft ECC */
  806. nand->ecc.mode = NAND_ECC_SOFT;
  807. nand->ecc.calculate = NULL;
  808. nand->ecc.correct = NULL;
  809. nand->ecc.hwctl = NULL;
  810. nand->ecc.read_page = NULL;
  811. nand->ecc.postpad = 0;
  812. nand->ecc.prepad = 0;
  813. nand->ecc.bytes = 0;
  814. break;
  815. }
  816. }
  817. return 0;
  818. }
  819. #endif /* CONFIG_ATMEL_NAND_HW_PMECC */
  820. #endif /* CONFIG_ATMEL_NAND_HWECC */
  821. static void at91_nand_hwcontrol(struct mtd_info *mtd,
  822. int cmd, unsigned int ctrl)
  823. {
  824. struct nand_chip *this = mtd->priv;
  825. if (ctrl & NAND_CTRL_CHANGE) {
  826. ulong IO_ADDR_W = (ulong) this->IO_ADDR_W;
  827. IO_ADDR_W &= ~(CONFIG_SYS_NAND_MASK_ALE
  828. | CONFIG_SYS_NAND_MASK_CLE);
  829. if (ctrl & NAND_CLE)
  830. IO_ADDR_W |= CONFIG_SYS_NAND_MASK_CLE;
  831. if (ctrl & NAND_ALE)
  832. IO_ADDR_W |= CONFIG_SYS_NAND_MASK_ALE;
  833. #ifdef CONFIG_SYS_NAND_ENABLE_PIN
  834. at91_set_gpio_value(CONFIG_SYS_NAND_ENABLE_PIN,
  835. !(ctrl & NAND_NCE));
  836. #endif
  837. this->IO_ADDR_W = (void *) IO_ADDR_W;
  838. }
  839. if (cmd != NAND_CMD_NONE)
  840. writeb(cmd, this->IO_ADDR_W);
  841. }
  842. #ifdef CONFIG_SYS_NAND_READY_PIN
  843. static int at91_nand_ready(struct mtd_info *mtd)
  844. {
  845. return at91_get_gpio_value(CONFIG_SYS_NAND_READY_PIN);
  846. }
  847. #endif
  848. #ifndef CONFIG_SYS_NAND_BASE_LIST
  849. #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
  850. #endif
  851. static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
  852. static ulong base_addr[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
  853. int atmel_nand_chip_init(int devnum, ulong base_addr)
  854. {
  855. int ret;
  856. struct mtd_info *mtd = &nand_info[devnum];
  857. struct nand_chip *nand = &nand_chip[devnum];
  858. mtd->priv = nand;
  859. nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
  860. nand->ecc.mode = NAND_ECC_SOFT;
  861. #ifdef CONFIG_SYS_NAND_DBW_16
  862. nand->options = NAND_BUSWIDTH_16;
  863. #endif
  864. nand->cmd_ctrl = at91_nand_hwcontrol;
  865. #ifdef CONFIG_SYS_NAND_READY_PIN
  866. nand->dev_ready = at91_nand_ready;
  867. #endif
  868. nand->chip_delay = 20;
  869. ret = nand_scan_ident(mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
  870. if (ret)
  871. return ret;
  872. #ifdef CONFIG_ATMEL_NAND_HWECC
  873. #ifdef CONFIG_ATMEL_NAND_HW_PMECC
  874. ret = atmel_pmecc_nand_init_params(nand, mtd);
  875. #else
  876. ret = atmel_hwecc_nand_init_param(nand, mtd);
  877. #endif
  878. if (ret)
  879. return ret;
  880. #endif
  881. ret = nand_scan_tail(mtd);
  882. if (!ret)
  883. nand_register(devnum);
  884. return ret;
  885. }
  886. void board_nand_init(void)
  887. {
  888. int i;
  889. for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
  890. if (atmel_nand_chip_init(i, base_addr[i]))
  891. printk(KERN_ERR "atmel_nand: Fail to initialize #%d chip",
  892. i);
  893. }