ams-delta.c 6.8 KB

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