omap2.c 29 KB

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