at91_nand.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. /*
  74. * Probe for the NAND device.
  75. */
  76. static int __init at91_nand_probe(struct platform_device *pdev)
  77. {
  78. struct at91_nand_host *host;
  79. struct mtd_info *mtd;
  80. struct nand_chip *nand_chip;
  81. int res;
  82. #ifdef CONFIG_MTD_PARTITIONS
  83. struct mtd_partition *partitions = NULL;
  84. int num_partitions = 0;
  85. #endif
  86. /* Allocate memory for the device structure (and zero it) */
  87. host = kzalloc(sizeof(struct at91_nand_host), GFP_KERNEL);
  88. if (!host) {
  89. printk(KERN_ERR "at91_nand: failed to allocate device structure.\n");
  90. return -ENOMEM;
  91. }
  92. host->io_base = ioremap(pdev->resource[0].start,
  93. pdev->resource[0].end - pdev->resource[0].start + 1);
  94. if (host->io_base == NULL) {
  95. printk(KERN_ERR "at91_nand: ioremap failed\n");
  96. kfree(host);
  97. return -EIO;
  98. }
  99. mtd = &host->mtd;
  100. nand_chip = &host->nand_chip;
  101. host->board = pdev->dev.platform_data;
  102. nand_chip->priv = host; /* link the private data structures */
  103. mtd->priv = nand_chip;
  104. mtd->owner = THIS_MODULE;
  105. /* Set address of NAND IO lines */
  106. nand_chip->IO_ADDR_R = host->io_base;
  107. nand_chip->IO_ADDR_W = host->io_base;
  108. nand_chip->cmd_ctrl = at91_nand_cmd_ctrl;
  109. nand_chip->dev_ready = at91_nand_device_ready;
  110. nand_chip->ecc.mode = NAND_ECC_SOFT; /* enable ECC */
  111. nand_chip->chip_delay = 20; /* 20us command delay time */
  112. if (host->board->bus_width_16) /* 16-bit bus width */
  113. nand_chip->options |= NAND_BUSWIDTH_16;
  114. platform_set_drvdata(pdev, host);
  115. at91_nand_enable(host);
  116. if (host->board->det_pin) {
  117. if (at91_get_gpio_value(host->board->det_pin)) {
  118. printk ("No SmartMedia card inserted.\n");
  119. res = ENXIO;
  120. goto out;
  121. }
  122. }
  123. /* Scan to find existance of the device */
  124. if (nand_scan(mtd, 1)) {
  125. res = -ENXIO;
  126. goto out;
  127. }
  128. #ifdef CONFIG_MTD_PARTITIONS
  129. if (host->board->partition_info)
  130. partitions = host->board->partition_info(mtd->size, &num_partitions);
  131. if ((!partitions) || (num_partitions == 0)) {
  132. printk(KERN_ERR "at91_nand: No parititions defined, or unsupported device.\n");
  133. res = ENXIO;
  134. goto release;
  135. }
  136. res = add_mtd_partitions(mtd, partitions, num_partitions);
  137. #else
  138. res = add_mtd_device(mtd);
  139. #endif
  140. if (!res)
  141. return res;
  142. release:
  143. nand_release(mtd);
  144. out:
  145. at91_nand_disable(host);
  146. platform_set_drvdata(pdev, NULL);
  147. iounmap(host->io_base);
  148. kfree(host);
  149. return res;
  150. }
  151. /*
  152. * Remove a NAND device.
  153. */
  154. static int __devexit at91_nand_remove(struct platform_device *pdev)
  155. {
  156. struct at91_nand_host *host = platform_get_drvdata(pdev);
  157. struct mtd_info *mtd = &host->mtd;
  158. nand_release(mtd);
  159. at91_nand_disable(host);
  160. iounmap(host->io_base);
  161. kfree(host);
  162. return 0;
  163. }
  164. static struct platform_driver at91_nand_driver = {
  165. .probe = at91_nand_probe,
  166. .remove = at91_nand_remove,
  167. .driver = {
  168. .name = "at91_nand",
  169. .owner = THIS_MODULE,
  170. },
  171. };
  172. static int __init at91_nand_init(void)
  173. {
  174. return platform_driver_register(&at91_nand_driver);
  175. }
  176. static void __exit at91_nand_exit(void)
  177. {
  178. platform_driver_unregister(&at91_nand_driver);
  179. }
  180. module_init(at91_nand_init);
  181. module_exit(at91_nand_exit);
  182. MODULE_LICENSE("GPL");
  183. MODULE_AUTHOR("Rick Bronson");
  184. MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91RM9200");