orion_nand.c 5.2 KB

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