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