Lines Matching +full:iso +full:- +full:out +full:- +full:mps

4  * SPDX-License-Identifier: Apache-2.0
39 #define REG_GET_BIT(reg, field) (USB->reg & USB_##reg##_##field##_Msk)
40 #define REG_SET_BIT(reg, field) (USB->reg |= USB_##reg##_##field##_Msk)
41 #define REG_CLR_BIT(reg, field) (USB->reg &= ~USB_##reg##_##field##_Msk)
43 (USB->reg = (USB->reg & ~USB_##reg##_##field##_Msk) | (val << USB_##reg##_##field##_Pos))
69 * Those two following states are added to allow going out of sleep mode
93 * after ACK is received or when filling ISO endpoint with size larger then
97 uint8_t iso: 1; /** ISO endpoint */ member
123 #define EP0_OUT_STATE(data) (&data->ep_state[0][0])
124 #define EP0_IN_STATE(data) (&data->ep_state[1][0])
128 const struct udc_smartbond_config *config = data->dev->config; in usb_smartbond_dma_config()
129 const struct usb_smartbond_dma_config *dma_cfg = &config->dma_cfg; in usb_smartbond_dma_config()
130 struct dma_config *tx = &data->dma_data.tx_cfg; in usb_smartbond_dma_config()
131 struct dma_config *rx = &data->dma_data.rx_cfg; in usb_smartbond_dma_config()
132 struct dma_block_config *tx_block = &data->dma_data.tx_block_cfg; in usb_smartbond_dma_config()
133 struct dma_block_config *rx_block = &data->dma_data.rx_block_cfg; in usb_smartbond_dma_config()
135 if (dma_request_channel(dma_cfg->rx_dev, (void *)&dma_cfg->rx_chan) < 0) { in usb_smartbond_dma_config()
137 return -EIO; in usb_smartbond_dma_config()
140 if (dma_request_channel(dma_cfg->tx_dev, (void *)&dma_cfg->tx_chan) < 0) { in usb_smartbond_dma_config()
142 return -EIO; in usb_smartbond_dma_config()
145 tx->channel_direction = MEMORY_TO_PERIPHERAL; in usb_smartbond_dma_config()
146 tx->dma_callback = NULL; in usb_smartbond_dma_config()
147 tx->user_data = NULL; in usb_smartbond_dma_config()
148 tx->block_count = 1; in usb_smartbond_dma_config()
149 tx->head_block = tx_block; in usb_smartbond_dma_config()
151 tx->error_callback_dis = 1; in usb_smartbond_dma_config()
153 tx->complete_callback_en = 1; in usb_smartbond_dma_config()
155 tx->dma_slot = dma_cfg->tx_slot_mux; in usb_smartbond_dma_config()
156 tx->channel_priority = 7; in usb_smartbond_dma_config()
159 tx->source_burst_length = 1; in usb_smartbond_dma_config()
160 tx->dest_burst_length = 1; in usb_smartbond_dma_config()
161 /* USB is byte-oriented protocol */ in usb_smartbond_dma_config()
162 tx->source_data_size = 1; in usb_smartbond_dma_config()
163 tx->dest_data_size = 1; in usb_smartbond_dma_config()
166 tx_block->dest_addr_adj = 0x2; in usb_smartbond_dma_config()
168 tx_block->source_addr_adj = 0x0; in usb_smartbond_dma_config()
171 tx_block->source_address = 0; in usb_smartbond_dma_config()
173 tx_block->dest_address = MCU_SYSRAM_M_BASE; in usb_smartbond_dma_config()
175 tx_block->block_size = 0; in usb_smartbond_dma_config()
177 rx->channel_direction = PERIPHERAL_TO_MEMORY; in usb_smartbond_dma_config()
178 rx->dma_callback = NULL; in usb_smartbond_dma_config()
179 rx->user_data = NULL; in usb_smartbond_dma_config()
180 rx->block_count = 1; in usb_smartbond_dma_config()
181 rx->head_block = rx_block; in usb_smartbond_dma_config()
183 rx->error_callback_dis = 1; in usb_smartbond_dma_config()
185 rx->complete_callback_en = 1; in usb_smartbond_dma_config()
187 rx->dma_slot = dma_cfg->rx_slot_mux; in usb_smartbond_dma_config()
188 rx->channel_priority = 2; in usb_smartbond_dma_config()
191 rx->source_burst_length = 1; in usb_smartbond_dma_config()
192 rx->dest_burst_length = 1; in usb_smartbond_dma_config()
193 /* USB is byte-oriented protocol */ in usb_smartbond_dma_config()
194 rx->source_data_size = 1; in usb_smartbond_dma_config()
195 rx->dest_data_size = 1; in usb_smartbond_dma_config()
198 rx_block->source_addr_adj = 0x2; in usb_smartbond_dma_config()
200 rx_block->dest_addr_adj = 0x0; in usb_smartbond_dma_config()
203 rx_block->source_address = 0; in usb_smartbond_dma_config()
205 rx_block->dest_address = MCU_SYSRAM_M_BASE; in usb_smartbond_dma_config()
207 rx_block->block_size = 0; in usb_smartbond_dma_config()
209 if (dma_config(dma_cfg->rx_dev, dma_cfg->rx_chan, rx) < 0) { in usb_smartbond_dma_config()
211 return -EINVAL; in usb_smartbond_dma_config()
214 if (dma_config(dma_cfg->tx_dev, dma_cfg->tx_chan, tx) < 0) { in usb_smartbond_dma_config()
216 return -EINVAL; in usb_smartbond_dma_config()
224 const struct udc_smartbond_config *config = data->dev->config; in usb_smartbond_dma_deconfig()
225 const struct usb_smartbond_dma_config *dma_cfg = &config->dma_cfg; in usb_smartbond_dma_deconfig()
227 dma_stop(dma_cfg->tx_dev, dma_cfg->tx_chan); in usb_smartbond_dma_deconfig()
228 dma_stop(dma_cfg->rx_dev, dma_cfg->rx_chan); in usb_smartbond_dma_deconfig()
230 dma_release_channel(dma_cfg->tx_dev, dma_cfg->tx_chan); in usb_smartbond_dma_deconfig()
231 dma_release_channel(dma_cfg->rx_dev, dma_cfg->rx_chan); in usb_smartbond_dma_deconfig()
236 const struct udc_smartbond_config *config = data->dev->config; in usb_dc_get_ep_state()
241 return (ep_idx < config->num_of_eps) ? &data->ep_state[ep_dir][ep_idx] : NULL; in usb_dc_get_ep_state()
247 const struct udc_smartbond_config *config = data->dev->config; in usb_dc_get_ep_out_state()
249 return ep_idx < config->num_of_eps ? &data->ep_state[0][ep_idx] : NULL; in usb_dc_get_ep_out_state()
255 const struct udc_smartbond_config *config = data->dev->config; in usb_dc_get_ep_in_state()
257 return ep_idx < config->num_of_eps ? &data->ep_state[1][ep_idx] : NULL; in usb_dc_get_ep_in_state()
262 data->nfsr = val; in set_nfsr()
268 USB->USB_NFSR_REG = val & 3; in set_nfsr()
275 struct smartbond_ep_reg_set *regs = ep_state->regs; in fill_tx_fifo()
276 struct net_buf *buf = ep_state->buf; in fill_tx_fifo()
277 const struct udc_ep_config *const ep_cfg = &ep_state->config; in fill_tx_fifo()
278 const uint16_t mps = udc_mps_ep_size(ep_cfg); in fill_tx_fifo() local
279 const uint8_t ep_idx = USB_EP_GET_IDX(ep_cfg->addr); in fill_tx_fifo()
281 src = buf->data; in fill_tx_fifo()
282 remaining = buf->len; in fill_tx_fifo()
283 if (remaining > mps - ep_state->last_packet_size) { in fill_tx_fifo()
284 remaining = mps - ep_state->last_packet_size; in fill_tx_fifo()
291 while ((regs->txs & USB_USB_TXS1_REG_USB_TCOUNT_Msk) > 0 && remaining > 0) { in fill_tx_fifo()
292 regs->txd = *src++; in fill_tx_fifo()
293 ep_state->last_packet_size++; in fill_tx_fifo()
294 remaining--; in fill_tx_fifo()
299 * in FIFO at once. This case only applies to ISO endpoints with packet in fill_tx_fifo()
303 if (ep_idx == 0 || ep_cfg->mps <= EP_FIFO_SIZE) { in fill_tx_fifo()
312 regs->txc |= (3 << USB_USB_TXC1_REG_USB_TFWL_Pos); in fill_tx_fifo()
313 USB->USB_FWMSK_REG |= BIT(ep_idx - 1 + USB_USB_FWMSK_REG_USB_M_TXWARN31_Pos); in fill_tx_fifo()
315 regs->txc &= ~USB_USB_TXC1_REG_USB_TFWL_Msk; in fill_tx_fifo()
316 USB->USB_FWMSK_REG &= ~(BIT(ep_idx - 1 + USB_USB_FWMSK_REG_USB_M_TXWARN31_Pos)); in fill_tx_fifo()
320 regs->txc |= USB_USB_TXC1_REG_USB_LAST_Msk; in fill_tx_fifo()
326 struct udc_ep_config *const ep_cfg = &ep_state->config; in try_allocate_dma()
327 const uint8_t ep = ep_cfg->addr; in try_allocate_dma()
331 if (atomic_ptr_cas(&data->dma_ep[dir_ix], NULL, ep_state)) { in try_allocate_dma()
333 USB->USB_DMA_CTRL_REG = in try_allocate_dma()
334 (USB->USB_DMA_CTRL_REG & ~USB_USB_DMA_CTRL_REG_USB_DMA_RX_Msk) | in try_allocate_dma()
335 ((ep_idx - 1) << USB_USB_DMA_CTRL_REG_USB_DMA_RX_Pos); in try_allocate_dma()
337 USB->USB_DMA_CTRL_REG = in try_allocate_dma()
338 (USB->USB_DMA_CTRL_REG & ~USB_USB_DMA_CTRL_REG_USB_DMA_TX_Msk) | in try_allocate_dma()
339 ((ep_idx - 1) << USB_USB_DMA_CTRL_REG_USB_DMA_TX_Pos); in try_allocate_dma()
341 USB->USB_DMA_CTRL_REG |= USB_USB_DMA_CTRL_REG_USB_DMA_EN_Msk; in try_allocate_dma()
351 if (dma_reload(dma_cfg->rx_dev, dma_cfg->rx_chan, src, dst, size) < 0) { in start_rx_dma()
354 dma_start(dma_cfg->rx_dev, dma_cfg->rx_chan); in start_rx_dma()
360 struct udc_ep_config *const ep_cfg = &ep_state->config; in start_rx_packet()
361 const struct udc_smartbond_config *config = data->dev->config; in start_rx_packet()
362 const uint8_t ep = ep_cfg->addr; in start_rx_packet()
363 struct smartbond_ep_reg_set *regs = ep_state->regs; in start_rx_packet()
364 struct net_buf *buf = ep_state->buf; in start_rx_packet()
366 const uint16_t mps = udc_mps_ep_size(ep_cfg); in start_rx_packet() local
367 uint8_t rxc = regs->rxc | USB_USB_RXC1_REG_USB_RX_EN_Msk; in start_rx_packet()
371 ep_state->last_packet_size = 0; in start_rx_packet()
373 if (mps > config->dma_min_transfer_size) { in start_rx_packet()
375 start_rx_dma(&config->dma_cfg, (uintptr_t)&regs->rxd, in start_rx_packet()
376 (uintptr_t)net_buf_tail(buf), mps); in start_rx_packet()
377 } else if (mps > EP_FIFO_SIZE) { in start_rx_packet()
386 USB->USB_FWMSK_REG |= in start_rx_packet()
387 BIT(ep_idx - 1 + USB_USB_FWMSK_REG_USB_M_RXWARN31_Pos); in start_rx_packet()
394 USB->USB_FWMSK_REG &= ~(BIT(ep_idx - 1 + USB_USB_FWMSK_REG_USB_M_RXWARN31_Pos)); in start_rx_packet()
397 regs->rxc = rxc; in start_rx_packet()
403 if (dma_reload(dma_cfg->tx_dev, dma_cfg->tx_chan, src, dst, size) < 0) { in start_tx_dma()
406 dma_start(dma_cfg->tx_dev, dma_cfg->tx_chan); in start_tx_dma()
412 const struct udc_smartbond_config *config = data->dev->config; in start_tx_packet()
413 struct smartbond_ep_reg_set *regs = ep_state->regs; in start_tx_packet()
414 struct udc_ep_config *const ep_cfg = &ep_state->config; in start_tx_packet()
415 struct net_buf *buf = ep_state->buf; in start_tx_packet()
416 const uint8_t ep = ep_cfg->addr; in start_tx_packet()
417 uint16_t remaining = buf->len; in start_tx_packet()
418 const uint16_t mps = udc_mps_ep_size(ep_cfg); in start_tx_packet() local
419 uint16_t size = MIN(remaining, mps); in start_tx_packet()
424 ep_state->last_packet_size = 0; in start_tx_packet()
426 regs->txc = USB_USB_TXC1_REG_USB_FLUSH_Msk; in start_tx_packet()
429 if (ep_cfg->stat.data1) { in start_tx_packet()
433 if (ep != USB_CONTROL_EP_IN && size > config->dma_min_transfer_size && in start_tx_packet()
434 (uint32_t)(buf->data) >= CONFIG_SRAM_BASE_ADDRESS && try_allocate_dma(data, ep_state)) { in start_tx_packet()
435 start_tx_dma(&config->dma_cfg, (uintptr_t)buf->data, (uintptr_t)&regs->txd, size); in start_tx_packet()
440 regs->txc = txc; in start_tx_packet()
442 (void)USB->USB_EP0_NAK_REG; in start_tx_packet()
456 struct smartbond_ep_reg_set *regs = ep_state->regs; in read_rx_fifo()
457 struct udc_ep_config *const ep_cfg = &ep_state->config; in read_rx_fifo()
458 const uint16_t mps = udc_mps_ep_size(ep_cfg); in read_rx_fifo() local
459 uint16_t remaining = mps - ep_state->last_packet_size; in read_rx_fifo()
467 dst[i] = regs->rxd; in read_rx_fifo()
470 ep_state->last_packet_size += receive_this_time; in read_rx_fifo()
472 return bytes_in_fifo - receive_this_time; in read_rx_fifo()
478 uint32_t rxs0 = USB->USB_RXS0_REG; in handle_ep0_rx()
480 struct udc_ep_config *ep0_out_config = &ep0_out_state->config; in handle_ep0_rx()
483 struct net_buf *buf = ep0_out_state->buf; in handle_ep0_rx()
489 ep0_in_config = &ep0_in_state->config; in handle_ep0_rx()
490 ep0_out_state->last_packet_size = 0; in handle_ep0_rx()
491 read_rx_fifo(ep0_out_state, data->setup_buffer, EP0_FIFO_SIZE); in handle_ep0_rx()
493 ep0_out_config->stat.halted = 0; in handle_ep0_rx()
494 ep0_out_config->stat.data1 = 1; in handle_ep0_rx()
495 ep0_in_config->stat.halted = 0; in handle_ep0_rx()
496 ep0_in_config->stat.data1 = 1; in handle_ep0_rx()
499 LOG_HEXDUMP_DBG(data->setup_buffer, 8, "setup"); in handle_ep0_rx()
502 (void)USB->USB_EP0_NAK_REG; in handle_ep0_rx()
503 k_work_submit_to_queue(udc_get_work_q(), &data->ep0_setup_work); in handle_ep0_rx()
505 (void)USB->USB_EP0_NAK_REG; in handle_ep0_rx()
506 if (GET_BIT(rxs0, USB_USB_RXS0_REG_USB_TOGGLE_RX0) != ep0_out_config->stat.data1) { in handle_ep0_rx()
509 ep0_out_state->last_packet_size = 0; in handle_ep0_rx()
513 net_buf_tail(buf) + ep0_out_state->last_packet_size, in handle_ep0_rx()
516 ep0_out_config->stat.data1 ^= 1; in handle_ep0_rx()
517 net_buf_add(ep0_out_state->buf, ep0_out_state->last_packet_size); in handle_ep0_rx()
518 if (ep0_out_state->last_packet_size < EP0_FIFO_SIZE || in handle_ep0_rx()
519 ep0_out_state->buf->len == 0) { in handle_ep0_rx()
521 &data->ep0_rx_work); in handle_ep0_rx()
534 const struct udc_smartbond_config *config = data->dev->config; in udc_smartbond_ep_abort()
537 if (data->dma_ep[0] == ep_state) { in udc_smartbond_ep_abort()
538 dma_stop(config->dma_cfg.rx_dev, config->dma_cfg.rx_chan); in udc_smartbond_ep_abort()
539 data->dma_ep[0] = NULL; in udc_smartbond_ep_abort()
540 } else if (data->dma_ep[1] == ep_state) { in udc_smartbond_ep_abort()
541 dma_stop(config->dma_cfg.tx_dev, config->dma_cfg.tx_chan); in udc_smartbond_ep_abort()
542 data->dma_ep[1] = NULL; in udc_smartbond_ep_abort()
545 if (USB_EP_DIR_IS_OUT(ep_cfg->addr)) { in udc_smartbond_ep_abort()
546 ep_state->regs->rxc |= USB_USB_RXC0_REG_USB_FLUSH_Msk; in udc_smartbond_ep_abort()
547 ep_state->regs->rxc &= ~USB_USB_RXC0_REG_USB_FLUSH_Msk; in udc_smartbond_ep_abort()
549 ep_state->regs->txc |= USB_USB_TXC0_REG_USB_FLUSH_Msk; in udc_smartbond_ep_abort()
550 ep_state->regs->txc &= ~USB_USB_TXC0_REG_USB_FLUSH_Msk; in udc_smartbond_ep_abort()
556 struct usb_smartbond_data *data = dev->data; in udc_smartbond_ep_tx()
561 (ep_state->regs->epc_in & USB_USB_EPC1_REG_USB_STALL_Msk) != 0) { in udc_smartbond_ep_tx()
566 LOG_DBG("TX ep 0x%02x len %u", ep, buf ? buf->len : -1); in udc_smartbond_ep_tx()
569 ep_state->buf = buf; in udc_smartbond_ep_tx()
570 ep_state->last_packet_size = 0; in udc_smartbond_ep_tx()
582 struct usb_smartbond_data *data = dev->data; in udc_smartbond_ep_rx()
593 LOG_DBG("RX ep 0x%02x len %u", ep, buf->size); in udc_smartbond_ep_rx()
595 ep_state->last_packet_size = 0; in udc_smartbond_ep_rx()
596 ep_state->buf = buf; in udc_smartbond_ep_rx()
610 const uint8_t ep = ep_cfg->addr; in udc_smartbond_ep_enqueue()
616 if (ep_cfg->stat.halted) { in udc_smartbond_ep_enqueue()
619 * you need to make sure that transfers are re-triggered when in udc_smartbond_ep_enqueue()
641 const uint8_t ep = ep_cfg->addr; in udc_smartbond_ep_dequeue()
653 udc_submit_ep_event(dev, buf, -ECONNABORTED); in udc_smartbond_ep_dequeue()
665 const uint8_t ep = ep_cfg->addr; in udc_smartbond_ep_enable()
667 bool iso = (ep_cfg->attributes & USB_EP_TRANSFER_TYPE_MASK) == USB_EP_TYPE_ISO; in udc_smartbond_ep_enable() local
676 ep_state->regs->txc &= ~USB_USB_TXC0_REG_USB_IGN_IN_Msk; in udc_smartbond_ep_enable()
678 ep_state->regs->epc_in |= USB_USB_EPC1_REG_USB_EP_EN_Msk | in udc_smartbond_ep_enable()
680 (iso ? USB_USB_EPC2_REG_USB_ISO_Msk : 0); in udc_smartbond_ep_enable()
681 USB->USB_TXMSK_REG |= 0x11 << (ep_idx - 1); in udc_smartbond_ep_enable()
685 ep_state->regs->rxc &= ~USB_USB_RXC0_REG_USB_IGN_OUT_Msk; in udc_smartbond_ep_enable()
687 ep_state->regs->rxc &= ~USB_USB_RXC0_REG_USB_IGN_SETUP_Msk; in udc_smartbond_ep_enable()
689 ep_state->regs->epc_out = USB_USB_EPC2_REG_USB_EP_EN_Msk | in udc_smartbond_ep_enable()
691 (iso ? USB_USB_EPC2_REG_USB_ISO_Msk : 0); in udc_smartbond_ep_enable()
692 USB->USB_RXMSK_REG |= 0x11 << (ep_idx - 1); in udc_smartbond_ep_enable()
703 const uint8_t ep = ep_cfg->addr; in udc_smartbond_ep_disable()
710 ep_state->regs->txc = in udc_smartbond_ep_disable()
713 ep_state->regs->rxc = in udc_smartbond_ep_disable()
724 const uint8_t ep = ep_cfg->addr; in udc_smartbond_ep_set_halt()
728 ep_cfg->stat.halted = 1; in udc_smartbond_ep_set_halt()
729 if (ep_cfg->addr == USB_CONTROL_EP_IN) { in udc_smartbond_ep_set_halt()
730 /* Stall in DATA IN phase, drop status OUT packet */ in udc_smartbond_ep_set_halt()
737 USB->USB_RXC0_REG = USB_USB_RXC0_REG_USB_FLUSH_Msk; in udc_smartbond_ep_set_halt()
738 USB->USB_EPC0_REG |= USB_USB_EPC0_REG_USB_STALL_Msk; in udc_smartbond_ep_set_halt()
739 USB->USB_TXC0_REG |= USB_USB_TXC0_REG_USB_TX_EN_Msk; in udc_smartbond_ep_set_halt()
741 ep_state->regs->rxc |= USB_USB_RXC0_REG_USB_RX_EN_Msk; in udc_smartbond_ep_set_halt()
742 ep_state->regs->epc_in |= USB_USB_EPC0_REG_USB_STALL_Msk; in udc_smartbond_ep_set_halt()
744 ep_state->regs->epc_out = USB_USB_EPC1_REG_USB_STALL_Msk; in udc_smartbond_ep_set_halt()
745 ep_state->regs->rxc = USB_USB_RXC1_REG_USB_RX_EN_Msk; in udc_smartbond_ep_set_halt()
747 ep_state->regs->epc_in |= USB_USB_EPC1_REG_USB_STALL_Msk; in udc_smartbond_ep_set_halt()
748 ep_state->regs->txc = in udc_smartbond_ep_set_halt()
757 const uint8_t ep = ep_cfg->addr; in udc_smartbond_ep_clear_halt()
762 ep_cfg->stat.data1 = 0; in udc_smartbond_ep_clear_halt()
763 ep_cfg->stat.halted = 0; in udc_smartbond_ep_clear_halt()
770 ep_state->regs->epc_out &= ~USB_USB_EPC1_REG_USB_STALL_Msk; in udc_smartbond_ep_clear_halt()
773 ep_state->regs->epc_in &= ~USB_USB_EPC1_REG_USB_STALL_Msk; in udc_smartbond_ep_clear_halt()
786 USB->USB_FAR_REG = (addr & USB_USB_FAR_REG_USB_AD_Msk) | USB_USB_FAR_REG_USB_AD_EN_Msk; in udc_smartbond_set_address()
797 if (data->nfsr == NFSR_NODE_SUSPEND) { in udc_smartbond_host_wakeup()
803 USB->USB_MAMSK_REG |= USB_USB_MAMSK_REG_USB_M_FRAME_Msk; in udc_smartbond_host_wakeup()
820 return -EIO; in udc_smartbond_shutdown()
825 return -EIO; in udc_smartbond_shutdown()
833 if (data->nfsr == NFSR_NODE_RESET) { in check_reset_end()
841 * Keep non-reset bits combined from two previous in check_reset_end()
844 alt_ev = (alt_ev & ~USB_USB_ALTEV_REG_USB_RESET_Msk) | USB->USB_ALTEV_REG; in check_reset_end()
848 USB->USB_ALTMSK_REG = in check_reset_end()
850 if (data->ep_state[0][0].buf != NULL) { in check_reset_end()
851 USB->USB_MAMSK_REG |= USB_USB_MAMSK_REG_USB_M_EP0_RX_Msk; in check_reset_end()
853 LOG_DBG("Set operational %02x", USB->USB_MAMSK_REG); in check_reset_end()
865 struct smartbond_ep_reg_set *regs = ep0_in_state->regs; in handle_ep0_tx()
866 struct udc_ep_config *ep0_in_config = &ep0_in_state->config; in handle_ep0_tx()
867 struct net_buf *buf = ep0_in_state->buf; in handle_ep0_tx()
870 txs0 = regs->txs; in handle_ep0_tx()
877 net_buf_pull(buf, ep0_in_state->last_packet_size); in handle_ep0_tx()
878 ep0_in_state->last_packet_size = 0; in handle_ep0_tx()
879 ep0_in_config->stat.data1 ^= 1; in handle_ep0_tx()
880 REG_SET_VAL(USB_TXC0_REG, USB_TOGGLE_TX0, ep0_in_config->stat.data1); in handle_ep0_tx()
883 * Packet was sent to host but host already sent OUT packet in handle_ep0_tx()
886 if (USB->USB_EP0_NAK_REG & USB_USB_EP0_NAK_REG_USB_EP0_OUTNAK_Msk) { in handle_ep0_tx()
887 net_buf_pull(buf, buf->len); in handle_ep0_tx()
890 if (buf->len == 0) { in handle_ep0_tx()
897 USB->USB_TXC0_REG |= USB_USB_TXC0_REG_USB_TX_EN_Msk; in handle_ep0_tx()
901 &data->ep0_tx_work); in handle_ep0_tx()
906 ep0_in_state->last_packet_size = 0; in handle_ep0_tx()
919 const struct udc_smartbond_config *config = data->dev->config; in handle_epx_rx_ev()
920 struct smartbond_ep_reg_set *regs = ep_state->regs; in handle_epx_rx_ev()
921 struct udc_ep_config *const ep_cfg = &ep_state->config; in handle_epx_rx_ev()
922 struct net_buf *buf = ep_state->buf; in handle_epx_rx_ev()
925 rxs = regs->rxs; in handle_epx_rx_ev()
928 regs->rxc |= USB_USB_RXC1_REG_USB_FLUSH_Msk; in handle_epx_rx_ev()
929 ep_state->last_packet_size = 0; in handle_epx_rx_ev()
930 if (data->dma_ep[0] == ep_state) { in handle_epx_rx_ev()
932 dma_stop(config->dma_cfg.rx_dev, config->dma_cfg.rx_chan); in handle_epx_rx_ev()
936 dma_start(config->dma_cfg.rx_dev, config->dma_cfg.rx_chan); in handle_epx_rx_ev()
941 if (data->dma_ep[0] == ep_state) { in handle_epx_rx_ev()
944 dma_get_status(config->dma_cfg.rx_dev, config->dma_cfg.rx_chan, in handle_epx_rx_ev()
950 dma_stop(config->dma_cfg.rx_dev, config->dma_cfg.rx_chan); in handle_epx_rx_ev()
951 ep_state->last_packet_size = rx_dma_status.total_copied; in handle_epx_rx_ev()
954 * When DMA did not finished (packet was smaller then MPS), in handle_epx_rx_ev()
959 if (ep_state->last_packet_size == in handle_epx_rx_ev()
961 ep_state->last_packet_size++; in handle_epx_rx_ev()
964 data->dma_ep[0] = NULL; in handle_epx_rx_ev()
974 net_buf_tail(buf) + ep_state->last_packet_size, in handle_epx_rx_ev()
979 if (!ep_state->iso && in handle_epx_rx_ev()
980 GET_BIT(rxs, USB_USB_RXS1_REG_USB_TOGGLE_RX) != ep_cfg->stat.data1) { in handle_epx_rx_ev()
982 regs->rxc |= USB_USB_RXC1_REG_USB_FLUSH_Msk; in handle_epx_rx_ev()
983 ep_state->last_packet_size = 0; in handle_epx_rx_ev()
985 /* Re-enable reception */ in handle_epx_rx_ev()
988 ep_cfg->stat.data1 ^= 1; in handle_epx_rx_ev()
989 REG_SET_VAL(USB_TXC0_REG, USB_TOGGLE_TX0, ep_cfg->stat.data1); in handle_epx_rx_ev()
990 net_buf_add(buf, ep_state->last_packet_size); in handle_epx_rx_ev()
993 ep_state->last_packet_size < udc_mps_ep_size(ep_cfg) || in handle_epx_rx_ev()
994 ep_state->iso) { in handle_epx_rx_ev()
995 buf = udc_buf_get(data->dev, ep_cfg->addr); in handle_epx_rx_ev()
997 LOG_ERR("ep 0x%02x queue is empty", ep_cfg->addr); in handle_epx_rx_ev()
1000 ep_cfg->stat.busy = 0; in handle_epx_rx_ev()
1001 udc_submit_ep_event(data->dev, buf, 0); in handle_epx_rx_ev()
1007 } while (fifo_bytes > config->fifo_read_threshold); in handle_epx_rx_ev()
1012 if (USB->USB_RXEV_REG & BIT(0)) { in handle_rx_ev()
1016 if (USB->USB_RXEV_REG & BIT(1)) { in handle_rx_ev()
1020 if (USB->USB_RXEV_REG & BIT(2)) { in handle_rx_ev()
1028 const struct udc_smartbond_config *config = data->dev->config; in handle_epx_tx_ev()
1029 struct smartbond_ep_reg_set *regs = ep_state->regs; in handle_epx_tx_ev()
1030 struct udc_ep_config *const ep_cfg = &ep_state->config; in handle_epx_tx_ev()
1031 struct net_buf *buf = ep_state->buf; in handle_epx_tx_ev()
1032 const uint8_t ep = ep_cfg->addr; in handle_epx_tx_ev()
1034 txs = regs->txs; in handle_epx_tx_ev()
1037 if (data->dma_ep[1] == ep_state) { in handle_epx_tx_ev()
1040 dma_get_status(config->dma_cfg.tx_dev, config->dma_cfg.tx_chan, in handle_epx_tx_ev()
1046 dma_stop(config->dma_cfg.tx_dev, config->dma_cfg.tx_chan); in handle_epx_tx_ev()
1047 ep_state->last_packet_size = tx_dma_status.total_copied + 1; in handle_epx_tx_ev()
1049 data->dma_ep[1] = NULL; in handle_epx_tx_ev()
1054 net_buf_pull(buf, ep_state->last_packet_size); in handle_epx_tx_ev()
1055 ep_state->last_packet_size = 0; in handle_epx_tx_ev()
1056 ep_cfg->stat.data1 ^= 1; in handle_epx_tx_ev()
1057 REG_SET_VAL(USB_TXC0_REG, USB_TOGGLE_TX0, ep_cfg->stat.data1); in handle_epx_tx_ev()
1059 if (buf->len == 0) { in handle_epx_tx_ev()
1063 regs->txc |= USB_USB_TXC1_REG_USB_TX_EN_Msk | in handle_epx_tx_ev()
1066 udc_ep_set_busy(data->dev, ep, false); in handle_epx_tx_ev()
1067 buf = udc_buf_get(data->dev, ep); in handle_epx_tx_ev()
1069 udc_submit_ep_event(data->dev, buf, 0); in handle_epx_tx_ev()
1070 udc_smartbond_ep_tx(data->dev, ep); in handle_epx_tx_ev()
1074 } else if (regs->epc_in & USB_USB_EPC1_REG_USB_STALL_Msk) { in handle_epx_tx_ev()
1085 LOG_DBG("EP 0x%02x FIFO under-run\n", ep); in handle_epx_tx_ev()
1093 if (USB->USB_TXEV_REG & BIT(0)) { in handle_tx_ev()
1096 if (USB->USB_TXEV_REG & BIT(1)) { in handle_tx_ev()
1099 if (USB->USB_TXEV_REG & BIT(2)) { in handle_tx_ev()
1111 uint32_t fifo_warning = USB->USB_FWEV_REG; in handle_fifo_warning()
1140 uint32_t ep0_nak = USB->USB_EP0_NAK_REG; in handle_ep0_nak()
1159 (void)USB->USB_EP0_NAK_REG; in handle_ep0_nak()
1160 k_work_submit_to_queue(udc_get_work_q(), &data->ep0_tx_work); in handle_ep0_nak()
1181 const struct udc_smartbond_config *config = data->dev->config; in handle_bus_reset()
1184 USB->USB_NFSR_REG = 0; in handle_bus_reset()
1185 USB->USB_FAR_REG = 0x80; in handle_bus_reset()
1186 USB->USB_ALTMSK_REG = 0; in handle_bus_reset()
1187 USB->USB_NFSR_REG = NFSR_NODE_RESET; in handle_bus_reset()
1188 USB->USB_TXMSK_REG = 0; in handle_bus_reset()
1189 USB->USB_RXMSK_REG = 0; in handle_bus_reset()
1192 for (int i = 0; i < config->num_of_eps; ++i) { in handle_bus_reset()
1193 data->ep_state[1][i].buf = NULL; in handle_bus_reset()
1194 data->ep_state[1][i].config.stat.busy = 0; in handle_bus_reset()
1198 udc_submit_event(data->dev, UDC_EVT_RESET, 0); in handle_bus_reset()
1199 USB->USB_DMA_CTRL_REG = 0; in handle_bus_reset()
1201 USB->USB_MAMSK_REG = USB_USB_MAMSK_REG_USB_M_INTR_Msk | USB_USB_MAMSK_REG_USB_M_FRAME_Msk | in handle_bus_reset()
1205 USB->USB_ALTMSK_REG = USB_USB_ALTMSK_REG_USB_M_RESUME_Msk; in handle_bus_reset()
1206 alt_ev = USB->USB_ALTEV_REG; in handle_bus_reset()
1208 empty_ep0_queues(data->dev); in handle_bus_reset()
1213 if (atomic_cas(&data->clk_requested, 0, 1)) { in usb_clock_on()
1221 if (atomic_cas(&data->clk_requested, 1, 0)) { in usb_clock_off()
1229 const struct udc_smartbond_config *config = data->dev->config; in handle_alt_ev()
1231 uint32_t alt_ev = USB->USB_ALTEV_REG; in handle_alt_ev()
1233 if (USB->USB_NFSR_REG == NFSR_NODE_SUSPEND) { in handle_alt_ev()
1237 if (GET_BIT(alt_ev, USB_USB_ALTEV_REG_USB_RESET) && data->nfsr != NFSR_NODE_RESET) { in handle_alt_ev()
1240 if (USB->USB_NFSR_REG == NFSR_NODE_SUSPEND) { in handle_alt_ev()
1242 if (data->ep_state[0][0].buf != NULL) { in handle_alt_ev()
1243 USB->USB_MAMSK_REG |= USB_USB_MAMSK_REG_USB_M_EP0_RX_Msk; in handle_alt_ev()
1245 USB->USB_ALTMSK_REG = USB_USB_ALTMSK_REG_USB_M_RESET_Msk | in handle_alt_ev()
1247 /* Re-enable reception of endpoint with pending transfer */ in handle_alt_ev()
1248 for (int ep_idx = 1; ep_idx < config->num_of_eps; ++ep_idx) { in handle_alt_ev()
1250 if (!ep_state->config.stat.halted) { in handle_alt_ev()
1254 udc_submit_event(data->dev, UDC_EVT_RESUME, 0); in handle_alt_ev()
1258 USB->USB_ALTMSK_REG = in handle_alt_ev()
1261 udc_submit_event(data->dev, UDC_EVT_SUSPEND, 0); in handle_alt_ev()
1268 uint32_t int_status = USB->USB_MAEV_REG & USB->USB_MAMSK_REG; in udc_smartbond_isr()
1276 (void)USB->USB_CHARGER_STAT_REG; in udc_smartbond_isr()
1296 (void)USB->USB_NAKEV_REG; in udc_smartbond_isr()
1300 if (data->nfsr == NFSR_NODE_RESET) { in udc_smartbond_isr()
1308 } else if (data->nfsr == NFSR_NODE_WAKING) { in udc_smartbond_isr()
1310 data->nfsr = NFSR_NODE_WAKING2; in udc_smartbond_isr()
1311 } else if (data->nfsr == NFSR_NODE_WAKING2) { in udc_smartbond_isr()
1313 data->nfsr = NFSR_NODE_RESUME; in udc_smartbond_isr()
1314 LOG_DBG("data->nfsr = NFSR_NODE_RESUME %02x", USB->USB_MAMSK_REG); in udc_smartbond_isr()
1315 } else if (data->nfsr == NFSR_NODE_RESUME) { in udc_smartbond_isr()
1317 if (data->ep_state[0][0].buf != NULL) { in udc_smartbond_isr()
1318 USB->USB_MAMSK_REG |= USB_USB_MAMSK_REG_USB_M_EP0_RX_Msk; in udc_smartbond_isr()
1320 LOG_DBG("Set operational %02x", USB->USB_MAMSK_REG); in udc_smartbond_isr()
1322 USB->USB_MAMSK_REG &= ~USB_USB_MAMSK_REG_USB_M_FRAME_Msk; in udc_smartbond_isr()
1345 if (data->attached == attached && data->vbus_present == vbus_present) { in usb_change_state()
1349 if (vbus_present != data->vbus_present && attached) { in usb_change_state()
1350 udc_submit_event(data->dev, in usb_change_state()
1354 data->attached = true; in usb_change_state()
1355 data->vbus_present = true; in usb_change_state()
1363 USB->USB_MCTRL_REG = USB_USB_MCTRL_REG_USBEN_Msk; in usb_change_state()
1364 USB->USB_NFSR_REG = 0; in usb_change_state()
1365 USB->USB_FAR_REG = 0x80; in usb_change_state()
1366 USB->USB_TXMSK_REG = 0; in usb_change_state()
1367 USB->USB_RXMSK_REG = 0; in usb_change_state()
1369 USB->USB_MAMSK_REG = USB_USB_MAMSK_REG_USB_M_INTR_Msk | in usb_change_state()
1372 USB->USB_ALTMSK_REG = in usb_change_state()
1375 USB->USB_MCTRL_REG = USB_USB_MCTRL_REG_USBEN_Msk | USB_USB_MCTRL_REG_USB_NAT_Msk; in usb_change_state()
1376 } else if (data->attached && data->vbus_present) { in usb_change_state()
1381 data->attached = attached; in usb_change_state()
1382 data->vbus_present = vbus_present; in usb_change_state()
1384 * It's imperative that USB_NAT bit-field is updated with the in usb_change_state()
1385 * USBEN bit-field being set. As such, zeroing the control in usb_change_state()
1393 USB->USB_MCTRL_REG = 0; in usb_change_state()
1400 data->attached = attached; in usb_change_state()
1401 data->vbus_present = vbus_present; in usb_change_state()
1409 CRG_TOP->VBUS_IRQ_CLEAR_REG = 1; in usb_dc_smartbond_vbus_isr()
1410 usb_change_state(data, data->attached, in usb_dc_smartbond_vbus_isr()
1411 (CRG_TOP->ANA_STATUS_REG & CRG_TOP_ANA_STATUS_REG_VBUS_AVAILABLE_Msk) != in usb_dc_smartbond_vbus_isr()
1423 return -ENOMEM; in usb_dc_smartbond_alloc_status_out()
1426 k_fifo_put(&ep_cfg->fifo, buf); in usb_dc_smartbond_alloc_status_out()
1435 struct udc_ep_config *const ep_cfg = &ep_state->config; in usbd_ctrl_feed_dout()
1440 return -ENOMEM; in usbd_ctrl_feed_dout()
1443 k_fifo_put(&ep_cfg->fifo, buf); in usbd_ctrl_feed_dout()
1444 ep_state->buf = buf; in usbd_ctrl_feed_dout()
1456 const struct device *dev = data->dev; in handle_ep0_rx_work()
1491 const struct device *dev = data->dev; in handle_ep0_tx_work()
1496 __ASSERT(buf == EP0_IN_STATE(data)->buf, "TX work without buffer %p %p", buf, in handle_ep0_tx_work()
1497 EP0_IN_STATE(data)->buf); in handle_ep0_tx_work()
1512 __ASSERT(buf == EP0_IN_STATE(data)->buf, "Internal error"); in handle_ep0_tx_work()
1527 * Flush TX FIFO in case host already send status OUT packet in handle_ep0_tx_work()
1530 USB->USB_TXC0_REG = USB_USB_TXC0_REG_USB_FLUSH_Msk; in handle_ep0_tx_work()
1531 /* Enable reception of status OUT packet */ in handle_ep0_tx_work()
1546 const struct device *dev = data->dev; in handle_ep0_setup_work()
1556 net_buf_add_mem(buf, data->setup_buffer, 8); in handle_ep0_setup_work()
1558 ep0_out_state->last_packet_size = 0; in handle_ep0_setup_work()
1559 ep0_out_state->buf = NULL; in handle_ep0_setup_work()
1563 /* Allocate and feed buffer for data OUT stage */ in handle_ep0_setup_work()
1564 LOG_DBG("s:%p|feed for -out-", buf); in handle_ep0_setup_work()
1566 if (err == -ENOMEM) { in handle_ep0_setup_work()
1570 /* Allocate buffer for Status OUT state */ in handle_ep0_setup_work()
1572 if (err == -ENOMEM) { in handle_ep0_setup_work()
1576 if (err == -ENOMEM) { in handle_ep0_setup_work()
1588 const struct udc_smartbond_config *config = dev->config; in udc_smartbond_enable()
1592 usb_change_state(data, true, data->vbus_present); in udc_smartbond_enable()
1596 return -EIO; in udc_smartbond_enable()
1601 return -EIO; in udc_smartbond_enable()
1604 irq_enable(config->udc_irq); in udc_smartbond_enable()
1612 const struct udc_smartbond_config *config = dev->config; in udc_smartbond_disable()
1616 usb_change_state(data, false, data->vbus_present); in udc_smartbond_disable()
1618 irq_disable(config->udc_irq); in udc_smartbond_disable()
1631 struct udc_data *udc_data = &data->udc_data; in udc_smartbond_init()
1632 const struct udc_smartbond_config *config = dev->config; in udc_smartbond_init()
1633 struct smartbond_ep_reg_set *reg_set = (struct smartbond_ep_reg_set *)&(USB->USB_EPC0_REG); in udc_smartbond_init()
1634 const uint16_t mps = 1023; in udc_smartbond_init() local
1637 data->dev = dev; in udc_smartbond_init()
1639 k_mutex_init(&udc_data->mutex); in udc_smartbond_init()
1640 k_work_init(&data->ep0_setup_work, handle_ep0_setup_work); in udc_smartbond_init()
1641 k_work_init(&data->ep0_rx_work, handle_ep0_rx_work); in udc_smartbond_init()
1642 k_work_init(&data->ep0_tx_work, handle_ep0_tx_work); in udc_smartbond_init()
1644 udc_data->caps.rwup = true; in udc_smartbond_init()
1645 udc_data->caps.mps0 = UDC_MPS0_8; in udc_smartbond_init()
1647 for (int i = 0; i < config->num_of_eps; i++) { in udc_smartbond_init()
1648 data->ep_state[0][i].config.caps.out = 1; in udc_smartbond_init()
1650 data->ep_state[0][i].config.caps.control = 1; in udc_smartbond_init()
1651 data->ep_state[0][i].config.caps.mps = 8; in udc_smartbond_init()
1653 data->ep_state[0][i].config.caps.bulk = 1; in udc_smartbond_init()
1654 data->ep_state[0][i].config.caps.interrupt = 1; in udc_smartbond_init()
1655 data->ep_state[0][i].config.caps.iso = 1; in udc_smartbond_init()
1656 data->ep_state[0][i].config.caps.mps = mps; in udc_smartbond_init()
1658 data->ep_state[0][i].config.addr = USB_EP_DIR_OUT | i; in udc_smartbond_init()
1659 err = udc_register_ep(dev, &data->ep_state[0][i].config); in udc_smartbond_init()
1664 data->ep_state[0][i].regs = reg_set + i; in udc_smartbond_init()
1667 for (int i = 0; i < config->num_of_eps; i++) { in udc_smartbond_init()
1668 data->ep_state[1][i].config.caps.in = 1; in udc_smartbond_init()
1670 data->ep_state[1][i].config.caps.control = 1; in udc_smartbond_init()
1671 data->ep_state[1][i].config.caps.mps = 8; in udc_smartbond_init()
1673 data->ep_state[1][i].config.caps.bulk = 1; in udc_smartbond_init()
1674 data->ep_state[1][i].config.caps.interrupt = 1; in udc_smartbond_init()
1675 data->ep_state[1][i].config.caps.iso = 1; in udc_smartbond_init()
1676 data->ep_state[1][i].config.caps.mps = mps; in udc_smartbond_init()
1679 data->ep_state[1][i].config.addr = USB_EP_DIR_IN | i; in udc_smartbond_init()
1680 err = udc_register_ep(dev, &data->ep_state[1][i].config); in udc_smartbond_init()
1685 data->ep_state[1][i].regs = reg_set + i; in udc_smartbond_init()
1688 CRG_TOP->VBUS_IRQ_CLEAR_REG = 1; in udc_smartbond_init()
1690 CRG_TOP->VBUS_IRQ_MASK_REG = CRG_TOP_VBUS_IRQ_MASK_REG_VBUS_IRQ_EN_FALL_Msk | in udc_smartbond_init()
1692 NVIC_SetPendingIRQ(config->vbus_irq); in udc_smartbond_init()
1693 irq_enable(config->vbus_irq); in udc_smartbond_init()