Home | History | Annotate | Line # | Download | only in ingenic
      1  1.8   thorpej /*	$NetBSD: ingenic_ehci.c,v 1.8 2021/08/07 16:18:59 thorpej Exp $ */
      2  1.1  macallan 
      3  1.1  macallan /*-
      4  1.1  macallan  * Copyright (c) 2015 Michael Lorenz
      5  1.1  macallan  * All rights reserved.
      6  1.1  macallan  *
      7  1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8  1.1  macallan  * modification, are permitted provided that the following conditions
      9  1.1  macallan  * are met:
     10  1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11  1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12  1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15  1.1  macallan  *
     16  1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  macallan  */
     28  1.1  macallan 
     29  1.1  macallan #include <sys/cdefs.h>
     30  1.8   thorpej __KERNEL_RCSID(0, "$NetBSD: ingenic_ehci.c,v 1.8 2021/08/07 16:18:59 thorpej Exp $");
     31  1.1  macallan 
     32  1.1  macallan #include <sys/param.h>
     33  1.1  macallan #include <sys/systm.h>
     34  1.1  macallan #include <sys/device.h>
     35  1.1  macallan #include <sys/mutex.h>
     36  1.1  macallan #include <sys/bus.h>
     37  1.1  macallan #include <sys/workqueue.h>
     38  1.1  macallan 
     39  1.1  macallan #include <mips/ingenic/ingenic_var.h>
     40  1.1  macallan #include <mips/ingenic/ingenic_regs.h>
     41  1.1  macallan 
     42  1.1  macallan #include <dev/usb/usb.h>
     43  1.1  macallan #include <dev/usb/usbdi.h>
     44  1.1  macallan #include <dev/usb/usbdivar.h>
     45  1.1  macallan #include <dev/usb/usb_mem.h>
     46  1.1  macallan 
     47  1.1  macallan #include <dev/usb/ehcireg.h>
     48  1.1  macallan #include <dev/usb/ehcivar.h>
     49  1.1  macallan 
     50  1.3  macallan #include <dev/usb/usbdevs.h>
     51  1.3  macallan 
     52  1.1  macallan #include "opt_ingenic.h"
     53  1.1  macallan #include "ohci.h"
     54  1.1  macallan 
     55  1.1  macallan static int ingenic_ehci_match(device_t, struct cfdata *, void *);
     56  1.1  macallan static void ingenic_ehci_attach(device_t, device_t, void *);
     57  1.1  macallan 
     58  1.1  macallan CFATTACH_DECL_NEW(ingenic_ehci, sizeof(struct ehci_softc),
     59  1.1  macallan     ingenic_ehci_match, ingenic_ehci_attach, NULL, NULL);
     60  1.1  macallan 
     61  1.1  macallan #if NOHCI > 0
     62  1.1  macallan extern device_t ingenic_ohci;
     63  1.1  macallan #endif
     64  1.1  macallan 
     65  1.1  macallan /* ARGSUSED */
     66  1.1  macallan static int
     67  1.1  macallan ingenic_ehci_match(device_t parent, struct cfdata *match, void *aux)
     68  1.1  macallan {
     69  1.1  macallan 	struct apbus_attach_args *aa = aux;
     70  1.1  macallan 
     71  1.1  macallan 	if (strcmp(aa->aa_name, "ehci") != 0)
     72  1.1  macallan 		return 0;
     73  1.1  macallan 
     74  1.1  macallan 	return 1;
     75  1.1  macallan }
     76  1.1  macallan 
     77  1.4  macallan static int
     78  1.4  macallan ingenic_ehci_enable(struct ehci_softc *sc)
     79  1.4  macallan {
     80  1.4  macallan 	uint32_t reg;
     81  1.4  macallan 
     82  1.4  macallan 	/* Togle VBUS pin */
     83  1.4  macallan 	gpio_set(5, 15, 0);
     84  1.4  macallan 	delay(250000);
     85  1.4  macallan 	gpio_set(5, 15, 1);
     86  1.4  macallan 	delay(250000);
     87  1.4  macallan 
     88  1.4  macallan 	/* Enable OTG, should not be necessary since we use PLL clock */
     89  1.4  macallan 	reg = readreg(JZ_USBPCR);
     90  1.4  macallan 	reg &= ~(PCR_OTG_DISABLE);
     91  1.4  macallan 	writereg(JZ_USBPCR, reg);
     92  1.4  macallan 
     93  1.4  macallan 	/* Select CORE as PLL reference */
     94  1.4  macallan 	reg = readreg(JZ_USBPCR1);
     95  1.4  macallan 	reg |= PCR_REFCLK_CORE;
     96  1.4  macallan 	writereg(JZ_USBPCR1, reg);
     97  1.4  macallan 
     98  1.4  macallan 	/* Configure OTG PHY clock frequency */
     99  1.4  macallan 	reg  = readreg(JZ_USBPCR1);
    100  1.4  macallan 	reg &= ~PCR_CLK_M;
    101  1.4  macallan 	reg |= PCR_CLK_48;
    102  1.4  macallan 	writereg(JZ_USBPCR1, reg);
    103  1.4  macallan 
    104  1.4  macallan 	/* Do not force port1 to suspend mode */
    105  1.4  macallan 	reg = readreg(JZ_OPCR);
    106  1.4  macallan 	reg |= OPCR_SPENDN1;
    107  1.4  macallan 	writereg(JZ_OPCR, reg);
    108  1.4  macallan 
    109  1.4  macallan 	/* D- pulldown */
    110  1.4  macallan 	reg = readreg(JZ_USBPCR1);
    111  1.4  macallan 	reg |= PCR_DMPD1;
    112  1.4  macallan 	writereg(JZ_USBPCR1, reg);
    113  1.4  macallan 
    114  1.4  macallan 	/* D+ pulldown */
    115  1.4  macallan 	reg = readreg(JZ_USBPCR1);
    116  1.4  macallan 	reg |= PCR_DPPD1;
    117  1.4  macallan 	writereg(JZ_USBPCR1, reg);
    118  1.4  macallan 
    119  1.4  macallan 	/* 16 bit bus witdth for port 1 (and 0) */
    120  1.4  macallan 	reg = readreg(JZ_USBPCR1);
    121  1.4  macallan 	reg |= PCR_WORD_I_F1 | PCR_WORD_I_F0;
    122  1.4  macallan 	writereg(JZ_USBPCR1, reg);
    123  1.4  macallan 
    124  1.4  macallan 	/* Reset USB */
    125  1.4  macallan 	reg = readreg(JZ_USBPCR);
    126  1.4  macallan 	reg |= PCR_POR;
    127  1.4  macallan 	writereg(JZ_USBPCR, reg);
    128  1.4  macallan 	delay(1);
    129  1.4  macallan 	reg = readreg(JZ_USBPCR);
    130  1.4  macallan 	reg &= ~(PCR_POR);
    131  1.4  macallan 	writereg(JZ_USBPCR, reg);
    132  1.4  macallan 
    133  1.4  macallan 	/* Soft-reset USB */
    134  1.4  macallan 	reg = readreg(JZ_SRBC);
    135  1.4  macallan 	reg |= (1 << 14);
    136  1.4  macallan 	writereg(JZ_SRBC, reg);
    137  1.4  macallan 	/* 300ms */
    138  1.4  macallan 	delay(300000);
    139  1.4  macallan 
    140  1.4  macallan 	reg = readreg(JZ_SRBC);
    141  1.4  macallan 	reg &= ~(1 << 14);
    142  1.4  macallan 	writereg(JZ_SRBC, reg);
    143  1.4  macallan 
    144  1.4  macallan 	/* 300ms */
    145  1.4  macallan 	delay(300000);
    146  1.4  macallan 
    147  1.4  macallan 	return (0);
    148  1.4  macallan }
    149  1.4  macallan 
    150  1.1  macallan /* ARGSUSED */
    151  1.1  macallan static void
    152  1.1  macallan ingenic_ehci_attach(device_t parent, device_t self, void *aux)
    153  1.1  macallan {
    154  1.1  macallan 	struct ehci_softc *sc = device_private(self);
    155  1.1  macallan 	struct apbus_attach_args *aa = aux;
    156  1.1  macallan 	void *ih;
    157  1.5     skrll 	int error;
    158  1.1  macallan 	uint32_t reg;
    159  1.1  macallan 
    160  1.1  macallan 	sc->sc_dev = self;
    161  1.1  macallan 
    162  1.1  macallan 	sc->iot = aa->aa_bst;
    163  1.5     skrll 	sc->sc_bus.ub_dmatag = aa->aa_dmat;
    164  1.5     skrll 	sc->sc_bus.ub_hcpriv = sc;
    165  1.1  macallan 	sc->sc_size = 0x1000;
    166  1.5     skrll 	sc->sc_bus.ub_revision = USBREV_2_0;
    167  1.1  macallan 
    168  1.1  macallan 	if (aa->aa_addr == 0)
    169  1.1  macallan 		aa->aa_addr = JZ_EHCI_BASE;
    170  1.1  macallan 
    171  1.1  macallan 	error = bus_space_map(aa->aa_bst, aa->aa_addr, 0x1000, 0, &sc->ioh);
    172  1.1  macallan 	if (error) {
    173  1.1  macallan 		aprint_error_dev(self,
    174  1.1  macallan 		    "can't map registers for %s: %d\n", aa->aa_name, error);
    175  1.1  macallan 		return;
    176  1.1  macallan 	}
    177  1.1  macallan 
    178  1.1  macallan 	aprint_naive(": EHCI USB controller\n");
    179  1.1  macallan 	aprint_normal(": EHCI USB controller\n");
    180  1.1  macallan 
    181  1.4  macallan 	ingenic_ehci_enable(sc);
    182  1.1  macallan 
    183  1.1  macallan 	/* Disable EHCI interrupts */
    184  1.1  macallan 	bus_space_write_4(sc->iot, sc->ioh, EHCI_USBINTR, 0);
    185  1.1  macallan 
    186  1.2  macallan 	ih = evbmips_intr_establish(aa->aa_irq, ehci_intr, sc);
    187  1.4  macallan 
    188  1.1  macallan 	if (ih == NULL) {
    189  1.1  macallan 		aprint_error_dev(self, "failed to establish interrupt %d\n",
    190  1.2  macallan 		     aa->aa_irq);
    191  1.1  macallan 		goto fail;
    192  1.1  macallan 	}
    193  1.1  macallan 
    194  1.1  macallan #if NOHCI > 0
    195  1.1  macallan 	if (ingenic_ohci != NULL) {
    196  1.1  macallan 		sc->sc_ncomp = 1;
    197  1.1  macallan 		sc->sc_comps[0] = ingenic_ohci;
    198  1.1  macallan 	} else
    199  1.1  macallan 		sc->sc_ncomp = 0;
    200  1.1  macallan #else
    201  1.1  macallan 	sc->sc_ncomp = 0;
    202  1.4  macallan 	sc->sc_npcomp = 0;
    203  1.1  macallan #endif
    204  1.1  macallan 
    205  1.5     skrll 	error = ehci_init(sc);
    206  1.5     skrll 	if (error) {
    207  1.5     skrll 		aprint_error_dev(self, "init failed, error=%d\n", error);
    208  1.1  macallan 		goto fail;
    209  1.1  macallan 	}
    210  1.1  macallan 
    211  1.4  macallan 	/*
    212  1.4  macallan 	 * voodoo from the linux driver:
    213  1.4  macallan 	 * select utmi data bus width of controller to 16bit
    214  1.4  macallan 	 */
    215  1.4  macallan 	reg = bus_space_read_4(sc->iot, sc->ioh,  0xb0);
    216  1.4  macallan 	reg |= 1 << 6;
    217  1.4  macallan 	bus_space_write_4(sc->iot, sc->ioh,  0xb0, reg);
    218  1.4  macallan 
    219  1.1  macallan 	/* Attach USB device */
    220  1.8   thorpej 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
    221  1.1  macallan 
    222  1.1  macallan 	return;
    223  1.1  macallan 
    224  1.1  macallan fail:
    225  1.1  macallan 	if (ih) {
    226  1.1  macallan 		evbmips_intr_disestablish(ih);
    227  1.1  macallan 	}
    228  1.1  macallan 	bus_space_unmap(sc->iot, sc->ioh, 0x1000);
    229  1.1  macallan }
    230