PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/boot/u-boot-1.1.4/cpu/ppc4xx/4xx_enet.c

http://snake-os.googlecode.com/
C | 1564 lines | 1098 code | 182 blank | 284 comment | 222 complexity | b0b8b0334355a9534f9f077ecd15bede MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, 0BSD, BSD-3-Clause, GPL-3.0, AGPL-1.0, CC-BY-SA-3.0
  1. /*-----------------------------------------------------------------------------+
  2. *
  3. * This source code has been made available to you by IBM on an AS-IS
  4. * basis. Anyone receiving this source is licensed under IBM
  5. * copyrights to use it in any way he or she deems fit, including
  6. * copying it, modifying it, compiling it, and redistributing it either
  7. * with or without modifications. No license under IBM patents or
  8. * patent applications is to be implied by the copyright license.
  9. *
  10. * Any user of this software should understand that IBM cannot provide
  11. * technical support for this software and will not be responsible for
  12. * any consequences resulting from the use of this software.
  13. *
  14. * Any person who transfers this source code or any derivative work
  15. * must include the IBM copyright notice, this paragraph, and the
  16. * preceding two paragraphs in the transferred software.
  17. *
  18. * COPYRIGHT I B M CORPORATION 1995
  19. * LICENSED MATERIAL - PROGRAM PROPERTY OF I B M
  20. *-----------------------------------------------------------------------------*/
  21. /*-----------------------------------------------------------------------------+
  22. *
  23. * File Name: enetemac.c
  24. *
  25. * Function: Device driver for the ethernet EMAC3 macro on the 405GP.
  26. *
  27. * Author: Mark Wisner
  28. *
  29. * Change Activity-
  30. *
  31. * Date Description of Change BY
  32. * --------- --------------------- ---
  33. * 05-May-99 Created MKW
  34. * 27-Jun-99 Clean up JWB
  35. * 16-Jul-99 Added MAL error recovery and better IP packet handling MKW
  36. * 29-Jul-99 Added Full duplex support MKW
  37. * 06-Aug-99 Changed names for Mal CR reg MKW
  38. * 23-Aug-99 Turned off SYE when running at 10Mbs MKW
  39. * 24-Aug-99 Marked descriptor empty after call_xlc MKW
  40. * 07-Sep-99 Set MAL RX buffer size reg to ENET_MAX_MTU_ALIGNED / 16 MCG
  41. * to avoid chaining maximum sized packets. Push starting
  42. * RX descriptor address up to the next cache line boundary.
  43. * 16-Jan-00 Added support for booting with IP of 0x0 MKW
  44. * 15-Mar-00 Updated enetInit() to enable broadcast addresses in the
  45. * EMAC_RXM register. JWB
  46. * 12-Mar-01 anne-sophie.harnois@nextream.fr
  47. * - Variables are compatible with those already defined in
  48. * include/net.h
  49. * - Receive buffer descriptor ring is used to send buffers
  50. * to the user
  51. * - Info print about send/received/handled packet number if
  52. * INFO_405_ENET is set
  53. * 17-Apr-01 stefan.roese@esd-electronics.com
  54. * - MAL reset in "eth_halt" included
  55. * - Enet speed and duplex output now in one line
  56. * 08-May-01 stefan.roese@esd-electronics.com
  57. * - MAL error handling added (eth_init called again)
  58. * 13-Nov-01 stefan.roese@esd-electronics.com
  59. * - Set IST bit in EMAC_M1 reg upon 100MBit or full duplex
  60. * 04-Jan-02 stefan.roese@esd-electronics.com
  61. * - Wait for PHY auto negotiation to complete added
  62. * 06-Feb-02 stefan.roese@esd-electronics.com
  63. * - Bug fixed in waiting for auto negotiation to complete
  64. * 26-Feb-02 stefan.roese@esd-electronics.com
  65. * - rx and tx buffer descriptors now allocated (no fixed address
  66. * used anymore)
  67. * 17-Jun-02 stefan.roese@esd-electronics.com
  68. * - MAL error debug printf 'M' removed (rx de interrupt may
  69. * occur upon many incoming packets with only 4 rx buffers).
  70. *-----------------------------------------------------------------------------*
  71. * 17-Nov-03 travis.sawyer@sandburst.com
  72. * - ported from 405gp_enet.c to utilized upto 4 EMAC ports
  73. * in the 440GX. This port should work with the 440GP
  74. * (2 EMACs) also
  75. * 15-Aug-05 sr@denx.de
  76. * - merged 405gp_enet.c and 440gx_enet.c to generic 4xx_enet.c
  77. now handling all 4xx cpu's.
  78. *-----------------------------------------------------------------------------*/
  79. #include <config.h>
  80. #include <common.h>
  81. #include <net.h>
  82. #include <asm/processor.h>
  83. #include <commproc.h>
  84. #include <ppc4xx.h>
  85. #include <ppc4xx_enet.h>
  86. #include <405_mal.h>
  87. #include <miiphy.h>
  88. #include <malloc.h>
  89. #include "vecnum.h"
  90. /*
  91. * Only compile for platform with AMCC EMAC ethernet controller and
  92. * network support enabled.
  93. * Remark: CONFIG_405 describes Xilinx PPC405 FPGA without EMAC controller!
  94. */
  95. #if (CONFIG_COMMANDS & CFG_CMD_NET) && !defined(CONFIG_405) && !defined(CONFIG_IOP480)
  96. #if !(defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII))
  97. #error "CONFIG_MII has to be defined!"
  98. #endif
  99. #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_NET_MULTI)
  100. #error "CONFIG_NET_MULTI has to be defined for NetConsole"
  101. #endif
  102. #define EMAC_RESET_TIMEOUT 1000 /* 1000 ms reset timeout */
  103. #define PHY_AUTONEGOTIATE_TIMEOUT 4000 /* 4000 ms autonegotiate timeout */
  104. /* Ethernet Transmit and Receive Buffers */
  105. /* AS.HARNOIS
  106. * In the same way ENET_MAX_MTU and ENET_MAX_MTU_ALIGNED are set from
  107. * PKTSIZE and PKTSIZE_ALIGN (include/net.h)
  108. */
  109. #define ENET_MAX_MTU PKTSIZE
  110. #define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
  111. /*-----------------------------------------------------------------------------+
  112. * Defines for MAL/EMAC interrupt conditions as reported in the UIC (Universal
  113. * Interrupt Controller).
  114. *-----------------------------------------------------------------------------*/
  115. #define MAL_UIC_ERR ( UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)
  116. #define MAL_UIC_DEF (UIC_MAL_RXEOB | MAL_UIC_ERR)
  117. #define EMAC_UIC_DEF UIC_ENET
  118. #define EMAC_UIC_DEF1 UIC_ENET1
  119. #define SEL_UIC_DEF(p) (p ? UIC_ENET1 : UIC_ENET )
  120. #undef INFO_4XX_ENET
  121. #define BI_PHYMODE_NONE 0
  122. #define BI_PHYMODE_ZMII 1
  123. #define BI_PHYMODE_RGMII 2
  124. /*-----------------------------------------------------------------------------+
  125. * Global variables. TX and RX descriptors and buffers.
  126. *-----------------------------------------------------------------------------*/
  127. /* IER globals */
  128. static uint32_t mal_ier;
  129. #if !defined(CONFIG_NET_MULTI)
  130. struct eth_device *emac0_dev = NULL;
  131. #endif
  132. /*
  133. * Get count of EMAC devices (doesn't have to be the max. possible number
  134. * supported by the cpu)
  135. */
  136. #if defined(CONFIG_HAS_ETH3)
  137. #define LAST_EMAC_NUM 4
  138. #elif defined(CONFIG_HAS_ETH2)
  139. #define LAST_EMAC_NUM 3
  140. #elif defined(CONFIG_HAS_ETH1)
  141. #define LAST_EMAC_NUM 2
  142. #else
  143. #define LAST_EMAC_NUM 1
  144. #endif
  145. /*-----------------------------------------------------------------------------+
  146. * Prototypes and externals.
  147. *-----------------------------------------------------------------------------*/
  148. static void enet_rcv (struct eth_device *dev, unsigned long malisr);
  149. int enetInt (struct eth_device *dev);
  150. static void mal_err (struct eth_device *dev, unsigned long isr,
  151. unsigned long uic, unsigned long maldef,
  152. unsigned long mal_errr);
  153. static void emac_err (struct eth_device *dev, unsigned long isr);
  154. extern int phy_setup_aneg (char *devname, unsigned char addr);
  155. extern int emac4xx_miiphy_read (char *devname, unsigned char addr,
  156. unsigned char reg, unsigned short *value);
  157. extern int emac4xx_miiphy_write (char *devname, unsigned char addr,
  158. unsigned char reg, unsigned short value);
  159. /*-----------------------------------------------------------------------------+
  160. | ppc_4xx_eth_halt
  161. | Disable MAL channel, and EMACn
  162. +-----------------------------------------------------------------------------*/
  163. static void ppc_4xx_eth_halt (struct eth_device *dev)
  164. {
  165. EMAC_4XX_HW_PST hw_p = dev->priv;
  166. uint32_t failsafe = 10000;
  167. out32 (EMAC_IER + hw_p->hw_addr, 0x00000000); /* disable emac interrupts */
  168. /* 1st reset MAL channel */
  169. /* Note: writing a 0 to a channel has no effect */
  170. #if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
  171. mtdcr (maltxcarr, (MAL_CR_MMSR >> (hw_p->devnum * 2)));
  172. #else
  173. mtdcr (maltxcarr, (MAL_CR_MMSR >> hw_p->devnum));
  174. #endif
  175. mtdcr (malrxcarr, (MAL_CR_MMSR >> hw_p->devnum));
  176. /* wait for reset */
  177. while (mfdcr (malrxcasr) & (MAL_CR_MMSR >> hw_p->devnum)) {
  178. udelay (1000); /* Delay 1 MS so as not to hammer the register */
  179. failsafe--;
  180. if (failsafe == 0)
  181. break;
  182. }
  183. /* EMAC RESET */
  184. out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
  185. #ifndef CONFIG_NETCONSOLE
  186. hw_p->print_speed = 1; /* print speed message again next time */
  187. #endif
  188. return;
  189. }
  190. #if defined (CONFIG_440GX)
  191. int ppc_4xx_eth_setup_bridge(int devnum, bd_t * bis)
  192. {
  193. unsigned long pfc1;
  194. unsigned long zmiifer;
  195. unsigned long rmiifer;
  196. mfsdr(sdr_pfc1, pfc1);
  197. pfc1 = SDR0_PFC1_EPS_DECODE(pfc1);
  198. zmiifer = 0;
  199. rmiifer = 0;
  200. switch (pfc1) {
  201. case 1:
  202. zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
  203. zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
  204. zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
  205. zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
  206. bis->bi_phymode[0] = BI_PHYMODE_ZMII;
  207. bis->bi_phymode[1] = BI_PHYMODE_ZMII;
  208. bis->bi_phymode[2] = BI_PHYMODE_ZMII;
  209. bis->bi_phymode[3] = BI_PHYMODE_ZMII;
  210. break;
  211. case 2:
  212. zmiifer = ZMII_FER_SMII << ZMII_FER_V(0);
  213. zmiifer = ZMII_FER_SMII << ZMII_FER_V(1);
  214. zmiifer = ZMII_FER_SMII << ZMII_FER_V(2);
  215. zmiifer = ZMII_FER_SMII << ZMII_FER_V(3);
  216. bis->bi_phymode[0] = BI_PHYMODE_ZMII;
  217. bis->bi_phymode[1] = BI_PHYMODE_ZMII;
  218. bis->bi_phymode[2] = BI_PHYMODE_ZMII;
  219. bis->bi_phymode[3] = BI_PHYMODE_ZMII;
  220. break;
  221. case 3:
  222. zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
  223. rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
  224. bis->bi_phymode[0] = BI_PHYMODE_ZMII;
  225. bis->bi_phymode[1] = BI_PHYMODE_NONE;
  226. bis->bi_phymode[2] = BI_PHYMODE_RGMII;
  227. bis->bi_phymode[3] = BI_PHYMODE_NONE;
  228. break;
  229. case 4:
  230. zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
  231. zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
  232. rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (2);
  233. rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (3);
  234. bis->bi_phymode[0] = BI_PHYMODE_ZMII;
  235. bis->bi_phymode[1] = BI_PHYMODE_ZMII;
  236. bis->bi_phymode[2] = BI_PHYMODE_RGMII;
  237. bis->bi_phymode[3] = BI_PHYMODE_RGMII;
  238. break;
  239. case 5:
  240. zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
  241. zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
  242. zmiifer |= ZMII_FER_SMII << ZMII_FER_V (2);
  243. rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
  244. bis->bi_phymode[0] = BI_PHYMODE_ZMII;
  245. bis->bi_phymode[1] = BI_PHYMODE_ZMII;
  246. bis->bi_phymode[2] = BI_PHYMODE_ZMII;
  247. bis->bi_phymode[3] = BI_PHYMODE_RGMII;
  248. break;
  249. case 6:
  250. zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
  251. zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
  252. rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
  253. bis->bi_phymode[0] = BI_PHYMODE_ZMII;
  254. bis->bi_phymode[1] = BI_PHYMODE_ZMII;
  255. bis->bi_phymode[2] = BI_PHYMODE_RGMII;
  256. break;
  257. case 0:
  258. default:
  259. zmiifer = ZMII_FER_MII << ZMII_FER_V(devnum);
  260. rmiifer = 0x0;
  261. bis->bi_phymode[0] = BI_PHYMODE_ZMII;
  262. bis->bi_phymode[1] = BI_PHYMODE_ZMII;
  263. bis->bi_phymode[2] = BI_PHYMODE_ZMII;
  264. bis->bi_phymode[3] = BI_PHYMODE_ZMII;
  265. break;
  266. }
  267. /* Ensure we setup mdio for this devnum and ONLY this devnum */
  268. zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
  269. out32 (ZMII_FER, zmiifer);
  270. out32 (RGMII_FER, rmiifer);
  271. return ((int)pfc1);
  272. }
  273. #endif
  274. static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
  275. {
  276. int i, j;
  277. unsigned long reg = 0;
  278. unsigned long msr;
  279. unsigned long speed;
  280. unsigned long duplex;
  281. unsigned long failsafe;
  282. unsigned mode_reg;
  283. unsigned short devnum;
  284. unsigned short reg_short;
  285. #if defined(CONFIG_440GX) || defined(CONFIG_440SP)
  286. sys_info_t sysinfo;
  287. #if defined(CONFIG_440GX)
  288. int ethgroup = -1;
  289. #endif
  290. #endif
  291. EMAC_4XX_HW_PST hw_p = dev->priv;
  292. /* before doing anything, figure out if we have a MAC address */
  293. /* if not, bail */
  294. if (memcmp (dev->enetaddr, "\0\0\0\0\0\0", 6) == 0) {
  295. printf("ERROR: ethaddr not set!\n");
  296. return -1;
  297. }
  298. #if defined(CONFIG_440GX) || defined(CONFIG_440SP)
  299. /* Need to get the OPB frequency so we can access the PHY */
  300. get_sys_info (&sysinfo);
  301. #endif
  302. msr = mfmsr ();
  303. mtmsr (msr & ~(MSR_EE)); /* disable interrupts */
  304. devnum = hw_p->devnum;
  305. #ifdef INFO_4XX_ENET
  306. /* AS.HARNOIS
  307. * We should have :
  308. * hw_p->stats.pkts_handled <= hw_p->stats.pkts_rx <= hw_p->stats.pkts_handled+PKTBUFSRX
  309. * In the most cases hw_p->stats.pkts_handled = hw_p->stats.pkts_rx, but it
  310. * is possible that new packets (without relationship with
  311. * current transfer) have got the time to arrived before
  312. * netloop calls eth_halt
  313. */
  314. printf ("About preceeding transfer (eth%d):\n"
  315. "- Sent packet number %d\n"
  316. "- Received packet number %d\n"
  317. "- Handled packet number %d\n",
  318. hw_p->devnum,
  319. hw_p->stats.pkts_tx,
  320. hw_p->stats.pkts_rx, hw_p->stats.pkts_handled);
  321. hw_p->stats.pkts_tx = 0;
  322. hw_p->stats.pkts_rx = 0;
  323. hw_p->stats.pkts_handled = 0;
  324. #endif
  325. hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
  326. hw_p->rx_err_index = 0; /* Receive Error Index for rx_err_log */
  327. hw_p->rx_slot = 0; /* MAL Receive Slot */
  328. hw_p->rx_i_index = 0; /* Receive Interrupt Queue Index */
  329. hw_p->rx_u_index = 0; /* Receive User Queue Index */
  330. hw_p->tx_slot = 0; /* MAL Transmit Slot */
  331. hw_p->tx_i_index = 0; /* Transmit Interrupt Queue Index */
  332. hw_p->tx_u_index = 0; /* Transmit User Queue Index */
  333. #if defined(CONFIG_440) && !defined(CONFIG_440SP)
  334. /* set RMII mode */
  335. /* NOTE: 440GX spec states that mode is mutually exclusive */
  336. /* NOTE: Therefore, disable all other EMACS, since we handle */
  337. /* NOTE: only one emac at a time */
  338. reg = 0;
  339. out32 (ZMII_FER, 0);
  340. udelay (100);
  341. #if defined(CONFIG_440EP) || defined(CONFIG_440GR)
  342. out32 (ZMII_FER, (ZMII_FER_RMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
  343. #elif defined(CONFIG_440GX)
  344. ethgroup = ppc_4xx_eth_setup_bridge(devnum, bis);
  345. #elif defined(CONFIG_440GP)
  346. /* set RMII mode */
  347. out32 (ZMII_FER, ZMII_RMII | ZMII_MDI0);
  348. #else
  349. if ((devnum == 0) || (devnum == 1)) {
  350. out32 (ZMII_FER, (ZMII_FER_SMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
  351. }
  352. else { /* ((devnum == 2) || (devnum == 3)) */
  353. out32 (ZMII_FER, ZMII_FER_MDI << ZMII_FER_V (devnum));
  354. out32 (RGMII_FER, ((RGMII_FER_RGMII << RGMII_FER_V (2)) |
  355. (RGMII_FER_RGMII << RGMII_FER_V (3))));
  356. }
  357. #endif
  358. out32 (ZMII_SSR, ZMII_SSR_SP << ZMII_SSR_V(devnum));
  359. #endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
  360. __asm__ volatile ("eieio");
  361. /* reset emac so we have access to the phy */
  362. out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
  363. __asm__ volatile ("eieio");
  364. failsafe = 1000;
  365. while ((in32 (EMAC_M0 + hw_p->hw_addr) & (EMAC_M0_SRST)) && failsafe) {
  366. udelay (1000);
  367. failsafe--;
  368. }
  369. #if defined(CONFIG_440GX) || defined(CONFIG_440SP)
  370. /* Whack the M1 register */
  371. mode_reg = 0x0;
  372. mode_reg &= ~0x00000038;
  373. if (sysinfo.freqOPB <= 50000000);
  374. else if (sysinfo.freqOPB <= 66666667)
  375. mode_reg |= EMAC_M1_OBCI_66;
  376. else if (sysinfo.freqOPB <= 83333333)
  377. mode_reg |= EMAC_M1_OBCI_83;
  378. else if (sysinfo.freqOPB <= 100000000)
  379. mode_reg |= EMAC_M1_OBCI_100;
  380. else
  381. mode_reg |= EMAC_M1_OBCI_GT100;
  382. out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
  383. #endif /* defined(CONFIG_440GX) || defined(CONFIG_440SP) */
  384. /* wait for PHY to complete auto negotiation */
  385. reg_short = 0;
  386. #ifndef CONFIG_CS8952_PHY
  387. switch (devnum) {
  388. case 0:
  389. reg = CONFIG_PHY_ADDR;
  390. break;
  391. #if defined (CONFIG_PHY1_ADDR)
  392. case 1:
  393. reg = CONFIG_PHY1_ADDR;
  394. break;
  395. #endif
  396. #if defined (CONFIG_440GX)
  397. case 2:
  398. reg = CONFIG_PHY2_ADDR;
  399. break;
  400. case 3:
  401. reg = CONFIG_PHY3_ADDR;
  402. break;
  403. #endif
  404. default:
  405. reg = CONFIG_PHY_ADDR;
  406. break;
  407. }
  408. bis->bi_phynum[devnum] = reg;
  409. #if defined(CONFIG_PHY_RESET)
  410. /*
  411. * Reset the phy, only if its the first time through
  412. * otherwise, just check the speeds & feeds
  413. */
  414. if (hw_p->first_init == 0) {
  415. miiphy_reset (dev->name, reg);
  416. #if defined(CONFIG_440GX) || defined(CONFIG_440SP)
  417. #if defined(CONFIG_CIS8201_PHY)
  418. /*
  419. * Cicada 8201 PHY needs to have an extended register whacked
  420. * for RGMII mode.
  421. */
  422. if ( ((devnum == 2) || (devnum ==3)) && (4 == ethgroup) ) {
  423. #if defined(CONFIG_CIS8201_SHORT_ETCH)
  424. miiphy_write (dev->name, reg, 23, 0x1300);
  425. #else
  426. miiphy_write (dev->name, reg, 23, 0x1000);
  427. #endif
  428. /*
  429. * Vitesse VSC8201/Cicada CIS8201 errata:
  430. * Interoperability problem with Intel 82547EI phys
  431. * This work around (provided by Vitesse) changes
  432. * the default timer convergence from 8ms to 12ms
  433. */
  434. miiphy_write (dev->name, reg, 0x1f, 0x2a30);
  435. miiphy_write (dev->name, reg, 0x08, 0x0200);
  436. miiphy_write (dev->name, reg, 0x1f, 0x52b5);
  437. miiphy_write (dev->name, reg, 0x02, 0x0004);
  438. miiphy_write (dev->name, reg, 0x01, 0x0671);
  439. miiphy_write (dev->name, reg, 0x00, 0x8fae);
  440. miiphy_write (dev->name, reg, 0x1f, 0x2a30);
  441. miiphy_write (dev->name, reg, 0x08, 0x0000);
  442. miiphy_write (dev->name, reg, 0x1f, 0x0000);
  443. /* end Vitesse/Cicada errata */
  444. }
  445. #endif
  446. #endif
  447. /* Start/Restart autonegotiation */
  448. phy_setup_aneg (dev->name, reg);
  449. udelay (1000);
  450. }
  451. #endif /* defined(CONFIG_PHY_RESET) */
  452. miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
  453. /*
  454. * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
  455. */
  456. if ((reg_short & PHY_BMSR_AUTN_ABLE)
  457. && !(reg_short & PHY_BMSR_AUTN_COMP)) {
  458. puts ("Waiting for PHY auto negotiation to complete");
  459. i = 0;
  460. while (!(reg_short & PHY_BMSR_AUTN_COMP)) {
  461. /*
  462. * Timeout reached ?
  463. */
  464. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  465. puts (" TIMEOUT !\n");
  466. break;
  467. }
  468. if ((i++ % 1000) == 0) {
  469. putc ('.');
  470. }
  471. udelay (1000); /* 1 ms */
  472. miiphy_read (dev->name, reg, PHY_BMSR, &reg_short);
  473. }
  474. puts (" done\n");
  475. udelay (500000); /* another 500 ms (results in faster booting) */
  476. }
  477. #endif /* #ifndef CONFIG_CS8952_PHY */
  478. speed = miiphy_speed (dev->name, reg);
  479. duplex = miiphy_duplex (dev->name, reg);
  480. if (hw_p->print_speed) {
  481. hw_p->print_speed = 0;
  482. printf ("ENET Speed is %d Mbps - %s duplex connection\n",
  483. (int) speed, (duplex == HALF) ? "HALF" : "FULL");
  484. }
  485. #if defined(CONFIG_440) && !defined(CONFIG_440SP)
  486. #if defined(CONFIG_440EP) || defined(CONFIG_440GR)
  487. mfsdr(sdr_mfr, reg);
  488. if (speed == 100) {
  489. reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_100M;
  490. } else {
  491. reg = (reg & ~SDR0_MFR_ZMII_MODE_MASK) | SDR0_MFR_ZMII_MODE_RMII_10M;
  492. }
  493. mtsdr(sdr_mfr, reg);
  494. #endif
  495. /* Set ZMII/RGMII speed according to the phy link speed */
  496. reg = in32 (ZMII_SSR);
  497. if ( (speed == 100) || (speed == 1000) )
  498. out32 (ZMII_SSR, reg | (ZMII_SSR_SP << ZMII_SSR_V (devnum)));
  499. else
  500. out32 (ZMII_SSR, reg & (~(ZMII_SSR_SP << ZMII_SSR_V (devnum))));
  501. if ((devnum == 2) || (devnum == 3)) {
  502. if (speed == 1000)
  503. reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
  504. else if (speed == 100)
  505. reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
  506. else
  507. reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
  508. out32 (RGMII_SSR, reg);
  509. }
  510. #endif /* defined(CONFIG_440) && !defined(CONFIG_440SP) */
  511. /* set the Mal configuration reg */
  512. #if defined(CONFIG_440GX) || defined(CONFIG_440SP)
  513. mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
  514. MAL_CR_PLBLT_DEFAULT | MAL_CR_EOPIE | 0x00330000);
  515. #else
  516. mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
  517. /* Errata 1.12: MAL_1 -- Disable MAL bursting */
  518. if (get_pvr() == PVR_440GP_RB) {
  519. mtdcr (malmcr, mfdcr(malmcr) & ~MAL_CR_PLBB);
  520. }
  521. #endif
  522. /* Free "old" buffers */
  523. if (hw_p->alloc_tx_buf)
  524. free (hw_p->alloc_tx_buf);
  525. if (hw_p->alloc_rx_buf)
  526. free (hw_p->alloc_rx_buf);
  527. /*
  528. * Malloc MAL buffer desciptors, make sure they are
  529. * aligned on cache line boundary size
  530. * (401/403/IOP480 = 16, 405 = 32)
  531. * and doesn't cross cache block boundaries.
  532. */
  533. hw_p->alloc_tx_buf =
  534. (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_TX_BUFF) +
  535. ((2 * CFG_CACHELINE_SIZE) - 2));
  536. if (NULL == hw_p->alloc_tx_buf)
  537. return -1;
  538. if (((int) hw_p->alloc_tx_buf & CACHELINE_MASK) != 0) {
  539. hw_p->tx =
  540. (mal_desc_t *) ((int) hw_p->alloc_tx_buf +
  541. CFG_CACHELINE_SIZE -
  542. ((int) hw_p->
  543. alloc_tx_buf & CACHELINE_MASK));
  544. } else {
  545. hw_p->tx = hw_p->alloc_tx_buf;
  546. }
  547. hw_p->alloc_rx_buf =
  548. (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_RX_BUFF) +
  549. ((2 * CFG_CACHELINE_SIZE) - 2));
  550. if (NULL == hw_p->alloc_rx_buf) {
  551. free(hw_p->alloc_tx_buf);
  552. hw_p->alloc_tx_buf = NULL;
  553. return -1;
  554. }
  555. if (((int) hw_p->alloc_rx_buf & CACHELINE_MASK) != 0) {
  556. hw_p->rx =
  557. (mal_desc_t *) ((int) hw_p->alloc_rx_buf +
  558. CFG_CACHELINE_SIZE -
  559. ((int) hw_p->
  560. alloc_rx_buf & CACHELINE_MASK));
  561. } else {
  562. hw_p->rx = hw_p->alloc_rx_buf;
  563. }
  564. for (i = 0; i < NUM_TX_BUFF; i++) {
  565. hw_p->tx[i].ctrl = 0;
  566. hw_p->tx[i].data_len = 0;
  567. if (hw_p->first_init == 0) {
  568. hw_p->txbuf_ptr =
  569. (char *) malloc (ENET_MAX_MTU_ALIGNED);
  570. if (NULL == hw_p->txbuf_ptr) {
  571. free(hw_p->alloc_rx_buf);
  572. free(hw_p->alloc_tx_buf);
  573. hw_p->alloc_rx_buf = NULL;
  574. hw_p->alloc_tx_buf = NULL;
  575. for(j = 0; j < i; j++) {
  576. free(hw_p->tx[i].data_ptr);
  577. hw_p->tx[i].data_ptr = NULL;
  578. }
  579. }
  580. }
  581. hw_p->tx[i].data_ptr = hw_p->txbuf_ptr;
  582. if ((NUM_TX_BUFF - 1) == i)
  583. hw_p->tx[i].ctrl |= MAL_TX_CTRL_WRAP;
  584. hw_p->tx_run[i] = -1;
  585. #if 0
  586. printf ("TX_BUFF %d @ 0x%08lx\n", i,
  587. (ulong) hw_p->tx[i].data_ptr);
  588. #endif
  589. }
  590. for (i = 0; i < NUM_RX_BUFF; i++) {
  591. hw_p->rx[i].ctrl = 0;
  592. hw_p->rx[i].data_len = 0;
  593. /* rx[i].data_ptr = (char *) &rx_buff[i]; */
  594. hw_p->rx[i].data_ptr = (char *) NetRxPackets[i];
  595. if ((NUM_RX_BUFF - 1) == i)
  596. hw_p->rx[i].ctrl |= MAL_RX_CTRL_WRAP;
  597. hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
  598. hw_p->rx_ready[i] = -1;
  599. #if 0
  600. printf ("RX_BUFF %d @ 0x%08lx\n", i, (ulong) rx[i].data_ptr);
  601. #endif
  602. }
  603. reg = 0x00000000;
  604. reg |= dev->enetaddr[0]; /* set high address */
  605. reg = reg << 8;
  606. reg |= dev->enetaddr[1];
  607. out32 (EMAC_IAH + hw_p->hw_addr, reg);
  608. reg = 0x00000000;
  609. reg |= dev->enetaddr[2]; /* set low address */
  610. reg = reg << 8;
  611. reg |= dev->enetaddr[3];
  612. reg = reg << 8;
  613. reg |= dev->enetaddr[4];
  614. reg = reg << 8;
  615. reg |= dev->enetaddr[5];
  616. out32 (EMAC_IAL + hw_p->hw_addr, reg);
  617. switch (devnum) {
  618. case 1:
  619. /* setup MAL tx & rx channel pointers */
  620. #if defined (CONFIG_405EP) || defined (CONFIG_440EP) || defined (CONFIG_440GR)
  621. mtdcr (maltxctp2r, hw_p->tx);
  622. #else
  623. mtdcr (maltxctp1r, hw_p->tx);
  624. #endif
  625. #if defined(CONFIG_440)
  626. mtdcr (maltxbattr, 0x0);
  627. mtdcr (malrxbattr, 0x0);
  628. #endif
  629. mtdcr (malrxctp1r, hw_p->rx);
  630. /* set RX buffer size */
  631. mtdcr (malrcbs1, ENET_MAX_MTU_ALIGNED / 16);
  632. break;
  633. #if defined (CONFIG_440GX)
  634. case 2:
  635. /* setup MAL tx & rx channel pointers */
  636. mtdcr (maltxbattr, 0x0);
  637. mtdcr (malrxbattr, 0x0);
  638. mtdcr (maltxctp2r, hw_p->tx);
  639. mtdcr (malrxctp2r, hw_p->rx);
  640. /* set RX buffer size */
  641. mtdcr (malrcbs2, ENET_MAX_MTU_ALIGNED / 16);
  642. break;
  643. case 3:
  644. /* setup MAL tx & rx channel pointers */
  645. mtdcr (maltxbattr, 0x0);
  646. mtdcr (maltxctp3r, hw_p->tx);
  647. mtdcr (malrxbattr, 0x0);
  648. mtdcr (malrxctp3r, hw_p->rx);
  649. /* set RX buffer size */
  650. mtdcr (malrcbs3, ENET_MAX_MTU_ALIGNED / 16);
  651. break;
  652. #endif /* CONFIG_440GX */
  653. case 0:
  654. default:
  655. /* setup MAL tx & rx channel pointers */
  656. #if defined(CONFIG_440)
  657. mtdcr (maltxbattr, 0x0);
  658. mtdcr (malrxbattr, 0x0);
  659. #endif
  660. mtdcr (maltxctp0r, hw_p->tx);
  661. mtdcr (malrxctp0r, hw_p->rx);
  662. /* set RX buffer size */
  663. mtdcr (malrcbs0, ENET_MAX_MTU_ALIGNED / 16);
  664. break;
  665. }
  666. /* Enable MAL transmit and receive channels */
  667. #if defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR)
  668. mtdcr (maltxcasr, (MAL_TXRX_CASR >> (hw_p->devnum*2)));
  669. #else
  670. mtdcr (maltxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
  671. #endif
  672. mtdcr (malrxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
  673. /* set transmit enable & receive enable */
  674. out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_TXE | EMAC_M0_RXE);
  675. /* set receive fifo to 4k and tx fifo to 2k */
  676. mode_reg = in32 (EMAC_M1 + hw_p->hw_addr);
  677. mode_reg |= EMAC_M1_RFS_4K | EMAC_M1_TX_FIFO_2K;
  678. /* set speed */
  679. if (speed == _1000BASET) {
  680. #if defined(CONFIG_440SP)
  681. #define SDR0_PFC1_EM_1000 0x00200000
  682. unsigned long pfc1;
  683. mfsdr (sdr_pfc1, pfc1);
  684. pfc1 |= SDR0_PFC1_EM_1000;
  685. mtsdr (sdr_pfc1, pfc1);
  686. #endif
  687. mode_reg = mode_reg | EMAC_M1_MF_1000MBPS | EMAC_M1_IST;
  688. } else if (speed == _100BASET)
  689. mode_reg = mode_reg | EMAC_M1_MF_100MBPS | EMAC_M1_IST;
  690. else
  691. mode_reg = mode_reg & ~0x00C00000; /* 10 MBPS */
  692. if (duplex == FULL)
  693. mode_reg = mode_reg | 0x80000000 | EMAC_M1_IST;
  694. out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
  695. /* Enable broadcast and indvidual address */
  696. /* TBS: enabling runts as some misbehaved nics will send runts */
  697. out32 (EMAC_RXM + hw_p->hw_addr, EMAC_RMR_BAE | EMAC_RMR_IAE);
  698. /* we probably need to set the tx mode1 reg? maybe at tx time */
  699. /* set transmit request threshold register */
  700. out32 (EMAC_TRTR + hw_p->hw_addr, 0x18000000); /* 256 byte threshold */
  701. /* set receive low/high water mark register */
  702. #if defined(CONFIG_440)
  703. /* 440GP has a 64 byte burst length */
  704. out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x80009000);
  705. #else
  706. /* 405s have a 16 byte burst length */
  707. out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x0f002000);
  708. #endif /* defined(CONFIG_440) */
  709. out32 (EMAC_TXM1 + hw_p->hw_addr, 0xf8640000);
  710. /* Set fifo limit entry in tx mode 0 */
  711. out32 (EMAC_TXM0 + hw_p->hw_addr, 0x00000003);
  712. /* Frame gap set */
  713. out32 (EMAC_I_FRAME_GAP_REG + hw_p->hw_addr, 0x00000008);
  714. /* Set EMAC IER */
  715. hw_p->emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS | EMAC_ISR_ORE | EMAC_ISR_IRE;
  716. if (speed == _100BASET)
  717. hw_p->emac_ier = hw_p->emac_ier | EMAC_ISR_SYE;
  718. out32 (EMAC_ISR + hw_p->hw_addr, 0xffffffff); /* clear pending interrupts */
  719. out32 (EMAC_IER + hw_p->hw_addr, hw_p->emac_ier);
  720. if (hw_p->first_init == 0) {
  721. /*
  722. * Connect interrupt service routines
  723. */
  724. irq_install_handler (VECNUM_ETH0 + (hw_p->devnum * 2),
  725. (interrupt_handler_t *) enetInt, dev);
  726. }
  727. mtmsr (msr); /* enable interrupts again */
  728. hw_p->bis = bis;
  729. hw_p->first_init = 1;
  730. return (1);
  731. }
  732. static int ppc_4xx_eth_send (struct eth_device *dev, volatile void *ptr,
  733. int len)
  734. {
  735. struct enet_frame *ef_ptr;
  736. ulong time_start, time_now;
  737. unsigned long temp_txm0;
  738. EMAC_4XX_HW_PST hw_p = dev->priv;
  739. ef_ptr = (struct enet_frame *) ptr;
  740. /*-----------------------------------------------------------------------+
  741. * Copy in our address into the frame.
  742. *-----------------------------------------------------------------------*/
  743. (void) memcpy (ef_ptr->source_addr, dev->enetaddr, ENET_ADDR_LENGTH);
  744. /*-----------------------------------------------------------------------+
  745. * If frame is too long or too short, modify length.
  746. *-----------------------------------------------------------------------*/
  747. /* TBS: where does the fragment go???? */
  748. if (len > ENET_MAX_MTU)
  749. len = ENET_MAX_MTU;
  750. /* memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
  751. memcpy ((void *) hw_p->txbuf_ptr, (const void *) ptr, len);
  752. /*-----------------------------------------------------------------------+
  753. * set TX Buffer busy, and send it
  754. *-----------------------------------------------------------------------*/
  755. hw_p->tx[hw_p->tx_slot].ctrl = (MAL_TX_CTRL_LAST |
  756. EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
  757. ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
  758. if ((NUM_TX_BUFF - 1) == hw_p->tx_slot)
  759. hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
  760. hw_p->tx[hw_p->tx_slot].data_len = (short) len;
  761. hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_READY;
  762. __asm__ volatile ("eieio");
  763. out32 (EMAC_TXM0 + hw_p->hw_addr,
  764. in32 (EMAC_TXM0 + hw_p->hw_addr) | EMAC_TXM0_GNP0);
  765. #ifdef INFO_4XX_ENET
  766. hw_p->stats.pkts_tx++;
  767. #endif
  768. /*-----------------------------------------------------------------------+
  769. * poll unitl the packet is sent and then make sure it is OK
  770. *-----------------------------------------------------------------------*/
  771. time_start = get_timer (0);
  772. while (1) {
  773. temp_txm0 = in32 (EMAC_TXM0 + hw_p->hw_addr);
  774. /* loop until either TINT turns on or 3 seconds elapse */
  775. if ((temp_txm0 & EMAC_TXM0_GNP0) != 0) {
  776. /* transmit is done, so now check for errors
  777. * If there is an error, an interrupt should
  778. * happen when we return
  779. */
  780. time_now = get_timer (0);
  781. if ((time_now - time_start) > 3000) {
  782. return (-1);
  783. }
  784. } else {
  785. return (len);
  786. }
  787. }
  788. }
  789. #if defined (CONFIG_440)
  790. #if defined(CONFIG_440SP)
  791. /*
  792. * Hack: On 440SP all enet irq sources are located on UIC1
  793. * Needs some cleanup. --sr
  794. */
  795. #define UIC0MSR uic1msr
  796. #define UIC0SR uic1sr
  797. #else
  798. #define UIC0MSR uic0msr
  799. #define UIC0SR uic0sr
  800. #endif
  801. int enetInt (struct eth_device *dev)
  802. {
  803. int serviced;
  804. int rc = -1; /* default to not us */
  805. unsigned long mal_isr;
  806. unsigned long emac_isr = 0;
  807. unsigned long mal_rx_eob;
  808. unsigned long my_uic0msr, my_uic1msr;
  809. #if defined(CONFIG_440GX)
  810. unsigned long my_uic2msr;
  811. #endif
  812. EMAC_4XX_HW_PST hw_p;
  813. /*
  814. * Because the mal is generic, we need to get the current
  815. * eth device
  816. */
  817. #if defined(CONFIG_NET_MULTI)
  818. dev = eth_get_dev();
  819. #else
  820. dev = emac0_dev;
  821. #endif
  822. hw_p = dev->priv;
  823. /* enter loop that stays in interrupt code until nothing to service */
  824. do {
  825. serviced = 0;
  826. my_uic0msr = mfdcr (UIC0MSR);
  827. my_uic1msr = mfdcr (uic1msr);
  828. #if defined(CONFIG_440GX)
  829. my_uic2msr = mfdcr (uic2msr);
  830. #endif
  831. if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
  832. && !(my_uic1msr & (UIC_ETH0 | UIC_ETH1 | UIC_MS | UIC_MTDE | UIC_MRDE))) {
  833. /* not for us */
  834. return (rc);
  835. }
  836. #if defined (CONFIG_440GX)
  837. if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
  838. && !(my_uic2msr & (UIC_ETH2 | UIC_ETH3))) {
  839. /* not for us */
  840. return (rc);
  841. }
  842. #endif
  843. /* get and clear controller status interrupts */
  844. /* look at Mal and EMAC interrupts */
  845. if ((my_uic0msr & (UIC_MRE | UIC_MTE))
  846. || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
  847. /* we have a MAL interrupt */
  848. mal_isr = mfdcr (malesr);
  849. /* look for mal error */
  850. if (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE)) {
  851. mal_err (dev, mal_isr, my_uic0msr,
  852. MAL_UIC_DEF, MAL_UIC_ERR);
  853. serviced = 1;
  854. rc = 0;
  855. }
  856. }
  857. /* port by port dispatch of emac interrupts */
  858. if (hw_p->devnum == 0) {
  859. if (UIC_ETH0 & my_uic1msr) { /* look for EMAC errors */
  860. emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
  861. if ((hw_p->emac_ier & emac_isr) != 0) {
  862. emac_err (dev, emac_isr);
  863. serviced = 1;
  864. rc = 0;
  865. }
  866. }
  867. if ((hw_p->emac_ier & emac_isr)
  868. || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
  869. mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
  870. mtdcr (uic1sr, UIC_ETH0 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
  871. return (rc); /* we had errors so get out */
  872. }
  873. }
  874. #if !defined(CONFIG_440SP)
  875. if (hw_p->devnum == 1) {
  876. if (UIC_ETH1 & my_uic1msr) { /* look for EMAC errors */
  877. emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
  878. if ((hw_p->emac_ier & emac_isr) != 0) {
  879. emac_err (dev, emac_isr);
  880. serviced = 1;
  881. rc = 0;
  882. }
  883. }
  884. if ((hw_p->emac_ier & emac_isr)
  885. || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
  886. mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
  887. mtdcr (uic1sr, UIC_ETH1 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
  888. return (rc); /* we had errors so get out */
  889. }
  890. }
  891. #if defined (CONFIG_440GX)
  892. if (hw_p->devnum == 2) {
  893. if (UIC_ETH2 & my_uic2msr) { /* look for EMAC errors */
  894. emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
  895. if ((hw_p->emac_ier & emac_isr) != 0) {
  896. emac_err (dev, emac_isr);
  897. serviced = 1;
  898. rc = 0;
  899. }
  900. }
  901. if ((hw_p->emac_ier & emac_isr)
  902. || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
  903. mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
  904. mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
  905. mtdcr (uic2sr, UIC_ETH2);
  906. return (rc); /* we had errors so get out */
  907. }
  908. }
  909. if (hw_p->devnum == 3) {
  910. if (UIC_ETH3 & my_uic2msr) { /* look for EMAC errors */
  911. emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
  912. if ((hw_p->emac_ier & emac_isr) != 0) {
  913. emac_err (dev, emac_isr);
  914. serviced = 1;
  915. rc = 0;
  916. }
  917. }
  918. if ((hw_p->emac_ier & emac_isr)
  919. || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
  920. mtdcr (UIC0SR, UIC_MRE | UIC_MTE); /* Clear */
  921. mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
  922. mtdcr (uic2sr, UIC_ETH3);
  923. return (rc); /* we had errors so get out */
  924. }
  925. }
  926. #endif /* CONFIG_440GX */
  927. #endif /* !CONFIG_440SP */
  928. /* handle MAX TX EOB interrupt from a tx */
  929. if (my_uic0msr & UIC_MTE) {
  930. mal_rx_eob = mfdcr (maltxeobisr);
  931. mtdcr (maltxeobisr, mal_rx_eob);
  932. mtdcr (UIC0SR, UIC_MTE);
  933. }
  934. /* handle MAL RX EOB interupt from a receive */
  935. /* check for EOB on valid channels */
  936. if (my_uic0msr & UIC_MRE) {
  937. mal_rx_eob = mfdcr (malrxeobisr);
  938. if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
  939. /* clear EOB
  940. mtdcr(malrxeobisr, mal_rx_eob); */
  941. enet_rcv (dev, emac_isr);
  942. /* indicate that we serviced an interrupt */
  943. serviced = 1;
  944. rc = 0;
  945. }
  946. }
  947. mtdcr (UIC0SR, UIC_MRE); /* Clear */
  948. mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
  949. switch (hw_p->devnum) {
  950. case 0:
  951. mtdcr (uic1sr, UIC_ETH0);
  952. break;
  953. case 1:
  954. mtdcr (uic1sr, UIC_ETH1);
  955. break;
  956. #if defined (CONFIG_440GX)
  957. case 2:
  958. mtdcr (uic2sr, UIC_ETH2);
  959. break;
  960. case 3:
  961. mtdcr (uic2sr, UIC_ETH3);
  962. break;
  963. #endif /* CONFIG_440GX */
  964. default:
  965. break;
  966. }
  967. } while (serviced);
  968. return (rc);
  969. }
  970. #else /* CONFIG_440 */
  971. int enetInt (struct eth_device *dev)
  972. {
  973. int serviced;
  974. int rc = -1; /* default to not us */
  975. unsigned long mal_isr;
  976. unsigned long emac_isr = 0;
  977. unsigned long mal_rx_eob;
  978. unsigned long my_uicmsr;
  979. EMAC_4XX_HW_PST hw_p;
  980. /*
  981. * Because the mal is generic, we need to get the current
  982. * eth device
  983. */
  984. #if defined(CONFIG_NET_MULTI)
  985. dev = eth_get_dev();
  986. #else
  987. dev = emac0_dev;
  988. #endif
  989. hw_p = dev->priv;
  990. /* enter loop that stays in interrupt code until nothing to service */
  991. do {
  992. serviced = 0;
  993. my_uicmsr = mfdcr (uicmsr);
  994. if ((my_uicmsr & (MAL_UIC_DEF | EMAC_UIC_DEF)) == 0) { /* not for us */
  995. return (rc);
  996. }
  997. /* get and clear controller status interrupts */
  998. /* look at Mal and EMAC interrupts */
  999. if ((MAL_UIC_DEF & my_uicmsr) != 0) { /* we have a MAL interrupt */
  1000. mal_isr = mfdcr (malesr);
  1001. /* look for mal error */
  1002. if ((my_uicmsr & MAL_UIC_ERR) != 0) {
  1003. mal_err (dev, mal_isr, my_uicmsr, MAL_UIC_DEF, MAL_UIC_ERR);
  1004. serviced = 1;
  1005. rc = 0;
  1006. }
  1007. }
  1008. /* port by port dispatch of emac interrupts */
  1009. if ((SEL_UIC_DEF(hw_p->devnum) & my_uicmsr) != 0) { /* look for EMAC errors */
  1010. emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
  1011. if ((hw_p->emac_ier & emac_isr) != 0) {
  1012. emac_err (dev, emac_isr);
  1013. serviced = 1;
  1014. rc = 0;
  1015. }
  1016. }
  1017. if (((hw_p->emac_ier & emac_isr) != 0) || ((MAL_UIC_ERR & my_uicmsr) != 0)) {
  1018. mtdcr (uicsr, MAL_UIC_DEF | SEL_UIC_DEF(hw_p->devnum)); /* Clear */
  1019. return (rc); /* we had errors so get out */
  1020. }
  1021. /* handle MAX TX EOB interrupt from a tx */
  1022. if (my_uicmsr & UIC_MAL_TXEOB) {
  1023. mal_rx_eob = mfdcr (maltxeobisr);
  1024. mtdcr (maltxeobisr, mal_rx_eob);
  1025. mtdcr (uicsr, UIC_MAL_TXEOB);
  1026. }
  1027. /* handle MAL RX EOB interupt from a receive */
  1028. /* check for EOB on valid channels */
  1029. if (my_uicmsr & UIC_MAL_RXEOB)
  1030. {
  1031. mal_rx_eob = mfdcr (malrxeobisr);
  1032. if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
  1033. /* clear EOB
  1034. mtdcr(malrxeobisr, mal_rx_eob); */
  1035. enet_rcv (dev, emac_isr);
  1036. /* indicate that we serviced an interrupt */
  1037. serviced = 1;
  1038. rc = 0;
  1039. }
  1040. }
  1041. mtdcr (uicsr, MAL_UIC_DEF|EMAC_UIC_DEF|EMAC_UIC_DEF1); /* Clear */
  1042. }
  1043. while (serviced);
  1044. return (rc);
  1045. }
  1046. #endif /* CONFIG_440 */
  1047. /*-----------------------------------------------------------------------------+
  1048. * MAL Error Routine
  1049. *-----------------------------------------------------------------------------*/
  1050. static void mal_err (struct eth_device *dev, unsigned long isr,
  1051. unsigned long uic, unsigned long maldef,
  1052. unsigned long mal_errr)
  1053. {
  1054. EMAC_4XX_HW_PST hw_p = dev->priv;
  1055. mtdcr (malesr, isr); /* clear interrupt */
  1056. /* clear DE interrupt */
  1057. mtdcr (maltxdeir, 0xC0000000);
  1058. mtdcr (malrxdeir, 0x80000000);
  1059. #ifdef INFO_4XX_ENET
  1060. printf ("\nMAL error occured.... ISR = %lx UIC = = %lx MAL_DEF = %lx MAL_ERR= %lx \n", isr, uic, maldef, mal_errr);
  1061. #endif
  1062. eth_init (hw_p->bis); /* start again... */
  1063. }
  1064. /*-----------------------------------------------------------------------------+
  1065. * EMAC Error Routine
  1066. *-----------------------------------------------------------------------------*/
  1067. static void emac_err (struct eth_device *dev, unsigned long isr)
  1068. {
  1069. EMAC_4XX_HW_PST hw_p = dev->priv;
  1070. printf ("EMAC%d error occured.... ISR = %lx\n", hw_p->devnum, isr);
  1071. out32 (EMAC_ISR + hw_p->hw_addr, isr);
  1072. }
  1073. /*-----------------------------------------------------------------------------+
  1074. * enet_rcv() handles the ethernet receive data
  1075. *-----------------------------------------------------------------------------*/
  1076. static void enet_rcv (struct eth_device *dev, unsigned long malisr)
  1077. {
  1078. struct enet_frame *ef_ptr;
  1079. unsigned long data_len;
  1080. unsigned long rx_eob_isr;
  1081. EMAC_4XX_HW_PST hw_p = dev->priv;
  1082. int handled = 0;
  1083. int i;
  1084. int loop_count = 0;
  1085. rx_eob_isr = mfdcr (malrxeobisr);
  1086. if ((0x80000000 >> hw_p->devnum) & rx_eob_isr) {
  1087. /* clear EOB */
  1088. mtdcr (malrxeobisr, rx_eob_isr);
  1089. /* EMAC RX done */
  1090. while (1) { /* do all */
  1091. i = hw_p->rx_slot;
  1092. if ((MAL_RX_CTRL_EMPTY & hw_p->rx[i].ctrl)
  1093. || (loop_count >= NUM_RX_BUFF))
  1094. break;
  1095. loop_count++;
  1096. hw_p->rx_slot++;
  1097. if (NUM_RX_BUFF == hw_p->rx_slot)
  1098. hw_p->rx_slot = 0;
  1099. handled++;
  1100. data_len = (unsigned long) hw_p->rx[i].data_len; /* Get len */
  1101. if (data_len) {
  1102. if (data_len > ENET_MAX_MTU) /* Check len */
  1103. data_len = 0;
  1104. else {
  1105. if (EMAC_RX_ERRORS & hw_p->rx[i].ctrl) { /* Check Errors */
  1106. data_len = 0;
  1107. hw_p->stats.rx_err_log[hw_p->
  1108. rx_err_index]
  1109. = hw_p->rx[i].ctrl;
  1110. hw_p->rx_err_index++;
  1111. if (hw_p->rx_err_index ==
  1112. MAX_ERR_LOG)
  1113. hw_p->rx_err_index =
  1114. 0;
  1115. } /* emac_erros */
  1116. } /* data_len < max mtu */
  1117. } /* if data_len */
  1118. if (!data_len) { /* no data */
  1119. hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY; /* Free Recv Buffer */
  1120. hw_p->stats.data_len_err++; /* Error at Rx */
  1121. }
  1122. /* !data_len */
  1123. /* AS.HARNOIS */
  1124. /* Check if user has already eaten buffer */
  1125. /* if not => ERROR */
  1126. else if (hw_p->rx_ready[hw_p->rx_i_index] != -1) {
  1127. if (hw_p->is_receiving)
  1128. printf ("ERROR : Receive buffers are full!\n");
  1129. break;
  1130. } else {
  1131. hw_p->stats.rx_frames++;
  1132. hw_p->stats.rx += data_len;
  1133. ef_ptr = (struct enet_frame *) hw_p->rx[i].
  1134. data_ptr;
  1135. #ifdef INFO_4XX_ENET
  1136. hw_p->stats.pkts_rx++;
  1137. #endif
  1138. /* AS.HARNOIS
  1139. * use ring buffer
  1140. */
  1141. hw_p->rx_ready[hw_p->rx_i_index] = i;
  1142. hw_p->rx_i_index++;
  1143. if (NUM_RX_BUFF == hw_p->rx_i_index)
  1144. hw_p->rx_i_index = 0;
  1145. /* AS.HARNOIS
  1146. * free receive buffer only when
  1147. * buffer has been handled (eth_rx)
  1148. rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
  1149. */
  1150. } /* if data_len */
  1151. } /* while */
  1152. } /* if EMACK_RXCHL */
  1153. }
  1154. static int ppc_4xx_eth_rx (struct eth_device *dev)
  1155. {
  1156. int length;
  1157. int user_index;
  1158. unsigned long msr;
  1159. EMAC_4XX_HW_PST hw_p = dev->priv;
  1160. hw_p->is_receiving = 1; /* tell driver */
  1161. for (;;) {
  1162. /* AS.HARNOIS
  1163. * use ring buffer and
  1164. * get index from rx buffer desciptor queue
  1165. */
  1166. user_index = hw_p->rx_ready[hw_p->rx_u_index];
  1167. if (user_index == -1) {
  1168. length = -1;
  1169. break; /* nothing received - leave for() loop */
  1170. }
  1171. msr = mfmsr ();
  1172. mtmsr (msr & ~(MSR_EE));
  1173. length = hw_p->rx[user_index].data_len;
  1174. /* Pass the packet up to the protocol layers. */
  1175. /* NetReceive(NetRxPackets[rxIdx], length - 4); */
  1176. /* NetReceive(NetRxPackets[i], length); */
  1177. NetReceive (NetRxPackets[user_index], length - 4);
  1178. /* Free Recv Buffer */
  1179. hw_p->rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
  1180. /* Free rx buffer descriptor queue */
  1181. hw_p->rx_ready[hw_p->rx_u_index] = -1;
  1182. hw_p->rx_u_index++;
  1183. if (NUM_RX_BUFF == hw_p->rx_u_index)
  1184. hw_p->rx_u_index = 0;
  1185. #ifdef INFO_4XX_ENET
  1186. hw_p->stats.pkts_handled++;
  1187. #endif
  1188. mtmsr (msr); /* Enable IRQ's */
  1189. }
  1190. hw_p->is_receiving = 0; /* tell driver */
  1191. return length;
  1192. }
  1193. int ppc_4xx_eth_initialize (bd_t * bis)
  1194. {
  1195. static int virgin = 0;
  1196. struct eth_device *dev;
  1197. int eth_num = 0;
  1198. EMAC_4XX_HW_PST hw = NULL;
  1199. #if defined(CONFIG_440GX)
  1200. unsigned long pfc1;
  1201. mfsdr (sdr_pfc1, pfc1);
  1202. pfc1 &= ~(0x01e00000);
  1203. pfc1 |= 0x01200000;
  1204. mtsdr (sdr_pfc1, pfc1);
  1205. #endif
  1206. /* set phy num and mode */
  1207. bis->bi_phynum[0] = CONFIG_PHY_ADDR;
  1208. #if defined(CONFIG_PHY1_ADDR)
  1209. bis->bi_phynum[1] = CONFIG_PHY1_ADDR;
  1210. #endif
  1211. #if defined(CONFIG_440GX)
  1212. bis->bi_phynum[2] = CONFIG_PHY2_ADDR;
  1213. bis->bi_phynum[3] = CONFIG_PHY3_ADDR;
  1214. bis->bi_phymode[0] = 0;
  1215. bis->bi_phymode[1] = 0;
  1216. bis->bi_phymode[2] = 2;
  1217. bis->bi_phymode[3] = 2;
  1218. #if defined (CONFIG_440GX)
  1219. ppc_4xx_eth_setup_bridge(0, bis);
  1220. #endif
  1221. #endif
  1222. for (eth_num = 0; eth_num < LAST_EMAC_NUM; eth_num++) {
  1223. /* See if we can actually bring up the interface, otherwise, skip it */
  1224. switch (eth_num) {
  1225. default: /* fall through */
  1226. case 0:
  1227. if (memcmp (bis->bi_enetaddr, "\0\0\0\0\0\0", 6) == 0) {
  1228. bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
  1229. continue;
  1230. }
  1231. break;
  1232. #ifdef CONFIG_HAS_ETH1
  1233. case 1:
  1234. if (memcmp (bis->bi_enet1addr, "\0\0\0\0\0\0", 6) == 0) {
  1235. bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
  1236. continue;
  1237. }
  1238. break;
  1239. #endif
  1240. #ifdef CONFIG_HAS_ETH2
  1241. case 2:
  1242. if (memcmp (bis->bi_enet2addr, "\0\0\0\0\0\0", 6) == 0) {
  1243. bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
  1244. continue;
  1245. }
  1246. break;
  1247. #endif
  1248. #ifdef CONFIG_HAS_ETH3
  1249. case 3:
  1250. if (memcmp (bis->bi_enet3addr, "\0\0\0\0\0\0", 6) == 0) {
  1251. bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
  1252. continue;
  1253. }
  1254. break;
  1255. #endif
  1256. }
  1257. /* Allocate device structure */
  1258. dev = (struct eth_device *) malloc (sizeof (*dev));
  1259. if (dev == NULL) {
  1260. printf ("ppc_4xx_eth_initialize: "
  1261. "Cannot allocate eth_device %d\n", eth_num);
  1262. return (-1);
  1263. }
  1264. memset(dev, 0, sizeof(*dev));
  1265. /* Allocate our private use data */
  1266. hw = (EMAC_4XX_HW_PST) malloc (sizeof (*hw));
  1267. if (hw == NULL) {
  1268. printf ("ppc_4xx_eth_initialize: "
  1269. "Cannot allocate private hw data for eth_device %d",
  1270. eth_num);
  1271. free (dev);
  1272. return (-1);
  1273. }
  1274. memset(hw, 0, sizeof(*hw));
  1275. switch (eth_num) {
  1276. default: /* fall through */
  1277. case 0:
  1278. hw->hw_addr = 0;
  1279. memcpy (dev->enetaddr, bis->bi_enetaddr, 6);
  1280. break;
  1281. #ifdef CONFIG_HAS_ETH1
  1282. case 1:
  1283. hw->hw_addr = 0x100;
  1284. memcpy (dev->enetaddr, bis->bi_enet1addr, 6);
  1285. break;
  1286. #endif
  1287. #ifdef CONFIG_HAS_ETH2
  1288. case 2:
  1289. hw->hw_addr = 0x400;
  1290. memcpy (dev->enetaddr, bis->bi_enet2addr, 6);
  1291. break;
  1292. #endif
  1293. #ifdef CONFIG_HAS_ETH3
  1294. case 3:
  1295. hw->hw_addr = 0x600;
  1296. memcpy (dev->enetaddr, bis->bi_enet3addr, 6);
  1297. break;
  1298. #endif
  1299. }
  1300. hw->devnum = eth_num;
  1301. hw->print_speed = 1;
  1302. sprintf (dev->name, "ppc_4xx_eth%d", eth_num);
  1303. dev->priv = (void *) hw;
  1304. dev->init = ppc_4xx_eth_init;
  1305. dev->halt = ppc_4xx_eth_halt;
  1306. dev->send = ppc_4xx_eth_send;
  1307. dev->recv = ppc_4xx_eth_rx;
  1308. if (0 == virgin) {
  1309. /* set the MAL IER ??? names may change with new spec ??? */
  1310. mal_ier =
  1311. MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
  1312. MAL_IER_OPBE | MAL_IER_PLBE;
  1313. mtdcr (malesr, 0xffffffff); /* clear pending interrupts */
  1314. mtdcr (maltxdeir, 0xffffffff); /* clear pending interrupts */
  1315. mtdcr (malrxdeir, 0xffffffff); /* clear pending interrupts */
  1316. mtdcr (malier, mal_ier);
  1317. /* install MAL interrupt handler */
  1318. irq_install_handler (VECNUM_MS,
  1319. (interrupt_handler_t *) enetInt,
  1320. dev);
  1321. irq_install_handler (VECNUM_MTE,
  1322. (interrupt_handler_t *) enetInt,
  1323. dev);
  1324. irq_install_handler (VECNUM_MRE,
  1325. (interrupt_handler_t *) enetInt,
  1326. dev);
  1327. irq_install_handler (VECNUM_TXDE,
  1328. (interrupt_handler_t *) enetInt,
  1329. dev);
  1330. irq_install_handler (VECNUM_RXDE,
  1331. (interrupt_handler_t *) enetInt,
  1332. dev);
  1333. virgin = 1;
  1334. }
  1335. #if defined(CONFIG_NET_MULTI)
  1336. eth_register (dev);
  1337. #else
  1338. emac0_dev = dev;
  1339. #endif
  1340. #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
  1341. miiphy_register (dev->name,
  1342. emac4xx_miiphy_read, emac4xx_miiphy_write);
  1343. #endif
  1344. } /* end for each supported device */
  1345. return (1);
  1346. }
  1347. #if !defined(CONFIG_NET_MULTI)
  1348. void eth_halt (void) {
  1349. if (emac0_dev) {
  1350. ppc_4xx_eth_halt(emac0_dev);
  1351. free(emac0_dev);
  1352. emac0_dev = NULL;
  1353. }
  1354. }
  1355. int eth_init (bd_t *bis)
  1356. {
  1357. ppc_4xx_eth_initialize(bis);
  1358. if (emac0_dev) {
  1359. return ppc_4xx_eth_init(emac0_dev, bis);
  1360. } else {
  1361. printf("ERROR: ethaddr not set!\n");
  1362. return -1;
  1363. }
  1364. }
  1365. int eth_send(volatile void *packet, int length)
  1366. {
  1367. return (ppc_4xx_eth_send(emac0_dev, packet, length));
  1368. }
  1369. int eth_rx(void)
  1370. {
  1371. return (ppc_4xx_eth_rx(emac0_dev));
  1372. }
  1373. int emac4xx_miiphy_initialize (bd_t * bis)
  1374. {
  1375. #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
  1376. miiphy_register ("ppc_4xx_eth0",
  1377. emac4xx_miiphy_read, emac4xx_miiphy_write);
  1378. #endif
  1379. return 0;
  1380. }
  1381. #endif /* !defined(CONFIG_NET_MULTI) */
  1382. #endif /* #if (CONFIG_COMMANDS & CFG_CMD_NET) */