omap2.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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/dma-mapping.h>
  12. #include <linux/delay.h>
  13. #include <linux/module.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/jiffies.h>
  16. #include <linux/sched.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/mtd/nand.h>
  19. #include <linux/mtd/partitions.h>
  20. #include <linux/io.h>
  21. #include <linux/slab.h>
  22. #include <plat/dma.h>
  23. #include <plat/gpmc.h>
  24. #include <plat/nand.h>
  25. #define DRIVER_NAME "omap2-nand"
  26. #define OMAP_NAND_TIMEOUT_MS 5000
  27. #define NAND_Ecc_P1e (1 << 0)
  28. #define NAND_Ecc_P2e (1 << 1)
  29. #define NAND_Ecc_P4e (1 << 2)
  30. #define NAND_Ecc_P8e (1 << 3)
  31. #define NAND_Ecc_P16e (1 << 4)
  32. #define NAND_Ecc_P32e (1 << 5)
  33. #define NAND_Ecc_P64e (1 << 6)
  34. #define NAND_Ecc_P128e (1 << 7)
  35. #define NAND_Ecc_P256e (1 << 8)
  36. #define NAND_Ecc_P512e (1 << 9)
  37. #define NAND_Ecc_P1024e (1 << 10)
  38. #define NAND_Ecc_P2048e (1 << 11)
  39. #define NAND_Ecc_P1o (1 << 16)
  40. #define NAND_Ecc_P2o (1 << 17)
  41. #define NAND_Ecc_P4o (1 << 18)
  42. #define NAND_Ecc_P8o (1 << 19)
  43. #define NAND_Ecc_P16o (1 << 20)
  44. #define NAND_Ecc_P32o (1 << 21)
  45. #define NAND_Ecc_P64o (1 << 22)
  46. #define NAND_Ecc_P128o (1 << 23)
  47. #define NAND_Ecc_P256o (1 << 24)
  48. #define NAND_Ecc_P512o (1 << 25)
  49. #define NAND_Ecc_P1024o (1 << 26)
  50. #define NAND_Ecc_P2048o (1 << 27)
  51. #define TF(value) (value ? 1 : 0)
  52. #define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
  53. #define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
  54. #define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
  55. #define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
  56. #define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
  57. #define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
  58. #define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
  59. #define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
  60. #define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
  61. #define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
  62. #define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
  63. #define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
  64. #define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
  65. #define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
  66. #define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
  67. #define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
  68. #define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
  69. #define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
  70. #define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
  71. #define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
  72. #define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
  73. #define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
  74. #define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
  75. #define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
  76. #define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
  77. #define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
  78. #define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
  79. #define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
  80. #define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
  81. #define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
  82. #define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
  83. #define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
  84. #define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
  85. #define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
  86. static const char *part_probes[] = { "cmdlinepart", NULL };
  87. /* oob info generated runtime depending on ecc algorithm and layout selected */
  88. static struct nand_ecclayout omap_oobinfo;
  89. /* Define some generic bad / good block scan pattern which are used
  90. * while scanning a device for factory marked good / bad blocks
  91. */
  92. static uint8_t scan_ff_pattern[] = { 0xff };
  93. static struct nand_bbt_descr bb_descrip_flashbased = {
  94. .options = NAND_BBT_SCANEMPTY | NAND_BBT_SCANALLPAGES,
  95. .offs = 0,
  96. .len = 1,
  97. .pattern = scan_ff_pattern,
  98. };
  99. struct omap_nand_info {
  100. struct nand_hw_control controller;
  101. struct omap_nand_platform_data *pdata;
  102. struct mtd_info mtd;
  103. struct mtd_partition *parts;
  104. struct nand_chip nand;
  105. struct platform_device *pdev;
  106. int gpmc_cs;
  107. unsigned long phys_base;
  108. struct completion comp;
  109. int dma_ch;
  110. int gpmc_irq;
  111. enum {
  112. OMAP_NAND_IO_READ = 0, /* read */
  113. OMAP_NAND_IO_WRITE, /* write */
  114. } iomode;
  115. u_char *buf;
  116. int buf_len;
  117. };
  118. /**
  119. * omap_hwcontrol - hardware specific access to control-lines
  120. * @mtd: MTD device structure
  121. * @cmd: command to device
  122. * @ctrl:
  123. * NAND_NCE: bit 0 -> don't care
  124. * NAND_CLE: bit 1 -> Command Latch
  125. * NAND_ALE: bit 2 -> Address Latch
  126. *
  127. * NOTE: boards may use different bits for these!!
  128. */
  129. static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  130. {
  131. struct omap_nand_info *info = container_of(mtd,
  132. struct omap_nand_info, mtd);
  133. if (cmd != NAND_CMD_NONE) {
  134. if (ctrl & NAND_CLE)
  135. gpmc_nand_write(info->gpmc_cs, GPMC_NAND_COMMAND, cmd);
  136. else if (ctrl & NAND_ALE)
  137. gpmc_nand_write(info->gpmc_cs, GPMC_NAND_ADDRESS, cmd);
  138. else /* NAND_NCE */
  139. gpmc_nand_write(info->gpmc_cs, GPMC_NAND_DATA, cmd);
  140. }
  141. }
  142. /**
  143. * omap_read_buf8 - read data from NAND controller into buffer
  144. * @mtd: MTD device structure
  145. * @buf: buffer to store date
  146. * @len: number of bytes to read
  147. */
  148. static void omap_read_buf8(struct mtd_info *mtd, u_char *buf, int len)
  149. {
  150. struct nand_chip *nand = mtd->priv;
  151. ioread8_rep(nand->IO_ADDR_R, buf, len);
  152. }
  153. /**
  154. * omap_write_buf8 - write buffer to NAND controller
  155. * @mtd: MTD device structure
  156. * @buf: data buffer
  157. * @len: number of bytes to write
  158. */
  159. static void omap_write_buf8(struct mtd_info *mtd, const u_char *buf, int len)
  160. {
  161. struct omap_nand_info *info = container_of(mtd,
  162. struct omap_nand_info, mtd);
  163. u_char *p = (u_char *)buf;
  164. u32 status = 0;
  165. while (len--) {
  166. iowrite8(*p++, info->nand.IO_ADDR_W);
  167. /* wait until buffer is available for write */
  168. do {
  169. status = gpmc_read_status(GPMC_STATUS_BUFFER);
  170. } while (!status);
  171. }
  172. }
  173. /**
  174. * omap_read_buf16 - read data from NAND controller into buffer
  175. * @mtd: MTD device structure
  176. * @buf: buffer to store date
  177. * @len: number of bytes to read
  178. */
  179. static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
  180. {
  181. struct nand_chip *nand = mtd->priv;
  182. ioread16_rep(nand->IO_ADDR_R, buf, len / 2);
  183. }
  184. /**
  185. * omap_write_buf16 - write buffer to NAND controller
  186. * @mtd: MTD device structure
  187. * @buf: data buffer
  188. * @len: number of bytes to write
  189. */
  190. static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len)
  191. {
  192. struct omap_nand_info *info = container_of(mtd,
  193. struct omap_nand_info, mtd);
  194. u16 *p = (u16 *) buf;
  195. u32 status = 0;
  196. /* FIXME try bursts of writesw() or DMA ... */
  197. len >>= 1;
  198. while (len--) {
  199. iowrite16(*p++, info->nand.IO_ADDR_W);
  200. /* wait until buffer is available for write */
  201. do {
  202. status = gpmc_read_status(GPMC_STATUS_BUFFER);
  203. } while (!status);
  204. }
  205. }
  206. /**
  207. * omap_read_buf_pref - read data from NAND controller into buffer
  208. * @mtd: MTD device structure
  209. * @buf: buffer to store date
  210. * @len: number of bytes to read
  211. */
  212. static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
  213. {
  214. struct omap_nand_info *info = container_of(mtd,
  215. struct omap_nand_info, mtd);
  216. uint32_t r_count = 0;
  217. int ret = 0;
  218. u32 *p = (u32 *)buf;
  219. /* take care of subpage reads */
  220. if (len % 4) {
  221. if (info->nand.options & NAND_BUSWIDTH_16)
  222. omap_read_buf16(mtd, buf, len % 4);
  223. else
  224. omap_read_buf8(mtd, buf, len % 4);
  225. p = (u32 *) (buf + len % 4);
  226. len -= len % 4;
  227. }
  228. /* configure and start prefetch transfer */
  229. ret = gpmc_prefetch_enable(info->gpmc_cs,
  230. PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x0);
  231. if (ret) {
  232. /* PFPW engine is busy, use cpu copy method */
  233. if (info->nand.options & NAND_BUSWIDTH_16)
  234. omap_read_buf16(mtd, (u_char *)p, len);
  235. else
  236. omap_read_buf8(mtd, (u_char *)p, len);
  237. } else {
  238. do {
  239. r_count = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
  240. r_count = r_count >> 2;
  241. ioread32_rep(info->nand.IO_ADDR_R, p, r_count);
  242. p += r_count;
  243. len -= r_count << 2;
  244. } while (len);
  245. /* disable and stop the PFPW engine */
  246. gpmc_prefetch_reset(info->gpmc_cs);
  247. }
  248. }
  249. /**
  250. * omap_write_buf_pref - write buffer to NAND controller
  251. * @mtd: MTD device structure
  252. * @buf: data buffer
  253. * @len: number of bytes to write
  254. */
  255. static void omap_write_buf_pref(struct mtd_info *mtd,
  256. const u_char *buf, int len)
  257. {
  258. struct omap_nand_info *info = container_of(mtd,
  259. struct omap_nand_info, mtd);
  260. uint32_t w_count = 0;
  261. int i = 0, ret = 0;
  262. u16 *p = (u16 *)buf;
  263. unsigned long tim, limit;
  264. /* take care of subpage writes */
  265. if (len % 2 != 0) {
  266. writeb(*buf, info->nand.IO_ADDR_W);
  267. p = (u16 *)(buf + 1);
  268. len--;
  269. }
  270. /* configure and start prefetch transfer */
  271. ret = gpmc_prefetch_enable(info->gpmc_cs,
  272. PREFETCH_FIFOTHRESHOLD_MAX, 0x0, len, 0x1);
  273. if (ret) {
  274. /* PFPW engine is busy, use cpu copy method */
  275. if (info->nand.options & NAND_BUSWIDTH_16)
  276. omap_write_buf16(mtd, (u_char *)p, len);
  277. else
  278. omap_write_buf8(mtd, (u_char *)p, len);
  279. } else {
  280. while (len) {
  281. w_count = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
  282. w_count = w_count >> 1;
  283. for (i = 0; (i < w_count) && len; i++, len -= 2)
  284. iowrite16(*p++, info->nand.IO_ADDR_W);
  285. }
  286. /* wait for data to flushed-out before reset the prefetch */
  287. tim = 0;
  288. limit = (loops_per_jiffy *
  289. msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  290. while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit))
  291. cpu_relax();
  292. /* disable and stop the PFPW engine */
  293. gpmc_prefetch_reset(info->gpmc_cs);
  294. }
  295. }
  296. /*
  297. * omap_nand_dma_cb: callback on the completion of dma transfer
  298. * @lch: logical channel
  299. * @ch_satuts: channel status
  300. * @data: pointer to completion data structure
  301. */
  302. static void omap_nand_dma_cb(int lch, u16 ch_status, void *data)
  303. {
  304. complete((struct completion *) data);
  305. }
  306. /*
  307. * omap_nand_dma_transfer: configer and start dma transfer
  308. * @mtd: MTD device structure
  309. * @addr: virtual address in RAM of source/destination
  310. * @len: number of data bytes to be transferred
  311. * @is_write: flag for read/write operation
  312. */
  313. static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
  314. unsigned int len, int is_write)
  315. {
  316. struct omap_nand_info *info = container_of(mtd,
  317. struct omap_nand_info, mtd);
  318. enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
  319. DMA_FROM_DEVICE;
  320. dma_addr_t dma_addr;
  321. int ret;
  322. unsigned long tim, limit;
  323. /* The fifo depth is 64 bytes max.
  324. * But configure the FIFO-threahold to 32 to get a sync at each frame
  325. * and frame length is 32 bytes.
  326. */
  327. int buf_len = len >> 6;
  328. if (addr >= high_memory) {
  329. struct page *p1;
  330. if (((size_t)addr & PAGE_MASK) !=
  331. ((size_t)(addr + len - 1) & PAGE_MASK))
  332. goto out_copy;
  333. p1 = vmalloc_to_page(addr);
  334. if (!p1)
  335. goto out_copy;
  336. addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK);
  337. }
  338. dma_addr = dma_map_single(&info->pdev->dev, addr, len, dir);
  339. if (dma_mapping_error(&info->pdev->dev, dma_addr)) {
  340. dev_err(&info->pdev->dev,
  341. "Couldn't DMA map a %d byte buffer\n", len);
  342. goto out_copy;
  343. }
  344. if (is_write) {
  345. omap_set_dma_dest_params(info->dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  346. info->phys_base, 0, 0);
  347. omap_set_dma_src_params(info->dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  348. dma_addr, 0, 0);
  349. omap_set_dma_transfer_params(info->dma_ch, OMAP_DMA_DATA_TYPE_S32,
  350. 0x10, buf_len, OMAP_DMA_SYNC_FRAME,
  351. OMAP24XX_DMA_GPMC, OMAP_DMA_DST_SYNC);
  352. } else {
  353. omap_set_dma_src_params(info->dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  354. info->phys_base, 0, 0);
  355. omap_set_dma_dest_params(info->dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  356. dma_addr, 0, 0);
  357. omap_set_dma_transfer_params(info->dma_ch, OMAP_DMA_DATA_TYPE_S32,
  358. 0x10, buf_len, OMAP_DMA_SYNC_FRAME,
  359. OMAP24XX_DMA_GPMC, OMAP_DMA_SRC_SYNC);
  360. }
  361. /* configure and start prefetch transfer */
  362. ret = gpmc_prefetch_enable(info->gpmc_cs,
  363. PREFETCH_FIFOTHRESHOLD_MAX, 0x1, len, is_write);
  364. if (ret)
  365. /* PFPW engine is busy, use cpu copy method */
  366. goto out_copy;
  367. init_completion(&info->comp);
  368. omap_start_dma(info->dma_ch);
  369. /* setup and start DMA using dma_addr */
  370. wait_for_completion(&info->comp);
  371. tim = 0;
  372. limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  373. while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit))
  374. cpu_relax();
  375. /* disable and stop the PFPW engine */
  376. gpmc_prefetch_reset(info->gpmc_cs);
  377. dma_unmap_single(&info->pdev->dev, dma_addr, len, dir);
  378. return 0;
  379. out_copy:
  380. if (info->nand.options & NAND_BUSWIDTH_16)
  381. is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
  382. : omap_write_buf16(mtd, (u_char *) addr, len);
  383. else
  384. is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
  385. : omap_write_buf8(mtd, (u_char *) addr, len);
  386. return 0;
  387. }
  388. /**
  389. * omap_read_buf_dma_pref - read data from NAND controller into buffer
  390. * @mtd: MTD device structure
  391. * @buf: buffer to store date
  392. * @len: number of bytes to read
  393. */
  394. static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len)
  395. {
  396. if (len <= mtd->oobsize)
  397. omap_read_buf_pref(mtd, buf, len);
  398. else
  399. /* start transfer in DMA mode */
  400. omap_nand_dma_transfer(mtd, buf, len, 0x0);
  401. }
  402. /**
  403. * omap_write_buf_dma_pref - write buffer to NAND controller
  404. * @mtd: MTD device structure
  405. * @buf: data buffer
  406. * @len: number of bytes to write
  407. */
  408. static void omap_write_buf_dma_pref(struct mtd_info *mtd,
  409. const u_char *buf, int len)
  410. {
  411. if (len <= mtd->oobsize)
  412. omap_write_buf_pref(mtd, buf, len);
  413. else
  414. /* start transfer in DMA mode */
  415. omap_nand_dma_transfer(mtd, (u_char *) buf, len, 0x1);
  416. }
  417. /*
  418. * omap_nand_irq - GMPC irq handler
  419. * @this_irq: gpmc irq number
  420. * @dev: omap_nand_info structure pointer is passed here
  421. */
  422. static irqreturn_t omap_nand_irq(int this_irq, void *dev)
  423. {
  424. struct omap_nand_info *info = (struct omap_nand_info *) dev;
  425. u32 bytes;
  426. u32 irq_stat;
  427. irq_stat = gpmc_read_status(GPMC_GET_IRQ_STATUS);
  428. bytes = gpmc_read_status(GPMC_PREFETCH_FIFO_CNT);
  429. bytes = bytes & 0xFFFC; /* io in multiple of 4 bytes */
  430. if (info->iomode == OMAP_NAND_IO_WRITE) { /* checks for write io */
  431. if (irq_stat & 0x2)
  432. goto done;
  433. if (info->buf_len && (info->buf_len < bytes))
  434. bytes = info->buf_len;
  435. else if (!info->buf_len)
  436. bytes = 0;
  437. iowrite32_rep(info->nand.IO_ADDR_W,
  438. (u32 *)info->buf, bytes >> 2);
  439. info->buf = info->buf + bytes;
  440. info->buf_len -= bytes;
  441. } else {
  442. ioread32_rep(info->nand.IO_ADDR_R,
  443. (u32 *)info->buf, bytes >> 2);
  444. info->buf = info->buf + bytes;
  445. if (irq_stat & 0x2)
  446. goto done;
  447. }
  448. gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, irq_stat);
  449. return IRQ_HANDLED;
  450. done:
  451. complete(&info->comp);
  452. /* disable irq */
  453. gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ, 0);
  454. /* clear status */
  455. gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, irq_stat);
  456. return IRQ_HANDLED;
  457. }
  458. /*
  459. * omap_read_buf_irq_pref - read data from NAND controller into buffer
  460. * @mtd: MTD device structure
  461. * @buf: buffer to store date
  462. * @len: number of bytes to read
  463. */
  464. static void omap_read_buf_irq_pref(struct mtd_info *mtd, u_char *buf, int len)
  465. {
  466. struct omap_nand_info *info = container_of(mtd,
  467. struct omap_nand_info, mtd);
  468. int ret = 0;
  469. if (len <= mtd->oobsize) {
  470. omap_read_buf_pref(mtd, buf, len);
  471. return;
  472. }
  473. info->iomode = OMAP_NAND_IO_READ;
  474. info->buf = buf;
  475. init_completion(&info->comp);
  476. /* configure and start prefetch transfer */
  477. ret = gpmc_prefetch_enable(info->gpmc_cs,
  478. PREFETCH_FIFOTHRESHOLD_MAX/2, 0x0, len, 0x0);
  479. if (ret)
  480. /* PFPW engine is busy, use cpu copy method */
  481. goto out_copy;
  482. info->buf_len = len;
  483. /* enable irq */
  484. gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ,
  485. (GPMC_IRQ_FIFOEVENTENABLE | GPMC_IRQ_COUNT_EVENT));
  486. /* waiting for read to complete */
  487. wait_for_completion(&info->comp);
  488. /* disable and stop the PFPW engine */
  489. gpmc_prefetch_reset(info->gpmc_cs);
  490. return;
  491. out_copy:
  492. if (info->nand.options & NAND_BUSWIDTH_16)
  493. omap_read_buf16(mtd, buf, len);
  494. else
  495. omap_read_buf8(mtd, buf, len);
  496. }
  497. /*
  498. * omap_write_buf_irq_pref - write buffer to NAND controller
  499. * @mtd: MTD device structure
  500. * @buf: data buffer
  501. * @len: number of bytes to write
  502. */
  503. static void omap_write_buf_irq_pref(struct mtd_info *mtd,
  504. const u_char *buf, int len)
  505. {
  506. struct omap_nand_info *info = container_of(mtd,
  507. struct omap_nand_info, mtd);
  508. int ret = 0;
  509. unsigned long tim, limit;
  510. if (len <= mtd->oobsize) {
  511. omap_write_buf_pref(mtd, buf, len);
  512. return;
  513. }
  514. info->iomode = OMAP_NAND_IO_WRITE;
  515. info->buf = (u_char *) buf;
  516. init_completion(&info->comp);
  517. /* configure and start prefetch transfer : size=24 */
  518. ret = gpmc_prefetch_enable(info->gpmc_cs,
  519. (PREFETCH_FIFOTHRESHOLD_MAX * 3) / 8, 0x0, len, 0x1);
  520. if (ret)
  521. /* PFPW engine is busy, use cpu copy method */
  522. goto out_copy;
  523. info->buf_len = len;
  524. /* enable irq */
  525. gpmc_cs_configure(info->gpmc_cs, GPMC_ENABLE_IRQ,
  526. (GPMC_IRQ_FIFOEVENTENABLE | GPMC_IRQ_COUNT_EVENT));
  527. /* waiting for write to complete */
  528. wait_for_completion(&info->comp);
  529. /* wait for data to flushed-out before reset the prefetch */
  530. tim = 0;
  531. limit = (loops_per_jiffy * msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS));
  532. while (gpmc_read_status(GPMC_PREFETCH_COUNT) && (tim++ < limit))
  533. cpu_relax();
  534. /* disable and stop the PFPW engine */
  535. gpmc_prefetch_reset(info->gpmc_cs);
  536. return;
  537. out_copy:
  538. if (info->nand.options & NAND_BUSWIDTH_16)
  539. omap_write_buf16(mtd, buf, len);
  540. else
  541. omap_write_buf8(mtd, buf, len);
  542. }
  543. /**
  544. * omap_verify_buf - Verify chip data against buffer
  545. * @mtd: MTD device structure
  546. * @buf: buffer containing the data to compare
  547. * @len: number of bytes to compare
  548. */
  549. static int omap_verify_buf(struct mtd_info *mtd, const u_char * buf, int len)
  550. {
  551. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  552. mtd);
  553. u16 *p = (u16 *) buf;
  554. len >>= 1;
  555. while (len--) {
  556. if (*p++ != cpu_to_le16(readw(info->nand.IO_ADDR_R)))
  557. return -EFAULT;
  558. }
  559. return 0;
  560. }
  561. /**
  562. * gen_true_ecc - This function will generate true ECC value
  563. * @ecc_buf: buffer to store ecc code
  564. *
  565. * This generated true ECC value can be used when correcting
  566. * data read from NAND flash memory core
  567. */
  568. static void gen_true_ecc(u8 *ecc_buf)
  569. {
  570. u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
  571. ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
  572. ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
  573. P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
  574. ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
  575. P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
  576. ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
  577. P1e(tmp) | P2048o(tmp) | P2048e(tmp));
  578. }
  579. /**
  580. * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
  581. * @ecc_data1: ecc code from nand spare area
  582. * @ecc_data2: ecc code from hardware register obtained from hardware ecc
  583. * @page_data: page data
  584. *
  585. * This function compares two ECC's and indicates if there is an error.
  586. * If the error can be corrected it will be corrected to the buffer.
  587. * If there is no error, %0 is returned. If there is an error but it
  588. * was corrected, %1 is returned. Otherwise, %-1 is returned.
  589. */
  590. static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
  591. u8 *ecc_data2, /* read from register */
  592. u8 *page_data)
  593. {
  594. uint i;
  595. u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
  596. u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
  597. u8 ecc_bit[24];
  598. u8 ecc_sum = 0;
  599. u8 find_bit = 0;
  600. uint find_byte = 0;
  601. int isEccFF;
  602. isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
  603. gen_true_ecc(ecc_data1);
  604. gen_true_ecc(ecc_data2);
  605. for (i = 0; i <= 2; i++) {
  606. *(ecc_data1 + i) = ~(*(ecc_data1 + i));
  607. *(ecc_data2 + i) = ~(*(ecc_data2 + i));
  608. }
  609. for (i = 0; i < 8; i++) {
  610. tmp0_bit[i] = *ecc_data1 % 2;
  611. *ecc_data1 = *ecc_data1 / 2;
  612. }
  613. for (i = 0; i < 8; i++) {
  614. tmp1_bit[i] = *(ecc_data1 + 1) % 2;
  615. *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
  616. }
  617. for (i = 0; i < 8; i++) {
  618. tmp2_bit[i] = *(ecc_data1 + 2) % 2;
  619. *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
  620. }
  621. for (i = 0; i < 8; i++) {
  622. comp0_bit[i] = *ecc_data2 % 2;
  623. *ecc_data2 = *ecc_data2 / 2;
  624. }
  625. for (i = 0; i < 8; i++) {
  626. comp1_bit[i] = *(ecc_data2 + 1) % 2;
  627. *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
  628. }
  629. for (i = 0; i < 8; i++) {
  630. comp2_bit[i] = *(ecc_data2 + 2) % 2;
  631. *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
  632. }
  633. for (i = 0; i < 6; i++)
  634. ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
  635. for (i = 0; i < 8; i++)
  636. ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
  637. for (i = 0; i < 8; i++)
  638. ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
  639. ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
  640. ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
  641. for (i = 0; i < 24; i++)
  642. ecc_sum += ecc_bit[i];
  643. switch (ecc_sum) {
  644. case 0:
  645. /* Not reached because this function is not called if
  646. * ECC values are equal
  647. */
  648. return 0;
  649. case 1:
  650. /* Uncorrectable error */
  651. DEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
  652. return -1;
  653. case 11:
  654. /* UN-Correctable error */
  655. DEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR B\n");
  656. return -1;
  657. case 12:
  658. /* Correctable error */
  659. find_byte = (ecc_bit[23] << 8) +
  660. (ecc_bit[21] << 7) +
  661. (ecc_bit[19] << 6) +
  662. (ecc_bit[17] << 5) +
  663. (ecc_bit[15] << 4) +
  664. (ecc_bit[13] << 3) +
  665. (ecc_bit[11] << 2) +
  666. (ecc_bit[9] << 1) +
  667. ecc_bit[7];
  668. find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
  669. DEBUG(MTD_DEBUG_LEVEL0, "Correcting single bit ECC error at "
  670. "offset: %d, bit: %d\n", find_byte, find_bit);
  671. page_data[find_byte] ^= (1 << find_bit);
  672. return 1;
  673. default:
  674. if (isEccFF) {
  675. if (ecc_data2[0] == 0 &&
  676. ecc_data2[1] == 0 &&
  677. ecc_data2[2] == 0)
  678. return 0;
  679. }
  680. DEBUG(MTD_DEBUG_LEVEL0, "UNCORRECTED_ERROR default\n");
  681. return -1;
  682. }
  683. }
  684. /**
  685. * omap_correct_data - Compares the ECC read with HW generated ECC
  686. * @mtd: MTD device structure
  687. * @dat: page data
  688. * @read_ecc: ecc read from nand flash
  689. * @calc_ecc: ecc read from HW ECC registers
  690. *
  691. * Compares the ecc read from nand spare area with ECC registers values
  692. * and if ECC's mismatched, it will call 'omap_compare_ecc' for error
  693. * detection and correction. If there are no errors, %0 is returned. If
  694. * there were errors and all of the errors were corrected, the number of
  695. * corrected errors is returned. If uncorrectable errors exist, %-1 is
  696. * returned.
  697. */
  698. static int omap_correct_data(struct mtd_info *mtd, u_char *dat,
  699. u_char *read_ecc, u_char *calc_ecc)
  700. {
  701. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  702. mtd);
  703. int blockCnt = 0, i = 0, ret = 0;
  704. int stat = 0;
  705. /* Ex NAND_ECC_HW12_2048 */
  706. if ((info->nand.ecc.mode == NAND_ECC_HW) &&
  707. (info->nand.ecc.size == 2048))
  708. blockCnt = 4;
  709. else
  710. blockCnt = 1;
  711. for (i = 0; i < blockCnt; i++) {
  712. if (memcmp(read_ecc, calc_ecc, 3) != 0) {
  713. ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
  714. if (ret < 0)
  715. return ret;
  716. /* keep track of the number of corrected errors */
  717. stat += ret;
  718. }
  719. read_ecc += 3;
  720. calc_ecc += 3;
  721. dat += 512;
  722. }
  723. return stat;
  724. }
  725. /**
  726. * omap_calcuate_ecc - Generate non-inverted ECC bytes.
  727. * @mtd: MTD device structure
  728. * @dat: The pointer to data on which ecc is computed
  729. * @ecc_code: The ecc_code buffer
  730. *
  731. * Using noninverted ECC can be considered ugly since writing a blank
  732. * page ie. padding will clear the ECC bytes. This is no problem as long
  733. * nobody is trying to write data on the seemingly unused page. Reading
  734. * an erased page will produce an ECC mismatch between generated and read
  735. * ECC bytes that has to be dealt with separately.
  736. */
  737. static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  738. u_char *ecc_code)
  739. {
  740. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  741. mtd);
  742. return gpmc_calculate_ecc(info->gpmc_cs, dat, ecc_code);
  743. }
  744. /**
  745. * omap_enable_hwecc - This function enables the hardware ecc functionality
  746. * @mtd: MTD device structure
  747. * @mode: Read/Write mode
  748. */
  749. static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
  750. {
  751. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  752. mtd);
  753. struct nand_chip *chip = mtd->priv;
  754. unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
  755. gpmc_enable_hwecc(info->gpmc_cs, mode, dev_width, info->nand.ecc.size);
  756. }
  757. /**
  758. * omap_wait - wait until the command is done
  759. * @mtd: MTD device structure
  760. * @chip: NAND Chip structure
  761. *
  762. * Wait function is called during Program and erase operations and
  763. * the way it is called from MTD layer, we should wait till the NAND
  764. * chip is ready after the programming/erase operation has completed.
  765. *
  766. * Erase can take up to 400ms and program up to 20ms according to
  767. * general NAND and SmartMedia specs
  768. */
  769. static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
  770. {
  771. struct nand_chip *this = mtd->priv;
  772. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  773. mtd);
  774. unsigned long timeo = jiffies;
  775. int status = NAND_STATUS_FAIL, state = this->state;
  776. if (state == FL_ERASING)
  777. timeo += (HZ * 400) / 1000;
  778. else
  779. timeo += (HZ * 20) / 1000;
  780. gpmc_nand_write(info->gpmc_cs,
  781. GPMC_NAND_COMMAND, (NAND_CMD_STATUS & 0xFF));
  782. while (time_before(jiffies, timeo)) {
  783. status = gpmc_nand_read(info->gpmc_cs, GPMC_NAND_DATA);
  784. if (status & NAND_STATUS_READY)
  785. break;
  786. cond_resched();
  787. }
  788. return status;
  789. }
  790. /**
  791. * omap_dev_ready - calls the platform specific dev_ready function
  792. * @mtd: MTD device structure
  793. */
  794. static int omap_dev_ready(struct mtd_info *mtd)
  795. {
  796. unsigned int val = 0;
  797. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  798. mtd);
  799. val = gpmc_read_status(GPMC_GET_IRQ_STATUS);
  800. if ((val & 0x100) == 0x100) {
  801. /* Clear IRQ Interrupt */
  802. val |= 0x100;
  803. val &= ~(0x0);
  804. gpmc_cs_configure(info->gpmc_cs, GPMC_SET_IRQ_STATUS, val);
  805. } else {
  806. unsigned int cnt = 0;
  807. while (cnt++ < 0x1FF) {
  808. if ((val & 0x100) == 0x100)
  809. return 0;
  810. val = gpmc_read_status(GPMC_GET_IRQ_STATUS);
  811. }
  812. }
  813. return 1;
  814. }
  815. static int __devinit omap_nand_probe(struct platform_device *pdev)
  816. {
  817. struct omap_nand_info *info;
  818. struct omap_nand_platform_data *pdata;
  819. int err;
  820. int i, offset;
  821. pdata = pdev->dev.platform_data;
  822. if (pdata == NULL) {
  823. dev_err(&pdev->dev, "platform data missing\n");
  824. return -ENODEV;
  825. }
  826. info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL);
  827. if (!info)
  828. return -ENOMEM;
  829. platform_set_drvdata(pdev, info);
  830. spin_lock_init(&info->controller.lock);
  831. init_waitqueue_head(&info->controller.wq);
  832. info->pdev = pdev;
  833. info->gpmc_cs = pdata->cs;
  834. info->phys_base = pdata->phys_base;
  835. info->mtd.priv = &info->nand;
  836. info->mtd.name = dev_name(&pdev->dev);
  837. info->mtd.owner = THIS_MODULE;
  838. info->nand.options = pdata->devsize;
  839. info->nand.options |= NAND_SKIP_BBTSCAN;
  840. /* NAND write protect off */
  841. gpmc_cs_configure(info->gpmc_cs, GPMC_CONFIG_WP, 0);
  842. if (!request_mem_region(info->phys_base, NAND_IO_SIZE,
  843. pdev->dev.driver->name)) {
  844. err = -EBUSY;
  845. goto out_free_info;
  846. }
  847. info->nand.IO_ADDR_R = ioremap(info->phys_base, NAND_IO_SIZE);
  848. if (!info->nand.IO_ADDR_R) {
  849. err = -ENOMEM;
  850. goto out_release_mem_region;
  851. }
  852. info->nand.controller = &info->controller;
  853. info->nand.IO_ADDR_W = info->nand.IO_ADDR_R;
  854. info->nand.cmd_ctrl = omap_hwcontrol;
  855. /*
  856. * If RDY/BSY line is connected to OMAP then use the omap ready
  857. * funcrtion and the generic nand_wait function which reads the status
  858. * register after monitoring the RDY/BSY line.Otherwise use a standard
  859. * chip delay which is slightly more than tR (AC Timing) of the NAND
  860. * device and read status register until you get a failure or success
  861. */
  862. if (pdata->dev_ready) {
  863. info->nand.dev_ready = omap_dev_ready;
  864. info->nand.chip_delay = 0;
  865. } else {
  866. info->nand.waitfunc = omap_wait;
  867. info->nand.chip_delay = 50;
  868. }
  869. switch (pdata->xfer_type) {
  870. case NAND_OMAP_PREFETCH_POLLED:
  871. info->nand.read_buf = omap_read_buf_pref;
  872. info->nand.write_buf = omap_write_buf_pref;
  873. break;
  874. case NAND_OMAP_POLLED:
  875. if (info->nand.options & NAND_BUSWIDTH_16) {
  876. info->nand.read_buf = omap_read_buf16;
  877. info->nand.write_buf = omap_write_buf16;
  878. } else {
  879. info->nand.read_buf = omap_read_buf8;
  880. info->nand.write_buf = omap_write_buf8;
  881. }
  882. break;
  883. case NAND_OMAP_PREFETCH_DMA:
  884. err = omap_request_dma(OMAP24XX_DMA_GPMC, "NAND",
  885. omap_nand_dma_cb, &info->comp, &info->dma_ch);
  886. if (err < 0) {
  887. info->dma_ch = -1;
  888. dev_err(&pdev->dev, "DMA request failed!\n");
  889. goto out_release_mem_region;
  890. } else {
  891. omap_set_dma_dest_burst_mode(info->dma_ch,
  892. OMAP_DMA_DATA_BURST_16);
  893. omap_set_dma_src_burst_mode(info->dma_ch,
  894. OMAP_DMA_DATA_BURST_16);
  895. info->nand.read_buf = omap_read_buf_dma_pref;
  896. info->nand.write_buf = omap_write_buf_dma_pref;
  897. }
  898. break;
  899. case NAND_OMAP_PREFETCH_IRQ:
  900. err = request_irq(pdata->gpmc_irq,
  901. omap_nand_irq, IRQF_SHARED, "gpmc-nand", info);
  902. if (err) {
  903. dev_err(&pdev->dev, "requesting irq(%d) error:%d",
  904. pdata->gpmc_irq, err);
  905. goto out_release_mem_region;
  906. } else {
  907. info->gpmc_irq = pdata->gpmc_irq;
  908. info->nand.read_buf = omap_read_buf_irq_pref;
  909. info->nand.write_buf = omap_write_buf_irq_pref;
  910. }
  911. break;
  912. default:
  913. dev_err(&pdev->dev,
  914. "xfer_type(%d) not supported!\n", pdata->xfer_type);
  915. err = -EINVAL;
  916. goto out_release_mem_region;
  917. }
  918. info->nand.verify_buf = omap_verify_buf;
  919. /* selsect the ecc type */
  920. if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_DEFAULT)
  921. info->nand.ecc.mode = NAND_ECC_SOFT;
  922. else if ((pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW) ||
  923. (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE)) {
  924. info->nand.ecc.bytes = 3;
  925. info->nand.ecc.size = 512;
  926. info->nand.ecc.calculate = omap_calculate_ecc;
  927. info->nand.ecc.hwctl = omap_enable_hwecc;
  928. info->nand.ecc.correct = omap_correct_data;
  929. info->nand.ecc.mode = NAND_ECC_HW;
  930. }
  931. /* DIP switches on some boards change between 8 and 16 bit
  932. * bus widths for flash. Try the other width if the first try fails.
  933. */
  934. if (nand_scan_ident(&info->mtd, 1, NULL)) {
  935. info->nand.options ^= NAND_BUSWIDTH_16;
  936. if (nand_scan_ident(&info->mtd, 1, NULL)) {
  937. err = -ENXIO;
  938. goto out_release_mem_region;
  939. }
  940. }
  941. /* rom code layout */
  942. if (pdata->ecc_opt == OMAP_ECC_HAMMING_CODE_HW_ROMCODE) {
  943. if (info->nand.options & NAND_BUSWIDTH_16)
  944. offset = 2;
  945. else {
  946. offset = 1;
  947. info->nand.badblock_pattern = &bb_descrip_flashbased;
  948. }
  949. omap_oobinfo.eccbytes = 3 * (info->mtd.oobsize/16);
  950. for (i = 0; i < omap_oobinfo.eccbytes; i++)
  951. omap_oobinfo.eccpos[i] = i+offset;
  952. omap_oobinfo.oobfree->offset = offset + omap_oobinfo.eccbytes;
  953. omap_oobinfo.oobfree->length = info->mtd.oobsize -
  954. (offset + omap_oobinfo.eccbytes);
  955. info->nand.ecc.layout = &omap_oobinfo;
  956. }
  957. /* second phase scan */
  958. if (nand_scan_tail(&info->mtd)) {
  959. err = -ENXIO;
  960. goto out_release_mem_region;
  961. }
  962. err = parse_mtd_partitions(&info->mtd, part_probes, &info->parts, 0);
  963. if (err > 0)
  964. mtd_device_register(&info->mtd, info->parts, err);
  965. else if (pdata->parts)
  966. mtd_device_register(&info->mtd, pdata->parts, pdata->nr_parts);
  967. else
  968. mtd_device_register(&info->mtd, NULL, 0);
  969. platform_set_drvdata(pdev, &info->mtd);
  970. return 0;
  971. out_release_mem_region:
  972. release_mem_region(info->phys_base, NAND_IO_SIZE);
  973. out_free_info:
  974. kfree(info);
  975. return err;
  976. }
  977. static int omap_nand_remove(struct platform_device *pdev)
  978. {
  979. struct mtd_info *mtd = platform_get_drvdata(pdev);
  980. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  981. mtd);
  982. platform_set_drvdata(pdev, NULL);
  983. if (info->dma_ch != -1)
  984. omap_free_dma(info->dma_ch);
  985. if (info->gpmc_irq)
  986. free_irq(info->gpmc_irq, info);
  987. /* Release NAND device, its internal structures and partitions */
  988. nand_release(&info->mtd);
  989. iounmap(info->nand.IO_ADDR_R);
  990. kfree(&info->mtd);
  991. return 0;
  992. }
  993. static struct platform_driver omap_nand_driver = {
  994. .probe = omap_nand_probe,
  995. .remove = omap_nand_remove,
  996. .driver = {
  997. .name = DRIVER_NAME,
  998. .owner = THIS_MODULE,
  999. },
  1000. };
  1001. static int __init omap_nand_init(void)
  1002. {
  1003. pr_info("%s driver initializing\n", DRIVER_NAME);
  1004. return platform_driver_register(&omap_nand_driver);
  1005. }
  1006. static void __exit omap_nand_exit(void)
  1007. {
  1008. platform_driver_unregister(&omap_nand_driver);
  1009. }
  1010. module_init(omap_nand_init);
  1011. module_exit(omap_nand_exit);
  1012. MODULE_ALIAS("platform:" DRIVER_NAME);
  1013. MODULE_LICENSE("GPL");
  1014. MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");