2 * drivers/net/ibm_emac/ibm_emac_zmii.c
4 * Driver for PowerPC 4xx on-chip ethernet controller, ZMII bridge support.
6 * Copyright (c) 2004, 2005 Zultys Technologies.
7 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
9 * Based on original work by
10 * Armin Kuster <akuster@mvista.com>
11 * Copyright 2001 MontaVista Softare Inc.
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.
19 #include <linux/config.h>
20 #include <linux/kernel.h>
21 #include <linux/ethtool.h>
24 #include "ibm_emac_core.h"
25 #include "ibm_emac_debug.h"
28 #define ZMII_FER_MDI(idx) (0x80000000 >> ((idx) * 4))
29 #define ZMII_FER_MDI_ALL (ZMII_FER_MDI(0) | ZMII_FER_MDI(1) | \
30 ZMII_FER_MDI(2) | ZMII_FER_MDI(3))
32 #define ZMII_FER_SMII(idx) (0x40000000 >> ((idx) * 4))
33 #define ZMII_FER_RMII(idx) (0x20000000 >> ((idx) * 4))
34 #define ZMII_FER_MII(idx) (0x10000000 >> ((idx) * 4))
37 #define ZMII_SSR_SCI(idx) (0x40000000 >> ((idx) * 4))
38 #define ZMII_SSR_FSS(idx) (0x20000000 >> ((idx) * 4))
39 #define ZMII_SSR_SP(idx) (0x10000000 >> ((idx) * 4))
41 /* ZMII only supports MII, RMII and SMII
42 * we also support autodetection for backward compatibility
44 static inline int zmii_valid_mode(int mode
)
46 return mode
== PHY_MODE_MII
||
47 mode
== PHY_MODE_RMII
||
48 mode
== PHY_MODE_SMII
||
52 static inline const char *zmii_mode_name(int mode
)
66 static inline u32
zmii_mode_mask(int mode
, int input
)
70 return ZMII_FER_MII(input
);
72 return ZMII_FER_RMII(input
);
74 return ZMII_FER_SMII(input
);
80 static int __init
zmii_init(struct ocp_device
*ocpdev
, int input
, int *mode
)
82 struct ibm_ocp_zmii
*dev
= ocp_get_drvdata(ocpdev
);
85 ZMII_DBG("%d: init(%d, %d)" NL
, ocpdev
->def
->index
, input
, *mode
);
88 dev
= kzalloc(sizeof(struct ibm_ocp_zmii
), GFP_KERNEL
);
91 "zmii%d: couldn't allocate device structure!\n",
95 dev
->mode
= PHY_MODE_NA
;
97 p
= (struct zmii_regs
*)ioremap(ocpdev
->def
->paddr
,
98 sizeof(struct zmii_regs
));
101 "zmii%d: could not ioremap device registers!\n",
107 ocp_set_drvdata(ocpdev
, dev
);
109 /* We may need FER value for autodetection later */
110 dev
->fer_save
= in_be32(&p
->fer
);
112 /* Disable all inputs by default */
113 out_be32(&p
->fer
, 0);
117 if (!zmii_valid_mode(*mode
)) {
118 /* Probably an EMAC connected to RGMII,
119 * but it still may need ZMII for MDIO
124 /* Autodetect ZMII mode if not specified.
125 * This is only for backward compatibility with the old driver.
126 * Please, always specify PHY mode in your board port to avoid
129 if (dev
->mode
== PHY_MODE_NA
) {
130 if (*mode
== PHY_MODE_NA
) {
131 u32 r
= dev
->fer_save
;
133 ZMII_DBG("%d: autodetecting mode, FER = 0x%08x" NL
,
134 ocpdev
->def
->index
, r
);
136 if (r
& (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
137 dev
->mode
= PHY_MODE_MII
;
138 else if (r
& (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
139 dev
->mode
= PHY_MODE_RMII
;
141 dev
->mode
= PHY_MODE_SMII
;
145 printk(KERN_NOTICE
"zmii%d: bridge in %s mode\n",
146 ocpdev
->def
->index
, zmii_mode_name(dev
->mode
));
148 /* All inputs must use the same mode */
149 if (*mode
!= PHY_MODE_NA
&& *mode
!= dev
->mode
) {
151 "zmii%d: invalid mode %d specified for input %d\n",
152 ocpdev
->def
->index
, *mode
, input
);
157 /* Report back correct PHY mode,
158 * it may be used during PHY initialization.
162 /* Enable this input */
163 out_be32(&p
->fer
, in_be32(&p
->fer
) | zmii_mode_mask(dev
->mode
, input
));
169 int __init
zmii_attach(void *emac
)
171 struct ocp_enet_private
*dev
= emac
;
172 struct ocp_func_emac_data
*emacdata
= dev
->def
->additions
;
174 if (emacdata
->zmii_idx
>= 0) {
175 dev
->zmii_input
= emacdata
->zmii_mux
;
177 ocp_find_device(OCP_VENDOR_IBM
, OCP_FUNC_ZMII
,
179 if (!dev
->zmii_dev
) {
180 printk(KERN_ERR
"emac%d: unknown zmii%d!\n",
181 dev
->def
->index
, emacdata
->zmii_idx
);
185 (dev
->zmii_dev
, dev
->zmii_input
, &emacdata
->phy_mode
)) {
187 "emac%d: zmii%d initialization failed!\n",
188 dev
->def
->index
, emacdata
->zmii_idx
);
195 void __zmii_enable_mdio(struct ocp_device
*ocpdev
, int input
)
197 struct ibm_ocp_zmii
*dev
= ocp_get_drvdata(ocpdev
);
198 u32 fer
= in_be32(&dev
->base
->fer
) & ~ZMII_FER_MDI_ALL
;
200 ZMII_DBG2("%d: mdio(%d)" NL
, ocpdev
->def
->index
, input
);
202 out_be32(&dev
->base
->fer
, fer
| ZMII_FER_MDI(input
));
205 void __zmii_set_speed(struct ocp_device
*ocpdev
, int input
, int speed
)
207 struct ibm_ocp_zmii
*dev
= ocp_get_drvdata(ocpdev
);
208 u32 ssr
= in_be32(&dev
->base
->ssr
);
210 ZMII_DBG("%d: speed(%d, %d)" NL
, ocpdev
->def
->index
, input
, speed
);
212 if (speed
== SPEED_100
)
213 ssr
|= ZMII_SSR_SP(input
);
215 ssr
&= ~ZMII_SSR_SP(input
);
217 out_be32(&dev
->base
->ssr
, ssr
);
220 void __exit
__zmii_fini(struct ocp_device
*ocpdev
, int input
)
222 struct ibm_ocp_zmii
*dev
= ocp_get_drvdata(ocpdev
);
223 BUG_ON(!dev
|| dev
->users
== 0);
225 ZMII_DBG("%d: fini(%d)" NL
, ocpdev
->def
->index
, input
);
227 /* Disable this input */
228 out_be32(&dev
->base
->fer
,
229 in_be32(&dev
->base
->fer
) & ~zmii_mode_mask(dev
->mode
, input
));
232 /* Free everything if this is the last user */
233 ocp_set_drvdata(ocpdev
, NULL
);
234 iounmap((void *)dev
->base
);
239 int __zmii_get_regs_len(struct ocp_device
*ocpdev
)
241 return sizeof(struct emac_ethtool_regs_subhdr
) +
242 sizeof(struct zmii_regs
);
245 void *zmii_dump_regs(struct ocp_device
*ocpdev
, void *buf
)
247 struct ibm_ocp_zmii
*dev
= ocp_get_drvdata(ocpdev
);
248 struct emac_ethtool_regs_subhdr
*hdr
= buf
;
249 struct zmii_regs
*regs
= (struct zmii_regs
*)(hdr
+ 1);
252 hdr
->index
= ocpdev
->def
->index
;
253 memcpy_fromio(regs
, dev
->base
, sizeof(struct zmii_regs
));