ehci-hub.c 31 KB

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