git clone https://garage44.eu/jeroen/pinnacle.git.. /kernel/src/board/rk3399.rs
//! Boards with the Rockchip RK3399 (or RK3399S) and U-Boot: RAM from 0, the
//! SoC's debug UART. Not yet run on hardware.
use crate::mmu::{Kind, Region};
pub const NAME: &str = "rk3399";
/// RAM up to 0xf800_0000, the SoC's registers above it.
pub const MEMORY: &[Region] = &[
Region {
start: 0,
end: 0xf800_0000,
kind: Kind::Normal,
},
Region {
start: 0xf800_0000,
end: 0x1_0000_0000,
kind: Kind::Device,
},
];
/// UART2, the SoC's debug port: a Synopsys DesignWare 8250
/// with 32-bit registers, which U-Boot leaves set to 1 500 000 baud.
pub mod uart {
const BASE: usize = crate::mmu::virt(0xff1a_0000);
const THR: *mut u32 = BASE as *mut u32;
const LSR: *const u32 = (BASE + (5 << 2)) as *const u32;
const LSR_THRE: u32 = 1 << 5;
pub fn put(byte: u8) {
// SAFETY: UART2's registers, mapped as Device memory.
unsafe {
while LSR.read_volatile() & LSR_THRE == 0 {}
THR.write_volatile(u32::from(byte));
}
}
}
/// PSCI SYSTEM_OFF through Trusted Firmware-A, which U-Boot starts us under.
/// The exit code has nowhere to go on a board.
pub fn power_off(_code: u32) -> ! {
const PSCI_SYSTEM_OFF: u64 = 0x8400_0008;
// SAFETY: SYSTEM_OFF does not return.
unsafe { core::arch::asm!("smc #0", in("x0") PSCI_SYSTEM_OFF, options(nostack)) };
loop {
core::hint::spin_loop();
}
}
/// Restarts the board: PSCI SYSTEM_RESET through Trusted Firmware-A.
pub fn restart() -> ! {
crate::smp::system_reset()
}
/// The RK3399's GIC-500: the distributor, and the first core's
/// redistributor (each next core's is 128 KiB further).
pub mod gic {
pub const DISTRIBUTOR: usize = crate::mmu::virt(0xfee0_0000);
pub const REDISTRIBUTOR: usize = crate::mmu::virt(0xfef0_0000);
}
/// RAM the kernel must not hand out besides its own image: Trusted
/// Firmware-A's BL31 at the bottom of RAM, which stays resident.
pub const RESERVED: &[(usize, usize)] = &[(0, 0x20_0000)];
/// Trusted Firmware-A answers PSCI by SMC.
pub const PSCI_SMC: bool = true;
/// What the kernel drives here without a driver of its own: the console.
pub const OWN: &[(&str, &str, crate::device::Class)] =
&[("snps,dw-apb-uart", "console", crate::device::Class::Serial)];
/// The board has no fw_cfg; its display comes with a VOP driver.
pub const FW_CFG: Option<usize> = None;
/// The board's clock is in its PMIC, behind I²C: not yet read.
pub fn clock() -> u64 {
0
}
/// No real-time clock driven yet: the time set stays in the kernel.
pub fn set_clock(_seconds: u64) {}
/// The two USB 2.0 hosts (EHCI and OHCI each), as Linux brings them up
/// whatever the loader left: their bus clocks ungated, and each one's PHY
/// port powered. A Pinebook Pro's keyboard and touchpad are on the first.
/// From Linux's `clk-rk3399.c` (a set gate bit is a gated clock; the CRU's
/// registers take a mask of the bits written in their upper half):
/// CLKGATE_CON6 bits 5 and 6 the PHYs' 24 MHz references, CLKGATE_CON20
/// bits 5 to 8 hclk_host0, its arbiter, hclk_host1 and its arbiter. From
/// its `phy-rockchip-inno-usb2.c`, in the GRF: each PHY's 480 MHz output
/// on (bit 4 of 0xe450 and 0xe460 written 0), then its host port out of
/// suspend (bits 1:0 of 0xe458 and 0xe468 written 2), each followed by
/// the wait Linux gives it. What was there before is said on the console,
/// for a first try on a board.
pub fn usb_hosts_on() {
const CRU: usize = 0xff76_0000;
const GRF: usize = 0xff77_0000;
let at = |base: usize, offset: usize| crate::mmu::virt(base + offset) as *mut u32;
// SAFETY: the CRU's and the GRF's registers, always clocked, mapped as
// Device memory.
let read = |p: *mut u32| unsafe { p.read_volatile() };
// SAFETY: as above.
let write = |p: *mut u32, value: u32| unsafe { p.write_volatile(value) };
let gates = (read(at(CRU, 0x300 + 6 * 4)), read(at(CRU, 0x300 + 20 * 4)));
let phys = (read(at(GRF, 0xe458)), read(at(GRF, 0xe468)));
crate::println!(
"usb: rk3399 hosts: gates {:#x} {:#x}, PHY ports {:#x} {:#x}; turned on",
gates.0 & 0b11 << 5,
gates.1 & 0b1111 << 5,
phys.0 & 3,
phys.1 & 3
);
write(at(CRU, 0x300 + 6 * 4), 0b11 << (5 + 16));
write(at(CRU, 0x300 + 20 * 4), 0b1111 << (5 + 16));
for phy in [0xe450, 0xe460] {
write(at(GRF, phy), 1 << (4 + 16));
}
crate::drivers::usb::spin_ms(2);
for port in [0xe458, 0xe468] {
write(at(GRF, port), 0b11 << 16 | 2);
}
crate::drivers::usb::spin_ms(2);
}
/// The screen U-Boot left lit, if it did: the first window of either
/// display controller (VOP-big, VOP-little), read from its registers.
/// A controller whose bus clocks are gated is not read: a read of a
/// peripheral without its clock can hang the bus. The gates, as Linux's
/// `clk-rk3399.c` has them (a set bit is a gated clock): the CRU's
/// CLKGATE_CON10 for the clocks they come from (aclk and hclk of VOP0 at
/// bits 8 and 9, of VOP1 at 10 and 11), CLKGATE_CON28 for their own (VOP0
/// at 2 and 3, VOP1 at 6 and 7).
pub fn lit_screen() -> Option<crate::drivers::scanout::Scanout> {
const CRU: usize = 0xff76_0000;
let gate = |n: usize| {
// SAFETY: the CRU's registers, always clocked, mapped as Device memory.
unsafe { (crate::mmu::virt(CRU + 0x300 + n * 4) as *const u32).read_volatile() }
};
let (con10, con28) = (gate(10), gate(28));
let vops = [
(0xff90_0000usize, 0b11 << 8, 0b11 << 2),
(0xff8f_0000, 0b11 << 10, 0b11 << 6),
];
vops.iter().find_map(|&(base, parents, own)| {
if con10 & parents != 0 || con28 & own != 0 {
return None;
}
let read = |offset: usize| {
// SAFETY: the VOP's registers, its clocks on, mapped as Device memory.
unsafe { (crate::mmu::virt(base + offset) as *const u32).read_volatile() }
};
crate::drivers::scanout::vop_window(read(0x08), read(0x30), read(0x3c), read(0x40), read(0x48))
})
}