at91_nand.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * drivers/mtd/nand/at91_nand.c
  3. *
  4. * Copyright (C) 2003 Rick Bronson
  5. *
  6. * Derived from drivers/mtd/nand/autcpu12.c
  7. * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
  8. *
  9. * Derived from drivers/mtd/spia.c
  10. * Copyright (C) 2000 Steven J. Hill (sjhill@cotw.com)
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/mtd/mtd.h>
  21. #include <linux/mtd/nand.h>
  22. #include <linux/mtd/partitions.h>
  23. #include <asm/io.h>
  24. #include <asm/sizes.h>
  25. #include <asm/hardware.h>
  26. #include <asm/arch/board.h>
  27. #include <asm/arch/gpio.h>
  28. struct at91_nand_host {
  29. struct nand_chip nand_chip;
  30. struct mtd_info mtd;
  31. void __iomem *io_base;
  32. struct at91_nand_data *board;
  33. };
  34. /*
  35. * Hardware specific access to control-lines
  36. */
  37. static void at91_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  38. {
  39. struct nand_chip *nand_chip = mtd->priv;
  40. struct at91_nand_host *host = nand_chip->priv;
  41. if (cmd == NAND_CMD_NONE)
  42. return;
  43. if (ctrl & NAND_CLE)
  44. writeb(cmd, host->io_base + (1 << host->board->cle));
  45. else
  46. writeb(cmd, host->io_base + (1 << host->board->ale));
  47. }
  48. /*
  49. * Read the Device Ready pin.
  50. */
  51. static int at91_nand_device_ready(struct mtd_info *mtd)
  52. {
  53. struct nand_chip *nand_chip = mtd->priv;
  54. struct at91_nand_host *host = nand_chip->priv;
  55. return at91_get_gpio_value(host->board->rdy_pin);
  56. }
  57. /*
  58. * Enable NAND.
  59. */
  60. static void at91_nand_enable(struct at91_nand_host *host)
  61. {
  62. if (host->board->enable_pin)
  63. at91_set_gpio_value(host->board->enable_pin, 0);
  64. }
  65. /*
  66. * Disable NAND.
  67. */
  68. static void at91_nand_disable(struct at91_nand_host *host)
  69. {
  70. if (host->board->enable_pin)
  71. at91_set_gpio_value(host->board->enable_pin, 1);
  72. }
  73. #ifdef CONFIG_MTD_PARTITIONS
  74. const char *part_probes[] = { "cmdlinepart", NULL };
  75. #endif
  76. /*
  77. * Probe for the NAND device.
  78. */
  79. static int __init at91_nand_probe(struct platform_device *pdev)
  80. {
  81. struct at91_nand_host *host;
  82. struct mtd_info *mtd;
  83. struct nand_chip *nand_chip;
  84. int res;
  85. #ifdef CONFIG_MTD_PARTITIONS
  86. struct mtd_partition *partitions = NULL;
  87. int num_partitions = 0;
  88. #endif
  89. /* Allocate memory for the device structure (and zero it) */
  90. host = kzalloc(sizeof(struct at91_nand_host), GFP_KERNEL);
  91. if (!host) {
  92. printk(KERN_ERR "at91_nand: failed to allocate device structure.\n");
  93. return -ENOMEM;
  94. }
  95. host->io_base = ioremap(pdev->resource[0].start,
  96. pdev->resource[0].end - pdev->resource[0].start + 1);
  97. if (host->io_base == NULL) {
  98. printk(KERN_ERR "at91_nand: ioremap failed\n");
  99. kfree(host);
  100. return -EIO;
  101. }
  102. mtd = &host->mtd;
  103. nand_chip = &host->nand_chip;
  104. host->board = pdev->dev.platform_data;
  105. nand_chip->priv = host; /* link the private data structures */
  106. mtd->priv = nand_chip;
  107. mtd->owner = THIS_MODULE;
  108. /* Set address of NAND IO lines */
  109. nand_chip->IO_ADDR_R = host->io_base;
  110. nand_chip->IO_ADDR_W = host->io_base;
  111. nand_chip->cmd_ctrl = at91_nand_cmd_ctrl;
  112. if (host->board->rdy_pin)
  113. nand_chip->dev_ready = at91_nand_device_ready;
  114. nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
  115. nand_chip->chip_delay = 20; /* 20us command delay time */
  116. if (host->board->bus_width_16) /* 16-bit bus width */
  117. nand_chip->options |= NAND_BUSWIDTH_16;
  118. platform_set_drvdata(pdev, host);
  119. at91_nand_enable(host);
  120. if (host->board->det_pin) {
  121. if (at91_get_gpio_value(host->board->det_pin)) {
  122. printk ("No SmartMedia card inserted.\n");
  123. res = ENXIO;
  124. goto out;
  125. }
  126. }
  127. /* Scan to find existance of the device */
  128. if (nand_scan(mtd, 1)) {
  129. res = -ENXIO;
  130. goto out;
  131. }
  132. #ifdef CONFIG_MTD_PARTITIONS
  133. if (host->board->partition_info)
  134. partitions = host->board->partition_info(mtd->size, &num_partitions);
  135. #ifdef CONFIG_MTD_CMDLINE_PARTS
  136. else {
  137. mtd->name = "at91_nand";
  138. num_partitions = parse_mtd_partitions(mtd, part_probes, &partitions, 0);
  139. }
  140. #endif
  141. if ((!partitions) || (num_partitions == 0)) {
  142. printk(KERN_ERR "at91_nand: No parititions defined, or unsupported device.\n");
  143. res = ENXIO;
  144. goto release;
  145. }
  146. res = add_mtd_partitions(mtd, partitions, num_partitions);
  147. #else
  148. res = add_mtd_device(mtd);
  149. #endif
  150. if (!res)
  151. return res;
  152. release:
  153. nand_release(mtd);
  154. out:
  155. at91_nand_disable(host);
  156. platform_set_drvdata(pdev, NULL);
  157. iounmap(host->io_base);
  158. kfree(host);
  159. return res;
  160. }
  161. /*
  162. * Remove a NAND device.
  163. */
  164. static int __devexit at91_nand_remove(struct platform_device *pdev)
  165. {
  166. struct at91_nand_host *host = platform_get_drvdata(pdev);
  167. struct mtd_info *mtd = &host->mtd;
  168. nand_release(mtd);
  169. at91_nand_disable(host);
  170. iounmap(host->io_base);
  171. kfree(host);
  172. return 0;
  173. }
  174. static struct platform_driver at91_nand_driver = {
  175. .probe = at91_nand_probe,
  176. .remove = at91_nand_remove,
  177. .driver = {
  178. .name = "at91_nand",
  179. .owner = THIS_MODULE,
  180. },
  181. };
  182. static int __init at91_nand_init(void)
  183. {
  184. return platform_driver_register(&at91_nand_driver);
  185. }
  186. static void __exit at91_nand_exit(void)
  187. {
  188. platform_driver_unregister(&at91_nand_driver);
  189. }
  190. module_init(at91_nand_init);
  191. module_exit(at91_nand_exit);
  192. MODULE_LICENSE("GPL");
  193. MODULE_AUTHOR("Rick Bronson");
  194. MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91RM9200");