Home | History | Annotate | Line # | Download | only in imx
imxusb.c revision 1.2.2.1
      1 /*	$NetBSD: imxusb.c,v 1.2.2.1 2012/11/20 03:01:05 tls Exp $	*/
      2 /*
      3  * Copyright (c) 2009, 2010  Genetec Corporation.  All rights reserved.
      4  * Written by Hashimoto Kenichi and Hiroyuki Bessho for Genetec Corporation.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     19  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  * POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: imxusb.c,v 1.2.2.1 2012/11/20 03:01:05 tls Exp $");
     29 
     30 #include <sys/param.h>
     31 #include <sys/systm.h>
     32 #include <sys/conf.h>
     33 #include <sys/kernel.h>
     34 #include <sys/device.h>
     35 #include <sys/intr.h>
     36 #include <sys/bus.h>
     37 
     38 #include <dev/usb/usb.h>
     39 #include <dev/usb/usbdi.h>
     40 #include <dev/usb/usbdivar.h>
     41 #include <dev/usb/usb_mem.h>
     42 
     43 #include <dev/usb/ehcireg.h>
     44 #include <dev/usb/ehcivar.h>
     45 
     46 #include <arm/imx/imxusbreg.h>
     47 #include <arm/imx/imxusbvar.h>
     48 #include <arm/imx/imxgpiovar.h>
     49 #include "locators.h"
     50 
     51 #include <dev/usb/ulpireg.h>	/* for test */
     52 
     53 static int	imxehci_match(device_t, cfdata_t, void *);
     54 static void	imxehci_attach(device_t, device_t, void *);
     55 
     56 uint8_t imxusb_ulpi_read(struct imxehci_softc *sc, int addr);
     57 void imxusb_ulpi_write(struct imxehci_softc *sc, int addr, uint8_t data);
     58 static void ulpi_reset(struct imxehci_softc *sc);
     59 
     60 
     61 
     62 /* attach structures */
     63 CFATTACH_DECL_NEW(imxehci, sizeof(struct imxehci_softc),
     64     imxehci_match, imxehci_attach, NULL, NULL);
     65 
     66 static int
     67 imxehci_match(device_t parent, cfdata_t cf, void *aux)
     68 {
     69 	struct imxusbc_attach_args *aa = aux;
     70 
     71 	if (aa->aa_unit < 0 || 3 < aa->aa_unit) {
     72 		return 0;
     73 	}
     74 
     75 	return 1;
     76 }
     77 
     78 static void
     79 imxehci_attach(device_t parent, device_t self, void *aux)
     80 {
     81 	struct imxusbc_attach_args *aa = aux;
     82 	struct imxusbc_softc *usbc = device_private(parent);
     83 	struct imxehci_softc *sc = device_private(self);
     84 	ehci_softc_t *hsc = &sc->sc_hsc;
     85 	bus_space_tag_t iot;
     86 	uint16_t hcirev;
     87 	usbd_status r;
     88 	uint32_t id, hwhost, hwdevice;
     89 	const char *comma;
     90 
     91 	sc->sc_hsc.sc_dev = self;
     92 	iot = sc->sc_iot = sc->sc_hsc.iot = aa->aa_iot;
     93 	sc->sc_unit = aa->aa_unit;
     94 	sc->sc_usbc = usbc;
     95 	hsc->sc_bus.hci_private = sc;
     96 
     97 	aprint_normal("\n");
     98 
     99 	/* per unit registers */
    100 	if (bus_space_subregion(iot, aa->aa_ioh,
    101 		aa->aa_unit * IMXUSB_EHCI_SIZE, IMXUSB_EHCI_SIZE,
    102 		&sc->sc_ioh) ||
    103 	    bus_space_subregion(iot, aa->aa_ioh,
    104 		aa->aa_unit * IMXUSB_EHCI_SIZE + IMXUSB_EHCIREGS,
    105 		IMXUSB_EHCI_SIZE - IMXUSB_EHCIREGS,
    106 		&sc->sc_hsc.ioh)) {
    107 
    108 		aprint_error_dev(self, "can't subregion\n");
    109 		return;
    110 	}
    111 
    112 	id = bus_space_read_4(iot, sc->sc_ioh, IMXUSB_ID);
    113 	hcirev = bus_space_read_2(iot, sc->sc_hsc.ioh, EHCI_HCIVERSION);
    114 
    115 	aprint_normal_dev(self,
    116 	    "i.MX USB Controller id=%d revision=%d HCI revision=0x%x\n",
    117 	    id & (uint32_t)IMXUSB_ID_ID_MASK,
    118 	    (id & (uint32_t)IMXUSB_ID_REVISION_MASK) >>
    119 	    	IMXUSB_ID_REVISION_SHIFT,
    120 	    hcirev);
    121 
    122 	hwhost = bus_space_read_4(iot, sc->sc_ioh, IMXUSB_HWHOST);
    123 	hwdevice = bus_space_read_4(iot, sc->sc_ioh, IMXUSB_HWDEVICE);
    124 
    125 	aprint_normal_dev(self, "");
    126 
    127 	comma = "";
    128 	if (hwhost & HWHOST_HC) {
    129 		int n_ports = 1 + ((hwhost & HWHOST_NPORT_MASK) >>
    130 		    HWHOST_NPORT_SHIFT);
    131 		aprint_normal("%d host port%s",
    132 		    n_ports, n_ports > 1 ? "s" : "");
    133 		comma = ", ";
    134 	}
    135 
    136 	if (hwdevice & HWDEVICE_DC) {
    137 		int n_endpoints = (hwdevice & HWDEVICE_DEVEP_MASK) >>
    138 		    HWDEVICE_DEVEP_SHIFT;
    139 		aprint_normal("%sdevice capable, %d endpoint%s",
    140 		    comma,
    141 		    n_endpoints, n_endpoints > 1 ? "s" : "");
    142 	}
    143 	aprint_normal("\n");
    144 
    145 	sc->sc_hsc.sc_bus.dmatag = aa->aa_dmat;
    146 
    147 	sc->sc_hsc.sc_offs = bus_space_read_1(iot, sc->sc_hsc.ioh,
    148 	    EHCI_CAPLENGTH);
    149 
    150 	/* Platform dependent setup */
    151 	if (usbc->sc_init_md_hook)
    152 		usbc->sc_init_md_hook(sc);
    153 
    154 
    155 	imxehci_reset(sc);
    156 	imxehci_select_interface(sc, sc->sc_iftype);
    157 
    158 	if (sc->sc_iftype == IMXUSBC_IF_ULPI) {
    159 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, IMXUSB_ULPIVIEW, 0);
    160 
    161 		aprint_normal_dev(hsc->sc_dev,
    162 		    "ULPI phy VID 0x%04x PID 0x%04x\n",
    163 		    (imxusb_ulpi_read(sc, ULPI_VENDOR_ID_LOW) |
    164 			imxusb_ulpi_read(sc, ULPI_VENDOR_ID_HIGH) << 8),
    165 		    (imxusb_ulpi_read(sc, ULPI_PRODUCT_ID_LOW) |
    166 			imxusb_ulpi_read(sc, ULPI_PRODUCT_ID_HIGH) << 8));
    167 
    168 		ulpi_reset(sc);
    169 
    170 	}
    171 
    172 	imxehci_host_mode(sc);
    173 
    174 	if (usbc->sc_setup_md_hook)
    175 		usbc->sc_setup_md_hook(sc, IMXUSB_HOST);
    176 	else if (sc->sc_iftype == IMXUSBC_IF_ULPI) {
    177 		imxusb_ulpi_write(sc, ULPI_FUNCTION_CONTROL + ULPI_REG_CLEAR,
    178 		    OTG_CONTROL_IDPULLUP);
    179 
    180 		imxusb_ulpi_write(sc, ULPI_OTG_CONTROL + ULPI_REG_SET,
    181 		    OTG_CONTROL_USEEXTVBUSIND |
    182 		    OTG_CONTROL_DRVVBUSEXT |
    183 		    OTG_CONTROL_DRVVBUS |
    184 		    OTG_CONTROL_CHRGVBUS
    185 		    );
    186 	}
    187 
    188 	/* Disable interrupts, so we don't get any spurious ones. */
    189 	EOWRITE4(hsc, EHCI_USBINTR, 0);
    190 
    191 	intr_establish(aa->aa_irq, IPL_USB, IST_LEVEL, ehci_intr, hsc);
    192 
    193 	/* Figure out vendor for root hub descriptor. */
    194 	strlcpy(hsc->sc_vendor, "i.MX", sizeof(hsc->sc_vendor));
    195 
    196 	r = ehci_init(hsc);
    197 	if (r != USBD_NORMAL_COMPLETION) {
    198 		aprint_error_dev(self, "init failed, error=%d\n", r);
    199 		return;
    200 	}
    201 
    202 	/* Attach usb device. */
    203 	hsc->sc_child = config_found(self, &hsc->sc_bus, usbctlprint);
    204 }
    205 
    206 
    207 
    208 
    209 void
    210 imxehci_select_interface(struct imxehci_softc *sc, enum imx_usb_if interface)
    211 {
    212 	uint32_t reg;
    213 	struct ehci_softc *hsc = &sc->sc_hsc;
    214 
    215 	reg = EOREAD4(hsc, EHCI_PORTSC(1));
    216 	reg = (reg & ~PORTSC_PTS_MASK) | (interface << PORTSC_PTS_SHIFT);
    217 	EOWRITE4(hsc, EHCI_PORTSC(1), reg);
    218 }
    219 
    220 
    221 static uint32_t
    222 ulpi_wakeup(struct imxehci_softc *sc, int tout)
    223 {
    224 	uint32_t ulpi_view;
    225 	int i = 0;
    226 	ulpi_view = bus_space_read_4(sc->sc_iot, sc->sc_ioh, IMXUSB_ULPIVIEW);
    227 
    228 	if ( !(ulpi_view & ULPI_SS) ) {
    229 		bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    230 		    IMXUSB_ULPIVIEW, ULPI_WU);
    231 		for (i = 0; (tout < 0) || (i < tout); i++) {
    232 			ulpi_view = bus_space_read_4(sc->sc_iot,
    233 			    sc->sc_ioh, IMXUSB_ULPIVIEW);
    234 			if ( !(ulpi_view & ULPI_WU) )
    235 				break;
    236 			delay(1);
    237 		};
    238 	}
    239 
    240 	if ((tout > 0) && (i >= tout)) {
    241 		aprint_error_dev(sc->sc_hsc.sc_dev, "%s: timeout\n", __func__);
    242 	}
    243 
    244 	return ulpi_view;
    245 }
    246 
    247 static uint32_t
    248 ulpi_wait(struct imxehci_softc *sc, int tout)
    249 {
    250 	uint32_t ulpi_view;
    251 	int i;
    252 	ulpi_view = bus_space_read_4(sc->sc_iot, sc->sc_ioh, IMXUSB_ULPIVIEW);
    253 
    254 	for (i = 0; (tout < 0) | (i < tout); i++) {
    255 		ulpi_view = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    256 		    IMXUSB_ULPIVIEW);
    257 		if (!(ulpi_view & ULPI_RUN))
    258 			break;
    259 		delay(1);
    260 	}
    261 
    262 	if ((tout > 0) && (i >= tout)) {
    263 		aprint_error_dev(sc->sc_hsc.sc_dev, "%s: timeout\n", __func__);
    264 	}
    265 
    266 	return ulpi_view;
    267 }
    268 
    269 #define	TIMEOUT	100000
    270 
    271 uint8_t
    272 imxusb_ulpi_read(struct imxehci_softc *sc, int addr)
    273 {
    274 	uint32_t data;
    275 
    276 	ulpi_wakeup(sc, TIMEOUT);
    277 
    278 	data = ULPI_RUN | (addr << ULPI_ADDR_SHIFT);
    279 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IMXUSB_ULPIVIEW, data);
    280 
    281 	data = ulpi_wait(sc, TIMEOUT);
    282 
    283 	return (data & ULPI_DATRD_MASK) >> ULPI_DATRD_SHIFT;
    284 }
    285 
    286 void
    287 imxusb_ulpi_write(struct imxehci_softc *sc, int addr, uint8_t data)
    288 {
    289 	uint32_t reg;
    290 
    291 	ulpi_wakeup(sc, TIMEOUT);
    292 
    293 	reg = ULPI_RUN | ULPI_RW | ((addr) << ULPI_ADDR_SHIFT) | data;
    294 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IMXUSB_ULPIVIEW, reg);
    295 
    296 	ulpi_wait(sc, TIMEOUT);
    297 
    298 	return;
    299 }
    300 
    301 #if 0
    302 static int
    303 ulpi_scratch_test(struct imxehci_softc *sc)
    304 {
    305 	uint32_t ulpi_view;
    306 
    307 	ulpi_view = ulpi_wakeup(sc, 1000);
    308 	if (ulpi_view & ULPI_WU) {
    309 		return -1;
    310 	}
    311 
    312 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IMXUSB_ULPIVIEW,
    313 		(ULPI_RUN | ULPI_RW |
    314 		 (ULPI_SCRATCH << ULPI_ADDR_SHIFT) | 0xAA));
    315 
    316 	ulpi_view = ulpi_wait(sc, 1000);
    317 
    318 	if (ulpi_view & ULPI_RUN) {
    319 		return -1;
    320 	}
    321 
    322 	return 0;
    323 }
    324 #endif
    325 
    326 static void
    327 ulpi_reset(struct imxehci_softc *sc)
    328 {
    329 	uint8_t data;
    330 	int timo = 1000 * 1000;	/* XXXX: 1sec */
    331 
    332 	imxusb_ulpi_write(sc, ULPI_FUNCTION_CONTROL + ULPI_REG_SET,
    333 	    FUNCTION_CONTROL_RESET /*0x20*/);
    334 	do {
    335 		data = imxusb_ulpi_read(sc, ULPI_FUNCTION_CONTROL);
    336 		if (!(data & FUNCTION_CONTROL_RESET))
    337 			break;
    338 		delay(100);
    339 		timo -= 100;
    340 	} while (timo > 0);
    341 	if (timo <= 0) {
    342 		aprint_error_dev(sc->sc_hsc.sc_dev, "%s: reset failed!!\n",
    343 		    __func__);
    344 		return;
    345 	}
    346 
    347 	return;
    348 }
    349 
    350 void
    351 imxehci_reset(struct imxehci_softc *sc)
    352 {
    353 	uint32_t reg;
    354 	int i;
    355 	struct ehci_softc *hsc = &sc->sc_hsc;
    356 #define	RESET_TIMEOUT 100
    357 
    358 	reg = EOREAD4(hsc, EHCI_USBCMD);
    359 	reg &= ~EHCI_CMD_RS;
    360 	EOWRITE4(hsc, EHCI_USBCMD, reg);
    361 
    362 	for (i=0; i < RESET_TIMEOUT; ++i) {
    363 		reg = EOREAD4(hsc, EHCI_USBCMD);
    364 		if ((reg & EHCI_CMD_RS) == 0)
    365 			break;
    366 		usb_delay_ms(&hsc->sc_bus, 1);
    367 	}
    368 
    369 	EOWRITE4(hsc, EHCI_USBCMD, reg | EHCI_CMD_HCRESET);
    370 	for (i = 0; i < RESET_TIMEOUT; i++) {
    371 		reg = EOREAD4(hsc, EHCI_USBCMD);
    372 		if ((reg &  EHCI_CMD_HCRESET) == 0)
    373 			break;
    374 		usb_delay_ms(&hsc->sc_bus, 1);
    375 	}
    376 	if (i >= RESET_TIMEOUT) {
    377 		aprint_error_dev(hsc->sc_dev, "reset timeout (%x)\n", reg);
    378 	}
    379 
    380 	usb_delay_ms(&hsc->sc_bus, 100);
    381 }
    382 
    383 void
    384 imxehci_host_mode(struct imxehci_softc *sc)
    385 {
    386 	struct ehci_softc *hsc = &sc->sc_hsc;
    387 	uint32_t reg;
    388 
    389 	reg = EOREAD4(hsc, EHCI_PORTSC(1));
    390 	reg &= ~(EHCI_PS_CSC | EHCI_PS_PEC | EHCI_PS_OCC);
    391 	reg |= EHCI_PS_PP | EHCI_PS_PE;
    392 	EOWRITE4(hsc, EHCI_PORTSC(1), reg);
    393 
    394 	reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, IMXUSB_OTGSC);
    395 	reg |= OTGSC_IDPU;
    396 	reg |= OTGSC_DPIE | OTGSC_IDIE;
    397 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IMXUSB_OTGSC, reg);
    398 
    399 	reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, IMXUSB_OTGMODE);
    400 	reg |= USBMODE_HOST;
    401 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IMXUSB_OTGMODE, reg);
    402 }
    403