bcm283x_platform.c revision 1.2.2.8 1 /* $NetBSD: bcm283x_platform.c,v 1.2.2.8 2018/11/26 01:52:18 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: bcm283x_platform.c,v 1.2.2.8 2018/11/26 01:52:18 pgoyette Exp $");
31
32 #include "opt_arm_debug.h"
33 #include "opt_bcm283x.h"
34 #include "opt_cpuoptions.h"
35 #include "opt_ddb.h"
36 #include "opt_evbarm_boardtype.h"
37 #include "opt_kgdb.h"
38 #include "opt_fdt.h"
39 #include "opt_rpi.h"
40 #include "opt_vcprop.h"
41
42 #include "sdhc.h"
43 #include "bcmsdhost.h"
44 #include "bcmdwctwo.h"
45 #include "bcmspi.h"
46 #include "bsciic.h"
47 #include "plcom.h"
48 #include "com.h"
49 #include "genfb.h"
50 #include "ukbd.h"
51
52 #include <sys/param.h>
53 #include <sys/bus.h>
54 #include <sys/cpu.h>
55 #include <sys/device.h>
56 #include <sys/termios.h>
57
58 #include <net/if_ether.h>
59
60 #include <prop/proplib.h>
61
62 #include <dev/fdt/fdtvar.h>
63
64 #include <uvm/uvm_extern.h>
65
66 #include <machine/bootconfig.h>
67
68 #include <arm/armreg.h>
69 #include <arm/cpufunc.h>
70
71 #include <libfdt.h>
72
73 #include <arm/broadcom/bcm2835reg.h>
74 #include <arm/broadcom/bcm2835var.h>
75 #include <arm/broadcom/bcm283x_platform.h>
76 #include <arm/broadcom/bcm2835_intr.h>
77 #include <arm/broadcom/bcm2835_mbox.h>
78 #include <arm/broadcom/bcm2835_pmwdogvar.h>
79
80 #include <evbarm/dev/plcomreg.h>
81 #include <evbarm/dev/plcomvar.h>
82 #include <evbarm/fdt/machdep.h>
83
84 #include <dev/ic/ns16550reg.h>
85 #include <dev/ic/comreg.h>
86
87 #include <evbarm/rpi/vcio.h>
88 #include <evbarm/rpi/vcpm.h>
89 #include <evbarm/rpi/vcprop.h>
90
91 #include <arm/fdt/arm_fdtvar.h>
92
93 #include <arm/cortex/gtmr_var.h>
94
95 #if NGENFB > 0
96 #include <dev/videomode/videomode.h>
97 #include <dev/videomode/edidvar.h>
98 #include <dev/wscons/wsconsio.h>
99 #endif
100
101 #if NUKBD > 0
102 #include <dev/usb/ukbdvar.h>
103 #endif
104
105 #ifdef DDB
106 #include <machine/db_machdep.h>
107 #include <ddb/db_sym.h>
108 #include <ddb/db_extern.h>
109 #endif
110
111 #define RPI_CPU_MAX 4
112
113 void bcm2835_platform_early_putchar(char c);
114 void bcm2836_platform_early_putchar(char c);
115 void bcm2837_platform_early_putchar(char c);
116
117 extern void bcmgenfb_set_console_dev(device_t dev);
118 void bcmgenfb_set_ioctl(int(*)(void *, void *, u_long, void *, int, struct lwp *));
119 extern void bcmgenfb_ddb_trap_callback(int where);
120 static int rpi_ioctl(void *, void *, u_long, void *, int, lwp_t *);
121
122 extern struct bus_space arm_generic_bs_tag;
123 extern struct bus_space arm_generic_a4x_bs_tag;
124
125 /* Prototypes for all the bus_space structure functions */
126 bs_protos(arm_generic);
127 bs_protos(arm_generic_a4x);
128 bs_protos(bcm2835);
129 bs_protos(bcm2835_a4x);
130 bs_protos(bcm2836);
131 bs_protos(bcm2836_a4x);
132
133 struct bus_space bcm2835_bs_tag;
134 struct bus_space bcm2835_a4x_bs_tag;
135 struct bus_space bcm2836_bs_tag;
136 struct bus_space bcm2836_a4x_bs_tag;
137
138 static paddr_t bcm2835_bus_to_phys(bus_addr_t);
139 static paddr_t bcm2836_bus_to_phys(bus_addr_t);
140
141 #ifdef VERBOSE_INIT_ARM
142 #define VPRINTF(...) printf(__VA_ARGS__)
143 #else
144 #define VPRINTF(...) __nothing
145 #endif
146
147 static paddr_t
148 bcm2835_bus_to_phys(bus_addr_t ba)
149 {
150
151 /* Attempt to find the PA device mapping */
152 if (ba >= BCM2835_PERIPHERALS_BASE_BUS &&
153 ba < BCM2835_PERIPHERALS_BASE_BUS + BCM2835_PERIPHERALS_SIZE)
154 return BCM2835_PERIPHERALS_BUS_TO_PHYS(ba);
155
156 return ba & ~BCM2835_BUSADDR_CACHE_MASK;
157 }
158
159 static paddr_t
160 bcm2836_bus_to_phys(bus_addr_t ba)
161 {
162
163 /* Attempt to find the PA device mapping */
164 if (ba >= BCM2835_PERIPHERALS_BASE_BUS &&
165 ba < BCM2835_PERIPHERALS_BASE_BUS + BCM2835_PERIPHERALS_SIZE)
166 return BCM2836_PERIPHERALS_BUS_TO_PHYS(ba);
167
168 if (ba >= BCM2836_ARM_LOCAL_BASE &&
169 ba < BCM2836_ARM_LOCAL_BASE + BCM2836_ARM_LOCAL_SIZE)
170 return ba;
171
172 return ba & ~BCM2835_BUSADDR_CACHE_MASK;
173 }
174
175 int
176 bcm2835_bs_map(void *t, bus_addr_t ba, bus_size_t size, int flag,
177 bus_space_handle_t *bshp)
178 {
179 const paddr_t pa = bcm2835_bus_to_phys(ba);
180
181 return bus_space_map(&arm_generic_bs_tag, pa, size, flag, bshp);
182 }
183
184 paddr_t
185 bcm2835_bs_mmap(void *t, bus_addr_t ba, off_t offset, int prot, int flags)
186 {
187 const paddr_t pa = bcm2835_bus_to_phys(ba);
188
189 return bus_space_mmap(&arm_generic_bs_tag, pa, offset, prot, flags);
190 }
191
192 paddr_t
193 bcm2835_a4x_bs_mmap(void *t, bus_addr_t ba, off_t offset, int prot, int flags)
194 {
195
196 return bcm2835_bs_mmap(t, ba, 4 * offset, prot, flags);
197 }
198
199 int
200 bcm2836_bs_map(void *t, bus_addr_t ba, bus_size_t size, int flag,
201 bus_space_handle_t *bshp)
202 {
203 const paddr_t pa = bcm2836_bus_to_phys(ba);
204
205 return bus_space_map(&arm_generic_bs_tag, pa, size, flag, bshp);
206 }
207
208 paddr_t
209 bcm2836_bs_mmap(void *t, bus_addr_t ba, off_t offset, int prot, int flags)
210 {
211 const paddr_t pa = bcm2836_bus_to_phys(ba);
212
213 return bus_space_mmap(&arm_generic_bs_tag, pa, offset, prot, flags);
214 }
215
216 paddr_t
217 bcm2836_a4x_bs_mmap(void *t, bus_addr_t ba, off_t offset, int prot, int flags)
218 {
219
220 return bcm2836_bs_mmap(t, ba, 4 * offset, prot, flags);
221 }
222
223 struct arm32_dma_range bcm2835_dma_ranges[] = {
224 [0] = {
225 .dr_sysbase = 0,
226 .dr_busbase = BCM2835_BUSADDR_CACHE_COHERENT,
227 }
228 };
229
230 struct arm32_dma_range bcm2836_dma_ranges[] = {
231 [0] = {
232 .dr_sysbase = 0,
233 .dr_busbase = BCM2835_BUSADDR_CACHE_DIRECT,
234 }
235 };
236
237
238 #if defined(SOC_BCM2835)
239 static const struct pmap_devmap *
240 bcm2835_platform_devmap(void)
241 {
242 static const struct pmap_devmap devmap[] = {
243 DEVMAP_ENTRY(BCM2835_PERIPHERALS_VBASE, BCM2835_PERIPHERALS_BASE,
244 BCM2835_PERIPHERALS_SIZE), /* 16Mb */
245
246 DEVMAP_ENTRY_END
247 };
248
249 return devmap;
250 }
251 #endif
252
253 #if defined(SOC_BCM2836)
254 static const struct pmap_devmap *
255 bcm2836_platform_devmap(void)
256 {
257 static const struct pmap_devmap devmap[] = {
258 DEVMAP_ENTRY(BCM2836_PERIPHERALS_VBASE, BCM2836_PERIPHERALS_BASE,
259 BCM2835_PERIPHERALS_SIZE), /* 16Mb */
260
261 DEVMAP_ENTRY(BCM2836_ARM_LOCAL_VBASE, BCM2836_ARM_LOCAL_BASE,
262 BCM2836_ARM_LOCAL_SIZE),
263 #if defined(MULTIPROCESSOR) && defined(__aarch64__)
264 /* for fdt cpu spin-table */
265 DEVMAP_ENTRY(BCM2836_ARM_SMP_VBASE, BCM2836_ARM_SMP_BASE,
266 BCM2836_ARM_SMP_SIZE),
267 #endif
268 DEVMAP_ENTRY_END
269 };
270
271 return devmap;
272 }
273 #endif
274 /*
275 * Macros to translate between physical and virtual for a subset of the
276 * kernel address space. *Not* for general use.
277 */
278
279 #ifndef RPI_FB_WIDTH
280 #define RPI_FB_WIDTH 1280
281 #endif
282 #ifndef RPI_FB_HEIGHT
283 #define RPI_FB_HEIGHT 720
284 #endif
285
286 int uart_clk = BCM2835_UART0_CLK;
287 int core_clk;
288
289 static struct {
290 struct vcprop_buffer_hdr vb_hdr;
291 struct vcprop_tag_clockrate vbt_uartclockrate;
292 struct vcprop_tag_clockrate vbt_vpuclockrate;
293 struct vcprop_tag end;
294 } vb_uart __cacheline_aligned = {
295 .vb_hdr = {
296 .vpb_len = sizeof(vb_uart),
297 .vpb_rcode = VCPROP_PROCESS_REQUEST,
298 },
299 .vbt_uartclockrate = {
300 .tag = {
301 .vpt_tag = VCPROPTAG_GET_CLOCKRATE,
302 .vpt_len = VCPROPTAG_LEN(vb_uart.vbt_uartclockrate),
303 .vpt_rcode = VCPROPTAG_REQUEST
304 },
305 .id = VCPROP_CLK_UART
306 },
307 .vbt_vpuclockrate = {
308 .tag = {
309 .vpt_tag = VCPROPTAG_GET_CLOCKRATE,
310 .vpt_len = VCPROPTAG_LEN(vb_uart.vbt_vpuclockrate),
311 .vpt_rcode = VCPROPTAG_REQUEST
312 },
313 .id = VCPROP_CLK_CORE
314 },
315 .end = {
316 .vpt_tag = VCPROPTAG_NULL
317 }
318 };
319
320 static struct {
321 struct vcprop_buffer_hdr vb_hdr;
322 struct vcprop_tag_fwrev vbt_fwrev;
323 struct vcprop_tag_boardmodel vbt_boardmodel;
324 struct vcprop_tag_boardrev vbt_boardrev;
325 struct vcprop_tag_macaddr vbt_macaddr;
326 struct vcprop_tag_memory vbt_memory;
327 struct vcprop_tag_boardserial vbt_serial;
328 struct vcprop_tag_dmachan vbt_dmachan;
329 struct vcprop_tag_cmdline vbt_cmdline;
330 struct vcprop_tag_clockrate vbt_emmcclockrate;
331 struct vcprop_tag_clockrate vbt_armclockrate;
332 struct vcprop_tag_clockrate vbt_vpuclockrate;
333 struct vcprop_tag end;
334 } vb __cacheline_aligned = {
335 .vb_hdr = {
336 .vpb_len = sizeof(vb),
337 .vpb_rcode = VCPROP_PROCESS_REQUEST,
338 },
339 .vbt_fwrev = {
340 .tag = {
341 .vpt_tag = VCPROPTAG_GET_FIRMWAREREV,
342 .vpt_len = VCPROPTAG_LEN(vb.vbt_fwrev),
343 .vpt_rcode = VCPROPTAG_REQUEST
344 },
345 },
346 .vbt_boardmodel = {
347 .tag = {
348 .vpt_tag = VCPROPTAG_GET_BOARDMODEL,
349 .vpt_len = VCPROPTAG_LEN(vb.vbt_boardmodel),
350 .vpt_rcode = VCPROPTAG_REQUEST
351 },
352 },
353 .vbt_boardrev = {
354 .tag = {
355 .vpt_tag = VCPROPTAG_GET_BOARDREVISION,
356 .vpt_len = VCPROPTAG_LEN(vb.vbt_boardrev),
357 .vpt_rcode = VCPROPTAG_REQUEST
358 },
359 },
360 .vbt_macaddr = {
361 .tag = {
362 .vpt_tag = VCPROPTAG_GET_MACADDRESS,
363 .vpt_len = VCPROPTAG_LEN(vb.vbt_macaddr),
364 .vpt_rcode = VCPROPTAG_REQUEST
365 },
366 },
367 .vbt_memory = {
368 .tag = {
369 .vpt_tag = VCPROPTAG_GET_ARMMEMORY,
370 .vpt_len = VCPROPTAG_LEN(vb.vbt_memory),
371 .vpt_rcode = VCPROPTAG_REQUEST
372 },
373 },
374 .vbt_serial = {
375 .tag = {
376 .vpt_tag = VCPROPTAG_GET_BOARDSERIAL,
377 .vpt_len = VCPROPTAG_LEN(vb.vbt_serial),
378 .vpt_rcode = VCPROPTAG_REQUEST
379 },
380 },
381 .vbt_dmachan = {
382 .tag = {
383 .vpt_tag = VCPROPTAG_GET_DMACHAN,
384 .vpt_len = VCPROPTAG_LEN(vb.vbt_dmachan),
385 .vpt_rcode = VCPROPTAG_REQUEST
386 },
387 },
388 .vbt_cmdline = {
389 .tag = {
390 .vpt_tag = VCPROPTAG_GET_CMDLINE,
391 .vpt_len = VCPROPTAG_LEN(vb.vbt_cmdline),
392 .vpt_rcode = VCPROPTAG_REQUEST
393 },
394 },
395 .vbt_emmcclockrate = {
396 .tag = {
397 .vpt_tag = VCPROPTAG_GET_CLOCKRATE,
398 .vpt_len = VCPROPTAG_LEN(vb.vbt_emmcclockrate),
399 .vpt_rcode = VCPROPTAG_REQUEST
400 },
401 .id = VCPROP_CLK_EMMC
402 },
403 .vbt_armclockrate = {
404 .tag = {
405 .vpt_tag = VCPROPTAG_GET_CLOCKRATE,
406 .vpt_len = VCPROPTAG_LEN(vb.vbt_armclockrate),
407 .vpt_rcode = VCPROPTAG_REQUEST
408 },
409 .id = VCPROP_CLK_ARM
410 },
411 .vbt_vpuclockrate = {
412 .tag = {
413 .vpt_tag = VCPROPTAG_GET_CLOCKRATE,
414 .vpt_len = VCPROPTAG_LEN(vb.vbt_vpuclockrate),
415 .vpt_rcode = VCPROPTAG_REQUEST
416 },
417 .id = VCPROP_CLK_CORE
418 },
419 .end = {
420 .vpt_tag = VCPROPTAG_NULL
421 }
422 };
423
424 #if NGENFB > 0
425 static struct {
426 struct vcprop_buffer_hdr vb_hdr;
427 struct vcprop_tag_edidblock vbt_edid;
428 struct vcprop_tag end;
429 } vb_edid __cacheline_aligned = {
430 .vb_hdr = {
431 .vpb_len = sizeof(vb_edid),
432 .vpb_rcode = VCPROP_PROCESS_REQUEST,
433 },
434 .vbt_edid = {
435 .tag = {
436 .vpt_tag = VCPROPTAG_GET_EDID_BLOCK,
437 .vpt_len = VCPROPTAG_LEN(vb_edid.vbt_edid),
438 .vpt_rcode = VCPROPTAG_REQUEST,
439 },
440 .blockno = 0,
441 },
442 .end = {
443 .vpt_tag = VCPROPTAG_NULL
444 }
445 };
446
447 static struct {
448 struct vcprop_buffer_hdr vb_hdr;
449 struct vcprop_tag_fbres vbt_res;
450 struct vcprop_tag_fbres vbt_vres;
451 struct vcprop_tag_fbdepth vbt_depth;
452 struct vcprop_tag_fbalpha vbt_alpha;
453 struct vcprop_tag_allocbuf vbt_allocbuf;
454 struct vcprop_tag_blankscreen vbt_blank;
455 struct vcprop_tag_fbpitch vbt_pitch;
456 struct vcprop_tag end;
457 } vb_setfb __cacheline_aligned = {
458 .vb_hdr = {
459 .vpb_len = sizeof(vb_setfb),
460 .vpb_rcode = VCPROP_PROCESS_REQUEST,
461 },
462 .vbt_res = {
463 .tag = {
464 .vpt_tag = VCPROPTAG_SET_FB_RES,
465 .vpt_len = VCPROPTAG_LEN(vb_setfb.vbt_res),
466 .vpt_rcode = VCPROPTAG_REQUEST,
467 },
468 .width = 0,
469 .height = 0,
470 },
471 .vbt_vres = {
472 .tag = {
473 .vpt_tag = VCPROPTAG_SET_FB_VRES,
474 .vpt_len = VCPROPTAG_LEN(vb_setfb.vbt_vres),
475 .vpt_rcode = VCPROPTAG_REQUEST,
476 },
477 .width = 0,
478 .height = 0,
479 },
480 .vbt_depth = {
481 .tag = {
482 .vpt_tag = VCPROPTAG_SET_FB_DEPTH,
483 .vpt_len = VCPROPTAG_LEN(vb_setfb.vbt_depth),
484 .vpt_rcode = VCPROPTAG_REQUEST,
485 },
486 .bpp = 32,
487 },
488 .vbt_alpha = {
489 .tag = {
490 .vpt_tag = VCPROPTAG_SET_FB_ALPHA_MODE,
491 .vpt_len = VCPROPTAG_LEN(vb_setfb.vbt_alpha),
492 .vpt_rcode = VCPROPTAG_REQUEST,
493 },
494 .state = VCPROP_ALPHA_IGNORED,
495 },
496 .vbt_allocbuf = {
497 .tag = {
498 .vpt_tag = VCPROPTAG_ALLOCATE_BUFFER,
499 .vpt_len = VCPROPTAG_LEN(vb_setfb.vbt_allocbuf),
500 .vpt_rcode = VCPROPTAG_REQUEST,
501 },
502 .address = PAGE_SIZE, /* alignment */
503 },
504 .vbt_blank = {
505 .tag = {
506 .vpt_tag = VCPROPTAG_BLANK_SCREEN,
507 .vpt_len = VCPROPTAG_LEN(vb_setfb.vbt_blank),
508 .vpt_rcode = VCPROPTAG_REQUEST,
509 },
510 .state = VCPROP_BLANK_OFF,
511 },
512 .vbt_pitch = {
513 .tag = {
514 .vpt_tag = VCPROPTAG_GET_FB_PITCH,
515 .vpt_len = VCPROPTAG_LEN(vb_setfb.vbt_pitch),
516 .vpt_rcode = VCPROPTAG_REQUEST,
517 },
518 },
519 .end = {
520 .vpt_tag = VCPROPTAG_NULL,
521 },
522 };
523
524 #endif
525
526 static int rpi_video_on = WSDISPLAYIO_VIDEO_ON;
527
528 #if defined(RPI_HWCURSOR)
529 #define CURSOR_BITMAP_SIZE (64 * 8)
530 #define CURSOR_ARGB_SIZE (64 * 64 * 4)
531 static uint32_t hcursor = 0;
532 static bus_addr_t pcursor = 0;
533 static uint32_t *cmem = NULL;
534 static int cursor_x = 0, cursor_y = 0, hot_x = 0, hot_y = 0, cursor_on = 0;
535 static uint32_t cursor_cmap[4];
536 static uint8_t cursor_mask[8 * 64], cursor_bitmap[8 * 64];
537 #endif
538
539 u_int
540 bcm283x_clk_get_rate_uart(void)
541 {
542
543 if (vcprop_tag_success_p(&vb_uart.vbt_uartclockrate.tag))
544 return vb_uart.vbt_uartclockrate.rate;
545 return 0;
546 }
547
548 u_int
549 bcm283x_clk_get_rate_vpu(void)
550 {
551
552 if (vcprop_tag_success_p(&vb.vbt_vpuclockrate.tag) &&
553 vb.vbt_vpuclockrate.rate > 0) {
554 return vb.vbt_vpuclockrate.rate;
555 }
556 return 0;
557 }
558
559 u_int
560 bcm283x_clk_get_rate_emmc(void)
561 {
562
563 if (vcprop_tag_success_p(&vb.vbt_emmcclockrate.tag) &&
564 vb.vbt_emmcclockrate.rate > 0) {
565 return vb.vbt_emmcclockrate.rate;
566 }
567 return 0;
568 }
569
570
571
572 static void
573 bcm283x_uartinit(bus_space_tag_t iot, bus_space_handle_t ioh)
574 {
575 uint32_t res;
576
577 bcm2835_mbox_write(iot, ioh, BCMMBOX_CHANARM2VC,
578 KERN_VTOPHYS((vaddr_t)&vb_uart));
579
580 bcm2835_mbox_read(iot, ioh, BCMMBOX_CHANARM2VC, &res);
581
582 if (vcprop_tag_success_p(&vb_uart.vbt_uartclockrate.tag))
583 uart_clk = vb_uart.vbt_uartclockrate.rate;
584 if (vcprop_tag_success_p(&vb_uart.vbt_vpuclockrate.tag))
585 core_clk = vb_uart.vbt_vpuclockrate.rate;
586 }
587
588 #if defined(SOC_BCM2835)
589 static void
590 bcm2835_uartinit(void)
591 {
592 const paddr_t pa = BCM2835_PERIPHERALS_BUS_TO_PHYS(BCM2835_ARMMBOX_BASE);
593 const bus_space_tag_t iot = &bcm2835_bs_tag;
594 const bus_space_handle_t ioh = BCM2835_IOPHYSTOVIRT(pa);
595
596 bcm283x_uartinit(iot, ioh);
597 }
598 #endif
599
600 #if defined(SOC_BCM2836)
601 static void
602 bcm2836_uartinit(void)
603 {
604 const paddr_t pa = BCM2836_PERIPHERALS_BUS_TO_PHYS(BCM2835_ARMMBOX_BASE);
605 const bus_space_tag_t iot = &bcm2836_bs_tag;
606 const bus_space_handle_t ioh = BCM2835_IOPHYSTOVIRT(pa);
607
608 bcm283x_uartinit(iot, ioh);
609 }
610 #endif
611
612 #define BCM283x_MINIMUM_SPLIT (128U * 1024 * 1024)
613
614 static size_t bcm283x_memorysize;
615
616 static void
617 bcm283x_bootparams(bus_space_tag_t iot, bus_space_handle_t ioh)
618 {
619 uint32_t res;
620
621 bcm2835_mbox_write(iot, ioh, BCMMBOX_CHANPM, (
622 #if (NSDHC > 0)
623 (1 << VCPM_POWER_SDCARD) |
624 #endif
625 #if (NPLCOM > 0)
626 (1 << VCPM_POWER_UART0) |
627 #endif
628 #if (NBCMDWCTWO > 0)
629 (1 << VCPM_POWER_USB) |
630 #endif
631 #if (NBSCIIC > 0)
632 (1 << VCPM_POWER_I2C0) | (1 << VCPM_POWER_I2C1) |
633 /* (1 << VCPM_POWER_I2C2) | */
634 #endif
635 #if (NBCMSPI > 0)
636 (1 << VCPM_POWER_SPI) |
637 #endif
638 0) << 4);
639
640 bcm2835_mbox_write(iot, ioh, BCMMBOX_CHANARM2VC,
641 KERN_VTOPHYS((vaddr_t)&vb));
642
643 bcm2835_mbox_read(iot, ioh, BCMMBOX_CHANARM2VC, &res);
644
645 if (!vcprop_buffer_success_p(&vb.vb_hdr)) {
646 bootconfig.dramblocks = 1;
647 bootconfig.dram[0].address = 0x0;
648 bootconfig.dram[0].pages = atop(BCM283x_MINIMUM_SPLIT);
649 return;
650 }
651
652 struct vcprop_tag_memory *vptp_mem = &vb.vbt_memory;
653 if (vcprop_tag_success_p(&vptp_mem->tag)) {
654 size_t n = vcprop_tag_resplen(&vptp_mem->tag) /
655 sizeof(struct vcprop_memory);
656
657 bcm283x_memorysize = 0;
658 bootconfig.dramblocks = 0;
659
660 for (int i = 0; i < n && i < DRAM_BLOCKS; i++) {
661 bootconfig.dram[i].address = vptp_mem->mem[i].base;
662 bootconfig.dram[i].pages = atop(vptp_mem->mem[i].size);
663 bootconfig.dramblocks++;
664
665 bcm283x_memorysize += vptp_mem->mem[i].size;
666 }
667 }
668
669 if (vcprop_tag_success_p(&vb.vbt_armclockrate.tag))
670 curcpu()->ci_data.cpu_cc_freq = vb.vbt_armclockrate.rate;
671
672 #ifdef VERBOSE_INIT_ARM
673 if (vcprop_tag_success_p(&vb.vbt_memory.tag))
674 printf("%s: memory size %zu\n", __func__,
675 bcm283x_memorysize);
676 if (vcprop_tag_success_p(&vb.vbt_armclockrate.tag))
677 printf("%s: arm clock %d\n", __func__,
678 vb.vbt_armclockrate.rate);
679 if (vcprop_tag_success_p(&vb.vbt_fwrev.tag))
680 printf("%s: firmware rev %x\n", __func__,
681 vb.vbt_fwrev.rev);
682 if (vcprop_tag_success_p(&vb.vbt_boardmodel.tag))
683 printf("%s: board model %x\n", __func__,
684 vb.vbt_boardmodel.model);
685 if (vcprop_tag_success_p(&vb.vbt_macaddr.tag))
686 printf("%s: mac-address %" PRIx64 "\n", __func__,
687 vb.vbt_macaddr.addr);
688 if (vcprop_tag_success_p(&vb.vbt_boardrev.tag))
689 printf("%s: board rev %x\n", __func__,
690 vb.vbt_boardrev.rev);
691 if (vcprop_tag_success_p(&vb.vbt_serial.tag))
692 printf("%s: board serial %" PRIx64 "\n", __func__,
693 vb.vbt_serial.sn);
694 if (vcprop_tag_success_p(&vb.vbt_dmachan.tag))
695 printf("%s: DMA channel mask 0x%08x\n", __func__,
696 vb.vbt_dmachan.mask);
697
698 if (vcprop_tag_success_p(&vb.vbt_cmdline.tag))
699 printf("%s: cmdline %s\n", __func__,
700 vb.vbt_cmdline.cmdline);
701 #endif
702 }
703
704 #if defined(SOC_BCM2835)
705 static void
706 bcm2835_bootparams(void)
707 {
708 const paddr_t pa = BCM2835_PERIPHERALS_BUS_TO_PHYS(BCM2835_ARMMBOX_BASE);
709 const bus_space_tag_t iot = &bcm2835_bs_tag;
710 const bus_space_handle_t ioh = BCM2835_IOPHYSTOVIRT(pa);
711
712 bcm283x_bootparams(iot, ioh);
713 }
714 #endif
715
716 #if defined(SOC_BCM2836)
717 static void
718 bcm2836_bootparams(void)
719 {
720 const paddr_t pa = BCM2836_PERIPHERALS_BUS_TO_PHYS(BCM2835_ARMMBOX_BASE);
721 const bus_space_tag_t iot = &bcm2836_bs_tag;
722 const bus_space_handle_t ioh = BCM2835_IOPHYSTOVIRT(pa);
723
724 bcm283x_bootparams(iot, ioh);
725 }
726
727 static void
728 bcm2836_mpstart(void)
729 {
730 #ifdef MULTIPROCESSOR
731 #ifdef __arm__
732 /* implementation dependent string "brcm,bcm2836-smp" for ARM 32-bit */
733 const char *method;
734
735 const int cpus = OF_finddevice("/cpus");
736 if (cpus == -1) {
737 aprint_error("%s: no /cpus node found\n", __func__);
738 arm_cpu_max = 1;
739 return;
740 }
741
742 method = fdtbus_get_string(cpus, "enable-method");
743 if ((method != NULL) && (strcmp(method, "brcm,bcm2836-smp") == 0)) {
744 arm_cpu_max = RPI_CPU_MAX;
745 VPRINTF("%s: %d cpus present\n", __func__, arm_cpu_max);
746
747 extern void cpu_mpstart(void);
748
749 for (size_t i = 1; i < RPI_CPU_MAX; i++) {
750 bus_space_tag_t iot = &bcm2836_bs_tag;
751 bus_space_handle_t ioh = BCM2836_ARM_LOCAL_VBASE;
752
753 bus_space_write_4(iot, ioh,
754 BCM2836_LOCAL_MAILBOX3_SETN(i),
755 KERN_VTOPHYS((vaddr_t)cpu_mpstart));
756 }
757
758 /* Wake up AP in case firmware has placed it in WFE state */
759 __asm __volatile("sev" ::: "memory");
760
761 for (int loop = 0; loop < 16; loop++) {
762 if (arm_cpu_hatched == __BITS(arm_cpu_max - 1, 1))
763 break;
764 gtmr_delay(10000);
765 }
766
767 for (size_t i = 1; i < arm_cpu_max; i++) {
768 if ((arm_cpu_hatched & __BIT(i)) == 0) {
769 printf("%s: warning: cpu%zu failed to hatch\n",
770 __func__, i);
771 }
772 }
773 return;
774 }
775 #endif /* __arm__ */
776
777 /* try enable-method each cpus */
778 arm_fdt_cpu_mpstart();
779 #endif /* MULTIPROCESSOR */
780 }
781
782 #endif /* SOC_BCM2836 */
783
784 #if NGENFB > 0
785 static bool
786 rpi_fb_parse_mode(const char *s, uint32_t *pwidth, uint32_t *pheight)
787 {
788 char *x;
789
790 if (strncmp(s, "disable", 7) == 0)
791 return false;
792
793 x = strchr(s, 'x');
794 if (x) {
795 *pwidth = strtoul(s, NULL, 10);
796 *pheight = strtoul(x + 1, NULL, 10);
797 }
798
799 return true;
800 }
801
802 static bool
803 rpi_fb_get_edid_mode(uint32_t *pwidth, uint32_t *pheight)
804 {
805 struct edid_info ei;
806 uint8_t edid_data[1024];
807 uint32_t res;
808 int error;
809
810 error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb_edid,
811 sizeof(vb_edid), &res);
812 if (error) {
813 printf("%s: mbox request failed (%d)\n", __func__, error);
814 return false;
815 }
816
817 if (!vcprop_buffer_success_p(&vb_edid.vb_hdr) ||
818 !vcprop_tag_success_p(&vb_edid.vbt_edid.tag) ||
819 vb_edid.vbt_edid.status != 0)
820 return false;
821
822 memset(edid_data, 0, sizeof(edid_data));
823 memcpy(edid_data, vb_edid.vbt_edid.data,
824 sizeof(vb_edid.vbt_edid.data));
825 edid_parse(edid_data, &ei);
826 #ifdef VERBOSE_INIT_ARM
827 edid_print(&ei);
828 #endif
829
830 if (ei.edid_preferred_mode) {
831 *pwidth = ei.edid_preferred_mode->hdisplay;
832 *pheight = ei.edid_preferred_mode->vdisplay;
833 }
834
835 return true;
836 }
837
838 /*
839 * Initialize framebuffer console.
840 *
841 * Some notes about boot parameters:
842 * - If "fb=disable" is present, ignore framebuffer completely.
843 * - If "fb=<width>x<height> is present, use the specified mode.
844 * - If "console=fb" is present, attach framebuffer to console.
845 */
846 static bool
847 rpi_fb_init(prop_dictionary_t dict, void *aux)
848 {
849 uint32_t width = 0, height = 0;
850 uint32_t res;
851 char *ptr;
852 int integer;
853 int error;
854 bool is_bgr = true;
855
856 if (get_bootconf_option(boot_args, "fb",
857 BOOTOPT_TYPE_STRING, &ptr)) {
858 if (rpi_fb_parse_mode(ptr, &width, &height) == false)
859 return false;
860 }
861 if (width == 0 || height == 0) {
862 rpi_fb_get_edid_mode(&width, &height);
863 }
864 if (width == 0 || height == 0) {
865 width = RPI_FB_WIDTH;
866 height = RPI_FB_HEIGHT;
867 }
868
869 vb_setfb.vbt_res.width = width;
870 vb_setfb.vbt_res.height = height;
871 vb_setfb.vbt_vres.width = width;
872 vb_setfb.vbt_vres.height = height;
873 error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb_setfb,
874 sizeof(vb_setfb), &res);
875 if (error) {
876 printf("%s: mbox request failed (%d)\n", __func__, error);
877 return false;
878 }
879
880 if (!vcprop_buffer_success_p(&vb_setfb.vb_hdr) ||
881 !vcprop_tag_success_p(&vb_setfb.vbt_res.tag) ||
882 !vcprop_tag_success_p(&vb_setfb.vbt_vres.tag) ||
883 !vcprop_tag_success_p(&vb_setfb.vbt_depth.tag) ||
884 !vcprop_tag_success_p(&vb_setfb.vbt_allocbuf.tag) ||
885 !vcprop_tag_success_p(&vb_setfb.vbt_blank.tag) ||
886 !vcprop_tag_success_p(&vb_setfb.vbt_pitch.tag)) {
887 printf("%s: prop tag failed\n", __func__);
888 return false;
889 }
890
891 #ifdef VERBOSE_INIT_ARM
892 printf("%s: addr = 0x%x size = %d\n", __func__,
893 vb_setfb.vbt_allocbuf.address,
894 vb_setfb.vbt_allocbuf.size);
895 printf("%s: depth = %d\n", __func__, vb_setfb.vbt_depth.bpp);
896 printf("%s: pitch = %d\n", __func__,
897 vb_setfb.vbt_pitch.linebytes);
898 printf("%s: width = %d height = %d\n", __func__,
899 vb_setfb.vbt_res.width, vb_setfb.vbt_res.height);
900 printf("%s: vwidth = %d vheight = %d\n", __func__,
901 vb_setfb.vbt_vres.width, vb_setfb.vbt_vres.height);
902 #endif
903
904 if (vb_setfb.vbt_allocbuf.address == 0 ||
905 vb_setfb.vbt_allocbuf.size == 0 ||
906 vb_setfb.vbt_res.width == 0 ||
907 vb_setfb.vbt_res.height == 0 ||
908 vb_setfb.vbt_vres.width == 0 ||
909 vb_setfb.vbt_vres.height == 0 ||
910 vb_setfb.vbt_pitch.linebytes == 0) {
911 printf("%s: failed to set mode %ux%u\n", __func__,
912 width, height);
913 return false;
914 }
915
916 prop_dictionary_set_uint32(dict, "width", vb_setfb.vbt_res.width);
917 prop_dictionary_set_uint32(dict, "height", vb_setfb.vbt_res.height);
918 prop_dictionary_set_uint8(dict, "depth", vb_setfb.vbt_depth.bpp);
919 prop_dictionary_set_uint16(dict, "linebytes",
920 vb_setfb.vbt_pitch.linebytes);
921 prop_dictionary_set_uint32(dict, "address",
922 vb_setfb.vbt_allocbuf.address);
923
924 /*
925 * Old firmware uses BGR. New firmware uses RGB. The get and set
926 * pixel order mailbox properties don't seem to work. The firmware
927 * adds a kernel cmdline option bcm2708_fb.fbswap=<0|1>, so use it
928 * to determine pixel order. 0 means BGR, 1 means RGB.
929 *
930 * See https://github.com/raspberrypi/linux/issues/514
931 */
932 if (get_bootconf_option(boot_args, "bcm2708_fb.fbswap",
933 BOOTOPT_TYPE_INT, &integer)) {
934 is_bgr = integer == 0;
935 }
936 prop_dictionary_set_bool(dict, "is_bgr", is_bgr);
937
938 /* if "genfb.type=<n>" is passed in cmdline, override wsdisplay type */
939 if (get_bootconf_option(boot_args, "genfb.type",
940 BOOTOPT_TYPE_INT, &integer)) {
941 prop_dictionary_set_uint32(dict, "wsdisplay_type", integer);
942 }
943
944 #if defined(RPI_HWCURSOR)
945 struct fdt_attach_args *faa = aux;
946 bus_space_handle_t hc;
947
948 hcursor = rpi_alloc_mem(CURSOR_ARGB_SIZE, PAGE_SIZE,
949 MEM_FLAG_L1_NONALLOCATING | MEM_FLAG_HINT_PERMALOCK);
950 pcursor = rpi_lock_mem(hcursor);
951 #ifdef RPI_IOCTL_DEBUG
952 printf("hcursor: %08x\n", hcursor);
953 printf("pcursor: %08x\n", (uint32_t)pcursor);
954 printf("fb: %08x\n", (uint32_t)vb_setfb.vbt_allocbuf.address);
955 #endif
956 if (bus_space_map(faa->faa_bst, pcursor, CURSOR_ARGB_SIZE,
957 BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE, &hc) != 0) {
958 printf("couldn't map cursor memory\n");
959 } else {
960 int i, j, k;
961
962 cmem = bus_space_vaddr(faa->faa_bst, hc);
963 k = 0;
964 for (j = 0; j < 64; j++) {
965 for (i = 0; i < 64; i++) {
966 cmem[i + k] =
967 ((i & 8) ^ (j & 8)) ? 0xa0ff0000 : 0xa000ff00;
968 }
969 k += 64;
970 }
971 cpu_dcache_wb_range((vaddr_t)cmem, CURSOR_ARGB_SIZE);
972 rpi_fb_initcursor(pcursor, 0, 0);
973 #ifdef RPI_IOCTL_DEBUG
974 rpi_fb_movecursor(600, 400, 1);
975 #else
976 rpi_fb_movecursor(cursor_x, cursor_y, cursor_on);
977 #endif
978 }
979 #endif
980
981 return true;
982 }
983
984
985 #if defined(RPI_HWCURSOR)
986 static int
987 rpi_fb_do_cursor(struct wsdisplay_cursor *cur)
988 {
989 int pos = 0;
990 int shape = 0;
991
992 if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
993 if (cursor_on != cur->enable) {
994 cursor_on = cur->enable;
995 pos = 1;
996 }
997 }
998 if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
999
1000 hot_x = cur->hot.x;
1001 hot_y = cur->hot.y;
1002 pos = 1;
1003 shape = 1;
1004 }
1005 if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
1006
1007 cursor_x = cur->pos.x;
1008 cursor_y = cur->pos.y;
1009 pos = 1;
1010 }
1011 if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
1012 int i;
1013 uint32_t val;
1014
1015 for (i = 0; i < uimin(cur->cmap.count, 3); i++) {
1016 val = (cur->cmap.red[i] << 16 ) |
1017 (cur->cmap.green[i] << 8) |
1018 (cur->cmap.blue[i] ) |
1019 0xff000000;
1020 cursor_cmap[i + cur->cmap.index + 2] = val;
1021 }
1022 shape = 1;
1023 }
1024 if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
1025 int err;
1026
1027 err = copyin(cur->mask, cursor_mask, CURSOR_BITMAP_SIZE);
1028 err += copyin(cur->image, cursor_bitmap, CURSOR_BITMAP_SIZE);
1029 if (err != 0)
1030 return EFAULT;
1031 shape = 1;
1032 }
1033 if (shape) {
1034 int i, j, idx;
1035 uint8_t mask;
1036
1037 for (i = 0; i < CURSOR_BITMAP_SIZE; i++) {
1038 mask = 0x01;
1039 for (j = 0; j < 8; j++) {
1040 idx = ((cursor_mask[i] & mask) ? 2 : 0) |
1041 ((cursor_bitmap[i] & mask) ? 1 : 0);
1042 cmem[i * 8 + j] = cursor_cmap[idx];
1043 mask = mask << 1;
1044 }
1045 }
1046 /* just in case */
1047 cpu_dcache_wb_range((vaddr_t)cmem, CURSOR_ARGB_SIZE);
1048 rpi_fb_initcursor(pcursor, hot_x, hot_y);
1049 }
1050 if (pos) {
1051 rpi_fb_movecursor(cursor_x, cursor_y, cursor_on);
1052 }
1053 return 0;
1054 }
1055 #endif
1056
1057 static int
1058 rpi_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
1059 {
1060
1061 switch (cmd) {
1062 case WSDISPLAYIO_SVIDEO:
1063 {
1064 int d = *(int *)data;
1065 if (d == rpi_video_on)
1066 return 0;
1067 rpi_video_on = d;
1068 rpi_fb_set_video(d);
1069 #if defined(RPI_HWCURSOR)
1070 rpi_fb_movecursor(cursor_x, cursor_y,
1071 d ? cursor_on : 0);
1072 #endif
1073 }
1074 return 0;
1075 case WSDISPLAYIO_GVIDEO:
1076 *(int *)data = rpi_video_on;
1077 return 0;
1078 #if defined(RPI_HWCURSOR)
1079 case WSDISPLAYIO_GCURPOS:
1080 {
1081 struct wsdisplay_curpos *cp = (void *)data;
1082
1083 cp->x = cursor_x;
1084 cp->y = cursor_y;
1085 }
1086 return 0;
1087 case WSDISPLAYIO_SCURPOS:
1088 {
1089 struct wsdisplay_curpos *cp = (void *)data;
1090
1091 cursor_x = cp->x;
1092 cursor_y = cp->y;
1093 rpi_fb_movecursor(cursor_x, cursor_y, cursor_on);
1094 }
1095 return 0;
1096 case WSDISPLAYIO_GCURMAX:
1097 {
1098 struct wsdisplay_curpos *cp = (void *)data;
1099
1100 cp->x = 64;
1101 cp->y = 64;
1102 }
1103 return 0;
1104 case WSDISPLAYIO_SCURSOR:
1105 {
1106 struct wsdisplay_cursor *cursor = (void *)data;
1107
1108 return rpi_fb_do_cursor(cursor);
1109 }
1110 #endif
1111 default:
1112 return EPASSTHROUGH;
1113 }
1114 }
1115
1116 #endif
1117
1118 SYSCTL_SETUP(sysctl_machdep_rpi, "sysctl machdep subtree setup (rpi)")
1119 {
1120 sysctl_createv(clog, 0, NULL, NULL,
1121 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
1122 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
1123
1124 sysctl_createv(clog, 0, NULL, NULL,
1125 CTLFLAG_PERMANENT|CTLFLAG_READONLY,
1126 CTLTYPE_INT, "firmware_revision", NULL, NULL, 0,
1127 &vb.vbt_fwrev.rev, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
1128
1129 sysctl_createv(clog, 0, NULL, NULL,
1130 CTLFLAG_PERMANENT|CTLFLAG_READONLY,
1131 CTLTYPE_INT, "board_model", NULL, NULL, 0,
1132 &vb.vbt_boardmodel.model, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
1133
1134 sysctl_createv(clog, 0, NULL, NULL,
1135 CTLFLAG_PERMANENT|CTLFLAG_READONLY,
1136 CTLTYPE_INT, "board_revision", NULL, NULL, 0,
1137 &vb.vbt_boardrev.rev, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
1138
1139 sysctl_createv(clog, 0, NULL, NULL,
1140 CTLFLAG_PERMANENT|CTLFLAG_READONLY|CTLFLAG_HEX|CTLFLAG_PRIVATE,
1141 CTLTYPE_QUAD, "serial", NULL, NULL, 0,
1142 &vb.vbt_serial.sn, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
1143 }
1144
1145 #if defined(SOC_BCM2835)
1146 static void
1147 bcm2835_platform_bootstrap(void)
1148 {
1149
1150 bcm2835_bs_tag = arm_generic_bs_tag;
1151 bcm2835_a4x_bs_tag = arm_generic_a4x_bs_tag;
1152
1153 bcm2835_bs_tag.bs_map = bcm2835_bs_map;
1154 bcm2835_bs_tag.bs_mmap = bcm2835_bs_mmap;
1155 bcm2835_a4x_bs_tag.bs_map = bcm2835_bs_map;
1156 bcm2835_a4x_bs_tag.bs_mmap = bcm2835_a4x_bs_mmap;
1157
1158 fdtbus_set_decoderegprop(false);
1159
1160 bcm2835_uartinit();
1161
1162 bcm2835_bootparams();
1163 }
1164 #endif
1165
1166 #if defined(SOC_BCM2836)
1167 static void
1168 bcm2836_platform_bootstrap(void)
1169 {
1170
1171 bcm2836_bs_tag = arm_generic_bs_tag;
1172 bcm2836_a4x_bs_tag = arm_generic_a4x_bs_tag;
1173
1174 bcm2836_bs_tag.bs_map = bcm2836_bs_map;
1175 bcm2836_bs_tag.bs_mmap = bcm2836_bs_mmap;
1176 bcm2836_a4x_bs_tag.bs_map = bcm2836_bs_map;
1177 bcm2836_a4x_bs_tag.bs_mmap = bcm2836_a4x_bs_mmap;
1178
1179 fdtbus_set_decoderegprop(false);
1180
1181 bcm2836_uartinit();
1182
1183 bcm2836_bootparams();
1184
1185 #ifdef MULTIPROCESSOR
1186 arm_cpu_max = RPI_CPU_MAX;
1187 arm_fdt_cpu_bootstrap();
1188 #endif
1189 }
1190 #endif
1191
1192 #if defined(SOC_BCM2835)
1193 static void
1194 bcm2835_platform_init_attach_args(struct fdt_attach_args *faa)
1195 {
1196
1197 faa->faa_bst = &bcm2835_bs_tag;
1198 faa->faa_a4x_bst = &bcm2835_a4x_bs_tag;
1199 faa->faa_dmat = &bcm2835_bus_dma_tag;
1200
1201 bcm2835_bus_dma_tag._ranges = bcm2835_dma_ranges;
1202 bcm2835_bus_dma_tag._nranges = __arraycount(bcm2835_dma_ranges);
1203 bcm2835_dma_ranges[0].dr_len = bcm283x_memorysize;
1204 }
1205 #endif
1206
1207 #if defined(SOC_BCM2836)
1208 static void
1209 bcm2836_platform_init_attach_args(struct fdt_attach_args *faa)
1210 {
1211
1212 faa->faa_bst = &bcm2836_bs_tag;
1213 faa->faa_a4x_bst = &bcm2836_a4x_bs_tag;
1214 faa->faa_dmat = &bcm2835_bus_dma_tag;
1215
1216 bcm2835_bus_dma_tag._ranges = bcm2836_dma_ranges;
1217 bcm2835_bus_dma_tag._nranges = __arraycount(bcm2836_dma_ranges);
1218 bcm2836_dma_ranges[0].dr_len = bcm283x_memorysize;
1219 }
1220 #endif
1221
1222
1223 static void
1224 bcm283x_platform_early_putchar(vaddr_t va, paddr_t pa, char c)
1225 {
1226 volatile uint32_t *uartaddr =
1227 cpu_earlydevice_va_p() ?
1228 (volatile uint32_t *)va :
1229 (volatile uint32_t *)pa;
1230
1231 while ((uartaddr[PL01XCOM_FR / 4] & PL01X_FR_TXFF) != 0)
1232 continue;
1233
1234 uartaddr[PL01XCOM_DR / 4] = c;
1235
1236 while ((uartaddr[PL01XCOM_FR / 4] & PL01X_FR_TXFE) == 0)
1237 continue;
1238 }
1239
1240 void
1241 bcm2835_platform_early_putchar(char c)
1242 {
1243 paddr_t pa = BCM2835_PERIPHERALS_BUS_TO_PHYS(BCM2835_UART0_BASE);
1244 vaddr_t va = BCM2835_IOPHYSTOVIRT(pa);
1245
1246 bcm283x_platform_early_putchar(va, pa, c);
1247 }
1248
1249 void
1250 bcm2836_platform_early_putchar(char c)
1251 {
1252 paddr_t pa = BCM2836_PERIPHERALS_BUS_TO_PHYS(BCM2835_UART0_BASE);
1253 vaddr_t va = BCM2835_IOPHYSTOVIRT(pa);
1254
1255 bcm283x_platform_early_putchar(va, pa, c);
1256 }
1257
1258 #define BCM283x_REF_FREQ 19200000
1259
1260 void
1261 bcm2837_platform_early_putchar(char c)
1262 {
1263 #define AUCONSADDR_PA BCM2836_PERIPHERALS_BUS_TO_PHYS(BCM2835_AUX_UART_BASE)
1264 #define AUCONSADDR_VA BCM2835_IOPHYSTOVIRT(AUCONSADDR_PA)
1265 volatile uint32_t *uartaddr =
1266 cpu_earlydevice_va_p() ?
1267 (volatile uint32_t *)AUCONSADDR_VA :
1268 (volatile uint32_t *)AUCONSADDR_PA;
1269
1270 while ((uartaddr[com_lsr] & LSR_TXRDY) == 0)
1271 ;
1272
1273 uartaddr[com_data] = c;
1274 }
1275
1276 static void
1277 bcm283x_platform_device_register(device_t dev, void *aux)
1278 {
1279 prop_dictionary_t dict = device_properties(dev);
1280
1281 if (device_is_a(dev, "bcmdmac") &&
1282 vcprop_tag_success_p(&vb.vbt_dmachan.tag)) {
1283 prop_dictionary_set_uint32(dict,
1284 "chanmask", vb.vbt_dmachan.mask);
1285 }
1286 #if NSDHC > 0
1287 if (booted_device == NULL &&
1288 device_is_a(dev, "ld") &&
1289 device_is_a(device_parent(dev), "sdmmc")) {
1290 booted_partition = 0;
1291 booted_device = dev;
1292 }
1293 #endif
1294 if ((device_is_a(dev, "usmsc") || device_is_a(dev, "mue")) &&
1295 vcprop_tag_success_p(&vb.vbt_macaddr.tag)) {
1296 const uint8_t enaddr[ETHER_ADDR_LEN] = {
1297 (vb.vbt_macaddr.addr >> 0) & 0xff,
1298 (vb.vbt_macaddr.addr >> 8) & 0xff,
1299 (vb.vbt_macaddr.addr >> 16) & 0xff,
1300 (vb.vbt_macaddr.addr >> 24) & 0xff,
1301 (vb.vbt_macaddr.addr >> 32) & 0xff,
1302 (vb.vbt_macaddr.addr >> 40) & 0xff
1303 };
1304
1305 prop_data_t pd = prop_data_create_data(enaddr, ETHER_ADDR_LEN);
1306 KASSERT(pd != NULL);
1307 if (prop_dictionary_set(device_properties(dev), "mac-address",
1308 pd) == false) {
1309 aprint_error_dev(dev,
1310 "WARNING: Unable to set mac-address property\n");
1311 }
1312 prop_object_release(pd);
1313 }
1314
1315 #if NGENFB > 0
1316 if (device_is_a(dev, "genfb")) {
1317 char *ptr;
1318
1319 bcmgenfb_set_console_dev(dev);
1320 bcmgenfb_set_ioctl(&rpi_ioctl);
1321 #ifdef DDB
1322 db_trap_callback = bcmgenfb_ddb_trap_callback;
1323 #endif
1324 if (rpi_fb_init(dict, aux) == false)
1325 return;
1326 if (get_bootconf_option(boot_args, "console",
1327 BOOTOPT_TYPE_STRING, &ptr) && strncmp(ptr, "fb", 2) == 0) {
1328 prop_dictionary_set_bool(dict, "is_console", true);
1329 #if NUKBD > 0
1330 /* allow ukbd to be the console keyboard */
1331 ukbd_cnattach();
1332 #endif
1333 } else {
1334 prop_dictionary_set_bool(dict, "is_console", false);
1335 }
1336 }
1337 #endif
1338 }
1339
1340 static u_int
1341 bcm283x_platform_uart_freq(void)
1342 {
1343
1344 return uart_clk;
1345 }
1346
1347 #if defined(SOC_BCM2835)
1348 static const struct arm_platform bcm2835_platform = {
1349 .ap_devmap = bcm2835_platform_devmap,
1350 .ap_bootstrap = bcm2835_platform_bootstrap,
1351 .ap_init_attach_args = bcm2835_platform_init_attach_args,
1352 .ap_device_register = bcm283x_platform_device_register,
1353 .ap_reset = bcm2835_system_reset,
1354 .ap_delay = bcm2835_tmr_delay,
1355 .ap_uart_freq = bcm283x_platform_uart_freq,
1356 };
1357
1358 ARM_PLATFORM(bcm2835, "brcm,bcm2835", &bcm2835_platform);
1359 #endif
1360
1361 #if defined(SOC_BCM2836)
1362 static u_int
1363 bcm2837_platform_uart_freq(void)
1364 {
1365
1366 return core_clk * 2;
1367 }
1368
1369 static const struct arm_platform bcm2836_platform = {
1370 .ap_devmap = bcm2836_platform_devmap,
1371 .ap_bootstrap = bcm2836_platform_bootstrap,
1372 .ap_init_attach_args = bcm2836_platform_init_attach_args,
1373 .ap_device_register = bcm283x_platform_device_register,
1374 .ap_reset = bcm2835_system_reset,
1375 .ap_delay = gtmr_delay,
1376 .ap_uart_freq = bcm283x_platform_uart_freq,
1377 .ap_mpstart = bcm2836_mpstart,
1378 };
1379
1380 static const struct arm_platform bcm2837_platform = {
1381 .ap_devmap = bcm2836_platform_devmap,
1382 .ap_bootstrap = bcm2836_platform_bootstrap,
1383 .ap_init_attach_args = bcm2836_platform_init_attach_args,
1384 .ap_device_register = bcm283x_platform_device_register,
1385 .ap_reset = bcm2835_system_reset,
1386 .ap_delay = gtmr_delay,
1387 .ap_uart_freq = bcm2837_platform_uart_freq,
1388 .ap_mpstart = bcm2836_mpstart,
1389 };
1390
1391 ARM_PLATFORM(bcm2836, "brcm,bcm2836", &bcm2836_platform);
1392 ARM_PLATFORM(bcm2837, "brcm,bcm2837", &bcm2837_platform);
1393 #endif
1394