tah.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * drivers/net/ethernet/ibm/emac/tah.c
  4. *
  5. * Driver for PowerPC 4xx on-chip ethernet controller, TAH support.
  6. *
  7. * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
  8. * <benh@kernel.crashing.org>
  9. *
  10. * Based on the arch/ppc version of the driver:
  11. *
  12. * Copyright 2004 MontaVista Software, Inc.
  13. * Matt Porter <mporter@kernel.crashing.org>
  14. *
  15. * Copyright (c) 2005 Eugene Surovegin <ebs@ebshome.net>
  16. */
  17. #include <linux/mod_devicetable.h>
  18. #include <linux/of_address.h>
  19. #include <linux/platform_device.h>
  20. #include <asm/io.h>
  21. #include "emac.h"
  22. #include "core.h"
  23. int tah_attach(struct platform_device *ofdev, int channel)
  24. {
  25. struct tah_instance *dev = platform_get_drvdata(ofdev);
  26. mutex_lock(&dev->lock);
  27. /* Reset has been done at probe() time... nothing else to do for now */
  28. ++dev->users;
  29. mutex_unlock(&dev->lock);
  30. return 0;
  31. }
  32. void tah_detach(struct platform_device *ofdev, int channel)
  33. {
  34. struct tah_instance *dev = platform_get_drvdata(ofdev);
  35. mutex_lock(&dev->lock);
  36. --dev->users;
  37. mutex_unlock(&dev->lock);
  38. }
  39. void tah_reset(struct platform_device *ofdev)
  40. {
  41. struct tah_instance *dev = platform_get_drvdata(ofdev);
  42. struct tah_regs __iomem *p = dev->base;
  43. int n;
  44. /* Reset TAH */
  45. out_be32(&p->mr, TAH_MR_SR);
  46. n = 100;
  47. while ((in_be32(&p->mr) & TAH_MR_SR) && n)
  48. --n;
  49. if (unlikely(!n))
  50. printk(KERN_ERR "%pOF: reset timeout\n", ofdev->dev.of_node);
  51. /* 10KB TAH TX FIFO accommodates the max MTU of 9000 */
  52. out_be32(&p->mr,
  53. TAH_MR_CVR | TAH_MR_ST_768 | TAH_MR_TFS_10KB | TAH_MR_DTFP |
  54. TAH_MR_DIG);
  55. }
  56. int tah_get_regs_len(struct platform_device *ofdev)
  57. {
  58. return sizeof(struct emac_ethtool_regs_subhdr) +
  59. sizeof(struct tah_regs);
  60. }
  61. void *tah_dump_regs(struct platform_device *ofdev, void *buf)
  62. {
  63. struct tah_instance *dev = platform_get_drvdata(ofdev);
  64. struct emac_ethtool_regs_subhdr *hdr = buf;
  65. struct tah_regs *regs = (struct tah_regs *)(hdr + 1);
  66. hdr->version = 0;
  67. hdr->index = 0; /* for now, are there chips with more than one
  68. * zmii ? if yes, then we'll add a cell_index
  69. * like we do for emac
  70. */
  71. memcpy_fromio(regs, dev->base, sizeof(struct tah_regs));
  72. return regs + 1;
  73. }
  74. static int tah_probe(struct platform_device *ofdev)
  75. {
  76. struct device_node *np = ofdev->dev.of_node;
  77. struct tah_instance *dev;
  78. struct resource regs;
  79. int rc;
  80. rc = -ENOMEM;
  81. dev = kzalloc(sizeof(struct tah_instance), GFP_KERNEL);
  82. if (dev == NULL)
  83. goto err_gone;
  84. mutex_init(&dev->lock);
  85. dev->ofdev = ofdev;
  86. rc = -ENXIO;
  87. if (of_address_to_resource(np, 0, &regs)) {
  88. printk(KERN_ERR "%pOF: Can't get registers address\n", np);
  89. goto err_free;
  90. }
  91. rc = -ENOMEM;
  92. dev->base = (struct tah_regs __iomem *)ioremap(regs.start,
  93. sizeof(struct tah_regs));
  94. if (dev->base == NULL) {
  95. printk(KERN_ERR "%pOF: Can't map device registers!\n", np);
  96. goto err_free;
  97. }
  98. platform_set_drvdata(ofdev, dev);
  99. /* Initialize TAH and enable IPv4 checksum verification, no TSO yet */
  100. tah_reset(ofdev);
  101. printk(KERN_INFO "TAH %pOF initialized\n", ofdev->dev.of_node);
  102. wmb();
  103. return 0;
  104. err_free:
  105. kfree(dev);
  106. err_gone:
  107. return rc;
  108. }
  109. static void tah_remove(struct platform_device *ofdev)
  110. {
  111. struct tah_instance *dev = platform_get_drvdata(ofdev);
  112. WARN_ON(dev->users != 0);
  113. iounmap(dev->base);
  114. kfree(dev);
  115. }
  116. static const struct of_device_id tah_match[] =
  117. {
  118. {
  119. .compatible = "ibm,tah",
  120. },
  121. /* For backward compat with old DT */
  122. {
  123. .type = "tah",
  124. },
  125. {},
  126. };
  127. static struct platform_driver tah_driver = {
  128. .driver = {
  129. .name = "emac-tah",
  130. .of_match_table = tah_match,
  131. },
  132. .probe = tah_probe,
  133. .remove_new = tah_remove,
  134. };
  135. int __init tah_init(void)
  136. {
  137. return platform_driver_register(&tah_driver);
  138. }
  139. void tah_exit(void)
  140. {
  141. platform_driver_unregister(&tah_driver);
  142. }