at91_nand.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. nand_chip->dev_ready = at91_nand_device_ready;
  113. nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
  114. nand_chip->chip_delay = 20; /* 20us command delay time */
  115. if (host->board->bus_width_16) /* 16-bit bus width */
  116. nand_chip->options |= NAND_BUSWIDTH_16;
  117. platform_set_drvdata(pdev, host);
  118. at91_nand_enable(host);
  119. if (host->board->det_pin) {
  120. if (at91_get_gpio_value(host->board->det_pin)) {
  121. printk ("No SmartMedia card inserted.\n");
  122. res = ENXIO;
  123. goto out;
  124. }
  125. }
  126. /* Scan to find existance of the device */
  127. if (nand_scan(mtd, 1)) {
  128. res = -ENXIO;
  129. goto out;
  130. }
  131. #ifdef CONFIG_MTD_PARTITIONS
  132. if (host->board->partition_info)
  133. partitions = host->board->partition_info(mtd->size, &num_partitions);
  134. #ifdef CONFIG_MTD_CMDLINE_PARTS
  135. else {
  136. mtd->name = "at91_nand";
  137. num_partitions = parse_mtd_partitions(mtd, part_probes, &partitions, 0);
  138. }
  139. #endif
  140. if ((!partitions) || (num_partitions == 0)) {
  141. printk(KERN_ERR "at91_nand: No parititions defined, or unsupported device.\n");
  142. res = ENXIO;
  143. goto release;
  144. }
  145. res = add_mtd_partitions(mtd, partitions, num_partitions);
  146. #else
  147. res = add_mtd_device(mtd);
  148. #endif
  149. if (!res)
  150. return res;
  151. release:
  152. nand_release(mtd);
  153. out:
  154. at91_nand_disable(host);
  155. platform_set_drvdata(pdev, NULL);
  156. iounmap(host->io_base);
  157. kfree(host);
  158. return res;
  159. }
  160. /*
  161. * Remove a NAND device.
  162. */
  163. static int __devexit at91_nand_remove(struct platform_device *pdev)
  164. {
  165. struct at91_nand_host *host = platform_get_drvdata(pdev);
  166. struct mtd_info *mtd = &host->mtd;
  167. nand_release(mtd);
  168. at91_nand_disable(host);
  169. iounmap(host->io_base);
  170. kfree(host);
  171. return 0;
  172. }
  173. static struct platform_driver at91_nand_driver = {
  174. .probe = at91_nand_probe,
  175. .remove = at91_nand_remove,
  176. .driver = {
  177. .name = "at91_nand",
  178. .owner = THIS_MODULE,
  179. },
  180. };
  181. static int __init at91_nand_init(void)
  182. {
  183. return platform_driver_register(&at91_nand_driver);
  184. }
  185. static void __exit at91_nand_exit(void)
  186. {
  187. platform_driver_unregister(&at91_nand_driver);
  188. }
  189. module_init(at91_nand_init);
  190. module_exit(at91_nand_exit);
  191. MODULE_LICENSE("GPL");
  192. MODULE_AUTHOR("Rick Bronson");
  193. MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91RM9200");