ams-delta.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * drivers/mtd/nand/ams-delta.c
  3. *
  4. * Copyright (C) 2006 Jonathan McDowell <noodles@earth.li>
  5. *
  6. * Derived from drivers/mtd/toto.c
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Overview:
  13. * This is a device driver for the NAND flash device found on the
  14. * Amstrad E3 (Delta).
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/delay.h>
  20. #include <linux/mtd/mtd.h>
  21. #include <linux/mtd/nand.h>
  22. #include <linux/mtd/partitions.h>
  23. #include <asm/io.h>
  24. #include <asm/arch/hardware.h>
  25. #include <asm/sizes.h>
  26. #include <asm/arch/gpio.h>
  27. #include <asm/arch/board-ams-delta.h>
  28. /*
  29. * MTD structure for E3 (Delta)
  30. */
  31. static struct mtd_info *ams_delta_mtd = NULL;
  32. #define NAND_MASK (AMS_DELTA_LATCH2_NAND_NRE | AMS_DELTA_LATCH2_NAND_NWE | AMS_DELTA_LATCH2_NAND_CLE | AMS_DELTA_LATCH2_NAND_ALE | AMS_DELTA_LATCH2_NAND_NCE | AMS_DELTA_LATCH2_NAND_NWP)
  33. #define T_NAND_CTL_CLRALE(iob) ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_ALE, 0)
  34. #define T_NAND_CTL_SETALE(iob) ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_ALE, AMS_DELTA_LATCH2_NAND_ALE)
  35. #define T_NAND_CTL_CLRCLE(iob) ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_CLE, 0)
  36. #define T_NAND_CTL_SETCLE(iob) ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_CLE, AMS_DELTA_LATCH2_NAND_CLE)
  37. #define T_NAND_CTL_SETNCE(iob) ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NCE, 0)
  38. #define T_NAND_CTL_CLRNCE(iob) ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NCE, AMS_DELTA_LATCH2_NAND_NCE)
  39. /*
  40. * Define partitions for flash devices
  41. */
  42. static struct mtd_partition partition_info[] = {
  43. { .name = "Kernel",
  44. .offset = 0,
  45. .size = 3 * SZ_1M + SZ_512K },
  46. { .name = "u-boot",
  47. .offset = 3 * SZ_1M + SZ_512K,
  48. .size = SZ_256K },
  49. { .name = "u-boot params",
  50. .offset = 3 * SZ_1M + SZ_512K + SZ_256K,
  51. .size = SZ_256K },
  52. { .name = "Amstrad LDR",
  53. .offset = 4 * SZ_1M,
  54. .size = SZ_256K },
  55. { .name = "File system",
  56. .offset = 4 * SZ_1M + 1 * SZ_256K,
  57. .size = 27 * SZ_1M },
  58. { .name = "PBL reserved",
  59. .offset = 32 * SZ_1M - 3 * SZ_256K,
  60. .size = 3 * SZ_256K },
  61. };
  62. /*
  63. * hardware specific access to control-lines
  64. */
  65. static void ams_delta_hwcontrol(struct mtd_info *mtd, int cmd)
  66. {
  67. switch (cmd) {
  68. case NAND_CTL_SETCLE: T_NAND_CTL_SETCLE(cmd); break;
  69. case NAND_CTL_CLRCLE: T_NAND_CTL_CLRCLE(cmd); break;
  70. case NAND_CTL_SETALE: T_NAND_CTL_SETALE(cmd); break;
  71. case NAND_CTL_CLRALE: T_NAND_CTL_CLRALE(cmd); break;
  72. case NAND_CTL_SETNCE: T_NAND_CTL_SETNCE(cmd); break;
  73. case NAND_CTL_CLRNCE: T_NAND_CTL_CLRNCE(cmd); break;
  74. }
  75. }
  76. static void ams_delta_write_byte(struct mtd_info *mtd, u_char byte)
  77. {
  78. struct nand_chip *this = mtd->priv;
  79. omap_writew(0, (OMAP_MPUIO_BASE + OMAP_MPUIO_IO_CNTL));
  80. omap_writew(byte, this->IO_ADDR_W);
  81. ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NWE, 0);
  82. ndelay(40);
  83. ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NWE,
  84. AMS_DELTA_LATCH2_NAND_NWE);
  85. }
  86. static u_char ams_delta_read_byte(struct mtd_info *mtd)
  87. {
  88. u_char res;
  89. struct nand_chip *this = mtd->priv;
  90. ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NRE, 0);
  91. ndelay(40);
  92. omap_writew(~0, (OMAP_MPUIO_BASE + OMAP_MPUIO_IO_CNTL));
  93. res = omap_readw(this->IO_ADDR_R);
  94. ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NRE,
  95. AMS_DELTA_LATCH2_NAND_NRE);
  96. return res;
  97. }
  98. static void ams_delta_write_buf(struct mtd_info *mtd, const u_char *buf,
  99. int len)
  100. {
  101. int i;
  102. for (i=0; i<len; i++)
  103. ams_delta_write_byte(mtd, buf[i]);
  104. }
  105. static void ams_delta_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  106. {
  107. int i;
  108. for (i=0; i<len; i++)
  109. buf[i] = ams_delta_read_byte(mtd);
  110. }
  111. static int ams_delta_verify_buf(struct mtd_info *mtd, const u_char *buf,
  112. int len)
  113. {
  114. int i;
  115. for (i=0; i<len; i++)
  116. if (buf[i] != ams_delta_read_byte(mtd))
  117. return -EFAULT;
  118. return 0;
  119. }
  120. static int ams_delta_nand_ready(struct mtd_info *mtd)
  121. {
  122. return omap_get_gpio_datain(AMS_DELTA_GPIO_PIN_NAND_RB);
  123. }
  124. /*
  125. * Main initialization routine
  126. */
  127. static int __init ams_delta_init(void)
  128. {
  129. struct nand_chip *this;
  130. int err = 0;
  131. /* Allocate memory for MTD device structure and private data */
  132. ams_delta_mtd = kmalloc(sizeof(struct mtd_info) +
  133. sizeof(struct nand_chip), GFP_KERNEL);
  134. if (!ams_delta_mtd) {
  135. printk (KERN_WARNING "Unable to allocate E3 NAND MTD device structure.\n");
  136. err = -ENOMEM;
  137. goto out;
  138. }
  139. ams_delta_mtd->owner = THIS_MODULE;
  140. /* Get pointer to private data */
  141. this = (struct nand_chip *) (&ams_delta_mtd[1]);
  142. /* Initialize structures */
  143. memset(ams_delta_mtd, 0, sizeof(struct mtd_info));
  144. memset(this, 0, sizeof(struct nand_chip));
  145. /* Link the private data with the MTD structure */
  146. ams_delta_mtd->priv = this;
  147. /* Set address of NAND IO lines */
  148. this->IO_ADDR_R = (OMAP_MPUIO_BASE + OMAP_MPUIO_INPUT_LATCH);
  149. this->IO_ADDR_W = (OMAP_MPUIO_BASE + OMAP_MPUIO_OUTPUT);
  150. this->read_byte = ams_delta_read_byte;
  151. this->write_byte = ams_delta_write_byte;
  152. this->write_buf = ams_delta_write_buf;
  153. this->read_buf = ams_delta_read_buf;
  154. this->verify_buf = ams_delta_verify_buf;
  155. this->hwcontrol = ams_delta_hwcontrol;
  156. if (!omap_request_gpio(AMS_DELTA_GPIO_PIN_NAND_RB)) {
  157. this->dev_ready = ams_delta_nand_ready;
  158. } else {
  159. this->dev_ready = NULL;
  160. printk(KERN_NOTICE "Couldn't request gpio for Delta NAND ready.\n");
  161. }
  162. /* 25 us command delay time */
  163. this->chip_delay = 30;
  164. this->ecc.mode = NAND_ECC_SOFT;
  165. /* Set chip enabled, but */
  166. ams_delta_latch2_write(NAND_MASK, AMS_DELTA_LATCH2_NAND_NRE |
  167. AMS_DELTA_LATCH2_NAND_NWE |
  168. AMS_DELTA_LATCH2_NAND_NCE |
  169. AMS_DELTA_LATCH2_NAND_NWP);
  170. /* Scan to find existance of the device */
  171. if (nand_scan(ams_delta_mtd, 1)) {
  172. err = -ENXIO;
  173. goto out_mtd;
  174. }
  175. /* Register the partitions */
  176. add_mtd_partitions(ams_delta_mtd, partition_info,
  177. ARRAY_SIZE(partition_info));
  178. goto out;
  179. out_mtd:
  180. kfree(ams_delta_mtd);
  181. out:
  182. return err;
  183. }
  184. module_init(ams_delta_init);
  185. /*
  186. * Clean up routine
  187. */
  188. static void __exit ams_delta_cleanup(void)
  189. {
  190. /* Release resources, unregister device */
  191. nand_release(ams_delta_mtd);
  192. /* Free the MTD device structure */
  193. kfree(ams_delta_mtd);
  194. }
  195. module_exit(ams_delta_cleanup);
  196. MODULE_LICENSE("GPL");
  197. MODULE_AUTHOR("Jonathan McDowell <noodles@earth.li>");
  198. MODULE_DESCRIPTION("Glue layer for NAND flash on Amstrad E3 (Delta)");