plat_nand.c 4.5 KB

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