plat_nand.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 __init 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->chip.IO_ADDR_R = data->io_base;
  52. data->chip.IO_ADDR_W = data->io_base;
  53. data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
  54. data->chip.dev_ready = pdata->ctrl.dev_ready;
  55. data->chip.select_chip = pdata->ctrl.select_chip;
  56. data->chip.chip_delay = pdata->chip.chip_delay;
  57. data->chip.options |= pdata->chip.options;
  58. data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
  59. data->chip.ecc.layout = pdata->chip.ecclayout;
  60. data->chip.ecc.mode = NAND_ECC_SOFT;
  61. platform_set_drvdata(pdev, data);
  62. /* Scan to find existance of the device */
  63. if (nand_scan(&data->mtd, 1)) {
  64. res = -ENXIO;
  65. goto out;
  66. }
  67. #ifdef CONFIG_MTD_PARTITIONS
  68. if (pdata->chip.part_probe_types) {
  69. res = parse_mtd_partitions(&data->mtd,
  70. pdata->chip.part_probe_types,
  71. &data->parts, 0);
  72. if (res > 0) {
  73. add_mtd_partitions(&data->mtd, data->parts, res);
  74. return 0;
  75. }
  76. }
  77. if (pdata->chip.partitions) {
  78. data->parts = pdata->chip.partitions;
  79. res = add_mtd_partitions(&data->mtd, data->parts,
  80. pdata->chip.nr_partitions);
  81. } else
  82. #endif
  83. res = add_mtd_device(&data->mtd);
  84. if (!res)
  85. return res;
  86. nand_release(&data->mtd);
  87. out:
  88. platform_set_drvdata(pdev, NULL);
  89. iounmap(data->io_base);
  90. kfree(data);
  91. return res;
  92. }
  93. /*
  94. * Remove a NAND device.
  95. */
  96. static int __devexit plat_nand_remove(struct platform_device *pdev)
  97. {
  98. struct plat_nand_data *data = platform_get_drvdata(pdev);
  99. struct platform_nand_data *pdata = pdev->dev.platform_data;
  100. nand_release(&data->mtd);
  101. #ifdef CONFIG_MTD_PARTITIONS
  102. if (data->parts && data->parts != pdata->chip.partitions)
  103. kfree(data->parts);
  104. #endif
  105. iounmap(data->io_base);
  106. kfree(data);
  107. return 0;
  108. }
  109. static struct platform_driver plat_nand_driver = {
  110. .probe = plat_nand_probe,
  111. .remove = plat_nand_remove,
  112. .driver = {
  113. .name = "gen_nand",
  114. .owner = THIS_MODULE,
  115. },
  116. };
  117. static int __init plat_nand_init(void)
  118. {
  119. return platform_driver_register(&plat_nand_driver);
  120. }
  121. static void __exit plat_nand_exit(void)
  122. {
  123. platform_driver_unregister(&plat_nand_driver);
  124. }
  125. module_init(plat_nand_init);
  126. module_exit(plat_nand_exit);
  127. MODULE_LICENSE("GPL");
  128. MODULE_AUTHOR("Vitaly Wool");
  129. MODULE_DESCRIPTION("Simple generic NAND driver");