Home | History | Annotate | Line # | Download | only in broadcom
bcm283x_platform.c revision 1.2.2.5
      1 /*	$NetBSD: bcm283x_platform.c,v 1.2.2.5 2018/09/06 06:55:25 pgoyette Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2017 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: bcm283x_platform.c,v 1.2.2.5 2018/09/06 06:55:25 pgoyette Exp $");
     31 
     32 #include "opt_arm_debug.h"
     33 #include "opt_bcm283x.h"
     34 #include "opt_cpuoptions.h"
     35 #include "opt_ddb.h"
     36 #include "opt_evbarm_boardtype.h"
     37 #include "opt_kgdb.h"
     38 #include "opt_fdt.h"
     39 #include "opt_rpi.h"
     40 #include "opt_vcprop.h"
     41 
     42 #include "sdhc.h"
     43 #include "bcmsdhost.h"
     44 #include "bcmdwctwo.h"
     45 #include "bcmspi.h"
     46 #include "bsciic.h"
     47 #include "plcom.h"
     48 #include "com.h"
     49 #include "genfb.h"
     50 #include "ukbd.h"
     51 
     52 #include <sys/param.h>
     53 #include <sys/bus.h>
     54 #include <sys/cpu.h>
     55 #include <sys/device.h>
     56 #include <sys/termios.h>
     57 
     58 #include <net/if_ether.h>
     59 
     60 #include <prop/proplib.h>
     61 
     62 #include <dev/fdt/fdtvar.h>
     63 
     64 #include <uvm/uvm_extern.h>
     65 
     66 #include <machine/bootconfig.h>
     67 
     68 #include <arm/armreg.h>
     69 #include <arm/cpufunc.h>
     70 
     71 #include <libfdt.h>
     72 
     73 #include <arm/broadcom/bcm2835reg.h>
     74 #include <arm/broadcom/bcm2835var.h>
     75 #include <arm/broadcom/bcm283x_platform.h>
     76 #include <arm/broadcom/bcm2835_intr.h>
     77 #include <arm/broadcom/bcm2835_mbox.h>
     78 #include <arm/broadcom/bcm2835_pmwdogvar.h>
     79 
     80 #include <evbarm/dev/plcomreg.h>
     81 #include <evbarm/dev/plcomvar.h>
     82 #include <evbarm/fdt/machdep.h>
     83 
     84 #include <dev/ic/ns16550reg.h>
     85 #include <dev/ic/comreg.h>
     86 
     87 #include <evbarm/rpi/vcio.h>
     88 #include <evbarm/rpi/vcpm.h>
     89 #include <evbarm/rpi/vcprop.h>
     90 
     91 #include <arm/fdt/arm_fdtvar.h>
     92 
     93 #include <arm/cortex/gtmr_var.h>
     94 
     95 #if NGENFB > 0
     96 #include <dev/videomode/videomode.h>
     97 #include <dev/videomode/edidvar.h>
     98 #include <dev/wscons/wsconsio.h>
     99 #endif
    100 
    101 #if NUKBD > 0
    102 #include <dev/usb/ukbdvar.h>
    103 #endif
    104 
    105 #ifdef DDB
    106 #include <machine/db_machdep.h>
    107 #include <ddb/db_sym.h>
    108 #include <ddb/db_extern.h>
    109 #endif
    110 
    111 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  %zu\n", __func__,
    668 		    bcm283x_memorysize);
    669 	if (vcprop_tag_success_p(&vb.vbt_armclockrate.tag))
    670 		printf("%s: arm clock    %d\n", __func__,
    671 		    vb.vbt_armclockrate.rate);
    672 	if (vcprop_tag_success_p(&vb.vbt_fwrev.tag))
    673 		printf("%s: firmware rev %x\n", __func__,
    674 		    vb.vbt_fwrev.rev);
    675 	if (vcprop_tag_success_p(&vb.vbt_boardmodel.tag))
    676 		printf("%s: board model  %x\n", __func__,
    677 		    vb.vbt_boardmodel.model);
    678 	if (vcprop_tag_success_p(&vb.vbt_macaddr.tag))
    679 		printf("%s: mac-address  %" PRIx64 "\n", __func__,
    680 		    vb.vbt_macaddr.addr);
    681 	if (vcprop_tag_success_p(&vb.vbt_boardrev.tag))
    682 		printf("%s: board rev    %x\n", __func__,
    683 		    vb.vbt_boardrev.rev);
    684 	if (vcprop_tag_success_p(&vb.vbt_serial.tag))
    685 		printf("%s: board serial %" PRIx64 "\n", __func__,
    686 		    vb.vbt_serial.sn);
    687 	if (vcprop_tag_success_p(&vb.vbt_dmachan.tag))
    688 		printf("%s: DMA channel mask 0x%08x\n", __func__,
    689 		    vb.vbt_dmachan.mask);
    690 
    691 	if (vcprop_tag_success_p(&vb.vbt_cmdline.tag))
    692 		printf("%s: cmdline      %s\n", __func__,
    693 		    vb.vbt_cmdline.cmdline);
    694 #endif
    695 }
    696 
    697 #if defined(SOC_BCM2835)
    698 static void
    699 bcm2835_bootparams(void)
    700 {
    701 	const paddr_t pa = BCM2835_PERIPHERALS_BUS_TO_PHYS(BCM2835_ARMMBOX_BASE);
    702 	const bus_space_tag_t iot = &bcm2835_bs_tag;
    703 	const bus_space_handle_t ioh = BCM2835_IOPHYSTOVIRT(pa);
    704 
    705 	bcm283x_bootparams(iot, ioh);
    706 }
    707 #endif
    708 
    709 #if defined(SOC_BCM2836)
    710 static void
    711 bcm2836_bootparams(void)
    712 {
    713 	const paddr_t pa = BCM2836_PERIPHERALS_BUS_TO_PHYS(BCM2835_ARMMBOX_BASE);
    714 	const bus_space_tag_t iot = &bcm2836_bs_tag;
    715 	const bus_space_handle_t ioh = BCM2835_IOPHYSTOVIRT(pa);
    716 
    717 	bcm283x_bootparams(iot, ioh);
    718 }
    719 
    720 static void
    721 bcm2836_bootstrap(void)
    722 {
    723 #define RPI_CPU_MAX	4
    724 
    725 #ifdef VERBOSE_INIT_ARM
    726 #define DPRINTF(...)	printf(__VA_ARGS__)
    727 #else
    728 #define DPRINTF(...)
    729 #endif
    730 
    731 #ifdef MULTIPROCESSOR
    732 	arm_cpu_max = RPI_CPU_MAX;
    733 	DPRINTF("%s: %d cpus present\n", __func__, arm_cpu_max);
    734 #ifdef __arm__
    735 	extern int cortex_mmuinfo;
    736 	cortex_mmuinfo = armreg_ttbr_read();
    737 	DPRINTF("%s: cortex_mmuinfo %x\n", __func__, cortex_mmuinfo);
    738 #endif
    739 #endif /* MULTIPROCESSOR */
    740 
    741 	/*
    742 	 * XXX: TODO:
    743 	 *   should make cpu_fdt_bootstrap() that support spin-table and use it
    744 	 *   to share with arm/aarch64.
    745 	 */
    746 #ifdef __aarch64__
    747 	extern void aarch64_mpstart(void);
    748 	for (int i = 1; i < RPI_CPU_MAX; i++) {
    749 		/* argument for mpstart() */
    750 		arm_cpu_hatch_arg = i;
    751 		cpu_dcache_wb_range((vaddr_t)&arm_cpu_hatch_arg,
    752 		    sizeof(arm_cpu_hatch_arg));
    753 
    754 		/*
    755 		 * Reference:
    756 		 *   armstubs/armstub8.S
    757 		 *   in https://github.com/raspberrypi/tools
    758 		 */
    759 		volatile uint64_t *cpu_release_addr;
    760 #define RPI3_ARMSTUB8_SPINADDR_BASE	0x000000d8
    761 		cpu_release_addr = (void *)
    762 		    AARCH64_PA_TO_KVA(RPI3_ARMSTUB8_SPINADDR_BASE + i * 8);
    763 		*cpu_release_addr =
    764 		    aarch64_kern_vtophys((vaddr_t)aarch64_mpstart);
    765 
    766 		/* need flush cache. secondary processors are cache disabled */
    767 		cpu_dcache_wb_range((vaddr_t)cpu_release_addr,
    768 		    sizeof(cpu_release_addr));
    769 		/* Wake up AP in case firmware has placed it in WFE state */
    770 		__asm __volatile("sev" ::: "memory");
    771 
    772 		/* Wait for APs to start */
    773 		for (int loop = 0; loop < 16; loop++) {
    774 			membar_consumer();
    775 			if (arm_cpu_hatched & __BIT(i))
    776 				break;
    777 			gtmr_delay(10000);
    778 		}
    779 	}
    780 #endif /* __aarch64__ */
    781 
    782 #ifdef __arm__
    783 	/*
    784 	 * Even if no options MULTIPROCESSOR,
    785 	 * It is need to initialize the secondary CPU,
    786 	 * and go into wfi loop (cortex_mpstart),
    787 	 * otherwise system would be freeze...
    788 	 * (because netbsd will use the spinning address)
    789 	 */
    790 	extern void cortex_mpstart(void);
    791 
    792 	for (size_t i = 1; i < RPI_CPU_MAX; i++) {
    793 		bus_space_tag_t iot = &bcm2836_bs_tag;
    794 		bus_space_handle_t ioh = BCM2836_ARM_LOCAL_VBASE;
    795 
    796 		bus_space_write_4(iot, ioh,
    797 		    BCM2836_LOCAL_MAILBOX3_SETN(i),
    798 		    (uint32_t)cortex_mpstart);
    799 		/* Wake up AP in case firmware has placed it in WFE state */
    800 		__asm __volatile("sev" ::: "memory");
    801 
    802 #ifdef MULTIPROCESSOR
    803 		/* Wait for APs to start */
    804 		for (int loop = 0; loop < 16; loop++) {
    805 			membar_consumer();
    806 			if (arm_cpu_hatched & __BIT(i))
    807 				break;
    808 			gtmr_delay(10000);
    809 		}
    810 #endif
    811 	}
    812 #endif
    813 
    814 #ifdef MULTIPROCESSOR
    815 	for (size_t i = 1; i < arm_cpu_max; i++) {
    816 		if ((arm_cpu_hatched & (1 << i)) == 0) {
    817 			printf("%s: warning: cpu%zu failed to hatch\n",
    818 			    __func__, i);
    819 		}
    820 	}
    821 #endif
    822 }
    823 
    824 #endif	/* SOC_BCM2836 */
    825 
    826 #if NGENFB > 0
    827 static bool
    828 rpi_fb_parse_mode(const char *s, uint32_t *pwidth, uint32_t *pheight)
    829 {
    830 	char *x;
    831 
    832 	if (strncmp(s, "disable", 7) == 0)
    833 		return false;
    834 
    835 	x = strchr(s, 'x');
    836 	if (x) {
    837 		*pwidth = strtoul(s, NULL, 10);
    838 		*pheight = strtoul(x + 1, NULL, 10);
    839 	}
    840 
    841 	return true;
    842 }
    843 
    844 static bool
    845 rpi_fb_get_edid_mode(uint32_t *pwidth, uint32_t *pheight)
    846 {
    847 	struct edid_info ei;
    848 	uint8_t edid_data[1024];
    849 	uint32_t res;
    850 	int error;
    851 
    852 	error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb_edid,
    853 	    sizeof(vb_edid), &res);
    854 	if (error) {
    855 		printf("%s: mbox request failed (%d)\n", __func__, error);
    856 		return false;
    857 	}
    858 
    859 	if (!vcprop_buffer_success_p(&vb_edid.vb_hdr) ||
    860 	    !vcprop_tag_success_p(&vb_edid.vbt_edid.tag) ||
    861 	    vb_edid.vbt_edid.status != 0)
    862 		return false;
    863 
    864 	memset(edid_data, 0, sizeof(edid_data));
    865 	memcpy(edid_data, vb_edid.vbt_edid.data,
    866 	    sizeof(vb_edid.vbt_edid.data));
    867 	edid_parse(edid_data, &ei);
    868 #ifdef VERBOSE_INIT_ARM
    869 	edid_print(&ei);
    870 #endif
    871 
    872 	if (ei.edid_preferred_mode) {
    873 		*pwidth = ei.edid_preferred_mode->hdisplay;
    874 		*pheight = ei.edid_preferred_mode->vdisplay;
    875 	}
    876 
    877 	return true;
    878 }
    879 
    880 /*
    881  * Initialize framebuffer console.
    882  *
    883  * Some notes about boot parameters:
    884  *  - If "fb=disable" is present, ignore framebuffer completely.
    885  *  - If "fb=<width>x<height> is present, use the specified mode.
    886  *  - If "console=fb" is present, attach framebuffer to console.
    887  */
    888 static bool
    889 rpi_fb_init(prop_dictionary_t dict, void *aux)
    890 {
    891 	uint32_t width = 0, height = 0;
    892 	uint32_t res;
    893 	char *ptr;
    894 	int integer;
    895 	int error;
    896 	bool is_bgr = true;
    897 
    898 	if (get_bootconf_option(boot_args, "fb",
    899 			      BOOTOPT_TYPE_STRING, &ptr)) {
    900 		if (rpi_fb_parse_mode(ptr, &width, &height) == false)
    901 			return false;
    902 	}
    903 	if (width == 0 || height == 0) {
    904 		rpi_fb_get_edid_mode(&width, &height);
    905 	}
    906 	if (width == 0 || height == 0) {
    907 		width = RPI_FB_WIDTH;
    908 		height = RPI_FB_HEIGHT;
    909 	}
    910 
    911 	vb_setfb.vbt_res.width = width;
    912 	vb_setfb.vbt_res.height = height;
    913 	vb_setfb.vbt_vres.width = width;
    914 	vb_setfb.vbt_vres.height = height;
    915 	error = bcmmbox_request(BCMMBOX_CHANARM2VC, &vb_setfb,
    916 	    sizeof(vb_setfb), &res);
    917 	if (error) {
    918 		printf("%s: mbox request failed (%d)\n", __func__, error);
    919 		return false;
    920 	}
    921 
    922 	if (!vcprop_buffer_success_p(&vb_setfb.vb_hdr) ||
    923 	    !vcprop_tag_success_p(&vb_setfb.vbt_res.tag) ||
    924 	    !vcprop_tag_success_p(&vb_setfb.vbt_vres.tag) ||
    925 	    !vcprop_tag_success_p(&vb_setfb.vbt_depth.tag) ||
    926 	    !vcprop_tag_success_p(&vb_setfb.vbt_allocbuf.tag) ||
    927 	    !vcprop_tag_success_p(&vb_setfb.vbt_blank.tag) ||
    928 	    !vcprop_tag_success_p(&vb_setfb.vbt_pitch.tag)) {
    929 		printf("%s: prop tag failed\n", __func__);
    930 		return false;
    931 	}
    932 
    933 #ifdef VERBOSE_INIT_ARM
    934 	printf("%s: addr = 0x%x size = %d\n", __func__,
    935 	    vb_setfb.vbt_allocbuf.address,
    936 	    vb_setfb.vbt_allocbuf.size);
    937 	printf("%s: depth = %d\n", __func__, vb_setfb.vbt_depth.bpp);
    938 	printf("%s: pitch = %d\n", __func__,
    939 	    vb_setfb.vbt_pitch.linebytes);
    940 	printf("%s: width = %d height = %d\n", __func__,
    941 	    vb_setfb.vbt_res.width, vb_setfb.vbt_res.height);
    942 	printf("%s: vwidth = %d vheight = %d\n", __func__,
    943 	    vb_setfb.vbt_vres.width, vb_setfb.vbt_vres.height);
    944 #endif
    945 
    946 	if (vb_setfb.vbt_allocbuf.address == 0 ||
    947 	    vb_setfb.vbt_allocbuf.size == 0 ||
    948 	    vb_setfb.vbt_res.width == 0 ||
    949 	    vb_setfb.vbt_res.height == 0 ||
    950 	    vb_setfb.vbt_vres.width == 0 ||
    951 	    vb_setfb.vbt_vres.height == 0 ||
    952 	    vb_setfb.vbt_pitch.linebytes == 0) {
    953 		printf("%s: failed to set mode %ux%u\n", __func__,
    954 		    width, height);
    955 		return false;
    956 	}
    957 
    958 	prop_dictionary_set_uint32(dict, "width", vb_setfb.vbt_res.width);
    959 	prop_dictionary_set_uint32(dict, "height", vb_setfb.vbt_res.height);
    960 	prop_dictionary_set_uint8(dict, "depth", vb_setfb.vbt_depth.bpp);
    961 	prop_dictionary_set_uint16(dict, "linebytes",
    962 	    vb_setfb.vbt_pitch.linebytes);
    963 	prop_dictionary_set_uint32(dict, "address",
    964 	    vb_setfb.vbt_allocbuf.address);
    965 
    966 	/*
    967 	 * Old firmware uses BGR. New firmware uses RGB. The get and set
    968 	 * pixel order mailbox properties don't seem to work. The firmware
    969 	 * adds a kernel cmdline option bcm2708_fb.fbswap=<0|1>, so use it
    970 	 * to determine pixel order. 0 means BGR, 1 means RGB.
    971 	 *
    972 	 * See https://github.com/raspberrypi/linux/issues/514
    973 	 */
    974 	if (get_bootconf_option(boot_args, "bcm2708_fb.fbswap",
    975 				BOOTOPT_TYPE_INT, &integer)) {
    976 		is_bgr = integer == 0;
    977 	}
    978 	prop_dictionary_set_bool(dict, "is_bgr", is_bgr);
    979 
    980 	/* if "genfb.type=<n>" is passed in cmdline, override wsdisplay type */
    981 	if (get_bootconf_option(boot_args, "genfb.type",
    982 				BOOTOPT_TYPE_INT, &integer)) {
    983 		prop_dictionary_set_uint32(dict, "wsdisplay_type", integer);
    984 	}
    985 
    986 #if defined(RPI_HWCURSOR)
    987 	struct fdt_attach_args *faa = aux;
    988 	bus_space_handle_t hc;
    989 
    990 	hcursor = rpi_alloc_mem(CURSOR_ARGB_SIZE, PAGE_SIZE,
    991 	    MEM_FLAG_L1_NONALLOCATING | MEM_FLAG_HINT_PERMALOCK);
    992 	pcursor = rpi_lock_mem(hcursor);
    993 #ifdef RPI_IOCTL_DEBUG
    994 	printf("hcursor: %08x\n", hcursor);
    995 	printf("pcursor: %08x\n", (uint32_t)pcursor);
    996 	printf("fb: %08x\n", (uint32_t)vb_setfb.vbt_allocbuf.address);
    997 #endif
    998 	if (bus_space_map(faa->faa_bst, pcursor, CURSOR_ARGB_SIZE,
    999 	    BUS_SPACE_MAP_LINEAR|BUS_SPACE_MAP_PREFETCHABLE, &hc) != 0) {
   1000 		printf("couldn't map cursor memory\n");
   1001 	} else {
   1002 		int i, j, k;
   1003 
   1004 		cmem = bus_space_vaddr(faa->faa_bst, hc);
   1005 		k = 0;
   1006 		for (j = 0; j < 64; j++) {
   1007 			for (i = 0; i < 64; i++) {
   1008 				cmem[i + k] =
   1009 				 ((i & 8) ^ (j & 8)) ? 0xa0ff0000 : 0xa000ff00;
   1010 			}
   1011 			k += 64;
   1012 		}
   1013 		cpu_dcache_wb_range((vaddr_t)cmem, CURSOR_ARGB_SIZE);
   1014 		rpi_fb_initcursor(pcursor, 0, 0);
   1015 #ifdef RPI_IOCTL_DEBUG
   1016 		rpi_fb_movecursor(600, 400, 1);
   1017 #else
   1018 		rpi_fb_movecursor(cursor_x, cursor_y, cursor_on);
   1019 #endif
   1020 	}
   1021 #endif
   1022 
   1023 	return true;
   1024 }
   1025 
   1026 
   1027 #if defined(RPI_HWCURSOR)
   1028 static int
   1029 rpi_fb_do_cursor(struct wsdisplay_cursor *cur)
   1030 {
   1031 	int pos = 0;
   1032 	int shape = 0;
   1033 
   1034 	if (cur->which & WSDISPLAY_CURSOR_DOCUR) {
   1035 		if (cursor_on != cur->enable) {
   1036 			cursor_on = cur->enable;
   1037 			pos = 1;
   1038 		}
   1039 	}
   1040 	if (cur->which & WSDISPLAY_CURSOR_DOHOT) {
   1041 
   1042 		hot_x = cur->hot.x;
   1043 		hot_y = cur->hot.y;
   1044 		pos = 1;
   1045 		shape = 1;
   1046 	}
   1047 	if (cur->which & WSDISPLAY_CURSOR_DOPOS) {
   1048 
   1049 		cursor_x = cur->pos.x;
   1050 		cursor_y = cur->pos.y;
   1051 		pos = 1;
   1052 	}
   1053 	if (cur->which & WSDISPLAY_CURSOR_DOCMAP) {
   1054 		int i;
   1055 		uint32_t val;
   1056 
   1057 		for (i = 0; i < uimin(cur->cmap.count, 3); i++) {
   1058 			val = (cur->cmap.red[i] << 16 ) |
   1059 			      (cur->cmap.green[i] << 8) |
   1060 			      (cur->cmap.blue[i] ) |
   1061 			      0xff000000;
   1062 			cursor_cmap[i + cur->cmap.index + 2] = val;
   1063 		}
   1064 		shape = 1;
   1065 	}
   1066 	if (cur->which & WSDISPLAY_CURSOR_DOSHAPE) {
   1067 		int err;
   1068 
   1069 		err = copyin(cur->mask, cursor_mask, CURSOR_BITMAP_SIZE);
   1070 		err += copyin(cur->image, cursor_bitmap, CURSOR_BITMAP_SIZE);
   1071 		if (err != 0)
   1072 			return EFAULT;
   1073 		shape = 1;
   1074 	}
   1075 	if (shape) {
   1076 		int i, j, idx;
   1077 		uint8_t mask;
   1078 
   1079 		for (i = 0; i < CURSOR_BITMAP_SIZE; i++) {
   1080 			mask = 0x01;
   1081 			for (j = 0; j < 8; j++) {
   1082 				idx = ((cursor_mask[i] & mask) ? 2 : 0) |
   1083 				    ((cursor_bitmap[i] & mask) ? 1 : 0);
   1084 				cmem[i * 8 + j] = cursor_cmap[idx];
   1085 				mask = mask << 1;
   1086 			}
   1087 		}
   1088 		/* just in case */
   1089 		cpu_dcache_wb_range((vaddr_t)cmem, CURSOR_ARGB_SIZE);
   1090 		rpi_fb_initcursor(pcursor, hot_x, hot_y);
   1091 	}
   1092 	if (pos) {
   1093 		rpi_fb_movecursor(cursor_x, cursor_y, cursor_on);
   1094 	}
   1095 	return 0;
   1096 }
   1097 #endif
   1098 
   1099 static int
   1100 rpi_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
   1101 {
   1102 
   1103 	switch (cmd) {
   1104 	case WSDISPLAYIO_SVIDEO:
   1105 		{
   1106 			int d = *(int *)data;
   1107 			if (d == rpi_video_on)
   1108 				return 0;
   1109 			rpi_video_on = d;
   1110 			rpi_fb_set_video(d);
   1111 #if defined(RPI_HWCURSOR)
   1112 			rpi_fb_movecursor(cursor_x, cursor_y,
   1113 			                  d ? cursor_on : 0);
   1114 #endif
   1115 		}
   1116 		return 0;
   1117 	case WSDISPLAYIO_GVIDEO:
   1118 		*(int *)data = rpi_video_on;
   1119 		return 0;
   1120 #if defined(RPI_HWCURSOR)
   1121 	case WSDISPLAYIO_GCURPOS:
   1122 		{
   1123 			struct wsdisplay_curpos *cp = (void *)data;
   1124 
   1125 			cp->x = cursor_x;
   1126 			cp->y = cursor_y;
   1127 		}
   1128 		return 0;
   1129 	case WSDISPLAYIO_SCURPOS:
   1130 		{
   1131 			struct wsdisplay_curpos *cp = (void *)data;
   1132 
   1133 			cursor_x = cp->x;
   1134 			cursor_y = cp->y;
   1135 			rpi_fb_movecursor(cursor_x, cursor_y, cursor_on);
   1136 		}
   1137 		return 0;
   1138 	case WSDISPLAYIO_GCURMAX:
   1139 		{
   1140 			struct wsdisplay_curpos *cp = (void *)data;
   1141 
   1142 			cp->x = 64;
   1143 			cp->y = 64;
   1144 		}
   1145 		return 0;
   1146 	case WSDISPLAYIO_SCURSOR:
   1147 		{
   1148 			struct wsdisplay_cursor *cursor = (void *)data;
   1149 
   1150 			return rpi_fb_do_cursor(cursor);
   1151 		}
   1152 #endif
   1153 	default:
   1154 		return EPASSTHROUGH;
   1155 	}
   1156 }
   1157 
   1158 #endif
   1159 
   1160 SYSCTL_SETUP(sysctl_machdep_rpi, "sysctl machdep subtree setup (rpi)")
   1161 {
   1162 	sysctl_createv(clog, 0, NULL, NULL,
   1163 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
   1164 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
   1165 
   1166 	sysctl_createv(clog, 0, NULL, NULL,
   1167 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   1168 	    CTLTYPE_INT, "firmware_revision", NULL, NULL, 0,
   1169 	    &vb.vbt_fwrev.rev, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
   1170 
   1171 	sysctl_createv(clog, 0, NULL, NULL,
   1172 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   1173 	    CTLTYPE_INT, "board_model", NULL, NULL, 0,
   1174 	    &vb.vbt_boardmodel.model, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
   1175 
   1176 	sysctl_createv(clog, 0, NULL, NULL,
   1177 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY,
   1178 	    CTLTYPE_INT, "board_revision", NULL, NULL, 0,
   1179 	    &vb.vbt_boardrev.rev, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
   1180 
   1181 	sysctl_createv(clog, 0, NULL, NULL,
   1182 	    CTLFLAG_PERMANENT|CTLFLAG_READONLY|CTLFLAG_HEX|CTLFLAG_PRIVATE,
   1183 	    CTLTYPE_QUAD, "serial", NULL, NULL, 0,
   1184 	    &vb.vbt_serial.sn, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL);
   1185 }
   1186 
   1187 #if defined(SOC_BCM2835)
   1188 static void
   1189 bcm2835_platform_bootstrap(void)
   1190 {
   1191 
   1192 	bcm2835_bs_tag = arm_generic_bs_tag;
   1193 	bcm2835_a4x_bs_tag = arm_generic_a4x_bs_tag;
   1194 
   1195 	bcm2835_bs_tag.bs_map = bcm2835_bs_map;
   1196 	bcm2835_bs_tag.bs_mmap = bcm2835_bs_mmap;
   1197 	bcm2835_a4x_bs_tag.bs_map = bcm2835_bs_map;
   1198 	bcm2835_a4x_bs_tag.bs_mmap = bcm2835_a4x_bs_mmap;
   1199 
   1200 	fdtbus_set_decoderegprop(false);
   1201 
   1202 	bcm2835_uartinit();
   1203 
   1204 	bcm2835_bootparams();
   1205 }
   1206 #endif
   1207 
   1208 #if defined(SOC_BCM2836)
   1209 static void
   1210 bcm2836_platform_bootstrap(void)
   1211 {
   1212 
   1213 	bcm2836_bs_tag = arm_generic_bs_tag;
   1214 	bcm2836_a4x_bs_tag = arm_generic_a4x_bs_tag;
   1215 
   1216 	bcm2836_bs_tag.bs_map = bcm2836_bs_map;
   1217 	bcm2836_bs_tag.bs_mmap = bcm2836_bs_mmap;
   1218 	bcm2836_a4x_bs_tag.bs_map = bcm2836_bs_map;
   1219 	bcm2836_a4x_bs_tag.bs_mmap = bcm2836_a4x_bs_mmap;
   1220 
   1221 	fdtbus_set_decoderegprop(false);
   1222 
   1223 	bcm2836_uartinit();
   1224 
   1225 	bcm2836_bootparams();
   1226 
   1227 	bcm2836_bootstrap();
   1228 }
   1229 #endif
   1230 
   1231 #if defined(SOC_BCM2835)
   1232 static void
   1233 bcm2835_platform_init_attach_args(struct fdt_attach_args *faa)
   1234 {
   1235 
   1236 	faa->faa_bst = &bcm2835_bs_tag;
   1237 	faa->faa_a4x_bst = &bcm2835_a4x_bs_tag;
   1238 	faa->faa_dmat = &bcm2835_bus_dma_tag;
   1239 
   1240 	bcm2835_bus_dma_tag._ranges = bcm2835_dma_ranges;
   1241 	bcm2835_bus_dma_tag._nranges = __arraycount(bcm2835_dma_ranges);
   1242 	bcm2835_dma_ranges[0].dr_len = bcm283x_memorysize;
   1243 }
   1244 #endif
   1245 
   1246 #if defined(SOC_BCM2836)
   1247 static void
   1248 bcm2836_platform_init_attach_args(struct fdt_attach_args *faa)
   1249 {
   1250 
   1251 	faa->faa_bst = &bcm2836_bs_tag;
   1252 	faa->faa_a4x_bst = &bcm2836_a4x_bs_tag;
   1253 	faa->faa_dmat = &bcm2835_bus_dma_tag;
   1254 
   1255 	bcm2835_bus_dma_tag._ranges = bcm2836_dma_ranges;
   1256 	bcm2835_bus_dma_tag._nranges = __arraycount(bcm2836_dma_ranges);
   1257 	bcm2836_dma_ranges[0].dr_len = bcm283x_memorysize;
   1258 }
   1259 #endif
   1260 
   1261 
   1262 void
   1263 bcm283x_platform_early_putchar(vaddr_t va, paddr_t pa, char c)
   1264 {
   1265 	volatile uint32_t *uartaddr =
   1266 	    cpu_earlydevice_va_p() ?
   1267 		(volatile uint32_t *)va :
   1268 		(volatile uint32_t *)pa;
   1269 
   1270 	while ((uartaddr[PL01XCOM_FR / 4] & PL01X_FR_TXFF) != 0)
   1271 		continue;
   1272 
   1273 	uartaddr[PL01XCOM_DR / 4] = c;
   1274 
   1275 	while ((uartaddr[PL01XCOM_FR / 4] & PL01X_FR_TXFE) == 0)
   1276 		continue;
   1277 }
   1278 
   1279 void
   1280 bcm2835_platform_early_putchar(char c)
   1281 {
   1282 	paddr_t pa = BCM2835_PERIPHERALS_BUS_TO_PHYS(BCM2835_UART0_BASE);
   1283 	vaddr_t va = BCM2835_IOPHYSTOVIRT(pa);
   1284 
   1285 	bcm283x_platform_early_putchar(va, pa, c);
   1286 }
   1287 
   1288 void
   1289 bcm2836_platform_early_putchar(char c)
   1290 {
   1291 	paddr_t pa = BCM2836_PERIPHERALS_BUS_TO_PHYS(BCM2835_UART0_BASE);
   1292 	vaddr_t va = BCM2835_IOPHYSTOVIRT(pa);
   1293 
   1294 	bcm283x_platform_early_putchar(va, pa, c);
   1295 }
   1296 
   1297 #define	BCM283x_REF_FREQ	19200000
   1298 
   1299 void
   1300 bcm2837_platform_early_putchar(char c)
   1301 {
   1302 #define AUCONSADDR_PA	BCM2836_PERIPHERALS_BUS_TO_PHYS(BCM2835_AUX_UART_BASE)
   1303 #define AUCONSADDR_VA	BCM2835_IOPHYSTOVIRT(AUCONSADDR_PA)
   1304 	volatile uint32_t *uartaddr =
   1305 	    cpu_earlydevice_va_p() ?
   1306 		(volatile uint32_t *)AUCONSADDR_VA :
   1307 		(volatile uint32_t *)AUCONSADDR_PA;
   1308 
   1309 	while ((uartaddr[com_lsr] & LSR_TXRDY) == 0)
   1310 		;
   1311 
   1312 	uartaddr[com_data] = c;
   1313 }
   1314 
   1315 static void
   1316 bcm283x_platform_device_register(device_t dev, void *aux)
   1317 {
   1318 	prop_dictionary_t dict = device_properties(dev);
   1319 
   1320 	if (device_is_a(dev, "bcmdmac") &&
   1321 	    vcprop_tag_success_p(&vb.vbt_dmachan.tag)) {
   1322 		prop_dictionary_set_uint32(dict,
   1323 		    "chanmask", vb.vbt_dmachan.mask);
   1324 	}
   1325 #if NSDHC > 0
   1326 	if (booted_device == NULL &&
   1327 	    device_is_a(dev, "ld") &&
   1328 	    device_is_a(device_parent(dev), "sdmmc")) {
   1329 		booted_partition = 0;
   1330 		booted_device = dev;
   1331 	}
   1332 #endif
   1333 	if ((device_is_a(dev, "usmsc") || device_is_a(dev, "mue")) &&
   1334 	    vcprop_tag_success_p(&vb.vbt_macaddr.tag)) {
   1335 		const uint8_t enaddr[ETHER_ADDR_LEN] = {
   1336 		     (vb.vbt_macaddr.addr >> 0) & 0xff,
   1337 		     (vb.vbt_macaddr.addr >> 8) & 0xff,
   1338 		     (vb.vbt_macaddr.addr >> 16) & 0xff,
   1339 		     (vb.vbt_macaddr.addr >> 24) & 0xff,
   1340 		     (vb.vbt_macaddr.addr >> 32) & 0xff,
   1341 		     (vb.vbt_macaddr.addr >> 40) & 0xff
   1342 		};
   1343 
   1344 		prop_data_t pd = prop_data_create_data(enaddr, ETHER_ADDR_LEN);
   1345 		KASSERT(pd != NULL);
   1346 		if (prop_dictionary_set(device_properties(dev), "mac-address",
   1347 		    pd) == false) {
   1348 			aprint_error_dev(dev,
   1349 			    "WARNING: Unable to set mac-address property\n");
   1350 		}
   1351 		prop_object_release(pd);
   1352 	}
   1353 
   1354 #if NGENFB > 0
   1355 	if (device_is_a(dev, "genfb")) {
   1356 		char *ptr;
   1357 
   1358 		bcmgenfb_set_console_dev(dev);
   1359 		bcmgenfb_set_ioctl(&rpi_ioctl);
   1360 #ifdef DDB
   1361 		db_trap_callback = bcmgenfb_ddb_trap_callback;
   1362 #endif
   1363 		if (rpi_fb_init(dict, aux) == false)
   1364 			return;
   1365 		if (get_bootconf_option(boot_args, "console",
   1366 		    BOOTOPT_TYPE_STRING, &ptr) && strncmp(ptr, "fb", 2) == 0) {
   1367 			prop_dictionary_set_bool(dict, "is_console", true);
   1368 #if NUKBD > 0
   1369 			/* allow ukbd to be the console keyboard */
   1370 			ukbd_cnattach();
   1371 #endif
   1372 		} else {
   1373 			prop_dictionary_set_bool(dict, "is_console", false);
   1374 		}
   1375 	}
   1376 #endif
   1377 }
   1378 
   1379 static u_int
   1380 bcm283x_platform_uart_freq(void)
   1381 {
   1382 
   1383 	return uart_clk;
   1384 }
   1385 
   1386 #if defined(SOC_BCM2835)
   1387 static const struct arm_platform bcm2835_platform = {
   1388 	.ap_devmap = bcm2835_platform_devmap,
   1389 	.ap_bootstrap = bcm2835_platform_bootstrap,
   1390 	.ap_init_attach_args = bcm2835_platform_init_attach_args,
   1391 	.ap_early_putchar = bcm2835_platform_early_putchar,
   1392 	.ap_device_register = bcm283x_platform_device_register,
   1393 	.ap_reset = bcm2835_system_reset,
   1394 	.ap_delay = bcm2835_tmr_delay,
   1395 	.ap_uart_freq = bcm283x_platform_uart_freq,
   1396 };
   1397 
   1398 ARM_PLATFORM(bcm2835, "brcm,bcm2835", &bcm2835_platform);
   1399 #endif
   1400 
   1401 #if defined(SOC_BCM2836)
   1402 static u_int
   1403 bcm2837_platform_uart_freq(void)
   1404 {
   1405 
   1406 	return core_clk * 2;
   1407 }
   1408 
   1409 static const struct arm_platform bcm2836_platform = {
   1410 	.ap_devmap = bcm2836_platform_devmap,
   1411 	.ap_bootstrap = bcm2836_platform_bootstrap,
   1412 	.ap_init_attach_args = bcm2836_platform_init_attach_args,
   1413 	.ap_early_putchar = bcm2836_platform_early_putchar,
   1414 	.ap_device_register = bcm283x_platform_device_register,
   1415 	.ap_reset = bcm2835_system_reset,
   1416 	.ap_delay = gtmr_delay,
   1417 	.ap_uart_freq = bcm283x_platform_uart_freq,
   1418 };
   1419 
   1420 static const struct arm_platform bcm2837_platform = {
   1421 	.ap_devmap = bcm2836_platform_devmap,
   1422 	.ap_bootstrap = bcm2836_platform_bootstrap,
   1423 	.ap_init_attach_args = bcm2836_platform_init_attach_args,
   1424 	.ap_early_putchar = bcm2837_platform_early_putchar,
   1425 	.ap_device_register = bcm283x_platform_device_register,
   1426 	.ap_reset = bcm2835_system_reset,
   1427 	.ap_delay = gtmr_delay,
   1428 	.ap_uart_freq = bcm2837_platform_uart_freq,
   1429 };
   1430 
   1431 ARM_PLATFORM(bcm2836, "brcm,bcm2836", &bcm2836_platform);
   1432 ARM_PLATFORM(bcm2837, "brcm,bcm2837", &bcm2837_platform);
   1433 #endif
   1434