orion_nand.c 4.9 KB

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