cmx270_nand.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 <asm/io.h>
  23. #include <asm/irq.h>
  24. #include <asm/arch/hardware.h>
  25. #include <asm/arch/pxa-regs.h>
  26. #define GPIO_NAND_CS (11)
  27. #define GPIO_NAND_RB (89)
  28. /* This macro needed to ensure in-order operation of GPIO and local
  29. * bus. Without both asm command and dummy uncached read there're
  30. * states when NAND access is broken. I've looked for such macro(s) in
  31. * include/asm-arm but found nothing approptiate.
  32. * dmac_clean_range is close, but is makes cache invalidation
  33. * unnecessary here and it cannot be used in module
  34. */
  35. #define DRAIN_WB() \
  36. do { \
  37. unsigned char dummy; \
  38. asm volatile ("mcr p15, 0, r0, c7, c10, 4":::"r0"); \
  39. dummy=*((unsigned char*)UNCACHED_ADDR); \
  40. } while(0)
  41. /* MTD structure for CM-X270 board */
  42. static struct mtd_info *cmx270_nand_mtd;
  43. /* remaped IO address of the device */
  44. static void __iomem *cmx270_nand_io;
  45. /*
  46. * Define static partitions for flash device
  47. */
  48. static struct mtd_partition partition_info[] = {
  49. [0] = {
  50. .name = "cmx270-0",
  51. .offset = 0,
  52. .size = MTDPART_SIZ_FULL
  53. }
  54. };
  55. #define NUM_PARTITIONS (ARRAY_SIZE(partition_info))
  56. const char *part_probes[] = { "cmdlinepart", NULL };
  57. static u_char cmx270_read_byte(struct mtd_info *mtd)
  58. {
  59. struct nand_chip *this = mtd->priv;
  60. return (readl(this->IO_ADDR_R) >> 16);
  61. }
  62. static void cmx270_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  63. {
  64. int i;
  65. struct nand_chip *this = mtd->priv;
  66. for (i=0; i<len; i++)
  67. writel((*buf++ << 16), this->IO_ADDR_W);
  68. }
  69. static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  70. {
  71. int i;
  72. struct nand_chip *this = mtd->priv;
  73. for (i=0; i<len; i++)
  74. *buf++ = readl(this->IO_ADDR_R) >> 16;
  75. }
  76. static int cmx270_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  77. {
  78. int i;
  79. struct nand_chip *this = mtd->priv;
  80. for (i=0; i<len; i++)
  81. if (buf[i] != (u_char)(readl(this->IO_ADDR_R) >> 16))
  82. return -EFAULT;
  83. return 0;
  84. }
  85. static inline void nand_cs_on(void)
  86. {
  87. GPCR(GPIO_NAND_CS) = GPIO_bit(GPIO_NAND_CS);
  88. }
  89. static void nand_cs_off(void)
  90. {
  91. DRAIN_WB();
  92. GPSR(GPIO_NAND_CS) = GPIO_bit(GPIO_NAND_CS);
  93. }
  94. /*
  95. * hardware specific access to control-lines
  96. */
  97. static void cmx270_hwcontrol(struct mtd_info *mtd, int dat,
  98. unsigned int ctrl)
  99. {
  100. struct nand_chip* this = mtd->priv;
  101. unsigned int nandaddr = (unsigned int)this->IO_ADDR_W;
  102. DRAIN_WB();
  103. if (ctrl & NAND_CTRL_CHANGE) {
  104. if ( ctrl & NAND_ALE )
  105. nandaddr |= (1 << 3);
  106. else
  107. nandaddr &= ~(1 << 3);
  108. if ( ctrl & NAND_CLE )
  109. nandaddr |= (1 << 2);
  110. else
  111. nandaddr &= ~(1 << 2);
  112. if ( ctrl & NAND_NCE )
  113. nand_cs_on();
  114. else
  115. nand_cs_off();
  116. }
  117. DRAIN_WB();
  118. this->IO_ADDR_W = (void __iomem*)nandaddr;
  119. if (dat != NAND_CMD_NONE)
  120. writel((dat << 16), this->IO_ADDR_W);
  121. DRAIN_WB();
  122. }
  123. /*
  124. * read device ready pin
  125. */
  126. static int cmx270_device_ready(struct mtd_info *mtd)
  127. {
  128. DRAIN_WB();
  129. return (GPLR(GPIO_NAND_RB) & GPIO_bit(GPIO_NAND_RB));
  130. }
  131. /*
  132. * Main initialization routine
  133. */
  134. static int cmx270_init(void)
  135. {
  136. struct nand_chip *this;
  137. const char *part_type;
  138. struct mtd_partition *mtd_parts;
  139. int mtd_parts_nb = 0;
  140. int ret;
  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. printk("Unable to allocate CM-X270 NAND MTD device structure.\n");
  147. return -ENOMEM;
  148. }
  149. cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
  150. if (!cmx270_nand_io) {
  151. printk("Unable to ioremap NAND device\n");
  152. ret = -EINVAL;
  153. goto err1;
  154. }
  155. /* Get pointer to private data */
  156. this = (struct nand_chip *)(&cmx270_nand_mtd[1]);
  157. /* Link the private data with the MTD structure */
  158. cmx270_nand_mtd->owner = THIS_MODULE;
  159. cmx270_nand_mtd->priv = this;
  160. /* insert callbacks */
  161. this->IO_ADDR_R = cmx270_nand_io;
  162. this->IO_ADDR_W = cmx270_nand_io;
  163. this->cmd_ctrl = cmx270_hwcontrol;
  164. this->dev_ready = cmx270_device_ready;
  165. /* 15 us command delay time */
  166. this->chip_delay = 20;
  167. this->ecc.mode = NAND_ECC_SOFT;
  168. /* read/write functions */
  169. this->read_byte = cmx270_read_byte;
  170. this->read_buf = cmx270_read_buf;
  171. this->write_buf = cmx270_write_buf;
  172. this->verify_buf = cmx270_verify_buf;
  173. /* Scan to find existence of the device */
  174. if (nand_scan (cmx270_nand_mtd, 1)) {
  175. printk(KERN_NOTICE "No NAND device\n");
  176. ret = -ENXIO;
  177. goto err2;
  178. }
  179. #ifdef CONFIG_MTD_CMDLINE_PARTS
  180. mtd_parts_nb = parse_mtd_partitions(cmx270_nand_mtd, part_probes,
  181. &mtd_parts, 0);
  182. if (mtd_parts_nb > 0)
  183. part_type = "command line";
  184. else
  185. mtd_parts_nb = 0;
  186. #endif
  187. if (!mtd_parts_nb) {
  188. mtd_parts = partition_info;
  189. mtd_parts_nb = NUM_PARTITIONS;
  190. part_type = "static";
  191. }
  192. /* Register the partitions */
  193. printk(KERN_NOTICE "Using %s partition definition\n", part_type);
  194. ret = add_mtd_partitions(cmx270_nand_mtd, mtd_parts, mtd_parts_nb);
  195. if (ret)
  196. goto err2;
  197. /* Return happy */
  198. return 0;
  199. err2:
  200. iounmap(cmx270_nand_io);
  201. err1:
  202. kfree(cmx270_nand_mtd);
  203. return ret;
  204. }
  205. module_init(cmx270_init);
  206. /*
  207. * Clean up routine
  208. */
  209. static void cmx270_cleanup(void)
  210. {
  211. /* Release resources, unregister device */
  212. nand_release(cmx270_nand_mtd);
  213. iounmap(cmx270_nand_io);
  214. /* Free the MTD device structure */
  215. kfree (cmx270_nand_mtd);
  216. }
  217. module_exit(cmx270_cleanup);
  218. MODULE_LICENSE("GPL");
  219. MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
  220. MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");