orion_nand.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. static void orion_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  23. {
  24. struct nand_chip *nc = mtd->priv;
  25. struct orion_nand_data *board = nc->priv;
  26. u32 offs;
  27. if (cmd == NAND_CMD_NONE)
  28. return;
  29. if (ctrl & NAND_CLE)
  30. offs = (1 << board->cle);
  31. else if (ctrl & NAND_ALE)
  32. offs = (1 << board->ale);
  33. else
  34. return;
  35. if (nc->options & NAND_BUSWIDTH_16)
  36. offs <<= 1;
  37. writeb(cmd, nc->IO_ADDR_W + offs);
  38. }
  39. static void orion_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  40. {
  41. struct nand_chip *chip = mtd->priv;
  42. void __iomem *io_base = chip->IO_ADDR_R;
  43. uint64_t *buf64;
  44. int i = 0;
  45. while (len && (unsigned long)buf & 7) {
  46. *buf++ = readb(io_base);
  47. len--;
  48. }
  49. buf64 = (uint64_t *)buf;
  50. while (i < len/8) {
  51. /*
  52. * Since GCC has no proper constraint (PR 43518)
  53. * force x variable to r2/r3 registers as ldrd instruction
  54. * requires first register to be even.
  55. */
  56. register uint64_t x asm ("r2");
  57. asm volatile ("ldrd\t%0, [%1]" : "=&r" (x) : "r" (io_base));
  58. buf64[i++] = x;
  59. }
  60. i *= 8;
  61. while (i < len)
  62. buf[i++] = readb(io_base);
  63. }
  64. static int __init orion_nand_probe(struct platform_device *pdev)
  65. {
  66. struct mtd_info *mtd;
  67. struct nand_chip *nc;
  68. struct orion_nand_data *board;
  69. struct resource *res;
  70. void __iomem *io_base;
  71. int ret = 0;
  72. nc = kzalloc(sizeof(struct nand_chip) + sizeof(struct mtd_info), GFP_KERNEL);
  73. if (!nc) {
  74. printk(KERN_ERR "orion_nand: failed to allocate device structure.\n");
  75. ret = -ENOMEM;
  76. goto no_res;
  77. }
  78. mtd = (struct mtd_info *)(nc + 1);
  79. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  80. if (!res) {
  81. ret = -ENODEV;
  82. goto no_res;
  83. }
  84. io_base = ioremap(res->start, resource_size(res));
  85. if (!io_base) {
  86. printk(KERN_ERR "orion_nand: ioremap failed\n");
  87. ret = -EIO;
  88. goto no_res;
  89. }
  90. board = pdev->dev.platform_data;
  91. mtd->priv = nc;
  92. mtd->owner = THIS_MODULE;
  93. nc->priv = board;
  94. nc->IO_ADDR_R = nc->IO_ADDR_W = io_base;
  95. nc->cmd_ctrl = orion_nand_cmd_ctrl;
  96. nc->read_buf = orion_nand_read_buf;
  97. nc->ecc.mode = NAND_ECC_SOFT;
  98. if (board->chip_delay)
  99. nc->chip_delay = board->chip_delay;
  100. if (board->width == 16)
  101. nc->options |= NAND_BUSWIDTH_16;
  102. if (board->dev_ready)
  103. nc->dev_ready = board->dev_ready;
  104. platform_set_drvdata(pdev, mtd);
  105. if (nand_scan(mtd, 1)) {
  106. ret = -ENXIO;
  107. goto no_dev;
  108. }
  109. mtd->name = "orion_nand";
  110. ret = mtd_device_parse_register(mtd, NULL, NULL, board->parts,
  111. board->nr_parts);
  112. if (ret) {
  113. nand_release(mtd);
  114. goto no_dev;
  115. }
  116. return 0;
  117. no_dev:
  118. platform_set_drvdata(pdev, NULL);
  119. iounmap(io_base);
  120. no_res:
  121. kfree(nc);
  122. return ret;
  123. }
  124. static int __devexit orion_nand_remove(struct platform_device *pdev)
  125. {
  126. struct mtd_info *mtd = platform_get_drvdata(pdev);
  127. struct nand_chip *nc = mtd->priv;
  128. nand_release(mtd);
  129. iounmap(nc->IO_ADDR_W);
  130. kfree(nc);
  131. return 0;
  132. }
  133. static struct platform_driver orion_nand_driver = {
  134. .remove = __devexit_p(orion_nand_remove),
  135. .driver = {
  136. .name = "orion_nand",
  137. .owner = THIS_MODULE,
  138. },
  139. };
  140. static int __init orion_nand_init(void)
  141. {
  142. return platform_driver_probe(&orion_nand_driver, orion_nand_probe);
  143. }
  144. static void __exit orion_nand_exit(void)
  145. {
  146. platform_driver_unregister(&orion_nand_driver);
  147. }
  148. module_init(orion_nand_init);
  149. module_exit(orion_nand_exit);
  150. MODULE_LICENSE("GPL");
  151. MODULE_AUTHOR("Tzachi Perelstein");
  152. MODULE_DESCRIPTION("NAND glue for Orion platforms");
  153. MODULE_ALIAS("platform:orion_nand");