|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Analog Devices, Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/device.h> |
| 8 | +#include <soc.h> |
| 9 | + |
| 10 | +#include <zephyr/drivers/clock_control/adi_max32_clock_control.h> |
| 11 | +#include <zephyr/drivers/pinctrl.h> |
| 12 | + |
| 13 | +#include <zephyr/logging/log.h> |
| 14 | + |
| 15 | +LOG_MODULE_REGISTER(memc_max32_hpb, CONFIG_MEMC_LOG_LEVEL); |
| 16 | + |
| 17 | +#include <hpb.h> |
| 18 | +#include <emcc.h> |
| 19 | + |
| 20 | +#define DT_DRV_COMPAT adi_max32_hpb |
| 21 | + |
| 22 | +struct memc_max32_hpb_config { |
| 23 | + const struct device *clock; |
| 24 | + const struct pinctrl_dev_config *pcfg; |
| 25 | +}; |
| 26 | + |
| 27 | +struct memc_max32_hpb_mem_config { |
| 28 | + uint8_t reg; |
| 29 | + |
| 30 | + mxc_hpb_mem_config_t config; |
| 31 | +}; |
| 32 | + |
| 33 | +/* clang-format off */ |
| 34 | + |
| 35 | +#define MEM_CONFIG(n) \ |
| 36 | + { \ |
| 37 | + .reg = DT_REG_ADDR(n), \ |
| 38 | + .config = {.device_type = DT_PROP(n, device_type), \ |
| 39 | + .base_addr = DT_PROP(n, base_address), \ |
| 40 | + .latency_cycle = DT_PROP_OR(n, latency_cycles, 1), \ |
| 41 | + .write_cs_high = DT_PROP_OR(n, write_cs_high, 0), \ |
| 42 | + .read_cs_high = DT_PROP_OR(n, read_cs_high, 0), \ |
| 43 | + .write_cs_hold = DT_PROP_OR(n, write_cs_hold, 0), \ |
| 44 | + .read_cs_hold = DT_PROP_OR(n, read_cs_hold, 0), \ |
| 45 | + .write_cs_setup = DT_PROP_OR(n, write_cs_setup, 0), \ |
| 46 | + .read_cs_setup = DT_PROP_OR(n, read_cs_setup, 0), \ |
| 47 | + .fixed_latency = DT_PROP_OR(n, fixed_read_latency, 0), \ |
| 48 | + COND_CODE_1(DT_NODE_HAS_PROP(n, config_regs), \ |
| 49 | + (.cfg_reg_val = config_regs_##n, \ |
| 50 | + .cfg_reg_val_len = ARRAY_SIZE(config_regs_##n)), ()) }, \ |
| 51 | + } |
| 52 | + |
| 53 | +/* clang-format on */ |
| 54 | + |
| 55 | +#define CR_ENTRY(idx, n) \ |
| 56 | + { \ |
| 57 | + .addr = DT_PROP_BY_IDX(n, config_regs, idx), \ |
| 58 | + .val = DT_PROP_BY_IDX(n, config_reg_vals, idx), \ |
| 59 | + } |
| 60 | + |
| 61 | +#define MEM_CR_ENTRIES(n) \ |
| 62 | + COND_CODE_1(DT_NODE_HAS_PROP(n, config_regs), ( \ |
| 63 | + BUILD_ASSERT(DT_PROP_LEN(n, config_regs) == DT_PROP_LEN(n, config_reg_vals), \ |
| 64 | + "The config-regs and config-reg-vals properties of adi,max32-hpb memory device" \ |
| 65 | + " child nodes must be the same length"); \ |
| 66 | + static const mxc_hpb_cfg_reg_val_t config_regs_##n[] = \ |
| 67 | + { LISTIFY(DT_PROP_LEN(n, config_regs), CR_ENTRY, (,), n) }; \ |
| 68 | + ), ()) |
| 69 | + |
| 70 | +/** memory device configuration(s). */ |
| 71 | +DT_INST_FOREACH_CHILD(0, MEM_CR_ENTRIES) |
| 72 | + |
| 73 | +/* clang-format off */ |
| 74 | + |
| 75 | +static const struct memc_max32_hpb_mem_config mem_configs[] = { |
| 76 | + DT_INST_FOREACH_CHILD_SEP(0, MEM_CONFIG, (,))}; |
| 77 | + |
| 78 | +#define CLOCK_CFG(node_id, prop, idx) \ |
| 79 | + {.bus = DT_CLOCKS_CELL_BY_IDX(node_id, idx, offset), \ |
| 80 | + .bit = DT_CLOCKS_CELL_BY_IDX(node_id, idx, bit)} |
| 81 | + |
| 82 | +static const struct max32_perclk perclks[] = { |
| 83 | + DT_INST_FOREACH_PROP_ELEM_SEP(0, clocks, CLOCK_CFG, (,))}; |
| 84 | + |
| 85 | +/* clang-format on */ |
| 86 | +static int memc_max32_hpb_init(const struct device *dev) |
| 87 | +{ |
| 88 | + const struct memc_max32_hpb_config *config = dev->config; |
| 89 | + |
| 90 | + int r; |
| 91 | + const mxc_hpb_mem_config_t *mem0 = NULL, *mem1 = NULL; |
| 92 | + |
| 93 | + if (!device_is_ready(config->clock)) { |
| 94 | + LOG_ERR("clock control device not ready"); |
| 95 | + return -ENODEV; |
| 96 | + } |
| 97 | + |
| 98 | + for (size_t i = 0; i < ARRAY_SIZE(perclks); i++) { |
| 99 | + r = clock_control_on(config->clock, (clock_control_subsys_t)&perclks[i]); |
| 100 | + if (r < 0) { |
| 101 | + LOG_ERR("Could not initialize HPB clock (%d)", r); |
| 102 | + return r; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + for (size_t i = 0; i < ARRAY_SIZE(mem_configs); i++) { |
| 107 | + if (mem_configs[i].reg == 0) { |
| 108 | + mem0 = &mem_configs[i].config; |
| 109 | + } else if (mem_configs[i].reg == 1) { |
| 110 | + mem1 = &mem_configs[i].config; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /* configure pinmux */ |
| 115 | + r = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); |
| 116 | + if (r < 0) { |
| 117 | + LOG_ERR("HPB pinctrl setup failed (%d)", r); |
| 118 | + return r; |
| 119 | + } |
| 120 | + |
| 121 | + r = MXC_HPB_Init(mem0, mem1); |
| 122 | + if (r < 0) { |
| 123 | + LOG_ERR("HPB init failed (%d)", r); |
| 124 | + return r; |
| 125 | + } |
| 126 | + |
| 127 | + COND_CODE_1(DT_INST_PROP(0, enable_emcc), (MXC_EMCC_Enable()), (MXC_EMCC_Disable())); |
| 128 | + |
| 129 | + return 0; |
| 130 | +} |
| 131 | + |
| 132 | +PINCTRL_DT_INST_DEFINE(0); |
| 133 | + |
| 134 | +static const struct memc_max32_hpb_config config = { |
| 135 | + .clock = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(0)), |
| 136 | + .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(0), |
| 137 | +}; |
| 138 | + |
| 139 | +DEVICE_DT_INST_DEFINE(0, memc_max32_hpb_init, NULL, NULL, &config, POST_KERNEL, |
| 140 | + CONFIG_MEMC_INIT_PRIORITY, NULL); |
0 commit comments