plat_nand.c 3.7 KB

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