ehci-hub.c 30 KB

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