eth.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /**************************************************************************
  2. Etherboot - BOOTP/TFTP Bootstrap Program
  3. Skeleton NIC driver for Etherboot
  4. ***************************************************************************/
  5. /*
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2, or (at
  9. * your option) any later version.
  10. */
  11. /*
  12. * This file is a modified version from the Galileo polled mode
  13. * network driver for the ethernet contained within the GT64260
  14. * chip. It has been modified to fit into the U-Boot framework, from
  15. * the original (etherboot) setup. Also, additional cleanup and features
  16. * were added.
  17. *
  18. * - Josh Huber <huber@mclx.com>
  19. */
  20. #include <common.h>
  21. #include <malloc.h>
  22. #include <galileo/gt64260R.h>
  23. #include <galileo/core.h>
  24. #include <asm/cache.h>
  25. #include <miiphy.h>
  26. #include <net.h>
  27. #include "eth.h"
  28. #include "eth_addrtbl.h"
  29. #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
  30. #define GT6426x_ETH_BUF_SIZE 1536
  31. /* if you like verbose output, turn this on! */
  32. #undef DEBUG
  33. /* Restart autoneg if we detect link is up on phy init. */
  34. /*
  35. * The GT doc's say that after Rst is deasserted, and the PHY
  36. * reports autoneg complete, it runs through its autoneg
  37. * procedures. This doesn't seem to be the case for MII
  38. * PHY's. To work around this check for link up && autoneg
  39. * complete when initilizing the port. If they are both set,
  40. * then restart PHY autoneg. Of course, it may be something
  41. * completly different.
  42. */
  43. #ifdef CONFIG_ETHER_PORT_MII
  44. # define RESTART_AUTONEG
  45. #endif
  46. /* do this if you dont want to use snooping */
  47. #define USE_SOFTWARE_CACHE_MANAGEMENT
  48. #ifdef USE_SOFTWARE_CACHE_MANAGEMENT
  49. #define FLUSH_DCACHE(a,b) if(dcache_status()){clean_dcache_range((u32)(a),(u32)(b));}
  50. #define FLUSH_AND_INVALIDATE_DCACHE(a,b) if(dcache_status()){flush_dcache_range((u32)(a),(u32)(b));}
  51. #define INVALIDATE_DCACHE(a,b) if(dcache_status()){invalidate_dcache_range((u32)(a),(u32)(b));}
  52. #else
  53. /* bummer - w/o flush, nothing works, even with snooping - FIXME */
  54. /* #define FLUSH_DCACHE(a,b) */
  55. #define FLUSH_DCACHE(a,b) if(dcache_status()){clean_dcache_range((u32)(a),(u32)(b));}
  56. #define FLUSH_AND_INVALIDATE_DCACHE(a,b)
  57. #define INVALIDATE_DCACHE(a,b)
  58. #endif
  59. struct eth_dev_s {
  60. eth0_tx_desc_single *eth_tx_desc;
  61. eth0_rx_desc_single *eth_rx_desc;
  62. char *eth_tx_buffer;
  63. char *eth_rx_buffer[NR];
  64. int tdn, rdn;
  65. int dev;
  66. unsigned int reg_base;
  67. };
  68. #ifdef CONFIG_INTEL_LXT97X
  69. /* for intel LXT972 */
  70. static const char ether_port_phy_addr[3]={0,1,2};
  71. #else
  72. static const char ether_port_phy_addr[3]={4,5,6};
  73. #endif
  74. /* MII PHY access routines are common for all i/f, use gal_ent0 */
  75. #define GT6426x_MII_DEVNAME "gal_enet0"
  76. int gt6426x_miiphy_read(char *devname, unsigned char phy,
  77. unsigned char reg, unsigned short *val);
  78. static inline unsigned short
  79. miiphy_read_ret(unsigned short phy, unsigned short reg)
  80. {
  81. unsigned short val;
  82. gt6426x_miiphy_read(GT6426x_MII_DEVNAME,phy,reg,&val);
  83. return val;
  84. }
  85. /**************************************************************************
  86. RESET - Reset adapter
  87. ***************************************************************************/
  88. void
  89. gt6426x_eth_reset(void *v)
  90. {
  91. /* we should do something here...
  92. struct eth_device *wp = (struct eth_device *)v;
  93. struct eth_dev_s *p = wp->priv;
  94. */
  95. printf ("RESET\n");
  96. /* put the card in its initial state */
  97. }
  98. static void gt6426x_handle_SMI(struct eth_dev_s *p, unsigned int icr)
  99. {
  100. #ifdef DEBUG
  101. printf("SMI interrupt: ");
  102. if(icr&0x20000000) {
  103. printf("SMI done\n");
  104. }
  105. #endif
  106. if(icr&0x10000000) {
  107. unsigned int psr;
  108. psr=GTREGREAD(ETHERNET0_PORT_STATUS_REGISTER + p->reg_base);
  109. #ifdef DEBUG
  110. printf("PHY state change:\n"
  111. " GT:%s:%s:%s:%s\n",
  112. psr&1?"100":" 10",
  113. psr&8?" Link":"nLink",
  114. psr&2?"FD":"HD",
  115. psr&4?" FC":"nFC");
  116. #ifdef CONFIG_INTEL_LXT97X /* non-standard mii reg (intel lxt972a) */
  117. {
  118. unsigned short mii_11;
  119. mii_11=miiphy_read_ret(ether_port_phy_addr[p->dev],0x11);
  120. printf(" mii:%s:%s:%s:%s %s:%s %s\n",
  121. mii_11&(1<<14)?"100":" 10",
  122. mii_11&(1<<10)?" Link":"nLink",
  123. mii_11&(1<<9)?"FD":"HD",
  124. mii_11&(1<<4)?" FC":"nFC",
  125. mii_11&(1<<7)?"ANc":"ANnc",
  126. mii_11&(1<<8)?"AN":"Manual",
  127. ""
  128. );
  129. }
  130. #endif /* CONFIG_INTEL_LXT97X */
  131. #endif /* DEBUG */
  132. }
  133. }
  134. static int
  135. gt6426x_eth_receive(struct eth_dev_s *p,unsigned int icr)
  136. {
  137. int eth_len=0;
  138. char *eth_data;
  139. eth0_rx_desc_single *rx=&p->eth_rx_desc[(p->rdn)];
  140. INVALIDATE_DCACHE((unsigned int)rx,(unsigned int)(rx+1));
  141. if (rx->command_status & 0x80000000) {
  142. return 0; /* No packet received */
  143. }
  144. eth_len = (unsigned int)
  145. (rx->buff_size_byte_count) & 0x0000ffff;
  146. eth_data = (char *) p->eth_rx_buffer[p->rdn];
  147. #ifdef DEBUG
  148. if (eth_len) {
  149. printf ("%s: Recived %d byte Packet @ 0x%p\n",
  150. __FUNCTION__, eth_len, eth_data);
  151. }
  152. #endif
  153. /*
  154. * packet is now in:
  155. * eth0_rx_buffer[RDN_ETH0];
  156. */
  157. /* let the upper layer handle the packet */
  158. NetReceive ((uchar *)eth_data, eth_len);
  159. rx->buff_size_byte_count = GT6426x_ETH_BUF_SIZE<<16;
  160. /* GT96100 Owner */
  161. rx->command_status = 0x80000000;
  162. FLUSH_DCACHE((unsigned int)rx,(unsigned int)(rx+1));
  163. p->rdn ++;
  164. if (p->rdn == NR) {p->rdn = 0;}
  165. sync();
  166. /* Start Rx*/
  167. GT_REG_WRITE (ETHERNET0_SDMA_COMMAND_REGISTER + p->reg_base, 0x00000080);
  168. #ifdef DEBUG
  169. {
  170. int i;
  171. for (i=0;i<12;i++) {
  172. printf(" %02x", eth_data[i]);
  173. }
  174. }
  175. printf(": %d bytes\n", eth_len);
  176. #endif
  177. INVALIDATE_DCACHE((unsigned int)eth_data,
  178. (unsigned int)eth_data+eth_len);
  179. return eth_len;
  180. }
  181. /**************************************************************************
  182. POLL - look for an rx frame, handle other conditions
  183. ***************************************************************************/
  184. int
  185. gt6426x_eth_poll(void *v)
  186. {
  187. struct eth_device *wp = (struct eth_device *)v;
  188. struct eth_dev_s *p = wp->priv;
  189. unsigned int icr=GTREGREAD(ETHERNET0_INTERRUPT_CAUSE_REGISTER + p->reg_base);
  190. if(icr) {
  191. GT_REG_WRITE(ETHERNET0_INTERRUPT_CAUSE_REGISTER +p->reg_base, 0);
  192. #ifdef DEBUG
  193. printf("poll got ICR %08x\n", icr);
  194. #endif
  195. /* SMI done or PHY state change*/
  196. if(icr&0x30000000) gt6426x_handle_SMI(p, icr);
  197. }
  198. /* always process. We aren't using RX interrupts */
  199. return gt6426x_eth_receive(p, icr);
  200. }
  201. /**************************************************************************
  202. TRANSMIT - Transmit a frame
  203. ***************************************************************************/
  204. int
  205. gt6426x_eth_transmit(void *v, volatile char *p, unsigned int s)
  206. {
  207. struct eth_device *wp = (struct eth_device *)v;
  208. struct eth_dev_s *dev = (struct eth_dev_s *)wp->priv;
  209. #ifdef DEBUG
  210. unsigned int old_command_stat,old_psr;
  211. #endif
  212. eth0_tx_desc_single *tx=&dev->eth_tx_desc[dev->tdn];
  213. /* wait for tx to be ready */
  214. INVALIDATE_DCACHE((unsigned int)tx,(unsigned int)(tx+1));
  215. while (tx->command_status & 0x80000000) {
  216. int i;
  217. for(i=0;i<1000;i++);
  218. INVALIDATE_DCACHE((unsigned int)tx,(unsigned int)(tx+1));
  219. }
  220. GT_REG_WRITE (ETHERNET0_CURRENT_TX_DESCRIPTOR_POINTER0 + dev->reg_base,
  221. (unsigned int)tx);
  222. #ifdef DEBUG
  223. printf("copying to tx_buffer [%p], length %x, desc = %p\n",
  224. dev->eth_tx_buffer, s, dev->eth_tx_desc);
  225. #endif
  226. memcpy(dev->eth_tx_buffer, (char *) p, s);
  227. tx->buff_pointer = (uchar *)dev->eth_tx_buffer;
  228. tx->bytecount_reserved = ((__u16)s) << 16;
  229. /* 31 - own
  230. * 22 - gencrc
  231. * 18:16 - pad, last, first */
  232. tx->command_status = (1<<31) | (1<<22) | (7<<16);
  233. #if 0
  234. /* FEr #18 */
  235. tx->next_desc = NULL;
  236. #else
  237. tx->next_desc =
  238. (struct eth0_tx_desc_struct *)
  239. &dev->eth_tx_desc[(dev->tdn+1)%NT].bytecount_reserved;
  240. /* cpu owned */
  241. dev->eth_tx_desc[(dev->tdn+1)%NT].command_status = (7<<16); /* pad, last, first */
  242. #endif
  243. #ifdef DEBUG
  244. old_command_stat=tx->command_status,
  245. old_psr=GTREGREAD(ETHERNET0_PORT_STATUS_REGISTER + dev->reg_base);
  246. #endif
  247. FLUSH_DCACHE((unsigned int)tx,
  248. (unsigned int)&dev->eth_tx_desc[(dev->tdn+2)%NT]);
  249. FLUSH_DCACHE((unsigned int)dev->eth_tx_buffer,(unsigned int)dev->eth_tx_buffer+s);
  250. GT_REG_WRITE(ETHERNET0_SDMA_COMMAND_REGISTER + dev->reg_base, 0x01000000);
  251. #ifdef DEBUG
  252. {
  253. unsigned int command_stat=0;
  254. printf("cmd_stat: %08x PSR: %08x\n", old_command_stat, old_psr);
  255. /* wait for tx to be ready */
  256. do {
  257. unsigned int psr=GTREGREAD(ETHERNET0_PORT_STATUS_REGISTER + dev->reg_base);
  258. command_stat=tx->command_status;
  259. if(command_stat!=old_command_stat || psr !=old_psr) {
  260. printf("cmd_stat: %08x PSR: %08x\n", command_stat, psr);
  261. old_command_stat = command_stat;
  262. old_psr = psr;
  263. }
  264. /* gt6426x_eth0_poll(); */
  265. } while (command_stat & 0x80000000);
  266. printf("sent %d byte frame\n", s);
  267. if((command_stat & (3<<15)) == 3) {
  268. printf("frame had error (stat=%08x)\n", command_stat);
  269. }
  270. }
  271. #endif
  272. return 0;
  273. }
  274. /**************************************************************************
  275. DISABLE - Turn off ethernet interface
  276. ***************************************************************************/
  277. void
  278. gt6426x_eth_disable(void *v)
  279. {
  280. struct eth_device *wp = (struct eth_device *)v;
  281. struct eth_dev_s *p = (struct eth_dev_s *)wp->priv;
  282. GT_REG_WRITE(ETHERNET0_SDMA_COMMAND_REGISTER + p->reg_base, 0x80008000);
  283. }
  284. /**************************************************************************
  285. MII utilities - write: write to an MII register via SMI
  286. ***************************************************************************/
  287. int
  288. gt6426x_miiphy_write(char *devname, unsigned char phy,
  289. unsigned char reg, unsigned short data)
  290. {
  291. unsigned int temp= (reg<<21) | (phy<<16) | data;
  292. while(GTREGREAD(ETHERNET_SMI_REGISTER) & (1<<28)); /* wait for !Busy */
  293. GT_REG_WRITE(ETHERNET_SMI_REGISTER, temp);
  294. return 0;
  295. }
  296. /**************************************************************************
  297. MII utilities - read: read from an MII register via SMI
  298. ***************************************************************************/
  299. int
  300. gt6426x_miiphy_read(char *devname, unsigned char phy,
  301. unsigned char reg, unsigned short *val)
  302. {
  303. unsigned int temp= (reg<<21) | (phy<<16) | 1<<26;
  304. while(GTREGREAD(ETHERNET_SMI_REGISTER) & (1<<28)); /* wait for !Busy */
  305. GT_REG_WRITE(ETHERNET_SMI_REGISTER, temp);
  306. while(1) {
  307. temp=GTREGREAD(ETHERNET_SMI_REGISTER);
  308. if(temp & (1<<27)) break; /* wait for ReadValid */
  309. }
  310. *val = temp & 0xffff;
  311. return 0;
  312. }
  313. #ifdef DEBUG
  314. /**************************************************************************
  315. MII utilities - dump mii registers
  316. ***************************************************************************/
  317. static void
  318. gt6426x_dump_mii(bd_t *bis, unsigned short phy)
  319. {
  320. printf("mii reg 0 - 3: %04x %04x %04x %04x\n",
  321. miiphy_read_ret(phy, 0x0),
  322. miiphy_read_ret(phy, 0x1),
  323. miiphy_read_ret(phy, 0x2),
  324. miiphy_read_ret(phy, 0x3)
  325. );
  326. printf(" 4 - 7: %04x %04x %04x %04x\n",
  327. miiphy_read_ret(phy, 0x4),
  328. miiphy_read_ret(phy, 0x5),
  329. miiphy_read_ret(phy, 0x6),
  330. miiphy_read_ret(phy, 0x7)
  331. );
  332. printf(" 8: %04x\n",
  333. miiphy_read_ret(phy, 0x8)
  334. );
  335. printf(" 16-19: %04x %04x %04x %04x\n",
  336. miiphy_read_ret(phy, 0x10),
  337. miiphy_read_ret(phy, 0x11),
  338. miiphy_read_ret(phy, 0x12),
  339. miiphy_read_ret(phy, 0x13)
  340. );
  341. printf(" 20,30: %04x %04x\n",
  342. miiphy_read_ret(phy, 20),
  343. miiphy_read_ret(phy, 30)
  344. );
  345. }
  346. #endif
  347. #ifdef RESTART_AUTONEG
  348. /* If link is up && autoneg compleate, and if
  349. * GT and PHY disagree about link capabilitys,
  350. * restart autoneg - something screwy with FD/HD
  351. * unless we do this. */
  352. static void
  353. check_phy_state(struct eth_dev_s *p)
  354. {
  355. int bmsr = miiphy_read_ret(ether_port_phy_addr[p->dev], PHY_BMSR);
  356. int psr = GTREGREAD(ETHERNET0_PORT_STATUS_REGISTER + p->reg_base);
  357. if ((psr & 1<<3) && (bmsr & PHY_BMSR_LS)) {
  358. int nego = miiphy_read_ret(ether_port_phy_addr[p->dev], PHY_ANAR) &
  359. miiphy_read_ret(ether_port_phy_addr[p->dev], PHY_ANLPAR);
  360. int want;
  361. if (nego & PHY_ANLPAR_TXFD) {
  362. want = 0x3;
  363. printf("MII: 100Base-TX, Full Duplex\n");
  364. } else if (nego & PHY_ANLPAR_TX) {
  365. want = 0x1;
  366. printf("MII: 100Base-TX, Half Duplex\n");
  367. } else if (nego & PHY_ANLPAR_10FD) {
  368. want = 0x2;
  369. printf("MII: 10Base-T, Full Duplex\n");
  370. } else if (nego & PHY_ANLPAR_10) {
  371. want = 0x0;
  372. printf("MII: 10Base-T, Half Duplex\n");
  373. } else {
  374. printf("MII: Unknown link-foo! %x\n", nego);
  375. return;
  376. }
  377. if ((psr & 0x3) != want) {
  378. printf("MII: GT thinks %x, PHY thinks %x, restarting autoneg..\n",
  379. psr & 0x3, want);
  380. miiphy_write(GT6426x_MII_DEVNAME,ether_port_phy_addr[p->dev],0,
  381. miiphy_read_ret(ether_port_phy_addr[p->dev],0) | (1<<9));
  382. udelay(10000); /* the EVB's GT takes a while to notice phy
  383. went down and up */
  384. }
  385. }
  386. }
  387. #endif
  388. /**************************************************************************
  389. PROBE - Look for an adapter, this routine's visible to the outside
  390. ***************************************************************************/
  391. int
  392. gt6426x_eth_probe(void *v, bd_t *bis)
  393. {
  394. struct eth_device *wp = (struct eth_device *)v;
  395. struct eth_dev_s *p = (struct eth_dev_s *)wp->priv;
  396. int dev = p->dev;
  397. unsigned int reg_base = p->reg_base;
  398. unsigned long temp;
  399. int i;
  400. if (( dev < 0 ) || ( dev >= GAL_ETH_DEVS ))
  401. { /* This should never happen */
  402. printf("%s: Invalid device %d\n", __FUNCTION__, dev );
  403. return 0;
  404. }
  405. #ifdef DEBUG
  406. printf ("%s: initializing %s\n", __FUNCTION__, wp->name );
  407. printf ("\nCOMM_CONTROL = %08x , COMM_CONF = %08x\n",
  408. GTREGREAD(COMM_UNIT_ARBITER_CONTROL),
  409. GTREGREAD(COMM_UNIT_ARBITER_CONFIGURATION_REGISTER));
  410. #endif
  411. /* clear MIB counters */
  412. for(i=0;i<255; i++)
  413. temp=GTREGREAD(ETHERNET0_MIB_COUNTER_BASE + reg_base +i);
  414. #ifdef CONFIG_INTEL_LXT97X
  415. /* for intel LXT972 */
  416. /* led 1: 0x1=txact
  417. led 2: 0xc=link/rxact
  418. led 3: 0x2=rxact (N/C)
  419. strch: 0,2=30 ms, enable */
  420. miiphy_write(GT6426x_MII_DEVNAME,ether_port_phy_addr[p->dev], 20, 0x1c22);
  421. /* 2.7ns port rise time */
  422. /*miiphy_write(ether_port_phy_addr[p->dev], 30, 0x0<<10); */
  423. #else
  424. /* already set up in mpsc.c */
  425. /*GT_REG_WRITE(MAIN_ROUTING_REGISTER, 0x7ffe38); / b400 */
  426. /* already set up in sdram_init.S... */
  427. /* MPSC0, MPSC1, RMII */
  428. /*GT_REG_WRITE(SERIAL_PORT_MULTIPLEX, 0x1102); / f010 */
  429. #endif
  430. GT_REG_WRITE(ETHERNET_PHY_ADDRESS_REGISTER,
  431. ether_port_phy_addr[0] |
  432. (ether_port_phy_addr[1]<<5) |
  433. (ether_port_phy_addr[2]<<10)); /* 2000 */
  434. /* 13:12 - 10: 4x64bit burst (cache line size = 32 bytes)
  435. * 9 - 1: RIFB - interrupt on frame boundaries only
  436. * 6:7 - 00: big endian rx and tx
  437. * 5:2 - 1111: 15 retries */
  438. GT_REG_WRITE(ETHERNET0_SDMA_CONFIGURATION_REGISTER + reg_base,
  439. (2<<12) | (1<<9) | (0xf<<2) ); /* 2440 */
  440. #ifndef USE_SOFTWARE_CACHE_MANAGEMENT
  441. /* enable rx/tx desc/buffer cache snoop */
  442. GT_REG_READ(ETHERNET_0_ADDRESS_CONTROL_LOW + dev*0x20,
  443. &temp); /* f200 */
  444. temp|= (1<<6)| (1<<14)| (1<<22)| (1<<30);
  445. GT_REG_WRITE(ETHERNET_0_ADDRESS_CONTROL_LOW + dev*0x20,
  446. temp);
  447. #endif
  448. /* 31 28 27 24 23 20 19 16
  449. * 0000 0000 0000 0000 [0004]
  450. * 15 12 11 8 7 4 3 0
  451. * 1000 1101 0000 0000 [4d00]
  452. * 20 - 0=MII 1=RMII
  453. * 19 - 0=speed autoneg
  454. * 15:14 - framesize 1536 (GT6426x_ETH_BUF_SIZE)
  455. * 11 - no force link pass
  456. * 10 - 1=disable fctl autoneg
  457. * 8 - override prio ?? */
  458. temp = 0x00004d00;
  459. #ifndef CONFIG_ETHER_PORT_MII
  460. temp |= (1<<20); /* RMII */
  461. #endif
  462. /* set En */
  463. GT_REG_WRITE(ETHERNET0_PORT_CONFIGURATION_EXTEND_REGISTER + reg_base,
  464. temp); /* 2408 */
  465. /* hardcode E1 also? */
  466. /* -- according to dox, this is safer due to extra pulldowns? */
  467. if (dev<2) {
  468. GT_REG_WRITE(ETHERNET0_PORT_CONFIGURATION_EXTEND_REGISTER + (dev+1) * 0x400,
  469. temp); /* 2408 */
  470. }
  471. /* wake up MAC */ /* 2400 */
  472. GT_REG_READ(ETHERNET0_PORT_CONFIGURATION_REGISTER + reg_base, &temp);
  473. temp |= (1<<7); /* enable port */
  474. #ifdef CONFIG_GT_USE_MAC_HASH_TABLE
  475. temp |= (1<<12); /* hash size 1/2k */
  476. #else
  477. temp |= 1; /* promisc */
  478. #endif
  479. GT_REG_WRITE(ETHERNET0_PORT_CONFIGURATION_REGISTER + reg_base, temp);
  480. /* 2400 */
  481. #ifdef RESTART_AUTONEG
  482. check_phy_state(p);
  483. #endif
  484. printf("%s: Waiting for link up..\n", wp->name);
  485. temp = 10 * 1000;
  486. /* wait for link back up */
  487. while(!(GTREGREAD(ETHERNET0_PORT_STATUS_REGISTER + reg_base) & 8)
  488. && (--temp > 0)){
  489. udelay(1000); /* wait 1 ms */
  490. }
  491. if ( temp == 0) {
  492. printf("%s: Failed!\n", wp->name);
  493. return (0);
  494. }
  495. printf("%s: OK!\n", wp->name);
  496. p->tdn = 0;
  497. p->rdn = 0;
  498. p->eth_tx_desc[p->tdn].command_status = 0;
  499. /* Initialize Rx Side */
  500. for (temp = 0; temp < NR; temp++) {
  501. p->eth_rx_desc[temp].buff_pointer = (uchar *)p->eth_rx_buffer[temp];
  502. p->eth_rx_desc[temp].buff_size_byte_count = GT6426x_ETH_BUF_SIZE<<16;
  503. /* GT96100 Owner */
  504. p->eth_rx_desc[temp].command_status = 0x80000000;
  505. p->eth_rx_desc[temp].next_desc =
  506. (struct eth0_rx_desc_struct *)
  507. &p->eth_rx_desc[(temp+1)%NR].buff_size_byte_count;
  508. }
  509. FLUSH_DCACHE((unsigned int)&p->eth_tx_desc[0],
  510. (unsigned int)&p->eth_tx_desc[NR]);
  511. FLUSH_DCACHE((unsigned int)&p->eth_rx_desc[0],
  512. (unsigned int)&p->eth_rx_desc[NR]);
  513. GT_REG_WRITE(ETHERNET0_CURRENT_TX_DESCRIPTOR_POINTER0 + reg_base,
  514. (unsigned int) p->eth_tx_desc);
  515. GT_REG_WRITE(ETHERNET0_FIRST_RX_DESCRIPTOR_POINTER0 + reg_base,
  516. (unsigned int) p->eth_rx_desc);
  517. GT_REG_WRITE(ETHERNET0_CURRENT_RX_DESCRIPTOR_POINTER0 + reg_base,
  518. (unsigned int) p->eth_rx_desc);
  519. #ifdef DEBUG
  520. printf ("\nRx descriptor pointer is %08x %08x\n",
  521. GTREGREAD(ETHERNET0_FIRST_RX_DESCRIPTOR_POINTER0 + reg_base),
  522. GTREGREAD(ETHERNET0_CURRENT_RX_DESCRIPTOR_POINTER0 + reg_base));
  523. printf ("\n\n%08x %08x\n",
  524. (unsigned int)p->eth_rx_desc,p->eth_rx_desc[0].command_status);
  525. printf ("Descriptor dump:\n");
  526. printf ("cmd status: %08x\n",p->eth_rx_desc[0].command_status);
  527. printf ("byte_count: %08x\n",p->eth_rx_desc[0].buff_size_byte_count);
  528. printf ("buff_ptr: %08x\n",(unsigned int)p->eth_rx_desc[0].buff_pointer);
  529. printf ("next_desc: %08x\n\n",(unsigned int)p->eth_rx_desc[0].next_desc);
  530. printf ("%08x\n",*(unsigned int *) ((unsigned int)p->eth_rx_desc + 0x0));
  531. printf ("%08x\n",*(unsigned int *) ((unsigned int)p->eth_rx_desc + 0x4));
  532. printf ("%08x\n",*(unsigned int *) ((unsigned int)p->eth_rx_desc + 0x8));
  533. printf ("%08x\n\n",
  534. *(unsigned int *) ((unsigned int)p->eth_rx_desc + 0xc));
  535. #endif
  536. #ifdef DEBUG
  537. gt6426x_dump_mii(bis,ether_port_phy_addr[p->dev]);
  538. #endif
  539. #ifdef CONFIG_GT_USE_MAC_HASH_TABLE
  540. {
  541. unsigned int hashtable_base;
  542. u8 *b = (u8 *)(wp->enetaddr);
  543. u32 macH, macL;
  544. /* twist the MAC up into the way the discovery wants it */
  545. macH= (b[0]<<8) | b[1];
  546. macL= (b[2]<<24) | (b[3]<<16) | (b[4]<<8) | b[5];
  547. /* mode 0, size 0x800 */
  548. hashtable_base =initAddressTable(dev,0,1);
  549. if(!hashtable_base) {
  550. printf("initAddressTable failed\n");
  551. return 0;
  552. }
  553. addAddressTableEntry(dev, macH, macL, 1, 0);
  554. GT_REG_WRITE(ETHERNET0_HASH_TABLE_POINTER_REGISTER + reg_base,
  555. hashtable_base);
  556. }
  557. #endif
  558. /* Start Rx*/
  559. GT_REG_WRITE(ETHERNET0_SDMA_COMMAND_REGISTER + reg_base, 0x00000080);
  560. printf("%s: gt6426x eth device %d init success \n", wp->name, dev );
  561. return 1;
  562. }
  563. /* enter all the galileo ethernet devs into MULTI-BOOT */
  564. void
  565. gt6426x_eth_initialize(bd_t *bis)
  566. {
  567. struct eth_device *dev;
  568. struct eth_dev_s *p;
  569. int devnum, x, temp;
  570. char *s, *e, buf[64];
  571. #ifdef DEBUG
  572. printf( "\n%s\n", __FUNCTION );
  573. #endif
  574. for (devnum = 0; devnum < GAL_ETH_DEVS; devnum++) {
  575. dev = calloc(sizeof(*dev), 1);
  576. if (!dev) {
  577. printf( "%s: gal_enet%d allocation failure, %s\n",
  578. __FUNCTION__, devnum, "eth_device structure");
  579. return;
  580. }
  581. /* must be less than NAMESIZE (16) */
  582. sprintf(dev->name, "gal_enet%d", devnum);
  583. #ifdef DEBUG
  584. printf( "Initializing %s\n", dev->name );
  585. #endif
  586. /* Extract the MAC address from the environment */
  587. switch (devnum)
  588. {
  589. case 0: s = "ethaddr"; break;
  590. #if (GAL_ETH_DEVS > 1)
  591. case 1: s = "eth1addr"; break;
  592. #endif
  593. #if (GAL_ETH_DEVS > 2)
  594. case 2: s = "eth2addr"; break;
  595. #endif
  596. default: /* this should never happen */
  597. printf( "%s: Invalid device number %d\n",
  598. __FUNCTION__, devnum );
  599. return;
  600. }
  601. temp = getenv_r (s, buf, sizeof(buf));
  602. s = (temp > 0) ? buf : NULL;
  603. #ifdef DEBUG
  604. printf ("Setting MAC %d to %s\n", devnum, s );
  605. #endif
  606. for (x = 0; x < 6; ++x) {
  607. dev->enetaddr[x] = s ? simple_strtoul(s, &e, 16) : 0;
  608. if (s)
  609. s = (*e) ? e+1 : e;
  610. }
  611. dev->init = (void*)gt6426x_eth_probe;
  612. dev->halt = (void*)gt6426x_eth_reset;
  613. dev->send = (void*)gt6426x_eth_transmit;
  614. dev->recv = (void*)gt6426x_eth_poll;
  615. p = calloc( sizeof(*p), 1 );
  616. dev->priv = (void*)p;
  617. if (!p)
  618. {
  619. printf( "%s: %s allocation failure, %s\n",
  620. __FUNCTION__, dev->name, "Private Device Structure");
  621. free(dev);
  622. return;
  623. }
  624. p->dev = devnum;
  625. p->tdn=0;
  626. p->rdn=0;
  627. p->reg_base = devnum * ETHERNET_PORTS_DIFFERENCE_OFFSETS;
  628. p->eth_tx_desc =
  629. (eth0_tx_desc_single *)
  630. (((unsigned int) malloc(sizeof (eth0_tx_desc_single) *
  631. (NT+1)) & 0xfffffff0) + 0x10);
  632. if (!p)
  633. {
  634. printf( "%s: %s allocation failure, %s\n",
  635. __FUNCTION__, dev->name, "Tx Descriptor");
  636. free(dev);
  637. return;
  638. }
  639. p->eth_rx_desc =
  640. (eth0_rx_desc_single *)
  641. (((unsigned int) malloc(sizeof (eth0_rx_desc_single) *
  642. (NR+1)) & 0xfffffff0) + 0x10);
  643. if (!p->eth_rx_desc)
  644. {
  645. printf( "%s: %s allocation failure, %s\n",
  646. __FUNCTION__, dev->name, "Rx Descriptor");
  647. free(dev);
  648. free(p);
  649. return;
  650. }
  651. p->eth_tx_buffer =
  652. (char *) (((unsigned int) malloc(GT6426x_ETH_BUF_SIZE) & 0xfffffff0) + 0x10);
  653. if (!p->eth_tx_buffer)
  654. {
  655. printf( "%s: %s allocation failure, %s\n",
  656. __FUNCTION__, dev->name, "Tx Bufffer");
  657. free(dev);
  658. free(p);
  659. free(p->eth_rx_desc);
  660. return;
  661. }
  662. for (temp = 0 ; temp < NR ; temp ++) {
  663. p->eth_rx_buffer[temp] =
  664. (char *)
  665. (((unsigned int) malloc(GT6426x_ETH_BUF_SIZE) & 0xfffffff0) + 0x10);
  666. if (!p->eth_rx_buffer[temp])
  667. {
  668. printf( "%s: %s allocation failure, %s\n",
  669. __FUNCTION__, dev->name, "Rx Buffers");
  670. free(dev);
  671. free(p);
  672. free(p->eth_tx_buffer);
  673. free(p->eth_rx_desc);
  674. free(p->eth_tx_desc);
  675. while (temp >= 0)
  676. free(p->eth_rx_buffer[--temp]);
  677. return;
  678. }
  679. }
  680. eth_register(dev);
  681. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  682. miiphy_register(dev->name,
  683. gt6426x_miiphy_read, gt6426x_miiphy_write);
  684. #endif
  685. }
  686. }
  687. #endif