plat_nand.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.bbt_options |= pdata->chip.bbt_options;
  73. data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
  74. data->chip.ecc.layout = pdata->chip.ecclayout;
  75. data->chip.ecc.mode = NAND_ECC_SOFT;
  76. platform_set_drvdata(pdev, data);
  77. /* Handle any platform specific setup */
  78. if (pdata->ctrl.probe) {
  79. err = pdata->ctrl.probe(pdev);
  80. if (err)
  81. goto out;
  82. }
  83. /* Scan to find existence of the device */
  84. if (nand_scan(&data->mtd, pdata->chip.nr_chips)) {
  85. err = -ENXIO;
  86. goto out;
  87. }
  88. if (pdata->chip.part_probe_types) {
  89. err = parse_mtd_partitions(&data->mtd,
  90. pdata->chip.part_probe_types,
  91. &data->parts, 0);
  92. if (err > 0) {
  93. mtd_device_register(&data->mtd, data->parts, err);
  94. return 0;
  95. }
  96. }
  97. if (pdata->chip.set_parts)
  98. pdata->chip.set_parts(data->mtd.size, &pdata->chip);
  99. if (pdata->chip.partitions) {
  100. data->parts = pdata->chip.partitions;
  101. err = mtd_device_register(&data->mtd, data->parts,
  102. pdata->chip.nr_partitions);
  103. } else
  104. err = mtd_device_register(&data->mtd, NULL, 0);
  105. if (!err)
  106. return err;
  107. nand_release(&data->mtd);
  108. out:
  109. if (pdata->ctrl.remove)
  110. pdata->ctrl.remove(pdev);
  111. platform_set_drvdata(pdev, NULL);
  112. iounmap(data->io_base);
  113. out_release_io:
  114. release_mem_region(res->start, resource_size(res));
  115. out_free:
  116. kfree(data);
  117. return err;
  118. }
  119. /*
  120. * Remove a NAND device.
  121. */
  122. static int __devexit plat_nand_remove(struct platform_device *pdev)
  123. {
  124. struct plat_nand_data *data = platform_get_drvdata(pdev);
  125. struct platform_nand_data *pdata = pdev->dev.platform_data;
  126. struct resource *res;
  127. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  128. nand_release(&data->mtd);
  129. if (data->parts && data->parts != pdata->chip.partitions)
  130. kfree(data->parts);
  131. if (pdata->ctrl.remove)
  132. pdata->ctrl.remove(pdev);
  133. iounmap(data->io_base);
  134. release_mem_region(res->start, resource_size(res));
  135. kfree(data);
  136. return 0;
  137. }
  138. static struct platform_driver plat_nand_driver = {
  139. .probe = plat_nand_probe,
  140. .remove = __devexit_p(plat_nand_remove),
  141. .driver = {
  142. .name = "gen_nand",
  143. .owner = THIS_MODULE,
  144. },
  145. };
  146. static int __init plat_nand_init(void)
  147. {
  148. return platform_driver_register(&plat_nand_driver);
  149. }
  150. static void __exit plat_nand_exit(void)
  151. {
  152. platform_driver_unregister(&plat_nand_driver);
  153. }
  154. module_init(plat_nand_init);
  155. module_exit(plat_nand_exit);
  156. MODULE_LICENSE("GPL");
  157. MODULE_AUTHOR("Vitaly Wool");
  158. MODULE_DESCRIPTION("Simple generic NAND driver");
  159. MODULE_ALIAS("platform:gen_nand");