orion_nand.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * drivers/mtd/nand/orion_nand.c
  3. *
  4. * NAND support for Marvell Orion SoC platforms
  5. *
  6. * Tzachi Perelstein <tzachi@marvell.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/nand.h>
  17. #include <linux/mtd/partitions.h>
  18. #include <asm/io.h>
  19. #include <asm/sizes.h>
  20. #include <mach/hardware.h>
  21. #include <plat/orion_nand.h>
  22. #ifdef CONFIG_MTD_CMDLINE_PARTS
  23. static const char *part_probes[] = { "cmdlinepart", NULL };
  24. #endif
  25. static void orion_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  26. {
  27. struct nand_chip *nc = mtd->priv;
  28. struct orion_nand_data *board = nc->priv;
  29. u32 offs;
  30. if (cmd == NAND_CMD_NONE)
  31. return;
  32. if (ctrl & NAND_CLE)
  33. offs = (1 << board->cle);
  34. else if (ctrl & NAND_ALE)
  35. offs = (1 << board->ale);
  36. else
  37. return;
  38. if (nc->options & NAND_BUSWIDTH_16)
  39. offs <<= 1;
  40. writeb(cmd, nc->IO_ADDR_W + offs);
  41. }
  42. static int __init orion_nand_probe(struct platform_device *pdev)
  43. {
  44. struct mtd_info *mtd;
  45. struct nand_chip *nc;
  46. struct orion_nand_data *board;
  47. void __iomem *io_base;
  48. int ret = 0;
  49. #ifdef CONFIG_MTD_PARTITIONS
  50. struct mtd_partition *partitions = NULL;
  51. int num_part = 0;
  52. #endif
  53. nc = kzalloc(sizeof(struct nand_chip) + sizeof(struct mtd_info), GFP_KERNEL);
  54. if (!nc) {
  55. printk(KERN_ERR "orion_nand: failed to allocate device structure.\n");
  56. ret = -ENOMEM;
  57. goto no_res;
  58. }
  59. mtd = (struct mtd_info *)(nc + 1);
  60. io_base = ioremap(pdev->resource[0].start,
  61. pdev->resource[0].end - pdev->resource[0].start + 1);
  62. if (!io_base) {
  63. printk(KERN_ERR "orion_nand: ioremap failed\n");
  64. ret = -EIO;
  65. goto no_res;
  66. }
  67. board = pdev->dev.platform_data;
  68. mtd->priv = nc;
  69. mtd->owner = THIS_MODULE;
  70. nc->priv = board;
  71. nc->IO_ADDR_R = nc->IO_ADDR_W = io_base;
  72. nc->cmd_ctrl = orion_nand_cmd_ctrl;
  73. nc->ecc.mode = NAND_ECC_SOFT;
  74. if (board->chip_delay)
  75. nc->chip_delay = board->chip_delay;
  76. if (board->width == 16)
  77. nc->options |= NAND_BUSWIDTH_16;
  78. platform_set_drvdata(pdev, mtd);
  79. if (nand_scan(mtd, 1)) {
  80. ret = -ENXIO;
  81. goto no_dev;
  82. }
  83. #ifdef CONFIG_MTD_PARTITIONS
  84. #ifdef CONFIG_MTD_CMDLINE_PARTS
  85. mtd->name = "orion_nand";
  86. num_part = parse_mtd_partitions(mtd, part_probes, &partitions, 0);
  87. #endif
  88. /* If cmdline partitions have been passed, let them be used */
  89. if (num_part <= 0) {
  90. num_part = board->nr_parts;
  91. partitions = board->parts;
  92. }
  93. if (partitions && num_part > 0)
  94. ret = add_mtd_partitions(mtd, partitions, num_part);
  95. else
  96. ret = add_mtd_device(mtd);
  97. #else
  98. ret = add_mtd_device(mtd);
  99. #endif
  100. if (ret) {
  101. nand_release(mtd);
  102. goto no_dev;
  103. }
  104. return 0;
  105. no_dev:
  106. platform_set_drvdata(pdev, NULL);
  107. iounmap(io_base);
  108. no_res:
  109. kfree(nc);
  110. return ret;
  111. }
  112. static int __devexit orion_nand_remove(struct platform_device *pdev)
  113. {
  114. struct mtd_info *mtd = platform_get_drvdata(pdev);
  115. struct nand_chip *nc = mtd->priv;
  116. nand_release(mtd);
  117. iounmap(nc->IO_ADDR_W);
  118. kfree(nc);
  119. return 0;
  120. }
  121. static struct platform_driver orion_nand_driver = {
  122. .probe = orion_nand_probe,
  123. .remove = orion_nand_remove,
  124. .driver = {
  125. .name = "orion_nand",
  126. .owner = THIS_MODULE,
  127. },
  128. };
  129. static int __init orion_nand_init(void)
  130. {
  131. return platform_driver_register(&orion_nand_driver);
  132. }
  133. static void __exit orion_nand_exit(void)
  134. {
  135. platform_driver_unregister(&orion_nand_driver);
  136. }
  137. module_init(orion_nand_init);
  138. module_exit(orion_nand_exit);
  139. MODULE_LICENSE("GPL");
  140. MODULE_AUTHOR("Tzachi Perelstein");
  141. MODULE_DESCRIPTION("NAND glue for Orion platforms");
  142. MODULE_ALIAS("platform:orion_nand");