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