ehci-hub.c 29 KB

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