orion_nand.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 void orion_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  43. {
  44. struct nand_chip *chip = mtd->priv;
  45. void __iomem *io_base = chip->IO_ADDR_R;
  46. uint64_t *buf64;
  47. int i = 0;
  48. while (len && (unsigned long)buf & 7) {
  49. *buf++ = readb(io_base);
  50. len--;
  51. }
  52. buf64 = (uint64_t *)buf;
  53. while (i < len/8) {
  54. /*
  55. * Since GCC has no proper constraint (PR 43518)
  56. * force x variable to r2/r3 registers as ldrd instruction
  57. * requires first register to be even.
  58. */
  59. register uint64_t x asm ("r2");
  60. asm volatile ("ldrd\t%0, [%1]" : "=&r" (x) : "r" (io_base));
  61. buf64[i++] = x;
  62. }
  63. i *= 8;
  64. while (i < len)
  65. buf[i++] = readb(io_base);
  66. }
  67. static int __init orion_nand_probe(struct platform_device *pdev)
  68. {
  69. struct mtd_info *mtd;
  70. struct nand_chip *nc;
  71. struct orion_nand_data *board;
  72. struct resource *res;
  73. void __iomem *io_base;
  74. int ret = 0;
  75. #ifdef CONFIG_MTD_PARTITIONS
  76. struct mtd_partition *partitions = NULL;
  77. int num_part = 0;
  78. #endif
  79. nc = kzalloc(sizeof(struct nand_chip) + sizeof(struct mtd_info), GFP_KERNEL);
  80. if (!nc) {
  81. printk(KERN_ERR "orion_nand: failed to allocate device structure.\n");
  82. ret = -ENOMEM;
  83. goto no_res;
  84. }
  85. mtd = (struct mtd_info *)(nc + 1);
  86. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  87. if (!res) {
  88. ret = -ENODEV;
  89. goto no_res;
  90. }
  91. io_base = ioremap(res->start, resource_size(res));
  92. if (!io_base) {
  93. printk(KERN_ERR "orion_nand: ioremap failed\n");
  94. ret = -EIO;
  95. goto no_res;
  96. }
  97. board = pdev->dev.platform_data;
  98. mtd->priv = nc;
  99. mtd->owner = THIS_MODULE;
  100. nc->priv = board;
  101. nc->IO_ADDR_R = nc->IO_ADDR_W = io_base;
  102. nc->cmd_ctrl = orion_nand_cmd_ctrl;
  103. nc->read_buf = orion_nand_read_buf;
  104. nc->ecc.mode = NAND_ECC_SOFT;
  105. if (board->chip_delay)
  106. nc->chip_delay = board->chip_delay;
  107. if (board->width == 16)
  108. nc->options |= NAND_BUSWIDTH_16;
  109. if (board->dev_ready)
  110. nc->dev_ready = board->dev_ready;
  111. platform_set_drvdata(pdev, mtd);
  112. if (nand_scan(mtd, 1)) {
  113. ret = -ENXIO;
  114. goto no_dev;
  115. }
  116. #ifdef CONFIG_MTD_PARTITIONS
  117. #ifdef CONFIG_MTD_CMDLINE_PARTS
  118. mtd->name = "orion_nand";
  119. num_part = parse_mtd_partitions(mtd, part_probes, &partitions, 0);
  120. #endif
  121. /* If cmdline partitions have been passed, let them be used */
  122. if (num_part <= 0) {
  123. num_part = board->nr_parts;
  124. partitions = board->parts;
  125. }
  126. if (partitions && num_part > 0)
  127. ret = add_mtd_partitions(mtd, partitions, num_part);
  128. else
  129. ret = add_mtd_device(mtd);
  130. #else
  131. ret = add_mtd_device(mtd);
  132. #endif
  133. if (ret) {
  134. nand_release(mtd);
  135. goto no_dev;
  136. }
  137. return 0;
  138. no_dev:
  139. platform_set_drvdata(pdev, NULL);
  140. iounmap(io_base);
  141. no_res:
  142. kfree(nc);
  143. return ret;
  144. }
  145. static int __devexit orion_nand_remove(struct platform_device *pdev)
  146. {
  147. struct mtd_info *mtd = platform_get_drvdata(pdev);
  148. struct nand_chip *nc = mtd->priv;
  149. nand_release(mtd);
  150. iounmap(nc->IO_ADDR_W);
  151. kfree(nc);
  152. return 0;
  153. }
  154. static struct platform_driver orion_nand_driver = {
  155. .remove = __devexit_p(orion_nand_remove),
  156. .driver = {
  157. .name = "orion_nand",
  158. .owner = THIS_MODULE,
  159. },
  160. };
  161. static int __init orion_nand_init(void)
  162. {
  163. return platform_driver_probe(&orion_nand_driver, orion_nand_probe);
  164. }
  165. static void __exit orion_nand_exit(void)
  166. {
  167. platform_driver_unregister(&orion_nand_driver);
  168. }
  169. module_init(orion_nand_init);
  170. module_exit(orion_nand_exit);
  171. MODULE_LICENSE("GPL");
  172. MODULE_AUTHOR("Tzachi Perelstein");
  173. MODULE_DESCRIPTION("NAND glue for Orion platforms");
  174. MODULE_ALIAS("platform:orion_nand");