Wed Dec 7 09:08:00 2011 UTC ()
consolidate bit twiddling functions into one generic case and a bunch of
macros, use them


(macallan)
diff -r1.6 -r1.7 src/sys/dev/pci/voyager.c
diff -r1.3 -r1.4 src/sys/dev/pci/voyagervar.h

cvs diff -r1.6 -r1.7 src/sys/dev/pci/voyager.c (expand / switch to unified diff)

--- src/sys/dev/pci/voyager.c 2011/10/20 22:04:10 1.6
+++ src/sys/dev/pci/voyager.c 2011/12/07 09:08:00 1.7
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: voyager.c,v 1.6 2011/10/20 22:04:10 macallan Exp $ */ 1/* $NetBSD: voyager.c,v 1.7 2011/12/07 09:08:00 macallan Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2009, 2011 Michael Lorenz 4 * Copyright (c) 2009, 2011 Michael Lorenz
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -16,27 +16,27 @@ @@ -16,27 +16,27 @@
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 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 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 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 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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 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. 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27  27
28#include <sys/cdefs.h> 28#include <sys/cdefs.h>
29__KERNEL_RCSID(0, "$NetBSD: voyager.c,v 1.6 2011/10/20 22:04:10 macallan Exp $"); 29__KERNEL_RCSID(0, "$NetBSD: voyager.c,v 1.7 2011/12/07 09:08:00 macallan Exp $");
30 30
31#include <sys/param.h> 31#include <sys/param.h>
32#include <sys/systm.h> 32#include <sys/systm.h>
33#include <sys/kernel.h> 33#include <sys/kernel.h>
34#include <sys/device.h> 34#include <sys/device.h>
35#include <sys/malloc.h> 35#include <sys/malloc.h>
36#include <sys/lwp.h> 36#include <sys/lwp.h>
37#include <sys/kauth.h> 37#include <sys/kauth.h>
38 38
39#include <dev/pci/pcivar.h> 39#include <dev/pci/pcivar.h>
40#include <dev/pci/pcireg.h> 40#include <dev/pci/pcireg.h>
41#include <dev/pci/pcidevs.h> 41#include <dev/pci/pcidevs.h>
42#include <dev/pci/pciio.h> 42#include <dev/pci/pciio.h>
@@ -212,48 +212,57 @@ voyager_attach(device_t parent, device_t @@ -212,48 +212,57 @@ voyager_attach(device_t parent, device_t
212 212
213 /* attach the framebuffer driver */ 213 /* attach the framebuffer driver */
214 vaa.vaa_memh = sc->sc_fbh; 214 vaa.vaa_memh = sc->sc_fbh;
215 vaa.vaa_mem_pa = sc->sc_fb; 215 vaa.vaa_mem_pa = sc->sc_fb;
216 vaa.vaa_regh = sc->sc_regh; 216 vaa.vaa_regh = sc->sc_regh;
217 vaa.vaa_reg_pa = sc->sc_reg; 217 vaa.vaa_reg_pa = sc->sc_reg;
218 vaa.vaa_tag = sc->sc_memt; 218 vaa.vaa_tag = sc->sc_memt;
219 vaa.vaa_pc = sc->sc_pc; 219 vaa.vaa_pc = sc->sc_pc;
220 vaa.vaa_pcitag = sc->sc_pcitag; 220 vaa.vaa_pcitag = sc->sc_pcitag;
221 strcpy(vaa.vaa_name, "voyagerfb"); 221 strcpy(vaa.vaa_name, "voyagerfb");
222 config_found_ia(sc->sc_dev, "voyagerbus", &vaa, voyager_print); 222 config_found_ia(sc->sc_dev, "voyagerbus", &vaa, voyager_print);
223 strcpy(vaa.vaa_name, "pwmclock"); 223 strcpy(vaa.vaa_name, "pwmclock");
224 config_found_ia(sc->sc_dev, "voyagerbus", &vaa, voyager_print); 224 config_found_ia(sc->sc_dev, "voyagerbus", &vaa, voyager_print);
 225 strcpy(vaa.vaa_name, "vac");
 226 config_found_ia(sc->sc_dev, "voyagerbus", &vaa, voyager_print);
 227
 228 /* we use this mutex wether there's an i2c bus or not */
 229 mutex_init(&sc->sc_i2c_lock, MUTEX_DEFAULT, IPL_NONE);
225 230
 231 /*
 232 * see if the i2c pins are configured as gpio and if so, use them
 233 * should probably be a compile time option
 234 */
