mt_musbphy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Copyright (c) 2012 MediaTek Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <sys/types.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <debug.h>
  27. #include <reg.h>
  28. #include <platform/bitops.h>
  29. #include <platform/mt_reg_base.h>
  30. #include <platform/mt_typedefs.h>
  31. #include <dev/udc.h>
  32. /* common */
  33. #include <mt_musb.h>
  34. #if CFG_FPGA_PLATFORM
  35. /* for usb phy */
  36. #include <platform/mt_i2c.h>
  37. #endif
  38. #ifdef MACH_FPGA
  39. #define CFG_FPGA_PLATFORM (1)
  40. #else
  41. #define DBG_PHY_CALIBRATION 1
  42. #endif
  43. /* usb phy bring up */
  44. #if CFG_FPGA_PLATFORM
  45. #define USB_I2C_ID I2C0 /* 0 - 6 */
  46. #define PATH_NORMAL 0
  47. #define PATH_PMIC 1
  48. U32 usb_i2c_read8 (U8 addr, U8 *dataBuffer)
  49. {
  50. U32 ret_code = I2C_OK;
  51. // U8 write_data = addr;
  52. U16 len;
  53. static struct mt_i2c_t i2c;
  54. i2c.id = USB_I2C_ID;
  55. i2c.addr = 0x60; /* i2c API will shift-left this value to 0xc0 */
  56. i2c.mode = ST_MODE;
  57. i2c.speed = 100;
  58. i2c.dma_en = 0;
  59. len = 1;
  60. /* set register command */
  61. #if 0
  62. ret_code = i2c_write(&i2c, &write_data, len);
  63. if (ret_code != len)
  64. return ret_code;
  65. ret_code = i2c_read(&i2c, dataBuffer, len);
  66. #else
  67. ret_code = i2c_write_read(&i2c, dataBuffer, len, len);
  68. #endif
  69. return ret_code;
  70. }
  71. U32 usb_i2c_write8(U8 addr, U8 value)
  72. {
  73. U32 ret_code = I2C_OK;
  74. U8 write_data[2];
  75. U16 len;
  76. static struct mt_i2c_t i2c;
  77. write_data[0]= addr;
  78. write_data[1] = value;
  79. i2c.id = USB_I2C_ID;
  80. i2c.addr = 0x60; /* i2c API will shift-left this value to 0xc0 */
  81. i2c.mode = ST_MODE;
  82. i2c.speed = 100;
  83. i2c.dma_en = 0;
  84. len = 2;
  85. ret_code = i2c_write(&i2c, write_data, len);
  86. return ret_code;
  87. }
  88. #define USBPHY_I2C_READ8(addr, buffer) usb_i2c_read8(addr, buffer)
  89. #define USBPHY_I2C_WRITE8(addr, value) usb_i2c_write8(addr, value)
  90. #endif
  91. /* use mt_typedefs.h */
  92. #define USBPHY_READ8(offset) readb(USB20_PHY_BASE+offset)
  93. #define USBPHY_WRITE8(offset, value) writeb(value, USB20_PHY_BASE+offset)
  94. #define USBPHY_SET8(offset, mask) USBPHY_WRITE8(offset, (USBPHY_READ8(offset)) | (mask))
  95. #define USBPHY_CLR8(offset, mask) USBPHY_WRITE8(offset, (USBPHY_READ8(offset)) & (~mask))
  96. #define USB11PHY_READ8(offset) readb(USB11_PHY_BASE+offset)
  97. #define USB11PHY_WRITE8(offset, value) writeb(value, USB11_PHY_BASE+offset)
  98. #define USB11PHY_SET8(offset, mask) USB11PHY_WRITE8(offset, (USB11PHY_READ8(offset)) | (mask))
  99. #define USB11PHY_CLR8(offset, mask) USB11PHY_WRITE8(offset, (USB11PHY_READ8(offset)) & (~mask))
  100. #if CFG_FPGA_PLATFORM
  101. void mt_usb_phy_poweron(void)
  102. {
  103. #define PHY_DRIVING 0x3
  104. #if defined(USB_PHY_DRIVING_TUNING)
  105. UINT8 usbreg8;
  106. unsigned int i;
  107. #endif
  108. /* force_suspendm = 0 */
  109. USBPHY_CLR8(0x6a, 0x04);
  110. USBPHY_I2C_WRITE8(0xff, 0x00);
  111. USBPHY_I2C_WRITE8(0x61, 0x04);
  112. USBPHY_I2C_WRITE8(0x68, 0x00);
  113. USBPHY_I2C_WRITE8(0x6a, 0x00);
  114. USBPHY_I2C_WRITE8(0x00, 0x6e);
  115. USBPHY_I2C_WRITE8(0x1b, 0x0c);
  116. USBPHY_I2C_WRITE8(0x08, 0x44);
  117. USBPHY_I2C_WRITE8(0x11, 0x55);
  118. USBPHY_I2C_WRITE8(0x1a, 0x68);
  119. #if defined(USB_PHY_DRIVING_TUNING)
  120. /* driving tuning */
  121. USBPHY_I2C_READ8(0xab, &usbreg8);
  122. usbreg8 &= ~0x3;
  123. usbreg8 |= PHY_DRIVING;
  124. USBPHY_I2C_WRITE8(0xab, usbreg8);
  125. for(i = 0; i < 16; i++)
  126. {
  127. USBPHY_I2C_READ8((0x92+i), &usbreg8);
  128. usbreg8 &= ~0x3;
  129. usbreg8 |= PHY_DRIVING;
  130. USBPHY_I2C_WRITE8((0x92+i), usbreg8);
  131. }
  132. USBPHY_I2C_READ8(0xbc, &usbreg8);
  133. usbreg8 &= ~0x3;
  134. usbreg8 |= PHY_DRIVING;
  135. USBPHY_I2C_WRITE8(0xbc, usbreg8);
  136. USBPHY_I2C_READ8(0xbe, &usbreg8);
  137. usbreg8 &= ~0x3;
  138. usbreg8 |= PHY_DRIVING;
  139. USBPHY_I2C_WRITE8(0xbe, usbreg8);
  140. USBPHY_I2C_READ8(0xbf, &usbreg8);
  141. usbreg8 &= ~0x3;
  142. usbreg8 |= PHY_DRIVING;
  143. USBPHY_I2C_WRITE8(0xbf, usbreg8);
  144. USBPHY_I2C_READ8(0xcd, &usbreg8);
  145. usbreg8 &= ~0x3;
  146. usbreg8 |= PHY_DRIVING;
  147. USBPHY_I2C_WRITE8(0xcd, usbreg8);
  148. USBPHY_I2C_READ8(0xf1, &usbreg8);
  149. usbreg8 &= ~0x3;
  150. usbreg8 |= PHY_DRIVING;
  151. USBPHY_I2C_WRITE8(0xf1, usbreg8);
  152. USBPHY_I2C_READ8(0xa7, &usbreg8);
  153. usbreg8 &= ~0x3;
  154. usbreg8 |= PHY_DRIVING;
  155. USBPHY_I2C_WRITE8(0xa7, usbreg8);
  156. USBPHY_I2C_READ8(0xa8, &usbreg8);
  157. usbreg8 &= ~0x3;
  158. usbreg8 |= PHY_DRIVING;
  159. USBPHY_I2C_WRITE8(0xa8, usbreg8);
  160. #endif
  161. udelay(800);
  162. }
  163. void mt_usb_phy_savecurrent (void)
  164. {
  165. /* no need */
  166. }
  167. void mt_usb_phy_recover (void)
  168. {
  169. /* no need */
  170. }
  171. void mt_usb11_phy_savecurrent(void)
  172. {
  173. /* no need */
  174. }
  175. void Charger_Detect_Init(void)
  176. {
  177. /* no need */
  178. }
  179. void Charger_Detect_Release(void)
  180. {
  181. /* no need */
  182. }
  183. #else
  184. void mt_usb_phy_poweron(void)
  185. {
  186. /*
  187. * swtich to USB function.
  188. * (system register, force ip into usb mode).
  189. */
  190. USBPHY_CLR8(0x6b, 0x04);
  191. USBPHY_CLR8(0x6e, 0x01);
  192. USBPHY_CLR8(0x21, 0x03);
  193. /* RG_USB20_BC11_SW_EN = 1'b0 */
  194. USBPHY_SET8(0x22, 0x04);
  195. USBPHY_CLR8(0x1a, 0x80);
  196. /* RG_USB20_DP_100K_EN = 1'b0 */
  197. /* RG_USB20_DP_100K_EN = 1'b0 */
  198. USBPHY_CLR8(0x22, 0x03);
  199. /*OTG enable*/
  200. USBPHY_SET8(0x20, 0x10);
  201. /* release force suspendm */
  202. USBPHY_CLR8(0x6a, 0x04);
  203. udelay(800);
  204. /* force enter device mode */
  205. USBPHY_CLR8(0x6c, 0x10);
  206. USBPHY_SET8(0x6c, 0x2E);
  207. USBPHY_SET8(0x6d, 0x3E);
  208. return;
  209. }
  210. void mt_usb_phy_savecurrent(void)
  211. {
  212. /*
  213. * swtich to USB function.
  214. * (system register, force ip into usb mode).
  215. */
  216. USBPHY_CLR8(0x6b, 0x04);
  217. USBPHY_CLR8(0x6e, 0x01);
  218. USBPHY_CLR8(0x21, 0x03);
  219. /* release force suspendm */
  220. USBPHY_CLR8(0x6a, 0x04);
  221. USBPHY_SET8(0x68, 0x04);
  222. /* RG_DPPULLDOWN./RG_DMPULLDOWN. */
  223. USBPHY_SET8(0x68, 0xc0);
  224. /* RG_XCVRSEL[1:0] = 2'b01 */
  225. USBPHY_CLR8(0x68, 0x30);
  226. USBPHY_SET8(0x68, 0x10);
  227. /* RG_TERMSEL = 1'b1 */
  228. USBPHY_SET8(0x68, 0x04);
  229. /* RG_DATAIN[3:0] = 4'b0000 */
  230. USBPHY_CLR8(0x69, 0x3c);
  231. /*
  232. * force_dp_pulldown, force_dm_pulldown,
  233. * force_xcversel, force_termsel.
  234. */
  235. USBPHY_SET8(0x6a, 0xba);
  236. /* RG_USB20_BC11_SW_EN = 1'b0 */
  237. USBPHY_CLR8(0x1a, 0x80);
  238. /* RG_USB20_OTG_VBUSSCMP_EN = 1'b0 */
  239. USBPHY_CLR8(0x1a, 0x10);
  240. udelay(800);
  241. USBPHY_CLR8(0x6a, 0x04);
  242. /* rg_usb20_pll_stable = 1 */
  243. //USBPHY_SET8(0x63, 0x02);
  244. udelay(1);
  245. /* force suspendm = 1 */
  246. //USBPHY_SET8(0x6a, 0x04);
  247. //udelay(1);
  248. return;
  249. }
  250. void mt_usb_phy_recover(void)
  251. {
  252. /* clean PUPD_BIST_EN */
  253. /* PUPD_BIST_EN = 1'b0 */
  254. /* PMIC will use it to detect charger type */
  255. USBPHY_CLR8(0x1d, 0x10);
  256. /* force_uart_en = 1'b0 */
  257. USBPHY_CLR8(0x6b, 0x04);
  258. /* RG_UART_EN = 1'b0 */
  259. USBPHY_CLR8(0x6e, 0x01);
  260. /* force_uart_en = 1'b0 */
  261. USBPHY_CLR8(0x6a, 0x04);
  262. USBPHY_CLR8(0x21, 0x03);
  263. USBPHY_CLR8(0x68, 0xf4);
  264. /* RG_DATAIN[3:0] = 4'b0000 */
  265. USBPHY_CLR8(0x69, 0x3c);
  266. USBPHY_CLR8(0x6a, 0xba);
  267. /* RG_USB20_BC11_SW_EN = 1'b0 */
  268. USBPHY_CLR8(0x1a, 0x80);
  269. /* RG_USB20_OTG_VBUSSCMP_EN = 1'b1 */
  270. USBPHY_SET8(0x1a, 0x10);
  271. udelay(800);
  272. /* force enter device mode */
  273. //USBPHY_CLR8(0x6c, 0x10);
  274. //USBPHY_SET8(0x6c, 0x2E);
  275. //USBPHY_SET8(0x6d, 0x3E);
  276. }
  277. void mt_usb11_phy_savecurrent(void)
  278. {
  279. #if 0
  280. USB11PHY_SET8(0xca, 0x10);
  281. USB11PHY_SET8(0xcb, 0x3c);
  282. USB11PHY_CLR8(0xc1, 0x08);
  283. USB11PHY_CLR8(0xc7, 0x06);
  284. USB11PHY_SET8(0xc6, 0x06);
  285. #endif
  286. }
  287. void Charger_Detect_Init(void)
  288. {
  289. /* RG_USB20_BC11_SW_EN = 1'b1 */
  290. USBPHY_SET8(0x1a, 0x80);
  291. }
  292. void Charger_Detect_Release(void)
  293. {
  294. /* RG_USB20_BC11_SW_EN = 1'b0 */
  295. USBPHY_CLR8(0x1a, 0x80);
  296. }
  297. #endif
  298. /* usb phy bring up end */
  299. #if !CFG_FPGA_PLATFORM
  300. //ALPS00427972, implement the analog register formula
  301. void mt_usb_phy_calibraion (int case_set, int input_reg)
  302. {
  303. #if 0
  304. int temp_added=0;
  305. int temp_test=0;
  306. int temp_mask;
  307. #if DBG_PHY_CALIBRATION
  308. DBG_I("%s: case_set %d, input_reg = 0x%x \n", __func__, case_set, input_reg);
  309. #endif
  310. switch(case_set)
  311. {
  312. case 1:
  313. //case 1
  314. //If M_HW_RES3[15:13] !=0
  315. //RG_USB20_TERM_VREF_SEL[2:0] <= RG_USB20_TERM_VREF_SEL[2:0] + M_HW_RES3[15:13]
  316. temp_mask = 0x07;
  317. temp_test = USBPHY_READ8(0x05);
  318. #if DBG_PHY_CALIBRATION
  319. DBG_I("%s: temp_test = 0x%x \n", __func__, temp_test);
  320. #endif
  321. temp_added = (USBPHY_READ8(0x05)& temp_mask) + input_reg;
  322. #if DBG_PHY_CALIBRATION
  323. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  324. #endif
  325. temp_added &= 0x07;
  326. #if DBG_PHY_CALIBRATION
  327. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  328. #endif
  329. USBPHY_CLR8(0x05, temp_mask);
  330. USBPHY_SET8(0x05, temp_added);
  331. temp_test = USBPHY_READ8(0x05);
  332. #if DBG_PHY_CALIBRATION
  333. DBG_I("%s: final temp_test = 0x%x \n", __func__, temp_test);
  334. #endif
  335. break;
  336. case 2:
  337. //case 2
  338. //If M_HW_RES3[12:10] !=0
  339. //RG_USB20_CLKREF_REF[2:0]<= RG_USB20_CLKREF_REF[2:0]+ M_HW_RES3[12:10]
  340. temp_mask = 0x07;
  341. temp_test = USBPHY_READ8(0x07);
  342. #if DBG_PHY_CALIBRATION
  343. DBG_I("%s: temp_test = 0x%x \n", __func__, temp_test);
  344. #endif
  345. temp_added = (USBPHY_READ8(0x07)& temp_mask) + input_reg;
  346. #if DBG_PHY_CALIBRATION
  347. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  348. #endif
  349. temp_added &= 0x07;
  350. #if DBG_PHY_CALIBRATION
  351. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  352. #endif
  353. USBPHY_CLR8(0x07, temp_mask);
  354. USBPHY_SET8(0x07, temp_added);
  355. temp_test = USBPHY_READ8(0x07);
  356. #if DBG_PHY_CALIBRATION
  357. DBG_I("%s: final temp_test = 0x%x \n", __func__, temp_test);
  358. #endif
  359. break;
  360. case 3:
  361. //case 3
  362. //If M_HW_RES3[9:7] !=0
  363. //RG_USB20_VRT_VREF_SEL[2:0]<=RG_USB20_VRT_VREF_SEL[2:0]+ M_HW_RES3[9:7]
  364. temp_mask = 0x70;
  365. temp_test = USBPHY_READ8(0x05);
  366. #if DBG_PHY_CALIBRATION
  367. DBG_I("%s: temp_test = 0x%x \n", __func__, temp_test);
  368. #endif
  369. temp_added = (USBPHY_READ8(0x05)& temp_mask) >> 4;
  370. #if DBG_PHY_CALIBRATION
  371. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  372. #endif
  373. temp_added += input_reg;
  374. #if DBG_PHY_CALIBRATION
  375. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  376. #endif
  377. temp_added &= 0x07;
  378. #if DBG_PHY_CALIBRATION
  379. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  380. #endif
  381. USBPHY_CLR8(0x05, temp_mask);
  382. USBPHY_SET8(0x05, temp_added<<4);
  383. temp_test = USBPHY_READ8(0x05);
  384. #if DBG_PHY_CALIBRATION
  385. DBG_I("%s: final temp_test = 0x%x \n", __func__, temp_test);
  386. #endif
  387. break;
  388. }
  389. #endif
  390. }
  391. //ALPS00427972, implement the analog register formula
  392. #endif