2 * drivers/net/ibm_emac/ibm_emac_rgmii.c
4 * Driver for PowerPC 4xx on-chip ethernet controller, RGMII 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 * Matt Porter <mporter@kernel.crashing.org>
11 * Copyright 2004 MontaVista Software, 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/kernel.h>
20 #include <linux/ethtool.h>
23 #include "ibm_emac_core.h"
24 #include "ibm_emac_debug.h"
27 #define RGMII_FER_MASK(idx) (0x7 << ((idx) * 4))
28 #define RGMII_FER_RTBI(idx) (0x4 << ((idx) * 4))
29 #define RGMII_FER_RGMII(idx) (0x5 << ((idx) * 4))
30 #define RGMII_FER_TBI(idx) (0x6 << ((idx) * 4))
31 #define RGMII_FER_GMII(idx) (0x7 << ((idx) * 4))
34 #define RGMII_SSR_MASK(idx) (0x7 << ((idx) * 8))
35 #define RGMII_SSR_100(idx) (0x2 << ((idx) * 8))
36 #define RGMII_SSR_1000(idx) (0x4 << ((idx) * 8))
38 /* RGMII bridge supports only GMII/TBI and RGMII/RTBI PHYs */
39 static inline int rgmii_valid_mode(int phy_mode
)
41 return phy_mode
== PHY_MODE_GMII
||
42 phy_mode
== PHY_MODE_RGMII
||
43 phy_mode
== PHY_MODE_TBI
||
44 phy_mode
== PHY_MODE_RTBI
;
47 static inline const char *rgmii_mode_name(int mode
)
63 static inline u32
rgmii_mode_mask(int mode
, int input
)
67 return RGMII_FER_RGMII(input
);
69 return RGMII_FER_TBI(input
);
71 return RGMII_FER_GMII(input
);
73 return RGMII_FER_RTBI(input
);
79 static int __init
rgmii_init(struct ocp_device
*ocpdev
, int input
, int mode
)
81 struct ibm_ocp_rgmii
*dev
= ocp_get_drvdata(ocpdev
);
84 RGMII_DBG("%d: init(%d, %d)" NL
, ocpdev
->def
->index
, input
, mode
);
87 dev
= kzalloc(sizeof(struct ibm_ocp_rgmii
), GFP_KERNEL
);
90 "rgmii%d: couldn't allocate device structure!\n",
95 p
= (struct rgmii_regs
*)ioremap(ocpdev
->def
->paddr
,
96 sizeof(struct rgmii_regs
));
99 "rgmii%d: could not ioremap device registers!\n",
106 ocp_set_drvdata(ocpdev
, dev
);
108 /* Disable all inputs by default */
109 out_be32(&p
->fer
, 0);
113 /* Enable this input */
114 out_be32(&p
->fer
, in_be32(&p
->fer
) | rgmii_mode_mask(mode
, input
));
116 printk(KERN_NOTICE
"rgmii%d: input %d in %s mode\n",
117 ocpdev
->def
->index
, input
, rgmii_mode_name(mode
));
123 int __init
rgmii_attach(void *emac
)
125 struct ocp_enet_private
*dev
= emac
;
126 struct ocp_func_emac_data
*emacdata
= dev
->def
->additions
;
128 /* Check if we need to attach to a RGMII */
129 if (emacdata
->rgmii_idx
>= 0 && rgmii_valid_mode(emacdata
->phy_mode
)) {
130 dev
->rgmii_input
= emacdata
->rgmii_mux
;
132 ocp_find_device(OCP_VENDOR_IBM
, OCP_FUNC_RGMII
,
133 emacdata
->rgmii_idx
);
134 if (!dev
->rgmii_dev
) {
135 printk(KERN_ERR
"emac%d: unknown rgmii%d!\n",
136 dev
->def
->index
, emacdata
->rgmii_idx
);
140 (dev
->rgmii_dev
, dev
->rgmii_input
, emacdata
->phy_mode
)) {
142 "emac%d: rgmii%d initialization failed!\n",
143 dev
->def
->index
, emacdata
->rgmii_idx
);
150 void rgmii_set_speed(struct ocp_device
*ocpdev
, int input
, int speed
)
152 struct ibm_ocp_rgmii
*dev
= ocp_get_drvdata(ocpdev
);
153 u32 ssr
= in_be32(&dev
->base
->ssr
) & ~RGMII_SSR_MASK(input
);
155 RGMII_DBG("%d: speed(%d, %d)" NL
, ocpdev
->def
->index
, input
, speed
);
157 if (speed
== SPEED_1000
)
158 ssr
|= RGMII_SSR_1000(input
);
159 else if (speed
== SPEED_100
)
160 ssr
|= RGMII_SSR_100(input
);
162 out_be32(&dev
->base
->ssr
, ssr
);
165 void __exit
__rgmii_fini(struct ocp_device
*ocpdev
, int input
)
167 struct ibm_ocp_rgmii
*dev
= ocp_get_drvdata(ocpdev
);
168 BUG_ON(!dev
|| dev
->users
== 0);
170 RGMII_DBG("%d: fini(%d)" NL
, ocpdev
->def
->index
, input
);
172 /* Disable this input */
173 out_be32(&dev
->base
->fer
,
174 in_be32(&dev
->base
->fer
) & ~RGMII_FER_MASK(input
));
177 /* Free everything if this is the last user */
178 ocp_set_drvdata(ocpdev
, NULL
);
179 iounmap((void *)dev
->base
);
184 int __rgmii_get_regs_len(struct ocp_device
*ocpdev
)
186 return sizeof(struct emac_ethtool_regs_subhdr
) +
187 sizeof(struct rgmii_regs
);
190 void *rgmii_dump_regs(struct ocp_device
*ocpdev
, void *buf
)
192 struct ibm_ocp_rgmii
*dev
= ocp_get_drvdata(ocpdev
);
193 struct emac_ethtool_regs_subhdr
*hdr
= buf
;
194 struct rgmii_regs
*regs
= (struct rgmii_regs
*)(hdr
+ 1);
197 hdr
->index
= ocpdev
->def
->index
;
198 memcpy_fromio(regs
, dev
->base
, sizeof(struct rgmii_regs
));