xref: /netbsd-src/sys/dev/tc/tcu.c (revision f21b7d7f2cbdd5c14b3882c4e8a3d43580d460a6)
1 /* $NetBSD: tcu.c,v 1.2 2016/09/13 16:54:26 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2016, Felix Deichmann
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 COPYRIGHT HOLDERS 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 /*
30  * flxd TC-USB - TURBOchannel USB host option
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: tcu.c,v 1.2 2016/09/13 16:54:26 christos Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/gpio.h>
40 
41 #include <sys/bus.h>
42 
43 #include <dev/tc/tcvar.h>
44 
45 #include <dev/gpio/gpiovar.h>
46 
47 #include "gpio.h"
48 #include "slhci_tcu.h"
49 
50 #define TCU_GPIO_NPINS	8
51 
52 #define TCU_CPLD_OFFS	0x80
53 #define TCU_CPLD_SIZE	(4 * 4)
54 
55 #define TCU_CFG		0x0
56 #define   TCU_CFG_RUN	__BIT(7)	/* write-only */
57 #define TCU_GPIO_DIR	0x4
58 #define TCU_GPIO_IN	0x8
59 #define TCU_GPIO_OUT	0xc
60 
61 struct tcu_softc {
62 #if NGPIO > 0
63 	kmutex_t		sc_gpio_mtx;
64 	struct gpio_chipset_tag	sc_gpio_gc;
65 	gpio_pin_t		sc_gpio_pins[TCU_GPIO_NPINS];
66 	bus_space_tag_t		sc_gpio_iot;
67 	bus_space_handle_t	sc_gpio_ioh;
68 #endif /* NGPIO > 0 */
69 };
70 
71 static int  tcu_match(device_t, cfdata_t, void *);
72 static void tcu_attach(device_t, device_t, void *);
73 
74 #if NSLHCI_TCU > 0
75 static int  tcu_print(void *, const char *);
76 #endif /* NSLHCI_TCU > 0 */
77 #if NGPIO > 0
78 static void tcu_gpio_attach(device_t, device_t, void *);
79 static int  tcu_gpio_read(void *, int);
80 static void tcu_gpio_write(void *, int, int);
81 static void tcu_gpio_ctl(void *, int, int);
82 #endif /* NGPIO > 0 */
83 
84 CFATTACH_DECL_NEW(tcu, sizeof(struct tcu_softc),
85     tcu_match, tcu_attach, NULL, NULL);
86 
87 static int
88 tcu_match(device_t parent, cfdata_t cf, void *aux)
89 {
90 	struct tc_attach_args *ta = aux;
91 
92 	if (strncmp("TC-USB  ", ta->ta_modname, TC_ROM_LLEN))
93 		return 0;
94 
95 	return 1;
96 }
97 
98 static void
99 tcu_attach(device_t parent, device_t self, void *aux)
100 {
101 	struct tc_attach_args *ta = aux;
102 	bus_space_tag_t iot = ta->ta_memt;
103 	bus_space_handle_t ioh;
104 	int error;
105 	uint8_t cfg;
106 	char buf[30];
107 
108 	printf(": TC-USB\n");
109 
110 	error = bus_space_map(iot, ta->ta_addr + TCU_CPLD_OFFS, TCU_CPLD_SIZE,
111 	    0, &ioh);
112 	if (error) {
113 		aprint_error_dev(self, "bus_space_map() failed (%d)\n", error);
114 		return;
115 	}
116 
117 	/*
118 	 * Force reset in case system didn't. SL811 reset pulse and hold time
119 	 * must be min. 16 clocks long (at 48 MHz clock) each.
120 	 */
121 	bus_space_write_1(iot, ioh, TCU_CFG, 0);
122 	DELAY(1000);
123 	bus_space_write_1(iot, ioh, TCU_CFG, TCU_CFG_RUN);
124 	DELAY(1000);
125 
126 	cfg = bus_space_read_1(iot, ioh, TCU_CFG);
127 
128 	bus_space_unmap(iot, ioh, TCU_CPLD_SIZE);
129 
130 	/* Display DIP switch configuration. */
131 	(void)snprintb(buf, sizeof(buf),
132 	    "\177\020"
133 	    "b\3S1-1\0"
134 	    "b\2S1-2\0"
135 	    "b\1S1-3\0"
136 	    "b\0S1-4\0"
137 	    "\0", cfg);
138 	aprint_normal_dev(self, "config %s\n", buf);
139 
140 #if NSLHCI_TCU > 0
141 	/* Attach slhci. */
142 	(void)config_found_ia(self, "tcu", aux, tcu_print);
143 #endif /* NSLHCI_TCU > 0 */
144 #if NGPIO > 0
145 	/* Attach gpio(bus). */
146 	tcu_gpio_attach(parent, self, aux);
147 #endif /* NGPIO > 0 */
148 }
149 
150 #if NSLHCI_TCU > 0
151 static int
152 tcu_print(void *aux, const char *pnp)
153 {
154 
155 	/* This function is only used for "slhci at tcu". */
156 	if (pnp)
157 		aprint_normal("slhci at %s", pnp);
158 
159 	return UNCONF;
160 }
161 #endif /* NSLHCI_TCU > 0 */
162 
163 #if NGPIO > 0
164 static void
165 tcu_gpio_attach(device_t parent, device_t self, void *aux)
166 {
167 	struct tcu_softc *sc = device_private(self);
168 	struct tc_attach_args *ta = aux;
169 	struct gpiobus_attach_args gba;
170 	bus_space_tag_t iot = ta->ta_memt;
171 	uint32_t v;
172 	int error;
173 
174 	sc->sc_gpio_iot = iot;
175 
176 	error = bus_space_map(iot, ta->ta_addr + TCU_CPLD_OFFS, TCU_CPLD_SIZE,
177 	    0, &sc->sc_gpio_ioh);
178 	if (error) {
179 		aprint_error_dev(self, "bus_space_map() failed (%d)\n", error);
180 		return;
181 	}
182 
183 	mutex_init(&sc->sc_gpio_mtx, MUTEX_DEFAULT, IPL_NONE);
184 
185 	v = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, TCU_GPIO_DIR);
186 
187 	for (int pin = 0; pin < TCU_GPIO_NPINS; pin++) {
188 		sc->sc_gpio_pins[pin].pin_num = pin;
189 		sc->sc_gpio_pins[pin].pin_caps = GPIO_PIN_INPUT |
190 		    GPIO_PIN_OUTPUT;
191 		sc->sc_gpio_pins[pin].pin_flags = (v & (UINT32_C(1) << pin)) ?
192 		    GPIO_PIN_OUTPUT : GPIO_PIN_INPUT;
193 		sc->sc_gpio_pins[pin].pin_state = tcu_gpio_read(sc, pin);
194 	}
195 
196 	sc->sc_gpio_gc.gp_cookie = sc;
197 	sc->sc_gpio_gc.gp_pin_read = tcu_gpio_read;
198 	sc->sc_gpio_gc.gp_pin_write = tcu_gpio_write;
199 	sc->sc_gpio_gc.gp_pin_ctl = tcu_gpio_ctl;
200 
201 	memset(&gba, 0, sizeof(gba));
202 
203 	gba.gba_gc = &sc->sc_gpio_gc;
204 	gba.gba_pins = sc->sc_gpio_pins;
205 	gba.gba_npins = TCU_GPIO_NPINS;
206 
207 	(void)config_found_ia(self, "gpiobus", &gba, gpiobus_print);
208 }
209 
210 static int
211 tcu_gpio_read(void *arg, int pin)
212 {
213 	struct tcu_softc *sc = arg;
214 	uint32_t v;
215 
216 	mutex_enter(&sc->sc_gpio_mtx);
217 	v = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, TCU_GPIO_IN);
218 	mutex_exit(&sc->sc_gpio_mtx);
219 
220 	return (v & (UINT32_C(1) << pin)) ? GPIO_PIN_HIGH : GPIO_PIN_LOW;
221 }
222 
223 static void
224 tcu_gpio_write(void *arg, int pin, int val)
225 {
226 	struct tcu_softc *sc = arg;
227 	uint32_t v;
228 
229 	mutex_enter(&sc->sc_gpio_mtx);
230 
231 	v = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, TCU_GPIO_OUT);
232 
233 	if (val == GPIO_PIN_LOW)
234 		v &= ~(UINT32_C(1) << pin);
235 	else if (val == GPIO_PIN_HIGH)
236 		v |= (UINT32_C(1) << pin);
237 
238 	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, TCU_GPIO_OUT, v);
239 
240 	mutex_exit(&sc->sc_gpio_mtx);
241 }
242 
243 static void
244 tcu_gpio_ctl(void *arg, int pin, int flags)
245 {
246 	struct tcu_softc *sc = arg;
247 	uint32_t v;
248 
249 	mutex_enter(&sc->sc_gpio_mtx);
250 
251 	v = bus_space_read_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, TCU_GPIO_DIR);
252 
253 	if (flags & GPIO_PIN_INPUT)
254 		v &= ~(UINT32_C(1) << pin);
255 	if (flags & GPIO_PIN_OUTPUT)
256 		v |= (UINT32_C(1) << pin);
257 
258 	bus_space_write_4(sc->sc_gpio_iot, sc->sc_gpio_ioh, TCU_GPIO_DIR, v);
259 
260 	mutex_exit(&sc->sc_gpio_mtx);
261 }
262 #endif /* NGPIO > 0 */
263