xhci-mtk.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. #include <xhci.h>
  2. #include <linux/kernel.h>
  3. #include <linux/slab.h>
  4. #include <linux/delay.h>
  5. #include <linux/uaccess.h>
  6. #include <linux/dma-mapping.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/wakelock.h>
  9. #include <linux/io.h>
  10. #include <linux/irq.h>
  11. #include <linux/switch.h>
  12. #include <linux/module.h>
  13. #ifdef CONFIG_SSUSB_PROJECT_PHY
  14. #include <mu3phy/mtk-phy-asic.h>
  15. #endif
  16. #include <linux/gpio.h>
  17. #include "mt_battery_common.h"
  18. #include "mu3d_hal_hw.h"
  19. #include "xhci-mtk.h"
  20. #define IDPIN_IN 0
  21. #define IDPIN_OUT 1
  22. #define USB2_PORT 2
  23. #define USB3_PORT 3
  24. #define OTG_IDDIG_DEBOUNCE 50
  25. #define U3_UX_EXIT_LFPS_TIMING_PAR 0xa0
  26. #define U3_REF_CK_PAR 0xb0
  27. #define U3_RX_UX_EXIT_LFPS_REF_OFFSET 8
  28. #define U3_RX_UX_EXIT_LFPS_REF (3 << (U3_RX_UX_EXIT_LFPS_REF_OFFSET))
  29. #define U3_REF_CK_VAL 10
  30. #define U3_TIMING_PULSE_CTRL 0xb4
  31. #define MTK_CNT_1US_VALUE 63 /* 62.5MHz:63, 70MHz:70, 80MHz:80, 100MHz:100, 125MHz:125 */
  32. #define USB20_TIMING_PARAMETER 0x40
  33. #define MTK_TIME_VALUE_1US 63 /* 62.5MHz:63, 80MHz:80, 100MHz:100, 125MHz:125 */
  34. #define LINK_PM_TIMER 0x8
  35. #define MTK_PM_LC_TIMEOUT_VALUE 3
  36. static struct xhci_hcd *mtk_xhci;
  37. static struct switch_dev *g_otg_state;
  38. /* avoid compile error if not support charger ic */
  39. void __weak tbl_charger_otg_vbus(int mode)
  40. {
  41. mu3d_dbg(K_ERR, "%s(): dummy func, maybe not what you need, check it!!\n", __func__);
  42. }
  43. static inline struct ssusb_mtk *otg_switch_to_ssusb(struct otg_switch_mtk *otg_sx)
  44. {
  45. return container_of(otg_sx, struct ssusb_mtk, otg_switch);
  46. }
  47. static bool wait_for_value(void __iomem *base, int addr, int msk, int value, int ms_intvl,
  48. int count)
  49. {
  50. int i;
  51. for (i = 0; i < count; i++) {
  52. if ((mu3d_readl(base, addr) & msk) == value)
  53. return true;
  54. mdelay(ms_intvl);
  55. }
  56. return false;
  57. }
  58. static void mtk_chk_usb_ip_ck_sts(struct ssusb_mtk *ssusb)
  59. {
  60. int ret;
  61. int num_u3_port = ssusb->u3_ports;
  62. int num_u2_port = ssusb->u2_ports;
  63. void __iomem *sif_base = ssusb->sif_base;
  64. ret =
  65. wait_for_value(sif_base, U3D_SSUSB_IP_PW_STS1, SSUSB_SYS125_RST_B_STS,
  66. SSUSB_SYS125_RST_B_STS, 1, 10);
  67. if (ret == false)
  68. mu3d_dbg(K_WARNIN, "sys125_ck is still active!!!\n");
  69. /* do not check when SSUSB_U2_PORT_PDN = 1, because U2 port stays in reset state */
  70. if (num_u2_port && !(mu3d_readl(sif_base, SSUSB_U2_CTRL(0)) & SSUSB_U2_PORT_PDN)) {
  71. ret =
  72. wait_for_value(sif_base, U3D_SSUSB_IP_PW_STS2, SSUSB_U2_MAC_SYS_RST_B_STS,
  73. SSUSB_U2_MAC_SYS_RST_B_STS, 1, 10);
  74. if (ret == false)
  75. mu3d_dbg(K_WARNIN, "mac2_sys_ck is still active!!!\n");
  76. }
  77. /* do not check when SSUSB_U3_PORT_PDN = 1, because U3 port stays in reset state */
  78. if (num_u3_port && !(mu3d_readl(sif_base, SSUSB_U3_CTRL(0)) & SSUSB_U3_PORT_PDN)) {
  79. ret =
  80. wait_for_value(sif_base, U3D_SSUSB_IP_PW_STS1, SSUSB_U3_MAC_RST_B_STS,
  81. SSUSB_U3_MAC_RST_B_STS, 1, 10);
  82. if (ret == false)
  83. mu3d_dbg(K_WARNIN, "mac3_mac_ck is still active!!!\n");
  84. }
  85. }
  86. /*
  87. * if there is not iddig-pin but also use dual-mode, should set FORCE_IDDIG/RG_IDDIG,
  88. * such as type A port, to emulate iddig detection.
  89. * in order to support both with-iddig-pin and without-iddig-pin cases,
  90. * make use of it anyway.
  91. * when OTG cable plug in, iddig is low level, otherwise is high level
  92. */
  93. void ssusb_otg_iddig_en(struct ssusb_mtk *ssusb)
  94. {
  95. mu3d_setmsk(ssusb->sif_base, U3D_U2PHYDTM1, FORCE_IDDIG);
  96. /*port0 is otg */
  97. mu3d_setmsk(ssusb->sif_base, SSUSB_U2_CTRL(0), SSUSB_U2_PORT_OTG_SEL);
  98. }
  99. /* tell mac switch to host mode */
  100. void ssusb_otg_plug_in(struct ssusb_mtk *ssusb)
  101. {
  102. mu3d_clrmsk(ssusb->sif_base, U3D_U2PHYDTM1, RG_IDDIG);
  103. }
  104. /* tell mac switch to device mode */
  105. void ssusb_otg_plug_out(struct ssusb_mtk *ssusb)
  106. {
  107. mu3d_setmsk(ssusb->sif_base, U3D_U2PHYDTM1, RG_IDDIG);
  108. }
  109. static int check_port_param(struct ssusb_mtk *ssusb, int version, int index)
  110. {
  111. int ret = -EINVAL;
  112. int num_u3_port = ssusb->u3_ports;
  113. int num_u2_port = ssusb->u2_ports;
  114. mu3d_dbg(K_INFO, "%s u2-ports:%d, u3-ports:%d\n", __func__, num_u2_port, num_u3_port);
  115. if (USB2_PORT == version) {
  116. if (num_u2_port && (index < num_u2_port))
  117. ret = 0;
  118. } else if (USB3_PORT == version) {
  119. if (num_u3_port && (index < num_u3_port))
  120. ret = 0;
  121. }
  122. if (ret)
  123. mu3d_dbg(K_WARNIN, "%s u2-ports:%d, u3-ports:%d (param: u%d-port-%d)\n",
  124. __func__, num_u2_port, num_u3_port, version, index);
  125. return ret;
  126. }
  127. /**
  128. * @version: only USB2_PORT(usb2's) & USB3_PORT(usb3's);
  129. * @index: virtual port index of usb3/usb2's, valid values : 0, 1, max-ports - 1.
  130. * set 1 to PORT_POWER of PORT_STATUS register of index-port (usb-version)
  131. */
  132. #define MTK_XHCI_PORT_RO ((1<<0) | (1<<3) | (0xf<<10) | (1<<30))
  133. /*
  134. * These bits are RW; writing a 0 clears the bit, writing a 1 sets the bit:
  135. * bits 5:8, 9, 14:15, 25:27
  136. * link state, port power, port indicator state, "wake on" enable state
  137. */
  138. #define MTK_XHCI_PORT_RWS ((0xf<<5) | (1<<9) | (0x3<<14) | (0x7<<25))
  139. static u32 mtk_xhci_port_state_to_neutral(u32 state)
  140. {
  141. /* Save read-only status and port state */
  142. return (state & MTK_XHCI_PORT_RO) | (state & MTK_XHCI_PORT_RWS);
  143. }
  144. static int xhci_port_power_on(struct ssusb_mtk *ssusb, int version, int index)
  145. {
  146. u32 temp;
  147. u32 __iomem *addr = NULL;
  148. struct xhci_hcd *xhci = mtk_xhci;
  149. mu3d_dbg(K_DEBUG, "%s in(xhci:%p)\n", __func__, xhci);
  150. if (check_port_param(ssusb, version, index))
  151. return -ENODEV;
  152. if (USB2_PORT == version)
  153. addr = xhci->usb2_ports[index];
  154. else
  155. addr = xhci->usb3_ports[index];
  156. temp = readl(addr);
  157. temp = mtk_xhci_port_state_to_neutral(temp);
  158. temp |= PORT_POWER;
  159. writel(temp, addr);
  160. while (!(readl(addr) & PORT_POWER))
  161. ;
  162. mu3d_dbg(K_WARNIN, "%s (u%dport:%d), port status:0x%x\n", __func__,
  163. version, index, readl(addr));
  164. return 0;
  165. }
  166. /**
  167. * @version: only USB2_PORT(usb2's) & USB3_PORT(usb3's);
  168. * @index: virtual port index of usb3/usb2's, valid values : 0, 1, max-ports - 1.
  169. * set 0 to PORT_POWER of PORT_STATUS register of index-port (usb-version)
  170. */
  171. static int xhci_port_power_off(struct ssusb_mtk *ssusb, int version, int index)
  172. {
  173. u32 temp;
  174. u32 __iomem *addr = NULL;
  175. struct xhci_hcd *xhci = mtk_xhci;
  176. mu3d_dbg(K_DEBUG, "%s in(xhci:%p)\n", __func__, xhci);
  177. if (check_port_param(ssusb, version, index))
  178. return -ENODEV;
  179. if (USB2_PORT == version)
  180. addr = xhci->usb2_ports[index];
  181. else
  182. addr = xhci->usb3_ports[index];
  183. temp = readl(addr);
  184. temp = mtk_xhci_port_state_to_neutral(temp);
  185. temp &= ~PORT_POWER;
  186. writel(temp, addr);
  187. while (readl(addr) & PORT_POWER)
  188. ;
  189. mu3d_dbg(K_WARNIN, "%s (u%dport:%d), port status:0x%x\n", __func__,
  190. version, index, readl(addr));
  191. return 0;
  192. }
  193. /* only operator ports will be used later */
  194. static void host_ports_enable(struct ssusb_mtk *ssusb)
  195. {
  196. int i;
  197. u32 temp;
  198. int num_u3_port = ssusb->u3_ports;
  199. int num_u2_port = ssusb->u2_ports;
  200. void __iomem *sif_base = ssusb->sif_base;
  201. mu3d_dbg(K_DEBUG, "%s in\n", __func__);
  202. mu3d_dbg(K_WARNIN, "%s u2p:%d, u3p:%d\n", __func__, num_u2_port, num_u3_port);
  203. /* disable ip host power down bit --> power on host ip */
  204. mu3d_clrmsk(sif_base, U3D_SSUSB_IP_PW_CTRL1, SSUSB_IP_HOST_PDN);
  205. /* disable all u3 port power down and disable bits --> power on and enable all u3 ports */
  206. for (i = 0; i < num_u3_port; i++) {
  207. temp = mu3d_readl(sif_base, SSUSB_U3_CTRL(i));
  208. temp &= ~(SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS);
  209. temp |= SSUSB_U3_PORT_HOST_SEL;
  210. mu3d_writel(sif_base, SSUSB_U3_CTRL(i), temp);
  211. }
  212. /* disable all u2 port power down and disable bits --> power on and enable all u2 ports */
  213. for (i = 0; i < num_u2_port; i++) {
  214. temp = mu3d_readl(sif_base, SSUSB_U2_CTRL(i));
  215. temp &= ~(SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS);
  216. temp |= SSUSB_U2_PORT_HOST_SEL;
  217. mu3d_writel(sif_base, SSUSB_U2_CTRL(i), temp);
  218. }
  219. ssusb_otg_plug_in(ssusb);
  220. /* msleep(100); */
  221. mtk_chk_usb_ip_ck_sts(ssusb);
  222. mu3d_dbg(K_DEBUG, "%s out\n", __func__);
  223. }
  224. static void host_ports_disable(struct ssusb_mtk *ssusb)
  225. {
  226. int i;
  227. u32 temp;
  228. int num_u3_port = ssusb->u3_ports;
  229. int num_u2_port = ssusb->u2_ports;
  230. void __iomem *sif_base = ssusb->sif_base;
  231. mu3d_dbg(K_DEBUG, "%s in\n", __func__);
  232. mu3d_dbg(K_WARNIN, "%s u2p:%d, u3p:%d\n", __func__, num_u2_port, num_u3_port);
  233. ssusb_otg_plug_out(ssusb);
  234. /* enable all u3 port power down and disable bits --> power down and disable all u3 ports */
  235. for (i = 0; i < num_u3_port; i++) {
  236. temp = mu3d_readl(sif_base, SSUSB_U3_CTRL(i));
  237. temp |= SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS;
  238. mu3d_writel(sif_base, SSUSB_U3_CTRL(i), temp);
  239. }
  240. /* enable all u2 port power down and disable bits --> power down and disable all u2 ports */
  241. for (i = 0; i < num_u2_port; i++) {
  242. temp = mu3d_readl(sif_base, SSUSB_U2_CTRL(i));
  243. temp |= SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS;
  244. mu3d_writel(sif_base, SSUSB_U2_CTRL(i), temp);
  245. }
  246. /* enable ip host power down bit --> power down host ip */
  247. mu3d_setmsk(sif_base, U3D_SSUSB_IP_PW_CTRL1, SSUSB_IP_HOST_PDN);
  248. mu3d_dbg(K_DEBUG, "%s out\n", __func__);
  249. }
  250. static void mtk_host_wakelock_lock(struct wake_lock *wakelock)
  251. {
  252. if (!wake_lock_active(wakelock))
  253. wake_lock(wakelock);
  254. mu3d_dbg(K_DEBUG, "done\n");
  255. }
  256. static void mtk_host_wakelock_unlock(struct wake_lock *wakelock)
  257. {
  258. if (wake_lock_active(wakelock))
  259. wake_unlock(wakelock);
  260. mu3d_dbg(K_DEBUG, "done\n");
  261. }
  262. static int ssusb_set_vbus(struct vbus_ctrl_info *vbus, int is_on)
  263. {
  264. if (SSUSB_VBUS_GPIO == vbus->vbus_mode) {
  265. mu3d_dbg(K_DEBUG, "%s() set vbus(gpio:%d).\n", __func__, vbus->vbus_gpio_num);
  266. gpio_set_value(vbus->vbus_gpio_num, !!is_on);
  267. } else if (SSUSB_VBUS_CHARGER == vbus->vbus_mode) {
  268. mu3d_dbg(K_DEBUG, "%s() call charger driver api.\n", __func__);
  269. bat_charger_boost_enable(is_on);
  270. } else if (SSUSB_VBUS_DEF_ON == vbus->vbus_mode) {
  271. /* nothing to do, turn on by default */
  272. mu3d_dbg(K_DEBUG, "%s() vbus turn on by default.\n", __func__);
  273. } else {
  274. /* Maybe LDO, TODO... */
  275. mu3d_dbg(K_ERR, "%s() error : no such case! check it...\n", __func__);
  276. }
  277. return 0;
  278. }
  279. static int ssusb_port_switch(struct ssusb_mtk *ssusb, bool tohost, int version, int index)
  280. {
  281. u32 temp;
  282. void __iomem *sif_base = ssusb->sif_base;
  283. mu3d_dbg(K_WARNIN, "%s in(tohost: %d, u%dport-%d)\n", __func__, tohost, version, index);
  284. if (check_port_param(ssusb, version, index))
  285. return -ENODEV;
  286. if (tohost)
  287. ssusb_otg_plug_in(ssusb);
  288. else
  289. ssusb_otg_plug_out(ssusb);
  290. if (USB2_PORT == version)
  291. mu3d_setmsk(sif_base, SSUSB_U2_CTRL(index), SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS);
  292. else
  293. mu3d_setmsk(sif_base, SSUSB_U3_CTRL(index), SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS);
  294. mtk_chk_usb_ip_ck_sts(ssusb);
  295. mu3d_dbg(K_DEBUG, "%s U3D_SSUSB_OTG_STS: %x\n", __func__,
  296. mu3d_readl(sif_base, U3D_SSUSB_OTG_STS));
  297. /* disable u2 port power down and disable bits --> power on and enable all u2 ports */
  298. if (USB2_PORT == version) {
  299. temp = mu3d_readl(sif_base, SSUSB_U2_CTRL(index));
  300. temp = temp & (~SSUSB_U2_PORT_PDN);
  301. temp = temp & (~SSUSB_U2_PORT_DIS);
  302. temp = (tohost) ? temp | SSUSB_U2_PORT_HOST_SEL : temp & (~SSUSB_U2_PORT_HOST_SEL);
  303. mu3d_writel(sif_base, SSUSB_U2_CTRL(index), temp);
  304. } else {
  305. /* disable u3 port power down and disable bits --> power on and enable all u3 ports */
  306. temp = mu3d_readl(sif_base, SSUSB_U3_CTRL(index));
  307. temp = temp & (~SSUSB_U3_PORT_PDN);
  308. temp = temp & (~SSUSB_U3_PORT_DIS);
  309. temp = (tohost) ? temp | SSUSB_U3_PORT_HOST_SEL : temp & (~SSUSB_U3_PORT_HOST_SEL);
  310. mu3d_writel(sif_base, SSUSB_U3_CTRL(index), temp);
  311. }
  312. mtk_chk_usb_ip_ck_sts(ssusb);
  313. mu3d_dbg(K_DEBUG, "%s U3D_SSUSB_OTG_STS: %x\n", __func__,
  314. mu3d_readl(sif_base, U3D_SSUSB_OTG_STS));
  315. mu3d_dbg(K_DEBUG, "%s out\n", __func__);
  316. return 0;
  317. }
  318. static void switch_port_to_host(struct ssusb_mtk *ssusb)
  319. {
  320. mu3d_dbg(K_WARNIN, "%s\n", __func__);
  321. /* assert port power bit to drive drv_vbus */
  322. if (ssusb->otg_switch.port0_u2) {
  323. ssusb_port_switch(ssusb, true, USB2_PORT, 0);
  324. xhci_port_power_on(ssusb, USB2_PORT, 0);
  325. }
  326. if (ssusb->otg_switch.port0_u3) {
  327. ssusb_port_switch(ssusb, true, USB3_PORT, 0);
  328. xhci_port_power_on(ssusb, USB3_PORT, 0);
  329. }
  330. }
  331. static void switch_port_to_device(struct ssusb_mtk *ssusb, bool skip_u3dev)
  332. {
  333. mu3d_dbg(K_WARNIN, "%s mtk_xhci:%p\n", __func__, mtk_xhci);
  334. /* deassert port power bit to drop off vbus */
  335. if (ssusb->otg_switch.port0_u2) {
  336. xhci_port_power_off(ssusb, USB2_PORT, 0);
  337. ssusb_port_switch(ssusb, false, USB2_PORT, 0);
  338. }
  339. if (ssusb->otg_switch.port0_u3) {
  340. xhci_port_power_off(ssusb, USB3_PORT, 0);
  341. ssusb_port_switch(ssusb, false, USB3_PORT, 0);
  342. }
  343. }
  344. static inline void set_iddig_out_detect(struct otg_switch_mtk *otg_switch)
  345. {
  346. irq_set_irq_type(otg_switch->iddig_eint_num, IRQF_TRIGGER_HIGH);
  347. }
  348. static inline void set_iddig_in_detect(struct otg_switch_mtk *otg_switch)
  349. {
  350. irq_set_irq_type(otg_switch->iddig_eint_num, IRQF_TRIGGER_LOW);
  351. }
  352. /*
  353. * it is used by the platform which has more than one usb ports
  354. * and port0 supports OTG, other ports works as host only.
  355. * in the case, should ensure that port0's OTG switch can't affect
  356. * other host ports.
  357. */
  358. static void ssusb_mode_switch(struct work_struct *work)
  359. {
  360. struct delayed_work *dwork = to_delayed_work(work);
  361. struct otg_switch_mtk *otg_switch =
  362. container_of(dwork, struct otg_switch_mtk, switch_dwork);
  363. struct ssusb_mtk *ssusb = otg_switch_to_ssusb(otg_switch);
  364. bool cur_id_state = otg_switch->next_idpin_state;
  365. if (cur_id_state == IDPIN_IN) {
  366. mu3d_dbg(K_DEBUG, "%s to host\n", __func__);
  367. /* open port power and switch resource to host */
  368. switch_port_to_host(ssusb);
  369. switch_set_state(&otg_switch->otg_state, 1);
  370. ssusb_set_vbus(&otg_switch->p0_vbus, 1);
  371. mtk_host_wakelock_lock(&otg_switch->xhci_wakelock);
  372. /* expect next isr is for id-pin out action */
  373. otg_switch->next_idpin_state = IDPIN_OUT;
  374. /* make id pin to detect the plug-out */
  375. set_iddig_out_detect(otg_switch);
  376. } else { /* IDPIN_OUT */
  377. mu3d_dbg(K_DEBUG, "%s to device\n", __func__);
  378. ssusb_set_vbus(&otg_switch->p0_vbus, 0);
  379. /* switch_port_to_device(false); */
  380. switch_port_to_device(ssusb, true);
  381. switch_set_state(&otg_switch->otg_state, 0);
  382. mtk_host_wakelock_unlock(&otg_switch->xhci_wakelock);
  383. /* expect next isr is for id-pin in action */
  384. /* mtk_id_nxt_state = IDPIN_IN; */
  385. otg_switch->next_idpin_state = IDPIN_IN;
  386. /* make id pin to detect the plug-in */
  387. set_iddig_in_detect(otg_switch);
  388. }
  389. enable_irq(otg_switch->iddig_eint_num);
  390. mu3d_dbg(K_WARNIN, "xhci switch resource to %s, switch(%d)\n",
  391. (cur_id_state == IDPIN_IN) ? "host" : "device",
  392. switch_get_state(&otg_switch->otg_state));
  393. }
  394. /*
  395. * the case is used by platform which only support one usb port,
  396. * especially such as tablet/phone which demand to close all
  397. * LDO, clocks and suspend phy when no cable plug in.
  398. */
  399. static void ssusb_mode_switch_lowpw(struct work_struct *work)
  400. {
  401. struct delayed_work *dwork = to_delayed_work(work);
  402. struct otg_switch_mtk *otg_switch =
  403. container_of(dwork, struct otg_switch_mtk, switch_dwork);
  404. struct ssusb_mtk *ssusb = otg_switch_to_ssusb(otg_switch);
  405. bool cur_id_state = otg_switch->next_idpin_state;
  406. if (cur_id_state == IDPIN_IN) {
  407. mu3d_dbg(K_DEBUG, "%s to host\n", __func__);
  408. ssusb_power_restore(ssusb);
  409. mtk_xhci_ip_init(ssusb);
  410. ssusb_host_init(ssusb);
  411. switch_set_state(&otg_switch->otg_state, 1);
  412. ssusb_set_vbus(&otg_switch->p0_vbus, 1);
  413. mtk_host_wakelock_lock(&otg_switch->xhci_wakelock);
  414. /* expect next isr is for id-pin out action */
  415. otg_switch->next_idpin_state = IDPIN_OUT;
  416. /* make id pin to detect the plug-out */
  417. set_iddig_out_detect(otg_switch);
  418. } else { /* IDPIN_OUT */
  419. mu3d_dbg(K_DEBUG, "%s to device\n", __func__);
  420. ssusb_set_vbus(&otg_switch->p0_vbus, 0);
  421. mtk_xhci_ip_exit(ssusb);
  422. ssusb_host_exit(ssusb);
  423. ssusb_power_save(ssusb);
  424. switch_set_state(&otg_switch->otg_state, 0);
  425. mtk_host_wakelock_unlock(&otg_switch->xhci_wakelock);
  426. /* expect next isr is for id-pin in action */
  427. otg_switch->next_idpin_state = IDPIN_IN;
  428. /* make id pin to detect the plug-in */
  429. set_iddig_in_detect(otg_switch);
  430. }
  431. enable_irq(otg_switch->iddig_eint_num);
  432. mu3d_dbg(K_WARNIN, "xhci switch resource to %s, switch(%d)\n",
  433. (cur_id_state == IDPIN_IN) ? "host" : "device",
  434. switch_get_state(&otg_switch->otg_state));
  435. }
  436. /*
  437. * use eint of idpin instead of sysfs interface to switch device and host mode
  438. */
  439. static void ssusb_mode_switch_idpin(struct work_struct *work)
  440. {
  441. struct delayed_work *dwork = to_delayed_work(work);
  442. struct otg_switch_mtk *otg_switch =
  443. container_of(dwork, struct otg_switch_mtk, switch_dwork);
  444. struct ssusb_mtk *ssusb = otg_switch_to_ssusb(otg_switch);
  445. struct musb *musb = ssusb->mu3di;
  446. bool cur_id_state = otg_switch->next_idpin_state;
  447. if (cur_id_state == IDPIN_IN) {
  448. musb_stop(musb);
  449. msleep(200); /* if not, there is something wrong with xhci's port status */
  450. ssusb_mode_switch_manual(ssusb, 1);
  451. /* expect next isr is for id-pin out action */
  452. otg_switch->next_idpin_state = IDPIN_OUT;
  453. /* make id pin to detect the plug-out */
  454. set_iddig_out_detect(otg_switch);
  455. } else {
  456. ssusb_mode_switch_manual(ssusb, 0);
  457. msleep(100);
  458. musb_start(musb);
  459. /* expect next isr is for id-pin in action */
  460. otg_switch->next_idpin_state = IDPIN_IN;
  461. /* make id pin to detect the plug-in */
  462. set_iddig_in_detect(otg_switch);
  463. }
  464. enable_irq(otg_switch->iddig_eint_num);
  465. }
  466. static irqreturn_t iddig_eint_isr(int irq, void *otg_sx)
  467. {
  468. struct otg_switch_mtk *otg_switch = (struct otg_switch_mtk *)otg_sx;
  469. mu3d_dbg(K_INFO, "%s : schedule to delayed work\n", __func__);
  470. disable_irq_nosync(otg_switch->iddig_eint_num);
  471. schedule_delayed_work(&otg_switch->switch_dwork, OTG_IDDIG_DEBOUNCE * HZ / 1000);
  472. return IRQ_HANDLED;
  473. }
  474. static int ssusb_iddig_eint_init(struct otg_switch_mtk *otg_sx)
  475. {
  476. unsigned int eint_num = otg_sx->iddig_eint_num;
  477. int ret;
  478. ret = request_irq(eint_num, iddig_eint_isr, IRQF_ONESHOT, "usb_iddig", otg_sx);
  479. if (ret) {
  480. mu3d_dbg(K_ERR, "fail to register otg eint iddig isr\n");
  481. return -EINVAL;
  482. }
  483. otg_sx->is_iddig_registered = 1;
  484. mu3d_dbg(K_INFO, "otg iddig(irq-num:%d) register done.\n", eint_num);
  485. return 0;
  486. }
  487. static void ssusb_iddig_eint_exit(struct otg_switch_mtk *otg_sx)
  488. {
  489. free_irq(otg_sx->iddig_eint_num, otg_sx);
  490. cancel_delayed_work(&otg_sx->switch_dwork);
  491. mu3d_dbg(K_INFO, "otg iddig unregister done.\n");
  492. }
  493. /*
  494. * only for box platform, which port0 is type A, but also need to support dual-mode,
  495. * and no iddig pin can be used;
  496. * you can switch to host through:
  497. * echo 1 > /sys/devices/bus.X/11270000.SSUSB/mode
  498. * when 'echo 0' to switch to device mode;
  499. */
  500. void ssusb_mode_switch_manual(struct ssusb_mtk *ssusb, int to_host)
  501. {
  502. struct otg_switch_mtk *otg_switch = &ssusb->otg_switch;
  503. struct vbus_ctrl_info *vbus = &otg_switch->p0_vbus;
  504. if (to_host) {
  505. mu3d_dbg(K_DEBUG, "%s to host\n", __func__);
  506. /* open port power and switch resource to host */
  507. switch_port_to_host(ssusb);
  508. /*mtk_host_wakelock_lock(&otg_switch->xhci_wakelock); */
  509. switch_set_state(&otg_switch->otg_state, 1);
  510. ssusb_set_vbus(vbus, 1);
  511. } else {
  512. mu3d_dbg(K_DEBUG, "%s to device\n", __func__);
  513. ssusb_set_vbus(vbus, 0);
  514. switch_port_to_device(ssusb, true);
  515. switch_set_state(&otg_switch->otg_state, 0);
  516. /*mtk_host_wakelock_unlock(&otg_switch->xhci_wakelock); */
  517. }
  518. mu3d_dbg(K_WARNIN, "xhci switch resource to %s, switch(%d)\n",
  519. (to_host) ? "host" : "device", switch_get_state(&otg_switch->otg_state));
  520. }
  521. static void iddig_eint_register_work(struct work_struct *work)
  522. {
  523. struct delayed_work *dwork = to_delayed_work(work);
  524. struct otg_switch_mtk *otg_switch =
  525. container_of(dwork, struct otg_switch_mtk, iddig_reg_dwork);
  526. mu3d_dbg(K_INFO, "%s\n", __func__);
  527. ssusb_iddig_eint_init(otg_switch);
  528. }
  529. int mtk_otg_switch_init(struct ssusb_mtk *ssusb)
  530. {
  531. struct otg_switch_mtk *otg_switch = &ssusb->otg_switch;
  532. otg_switch->otg_state.name = "otg_state";
  533. otg_switch->otg_state.index = 0;
  534. otg_switch->otg_state.state = 0;
  535. g_otg_state = &otg_switch->otg_state;
  536. if (switch_dev_register(&otg_switch->otg_state)) {
  537. mu3d_dbg(K_ERR, "switch_dev register fail\n");
  538. return -1;
  539. }
  540. /* should not call switch_set_state, only after switch_dev register; */
  541. if ((SSUSB_MODE_HOST == ssusb->drv_mode) || otg_switch->is_init_as_host)
  542. switch_set_state(&otg_switch->otg_state, 1);
  543. else
  544. switch_set_state(&otg_switch->otg_state, 0);
  545. wake_lock_init(&otg_switch->xhci_wakelock, WAKE_LOCK_SUSPEND, "xhci.wakelock");
  546. if (is_eint_used(otg_switch->otg_mode)) {
  547. otg_switch->next_idpin_state = IDPIN_IN;
  548. INIT_DELAYED_WORK(&otg_switch->iddig_reg_dwork, iddig_eint_register_work);
  549. if (SSUSB_OTG_IDPIN == otg_switch->otg_mode)
  550. INIT_DELAYED_WORK(&otg_switch->switch_dwork, ssusb_mode_switch_idpin);
  551. else if (ssusb->is_power_saving_mode)
  552. INIT_DELAYED_WORK(&otg_switch->switch_dwork, ssusb_mode_switch_lowpw);
  553. else
  554. INIT_DELAYED_WORK(&otg_switch->switch_dwork, ssusb_mode_switch);
  555. /* It is enough to delay 2sec for waiting for charger ic initialization */
  556. schedule_delayed_work(&otg_switch->iddig_reg_dwork, HZ * 2);
  557. }
  558. return 0;
  559. }
  560. void mtk_otg_switch_exit(struct ssusb_mtk *ssusb)
  561. {
  562. struct otg_switch_mtk *otg_switch = &ssusb->otg_switch;
  563. if (is_eint_used(otg_switch->otg_mode)) {
  564. cancel_delayed_work(&otg_switch->iddig_reg_dwork);
  565. if (otg_switch->is_iddig_registered)
  566. ssusb_iddig_eint_exit(otg_switch);
  567. }
  568. switch_dev_unregister(&otg_switch->otg_state);
  569. mu3d_dbg(K_INFO, "mtk-otg-switch exit done.\n");
  570. }
  571. #define WIFI_HW_RESET 104
  572. static void wifi_hw_reset(void)
  573. {
  574. int retval;
  575. retval = gpio_request(WIFI_HW_RESET, "wifi_hw_rst");
  576. if (retval) {
  577. mu3d_dbg(K_ERR, "fail to requeset wifi_hw_rst :%d\n", WIFI_HW_RESET);
  578. return;
  579. }
  580. gpio_direction_output(WIFI_HW_RESET, 1);
  581. gpio_set_value(WIFI_HW_RESET, 0);
  582. mu3d_dbg(K_ERR, "%s %d gpio:%d (val: %d)\n", __func__, __LINE__, WIFI_HW_RESET,
  583. gpio_get_value(WIFI_HW_RESET));
  584. mdelay(10);
  585. gpio_set_value(WIFI_HW_RESET, 1);
  586. mu3d_dbg(K_ERR, "%s %d gpio:%d (val: %d)\n", __func__, __LINE__, WIFI_HW_RESET,
  587. gpio_get_value(WIFI_HW_RESET));
  588. }
  589. void mtk_xhci_set(void *xhci)
  590. {
  591. mu3d_dbg(K_WARNIN, "mtk_xhci = 0x%p\n", xhci);
  592. mtk_xhci = (struct xhci_hcd *)xhci;
  593. wifi_hw_reset();
  594. }
  595. void __iomem *get_xhci_base(void)
  596. {
  597. if (mtk_xhci && mtk_xhci->main_hcd)
  598. return mtk_xhci->main_hcd->regs;
  599. return NULL;
  600. }
  601. bool mtk_is_host_mode(void)
  602. {
  603. return switch_get_state(g_otg_state) ? true : false;
  604. }
  605. int is_init_host_for_manual_otg(struct ssusb_mtk *ssusb)
  606. {
  607. struct otg_switch_mtk *otg_switch = &ssusb->otg_switch;
  608. return is_maual_otg(ssusb) && otg_switch->is_init_as_host;
  609. }
  610. /* only for box without charge or pmic to detect vbus */
  611. int is_ssusb_connected_to_pc(struct ssusb_mtk *ssusb)
  612. {
  613. u32 otg_status;
  614. int is_device;
  615. int vbus_valid = 0;
  616. is_device = !mtk_is_host_mode();
  617. if (is_device) {
  618. otg_status = mu3d_readl(ssusb->sif_base, U3D_SSUSB_OTG_STS);
  619. vbus_valid = otg_status & SSUSB_VBUS_VALID;
  620. }
  621. return is_device && vbus_valid;
  622. }
  623. static void mtk_xhci_ck_timer_init(struct ssusb_mtk *ssusb)
  624. {
  625. int num_u3_port = ssusb->u3_ports;
  626. void __iomem *mac_base = ssusb->mac_base;
  627. mu3d_dbg(K_INFO, "num-u3-port:%d\n", num_u3_port);
  628. if (num_u3_port) {
  629. /* set MAC reference clock speed */
  630. mu3d_writelmsk(mac_base, U3D_UX_EXIT_LFPS_TIMING_PARAMETER,
  631. RX_UX_EXIT_LFPS_REF, U3_RX_UX_EXIT_LFPS_REF);
  632. mu3d_writelmsk(mac_base, U3D_REF_CK_PARAMETER, REF_1000NS, U3_REF_CK_VAL);
  633. /* set SYS_CK */
  634. mu3d_writelmsk(mac_base, U3D_TIMING_PULSE_CTRL, CNT_1US_VALUE, MTK_CNT_1US_VALUE);
  635. }
  636. mu3d_writelmsk(mac_base, U3D_USB20_TIMING_PARAMETER, TIME_VALUE_1US, MTK_TIME_VALUE_1US);
  637. if (num_u3_port) {
  638. /* set LINK_PM_TIMER=3 */
  639. mu3d_writelmsk(mac_base, U3D_LINK_PM_TIMER, PM_LC_TIMEOUT_VALUE,
  640. MTK_PM_LC_TIMEOUT_VALUE);
  641. }
  642. }
  643. void mtk_xhci_ip_init(struct ssusb_mtk *ssusb)
  644. {
  645. struct vbus_ctrl_info *vbus;
  646. mu3d_dbg(K_DEBUG, "%s in\n", __func__);
  647. /* port1 only as host, so for port1 vbus, if not power on by default, enable it */
  648. if (ssusb->p1_exist) {
  649. vbus = &ssusb->p1_vbus;
  650. ssusb_set_vbus(vbus, 1);
  651. }
  652. /* port0 maybe support OTG, so for its vbus, turn it on only for special cases */
  653. if (SSUSB_MODE_DEVICE != ssusb->drv_mode) {
  654. vbus = &ssusb->otg_switch.p0_vbus;
  655. if ((SSUSB_MODE_HOST == ssusb->drv_mode) || ssusb->otg_switch.is_init_as_host) {
  656. /* power on by default */
  657. ssusb_set_vbus(vbus, 1);
  658. }
  659. }
  660. /*
  661. * power on host and power on/enable all ports
  662. * if support OTG, gadget driver will switch port0 to device mode
  663. */
  664. host_ports_enable(ssusb);
  665. mtk_xhci_ck_timer_init(ssusb);
  666. mu3d_dbg(K_DEBUG, "%s out\n", __func__);
  667. }
  668. void mtk_xhci_ip_exit(struct ssusb_mtk *ssusb)
  669. {
  670. struct vbus_ctrl_info *vbus;
  671. mu3d_dbg(K_DEBUG, "%s in\n", __func__);
  672. if (ssusb->p1_exist) {
  673. vbus = &ssusb->p1_vbus;
  674. ssusb_set_vbus(vbus, 0);
  675. }
  676. if ((SSUSB_MODE_HOST == ssusb->drv_mode) || ssusb->otg_switch.is_init_as_host) {
  677. vbus = &ssusb->otg_switch.p0_vbus;
  678. ssusb_set_vbus(vbus, 0);
  679. }
  680. host_ports_disable(ssusb);
  681. mu3d_dbg(K_DEBUG, "%s out\n", __func__);
  682. }
  683. int ssusb_host_init(struct ssusb_mtk *ssusb)
  684. {
  685. struct platform_device *xhci;
  686. struct ssusb_xhci_pdata pdata;
  687. int ret;
  688. xhci = platform_device_alloc("xhci-hcd", PLATFORM_DEVID_AUTO);
  689. if (!xhci) {
  690. dev_err(ssusb->dev, "couldn't allocate xHCI device\n");
  691. ret = -ENOMEM;
  692. goto err0;
  693. }
  694. xhci->dev.parent = ssusb->dev;
  695. xhci->dev.dma_mask = ssusb->dev->dma_mask;
  696. dma_set_coherent_mask(&xhci->dev, ssusb->dev->coherent_dma_mask);
  697. ssusb->xhci = xhci;
  698. ret = platform_device_add_resources(xhci, ssusb->xhci_rscs, SSUSB_XHCI_RSCS_NUM);
  699. if (ret) {
  700. dev_err(ssusb->dev, "couldn't add resources to xHCI device\n");
  701. goto err1;
  702. }
  703. memset(&pdata, 0, sizeof(pdata));
  704. pdata.need_str = !!(ssusb->str_mode == SSUSB_STR_DEEP);
  705. ret = platform_device_add_data(xhci, &pdata, sizeof(pdata));
  706. if (ret) {
  707. dev_err(ssusb->dev, "couldn't add platform data to xHCI device\n");
  708. goto err1;
  709. }
  710. ret = platform_device_add(xhci);
  711. if (ret) {
  712. dev_err(ssusb->dev, "failed to register xHCI device\n");
  713. goto err1;
  714. }
  715. dev_notice(ssusb->dev, "XHCI device register success...\n");
  716. return 0;
  717. err1:
  718. platform_device_put(xhci);
  719. err0:
  720. return ret;
  721. }
  722. void ssusb_host_exit(struct ssusb_mtk *ssusb)
  723. {
  724. platform_device_unregister(ssusb->xhci);
  725. }