xref: /netbsd/sys/dev/pci/voyager.c (revision 6da48fab)
1 /*	$NetBSD: voyager.c,v 1.14 2019/04/21 11:45:08 maya Exp $	*/
2 
3 /*
4  * Copyright (c) 2009, 2011 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: voyager.c,v 1.14 2019/04/21 11:45:08 maya Exp $");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/device.h>
35 #include <sys/malloc.h>
36 #include <sys/lwp.h>
37 #include <sys/kauth.h>
38 
39 #include <dev/pci/pcivar.h>
40 #include <dev/pci/pcireg.h>
41 #include <dev/pci/pcidevs.h>
42 #include <dev/pci/pciio.h>
43 #include <dev/ic/sm502reg.h>
44 #include <dev/i2c/i2cvar.h>
45 #include <dev/i2c/i2c_bitbang.h>
46 
47 #include <sys/evcnt.h>
48 #include <sys/bitops.h>
49 
50 #include <dev/pci/voyagervar.h>
51 
52 #include "opt_voyager.h"
53 #include "voyagerfb.h"
54 #include "pwmclock.h"
55 
56 #ifdef VOYAGER_DEBUG
57 #define DPRINTF aprint_normal
58 #else
59 #define DPRINTF while (0) printf
60 #endif
61 
62 /* interrupt stuff */
63 struct voyager_intr {
64 	int (*vih_func)(void *);
65 	void *vih_arg;
66 	struct evcnt vih_count;
67 	char vih_name[32];
68 };
69 
70 struct voyager_softc {
71 	device_t sc_dev;
72 
73 	pci_chipset_tag_t sc_pc;
74 	pcitag_t sc_pcitag;
75 
76 	bus_space_tag_t sc_memt;
77 	bus_space_tag_t sc_iot;
78 
79 	bus_space_handle_t sc_fbh, sc_regh;
80 	bus_addr_t sc_fb, sc_reg;
81 	bus_size_t sc_fbsize, sc_regsize;
82 
83 	struct i2c_controller sc_i2c;
84 	kmutex_t sc_i2c_lock;
85 
86 	/* interrupt dispatcher */
87 	void *sc_ih;
88 	struct voyager_intr sc_intrs[32];
89 };
90 
91 void *voyager_cookie = NULL;
92 
93 static int	voyager_match(device_t, cfdata_t, void *);
94 static void	voyager_attach(device_t, device_t, void *);
95 static int	voyager_print(void *, const char *);
96 static int	voyager_intr(void *);
97 
98 CFATTACH_DECL_NEW(voyager, sizeof(struct voyager_softc),
99     voyager_match, voyager_attach, NULL, NULL);
100 
101 /* I2C glue */
102 static int voyager_i2c_acquire_bus(void *, int);
103 static void voyager_i2c_release_bus(void *, int);
104 static int voyager_i2c_send_start(void *, int);
105 static int voyager_i2c_send_stop(void *, int);
106 static int voyager_i2c_initiate_xfer(void *, i2c_addr_t, int);
107 static int voyager_i2c_read_byte(void *, uint8_t *, int);
108 static int voyager_i2c_write_byte(void *, uint8_t, int);
109 
110 /* I2C bitbang glue */
111 static void voyager_i2cbb_set_bits(void *, uint32_t);
112 static void voyager_i2cbb_set_dir(void *, uint32_t);
113 static uint32_t voyager_i2cbb_read(void *);
114 
115 static const struct i2c_bitbang_ops voyager_i2cbb_ops = {
116 	voyager_i2cbb_set_bits,
117 	voyager_i2cbb_set_dir,
118 	voyager_i2cbb_read,
119 	{
120 		1 << 13,
121 		1 << 6,
122 		1 << 13,
123 		0
124 	}
125 };
126 #define GPIO_I2C_BITS ((1 << 6) | (1 << 13))
127 
128 #ifdef VOYAGER_DEBUG
129 static void voyager_print_pwm(struct voyager_softc *, int);
130 #endif
131 
132 static int
133 voyager_match(device_t parent, cfdata_t match, void *aux)
134 {
135 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
136 
137 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
138 		return 0;
139 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_SILMOTION)
140 		return 0;
141 
142 	/* only chip tested on so far - may need a list */
143 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SILMOTION_SM502)
144 		return 100;
145 	return (0);
146 }
147 
148 static void
149 voyager_attach(device_t parent, device_t self, void *aux)
150 {
151 	struct voyager_softc	*sc = device_private(self);
152 	struct pci_attach_args	*pa = aux;
153 	pci_intr_handle_t ih;
154 	struct voyager_attach_args vaa;
155 	struct i2cbus_attach_args iba;
156 	uint32_t reg;
157 	const char *intrstr;
158 	int i;
159 	char intrbuf[PCI_INTRSTR_LEN];
160 
161 	sc->sc_pc = pa->pa_pc;
162 	sc->sc_pcitag = pa->pa_tag;
163 	sc->sc_memt = pa->pa_memt;
164 	sc->sc_iot = pa->pa_iot;
165 	sc->sc_dev = self;
166 
167 	voyager_cookie = sc;
168 
169 	pci_aprint_devinfo(pa, NULL);
170 
171 	if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM, 0,
172 	    &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
173 		aprint_error("%s: failed to map registers.\n",
174 		    device_xname(sc->sc_dev));
175 	}
176 
177 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM,
178 	    BUS_SPACE_MAP_LINEAR,
179 	    &sc->sc_memt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
180 		aprint_error("%s: failed to map the frame buffer.\n",
181 		    device_xname(sc->sc_dev));
182 	}
183 
184 	/* disable all interrupts */
185 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK, 0);
186 
187 	/* initialize handler list */
188 	for (i = 0; i < 32; i++) {
189 		sc->sc_intrs[i].vih_func = NULL;
190 		snprintf(sc->sc_intrs[i].vih_name, 32, "int %d", i);
191 		evcnt_attach_dynamic(&sc->sc_intrs[i].vih_count,
192 		    EVCNT_TYPE_INTR, NULL, "voyager", sc->sc_intrs[i].vih_name);
193 	}
194 
195 	/* Map and establish the interrupt. */
196 	if (pci_intr_map(pa, &ih)) {
197 		aprint_error_dev(self, "couldn't map interrupt\n");
198 		return;
199 	}
200 
201 	intrstr = pci_intr_string(sc->sc_pc, ih, intrbuf, sizeof(intrbuf));
202 	sc->sc_ih = pci_intr_establish_xname(sc->sc_pc, ih, IPL_VM,
203 	    voyager_intr, NULL, /* so we get the clock frame instead */
204 	    device_xname(self));
205 	if (sc->sc_ih == NULL) {
206 		aprint_error_dev(self, "couldn't establish interrupt");
207 		if (intrstr != NULL)
208 			aprint_error(" at %s", intrstr);
209 		aprint_error("\n");
210 		return;
211 	}
212 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
213 
214 #ifdef VOYAGER_DEBUG
215 	voyager_print_pwm(sc, SM502_PWM0);
216 	voyager_print_pwm(sc, SM502_PWM1);
217 	voyager_print_pwm(sc, SM502_PWM2);
218 #endif
219 
220 	/* attach the framebuffer driver */
221 	vaa.vaa_memh = sc->sc_fbh;
222 	vaa.vaa_mem_pa = sc->sc_fb;
223 	vaa.vaa_regh = sc->sc_regh;
224 	vaa.vaa_reg_pa = sc->sc_reg;
225 	vaa.vaa_tag = sc->sc_memt;
226 	vaa.vaa_pc = sc->sc_pc;
227 	vaa.vaa_pcitag = sc->sc_pcitag;
228 #if NVOYAGERFB > 0
229 	strcpy(vaa.vaa_name, "voyagerfb");
230 	config_found_ia(sc->sc_dev, "voyagerbus", &vaa, voyager_print);
231 #endif
232 #if NPWMCLOCK > 0
233 	strcpy(vaa.vaa_name, "pwmclock");
234 	config_found_ia(sc->sc_dev, "voyagerbus", &vaa, voyager_print);
235 #endif
236 #ifdef notyet
237 	strcpy(vaa.vaa_name, "vac");
238 	config_found_ia(sc->sc_dev, "voyagerbus", &vaa, voyager_print);
239 #endif
240 	/* we use this mutex whether there's an i2c bus or not */
241 	mutex_init(&sc->sc_i2c_lock, MUTEX_DEFAULT, IPL_NONE);
242 
243 	/*
244 	 * see if the i2c pins are configured as gpio and if so, use them
245 	 * should probably be a compile time option
246 	 */
247 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO0_CONTROL);
248 	if ((reg & GPIO_I2C_BITS) == 0) {
249 		/* both bits as outputs */
250 		voyager_gpio_dir(sc, 0xffffffff, GPIO_I2C_BITS);
251 
252 		/* Fill in the i2c tag */
253 		memset(&sc->sc_i2c, 0, sizeof(sc->sc_i2c));
254 		memset(&iba, 0, sizeof(iba));
255 		sc->sc_i2c.ic_cookie = sc;
256 		sc->sc_i2c.ic_acquire_bus = voyager_i2c_acquire_bus;
257 		sc->sc_i2c.ic_release_bus = voyager_i2c_release_bus;
258 		sc->sc_i2c.ic_send_start = voyager_i2c_send_start;
259 		sc->sc_i2c.ic_send_stop = voyager_i2c_send_stop;
260 		sc->sc_i2c.ic_initiate_xfer = voyager_i2c_initiate_xfer;
261 		sc->sc_i2c.ic_read_byte = voyager_i2c_read_byte;
262 		sc->sc_i2c.ic_write_byte = voyager_i2c_write_byte;
263 		sc->sc_i2c.ic_exec = NULL;
264 		iba.iba_tag = &sc->sc_i2c;
265 		config_found_ia(self, "i2cbus", &iba, iicbus_print);
266 	}
267 	voyager_control_gpio(sc, ~(1 << 16), 0);
268 	voyager_gpio_dir(sc, 0xffffffff, 1 << 16);
269 	voyager_write_gpio(sc, 0xffffffff, 1 << 16);
270 }
271 
272 static int
273 voyager_print(void *aux, const char *what)
274 {
275 	/*struct voyager_attach_args *vaa = aux;*/
276 
277 	if (what == NULL)
278 		return 0;
279 
280 	printf("%s:", what);
281 
282 	return 0;
283 }
284 
285 static void
286 voyager_i2cbb_set_bits(void *cookie, uint32_t bits)
287 {
288 	struct voyager_softc *sc = cookie;
289 	uint32_t reg;
290 
291 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0);
292 	reg &= ~GPIO_I2C_BITS;
293 	reg |= bits;
294 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0, reg);
295 }
296 
297 static void
298 voyager_i2cbb_set_dir(void *cookie, uint32_t bits)
299 {
300 	struct voyager_softc *sc = cookie;
301 	uint32_t reg;
302 
303 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DIR0);
304 	reg &= ~(1 << 13);
305 	reg |= bits;
306 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DIR0, reg);
307 }
308 
309 static uint32_t
310 voyager_i2cbb_read(void *cookie)
311 {
312 	struct voyager_softc *sc = cookie;
313 	uint32_t reg;
314 
315 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0);
316 	return reg;
317 }
318 
319 /* higher level I2C stuff */
320 static int
321 voyager_i2c_acquire_bus(void *cookie, int flags)
322 {
323 	struct voyager_softc *sc = cookie;
324 
325 	mutex_enter(&sc->sc_i2c_lock);
326 	return 0;
327 }
328 
329 static void
330 voyager_i2c_release_bus(void *cookie, int flags)
331 {
332 	struct voyager_softc *sc = cookie;
333 
334 	mutex_exit(&sc->sc_i2c_lock);
335 }
336 
337 static int
338 voyager_i2c_send_start(void *cookie, int flags)
339 {
340 	return (i2c_bitbang_send_start(cookie, flags, &voyager_i2cbb_ops));
341 }
342 
343 static int
344 voyager_i2c_send_stop(void *cookie, int flags)
345 {
346 
347 	return (i2c_bitbang_send_stop(cookie, flags, &voyager_i2cbb_ops));
348 }
349 
350 static int
351 voyager_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
352 {
353 	/*
354 	 * for some reason i2c_bitbang_initiate_xfer left-shifts
355 	 * the I2C-address and then sets the direction bit
356 	 */
357 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags,
358 	    &voyager_i2cbb_ops));
359 }
360 
361 static int
362 voyager_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
363 {
364 	int ret;
365 
366 	ret = i2c_bitbang_read_byte(cookie, valp, flags, &voyager_i2cbb_ops);
367 	return ret;
368 }
369 
370 static int
371 voyager_i2c_write_byte(void *cookie, uint8_t val, int flags)
372 {
373 	int ret;
374 
375 	ret = i2c_bitbang_write_byte(cookie, val, flags, &voyager_i2cbb_ops);
376 	delay(500);
377 	return ret;
378 }
379 
380 void
381 voyager_twiddle_bits(void *cookie, int regnum, uint32_t mask, uint32_t bits)
382 {
383 	struct voyager_softc *sc = cookie;
384 	uint32_t reg;
385 
386 	/* don't interfere with i2c ops */
387 	mutex_enter(&sc->sc_i2c_lock);
388 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, regnum);
389 	reg &= mask;
390 	reg |= bits;
391 	bus_space_write_4(sc->sc_memt, sc->sc_regh, regnum, reg);
392 	mutex_exit(&sc->sc_i2c_lock);
393 }
394 
395 static int
396 voyager_intr(void *cookie)
397 {
398 	struct voyager_softc *sc = voyager_cookie;
399 	struct voyager_intr *ih;
400 	uint32_t intrs;
401 	uint32_t mask, bit;
402 	int num;
403 
404 	intrs = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_STATUS);
405 	mask = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK);
406 	intrs &= mask;
407 
408 	while (intrs != 0) {
409 		num = ffs32(intrs) - 1;
410 		bit = 1 << num;
411 		intrs &= ~bit;
412 		ih = &sc->sc_intrs[num];
413 		if (ih->vih_func != NULL) {
414 			if (ih->vih_arg == NULL) {
415 				ih->vih_func(cookie);
416 			} else {
417 				ih->vih_func(ih->vih_arg);
418 			}
419 		}
420 		ih->vih_count.ev_count++;
421 	}
422 	return 0;
423 }
424 
425 void *
426 voyager_establish_intr(device_t dev, int bit, int (*handler)(void *), void *arg)
427 {
428 	struct voyager_softc *sc = device_private(dev);
429 	struct voyager_intr *ih;
430 	uint32_t reg;
431 
432 	if ((bit < 0) || (bit > 31)) {
433 		aprint_error_dev(dev, "bogus interrupt %d\n", bit);
434 		return NULL;
435 	}
436 
437 	ih = &sc->sc_intrs[bit];
438 	if (ih->vih_func != NULL) {
439 		aprint_error_dev(dev, "interrupt %d is already in use\n", bit);
440 		return NULL;
441 	}
442 	ih->vih_func = handler;
443 	ih->vih_arg = arg;
444 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK);
445 	reg |= 1 << bit;
446 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK, reg);
447 
448 	return (void *)(uintptr_t)(0x80000000 | bit);
449 }
450 
451 void
452 voyager_disestablish_intr(device_t dev, void *ih)
453 {
454 }
455 
456 /* timer */
457 #ifdef VOYAGER_DEBUG
458 static void
459 voyager_print_pwm(struct voyager_softc *sc, int pwmreg)
460 {
461 	uint32_t reg;
462 
463 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, pwmreg);
464 	aprint_debug_dev(sc->sc_dev, "%08x: %08x = %d Hz, %d high, %d low\n",
465 	    pwmreg, reg,
466 	    96000000 / (1 << ((reg & SM502_PWM_CLOCK_DIV_MASK) >> SM502_PWM_CLOCK_DIV_SHIFT)),
467 	    (reg & SM502_PWM_CLOCK_HIGH_MASK) >> SM502_PWM_CLOCK_HIGH_SHIFT,
468 	    (reg & SM502_PWM_CLOCK_LOW_MASK) >> SM502_PWM_CLOCK_LOW_SHIFT);
469 }
470 #endif
471 
472 uint32_t
473 voyager_set_pwm(int freq, int duty_cycle)
474 {
475 	int ifreq, factor, bit, steps;
476 	uint32_t reg = 0, hi, lo;
477 
478 	/*
479 	 * find the smallest divider that gets us within 4096 steps of the
480 	 * target frequency
481 	 */
482 	ifreq = freq * 4096;
483 	factor = 96000000 / ifreq;
484 	bit = fls32(factor);
485 	factor = 1 << bit;
486 	steps = 96000000 / (factor * freq);
487 	/* can't have it all off */
488 	if (duty_cycle < 1)
489 		duty_cycle = 1;
490 	/* can't be always on either */
491 	if (duty_cycle > 999)
492 		duty_cycle = 999;
493 	hi = steps * duty_cycle / 1000;
494 	if (hi < 1)
495 		hi = 1;
496 	lo = steps - hi;
497 	if (lo < 1) {
498 		hi = steps - 1;
499 		lo = 1;
500 	}
501 	DPRINTF("%d hz -> %d, %d, %d / %d\n", freq, factor, steps, lo, hi);
502 	reg = ((hi - 1) & 0xfff) << SM502_PWM_CLOCK_HIGH_SHIFT;
503 	reg |= ((lo - 1) & 0xfff) << SM502_PWM_CLOCK_LOW_SHIFT;
504 	reg |= (bit & 0xf) << SM502_PWM_CLOCK_DIV_SHIFT;
505 	DPRINTF("reg: %08x\n", reg);
506 	return reg;
507 }
508