plat_nand.c 3.7 KB

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