ehci-hub.c 29 KB

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