armperiph.c revision 1.4.10.1 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.4.10.1 2015/03/11 20:22:55 snj 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 #include <arm/cortex/gtmr_intr.h>
44
45 static int armperiph_match(device_t, cfdata_t, void *);
46 static void armperiph_attach(device_t, device_t, void *);
47
48 static bool attached;
49
50 struct armperiph_softc {
51 device_t sc_dev;
52 bus_space_tag_t sc_memt;
53 bus_space_handle_t sc_memh;
54 };
55
56 struct armperiph_info {
57 const char pi_name[12];
58 bus_size_t pi_off1;
59 bus_size_t pi_off2;
60 };
61
62 #ifdef CPU_CORTEXA5
63 static const struct armperiph_info a5_devices[] = {
64 { "armscu", 0x0000, 0 },
65 { "armgic", 0x1000, 0x0100 },
66 { "a9tmr", 0x0200, 0 },
67 { "", 0, 0 },
68 };
69 #endif
70
71 #ifdef CPU_CORTEXA7
72 static const struct armperiph_info a7_devices[] = {
73 { "armgic", 0x1000, 0x2000 },
74 { "armgtmr", 0, 0 },
75 { "", 0, 0 },
76 };
77 #endif
78
79 #ifdef CPU_CORTEXA9
80 static const struct armperiph_info a9_devices[] = {
81 { "armscu", 0x0000, 0 },
82 { "arml2cc", 0x2000, 0 },
83 { "armgic", 0x1000, 0x0100 },
84 { "a9tmr", 0x0200, 0 },
85 { "a9wdt", 0x0600, 0 },
86 { "", 0, 0 },
87 };
88 #endif
89
90 #ifdef CPU_CORTEXA15
91 static const struct armperiph_info a15_devices[] = {
92 { "armgic", 0x1000, 0x2000 },
93 { "armgtmr", 0, 0 },
94 { "", 0, 0 },
95 };
96 #endif
97
98
99 static const struct mpcore_config {
100 const struct armperiph_info *cfg_devices;
101 uint32_t cfg_cpuid;
102 uint32_t cfg_cbar_size;
103 } configs[] = {
104 #ifdef CPU_CORTEXA5
105 { a5_devices, 0x410fc050, 2*4096 },
106 #endif
107 #ifdef CPU_CORTEXA7
108 { a7_devices, 0x410fc070, 8*4096 },
109 #endif
110 #ifdef CPU_CORTEXA9
111 { a9_devices, 0x410fc090, 3*4096 },
112 #endif
113 #ifdef CPU_CORTEXA15
114 { a15_devices, 0x410fc0f0, 8*4096 },
115 #endif
116 };
117
118 static const struct mpcore_config *
119 armperiph_find_config(void)
120 {
121 const uint32_t arm_cpuid = curcpu()->ci_arm_cpuid & 0xff0ff0f0;
122 for (size_t i = 0; i < __arraycount(configs); i++) {
123 if (arm_cpuid == configs[i].cfg_cpuid) {
124 return configs + i;
125 }
126 }
127
128 return NULL;
129 }
130
131 CFATTACH_DECL_NEW(armperiph, sizeof(struct armperiph_softc),
132 armperiph_match, armperiph_attach, NULL, NULL);
133
134 static int
135 armperiph_match(device_t parent, cfdata_t cf, void *aux)
136 {
137 struct mainbus_attach_args * const mb = aux;
138 const int base = cf->cf_loc[MAINBUSCF_BASE];
139 const int size = cf->cf_loc[MAINBUSCF_SIZE];
140 const int dack = cf->cf_loc[MAINBUSCF_DACK];
141 const int irq = cf->cf_loc[MAINBUSCF_IRQ];
142 const int intrbase = cf->cf_loc[MAINBUSCF_INTRBASE];
143
144 if (attached)
145 return 0;
146
147 if (base != MAINBUSCF_BASE_DEFAULT || base != mb->mb_iobase
148 || size != MAINBUSCF_SIZE_DEFAULT || size != mb->mb_iosize
149 || dack != MAINBUSCF_DACK_DEFAULT || dack != mb->mb_drq
150 || irq != MAINBUSCF_IRQ_DEFAULT || irq != mb->mb_irq
151 || intrbase != MAINBUSCF_INTRBASE_DEFAULT
152 || intrbase != mb->mb_intrbase)
153 return 0;
154
155 if (!CPU_ID_CORTEX_P(curcpu()->ci_arm_cpuid))
156 return 0;
157
158 if (armreg_cbar_read() == 0)
159 return 0;
160
161 if (armperiph_find_config() == NULL)
162 return 0;
163
164 return 1;
165 }
166
167 static void
168 armperiph_attach(device_t parent, device_t self, void *aux)
169 {
170 struct armperiph_softc * const sc = device_private(self);
171 struct mainbus_attach_args * const mb = aux;
172 bus_addr_t cbar = armreg_cbar_read();
173 const struct mpcore_config * const cfg = armperiph_find_config();
174
175 /*
176 * The normal mainbus bus space will not work for us so the port's
177 * device_register must have replaced it with one that will work.
178 */
179 sc->sc_dev = self;
180 sc->sc_memt = mb->mb_iot;
181
182 int error = bus_space_map(sc->sc_memt, cbar, cfg->cfg_cbar_size, 0,
183 &sc->sc_memh);
184 if (error) {
185 aprint_normal(": error mapping registers at %#lx: %d\n",
186 cbar, error);
187 return;
188 }
189 aprint_normal("\n");
190
191 /*
192 * Let's try to attach any children we may have.
193 */
194 for (size_t i = 0; cfg->cfg_devices[i].pi_name[0] != 0; i++) {
195 struct mpcore_attach_args mpcaa = {
196 .mpcaa_name = cfg->cfg_devices[i].pi_name,
197 .mpcaa_memt = sc->sc_memt,
198 .mpcaa_memh = sc->sc_memh,
199 .mpcaa_off1 = cfg->cfg_devices[i].pi_off1,
200 .mpcaa_off2 = cfg->cfg_devices[i].pi_off2,
201 };
202 #if defined(CPU_CORTEXA7) || defined(CPU_CORTEXA15)
203 if (strcmp(mpcaa.mpcaa_name, "armgtmr") == 0) {
204 mpcaa.mpcaa_irq = IRQ_GTMR_PPI_VTIMER;
205 }
206 #endif
207
208 config_found(self, &mpcaa, NULL);
209 }
210 }
211