plat_nand.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.dev.parent = &pdev->dev;
  55. data->chip.IO_ADDR_R = data->io_base;
  56. data->chip.IO_ADDR_W = data->io_base;
  57. data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
  58. data->chip.dev_ready = pdata->ctrl.dev_ready;
  59. data->chip.select_chip = pdata->ctrl.select_chip;
  60. data->chip.write_buf = pdata->ctrl.write_buf;
  61. data->chip.read_buf = pdata->ctrl.read_buf;
  62. data->chip.read_byte = pdata->ctrl.read_byte;
  63. data->chip.chip_delay = pdata->chip.chip_delay;
  64. data->chip.options |= pdata->chip.options;
  65. data->chip.bbt_options |= pdata->chip.bbt_options;
  66. data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
  67. data->chip.ecc.layout = pdata->chip.ecclayout;
  68. data->chip.ecc.mode = NAND_ECC_SOFT;
  69. platform_set_drvdata(pdev, data);
  70. /* Handle any platform specific setup */
  71. if (pdata->ctrl.probe) {
  72. err = pdata->ctrl.probe(pdev);
  73. if (err)
  74. goto out;
  75. }
  76. /* Scan to find existence of the device */
  77. if (nand_scan(&data->mtd, pdata->chip.nr_chips)) {
  78. err = -ENXIO;
  79. goto out;
  80. }
  81. part_types = pdata->chip.part_probe_types;
  82. ppdata.of_node = pdev->dev.of_node;
  83. err = mtd_device_parse_register(&data->mtd, part_types, &ppdata,
  84. pdata->chip.partitions,
  85. pdata->chip.nr_partitions);
  86. if (!err)
  87. return err;
  88. nand_release(&data->mtd);
  89. out:
  90. if (pdata->ctrl.remove)
  91. pdata->ctrl.remove(pdev);
  92. return err;
  93. }
  94. /*
  95. * Remove a NAND device.
  96. */
  97. static int plat_nand_remove(struct platform_device *pdev)
  98. {
  99. struct plat_nand_data *data = platform_get_drvdata(pdev);
  100. struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
  101. nand_release(&data->mtd);
  102. if (pdata->ctrl.remove)
  103. pdata->ctrl.remove(pdev);
  104. return 0;
  105. }
  106. static const struct of_device_id plat_nand_match[] = {
  107. { .compatible = "gen_nand" },
  108. {},
  109. };
  110. MODULE_DEVICE_TABLE(of, plat_nand_match);
  111. static struct platform_driver plat_nand_driver = {
  112. .probe = plat_nand_probe,
  113. .remove = plat_nand_remove,
  114. .driver = {
  115. .name = "gen_nand",
  116. .of_match_table = plat_nand_match,
  117. },
  118. };
  119. module_platform_driver(plat_nand_driver);
  120. MODULE_LICENSE("GPL");
  121. MODULE_AUTHOR("Vitaly Wool");
  122. MODULE_DESCRIPTION("Simple generic NAND driver");
  123. MODULE_ALIAS("platform:gen_nand");