tx4925ndfmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * drivers/mtd/tx4925ndfmc.c
  3. *
  4. * Overview:
  5. * This is a device driver for the NAND flash device found on the
  6. * Toshiba RBTX4925 reference board, which is a SmartMediaCard. It supports
  7. * 16MiB, 32MiB and 64MiB cards.
  8. *
  9. * Author: MontaVista Software, Inc. source@mvista.com
  10. *
  11. * Derived from drivers/mtd/autcpu12.c
  12. * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
  13. *
  14. * $Id: tx4925ndfmc.c,v 1.5 2004/10/05 13:50:20 gleixner Exp $
  15. *
  16. * Copyright (C) 2001 Toshiba Corporation
  17. *
  18. * 2003 (c) MontaVista Software, Inc. This file is licensed under
  19. * the terms of the GNU General Public License version 2. This program
  20. * is licensed "as is" without any warranty of any kind, whether express
  21. * or implied.
  22. *
  23. */
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/mtd/mtd.h>
  28. #include <linux/mtd/nand.h>
  29. #include <linux/mtd/partitions.h>
  30. #include <linux/delay.h>
  31. #include <asm/io.h>
  32. #include <asm/tx4925/tx4925_nand.h>
  33. extern struct nand_oobinfo jffs2_oobinfo;
  34. /*
  35. * MTD structure for RBTX4925 board
  36. */
  37. static struct mtd_info *tx4925ndfmc_mtd = NULL;
  38. /*
  39. * Define partitions for flash devices
  40. */
  41. static struct mtd_partition partition_info16k[] = {
  42. { .name = "RBTX4925 flash partition 1",
  43. .offset = 0,
  44. .size = 8 * 0x00100000 },
  45. { .name = "RBTX4925 flash partition 2",
  46. .offset = 8 * 0x00100000,
  47. .size = 8 * 0x00100000 },
  48. };
  49. static struct mtd_partition partition_info32k[] = {
  50. { .name = "RBTX4925 flash partition 1",
  51. .offset = 0,
  52. .size = 8 * 0x00100000 },
  53. { .name = "RBTX4925 flash partition 2",
  54. .offset = 8 * 0x00100000,
  55. .size = 24 * 0x00100000 },
  56. };
  57. static struct mtd_partition partition_info64k[] = {
  58. { .name = "User FS",
  59. .offset = 0,
  60. .size = 16 * 0x00100000 },
  61. { .name = "RBTX4925 flash partition 2",
  62. .offset = 16 * 0x00100000,
  63. .size = 48 * 0x00100000},
  64. };
  65. static struct mtd_partition partition_info128k[] = {
  66. { .name = "Skip bad section",
  67. .offset = 0,
  68. .size = 16 * 0x00100000 },
  69. { .name = "User FS",
  70. .offset = 16 * 0x00100000,
  71. .size = 112 * 0x00100000 },
  72. };
  73. #define NUM_PARTITIONS16K 2
  74. #define NUM_PARTITIONS32K 2
  75. #define NUM_PARTITIONS64K 2
  76. #define NUM_PARTITIONS128K 2
  77. /*
  78. * hardware specific access to control-lines
  79. */
  80. static void tx4925ndfmc_hwcontrol(struct mtd_info *mtd, int cmd)
  81. {
  82. switch(cmd){
  83. case NAND_CTL_SETCLE:
  84. tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_CLE;
  85. break;
  86. case NAND_CTL_CLRCLE:
  87. tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_CLE;
  88. break;
  89. case NAND_CTL_SETALE:
  90. tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_ALE;
  91. break;
  92. case NAND_CTL_CLRALE:
  93. tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_ALE;
  94. break;
  95. case NAND_CTL_SETNCE:
  96. tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_CE;
  97. break;
  98. case NAND_CTL_CLRNCE:
  99. tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_CE;
  100. break;
  101. case NAND_CTL_SETWP:
  102. tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_WE;
  103. break;
  104. case NAND_CTL_CLRWP:
  105. tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_WE;
  106. break;
  107. }
  108. }
  109. /*
  110. * read device ready pin
  111. */
  112. static int tx4925ndfmc_device_ready(struct mtd_info *mtd)
  113. {
  114. int ready;
  115. ready = (tx4925_ndfmcptr->sr & TX4925_NDSFR_BUSY) ? 0 : 1;
  116. return ready;
  117. }
  118. void tx4925ndfmc_enable_hwecc(struct mtd_info *mtd, int mode)
  119. {
  120. /* reset first */
  121. tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_ECC_CNTL_MASK;
  122. tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_ECC_CNTL_MASK;
  123. tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_ECC_CNTL_ENAB;
  124. }
  125. static void tx4925ndfmc_disable_ecc(void)
  126. {
  127. tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_ECC_CNTL_MASK;
  128. }
  129. static void tx4925ndfmc_enable_read_ecc(void)
  130. {
  131. tx4925_ndfmcptr->mcr &= ~TX4925_NDFMCR_ECC_CNTL_MASK;
  132. tx4925_ndfmcptr->mcr |= TX4925_NDFMCR_ECC_CNTL_READ;
  133. }
  134. void tx4925ndfmc_readecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code){
  135. int i;
  136. u_char *ecc = ecc_code;
  137. tx4925ndfmc_enable_read_ecc();
  138. for (i = 0;i < 6;i++,ecc++)
  139. *ecc = tx4925_read_nfmc(&(tx4925_ndfmcptr->dtr));
  140. tx4925ndfmc_disable_ecc();
  141. }
  142. void tx4925ndfmc_device_setup(void)
  143. {
  144. *(unsigned char *)0xbb005000 &= ~0x08;
  145. /* reset NDFMC */
  146. tx4925_ndfmcptr->rstr |= TX4925_NDFRSTR_RST;
  147. while (tx4925_ndfmcptr->rstr & TX4925_NDFRSTR_RST);
  148. /* setup BusSeparete, Hold Time, Strobe Pulse Width */
  149. tx4925_ndfmcptr->mcr = TX4925_BSPRT ? TX4925_NDFMCR_BSPRT : 0;
  150. tx4925_ndfmcptr->spr = TX4925_HOLD << 4 | TX4925_SPW;
  151. }
  152. static u_char tx4925ndfmc_nand_read_byte(struct mtd_info *mtd)
  153. {
  154. struct nand_chip *this = mtd->priv;
  155. return tx4925_read_nfmc(this->IO_ADDR_R);
  156. }
  157. static void tx4925ndfmc_nand_write_byte(struct mtd_info *mtd, u_char byte)
  158. {
  159. struct nand_chip *this = mtd->priv;
  160. tx4925_write_nfmc(byte, this->IO_ADDR_W);
  161. }
  162. static void tx4925ndfmc_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  163. {
  164. int i;
  165. struct nand_chip *this = mtd->priv;
  166. for (i=0; i<len; i++)
  167. tx4925_write_nfmc(buf[i], this->IO_ADDR_W);
  168. }
  169. static void tx4925ndfmc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  170. {
  171. int i;
  172. struct nand_chip *this = mtd->priv;
  173. for (i=0; i<len; i++)
  174. buf[i] = tx4925_read_nfmc(this->IO_ADDR_R);
  175. }
  176. static int tx4925ndfmc_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  177. {
  178. int i;
  179. struct nand_chip *this = mtd->priv;
  180. for (i=0; i<len; i++)
  181. if (buf[i] != tx4925_read_nfmc(this->IO_ADDR_R))
  182. return -EFAULT;
  183. return 0;
  184. }
  185. /*
  186. * Send command to NAND device
  187. */
  188. static void tx4925ndfmc_nand_command (struct mtd_info *mtd, unsigned command, int column, int page_addr)
  189. {
  190. register struct nand_chip *this = mtd->priv;
  191. /* Begin command latch cycle */
  192. this->hwcontrol(mtd, NAND_CTL_SETCLE);
  193. /*
  194. * Write out the command to the device.
  195. */
  196. if (command == NAND_CMD_SEQIN) {
  197. int readcmd;
  198. if (column >= mtd->oobblock) {
  199. /* OOB area */
  200. column -= mtd->oobblock;
  201. readcmd = NAND_CMD_READOOB;
  202. } else if (column < 256) {
  203. /* First 256 bytes --> READ0 */
  204. readcmd = NAND_CMD_READ0;
  205. } else {
  206. column -= 256;
  207. readcmd = NAND_CMD_READ1;
  208. }
  209. this->write_byte(mtd, readcmd);
  210. }
  211. this->write_byte(mtd, command);
  212. /* Set ALE and clear CLE to start address cycle */
  213. this->hwcontrol(mtd, NAND_CTL_CLRCLE);
  214. if (column != -1 || page_addr != -1) {
  215. this->hwcontrol(mtd, NAND_CTL_SETALE);
  216. /* Serially input address */
  217. if (column != -1)
  218. this->write_byte(mtd, column);
  219. if (page_addr != -1) {
  220. this->write_byte(mtd, (unsigned char) (page_addr & 0xff));
  221. this->write_byte(mtd, (unsigned char) ((page_addr >> 8) & 0xff));
  222. /* One more address cycle for higher density devices */
  223. if (mtd->size & 0x0c000000)
  224. this->write_byte(mtd, (unsigned char) ((page_addr >> 16) & 0x0f));
  225. }
  226. /* Latch in address */
  227. this->hwcontrol(mtd, NAND_CTL_CLRALE);
  228. }
  229. /*
  230. * program and erase have their own busy handlers
  231. * status and sequential in needs no delay
  232. */
  233. switch (command) {
  234. case NAND_CMD_PAGEPROG:
  235. /* Turn off WE */
  236. this->hwcontrol (mtd, NAND_CTL_CLRWP);
  237. return;
  238. case NAND_CMD_SEQIN:
  239. /* Turn on WE */
  240. this->hwcontrol (mtd, NAND_CTL_SETWP);
  241. return;
  242. case NAND_CMD_ERASE1:
  243. case NAND_CMD_ERASE2:
  244. case NAND_CMD_STATUS:
  245. return;
  246. case NAND_CMD_RESET:
  247. if (this->dev_ready)
  248. break;
  249. this->hwcontrol(mtd, NAND_CTL_SETCLE);
  250. this->write_byte(mtd, NAND_CMD_STATUS);
  251. this->hwcontrol(mtd, NAND_CTL_CLRCLE);
  252. while ( !(this->read_byte(mtd) & 0x40));
  253. return;
  254. /* This applies to read commands */
  255. default:
  256. /*
  257. * If we don't have access to the busy pin, we apply the given
  258. * command delay
  259. */
  260. if (!this->dev_ready) {
  261. udelay (this->chip_delay);
  262. return;
  263. }
  264. }
  265. /* wait until command is processed */
  266. while (!this->dev_ready(mtd));
  267. }
  268. #ifdef CONFIG_MTD_CMDLINE_PARTS
  269. extern int parse_cmdline_partitions(struct mtd_info *master, struct mtd_partitio
  270. n **pparts, char *);
  271. #endif
  272. /*
  273. * Main initialization routine
  274. */
  275. extern int nand_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc);
  276. int __init tx4925ndfmc_init (void)
  277. {
  278. struct nand_chip *this;
  279. int err = 0;
  280. /* Allocate memory for MTD device structure and private data */
  281. tx4925ndfmc_mtd = kmalloc (sizeof(struct mtd_info) + sizeof (struct nand_chip),
  282. GFP_KERNEL);
  283. if (!tx4925ndfmc_mtd) {
  284. printk ("Unable to allocate RBTX4925 NAND MTD device structure.\n");
  285. err = -ENOMEM;
  286. goto out;
  287. }
  288. tx4925ndfmc_device_setup();
  289. /* io is indirect via a register so don't need to ioremap address */
  290. /* Get pointer to private data */
  291. this = (struct nand_chip *) (&tx4925ndfmc_mtd[1]);
  292. /* Initialize structures */
  293. memset((char *) tx4925ndfmc_mtd, 0, sizeof(struct mtd_info));
  294. memset((char *) this, 0, sizeof(struct nand_chip));
  295. /* Link the private data with the MTD structure */
  296. tx4925ndfmc_mtd->priv = this;
  297. /* Set address of NAND IO lines */
  298. this->IO_ADDR_R = (void __iomem *)&(tx4925_ndfmcptr->dtr);
  299. this->IO_ADDR_W = (void __iomem *)&(tx4925_ndfmcptr->dtr);
  300. this->hwcontrol = tx4925ndfmc_hwcontrol;
  301. this->enable_hwecc = tx4925ndfmc_enable_hwecc;
  302. this->calculate_ecc = tx4925ndfmc_readecc;
  303. this->correct_data = nand_correct_data;
  304. this->eccmode = NAND_ECC_HW6_512;
  305. this->dev_ready = tx4925ndfmc_device_ready;
  306. /* 20 us command delay time */
  307. this->chip_delay = 20;
  308. this->read_byte = tx4925ndfmc_nand_read_byte;
  309. this->write_byte = tx4925ndfmc_nand_write_byte;
  310. this->cmdfunc = tx4925ndfmc_nand_command;
  311. this->write_buf = tx4925ndfmc_nand_write_buf;
  312. this->read_buf = tx4925ndfmc_nand_read_buf;
  313. this->verify_buf = tx4925ndfmc_nand_verify_buf;
  314. /* Scan to find existance of the device */
  315. if (nand_scan (tx4925ndfmc_mtd, 1)) {
  316. err = -ENXIO;
  317. goto out_ior;
  318. }
  319. /* Register the partitions */
  320. #ifdef CONFIG_MTD_CMDLINE_PARTS
  321. {
  322. int mtd_parts_nb = 0;
  323. struct mtd_partition *mtd_parts = 0;
  324. mtd_parts_nb = parse_cmdline_partitions(tx4925ndfmc_mtd, &mtd_parts, "tx4925ndfmc");
  325. if (mtd_parts_nb > 0)
  326. add_mtd_partitions(tx4925ndfmc_mtd, mtd_parts, mtd_parts_nb);
  327. else
  328. add_mtd_device(tx4925ndfmc_mtd);
  329. }
  330. #else /* ifdef CONFIG_MTD_CMDLINE_PARTS */
  331. switch(tx4925ndfmc_mtd->size){
  332. case 0x01000000: add_mtd_partitions(tx4925ndfmc_mtd, partition_info16k, NUM_PARTITIONS16K); break;
  333. case 0x02000000: add_mtd_partitions(tx4925ndfmc_mtd, partition_info32k, NUM_PARTITIONS32K); break;
  334. case 0x04000000: add_mtd_partitions(tx4925ndfmc_mtd, partition_info64k, NUM_PARTITIONS64K); break;
  335. case 0x08000000: add_mtd_partitions(tx4925ndfmc_mtd, partition_info128k, NUM_PARTITIONS128K); break;
  336. default: {
  337. printk ("Unsupported SmartMedia device\n");
  338. err = -ENXIO;
  339. goto out_ior;
  340. }
  341. }
  342. #endif /* ifdef CONFIG_MTD_CMDLINE_PARTS */
  343. goto out;
  344. out_ior:
  345. out:
  346. return err;
  347. }
  348. module_init(tx4925ndfmc_init);
  349. /*
  350. * Clean up routine
  351. */
  352. #ifdef MODULE
  353. static void __exit tx4925ndfmc_cleanup (void)
  354. {
  355. /* Release resources, unregister device */
  356. nand_release (tx4925ndfmc_mtd);
  357. /* Free the MTD device structure */
  358. kfree (tx4925ndfmc_mtd);
  359. }
  360. module_exit(tx4925ndfmc_cleanup);
  361. #endif
  362. MODULE_LICENSE("GPL");
  363. MODULE_AUTHOR("Alice Hennessy <ahennessy@mvista.com>");
  364. MODULE_DESCRIPTION("Glue layer for SmartMediaCard on Toshiba RBTX4925");