ams-delta.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. * Copyright (c) 2003 Texas Instruments
  6. * Copyright (c) 2002 Thomas Gleixner <tgxl@linutronix.de>
  7. *
  8. * Converted to platform driver by Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
  9. * Partially stolen from plat_nand.c
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * Overview:
  16. * This is a device driver for the NAND flash device found on the
  17. * Amstrad E3 (Delta).
  18. */
  19. #include <linux/slab.h>
  20. #include <linux/module.h>
  21. #include <linux/delay.h>
  22. #include <linux/gpio/consumer.h>
  23. #include <linux/mtd/mtd.h>
  24. #include <linux/mtd/rawnand.h>
  25. #include <linux/mtd/partitions.h>
  26. #include <linux/platform_data/gpio-omap.h>
  27. #include <asm/io.h>
  28. #include <asm/sizes.h>
  29. #include <mach/hardware.h>
  30. /*
  31. * MTD structure for E3 (Delta)
  32. */
  33. struct ams_delta_nand {
  34. struct nand_chip nand_chip;
  35. struct gpio_desc *gpiod_rdy;
  36. struct gpio_desc *gpiod_nce;
  37. struct gpio_desc *gpiod_nre;
  38. struct gpio_desc *gpiod_nwp;
  39. struct gpio_desc *gpiod_nwe;
  40. struct gpio_desc *gpiod_ale;
  41. struct gpio_desc *gpiod_cle;
  42. void __iomem *io_base;
  43. bool data_in;
  44. };
  45. /*
  46. * Define partitions for flash devices
  47. */
  48. static const struct mtd_partition partition_info[] = {
  49. { .name = "Kernel",
  50. .offset = 0,
  51. .size = 3 * SZ_1M + SZ_512K },
  52. { .name = "u-boot",
  53. .offset = 3 * SZ_1M + SZ_512K,
  54. .size = SZ_256K },
  55. { .name = "u-boot params",
  56. .offset = 3 * SZ_1M + SZ_512K + SZ_256K,
  57. .size = SZ_256K },
  58. { .name = "Amstrad LDR",
  59. .offset = 4 * SZ_1M,
  60. .size = SZ_256K },
  61. { .name = "File system",
  62. .offset = 4 * SZ_1M + 1 * SZ_256K,
  63. .size = 27 * SZ_1M },
  64. { .name = "PBL reserved",
  65. .offset = 32 * SZ_1M - 3 * SZ_256K,
  66. .size = 3 * SZ_256K },
  67. };
  68. static void ams_delta_io_write(struct ams_delta_nand *priv, u_char byte)
  69. {
  70. writew(byte, priv->nand_chip.legacy.IO_ADDR_W);
  71. gpiod_set_value(priv->gpiod_nwe, 0);
  72. ndelay(40);
  73. gpiod_set_value(priv->gpiod_nwe, 1);
  74. }
  75. static u_char ams_delta_io_read(struct ams_delta_nand *priv)
  76. {
  77. u_char res;
  78. gpiod_set_value(priv->gpiod_nre, 0);
  79. ndelay(40);
  80. res = readw(priv->nand_chip.legacy.IO_ADDR_R);
  81. gpiod_set_value(priv->gpiod_nre, 1);
  82. return res;
  83. }
  84. static void ams_delta_dir_input(struct ams_delta_nand *priv, bool in)
  85. {
  86. writew(in ? ~0 : 0, priv->io_base + OMAP_MPUIO_IO_CNTL);
  87. priv->data_in = in;
  88. }
  89. static void ams_delta_write_buf(struct nand_chip *this, const u_char *buf,
  90. int len)
  91. {
  92. struct ams_delta_nand *priv = nand_get_controller_data(this);
  93. int i;
  94. if (priv->data_in)
  95. ams_delta_dir_input(priv, false);
  96. for (i = 0; i < len; i++)
  97. ams_delta_io_write(priv, buf[i]);
  98. }
  99. static void ams_delta_read_buf(struct nand_chip *this, u_char *buf, int len)
  100. {
  101. struct ams_delta_nand *priv = nand_get_controller_data(this);
  102. int i;
  103. if (!priv->data_in)
  104. ams_delta_dir_input(priv, true);
  105. for (i = 0; i < len; i++)
  106. buf[i] = ams_delta_io_read(priv);
  107. }
  108. static u_char ams_delta_read_byte(struct nand_chip *this)
  109. {
  110. u_char res;
  111. ams_delta_read_buf(this, &res, 1);
  112. return res;
  113. }
  114. /*
  115. * Command control function
  116. *
  117. * ctrl:
  118. * NAND_NCE: bit 0 -> bit 2
  119. * NAND_CLE: bit 1 -> bit 7
  120. * NAND_ALE: bit 2 -> bit 6
  121. */
  122. static void ams_delta_hwcontrol(struct nand_chip *this, int cmd,
  123. unsigned int ctrl)
  124. {
  125. struct ams_delta_nand *priv = nand_get_controller_data(this);
  126. if (ctrl & NAND_CTRL_CHANGE) {
  127. gpiod_set_value(priv->gpiod_nce, !(ctrl & NAND_NCE));
  128. gpiod_set_value(priv->gpiod_cle, !!(ctrl & NAND_CLE));
  129. gpiod_set_value(priv->gpiod_ale, !!(ctrl & NAND_ALE));
  130. }
  131. if (cmd != NAND_CMD_NONE) {
  132. u_char byte = cmd;
  133. ams_delta_write_buf(this, &byte, 1);
  134. }
  135. }
  136. static int ams_delta_nand_ready(struct nand_chip *this)
  137. {
  138. struct ams_delta_nand *priv = nand_get_controller_data(this);
  139. return gpiod_get_value(priv->gpiod_rdy);
  140. }
  141. /*
  142. * Main initialization routine
  143. */
  144. static int ams_delta_init(struct platform_device *pdev)
  145. {
  146. struct ams_delta_nand *priv;
  147. struct nand_chip *this;
  148. struct mtd_info *mtd;
  149. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  150. void __iomem *io_base;
  151. int err = 0;
  152. if (!res)
  153. return -ENXIO;
  154. /* Allocate memory for MTD device structure and private data */
  155. priv = devm_kzalloc(&pdev->dev, sizeof(struct ams_delta_nand),
  156. GFP_KERNEL);
  157. if (!priv) {
  158. pr_warn("Unable to allocate E3 NAND MTD device structure.\n");
  159. return -ENOMEM;
  160. }
  161. this = &priv->nand_chip;
  162. mtd = nand_to_mtd(this);
  163. mtd->dev.parent = &pdev->dev;
  164. /*
  165. * Don't try to request the memory region from here,
  166. * it should have been already requested from the
  167. * gpio-omap driver and requesting it again would fail.
  168. */
  169. io_base = ioremap(res->start, resource_size(res));
  170. if (io_base == NULL) {
  171. dev_err(&pdev->dev, "ioremap failed\n");
  172. err = -EIO;
  173. goto out_free;
  174. }
  175. priv->io_base = io_base;
  176. nand_set_controller_data(this, priv);
  177. /* Set address of NAND IO lines */
  178. this->legacy.IO_ADDR_R = io_base + OMAP_MPUIO_INPUT_LATCH;
  179. this->legacy.IO_ADDR_W = io_base + OMAP_MPUIO_OUTPUT;
  180. this->legacy.read_byte = ams_delta_read_byte;
  181. this->legacy.write_buf = ams_delta_write_buf;
  182. this->legacy.read_buf = ams_delta_read_buf;
  183. this->legacy.cmd_ctrl = ams_delta_hwcontrol;
  184. priv->gpiod_rdy = devm_gpiod_get_optional(&pdev->dev, "rdy", GPIOD_IN);
  185. if (IS_ERR(priv->gpiod_rdy)) {
  186. err = PTR_ERR(priv->gpiod_rdy);
  187. dev_warn(&pdev->dev, "RDY GPIO request failed (%d)\n", err);
  188. goto out_mtd;
  189. }
  190. if (priv->gpiod_rdy)
  191. this->legacy.dev_ready = ams_delta_nand_ready;
  192. /* 25 us command delay time */
  193. this->legacy.chip_delay = 30;
  194. this->ecc.mode = NAND_ECC_SOFT;
  195. this->ecc.algo = NAND_ECC_HAMMING;
  196. platform_set_drvdata(pdev, priv);
  197. /* Set chip enabled, but */
  198. priv->gpiod_nwp = devm_gpiod_get(&pdev->dev, "nwp", GPIOD_OUT_HIGH);
  199. if (IS_ERR(priv->gpiod_nwp)) {
  200. err = PTR_ERR(priv->gpiod_nwp);
  201. dev_err(&pdev->dev, "NWP GPIO request failed (%d)\n", err);
  202. goto out_mtd;
  203. }
  204. priv->gpiod_nce = devm_gpiod_get(&pdev->dev, "nce", GPIOD_OUT_HIGH);
  205. if (IS_ERR(priv->gpiod_nce)) {
  206. err = PTR_ERR(priv->gpiod_nce);
  207. dev_err(&pdev->dev, "NCE GPIO request failed (%d)\n", err);
  208. goto out_mtd;
  209. }
  210. priv->gpiod_nre = devm_gpiod_get(&pdev->dev, "nre", GPIOD_OUT_HIGH);
  211. if (IS_ERR(priv->gpiod_nre)) {
  212. err = PTR_ERR(priv->gpiod_nre);
  213. dev_err(&pdev->dev, "NRE GPIO request failed (%d)\n", err);
  214. goto out_mtd;
  215. }
  216. priv->gpiod_nwe = devm_gpiod_get(&pdev->dev, "nwe", GPIOD_OUT_HIGH);
  217. if (IS_ERR(priv->gpiod_nwe)) {
  218. err = PTR_ERR(priv->gpiod_nwe);
  219. dev_err(&pdev->dev, "NWE GPIO request failed (%d)\n", err);
  220. goto out_mtd;
  221. }
  222. priv->gpiod_ale = devm_gpiod_get(&pdev->dev, "ale", GPIOD_OUT_LOW);
  223. if (IS_ERR(priv->gpiod_ale)) {
  224. err = PTR_ERR(priv->gpiod_ale);
  225. dev_err(&pdev->dev, "ALE GPIO request failed (%d)\n", err);
  226. goto out_mtd;
  227. }
  228. priv->gpiod_cle = devm_gpiod_get(&pdev->dev, "cle", GPIOD_OUT_LOW);
  229. if (IS_ERR(priv->gpiod_cle)) {
  230. err = PTR_ERR(priv->gpiod_cle);
  231. dev_err(&pdev->dev, "CLE GPIO request failed (%d)\n", err);
  232. goto out_mtd;
  233. }
  234. /* Initialize data port direction to a known state */
  235. ams_delta_dir_input(priv, true);
  236. /* Scan to find existence of the device */
  237. err = nand_scan(this, 1);
  238. if (err)
  239. goto out_mtd;
  240. /* Register the partitions */
  241. mtd_device_register(mtd, partition_info, ARRAY_SIZE(partition_info));
  242. goto out;
  243. out_mtd:
  244. iounmap(io_base);
  245. out_free:
  246. out:
  247. return err;
  248. }
  249. /*
  250. * Clean up routine
  251. */
  252. static int ams_delta_cleanup(struct platform_device *pdev)
  253. {
  254. struct ams_delta_nand *priv = platform_get_drvdata(pdev);
  255. struct mtd_info *mtd = nand_to_mtd(&priv->nand_chip);
  256. void __iomem *io_base = priv->io_base;
  257. /* Release resources, unregister device */
  258. nand_release(mtd_to_nand(mtd));
  259. iounmap(io_base);
  260. return 0;
  261. }
  262. static struct platform_driver ams_delta_nand_driver = {
  263. .probe = ams_delta_init,
  264. .remove = ams_delta_cleanup,
  265. .driver = {
  266. .name = "ams-delta-nand",
  267. },
  268. };
  269. module_platform_driver(ams_delta_nand_driver);
  270. MODULE_LICENSE("GPL");
  271. MODULE_AUTHOR("Jonathan McDowell <noodles@earth.li>");
  272. MODULE_DESCRIPTION("Glue layer for NAND flash on Amstrad E3 (Delta)");