ehci-hub.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. spin_unlock_irq (&ehci->lock);
  264. /* ehci_work() may have re-enabled the watchdog timer, which we do not
  265. * want, and so we must delete any pending watchdog timer events.
  266. */
  267. del_timer_sync(&ehci->watchdog);
  268. return 0;
  269. }
  270. /* caller has locked the root hub, and should reset/reinit on error */
  271. static int ehci_bus_resume (struct usb_hcd *hcd)
  272. {
  273. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  274. u32 temp;
  275. u32 power_okay;
  276. int i;
  277. unsigned long resume_needed = 0;
  278. if (time_before (jiffies, ehci->next_statechange))
  279. msleep(5);
  280. spin_lock_irq (&ehci->lock);
  281. if (!HCD_HW_ACCESSIBLE(hcd)) {
  282. spin_unlock_irq(&ehci->lock);
  283. return -ESHUTDOWN;
  284. }
  285. if (unlikely(ehci->debug)) {
  286. if (!dbgp_reset_prep())
  287. ehci->debug = NULL;
  288. else
  289. dbgp_external_startup();
  290. }
  291. /* Ideally and we've got a real resume here, and no port's power
  292. * was lost. (For PCI, that means Vaux was maintained.) But we
  293. * could instead be restoring a swsusp snapshot -- so that BIOS was
  294. * the last user of the controller, not reset/pm hardware keeping
  295. * state we gave to it.
  296. */
  297. power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
  298. ehci_dbg(ehci, "resume root hub%s\n",
  299. power_okay ? "" : " after power loss");
  300. /* at least some APM implementations will try to deliver
  301. * IRQs right away, so delay them until we're ready.
  302. */
  303. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  304. /* re-init operational registers */
  305. ehci_writel(ehci, 0, &ehci->regs->segment);
  306. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  307. ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
  308. /* restore CMD_RUN, framelist size, and irq threshold */
  309. ehci->command |= CMD_RUN;
  310. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  311. ehci->rh_state = EHCI_RH_RUNNING;
  312. /* Some controller/firmware combinations need a delay during which
  313. * they set up the port statuses. See Bugzilla #8190. */
  314. spin_unlock_irq(&ehci->lock);
  315. msleep(8);
  316. spin_lock_irq(&ehci->lock);
  317. /* clear phy low-power mode before resume */
  318. if (ehci->bus_suspended && ehci->has_hostpc) {
  319. i = HCS_N_PORTS(ehci->hcs_params);
  320. while (i--) {
  321. if (test_bit(i, &ehci->bus_suspended)) {
  322. u32 __iomem *hostpc_reg =
  323. &ehci->regs->hostpc[i];
  324. temp = ehci_readl(ehci, hostpc_reg);
  325. ehci_writel(ehci, temp & ~HOSTPC_PHCD,
  326. hostpc_reg);
  327. }
  328. }
  329. spin_unlock_irq(&ehci->lock);
  330. msleep(5);
  331. spin_lock_irq(&ehci->lock);
  332. }
  333. /* manually resume the ports we suspended during bus_suspend() */
  334. i = HCS_N_PORTS (ehci->hcs_params);
  335. while (i--) {
  336. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  337. temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
  338. if (test_bit(i, &ehci->bus_suspended) &&
  339. (temp & PORT_SUSPEND)) {
  340. temp |= PORT_RESUME;
  341. set_bit(i, &resume_needed);
  342. }
  343. ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
  344. }
  345. /* msleep for 20ms only if code is trying to resume port */
  346. if (resume_needed) {
  347. spin_unlock_irq(&ehci->lock);
  348. msleep(20);
  349. spin_lock_irq(&ehci->lock);
  350. }
  351. i = HCS_N_PORTS (ehci->hcs_params);
  352. while (i--) {
  353. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  354. if (test_bit(i, &resume_needed)) {
  355. temp &= ~(PORT_RWC_BITS | PORT_RESUME);
  356. ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
  357. ehci_vdbg (ehci, "resumed port %d\n", i + 1);
  358. }
  359. }
  360. ehci->next_statechange = jiffies + msecs_to_jiffies(5);
  361. /* Now we can safely re-enable irqs */
  362. ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
  363. (void) ehci_readl(ehci, &ehci->regs->intr_enable);
  364. spin_unlock_irq (&ehci->lock);
  365. ehci_handover_companion_ports(ehci);
  366. return 0;
  367. }
  368. #else
  369. #define ehci_bus_suspend NULL
  370. #define ehci_bus_resume NULL
  371. #endif /* CONFIG_PM */
  372. /*-------------------------------------------------------------------------*/
  373. /*
  374. * Sets the owner of a port
  375. */
  376. static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
  377. {
  378. u32 __iomem *status_reg;
  379. u32 port_status;
  380. int try;
  381. status_reg = &ehci->regs->port_status[portnum];
  382. /*
  383. * The controller won't set the OWNER bit if the port is
  384. * enabled, so this loop will sometimes require at least two
  385. * iterations: one to disable the port and one to set OWNER.
  386. */
  387. for (try = 4; try > 0; --try) {
  388. spin_lock_irq(&ehci->lock);
  389. port_status = ehci_readl(ehci, status_reg);
  390. if ((port_status & PORT_OWNER) == new_owner
  391. || (port_status & (PORT_OWNER | PORT_CONNECT))
  392. == 0)
  393. try = 0;
  394. else {
  395. port_status ^= PORT_OWNER;
  396. port_status &= ~(PORT_PE | PORT_RWC_BITS);
  397. ehci_writel(ehci, port_status, status_reg);
  398. }
  399. spin_unlock_irq(&ehci->lock);
  400. if (try > 1)
  401. msleep(5);
  402. }
  403. }
  404. /*-------------------------------------------------------------------------*/
  405. static int check_reset_complete (
  406. struct ehci_hcd *ehci,
  407. int index,
  408. u32 __iomem *status_reg,
  409. int port_status
  410. ) {
  411. if (!(port_status & PORT_CONNECT))
  412. return port_status;
  413. /* if reset finished and it's still not enabled -- handoff */
  414. if (!(port_status & PORT_PE)) {
  415. /* with integrated TT, there's nobody to hand it to! */
  416. if (ehci_is_TDI(ehci)) {
  417. ehci_dbg (ehci,
  418. "Failed to enable port %d on root hub TT\n",
  419. index+1);
  420. return port_status;
  421. }
  422. ehci_dbg (ehci, "port %d full speed --> companion\n",
  423. index + 1);
  424. // what happens if HCS_N_CC(params) == 0 ?
  425. port_status |= PORT_OWNER;
  426. port_status &= ~PORT_RWC_BITS;
  427. ehci_writel(ehci, port_status, status_reg);
  428. /* ensure 440EPX ohci controller state is operational */
  429. if (ehci->has_amcc_usb23)
  430. set_ohci_hcfs(ehci, 1);
  431. } else {
  432. ehci_dbg(ehci, "port %d reset complete, port enabled\n",
  433. index + 1);
  434. /* ensure 440EPx ohci controller state is suspended */
  435. if (ehci->has_amcc_usb23)
  436. set_ohci_hcfs(ehci, 0);
  437. }
  438. return port_status;
  439. }
  440. /*-------------------------------------------------------------------------*/
  441. /* build "status change" packet (one or two bytes) from HC registers */
  442. static int
  443. ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
  444. {
  445. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  446. u32 temp, status;
  447. u32 mask;
  448. int ports, i, retval = 1;
  449. unsigned long flags;
  450. u32 ppcd = 0;
  451. /* init status to no-changes */
  452. buf [0] = 0;
  453. ports = HCS_N_PORTS (ehci->hcs_params);
  454. if (ports > 7) {
  455. buf [1] = 0;
  456. retval++;
  457. }
  458. /* Inform the core about resumes-in-progress by returning
  459. * a non-zero value even if there are no status changes.
  460. */
  461. status = ehci->resuming_ports;
  462. /* Some boards (mostly VIA?) report bogus overcurrent indications,
  463. * causing massive log spam unless we completely ignore them. It
  464. * may be relevant that VIA VT8235 controllers, where PORT_POWER is
  465. * always set, seem to clear PORT_OCC and PORT_CSC when writing to
  466. * PORT_POWER; that's surprising, but maybe within-spec.
  467. */
  468. if (!ignore_oc)
  469. mask = PORT_CSC | PORT_PEC | PORT_OCC;
  470. else
  471. mask = PORT_CSC | PORT_PEC;
  472. // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
  473. /* no hub change reports (bit 0) for now (power, ...) */
  474. /* port N changes (bit N)? */
  475. spin_lock_irqsave (&ehci->lock, flags);
  476. /* get per-port change detect bits */
  477. if (ehci->has_ppcd)
  478. ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
  479. for (i = 0; i < ports; i++) {
  480. /* leverage per-port change bits feature */
  481. if (ehci->has_ppcd && !(ppcd & (1 << i)))
  482. continue;
  483. temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
  484. /*
  485. * Return status information even for ports with OWNER set.
  486. * Otherwise khubd wouldn't see the disconnect event when a
  487. * high-speed device is switched over to the companion
  488. * controller by the user.
  489. */
  490. if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
  491. || (ehci->reset_done[i] && time_after_eq(
  492. jiffies, ehci->reset_done[i]))) {
  493. if (i < 7)
  494. buf [0] |= 1 << (i + 1);
  495. else
  496. buf [1] |= 1 << (i - 7);
  497. status = STS_PCD;
  498. }
  499. }
  500. /* FIXME autosuspend idle root hubs */
  501. spin_unlock_irqrestore (&ehci->lock, flags);
  502. return status ? retval : 0;
  503. }
  504. /*-------------------------------------------------------------------------*/
  505. static void
  506. ehci_hub_descriptor (
  507. struct ehci_hcd *ehci,
  508. struct usb_hub_descriptor *desc
  509. ) {
  510. int ports = HCS_N_PORTS (ehci->hcs_params);
  511. u16 temp;
  512. desc->bDescriptorType = 0x29;
  513. desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
  514. desc->bHubContrCurrent = 0;
  515. desc->bNbrPorts = ports;
  516. temp = 1 + (ports / 8);
  517. desc->bDescLength = 7 + 2 * temp;
  518. /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  519. memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
  520. memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
  521. temp = 0x0008; /* per-port overcurrent reporting */
  522. if (HCS_PPC (ehci->hcs_params))
  523. temp |= 0x0001; /* per-port power control */
  524. else
  525. temp |= 0x0002; /* no power switching */
  526. #if 0
  527. // re-enable when we support USB_PORT_FEAT_INDICATOR below.
  528. if (HCS_INDICATOR (ehci->hcs_params))
  529. temp |= 0x0080; /* per-port indicators (LEDs) */
  530. #endif
  531. desc->wHubCharacteristics = cpu_to_le16(temp);
  532. }
  533. /*-------------------------------------------------------------------------*/
  534. static int ehci_hub_control (
  535. struct usb_hcd *hcd,
  536. u16 typeReq,
  537. u16 wValue,
  538. u16 wIndex,
  539. char *buf,
  540. u16 wLength
  541. ) {
  542. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  543. int ports = HCS_N_PORTS (ehci->hcs_params);
  544. u32 __iomem *status_reg = &ehci->regs->port_status[
  545. (wIndex & 0xff) - 1];
  546. u32 __iomem *hostpc_reg = &ehci->regs->hostpc[(wIndex & 0xff) - 1];
  547. u32 temp, temp1, status;
  548. unsigned long flags;
  549. int retval = 0;
  550. unsigned selector;
  551. /*
  552. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  553. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  554. * (track current state ourselves) ... blink for diagnostics,
  555. * power, "this is the one", etc. EHCI spec supports this.
  556. */
  557. spin_lock_irqsave (&ehci->lock, flags);
  558. switch (typeReq) {
  559. case ClearHubFeature:
  560. switch (wValue) {
  561. case C_HUB_LOCAL_POWER:
  562. case C_HUB_OVER_CURRENT:
  563. /* no hub-wide feature/status flags */
  564. break;
  565. default:
  566. goto error;
  567. }
  568. break;
  569. case ClearPortFeature:
  570. if (!wIndex || wIndex > ports)
  571. goto error;
  572. wIndex--;
  573. temp = ehci_readl(ehci, status_reg);
  574. temp &= ~PORT_RWC_BITS;
  575. /*
  576. * Even if OWNER is set, so the port is owned by the
  577. * companion controller, khubd needs to be able to clear
  578. * the port-change status bits (especially
  579. * USB_PORT_STAT_C_CONNECTION).
  580. */
  581. switch (wValue) {
  582. case USB_PORT_FEAT_ENABLE:
  583. ehci_writel(ehci, temp & ~PORT_PE, status_reg);
  584. break;
  585. case USB_PORT_FEAT_C_ENABLE:
  586. ehci_writel(ehci, temp | PORT_PEC, status_reg);
  587. break;
  588. case USB_PORT_FEAT_SUSPEND:
  589. if (temp & PORT_RESET)
  590. goto error;
  591. if (ehci->no_selective_suspend)
  592. break;
  593. #ifdef CONFIG_USB_OTG
  594. if ((hcd->self.otg_port == (wIndex + 1))
  595. && hcd->self.b_hnp_enable) {
  596. otg_start_hnp(hcd->phy->otg);
  597. break;
  598. }
  599. #endif
  600. if (!(temp & PORT_SUSPEND))
  601. break;
  602. if ((temp & PORT_PE) == 0)
  603. goto error;
  604. /* clear phy low-power mode before resume */
  605. if (ehci->has_hostpc) {
  606. temp1 = ehci_readl(ehci, hostpc_reg);
  607. ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
  608. hostpc_reg);
  609. spin_unlock_irqrestore(&ehci->lock, flags);
  610. msleep(5);/* wait to leave low-power mode */
  611. spin_lock_irqsave(&ehci->lock, flags);
  612. }
  613. /* resume signaling for 20 msec */
  614. temp &= ~PORT_WAKE_BITS;
  615. ehci_writel(ehci, temp | PORT_RESUME, status_reg);
  616. ehci->reset_done[wIndex] = jiffies
  617. + msecs_to_jiffies(20);
  618. break;
  619. case USB_PORT_FEAT_C_SUSPEND:
  620. clear_bit(wIndex, &ehci->port_c_suspend);
  621. break;
  622. case USB_PORT_FEAT_POWER:
  623. if (HCS_PPC (ehci->hcs_params))
  624. ehci_writel(ehci, temp & ~PORT_POWER,
  625. status_reg);
  626. break;
  627. case USB_PORT_FEAT_C_CONNECTION:
  628. if (ehci->has_lpm) {
  629. /* clear PORTSC bits on disconnect */
  630. temp &= ~PORT_LPM;
  631. temp &= ~PORT_DEV_ADDR;
  632. }
  633. ehci_writel(ehci, temp | PORT_CSC, status_reg);
  634. break;
  635. case USB_PORT_FEAT_C_OVER_CURRENT:
  636. ehci_writel(ehci, temp | PORT_OCC, status_reg);
  637. break;
  638. case USB_PORT_FEAT_C_RESET:
  639. /* GetPortStatus clears reset */
  640. break;
  641. default:
  642. goto error;
  643. }
  644. ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
  645. break;
  646. case GetHubDescriptor:
  647. ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
  648. buf);
  649. break;
  650. case GetHubStatus:
  651. /* no hub-wide feature/status flags */
  652. memset (buf, 0, 4);
  653. //cpu_to_le32s ((u32 *) buf);
  654. break;
  655. case GetPortStatus:
  656. if (!wIndex || wIndex > ports)
  657. goto error;
  658. wIndex--;
  659. status = 0;
  660. temp = ehci_readl(ehci, status_reg);
  661. // wPortChange bits
  662. if (temp & PORT_CSC)
  663. status |= USB_PORT_STAT_C_CONNECTION << 16;
  664. if (temp & PORT_PEC)
  665. status |= USB_PORT_STAT_C_ENABLE << 16;
  666. if ((temp & PORT_OCC) && !ignore_oc){
  667. status |= USB_PORT_STAT_C_OVERCURRENT << 16;
  668. /*
  669. * Hubs should disable port power on over-current.
  670. * However, not all EHCI implementations do this
  671. * automatically, even if they _do_ support per-port
  672. * power switching; they're allowed to just limit the
  673. * current. khubd will turn the power back on.
  674. */
  675. if ((temp & PORT_OC) && HCS_PPC(ehci->hcs_params)) {
  676. ehci_writel(ehci,
  677. temp & ~(PORT_RWC_BITS | PORT_POWER),
  678. status_reg);
  679. temp = ehci_readl(ehci, status_reg);
  680. }
  681. }
  682. /* whoever resumes must GetPortStatus to complete it!! */
  683. if (temp & PORT_RESUME) {
  684. /* Remote Wakeup received? */
  685. if (!ehci->reset_done[wIndex]) {
  686. /* resume signaling for 20 msec */
  687. ehci->reset_done[wIndex] = jiffies
  688. + msecs_to_jiffies(20);
  689. /* check the port again */
  690. mod_timer(&ehci_to_hcd(ehci)->rh_timer,
  691. ehci->reset_done[wIndex]);
  692. }
  693. /* resume completed? */
  694. else if (time_after_eq(jiffies,
  695. ehci->reset_done[wIndex])) {
  696. clear_bit(wIndex, &ehci->suspended_ports);
  697. set_bit(wIndex, &ehci->port_c_suspend);
  698. ehci->reset_done[wIndex] = 0;
  699. /* stop resume signaling */
  700. temp = ehci_readl(ehci, status_reg);
  701. ehci_writel(ehci,
  702. temp & ~(PORT_RWC_BITS | PORT_RESUME),
  703. status_reg);
  704. clear_bit(wIndex, &ehci->resuming_ports);
  705. retval = handshake(ehci, status_reg,
  706. PORT_RESUME, 0, 2000 /* 2msec */);
  707. if (retval != 0) {
  708. ehci_err(ehci,
  709. "port %d resume error %d\n",
  710. wIndex + 1, retval);
  711. goto error;
  712. }
  713. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  714. }
  715. }
  716. /* whoever resets must GetPortStatus to complete it!! */
  717. if ((temp & PORT_RESET)
  718. && time_after_eq(jiffies,
  719. ehci->reset_done[wIndex])) {
  720. status |= USB_PORT_STAT_C_RESET << 16;
  721. ehci->reset_done [wIndex] = 0;
  722. clear_bit(wIndex, &ehci->resuming_ports);
  723. /* force reset to complete */
  724. ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
  725. status_reg);
  726. /* REVISIT: some hardware needs 550+ usec to clear
  727. * this bit; seems too long to spin routinely...
  728. */
  729. retval = handshake(ehci, status_reg,
  730. PORT_RESET, 0, 1000);
  731. if (retval != 0) {
  732. ehci_err (ehci, "port %d reset error %d\n",
  733. wIndex + 1, retval);
  734. goto error;
  735. }
  736. /* see what we found out */
  737. temp = check_reset_complete (ehci, wIndex, status_reg,
  738. ehci_readl(ehci, status_reg));
  739. }
  740. if (!(temp & (PORT_RESUME|PORT_RESET))) {
  741. ehci->reset_done[wIndex] = 0;
  742. clear_bit(wIndex, &ehci->resuming_ports);
  743. }
  744. /* transfer dedicated ports to the companion hc */
  745. if ((temp & PORT_CONNECT) &&
  746. test_bit(wIndex, &ehci->companion_ports)) {
  747. temp &= ~PORT_RWC_BITS;
  748. temp |= PORT_OWNER;
  749. ehci_writel(ehci, temp, status_reg);
  750. ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
  751. temp = ehci_readl(ehci, status_reg);
  752. }
  753. /*
  754. * Even if OWNER is set, there's no harm letting khubd
  755. * see the wPortStatus values (they should all be 0 except
  756. * for PORT_POWER anyway).
  757. */
  758. if (temp & PORT_CONNECT) {
  759. status |= USB_PORT_STAT_CONNECTION;
  760. // status may be from integrated TT
  761. if (ehci->has_hostpc) {
  762. temp1 = ehci_readl(ehci, hostpc_reg);
  763. status |= ehci_port_speed(ehci, temp1);
  764. } else
  765. status |= ehci_port_speed(ehci, temp);
  766. }
  767. if (temp & PORT_PE)
  768. status |= USB_PORT_STAT_ENABLE;
  769. /* maybe the port was unsuspended without our knowledge */
  770. if (temp & (PORT_SUSPEND|PORT_RESUME)) {
  771. status |= USB_PORT_STAT_SUSPEND;
  772. } else if (test_bit(wIndex, &ehci->suspended_ports)) {
  773. clear_bit(wIndex, &ehci->suspended_ports);
  774. clear_bit(wIndex, &ehci->resuming_ports);
  775. ehci->reset_done[wIndex] = 0;
  776. if (temp & PORT_PE)
  777. set_bit(wIndex, &ehci->port_c_suspend);
  778. }
  779. if (temp & PORT_OC)
  780. status |= USB_PORT_STAT_OVERCURRENT;
  781. if (temp & PORT_RESET)
  782. status |= USB_PORT_STAT_RESET;
  783. if (temp & PORT_POWER)
  784. status |= USB_PORT_STAT_POWER;
  785. if (test_bit(wIndex, &ehci->port_c_suspend))
  786. status |= USB_PORT_STAT_C_SUSPEND << 16;
  787. #ifndef VERBOSE_DEBUG
  788. if (status & ~0xffff) /* only if wPortChange is interesting */
  789. #endif
  790. dbg_port (ehci, "GetStatus", wIndex + 1, temp);
  791. put_unaligned_le32(status, buf);
  792. break;
  793. case SetHubFeature:
  794. switch (wValue) {
  795. case C_HUB_LOCAL_POWER:
  796. case C_HUB_OVER_CURRENT:
  797. /* no hub-wide feature/status flags */
  798. break;
  799. default:
  800. goto error;
  801. }
  802. break;
  803. case SetPortFeature:
  804. selector = wIndex >> 8;
  805. wIndex &= 0xff;
  806. if (unlikely(ehci->debug)) {
  807. /* If the debug port is active any port
  808. * feature requests should get denied */
  809. if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
  810. (readl(&ehci->debug->control) & DBGP_ENABLED)) {
  811. retval = -ENODEV;
  812. goto error_exit;
  813. }
  814. }
  815. if (!wIndex || wIndex > ports)
  816. goto error;
  817. wIndex--;
  818. temp = ehci_readl(ehci, status_reg);
  819. if (temp & PORT_OWNER)
  820. break;
  821. temp &= ~PORT_RWC_BITS;
  822. switch (wValue) {
  823. case USB_PORT_FEAT_SUSPEND:
  824. if (ehci->no_selective_suspend)
  825. break;
  826. if ((temp & PORT_PE) == 0
  827. || (temp & PORT_RESET) != 0)
  828. goto error;
  829. /* After above check the port must be connected.
  830. * Set appropriate bit thus could put phy into low power
  831. * mode if we have hostpc feature
  832. */
  833. temp &= ~PORT_WKCONN_E;
  834. temp |= PORT_WKDISC_E | PORT_WKOC_E;
  835. ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
  836. if (ehci->has_hostpc) {
  837. spin_unlock_irqrestore(&ehci->lock, flags);
  838. msleep(5);/* 5ms for HCD enter low pwr mode */
  839. spin_lock_irqsave(&ehci->lock, flags);
  840. temp1 = ehci_readl(ehci, hostpc_reg);
  841. ehci_writel(ehci, temp1 | HOSTPC_PHCD,
  842. hostpc_reg);
  843. temp1 = ehci_readl(ehci, hostpc_reg);
  844. ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
  845. wIndex, (temp1 & HOSTPC_PHCD) ?
  846. "succeeded" : "failed");
  847. }
  848. set_bit(wIndex, &ehci->suspended_ports);
  849. break;
  850. case USB_PORT_FEAT_POWER:
  851. if (HCS_PPC (ehci->hcs_params))
  852. ehci_writel(ehci, temp | PORT_POWER,
  853. status_reg);
  854. break;
  855. case USB_PORT_FEAT_RESET:
  856. if (temp & PORT_RESUME)
  857. goto error;
  858. /* line status bits may report this as low speed,
  859. * which can be fine if this root hub has a
  860. * transaction translator built in.
  861. */
  862. if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
  863. && !ehci_is_TDI(ehci)
  864. && PORT_USB11 (temp)) {
  865. ehci_dbg (ehci,
  866. "port %d low speed --> companion\n",
  867. wIndex + 1);
  868. temp |= PORT_OWNER;
  869. } else {
  870. ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
  871. temp |= PORT_RESET;
  872. temp &= ~PORT_PE;
  873. /*
  874. * caller must wait, then call GetPortStatus
  875. * usb 2.0 spec says 50 ms resets on root
  876. */
  877. ehci->reset_done [wIndex] = jiffies
  878. + msecs_to_jiffies (50);
  879. }
  880. ehci_writel(ehci, temp, status_reg);
  881. break;
  882. /* For downstream facing ports (these): one hub port is put
  883. * into test mode according to USB2 11.24.2.13, then the hub
  884. * must be reset (which for root hub now means rmmod+modprobe,
  885. * or else system reboot). See EHCI 2.3.9 and 4.14 for info
  886. * about the EHCI-specific stuff.
  887. */
  888. case USB_PORT_FEAT_TEST:
  889. if (!selector || selector > 5)
  890. goto error;
  891. ehci_quiesce(ehci);
  892. /* Put all enabled ports into suspend */
  893. while (ports--) {
  894. u32 __iomem *sreg =
  895. &ehci->regs->port_status[ports];
  896. temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
  897. if (temp & PORT_PE)
  898. ehci_writel(ehci, temp | PORT_SUSPEND,
  899. sreg);
  900. }
  901. ehci_halt(ehci);
  902. temp = ehci_readl(ehci, status_reg);
  903. temp |= selector << 16;
  904. ehci_writel(ehci, temp, status_reg);
  905. break;
  906. default:
  907. goto error;
  908. }
  909. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  910. break;
  911. default:
  912. error:
  913. /* "stall" on error */
  914. retval = -EPIPE;
  915. }
  916. error_exit:
  917. spin_unlock_irqrestore (&ehci->lock, flags);
  918. return retval;
  919. }
  920. static void __maybe_unused ehci_relinquish_port(struct usb_hcd *hcd,
  921. int portnum)
  922. {
  923. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  924. if (ehci_is_TDI(ehci))
  925. return;
  926. set_owner(ehci, --portnum, PORT_OWNER);
  927. }
  928. static int __maybe_unused ehci_port_handed_over(struct usb_hcd *hcd,
  929. int portnum)
  930. {
  931. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  932. u32 __iomem *reg;
  933. if (ehci_is_TDI(ehci))
  934. return 0;
  935. reg = &ehci->regs->port_status[portnum - 1];
  936. return ehci_readl(ehci, reg) & PORT_OWNER;
  937. }