imx6_gpc.c revision 1.3 1 1.3 thorpej /* $NetBSD: imx6_gpc.c,v 1.3 2021/01/27 03:10:20 thorpej Exp $ */
2 1.1 skrll
3 1.1 skrll /*-
4 1.1 skrll * Copyright (c) 2019 Genetec Corporation. All rights reserved.
5 1.1 skrll * Written by Hashimoto Kenichi for Genetec Corporation.
6 1.1 skrll *
7 1.1 skrll * Redistribution and use in source and binary forms, with or without
8 1.1 skrll * modification, are permitted provided that the following conditions
9 1.1 skrll * are met:
10 1.1 skrll * 1. Redistributions of source code must retain the above copyright
11 1.1 skrll * notice, this list of conditions and the following disclaimer.
12 1.1 skrll * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 skrll * notice, this list of conditions and the following disclaimer in the
14 1.1 skrll * documentation and/or other materials provided with the distribution.
15 1.1 skrll *
16 1.1 skrll * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 skrll * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 skrll * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 skrll * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 skrll * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 skrll * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 skrll * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 skrll * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 skrll * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 skrll * SUCH DAMAGE.
27 1.1 skrll */
28 1.1 skrll
29 1.1 skrll #include <sys/cdefs.h>
30 1.3 thorpej __KERNEL_RCSID(0, "$NetBSD: imx6_gpc.c,v 1.3 2021/01/27 03:10:20 thorpej Exp $");
31 1.1 skrll
32 1.1 skrll #include "opt_fdt.h"
33 1.1 skrll
34 1.1 skrll #include <sys/param.h>
35 1.1 skrll #include <sys/bus.h>
36 1.1 skrll #include <sys/device.h>
37 1.1 skrll
38 1.1 skrll #include <dev/fdt/fdtvar.h>
39 1.1 skrll
40 1.1 skrll #include <arm/cortex/gic_intr.h>
41 1.1 skrll #include <arm/nxp/imx6_gpcreg.h>
42 1.1 skrll
43 1.1 skrll struct imxgpc_softc {
44 1.1 skrll device_t sc_dev;
45 1.1 skrll
46 1.1 skrll bus_space_tag_t sc_iot;
47 1.1 skrll bus_space_handle_t sc_ioh;
48 1.1 skrll };
49 1.1 skrll
50 1.1 skrll static int imxgpc_match(device_t, struct cfdata *, void *);
51 1.1 skrll static void imxgpc_attach(device_t, device_t, void *);
52 1.1 skrll
53 1.1 skrll static void *imxgpc_establish(device_t, u_int *, int, int,
54 1.2 jmcneill int (*)(void *), void *, const char *);
55 1.1 skrll static void imxgpc_disestablish(device_t, void *);
56 1.1 skrll static bool imxgpc_intrstr(device_t, u_int *, char *, size_t);
57 1.1 skrll
58 1.1 skrll struct fdtbus_interrupt_controller_func imxgpc_funcs = {
59 1.1 skrll .establish = imxgpc_establish,
60 1.1 skrll .disestablish = imxgpc_disestablish,
61 1.1 skrll .intrstr = imxgpc_intrstr
62 1.1 skrll };
63 1.1 skrll
64 1.1 skrll CFATTACH_DECL_NEW(imxgpc, sizeof(struct imxgpc_softc),
65 1.1 skrll imxgpc_match, imxgpc_attach, NULL, NULL);
66 1.1 skrll
67 1.3 thorpej static const struct device_compatible_entry compat_data[] = {
68 1.3 thorpej { .compat = "fsl,imx6q-gpc" },
69 1.3 thorpej DEVICE_COMPAT_EOL
70 1.3 thorpej };
71 1.3 thorpej
72 1.1 skrll static int
73 1.1 skrll imxgpc_match(device_t parent, cfdata_t cf, void *aux)
74 1.1 skrll {
75 1.1 skrll struct fdt_attach_args * const faa = aux;
76 1.1 skrll
77 1.3 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
78 1.1 skrll }
79 1.1 skrll
80 1.1 skrll static void
81 1.1 skrll imxgpc_attach(device_t parent, device_t self, void *aux)
82 1.1 skrll {
83 1.1 skrll struct imxgpc_softc * const sc = device_private(self);
84 1.1 skrll struct fdt_attach_args * const faa = aux;
85 1.1 skrll const int phandle = faa->faa_phandle;
86 1.1 skrll bus_addr_t gpc_addr;
87 1.1 skrll bus_size_t gpc_size;
88 1.1 skrll int error;
89 1.1 skrll
90 1.1 skrll if (fdtbus_get_reg(phandle, 0, &gpc_addr, &gpc_size) != 0) {
91 1.1 skrll aprint_error(": couldn't get gpc registers\n");
92 1.1 skrll return;
93 1.1 skrll }
94 1.1 skrll
95 1.1 skrll sc->sc_dev = self;
96 1.1 skrll sc->sc_iot = faa->faa_bst;
97 1.1 skrll
98 1.1 skrll error = bus_space_map(sc->sc_iot, gpc_addr, gpc_size, 0,
99 1.1 skrll &sc->sc_ioh);
100 1.1 skrll if (error) {
101 1.1 skrll aprint_error(": couldn't map gpc registers: %d\n", error);
102 1.1 skrll return;
103 1.1 skrll }
104 1.1 skrll
105 1.1 skrll error = fdtbus_register_interrupt_controller(self, faa->faa_phandle,
106 1.1 skrll &imxgpc_funcs);
107 1.1 skrll if (error) {
108 1.1 skrll aprint_error(": couldn't register with fdtbus: %d\n", error);
109 1.1 skrll return;
110 1.1 skrll }
111 1.1 skrll
112 1.1 skrll aprint_naive("\n");
113 1.1 skrll aprint_normal(": General Power Controller\n");
114 1.1 skrll
115 1.1 skrll return;
116 1.1 skrll }
117 1.1 skrll
118 1.1 skrll static void *
119 1.1 skrll imxgpc_establish(device_t dev, u_int *specifier, int ipl, int flags,
120 1.2 jmcneill int (*func)(void *), void *arg, const char *xname)
121 1.1 skrll {
122 1.1 skrll /* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
123 1.1 skrll /* 2nd cell is the interrupt number */
124 1.1 skrll /* 3rd cell is flags */
125 1.1 skrll
126 1.1 skrll const u_int type = be32toh(specifier[0]);
127 1.1 skrll const u_int intr = be32toh(specifier[1]);
128 1.1 skrll const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
129 1.1 skrll const u_int trig = be32toh(specifier[2]) & 0xf;
130 1.1 skrll const u_int level = (trig & 0x3) ? IST_EDGE : IST_LEVEL;
131 1.1 skrll
132 1.1 skrll const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
133 1.1 skrll
134 1.1 skrll aprint_debug_dev(dev, "intr establish irq %d, level %d\n", irq, level);
135 1.2 jmcneill return intr_establish_xname(irq, ipl, level | mpsafe, func, arg,
136 1.2 jmcneill xname);
137 1.1 skrll }
138 1.1 skrll
139 1.1 skrll static void
140 1.1 skrll imxgpc_disestablish(device_t dev, void *ih)
141 1.1 skrll {
142 1.1 skrll intr_disestablish(ih);
143 1.1 skrll }
144 1.1 skrll
145 1.1 skrll static bool
146 1.1 skrll imxgpc_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
147 1.1 skrll {
148 1.1 skrll /* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
149 1.1 skrll /* 2nd cell is the interrupt number */
150 1.1 skrll /* 3rd cell is flags */
151 1.1 skrll
152 1.1 skrll if (!specifier)
153 1.1 skrll return false;
154 1.1 skrll
155 1.1 skrll const u_int type = be32toh(specifier[0]);
156 1.1 skrll const u_int intr = be32toh(specifier[1]);
157 1.1 skrll const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
158 1.1 skrll
159 1.1 skrll snprintf(buf, buflen, "irq %d", irq);
160 1.1 skrll
161 1.1 skrll return true;
162 1.1 skrll }
163