ehci-hub.c 30 KB

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