cmx270_nand.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * linux/drivers/mtd/nand/cmx270-nand.c
  3. *
  4. * Copyright (C) 2006 Compulab, Ltd.
  5. * Mike Rapoport <mike@compulab.co.il>
  6. *
  7. * Derived from drivers/mtd/nand/h1910.c
  8. * Copyright (C) 2002 Marius Gröger (mag@sysgo.de)
  9. * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
  10. *
  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. * Overview:
  17. * This is a device driver for the NAND flash device found on the
  18. * CM-X270 board.
  19. */
  20. #include <linux/mtd/nand.h>
  21. #include <linux/mtd/partitions.h>
  22. #include <linux/slab.h>
  23. #include <linux/gpio.h>
  24. #include <linux/module.h>
  25. #include <asm/io.h>
  26. #include <asm/irq.h>
  27. #include <asm/mach-types.h>
  28. #include <mach/pxa2xx-regs.h>
  29. #define GPIO_NAND_CS (11)
  30. #define GPIO_NAND_RB (89)
  31. /* MTD structure for CM-X270 board */
  32. static struct mtd_info *cmx270_nand_mtd;
  33. /* remaped IO address of the device */
  34. static void __iomem *cmx270_nand_io;
  35. /*
  36. * Define static partitions for flash device
  37. */
  38. static struct mtd_partition partition_info[] = {
  39. [0] = {
  40. .name = "cmx270-0",
  41. .offset = 0,
  42. .size = MTDPART_SIZ_FULL
  43. }
  44. };
  45. #define NUM_PARTITIONS (ARRAY_SIZE(partition_info))
  46. static u_char cmx270_read_byte(struct mtd_info *mtd)
  47. {
  48. struct nand_chip *this = mtd->priv;
  49. return (readl(this->IO_ADDR_R) >> 16);
  50. }
  51. static void cmx270_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  52. {
  53. int i;
  54. struct nand_chip *this = mtd->priv;
  55. for (i=0; i<len; i++)
  56. writel((*buf++ << 16), this->IO_ADDR_W);
  57. }
  58. static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  59. {
  60. int i;
  61. struct nand_chip *this = mtd->priv;
  62. for (i=0; i<len; i++)
  63. *buf++ = readl(this->IO_ADDR_R) >> 16;
  64. }
  65. static inline void nand_cs_on(void)
  66. {
  67. gpio_set_value(GPIO_NAND_CS, 0);
  68. }
  69. static void nand_cs_off(void)
  70. {
  71. dsb();
  72. gpio_set_value(GPIO_NAND_CS, 1);
  73. }
  74. /*
  75. * hardware specific access to control-lines
  76. */
  77. static void cmx270_hwcontrol(struct mtd_info *mtd, int dat,
  78. unsigned int ctrl)
  79. {
  80. struct nand_chip* this = mtd->priv;
  81. unsigned int nandaddr = (unsigned int)this->IO_ADDR_W;
  82. dsb();
  83. if (ctrl & NAND_CTRL_CHANGE) {
  84. if ( ctrl & NAND_ALE )
  85. nandaddr |= (1 << 3);
  86. else
  87. nandaddr &= ~(1 << 3);
  88. if ( ctrl & NAND_CLE )
  89. nandaddr |= (1 << 2);
  90. else
  91. nandaddr &= ~(1 << 2);
  92. if ( ctrl & NAND_NCE )
  93. nand_cs_on();
  94. else
  95. nand_cs_off();
  96. }
  97. dsb();
  98. this->IO_ADDR_W = (void __iomem*)nandaddr;
  99. if (dat != NAND_CMD_NONE)
  100. writel((dat << 16), this->IO_ADDR_W);
  101. dsb();
  102. }
  103. /*
  104. * read device ready pin
  105. */
  106. static int cmx270_device_ready(struct mtd_info *mtd)
  107. {
  108. dsb();
  109. return (gpio_get_value(GPIO_NAND_RB));
  110. }
  111. /*
  112. * Main initialization routine
  113. */
  114. static int __init cmx270_init(void)
  115. {
  116. struct nand_chip *this;
  117. int ret;
  118. if (!(machine_is_armcore() && cpu_is_pxa27x()))
  119. return -ENODEV;
  120. ret = gpio_request(GPIO_NAND_CS, "NAND CS");
  121. if (ret) {
  122. pr_warning("CM-X270: failed to request NAND CS gpio\n");
  123. return ret;
  124. }
  125. gpio_direction_output(GPIO_NAND_CS, 1);
  126. ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
  127. if (ret) {
  128. pr_warning("CM-X270: failed to request NAND R/B gpio\n");
  129. goto err_gpio_request;
  130. }
  131. gpio_direction_input(GPIO_NAND_RB);
  132. /* Allocate memory for MTD device structure and private data */
  133. cmx270_nand_mtd = kzalloc(sizeof(struct mtd_info) +
  134. sizeof(struct nand_chip),
  135. GFP_KERNEL);
  136. if (!cmx270_nand_mtd) {
  137. ret = -ENOMEM;
  138. goto err_kzalloc;
  139. }
  140. cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
  141. if (!cmx270_nand_io) {
  142. pr_debug("Unable to ioremap NAND device\n");
  143. ret = -EINVAL;
  144. goto err_ioremap;
  145. }
  146. /* Get pointer to private data */
  147. this = (struct nand_chip *)(&cmx270_nand_mtd[1]);
  148. /* Link the private data with the MTD structure */
  149. cmx270_nand_mtd->owner = THIS_MODULE;
  150. cmx270_nand_mtd->priv = this;
  151. /* insert callbacks */
  152. this->IO_ADDR_R = cmx270_nand_io;
  153. this->IO_ADDR_W = cmx270_nand_io;
  154. this->cmd_ctrl = cmx270_hwcontrol;
  155. this->dev_ready = cmx270_device_ready;
  156. /* 15 us command delay time */
  157. this->chip_delay = 20;
  158. this->ecc.mode = NAND_ECC_SOFT;
  159. /* read/write functions */
  160. this->read_byte = cmx270_read_byte;
  161. this->read_buf = cmx270_read_buf;
  162. this->write_buf = cmx270_write_buf;
  163. /* Scan to find existence of the device */
  164. if (nand_scan (cmx270_nand_mtd, 1)) {
  165. pr_notice("No NAND device\n");
  166. ret = -ENXIO;
  167. goto err_scan;
  168. }
  169. /* Register the partitions */
  170. ret = mtd_device_parse_register(cmx270_nand_mtd, NULL, NULL,
  171. partition_info, NUM_PARTITIONS);
  172. if (ret)
  173. goto err_scan;
  174. /* Return happy */
  175. return 0;
  176. err_scan:
  177. iounmap(cmx270_nand_io);
  178. err_ioremap:
  179. kfree(cmx270_nand_mtd);
  180. err_kzalloc:
  181. gpio_free(GPIO_NAND_RB);
  182. err_gpio_request:
  183. gpio_free(GPIO_NAND_CS);
  184. return ret;
  185. }
  186. module_init(cmx270_init);
  187. /*
  188. * Clean up routine
  189. */
  190. static void __exit cmx270_cleanup(void)
  191. {
  192. /* Release resources, unregister device */
  193. nand_release(cmx270_nand_mtd);
  194. gpio_free(GPIO_NAND_RB);
  195. gpio_free(GPIO_NAND_CS);
  196. iounmap(cmx270_nand_io);
  197. /* Free the MTD device structure */
  198. kfree (cmx270_nand_mtd);
  199. }
  200. module_exit(cmx270_cleanup);
  201. MODULE_LICENSE("GPL");
  202. MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
  203. MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");