imx7_gpc.c revision 1.1.2.3 1 1.1.2.3 thorpej /* $NetBSD: imx7_gpc.c,v 1.1.2.3 2021/04/03 22:28:17 thorpej Exp $ */
2 1.1.2.2 thorpej /*-
3 1.1.2.2 thorpej * Copyright (c) 2019 Genetec Corporation. All rights reserved.
4 1.1.2.2 thorpej * Written by Hashimoto Kenichi for Genetec Corporation.
5 1.1.2.2 thorpej *
6 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
7 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
8 1.1.2.2 thorpej * are met:
9 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
10 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
11 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
13 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
14 1.1.2.2 thorpej *
15 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1.2.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1.2.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1.2.2 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1.2.2 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 1.1.2.2 thorpej * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 1.1.2.2 thorpej * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 1.1.2.2 thorpej * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 1.1.2.2 thorpej * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1.2.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1.2.2 thorpej * SUCH DAMAGE.
26 1.1.2.2 thorpej */
27 1.1.2.2 thorpej #include <sys/cdefs.h>
28 1.1.2.3 thorpej __KERNEL_RCSID(0, "$NetBSD: imx7_gpc.c,v 1.1.2.3 2021/04/03 22:28:17 thorpej Exp $");
29 1.1.2.2 thorpej
30 1.1.2.2 thorpej #include "opt_fdt.h"
31 1.1.2.2 thorpej
32 1.1.2.2 thorpej #define _INTR_PRIVATE
33 1.1.2.2 thorpej
34 1.1.2.2 thorpej #include <sys/param.h>
35 1.1.2.2 thorpej #include <sys/bus.h>
36 1.1.2.2 thorpej #include <sys/device.h>
37 1.1.2.2 thorpej
38 1.1.2.2 thorpej #include <arm/nxp/imx6var.h>
39 1.1.2.2 thorpej #include <arm/nxp/imx6_reg.h>
40 1.1.2.2 thorpej #include <arm/nxp/imx6_gpcreg.h>
41 1.1.2.2 thorpej
42 1.1.2.2 thorpej #include <arm/cortex/gic_intr.h>
43 1.1.2.2 thorpej
44 1.1.2.2 thorpej #include <dev/fdt/fdtvar.h>
45 1.1.2.2 thorpej
46 1.1.2.2 thorpej #define GPC_PCG_CPU_0_1_MAPPING 0xec
47 1.1.2.2 thorpej #define OTG2_A53_DOMAIN __BIT(5)
48 1.1.2.2 thorpej #define OTG1_A53_DOMAIN __BIT(4)
49 1.1.2.2 thorpej
50 1.1.2.2 thorpej #define GPC_PU_PGC_SW_PUP_REQ 0xf8
51 1.1.2.2 thorpej #define USB_OTG2_SW_PUP_REQ __BIT(3)
52 1.1.2.2 thorpej #define USB_OTG1_SW_PUP_REQ __BIT(2)
53 1.1.2.2 thorpej
54 1.1.2.2 thorpej #define IMXGPC_MAXCPUS 4
55 1.1.2.2 thorpej
56 1.1.2.2 thorpej /* Mapping of CPU number to GPC_IMR1_COREx base offset */
57 1.1.2.2 thorpej static const bus_size_t imx7gpc_imr_base[IMXGPC_MAXCPUS] = {
58 1.1.2.2 thorpej 0x30,
59 1.1.2.2 thorpej 0x40,
60 1.1.2.2 thorpej 0x1c0,
61 1.1.2.2 thorpej 0x1d0,
62 1.1.2.2 thorpej };
63 1.1.2.2 thorpej
64 1.1.2.2 thorpej #define GPC_IMRn_COREx(n,x) (imx7gpc_imr_base[(x)] + (n) * 0x4)
65 1.1.2.2 thorpej
66 1.1.2.2 thorpej struct imx7gpc_softc {
67 1.1.2.2 thorpej device_t sc_dev;
68 1.1.2.2 thorpej
69 1.1.2.2 thorpej bus_space_tag_t sc_iot;
70 1.1.2.2 thorpej bus_space_handle_t sc_ioh;
71 1.1.2.2 thorpej };
72 1.1.2.2 thorpej
73 1.1.2.2 thorpej static int imx7gpc_match(device_t, struct cfdata *, void *);
74 1.1.2.2 thorpej static void imx7gpc_attach(device_t, device_t, void *);
75 1.1.2.2 thorpej
76 1.1.2.2 thorpej static void imx7gpc_powerup(struct imx7gpc_softc *, uint32_t, uint32_t);
77 1.1.2.2 thorpej static void imx7gpc_mask(struct imx7gpc_softc *, u_int, bool);
78 1.1.2.2 thorpej static void imx7gpc_unmask(struct imx7gpc_softc *, u_int, bool);
79 1.1.2.2 thorpej
80 1.1.2.2 thorpej static void *imx7gpc_establish(device_t, u_int *, int, int,
81 1.1.2.3 thorpej int (*)(void *), void *, const char *);
82 1.1.2.2 thorpej static void imx7gpc_disestablish(device_t, void *);
83 1.1.2.2 thorpej static bool imx7gpc_intrstr(device_t, u_int *, char *, size_t);
84 1.1.2.2 thorpej
85 1.1.2.2 thorpej struct fdtbus_interrupt_controller_func imx7gpc_funcs = {
86 1.1.2.2 thorpej .establish = imx7gpc_establish,
87 1.1.2.2 thorpej .disestablish = imx7gpc_disestablish,
88 1.1.2.2 thorpej .intrstr = imx7gpc_intrstr
89 1.1.2.2 thorpej };
90 1.1.2.2 thorpej
91 1.1.2.2 thorpej CFATTACH_DECL_NEW(imx7gpc, sizeof(struct imx7gpc_softc),
92 1.1.2.2 thorpej imx7gpc_match, imx7gpc_attach, NULL, NULL);
93 1.1.2.2 thorpej
94 1.1.2.3 thorpej static const struct device_compatible_entry compat_data[] = {
95 1.1.2.3 thorpej { .compat = "fsl,imx7d-gpc" },
96 1.1.2.3 thorpej { .compat = "fsl,imx8mq-gpc" },
97 1.1.2.3 thorpej DEVICE_COMPAT_EOL
98 1.1.2.3 thorpej };
99 1.1.2.3 thorpej
100 1.1.2.2 thorpej static int
101 1.1.2.2 thorpej imx7gpc_match(device_t parent, cfdata_t cf, void *aux)
102 1.1.2.2 thorpej {
103 1.1.2.2 thorpej struct fdt_attach_args * const faa = aux;
104 1.1.2.2 thorpej
105 1.1.2.3 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
106 1.1.2.2 thorpej }
107 1.1.2.2 thorpej
108 1.1.2.2 thorpej static void
109 1.1.2.2 thorpej imx7gpc_attach(device_t parent, device_t self, void *aux)
110 1.1.2.2 thorpej {
111 1.1.2.2 thorpej struct imx7gpc_softc * const sc = device_private(self);
112 1.1.2.2 thorpej struct fdt_attach_args * const faa = aux;
113 1.1.2.2 thorpej const int phandle = faa->faa_phandle;
114 1.1.2.2 thorpej bus_addr_t gpc_addr;
115 1.1.2.2 thorpej bus_size_t gpc_size;
116 1.1.2.2 thorpej int error;
117 1.1.2.2 thorpej
118 1.1.2.2 thorpej KASSERT(ncpu <= IMXGPC_MAXCPUS);
119 1.1.2.2 thorpej
120 1.1.2.2 thorpej if (fdtbus_get_reg(phandle, 0, &gpc_addr, &gpc_size) != 0) {
121 1.1.2.2 thorpej aprint_error(": couldn't get gpc registers\n");
122 1.1.2.2 thorpej return;
123 1.1.2.2 thorpej }
124 1.1.2.2 thorpej
125 1.1.2.2 thorpej sc->sc_dev = self;
126 1.1.2.2 thorpej sc->sc_iot = faa->faa_bst;
127 1.1.2.2 thorpej
128 1.1.2.2 thorpej error = bus_space_map(sc->sc_iot, gpc_addr, gpc_size, 0,
129 1.1.2.2 thorpej &sc->sc_ioh);
130 1.1.2.2 thorpej if (error) {
131 1.1.2.2 thorpej aprint_error(": couldn't map gpc registers: %d\n", error);
132 1.1.2.2 thorpej return;
133 1.1.2.2 thorpej }
134 1.1.2.2 thorpej
135 1.1.2.2 thorpej error = fdtbus_register_interrupt_controller(self, faa->faa_phandle,
136 1.1.2.2 thorpej &imx7gpc_funcs);
137 1.1.2.2 thorpej if (error) {
138 1.1.2.2 thorpej aprint_error(": couldn't register with fdtbus: %d\n", error);
139 1.1.2.2 thorpej return;
140 1.1.2.2 thorpej }
141 1.1.2.2 thorpej
142 1.1.2.2 thorpej aprint_naive("\n");
143 1.1.2.2 thorpej aprint_normal(": General Power Controller\n");
144 1.1.2.2 thorpej
145 1.1.2.2 thorpej /* XXX enable OTG power domains */
146 1.1.2.2 thorpej imx7gpc_powerup(sc, USB_OTG2_SW_PUP_REQ | USB_OTG1_SW_PUP_REQ,
147 1.1.2.2 thorpej OTG2_A53_DOMAIN | OTG1_A53_DOMAIN);
148 1.1.2.2 thorpej }
149 1.1.2.2 thorpej
150 1.1.2.2 thorpej static void
151 1.1.2.2 thorpej imx7gpc_powerup(struct imx7gpc_softc *sc, uint32_t req, uint32_t map)
152 1.1.2.2 thorpej {
153 1.1.2.2 thorpej uint32_t val;
154 1.1.2.2 thorpej
155 1.1.2.2 thorpej val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GPC_PCG_CPU_0_1_MAPPING);
156 1.1.2.2 thorpej val |= map;
157 1.1.2.2 thorpej bus_space_write_4(sc->sc_iot, sc->sc_ioh, GPC_PCG_CPU_0_1_MAPPING, val);
158 1.1.2.2 thorpej
159 1.1.2.2 thorpej val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GPC_PU_PGC_SW_PUP_REQ);
160 1.1.2.2 thorpej val |= req;
161 1.1.2.2 thorpej bus_space_write_4(sc->sc_iot, sc->sc_ioh, GPC_PU_PGC_SW_PUP_REQ, val);
162 1.1.2.2 thorpej
163 1.1.2.2 thorpej delay(5000);
164 1.1.2.2 thorpej
165 1.1.2.2 thorpej val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GPC_PCG_CPU_0_1_MAPPING);
166 1.1.2.2 thorpej val &= ~map;
167 1.1.2.2 thorpej bus_space_write_4(sc->sc_iot, sc->sc_ioh, GPC_PCG_CPU_0_1_MAPPING, val);
168 1.1.2.2 thorpej }
169 1.1.2.2 thorpej
170 1.1.2.2 thorpej static void
171 1.1.2.2 thorpej imx7gpc_mask(struct imx7gpc_softc *sc, u_int irq, bool mpsafe)
172 1.1.2.2 thorpej {
173 1.1.2.2 thorpej const u_int reg = irq / 32;
174 1.1.2.2 thorpej const u_int bit = irq % 32;
175 1.1.2.2 thorpej uint32_t val;
176 1.1.2.2 thorpej
177 1.1.2.2 thorpej for (u_int cpu = 0; cpu < (mpsafe ? ncpu : 1); cpu++) {
178 1.1.2.2 thorpej val = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
179 1.1.2.2 thorpej GPC_IMRn_COREx(reg, cpu));
180 1.1.2.2 thorpej val |= __BIT(bit);
181 1.1.2.2 thorpej bus_space_write_4(sc->sc_iot, sc->sc_ioh,
182 1.1.2.2 thorpej GPC_IMRn_COREx(reg, cpu), val);
183 1.1.2.2 thorpej }
184 1.1.2.2 thorpej }
185 1.1.2.2 thorpej
186 1.1.2.2 thorpej static void
187 1.1.2.2 thorpej imx7gpc_unmask(struct imx7gpc_softc *sc, u_int irq, bool mpsafe)
188 1.1.2.2 thorpej {
189 1.1.2.2 thorpej const u_int reg = irq / 32;
190 1.1.2.2 thorpej const u_int bit = irq % 32;
191 1.1.2.2 thorpej uint32_t val;
192 1.1.2.2 thorpej
193 1.1.2.2 thorpej for (u_int cpu = 0; cpu < (mpsafe ? ncpu : 1); cpu++) {
194 1.1.2.2 thorpej val = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
195 1.1.2.2 thorpej GPC_IMRn_COREx(reg, cpu));
196 1.1.2.2 thorpej val &= ~__BIT(bit);
197 1.1.2.2 thorpej bus_space_write_4(sc->sc_iot, sc->sc_ioh,
198 1.1.2.2 thorpej GPC_IMRn_COREx(reg, cpu), val);
199 1.1.2.2 thorpej }
200 1.1.2.2 thorpej }
201 1.1.2.2 thorpej
202 1.1.2.2 thorpej
203 1.1.2.2 thorpej static void *
204 1.1.2.2 thorpej imx7gpc_establish(device_t dev, u_int *specifier, int ipl, int flags,
205 1.1.2.3 thorpej int (*func)(void *), void *arg, const char *xname)
206 1.1.2.2 thorpej {
207 1.1.2.2 thorpej struct imx7gpc_softc * const sc = device_private(dev);
208 1.1.2.2 thorpej void *ih;
209 1.1.2.2 thorpej
210 1.1.2.2 thorpej /* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
211 1.1.2.2 thorpej /* 2nd cell is the interrupt number */
212 1.1.2.2 thorpej /* 3rd cell is flags */
213 1.1.2.2 thorpej
214 1.1.2.2 thorpej const u_int type = be32toh(specifier[0]);
215 1.1.2.2 thorpej const u_int intr = be32toh(specifier[1]);
216 1.1.2.2 thorpej const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
217 1.1.2.2 thorpej const u_int trig = be32toh(specifier[2]) & 0xf;
218 1.1.2.2 thorpej const u_int level = (trig & 0x3) ? IST_EDGE : IST_LEVEL;
219 1.1.2.2 thorpej
220 1.1.2.2 thorpej const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
221 1.1.2.2 thorpej
222 1.1.2.2 thorpej if (type != 0)
223 1.1.2.2 thorpej return NULL; /* Only SPIs are supported */
224 1.1.2.2 thorpej
225 1.1.2.2 thorpej KASSERT(irq >= 32);
226 1.1.2.2 thorpej
227 1.1.2.2 thorpej aprint_debug_dev(dev, "intr establish irq %d, level %d\n", irq, level);
228 1.1.2.2 thorpej
229 1.1.2.3 thorpej ih = intr_establish_xname(irq, ipl, level | mpsafe, func, arg, xname);
230 1.1.2.2 thorpej if (ih != NULL)
231 1.1.2.2 thorpej imx7gpc_unmask(sc, irq - 32, mpsafe == IST_MPSAFE);
232 1.1.2.2 thorpej
233 1.1.2.2 thorpej return ih;
234 1.1.2.2 thorpej }
235 1.1.2.2 thorpej
236 1.1.2.2 thorpej static void
237 1.1.2.2 thorpej imx7gpc_disestablish(device_t dev, void *ih)
238 1.1.2.2 thorpej {
239 1.1.2.2 thorpej struct imx7gpc_softc * const sc = device_private(dev);
240 1.1.2.2 thorpej struct intrsource *is = ih;
241 1.1.2.2 thorpej const u_int irq = is->is_irq;
242 1.1.2.2 thorpej const bool mpsafe = is->is_mpsafe;
243 1.1.2.2 thorpej
244 1.1.2.2 thorpej intr_disestablish(ih);
245 1.1.2.2 thorpej imx7gpc_mask(sc, irq - 32, mpsafe);
246 1.1.2.2 thorpej }
247 1.1.2.2 thorpej
248 1.1.2.2 thorpej static bool
249 1.1.2.2 thorpej imx7gpc_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
250 1.1.2.2 thorpej {
251 1.1.2.2 thorpej /* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
252 1.1.2.2 thorpej /* 2nd cell is the interrupt number */
253 1.1.2.2 thorpej /* 3rd cell is flags */
254 1.1.2.2 thorpej
255 1.1.2.2 thorpej if (!specifier)
256 1.1.2.2 thorpej return false;
257 1.1.2.2 thorpej
258 1.1.2.2 thorpej const u_int type = be32toh(specifier[0]);
259 1.1.2.2 thorpej const u_int intr = be32toh(specifier[1]);
260 1.1.2.2 thorpej const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
261 1.1.2.2 thorpej
262 1.1.2.2 thorpej snprintf(buf, buflen, "irq %d", irq);
263 1.1.2.2 thorpej
264 1.1.2.2 thorpej return true;
265 1.1.2.2 thorpej }
266