plat_nand.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/io.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/nand.h>
  17. #include <linux/mtd/partitions.h>
  18. struct plat_nand_data {
  19. struct nand_chip chip;
  20. struct mtd_info mtd;
  21. void __iomem *io_base;
  22. };
  23. static const char *part_probe_types[] = { "cmdlinepart", NULL };
  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 = pdev->dev.platform_data;
  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. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  44. if (!res)
  45. return -ENXIO;
  46. /* Allocate memory for the device structure (and zero it) */
  47. data = kzalloc(sizeof(struct plat_nand_data), GFP_KERNEL);
  48. if (!data) {
  49. dev_err(&pdev->dev, "failed to allocate device structure.\n");
  50. return -ENOMEM;
  51. }
  52. if (!request_mem_region(res->start, resource_size(res),
  53. dev_name(&pdev->dev))) {
  54. dev_err(&pdev->dev, "request_mem_region failed\n");
  55. err = -EBUSY;
  56. goto out_free;
  57. }
  58. data->io_base = ioremap(res->start, resource_size(res));
  59. if (data->io_base == NULL) {
  60. dev_err(&pdev->dev, "ioremap failed\n");
  61. err = -EIO;
  62. goto out_release_io;
  63. }
  64. data->chip.priv = &data;
  65. data->mtd.priv = &data->chip;
  66. data->mtd.owner = THIS_MODULE;
  67. data->mtd.name = dev_name(&pdev->dev);
  68. data->chip.IO_ADDR_R = data->io_base;
  69. data->chip.IO_ADDR_W = data->io_base;
  70. data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
  71. data->chip.dev_ready = pdata->ctrl.dev_ready;
  72. data->chip.select_chip = pdata->ctrl.select_chip;
  73. data->chip.write_buf = pdata->ctrl.write_buf;
  74. data->chip.read_buf = pdata->ctrl.read_buf;
  75. data->chip.read_byte = pdata->ctrl.read_byte;
  76. data->chip.chip_delay = pdata->chip.chip_delay;
  77. data->chip.options |= pdata->chip.options;
  78. data->chip.bbt_options |= pdata->chip.bbt_options;
  79. data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
  80. data->chip.ecc.layout = pdata->chip.ecclayout;
  81. data->chip.ecc.mode = NAND_ECC_SOFT;
  82. platform_set_drvdata(pdev, data);
  83. /* Handle any platform specific setup */
  84. if (pdata->ctrl.probe) {
  85. err = pdata->ctrl.probe(pdev);
  86. if (err)
  87. goto out;
  88. }
  89. /* Scan to find existence of the device */
  90. if (nand_scan(&data->mtd, pdata->chip.nr_chips)) {
  91. err = -ENXIO;
  92. goto out;
  93. }
  94. part_types = pdata->chip.part_probe_types ? : part_probe_types;
  95. ppdata.of_node = pdev->dev.of_node;
  96. err = mtd_device_parse_register(&data->mtd, part_types, &ppdata,
  97. pdata->chip.partitions,
  98. pdata->chip.nr_partitions);
  99. if (!err)
  100. return err;
  101. nand_release(&data->mtd);
  102. out:
  103. if (pdata->ctrl.remove)
  104. pdata->ctrl.remove(pdev);
  105. platform_set_drvdata(pdev, NULL);
  106. iounmap(data->io_base);
  107. out_release_io:
  108. release_mem_region(res->start, resource_size(res));
  109. out_free:
  110. kfree(data);
  111. return err;
  112. }
  113. /*
  114. * Remove a NAND device.
  115. */
  116. static int plat_nand_remove(struct platform_device *pdev)
  117. {
  118. struct plat_nand_data *data = platform_get_drvdata(pdev);
  119. struct platform_nand_data *pdata = pdev->dev.platform_data;
  120. struct resource *res;
  121. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  122. nand_release(&data->mtd);
  123. if (pdata->ctrl.remove)
  124. pdata->ctrl.remove(pdev);
  125. iounmap(data->io_base);
  126. release_mem_region(res->start, resource_size(res));
  127. kfree(data);
  128. return 0;
  129. }
  130. static const struct of_device_id plat_nand_match[] = {
  131. { .compatible = "gen_nand" },
  132. {},
  133. };
  134. MODULE_DEVICE_TABLE(of, plat_nand_match);
  135. static struct platform_driver plat_nand_driver = {
  136. .probe = plat_nand_probe,
  137. .remove = plat_nand_remove,
  138. .driver = {
  139. .name = "gen_nand",
  140. .owner = THIS_MODULE,
  141. .of_match_table = plat_nand_match,
  142. },
  143. };
  144. module_platform_driver(plat_nand_driver);
  145. MODULE_LICENSE("GPL");
  146. MODULE_AUTHOR("Vitaly Wool");
  147. MODULE_DESCRIPTION("Simple generic NAND driver");
  148. MODULE_ALIAS("platform:gen_nand");