inca-ip_sw.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. typedef struct
  57. {
  58. union
  59. {
  60. struct
  61. {
  62. volatile u32 HOLD :1;
  63. volatile u32 ICpt :1;
  64. volatile u32 IEop :1;
  65. volatile u32 offset :3;
  66. volatile u32 reserved0 :4;
  67. volatile u32 NFB :22;
  68. }field;
  69. volatile u32 word;
  70. }params;
  71. volatile u32 nextRxDescPtr;
  72. volatile u32 RxDataPtr;
  73. union
  74. {
  75. struct
  76. {
  77. volatile u32 C :1;
  78. volatile u32 Sop :1;
  79. volatile u32 Eop :1;
  80. volatile u32 reserved3 :12;
  81. volatile u32 NBT :17;
  82. }field;
  83. volatile u32 word;
  84. }status;
  85. } inca_rx_descriptor_t;
  86. typedef struct
  87. {
  88. union
  89. {
  90. struct
  91. {
  92. volatile u32 HOLD :1;
  93. volatile u32 Eop :1;
  94. volatile u32 Sop :1;
  95. volatile u32 ICpt :1;
  96. volatile u32 IEop :1;
  97. volatile u32 reserved0 :5;
  98. volatile u32 NBA :22;
  99. }field;
  100. volatile u32 word;
  101. }params;
  102. volatile u32 nextTxDescPtr;
  103. volatile u32 TxDataPtr;
  104. volatile u32 C :1;
  105. volatile u32 reserved3 :31;
  106. } inca_tx_descriptor_t;
  107. static inca_rx_descriptor_t rx_ring[NUM_RX_DESC] __attribute__ ((aligned(16)));
  108. static inca_tx_descriptor_t tx_ring[NUM_TX_DESC] __attribute__ ((aligned(16)));
  109. static int tx_new, rx_new, tx_hold, rx_hold;
  110. static int tx_old_hold = -1;
  111. static int initialized = 0;
  112. static int inca_switch_init(struct eth_device *dev, bd_t * bis);
  113. static int inca_switch_send(struct eth_device *dev, volatile void *packet,
  114. int length);
  115. static int inca_switch_recv(struct eth_device *dev);
  116. static void inca_switch_halt(struct eth_device *dev);
  117. static void inca_init_switch_chip(void);
  118. static void inca_dma_init(void);
  119. int inca_switch_initialize(bd_t * bis)
  120. {
  121. struct eth_device *dev;
  122. #if 0
  123. printf("Entered inca_switch_initialize()\n");
  124. #endif
  125. if (!(dev = (struct eth_device *) malloc (sizeof *dev)))
  126. {
  127. printf("Failed to allocate memory\n");
  128. return 0;
  129. }
  130. memset(dev, 0, sizeof(*dev));
  131. inca_dma_init();
  132. inca_init_switch_chip();
  133. sprintf(dev->name, "INCA-IP Switch");
  134. dev->init = inca_switch_init;
  135. dev->halt = inca_switch_halt;
  136. dev->send = inca_switch_send;
  137. dev->recv = inca_switch_recv;
  138. eth_register(dev);
  139. #if 0
  140. printf("Leaving inca_switch_initialize()\n");
  141. #endif
  142. return 1;
  143. }
  144. static int inca_switch_init(struct eth_device *dev, bd_t * bis)
  145. {
  146. int i;
  147. u32 v, regValue;
  148. u16 wTmp;
  149. #if 0
  150. printf("Entering inca_switch_init()\n");
  151. #endif
  152. /* Set MAC address.
  153. */
  154. wTmp = (u16)dev->enetaddr[0];
  155. regValue = (wTmp << 8) | dev->enetaddr[1];
  156. SW_WRITE_REG(INCA_IP_Switch_PMAC_SA1, regValue);
  157. wTmp = (u16)dev->enetaddr[2];
  158. regValue = (wTmp << 8) | dev->enetaddr[3];
  159. regValue = regValue << 16;
  160. wTmp = (u16)dev->enetaddr[4];
  161. regValue |= (wTmp<<8) | dev->enetaddr[5];
  162. SW_WRITE_REG(INCA_IP_Switch_PMAC_SA2, regValue);
  163. /* Initialize the descriptor rings.
  164. */
  165. for (i = 0; i < NUM_RX_DESC; i++)
  166. {
  167. inca_rx_descriptor_t * rx_desc = KSEG1ADDR(&rx_ring[i]);
  168. memset(rx_desc, 0, sizeof(rx_ring[i]));
  169. /* Set maximum size of receive buffer.
  170. */
  171. rx_desc->params.field.NFB = PKTSIZE_ALIGN;
  172. /* Set the offset of the receive buffer. Zero means
  173. * that the offset mechanism is not used.
  174. */
  175. rx_desc->params.field.offset = 0;
  176. /* Check if it is the last descriptor.
  177. */
  178. if (i == (NUM_RX_DESC - 1))
  179. {
  180. /* Let the last descriptor point to the first
  181. * one.
  182. */
  183. rx_desc->nextRxDescPtr = KSEG1ADDR((u32)rx_ring);
  184. }
  185. else
  186. {
  187. /* Set the address of the next descriptor.
  188. */
  189. rx_desc->nextRxDescPtr = (u32)KSEG1ADDR(&rx_ring[i+1]);
  190. }
  191. rx_desc->RxDataPtr = (u32)KSEG1ADDR(NetRxPackets[i]);
  192. }
  193. #if 0
  194. printf("rx_ring = 0x%08X 0x%08X\n", (u32)rx_ring, (u32)&rx_ring[0]);
  195. printf("tx_ring = 0x%08X 0x%08X\n", (u32)tx_ring, (u32)&tx_ring[0]);
  196. #endif
  197. for (i = 0; i < NUM_TX_DESC; i++)
  198. {
  199. inca_tx_descriptor_t * tx_desc = KSEG1ADDR(&tx_ring[i]);
  200. memset(tx_desc, 0, sizeof(tx_ring[i]));
  201. tx_desc->params.word = 0;
  202. tx_desc->params.field.HOLD = 1;
  203. tx_desc->C = 1;
  204. /* Check if it is the last descriptor.
  205. */
  206. if (i == (NUM_TX_DESC - 1))
  207. {
  208. /* Let the last descriptor point to the
  209. * first one.
  210. */
  211. tx_desc->nextTxDescPtr = KSEG1ADDR((u32)tx_ring);
  212. }
  213. else
  214. {
  215. /* Set the address of the next descriptor.
  216. */
  217. tx_desc->nextTxDescPtr = (u32)KSEG1ADDR(&tx_ring[i+1]);
  218. }
  219. }
  220. /* Initialize RxDMA.
  221. */
  222. DMA_READ_REG(INCA_IP_DMA_DMA_RXISR, v);
  223. #if 0
  224. printf("RX status = 0x%08X\n", v);
  225. #endif
  226. /* Writing to the FRDA of CHANNEL.
  227. */
  228. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXFRDA0, (u32)rx_ring);
  229. /* Writing to the COMMAND REG.
  230. */
  231. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0,
  232. INCA_IP_DMA_DMA_RXCCR0_INIT);
  233. /* Initialize TxDMA.
  234. */
  235. DMA_READ_REG(INCA_IP_DMA_DMA_TXISR, v);
  236. #if 0
  237. printf("TX status = 0x%08X\n", v);
  238. #endif
  239. /* Writing to the FRDA of CHANNEL.
  240. */
  241. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXFRDA0, (u32)tx_ring);
  242. tx_new = rx_new = 0;
  243. tx_hold = NUM_TX_DESC - 1;
  244. rx_hold = NUM_RX_DESC - 1;
  245. #if 0
  246. rx_ring[rx_hold].params.field.HOLD = 1;
  247. #endif
  248. /* enable spanning tree forwarding, enable the CPU port */
  249. /* ST_PT:
  250. CPS (CPU port status) 0x3 (forwarding)
  251. LPS (LAN port status) 0x3 (forwarding)
  252. PPS (PC port status) 0x3 (forwarding)
  253. */
  254. SW_WRITE_REG(INCA_IP_Switch_ST_PT,0x3f);
  255. #if 0
  256. printf("Leaving inca_switch_init()\n");
  257. #endif
  258. return 0;
  259. }
  260. static int inca_switch_send(struct eth_device *dev, volatile void *packet,
  261. int length)
  262. {
  263. int i;
  264. int res = -1;
  265. u32 command;
  266. u32 regValue;
  267. inca_tx_descriptor_t * tx_desc = KSEG1ADDR(&tx_ring[tx_new]);
  268. #if 0
  269. printf("Entered inca_switch_send()\n");
  270. #endif
  271. if (length <= 0)
  272. {
  273. printf ("%s: bad packet size: %d\n", dev->name, length);
  274. goto Done;
  275. }
  276. for(i = 0; tx_desc->C == 0; i++)
  277. {
  278. if (i >= TOUT_LOOP)
  279. {
  280. printf("%s: tx error buffer not ready\n", dev->name);
  281. goto Done;
  282. }
  283. }
  284. if (tx_old_hold >= 0)
  285. {
  286. KSEG1ADDR(&tx_ring[tx_old_hold])->params.field.HOLD = 1;
  287. }
  288. tx_old_hold = tx_hold;
  289. tx_desc->params.word =
  290. (INCA_DMA_TX_SOP | INCA_DMA_TX_EOP | INCA_DMA_TX_HOLD);
  291. tx_desc->C = 0;
  292. tx_desc->TxDataPtr = (u32)packet;
  293. tx_desc->params.field.NBA = length;
  294. KSEG1ADDR(&tx_ring[tx_hold])->params.field.HOLD = 0;
  295. tx_hold = tx_new;
  296. tx_new = (tx_new + 1) % NUM_TX_DESC;
  297. if (! initialized)
  298. {
  299. command = INCA_IP_DMA_DMA_TXCCR0_INIT;
  300. initialized = 1;
  301. }
  302. else
  303. {
  304. command = INCA_IP_DMA_DMA_TXCCR0_HR;
  305. }
  306. DMA_READ_REG(INCA_IP_DMA_DMA_TXCCR0, regValue);
  307. regValue |= command;
  308. #if 0
  309. printf("regValue = 0x%x\n", regValue);
  310. #endif
  311. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, regValue);
  312. #if 1
  313. for(i = 0; KSEG1ADDR(&tx_ring[tx_hold])->C == 0; i++)
  314. {
  315. if (i >= TOUT_LOOP)
  316. {
  317. printf("%s: tx buffer not ready\n", dev->name);
  318. goto Done;
  319. }
  320. }
  321. #endif
  322. res = length;
  323. Done:
  324. #if 0
  325. printf("Leaving inca_switch_send()\n");
  326. #endif
  327. return res;
  328. }
  329. static int inca_switch_recv(struct eth_device *dev)
  330. {
  331. int length = 0;
  332. inca_rx_descriptor_t * rx_desc;
  333. #if 0
  334. printf("Entered inca_switch_recv()\n");
  335. #endif
  336. for (;;)
  337. {
  338. rx_desc = KSEG1ADDR(&rx_ring[rx_new]);
  339. if (rx_desc->status.field.C == 0)
  340. {
  341. break;
  342. }
  343. #if 0
  344. rx_ring[rx_new].params.field.HOLD = 1;
  345. #endif
  346. if (! rx_desc->status.field.Eop)
  347. {
  348. printf("Partly received packet!!!\n");
  349. break;
  350. }
  351. length = rx_desc->status.field.NBT;
  352. rx_desc->status.word &=
  353. ~(INCA_DMA_RX_EOP | INCA_DMA_RX_SOP | INCA_DMA_RX_C);
  354. #if 0
  355. {
  356. int i;
  357. for (i=0;i<length - 4;i++) {
  358. if (i % 16 == 0) printf("\n%04x: ", i);
  359. printf("%02X ", NetRxPackets[rx_new][i]);
  360. }
  361. printf("\n");
  362. }
  363. #endif
  364. if (length)
  365. {
  366. #if 0
  367. printf("Received %d bytes\n", length);
  368. #endif
  369. NetReceive((void*)KSEG1ADDR(NetRxPackets[rx_new]),
  370. length - 4);
  371. }
  372. else
  373. {
  374. #if 1
  375. printf("Zero length!!!\n");
  376. #endif
  377. }
  378. KSEG1ADDR(&rx_ring[rx_hold])->params.field.HOLD = 0;
  379. rx_hold = rx_new;
  380. rx_new = (rx_new + 1) % NUM_RX_DESC;
  381. }
  382. #if 0
  383. printf("Leaving inca_switch_recv()\n");
  384. #endif
  385. return length;
  386. }
  387. static void inca_switch_halt(struct eth_device *dev)
  388. {
  389. #if 0
  390. printf("Entered inca_switch_halt()\n");
  391. #endif
  392. #if 1
  393. initialized = 0;
  394. #endif
  395. #if 1
  396. /* Disable forwarding to the CPU port.
  397. */
  398. SW_WRITE_REG(INCA_IP_Switch_ST_PT,0xf);
  399. /* Close RxDMA channel.
  400. */
  401. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
  402. /* Close TxDMA channel.
  403. */
  404. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, INCA_IP_DMA_DMA_TXCCR0_OFF);
  405. #endif
  406. #if 0
  407. printf("Leaving inca_switch_halt()\n");
  408. #endif
  409. }
  410. static void inca_init_switch_chip(void)
  411. {
  412. u32 regValue;
  413. /* To workaround a problem with collision counter
  414. * (see Errata sheet).
  415. */
  416. SW_WRITE_REG(INCA_IP_Switch_PC_TX_CTL, 0x00000001);
  417. SW_WRITE_REG(INCA_IP_Switch_LAN_TX_CTL, 0x00000001);
  418. #if 1
  419. /* init MDIO configuration:
  420. MDS (Poll speed): 0x01 (4ms)
  421. PHY_LAN_ADDR: 0x06
  422. PHY_PC_ADDR: 0x05
  423. UEP (Use External PHY): 0x00 (Internal PHY is used)
  424. PS (Port Select): 0x00 (PT/UMM for LAN)
  425. PT (PHY Test): 0x00 (no test mode)
  426. UMM (Use MDIO Mode): 0x00 (state machine is disabled)
  427. */
  428. SW_WRITE_REG(INCA_IP_Switch_MDIO_CFG, 0x4c50);
  429. /* init PHY:
  430. SL (Auto Neg. Speed for LAN)
  431. SP (Auto Neg. Speed for PC)
  432. LL (Link Status for LAN)
  433. LP (Link Status for PC)
  434. DL (Duplex Status for LAN)
  435. DP (Duplex Status for PC)
  436. PL (Auto Neg. Pause Status for LAN)
  437. PP (Auto Neg. Pause Status for PC)
  438. */
  439. SW_WRITE_REG (INCA_IP_Switch_EPHY, 0xff);
  440. /* MDIO_ACC:
  441. RA (Request/Ack) 0x01 (Request)
  442. RW (Read/Write) 0x01 (Write)
  443. PHY_ADDR 0x05 (PC)
  444. REG_ADDR 0x00 (PHY_BCR: basic control register)
  445. PHY_DATA 0x8000
  446. Reset - software reset
  447. LB (loop back) - normal
  448. SS (speed select) - 10 Mbit/s
  449. ANE (auto neg. enable) - disable
  450. PD (power down) - normal
  451. ISO (isolate) - normal
  452. RAN (restart auto neg.) - normal
  453. DM (duplex mode) - half duplex
  454. CT (collision test) - enable
  455. */
  456. SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC, 0xc0a08000);
  457. /* MDIO_ACC:
  458. RA (Request/Ack) 0x01 (Request)
  459. RW (Read/Write) 0x01 (Write)
  460. PHY_ADDR 0x06 (LAN)
  461. REG_ADDR 0x00 (PHY_BCR: basic control register)
  462. PHY_DATA 0x8000
  463. Reset - software reset
  464. LB (loop back) - normal
  465. SS (speed select) - 10 Mbit/s
  466. ANE (auto neg. enable) - disable
  467. PD (power down) - normal
  468. ISO (isolate) - normal
  469. RAN (restart auto neg.) - normal
  470. DM (duplex mode) - half duplex
  471. CT (collision test) - enable
  472. */
  473. SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC, 0xc0c08000);
  474. #endif
  475. /* Make sure the CPU port is disabled for now. We
  476. * don't want packets to get stacked for us until
  477. * we enable DMA and are prepared to receive them.
  478. */
  479. SW_WRITE_REG(INCA_IP_Switch_ST_PT,0xf);
  480. SW_READ_REG(INCA_IP_Switch_ARL_CTL, regValue);
  481. /* CRC GEN is enabled.
  482. */
  483. regValue |= 0x00000200;
  484. SW_WRITE_REG(INCA_IP_Switch_ARL_CTL, regValue);
  485. /* ADD TAG is disabled.
  486. */
  487. SW_READ_REG(INCA_IP_Switch_PMAC_HD_CTL, regValue);
  488. regValue &= ~0x00000002;
  489. SW_WRITE_REG(INCA_IP_Switch_PMAC_HD_CTL, regValue);
  490. }
  491. static void inca_dma_init(void)
  492. {
  493. /* Switch off all DMA channels.
  494. */
  495. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
  496. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR1, INCA_IP_DMA_DMA_RXCCR1_OFF);
  497. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
  498. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR1, INCA_IP_DMA_DMA_TXCCR1_OFF);
  499. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR2, INCA_IP_DMA_DMA_TXCCR2_OFF);
  500. /* Setup TX channel polling time.
  501. */
  502. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXPOLL, INCA_DMA_TX_POLLING_TIME);
  503. /* Setup RX channel polling time.
  504. */
  505. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXPOLL, INCA_DMA_RX_POLLING_TIME);
  506. /* ERRATA: write reset value into the DMA RX IMR register.
  507. */
  508. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXIMR, 0xFFFFFFFF);
  509. /* Just in case: disable all transmit interrupts also.
  510. */
  511. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXIMR, 0xFFFFFFFF);
  512. DMA_WRITE_REG(INCA_IP_DMA_DMA_TXISR, 0xFFFFFFFF);
  513. DMA_WRITE_REG(INCA_IP_DMA_DMA_RXISR, 0xFFFFFFFF);
  514. }
  515. #endif
  516. /* End of file.
  517. */