ehci-hub.c 30 KB

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