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