plat_nand.c 4.4 KB

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