omap2.c 30 KB

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