Home | History | Annotate | Line # | Download | only in dev
      1  1.6  dyoung /*	$NetBSD: zusb.c,v 1.6 2011/07/19 15:11:49 dyoung Exp $	*/
      2  1.1   chris 
      3  1.1   chris /*
      4  1.1   chris  * Copyright (c) 2008 Christopher Gilbert
      5  1.1   chris  * All rights reserved.
      6  1.1   chris  *
      7  1.1   chris  * 1. Redistributions of source code must retain the above copyright
      8  1.1   chris  *    notice, this list of conditions and the following disclaimer.
      9  1.1   chris  * 2. Redistributions in binary form must reproduce the above copyright
     10  1.1   chris  *    notice, this list of conditions and the following disclaimer in the
     11  1.1   chris  *    documentation and/or other materials provided with the distribution.
     12  1.1   chris  * 3. The name of the company nor the name of the author may be used to
     13  1.1   chris  *    endorse or promote products derived from this software without specific
     14  1.1   chris  *    prior written permission.
     15  1.1   chris  *
     16  1.1   chris  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  1.1   chris  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  1.1   chris  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1   chris  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     20  1.1   chris  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  1.1   chris  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  1.1   chris  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1   chris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1   chris  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1   chris  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1   chris  * SUCH DAMAGE.
     27  1.1   chris  */
     28  1.1   chris 
     29  1.1   chris #include <sys/cdefs.h>
     30  1.6  dyoung __KERNEL_RCSID(0, "$NetBSD: zusb.c,v 1.6 2011/07/19 15:11:49 dyoung Exp $");
     31  1.1   chris 
     32  1.1   chris #include <sys/param.h>
     33  1.1   chris #include <sys/device.h>
     34  1.1   chris #include <sys/kernel.h>
     35  1.6  dyoung #include <sys/bus.h>
     36  1.1   chris 
     37  1.1   chris #include <arm/xscale/pxa2x0reg.h>
     38  1.1   chris #include <arm/xscale/pxa2x0var.h>
     39  1.1   chris #include <arm/xscale/pxa2x0_gpio.h>
     40  1.1   chris 
     41  1.1   chris #include <machine/intr.h>
     42  1.1   chris 
     43  1.1   chris #include <zaurus/zaurus/zaurus_reg.h>
     44  1.1   chris #include <zaurus/zaurus/zaurus_var.h>
     45  1.1   chris 
     46  1.2  nonaka 
     47  1.2  nonaka #ifdef ZUSB_DEBUG
     48  1.2  nonaka #define	DPRINTF(s)	printf s
     49  1.2  nonaka #else
     50  1.2  nonaka #define	DPRINTF(s)
     51  1.2  nonaka #endif
     52  1.2  nonaka 
     53  1.2  nonaka 
     54  1.1   chris struct zusb_softc {
     55  1.3  nonaka 	device_t sc_dev;
     56  1.2  nonaka 
     57  1.1   chris 	bus_space_tag_t sc_iot;
     58  1.1   chris 	bus_space_handle_t sc_ioh;
     59  1.2  nonaka 
     60  1.2  nonaka 	void *sc_client_ih;
     61  1.2  nonaka 	void *sc_host_ih;
     62  1.1   chris };
     63  1.1   chris 
     64  1.4  nonaka static int	zusb_match(device_t, cfdata_t, void *);
     65  1.4  nonaka static void	zusb_attach(device_t, device_t, void *);
     66  1.1   chris 
     67  1.3  nonaka CFATTACH_DECL_NEW(zusb, sizeof(struct zusb_softc),
     68  1.1   chris     zusb_match, zusb_attach, NULL, NULL);
     69  1.1   chris 
     70  1.1   chris static int	zusb_client_intr(void *);
     71  1.1   chris static int	zusb_host_intr(void *);
     72  1.1   chris static void	zusb_test_and_enabled_host_port(struct zusb_softc *);
     73  1.1   chris 
     74  1.1   chris static int
     75  1.3  nonaka zusb_match(device_t parent, cfdata_t cf, void *aux)
     76  1.1   chris {
     77  1.1   chris 
     78  1.5  nonaka 	if (ZAURUS_ISC1000 || ZAURUS_ISC3000)
     79  1.1   chris 		return 1;
     80  1.1   chris 	return 0;
     81  1.1   chris }
     82  1.1   chris 
     83  1.1   chris static void
     84  1.3  nonaka zusb_attach(device_t parent, device_t self, void *aux)
     85  1.1   chris {
     86  1.3  nonaka 	struct zusb_softc *sc = device_private(self);
     87  1.1   chris 	struct pxaip_attach_args *pxa = aux;
     88  1.1   chris 
     89  1.2  nonaka 	aprint_normal(": USB Mode detection\n");
     90  1.4  nonaka 	aprint_naive("\n");
     91  1.2  nonaka 
     92  1.3  nonaka 	sc->sc_dev = self;
     93  1.1   chris 	sc->sc_iot = pxa->pxa_iot;
     94  1.2  nonaka 
     95  1.1   chris 	/* Map I/O space */
     96  1.1   chris 	if (bus_space_map(sc->sc_iot, PXA2X0_USBDC_BASE, PXA270_USBDC_SIZE, 0,
     97  1.1   chris 				&sc->sc_ioh)) {
     98  1.3  nonaka 		aprint_error_dev(sc->sc_dev, "couldn't map memory space\n");
     99  1.1   chris 		return;
    100  1.1   chris 	}
    101  1.1   chris 
    102  1.1   chris 	pxa2x0_gpio_set_function(C3000_USB_DEVICE_PIN, GPIO_IN);
    103  1.1   chris 	sc->sc_client_ih = pxa2x0_gpio_intr_establish(C3000_USB_DEVICE_PIN,
    104  1.1   chris 	    IST_EDGE_BOTH, IPL_BIO, zusb_client_intr, sc);
    105  1.1   chris 
    106  1.1   chris 	pxa2x0_gpio_set_function(C3000_USB_HOST_PIN, GPIO_IN);
    107  1.1   chris 	sc->sc_host_ih = pxa2x0_gpio_intr_establish(C3000_USB_HOST_PIN,
    108  1.1   chris 	    IST_EDGE_BOTH, IPL_BIO, zusb_host_intr, sc);
    109  1.1   chris 
    110  1.1   chris 	/* configure port 2 for input */
    111  1.1   chris 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, USBDC_UP2OCR,
    112  1.1   chris 			USBDC_UP2OCR_HXS | USBDC_UP2OCR_HXOE |
    113  1.1   chris 			USBDC_UP2OCR_DPPDE | USBDC_UP2OCR_DMPDE);
    114  1.1   chris 
    115  1.1   chris 	zusb_test_and_enabled_host_port(sc);
    116  1.1   chris }
    117  1.1   chris 
    118  1.1   chris static int
    119  1.1   chris zusb_client_intr(void *v)
    120  1.1   chris {
    121  1.1   chris 	struct zusb_softc *sc = v;
    122  1.2  nonaka 
    123  1.3  nonaka 	DPRINTF(("%s: USB client cable changed\n", device_xname(sc->sc_dev)));
    124  1.1   chris 	zusb_test_and_enabled_host_port(sc);
    125  1.1   chris 
    126  1.1   chris 	return 1;
    127  1.1   chris }
    128  1.1   chris 
    129  1.1   chris static int
    130  1.1   chris zusb_host_intr(void *v)
    131  1.1   chris {
    132  1.1   chris 	struct zusb_softc *sc = v;
    133  1.1   chris 
    134  1.3  nonaka 	DPRINTF(("%s: USB host cable changed\n", device_xname(sc->sc_dev)));
    135  1.1   chris 	zusb_test_and_enabled_host_port(sc);
    136  1.1   chris 
    137  1.1   chris 	return 1;
    138  1.1   chris }
    139  1.1   chris 
    140  1.1   chris static void
    141  1.1   chris zusb_test_and_enabled_host_port(struct zusb_softc *sc)
    142  1.1   chris {
    143  1.1   chris 	int host_cable = pxa2x0_gpio_get_bit(C3000_USB_HOST_PIN);
    144  1.2  nonaka #ifdef ZUSB_DEBUG
    145  1.1   chris 	int client_cable = pxa2x0_gpio_get_bit(C3000_USB_DEVICE_PIN);
    146  1.2  nonaka #endif
    147  1.2  nonaka 
    148  1.2  nonaka 	DPRINTF(("%s: USB cable: host %d, client %d\n",
    149  1.3  nonaka 	    device_xname(sc->sc_dev), host_cable, client_cable));
    150  1.1   chris 
    151  1.1   chris 	if (!host_cable) {
    152  1.2  nonaka 		pxa2x0_gpio_set_function(C3000_USB_HOST_POWER_PIN,
    153  1.2  nonaka 		    GPIO_OUT | GPIO_SET);
    154  1.2  nonaka 		DPRINTF(("%s: USB host power enabled\n",
    155  1.3  nonaka 		    device_xname(sc->sc_dev)));
    156  1.1   chris 	} else {
    157  1.2  nonaka 		pxa2x0_gpio_set_function(C3000_USB_HOST_POWER_PIN,
    158  1.2  nonaka 		    GPIO_OUT | GPIO_CLR);
    159  1.2  nonaka 		DPRINTF(("%s: USB host power disabled\n",
    160  1.3  nonaka 		    device_xname(sc->sc_dev)));
    161  1.1   chris 	}
    162  1.1   chris }
    163