ehci-hub.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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. u8 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. resume_needed = 1;
  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, &ehci->bus_suspended) &&
  369. (temp & PORT_SUSPEND)) {
  370. temp &= ~(PORT_RWC_BITS | PORT_RESUME);
  371. ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
  372. ehci_vdbg (ehci, "resumed port %d\n", i + 1);
  373. }
  374. }
  375. (void) ehci_readl(ehci, &ehci->regs->command);
  376. /* maybe re-activate the schedule(s) */
  377. temp = 0;
  378. if (ehci->async->qh_next.qh)
  379. temp |= CMD_ASE;
  380. if (ehci->periodic_sched)
  381. temp |= CMD_PSE;
  382. if (temp) {
  383. ehci->command |= temp;
  384. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  385. }
  386. ehci->next_statechange = jiffies + msecs_to_jiffies(5);
  387. hcd->state = HC_STATE_RUNNING;
  388. /* Now we can safely re-enable irqs */
  389. ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
  390. spin_unlock_irq (&ehci->lock);
  391. ehci_handover_companion_ports(ehci);
  392. return 0;
  393. }
  394. #else
  395. #define ehci_bus_suspend NULL
  396. #define ehci_bus_resume NULL
  397. #endif /* CONFIG_PM */
  398. /*-------------------------------------------------------------------------*/
  399. /* Display the ports dedicated to the companion controller */
  400. static ssize_t show_companion(struct device *dev,
  401. struct device_attribute *attr,
  402. char *buf)
  403. {
  404. struct ehci_hcd *ehci;
  405. int nports, index, n;
  406. int count = PAGE_SIZE;
  407. char *ptr = buf;
  408. ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
  409. nports = HCS_N_PORTS(ehci->hcs_params);
  410. for (index = 0; index < nports; ++index) {
  411. if (test_bit(index, &ehci->companion_ports)) {
  412. n = scnprintf(ptr, count, "%d\n", index + 1);
  413. ptr += n;
  414. count -= n;
  415. }
  416. }
  417. return ptr - buf;
  418. }
  419. /*
  420. * Sets the owner of a port
  421. */
  422. static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
  423. {
  424. u32 __iomem *status_reg;
  425. u32 port_status;
  426. int try;
  427. status_reg = &ehci->regs->port_status[portnum];
  428. /*
  429. * The controller won't set the OWNER bit if the port is
  430. * enabled, so this loop will sometimes require at least two
  431. * iterations: one to disable the port and one to set OWNER.
  432. */
  433. for (try = 4; try > 0; --try) {
  434. spin_lock_irq(&ehci->lock);
  435. port_status = ehci_readl(ehci, status_reg);
  436. if ((port_status & PORT_OWNER) == new_owner
  437. || (port_status & (PORT_OWNER | PORT_CONNECT))
  438. == 0)
  439. try = 0;
  440. else {
  441. port_status ^= PORT_OWNER;
  442. port_status &= ~(PORT_PE | PORT_RWC_BITS);
  443. ehci_writel(ehci, port_status, status_reg);
  444. }
  445. spin_unlock_irq(&ehci->lock);
  446. if (try > 1)
  447. msleep(5);
  448. }
  449. }
  450. /*
  451. * Dedicate or undedicate a port to the companion controller.
  452. * Syntax is "[-]portnum", where a leading '-' sign means
  453. * return control of the port to the EHCI controller.
  454. */
  455. static ssize_t store_companion(struct device *dev,
  456. struct device_attribute *attr,
  457. const char *buf, size_t count)
  458. {
  459. struct ehci_hcd *ehci;
  460. int portnum, new_owner;
  461. ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
  462. new_owner = PORT_OWNER; /* Owned by companion */
  463. if (sscanf(buf, "%d", &portnum) != 1)
  464. return -EINVAL;
  465. if (portnum < 0) {
  466. portnum = - portnum;
  467. new_owner = 0; /* Owned by EHCI */
  468. }
  469. if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
  470. return -ENOENT;
  471. portnum--;
  472. if (new_owner)
  473. set_bit(portnum, &ehci->companion_ports);
  474. else
  475. clear_bit(portnum, &ehci->companion_ports);
  476. set_owner(ehci, portnum, new_owner);
  477. return count;
  478. }
  479. static DEVICE_ATTR(companion, 0644, show_companion, store_companion);
  480. static inline int create_companion_file(struct ehci_hcd *ehci)
  481. {
  482. int i = 0;
  483. /* with integrated TT there is no companion! */
  484. if (!ehci_is_TDI(ehci))
  485. i = device_create_file(ehci_to_hcd(ehci)->self.controller,
  486. &dev_attr_companion);
  487. return i;
  488. }
  489. static inline void remove_companion_file(struct ehci_hcd *ehci)
  490. {
  491. /* with integrated TT there is no companion! */
  492. if (!ehci_is_TDI(ehci))
  493. device_remove_file(ehci_to_hcd(ehci)->self.controller,
  494. &dev_attr_companion);
  495. }
  496. /*-------------------------------------------------------------------------*/
  497. static int check_reset_complete (
  498. struct ehci_hcd *ehci,
  499. int index,
  500. u32 __iomem *status_reg,
  501. int port_status
  502. ) {
  503. if (!(port_status & PORT_CONNECT))
  504. return port_status;
  505. /* if reset finished and it's still not enabled -- handoff */
  506. if (!(port_status & PORT_PE)) {
  507. /* with integrated TT, there's nobody to hand it to! */
  508. if (ehci_is_TDI(ehci)) {
  509. ehci_dbg (ehci,
  510. "Failed to enable port %d on root hub TT\n",
  511. index+1);
  512. return port_status;
  513. }
  514. ehci_dbg (ehci, "port %d full speed --> companion\n",
  515. index + 1);
  516. // what happens if HCS_N_CC(params) == 0 ?
  517. port_status |= PORT_OWNER;
  518. port_status &= ~PORT_RWC_BITS;
  519. ehci_writel(ehci, port_status, status_reg);
  520. /* ensure 440EPX ohci controller state is operational */
  521. if (ehci->has_amcc_usb23)
  522. set_ohci_hcfs(ehci, 1);
  523. } else {
  524. ehci_dbg (ehci, "port %d high speed\n", index + 1);
  525. /* ensure 440EPx ohci controller state is suspended */
  526. if (ehci->has_amcc_usb23)
  527. set_ohci_hcfs(ehci, 0);
  528. }
  529. return port_status;
  530. }
  531. /*-------------------------------------------------------------------------*/
  532. /* build "status change" packet (one or two bytes) from HC registers */
  533. static int
  534. ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
  535. {
  536. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  537. u32 temp, status = 0;
  538. u32 mask;
  539. int ports, i, retval = 1;
  540. unsigned long flags;
  541. u32 ppcd = 0;
  542. /* if !USB_SUSPEND, root hub timers won't get shut down ... */
  543. if (!HC_IS_RUNNING(hcd->state))
  544. return 0;
  545. /* init status to no-changes */
  546. buf [0] = 0;
  547. ports = HCS_N_PORTS (ehci->hcs_params);
  548. if (ports > 7) {
  549. buf [1] = 0;
  550. retval++;
  551. }
  552. /* Some boards (mostly VIA?) report bogus overcurrent indications,
  553. * causing massive log spam unless we completely ignore them. It
  554. * may be relevant that VIA VT8235 controllers, where PORT_POWER is
  555. * always set, seem to clear PORT_OCC and PORT_CSC when writing to
  556. * PORT_POWER; that's surprising, but maybe within-spec.
  557. */
  558. if (!ignore_oc)
  559. mask = PORT_CSC | PORT_PEC | PORT_OCC;
  560. else
  561. mask = PORT_CSC | PORT_PEC;
  562. // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
  563. /* no hub change reports (bit 0) for now (power, ...) */
  564. /* port N changes (bit N)? */
  565. spin_lock_irqsave (&ehci->lock, flags);
  566. /* get per-port change detect bits */
  567. if (ehci->has_ppcd)
  568. ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
  569. for (i = 0; i < ports; i++) {
  570. /* leverage per-port change bits feature */
  571. if (ehci->has_ppcd && !(ppcd & (1 << i)))
  572. continue;
  573. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  574. /*
  575. * Return status information even for ports with OWNER set.
  576. * Otherwise khubd wouldn't see the disconnect event when a
  577. * high-speed device is switched over to the companion
  578. * controller by the user.
  579. */
  580. if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
  581. || (ehci->reset_done[i] && time_after_eq(
  582. jiffies, ehci->reset_done[i]))) {
  583. if (i < 7)
  584. buf [0] |= 1 << (i + 1);
  585. else
  586. buf [1] |= 1 << (i - 7);
  587. status = STS_PCD;
  588. }
  589. }
  590. /* FIXME autosuspend idle root hubs */
  591. spin_unlock_irqrestore (&ehci->lock, flags);
  592. return status ? retval : 0;
  593. }
  594. /*-------------------------------------------------------------------------*/
  595. static void
  596. ehci_hub_descriptor (
  597. struct ehci_hcd *ehci,
  598. struct usb_hub_descriptor *desc
  599. ) {
  600. int ports = HCS_N_PORTS (ehci->hcs_params);
  601. u16 temp;
  602. desc->bDescriptorType = 0x29;
  603. desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
  604. desc->bHubContrCurrent = 0;
  605. desc->bNbrPorts = ports;
  606. temp = 1 + (ports / 8);
  607. desc->bDescLength = 7 + 2 * temp;
  608. /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  609. memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
  610. memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
  611. temp = 0x0008; /* per-port overcurrent reporting */
  612. if (HCS_PPC (ehci->hcs_params))
  613. temp |= 0x0001; /* per-port power control */
  614. else
  615. temp |= 0x0002; /* no power switching */
  616. #if 0
  617. // re-enable when we support USB_PORT_FEAT_INDICATOR below.
  618. if (HCS_INDICATOR (ehci->hcs_params))
  619. temp |= 0x0080; /* per-port indicators (LEDs) */
  620. #endif
  621. desc->wHubCharacteristics = cpu_to_le16(temp);
  622. }
  623. /*-------------------------------------------------------------------------*/
  624. static int ehci_hub_control (
  625. struct usb_hcd *hcd,
  626. u16 typeReq,
  627. u16 wValue,
  628. u16 wIndex,
  629. char *buf,
  630. u16 wLength
  631. ) {
  632. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  633. int ports = HCS_N_PORTS (ehci->hcs_params);
  634. u32 __iomem *status_reg = &ehci->regs->port_status[
  635. (wIndex & 0xff) - 1];
  636. u32 __iomem *hostpc_reg = NULL;
  637. u32 temp, temp1, status;
  638. unsigned long flags;
  639. int retval = 0;
  640. unsigned selector;
  641. /*
  642. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  643. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  644. * (track current state ourselves) ... blink for diagnostics,
  645. * power, "this is the one", etc. EHCI spec supports this.
  646. */
  647. if (ehci->has_hostpc)
  648. hostpc_reg = (u32 __iomem *)((u8 *)ehci->regs
  649. + HOSTPC0 + 4 * ((wIndex & 0xff) - 1));
  650. spin_lock_irqsave (&ehci->lock, flags);
  651. switch (typeReq) {
  652. case ClearHubFeature:
  653. switch (wValue) {
  654. case C_HUB_LOCAL_POWER:
  655. case C_HUB_OVER_CURRENT:
  656. /* no hub-wide feature/status flags */
  657. break;
  658. default:
  659. goto error;
  660. }
  661. break;
  662. case ClearPortFeature:
  663. if (!wIndex || wIndex > ports)
  664. goto error;
  665. wIndex--;
  666. temp = ehci_readl(ehci, status_reg);
  667. /*
  668. * Even if OWNER is set, so the port is owned by the
  669. * companion controller, khubd needs to be able to clear
  670. * the port-change status bits (especially
  671. * USB_PORT_STAT_C_CONNECTION).
  672. */
  673. switch (wValue) {
  674. case USB_PORT_FEAT_ENABLE:
  675. ehci_writel(ehci, temp & ~PORT_PE, status_reg);
  676. break;
  677. case USB_PORT_FEAT_C_ENABLE:
  678. ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
  679. status_reg);
  680. break;
  681. case USB_PORT_FEAT_SUSPEND:
  682. if (temp & PORT_RESET)
  683. goto error;
  684. if (ehci->no_selective_suspend)
  685. break;
  686. #ifdef CONFIG_USB_OTG
  687. if ((hcd->self.otg_port == (wIndex + 1))
  688. && hcd->self.b_hnp_enable) {
  689. otg_start_hnp(ehci->transceiver);
  690. break;
  691. }
  692. #endif
  693. if (!(temp & PORT_SUSPEND))
  694. break;
  695. if ((temp & PORT_PE) == 0)
  696. goto error;
  697. /* clear phy low-power mode before resume */
  698. if (hostpc_reg) {
  699. temp1 = ehci_readl(ehci, hostpc_reg);
  700. ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
  701. hostpc_reg);
  702. spin_unlock_irqrestore(&ehci->lock, flags);
  703. msleep(5);/* wait to leave low-power mode */
  704. spin_lock_irqsave(&ehci->lock, flags);
  705. }
  706. /* resume signaling for 20 msec */
  707. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  708. ehci_writel(ehci, temp | PORT_RESUME, status_reg);
  709. ehci->reset_done[wIndex] = jiffies
  710. + msecs_to_jiffies(20);
  711. break;
  712. case USB_PORT_FEAT_C_SUSPEND:
  713. clear_bit(wIndex, &ehci->port_c_suspend);
  714. break;
  715. case USB_PORT_FEAT_POWER:
  716. if (HCS_PPC (ehci->hcs_params))
  717. ehci_writel(ehci,
  718. temp & ~(PORT_RWC_BITS | PORT_POWER),
  719. status_reg);
  720. break;
  721. case USB_PORT_FEAT_C_CONNECTION:
  722. if (ehci->has_lpm) {
  723. /* clear PORTSC bits on disconnect */
  724. temp &= ~PORT_LPM;
  725. temp &= ~PORT_DEV_ADDR;
  726. }
  727. ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
  728. status_reg);
  729. break;
  730. case USB_PORT_FEAT_C_OVER_CURRENT:
  731. ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
  732. status_reg);
  733. break;
  734. case USB_PORT_FEAT_C_RESET:
  735. /* GetPortStatus clears reset */
  736. break;
  737. default:
  738. goto error;
  739. }
  740. ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
  741. break;
  742. case GetHubDescriptor:
  743. ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
  744. buf);
  745. break;
  746. case GetHubStatus:
  747. /* no hub-wide feature/status flags */
  748. memset (buf, 0, 4);
  749. //cpu_to_le32s ((u32 *) buf);
  750. break;
  751. case GetPortStatus:
  752. if (!wIndex || wIndex > ports)
  753. goto error;
  754. wIndex--;
  755. status = 0;
  756. temp = ehci_readl(ehci, status_reg);
  757. // wPortChange bits
  758. if (temp & PORT_CSC)
  759. status |= USB_PORT_STAT_C_CONNECTION << 16;
  760. if (temp & PORT_PEC)
  761. status |= USB_PORT_STAT_C_ENABLE << 16;
  762. if ((temp & PORT_OCC) && !ignore_oc){
  763. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  764. /*
  765. * Hubs should disable port power on over-current.
  766. * However, not all EHCI implementations do this
  767. * automatically, even if they _do_ support per-port
  768. * power switching; they're allowed to just limit the
  769. * current. khubd will turn the power back on.
  770. */
  771. if (HCS_PPC (ehci->hcs_params)){
  772. ehci_writel(ehci,
  773. temp & ~(PORT_RWC_BITS | PORT_POWER),
  774. status_reg);
  775. }
  776. }
  777. /* whoever resumes must GetPortStatus to complete it!! */
  778. if (temp & PORT_RESUME) {
  779. /* Remote Wakeup received? */
  780. if (!ehci->reset_done[wIndex]) {
  781. /* resume signaling for 20 msec */
  782. ehci->reset_done[wIndex] = jiffies
  783. + msecs_to_jiffies(20);
  784. /* check the port again */
  785. mod_timer(&ehci_to_hcd(ehci)->rh_timer,
  786. ehci->reset_done[wIndex]);
  787. }
  788. /* resume completed? */
  789. else if (time_after_eq(jiffies,
  790. ehci->reset_done[wIndex])) {
  791. clear_bit(wIndex, &ehci->suspended_ports);
  792. set_bit(wIndex, &ehci->port_c_suspend);
  793. ehci->reset_done[wIndex] = 0;
  794. /* stop resume signaling */
  795. temp = ehci_readl(ehci, status_reg);
  796. ehci_writel(ehci,
  797. temp & ~(PORT_RWC_BITS | PORT_RESUME),
  798. status_reg);
  799. retval = handshake(ehci, status_reg,
  800. PORT_RESUME, 0, 2000 /* 2msec */);
  801. if (retval != 0) {
  802. ehci_err(ehci,
  803. "port %d resume error %d\n",
  804. wIndex + 1, retval);
  805. goto error;
  806. }
  807. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  808. }
  809. }
  810. /* whoever resets must GetPortStatus to complete it!! */
  811. if ((temp & PORT_RESET)
  812. && time_after_eq(jiffies,
  813. ehci->reset_done[wIndex])) {
  814. status |= USB_PORT_STAT_C_RESET << 16;
  815. ehci->reset_done [wIndex] = 0;
  816. /* force reset to complete */
  817. ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
  818. status_reg);
  819. /* REVISIT: some hardware needs 550+ usec to clear
  820. * this bit; seems too long to spin routinely...
  821. */
  822. retval = handshake(ehci, status_reg,
  823. PORT_RESET, 0, 1000);
  824. if (retval != 0) {
  825. ehci_err (ehci, "port %d reset error %d\n",
  826. wIndex + 1, retval);
  827. goto error;
  828. }
  829. /* see what we found out */
  830. temp = check_reset_complete (ehci, wIndex, status_reg,
  831. ehci_readl(ehci, status_reg));
  832. }
  833. if (!(temp & (PORT_RESUME|PORT_RESET)))
  834. ehci->reset_done[wIndex] = 0;
  835. /* transfer dedicated ports to the companion hc */
  836. if ((temp & PORT_CONNECT) &&
  837. test_bit(wIndex, &ehci->companion_ports)) {
  838. temp &= ~PORT_RWC_BITS;
  839. temp |= PORT_OWNER;
  840. ehci_writel(ehci, temp, status_reg);
  841. ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
  842. temp = ehci_readl(ehci, status_reg);
  843. }
  844. /*
  845. * Even if OWNER is set, there's no harm letting khubd
  846. * see the wPortStatus values (they should all be 0 except
  847. * for PORT_POWER anyway).
  848. */
  849. if (temp & PORT_CONNECT) {
  850. status |= USB_PORT_STAT_CONNECTION;
  851. // status may be from integrated TT
  852. if (ehci->has_hostpc) {
  853. temp1 = ehci_readl(ehci, hostpc_reg);
  854. status |= ehci_port_speed(ehci, temp1);
  855. } else
  856. status |= ehci_port_speed(ehci, temp);
  857. }
  858. if (temp & PORT_PE)
  859. status |= USB_PORT_STAT_ENABLE;
  860. /* maybe the port was unsuspended without our knowledge */
  861. if (temp & (PORT_SUSPEND|PORT_RESUME)) {
  862. status |= USB_PORT_STAT_SUSPEND;
  863. } else if (test_bit(wIndex, &ehci->suspended_ports)) {
  864. clear_bit(wIndex, &ehci->suspended_ports);
  865. ehci->reset_done[wIndex] = 0;
  866. if (temp & PORT_PE)
  867. set_bit(wIndex, &ehci->port_c_suspend);
  868. }
  869. if (temp & PORT_OC)
  870. status |= USB_PORT_STAT_OVERCURRENT;
  871. if (temp & PORT_RESET)
  872. status |= USB_PORT_STAT_RESET;
  873. if (temp & PORT_POWER)
  874. status |= USB_PORT_STAT_POWER;
  875. if (test_bit(wIndex, &ehci->port_c_suspend))
  876. status |= USB_PORT_STAT_C_SUSPEND << 16;
  877. #ifndef VERBOSE_DEBUG
  878. if (status & ~0xffff) /* only if wPortChange is interesting */
  879. #endif
  880. dbg_port (ehci, "GetStatus", wIndex + 1, temp);
  881. put_unaligned_le32(status, buf);
  882. break;
  883. case SetHubFeature:
  884. switch (wValue) {
  885. case C_HUB_LOCAL_POWER:
  886. case C_HUB_OVER_CURRENT:
  887. /* no hub-wide feature/status flags */
  888. break;
  889. default:
  890. goto error;
  891. }
  892. break;
  893. case SetPortFeature:
  894. selector = wIndex >> 8;
  895. wIndex &= 0xff;
  896. if (unlikely(ehci->debug)) {
  897. /* If the debug port is active any port
  898. * feature requests should get denied */
  899. if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
  900. (readl(&ehci->debug->control) & DBGP_ENABLED)) {
  901. retval = -ENODEV;
  902. goto error_exit;
  903. }
  904. }
  905. if (!wIndex || wIndex > ports)
  906. goto error;
  907. wIndex--;
  908. temp = ehci_readl(ehci, status_reg);
  909. if (temp & PORT_OWNER)
  910. break;
  911. temp &= ~PORT_RWC_BITS;
  912. switch (wValue) {
  913. case USB_PORT_FEAT_SUSPEND:
  914. if (ehci->no_selective_suspend)
  915. break;
  916. if ((temp & PORT_PE) == 0
  917. || (temp & PORT_RESET) != 0)
  918. goto error;
  919. /* After above check the port must be connected.
  920. * Set appropriate bit thus could put phy into low power
  921. * mode if we have hostpc feature
  922. */
  923. temp &= ~PORT_WKCONN_E;
  924. temp |= PORT_WKDISC_E | PORT_WKOC_E;
  925. ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
  926. if (hostpc_reg) {
  927. spin_unlock_irqrestore(&ehci->lock, flags);
  928. msleep(5);/* 5ms for HCD enter low pwr mode */
  929. spin_lock_irqsave(&ehci->lock, flags);
  930. temp1 = ehci_readl(ehci, hostpc_reg);
  931. ehci_writel(ehci, temp1 | HOSTPC_PHCD,
  932. hostpc_reg);
  933. temp1 = ehci_readl(ehci, hostpc_reg);
  934. ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
  935. wIndex, (temp1 & HOSTPC_PHCD) ?
  936. "succeeded" : "failed");
  937. }
  938. set_bit(wIndex, &ehci->suspended_ports);
  939. break;
  940. case USB_PORT_FEAT_POWER:
  941. if (HCS_PPC (ehci->hcs_params))
  942. ehci_writel(ehci, temp | PORT_POWER,
  943. status_reg);
  944. break;
  945. case USB_PORT_FEAT_RESET:
  946. if (temp & PORT_RESUME)
  947. goto error;
  948. /* line status bits may report this as low speed,
  949. * which can be fine if this root hub has a
  950. * transaction translator built in.
  951. */
  952. if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
  953. && !ehci_is_TDI(ehci)
  954. && PORT_USB11 (temp)) {
  955. ehci_dbg (ehci,
  956. "port %d low speed --> companion\n",
  957. wIndex + 1);
  958. temp |= PORT_OWNER;
  959. } else {
  960. ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
  961. temp |= PORT_RESET;
  962. temp &= ~PORT_PE;
  963. /*
  964. * caller must wait, then call GetPortStatus
  965. * usb 2.0 spec says 50 ms resets on root
  966. */
  967. ehci->reset_done [wIndex] = jiffies
  968. + msecs_to_jiffies (50);
  969. }
  970. ehci_writel(ehci, temp, status_reg);
  971. break;
  972. /* For downstream facing ports (these): one hub port is put
  973. * into test mode according to USB2 11.24.2.13, then the hub
  974. * must be reset (which for root hub now means rmmod+modprobe,
  975. * or else system reboot). See EHCI 2.3.9 and 4.14 for info
  976. * about the EHCI-specific stuff.
  977. */
  978. case USB_PORT_FEAT_TEST:
  979. if (!selector || selector > 5)
  980. goto error;
  981. ehci_quiesce(ehci);
  982. ehci_halt(ehci);
  983. temp |= selector << 16;
  984. ehci_writel(ehci, temp, status_reg);
  985. break;
  986. default:
  987. goto error;
  988. }
  989. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  990. break;
  991. default:
  992. error:
  993. /* "stall" on error */
  994. retval = -EPIPE;
  995. }
  996. error_exit:
  997. spin_unlock_irqrestore (&ehci->lock, flags);
  998. return retval;
  999. }
  1000. static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
  1001. {
  1002. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  1003. if (ehci_is_TDI(ehci))
  1004. return;
  1005. set_owner(ehci, --portnum, PORT_OWNER);
  1006. }
  1007. static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
  1008. {
  1009. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  1010. u32 __iomem *reg;
  1011. if (ehci_is_TDI(ehci))
  1012. return 0;
  1013. reg = &ehci->regs->port_status[portnum - 1];
  1014. return ehci_readl(ehci, reg) & PORT_OWNER;
  1015. }