drivers: flash: mcux_flexspi_nor: use DEVICE_DT_GET

Controller device can be obtained at compile time, so make
implementation more efficient thanks to that.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2022-01-28 15:29:15 +01:00 committed by Anas Nashif
commit 5af7fe66a4

View file

@ -55,17 +55,13 @@ enum {
}; };
struct flash_flexspi_nor_config { struct flash_flexspi_nor_config {
char *controller_label; const struct device *controller;
flexspi_port_t port; flexspi_port_t port;
flexspi_device_config_t config; flexspi_device_config_t config;
struct flash_pages_layout layout; struct flash_pages_layout layout;
struct flash_parameters flash_parameters; struct flash_parameters flash_parameters;
}; };
struct flash_flexspi_nor_data {
const struct device *controller;
};
static const uint32_t flash_flexspi_nor_lut[][4] = { static const uint32_t flash_flexspi_nor_lut[][4] = {
[READ_ID] = { [READ_ID] = {
FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, SPI_NOR_CMD_RDID, FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_1PAD, SPI_NOR_CMD_RDID,
@ -152,7 +148,6 @@ static int flash_flexspi_nor_get_vendor_id(const struct device *dev,
uint8_t *vendor_id) uint8_t *vendor_id)
{ {
const struct flash_flexspi_nor_config *config = dev->config; const struct flash_flexspi_nor_config *config = dev->config;
struct flash_flexspi_nor_data *data = dev->data;
uint32_t buffer = 0; uint32_t buffer = 0;
int ret; int ret;
@ -168,7 +163,7 @@ static int flash_flexspi_nor_get_vendor_id(const struct device *dev,
LOG_DBG("Reading id"); LOG_DBG("Reading id");
ret = memc_flexspi_transfer(data->controller, &transfer); ret = memc_flexspi_transfer(config->controller, &transfer);
*vendor_id = buffer; *vendor_id = buffer;
return ret; return ret;
@ -178,7 +173,6 @@ static int flash_flexspi_nor_read_status(const struct device *dev,
uint32_t *status) uint32_t *status)
{ {
const struct flash_flexspi_nor_config *config = dev->config; const struct flash_flexspi_nor_config *config = dev->config;
struct flash_flexspi_nor_data *data = dev->data;
flexspi_transfer_t transfer = { flexspi_transfer_t transfer = {
.deviceAddress = 0, .deviceAddress = 0,
@ -192,14 +186,13 @@ static int flash_flexspi_nor_read_status(const struct device *dev,
LOG_DBG("Reading status register"); LOG_DBG("Reading status register");
return memc_flexspi_transfer(data->controller, &transfer); return memc_flexspi_transfer(config->controller, &transfer);
} }
static int flash_flexspi_nor_write_status(const struct device *dev, static int flash_flexspi_nor_write_status(const struct device *dev,
uint32_t *status) uint32_t *status)
{ {
const struct flash_flexspi_nor_config *config = dev->config; const struct flash_flexspi_nor_config *config = dev->config;
struct flash_flexspi_nor_data *data = dev->data;
flexspi_transfer_t transfer = { flexspi_transfer_t transfer = {
.deviceAddress = 0, .deviceAddress = 0,
@ -213,13 +206,12 @@ static int flash_flexspi_nor_write_status(const struct device *dev,
LOG_DBG("Writing status register"); LOG_DBG("Writing status register");
return memc_flexspi_transfer(data->controller, &transfer); return memc_flexspi_transfer(config->controller, &transfer);
} }
static int flash_flexspi_nor_write_enable(const struct device *dev) static int flash_flexspi_nor_write_enable(const struct device *dev)
{ {
const struct flash_flexspi_nor_config *config = dev->config; const struct flash_flexspi_nor_config *config = dev->config;
struct flash_flexspi_nor_data *data = dev->data;
flexspi_transfer_t transfer = { flexspi_transfer_t transfer = {
.deviceAddress = 0, .deviceAddress = 0,
@ -233,14 +225,13 @@ static int flash_flexspi_nor_write_enable(const struct device *dev)
LOG_DBG("Enabling write"); LOG_DBG("Enabling write");
return memc_flexspi_transfer(data->controller, &transfer); return memc_flexspi_transfer(config->controller, &transfer);
} }
static int flash_flexspi_nor_erase_sector(const struct device *dev, static int flash_flexspi_nor_erase_sector(const struct device *dev,
off_t offset) off_t offset)
{ {
const struct flash_flexspi_nor_config *config = dev->config; const struct flash_flexspi_nor_config *config = dev->config;
struct flash_flexspi_nor_data *data = dev->data;
flexspi_transfer_t transfer = { flexspi_transfer_t transfer = {
.deviceAddress = offset, .deviceAddress = offset,
@ -254,13 +245,12 @@ static int flash_flexspi_nor_erase_sector(const struct device *dev,
LOG_DBG("Erasing sector at 0x%08zx", (ssize_t) offset); LOG_DBG("Erasing sector at 0x%08zx", (ssize_t) offset);
return memc_flexspi_transfer(data->controller, &transfer); return memc_flexspi_transfer(config->controller, &transfer);
} }
static int flash_flexspi_nor_erase_chip(const struct device *dev) static int flash_flexspi_nor_erase_chip(const struct device *dev)
{ {
const struct flash_flexspi_nor_config *config = dev->config; const struct flash_flexspi_nor_config *config = dev->config;
struct flash_flexspi_nor_data *data = dev->data;
flexspi_transfer_t transfer = { flexspi_transfer_t transfer = {
.deviceAddress = 0, .deviceAddress = 0,
@ -274,14 +264,13 @@ static int flash_flexspi_nor_erase_chip(const struct device *dev)
LOG_DBG("Erasing chip"); LOG_DBG("Erasing chip");
return memc_flexspi_transfer(data->controller, &transfer); return memc_flexspi_transfer(config->controller, &transfer);
} }
static int flash_flexspi_nor_page_program(const struct device *dev, static int flash_flexspi_nor_page_program(const struct device *dev,
off_t offset, const void *buffer, size_t len) off_t offset, const void *buffer, size_t len)
{ {
const struct flash_flexspi_nor_config *config = dev->config; const struct flash_flexspi_nor_config *config = dev->config;
struct flash_flexspi_nor_data *data = dev->data;
flexspi_transfer_t transfer = { flexspi_transfer_t transfer = {
.deviceAddress = offset, .deviceAddress = offset,
@ -295,7 +284,7 @@ static int flash_flexspi_nor_page_program(const struct device *dev,
LOG_DBG("Page programming %d bytes to 0x%08zx", len, (ssize_t) offset); LOG_DBG("Page programming %d bytes to 0x%08zx", len, (ssize_t) offset);
return memc_flexspi_transfer(data->controller, &transfer); return memc_flexspi_transfer(config->controller, &transfer);
} }
static int flash_flexspi_nor_wait_bus_busy(const struct device *dev) static int flash_flexspi_nor_wait_bus_busy(const struct device *dev)
@ -317,12 +306,12 @@ static int flash_flexspi_nor_wait_bus_busy(const struct device *dev)
static int flash_flexspi_nor_enable_quad_mode(const struct device *dev) static int flash_flexspi_nor_enable_quad_mode(const struct device *dev)
{ {
struct flash_flexspi_nor_data *data = dev->data; const struct flash_flexspi_nor_config *config = dev->config;
uint32_t status = 0x40; uint32_t status = 0x40;
flash_flexspi_nor_write_status(dev, &status); flash_flexspi_nor_write_status(dev, &status);
flash_flexspi_nor_wait_bus_busy(dev); flash_flexspi_nor_wait_bus_busy(dev);
memc_flexspi_reset(data->controller); memc_flexspi_reset(config->controller);
return 0; return 0;
} }
@ -331,8 +320,7 @@ static int flash_flexspi_nor_read(const struct device *dev, off_t offset,
void *buffer, size_t len) void *buffer, size_t len)
{ {
const struct flash_flexspi_nor_config *config = dev->config; const struct flash_flexspi_nor_config *config = dev->config;
struct flash_flexspi_nor_data *data = dev->data; uint8_t *src = memc_flexspi_get_ahb_address(config->controller,
uint8_t *src = memc_flexspi_get_ahb_address(data->controller,
config->port, config->port,
offset); offset);
@ -345,17 +333,16 @@ static int flash_flexspi_nor_write(const struct device *dev, off_t offset,
const void *buffer, size_t len) const void *buffer, size_t len)
{ {
const struct flash_flexspi_nor_config *config = dev->config; const struct flash_flexspi_nor_config *config = dev->config;
struct flash_flexspi_nor_data *data = dev->data;
size_t size = len; size_t size = len;
uint8_t *src = (uint8_t *) buffer; uint8_t *src = (uint8_t *) buffer;
int i; int i;
unsigned int key = 0; unsigned int key = 0;
uint8_t *dst = memc_flexspi_get_ahb_address(data->controller, uint8_t *dst = memc_flexspi_get_ahb_address(config->controller,
config->port, config->port,
offset); offset);
if (memc_flexspi_is_running_xip(data->controller)) { if (memc_flexspi_is_running_xip(config->controller)) {
key = irq_lock(); key = irq_lock();
} }
@ -375,13 +362,13 @@ static int flash_flexspi_nor_write(const struct device *dev, off_t offset,
flash_flexspi_nor_page_program(dev, offset, src, i); flash_flexspi_nor_page_program(dev, offset, src, i);
#endif #endif
flash_flexspi_nor_wait_bus_busy(dev); flash_flexspi_nor_wait_bus_busy(dev);
memc_flexspi_reset(data->controller); memc_flexspi_reset(config->controller);
src += i; src += i;
offset += i; offset += i;
len -= i; len -= i;
} }
if (memc_flexspi_is_running_xip(data->controller)) { if (memc_flexspi_is_running_xip(config->controller)) {
irq_unlock(key); irq_unlock(key);
} }
@ -396,12 +383,11 @@ static int flash_flexspi_nor_erase(const struct device *dev, off_t offset,
size_t size) size_t size)
{ {
const struct flash_flexspi_nor_config *config = dev->config; const struct flash_flexspi_nor_config *config = dev->config;
struct flash_flexspi_nor_data *data = dev->data;
int num_sectors = size / SPI_NOR_SECTOR_SIZE; int num_sectors = size / SPI_NOR_SECTOR_SIZE;
int i; int i;
unsigned int key = 0; unsigned int key = 0;
uint8_t *dst = memc_flexspi_get_ahb_address(data->controller, uint8_t *dst = memc_flexspi_get_ahb_address(config->controller,
config->port, config->port,
offset); offset);
@ -415,7 +401,7 @@ static int flash_flexspi_nor_erase(const struct device *dev, off_t offset,
return -EINVAL; return -EINVAL;
} }
if (memc_flexspi_is_running_xip(data->controller)) { if (memc_flexspi_is_running_xip(config->controller)) {
key = irq_lock(); key = irq_lock();
} }
@ -423,18 +409,18 @@ static int flash_flexspi_nor_erase(const struct device *dev, off_t offset,
flash_flexspi_nor_write_enable(dev); flash_flexspi_nor_write_enable(dev);
flash_flexspi_nor_erase_chip(dev); flash_flexspi_nor_erase_chip(dev);
flash_flexspi_nor_wait_bus_busy(dev); flash_flexspi_nor_wait_bus_busy(dev);
memc_flexspi_reset(data->controller); memc_flexspi_reset(config->controller);
} else { } else {
for (i = 0; i < num_sectors; i++) { for (i = 0; i < num_sectors; i++) {
flash_flexspi_nor_write_enable(dev); flash_flexspi_nor_write_enable(dev);
flash_flexspi_nor_erase_sector(dev, offset); flash_flexspi_nor_erase_sector(dev, offset);
flash_flexspi_nor_wait_bus_busy(dev); flash_flexspi_nor_wait_bus_busy(dev);
memc_flexspi_reset(data->controller); memc_flexspi_reset(config->controller);
offset += SPI_NOR_SECTOR_SIZE; offset += SPI_NOR_SECTOR_SIZE;
} }
} }
if (memc_flexspi_is_running_xip(data->controller)) { if (memc_flexspi_is_running_xip(config->controller)) {
irq_unlock(key); irq_unlock(key);
} }
@ -467,30 +453,28 @@ static void flash_flexspi_nor_pages_layout(const struct device *dev,
static int flash_flexspi_nor_init(const struct device *dev) static int flash_flexspi_nor_init(const struct device *dev)
{ {
const struct flash_flexspi_nor_config *config = dev->config; const struct flash_flexspi_nor_config *config = dev->config;
struct flash_flexspi_nor_data *data = dev->data;
uint8_t vendor_id; uint8_t vendor_id;
data->controller = device_get_binding(config->controller_label); if (!device_is_ready(config->controller)) {
if (data->controller == NULL) { LOG_ERR("Controller device is not ready");
LOG_ERR("Could not find controller"); return -ENODEV;
return -EINVAL;
} }
if (!memc_flexspi_is_running_xip(data->controller) && if (!memc_flexspi_is_running_xip(config->controller) &&
memc_flexspi_set_device_config(data->controller, &config->config, memc_flexspi_set_device_config(config->controller, &config->config,
config->port)) { config->port)) {
LOG_ERR("Could not set device configuration"); LOG_ERR("Could not set device configuration");
return -EINVAL; return -EINVAL;
} }
if (memc_flexspi_update_lut(data->controller, 0, if (memc_flexspi_update_lut(config->controller, 0,
(const uint32_t *) flash_flexspi_nor_lut, (const uint32_t *) flash_flexspi_nor_lut,
sizeof(flash_flexspi_nor_lut) / 4)) { sizeof(flash_flexspi_nor_lut) / 4)) {
LOG_ERR("Could not update lut"); LOG_ERR("Could not update lut");
return -EINVAL; return -EINVAL;
} }
memc_flexspi_reset(data->controller); memc_flexspi_reset(config->controller);
if (flash_flexspi_nor_get_vendor_id(dev, &vendor_id)) { if (flash_flexspi_nor_get_vendor_id(dev, &vendor_id)) {
LOG_ERR("Could not read vendor id"); LOG_ERR("Could not read vendor id");
@ -551,7 +535,7 @@ static const struct flash_driver_api flash_flexspi_nor_api = {
#define FLASH_FLEXSPI_NOR(n) \ #define FLASH_FLEXSPI_NOR(n) \
static const struct flash_flexspi_nor_config \ static const struct flash_flexspi_nor_config \
flash_flexspi_nor_config_##n = { \ flash_flexspi_nor_config_##n = { \
.controller_label = DT_INST_BUS_LABEL(n), \ .controller = DEVICE_DT_GET(DT_INST_BUS(n)), \
.port = DT_INST_REG_ADDR(n), \ .port = DT_INST_REG_ADDR(n), \
.config = FLASH_FLEXSPI_DEVICE_CONFIG(n), \ .config = FLASH_FLEXSPI_DEVICE_CONFIG(n), \
.layout = { \ .layout = { \
@ -565,13 +549,10 @@ static const struct flash_driver_api flash_flexspi_nor_api = {
}, \ }, \
}; \ }; \
\ \
static struct flash_flexspi_nor_data \
flash_flexspi_nor_data_##n; \
\
DEVICE_DT_INST_DEFINE(n, \ DEVICE_DT_INST_DEFINE(n, \
flash_flexspi_nor_init, \ flash_flexspi_nor_init, \
NULL, \ NULL, \
&flash_flexspi_nor_data_##n, \ NULL, \
&flash_flexspi_nor_config_##n, \ &flash_flexspi_nor_config_##n, \
POST_KERNEL, \ POST_KERNEL, \
CONFIG_FLASH_INIT_PRIORITY, \ CONFIG_FLASH_INIT_PRIORITY, \