ehci-hub.c 31 KB

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