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