Home | History | Annotate | Line # | Download | only in broadcom
bcm53xx_usb.c revision 1.6
      1  1.1   matt /*-
      2  1.1   matt  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      3  1.1   matt  * All rights reserved.
      4  1.1   matt  *
      5  1.1   matt  * This code is derived from software contributed to The NetBSD Foundation
      6  1.1   matt  * by Matt Thomas of 3am Software Foundry.
      7  1.1   matt  *
      8  1.1   matt  * Redistribution and use in source and binary forms, with or without
      9  1.1   matt  * modification, are permitted provided that the following conditions
     10  1.1   matt  * are met:
     11  1.1   matt  * 1. Redistributions of source code must retain the above copyright
     12  1.1   matt  *    notice, this list of conditions and the following disclaimer.
     13  1.1   matt  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1   matt  *    notice, this list of conditions and the following disclaimer in the
     15  1.1   matt  *    documentation and/or other materials provided with the distribution.
     16  1.1   matt  *
     17  1.1   matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  1.1   matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.1   matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.1   matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  1.1   matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  1.1   matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  1.1   matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1   matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  1.1   matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  1.1   matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  1.1   matt  * POSSIBILITY OF SUCH DAMAGE.
     28  1.1   matt  */
     29  1.3   matt #define USBH_PRIVATE
     30  1.1   matt 
     31  1.1   matt #include "locators.h"
     32  1.1   matt 
     33  1.1   matt #include <sys/cdefs.h>
     34  1.1   matt 
     35  1.6  skrll __KERNEL_RCSID(1, "$NetBSD: bcm53xx_usb.c,v 1.6 2014/06/24 05:07:31 skrll Exp $");
     36  1.1   matt 
     37  1.5   matt #include <sys/param.h>
     38  1.1   matt #include <sys/bus.h>
     39  1.1   matt #include <sys/device.h>
     40  1.1   matt #include <sys/intr.h>
     41  1.1   matt #include <sys/systm.h>
     42  1.1   matt 
     43  1.1   matt #include <arm/broadcom/bcm53xx_reg.h>
     44  1.1   matt #include <arm/broadcom/bcm53xx_var.h>
     45  1.1   matt 
     46  1.1   matt #include <dev/usb/usb.h>
     47  1.1   matt #include <dev/usb/usbdi.h>
     48  1.1   matt #include <dev/usb/usbdivar.h>
     49  1.1   matt #include <dev/usb/usb_mem.h>
     50  1.1   matt 
     51  1.1   matt #include <dev/usb/ohcireg.h>
     52  1.1   matt #include <dev/usb/ohcivar.h>
     53  1.1   matt 
     54  1.1   matt #include <dev/usb/ehcireg.h>
     55  1.1   matt #include <dev/usb/ehcivar.h>
     56  1.1   matt 
     57  1.2   matt #include <dev/pci/pcidevs.h>
     58  1.2   matt 
     59  1.1   matt struct bcmusb_softc {
     60  1.1   matt 	device_t usbsc_dev;
     61  1.1   matt 	bus_dma_tag_t usbsc_dmat;
     62  1.1   matt 	bus_space_tag_t usbsc_bst;
     63  1.1   matt 	bus_space_handle_t usbsc_ehci_bsh;
     64  1.1   matt 	bus_space_handle_t usbsc_ohci_bsh;
     65  1.1   matt 
     66  1.1   matt 	device_t usbsc_ohci_dev;
     67  1.1   matt 	device_t usbsc_ehci_dev;
     68  1.1   matt 	void *usbsc_ohci_sc;
     69  1.1   matt 	void *usbsc_ehci_sc;
     70  1.1   matt 	void *usbsc_ih;
     71  1.1   matt };
     72  1.1   matt 
     73  1.1   matt struct bcmusb_attach_args {
     74  1.1   matt 	const char *usbaa_name;
     75  1.1   matt 	bus_dma_tag_t usbaa_dmat;
     76  1.1   matt 	bus_space_tag_t usbaa_bst;
     77  1.1   matt 	bus_space_handle_t usbaa_bsh;
     78  1.1   matt 	bus_size_t usbaa_size;
     79  1.1   matt };
     80  1.1   matt 
     81  1.1   matt #ifdef OHCI_DEBUG
     82  1.1   matt #define OHCI_DPRINTF(x)	if (ohcidebug) printf x
     83  1.1   matt extern int ohcidebug;
     84  1.1   matt #else
     85  1.1   matt #define OHCI_DPRINTF(x)
     86  1.1   matt #endif
     87  1.1   matt 
     88  1.1   matt static int ohci_bcmusb_match(device_t, cfdata_t, void *);
     89  1.1   matt static void ohci_bcmusb_attach(device_t, device_t, void *);
     90  1.1   matt 
     91  1.1   matt CFATTACH_DECL_NEW(ohci_bcmusb, sizeof(struct ohci_softc),
     92  1.1   matt 	ohci_bcmusb_match, ohci_bcmusb_attach, NULL, NULL);
     93  1.1   matt 
     94  1.1   matt static int
     95  1.1   matt ohci_bcmusb_match(device_t parent, cfdata_t cf, void *aux)
     96  1.1   matt {
     97  1.1   matt 	struct bcmusb_attach_args * const usbaa = aux;
     98  1.1   matt 
     99  1.1   matt 	if (strcmp(cf->cf_name, usbaa->usbaa_name))
    100  1.1   matt 		return 0;
    101  1.1   matt 
    102  1.1   matt 	return 1;
    103  1.1   matt }
    104  1.1   matt 
    105  1.1   matt static void
    106  1.1   matt ohci_bcmusb_attach(device_t parent, device_t self, void *aux)
    107  1.1   matt {
    108  1.1   matt 	struct ohci_softc * const sc = device_private(self);
    109  1.1   matt 	struct bcmusb_attach_args * const usbaa = aux;
    110  1.1   matt 
    111  1.1   matt 	sc->sc_dev = self;
    112  1.1   matt 
    113  1.1   matt 	sc->iot = usbaa->usbaa_bst;
    114  1.1   matt 	sc->ioh = usbaa->usbaa_bsh;
    115  1.1   matt 	sc->sc_size = usbaa->usbaa_size;
    116  1.1   matt 	sc->sc_bus.dmatag = usbaa->usbaa_dmat;
    117  1.1   matt 	sc->sc_bus.hci_private = sc;
    118  1.1   matt 
    119  1.2   matt 	sc->sc_id_vendor = PCI_VENDOR_BROADCOM;
    120  1.2   matt 	strlcpy(sc->sc_vendor, "Broadcom", sizeof(sc->sc_vendor));
    121  1.2   matt 
    122  1.1   matt 	aprint_naive(": OHCI USB controller\n");
    123  1.1   matt 	aprint_normal(": OHCI USB controller\n");
    124  1.1   matt 
    125  1.1   matt 	int error = ohci_init(sc);
    126  1.1   matt 	if (error != USBD_NORMAL_COMPLETION) {
    127  1.1   matt 		aprint_error_dev(self, "init failed, error=%d\n", error);
    128  1.6  skrll 		return;
    129  1.1   matt 	}
    130  1.6  skrll 
    131  1.6  skrll 	/* Attach usb device. */
    132  1.6  skrll 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
    133  1.1   matt }
    134  1.1   matt 
    135  1.1   matt #ifdef EHCI_DEBUG
    136  1.1   matt #define EHCI_DPRINTF(x)	if (ehcidebug) printf x
    137  1.1   matt extern int ehcidebug;
    138  1.1   matt #else
    139  1.1   matt #define EHCI_DPRINTF(x)
    140  1.1   matt #endif
    141  1.1   matt 
    142  1.1   matt static int ehci_bcmusb_match(device_t, cfdata_t, void *);
    143  1.1   matt static void ehci_bcmusb_attach(device_t, device_t, void *);
    144  1.1   matt 
    145  1.1   matt CFATTACH_DECL_NEW(ehci_bcmusb, sizeof(struct ehci_softc),
    146  1.1   matt 	ehci_bcmusb_match, ehci_bcmusb_attach, NULL, NULL);
    147  1.1   matt 
    148  1.1   matt static int
    149  1.1   matt ehci_bcmusb_match(device_t parent, cfdata_t cf, void *aux)
    150  1.1   matt {
    151  1.1   matt 	struct bcmusb_attach_args * const usbaa = aux;
    152  1.1   matt 
    153  1.1   matt 	if (strcmp(cf->cf_name, usbaa->usbaa_name))
    154  1.1   matt 		return 0;
    155  1.1   matt 
    156  1.1   matt 	return 1;
    157  1.1   matt }
    158  1.1   matt 
    159  1.1   matt static void
    160  1.1   matt ehci_bcmusb_attach(device_t parent, device_t self, void *aux)
    161  1.1   matt {
    162  1.1   matt 	struct bcmusb_softc * const usbsc = device_private(parent);
    163  1.1   matt 	struct ehci_softc * const sc = device_private(self);
    164  1.1   matt 	struct bcmusb_attach_args * const usbaa = aux;
    165  1.1   matt 
    166  1.1   matt 	sc->sc_dev = self;
    167  1.1   matt 
    168  1.1   matt 	sc->iot = usbaa->usbaa_bst;
    169  1.1   matt 	sc->ioh = usbaa->usbaa_bsh;
    170  1.1   matt 	sc->sc_size = usbaa->usbaa_size;
    171  1.1   matt 	sc->sc_bus.dmatag = usbaa->usbaa_dmat;
    172  1.1   matt 	sc->sc_bus.hci_private = sc;
    173  1.1   matt 	sc->sc_bus.usbrev = USBREV_2_0;
    174  1.1   matt 	sc->sc_ncomp = 0;
    175  1.1   matt 	if (usbsc->usbsc_ohci_dev != NULL) {
    176  1.1   matt 		sc->sc_comps[sc->sc_ncomp++] = usbsc->usbsc_ohci_dev;
    177  1.1   matt 	}
    178  1.1   matt 
    179  1.2   matt 	sc->sc_id_vendor = PCI_VENDOR_BROADCOM;
    180  1.2   matt 	strlcpy(sc->sc_vendor, "Broadcom", sizeof(sc->sc_vendor));
    181  1.2   matt 
    182  1.1   matt 	aprint_naive(": EHCI USB controller\n");
    183  1.1   matt 	aprint_normal(": ECHI USB controller\n");
    184  1.1   matt 
    185  1.1   matt 	int error = ehci_init(sc);
    186  1.1   matt 	if (error != USBD_NORMAL_COMPLETION) {
    187  1.1   matt 		aprint_error_dev(self, "init failed, error=%d\n", error);
    188  1.6  skrll 		return;
    189  1.1   matt 	}
    190  1.6  skrll 	/* Attach usb device. */
    191  1.6  skrll 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
    192  1.1   matt }
    193  1.1   matt 
    194  1.1   matt /*
    195  1.1   matt  * There's only IRQ shared between both OCHI and EHCI devices.
    196  1.1   matt  */
    197  1.1   matt static int
    198  1.1   matt bcmusb_intr(void *arg)
    199  1.1   matt {
    200  1.1   matt 	struct bcmusb_softc * const usbsc = arg;
    201  1.1   matt 	int rv0 = 0, rv1 = 0;
    202  1.1   matt 
    203  1.1   matt 	if (usbsc->usbsc_ohci_sc)
    204  1.1   matt 		rv0 = ohci_intr(usbsc->usbsc_ohci_sc);
    205  1.1   matt 
    206  1.1   matt 	if (usbsc->usbsc_ehci_sc)
    207  1.1   matt 		rv1 = ehci_intr(usbsc->usbsc_ehci_sc);
    208  1.1   matt 
    209  1.1   matt 	return rv0 ? rv0 : rv1;
    210  1.1   matt }
    211  1.1   matt 
    212  1.1   matt static int bcmusb_ccb_match(device_t, cfdata_t, void *);
    213  1.1   matt static void bcmusb_ccb_attach(device_t, device_t, void *);
    214  1.1   matt 
    215  1.1   matt CFATTACH_DECL_NEW(bcmusb_ccb, sizeof(struct bcmusb_softc),
    216  1.1   matt 	bcmusb_ccb_match, bcmusb_ccb_attach, NULL, NULL);
    217  1.1   matt 
    218  1.1   matt int
    219  1.1   matt bcmusb_ccb_match(device_t parent, cfdata_t cf, void *aux)
    220  1.1   matt {
    221  1.1   matt 	struct bcmccb_attach_args * const ccbaa = aux;
    222  1.1   matt 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
    223  1.1   matt 
    224  1.1   matt 	if (strcmp(cf->cf_name, loc->loc_name) != 0)
    225  1.1   matt 		return 0;
    226  1.1   matt 
    227  1.1   matt 	KASSERT(cf->cf_loc[BCMCCBCF_PORT] == BCMCCBCF_PORT_DEFAULT);
    228  1.1   matt 
    229  1.1   matt 	return 1;
    230  1.1   matt }
    231  1.1   matt 
    232  1.4   matt #define	OHCI_OFFSET	(OHCI_BASE - EHCI_BASE)
    233  1.4   matt 
    234  1.1   matt void
    235  1.1   matt bcmusb_ccb_attach(device_t parent, device_t self, void *aux)
    236  1.1   matt {
    237  1.1   matt 	struct bcmusb_softc * const usbsc = device_private(self);
    238  1.1   matt 	const struct bcmccb_attach_args * const ccbaa = aux;
    239  1.1   matt 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
    240  1.1   matt 
    241  1.1   matt 	usbsc->usbsc_bst = ccbaa->ccbaa_ccb_bst;
    242  1.1   matt 	usbsc->usbsc_dmat = ccbaa->ccbaa_dmat;
    243  1.1   matt 
    244  1.4   matt 	bus_space_subregion(usbsc->usbsc_bst, ccbaa->ccbaa_ccb_bsh,
    245  1.4   matt 	    loc->loc_offset, 0x1000, &usbsc->usbsc_ehci_bsh);
    246  1.4   matt 	bus_space_subregion(usbsc->usbsc_bst, ccbaa->ccbaa_ccb_bsh,
    247  1.4   matt 	    loc->loc_offset + OHCI_OFFSET, 0x1000, &usbsc->usbsc_ohci_bsh);
    248  1.1   matt 
    249  1.1   matt 	/*
    250  1.3   matt 	 * Bring the PHYs out of reset.
    251  1.3   matt 	 */
    252  1.3   matt 	bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ehci_bsh,
    253  1.3   matt 	    USBH_PHY_CTRL_P0, USBH_PHY_CTRL_INIT);
    254  1.3   matt 	bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ehci_bsh,
    255  1.3   matt 	    USBH_PHY_CTRL_P1, USBH_PHY_CTRL_INIT);
    256  1.3   matt 
    257  1.3   matt 	/*
    258  1.1   matt 	 * Disable interrupts
    259  1.1   matt 	 */
    260  1.1   matt 	bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ohci_bsh,
    261  1.1   matt 	    OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
    262  1.1   matt 	bus_size_t caplength = bus_space_read_1(usbsc->usbsc_bst,
    263  1.1   matt 	    usbsc->usbsc_ehci_bsh, EHCI_CAPLENGTH);
    264  1.1   matt 	bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ehci_bsh,
    265  1.1   matt 	    caplength + EHCI_USBINTR, 0);
    266  1.1   matt 
    267  1.1   matt 	aprint_naive("\n");
    268  1.1   matt 	aprint_normal("\n");
    269  1.1   matt 
    270  1.1   matt 	struct bcmusb_attach_args usbaa_ohci = {
    271  1.1   matt 		.usbaa_name = "ohci",
    272  1.1   matt 		.usbaa_dmat = usbsc->usbsc_dmat,
    273  1.1   matt 		.usbaa_bst = usbsc->usbsc_bst,
    274  1.1   matt 		.usbaa_bsh = usbsc->usbsc_ohci_bsh,
    275  1.1   matt 		.usbaa_size = 0x100,
    276  1.1   matt 	};
    277  1.1   matt 
    278  1.1   matt 	usbsc->usbsc_ohci_dev = config_found(self, &usbaa_ohci, NULL);
    279  1.1   matt 	if (usbsc->usbsc_ohci_dev != NULL)
    280  1.1   matt 		usbsc->usbsc_ohci_sc = device_private(usbsc->usbsc_ohci_dev);
    281  1.1   matt 
    282  1.1   matt 	struct bcmusb_attach_args usbaa_ehci = {
    283  1.1   matt 		.usbaa_name = "ehci",
    284  1.1   matt 		.usbaa_dmat = usbsc->usbsc_dmat,
    285  1.1   matt 		.usbaa_bst = usbsc->usbsc_bst,
    286  1.1   matt 		.usbaa_bsh = usbsc->usbsc_ehci_bsh,
    287  1.1   matt 		.usbaa_size = 0x100,
    288  1.1   matt 	};
    289  1.1   matt 
    290  1.1   matt 	usbsc->usbsc_ehci_dev = config_found(self, &usbaa_ehci, NULL);
    291  1.1   matt 	if (usbsc->usbsc_ehci_dev != NULL)
    292  1.1   matt 		usbsc->usbsc_ehci_sc = device_private(usbsc->usbsc_ehci_dev);
    293  1.1   matt 
    294  1.1   matt 	usbsc->usbsc_ih = intr_establish(loc->loc_intrs[0], IPL_USB, IST_LEVEL,
    295  1.1   matt 	    bcmusb_intr, usbsc);
    296  1.1   matt 	if (usbsc->usbsc_ih == NULL) {
    297  1.1   matt 		aprint_error_dev(self, "failed to establish interrupt %d\n",
    298  1.1   matt 		     loc->loc_intrs[0]);
    299  1.1   matt 		return;
    300  1.1   matt 	}
    301  1.1   matt 	aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intrs[0]);
    302  1.1   matt }
    303