orion_nand.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * drivers/mtd/nand/orion_nand.c
  3. *
  4. * NAND support for Marvell Orion SoC platforms
  5. *
  6. * Tzachi Perelstein <tzachi@marvell.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/of.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/nand.h>
  18. #include <linux/mtd/partitions.h>
  19. #include <linux/clk.h>
  20. #include <linux/err.h>
  21. #include <asm/io.h>
  22. #include <asm/sizes.h>
  23. #include <mach/hardware.h>
  24. #include <plat/orion_nand.h>
  25. static void orion_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  26. {
  27. struct nand_chip *nc = mtd->priv;
  28. struct orion_nand_data *board = nc->priv;
  29. u32 offs;
  30. if (cmd == NAND_CMD_NONE)
  31. return;
  32. if (ctrl & NAND_CLE)
  33. offs = (1 << board->cle);
  34. else if (ctrl & NAND_ALE)
  35. offs = (1 << board->ale);
  36. else
  37. return;
  38. if (nc->options & NAND_BUSWIDTH_16)
  39. offs <<= 1;
  40. writeb(cmd, nc->IO_ADDR_W + offs);
  41. }
  42. static void orion_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  43. {
  44. struct nand_chip *chip = mtd->priv;
  45. void __iomem *io_base = chip->IO_ADDR_R;
  46. uint64_t *buf64;
  47. int i = 0;
  48. while (len && (unsigned long)buf & 7) {
  49. *buf++ = readb(io_base);
  50. len--;
  51. }
  52. buf64 = (uint64_t *)buf;
  53. while (i < len/8) {
  54. /*
  55. * Since GCC has no proper constraint (PR 43518)
  56. * force x variable to r2/r3 registers as ldrd instruction
  57. * requires first register to be even.
  58. */
  59. register uint64_t x asm ("r2");
  60. asm volatile ("ldrd\t%0, [%1]" : "=&r" (x) : "r" (io_base));
  61. buf64[i++] = x;
  62. }
  63. i *= 8;
  64. while (i < len)
  65. buf[i++] = readb(io_base);
  66. }
  67. static int __init orion_nand_probe(struct platform_device *pdev)
  68. {
  69. struct mtd_info *mtd;
  70. struct mtd_part_parser_data ppdata = {};
  71. struct nand_chip *nc;
  72. struct orion_nand_data *board;
  73. struct resource *res;
  74. struct clk *clk;
  75. void __iomem *io_base;
  76. int ret = 0;
  77. u32 val = 0;
  78. nc = kzalloc(sizeof(struct nand_chip) + sizeof(struct mtd_info), GFP_KERNEL);
  79. if (!nc) {
  80. printk(KERN_ERR "orion_nand: failed to allocate device structure.\n");
  81. ret = -ENOMEM;
  82. goto no_res;
  83. }
  84. mtd = (struct mtd_info *)(nc + 1);
  85. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  86. if (!res) {
  87. ret = -ENODEV;
  88. goto no_res;
  89. }
  90. io_base = ioremap(res->start, resource_size(res));
  91. if (!io_base) {
  92. printk(KERN_ERR "orion_nand: ioremap failed\n");
  93. ret = -EIO;
  94. goto no_res;
  95. }
  96. if (pdev->dev.of_node) {
  97. board = devm_kzalloc(&pdev->dev, sizeof(struct orion_nand_data),
  98. GFP_KERNEL);
  99. if (!board) {
  100. printk(KERN_ERR "orion_nand: failed to allocate board structure.\n");
  101. ret = -ENOMEM;
  102. goto no_res;
  103. }
  104. if (!of_property_read_u32(pdev->dev.of_node, "cle", &val))
  105. board->cle = (u8)val;
  106. else
  107. board->cle = 0;
  108. if (!of_property_read_u32(pdev->dev.of_node, "ale", &val))
  109. board->ale = (u8)val;
  110. else
  111. board->ale = 1;
  112. if (!of_property_read_u32(pdev->dev.of_node,
  113. "bank-width", &val))
  114. board->width = (u8)val * 8;
  115. else
  116. board->width = 8;
  117. if (!of_property_read_u32(pdev->dev.of_node,
  118. "chip-delay", &val))
  119. board->chip_delay = (u8)val;
  120. } else
  121. board = pdev->dev.platform_data;
  122. mtd->priv = nc;
  123. mtd->owner = THIS_MODULE;
  124. nc->priv = board;
  125. nc->IO_ADDR_R = nc->IO_ADDR_W = io_base;
  126. nc->cmd_ctrl = orion_nand_cmd_ctrl;
  127. nc->read_buf = orion_nand_read_buf;
  128. nc->ecc.mode = NAND_ECC_SOFT;
  129. if (board->chip_delay)
  130. nc->chip_delay = board->chip_delay;
  131. WARN(board->width > 16,
  132. "%d bit bus width out of range",
  133. board->width);
  134. if (board->width == 16)
  135. nc->options |= NAND_BUSWIDTH_16;
  136. if (board->dev_ready)
  137. nc->dev_ready = board->dev_ready;
  138. platform_set_drvdata(pdev, mtd);
  139. /* Not all platforms can gate the clock, so it is not
  140. an error if the clock does not exists. */
  141. clk = clk_get(&pdev->dev, NULL);
  142. if (!IS_ERR(clk)) {
  143. clk_prepare_enable(clk);
  144. clk_put(clk);
  145. }
  146. if (nand_scan(mtd, 1)) {
  147. ret = -ENXIO;
  148. goto no_dev;
  149. }
  150. mtd->name = "orion_nand";
  151. ppdata.of_node = pdev->dev.of_node;
  152. ret = mtd_device_parse_register(mtd, NULL, &ppdata,
  153. board->parts, board->nr_parts);
  154. if (ret) {
  155. nand_release(mtd);
  156. goto no_dev;
  157. }
  158. return 0;
  159. no_dev:
  160. if (!IS_ERR(clk)) {
  161. clk_disable_unprepare(clk);
  162. clk_put(clk);
  163. }
  164. platform_set_drvdata(pdev, NULL);
  165. iounmap(io_base);
  166. no_res:
  167. kfree(nc);
  168. return ret;
  169. }
  170. static int __devexit orion_nand_remove(struct platform_device *pdev)
  171. {
  172. struct mtd_info *mtd = platform_get_drvdata(pdev);
  173. struct nand_chip *nc = mtd->priv;
  174. struct clk *clk;
  175. nand_release(mtd);
  176. iounmap(nc->IO_ADDR_W);
  177. kfree(nc);
  178. clk = clk_get(&pdev->dev, NULL);
  179. if (!IS_ERR(clk)) {
  180. clk_disable_unprepare(clk);
  181. clk_put(clk);
  182. }
  183. return 0;
  184. }
  185. #ifdef CONFIG_OF
  186. static struct of_device_id orion_nand_of_match_table[] = {
  187. { .compatible = "marvell,orion-nand", },
  188. {},
  189. };
  190. #endif
  191. static struct platform_driver orion_nand_driver = {
  192. .remove = __devexit_p(orion_nand_remove),
  193. .driver = {
  194. .name = "orion_nand",
  195. .owner = THIS_MODULE,
  196. .of_match_table = of_match_ptr(orion_nand_of_match_table),
  197. },
  198. };
  199. static int __init orion_nand_init(void)
  200. {
  201. return platform_driver_probe(&orion_nand_driver, orion_nand_probe);
  202. }
  203. static void __exit orion_nand_exit(void)
  204. {
  205. platform_driver_unregister(&orion_nand_driver);
  206. }
  207. module_init(orion_nand_init);
  208. module_exit(orion_nand_exit);
  209. MODULE_LICENSE("GPL");
  210. MODULE_AUTHOR("Tzachi Perelstein");
  211. MODULE_DESCRIPTION("NAND glue for Orion platforms");
  212. MODULE_ALIAS("platform:orion_nand");