omap2.c 30 KB

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