orion_nand.c 5.4 KB

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