|
| 1 | +/* Copyright (c) 2025 Silicon Laboratories Inc. |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + */ |
| 4 | +#define DT_DRV_COMPAT silabs_siwx91x_qspi_memory |
| 5 | +#include <zephyr/logging/log.h> |
| 6 | +#include <zephyr/drivers/clock_control.h> |
| 7 | +#include <zephyr/drivers/pinctrl.h> |
| 8 | + |
| 9 | +#include <zephyr/sys/util.h> |
| 10 | +#include <zephyr/device.h> |
| 11 | +#include <zephyr/kernel.h> |
| 12 | + |
| 13 | +#include "rsi_qspi_proto.h" |
| 14 | +#include "sl_si91x_psram_handle.h" |
| 15 | + |
| 16 | +LOG_MODULE_REGISTER(siwx91x_memc, CONFIG_MEMC_LOG_LEVEL); |
| 17 | + |
| 18 | +struct siwx91x_memc_config { |
| 19 | + qspi_reg_t *reg; |
| 20 | + const struct device *clock_dev; |
| 21 | + clock_control_subsys_t clock_subsys; |
| 22 | + const struct pinctrl_dev_config *pincfg; |
| 23 | +}; |
| 24 | + |
| 25 | +static int siwx91x_memc_init(const struct device *dev) |
| 26 | +{ |
| 27 | + const struct siwx91x_memc_config *config = dev->config; |
| 28 | + int ret; |
| 29 | + |
| 30 | + /* Memory controller is automatically setup by the siwx91x bootloader, |
| 31 | + * so we have to uninitialize it before to change the configuration |
| 32 | + */ |
| 33 | + ret = sl_si91x_psram_uninit(); |
| 34 | + if (ret) { |
| 35 | + return -EIO; |
| 36 | + } |
| 37 | + |
| 38 | + ret = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT); |
| 39 | + if (ret) { |
| 40 | + return -EIO; |
| 41 | + } |
| 42 | + if (config->clock_dev) { |
| 43 | + ret = device_is_ready(config->clock_dev); |
| 44 | + if (!ret) { |
| 45 | + return -EINVAL; |
| 46 | + } |
| 47 | + ret = clock_control_on(config->clock_dev, config->clock_subsys); |
| 48 | + if (ret && ret != -EALREADY && ret != -ENOSYS) { |
| 49 | + return ret; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + ret = sl_si91x_psram_init(); |
| 54 | + if (ret) { |
| 55 | + LOG_ERR("sl_si91x_psram_init() returned %d", ret); |
| 56 | + return -EIO; |
| 57 | + } |
| 58 | + |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +PINCTRL_DT_INST_DEFINE(0); |
| 63 | +static const struct siwx91x_memc_config siwx91x_memc_config = { |
| 64 | + .reg = (void *)DT_INST_REG_ADDR(0), |
| 65 | + .clock_dev = DEVICE_DT_GET_OR_NULL(DT_INST_CLOCKS_CTLR(0)), |
| 66 | + .clock_subsys = (void *)DT_INST_PHA_OR(0, clocks, clkid, NULL), |
| 67 | + .pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(0), |
| 68 | +}; |
| 69 | +/* Required to properly initialize ,deviceID */ |
| 70 | +static const uint8_t devid[] = DT_INST_PROP(0, device_id); |
| 71 | + |
| 72 | +/* PSRAM_Device is directly referenced by sl_si91x_psram_init() */ |
| 73 | +struct sl_psram_info_type_t PSRAM_Device = { |
| 74 | + .deviceID.MFID = devid[0], |
| 75 | + .deviceID.KGD = devid[1], |
| 76 | + .deviceID.EID = { devid[2], devid[3], devid[4], devid[5], devid[6], devid[7] }, |
| 77 | + /* FIXME: Currently, the Chip Select (.cs_no property) and the RAM start |
| 78 | + * address are hard coded. The hardware also support Chip Select == 1, |
| 79 | + * then RAM start address will be 0xb000000. |
| 80 | + */ |
| 81 | + .devDensity = DT_REG_SIZE(DT_INST_CHILD(0, psram_a000000)), |
| 82 | + .normalReadMAXFrequency = DT_INST_PROP(0, normal_freq), |
| 83 | + .fastReadMAXFrequency = DT_INST_PROP(0, fast_freq), |
| 84 | + .rwType = QUAD_RW, |
| 85 | + .defaultBurstWrapSize = 1024, |
| 86 | + .toggleBurstWrapSize = 0, |
| 87 | + .spi_config.spi_config_2.auto_mode = 1, |
| 88 | + /* FIXME: user may want to customize these values */ |
| 89 | + .spi_config.spi_config_1.read_cmd = 0xEB, |
| 90 | + .spi_config.spi_config_3.wr_cmd = 0x38, |
| 91 | + .spi_config.spi_config_1.extra_byte_mode = QUAD_MODE, |
| 92 | + .spi_config.spi_config_1.dummy_mode = QUAD_MODE, |
| 93 | + .spi_config.spi_config_1.addr_mode = QUAD_MODE, |
| 94 | + .spi_config.spi_config_1.data_mode = QUAD_MODE, |
| 95 | + .spi_config.spi_config_1.inst_mode = QUAD_MODE, |
| 96 | + .spi_config.spi_config_3.wr_addr_mode = QUAD_MODE, |
| 97 | + .spi_config.spi_config_3.wr_data_mode = QUAD_MODE, |
| 98 | + .spi_config.spi_config_3.wr_inst_mode = QUAD_MODE, |
| 99 | + .spi_config.spi_config_2.wrap_len_in_bytes = NO_WRAP, |
| 100 | + .spi_config.spi_config_2.swap_en = 1, |
| 101 | + .spi_config.spi_config_2.addr_width = 3, /* 24 bits */ |
| 102 | + .spi_config.spi_config_2.cs_no = 0, |
| 103 | + .spi_config.spi_config_4.secondary_csn = 1, |
| 104 | + .spi_config.spi_config_2.neg_edge_sampling = 1, |
| 105 | + .spi_config.spi_config_1.no_of_dummy_bytes = 3, |
| 106 | + .spi_config.spi_config_1.dummy_W_or_R = DUMMY_READS, |
| 107 | + .spi_config.spi_config_2.full_duplex = IGNORE_FULL_DUPLEX, |
| 108 | + .spi_config.spi_config_2.qspi_clk_en = QSPI_FULL_TIME_CLK, |
| 109 | + .spi_config.spi_config_1.flash_type = 0xf, |
| 110 | + .spi_config.spi_config_3.dummys_4_jump = 1, |
| 111 | + .spi_config.spi_config_4.valid_prot_bits = 4, |
| 112 | + .spi_config.spi_config_1.d3d2_data = 0x03, |
| 113 | + .spi_config.spi_config_5.d7_d4_data = 0x0f, |
| 114 | +}; |
| 115 | +/* PSRAMSecureSegments is directly referenced by sl_si91x_psram_init() */ |
| 116 | +struct PSRAMSecureSegmentType PSRAMSecureSegments[MAX_SEC_SEGMENTS] = { |
| 117 | + [0].segmentEnable = 1, |
| 118 | + [0].lowerBoundary = 0x00000, |
| 119 | + [0].higherBoundary = 0x0ffff, |
| 120 | +}; |
| 121 | +DEVICE_DT_INST_DEFINE(0, siwx91x_memc_init, NULL, NULL, &siwx91x_memc_config, |
| 122 | + PRE_KERNEL_1, CONFIG_MEMC_INIT_PRIORITY, NULL); |
0 commit comments