ehci-hub.c 29 KB

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