oxnas_nand.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/nand.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 {
  30. struct nand_hw_control base;
  31. void __iomem *io_base;
  32. struct clk *clk;
  33. struct nand_chip *chips[OXNAS_NAND_MAX_CHIPS];
  34. unsigned long ctrl;
  35. struct mtd_partition *partitions;
  36. int nr_partitions;
  37. };
  38. static uint8_t oxnas_nand_read_byte(struct mtd_info *mtd)
  39. {
  40. struct nand_chip *chip = mtd_to_nand(mtd);
  41. struct oxnas_nand *oxnas = nand_get_controller_data(chip);
  42. return readb(oxnas->io_base);
  43. }
  44. static void oxnas_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  45. {
  46. struct nand_chip *chip = mtd_to_nand(mtd);
  47. struct oxnas_nand *oxnas = nand_get_controller_data(chip);
  48. ioread8_rep(oxnas->io_base, buf, len);
  49. }
  50. static void oxnas_nand_write_buf(struct mtd_info *mtd,
  51. const uint8_t *buf, int len)
  52. {
  53. struct nand_chip *chip = mtd_to_nand(mtd);
  54. struct oxnas_nand *oxnas = nand_get_controller_data(chip);
  55. iowrite8_rep(oxnas->io_base + oxnas->ctrl, buf, len);
  56. }
  57. /* Single CS command control */
  58. static void oxnas_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
  59. unsigned int ctrl)
  60. {
  61. struct nand_chip *chip = mtd_to_nand(mtd);
  62. struct oxnas_nand *oxnas = nand_get_controller_data(chip);
  63. if (ctrl & NAND_CTRL_CHANGE) {
  64. if (ctrl & NAND_CLE)
  65. oxnas->ctrl = OXNAS_NAND_CMD_CLE;
  66. else if (ctrl & NAND_ALE)
  67. oxnas->ctrl = OXNAS_NAND_CMD_ALE;
  68. else
  69. oxnas->ctrl = 0;
  70. }
  71. if (cmd != NAND_CMD_NONE)
  72. writeb(cmd, oxnas->io_base + oxnas->ctrl);
  73. }
  74. /*
  75. * Probe for the NAND device.
  76. */
  77. static int oxnas_nand_probe(struct platform_device *pdev)
  78. {
  79. struct device_node *np = pdev->dev.of_node;
  80. struct device_node *nand_np;
  81. struct oxnas_nand *oxnas;
  82. struct nand_chip *chip;
  83. struct mtd_info *mtd;
  84. struct resource *res;
  85. int nchips = 0;
  86. int count = 0;
  87. int err = 0;
  88. /* Allocate memory for the device structure (and zero it) */
  89. oxnas = devm_kzalloc(&pdev->dev, sizeof(struct nand_chip),
  90. GFP_KERNEL);
  91. if (!oxnas)
  92. return -ENOMEM;
  93. nand_hw_control_init(&oxnas->base);
  94. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  95. oxnas->io_base = devm_ioremap_resource(&pdev->dev, res);
  96. if (IS_ERR(oxnas->io_base))
  97. return PTR_ERR(oxnas->io_base);
  98. oxnas->clk = devm_clk_get(&pdev->dev, NULL);
  99. if (IS_ERR(oxnas->clk))
  100. oxnas->clk = NULL;
  101. /* Only a single chip node is supported */
  102. count = of_get_child_count(np);
  103. if (count > 1)
  104. return -EINVAL;
  105. clk_prepare_enable(oxnas->clk);
  106. device_reset_optional(&pdev->dev);
  107. for_each_child_of_node(np, nand_np) {
  108. chip = devm_kzalloc(&pdev->dev, sizeof(struct nand_chip),
  109. GFP_KERNEL);
  110. if (!chip)
  111. return -ENOMEM;
  112. chip->controller = &oxnas->base;
  113. nand_set_flash_node(chip, nand_np);
  114. nand_set_controller_data(chip, oxnas);
  115. mtd = nand_to_mtd(chip);
  116. mtd->dev.parent = &pdev->dev;
  117. mtd->priv = chip;
  118. chip->cmd_ctrl = oxnas_nand_cmd_ctrl;
  119. chip->read_buf = oxnas_nand_read_buf;
  120. chip->read_byte = oxnas_nand_read_byte;
  121. chip->write_buf = oxnas_nand_write_buf;
  122. chip->chip_delay = 30;
  123. /* Scan to find existence of the device */
  124. err = nand_scan(mtd, 1);
  125. if (err)
  126. return err;
  127. err = mtd_device_register(mtd, NULL, 0);
  128. if (err) {
  129. nand_release(mtd);
  130. return err;
  131. }
  132. oxnas->chips[nchips] = chip;
  133. ++nchips;
  134. }
  135. /* Exit if no chips found */
  136. if (!nchips)
  137. return -ENODEV;
  138. platform_set_drvdata(pdev, oxnas);
  139. return 0;
  140. }
  141. static int oxnas_nand_remove(struct platform_device *pdev)
  142. {
  143. struct oxnas_nand *oxnas = platform_get_drvdata(pdev);
  144. if (oxnas->chips[0])
  145. nand_release(nand_to_mtd(oxnas->chips[0]));
  146. clk_disable_unprepare(oxnas->clk);
  147. return 0;
  148. }
  149. static const struct of_device_id oxnas_nand_match[] = {
  150. { .compatible = "oxsemi,ox820-nand" },
  151. {},
  152. };
  153. MODULE_DEVICE_TABLE(of, oxnas_nand_match);
  154. static struct platform_driver oxnas_nand_driver = {
  155. .probe = oxnas_nand_probe,
  156. .remove = oxnas_nand_remove,
  157. .driver = {
  158. .name = "oxnas_nand",
  159. .of_match_table = oxnas_nand_match,
  160. },
  161. };
  162. module_platform_driver(oxnas_nand_driver);
  163. MODULE_LICENSE("GPL");
  164. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  165. MODULE_DESCRIPTION("Oxnas NAND driver");
  166. MODULE_ALIAS("platform:oxnas_nand");