Home | History | Annotate | Line # | Download | only in ingenic
ingenic_dwctwo.c revision 1.6
      1 /*	$NetBSD: ingenic_dwctwo.c,v 1.6 2015/03/09 13:23:57 macallan Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: ingenic_dwctwo.c,v 1.6 2015/03/09 13:23:57 macallan Exp $");
     31 
     32 /*
     33  * adapted from bcm2835_dwctwo.c
     34  */
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/device.h>
     39 #include <sys/mutex.h>
     40 #include <sys/bus.h>
     41 #include <sys/workqueue.h>
     42 
     43 #include <mips/ingenic/ingenic_var.h>
     44 #include <mips/ingenic/ingenic_regs.h>
     45 
     46 #include <dev/usb/usb.h>
     47 #include <dev/usb/usbdi.h>
     48 #include <dev/usb/usbdivar.h>
     49 #include <dev/usb/usb_mem.h>
     50 
     51 #include <dwc2/dwc2var.h>
     52 
     53 #include <dwc2/dwc2.h>
     54 #include "dwc2_core.h"
     55 
     56 #include "opt_ingenic.h"
     57 
     58 struct ingenic_dwc2_softc {
     59 	struct dwc2_softc	sc_dwc2;
     60 
     61 	void			*sc_ih;
     62 };
     63 
     64 static struct dwc2_core_params ingenic_dwc2_params = {
     65 	.otg_cap			= -1,	/* HNP/SRP capable */
     66 	.otg_ver			= -1,	/* 1.3 */
     67 	.dma_enable			= 1,
     68 	.dma_desc_enable		= 0,
     69 	.speed				= -1,	/* High Speed */
     70 	.enable_dynamic_fifo		= -1,
     71 	.en_multiple_tx_fifo		= -1,
     72 	.host_rx_fifo_size		= 1024,	/* 1024 DWORDs */
     73 	.host_nperio_tx_fifo_size	= 1024,	/* 1024 DWORDs */
     74 	.host_perio_tx_fifo_size	= 1024,	/* 1024 DWORDs */
     75 	.max_transfer_size		= -1,
     76 	.max_packet_count		= -1,
     77 	.host_channels			= -1,
     78 	.phy_type			= -1,	/* UTMI */
     79 	.phy_utmi_width			= -1,	/* 16 bits */
     80 	.phy_ulpi_ddr			= -1,	/* Single */
     81 	.phy_ulpi_ext_vbus		= -1,
     82 	.i2c_enable			= -1,
     83 	.ulpi_fs_ls			= -1,
     84 	.host_support_fs_ls_low_power	= -1,
     85 	.host_ls_low_power_phy_clk	= -1,	/* 48 MHz */
     86 	.ts_dline			= -1,
     87 	.reload_ctl			= -1,
     88 	.ahbcfg				= -1,
     89 	.uframe_sched			= 0,
     90 };
     91 
     92 static int ingenic_dwc2_match(device_t, struct cfdata *, void *);
     93 static void ingenic_dwc2_attach(device_t, device_t, void *);
     94 static void ingenic_dwc2_deferred(device_t);
     95 
     96 CFATTACH_DECL_NEW(ingenic_dwctwo, sizeof(struct ingenic_dwc2_softc),
     97     ingenic_dwc2_match, ingenic_dwc2_attach, NULL, NULL);
     98 
     99 /* ARGSUSED */
    100 static int
    101 ingenic_dwc2_match(device_t parent, struct cfdata *match, void *aux)
    102 {
    103 	struct apbus_attach_args *aa = aux;
    104 
    105 	if (strcmp(aa->aa_name, "dwctwo") != 0)
    106 		return 0;
    107 
    108 	return 1;
    109 }
    110 
    111 /* ARGSUSED */
    112 static void
    113 ingenic_dwc2_attach(device_t parent, device_t self, void *aux)
    114 {
    115 	struct ingenic_dwc2_softc *sc = device_private(self);
    116 	struct apbus_attach_args *aa = aux;
    117 	uint32_t reg;
    118 	int error;
    119 
    120 	sc->sc_dwc2.sc_dev = self;
    121 
    122 	sc->sc_dwc2.sc_iot = aa->aa_bst;
    123 	sc->sc_dwc2.sc_bus.dmatag = aa->aa_dmat;
    124 	sc->sc_dwc2.sc_params = &ingenic_dwc2_params;
    125 
    126 	if (aa->aa_addr == 0)
    127 		aa->aa_addr = JZ_DWC2_BASE;
    128 
    129 	error = bus_space_map(aa->aa_bst, aa->aa_addr, 0x20000, 0,
    130 	    &sc->sc_dwc2.sc_ioh);
    131 	if (error) {
    132 		aprint_error_dev(self,
    133 		    "can't map registers for %s: %d\n", aa->aa_name, error);
    134 		return;
    135 	}
    136 
    137 	aprint_naive(": USB controller\n");
    138 	aprint_normal(": USB controller\n");
    139 
    140 	reg = readreg(JZ_USBPCR);
    141 	reg |= PCR_VBUSVLDEXTSEL;
    142 	reg |= PCR_VBUSVLDEXT;
    143 	reg |= PCR_USB_MODE;
    144 	reg |= PCR_COMMONONN;
    145 	reg &= ~PCR_OTG_DISABLE;
    146 	writereg(JZ_USBPCR, reg);
    147 #ifdef INGENIC_DEBUG
    148 	printf("JZ_USBPCR  %08x\n", reg);
    149 #endif
    150 
    151 	reg = readreg(JZ_USBPCR1);
    152 	reg |= PCR_SYNOPSYS;
    153 	reg |= PCR_REFCLK_CORE;
    154 	reg &= ~PCR_CLK_M;
    155 	reg |= PCR_CLK_48;
    156 	reg |= PCR_WORD_I_F0;
    157 	reg |= PCR_WORD_I_F1;
    158 	writereg(JZ_USBPCR1, reg);
    159 #ifdef INGENIC_DEBUG
    160 	printf("JZ_USBPCR1 %08x\n", reg);
    161 	printf("JZ_USBRDT  %08x\n", readreg(JZ_USBRDT));
    162 #endif
    163 
    164 	delay(10000);
    165 
    166 	reg = readreg(JZ_USBPCR);
    167 	reg |= PCR_POR;
    168 	writereg(JZ_USBPCR, reg);
    169 	delay(1000);
    170 	reg &= ~PCR_POR;
    171 	writereg(JZ_USBPCR, reg);
    172 
    173 	delay(10000);
    174 
    175 	sc->sc_ih = evbmips_intr_establish(21, dwc2_intr, &sc->sc_dwc2);
    176 
    177 	if (sc->sc_ih == NULL) {
    178 		aprint_error_dev(self, "failed to establish interrupt %d\n",
    179 		     21);
    180 		goto fail;
    181 	}
    182 
    183 	config_defer(self, ingenic_dwc2_deferred);
    184 
    185 	return;
    186 
    187 fail:
    188 	if (sc->sc_ih) {
    189 		evbmips_intr_disestablish(sc->sc_ih);
    190 		sc->sc_ih = NULL;
    191 	}
    192 	bus_space_unmap(sc->sc_dwc2.sc_iot, sc->sc_dwc2.sc_ioh, 0x20000);
    193 }
    194 
    195 static void
    196 ingenic_dwc2_deferred(device_t self)
    197 {
    198 	struct ingenic_dwc2_softc *sc = device_private(self);
    199 	int error;
    200 
    201 	error = dwc2_init(&sc->sc_dwc2);
    202 	if (error != 0) {
    203 		aprint_error_dev(self, "couldn't initialize host, error=%d\n",
    204 		    error);
    205 		return;
    206 	}
    207 	sc->sc_dwc2.sc_child = config_found(sc->sc_dwc2.sc_dev,
    208 	    &sc->sc_dwc2.sc_bus, usbctlprint);
    209 }
    210