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