|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 STMicroelectronics |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT st_stm32_npu |
| 8 | + |
| 9 | +#include <errno.h> |
| 10 | + |
| 11 | +#include <zephyr/device.h> |
| 12 | +#include <zephyr/drivers/reset.h> |
| 13 | +#include <zephyr/init.h> |
| 14 | +#include <soc.h> |
| 15 | + |
| 16 | +#include <zephyr/drivers/clock_control/stm32_clock_control.h> |
| 17 | + |
| 18 | +/* Read-only driver configuration */ |
| 19 | +struct npu_stm32_cfg { |
| 20 | + /* Clock configuration. */ |
| 21 | + struct stm32_pclken pclken; |
| 22 | + /* Reset configuration */ |
| 23 | + const struct reset_dt_spec reset; |
| 24 | +}; |
| 25 | + |
| 26 | +static void npu_risaf_config(void) |
| 27 | +{ |
| 28 | + RIMC_MasterConfig_t RIMC_master = {0}; |
| 29 | + |
| 30 | + RIMC_master.MasterCID = RIF_CID_1; |
| 31 | + RIMC_master.SecPriv = RIF_ATTRIBUTE_SEC | RIF_ATTRIBUTE_PRIV; |
| 32 | + HAL_RIF_RIMC_ConfigMasterAttributes(RIF_MASTER_INDEX_NPU, &RIMC_master); |
| 33 | + HAL_RIF_RISC_SetSlaveSecureAttributes(RIF_RISC_PERIPH_INDEX_NPU, |
| 34 | + RIF_ATTRIBUTE_SEC | RIF_ATTRIBUTE_PRIV); |
| 35 | +} |
| 36 | + |
| 37 | +static int npu_stm32_init(const struct device *dev) |
| 38 | +{ |
| 39 | + const struct device *const clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE); |
| 40 | + const struct npu_stm32_cfg *cfg = dev->config; |
| 41 | + |
| 42 | + if (!device_is_ready(clk)) { |
| 43 | + return -ENODEV; |
| 44 | + } |
| 45 | + |
| 46 | + if (clock_control_on(clk, (clock_control_subsys_t) &cfg->pclken) != 0) { |
| 47 | + return -EIO; |
| 48 | + } |
| 49 | + |
| 50 | + if (!device_is_ready(cfg->reset.dev)) { |
| 51 | + return -ENODEV; |
| 52 | + } |
| 53 | + |
| 54 | + /* Reset timer to default state using RCC */ |
| 55 | + (void)reset_line_toggle_dt(&cfg->reset); |
| 56 | + |
| 57 | + npu_risaf_config(); |
| 58 | + |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +static const struct npu_stm32_cfg npu_stm32_cfg = { |
| 64 | + .pclken = { |
| 65 | + .enr = DT_CLOCKS_CELL(DT_NODELABEL(npu), bits), |
| 66 | + .bus = DT_CLOCKS_CELL(DT_NODELABEL(npu), bus), |
| 67 | + }, |
| 68 | + .reset = RESET_DT_SPEC_GET(DT_NODELABEL(npu)), |
| 69 | +}; |
| 70 | + |
| 71 | +DEVICE_DT_DEFINE(DT_NODELABEL(npu), npu_stm32_init, NULL, |
| 72 | + NULL, &npu_stm32_cfg, POST_KERNEL, |
| 73 | + CONFIG_APPLICATION_INIT_PRIORITY, NULL); |
0 commit comments