oxnas_nand.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Oxford Semiconductor OXNAS NAND driver
  4. * Copyright (C) 2016 Neil Armstrong <[email protected]>
  5. * Heavily based on plat_nand.c :
  6. * Author: Vitaly Wool <[email protected]>
  7. * Copyright (C) 2013 Ma Haijun <[email protected]>
  8. * Copyright (C) 2012 John Crispin <[email protected]>
  9. */
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/clk.h>
  16. #include <linux/reset.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/mtd/rawnand.h>
  19. #include <linux/mtd/partitions.h>
  20. #include <linux/of.h>
  21. /* Nand commands */
  22. #define OXNAS_NAND_CMD_ALE BIT(18)
  23. #define OXNAS_NAND_CMD_CLE BIT(19)
  24. #define OXNAS_NAND_MAX_CHIPS 1
  25. struct oxnas_nand_ctrl {
  26. struct nand_controller base;
  27. void __iomem *io_base;
  28. struct clk *clk;
  29. struct nand_chip *chips[OXNAS_NAND_MAX_CHIPS];
  30. unsigned int nchips;
  31. };
  32. static uint8_t oxnas_nand_read_byte(struct nand_chip *chip)
  33. {
  34. struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
  35. return readb(oxnas->io_base);
  36. }
  37. static void oxnas_nand_read_buf(struct nand_chip *chip, u8 *buf, int len)
  38. {
  39. struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
  40. ioread8_rep(oxnas->io_base, buf, len);
  41. }
  42. static void oxnas_nand_write_buf(struct nand_chip *chip, const u8 *buf,
  43. int len)
  44. {
  45. struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
  46. iowrite8_rep(oxnas->io_base, buf, len);
  47. }
  48. /* Single CS command control */
  49. static void oxnas_nand_cmd_ctrl(struct nand_chip *chip, int cmd,
  50. unsigned int ctrl)
  51. {
  52. struct oxnas_nand_ctrl *oxnas = nand_get_controller_data(chip);
  53. if (ctrl & NAND_CLE)
  54. writeb(cmd, oxnas->io_base + OXNAS_NAND_CMD_CLE);
  55. else if (ctrl & NAND_ALE)
  56. writeb(cmd, oxnas->io_base + OXNAS_NAND_CMD_ALE);
  57. }
  58. /*
  59. * Probe for the NAND device.
  60. */
  61. static int oxnas_nand_probe(struct platform_device *pdev)
  62. {
  63. struct device_node *np = pdev->dev.of_node;
  64. struct device_node *nand_np;
  65. struct oxnas_nand_ctrl *oxnas;
  66. struct nand_chip *chip;
  67. struct mtd_info *mtd;
  68. int count = 0;
  69. int err = 0;
  70. int i;
  71. /* Allocate memory for the device structure (and zero it) */
  72. oxnas = devm_kzalloc(&pdev->dev, sizeof(*oxnas),
  73. GFP_KERNEL);
  74. if (!oxnas)
  75. return -ENOMEM;
  76. nand_controller_init(&oxnas->base);
  77. oxnas->io_base = devm_platform_ioremap_resource(pdev, 0);
  78. if (IS_ERR(oxnas->io_base))
  79. return PTR_ERR(oxnas->io_base);
  80. oxnas->clk = devm_clk_get(&pdev->dev, NULL);
  81. if (IS_ERR(oxnas->clk))
  82. oxnas->clk = NULL;
  83. /* Only a single chip node is supported */
  84. count = of_get_child_count(np);
  85. if (count > 1)
  86. return -EINVAL;
  87. err = clk_prepare_enable(oxnas->clk);
  88. if (err)
  89. return err;
  90. device_reset_optional(&pdev->dev);
  91. for_each_child_of_node(np, nand_np) {
  92. chip = devm_kzalloc(&pdev->dev, sizeof(struct nand_chip),
  93. GFP_KERNEL);
  94. if (!chip) {
  95. err = -ENOMEM;
  96. goto err_release_child;
  97. }
  98. chip->controller = &oxnas->base;
  99. nand_set_flash_node(chip, nand_np);
  100. nand_set_controller_data(chip, oxnas);
  101. mtd = nand_to_mtd(chip);
  102. mtd->dev.parent = &pdev->dev;
  103. mtd->priv = chip;
  104. chip->legacy.cmd_ctrl = oxnas_nand_cmd_ctrl;
  105. chip->legacy.read_buf = oxnas_nand_read_buf;
  106. chip->legacy.read_byte = oxnas_nand_read_byte;
  107. chip->legacy.write_buf = oxnas_nand_write_buf;
  108. chip->legacy.chip_delay = 30;
  109. /* Scan to find existence of the device */
  110. err = nand_scan(chip, 1);
  111. if (err)
  112. goto err_release_child;
  113. err = mtd_device_register(mtd, NULL, 0);
  114. if (err)
  115. goto err_cleanup_nand;
  116. oxnas->chips[oxnas->nchips++] = chip;
  117. }
  118. /* Exit if no chips found */
  119. if (!oxnas->nchips) {
  120. err = -ENODEV;
  121. goto err_clk_unprepare;
  122. }
  123. platform_set_drvdata(pdev, oxnas);
  124. return 0;
  125. err_cleanup_nand:
  126. nand_cleanup(chip);
  127. err_release_child:
  128. of_node_put(nand_np);
  129. for (i = 0; i < oxnas->nchips; i++) {
  130. chip = oxnas->chips[i];
  131. WARN_ON(mtd_device_unregister(nand_to_mtd(chip)));
  132. nand_cleanup(chip);
  133. }
  134. err_clk_unprepare:
  135. clk_disable_unprepare(oxnas->clk);
  136. return err;
  137. }
  138. static int oxnas_nand_remove(struct platform_device *pdev)
  139. {
  140. struct oxnas_nand_ctrl *oxnas = platform_get_drvdata(pdev);
  141. struct nand_chip *chip;
  142. int i;
  143. for (i = 0; i < oxnas->nchips; i++) {
  144. chip = oxnas->chips[i];
  145. WARN_ON(mtd_device_unregister(nand_to_mtd(chip)));
  146. nand_cleanup(chip);
  147. }
  148. clk_disable_unprepare(oxnas->clk);
  149. return 0;
  150. }
  151. static const struct of_device_id oxnas_nand_match[] = {
  152. { .compatible = "oxsemi,ox820-nand" },
  153. {},
  154. };
  155. MODULE_DEVICE_TABLE(of, oxnas_nand_match);
  156. static struct platform_driver oxnas_nand_driver = {
  157. .probe = oxnas_nand_probe,
  158. .remove = oxnas_nand_remove,
  159. .driver = {
  160. .name = "oxnas_nand",
  161. .of_match_table = oxnas_nand_match,
  162. },
  163. };
  164. module_platform_driver(oxnas_nand_driver);
  165. MODULE_LICENSE("GPL");
  166. MODULE_AUTHOR("Neil Armstrong <[email protected]>");
  167. MODULE_DESCRIPTION("Oxnas NAND driver");
  168. MODULE_ALIAS("platform:oxnas_nand");