ibm_emac_rgmii.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/config.h>
  20. #include <linux/kernel.h>
  21. #include <linux/ethtool.h>
  22. #include <asm/io.h>
  23. #include "ibm_emac_core.h"
  24. #include "ibm_emac_debug.h"
  25. /* RGMIIx_FER */
  26. #define RGMII_FER_MASK(idx) (0x7 << ((idx) * 4))
  27. #define RGMII_FER_RTBI(idx) (0x4 << ((idx) * 4))
  28. #define RGMII_FER_RGMII(idx) (0x5 << ((idx) * 4))
  29. #define RGMII_FER_TBI(idx) (0x6 << ((idx) * 4))
  30. #define RGMII_FER_GMII(idx) (0x7 << ((idx) * 4))
  31. /* RGMIIx_SSR */
  32. #define RGMII_SSR_MASK(idx) (0x7 << ((idx) * 8))
  33. #define RGMII_SSR_100(idx) (0x2 << ((idx) * 8))
  34. #define RGMII_SSR_1000(idx) (0x4 << ((idx) * 8))
  35. /* RGMII bridge supports only GMII/TBI and RGMII/RTBI PHYs */
  36. static inline int rgmii_valid_mode(int phy_mode)
  37. {
  38. return phy_mode == PHY_MODE_GMII ||
  39. phy_mode == PHY_MODE_RGMII ||
  40. phy_mode == PHY_MODE_TBI ||
  41. phy_mode == PHY_MODE_RTBI;
  42. }
  43. static inline const char *rgmii_mode_name(int mode)
  44. {
  45. switch (mode) {
  46. case PHY_MODE_RGMII:
  47. return "RGMII";
  48. case PHY_MODE_TBI:
  49. return "TBI";
  50. case PHY_MODE_GMII:
  51. return "GMII";
  52. case PHY_MODE_RTBI:
  53. return "RTBI";
  54. default:
  55. BUG();
  56. }
  57. }
  58. static inline u32 rgmii_mode_mask(int mode, int input)
  59. {
  60. switch (mode) {
  61. case PHY_MODE_RGMII:
  62. return RGMII_FER_RGMII(input);
  63. case PHY_MODE_TBI:
  64. return RGMII_FER_TBI(input);
  65. case PHY_MODE_GMII:
  66. return RGMII_FER_GMII(input);
  67. case PHY_MODE_RTBI:
  68. return RGMII_FER_RTBI(input);
  69. default:
  70. BUG();
  71. }
  72. }
  73. static int __init rgmii_init(struct ocp_device *ocpdev, int input, int mode)
  74. {
  75. struct ibm_ocp_rgmii *dev = ocp_get_drvdata(ocpdev);
  76. struct rgmii_regs *p;
  77. RGMII_DBG("%d: init(%d, %d)" NL, ocpdev->def->index, input, mode);
  78. if (!dev) {
  79. dev = kzalloc(sizeof(struct ibm_ocp_rgmii), GFP_KERNEL);
  80. if (!dev) {
  81. printk(KERN_ERR
  82. "rgmii%d: couldn't allocate device structure!\n",
  83. ocpdev->def->index);
  84. return -ENOMEM;
  85. }
  86. p = (struct rgmii_regs *)ioremap(ocpdev->def->paddr,
  87. sizeof(struct rgmii_regs));
  88. if (!p) {
  89. printk(KERN_ERR
  90. "rgmii%d: could not ioremap device registers!\n",
  91. ocpdev->def->index);
  92. kfree(dev);
  93. return -ENOMEM;
  94. }
  95. dev->base = p;
  96. ocp_set_drvdata(ocpdev, dev);
  97. /* Disable all inputs by default */
  98. out_be32(&p->fer, 0);
  99. } else
  100. p = dev->base;
  101. /* Enable this input */
  102. out_be32(&p->fer, in_be32(&p->fer) | rgmii_mode_mask(mode, input));
  103. printk(KERN_NOTICE "rgmii%d: input %d in %s mode\n",
  104. ocpdev->def->index, input, rgmii_mode_name(mode));
  105. ++dev->users;
  106. return 0;
  107. }
  108. int __init rgmii_attach(void *emac)
  109. {
  110. struct ocp_enet_private *dev = emac;
  111. struct ocp_func_emac_data *emacdata = dev->def->additions;
  112. /* Check if we need to attach to a RGMII */
  113. if (emacdata->rgmii_idx >= 0 && rgmii_valid_mode(emacdata->phy_mode)) {
  114. dev->rgmii_input = emacdata->rgmii_mux;
  115. dev->rgmii_dev =
  116. ocp_find_device(OCP_VENDOR_IBM, OCP_FUNC_RGMII,
  117. emacdata->rgmii_idx);
  118. if (!dev->rgmii_dev) {
  119. printk(KERN_ERR "emac%d: unknown rgmii%d!\n",
  120. dev->def->index, emacdata->rgmii_idx);
  121. return -ENODEV;
  122. }
  123. if (rgmii_init
  124. (dev->rgmii_dev, dev->rgmii_input, emacdata->phy_mode)) {
  125. printk(KERN_ERR
  126. "emac%d: rgmii%d initialization failed!\n",
  127. dev->def->index, emacdata->rgmii_idx);
  128. return -ENODEV;
  129. }
  130. }
  131. return 0;
  132. }
  133. void rgmii_set_speed(struct ocp_device *ocpdev, int input, int speed)
  134. {
  135. struct ibm_ocp_rgmii *dev = ocp_get_drvdata(ocpdev);
  136. u32 ssr = in_be32(&dev->base->ssr) & ~RGMII_SSR_MASK(input);
  137. RGMII_DBG("%d: speed(%d, %d)" NL, ocpdev->def->index, input, speed);
  138. if (speed == SPEED_1000)
  139. ssr |= RGMII_SSR_1000(input);
  140. else if (speed == SPEED_100)
  141. ssr |= RGMII_SSR_100(input);
  142. out_be32(&dev->base->ssr, ssr);
  143. }
  144. void __exit __rgmii_fini(struct ocp_device *ocpdev, int input)
  145. {
  146. struct ibm_ocp_rgmii *dev = ocp_get_drvdata(ocpdev);
  147. BUG_ON(!dev || dev->users == 0);
  148. RGMII_DBG("%d: fini(%d)" NL, ocpdev->def->index, input);
  149. /* Disable this input */
  150. out_be32(&dev->base->fer,
  151. in_be32(&dev->base->fer) & ~RGMII_FER_MASK(input));
  152. if (!--dev->users) {
  153. /* Free everything if this is the last user */
  154. ocp_set_drvdata(ocpdev, NULL);
  155. iounmap((void *)dev->base);
  156. kfree(dev);
  157. }
  158. }
  159. int __rgmii_get_regs_len(struct ocp_device *ocpdev)
  160. {
  161. return sizeof(struct emac_ethtool_regs_subhdr) +
  162. sizeof(struct rgmii_regs);
  163. }
  164. void *rgmii_dump_regs(struct ocp_device *ocpdev, void *buf)
  165. {
  166. struct ibm_ocp_rgmii *dev = ocp_get_drvdata(ocpdev);
  167. struct emac_ethtool_regs_subhdr *hdr = buf;
  168. struct rgmii_regs *regs = (struct rgmii_regs *)(hdr + 1);
  169. hdr->version = 0;
  170. hdr->index = ocpdev->def->index;
  171. memcpy_fromio(regs, dev->base, sizeof(struct rgmii_regs));
  172. return regs + 1;
  173. }