orion_nand.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. ret = -ENOMEM;
  80. goto no_res;
  81. }
  82. mtd = (struct mtd_info *)(nc + 1);
  83. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  84. if (!res) {
  85. ret = -ENODEV;
  86. goto no_res;
  87. }
  88. io_base = ioremap(res->start, resource_size(res));
  89. if (!io_base) {
  90. dev_err(&pdev->dev, "ioremap failed\n");
  91. ret = -EIO;
  92. goto no_res;
  93. }
  94. if (pdev->dev.of_node) {
  95. board = devm_kzalloc(&pdev->dev, sizeof(struct orion_nand_data),
  96. GFP_KERNEL);
  97. if (!board) {
  98. ret = -ENOMEM;
  99. goto no_res;
  100. }
  101. if (!of_property_read_u32(pdev->dev.of_node, "cle", &val))
  102. board->cle = (u8)val;
  103. else
  104. board->cle = 0;
  105. if (!of_property_read_u32(pdev->dev.of_node, "ale", &val))
  106. board->ale = (u8)val;
  107. else
  108. board->ale = 1;
  109. if (!of_property_read_u32(pdev->dev.of_node,
  110. "bank-width", &val))
  111. board->width = (u8)val * 8;
  112. else
  113. board->width = 8;
  114. if (!of_property_read_u32(pdev->dev.of_node,
  115. "chip-delay", &val))
  116. board->chip_delay = (u8)val;
  117. } else {
  118. board = dev_get_platdata(&pdev->dev);
  119. }
  120. mtd->priv = nc;
  121. mtd->owner = THIS_MODULE;
  122. nc->priv = board;
  123. nc->IO_ADDR_R = nc->IO_ADDR_W = io_base;
  124. nc->cmd_ctrl = orion_nand_cmd_ctrl;
  125. nc->read_buf = orion_nand_read_buf;
  126. nc->ecc.mode = NAND_ECC_SOFT;
  127. if (board->chip_delay)
  128. nc->chip_delay = board->chip_delay;
  129. WARN(board->width > 16,
  130. "%d bit bus width out of range",
  131. board->width);
  132. if (board->width == 16)
  133. nc->options |= NAND_BUSWIDTH_16;
  134. if (board->dev_ready)
  135. nc->dev_ready = board->dev_ready;
  136. platform_set_drvdata(pdev, mtd);
  137. /* Not all platforms can gate the clock, so it is not
  138. an error if the clock does not exists. */
  139. clk = clk_get(&pdev->dev, NULL);
  140. if (!IS_ERR(clk)) {
  141. clk_prepare_enable(clk);
  142. clk_put(clk);
  143. }
  144. if (nand_scan(mtd, 1)) {
  145. ret = -ENXIO;
  146. goto no_dev;
  147. }
  148. mtd->name = "orion_nand";
  149. ppdata.of_node = pdev->dev.of_node;
  150. ret = mtd_device_parse_register(mtd, NULL, &ppdata,
  151. board->parts, board->nr_parts);
  152. if (ret) {
  153. nand_release(mtd);
  154. goto no_dev;
  155. }
  156. return 0;
  157. no_dev:
  158. if (!IS_ERR(clk)) {
  159. clk_disable_unprepare(clk);
  160. clk_put(clk);
  161. }
  162. iounmap(io_base);
  163. no_res:
  164. kfree(nc);
  165. return ret;
  166. }
  167. static int orion_nand_remove(struct platform_device *pdev)
  168. {
  169. struct mtd_info *mtd = platform_get_drvdata(pdev);
  170. struct nand_chip *nc = mtd->priv;
  171. struct clk *clk;
  172. nand_release(mtd);
  173. iounmap(nc->IO_ADDR_W);
  174. kfree(nc);
  175. clk = clk_get(&pdev->dev, NULL);
  176. if (!IS_ERR(clk)) {
  177. clk_disable_unprepare(clk);
  178. clk_put(clk);
  179. }
  180. return 0;
  181. }
  182. #ifdef CONFIG_OF
  183. static const struct of_device_id orion_nand_of_match_table[] = {
  184. { .compatible = "marvell,orion-nand", },
  185. {},
  186. };
  187. #endif
  188. static struct platform_driver orion_nand_driver = {
  189. .remove = orion_nand_remove,
  190. .driver = {
  191. .name = "orion_nand",
  192. .owner = THIS_MODULE,
  193. .of_match_table = of_match_ptr(orion_nand_of_match_table),
  194. },
  195. };
  196. module_platform_driver_probe(orion_nand_driver, orion_nand_probe);
  197. MODULE_LICENSE("GPL");
  198. MODULE_AUTHOR("Tzachi Perelstein");
  199. MODULE_DESCRIPTION("NAND glue for Orion platforms");
  200. MODULE_ALIAS("platform:orion_nand");