plat_nand.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic NAND driver
  4. *
  5. * Author: Vitaly Wool <[email protected]>
  6. */
  7. #include <linux/err.h>
  8. #include <linux/io.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/slab.h>
  12. #include <linux/mtd/mtd.h>
  13. #include <linux/mtd/platnand.h>
  14. struct plat_nand_data {
  15. struct nand_controller controller;
  16. struct nand_chip chip;
  17. void __iomem *io_base;
  18. };
  19. static int plat_nand_attach_chip(struct nand_chip *chip)
  20. {
  21. if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
  22. chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN)
  23. chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
  24. return 0;
  25. }
  26. static const struct nand_controller_ops plat_nand_ops = {
  27. .attach_chip = plat_nand_attach_chip,
  28. };
  29. /*
  30. * Probe for the NAND device.
  31. */
  32. static int plat_nand_probe(struct platform_device *pdev)
  33. {
  34. struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
  35. struct plat_nand_data *data;
  36. struct mtd_info *mtd;
  37. const char **part_types;
  38. int err = 0;
  39. if (!pdata) {
  40. dev_err(&pdev->dev, "platform_nand_data is missing\n");
  41. return -EINVAL;
  42. }
  43. if (pdata->chip.nr_chips < 1) {
  44. dev_err(&pdev->dev, "invalid number of chips specified\n");
  45. return -EINVAL;
  46. }
  47. /* Allocate memory for the device structure (and zero it) */
  48. data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
  49. GFP_KERNEL);
  50. if (!data)
  51. return -ENOMEM;
  52. data->controller.ops = &plat_nand_ops;
  53. nand_controller_init(&data->controller);
  54. data->chip.controller = &data->controller;
  55. data->io_base = devm_platform_ioremap_resource(pdev, 0);
  56. if (IS_ERR(data->io_base))
  57. return PTR_ERR(data->io_base);
  58. nand_set_flash_node(&data->chip, pdev->dev.of_node);
  59. mtd = nand_to_mtd(&data->chip);
  60. mtd->dev.parent = &pdev->dev;
  61. data->chip.legacy.IO_ADDR_R = data->io_base;
  62. data->chip.legacy.IO_ADDR_W = data->io_base;
  63. data->chip.legacy.cmd_ctrl = pdata->ctrl.cmd_ctrl;
  64. data->chip.legacy.dev_ready = pdata->ctrl.dev_ready;
  65. data->chip.legacy.select_chip = pdata->ctrl.select_chip;
  66. data->chip.legacy.write_buf = pdata->ctrl.write_buf;
  67. data->chip.legacy.read_buf = pdata->ctrl.read_buf;
  68. data->chip.legacy.chip_delay = pdata->chip.chip_delay;
  69. data->chip.options |= pdata->chip.options;
  70. data->chip.bbt_options |= pdata->chip.bbt_options;
  71. platform_set_drvdata(pdev, data);
  72. /* Handle any platform specific setup */
  73. if (pdata->ctrl.probe) {
  74. err = pdata->ctrl.probe(pdev);
  75. if (err)
  76. goto out;
  77. }
  78. /*
  79. * This driver assumes that the default ECC engine should be TYPE_SOFT.
  80. * Set ->engine_type before registering the NAND devices in order to
  81. * provide a driver specific default value.
  82. */
  83. data->chip.ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
  84. /* Scan to find existence of the device */
  85. err = nand_scan(&data->chip, pdata->chip.nr_chips);
  86. if (err)
  87. goto out;
  88. part_types = pdata->chip.part_probe_types;
  89. err = mtd_device_parse_register(mtd, part_types, NULL,
  90. pdata->chip.partitions,
  91. pdata->chip.nr_partitions);
  92. if (!err)
  93. return err;
  94. nand_cleanup(&data->chip);
  95. out:
  96. if (pdata->ctrl.remove)
  97. pdata->ctrl.remove(pdev);
  98. return err;
  99. }
  100. /*
  101. * Remove a NAND device.
  102. */
  103. static int plat_nand_remove(struct platform_device *pdev)
  104. {
  105. struct plat_nand_data *data = platform_get_drvdata(pdev);
  106. struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
  107. struct nand_chip *chip = &data->chip;
  108. int ret;
  109. ret = mtd_device_unregister(nand_to_mtd(chip));
  110. WARN_ON(ret);
  111. nand_cleanup(chip);
  112. if (pdata->ctrl.remove)
  113. pdata->ctrl.remove(pdev);
  114. return 0;
  115. }
  116. static const struct of_device_id plat_nand_match[] = {
  117. { .compatible = "gen_nand" },
  118. {},
  119. };
  120. MODULE_DEVICE_TABLE(of, plat_nand_match);
  121. static struct platform_driver plat_nand_driver = {
  122. .probe = plat_nand_probe,
  123. .remove = plat_nand_remove,
  124. .driver = {
  125. .name = "gen_nand",
  126. .of_match_table = plat_nand_match,
  127. },
  128. };
  129. module_platform_driver(plat_nand_driver);
  130. MODULE_LICENSE("GPL");
  131. MODULE_AUTHOR("Vitaly Wool");
  132. MODULE_DESCRIPTION("Simple generic NAND driver");
  133. MODULE_ALIAS("platform:gen_nand");