tx4938ndfmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * drivers/mtd/nand/tx4938ndfmc.c
  3. *
  4. * Overview:
  5. * This is a device driver for the NAND flash device connected to
  6. * TX4938 internal NAND Memory Controller.
  7. * TX4938 NDFMC is almost same as TX4925 NDFMC, but register size are 64 bit.
  8. *
  9. * Author: source@mvista.com
  10. *
  11. * Based on spia.c by Steven J. Hill
  12. *
  13. * $Id: tx4938ndfmc.c,v 1.4 2004/10/05 13:50:20 gleixner Exp $
  14. *
  15. * Copyright (C) 2000-2001 Toshiba Corporation
  16. *
  17. * 2003 (c) MontaVista Software, Inc. This file is licensed under the
  18. * terms of the GNU General Public License version 2. This program is
  19. * licensed "as is" without any warranty of any kind, whether express
  20. * or implied.
  21. */
  22. #include <linux/config.h>
  23. #include <linux/slab.h>
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/mtd/nand.h>
  28. #include <linux/mtd/nand_ecc.h>
  29. #include <linux/mtd/partitions.h>
  30. #include <asm/io.h>
  31. #include <asm/bootinfo.h>
  32. #include <linux/delay.h>
  33. #include <asm/tx4938/rbtx4938.h>
  34. extern struct nand_oobinfo jffs2_oobinfo;
  35. /*
  36. * MTD structure for TX4938 NDFMC
  37. */
  38. static struct mtd_info *tx4938ndfmc_mtd;
  39. /*
  40. * Define partitions for flash device
  41. */
  42. #define flush_wb() (void)tx4938_ndfmcptr->mcr;
  43. #define NUM_PARTITIONS 3
  44. #define NUMBER_OF_CIS_BLOCKS 24
  45. #define SIZE_OF_BLOCK 0x00004000
  46. #define NUMBER_OF_BLOCK_PER_ZONE 1024
  47. #define SIZE_OF_ZONE (NUMBER_OF_BLOCK_PER_ZONE * SIZE_OF_BLOCK)
  48. #ifndef CONFIG_MTD_CMDLINE_PARTS
  49. /*
  50. * You can use the following sample of MTD partitions
  51. * on the NAND Flash Memory 32MB or more.
  52. *
  53. * The following figure shows the image of the sample partition on
  54. * the 32MB NAND Flash Memory.
  55. *
  56. * Block No.
  57. * 0 +-----------------------------+ ------
  58. * | CIS | ^
  59. * 24 +-----------------------------+ |
  60. * | kernel image | | Zone 0
  61. * | | |
  62. * +-----------------------------+ |
  63. * 1023 | unused area | v
  64. * +-----------------------------+ ------
  65. * 1024 | JFFS2 | ^
  66. * | | |
  67. * | | | Zone 1
  68. * | | |
  69. * | | |
  70. * | | v
  71. * 2047 +-----------------------------+ ------
  72. *
  73. */
  74. static struct mtd_partition partition_info[NUM_PARTITIONS] = {
  75. {
  76. .name = "RBTX4938 CIS Area",
  77. .offset = 0,
  78. .size = (NUMBER_OF_CIS_BLOCKS * SIZE_OF_BLOCK),
  79. .mask_flags = MTD_WRITEABLE /* This partition is NOT writable */
  80. },
  81. {
  82. .name = "RBTX4938 kernel image",
  83. .offset = MTDPART_OFS_APPEND,
  84. .size = 8 * 0x00100000, /* 8MB (Depends on size of kernel image) */
  85. .mask_flags = MTD_WRITEABLE /* This partition is NOT writable */
  86. },
  87. {
  88. .name = "Root FS (JFFS2)",
  89. .offset = (0 + SIZE_OF_ZONE), /* start address of next zone */
  90. .size = MTDPART_SIZ_FULL
  91. },
  92. };
  93. #endif
  94. static void tx4938ndfmc_hwcontrol(struct mtd_info *mtd, int cmd)
  95. {
  96. switch (cmd) {
  97. case NAND_CTL_SETCLE:
  98. tx4938_ndfmcptr->mcr |= TX4938_NDFMCR_CLE;
  99. break;
  100. case NAND_CTL_CLRCLE:
  101. tx4938_ndfmcptr->mcr &= ~TX4938_NDFMCR_CLE;
  102. break;
  103. case NAND_CTL_SETALE:
  104. tx4938_ndfmcptr->mcr |= TX4938_NDFMCR_ALE;
  105. break;
  106. case NAND_CTL_CLRALE:
  107. tx4938_ndfmcptr->mcr &= ~TX4938_NDFMCR_ALE;
  108. break;
  109. /* TX4938_NDFMCR_CE bit is 0:high 1:low */
  110. case NAND_CTL_SETNCE:
  111. tx4938_ndfmcptr->mcr |= TX4938_NDFMCR_CE;
  112. break;
  113. case NAND_CTL_CLRNCE:
  114. tx4938_ndfmcptr->mcr &= ~TX4938_NDFMCR_CE;
  115. break;
  116. case NAND_CTL_SETWP:
  117. tx4938_ndfmcptr->mcr |= TX4938_NDFMCR_WE;
  118. break;
  119. case NAND_CTL_CLRWP:
  120. tx4938_ndfmcptr->mcr &= ~TX4938_NDFMCR_WE;
  121. break;
  122. }
  123. }
  124. static int tx4938ndfmc_dev_ready(struct mtd_info *mtd)
  125. {
  126. flush_wb();
  127. return !(tx4938_ndfmcptr->sr & TX4938_NDFSR_BUSY);
  128. }
  129. static void tx4938ndfmc_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  130. {
  131. u32 mcr = tx4938_ndfmcptr->mcr;
  132. mcr &= ~TX4938_NDFMCR_ECC_ALL;
  133. tx4938_ndfmcptr->mcr = mcr | TX4938_NDFMCR_ECC_OFF;
  134. tx4938_ndfmcptr->mcr = mcr | TX4938_NDFMCR_ECC_READ;
  135. ecc_code[1] = tx4938_ndfmcptr->dtr;
  136. ecc_code[0] = tx4938_ndfmcptr->dtr;
  137. ecc_code[2] = tx4938_ndfmcptr->dtr;
  138. tx4938_ndfmcptr->mcr = mcr | TX4938_NDFMCR_ECC_OFF;
  139. }
  140. static void tx4938ndfmc_enable_hwecc(struct mtd_info *mtd, int mode)
  141. {
  142. u32 mcr = tx4938_ndfmcptr->mcr;
  143. mcr &= ~TX4938_NDFMCR_ECC_ALL;
  144. tx4938_ndfmcptr->mcr = mcr | TX4938_NDFMCR_ECC_RESET;
  145. tx4938_ndfmcptr->mcr = mcr | TX4938_NDFMCR_ECC_OFF;
  146. tx4938_ndfmcptr->mcr = mcr | TX4938_NDFMCR_ECC_ON;
  147. }
  148. static u_char tx4938ndfmc_nand_read_byte(struct mtd_info *mtd)
  149. {
  150. struct nand_chip *this = mtd->priv;
  151. return tx4938_read_nfmc(this->IO_ADDR_R);
  152. }
  153. static void tx4938ndfmc_nand_write_byte(struct mtd_info *mtd, u_char byte)
  154. {
  155. struct nand_chip *this = mtd->priv;
  156. tx4938_write_nfmc(byte, this->IO_ADDR_W);
  157. }
  158. static void tx4938ndfmc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  159. {
  160. int i;
  161. struct nand_chip *this = mtd->priv;
  162. for (i=0; i<len; i++)
  163. tx4938_write_nfmc(buf[i], this->IO_ADDR_W);
  164. }
  165. static void tx4938ndfmc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  166. {
  167. int i;
  168. struct nand_chip *this = mtd->priv;
  169. for (i=0; i<len; i++)
  170. buf[i] = tx4938_read_nfmc(this->IO_ADDR_R);
  171. }
  172. static int tx4938ndfmc_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  173. {
  174. int i;
  175. struct nand_chip *this = mtd->priv;
  176. for (i=0; i<len; i++)
  177. if (buf[i] != tx4938_read_nfmc(this->IO_ADDR_R))
  178. return -EFAULT;
  179. return 0;
  180. }
  181. /*
  182. * Send command to NAND device
  183. */
  184. static void tx4938ndfmc_nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
  185. {
  186. register struct nand_chip *this = mtd->priv;
  187. /* Begin command latch cycle */
  188. this->hwcontrol(mtd, NAND_CTL_SETCLE);
  189. /*
  190. * Write out the command to the device.
  191. */
  192. if (command == NAND_CMD_SEQIN) {
  193. int readcmd;
  194. if (column >= mtd->oobblock) {
  195. /* OOB area */
  196. column -= mtd->oobblock;
  197. readcmd = NAND_CMD_READOOB;
  198. } else if (column < 256) {
  199. /* First 256 bytes --> READ0 */
  200. readcmd = NAND_CMD_READ0;
  201. } else {
  202. column -= 256;
  203. readcmd = NAND_CMD_READ1;
  204. }
  205. this->write_byte(mtd, readcmd);
  206. }
  207. this->write_byte(mtd, command);
  208. /* Set ALE and clear CLE to start address cycle */
  209. this->hwcontrol(mtd, NAND_CTL_CLRCLE);
  210. if (column != -1 || page_addr != -1) {
  211. this->hwcontrol(mtd, NAND_CTL_SETALE);
  212. /* Serially input address */
  213. if (column != -1)
  214. this->write_byte(mtd, column);
  215. if (page_addr != -1) {
  216. this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
  217. this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
  218. /* One more address cycle for higher density devices */
  219. if (mtd->size & 0x0c000000)
  220. this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));
  221. }
  222. /* Latch in address */
  223. this->hwcontrol(mtd, NAND_CTL_CLRALE);
  224. }
  225. /*
  226. * program and erase have their own busy handlers
  227. * status and sequential in needs no delay
  228. */
  229. switch (command) {
  230. case NAND_CMD_PAGEPROG:
  231. /* Turn off WE */
  232. this->hwcontrol (mtd, NAND_CTL_CLRWP);
  233. return;
  234. case NAND_CMD_SEQIN:
  235. /* Turn on WE */
  236. this->hwcontrol (mtd, NAND_CTL_SETWP);
  237. return;
  238. case NAND_CMD_ERASE1:
  239. case NAND_CMD_ERASE2:
  240. case NAND_CMD_STATUS:
  241. return;
  242. case NAND_CMD_RESET:
  243. if (this->dev_ready)
  244. break;
  245. this->hwcontrol(mtd, NAND_CTL_SETCLE);
  246. this->write_byte(mtd, NAND_CMD_STATUS);
  247. this->hwcontrol(mtd, NAND_CTL_CLRCLE);
  248. while ( !(this->read_byte(mtd) & 0x40));
  249. return;
  250. /* This applies to read commands */
  251. default:
  252. /*
  253. * If we don't have access to the busy pin, we apply the given
  254. * command delay
  255. */
  256. if (!this->dev_ready) {
  257. udelay (this->chip_delay);
  258. return;
  259. }
  260. }
  261. /* wait until command is processed */
  262. while (!this->dev_ready(mtd));
  263. }
  264. #ifdef CONFIG_MTD_CMDLINE_PARTS
  265. extern int parse_cmdline_partitions(struct mtd_info *master, struct mtd_partition **pparts, char *);
  266. #endif
  267. /*
  268. * Main initialization routine
  269. */
  270. int __init tx4938ndfmc_init (void)
  271. {
  272. struct nand_chip *this;
  273. int bsprt = 0, hold = 0xf, spw = 0xf;
  274. int protected = 0;
  275. if ((*rbtx4938_piosel_ptr & 0x0c) != 0x08) {
  276. printk("TX4938 NDFMC: disabled by IOC PIOSEL\n");
  277. return -ENODEV;
  278. }
  279. bsprt = 1;
  280. hold = 2;
  281. spw = 9 - 1; /* 8 GBUSCLK = 80ns (@ GBUSCLK 100MHz) */
  282. if ((tx4938_ccfgptr->pcfg &
  283. (TX4938_PCFG_ATA_SEL|TX4938_PCFG_ISA_SEL|TX4938_PCFG_NDF_SEL))
  284. != TX4938_PCFG_NDF_SEL) {
  285. printk("TX4938 NDFMC: disabled by PCFG.\n");
  286. return -ENODEV;
  287. }
  288. /* reset NDFMC */
  289. tx4938_ndfmcptr->rstr |= TX4938_NDFRSTR_RST;
  290. while (tx4938_ndfmcptr->rstr & TX4938_NDFRSTR_RST)
  291. ;
  292. /* setup BusSeparete, Hold Time, Strobe Pulse Width */
  293. tx4938_ndfmcptr->mcr = bsprt ? TX4938_NDFMCR_BSPRT : 0;
  294. tx4938_ndfmcptr->spr = hold << 4 | spw;
  295. /* Allocate memory for MTD device structure and private data */
  296. tx4938ndfmc_mtd = kmalloc (sizeof(struct mtd_info) + sizeof (struct nand_chip),
  297. GFP_KERNEL);
  298. if (!tx4938ndfmc_mtd) {
  299. printk ("Unable to allocate TX4938 NDFMC MTD device structure.\n");
  300. return -ENOMEM;
  301. }
  302. /* Get pointer to private data */
  303. this = (struct nand_chip *) (&tx4938ndfmc_mtd[1]);
  304. /* Initialize structures */
  305. memset((char *) tx4938ndfmc_mtd, 0, sizeof(struct mtd_info));
  306. memset((char *) this, 0, sizeof(struct nand_chip));
  307. /* Link the private data with the MTD structure */
  308. tx4938ndfmc_mtd->priv = this;
  309. /* Set address of NAND IO lines */
  310. this->IO_ADDR_R = (unsigned long)&tx4938_ndfmcptr->dtr;
  311. this->IO_ADDR_W = (unsigned long)&tx4938_ndfmcptr->dtr;
  312. this->hwcontrol = tx4938ndfmc_hwcontrol;
  313. this->dev_ready = tx4938ndfmc_dev_ready;
  314. this->calculate_ecc = tx4938ndfmc_calculate_ecc;
  315. this->correct_data = nand_correct_data;
  316. this->enable_hwecc = tx4938ndfmc_enable_hwecc;
  317. this->eccmode = NAND_ECC_HW3_256;
  318. this->chip_delay = 100;
  319. this->read_byte = tx4938ndfmc_nand_read_byte;
  320. this->write_byte = tx4938ndfmc_nand_write_byte;
  321. this->cmdfunc = tx4938ndfmc_nand_command;
  322. this->write_buf = tx4938ndfmc_nand_write_buf;
  323. this->read_buf = tx4938ndfmc_nand_read_buf;
  324. this->verify_buf = tx4938ndfmc_nand_verify_buf;
  325. /* Scan to find existance of the device */
  326. if (nand_scan (tx4938ndfmc_mtd, 1)) {
  327. kfree (tx4938ndfmc_mtd);
  328. return -ENXIO;
  329. }
  330. if (protected) {
  331. printk(KERN_INFO "TX4938 NDFMC: write protected.\n");
  332. tx4938ndfmc_mtd->flags &= ~(MTD_WRITEABLE | MTD_ERASEABLE);
  333. }
  334. #ifdef CONFIG_MTD_CMDLINE_PARTS
  335. {
  336. int mtd_parts_nb = 0;
  337. struct mtd_partition *mtd_parts = 0;
  338. mtd_parts_nb = parse_cmdline_partitions(tx4938ndfmc_mtd, &mtd_parts, "tx4938ndfmc");
  339. if (mtd_parts_nb > 0)
  340. add_mtd_partitions(tx4938ndfmc_mtd, mtd_parts, mtd_parts_nb);
  341. else
  342. add_mtd_device(tx4938ndfmc_mtd);
  343. }
  344. #else
  345. add_mtd_partitions(tx4938ndfmc_mtd, partition_info, NUM_PARTITIONS );
  346. #endif
  347. return 0;
  348. }
  349. module_init(tx4938ndfmc_init);
  350. /*
  351. * Clean up routine
  352. */
  353. static void __exit tx4938ndfmc_cleanup (void)
  354. {
  355. /* Release resources, unregister device */
  356. nand_release (tx4938ndfmc_mtd);
  357. /* Free the MTD device structure */
  358. kfree (tx4938ndfmc_mtd);
  359. }
  360. module_exit(tx4938ndfmc_cleanup);
  361. MODULE_LICENSE("GPL");
  362. MODULE_AUTHOR("Alice Hennessy <ahennessy@mvista.com>");
  363. MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on TX4938 NDFMC");