mainbus.c revision 1.1 1 1.1 thorpej /* $NetBSD: mainbus.c,v 1.1 2024/01/02 07:40:59 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2023 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej #include <sys/cdefs.h>
33 1.1 thorpej __KERNEL_RCSID(0, "$NetBSD: mainbus.c,v 1.1 2024/01/02 07:40:59 thorpej Exp $");
34 1.1 thorpej
35 1.1 thorpej #define _VIRT68K_BUS_DMA_PRIVATE
36 1.1 thorpej #define _VIRT68K_BUS_SPACE_PRIVATE
37 1.1 thorpej
38 1.1 thorpej #include <sys/param.h>
39 1.1 thorpej #include <sys/kernel.h>
40 1.1 thorpej #include <sys/systm.h>
41 1.1 thorpej #include <sys/device.h>
42 1.1 thorpej #include <sys/bus.h>
43 1.1 thorpej #include <sys/intr.h>
44 1.1 thorpej
45 1.1 thorpej #include <machine/bootinfo.h>
46 1.1 thorpej #include <machine/cpu.h>
47 1.1 thorpej
48 1.1 thorpej #include <virt68k/dev/mainbusvar.h>
49 1.1 thorpej
50 1.1 thorpej struct virt68k_bus_dma_tag _mainbus_dma_tag = {
51 1.1 thorpej NULL,
52 1.1 thorpej _bus_dmamap_create,
53 1.1 thorpej _bus_dmamap_destroy,
54 1.1 thorpej _bus_dmamap_load_direct,
55 1.1 thorpej _bus_dmamap_load_mbuf_direct,
56 1.1 thorpej _bus_dmamap_load_uio_direct,
57 1.1 thorpej _bus_dmamap_load_raw_direct,
58 1.1 thorpej _bus_dmamap_unload,
59 1.1 thorpej NULL, /* Set up at run-time */
60 1.1 thorpej _bus_dmamem_alloc,
61 1.1 thorpej _bus_dmamem_free,
62 1.1 thorpej _bus_dmamem_map,
63 1.1 thorpej _bus_dmamem_unmap,
64 1.1 thorpej _bus_dmamem_mmap
65 1.1 thorpej };
66 1.1 thorpej
67 1.1 thorpej struct virt68k_bus_space_tag _mainbus_space_tag = {
68 1.1 thorpej NULL,
69 1.1 thorpej _bus_space_map,
70 1.1 thorpej _bus_space_unmap,
71 1.1 thorpej _bus_space_peek_1,
72 1.1 thorpej _bus_space_peek_2,
73 1.1 thorpej _bus_space_peek_4,
74 1.1 thorpej _bus_space_poke_1,
75 1.1 thorpej _bus_space_poke_2,
76 1.1 thorpej _bus_space_poke_4
77 1.1 thorpej };
78 1.1 thorpej
79 1.1 thorpej static int
80 1.1 thorpej mainbus_print(void *aux, const char *cp)
81 1.1 thorpej {
82 1.1 thorpej struct mainbus_attach_args *ma = aux;
83 1.1 thorpej
84 1.1 thorpej if (cp) {
85 1.1 thorpej aprint_normal("%s at %s", ma->ma_compatible, cp);
86 1.1 thorpej }
87 1.1 thorpej
88 1.1 thorpej aprint_normal(" addr 0x%lx", ma->ma_addr);
89 1.1 thorpej
90 1.1 thorpej return UNCONF;
91 1.1 thorpej }
92 1.1 thorpej
93 1.1 thorpej static int
94 1.1 thorpej mainbus_match(device_t parent __unused, cfdata_t cf __unused,
95 1.1 thorpej void *args __unused)
96 1.1 thorpej {
97 1.1 thorpej static int mainbus_matched;
98 1.1 thorpej
99 1.1 thorpej if (mainbus_matched)
100 1.1 thorpej return 0;
101 1.1 thorpej
102 1.1 thorpej return (mainbus_matched = 1);
103 1.1 thorpej }
104 1.1 thorpej
105 1.1 thorpej static bool
106 1.1 thorpej mainbus_attach_gfpic(struct bi_record *bi, void *v)
107 1.1 thorpej {
108 1.1 thorpej struct mainbus_attach_args ma = {
109 1.1 thorpej .ma_st = &_mainbus_space_tag,
110 1.1 thorpej .ma_dmat = &_mainbus_dma_tag,
111 1.1 thorpej };
112 1.1 thorpej device_t self = v;
113 1.1 thorpej struct bi_virt_dev *vd = bootinfo_dataptr(bi);
114 1.1 thorpej int i;
115 1.1 thorpej
116 1.1 thorpej if (bi->bi_tag == BI_VIRT_GF_PIC_BASE) {
117 1.1 thorpej for (i = 0; i < NPIC; i++) {
118 1.1 thorpej ma.ma_compatible = "google,goldfish-pic";
119 1.1 thorpej ma.ma_addr = vd->vd_mmio_base + (i * 0x1000);
120 1.1 thorpej ma.ma_size = 0x1000;
121 1.1 thorpej ma.ma_irq = vd->vd_irq_base + i;
122 1.1 thorpej config_found(self, &ma, mainbus_print, CFARGS_NONE);
123 1.1 thorpej }
124 1.1 thorpej return false; /* done searching */
125 1.1 thorpej }
126 1.1 thorpej return true; /* keep searching */
127 1.1 thorpej }
128 1.1 thorpej
129 1.1 thorpej static bool
130 1.1 thorpej mainbus_attach_gfother(struct bi_record *bi, void *v)
131 1.1 thorpej {
132 1.1 thorpej struct mainbus_attach_args ma = {
133 1.1 thorpej .ma_st = &_mainbus_space_tag,
134 1.1 thorpej .ma_dmat = &_mainbus_dma_tag,
135 1.1 thorpej };
136 1.1 thorpej device_t self = v;
137 1.1 thorpej struct bi_virt_dev *vd = bootinfo_dataptr(bi);
138 1.1 thorpej int i;
139 1.1 thorpej
140 1.1 thorpej switch (bi->bi_tag) {
141 1.1 thorpej case BI_VIRT_GF_RTC_BASE:
142 1.1 thorpej /*
143 1.1 thorpej * There are 2 Goldfish RTC instances on the virt68k
144 1.1 thorpej * platform:
145 1.1 thorpej *
146 1.1 thorpej * 1- This is used as the system timer / hard-clock.
147 1.1 thorpej * 2- This is used as the TODR.
148 1.1 thorpej */
149 1.1 thorpej for (i = 0; i < 2; i++) {
150 1.1 thorpej ma.ma_compatible = (i == 0)
151 1.1 thorpej ? "netbsd,goldfish-rtc-hardclock"
152 1.1 thorpej : "google,goldfish-rtc";
153 1.1 thorpej ma.ma_addr = vd->vd_mmio_base + (i * 0x1000);
154 1.1 thorpej ma.ma_size = 0x1000;
155 1.1 thorpej ma.ma_irq = vd->vd_irq_base + i;
156 1.1 thorpej config_found(self, &ma, mainbus_print, CFARGS_NONE);
157 1.1 thorpej }
158 1.1 thorpej break;
159 1.1 thorpej
160 1.1 thorpej case BI_VIRT_GF_TTY_BASE:
161 1.1 thorpej ma.ma_compatible = "google,goldfish-tty";
162 1.1 thorpej ma.ma_addr = vd->vd_mmio_base;
163 1.1 thorpej ma.ma_size = 0x1000;
164 1.1 thorpej ma.ma_irq = vd->vd_irq_base;
165 1.1 thorpej config_found(self, &ma, mainbus_print, CFARGS_NONE);
166 1.1 thorpej break;
167 1.1 thorpej }
168 1.1 thorpej
169 1.1 thorpej return true; /* keep searching */
170 1.1 thorpej }
171 1.1 thorpej
172 1.1 thorpej #define VIRTIO_MMIO_DEVICE_ID 0x008
173 1.1 thorpej
174 1.1 thorpej static void
175 1.1 thorpej mainbus_attach_virtio(device_t self, struct mainbus_attach_args *ma)
176 1.1 thorpej {
177 1.1 thorpej bus_space_handle_t bsh;
178 1.1 thorpej uint32_t val;
179 1.1 thorpej
180 1.1 thorpej /*
181 1.1 thorpej * Probe the virtio slot to see if there's actually something
182 1.1 thorpej * there before we claim that it is "found".
183 1.1 thorpej */
184 1.1 thorpej if (bus_space_map(ma->ma_st, ma->ma_addr, ma->ma_size, 0, &bsh) != 0) {
185 1.1 thorpej aprint_error_dev(self,
186 1.1 thorpej "unable to map virtio slot @ 0x%lx\n", ma->ma_addr);
187 1.1 thorpej return;
188 1.1 thorpej }
189 1.1 thorpej val = bus_space_read_4(ma->ma_st, bsh, VIRTIO_MMIO_DEVICE_ID);
190 1.1 thorpej bus_space_unmap(ma->ma_st, bsh, ma->ma_size);
191 1.1 thorpej
192 1.1 thorpej if (val != 0) {
193 1.1 thorpej config_found(self, ma, mainbus_print, CFARGS_NONE);
194 1.1 thorpej }
195 1.1 thorpej }
196 1.1 thorpej
197 1.1 thorpej static bool
198 1.1 thorpej mainbus_attach_other(struct bi_record *bi, void *v)
199 1.1 thorpej {
200 1.1 thorpej struct mainbus_attach_args ma = {
201 1.1 thorpej .ma_st = &_mainbus_space_tag,
202 1.1 thorpej .ma_dmat = &_mainbus_dma_tag,
203 1.1 thorpej };
204 1.1 thorpej device_t self = v;
205 1.1 thorpej struct bi_virt_dev *vd = bootinfo_dataptr(bi);
206 1.1 thorpej int i;
207 1.1 thorpej
208 1.1 thorpej switch (bi->bi_tag) {
209 1.1 thorpej case BI_VIRT_QEMU_VERSION:
210 1.1 thorpej case BI_VIRT_GF_PIC_BASE:
211 1.1 thorpej case BI_VIRT_GF_RTC_BASE:
212 1.1 thorpej case BI_VIRT_GF_TTY_BASE:
213 1.1 thorpej /* Handled elsewhere. */
214 1.1 thorpej break;
215 1.1 thorpej
216 1.1 thorpej case BI_VIRT_VIRTIO_BASE:
217 1.1 thorpej for (i = 0; i < (32 * 4); i++) {
218 1.1 thorpej ma.ma_compatible = "virtio,mmio";
219 1.1 thorpej ma.ma_addr = vd->vd_mmio_base + (i * 0x200);
220 1.1 thorpej ma.ma_size = 0x200;
221 1.1 thorpej ma.ma_irq = vd->vd_irq_base + i;
222 1.1 thorpej mainbus_attach_virtio(self, &ma);
223 1.1 thorpej }
224 1.1 thorpej break;
225 1.1 thorpej
226 1.1 thorpej case BI_VIRT_CTRL_BASE:
227 1.1 thorpej ma.ma_compatible = "netbsd,qemu-virt-ctrl"; /* XXX */
228 1.1 thorpej ma.ma_addr = vd->vd_mmio_base;
229 1.1 thorpej ma.ma_size = 0x1000;
230 1.1 thorpej ma.ma_irq = vd->vd_irq_base;
231 1.1 thorpej config_found(self, &ma, mainbus_print, CFARGS_NONE);
232 1.1 thorpej break;
233 1.1 thorpej
234 1.1 thorpej default:
235 1.1 thorpej if (bi->bi_tag >= BI_MACHDEP(0)) {
236 1.1 thorpej aprint_error_dev(self,
237 1.1 thorpej "unknown bootinfo tag: 0x%08x\n", bi->bi_tag);
238 1.1 thorpej }
239 1.1 thorpej break;
240 1.1 thorpej }
241 1.1 thorpej return true; /* keep searching */
242 1.1 thorpej }
243 1.1 thorpej
244 1.1 thorpej static void
245 1.1 thorpej mainbus_attach(device_t parent __unused, device_t self, void *args __unused)
246 1.1 thorpej {
247 1.1 thorpej
248 1.1 thorpej printf("\n");
249 1.1 thorpej
250 1.1 thorpej _mainbus_dma_tag._dmamap_sync =
251 1.1 thorpej (mmutype == MMU_68040) ? _bus_dmamap_sync_0460
252 1.1 thorpej : _bus_dmamap_sync_030;
253 1.1 thorpej
254 1.1 thorpej /* Attach the PICs first. */
255 1.1 thorpej bootinfo_enumerate(mainbus_attach_gfpic, self);
256 1.1 thorpej
257 1.1 thorpej /* Attach the reset of the Goldfish devices next. */
258 1.1 thorpej bootinfo_enumerate(mainbus_attach_gfother, self);
259 1.1 thorpej
260 1.1 thorpej /* Now the rest. */
261 1.1 thorpej bootinfo_enumerate(mainbus_attach_other, self);
262 1.1 thorpej }
263 1.1 thorpej
264 1.1 thorpej int
265 1.1 thorpej mainbus_compatible_match(const struct mainbus_attach_args * const ma,
266 1.1 thorpej const struct device_compatible_entry * driver_compats)
267 1.1 thorpej {
268 1.1 thorpej const char *device_compats[1] = {
269 1.1 thorpej [0] = ma->ma_compatible,
270 1.1 thorpej };
271 1.1 thorpej return device_compatible_match(device_compats,
272 1.1 thorpej __arraycount(device_compats), driver_compats);
273 1.1 thorpej }
274 1.1 thorpej
275 1.1 thorpej const struct device_compatible_entry *
276 1.1 thorpej mainbus_compatible_lookup(const struct mainbus_attach_args * const ma,
277 1.1 thorpej const struct device_compatible_entry * driver_compats)
278 1.1 thorpej {
279 1.1 thorpej const char *device_compats[1] = {
280 1.1 thorpej [0] = ma->ma_compatible,
281 1.1 thorpej };
282 1.1 thorpej return device_compatible_lookup(device_compats,
283 1.1 thorpej __arraycount(device_compats), driver_compats);
284 1.1 thorpej }
285 1.1 thorpej
286 1.1 thorpej CFATTACH_DECL_NEW(mainbus, 0,
287 1.1 thorpej mainbus_match, mainbus_attach, NULL, NULL);
288