atmel_nand.c 36 KB

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