imx7_gpc.c revision 1.1.2.2 1 1.1.2.2 thorpej /* $NetBSD: imx7_gpc.c,v 1.1.2.2 2021/01/03 16:34:52 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.2 thorpej __KERNEL_RCSID(0, "$NetBSD: imx7_gpc.c,v 1.1.2.2 2021/01/03 16:34:52 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.2 thorpej int (*)(void *), void *);
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.2 thorpej static int
95 1.1.2.2 thorpej imx7gpc_match(device_t parent, cfdata_t cf, void *aux)
96 1.1.2.2 thorpej {
97 1.1.2.2 thorpej const char * const compatible[] = {
98 1.1.2.2 thorpej "fsl,imx7d-gpc",
99 1.1.2.2 thorpej "fsl,imx8mq-gpc",
100 1.1.2.2 thorpej NULL
101 1.1.2.2 thorpej };
102 1.1.2.2 thorpej struct fdt_attach_args * const faa = aux;
103 1.1.2.2 thorpej
104 1.1.2.2 thorpej return of_match_compatible(faa->faa_phandle, compatible);
105 1.1.2.2 thorpej }
106 1.1.2.2 thorpej
107 1.1.2.2 thorpej static void
108 1.1.2.2 thorpej imx7gpc_attach(device_t parent, device_t self, void *aux)
109 1.1.2.2 thorpej {
110 1.1.2.2 thorpej struct imx7gpc_softc * const sc = device_private(self);
111 1.1.2.2 thorpej struct fdt_attach_args * const faa = aux;
112 1.1.2.2 thorpej const int phandle = faa->faa_phandle;
113 1.1.2.2 thorpej bus_addr_t gpc_addr;
114 1.1.2.2 thorpej bus_size_t gpc_size;
115 1.1.2.2 thorpej int error;
116 1.1.2.2 thorpej
117 1.1.2.2 thorpej KASSERT(ncpu <= IMXGPC_MAXCPUS);
118 1.1.2.2 thorpej
119 1.1.2.2 thorpej if (fdtbus_get_reg(phandle, 0, &gpc_addr, &gpc_size) != 0) {
120 1.1.2.2 thorpej aprint_error(": couldn't get gpc registers\n");
121 1.1.2.2 thorpej return;
122 1.1.2.2 thorpej }
123 1.1.2.2 thorpej
124 1.1.2.2 thorpej sc->sc_dev = self;
125 1.1.2.2 thorpej sc->sc_iot = faa->faa_bst;
126 1.1.2.2 thorpej
127 1.1.2.2 thorpej error = bus_space_map(sc->sc_iot, gpc_addr, gpc_size, 0,
128 1.1.2.2 thorpej &sc->sc_ioh);
129 1.1.2.2 thorpej if (error) {
130 1.1.2.2 thorpej aprint_error(": couldn't map gpc registers: %d\n", error);
131 1.1.2.2 thorpej return;
132 1.1.2.2 thorpej }
133 1.1.2.2 thorpej
134 1.1.2.2 thorpej error = fdtbus_register_interrupt_controller(self, faa->faa_phandle,
135 1.1.2.2 thorpej &imx7gpc_funcs);
136 1.1.2.2 thorpej if (error) {
137 1.1.2.2 thorpej aprint_error(": couldn't register with fdtbus: %d\n", error);
138 1.1.2.2 thorpej return;
139 1.1.2.2 thorpej }
140 1.1.2.2 thorpej
141 1.1.2.2 thorpej aprint_naive("\n");
142 1.1.2.2 thorpej aprint_normal(": General Power Controller\n");
143 1.1.2.2 thorpej
144 1.1.2.2 thorpej /* XXX enable OTG power domains */
145 1.1.2.2 thorpej imx7gpc_powerup(sc, USB_OTG2_SW_PUP_REQ | USB_OTG1_SW_PUP_REQ,
146 1.1.2.2 thorpej OTG2_A53_DOMAIN | OTG1_A53_DOMAIN);
147 1.1.2.2 thorpej }
148 1.1.2.2 thorpej
149 1.1.2.2 thorpej static void
150 1.1.2.2 thorpej imx7gpc_powerup(struct imx7gpc_softc *sc, uint32_t req, uint32_t map)
151 1.1.2.2 thorpej {
152 1.1.2.2 thorpej uint32_t val;
153 1.1.2.2 thorpej
154 1.1.2.2 thorpej val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GPC_PCG_CPU_0_1_MAPPING);
155 1.1.2.2 thorpej val |= map;
156 1.1.2.2 thorpej bus_space_write_4(sc->sc_iot, sc->sc_ioh, GPC_PCG_CPU_0_1_MAPPING, val);
157 1.1.2.2 thorpej
158 1.1.2.2 thorpej val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GPC_PU_PGC_SW_PUP_REQ);
159 1.1.2.2 thorpej val |= req;
160 1.1.2.2 thorpej bus_space_write_4(sc->sc_iot, sc->sc_ioh, GPC_PU_PGC_SW_PUP_REQ, val);
161 1.1.2.2 thorpej
162 1.1.2.2 thorpej delay(5000);
163 1.1.2.2 thorpej
164 1.1.2.2 thorpej val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GPC_PCG_CPU_0_1_MAPPING);
165 1.1.2.2 thorpej val &= ~map;
166 1.1.2.2 thorpej bus_space_write_4(sc->sc_iot, sc->sc_ioh, GPC_PCG_CPU_0_1_MAPPING, val);
167 1.1.2.2 thorpej }
168 1.1.2.2 thorpej
169 1.1.2.2 thorpej static void
170 1.1.2.2 thorpej imx7gpc_mask(struct imx7gpc_softc *sc, u_int irq, bool mpsafe)
171 1.1.2.2 thorpej {
172 1.1.2.2 thorpej const u_int reg = irq / 32;
173 1.1.2.2 thorpej const u_int bit = irq % 32;
174 1.1.2.2 thorpej uint32_t val;
175 1.1.2.2 thorpej
176 1.1.2.2 thorpej for (u_int cpu = 0; cpu < (mpsafe ? ncpu : 1); cpu++) {
177 1.1.2.2 thorpej val = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
178 1.1.2.2 thorpej GPC_IMRn_COREx(reg, cpu));
179 1.1.2.2 thorpej val |= __BIT(bit);
180 1.1.2.2 thorpej bus_space_write_4(sc->sc_iot, sc->sc_ioh,
181 1.1.2.2 thorpej GPC_IMRn_COREx(reg, cpu), val);
182 1.1.2.2 thorpej }
183 1.1.2.2 thorpej }
184 1.1.2.2 thorpej
185 1.1.2.2 thorpej static void
186 1.1.2.2 thorpej imx7gpc_unmask(struct imx7gpc_softc *sc, u_int irq, bool mpsafe)
187 1.1.2.2 thorpej {
188 1.1.2.2 thorpej const u_int reg = irq / 32;
189 1.1.2.2 thorpej const u_int bit = irq % 32;
190 1.1.2.2 thorpej uint32_t val;
191 1.1.2.2 thorpej
192 1.1.2.2 thorpej for (u_int cpu = 0; cpu < (mpsafe ? ncpu : 1); cpu++) {
193 1.1.2.2 thorpej val = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
194 1.1.2.2 thorpej GPC_IMRn_COREx(reg, cpu));
195 1.1.2.2 thorpej val &= ~__BIT(bit);
196 1.1.2.2 thorpej bus_space_write_4(sc->sc_iot, sc->sc_ioh,
197 1.1.2.2 thorpej GPC_IMRn_COREx(reg, cpu), val);
198 1.1.2.2 thorpej }
199 1.1.2.2 thorpej }
200 1.1.2.2 thorpej
201 1.1.2.2 thorpej
202 1.1.2.2 thorpej static void *
203 1.1.2.2 thorpej imx7gpc_establish(device_t dev, u_int *specifier, int ipl, int flags,
204 1.1.2.2 thorpej int (*func)(void *), void *arg)
205 1.1.2.2 thorpej {
206 1.1.2.2 thorpej struct imx7gpc_softc * const sc = device_private(dev);
207 1.1.2.2 thorpej void *ih;
208 1.1.2.2 thorpej
209 1.1.2.2 thorpej /* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
210 1.1.2.2 thorpej /* 2nd cell is the interrupt number */
211 1.1.2.2 thorpej /* 3rd cell is flags */
212 1.1.2.2 thorpej
213 1.1.2.2 thorpej const u_int type = be32toh(specifier[0]);
214 1.1.2.2 thorpej const u_int intr = be32toh(specifier[1]);
215 1.1.2.2 thorpej const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
216 1.1.2.2 thorpej const u_int trig = be32toh(specifier[2]) & 0xf;
217 1.1.2.2 thorpej const u_int level = (trig & 0x3) ? IST_EDGE : IST_LEVEL;
218 1.1.2.2 thorpej
219 1.1.2.2 thorpej const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
220 1.1.2.2 thorpej
221 1.1.2.2 thorpej if (type != 0)
222 1.1.2.2 thorpej return NULL; /* Only SPIs are supported */
223 1.1.2.2 thorpej
224 1.1.2.2 thorpej KASSERT(irq >= 32);
225 1.1.2.2 thorpej
226 1.1.2.2 thorpej aprint_debug_dev(dev, "intr establish irq %d, level %d\n", irq, level);
227 1.1.2.2 thorpej
228 1.1.2.2 thorpej ih = intr_establish(irq, ipl, level | mpsafe, func, arg);
229 1.1.2.2 thorpej if (ih != NULL)
230 1.1.2.2 thorpej imx7gpc_unmask(sc, irq - 32, mpsafe == IST_MPSAFE);
231 1.1.2.2 thorpej
232 1.1.2.2 thorpej return ih;
233 1.1.2.2 thorpej }
234 1.1.2.2 thorpej
235 1.1.2.2 thorpej static void
236 1.1.2.2 thorpej imx7gpc_disestablish(device_t dev, void *ih)
237 1.1.2.2 thorpej {
238 1.1.2.2 thorpej struct imx7gpc_softc * const sc = device_private(dev);
239 1.1.2.2 thorpej struct intrsource *is = ih;
240 1.1.2.2 thorpej const u_int irq = is->is_irq;
241 1.1.2.2 thorpej const bool mpsafe = is->is_mpsafe;
242 1.1.2.2 thorpej
243 1.1.2.2 thorpej intr_disestablish(ih);
244 1.1.2.2 thorpej imx7gpc_mask(sc, irq - 32, mpsafe);
245 1.1.2.2 thorpej }
246 1.1.2.2 thorpej
247 1.1.2.2 thorpej static bool
248 1.1.2.2 thorpej imx7gpc_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
249 1.1.2.2 thorpej {
250 1.1.2.2 thorpej /* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
251 1.1.2.2 thorpej /* 2nd cell is the interrupt number */
252 1.1.2.2 thorpej /* 3rd cell is flags */
253 1.1.2.2 thorpej
254 1.1.2.2 thorpej if (!specifier)
255 1.1.2.2 thorpej return false;
256 1.1.2.2 thorpej
257 1.1.2.2 thorpej const u_int type = be32toh(specifier[0]);
258 1.1.2.2 thorpej const u_int intr = be32toh(specifier[1]);
259 1.1.2.2 thorpej const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
260 1.1.2.2 thorpej
261 1.1.2.2 thorpej snprintf(buf, buflen, "irq %d", irq);
262 1.1.2.2 thorpej
263 1.1.2.2 thorpej return true;
264 1.1.2.2 thorpej }
265