Home | History | Annotate | Line # | Download | only in broadcom
bcm53xx_eth.c revision 1.1
      1 /*-
      2  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Matt Thomas of 3am Software Foundry.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #define GMAC_PRIVATE
     31 
     32 #include "locators.h"
     33 
     34 #include <sys/cdefs.h>
     35 
     36 __KERNEL_RCSID(1, "$NetBSD: bcm53xx_eth.c,v 1.1 2012/09/01 00:04:44 matt Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/bus.h>
     40 #include <sys/device.h>
     41 #include <sys/intr.h>
     42 #include <sys/mutex.h>
     43 #include <sys/systm.h>
     44 
     45 #include <net/if.h>
     46 #include <net/if_ether.h>
     47 #include <net/if_media.h>
     48 
     49 #include <dev/mii/miivar.h>
     50 
     51 #include <arm/broadcom/bcm53xx_reg.h>
     52 #include <arm/broadcom/bcm53xx_var.h>
     53 
     54 static int bcmeth_ccb_match(device_t, cfdata_t, void *);
     55 static void bcmeth_ccb_attach(device_t, device_t, void *);
     56 
     57 struct bcmeth_softc {
     58 	device_t sc_dev;
     59 	bus_space_tag_t sc_bst;
     60 	bus_space_handle_t sc_bsh;
     61 	bus_dma_tag_t sc_dmat;
     62 	kmutex_t *sc_lock;
     63 	kmutex_t *sc_hwlock;
     64 	struct ethercom sc_ec;
     65 #define	sc_if		sc_ec.ec_if;
     66 	void *sc_sih;
     67 	void *sc_ih;
     68 };
     69 
     70 static int bcmeth_intr(void *);
     71 static void bcmeth_softint(void *);
     72 
     73 static inline uint32_t
     74 bcmeth_read_4(struct bcmeth_softc *sc, bus_size_t o)
     75 {
     76 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
     77 }
     78 
     79 static inline void
     80 bcmeth_write_4(struct bcmeth_softc *sc, bus_size_t o, uint32_t v)
     81 {
     82 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
     83 }
     84 
     85 CFATTACH_DECL_NEW(bcmeth_ccb, sizeof(struct bcmeth_softc),
     86 	bcmeth_ccb_match, bcmeth_ccb_attach, NULL, NULL);
     87 
     88 static int
     89 bcmeth_ccb_match(device_t parent, cfdata_t cf, void *aux)
     90 {
     91 	struct bcmccb_attach_args * const ccbaa = aux;
     92 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
     93 
     94 	if (strcmp(cf->cf_name, loc->loc_name))
     95 		return 0;
     96 
     97 #ifdef DIAGNOSTIC
     98 	const int port = cf->cf_loc[BCMCCBCF_PORT];
     99 #endif
    100 	KASSERT(port == BCMCCBCF_PORT_DEFAULT || port == loc->loc_port);
    101 
    102 	return 1;
    103 }
    104 
    105 static void
    106 bcmeth_ccb_attach(device_t parent, device_t self, void *aux)
    107 {
    108 	struct bcmeth_softc * const sc = device_private(self);
    109 	struct bcmccb_attach_args * const ccbaa = aux;
    110 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
    111 
    112 	sc->sc_dev = self;
    113 	sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
    114 	sc->sc_hwlock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_VM);
    115 
    116 	sc->sc_bst = ccbaa->ccbaa_ccb_bst;
    117 	sc->sc_dmat = ccbaa->ccbaa_dmat;
    118 	bus_space_subregion(sc->sc_bst, ccbaa->ccbaa_ccb_bsh,
    119 	    loc->loc_offset, loc->loc_size, &sc->sc_bsh);
    120 
    121 	bcmeth_write_4(sc, GMAC_INTMASK, 0);	// disable interrupts
    122 
    123 	aprint_naive("\n");
    124 	aprint_normal(": Gigabit Ethernet Controller\n");
    125 
    126 	sc->sc_sih = softint_establish(SOFTINT_MPSAFE | SOFTINT_NET,
    127 	    bcmeth_softint, sc);
    128 
    129 	sc->sc_ih = intr_establish(loc->loc_intrs[0], IPL_VM, IST_LEVEL,
    130 	    bcmeth_intr, sc);
    131 
    132 	if (sc->sc_ih == NULL) {
    133 		aprint_error_dev(self, "failed to establish interrupt %d\n",
    134 		     loc->loc_intrs[0]);
    135 	} else {
    136 		aprint_normal_dev(self, "interrupting on irq %d\n",
    137 		     loc->loc_intrs[0]);
    138 	}
    139 }
    140 
    141 static void
    142 bcmeth_softint(void *arg)
    143 {
    144 	struct bcmeth_softc * const sc = arg;
    145 
    146 	mutex_enter(sc->sc_lock);
    147 	mutex_exit(sc->sc_lock);
    148 }
    149 
    150 static int
    151 bcmeth_intr(void *arg)
    152 {
    153 	struct bcmeth_softc * const sc = arg;
    154 	int rv = 0;
    155 
    156 	mutex_enter(sc->sc_hwlock);
    157 
    158 	uint32_t intstatus = bcmeth_read_4(sc, GMAC_INTSTATUS);
    159 	bcmeth_write_4(sc, GMAC_INTSTATUS, intstatus);
    160 
    161 	mutex_exit(sc->sc_hwlock);
    162 
    163 	return rv;
    164 }
    165