cmx270_nand.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (C) 2006 Compulab, Ltd.
  3. * Mike Rapoport <mike@compulab.co.il>
  4. *
  5. * Derived from drivers/mtd/nand/h1910.c (removed in v3.10)
  6. * Copyright (C) 2002 Marius Gröger (mag@sysgo.de)
  7. * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * Overview:
  15. * This is a device driver for the NAND flash device found on the
  16. * CM-X270 board.
  17. */
  18. #include <linux/mtd/rawnand.h>
  19. #include <linux/mtd/partitions.h>
  20. #include <linux/slab.h>
  21. #include <linux/gpio.h>
  22. #include <linux/module.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 const 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. static u_char cmx270_read_byte(struct nand_chip *this)
  45. {
  46. return (readl(this->legacy.IO_ADDR_R) >> 16);
  47. }
  48. static void cmx270_write_buf(struct nand_chip *this, const u_char *buf,
  49. int len)
  50. {
  51. int i;
  52. for (i=0; i<len; i++)
  53. writel((*buf++ << 16), this->legacy.IO_ADDR_W);
  54. }
  55. static void cmx270_read_buf(struct nand_chip *this, u_char *buf, int len)
  56. {
  57. int i;
  58. for (i=0; i<len; i++)
  59. *buf++ = readl(this->legacy.IO_ADDR_R) >> 16;
  60. }
  61. static inline void nand_cs_on(void)
  62. {
  63. gpio_set_value(GPIO_NAND_CS, 0);
  64. }
  65. static void nand_cs_off(void)
  66. {
  67. dsb();
  68. gpio_set_value(GPIO_NAND_CS, 1);
  69. }
  70. /*
  71. * hardware specific access to control-lines
  72. */
  73. static void cmx270_hwcontrol(struct nand_chip *this, int dat,
  74. unsigned int ctrl)
  75. {
  76. unsigned int nandaddr = (unsigned int)this->legacy.IO_ADDR_W;
  77. dsb();
  78. if (ctrl & NAND_CTRL_CHANGE) {
  79. if ( ctrl & NAND_ALE )
  80. nandaddr |= (1 << 3);
  81. else
  82. nandaddr &= ~(1 << 3);
  83. if ( ctrl & NAND_CLE )
  84. nandaddr |= (1 << 2);
  85. else
  86. nandaddr &= ~(1 << 2);
  87. if ( ctrl & NAND_NCE )
  88. nand_cs_on();
  89. else
  90. nand_cs_off();
  91. }
  92. dsb();
  93. this->legacy.IO_ADDR_W = (void __iomem*)nandaddr;
  94. if (dat != NAND_CMD_NONE)
  95. writel((dat << 16), this->legacy.IO_ADDR_W);
  96. dsb();
  97. }
  98. /*
  99. * read device ready pin
  100. */
  101. static int cmx270_device_ready(struct nand_chip *this)
  102. {
  103. dsb();
  104. return (gpio_get_value(GPIO_NAND_RB));
  105. }
  106. /*
  107. * Main initialization routine
  108. */
  109. static int __init cmx270_init(void)
  110. {
  111. struct nand_chip *this;
  112. int ret;
  113. if (!(machine_is_armcore() && cpu_is_pxa27x()))
  114. return -ENODEV;
  115. ret = gpio_request(GPIO_NAND_CS, "NAND CS");
  116. if (ret) {
  117. pr_warn("CM-X270: failed to request NAND CS gpio\n");
  118. return ret;
  119. }
  120. gpio_direction_output(GPIO_NAND_CS, 1);
  121. ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
  122. if (ret) {
  123. pr_warn("CM-X270: failed to request NAND R/B gpio\n");
  124. goto err_gpio_request;
  125. }
  126. gpio_direction_input(GPIO_NAND_RB);
  127. /* Allocate memory for MTD device structure and private data */
  128. this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
  129. if (!this) {
  130. ret = -ENOMEM;
  131. goto err_kzalloc;
  132. }
  133. cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
  134. if (!cmx270_nand_io) {
  135. pr_debug("Unable to ioremap NAND device\n");
  136. ret = -EINVAL;
  137. goto err_ioremap;
  138. }
  139. cmx270_nand_mtd = nand_to_mtd(this);
  140. /* Link the private data with the MTD structure */
  141. cmx270_nand_mtd->owner = THIS_MODULE;
  142. /* insert callbacks */
  143. this->legacy.IO_ADDR_R = cmx270_nand_io;
  144. this->legacy.IO_ADDR_W = cmx270_nand_io;
  145. this->legacy.cmd_ctrl = cmx270_hwcontrol;
  146. this->legacy.dev_ready = cmx270_device_ready;
  147. /* 15 us command delay time */
  148. this->legacy.chip_delay = 20;
  149. this->ecc.mode = NAND_ECC_SOFT;
  150. this->ecc.algo = NAND_ECC_HAMMING;
  151. /* read/write functions */
  152. this->legacy.read_byte = cmx270_read_byte;
  153. this->legacy.read_buf = cmx270_read_buf;
  154. this->legacy.write_buf = cmx270_write_buf;
  155. /* Scan to find existence of the device */
  156. ret = nand_scan(this, 1);
  157. if (ret) {
  158. pr_notice("No NAND device\n");
  159. goto err_scan;
  160. }
  161. /* Register the partitions */
  162. ret = mtd_device_register(cmx270_nand_mtd, partition_info,
  163. NUM_PARTITIONS);
  164. if (ret)
  165. goto err_scan;
  166. /* Return happy */
  167. return 0;
  168. err_scan:
  169. iounmap(cmx270_nand_io);
  170. err_ioremap:
  171. kfree(this);
  172. err_kzalloc:
  173. gpio_free(GPIO_NAND_RB);
  174. err_gpio_request:
  175. gpio_free(GPIO_NAND_CS);
  176. return ret;
  177. }
  178. module_init(cmx270_init);
  179. /*
  180. * Clean up routine
  181. */
  182. static void __exit cmx270_cleanup(void)
  183. {
  184. /* Release resources, unregister device */
  185. nand_release(mtd_to_nand(cmx270_nand_mtd));
  186. gpio_free(GPIO_NAND_RB);
  187. gpio_free(GPIO_NAND_CS);
  188. iounmap(cmx270_nand_io);
  189. kfree(mtd_to_nand(cmx270_nand_mtd));
  190. }
  191. module_exit(cmx270_cleanup);
  192. MODULE_LICENSE("GPL");
  193. MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
  194. MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");