imx6_gpc.c revision 1.2 1 1.2 jmcneill /* $NetBSD: imx6_gpc.c,v 1.2 2021/01/15 00:38:22 jmcneill 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.2 jmcneill __KERNEL_RCSID(0, "$NetBSD: imx6_gpc.c,v 1.2 2021/01/15 00:38:22 jmcneill 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.1 skrll static int
68 1.1 skrll imxgpc_match(device_t parent, cfdata_t cf, void *aux)
69 1.1 skrll {
70 1.1 skrll const char * const compatible[] = { "fsl,imx6q-gpc", NULL };
71 1.1 skrll struct fdt_attach_args * const faa = aux;
72 1.1 skrll
73 1.1 skrll return of_match_compatible(faa->faa_phandle, compatible);
74 1.1 skrll }
75 1.1 skrll
76 1.1 skrll static void
77 1.1 skrll imxgpc_attach(device_t parent, device_t self, void *aux)
78 1.1 skrll {
79 1.1 skrll struct imxgpc_softc * const sc = device_private(self);
80 1.1 skrll struct fdt_attach_args * const faa = aux;
81 1.1 skrll const int phandle = faa->faa_phandle;
82 1.1 skrll bus_addr_t gpc_addr;
83 1.1 skrll bus_size_t gpc_size;
84 1.1 skrll int error;
85 1.1 skrll
86 1.1 skrll if (fdtbus_get_reg(phandle, 0, &gpc_addr, &gpc_size) != 0) {
87 1.1 skrll aprint_error(": couldn't get gpc registers\n");
88 1.1 skrll return;
89 1.1 skrll }
90 1.1 skrll
91 1.1 skrll sc->sc_dev = self;
92 1.1 skrll sc->sc_iot = faa->faa_bst;
93 1.1 skrll
94 1.1 skrll error = bus_space_map(sc->sc_iot, gpc_addr, gpc_size, 0,
95 1.1 skrll &sc->sc_ioh);
96 1.1 skrll if (error) {
97 1.1 skrll aprint_error(": couldn't map gpc registers: %d\n", error);
98 1.1 skrll return;
99 1.1 skrll }
100 1.1 skrll
101 1.1 skrll error = fdtbus_register_interrupt_controller(self, faa->faa_phandle,
102 1.1 skrll &imxgpc_funcs);
103 1.1 skrll if (error) {
104 1.1 skrll aprint_error(": couldn't register with fdtbus: %d\n", error);
105 1.1 skrll return;
106 1.1 skrll }
107 1.1 skrll
108 1.1 skrll aprint_naive("\n");
109 1.1 skrll aprint_normal(": General Power Controller\n");
110 1.1 skrll
111 1.1 skrll return;
112 1.1 skrll }
113 1.1 skrll
114 1.1 skrll static void *
115 1.1 skrll imxgpc_establish(device_t dev, u_int *specifier, int ipl, int flags,
116 1.2 jmcneill int (*func)(void *), void *arg, const char *xname)
117 1.1 skrll {
118 1.1 skrll /* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
119 1.1 skrll /* 2nd cell is the interrupt number */
120 1.1 skrll /* 3rd cell is flags */
121 1.1 skrll
122 1.1 skrll const u_int type = be32toh(specifier[0]);
123 1.1 skrll const u_int intr = be32toh(specifier[1]);
124 1.1 skrll const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
125 1.1 skrll const u_int trig = be32toh(specifier[2]) & 0xf;
126 1.1 skrll const u_int level = (trig & 0x3) ? IST_EDGE : IST_LEVEL;
127 1.1 skrll
128 1.1 skrll const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
129 1.1 skrll
130 1.1 skrll aprint_debug_dev(dev, "intr establish irq %d, level %d\n", irq, level);
131 1.2 jmcneill return intr_establish_xname(irq, ipl, level | mpsafe, func, arg,
132 1.2 jmcneill xname);
133 1.1 skrll }
134 1.1 skrll
135 1.1 skrll static void
136 1.1 skrll imxgpc_disestablish(device_t dev, void *ih)
137 1.1 skrll {
138 1.1 skrll intr_disestablish(ih);
139 1.1 skrll }
140 1.1 skrll
141 1.1 skrll static bool
142 1.1 skrll imxgpc_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
143 1.1 skrll {
144 1.1 skrll /* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
145 1.1 skrll /* 2nd cell is the interrupt number */
146 1.1 skrll /* 3rd cell is flags */
147 1.1 skrll
148 1.1 skrll if (!specifier)
149 1.1 skrll return false;
150 1.1 skrll
151 1.1 skrll const u_int type = be32toh(specifier[0]);
152 1.1 skrll const u_int intr = be32toh(specifier[1]);
153 1.1 skrll const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
154 1.1 skrll
155 1.1 skrll snprintf(buf, buflen, "irq %d", irq);
156 1.1 skrll
157 1.1 skrll return true;
158 1.1 skrll }
159