Files
tinyUSB/src/portable/raspberrypi/rp2040/rp2040_usb.c

383 lines
14 KiB
C
Raw Normal View History

2021-01-19 19:52:07 -06:00
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
* Copyright (c) 2021 Ha Thach (tinyusb.org) for Double Buffered
2021-01-19 19:52:07 -06:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* This file is part of the TinyUSB stack.
*/
2021-01-27 13:04:38 +07:00
#include "tusb_option.h"
#if CFG_TUSB_MCU == OPT_MCU_RP2040
2021-01-19 19:52:07 -06:00
#include <stdlib.h>
#include "rp2040_usb.h"
2023-01-31 17:37:14 +07:00
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF PROTOTYPE
//--------------------------------------------------------------------+
2024-03-05 19:43:37 +07:00
static void _hw_endpoint_xfer_sync(struct hw_endpoint* ep);
2021-01-19 19:52:07 -06:00
2023-01-31 17:37:14 +07:00
#if TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX
2024-03-05 19:43:37 +07:00
static bool e15_is_bulkin_ep(struct hw_endpoint* ep);
static bool e15_is_critical_frame_period(struct hw_endpoint* ep);
2023-01-31 17:37:14 +07:00
#else
#define e15_is_bulkin_ep(x) (false)
#define e15_is_critical_frame_period(x) (false)
#endif
2022-11-30 18:36:01 +07:00
// if usb hardware is in host mode
2024-03-05 19:43:37 +07:00
TU_ATTR_ALWAYS_INLINE static inline bool is_host_mode(void) {
2022-11-30 18:36:01 +07:00
return (usb_hw->main_ctrl & USB_MAIN_CTRL_HOST_NDEVICE_BITS) ? true : false;
}
2021-01-19 19:52:07 -06:00
//--------------------------------------------------------------------+
2023-01-31 17:37:14 +07:00
// Implementation
//--------------------------------------------------------------------+
// Provide own byte by byte memcpy as not all copies are aligned
static void unaligned_memcpy(void *dst, const void *src, size_t n) {
uint8_t *dst_byte = (uint8_t*)dst;
const uint8_t *src_byte = (const uint8_t*)src;
while (n--) {
*dst_byte++ = *src_byte++;
}
}
2024-03-05 19:43:37 +07:00
void rp2040_usb_init(void) {
2021-06-11 18:14:11 +07:00
// Reset usb controller
reset_block(RESETS_RESET_USBCTRL_BITS);
unreset_block_wait(RESETS_RESET_USBCTRL_BITS);
2021-01-19 19:52:07 -06:00
#ifdef __GNUC__
2021-06-11 18:14:11 +07:00
// Clear any previous state just in case
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
#if __GNUC__ > 6
#pragma GCC diagnostic ignored "-Wstringop-overflow"
#endif
#endif
2021-06-11 18:14:11 +07:00
memset(usb_dpram, 0, sizeof(*usb_dpram));
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
2021-01-19 19:52:07 -06:00
2021-06-11 18:14:11 +07:00
// Mux the controller to the onboard usb phy
usb_hw->muxing = USB_USB_MUXING_TO_PHY_BITS | USB_USB_MUXING_SOFTCON_BITS;
2022-11-30 18:36:01 +07:00
TU_LOG2_INT(sizeof(hw_endpoint_t));
2021-01-19 19:52:07 -06:00
}
2024-03-05 19:43:37 +07:00
void __tusb_irq_path_func(hw_endpoint_reset_transfer)(struct hw_endpoint* ep) {
2021-06-11 18:14:11 +07:00
ep->active = false;
ep->remaining_len = 0;
ep->xferred_len = 0;
ep->user_buf = 0;
2021-01-19 19:52:07 -06:00
}
2024-03-05 19:43:37 +07:00
void __tusb_irq_path_func(_hw_endpoint_buffer_control_update32)(struct hw_endpoint* ep, uint32_t and_mask,
uint32_t or_mask) {
2022-11-30 18:36:01 +07:00
uint32_t value = 0;
2023-01-31 11:38:15 +07:00
2024-03-05 19:43:37 +07:00
if (and_mask) {
2022-11-30 18:36:01 +07:00
value = *ep->buffer_control & and_mask;
}
2023-01-31 11:38:15 +07:00
2024-03-05 19:43:37 +07:00
if (or_mask) {
2022-11-30 18:36:01 +07:00
value |= or_mask;
2024-03-05 19:43:37 +07:00
if (or_mask & USB_BUF_CTRL_AVAIL) {
if (*ep->buffer_control & USB_BUF_CTRL_AVAIL) {
panic("ep %02X was already available", ep->ep_addr);
2022-11-30 18:36:01 +07:00
}
*ep->buffer_control = value & ~USB_BUF_CTRL_AVAIL;
// 4.1.2.5.1 Con-current access: 12 cycles (should be good for 48*12Mhz = 576Mhz) after write to buffer control
2022-11-30 18:36:01 +07:00
// Don't need delay in host mode as host is in charge
if (!is_host_mode()) {
busy_wait_at_least_cycles(12);
}
2021-01-19 19:52:07 -06:00
}
2022-11-30 18:36:01 +07:00
}
2023-01-31 11:38:15 +07:00
2022-11-30 18:36:01 +07:00
*ep->buffer_control = value;
2021-01-19 19:52:07 -06:00
}
2021-06-13 17:19:14 +07:00
// prepare buffer, return buffer control
2024-03-05 19:43:37 +07:00
static uint32_t __tusb_irq_path_func(prepare_ep_buffer)(struct hw_endpoint* ep, uint8_t buf_id) {
2021-06-11 18:14:11 +07:00
uint16_t const buflen = tu_min16(ep->remaining_len, ep->wMaxPacketSize);
2024-03-05 19:43:37 +07:00
ep->remaining_len = (uint16_t) (ep->remaining_len - buflen);
2021-06-11 17:05:49 +07:00
2021-06-11 18:14:11 +07:00
uint32_t buf_ctrl = buflen | USB_BUF_CTRL_AVAIL;
// PID
buf_ctrl |= ep->next_pid ? USB_BUF_CTRL_DATA1_PID : USB_BUF_CTRL_DATA0_PID;
ep->next_pid ^= 1u;
2021-06-11 17:05:49 +07:00
2024-03-05 19:43:37 +07:00
if (!ep->rx) {
2021-06-11 17:05:49 +07:00
// Copy data from user buffer to hw buffer
unaligned_memcpy(ep->hw_data_buf + buf_id * 64, ep->user_buf, buflen);
2021-06-11 18:14:11 +07:00
ep->user_buf += buflen;
2021-06-11 17:05:49 +07:00
// Mark as full
buf_ctrl |= USB_BUF_CTRL_FULL;
}
2021-06-11 18:14:11 +07:00
// Is this the last buffer? Only really matters for host mode. Will trigger
// the trans complete irq but also stop it polling. We only really care about
// trans complete for setup packets being sent
2024-03-05 19:43:37 +07:00
if (ep->remaining_len == 0) {
2021-06-11 18:14:11 +07:00
buf_ctrl |= USB_BUF_CTRL_LAST;
}
if (buf_id) buf_ctrl = buf_ctrl << 16;
return buf_ctrl;
}
// Prepare buffer control register value
2024-03-05 19:43:37 +07:00
void __tusb_irq_path_func(hw_endpoint_start_next_buffer)(struct hw_endpoint* ep) {
2021-06-11 18:14:11 +07:00
uint32_t ep_ctrl = *ep->endpoint_control;
2021-06-11 17:05:49 +07:00
// always compute and start with buffer 0
uint32_t buf_ctrl = prepare_ep_buffer(ep, 0) | USB_BUF_CTRL_SEL;
2021-06-11 17:05:49 +07:00
2022-11-30 18:36:01 +07:00
// For now: skip double buffered for OUT endpoint in Device mode, since
// host could send < 64 bytes and cause short packet on buffer0
2022-11-30 18:36:01 +07:00
// NOTE: this could happen to Host mode IN endpoint
// Also, Host mode "interrupt" endpoint hardware is only single buffered,
// NOTE2: Currently Host bulk is implemented using "interrupt" endpoint
bool const is_host = is_host_mode();
bool const force_single = (!is_host && !tu_edpt_dir(ep->ep_addr)) ||
(is_host && tu_edpt_number(ep->ep_addr) != 0);
2024-03-05 19:43:37 +07:00
if (ep->remaining_len && !force_single) {
2021-06-11 18:14:11 +07:00
// Use buffer 1 (double buffered) if there is still data
// TODO: Isochronous for buffer1 bit-field is different than CBI (control bulk, interrupt)
2021-06-13 17:19:14 +07:00
buf_ctrl |= prepare_ep_buffer(ep, 1);
2021-06-11 17:05:49 +07:00
// Set endpoint control double buffered bit if needed
ep_ctrl &= ~EP_CTRL_INTERRUPT_PER_BUFFER;
ep_ctrl |= EP_CTRL_DOUBLE_BUFFERED_BITS | EP_CTRL_INTERRUPT_PER_DOUBLE_BUFFER;
2024-03-05 19:43:37 +07:00
} else {
2021-06-11 18:14:11 +07:00
// Single buffered since 1 is enough
2021-06-11 17:05:49 +07:00
ep_ctrl &= ~(EP_CTRL_DOUBLE_BUFFERED_BITS | EP_CTRL_INTERRUPT_PER_DOUBLE_BUFFER);
ep_ctrl |= EP_CTRL_INTERRUPT_PER_BUFFER;
}
*ep->endpoint_control = ep_ctrl;
2022-06-17 16:50:24 +08:00
TU_LOG(3, " Prepare BufCtrl: [0] = 0x%04x [1] = 0x%04x\r\n", tu_u32_low16(buf_ctrl), tu_u32_high16(buf_ctrl));
2021-06-11 17:05:49 +07:00
// Finally, write to buffer_control which will trigger the transfer
// the next time the controller polls this dpram address
_hw_endpoint_buffer_control_set_value32(ep, buf_ctrl);
2021-01-19 19:52:07 -06:00
}
2024-03-05 19:43:37 +07:00
void hw_endpoint_xfer_start(struct hw_endpoint* ep, uint8_t* buffer, uint16_t total_len) {
hw_endpoint_lock_update(ep, 1);
2024-03-05 19:43:37 +07:00
if (ep->active) {
2021-06-11 17:05:49 +07:00
// TODO: Is this acceptable for interrupt packets?
TU_LOG(1, "WARN: starting new transfer on already active ep %02X\r\n", ep->ep_addr);
2021-06-11 17:05:49 +07:00
hw_endpoint_reset_transfer(ep);
}
// Fill in info now that we're kicking off the hw
ep->remaining_len = total_len;
2024-03-05 19:43:37 +07:00
ep->xferred_len = 0;
ep->active = true;
ep->user_buf = buffer;
2021-06-11 17:05:49 +07:00
2024-03-05 19:43:37 +07:00
if (e15_is_bulkin_ep(ep)) {
usb_hw_set->inte = USB_INTS_DEV_SOF_BITS;
2023-01-31 11:38:15 +07:00
}
2024-03-05 19:43:37 +07:00
if (e15_is_critical_frame_period(ep)) {
ep->pending = 1;
2024-03-05 19:43:37 +07:00
} else {
hw_endpoint_start_next_buffer(ep);
}
2023-01-31 11:38:15 +07:00
hw_endpoint_lock_update(ep, -1);
2021-01-19 19:52:07 -06:00
}
// sync endpoint buffer and return transferred bytes
2024-03-05 19:43:37 +07:00
static uint16_t __tusb_irq_path_func(sync_ep_buffer)(struct hw_endpoint* ep, uint8_t buf_id) {
uint32_t buf_ctrl = _hw_endpoint_buffer_control_get_value32(ep);
2024-03-05 19:43:37 +07:00
if (buf_id) buf_ctrl = buf_ctrl >> 16;
2021-06-11 17:05:49 +07:00
uint16_t xferred_bytes = buf_ctrl & USB_BUF_CTRL_LEN_MASK;
2021-06-11 17:05:49 +07:00
2024-03-05 19:43:37 +07:00
if (!ep->rx) {
// We are continuing a transfer here. If we are TX, we have successfully
// sent some data can increase the length we have sent
2021-06-11 17:05:49 +07:00
assert(!(buf_ctrl & USB_BUF_CTRL_FULL));
2024-03-05 19:43:37 +07:00
ep->xferred_len = (uint16_t) (ep->xferred_len + xferred_bytes);
} else {
// If we have received some data, so can increase the length
// we have received AFTER we have copied it to the user buffer at the appropriate offset
2021-06-11 17:05:49 +07:00
assert(buf_ctrl & USB_BUF_CTRL_FULL);
unaligned_memcpy(ep->user_buf, ep->hw_data_buf + buf_id * 64, xferred_bytes);
2024-03-05 19:43:37 +07:00
ep->xferred_len = (uint16_t) (ep->xferred_len + xferred_bytes);
ep->user_buf += xferred_bytes;
2021-06-11 17:05:49 +07:00
}
// Short packet
2024-03-05 19:43:37 +07:00
if (xferred_bytes < ep->wMaxPacketSize) {
2023-07-02 10:53:08 +02:00
pico_trace(" Short packet on buffer %d with %u bytes\r\n", buf_id, xferred_bytes);
2021-06-11 17:05:49 +07:00
// Reduce total length as this is last packet
ep->remaining_len = 0;
2021-06-11 17:05:49 +07:00
}
return xferred_bytes;
2021-01-19 19:52:07 -06:00
}
2024-03-05 19:43:37 +07:00
static void __tusb_irq_path_func(_hw_endpoint_xfer_sync)(struct hw_endpoint* ep) {
// Update hw endpoint struct with info from hardware
// after a buff status interrupt
2021-10-14 12:13:39 -05:00
uint32_t __unused buf_ctrl = _hw_endpoint_buffer_control_get_value32(ep);
2022-06-17 16:50:24 +08:00
TU_LOG(3, " Sync BufCtrl: [0] = 0x%04x [1] = 0x%04x\r\n", tu_u32_low16(buf_ctrl), tu_u32_high16(buf_ctrl));
// always sync buffer 0
uint16_t buf0_bytes = sync_ep_buffer(ep, 0);
// sync buffer 1 if double buffered
2024-03-05 19:43:37 +07:00
if ((*ep->endpoint_control) & EP_CTRL_DOUBLE_BUFFERED_BITS) {
if (buf0_bytes == ep->wMaxPacketSize) {
// sync buffer 1 if not short packet
sync_ep_buffer(ep, 1);
2024-03-05 19:43:37 +07:00
} else {
// short packet on buffer 0
// TODO couldn't figure out how to handle this case which happen with net_lwip_webserver example
// At this time (currently trigger per 2 buffer), the buffer1 is probably filled with data from
// the next transfer (not current one). For now we disable double buffered for device OUT
// NOTE this could happen to Host IN
#if 0
uint8_t const ep_num = tu_edpt_number(ep->ep_addr);
uint8_t const dir = (uint8_t) tu_edpt_dir(ep->ep_addr);
uint8_t const ep_id = 2*ep_num + (dir ? 0 : 1);
// abort queued transfer on buffer 1
usb_hw->abort |= TU_BIT(ep_id);
while ( !(usb_hw->abort_done & TU_BIT(ep_id)) ) {}
uint32_t ep_ctrl = *ep->endpoint_control;
ep_ctrl &= ~(EP_CTRL_DOUBLE_BUFFERED_BITS | EP_CTRL_INTERRUPT_PER_DOUBLE_BUFFER);
ep_ctrl |= EP_CTRL_INTERRUPT_PER_BUFFER;
_hw_endpoint_buffer_control_set_value32(ep, 0);
usb_hw->abort &= ~TU_BIT(ep_id);
TU_LOG(3, "----SHORT PACKET buffer0 on EP %02X:\r\n", ep->ep_addr);
2022-06-17 16:50:24 +08:00
TU_LOG(3, " BufCtrl: [0] = 0x%04x [1] = 0x%04x\r\n", tu_u32_low16(buf_ctrl), tu_u32_high16(buf_ctrl));
#endif
}
}
}
2021-01-19 19:52:07 -06:00
// Returns true if transfer is complete
2024-03-05 19:43:37 +07:00
bool __tusb_irq_path_func(hw_endpoint_xfer_continue)(struct hw_endpoint* ep) {
hw_endpoint_lock_update(ep, 1);
2021-06-11 18:14:11 +07:00
// Part way through a transfer
2024-03-05 19:43:37 +07:00
if (!ep->active) {
panic("Can't continue xfer on inactive ep %02X", ep->ep_addr);
2021-06-11 18:14:11 +07:00
}
2021-01-19 19:52:07 -06:00
2021-06-11 18:14:11 +07:00
// Update EP struct from hardware state
_hw_endpoint_xfer_sync(ep);
2021-01-19 19:52:07 -06:00
2021-06-11 18:14:11 +07:00
// Now we have synced our state with the hardware. Is there more data to transfer?
// If we are done then notify tinyusb
2024-03-05 19:43:37 +07:00
if (ep->remaining_len == 0) {
pico_trace("Completed transfer of %d bytes on ep %02X\r\n", ep->xferred_len, ep->ep_addr);
2021-06-11 18:14:11 +07:00
// Notify caller we are done so it can notify the tinyusb stack
hw_endpoint_lock_update(ep, -1);
2021-06-11 18:14:11 +07:00
return true;
2024-03-05 19:43:37 +07:00
} else {
if (e15_is_critical_frame_period(ep)) {
ep->pending = 1;
2024-03-05 19:43:37 +07:00
} else {
hw_endpoint_start_next_buffer(ep);
}
2021-06-11 18:14:11 +07:00
}
hw_endpoint_lock_update(ep, -1);
2021-06-11 18:14:11 +07:00
// More work to do
return false;
2021-01-19 19:52:07 -06:00
}
2023-01-31 17:37:14 +07:00
//--------------------------------------------------------------------+
// Errata 15
//--------------------------------------------------------------------+
#if TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX
/* Don't mark IN buffers as available during the last 200us of a full-speed
frame. This avoids a situation seen with the USB2.0 hub on a Raspberry
Pi 4 where a late IN token before the next full-speed SOF can cause port
babble and a corrupt ACK packet. The nature of the data corruption has a
chance to cause device lockup.
Use the next SOF to mark delayed buffers as available. This reduces
available Bulk IN bandwidth by approximately 20%, and requires that the
SOF interrupt is enabled while these transfers are ongoing.
Inherit the top-level enable from the corresponding Pico-SDK flag.
Applications that will not use the device in a situation where it could
be plugged into a Pi 4 or Pi 400 (for example, when directly connected
to a commodity hub or other host) can turn off the flag in the SDK.
*/
volatile uint32_t e15_last_sof = 0;
// check if Errata 15 is needed for this endpoint i.e device bulk-in
2024-03-05 19:43:37 +07:00
static bool __tusb_irq_path_func(e15_is_bulkin_ep)(struct hw_endpoint* ep) {
2023-01-31 17:37:14 +07:00
return (!is_host_mode() && tu_edpt_dir(ep->ep_addr) == TUSB_DIR_IN &&
ep->transfer_type == TUSB_XFER_BULK);
}
2023-01-31 19:03:31 +07:00
// check if we need to apply Errata 15 workaround : i.e
2023-01-31 17:37:14 +07:00
// Endpoint is BULK IN and is currently in critical frame period i.e 20% of last usb frame
2024-03-05 19:43:37 +07:00
static bool __tusb_irq_path_func(e15_is_critical_frame_period)(struct hw_endpoint* ep) {
2023-01-31 17:37:14 +07:00
TU_VERIFY(e15_is_bulkin_ep(ep));
/* Avoid the last 200us (uframe 6.5-7) of a frame, up to the EOF2 point.
* The device state machine cannot recover from receiving an incorrect PID
* when it is expecting an ACK.
*/
uint32_t delta = time_us_32() - e15_last_sof;
if (delta < 800 || delta > 998) {
return false;
}
2024-03-05 19:43:37 +07:00
TU_LOG(3, "Avoiding sof %lu now %lu last %lu\r\n", (usb_hw->sof_rd + 1) & USB_SOF_RD_BITS, time_us_32(),
e15_last_sof);
2023-01-31 17:37:14 +07:00
return true;
}
2024-03-05 19:43:37 +07:00
#endif // TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX
2021-01-27 13:04:38 +07:00
#endif