Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 1 | // Code for handling UHCI USB controllers. |
| 2 | // |
| 3 | // Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // |
| 5 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
| 6 | |
| 7 | #include "util.h" // dprintf |
| 8 | #include "pci.h" // pci_bdf_to_bus |
| 9 | #include "config.h" // CONFIG_* |
| 10 | #include "ioport.h" // outw |
| 11 | #include "usb-uhci.h" // USBLEGSUP |
| 12 | #include "pci_regs.h" // PCI_BASE_ADDRESS_4 |
| 13 | #include "usb.h" // struct usb_s |
| 14 | #include "farptr.h" // GET_FLATPTR |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 15 | |
| 16 | static void |
| 17 | reset_uhci(struct usb_s *cntl) |
| 18 | { |
| 19 | // XXX - don't reset if not needed. |
| 20 | |
| 21 | // Reset PIRQ and SMI |
| 22 | pci_config_writew(cntl->bdf, USBLEGSUP, USBLEGSUP_RWC); |
| 23 | |
| 24 | // Reset the HC |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 25 | outw(USBCMD_HCRESET, cntl->uhci.iobase + USBCMD); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 26 | udelay(5); |
| 27 | |
| 28 | // Disable interrupts and commands (just to be safe). |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 29 | outw(0, cntl->uhci.iobase + USBINTR); |
| 30 | outw(0, cntl->uhci.iobase + USBCMD); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static void |
| 34 | configure_uhci(struct usb_s *cntl) |
| 35 | { |
| 36 | // Allocate ram for schedule storage |
| 37 | struct uhci_td *term_td = malloc_high(sizeof(*term_td)); |
| 38 | struct uhci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl)); |
| 39 | struct uhci_qh *data_qh = malloc_low(sizeof(*data_qh)); |
| 40 | struct uhci_qh *term_qh = malloc_high(sizeof(*term_qh)); |
| 41 | if (!term_td || !fl || !data_qh || !term_qh) { |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 42 | dprintf(1, "No ram for uhci init\n"); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 43 | return; |
| 44 | } |
| 45 | |
| 46 | // Work around for PIIX errata |
| 47 | memset(term_td, 0, sizeof(*term_td)); |
| 48 | term_td->link = UHCI_PTR_TERM; |
| 49 | term_td->token = (uhci_explen(0) | (0x7f << TD_TOKEN_DEVADDR_SHIFT) |
| 50 | | USB_PID_IN); |
| 51 | memset(term_qh, 0, sizeof(*term_qh)); |
| 52 | term_qh->element = (u32)term_td; |
| 53 | term_qh->link = UHCI_PTR_TERM; |
| 54 | |
| 55 | // Setup primary queue head. |
| 56 | memset(data_qh, 0, sizeof(*data_qh)); |
| 57 | data_qh->element = UHCI_PTR_TERM; |
| 58 | data_qh->link = (u32)term_qh | UHCI_PTR_QH; |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 59 | cntl->uhci.qh = data_qh; |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 60 | |
| 61 | // Set schedule to point to primary queue head |
| 62 | int i; |
| 63 | for (i=0; i<ARRAY_SIZE(fl->links); i++) { |
| 64 | fl->links[i] = (u32)data_qh | UHCI_PTR_QH; |
| 65 | } |
| 66 | |
| 67 | // Set the frame length to the default: 1 ms exactly |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 68 | outb(USBSOF_DEFAULT, cntl->uhci.iobase + USBSOF); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 69 | |
| 70 | // Store the frame list base address |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 71 | outl((u32)fl->links, cntl->uhci.iobase + USBFLBASEADD); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 72 | |
| 73 | // Set the current frame number |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 74 | outw(0, cntl->uhci.iobase + USBFRNUM); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void |
| 78 | start_uhci(struct usb_s *cntl) |
| 79 | { |
| 80 | // Mark as configured and running with a 64-byte max packet. |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 81 | outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, cntl->uhci.iobase + USBCMD); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Find any devices connected to the root hub. |
| 85 | static int |
| 86 | check_ports(struct usb_s *cntl) |
| 87 | { |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 88 | u16 port1 = inw(cntl->uhci.iobase + USBPORTSC1); |
| 89 | u16 port2 = inw(cntl->uhci.iobase + USBPORTSC2); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 90 | |
| 91 | if (!((port1 & USBPORTSC_CCS) || (port2 & USBPORTSC_CCS))) |
| 92 | // No devices |
| 93 | return 0; |
| 94 | |
| 95 | // reset ports |
| 96 | if (port1 & USBPORTSC_CCS) |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 97 | outw(USBPORTSC_PR, cntl->uhci.iobase + USBPORTSC1); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 98 | if (port2 & USBPORTSC_CCS) |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 99 | outw(USBPORTSC_PR, cntl->uhci.iobase + USBPORTSC2); |
Kevin O'Connor | 10ad799 | 2009-10-24 11:06:08 -0400 | [diff] [blame] | 100 | msleep(50); |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 101 | outw(0, cntl->uhci.iobase + USBPORTSC1); |
| 102 | outw(0, cntl->uhci.iobase + USBPORTSC2); |
Kevin O'Connor | 10ad799 | 2009-10-24 11:06:08 -0400 | [diff] [blame] | 103 | msleep(10); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 104 | |
| 105 | // Configure ports |
| 106 | int totalcount = 0; |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 107 | port1 = inw(cntl->uhci.iobase + USBPORTSC1); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 108 | if (port1 & USBPORTSC_CCS) { |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 109 | outw(USBPORTSC_PE, cntl->uhci.iobase + USBPORTSC1); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 110 | int count = configure_usb_device(cntl, !!(port1 & USBPORTSC_LSDA)); |
| 111 | if (! count) |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 112 | outw(0, cntl->uhci.iobase + USBPORTSC1); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 113 | totalcount += count; |
| 114 | } |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 115 | port2 = inw(cntl->uhci.iobase + USBPORTSC2); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 116 | if (port2 & USBPORTSC_CCS) { |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 117 | outw(USBPORTSC_PE, cntl->uhci.iobase + USBPORTSC2); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 118 | int count = configure_usb_device(cntl, !!(port2 & USBPORTSC_LSDA)); |
| 119 | if (! count) |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 120 | outw(0, cntl->uhci.iobase + USBPORTSC2); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 121 | totalcount += count; |
| 122 | } |
| 123 | return totalcount; |
| 124 | } |
| 125 | |
Kevin O'Connor | a5826b5 | 2009-10-24 17:57:29 -0400 | [diff] [blame] | 126 | void |
| 127 | uhci_init(void *data) |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 128 | { |
| 129 | if (! CONFIG_USB_UHCI) |
Kevin O'Connor | a5826b5 | 2009-10-24 17:57:29 -0400 | [diff] [blame] | 130 | return; |
| 131 | struct usb_s *cntl = data; |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 132 | |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 133 | // XXX - don't call pci_config_XXX from a thread |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 134 | cntl->type = USB_TYPE_UHCI; |
| 135 | cntl->uhci.iobase = (pci_config_readl(cntl->bdf, PCI_BASE_ADDRESS_4) |
| 136 | & PCI_BASE_ADDRESS_IO_MASK); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 137 | |
| 138 | dprintf(3, "UHCI init on dev %02x:%02x.%x (io=%x)\n" |
| 139 | , pci_bdf_to_bus(cntl->bdf), pci_bdf_to_dev(cntl->bdf) |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 140 | , pci_bdf_to_fn(cntl->bdf), cntl->uhci.iobase); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 141 | |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 142 | pci_config_maskw(cntl->bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 143 | |
| 144 | reset_uhci(cntl); |
| 145 | configure_uhci(cntl); |
| 146 | start_uhci(cntl); |
| 147 | |
| 148 | int count = check_ports(cntl); |
| 149 | if (! count) { |
| 150 | // XXX - no devices; free data structures. |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 151 | } |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static int |
Kevin O'Connor | fcc116f | 2009-10-30 23:16:07 -0400 | [diff] [blame] | 155 | wait_qh(struct usb_s *cntl, struct uhci_qh *qh) |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 156 | { |
| 157 | // XXX - 500ms just a guess |
| 158 | u64 end = calc_future_tsc(500); |
| 159 | for (;;) { |
| 160 | if (qh->element & UHCI_PTR_TERM) |
| 161 | return 0; |
Kevin O'Connor | 89eb624 | 2009-10-22 22:30:37 -0400 | [diff] [blame] | 162 | if (check_time(end)) { |
Kevin O'Connor | fcc116f | 2009-10-30 23:16:07 -0400 | [diff] [blame] | 163 | struct uhci_td *td = (void*)(qh->element & ~UHCI_PTR_BITS); |
| 164 | dprintf(1, "Timeout on wait_qh %p (td=%p s=%x c=%x/%x)\n" |
| 165 | , qh, td, td->status |
| 166 | , inw(cntl->uhci.iobase + USBCMD) |
| 167 | , inw(cntl->uhci.iobase + USBSTS)); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 168 | return -1; |
| 169 | } |
Kevin O'Connor | 229e3be | 2009-10-31 13:55:59 -0400 | [diff] [blame] | 170 | yield(); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | int |
| 175 | uhci_control(u32 endp, int dir, const void *cmd, int cmdsize |
| 176 | , void *data, int datasize) |
| 177 | { |
| 178 | if (! CONFIG_USB_UHCI) |
Kevin O'Connor | 1c46a54 | 2009-10-17 23:53:32 -0400 | [diff] [blame] | 179 | return -1; |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 180 | |
| 181 | dprintf(5, "uhci_control %x\n", endp); |
| 182 | struct usb_s *cntl = endp2cntl(endp); |
| 183 | int maxpacket = endp2maxsize(endp); |
| 184 | int lowspeed = endp2speed(endp); |
| 185 | int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7); |
| 186 | |
| 187 | // Setup transfer descriptors |
| 188 | int count = 2 + DIV_ROUND_UP(datasize, maxpacket); |
| 189 | struct uhci_td *tds = malloc_tmphigh(sizeof(*tds) * count); |
| 190 | |
| 191 | tds[0].link = (u32)&tds[1] | UHCI_PTR_DEPTH; |
| 192 | tds[0].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0) |
| 193 | | TD_CTRL_ACTIVE); |
| 194 | tds[0].token = (uhci_explen(cmdsize) | (devaddr << TD_TOKEN_DEVADDR_SHIFT) |
| 195 | | USB_PID_SETUP); |
| 196 | tds[0].buffer = (void*)cmd; |
| 197 | int toggle = TD_TOKEN_TOGGLE; |
| 198 | int i; |
| 199 | for (i=1; i<count-1; i++) { |
| 200 | tds[i].link = (u32)&tds[i+1] | UHCI_PTR_DEPTH; |
| 201 | tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0) |
| 202 | | TD_CTRL_ACTIVE); |
| 203 | int len = (i == count-2 ? (datasize - (i-1)*maxpacket) : maxpacket); |
| 204 | tds[i].token = (uhci_explen(len) | toggle |
| 205 | | (devaddr << TD_TOKEN_DEVADDR_SHIFT) |
| 206 | | (dir ? USB_PID_IN : USB_PID_OUT)); |
| 207 | tds[i].buffer = data + (i-1) * maxpacket; |
| 208 | toggle ^= TD_TOKEN_TOGGLE; |
| 209 | } |
| 210 | tds[i].link = UHCI_PTR_TERM; |
| 211 | tds[i].status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0) |
| 212 | | TD_CTRL_ACTIVE); |
| 213 | tds[i].token = (uhci_explen(0) | TD_TOKEN_TOGGLE |
| 214 | | (devaddr << TD_TOKEN_DEVADDR_SHIFT) |
| 215 | | (dir ? USB_PID_OUT : USB_PID_IN)); |
| 216 | tds[i].buffer = 0; |
| 217 | |
| 218 | // Transfer data |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 219 | struct uhci_qh *data_qh = cntl->uhci.qh; |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 220 | data_qh->element = (u32)&tds[0]; |
Kevin O'Connor | fcc116f | 2009-10-30 23:16:07 -0400 | [diff] [blame] | 221 | int ret = wait_qh(cntl, data_qh); |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 222 | if (ret) { |
| 223 | data_qh->element = UHCI_PTR_TERM; |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 224 | // XXX - leak tds |
| 225 | return ret; |
Kevin O'Connor | ad90159 | 2009-12-13 11:25:25 -0500 | [diff] [blame] | 226 | } |
| 227 | free(tds); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 231 | struct usb_pipe * |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 232 | uhci_alloc_intr_pipe(u32 endp, int period) |
| 233 | { |
| 234 | if (! CONFIG_USB_UHCI) |
| 235 | return NULL; |
| 236 | |
| 237 | dprintf(7, "uhci_alloc_intr_pipe %x %d\n", endp, period); |
| 238 | struct usb_s *cntl = endp2cntl(endp); |
| 239 | int maxpacket = endp2maxsize(endp); |
| 240 | int lowspeed = endp2speed(endp); |
| 241 | int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7); |
| 242 | // XXX - just grab 20 for now. |
| 243 | int count = 20; |
| 244 | struct uhci_qh *qh = malloc_low(sizeof(*qh)); |
| 245 | struct uhci_td *tds = malloc_low(sizeof(*tds) * count); |
| 246 | if (!qh || !tds) |
| 247 | return NULL; |
| 248 | if (maxpacket > sizeof(tds[0].data)) |
| 249 | // XXX - free qh/tds |
| 250 | return NULL; |
| 251 | |
| 252 | qh->element = (u32)tds; |
| 253 | int toggle = 0; |
| 254 | int i; |
| 255 | for (i=0; i<count; i++) { |
| 256 | tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]); |
| 257 | tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0) |
| 258 | | TD_CTRL_ACTIVE); |
| 259 | tds[i].token = (uhci_explen(maxpacket) | toggle |
| 260 | | (devaddr << TD_TOKEN_DEVADDR_SHIFT) |
| 261 | | USB_PID_IN); |
| 262 | tds[i].buffer = &tds[i].data; |
| 263 | toggle ^= TD_TOKEN_TOGGLE; |
| 264 | } |
| 265 | |
| 266 | qh->next_td = &tds[0]; |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 267 | qh->pipe.endp = endp; |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 268 | |
| 269 | // XXX - need schedule - just add to primary list for now. |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 270 | struct uhci_qh *data_qh = cntl->uhci.qh; |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 271 | qh->link = data_qh->link; |
| 272 | data_qh->link = (u32)qh | UHCI_PTR_QH; |
| 273 | |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 274 | return &qh->pipe; |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | int |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 278 | uhci_poll_intr(struct usb_pipe *pipe, void *data) |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 279 | { |
| 280 | ASSERT16(); |
| 281 | if (! CONFIG_USB_UHCI) |
| 282 | return -1; |
| 283 | |
Kevin O'Connor | 59f0283 | 2009-10-12 10:09:15 -0400 | [diff] [blame] | 284 | struct uhci_qh *qh = container_of(pipe, struct uhci_qh, pipe); |
Kevin O'Connor | 114592f | 2009-09-28 21:32:08 -0400 | [diff] [blame] | 285 | struct uhci_td *td = GET_FLATPTR(qh->next_td); |
| 286 | u32 status = GET_FLATPTR(td->status); |
| 287 | u32 token = GET_FLATPTR(td->token); |
| 288 | if (status & TD_CTRL_ACTIVE) |
| 289 | // No intrs found. |
| 290 | return -1; |
| 291 | // XXX - check for errors. |
| 292 | |
| 293 | // Copy data. |
| 294 | memcpy_far(GET_SEG(SS), data |
| 295 | , FLATPTR_TO_SEG(td->data), (void*)FLATPTR_TO_OFFSET(td->data) |
| 296 | , uhci_expected_length(token)); |
| 297 | |
| 298 | // Reenable this td. |
| 299 | u32 next = GET_FLATPTR(td->link); |
| 300 | SET_FLATPTR(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS) |
| 301 | | TD_CTRL_ACTIVE)); |
| 302 | SET_FLATPTR(qh->next_td, (void*)(next & ~UHCI_PTR_BITS)); |
| 303 | |
| 304 | return 0; |
| 305 | } |