ams-delta.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 <asm/io.h>
  26. #include <mach/hardware.h>
  27. #include <asm/sizes.h>
  28. #include <mach/gpio.h>
  29. #include <plat/board-ams-delta.h>
  30. /*
  31. * MTD structure for E3 (Delta)
  32. */
  33. static struct mtd_info *ams_delta_mtd = NULL;
  34. #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)
  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. ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NWE, 0);
  65. ndelay(40);
  66. ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NWE,
  67. AMS_DELTA_LATCH2_NAND_NWE);
  68. }
  69. static u_char ams_delta_read_byte(struct mtd_info *mtd)
  70. {
  71. u_char res;
  72. struct nand_chip *this = mtd->priv;
  73. void __iomem *io_base = this->priv;
  74. ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NRE, 0);
  75. ndelay(40);
  76. writew(~0, io_base + OMAP_MPUIO_IO_CNTL);
  77. res = readw(this->IO_ADDR_R);
  78. ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NRE,
  79. AMS_DELTA_LATCH2_NAND_NRE);
  80. return res;
  81. }
  82. static void ams_delta_write_buf(struct mtd_info *mtd, const u_char *buf,
  83. int len)
  84. {
  85. int i;
  86. for (i=0; i<len; i++)
  87. ams_delta_write_byte(mtd, buf[i]);
  88. }
  89. static void ams_delta_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  90. {
  91. int i;
  92. for (i=0; i<len; i++)
  93. buf[i] = ams_delta_read_byte(mtd);
  94. }
  95. static int ams_delta_verify_buf(struct mtd_info *mtd, const u_char *buf,
  96. int len)
  97. {
  98. int i;
  99. for (i=0; i<len; i++)
  100. if (buf[i] != ams_delta_read_byte(mtd))
  101. return -EFAULT;
  102. return 0;
  103. }
  104. /*
  105. * Command control function
  106. *
  107. * ctrl:
  108. * NAND_NCE: bit 0 -> bit 2
  109. * NAND_CLE: bit 1 -> bit 7
  110. * NAND_ALE: bit 2 -> bit 6
  111. */
  112. static void ams_delta_hwcontrol(struct mtd_info *mtd, int cmd,
  113. unsigned int ctrl)
  114. {
  115. if (ctrl & NAND_CTRL_CHANGE) {
  116. unsigned long bits;
  117. bits = (~ctrl & NAND_NCE) ? AMS_DELTA_LATCH2_NAND_NCE : 0;
  118. bits |= (ctrl & NAND_CLE) ? AMS_DELTA_LATCH2_NAND_CLE : 0;
  119. bits |= (ctrl & NAND_ALE) ? AMS_DELTA_LATCH2_NAND_ALE : 0;
  120. ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_CLE |
  121. AMS_DELTA_LATCH2_NAND_ALE |
  122. AMS_DELTA_LATCH2_NAND_NCE, bits);
  123. }
  124. if (cmd != NAND_CMD_NONE)
  125. ams_delta_write_byte(mtd, cmd);
  126. }
  127. static int ams_delta_nand_ready(struct mtd_info *mtd)
  128. {
  129. return gpio_get_value(AMS_DELTA_GPIO_PIN_NAND_RB);
  130. }
  131. /*
  132. * Main initialization routine
  133. */
  134. static int __devinit ams_delta_init(struct platform_device *pdev)
  135. {
  136. struct nand_chip *this;
  137. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  138. void __iomem *io_base;
  139. int err = 0;
  140. if (!res)
  141. return -ENXIO;
  142. /* Allocate memory for MTD device structure and private data */
  143. ams_delta_mtd = kmalloc(sizeof(struct mtd_info) +
  144. sizeof(struct nand_chip), GFP_KERNEL);
  145. if (!ams_delta_mtd) {
  146. printk (KERN_WARNING "Unable to allocate E3 NAND MTD device structure.\n");
  147. err = -ENOMEM;
  148. goto out;
  149. }
  150. ams_delta_mtd->owner = THIS_MODULE;
  151. /* Get pointer to private data */
  152. this = (struct nand_chip *) (&ams_delta_mtd[1]);
  153. /* Initialize structures */
  154. memset(ams_delta_mtd, 0, sizeof(struct mtd_info));
  155. memset(this, 0, sizeof(struct nand_chip));
  156. /* Link the private data with the MTD structure */
  157. ams_delta_mtd->priv = this;
  158. if (!request_mem_region(res->start, resource_size(res),
  159. dev_name(&pdev->dev))) {
  160. dev_err(&pdev->dev, "request_mem_region failed\n");
  161. err = -EBUSY;
  162. goto out_free;
  163. }
  164. io_base = ioremap(res->start, resource_size(res));
  165. if (io_base == NULL) {
  166. dev_err(&pdev->dev, "ioremap failed\n");
  167. err = -EIO;
  168. goto out_release_io;
  169. }
  170. this->priv = io_base;
  171. /* Set address of NAND IO lines */
  172. this->IO_ADDR_R = io_base + OMAP_MPUIO_INPUT_LATCH;
  173. this->IO_ADDR_W = io_base + OMAP_MPUIO_OUTPUT;
  174. this->read_byte = ams_delta_read_byte;
  175. this->write_buf = ams_delta_write_buf;
  176. this->read_buf = ams_delta_read_buf;
  177. this->verify_buf = ams_delta_verify_buf;
  178. this->cmd_ctrl = ams_delta_hwcontrol;
  179. if (gpio_request(AMS_DELTA_GPIO_PIN_NAND_RB, "nand_rdy") == 0) {
  180. this->dev_ready = ams_delta_nand_ready;
  181. } else {
  182. this->dev_ready = NULL;
  183. printk(KERN_NOTICE "Couldn't request gpio for Delta NAND ready.\n");
  184. }
  185. /* 25 us command delay time */
  186. this->chip_delay = 30;
  187. this->ecc.mode = NAND_ECC_SOFT;
  188. platform_set_drvdata(pdev, io_base);
  189. /* Set chip enabled, but */
  190. ams_delta_latch2_write(NAND_MASK, AMS_DELTA_LATCH2_NAND_NRE |
  191. AMS_DELTA_LATCH2_NAND_NWE |
  192. AMS_DELTA_LATCH2_NAND_NCE |
  193. AMS_DELTA_LATCH2_NAND_NWP);
  194. /* Scan to find existance of the device */
  195. if (nand_scan(ams_delta_mtd, 1)) {
  196. err = -ENXIO;
  197. goto out_mtd;
  198. }
  199. /* Register the partitions */
  200. add_mtd_partitions(ams_delta_mtd, partition_info,
  201. ARRAY_SIZE(partition_info));
  202. goto out;
  203. out_mtd:
  204. platform_set_drvdata(pdev, NULL);
  205. iounmap(io_base);
  206. out_release_io:
  207. release_mem_region(res->start, resource_size(res));
  208. out_free:
  209. kfree(ams_delta_mtd);
  210. out:
  211. return err;
  212. }
  213. /*
  214. * Clean up routine
  215. */
  216. static int __devexit ams_delta_cleanup(struct platform_device *pdev)
  217. {
  218. void __iomem *io_base = platform_get_drvdata(pdev);
  219. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  220. /* Release resources, unregister device */
  221. nand_release(ams_delta_mtd);
  222. iounmap(io_base);
  223. release_mem_region(res->start, resource_size(res));
  224. /* Free the MTD device structure */
  225. kfree(ams_delta_mtd);
  226. return 0;
  227. }
  228. static struct platform_driver ams_delta_nand_driver = {
  229. .probe = ams_delta_init,
  230. .remove = __devexit_p(ams_delta_cleanup),
  231. .driver = {
  232. .name = "ams-delta-nand",
  233. .owner = THIS_MODULE,
  234. },
  235. };
  236. static int __init ams_delta_nand_init(void)
  237. {
  238. return platform_driver_register(&ams_delta_nand_driver);
  239. }
  240. module_init(ams_delta_nand_init);
  241. static void __exit ams_delta_nand_exit(void)
  242. {
  243. platform_driver_unregister(&ams_delta_nand_driver);
  244. }
  245. module_exit(ams_delta_nand_exit);
  246. MODULE_LICENSE("GPL");
  247. MODULE_AUTHOR("Jonathan McDowell <noodles@earth.li>");
  248. MODULE_DESCRIPTION("Glue layer for NAND flash on Amstrad E3 (Delta)");