plat_nand.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #ifdef CONFIG_MTD_PARTITIONS
  23. int nr_parts;
  24. struct mtd_partition *parts;
  25. #endif
  26. };
  27. /*
  28. * Probe for the NAND device.
  29. */
  30. static int __devinit plat_nand_probe(struct platform_device *pdev)
  31. {
  32. struct platform_nand_data *pdata = pdev->dev.platform_data;
  33. struct plat_nand_data *data;
  34. int res = 0;
  35. /* Allocate memory for the device structure (and zero it) */
  36. data = kzalloc(sizeof(struct plat_nand_data), GFP_KERNEL);
  37. if (!data) {
  38. dev_err(&pdev->dev, "failed to allocate device structure.\n");
  39. return -ENOMEM;
  40. }
  41. data->io_base = ioremap(pdev->resource[0].start,
  42. pdev->resource[0].end - pdev->resource[0].start + 1);
  43. if (data->io_base == NULL) {
  44. dev_err(&pdev->dev, "ioremap failed\n");
  45. kfree(data);
  46. return -EIO;
  47. }
  48. data->chip.priv = &data;
  49. data->mtd.priv = &data->chip;
  50. data->mtd.owner = THIS_MODULE;
  51. data->mtd.name = dev_name(&pdev->dev);
  52. data->chip.IO_ADDR_R = data->io_base;
  53. data->chip.IO_ADDR_W = data->io_base;
  54. data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
  55. data->chip.dev_ready = pdata->ctrl.dev_ready;
  56. data->chip.select_chip = pdata->ctrl.select_chip;
  57. data->chip.write_buf = pdata->ctrl.write_buf;
  58. data->chip.read_buf = pdata->ctrl.read_buf;
  59. data->chip.chip_delay = pdata->chip.chip_delay;
  60. data->chip.options |= pdata->chip.options;
  61. data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
  62. data->chip.ecc.layout = pdata->chip.ecclayout;
  63. data->chip.ecc.mode = NAND_ECC_SOFT;
  64. platform_set_drvdata(pdev, data);
  65. /* Handle any platform specific setup */
  66. if (pdata->ctrl.probe) {
  67. res = pdata->ctrl.probe(pdev);
  68. if (res)
  69. goto out;
  70. }
  71. /* Scan to find existance of the device */
  72. if (nand_scan(&data->mtd, 1)) {
  73. res = -ENXIO;
  74. goto out;
  75. }
  76. #ifdef CONFIG_MTD_PARTITIONS
  77. if (pdata->chip.part_probe_types) {
  78. res = parse_mtd_partitions(&data->mtd,
  79. pdata->chip.part_probe_types,
  80. &data->parts, 0);
  81. if (res > 0) {
  82. add_mtd_partitions(&data->mtd, data->parts, res);
  83. return 0;
  84. }
  85. }
  86. if (pdata->chip.set_parts)
  87. pdata->chip.set_parts(data->mtd.size, &pdata->chip);
  88. if (pdata->chip.partitions) {
  89. data->parts = pdata->chip.partitions;
  90. res = add_mtd_partitions(&data->mtd, data->parts,
  91. pdata->chip.nr_partitions);
  92. } else
  93. #endif
  94. res = add_mtd_device(&data->mtd);
  95. if (!res)
  96. return res;
  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. kfree(data);
  104. return res;
  105. }
  106. /*
  107. * Remove a NAND device.
  108. */
  109. static int __devexit plat_nand_remove(struct platform_device *pdev)
  110. {
  111. struct plat_nand_data *data = platform_get_drvdata(pdev);
  112. struct platform_nand_data *pdata = pdev->dev.platform_data;
  113. nand_release(&data->mtd);
  114. #ifdef CONFIG_MTD_PARTITIONS
  115. if (data->parts && data->parts != pdata->chip.partitions)
  116. kfree(data->parts);
  117. #endif
  118. if (pdata->ctrl.remove)
  119. pdata->ctrl.remove(pdev);
  120. iounmap(data->io_base);
  121. kfree(data);
  122. return 0;
  123. }
  124. static struct platform_driver plat_nand_driver = {
  125. .probe = plat_nand_probe,
  126. .remove = __devexit_p(plat_nand_remove),
  127. .driver = {
  128. .name = "gen_nand",
  129. .owner = THIS_MODULE,
  130. },
  131. };
  132. static int __init plat_nand_init(void)
  133. {
  134. return platform_driver_register(&plat_nand_driver);
  135. }
  136. static void __exit plat_nand_exit(void)
  137. {
  138. platform_driver_unregister(&plat_nand_driver);
  139. }
  140. module_init(plat_nand_init);
  141. module_exit(plat_nand_exit);
  142. MODULE_LICENSE("GPL");
  143. MODULE_AUTHOR("Vitaly Wool");
  144. MODULE_DESCRIPTION("Simple generic NAND driver");
  145. MODULE_ALIAS("platform:gen_nand");