ibm_emac_zmii.c 6.4 KB

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