plat_nand.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Generic NAND driver
  3. *
  4. * Author: Vitaly Wool <vitalywool@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/nand.h>
  18. #include <linux/mtd/partitions.h>
  19. struct plat_nand_data {
  20. struct nand_chip chip;
  21. void __iomem *io_base;
  22. };
  23. /*
  24. * Probe for the NAND device.
  25. */
  26. static int plat_nand_probe(struct platform_device *pdev)
  27. {
  28. struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
  29. struct plat_nand_data *data;
  30. struct mtd_info *mtd;
  31. struct resource *res;
  32. const char **part_types;
  33. int err = 0;
  34. if (!pdata) {
  35. dev_err(&pdev->dev, "platform_nand_data is missing\n");
  36. return -EINVAL;
  37. }
  38. if (pdata->chip.nr_chips < 1) {
  39. dev_err(&pdev->dev, "invalid number of chips specified\n");
  40. return -EINVAL;
  41. }
  42. /* Allocate memory for the device structure (and zero it) */
  43. data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
  44. GFP_KERNEL);
  45. if (!data)
  46. return -ENOMEM;
  47. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  48. data->io_base = devm_ioremap_resource(&pdev->dev, res);
  49. if (IS_ERR(data->io_base))
  50. return PTR_ERR(data->io_base);
  51. nand_set_flash_node(&data->chip, pdev->dev.of_node);
  52. mtd = nand_to_mtd(&data->chip);
  53. mtd->dev.parent = &pdev->dev;
  54. data->chip.IO_ADDR_R = data->io_base;
  55. data->chip.IO_ADDR_W = data->io_base;
  56. data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
  57. data->chip.dev_ready = pdata->ctrl.dev_ready;
  58. data->chip.select_chip = pdata->ctrl.select_chip;
  59. data->chip.write_buf = pdata->ctrl.write_buf;
  60. data->chip.read_buf = pdata->ctrl.read_buf;
  61. data->chip.read_byte = pdata->ctrl.read_byte;
  62. data->chip.chip_delay = pdata->chip.chip_delay;
  63. data->chip.options |= pdata->chip.options;
  64. data->chip.bbt_options |= pdata->chip.bbt_options;
  65. data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
  66. data->chip.ecc.mode = NAND_ECC_SOFT;
  67. data->chip.ecc.algo = NAND_ECC_HAMMING;
  68. platform_set_drvdata(pdev, data);
  69. /* Handle any platform specific setup */
  70. if (pdata->ctrl.probe) {
  71. err = pdata->ctrl.probe(pdev);
  72. if (err)
  73. goto out;
  74. }
  75. /* Scan to find existence of the device */
  76. if (nand_scan(mtd, pdata->chip.nr_chips)) {
  77. err = -ENXIO;
  78. goto out;
  79. }
  80. part_types = pdata->chip.part_probe_types;
  81. err = mtd_device_parse_register(mtd, part_types, NULL,
  82. pdata->chip.partitions,
  83. pdata->chip.nr_partitions);
  84. if (!err)
  85. return err;
  86. nand_release(mtd);
  87. out:
  88. if (pdata->ctrl.remove)
  89. pdata->ctrl.remove(pdev);
  90. return err;
  91. }
  92. /*
  93. * Remove a NAND device.
  94. */
  95. static int plat_nand_remove(struct platform_device *pdev)
  96. {
  97. struct plat_nand_data *data = platform_get_drvdata(pdev);
  98. struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
  99. nand_release(nand_to_mtd(&data->chip));
  100. if (pdata->ctrl.remove)
  101. pdata->ctrl.remove(pdev);
  102. return 0;
  103. }
  104. static const struct of_device_id plat_nand_match[] = {
  105. { .compatible = "gen_nand" },
  106. {},
  107. };
  108. MODULE_DEVICE_TABLE(of, plat_nand_match);
  109. static struct platform_driver plat_nand_driver = {
  110. .probe = plat_nand_probe,
  111. .remove = plat_nand_remove,
  112. .driver = {
  113. .name = "gen_nand",
  114. .of_match_table = plat_nand_match,
  115. },
  116. };
  117. module_platform_driver(plat_nand_driver);
  118. MODULE_LICENSE("GPL");
  119. MODULE_AUTHOR("Vitaly Wool");
  120. MODULE_DESCRIPTION("Simple generic NAND driver");
  121. MODULE_ALIAS("platform:gen_nand");