tcu.c revision 1.2
11.2Schristos/* $NetBSD: tcu.c,v 1.2 2016/09/13 16:54:26 christos Exp $ */
21.1Schristos
31.1Schristos/*-
41.1Schristos * Copyright (c) 2016, Felix Deichmann
51.1Schristos * All rights reserved.
61.1Schristos *
71.1Schristos * Redistribution and use in source and binary forms, with or without
81.1Schristos * modification, are permitted provided that the following conditions
91.1Schristos * are met:
101.1Schristos * 1. Redistributions of source code must retain the above copyright
111.1Schristos *    notice, this list of conditions and the following disclaimer.
121.1Schristos * 2. Redistributions in binary form must reproduce the above copyright
131.1Schristos *    notice, this list of conditions and the following disclaimer in the
141.1Schristos *    documentation and/or other materials provided with the distribution.
151.1Schristos *
161.1Schristos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171.1Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181.1Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191.1Schristos * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201.1Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211.1Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221.1Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231.1Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241.1Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251.1Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261.1Schristos * POSSIBILITY OF SUCH DAMAGE.
271.1Schristos */
281.1Schristos
291.1Schristos/*
301.1Schristos * flxd TC-USB - TURBOchannel USB host option
311.1Schristos */
321.1Schristos
331.1Schristos#include <sys/cdefs.h>
341.2Schristos__KERNEL_RCSID(0, "$NetBSD: tcu.c,v 1.2 2016/09/13 16:54:26 christos Exp $");
351.1Schristos
361.1Schristos#include <sys/param.h>
371.1Schristos#include <sys/systm.h>
381.1Schristos#include <sys/device.h>
391.1Schristos#include <sys/gpio.h>
401.1Schristos
411.1Schristos#include <sys/bus.h>
421.1Schristos
431.1Schristos#include <dev/tc/tcvar.h>
441.1Schristos
451.1Schristos#include <dev/gpio/gpiovar.h>
461.1Schristos
471.1Schristos#include "gpio.h"
481.1Schristos#include "slhci_tcu.h"
491.1Schristos
501.1Schristos#define TCU_GPIO_NPINS	8
511.2Schristos
521.2Schristos#define TCU_CPLD_OFFS	0x80
531.2Schristos#define TCU_CPLD_SIZE	(4 * 4)
541.2Schristos
551.2Schristos#define TCU_CFG		0x0
561.2Schristos#define   TCU_CFG_RUN	__BIT(7)	/* write-only */
571.2Schristos#define TCU_GPIO_DIR	0x4
581.2Schristos#define TCU_GPIO_IN	0x8
591.2Schristos#define TCU_GPIO_OUT	0xc
601.1Schristos
611.1Schristosstruct tcu_softc {
621.1Schristos#if NGPIO > 0
631.1Schristos	kmutex_t		sc_gpio_mtx;
641.1Schristos	struct gpio_chipset_tag	sc_gpio_gc;
651.1Schristos	gpio_pin_t		sc_gpio_pins[TCU_GPIO_NPINS];
661.1Schristos	bus_space_tag_t		sc_gpio_iot;
671.1Schristos	bus_space_handle_t	sc_gpio_ioh;
681.1Schristos#endif /* NGPIO > 0 */
691.1Schristos};
701.1Schristos
711.1Schristosstatic int  tcu_match(device_t, cfdata_t, void *);
721.1Schristosstatic void tcu_attach(device_t, device_t, void *);
731.1Schristos
741.1Schristos#if NSLHCI_TCU > 0
751.1Schristosstatic int  tcu_print(void *, const char *);
761.1Schristos#endif /* NSLHCI_TCU > 0 */
771.1Schristos#if NGPIO > 0
781.1Schristosstatic void tcu_gpio_attach(device_t, device_t, void *);
791.1Schristosstatic int  tcu_gpio_read(void *, int);
801.1Schristosstatic void tcu_gpio_write(void *, int, int);
811.1Schristosstatic void tcu_gpio_ctl(void *, int, int);
821.1Schristos#endif /* NGPIO > 0 */
831.1Schristos
841.1SchristosCFATTACH_DECL_NEW(tcu, sizeof(struct tcu_softc),
851.1Schristos    tcu_match, tcu_attach, NULL, NULL);
861.1Schristos
871.1Schristosstatic int
881.1Schristostcu_match(device_t parent, cfdata_t cf, void *aux)
891.1Schristos{
901.1Schristos	struct tc_attach_args *ta = aux;
911.1Schristos
921.1Schristos	if (strncmp("TC-USB  ", ta->ta_modname, TC_ROM_LLEN))
931.1Schristos		return 0;
941.1Schristos
951.1Schristos	return 1;
961.1Schristos}
971.1Schristos
981.1Schristosstatic void
991.1Schristostcu_attach(device_t parent, device_t self, void *aux)
1001.1Schristos{
1011.2Schristos	struct tc_attach_args *ta = aux;
1021.2Schristos	bus_space_tag_t iot = ta->ta_memt;
1031.2Schristos	bus_space_handle_t ioh;
1041.2Schristos	int error;
1051.2Schristos	uint8_t cfg;
1061.2Schristos	char buf[30];
1071.1Schristos
1081.1Schristos	printf(": TC-USB\n");
1091.2Schristos
1101.2Schristos	error = bus_space_map(iot, ta->ta_addr + TCU_CPLD_OFFS, TCU_CPLD_SIZE,
1111.2Schristos	    0, &ioh);
1121.2Schristos	if (error) {
1131.2Schristos		aprint_error_dev(self, "bus_space_map() failed (%d)\n", error);
1141.2Schristos		return;
1151.2Schristos	}
1161.2Schristos
1171.2Schristos	/*
1181.2Schristos	 * Force reset in case system didn't. SL811 reset pulse and hold time
1191.2Schristos	 * must be min. 16 clocks long (at 48 MHz clock) each.
1201.2Schristos	 */
1211.2Schristos	bus_space_write_1(iot, ioh, TCU_CFG, 0);
1221.2Schristos	DELAY(1000);
1231.2Schristos	bus_space_write_1(iot, ioh, TCU_CFG, TCU_CFG_RUN);
1241.2Schristos	DELAY(1000);
1251.2Schristos
1261.2Schristos	cfg = bus_space_read_1(iot, ioh, TCU_CFG);
1271.2Schristos
1281.2Schristos	bus_space_unmap(iot, ioh, TCU_CPLD_SIZE);
1291.2Schristos
1301.2Schristos	/* Display DIP switch configuration. */
1311.2Schristos	(void)snprintb(buf, sizeof(buf),
1321.2Schristos	    "\177\020"
1331.2Schristos	    "b\3S1-1\0"
1341.2Schristos	    "b\2S1-2\0"
1351.2Schristos	    "b\1S1-3\0"
1361.2Schristos	    "b\0S1-4\0"
1371.2Schristos	    "\0", cfg);
1381.2Schristos	aprint_normal_dev(self, "config %s\n", buf);
1391.2Schristos
1401.1Schristos#if NSLHCI_TCU > 0
1411.1Schristos	/* Attach slhci. */
1421.1Schristos	(void)config_found_ia(self, "tcu", aux, tcu_print);
1431.1Schristos#endif /* NSLHCI_TCU > 0 */
1441.1Schristos#if NGPIO > 0
1451.1Schristos	/* Attach gpio(bus). */
1461.1Schristos	tcu_gpio_attach(parent, self, aux);
1471.1Schristos#endif /* NGPIO > 0 */
1481.1Schristos}
1491.1Schristos
1501.1Schristos#if NSLHCI_TCU > 0
1511.1Schristosstatic int
1521.1Schristostcu_print(void *aux, const char *pnp)
1531.1Schristos{
1541.1Schristos
1551.1Schristos	/* This function is only used for "slhci at tcu". */
1561.1Schristos	if (pnp)
1571.1Schristos		aprint_normal("slhci at %s", pnp);
1581.1Schristos
1591.1Schristos	return UNCONF;
1601.1Schristos}
1611.1Schristos#endif /* NSLHCI_TCU > 0 */
1621.1Schristos
1631.1Schristos#if NGPIO > 0
1641.1Schristosstatic void
1651.1Schristostcu_gpio_attach(device_t parent, device_t self, void *aux)
1661.1Schristos{
1671.1Schristos	struct tcu_softc *sc = device_private(self);
1681.1Schristos	struct tc_attach_args *ta = aux;
1691.1Schristos	struct gpiobus_attach_args gba;
1701.1Schristos	bus_space_tag_t iot = ta->ta_memt;
1711.1Schristos	uint32_t v;
1721.1Schristos	int error;
1731.1Schristos
1741.1Schristos	sc->sc_gpio_iot = iot;
1751.1Schristos
1761.2Schristos	error = bus_space_map(iot, ta->ta_addr + TCU_CPLD_OFFS, TCU_CPLD_SIZE,
1771.1Schristos	    0, &sc->sc_gpio_ioh);
1781.1Schristos	if (error) {
1791.1Schristos		aprint_error_dev(self, "bus_space_map() failed (%d)\n", error);
1801.1Schristos		return;
1811.1Schristos	}
1821.1Schristos
1831.1Schristos	mutex_init(&sc->sc_gpio_mtx, MUTEX_DEFAULT, IPL_NONE);
1841.1Schristos
1851.1Schristos	v = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, TCU_GPIO_DIR);
1861.1Schristos
1871.1Schristos	for (int pin = 0; pin < TCU_GPIO_NPINS; pin++) {
1881.1Schristos		sc->sc_gpio_pins[pin].pin_num = pin;
1891.1Schristos		sc->sc_gpio_pins[pin].pin_caps = GPIO_PIN_INPUT |
1901.1Schristos		    GPIO_PIN_OUTPUT;
1911.1Schristos		sc->sc_gpio_pins[pin].pin_flags = (v & (UINT32_C(1) << pin)) ?
1921.1Schristos		    GPIO_PIN_OUTPUT : GPIO_PIN_INPUT;
1931.1Schristos		sc->sc_gpio_pins[pin].pin_state = tcu_gpio_read(sc, pin);
1941.1Schristos	}
1951.1Schristos
1961.1Schristos	sc->sc_gpio_gc.gp_cookie = sc;
1971.1Schristos	sc->sc_gpio_gc.gp_pin_read = tcu_gpio_read;
1981.1Schristos	sc->sc_gpio_gc.gp_pin_write = tcu_gpio_write;
1991.1Schristos	sc->sc_gpio_gc.gp_pin_ctl = tcu_gpio_ctl;
2001.1Schristos
2011.1Schristos	memset(&gba, 0, sizeof(gba));
2021.1Schristos
2031.1Schristos	gba.gba_gc = &sc->sc_gpio_gc;
2041.1Schristos	gba.gba_pins = sc->sc_gpio_pins;
2051.1Schristos	gba.gba_npins = TCU_GPIO_NPINS;
2061.1Schristos
2071.1Schristos	(void)config_found_ia(self, "gpiobus", &gba, gpiobus_print);
2081.1Schristos}
2091.1Schristos
2101.1Schristosstatic int
2111.1Schristostcu_gpio_read(void *arg, int pin)
2121.1Schristos{
2131.1Schristos	struct tcu_softc *sc = arg;
2141.1Schristos	uint32_t v;
2151.1Schristos
2161.1Schristos	mutex_enter(&sc->sc_gpio_mtx);
2171.1Schristos	v = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, TCU_GPIO_IN);
2181.1Schristos	mutex_exit(&sc->sc_gpio_mtx);
2191.1Schristos
2201.1Schristos	return (v & (UINT32_C(1) << pin)) ? GPIO_PIN_HIGH : GPIO_PIN_LOW;
2211.1Schristos}
2221.1Schristos
2231.1Schristosstatic void
2241.1Schristostcu_gpio_write(void *arg, int pin, int val)
2251.1Schristos{
2261.1Schristos	struct tcu_softc *sc = arg;
2271.1Schristos	uint32_t v;
2281.1Schristos
2291.1Schristos	mutex_enter(&sc->sc_gpio_mtx);
2301.1Schristos
2311.1Schristos	v = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, TCU_GPIO_OUT);
2321.1Schristos
2331.1Schristos	if (val == GPIO_PIN_LOW)
2341.1Schristos		v &= ~(UINT32_C(1) << pin);
2351.1Schristos	else if (val == GPIO_PIN_HIGH)
2361.1Schristos		v |= (UINT32_C(1) << pin);
2371.1Schristos
2381.1Schristos	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, TCU_GPIO_OUT, v);
2391.1Schristos
2401.1Schristos	mutex_exit(&sc->sc_gpio_mtx);
2411.1Schristos}
2421.1Schristos
2431.1Schristosstatic void
2441.1Schristostcu_gpio_ctl(void *arg, int pin, int flags)
2451.1Schristos{
2461.1Schristos	struct tcu_softc *sc = arg;
2471.1Schristos	uint32_t v;
2481.1Schristos
2491.1Schristos	mutex_enter(&sc->sc_gpio_mtx);
2501.1Schristos
2511.1Schristos	v = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, TCU_GPIO_DIR);
2521.1Schristos
2531.1Schristos	if (flags & GPIO_PIN_INPUT)
2541.1Schristos		v &= ~(UINT32_C(1) << pin);
2551.1Schristos	if (flags & GPIO_PIN_OUTPUT)
2561.1Schristos		v |= (UINT32_C(1) << pin);
2571.1Schristos
2581.1Schristos	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, TCU_GPIO_DIR, v);
2591.1Schristos
2601.1Schristos	mutex_exit(&sc->sc_gpio_mtx);
2611.1Schristos}
2621.1Schristos#endif /* NGPIO > 0 */
263