2020-11-02 03:48:15 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Teslabs Engineering S.L.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/device.h>
|
2022-04-01 17:14:13 +08:00
|
|
|
#include <soc.h>
|
2020-11-02 03:48:15 +08:00
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
|
|
|
|
#include <zephyr/drivers/pinctrl.h>
|
2020-11-02 03:48:15 +08:00
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/logging/log.h>
|
2020-11-02 03:48:15 +08:00
|
|
|
LOG_MODULE_REGISTER(memc_stm32, CONFIG_MEMC_LOG_LEVEL);
|
|
|
|
|
2022-04-01 17:14:13 +08:00
|
|
|
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32_fmc)
|
|
|
|
#define DT_DRV_COMPAT st_stm32_fmc
|
|
|
|
#elif DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_fmc)
|
|
|
|
#define DT_DRV_COMPAT st_stm32h7_fmc
|
|
|
|
#else
|
|
|
|
#error "No compatible FMC devicetree node found"
|
|
|
|
#endif
|
|
|
|
|
2020-11-02 03:48:15 +08:00
|
|
|
struct memc_stm32_config {
|
|
|
|
uint32_t fmc;
|
|
|
|
struct stm32_pclken pclken;
|
2021-11-08 18:34:00 +08:00
|
|
|
const struct pinctrl_dev_config *pcfg;
|
2020-11-02 03:48:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int memc_stm32_init(const struct device *dev)
|
|
|
|
{
|
|
|
|
const struct memc_stm32_config *config = dev->config;
|
|
|
|
|
|
|
|
int r;
|
|
|
|
const struct device *clk;
|
|
|
|
|
|
|
|
/* configure pinmux */
|
2021-11-08 18:34:00 +08:00
|
|
|
r = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
|
2020-11-02 03:48:15 +08:00
|
|
|
if (r < 0) {
|
|
|
|
LOG_ERR("FMC pinctrl setup failed (%d)", r);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enable FMC peripheral clock */
|
2021-02-12 01:49:24 +08:00
|
|
|
clk = DEVICE_DT_GET(STM32_CLOCK_CONTROL_NODE);
|
2020-11-02 03:48:15 +08:00
|
|
|
|
2022-08-08 22:04:09 +08:00
|
|
|
if (!device_is_ready(clk)) {
|
|
|
|
LOG_ERR("clock control device not ready");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2020-11-02 03:48:15 +08:00
|
|
|
r = clock_control_on(clk, (clock_control_subsys_t *)&config->pclken);
|
|
|
|
if (r < 0) {
|
|
|
|
LOG_ERR("Could not initialize FMC clock (%d)", r);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:14:13 +08:00
|
|
|
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32h7_fmc)
|
|
|
|
#if (DT_ENUM_IDX(DT_DRV_INST(0), st_mem_swap) == 1)
|
|
|
|
/* sdram-sram */
|
|
|
|
MODIFY_REG(FMC_Bank1_R->BTCR[0], FMC_BCR1_BMAP, FMC_BCR1_BMAP_0);
|
|
|
|
#elif (DT_ENUM_IDX(DT_DRV_INST(0), st_mem_swap) == 2)
|
|
|
|
/* sdramb2 */
|
|
|
|
MODIFY_REG(FMC_Bank1_R->BTCR[0], FMC_BCR1_BMAP, FMC_BCR1_BMAP_1);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2020-11-02 03:48:15 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-23 19:33:03 +08:00
|
|
|
PINCTRL_DT_INST_DEFINE(0);
|
2020-11-02 03:48:15 +08:00
|
|
|
|
|
|
|
static const struct memc_stm32_config config = {
|
|
|
|
.fmc = DT_INST_REG_ADDR(0),
|
|
|
|
.pclken = { .bus = DT_INST_CLOCKS_CELL(0, bus),
|
|
|
|
.enr = DT_INST_CLOCKS_CELL(0, bits) },
|
2021-11-08 18:34:00 +08:00
|
|
|
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(0),
|
2020-11-02 03:48:15 +08:00
|
|
|
};
|
|
|
|
|
2021-04-28 17:19:56 +08:00
|
|
|
DEVICE_DT_INST_DEFINE(0, memc_stm32_init, NULL, NULL,
|
2020-11-02 03:48:15 +08:00
|
|
|
&config, POST_KERNEL, CONFIG_MEMC_INIT_PRIORITY, NULL);
|