xref: /netbsd-src/sys/dev/pci/voyager.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: voyager.c,v 1.10 2014/03/29 19:28:25 christos 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.10 2014/03/29 19:28:25 christos 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 static int	voyager_match(device_t, cfdata_t, void *);
92 static void	voyager_attach(device_t, device_t, void *);
93 static int	voyager_print(void *, const char *);
94 static int	voyager_intr(void *);
95 
96 CFATTACH_DECL_NEW(voyager, sizeof(struct voyager_softc),
97     voyager_match, voyager_attach, NULL, NULL);
98 
99 /* I2C glue */
100 static int voyager_i2c_acquire_bus(void *, int);
101 static void voyager_i2c_release_bus(void *, int);
102 static int voyager_i2c_send_start(void *, int);
103 static int voyager_i2c_send_stop(void *, int);
104 static int voyager_i2c_initiate_xfer(void *, i2c_addr_t, int);
105 static int voyager_i2c_read_byte(void *, uint8_t *, int);
106 static int voyager_i2c_write_byte(void *, uint8_t, int);
107 
108 /* I2C bitbang glue */
109 static void voyager_i2cbb_set_bits(void *, uint32_t);
110 static void voyager_i2cbb_set_dir(void *, uint32_t);
111 static uint32_t voyager_i2cbb_read(void *);
112 
113 static const struct i2c_bitbang_ops voyager_i2cbb_ops = {
114 	voyager_i2cbb_set_bits,
115 	voyager_i2cbb_set_dir,
116 	voyager_i2cbb_read,
117 	{
118 		1 << 13,
119 		1 << 6,
120 		1 << 13,
121 		0
122 	}
123 };
124 #define GPIO_I2C_BITS ((1 << 6) | (1 << 13))
125 
126 #ifdef VOYAGER_DEBUG
127 static void voyager_print_pwm(struct voyager_softc *, int);
128 #endif
129 
130 static int
131 voyager_match(device_t parent, cfdata_t match, void *aux)
132 {
133 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
134 
135 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
136 		return 0;
137 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_SILMOTION)
138 		return 0;
139 
140 	/* only chip tested on so far - may need a list */
141 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SILMOTION_SM502)
142 		return 100;
143 	return (0);
144 }
145 
146 static void
147 voyager_attach(device_t parent, device_t self, void *aux)
148 {
149 	struct voyager_softc	*sc = device_private(self);
150 	struct pci_attach_args	*pa = aux;
151 	pci_intr_handle_t ih;
152 	struct voyager_attach_args vaa;
153 	struct i2cbus_attach_args iba;
154 	uint32_t reg;
155 	const char *intrstr;
156 	int i;
157 	char intrbuf[PCI_INTRSTR_LEN];
158 
159 	sc->sc_pc = pa->pa_pc;
160 	sc->sc_pcitag = pa->pa_tag;
161 	sc->sc_memt = pa->pa_memt;
162 	sc->sc_iot = pa->pa_iot;
163 	sc->sc_dev = self;
164 
165 	pci_aprint_devinfo(pa, NULL);
166 
167 	if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM, 0,
168 	    &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
169 		aprint_error("%s: failed to map registers.\n",
170 		    device_xname(sc->sc_dev));
171 	}
172 
173 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM,
174 	    BUS_SPACE_MAP_LINEAR,
175 	    &sc->sc_memt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
176 		aprint_error("%s: failed to map the frame buffer.\n",
177 		    device_xname(sc->sc_dev));
178 	}
179 
180 	/* disable all interrupts */
181 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK, 0);
182 
183 	/* initialize handler list */
184 	for (i = 0; i < 32; i++) {
185 		sc->sc_intrs[i].vih_func = NULL;
186 		snprintf(sc->sc_intrs[i].vih_name, 32, "int %d", i);
187 		evcnt_attach_dynamic(&sc->sc_intrs[i].vih_count,
188 		    EVCNT_TYPE_INTR, NULL, "voyager", sc->sc_intrs[i].vih_name);
189 	}
190 
191 	/* Map and establish the interrupt. */
192 	if (pci_intr_map(pa, &ih)) {
193 		aprint_error_dev(self, "couldn't map interrupt\n");
194 		return;
195 	}
196 
197 	intrstr = pci_intr_string(sc->sc_pc, ih, intrbuf, sizeof(intrbuf));
198 	sc->sc_ih = pci_intr_establish(sc->sc_pc, ih, IPL_AUDIO, voyager_intr, sc);
199 	if (sc->sc_ih == NULL) {
200 		aprint_error_dev(self, "couldn't establish interrupt");
201 		if (intrstr != NULL)
202 			aprint_error(" at %s", intrstr);
203 		aprint_error("\n");
204 		return;
205 	}
206 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
207 
208 #ifdef VOYAGER_DEBUG
209 	voyager_print_pwm(sc, SM502_PWM0);
210 	voyager_print_pwm(sc, SM502_PWM1);
211 	voyager_print_pwm(sc, SM502_PWM2);
212 #endif
213 
214 	/* attach the framebuffer driver */
215 	vaa.vaa_memh = sc->sc_fbh;
216 	vaa.vaa_mem_pa = sc->sc_fb;
217 	vaa.vaa_regh = sc->sc_regh;
218 	vaa.vaa_reg_pa = sc->sc_reg;
219 	vaa.vaa_tag = sc->sc_memt;
220 	vaa.vaa_pc = sc->sc_pc;
221 	vaa.vaa_pcitag = sc->sc_pcitag;
222 #if NVOYAGERFB > 0
223 	strcpy(vaa.vaa_name, "voyagerfb");
224 	config_found_ia(sc->sc_dev, "voyagerbus", &vaa, voyager_print);
225 #endif
226 #if NPWMCLOCK > 0
227 	strcpy(vaa.vaa_name, "pwmclock");
228 	config_found_ia(sc->sc_dev, "voyagerbus", &vaa, voyager_print);
229 #endif
230 #ifdef notyet
231 	strcpy(vaa.vaa_name, "vac");
232 	config_found_ia(sc->sc_dev, "voyagerbus", &vaa, voyager_print);
233 #endif
234 	/* we use this mutex wether there's an i2c bus or not */
235 	mutex_init(&sc->sc_i2c_lock, MUTEX_DEFAULT, IPL_NONE);
236 
237 	/*
238 	 * see if the i2c pins are configured as gpio and if so, use them
239 	 * should probably be a compile time option
240 	 */
241 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO0_CONTROL);
242 	if ((reg & GPIO_I2C_BITS) == 0) {
243 		/* both bits as outputs */
244 		voyager_gpio_dir(sc, 0xffffffff, GPIO_I2C_BITS);
245 
246 		/* Fill in the i2c tag */
247 		sc->sc_i2c.ic_cookie = sc;
248 		sc->sc_i2c.ic_acquire_bus = voyager_i2c_acquire_bus;
249 		sc->sc_i2c.ic_release_bus = voyager_i2c_release_bus;
250 		sc->sc_i2c.ic_send_start = voyager_i2c_send_start;
251 		sc->sc_i2c.ic_send_stop = voyager_i2c_send_stop;
252 		sc->sc_i2c.ic_initiate_xfer = voyager_i2c_initiate_xfer;
253 		sc->sc_i2c.ic_read_byte = voyager_i2c_read_byte;
254 		sc->sc_i2c.ic_write_byte = voyager_i2c_write_byte;
255 		sc->sc_i2c.ic_exec = NULL;
256 		iba.iba_tag = &sc->sc_i2c;
257 		config_found_ia(self, "i2cbus", &iba, iicbus_print);
258 	}
259 	voyager_control_gpio(sc, ~(1 << 16), 0);
260 	voyager_gpio_dir(sc, 0xffffffff, 1 << 16);
261 	voyager_write_gpio(sc, 0xffffffff, 1 << 16);
262 }
263 
264 static int
265 voyager_print(void *aux, const char *what)
266 {
267 	/*struct voyager_attach_args *vaa = aux;*/
268 
269 	if (what == NULL)
270 		return 0;
271 
272 	printf("%s:", what);
273 
274 	return 0;
275 }
276 
277 static void
278 voyager_i2cbb_set_bits(void *cookie, uint32_t bits)
279 {
280 	struct voyager_softc *sc = cookie;
281 	uint32_t reg;
282 
283 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0);
284 	reg &= ~GPIO_I2C_BITS;
285 	reg |= bits;
286 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0, reg);
287 }
288 
289 static void
290 voyager_i2cbb_set_dir(void *cookie, uint32_t bits)
291 {
292 	struct voyager_softc *sc = cookie;
293 	uint32_t reg;
294 
295 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DIR0);
296 	reg &= ~(1 << 13);
297 	reg |= bits;
298 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DIR0, reg);
299 }
300 
301 static uint32_t
302 voyager_i2cbb_read(void *cookie)
303 {
304 	struct voyager_softc *sc = cookie;
305 	uint32_t reg;
306 
307 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0);
308 	return reg;
309 }
310 
311 /* higher level I2C stuff */
312 static int
313 voyager_i2c_acquire_bus(void *cookie, int flags)
314 {
315 	struct voyager_softc *sc = cookie;
316 
317 	mutex_enter(&sc->sc_i2c_lock);
318 	return 0;
319 }
320 
321 static void
322 voyager_i2c_release_bus(void *cookie, int flags)
323 {
324 	struct voyager_softc *sc = cookie;
325 
326 	mutex_exit(&sc->sc_i2c_lock);
327 }
328 
329 static int
330 voyager_i2c_send_start(void *cookie, int flags)
331 {
332 	return (i2c_bitbang_send_start(cookie, flags, &voyager_i2cbb_ops));
333 }
334 
335 static int
336 voyager_i2c_send_stop(void *cookie, int flags)
337 {
338 
339 	return (i2c_bitbang_send_stop(cookie, flags, &voyager_i2cbb_ops));
340 }
341 
342 static int
343 voyager_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
344 {
345 	/*
346 	 * for some reason i2c_bitbang_initiate_xfer left-shifts
347 	 * the I2C-address and then sets the direction bit
348 	 */
349 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags,
350 	    &voyager_i2cbb_ops));
351 }
352 
353 static int
354 voyager_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
355 {
356 	int ret;
357 
358 	ret = i2c_bitbang_read_byte(cookie, valp, flags, &voyager_i2cbb_ops);
359 	return ret;
360 }
361 
362 static int
363 voyager_i2c_write_byte(void *cookie, uint8_t val, int flags)
364 {
365 	int ret;
366 
367 	ret = i2c_bitbang_write_byte(cookie, val, flags, &voyager_i2cbb_ops);
368 	delay(500);
369 	return ret;
370 }
371 
372 void
373 voyager_twiddle_bits(void *cookie, int regnum, uint32_t mask, uint32_t bits)
374 {
375 	struct voyager_softc *sc = cookie;
376 	uint32_t reg;
377 
378 	/* don't interfere with i2c ops */
379 	mutex_enter(&sc->sc_i2c_lock);
380 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, regnum);
381 	reg &= mask;
382 	reg |= bits;
383 	bus_space_write_4(sc->sc_memt, sc->sc_regh, regnum, reg);
384 	mutex_exit(&sc->sc_i2c_lock);
385 }
386 
387 static int
388 voyager_intr(void *cookie)
389 {
390 	struct voyager_softc *sc = cookie;
391 	struct voyager_intr *ih;
392 	uint32_t intrs;
393 	uint32_t mask, bit;
394 	int num;
395 
396 	intrs = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_STATUS);
397 	mask = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK);
398 	intrs &= mask;
399 
400 	while (intrs != 0) {
401 		num = ffs32(intrs) - 1;
402 		bit = 1 << num;
403 		intrs &= ~bit;
404 		ih = &sc->sc_intrs[num];
405 		if (ih->vih_func != NULL) {
406 			ih->vih_func(ih->vih_arg);
407 		}
408 		ih->vih_count.ev_count++;
409 	}
410 	return 0;
411 }
412 
413 void *
414 voyager_establish_intr(device_t dev, int bit, int (*handler)(void *), void *arg)
415 {
416 	struct voyager_softc *sc = device_private(dev);
417 	struct voyager_intr *ih;
418 	uint32_t reg;
419 
420 	if ((bit < 0) || (bit > 31)) {
421 		aprint_error_dev(dev, "bogus interrupt %d\n", bit);
422 		return NULL;
423 	}
424 
425 	ih = &sc->sc_intrs[bit];
426 	if (ih->vih_func != NULL) {
427 		aprint_error_dev(dev, "interrupt %d is already in use\n", bit);
428 		return NULL;
429 	}
430 	ih->vih_func = handler;
431 	ih->vih_arg = arg;
432 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK);
433 	reg |= 1 << bit;
434 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_INTR_MASK, reg);
435 
436 	return (void *)(uintptr_t)(0x80000000 | bit);
437 }
438 
439 void
440 voyager_disestablish_intr(device_t dev, void *ih)
441 {
442 }
443 
444 /* timer */
445 #ifdef VOYAGER_DEBUG
446 static void
447 voyager_print_pwm(struct voyager_softc *sc, int pwmreg)
448 {
449 	uint32_t reg;
450 
451 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, pwmreg);
452 	aprint_debug_dev(sc->sc_dev, "%08x: %08x = %d Hz, %d high, %d low\n",
453 	    pwmreg, reg,
454 	    96000000 / (1 << ((reg & SM502_PWM_CLOCK_DIV_MASK) >> SM502_PWM_CLOCK_DIV_SHIFT)),
455 	    (reg & SM502_PWM_CLOCK_HIGH_MASK) >> SM502_PWM_CLOCK_HIGH_SHIFT,
456 	    (reg & SM502_PWM_CLOCK_LOW_MASK) >> SM502_PWM_CLOCK_LOW_SHIFT);
457 }
458 #endif
459 
460 uint32_t
461 voyager_set_pwm(int freq, int duty_cycle)
462 {
463 	int ifreq, factor, bit, steps;
464 	uint32_t reg = 0, hi, lo;
465 
466 	/*
467 	 * find the smallest divider that gets us within 4096 steps of the
468 	 * target frequency
469 	 */
470 	ifreq = freq * 4096;
471 	factor = 96000000 / ifreq;
472 	bit = fls32(factor);
473 	factor = 1 << bit;
474 	steps = 96000000 / (factor * freq);
475 	/* can't have it all off */
476 	if (duty_cycle < 1)
477 		duty_cycle = 1;
478 	/* can't be always on either */
479 	if (duty_cycle > 999)
480 		duty_cycle = 999;
481 	hi = steps * duty_cycle / 1000;
482 	if (hi < 1)
483 		hi = 1;
484 	lo = steps - hi;
485 	if (lo < 1) {
486 		hi = steps - 1;
487 		lo = 1;
488 	}
489 	DPRINTF("%d hz -> %d, %d, %d / %d\n", freq, factor, steps, lo, hi);
490 	reg = ((hi - 1) & 0xfff) << SM502_PWM_CLOCK_HIGH_SHIFT;
491 	reg |= ((lo - 1) & 0xfff) << SM502_PWM_CLOCK_LOW_SHIFT;
492 	reg |= (bit & 0xf) << SM502_PWM_CLOCK_DIV_SHIFT;
493 	DPRINTF("reg: %08x\n", reg);
494 	return reg;
495 }
496