cmx270_nand.c 5.9 KB

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