cmx270_nand.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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_to_nand(mtd);
  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_to_nand(mtd);
  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_to_nand(mtd);
  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_to_nand(mtd);
  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. this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
  134. if (!this) {
  135. ret = -ENOMEM;
  136. goto err_kzalloc;
  137. }
  138. cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
  139. if (!cmx270_nand_io) {
  140. pr_debug("Unable to ioremap NAND device\n");
  141. ret = -EINVAL;
  142. goto err_ioremap;
  143. }
  144. cmx270_nand_mtd = nand_to_mtd(this);
  145. /* Link the private data with the MTD structure */
  146. cmx270_nand_mtd->owner = THIS_MODULE;
  147. /* insert callbacks */
  148. this->IO_ADDR_R = cmx270_nand_io;
  149. this->IO_ADDR_W = cmx270_nand_io;
  150. this->cmd_ctrl = cmx270_hwcontrol;
  151. this->dev_ready = cmx270_device_ready;
  152. /* 15 us command delay time */
  153. this->chip_delay = 20;
  154. this->ecc.mode = NAND_ECC_SOFT;
  155. this->ecc.algo = NAND_ECC_HAMMING;
  156. /* read/write functions */
  157. this->read_byte = cmx270_read_byte;
  158. this->read_buf = cmx270_read_buf;
  159. this->write_buf = cmx270_write_buf;
  160. /* Scan to find existence of the device */
  161. if (nand_scan (cmx270_nand_mtd, 1)) {
  162. pr_notice("No NAND device\n");
  163. ret = -ENXIO;
  164. goto err_scan;
  165. }
  166. /* Register the partitions */
  167. ret = mtd_device_parse_register(cmx270_nand_mtd, NULL, NULL,
  168. partition_info, NUM_PARTITIONS);
  169. if (ret)
  170. goto err_scan;
  171. /* Return happy */
  172. return 0;
  173. err_scan:
  174. iounmap(cmx270_nand_io);
  175. err_ioremap:
  176. kfree(this);
  177. err_kzalloc:
  178. gpio_free(GPIO_NAND_RB);
  179. err_gpio_request:
  180. gpio_free(GPIO_NAND_CS);
  181. return ret;
  182. }
  183. module_init(cmx270_init);
  184. /*
  185. * Clean up routine
  186. */
  187. static void __exit cmx270_cleanup(void)
  188. {
  189. /* Release resources, unregister device */
  190. nand_release(cmx270_nand_mtd);
  191. gpio_free(GPIO_NAND_RB);
  192. gpio_free(GPIO_NAND_CS);
  193. iounmap(cmx270_nand_io);
  194. kfree(mtd_to_nand(cmx270_nand_mtd));
  195. }
  196. module_exit(cmx270_cleanup);
  197. MODULE_LICENSE("GPL");
  198. MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
  199. MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");