2 * Copyright (c) 2015,2016 Annapurna Labs Ltd. and affiliates
5 * Developed by Semihalf.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
39 #include <sys/resource.h>
41 #include <machine/bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
44 #include "al_serdes.h"
45 #include "alpine_serdes.h"
47 #define SERDES_NUM_GROUPS 5
49 static void *serdes_base;
50 static uint32_t serdes_grp_offset[] = {0, 0x400, 0x800, 0xc00, 0x2000};
52 static struct alpine_serdes_eth_group_mode {
54 enum alpine_serdes_eth_mode mode;
56 } alpine_serdes_eth_group_mode[SERDES_NUM_GROUPS];
58 static int al_serdes_probe(device_t dev);
59 static int al_serdes_attach(device_t dev);
60 static int al_serdes_detach(device_t dev);
62 static struct resource_spec al_serdes_spec[] = {
63 { SYS_RES_MEMORY, 0, RF_ACTIVE },
67 struct al_serdes_softc {
71 static device_method_t al_serdes_methods[] = {
72 DEVMETHOD(device_probe, al_serdes_probe),
73 DEVMETHOD(device_attach, al_serdes_attach),
74 DEVMETHOD(device_detach, al_serdes_detach),
79 static driver_t al_serdes_driver = {
82 sizeof(struct al_serdes_softc)
85 DRIVER_MODULE(al_serdes, simplebus, al_serdes_driver, 0, 0);
86 DRIVER_MODULE(al_serdes, ofwbus, al_serdes_driver, 0, 0);
89 al_serdes_probe(device_t dev)
92 if (!ofw_bus_status_okay(dev))
95 if (!ofw_bus_is_compatible(dev, "annapurna-labs,al-serdes"))
98 device_set_desc(dev, "Alpine Serdes");
100 return (BUS_PROBE_DEFAULT);
104 al_serdes_attach(device_t dev)
106 struct al_serdes_softc *sc;
109 sc = device_get_softc(dev);
111 err = bus_alloc_resources(dev, al_serdes_spec, &sc->res);
113 device_printf(dev, "could not allocate resources\n");
117 /* Initialize Serdes group locks and mode */
118 for (int i = 0; i < nitems(alpine_serdes_eth_group_mode); i++) {
119 mtx_init(&alpine_serdes_eth_group_mode[i].lock, "AlSerdesMtx",
121 alpine_serdes_eth_group_mode[i].mode_set = false;
124 serdes_base = (void *)rman_get_bushandle(sc->res);
130 al_serdes_detach(device_t dev)
132 struct al_serdes_softc *sc;
134 sc = device_get_softc(dev);
136 bus_release_resources(dev, al_serdes_spec, &sc->res);
138 for (int i = 0; i < nitems(alpine_serdes_eth_group_mode); i++) {
139 mtx_destroy(&alpine_serdes_eth_group_mode[i].lock);
140 alpine_serdes_eth_group_mode[i].mode_set = false;
147 alpine_serdes_resource_get(uint32_t group)
152 if (group >= SERDES_NUM_GROUPS)
155 if (serdes_base != NULL)
156 base = (void *)((uintptr_t)serdes_base +
157 serdes_grp_offset[group]);
163 alpine_serdes_eth_mode_set(uint32_t group, enum alpine_serdes_eth_mode mode)
165 struct alpine_serdes_eth_group_mode *group_mode;
167 group_mode = &alpine_serdes_eth_group_mode[group];
169 if (serdes_base == NULL)
172 if (group >= SERDES_NUM_GROUPS)
175 mtx_lock(&group_mode->lock);
177 if (!group_mode->mode_set || (group_mode->mode != mode)) {
178 struct al_serdes_grp_obj obj;
180 al_serdes_handle_grp_init(alpine_serdes_resource_get(group),
183 if (mode == ALPINE_SERDES_ETH_MODE_SGMII)
184 obj.mode_set_sgmii(&obj);
186 obj.mode_set_kr(&obj);
188 group_mode->mode = mode;
189 group_mode->mode_set = true;
192 mtx_unlock(&group_mode->lock);
198 alpine_serdes_eth_group_lock(uint32_t group)
200 struct alpine_serdes_eth_group_mode *group_mode;
202 group_mode = &alpine_serdes_eth_group_mode[group];
204 if (mtx_initialized(&group_mode->lock) == 0)
207 mtx_lock(&group_mode->lock);
211 alpine_serdes_eth_group_unlock(uint32_t group)
213 struct alpine_serdes_eth_group_mode *group_mode;
215 group_mode = &alpine_serdes_eth_group_mode[group];
217 if (mtx_initialized(&group_mode->lock) == 0)
220 mtx_unlock(&group_mode->lock);