ibm_emac_zmii.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * drivers/net/ibm_emac/ibm_emac_zmii.c
  3. *
  4. * Driver for PowerPC 4xx on-chip ethernet controller, ZMII 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. * Armin Kuster <akuster@mvista.com>
  11. * Copyright 2001 MontaVista Softare 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. /* ZMIIx_FER */
  26. #define ZMII_FER_MDI(idx) (0x80000000 >> ((idx) * 4))
  27. #define ZMII_FER_MDI_ALL (ZMII_FER_MDI(0) | ZMII_FER_MDI(1) | \
  28. ZMII_FER_MDI(2) | ZMII_FER_MDI(3))
  29. #define ZMII_FER_SMII(idx) (0x40000000 >> ((idx) * 4))
  30. #define ZMII_FER_RMII(idx) (0x20000000 >> ((idx) * 4))
  31. #define ZMII_FER_MII(idx) (0x10000000 >> ((idx) * 4))
  32. /* ZMIIx_SSR */
  33. #define ZMII_SSR_SCI(idx) (0x40000000 >> ((idx) * 4))
  34. #define ZMII_SSR_FSS(idx) (0x20000000 >> ((idx) * 4))
  35. #define ZMII_SSR_SP(idx) (0x10000000 >> ((idx) * 4))
  36. /* ZMII only supports MII, RMII and SMII
  37. * we also support autodetection for backward compatibility
  38. */
  39. static inline int zmii_valid_mode(int mode)
  40. {
  41. return mode == PHY_MODE_MII ||
  42. mode == PHY_MODE_RMII ||
  43. mode == PHY_MODE_SMII ||
  44. mode == PHY_MODE_NA;
  45. }
  46. static inline const char *zmii_mode_name(int mode)
  47. {
  48. switch (mode) {
  49. case PHY_MODE_MII:
  50. return "MII";
  51. case PHY_MODE_RMII:
  52. return "RMII";
  53. case PHY_MODE_SMII:
  54. return "SMII";
  55. default:
  56. BUG();
  57. }
  58. }
  59. static inline u32 zmii_mode_mask(int mode, int input)
  60. {
  61. switch (mode) {
  62. case PHY_MODE_MII:
  63. return ZMII_FER_MII(input);
  64. case PHY_MODE_RMII:
  65. return ZMII_FER_RMII(input);
  66. case PHY_MODE_SMII:
  67. return ZMII_FER_SMII(input);
  68. default:
  69. return 0;
  70. }
  71. }
  72. static int __init zmii_init(struct ocp_device *ocpdev, int input, int *mode)
  73. {
  74. struct ibm_ocp_zmii *dev = ocp_get_drvdata(ocpdev);
  75. struct zmii_regs __iomem *p;
  76. ZMII_DBG("%d: init(%d, %d)" NL, ocpdev->def->index, input, *mode);
  77. if (!dev) {
  78. dev = kzalloc(sizeof(struct ibm_ocp_zmii), GFP_KERNEL);
  79. if (!dev) {
  80. printk(KERN_ERR
  81. "zmii%d: couldn't allocate device structure!\n",
  82. ocpdev->def->index);
  83. return -ENOMEM;
  84. }
  85. dev->mode = PHY_MODE_NA;
  86. p = ioremap(ocpdev->def->paddr, sizeof(struct zmii_regs));
  87. if (!p) {
  88. printk(KERN_ERR
  89. "zmii%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. /* We may need FER value for autodetection later */
  97. dev->fer_save = in_be32(&p->fer);
  98. /* Disable all inputs by default */
  99. out_be32(&p->fer, 0);
  100. } else
  101. p = dev->base;
  102. if (!zmii_valid_mode(*mode)) {
  103. /* Probably an EMAC connected to RGMII,
  104. * but it still may need ZMII for MDIO
  105. */
  106. goto out;
  107. }
  108. /* Autodetect ZMII mode if not specified.
  109. * This is only for backward compatibility with the old driver.
  110. * Please, always specify PHY mode in your board port to avoid
  111. * any surprises.
  112. */
  113. if (dev->mode == PHY_MODE_NA) {
  114. if (*mode == PHY_MODE_NA) {
  115. u32 r = dev->fer_save;
  116. ZMII_DBG("%d: autodetecting mode, FER = 0x%08x" NL,
  117. ocpdev->def->index, r);
  118. if (r & (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
  119. dev->mode = PHY_MODE_MII;
  120. else if (r & (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
  121. dev->mode = PHY_MODE_RMII;
  122. else
  123. dev->mode = PHY_MODE_SMII;
  124. } else
  125. dev->mode = *mode;
  126. printk(KERN_NOTICE "zmii%d: bridge in %s mode\n",
  127. ocpdev->def->index, zmii_mode_name(dev->mode));
  128. } else {
  129. /* All inputs must use the same mode */
  130. if (*mode != PHY_MODE_NA && *mode != dev->mode) {
  131. printk(KERN_ERR
  132. "zmii%d: invalid mode %d specified for input %d\n",
  133. ocpdev->def->index, *mode, input);
  134. return -EINVAL;
  135. }
  136. }
  137. /* Report back correct PHY mode,
  138. * it may be used during PHY initialization.
  139. */
  140. *mode = dev->mode;
  141. /* Enable this input */
  142. out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
  143. out:
  144. ++dev->users;
  145. return 0;
  146. }
  147. int __init zmii_attach(void *emac)
  148. {
  149. struct ocp_enet_private *dev = emac;
  150. struct ocp_func_emac_data *emacdata = dev->def->additions;
  151. if (emacdata->zmii_idx >= 0) {
  152. dev->zmii_input = emacdata->zmii_mux;
  153. dev->zmii_dev =
  154. ocp_find_device(OCP_VENDOR_IBM, OCP_FUNC_ZMII,
  155. emacdata->zmii_idx);
  156. if (!dev->zmii_dev) {
  157. printk(KERN_ERR "emac%d: unknown zmii%d!\n",
  158. dev->def->index, emacdata->zmii_idx);
  159. return -ENODEV;
  160. }
  161. if (zmii_init
  162. (dev->zmii_dev, dev->zmii_input, &emacdata->phy_mode)) {
  163. printk(KERN_ERR
  164. "emac%d: zmii%d initialization failed!\n",
  165. dev->def->index, emacdata->zmii_idx);
  166. return -ENODEV;
  167. }
  168. }
  169. return 0;
  170. }
  171. void __zmii_enable_mdio(struct ocp_device *ocpdev, int input)
  172. {
  173. struct ibm_ocp_zmii *dev = ocp_get_drvdata(ocpdev);
  174. u32 fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
  175. ZMII_DBG2("%d: mdio(%d)" NL, ocpdev->def->index, input);
  176. out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
  177. }
  178. void __zmii_set_speed(struct ocp_device *ocpdev, int input, int speed)
  179. {
  180. struct ibm_ocp_zmii *dev = ocp_get_drvdata(ocpdev);
  181. u32 ssr = in_be32(&dev->base->ssr);
  182. ZMII_DBG("%d: speed(%d, %d)" NL, ocpdev->def->index, input, speed);
  183. if (speed == SPEED_100)
  184. ssr |= ZMII_SSR_SP(input);
  185. else
  186. ssr &= ~ZMII_SSR_SP(input);
  187. out_be32(&dev->base->ssr, ssr);
  188. }
  189. void __exit __zmii_fini(struct ocp_device *ocpdev, int input)
  190. {
  191. struct ibm_ocp_zmii *dev = ocp_get_drvdata(ocpdev);
  192. BUG_ON(!dev || dev->users == 0);
  193. ZMII_DBG("%d: fini(%d)" NL, ocpdev->def->index, input);
  194. /* Disable this input */
  195. out_be32(&dev->base->fer,
  196. in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
  197. if (!--dev->users) {
  198. /* Free everything if this is the last user */
  199. ocp_set_drvdata(ocpdev, NULL);
  200. iounmap(dev->base);
  201. kfree(dev);
  202. }
  203. }
  204. int __zmii_get_regs_len(struct ocp_device *ocpdev)
  205. {
  206. return sizeof(struct emac_ethtool_regs_subhdr) +
  207. sizeof(struct zmii_regs);
  208. }
  209. void *zmii_dump_regs(struct ocp_device *ocpdev, void *buf)
  210. {
  211. struct ibm_ocp_zmii *dev = ocp_get_drvdata(ocpdev);
  212. struct emac_ethtool_regs_subhdr *hdr = buf;
  213. struct zmii_regs *regs = (struct zmii_regs *)(hdr + 1);
  214. hdr->version = 0;
  215. hdr->index = ocpdev->def->index;
  216. memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
  217. return regs + 1;
  218. }