ehci-hub.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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. #include <linux/usb/otg.h>
  27. #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  28. #ifdef CONFIG_PM
  29. static int ehci_hub_control(
  30. struct usb_hcd *hcd,
  31. u16 typeReq,
  32. u16 wValue,
  33. u16 wIndex,
  34. char *buf,
  35. u16 wLength
  36. );
  37. /* After a power loss, ports that were owned by the companion must be
  38. * reset so that the companion can still own them.
  39. */
  40. static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
  41. {
  42. u32 __iomem *reg;
  43. u32 status;
  44. int port;
  45. __le32 buf;
  46. struct usb_hcd *hcd = ehci_to_hcd(ehci);
  47. if (!ehci->owned_ports)
  48. return;
  49. /* Give the connections some time to appear */
  50. msleep(20);
  51. spin_lock_irq(&ehci->lock);
  52. port = HCS_N_PORTS(ehci->hcs_params);
  53. while (port--) {
  54. if (test_bit(port, &ehci->owned_ports)) {
  55. reg = &ehci->regs->port_status[port];
  56. status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  57. /* Port already owned by companion? */
  58. if (status & PORT_OWNER)
  59. clear_bit(port, &ehci->owned_ports);
  60. else if (test_bit(port, &ehci->companion_ports))
  61. ehci_writel(ehci, status & ~PORT_PE, reg);
  62. else {
  63. spin_unlock_irq(&ehci->lock);
  64. ehci_hub_control(hcd, SetPortFeature,
  65. USB_PORT_FEAT_RESET, port + 1,
  66. NULL, 0);
  67. spin_lock_irq(&ehci->lock);
  68. }
  69. }
  70. }
  71. spin_unlock_irq(&ehci->lock);
  72. if (!ehci->owned_ports)
  73. return;
  74. msleep(90); /* Wait for resets to complete */
  75. spin_lock_irq(&ehci->lock);
  76. port = HCS_N_PORTS(ehci->hcs_params);
  77. while (port--) {
  78. if (test_bit(port, &ehci->owned_ports)) {
  79. spin_unlock_irq(&ehci->lock);
  80. ehci_hub_control(hcd, GetPortStatus,
  81. 0, port + 1,
  82. (char *) &buf, sizeof(buf));
  83. spin_lock_irq(&ehci->lock);
  84. /* The companion should now own the port,
  85. * but if something went wrong the port must not
  86. * remain enabled.
  87. */
  88. reg = &ehci->regs->port_status[port];
  89. status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  90. if (status & PORT_OWNER)
  91. ehci_writel(ehci, status | PORT_CSC, reg);
  92. else {
  93. ehci_dbg(ehci, "failed handover port %d: %x\n",
  94. port + 1, status);
  95. ehci_writel(ehci, status & ~PORT_PE, reg);
  96. }
  97. }
  98. }
  99. ehci->owned_ports = 0;
  100. spin_unlock_irq(&ehci->lock);
  101. }
  102. static int ehci_port_change(struct ehci_hcd *ehci)
  103. {
  104. int i = HCS_N_PORTS(ehci->hcs_params);
  105. /* First check if the controller indicates a change event */
  106. if (ehci_readl(ehci, &ehci->regs->status) & STS_PCD)
  107. return 1;
  108. /*
  109. * Not all controllers appear to update this while going from D3 to D0,
  110. * so check the individual port status registers as well
  111. */
  112. while (i--)
  113. if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC)
  114. return 1;
  115. return 0;
  116. }
  117. static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
  118. bool suspending, bool do_wakeup)
  119. {
  120. int port;
  121. u32 temp;
  122. /* If remote wakeup is enabled for the root hub but disabled
  123. * for the controller, we must adjust all the port wakeup flags
  124. * when the controller is suspended or resumed. In all other
  125. * cases they don't need to be changed.
  126. */
  127. if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
  128. return;
  129. spin_lock_irq(&ehci->lock);
  130. /* clear phy low-power mode before changing wakeup flags */
  131. if (ehci->has_hostpc) {
  132. port = HCS_N_PORTS(ehci->hcs_params);
  133. while (port--) {
  134. u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
  135. temp = ehci_readl(ehci, hostpc_reg);
  136. ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
  137. }
  138. spin_unlock_irq(&ehci->lock);
  139. msleep(5);
  140. spin_lock_irq(&ehci->lock);
  141. }
  142. port = HCS_N_PORTS(ehci->hcs_params);
  143. while (port--) {
  144. u32 __iomem *reg = &ehci->regs->port_status[port];
  145. u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  146. u32 t2 = t1 & ~PORT_WAKE_BITS;
  147. /* If we are suspending the controller, clear the flags.
  148. * If we are resuming the controller, set the wakeup flags.
  149. */
  150. if (!suspending) {
  151. if (t1 & PORT_CONNECT)
  152. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  153. else
  154. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  155. }
  156. ehci_vdbg(ehci, "port %d, %08x -> %08x\n",
  157. port + 1, t1, t2);
  158. ehci_writel(ehci, t2, reg);
  159. }
  160. /* enter phy low-power mode again */
  161. if (ehci->has_hostpc) {
  162. port = HCS_N_PORTS(ehci->hcs_params);
  163. while (port--) {
  164. u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
  165. temp = ehci_readl(ehci, hostpc_reg);
  166. ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
  167. }
  168. }
  169. /* Does the root hub have a port wakeup pending? */
  170. if (!suspending && ehci_port_change(ehci))
  171. usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
  172. spin_unlock_irq(&ehci->lock);
  173. }
  174. static int ehci_bus_suspend (struct usb_hcd *hcd)
  175. {
  176. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  177. int port;
  178. int mask;
  179. int changed;
  180. ehci_dbg(ehci, "suspend root hub\n");
  181. if (time_before (jiffies, ehci->next_statechange))
  182. msleep(5);
  183. /* stop the schedules */
  184. ehci_quiesce(ehci);
  185. spin_lock_irq (&ehci->lock);
  186. if (ehci->rh_state < EHCI_RH_RUNNING)
  187. goto done;
  188. /* Once the controller is stopped, port resumes that are already
  189. * in progress won't complete. Hence if remote wakeup is enabled
  190. * for the root hub and any ports are in the middle of a resume or
  191. * remote wakeup, we must fail the suspend.
  192. */
  193. if (hcd->self.root_hub->do_remote_wakeup) {
  194. if (ehci->resuming_ports) {
  195. spin_unlock_irq(&ehci->lock);
  196. ehci_dbg(ehci, "suspend failed because a port is resuming\n");
  197. return -EBUSY;
  198. }
  199. }
  200. /* Unlike other USB host controller types, EHCI doesn't have
  201. * any notion of "global" or bus-wide suspend. The driver has
  202. * to manually suspend all the active unsuspended ports, and
  203. * then manually resume them in the bus_resume() routine.
  204. */
  205. ehci->bus_suspended = 0;
  206. ehci->owned_ports = 0;
  207. changed = 0;
  208. port = HCS_N_PORTS(ehci->hcs_params);
  209. while (port--) {
  210. u32 __iomem *reg = &ehci->regs->port_status [port];
  211. u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
  212. u32 t2 = t1 & ~PORT_WAKE_BITS;
  213. /* keep track of which ports we suspend */
  214. if (t1 & PORT_OWNER)
  215. set_bit(port, &ehci->owned_ports);
  216. else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
  217. t2 |= PORT_SUSPEND;
  218. set_bit(port, &ehci->bus_suspended);
  219. }
  220. /* enable remote wakeup on all ports, if told to do so */
  221. if (hcd->self.root_hub->do_remote_wakeup) {
  222. /* only enable appropriate wake bits, otherwise the
  223. * hardware can not go phy low power mode. If a race
  224. * condition happens here(connection change during bits
  225. * set), the port change detection will finally fix it.
  226. */
  227. if (t1 & PORT_CONNECT)
  228. t2 |= PORT_WKOC_E | PORT_WKDISC_E;
  229. else
  230. t2 |= PORT_WKOC_E | PORT_WKCONN_E;
  231. }
  232. if (t1 != t2) {
  233. ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
  234. port + 1, t1, t2);
  235. ehci_writel(ehci, t2, reg);
  236. changed = 1;
  237. }
  238. }
  239. if (changed && ehci->has_hostpc) {
  240. spin_unlock_irq(&ehci->lock);
  241. msleep(5); /* 5 ms for HCD to enter low-power mode */
  242. spin_lock_irq(&ehci->lock);
  243. port = HCS_N_PORTS(ehci->hcs_params);
  244. while (port--) {
  245. u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
  246. u32 t3;
  247. t3 = ehci_readl(ehci, hostpc_reg);
  248. ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
  249. t3 = ehci_readl(ehci, hostpc_reg);
  250. ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
  251. port, (t3 & HOSTPC_PHCD) ?
  252. "succeeded" : "failed");
  253. }
  254. }
  255. spin_unlock_irq(&ehci->lock);
  256. /* Apparently some devices need a >= 1-uframe delay here */
  257. if (ehci->bus_suspended)
  258. udelay(150);
  259. /* turn off now-idle HC */
  260. ehci_halt (ehci);
  261. spin_lock_irq(&ehci->lock);
  262. if (ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_POLL_DEAD))
  263. ehci_handle_controller_death(ehci);
  264. if (ehci->rh_state != EHCI_RH_RUNNING)
  265. goto done;
  266. ehci->rh_state = EHCI_RH_SUSPENDED;
  267. end_unlink_async(ehci);
  268. unlink_empty_async(ehci);
  269. ehci_handle_intr_unlinks(ehci);
  270. end_free_itds(ehci);
  271. /* allow remote wakeup */
  272. mask = INTR_MASK;
  273. if (!hcd->self.root_hub->do_remote_wakeup)
  274. mask &= ~STS_PCD;
  275. ehci_writel(ehci, mask, &ehci->regs->intr_enable);
  276. ehci_readl(ehci, &ehci->regs->intr_enable);
  277. done:
  278. ehci->next_statechange = jiffies + msecs_to_jiffies(10);
  279. ehci->enabled_hrtimer_events = 0;
  280. ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
  281. spin_unlock_irq (&ehci->lock);
  282. hrtimer_cancel(&ehci->hrtimer);
  283. return 0;
  284. }
  285. /* caller has locked the root hub, and should reset/reinit on error */
  286. static int ehci_bus_resume (struct usb_hcd *hcd)
  287. {
  288. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  289. u32 temp;
  290. u32 power_okay;
  291. int i;
  292. unsigned long resume_needed = 0;
  293. if (time_before (jiffies, ehci->next_statechange))
  294. msleep(5);
  295. spin_lock_irq (&ehci->lock);
  296. if (!HCD_HW_ACCESSIBLE(hcd) || ehci->shutdown)
  297. goto shutdown;
  298. if (unlikely(ehci->debug)) {
  299. if (!dbgp_reset_prep())
  300. ehci->debug = NULL;
  301. else
  302. dbgp_external_startup();
  303. }
  304. /* Ideally and we've got a real resume here, and no port's power
  305. * was lost. (For PCI, that means Vaux was maintained.) But we
  306. * could instead be restoring a swsusp snapshot -- so that BIOS was
  307. * the last user of the controller, not reset/pm hardware keeping
  308. * state we gave to it.
  309. */
  310. power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
  311. ehci_dbg(ehci, "resume root hub%s\n",
  312. power_okay ? "" : " after power loss");
  313. /* at least some APM implementations will try to deliver
  314. * IRQs right away, so delay them until we're ready.
  315. */
  316. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  317. /* re-init operational registers */
  318. ehci_writel(ehci, 0, &ehci->regs->segment);
  319. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  320. ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
  321. /* restore CMD_RUN, framelist size, and irq threshold */
  322. ehci->command |= CMD_RUN;
  323. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  324. ehci->rh_state = EHCI_RH_RUNNING;
  325. /* Some controller/firmware combinations need a delay during which
  326. * they set up the port statuses. See Bugzilla #8190. */
  327. spin_unlock_irq(&ehci->lock);
  328. msleep(8);
  329. spin_lock_irq(&ehci->lock);
  330. if (ehci->shutdown)
  331. goto shutdown;
  332. /* clear phy low-power mode before resume */
  333. if (ehci->bus_suspended && ehci->has_hostpc) {
  334. i = HCS_N_PORTS(ehci->hcs_params);
  335. while (i--) {
  336. if (test_bit(i, &ehci->bus_suspended)) {
  337. u32 __iomem *hostpc_reg =
  338. &ehci->regs->hostpc[i];
  339. temp = ehci_readl(ehci, hostpc_reg);
  340. ehci_writel(ehci, temp & ~HOSTPC_PHCD,
  341. hostpc_reg);
  342. }
  343. }
  344. spin_unlock_irq(&ehci->lock);
  345. msleep(5);
  346. spin_lock_irq(&ehci->lock);
  347. if (ehci->shutdown)
  348. goto shutdown;
  349. }
  350. /* manually resume the ports we suspended during bus_suspend() */
  351. i = HCS_N_PORTS (ehci->hcs_params);
  352. while (i--) {
  353. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  354. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  355. if (test_bit(i, &ehci->bus_suspended) &&
  356. (temp & PORT_SUSPEND)) {
  357. temp |= PORT_RESUME;
  358. set_bit(i, &resume_needed);
  359. }
  360. ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
  361. }
  362. /* msleep for 20ms only if code is trying to resume port */
  363. if (resume_needed) {
  364. spin_unlock_irq(&ehci->lock);
  365. msleep(20);
  366. spin_lock_irq(&ehci->lock);
  367. if (ehci->shutdown)
  368. goto shutdown;
  369. }
  370. i = HCS_N_PORTS (ehci->hcs_params);
  371. while (i--) {
  372. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  373. if (test_bit(i, &resume_needed)) {
  374. temp &= ~(PORT_RWC_BITS | PORT_RESUME);
  375. ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
  376. ehci_vdbg (ehci, "resumed port %d\n", i + 1);
  377. }
  378. }
  379. ehci->next_statechange = jiffies + msecs_to_jiffies(5);
  380. spin_unlock_irq(&ehci->lock);
  381. ehci_handover_companion_ports(ehci);
  382. /* Now we can safely re-enable irqs */
  383. spin_lock_irq(&ehci->lock);
  384. if (ehci->shutdown)
  385. goto shutdown;
  386. ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
  387. (void) ehci_readl(ehci, &ehci->regs->intr_enable);
  388. spin_unlock_irq(&ehci->lock);
  389. return 0;
  390. shutdown:
  391. spin_unlock_irq(&ehci->lock);
  392. return -ESHUTDOWN;
  393. }
  394. #else
  395. #define ehci_bus_suspend NULL
  396. #define ehci_bus_resume NULL
  397. #endif /* CONFIG_PM */
  398. /*-------------------------------------------------------------------------*/
  399. /*
  400. * Sets the owner of a port
  401. */
  402. static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
  403. {
  404. u32 __iomem *status_reg;
  405. u32 port_status;
  406. int try;
  407. status_reg = &ehci->regs->port_status[portnum];
  408. /*
  409. * The controller won't set the OWNER bit if the port is
  410. * enabled, so this loop will sometimes require at least two
  411. * iterations: one to disable the port and one to set OWNER.
  412. */
  413. for (try = 4; try > 0; --try) {
  414. spin_lock_irq(&ehci->lock);
  415. port_status = ehci_readl(ehci, status_reg);
  416. if ((port_status & PORT_OWNER) == new_owner
  417. || (port_status & (PORT_OWNER | PORT_CONNECT))
  418. == 0)
  419. try = 0;
  420. else {
  421. port_status ^= PORT_OWNER;
  422. port_status &= ~(PORT_PE | PORT_RWC_BITS);
  423. ehci_writel(ehci, port_status, status_reg);
  424. }
  425. spin_unlock_irq(&ehci->lock);
  426. if (try > 1)
  427. msleep(5);
  428. }
  429. }
  430. /*-------------------------------------------------------------------------*/
  431. static int check_reset_complete (
  432. struct ehci_hcd *ehci,
  433. int index,
  434. u32 __iomem *status_reg,
  435. int port_status
  436. ) {
  437. if (!(port_status & PORT_CONNECT))
  438. return port_status;
  439. /* if reset finished and it's still not enabled -- handoff */
  440. if (!(port_status & PORT_PE)) {
  441. /* with integrated TT, there's nobody to hand it to! */
  442. if (ehci_is_TDI(ehci)) {
  443. ehci_dbg (ehci,
  444. "Failed to enable port %d on root hub TT\n",
  445. index+1);
  446. return port_status;
  447. }
  448. ehci_dbg (ehci, "port %d full speed --> companion\n",
  449. index + 1);
  450. // what happens if HCS_N_CC(params) == 0 ?
  451. port_status |= PORT_OWNER;
  452. port_status &= ~PORT_RWC_BITS;
  453. ehci_writel(ehci, port_status, status_reg);
  454. /* ensure 440EPX ohci controller state is operational */
  455. if (ehci->has_amcc_usb23)
  456. set_ohci_hcfs(ehci, 1);
  457. } else {
  458. ehci_dbg(ehci, "port %d reset complete, port enabled\n",
  459. index + 1);
  460. /* ensure 440EPx ohci controller state is suspended */
  461. if (ehci->has_amcc_usb23)
  462. set_ohci_hcfs(ehci, 0);
  463. }
  464. return port_status;
  465. }
  466. /*-------------------------------------------------------------------------*/
  467. /* build "status change" packet (one or two bytes) from HC registers */
  468. static int
  469. ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
  470. {
  471. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  472. u32 temp, status;
  473. u32 mask;
  474. int ports, i, retval = 1;
  475. unsigned long flags;
  476. u32 ppcd = 0;
  477. /* init status to no-changes */
  478. buf [0] = 0;
  479. ports = HCS_N_PORTS (ehci->hcs_params);
  480. if (ports > 7) {
  481. buf [1] = 0;
  482. retval++;
  483. }
  484. /* Inform the core about resumes-in-progress by returning
  485. * a non-zero value even if there are no status changes.
  486. */
  487. status = ehci->resuming_ports;
  488. /* Some boards (mostly VIA?) report bogus overcurrent indications,
  489. * causing massive log spam unless we completely ignore them. It
  490. * may be relevant that VIA VT8235 controllers, where PORT_POWER is
  491. * always set, seem to clear PORT_OCC and PORT_CSC when writing to
  492. * PORT_POWER; that's surprising, but maybe within-spec.
  493. */
  494. if (!ignore_oc)
  495. mask = PORT_CSC | PORT_PEC | PORT_OCC;
  496. else
  497. mask = PORT_CSC | PORT_PEC;
  498. // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
  499. /* no hub change reports (bit 0) for now (power, ...) */
  500. /* port N changes (bit N)? */
  501. spin_lock_irqsave (&ehci->lock, flags);
  502. /* get per-port change detect bits */
  503. if (ehci->has_ppcd)
  504. ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
  505. for (i = 0; i < ports; i++) {
  506. /* leverage per-port change bits feature */
  507. if (ehci->has_ppcd && !(ppcd & (1 << i)))
  508. continue;
  509. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  510. /*
  511. * Return status information even for ports with OWNER set.
  512. * Otherwise khubd wouldn't see the disconnect event when a
  513. * high-speed device is switched over to the companion
  514. * controller by the user.
  515. */
  516. if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
  517. || (ehci->reset_done[i] && time_after_eq(
  518. jiffies, ehci->reset_done[i]))) {
  519. if (i < 7)
  520. buf [0] |= 1 << (i + 1);
  521. else
  522. buf [1] |= 1 << (i - 7);
  523. status = STS_PCD;
  524. }
  525. }
  526. /* FIXME autosuspend idle root hubs */
  527. spin_unlock_irqrestore (&ehci->lock, flags);
  528. return status ? retval : 0;
  529. }
  530. /*-------------------------------------------------------------------------*/
  531. static void
  532. ehci_hub_descriptor (
  533. struct ehci_hcd *ehci,
  534. struct usb_hub_descriptor *desc
  535. ) {
  536. int ports = HCS_N_PORTS (ehci->hcs_params);
  537. u16 temp;
  538. desc->bDescriptorType = 0x29;
  539. desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
  540. desc->bHubContrCurrent = 0;
  541. desc->bNbrPorts = ports;
  542. temp = 1 + (ports / 8);
  543. desc->bDescLength = 7 + 2 * temp;
  544. /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  545. memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
  546. memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
  547. temp = 0x0008; /* per-port overcurrent reporting */
  548. if (HCS_PPC (ehci->hcs_params))
  549. temp |= 0x0001; /* per-port power control */
  550. else
  551. temp |= 0x0002; /* no power switching */
  552. #if 0
  553. // re-enable when we support USB_PORT_FEAT_INDICATOR below.
  554. if (HCS_INDICATOR (ehci->hcs_params))
  555. temp |= 0x0080; /* per-port indicators (LEDs) */
  556. #endif
  557. desc->wHubCharacteristics = cpu_to_le16(temp);
  558. }
  559. /*-------------------------------------------------------------------------*/
  560. static int ehci_hub_control (
  561. struct usb_hcd *hcd,
  562. u16 typeReq,
  563. u16 wValue,
  564. u16 wIndex,
  565. char *buf,
  566. u16 wLength
  567. ) {
  568. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  569. int ports = HCS_N_PORTS (ehci->hcs_params);
  570. u32 __iomem *status_reg = &ehci->regs->port_status[
  571. (wIndex & 0xff) - 1];
  572. u32 __iomem *hostpc_reg = &ehci->regs->hostpc[(wIndex & 0xff) - 1];
  573. u32 temp, temp1, status;
  574. unsigned long flags;
  575. int retval = 0;
  576. unsigned selector;
  577. /*
  578. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  579. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  580. * (track current state ourselves) ... blink for diagnostics,
  581. * power, "this is the one", etc. EHCI spec supports this.
  582. */
  583. spin_lock_irqsave (&ehci->lock, flags);
  584. switch (typeReq) {
  585. case ClearHubFeature:
  586. switch (wValue) {
  587. case C_HUB_LOCAL_POWER:
  588. case C_HUB_OVER_CURRENT:
  589. /* no hub-wide feature/status flags */
  590. break;
  591. default:
  592. goto error;
  593. }
  594. break;
  595. case ClearPortFeature:
  596. if (!wIndex || wIndex > ports)
  597. goto error;
  598. wIndex--;
  599. temp = ehci_readl(ehci, status_reg);
  600. temp &= ~PORT_RWC_BITS;
  601. /*
  602. * Even if OWNER is set, so the port is owned by the
  603. * companion controller, khubd needs to be able to clear
  604. * the port-change status bits (especially
  605. * USB_PORT_STAT_C_CONNECTION).
  606. */
  607. switch (wValue) {
  608. case USB_PORT_FEAT_ENABLE:
  609. ehci_writel(ehci, temp & ~PORT_PE, status_reg);
  610. break;
  611. case USB_PORT_FEAT_C_ENABLE:
  612. ehci_writel(ehci, temp | PORT_PEC, status_reg);
  613. break;
  614. case USB_PORT_FEAT_SUSPEND:
  615. if (temp & PORT_RESET)
  616. goto error;
  617. if (ehci->no_selective_suspend)
  618. break;
  619. #ifdef CONFIG_USB_OTG
  620. if ((hcd->self.otg_port == (wIndex + 1))
  621. && hcd->self.b_hnp_enable) {
  622. otg_start_hnp(hcd->phy->otg);
  623. break;
  624. }
  625. #endif
  626. if (!(temp & PORT_SUSPEND))
  627. break;
  628. if ((temp & PORT_PE) == 0)
  629. goto error;
  630. /* clear phy low-power mode before resume */
  631. if (ehci->has_hostpc) {
  632. temp1 = ehci_readl(ehci, hostpc_reg);
  633. ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
  634. hostpc_reg);
  635. spin_unlock_irqrestore(&ehci->lock, flags);
  636. msleep(5);/* wait to leave low-power mode */
  637. spin_lock_irqsave(&ehci->lock, flags);
  638. }
  639. /* resume signaling for 20 msec */
  640. temp &= ~PORT_WAKE_BITS;
  641. ehci_writel(ehci, temp | PORT_RESUME, status_reg);
  642. ehci->reset_done[wIndex] = jiffies
  643. + msecs_to_jiffies(20);
  644. break;
  645. case USB_PORT_FEAT_C_SUSPEND:
  646. clear_bit(wIndex, &ehci->port_c_suspend);
  647. break;
  648. case USB_PORT_FEAT_POWER:
  649. if (HCS_PPC (ehci->hcs_params))
  650. ehci_writel(ehci, temp & ~PORT_POWER,
  651. status_reg);
  652. break;
  653. case USB_PORT_FEAT_C_CONNECTION:
  654. if (ehci->has_lpm) {
  655. /* clear PORTSC bits on disconnect */
  656. temp &= ~PORT_LPM;
  657. temp &= ~PORT_DEV_ADDR;
  658. }
  659. ehci_writel(ehci, temp | PORT_CSC, status_reg);
  660. break;
  661. case USB_PORT_FEAT_C_OVER_CURRENT:
  662. ehci_writel(ehci, temp | PORT_OCC, status_reg);
  663. break;
  664. case USB_PORT_FEAT_C_RESET:
  665. /* GetPortStatus clears reset */
  666. break;
  667. default:
  668. goto error;
  669. }
  670. ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
  671. break;
  672. case GetHubDescriptor:
  673. ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
  674. buf);
  675. break;
  676. case GetHubStatus:
  677. /* no hub-wide feature/status flags */
  678. memset (buf, 0, 4);
  679. //cpu_to_le32s ((u32 *) buf);
  680. break;
  681. case GetPortStatus:
  682. if (!wIndex || wIndex > ports)
  683. goto error;
  684. wIndex--;
  685. status = 0;
  686. temp = ehci_readl(ehci, status_reg);
  687. // wPortChange bits
  688. if (temp & PORT_CSC)
  689. status |= USB_PORT_STAT_C_CONNECTION << 16;
  690. if (temp & PORT_PEC)
  691. status |= USB_PORT_STAT_C_ENABLE << 16;
  692. if ((temp & PORT_OCC) && !ignore_oc){
  693. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  694. /*
  695. * Hubs should disable port power on over-current.
  696. * However, not all EHCI implementations do this
  697. * automatically, even if they _do_ support per-port
  698. * power switching; they're allowed to just limit the
  699. * current. khubd will turn the power back on.
  700. */
  701. if ((temp & PORT_OC) && HCS_PPC(ehci->hcs_params)) {
  702. ehci_writel(ehci,
  703. temp & ~(PORT_RWC_BITS | PORT_POWER),
  704. status_reg);
  705. temp = ehci_readl(ehci, status_reg);
  706. }
  707. }
  708. /* whoever resumes must GetPortStatus to complete it!! */
  709. if (temp & PORT_RESUME) {
  710. /* Remote Wakeup received? */
  711. if (!ehci->reset_done[wIndex]) {
  712. /* resume signaling for 20 msec */
  713. ehci->reset_done[wIndex] = jiffies
  714. + msecs_to_jiffies(20);
  715. /* check the port again */
  716. mod_timer(&ehci_to_hcd(ehci)->rh_timer,
  717. ehci->reset_done[wIndex]);
  718. }
  719. /* resume completed? */
  720. else if (time_after_eq(jiffies,
  721. ehci->reset_done[wIndex])) {
  722. clear_bit(wIndex, &ehci->suspended_ports);
  723. set_bit(wIndex, &ehci->port_c_suspend);
  724. ehci->reset_done[wIndex] = 0;
  725. /* stop resume signaling */
  726. temp = ehci_readl(ehci, status_reg);
  727. ehci_writel(ehci,
  728. temp & ~(PORT_RWC_BITS | PORT_RESUME),
  729. status_reg);
  730. clear_bit(wIndex, &ehci->resuming_ports);
  731. retval = handshake(ehci, status_reg,
  732. PORT_RESUME, 0, 2000 /* 2msec */);
  733. if (retval != 0) {
  734. ehci_err(ehci,
  735. "port %d resume error %d\n",
  736. wIndex + 1, retval);
  737. goto error;
  738. }
  739. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  740. }
  741. }
  742. /* whoever resets must GetPortStatus to complete it!! */
  743. if ((temp & PORT_RESET)
  744. && time_after_eq(jiffies,
  745. ehci->reset_done[wIndex])) {
  746. status |= USB_PORT_STAT_C_RESET << 16;
  747. ehci->reset_done [wIndex] = 0;
  748. clear_bit(wIndex, &ehci->resuming_ports);
  749. /* force reset to complete */
  750. ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
  751. status_reg);
  752. /* REVISIT: some hardware needs 550+ usec to clear
  753. * this bit; seems too long to spin routinely...
  754. */
  755. retval = handshake(ehci, status_reg,
  756. PORT_RESET, 0, 1000);
  757. if (retval != 0) {
  758. ehci_err (ehci, "port %d reset error %d\n",
  759. wIndex + 1, retval);
  760. goto error;
  761. }
  762. /* see what we found out */
  763. temp = check_reset_complete (ehci, wIndex, status_reg,
  764. ehci_readl(ehci, status_reg));
  765. }
  766. if (!(temp & (PORT_RESUME|PORT_RESET))) {
  767. ehci->reset_done[wIndex] = 0;
  768. clear_bit(wIndex, &ehci->resuming_ports);
  769. }
  770. /* transfer dedicated ports to the companion hc */
  771. if ((temp & PORT_CONNECT) &&
  772. test_bit(wIndex, &ehci->companion_ports)) {
  773. temp &= ~PORT_RWC_BITS;
  774. temp |= PORT_OWNER;
  775. ehci_writel(ehci, temp, status_reg);
  776. ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
  777. temp = ehci_readl(ehci, status_reg);
  778. }
  779. /*
  780. * Even if OWNER is set, there's no harm letting khubd
  781. * see the wPortStatus values (they should all be 0 except
  782. * for PORT_POWER anyway).
  783. */
  784. if (temp & PORT_CONNECT) {
  785. status |= USB_PORT_STAT_CONNECTION;
  786. // status may be from integrated TT
  787. if (ehci->has_hostpc) {
  788. temp1 = ehci_readl(ehci, hostpc_reg);
  789. status |= ehci_port_speed(ehci, temp1);
  790. } else
  791. status |= ehci_port_speed(ehci, temp);
  792. }
  793. if (temp & PORT_PE)
  794. status |= USB_PORT_STAT_ENABLE;
  795. /* maybe the port was unsuspended without our knowledge */
  796. if (temp & (PORT_SUSPEND|PORT_RESUME)) {
  797. status |= USB_PORT_STAT_SUSPEND;
  798. } else if (test_bit(wIndex, &ehci->suspended_ports)) {
  799. clear_bit(wIndex, &ehci->suspended_ports);
  800. clear_bit(wIndex, &ehci->resuming_ports);
  801. ehci->reset_done[wIndex] = 0;
  802. if (temp & PORT_PE)
  803. set_bit(wIndex, &ehci->port_c_suspend);
  804. }
  805. if (temp & PORT_OC)
  806. status |= USB_PORT_STAT_OVERCURRENT;
  807. if (temp & PORT_RESET)
  808. status |= USB_PORT_STAT_RESET;
  809. if (temp & PORT_POWER)
  810. status |= USB_PORT_STAT_POWER;
  811. if (test_bit(wIndex, &ehci->port_c_suspend))
  812. status |= USB_PORT_STAT_C_SUSPEND << 16;
  813. #ifndef VERBOSE_DEBUG
  814. if (status & ~0xffff) /* only if wPortChange is interesting */
  815. #endif
  816. dbg_port (ehci, "GetStatus", wIndex + 1, temp);
  817. put_unaligned_le32(status, buf);
  818. break;
  819. case SetHubFeature:
  820. switch (wValue) {
  821. case C_HUB_LOCAL_POWER:
  822. case C_HUB_OVER_CURRENT:
  823. /* no hub-wide feature/status flags */
  824. break;
  825. default:
  826. goto error;
  827. }
  828. break;
  829. case SetPortFeature:
  830. selector = wIndex >> 8;
  831. wIndex &= 0xff;
  832. if (unlikely(ehci->debug)) {
  833. /* If the debug port is active any port
  834. * feature requests should get denied */
  835. if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
  836. (readl(&ehci->debug->control) & DBGP_ENABLED)) {
  837. retval = -ENODEV;
  838. goto error_exit;
  839. }
  840. }
  841. if (!wIndex || wIndex > ports)
  842. goto error;
  843. wIndex--;
  844. temp = ehci_readl(ehci, status_reg);
  845. if (temp & PORT_OWNER)
  846. break;
  847. temp &= ~PORT_RWC_BITS;
  848. switch (wValue) {
  849. case USB_PORT_FEAT_SUSPEND:
  850. if (ehci->no_selective_suspend)
  851. break;
  852. if ((temp & PORT_PE) == 0
  853. || (temp & PORT_RESET) != 0)
  854. goto error;
  855. /* After above check the port must be connected.
  856. * Set appropriate bit thus could put phy into low power
  857. * mode if we have hostpc feature
  858. */
  859. temp &= ~PORT_WKCONN_E;
  860. temp |= PORT_WKDISC_E | PORT_WKOC_E;
  861. ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
  862. if (ehci->has_hostpc) {
  863. spin_unlock_irqrestore(&ehci->lock, flags);
  864. msleep(5);/* 5ms for HCD enter low pwr mode */
  865. spin_lock_irqsave(&ehci->lock, flags);
  866. temp1 = ehci_readl(ehci, hostpc_reg);
  867. ehci_writel(ehci, temp1 | HOSTPC_PHCD,
  868. hostpc_reg);
  869. temp1 = ehci_readl(ehci, hostpc_reg);
  870. ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
  871. wIndex, (temp1 & HOSTPC_PHCD) ?
  872. "succeeded" : "failed");
  873. }
  874. set_bit(wIndex, &ehci->suspended_ports);
  875. break;
  876. case USB_PORT_FEAT_POWER:
  877. if (HCS_PPC (ehci->hcs_params))
  878. ehci_writel(ehci, temp | PORT_POWER,
  879. status_reg);
  880. break;
  881. case USB_PORT_FEAT_RESET:
  882. if (temp & PORT_RESUME)
  883. goto error;
  884. /* line status bits may report this as low speed,
  885. * which can be fine if this root hub has a
  886. * transaction translator built in.
  887. */
  888. if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
  889. && !ehci_is_TDI(ehci)
  890. && PORT_USB11 (temp)) {
  891. ehci_dbg (ehci,
  892. "port %d low speed --> companion\n",
  893. wIndex + 1);
  894. temp |= PORT_OWNER;
  895. } else {
  896. ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
  897. temp |= PORT_RESET;
  898. temp &= ~PORT_PE;
  899. /*
  900. * caller must wait, then call GetPortStatus
  901. * usb 2.0 spec says 50 ms resets on root
  902. */
  903. ehci->reset_done [wIndex] = jiffies
  904. + msecs_to_jiffies (50);
  905. }
  906. ehci_writel(ehci, temp, status_reg);
  907. break;
  908. /* For downstream facing ports (these): one hub port is put
  909. * into test mode according to USB2 11.24.2.13, then the hub
  910. * must be reset (which for root hub now means rmmod+modprobe,
  911. * or else system reboot). See EHCI 2.3.9 and 4.14 for info
  912. * about the EHCI-specific stuff.
  913. */
  914. case USB_PORT_FEAT_TEST:
  915. if (!selector || selector > 5)
  916. goto error;
  917. spin_unlock_irqrestore(&ehci->lock, flags);
  918. ehci_quiesce(ehci);
  919. spin_lock_irqsave(&ehci->lock, flags);
  920. /* Put all enabled ports into suspend */
  921. while (ports--) {
  922. u32 __iomem *sreg =
  923. &ehci->regs->port_status[ports];
  924. temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
  925. if (temp & PORT_PE)
  926. ehci_writel(ehci, temp | PORT_SUSPEND,
  927. sreg);
  928. }
  929. spin_unlock_irqrestore(&ehci->lock, flags);
  930. ehci_halt(ehci);
  931. spin_lock_irqsave(&ehci->lock, flags);
  932. temp = ehci_readl(ehci, status_reg);
  933. temp |= selector << 16;
  934. ehci_writel(ehci, temp, status_reg);
  935. break;
  936. default:
  937. goto error;
  938. }
  939. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  940. break;
  941. default:
  942. error:
  943. /* "stall" on error */
  944. retval = -EPIPE;
  945. }
  946. error_exit:
  947. spin_unlock_irqrestore (&ehci->lock, flags);
  948. return retval;
  949. }
  950. static void __maybe_unused ehci_relinquish_port(struct usb_hcd *hcd,
  951. int portnum)
  952. {
  953. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  954. if (ehci_is_TDI(ehci))
  955. return;
  956. set_owner(ehci, --portnum, PORT_OWNER);
  957. }
  958. static int __maybe_unused ehci_port_handed_over(struct usb_hcd *hcd,
  959. int portnum)
  960. {
  961. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  962. u32 __iomem *reg;
  963. if (ehci_is_TDI(ehci))
  964. return 0;
  965. reg = &ehci->regs->port_status[portnum - 1];
  966. return ehci_readl(ehci, reg) & PORT_OWNER;
  967. }