orion_nand.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. 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. uint64_t *buf64;
  50. int i = 0;
  51. while (len && (unsigned long)buf & 7) {
  52. *buf++ = readb(io_base);
  53. len--;
  54. }
  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. while (i < len)
  68. buf[i++] = readb(io_base);
  69. }
  70. static int __init orion_nand_probe(struct platform_device *pdev)
  71. {
  72. struct orion_nand_info *info;
  73. struct mtd_info *mtd;
  74. struct nand_chip *nc;
  75. struct orion_nand_data *board;
  76. struct resource *res;
  77. void __iomem *io_base;
  78. int ret = 0;
  79. u32 val = 0;
  80. info = devm_kzalloc(&pdev->dev,
  81. sizeof(struct orion_nand_info),
  82. GFP_KERNEL);
  83. if (!info)
  84. return -ENOMEM;
  85. nc = &info->chip;
  86. mtd = nand_to_mtd(nc);
  87. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  88. io_base = devm_ioremap_resource(&pdev->dev, res);
  89. if (IS_ERR(io_base))
  90. return PTR_ERR(io_base);
  91. if (pdev->dev.of_node) {
  92. board = devm_kzalloc(&pdev->dev, sizeof(struct orion_nand_data),
  93. GFP_KERNEL);
  94. if (!board)
  95. return -ENOMEM;
  96. if (!of_property_read_u32(pdev->dev.of_node, "cle", &val))
  97. board->cle = (u8)val;
  98. else
  99. board->cle = 0;
  100. if (!of_property_read_u32(pdev->dev.of_node, "ale", &val))
  101. board->ale = (u8)val;
  102. else
  103. board->ale = 1;
  104. if (!of_property_read_u32(pdev->dev.of_node,
  105. "bank-width", &val))
  106. board->width = (u8)val * 8;
  107. else
  108. board->width = 8;
  109. if (!of_property_read_u32(pdev->dev.of_node,
  110. "chip-delay", &val))
  111. board->chip_delay = (u8)val;
  112. } else {
  113. board = dev_get_platdata(&pdev->dev);
  114. }
  115. mtd->dev.parent = &pdev->dev;
  116. nand_set_controller_data(nc, board);
  117. nand_set_flash_node(nc, pdev->dev.of_node);
  118. nc->IO_ADDR_R = nc->IO_ADDR_W = io_base;
  119. nc->cmd_ctrl = orion_nand_cmd_ctrl;
  120. nc->read_buf = orion_nand_read_buf;
  121. nc->ecc.mode = NAND_ECC_SOFT;
  122. nc->ecc.algo = NAND_ECC_HAMMING;
  123. if (board->chip_delay)
  124. nc->chip_delay = board->chip_delay;
  125. WARN(board->width > 16,
  126. "%d bit bus width out of range",
  127. board->width);
  128. if (board->width == 16)
  129. nc->options |= NAND_BUSWIDTH_16;
  130. if (board->dev_ready)
  131. nc->dev_ready = board->dev_ready;
  132. platform_set_drvdata(pdev, info);
  133. /* Not all platforms can gate the clock, so it is not
  134. an error if the clock does not exists. */
  135. info->clk = devm_clk_get(&pdev->dev, NULL);
  136. if (!IS_ERR(info->clk))
  137. clk_prepare_enable(info->clk);
  138. if (nand_scan(mtd, 1)) {
  139. ret = -ENXIO;
  140. goto no_dev;
  141. }
  142. mtd->name = "orion_nand";
  143. ret = mtd_device_register(mtd, board->parts, board->nr_parts);
  144. if (ret) {
  145. nand_release(mtd);
  146. goto no_dev;
  147. }
  148. return 0;
  149. no_dev:
  150. if (!IS_ERR(info->clk))
  151. clk_disable_unprepare(info->clk);
  152. return ret;
  153. }
  154. static int orion_nand_remove(struct platform_device *pdev)
  155. {
  156. struct orion_nand_info *info = platform_get_drvdata(pdev);
  157. struct nand_chip *chip = &info->chip;
  158. struct mtd_info *mtd = nand_to_mtd(chip);
  159. nand_release(mtd);
  160. if (!IS_ERR(info->clk))
  161. clk_disable_unprepare(info->clk);
  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");