omap2.c 25 KB

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