Home | History | Annotate | Line # | Download | only in netwalker
netwalker_usb.c revision 1.3
      1  1.1  bsh /*
      2  1.1  bsh  * Copyright (c) 2010  Genetec Corporation.  All rights reserved.
      3  1.1  bsh  * Written by Hiroyuki Bessho for Genetec Corporation.
      4  1.1  bsh  *
      5  1.1  bsh  * Redistribution and use in source and binary forms, with or without
      6  1.1  bsh  * modification, are permitted provided that the following conditions
      7  1.1  bsh  * are met:
      8  1.1  bsh  * 1. Redistributions of source code must retain the above copyright
      9  1.1  bsh  *    notice, this list of conditions and the following disclaimer.
     10  1.1  bsh  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  bsh  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  bsh  *    documentation and/or other materials provided with the distribution.
     13  1.1  bsh  *
     14  1.1  bsh  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     15  1.1  bsh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     16  1.1  bsh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  1.1  bsh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     18  1.1  bsh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     19  1.1  bsh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     20  1.1  bsh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     21  1.1  bsh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     22  1.1  bsh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     23  1.1  bsh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     24  1.1  bsh  * POSSIBILITY OF SUCH DAMAGE.
     25  1.1  bsh  *
     26  1.1  bsh  */
     27  1.1  bsh #include <sys/cdefs.h>
     28  1.3  bsh __KERNEL_RCSID(0, "$NetBSD: netwalker_usb.c,v 1.3 2012/04/15 10:19:47 bsh Exp $");
     29  1.1  bsh 
     30  1.1  bsh #include <sys/param.h>
     31  1.1  bsh #include <sys/systm.h>
     32  1.1  bsh #include <sys/conf.h>
     33  1.1  bsh #include <sys/kernel.h>
     34  1.1  bsh #include <sys/device.h>
     35  1.1  bsh #include <sys/intr.h>
     36  1.1  bsh #include <sys/bus.h>
     37  1.1  bsh 
     38  1.1  bsh #include <dev/usb/usb.h>
     39  1.1  bsh #include <dev/usb/usbdi.h>
     40  1.1  bsh #include <dev/usb/usbdivar.h>
     41  1.1  bsh #include <dev/usb/usb_mem.h>
     42  1.1  bsh 
     43  1.1  bsh #include <dev/usb/ehcireg.h>
     44  1.1  bsh #include <dev/usb/ehcivar.h>
     45  1.1  bsh 
     46  1.1  bsh #include <arm/imx/imx51reg.h>
     47  1.1  bsh #include <arm/imx/imx51var.h>
     48  1.1  bsh #include <arm/imx/imxusbreg.h>
     49  1.1  bsh #include <arm/imx/imxusbvar.h>
     50  1.1  bsh #include <arm/imx/imx51_iomuxreg.h>
     51  1.1  bsh #include <arm/imx/imxgpiovar.h>
     52  1.1  bsh #include "locators.h"
     53  1.1  bsh 
     54  1.1  bsh 
     55  1.1  bsh struct netwalker_usbc_softc {
     56  1.1  bsh 	struct imxusbc_softc  sc_imxusbc;
     57  1.1  bsh };
     58  1.1  bsh 
     59  1.1  bsh 
     60  1.1  bsh static int	imxusbc_match(device_t, cfdata_t, void *);
     61  1.1  bsh static void	imxusbc_attach(device_t, device_t, void *);
     62  1.1  bsh static void	netwalker_usb_init(struct imxehci_softc *);
     63  1.1  bsh 
     64  1.1  bsh static void	init_otg(struct imxehci_softc *);
     65  1.1  bsh static void	init_h1(struct imxehci_softc *);
     66  1.1  bsh 
     67  1.1  bsh extern const struct iomux_conf iomux_usb1_config[];
     68  1.1  bsh 
     69  1.1  bsh /* attach structures */
     70  1.1  bsh CFATTACH_DECL_NEW(imxusbc_axi, sizeof(struct netwalker_usbc_softc),
     71  1.1  bsh     imxusbc_match, imxusbc_attach, NULL, NULL);
     72  1.1  bsh 
     73  1.1  bsh static int
     74  1.1  bsh imxusbc_match(device_t parent, cfdata_t cf, void *aux)
     75  1.1  bsh {
     76  1.1  bsh 	struct axi_attach_args *aa = aux;
     77  1.1  bsh 
     78  1.1  bsh 	printf("%s\n", __func__);
     79  1.1  bsh 
     80  1.1  bsh 	if (aa->aa_addr == USBOH3_BASE)
     81  1.1  bsh 		return 1;
     82  1.1  bsh 	return 0;
     83  1.1  bsh }
     84  1.1  bsh 
     85  1.1  bsh static void
     86  1.1  bsh imxusbc_attach(device_t parent, device_t self, void *aux)
     87  1.1  bsh {
     88  1.1  bsh 	struct axi_attach_args *aa = aux;
     89  1.1  bsh 	struct imxusbc_softc *sc = device_private(self);
     90  1.1  bsh 
     91  1.1  bsh 	sc->sc_init_md_hook = netwalker_usb_init;
     92  1.1  bsh 	sc->sc_setup_md_hook = NULL;
     93  1.1  bsh 
     94  1.1  bsh 	imxusbc_attach_common(parent, self, aa->aa_iot);
     95  1.1  bsh 
     96  1.1  bsh }
     97  1.1  bsh 
     98  1.1  bsh static void
     99  1.1  bsh netwalker_usb_init(struct imxehci_softc *sc)
    100  1.1  bsh {
    101  1.1  bsh 	switch (sc->sc_unit) {
    102  1.1  bsh 	case 0:	/* OTG controller */
    103  1.1  bsh 		init_otg(sc);
    104  1.1  bsh 		break;
    105  1.1  bsh 	case 1:	/* EHCI Host 1 */
    106  1.1  bsh 		init_h1(sc);
    107  1.1  bsh 		break;
    108  1.1  bsh 	default:
    109  1.1  bsh 		aprint_error_dev(sc->sc_hsc.sc_dev, "unit %d not supprted\n",
    110  1.1  bsh 		    sc->sc_unit);
    111  1.1  bsh 	}
    112  1.1  bsh }
    113  1.1  bsh 
    114  1.1  bsh static void
    115  1.1  bsh init_otg(struct imxehci_softc *sc)
    116  1.1  bsh {
    117  1.1  bsh 	struct imxusbc_softc *usbc = sc->sc_usbc;
    118  1.1  bsh 	uint32_t reg;
    119  1.1  bsh 
    120  1.1  bsh 	sc->sc_iftype = IMXUSBC_IF_UTMI;
    121  1.1  bsh 
    122  1.1  bsh 	imxehci_reset(sc);
    123  1.1  bsh 
    124  1.1  bsh 	reg = bus_space_read_4(usbc->sc_iot, usbc->sc_ioh, USBOH3_PHYCTRL0);
    125  1.1  bsh 	reg |= PHYCTRL0_OTG_OVER_CUR_DIS;
    126  1.1  bsh 	bus_space_write_4(usbc->sc_iot, usbc->sc_ioh, USBOH3_PHYCTRL0, reg);
    127  1.1  bsh 
    128  1.1  bsh 	reg = bus_space_read_4(usbc->sc_iot, usbc->sc_ioh, USBOH3_USBCTRL);
    129  1.1  bsh 	reg &= ~(USBCTRL_OWIR|USBCTRL_OPM);
    130  1.1  bsh 	bus_space_write_4(usbc->sc_iot, usbc->sc_ioh, USBOH3_USBCTRL, reg);
    131  1.1  bsh 
    132  1.1  bsh 	reg = bus_space_read_4(usbc->sc_iot, usbc->sc_ioh, USBOH3_PHYCTRL1);
    133  1.1  bsh 	reg = (reg & ~PHYCTRL1_PLLDIVVALUE_MASK) | PHYCTRL1_PLLDIVVALUE_24MHZ;
    134  1.1  bsh 	bus_space_write_4(usbc->sc_iot, usbc->sc_ioh, USBOH3_PHYCTRL1, reg);
    135  1.1  bsh }
    136  1.1  bsh 
    137  1.1  bsh 
    138  1.1  bsh 
    139  1.1  bsh 
    140  1.1  bsh static void
    141  1.1  bsh init_h1(struct imxehci_softc *sc)
    142  1.1  bsh {
    143  1.1  bsh 	struct imxusbc_softc *usbc = sc->sc_usbc;
    144  1.1  bsh 	uint32_t reg;
    145  1.1  bsh 
    146  1.1  bsh 	/* output HIGH to USBH1_STP */
    147  1.2  bsh 	gpio_data_write(GPIO_NO(1, 27), 1);
    148  1.1  bsh 	gpio_set_direction(GPIO_NO(1, 27), GPIO_DIR_OUT);
    149  1.1  bsh 
    150  1.1  bsh 	iomux_mux_config(iomux_usb1_config);
    151  1.1  bsh 
    152  1.1  bsh 	delay(100 * 1000);
    153  1.1  bsh 
    154  1.1  bsh 	/* XXX enable USB clock */
    155  1.1  bsh 
    156  1.1  bsh 	imxehci_reset(sc);
    157  1.1  bsh 
    158  1.1  bsh 	/* select external clock for Host 1 */
    159  1.1  bsh 	reg = bus_space_read_4(usbc->sc_iot, usbc->sc_ioh,
    160  1.1  bsh 			       USBOH3_USBCTRL1);
    161  1.1  bsh 	reg |= USBCTRL1_UH1_EXT_CLK_EN;
    162  1.1  bsh 	bus_space_write_4(usbc->sc_iot, usbc->sc_ioh,
    163  1.1  bsh 			  USBOH3_USBCTRL1, reg);
    164  1.1  bsh 
    165  1.1  bsh 
    166  1.1  bsh 	/* select ULPI interface for Host 1 */
    167  1.1  bsh 	sc->sc_iftype = IMXUSBC_IF_ULPI;
    168  1.1  bsh 
    169  1.1  bsh 	reg = bus_space_read_4(usbc->sc_iot, usbc->sc_ioh,
    170  1.1  bsh 			       USBOH3_USBCTRL);
    171  1.1  bsh 	reg &= ~(USBCTRL_H1PM);
    172  1.1  bsh 	reg |= USBCTRL_H1UIE|USBCTRL_H1WIE;
    173  1.1  bsh 	bus_space_write_4(usbc->sc_iot, usbc->sc_ioh,
    174  1.1  bsh 			  USBOH3_USBCTRL, reg);
    175  1.1  bsh 
    176  1.2  bsh 	iomux_set_function(MUX_PIN(USBH1_STP), IOMUX_CONFIG_ALT0);
    177  1.1  bsh 
    178  1.1  bsh 
    179  1.1  bsh 	/* HUB RESET release */
    180  1.1  bsh 	gpio_data_write(GPIO_NO(1, 7), 1);
    181  1.1  bsh 	gpio_set_direction(GPIO_NO(1, 7), GPIO_DIR_OUT);
    182  1.1  bsh 
    183  1.1  bsh 	/* Drive 26M_OSC_EN line high 3_1 */
    184  1.1  bsh 	gpio_data_write(GPIO_NO(3, 1), 1);
    185  1.1  bsh 	gpio_set_direction(GPIO_NO(3, 1), GPIO_DIR_OUT);
    186  1.1  bsh 
    187  1.1  bsh 	/* Drive USB_CLK_EN_B line low  2_1 */
    188  1.1  bsh 	gpio_data_write(GPIO_NO(2, 1), 0);
    189  1.1  bsh 	gpio_set_direction(GPIO_NO(2, 1), GPIO_DIR_IN);
    190  1.1  bsh 
    191  1.1  bsh 	/* MX51_PIN_EIM_D21 - De-assert USB PHY RESETB */
    192  1.1  bsh 	delay(10 * 1000);
    193  1.1  bsh 	gpio_data_write(GPIO_NO(2, 5), 1);
    194  1.1  bsh 	gpio_set_direction(GPIO_NO(2, 5), GPIO_DIR_OUT);
    195  1.2  bsh 	iomux_set_function(MUX_PIN(EIM_D21), IOMUX_CONFIG_ALT1);
    196  1.1  bsh 	delay(5 * 1000);
    197  1.1  bsh }
    198  1.1  bsh 
    199  1.1  bsh /*
    200  1.1  bsh  * IOMUX setting for USB Host1
    201  1.1  bsh  * taken from Linux driver
    202  1.1  bsh  */
    203  1.1  bsh const struct iomux_conf iomux_usb1_config[] = {
    204  1.1  bsh 
    205  1.1  bsh 	{
    206  1.1  bsh 		/* Initially setup this pin for GPIO, and change to
    207  1.1  bsh 		 * USBH1_STP later */
    208  1.2  bsh 		.pin = MUX_PIN(USBH1_STP),
    209  1.1  bsh 		.mux = IOMUX_CONFIG_ALT2,
    210  1.1  bsh 		.pad = (PAD_CTL_SRE | PAD_CTL_DSE_HIGH |
    211  1.1  bsh 		    PAD_CTL_KEEPER | PAD_CTL_HYS)
    212  1.1  bsh 	},
    213  1.1  bsh 
    214  1.1  bsh 	{
    215  1.1  bsh 		/* Clock */
    216  1.2  bsh 		.pin = MUX_PIN(USBH1_CLK),
    217  1.1  bsh 		.mux = IOMUX_CONFIG_ALT0,
    218  1.2  bsh 		.pad = (PAD_CTL_SRE | PAD_CTL_DSE_HIGH |
    219  1.1  bsh 		    PAD_CTL_KEEPER | PAD_CTL_HYS)
    220  1.1  bsh 	},
    221  1.1  bsh 	{
    222  1.1  bsh 		/* DIR */
    223  1.2  bsh 		.pin = MUX_PIN(USBH1_DIR),
    224  1.1  bsh 		.mux = IOMUX_CONFIG_ALT0,
    225  1.1  bsh 		.pad = (PAD_CTL_SRE | PAD_CTL_DSE_HIGH |
    226  1.1  bsh 		    PAD_CTL_KEEPER | PAD_CTL_HYS)
    227  1.1  bsh 	},
    228  1.1  bsh 
    229  1.1  bsh 	{
    230  1.1  bsh 		/* NXT */
    231  1.2  bsh 		.pin = MUX_PIN(USBH1_NXT),
    232  1.1  bsh 		.mux = IOMUX_CONFIG_ALT0,
    233  1.1  bsh 		.pad = (PAD_CTL_SRE | PAD_CTL_DSE_HIGH |
    234  1.1  bsh 		    PAD_CTL_KEEPER | PAD_CTL_HYS)
    235  1.1  bsh 	},
    236  1.1  bsh 
    237  1.1  bsh #define	USBH1_DATA_CONFIG(n)					\
    238  1.1  bsh 	{							\
    239  1.1  bsh 		/* DATA n */					\
    240  1.2  bsh 		.pin = MUX_PIN(USBH1_DATA##n),			\
    241  1.1  bsh 		.mux = IOMUX_CONFIG_ALT0,			\
    242  1.1  bsh 		.pad = (PAD_CTL_SRE | PAD_CTL_DSE_HIGH |	\
    243  1.1  bsh 		    PAD_CTL_KEEPER | PAD_CTL_PUS_100K_PU |	\
    244  1.1  bsh 		    PAD_CTL_HYS),				\
    245  1.1  bsh 		/* XXX: what does 100K_PU with KEEPER ? */	\
    246  1.1  bsh 	}
    247  1.1  bsh 
    248  1.1  bsh 	USBH1_DATA_CONFIG(0),
    249  1.1  bsh 	USBH1_DATA_CONFIG(1),
    250  1.1  bsh 	USBH1_DATA_CONFIG(2),
    251  1.1  bsh 	USBH1_DATA_CONFIG(3),
    252  1.1  bsh 	USBH1_DATA_CONFIG(4),
    253  1.1  bsh 	USBH1_DATA_CONFIG(5),
    254  1.1  bsh 	USBH1_DATA_CONFIG(6),
    255  1.1  bsh 	USBH1_DATA_CONFIG(7),
    256  1.1  bsh 
    257  1.1  bsh 	{
    258  1.1  bsh 		/* USB_CLK_EN_B  GPIO2[1]*/
    259  1.2  bsh 		.pin = MUX_PIN(EIM_D17),
    260  1.1  bsh 		.mux = IOMUX_CONFIG_ALT1,
    261  1.1  bsh 		.pad = (PAD_CTL_DSE_HIGH | PAD_CTL_PKE | PAD_CTL_SRE),
    262  1.1  bsh 	},
    263  1.1  bsh 
    264  1.1  bsh 	{
    265  1.1  bsh 		/* USB PHY RESETB */
    266  1.2  bsh 		.pin = MUX_PIN(EIM_D21),
    267  1.1  bsh 		.mux = IOMUX_CONFIG_ALT1,
    268  1.1  bsh 		.pad = (PAD_CTL_DSE_HIGH | PAD_CTL_KEEPER |
    269  1.1  bsh 		    PAD_CTL_PUS_100K_PU | PAD_CTL_SRE)
    270  1.1  bsh 	},
    271  1.1  bsh 	{
    272  1.1  bsh 		/* USB HUB RESET */
    273  1.2  bsh 		.pin = MUX_PIN(GPIO1_7),
    274  1.1  bsh 		.mux = IOMUX_CONFIG_ALT0,
    275  1.1  bsh 		.pad = (PAD_CTL_DSE_HIGH | PAD_CTL_SRE),
    276  1.1  bsh 	},
    277  1.3  bsh 	{
    278  1.3  bsh 		/* 26M_OSC pin settings */
    279  1.3  bsh 		.pin = MUX_PIN(DI1_PIN12),
    280  1.3  bsh 		.mux = IOMUX_CONFIG_ALT4,
    281  1.3  bsh 		.pad = (PAD_CTL_DSE_HIGH | PAD_CTL_KEEPER |
    282  1.3  bsh 		    PAD_CTL_SRE),
    283  1.3  bsh 	},
    284  1.1  bsh 
    285  1.1  bsh 	/* end of table */
    286  1.1  bsh 	{.pin = IOMUX_CONF_EOT}
    287  1.1  bsh };
    288