hwgpio.c revision 1.1.2.2 1 1.1.2.2 martin /* $NetBSD: hwgpio.c,v 1.1.2.2 2024/02/03 11:47:04 martin Exp $ */
2 1.1.2.2 martin
3 1.1.2.2 martin /*-
4 1.1.2.2 martin * Copyright (c) 2024 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1.2.2 martin * All rights reserved.
6 1.1.2.2 martin *
7 1.1.2.2 martin * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 martin * modification, are permitted provided that the following conditions
9 1.1.2.2 martin * are met:
10 1.1.2.2 martin * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 martin * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 martin * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 martin * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 martin * documentation and/or other materials provided with the distribution.
15 1.1.2.2 martin *
16 1.1.2.2 martin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1.2.2 martin * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1.2.2 martin * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1.2.2 martin * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1.2.2 martin * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1.2.2 martin * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1.2.2 martin * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1.2.2 martin * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1.2.2 martin * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.2.2 martin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.2.2 martin * SUCH DAMAGE.
27 1.1.2.2 martin */
28 1.1.2.2 martin
29 1.1.2.2 martin #include <sys/cdefs.h>
30 1.1.2.2 martin __KERNEL_RCSID(0, "$NetBSD: hwgpio.c,v 1.1.2.2 2024/02/03 11:47:04 martin Exp $");
31 1.1.2.2 martin
32 1.1.2.2 martin #include <sys/param.h>
33 1.1.2.2 martin #include <sys/bus.h>
34 1.1.2.2 martin #include <sys/cpu.h>
35 1.1.2.2 martin #include <sys/device.h>
36 1.1.2.2 martin #include <sys/kmem.h>
37 1.1.2.2 martin #include <sys/bitops.h>
38 1.1.2.2 martin #include <sys/gpio.h>
39 1.1.2.2 martin
40 1.1.2.2 martin #include <dev/gpio/gpiovar.h>
41 1.1.2.2 martin
42 1.1.2.2 martin #include <machine/wii.h>
43 1.1.2.2 martin
44 1.1.2.2 martin #include "hollywood.h"
45 1.1.2.2 martin
46 1.1.2.2 martin #define PIN(_num, _name, _caps) \
47 1.1.2.2 martin { .pin_num = (_num), \
48 1.1.2.2 martin .pin_caps = (_caps), \
49 1.1.2.2 martin .pin_defname = (_name), \
50 1.1.2.2 martin }
51 1.1.2.2 martin static gpio_pin_t hwgpio_pins[] = {
52 1.1.2.2 martin PIN( 0, "POWER", GPIO_PIN_INPUT),
53 1.1.2.2 martin PIN( 1, "SHUTDOWN", GPIO_PIN_OUTPUT),
54 1.1.2.2 martin PIN( 2, "FAN", GPIO_PIN_OUTPUT),
55 1.1.2.2 martin PIN( 3, "DC_DC", GPIO_PIN_OUTPUT),
56 1.1.2.2 martin PIN( 4, "DI_SPIN", GPIO_PIN_OUTPUT),
57 1.1.2.2 martin PIN( 5, "SLOT_LED", GPIO_PIN_OUTPUT),
58 1.1.2.2 martin PIN( 6, "EJECT_BTN", GPIO_PIN_INPUT),
59 1.1.2.2 martin PIN( 7, "SLOT_IN", GPIO_PIN_INPUT),
60 1.1.2.2 martin PIN( 8, "SENSOR_BAR", GPIO_PIN_OUTPUT),
61 1.1.2.2 martin PIN( 9, "DO_EJECT", GPIO_PIN_OUTPUT),
62 1.1.2.2 martin PIN(10, "EEP_CS", GPIO_PIN_OUTPUT),
63 1.1.2.2 martin PIN(11, "EEP_CLK", GPIO_PIN_OUTPUT),
64 1.1.2.2 martin PIN(12, "EEP_MOSI", GPIO_PIN_OUTPUT),
65 1.1.2.2 martin PIN(13, "EEP_MISO", GPIO_PIN_INPUT),
66 1.1.2.2 martin PIN(14, "AVE_SCL", GPIO_PIN_OUTPUT),
67 1.1.2.2 martin PIN(15, "AVE_SDA", GPIO_PIN_INPUT | GPIO_PIN_OUTPUT),
68 1.1.2.2 martin PIN(16, "DEBUG0", GPIO_PIN_INPUT | GPIO_PIN_OUTPUT),
69 1.1.2.2 martin PIN(17, "DEBUG1", GPIO_PIN_INPUT | GPIO_PIN_OUTPUT),
70 1.1.2.2 martin PIN(18, "DEBUG2", GPIO_PIN_INPUT | GPIO_PIN_OUTPUT),
71 1.1.2.2 martin PIN(19, "DEBUG3", GPIO_PIN_INPUT | GPIO_PIN_OUTPUT),
72 1.1.2.2 martin PIN(20, "DEBUG4", GPIO_PIN_INPUT | GPIO_PIN_OUTPUT),
73 1.1.2.2 martin PIN(21, "DEBUG5", GPIO_PIN_INPUT | GPIO_PIN_OUTPUT),
74 1.1.2.2 martin PIN(22, "DEBUG6", GPIO_PIN_INPUT | GPIO_PIN_OUTPUT),
75 1.1.2.2 martin PIN(23, "DEBUG7", GPIO_PIN_INPUT | GPIO_PIN_OUTPUT),
76 1.1.2.2 martin };
77 1.1.2.2 martin #undef PIN
78 1.1.2.2 martin
79 1.1.2.2 martin struct hwgpio_softc {
80 1.1.2.2 martin struct gpio_chipset_tag sc_gp;
81 1.1.2.2 martin };
82 1.1.2.2 martin
83 1.1.2.2 martin #define RD4(reg) in32(reg)
84 1.1.2.2 martin #define WR4(reg, val) out32((reg), (val))
85 1.1.2.2 martin
86 1.1.2.2 martin static int
87 1.1.2.2 martin hwgpio_pin_read(void *priv, int pin)
88 1.1.2.2 martin {
89 1.1.2.2 martin return (RD4(HW_GPIOB_IN) & __BIT(pin)) != 0;
90 1.1.2.2 martin }
91 1.1.2.2 martin
92 1.1.2.2 martin static void
93 1.1.2.2 martin hwgpio_pin_write(void *priv, int pin, int value)
94 1.1.2.2 martin {
95 1.1.2.2 martin uint32_t out;
96 1.1.2.2 martin int s;
97 1.1.2.2 martin
98 1.1.2.2 martin s = splhigh();
99 1.1.2.2 martin out = RD4(HW_GPIOB_OUT);
100 1.1.2.2 martin if (value) {
101 1.1.2.2 martin out |= __BIT(pin);
102 1.1.2.2 martin } else {
103 1.1.2.2 martin out &= ~__BIT(pin);
104 1.1.2.2 martin }
105 1.1.2.2 martin WR4(HW_GPIOB_OUT, out);
106 1.1.2.2 martin splx(s);
107 1.1.2.2 martin }
108 1.1.2.2 martin
109 1.1.2.2 martin static void
110 1.1.2.2 martin hwgpio_pin_ctl(void *priv, int pin, int flags)
111 1.1.2.2 martin {
112 1.1.2.2 martin uint32_t dir;
113 1.1.2.2 martin int s;
114 1.1.2.2 martin
115 1.1.2.2 martin s = splhigh();
116 1.1.2.2 martin dir = RD4(HW_GPIOB_DIR);
117 1.1.2.2 martin if (flags & GPIO_PIN_OUTPUT) {
118 1.1.2.2 martin dir |= __BIT(pin);
119 1.1.2.2 martin } else {
120 1.1.2.2 martin dir &= ~__BIT(pin);
121 1.1.2.2 martin }
122 1.1.2.2 martin WR4(HW_GPIOB_DIR, dir);
123 1.1.2.2 martin splx(s);
124 1.1.2.2 martin }
125 1.1.2.2 martin
126 1.1.2.2 martin static int
127 1.1.2.2 martin hwgpio_match(device_t parent, cfdata_t cf, void *aux)
128 1.1.2.2 martin {
129 1.1.2.2 martin return 1;
130 1.1.2.2 martin }
131 1.1.2.2 martin
132 1.1.2.2 martin static void
133 1.1.2.2 martin hwgpio_attach(device_t parent, device_t self, void *aux)
134 1.1.2.2 martin {
135 1.1.2.2 martin struct hwgpio_softc * const sc = device_private(self);
136 1.1.2.2 martin struct gpio_chipset_tag *gp = &sc->sc_gp;
137 1.1.2.2 martin struct gpiobus_attach_args gba = {};
138 1.1.2.2 martin uint32_t in, out, dir;
139 1.1.2.2 martin u_int n;
140 1.1.2.2 martin
141 1.1.2.2 martin gp->gp_cookie = sc;
142 1.1.2.2 martin gp->gp_pin_read = hwgpio_pin_read;
143 1.1.2.2 martin gp->gp_pin_write = hwgpio_pin_write;
144 1.1.2.2 martin gp->gp_pin_ctl = hwgpio_pin_ctl;
145 1.1.2.2 martin
146 1.1.2.2 martin aprint_naive("\n");
147 1.1.2.2 martin aprint_normal(": GPIO\n");
148 1.1.2.2 martin
149 1.1.2.2 martin in = RD4(HW_GPIOB_IN);
150 1.1.2.2 martin out = RD4(HW_GPIOB_OUT);
151 1.1.2.2 martin dir = RD4(HW_GPIOB_DIR);
152 1.1.2.2 martin for (n = 0; n < __arraycount(hwgpio_pins); n++) {
153 1.1.2.2 martin const uint32_t mask = __BIT(hwgpio_pins[n].pin_num);
154 1.1.2.2 martin if (dir & mask) {
155 1.1.2.2 martin hwgpio_pins[n].pin_state = (out & mask) != 0;
156 1.1.2.2 martin } else {
157 1.1.2.2 martin hwgpio_pins[n].pin_state = (in & mask) != 0;
158 1.1.2.2 martin }
159 1.1.2.2 martin }
160 1.1.2.2 martin
161 1.1.2.2 martin gba.gba_gc = &sc->sc_gp;
162 1.1.2.2 martin gba.gba_pins = hwgpio_pins;
163 1.1.2.2 martin gba.gba_npins = __arraycount(hwgpio_pins);
164 1.1.2.2 martin config_found(self, &gba, NULL, CFARGS_NONE);
165 1.1.2.2 martin }
166 1.1.2.2 martin
167 1.1.2.2 martin CFATTACH_DECL_NEW(hwgpio, sizeof(struct hwgpio_softc),
168 1.1.2.2 martin hwgpio_match, hwgpio_attach, NULL, NULL);
169