atmel_nand.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  1. /*
  2. * Copyright © 2003 Rick Bronson
  3. *
  4. * Derived from drivers/mtd/nand/autcpu12.c
  5. * Copyright © 2001 Thomas Gleixner (gleixner@autronix.de)
  6. *
  7. * Derived from drivers/mtd/spia.c
  8. * Copyright © 2000 Steven J. Hill (sjhill@cotw.com)
  9. *
  10. *
  11. * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263
  12. * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright © 2007
  13. *
  14. * Derived from Das U-Boot source code
  15. * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c)
  16. * © Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
  17. *
  18. * Add Programmable Multibit ECC support for various AT91 SoC
  19. * © Copyright 2012 ATMEL, Hong Xu
  20. *
  21. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU General Public License version 2 as
  23. * published by the Free Software Foundation.
  24. *
  25. */
  26. #include <linux/dma-mapping.h>
  27. #include <linux/slab.h>
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/of.h>
  32. #include <linux/of_device.h>
  33. #include <linux/of_gpio.h>
  34. #include <linux/of_mtd.h>
  35. #include <linux/mtd/mtd.h>
  36. #include <linux/mtd/nand.h>
  37. #include <linux/mtd/partitions.h>
  38. #include <linux/dmaengine.h>
  39. #include <linux/gpio.h>
  40. #include <linux/io.h>
  41. #include <linux/platform_data/atmel.h>
  42. static int use_dma = 1;
  43. module_param(use_dma, int, 0);
  44. static int on_flash_bbt = 0;
  45. module_param(on_flash_bbt, int, 0);
  46. /* Register access macros */
  47. #define ecc_readl(add, reg) \
  48. __raw_readl(add + ATMEL_ECC_##reg)
  49. #define ecc_writel(add, reg, value) \
  50. __raw_writel((value), add + ATMEL_ECC_##reg)
  51. #include "atmel_nand_ecc.h" /* Hardware ECC registers */
  52. /* oob layout for large page size
  53. * bad block info is on bytes 0 and 1
  54. * the bytes have to be consecutives to avoid
  55. * several NAND_CMD_RNDOUT during read
  56. */
  57. static struct nand_ecclayout atmel_oobinfo_large = {
  58. .eccbytes = 4,
  59. .eccpos = {60, 61, 62, 63},
  60. .oobfree = {
  61. {2, 58}
  62. },
  63. };
  64. /* oob layout for small page size
  65. * bad block info is on bytes 4 and 5
  66. * the bytes have to be consecutives to avoid
  67. * several NAND_CMD_RNDOUT during read
  68. */
  69. static struct nand_ecclayout atmel_oobinfo_small = {
  70. .eccbytes = 4,
  71. .eccpos = {0, 1, 2, 3},
  72. .oobfree = {
  73. {6, 10}
  74. },
  75. };
  76. struct atmel_nand_host {
  77. struct nand_chip nand_chip;
  78. struct mtd_info mtd;
  79. void __iomem *io_base;
  80. dma_addr_t io_phys;
  81. struct atmel_nand_data board;
  82. struct device *dev;
  83. void __iomem *ecc;
  84. struct completion comp;
  85. struct dma_chan *dma_chan;
  86. bool has_pmecc;
  87. u8 pmecc_corr_cap;
  88. u16 pmecc_sector_size;
  89. u32 pmecc_lookup_table_offset;
  90. u32 pmecc_lookup_table_offset_512;
  91. u32 pmecc_lookup_table_offset_1024;
  92. int pmecc_bytes_per_sector;
  93. int pmecc_sector_number;
  94. int pmecc_degree; /* Degree of remainders */
  95. int pmecc_cw_len; /* Length of codeword */
  96. void __iomem *pmerrloc_base;
  97. void __iomem *pmecc_rom_base;
  98. /* lookup table for alpha_to and index_of */
  99. void __iomem *pmecc_alpha_to;
  100. void __iomem *pmecc_index_of;
  101. /* data for pmecc computation */
  102. int16_t *pmecc_partial_syn;
  103. int16_t *pmecc_si;
  104. int16_t *pmecc_smu; /* Sigma table */
  105. int16_t *pmecc_lmu; /* polynomal order */
  106. int *pmecc_mu;
  107. int *pmecc_dmu;
  108. int *pmecc_delta;
  109. };
  110. static struct nand_ecclayout atmel_pmecc_oobinfo;
  111. /*
  112. * Enable NAND.
  113. */
  114. static void atmel_nand_enable(struct atmel_nand_host *host)
  115. {
  116. if (gpio_is_valid(host->board.enable_pin))
  117. gpio_set_value(host->board.enable_pin, 0);
  118. }
  119. /*
  120. * Disable NAND.
  121. */
  122. static void atmel_nand_disable(struct atmel_nand_host *host)
  123. {
  124. if (gpio_is_valid(host->board.enable_pin))
  125. gpio_set_value(host->board.enable_pin, 1);
  126. }
  127. /*
  128. * Hardware specific access to control-lines
  129. */
  130. static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  131. {
  132. struct nand_chip *nand_chip = mtd->priv;
  133. struct atmel_nand_host *host = nand_chip->priv;
  134. if (ctrl & NAND_CTRL_CHANGE) {
  135. if (ctrl & NAND_NCE)
  136. atmel_nand_enable(host);
  137. else
  138. atmel_nand_disable(host);
  139. }
  140. if (cmd == NAND_CMD_NONE)
  141. return;
  142. if (ctrl & NAND_CLE)
  143. writeb(cmd, host->io_base + (1 << host->board.cle));
  144. else
  145. writeb(cmd, host->io_base + (1 << host->board.ale));
  146. }
  147. /*
  148. * Read the Device Ready pin.
  149. */
  150. static int atmel_nand_device_ready(struct mtd_info *mtd)
  151. {
  152. struct nand_chip *nand_chip = mtd->priv;
  153. struct atmel_nand_host *host = nand_chip->priv;
  154. return gpio_get_value(host->board.rdy_pin) ^
  155. !!host->board.rdy_pin_active_low;
  156. }
  157. /*
  158. * Minimal-overhead PIO for data access.
  159. */
  160. static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len)
  161. {
  162. struct nand_chip *nand_chip = mtd->priv;
  163. __raw_readsb(nand_chip->IO_ADDR_R, buf, len);
  164. }
  165. static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len)
  166. {
  167. struct nand_chip *nand_chip = mtd->priv;
  168. __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2);
  169. }
  170. static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len)
  171. {
  172. struct nand_chip *nand_chip = mtd->priv;
  173. __raw_writesb(nand_chip->IO_ADDR_W, buf, len);
  174. }
  175. static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len)
  176. {
  177. struct nand_chip *nand_chip = mtd->priv;
  178. __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2);
  179. }
  180. static void dma_complete_func(void *completion)
  181. {
  182. complete(completion);
  183. }
  184. static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len,
  185. int is_read)
  186. {
  187. struct dma_device *dma_dev;
  188. enum dma_ctrl_flags flags;
  189. dma_addr_t dma_src_addr, dma_dst_addr, phys_addr;
  190. struct dma_async_tx_descriptor *tx = NULL;
  191. dma_cookie_t cookie;
  192. struct nand_chip *chip = mtd->priv;
  193. struct atmel_nand_host *host = chip->priv;
  194. void *p = buf;
  195. int err = -EIO;
  196. enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  197. if (buf >= high_memory)
  198. goto err_buf;
  199. dma_dev = host->dma_chan->device;
  200. flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT | DMA_COMPL_SKIP_SRC_UNMAP |
  201. DMA_COMPL_SKIP_DEST_UNMAP;
  202. phys_addr = dma_map_single(dma_dev->dev, p, len, dir);
  203. if (dma_mapping_error(dma_dev->dev, phys_addr)) {
  204. dev_err(host->dev, "Failed to dma_map_single\n");
  205. goto err_buf;
  206. }
  207. if (is_read) {
  208. dma_src_addr = host->io_phys;
  209. dma_dst_addr = phys_addr;
  210. } else {
  211. dma_src_addr = phys_addr;
  212. dma_dst_addr = host->io_phys;
  213. }
  214. tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr,
  215. dma_src_addr, len, flags);
  216. if (!tx) {
  217. dev_err(host->dev, "Failed to prepare DMA memcpy\n");
  218. goto err_dma;
  219. }
  220. init_completion(&host->comp);
  221. tx->callback = dma_complete_func;
  222. tx->callback_param = &host->comp;
  223. cookie = tx->tx_submit(tx);
  224. if (dma_submit_error(cookie)) {
  225. dev_err(host->dev, "Failed to do DMA tx_submit\n");
  226. goto err_dma;
  227. }
  228. dma_async_issue_pending(host->dma_chan);
  229. wait_for_completion(&host->comp);
  230. err = 0;
  231. err_dma:
  232. dma_unmap_single(dma_dev->dev, phys_addr, len, dir);
  233. err_buf:
  234. if (err != 0)
  235. dev_warn(host->dev, "Fall back to CPU I/O\n");
  236. return err;
  237. }
  238. static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len)
  239. {
  240. struct nand_chip *chip = mtd->priv;
  241. struct atmel_nand_host *host = chip->priv;
  242. if (use_dma && len > mtd->oobsize)
  243. /* only use DMA for bigger than oob size: better performances */
  244. if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
  245. return;
  246. if (host->board.bus_width_16)
  247. atmel_read_buf16(mtd, buf, len);
  248. else
  249. atmel_read_buf8(mtd, buf, len);
  250. }
  251. static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
  252. {
  253. struct nand_chip *chip = mtd->priv;
  254. struct atmel_nand_host *host = chip->priv;
  255. if (use_dma && len > mtd->oobsize)
  256. /* only use DMA for bigger than oob size: better performances */
  257. if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0)
  258. return;
  259. if (host->board.bus_width_16)
  260. atmel_write_buf16(mtd, buf, len);
  261. else
  262. atmel_write_buf8(mtd, buf, len);
  263. }
  264. /*
  265. * Return number of ecc bytes per sector according to sector size and
  266. * correction capability
  267. *
  268. * Following table shows what at91 PMECC supported:
  269. * Correction Capability Sector_512_bytes Sector_1024_bytes
  270. * ===================== ================ =================
  271. * 2-bits 4-bytes 4-bytes
  272. * 4-bits 7-bytes 7-bytes
  273. * 8-bits 13-bytes 14-bytes
  274. * 12-bits 20-bytes 21-bytes
  275. * 24-bits 39-bytes 42-bytes
  276. */
  277. static int pmecc_get_ecc_bytes(int cap, int sector_size)
  278. {
  279. int m = 12 + sector_size / 512;
  280. return (m * cap + 7) / 8;
  281. }
  282. static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
  283. int oobsize, int ecc_len)
  284. {
  285. int i;
  286. layout->eccbytes = ecc_len;
  287. /* ECC will occupy the last ecc_len bytes continuously */
  288. for (i = 0; i < ecc_len; i++)
  289. layout->eccpos[i] = oobsize - ecc_len + i;
  290. layout->oobfree[0].offset = 2;
  291. layout->oobfree[0].length =
  292. oobsize - ecc_len - layout->oobfree[0].offset;
  293. }
  294. static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
  295. {
  296. int table_size;
  297. table_size = host->pmecc_sector_size == 512 ?
  298. PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024;
  299. return host->pmecc_rom_base + host->pmecc_lookup_table_offset +
  300. table_size * sizeof(int16_t);
  301. }
  302. static int pmecc_data_alloc(struct atmel_nand_host *host)
  303. {
  304. const int cap = host->pmecc_corr_cap;
  305. int size;
  306. size = (2 * cap + 1) * sizeof(int16_t);
  307. host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL);
  308. host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL);
  309. host->pmecc_lmu = devm_kzalloc(host->dev,
  310. (cap + 1) * sizeof(int16_t), GFP_KERNEL);
  311. host->pmecc_smu = devm_kzalloc(host->dev,
  312. (cap + 2) * size, GFP_KERNEL);
  313. size = (cap + 1) * sizeof(int);
  314. host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL);
  315. host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL);
  316. host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL);
  317. if (!host->pmecc_partial_syn ||
  318. !host->pmecc_si ||
  319. !host->pmecc_lmu ||
  320. !host->pmecc_smu ||
  321. !host->pmecc_mu ||
  322. !host->pmecc_dmu ||
  323. !host->pmecc_delta)
  324. return -ENOMEM;
  325. return 0;
  326. }
  327. static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
  328. {
  329. struct nand_chip *nand_chip = mtd->priv;
  330. struct atmel_nand_host *host = nand_chip->priv;
  331. int i;
  332. uint32_t value;
  333. /* Fill odd syndromes */
  334. for (i = 0; i < host->pmecc_corr_cap; i++) {
  335. value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2);
  336. if (i & 1)
  337. value >>= 16;
  338. value &= 0xffff;
  339. host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
  340. }
  341. }
  342. static void pmecc_substitute(struct mtd_info *mtd)
  343. {
  344. struct nand_chip *nand_chip = mtd->priv;
  345. struct atmel_nand_host *host = nand_chip->priv;
  346. int16_t __iomem *alpha_to = host->pmecc_alpha_to;
  347. int16_t __iomem *index_of = host->pmecc_index_of;
  348. int16_t *partial_syn = host->pmecc_partial_syn;
  349. const int cap = host->pmecc_corr_cap;
  350. int16_t *si;
  351. int i, j;
  352. /* si[] is a table that holds the current syndrome value,
  353. * an element of that table belongs to the field
  354. */
  355. si = host->pmecc_si;
  356. memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
  357. /* Computation 2t syndromes based on S(x) */
  358. /* Odd syndromes */
  359. for (i = 1; i < 2 * cap; i += 2) {
  360. for (j = 0; j < host->pmecc_degree; j++) {
  361. if (partial_syn[i] & ((unsigned short)0x1 << j))
  362. si[i] = readw_relaxed(alpha_to + i * j) ^ si[i];
  363. }
  364. }
  365. /* Even syndrome = (Odd syndrome) ** 2 */
  366. for (i = 2, j = 1; j <= cap; i = ++j << 1) {
  367. if (si[j] == 0) {
  368. si[i] = 0;
  369. } else {
  370. int16_t tmp;
  371. tmp = readw_relaxed(index_of + si[j]);
  372. tmp = (tmp * 2) % host->pmecc_cw_len;
  373. si[i] = readw_relaxed(alpha_to + tmp);
  374. }
  375. }
  376. return;
  377. }
  378. static void pmecc_get_sigma(struct mtd_info *mtd)
  379. {
  380. struct nand_chip *nand_chip = mtd->priv;
  381. struct atmel_nand_host *host = nand_chip->priv;
  382. int16_t *lmu = host->pmecc_lmu;
  383. int16_t *si = host->pmecc_si;
  384. int *mu = host->pmecc_mu;
  385. int *dmu = host->pmecc_dmu; /* Discrepancy */
  386. int *delta = host->pmecc_delta; /* Delta order */
  387. int cw_len = host->pmecc_cw_len;
  388. const int16_t cap = host->pmecc_corr_cap;
  389. const int num = 2 * cap + 1;
  390. int16_t __iomem *index_of = host->pmecc_index_of;
  391. int16_t __iomem *alpha_to = host->pmecc_alpha_to;
  392. int i, j, k;
  393. uint32_t dmu_0_count, tmp;
  394. int16_t *smu = host->pmecc_smu;
  395. /* index of largest delta */
  396. int ro;
  397. int largest;
  398. int diff;
  399. dmu_0_count = 0;
  400. /* First Row */
  401. /* Mu */
  402. mu[0] = -1;
  403. memset(smu, 0, sizeof(int16_t) * num);
  404. smu[0] = 1;
  405. /* discrepancy set to 1 */
  406. dmu[0] = 1;
  407. /* polynom order set to 0 */
  408. lmu[0] = 0;
  409. delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
  410. /* Second Row */
  411. /* Mu */
  412. mu[1] = 0;
  413. /* Sigma(x) set to 1 */
  414. memset(&smu[num], 0, sizeof(int16_t) * num);
  415. smu[num] = 1;
  416. /* discrepancy set to S1 */
  417. dmu[1] = si[1];
  418. /* polynom order set to 0 */
  419. lmu[1] = 0;
  420. delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
  421. /* Init the Sigma(x) last row */
  422. memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num);
  423. for (i = 1; i <= cap; i++) {
  424. mu[i + 1] = i << 1;
  425. /* Begin Computing Sigma (Mu+1) and L(mu) */
  426. /* check if discrepancy is set to 0 */
  427. if (dmu[i] == 0) {
  428. dmu_0_count++;
  429. tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
  430. if ((cap - (lmu[i] >> 1) - 1) & 0x1)
  431. tmp += 2;
  432. else
  433. tmp += 1;
  434. if (dmu_0_count == tmp) {
  435. for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
  436. smu[(cap + 1) * num + j] =
  437. smu[i * num + j];
  438. lmu[cap + 1] = lmu[i];
  439. return;
  440. }
  441. /* copy polynom */
  442. for (j = 0; j <= lmu[i] >> 1; j++)
  443. smu[(i + 1) * num + j] = smu[i * num + j];
  444. /* copy previous polynom order to the next */
  445. lmu[i + 1] = lmu[i];
  446. } else {
  447. ro = 0;
  448. largest = -1;
  449. /* find largest delta with dmu != 0 */
  450. for (j = 0; j < i; j++) {
  451. if ((dmu[j]) && (delta[j] > largest)) {
  452. largest = delta[j];
  453. ro = j;
  454. }
  455. }
  456. /* compute difference */
  457. diff = (mu[i] - mu[ro]);
  458. /* Compute degree of the new smu polynomial */
  459. if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
  460. lmu[i + 1] = lmu[i];
  461. else
  462. lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
  463. /* Init smu[i+1] with 0 */
  464. for (k = 0; k < num; k++)
  465. smu[(i + 1) * num + k] = 0;
  466. /* Compute smu[i+1] */
  467. for (k = 0; k <= lmu[ro] >> 1; k++) {
  468. int16_t a, b, c;
  469. if (!(smu[ro * num + k] && dmu[i]))
  470. continue;
  471. a = readw_relaxed(index_of + dmu[i]);
  472. b = readw_relaxed(index_of + dmu[ro]);
  473. c = readw_relaxed(index_of + smu[ro * num + k]);
  474. tmp = a + (cw_len - b) + c;
  475. a = readw_relaxed(alpha_to + tmp % cw_len);
  476. smu[(i + 1) * num + (k + diff)] = a;
  477. }
  478. for (k = 0; k <= lmu[i] >> 1; k++)
  479. smu[(i + 1) * num + k] ^= smu[i * num + k];
  480. }
  481. /* End Computing Sigma (Mu+1) and L(mu) */
  482. /* In either case compute delta */
  483. delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
  484. /* Do not compute discrepancy for the last iteration */
  485. if (i >= cap)
  486. continue;
  487. for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
  488. tmp = 2 * (i - 1);
  489. if (k == 0) {
  490. dmu[i + 1] = si[tmp + 3];
  491. } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
  492. int16_t a, b, c;
  493. a = readw_relaxed(index_of +
  494. smu[(i + 1) * num + k]);
  495. b = si[2 * (i - 1) + 3 - k];
  496. c = readw_relaxed(index_of + b);
  497. tmp = a + c;
  498. tmp %= cw_len;
  499. dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^
  500. dmu[i + 1];
  501. }
  502. }
  503. }
  504. return;
  505. }
  506. static int pmecc_err_location(struct mtd_info *mtd)
  507. {
  508. struct nand_chip *nand_chip = mtd->priv;
  509. struct atmel_nand_host *host = nand_chip->priv;
  510. unsigned long end_time;
  511. const int cap = host->pmecc_corr_cap;
  512. const int num = 2 * cap + 1;
  513. int sector_size = host->pmecc_sector_size;
  514. int err_nbr = 0; /* number of error */
  515. int roots_nbr; /* number of roots */
  516. int i;
  517. uint32_t val;
  518. int16_t *smu = host->pmecc_smu;
  519. pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE);
  520. for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
  521. pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i,
  522. smu[(cap + 1) * num + i]);
  523. err_nbr++;
  524. }
  525. val = (err_nbr - 1) << 16;
  526. if (sector_size == 1024)
  527. val |= 1;
  528. pmerrloc_writel(host->pmerrloc_base, ELCFG, val);
  529. pmerrloc_writel(host->pmerrloc_base, ELEN,
  530. sector_size * 8 + host->pmecc_degree * cap);
  531. end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
  532. while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
  533. & PMERRLOC_CALC_DONE)) {
  534. if (unlikely(time_after(jiffies, end_time))) {
  535. dev_err(host->dev, "PMECC: Timeout to calculate error location.\n");
  536. return -1;
  537. }
  538. cpu_relax();
  539. }
  540. roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR)
  541. & PMERRLOC_ERR_NUM_MASK) >> 8;
  542. /* Number of roots == degree of smu hence <= cap */
  543. if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
  544. return err_nbr - 1;
  545. /* Number of roots does not match the degree of smu
  546. * unable to correct error */
  547. return -1;
  548. }
  549. static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
  550. int sector_num, int extra_bytes, int err_nbr)
  551. {
  552. struct nand_chip *nand_chip = mtd->priv;
  553. struct atmel_nand_host *host = nand_chip->priv;
  554. int i = 0;
  555. int byte_pos, bit_pos, sector_size, pos;
  556. uint32_t tmp;
  557. uint8_t err_byte;
  558. sector_size = host->pmecc_sector_size;
  559. while (err_nbr) {
  560. tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_base, i) - 1;
  561. byte_pos = tmp / 8;
  562. bit_pos = tmp % 8;
  563. if (byte_pos >= (sector_size + extra_bytes))
  564. BUG(); /* should never happen */
  565. if (byte_pos < sector_size) {
  566. err_byte = *(buf + byte_pos);
  567. *(buf + byte_pos) ^= (1 << bit_pos);
  568. pos = sector_num * host->pmecc_sector_size + byte_pos;
  569. dev_info(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
  570. pos, bit_pos, err_byte, *(buf + byte_pos));
  571. } else {
  572. /* Bit flip in OOB area */
  573. tmp = sector_num * host->pmecc_bytes_per_sector
  574. + (byte_pos - sector_size);
  575. err_byte = ecc[tmp];
  576. ecc[tmp] ^= (1 << bit_pos);
  577. pos = tmp + nand_chip->ecc.layout->eccpos[0];
  578. dev_info(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
  579. pos, bit_pos, err_byte, ecc[tmp]);
  580. }
  581. i++;
  582. err_nbr--;
  583. }
  584. return;
  585. }
  586. static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
  587. u8 *ecc)
  588. {
  589. struct nand_chip *nand_chip = mtd->priv;
  590. struct atmel_nand_host *host = nand_chip->priv;
  591. int i, err_nbr, eccbytes;
  592. uint8_t *buf_pos;
  593. int total_err = 0;
  594. eccbytes = nand_chip->ecc.bytes;
  595. for (i = 0; i < eccbytes; i++)
  596. if (ecc[i] != 0xff)
  597. goto normal_check;
  598. /* Erased page, return OK */
  599. return 0;
  600. normal_check:
  601. for (i = 0; i < host->pmecc_sector_number; i++) {
  602. err_nbr = 0;
  603. if (pmecc_stat & 0x1) {
  604. buf_pos = buf + i * host->pmecc_sector_size;
  605. pmecc_gen_syndrome(mtd, i);
  606. pmecc_substitute(mtd);
  607. pmecc_get_sigma(mtd);
  608. err_nbr = pmecc_err_location(mtd);
  609. if (err_nbr == -1) {
  610. dev_err(host->dev, "PMECC: Too many errors\n");
  611. mtd->ecc_stats.failed++;
  612. return -EIO;
  613. } else {
  614. pmecc_correct_data(mtd, buf_pos, ecc, i,
  615. host->pmecc_bytes_per_sector, err_nbr);
  616. mtd->ecc_stats.corrected += err_nbr;
  617. total_err += err_nbr;
  618. }
  619. }
  620. pmecc_stat >>= 1;
  621. }
  622. return total_err;
  623. }
  624. static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
  625. struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
  626. {
  627. struct atmel_nand_host *host = chip->priv;
  628. int eccsize = chip->ecc.size;
  629. uint8_t *oob = chip->oob_poi;
  630. uint32_t *eccpos = chip->ecc.layout->eccpos;
  631. uint32_t stat;
  632. unsigned long end_time;
  633. int bitflips = 0;
  634. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
  635. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  636. pmecc_writel(host->ecc, CFG, (pmecc_readl_relaxed(host->ecc, CFG)
  637. & ~PMECC_CFG_WRITE_OP) | PMECC_CFG_AUTO_ENABLE);
  638. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
  639. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
  640. chip->read_buf(mtd, buf, eccsize);
  641. chip->read_buf(mtd, oob, mtd->oobsize);
  642. end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
  643. while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
  644. if (unlikely(time_after(jiffies, end_time))) {
  645. dev_err(host->dev, "PMECC: Timeout to get error status.\n");
  646. return -EIO;
  647. }
  648. cpu_relax();
  649. }
  650. stat = pmecc_readl_relaxed(host->ecc, ISR);
  651. if (stat != 0) {
  652. bitflips = pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]);
  653. if (bitflips < 0)
  654. /* uncorrectable errors */
  655. return 0;
  656. }
  657. return bitflips;
  658. }
  659. static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
  660. struct nand_chip *chip, const uint8_t *buf, int oob_required)
  661. {
  662. struct atmel_nand_host *host = chip->priv;
  663. uint32_t *eccpos = chip->ecc.layout->eccpos;
  664. int i, j;
  665. unsigned long end_time;
  666. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
  667. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  668. pmecc_writel(host->ecc, CFG, (pmecc_readl_relaxed(host->ecc, CFG) |
  669. PMECC_CFG_WRITE_OP) & ~PMECC_CFG_AUTO_ENABLE);
  670. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
  671. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA);
  672. chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
  673. end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS);
  674. while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) {
  675. if (unlikely(time_after(jiffies, end_time))) {
  676. dev_err(host->dev, "PMECC: Timeout to get ECC value.\n");
  677. return -EIO;
  678. }
  679. cpu_relax();
  680. }
  681. for (i = 0; i < host->pmecc_sector_number; i++) {
  682. for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
  683. int pos;
  684. pos = i * host->pmecc_bytes_per_sector + j;
  685. chip->oob_poi[eccpos[pos]] =
  686. pmecc_readb_ecc_relaxed(host->ecc, i, j);
  687. }
  688. }
  689. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  690. return 0;
  691. }
  692. static void atmel_pmecc_core_init(struct mtd_info *mtd)
  693. {
  694. struct nand_chip *nand_chip = mtd->priv;
  695. struct atmel_nand_host *host = nand_chip->priv;
  696. uint32_t val = 0;
  697. struct nand_ecclayout *ecc_layout;
  698. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST);
  699. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  700. switch (host->pmecc_corr_cap) {
  701. case 2:
  702. val = PMECC_CFG_BCH_ERR2;
  703. break;
  704. case 4:
  705. val = PMECC_CFG_BCH_ERR4;
  706. break;
  707. case 8:
  708. val = PMECC_CFG_BCH_ERR8;
  709. break;
  710. case 12:
  711. val = PMECC_CFG_BCH_ERR12;
  712. break;
  713. case 24:
  714. val = PMECC_CFG_BCH_ERR24;
  715. break;
  716. }
  717. if (host->pmecc_sector_size == 512)
  718. val |= PMECC_CFG_SECTOR512;
  719. else if (host->pmecc_sector_size == 1024)
  720. val |= PMECC_CFG_SECTOR1024;
  721. switch (host->pmecc_sector_number) {
  722. case 1:
  723. val |= PMECC_CFG_PAGE_1SECTOR;
  724. break;
  725. case 2:
  726. val |= PMECC_CFG_PAGE_2SECTORS;
  727. break;
  728. case 4:
  729. val |= PMECC_CFG_PAGE_4SECTORS;
  730. break;
  731. case 8:
  732. val |= PMECC_CFG_PAGE_8SECTORS;
  733. break;
  734. }
  735. val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
  736. | PMECC_CFG_AUTO_DISABLE);
  737. pmecc_writel(host->ecc, CFG, val);
  738. ecc_layout = nand_chip->ecc.layout;
  739. pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1);
  740. pmecc_writel(host->ecc, SADDR, ecc_layout->eccpos[0]);
  741. pmecc_writel(host->ecc, EADDR,
  742. ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
  743. /* See datasheet about PMECC Clock Control Register */
  744. pmecc_writel(host->ecc, CLK, 2);
  745. pmecc_writel(host->ecc, IDR, 0xff);
  746. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE);
  747. }
  748. /*
  749. * Get ECC requirement in ONFI parameters, returns -1 if ONFI
  750. * parameters is not supported.
  751. * return 0 if success to get the ECC requirement.
  752. */
  753. static int get_onfi_ecc_param(struct nand_chip *chip,
  754. int *ecc_bits, int *sector_size)
  755. {
  756. *ecc_bits = *sector_size = 0;
  757. if (chip->onfi_params.ecc_bits == 0xff)
  758. /* TODO: the sector_size and ecc_bits need to be find in
  759. * extended ecc parameter, currently we don't support it.
  760. */
  761. return -1;
  762. *ecc_bits = chip->onfi_params.ecc_bits;
  763. /* The default sector size (ecc codeword size) is 512 */
  764. *sector_size = 512;
  765. return 0;
  766. }
  767. /*
  768. * Get ecc requirement from ONFI parameters ecc requirement.
  769. * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function
  770. * will set them according to ONFI ecc requirement. Otherwise, use the
  771. * value in DTS file.
  772. * return 0 if success. otherwise return error code.
  773. */
  774. static int pmecc_choose_ecc(struct atmel_nand_host *host,
  775. int *cap, int *sector_size)
  776. {
  777. /* Get ECC requirement from ONFI parameters */
  778. *cap = *sector_size = 0;
  779. if (host->nand_chip.onfi_version) {
  780. if (!get_onfi_ecc_param(&host->nand_chip, cap, sector_size))
  781. dev_info(host->dev, "ONFI params, minimum required ECC: %d bits in %d bytes\n",
  782. *cap, *sector_size);
  783. else
  784. dev_info(host->dev, "NAND chip ECC reqirement is in Extended ONFI parameter, we don't support yet.\n");
  785. } else {
  786. dev_info(host->dev, "NAND chip is not ONFI compliant, assume ecc_bits is 2 in 512 bytes");
  787. }
  788. if (*cap == 0 && *sector_size == 0) {
  789. *cap = 2;
  790. *sector_size = 512;
  791. }
  792. /* If dts file doesn't specify then use the one in ONFI parameters */
  793. if (host->pmecc_corr_cap == 0) {
  794. /* use the most fitable ecc bits (the near bigger one ) */
  795. if (*cap <= 2)
  796. host->pmecc_corr_cap = 2;
  797. else if (*cap <= 4)
  798. host->pmecc_corr_cap = 4;
  799. else if (*cap <= 8)
  800. host->pmecc_corr_cap = 8;
  801. else if (*cap <= 12)
  802. host->pmecc_corr_cap = 12;
  803. else if (*cap <= 24)
  804. host->pmecc_corr_cap = 24;
  805. else
  806. return -EINVAL;
  807. }
  808. if (host->pmecc_sector_size == 0) {
  809. /* use the most fitable sector size (the near smaller one ) */
  810. if (*sector_size >= 1024)
  811. host->pmecc_sector_size = 1024;
  812. else if (*sector_size >= 512)
  813. host->pmecc_sector_size = 512;
  814. else
  815. return -EINVAL;
  816. }
  817. return 0;
  818. }
  819. static int __init atmel_pmecc_nand_init_params(struct platform_device *pdev,
  820. struct atmel_nand_host *host)
  821. {
  822. struct mtd_info *mtd = &host->mtd;
  823. struct nand_chip *nand_chip = &host->nand_chip;
  824. struct resource *regs, *regs_pmerr, *regs_rom;
  825. int cap, sector_size, err_no;
  826. err_no = pmecc_choose_ecc(host, &cap, &sector_size);
  827. if (err_no) {
  828. dev_err(host->dev, "The NAND flash's ECC requirement are not support!");
  829. return err_no;
  830. }
  831. if (cap > host->pmecc_corr_cap ||
  832. sector_size != host->pmecc_sector_size)
  833. dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n");
  834. cap = host->pmecc_corr_cap;
  835. sector_size = host->pmecc_sector_size;
  836. host->pmecc_lookup_table_offset = (sector_size == 512) ?
  837. host->pmecc_lookup_table_offset_512 :
  838. host->pmecc_lookup_table_offset_1024;
  839. dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n",
  840. cap, sector_size);
  841. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  842. if (!regs) {
  843. dev_warn(host->dev,
  844. "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
  845. nand_chip->ecc.mode = NAND_ECC_SOFT;
  846. return 0;
  847. }
  848. host->ecc = devm_ioremap_resource(&pdev->dev, regs);
  849. if (IS_ERR(host->ecc)) {
  850. dev_err(host->dev, "ioremap failed\n");
  851. err_no = PTR_ERR(host->ecc);
  852. goto err;
  853. }
  854. regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  855. host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr);
  856. if (IS_ERR(host->pmerrloc_base)) {
  857. dev_err(host->dev,
  858. "Can not get I/O resource for PMECC ERRLOC controller!\n");
  859. err_no = PTR_ERR(host->pmerrloc_base);
  860. goto err;
  861. }
  862. regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3);
  863. host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev, regs_rom);
  864. if (IS_ERR(host->pmecc_rom_base)) {
  865. dev_err(host->dev, "Can not get I/O resource for ROM!\n");
  866. err_no = PTR_ERR(host->pmecc_rom_base);
  867. goto err;
  868. }
  869. /* ECC is calculated for the whole page (1 step) */
  870. nand_chip->ecc.size = mtd->writesize;
  871. /* set ECC page size and oob layout */
  872. switch (mtd->writesize) {
  873. case 2048:
  874. host->pmecc_degree = PMECC_GF_DIMENSION_13;
  875. host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
  876. host->pmecc_sector_number = mtd->writesize / sector_size;
  877. host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
  878. cap, sector_size);
  879. host->pmecc_alpha_to = pmecc_get_alpha_to(host);
  880. host->pmecc_index_of = host->pmecc_rom_base +
  881. host->pmecc_lookup_table_offset;
  882. nand_chip->ecc.steps = 1;
  883. nand_chip->ecc.strength = cap;
  884. nand_chip->ecc.bytes = host->pmecc_bytes_per_sector *
  885. host->pmecc_sector_number;
  886. if (nand_chip->ecc.bytes > mtd->oobsize - 2) {
  887. dev_err(host->dev, "No room for ECC bytes\n");
  888. err_no = -EINVAL;
  889. goto err;
  890. }
  891. pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
  892. mtd->oobsize,
  893. nand_chip->ecc.bytes);
  894. nand_chip->ecc.layout = &atmel_pmecc_oobinfo;
  895. break;
  896. case 512:
  897. case 1024:
  898. case 4096:
  899. /* TODO */
  900. dev_warn(host->dev,
  901. "Unsupported page size for PMECC, use Software ECC\n");
  902. default:
  903. /* page size not handled by HW ECC */
  904. /* switching back to soft ECC */
  905. nand_chip->ecc.mode = NAND_ECC_SOFT;
  906. return 0;
  907. }
  908. /* Allocate data for PMECC computation */
  909. err_no = pmecc_data_alloc(host);
  910. if (err_no) {
  911. dev_err(host->dev,
  912. "Cannot allocate memory for PMECC computation!\n");
  913. goto err;
  914. }
  915. nand_chip->ecc.read_page = atmel_nand_pmecc_read_page;
  916. nand_chip->ecc.write_page = atmel_nand_pmecc_write_page;
  917. atmel_pmecc_core_init(mtd);
  918. return 0;
  919. err:
  920. return err_no;
  921. }
  922. /*
  923. * Calculate HW ECC
  924. *
  925. * function called after a write
  926. *
  927. * mtd: MTD block structure
  928. * dat: raw data (unused)
  929. * ecc_code: buffer for ECC
  930. */
  931. static int atmel_nand_calculate(struct mtd_info *mtd,
  932. const u_char *dat, unsigned char *ecc_code)
  933. {
  934. struct nand_chip *nand_chip = mtd->priv;
  935. struct atmel_nand_host *host = nand_chip->priv;
  936. unsigned int ecc_value;
  937. /* get the first 2 ECC bytes */
  938. ecc_value = ecc_readl(host->ecc, PR);
  939. ecc_code[0] = ecc_value & 0xFF;
  940. ecc_code[1] = (ecc_value >> 8) & 0xFF;
  941. /* get the last 2 ECC bytes */
  942. ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY;
  943. ecc_code[2] = ecc_value & 0xFF;
  944. ecc_code[3] = (ecc_value >> 8) & 0xFF;
  945. return 0;
  946. }
  947. /*
  948. * HW ECC read page function
  949. *
  950. * mtd: mtd info structure
  951. * chip: nand chip info structure
  952. * buf: buffer to store read data
  953. * oob_required: caller expects OOB data read to chip->oob_poi
  954. */
  955. static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  956. uint8_t *buf, int oob_required, int page)
  957. {
  958. int eccsize = chip->ecc.size;
  959. int eccbytes = chip->ecc.bytes;
  960. uint32_t *eccpos = chip->ecc.layout->eccpos;
  961. uint8_t *p = buf;
  962. uint8_t *oob = chip->oob_poi;
  963. uint8_t *ecc_pos;
  964. int stat;
  965. unsigned int max_bitflips = 0;
  966. /*
  967. * Errata: ALE is incorrectly wired up to the ECC controller
  968. * on the AP7000, so it will include the address cycles in the
  969. * ECC calculation.
  970. *
  971. * Workaround: Reset the parity registers before reading the
  972. * actual data.
  973. */
  974. struct atmel_nand_host *host = chip->priv;
  975. if (host->board.need_reset_workaround)
  976. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  977. /* read the page */
  978. chip->read_buf(mtd, p, eccsize);
  979. /* move to ECC position if needed */
  980. if (eccpos[0] != 0) {
  981. /* This only works on large pages
  982. * because the ECC controller waits for
  983. * NAND_CMD_RNDOUTSTART after the
  984. * NAND_CMD_RNDOUT.
  985. * anyway, for small pages, the eccpos[0] == 0
  986. */
  987. chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
  988. mtd->writesize + eccpos[0], -1);
  989. }
  990. /* the ECC controller needs to read the ECC just after the data */
  991. ecc_pos = oob + eccpos[0];
  992. chip->read_buf(mtd, ecc_pos, eccbytes);
  993. /* check if there's an error */
  994. stat = chip->ecc.correct(mtd, p, oob, NULL);
  995. if (stat < 0) {
  996. mtd->ecc_stats.failed++;
  997. } else {
  998. mtd->ecc_stats.corrected += stat;
  999. max_bitflips = max_t(unsigned int, max_bitflips, stat);
  1000. }
  1001. /* get back to oob start (end of page) */
  1002. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
  1003. /* read the oob */
  1004. chip->read_buf(mtd, oob, mtd->oobsize);
  1005. return max_bitflips;
  1006. }
  1007. /*
  1008. * HW ECC Correction
  1009. *
  1010. * function called after a read
  1011. *
  1012. * mtd: MTD block structure
  1013. * dat: raw data read from the chip
  1014. * read_ecc: ECC from the chip (unused)
  1015. * isnull: unused
  1016. *
  1017. * Detect and correct a 1 bit error for a page
  1018. */
  1019. static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
  1020. u_char *read_ecc, u_char *isnull)
  1021. {
  1022. struct nand_chip *nand_chip = mtd->priv;
  1023. struct atmel_nand_host *host = nand_chip->priv;
  1024. unsigned int ecc_status;
  1025. unsigned int ecc_word, ecc_bit;
  1026. /* get the status from the Status Register */
  1027. ecc_status = ecc_readl(host->ecc, SR);
  1028. /* if there's no error */
  1029. if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
  1030. return 0;
  1031. /* get error bit offset (4 bits) */
  1032. ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR;
  1033. /* get word address (12 bits) */
  1034. ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR;
  1035. ecc_word >>= 4;
  1036. /* if there are multiple errors */
  1037. if (ecc_status & ATMEL_ECC_MULERR) {
  1038. /* check if it is a freshly erased block
  1039. * (filled with 0xff) */
  1040. if ((ecc_bit == ATMEL_ECC_BITADDR)
  1041. && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
  1042. /* the block has just been erased, return OK */
  1043. return 0;
  1044. }
  1045. /* it doesn't seems to be a freshly
  1046. * erased block.
  1047. * We can't correct so many errors */
  1048. dev_dbg(host->dev, "atmel_nand : multiple errors detected."
  1049. " Unable to correct.\n");
  1050. return -EIO;
  1051. }
  1052. /* if there's a single bit error : we can correct it */
  1053. if (ecc_status & ATMEL_ECC_ECCERR) {
  1054. /* there's nothing much to do here.
  1055. * the bit error is on the ECC itself.
  1056. */
  1057. dev_dbg(host->dev, "atmel_nand : one bit error on ECC code."
  1058. " Nothing to correct\n");
  1059. return 0;
  1060. }
  1061. dev_dbg(host->dev, "atmel_nand : one bit error on data."
  1062. " (word offset in the page :"
  1063. " 0x%x bit offset : 0x%x)\n",
  1064. ecc_word, ecc_bit);
  1065. /* correct the error */
  1066. if (nand_chip->options & NAND_BUSWIDTH_16) {
  1067. /* 16 bits words */
  1068. ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
  1069. } else {
  1070. /* 8 bits words */
  1071. dat[ecc_word] ^= (1 << ecc_bit);
  1072. }
  1073. dev_dbg(host->dev, "atmel_nand : error corrected\n");
  1074. return 1;
  1075. }
  1076. /*
  1077. * Enable HW ECC : unused on most chips
  1078. */
  1079. static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
  1080. {
  1081. struct nand_chip *nand_chip = mtd->priv;
  1082. struct atmel_nand_host *host = nand_chip->priv;
  1083. if (host->board.need_reset_workaround)
  1084. ecc_writel(host->ecc, CR, ATMEL_ECC_RST);
  1085. }
  1086. #if defined(CONFIG_OF)
  1087. static int atmel_of_init_port(struct atmel_nand_host *host,
  1088. struct device_node *np)
  1089. {
  1090. u32 val;
  1091. u32 offset[2];
  1092. int ecc_mode;
  1093. struct atmel_nand_data *board = &host->board;
  1094. enum of_gpio_flags flags;
  1095. if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) {
  1096. if (val >= 32) {
  1097. dev_err(host->dev, "invalid addr-offset %u\n", val);
  1098. return -EINVAL;
  1099. }
  1100. board->ale = val;
  1101. }
  1102. if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) {
  1103. if (val >= 32) {
  1104. dev_err(host->dev, "invalid cmd-offset %u\n", val);
  1105. return -EINVAL;
  1106. }
  1107. board->cle = val;
  1108. }
  1109. ecc_mode = of_get_nand_ecc_mode(np);
  1110. board->ecc_mode = ecc_mode < 0 ? NAND_ECC_SOFT : ecc_mode;
  1111. board->on_flash_bbt = of_get_nand_on_flash_bbt(np);
  1112. board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma");
  1113. if (of_get_nand_bus_width(np) == 16)
  1114. board->bus_width_16 = 1;
  1115. board->rdy_pin = of_get_gpio_flags(np, 0, &flags);
  1116. board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW);
  1117. board->enable_pin = of_get_gpio(np, 1);
  1118. board->det_pin = of_get_gpio(np, 2);
  1119. host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc");
  1120. if (!(board->ecc_mode == NAND_ECC_HW) || !host->has_pmecc)
  1121. return 0; /* Not using PMECC */
  1122. /* use PMECC, get correction capability, sector size and lookup
  1123. * table offset.
  1124. * If correction bits and sector size are not specified, then find
  1125. * them from NAND ONFI parameters.
  1126. */
  1127. if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) {
  1128. if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
  1129. (val != 24)) {
  1130. dev_err(host->dev,
  1131. "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
  1132. val);
  1133. return -EINVAL;
  1134. }
  1135. host->pmecc_corr_cap = (u8)val;
  1136. }
  1137. if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) {
  1138. if ((val != 512) && (val != 1024)) {
  1139. dev_err(host->dev,
  1140. "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
  1141. val);
  1142. return -EINVAL;
  1143. }
  1144. host->pmecc_sector_size = (u16)val;
  1145. }
  1146. if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset",
  1147. offset, 2) != 0) {
  1148. dev_err(host->dev, "Cannot get PMECC lookup table offset\n");
  1149. return -EINVAL;
  1150. }
  1151. if (!offset[0] && !offset[1]) {
  1152. dev_err(host->dev, "Invalid PMECC lookup table offset\n");
  1153. return -EINVAL;
  1154. }
  1155. host->pmecc_lookup_table_offset_512 = offset[0];
  1156. host->pmecc_lookup_table_offset_1024 = offset[1];
  1157. return 0;
  1158. }
  1159. #else
  1160. static int atmel_of_init_port(struct atmel_nand_host *host,
  1161. struct device_node *np)
  1162. {
  1163. return -EINVAL;
  1164. }
  1165. #endif
  1166. static int __init atmel_hw_nand_init_params(struct platform_device *pdev,
  1167. struct atmel_nand_host *host)
  1168. {
  1169. struct mtd_info *mtd = &host->mtd;
  1170. struct nand_chip *nand_chip = &host->nand_chip;
  1171. struct resource *regs;
  1172. regs = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1173. if (!regs) {
  1174. dev_err(host->dev,
  1175. "Can't get I/O resource regs, use software ECC\n");
  1176. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1177. return 0;
  1178. }
  1179. host->ecc = devm_ioremap_resource(&pdev->dev, regs);
  1180. if (IS_ERR(host->ecc)) {
  1181. dev_err(host->dev, "ioremap failed\n");
  1182. return PTR_ERR(host->ecc);
  1183. }
  1184. /* ECC is calculated for the whole page (1 step) */
  1185. nand_chip->ecc.size = mtd->writesize;
  1186. /* set ECC page size and oob layout */
  1187. switch (mtd->writesize) {
  1188. case 512:
  1189. nand_chip->ecc.layout = &atmel_oobinfo_small;
  1190. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528);
  1191. break;
  1192. case 1024:
  1193. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1194. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056);
  1195. break;
  1196. case 2048:
  1197. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1198. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112);
  1199. break;
  1200. case 4096:
  1201. nand_chip->ecc.layout = &atmel_oobinfo_large;
  1202. ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224);
  1203. break;
  1204. default:
  1205. /* page size not handled by HW ECC */
  1206. /* switching back to soft ECC */
  1207. nand_chip->ecc.mode = NAND_ECC_SOFT;
  1208. return 0;
  1209. }
  1210. /* set up for HW ECC */
  1211. nand_chip->ecc.calculate = atmel_nand_calculate;
  1212. nand_chip->ecc.correct = atmel_nand_correct;
  1213. nand_chip->ecc.hwctl = atmel_nand_hwctl;
  1214. nand_chip->ecc.read_page = atmel_nand_read_page;
  1215. nand_chip->ecc.bytes = 4;
  1216. nand_chip->ecc.strength = 1;
  1217. return 0;
  1218. }
  1219. /*
  1220. * Probe for the NAND device.
  1221. */
  1222. static int __init atmel_nand_probe(struct platform_device *pdev)
  1223. {
  1224. struct atmel_nand_host *host;
  1225. struct mtd_info *mtd;
  1226. struct nand_chip *nand_chip;
  1227. struct resource *mem;
  1228. struct mtd_part_parser_data ppdata = {};
  1229. int res;
  1230. /* Allocate memory for the device structure (and zero it) */
  1231. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  1232. if (!host) {
  1233. printk(KERN_ERR "atmel_nand: failed to allocate device structure.\n");
  1234. return -ENOMEM;
  1235. }
  1236. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1237. host->io_base = devm_ioremap_resource(&pdev->dev, mem);
  1238. if (IS_ERR(host->io_base)) {
  1239. dev_err(&pdev->dev, "atmel_nand: ioremap resource failed\n");
  1240. res = PTR_ERR(host->io_base);
  1241. goto err_nand_ioremap;
  1242. }
  1243. host->io_phys = (dma_addr_t)mem->start;
  1244. mtd = &host->mtd;
  1245. nand_chip = &host->nand_chip;
  1246. host->dev = &pdev->dev;
  1247. if (pdev->dev.of_node) {
  1248. res = atmel_of_init_port(host, pdev->dev.of_node);
  1249. if (res)
  1250. goto err_nand_ioremap;
  1251. } else {
  1252. memcpy(&host->board, pdev->dev.platform_data,
  1253. sizeof(struct atmel_nand_data));
  1254. }
  1255. nand_chip->priv = host; /* link the private data structures */
  1256. mtd->priv = nand_chip;
  1257. mtd->owner = THIS_MODULE;
  1258. /* Set address of NAND IO lines */
  1259. nand_chip->IO_ADDR_R = host->io_base;
  1260. nand_chip->IO_ADDR_W = host->io_base;
  1261. nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl;
  1262. if (gpio_is_valid(host->board.rdy_pin)) {
  1263. res = devm_gpio_request(&pdev->dev,
  1264. host->board.rdy_pin, "nand_rdy");
  1265. if (res < 0) {
  1266. dev_err(&pdev->dev,
  1267. "can't request rdy gpio %d\n",
  1268. host->board.rdy_pin);
  1269. goto err_nand_ioremap;
  1270. }
  1271. res = gpio_direction_input(host->board.rdy_pin);
  1272. if (res < 0) {
  1273. dev_err(&pdev->dev,
  1274. "can't request input direction rdy gpio %d\n",
  1275. host->board.rdy_pin);
  1276. goto err_nand_ioremap;
  1277. }
  1278. nand_chip->dev_ready = atmel_nand_device_ready;
  1279. }
  1280. if (gpio_is_valid(host->board.enable_pin)) {
  1281. res = devm_gpio_request(&pdev->dev,
  1282. host->board.enable_pin, "nand_enable");
  1283. if (res < 0) {
  1284. dev_err(&pdev->dev,
  1285. "can't request enable gpio %d\n",
  1286. host->board.enable_pin);
  1287. goto err_nand_ioremap;
  1288. }
  1289. res = gpio_direction_output(host->board.enable_pin, 1);
  1290. if (res < 0) {
  1291. dev_err(&pdev->dev,
  1292. "can't request output direction enable gpio %d\n",
  1293. host->board.enable_pin);
  1294. goto err_nand_ioremap;
  1295. }
  1296. }
  1297. nand_chip->ecc.mode = host->board.ecc_mode;
  1298. nand_chip->chip_delay = 20; /* 20us command delay time */
  1299. if (host->board.bus_width_16) /* 16-bit bus width */
  1300. nand_chip->options |= NAND_BUSWIDTH_16;
  1301. nand_chip->read_buf = atmel_read_buf;
  1302. nand_chip->write_buf = atmel_write_buf;
  1303. platform_set_drvdata(pdev, host);
  1304. atmel_nand_enable(host);
  1305. if (gpio_is_valid(host->board.det_pin)) {
  1306. res = devm_gpio_request(&pdev->dev,
  1307. host->board.det_pin, "nand_det");
  1308. if (res < 0) {
  1309. dev_err(&pdev->dev,
  1310. "can't request det gpio %d\n",
  1311. host->board.det_pin);
  1312. goto err_no_card;
  1313. }
  1314. res = gpio_direction_input(host->board.det_pin);
  1315. if (res < 0) {
  1316. dev_err(&pdev->dev,
  1317. "can't request input direction det gpio %d\n",
  1318. host->board.det_pin);
  1319. goto err_no_card;
  1320. }
  1321. if (gpio_get_value(host->board.det_pin)) {
  1322. printk(KERN_INFO "No SmartMedia card inserted.\n");
  1323. res = -ENXIO;
  1324. goto err_no_card;
  1325. }
  1326. }
  1327. if (host->board.on_flash_bbt || on_flash_bbt) {
  1328. printk(KERN_INFO "atmel_nand: Use On Flash BBT\n");
  1329. nand_chip->bbt_options |= NAND_BBT_USE_FLASH;
  1330. }
  1331. if (!host->board.has_dma)
  1332. use_dma = 0;
  1333. if (use_dma) {
  1334. dma_cap_mask_t mask;
  1335. dma_cap_zero(mask);
  1336. dma_cap_set(DMA_MEMCPY, mask);
  1337. host->dma_chan = dma_request_channel(mask, NULL, NULL);
  1338. if (!host->dma_chan) {
  1339. dev_err(host->dev, "Failed to request DMA channel\n");
  1340. use_dma = 0;
  1341. }
  1342. }
  1343. if (use_dma)
  1344. dev_info(host->dev, "Using %s for DMA transfers.\n",
  1345. dma_chan_name(host->dma_chan));
  1346. else
  1347. dev_info(host->dev, "No DMA support for NAND access.\n");
  1348. /* first scan to find the device and get the page size */
  1349. if (nand_scan_ident(mtd, 1, NULL)) {
  1350. res = -ENXIO;
  1351. goto err_scan_ident;
  1352. }
  1353. if (nand_chip->ecc.mode == NAND_ECC_HW) {
  1354. if (host->has_pmecc)
  1355. res = atmel_pmecc_nand_init_params(pdev, host);
  1356. else
  1357. res = atmel_hw_nand_init_params(pdev, host);
  1358. if (res != 0)
  1359. goto err_hw_ecc;
  1360. }
  1361. /* second phase scan */
  1362. if (nand_scan_tail(mtd)) {
  1363. res = -ENXIO;
  1364. goto err_scan_tail;
  1365. }
  1366. mtd->name = "atmel_nand";
  1367. ppdata.of_node = pdev->dev.of_node;
  1368. res = mtd_device_parse_register(mtd, NULL, &ppdata,
  1369. host->board.parts, host->board.num_parts);
  1370. if (!res)
  1371. return res;
  1372. err_scan_tail:
  1373. if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW)
  1374. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  1375. err_hw_ecc:
  1376. err_scan_ident:
  1377. err_no_card:
  1378. atmel_nand_disable(host);
  1379. if (host->dma_chan)
  1380. dma_release_channel(host->dma_chan);
  1381. err_nand_ioremap:
  1382. return res;
  1383. }
  1384. /*
  1385. * Remove a NAND device.
  1386. */
  1387. static int __exit atmel_nand_remove(struct platform_device *pdev)
  1388. {
  1389. struct atmel_nand_host *host = platform_get_drvdata(pdev);
  1390. struct mtd_info *mtd = &host->mtd;
  1391. nand_release(mtd);
  1392. atmel_nand_disable(host);
  1393. if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) {
  1394. pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE);
  1395. pmerrloc_writel(host->pmerrloc_base, ELDIS,
  1396. PMERRLOC_DISABLE);
  1397. }
  1398. if (host->dma_chan)
  1399. dma_release_channel(host->dma_chan);
  1400. return 0;
  1401. }
  1402. #if defined(CONFIG_OF)
  1403. static const struct of_device_id atmel_nand_dt_ids[] = {
  1404. { .compatible = "atmel,at91rm9200-nand" },
  1405. { /* sentinel */ }
  1406. };
  1407. MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids);
  1408. #endif
  1409. static struct platform_driver atmel_nand_driver = {
  1410. .remove = __exit_p(atmel_nand_remove),
  1411. .driver = {
  1412. .name = "atmel_nand",
  1413. .owner = THIS_MODULE,
  1414. .of_match_table = of_match_ptr(atmel_nand_dt_ids),
  1415. },
  1416. };
  1417. module_platform_driver_probe(atmel_nand_driver, atmel_nand_probe);
  1418. MODULE_LICENSE("GPL");
  1419. MODULE_AUTHOR("Rick Bronson");
  1420. MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32");
  1421. MODULE_ALIAS("platform:atmel_nand");