orion_nand.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <asm/arch/hardware.h>
  21. #include <asm/plat-orion/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->width == 16)
  75. nc->options |= NAND_BUSWIDTH_16;
  76. platform_set_drvdata(pdev, mtd);
  77. if (nand_scan(mtd, 1)) {
  78. ret = -ENXIO;
  79. goto no_dev;
  80. }
  81. #ifdef CONFIG_MTD_PARTITIONS
  82. #ifdef CONFIG_MTD_CMDLINE_PARTS
  83. mtd->name = "orion_nand";
  84. num_part = parse_mtd_partitions(mtd, part_probes, &partitions, 0);
  85. #endif
  86. /* If cmdline partitions have been passed, let them be used */
  87. if (num_part <= 0) {
  88. num_part = board->nr_parts;
  89. partitions = board->parts;
  90. }
  91. if (partitions && num_part > 0)
  92. ret = add_mtd_partitions(mtd, partitions, num_part);
  93. else
  94. ret = add_mtd_device(mtd);
  95. #else
  96. ret = add_mtd_device(mtd);
  97. #endif
  98. if (ret) {
  99. nand_release(mtd);
  100. goto no_dev;
  101. }
  102. return 0;
  103. no_dev:
  104. platform_set_drvdata(pdev, NULL);
  105. iounmap(io_base);
  106. no_res:
  107. kfree(nc);
  108. return ret;
  109. }
  110. static int __devexit orion_nand_remove(struct platform_device *pdev)
  111. {
  112. struct mtd_info *mtd = platform_get_drvdata(pdev);
  113. struct nand_chip *nc = mtd->priv;
  114. nand_release(mtd);
  115. iounmap(nc->IO_ADDR_W);
  116. kfree(nc);
  117. return 0;
  118. }
  119. static struct platform_driver orion_nand_driver = {
  120. .probe = orion_nand_probe,
  121. .remove = orion_nand_remove,
  122. .driver = {
  123. .name = "orion_nand",
  124. .owner = THIS_MODULE,
  125. },
  126. };
  127. static int __init orion_nand_init(void)
  128. {
  129. return platform_driver_register(&orion_nand_driver);
  130. }
  131. static void __exit orion_nand_exit(void)
  132. {
  133. platform_driver_unregister(&orion_nand_driver);
  134. }
  135. module_init(orion_nand_init);
  136. module_exit(orion_nand_exit);
  137. MODULE_LICENSE("GPL");
  138. MODULE_AUTHOR("Tzachi Perelstein");
  139. MODULE_DESCRIPTION("NAND glue for Orion platforms");
  140. MODULE_ALIAS("platform:orion_nand");