Home | History | Annotate | Line # | Download | only in broadcom
bcm53xx_usb.c revision 1.4
      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.4  matt __KERNEL_RCSID(1, "$NetBSD: bcm53xx_usb.c,v 1.4 2013/10/28 22:51:16 matt Exp $");
     36  1.1  matt 
     37  1.1  matt #include <sys/bus.h>
     38  1.1  matt #include <sys/device.h>
     39  1.1  matt #include <sys/intr.h>
     40  1.1  matt #include <sys/systm.h>
     41  1.1  matt 
     42  1.1  matt #include <arm/broadcom/bcm53xx_reg.h>
     43  1.1  matt #include <arm/broadcom/bcm53xx_var.h>
     44  1.1  matt 
     45  1.1  matt #include <dev/usb/usb.h>
     46  1.1  matt #include <dev/usb/usbdi.h>
     47  1.1  matt #include <dev/usb/usbdivar.h>
     48  1.1  matt #include <dev/usb/usb_mem.h>
     49  1.1  matt 
     50  1.1  matt #include <dev/usb/ohcireg.h>
     51  1.1  matt #include <dev/usb/ohcivar.h>
     52  1.1  matt 
     53  1.1  matt #include <dev/usb/ehcireg.h>
     54  1.1  matt #include <dev/usb/ehcivar.h>
     55  1.1  matt 
     56  1.2  matt #include <dev/pci/pcidevs.h>
     57  1.2  matt 
     58  1.1  matt struct bcmusb_softc {
     59  1.1  matt 	device_t usbsc_dev;
     60  1.1  matt 	bus_dma_tag_t usbsc_dmat;
     61  1.1  matt 	bus_space_tag_t usbsc_bst;
     62  1.1  matt 	bus_space_handle_t usbsc_ehci_bsh;
     63  1.1  matt 	bus_space_handle_t usbsc_ohci_bsh;
     64  1.1  matt 
     65  1.1  matt 	device_t usbsc_ohci_dev;
     66  1.1  matt 	device_t usbsc_ehci_dev;
     67  1.1  matt 	void *usbsc_ohci_sc;
     68  1.1  matt 	void *usbsc_ehci_sc;
     69  1.1  matt 	void *usbsc_ih;
     70  1.1  matt };
     71  1.1  matt 
     72  1.1  matt struct bcmusb_attach_args {
     73  1.1  matt 	const char *usbaa_name;
     74  1.1  matt 	bus_dma_tag_t usbaa_dmat;
     75  1.1  matt 	bus_space_tag_t usbaa_bst;
     76  1.1  matt 	bus_space_handle_t usbaa_bsh;
     77  1.1  matt 	bus_size_t usbaa_size;
     78  1.1  matt };
     79  1.1  matt 
     80  1.1  matt #ifdef OHCI_DEBUG
     81  1.1  matt #define OHCI_DPRINTF(x)	if (ohcidebug) printf x
     82  1.1  matt extern int ohcidebug;
     83  1.1  matt #else
     84  1.1  matt #define OHCI_DPRINTF(x)
     85  1.1  matt #endif
     86  1.1  matt 
     87  1.1  matt static int ohci_bcmusb_match(device_t, cfdata_t, void *);
     88  1.1  matt static void ohci_bcmusb_attach(device_t, device_t, void *);
     89  1.1  matt 
     90  1.1  matt CFATTACH_DECL_NEW(ohci_bcmusb, sizeof(struct ohci_softc),
     91  1.1  matt 	ohci_bcmusb_match, ohci_bcmusb_attach, NULL, NULL);
     92  1.1  matt 
     93  1.1  matt static int
     94  1.1  matt ohci_bcmusb_match(device_t parent, cfdata_t cf, void *aux)
     95  1.1  matt {
     96  1.1  matt 	struct bcmusb_attach_args * const usbaa = aux;
     97  1.1  matt 
     98  1.1  matt 	if (strcmp(cf->cf_name, usbaa->usbaa_name))
     99  1.1  matt 		return 0;
    100  1.1  matt 
    101  1.1  matt 	return 1;
    102  1.1  matt }
    103  1.1  matt 
    104  1.1  matt static void
    105  1.1  matt ohci_bcmusb_attach(device_t parent, device_t self, void *aux)
    106  1.1  matt {
    107  1.1  matt 	struct ohci_softc * const sc = device_private(self);
    108  1.1  matt 	struct bcmusb_attach_args * const usbaa = aux;
    109  1.1  matt 
    110  1.1  matt 	sc->sc_dev = self;
    111  1.1  matt 
    112  1.1  matt 	sc->iot = usbaa->usbaa_bst;
    113  1.1  matt 	sc->ioh = usbaa->usbaa_bsh;
    114  1.1  matt 	sc->sc_size = usbaa->usbaa_size;
    115  1.1  matt 	sc->sc_bus.dmatag = usbaa->usbaa_dmat;
    116  1.1  matt 	sc->sc_bus.hci_private = sc;
    117  1.1  matt 
    118  1.2  matt 	sc->sc_id_vendor = PCI_VENDOR_BROADCOM;
    119  1.2  matt 	strlcpy(sc->sc_vendor, "Broadcom", sizeof(sc->sc_vendor));
    120  1.2  matt 
    121  1.1  matt 	aprint_naive(": OHCI USB controller\n");
    122  1.1  matt 	aprint_normal(": OHCI USB controller\n");
    123  1.1  matt 
    124  1.1  matt 	int error = ohci_init(sc);
    125  1.1  matt 	if (error != USBD_NORMAL_COMPLETION) {
    126  1.1  matt 		aprint_error_dev(self, "init failed, error=%d\n", error);
    127  1.1  matt 	} else {
    128  1.1  matt 		/* Attach usb device. */
    129  1.1  matt 		sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
    130  1.1  matt 	}
    131  1.1  matt }
    132  1.1  matt 
    133  1.1  matt #ifdef EHCI_DEBUG
    134  1.1  matt #define EHCI_DPRINTF(x)	if (ehcidebug) printf x
    135  1.1  matt extern int ehcidebug;
    136  1.1  matt #else
    137  1.1  matt #define EHCI_DPRINTF(x)
    138  1.1  matt #endif
    139  1.1  matt 
    140  1.1  matt static int ehci_bcmusb_match(device_t, cfdata_t, void *);
    141  1.1  matt static void ehci_bcmusb_attach(device_t, device_t, void *);
    142  1.1  matt 
    143  1.1  matt CFATTACH_DECL_NEW(ehci_bcmusb, sizeof(struct ehci_softc),
    144  1.1  matt 	ehci_bcmusb_match, ehci_bcmusb_attach, NULL, NULL);
    145  1.1  matt 
    146  1.1  matt static int
    147  1.1  matt ehci_bcmusb_match(device_t parent, cfdata_t cf, void *aux)
    148  1.1  matt {
    149  1.1  matt 	struct bcmusb_attach_args * const usbaa = aux;
    150  1.1  matt 
    151  1.1  matt 	if (strcmp(cf->cf_name, usbaa->usbaa_name))
    152  1.1  matt 		return 0;
    153  1.1  matt 
    154  1.1  matt 	return 1;
    155  1.1  matt }
    156  1.1  matt 
    157  1.1  matt static void
    158  1.1  matt ehci_bcmusb_attach(device_t parent, device_t self, void *aux)
    159  1.1  matt {
    160  1.1  matt 	struct bcmusb_softc * const usbsc = device_private(parent);
    161  1.1  matt 	struct ehci_softc * const sc = device_private(self);
    162  1.1  matt 	struct bcmusb_attach_args * const usbaa = aux;
    163  1.1  matt 
    164  1.1  matt 	sc->sc_dev = self;
    165  1.1  matt 
    166  1.1  matt 	sc->iot = usbaa->usbaa_bst;
    167  1.1  matt 	sc->ioh = usbaa->usbaa_bsh;
    168  1.1  matt 	sc->sc_size = usbaa->usbaa_size;
    169  1.1  matt 	sc->sc_bus.dmatag = usbaa->usbaa_dmat;
    170  1.1  matt 	sc->sc_bus.hci_private = sc;
    171  1.1  matt 	sc->sc_bus.usbrev = USBREV_2_0;
    172  1.1  matt 	sc->sc_ncomp = 0;
    173  1.1  matt 	if (usbsc->usbsc_ohci_dev != NULL) {
    174  1.1  matt 		sc->sc_comps[sc->sc_ncomp++] = usbsc->usbsc_ohci_dev;
    175  1.1  matt 	}
    176  1.1  matt 
    177  1.2  matt 	sc->sc_id_vendor = PCI_VENDOR_BROADCOM;
    178  1.2  matt 	strlcpy(sc->sc_vendor, "Broadcom", sizeof(sc->sc_vendor));
    179  1.2  matt 
    180  1.1  matt 	aprint_naive(": EHCI USB controller\n");
    181  1.1  matt 	aprint_normal(": ECHI USB controller\n");
    182  1.1  matt 
    183  1.1  matt 	int error = ehci_init(sc);
    184  1.1  matt 	if (error != USBD_NORMAL_COMPLETION) {
    185  1.1  matt 		aprint_error_dev(self, "init failed, error=%d\n", error);
    186  1.1  matt 	} else {
    187  1.1  matt 		/* Attach usb device. */
    188  1.1  matt 		sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
    189  1.1  matt 	}
    190  1.1  matt }
    191  1.1  matt 
    192  1.1  matt /*
    193  1.1  matt  * There's only IRQ shared between both OCHI and EHCI devices.
    194  1.1  matt  */
    195  1.1  matt static int
    196  1.1  matt bcmusb_intr(void *arg)
    197  1.1  matt {
    198  1.1  matt 	struct bcmusb_softc * const usbsc = arg;
    199  1.1  matt 	int rv0 = 0, rv1 = 0;
    200  1.1  matt 
    201  1.1  matt 	if (usbsc->usbsc_ohci_sc)
    202  1.1  matt 		rv0 = ohci_intr(usbsc->usbsc_ohci_sc);
    203  1.1  matt 
    204  1.1  matt 	if (usbsc->usbsc_ehci_sc)
    205  1.1  matt 		rv1 = ehci_intr(usbsc->usbsc_ehci_sc);
    206  1.1  matt 
    207  1.1  matt 	return rv0 ? rv0 : rv1;
    208  1.1  matt }
    209  1.1  matt 
    210  1.1  matt static int bcmusb_ccb_match(device_t, cfdata_t, void *);
    211  1.1  matt static void bcmusb_ccb_attach(device_t, device_t, void *);
    212  1.1  matt 
    213  1.1  matt CFATTACH_DECL_NEW(bcmusb_ccb, sizeof(struct bcmusb_softc),
    214  1.1  matt 	bcmusb_ccb_match, bcmusb_ccb_attach, NULL, NULL);
    215  1.1  matt 
    216  1.1  matt int
    217  1.1  matt bcmusb_ccb_match(device_t parent, cfdata_t cf, void *aux)
    218  1.1  matt {
    219  1.1  matt 	struct bcmccb_attach_args * const ccbaa = aux;
    220  1.1  matt 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
    221  1.1  matt 
    222  1.1  matt 	if (strcmp(cf->cf_name, loc->loc_name) != 0)
    223  1.1  matt 		return 0;
    224  1.1  matt 
    225  1.1  matt 	KASSERT(cf->cf_loc[BCMCCBCF_PORT] == BCMCCBCF_PORT_DEFAULT);
    226  1.1  matt 
    227  1.1  matt 	return 1;
    228  1.1  matt }
    229  1.1  matt 
    230  1.4  matt #define	OHCI_OFFSET	(OHCI_BASE - EHCI_BASE)
    231  1.4  matt 
    232  1.1  matt void
    233  1.1  matt bcmusb_ccb_attach(device_t parent, device_t self, void *aux)
    234  1.1  matt {
    235  1.1  matt 	struct bcmusb_softc * const usbsc = device_private(self);
    236  1.1  matt 	const struct bcmccb_attach_args * const ccbaa = aux;
    237  1.1  matt 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
    238  1.1  matt 
    239  1.1  matt 	usbsc->usbsc_bst = ccbaa->ccbaa_ccb_bst;
    240  1.1  matt 	usbsc->usbsc_dmat = ccbaa->ccbaa_dmat;
    241  1.1  matt 
    242  1.4  matt 	bus_space_subregion(usbsc->usbsc_bst, ccbaa->ccbaa_ccb_bsh,
    243  1.4  matt 	    loc->loc_offset, 0x1000, &usbsc->usbsc_ehci_bsh);
    244  1.4  matt 	bus_space_subregion(usbsc->usbsc_bst, ccbaa->ccbaa_ccb_bsh,
    245  1.4  matt 	    loc->loc_offset + OHCI_OFFSET, 0x1000, &usbsc->usbsc_ohci_bsh);
    246  1.1  matt 
    247  1.1  matt 	/*
    248  1.3  matt 	 * Bring the PHYs out of reset.
    249  1.3  matt 	 */
    250  1.3  matt 	bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ehci_bsh,
    251  1.3  matt 	    USBH_PHY_CTRL_P0, USBH_PHY_CTRL_INIT);
    252  1.3  matt 	bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ehci_bsh,
    253  1.3  matt 	    USBH_PHY_CTRL_P1, USBH_PHY_CTRL_INIT);
    254  1.3  matt 
    255  1.3  matt 	/*
    256  1.1  matt 	 * Disable interrupts
    257  1.1  matt 	 */
    258  1.1  matt 	bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ohci_bsh,
    259  1.1  matt 	    OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
    260  1.1  matt 	bus_size_t caplength = bus_space_read_1(usbsc->usbsc_bst,
    261  1.1  matt 	    usbsc->usbsc_ehci_bsh, EHCI_CAPLENGTH);
    262  1.1  matt 	bus_space_write_4(usbsc->usbsc_bst, usbsc->usbsc_ehci_bsh,
    263  1.1  matt 	    caplength + EHCI_USBINTR, 0);
    264  1.1  matt 
    265  1.1  matt 	aprint_naive("\n");
    266  1.1  matt 	aprint_normal("\n");
    267  1.1  matt 
    268  1.1  matt 	struct bcmusb_attach_args usbaa_ohci = {
    269  1.1  matt 		.usbaa_name = "ohci",
    270  1.1  matt 		.usbaa_dmat = usbsc->usbsc_dmat,
    271  1.1  matt 		.usbaa_bst = usbsc->usbsc_bst,
    272  1.1  matt 		.usbaa_bsh = usbsc->usbsc_ohci_bsh,
    273  1.1  matt 		.usbaa_size = 0x100,
    274  1.1  matt 	};
    275  1.1  matt 
    276  1.1  matt 	usbsc->usbsc_ohci_dev = config_found(self, &usbaa_ohci, NULL);
    277  1.1  matt 	if (usbsc->usbsc_ohci_dev != NULL)
    278  1.1  matt 		usbsc->usbsc_ohci_sc = device_private(usbsc->usbsc_ohci_dev);
    279  1.1  matt 
    280  1.1  matt 	struct bcmusb_attach_args usbaa_ehci = {
    281  1.1  matt 		.usbaa_name = "ehci",
    282  1.1  matt 		.usbaa_dmat = usbsc->usbsc_dmat,
    283  1.1  matt 		.usbaa_bst = usbsc->usbsc_bst,
    284  1.1  matt 		.usbaa_bsh = usbsc->usbsc_ehci_bsh,
    285  1.1  matt 		.usbaa_size = 0x100,
    286  1.1  matt 	};
    287  1.1  matt 
    288  1.1  matt 	usbsc->usbsc_ehci_dev = config_found(self, &usbaa_ehci, NULL);
    289  1.1  matt 	if (usbsc->usbsc_ehci_dev != NULL)
    290  1.1  matt 		usbsc->usbsc_ehci_sc = device_private(usbsc->usbsc_ehci_dev);
    291  1.1  matt 
    292  1.1  matt 	usbsc->usbsc_ih = intr_establish(loc->loc_intrs[0], IPL_USB, IST_LEVEL,
    293  1.1  matt 	    bcmusb_intr, usbsc);
    294  1.1  matt 	if (usbsc->usbsc_ih == NULL) {
    295  1.1  matt 		aprint_error_dev(self, "failed to establish interrupt %d\n",
    296  1.1  matt 		     loc->loc_intrs[0]);
    297  1.1  matt 		return;
    298  1.1  matt 	}
    299  1.1  matt 	aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intrs[0]);
    300  1.1  matt }
    301