Skip to content

Commit

Permalink
Merge pull request tock#3710 from mazurek-michal/pinmux_gpio_02
Browse files Browse the repository at this point in the history
Refactoring of Earlgrey GPIO driver
  • Loading branch information
bradjc authored Nov 22, 2023
2 parents e02f7f9 + 7523a53 commit 9d2ffac
Show file tree
Hide file tree
Showing 9 changed files with 592 additions and 138 deletions.
7 changes: 5 additions & 2 deletions boards/opentitan/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ use kernel::hil::led;
#[panic_handler]
pub unsafe extern "C" fn panic_fmt(pi: &PanicInfo) -> ! {
let first_led_pin = &mut earlgrey::gpio::GpioPin::new(
earlgrey::gpio::GPIO0_BASE,
earlgrey::gpio::PADCTRL_BASE,
earlgrey::gpio::GPIO_BASE,
earlgrey::pinmux::PadConfig::Output(
earlgrey::registers::top_earlgrey::MuxedPads::Ioa6,
earlgrey::registers::top_earlgrey::PinmuxOutsel::GpioGpio7,
),
earlgrey::gpio::pins::pin7,
);
first_led_pin.make_output();
Expand Down
46 changes: 34 additions & 12 deletions boards/opentitan/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

use crate::hil::symmetric_encryption::AES128_BLOCK_SIZE;
use crate::otbn::OtbnComponent;
use crate::pinmux_layout::BoardPinmuxLayout;
use capsules_aes_gcm::aes_gcm;
use capsules_core::virtualizers::virtual_aes_ccm;
use capsules_core::virtualizers::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
use earlgrey::chip::EarlGreyDefaultPeripherals;
use earlgrey::chip_config::EarlGreyConfig;
use earlgrey::pinmux_config::EarlGreyPinmuxConfig;
use kernel::capabilities;
use kernel::component::Component;
use kernel::hil;
Expand All @@ -42,6 +44,7 @@ use rv32i::csr;

pub mod io;
mod otbn;
pub mod pinmux_layout;
#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -84,7 +87,8 @@ static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; 4] = [None

// Test access to the peripherals
#[cfg(test)]
static mut PERIPHERALS: Option<&'static EarlGreyDefaultPeripherals<ChipConfig>> = None;
static mut PERIPHERALS: Option<&'static EarlGreyDefaultPeripherals<ChipConfig, BoardPinmuxLayout>> =
None;
// Test access to board
#[cfg(test)]
static mut BOARD: Option<&'static kernel::Kernel> = None;
Expand Down Expand Up @@ -127,7 +131,11 @@ static mut RSA_HARDWARE: Option<&lowrisc::rsa::OtbnRsa<'static>> = None;
static mut SHA256SOFT: Option<&capsules_extra::sha256::Sha256Software<'static>> = None;

static mut CHIP: Option<
&'static earlgrey::chip::EarlGrey<EarlGreyDefaultPeripherals<ChipConfig>, ChipConfig>,
&'static earlgrey::chip::EarlGrey<
EarlGreyDefaultPeripherals<ChipConfig, BoardPinmuxLayout>,
ChipConfig,
BoardPinmuxLayout,
>,
> = None;
static mut PROCESS_PRINTER: Option<&'static kernel::process::ProcessPrinterText> = None;

Expand All @@ -144,10 +152,13 @@ pub static mut STACK_MEMORY: [u8; 0x1400] = [0; 0x1400];
struct EarlGrey {
led: &'static capsules_core::led::LedDriver<
'static,
LedHigh<'static, earlgrey::gpio::GpioPin<'static>>,
LedHigh<'static, earlgrey::gpio::GpioPin<'static, earlgrey::pinmux::PadConfig>>,
8,
>,
gpio: &'static capsules_core::gpio::GPIO<'static, earlgrey::gpio::GpioPin<'static>>,
gpio: &'static capsules_core::gpio::GPIO<
'static,
earlgrey::gpio::GpioPin<'static, earlgrey::pinmux::PadConfig>,
>,
console: &'static capsules_core::console::Console<'static>,
alarm: &'static capsules_core::alarm::AlarmDriver<
'static,
Expand Down Expand Up @@ -232,8 +243,9 @@ impl
KernelResources<
earlgrey::chip::EarlGrey<
'static,
EarlGreyDefaultPeripherals<'static, ChipConfig>,
EarlGreyDefaultPeripherals<'static, ChipConfig, BoardPinmuxLayout>,
ChipConfig,
BoardPinmuxLayout,
>,
> for EarlGrey
{
Expand Down Expand Up @@ -279,22 +291,26 @@ unsafe fn setup() -> (
&'static EarlGrey,
&'static earlgrey::chip::EarlGrey<
'static,
EarlGreyDefaultPeripherals<'static, ChipConfig>,
EarlGreyDefaultPeripherals<'static, ChipConfig, BoardPinmuxLayout>,
ChipConfig,
BoardPinmuxLayout,
>,
&'static EarlGreyDefaultPeripherals<'static, ChipConfig>,
&'static EarlGreyDefaultPeripherals<'static, ChipConfig, BoardPinmuxLayout>,
) {
// Ibex-specific handler
earlgrey::chip::configure_trap_handler();

// Configure board layout in pinmux
BoardPinmuxLayout::setup();

// initialize capabilities
let process_mgmt_cap = create_capability!(capabilities::ProcessManagementCapability);
let memory_allocation_cap = create_capability!(capabilities::MemoryAllocationCapability);

let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&PROCESSES));

let peripherals = static_init!(
EarlGreyDefaultPeripherals<ChipConfig>,
EarlGreyDefaultPeripherals<ChipConfig, BoardPinmuxLayout>,
EarlGreyDefaultPeripherals::new()
);
peripherals.init();
Expand All @@ -314,7 +330,7 @@ unsafe fn setup() -> (
// LEDs
// Start with half on and half off
let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
LedHigh<'static, earlgrey::gpio::GpioPin>,
LedHigh<'static, earlgrey::gpio::GpioPin<earlgrey::pinmux::PadConfig>>,
LedHigh::new(&peripherals.gpio_port[8]),
LedHigh::new(&peripherals.gpio_port[9]),
LedHigh::new(&peripherals.gpio_port[10]),
Expand All @@ -329,7 +345,7 @@ unsafe fn setup() -> (
board_kernel,
capsules_core::gpio::DRIVER_NUM,
components::gpio_component_helper!(
earlgrey::gpio::GpioPin,
earlgrey::gpio::GpioPin<earlgrey::pinmux::PadConfig>,
0 => &peripherals.gpio_port[0],
1 => &peripherals.gpio_port[1],
2 => &peripherals.gpio_port[2],
Expand All @@ -340,7 +356,9 @@ unsafe fn setup() -> (
7 => &peripherals.gpio_port[15]
),
)
.finalize(components::gpio_component_static!(earlgrey::gpio::GpioPin));
.finalize(components::gpio_component_static!(
earlgrey::gpio::GpioPin<earlgrey::pinmux::PadConfig>
));

let hardware_alarm = static_init!(
earlgrey::timer::RvTimer<ChipConfig>,
Expand Down Expand Up @@ -391,7 +409,11 @@ unsafe fn setup() -> (
);

let chip = static_init!(
earlgrey::chip::EarlGrey<EarlGreyDefaultPeripherals<ChipConfig>, ChipConfig>,
earlgrey::chip::EarlGrey<
EarlGreyDefaultPeripherals<ChipConfig, BoardPinmuxLayout>,
ChipConfig,
BoardPinmuxLayout,
>,
earlgrey::chip::EarlGrey::new(peripherals, hardware_alarm)
);
CHIP = Some(chip);
Expand Down
140 changes: 140 additions & 0 deletions boards/opentitan/src/pinmux_layout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2023.

use earlgrey::pinmux_config::{EarlGreyPinmuxConfig, INPUT_NUM, OUTPUT_NUM};
use earlgrey::registers::top_earlgrey::{PinmuxInsel, PinmuxOutsel};

type In = PinmuxInsel;
type Out = PinmuxOutsel;

pub enum BoardPinmuxLayout {}

/// Implementations of Pinmux initial board configurations.
/// Defined Pinmux layout is designed for CW310 FPGA board
/// and is compatible with Hyperdebug test board IO layout.
/// In feature we should add layouts compatible with other
/// OpenTitan boards.
/// Source of true:
/// <OPENTITAN_TREE/hw/top_earlgrey/data/pins_cw310_hyperdebug.xdc>
impl EarlGreyPinmuxConfig for BoardPinmuxLayout {
/// Array of input selector initial configurations
#[rustfmt::skip]
const INPUT: &'static [PinmuxInsel; INPUT_NUM] = &[
In::Ioa2, // GpioGpio0
In::Ioa3, // GpioGpio1
In::Ioa6, // GpioGpio2
In::Iob0, // GpioGpio3
In::Iob1, // GpioGpio4
In::Iob2, // GpioGpio5
In::Iob3, // GpioGpio6
In::Iob6, // GpioGpio7
In::Iob7, // GpioGpio8
In::Iob8, // GpioGpio9
In::Ioc0, // GpioGpio10
In::Ioc1, // GpioGpio11
In::Ioc2, // GpioGpio12
In::Ioc5, // GpioGpio13
In::Ioc6, // GpioGpio14
In::Ioc7, // GpioGpio15
In::Ioc8, // GpioGpio16
In::Ioc9, // GpioGpio17
In::Ioc10, // GpioGpio18
In::Ioc11, // GpioGpio19
In::Ioc12, // GpioGpio20
In::Ior0, // GpioGpio21
In::Ior1, // GpioGpio22
In::Ior2, // GpioGpio23
In::Ior3, // GpioGpio24
In::Ior4, // GpioGpio25
In::Ior5, // GpioGpio26
In::Ior6, // GpioGpio27
In::Ior7, // GpioGpio28
In::Ior10, // GpioGpio29
In::Ior11, // GpioGpio30
In::Ior12, // GpioGpio31
In::Ioa7, // I2c0Sda
In::Ioa8, // I2c0Scl
In::Iob10, // I2c1Sda
In::Iob9, // I2c1Scl
In::Iob11, // I2c2Sda
In::Iob12, // I2c2Scl
In::ConstantZero, // SpiHost1Sd0
In::ConstantZero, // SpiHost1Sd1
In::ConstantZero, // SpiHost1Sd2
In::ConstantZero, // SpiHost1Sd3
In::Ioa0, // Uart0Rx
In::Ioa4, // Uart1Rx
In::Iob4, // Uart2Rx
In::Ioc3, // Uart3Rx
In::ConstantZero, // SpiDeviceTpmCsb
In::ConstantZero, // FlashCtrlTck
In::ConstantZero, // FlashCtrlTms
In::ConstantZero, // FlashCtrlTdi
In::ConstantZero, // SysrstCtrlAonAcPresent
In::ConstantZero, // SysrstCtrlAonKey0In
In::ConstantZero, // SysrstCtrlAonKey1In
In::ConstantZero, // SysrstCtrlAonKey2In
In::ConstantZero, // SysrstCtrlAonPwrbIn
In::ConstantZero, // SysrstCtrlAonLidOpen
In::ConstantZero, // UsbdevSense
];

/// Array representing configgurations of pinmux output selector
#[rustfmt::skip]
const OUTPUT: &'static [PinmuxOutsel; OUTPUT_NUM] = &[
// __________ BANK IOA __________
Out::ConstantHighZ, // Ioa0 (CW310Hyp Uart_RX / CW310 SAM3X)
Out::Uart0Tx, // Ioa1 (CW310Hyp Uart_Tx / CW310 SAM3x)
Out::GpioGpio0, // Ioa2
Out::GpioGpio1, // Ioa3
Out::ConstantHighZ, // Ioa4
Out::Uart1Tx, // Ioa5
Out::GpioGpio2, // Ioa6
Out::I2c0Sda, // Ioa7 I2C0_TPM_SDA
Out::I2c0Scl, // Ioa8 I2C0_TPM_SCL
// __________ BANK IOB __________
Out::GpioGpio3, // Iob0 SPI_HOST_CS
Out::GpioGpio4, // Iob1 SPI_HOST_DI
Out::GpioGpio5, // Iob2 SPI_HOST_DO
Out::GpioGpio6, // Iob3 SPI_HOST_CLK
Out::ConstantHighZ, // Iob4 UART2_RX
Out::Uart2Tx, // Iob5 UART2_TX
Out::GpioGpio7, // Iob6
Out::GpioGpio8, // Iob7
Out::GpioGpio9, // Iob8
Out::I2c1Scl, // Iob9 I2C1_SCL
Out::I2c1Sda, // Iob10 I2C1_SDA
Out::I2c2Sda, // Iob11 I2C2_SDA
Out::I2c2Scl, // Iob12 I2C2_SCL
// __________ BANK IOC __________
Out::GpioGpio10, // Ioc0
Out::GpioGpio11, // Ioc1
Out::GpioGpio12, // Ioc2
Out::ConstantHighZ, // Ioc3 UART3_RX
Out::Uart3Tx, // Ioc4 UART3_TX
Out::GpioGpio13, // Ioc5
Out::GpioGpio14, // Ioc6
Out::GpioGpio15, // Ioc7
Out::GpioGpio16, // Ioc8
Out::GpioGpio17, // Ioc9
Out::GpioGpio18, // Ioc10
Out::GpioGpio19, // Ioc11
Out::GpioGpio20, // Ioc12
// __________ BANK IOR __________
Out::GpioGpio21, // Ior0
Out::GpioGpio22, // Ior1
Out::GpioGpio23, // Ior2
Out::GpioGpio24, // Ior3
Out::GpioGpio25, // Ior4
Out::GpioGpio26, // Ior5
Out::GpioGpio27, // Ior6
Out::GpioGpio28, // Ior7
// DIO CW310_hyp Ior8
// DIO CW310_hyp Ior9
Out::GpioGpio29, // Ior10
Out::GpioGpio30, // Ior11
Out::GpioGpio31, // Ior12
Out::ConstantHighZ, // Ior13
];
}
Loading

0 comments on commit 9d2ffac

Please sign in to comment.