omap2.c 29 KB

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