adm5120-hub.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * ADM5120 HCD (Host Controller Driver) for USB
  3. *
  4. * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This file was derived from: drivers/usb/host/ohci-hub.c
  7. * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  8. * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. *
  14. */
  15. /*-------------------------------------------------------------------------*/
  16. /*
  17. * ADM5120 Root Hub ... the nonsharable stuff
  18. */
  19. #define dbg_port(hc, label, num, value) \
  20. admhc_dbg(hc, \
  21. "%s port%d " \
  22. "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
  23. label, num, value, \
  24. (value & ADMHC_PS_PRSC) ? " PRSC" : "", \
  25. (value & ADMHC_PS_OCIC) ? " OCIC" : "", \
  26. (value & ADMHC_PS_PSSC) ? " PSSC" : "", \
  27. (value & ADMHC_PS_PESC) ? " PESC" : "", \
  28. (value & ADMHC_PS_CSC) ? " CSC" : "", \
  29. \
  30. (value & ADMHC_PS_LSDA) ? " LSDA" : "", \
  31. (value & ADMHC_PS_PPS) ? " PPS" : "", \
  32. (value & ADMHC_PS_PRS) ? " PRS" : "", \
  33. (value & ADMHC_PS_POCI) ? " POCI" : "", \
  34. (value & ADMHC_PS_PSS) ? " PSS" : "", \
  35. \
  36. (value & ADMHC_PS_PES) ? " PES" : "", \
  37. (value & ADMHC_PS_CCS) ? " CCS" : "" \
  38. );
  39. #define dbg_port_write(hc, label, num, value) \
  40. admhc_dbg(hc, \
  41. "%s port%d " \
  42. "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
  43. label, num, value, \
  44. (value & ADMHC_PS_PRSC) ? " PRSC" : "", \
  45. (value & ADMHC_PS_OCIC) ? " OCIC" : "", \
  46. (value & ADMHC_PS_PSSC) ? " PSSC" : "", \
  47. (value & ADMHC_PS_PESC) ? " PESC" : "", \
  48. (value & ADMHC_PS_CSC) ? " CSC" : "", \
  49. \
  50. (value & ADMHC_PS_CPP) ? " CPP" : "", \
  51. (value & ADMHC_PS_SPP) ? " SPP" : "", \
  52. (value & ADMHC_PS_SPR) ? " SPR" : "", \
  53. (value & ADMHC_PS_CPS) ? " CPS" : "", \
  54. (value & ADMHC_PS_SPS) ? " SPS" : "", \
  55. \
  56. (value & ADMHC_PS_SPE) ? " SPE" : "", \
  57. (value & ADMHC_PS_CPE) ? " CPE" : "" \
  58. );
  59. /*-------------------------------------------------------------------------*/
  60. /* build "status change" packet (one or two bytes) from HC registers */
  61. static int
  62. admhc_hub_status_data(struct usb_hcd *hcd, char *buf)
  63. {
  64. struct admhcd *ahcd = hcd_to_admhcd(hcd);
  65. int i, changed = 0, length = 1;
  66. int any_connected = 0;
  67. unsigned long flags;
  68. u32 status;
  69. spin_lock_irqsave(&ahcd->lock, flags);
  70. if (!HCD_HW_ACCESSIBLE(hcd))
  71. goto done;
  72. /* init status */
  73. status = admhc_read_rhdesc(ahcd);
  74. if (status & (ADMHC_RH_LPSC | ADMHC_RH_OCIC))
  75. buf[0] = changed = 1;
  76. else
  77. buf[0] = 0;
  78. if (ahcd->num_ports > 7) {
  79. buf[1] = 0;
  80. length++;
  81. }
  82. /* look at each port */
  83. for (i = 0; i < ahcd->num_ports; i++) {
  84. status = admhc_read_portstatus(ahcd, i);
  85. /* can't autostop if ports are connected */
  86. any_connected |= (status & ADMHC_PS_CCS);
  87. if (status & (ADMHC_PS_CSC | ADMHC_PS_PESC | ADMHC_PS_PSSC
  88. | ADMHC_PS_OCIC | ADMHC_PS_PRSC)) {
  89. changed = 1;
  90. if (i < 7)
  91. buf[0] |= 1 << (i + 1);
  92. else
  93. buf[1] |= 1 << (i - 7);
  94. }
  95. }
  96. if (admhc_root_hub_state_changes(ahcd, changed,
  97. any_connected))
  98. set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  99. else
  100. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  101. done:
  102. spin_unlock_irqrestore(&ahcd->lock, flags);
  103. return changed ? length : 0;
  104. }
  105. /*-------------------------------------------------------------------------*/
  106. static int admhc_get_hub_descriptor(struct admhcd *ahcd, char *buf)
  107. {
  108. struct usb_hub_descriptor *desc = (struct usb_hub_descriptor *)buf;
  109. u32 rh = admhc_read_rhdesc(ahcd);
  110. u16 temp;
  111. desc->bDescriptorType = USB_DT_HUB; /* Hub-descriptor */
  112. desc->bPwrOn2PwrGood = ADMHC_POTPGT/2; /* use default value */
  113. desc->bHubContrCurrent = 0x00; /* 0mA */
  114. desc->bNbrPorts = ahcd->num_ports;
  115. temp = 1 + (ahcd->num_ports / 8);
  116. desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * temp;
  117. /* FIXME */
  118. temp = 0;
  119. if (rh & ADMHC_RH_NPS) /* no power switching? */
  120. temp |= 0x0002;
  121. if (rh & ADMHC_RH_PSM) /* per-port power switching? */
  122. temp |= 0x0001;
  123. if (rh & ADMHC_RH_NOCP) /* no overcurrent reporting? */
  124. temp |= 0x0010;
  125. else if (rh & ADMHC_RH_OCPM) /* per-port overcurrent reporting? */
  126. temp |= 0x0008;
  127. desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ahcd, temp);
  128. /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  129. desc->u.hs.DeviceRemovable[0] = 0;
  130. desc->u.hs.DeviceRemovable[0] = ~0;
  131. return 0;
  132. }
  133. static int admhc_get_hub_status(struct admhcd *ahcd, char *buf)
  134. {
  135. struct usb_hub_status *hs = (struct usb_hub_status *)buf;
  136. u32 t = admhc_read_rhdesc(ahcd);
  137. u16 status, change;
  138. status = 0;
  139. status |= (t & ADMHC_RH_LPS) ? HUB_STATUS_LOCAL_POWER : 0;
  140. status |= (t & ADMHC_RH_OCI) ? HUB_STATUS_OVERCURRENT : 0;
  141. change = 0;
  142. change |= (t & ADMHC_RH_LPSC) ? HUB_CHANGE_LOCAL_POWER : 0;
  143. change |= (t & ADMHC_RH_OCIC) ? HUB_CHANGE_OVERCURRENT : 0;
  144. hs->wHubStatus = (__force __u16)cpu_to_hc16(ahcd, status);
  145. hs->wHubChange = (__force __u16)cpu_to_hc16(ahcd, change);
  146. return 0;
  147. }
  148. static int admhc_get_port_status(struct admhcd *ahcd, unsigned port, char *buf)
  149. {
  150. struct usb_port_status *ps = (struct usb_port_status *)buf;
  151. u32 t = admhc_read_portstatus(ahcd, port);
  152. u16 status, change;
  153. status = 0;
  154. status |= (t & ADMHC_PS_CCS) ? USB_PORT_STAT_CONNECTION : 0;
  155. status |= (t & ADMHC_PS_PES) ? USB_PORT_STAT_ENABLE : 0;
  156. status |= (t & ADMHC_PS_PSS) ? USB_PORT_STAT_SUSPEND : 0;
  157. status |= (t & ADMHC_PS_POCI) ? USB_PORT_STAT_OVERCURRENT : 0;
  158. status |= (t & ADMHC_PS_PRS) ? USB_PORT_STAT_RESET : 0;
  159. status |= (t & ADMHC_PS_PPS) ? USB_PORT_STAT_POWER : 0;
  160. status |= (t & ADMHC_PS_LSDA) ? USB_PORT_STAT_LOW_SPEED : 0;
  161. change = 0;
  162. change |= (t & ADMHC_PS_CSC) ? USB_PORT_STAT_C_CONNECTION : 0;
  163. change |= (t & ADMHC_PS_PESC) ? USB_PORT_STAT_C_ENABLE : 0;
  164. change |= (t & ADMHC_PS_PSSC) ? USB_PORT_STAT_C_SUSPEND : 0;
  165. change |= (t & ADMHC_PS_OCIC) ? USB_PORT_STAT_C_OVERCURRENT : 0;
  166. change |= (t & ADMHC_PS_PRSC) ? USB_PORT_STAT_C_RESET : 0;
  167. ps->wPortStatus = (__force __u16)cpu_to_hc16(ahcd, status);
  168. ps->wPortChange = (__force __u16)cpu_to_hc16(ahcd, change);
  169. return 0;
  170. }
  171. /*-------------------------------------------------------------------------*/
  172. #ifdef CONFIG_USB_OTG
  173. static int admhc_start_port_reset(struct usb_hcd *hcd, unsigned port)
  174. {
  175. struct admhcd *ahcd = hcd_to_admhcd(hcd);
  176. u32 status;
  177. if (!port)
  178. return -EINVAL;
  179. port--;
  180. /* start port reset before HNP protocol times out */
  181. status = admhc_read_portstatus(ahcd, port);
  182. if (!(status & ADMHC_PS_CCS))
  183. return -ENODEV;
  184. /* khubd will finish the reset later */
  185. admhc_write_portstatus(ahcd, port, ADMHC_PS_PRS);
  186. return 0;
  187. }
  188. static void start_hnp(struct admhcd *ahcd);
  189. #else
  190. #define admhc_start_port_reset NULL
  191. #endif
  192. /*-------------------------------------------------------------------------*/
  193. /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
  194. * not necessarily continuous ... to guard against resume signaling.
  195. * The short timeout is safe for non-root hubs, and is backward-compatible
  196. * with earlier Linux hosts.
  197. */
  198. #ifdef CONFIG_USB_SUSPEND
  199. #define PORT_RESET_MSEC 50
  200. #else
  201. #define PORT_RESET_MSEC 10
  202. #endif
  203. /* this timer value might be vendor-specific ... */
  204. #define PORT_RESET_HW_MSEC 10
  205. /* wrap-aware logic morphed from <linux/jiffies.h> */
  206. #define tick_before(t1, t2) ((s16)(((s16)(t1)) - ((s16)(t2))) < 0)
  207. /* called from some task, normally khubd */
  208. static inline int admhc_port_reset(struct admhcd *ahcd, unsigned port)
  209. {
  210. u32 t;
  211. admhc_vdbg(ahcd, "reset port%d\n", port);
  212. t = admhc_read_portstatus(ahcd, port);
  213. if (!(t & ADMHC_PS_CCS))
  214. return -ENODEV;
  215. admhc_write_portstatus(ahcd, port, ADMHC_PS_SPR);
  216. mdelay(10);
  217. admhc_write_portstatus(ahcd, port, (ADMHC_PS_SPE | ADMHC_PS_CSC));
  218. mdelay(100);
  219. return 0;
  220. }
  221. static inline int admhc_port_enable(struct admhcd *ahcd, unsigned port)
  222. {
  223. u32 t;
  224. admhc_vdbg(ahcd, "enable port%d\n", port);
  225. t = admhc_read_portstatus(ahcd, port);
  226. if (!(t & ADMHC_PS_CCS))
  227. return -ENODEV;
  228. admhc_write_portstatus(ahcd, port, ADMHC_PS_SPE);
  229. return 0;
  230. }
  231. static inline int admhc_port_disable(struct admhcd *ahcd, unsigned port)
  232. {
  233. u32 t;
  234. admhc_vdbg(ahcd, "disable port%d\n", port);
  235. t = admhc_read_portstatus(ahcd, port);
  236. if (!(t & ADMHC_PS_CCS))
  237. return -ENODEV;
  238. admhc_write_portstatus(ahcd, port, ADMHC_PS_CPE);
  239. return 0;
  240. }
  241. static inline int admhc_port_write(struct admhcd *ahcd, unsigned port,
  242. u32 val)
  243. {
  244. #ifdef ADMHC_VERBOSE_DEBUG
  245. dbg_port_write(ahcd, "write", port, val);
  246. #endif
  247. admhc_write_portstatus(ahcd, port, val);
  248. return 0;
  249. }
  250. static int admhc_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  251. u16 wIndex, char *buf, u16 wLength)
  252. {
  253. struct admhcd *ahcd = hcd_to_admhcd(hcd);
  254. int ports = ahcd->num_ports;
  255. int ret = 0;
  256. if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
  257. return -ESHUTDOWN;
  258. switch (typeReq) {
  259. case ClearHubFeature:
  260. switch (wValue) {
  261. case C_HUB_OVER_CURRENT:
  262. #if 0 /* FIXME */
  263. admhc_writel(ahcd, ADMHC_RH_OCIC,
  264. &ahcd->regs->roothub.status);
  265. #endif
  266. case C_HUB_LOCAL_POWER:
  267. break;
  268. default:
  269. goto error;
  270. }
  271. break;
  272. case ClearPortFeature:
  273. if (!wIndex || wIndex > ports)
  274. goto error;
  275. wIndex--;
  276. switch (wValue) {
  277. case USB_PORT_FEAT_ENABLE:
  278. ret = admhc_port_disable(ahcd, wIndex);
  279. break;
  280. case USB_PORT_FEAT_SUSPEND:
  281. ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_CPS);
  282. break;
  283. case USB_PORT_FEAT_POWER:
  284. ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_CPP);
  285. break;
  286. case USB_PORT_FEAT_C_CONNECTION:
  287. ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_CSC);
  288. break;
  289. case USB_PORT_FEAT_C_ENABLE:
  290. ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_PESC);
  291. break;
  292. case USB_PORT_FEAT_C_SUSPEND:
  293. ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_PSSC);
  294. break;
  295. case USB_PORT_FEAT_C_OVER_CURRENT:
  296. ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_OCIC);
  297. break;
  298. case USB_PORT_FEAT_C_RESET:
  299. ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_PRSC);
  300. break;
  301. default:
  302. goto error;
  303. }
  304. break;
  305. case GetHubDescriptor:
  306. ret = admhc_get_hub_descriptor(ahcd, buf);
  307. break;
  308. case GetHubStatus:
  309. ret = admhc_get_hub_status(ahcd, buf);
  310. break;
  311. case GetPortStatus:
  312. if (!wIndex || wIndex > ports)
  313. goto error;
  314. wIndex--;
  315. ret = admhc_get_port_status(ahcd, wIndex, buf);
  316. break;
  317. case SetHubFeature:
  318. switch (wValue) {
  319. case C_HUB_OVER_CURRENT:
  320. /* FIXME: this can be cleared, yes? */
  321. case C_HUB_LOCAL_POWER:
  322. break;
  323. default:
  324. goto error;
  325. }
  326. break;
  327. case SetPortFeature:
  328. if (!wIndex || wIndex > ports)
  329. goto error;
  330. wIndex--;
  331. switch (wValue) {
  332. case USB_PORT_FEAT_ENABLE:
  333. ret = admhc_port_enable(ahcd, wIndex);
  334. break;
  335. case USB_PORT_FEAT_RESET:
  336. ret = admhc_port_reset(ahcd, wIndex);
  337. break;
  338. case USB_PORT_FEAT_SUSPEND:
  339. #ifdef CONFIG_USB_OTG
  340. if (hcd->self.otg_port == (wIndex + 1)
  341. && hcd->self.b_hnp_enable)
  342. start_hnp(ahcd);
  343. else
  344. #endif
  345. ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_SPS);
  346. break;
  347. case USB_PORT_FEAT_POWER:
  348. ret = admhc_port_write(ahcd, wIndex, ADMHC_PS_SPP);
  349. break;
  350. default:
  351. goto error;
  352. }
  353. break;
  354. default:
  355. error:
  356. /* "protocol stall" on error */
  357. ret = -EPIPE;
  358. }
  359. return ret;
  360. }