tsec.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. #undef TSEC_DEBUG
  25. #ifdef TSEC_DEBUG
  26. #define DBGPRINT(x,y) printf(x,y)
  27. #else
  28. #define DBGPRINT(x,y)
  29. #endif
  30. static uint rxIdx; /* index of the current RX buffer */
  31. static uint txIdx; /* index of the current TX buffer */
  32. typedef volatile struct rtxbd {
  33. txbd8_t txbd[TX_BUF_CNT];
  34. rxbd8_t rxbd[PKTBUFSRX];
  35. } RTXBD;
  36. struct tsec_info_struct {
  37. unsigned int phyaddr;
  38. unsigned int gigabit;
  39. unsigned int phyregidx;
  40. };
  41. /* The tsec_info structure contains 3 values which the
  42. * driver uses to determine how to operate a given ethernet
  43. * device. For now, the structure is initialized with the
  44. * knowledge that all current implementations have 2 TSEC
  45. * devices, and one FEC. The information needed is:
  46. * phyaddr - The address of the PHY which is attached to
  47. * the given device.
  48. *
  49. * gigabit - This variable indicates whether the device
  50. * supports gigabit speed ethernet
  51. *
  52. * phyregidx - This variable specifies which ethernet device
  53. * controls the MII Management registers which are connected
  54. * to the PHY. For 8540/8560, only TSEC1 (index 0) has
  55. * access to the PHYs, so all of the entries have "0".
  56. *
  57. * The values specified in the table are taken from the board's
  58. * config file in include/configs/. When implementing a new
  59. * board with ethernet capability, it is necessary to define:
  60. * TSEC1_PHY_ADDR
  61. * TSEC1_PHYIDX
  62. * TSEC2_PHY_ADDR
  63. * TSEC2_PHYIDX
  64. *
  65. * and for 8560:
  66. * FEC_PHY_ADDR
  67. * FEC_PHYIDX
  68. */
  69. static struct tsec_info_struct tsec_info[] = {
  70. #ifdef CONFIG_MPC85XX_TSEC1
  71. {TSEC1_PHY_ADDR, 1, TSEC1_PHYIDX},
  72. #endif
  73. #ifdef CONFIG_MPC85XX_TSEC2
  74. {TSEC2_PHY_ADDR, 1, TSEC2_PHYIDX},
  75. #endif
  76. #ifdef CONFIG_MPC85XX_FEC
  77. {FEC_PHY_ADDR, 0, FEC_PHYIDX},
  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, "MOTO 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. DBGPRINT("%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. DBGPRINT("%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_cis8204 = {
  538. 0x3f11,
  539. "Cicada Cis8204",
  540. 6,
  541. (struct phy_cmd[]) { /* config */
  542. /* Override PHY config settings */
  543. {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
  544. /* Configure some basic stuff */
  545. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  546. {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT, &mii_cis8204_fixled},
  547. {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT, NULL},
  548. {miim_end,}
  549. },
  550. (struct phy_cmd[]) { /* startup */
  551. /* Read the Status (2x to make sure link is right) */
  552. {MIIM_STATUS, miim_read, NULL},
  553. /* Auto-negotiate */
  554. {MIIM_STATUS, miim_read, &mii_parse_sr},
  555. /* Read the status */
  556. {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201},
  557. {miim_end,}
  558. },
  559. (struct phy_cmd[]) { /* shutdown */
  560. {miim_end,}
  561. },
  562. };
  563. /* Cicada 8201 */
  564. struct phy_info phy_info_cis8201 = {
  565. 0xfc41,
  566. "CIS8201",
  567. 4,
  568. (struct phy_cmd[]) { /* config */
  569. /* Override PHY config settings */
  570. {MIIM_CIS8201_AUX_CONSTAT, MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
  571. /* Set up the interface mode */
  572. {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT, NULL},
  573. /* Configure some basic stuff */
  574. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  575. {miim_end,}
  576. },
  577. (struct phy_cmd[]) { /* startup */
  578. /* Read the Status (2x to make sure link is right) */
  579. {MIIM_STATUS, miim_read, NULL},
  580. /* Auto-negotiate */
  581. {MIIM_STATUS, miim_read, &mii_parse_sr},
  582. /* Read the status */
  583. {MIIM_CIS8201_AUX_CONSTAT, miim_read, &mii_parse_cis8201},
  584. {miim_end,}
  585. },
  586. (struct phy_cmd[]) { /* shutdown */
  587. {miim_end,}
  588. },
  589. };
  590. struct phy_info phy_info_dm9161 = {
  591. 0x0181b88,
  592. "Davicom DM9161E",
  593. 4,
  594. (struct phy_cmd[]) { /* config */
  595. {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
  596. /* Do not bypass the scrambler/descrambler */
  597. {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
  598. /* Clear 10BTCSR to default */
  599. {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT, NULL},
  600. /* Configure some basic stuff */
  601. {MIIM_CONTROL, MIIM_CR_INIT, NULL},
  602. /* Restart Auto Negotiation */
  603. {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
  604. {miim_end,}
  605. },
  606. (struct phy_cmd[]) { /* startup */
  607. /* Status is read once to clear old link state */
  608. {MIIM_STATUS, miim_read, NULL},
  609. /* Auto-negotiate */
  610. {MIIM_STATUS, miim_read, &mii_parse_sr},
  611. /* Read the status */
  612. {MIIM_DM9161_SCSR, miim_read, &mii_parse_dm9161_scsr},
  613. {miim_end,}
  614. },
  615. (struct phy_cmd[]) { /* shutdown */
  616. {miim_end,}
  617. },
  618. };
  619. struct phy_info *phy_info[] = {
  620. #if 0
  621. &phy_info_cis8201,
  622. #endif
  623. &phy_info_cis8204,
  624. &phy_info_M88E1011S,
  625. &phy_info_dm9161,
  626. NULL
  627. };
  628. /* Grab the identifier of the device's PHY, and search through
  629. * all of the known PHYs to see if one matches. If so, return
  630. * it, if not, return NULL */
  631. struct phy_info * get_phy_info(struct eth_device *dev)
  632. {
  633. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  634. uint phy_reg, phy_ID;
  635. int i;
  636. struct phy_info *theInfo = NULL;
  637. /* Grab the bits from PHYIR1, and put them in the upper half */
  638. phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
  639. phy_ID = (phy_reg & 0xffff) << 16;
  640. /* Grab the bits from PHYIR2, and put them in the lower half */
  641. phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
  642. phy_ID |= (phy_reg & 0xffff);
  643. /* loop through all the known PHY types, and find one that */
  644. /* matches the ID we read from the PHY. */
  645. for(i=0; phy_info[i]; i++) {
  646. if(phy_info[i]->id == (phy_ID >> phy_info[i]->shift))
  647. theInfo = phy_info[i];
  648. }
  649. if(theInfo == NULL)
  650. {
  651. printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
  652. return NULL;
  653. } else {
  654. printf("%s: PHY is %s (%x)\n", dev->name, theInfo->name,
  655. phy_ID);
  656. }
  657. return theInfo;
  658. }
  659. /* Execute the given series of commands on the given device's
  660. * PHY, running functions as necessary*/
  661. void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
  662. {
  663. int i;
  664. uint result;
  665. volatile tsec_t *phyregs = priv->phyregs;
  666. phyregs->miimcfg = MIIMCFG_RESET;
  667. phyregs->miimcfg = MIIMCFG_INIT_VALUE;
  668. while(phyregs->miimind & MIIMIND_BUSY);
  669. for(i=0;cmd->mii_reg != miim_end;i++) {
  670. if(cmd->mii_data == miim_read) {
  671. result = read_phy_reg(priv, cmd->mii_reg);
  672. if(cmd->funct != NULL)
  673. (*(cmd->funct))(result, priv);
  674. } else {
  675. if(cmd->funct != NULL)
  676. result = (*(cmd->funct))(cmd->mii_reg, priv);
  677. else
  678. result = cmd->mii_data;
  679. write_phy_reg(priv, cmd->mii_reg, result);
  680. }
  681. cmd++;
  682. }
  683. }
  684. /* Relocate the function pointers in the phy cmd lists */
  685. static void relocate_cmds(void)
  686. {
  687. struct phy_cmd **cmdlistptr;
  688. struct phy_cmd *cmd;
  689. int i,j,k;
  690. DECLARE_GLOBAL_DATA_PTR;
  691. for(i=0; phy_info[i]; i++) {
  692. /* First thing's first: relocate the pointers to the
  693. * PHY command structures (the structs were done) */
  694. phy_info[i] = (struct phy_info *) ((uint)phy_info[i]
  695. + gd->reloc_off);
  696. phy_info[i]->name += gd->reloc_off;
  697. phy_info[i]->config =
  698. (struct phy_cmd *)((uint)phy_info[i]->config
  699. + gd->reloc_off);
  700. phy_info[i]->startup =
  701. (struct phy_cmd *)((uint)phy_info[i]->startup
  702. + gd->reloc_off);
  703. phy_info[i]->shutdown =
  704. (struct phy_cmd *)((uint)phy_info[i]->shutdown
  705. + gd->reloc_off);
  706. cmdlistptr = &phy_info[i]->config;
  707. j=0;
  708. for(;cmdlistptr <= &phy_info[i]->shutdown;cmdlistptr++) {
  709. k=0;
  710. for(cmd=*cmdlistptr;cmd->mii_reg != miim_end;cmd++) {
  711. /* Only relocate non-NULL pointers */
  712. if(cmd->funct)
  713. cmd->funct += gd->reloc_off;
  714. k++;
  715. }
  716. j++;
  717. }
  718. }
  719. relocated = 1;
  720. }
  721. #ifndef CONFIG_BITBANGMII
  722. struct tsec_private * get_priv_for_phy(unsigned char phyaddr)
  723. {
  724. int i;
  725. for(i=0;i<MAXCONTROLLERS;i++) {
  726. if(privlist[i]->phyaddr == phyaddr)
  727. return privlist[i];
  728. }
  729. return NULL;
  730. }
  731. /*
  732. * Read a MII PHY register.
  733. *
  734. * Returns:
  735. * 0 on success
  736. */
  737. int miiphy_read(unsigned char addr, unsigned char reg, unsigned short *value)
  738. {
  739. unsigned short ret;
  740. struct tsec_private *priv = get_priv_for_phy(addr);
  741. if(NULL == priv) {
  742. printf("Can't read PHY at address %d\n", addr);
  743. return -1;
  744. }
  745. ret = (unsigned short)read_phy_reg(priv, reg);
  746. *value = ret;
  747. return 0;
  748. }
  749. /*
  750. * Write a MII PHY register.
  751. *
  752. * Returns:
  753. * 0 on success
  754. */
  755. int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value)
  756. {
  757. struct tsec_private *priv = get_priv_for_phy(addr);
  758. if(NULL == priv) {
  759. printf("Can't write PHY at address %d\n", addr);
  760. return -1;
  761. }
  762. write_phy_reg(priv, reg, value);
  763. return 0;
  764. }
  765. #endif /* CONFIG_BITBANGMII */
  766. #endif /* CONFIG_TSEC_ENET */