ibm_emac_zmii.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 *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 = (struct zmii_regs *)ioremap(ocpdev->def->paddr,
  87. sizeof(struct zmii_regs));
  88. if (!p) {
  89. printk(KERN_ERR
  90. "zmii%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. /* We may need FER value for autodetection later */
  98. dev->fer_save = in_be32(&p->fer);
  99. /* Disable all inputs by default */
  100. out_be32(&p->fer, 0);
  101. } else
  102. p = dev->base;
  103. if (!zmii_valid_mode(*mode)) {
  104. /* Probably an EMAC connected to RGMII,
  105. * but it still may need ZMII for MDIO
  106. */
  107. goto out;
  108. }
  109. /* Autodetect ZMII mode if not specified.
  110. * This is only for backward compatibility with the old driver.
  111. * Please, always specify PHY mode in your board port to avoid
  112. * any surprises.
  113. */
  114. if (dev->mode == PHY_MODE_NA) {
  115. if (*mode == PHY_MODE_NA) {
  116. u32 r = dev->fer_save;
  117. ZMII_DBG("%d: autodetecting mode, FER = 0x%08x" NL,
  118. ocpdev->def->index, r);
  119. if (r & (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
  120. dev->mode = PHY_MODE_MII;
  121. else if (r & (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
  122. dev->mode = PHY_MODE_RMII;
  123. else
  124. dev->mode = PHY_MODE_SMII;
  125. } else
  126. dev->mode = *mode;
  127. printk(KERN_NOTICE "zmii%d: bridge in %s mode\n",
  128. ocpdev->def->index, zmii_mode_name(dev->mode));
  129. } else {
  130. /* All inputs must use the same mode */
  131. if (*mode != PHY_MODE_NA && *mode != dev->mode) {
  132. printk(KERN_ERR
  133. "zmii%d: invalid mode %d specified for input %d\n",
  134. ocpdev->def->index, *mode, input);
  135. return -EINVAL;
  136. }
  137. }
  138. /* Report back correct PHY mode,
  139. * it may be used during PHY initialization.
  140. */
  141. *mode = dev->mode;
  142. /* Enable this input */
  143. out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
  144. out:
  145. ++dev->users;
  146. return 0;
  147. }
  148. int __init zmii_attach(void *emac)
  149. {
  150. struct ocp_enet_private *dev = emac;
  151. struct ocp_func_emac_data *emacdata = dev->def->additions;
  152. if (emacdata->zmii_idx >= 0) {
  153. dev->zmii_input = emacdata->zmii_mux;
  154. dev->zmii_dev =
  155. ocp_find_device(OCP_VENDOR_IBM, OCP_FUNC_ZMII,
  156. emacdata->zmii_idx);
  157. if (!dev->zmii_dev) {
  158. printk(KERN_ERR "emac%d: unknown zmii%d!\n",
  159. dev->def->index, emacdata->zmii_idx);
  160. return -ENODEV;
  161. }
  162. if (zmii_init
  163. (dev->zmii_dev, dev->zmii_input, &emacdata->phy_mode)) {
  164. printk(KERN_ERR
  165. "emac%d: zmii%d initialization failed!\n",
  166. dev->def->index, emacdata->zmii_idx);
  167. return -ENODEV;
  168. }
  169. }
  170. return 0;
  171. }
  172. void __zmii_enable_mdio(struct ocp_device *ocpdev, int input)
  173. {
  174. struct ibm_ocp_zmii *dev = ocp_get_drvdata(ocpdev);
  175. u32 fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
  176. ZMII_DBG2("%d: mdio(%d)" NL, ocpdev->def->index, input);
  177. out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
  178. }
  179. void __zmii_set_speed(struct ocp_device *ocpdev, int input, int speed)
  180. {
  181. struct ibm_ocp_zmii *dev = ocp_get_drvdata(ocpdev);
  182. u32 ssr = in_be32(&dev->base->ssr);
  183. ZMII_DBG("%d: speed(%d, %d)" NL, ocpdev->def->index, input, speed);
  184. if (speed == SPEED_100)
  185. ssr |= ZMII_SSR_SP(input);
  186. else
  187. ssr &= ~ZMII_SSR_SP(input);
  188. out_be32(&dev->base->ssr, ssr);
  189. }
  190. void __exit __zmii_fini(struct ocp_device *ocpdev, int input)
  191. {
  192. struct ibm_ocp_zmii *dev = ocp_get_drvdata(ocpdev);
  193. BUG_ON(!dev || dev->users == 0);
  194. ZMII_DBG("%d: fini(%d)" NL, ocpdev->def->index, input);
  195. /* Disable this input */
  196. out_be32(&dev->base->fer,
  197. in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
  198. if (!--dev->users) {
  199. /* Free everything if this is the last user */
  200. ocp_set_drvdata(ocpdev, NULL);
  201. iounmap((void *)dev->base);
  202. kfree(dev);
  203. }
  204. }
  205. int __zmii_get_regs_len(struct ocp_device *ocpdev)
  206. {
  207. return sizeof(struct emac_ethtool_regs_subhdr) +
  208. sizeof(struct zmii_regs);
  209. }
  210. void *zmii_dump_regs(struct ocp_device *ocpdev, void *buf)
  211. {
  212. struct ibm_ocp_zmii *dev = ocp_get_drvdata(ocpdev);
  213. struct emac_ethtool_regs_subhdr *hdr = buf;
  214. struct zmii_regs *regs = (struct zmii_regs *)(hdr + 1);
  215. hdr->version = 0;
  216. hdr->index = ocpdev->def->index;
  217. memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
  218. return regs + 1;
  219. }