cmx270_nand.c 5.0 KB

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