USB端口寄存器
400-13FF为端口寄存器,每个端口占用 16 字节的 MMIO 空间,包含状态/控制、电源管理、链路信息和硬件 LPM 配置四个寄存器。
| 要点 | 说明 |
|---|---|
| 端口数量获取 | 通过读取 HCSPARAMS1 寄存器获取端口总数。 |
| 电源域 | 端口寄存器位于 Aux Power well,仅冷复位或 HCRST 时复位。 |
| 停止时无事件 | xHC 停止时(HCH=1),无法生成端口状态变化事件。 |
| 写入前提 | 软件写入端口寄存器前,必须确保 xHC 正在运行(HCH=0)。 |
| 端口电源要求 | 修改端口状态的前提是 PP=1。若 PPC=1,置位 PP 后需等待 20ms 才能操作。 |
| 调试能力冲突 | 端口分配给调试能力时,即使 PP=1,也不能报告设备连接(CCS=0)或启用端口。 |
核心目的:软件操作端口寄存器前必须满足多项条件(xHC 运行、端口上电、稳定延时),避免未定义行为。调试能力会覆盖正常端口功能。
| 偏移 | 缩写 | 寄存器名称 | 章节 |
|---|---|---|---|
| 0h | PORTSC | 端口状态与控制寄存器 | 5.4.8 |
| 4h | PORTPMSC | 端口电源管理状态与控制寄存器 | 5.4.9 |
| 8h | PORTLI | 端口链路信息寄存器 | 5.4.10 |
| Ch | PORTHLPMC | 端口硬件 LPM 控制寄存器 | 5.4.11 |
struct xhci_port_regs {
__le32 portsc;
__le32 portpmsc;
__le32 portli;
__le32 porthlmpc;
};
PORTSC
PORTSC 是 32 位混合读写属性的寄存器。按功能可以分成两组:
- 第一组:状态与控制位(非 change)
- 第二组:Change 位(写 1 清除)
一、第一组:状态与控制位(非 change)
| 位 | 名称 | 含义 / 取值 |
|---|---|---|
| 0 | CCS(Current Connect Status,RO) | 0:无设备连接;1:设备已连接 |
| 1 | PED(Port Enabled/Disabled,RW1CS) | 0:端口禁用;1:端口使能。注意写 1 会清除使能,即“写 1 禁用端口” |
| 2 | 保留 | 必须写 0 |
| 3 | OCA(Over-current Active,RO) | 0:无过流;1:过流有效 |
| 4 | PR(Port Reset,RW1S) | 0:无复位;1:复位信号有效。写 1 启动端口复位 |
| 5~8 | PLS(Port Link State,RWS) | 0=U0,1=U1,2=U2,3=U3,4=Disabled,5=RxDetect,6=Inactive(常表示链路错误或设备异常),7=Polling,8=Recovery,9=Hot Reset,10=Compliance,11=Test,15=Resume;12~14 保留。USB2 端口:U0=正常,U2=L1,U3=Suspend;USB3 端口:U0/U1/U2/U3 为链路状态 |
| 9 | PP(Port Power,RWS) | 0:端口断电;1:端口上电 |
| 10~13 | Speed(RO) | 0=未定义,1=Full Speed,2=Low Speed,3=High Speed,4=SuperSpeed,5=SuperSpeedPlus;6~15 保留 |
| 14~15 | PIC(Port Indicator Control,RWS) | 0=关闭,1=琥珀色,2=绿色,3=未定义 |
| 16 | LWS(Link State Write Strobe,RW) | 写 PLS 时必须置 1,PLS 才会生效;写 0 时 PLS 写入被忽略 |
| 24 | CAS(Cold Attach Status) | 0:正常;1:系统睡眠期间插入设备,通常需要 warm reset 清除。不是普通 change 位 |
| 25 | WCE(Wake on Connect Enable,RWS) | 0:禁止连接唤醒;1:允许连接唤醒 |
| 26 | WDE(Wake on Disconnect Enable,RWS) | 0:禁止断开唤醒;1:允许断开唤醒 |
| 27 | WOE(Wake on Over-current Enable,RWS) | 0:禁止过流唤醒;1:允许过流唤醒 |
| 28~29 | 保留 | 必须写 0 |
| 30 | DR(Device Removable,RO) | 0:可插拔;1:不可插拔 |
| 31 | WPR(Warm Port Reset,RW1S) | 写 1 启动 warm reset;写 0 无效果;完成后置 WRC |
严格说第一组里既有状态位(CCS/OCA/Speed/DR),也有控制位(PED/PR/PP/PIC/LWS/WCE/WDE/WOE/WPR)。它们都不是“变化事件位”。
二、第二组:Change 位(写 1 清除)
| 位 | 名称 | 含义 / 取值 |
|---|---|---|
| 17 | CSC(Connect Status Change) | 0:无变化;1:连接状态发生变化(插入或拔出) |
| 18 | PEC(Port Enabled/Disabled Change) | 0:无变化;1:端口使能/禁用状态发生变化 |
| 19 | WRC(Warm Port Reset Change) | 0:无变化;1:warm reset 完成。USB2 端口通常保留为 0 |
| 20 | OCC(Over-current Change) | 0:无变化;1:过流状态发生变化 |
| 21 | PRC / PORT_RC(Port Reset Change) | 0:无变化;1:端口复位完成(PR 由 1 变 0) |
| 22 | PLC(Port Link State Change) | 0:无变化;1:链路状态发生变化,如 U3→Resume、Resume→U0、U2→U0 等 |
| 23 | CEC(Port Config Error Change) | 0:无变化;1:端口配置错误状态发生变化 |
这些位都是 RW1C:
- 写 1 清除;
- 写 0 无效果;
- 读出来是当前是否还有未处理的变化。
PORT_CHANGE_MASK 就是这 7 位的组合:
#define PORT_CHANGE_MASK (PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | PORT_RC | PORT_PLC | PORT_CEC)
当这些位的“或”从 0 变 1 时,xHC 产生 Port Status Change Event;如果软件不清位,后续变化不会再产生新事件。
三、PORTSC 的使用方法
1. 读端口状态
portsc = xhci_portsc_readl(port);
xhci_portsc_readl() 内部带 trace,最终是 readl(&port->port_reg->portsc)。
2. 写端口状态不能直接 read-modify-write
PORTSC 混合了:
- RO:CCS、OCA、Speed、DR;
- RWS:PLS、PP、PIC、WCE/WDE/WOE;
- RW1S:PR、WPR;
- RW1CS:PED、change 位;
- RW:LWS;
- RZ:保留位。
如果直接 portsc |= xxx; writel(portsc),可能误清 change 位、误启动 reset。所以驱动先用:
temp = xhci_port_state_to_neutral(portsc);
它只保留 RO 和 RWS 位:
#define XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
#define XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
u32 xhci_port_state_to_neutral(u32 state)
{
/* Save read-only status and port state */
return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
}
其余 RW1S/RW1CS/RW/RZ 位清零,写回去不会产生副作用。
故最终代码为:
portsc = xhci_portsc_readl(port);
portsc = xhci_port_state_to_neutral(portsc);
3. 端口上电/断电
#define PORT_POWER BIT(9)
temp = xhci_port_state_to_neutral(portsc);
temp |= PORT_POWER; // 上电
xhci_portsc_writel(port, temp);
断电则清 PORT_POWER。
对应 xhci_set_port_power()。
4. 端口复位
普通复位:
#define PORT_RESET BIT(4)
temp = xhci_port_state_to_neutral(portsc);
temp |= PORT_RESET; // PR,RW1S
xhci_portsc_writel(port, temp);
warm reset:
#define PORT_WR BIT(31)
temp = xhci_port_state_to_neutral(portsc);
temp |= PORT_WR; // WPR
xhci_portsc_writel(port, temp);
完成后硬件置 PORT_RC 或 PORT_WRC,USB Core 再 ClearPortFeature 清除。
5. 禁用端口
PED 是 RW1CS,写 1 表示清除使能:
#define PORT_PE (1 << 1)
portsc = xhci_port_state_to_neutral(portsc);
xhci_portsc_writel(port, portsc | PORT_PE);
对应 xhci_disable_port()。
6. 设置链路状态 PLS
必须同时置 PORT_LINK_STROBE:
#define PORT_PLS_MASK (0xf << 5)
temp = xhci_port_state_to_neutral(portsc);
temp &= ~PORT_PLS_MASK;
temp |= PORT_LINK_STROBE | link_state;
xhci_portsc_writel(port, temp);
对应 xhci_set_link_state()。
- USB2:写 U3 表示 suspend,写 Resume 驱动 K 状态;
- USB3:写 U3 挂起,写 U0 恢复。
7. 清除 change 位
xhci_portsc_writel(port, port_status | status);
对应 xhci_clear_port_change_bit():
C_CONNECTION→PORT_CSCC_ENABLE→PORT_PECC_RESET→PORT_RCC_BH_PORT_RESET→PORT_WRCC_OVER_CURRENT→PORT_OCCC_SUSPEND/C_PORT_LINK_STATE→PORT_PLCC_PORT_CONFIG_ERROR→PORT_CEC
xhci_test_and_clear_bit() 也是同样思路:读到某位置位后,写 neutral | 该位清除。
8. 读取变化位、判断端口事件
xhci_hub_status_data():
/* true: connect status change */
#define PORT_CSC (1 << 17)
/* true: port enable change */
#define PORT_PEC (1 << 18)
/* true: warm reset for a USB 3.0 device is done. A "hot" reset puts the port
* into an enabled state, and the device into the default state. A "warm" reset
* also resets the link, forcing the device through the link training sequence.
* SW can also look at the Port Reset register to see when warm reset is done.
*/
#define PORT_WRC (1 << 19)
/* true: over-current change */
#define PORT_OCC (1 << 20)
/* true: reset change - 1 to 0 transition of PORT_RESET */
#define PORT_RC (1 << 21)
/* port link status change - set on some port link state transitions:
* Transition Reason
* ------------------------------------------------------------------------------
* - U3 to Resume Wakeup signaling from a device
* - Resume to Recovery to U0 USB 3.0 device resume
* - Resume to U0 USB 2.0 device resume
* - U3 to Recovery to U0 Software resume of USB 3.0 device complete
* - U3 to U0 Software resume of USB 2.0 device complete
* - U2 to U0 L1 resume of USB 2.1 device complete
* - U0 to U0 (???) L1 entry rejection by USB 2.1 device
* - U0 to disabled L1 entry error with USB 2.1 device
* - Any state to inactive Error on USB 3.0 port
*/
#define PORT_PLC (1 << 22)
/* port configure error change - port failed to configure its link partner */
#define PORT_CEC (1 << 23)
#define PORT_CHANGE_MASK (PORT_CSC | PORT_PEC | PORT_WRC | PORT_OCC | PORT_RC | PORT_PLC | PORT_CEC)
mask = PORT_CSC | PORT_PEC | PORT_OCC | PORT_PLC | PORT_WRC | PORT_CEC;
/* For each port, did anything change? If so, set that bit in buf. */
for (i = 0; i < max_ports; i++) {
temp = xhci_portsc_readl(ports[i]);
if (temp == ~(u32)0) {
xhci_hc_died(xhci);
retval = -ENODEV;
break;
}
trace_xhci_hub_status_data(ports[i], temp);
if ((temp & mask) != 0 ||
(bus_state->port_c_suspend & 1 << i) ||
(ports[i]->resume_timestamp && time_after_eq(
jiffies, ports[i]->resume_timestamp))) {
buf[(i + 1) / 8] |= 1 << (i + 1) % 8;
status = 1;
}
if ((temp & PORT_RC)) //端口复位完成
reset_change = true;
if (temp & PORT_OC)//过流
status = 1;
}
handle_port_status() 则读 PORTSC 判断:
PORT_CSC:插入/拔出;PORT_PLC+PLS:resume / U3 退出 / 链路变化;PORT_PLS_MASK == XDEV_INACTIVE:USB3 链路错误;PORT_RC:复位完成。
9. 远程唤醒使能
/* wake on connect (enable) */
#define PORT_WKCONN_E (1 << 25)
/* wake on disconnect (enable) */
#define PORT_WKDISC_E (1 << 26)
/* wake on over-current (enable) */
#define PORT_WKOC_E (1 << 27)
temp = xhci_port_state_to_neutral(portsc);
temp |= PORT_WKCONN_E | PORT_WKDISC_E | PORT_WKOC_E;
xhci_portsc_writel(port, temp);
对应 xhci_set_remote_wake_mask();xhci_disable_hub_port_wake() 则清这些位。
10. 为什么必须清 change 位
xHCI 端口事件是边沿触发的:
change 位 OR 从 0 → 1 → 产生 Port Status Change Event
如果软件不清位:
- OR 一直是 1;
- 后续插入/拔出/复位不会再产生新事件;
- 端口状态机就“卡住”了。
所以 USB Core 处理完端口后,必须通过 ClearPortFeature 清除对应 change 位。
端口PM状态和控制寄存器 (PORTPMSC)
端口链路信息寄存器 (PORTLI)
端口硬件 LPM 控制寄存器 (PORTHLPMC)
本文链接为:http://www.usbzh.com/article/detail-1829.html ,欢迎转载,转载请附上本文链接。
XHCI 1.2b 规范





