omap2.c 21 KB

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