xref: /netbsd-src/sys/arch/powerpc/ibm4xx/dev/rgmii.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /* $NetBSD: rgmii.c,v 1.1 2010/03/18 13:47:04 kiyohara Exp $ */
2 /*
3  * Copyright (c) 2010 KIYOHARA Takashi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: rgmii.c,v 1.1 2010/03/18 13:47:04 kiyohara Exp $");
29 
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/device.h>
33 
34 #include <net/if.h>
35 #include <net/if_media.h>
36 
37 #include <powerpc/ibm4xx/dev/rgmiireg.h>
38 #include <powerpc/ibm4xx/dev/rmiivar.h>
39 #include <powerpc/ibm4xx/dev/opbvar.h>
40 
41 
42 static void rgmii_enable(device_t, int);
43 static void rgmii_disable(device_t, int);
44 static void rgmii_speed(device_t, int, int);
45 
46 
47 void
48 rgmii_attach(device_t self, int instance,
49 	     void (**enable)(device_t, int),
50 	     void (**disable)(device_t, int),
51 	     void (**speed)(device_t, int, int))
52 {
53 	struct opb_softc *sc = device_private(self);
54 	uint32_t ssr;
55 
56 	instance %= 2;
57 
58 	rgmii_disable(self, instance);
59 	ssr = bus_space_read_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_SSR);
60 	ssr &= ~SSR_SP(instance, SSR_SP_MASK);
61 	bus_space_write_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_SSR, ssr);
62 
63 	*enable = rgmii_enable;
64 	*disable = rgmii_disable;
65 	*speed = rgmii_speed;
66 }
67 
68 static void
69 rgmii_enable(device_t self, int instance)
70 {
71 	struct opb_softc *sc = device_private(self);
72 	uint32_t fer;
73 
74 	instance %= 2;
75 
76 	fer = bus_space_read_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_FER);
77 	fer &= ~FER_MDIOEN_MASK;
78 	fer |= FER_MDIOEN(instance);
79 	bus_space_write_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_FER, fer);
80 }
81 
82 static void
83 rgmii_disable(device_t self, int instance)
84 {
85 	struct opb_softc *sc = device_private(self);
86 	uint32_t fer;
87 
88 	instance %= 2;
89 
90 	fer = bus_space_read_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_FER);
91 	fer &= ~FER_MDIOEN_MASK;
92 	bus_space_write_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_FER, fer);
93 }
94 
95 static void
96 rgmii_speed(device_t self, int instance, int speed)
97 {
98 	struct opb_softc *sc = device_private(self);
99 	uint32_t ssr;
100 
101 	instance %= 2;
102 
103 	ssr = bus_space_read_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_SSR);
104 	ssr &= ~SSR_SP(instance, SSR_SP_MASK);
105 	switch (speed) {
106 	case IFM_1000_T:
107 		ssr |= SSR_SP(instance, SSR_SP_1000MBPS);
108 		break;
109 	case IFM_100_TX:
110 		ssr |= SSR_SP(instance, SSR_SP_100MBPS);
111 		break;
112 	case IFM_10_T:
113 		ssr |= SSR_SP(instance, SSR_SP_10MBPS);
114 		break;
115 	}
116 	bus_space_write_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_SSR, ssr);
117 }
118