ti_otg.c revision 1.1
11.1Sjmcneill/* $NetBSD: ti_otg.c,v 1.1 2019/10/27 16:31:26 jmcneill Exp $ */
21.1Sjmcneill/*
31.1Sjmcneill * Copyright (c) 2013 Manuel Bouyer.  All rights reserved.
41.1Sjmcneill *
51.1Sjmcneill * Redistribution and use in source and binary forms, with or without
61.1Sjmcneill * modification, are permitted provided that the following conditions
71.1Sjmcneill * are met:
81.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
91.1Sjmcneill *	notice, this list of conditions and the following disclaimer.
101.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
111.1Sjmcneill *	notice, this list of conditions and the following disclaimer in the
121.1Sjmcneill *	documentation and/or other materials provided with the distribution.
131.1Sjmcneill *
141.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
151.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
161.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
171.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
181.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
191.1Sjmcneill * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201.1Sjmcneill * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
211.1Sjmcneill * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
221.1Sjmcneill * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
231.1Sjmcneill * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
241.1Sjmcneill */
251.1Sjmcneill
261.1Sjmcneill#include <sys/cdefs.h>
271.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: ti_otg.c,v 1.1 2019/10/27 16:31:26 jmcneill Exp $");
281.1Sjmcneill
291.1Sjmcneill#include <sys/param.h>
301.1Sjmcneill#include <sys/systm.h>
311.1Sjmcneill#include <sys/device.h>
321.1Sjmcneill#include <sys/conf.h>
331.1Sjmcneill#include <sys/bus.h>
341.1Sjmcneill#include <sys/proc.h>
351.1Sjmcneill#include <sys/kernel.h>
361.1Sjmcneill#include <sys/mutex.h>
371.1Sjmcneill#include <sys/condvar.h>
381.1Sjmcneill
391.1Sjmcneill#include <arm/ti/ti_prcm.h>
401.1Sjmcneill#include <arm/ti/ti_otgreg.h>
411.1Sjmcneill
421.1Sjmcneill#include <dev/fdt/fdtvar.h>
431.1Sjmcneill
441.1Sjmcneillstatic const char * compatible [] = {
451.1Sjmcneill	"ti,am33xx-usb",
461.1Sjmcneill	NULL
471.1Sjmcneill};
481.1Sjmcneill
491.1Sjmcneillstruct tiotg_softc {
501.1Sjmcneill	device_t		sc_dev;
511.1Sjmcneill	bus_space_tag_t		sc_iot;
521.1Sjmcneill	bus_space_handle_t	sc_ioh;
531.1Sjmcneill	bus_dma_tag_t		sc_dmat;
541.1Sjmcneill	void *			sc_ih;
551.1Sjmcneill};
561.1Sjmcneill
571.1Sjmcneillstatic int	tiotg_match(device_t, cfdata_t, void *);
581.1Sjmcneillstatic void	tiotg_attach(device_t, device_t, void *);
591.1Sjmcneill
601.1SjmcneillCFATTACH_DECL_NEW(ti_otg, sizeof(struct tiotg_softc),
611.1Sjmcneill    tiotg_match, tiotg_attach, NULL, NULL);
621.1Sjmcneill
631.1Sjmcneillstatic int
641.1Sjmcneilltiotg_match(device_t parent, cfdata_t match, void *aux)
651.1Sjmcneill{
661.1Sjmcneill	struct fdt_attach_args * const faa = aux;
671.1Sjmcneill
681.1Sjmcneill	return of_match_compatible(faa->faa_phandle, compatible);
691.1Sjmcneill}
701.1Sjmcneill
711.1Sjmcneillstatic void
721.1Sjmcneilltiotg_attach(device_t parent, device_t self, void *aux)
731.1Sjmcneill{
741.1Sjmcneill	struct tiotg_softc *sc = device_private(self);
751.1Sjmcneill	struct fdt_attach_args * const faa = aux;
761.1Sjmcneill	const int phandle = faa->faa_phandle;
771.1Sjmcneill	bus_addr_t addr;
781.1Sjmcneill	bus_size_t size;
791.1Sjmcneill	uint32_t val;
801.1Sjmcneill
811.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
821.1Sjmcneill		aprint_error(": couldn't get registers\n");
831.1Sjmcneill		return;
841.1Sjmcneill	}
851.1Sjmcneill
861.1Sjmcneill	sc->sc_iot = faa->faa_bst;
871.1Sjmcneill	sc->sc_dmat = faa->faa_dmat;
881.1Sjmcneill	sc->sc_dev = self;
891.1Sjmcneill
901.1Sjmcneill	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
911.1Sjmcneill		aprint_error(": couldn't map registers\n");
921.1Sjmcneill		return;
931.1Sjmcneill	}
941.1Sjmcneill
951.1Sjmcneill	aprint_normal(": TI dual-port USB controller");
961.1Sjmcneill
971.1Sjmcneill#if 0
981.1Sjmcneill	/* XXX this looks wrong */
991.1Sjmcneill	prcm_write_4(AM335X_PRCM_CM_WKUP, CM_WKUP_CM_CLKDCOLDO_DPLL_PER,
1001.1Sjmcneill	    CM_WKUP_CM_CLKDCOLDO_DPLL_PER_CLKDCOLDO_GATE_CTRL|
1011.1Sjmcneill	    CM_WKUP_CM_CLKDCOLDO_DPLL_PER_CLKDCOLDO_ST);
1021.1Sjmcneill
1031.1Sjmcneill	prcm_write_4(AM335X_PRCM_CM_PER, CM_PER_USB0_CLKCTRL, 2);
1041.1Sjmcneill	while ((prcm_read_4(AM335X_PRCM_CM_PER, CM_PER_USB0_CLKCTRL) & 0x3) != 2)
1051.1Sjmcneill		delay(10);
1061.1Sjmcneill
1071.1Sjmcneill	while (prcm_read_4(AM335X_PRCM_CM_PER, CM_PER_USB0_CLKCTRL) & (3<<16))
1081.1Sjmcneill		delay(10);
1091.1Sjmcneill#endif
1101.1Sjmcneill
1111.1Sjmcneill	/* reset module */
1121.1Sjmcneill	TIOTG_USBSS_WRITE4(sc, USBSS_SYSCONFIG, USBSS_SYSCONFIG_SRESET);
1131.1Sjmcneill	while (TIOTG_USBSS_READ4(sc, USBSS_SYSCONFIG) & USBSS_SYSCONFIG_SRESET)
1141.1Sjmcneill		delay(10);
1151.1Sjmcneill	val = TIOTG_USBSS_READ4(sc, USBSS_REVREG);
1161.1Sjmcneill	aprint_normal(": version v%d.%d.%d.%d",
1171.1Sjmcneill	    (val >> 11) & 15, (val >> 8) & 7, (val >> 6) & 3, val & 63);
1181.1Sjmcneill	aprint_normal("\n");
1191.1Sjmcneill
1201.1Sjmcneill	/* enable clock */
1211.1Sjmcneill	if (ti_prcm_enable_hwmod(phandle, 0) != 0) {
1221.1Sjmcneill		aprint_error(": couldn't enable module\n");
1231.1Sjmcneill		return;
1241.1Sjmcneill	}
1251.1Sjmcneill
1261.1Sjmcneill	fdt_add_bus(self, phandle, faa);
1271.1Sjmcneill}
128