plat_nand.c 3.8 KB

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