tsec.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /*
  2. * tsec.c
  3. * Freescale Three Speed Ethernet Controller driver
  4. *
  5. * This software may be used and distributed according to the
  6. * terms of the GNU Public License, Version 2, incorporated
  7. * herein by reference.
  8. *
  9. * Copyright 2004 Freescale Semiconductor.
  10. * (C) Copyright 2003, Motorola, Inc.
  11. * maintained by Jon Loeliger (loeliger@freescale.com)
  12. * author Andy Fleming
  13. *
  14. */
  15. #include <config.h>
  16. #include <mpc85xx.h>
  17. #include <common.h>
  18. #include <malloc.h>
  19. #include <net.h>
  20. #include <command.h>
  21. #if defined(CONFIG_TSEC_ENET)
  22. #include "tsec.h"
  23. #define TX_BUF_CNT 2
  24. static uint rxIdx; /* index of the current RX buffer */
  25. static uint txIdx; /* index of the current TX buffer */
  26. typedef volatile struct rtxbd {
  27. txbd8_t txbd[TX_BUF_CNT];
  28. rxbd8_t rxbd[PKTBUFSRX];
  29. } RTXBD;
  30. struct tsec_info_struct {
  31. unsigned int phyaddr;
  32. unsigned int gigabit;
  33. unsigned int phyregidx;
  34. };
  35. /* The tsec_info structure contains 3 values which the
  36. * driver uses to determine how to operate a given ethernet
  37. * device. For now, the structure is initialized with the
  38. * knowledge that all current implementations have 2 TSEC
  39. * devices, and one FEC. The information needed is:
  40. * phyaddr - The address of the PHY which is attached to
  41. * the given device.
  42. *
  43. * gigabit - This variable indicates whether the device
  44. * supports gigabit speed ethernet
  45. *
  46. * phyregidx - This variable specifies which ethernet device
  47. * controls the MII Management registers which are connected
  48. * to the PHY. For 8540/8560, only TSEC1 (index 0) has
  49. * access to the PHYs, so all of the entries have "0".
  50. *
  51. * The values specified in the table are taken from the board's
  52. * config file in include/configs/. When implementing a new
  53. * board with ethernet capability, it is necessary to define:
  54. * TSEC1_PHY_ADDR
  55. * TSEC1_PHYIDX
  56. * TSEC2_PHY_ADDR
  57. * TSEC2_PHYIDX
  58. *
  59. * and for 8560:
  60. * FEC_PHY_ADDR
  61. * FEC_PHYIDX
  62. */
  63. static struct tsec_info_struct tsec_info[] = {
  64. #ifdef CONFIG_MPC85XX_TSEC1
  65. {TSEC1_PHY_ADDR, 1, TSEC1_PHYIDX},
  66. #else
  67. { 0, 0, 0},
  68. #endif
  69. #ifdef CONFIG_MPC85XX_TSEC2
  70. {TSEC2_PHY_ADDR, 1, TSEC2_PHYIDX},
  71. #else
  72. { 0, 0, 0},
  73. #endif
  74. #ifdef CONFIG_MPC85XX_FEC
  75. {FEC_PHY_ADDR, 0, FEC_PHYIDX},
  76. #else
  77. { 0, 0, 0},
  78. #endif
  79. };
  80. #define MAXCONTROLLERS 3
  81. static int relocated = 0;
  82. static struct tsec_private *privlist[MAXCONTROLLERS];
  83. #ifdef __GNUC__
  84. static RTXBD rtx __attribute__ ((aligned(8)));
  85. #else
  86. #error "rtx must be 64-bit aligned"
  87. #endif
  88. static int tsec_send(struct eth_device* dev, volatile void *packet, int length);
  89. static int tsec_recv(struct eth_device* dev);
  90. static int tsec_init(struct eth_device* dev, bd_t * bd);
  91. static void tsec_halt(struct eth_device* dev);
  92. static void init_registers(volatile tsec_t *regs);
  93. static void startup_tsec(struct eth_device *dev);
  94. static int init_phy(struct eth_device *dev);
  95. void write_phy_reg(struct tsec_private *priv, uint regnum, uint value);
  96. uint read_phy_reg(struct tsec_private *priv, uint regnum);
  97. struct phy_info * get_phy_info(struct eth_device *dev);
  98. void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd);
  99. static void adjust_link(struct eth_device *dev);
  100. static void relocate_cmds(void);
  101. /* Initialize device structure. Returns success if PHY
  102. * initialization succeeded (i.e. if it recognizes the PHY)
  103. */
  104. int tsec_initialize(bd_t *bis, int index)
  105. {
  106. struct eth_device* dev;
  107. int i;
  108. struct tsec_private *priv;
  109. dev = (struct eth_device*) malloc(sizeof *dev);
  110. if(NULL == dev)
  111. return 0;
  112. memset(dev, 0, sizeof *dev);
  113. priv = (struct tsec_private *) malloc(sizeof(*priv));
  114. if(NULL == priv)
  115. return 0;
  116. privlist[index] = priv;
  117. priv->regs = (volatile tsec_t *)(TSEC_BASE_ADDR + index*TSEC_SIZE);
  118. priv->phyregs = (volatile tsec_t *)(TSEC_BASE_ADDR +
  119. tsec_info[index].phyregidx*TSEC_SIZE);
  120. priv->phyaddr = tsec_info[index].phyaddr;
  121. priv->gigabit = tsec_info[index].gigabit;
  122. sprintf(dev->name, "ENET%d", index);
  123. dev->iobase = 0;
  124. dev->priv = priv;
  125. dev->init = tsec_init;
  126. dev->halt = tsec_halt;
  127. dev->send = tsec_send;
  128. dev->recv = tsec_recv;
  129. /* Tell u-boot to get the addr from the env */
  130. for(i=0;i<6;i++)
  131. dev->enetaddr[i] = 0;
  132. eth_register(dev);
  133. /* Reset the MAC */
  134. priv->regs->maccfg1 |= MACCFG1_SOFT_RESET;
  135. priv->regs->maccfg1 &= ~(MACCFG1_SOFT_RESET);
  136. /* Try to initialize PHY here, and return */
  137. return init_phy(dev);
  138. }
  139. /* Initializes data structures and registers for the controller,
  140. * and brings the interface up. Returns the link status, meaning
  141. * that it returns success if the link is up, failure otherwise.
  142. * This allows u-boot to find the first active controller. */
  143. int tsec_init(struct eth_device* dev, bd_t * bd)
  144. {
  145. uint tempval;
  146. char tmpbuf[MAC_ADDR_LEN];
  147. int i;
  148. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  149. volatile tsec_t *regs = priv->regs;
  150. /* Make sure the controller is stopped */
  151. tsec_halt(dev);
  152. /* Init MACCFG2. Defaults to GMII */
  153. regs->maccfg2 = MACCFG2_INIT_SETTINGS;
  154. /* Init ECNTRL */
  155. regs->ecntrl = ECNTRL_INIT_SETTINGS;
  156. /* Copy the station address into the address registers.
  157. * Backwards, because little endian MACS are dumb */
  158. for(i=0;i<MAC_ADDR_LEN;i++) {
  159. tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i];
  160. }
  161. (uint)(regs->macstnaddr1) = *((uint *)(tmpbuf));
  162. tempval = *((uint *)(tmpbuf +4));
  163. (uint)(regs->macstnaddr2) = tempval;
  164. /* reset the indices to zero */
  165. rxIdx = 0;
  166. txIdx = 0;
  167. /* Clear out (for the most part) the other registers */
  168. init_registers(regs);
  169. /* Ready the device for tx/rx */
  170. startup_tsec(dev);
  171. /* If there's no link, fail */
  172. return priv->link;
  173. }
  174. /* Write value to the device's PHY through the registers
  175. * specified in priv, modifying the register specified in regnum.
  176. * It will wait for the write to be done (or for a timeout to
  177. * expire) before exiting
  178. */
  179. void write_phy_reg(struct tsec_private *priv, uint regnum, uint value)
  180. {
  181. volatile tsec_t *regbase = priv->phyregs;
  182. uint phyid = priv->phyaddr;
  183. int timeout=1000000;
  184. regbase->miimadd = (phyid << 8) | regnum;
  185. regbase->miimcon = value;
  186. asm("msync");
  187. timeout=1000000;
  188. while((regbase->miimind & MIIMIND_BUSY) && timeout--);
  189. }
  190. /* Reads register regnum on the device's PHY through the
  191. * registers specified in priv. It lowers and raises the read
  192. * command, and waits for the data to become valid (miimind
  193. * notvalid bit cleared), and the bus to cease activity (miimind
  194. * busy bit cleared), and then returns the value
  195. */
  196. uint read_phy_reg(struct tsec_private *priv, uint regnum)
  197. {
  198. uint value;
  199. volatile tsec_t *regbase = priv->phyregs;
  200. uint phyid = priv->phyaddr;
  201. /* Put the address of the phy, and the register
  202. * number into MIIMADD */
  203. regbase->miimadd = (phyid << 8) | regnum;
  204. /* Clear the command register, and wait */
  205. regbase->miimcom = 0;
  206. asm("msync");
  207. /* Initiate a read command, and wait */
  208. regbase->miimcom = MIIM_READ_COMMAND;
  209. asm("msync");
  210. /* Wait for the the indication that the read is done */
  211. while((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY)));
  212. /* Grab the value read from the PHY */
  213. value = regbase->miimstat;
  214. return value;
  215. }
  216. /* Discover which PHY is attached to the device, and configure it
  217. * properly. If the PHY is not recognized, then return 0
  218. * (failure). Otherwise, return 1
  219. */
  220. static int init_phy(struct eth_device *dev)
  221. {
  222. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  223. struct phy_info *curphy;
  224. /* Assign a Physical address to the TBI */
  225. priv->regs->tbipa=TBIPA_VALUE;
  226. if(0 == relocated)
  227. relocate_cmds();
  228. /* Get the cmd structure corresponding to the attached
  229. * PHY */
  230. curphy = get_phy_info(dev);
  231. if(NULL == curphy) {
  232. printf("%s: No PHY found\n", dev->name);
  233. return 0;
  234. }
  235. priv->phyinfo = curphy;
  236. phy_run_commands(priv, priv->phyinfo->config);
  237. return 1;
  238. }
  239. /* Returns which value to write to the control register. */
  240. /* For 10/100, the value is slightly different */
  241. uint mii_cr_init(uint mii_reg, struct tsec_private *priv)
  242. {
  243. if(priv->gigabit)
  244. return MIIM_CONTROL_INIT;
  245. else
  246. return MIIM_CR_INIT;
  247. }
  248. /* Parse the status register for link, and then do
  249. * auto-negotiation */
  250. uint mii_parse_sr(uint mii_reg, struct tsec_private *priv)
  251. {
  252. uint timeout = TSEC_TIMEOUT;
  253. if(mii_reg & MIIM_STATUS_LINK)
  254. priv->link = 1;
  255. else
  256. priv->link = 0;
  257. if(priv->link) {
  258. while((!(mii_reg & MIIM_STATUS_AN_DONE)) && timeout--)
  259. mii_reg = read_phy_reg(priv, MIIM_STATUS);
  260. }
  261. return 0;
  262. }
  263. /* Parse the 88E1011's status register for speed and duplex
  264. * information */
  265. uint mii_parse_88E1011_psr(uint mii_reg, struct tsec_private *priv)
  266. {
  267. uint speed;
  268. if(mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
  269. priv->duplexity = 1;
  270. else
  271. priv->duplexity = 0;
  272. speed = (mii_reg &MIIM_88E1011_PHYSTAT_SPEED);
  273. switch(speed) {
  274. case MIIM_88E1011_PHYSTAT_GBIT:
  275. priv->speed = 1000;
  276. break;
  277. case MIIM_88E1011_PHYSTAT_100:
  278. priv->speed = 100;
  279. break;
  280. default:
  281. priv->speed = 10;
  282. }
  283. return 0;
  284. }
  285. /* Parse the cis8201's status register for speed and duplex
  286. * information */
  287. uint mii_parse_cis8201(uint mii_reg, struct tsec_private *priv)
  288. {
  289. uint speed;
  290. if(mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX)
  291. priv->duplexity = 1;
  292. else
  293. priv->duplexity = 0;
  294. speed = mii_reg & MIIM_CIS8201_AUXCONSTAT_SPEED;
  295. switch(speed) {
  296. case MIIM_CIS8201_AUXCONSTAT_GBIT:
  297. priv->speed = 1000;
  298. break;
  299. case MIIM_CIS8201_AUXCONSTAT_100:
  300. priv->speed = 100;
  301. break;
  302. default:
  303. priv->speed = 10;
  304. break;
  305. }
  306. return 0;
  307. }
  308. /* Parse the DM9161's status register for speed and duplex
  309. * information */
  310. uint mii_parse_dm9161_scsr(uint mii_reg, struct tsec_private *priv)
  311. {
  312. if(mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_100H))
  313. priv->speed = 100;
  314. else
  315. priv->speed = 10;
  316. if(mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_10F))
  317. priv->duplexity = 1;
  318. else
  319. priv->duplexity = 0;
  320. return 0;
  321. }
  322. /* Hack to write all 4 PHYs with the LED values */
  323. uint mii_cis8204_fixled(uint mii_reg, struct tsec_private *priv)
  324. {
  325. uint phyid;
  326. volatile tsec_t *regbase = priv->phyregs;
  327. int timeout=1000000;
  328. for(phyid=0;phyid<4;phyid++) {
  329. regbase->miimadd = (phyid << 8) | mii_reg;
  330. regbase->miimcon = MIIM_CIS8204_SLEDCON_INIT;
  331. asm("msync");
  332. timeout=1000000;
  333. while((regbase->miimind & MIIMIND_BUSY) && timeout--);
  334. }
  335. return MIIM_CIS8204_SLEDCON_INIT;
  336. }
  337. /* Initialized required registers to appropriate values, zeroing
  338. * those we don't care about (unless zero is bad, in which case,
  339. * choose a more appropriate value) */
  340. static void init_registers(volatile tsec_t *regs)
  341. {
  342. /* Clear IEVENT */
  343. regs->ievent = IEVENT_INIT_CLEAR;
  344. regs->imask = IMASK_INIT_CLEAR;
  345. regs->hash.iaddr0 = 0;
  346. regs->hash.iaddr1 = 0;
  347. regs->hash.iaddr2 = 0;
  348. regs->hash.iaddr3 = 0;
  349. regs->hash.iaddr4 = 0;
  350. regs->hash.iaddr5 = 0;
  351. regs->hash.iaddr6 = 0;
  352. regs->hash.iaddr7 = 0;
  353. regs->hash.gaddr0 = 0;
  354. regs->hash.gaddr1 = 0;
  355. regs->hash.gaddr2 = 0;
  356. regs->hash.gaddr3 = 0;
  357. regs->hash.gaddr4 = 0;
  358. regs->hash.gaddr5 = 0;
  359. regs->hash.gaddr6 = 0;
  360. regs->hash.gaddr7 = 0;
  361. regs->rctrl = 0x00000000;
  362. /* Init RMON mib registers */
  363. memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
  364. regs->rmon.cam1 = 0xffffffff;
  365. regs->rmon.cam2 = 0xffffffff;
  366. regs->mrblr = MRBLR_INIT_SETTINGS;
  367. regs->minflr = MINFLR_INIT_SETTINGS;
  368. regs->attr = ATTR_INIT_SETTINGS;
  369. regs->attreli = ATTRELI_INIT_SETTINGS;
  370. }
  371. /* Configure maccfg2 based on negotiated speed and duplex
  372. * reported by PHY handling code */
  373. static void adjust_link(struct eth_device *dev)
  374. {
  375. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  376. volatile tsec_t *regs = priv->regs;
  377. if(priv->link) {
  378. if(priv->duplexity != 0)
  379. regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
  380. else
  381. regs->maccfg2 &= ~(MACCFG2_FULL_DUPLEX);
  382. switch(priv->speed) {
  383. case 1000:
  384. regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF))
  385. | MACCFG2_GMII);
  386. break;
  387. case 100:
  388. case 10:
  389. regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF))
  390. | MACCFG2_MII);
  391. break;
  392. default:
  393. printf("%s: Speed was bad\n", dev->name);
  394. break;
  395. }
  396. printf("Speed: %d, %s duplex\n", priv->speed,
  397. (priv->duplexity) ? "full" : "half");
  398. } else {
  399. printf("%s: No link.\n", dev->name);
  400. }
  401. }
  402. /* Set up the buffers and their descriptors, and bring up the
  403. * interface */
  404. static void startup_tsec(struct eth_device *dev)
  405. {
  406. int i;
  407. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  408. volatile tsec_t *regs = priv->regs;
  409. /* Point to the buffer descriptors */
  410. regs->tbase = (unsigned int)(&rtx.txbd[txIdx]);
  411. regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
  412. /* Initialize the Rx Buffer descriptors */
  413. for (i = 0; i < PKTBUFSRX; i++) {
  414. rtx.rxbd[i].status = RXBD_EMPTY;
  415. rtx.rxbd[i].length = 0;
  416. rtx.rxbd[i].bufPtr = (uint)NetRxPackets[i];
  417. }
  418. rtx.rxbd[PKTBUFSRX -1].status |= RXBD_WRAP;
  419. /* Initialize the TX Buffer Descriptors */
  420. for(i=0; i<TX_BUF_CNT; i++) {
  421. rtx.txbd[i].status = 0;
  422. rtx.txbd[i].length = 0;
  423. rtx.txbd[i].bufPtr = 0;
  424. }
  425. rtx.txbd[TX_BUF_CNT -1].status |= TXBD_WRAP;
  426. /* Start up the PHY */
  427. phy_run_commands(priv, priv->phyinfo->startup);
  428. adjust_link(dev);
  429. /* Enable Transmit and Receive */
  430. regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
  431. /* Tell the DMA it is clear to go */
  432. regs->dmactrl |= DMACTRL_INIT_SETTINGS;
  433. regs->tstat = TSTAT_CLEAR_THALT;
  434. regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
  435. }
  436. /* This returns the status bits of the device. The return value
  437. * is never checked, and this is what the 8260 driver did, so we
  438. * do the same. Presumably, this would be zero if there were no
  439. * errors */
  440. static int tsec_send(struct eth_device* dev, volatile void *packet, int length)
  441. {
  442. int i;
  443. int result = 0;
  444. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  445. volatile tsec_t *regs = priv->regs;
  446. /* Find an empty buffer descriptor */
  447. for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
  448. if (i >= TOUT_LOOP) {
  449. debug ("%s: tsec: tx buffers full\n", dev->name);
  450. return result;
  451. }
  452. }
  453. rtx.txbd[txIdx].bufPtr = (uint)packet;
  454. rtx.txbd[txIdx].length = length;
  455. rtx.txbd[txIdx].status |= (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
  456. /* Tell the DMA to go */
  457. regs->tstat = TSTAT_CLEAR_THALT;
  458. /* Wait for buffer to be transmitted */
  459. for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
  460. if (i >= TOUT_LOOP) {
  461. debug ("%s: tsec: tx error\n", dev->name);
  462. return result;
  463. }
  464. }
  465. txIdx = (txIdx + 1) % TX_BUF_CNT;
  466. result = rtx.txbd[txIdx].status & TXBD_STATS;
  467. return result;
  468. }
  469. static int tsec_recv(struct eth_device* dev)
  470. {
  471. int length;
  472. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  473. volatile tsec_t *regs = priv->regs;
  474. while(!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
  475. length = rtx.rxbd[rxIdx].length;
  476. /* Send the packet up if there were no errors */
  477. if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
  478. NetReceive(NetRxPackets[rxIdx], length - 4);
  479. } else {
  480. printf("Got error %x\n",
  481. (rtx.rxbd[rxIdx].status & RXBD_STATS));
  482. }
  483. rtx.rxbd[rxIdx].length = 0;
  484. /* Set the wrap bit if this is the last element in the list */
  485. rtx.rxbd[rxIdx].status = RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
  486. rxIdx = (rxIdx + 1) % PKTBUFSRX;
  487. }
  488. if(regs->ievent&IEVENT_BSY) {
  489. regs->ievent = IEVENT_BSY;
  490. regs->rstat = RSTAT_CLEAR_RHALT;
  491. }
  492. return -1;
  493. }
  494. /* Stop the interface */
  495. static void tsec_halt(struct eth_device* dev)
  496. {
  497. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  498. volatile tsec_t *regs = priv->regs;
  499. regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
  500. regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS);
  501. while(!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC)));
  502. regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
  503. /* Shut down the PHY, as needed */
  504. phy_run_commands(priv, priv->phyinfo->shutdown);
  505. }
  506. struct phy_info phy_info_M88E1011S = {
  507. 0x01410c6,
  508. "Marvell 88E1011S",
  509. 4,
  510. (struct phy_cmd[]) { /* config */
  511. /* Reset and configure the PHY */
  512. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  513. {0x1d, 0x1f, NULL},
  514. {0x1e, 0x200c, NULL},
  515. {0x1d, 0x5, NULL},
  516. {0x1e, 0x0, NULL},
  517. {0x1e, 0x100, NULL},
  518. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  519. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  520. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  521. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  522. {miim_end,}
  523. },
  524. (struct phy_cmd[]) { /* startup */
  525. /* Status is read once to clear old link state */
  526. {MIIM_STATUS, miim_read, NULL},
  527. /* Auto-negotiate */
  528. {MIIM_STATUS, miim_read, &mii_parse_sr},
  529. /* Read the status */
  530. {MIIM_88E1011_PHY_STATUS, miim_read, &mii_parse_88E1011_psr},
  531. {miim_end,}
  532. },
  533. (struct phy_cmd[]) { /* shutdown */
  534. {miim_end,}
  535. },
  536. };
  537. struct phy_info phy_info_M88E1111S = {
  538. 0x01410cc,
  539. "Marvell 88E1111S",
  540. 4,
  541. (struct phy_cmd[]) { /* config */
  542. /* Reset and configure the PHY */
  543. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  544. {0x1d, 0x1f, NULL},
  545. {0x1e, 0x200c, NULL},
  546. {0x1d, 0x5, NULL},
  547. {0x1e, 0x0, NULL},
  548. {0x1e, 0x100, NULL},
  549. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  550. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  551. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  552. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  553. {miim_end,}
  554. },
  555. (struct phy_cmd[]) { /* startup */
  556. /* Status is read once to clear old link state */
  557. {MIIM_STATUS, miim_read, NULL},
  558. /* Auto-negotiate */
  559. {MIIM_STATUS, miim_read, &mii_parse_sr},
  560. /* Read the status */
  561. {MIIM_88E1011_PHY_STATUS, miim_read, &mii_parse_88E1011_psr},
  562. {miim_end,}
  563. },
  564. (struct phy_cmd[]) { /* shutdown */
  565. {miim_end,}
  566. },
  567. };
  568. struct phy_info phy_info_cis8204 = {
  569. 0x3f11,
  570. "Cicada Cis8204",
  571. 6,
  572. (struct phy_cmd[]) { /* config */
  573. /* Override PHY config settings */
  574. {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
  575. /* Configure some basic stuff */
  576. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  577. {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT, &mii_cis8204_fixled},
  578. {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT, NULL},
  579. {miim_end,}
  580. },
  581. (struct phy_cmd[]) { /* startup */
  582. /* Read the Status (2x to make sure link is right) */
  583. {MIIM_STATUS, miim_read, NULL},
  584. /* Auto-negotiate */
  585. {MIIM_STATUS, miim_read, &mii_parse_sr},
  586. /* Read the status */
  587. {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201},
  588. {miim_end,}
  589. },
  590. (struct phy_cmd[]) { /* shutdown */
  591. {miim_end,}
  592. },
  593. };
  594. /* Cicada 8201 */
  595. struct phy_info phy_info_cis8201 = {
  596. 0xfc41,
  597. "CIS8201",
  598. 4,
  599. (struct phy_cmd[]) { /* config */
  600. /* Override PHY config settings */
  601. {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
  602. /* Set up the interface mode */
  603. {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT, NULL},
  604. /* Configure some basic stuff */
  605. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  606. {miim_end,}
  607. },
  608. (struct phy_cmd[]) { /* startup */
  609. /* Read the Status (2x to make sure link is right) */
  610. {MIIM_STATUS, miim_read, NULL},
  611. /* Auto-negotiate */
  612. {MIIM_STATUS, miim_read, &mii_parse_sr},
  613. /* Read the status */
  614. {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201},
  615. {miim_end,}
  616. },
  617. (struct phy_cmd[]) { /* shutdown */
  618. {miim_end,}
  619. },
  620. };
  621. struct phy_info phy_info_dm9161 = {
  622. 0x0181b88,
  623. "Davicom DM9161E",
  624. 4,
  625. (struct phy_cmd[]) { /* config */
  626. {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
  627. /* Do not bypass the scrambler/descrambler */
  628. {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
  629. /* Clear 10BTCSR to default */
  630. {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT, NULL},
  631. /* Configure some basic stuff */
  632. {MIIM_CONTROL, MIIM_CR_INIT, NULL},
  633. /* Restart Auto Negotiation */
  634. {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
  635. {miim_end,}
  636. },
  637. (struct phy_cmd[]) { /* startup */
  638. /* Status is read once to clear old link state */
  639. {MIIM_STATUS, miim_read, NULL},
  640. /* Auto-negotiate */
  641. {MIIM_STATUS, miim_read, &mii_parse_sr},
  642. /* Read the status */
  643. {MIIM_DM9161_SCSR, miim_read, &mii_parse_dm9161_scsr},
  644. {miim_end,}
  645. },
  646. (struct phy_cmd[]) { /* shutdown */
  647. {miim_end,}
  648. },
  649. };
  650. static struct phy_info phy_info_lxt971 = {
  651. 0x0001378e,
  652. "LXT971",
  653. 4,
  654. (struct phy_cmd []) { /* config */
  655. { MIIM_CONTROL, MIIM_CONTROL_INIT, mii_cr_init }, /* autonegotiate */
  656. { miim_end, }
  657. },
  658. (struct phy_cmd []) { /* startup - enable interrupts */
  659. /* { 0x12, 0x00f2, NULL }, */
  660. { 0x14, 0xd422, NULL }, /* LED config */
  661. { MIIM_STATUS, miim_read, NULL },
  662. { MIIM_STATUS, miim_read, mii_parse_sr },
  663. { miim_end, }
  664. },
  665. (struct phy_cmd []) { /* shutdown - disable interrupts */
  666. { miim_end, }
  667. },
  668. };
  669. struct phy_info *phy_info[] = {
  670. #if 0
  671. &phy_info_cis8201,
  672. #endif
  673. &phy_info_cis8204,
  674. &phy_info_M88E1011S,
  675. &phy_info_M88E1111S,
  676. &phy_info_dm9161,
  677. &phy_info_lxt971,
  678. NULL
  679. };
  680. /* Grab the identifier of the device's PHY, and search through
  681. * all of the known PHYs to see if one matches. If so, return
  682. * it, if not, return NULL */
  683. struct phy_info * get_phy_info(struct eth_device *dev)
  684. {
  685. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  686. uint phy_reg, phy_ID;
  687. int i;
  688. struct phy_info *theInfo = NULL;
  689. /* Grab the bits from PHYIR1, and put them in the upper half */
  690. phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
  691. phy_ID = (phy_reg & 0xffff) << 16;
  692. /* Grab the bits from PHYIR2, and put them in the lower half */
  693. phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
  694. phy_ID |= (phy_reg & 0xffff);
  695. /* loop through all the known PHY types, and find one that */
  696. /* matches the ID we read from the PHY. */
  697. for(i=0; phy_info[i]; i++) {
  698. if(phy_info[i]->id == (phy_ID >> phy_info[i]->shift))
  699. theInfo = phy_info[i];
  700. }
  701. if(theInfo == NULL)
  702. {
  703. printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
  704. return NULL;
  705. } else {
  706. printf("%s: PHY is %s (%x)\n", dev->name, theInfo->name,
  707. phy_ID);
  708. }
  709. return theInfo;
  710. }
  711. /* Execute the given series of commands on the given device's
  712. * PHY, running functions as necessary*/
  713. void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
  714. {
  715. int i;
  716. uint result;
  717. volatile tsec_t *phyregs = priv->phyregs;
  718. phyregs->miimcfg = MIIMCFG_RESET;
  719. phyregs->miimcfg = MIIMCFG_INIT_VALUE;
  720. while(phyregs->miimind & MIIMIND_BUSY);
  721. for(i=0;cmd->mii_reg != miim_end;i++) {
  722. if(cmd->mii_data == miim_read) {
  723. result = read_phy_reg(priv, cmd->mii_reg);
  724. if(cmd->funct != NULL)
  725. (*(cmd->funct))(result, priv);
  726. } else {
  727. if(cmd->funct != NULL)
  728. result = (*(cmd->funct))(cmd->mii_reg, priv);
  729. else
  730. result = cmd->mii_data;
  731. write_phy_reg(priv, cmd->mii_reg, result);
  732. }
  733. cmd++;
  734. }
  735. }
  736. /* Relocate the function pointers in the phy cmd lists */
  737. static void relocate_cmds(void)
  738. {
  739. struct phy_cmd **cmdlistptr;
  740. struct phy_cmd *cmd;
  741. int i,j,k;
  742. DECLARE_GLOBAL_DATA_PTR;
  743. for(i=0; phy_info[i]; i++) {
  744. /* First thing's first: relocate the pointers to the
  745. * PHY command structures (the structs were done) */
  746. phy_info[i] = (struct phy_info *) ((uint)phy_info[i]
  747. + gd->reloc_off);
  748. phy_info[i]->name += gd->reloc_off;
  749. phy_info[i]->config =
  750. (struct phy_cmd *)((uint)phy_info[i]->config
  751. + gd->reloc_off);
  752. phy_info[i]->startup =
  753. (struct phy_cmd *)((uint)phy_info[i]->startup
  754. + gd->reloc_off);
  755. phy_info[i]->shutdown =
  756. (struct phy_cmd *)((uint)phy_info[i]->shutdown
  757. + gd->reloc_off);
  758. cmdlistptr = &phy_info[i]->config;
  759. j=0;
  760. for(;cmdlistptr <= &phy_info[i]->shutdown;cmdlistptr++) {
  761. k=0;
  762. for(cmd=*cmdlistptr;cmd->mii_reg != miim_end;cmd++) {
  763. /* Only relocate non-NULL pointers */
  764. if(cmd->funct)
  765. cmd->funct += gd->reloc_off;
  766. k++;
  767. }
  768. j++;
  769. }
  770. }
  771. relocated = 1;
  772. }
  773. #ifndef CONFIG_BITBANGMII
  774. struct tsec_private * get_priv_for_phy(unsigned char phyaddr)
  775. {
  776. int i;
  777. for(i=0;i<MAXCONTROLLERS;i++) {
  778. if(privlist[i]->phyaddr == phyaddr)
  779. return privlist[i];
  780. }
  781. return NULL;
  782. }
  783. /*
  784. * Read a MII PHY register.
  785. *
  786. * Returns:
  787. * 0 on success
  788. */
  789. int miiphy_read(unsigned char addr, unsigned char reg, unsigned short *value)
  790. {
  791. unsigned short ret;
  792. struct tsec_private *priv = get_priv_for_phy(addr);
  793. if(NULL == priv) {
  794. printf("Can't read PHY at address %d\n", addr);
  795. return -1;
  796. }
  797. ret = (unsigned short)read_phy_reg(priv, reg);
  798. *value = ret;
  799. return 0;
  800. }
  801. /*
  802. * Write a MII PHY register.
  803. *
  804. * Returns:
  805. * 0 on success
  806. */
  807. int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value)
  808. {
  809. struct tsec_private *priv = get_priv_for_phy(addr);
  810. if(NULL == priv) {
  811. printf("Can't write PHY at address %d\n", addr);
  812. return -1;
  813. }
  814. write_phy_reg(priv, reg, value);
  815. return 0;
  816. }
  817. #endif /* CONFIG_BITBANGMII */
  818. #endif /* CONFIG_TSEC_ENET */