plat_nand.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 = 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. 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. iounmap(data->io_base);
  106. out_release_io:
  107. release_mem_region(res->start, resource_size(res));
  108. out_free:
  109. kfree(data);
  110. return err;
  111. }
  112. /*
  113. * Remove a NAND device.
  114. */
  115. static int plat_nand_remove(struct platform_device *pdev)
  116. {
  117. struct plat_nand_data *data = platform_get_drvdata(pdev);
  118. struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
  119. struct resource *res;
  120. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  121. nand_release(&data->mtd);
  122. if (pdata->ctrl.remove)
  123. pdata->ctrl.remove(pdev);
  124. iounmap(data->io_base);
  125. release_mem_region(res->start, resource_size(res));
  126. kfree(data);
  127. return 0;
  128. }
  129. static const struct of_device_id plat_nand_match[] = {
  130. { .compatible = "gen_nand" },
  131. {},
  132. };
  133. MODULE_DEVICE_TABLE(of, plat_nand_match);
  134. static struct platform_driver plat_nand_driver = {
  135. .probe = plat_nand_probe,
  136. .remove = plat_nand_remove,
  137. .driver = {
  138. .name = "gen_nand",
  139. .owner = THIS_MODULE,
  140. .of_match_table = plat_nand_match,
  141. },
  142. };
  143. module_platform_driver(plat_nand_driver);
  144. MODULE_LICENSE("GPL");
  145. MODULE_AUTHOR("Vitaly Wool");
  146. MODULE_DESCRIPTION("Simple generic NAND driver");
  147. MODULE_ALIAS("platform:gen_nand");