atmel_nand.c 44 KB

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