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