226 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO0_CONTROL); 235 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO0_CONTROL);
227 if ((reg & GPIO_I2C_BITS) == 0) { 236 if ((reg & GPIO_I2C_BITS) == 0) {
228 /* both bits as outputs */ 237 /* both bits as outputs */
229 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DIR0); 238 voyager_gpio_dir(sc, 0xffffffff, GPIO_I2C_BITS);
230 reg |= GPIO_I2C_BITS; 
231 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DIR0, reg); 
232  239
233 /* Fill in the i2c tag */ 240 /* Fill in the i2c tag */
234 sc->sc_i2c.ic_cookie = sc; 241 sc->sc_i2c.ic_cookie = sc;
235 sc->sc_i2c.ic_acquire_bus = voyager_i2c_acquire_bus; 242 sc->sc_i2c.ic_acquire_bus = voyager_i2c_acquire_bus;
236 sc->sc_i2c.ic_release_bus = voyager_i2c_release_bus; 243 sc->sc_i2c.ic_release_bus = voyager_i2c_release_bus;
237 sc->sc_i2c.ic_send_start = voyager_i2c_send_start; 244 sc->sc_i2c.ic_send_start = voyager_i2c_send_start;
238 sc->sc_i2c.ic_send_stop = voyager_i2c_send_stop; 245 sc->sc_i2c.ic_send_stop = voyager_i2c_send_stop;
239 sc->sc_i2c.ic_initiate_xfer = voyager_i2c_initiate_xfer; 246 sc->sc_i2c.ic_initiate_xfer = voyager_i2c_initiate_xfer;
240 sc->sc_i2c.ic_read_byte = voyager_i2c_read_byte; 247 sc->sc_i2c.ic_read_byte = voyager_i2c_read_byte;
241 sc->sc_i2c.ic_write_byte = voyager_i2c_write_byte; 248 sc->sc_i2c.ic_write_byte = voyager_i2c_write_byte;
242 sc->sc_i2c.ic_exec = NULL; 249 sc->sc_i2c.ic_exec = NULL;
243 mutex_init(&sc->sc_i2c_lock, MUTEX_DEFAULT, IPL_NONE); 
244 iba.iba_tag = &sc->sc_i2c; 250 iba.iba_tag = &sc->sc_i2c;
245 config_found_ia(self, "i2cbus", &iba, iicbus_print); 251 config_found_ia(self, "i2cbus", &iba, iicbus_print);
246 } 252 }
 253 voyager_control_gpio(sc, ~(1 << 16), 0);
 254 voyager_gpio_dir(sc, 0xffffffff, 1 << 16);
 255 voyager_write_gpio(sc, 0xffffffff, 1 << 16);
247} 256}
248 257
249static int 258static int
250voyager_print(void *aux, const char *what) 259voyager_print(void *aux, const char *what)
251{ 260{
252 /*struct voyager_attach_args *vaa = aux;*/ 261 /*struct voyager_attach_args *vaa = aux;*/
253 262
254 if (what == NULL) 263 if (what == NULL)
255 return 0; 264 return 0;
256 265
257 printf("%s:", what); 266 printf("%s:", what);
258 267
259 return 0; 268 return 0;
@@ -344,54 +353,38 @@ voyager_i2c_read_byte(void *cookie, uint @@ -344,54 +353,38 @@ voyager_i2c_read_byte(void *cookie, uint
344 return ret; 353 return ret;
345} 354}
346 355
347static int 356static int
348voyager_i2c_write_byte(void *cookie, uint8_t val, int flags) 357voyager_i2c_write_byte(void *cookie, uint8_t val, int flags)
349{ 358{
350 int ret; 359 int ret;
351 360
352 ret = i2c_bitbang_write_byte(cookie, val, flags, &voyager_i2cbb_ops); 361 ret = i2c_bitbang_write_byte(cookie, val, flags, &voyager_i2cbb_ops);
353 delay(500); 362 delay(500);
354 return ret; 363 return ret;
355} 364}
356 365
357/* gpio stuff */ 
358void 366void
359voyager_write_gpio(void *cookie, uint32_t mask, uint32_t bits) 367voyager_twiddle_bits(void *cookie, int regnum, uint32_t mask, uint32_t bits)
360{ 368{
361 struct voyager_softc *sc = cookie; 369 struct voyager_softc *sc = cookie;
362 uint32_t reg; 370 uint32_t reg;
363 371
364 /* don't interfere with i2c ops */ 372 /* don't interfere with i2c ops */
365 mutex_enter(&sc->sc_i2c_lock); 373 mutex_enter(&sc->sc_i2c_lock);
366 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0); 374 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, regnum);
367 reg &= mask; 
368 reg |= bits; 
369 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_GPIO_DATA0, reg); 
370 mutex_exit(&sc->sc_i2c_lock); 
371} 
372 
373void 
374voyager_control_gpio(void *cookie, uint32_t mask, uint32_t bits) 
375{ 
376 struct voyager_softc *sc = cookie; 
377 uint32_t reg; 
378 
379 /* don't interfere with i2c ops */ 
380 mutex_enter(&sc->sc_i2c_lock); 
381 reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_GPIO0_CONTROL); 
382 reg &= mask; 375 reg &= mask;
383 reg |= bits; 376 reg |= bits;
384 bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_GPIO0_CONTROL, reg); 377 bus_space_write_4(sc->sc_memt, sc->sc_regh, regnum, reg);
385 mutex_exit(&sc->sc_i2c_lock); 378 mutex_exit(&sc->sc_i2c_lock);
386} 379}
387 380
388static int 381static int
389voyager_intr(void *cookie) 382voyager_intr(void *cookie)
390{ 383{
391 struct voyager_softc *sc = cookie; 384 struct voyager_softc *sc = cookie;
392 struct voyager_intr *ih; 385 struct voyager_intr *ih;
393 uint32_t intrs; 386 uint32_t intrs;
394 uint32_t mask, bit; 387 uint32_t mask, bit;
395 int num; 388 int num;
396 389
397 intrs = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_STATUS); 390 intrs = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_INTR_STATUS);

