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