plat_nand.c 3.6 KB

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