plat_nand.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. struct mtd_info mtd;
  22. void __iomem *io_base;
  23. };
  24. /*
  25. * Probe for the NAND device.
  26. */
  27. static int plat_nand_probe(struct platform_device *pdev)
  28. {
  29. struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
  30. struct mtd_part_parser_data ppdata;
  31. struct plat_nand_data *data;
  32. struct resource *res;
  33. const char **part_types;
  34. int err = 0;
  35. if (!pdata) {
  36. dev_err(&pdev->dev, "platform_nand_data is missing\n");
  37. return -EINVAL;
  38. }
  39. if (pdata->chip.nr_chips < 1) {
  40. dev_err(&pdev->dev, "invalid number of chips specified\n");
  41. return -EINVAL;
  42. }
  43. /* Allocate memory for the device structure (and zero it) */
  44. data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
  45. GFP_KERNEL);
  46. if (!data)
  47. return -ENOMEM;
  48. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  49. data->io_base = devm_ioremap_resource(&pdev->dev, res);
  50. if (IS_ERR(data->io_base))
  51. return PTR_ERR(data->io_base);
  52. data->chip.priv = &data;
  53. data->mtd.priv = &data->chip;
  54. data->mtd.owner = THIS_MODULE;
  55. data->mtd.name = dev_name(&pdev->dev);
  56. data->chip.IO_ADDR_R = data->io_base;
  57. data->chip.IO_ADDR_W = data->io_base;
  58. data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
  59. data->chip.dev_ready = pdata->ctrl.dev_ready;
  60. data->chip.select_chip = pdata->ctrl.select_chip;
  61. data->chip.write_buf = pdata->ctrl.write_buf;
  62. data->chip.read_buf = pdata->ctrl.read_buf;
  63. data->chip.read_byte = pdata->ctrl.read_byte;
  64. data->chip.chip_delay = pdata->chip.chip_delay;
  65. data->chip.options |= pdata->chip.options;
  66. data->chip.bbt_options |= pdata->chip.bbt_options;
  67. data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
  68. data->chip.ecc.layout = pdata->chip.ecclayout;
  69. data->chip.ecc.mode = NAND_ECC_SOFT;
  70. platform_set_drvdata(pdev, data);
  71. /* Handle any platform specific setup */
  72. if (pdata->ctrl.probe) {
  73. err = pdata->ctrl.probe(pdev);
  74. if (err)
  75. goto out;
  76. }
  77. /* Scan to find existence of the device */
  78. if (nand_scan(&data->mtd, pdata->chip.nr_chips)) {
  79. err = -ENXIO;
  80. goto out;
  81. }
  82. part_types = pdata->chip.part_probe_types;
  83. ppdata.of_node = pdev->dev.of_node;
  84. err = mtd_device_parse_register(&data->mtd, part_types, &ppdata,
  85. pdata->chip.partitions,
  86. pdata->chip.nr_partitions);
  87. if (!err)
  88. return err;
  89. nand_release(&data->mtd);
  90. out:
  91. if (pdata->ctrl.remove)
  92. pdata->ctrl.remove(pdev);
  93. return err;
  94. }
  95. /*
  96. * Remove a NAND device.
  97. */
  98. static int plat_nand_remove(struct platform_device *pdev)
  99. {
  100. struct plat_nand_data *data = platform_get_drvdata(pdev);
  101. struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
  102. nand_release(&data->mtd);
  103. if (pdata->ctrl.remove)
  104. pdata->ctrl.remove(pdev);
  105. return 0;
  106. }
  107. static const struct of_device_id plat_nand_match[] = {
  108. { .compatible = "gen_nand" },
  109. {},
  110. };
  111. MODULE_DEVICE_TABLE(of, plat_nand_match);
  112. static struct platform_driver plat_nand_driver = {
  113. .probe = plat_nand_probe,
  114. .remove = plat_nand_remove,
  115. .driver = {
  116. .name = "gen_nand",
  117. .of_match_table = plat_nand_match,
  118. },
  119. };
  120. module_platform_driver(plat_nand_driver);
  121. MODULE_LICENSE("GPL");
  122. MODULE_AUTHOR("Vitaly Wool");
  123. MODULE_DESCRIPTION("Simple generic NAND driver");
  124. MODULE_ALIAS("platform:gen_nand");