ams-delta.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. * Converted to platform driver by Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
  8. * Partially stolen from drivers/mtd/nand/plat_nand.c
  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. * Amstrad E3 (Delta).
  17. */
  18. #include <linux/slab.h>
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/delay.h>
  22. #include <linux/mtd/mtd.h>
  23. #include <linux/mtd/nand.h>
  24. #include <linux/mtd/partitions.h>
  25. #include <linux/gpio.h>
  26. #include <linux/platform_data/gpio-omap.h>
  27. #include <asm/io.h>
  28. #include <asm/sizes.h>
  29. #include <mach/board-ams-delta.h>
  30. #include <mach/hardware.h>
  31. /*
  32. * MTD structure for E3 (Delta)
  33. */
  34. static struct mtd_info *ams_delta_mtd = NULL;
  35. /*
  36. * Define partitions for flash devices
  37. */
  38. static struct mtd_partition partition_info[] = {
  39. { .name = "Kernel",
  40. .offset = 0,
  41. .size = 3 * SZ_1M + SZ_512K },
  42. { .name = "u-boot",
  43. .offset = 3 * SZ_1M + SZ_512K,
  44. .size = SZ_256K },
  45. { .name = "u-boot params",
  46. .offset = 3 * SZ_1M + SZ_512K + SZ_256K,
  47. .size = SZ_256K },
  48. { .name = "Amstrad LDR",
  49. .offset = 4 * SZ_1M,
  50. .size = SZ_256K },
  51. { .name = "File system",
  52. .offset = 4 * SZ_1M + 1 * SZ_256K,
  53. .size = 27 * SZ_1M },
  54. { .name = "PBL reserved",
  55. .offset = 32 * SZ_1M - 3 * SZ_256K,
  56. .size = 3 * SZ_256K },
  57. };
  58. static void ams_delta_write_byte(struct mtd_info *mtd, u_char byte)
  59. {
  60. struct nand_chip *this = mtd->priv;
  61. void __iomem *io_base = this->priv;
  62. writew(0, io_base + OMAP_MPUIO_IO_CNTL);
  63. writew(byte, this->IO_ADDR_W);
  64. gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NWE, 0);
  65. ndelay(40);
  66. gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NWE, 1);
  67. }
  68. static u_char ams_delta_read_byte(struct mtd_info *mtd)
  69. {
  70. u_char res;
  71. struct nand_chip *this = mtd->priv;
  72. void __iomem *io_base = this->priv;
  73. gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NRE, 0);
  74. ndelay(40);
  75. writew(~0, io_base + OMAP_MPUIO_IO_CNTL);
  76. res = readw(this->IO_ADDR_R);
  77. gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NRE, 1);
  78. return res;
  79. }
  80. static void ams_delta_write_buf(struct mtd_info *mtd, const u_char *buf,
  81. int len)
  82. {
  83. int i;
  84. for (i=0; i<len; i++)
  85. ams_delta_write_byte(mtd, buf[i]);
  86. }
  87. static void ams_delta_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  88. {
  89. int i;
  90. for (i=0; i<len; i++)
  91. buf[i] = ams_delta_read_byte(mtd);
  92. }
  93. /*
  94. * Command control function
  95. *
  96. * ctrl:
  97. * NAND_NCE: bit 0 -> bit 2
  98. * NAND_CLE: bit 1 -> bit 7
  99. * NAND_ALE: bit 2 -> bit 6
  100. */
  101. static void ams_delta_hwcontrol(struct mtd_info *mtd, int cmd,
  102. unsigned int ctrl)
  103. {
  104. if (ctrl & NAND_CTRL_CHANGE) {
  105. gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NCE,
  106. (ctrl & NAND_NCE) == 0);
  107. gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_CLE,
  108. (ctrl & NAND_CLE) != 0);
  109. gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_ALE,
  110. (ctrl & NAND_ALE) != 0);
  111. }
  112. if (cmd != NAND_CMD_NONE)
  113. ams_delta_write_byte(mtd, cmd);
  114. }
  115. static int ams_delta_nand_ready(struct mtd_info *mtd)
  116. {
  117. return gpio_get_value(AMS_DELTA_GPIO_PIN_NAND_RB);
  118. }
  119. static const struct gpio _mandatory_gpio[] = {
  120. {
  121. .gpio = AMS_DELTA_GPIO_PIN_NAND_NCE,
  122. .flags = GPIOF_OUT_INIT_HIGH,
  123. .label = "nand_nce",
  124. },
  125. {
  126. .gpio = AMS_DELTA_GPIO_PIN_NAND_NRE,
  127. .flags = GPIOF_OUT_INIT_HIGH,
  128. .label = "nand_nre",
  129. },
  130. {
  131. .gpio = AMS_DELTA_GPIO_PIN_NAND_NWP,
  132. .flags = GPIOF_OUT_INIT_HIGH,
  133. .label = "nand_nwp",
  134. },
  135. {
  136. .gpio = AMS_DELTA_GPIO_PIN_NAND_NWE,
  137. .flags = GPIOF_OUT_INIT_HIGH,
  138. .label = "nand_nwe",
  139. },
  140. {
  141. .gpio = AMS_DELTA_GPIO_PIN_NAND_ALE,
  142. .flags = GPIOF_OUT_INIT_LOW,
  143. .label = "nand_ale",
  144. },
  145. {
  146. .gpio = AMS_DELTA_GPIO_PIN_NAND_CLE,
  147. .flags = GPIOF_OUT_INIT_LOW,
  148. .label = "nand_cle",
  149. },
  150. };
  151. /*
  152. * Main initialization routine
  153. */
  154. static int ams_delta_init(struct platform_device *pdev)
  155. {
  156. struct nand_chip *this;
  157. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  158. void __iomem *io_base;
  159. int err = 0;
  160. if (!res)
  161. return -ENXIO;
  162. /* Allocate memory for MTD device structure and private data */
  163. ams_delta_mtd = kmalloc(sizeof(struct mtd_info) +
  164. sizeof(struct nand_chip), GFP_KERNEL);
  165. if (!ams_delta_mtd) {
  166. printk (KERN_WARNING "Unable to allocate E3 NAND MTD device structure.\n");
  167. err = -ENOMEM;
  168. goto out;
  169. }
  170. ams_delta_mtd->owner = THIS_MODULE;
  171. /* Get pointer to private data */
  172. this = (struct nand_chip *) (&ams_delta_mtd[1]);
  173. /* Initialize structures */
  174. memset(ams_delta_mtd, 0, sizeof(struct mtd_info));
  175. memset(this, 0, sizeof(struct nand_chip));
  176. /* Link the private data with the MTD structure */
  177. ams_delta_mtd->priv = this;
  178. /*
  179. * Don't try to request the memory region from here,
  180. * it should have been already requested from the
  181. * gpio-omap driver and requesting it again would fail.
  182. */
  183. io_base = ioremap(res->start, resource_size(res));
  184. if (io_base == NULL) {
  185. dev_err(&pdev->dev, "ioremap failed\n");
  186. err = -EIO;
  187. goto out_free;
  188. }
  189. this->priv = io_base;
  190. /* Set address of NAND IO lines */
  191. this->IO_ADDR_R = io_base + OMAP_MPUIO_INPUT_LATCH;
  192. this->IO_ADDR_W = io_base + OMAP_MPUIO_OUTPUT;
  193. this->read_byte = ams_delta_read_byte;
  194. this->write_buf = ams_delta_write_buf;
  195. this->read_buf = ams_delta_read_buf;
  196. this->cmd_ctrl = ams_delta_hwcontrol;
  197. if (gpio_request(AMS_DELTA_GPIO_PIN_NAND_RB, "nand_rdy") == 0) {
  198. this->dev_ready = ams_delta_nand_ready;
  199. } else {
  200. this->dev_ready = NULL;
  201. printk(KERN_NOTICE "Couldn't request gpio for Delta NAND ready.\n");
  202. }
  203. /* 25 us command delay time */
  204. this->chip_delay = 30;
  205. this->ecc.mode = NAND_ECC_SOFT;
  206. platform_set_drvdata(pdev, io_base);
  207. /* Set chip enabled, but */
  208. err = gpio_request_array(_mandatory_gpio, ARRAY_SIZE(_mandatory_gpio));
  209. if (err)
  210. goto out_gpio;
  211. /* Scan to find existence of the device */
  212. if (nand_scan(ams_delta_mtd, 1)) {
  213. err = -ENXIO;
  214. goto out_mtd;
  215. }
  216. /* Register the partitions */
  217. mtd_device_register(ams_delta_mtd, partition_info,
  218. ARRAY_SIZE(partition_info));
  219. goto out;
  220. out_mtd:
  221. gpio_free_array(_mandatory_gpio, ARRAY_SIZE(_mandatory_gpio));
  222. out_gpio:
  223. platform_set_drvdata(pdev, NULL);
  224. gpio_free(AMS_DELTA_GPIO_PIN_NAND_RB);
  225. iounmap(io_base);
  226. out_free:
  227. kfree(ams_delta_mtd);
  228. out:
  229. return err;
  230. }
  231. /*
  232. * Clean up routine
  233. */
  234. static int ams_delta_cleanup(struct platform_device *pdev)
  235. {
  236. void __iomem *io_base = platform_get_drvdata(pdev);
  237. /* Release resources, unregister device */
  238. nand_release(ams_delta_mtd);
  239. gpio_free_array(_mandatory_gpio, ARRAY_SIZE(_mandatory_gpio));
  240. gpio_free(AMS_DELTA_GPIO_PIN_NAND_RB);
  241. iounmap(io_base);
  242. /* Free the MTD device structure */
  243. kfree(ams_delta_mtd);
  244. return 0;
  245. }
  246. static struct platform_driver ams_delta_nand_driver = {
  247. .probe = ams_delta_init,
  248. .remove = ams_delta_cleanup,
  249. .driver = {
  250. .name = "ams-delta-nand",
  251. .owner = THIS_MODULE,
  252. },
  253. };
  254. module_platform_driver(ams_delta_nand_driver);
  255. MODULE_LICENSE("GPL");
  256. MODULE_AUTHOR("Jonathan McDowell <noodles@earth.li>");
  257. MODULE_DESCRIPTION("Glue layer for NAND flash on Amstrad E3 (Delta)");