1/* $NetBSD: rgmii.c,v 1.2 2020/07/06 09:34:17 rin 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
28#include <sys/cdefs.h>
29__KERNEL_RCSID(0, "$NetBSD: rgmii.c,v 1.2 2020/07/06 09:34:17 rin Exp $");
30
31#include <sys/param.h>
32#include <sys/bus.h>
33#include <sys/device.h>
34
35#include <net/if.h>
36#include <net/if_media.h>
37
38#include <powerpc/ibm4xx/dev/rgmiireg.h>
39#include <powerpc/ibm4xx/dev/rmiivar.h>
40#include <powerpc/ibm4xx/dev/opbvar.h>
41
42
43static void rgmii_enable(device_t, int);
44static void rgmii_disable(device_t, int);
45static void rgmii_speed(device_t, int, int);
46
47
48void
49rgmii_attach(device_t self, int instance,
50	     void (**enable)(device_t, int),
51	     void (**disable)(device_t, int),
52	     void (**speed)(device_t, int, int))
53{
54	struct opb_softc *sc = device_private(self);
55	uint32_t ssr;
56
57	instance %= 2;
58
59	rgmii_disable(self, instance);
60	ssr = bus_space_read_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_SSR);
61	ssr &= ~SSR_SP(instance, SSR_SP_MASK);
62	bus_space_write_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_SSR, ssr);
63
64	*enable = rgmii_enable;
65	*disable = rgmii_disable;
66	*speed = rgmii_speed;
67}
68
69static void
70rgmii_enable(device_t self, int instance)
71{
72	struct opb_softc *sc = device_private(self);
73	uint32_t fer;
74
75	instance %= 2;
76
77	fer = bus_space_read_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_FER);
78	fer &= ~FER_MDIOEN_MASK;
79	fer |= FER_MDIOEN(instance);
80	bus_space_write_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_FER, fer);
81}
82
83static void
84rgmii_disable(device_t self, int instance)
85{
86	struct opb_softc *sc = device_private(self);
87	uint32_t fer;
88
89	instance %= 2;
90
91	fer = bus_space_read_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_FER);
92	fer &= ~FER_MDIOEN_MASK;
93	bus_space_write_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_FER, fer);
94}
95
96static void
97rgmii_speed(device_t self, int instance, int speed)
98{
99	struct opb_softc *sc = device_private(self);
100	uint32_t ssr;
101
102	instance %= 2;
103
104	ssr = bus_space_read_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_SSR);
105	ssr &= ~SSR_SP(instance, SSR_SP_MASK);
106	switch (speed) {
107	case IFM_1000_T:
108		ssr |= SSR_SP(instance, SSR_SP_1000MBPS);
109		break;
110	case IFM_100_TX:
111		ssr |= SSR_SP(instance, SSR_SP_100MBPS);
112		break;
113	case IFM_10_T:
114		ssr |= SSR_SP(instance, SSR_SP_10MBPS);
115		break;
116	}
117	bus_space_write_4(sc->sc_iot, sc->sc_rgmiih, RGMII0_SSR, ssr);
118}
119