1 /*- 2 * Copyright (c) 2015,2016 Annapurna Labs Ltd. and affiliates 3 * All rights reserved. 4 * 5 * Developed by Semihalf. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/module.h> 36 #include <sys/bus.h> 37 #include <sys/rman.h> 38 #include <sys/conf.h> 39 #include <sys/resource.h> 40 41 #include <machine/bus.h> 42 #include <dev/ofw/ofw_bus_subr.h> 43 44 #include "al_serdes.h" 45 #include "alpine_serdes.h" 46 47 #define SERDES_NUM_GROUPS 5 48 49 static void *serdes_base; 50 static uint32_t serdes_grp_offset[] = {0, 0x400, 0x800, 0xc00, 0x2000}; 51 52 static struct alpine_serdes_eth_group_mode { 53 struct mtx lock; 54 enum alpine_serdes_eth_mode mode; 55 bool mode_set; 56 } alpine_serdes_eth_group_mode[SERDES_NUM_GROUPS]; 57 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); 61 62 static struct resource_spec al_serdes_spec[] = { 63 { SYS_RES_MEMORY, 0, RF_ACTIVE }, 64 { -1, 0 } 65 }; 66 67 struct al_serdes_softc { 68 struct resource *res; 69 }; 70 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), 75 76 DEVMETHOD_END 77 }; 78 79 static driver_t al_serdes_driver = { 80 "serdes", 81 al_serdes_methods, 82 sizeof(struct al_serdes_softc) 83 }; 84 85 DRIVER_MODULE(al_serdes, simplebus, al_serdes_driver, 0, 0); 86 DRIVER_MODULE(al_serdes, ofwbus, al_serdes_driver, 0, 0); 87 88 static int 89 al_serdes_probe(device_t dev) 90 { 91 92 if (!ofw_bus_status_okay(dev)) 93 return (ENXIO); 94 95 if (!ofw_bus_is_compatible(dev, "annapurna-labs,al-serdes")) 96 return (ENXIO); 97 98 device_set_desc(dev, "Alpine Serdes"); 99 100 return (BUS_PROBE_DEFAULT); 101 } 102 103 static int 104 al_serdes_attach(device_t dev) 105 { 106 struct al_serdes_softc *sc; 107 int err; 108 109 sc = device_get_softc(dev); 110 111 err = bus_alloc_resources(dev, al_serdes_spec, &sc->res); 112 if (err != 0) { 113 device_printf(dev, "could not allocate resources\n"); 114 return (err); 115 } 116 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", 120 NULL, MTX_DEF); 121 alpine_serdes_eth_group_mode[i].mode_set = false; 122 } 123 124 serdes_base = (void *)rman_get_bushandle(sc->res); 125 126 return (0); 127 } 128 129 static int 130 al_serdes_detach(device_t dev) 131 { 132 struct al_serdes_softc *sc; 133 134 sc = device_get_softc(dev); 135 136 bus_release_resources(dev, al_serdes_spec, &sc->res); 137 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; 141 } 142 143 return (0); 144 } 145 146 void * 147 alpine_serdes_resource_get(uint32_t group) 148 { 149 void *base; 150 151 base = NULL; 152 if (group >= SERDES_NUM_GROUPS) 153 return (NULL); 154 155 if (serdes_base != NULL) 156 base = (void *)((uintptr_t)serdes_base + 157 serdes_grp_offset[group]); 158 159 return (base); 160 } 161 162 int 163 alpine_serdes_eth_mode_set(uint32_t group, enum alpine_serdes_eth_mode mode) 164 { 165 struct alpine_serdes_eth_group_mode *group_mode; 166 167 group_mode = &alpine_serdes_eth_group_mode[group]; 168 169 if (serdes_base == NULL) 170 return (EINVAL); 171 172 if (group >= SERDES_NUM_GROUPS) 173 return (EINVAL); 174 175 mtx_lock(&group_mode->lock); 176 177 if (!group_mode->mode_set || (group_mode->mode != mode)) { 178 struct al_serdes_grp_obj obj; 179 180 al_serdes_handle_grp_init(alpine_serdes_resource_get(group), 181 group, &obj); 182 183 if (mode == ALPINE_SERDES_ETH_MODE_SGMII) 184 obj.mode_set_sgmii(&obj); 185 else 186 obj.mode_set_kr(&obj); 187 188 group_mode->mode = mode; 189 group_mode->mode_set = true; 190 } 191 192 mtx_unlock(&group_mode->lock); 193 194 return (0); 195 } 196 197 void 198 alpine_serdes_eth_group_lock(uint32_t group) 199 { 200 struct alpine_serdes_eth_group_mode *group_mode; 201 202 group_mode = &alpine_serdes_eth_group_mode[group]; 203 204 if (mtx_initialized(&group_mode->lock) == 0) 205 return; 206 207 mtx_lock(&group_mode->lock); 208 } 209 210 void 211 alpine_serdes_eth_group_unlock(uint32_t group) 212 { 213 struct alpine_serdes_eth_group_mode *group_mode; 214 215 group_mode = &alpine_serdes_eth_group_mode[group]; 216 217 if (mtx_initialized(&group_mode->lock) == 0) 218 return; 219 220 mtx_unlock(&group_mode->lock); 221 } 222