ehci-hub.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * Copyright (C) 2001-2004 by David Brownell
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* this file is part of ehci-hcd.c */
  19. /*-------------------------------------------------------------------------*/
  20. /*
  21. * EHCI Root Hub ... the nonsharable stuff
  22. *
  23. * Registers don't need cpu_to_le32, that happens transparently
  24. */
  25. /*-------------------------------------------------------------------------*/
  26. #ifdef CONFIG_PM
  27. static int ehci_hub_suspend (struct usb_hcd *hcd)
  28. {
  29. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  30. int port;
  31. if (time_before (jiffies, ehci->next_statechange))
  32. msleep(5);
  33. port = HCS_N_PORTS (ehci->hcs_params);
  34. spin_lock_irq (&ehci->lock);
  35. /* stop schedules, clean any completed work */
  36. if (HC_IS_RUNNING(hcd->state)) {
  37. ehci_quiesce (ehci);
  38. hcd->state = HC_STATE_QUIESCING;
  39. }
  40. ehci->command = readl (&ehci->regs->command);
  41. if (ehci->reclaim)
  42. ehci->reclaim_ready = 1;
  43. ehci_work(ehci, NULL);
  44. /* suspend any active/unsuspended ports, maybe allow wakeup */
  45. while (port--) {
  46. u32 __iomem *reg = &ehci->regs->port_status [port];
  47. u32 t1 = readl (reg);
  48. u32 t2 = t1;
  49. if ((t1 & PORT_PE) && !(t1 & PORT_OWNER))
  50. t2 |= PORT_SUSPEND;
  51. if (hcd->remote_wakeup)
  52. t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E;
  53. else
  54. t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
  55. if (t1 != t2) {
  56. ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
  57. port + 1, t1, t2);
  58. writel (t2, reg);
  59. }
  60. }
  61. /* turn off now-idle HC */
  62. del_timer_sync (&ehci->watchdog);
  63. ehci_halt (ehci);
  64. hcd->state = HC_STATE_SUSPENDED;
  65. ehci->next_statechange = jiffies + msecs_to_jiffies(10);
  66. spin_unlock_irq (&ehci->lock);
  67. return 0;
  68. }
  69. /* caller has locked the root hub, and should reset/reinit on error */
  70. static int ehci_hub_resume (struct usb_hcd *hcd)
  71. {
  72. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  73. u32 temp;
  74. int i;
  75. int intr_enable;
  76. if (time_before (jiffies, ehci->next_statechange))
  77. msleep(5);
  78. spin_lock_irq (&ehci->lock);
  79. /* re-init operational registers in case we lost power */
  80. if (readl (&ehci->regs->intr_enable) == 0) {
  81. /* at least some APM implementations will try to deliver
  82. * IRQs right away, so delay them until we're ready.
  83. */
  84. intr_enable = 1;
  85. writel (0, &ehci->regs->segment);
  86. writel (ehci->periodic_dma, &ehci->regs->frame_list);
  87. writel ((u32)ehci->async->qh_dma, &ehci->regs->async_next);
  88. } else
  89. intr_enable = 0;
  90. ehci_dbg(ehci, "resume root hub%s\n",
  91. intr_enable ? " after power loss" : "");
  92. /* restore CMD_RUN, framelist size, and irq threshold */
  93. writel (ehci->command, &ehci->regs->command);
  94. /* take ports out of suspend */
  95. i = HCS_N_PORTS (ehci->hcs_params);
  96. while (i--) {
  97. temp = readl (&ehci->regs->port_status [i]);
  98. temp &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
  99. if (temp & PORT_SUSPEND) {
  100. ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
  101. temp |= PORT_RESUME;
  102. }
  103. writel (temp, &ehci->regs->port_status [i]);
  104. }
  105. i = HCS_N_PORTS (ehci->hcs_params);
  106. mdelay (20);
  107. while (i--) {
  108. temp = readl (&ehci->regs->port_status [i]);
  109. if ((temp & PORT_SUSPEND) == 0)
  110. continue;
  111. temp &= ~PORT_RESUME;
  112. writel (temp, &ehci->regs->port_status [i]);
  113. ehci_vdbg (ehci, "resumed port %d\n", i + 1);
  114. }
  115. (void) readl (&ehci->regs->command);
  116. /* maybe re-activate the schedule(s) */
  117. temp = 0;
  118. if (ehci->async->qh_next.qh)
  119. temp |= CMD_ASE;
  120. if (ehci->periodic_sched)
  121. temp |= CMD_PSE;
  122. if (temp) {
  123. ehci->command |= temp;
  124. writel (ehci->command, &ehci->regs->command);
  125. }
  126. ehci->next_statechange = jiffies + msecs_to_jiffies(5);
  127. hcd->state = HC_STATE_RUNNING;
  128. /* Now we can safely re-enable irqs */
  129. if (intr_enable)
  130. writel (INTR_MASK, &ehci->regs->intr_enable);
  131. spin_unlock_irq (&ehci->lock);
  132. return 0;
  133. }
  134. #else
  135. #define ehci_hub_suspend NULL
  136. #define ehci_hub_resume NULL
  137. #endif /* CONFIG_PM */
  138. /*-------------------------------------------------------------------------*/
  139. static int check_reset_complete (
  140. struct ehci_hcd *ehci,
  141. int index,
  142. int port_status
  143. ) {
  144. if (!(port_status & PORT_CONNECT)) {
  145. ehci->reset_done [index] = 0;
  146. return port_status;
  147. }
  148. /* if reset finished and it's still not enabled -- handoff */
  149. if (!(port_status & PORT_PE)) {
  150. /* with integrated TT, there's nobody to hand it to! */
  151. if (ehci_is_TDI(ehci)) {
  152. ehci_dbg (ehci,
  153. "Failed to enable port %d on root hub TT\n",
  154. index+1);
  155. return port_status;
  156. }
  157. ehci_dbg (ehci, "port %d full speed --> companion\n",
  158. index + 1);
  159. // what happens if HCS_N_CC(params) == 0 ?
  160. port_status |= PORT_OWNER;
  161. writel (port_status, &ehci->regs->port_status [index]);
  162. } else
  163. ehci_dbg (ehci, "port %d high speed\n", index + 1);
  164. return port_status;
  165. }
  166. /*-------------------------------------------------------------------------*/
  167. /* build "status change" packet (one or two bytes) from HC registers */
  168. static int
  169. ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
  170. {
  171. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  172. u32 temp, status = 0;
  173. int ports, i, retval = 1;
  174. unsigned long flags;
  175. /* if !USB_SUSPEND, root hub timers won't get shut down ... */
  176. if (!HC_IS_RUNNING(hcd->state))
  177. return 0;
  178. /* init status to no-changes */
  179. buf [0] = 0;
  180. ports = HCS_N_PORTS (ehci->hcs_params);
  181. if (ports > 7) {
  182. buf [1] = 0;
  183. retval++;
  184. }
  185. /* no hub change reports (bit 0) for now (power, ...) */
  186. /* port N changes (bit N)? */
  187. spin_lock_irqsave (&ehci->lock, flags);
  188. for (i = 0; i < ports; i++) {
  189. temp = readl (&ehci->regs->port_status [i]);
  190. if (temp & PORT_OWNER) {
  191. /* don't report this in GetPortStatus */
  192. if (temp & PORT_CSC) {
  193. temp &= ~PORT_CSC;
  194. writel (temp, &ehci->regs->port_status [i]);
  195. }
  196. continue;
  197. }
  198. if (!(temp & PORT_CONNECT))
  199. ehci->reset_done [i] = 0;
  200. if ((temp & (PORT_CSC | PORT_PEC | PORT_OCC)) != 0
  201. // PORT_STAT_C_SUSPEND?
  202. || ((temp & PORT_RESUME) != 0
  203. && time_after (jiffies,
  204. ehci->reset_done [i]))) {
  205. if (i < 7)
  206. buf [0] |= 1 << (i + 1);
  207. else
  208. buf [1] |= 1 << (i - 7);
  209. status = STS_PCD;
  210. }
  211. }
  212. /* FIXME autosuspend idle root hubs */
  213. spin_unlock_irqrestore (&ehci->lock, flags);
  214. return status ? retval : 0;
  215. }
  216. /*-------------------------------------------------------------------------*/
  217. static void
  218. ehci_hub_descriptor (
  219. struct ehci_hcd *ehci,
  220. struct usb_hub_descriptor *desc
  221. ) {
  222. int ports = HCS_N_PORTS (ehci->hcs_params);
  223. u16 temp;
  224. desc->bDescriptorType = 0x29;
  225. desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
  226. desc->bHubContrCurrent = 0;
  227. desc->bNbrPorts = ports;
  228. temp = 1 + (ports / 8);
  229. desc->bDescLength = 7 + 2 * temp;
  230. /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  231. memset (&desc->bitmap [0], 0, temp);
  232. memset (&desc->bitmap [temp], 0xff, temp);
  233. temp = 0x0008; /* per-port overcurrent reporting */
  234. if (HCS_PPC (ehci->hcs_params))
  235. temp |= 0x0001; /* per-port power control */
  236. else
  237. temp |= 0x0002; /* no power switching */
  238. #if 0
  239. // re-enable when we support USB_PORT_FEAT_INDICATOR below.
  240. if (HCS_INDICATOR (ehci->hcs_params))
  241. temp |= 0x0080; /* per-port indicators (LEDs) */
  242. #endif
  243. desc->wHubCharacteristics = (__force __u16)cpu_to_le16 (temp);
  244. }
  245. /*-------------------------------------------------------------------------*/
  246. #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  247. static int ehci_hub_control (
  248. struct usb_hcd *hcd,
  249. u16 typeReq,
  250. u16 wValue,
  251. u16 wIndex,
  252. char *buf,
  253. u16 wLength
  254. ) {
  255. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  256. int ports = HCS_N_PORTS (ehci->hcs_params);
  257. u32 temp, status;
  258. unsigned long flags;
  259. int retval = 0;
  260. /*
  261. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  262. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  263. * (track current state ourselves) ... blink for diagnostics,
  264. * power, "this is the one", etc. EHCI spec supports this.
  265. */
  266. spin_lock_irqsave (&ehci->lock, flags);
  267. switch (typeReq) {
  268. case ClearHubFeature:
  269. switch (wValue) {
  270. case C_HUB_LOCAL_POWER:
  271. case C_HUB_OVER_CURRENT:
  272. /* no hub-wide feature/status flags */
  273. break;
  274. default:
  275. goto error;
  276. }
  277. break;
  278. case ClearPortFeature:
  279. if (!wIndex || wIndex > ports)
  280. goto error;
  281. wIndex--;
  282. temp = readl (&ehci->regs->port_status [wIndex]);
  283. if (temp & PORT_OWNER)
  284. break;
  285. switch (wValue) {
  286. case USB_PORT_FEAT_ENABLE:
  287. writel (temp & ~PORT_PE,
  288. &ehci->regs->port_status [wIndex]);
  289. break;
  290. case USB_PORT_FEAT_C_ENABLE:
  291. writel (temp | PORT_PEC,
  292. &ehci->regs->port_status [wIndex]);
  293. break;
  294. case USB_PORT_FEAT_SUSPEND:
  295. if (temp & PORT_RESET)
  296. goto error;
  297. if (temp & PORT_SUSPEND) {
  298. if ((temp & PORT_PE) == 0)
  299. goto error;
  300. /* resume signaling for 20 msec */
  301. writel ((temp & ~PORT_WAKE_BITS) | PORT_RESUME,
  302. &ehci->regs->port_status [wIndex]);
  303. ehci->reset_done [wIndex] = jiffies
  304. + msecs_to_jiffies (20);
  305. }
  306. break;
  307. case USB_PORT_FEAT_C_SUSPEND:
  308. /* we auto-clear this feature */
  309. break;
  310. case USB_PORT_FEAT_POWER:
  311. if (HCS_PPC (ehci->hcs_params))
  312. writel (temp & ~PORT_POWER,
  313. &ehci->regs->port_status [wIndex]);
  314. break;
  315. case USB_PORT_FEAT_C_CONNECTION:
  316. writel (temp | PORT_CSC,
  317. &ehci->regs->port_status [wIndex]);
  318. break;
  319. case USB_PORT_FEAT_C_OVER_CURRENT:
  320. writel (temp | PORT_OCC,
  321. &ehci->regs->port_status [wIndex]);
  322. break;
  323. case USB_PORT_FEAT_C_RESET:
  324. /* GetPortStatus clears reset */
  325. break;
  326. default:
  327. goto error;
  328. }
  329. readl (&ehci->regs->command); /* unblock posted write */
  330. break;
  331. case GetHubDescriptor:
  332. ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
  333. buf);
  334. break;
  335. case GetHubStatus:
  336. /* no hub-wide feature/status flags */
  337. memset (buf, 0, 4);
  338. //cpu_to_le32s ((u32 *) buf);
  339. break;
  340. case GetPortStatus:
  341. if (!wIndex || wIndex > ports)
  342. goto error;
  343. wIndex--;
  344. status = 0;
  345. temp = readl (&ehci->regs->port_status [wIndex]);
  346. // wPortChange bits
  347. if (temp & PORT_CSC)
  348. status |= 1 << USB_PORT_FEAT_C_CONNECTION;
  349. if (temp & PORT_PEC)
  350. status |= 1 << USB_PORT_FEAT_C_ENABLE;
  351. if (temp & PORT_OCC)
  352. status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
  353. /* whoever resumes must GetPortStatus to complete it!! */
  354. if ((temp & PORT_RESUME)
  355. && time_after (jiffies,
  356. ehci->reset_done [wIndex])) {
  357. status |= 1 << USB_PORT_FEAT_C_SUSPEND;
  358. ehci->reset_done [wIndex] = 0;
  359. /* stop resume signaling */
  360. temp = readl (&ehci->regs->port_status [wIndex]);
  361. writel (temp & ~PORT_RESUME,
  362. &ehci->regs->port_status [wIndex]);
  363. retval = handshake (
  364. &ehci->regs->port_status [wIndex],
  365. PORT_RESUME, 0, 2000 /* 2msec */);
  366. if (retval != 0) {
  367. ehci_err (ehci, "port %d resume error %d\n",
  368. wIndex + 1, retval);
  369. goto error;
  370. }
  371. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  372. }
  373. /* whoever resets must GetPortStatus to complete it!! */
  374. if ((temp & PORT_RESET)
  375. && time_after (jiffies,
  376. ehci->reset_done [wIndex])) {
  377. status |= 1 << USB_PORT_FEAT_C_RESET;
  378. ehci->reset_done [wIndex] = 0;
  379. /* force reset to complete */
  380. writel (temp & ~PORT_RESET,
  381. &ehci->regs->port_status [wIndex]);
  382. /* REVISIT: some hardware needs 550+ usec to clear
  383. * this bit; seems too long to spin routinely...
  384. */
  385. retval = handshake (
  386. &ehci->regs->port_status [wIndex],
  387. PORT_RESET, 0, 750);
  388. if (retval != 0) {
  389. ehci_err (ehci, "port %d reset error %d\n",
  390. wIndex + 1, retval);
  391. goto error;
  392. }
  393. /* see what we found out */
  394. temp = check_reset_complete (ehci, wIndex,
  395. readl (&ehci->regs->port_status [wIndex]));
  396. }
  397. // don't show wPortStatus if it's owned by a companion hc
  398. if (!(temp & PORT_OWNER)) {
  399. if (temp & PORT_CONNECT) {
  400. status |= 1 << USB_PORT_FEAT_CONNECTION;
  401. // status may be from integrated TT
  402. status |= ehci_port_speed(ehci, temp);
  403. }
  404. if (temp & PORT_PE)
  405. status |= 1 << USB_PORT_FEAT_ENABLE;
  406. if (temp & (PORT_SUSPEND|PORT_RESUME))
  407. status |= 1 << USB_PORT_FEAT_SUSPEND;
  408. if (temp & PORT_OC)
  409. status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
  410. if (temp & PORT_RESET)
  411. status |= 1 << USB_PORT_FEAT_RESET;
  412. if (temp & PORT_POWER)
  413. status |= 1 << USB_PORT_FEAT_POWER;
  414. }
  415. #ifndef EHCI_VERBOSE_DEBUG
  416. if (status & ~0xffff) /* only if wPortChange is interesting */
  417. #endif
  418. dbg_port (ehci, "GetStatus", wIndex + 1, temp);
  419. // we "know" this alignment is good, caller used kmalloc()...
  420. *((__le32 *) buf) = cpu_to_le32 (status);
  421. break;
  422. case SetHubFeature:
  423. switch (wValue) {
  424. case C_HUB_LOCAL_POWER:
  425. case C_HUB_OVER_CURRENT:
  426. /* no hub-wide feature/status flags */
  427. break;
  428. default:
  429. goto error;
  430. }
  431. break;
  432. case SetPortFeature:
  433. if (!wIndex || wIndex > ports)
  434. goto error;
  435. wIndex--;
  436. temp = readl (&ehci->regs->port_status [wIndex]);
  437. if (temp & PORT_OWNER)
  438. break;
  439. switch (wValue) {
  440. case USB_PORT_FEAT_SUSPEND:
  441. if ((temp & PORT_PE) == 0
  442. || (temp & PORT_RESET) != 0)
  443. goto error;
  444. if (hcd->remote_wakeup)
  445. temp |= PORT_WAKE_BITS;
  446. writel (temp | PORT_SUSPEND,
  447. &ehci->regs->port_status [wIndex]);
  448. break;
  449. case USB_PORT_FEAT_POWER:
  450. if (HCS_PPC (ehci->hcs_params))
  451. writel (temp | PORT_POWER,
  452. &ehci->regs->port_status [wIndex]);
  453. break;
  454. case USB_PORT_FEAT_RESET:
  455. if (temp & PORT_RESUME)
  456. goto error;
  457. /* line status bits may report this as low speed,
  458. * which can be fine if this root hub has a
  459. * transaction translator built in.
  460. */
  461. if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
  462. && !ehci_is_TDI(ehci)
  463. && PORT_USB11 (temp)) {
  464. ehci_dbg (ehci,
  465. "port %d low speed --> companion\n",
  466. wIndex + 1);
  467. temp |= PORT_OWNER;
  468. } else {
  469. ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
  470. temp |= PORT_RESET;
  471. temp &= ~PORT_PE;
  472. /*
  473. * caller must wait, then call GetPortStatus
  474. * usb 2.0 spec says 50 ms resets on root
  475. */
  476. ehci->reset_done [wIndex] = jiffies
  477. + msecs_to_jiffies (50);
  478. }
  479. writel (temp, &ehci->regs->port_status [wIndex]);
  480. break;
  481. default:
  482. goto error;
  483. }
  484. readl (&ehci->regs->command); /* unblock posted writes */
  485. break;
  486. default:
  487. error:
  488. /* "stall" on error */
  489. retval = -EPIPE;
  490. }
  491. spin_unlock_irqrestore (&ehci->lock, flags);
  492. return retval;
  493. }