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 static devclass_t al_serdes_devclass;
87 DRIVER_MODULE(al_serdes, simplebus, al_serdes_driver,
88 al_serdes_devclass, 0, 0);
89 DRIVER_MODULE(al_serdes, ofwbus, al_serdes_driver,
90 al_serdes_devclass, 0, 0);
93 al_serdes_probe(device_t dev)
96 if (!ofw_bus_status_okay(dev))
99 if (!ofw_bus_is_compatible(dev, "annapurna-labs,al-serdes"))
102 device_set_desc(dev, "Alpine Serdes");
104 return (BUS_PROBE_DEFAULT);
108 al_serdes_attach(device_t dev)
110 struct al_serdes_softc *sc;
113 sc = device_get_softc(dev);
115 err = bus_alloc_resources(dev, al_serdes_spec, &sc->res);
117 device_printf(dev, "could not allocate resources\n");
121 /* Initialize Serdes group locks and mode */
122 for (int i = 0; i < nitems(alpine_serdes_eth_group_mode); i++) {
123 mtx_init(&alpine_serdes_eth_group_mode[i].lock, "AlSerdesMtx",
125 alpine_serdes_eth_group_mode[i].mode_set = false;
128 serdes_base = (void *)rman_get_bushandle(sc->res);
134 al_serdes_detach(device_t dev)
136 struct al_serdes_softc *sc;
138 sc = device_get_softc(dev);
140 bus_release_resources(dev, al_serdes_spec, &sc->res);
142 for (int i = 0; i < nitems(alpine_serdes_eth_group_mode); i++) {
143 mtx_destroy(&alpine_serdes_eth_group_mode[i].lock);
144 alpine_serdes_eth_group_mode[i].mode_set = false;
151 alpine_serdes_resource_get(uint32_t group)
156 if (group >= SERDES_NUM_GROUPS)
159 if (serdes_base != NULL)
160 base = (void *)((uintptr_t)serdes_base +
161 serdes_grp_offset[group]);
167 alpine_serdes_eth_mode_set(uint32_t group, enum alpine_serdes_eth_mode mode)
169 struct alpine_serdes_eth_group_mode *group_mode;
171 group_mode = &alpine_serdes_eth_group_mode[group];
173 if (serdes_base == NULL)
176 if (group >= SERDES_NUM_GROUPS)
179 mtx_lock(&group_mode->lock);
181 if (!group_mode->mode_set || (group_mode->mode != mode)) {
182 struct al_serdes_grp_obj obj;
184 al_serdes_handle_grp_init(alpine_serdes_resource_get(group),
187 if (mode == ALPINE_SERDES_ETH_MODE_SGMII)
188 obj.mode_set_sgmii(&obj);
190 obj.mode_set_kr(&obj);
192 group_mode->mode = mode;
193 group_mode->mode_set = true;
196 mtx_unlock(&group_mode->lock);
202 alpine_serdes_eth_group_lock(uint32_t group)
204 struct alpine_serdes_eth_group_mode *group_mode;
206 group_mode = &alpine_serdes_eth_group_mode[group];
208 if (mtx_initialized(&group_mode->lock) == 0)
211 mtx_lock(&group_mode->lock);
215 alpine_serdes_eth_group_unlock(uint32_t group)
217 struct alpine_serdes_eth_group_mode *group_mode;
219 group_mode = &alpine_serdes_eth_group_mode[group];
221 if (mtx_initialized(&group_mode->lock) == 0)
224 mtx_unlock(&group_mode->lock);