xref: /freebsd/sys/dev/dpaa/fman_mdio.c (revision 5d3e7166)
1 /*-
2  * Copyright (c) 2016 Justin Hibbits
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/resource.h>
37 #include <sys/socket.h>
38 
39 #include <machine/bus.h>
40 
41 #include <net/if.h>
42 #include <net/if_media.h>
43 #include <net/if_types.h>
44 #include <net/if_var.h>
45 
46 #include <dev/mii/mii.h>
47 #include <dev/mii/miivar.h>
48 
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 
52 #include <contrib/ncsw/inc/Peripherals/fm_ext.h>
53 
54 #include "fman.h"
55 #include "miibus_if.h"
56 
57 #define MDIO_LOCK()	mtx_lock(&sc->sc_lock)
58 #define MDIO_UNLOCK()	mtx_unlock(&sc->sc_lock)
59 #define	MDIO_WRITE4(sc,r,v) \
60 	bus_space_write_4(&bs_be_tag, sc->sc_handle, sc->sc_offset + r, v)
61 #define	MDIO_READ4(sc, r) \
62 	bus_space_read_4(&bs_be_tag, sc->sc_handle, sc->sc_offset + r)
63 
64 #define	MDIO_MIIMCFG	0x0
65 #define	MDIO_MIIMCOM	0x4
66 #define	  MIIMCOM_SCAN_CYCLE	  0x00000002
67 #define	  MIIMCOM_READ_CYCLE	  0x00000001
68 #define	MDIO_MIIMADD	0x8
69 #define	MDIO_MIIMCON	0xc
70 #define	MDIO_MIIMSTAT	0x10
71 #define	MDIO_MIIMIND	0x14
72 #define	  MIIMIND_BUSY	  0x1
73 
74 static int pqmdio_fdt_probe(device_t dev);
75 static int pqmdio_fdt_attach(device_t dev);
76 static int pqmdio_detach(device_t dev);
77 static int pqmdio_miibus_readreg(device_t dev, int phy, int reg);
78 static int pqmdio_miibus_writereg(device_t dev, int phy, int reg, int value);
79 
80 struct pqmdio_softc {
81 	struct mtx sc_lock;
82 	bus_space_handle_t sc_handle;
83 	int sc_offset;
84 };
85 
86 static device_method_t pqmdio_methods[] = {
87 	/* Device interface */
88 	DEVMETHOD(device_probe,		pqmdio_fdt_probe),
89 	DEVMETHOD(device_attach,	pqmdio_fdt_attach),
90 	DEVMETHOD(device_detach,	pqmdio_detach),
91 
92 	/* MII interface */
93 	DEVMETHOD(miibus_readreg,	pqmdio_miibus_readreg),
94 	DEVMETHOD(miibus_writereg,	pqmdio_miibus_writereg),
95 
96 	{ 0, 0 }
97 };
98 
99 static struct ofw_compat_data mdio_compat_data[] = {
100 	{"fsl,fman-mdio", 0},
101 	{NULL, 0}
102 };
103 
104 static driver_t pqmdio_driver = {
105 	"pq_mdio",
106 	pqmdio_methods,
107 	sizeof(struct pqmdio_softc),
108 };
109 
110 static int
111 pqmdio_fdt_probe(device_t dev)
112 {
113 
114 	if (!ofw_bus_status_okay(dev))
115 		return (ENXIO);
116 
117 	if (!ofw_bus_search_compatible(dev, mdio_compat_data)->ocd_str)
118 		return (ENXIO);
119 
120 	device_set_desc(dev, "Freescale QorIQ MDIO");
121 
122 	return (BUS_PROBE_DEFAULT);
123 }
124 
125 static int
126 pqmdio_fdt_attach(device_t dev)
127 {
128 	struct pqmdio_softc *sc;
129 	rman_res_t start, count;
130 
131 	sc = device_get_softc(dev);
132 
133 	fman_get_bushandle(device_get_parent(dev), &sc->sc_handle);
134 	bus_get_resource(dev, SYS_RES_MEMORY, 0, &start, &count);
135 	sc->sc_offset = start;
136 
137 	OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
138 
139 	mtx_init(&sc->sc_lock, device_get_nameunit(dev), "QorIQ MDIO lock",
140 	    MTX_DEF);
141 
142 	return (0);
143 }
144 
145 static int
146 pqmdio_detach(device_t dev)
147 {
148 	struct pqmdio_softc *sc;
149 
150 	sc = device_get_softc(dev);
151 
152 	mtx_destroy(&sc->sc_lock);
153 
154 	return (0);
155 }
156 
157 int
158 pqmdio_miibus_readreg(device_t dev, int phy, int reg)
159 {
160 	struct pqmdio_softc *sc;
161 	int                  rv;
162 
163 	sc = device_get_softc(dev);
164 
165 	MDIO_LOCK();
166 
167 	MDIO_WRITE4(sc, MDIO_MIIMADD, (phy << 8) | reg);
168 	MDIO_WRITE4(sc, MDIO_MIIMCOM, MIIMCOM_READ_CYCLE);
169 
170 	MDIO_READ4(sc, MDIO_MIIMCOM);
171 
172 	while ((MDIO_READ4(sc, MDIO_MIIMIND)) & MIIMIND_BUSY)
173 		;
174 
175 	rv = MDIO_READ4(sc, MDIO_MIIMSTAT);
176 
177 	MDIO_WRITE4(sc, MDIO_MIIMCOM, 0);
178 	MDIO_READ4(sc, MDIO_MIIMCOM);
179 	MDIO_UNLOCK();
180 
181 	return (rv);
182 }
183 
184 int
185 pqmdio_miibus_writereg(device_t dev, int phy, int reg, int value)
186 {
187 	struct pqmdio_softc *sc;
188 
189 	sc = device_get_softc(dev);
190 
191 	MDIO_LOCK();
192 	/* Stop the MII management read cycle */
193 	MDIO_WRITE4(sc, MDIO_MIIMCOM, 0);
194 	MDIO_READ4(sc, MDIO_MIIMCOM);
195 
196 	MDIO_WRITE4(sc, MDIO_MIIMADD, (phy << 8) | reg);
197 
198 	MDIO_WRITE4(sc, MDIO_MIIMCON, value);
199 	MDIO_READ4(sc, MDIO_MIIMCON);
200 
201 	/* Wait till MII management write is complete */
202 	while ((MDIO_READ4(sc, MDIO_MIIMIND)) & MIIMIND_BUSY)
203 		;
204 	MDIO_UNLOCK();
205 
206 	return (0);
207 }
208 
209 EARLY_DRIVER_MODULE(pqmdio, fman, pqmdio_driver, 0, 0,
210     BUS_PASS_SUPPORTDEV);
211 DRIVER_MODULE(miibus, pqmdio, miibus_driver, 0, 0);
212 MODULE_DEPEND(pqmdio, miibus, 1, 1, 1);
213 
214