omap2.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. /*
  2. * Copyright © 2004 Texas Instruments, Jian Zhang <jzhang@ti.com>
  3. * Copyright © 2004 Micron Technology Inc.
  4. * Copyright © 2004 David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/platform_device.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/delay.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/sched.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/nand.h>
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/omap-dma.h>
  22. #include <linux/io.h>
  23. #include <linux/slab.h>
  24. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  25. #include <linux/bch.h>
  26. #endif
  27. #include <plat/dma.h>
  28. #include <plat/gpmc.h>
  29. #include <plat/nand.h>
  30. #define DRIVER_NAME "omap2-nand"
  31. #define OMAP_NAND_TIMEOUT_MS 5000
  32. #define NAND_Ecc_P1e (1 << 0)
  33. #define NAND_Ecc_P2e (1 << 1)
  34. #define NAND_Ecc_P4e (1 << 2)
  35. #define NAND_Ecc_P8e (1 << 3)
  36. #define NAND_Ecc_P16e (1 << 4)
  37. #define NAND_Ecc_P32e (1 << 5)
  38. #define NAND_Ecc_P64e (1 << 6)
  39. #define NAND_Ecc_P128e (1 << 7)
  40. #define NAND_Ecc_P256e (1 << 8)
  41. #define NAND_Ecc_P512e (1 << 9)
  42. #define NAND_Ecc_P1024e (1 << 10)
  43. #define NAND_Ecc_P2048e (1 << 11)
  44. #define NAND_Ecc_P1o (1 << 16)
  45. #define NAND_Ecc_P2o (1 << 17)
  46. #define NAND_Ecc_P4o (1 << 18)
  47. #define NAND_Ecc_P8o (1 << 19)
  48. #define NAND_Ecc_P16o (1 << 20)
  49. #define NAND_Ecc_P32o (1 << 21)
  50. #define NAND_Ecc_P64o (1 << 22)
  51. #define NAND_Ecc_P128o (1 << 23)
  52. #define NAND_Ecc_P256o (1 << 24)
  53. #define NAND_Ecc_P512o (1 << 25)
  54. #define NAND_Ecc_P1024o (1 << 26)
  55. #define NAND_Ecc_P2048o (1 << 27)
  56. #define TF(value) (value ? 1 : 0)
  57. #define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
  58. #define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
  59. #define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
  60. #define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
  61. #define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
  62. #define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
  63. #define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
  64. #define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
  65. #define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
  66. #define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
  67. #define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
  68. #define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
  69. #define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
  70. #define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
  71. #define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
  72. #define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
  73. #define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
  74. #define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
  75. #define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
  76. #define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
  77. #define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
  78. #define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
  79. #define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
  80. #define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
  81. #define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
  82. #define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
  83. #define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
  84. #define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
  85. #define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
  86. #define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
  87. #define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
  88. #define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
  89. #define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
  90. #define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
  91. /* oob info generated runtime depending on ecc algorithm and layout selected */
  92. static struct nand_ecclayout omap_oobinfo;
  93. /* Define some generic bad / good block scan pattern which are used
  94. * while scanning a device for factory marked good / bad blocks
  95. */
  96. static uint8_t scan_ff_pattern[] = { 0xff };
  97. static struct nand_bbt_descr bb_descrip_flashbased = {
  98. .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
  99. .offs = 0,
  100. .len = 1,
  101. .pattern = scan_ff_pattern,
  102. };
  103. struct omap_nand_info {
  104. struct nand_hw_control controller;
  105. struct omap_nand_platform_data *pdata;
  106. struct mtd_info mtd;
  107. struct nand_chip nand;
  108. struct platform_device *pdev;
  109. int gpmc_cs;
  110. unsigned long phys_base;
  111. struct completion comp;
  112. struct dma_chan *dma;
  113. int gpmc_irq;
  114. enum {
  115. OMAP_NAND_IO_READ = 0, /* read */
  116. OMAP_NAND_IO_WRITE, /* write */
  117. } iomode;
  118. u_char *buf;
  119. int buf_len;
  120. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  121. struct bch_control *bch;
  122. struct nand_ecclayout ecclayout;
  123. #endif
  124. };
  125. /**
  126. * omap_hwcontrol - hardware specific access to control-lines
  127. * @mtd: MTD device structure
  128. * @cmd: command to device
  129. * @ctrl:
  130. * NAND_NCE: bit 0 -> don't care
  131. * NAND_CLE: bit 1 -> Command Latch
  132. * NAND_ALE: bit 2 -> Address Latch
  133. *
  134. * NOTE: boards may use different bits for these!!
  135. */
  136. static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  137. {
  138. struct omap_nand_info *info = container_of(mtd,
  139. struct omap_nand_info, mtd);
  140. if (cmd != NAND_CMD_NONE) {
  141. if (ctrl & NAND_CLE)
  142. gpmc_nand_write(info->gpmc_cs, GPMC_NAND_COMMAND, cmd);
  143. else if (ctrl & NAND_ALE)
  144. gpmc_nand_write(info->gpmc_cs, GPMC_NAND_ADDRESS, cmd);
  145. else /* NAND_NCE */
  146. gpmc_nand_write(info->gpmc_cs, GPMC_NAND_DATA, cmd);
  147. }
  148. }
  149. /**
  150. * omap_read_buf8 - read data from NAND controller into buffer
  151. * @mtd: MTD device structure
  152. * @buf: buffer to store date
  153. * @len: number of bytes to read
  154. */
  155. static void omap_read_buf8(struct mtd_info *mtd, u_char *buf, int len)
  156. {
  157. struct nand_chip *nand = mtd->priv;
  158. ioread8_rep(nand->IO_ADDR_R, buf, len);
  159. }
  160. /**
  161. * omap_write_buf8 - write buffer to NAND controller
  162. * @mtd: MTD device structure
  163. * @buf: data buffer
  164. * @len: number of bytes to write
  165. */
  166. static void omap_write_buf8(struct mtd_info *mtd, const u_char *buf, int len)
  167. {
  168. struct omap_nand_info *info = container_of(mtd,
  169. struct omap_nand_info, mtd);
  170. u_char *p = (u_char *)buf;
  171. u32 status = 0;
  172. while (len--) {
  173. iowrite8(*p++, info->nand.IO_ADDR_W);
  174. /* wait until buffer is available for write */
  175. do {
  176. status = gpmc_read_status(GPMC_STATUS_BUFFER);
  177. } while (!status);
  178. }
  179. }
  180. /**
  181. * omap_read_buf16 - read data from NAND controller into buffer
  182. * @mtd: MTD device structure
  183. * @buf: buffer to store date
  184. * @len: number of bytes to read
  185. */
  186. static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
  187. {
  188. struct nand_chip *nand = mtd->priv;
  189. ioread16_rep(nand->IO_ADDR_R, buf, len / 2);
  190. }
  191. /**
  192. * omap_write_buf16 - write buffer to NAND controller
  193. * @mtd: MTD device structure
  194. * @buf: data buffer
  195. * @len: number of bytes to write
  196. */
  197. static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len)
  198. {
  199. struct omap_nand_info *info = container_of(mtd,
  200. struct omap_nand_info, mtd);
  201. u16 *p = (u16 *) buf;
  202. u32 status = 0;
  203. /* FIXME try bursts of writesw() or DMA ... */
  204. len >>= 1;
  205. while (len--) {
  206. iowrite16(*p++, info->nand.IO_ADDR_W);
  207. /* wait until buffer is available for write */
  208. do {
  209. status = gpmc_read_status(GPMC_STATUS_BUFFER);
  210. } while (!status);
  211. }
  212. }
  213. /**
  214. * omap_read_buf_pref - read data from NAND controller into buffer
  215. * @mtd: MTD device structure
  216. * @buf: buffer to store date
  217. * @len: number of bytes to read
  218. */
  219. static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
  220. {
  221. struct omap_nand_info *info = container_of(mtd,
  222. struct omap_nand_info, mtd);
  223. uint32_t r_count = 0;
  224. int ret = 0;
  225. u32 *p = (u32 *)buf;
  226. /* take care of subpage reads */
  227. if (len % 4) {
  228. if (info->nand.options & NAND_BUSWIDTH_16)
  229. omap_read_buf16(mtd, buf, len % 4);
  230. else
  231. omap_read_buf8(mtd, buf, len % 4);
  232. p = (u32 *) (buf + len % 4);
  233. len -= len % 4;
  234. }
  235. /* configure and start prefetch transfer */
  236. ret = gpmc_prefetch_enable(info->gpmc_cs,
  237. PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x0);
  238. if (ret) {
  239. /* PFPW engine is busy, use cpu copy method */
  240. if (info->nand.options & NAND_BUSWIDTH_16)
  241. omap_read_buf16(mtd, (u_char *)p, len);
  242. else
  243. omap_read_buf8(mtd, (u_char *)p, len);
  244. } else {
  245. do {
  246. r_count = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
  247. r_count = r_count >> 2;
  248. ioread32_rep(info->nand.IO_ADDR_R, p, r_count);
  249. p += r_count;
  250. len -= r_count << 2;
  251. } while (len);
  252. /* disable and stop the PFPW engine */
  253. gpmc_prefetch_reset(info->gpmc_cs);
  254. }
  255. }
  256. /**
  257. * omap_write_buf_pref - write buffer to NAND controller
  258. * @mtd: MTD device structure
  259. * @buf: data buffer
  260. * @len: number of bytes to write
  261. */
  262. static void omap_write_buf_pref(struct mtd_info *mtd,
  263. const u_char *buf, int len)
  264. {
  265. struct omap_nand_info *info = container_of(mtd,
  266. struct omap_nand_info, mtd);
  267. uint32_t w_count = 0;
  268. int i = 0, ret = 0;
  269. u16 *p = (u16 *)buf;
  270. unsigned long tim, limit;
  271. /* take care of subpage writes */
  272. if (len % 2 != 0) {
  273. writeb(*buf, info->nand.IO_ADDR_W);
  274. p = (u16 *)(buf + 1);
  275. len--;
  276. }
  277. /* configure and start prefetch transfer */
  278. ret = gpmc_prefetch_enable(info->gpmc_cs,
  279. PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x1);
  280. if (ret) {
  281. /* PFPW engine is busy, use cpu copy method */
  282. if (info->nand.options & NAND_BUSWIDTH_16)
  283. omap_write_buf16(mtd, (u_char *)p, len);
  284. else
  285. omap_write_buf8(mtd, (u_char *)p, len);
  286. } else {
  287. while (len) {
  288. w_count = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
  289. w_count = w_count >> 1;
  290. for (i = 0; (i < w_count) && len; i++, len -= 2)
  291. iowrite16(*p++, info->nand.IO_ADDR_W);
  292. }
  293. /* wait for data to flushed-out before reset the prefetch */
  294. tim = 0;
  295. limit = (loops_per_jiffy *
  296. msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  297. while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit))
  298. cpu_relax();
  299. /* disable and stop the PFPW engine */
  300. gpmc_prefetch_reset(info->gpmc_cs);
  301. }
  302. }
  303. /*
  304. * omap_nand_dma_callback: callback on the completion of dma transfer
  305. * @data: pointer to completion data structure
  306. */
  307. static void omap_nand_dma_callback(void *data)
  308. {
  309. complete((struct completion *) data);
  310. }
  311. /*
  312. * omap_nand_dma_transfer: configer and start dma transfer
  313. * @mtd: MTD device structure
  314. * @addr: virtual address in RAM of source/destination
  315. * @len: number of data bytes to be transferred
  316. * @is_write: flag for read/write operation
  317. */
  318. static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
  319. unsigned int len, int is_write)
  320. {
  321. struct omap_nand_info *info = container_of(mtd,
  322. struct omap_nand_info, mtd);
  323. struct dma_async_tx_descriptor *tx;
  324. enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
  325. DMA_FROM_DEVICE;
  326. struct scatterlist sg;
  327. unsigned long tim, limit;
  328. unsigned n;
  329. int ret;
  330. if (addr >= high_memory) {
  331. struct page *p1;
  332. if (((size_t)addr & PAGE_MASK) !=
  333. ((size_t)(addr + len - 1) & PAGE_MASK))
  334. goto out_copy;
  335. p1 = vmalloc_to_page(addr);
  336. if (!p1)
  337. goto out_copy;
  338. addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK);
  339. }
  340. sg_init_one(&sg, addr, len);
  341. n = dma_map_sg(info->dma->device->dev, &sg, 1, dir);
  342. if (n == 0) {
  343. dev_err(&info->pdev->dev,
  344. "Couldn't DMA map a %d byte buffer\n", len);
  345. goto out_copy;
  346. }
  347. tx = dmaengine_prep_slave_sg(info->dma, &sg, n,
  348. is_write ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  349. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  350. if (!tx)
  351. goto out_copy_unmap;
  352. tx->callback = omap_nand_dma_callback;
  353. tx->callback_param = &info->comp;
  354. dmaengine_submit(tx);
  355. /* configure and start prefetch transfer */
  356. ret = gpmc_prefetch_enable(info->gpmc_cs,
  357. PREFETCH_FIFOTHRESHOLD_MAX, 0x1, len, is_write);
  358. if (ret)
  359. /* PFPW engine is busy, use cpu copy method */
  360. goto out_copy_unmap;
  361. init_completion(&info->comp);
  362. dma_async_issue_pending(info->dma);
  363. /* setup and start DMA using dma_addr */
  364. wait_for_completion(&info->comp);
  365. tim = 0;
  366. limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  367. while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit))
  368. cpu_relax();
  369. /* disable and stop the PFPW engine */
  370. gpmc_prefetch_reset(info->gpmc_cs);
  371. dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
  372. return 0;
  373. out_copy_unmap:
  374. dma_unmap_sg(info->dma->device->dev, &sg, 1, dir);
  375. out_copy:
  376. if (info->nand.options & NAND_BUSWIDTH_16)
  377. is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
  378. : omap_write_buf16(mtd, (u_char *) addr, len);
  379. else
  380. is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
  381. : omap_write_buf8(mtd, (u_char *) addr, len);
  382. return 0;
  383. }
  384. /**
  385. * omap_read_buf_dma_pref - read data from NAND controller into buffer
  386. * @mtd: MTD device structure
  387. * @buf: buffer to store date
  388. * @len: number of bytes to read
  389. */
  390. static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len)
  391. {
  392. if (len <= mtd->oobsize)
  393. omap_read_buf_pref(mtd, buf, len);
  394. else
  395. /* start transfer in DMA mode */
  396. omap_nand_dma_transfer(mtd, buf, len, 0x0);
  397. }
  398. /**
  399. * omap_write_buf_dma_pref - write buffer to NAND controller
  400. * @mtd: MTD device structure
  401. * @buf: data buffer
  402. * @len: number of bytes to write
  403. */
  404. static void omap_write_buf_dma_pref(struct mtd_info *mtd,
  405. const u_char *buf, int len)
  406. {
  407. if (len <= mtd->oobsize)
  408. omap_write_buf_pref(mtd, buf, len);
  409. else
  410. /* start transfer in DMA mode */
  411. omap_nand_dma_transfer(mtd, (u_char *) buf, len, 0x1);
  412. }
  413. /*
  414. * omap_nand_irq - GMPC irq handler
  415. * @this_irq: gpmc irq number
  416. * @dev: omap_nand_info structure pointer is passed here
  417. */
  418. static irqreturn_t omap_nand_irq(int this_irq, void *dev)
  419. {
  420. struct omap_nand_info *info = (struct omap_nand_info *) dev;
  421. u32 bytes;
  422. u32 irq_stat;
  423. irq_stat = gpmc_read_status(GPMC_GET_IRQ_STATUS);
  424. bytes = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
  425. bytes = bytes & 0xFFFC; /* io in multiple of 4 bytes */
  426. if (info->iomode == OMAP_NAND_IO_WRITE) { /* checks for write io */
  427. if (irq_stat & 0x2)
  428. goto done;
  429. if (info->buf_len && (info->buf_len < bytes))
  430. bytes = info->buf_len;
  431. else if (!info->buf_len)
  432. bytes = 0;
  433. iowrite32_rep(info->nand.IO_ADDR_W,
  434. (u32 *)info->buf, bytes >> 2);
  435. info->buf = info->buf + bytes;
  436. info->buf_len -= bytes;
  437. } else {
  438. ioread32_rep(info->nand.IO_ADDR_R,
  439. (u32 *)info->buf, bytes >> 2);
  440. info->buf = info->buf + bytes;
  441. if (irq_stat & 0x2)
  442. goto done;
  443. }
  444. gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, irq_stat);
  445. return IRQ_HANDLED;
  446. done:
  447. complete(&info->comp);
  448. /* disable irq */
  449. gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ, 0);
  450. /* clear status */
  451. gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, irq_stat);
  452. return IRQ_HANDLED;
  453. }
  454. /*
  455. * omap_read_buf_irq_pref - read data from NAND controller into buffer
  456. * @mtd: MTD device structure
  457. * @buf: buffer to store date
  458. * @len: number of bytes to read
  459. */
  460. static void omap_read_buf_irq_pref(struct mtd_info *mtd, u_char *buf, int len)
  461. {
  462. struct omap_nand_info *info = container_of(mtd,
  463. struct omap_nand_info, mtd);
  464. int ret = 0;
  465. if (len <= mtd->oobsize) {
  466. omap_read_buf_pref(mtd, buf, len);
  467. return;
  468. }
  469. info->iomode = OMAP_NAND_IO_READ;
  470. info->buf = buf;
  471. init_completion(&info->comp);
  472. /* configure and start prefetch transfer */
  473. ret = gpmc_prefetch_enable(info->gpmc_cs,
  474. PREFETCH_FIFOTHRESHOLD_MAX/2, 0x0, len, 0x0);
  475. if (ret)
  476. /* PFPW engine is busy, use cpu copy method */
  477. goto out_copy;
  478. info->buf_len = len;
  479. /* enable irq */
  480. gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ,
  481. (GPMC_IRQ_FIFOEVENTENABLE | GPMC_IRQ_COUNT_EVENT));
  482. /* waiting for read to complete */
  483. wait_for_completion(&info->comp);
  484. /* disable and stop the PFPW engine */
  485. gpmc_prefetch_reset(info->gpmc_cs);
  486. return;
  487. out_copy:
  488. if (info->nand.options & NAND_BUSWIDTH_16)
  489. omap_read_buf16(mtd, buf, len);
  490. else
  491. omap_read_buf8(mtd, buf, len);
  492. }
  493. /*
  494. * omap_write_buf_irq_pref - write buffer to NAND controller
  495. * @mtd: MTD device structure
  496. * @buf: data buffer
  497. * @len: number of bytes to write
  498. */
  499. static void omap_write_buf_irq_pref(struct mtd_info *mtd,
  500. const u_char *buf, int len)
  501. {
  502. struct omap_nand_info *info = container_of(mtd,
  503. struct omap_nand_info, mtd);
  504. int ret = 0;
  505. unsigned long tim, limit;
  506. if (len <= mtd->oobsize) {
  507. omap_write_buf_pref(mtd, buf, len);
  508. return;
  509. }
  510. info->iomode = OMAP_NAND_IO_WRITE;
  511. info->buf = (u_char *) buf;
  512. init_completion(&info->comp);
  513. /* configure and start prefetch transfer : size=24 */
  514. ret = gpmc_prefetch_enable(info->gpmc_cs,
  515. (PREFETCH_FIFOTHRESHOLD_MAX * 3) / 8, 0x0, len, 0x1);
  516. if (ret)
  517. /* PFPW engine is busy, use cpu copy method */
  518. goto out_copy;
  519. info->buf_len = len;
  520. /* enable irq */
  521. gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ,
  522. (GPMC_IRQ_FIFOEVENTENABLE | GPMC_IRQ_COUNT_EVENT));
  523. /* waiting for write to complete */
  524. wait_for_completion(&info->comp);
  525. /* wait for data to flushed-out before reset the prefetch */
  526. tim = 0;
  527. limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  528. while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit))
  529. cpu_relax();
  530. /* disable and stop the PFPW engine */
  531. gpmc_prefetch_reset(info->gpmc_cs);
  532. return;
  533. out_copy:
  534. if (info->nand.options & NAND_BUSWIDTH_16)
  535. omap_write_buf16(mtd, buf, len);
  536. else
  537. omap_write_buf8(mtd, buf, len);
  538. }
  539. /**
  540. * omap_verify_buf - Verify chip data against buffer
  541. * @mtd: MTD device structure
  542. * @buf: buffer containing the data to compare
  543. * @len: number of bytes to compare
  544. */
  545. static int omap_verify_buf(struct mtd_info *mtd, const u_char * buf, int len)
  546. {
  547. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  548. mtd);
  549. u16 *p = (u16 *) buf;
  550. len >>= 1;
  551. while (len--) {
  552. if (*p++ != cpu_to_le16(readw(info->nand.IO_ADDR_R)))
  553. return -EFAULT;
  554. }
  555. return 0;
  556. }
  557. /**
  558. * gen_true_ecc - This function will generate true ECC value
  559. * @ecc_buf: buffer to store ecc code
  560. *
  561. * This generated true ECC value can be used when correcting
  562. * data read from NAND flash memory core
  563. */
  564. static void gen_true_ecc(u8 *ecc_buf)
  565. {
  566. u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
  567. ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
  568. ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
  569. P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
  570. ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
  571. P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
  572. ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
  573. P1e(tmp) | P2048o(tmp) | P2048e(tmp));
  574. }
  575. /**
  576. * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
  577. * @ecc_data1: ecc code from nand spare area
  578. * @ecc_data2: ecc code from hardware register obtained from hardware ecc
  579. * @page_data: page data
  580. *
  581. * This function compares two ECC's and indicates if there is an error.
  582. * If the error can be corrected it will be corrected to the buffer.
  583. * If there is no error, %0 is returned. If there is an error but it
  584. * was corrected, %1 is returned. Otherwise, %-1 is returned.
  585. */
  586. static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
  587. u8 *ecc_data2, /* read from register */
  588. u8 *page_data)
  589. {
  590. uint i;
  591. u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
  592. u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
  593. u8 ecc_bit[24];
  594. u8 ecc_sum = 0;
  595. u8 find_bit = 0;
  596. uint find_byte = 0;
  597. int isEccFF;
  598. isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
  599. gen_true_ecc(ecc_data1);
  600. gen_true_ecc(ecc_data2);
  601. for (i = 0; i <= 2; i++) {
  602. *(ecc_data1 + i) = ~(*(ecc_data1 + i));
  603. *(ecc_data2 + i) = ~(*(ecc_data2 + i));
  604. }
  605. for (i = 0; i < 8; i++) {
  606. tmp0_bit[i] = *ecc_data1 % 2;
  607. *ecc_data1 = *ecc_data1 / 2;
  608. }
  609. for (i = 0; i < 8; i++) {
  610. tmp1_bit[i] = *(ecc_data1 + 1) % 2;
  611. *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
  612. }
  613. for (i = 0; i < 8; i++) {
  614. tmp2_bit[i] = *(ecc_data1 + 2) % 2;
  615. *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
  616. }
  617. for (i = 0; i < 8; i++) {
  618. comp0_bit[i] = *ecc_data2 % 2;
  619. *ecc_data2 = *ecc_data2 / 2;
  620. }
  621. for (i = 0; i < 8; i++) {
  622. comp1_bit[i] = *(ecc_data2 + 1) % 2;
  623. *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
  624. }
  625. for (i = 0; i < 8; i++) {
  626. comp2_bit[i] = *(ecc_data2 + 2) % 2;
  627. *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
  628. }
  629. for (i = 0; i < 6; i++)
  630. ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
  631. for (i = 0; i < 8; i++)
  632. ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
  633. for (i = 0; i < 8; i++)
  634. ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
  635. ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
  636. ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
  637. for (i = 0; i < 24; i++)
  638. ecc_sum += ecc_bit[i];
  639. switch (ecc_sum) {
  640. case 0:
  641. /* Not reached because this function is not called if
  642. * ECC values are equal
  643. */
  644. return 0;
  645. case 1:
  646. /* Uncorrectable error */
  647. pr_debug("ECC UNCORRECTED_ERROR 1\n");
  648. return -1;
  649. case 11:
  650. /* UN-Correctable error */
  651. pr_debug("ECC UNCORRECTED_ERROR B\n");
  652. return -1;
  653. case 12:
  654. /* Correctable error */
  655. find_byte = (ecc_bit[23] << 8) +
  656. (ecc_bit[21] << 7) +
  657. (ecc_bit[19] << 6) +
  658. (ecc_bit[17] << 5) +
  659. (ecc_bit[15] << 4) +
  660. (ecc_bit[13] << 3) +
  661. (ecc_bit[11] << 2) +
  662. (ecc_bit[9] << 1) +
  663. ecc_bit[7];
  664. find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
  665. pr_debug("Correcting single bit ECC error at offset: "
  666. "%d, bit: %d\n", find_byte, find_bit);
  667. page_data[find_byte] ^= (1 << find_bit);
  668. return 1;
  669. default:
  670. if (isEccFF) {
  671. if (ecc_data2[0] == 0 &&
  672. ecc_data2[1] == 0 &&
  673. ecc_data2[2] == 0)
  674. return 0;
  675. }
  676. pr_debug("UNCORRECTED_ERROR default\n");
  677. return -1;
  678. }
  679. }
  680. /**
  681. * omap_correct_data - Compares the ECC read with HW generated ECC
  682. * @mtd: MTD device structure
  683. * @dat: page data
  684. * @read_ecc: ecc read from nand flash
  685. * @calc_ecc: ecc read from HW ECC registers
  686. *
  687. * Compares the ecc read from nand spare area with ECC registers values
  688. * and if ECC's mismatched, it will call 'omap_compare_ecc' for error
  689. * detection and correction. If there are no errors, %0 is returned. If
  690. * there were errors and all of the errors were corrected, the number of
  691. * corrected errors is returned. If uncorrectable errors exist, %-1 is
  692. * returned.
  693. */
  694. static int omap_correct_data(struct mtd_info *mtd, u_char *dat,
  695. u_char *read_ecc, u_char *calc_ecc)
  696. {
  697. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  698. mtd);
  699. int blockCnt = 0, i = 0, ret = 0;
  700. int stat = 0;
  701. /* Ex NAND_ECC_HW12_2048 */
  702. if ((info->nand.ecc.mode == NAND_ECC_HW) &&
  703. (info->nand.ecc.size == 2048))
  704. blockCnt = 4;
  705. else
  706. blockCnt = 1;
  707. for (i = 0; i < blockCnt; i++) {
  708. if (memcmp(read_ecc, calc_ecc, 3) != 0) {
  709. ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
  710. if (ret < 0)
  711. return ret;
  712. /* keep track of the number of corrected errors */
  713. stat += ret;
  714. }
  715. read_ecc += 3;
  716. calc_ecc += 3;
  717. dat += 512;
  718. }
  719. return stat;
  720. }
  721. /**
  722. * omap_calcuate_ecc - Generate non-inverted ECC bytes.
  723. * @mtd: MTD device structure
  724. * @dat: The pointer to data on which ecc is computed
  725. * @ecc_code: The ecc_code buffer
  726. *
  727. * Using noninverted ECC can be considered ugly since writing a blank
  728. * page ie. padding will clear the ECC bytes. This is no problem as long
  729. * nobody is trying to write data on the seemingly unused page. Reading
  730. * an erased page will produce an ECC mismatch between generated and read
  731. * ECC bytes that has to be dealt with separately.
  732. */
  733. static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  734. u_char *ecc_code)
  735. {
  736. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  737. mtd);
  738. return gpmc_calculate_ecc(info->gpmc_cs, dat, ecc_code);
  739. }
  740. /**
  741. * omap_enable_hwecc - This function enables the hardware ecc functionality
  742. * @mtd: MTD device structure
  743. * @mode: Read/Write mode
  744. */
  745. static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
  746. {
  747. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  748. mtd);
  749. struct nand_chip *chip = mtd->priv;
  750. unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
  751. gpmc_enable_hwecc(info->gpmc_cs, mode, dev_width, info->nand.ecc.size);
  752. }
  753. /**
  754. * omap_wait - wait until the command is done
  755. * @mtd: MTD device structure
  756. * @chip: NAND Chip structure
  757. *
  758. * Wait function is called during Program and erase operations and
  759. * the way it is called from MTD layer, we should wait till the NAND
  760. * chip is ready after the programming/erase operation has completed.
  761. *
  762. * Erase can take up to 400ms and program up to 20ms according to
  763. * general NAND and SmartMedia specs
  764. */
  765. static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
  766. {
  767. struct nand_chip *this = mtd->priv;
  768. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  769. mtd);
  770. unsigned long timeo = jiffies;
  771. int status, state = this->state;
  772. if (state == FL_ERASING)
  773. timeo += (HZ * 400) / 1000;
  774. else
  775. timeo += (HZ * 20) / 1000;
  776. gpmc_nand_write(info->gpmc_cs,
  777. GPMC_NAND_COMMAND, (NAND_CMD_STATUS & 0xFF));
  778. while (time_before(jiffies, timeo)) {
  779. status = gpmc_nand_read(info->gpmc_cs, GPMC_NAND_DATA);
  780. if (status & NAND_STATUS_READY)
  781. break;
  782. cond_resched();
  783. }
  784. status = gpmc_nand_read(info->gpmc_cs, GPMC_NAND_DATA);
  785. return status;
  786. }
  787. /**
  788. * omap_dev_ready - calls the platform specific dev_ready function
  789. * @mtd: MTD device structure
  790. */
  791. static int omap_dev_ready(struct mtd_info *mtd)
  792. {
  793. unsigned int val = 0;
  794. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  795. mtd);
  796. val = gpmc_read_status(GPMC_GET_IRQ_STATUS);
  797. if ((val & 0x100) == 0x100) {
  798. /* Clear IRQ Interrupt */
  799. val |= 0x100;
  800. val &= ~(0x0);
  801. gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, val);
  802. } else {
  803. unsigned int cnt = 0;
  804. while (cnt++ < 0x1FF) {
  805. if ((val & 0x100) == 0x100)
  806. return 0;
  807. val = gpmc_read_status(GPMC_GET_IRQ_STATUS);
  808. }
  809. }
  810. return 1;
  811. }
  812. #ifdef CONFIG_MTD_NAND_OMAP_BCH
  813. /**
  814. * omap3_enable_hwecc_bch - Program OMAP3 GPMC to perform BCH ECC correction
  815. * @mtd: MTD device structure
  816. * @mode: Read/Write mode
  817. */
  818. static void omap3_enable_hwecc_bch(struct mtd_info *mtd, int mode)
  819. {
  820. int nerrors;
  821. unsigned int dev_width;
  822. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  823. mtd);
  824. struct nand_chip *chip = mtd->priv;
  825. nerrors = (info->nand.ecc.bytes == 13) ? 8 : 4;
  826. dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
  827. /*
  828. * Program GPMC to perform correction on one 512-byte sector at a time.
  829. * Using 4 sectors at a time (i.e. ecc.size = 2048) is also possible and
  830. * gives a slight (5%) performance gain (but requires additional code).
  831. */
  832. (void)gpmc_enable_hwecc_bch(info->gpmc_cs, mode, dev_width, 1, nerrors);
  833. }
  834. /**
  835. * omap3_calculate_ecc_bch4 - Generate 7 bytes of ECC bytes
  836. * @mtd: MTD device structure
  837. * @dat: The pointer to data on which ecc is computed
  838. * @ecc_code: The ecc_code buffer
  839. */
  840. static int omap3_calculate_ecc_bch4(struct mtd_info *mtd, const u_char *dat,
  841. u_char *ecc_code)
  842. {
  843. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  844. mtd);
  845. return gpmc_calculate_ecc_bch4(info->gpmc_cs, dat, ecc_code);
  846. }
  847. /**
  848. * omap3_calculate_ecc_bch8 - Generate 13 bytes of ECC bytes
  849. * @mtd: MTD device structure
  850. * @dat: The pointer to data on which ecc is computed
  851. * @ecc_code: The ecc_code buffer
  852. */
  853. static int omap3_calculate_ecc_bch8(struct mtd_info *mtd, const u_char *dat,
  854. u_char *ecc_code)
  855. {
  856. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  857. mtd);
  858. return gpmc_calculate_ecc_bch8(info->gpmc_cs, dat, ecc_code);
  859. }
  860. /**
  861. * omap3_correct_data_bch - Decode received data and correct errors
  862. * @mtd: MTD device structure
  863. * @data: page data
  864. * @read_ecc: ecc read from nand flash
  865. * @calc_ecc: ecc read from HW ECC registers
  866. */
  867. static int omap3_correct_data_bch(struct mtd_info *mtd, u_char *data,
  868. u_char *read_ecc, u_char *calc_ecc)
  869. {
  870. int i, count;
  871. /* cannot correct more than 8 errors */
  872. unsigned int errloc[8];
  873. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  874. mtd);
  875. count = decode_bch(info->bch, NULL, 512, read_ecc, calc_ecc, NULL,
  876. errloc);
  877. if (count > 0) {
  878. /* correct errors */
  879. for (i = 0; i < count; i++) {
  880. /* correct data only, not ecc bytes */
  881. if (errloc[i] < 8*512)
  882. data[errloc[i]/8] ^= 1 << (errloc[i] & 7);
  883. pr_debug("corrected bitflip %u\n", errloc[i]);
  884. }
  885. } else if (count < 0) {
  886. pr_err("ecc unrecoverable error\n");
  887. }
  888. return count;
  889. }
  890. /**
  891. * omap3_free_bch - Release BCH ecc resources
  892. * @mtd: MTD device structure
  893. */
  894. static void omap3_free_bch(struct mtd_info *mtd)
  895. {
  896. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  897. mtd);
  898. if (info->bch) {
  899. free_bch(info->bch);
  900. info->bch = NULL;
  901. }
  902. }
  903. /**
  904. * omap3_init_bch - Initialize BCH ECC
  905. * @mtd: MTD device structure
  906. * @ecc_opt: OMAP ECC mode (OMAP_ECC_BCH4_CODE_HW or OMAP_ECC_BCH8_CODE_HW)
  907. */
  908. static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt)
  909. {
  910. int ret, max_errors;
  911. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  912. mtd);
  913. #ifdef CONFIG_MTD_NAND_OMAP_BCH8
  914. const int hw_errors = 8;
  915. #else
  916. const int hw_errors = 4;
  917. #endif
  918. info->bch = NULL;
  919. max_errors = (ecc_opt == OMAP_ECC_BCH8_CODE_HW) ? 8 : 4;
  920. if (max_errors != hw_errors) {
  921. pr_err("cannot configure %d-bit BCH ecc, only %d-bit supported",
  922. max_errors, hw_errors);
  923. goto fail;
  924. }
  925. /* initialize GPMC BCH engine */
  926. ret = gpmc_init_hwecc_bch(info->gpmc_cs, 1, max_errors);
  927. if (ret)
  928. goto fail;
  929. /* software bch library is only used to detect and locate errors */
  930. info->bch = init_bch(13, max_errors, 0x201b /* hw polynomial */);
  931. if (!info->bch)
  932. goto fail;
  933. info->nand.ecc.size = 512;
  934. info->nand.ecc.hwctl = omap3_enable_hwecc_bch;
  935. info->nand.ecc.correct = omap3_correct_data_bch;
  936. info->nand.ecc.mode = NAND_ECC_HW;
  937. /*
  938. * The number of corrected errors in an ecc block that will trigger
  939. * block scrubbing defaults to the ecc strength (4 or 8).
  940. * Set mtd->bitflip_threshold here to define a custom threshold.
  941. */
  942. if (max_errors == 8) {
  943. info->nand.ecc.strength = 8;
  944. info->nand.ecc.bytes = 13;
  945. info->nand.ecc.calculate = omap3_calculate_ecc_bch8;
  946. } else {
  947. info->nand.ecc.strength = 4;
  948. info->nand.ecc.bytes = 7;
  949. info->nand.ecc.calculate = omap3_calculate_ecc_bch4;
  950. }
  951. pr_info("enabling NAND BCH ecc with %d-bit correction\n", max_errors);
  952. return 0;
  953. fail:
  954. omap3_free_bch(mtd);
  955. return -1;
  956. }
  957. /**
  958. * omap3_init_bch_tail - Build an oob layout for BCH ECC correction.
  959. * @mtd: MTD device structure
  960. */
  961. static int omap3_init_bch_tail(struct mtd_info *mtd)
  962. {
  963. int i, steps;
  964. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  965. mtd);
  966. struct nand_ecclayout *layout = &info->ecclayout;
  967. /* build oob layout */
  968. steps = mtd->writesize/info->nand.ecc.size;
  969. layout->eccbytes = steps*info->nand.ecc.bytes;
  970. /* do not bother creating special oob layouts for small page devices */
  971. if (mtd->oobsize < 64) {
  972. pr_err("BCH ecc is not supported on small page devices\n");
  973. goto fail;
  974. }
  975. /* reserve 2 bytes for bad block marker */
  976. if (layout->eccbytes+2 > mtd->oobsize) {
  977. pr_err("no oob layout available for oobsize %d eccbytes %u\n",
  978. mtd->oobsize, layout->eccbytes);
  979. goto fail;
  980. }
  981. /* put ecc bytes at oob tail */
  982. for (i = 0; i < layout->eccbytes; i++)
  983. layout->eccpos[i] = mtd->oobsize-layout->eccbytes+i;
  984. layout->oobfree[0].offset = 2;
  985. layout->oobfree[0].length = mtd->oobsize-2-layout->eccbytes;
  986. info->nand.ecc.layout = layout;
  987. if (!(info->nand.options & NAND_BUSWIDTH_16))
  988. info->nand.badblock_pattern = &bb_descrip_flashbased;
  989. return 0;
  990. fail:
  991. omap3_free_bch(mtd);
  992. return -1;
  993. }
  994. #else
  995. static int omap3_init_bch(struct mtd_info *mtd, int ecc_opt)
  996. {
  997. pr_err("CONFIG_MTD_NAND_OMAP_BCH is not enabled\n");
  998. return -1;
  999. }
  1000. static int omap3_init_bch_tail(struct mtd_info *mtd)
  1001. {
  1002. return -1;
  1003. }
  1004. static void omap3_free_bch(struct mtd_info *mtd)
  1005. {
  1006. }
  1007. #endif /* CONFIG_MTD_NAND_OMAP_BCH */
  1008. static int __devinit omap_nand_probe(struct platform_device *pdev)
  1009. {
  1010. struct omap_nand_info *info;
  1011. struct omap_nand_platform_data *pdata;
  1012. int err;
  1013. int i, offset;
  1014. dma_cap_mask_t mask;
  1015. unsigned sig;
  1016. pdata = pdev->dev.platform_data;
  1017. if (pdata == NULL) {
  1018. dev_err(&pdev->dev, "platform data missing\n");
  1019. return -ENODEV;
  1020. }
  1021. info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL);
  1022. if (!info)
  1023. return -ENOMEM;
  1024. platform_set_drvdata(pdev, info);
  1025. spin_lock_init(&info->controller.lock);
  1026. init_waitqueue_head(&info->controller.wq);
  1027. info->pdev = pdev;
  1028. info->gpmc_cs = pdata->cs;
  1029. info->phys_base = pdata->phys_base;
  1030. info->mtd.priv = &info->nand;
  1031. info->mtd.name = dev_name(&pdev->dev);
  1032. info->mtd.owner = THIS_MODULE;
  1033. info->nand.options = pdata->devsize;
  1034. info->nand.options |= NAND_SKIP_BBTSCAN;
  1035. /* NAND write protect off */
  1036. gpmc_cs_configure(info->gpmc_cs, GPMC_CONFIG_WP, 0);
  1037. if (!request_mem_region(info->phys_base, NAND_IO_SIZE,
  1038. pdev->dev.driver->name)) {
  1039. err = -EBUSY;
  1040. goto out_free_info;
  1041. }
  1042. info->nand.IO_ADDR_R = ioremap(info->phys_base, NAND_IO_SIZE);
  1043. if (!info->nand.IO_ADDR_R) {
  1044. err = -ENOMEM;
  1045. goto out_release_mem_region;
  1046. }
  1047. info->nand.controller = &info->controller;
  1048. info->nand.IO_ADDR_W = info->nand.IO_ADDR_R;
  1049. info->nand.cmd_ctrl = omap_hwcontrol;
  1050. /*
  1051. * If RDY/BSY line is connected to OMAP then use the omap ready
  1052. * funcrtion and the generic nand_wait function which reads the status
  1053. * register after monitoring the RDY/BSY line.Otherwise use a standard
  1054. * chip delay which is slightly more than tR (AC Timing) of the NAND
  1055. * device and read status register until you get a failure or success
  1056. */
  1057. if (pdata->dev_ready) {
  1058. info->nand.dev_ready = omap_dev_ready;
  1059. info->nand.chip_delay = 0;
  1060. } else {
  1061. info->nand.waitfunc = omap_wait;
  1062. info->nand.chip_delay = 50;
  1063. }
  1064. switch (pdata->xfer_type) {
  1065. case NAND_OMAP_PREFETCH_POLLED:
  1066. info->nand.read_buf = omap_read_buf_pref;
  1067. info->nand.write_buf = omap_write_buf_pref;
  1068. break;
  1069. case NAND_OMAP_POLLED:
  1070. if (info->nand.options & NAND_BUSWIDTH_16) {
  1071. info->nand.read_buf = omap_read_buf16;
  1072. info->nand.write_buf = omap_write_buf16;
  1073. } else {
  1074. info->nand.read_buf = omap_read_buf8;
  1075. info->nand.write_buf = omap_write_buf8;
  1076. }
  1077. break;
  1078. case NAND_OMAP_PREFETCH_DMA:
  1079. dma_cap_zero(mask);
  1080. dma_cap_set(DMA_SLAVE, mask);
  1081. sig = OMAP24XX_DMA_GPMC;
  1082. info->dma = dma_request_channel(mask, omap_dma_filter_fn, &sig);
  1083. if (!info->dma) {
  1084. dev_err(&pdev->dev, "DMA engine request failed\n");
  1085. err = -ENXIO;
  1086. goto out_release_mem_region;
  1087. } else {
  1088. struct dma_slave_config cfg;
  1089. memset(&cfg, 0, sizeof(cfg));
  1090. cfg.src_addr = info->phys_base;
  1091. cfg.dst_addr = info->phys_base;
  1092. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1093. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1094. cfg.src_maxburst = 16;
  1095. cfg.dst_maxburst = 16;
  1096. err = dmaengine_slave_config(info->dma, &cfg);
  1097. if (err) {
  1098. dev_err(&pdev->dev, "DMA engine slave config failed: %d\n",
  1099. err);
  1100. goto out_release_mem_region;
  1101. }
  1102. info->nand.read_buf = omap_read_buf_dma_pref;
  1103. info->nand.write_buf = omap_write_buf_dma_pref;
  1104. }
  1105. break;
  1106. case NAND_OMAP_PREFETCH_IRQ:
  1107. err = request_irq(pdata->gpmc_irq,
  1108. omap_nand_irq, IRQF_SHARED, "gpmc-nand", info);
  1109. if (err) {
  1110. dev_err(&pdev->dev, "requesting irq(%d) error:%d",
  1111. pdata->gpmc_irq, err);
  1112. goto out_release_mem_region;
  1113. } else {
  1114. info->gpmc_irq = pdata->gpmc_irq;
  1115. info->nand.read_buf = omap_read_buf_irq_pref;
  1116. info->nand.write_buf = omap_write_buf_irq_pref;
  1117. }
  1118. break;
  1119. default:
  1120. dev_err(&pdev->dev,
  1121. "xfer_type(%d) not supported!\n", pdata->xfer_type);
  1122. err = -EINVAL;
  1123. goto out_release_mem_region;
  1124. }
  1125. info->nand.verify_buf = omap_verify_buf;
  1126. /* selsect the ecc type */
  1127. if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_DEFAULT)
  1128. info->nand.ecc.mode = NAND_ECC_SOFT;
  1129. else if ((pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW) ||
  1130. (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE)) {
  1131. info->nand.ecc.bytes = 3;
  1132. info->nand.ecc.size = 512;
  1133. info->nand.ecc.strength = 1;
  1134. info->nand.ecc.calculate = omap_calculate_ecc;
  1135. info->nand.ecc.hwctl = omap_enable_hwecc;
  1136. info->nand.ecc.correct = omap_correct_data;
  1137. info->nand.ecc.mode = NAND_ECC_HW;
  1138. } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
  1139. (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) {
  1140. err = omap3_init_bch(&info->mtd, pdata->ecc_opt);
  1141. if (err) {
  1142. err = -EINVAL;
  1143. goto out_release_mem_region;
  1144. }
  1145. }
  1146. /* DIP switches on some boards change between 8 and 16 bit
  1147. * bus widths for flash. Try the other width if the first try fails.
  1148. */
  1149. if (nand_scan_ident(&info->mtd, 1, NULL)) {
  1150. info->nand.options ^= NAND_BUSWIDTH_16;
  1151. if (nand_scan_ident(&info->mtd, 1, NULL)) {
  1152. err = -ENXIO;
  1153. goto out_release_mem_region;
  1154. }
  1155. }
  1156. /* rom code layout */
  1157. if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE) {
  1158. if (info->nand.options & NAND_BUSWIDTH_16)
  1159. offset = 2;
  1160. else {
  1161. offset = 1;
  1162. info->nand.badblock_pattern = &bb_descrip_flashbased;
  1163. }
  1164. omap_oobinfo.eccbytes = 3 * (info->mtd.oobsize/16);
  1165. for (i = 0; i < omap_oobinfo.eccbytes; i++)
  1166. omap_oobinfo.eccpos[i] = i+offset;
  1167. omap_oobinfo.oobfree->offset = offset + omap_oobinfo.eccbytes;
  1168. omap_oobinfo.oobfree->length = info->mtd.oobsize -
  1169. (offset + omap_oobinfo.eccbytes);
  1170. info->nand.ecc.layout = &omap_oobinfo;
  1171. } else if ((pdata->ecc_opt == OMAP_ECC_BCH4_CODE_HW) ||
  1172. (pdata->ecc_opt == OMAP_ECC_BCH8_CODE_HW)) {
  1173. /* build OOB layout for BCH ECC correction */
  1174. err = omap3_init_bch_tail(&info->mtd);
  1175. if (err) {
  1176. err = -EINVAL;
  1177. goto out_release_mem_region;
  1178. }
  1179. }
  1180. /* second phase scan */
  1181. if (nand_scan_tail(&info->mtd)) {
  1182. err = -ENXIO;
  1183. goto out_release_mem_region;
  1184. }
  1185. mtd_device_parse_register(&info->mtd, NULL, NULL, pdata->parts,
  1186. pdata->nr_parts);
  1187. platform_set_drvdata(pdev, &info->mtd);
  1188. return 0;
  1189. out_release_mem_region:
  1190. if (info->dma)
  1191. dma_release_channel(info->dma);
  1192. release_mem_region(info->phys_base, NAND_IO_SIZE);
  1193. out_free_info:
  1194. kfree(info);
  1195. return err;
  1196. }
  1197. static int omap_nand_remove(struct platform_device *pdev)
  1198. {
  1199. struct mtd_info *mtd = platform_get_drvdata(pdev);
  1200. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  1201. mtd);
  1202. omap3_free_bch(&info->mtd);
  1203. platform_set_drvdata(pdev, NULL);
  1204. if (info->dma)
  1205. dma_release_channel(info->dma);
  1206. if (info->gpmc_irq)
  1207. free_irq(info->gpmc_irq, info);
  1208. /* Release NAND device, its internal structures and partitions */
  1209. nand_release(&info->mtd);
  1210. iounmap(info->nand.IO_ADDR_R);
  1211. kfree(&info->mtd);
  1212. return 0;
  1213. }
  1214. static struct platform_driver omap_nand_driver = {
  1215. .probe = omap_nand_probe,
  1216. .remove = omap_nand_remove,
  1217. .driver = {
  1218. .name = DRIVER_NAME,
  1219. .owner = THIS_MODULE,
  1220. },
  1221. };
  1222. module_platform_driver(omap_nand_driver);
  1223. MODULE_ALIAS("platform:" DRIVER_NAME);
  1224. MODULE_LICENSE("GPL");
  1225. MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");