1/*- 2 * Copyright (c) 2006 Marius Strobl <marius@FreeBSD.org> 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/bus.h> 33#include <sys/endian.h> 34#include <sys/kernel.h> 35#include <sys/lock.h> 36#include <sys/module.h> 37#include <sys/mutex.h> 38#include <sys/resource.h> 39#include <sys/rman.h> 40#include <sys/socket.h> 41 42#include <dev/ofw/ofw_bus.h> 43 44#include <machine/bus.h> 45#include <machine/ofw_machdep.h> 46#include <machine/resource.h> 47 48#include <net/ethernet.h> 49#include <net/if.h> 50#include <net/if_media.h> 51 52#include <dev/le/lancereg.h> 53#include <dev/le/lancevar.h> 54#include <dev/le/am7990reg.h> 55#include <dev/le/am7990var.h> 56 57/* 58 * LANCE registers 59 */ 60#define LEREG1_RDP 0 /* Register Data port */ 61#define LEREG1_RAP 2 /* Register Address port */ 62 63struct le_lebuffer_softc { 64 struct am7990_softc sc_am7990; /* glue to MI code */ 65 66 struct resource *sc_bres; 67 68 struct resource *sc_rres; 69 70 struct resource *sc_ires; 71 void *sc_ih; 72}; 73 74static devclass_t le_lebuffer_devclass; 75 76static device_probe_t le_lebuffer_probe; 77static device_attach_t le_lebuffer_attach; 78static device_detach_t le_lebuffer_detach; 79static device_resume_t le_buffer_resume; 80static device_suspend_t le_buffer_suspend; 81 82static device_method_t le_lebuffer_methods[] = { 83 /* Device interface */ 84 DEVMETHOD(device_probe, le_lebuffer_probe), 85 DEVMETHOD(device_attach, le_lebuffer_attach), 86 DEVMETHOD(device_detach, le_lebuffer_detach), 87 /* We can just use the suspend method here. */ 88 DEVMETHOD(device_shutdown, le_buffer_suspend), 89 DEVMETHOD(device_suspend, le_buffer_suspend), 90 DEVMETHOD(device_resume, le_buffer_resume), 91 92 { 0, 0 } 93}; 94 95DEFINE_CLASS_0(le, le_lebuffer_driver, le_lebuffer_methods, 96 sizeof(struct le_lebuffer_softc)); 97DRIVER_MODULE(le, lebuffer, le_lebuffer_driver, le_lebuffer_devclass, 0, 0); 98MODULE_DEPEND(le, ether, 1, 1, 1); 99MODULE_DEPEND(le, lebuffer, 1, 1, 1); 100 101/* 102 * Media types supported 103 */ 104static const int le_lebuffer_media[] = { 105 IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0) 106}; 107#define NLEMEDIA nitems(le_lebuffer_media) 108 109static void le_lebuffer_wrcsr(struct lance_softc *, uint16_t, uint16_t); 110static uint16_t le_lebuffer_rdcsr(struct lance_softc *, uint16_t); 111static void le_lebuffer_copytodesc(struct lance_softc *, void *, int, int); 112static void le_lebuffer_copyfromdesc(struct lance_softc *, void *, int, int); 113static void le_lebuffer_copytobuf(struct lance_softc *, void *, int, int); 114static void le_lebuffer_copyfrombuf(struct lance_softc *, void *, int, int); 115static void le_lebuffer_zerobuf(struct lance_softc *, int, int); 116 117static void 118le_lebuffer_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val) 119{ 120 struct le_lebuffer_softc *lesc = (struct le_lebuffer_softc *)sc; 121 122 bus_write_2(lesc->sc_rres, LEREG1_RAP, port); 123 bus_barrier(lesc->sc_rres, LEREG1_RAP, 2, BUS_SPACE_BARRIER_WRITE); 124 bus_write_2(lesc->sc_rres, LEREG1_RDP, val); 125} 126 127static uint16_t 128le_lebuffer_rdcsr(struct lance_softc *sc, uint16_t port) 129{ 130 struct le_lebuffer_softc *lesc = (struct le_lebuffer_softc *)sc; 131 132 bus_write_2(lesc->sc_rres, LEREG1_RAP, port); 133 bus_barrier(lesc->sc_rres, LEREG1_RAP, 2, BUS_SPACE_BARRIER_WRITE); 134 return (bus_read_2(lesc->sc_rres, LEREG1_RDP)); 135} 136 137/* 138 * It turns out that using bus_space(9) to access the buffers and the 139 * descriptors yields way more throughput than accessing them via the 140 * KVA returned by rman_get_virtual(9). The descriptor rings can be 141 * accessed using 8-bit up to 64-bit operations while the buffers can 142 * be only accessed using 8-bit and 16-bit operations. 143 * NB: For whatever reason setting LE_C3_BSWP has no effect with at 144 * least the 501-2981 (although their 'busmaster-regval' property 145 * indicates to set LE_C3_BSWP also for these cards), so we need 146 * to manually byte swap access to the buffers, i.e. the accesses 147 * going through the RX/TX FIFOs. 148 */ 149 150static void 151le_lebuffer_copytodesc(struct lance_softc *sc, void *fromv, int off, int len) 152{ 153 struct le_lebuffer_softc *lesc = (struct le_lebuffer_softc *)sc; 154 caddr_t from = fromv; 155 156 for (; len >= 8; len -= 8, off += 8, from += 8) 157 bus_write_8(lesc->sc_bres, off, be64dec(from)); 158 for (; len >= 4; len -= 4, off += 4, from += 4) 159 bus_write_4(lesc->sc_bres, off, be32dec(from)); 160 for (; len >= 2; len -= 2, off += 2, from += 2) 161 bus_write_2(lesc->sc_bres, off, be16dec(from)); 162 if (len == 1) 163 bus_write_1(lesc->sc_bres, off, *from); 164} 165 166static void 167le_lebuffer_copyfromdesc(struct lance_softc *sc, void *tov, int off, int len) 168{ 169 struct le_lebuffer_softc *lesc = (struct le_lebuffer_softc *)sc; 170 caddr_t to = tov; 171 172 for (; len >= 8; len -= 8, off += 8, to += 8) 173 be64enc(to, 174 bus_read_8(lesc->sc_bres, off)); 175 for (; len >= 4; len -= 4, off += 4, to += 4) 176 be32enc(to, 177 bus_read_4(lesc->sc_bres, off)); 178 for (; len >= 2; len -= 2, off += 2, to += 2) 179 be16enc(to, 180 bus_read_2(lesc->sc_bres, off)); 181 if (len == 1) 182 *to = bus_read_1(lesc->sc_bres, off); 183} 184 185static void 186le_lebuffer_copytobuf(struct lance_softc *sc, void *fromv, int off, int len) 187{ 188 struct le_lebuffer_softc *lesc = (struct le_lebuffer_softc *)sc; 189 caddr_t from = fromv; 190 191 for (; len >= 2; len -= 2, off += 2, from += 2) 192 bus_write_2(lesc->sc_bres, off, le16dec(from)); 193 if (len == 1) 194 bus_write_1(lesc->sc_bres, off + 1, *from); 195} 196 197static void 198le_lebuffer_copyfrombuf(struct lance_softc *sc, void *tov, int off, int len) 199{ 200 struct le_lebuffer_softc *lesc = (struct le_lebuffer_softc *)sc; 201 caddr_t to = tov; 202 203 for (; len >= 2; len -= 2, off += 2, to += 2) 204 le16enc(to, 205 bus_read_2(lesc->sc_bres, off)); 206 if (len == 1) 207 *to = bus_read_1(lesc->sc_bres, off + 1); 208} 209 210static void 211le_lebuffer_zerobuf(struct lance_softc *sc, int off, int len) 212{ 213 struct le_lebuffer_softc *lesc = (struct le_lebuffer_softc *)sc; 214 215 for (; len >= 2; len -= 2, off += 2) 216 bus_write_2(lesc->sc_bres, off, 0); 217 if (len == 1) 218 bus_write_1(lesc->sc_bres, off + 1, 0); 219} 220 221static int 222le_lebuffer_probe(device_t dev) 223{ 224 225 if (strcmp(ofw_bus_get_name(dev), "le") == 0) { 226 device_set_desc(dev, "LANCE Ethernet"); 227 return (BUS_PROBE_DEFAULT); 228 } 229 return (ENXIO); 230} 231 232static int 233le_lebuffer_attach(device_t dev) 234{ 235 struct le_lebuffer_softc *lesc; 236 struct lance_softc *sc; 237 int error, i; 238 239 lesc = device_get_softc(dev); 240 sc = &lesc->sc_am7990.lsc; 241 242 LE_LOCK_INIT(sc, device_get_nameunit(dev)); 243 244 /* 245 * The "register space" of the parent is just a buffer where the 246 * the LANCE descriptor rings and the RX/TX buffers can be stored. 247 */ 248 i = 0; 249 lesc->sc_bres = bus_alloc_resource_any(device_get_parent(dev), 250 SYS_RES_MEMORY, &i, RF_ACTIVE); 251 if (lesc->sc_bres == NULL) { 252 device_printf(dev, "cannot allocate LANCE buffer\n"); 253 error = ENXIO; 254 goto fail_mtx; 255 } 256 257 /* Allocate LANCE registers. */ 258 i = 0; 259 lesc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 260 &i, RF_ACTIVE); 261 if (lesc->sc_rres == NULL) { 262 device_printf(dev, "cannot allocate LANCE registers\n"); 263 error = ENXIO; 264 goto fail_bres; 265 } 266 267 /* Allocate LANCE interrupt. */ 268 i = 0; 269 if ((lesc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, 270 &i, RF_SHAREABLE | RF_ACTIVE)) == NULL) { 271 device_printf(dev, "cannot allocate interrupt\n"); 272 error = ENXIO; 273 goto fail_rres; 274 } 275 276 /* 277 * LANCE view is offset by buffer location. 278 * Note that we don't use sc->sc_mem. 279 */ 280 sc->sc_addr = 0; 281 sc->sc_memsize = rman_get_size(lesc->sc_bres); 282 sc->sc_flags = 0; 283 284 /* That old black magic... */ 285 if (OF_getprop(ofw_bus_get_node(dev), "busmaster-regval", 286 &sc->sc_conf3, sizeof(sc->sc_conf3)) == -1) 287 sc->sc_conf3 = LE_C3_ACON | LE_C3_BCON; 288 /* 289 * Make sure LE_C3_BSWP is cleared so that for cards where 290 * that flag actually works le_lebuffer_copy{from,to}buf() 291 * don't fail... 292 */ 293 sc->sc_conf3 &= ~LE_C3_BSWP; 294 295 OF_getetheraddr(dev, sc->sc_enaddr); 296 297 sc->sc_copytodesc = le_lebuffer_copytodesc; 298 sc->sc_copyfromdesc = le_lebuffer_copyfromdesc; 299 sc->sc_copytobuf = le_lebuffer_copytobuf; 300 sc->sc_copyfrombuf = le_lebuffer_copyfrombuf; 301 sc->sc_zerobuf = le_lebuffer_zerobuf; 302 303 sc->sc_rdcsr = le_lebuffer_rdcsr; 304 sc->sc_wrcsr = le_lebuffer_wrcsr; 305 sc->sc_hwreset = NULL; 306 sc->sc_hwinit = NULL; 307 sc->sc_hwintr = NULL; 308 sc->sc_nocarrier = NULL; 309 sc->sc_mediachange = NULL; 310 sc->sc_mediastatus = NULL; 311 sc->sc_supmedia = le_lebuffer_media; 312 sc->sc_nsupmedia = NLEMEDIA; 313 sc->sc_defaultmedia = le_lebuffer_media[0]; 314 315 error = am7990_config(&lesc->sc_am7990, device_get_name(dev), 316 device_get_unit(dev)); 317 if (error != 0) { 318 device_printf(dev, "cannot attach Am7990\n"); 319 goto fail_ires; 320 } 321 322 error = bus_setup_intr(dev, lesc->sc_ires, INTR_TYPE_NET | INTR_MPSAFE, 323 NULL, am7990_intr, sc, &lesc->sc_ih); 324 if (error != 0) { 325 device_printf(dev, "cannot set up interrupt\n"); 326 goto fail_am7990; 327 } 328 329 return (0); 330 331 fail_am7990: 332 am7990_detach(&lesc->sc_am7990); 333 fail_ires: 334 bus_release_resource(dev, SYS_RES_IRQ, 335 rman_get_rid(lesc->sc_ires), lesc->sc_ires); 336 fail_rres: 337 bus_release_resource(dev, SYS_RES_MEMORY, 338 rman_get_rid(lesc->sc_rres), lesc->sc_rres); 339 fail_bres: 340 bus_release_resource(device_get_parent(dev), SYS_RES_MEMORY, 341 rman_get_rid(lesc->sc_bres), lesc->sc_bres); 342 fail_mtx: 343 LE_LOCK_DESTROY(sc); 344 return (error); 345} 346 347static int 348le_lebuffer_detach(device_t dev) 349{ 350 struct le_lebuffer_softc *lesc; 351 struct lance_softc *sc; 352 353 lesc = device_get_softc(dev); 354 sc = &lesc->sc_am7990.lsc; 355 356 bus_teardown_intr(dev, lesc->sc_ires, lesc->sc_ih); 357 am7990_detach(&lesc->sc_am7990); 358 bus_release_resource(dev, SYS_RES_IRQ, 359 rman_get_rid(lesc->sc_ires), lesc->sc_ires); 360 bus_release_resource(dev, SYS_RES_MEMORY, 361 rman_get_rid(lesc->sc_rres), lesc->sc_rres); 362 bus_release_resource(device_get_parent(dev), SYS_RES_MEMORY, 363 rman_get_rid(lesc->sc_bres), lesc->sc_bres); 364 LE_LOCK_DESTROY(sc); 365 366 return (0); 367} 368 369static int 370le_buffer_suspend(device_t dev) 371{ 372 struct le_lebuffer_softc *lesc; 373 374 lesc = device_get_softc(dev); 375 376 lance_suspend(&lesc->sc_am7990.lsc); 377 378 return (0); 379} 380 381static int 382le_buffer_resume(device_t dev) 383{ 384 struct le_lebuffer_softc *lesc; 385 386 lesc = device_get_softc(dev); 387 388 lance_resume(&lesc->sc_am7990.lsc); 389 390 return (0); 391} 392