Lines Matching +full:mspi +full:- +full:device
3 * SPDX-License-Identifier: Apache-2.0
4 * Emulate a memory device on MSPI emulator bus
13 #include <zephyr/drivers/mspi.h>
40 const struct device *bus;
54 * Acquire the device lock.
56 * @param flash MSPI emulation flash device.
58 static void acquire(const struct device *flash) in acquire()
60 const struct flash_mspi_emul_device_config *cfg = flash->config; in acquire()
61 struct flash_mspi_emul_device_data *data = flash->data; in acquire()
63 k_sem_take(&data->lock, K_FOREVER); in acquire()
64 if (cfg->sw_multi_periph) { in acquire()
65 while (mspi_dev_config(data->bus, &cfg->dev_id, in acquire()
66 MSPI_DEVICE_CONFIG_ALL, &data->dev_cfg)) { in acquire()
70 while (mspi_dev_config(data->bus, &cfg->dev_id, in acquire()
78 * Release the device lock.
80 * @param flash MSPI emulation flash device.
82 static void release(const struct device *flash) in release()
84 struct flash_mspi_emul_device_data *data = flash->data; in release()
86 while (mspi_get_channel_status(data->bus, 0)) { in release()
90 k_sem_give(&data->lock); in release()
96 * @param target Pointer to MSPI device emulator.
97 * @param dev_id Pointer to the device ID structure from a device.
98 * @param xfer Pointer to the MSPI transfer started by dev_id.
101 * @retval -ESTALE device ID don't match, need to call mspi_dev_config first.
102 * @retval -Error transfer failed.
111 const struct flash_mspi_emul_device_config *cfg = target->dev->config; in emul_mspi_device_transceive()
112 struct flash_mspi_emul_device_data *data = target->dev->data; in emul_mspi_device_transceive()
113 struct emul_mspi_driver_api *api = (struct emul_mspi_driver_api *)data->bus->api; in emul_mspi_device_transceive()
116 __ASSERT_NO_MSG(api->trigger_event); in emul_mspi_device_transceive()
121 if (packet->address > cfg->size || in emul_mspi_device_transceive()
122 packet->address + packet->num_bytes > cfg->size) { in emul_mspi_device_transceive()
123 return -ENOMEM; in emul_mspi_device_transceive()
126 if (packet->dir == MSPI_RX) { in emul_mspi_device_transceive()
127 memcpy(packet->data_buf, data->mem + packet->address, in emul_mspi_device_transceive()
128 packet->num_bytes); in emul_mspi_device_transceive()
129 } else if (packet->dir == MSPI_TX) { in emul_mspi_device_transceive()
130 memcpy(data->mem + packet->address, packet->data_buf, in emul_mspi_device_transceive()
131 packet->num_bytes); in emul_mspi_device_transceive()
135 if (packet->cb_mask == MSPI_BUS_XFER_COMPLETE_CB) { in emul_mspi_device_transceive()
136 api->trigger_event(data->bus, MSPI_BUS_XFER_COMPLETE); in emul_mspi_device_transceive()
144 static int flash_mspi_emul_erase(const struct device *flash, off_t offset, size_t size) in flash_mspi_emul_erase()
146 const struct flash_mspi_emul_device_config *cfg = flash->config; in flash_mspi_emul_erase()
147 struct flash_mspi_emul_device_data *data = flash->data; in flash_mspi_emul_erase()
158 return -EINVAL; in flash_mspi_emul_erase()
163 return -EINVAL; in flash_mspi_emul_erase()
166 if ((offset == 0) && (size == cfg->size)) { in flash_mspi_emul_erase()
168 memset(data->mem, cfg->flash_param.erase_value, size); in flash_mspi_emul_erase()
172 memset(data->mem + offset, cfg->flash_param.erase_value, in flash_mspi_emul_erase()
178 memset(data->mem + offset, cfg->flash_param.erase_value, in flash_mspi_emul_erase()
192 * @param flash Pointer to MSPI flash device.
193 * @param offset Flash device address.
198 * @retval -Error flash read fail.
200 static int flash_mspi_emul_write(const struct device *flash, off_t offset, in flash_mspi_emul_write()
203 const struct flash_mspi_emul_device_config *cfg = flash->config; in flash_mspi_emul_write()
204 struct flash_mspi_emul_device_data *data = flash->data; in flash_mspi_emul_write()
212 data->xfer.async = false; in flash_mspi_emul_write()
213 data->xfer.xfer_mode = MSPI_DMA; in flash_mspi_emul_write()
214 data->xfer.tx_dummy = data->dev_cfg.tx_dummy; in flash_mspi_emul_write()
215 data->xfer.cmd_length = data->dev_cfg.cmd_length; in flash_mspi_emul_write()
216 data->xfer.addr_length = data->dev_cfg.addr_length; in flash_mspi_emul_write()
217 data->xfer.hold_ce = false; in flash_mspi_emul_write()
218 data->xfer.priority = 1; in flash_mspi_emul_write()
219 data->xfer.packets = &data->packet; in flash_mspi_emul_write()
220 data->xfer.num_packet = 1; in flash_mspi_emul_write()
221 data->xfer.timeout = CONFIG_MSPI_COMPLETION_TIMEOUT_TOLERANCE; in flash_mspi_emul_write()
228 i = MIN(SPI_NOR_PAGE_SIZE - (offset % SPI_NOR_PAGE_SIZE), len); in flash_mspi_emul_write()
230 data->packet.dir = MSPI_TX; in flash_mspi_emul_write()
231 data->packet.cmd = data->dev_cfg.write_cmd; in flash_mspi_emul_write()
232 data->packet.address = offset; in flash_mspi_emul_write()
233 data->packet.data_buf = src; in flash_mspi_emul_write()
234 data->packet.num_bytes = i; in flash_mspi_emul_write()
238 ret = mspi_transceive(data->bus, &cfg->dev_id, in flash_mspi_emul_write()
239 (const struct mspi_xfer *)&data->xfer); in flash_mspi_emul_write()
241 LOG_ERR("%u, MSPI write transaction failed with code: %d", __LINE__, ret); in flash_mspi_emul_write()
242 return -EIO; in flash_mspi_emul_write()
250 len -= i; in flash_mspi_emul_write()
261 * @param flash Pointer to MSPI flash device.
262 * @param offset Flash device address.
267 * @retval -Error flash read fail.
269 static int flash_mspi_emul_read(const struct device *flash, off_t offset, in flash_mspi_emul_read()
272 const struct flash_mspi_emul_device_config *cfg = flash->config; in flash_mspi_emul_read()
273 struct flash_mspi_emul_device_data *data = flash->data; in flash_mspi_emul_read()
279 data->packet.dir = MSPI_RX; in flash_mspi_emul_read()
280 data->packet.cmd = data->dev_cfg.read_cmd; in flash_mspi_emul_read()
281 data->packet.address = offset; in flash_mspi_emul_read()
282 data->packet.data_buf = rdata; in flash_mspi_emul_read()
283 data->packet.num_bytes = len; in flash_mspi_emul_read()
285 data->xfer.async = false; in flash_mspi_emul_read()
286 data->xfer.xfer_mode = MSPI_DMA; in flash_mspi_emul_read()
287 data->xfer.rx_dummy = data->dev_cfg.rx_dummy; in flash_mspi_emul_read()
288 data->xfer.cmd_length = data->dev_cfg.cmd_length; in flash_mspi_emul_read()
289 data->xfer.addr_length = data->dev_cfg.addr_length; in flash_mspi_emul_read()
290 data->xfer.hold_ce = false; in flash_mspi_emul_read()
291 data->xfer.priority = 1; in flash_mspi_emul_read()
292 data->xfer.packets = &data->packet; in flash_mspi_emul_read()
293 data->xfer.num_packet = 1; in flash_mspi_emul_read()
294 data->xfer.timeout = CONFIG_MSPI_COMPLETION_TIMEOUT_TOLERANCE; in flash_mspi_emul_read()
298 ret = mspi_transceive(data->bus, &cfg->dev_id, (const struct mspi_xfer *)&data->xfer); in flash_mspi_emul_read()
300 LOG_ERR("%u, MSPI read transaction failed with code: %d", __LINE__, ret); in flash_mspi_emul_read()
301 return -EIO; in flash_mspi_emul_read()
312 * @param flash Pointer to MSPI flash device.
316 static const struct flash_parameters *flash_mspi_emul_get_parameters(const struct device *flash) in flash_mspi_emul_get_parameters()
318 const struct flash_mspi_emul_device_config *cfg = flash->config; in flash_mspi_emul_get_parameters()
320 return &cfg->flash_param; in flash_mspi_emul_get_parameters()
327 * @param flash Pointer to MSPI flash device.
331 static void flash_mspi_emul_pages_layout(const struct device *flash, in flash_mspi_emul_pages_layout()
335 const struct flash_mspi_emul_device_config *cfg = flash->config; in flash_mspi_emul_pages_layout()
337 *layout = &cfg->page_layout; in flash_mspi_emul_pages_layout()
357 * Set up a new MSPI device emulator
359 * @param emul The MSPI device emulator instance itself
360 * @param bus The MSPI bus emulator instance
363 static int emul_mspi_device_init(const struct emul *emul_flash, const struct device *bus) in emul_mspi_device_init()
365 const struct flash_mspi_emul_device_config *cfg = emul_flash->dev->config; in emul_mspi_device_init()
366 struct flash_mspi_emul_device_data *data = emul_flash->dev->data; in emul_mspi_device_init()
368 data->bus = bus; in emul_mspi_device_init()
370 if (mspi_dev_config(data->bus, &cfg->dev_id, MSPI_DEVICE_CONFIG_ALL, in emul_mspi_device_init()
371 &cfg->tar_dev_cfg)) { in emul_mspi_device_init()
372 LOG_ERR("%u, Failed to config mspi controller", __LINE__); in emul_mspi_device_init()
373 return -EIO; in emul_mspi_device_init()
375 data->dev_cfg = cfg->tar_dev_cfg; in emul_mspi_device_init()
378 if (cfg->tar_xip_cfg.enable) { in emul_mspi_device_init()
379 if (mspi_xip_config(data->bus, &cfg->dev_id, &cfg->tar_xip_cfg)) { in emul_mspi_device_init()
381 return -EIO; in emul_mspi_device_init()
383 data->xip_cfg = cfg->tar_xip_cfg; in emul_mspi_device_init()
388 if (cfg->tar_scramble_cfg.enable) { in emul_mspi_device_init()
389 if (mspi_scramble_config(data->bus, &cfg->dev_id, &cfg->tar_scramble_cfg)) { in emul_mspi_device_init()
391 return -EIO; in emul_mspi_device_init()
393 data->scramble_cfg = cfg->tar_scramble_cfg; in emul_mspi_device_init()
398 if (mspi_timing_config(data->bus, &cfg->dev_id, in emul_mspi_device_init()
399 MSPI_TIMING_PARAM_DUMMY, &data->timing_cfg)) { in emul_mspi_device_init()
401 return -EIO; in emul_mspi_device_init()
405 release(emul_flash->dev); in emul_mspi_device_init()
410 static int flash_mspi_emul_device_init_stub(const struct device *dev) in flash_mspi_emul_device_init_stub()