cmx270_nand.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 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 __init cmx270_init(void)
  124. {
  125. struct nand_chip *this;
  126. int ret;
  127. if (!(machine_is_armcore() && cpu_is_pxa27x()))
  128. return -ENODEV;
  129. ret = gpio_request(GPIO_NAND_CS, "NAND CS");
  130. if (ret) {
  131. pr_warning("CM-X270: failed to request NAND CS gpio\n");
  132. return ret;
  133. }
  134. gpio_direction_output(GPIO_NAND_CS, 1);
  135. ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
  136. if (ret) {
  137. pr_warning("CM-X270: failed to request NAND R/B gpio\n");
  138. goto err_gpio_request;
  139. }
  140. gpio_direction_input(GPIO_NAND_RB);
  141. /* Allocate memory for MTD device structure and private data */
  142. cmx270_nand_mtd = kzalloc(sizeof(struct mtd_info) +
  143. sizeof(struct nand_chip),
  144. GFP_KERNEL);
  145. if (!cmx270_nand_mtd) {
  146. pr_debug("Unable to allocate CM-X270 NAND MTD device structure.\n");
  147. ret = -ENOMEM;
  148. goto err_kzalloc;
  149. }
  150. cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
  151. if (!cmx270_nand_io) {
  152. pr_debug("Unable to ioremap NAND device\n");
  153. ret = -EINVAL;
  154. goto err_ioremap;
  155. }
  156. /* Get pointer to private data */
  157. this = (struct nand_chip *)(&cmx270_nand_mtd[1]);
  158. /* Link the private data with the MTD structure */
  159. cmx270_nand_mtd->owner = THIS_MODULE;
  160. cmx270_nand_mtd->priv = this;
  161. /* insert callbacks */
  162. this->IO_ADDR_R = cmx270_nand_io;
  163. this->IO_ADDR_W = cmx270_nand_io;
  164. this->cmd_ctrl = cmx270_hwcontrol;
  165. this->dev_ready = cmx270_device_ready;
  166. /* 15 us command delay time */
  167. this->chip_delay = 20;
  168. this->ecc.mode = NAND_ECC_SOFT;
  169. /* read/write functions */
  170. this->read_byte = cmx270_read_byte;
  171. this->read_buf = cmx270_read_buf;
  172. this->write_buf = cmx270_write_buf;
  173. this->verify_buf = cmx270_verify_buf;
  174. /* Scan to find existence of the device */
  175. if (nand_scan (cmx270_nand_mtd, 1)) {
  176. pr_notice("No NAND device\n");
  177. ret = -ENXIO;
  178. goto err_scan;
  179. }
  180. /* Register the partitions */
  181. ret = mtd_device_parse_register(cmx270_nand_mtd, NULL, NULL,
  182. partition_info, NUM_PARTITIONS);
  183. if (ret)
  184. goto err_scan;
  185. /* Return happy */
  186. return 0;
  187. err_scan:
  188. iounmap(cmx270_nand_io);
  189. err_ioremap:
  190. kfree(cmx270_nand_mtd);
  191. err_kzalloc:
  192. gpio_free(GPIO_NAND_RB);
  193. err_gpio_request:
  194. gpio_free(GPIO_NAND_CS);
  195. return ret;
  196. }
  197. module_init(cmx270_init);
  198. /*
  199. * Clean up routine
  200. */
  201. static void __exit cmx270_cleanup(void)
  202. {
  203. /* Release resources, unregister device */
  204. nand_release(cmx270_nand_mtd);
  205. gpio_free(GPIO_NAND_RB);
  206. gpio_free(GPIO_NAND_CS);
  207. iounmap(cmx270_nand_io);
  208. /* Free the MTD device structure */
  209. kfree (cmx270_nand_mtd);
  210. }
  211. module_exit(cmx270_cleanup);
  212. MODULE_LICENSE("GPL");
  213. MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
  214. MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");