You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

853 lines
27KB

  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <Arduino.h>
  24. #include "USBHost.h"
  25. #define PERIODIC_LIST_SIZE 32
  26. static uint32_t periodictable[PERIODIC_LIST_SIZE] __attribute__ ((aligned(4096), used));
  27. static uint8_t uframe_bandwidth[PERIODIC_LIST_SIZE*8];
  28. static uint8_t port_state;
  29. #define PORT_STATE_DISCONNECTED 0
  30. #define PORT_STATE_DEBOUNCE 1
  31. #define PORT_STATE_RESET 2
  32. #define PORT_STATE_RECOVERY 3
  33. #define PORT_STATE_ACTIVE 4
  34. static Device_t *rootdev=NULL;
  35. static Transfer_t *async_followup_first=NULL;
  36. static Transfer_t *async_followup_last=NULL;
  37. static Transfer_t *periodic_followup_first=NULL;
  38. static Transfer_t *periodic_followup_last=NULL;
  39. static void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,
  40. uint32_t pid, uint32_t data01, bool irq);
  41. static bool followup_Transfer(Transfer_t *transfer);
  42. static void add_to_async_followup_list(Transfer_t *first, Transfer_t *last);
  43. static void remove_from_async_followup_list(Transfer_t *transfer);
  44. static void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last);
  45. static void remove_from_periodic_followup_list(Transfer_t *transfer);
  46. void USBHost::begin()
  47. {
  48. // Teensy 3.6 has USB host power controlled by PTE6
  49. PORTE_PCR6 = PORT_PCR_MUX(1);
  50. GPIOE_PDDR |= (1<<6);
  51. GPIOE_PSOR = (1<<6); // turn on USB host power
  52. delay(10);
  53. println("sizeof Device = ", sizeof(Device_t));
  54. println("sizeof Pipe = ", sizeof(Pipe_t));
  55. println("sizeof Transfer = ", sizeof(Transfer_t));
  56. // configure the MPU to allow USBHS DMA to access memory
  57. MPU_RGDAAC0 |= 0x30000000;
  58. //println("MPU_RGDAAC0 = ", MPU_RGDAAC0, HEX);
  59. // turn on clocks
  60. MCG_C1 |= MCG_C1_IRCLKEN; // enable MCGIRCLK 32kHz
  61. OSC0_CR |= OSC_ERCLKEN;
  62. SIM_SOPT2 |= SIM_SOPT2_USBREGEN; // turn on USB regulator
  63. SIM_SOPT2 &= ~SIM_SOPT2_USBSLSRC; // use IRC for slow clock
  64. println("power up USBHS PHY");
  65. SIM_USBPHYCTL |= SIM_USBPHYCTL_USBDISILIM; // disable USB current limit
  66. //SIM_USBPHYCTL = SIM_USBPHYCTL_USBDISILIM | SIM_USBPHYCTL_USB3VOUTTRG(6); // pg 237
  67. SIM_SCGC3 |= SIM_SCGC3_USBHSDCD | SIM_SCGC3_USBHSPHY | SIM_SCGC3_USBHS;
  68. USBHSDCD_CLOCK = 33 << 2;
  69. //print("init USBHS PHY & PLL");
  70. // init process: page 1681-1682
  71. USBPHY_CTRL_CLR = (USBPHY_CTRL_SFTRST | USBPHY_CTRL_CLKGATE); // // CTRL pg 1698
  72. USBPHY_CTRL_SET = USBPHY_CTRL_ENUTMILEVEL2 | USBPHY_CTRL_ENUTMILEVEL3;
  73. //USBPHY_CTRL_SET = USBPHY_CTRL_FSDLL_RST_EN; // TODO: what does this do??
  74. USBPHY_TRIM_OVERRIDE_EN_SET = 1;
  75. USBPHY_PLL_SIC = USBPHY_PLL_SIC_PLL_POWER | USBPHY_PLL_SIC_PLL_ENABLE |
  76. USBPHY_PLL_SIC_PLL_DIV_SEL(1) | USBPHY_PLL_SIC_PLL_EN_USB_CLKS;
  77. // wait for the PLL to lock
  78. int count=0;
  79. while ((USBPHY_PLL_SIC & USBPHY_PLL_SIC_PLL_LOCK) == 0) {
  80. count++;
  81. }
  82. //println("PLL locked, waited ", count);
  83. // turn on power to PHY
  84. USBPHY_PWD = 0;
  85. delay(10);
  86. // sanity check, connect 470K pullup & 100K pulldown and watch D+ voltage change
  87. //USBPHY_ANACTRL_CLR = (1<<10); // turn off both 15K pulldowns... works! :)
  88. // sanity check, output clocks on pin 9 for testing
  89. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(3); // LPO 1kHz
  90. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(2); // Flash
  91. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(6); // XTAL
  92. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(7); // IRC 48MHz
  93. //SIM_SOPT2 = SIM_SOPT2 & (~SIM_SOPT2_CLKOUTSEL(7)) | SIM_SOPT2_CLKOUTSEL(4); // MCGIRCLK
  94. //CORE_PIN9_CONFIG = PORT_PCR_MUX(5); // CLKOUT on PTC3 Alt5 (Arduino pin 9)
  95. // now with the PHY up and running, start up USBHS
  96. //print("begin ehci reset");
  97. USBHS_USBCMD |= USBHS_USBCMD_RST;
  98. //count = 0;
  99. while (USBHS_USBCMD & USBHS_USBCMD_RST) {
  100. //count++;
  101. }
  102. //println(" reset waited ", count);
  103. init_Device_Pipe_Transfer_memory();
  104. for (int i=0; i < 32; i++) {
  105. periodictable[i] = 1;
  106. }
  107. memset(uframe_bandwidth, 0, sizeof(uframe_bandwidth));
  108. port_state = PORT_STATE_DISCONNECTED;
  109. USBHS_USB_SBUSCFG = 1; // System Bus Interface Configuration
  110. // turn on the USBHS controller
  111. //USBHS_USBMODE = USBHS_USBMODE_TXHSD(5) | USBHS_USBMODE_CM(3); // host mode
  112. USBHS_USBMODE = USBHS_USBMODE_CM(3); // host mode
  113. USBHS_USBINTR = 0;
  114. USBHS_PERIODICLISTBASE = (uint32_t)periodictable;
  115. USBHS_FRINDEX = 0;
  116. USBHS_ASYNCLISTADDR = 0;
  117. USBHS_USBCMD = USBHS_USBCMD_ITC(8) | USBHS_USBCMD_RS |
  118. USBHS_USBCMD_ASP(3) | USBHS_USBCMD_ASPE | USBHS_USBCMD_PSE |
  119. #if PERIODIC_LIST_SIZE == 8
  120. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(3);
  121. #elif PERIODIC_LIST_SIZE == 16
  122. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(2);
  123. #elif PERIODIC_LIST_SIZE == 32
  124. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(1);
  125. #elif PERIODIC_LIST_SIZE == 64
  126. USBHS_USBCMD_FS2 | USBHS_USBCMD_FS(0);
  127. #elif PERIODIC_LIST_SIZE == 128
  128. USBHS_USBCMD_FS(3);
  129. #elif PERIODIC_LIST_SIZE == 256
  130. USBHS_USBCMD_FS(2);
  131. #elif PERIODIC_LIST_SIZE == 512
  132. USBHS_USBCMD_FS(1);
  133. #elif PERIODIC_LIST_SIZE == 1024
  134. USBHS_USBCMD_FS(0);
  135. #else
  136. #error "Unsupported PERIODIC_LIST_SIZE"
  137. #endif
  138. // turn on the USB port
  139. //USBHS_PORTSC1 = USBHS_PORTSC_PP;
  140. USBHS_PORTSC1 |= USBHS_PORTSC_PP;
  141. //USBHS_PORTSC1 |= USBHS_PORTSC_PFSC; // force 12 Mbit/sec
  142. //USBHS_PORTSC1 |= USBHS_PORTSC_PHCD; // phy off
  143. //println("USBHS_ASYNCLISTADDR = ", USBHS_ASYNCLISTADDR, HEX);
  144. //println("USBHS_PERIODICLISTBASE = ", USBHS_PERIODICLISTBASE, HEX);
  145. //println("periodictable = ", (uint32_t)periodictable, HEX);
  146. // enable interrupts, after this point interruts to all the work
  147. attachInterruptVector(IRQ_USBHS, isr);
  148. NVIC_ENABLE_IRQ(IRQ_USBHS);
  149. USBHS_USBINTR = USBHS_USBINTR_PCE | USBHS_USBINTR_TIE0;
  150. USBHS_USBINTR |= USBHS_USBINTR_UEE | USBHS_USBINTR_SEE;
  151. USBHS_USBINTR |= USBHS_USBINTR_AAE;
  152. USBHS_USBINTR |= USBHS_USBINTR_UPIE | USBHS_USBINTR_UAIE;
  153. }
  154. // EHCI registers page default
  155. // -------------- ---- -------
  156. // USBHS_USBCMD 1599 00080000 USB Command
  157. // USBHS_USBSTS 1602 00000000 USB Status
  158. // USBHS_USBINTR 1606 00000000 USB Interrupt Enable
  159. // USBHS_FRINDEX 1609 00000000 Frame Index Register
  160. // USBHS_PERIODICLISTBASE 1610 undefine Periodic Frame List Base Address
  161. // USBHS_ASYNCLISTADDR 1612 undefine Asynchronous List Address
  162. // USBHS_PORTSC1 1619 00002000 Port Status and Control
  163. // USBHS_USBMODE 1629 00005000 USB Mode
  164. // USBHS_GPTIMERnCTL 1591 00000000 General Purpose Timer n Control
  165. // PORT_STATE_DISCONNECTED 0
  166. // PORT_STATE_DEBOUNCE 1
  167. // PORT_STATE_RESET 2
  168. // PORT_STATE_RECOVERY 3
  169. // PORT_STATE_ACTIVE 4
  170. void USBHost::isr()
  171. {
  172. uint32_t stat = USBHS_USBSTS;
  173. USBHS_USBSTS = stat; // clear pending interrupts
  174. //stat &= USBHS_USBINTR; // mask away unwanted interrupts
  175. println();
  176. println("ISR: ", stat, HEX);
  177. //if (stat & USBHS_USBSTS_UI) println(" USB Interrupt");
  178. if (stat & USBHS_USBSTS_UEI) println(" USB Error");
  179. if (stat & USBHS_USBSTS_PCI) println(" Port Change");
  180. //if (stat & USBHS_USBSTS_FRI) println(" Frame List Rollover");
  181. if (stat & USBHS_USBSTS_SEI) println(" System Error");
  182. if (stat & USBHS_USBSTS_AAI) println(" Async Advance (doorbell)");
  183. if (stat & USBHS_USBSTS_URI) println(" Reset Recv");
  184. //if (stat & USBHS_USBSTS_SRI) println(" SOF");
  185. if (stat & USBHS_USBSTS_SLI) println(" Suspend");
  186. if (stat & USBHS_USBSTS_HCH) println(" Host Halted");
  187. //if (stat & USBHS_USBSTS_RCL) println(" Reclamation");
  188. //if (stat & USBHS_USBSTS_PS) println(" Periodic Sched En");
  189. //if (stat & USBHS_USBSTS_AS) println(" Async Sched En");
  190. if (stat & USBHS_USBSTS_NAKI) println(" NAK");
  191. if (stat & USBHS_USBSTS_UAI) println(" USB Async");
  192. if (stat & USBHS_USBSTS_UPI) println(" USB Periodic");
  193. if (stat & USBHS_USBSTS_TI0) println(" Timer0");
  194. if (stat & USBHS_USBSTS_TI1) println(" Timer1");
  195. if (stat & USBHS_USBSTS_UAI) { // completed qTD(s) from the async schedule
  196. println("Async Followup");
  197. //print(async_followup_first, async_followup_last);
  198. Transfer_t *p = async_followup_first;
  199. while (p) {
  200. if (followup_Transfer(p)) {
  201. // transfer completed
  202. Transfer_t *next = p->next_followup;
  203. remove_from_async_followup_list(p);
  204. free_Transfer(p);
  205. p = next;
  206. } else {
  207. // transfer still pending
  208. p = p->next_followup;
  209. }
  210. }
  211. //print(async_followup_first, async_followup_last);
  212. }
  213. if (stat & USBHS_USBSTS_UPI) { // completed qTD(s) from the periodic schedule
  214. println("Periodic Followup");
  215. Transfer_t *p = periodic_followup_first;
  216. while (p) {
  217. if (followup_Transfer(p)) {
  218. // transfer completed
  219. Transfer_t *next = p->next_followup;
  220. remove_from_periodic_followup_list(p);
  221. free_Transfer(p);
  222. p = next;
  223. } else {
  224. // transfer still pending
  225. p = p->next_followup;
  226. }
  227. }
  228. }
  229. if (stat & USBHS_USBSTS_PCI) { // port change detected
  230. const uint32_t portstat = USBHS_PORTSC1;
  231. println("port change: ", portstat, HEX);
  232. USBHS_PORTSC1 = portstat | (USBHS_PORTSC_OCC|USBHS_PORTSC_PEC|USBHS_PORTSC_CSC);
  233. if (portstat & USBHS_PORTSC_OCC) {
  234. println(" overcurrent change");
  235. }
  236. if (portstat & USBHS_PORTSC_CSC) {
  237. if (portstat & USBHS_PORTSC_CCS) {
  238. println(" connect");
  239. if (port_state == PORT_STATE_DISCONNECTED
  240. || port_state == PORT_STATE_DEBOUNCE) {
  241. // 100 ms debounce (USB 2.0: TATTDB, page 150 & 188)
  242. port_state = PORT_STATE_DEBOUNCE;
  243. USBHS_GPTIMER0LD = 100000; // microseconds
  244. USBHS_GPTIMER0CTL =
  245. USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  246. stat &= ~USBHS_USBSTS_TI0;
  247. }
  248. } else {
  249. println(" disconnect");
  250. port_state = PORT_STATE_DISCONNECTED;
  251. USBPHY_CTRL_CLR = USBPHY_CTRL_ENHOSTDISCONDETECT;
  252. disconnect_Device(rootdev);
  253. rootdev = NULL;
  254. }
  255. }
  256. if (portstat & USBHS_PORTSC_PEC) {
  257. // PEC bit only detects disable
  258. println(" disable");
  259. } else if (port_state == PORT_STATE_RESET && portstat & USBHS_PORTSC_PE) {
  260. println(" port enabled");
  261. port_state = PORT_STATE_RECOVERY;
  262. // 10 ms reset recover (USB 2.0: TRSTRCY, page 151 & 188)
  263. USBHS_GPTIMER0LD = 10000; // microseconds
  264. USBHS_GPTIMER0CTL = USBHS_GPTIMERCTL_RST | USBHS_GPTIMERCTL_RUN;
  265. if (USBHS_PORTSC1 & USBHS_PORTSC_HSP) {
  266. // turn on high-speed disconnect detector
  267. USBPHY_CTRL_SET = USBPHY_CTRL_ENHOSTDISCONDETECT;
  268. }
  269. }
  270. if (portstat & USBHS_PORTSC_FPR) {
  271. println(" force resume");
  272. }
  273. }
  274. if (stat & USBHS_USBSTS_TI0) { // timer 0
  275. println("timer");
  276. if (port_state == PORT_STATE_DEBOUNCE) {
  277. port_state = PORT_STATE_RESET;
  278. USBHS_PORTSC1 |= USBHS_PORTSC_PR; // begin reset sequence
  279. println(" begin reset");
  280. } else if (port_state == PORT_STATE_RECOVERY) {
  281. port_state = PORT_STATE_ACTIVE;
  282. println(" end recovery");
  283. // HCSPARAMS TTCTRL page 1671
  284. uint32_t speed = (USBHS_PORTSC1 >> 26) & 3;
  285. rootdev = new_Device(speed, 0, 0);
  286. }
  287. }
  288. }
  289. static uint32_t QH_capabilities1(uint32_t nak_count_reload, uint32_t control_endpoint_flag,
  290. uint32_t max_packet_length, uint32_t head_of_list, uint32_t data_toggle_control,
  291. uint32_t speed, uint32_t endpoint_number, uint32_t inactivate, uint32_t address)
  292. {
  293. return ( (nak_count_reload << 28) | (control_endpoint_flag << 27) |
  294. (max_packet_length << 16) | (head_of_list << 15) |
  295. (data_toggle_control << 14) | (speed << 12) | (endpoint_number << 8) |
  296. (inactivate << 7) | (address << 0) );
  297. }
  298. static uint32_t QH_capabilities2(uint32_t high_bw_mult, uint32_t hub_port_number,
  299. uint32_t hub_address, uint32_t split_completion_mask, uint32_t interrupt_schedule_mask)
  300. {
  301. return ( (high_bw_mult << 30) | (hub_port_number << 23) | (hub_address << 16) |
  302. (split_completion_mask << 8) | (interrupt_schedule_mask << 0) );
  303. }
  304. // Create a new pipe. It's QH is added to the async or periodic schedule,
  305. // and a halt qTD is added to the QH, so we can grow the qTD list later.
  306. // dev: device owning this pipe/endpoint
  307. // type: 0=control, 2=bulk, 3=interrupt
  308. // endpoint: 0 for control, 1-15 for bulk or interrupt
  309. // direction: 0=OUT, 1=IN (unused for control)
  310. // maxlen: maximum packet size
  311. // interval: polling interval for interrupt, power of 2, unused if control or bulk
  312. //
  313. Pipe_t * USBHost::new_Pipe(Device_t *dev, uint32_t type, uint32_t endpoint,
  314. uint32_t direction, uint32_t maxlen, uint32_t interval)
  315. {
  316. Pipe_t *pipe;
  317. Transfer_t *halt;
  318. uint32_t c=0, dtc=0, smask=0, cmask=0, offset=0;
  319. println("new_Pipe");
  320. pipe = allocate_Pipe();
  321. if (!pipe) return NULL;
  322. halt = allocate_Transfer();
  323. if (!halt) {
  324. free_Pipe(pipe);
  325. return NULL;
  326. }
  327. if (type == 3) {
  328. // interrupt transfers require bandwidth & microframe scheduling
  329. if (interval > PERIODIC_LIST_SIZE*8) interval = PERIODIC_LIST_SIZE*8;
  330. if (dev->speed < 2 && interval < 8) interval = 8;
  331. if (!allocate_interrupt_pipe_bandwidth(dev->speed,
  332. maxlen, interval, direction, &offset, &smask, &cmask)) {
  333. free_Transfer(halt);
  334. free_Pipe(pipe);
  335. return NULL;
  336. }
  337. }
  338. memset(pipe, 0, sizeof(Pipe_t));
  339. if (endpoint > 0) {
  340. // if non-control pipe, update dev->data_pipes list
  341. Pipe_t *p = dev->data_pipes;
  342. if (p == NULL) {
  343. dev->data_pipes = pipe;
  344. } else {
  345. while (p->next) p = p->next;
  346. p->next = pipe;
  347. }
  348. }
  349. memset(halt, 0, sizeof(Transfer_t));
  350. halt->qtd.next = 1;
  351. halt->qtd.token = 0x40;
  352. pipe->device = dev;
  353. pipe->qh.next = (uint32_t)halt;
  354. pipe->qh.alt_next = 1;
  355. pipe->direction = direction;
  356. pipe->type = type;
  357. if (type == 0) {
  358. // control
  359. if (dev->speed < 2) c = 1;
  360. dtc = 1;
  361. } else if (type == 2) {
  362. // bulk
  363. } else if (type == 3) {
  364. // interrupt
  365. }
  366. pipe->qh.capabilities[0] = QH_capabilities1(15, c, maxlen, 0,
  367. dtc, dev->speed, endpoint, 0, dev->address);
  368. pipe->qh.capabilities[1] = QH_capabilities2(1, dev->hub_port,
  369. dev->hub_address, cmask, smask);
  370. if (type == 0 || type == 2) {
  371. // control or bulk: add to async queue
  372. Pipe_t *list = (Pipe_t *)USBHS_ASYNCLISTADDR;
  373. if (list == NULL) {
  374. pipe->qh.capabilities[0] |= 0x8000; // H bit
  375. pipe->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2; // 2=QH
  376. USBHS_ASYNCLISTADDR = (uint32_t)&(pipe->qh);
  377. USBHS_USBCMD |= USBHS_USBCMD_ASE; // enable async schedule
  378. //println(" first in async list");
  379. } else {
  380. // EHCI 1.0: section 4.8.1, page 72
  381. pipe->qh.horizontal_link = list->qh.horizontal_link;
  382. list->qh.horizontal_link = (uint32_t)&(pipe->qh) | 2;
  383. //println(" added to async list");
  384. }
  385. } else if (type == 3) {
  386. // interrupt: add to periodic schedule
  387. // TODO: link it into the periodic table
  388. // TODO: built tree...
  389. //uint32_t finterval = interval >> 3;
  390. //for (uint32_t i=offset; i < PERIODIC_LIST_SIZE; i += finterval) {
  391. // uint32_t list = periodictable[i];
  392. //}
  393. // quick hack for testing, just put it into the first table entry
  394. pipe->qh.horizontal_link = periodictable[0];
  395. periodictable[0] = (uint32_t)&(pipe->qh) | 2; // 2=QH
  396. println("init periodictable with ", periodictable[0], HEX);
  397. }
  398. return pipe;
  399. }
  400. // Fill in the qTD fields (token & data)
  401. // t the Transfer qTD to initialize
  402. // buf data to transfer
  403. // len length of data
  404. // pid type of packet: 0=OUT, 1=IN, 2=SETUP
  405. // data01 value of DATA0/DATA1 toggle on 1st packet
  406. // irq whether to generate an interrupt when transfer complete
  407. //
  408. static void init_qTD(volatile Transfer_t *t, void *buf, uint32_t len,
  409. uint32_t pid, uint32_t data01, bool irq)
  410. {
  411. t->qtd.alt_next = 1; // 1=terminate
  412. if (data01) data01 = 0x80000000;
  413. t->qtd.token = data01 | (len << 16) | (irq ? 0x8000 : 0) | (pid << 8) | 0x80;
  414. uint32_t addr = (uint32_t)buf;
  415. t->qtd.buffer[0] = addr;
  416. addr &= 0xFFFFF000;
  417. t->qtd.buffer[1] = addr + 0x1000;
  418. t->qtd.buffer[2] = addr + 0x2000;
  419. t->qtd.buffer[3] = addr + 0x3000;
  420. t->qtd.buffer[4] = addr + 0x4000;
  421. }
  422. // Create a Control Transfer and queue it
  423. //
  424. bool USBHost::queue_Control_Transfer(Device_t *dev, setup_t *setup, void *buf, USBDriver *driver)
  425. {
  426. Transfer_t *transfer, *data, *status;
  427. uint32_t status_direction;
  428. println("new_Control_Transfer");
  429. if (setup->wLength > 16384) return false; // max 16K data for control
  430. transfer = allocate_Transfer();
  431. if (!transfer) return false;
  432. status = allocate_Transfer();
  433. if (!status) {
  434. free_Transfer(transfer);
  435. return false;
  436. }
  437. if (setup->wLength > 0) {
  438. data = allocate_Transfer();
  439. if (!data) {
  440. free_Transfer(transfer);
  441. free_Transfer(status);
  442. return false;
  443. }
  444. uint32_t pid = (setup->bmRequestType & 0x80) ? 1 : 0;
  445. init_qTD(data, buf, setup->wLength, pid, 1, false);
  446. transfer->qtd.next = (uint32_t)data;
  447. data->qtd.next = (uint32_t)status;
  448. status_direction = pid ^ 1;
  449. } else {
  450. transfer->qtd.next = (uint32_t)status;
  451. status_direction = 1; // always IN, USB 2.0 page 226
  452. }
  453. //println("setup address ", (uint32_t)setup, HEX);
  454. init_qTD(transfer, setup, 8, 2, 0, false);
  455. init_qTD(status, NULL, 0, status_direction, 1, true);
  456. status->pipe = dev->control_pipe;
  457. status->buffer = buf;
  458. status->length = setup->wLength;
  459. status->setup = setup;
  460. status->driver = driver;
  461. status->qtd.next = 1;
  462. return queue_Transfer(dev->control_pipe, transfer);
  463. }
  464. // Create a Bulk or Interrupt Transfer and queue it
  465. //
  466. bool USBHost::queue_Data_Transfer(Pipe_t *pipe, void *buffer, uint32_t len, USBDriver *driver)
  467. {
  468. Transfer_t *transfer, *data, *next;
  469. uint8_t *p = (uint8_t *)buffer;
  470. uint32_t count;
  471. bool last = false;
  472. // TODO: option for zero length packet? Maybe in Pipe_t fields?
  473. println("new_Data_Transfer");
  474. // allocate qTDs
  475. transfer = allocate_Transfer();
  476. if (!transfer) return false;
  477. data = transfer;
  478. for (count=(len >> 14); count; count--) {
  479. next = allocate_Transfer();
  480. if (!next) {
  481. // free already-allocated qTDs
  482. while (1) {
  483. next = (Transfer_t *)transfer->qtd.next;
  484. free_Transfer(transfer);
  485. if (transfer == data) break;
  486. transfer = next;
  487. }
  488. return false;
  489. }
  490. data->qtd.next = (uint32_t)next;
  491. data = next;
  492. }
  493. // last qTD needs info for followup
  494. data->qtd.next = 1;
  495. data->pipe = pipe;
  496. data->buffer = buffer;
  497. data->length = len;
  498. data->setup = NULL;
  499. data->driver = driver;
  500. // initialize all qTDs
  501. data = transfer;
  502. while (1) {
  503. uint32_t count = len;
  504. if (count > 16384) {
  505. count = 16384;
  506. } else {
  507. last = true;
  508. }
  509. init_qTD(data, p, count, pipe->direction, 0, last);
  510. if (last) break;
  511. p += count;
  512. len -= count;
  513. data = (Transfer_t *)(data->qtd.next);
  514. }
  515. return queue_Transfer(pipe, transfer);
  516. }
  517. bool USBHost::queue_Transfer(Pipe_t *pipe, Transfer_t *transfer)
  518. {
  519. // find halt qTD
  520. Transfer_t *halt = (Transfer_t *)(pipe->qh.next);
  521. while (!(halt->qtd.token & 0x40)) halt = (Transfer_t *)(halt->qtd.next);
  522. // transfer's token
  523. uint32_t token = transfer->qtd.token;
  524. // transfer becomes new halt qTD
  525. transfer->qtd.token = 0x40;
  526. // copy transfer non-token fields to halt
  527. halt->qtd.next = transfer->qtd.next;
  528. halt->qtd.alt_next = transfer->qtd.alt_next;
  529. halt->qtd.buffer[0] = transfer->qtd.buffer[0]; // TODO: optimize memcpy, all
  530. halt->qtd.buffer[1] = transfer->qtd.buffer[1]; // fields except token
  531. halt->qtd.buffer[2] = transfer->qtd.buffer[2];
  532. halt->qtd.buffer[3] = transfer->qtd.buffer[3];
  533. halt->qtd.buffer[4] = transfer->qtd.buffer[4];
  534. halt->pipe = pipe;
  535. halt->buffer = transfer->buffer;
  536. halt->length = transfer->length;
  537. halt->setup = transfer->setup;
  538. halt->driver = transfer->driver;
  539. // find the last qTD we're adding
  540. Transfer_t *last = halt;
  541. while ((uint32_t)(last->qtd.next) != 1) last = (Transfer_t *)(last->qtd.next);
  542. // last points to transfer (which becomes new halt)
  543. last->qtd.next = (uint32_t)transfer;
  544. transfer->qtd.next = 1;
  545. // link all the new qTD by next_followup & prev_followup
  546. Transfer_t *prev = NULL;
  547. Transfer_t *p = halt;
  548. while (p->qtd.next != (uint32_t)transfer) {
  549. Transfer_t *next = (Transfer_t *)p->qtd.next;
  550. p->prev_followup = prev;
  551. p->next_followup = next;
  552. prev = p;
  553. p = next;
  554. }
  555. p->prev_followup = prev;
  556. p->next_followup = NULL;
  557. //print(halt, p);
  558. // add them to a followup list
  559. if (pipe->type == 0 || pipe->type == 2) {
  560. // control or bulk
  561. add_to_async_followup_list(halt, p);
  562. } else {
  563. // interrupt
  564. add_to_periodic_followup_list(halt, p);
  565. }
  566. // old halt becomes new transfer, this commits all new qTDs to QH
  567. halt->qtd.token = token;
  568. return true;
  569. }
  570. static bool followup_Transfer(Transfer_t *transfer)
  571. {
  572. //println(" Followup ", (uint32_t)transfer, HEX);
  573. if (!(transfer->qtd.token & 0x80)) {
  574. // TODO: check error status
  575. if (transfer->qtd.token & 0x8000) {
  576. // this transfer caused an interrupt
  577. if (transfer->pipe->callback_function) {
  578. // do the callback
  579. (*(transfer->pipe->callback_function))(transfer);
  580. }
  581. }
  582. // do callback function...
  583. //println(" completed");
  584. return true;
  585. }
  586. return false;
  587. }
  588. static void add_to_async_followup_list(Transfer_t *first, Transfer_t *last)
  589. {
  590. last->next_followup = NULL; // always add to end of list
  591. if (async_followup_last == NULL) {
  592. first->prev_followup = NULL;
  593. async_followup_first = first;
  594. } else {
  595. first->prev_followup = async_followup_last;
  596. async_followup_last->next_followup = first;
  597. }
  598. async_followup_last = last;
  599. }
  600. static void remove_from_async_followup_list(Transfer_t *transfer)
  601. {
  602. Transfer_t *next = transfer->next_followup;
  603. Transfer_t *prev = transfer->prev_followup;
  604. if (prev) {
  605. prev->next_followup = next;
  606. } else {
  607. async_followup_first = next;
  608. }
  609. if (next) {
  610. next->prev_followup = prev;
  611. } else {
  612. async_followup_last = prev;
  613. }
  614. }
  615. static void add_to_periodic_followup_list(Transfer_t *first, Transfer_t *last)
  616. {
  617. last->next_followup = NULL; // always add to end of list
  618. if (periodic_followup_last == NULL) {
  619. first->prev_followup = NULL;
  620. periodic_followup_first = first;
  621. } else {
  622. first->prev_followup = periodic_followup_last;
  623. periodic_followup_last->next_followup = first;
  624. }
  625. periodic_followup_last = last;
  626. }
  627. static void remove_from_periodic_followup_list(Transfer_t *transfer)
  628. {
  629. Transfer_t *next = transfer->next_followup;
  630. Transfer_t *prev = transfer->prev_followup;
  631. if (prev) {
  632. prev->next_followup = next;
  633. } else {
  634. periodic_followup_first = next;
  635. }
  636. if (next) {
  637. next->prev_followup = prev;
  638. } else {
  639. periodic_followup_last = prev;
  640. }
  641. }
  642. static uint32_t max4(uint32_t n1, uint32_t n2, uint32_t n3, uint32_t n4)
  643. {
  644. if (n1 > n2) {
  645. // can't be n2
  646. if (n1 > n3) {
  647. // can't be n3
  648. if (n1 > n4) return n1;
  649. } else {
  650. // can't be n1
  651. if (n3 > n4) return n3;
  652. }
  653. } else {
  654. // can't be n1
  655. if (n2 > n3) {
  656. // can't be n3
  657. if (n2 > n4) return n2;
  658. } else {
  659. // can't be n2
  660. if (n3 > n4) return n3;
  661. }
  662. }
  663. return n4;
  664. }
  665. // Allocate bandwidth for an interrupt pipe. Given the packet size
  666. // and other parameters, find the best place to schedule this pipe.
  667. // Returns true if enough bandwidth is available, and the best
  668. // frame offset, smask and cmask. Or returns false if no group
  669. // of microframes has enough bandwidth available.
  670. //
  671. // speed: [in] 0=full speed, 1=low speed, 2=high speed
  672. // maxlen: [in] maximum packet length
  673. // interval: [in] polling interval, in 125 us micro frames
  674. // direction: [in] 0=OUT, 1=IN
  675. // offset: [out] frame offset, 0 to PERIODIC_LIST_SIZE-1
  676. // smask: [out] Start Mask
  677. // cmask: [out] Complete Mask
  678. //
  679. bool USBHost::allocate_interrupt_pipe_bandwidth(uint32_t speed, uint32_t maxlen,
  680. uint32_t interval, uint32_t direction, uint32_t *offset_out,
  681. uint32_t *smask_out, uint32_t *cmask_out)
  682. {
  683. println("allocate_interrupt_pipe_bandwidth");
  684. maxlen = (maxlen * 76459) >> 16; // worst case bit stuffing
  685. if (speed == 2) {
  686. // high speed 480 Mbit/sec
  687. uint32_t stime = (55 + 32 + maxlen) >> 5; // time units: 32 bytes or 533 ns
  688. uint32_t min_offset = 0xFFFFFFFF;
  689. uint32_t min_bw = 0xFFFFFFFF;
  690. for (uint32_t offset=0; offset < interval; offset++) {
  691. uint32_t max_bw = 0;
  692. for (uint32_t i=offset; i < PERIODIC_LIST_SIZE*8; i += interval) {
  693. uint32_t bw = uframe_bandwidth[i] + stime;
  694. if (bw > max_bw) max_bw = bw;
  695. }
  696. if (max_bw < min_bw) {
  697. min_bw = max_bw;
  698. min_offset = offset;
  699. }
  700. }
  701. print(" min_bw = ");
  702. print(min_bw);
  703. print(", at offset = ");
  704. println(min_offset);
  705. if (min_bw > 187) return false;
  706. for (uint32_t i=min_offset; i < PERIODIC_LIST_SIZE*8; i += interval) {
  707. uframe_bandwidth[i] += stime;
  708. }
  709. *offset_out = min_offset >> 3;
  710. if (interval == 1) {
  711. *smask_out = 0xFF;
  712. } else if (interval == 2) {
  713. *smask_out = 0x55 << (min_offset & 1);
  714. } else if (interval <= 4) {
  715. *smask_out = 0x11 << (min_offset & 3);
  716. } else {
  717. *smask_out = 0x01 << (min_offset & 7);
  718. }
  719. *cmask_out = 0;
  720. } else {
  721. // full speed 12 Mbit/sec or low speed 1.5 Mbit/sec
  722. uint32_t stime, ctime;
  723. if (direction == 0) {
  724. // TODO: how much time to SSPLIT & CSPLIT actually take?
  725. // they're not documented in 5.7 or 5.11.3.
  726. stime = (100 + 32 + maxlen) >> 5;
  727. ctime = (55 + 32) >> 5;
  728. } else {
  729. stime = (40 + 32) >> 5;
  730. ctime = (70 + 32 + maxlen) >> 5;
  731. }
  732. interval = interval >> 3; // can't be zero, earlier check for interval >= 8
  733. // TODO: should we take Single-TT hubs into account, avoid
  734. // scheduling overlapping SSPLIT & CSPLIT to the same hub?
  735. uint32_t min_shift = 0;
  736. uint32_t min_offset = 0xFFFFFFFF;
  737. uint32_t min_bw = 0xFFFFFFFF;
  738. for (uint32_t offset=0; offset < interval; offset++) {
  739. uint32_t max_bw = 0;
  740. for (uint32_t i=offset; i < PERIODIC_LIST_SIZE; i += interval) {
  741. for (uint32_t j=0; j <= 3; j++) { // max 3 without FSTN
  742. uint32_t n = (i << 3) + j;
  743. uint32_t bw1 = uframe_bandwidth[n+0] + stime;
  744. uint32_t bw2 = uframe_bandwidth[n+2] + ctime;
  745. uint32_t bw3 = uframe_bandwidth[n+3] + ctime;
  746. uint32_t bw4 = uframe_bandwidth[n+4] + ctime;
  747. max_bw = max4(bw1, bw2, bw3, bw4);
  748. if (max_bw < min_bw) {
  749. min_bw = max_bw;
  750. min_offset = i;
  751. min_shift = j;
  752. }
  753. }
  754. }
  755. }
  756. print(" min_bw = ");
  757. println(min_bw);
  758. print(", at offset = ");
  759. print(min_offset);
  760. print(", shift= ");
  761. println(min_shift);
  762. if (min_bw > 187) return false;
  763. for (uint32_t i=min_offset; i < PERIODIC_LIST_SIZE; i += interval) {
  764. uint32_t n = (i << 3) + min_shift;
  765. uframe_bandwidth[n+0] += stime;
  766. uframe_bandwidth[n+2] += ctime;
  767. uframe_bandwidth[n+3] += ctime;
  768. uframe_bandwidth[n+4] += ctime;
  769. }
  770. *smask_out = 0x01 << min_shift;
  771. *cmask_out = 0x1C << min_shift;
  772. *offset_out = min_offset;
  773. }
  774. return true;
  775. }
  776. void USBHost::delete_Pipe(Pipe_t *pipe)
  777. {
  778. // TODO: a *LOT* of work here.....
  779. println("delete_Pipe ", (uint32_t)pipe, HEX);
  780. // halt pipe, find and free all Transfer_t
  781. // remove periodic scheduled pipes
  782. // remove async scheduled pipes
  783. // can't free the pipe until the ECHI and all qTD referencing are done
  784. // free_Pipe(pipe);
  785. }