orion_nand.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/rawnand.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. struct orion_nand_info {
  25. struct nand_chip chip;
  26. struct clk *clk;
  27. };
  28. static void orion_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  29. {
  30. struct nand_chip *nc = mtd_to_nand(mtd);
  31. struct orion_nand_data *board = nand_get_controller_data(nc);
  32. u32 offs;
  33. if (cmd == NAND_CMD_NONE)
  34. return;
  35. if (ctrl & NAND_CLE)
  36. offs = (1 << board->cle);
  37. else if (ctrl & NAND_ALE)
  38. offs = (1 << board->ale);
  39. else
  40. return;
  41. if (nc->options & NAND_BUSWIDTH_16)
  42. offs <<= 1;
  43. writeb(cmd, nc->IO_ADDR_W + offs);
  44. }
  45. static void orion_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  46. {
  47. struct nand_chip *chip = mtd_to_nand(mtd);
  48. void __iomem *io_base = chip->IO_ADDR_R;
  49. #if __LINUX_ARM_ARCH__ >= 5
  50. uint64_t *buf64;
  51. #endif
  52. int i = 0;
  53. while (len && (unsigned long)buf & 7) {
  54. *buf++ = readb(io_base);
  55. len--;
  56. }
  57. #if __LINUX_ARM_ARCH__ >= 5
  58. buf64 = (uint64_t *)buf;
  59. while (i < len/8) {
  60. /*
  61. * Since GCC has no proper constraint (PR 43518)
  62. * force x variable to r2/r3 registers as ldrd instruction
  63. * requires first register to be even.
  64. */
  65. register uint64_t x asm ("r2");
  66. asm volatile ("ldrd\t%0, [%1]" : "=&r" (x) : "r" (io_base));
  67. buf64[i++] = x;
  68. }
  69. i *= 8;
  70. #else
  71. readsl(io_base, buf, len/4);
  72. i = len / 4 * 4;
  73. #endif
  74. while (i < len)
  75. buf[i++] = readb(io_base);
  76. }
  77. static int __init orion_nand_probe(struct platform_device *pdev)
  78. {
  79. struct orion_nand_info *info;
  80. struct mtd_info *mtd;
  81. struct nand_chip *nc;
  82. struct orion_nand_data *board;
  83. struct resource *res;
  84. void __iomem *io_base;
  85. int ret = 0;
  86. u32 val = 0;
  87. info = devm_kzalloc(&pdev->dev,
  88. sizeof(struct orion_nand_info),
  89. GFP_KERNEL);
  90. if (!info)
  91. return -ENOMEM;
  92. nc = &info->chip;
  93. mtd = nand_to_mtd(nc);
  94. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  95. io_base = devm_ioremap_resource(&pdev->dev, res);
  96. if (IS_ERR(io_base))
  97. return PTR_ERR(io_base);
  98. if (pdev->dev.of_node) {
  99. board = devm_kzalloc(&pdev->dev, sizeof(struct orion_nand_data),
  100. GFP_KERNEL);
  101. if (!board)
  102. return -ENOMEM;
  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 = dev_get_platdata(&pdev->dev);
  121. }
  122. mtd->dev.parent = &pdev->dev;
  123. nand_set_controller_data(nc, board);
  124. nand_set_flash_node(nc, pdev->dev.of_node);
  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. nc->ecc.algo = NAND_ECC_HAMMING;
  130. if (board->chip_delay)
  131. nc->chip_delay = board->chip_delay;
  132. WARN(board->width > 16,
  133. "%d bit bus width out of range",
  134. board->width);
  135. if (board->width == 16)
  136. nc->options |= NAND_BUSWIDTH_16;
  137. if (board->dev_ready)
  138. nc->dev_ready = board->dev_ready;
  139. platform_set_drvdata(pdev, info);
  140. /* Not all platforms can gate the clock, so it is not
  141. an error if the clock does not exists. */
  142. info->clk = devm_clk_get(&pdev->dev, NULL);
  143. if (IS_ERR(info->clk)) {
  144. ret = PTR_ERR(info->clk);
  145. if (ret == -ENOENT) {
  146. info->clk = NULL;
  147. } else {
  148. dev_err(&pdev->dev, "failed to get clock!\n");
  149. return ret;
  150. }
  151. }
  152. ret = clk_prepare_enable(info->clk);
  153. if (ret) {
  154. dev_err(&pdev->dev, "failed to prepare clock!\n");
  155. return ret;
  156. }
  157. ret = nand_scan(mtd, 1);
  158. if (ret)
  159. goto no_dev;
  160. mtd->name = "orion_nand";
  161. ret = mtd_device_register(mtd, board->parts, board->nr_parts);
  162. if (ret) {
  163. nand_cleanup(nc);
  164. goto no_dev;
  165. }
  166. return 0;
  167. no_dev:
  168. clk_disable_unprepare(info->clk);
  169. return ret;
  170. }
  171. static int orion_nand_remove(struct platform_device *pdev)
  172. {
  173. struct orion_nand_info *info = platform_get_drvdata(pdev);
  174. struct nand_chip *chip = &info->chip;
  175. nand_release(chip);
  176. clk_disable_unprepare(info->clk);
  177. return 0;
  178. }
  179. #ifdef CONFIG_OF
  180. static const struct of_device_id orion_nand_of_match_table[] = {
  181. { .compatible = "marvell,orion-nand", },
  182. {},
  183. };
  184. MODULE_DEVICE_TABLE(of, orion_nand_of_match_table);
  185. #endif
  186. static struct platform_driver orion_nand_driver = {
  187. .remove = orion_nand_remove,
  188. .driver = {
  189. .name = "orion_nand",
  190. .of_match_table = of_match_ptr(orion_nand_of_match_table),
  191. },
  192. };
  193. module_platform_driver_probe(orion_nand_driver, orion_nand_probe);
  194. MODULE_LICENSE("GPL");
  195. MODULE_AUTHOR("Tzachi Perelstein");
  196. MODULE_DESCRIPTION("NAND glue for Orion platforms");
  197. MODULE_ALIAS("platform:orion_nand");