omap2.c 21 KB

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