mt_musbphy.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 I2C3 /* 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. USBPHY_I2C_READ8((0x92+i), &usbreg8);
  127. usbreg8 &= ~0x3;
  128. usbreg8 |= PHY_DRIVING;
  129. USBPHY_I2C_WRITE8((0x92+i), usbreg8);
  130. }
  131. USBPHY_I2C_READ8(0xbc, &usbreg8);
  132. usbreg8 &= ~0x3;
  133. usbreg8 |= PHY_DRIVING;
  134. USBPHY_I2C_WRITE8(0xbc, usbreg8);
  135. USBPHY_I2C_READ8(0xbe, &usbreg8);
  136. usbreg8 &= ~0x3;
  137. usbreg8 |= PHY_DRIVING;
  138. USBPHY_I2C_WRITE8(0xbe, usbreg8);
  139. USBPHY_I2C_READ8(0xbf, &usbreg8);
  140. usbreg8 &= ~0x3;
  141. usbreg8 |= PHY_DRIVING;
  142. USBPHY_I2C_WRITE8(0xbf, usbreg8);
  143. USBPHY_I2C_READ8(0xcd, &usbreg8);
  144. usbreg8 &= ~0x3;
  145. usbreg8 |= PHY_DRIVING;
  146. USBPHY_I2C_WRITE8(0xcd, usbreg8);
  147. USBPHY_I2C_READ8(0xf1, &usbreg8);
  148. usbreg8 &= ~0x3;
  149. usbreg8 |= PHY_DRIVING;
  150. USBPHY_I2C_WRITE8(0xf1, usbreg8);
  151. USBPHY_I2C_READ8(0xa7, &usbreg8);
  152. usbreg8 &= ~0x3;
  153. usbreg8 |= PHY_DRIVING;
  154. USBPHY_I2C_WRITE8(0xa7, usbreg8);
  155. USBPHY_I2C_READ8(0xa8, &usbreg8);
  156. usbreg8 &= ~0x3;
  157. usbreg8 |= PHY_DRIVING;
  158. USBPHY_I2C_WRITE8(0xa8, usbreg8);
  159. #endif
  160. udelay(800);
  161. }
  162. void mt_usb_phy_savecurrent (void)
  163. {
  164. /* no need */
  165. }
  166. void mt_usb_phy_recover (void)
  167. {
  168. /* no need */
  169. }
  170. void mt_usb11_phy_savecurrent(void)
  171. {
  172. /* no need */
  173. }
  174. void Charger_Detect_Init(void)
  175. {
  176. /* no need */
  177. }
  178. void Charger_Detect_Release(void)
  179. {
  180. /* no need */
  181. }
  182. #else
  183. void mt_usb_phy_poweron(void)
  184. {
  185. /*
  186. * swtich to USB function.
  187. * (system register, force ip into usb mode).
  188. */
  189. USBPHY_CLR8(0x6b, 0x04);
  190. USBPHY_CLR8(0x6e, 0x01);
  191. USBPHY_CLR8(0x21, 0x03);
  192. /* RG_USB20_BC11_SW_EN = 1'b0 */
  193. USBPHY_SET8(0x22, 0x04);
  194. USBPHY_CLR8(0x1a, 0x80);
  195. /* RG_USB20_DP_100K_EN = 1'b0 */
  196. /* RG_USB20_DP_100K_EN = 1'b0 */
  197. USBPHY_CLR8(0x22, 0x03);
  198. /*OTG enable*/
  199. USBPHY_SET8(0x20, 0x10);
  200. /* release force suspendm */
  201. USBPHY_CLR8(0x6a, 0x04);
  202. udelay(800);
  203. /* force enter device mode */
  204. USBPHY_CLR8(0x6c, 0x10);
  205. USBPHY_SET8(0x6c, 0x2E);
  206. USBPHY_SET8(0x6d, 0x3E);
  207. return;
  208. }
  209. void mt_usb_phy_savecurrent(void)
  210. {
  211. /*
  212. * swtich to USB function.
  213. * (system register, force ip into usb mode).
  214. */
  215. USBPHY_CLR8(0x6b, 0x04);
  216. USBPHY_CLR8(0x6e, 0x01);
  217. USBPHY_CLR8(0x21, 0x03);
  218. /* release force suspendm */
  219. USBPHY_CLR8(0x6a, 0x04);
  220. USBPHY_SET8(0x68, 0x04);
  221. /* RG_DPPULLDOWN./RG_DMPULLDOWN. */
  222. USBPHY_SET8(0x68, 0xc0);
  223. /* RG_XCVRSEL[1:0] = 2'b01 */
  224. USBPHY_CLR8(0x68, 0x30);
  225. USBPHY_SET8(0x68, 0x10);
  226. /* RG_TERMSEL = 1'b1 */
  227. USBPHY_SET8(0x68, 0x04);
  228. /* RG_DATAIN[3:0] = 4'b0000 */
  229. USBPHY_CLR8(0x69, 0x3c);
  230. /*
  231. * force_dp_pulldown, force_dm_pulldown,
  232. * force_xcversel, force_termsel.
  233. */
  234. USBPHY_SET8(0x6a, 0xba);
  235. /* RG_USB20_BC11_SW_EN = 1'b0 */
  236. USBPHY_CLR8(0x1a, 0x80);
  237. /* RG_USB20_OTG_VBUSSCMP_EN = 1'b0 */
  238. USBPHY_CLR8(0x1a, 0x10);
  239. udelay(800);
  240. USBPHY_CLR8(0x6a, 0x04);
  241. /* rg_usb20_pll_stable = 1 */
  242. //USBPHY_SET8(0x63, 0x02);
  243. udelay(1);
  244. /* force suspendm = 1 */
  245. //USBPHY_SET8(0x6a, 0x04);
  246. //udelay(1);
  247. return;
  248. }
  249. void mt_usb_phy_recover(void)
  250. {
  251. /* clean PUPD_BIST_EN */
  252. /* PUPD_BIST_EN = 1'b0 */
  253. /* PMIC will use it to detect charger type */
  254. USBPHY_CLR8(0x1d, 0x10);
  255. /* force_uart_en = 1'b0 */
  256. USBPHY_CLR8(0x6b, 0x04);
  257. /* RG_UART_EN = 1'b0 */
  258. USBPHY_CLR8(0x6e, 0x01);
  259. /* force_uart_en = 1'b0 */
  260. USBPHY_CLR8(0x6a, 0x04);
  261. USBPHY_CLR8(0x21, 0x03);
  262. USBPHY_CLR8(0x68, 0xf4);
  263. /* RG_DATAIN[3:0] = 4'b0000 */
  264. USBPHY_CLR8(0x69, 0x3c);
  265. USBPHY_CLR8(0x6a, 0xba);
  266. /* RG_USB20_BC11_SW_EN = 1'b0 */
  267. USBPHY_CLR8(0x1a, 0x80);
  268. /* RG_USB20_OTG_VBUSSCMP_EN = 1'b1 */
  269. USBPHY_SET8(0x1a, 0x10);
  270. //HQA adjustment
  271. USBPHY_CLR8(0x18, 0x08);
  272. USBPHY_SET8(0x18, 0x06);
  273. udelay(800);
  274. /* force enter device mode */
  275. //USBPHY_CLR8(0x6c, 0x10);
  276. //USBPHY_SET8(0x6c, 0x2E);
  277. //USBPHY_SET8(0x6d, 0x3E);
  278. }
  279. void mt_usb11_phy_savecurrent(void)
  280. {
  281. #if 0
  282. USB11PHY_SET8(0xca, 0x10);
  283. USB11PHY_SET8(0xcb, 0x3c);
  284. USB11PHY_CLR8(0xc1, 0x08);
  285. USB11PHY_CLR8(0xc7, 0x06);
  286. USB11PHY_SET8(0xc6, 0x06);
  287. #endif
  288. }
  289. void Charger_Detect_Init(void)
  290. {
  291. /* RG_USB20_BC11_SW_EN = 1'b1 */
  292. USBPHY_SET8(0x1a, 0x80);
  293. }
  294. void Charger_Detect_Release(void)
  295. {
  296. /* RG_USB20_BC11_SW_EN = 1'b0 */
  297. USBPHY_CLR8(0x1a, 0x80);
  298. }
  299. #endif
  300. /* usb phy bring up end */
  301. #if !CFG_FPGA_PLATFORM
  302. //ALPS00427972, implement the analog register formula
  303. void mt_usb_phy_calibraion (int case_set, int input_reg)
  304. {
  305. #if 0
  306. int temp_added=0;
  307. int temp_test=0;
  308. int temp_mask;
  309. #if DBG_PHY_CALIBRATION
  310. DBG_I("%s: case_set %d, input_reg = 0x%x \n", __func__, case_set, input_reg);
  311. #endif
  312. switch (case_set) {
  313. case 1:
  314. //case 1
  315. //If M_HW_RES3[15:13] !=0
  316. //RG_USB20_TERM_VREF_SEL[2:0] <= RG_USB20_TERM_VREF_SEL[2:0] + M_HW_RES3[15:13]
  317. temp_mask = 0x07;
  318. temp_test = USBPHY_READ8(0x05);
  319. #if DBG_PHY_CALIBRATION
  320. DBG_I("%s: temp_test = 0x%x \n", __func__, temp_test);
  321. #endif
  322. temp_added = (USBPHY_READ8(0x05)& temp_mask) + input_reg;
  323. #if DBG_PHY_CALIBRATION
  324. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  325. #endif
  326. temp_added &= 0x07;
  327. #if DBG_PHY_CALIBRATION
  328. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  329. #endif
  330. USBPHY_CLR8(0x05, temp_mask);
  331. USBPHY_SET8(0x05, temp_added);
  332. temp_test = USBPHY_READ8(0x05);
  333. #if DBG_PHY_CALIBRATION
  334. DBG_I("%s: final temp_test = 0x%x \n", __func__, temp_test);
  335. #endif
  336. break;
  337. case 2:
  338. //case 2
  339. //If M_HW_RES3[12:10] !=0
  340. //RG_USB20_CLKREF_REF[2:0]<= RG_USB20_CLKREF_REF[2:0]+ M_HW_RES3[12:10]
  341. temp_mask = 0x07;
  342. temp_test = USBPHY_READ8(0x07);
  343. #if DBG_PHY_CALIBRATION
  344. DBG_I("%s: temp_test = 0x%x \n", __func__, temp_test);
  345. #endif
  346. temp_added = (USBPHY_READ8(0x07)& temp_mask) + input_reg;
  347. #if DBG_PHY_CALIBRATION
  348. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  349. #endif
  350. temp_added &= 0x07;
  351. #if DBG_PHY_CALIBRATION
  352. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  353. #endif
  354. USBPHY_CLR8(0x07, temp_mask);
  355. USBPHY_SET8(0x07, temp_added);
  356. temp_test = USBPHY_READ8(0x07);
  357. #if DBG_PHY_CALIBRATION
  358. DBG_I("%s: final temp_test = 0x%x \n", __func__, temp_test);
  359. #endif
  360. break;
  361. case 3:
  362. //case 3
  363. //If M_HW_RES3[9:7] !=0
  364. //RG_USB20_VRT_VREF_SEL[2:0]<=RG_USB20_VRT_VREF_SEL[2:0]+ M_HW_RES3[9:7]
  365. temp_mask = 0x70;
  366. temp_test = USBPHY_READ8(0x05);
  367. #if DBG_PHY_CALIBRATION
  368. DBG_I("%s: temp_test = 0x%x \n", __func__, temp_test);
  369. #endif
  370. temp_added = (USBPHY_READ8(0x05)& temp_mask) >> 4;
  371. #if DBG_PHY_CALIBRATION
  372. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  373. #endif
  374. temp_added += input_reg;
  375. #if DBG_PHY_CALIBRATION
  376. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  377. #endif
  378. temp_added &= 0x07;
  379. #if DBG_PHY_CALIBRATION
  380. DBG_I("%s: temp_added = 0x%x \n", __func__, temp_added);
  381. #endif
  382. USBPHY_CLR8(0x05, temp_mask);
  383. USBPHY_SET8(0x05, temp_added<<4);
  384. temp_test = USBPHY_READ8(0x05);
  385. #if DBG_PHY_CALIBRATION
  386. DBG_I("%s: final temp_test = 0x%x \n", __func__, temp_test);
  387. #endif
  388. break;
  389. }
  390. #endif
  391. }
  392. //ALPS00427972, implement the analog register formula
  393. #endif