cvs diff -r1.3 -r1.4 src/sys/dev/pci/voyagervar.h (expand / switch to unified diff)

--- src/sys/dev/pci/voyagervar.h 2011/10/18 17:57:40 1.3
+++ src/sys/dev/pci/voyagervar.h 2011/12/07 09:08:00 1.4
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: voyagervar.h,v 1.3 2011/10/18 17:57:40 macallan Exp $ */ 1/* $NetBSD: voyagervar.h,v 1.4 2011/12/07 09:08:00 macallan Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2011 Michael Lorenz 4 * Copyright (c) 2011 Michael Lorenz
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -16,44 +16,53 @@ @@ -16,44 +16,53 @@
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 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 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 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 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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 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. 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27 27
28#include <sys/cdefs.h> 28#include <sys/cdefs.h>
29__KERNEL_RCSID(0, "$NetBSD: voyagervar.h,v 1.3 2011/10/18 17:57:40 macallan Exp $"); 29__KERNEL_RCSID(0, "$NetBSD: voyagervar.h,v 1.4 2011/12/07 09:08:00 macallan Exp $");
30 30
31#ifndef VOYAGERVAR_H 31#ifndef VOYAGERVAR_H
32#define VOYAGERVAR_H 32#define VOYAGERVAR_H
33 33
34#include <dev/pci/pcivar.h> 34#include <dev/pci/pcivar.h>
35#include <dev/pci/pcireg.h> 35#include <dev/pci/pcireg.h>
36 36
37struct voyager_attach_args { 37struct voyager_attach_args {
38 pci_chipset_tag_t vaa_pc; 38 pci_chipset_tag_t vaa_pc;
39 pcitag_t vaa_pcitag; 39 pcitag_t vaa_pcitag;
40 bus_space_tag_t vaa_tag; 40 bus_space_tag_t vaa_tag;
41 bus_space_handle_t vaa_regh; 41 bus_space_handle_t vaa_regh;
42 bus_space_handle_t vaa_memh; 42 bus_space_handle_t vaa_memh;
43 bus_addr_t vaa_reg_pa; 43 bus_addr_t vaa_reg_pa;
44 bus_addr_t vaa_mem_pa; 44 bus_addr_t vaa_mem_pa;
45 char vaa_name[32]; 45 char vaa_name[32];
46}; 46};
47 47
 48void voyager_twiddle_bits(void *, int, uint32_t, uint32_t);
 49
48/* set gpio bits - (register & param1) | param2 */ 50/* set gpio bits - (register & param1) | param2 */
49void voyager_write_gpio(void *, uint32_t, uint32_t); 51#define voyager_write_gpio(cookie, m_and, m_or) \
 52 voyager_twiddle_bits(cookie, SM502_GPIO_DATA0, m_and, m_or)
50/* control gpio pin usage - 0 is gpio, 1 is other stuff ( like PWM ) */ 53/* control gpio pin usage - 0 is gpio, 1 is other stuff ( like PWM ) */
51void voyager_control_gpio(void *, uint32_t, uint32_t); 54#define voyager_control_gpio(cookie, m_and, m_or) \
 55 voyager_twiddle_bits(cookie, SM502_GPIO0_CONTROL, m_and, m_or)
 56/* gpio direction - 1 is output */
 57#define voyager_gpio_dir(cookie, m_and, m_or) \
 58 voyager_twiddle_bits(cookie, SM502_GPIO_DIR0, m_and, m_or)
 59#define voyager_control_power_0(cookie, m_and, m_or) \
 60 voyager_twiddle_bits(cookie, SM502_POWER_MODE0_GATE, m_and, m_or)
52 61
53void *voyager_establish_intr(device_t, int, int (*)(void *), void *); 62void *voyager_establish_intr(device_t, int, int (*)(void *), void *);
54void voyager_disestablish_intr(device_t, void *); 63void voyager_disestablish_intr(device_t, void *);
55 64
56/* frequency in Hz, duty cycle in 1000ths */ 65/* frequency in Hz, duty cycle in 1000ths */
57uint32_t voyager_set_pwm(int, int); 66uint32_t voyager_set_pwm(int, int);
58 67
59#endif 68#endif