mt_ssusbphy.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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 <reg.h>
  24. #include <platform/bitops.h>
  25. #include <platform/mt_reg_base.h>
  26. #include <platform/mt_typedefs.h>
  27. #include <platform/timer.h>
  28. #include <mt_ssusb.h>
  29. #include <mt_ssusbphy.h>
  30. #include <mt_ssusb_sifslv_ippc.h>
  31. #if defined(CONFIG_D60802_SUPPORT)
  32. #include <mt_usbphy_d60802.h>
  33. #elif defined(CONFIG_E60802_SUPPORT)
  34. #include <mt_usbphy_e60802.h>
  35. #elif defined(CONFIG_A60810_SUPPORT)
  36. #include <mt_usbphy_a60810.h>
  37. #endif
  38. /* for usb phy */
  39. #include <mt_i2c.h>
  40. #include <mt_ssusb_usb3_mac_csr.h>
  41. #ifdef MACH_FPGA
  42. #define CFG_FPGA_PLATFORM (1)
  43. #else
  44. #define DBG_PHY_CALIBRATION 1
  45. #endif
  46. /* used by phy scan */
  47. #define CONFIG_U3_PHY_GPIO_SUPPORT
  48. #define USB11PHY_READ8(offset) readb(USB11_PHY_BASE + offset)
  49. #define USB11PHY_WRITE8(offset, value) writeb(value, USB11_PHY_BASE+offset)
  50. #define USB11PHY_SET8(offset, mask) USB11PHY_WRITE8(offset, USB11PHY_READ8(offset) | mask)
  51. #define USB11PHY_CLR8(offset, mask) USB11PHY_WRITE8(offset, USB11PHY_READ8(offset) & ~mask)
  52. #define SSUSB_PHY_BASE (SSUSB_SIFSLV_IPPC_BASE)
  53. //#define USB20_PHY_BASE (USBSIF_BASE + 0x0800)
  54. #define USB11_PHY_BASE (USBSIF_BASE + 0x0900)
  55. #define PERI_GLOBALCON_PDN0_SET (PERICFG_BASE+0x008)
  56. #define USB0_PDN 1 << 10
  57. extern void mu3d_hal_ssusb_en(void);
  58. extern void mu3d_hal_rst_dev(void);
  59. #define USB_I2C_ID I2C1 /* 0 - 6 */
  60. #define PATH_NORMAL 0
  61. #define PATH_PMIC 1
  62. #define U3_PHY_PAGE 0xff
  63. #define I2C_CHIP 0xc0
  64. #ifdef DBG_USB_PHY
  65. #define PHY_LOG(x...) dprintf(INFO, "[USB][PHY] " x)
  66. #else
  67. #define PHY_LOG(x...) do{} while(0)
  68. #endif
  69. #define ENTER_U0_TH 5
  70. #define MAX_PHASE_RANGE 31
  71. #define MAX_TIMEOUT_COUNT 100
  72. #define DATA_DRIVING_MASK 0x06
  73. #define MAX_DRIVING_RANGE 0x04
  74. #define MAX_LATCH_SELECT 0x02
  75. #define U3_PHY_I2C_PCLK_DRV_REG 0x0A
  76. #define U3_PHY_I2C_PCLK_PHASE_REG 0x0B
  77. #define STATE_U0_STATE (13)
  78. #define CLR_RECOV_CNT (0x1 << 16) /* 16:16 */
  79. #define CLR_LINK_ERR_CNT (0x1 << 16) /* 16:16 */
  80. #define STATE_DISABLE (1)
  81. #if CFG_FPGA_PLATFORM
  82. #define USE_USB_I2C
  83. #ifdef USE_USB_I2C
  84. /* TEST CHIP PHY define, edit this in different platform */
  85. #define U3_PHY_I2C_DEV 0x60
  86. #define U3_PHY_PAGE 0xff
  87. #define _GPIO_BASE USB3_SIF_BASE+0x700 //0xf0044700 //0x80080000
  88. #define SSUSB_I2C_OUT _GPIO_BASE+0xd0
  89. #define SSUSB_I2C_IN _GPIO_BASE+0xd4
  90. /////////////////////////////////////////////////////////////////
  91. #define OUTPUT 1
  92. #define INPUT 0
  93. #define SDA 0 /// GPIO #0: I2C data pin
  94. #define SCL 1 /// GPIO #1: I2C clock pin
  95. /////////////////////////////////////////////////////////////////
  96. #define SDA_OUT (1<<0)
  97. #define SDA_OEN (1<<1)
  98. #define SCL_OUT (1<<2)
  99. #define SCL_OEN (1<<3)
  100. #define SDA_IN_OFFSET 0
  101. #define SCL_IN_OFFSET 1
  102. //#define GPIO_PULLEN1_SET (GPIO_BASE+0x0030+0x04)
  103. //#define GPIO_DIR1_SET (GPIO_BASE+0x0000+0x04)
  104. //#define GPIO_PULLEN1_CLR (GPIO_BASE+0x0030+0x08)
  105. //#define GPIO_DIR1_CLR (GPIO_BASE+0x0000+0x08)
  106. //#define GPIO_DOUT1_SET (GPIO_BASE+0x00C0+0x04)
  107. //#define GPIO_DOUT1_CLR (GPIO_BASE+0x00C0+0x08)
  108. //#define GPIO_DIN1 (GPIO_BASE+0x00F0)
  109. void gpio_dir_set(u32 pin)
  110. {
  111. u32 temp;
  112. u64 addr;
  113. addr = SSUSB_I2C_OUT;
  114. temp = DRV_Reg32(addr);
  115. if (pin == SDA) {
  116. temp |= SDA_OEN;
  117. DRV_WriteReg32(addr,temp);
  118. } else {
  119. temp |= SCL_OEN;
  120. DRV_WriteReg32(addr,temp);
  121. }
  122. }
  123. void gpio_dir_clr(u32 pin)
  124. {
  125. u32 temp;
  126. u64 addr;
  127. addr = SSUSB_I2C_OUT;
  128. temp = DRV_Reg32(addr);
  129. if (pin == SDA) {
  130. temp &= ~SDA_OEN;
  131. DRV_WriteReg32(addr,temp);
  132. } else {
  133. temp &= ~SCL_OEN;
  134. DRV_WriteReg32(addr,temp);
  135. }
  136. }
  137. void gpio_dout_set(u32 pin)
  138. {
  139. u32 temp;
  140. u64 addr;
  141. addr = SSUSB_I2C_OUT;
  142. temp = DRV_Reg32(addr);
  143. if (pin == SDA) {
  144. temp |= SDA_OUT;
  145. DRV_WriteReg32(addr,temp);
  146. } else {
  147. temp |= SCL_OUT;
  148. DRV_WriteReg32(addr,temp);
  149. }
  150. }
  151. void gpio_dout_clr(u32 pin)
  152. {
  153. u32 temp;
  154. u64 addr;
  155. addr = SSUSB_I2C_OUT;
  156. temp = DRV_Reg32(addr);
  157. if (pin == SDA) {
  158. temp &= ~SDA_OUT;
  159. DRV_WriteReg32(addr,temp);
  160. } else {
  161. temp &= ~SCL_OUT;
  162. DRV_WriteReg32(addr,temp);
  163. }
  164. }
  165. u32 gpio_din(u32 pin)
  166. {
  167. u32 temp;
  168. u64 addr;
  169. addr = SSUSB_I2C_IN;
  170. temp = DRV_Reg32(addr);
  171. if (pin == SDA) {
  172. temp = (temp >> SDA_IN_OFFSET) & 1;
  173. } else {
  174. temp = (temp >> SCL_IN_OFFSET) & 1;
  175. }
  176. return temp;
  177. }
  178. //#define GPIO_PULLEN_SET(_no) (GPIO_PULLEN1_SET+(0x10*(_no)))
  179. #define GPIO_DIR_SET(pin) gpio_dir_set(pin)
  180. #define GPIO_DOUT_SET(pin) gpio_dout_set(pin);
  181. //#define GPIO_PULLEN_CLR(_no) (GPIO_PULLEN1_CLR+(0x10*(_no)))
  182. #define GPIO_DIR_CLR(pin) gpio_dir_clr(pin)
  183. #define GPIO_DOUT_CLR(pin) gpio_dout_clr(pin)
  184. #define GPIO_DIN(pin) gpio_din(pin)
  185. u32 i2c_dummy_cnt;
  186. #define I2C_DELAY 10
  187. #define I2C_DUMMY_DELAY(_delay) for (i2c_dummy_cnt = ((_delay)) ; i2c_dummy_cnt!=0; i2c_dummy_cnt--)
  188. void GPIO_InitIO(u32 dir, u32 pin)
  189. {
  190. if (dir == OUTPUT) {
  191. // DRV_WriteReg16(GPIO_PULLEN_SET(no),(1 << remainder));
  192. GPIO_DIR_SET(pin);
  193. } else {
  194. // DRV_WriteReg16(GPIO_PULLEN_CLR(no),(1 << remainder));
  195. GPIO_DIR_CLR(pin);
  196. }
  197. I2C_DUMMY_DELAY(100);
  198. }
  199. void GPIO_WriteIO(u32 data, u32 pin)
  200. {
  201. if (data == 1) {
  202. GPIO_DOUT_SET(pin);
  203. } else {
  204. GPIO_DOUT_CLR(pin);
  205. }
  206. }
  207. u32 GPIO_ReadIO( u32 pin)
  208. {
  209. u16 data;
  210. data=GPIO_DIN(pin);
  211. return (u32)data;
  212. }
  213. void SerialCommStop(void)
  214. {
  215. GPIO_InitIO(OUTPUT,SDA);
  216. GPIO_WriteIO(0,SCL);
  217. I2C_DUMMY_DELAY(I2C_DELAY);
  218. GPIO_WriteIO(0,SDA);
  219. I2C_DUMMY_DELAY(I2C_DELAY);
  220. GPIO_WriteIO(1,SCL);
  221. I2C_DUMMY_DELAY(I2C_DELAY);
  222. GPIO_WriteIO(1,SDA);
  223. I2C_DUMMY_DELAY(I2C_DELAY);
  224. GPIO_InitIO(INPUT,SCL);
  225. GPIO_InitIO(INPUT,SDA);
  226. }
  227. void SerialCommStart(void) /* Prepare the SDA and SCL for sending/receiving */
  228. {
  229. GPIO_InitIO(OUTPUT,SCL);
  230. GPIO_InitIO(OUTPUT,SDA);
  231. GPIO_WriteIO(1,SDA);
  232. I2C_DUMMY_DELAY(I2C_DELAY);
  233. GPIO_WriteIO(1,SCL);
  234. I2C_DUMMY_DELAY(I2C_DELAY);
  235. GPIO_WriteIO(0,SDA);
  236. I2C_DUMMY_DELAY(I2C_DELAY);
  237. GPIO_WriteIO(0,SCL);
  238. I2C_DUMMY_DELAY(I2C_DELAY);
  239. }
  240. u32 SerialCommTxByte(u8 data) /* return 0 --> ack */
  241. {
  242. u32 i, ack;
  243. GPIO_InitIO(OUTPUT,SDA);
  244. for (i=8; --i>0;) {
  245. GPIO_WriteIO((data>>i)&0x01, SDA);
  246. I2C_DUMMY_DELAY(I2C_DELAY);
  247. GPIO_WriteIO( 1, SCL); /* high */
  248. I2C_DUMMY_DELAY(I2C_DELAY);
  249. GPIO_WriteIO( 0, SCL); /* low */
  250. I2C_DUMMY_DELAY(I2C_DELAY);
  251. }
  252. GPIO_WriteIO((data>>i)&0x01, SDA);
  253. I2C_DUMMY_DELAY(I2C_DELAY);
  254. GPIO_WriteIO( 1, SCL); /* high */
  255. I2C_DUMMY_DELAY(I2C_DELAY);
  256. GPIO_WriteIO( 0, SCL); /* low */
  257. I2C_DUMMY_DELAY(I2C_DELAY);
  258. GPIO_WriteIO(0, SDA);
  259. I2C_DUMMY_DELAY(I2C_DELAY);
  260. GPIO_InitIO(INPUT,SDA);
  261. I2C_DUMMY_DELAY(I2C_DELAY);
  262. GPIO_WriteIO(1, SCL);
  263. I2C_DUMMY_DELAY(I2C_DELAY);
  264. ack = GPIO_ReadIO(SDA); /// ack 1: error , 0:ok
  265. GPIO_WriteIO(0, SCL);
  266. I2C_DUMMY_DELAY(I2C_DELAY);
  267. if (ack==1)
  268. return FALSE;
  269. else
  270. return TRUE;
  271. }
  272. void SerialCommRxByte(u8 *data, u8 ack)
  273. {
  274. S32 i;
  275. u32 dataCache;
  276. dataCache = 0;
  277. GPIO_InitIO(INPUT,SDA);
  278. for (i=8; --i>=0;) {
  279. dataCache <<= 1;
  280. I2C_DUMMY_DELAY(I2C_DELAY);
  281. GPIO_WriteIO(1, SCL);
  282. I2C_DUMMY_DELAY(I2C_DELAY);
  283. dataCache |= GPIO_ReadIO(SDA);
  284. GPIO_WriteIO(0, SCL);
  285. I2C_DUMMY_DELAY(I2C_DELAY);
  286. }
  287. GPIO_InitIO(OUTPUT,SDA);
  288. GPIO_WriteIO(ack, SDA);
  289. I2C_DUMMY_DELAY(I2C_DELAY);
  290. GPIO_WriteIO(1, SCL);
  291. I2C_DUMMY_DELAY(I2C_DELAY);
  292. GPIO_WriteIO(0, SCL);
  293. I2C_DUMMY_DELAY(I2C_DELAY);
  294. *data = (unsigned char)dataCache;
  295. }
  296. u32 I2cWriteReg(u8 dev_id, u8 Addr, u8 Data)
  297. {
  298. u32 acknowledge=0;
  299. SerialCommStart();
  300. acknowledge=SerialCommTxByte((dev_id<<1) & 0xff);
  301. if (acknowledge)
  302. acknowledge=SerialCommTxByte(Addr);
  303. else
  304. return FALSE;
  305. acknowledge=SerialCommTxByte(Data);
  306. if (acknowledge) {
  307. SerialCommStop();
  308. return FALSE;
  309. } else {
  310. return TRUE;
  311. }
  312. }
  313. u32 I2cReadReg(u8 dev_id, u8 Addr, u8 *Data)
  314. {
  315. u32 acknowledge=0;
  316. SerialCommStart();
  317. acknowledge=SerialCommTxByte((dev_id<<1) & 0xff);
  318. if (acknowledge)
  319. acknowledge=SerialCommTxByte(Addr);
  320. else
  321. return FALSE;
  322. SerialCommStart();
  323. acknowledge=SerialCommTxByte(((dev_id<<1) & 0xff) | 0x01);
  324. if (acknowledge)
  325. SerialCommRxByte(Data, 1); // ack 0: ok , 1 error
  326. else
  327. return FALSE;
  328. SerialCommStop();
  329. return acknowledge;
  330. }
  331. #endif
  332. static struct mt_i2c_t usb_i2c;
  333. #define U3_PHY_I2C_DEV 0x60
  334. static u32 usb_i2c_read8(u8 addr, u8 *databuffer)
  335. {
  336. #ifdef USE_USB_I2C
  337. u32 ret;
  338. ret = I2cReadReg(U3_PHY_I2C_DEV, addr, databuffer);
  339. return ret;
  340. #else
  341. u32 ret_code = I2C_OK;
  342. u16 len;
  343. *databuffer = addr;
  344. usb_i2c.id = USB_I2C_ID;
  345. /* Since i2c will left shift 1 bit, we need to set USB I2C address to 0x60 (0xC0>>1) */
  346. usb_i2c.addr = 0x60;
  347. usb_i2c.mode = ST_MODE;
  348. usb_i2c.speed = 100;
  349. len = 1;
  350. ret_code = i2c_write_read(&usb_i2c, databuffer, len, len);
  351. return ret_code;
  352. #endif
  353. }
  354. static u32 usb_i2c_write8(u8 addr, u8 value)
  355. {
  356. #ifdef USE_USB_I2C
  357. u32 ret;
  358. ret = I2cWriteReg(U3_PHY_I2C_DEV, addr, value);
  359. return ret;
  360. #else
  361. u32 ret_code = I2C_OK;
  362. u8 write_data[2];
  363. u16 len;
  364. write_data[0]= addr;
  365. write_data[1] = value;
  366. usb_i2c.id = USB_I2C_ID;
  367. /* Since i2c will left shift 1 bit, we need to set USB I2C address to 0x60 (0xC0>>1) */
  368. usb_i2c.addr = 0x60;
  369. usb_i2c.mode = ST_MODE;
  370. usb_i2c.speed = 100;
  371. len = 2;
  372. ret_code = i2c_write(&usb_i2c, write_data, len);
  373. return ret_code;
  374. #endif
  375. }
  376. static void _u3_write_bank(u32 value)
  377. {
  378. usb_i2c_write8((u8)U3_PHY_PAGE, (u8)value);
  379. }
  380. static u32 _u3_read_reg(u32 address)
  381. {
  382. u8 databuffer = 0;
  383. usb_i2c_read8((u8)address, &databuffer);
  384. return databuffer;
  385. }
  386. static void _u3_write_reg(u32 address, u32 value)
  387. {
  388. usb_i2c_write8((u8 )address, (u8 )value);
  389. }
  390. u32 u3_phy_read_reg32(u32 addr)
  391. {
  392. u32 bank;
  393. u32 addr8;
  394. u32 data;
  395. bank = (addr >> 16) & 0xff;
  396. addr8 = addr & 0xff;
  397. _u3_write_bank(bank);
  398. data = _u3_read_reg(addr8);
  399. data |= (_u3_read_reg(addr8 + 1) << 8);
  400. data |= (_u3_read_reg(addr8 + 2) << 16);
  401. data |= (_u3_read_reg(addr8 + 3) << 24);
  402. return data;
  403. }
  404. u32 u3_phy_write_reg32(u32 addr, u32 data)
  405. {
  406. u32 bank;
  407. u32 addr8;
  408. u32 data_0, data_1, data_2, data_3;
  409. bank = (addr >> 16) & 0xff;
  410. addr8 = addr & 0xff;
  411. data_0 = data & 0xff;
  412. data_1 = (data >> 8) & 0xff;
  413. data_2 = (data >> 16) & 0xff;
  414. data_3 = (data >> 24) & 0xff;
  415. _u3_write_bank(bank);
  416. _u3_write_reg(addr8, data_0);
  417. _u3_write_reg(addr8 + 1, data_1);
  418. _u3_write_reg(addr8 + 2, data_2);
  419. _u3_write_reg(addr8 + 3, data_3);
  420. return 0;
  421. }
  422. void u3_phy_write_field32(int addr, int offset, int mask, int value)
  423. {
  424. u32 cur_value;
  425. u32 new_value;
  426. cur_value = u3_phy_read_reg32(addr);
  427. new_value = (cur_value & (~mask)) | ((value << offset) & mask);
  428. //udelay(i2cdelayus);
  429. u3_phy_write_reg32(addr, new_value);
  430. }
  431. u32 u3_phy_write_reg8(u32 addr, u8 data)
  432. {
  433. u32 bank;
  434. u32 addr8;
  435. PHY_LOG("addr8: %x\n", addr);
  436. bank = (addr >> 16) & 0xff;
  437. addr8 = addr & 0xff;
  438. _u3_write_bank(bank);
  439. _u3_write_reg(addr8, data);
  440. return 0;
  441. }
  442. char u3_phy_read_reg8(u32 addr)
  443. {
  444. int bank;
  445. int addr8;
  446. int data;
  447. bank = (addr >> 16) & 0xff;
  448. addr8 = addr & 0xff;
  449. _u3_write_bank(bank);
  450. data = _u3_read_reg(addr8);
  451. return data;
  452. }
  453. static void mu3d_hal_pdn_dis(void)
  454. {
  455. clrbits(U3D_SSUSB_IP_PW_CTRL2, SSUSB_IP_DEV_PDN);
  456. #ifdef SUPPORT_U3
  457. clrbits(U3D_SSUSB_U3_CTRL_0P, (SSUSB_U3_PORT_DIS | SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_U2_CG_EN));
  458. #endif
  459. clrbits(U3D_SSUSB_U2_CTRL_0P, (SSUSB_U2_PORT_DIS | SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_U2_CG_EN));
  460. }
  461. int mu3d_hal_phy_scan(struct u3phy_info *u3phy, int latch_val)
  462. {
  463. #ifdef CONFIG_U3_PHY_GPIO_SUPPORT
  464. int count, fset_phase_val, u0_count;
  465. u8 phase_val, driving;
  466. //temp, data_driving_val, clk_driving_val;
  467. #ifdef DBG_USB_PHY
  468. int link_error_count, recov_cnt;
  469. #endif
  470. /* disable ip power down,disable U2/U3 ip power down. */
  471. mu3d_hal_ssusb_en();
  472. mu3d_hal_pdn_dis();
  473. #if defined(CONFIG_D60802_SUPPORT)
  474. phy_change_pipe_phase_d60802(u3phy, 0, 0);
  475. #elif defined(CONFIG_E60802_SUPPORT)
  476. phy_change_pipe_phase_e60802(u3phy, 0, 0);
  477. #elif defined(CONFIG_A60810_SUPPORT)
  478. phy_change_pipe_phase_a60810(u3phy, 0, 0);
  479. #endif
  480. writel(latch_val, U3D_PIPE_LATCH_SELECT); /* set tx/rx latch sel */
  481. driving = 2;
  482. #if defined(CONFIG_D60802_SUPPORT)
  483. phy_change_pipe_phase_d60802(u3phy, driving, driving);
  484. #elif defined(CONFIG_E60802_SUPPORT)
  485. phy_change_pipe_phase_e60802(u3phy, driving, driving);
  486. #elif defined(CONFIG_A60810_SUPPORT)
  487. phy_change_pipe_phase_a60810(u3phy, driving, driving);
  488. #endif
  489. phase_val = 0;
  490. count = 0;
  491. fset_phase_val = TRUE;
  492. while (TRUE) {
  493. if (fset_phase_val) {
  494. #if defined(CONFIG_D60802_SUPPORT)
  495. phy_change_pipe_phase_d60802(u3phy, driving, phase_val);
  496. #elif defined(CONFIG_E60802_SUPPORT)
  497. phy_change_pipe_phase_e60802(u3phy, driving, phase_val);
  498. #elif defined(CONFIG_A60810_SUPPORT)
  499. phy_change_pipe_phase_a60810(u3phy, driving, phase_val);
  500. #endif
  501. mu3d_hal_rst_dev();
  502. mdelay(50);
  503. writel(USB3_EN, U3D_USB3_CONFIG);
  504. writel(latch_val, U3D_PIPE_LATCH_SELECT); /* set tx/rx latch sel */
  505. fset_phase_val = FALSE;
  506. u0_count = 0;
  507. #ifdef DBG_USB_PHY
  508. link_error_count = 0;
  509. recov_cnt = 0;
  510. #endif
  511. count = 0;
  512. }
  513. mdelay(50);
  514. count++;
  515. #ifdef DBG_USB_PHY
  516. /* read U0 recovery count */
  517. recov_cnt = readl(U3D_RECOVERY_COUNT);
  518. /* read link error count */
  519. link_error_count = readl(U3D_LINK_ERR_COUNT);
  520. #endif
  521. /* enter U0 state */
  522. if ((readl(U3D_LINK_STATE_MACHINE) & LTSSM) == STATE_U0_STATE) {
  523. u0_count++;
  524. }
  525. /* link up */
  526. if (u0_count > ENTER_U0_TH) {
  527. mdelay(1000);//1s
  528. #ifdef DBG_USB_PHY
  529. recov_cnt = readl(U3D_RECOVERY_COUNT);
  530. link_error_count = readl(U3D_LINK_ERR_COUNT);
  531. #endif
  532. writel(CLR_RECOV_CNT, U3D_RECOVERY_COUNT); /* clear recovery count */
  533. writel(CLR_LINK_ERR_CNT, U3D_LINK_ERR_COUNT); /* clear link error count */
  534. #ifdef DBG_USB_PHY
  535. PHY_LOG("[PASS] Link Error Count=%d, Recovery Count=%d, I2C(0x%x) : [0x%x], I2C(0x%x) : [0x%x], Reg(0x130) : [0x%x], PhaseDelay[0x%x], Driving[0x%x], Latch[0x%x]\n",
  536. link_error_count, recov_cnt,
  537. U3_PHY_I2C_PCLK_DRV_REG, _u3_read_reg(U3_PHY_I2C_PCLK_DRV_REG),
  538. U3_PHY_I2C_PCLK_PHASE_REG, _u3_read_reg(U3_PHY_I2C_PCLK_PHASE_REG),
  539. readl(U3D_PIPE_LATCH_SELECT),
  540. phase_val, driving, latch_val);
  541. #endif
  542. phase_val++;
  543. fset_phase_val = TRUE;
  544. } else if ((readl(U3D_LINK_STATE_MACHINE) & LTSSM) == STATE_DISABLE) { /* link fail */
  545. PHY_LOG("[FAIL] STATE_DISABLE, PhaseDelay[0x%x]\n", phase_val);
  546. phase_val++;
  547. fset_phase_val=TRUE;
  548. } else if (count > MAX_TIMEOUT_COUNT) { /* link timeout */
  549. PHY_LOG("[FAIL] TIMEOUT, PhaseDelay[0x%x]\n", phase_val);
  550. phase_val++;
  551. fset_phase_val = TRUE;
  552. }
  553. if (phase_val > MAX_PHASE_RANGE) {
  554. /* reset device */
  555. mu3d_hal_rst_dev();
  556. mdelay(50);
  557. /* disable ip power down, disable U2/U3 ip power down. */
  558. mu3d_hal_ssusb_en();
  559. mu3d_hal_pdn_dis();
  560. mdelay(10);
  561. break;
  562. }
  563. }
  564. #endif /* CONFIG_U3_PHY_GPIO_SUPPORT */
  565. return 0;
  566. }
  567. void mt_usb_phy_poweron(void)
  568. {
  569. static struct u3phy_info info;
  570. #ifdef DBG_USB_PHY
  571. volatile u32 u3phy_version;
  572. #endif
  573. info.phyd_version_addr = 0x2000e4;
  574. #ifdef DBG_USB_PHY
  575. u3phy_version = u3_phy_read_reg32(info.phyd_version_addr);
  576. PHY_LOG("[USBPHY] Phy version is %x\n", u3phy_version);
  577. #endif
  578. #if defined(CONFIG_D60802_SUPPORT)
  579. info.u2phy_regs_d = (struct u2phy_reg_d *)0x0;
  580. info.u3phyd_regs_d = (struct u3phyd_reg_d *)0x100000;
  581. info.u3phyd_bank2_regs_d = (struct u3phyd_bank2_reg_d *)0x200000;
  582. info.u3phya_regs_d = (struct u3phya_reg_d *)0x300000;
  583. info.u3phya_da_regs_d = (struct u3phya_da_reg_d *)0x400000;
  584. info.sifslv_chip_regs_d = (struct sifslv_chip_reg_d *)0x500000;
  585. info.sifslv_fm_regs_d = (struct sifslv_fm_feg_d *)0xf00000;
  586. phy_init_d60802(&info);
  587. #elif defined(CONFIG_E60802_SUPPORT)
  588. info.u2phy_regs_e = (struct u2phy_reg_e *)0x0;
  589. info.u3phyd_regs_e = (struct u3phyd_reg_e *)0x100000;
  590. info.u3phyd_bank2_regs_e = (struct u3phyd_bank2_reg_e *)0x200000;
  591. info.u3phya_regs_e = (struct u3phya_reg_e *)0x300000;
  592. info.u3phya_da_regs_e = (struct u3phya_da_reg_e *)0x400000;
  593. info.sifslv_chip_regs_e = (struct sifslv_chip_reg_e *)0x500000;
  594. info.sifslv_fm_regs_e = (struct sifslv_fm_feg_e *)0xf00000;
  595. phy_init_e60802(&info);
  596. #elif defined(CONFIG_A60810_SUPPORT)
  597. info.u2phy_regs_a = (struct u2phy_reg_a *)0x0;
  598. info.u3phyd_regs_a = (struct u3phyd_reg_a *)0x100000;
  599. info.u3phyd_bank2_regs_a = (struct u3phyd_bank2_reg_a *)0x200000;
  600. info.u3phya_regs_a = (struct u3phya_reg_a *)0x300000;
  601. info.u3phya_da_regs_a = (struct u3phya_da_reg_a *)0x400000;
  602. info.sifslv_chip_regs_a = (struct sifslv_chip_reg_a *)0x500000;
  603. info.spllc_regs_a = (struct spllc_reg_a *)0x600000;
  604. info.sifslv_fm_regs_a = (struct sifslv_fm_feg_a *)0xf00000;
  605. phy_init_a60810(&info);
  606. #endif
  607. /* for RF desense */
  608. #if defined(CONFIG_D60802_SUPPORT)
  609. //u2_slew_rate_calibration_d60802(&info);
  610. #elif defined(CONFIG_E60802_SUPPORT)
  611. //u2_slew_rate_calibration_e60802(&info);
  612. #elif defined(CONFIG_A60810_SUPPORT)
  613. //u2_slew_rate_calibration_a60810(&info);
  614. #endif
  615. mu3d_hal_ssusb_en();
  616. mu3d_hal_rst_dev();
  617. }
  618. void mt_usb_phy_savecurrent(void)
  619. {
  620. }
  621. void mt_usb_phy_recover(void)
  622. {
  623. }
  624. void mt_usb11_phy_savecurrent(void)
  625. {
  626. }
  627. void Charger_Detect_Init(void)
  628. {
  629. /* no need */
  630. }
  631. void Charger_Detect_Release(void)
  632. {
  633. /* no need */
  634. }
  635. #else
  636. #include <platform/project.h>
  637. void switch_2_usb()
  638. {
  639. clrbits(U3D_U2PHYDTM0, A60810_FORCE_UART_EN);
  640. clrbits(U3D_U2PHYDTM1, A60810_RG_UART_EN);
  641. clrbits(U3D_U2PHYACR4, A60810_RG_USB20_GPIO_CTL);
  642. clrbits(U3D_U2PHYACR4, A60810_USB20_GPIO_MODE);
  643. }
  644. void mt_usb_phy_poweron(void)
  645. {
  646. /* 5, switch to USB function */
  647. switch_2_usb();
  648. /* 6, DP/DM BC1.1 path Disable */
  649. clrbits(U3D_USBPHYACR6, A60810_RG_USB20_BC11_SW_EN);
  650. /* 7, dp_100k diable */
  651. clrbits(U3D_USBPHYACR4, A60810_USB20_DP_100K_EN);
  652. /* 8, rg_usb20_dp_100k_mode, 1'b1 */
  653. setbits(U3D_USBPHYACR4, A60810_RG_USB20_DP_100K_MODE);
  654. /* 9, dm_100k disable */
  655. clrbits(U3D_USBPHYACR4, A60810_RG_USB20_DM_100K_EN);
  656. /* 10, OTG enable */
  657. setbits(U3D_USBPHYACR6, A60810_RG_USB20_OTG_VBUSCMP_EN);
  658. /* 11, Release force suspendm */
  659. clrbits(U3D_U2PHYDTM0, A60810_FORCE_SUSPENDM);
  660. /* 12, Pre-emphasis level1*/
  661. setbits(U3D_USBPHYACR6, (0x1000000 << 24));
  662. /* Wait 800 usec */
  663. udelay(800);
  664. mu3d_hal_ssusb_en();
  665. }
  666. void mt_usb_phy_savecurrent(void)
  667. {
  668. /* 1, switch to usb function */
  669. switch_2_usb();
  670. /* 2, DP/DM BC1.1 path Disable */
  671. clrbits(U3D_USBPHYACR6, A60810_RG_USB20_BC11_SW_EN);
  672. /* 3, OTG disable */
  673. clrbits(U3D_USBPHYACR6, A60810_RG_USB20_OTG_VBUSCMP_EN);
  674. /* 4, let syspendm=1 */
  675. setbits(U3D_U2PHYDTM0, A60810_RG_SUSPENDM);
  676. /* 5, force_suspendm */
  677. setbits(U3D_U2PHYDTM0, A60810_FORCE_SUSPENDM);
  678. /* 6 wait for USBPLL stable */
  679. mdelay(2);
  680. /* 7 */
  681. setbits(U3D_U2PHYDTM0, A60810_RG_DPPULLDOWN);
  682. /* 8 */
  683. setbits(U3D_U2PHYDTM0, A60810_RG_DMPULLDOWN);
  684. /* 9 */
  685. writel((readl(U3D_U2PHYDTM0) & ~A60810_RG_XCVRSEL) | (0x1 << A60810_RG_XCVRSEL_OFST), U3D_U2PHYDTM0);
  686. /* 10 */
  687. setbits(U3D_U2PHYDTM0, A60810_RG_TERMSEL);
  688. /* 11 */
  689. clrbits(U3D_U2PHYDTM0, A60810_RG_DATAIN);
  690. /* 12 */
  691. setbits(U3D_U2PHYDTM0, A60810_FORCE_DP_PULLDOWN);
  692. /* 13 */
  693. setbits(U3D_U2PHYDTM0, A60810_FORCE_DM_PULLDOWN);
  694. /* 14 */
  695. setbits(U3D_U2PHYDTM0, A60810_FORCE_XCVRSEL);
  696. /* 15 */
  697. setbits(U3D_U2PHYDTM0, A60810_FORCE_TERMSEL);
  698. /* 16 */
  699. setbits(U3D_U2PHYDTM0, A60810_FORCE_DATAIN);
  700. /* 17 */
  701. udelay(800);
  702. /* 18 */
  703. clrbits(U3D_U2PHYDTM0, A60810_RG_SUSPENDM);
  704. /* 19 wait 1us */
  705. udelay(1);
  706. }
  707. void setting_ref_clk_e60802(void)
  708. {
  709. u32 val;
  710. /*
  711. * Reference clock use digital P&R, instead of analog path
  712. * possibly more interference
  713. * ---------REG Name---------- | -----Modification---| -----Description----
  714. * DA_SSUSB_XTAL_EXT_EN[1:0] | 2'b01-->2'b10 | 0x11290c00 bit[11:10]
  715. * DA_SSUSB_XTAL_RX_PWD[9:9] | -->1'b1 | 0x11280018 bit[9]
  716. */
  717. //USB_WRITE_FIELD32(U3D_U3PHYA_DA_REG0, A60810_RG_SSUSB_XTAL_EXT_EN_U3_OFST, A60810_RG_SSUSB_XTAL_EXT_EN_U3, 0x2);
  718. //val = readl(U3D_U3PHYA_DA_REG0) & ~A60810_RG_SSUSB_XTAL_EXT_EN_U3;
  719. val = readl(U3D_U3PHYA_DA_REG0) & ~A60810_RG_SSUSB_XTAL_EXT_EN_U3;
  720. val |= ((0x2) << A60810_RG_SSUSB_XTAL_EXT_EN_U3_OFST) & A60810_RG_SSUSB_XTAL_EXT_EN_U3;
  721. writel(val, U3D_U3PHYA_DA_REG0);
  722. //USB_WRITE_FIELD32(U3D_SPLLC_XTALCTL3, A60810_RG_SSUSB_XTAL_RX_PWD_OFST, A60810_RG_SSUSB_XTAL_RX_PWD, 0x01);
  723. val = readl(U3D_SPLLC_XTALCTL3) & ~A60810_RG_SSUSB_XTAL_RX_PWD;
  724. val |= ((0x01) << A60810_RG_SSUSB_XTAL_RX_PWD_OFST) & A60810_RG_SSUSB_XTAL_RX_PWD;
  725. writel(val, U3D_SPLLC_XTALCTL3);
  726. /*
  727. * ---------REG Name---------- | -----Modification---| -----Description----
  728. * 1 RG_SSUSB_TX_EIDLE_CM[3:0] | 1100-->1110 | low-power E-idle common mode(650mV to 600mV)
  729. * | | - 0x11290b18 bit [31:28]
  730. * 2 RG_SSUSB_CDR_BIR_LTD0[4:0]| 5'b01000-->5'b01100 | Increase BW - 0x1128095c bit [12:8]
  731. * 3 RG_XXX_CDR_BIR_LTD1[4:0] | 5'b00010-->5'b00011 | Increase BW - 0x1128095c bit [28:24]
  732. */
  733. //USB_WRITE_FIELD32(U3D_USB30_PHYA_REG6, RG_SSUSB_TX_EIDLE_CM_OFST, RG_SSUSB_TX_EIDLE_CM, 0xE);
  734. val = readl(U3D_USB30_PHYA_REG6) & ~A60810_RG_SSUSB_TX_EIDLE_CM;
  735. val |= ((0xE) << A60810_RG_SSUSB_TX_EIDLE_CM_OFST) & A60810_RG_SSUSB_TX_EIDLE_CM;
  736. writel(val, U3D_USB30_PHYA_REG6);
  737. //USB_WRITE_FIELD32(U3D_PHYD_CDR1, RG_SSUSB_CDR_BIR_LTD0_OFST, RG_SSUSB_CDR_BIR_LTD0, 0xC);
  738. val = readl(U3D_PHYD_CDR1) & ~A60810_RG_SSUSB_CDR_BIR_LTD0;
  739. val |= ((0xC) << A60810_RG_SSUSB_CDR_BIR_LTD0_OFST) & A60810_RG_SSUSB_CDR_BIR_LTD0;
  740. writel(val, U3D_PHYD_CDR1);
  741. //USB_WRITE_FIELD32(U3D_PHYD_CDR1, RG_SSUSB_CDR_BIR_LTD1_OFST, RG_SSUSB_CDR_BIR_LTD1, 0x3);
  742. val = readl(U3D_PHYD_CDR1) & ~A60810_RG_SSUSB_CDR_BIR_LTD1;
  743. val |= ((0x3) << A60810_RG_SSUSB_CDR_BIR_LTD1_OFST) & A60810_RG_SSUSB_CDR_BIR_LTD1;
  744. writel(val, U3D_PHYD_CDR1);
  745. }
  746. #if 0
  747. void setting_ref_clk_d60802(void)
  748. {
  749. u32 val;
  750. /*
  751. * Reference clock use digital P&R, instead of analog path
  752. * possibly more interference
  753. * ---------REG Name---------- | -----Modification---| -----Description----
  754. * DA_SSUSB_XTAL_EXT_EN[1:0] | 2'b01-->2'b10 | 0x11290c00 bit[11:10]
  755. * DA_SSUSB_XTAL_RX_PWD[9:9] | -->1'b1 | 0x11280018 bit[9]
  756. */
  757. /* D60802 */
  758. //USB_WRITE_FIELD32(U3D_U3PHYA_DA_REG0, D60802_RG_SSUSB_XTAL_EXT_EN_U3_OFST, D60802_RG_SSUSB_XTAL_EXT_EN_U3, 0x2);
  759. //val = readl(U3D_U3PHYA_DA_REG0) & ~D60802_RG_SSUSB_XTAL_EXT_EN_U3;
  760. val = readl(U3D_U3PHYA_DA_REG0) & ~D60802_REG0_FLD_RG_SSUSB_XTAL_EXT_EN_U3;
  761. val |= ((0x2) << D60802_REG0_FLD_RG_SSUSB_XTAL_EXT_EN_U3_OFST) & D60802_REG0_FLD_RG_SSUSB_XTAL_EXT_EN_U3;
  762. writel(val, U3D_U3PHYA_DA_REG0);
  763. /* E60802 only */
  764. #if 0
  765. USB_WRITE_FIELD32(U3D_SPLLC_XTALCTL3, D60802_RG_SSUSB_XTAL_RX_PWD_OFST, D60802_RG_SSUSB_XTAL_RX_PWD, 0x01);
  766. val = readl(U3D_SPLLC_XTALCTL3) & ~D60802_RG_SSUSB_XTAL_RX_PWD;
  767. val |= ((0x01) << D60802_RG_SSUSB_XTAL_RX_PWD_OFST) & D60802_RG_SSUSB_XTAL_RX_PWD;
  768. writel(val, U3D_SPLLC_XTALCTL3);
  769. #endif
  770. /*
  771. * ---------REG Name---------- | -----Modification---| -----Description----
  772. * 1 RG_SSUSB_TX_EIDLE_CM[3:0] | 1100-->1110 | low-power E-idle common mode(650mV to 600mV)
  773. * | | - 0x11290b18 bit [31:28]
  774. * 2 RG_SSUSB_CDR_BIR_LTD0[4:0]| 5'b01000-->5'b01100 | Increase BW - 0x1128095c bit [12:8]
  775. * 3 RG_XXX_CDR_BIR_LTD1[4:0] | 5'b00010-->5'b00011 | Increase BW - 0x1128095c bit [28:24]
  776. */
  777. // U3D_USB30_PHYA_REG6
  778. //USB_WRITE_FIELD32(U3D_USB30_PHYA_REG6, RG_SSUSB_TX_EIDLE_CM_OFST, RG_SSUSB_TX_EIDLE_CM, 0xE);
  779. val = readl(U3D_USB30_PHYA_REG6) & ~D60802_REG6_FLD_RG_SSUSB_TX_EIDLE_CM;
  780. val |= ((0xE) << D60802_REG6_FLD_RG_SSUSB_TX_EIDLE_CM_OFST) & D60802_REG6_FLD_RG_SSUSB_TX_EIDLE_CM;
  781. writel(val, U3D_USB30_PHYA_REG6);
  782. // U3D_PHYD_CDR1
  783. //USB_WRITE_FIELD32(U3D_PHYD_CDR1, RG_SSUSB_CDR_BIR_LTD0_OFST, RG_SSUSB_CDR_BIR_LTD0, 0xC);
  784. val = readl(U3D_PHYD_CDR1) & ~D60802_RG_SSUSB_CDR_BIR_LTD0;
  785. val |= ((0xC) << D60802_RG_SSUSB_CDR_BIR_LTD0_OFST) & D60802_RG_SSUSB_CDR_BIR_LTD0;
  786. writel(val, U3D_PHYD_CDR1);
  787. //USB_WRITE_FIELD32(U3D_PHYD_CDR1, RG_SSUSB_CDR_BIR_LTD1_OFST, RG_SSUSB_CDR_BIR_LTD1, 0x3);
  788. val = readl(U3D_PHYD_CDR1) & ~D60802_RG_SSUSB_CDR_BIR_LTD1;
  789. val |= ((0x3) << D60802_RG_SSUSB_CDR_BIR_LTD1_OFST) & D60802_RG_SSUSB_CDR_BIR_LTD1;
  790. writel(val, U3D_PHYD_CDR1);
  791. }
  792. #endif
  793. void mt_usb_phy_recover(void)
  794. {
  795. /* 4, switch to usb function */
  796. switch_2_usb();
  797. /* 5, force_suspendm */
  798. clrbits(U3D_U2PHYDTM0, A60810_FORCE_SUSPENDM);
  799. /* 6 */
  800. clrbits(U3D_U2PHYDTM0, A60810_RG_DPPULLDOWN);
  801. /* 7,*/
  802. clrbits(U3D_U2PHYDTM0, A60810_RG_DMPULLDOWN);
  803. /* 8 */
  804. clrbits(U3D_U2PHYDTM0, A60810_RG_XCVRSEL);
  805. /* 9 */
  806. clrbits(U3D_U2PHYDTM0, A60810_FORCE_TERMSEL);
  807. /* 10 */
  808. clrbits(U3D_U2PHYDTM0, A60810_RG_DATAIN);
  809. /* 11 */
  810. clrbits(U3D_U2PHYDTM0, A60810_FORCE_DP_PULLDOWN);
  811. /* 12 */
  812. clrbits(U3D_U2PHYDTM0, A60810_FORCE_DM_PULLDOWN);
  813. /* 13 */
  814. clrbits(U3D_U2PHYDTM0, A60810_FORCE_XCVRSEL);
  815. /* 14 */
  816. clrbits(U3D_U2PHYDTM0, A60810_FORCE_TERMSEL);
  817. /* 15 */
  818. clrbits(U3D_U2PHYDTM0, A60810_FORCE_DATAIN);
  819. /* 16, DP/DM BC1.1 path Disable */
  820. clrbits(U3D_USBPHYACR6, A60810_RG_USB20_BC11_SW_EN);
  821. /* 17, OTG enable */
  822. setbits(U3D_USBPHYACR6, A60810_RG_USB20_OTG_VBUSCMP_EN);
  823. /* 18, Pre-emphasis level1*/
  824. setbits(U3D_USBPHYACR6, (0x1000000 << 24));
  825. /* 19, wait 800us */
  826. udelay(800);
  827. }
  828. #endif