Home | History | Annotate | Line # | Download | only in broadcom
bcm283x_platform.c revision 1.23
      1 /*	$NetBSD: bcm283x_platform.c,v 1.23 2019/01/03 12:52:40 jmcneill 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.23 2019/01/03 12:52:40 jmcneill 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 #if defined(MULTIPROCESSOR)
    728 static int
    729 cpu_enable_bcm2836(int phandle)
    730 {
    731 	bus_space_tag_t iot = &bcm2836_bs_tag;
    732 	bus_space_handle_t ioh = BCM2836_ARM_LOCAL_VBASE;
    733 	uint64_t mpidr;
    734 
    735 	fdtbus_get_reg64(phandle, 0, &mpidr, NULL);
    736 
    737 	const u_int cpuno = __SHIFTOUT(mpidr, MPIDR_AFF0);
    738 
    739 	bus_space_write_4(iot, ioh, BCM2836_LOCAL_MAILBOX3_SETN(cpuno),
    740 	    KERN_VTOPHYS((vaddr_t)cpu_mpstart));
    741 
    742 	return 0;
    743 }
    744 ARM_CPU_METHOD(bcm2836, "brcm,bcm2836-smp", cpu_enable_bcm2836);
    745 #endif
    746 
    747 #endif	/* SOC_BCM2836 */
    748 
    749 #if NGENFB > 0
    750 static bool
    751 rpi_fb_parse_mode(const char *s, uint32_t *pwidth, uint32_t *pheight)
    752 {
    753 	char *x;
    754 
    755 	if (strncmp(s, "disable", 7) == 0)
    756 		return false;
    757 
    758 	x = strchr(s, 'x');
    759 	if (x) {
    760 		*pwidth = strtoul(s, NULL, 10);
    761 		*pheight = strtoul(x + 1, NULL, 10);
    762 	}
    763 
    764 	return true;
    765 }
    766 
    767 static bool
    768 rpi_fb_get_edid_mode(uint32_t *pwidth, uint32_t *pheight)
    769 {
    770 	struct edid_info ei;
    771 	uint8_t edid_data[1024];
    772 	uint32_t res;
    773 	int error;
    774 
    775 	error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb_edid,
    776 	    sizeof(vb_edid), &res);
    777 	if (error) {
    778 		printf("%s: mbox request failed (%d)\n", __func__, error);
    779 		return false;
    780 	}
    781 
    782 	if (!vcprop_buffer_success_p(&vb_edid.vb_hdr) ||
    783 	    !vcprop_tag_success_p(&vb_edid.vbt_edid.tag) ||
    784 	    vb_edid.vbt_edid.status != 0)
    785 		return false;
    786 
    787 	memset(edid_data, 0, sizeof(edid_data));
    788 	memcpy(edid_data, vb_edid.vbt_edid.data,
    789 	    sizeof(vb_edid.vbt_edid.data));
    790 	edid_parse(edid_data, &ei);
    791 #ifdef VERBOSE_INIT_ARM
    792 	edid_print(&ei);
    793 #endif
    794 
    795 	if (ei.edid_preferred_mode) {
    796 		*pwidth = ei.edid_preferred_mode->hdisplay;
    797 		*pheight = ei.edid_preferred_mode->vdisplay;
    798 	}
    799 
    800 	return true;
    801 }
    802 
    803 /*
    804  * Initialize framebuffer console.
    805  *
    806  * Some notes about boot parameters:
    807  *  - If "fb=disable" is present, ignore framebuffer completely.
    808  *  - If "fb=<width>x<height> is present, use the specified mode.
    809  *  - If "console=fb" is present, attach framebuffer to console.
    810  */
    811 static bool
    812 rpi_fb_init(prop_dictionary_t dict, void *aux)
    813 {
    814 	uint32_t width = 0, height = 0;
    815 	uint32_t res;
    816 	char *ptr;
    817 	int integer;
    818 	int error;
    819 	bool is_bgr = true;
    820 
    821 	if (get_bootconf_option(boot_args, "fb",
    822 			      BOOTOPT_TYPE_STRING, &ptr)) {
    823 		if (rpi_fb_parse_mode(ptr, &width, &height) == false)
    824 			return false;
    825 	}
    826 	if (width == 0 || height == 0) {
    827 		rpi_fb_get_edid_mode(&width, &height);
    828 	}
    829 	if (width == 0 || height == 0) {
    830 		width = RPI_FB_WIDTH;
    831 		height = RPI_FB_HEIGHT;
    832 	}
    833 
    834 	vb_setfb.vbt_res.width = width;
    835 	vb_setfb.vbt_res.height = height;
    836 	vb_setfb.vbt_vres.width = width;
    837 	vb_setfb.vbt_vres.height = height;
    838 	error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb_setfb,
    839 	    sizeof(vb_setfb), &res);
    840 	if (error) {
    841 		printf("%s: mbox request failed (%d)\n", __func__, error);
    842 		return false;
    843 	}
    844 
    845 	if (!vcprop_buffer_success_p(&vb_setfb.vb_hdr) ||
    846 	    !vcprop_tag_success_p(&vb_setfb.vbt_res.tag) ||
    847 	    !vcprop_tag_success_p(&vb_setfb.vbt_vres.tag) ||
    848 	    !vcprop_tag_success_p(&vb_setfb.vbt_depth.tag) ||
    849 	    !vcprop_tag_success_p(&vb_setfb.vbt_allocbuf.tag) ||
    850 	    !vcprop_tag_success_p(&vb_setfb.vbt_blank.tag) ||
    851 	    !vcprop_tag_success_p(&vb_setfb.vbt_pitch.tag)) {
    852 		printf("%s: prop tag failed\n", __func__);
    853 		return false;
    854 	}
    855 
    856 #ifdef VERBOSE_INIT_ARM
    857 	printf("%s: addr = 0x%x size = %d\n", __func__,
    858 	    vb_setfb.vbt_allocbuf.address,
    859 	    vb_setfb.vbt_allocbuf.size);
    860 	printf("%s: depth = %d\n", __func__, vb_setfb.vbt_depth.bpp);
    861 	printf("%s: pitch = %d\n", __func__,
    862 	    vb_setfb.vbt_pitch.linebytes);
    863 	printf("%s: width = %d height = %d\n", __func__,
    864 	    vb_setfb.vbt_res.width, vb_setfb.vbt_res.height);
    865 	printf("%s: vwidth = %d vheight = %d\n", __func__,
    866 	    vb_setfb.vbt_vres.width, vb_setfb.vbt_vres.height);
    867 #endif
    868 
    869 	if (vb_setfb.vbt_allocbuf.address == 0 ||
    870 	    vb_setfb.vbt_allocbuf.size == 0 ||
    871 	    vb_setfb.vbt_res.width == 0 ||
    872 	    vb_setfb.vbt_res.height == 0 ||
    873 	    vb_setfb.vbt_vres.width == 0 ||
    874 	    vb_setfb.vbt_vres.height == 0 ||
    875 	    vb_setfb.vbt_pitch.linebytes == 0) {
    876 		printf("%s: failed to set mode %ux%u\n", __func__,
    877 		    width, height);
    878 		return false;
    879 	}
    880 
    881 	prop_dictionary_set_uint32(dict, "width", vb_setfb.vbt_res.width);
    882 	prop_dictionary_set_uint32(dict, "height", vb_setfb.vbt_res.height);
    883 	prop_dictionary_set_uint8(dict, "depth", vb_setfb.vbt_depth.bpp);
    884 	prop_dictionary_set_uint16(dict, "linebytes",
    885 	    vb_setfb.vbt_pitch.linebytes);
    886 	prop_dictionary_set_uint32(dict, "address",
    887 	    vb_setfb.vbt_allocbuf.address);
    888 
    889 	/*
    890 	 * Old firmware uses BGR. New firmware uses RGB. The get and set
    891 	 * pixel order mailbox properties don't seem to work. The firmware
    892 	 * adds a kernel cmdline option bcm2708_fb.fbswap=<0|1>, so use it
    893 	 * to determine pixel order. 0 means BGR, 1 means RGB.
    894 	 *
    895 	 * See https://github.com/raspberrypi/linux/issues/514
    896 	 */
    897 	if (get_bootconf_option(boot_args, "bcm2708_fb.fbswap",
    898 				BOOTOPT_TYPE_INT, &integer)) {
    899 		is_bgr = integer == 0;
    900 	}
    901 	prop_dictionary_set_bool(dict, "is_bgr", is_bgr);
    902 
    903 	/* if "genfb.type=<n>" is passed in cmdline, override wsdisplay type */
    904 	if (get_bootconf_option(boot_args, "genfb.type",
    905 				BOOTOPT_TYPE_INT, &integer)) {
    906 		prop_dictionary_set_uint32(dict, "wsdisplay_type", integer);
    907 	}
    908 
    909 #if defined(RPI_HWCURSOR)
    910 	struct fdt_attach_args *faa = aux;
    911 	bus_space_handle_t hc;
    912 
    913 	hcursor = rpi_alloc_mem(CURSOR_ARGB_SIZE, PAGE_SIZE,
    914 	    MEM_FLAG_L1_NONALLOCATING | MEM_FLAG_HINT_PERMALOCK);
    915 	pcursor = rpi_lock_mem(hcursor);
    916 #ifdef RPI_IOCTL_DEBUG
    917 	printf("hcursor: %08x\n", hcursor);
    918 	printf("pcursor: %08x\n", (uint32_t)pcursor);
    919 	printf("fb: %08x\n", (uint32_t)vb_setfb.vbt_allocbuf.address);
    920 #endif
    921 	if (bus_space_map(faa->faa_bst, pcursor, CURSOR_ARGB_SIZE,
    922 	    BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE, &hc) != 0) {
    923 		printf("couldn't map cursor memory\n");
    924 	} else {
    925 		int i, j, k;
    926 
    927 		cmem = bus_space_vaddr(faa->faa_bst, hc);
    928 		k = 0;
    929 		for (j = 0; j < 64; j++) {
    930 			for (i = 0; i < 64; i++) {
    931 				cmem[i + k] =
    932 				 ((i & 8) ^ (j & 8)) ? 0xa0ff0000 : 0xa000ff00;
    933 			}
    934 			k += 64;
    935 		}
    936 		cpu_dcache_wb_range((vaddr_t)cmem, CURSOR_ARGB_SIZE);
    937 		rpi_fb_initcursor(pcursor, 0, 0);
    938 #ifdef RPI_IOCTL_DEBUG
    939 		rpi_fb_movecursor(600, 400, 1);
    940 #else
    941 		rpi_fb_movecursor(cursor_x, cursor_y, cursor_on);
    942 #endif
    943 	}
    944 #endif
    945 
    946 	return true;
    947 }
    948 
    949 
    950 #if defined(RPI_HWCURSOR)
    951 static int
    952 rpi_fb_do_cursor(struct wsdisplay_cursor *cur)
    953 {
    954 	int pos = 0;
    955 	int shape = 0;
    956 
    957 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
    958 		if (cursor_on != cur->enable) {
    959 			cursor_on = cur->enable;
    960 			pos = 1;
    961 		}
    962 	}
    963 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
    964 
    965 		hot_x = cur->hot.x;
    966 		hot_y = cur->hot.y;
    967 		pos = 1;
    968 		shape = 1;
    969 	}
    970 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
    971 
    972 		cursor_x = cur->pos.x;
    973 		cursor_y = cur->pos.y;
    974 		pos = 1;
    975 	}
    976 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
    977 		int i;
    978 		uint32_t val;
    979 
    980 		for (i = 0; i < uimin(cur->cmap.count, 3); i++) {
    981 			val = (cur->cmap.red[i] << 16 ) |
    982 			      (cur->cmap.green[i] << 8) |
    983 			      (cur->cmap.blue[i] ) |
    984 			      0xff000000;
    985 			cursor_cmap[i + cur->cmap.index + 2] = val;
    986 		}
    987 		shape = 1;
    988 	}
    989 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
    990 		int err;
    991 
    992 		err = copyin(cur->mask, cursor_mask, CURSOR_BITMAP_SIZE);
    993 		err += copyin(cur->image, cursor_bitmap, CURSOR_BITMAP_SIZE);
    994 		if (err != 0)
    995 			return EFAULT;
    996 		shape = 1;
    997 	}
    998 	if (shape) {
    999 		int i, j, idx;
   1000 		uint8_t mask;
   1001 
   1002 		for (i = 0; i < CURSOR_BITMAP_SIZE; i++) {
   1003 			mask = 0x01;
   1004 			for (j = 0; j < 8; j++) {
   1005 				idx = ((cursor_mask[i] & mask) ? 2 : 0) |
   1006 				    ((cursor_bitmap[i] & mask) ? 1 : 0);
   1007 				cmem[i * 8 + j] = cursor_cmap[idx];
   1008 				mask = mask << 1;
   1009 			}
   1010 		}
   1011 		/* just in case */
   1012 		cpu_dcache_wb_range((vaddr_t)cmem, CURSOR_ARGB_SIZE);
   1013 		rpi_fb_initcursor(pcursor, hot_x, hot_y);
   1014 	}
   1015 	if (pos) {
   1016 		rpi_fb_movecursor(cursor_x, cursor_y, cursor_on);
   1017 	}
   1018 	return 0;
   1019 }
   1020 #endif
   1021 
   1022 static int
   1023 rpi_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
   1024 {
   1025 
   1026 	switch (cmd) {
   1027 	case WSDISPLAYIO_SVIDEO:
   1028 		{
   1029 			int d = *(int *)data;
   1030 			if (d == rpi_video_on)
   1031 				return 0;
   1032 			rpi_video_on = d;
   1033 			rpi_fb_set_video(d);
   1034 #if defined(RPI_HWCURSOR)
   1035 			rpi_fb_movecursor(cursor_x, cursor_y,
   1036 			                  d ? cursor_on : 0);
   1037 #endif
   1038 		}
   1039 		return 0;
   1040 	case WSDISPLAYIO_GVIDEO:
   1041 		*(int *)data = rpi_video_on;
   1042 		return 0;
   1043 #if defined(RPI_HWCURSOR)
   1044 	case WSDISPLAYIO_GCURPOS:
   1045 		{
   1046 			struct wsdisplay_curpos *cp = (void *)data;
   1047 
   1048 			cp->x = cursor_x;
   1049 			cp->y = cursor_y;
   1050 		}
   1051 		return 0;
   1052 	case WSDISPLAYIO_SCURPOS:
   1053 		{
   1054 			struct wsdisplay_curpos *cp = (void *)data;
   1055 
   1056 			cursor_x = cp->x;
   1057 			cursor_y = cp->y;
   1058 			rpi_fb_movecursor(cursor_x, cursor_y, cursor_on);
   1059 		}
   1060 		return 0;
   1061 	case WSDISPLAYIO_GCURMAX:
   1062 		{
   1063 			struct wsdisplay_curpos *cp = (void *)data;
   1064 
   1065 			cp->x = 64;
   1066 			cp->y = 64;
   1067 		}
   1068 		return 0;
   1069 	case WSDISPLAYIO_SCURSOR:
   1070 		{
   1071 			struct wsdisplay_cursor *cursor = (void *)data;
   1072 
   1073 			return rpi_fb_do_cursor(cursor);
   1074 		}
   1075 #endif
   1076 	default:
   1077 		return EPASSTHROUGH;
   1078 	}
   1079 }
   1080 
   1081 #endif
   1082 
   1083 SYSCTL_SETUP(sysctl_machdep_rpi, "sysctl machdep subtree setup (rpi)")
   1084 {
   1085 	sysctl_createv(clog, 0, NULL, NULL,
   1086 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
   1087 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
   1088 
   1089 	sysctl_createv(clog, 0, NULL, NULL,
   1090 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   1091 	    CTLTYPE_INT, "firmware_revision", NULL, NULL, 0,
   1092 	    &vb.vbt_fwrev.rev, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
   1093 
   1094 	sysctl_createv(clog, 0, NULL, NULL,
   1095 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   1096 	    CTLTYPE_INT, "board_model", NULL, NULL, 0,
   1097 	    &vb.vbt_boardmodel.model, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
   1098 
   1099 	sysctl_createv(clog, 0, NULL, NULL,
   1100 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   1101 	    CTLTYPE_INT, "board_revision", NULL, NULL, 0,
   1102 	    &vb.vbt_boardrev.rev, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
   1103 
   1104 	sysctl_createv(clog, 0, NULL, NULL,
   1105 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY|CTLFLAG_HEX|CTLFLAG_PRIVATE,
   1106 	    CTLTYPE_QUAD, "serial", NULL, NULL, 0,
   1107 	    &vb.vbt_serial.sn, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
   1108 }
   1109 
   1110 #if defined(SOC_BCM2835)
   1111 static void
   1112 bcm2835_platform_bootstrap(void)
   1113 {
   1114 
   1115 	bcm2835_bs_tag = arm_generic_bs_tag;
   1116 	bcm2835_a4x_bs_tag = arm_generic_a4x_bs_tag;
   1117 
   1118 	bcm2835_bs_tag.bs_map = bcm2835_bs_map;
   1119 	bcm2835_bs_tag.bs_mmap = bcm2835_bs_mmap;
   1120 	bcm2835_a4x_bs_tag.bs_map = bcm2835_bs_map;
   1121 	bcm2835_a4x_bs_tag.bs_mmap = bcm2835_a4x_bs_mmap;
   1122 
   1123 	fdtbus_set_decoderegprop(false);
   1124 
   1125 	bcm2835_uartinit();
   1126 
   1127 	bcm2835_bootparams();
   1128 }
   1129 #endif
   1130 
   1131 #if defined(SOC_BCM2836)
   1132 static void
   1133 bcm2836_platform_bootstrap(void)
   1134 {
   1135 
   1136 	bcm2836_bs_tag = arm_generic_bs_tag;
   1137 	bcm2836_a4x_bs_tag = arm_generic_a4x_bs_tag;
   1138 
   1139 	bcm2836_bs_tag.bs_map = bcm2836_bs_map;
   1140 	bcm2836_bs_tag.bs_mmap = bcm2836_bs_mmap;
   1141 	bcm2836_a4x_bs_tag.bs_map = bcm2836_bs_map;
   1142 	bcm2836_a4x_bs_tag.bs_mmap = bcm2836_a4x_bs_mmap;
   1143 
   1144 	fdtbus_set_decoderegprop(false);
   1145 
   1146 	bcm2836_uartinit();
   1147 
   1148 	bcm2836_bootparams();
   1149 
   1150 #ifdef MULTIPROCESSOR
   1151 	arm_cpu_max = RPI_CPU_MAX;
   1152 	arm_fdt_cpu_bootstrap();
   1153 #endif
   1154 }
   1155 #endif
   1156 
   1157 #if defined(SOC_BCM2835)
   1158 static void
   1159 bcm2835_platform_init_attach_args(struct fdt_attach_args *faa)
   1160 {
   1161 
   1162 	faa->faa_bst = &bcm2835_bs_tag;
   1163 	faa->faa_a4x_bst = &bcm2835_a4x_bs_tag;
   1164 	faa->faa_dmat = &bcm2835_bus_dma_tag;
   1165 
   1166 	bcm2835_bus_dma_tag._ranges = bcm2835_dma_ranges;
   1167 	bcm2835_bus_dma_tag._nranges = __arraycount(bcm2835_dma_ranges);
   1168 	bcm2835_dma_ranges[0].dr_len = bcm283x_memorysize;
   1169 }
   1170 #endif
   1171 
   1172 #if defined(SOC_BCM2836)
   1173 static void
   1174 bcm2836_platform_init_attach_args(struct fdt_attach_args *faa)
   1175 {
   1176 
   1177 	faa->faa_bst = &bcm2836_bs_tag;
   1178 	faa->faa_a4x_bst = &bcm2836_a4x_bs_tag;
   1179 	faa->faa_dmat = &bcm2835_bus_dma_tag;
   1180 
   1181 	bcm2835_bus_dma_tag._ranges = bcm2836_dma_ranges;
   1182 	bcm2835_bus_dma_tag._nranges = __arraycount(bcm2836_dma_ranges);
   1183 	bcm2836_dma_ranges[0].dr_len = bcm283x_memorysize;
   1184 }
   1185 #endif
   1186 
   1187 
   1188 static void
   1189 bcm283x_platform_early_putchar(vaddr_t va, paddr_t pa, char c)
   1190 {
   1191 	volatile uint32_t *uartaddr =
   1192 	    cpu_earlydevice_va_p() ?
   1193 		(volatile uint32_t *)va :
   1194 		(volatile uint32_t *)pa;
   1195 
   1196 	while ((uartaddr[PL01XCOM_FR / 4] & PL01X_FR_TXFF) != 0)
   1197 		continue;
   1198 
   1199 	uartaddr[PL01XCOM_DR / 4] = c;
   1200 
   1201 	while ((uartaddr[PL01XCOM_FR / 4] & PL01X_FR_TXFE) == 0)
   1202 		continue;
   1203 }
   1204 
   1205 void
   1206 bcm2835_platform_early_putchar(char c)
   1207 {
   1208 	paddr_t pa = BCM2835_PERIPHERALS_BUS_TO_PHYS(BCM2835_UART0_BASE);
   1209 	vaddr_t va = BCM2835_IOPHYSTOVIRT(pa);
   1210 
   1211 	bcm283x_platform_early_putchar(va, pa, c);
   1212 }
   1213 
   1214 void
   1215 bcm2836_platform_early_putchar(char c)
   1216 {
   1217 	paddr_t pa = BCM2836_PERIPHERALS_BUS_TO_PHYS(BCM2835_UART0_BASE);
   1218 	vaddr_t va = BCM2835_IOPHYSTOVIRT(pa);
   1219 
   1220 	bcm283x_platform_early_putchar(va, pa, c);
   1221 }
   1222 
   1223 #define	BCM283x_REF_FREQ	19200000
   1224 
   1225 void
   1226 bcm2837_platform_early_putchar(char c)
   1227 {
   1228 #define AUCONSADDR_PA	BCM2836_PERIPHERALS_BUS_TO_PHYS(BCM2835_AUX_UART_BASE)
   1229 #define AUCONSADDR_VA	BCM2835_IOPHYSTOVIRT(AUCONSADDR_PA)
   1230 	volatile uint32_t *uartaddr =
   1231 	    cpu_earlydevice_va_p() ?
   1232 		(volatile uint32_t *)AUCONSADDR_VA :
   1233 		(volatile uint32_t *)AUCONSADDR_PA;
   1234 
   1235 	while ((uartaddr[com_lsr] & LSR_TXRDY) == 0)
   1236 		;
   1237 
   1238 	uartaddr[com_data] = c;
   1239 }
   1240 
   1241 static void
   1242 bcm283x_platform_device_register(device_t dev, void *aux)
   1243 {
   1244 	prop_dictionary_t dict = device_properties(dev);
   1245 
   1246 	if (device_is_a(dev, "bcmdmac") &&
   1247 	    vcprop_tag_success_p(&vb.vbt_dmachan.tag)) {
   1248 		prop_dictionary_set_uint32(dict,
   1249 		    "chanmask", vb.vbt_dmachan.mask);
   1250 	}
   1251 #if NSDHC > 0
   1252 	if (booted_device == NULL &&
   1253 	    device_is_a(dev, "ld") &&
   1254 	    device_is_a(device_parent(dev), "sdmmc")) {
   1255 		booted_partition = 0;
   1256 		booted_device = dev;
   1257 	}
   1258 #endif
   1259 	if ((device_is_a(dev, "usmsc") || device_is_a(dev, "mue")) &&
   1260 	    vcprop_tag_success_p(&vb.vbt_macaddr.tag)) {
   1261 		const uint8_t enaddr[ETHER_ADDR_LEN] = {
   1262 		     (vb.vbt_macaddr.addr >> 0) & 0xff,
   1263 		     (vb.vbt_macaddr.addr >> 8) & 0xff,
   1264 		     (vb.vbt_macaddr.addr >> 16) & 0xff,
   1265 		     (vb.vbt_macaddr.addr >> 24) & 0xff,
   1266 		     (vb.vbt_macaddr.addr >> 32) & 0xff,
   1267 		     (vb.vbt_macaddr.addr >> 40) & 0xff
   1268 		};
   1269 
   1270 		prop_data_t pd = prop_data_create_data(enaddr, ETHER_ADDR_LEN);
   1271 		KASSERT(pd != NULL);
   1272 		if (prop_dictionary_set(device_properties(dev), "mac-address",
   1273 		    pd) == false) {
   1274 			aprint_error_dev(dev,
   1275 			    "WARNING: Unable to set mac-address property\n");
   1276 		}
   1277 		prop_object_release(pd);
   1278 	}
   1279 
   1280 #if NGENFB > 0
   1281 	if (device_is_a(dev, "genfb")) {
   1282 		char *ptr;
   1283 
   1284 		bcmgenfb_set_console_dev(dev);
   1285 		bcmgenfb_set_ioctl(&rpi_ioctl);
   1286 #ifdef DDB
   1287 		db_trap_callback = bcmgenfb_ddb_trap_callback;
   1288 #endif
   1289 		if (rpi_fb_init(dict, aux) == false)
   1290 			return;
   1291 		if (get_bootconf_option(boot_args, "console",
   1292 		    BOOTOPT_TYPE_STRING, &ptr) && strncmp(ptr, "fb", 2) == 0) {
   1293 			prop_dictionary_set_bool(dict, "is_console", true);
   1294 #if NUKBD > 0
   1295 			/* allow ukbd to be the console keyboard */
   1296 			ukbd_cnattach();
   1297 #endif
   1298 		} else {
   1299 			prop_dictionary_set_bool(dict, "is_console", false);
   1300 		}
   1301 	}
   1302 #endif
   1303 }
   1304 
   1305 static u_int
   1306 bcm283x_platform_uart_freq(void)
   1307 {
   1308 
   1309 	return uart_clk;
   1310 }
   1311 
   1312 #if defined(SOC_BCM2835)
   1313 static const struct arm_platform bcm2835_platform = {
   1314 	.ap_devmap = bcm2835_platform_devmap,
   1315 	.ap_bootstrap = bcm2835_platform_bootstrap,
   1316 	.ap_init_attach_args = bcm2835_platform_init_attach_args,
   1317 	.ap_device_register = bcm283x_platform_device_register,
   1318 	.ap_reset = bcm2835_system_reset,
   1319 	.ap_delay = bcm2835_tmr_delay,
   1320 	.ap_uart_freq = bcm283x_platform_uart_freq,
   1321 };
   1322 
   1323 ARM_PLATFORM(bcm2835, "brcm,bcm2835", &bcm2835_platform);
   1324 #endif
   1325 
   1326 #if defined(SOC_BCM2836)
   1327 static u_int
   1328 bcm2837_platform_uart_freq(void)
   1329 {
   1330 
   1331 	return core_clk * 2;
   1332 }
   1333 
   1334 static const struct arm_platform bcm2836_platform = {
   1335 	.ap_devmap = bcm2836_platform_devmap,
   1336 	.ap_bootstrap = bcm2836_platform_bootstrap,
   1337 	.ap_init_attach_args = bcm2836_platform_init_attach_args,
   1338 	.ap_device_register = bcm283x_platform_device_register,
   1339 	.ap_reset = bcm2835_system_reset,
   1340 	.ap_delay = gtmr_delay,
   1341 	.ap_uart_freq = bcm283x_platform_uart_freq,
   1342 	.ap_mpstart = arm_fdt_cpu_mpstart,
   1343 };
   1344 
   1345 static const struct arm_platform bcm2837_platform = {
   1346 	.ap_devmap = bcm2836_platform_devmap,
   1347 	.ap_bootstrap = bcm2836_platform_bootstrap,
   1348 	.ap_init_attach_args = bcm2836_platform_init_attach_args,
   1349 	.ap_device_register = bcm283x_platform_device_register,
   1350 	.ap_reset = bcm2835_system_reset,
   1351 	.ap_delay = gtmr_delay,
   1352 	.ap_uart_freq = bcm2837_platform_uart_freq,
   1353 	.ap_mpstart = arm_fdt_cpu_mpstart,
   1354 };
   1355 
   1356 ARM_PLATFORM(bcm2836, "brcm,bcm2836", &bcm2836_platform);
   1357 ARM_PLATFORM(bcm2837, "brcm,bcm2837", &bcm2837_platform);
   1358 #endif
   1359