cmx270_nand.c 6.0 KB

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