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