| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include <stdint.h> |
| 32 |
#include <stdbool.h> |
| 33 |
#include <usb/usb.h> |
| 34 |
|
| 35 |
#include "generic_hub.h" |
| 36 |
#include "uhci_private.h" |
| 37 |
#include "uhci.h" |
| 38 |
|
| 39 |
#define PORTSC(p) (PORTSC1 + ((p) - 1) * sizeof(uint16_t)) |
| 40 |
#define PORTSC_CCS (1 << 0) |
| 41 |
#define PORTSC_CSC (1 << 1) |
| 42 |
#define PORTSC_PED (1 << 2) |
| 43 |
#define PORTSC_PEDC (1 << 3) |
| 44 |
#define PORTSC_LSDA (1 << 8) |
| 45 |
#define PORTSC_PR (1 << 9) |
| 46 |
#define PORTSC_SUSP (1 << 12) |
| 47 |
#define PORTSC_SC_BITS (PORTSC_CSC | PORTSC_PEDC) |
| 48 |
|
| 49 |
static int |
| 50 |
uhci_rh_port_status_changed(usbdev_t *const dev, const int port) |
| 51 |
{ |
| 52 |
const u16 portsc = uhci_reg_read16(dev->controller, PORTSC(port)); |
| 53 |
const bool changed = portsc & (PORTSC_CSC | PORTSC_PEDC); |
| 54 |
|
| 55 |
|
| 56 |
uhci_reg_write16(dev->controller, PORTSC(port), portsc); |
| 57 |
|
| 58 |
return changed; |
| 59 |
} |
| 60 |
|
| 61 |
static int |
| 62 |
uhci_rh_port_connected(usbdev_t *const dev, const int port) |
| 63 |
{ |
| 64 |
const u16 portsc = uhci_reg_read16(dev->controller, PORTSC(port)); |
| 65 |
return !!(portsc & PORTSC_CCS); |
| 66 |
} |
| 67 |
|
| 68 |
static int |
| 69 |
uhci_rh_port_enabled(usbdev_t *const dev, const int port) |
| 70 |
{ |
| 71 |
const u16 portsc = uhci_reg_read16(dev->controller, PORTSC(port)); |
| 72 |
return !(portsc & PORTSC_SUSP) && (portsc & PORTSC_PED); |
| 73 |
} |
| 74 |
|
| 75 |
static usb_speed |
| 76 |
uhci_rh_port_speed(usbdev_t *const dev, const int port) |
| 77 |
{ |
| 78 |
const u16 portsc = uhci_reg_read16(dev->controller, PORTSC(port)); |
| 79 |
const bool low_speed = portsc & PORTSC_LSDA; |
| 80 |
|
| 81 |
return low_speed ? LOW_SPEED : FULL_SPEED; |
| 82 |
} |
| 83 |
|
| 84 |
static int |
| 85 |
uhci_rh_enable_port(usbdev_t *const dev, const int port) |
| 86 |
{ |
| 87 |
const u16 portsc = uhci_reg_read16(dev->controller, PORTSC(port)) & ~PORTSC_SUSP; |
| 88 |
uhci_reg_write16(dev->controller, PORTSC(port), portsc & ~(PORTSC_SC_BITS | PORTSC_SUSP)); |
| 89 |
uhci_reg_write16(dev->controller, PORTSC(port), (portsc & ~PORTSC_SC_BITS) | PORTSC_PED); |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
static int |
| 94 |
uhci_rh_disable_port(usbdev_t *const dev, const int port) |
| 95 |
{ |
| 96 |
const u16 portsc = uhci_reg_read16(dev->controller, PORTSC(port)); |
| 97 |
uhci_reg_write16(dev->controller, PORTSC(port), portsc & ~(PORTSC_SC_BITS | PORTSC_PED)); |
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
static int |
| 102 |
uhci_rh_reset_port(usbdev_t *const dev, const int port) |
| 103 |
{ |
| 104 |
const u16 portsc = uhci_reg_read16(dev->controller, PORTSC(port)) & ~PORTSC_PR; |
| 105 |
|
| 106 |
|
| 107 |
uhci_reg_write16(dev->controller, PORTSC(port), portsc | PORTSC_PR); |
| 108 |
|
| 109 |
|
| 110 |
mdelay(15); |
| 111 |
|
| 112 |
|
| 113 |
uhci_reg_write16(dev->controller, PORTSC(port), portsc & ~PORTSC_PR); |
| 114 |
|
| 115 |
udelay(10); |
| 116 |
|
| 117 |
|
| 118 |
uhci_reg_write16(dev->controller, PORTSC(port), portsc | PORTSC_PED); |
| 119 |
|
| 120 |
|
| 121 |
uhci_reg_write16(dev->controller, PORTSC(port), portsc | PORTSC_PED | PORTSC_SC_BITS); |
| 122 |
|
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
static const generic_hub_ops_t uhci_rh_ops = { |
| 127 |
.hub_status_changed = NULL, |
| 128 |
.port_status_changed = uhci_rh_port_status_changed, |
| 129 |
.port_connected = uhci_rh_port_connected, |
| 130 |
.port_in_reset = NULL, |
| 131 |
.port_enabled = uhci_rh_port_enabled, |
| 132 |
.port_speed = uhci_rh_port_speed, |
| 133 |
.enable_port = uhci_rh_enable_port, |
| 134 |
.disable_port = uhci_rh_disable_port, |
| 135 |
.start_port_reset = NULL, |
| 136 |
.reset_port = uhci_rh_reset_port, |
| 137 |
}; |
| 138 |
|
| 139 |
void |
| 140 |
uhci_rh_init(usbdev_t *dev) |
| 141 |
{ |
| 142 |
|
| 143 |
dev->address = 0; |
| 144 |
dev->hub = -1; |
| 145 |
dev->port = -1; |
| 146 |
|
| 147 |
generic_hub_init(dev, 2, &uhci_rh_ops); |
| 148 |
|
| 149 |
usb_debug("UHCI: root hub init done\n"); |
| 150 |
} |