orion_nand.c 5.2 KB

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