omap2.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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/jiffies.h>
  14. #include <linux/sched.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/nand.h>
  17. #include <linux/mtd/partitions.h>
  18. #include <linux/io.h>
  19. #include <plat/dma.h>
  20. #include <plat/gpmc.h>
  21. #include <plat/nand.h>
  22. #define GPMC_IRQ_STATUS 0x18
  23. #define GPMC_ECC_CONFIG 0x1F4
  24. #define GPMC_ECC_CONTROL 0x1F8
  25. #define GPMC_ECC_SIZE_CONFIG 0x1FC
  26. #define GPMC_ECC1_RESULT 0x200
  27. #define DRIVER_NAME "omap2-nand"
  28. #define NAND_WP_OFF 0
  29. #define NAND_WP_BIT 0x00000010
  30. #define GPMC_BUF_FULL 0x00000001
  31. #define GPMC_BUF_EMPTY 0x00000000
  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. #ifdef CONFIG_MTD_PARTITIONS
  92. static const char *part_probes[] = { "cmdlinepart", NULL };
  93. #endif
  94. #ifdef CONFIG_MTD_NAND_OMAP_PREFETCH
  95. static int use_prefetch = 1;
  96. /* "modprobe ... use_prefetch=0" etc */
  97. module_param(use_prefetch, bool, 0);
  98. MODULE_PARM_DESC(use_prefetch, "enable/disable use of PREFETCH");
  99. #ifdef CONFIG_MTD_NAND_OMAP_PREFETCH_DMA
  100. static int use_dma = 1;
  101. /* "modprobe ... use_dma=0" etc */
  102. module_param(use_dma, bool, 0);
  103. MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
  104. #else
  105. const int use_dma;
  106. #endif
  107. #else
  108. const int use_prefetch;
  109. const int use_dma;
  110. #endif
  111. struct omap_nand_info {
  112. struct nand_hw_control controller;
  113. struct omap_nand_platform_data *pdata;
  114. struct mtd_info mtd;
  115. struct mtd_partition *parts;
  116. struct nand_chip nand;
  117. struct platform_device *pdev;
  118. int gpmc_cs;
  119. unsigned long phys_base;
  120. void __iomem *gpmc_cs_baseaddr;
  121. void __iomem *gpmc_baseaddr;
  122. void __iomem *nand_pref_fifo_add;
  123. struct completion comp;
  124. int dma_ch;
  125. };
  126. /**
  127. * omap_nand_wp - This function enable or disable the Write Protect feature
  128. * @mtd: MTD device structure
  129. * @mode: WP ON/OFF
  130. */
  131. static void omap_nand_wp(struct mtd_info *mtd, int mode)
  132. {
  133. struct omap_nand_info *info = container_of(mtd,
  134. struct omap_nand_info, mtd);
  135. unsigned long config = __raw_readl(info->gpmc_baseaddr + GPMC_CONFIG);
  136. if (mode)
  137. config &= ~(NAND_WP_BIT); /* WP is ON */
  138. else
  139. config |= (NAND_WP_BIT); /* WP is OFF */
  140. __raw_writel(config, (info->gpmc_baseaddr + GPMC_CONFIG));
  141. }
  142. /**
  143. * omap_hwcontrol - hardware specific access to control-lines
  144. * @mtd: MTD device structure
  145. * @cmd: command to device
  146. * @ctrl:
  147. * NAND_NCE: bit 0 -> don't care
  148. * NAND_CLE: bit 1 -> Command Latch
  149. * NAND_ALE: bit 2 -> Address Latch
  150. *
  151. * NOTE: boards may use different bits for these!!
  152. */
  153. static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  154. {
  155. struct omap_nand_info *info = container_of(mtd,
  156. struct omap_nand_info, mtd);
  157. switch (ctrl) {
  158. case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
  159. info->nand.IO_ADDR_W = info->gpmc_cs_baseaddr +
  160. GPMC_CS_NAND_COMMAND;
  161. info->nand.IO_ADDR_R = info->gpmc_cs_baseaddr +
  162. GPMC_CS_NAND_DATA;
  163. break;
  164. case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
  165. info->nand.IO_ADDR_W = info->gpmc_cs_baseaddr +
  166. GPMC_CS_NAND_ADDRESS;
  167. info->nand.IO_ADDR_R = info->gpmc_cs_baseaddr +
  168. GPMC_CS_NAND_DATA;
  169. break;
  170. case NAND_CTRL_CHANGE | NAND_NCE:
  171. info->nand.IO_ADDR_W = info->gpmc_cs_baseaddr +
  172. GPMC_CS_NAND_DATA;
  173. info->nand.IO_ADDR_R = info->gpmc_cs_baseaddr +
  174. GPMC_CS_NAND_DATA;
  175. break;
  176. }
  177. if (cmd != NAND_CMD_NONE)
  178. __raw_writeb(cmd, info->nand.IO_ADDR_W);
  179. }
  180. /**
  181. * omap_read_buf8 - 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_buf8(struct mtd_info *mtd, u_char *buf, int len)
  187. {
  188. struct nand_chip *nand = mtd->priv;
  189. ioread8_rep(nand->IO_ADDR_R, buf, len);
  190. }
  191. /**
  192. * omap_write_buf8 - 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_buf8(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. u_char *p = (u_char *)buf;
  202. while (len--) {
  203. iowrite8(*p++, info->nand.IO_ADDR_W);
  204. while (GPMC_BUF_EMPTY == (readl(info->gpmc_baseaddr +
  205. GPMC_STATUS) & GPMC_BUF_FULL));
  206. }
  207. }
  208. /**
  209. * omap_read_buf16 - read data from NAND controller into buffer
  210. * @mtd: MTD device structure
  211. * @buf: buffer to store date
  212. * @len: number of bytes to read
  213. */
  214. static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
  215. {
  216. struct nand_chip *nand = mtd->priv;
  217. ioread16_rep(nand->IO_ADDR_R, buf, len / 2);
  218. }
  219. /**
  220. * omap_write_buf16 - write buffer to NAND controller
  221. * @mtd: MTD device structure
  222. * @buf: data buffer
  223. * @len: number of bytes to write
  224. */
  225. static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len)
  226. {
  227. struct omap_nand_info *info = container_of(mtd,
  228. struct omap_nand_info, mtd);
  229. u16 *p = (u16 *) buf;
  230. /* FIXME try bursts of writesw() or DMA ... */
  231. len >>= 1;
  232. while (len--) {
  233. iowrite16(*p++, info->nand.IO_ADDR_W);
  234. while (GPMC_BUF_EMPTY == (readl(info->gpmc_baseaddr +
  235. GPMC_STATUS) & GPMC_BUF_FULL))
  236. ;
  237. }
  238. }
  239. /**
  240. * omap_read_buf_pref - read data from NAND controller into buffer
  241. * @mtd: MTD device structure
  242. * @buf: buffer to store date
  243. * @len: number of bytes to read
  244. */
  245. static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
  246. {
  247. struct omap_nand_info *info = container_of(mtd,
  248. struct omap_nand_info, mtd);
  249. uint32_t pfpw_status = 0, r_count = 0;
  250. int ret = 0;
  251. u32 *p = (u32 *)buf;
  252. /* take care of subpage reads */
  253. for (; len % 4 != 0; ) {
  254. *buf++ = __raw_readb(info->nand.IO_ADDR_R);
  255. len--;
  256. }
  257. p = (u32 *) buf;
  258. /* configure and start prefetch transfer */
  259. ret = gpmc_prefetch_enable(info->gpmc_cs, 0x0, len, 0x0);
  260. if (ret) {
  261. /* PFPW engine is busy, use cpu copy method */
  262. if (info->nand.options & NAND_BUSWIDTH_16)
  263. omap_read_buf16(mtd, buf, len);
  264. else
  265. omap_read_buf8(mtd, buf, len);
  266. } else {
  267. do {
  268. pfpw_status = gpmc_prefetch_status();
  269. r_count = ((pfpw_status >> 24) & 0x7F) >> 2;
  270. ioread32_rep(info->nand_pref_fifo_add, p, r_count);
  271. p += r_count;
  272. len -= r_count << 2;
  273. } while (len);
  274. /* disable and stop the PFPW engine */
  275. gpmc_prefetch_reset();
  276. }
  277. }
  278. /**
  279. * omap_write_buf_pref - write buffer to NAND controller
  280. * @mtd: MTD device structure
  281. * @buf: data buffer
  282. * @len: number of bytes to write
  283. */
  284. static void omap_write_buf_pref(struct mtd_info *mtd,
  285. const u_char *buf, int len)
  286. {
  287. struct omap_nand_info *info = container_of(mtd,
  288. struct omap_nand_info, mtd);
  289. uint32_t pfpw_status = 0, w_count = 0;
  290. int i = 0, ret = 0;
  291. u16 *p = (u16 *) buf;
  292. /* take care of subpage writes */
  293. if (len % 2 != 0) {
  294. writeb(*buf, info->nand.IO_ADDR_R);
  295. p = (u16 *)(buf + 1);
  296. len--;
  297. }
  298. /* configure and start prefetch transfer */
  299. ret = gpmc_prefetch_enable(info->gpmc_cs, 0x0, len, 0x1);
  300. if (ret) {
  301. /* PFPW engine is busy, use cpu copy method */
  302. if (info->nand.options & NAND_BUSWIDTH_16)
  303. omap_write_buf16(mtd, buf, len);
  304. else
  305. omap_write_buf8(mtd, buf, len);
  306. } else {
  307. pfpw_status = gpmc_prefetch_status();
  308. while (pfpw_status & 0x3FFF) {
  309. w_count = ((pfpw_status >> 24) & 0x7F) >> 1;
  310. for (i = 0; (i < w_count) && len; i++, len -= 2)
  311. iowrite16(*p++, info->nand_pref_fifo_add);
  312. pfpw_status = gpmc_prefetch_status();
  313. }
  314. /* disable and stop the PFPW engine */
  315. gpmc_prefetch_reset();
  316. }
  317. }
  318. #ifdef CONFIG_MTD_NAND_OMAP_PREFETCH_DMA
  319. /*
  320. * omap_nand_dma_cb: callback on the completion of dma transfer
  321. * @lch: logical channel
  322. * @ch_satuts: channel status
  323. * @data: pointer to completion data structure
  324. */
  325. static void omap_nand_dma_cb(int lch, u16 ch_status, void *data)
  326. {
  327. complete((struct completion *) data);
  328. }
  329. /*
  330. * omap_nand_dma_transfer: configer and start dma transfer
  331. * @mtd: MTD device structure
  332. * @addr: virtual address in RAM of source/destination
  333. * @len: number of data bytes to be transferred
  334. * @is_write: flag for read/write operation
  335. */
  336. static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
  337. unsigned int len, int is_write)
  338. {
  339. struct omap_nand_info *info = container_of(mtd,
  340. struct omap_nand_info, mtd);
  341. uint32_t prefetch_status = 0;
  342. enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
  343. DMA_FROM_DEVICE;
  344. dma_addr_t dma_addr;
  345. int ret;
  346. /* The fifo depth is 64 bytes. We have a sync at each frame and frame
  347. * length is 64 bytes.
  348. */
  349. int buf_len = len >> 6;
  350. if (addr >= high_memory) {
  351. struct page *p1;
  352. if (((size_t)addr & PAGE_MASK) !=
  353. ((size_t)(addr + len - 1) & PAGE_MASK))
  354. goto out_copy;
  355. p1 = vmalloc_to_page(addr);
  356. if (!p1)
  357. goto out_copy;
  358. addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK);
  359. }
  360. dma_addr = dma_map_single(&info->pdev->dev, addr, len, dir);
  361. if (dma_mapping_error(&info->pdev->dev, dma_addr)) {
  362. dev_err(&info->pdev->dev,
  363. "Couldn't DMA map a %d byte buffer\n", len);
  364. goto out_copy;
  365. }
  366. if (is_write) {
  367. omap_set_dma_dest_params(info->dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  368. info->phys_base, 0, 0);
  369. omap_set_dma_src_params(info->dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  370. dma_addr, 0, 0);
  371. omap_set_dma_transfer_params(info->dma_ch, OMAP_DMA_DATA_TYPE_S32,
  372. 0x10, buf_len, OMAP_DMA_SYNC_FRAME,
  373. OMAP24XX_DMA_GPMC, OMAP_DMA_DST_SYNC);
  374. } else {
  375. omap_set_dma_src_params(info->dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
  376. info->phys_base, 0, 0);
  377. omap_set_dma_dest_params(info->dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
  378. dma_addr, 0, 0);
  379. omap_set_dma_transfer_params(info->dma_ch, OMAP_DMA_DATA_TYPE_S32,
  380. 0x10, buf_len, OMAP_DMA_SYNC_FRAME,
  381. OMAP24XX_DMA_GPMC, OMAP_DMA_SRC_SYNC);
  382. }
  383. /* configure and start prefetch transfer */
  384. ret = gpmc_prefetch_enable(info->gpmc_cs, 0x1, len, is_write);
  385. if (ret)
  386. /* PFPW engine is busy, use cpu copy methode */
  387. goto out_copy;
  388. init_completion(&info->comp);
  389. omap_start_dma(info->dma_ch);
  390. /* setup and start DMA using dma_addr */
  391. wait_for_completion(&info->comp);
  392. while (0x3fff & (prefetch_status = gpmc_prefetch_status()))
  393. ;
  394. /* disable and stop the PFPW engine */
  395. gpmc_prefetch_reset();
  396. dma_unmap_single(&info->pdev->dev, dma_addr, len, dir);
  397. return 0;
  398. out_copy:
  399. if (info->nand.options & NAND_BUSWIDTH_16)
  400. is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
  401. : omap_write_buf16(mtd, (u_char *) addr, len);
  402. else
  403. is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
  404. : omap_write_buf8(mtd, (u_char *) addr, len);
  405. return 0;
  406. }
  407. #else
  408. static void omap_nand_dma_cb(int lch, u16 ch_status, void *data) {}
  409. static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
  410. unsigned int len, int is_write)
  411. {
  412. return 0;
  413. }
  414. #endif
  415. /**
  416. * omap_read_buf_dma_pref - read data from NAND controller into buffer
  417. * @mtd: MTD device structure
  418. * @buf: buffer to store date
  419. * @len: number of bytes to read
  420. */
  421. static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len)
  422. {
  423. if (len <= mtd->oobsize)
  424. omap_read_buf_pref(mtd, buf, len);
  425. else
  426. /* start transfer in DMA mode */
  427. omap_nand_dma_transfer(mtd, buf, len, 0x0);
  428. }
  429. /**
  430. * omap_write_buf_dma_pref - write buffer to NAND controller
  431. * @mtd: MTD device structure
  432. * @buf: data buffer
  433. * @len: number of bytes to write
  434. */
  435. static void omap_write_buf_dma_pref(struct mtd_info *mtd,
  436. const u_char *buf, int len)
  437. {
  438. if (len <= mtd->oobsize)
  439. omap_write_buf_pref(mtd, buf, len);
  440. else
  441. /* start transfer in DMA mode */
  442. omap_nand_dma_transfer(mtd, buf, len, 0x1);
  443. }
  444. /**
  445. * omap_verify_buf - Verify chip data against buffer
  446. * @mtd: MTD device structure
  447. * @buf: buffer containing the data to compare
  448. * @len: number of bytes to compare
  449. */
  450. static int omap_verify_buf(struct mtd_info *mtd, const u_char * buf, int len)
  451. {
  452. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  453. mtd);
  454. u16 *p = (u16 *) buf;
  455. len >>= 1;
  456. while (len--) {
  457. if (*p++ != cpu_to_le16(readw(info->nand.IO_ADDR_R)))
  458. return -EFAULT;
  459. }
  460. return 0;
  461. }
  462. #ifdef CONFIG_MTD_NAND_OMAP_HWECC
  463. /**
  464. * omap_hwecc_init - Initialize the HW ECC for NAND flash in GPMC controller
  465. * @mtd: MTD device structure
  466. */
  467. static void omap_hwecc_init(struct mtd_info *mtd)
  468. {
  469. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  470. mtd);
  471. struct nand_chip *chip = mtd->priv;
  472. unsigned long val = 0x0;
  473. /* Read from ECC Control Register */
  474. val = __raw_readl(info->gpmc_baseaddr + GPMC_ECC_CONTROL);
  475. /* Clear all ECC | Enable Reg1 */
  476. val = ((0x00000001<<8) | 0x00000001);
  477. __raw_writel(val, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
  478. /* Read from ECC Size Config Register */
  479. val = __raw_readl(info->gpmc_baseaddr + GPMC_ECC_SIZE_CONFIG);
  480. /* ECCSIZE1=512 | Select eccResultsize[0-3] */
  481. val = ((((chip->ecc.size >> 1) - 1) << 22) | (0x0000000F));
  482. __raw_writel(val, info->gpmc_baseaddr + GPMC_ECC_SIZE_CONFIG);
  483. }
  484. /**
  485. * gen_true_ecc - This function will generate true ECC value
  486. * @ecc_buf: buffer to store ecc code
  487. *
  488. * This generated true ECC value can be used when correcting
  489. * data read from NAND flash memory core
  490. */
  491. static void gen_true_ecc(u8 *ecc_buf)
  492. {
  493. u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
  494. ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
  495. ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
  496. P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
  497. ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
  498. P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
  499. ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
  500. P1e(tmp) | P2048o(tmp) | P2048e(tmp));
  501. }
  502. /**
  503. * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
  504. * @ecc_data1: ecc code from nand spare area
  505. * @ecc_data2: ecc code from hardware register obtained from hardware ecc
  506. * @page_data: page data
  507. *
  508. * This function compares two ECC's and indicates if there is an error.
  509. * If the error can be corrected it will be corrected to the buffer.
  510. */
  511. static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
  512. u8 *ecc_data2, /* read from register */
  513. u8 *page_data)
  514. {
  515. uint i;
  516. u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
  517. u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
  518. u8 ecc_bit[24];
  519. u8 ecc_sum = 0;
  520. u8 find_bit = 0;
  521. uint find_byte = 0;
  522. int isEccFF;
  523. isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
  524. gen_true_ecc(ecc_data1);
  525. gen_true_ecc(ecc_data2);
  526. for (i = 0; i <= 2; i++) {
  527. *(ecc_data1 + i) = ~(*(ecc_data1 + i));
  528. *(ecc_data2 + i) = ~(*(ecc_data2 + i));
  529. }
  530. for (i = 0; i < 8; i++) {
  531. tmp0_bit[i] = *ecc_data1 % 2;
  532. *ecc_data1 = *ecc_data1 / 2;
  533. }
  534. for (i = 0; i < 8; i++) {
  535. tmp1_bit[i] = *(ecc_data1 + 1) % 2;
  536. *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
  537. }
  538. for (i = 0; i < 8; i++) {
  539. tmp2_bit[i] = *(ecc_data1 + 2) % 2;
  540. *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
  541. }
  542. for (i = 0; i < 8; i++) {
  543. comp0_bit[i] = *ecc_data2 % 2;
  544. *ecc_data2 = *ecc_data2 / 2;
  545. }
  546. for (i = 0; i < 8; i++) {
  547. comp1_bit[i] = *(ecc_data2 + 1) % 2;
  548. *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
  549. }
  550. for (i = 0; i < 8; i++) {
  551. comp2_bit[i] = *(ecc_data2 + 2) % 2;
  552. *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
  553. }
  554. for (i = 0; i < 6; i++)
  555. ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
  556. for (i = 0; i < 8; i++)
  557. ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
  558. for (i = 0; i < 8; i++)
  559. ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
  560. ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
  561. ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
  562. for (i = 0; i < 24; i++)
  563. ecc_sum += ecc_bit[i];
  564. switch (ecc_sum) {
  565. case 0:
  566. /* Not reached because this function is not called if
  567. * ECC values are equal
  568. */
  569. return 0;
  570. case 1:
  571. /* Uncorrectable error */
  572. DEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
  573. return -1;
  574. case 11:
  575. /* UN-Correctable error */
  576. DEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR B\n");
  577. return -1;
  578. case 12:
  579. /* Correctable error */
  580. find_byte = (ecc_bit[23] << 8) +
  581. (ecc_bit[21] << 7) +
  582. (ecc_bit[19] << 6) +
  583. (ecc_bit[17] << 5) +
  584. (ecc_bit[15] << 4) +
  585. (ecc_bit[13] << 3) +
  586. (ecc_bit[11] << 2) +
  587. (ecc_bit[9] << 1) +
  588. ecc_bit[7];
  589. find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
  590. DEBUG(MTD_DEBUG_LEVEL0, "Correcting single bit ECC error at "
  591. "offset: %d, bit: %d\n", find_byte, find_bit);
  592. page_data[find_byte] ^= (1 << find_bit);
  593. return 0;
  594. default:
  595. if (isEccFF) {
  596. if (ecc_data2[0] == 0 &&
  597. ecc_data2[1] == 0 &&
  598. ecc_data2[2] == 0)
  599. return 0;
  600. }
  601. DEBUG(MTD_DEBUG_LEVEL0, "UNCORRECTED_ERROR default\n");
  602. return -1;
  603. }
  604. }
  605. /**
  606. * omap_correct_data - Compares the ECC read with HW generated ECC
  607. * @mtd: MTD device structure
  608. * @dat: page data
  609. * @read_ecc: ecc read from nand flash
  610. * @calc_ecc: ecc read from HW ECC registers
  611. *
  612. * Compares the ecc read from nand spare area with ECC registers values
  613. * and if ECC's mismached, it will call 'omap_compare_ecc' for error detection
  614. * and correction.
  615. */
  616. static int omap_correct_data(struct mtd_info *mtd, u_char *dat,
  617. u_char *read_ecc, u_char *calc_ecc)
  618. {
  619. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  620. mtd);
  621. int blockCnt = 0, i = 0, ret = 0;
  622. /* Ex NAND_ECC_HW12_2048 */
  623. if ((info->nand.ecc.mode == NAND_ECC_HW) &&
  624. (info->nand.ecc.size == 2048))
  625. blockCnt = 4;
  626. else
  627. blockCnt = 1;
  628. for (i = 0; i < blockCnt; i++) {
  629. if (memcmp(read_ecc, calc_ecc, 3) != 0) {
  630. ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
  631. if (ret < 0)
  632. return ret;
  633. }
  634. read_ecc += 3;
  635. calc_ecc += 3;
  636. dat += 512;
  637. }
  638. return 0;
  639. }
  640. /**
  641. * omap_calcuate_ecc - Generate non-inverted ECC bytes.
  642. * @mtd: MTD device structure
  643. * @dat: The pointer to data on which ecc is computed
  644. * @ecc_code: The ecc_code buffer
  645. *
  646. * Using noninverted ECC can be considered ugly since writing a blank
  647. * page ie. padding will clear the ECC bytes. This is no problem as long
  648. * nobody is trying to write data on the seemingly unused page. Reading
  649. * an erased page will produce an ECC mismatch between generated and read
  650. * ECC bytes that has to be dealt with separately.
  651. */
  652. static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  653. u_char *ecc_code)
  654. {
  655. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  656. mtd);
  657. unsigned long val = 0x0;
  658. unsigned long reg;
  659. /* Start Reading from HW ECC1_Result = 0x200 */
  660. reg = (unsigned long)(info->gpmc_baseaddr + GPMC_ECC1_RESULT);
  661. val = __raw_readl(reg);
  662. *ecc_code++ = val; /* P128e, ..., P1e */
  663. *ecc_code++ = val >> 16; /* P128o, ..., P1o */
  664. /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
  665. *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
  666. reg += 4;
  667. return 0;
  668. }
  669. /**
  670. * omap_enable_hwecc - This function enables the hardware ecc functionality
  671. * @mtd: MTD device structure
  672. * @mode: Read/Write mode
  673. */
  674. static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
  675. {
  676. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  677. mtd);
  678. struct nand_chip *chip = mtd->priv;
  679. unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
  680. unsigned long val = __raw_readl(info->gpmc_baseaddr + GPMC_ECC_CONFIG);
  681. switch (mode) {
  682. case NAND_ECC_READ:
  683. __raw_writel(0x101, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
  684. /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
  685. val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
  686. break;
  687. case NAND_ECC_READSYN:
  688. __raw_writel(0x100, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
  689. /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
  690. val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
  691. break;
  692. case NAND_ECC_WRITE:
  693. __raw_writel(0x101, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
  694. /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
  695. val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
  696. break;
  697. default:
  698. DEBUG(MTD_DEBUG_LEVEL0, "Error: Unrecognized Mode[%d]!\n",
  699. mode);
  700. break;
  701. }
  702. __raw_writel(val, info->gpmc_baseaddr + GPMC_ECC_CONFIG);
  703. }
  704. #endif
  705. /**
  706. * omap_wait - wait until the command is done
  707. * @mtd: MTD device structure
  708. * @chip: NAND Chip structure
  709. *
  710. * Wait function is called during Program and erase operations and
  711. * the way it is called from MTD layer, we should wait till the NAND
  712. * chip is ready after the programming/erase operation has completed.
  713. *
  714. * Erase can take up to 400ms and program up to 20ms according to
  715. * general NAND and SmartMedia specs
  716. */
  717. static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
  718. {
  719. struct nand_chip *this = mtd->priv;
  720. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  721. mtd);
  722. unsigned long timeo = jiffies;
  723. int status = NAND_STATUS_FAIL, state = this->state;
  724. if (state == FL_ERASING)
  725. timeo += (HZ * 400) / 1000;
  726. else
  727. timeo += (HZ * 20) / 1000;
  728. this->IO_ADDR_W = (void *) info->gpmc_cs_baseaddr +
  729. GPMC_CS_NAND_COMMAND;
  730. this->IO_ADDR_R = (void *) info->gpmc_cs_baseaddr + GPMC_CS_NAND_DATA;
  731. __raw_writeb(NAND_CMD_STATUS & 0xFF, this->IO_ADDR_W);
  732. while (time_before(jiffies, timeo)) {
  733. status = __raw_readb(this->IO_ADDR_R);
  734. if (status & NAND_STATUS_READY)
  735. break;
  736. cond_resched();
  737. }
  738. return status;
  739. }
  740. /**
  741. * omap_dev_ready - calls the platform specific dev_ready function
  742. * @mtd: MTD device structure
  743. */
  744. static int omap_dev_ready(struct mtd_info *mtd)
  745. {
  746. struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
  747. mtd);
  748. unsigned int val = __raw_readl(info->gpmc_baseaddr + GPMC_IRQ_STATUS);
  749. if ((val & 0x100) == 0x100) {
  750. /* Clear IRQ Interrupt */
  751. val |= 0x100;
  752. val &= ~(0x0);
  753. __raw_writel(val, info->gpmc_baseaddr + GPMC_IRQ_STATUS);
  754. } else {
  755. unsigned int cnt = 0;
  756. while (cnt++ < 0x1FF) {
  757. if ((val & 0x100) == 0x100)
  758. return 0;
  759. val = __raw_readl(info->gpmc_baseaddr +
  760. GPMC_IRQ_STATUS);
  761. }
  762. }
  763. return 1;
  764. }
  765. static int __devinit omap_nand_probe(struct platform_device *pdev)
  766. {
  767. struct omap_nand_info *info;
  768. struct omap_nand_platform_data *pdata;
  769. int err;
  770. pdata = pdev->dev.platform_data;
  771. if (pdata == NULL) {
  772. dev_err(&pdev->dev, "platform data missing\n");
  773. return -ENODEV;
  774. }
  775. info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL);
  776. if (!info)
  777. return -ENOMEM;
  778. platform_set_drvdata(pdev, info);
  779. spin_lock_init(&info->controller.lock);
  780. init_waitqueue_head(&info->controller.wq);
  781. info->pdev = pdev;
  782. info->gpmc_cs = pdata->cs;
  783. info->gpmc_baseaddr = pdata->gpmc_baseaddr;
  784. info->gpmc_cs_baseaddr = pdata->gpmc_cs_baseaddr;
  785. info->phys_base = pdata->phys_base;
  786. info->mtd.priv = &info->nand;
  787. info->mtd.name = dev_name(&pdev->dev);
  788. info->mtd.owner = THIS_MODULE;
  789. info->nand.options |= pdata->devsize ? NAND_BUSWIDTH_16 : 0;
  790. info->nand.options |= NAND_SKIP_BBTSCAN;
  791. /* NAND write protect off */
  792. omap_nand_wp(&info->mtd, NAND_WP_OFF);
  793. if (!request_mem_region(info->phys_base, NAND_IO_SIZE,
  794. pdev->dev.driver->name)) {
  795. err = -EBUSY;
  796. goto out_free_info;
  797. }
  798. info->nand.IO_ADDR_R = ioremap(info->phys_base, NAND_IO_SIZE);
  799. if (!info->nand.IO_ADDR_R) {
  800. err = -ENOMEM;
  801. goto out_release_mem_region;
  802. }
  803. info->nand.controller = &info->controller;
  804. info->nand.IO_ADDR_W = info->nand.IO_ADDR_R;
  805. info->nand.cmd_ctrl = omap_hwcontrol;
  806. /*
  807. * If RDY/BSY line is connected to OMAP then use the omap ready
  808. * funcrtion and the generic nand_wait function which reads the status
  809. * register after monitoring the RDY/BSY line.Otherwise use a standard
  810. * chip delay which is slightly more than tR (AC Timing) of the NAND
  811. * device and read status register until you get a failure or success
  812. */
  813. if (pdata->dev_ready) {
  814. info->nand.dev_ready = omap_dev_ready;
  815. info->nand.chip_delay = 0;
  816. } else {
  817. info->nand.waitfunc = omap_wait;
  818. info->nand.chip_delay = 50;
  819. }
  820. if (use_prefetch) {
  821. /* copy the virtual address of nand base for fifo access */
  822. info->nand_pref_fifo_add = info->nand.IO_ADDR_R;
  823. info->nand.read_buf = omap_read_buf_pref;
  824. info->nand.write_buf = omap_write_buf_pref;
  825. if (use_dma) {
  826. err = omap_request_dma(OMAP24XX_DMA_GPMC, "NAND",
  827. omap_nand_dma_cb, &info->comp, &info->dma_ch);
  828. if (err < 0) {
  829. info->dma_ch = -1;
  830. printk(KERN_WARNING "DMA request failed."
  831. " Non-dma data transfer mode\n");
  832. } else {
  833. omap_set_dma_dest_burst_mode(info->dma_ch,
  834. OMAP_DMA_DATA_BURST_16);
  835. omap_set_dma_src_burst_mode(info->dma_ch,
  836. OMAP_DMA_DATA_BURST_16);
  837. info->nand.read_buf = omap_read_buf_dma_pref;
  838. info->nand.write_buf = omap_write_buf_dma_pref;
  839. }
  840. }
  841. } else {
  842. if (info->nand.options & NAND_BUSWIDTH_16) {
  843. info->nand.read_buf = omap_read_buf16;
  844. info->nand.write_buf = omap_write_buf16;
  845. } else {
  846. info->nand.read_buf = omap_read_buf8;
  847. info->nand.write_buf = omap_write_buf8;
  848. }
  849. }
  850. info->nand.verify_buf = omap_verify_buf;
  851. #ifdef CONFIG_MTD_NAND_OMAP_HWECC
  852. info->nand.ecc.bytes = 3;
  853. info->nand.ecc.size = 512;
  854. info->nand.ecc.calculate = omap_calculate_ecc;
  855. info->nand.ecc.hwctl = omap_enable_hwecc;
  856. info->nand.ecc.correct = omap_correct_data;
  857. info->nand.ecc.mode = NAND_ECC_HW;
  858. /* init HW ECC */
  859. omap_hwecc_init(&info->mtd);
  860. #else
  861. info->nand.ecc.mode = NAND_ECC_SOFT;
  862. #endif
  863. /* DIP switches on some boards change between 8 and 16 bit
  864. * bus widths for flash. Try the other width if the first try fails.
  865. */
  866. if (nand_scan(&info->mtd, 1)) {
  867. info->nand.options ^= NAND_BUSWIDTH_16;
  868. if (nand_scan(&info->mtd, 1)) {
  869. err = -ENXIO;
  870. goto out_release_mem_region;
  871. }
  872. }
  873. #ifdef CONFIG_MTD_PARTITIONS
  874. err = parse_mtd_partitions(&info->mtd, part_probes, &info->parts, 0);
  875. if (err > 0)
  876. add_mtd_partitions(&info->mtd, info->parts, err);
  877. else if (pdata->parts)
  878. add_mtd_partitions(&info->mtd, pdata->parts, pdata->nr_parts);
  879. else
  880. #endif
  881. add_mtd_device(&info->mtd);
  882. platform_set_drvdata(pdev, &info->mtd);
  883. return 0;
  884. out_release_mem_region:
  885. release_mem_region(info->phys_base, NAND_IO_SIZE);
  886. out_free_info:
  887. kfree(info);
  888. return err;
  889. }
  890. static int omap_nand_remove(struct platform_device *pdev)
  891. {
  892. struct mtd_info *mtd = platform_get_drvdata(pdev);
  893. struct omap_nand_info *info = mtd->priv;
  894. platform_set_drvdata(pdev, NULL);
  895. if (use_dma)
  896. omap_free_dma(info->dma_ch);
  897. /* Release NAND device, its internal structures and partitions */
  898. nand_release(&info->mtd);
  899. iounmap(info->nand_pref_fifo_add);
  900. kfree(&info->mtd);
  901. return 0;
  902. }
  903. static struct platform_driver omap_nand_driver = {
  904. .probe = omap_nand_probe,
  905. .remove = omap_nand_remove,
  906. .driver = {
  907. .name = DRIVER_NAME,
  908. .owner = THIS_MODULE,
  909. },
  910. };
  911. static int __init omap_nand_init(void)
  912. {
  913. printk(KERN_INFO "%s driver initializing\n", DRIVER_NAME);
  914. /* This check is required if driver is being
  915. * loaded run time as a module
  916. */
  917. if ((1 == use_dma) && (0 == use_prefetch)) {
  918. printk(KERN_INFO"Wrong parameters: 'use_dma' can not be 1 "
  919. "without use_prefetch'. Prefetch will not be"
  920. " used in either mode (mpu or dma)\n");
  921. }
  922. return platform_driver_register(&omap_nand_driver);
  923. }
  924. static void __exit omap_nand_exit(void)
  925. {
  926. platform_driver_unregister(&omap_nand_driver);
  927. }
  928. module_init(omap_nand_init);
  929. module_exit(omap_nand_exit);
  930. MODULE_ALIAS(DRIVER_NAME);
  931. MODULE_LICENSE("GPL");
  932. MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");