ibm_emac_rgmii.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * drivers/net/ibm_emac/ibm_emac_rgmii.c
  3. *
  4. * Driver for PowerPC 4xx on-chip ethernet controller, RGMII bridge support.
  5. *
  6. * Copyright (c) 2004, 2005 Zultys Technologies.
  7. * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
  8. *
  9. * Based on original work by
  10. * Matt Porter <mporter@kernel.crashing.org>
  11. * Copyright 2004 MontaVista Software, Inc.
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/ethtool.h>
  21. #include <asm/io.h>
  22. #include "ibm_emac_core.h"
  23. #include "ibm_emac_debug.h"
  24. /* RGMIIx_FER */
  25. #define RGMII_FER_MASK(idx) (0x7 << ((idx) * 4))
  26. #define RGMII_FER_RTBI(idx) (0x4 << ((idx) * 4))
  27. #define RGMII_FER_RGMII(idx) (0x5 << ((idx) * 4))
  28. #define RGMII_FER_TBI(idx) (0x6 << ((idx) * 4))
  29. #define RGMII_FER_GMII(idx) (0x7 << ((idx) * 4))
  30. /* RGMIIx_SSR */
  31. #define RGMII_SSR_MASK(idx) (0x7 << ((idx) * 8))
  32. #define RGMII_SSR_100(idx) (0x2 << ((idx) * 8))
  33. #define RGMII_SSR_1000(idx) (0x4 << ((idx) * 8))
  34. /* RGMII bridge supports only GMII/TBI and RGMII/RTBI PHYs */
  35. static inline int rgmii_valid_mode(int phy_mode)
  36. {
  37. return phy_mode == PHY_MODE_GMII ||
  38. phy_mode == PHY_MODE_RGMII ||
  39. phy_mode == PHY_MODE_TBI ||
  40. phy_mode == PHY_MODE_RTBI;
  41. }
  42. static inline const char *rgmii_mode_name(int mode)
  43. {
  44. switch (mode) {
  45. case PHY_MODE_RGMII:
  46. return "RGMII";
  47. case PHY_MODE_TBI:
  48. return "TBI";
  49. case PHY_MODE_GMII:
  50. return "GMII";
  51. case PHY_MODE_RTBI:
  52. return "RTBI";
  53. default:
  54. BUG();
  55. }
  56. }
  57. static inline u32 rgmii_mode_mask(int mode, int input)
  58. {
  59. switch (mode) {
  60. case PHY_MODE_RGMII:
  61. return RGMII_FER_RGMII(input);
  62. case PHY_MODE_TBI:
  63. return RGMII_FER_TBI(input);
  64. case PHY_MODE_GMII:
  65. return RGMII_FER_GMII(input);
  66. case PHY_MODE_RTBI:
  67. return RGMII_FER_RTBI(input);
  68. default:
  69. BUG();
  70. }
  71. }
  72. static int __init rgmii_init(struct ocp_device *ocpdev, int input, int mode)
  73. {
  74. struct ibm_ocp_rgmii *dev = ocp_get_drvdata(ocpdev);
  75. struct rgmii_regs *p;
  76. RGMII_DBG("%d: init(%d, %d)" NL, ocpdev->def->index, input, mode);
  77. if (!dev) {
  78. dev = kzalloc(sizeof(struct ibm_ocp_rgmii), GFP_KERNEL);
  79. if (!dev) {
  80. printk(KERN_ERR
  81. "rgmii%d: couldn't allocate device structure!\n",
  82. ocpdev->def->index);
  83. return -ENOMEM;
  84. }
  85. p = (struct rgmii_regs *)ioremap(ocpdev->def->paddr,
  86. sizeof(struct rgmii_regs));
  87. if (!p) {
  88. printk(KERN_ERR
  89. "rgmii%d: could not ioremap device registers!\n",
  90. ocpdev->def->index);
  91. kfree(dev);
  92. return -ENOMEM;
  93. }
  94. dev->base = p;
  95. ocp_set_drvdata(ocpdev, dev);
  96. /* Disable all inputs by default */
  97. out_be32(&p->fer, 0);
  98. } else
  99. p = dev->base;
  100. /* Enable this input */
  101. out_be32(&p->fer, in_be32(&p->fer) | rgmii_mode_mask(mode, input));
  102. printk(KERN_NOTICE "rgmii%d: input %d in %s mode\n",
  103. ocpdev->def->index, input, rgmii_mode_name(mode));
  104. ++dev->users;
  105. return 0;
  106. }
  107. int __init rgmii_attach(void *emac)
  108. {
  109. struct ocp_enet_private *dev = emac;
  110. struct ocp_func_emac_data *emacdata = dev->def->additions;
  111. /* Check if we need to attach to a RGMII */
  112. if (emacdata->rgmii_idx >= 0 && rgmii_valid_mode(emacdata->phy_mode)) {
  113. dev->rgmii_input = emacdata->rgmii_mux;
  114. dev->rgmii_dev =
  115. ocp_find_device(OCP_VENDOR_IBM, OCP_FUNC_RGMII,
  116. emacdata->rgmii_idx);
  117. if (!dev->rgmii_dev) {
  118. printk(KERN_ERR "emac%d: unknown rgmii%d!\n",
  119. dev->def->index, emacdata->rgmii_idx);
  120. return -ENODEV;
  121. }
  122. if (rgmii_init
  123. (dev->rgmii_dev, dev->rgmii_input, emacdata->phy_mode)) {
  124. printk(KERN_ERR
  125. "emac%d: rgmii%d initialization failed!\n",
  126. dev->def->index, emacdata->rgmii_idx);
  127. return -ENODEV;
  128. }
  129. }
  130. return 0;
  131. }
  132. void rgmii_set_speed(struct ocp_device *ocpdev, int input, int speed)
  133. {
  134. struct ibm_ocp_rgmii *dev = ocp_get_drvdata(ocpdev);
  135. u32 ssr = in_be32(&dev->base->ssr) & ~RGMII_SSR_MASK(input);
  136. RGMII_DBG("%d: speed(%d, %d)" NL, ocpdev->def->index, input, speed);
  137. if (speed == SPEED_1000)
  138. ssr |= RGMII_SSR_1000(input);
  139. else if (speed == SPEED_100)
  140. ssr |= RGMII_SSR_100(input);
  141. out_be32(&dev->base->ssr, ssr);
  142. }
  143. void __exit __rgmii_fini(struct ocp_device *ocpdev, int input)
  144. {
  145. struct ibm_ocp_rgmii *dev = ocp_get_drvdata(ocpdev);
  146. BUG_ON(!dev || dev->users == 0);
  147. RGMII_DBG("%d: fini(%d)" NL, ocpdev->def->index, input);
  148. /* Disable this input */
  149. out_be32(&dev->base->fer,
  150. in_be32(&dev->base->fer) & ~RGMII_FER_MASK(input));
  151. if (!--dev->users) {
  152. /* Free everything if this is the last user */
  153. ocp_set_drvdata(ocpdev, NULL);
  154. iounmap((void *)dev->base);
  155. kfree(dev);
  156. }
  157. }
  158. int __rgmii_get_regs_len(struct ocp_device *ocpdev)
  159. {
  160. return sizeof(struct emac_ethtool_regs_subhdr) +
  161. sizeof(struct rgmii_regs);
  162. }
  163. void *rgmii_dump_regs(struct ocp_device *ocpdev, void *buf)
  164. {
  165. struct ibm_ocp_rgmii *dev = ocp_get_drvdata(ocpdev);
  166. struct emac_ethtool_regs_subhdr *hdr = buf;
  167. struct rgmii_regs *regs = (struct rgmii_regs *)(hdr + 1);
  168. hdr->version = 0;
  169. hdr->index = ocpdev->def->index;
  170. memcpy_fromio(regs, dev->base, sizeof(struct rgmii_regs));
  171. return regs + 1;
  172. }