Lines Matching +full:mspi +full:- +full:data +full:- +full:rate

4  * SPDX-License-Identifier: Apache-2.0
16 #include <zephyr/drivers/mspi.h>
91 LOG_INST_ERR(cfg->log, "%u,Frequency not supported!", __LINE__); in mspi_set_freq()
105 LOG_INST_ERR(cfg->log, "%u, incorrect data rate, only SDR is supported.", __LINE__); in mspi_set_line()
188 if (ctx->owner) { in mspi_context_ce_control()
189 if (ctx->xfer.hold_ce && in mspi_context_ce_control()
190 ctx->xfer.ce_sw_ctrl.gpio.port != NULL) { in mspi_context_ce_control()
192 gpio_pin_set_dt(&ctx->xfer.ce_sw_ctrl.gpio, 1); in mspi_context_ce_control()
193 k_busy_wait(ctx->xfer.ce_sw_ctrl.delay); in mspi_context_ce_control()
195 k_busy_wait(ctx->xfer.ce_sw_ctrl.delay); in mspi_context_ce_control()
196 gpio_pin_set_dt(&ctx->xfer.ce_sw_ctrl.gpio, 0); in mspi_context_ce_control()
204 ctx->owner = NULL; in mspi_context_release()
205 k_sem_give(&ctx->lock); in mspi_context_release()
212 if (!k_sem_count_get(&ctx->lock)) { in mspi_context_unlock_unconditionally()
213 ctx->owner = NULL; in mspi_context_unlock_unconditionally()
214 k_sem_give(&ctx->lock); in mspi_context_unlock_unconditionally()
227 if ((k_sem_count_get(&ctx->lock) == 0) && !lockon && in mspi_context_lock()
228 (ctx->owner == req)) { in mspi_context_lock()
232 if (k_sem_take(&ctx->lock, K_MSEC(xfer->timeout))) { in mspi_context_lock()
233 return -EBUSY; in mspi_context_lock()
235 if (ctx->xfer.async) { in mspi_context_lock()
236 if ((xfer->tx_dummy == ctx->xfer.tx_dummy) && in mspi_context_lock()
237 (xfer->rx_dummy == ctx->xfer.rx_dummy) && in mspi_context_lock()
238 (xfer->cmd_length == ctx->xfer.cmd_length) && in mspi_context_lock()
239 (xfer->addr_length == ctx->xfer.addr_length)) { in mspi_context_lock()
241 } else if (ctx->packets_left == 0) { in mspi_context_lock()
242 if (ctx->callback_ctx) { in mspi_context_lock()
245 evt_data = &ctx->callback_ctx->mspi_evt.evt_data; in mspi_context_lock()
246 while (evt_data->status != 0) { in mspi_context_lock()
253 return -EIO; in mspi_context_lock()
256 ctx->owner = req; in mspi_context_lock()
257 ctx->xfer = *xfer; in mspi_context_lock()
258 ctx->packets_done = 0; in mspi_context_lock()
259 ctx->packets_left = ctx->xfer.num_packet; in mspi_context_lock()
260 ctx->callback = callback; in mspi_context_lock()
261 ctx->callback_ctx = callback_ctx; in mspi_context_lock()
267 struct mspi_ambiq_data *data = controller->data; in mspi_is_inp() local
269 return (k_sem_count_get(&data->ctx.lock) == 0); in mspi_is_inp()
275 const struct mspi_ambiq_config *cfg = controller->config; in mspi_verify_device()
276 int device_index = cfg->mspicfg.num_periph; in mspi_verify_device()
279 for (int i = 0; i < cfg->mspicfg.num_periph; i++) { in mspi_verify_device()
280 if (dev_id->ce.port == cfg->mspicfg.ce_group[i].port && in mspi_verify_device()
281 dev_id->ce.pin == cfg->mspicfg.ce_group[i].pin && in mspi_verify_device()
282 dev_id->ce.dt_flags == cfg->mspicfg.ce_group[i].dt_flags) { in mspi_verify_device()
287 if (device_index >= cfg->mspicfg.num_periph || in mspi_verify_device()
288 device_index != dev_id->dev_idx) { in mspi_verify_device()
289 LOG_INST_ERR(cfg->log, "%u, invalid device ID.", __LINE__); in mspi_verify_device()
290 return -ENODEV; in mspi_verify_device()
298 const struct mspi_ambiq_config *cfg = controller->config; in mspi_ambiq_deinit()
299 struct mspi_ambiq_data *data = controller->data; in mspi_ambiq_deinit() local
302 if (!data->mspiHandle) { in mspi_ambiq_deinit()
303 LOG_INST_ERR(cfg->log, "%u, the mspi not yet initialized.", __LINE__); in mspi_ambiq_deinit()
304 return -ENODEV; in mspi_ambiq_deinit()
307 if (k_mutex_lock(&data->lock, K_MSEC(CONFIG_MSPI_COMPLETION_TIMEOUT_TOLERANCE))) { in mspi_ambiq_deinit()
308 LOG_INST_ERR(cfg->log, "%u, fail to gain controller access.", __LINE__); in mspi_ambiq_deinit()
309 return -EBUSY; in mspi_ambiq_deinit()
312 ret = am_hal_mspi_interrupt_disable(data->mspiHandle, 0xFFFFFFFF); in mspi_ambiq_deinit()
314 LOG_INST_ERR(cfg->log, "%u, fail to disable interrupt, code:%d.", in mspi_ambiq_deinit()
316 ret = -EHOSTDOWN; in mspi_ambiq_deinit()
320 ret = am_hal_mspi_interrupt_clear(data->mspiHandle, 0xFFFFFFFF); in mspi_ambiq_deinit()
322 LOG_INST_ERR(cfg->log, "%u, fail to clear interrupt, code:%d.", in mspi_ambiq_deinit()
324 ret = -EHOSTDOWN; in mspi_ambiq_deinit()
328 ret = am_hal_mspi_disable(data->mspiHandle); in mspi_ambiq_deinit()
330 LOG_INST_ERR(cfg->log, "%u, fail to disable MSPI, code:%d.", in mspi_ambiq_deinit()
332 ret = -EHOSTDOWN; in mspi_ambiq_deinit()
336 ret = am_hal_mspi_power_control(data->mspiHandle, AM_HAL_SYSCTRL_DEEPSLEEP, false); in mspi_ambiq_deinit()
338 LOG_INST_ERR(cfg->log, "%u, fail to power off MSPI, code:%d.", in mspi_ambiq_deinit()
340 ret = -EHOSTDOWN; in mspi_ambiq_deinit()
344 ret = am_hal_mspi_deinitialize(data->mspiHandle); in mspi_ambiq_deinit()
346 LOG_INST_ERR(cfg->log, "%u, fail to deinit MSPI.", __LINE__); in mspi_ambiq_deinit()
347 ret = -ENODEV; in mspi_ambiq_deinit()
353 k_mutex_unlock(&data->lock); in mspi_ambiq_deinit()
361 const struct mspi_ambiq_config *cfg = controller->config; in mspi_xfer_config()
362 struct mspi_ambiq_data *data = controller->data; in mspi_xfer_config() local
363 am_hal_mspi_dev_config_t hal_dev_cfg = data->hal_dev_cfg; in mspi_xfer_config()
367 if (data->scramble_cfg.enable) { in mspi_xfer_config()
373 ret = am_hal_mspi_disable(data->mspiHandle); in mspi_xfer_config()
375 LOG_INST_ERR(cfg->log, "%u, fail to disable MSPI, code:%d.", in mspi_xfer_config()
377 return -EHOSTDOWN; in mspi_xfer_config()
380 ret = am_hal_mspi_control(data->mspiHandle, eRequest, NULL); in mspi_xfer_config()
382 LOG_INST_ERR(cfg->log, "%u,Unable to complete scramble config:%d.", in mspi_xfer_config()
383 __LINE__, data->scramble_cfg.enable); in mspi_xfer_config()
384 return -EHOSTDOWN; in mspi_xfer_config()
387 if (xfer->cmd_length > AM_HAL_MSPI_INSTR_2_BYTE + 1) { in mspi_xfer_config()
388 LOG_INST_ERR(cfg->log, "%u, cmd_length is too large.", __LINE__); in mspi_xfer_config()
389 return -ENOTSUP; in mspi_xfer_config()
391 if (xfer->cmd_length == 0) { in mspi_xfer_config()
395 hal_dev_cfg.eInstrCfg = xfer->cmd_length - 1; in mspi_xfer_config()
398 if (xfer->addr_length > AM_HAL_MSPI_ADDR_4_BYTE + 1) { in mspi_xfer_config()
399 LOG_INST_ERR(cfg->log, "%u, addr_length is too large.", __LINE__); in mspi_xfer_config()
400 return -ENOTSUP; in mspi_xfer_config()
402 if (xfer->addr_length == 0) { in mspi_xfer_config()
406 hal_dev_cfg.eAddrCfg = xfer->addr_length - 1; in mspi_xfer_config()
409 hal_dev_cfg.bTurnaround = (xfer->rx_dummy != 0); in mspi_xfer_config()
410 hal_dev_cfg.ui8TurnAround = (uint8_t)xfer->rx_dummy; in mspi_xfer_config()
411 hal_dev_cfg.bEnWriteLatency = (xfer->tx_dummy != 0); in mspi_xfer_config()
412 hal_dev_cfg.ui8WriteLatency = (uint8_t)xfer->tx_dummy; in mspi_xfer_config()
414 ret = am_hal_mspi_device_configure(data->mspiHandle, &hal_dev_cfg); in mspi_xfer_config()
416 LOG_INST_ERR(cfg->log, "%u, fail to configure MSPI, code:%d.", in mspi_xfer_config()
418 return -EHOSTDOWN; in mspi_xfer_config()
421 ret = am_hal_mspi_enable(data->mspiHandle); in mspi_xfer_config()
423 LOG_INST_ERR(cfg->log, "%u, fail to enable MSPI, code:%d.", in mspi_xfer_config()
425 return -EHOSTDOWN; in mspi_xfer_config()
428 data->hal_dev_cfg = hal_dev_cfg; in mspi_xfer_config()
434 const struct mspi_cfg *config = &spec->config; in mspi_ambiq_config()
435 const struct mspi_ambiq_config *cfg = spec->bus->config; in mspi_ambiq_config()
436 struct mspi_ambiq_data *data = spec->bus->data; in mspi_ambiq_config() local
440 if (config->op_mode != MSPI_OP_MODE_CONTROLLER) { in mspi_ambiq_config()
441 LOG_INST_ERR(cfg->log, "%u, only support MSPI controller mode.", __LINE__); in mspi_ambiq_config()
442 return -ENOTSUP; in mspi_ambiq_config()
445 if (config->max_freq > MSPI_MAX_FREQ) { in mspi_ambiq_config()
446 LOG_INST_ERR(cfg->log, "%u, max_freq too large.", __LINE__); in mspi_ambiq_config()
447 return -ENOTSUP; in mspi_ambiq_config()
450 if (config->duplex != MSPI_HALF_DUPLEX) { in mspi_ambiq_config()
451 LOG_INST_ERR(cfg->log, "%u, only support half duplex mode.", __LINE__); in mspi_ambiq_config()
452 return -ENOTSUP; in mspi_ambiq_config()
455 if (config->dqs_support) { in mspi_ambiq_config()
456 LOG_INST_ERR(cfg->log, "%u, only support non-DQS mode.", __LINE__); in mspi_ambiq_config()
457 return -ENOTSUP; in mspi_ambiq_config()
460 if (config->re_init) { in mspi_ambiq_config()
461 ret = mspi_ambiq_deinit(spec->bus); in mspi_ambiq_config()
467 ret = am_hal_mspi_initialize(config->channel_num, &data->mspiHandle); in mspi_ambiq_config()
469 LOG_INST_ERR(cfg->log, "%u, fail to initialize MSPI, code:%d.", in mspi_ambiq_config()
471 return -EPERM; in mspi_ambiq_config()
474 ret = am_hal_mspi_power_control(data->mspiHandle, AM_HAL_SYSCTRL_WAKE, false); in mspi_ambiq_config()
476 LOG_INST_ERR(cfg->log, "%u, fail to power on MSPI, code:%d.", in mspi_ambiq_config()
478 return -EHOSTDOWN; in mspi_ambiq_config()
481 ret = am_hal_mspi_enable(data->mspiHandle); in mspi_ambiq_config()
483 LOG_INST_ERR(cfg->log, "%u, fail to Enable MSPI, code:%d.", in mspi_ambiq_config()
485 return -EHOSTDOWN; in mspi_ambiq_config()
488 ret = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT); in mspi_ambiq_config()
493 ret = am_hal_mspi_interrupt_clear(data->mspiHandle, AM_HAL_MSPI_INT_CQUPD | in mspi_ambiq_config()
496 LOG_INST_ERR(cfg->log, "%u, fail to clear interrupt, code:%d.", in mspi_ambiq_config()
498 return -EHOSTDOWN; in mspi_ambiq_config()
501 ret = am_hal_mspi_interrupt_enable(data->mspiHandle, AM_HAL_MSPI_INT_CQUPD | in mspi_ambiq_config()
504 LOG_INST_ERR(cfg->log, "%u, fail to turn on interrupt, code:%d.", in mspi_ambiq_config()
506 return -EHOSTDOWN; in mspi_ambiq_config()
509 cfg->irq_cfg_func(); in mspi_ambiq_config()
511 mspi_context_unlock_unconditionally(&data->ctx); in mspi_ambiq_config()
513 if (config->re_init) { in mspi_ambiq_config()
514 k_mutex_unlock(&data->lock); in mspi_ambiq_config()
525 const struct mspi_ambiq_config *cfg = controller->config; in mspi_ambiq_dev_config()
526 struct mspi_ambiq_data *data = controller->data; in mspi_ambiq_dev_config() local
527 am_hal_mspi_dev_config_t hal_dev_cfg = data->hal_dev_cfg; in mspi_ambiq_dev_config()
530 if (data->dev_id != dev_id) { in mspi_ambiq_dev_config()
531 if (k_mutex_lock(&data->lock, K_MSEC(CONFIG_MSPI_COMPLETION_TIMEOUT_TOLERANCE))) { in mspi_ambiq_dev_config()
532 LOG_INST_ERR(cfg->log, "%u, fail to gain controller access.", __LINE__); in mspi_ambiq_dev_config()
533 return -EBUSY; in mspi_ambiq_dev_config()
543 ret = -EBUSY; in mspi_ambiq_dev_config()
548 !cfg->mspicfg.sw_multi_periph) { in mspi_ambiq_dev_config()
550 data->dev_id = (struct mspi_dev_id *)dev_id; in mspi_ambiq_dev_config()
554 if (data->dev_id != dev_id) { in mspi_ambiq_dev_config()
555 LOG_INST_ERR(cfg->log, "%u, config failed, must be the same device.", in mspi_ambiq_dev_config()
557 ret = -ENOTSUP; in mspi_ambiq_dev_config()
567 LOG_INST_ERR(cfg->log, "%u, config type not supported.", __LINE__); in mspi_ambiq_dev_config()
568 ret = -ENOTSUP; in mspi_ambiq_dev_config()
573 hal_dev_cfg.eClockFreq = mspi_set_freq(cfg, dev_cfg->freq); in mspi_ambiq_dev_config()
575 ret = -ENOTSUP; in mspi_ambiq_dev_config()
578 ret = am_hal_mspi_control(data->mspiHandle, in mspi_ambiq_dev_config()
582 LOG_INST_ERR(cfg->log, "%u, failed to configure eClockFreq.", in mspi_ambiq_dev_config()
584 ret = -EHOSTDOWN; in mspi_ambiq_dev_config()
587 data->dev_cfg.freq = dev_cfg->freq; in mspi_ambiq_dev_config()
593 hal_dev_cfg.eDeviceConfig = mspi_set_line(cfg, dev_cfg->io_mode, in mspi_ambiq_dev_config()
594 dev_cfg->data_rate, in mspi_ambiq_dev_config()
595 dev_cfg->ce_num); in mspi_ambiq_dev_config()
597 ret = -ENOTSUP; in mspi_ambiq_dev_config()
600 ret = am_hal_mspi_control(data->mspiHandle, in mspi_ambiq_dev_config()
604 LOG_INST_ERR(cfg->log, "%u, failed to configure device.", __LINE__); in mspi_ambiq_dev_config()
605 ret = -EHOSTDOWN; in mspi_ambiq_dev_config()
608 data->dev_cfg.freq = dev_cfg->io_mode; in mspi_ambiq_dev_config()
609 data->dev_cfg.data_rate = dev_cfg->data_rate; in mspi_ambiq_dev_config()
610 data->dev_cfg.ce_num = dev_cfg->ce_num; in mspi_ambiq_dev_config()
614 if (dev_cfg->cmd_length > AM_HAL_MSPI_INSTR_2_BYTE + 1 || in mspi_ambiq_dev_config()
615 dev_cfg->cmd_length == 0) { in mspi_ambiq_dev_config()
616 LOG_INST_ERR(cfg->log, "%u, invalid cmd_length.", __LINE__); in mspi_ambiq_dev_config()
617 ret = -ENOTSUP; in mspi_ambiq_dev_config()
620 hal_dev_cfg.eInstrCfg = dev_cfg->cmd_length - 1; in mspi_ambiq_dev_config()
621 ret = am_hal_mspi_control(data->mspiHandle, in mspi_ambiq_dev_config()
625 LOG_INST_ERR(cfg->log, "%u, failed to configure cmd_length.", in mspi_ambiq_dev_config()
627 ret = -EHOSTDOWN; in mspi_ambiq_dev_config()
630 data->dev_cfg.cmd_length = dev_cfg->cmd_length; in mspi_ambiq_dev_config()
634 if (dev_cfg->addr_length > AM_HAL_MSPI_ADDR_4_BYTE + 1 || in mspi_ambiq_dev_config()
635 dev_cfg->addr_length == 0) { in mspi_ambiq_dev_config()
636 LOG_INST_ERR(cfg->log, "%u, invalid addr_length.", __LINE__); in mspi_ambiq_dev_config()
637 ret = -ENOTSUP; in mspi_ambiq_dev_config()
640 hal_dev_cfg.eAddrCfg = dev_cfg->addr_length - 1; in mspi_ambiq_dev_config()
641 ret = am_hal_mspi_control(data->mspiHandle, in mspi_ambiq_dev_config()
645 LOG_INST_ERR(cfg->log, "%u, failed to configure addr_length.", in mspi_ambiq_dev_config()
647 ret = -EHOSTDOWN; in mspi_ambiq_dev_config()
650 data->dev_cfg.addr_length = dev_cfg->addr_length; in mspi_ambiq_dev_config()
655 if (data->dev_id != dev_id) { in mspi_ambiq_dev_config()
656 ret = pinctrl_apply_state(cfg->pcfg, in mspi_ambiq_dev_config()
657 PINCTRL_STATE_PRIV_START + dev_id->dev_idx); in mspi_ambiq_dev_config()
663 if (memcmp(&data->dev_cfg, dev_cfg, sizeof(struct mspi_dev_cfg)) == 0) { in mspi_ambiq_dev_config()
665 data->dev_id = (struct mspi_dev_id *)dev_id; in mspi_ambiq_dev_config()
669 if (dev_cfg->endian != MSPI_XFER_LITTLE_ENDIAN) { in mspi_ambiq_dev_config()
670 LOG_INST_ERR(cfg->log, "%u, only support MSB first.", __LINE__); in mspi_ambiq_dev_config()
671 ret = -ENOTSUP; in mspi_ambiq_dev_config()
675 if (dev_cfg->dqs_enable && !cfg->mspicfg.dqs_support) { in mspi_ambiq_dev_config()
676 LOG_INST_ERR(cfg->log, "%u, only support non-DQS mode.", __LINE__); in mspi_ambiq_dev_config()
677 ret = -ENOTSUP; in mspi_ambiq_dev_config()
681 hal_dev_cfg.eSpiMode = dev_cfg->cpp; in mspi_ambiq_dev_config()
682 hal_dev_cfg.bEnWriteLatency = (dev_cfg->tx_dummy != 0); in mspi_ambiq_dev_config()
683 hal_dev_cfg.ui8WriteLatency = dev_cfg->tx_dummy; in mspi_ambiq_dev_config()
684 hal_dev_cfg.bTurnaround = (dev_cfg->rx_dummy != 0); in mspi_ambiq_dev_config()
685 hal_dev_cfg.ui8TurnAround = dev_cfg->rx_dummy; in mspi_ambiq_dev_config()
687 hal_dev_cfg.eClockFreq = mspi_set_freq(cfg, dev_cfg->freq); in mspi_ambiq_dev_config()
689 ret = -ENOTSUP; in mspi_ambiq_dev_config()
693 hal_dev_cfg.eDeviceConfig = mspi_set_line(cfg, dev_cfg->io_mode, dev_cfg->data_rate, in mspi_ambiq_dev_config()
694 dev_cfg->ce_num); in mspi_ambiq_dev_config()
696 ret = -ENOTSUP; in mspi_ambiq_dev_config()
700 if (dev_cfg->cmd_length > AM_HAL_MSPI_INSTR_2_BYTE + 1) { in mspi_ambiq_dev_config()
701 LOG_INST_ERR(cfg->log, "%u, cmd_length too large.", __LINE__); in mspi_ambiq_dev_config()
702 ret = -ENOTSUP; in mspi_ambiq_dev_config()
705 if (dev_cfg->cmd_length == 0) { in mspi_ambiq_dev_config()
709 hal_dev_cfg.eInstrCfg = dev_cfg->cmd_length - 1; in mspi_ambiq_dev_config()
712 if (dev_cfg->addr_length > AM_HAL_MSPI_ADDR_4_BYTE + 1) { in mspi_ambiq_dev_config()
713 LOG_INST_ERR(cfg->log, "%u, addr_length too large.", __LINE__); in mspi_ambiq_dev_config()
714 ret = -ENOTSUP; in mspi_ambiq_dev_config()
717 if (dev_cfg->addr_length == 0) { in mspi_ambiq_dev_config()
721 hal_dev_cfg.eAddrCfg = dev_cfg->addr_length - 1; in mspi_ambiq_dev_config()
724 hal_dev_cfg.ui8ReadInstr = (uint8_t)dev_cfg->read_cmd; in mspi_ambiq_dev_config()
725 hal_dev_cfg.ui8WriteInstr = (uint8_t)dev_cfg->write_cmd; in mspi_ambiq_dev_config()
727 hal_dev_cfg.eDMABoundary = mspi_set_mem_boundary(dev_cfg->mem_boundary); in mspi_ambiq_dev_config()
729 LOG_INST_ERR(cfg->log, "%u, mem_boundary too large.", __LINE__); in mspi_ambiq_dev_config()
730 ret = -ENOTSUP; in mspi_ambiq_dev_config()
735 hal_dev_cfg.ui16DMATimeLimit = dev_cfg->time_to_break * 10; in mspi_ambiq_dev_config()
737 ret = am_hal_mspi_disable(data->mspiHandle); in mspi_ambiq_dev_config()
739 LOG_INST_ERR(cfg->log, "%u, fail to disable MSPI, code:%d.", __LINE__, ret); in mspi_ambiq_dev_config()
740 ret = -EHOSTDOWN; in mspi_ambiq_dev_config()
744 ret = am_hal_mspi_device_configure(data->mspiHandle, &hal_dev_cfg); in mspi_ambiq_dev_config()
746 LOG_INST_ERR(cfg->log, "%u, fail to configure MSPI, code:%d.", __LINE__, in mspi_ambiq_dev_config()
748 ret = -EHOSTDOWN; in mspi_ambiq_dev_config()
752 ret = am_hal_mspi_enable(data->mspiHandle); in mspi_ambiq_dev_config()
754 LOG_INST_ERR(cfg->log, "%u, fail to enable MSPI, code:%d.", __LINE__, ret); in mspi_ambiq_dev_config()
755 ret = -EHOSTDOWN; in mspi_ambiq_dev_config()
758 data->dev_cfg = *dev_cfg; in mspi_ambiq_dev_config()
759 data->dev_id = (struct mspi_dev_id *)dev_id; in mspi_ambiq_dev_config()
761 data->hal_dev_cfg = hal_dev_cfg; in mspi_ambiq_dev_config()
766 k_mutex_unlock(&data->lock); in mspi_ambiq_dev_config()
774 const struct mspi_ambiq_config *cfg = controller->config; in mspi_ambiq_xip_config()
775 struct mspi_ambiq_data *data = controller->data; in mspi_ambiq_xip_config() local
779 if (dev_id != data->dev_id) { in mspi_ambiq_xip_config()
780 LOG_INST_ERR(cfg->log, "%u, dev_id don't match.", __LINE__); in mspi_ambiq_xip_config()
781 return -ESTALE; in mspi_ambiq_xip_config()
784 if (xip_cfg->enable) { in mspi_ambiq_xip_config()
790 ret = am_hal_mspi_control(data->mspiHandle, eRequest, NULL); in mspi_ambiq_xip_config()
792 LOG_INST_ERR(cfg->log, "%u,Unable to complete xip config:%d.", __LINE__, in mspi_ambiq_xip_config()
793 xip_cfg->enable); in mspi_ambiq_xip_config()
794 return -EHOSTDOWN; in mspi_ambiq_xip_config()
797 data->xip_cfg = *xip_cfg; in mspi_ambiq_xip_config()
805 const struct mspi_ambiq_config *cfg = controller->config; in mspi_ambiq_scramble_config()
806 struct mspi_ambiq_data *data = controller->data; in mspi_ambiq_scramble_config() local
807 am_hal_mspi_dev_config_t hal_dev_cfg = data->hal_dev_cfg; in mspi_ambiq_scramble_config()
812 return -EBUSY; in mspi_ambiq_scramble_config()
815 if (dev_id != data->dev_id) { in mspi_ambiq_scramble_config()
816 LOG_INST_ERR(cfg->log, "%u, dev_id don't match.", __LINE__); in mspi_ambiq_scramble_config()
817 return -ESTALE; in mspi_ambiq_scramble_config()
820 if (scramble_cfg->enable) { in mspi_ambiq_scramble_config()
826 ret = am_hal_mspi_disable(data->mspiHandle); in mspi_ambiq_scramble_config()
828 LOG_INST_ERR(cfg->log, "%u, fail to disable MSPI, code:%d.", __LINE__, ret); in mspi_ambiq_scramble_config()
829 return -EHOSTDOWN; in mspi_ambiq_scramble_config()
832 ret = am_hal_mspi_control(data->mspiHandle, eRequest, NULL); in mspi_ambiq_scramble_config()
834 LOG_INST_ERR(cfg->log, "%u,Unable to complete scramble config:%d.", __LINE__, in mspi_ambiq_scramble_config()
835 scramble_cfg->enable); in mspi_ambiq_scramble_config()
836 return -EHOSTDOWN; in mspi_ambiq_scramble_config()
839 hal_dev_cfg.scramblingStartAddr = 0 + scramble_cfg->address_offset; in mspi_ambiq_scramble_config()
840 hal_dev_cfg.scramblingEndAddr = hal_dev_cfg.scramblingStartAddr + scramble_cfg->size; in mspi_ambiq_scramble_config()
842 ret = am_hal_mspi_device_configure(data->mspiHandle, &hal_dev_cfg); in mspi_ambiq_scramble_config()
844 LOG_INST_ERR(cfg->log, "%u, fail to configure MSPI, code:%d.", __LINE__, ret); in mspi_ambiq_scramble_config()
845 return -EHOSTDOWN; in mspi_ambiq_scramble_config()
848 ret = am_hal_mspi_enable(data->mspiHandle); in mspi_ambiq_scramble_config()
850 LOG_INST_ERR(cfg->log, "%u, fail to enable MSPI, code:%d.", __LINE__, ret); in mspi_ambiq_scramble_config()
851 return -EHOSTDOWN; in mspi_ambiq_scramble_config()
854 data->scramble_cfg = *scramble_cfg; in mspi_ambiq_scramble_config()
855 data->hal_dev_cfg = hal_dev_cfg; in mspi_ambiq_scramble_config()
864 const struct mspi_ambiq_config *cfg = controller->config; in mspi_ambiq_timing_config()
865 struct mspi_ambiq_data *data = controller->data; in mspi_ambiq_timing_config() local
866 am_hal_mspi_dev_config_t hal_dev_cfg = data->hal_dev_cfg; in mspi_ambiq_timing_config()
872 return -EBUSY; in mspi_ambiq_timing_config()
875 if (dev_id != data->dev_id) { in mspi_ambiq_timing_config()
876 LOG_INST_ERR(cfg->log, "%u, dev_id don't match.", __LINE__); in mspi_ambiq_timing_config()
877 return -ESTALE; in mspi_ambiq_timing_config()
881 LOG_INST_ERR(cfg->log, "%u, config type not supported.", __LINE__); in mspi_ambiq_timing_config()
882 return -ENOTSUP; in mspi_ambiq_timing_config()
886 if (time_cfg->ui8WriteLatency) { in mspi_ambiq_timing_config()
891 hal_dev_cfg.ui8WriteLatency = time_cfg->ui8WriteLatency; in mspi_ambiq_timing_config()
895 if (time_cfg->ui8TurnAround) { in mspi_ambiq_timing_config()
900 hal_dev_cfg.ui8TurnAround = time_cfg->ui8TurnAround; in mspi_ambiq_timing_config()
906 ret = am_hal_mspi_control(data->mspiHandle, AM_HAL_MSPI_REQ_TIMING_SCAN, &timing); in mspi_ambiq_timing_config()
908 LOG_INST_ERR(cfg->log, "%u, fail to configure timing.", __LINE__); in mspi_ambiq_timing_config()
909 return -EHOSTDOWN; in mspi_ambiq_timing_config()
912 data->hal_dev_cfg = hal_dev_cfg; in mspi_ambiq_timing_config()
920 const struct mspi_ambiq_config *cfg = controller->config; in mspi_ambiq_get_channel_status()
921 struct mspi_ambiq_data *data = controller->data; in mspi_ambiq_get_channel_status() local
924 if (sys_read32(cfg->reg_base) & MSPI_BUSY) { in mspi_ambiq_get_channel_status()
925 ret = -EBUSY; in mspi_ambiq_get_channel_status()
929 return -EBUSY; in mspi_ambiq_get_channel_status()
932 data->dev_id = NULL; in mspi_ambiq_get_channel_status()
933 k_mutex_unlock(&data->lock); in mspi_ambiq_get_channel_status()
940 struct mspi_ambiq_data *data = dev->data; in mspi_ambiq_isr() local
943 am_hal_mspi_interrupt_status_get(data->mspiHandle, &ui32Status, false); in mspi_ambiq_isr()
944 am_hal_mspi_interrupt_clear(data->mspiHandle, ui32Status); in mspi_ambiq_isr()
945 am_hal_mspi_interrupt_service(data->mspiHandle, ui32Status); in mspi_ambiq_isr()
952 struct mspi_ambiq_data *data = controller->data; in hal_mspi_callback() local
954 data->ctx.packets_done++; in hal_mspi_callback()
960 const struct mspi_ambiq_config *cfg = controller->config; in mspi_pio_prepare()
961 struct mspi_ambiq_data *data = controller->data; in mspi_pio_prepare() local
962 const struct mspi_xfer *xfer = &data->ctx.xfer; in mspi_pio_prepare()
965 trans->bScrambling = false; in mspi_pio_prepare()
966 trans->bSendAddr = (xfer->addr_length != 0); in mspi_pio_prepare()
967 trans->bSendInstr = (xfer->cmd_length != 0); in mspi_pio_prepare()
968 trans->bTurnaround = (xfer->rx_dummy != 0); in mspi_pio_prepare()
969 trans->bEnWRLatency = (xfer->tx_dummy != 0); in mspi_pio_prepare()
970 trans->bDCX = false; in mspi_pio_prepare()
971 trans->bQuadCmd = false; in mspi_pio_prepare()
972 trans->bContinue = false; in mspi_pio_prepare()
974 if (xfer->cmd_length > AM_HAL_MSPI_INSTR_2_BYTE + 1) { in mspi_pio_prepare()
975 LOG_INST_ERR(cfg->log, "%u, invalid cmd_length.", __LINE__); in mspi_pio_prepare()
976 return -ENOTSUP; in mspi_pio_prepare()
978 if (xfer->cmd_length != 0) { in mspi_pio_prepare()
979 am_hal_mspi_instr_e eInstrCfg = xfer->cmd_length - 1; in mspi_pio_prepare()
981 ret = am_hal_mspi_control(data->mspiHandle, AM_HAL_MSPI_REQ_ISIZE_SET, &eInstrCfg); in mspi_pio_prepare()
983 LOG_INST_ERR(cfg->log, "%u, failed to configure cmd_length.", in mspi_pio_prepare()
985 return -EHOSTDOWN; in mspi_pio_prepare()
987 data->hal_dev_cfg.eInstrCfg = eInstrCfg; in mspi_pio_prepare()
989 data->dev_cfg.cmd_length = xfer->cmd_length; in mspi_pio_prepare()
991 if (xfer->addr_length > AM_HAL_MSPI_ADDR_4_BYTE + 1) { in mspi_pio_prepare()
992 LOG_INST_ERR(cfg->log, "%u, invalid addr_length.", __LINE__); in mspi_pio_prepare()
993 return -ENOTSUP; in mspi_pio_prepare()
995 if (xfer->addr_length != 0) { in mspi_pio_prepare()
996 am_hal_mspi_addr_e eAddrCfg = xfer->addr_length - 1; in mspi_pio_prepare()
998 ret = am_hal_mspi_control(data->mspiHandle, AM_HAL_MSPI_REQ_ASIZE_SET, &eAddrCfg); in mspi_pio_prepare()
1000 LOG_INST_ERR(cfg->log, "%u, failed to configure addr_length.", __LINE__); in mspi_pio_prepare()
1001 return -EHOSTDOWN; in mspi_pio_prepare()
1003 data->hal_dev_cfg.eAddrCfg = eAddrCfg; in mspi_pio_prepare()
1005 data->dev_cfg.addr_length = xfer->addr_length; in mspi_pio_prepare()
1015 const struct mspi_ambiq_config *cfg = controller->config; in mspi_pio_transceive()
1016 struct mspi_ambiq_data *data = controller->data; in mspi_pio_transceive() local
1017 struct mspi_context *ctx = &data->ctx; in mspi_pio_transceive()
1024 if (xfer->num_packet == 0 || in mspi_pio_transceive()
1025 !xfer->packets || in mspi_pio_transceive()
1026 xfer->timeout > CONFIG_MSPI_COMPLETION_TIMEOUT_TOLERANCE) { in mspi_pio_transceive()
1027 return -EFAULT; in mspi_pio_transceive()
1030 cfg_flag = mspi_context_lock(ctx, data->dev_id, xfer, cb, cb_ctx, true); in mspi_pio_transceive()
1046 if (!ctx->xfer.async) { in mspi_pio_transceive()
1048 while (ctx->packets_left > 0) { in mspi_pio_transceive()
1049 packet_idx = ctx->xfer.num_packet - ctx->packets_left; in mspi_pio_transceive()
1050 packet = &ctx->xfer.packets[packet_idx]; in mspi_pio_transceive()
1051 trans.eDirection = packet->dir; in mspi_pio_transceive()
1052 trans.ui16DeviceInstr = (uint16_t)packet->cmd; in mspi_pio_transceive()
1053 trans.ui32DeviceAddr = packet->address; in mspi_pio_transceive()
1054 trans.ui32NumBytes = packet->num_bytes; in mspi_pio_transceive()
1055 trans.pui32Buffer = (uint32_t *)packet->data_buf; in mspi_pio_transceive()
1057 ret = am_hal_mspi_blocking_transfer(data->mspiHandle, &trans, in mspi_pio_transceive()
1059 ctx->packets_left--; in mspi_pio_transceive()
1061 ret = -EIO; in mspi_pio_transceive()
1068 ret = am_hal_mspi_interrupt_enable(data->mspiHandle, AM_HAL_MSPI_INT_DMACMP); in mspi_pio_transceive()
1070 LOG_INST_ERR(cfg->log, "%u, failed to enable interrupt.", __LINE__); in mspi_pio_transceive()
1071 ret = -EHOSTDOWN; in mspi_pio_transceive()
1075 while (ctx->packets_left > 0) { in mspi_pio_transceive()
1076 packet_idx = ctx->xfer.num_packet - ctx->packets_left; in mspi_pio_transceive()
1077 packet = &ctx->xfer.packets[packet_idx]; in mspi_pio_transceive()
1078 trans.eDirection = packet->dir; in mspi_pio_transceive()
1079 trans.ui16DeviceInstr = (uint16_t)packet->cmd; in mspi_pio_transceive()
1080 trans.ui32DeviceAddr = packet->address; in mspi_pio_transceive()
1081 trans.ui32NumBytes = packet->num_bytes; in mspi_pio_transceive()
1082 trans.pui32Buffer = (uint32_t *)packet->data_buf; in mspi_pio_transceive()
1084 if (ctx->callback && packet->cb_mask == MSPI_BUS_XFER_COMPLETE_CB) { in mspi_pio_transceive()
1085 ctx->callback_ctx->mspi_evt.evt_type = MSPI_BUS_XFER_COMPLETE; in mspi_pio_transceive()
1086 ctx->callback_ctx->mspi_evt.evt_data.controller = controller; in mspi_pio_transceive()
1087 ctx->callback_ctx->mspi_evt.evt_data.dev_id = data->ctx.owner; in mspi_pio_transceive()
1088 ctx->callback_ctx->mspi_evt.evt_data.packet = packet; in mspi_pio_transceive()
1089 ctx->callback_ctx->mspi_evt.evt_data.packet_idx = packet_idx; in mspi_pio_transceive()
1090 ctx->callback_ctx->mspi_evt.evt_data.status = ~0; in mspi_pio_transceive()
1095 if (packet->cb_mask == MSPI_BUS_XFER_COMPLETE_CB) { in mspi_pio_transceive()
1096 callback = (am_hal_mspi_callback_t)ctx->callback; in mspi_pio_transceive()
1099 ret = am_hal_mspi_nonblocking_transfer(data->mspiHandle, &trans, MSPI_PIO, in mspi_pio_transceive()
1100 callback, (void *)ctx->callback_ctx); in mspi_pio_transceive()
1101 ctx->packets_left--; in mspi_pio_transceive()
1104 ret = -ENOMEM; in mspi_pio_transceive()
1106 ret = -EIO; in mspi_pio_transceive()
1123 const struct mspi_ambiq_config *cfg = controller->config; in mspi_dma_transceive()
1124 struct mspi_ambiq_data *data = controller->data; in mspi_dma_transceive() local
1125 struct mspi_context *ctx = &data->ctx; in mspi_dma_transceive()
1130 if (xfer->num_packet == 0 || in mspi_dma_transceive()
1131 !xfer->packets || in mspi_dma_transceive()
1132 xfer->timeout > CONFIG_MSPI_COMPLETION_TIMEOUT_TOLERANCE) { in mspi_dma_transceive()
1133 return -EFAULT; in mspi_dma_transceive()
1136 cfg_flag = mspi_context_lock(ctx, data->dev_id, xfer, cb, cb_ctx, true); in mspi_dma_transceive()
1152 ret = am_hal_mspi_interrupt_enable(data->mspiHandle, AM_HAL_MSPI_INT_DMACMP); in mspi_dma_transceive()
1154 LOG_INST_ERR(cfg->log, "%u, failed to enable interrupt.", __LINE__); in mspi_dma_transceive()
1155 ret = -EHOSTDOWN; in mspi_dma_transceive()
1159 while (ctx->packets_left > 0) { in mspi_dma_transceive()
1160 uint32_t packet_idx = ctx->xfer.num_packet - ctx->packets_left; in mspi_dma_transceive()
1163 packet = &ctx->xfer.packets[packet_idx]; in mspi_dma_transceive()
1164 trans.ui8Priority = ctx->xfer.priority; in mspi_dma_transceive()
1165 trans.eDirection = packet->dir; in mspi_dma_transceive()
1166 trans.ui32TransferCount = packet->num_bytes; in mspi_dma_transceive()
1167 trans.ui32DeviceAddress = packet->address; in mspi_dma_transceive()
1168 trans.ui32SRAMAddress = (uint32_t)packet->data_buf; in mspi_dma_transceive()
1172 if (ctx->xfer.async) { in mspi_dma_transceive()
1174 if (ctx->callback && packet->cb_mask == MSPI_BUS_XFER_COMPLETE_CB) { in mspi_dma_transceive()
1175 ctx->callback_ctx->mspi_evt.evt_type = MSPI_BUS_XFER_COMPLETE; in mspi_dma_transceive()
1176 ctx->callback_ctx->mspi_evt.evt_data.controller = controller; in mspi_dma_transceive()
1177 ctx->callback_ctx->mspi_evt.evt_data.dev_id = data->ctx.owner; in mspi_dma_transceive()
1178 ctx->callback_ctx->mspi_evt.evt_data.packet = packet; in mspi_dma_transceive()
1179 ctx->callback_ctx->mspi_evt.evt_data.packet_idx = packet_idx; in mspi_dma_transceive()
1180 ctx->callback_ctx->mspi_evt.evt_data.status = ~0; in mspi_dma_transceive()
1185 if (packet->cb_mask == MSPI_BUS_XFER_COMPLETE_CB) { in mspi_dma_transceive()
1186 callback = (am_hal_mspi_callback_t)ctx->callback; in mspi_dma_transceive()
1189 ret = am_hal_mspi_nonblocking_transfer(data->mspiHandle, &trans, MSPI_DMA, in mspi_dma_transceive()
1190 callback, (void *)ctx->callback_ctx); in mspi_dma_transceive()
1192 ret = am_hal_mspi_nonblocking_transfer(data->mspiHandle, &trans, MSPI_DMA, in mspi_dma_transceive()
1196 ctx->packets_left--; in mspi_dma_transceive()
1199 ret = -ENOMEM; in mspi_dma_transceive()
1201 ret = -EIO; in mspi_dma_transceive()
1207 if (!ctx->xfer.async) { in mspi_dma_transceive()
1208 while (ctx->packets_done < ctx->xfer.num_packet) { in mspi_dma_transceive()
1222 const struct mspi_ambiq_config *cfg = controller->config; in mspi_ambiq_transceive()
1223 struct mspi_ambiq_data *data = controller->data; in mspi_ambiq_transceive() local
1227 if (dev_id != data->dev_id) { in mspi_ambiq_transceive()
1228 LOG_INST_ERR(cfg->log, "%u, dev_id don't match.", __LINE__); in mspi_ambiq_transceive()
1229 return -ESTALE; in mspi_ambiq_transceive()
1232 if (xfer->async) { in mspi_ambiq_transceive()
1233 cb = data->cbs[MSPI_BUS_XFER_COMPLETE]; in mspi_ambiq_transceive()
1234 cb_ctx = data->cb_ctxs[MSPI_BUS_XFER_COMPLETE]; in mspi_ambiq_transceive()
1237 if (xfer->xfer_mode == MSPI_PIO) { in mspi_ambiq_transceive()
1239 } else if (xfer->xfer_mode == MSPI_DMA) { in mspi_ambiq_transceive()
1242 return -EIO; in mspi_ambiq_transceive()
1252 const struct mspi_ambiq_config *cfg = controller->config; in mspi_ambiq_register_callback()
1253 struct mspi_ambiq_data *data = controller->data; in mspi_ambiq_register_callback() local
1256 return -EBUSY; in mspi_ambiq_register_callback()
1259 if (dev_id != data->dev_id) { in mspi_ambiq_register_callback()
1260 LOG_INST_ERR(cfg->log, "%u, dev_id don't match.", __LINE__); in mspi_ambiq_register_callback()
1261 return -ESTALE; in mspi_ambiq_register_callback()
1265 LOG_INST_ERR(cfg->log, "%u, callback types not supported.", __LINE__); in mspi_ambiq_register_callback()
1266 return -ENOTSUP; in mspi_ambiq_register_callback()
1269 data->cbs[evt_type] = cb; in mspi_ambiq_register_callback()
1270 data->cb_ctxs[evt_type] = ctx; in mspi_ambiq_register_callback()
1277 const struct mspi_ambiq_config *cfg = controller->config; in mspi_ambiq_pm_action()
1278 struct mspi_ambiq_data *data = controller->data; in mspi_ambiq_pm_action() local
1282 return -EBUSY; in mspi_ambiq_pm_action()
1287 ret = am_hal_mspi_power_control(data->mspiHandle, AM_HAL_SYSCTRL_WAKE, true); in mspi_ambiq_pm_action()
1289 LOG_INST_ERR(cfg->log, "%u, fail to power on MSPI, code:%d.", __LINE__, in mspi_ambiq_pm_action()
1291 return -EHOSTDOWN; in mspi_ambiq_pm_action()
1296 ret = am_hal_mspi_power_control(data->mspiHandle, AM_HAL_SYSCTRL_DEEPSLEEP, true); in mspi_ambiq_pm_action()
1298 LOG_INST_ERR(cfg->log, "%u, fail to power off MSPI, code:%d.", __LINE__, in mspi_ambiq_pm_action()
1300 return -EHOSTDOWN; in mspi_ambiq_pm_action()
1305 return -ENOTSUP; in mspi_ambiq_pm_action()
1314 const struct mspi_ambiq_config *cfg = controller->config; in mspi_ambiq_init()
1317 .config = cfg->mspicfg, in mspi_ambiq_init()
1323 static DEVICE_API(mspi, mspi_ambiq_driver_api) = {
1359 .channel_num = (DT_INST_REG_ADDR(n) - MSPI0_BASE) / \
1396 LOG_INSTANCE_REGISTER(DT_DRV_INST(n), mspi##n, CONFIG_MSPI_LOG_LEVEL); \
1431 LOG_INSTANCE_PTR_INIT(log, DT_DRV_INST(n), mspi##n) \