ehci-hub.c 29 KB

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