ehci-hub.c 30 KB

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