1 /* $NetBSD: admgpio.c,v 1.1 2007/03/20 08:52:01 dyoung Exp $ */
4 * Copyright (c) 2007 David Young. All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
29 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: admgpio.c,v 1.1 2007/03/20 08:52:01 dyoung Exp $");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/device.h>
37 #include <sys/kernel.h> /* for hz */
39 #include <machine/bus.h>
41 #include <dev/gpio/gpiovar.h>
43 #include <mips/adm5120/include/adm5120reg.h>
44 #include <mips/adm5120/include/adm5120var.h>
45 #include <mips/adm5120/include/adm5120_mainbusvar.h>
47 static inline uint32_t
48 admgpio_read(struct mainbus_softc
*sc
)
50 return bus_space_read_4(sc
->sc_obiot
, sc
->sc_gpioh
, ADM5120_GPIO0
);
54 admgpio_write(struct mainbus_softc
*sc
, uint32_t val
)
56 bus_space_write_4(sc
->sc_obiot
, sc
->sc_gpioh
, ADM5120_GPIO0
, val
);
60 admgpio_pin_ctl(void *cookie
, int pin
, int flags
)
62 struct mainbus_softc
*sc
= cookie
;
65 KASSERT(flags
== GPIO_PIN_INPUT
|| flags
== GPIO_PIN_OUTPUT
);
67 mask
= __SHIFTIN(1 << pin
, ADM5120_GPIO0_OE
);
68 gpio0
= admgpio_read(sc
);
70 if (flags
== GPIO_PIN_OUTPUT
)
71 admgpio_write(sc
, gpio0
| mask
);
73 admgpio_write(sc
, gpio0
& ~mask
);
77 admgpio_pin_read(void *cookie
, int pin
)
79 struct mainbus_softc
*sc
= cookie
;
82 KASSERT(pin
>= 0 && pin
< 8);
84 if (sc
->sc_pins
[pin
].pin_flags
== GPIO_PIN_INPUT
)
85 mask
= __SHIFTIN(1 << pin
, ADM5120_GPIO0_IV
);
87 mask
= __SHIFTIN(1 << pin
, ADM5120_GPIO0_OV
);
89 gpio0
= admgpio_read(sc
);
91 return ((gpio0
& mask
) != 0) ? GPIO_PIN_HIGH
: GPIO_PIN_LOW
;
95 admgpio_pin_write(void *cookie
, int pin
, int value
)
97 struct mainbus_softc
*sc
= cookie
;
100 KASSERT(pin
>= 0 && pin
< 8);
102 mask
= __SHIFTIN(1 << pin
, ADM5120_GPIO0_OV
);
103 gpio0
= admgpio_read(sc
);
106 (value
== GPIO_PIN_HIGH
) ? (gpio0
| mask
) : (gpio0
& ~mask
));
110 admgpio_attach(struct mainbus_softc
*sc
)
114 struct gpiobus_attach_args gba
;
116 oe
= __SHIFTOUT(admgpio_read(sc
), ADM5120_GPIO0_OE
);
118 for (pin
= 0; pin
< __arraycount(sc
->sc_pins
); pin
++) {
119 sc
->sc_pins
[pin
].pin_num
= pin
;
120 sc
->sc_pins
[pin
].pin_caps
= GPIO_PIN_INPUT
| GPIO_PIN_OUTPUT
;
122 if ((oe
& (1 << pin
)) != 0)
123 sc
->sc_pins
[pin
].pin_flags
= GPIO_PIN_OUTPUT
;
125 sc
->sc_pins
[pin
].pin_flags
= GPIO_PIN_INPUT
;
127 sc
->sc_pins
[pin
].pin_state
= admgpio_pin_read(sc
, pin
);
130 sc
->sc_gp
.gp_cookie
= sc
;
131 sc
->sc_gp
.gp_pin_read
= admgpio_pin_read
;
132 sc
->sc_gp
.gp_pin_write
= admgpio_pin_write
;
133 sc
->sc_gp
.gp_pin_ctl
= admgpio_pin_ctl
;
135 gba
.gba_gc
= &sc
->sc_gp
;
136 gba
.gba_pins
= &sc
->sc_pins
[0];
137 gba
.gba_npins
= __arraycount(sc
->sc_pins
);
139 /* Attach GPIO framework */
140 return config_found_ia(&sc
->sc_dev
, "gpiobus", &gba
, gpiobus_print
);