armperiph.c revision 1.8 1 /*-
2 * Copyright (c) 2012 The NetBSD Foundation, Inc.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to The NetBSD Foundation
6 * by Matt Thomas of 3am Software Foundry.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "locators.h"
31
32 #include <sys/cdefs.h>
33
34 __KERNEL_RCSID(1, "$NetBSD: armperiph.c,v 1.8 2015/02/28 09:34:35 skrll Exp $");
35
36 #include <sys/param.h>
37 #include <sys/device.h>
38
39 #include "ioconf.h"
40
41 #include <arm/mainbus/mainbus.h>
42 #include <arm/cortex/mpcore_var.h>
43
44 static int armperiph_match(device_t, cfdata_t, void *);
45 static void armperiph_attach(device_t, device_t, void *);
46
47 static bool attached;
48
49 struct armperiph_softc {
50 device_t sc_dev;
51 bus_space_tag_t sc_memt;
52 bus_space_handle_t sc_memh;
53 };
54
55 struct armperiph_info {
56 const char pi_name[12];
57 bus_size_t pi_off1;
58 bus_size_t pi_off2;
59 };
60
61 #ifdef CPU_CORTEXA5
62 static const struct armperiph_info a5_devices[] = {
63 { "armscu", 0x0000, 0 },
64 { "armgic", 0x1000, 0x0100 },
65 { "a9tmr", 0x0200, 0 },
66 { "a9wdt", 0x0600, 0 },
67 { "arml2cc", 0, 0 }, /* external; needs "offset" property */
68 { "", 0, 0 },
69 };
70 #endif
71
72 #ifdef CPU_CORTEXA7
73 static const struct armperiph_info a7_devices[] = {
74 { "armgic", 0x1000, 0x2000 },
75 { "armgtmr", 0, 0 },
76 { "", 0, 0 },
77 };
78 #endif
79
80 #ifdef CPU_CORTEXA9
81 static const struct armperiph_info a9_devices[] = {
82 { "armscu", 0x0000, 0 },
83 { "arml2cc", 0x2000, 0 },
84 { "armgic", 0x1000, 0x0100 },
85 { "a9tmr", 0x0200, 0 },
86 { "a9wdt", 0x0600, 0 },
87 { "", 0, 0 },
88 };
89 #endif
90
91 #ifdef CPU_CORTEXA15
92 static const struct armperiph_info a15_devices[] = {
93 { "armgic", 0x1000, 0x2000 },
94 { "armgtmr", 0, 0 },
95 { "", 0, 0 },
96 };
97 #endif
98
99
100 static const struct mpcore_config {
101 const struct armperiph_info *cfg_devices;
102 uint32_t cfg_cpuid;
103 uint32_t cfg_cbar_size;
104 } configs[] = {
105 #ifdef CPU_CORTEXA5
106 { a5_devices, 0x410fc050, 2*4096 },
107 #endif
108 #ifdef CPU_CORTEXA7
109 { a7_devices, 0x410fc070, 8*4096 },
110 #endif
111 #ifdef CPU_CORTEXA9
112 { a9_devices, 0x410fc090, 3*4096 },
113 #endif
114 #ifdef CPU_CORTEXA15
115 { a15_devices, 0x410fc0f0, 8*4096 },
116 #endif
117 };
118
119 static const struct mpcore_config *
120 armperiph_find_config(void)
121 {
122 const uint32_t arm_cpuid = curcpu()->ci_arm_cpuid & 0xff0ff0f0;
123 for (size_t i = 0; i < __arraycount(configs); i++) {
124 if (arm_cpuid == configs[i].cfg_cpuid) {
125 return configs + i;
126 }
127 }
128
129 return NULL;
130 }
131
132 CFATTACH_DECL_NEW(armperiph, sizeof(struct armperiph_softc),
133 armperiph_match, armperiph_attach, NULL, NULL);
134
135 static int
136 armperiph_match(device_t parent, cfdata_t cf, void *aux)
137 {
138 struct mainbus_attach_args * const mb = aux;
139 const int base = cf->cf_loc[MAINBUSCF_BASE];
140 const int size = cf->cf_loc[MAINBUSCF_SIZE];
141 const int dack = cf->cf_loc[MAINBUSCF_DACK];
142 const int irq = cf->cf_loc[MAINBUSCF_IRQ];
143 const int intrbase = cf->cf_loc[MAINBUSCF_INTRBASE];
144
145 if (attached)
146 return 0;
147
148 if (base != MAINBUSCF_BASE_DEFAULT || base != mb->mb_iobase
149 || size != MAINBUSCF_SIZE_DEFAULT || size != mb->mb_iosize
150 || dack != MAINBUSCF_DACK_DEFAULT || dack != mb->mb_drq
151 || irq != MAINBUSCF_IRQ_DEFAULT || irq != mb->mb_irq
152 || intrbase != MAINBUSCF_INTRBASE_DEFAULT
153 || intrbase != mb->mb_intrbase)
154 return 0;
155
156 if (!CPU_ID_CORTEX_P(curcpu()->ci_arm_cpuid))
157 return 0;
158
159 if (armreg_cbar_read() == 0)
160 return 0;
161
162 if (armperiph_find_config() == NULL)
163 return 0;
164
165 return 1;
166 }
167
168 static void
169 armperiph_attach(device_t parent, device_t self, void *aux)
170 {
171 struct armperiph_softc * const sc = device_private(self);
172 struct mainbus_attach_args * const mb = aux;
173 bus_addr_t cbar = armreg_cbar_read();
174 const struct mpcore_config * const cfg = armperiph_find_config();
175 prop_dictionary_t prop = device_properties(self);
176 uint32_t cbar_override;
177
178 if (prop_dictionary_get_uint32(prop, "cbar", &cbar_override))
179 cbar = (bus_addr_t)cbar_override;
180
181 /*
182 * The normal mainbus bus space will not work for us so the port's
183 * device_register must have replaced it with one that will work.
184 */
185 sc->sc_dev = self;
186 sc->sc_memt = mb->mb_iot;
187
188 int error = bus_space_map(sc->sc_memt, cbar, cfg->cfg_cbar_size, 0,
189 &sc->sc_memh);
190 if (error) {
191 aprint_normal(": error mapping registers at %#lx: %d\n",
192 cbar, error);
193 return;
194 }
195 aprint_normal("\n");
196
197 /*
198 * Let's try to attach any children we may have.
199 */
200 for (size_t i = 0; cfg->cfg_devices[i].pi_name[0] != 0; i++) {
201 struct mpcore_attach_args mpcaa = {
202 .mpcaa_name = cfg->cfg_devices[i].pi_name,
203 .mpcaa_memt = sc->sc_memt,
204 .mpcaa_memh = sc->sc_memh,
205 .mpcaa_off1 = cfg->cfg_devices[i].pi_off1,
206 .mpcaa_off2 = cfg->cfg_devices[i].pi_off2,
207 };
208 if (strcmp(mpcaa.mpcaa_name, "armgtmr") == 0) {
209 mpcaa.mpcaa_irq = IRQ_GTMR_PPI_VTIMER;
210 }
211
212 config_found(self, &mpcaa, NULL);
213 }
214 }
215