exynos_combiner.c revision 1.2 1 1.2 marty /* $NetBSD: exynos_combiner.c,v 1.2 2015/12/21 04:58:50 marty Exp $ */
2 1.1 marty
3 1.1 marty /*-
4 1.1 marty * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 1.1 marty * All rights reserved.
6 1.1 marty *
7 1.1 marty * This code is derived from software contributed to The NetBSD Foundation
8 1.1 marty * by Marty Fouts
9 1.1 marty *
10 1.1 marty * Redistribution and use in source and binary forms, with or without
11 1.1 marty * modification, are permitted provided that the following conditions
12 1.1 marty * are met:
13 1.1 marty * 1. Redistributions of source code must retain the above copyright
14 1.1 marty * notice, this list of conditions and the following disclaimer.
15 1.1 marty * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 marty * notice, this list of conditions and the following disclaimer in the
17 1.1 marty * documentation and/or other materials provided with the distribution.
18 1.1 marty *
19 1.1 marty * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 marty * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 marty * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 marty * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 marty * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 marty * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 marty * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 marty * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 marty * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 marty * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 marty * POSSIBILITY OF SUCH DAMAGE.
30 1.1 marty */
31 1.1 marty
32 1.1 marty #include "opt_exynos.h"
33 1.1 marty #include "opt_arm_debug.h"
34 1.1 marty #include "gpio.h"
35 1.1 marty
36 1.1 marty #include <sys/cdefs.h>
37 1.2 marty __KERNEL_RCSID(1, "$NetBSD: exynos_combiner.c,v 1.2 2015/12/21 04:58:50 marty Exp $");
38 1.1 marty
39 1.1 marty #include <sys/param.h>
40 1.1 marty #include <sys/bus.h>
41 1.1 marty #include <sys/device.h>
42 1.1 marty #include <sys/intr.h>
43 1.1 marty #include <sys/systm.h>
44 1.1 marty #include <sys/kmem.h>
45 1.1 marty
46 1.1 marty #include <arm/cortex/gic_intr.h>
47 1.1 marty
48 1.1 marty #include <arm/samsung/exynos_reg.h>
49 1.1 marty #include <arm/samsung/exynos_intr.h>
50 1.1 marty
51 1.1 marty #include <dev/fdt/fdtvar.h>
52 1.1 marty
53 1.1 marty struct exynos_combiner_softc {
54 1.1 marty device_t sc_dev;
55 1.1 marty int sc_phandle;
56 1.1 marty
57 1.1 marty };
58 1.1 marty
59 1.1 marty static int exynos_combiner_match(device_t, cfdata_t, void *);
60 1.1 marty static void exynos_combiner_attach(device_t, device_t, void *);
61 1.1 marty
62 1.1 marty static void * exynos_combiner_establish(device_t, int, u_int, int, int,
63 1.1 marty int (*)(void *), void *);
64 1.1 marty static void exynos_combiner_disestablish(device_t, void *);
65 1.1 marty static bool exynos_combiner_intrstr(device_t, int, u_int, char *, size_t);
66 1.1 marty
67 1.1 marty struct fdtbus_interrupt_controller_func exynos_combiner_funcs = {
68 1.1 marty .establish = exynos_combiner_establish,
69 1.1 marty .disestablish = exynos_combiner_disestablish,
70 1.1 marty .intrstr = exynos_combiner_intrstr
71 1.1 marty };
72 1.1 marty
73 1.1 marty CFATTACH_DECL_NEW(exynos_intr, sizeof(struct exynos_combiner_softc),
74 1.1 marty exynos_combiner_match, exynos_combiner_attach, NULL, NULL);
75 1.1 marty
76 1.1 marty static int
77 1.1 marty exynos_combiner_match(device_t parent, cfdata_t cf, void *aux)
78 1.1 marty {
79 1.1 marty const char * const compatible[] = { "samsung,exynos4210-combiner",
80 1.1 marty NULL };
81 1.1 marty struct fdt_attach_args * const faa = aux;
82 1.1 marty return of_match_compatible(faa->faa_phandle, compatible);
83 1.1 marty }
84 1.1 marty
85 1.1 marty static void
86 1.1 marty exynos_combiner_attach(device_t parent, device_t self, void *aux)
87 1.1 marty {
88 1.1 marty struct exynos_combiner_softc * const sc
89 1.1 marty = kmem_zalloc(sizeof(*sc), KM_SLEEP);
90 1.1 marty struct fdt_attach_args * const faa = aux;
91 1.1 marty bus_addr_t addr;
92 1.1 marty bus_size_t size;
93 1.1 marty int error;
94 1.1 marty
95 1.1 marty if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
96 1.1 marty aprint_error(": couldn't get registers\n");
97 1.1 marty return;
98 1.1 marty }
99 1.1 marty
100 1.1 marty sc->sc_dev = self;
101 1.1 marty sc->sc_phandle = faa->faa_phandle;
102 1.1 marty error = fdtbus_register_interrupt_controller(self, faa->faa_phandle,
103 1.1 marty &exynos_combiner_funcs);
104 1.1 marty if (error) {
105 1.1 marty aprint_error(": couldn't register with fdtbus: %d\n", error);
106 1.1 marty return;
107 1.1 marty }
108 1.1 marty
109 1.1 marty
110 1.1 marty aprint_normal(" @ 0x%08x: interrupt combiner, NOT IMPLEMENTED",
111 1.1 marty (uint)addr);
112 1.1 marty aprint_naive("\n");
113 1.1 marty aprint_normal("\n");
114 1.1 marty
115 1.1 marty }
116 1.1 marty
117 1.1 marty
118 1.1 marty static void *
119 1.1 marty exynos_combiner_establish(device_t dev, int phandle, u_int index, int ipl,
120 1.1 marty int flags,
121 1.1 marty int (*func)(void *), void *arg)
122 1.1 marty {
123 1.1 marty struct exynos_combiner_softc * const sc = device_private(dev);
124 1.1 marty int iflags = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
125 1.1 marty u_int *interrupts;
126 1.1 marty int interrupt_cells, len;
127 1.1 marty
128 1.1 marty if (of_getprop_uint32(sc->sc_phandle, "#interrupt-cells",
129 1.1 marty &interrupt_cells)) {
130 1.1 marty return NULL;
131 1.1 marty }
132 1.1 marty
133 1.1 marty len = OF_getproplen(phandle, "interrupts");
134 1.1 marty if (len <= 0)
135 1.1 marty return NULL;
136 1.1 marty
137 1.1 marty const u_int clen = interrupt_cells * 4;
138 1.1 marty const u_int nintr = len / interrupt_cells;
139 1.1 marty
140 1.1 marty if (index >= nintr)
141 1.1 marty return NULL;
142 1.1 marty
143 1.1 marty interrupts = kmem_alloc(len, KM_SLEEP);
144 1.1 marty
145 1.1 marty if (OF_getprop(phandle, "interrupts", interrupts, len) != len) {
146 1.1 marty kmem_free(interrupts, len);
147 1.1 marty return NULL;
148 1.1 marty }
149 1.1 marty
150 1.1 marty /* 1st cell is the interrupt type; */
151 1.1 marty /* 2nd cell is the interrupt number */
152 1.1 marty /* 3rd cell is flags */
153 1.1 marty
154 1.1 marty const u_int type = be32toh(interrupts[index * clen + 0]);
155 1.1 marty const u_int intr = be32toh(interrupts[index * clen + 1]);
156 1.1 marty const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
157 1.1 marty const u_int trig = be32toh(interrupts[index * clen + 2]) & 0xf;
158 1.1 marty const u_int level = (trig & 0x3) ? IST_EDGE : IST_LEVEL;
159 1.1 marty
160 1.1 marty kmem_free(interrupts, len);
161 1.1 marty
162 1.1 marty return intr_establish(irq, ipl, level | iflags, func, arg);
163 1.1 marty }
164 1.1 marty
165 1.1 marty static void
166 1.1 marty exynos_combiner_disestablish(device_t dev, void *ih)
167 1.1 marty {
168 1.1 marty intr_disestablish(ih);
169 1.1 marty }
170 1.1 marty
171 1.1 marty static bool
172 1.1 marty exynos_combiner_intrstr(device_t dev, int phandle, u_int index, char *buf,
173 1.1 marty size_t buflen)
174 1.1 marty {
175 1.1 marty struct exynos_combiner_softc * const sc = device_private(dev);
176 1.1 marty u_int *interrupts;
177 1.1 marty int interrupt_cells, len;
178 1.1 marty
179 1.1 marty if (of_getprop_uint32(sc->sc_phandle, "#interrupt-cells",
180 1.1 marty &interrupt_cells)) {
181 1.1 marty return false;
182 1.1 marty }
183 1.1 marty
184 1.1 marty len = OF_getproplen(phandle, "interrupts");
185 1.1 marty if (len <= 0) {
186 1.1 marty return false;
187 1.1 marty }
188 1.1 marty
189 1.1 marty const u_int clen = interrupt_cells * 4;
190 1.1 marty const u_int nintr = len / interrupt_cells;
191 1.1 marty
192 1.1 marty if (index >= nintr) {
193 1.1 marty return false;
194 1.1 marty }
195 1.1 marty
196 1.1 marty interrupts = kmem_alloc(len, KM_SLEEP);
197 1.1 marty
198 1.1 marty if (OF_getprop(phandle, "interrupts", interrupts, len) != len) {
199 1.1 marty kmem_free(interrupts, len);
200 1.1 marty return false;
201 1.1 marty }
202 1.1 marty
203 1.1 marty /* 1st cell is the interrupt type; */
204 1.1 marty /* 2nd cell is the interrupt number */
205 1.1 marty /* 3rd cell is flags */
206 1.1 marty
207 1.1 marty const u_int type = be32toh(interrupts[index * clen + 0]);
208 1.1 marty const u_int intr = be32toh(interrupts[index * clen + 1]);
209 1.1 marty const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
210 1.1 marty
211 1.1 marty kmem_free(interrupts, len);
212 1.1 marty
213 1.1 marty snprintf(buf, buflen, "LIC irq %d", irq);
214 1.1 marty
215 1.1 marty return true;
216 1.1 marty }
217