atmel_nand.c 39 KB

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