ehci-hub.c 30 KB

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