ehci-hub.c 31 KB

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