ehci-hub.c 29 KB

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