1 /* $NetBSD: gpio_opb.c,v 1.5 2006/03/13 15:31:11 shige Exp $ */
4 * Copyright (c) 2004 Shigeyuki Fukushima.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
21 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/systm.h>
39 #include <machine/pio.h>
42 #include <dev/gpio/gpiovar.h>
44 #include <powerpc/ibm4xx/dcr405gp.h>
45 #include <powerpc/ibm4xx/dev/opbvar.h>
46 #include <powerpc/ibm4xx/dev/gpioreg.h>
48 struct gpio_opb_softc
{
49 struct device sc_dev
; /* device generic */
51 bus_space_tag_t sc_gpio_iot
;
52 bus_space_handle_t sc_gpio_ioh
;
53 struct gpio_chipset_tag sc_gpio_gc
;
54 gpio_pin_t sc_gpio_pins
[GPIO_NPINS
];
57 static int gpio_opb_match(struct device
*, struct cfdata
*, void *);
58 static void gpio_opb_attach(struct device
*, struct device
*, void *);
60 CFATTACH_DECL(opbgpio
, sizeof(struct gpio_opb_softc
),
61 gpio_opb_match
, gpio_opb_attach
, NULL
, NULL
);
63 static int gpio_opb_pin_read(void *, int);
64 static void gpio_opb_pin_write(void *, int, int);
65 static void gpio_opb_pin_ctl(void *, int, int);
69 gpio_opb_match(struct device
*parent
, struct cfdata
*cf
, void *aux
)
71 struct opb_attach_args
*oaa
= aux
;
73 if (strcmp(oaa
->opb_name
, cf
->cf_name
) != 0)
80 gpio_opb_attach(struct device
*parent
, struct device
*self
, void *aux
)
82 struct gpio_opb_softc
*sc
= (struct gpio_opb_softc
*)self
;
83 struct opb_attach_args
*oaa
= aux
;
84 struct gpiobus_attach_args gba
;
86 uint32_t reg1
, reg2
, reg3
;
88 aprint_naive(": GPIO controller\n");
89 aprint_normal(": On-Chip GPIO controller\n");
91 /* Map GPIO I/O space */
92 sc
->sc_gpio_iot
= oaa
->opb_bt
;
93 bus_space_map(sc
->sc_gpio_iot
, oaa
->opb_addr
,
94 GPIO_NREG
, 0, &sc
->sc_gpio_ioh
);
96 /* Read current register status */
97 reg1
= bus_space_read_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
, GPIO_IR
);
98 reg2
= bus_space_read_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
, GPIO_TCR
);
99 reg3
= bus_space_read_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
, GPIO_ODR
);
101 /* Initialize pins array */
102 for (i
= 0 ; i
< GPIO_NPINS
; i
++) {
104 sc
->sc_gpio_pins
[i
].pin_num
= i
;
105 sc
->sc_gpio_pins
[i
].pin_caps
= GPIO_PIN_INOUT
109 /* current defaults */
110 sc
->sc_gpio_pins
[i
].pin_flags
=
111 ((reg3
>> GPIO_PIN_SHIFT(p
)) & 0x01)
113 : (((reg2
>> GPIO_PIN_SHIFT(p
)) & 0x01)
115 : GPIO_PIN_TRISTATE
);
116 sc
->sc_gpio_pins
[i
].pin_state
=
117 ((reg1
>> GPIO_PIN_SHIFT(p
)) & 0x01);
118 sc
->sc_gpio_pins
[i
].pin_mapped
= 0;
121 /* Create controller tag */
122 sc
->sc_gpio_gc
.gp_cookie
= sc
;
123 sc
->sc_gpio_gc
.gp_pin_read
= gpio_opb_pin_read
;
124 sc
->sc_gpio_gc
.gp_pin_write
= gpio_opb_pin_write
;
125 sc
->sc_gpio_gc
.gp_pin_ctl
= gpio_opb_pin_ctl
;
127 gba
.gba_gc
= &sc
->sc_gpio_gc
;
128 gba
.gba_pins
= sc
->sc_gpio_pins
;
129 gba
.gba_npins
= GPIO_NPINS
;
131 /* Attach GPIO framework */
132 (void) config_found(&sc
->sc_dev
, &gba
, gpiobus_print
);
136 gpio_opb_pin_read(void *arg
, int pin
)
138 struct gpio_opb_softc
*sc
= arg
;
142 p
= pin
% GPIO_NPINS
;
145 data
= bus_space_read_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
, GPIO_IR
);
147 return (data
>> GPIO_PIN_SHIFT(p
)) & 0x01;
151 gpio_opb_pin_write(void *arg
, int pin
, int value
)
153 struct gpio_opb_softc
*sc
= arg
;
157 p
= pin
% GPIO_NPINS
;
160 data
= bus_space_read_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
, GPIO_OR
);
162 data
&= ~(1 << GPIO_PIN_SHIFT(p
));
163 } else if (value
== 1) {
164 data
|= (1 << GPIO_PIN_SHIFT(p
));
167 bus_space_write_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
, GPIO_OR
, data
);
171 gpio_opb_pin_ctl(void *arg
, int pin
, int flags
)
173 struct gpio_opb_softc
*sc
= arg
;
177 p
= pin
% GPIO_NPINS
;
180 if (flags
& GPIO_PIN_INOUT
) {
181 /* GPIOn_ODR register bit is 0 */
182 data
= bus_space_read_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
,
184 data
&= ~(1 << GPIO_PIN_SHIFT(p
));
185 bus_space_write_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
,
187 /* GPIOn_TCR register bit is 1 */
188 data
= bus_space_read_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
,
190 data
|= (1 << GPIO_PIN_SHIFT(p
));
191 bus_space_write_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
,
195 if (flags
& GPIO_PIN_TRISTATE
) {
196 /* GPIOn_ODR register bit is 0 */
197 data
= bus_space_read_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
,
199 data
&= ~(1 << GPIO_PIN_SHIFT(p
));
200 bus_space_write_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
,
202 /* GPIOn_TCR register bit is 0 */
203 data
= bus_space_read_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
,
205 data
&= ~(1 << GPIO_PIN_SHIFT(p
));
206 bus_space_write_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
,
210 if (flags
& GPIO_PIN_OPENDRAIN
) {
211 /* GPIOn_ODR register bit is 1 */
212 data
= bus_space_read_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
,
214 data
|= (1 << GPIO_PIN_SHIFT(p
));
215 bus_space_write_4(sc
->sc_gpio_iot
, sc
->sc_gpio_ioh
,