orion_nand.c 4.8 KB

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