inca-ip_sw.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * INCA-IP internal switch ethernet driver.
  3. *
  4. * (C) Copyright 2003
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI) \
  27. && defined(CONFIG_INCA_IP_SWITCH)
  28. #include <malloc.h>
  29. #include <net.h>
  30. #include <asm/inca-ip.h>
  31. #include <asm/addrspace.h>
  32. #define NUM_RX_DESC PKTBUFSRX
  33. #define NUM_TX_DESC 3
  34. #define TOUT_LOOP 1000000
  35. #define DELAY udelay(10000)
  36. #define DMA_WRITE_REG(reg, value) *((volatile u32 *)reg) = (u32)value;
  37. #define DMA_READ_REG(reg, value) value = (u32)*((volatile u32*)reg)
  38. #define SW_WRITE_REG(reg, value) \
  39. *((volatile u32*)reg) = (u32)value;\
  40. DELAY;\
  41. *((volatile u32*)reg) = (u32)value;
  42. #define SW_READ_REG(reg, value) \
  43. value = (u32)*((volatile u32*)reg);\
  44. DELAY;\
  45. value = (u32)*((volatile u32*)reg);
  46. #define INCA_DMA_TX_POLLING_TIME 0x07
  47. #define INCA_DMA_RX_POLLING_TIME 0x07
  48. #define INCA_DMA_TX_HOLD 0x80000000
  49. #define INCA_DMA_TX_EOP 0x40000000
  50. #define INCA_DMA_TX_SOP 0x20000000
  51. #define INCA_DMA_TX_ICPT 0x10000000
  52. #define INCA_DMA_TX_IEOP 0x08000000
  53. #define INCA_DMA_RX_C 0x80000000
  54. #define INCA_DMA_RX_SOP 0x40000000
  55. #define INCA_DMA_RX_EOP 0x20000000
  56. /************************ Auto MDIX settings ************************/
  57. #define INCA_IP_AUTO_MDIX_LAN_PORTS_DIR INCA_IP_Ports_P1_DIR
  58. #define INCA_IP_AUTO_MDIX_LAN_PORTS_ALTSEL INCA_IP_Ports_P1_ALTSEL
  59. #define INCA_IP_AUTO_MDIX_LAN_PORTS_OUT INCA_IP_Ports_P1_OUT
  60. #define INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX 16
  61. #define WAIT_SIGNAL_RETRIES 100
  62. #define WAIT_LINK_RETRIES 100
  63. #define LINK_RETRY_DELAY 300 /* ms */
  64. /********************************************************************/
  65. typedef struct
  66. {
  67. union {
  68. struct {
  69. volatile u32 HOLD :1;
  70. volatile u32 ICpt :1;
  71. volatile u32 IEop :1;
  72. volatile u32 offset :3;
  73. volatile u32 reserved0 :4;
  74. volatile u32 NFB :22;
  75. }field;
  76. volatile u32 word;
  77. }params;
  78. volatile u32 nextRxDescPtr;
  79. volatile u32 RxDataPtr;
  80. union {
  81. struct {
  82. volatile u32 C :1;
  83. volatile u32 Sop :1;
  84. volatile u32 Eop :1;
  85. volatile u32 reserved3 :12;
  86. volatile u32 NBT :17;
  87. }field;
  88. volatile u32 word;
  89. }status;
  90. } inca_rx_descriptor_t;
  91. typedef struct
  92. {
  93. union {
  94. struct {
  95. volatile u32 HOLD :1;
  96. volatile u32 Eop :1;
  97. volatile u32 Sop :1;
  98. volatile u32 ICpt :1;
  99. volatile u32 IEop :1;
  100. volatile u32 reserved0 :5;
  101. volatile u32 NBA :22;
  102. }field;
  103. volatile u32 word;
  104. }params;
  105. volatile u32 nextTxDescPtr;
  106. volatile u32 TxDataPtr;
  107. volatile u32 C :1;
  108. volatile u32 reserved3 :31;
  109. } inca_tx_descriptor_t;
  110. static inca_rx_descriptor_t rx_ring[NUM_RX_DESC] __attribute__ ((aligned(16)));
  111. static inca_tx_descriptor_t tx_ring[NUM_TX_DESC] __attribute__ ((aligned(16)));
  112. static int tx_new, rx_new, tx_hold, rx_hold;
  113. static int tx_old_hold = -1;
  114. static int initialized = 0;
  115. static int inca_switch_init(struct eth_device *dev, bd_t * bis);
  116. static int inca_switch_send(struct eth_device *dev, volatile void *packet,
  117. int length);
  118. static int inca_switch_recv(struct eth_device *dev);
  119. static void inca_switch_halt(struct eth_device *dev);
  120. static void inca_init_switch_chip(void);
  121. static void inca_dma_init(void);
  122. static int inca_amdix(void);
  123. int inca_switch_initialize(bd_t * bis)
  124. {
  125. struct eth_device *dev;
  126. #if 0
  127. printf("Entered inca_switch_initialize()\n");
  128. #endif
  129. if (!(dev = (struct eth_device *) malloc (sizeof *dev))) {
  130. printf("Failed to allocate memory\n");
  131. return 0;
  132. }
  133. memset(dev, 0, sizeof(*dev));
  134. inca_dma_init();
  135. inca_init_switch_chip();
  136. #if defined(CONFIG_INCA_IP_SWITCH_AMDIX)
  137. inca_amdix();
  138. #endif
  139. sprintf(dev->name, "INCA-IP Switch");
  140. dev->init = inca_switch_init;
  141. dev->halt = inca_switch_halt;
  142. dev->send = inca_switch_send;
  143. dev->recv = inca_switch_recv;
  144. eth_register(dev);
  145. #if 0
  146. printf("Leaving inca_switch_initialize()\n");
  147. #endif
  148. return 1;
  149. }
  150. static int inca_switch_init(struct eth_device *dev, bd_t * bis)
  151. {
  152. int i;
  153. u32 v, regValue;
  154. u16 wTmp;
  155. #if 0
  156. printf("Entering inca_switch_init()\n");
  157. #endif
  158. /* Set MAC address.
  159. */
  160. wTmp = (u16)dev->enetaddr[0];
  161. regValue = (wTmp << 8) | dev->enetaddr[1];
  162. SW_WRITE_REG(INCA_IP_Switch_PMAC_SA1, regValue);
  163. wTmp = (u16)dev->enetaddr[2];
  164. regValue = (wTmp << 8) | dev->enetaddr[3];
  165. regValue = regValue << 16;
  166. wTmp = (u16)dev->enetaddr[4];
  167. regValue |= (wTmp<<8) | dev->enetaddr[5];
  168. SW_WRITE_REG(INCA_IP_Switch_PMAC_SA2, regValue);
  169. /* Initialize the descriptor rings.
  170. */
  171. for (i = 0; i < NUM_RX_DESC; i++)
  172. {
  173. inca_rx_descriptor_t * rx_desc = KSEG1ADDR(&rx_ring[i]);
  174. memset(rx_desc, 0, sizeof(rx_ring[i]));
  175. /* Set maximum size of receive buffer.
  176. */
  177. rx_desc->params.field.NFB = PKTSIZE_ALIGN;
  178. /* Set the offset of the receive buffer. Zero means
  179. * that the offset mechanism is not used.
  180. */
  181. rx_desc->params.field.offset = 0;
  182. /* Check if it is the last descriptor.
  183. */
  184. if (i == (NUM_RX_DESC - 1)) {
  185. /* Let the last descriptor point to the first
  186. * one.
  187. */
  188. rx_desc->nextRxDescPtr = KSEG1ADDR((u32)rx_ring);
  189. } else {
  190. /* Set the address of the next descriptor.
  191. */
  192. rx_desc->nextRxDescPtr = (u32)KSEG1ADDR(&rx_ring[i+1]);
  193. }
  194. rx_desc->RxDataPtr = (u32)KSEG1ADDR(NetRxPackets[i]);
  195. }
  196. #if 0
  197. printf("rx_ring = 0x%08X 0x%08X\n", (u32)rx_ring, (u32)&rx_ring[0]);
  198. printf("tx_ring = 0x%08X 0x%08X\n", (u32)tx_ring, (u32)&tx_ring[0]);
  199. #endif
  200. for (i = 0; i < NUM_TX_DESC; i++) {
  201. inca_tx_descriptor_t * tx_desc = KSEG1ADDR(&tx_ring[i]);
  202. memset(tx_desc, 0, sizeof(tx_ring[i]));
  203. tx_desc->params.word = 0;
  204. tx_desc->params.field.HOLD = 1;
  205. tx_desc->C = 1;
  206. /* Check if it is the last descriptor.
  207. */
  208. if (i == (NUM_TX_DESC - 1)) {
  209. /* Let the last descriptor point to the
  210. * first one.
  211. */
  212. tx_desc->nextTxDescPtr = KSEG1ADDR((u32)tx_ring);
  213. } else {
  214. /* Set the address of the next descriptor.
  215. */
  216. tx_desc->nextTxDescPtr = (u32)KSEG1ADDR(&tx_ring[i+1]);
  217. }
  218. }
  219. /* Initialize RxDMA.
  220. */
  221. DMA_READ_REG(INCA_IP_DMA_DMA_RXISR, v);
  222. #if 0
  223. printf("RX status = 0x%08X\n", v);
  224. #endif
  225. /* Writing to the FRDA of CHANNEL.
  226. */
  227. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXFRDA0, (u32)rx_ring);
  228. /* Writing to the COMMAND REG.
  229. */
  230. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0,
  231. INCA_IP_DMA_DMA_RXCCR0_INIT);
  232. /* Initialize TxDMA.
  233. */
  234. DMA_READ_REG(INCA_IP_DMA_DMA_TXISR, v);
  235. #if 0
  236. printf("TX status = 0x%08X\n", v);
  237. #endif
  238. /* Writing to the FRDA of CHANNEL.
  239. */
  240. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXFRDA0, (u32)tx_ring);
  241. tx_new = rx_new = 0;
  242. tx_hold = NUM_TX_DESC - 1;
  243. rx_hold = NUM_RX_DESC - 1;
  244. #if 0
  245. rx_ring[rx_hold].params.field.HOLD = 1;
  246. #endif
  247. /* enable spanning tree forwarding, enable the CPU port */
  248. /* ST_PT:
  249. * CPS (CPU port status) 0x3 (forwarding)
  250. * LPS (LAN port status) 0x3 (forwarding)
  251. * PPS (PC port status) 0x3 (forwarding)
  252. */
  253. SW_WRITE_REG(INCA_IP_Switch_ST_PT,0x3f);
  254. #if 0
  255. printf("Leaving inca_switch_init()\n");
  256. #endif
  257. return 0;
  258. }
  259. static int inca_switch_send(struct eth_device *dev, volatile void *packet,
  260. int length)
  261. {
  262. int i;
  263. int res = -1;
  264. u32 command;
  265. u32 regValue;
  266. inca_tx_descriptor_t * tx_desc = KSEG1ADDR(&tx_ring[tx_new]);
  267. #if 0
  268. printf("Entered inca_switch_send()\n");
  269. #endif
  270. if (length <= 0) {
  271. printf ("%s: bad packet size: %d\n", dev->name, length);
  272. goto Done;
  273. }
  274. for(i = 0; tx_desc->C == 0; i++) {
  275. if (i >= TOUT_LOOP) {
  276. printf("%s: tx error buffer not ready\n", dev->name);
  277. goto Done;
  278. }
  279. }
  280. if (tx_old_hold >= 0) {
  281. KSEG1ADDR(&tx_ring[tx_old_hold])->params.field.HOLD = 1;
  282. }
  283. tx_old_hold = tx_hold;
  284. tx_desc->params.word =
  285. (INCA_DMA_TX_SOP | INCA_DMA_TX_EOP | INCA_DMA_TX_HOLD);
  286. tx_desc->C = 0;
  287. tx_desc->TxDataPtr = (u32)packet;
  288. tx_desc->params.field.NBA = length;
  289. KSEG1ADDR(&tx_ring[tx_hold])->params.field.HOLD = 0;
  290. tx_hold = tx_new;
  291. tx_new = (tx_new + 1) % NUM_TX_DESC;
  292. if (! initialized) {
  293. command = INCA_IP_DMA_DMA_TXCCR0_INIT;
  294. initialized = 1;
  295. } else {
  296. command = INCA_IP_DMA_DMA_TXCCR0_HR;
  297. }
  298. DMA_READ_REG(INCA_IP_DMA_DMA_TXCCR0, regValue);
  299. regValue |= command;
  300. #if 0
  301. printf("regValue = 0x%x\n", regValue);
  302. #endif
  303. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, regValue);
  304. #if 1
  305. for(i = 0; KSEG1ADDR(&tx_ring[tx_hold])->C == 0; i++) {
  306. if (i >= TOUT_LOOP) {
  307. printf("%s: tx buffer not ready\n", dev->name);
  308. goto Done;
  309. }
  310. }
  311. #endif
  312. res = length;
  313. Done:
  314. #if 0
  315. printf("Leaving inca_switch_send()\n");
  316. #endif
  317. return res;
  318. }
  319. static int inca_switch_recv(struct eth_device *dev)
  320. {
  321. int length = 0;
  322. inca_rx_descriptor_t * rx_desc;
  323. #if 0
  324. printf("Entered inca_switch_recv()\n");
  325. #endif
  326. for (;;) {
  327. rx_desc = KSEG1ADDR(&rx_ring[rx_new]);
  328. if (rx_desc->status.field.C == 0) {
  329. break;
  330. }
  331. #if 0
  332. rx_ring[rx_new].params.field.HOLD = 1;
  333. #endif
  334. if (! rx_desc->status.field.Eop) {
  335. printf("Partly received packet!!!\n");
  336. break;
  337. }
  338. length = rx_desc->status.field.NBT;
  339. rx_desc->status.word &=
  340. ~(INCA_DMA_RX_EOP | INCA_DMA_RX_SOP | INCA_DMA_RX_C);
  341. #if 0
  342. {
  343. int i;
  344. for (i=0;i<length - 4;i++) {
  345. if (i % 16 == 0) printf("\n%04x: ", i);
  346. printf("%02X ", NetRxPackets[rx_new][i]);
  347. }
  348. printf("\n");
  349. }
  350. #endif
  351. if (length) {
  352. #if 0
  353. printf("Received %d bytes\n", length);
  354. #endif
  355. NetReceive((void*)KSEG1ADDR(NetRxPackets[rx_new]),
  356. length - 4);
  357. } else {
  358. #if 1
  359. printf("Zero length!!!\n");
  360. #endif
  361. }
  362. KSEG1ADDR(&rx_ring[rx_hold])->params.field.HOLD = 0;
  363. rx_hold = rx_new;
  364. rx_new = (rx_new + 1) % NUM_RX_DESC;
  365. }
  366. #if 0
  367. printf("Leaving inca_switch_recv()\n");
  368. #endif
  369. return length;
  370. }
  371. static void inca_switch_halt(struct eth_device *dev)
  372. {
  373. #if 0
  374. printf("Entered inca_switch_halt()\n");
  375. #endif
  376. #if 1
  377. initialized = 0;
  378. #endif
  379. #if 1
  380. /* Disable forwarding to the CPU port.
  381. */
  382. SW_WRITE_REG(INCA_IP_Switch_ST_PT,0xf);
  383. /* Close RxDMA channel.
  384. */
  385. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
  386. /* Close TxDMA channel.
  387. */
  388. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, INCA_IP_DMA_DMA_TXCCR0_OFF);
  389. #endif
  390. #if 0
  391. printf("Leaving inca_switch_halt()\n");
  392. #endif
  393. }
  394. static void inca_init_switch_chip(void)
  395. {
  396. u32 regValue;
  397. /* To workaround a problem with collision counter
  398. * (see Errata sheet).
  399. */
  400. SW_WRITE_REG(INCA_IP_Switch_PC_TX_CTL, 0x00000001);
  401. SW_WRITE_REG(INCA_IP_Switch_LAN_TX_CTL, 0x00000001);
  402. #if 1
  403. /* init MDIO configuration:
  404. * MDS (Poll speed): 0x01 (4ms)
  405. * PHY_LAN_ADDR: 0x06
  406. * PHY_PC_ADDR: 0x05
  407. * UEP (Use External PHY): 0x00 (Internal PHY is used)
  408. * PS (Port Select): 0x00 (PT/UMM for LAN)
  409. * PT (PHY Test): 0x00 (no test mode)
  410. * UMM (Use MDIO Mode): 0x00 (state machine is disabled)
  411. */
  412. SW_WRITE_REG(INCA_IP_Switch_MDIO_CFG, 0x4c50);
  413. /* init PHY:
  414. * SL (Auto Neg. Speed for LAN)
  415. * SP (Auto Neg. Speed for PC)
  416. * LL (Link Status for LAN)
  417. * LP (Link Status for PC)
  418. * DL (Duplex Status for LAN)
  419. * DP (Duplex Status for PC)
  420. * PL (Auto Neg. Pause Status for LAN)
  421. * PP (Auto Neg. Pause Status for PC)
  422. */
  423. SW_WRITE_REG (INCA_IP_Switch_EPHY, 0xff);
  424. /* MDIO_ACC:
  425. * RA (Request/Ack) 0x01 (Request)
  426. * RW (Read/Write) 0x01 (Write)
  427. * PHY_ADDR 0x05 (PC)
  428. * REG_ADDR 0x00 (PHY_BCR: basic control register)
  429. * PHY_DATA 0x8000
  430. * Reset - software reset
  431. * LB (loop back) - normal
  432. * SS (speed select) - 10 Mbit/s
  433. * ANE (auto neg. enable) - enable
  434. * PD (power down) - normal
  435. * ISO (isolate) - normal
  436. * RAN (restart auto neg.) - normal
  437. * DM (duplex mode) - half duplex
  438. * CT (collision test) - enable
  439. */
  440. SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC, 0xc0a09000);
  441. /* MDIO_ACC:
  442. * RA (Request/Ack) 0x01 (Request)
  443. * RW (Read/Write) 0x01 (Write)
  444. * PHY_ADDR 0x06 (LAN)
  445. * REG_ADDR 0x00 (PHY_BCR: basic control register)
  446. * PHY_DATA 0x8000
  447. * Reset - software reset
  448. * LB (loop back) - normal
  449. * SS (speed select) - 10 Mbit/s
  450. * ANE (auto neg. enable) - enable
  451. * PD (power down) - normal
  452. * ISO (isolate) - normal
  453. * RAN (restart auto neg.) - normal
  454. * DM (duplex mode) - half duplex
  455. * CT (collision test) - enable
  456. */
  457. SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC, 0xc0c09000);
  458. #endif
  459. /* Make sure the CPU port is disabled for now. We
  460. * don't want packets to get stacked for us until
  461. * we enable DMA and are prepared to receive them.
  462. */
  463. SW_WRITE_REG(INCA_IP_Switch_ST_PT,0xf);
  464. SW_READ_REG(INCA_IP_Switch_ARL_CTL, regValue);
  465. /* CRC GEN is enabled.
  466. */
  467. regValue |= 0x00000200;
  468. SW_WRITE_REG(INCA_IP_Switch_ARL_CTL, regValue);
  469. /* ADD TAG is disabled.
  470. */
  471. SW_READ_REG(INCA_IP_Switch_PMAC_HD_CTL, regValue);
  472. regValue &= ~0x00000002;
  473. SW_WRITE_REG(INCA_IP_Switch_PMAC_HD_CTL, regValue);
  474. }
  475. static void inca_dma_init(void)
  476. {
  477. /* Switch off all DMA channels.
  478. */
  479. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
  480. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR1, INCA_IP_DMA_DMA_RXCCR1_OFF);
  481. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
  482. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR1, INCA_IP_DMA_DMA_TXCCR1_OFF);
  483. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR2, INCA_IP_DMA_DMA_TXCCR2_OFF);
  484. /* Setup TX channel polling time.
  485. */
  486. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXPOLL, INCA_DMA_TX_POLLING_TIME);
  487. /* Setup RX channel polling time.
  488. */
  489. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXPOLL, INCA_DMA_RX_POLLING_TIME);
  490. /* ERRATA: write reset value into the DMA RX IMR register.
  491. */
  492. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXIMR, 0xFFFFFFFF);
  493. /* Just in case: disable all transmit interrupts also.
  494. */
  495. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXIMR, 0xFFFFFFFF);
  496. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXISR, 0xFFFFFFFF);
  497. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXISR, 0xFFFFFFFF);
  498. }
  499. #if defined(CONFIG_INCA_IP_SWITCH_AMDIX)
  500. static int inca_amdix(void)
  501. {
  502. u32 regValue = 0;
  503. int mdi_flag;
  504. int retries;
  505. /* Setup GPIO pins.
  506. */
  507. *INCA_IP_AUTO_MDIX_LAN_PORTS_DIR |= (1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
  508. *INCA_IP_AUTO_MDIX_LAN_PORTS_ALTSEL |= (1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
  509. /* Wait for signal.
  510. */
  511. retries = WAIT_SIGNAL_RETRIES;
  512. while (--retries)
  513. {
  514. SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
  515. (0x1 << 31) | /* RA */
  516. (0x0 << 30) | /* Read */
  517. (0x6 << 21) | /* LAN */
  518. (17 << 16)); /* PHY_MCSR */
  519. do
  520. {
  521. SW_READ_REG(INCA_IP_Switch_MDIO_ACC, regValue);
  522. }
  523. while (regValue & (1 << 31));
  524. if (regValue & (1 << 1))
  525. {
  526. /* Signal detected */
  527. break;
  528. }
  529. }
  530. if (!retries)
  531. return -1;
  532. /* Set MDI mode.
  533. */
  534. *INCA_IP_AUTO_MDIX_LAN_PORTS_OUT &= ~(1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
  535. mdi_flag = 1;
  536. /* Wait for link.
  537. */
  538. retries = WAIT_LINK_RETRIES;
  539. while (--retries)
  540. {
  541. udelay(LINK_RETRY_DELAY * 1000);
  542. SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
  543. (0x1 << 31) | /* RA */
  544. (0x0 << 30) | /* Read */
  545. (0x6 << 21) | /* LAN */
  546. (1 << 16)); /* PHY_BSR */
  547. do
  548. {
  549. SW_READ_REG(INCA_IP_Switch_MDIO_ACC, regValue);
  550. }
  551. while (regValue & (1 << 31));
  552. if (regValue & (1 << 2))
  553. {
  554. /* Link is up */
  555. break;
  556. }
  557. else if (mdi_flag)
  558. {
  559. /* Set MDIX mode */
  560. *INCA_IP_AUTO_MDIX_LAN_PORTS_OUT |= (1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
  561. mdi_flag = 0;
  562. }
  563. else
  564. {
  565. /* Set MDI mode */
  566. *INCA_IP_AUTO_MDIX_LAN_PORTS_OUT &= ~(1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
  567. mdi_flag = 1;
  568. }
  569. }
  570. if (!retries)
  571. return -1;
  572. return 0;
  573. }
  574. #endif /* CONFIG_INCA_IP_SWITCH_AMDIX */
  575. #endif