usb20_phy.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. #ifdef CONFIG_MTK_CLKMGR
  2. #include <mach/mt_clkmgr.h>
  3. #else
  4. #include <linux/clk.h>
  5. #endif
  6. #include <linux/jiffies.h>
  7. #include <linux/delay.h>
  8. #include <asm/io.h>
  9. #include <linux/spinlock.h>
  10. #include "mtk_musb.h"
  11. #include "musb_core.h"
  12. #include "usb20.h"
  13. #define FRA (48)
  14. #define PARA (28)
  15. static void HQA_special(void)
  16. {
  17. u8 val;
  18. val = USBPHY_READ8(0x18);
  19. DBG(0, "HQA, 0x18, before:%x\n", val);
  20. USBPHY_CLR8(0x18, 0x08);
  21. USBPHY_SET8(0x18, 0x06);
  22. val = USBPHY_READ8(0x18);
  23. DBG(0, "HQA, 0x18, after:%x\n", val);
  24. }
  25. #ifdef FPGA_PLATFORM
  26. bool usb_enable_clock(bool enable)
  27. {
  28. return true;
  29. }
  30. void usb_phy_poweron(void)
  31. {
  32. }
  33. void usb_phy_savecurrent(void)
  34. {
  35. }
  36. void usb_phy_recover(void)
  37. {
  38. }
  39. /* BC1.2 */
  40. void Charger_Detect_Init(void)
  41. {
  42. }
  43. void Charger_Detect_Release(void)
  44. {
  45. }
  46. void usb_phy_context_save(void)
  47. {
  48. }
  49. void usb_phy_context_restore(void)
  50. {
  51. }
  52. #ifdef CONFIG_MTK_UART_USB_SWITCH
  53. bool usb_phy_check_in_uart_mode(void)
  54. {
  55. UINT8 usb_port_mode;
  56. usb_enable_clock(true);
  57. udelay(50);
  58. usb_port_mode = USB_PHY_Read_Register8(0x6B);
  59. usb_enable_clock(false);
  60. if ((usb_port_mode == 0x5C) || (usb_port_mode == 0x5E))
  61. return true;
  62. else
  63. return false;
  64. }
  65. void usb_phy_switch_to_uart(void)
  66. {
  67. int var;
  68. #if 0
  69. /* SW disconnect */
  70. var = USB_PHY_Read_Register8(0x68);
  71. DBG(0, "[MUSB]addr: 0x68, value: %x\n", var);
  72. USB_PHY_Write_Register8(0x15, 0x68);
  73. DBG(0, "[MUSB]addr: 0x68, value after: %x\n", USB_PHY_Read_Register8(0x68));
  74. var = USB_PHY_Read_Register8(0x6A);
  75. DBG(0, "[MUSB]addr: 0x6A, value: %x\n", var);
  76. USB_PHY_Write_Register8(0x0, 0x6A);
  77. DBG(0, "[MUSB]addr: 0x6A, value after: %x\n", USB_PHY_Read_Register8(0x6A));
  78. /* SW disconnect */
  79. #endif
  80. /* Set ru_uart_mode to 2'b01 */
  81. var = USB_PHY_Read_Register8(0x6B);
  82. DBG(0, "[MUSB]addr: 0x6B, value: %x\n", var);
  83. USB_PHY_Write_Register8(var | 0x7C, 0x6B);
  84. DBG(0, "[MUSB]addr: 0x6B, value after: %x\n", USB_PHY_Read_Register8(0x6B));
  85. /* Set RG_UART_EN to 1 */
  86. var = USB_PHY_Read_Register8(0x6E);
  87. DBG(0, "[MUSB]addr: 0x6E, value: %x\n", var);
  88. USB_PHY_Write_Register8(var | 0x07, 0x6E);
  89. DBG(0, "[MUSB]addr: 0x6E, value after: %x\n", USB_PHY_Read_Register8(0x6E));
  90. /* Set RG_USB20_DM_100K_EN to 1 */
  91. var = USB_PHY_Read_Register8(0x22);
  92. DBG(0, "[MUSB]addr: 0x22, value: %x\n", var);
  93. USB_PHY_Write_Register8(var | 0x02, 0x22);
  94. DBG(0, "[MUSB]addr: 0x22, value after: %x\n", USB_PHY_Read_Register8(0x22));
  95. var = DRV_Reg8(UART1_BASE + 0x90);
  96. DBG(0, "[MUSB]addr: 0x11002090 (UART1), value: %x\n", var);
  97. DRV_WriteReg8(UART1_BASE + 0x90, var | 0x01);
  98. DBG(0, "[MUSB]addr: 0x11002090 (UART1), value after: %x\n\n", DRV_Reg8(UART1_BASE + 0x90));
  99. /* SW disconnect */
  100. mt_usb_disconnect();
  101. }
  102. void usb_phy_switch_to_usb(void)
  103. {
  104. int var;
  105. /* Set RG_UART_EN to 0 */
  106. var = USB_PHY_Read_Register8(0x6E);
  107. DBG(0, "[MUSB]addr: 0x6E, value: %x\n", var);
  108. USB_PHY_Write_Register8(var & ~0x01, 0x6E);
  109. DBG(0, "[MUSB]addr: 0x6E, value after: %x\n", USB_PHY_Read_Register8(0x6E));
  110. /* Set RG_USB20_DM_100K_EN to 0 */
  111. var = USB_PHY_Read_Register8(0x22);
  112. DBG(0, "[MUSB]addr: 0x22, value: %x\n", var);
  113. USB_PHY_Write_Register8(var & ~0x02, 0x22);
  114. DBG(0, "[MUSB]addr: 0x22, value after: %x\n", USB_PHY_Read_Register8(0x22));
  115. var = DRV_Reg8(UART1_BASE + 0x90);
  116. DBG(0, "[MUSB]addr: 0x11002090 (UART1), value: %x\n", var);
  117. DRV_WriteReg8(UART1_BASE + 0x90, var & ~0x01);
  118. DBG(0, "[MUSB]addr: 0x11002090 (UART1), value after: %x\n\n", DRV_Reg8(UART1_BASE + 0x90));
  119. #if 0
  120. /* SW connect */
  121. var = USB_PHY_Read_Register8(0x68);
  122. DBG(0, "[MUSB]addr: 0x68, value: %x\n", var);
  123. USB_PHY_Write_Register8(0x0, 0x68);
  124. DBG(0, "[MUSB]addr: 0x68, value after: %x\n", USB_PHY_Read_Register8(0x68));
  125. var = USB_PHY_Read_Register8(0x6A);
  126. DBG(0, "[MUSB]addr: 0x6A, value: %x\n", var);
  127. USB_PHY_Write_Register8(0x0, 0x6A);
  128. DBG(0, "[MUSB]addr: 0x6A, value after: %x\n", USB_PHY_Read_Register8(0x6A));
  129. /* SW connect */
  130. #endif
  131. /* SW connect */
  132. mt_usb_connect();
  133. }
  134. #endif
  135. #else
  136. #ifdef CONFIG_MTK_UART_USB_SWITCH
  137. bool in_uart_mode = false;
  138. #endif
  139. static DEFINE_SPINLOCK(musb_reg_clock_lock);
  140. static void enable_phy_clock(bool enable)
  141. {
  142. /* USB phy 48M clock , UNIVPLL_CON0[26] */
  143. /* if (enable) { */
  144. /* writel(readl((void __iomem *)UNIVPLL_CON0)|(0x04000000), */
  145. /* (void __iomem *)UNIVPLL_CON0); */
  146. /* } else { */
  147. /* writel(readl((void __iomem *)UNIVPLL_CON0)&~(0x04000000), */
  148. /* (void __iomem *)UNIVPLL_CON0); */
  149. /* } */
  150. }
  151. bool usb_enable_clock(bool enable)
  152. {
  153. static int count;
  154. static int real_enable = 0, real_disable;
  155. static int virt_enable = 0, virt_disable;
  156. bool res = TRUE;
  157. unsigned long flags;
  158. DBG(1, "enable(%d),count(%d),<%d,%d,%d,%d>\n",
  159. enable, count, virt_enable, virt_disable, real_enable, real_disable);
  160. spin_lock_irqsave(&musb_reg_clock_lock, flags);
  161. if (enable && count == 0) {
  162. real_enable++;
  163. enable_phy_clock(true);
  164. #ifdef CONFIG_MTK_CLKMGR
  165. res = enable_clock(MT_CG_PERI_USB0, "PERI_USB");
  166. #else
  167. res = clk_enable(musb_clk);
  168. #endif
  169. } else if (!enable && count == 1) {
  170. real_disable++;
  171. #ifdef CONFIG_MTK_CLKMGR
  172. res = disable_clock(MT_CG_PERI_USB0, "PERI_USB");
  173. #else
  174. res = 0;
  175. clk_disable(musb_clk);
  176. #endif
  177. enable_phy_clock(false);
  178. }
  179. if (enable) {
  180. virt_enable++;
  181. count++;
  182. } else {
  183. virt_disable++;
  184. count = (count == 0) ? 0 : (count - 1);
  185. }
  186. spin_unlock_irqrestore(&musb_reg_clock_lock, flags);
  187. DBG(1, "enable(%d),count(%d),res(%d),<%d,%d,%d,%d>\n",
  188. enable, count, res, virt_enable, virt_disable, real_enable, real_disable);
  189. return 1;
  190. }
  191. static void hs_slew_rate_cal(void)
  192. {
  193. unsigned long data;
  194. unsigned long x;
  195. unsigned char value;
  196. unsigned long start_time, timeout;
  197. unsigned int timeout_flag = 0;
  198. /* 4 s1:enable usb ring oscillator. */
  199. USBPHY_SET8(0x15, 0x80);
  200. /* 4 s2:wait 1us. */
  201. udelay(1);
  202. /* 4 s3:enable free run clock */
  203. USBPHY_SET8(0xf00 - 0x800 + 0x11, 0x01);
  204. /* 4 s4:setting cyclecnt. */
  205. USBPHY_SET8(0xf00 - 0x800 + 0x01, 0x04);
  206. /* 4 s5:enable frequency meter */
  207. USBPHY_SET8(0xf00 - 0x800 + 0x03, 0x01);
  208. /* 4 s6:wait for frequency valid. */
  209. start_time = jiffies;
  210. timeout = jiffies + 3 * HZ;
  211. while (!(USBPHY_READ8(0xf00 - 0x800 + 0x10) & 0x1)) {
  212. if (time_after(jiffies, timeout)) {
  213. timeout_flag = 1;
  214. break;
  215. }
  216. }
  217. /* 4 s7: read result. */
  218. if (timeout_flag) {
  219. DBG(0, "[USBPHY] Slew Rate Calibration: Timeout\n");
  220. value = 0x4;
  221. } else {
  222. data = USBPHY_READ32(0xf00 - 0x800 + 0x0c);
  223. x = ((1024 * FRA * PARA) / data);
  224. value = (unsigned char)(x / 1000);
  225. if ((x - value * 1000) / 100 >= 5)
  226. value += 1;
  227. DBG(0, "[USBPHY]slew calibration:FM_OUT =%lu,x=%lu,value=%d\n", data, x, value);
  228. }
  229. /* 4 s8: disable Frequency and run clock. */
  230. USBPHY_CLR8(0xf00 - 0x800 + 0x03, 0x01);
  231. USBPHY_CLR8(0xf00 - 0x800 + 0x11, 0x01);
  232. /* 4 s9: */
  233. #define MSK_RG_USB20_HSTX_SRCTRL 0x7
  234. /* all clr first then set */
  235. USBPHY_CLR8(0x15, MSK_RG_USB20_HSTX_SRCTRL << 4);
  236. USBPHY_SET8(0x15, (value & MSK_RG_USB20_HSTX_SRCTRL) << 4);
  237. /* 4 s10:disable usb ring oscillator. */
  238. USBPHY_CLR8(0x15, 0x80);
  239. }
  240. #ifdef CONFIG_MTK_UART_USB_SWITCH
  241. bool usb_phy_check_in_uart_mode(void)
  242. {
  243. UINT8 usb_port_mode;
  244. usb_enable_clock(true);
  245. udelay(50);
  246. usb_port_mode = USBPHY_READ8(0x6B);
  247. usb_enable_clock(false);
  248. if ((usb_port_mode == 0x5C) || (usb_port_mode == 0x5E)) {
  249. DBG(0, "%s:%d - IN UART MODE : 0x%x\n", __func__, __LINE__, usb_port_mode);
  250. DBG(0, "Mask PMIC charger detection in UART mode.\n");
  251. pmic_chrdet_int_en(0);
  252. in_uart_mode = true;
  253. } else {
  254. DBG(0, "%s:%d - NOT IN UART MODE : 0x%x\n", __func__, __LINE__, usb_port_mode);
  255. in_uart_mode = false;
  256. }
  257. return in_uart_mode;
  258. }
  259. void usb_phy_switch_to_uart(void)
  260. {
  261. if (usb_phy_check_in_uart_mode())
  262. return;
  263. DBG(0, "Mask PMIC charger detection in UART mode.\n");
  264. pmic_chrdet_int_en(0);
  265. usb_enable_clock(true);
  266. udelay(50);
  267. /* RG_USB20_BC11_SW_EN = 1'b0 */
  268. USBPHY_CLR8(0x1a, 0x80);
  269. /* Set RG_SUSPENDM to 1 */
  270. USBPHY_SET8(0x68, 0x08);
  271. /* force suspendm = 1 */
  272. USBPHY_SET8(0x6a, 0x04);
  273. /* Set ru_uart_mode to 2'b01 */
  274. USBPHY_SET8(0x6B, 0x5C);
  275. /* Set RG_UART_EN to 1 */
  276. USBPHY_SET8(0x6E, 0x07);
  277. /* Set RG_USB20_DM_100K_EN to 1 */
  278. USBPHY_SET8(0x22, 0x02);
  279. usb_enable_clock(false);
  280. /* GPIO Selection */
  281. DRV_WriteReg32(ap_uart0_base + 0xB0, 0x1);
  282. }
  283. void usb_phy_switch_to_usb(void)
  284. {
  285. /* GPIO Selection */
  286. DRV_WriteReg32(ap_uart0_base + 0xB0, 0x0);
  287. usb_enable_clock(true);
  288. udelay(50);
  289. /* clear force_uart_en */
  290. USBPHY_WRITE8(0x6B, 0x00);
  291. usb_enable_clock(false);
  292. usb_phy_poweron();
  293. /* disable the USB clock turned on in usb_phy_poweron() */
  294. usb_enable_clock(false);
  295. DBG(0, "Unmask PMIC charger detection in USB mode.\n");
  296. pmic_chrdet_int_en(1);
  297. }
  298. #endif
  299. /* Denali_USB_PWR Sequence 20141030.xls */
  300. void usb_phy_poweron(void)
  301. {
  302. #ifdef CONFIG_MTK_UART_USB_SWITCH
  303. if (usb_phy_check_in_uart_mode())
  304. return;
  305. #endif
  306. /* enable USB MAC clock. */
  307. usb_enable_clock(true);
  308. /* wait 50 usec for PHY3.3v/1.8v stable. */
  309. udelay(50);
  310. /* force_uart_en, 1'b0 */
  311. USBPHY_CLR8(0x6b, 0x04);
  312. /* RG_UART_EN, 1'b0 */
  313. USBPHY_CLR8(0x6e, 0x01);
  314. /* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
  315. USBPHY_CLR8(0x21, 0x03);
  316. /* RG_USB20_BC11_SW_EN, 1'b0 */
  317. USBPHY_CLR8(0x1a, 0x80);
  318. /* rg_usb20_dp_100k_mode, 1'b1 */
  319. USBPHY_SET8(0x22, 0x04);
  320. /* USB20_DP_100K_EN 1'b0, RG_USB20_DM_100K_EN, 1'b0 */
  321. USBPHY_CLR8(0x22, 0x03);
  322. /* RG_USB20_OTG_VBUSCMP_EN, 1'b1 */
  323. USBPHY_SET8(0x1a, 0x10);
  324. /* force_suspendm, 1'b0 */
  325. USBPHY_CLR8(0x6a, 0x04);
  326. /* 7 s7: wait for 800 usec. */
  327. udelay(800);
  328. /* force enter device mode, from K2, FIXME */
  329. USBPHY_CLR8(0x6c, 0x10);
  330. USBPHY_SET8(0x6c, 0x2F);
  331. USBPHY_SET8(0x6d, 0x3F);
  332. DBG(0, "usb power on success\n");
  333. }
  334. #ifdef CONFIG_MTK_UART_USB_SWITCH
  335. static bool skipDisableUartMode = true;
  336. #endif
  337. /* Denali_USB_PWR Sequence 20141030.xls */
  338. static void usb_phy_savecurrent_internal(void)
  339. {
  340. /* 4 1. swtich to USB function. (system register, force ip into usb mode. */
  341. #ifdef CONFIG_MTK_UART_USB_SWITCH
  342. if (!usb_phy_check_in_uart_mode()) {
  343. /* enable USB MAC clock. */
  344. usb_enable_clock(true);
  345. /* wait 50 usec for PHY3.3v/1.8v stable. */
  346. udelay(50);
  347. /* force_uart_en, 1'b0 */
  348. USBPHY_CLR8(0x6b, 0x04);
  349. /* RG_UART_EN, 1'b0 */
  350. USBPHY_CLR8(0x6e, 0x01);
  351. /* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
  352. USBPHY_CLR8(0x21, 0x03);
  353. /* RG_USB20_BC11_SW_EN, 1'b0 */
  354. USBPHY_CLR8(0x1a, 0x80);
  355. /* RG_USB20_OTG_VBUSCMP_EN, 1'b0 */
  356. USBPHY_CLR8(0x1a, 0x10);
  357. /* RG_SUSPENDM, 1'b1 */
  358. USBPHY_SET8(0x68, 0x08);
  359. /* force_suspendm, 1'b1 */
  360. USBPHY_SET8(0x6a, 0x04);
  361. usb_enable_clock(false);
  362. } else {
  363. if (skipDisableUartMode)
  364. skipDisableUartMode = false;
  365. else
  366. return;
  367. }
  368. #else
  369. /* force_uart_en, 1'b0 */
  370. USBPHY_CLR8(0x6b, 0x04);
  371. /* RG_UART_EN, 1'b0 */
  372. USBPHY_CLR8(0x6e, 0x01);
  373. /* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
  374. USBPHY_CLR8(0x21, 0x03);
  375. /* RG_USB20_BC11_SW_EN, 1'b0 */
  376. USBPHY_CLR8(0x1a, 0x80);
  377. /* RG_USB20_OTG_VBUSCMP_EN, 1'b0 */
  378. USBPHY_CLR8(0x1a, 0x10);
  379. /* RG_SUSPENDM, 1'b1 */
  380. USBPHY_SET8(0x68, 0x08);
  381. /* force_suspendm, 1'b1 */
  382. USBPHY_SET8(0x6a, 0x04);
  383. #endif
  384. /* RG_DPPULLDOWN, 1'b1, RG_DMPULLDOWN, 1'b1 */
  385. USBPHY_SET8(0x68, 0xc0);
  386. /* RG_XCVRSEL[1:0], 2'b01. */
  387. USBPHY_CLR8(0x68, 0x20);
  388. USBPHY_SET8(0x68, 0x10);
  389. /* RG_TERMSEL, 1'b1 */
  390. USBPHY_SET8(0x68, 0x04);
  391. /* RG_DATAIN[3:0], 4'b0000 */
  392. USBPHY_CLR8(0x69, 0x3c);
  393. /* force_dp_pulldown, 1'b1, force_dm_pulldown, 1'b1,
  394. force_xcversel, 1'b1, force_termsel, 1'b1, force_datain, 1'b1 */
  395. USBPHY_SET8(0x6a, 0xba);
  396. udelay(800);
  397. /* RG_SUSPENDM, 1'b0 */
  398. USBPHY_CLR8(0x68, 0x08);
  399. /* ALPS00427972, implement the analog register formula */
  400. /*
  401. DBG(0, "%s: USBPHY_READ8(0x05) = 0x%x\n", __func__, USBPHY_READ8(0x05));
  402. DBG(0, "%s: USBPHY_READ8(0x07) = 0x%x\n", __func__, USBPHY_READ8(0x07));
  403. */
  404. /* ALPS00427972, implement the analog register formula */
  405. udelay(1);
  406. /* force enter device mode, from K2, FIXME */
  407. /* force enter device mode */
  408. /* USBPHY_CLR8(0x6c, 0x10); */
  409. /* USBPHY_SET8(0x6c, 0x2E); */
  410. /* USBPHY_SET8(0x6d, 0x3E); */
  411. #ifdef CONFIG_MTK_UART_USB_SWITCH
  412. if (in_uart_mode) {
  413. USBPHY_SET8(0x68, 0x08);
  414. DBG(0, "%s:%d - SWITCH to UART MODE after savecurrent!\n", __func__, __LINE__);
  415. }
  416. #endif
  417. }
  418. void usb_phy_savecurrent(void)
  419. {
  420. /* to avoid hw access during clock-off */
  421. unsigned long flags;
  422. int do_lock;
  423. do_lock = 0;
  424. usb_phy_savecurrent_internal();
  425. /* to avoid deadlock, musb_shutdown will hold this clock too */
  426. if (mtk_musb && !musb_is_shutting) {
  427. spin_lock_irqsave(&mtk_musb->lock, flags);
  428. do_lock = 1;
  429. }
  430. /* 4 14. turn off internal 48Mhz PLL. */
  431. usb_enable_clock(false);
  432. if (do_lock)
  433. spin_unlock_irqrestore(&mtk_musb->lock, flags);
  434. DBG(0, "usb save current success\n");
  435. }
  436. /* Denali_USB_PWR Sequence 20141030.xls */
  437. void usb_phy_recover(void)
  438. {
  439. /* to avoid hw access during clock-on */
  440. unsigned long flags;
  441. int do_lock;
  442. do_lock = 0;
  443. if (mtk_musb) {
  444. spin_lock_irqsave(&mtk_musb->lock, flags);
  445. do_lock = 1;
  446. } else {
  447. DBG(0, "mtk_musb is NULL\n");
  448. return;
  449. }
  450. /* turn on USB reference clock. */
  451. usb_enable_clock(true);
  452. if (do_lock)
  453. spin_unlock_irqrestore(&mtk_musb->lock, flags);
  454. /* wait 50 usec. */
  455. udelay(50);
  456. #ifdef CONFIG_MTK_UART_USB_SWITCH
  457. if (!usb_phy_check_in_uart_mode()) {
  458. /* clean PUPD_BIST_EN */
  459. /* PUPD_BIST_EN = 1'b0 */
  460. /* PMIC will use it to detect charger type */
  461. USBPHY_CLR8(0x1d, 0x10);
  462. /* force_uart_en, 1'b0 */
  463. USBPHY_CLR8(0x6b, 0x04);
  464. /* RG_UART_EN, 1'b0 */
  465. USBPHY_CLR8(0x6e, 0x01);
  466. /* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
  467. USBPHY_CLR8(0x21, 0x03);
  468. /* force_suspendm, 1'b0 */
  469. USBPHY_CLR8(0x6a, 0x04);
  470. skipDisableUartMode = false;
  471. } else {
  472. if (!skipDisableUartMode)
  473. return;
  474. }
  475. #else
  476. /* clean PUPD_BIST_EN */
  477. /* PUPD_BIST_EN = 1'b0 */
  478. /* PMIC will use it to detect charger type */
  479. USBPHY_CLR8(0x1d, 0x10);
  480. /* force_uart_en, 1'b0 */
  481. USBPHY_CLR8(0x6b, 0x04);
  482. /* RG_UART_EN, 1'b0 */
  483. USBPHY_CLR8(0x6e, 0x01);
  484. /* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
  485. USBPHY_CLR8(0x21, 0x03);
  486. /* force_suspendm, 1'b0 */
  487. USBPHY_CLR8(0x6a, 0x04);
  488. #endif
  489. /* RG_DPPULLDOWN, 1'b0, RG_DMPULLDOWN, 1'b0 */
  490. USBPHY_CLR8(0x68, 0xc0);
  491. /* RG_XCVRSEL[1:0], 2'b00. */
  492. USBPHY_CLR8(0x68, 0x30);
  493. /* RG_TERMSEL, 1'b0 */
  494. USBPHY_CLR8(0x68, 0x04);
  495. /* RG_DATAIN[3:0], 4'b0000 */
  496. USBPHY_CLR8(0x69, 0x3c);
  497. /* force_dp_pulldown, 1'b0, force_dm_pulldown, 1'b0,
  498. force_xcversel, 1'b0, force_termsel, 1'b0, force_datain, 1'b0 */
  499. USBPHY_CLR8(0x6a, 0xba);
  500. /* RG_USB20_BC11_SW_EN, 1'b0 */
  501. USBPHY_CLR8(0x1a, 0x80);
  502. /* RG_USB20_OTG_VBUSCMP_EN, 1'b1 */
  503. USBPHY_SET8(0x1a, 0x10);
  504. /* wait 800 usec. */
  505. udelay(800);
  506. /* force enter device mode, from K2, FIXME */
  507. USBPHY_CLR8(0x6c, 0x10);
  508. USBPHY_SET8(0x6c, 0x2F);
  509. USBPHY_SET8(0x6d, 0x3F);
  510. /* from K2, FIXME */
  511. #if defined(MTK_HDMI_SUPPORT)
  512. USBPHY_SET8(0x05, 0x05);
  513. USBPHY_SET8(0x05, 0x50);
  514. #endif
  515. /* adjustment after HQA */
  516. HQA_special();
  517. hs_slew_rate_cal();
  518. DBG(0, "usb recovery success\n");
  519. }
  520. /* BC1.2 */
  521. void Charger_Detect_Init(void)
  522. {
  523. /* turn on USB reference clock. */
  524. usb_enable_clock(true);
  525. /* wait 50 usec. */
  526. udelay(50);
  527. /* RG_USB20_BC11_SW_EN = 1'b1 */
  528. USBPHY_SET8(0x1a, 0x80);
  529. DBG(0, "Charger_Detect_Init\n");
  530. }
  531. void Charger_Detect_Release(void)
  532. {
  533. /* RG_USB20_BC11_SW_EN = 1'b0 */
  534. USBPHY_CLR8(0x1a, 0x80);
  535. udelay(1);
  536. /* 4 14. turn off internal 48Mhz PLL. */
  537. usb_enable_clock(false);
  538. DBG(0, "Charger_Detect_Release\n");
  539. }
  540. void usb_phy_context_save(void)
  541. {
  542. #ifdef CONFIG_MTK_UART_USB_SWITCH
  543. in_uart_mode = usb_phy_check_in_uart_mode();
  544. #endif
  545. }
  546. void usb_phy_context_restore(void)
  547. {
  548. #ifdef CONFIG_MTK_UART_USB_SWITCH
  549. if (in_uart_mode)
  550. usb_phy_switch_to_uart();
  551. #endif
  552. usb_phy_savecurrent_internal();
  553. }
  554. #endif