From d3893ead3a2a75827deeb77eecffcc908796a8c2 Mon Sep 17 00:00:00 2001 From: Marcin Bukat Date: Fri, 25 Sep 2026 20:24:05 +0200 Subject: [PATCH] rk27xx: keep interrupt and bulk endpoints in separate groups USB mass storage writes ran at 0.03 MB/s in the firmware - to the NAND and to the SD card alike - while the bootloader, with the same driver, wrote the same NAND at 2 MB/s. The firmware adds a HID interface, and without it writes ran at full speed. The UDC's endpoints come in groups of three - bulk OUT, bulk IN, interrupt IN: 1-3, 4-6 and so on - and allocation handed out the first free endpoint of each type, so HID's interrupt endpoint 3 landed in the group of mass storage's bulk endpoints 1 and 2. The host polls it every 16 ms, the idle endpoint NAKs, and each poll costs the group's bulk traffic: writes advanced about one packet per poll. Polled every 125 us instead, they all but stopped; moved to endpoint 6, in a group of its own, they ran at 2.36 MB/s, as without HID. Never give an interrupt endpoint a group with bulk endpoints in use, or the other way round, whichever class asks first. Since which endpoints are available then depends on what is already allocated, the driver tracks the allocation itself - option 2 of usb_drv.h, as usb-designware does - with its context in usb-rk27xx.h, which usb_core.c includes before usb_drv.h. HID keeps working. Tested on a generic rk2705 with ums_stress.py over USB mass storage to the SD card: 0.03 MB/s with HID on endpoint 3, 2.36 MB/s with it on endpoint 6 - the allocation this change produces for mass storage plus HID. (Measured with the driver's earlier allocator, before the USB core took allocation over; this carries the same rule into the new scheme.) Co-Authored-By: Claude Opus 5.5 Change-Id: Ib80ed206a3705f2c76fc8457b5c0a54d60a46402 --- firmware/export/usb-rk27xx.h | 37 +++++++++++++ firmware/target/arm/rk27xx/usb-drv-rk27xx.c | 58 ++++++++++++++++----- firmware/usbstack/usb_core.c | 2 + 3 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 firmware/export/usb-rk27xx.h diff --git a/firmware/export/usb-rk27xx.h b/firmware/export/usb-rk27xx.h new file mode 100644 index 0000000000..1d9a19518c --- /dev/null +++ b/firmware/export/usb-rk27xx.h @@ -0,0 +1,37 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * + * Copyright (C) 2026 by Marcin Bukat + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#ifndef __USB_RK27XX_H__ +#define __USB_RK27XX_H__ + +#include +#include "cpu.h" /* USB_NUM_ENDPOINTS */ + +/* rk27xx UDC endpoint allocation: which endpoints may be allocated depends + * on what else is - interrupt and bulk endpoints never share a group of + * three - so the driver tracks the allocation itself (usb_drv.h, option 2; + * usb-drv-rk27xx.c). Include before usb_drv.h. */ +struct usb_drv_ep_alloc_ctx_rk27xx +{ + int8_t type[USB_NUM_ENDPOINTS][2]; + int max_packet_size[USB_NUM_ENDPOINTS][2]; +}; +#define usb_drv_ep_alloc_ctx usb_drv_ep_alloc_ctx_rk27xx + +#endif /* __USB_RK27XX_H__ */ diff --git a/firmware/target/arm/rk27xx/usb-drv-rk27xx.c b/firmware/target/arm/rk27xx/usb-drv-rk27xx.c index a1dda45ba0..fb1775f993 100644 --- a/firmware/target/arm/rk27xx/usb-drv-rk27xx.c +++ b/firmware/target/arm/rk27xx/usb-drv-rk27xx.c @@ -20,6 +20,7 @@ #include "config.h" #include "usb.h" +#include "usb-rk27xx.h" #include "usb_drv.h" #include "cpu.h" @@ -30,6 +31,7 @@ #include "usb_ch9.h" #include "usb_core.h" #include +#include #include "power.h" #define LOGF_ENABLE @@ -100,9 +102,6 @@ static struct endpoint_t endpoints[16] = ENDPOINT(15, INT, IN, &TX15STAT), /* IIN15 */ }; -struct usb_drv_ep_spec usb_drv_ep_specs[16]; /* filled in usb_drv_startup */ -uint8_t usb_drv_ep_specs_flags = 0; - static volatile bool set_address = false; static volatile bool set_configuration = false; @@ -273,16 +272,51 @@ static void udc_helper(void) } } -void usb_drv_startup(void) { - /* fill the endpoint spec table */ - usb_drv_ep_specs[0].type[DIR_OUT] = USB_ENDPOINT_XFER_CONTROL; - usb_drv_ep_specs[0].type[DIR_IN] = USB_ENDPOINT_XFER_CONTROL; - for(int ep_num = 1; ep_num < 16; ep_num++) { - int dir = endpoints[ep_num].dir; - int type = endpoints[ep_num].type; - usb_drv_ep_specs[ep_num].type[dir] = type; - usb_drv_ep_specs[ep_num].type[!dir] = USB_ENDPOINT_TYPE_NONE; +/* The UDC's endpoints come in groups of three - bulk OUT, bulk IN, + * interrupt IN: 1-3, 4-6 and so on - each endpoint with one fixed type and + * direction. An interrupt endpoint in the same group as bulk endpoints in + * use slows the bulk transfers down: every IN token the host polls it with + * and it NAKs costs the group's bulk traffic. With HID on endpoint 3 beside + * mass storage on 1 and 2, writes ran at 0.03 MB/s, and polled every 125 us + * instead of every 16 ms they all but stopped; on endpoint 6 they ran at + * 2.3 MB/s, as with no HID at all. So interrupt and bulk endpoints never + * share a group - which depends on what else is allocated, hence the + * driver's own allocator (usb_drv.h, option 2). */ +void usb_drv_ep_reset_alloc_ctx(struct usb_drv_ep_alloc_ctx* ctx) +{ + memset(ctx->type, -1, sizeof(ctx->type)); + memset(ctx->max_packet_size, 0, sizeof(ctx->max_packet_size)); +} + +bool usb_drv_ep_allocate(struct usb_drv_ep_alloc_ctx* ctx, int ep, int type, + int max_packet_size) +{ + int ep_num = EP_NUM(ep); + int dir = EP_DIR(ep); + struct endpoint_t *endp = &endpoints[ep_num]; + + if(ep_num == 0 || endp->type != type || + endp->dir != (dir == DIR_IN ? USB_DIR_IN : USB_DIR_OUT)) + return false; + + int first = (ep_num - 1) / 3 * 3 + 1; + for(int i = first; i < first + 3 && i < USB_NUM_ENDPOINTS; i++) + { + for(int d = 0; d < 2; d++) + { + if(ctx->type[i][d] != -1 && ctx->type[i][d] != type) + return false; + } } + + ctx->type[ep_num][dir] = type; + ctx->max_packet_size[ep_num][dir] = max_packet_size; + return true; +} + +/* one-time init: nothing to do, the endpoints are fixed in endpoints[] */ +void usb_drv_startup(void) +{ } /* return port speed FS=0, HS=1 */ diff --git a/firmware/usbstack/usb_core.c b/firmware/usbstack/usb_core.c index 78becb0fc4..f1b7359973 100644 --- a/firmware/usbstack/usb_core.c +++ b/firmware/usbstack/usb_core.c @@ -58,6 +58,8 @@ /* include order matters, include driver header before usb_drv.h */ #if CONFIG_USBOTG == USBOTG_DESIGNWARE #include "usb-designware.h" +#elif CONFIG_USBOTG == USBOTG_RK27XX +#include "usb-rk27xx.h" #endif #include "usb_drv.h"