ehci-hub.c 30 KB

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