summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/dev/mii/micphy.c
blob: 501f7f19f7bbd08e6ea57378ca99a41cf961c954 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <machine/rtems-bsd-kernel-space.h>

/*-
 * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
 * All rights reserved.
 *
 * This software was developed by SRI International and the University of
 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
 * ("CTSRD"), as part of the DARPA CRASH research programme.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

/*
 * Micrel KSZ9021 Gigabit Ethernet Transceiver
 */

#include <rtems/bsd/sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/malloc.h>

#include <machine/bus.h>

#include <net/if.h>
#include <net/if_media.h>

#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>
#include <rtems/bsd/local/miidevs.h>

#include <rtems/bsd/local/miibus_if.h>

#ifndef __rtems__
#include <dev/fdt/fdt_common.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#endif /* __rtems__ */

#define	MII_KSZPHY_EXTREG			0x0b
#define	 KSZPHY_EXTREG_WRITE			(1 << 15)
#define	MII_KSZPHY_EXTREG_WRITE			0x0c
#define	MII_KSZPHY_EXTREG_READ			0x0d
#define	MII_KSZPHY_CLK_CONTROL_PAD_SKEW		0x104
#define	MII_KSZPHY_RX_DATA_PAD_SKEW		0x105
#define	MII_KSZPHY_TX_DATA_PAD_SKEW		0x106

#define	PS_TO_REG(p)	((p) / 200)

static int micphy_probe(device_t);
static int micphy_attach(device_t);
static int micphy_service(struct mii_softc *, struct mii_data *, int);

static device_method_t micphy_methods[] = {
	/* device interface */
	DEVMETHOD(device_probe,		micphy_probe),
	DEVMETHOD(device_attach,	micphy_attach),
	DEVMETHOD(device_detach,	mii_phy_detach),
	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
	DEVMETHOD_END
};

static devclass_t micphy_devclass;

static driver_t micphy_driver = {
	"micphy",
	micphy_methods,
	sizeof(struct mii_softc)
};

DRIVER_MODULE(micphy, miibus, micphy_driver, micphy_devclass, 0, 0);

static const struct mii_phydesc micphys[] = {
	MII_PHY_DESC(MICREL, KSZ9021),
	MII_PHY_END
};

static const struct mii_phy_funcs micphy_funcs = {
	micphy_service,
	ukphy_status,
	mii_phy_reset
};

static void
micphy_write(struct mii_softc *sc, uint32_t reg, uint32_t val)
{

	PHY_WRITE(sc, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | reg);
	PHY_WRITE(sc, MII_KSZPHY_EXTREG_WRITE, val);
}

#ifndef __rtems__
static int
ksz9021_load_values(struct mii_softc *sc, phandle_t node, uint32_t reg,
			char *field1, char *field2,
			char *field3, char *field4)
{
	pcell_t dts_value[1];
	int len;
	int val;

	val = 0;

	if ((len = OF_getproplen(node, field1)) > 0) {
		OF_getencprop(node, field1, dts_value, len);
		val = PS_TO_REG(dts_value[0]);
	}

	if ((len = OF_getproplen(node, field2)) > 0) {
		OF_getencprop(node, field2, dts_value, len);
		val |= PS_TO_REG(dts_value[0]) << 4;
	}

	if ((len = OF_getproplen(node, field3)) > 0) {
		OF_getencprop(node, field3, dts_value, len);
		val |= PS_TO_REG(dts_value[0]) << 8;
	}

	if ((len = OF_getproplen(node, field4)) > 0) {
		OF_getencprop(node, field4, dts_value, len);
		val |= PS_TO_REG(dts_value[0]) << 12;
	}

	micphy_write(sc, reg, val);

	return (0);
}
#endif /* __rtems__ */

static int
micphy_probe(device_t dev)
{

	return (mii_phy_dev_probe(dev, micphys, BUS_PROBE_DEFAULT));
}

static int
micphy_attach(device_t dev)
{
	struct mii_softc *sc;
#ifndef __rtems__
	phandle_t node;
	device_t miibus;
	device_t parent;
#endif /* __rtems__ */

	sc = device_get_softc(dev);

	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &micphy_funcs, 1);
	mii_phy_setmedia(sc);

#ifndef __rtems__
	miibus = device_get_parent(dev);
	parent = device_get_parent(miibus);

	if ((node = ofw_bus_get_node(parent)) == -1)
		return (ENXIO);

	ksz9021_load_values(sc, node, MII_KSZPHY_CLK_CONTROL_PAD_SKEW,
			"txen-skew-ps", "txc-skew-ps",
			"rxdv-skew-ps", "rxc-skew-ps");

	ksz9021_load_values(sc, node, MII_KSZPHY_RX_DATA_PAD_SKEW,
			"rxd0-skew-ps", "rxd1-skew-ps",
			"rxd2-skew-ps", "rxd3-skew-ps");

	ksz9021_load_values(sc, node, MII_KSZPHY_TX_DATA_PAD_SKEW,
			"txd0-skew-ps", "txd1-skew-ps",
			"txd2-skew-ps", "txd3-skew-ps");
#else /* __rtems__ */
	/* FIXME */
	micphy_write(sc, MII_KSZPHY_CLK_CONTROL_PAD_SKEW, 0xf0f0);
	micphy_write(sc, MII_KSZPHY_RX_DATA_PAD_SKEW, 0x0000);
	micphy_write(sc, MII_KSZPHY_TX_DATA_PAD_SKEW, 0x0000);
#endif /* __rtems__ */

	return (0);
}

static int
micphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
{

	switch (cmd) {
	case MII_POLLSTAT:
		break;

	case MII_MEDIACHG:
		mii_phy_setmedia(sc);
		break;

	case MII_TICK:
		if (mii_phy_tick(sc) == EJUSTRETURN)
			return (0);
		break;
	}

	/* Update the media status. */
	PHY_STATUS(sc);

	/* Callback if something changed. */
	mii_phy_update(sc, cmd);
	return (0);
}