plat_nand.c 4.2 KB

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