source: rtems-libbsd/freebsd/sys/dev/mii/micphy.c @ 710d2a1

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 710d2a1 was 710d2a1, checked in by Sebastian Huber <sebastian.huber@…>, on 03/27/15 at 10:13:25

mii: Add phy devices

  • Property mode set to 100644
File size: 5.7 KB
RevLine 
[710d2a1]1#include <machine/rtems-bsd-kernel-space.h>
2
3/*-
4 * Copyright (c) 2014 Ruslan Bukin <br@bsdpad.com>
5 * All rights reserved.
6 *
7 * This software was developed by SRI International and the University of
8 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
9 * ("CTSRD"), as part of the DARPA CRASH research programme.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD$");
35
36/*
37 * Micrel KSZ9021 Gigabit Ethernet Transceiver
38 */
39
40#include <rtems/bsd/sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/socket.h>
44#include <sys/errno.h>
45#include <sys/module.h>
46#include <sys/bus.h>
47#include <sys/malloc.h>
48
49#include <machine/bus.h>
50
51#include <net/if.h>
52#include <net/if_media.h>
53
54#include <dev/mii/mii.h>
55#include <dev/mii/miivar.h>
56#include <rtems/bsd/local/miidevs.h>
57
58#include <rtems/bsd/local/miibus_if.h>
59
60#ifndef __rtems__
61#include <dev/fdt/fdt_common.h>
62#include <dev/ofw/openfirm.h>
63#include <dev/ofw/ofw_bus.h>
64#include <dev/ofw/ofw_bus_subr.h>
65#endif /* __rtems__ */
66
67#define MII_KSZPHY_EXTREG                       0x0b
68#define  KSZPHY_EXTREG_WRITE                    (1 << 15)
69#define MII_KSZPHY_EXTREG_WRITE                 0x0c
70#define MII_KSZPHY_EXTREG_READ                  0x0d
71#define MII_KSZPHY_CLK_CONTROL_PAD_SKEW         0x104
72#define MII_KSZPHY_RX_DATA_PAD_SKEW             0x105
73#define MII_KSZPHY_TX_DATA_PAD_SKEW             0x106
74
75#define PS_TO_REG(p)    ((p) / 200)
76
77static int micphy_probe(device_t);
78static int micphy_attach(device_t);
79static int micphy_service(struct mii_softc *, struct mii_data *, int);
80
81static device_method_t micphy_methods[] = {
82        /* device interface */
83        DEVMETHOD(device_probe,         micphy_probe),
84        DEVMETHOD(device_attach,        micphy_attach),
85        DEVMETHOD(device_detach,        mii_phy_detach),
86        DEVMETHOD(device_shutdown,      bus_generic_shutdown),
87        DEVMETHOD_END
88};
89
90static devclass_t micphy_devclass;
91
92static driver_t micphy_driver = {
93        "micphy",
94        micphy_methods,
95        sizeof(struct mii_softc)
96};
97
98DRIVER_MODULE(micphy, miibus, micphy_driver, micphy_devclass, 0, 0);
99
100static const struct mii_phydesc micphys[] = {
101        MII_PHY_DESC(MICREL, KSZ9021),
102        MII_PHY_END
103};
104
105static const struct mii_phy_funcs micphy_funcs = {
106        micphy_service,
107        ukphy_status,
108        mii_phy_reset
109};
110
111static void
112micphy_write(struct mii_softc *sc, uint32_t reg, uint32_t val)
113{
114
115        PHY_WRITE(sc, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | reg);
116        PHY_WRITE(sc, MII_KSZPHY_EXTREG_WRITE, val);
117}
118
119#ifndef __rtems__
120static int
121ksz9021_load_values(struct mii_softc *sc, phandle_t node, uint32_t reg,
122                        char *field1, char *field2,
123                        char *field3, char *field4)
124{
125        pcell_t dts_value[1];
126        int len;
127        int val;
128
129        val = 0;
130
131        if ((len = OF_getproplen(node, field1)) > 0) {
132                OF_getencprop(node, field1, dts_value, len);
133                val = PS_TO_REG(dts_value[0]);
134        }
135
136        if ((len = OF_getproplen(node, field2)) > 0) {
137                OF_getencprop(node, field2, dts_value, len);
138                val |= PS_TO_REG(dts_value[0]) << 4;
139        }
140
141        if ((len = OF_getproplen(node, field3)) > 0) {
142                OF_getencprop(node, field3, dts_value, len);
143                val |= PS_TO_REG(dts_value[0]) << 8;
144        }
145
146        if ((len = OF_getproplen(node, field4)) > 0) {
147                OF_getencprop(node, field4, dts_value, len);
148                val |= PS_TO_REG(dts_value[0]) << 12;
149        }
150
151        micphy_write(sc, reg, val);
152
153        return (0);
154}
155#endif /* __rtems__ */
156
157static int
158micphy_probe(device_t dev)
159{
160
161        return (mii_phy_dev_probe(dev, micphys, BUS_PROBE_DEFAULT));
162}
163
164static int
165micphy_attach(device_t dev)
166{
167        struct mii_softc *sc;
168#ifndef __rtems__
169        phandle_t node;
170        device_t miibus;
171        device_t parent;
172#endif /* __rtems__ */
173
174        sc = device_get_softc(dev);
175
176        mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &micphy_funcs, 1);
177        mii_phy_setmedia(sc);
178
179#ifndef __rtems__
180        miibus = device_get_parent(dev);
181        parent = device_get_parent(miibus);
182
183        if ((node = ofw_bus_get_node(parent)) == -1)
184                return (ENXIO);
185
186        ksz9021_load_values(sc, node, MII_KSZPHY_CLK_CONTROL_PAD_SKEW,
187                        "txen-skew-ps", "txc-skew-ps",
188                        "rxdv-skew-ps", "rxc-skew-ps");
189
190        ksz9021_load_values(sc, node, MII_KSZPHY_RX_DATA_PAD_SKEW,
191                        "rxd0-skew-ps", "rxd1-skew-ps",
192                        "rxd2-skew-ps", "rxd3-skew-ps");
193
194        ksz9021_load_values(sc, node, MII_KSZPHY_TX_DATA_PAD_SKEW,
195                        "txd0-skew-ps", "txd1-skew-ps",
196                        "txd2-skew-ps", "txd3-skew-ps");
197#endif /* __rtems__ */
198
199        return (0);
200}
201
202static int
203micphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
204{
205
206        switch (cmd) {
207        case MII_POLLSTAT:
208                break;
209
210        case MII_MEDIACHG:
211                mii_phy_setmedia(sc);
212                break;
213
214        case MII_TICK:
215                if (mii_phy_tick(sc) == EJUSTRETURN)
216                        return (0);
217                break;
218        }
219
220        /* Update the media status. */
221        PHY_STATUS(sc);
222
223        /* Callback if something changed. */
224        mii_phy_update(sc, cmd);
225        return (0);
226}
Note: See TracBrowser for help on using the repository browser.