tsec.c 27 KB

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