atmel_nand.c 45 KB

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