blob: a86bbbdf56e7b3f81eb970f438f2f8aa945e9022 [file] [log] [blame]
/*
* Copyright 2023 Google LLC
* Copyright lowRISC contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hw/top_matcha/sw/autogen/top_matcha.h"
#include "sw/device/lib/arch/device.h"
#include "sw/device/lib/dif/dif_cam_ctrl.h"
#include "sw/device/lib/dif/dif_rv_plic.h"
#include "sw/device/lib/dif/dif_uart.h"
#include "sw/device/lib/runtime/irq.h"
#include "sw/device/lib/runtime/log.h"
#include "sw/device/lib/runtime/print.h"
#include "sw/device/lib/testing/test_framework/check.h"
#include "sw/device/lib/testing/test_framework/ottf_test_config.h"
#include "sw/device/lib/testing/test_framework/test_util.h"
OTTF_DEFINE_TEST_CONFIG();
static dif_uart_t uart;
static dif_cam_ctrl_t cam_ctrl;
static dif_rv_plic_t plic_smc;
// These flags are used in the test routine to verify that a corresponding
// interrupt has elapsed, and has been serviced. These are declared as volatile
// since they are referenced in the ISR routine as well as in the main program
// flow.
static volatile bool cam_ctrl_motion_detect_handled;
/**
* CAM_CTRL ISR.
*
* Services CAM_CTRL interrupts, sets the appropriate flags that are used to
* determine success or failure of the test.
*/
static void handle_cam_ctrl_isr(const dif_rv_plic_irq_id_t interrupt_id) {
// NOTE: This initialization is superfluous, since the `default` case below
// is effectively noreturn, but the compiler is unable to prove this.
dif_cam_ctrl_irq_t cam_ctrl_irq = 0;
switch (interrupt_id) {
case kTopMatchaPlicIrqIdCamCtrlCamMotionDetect:
CHECK(!cam_ctrl_motion_detect_handled,
"CAM_CTRL motion detect IRQ asserted more than once");
cam_ctrl_irq = kDifCamCtrlIrqCamMotionDetect;
cam_ctrl_motion_detect_handled = true;
break;
default:
LOG_FATAL("ISR is not implemented!");
}
CHECK_DIF_OK(dif_cam_ctrl_irq_acknowledge(&cam_ctrl, cam_ctrl_irq));
}
/**
* External ISR.
*
* Handles all peripheral interrupts on Ibex. PLIC asserts an external interrupt
* line to the CPU, which results in a call to this OTTF ISR. This ISR
* overrides the default OTTF implementation.
*/
void ottf_external_isr(void) {
//// Claim the IRQ by reading the Ibex specific CC register.
dif_rv_plic_irq_id_t interrupt_id;
CHECK_DIF_OK(dif_rv_plic_irq_claim(&plic_smc, kTopMatchaPlicTargetIbex0Smc,
&interrupt_id));
// Check if the interrupted peripheral is UART.
top_matcha_plic_peripheral_smc_t peripheral_id =
top_matcha_plic_interrupt_for_peripheral_smc[interrupt_id];
CHECK(peripheral_id == kTopMatchaPlicPeripheralCamCtrl ||
peripheral_id == kTopMatchaPlicPeripheralIspWrapper,
"ISR interrupted peripheral is not CAM_CTRL or ISP_WRAPPER!");
switch (peripheral_id) {
case kTopMatchaPlicPeripheralCamCtrl:
handle_cam_ctrl_isr(interrupt_id);
break;
default:
LOG_FATAL("Peripheral is not implemented!");
}
// Complete the IRQ by writing the IRQ source to the Ibex specific CC
// register.
CHECK_DIF_OK(dif_rv_plic_irq_complete(&plic_smc, kTopMatchaPlicTargetIbex0Smc,
interrupt_id));
}
/**
* Configures all the relevant interrupts in CAM_CTRL.
*/
static void cam_ctrl_configure_irqs(dif_cam_ctrl_t *cam_ctrl) {
CHECK_DIF_OK(dif_cam_ctrl_irq_set_enabled(
cam_ctrl, kDifCamCtrlIrqCamMotionDetect, kDifToggleEnabled));
}
/**
* Configures all the relevant interrupts in PLIC_SMC.
*/
static void plic_smc_configure_irqs(dif_rv_plic_t *plic) {
// Set IRQ priorities to MAX
CHECK_DIF_OK(dif_rv_plic_irq_set_priority(
plic, kTopMatchaPlicIrqIdCamCtrlCamMotionDetect, kDifRvPlicMaxPriority));
CHECK_DIF_OK(dif_rv_plic_irq_set_priority(
plic, kTopMatchaPlicIrqIdIspWrapperIsp, kDifRvPlicMaxPriority));
CHECK_DIF_OK(dif_rv_plic_irq_set_priority(
plic, kTopMatchaPlicIrqIdIspWrapperMi, kDifRvPlicMaxPriority));
// Set Ibex IRQ priority threshold level
CHECK_DIF_OK(dif_rv_plic_target_set_threshold(
plic, kTopMatchaPlicTargetIbex0Smc, kDifRvPlicMinPriority));
// Enable IRQs in PLIC
CHECK_DIF_OK(dif_rv_plic_irq_set_enabled(
plic, kTopMatchaPlicIrqIdCamCtrlCamMotionDetect,
kTopMatchaPlicTargetIbex0Smc, kDifToggleEnabled));
CHECK_DIF_OK(dif_rv_plic_irq_set_enabled(
plic, kTopMatchaPlicIrqIdIspWrapperIsp, kTopMatchaPlicTargetIbex0Smc,
kDifToggleEnabled));
CHECK_DIF_OK(dif_rv_plic_irq_set_enabled(
plic, kTopMatchaPlicIrqIdIspWrapperMi, kTopMatchaPlicTargetIbex0Smc,
kDifToggleEnabled));
}
static void execute_test() {
// Force CAM_CTRL motion detect interrupt.
cam_ctrl_motion_detect_handled = false;
CHECK_DIF_OK(
dif_cam_ctrl_irq_force(&cam_ctrl, kDifCamCtrlIrqCamMotionDetect));
// Check if the IRQ has occured and has been handled appropriately.
if (!cam_ctrl_motion_detect_handled) {
busy_spin_micros(10);
}
CHECK(cam_ctrl_motion_detect_handled,
"CAM_CTRL motion detect IRQ has not been handled!");
}
void _ottf_main(void) {
// Initialize the SMC UART to enable logging for non-DV simulation platforms.
if (kDeviceType != kDeviceSimDV) {
init_uart(TOP_MATCHA_SMC_UART_BASE_ADDR, &uart);
}
test_status_set(kTestStatusInTest);
irq_global_ctrl(true);
irq_external_ctrl(true);
// Initialize each dif device.
CHECK_DIF_OK(dif_rv_plic_init(
mmio_region_from_addr(TOP_MATCHA_RV_PLIC_SMC_BASE_ADDR), &plic_smc));
CHECK_DIF_OK(dif_cam_ctrl_init(
mmio_region_from_addr(TOP_MATCHA_CAM_CTRL_BASE_ADDR), &cam_ctrl));
// Configure irqs for cam, isp and plic.
cam_ctrl_configure_irqs(&cam_ctrl);
plic_smc_configure_irqs(&plic_smc);
execute_test();
test_status_set(kTestStatusPassed);
}