oxnas_nand.c 5.1 KB

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