cmx270_nand.c 5.9 KB

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