tsec.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. /*
  2. * Freescale Three Speed Ethernet Controller driver
  3. *
  4. * This software may be used and distributed according to the
  5. * terms of the GNU Public License, Version 2, incorporated
  6. * herein by reference.
  7. *
  8. * Copyright 2004, 2007 Freescale Semiconductor, Inc.
  9. * (C) Copyright 2003, Motorola, Inc.
  10. * author Andy Fleming
  11. *
  12. */
  13. #include <config.h>
  14. #include <common.h>
  15. #include <malloc.h>
  16. #include <net.h>
  17. #include <command.h>
  18. #if defined(CONFIG_TSEC_ENET)
  19. #include "tsec.h"
  20. #include "miiphy.h"
  21. DECLARE_GLOBAL_DATA_PTR;
  22. #define TX_BUF_CNT 2
  23. static uint rxIdx; /* index of the current RX buffer */
  24. static uint txIdx; /* index of the current TX buffer */
  25. typedef volatile struct rtxbd {
  26. txbd8_t txbd[TX_BUF_CNT];
  27. rxbd8_t rxbd[PKTBUFSRX];
  28. } RTXBD;
  29. struct tsec_info_struct {
  30. unsigned int phyaddr;
  31. u32 flags;
  32. unsigned int phyregidx;
  33. };
  34. /* The tsec_info structure contains 3 values which the
  35. * driver uses to determine how to operate a given ethernet
  36. * device. The information needed is:
  37. * phyaddr - The address of the PHY which is attached to
  38. * the given device.
  39. *
  40. * flags - This variable indicates whether the device
  41. * supports gigabit speed ethernet, and whether it should be
  42. * in reduced mode.
  43. *
  44. * phyregidx - This variable specifies which ethernet device
  45. * controls the MII Management registers which are connected
  46. * to the PHY. For now, only TSEC1 (index 0) has
  47. * access to the PHYs, so all of the entries have "0".
  48. *
  49. * The values specified in the table are taken from the board's
  50. * config file in include/configs/. When implementing a new
  51. * board with ethernet capability, it is necessary to define:
  52. * TSECn_PHY_ADDR
  53. * TSECn_PHYIDX
  54. *
  55. * for n = 1,2,3, etc. And for FEC:
  56. * FEC_PHY_ADDR
  57. * FEC_PHYIDX
  58. */
  59. static struct tsec_info_struct tsec_info[] = {
  60. #ifdef CONFIG_TSEC1
  61. {TSEC1_PHY_ADDR, TSEC1_FLAGS, TSEC1_PHYIDX},
  62. #else
  63. {0, 0, 0},
  64. #endif
  65. #ifdef CONFIG_TSEC2
  66. {TSEC2_PHY_ADDR, TSEC2_FLAGS, TSEC2_PHYIDX},
  67. #else
  68. {0, 0, 0},
  69. #endif
  70. #ifdef CONFIG_MPC85XX_FEC
  71. {FEC_PHY_ADDR, FEC_FLAGS, FEC_PHYIDX},
  72. #else
  73. #ifdef CONFIG_TSEC3
  74. {TSEC3_PHY_ADDR, TSEC3_FLAGS, TSEC3_PHYIDX},
  75. #else
  76. {0, 0, 0},
  77. #endif
  78. #ifdef CONFIG_TSEC4
  79. {TSEC4_PHY_ADDR, TSEC4_FLAGS, TSEC4_PHYIDX},
  80. #else
  81. {0, 0, 0},
  82. #endif /* CONFIG_TSEC4 */
  83. #endif /* CONFIG_MPC85XX_FEC */
  84. };
  85. #define MAXCONTROLLERS (4)
  86. static int relocated = 0;
  87. static struct tsec_private *privlist[MAXCONTROLLERS];
  88. #ifdef __GNUC__
  89. static RTXBD rtx __attribute__ ((aligned(8)));
  90. #else
  91. #error "rtx must be 64-bit aligned"
  92. #endif
  93. static int tsec_send(struct eth_device *dev,
  94. volatile void *packet, int length);
  95. static int tsec_recv(struct eth_device *dev);
  96. static int tsec_init(struct eth_device *dev, bd_t * bd);
  97. static void tsec_halt(struct eth_device *dev);
  98. static void init_registers(volatile tsec_t * regs);
  99. static void startup_tsec(struct eth_device *dev);
  100. static int init_phy(struct eth_device *dev);
  101. void write_phy_reg(struct tsec_private *priv, uint regnum, uint value);
  102. uint read_phy_reg(struct tsec_private *priv, uint regnum);
  103. struct phy_info *get_phy_info(struct eth_device *dev);
  104. void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd);
  105. static void adjust_link(struct eth_device *dev);
  106. static void relocate_cmds(void);
  107. static int tsec_miiphy_write(char *devname, unsigned char addr,
  108. unsigned char reg, unsigned short value);
  109. static int tsec_miiphy_read(char *devname, unsigned char addr,
  110. unsigned char reg, unsigned short *value);
  111. #ifdef CONFIG_MCAST_TFTP
  112. static int tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set);
  113. #endif
  114. /* Initialize device structure. Returns success if PHY
  115. * initialization succeeded (i.e. if it recognizes the PHY)
  116. */
  117. int tsec_initialize(bd_t * bis, int index, char *devname)
  118. {
  119. struct eth_device *dev;
  120. int i;
  121. struct tsec_private *priv;
  122. dev = (struct eth_device *)malloc(sizeof *dev);
  123. if (NULL == dev)
  124. return 0;
  125. memset(dev, 0, sizeof *dev);
  126. priv = (struct tsec_private *)malloc(sizeof(*priv));
  127. if (NULL == priv)
  128. return 0;
  129. privlist[index] = priv;
  130. priv->regs = (volatile tsec_t *)(TSEC_BASE_ADDR + index * TSEC_SIZE);
  131. priv->phyregs = (volatile tsec_t *)(TSEC_BASE_ADDR +
  132. tsec_info[index].phyregidx *
  133. 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. #ifdef CONFIG_MCAST_TFTP
  144. dev->mcast = tsec_mcast_addr;
  145. #endif
  146. /* Tell u-boot to get the addr from the env */
  147. for (i = 0; i < 6; i++)
  148. dev->enetaddr[i] = 0;
  149. eth_register(dev);
  150. /* Reset the MAC */
  151. priv->regs->maccfg1 |= MACCFG1_SOFT_RESET;
  152. priv->regs->maccfg1 &= ~(MACCFG1_SOFT_RESET);
  153. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
  154. && !defined(BITBANGMII)
  155. miiphy_register(dev->name, tsec_miiphy_read, tsec_miiphy_write);
  156. #endif
  157. /* Try to initialize PHY here, and return */
  158. return init_phy(dev);
  159. }
  160. /* Initializes data structures and registers for the controller,
  161. * and brings the interface up. Returns the link status, meaning
  162. * that it returns success if the link is up, failure otherwise.
  163. * This allows u-boot to find the first active controller.
  164. */
  165. int tsec_init(struct eth_device *dev, bd_t * bd)
  166. {
  167. uint tempval;
  168. char tmpbuf[MAC_ADDR_LEN];
  169. int i;
  170. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  171. volatile tsec_t *regs = priv->regs;
  172. /* Make sure the controller is stopped */
  173. tsec_halt(dev);
  174. /* Init MACCFG2. Defaults to GMII */
  175. regs->maccfg2 = MACCFG2_INIT_SETTINGS;
  176. /* Init ECNTRL */
  177. regs->ecntrl = ECNTRL_INIT_SETTINGS;
  178. /* Copy the station address into the address registers.
  179. * Backwards, because little endian MACS are dumb */
  180. for (i = 0; i < MAC_ADDR_LEN; i++) {
  181. tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i];
  182. }
  183. regs->macstnaddr1 = *((uint *) (tmpbuf));
  184. tempval = *((uint *) (tmpbuf + 4));
  185. regs->macstnaddr2 = tempval;
  186. /* reset the indices to zero */
  187. rxIdx = 0;
  188. txIdx = 0;
  189. /* Clear out (for the most part) the other registers */
  190. init_registers(regs);
  191. /* Ready the device for tx/rx */
  192. startup_tsec(dev);
  193. /* If there's no link, fail */
  194. return priv->link;
  195. }
  196. /* Write value to the device's PHY through the registers
  197. * specified in priv, modifying the register specified in regnum.
  198. * It will wait for the write to be done (or for a timeout to
  199. * expire) before exiting
  200. */
  201. void write_phy_reg(struct tsec_private *priv, uint regnum, uint value)
  202. {
  203. volatile tsec_t *regbase = priv->phyregs;
  204. uint phyid = priv->phyaddr;
  205. int timeout = 1000000;
  206. regbase->miimadd = (phyid << 8) | regnum;
  207. regbase->miimcon = value;
  208. asm("sync");
  209. timeout = 1000000;
  210. while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
  211. }
  212. /* Reads register regnum on the device's PHY through the
  213. * registers specified in priv. It lowers and raises the read
  214. * command, and waits for the data to become valid (miimind
  215. * notvalid bit cleared), and the bus to cease activity (miimind
  216. * busy bit cleared), and then returns the value
  217. */
  218. uint read_phy_reg(struct tsec_private *priv, uint regnum)
  219. {
  220. uint value;
  221. volatile tsec_t *regbase = priv->phyregs;
  222. uint phyid = priv->phyaddr;
  223. /* Put the address of the phy, and the register
  224. * number into MIIMADD */
  225. regbase->miimadd = (phyid << 8) | regnum;
  226. /* Clear the command register, and wait */
  227. regbase->miimcom = 0;
  228. asm("sync");
  229. /* Initiate a read command, and wait */
  230. regbase->miimcom = MIIM_READ_COMMAND;
  231. asm("sync");
  232. /* Wait for the the indication that the read is done */
  233. while ((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY))) ;
  234. /* Grab the value read from the PHY */
  235. value = regbase->miimstat;
  236. return value;
  237. }
  238. /* Discover which PHY is attached to the device, and configure it
  239. * properly. If the PHY is not recognized, then return 0
  240. * (failure). Otherwise, return 1
  241. */
  242. static int init_phy(struct eth_device *dev)
  243. {
  244. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  245. struct phy_info *curphy;
  246. volatile tsec_t *regs = (volatile tsec_t *)(TSEC_BASE_ADDR);
  247. /* Assign a Physical address to the TBI */
  248. regs->tbipa = CFG_TBIPA_VALUE;
  249. regs = (volatile tsec_t *)(TSEC_BASE_ADDR + TSEC_SIZE);
  250. regs->tbipa = CFG_TBIPA_VALUE;
  251. asm("sync");
  252. /* Reset MII (due to new addresses) */
  253. priv->phyregs->miimcfg = MIIMCFG_RESET;
  254. asm("sync");
  255. priv->phyregs->miimcfg = MIIMCFG_INIT_VALUE;
  256. asm("sync");
  257. while (priv->phyregs->miimind & MIIMIND_BUSY) ;
  258. if (0 == relocated)
  259. relocate_cmds();
  260. /* Get the cmd structure corresponding to the attached
  261. * PHY */
  262. curphy = get_phy_info(dev);
  263. if (curphy == NULL) {
  264. priv->phyinfo = NULL;
  265. printf("%s: No PHY found\n", dev->name);
  266. return 0;
  267. }
  268. priv->phyinfo = curphy;
  269. phy_run_commands(priv, priv->phyinfo->config);
  270. return 1;
  271. }
  272. /*
  273. * Returns which value to write to the control register.
  274. * For 10/100, the value is slightly different
  275. */
  276. uint mii_cr_init(uint mii_reg, struct tsec_private * priv)
  277. {
  278. if (priv->flags & TSEC_GIGABIT)
  279. return MIIM_CONTROL_INIT;
  280. else
  281. return MIIM_CR_INIT;
  282. }
  283. /* Parse the status register for link, and then do
  284. * auto-negotiation
  285. */
  286. uint mii_parse_sr(uint mii_reg, struct tsec_private * priv)
  287. {
  288. /*
  289. * Wait if PHY is capable of autonegotiation and autonegotiation
  290. * is not complete.
  291. */
  292. mii_reg = read_phy_reg(priv, MIIM_STATUS);
  293. if ((mii_reg & PHY_BMSR_AUTN_ABLE)
  294. && !(mii_reg & PHY_BMSR_AUTN_COMP)) {
  295. int i = 0;
  296. puts("Waiting for PHY auto negotiation to complete");
  297. while (!((mii_reg & PHY_BMSR_AUTN_COMP)
  298. && (mii_reg & MIIM_STATUS_LINK))) {
  299. /*
  300. * Timeout reached ?
  301. */
  302. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  303. puts(" TIMEOUT !\n");
  304. priv->link = 0;
  305. return 0;
  306. }
  307. if ((i++ % 1000) == 0) {
  308. putc('.');
  309. }
  310. udelay(1000); /* 1 ms */
  311. mii_reg = read_phy_reg(priv, MIIM_STATUS);
  312. }
  313. puts(" done\n");
  314. priv->link = 1;
  315. udelay(500000); /* another 500 ms (results in faster booting) */
  316. } else {
  317. priv->link = 1;
  318. }
  319. return 0;
  320. }
  321. /* Generic function which updates the speed and duplex. If
  322. * autonegotiation is enabled, it uses the AND of the link
  323. * partner's advertised capabilities and our advertised
  324. * capabilities. If autonegotiation is disabled, we use the
  325. * appropriate bits in the control register.
  326. *
  327. * Stolen from Linux's mii.c and phy_device.c
  328. */
  329. uint mii_parse_link(uint mii_reg, struct tsec_private *priv)
  330. {
  331. /* We're using autonegotiation */
  332. if (mii_reg & PHY_BMSR_AUTN_ABLE) {
  333. uint lpa = 0;
  334. uint gblpa = 0;
  335. /* Check for gigabit capability */
  336. if (mii_reg & PHY_BMSR_EXT) {
  337. /* We want a list of states supported by
  338. * both PHYs in the link
  339. */
  340. gblpa = read_phy_reg(priv, PHY_1000BTSR);
  341. gblpa &= read_phy_reg(priv, PHY_1000BTCR) << 2;
  342. }
  343. /* Set the baseline so we only have to set them
  344. * if they're different
  345. */
  346. priv->speed = 10;
  347. priv->duplexity = 0;
  348. /* Check the gigabit fields */
  349. if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
  350. priv->speed = 1000;
  351. if (gblpa & PHY_1000BTSR_1000FD)
  352. priv->duplexity = 1;
  353. /* We're done! */
  354. return 0;
  355. }
  356. lpa = read_phy_reg(priv, PHY_ANAR);
  357. lpa &= read_phy_reg(priv, PHY_ANLPAR);
  358. if (lpa & (PHY_ANLPAR_TXFD | PHY_ANLPAR_TX)) {
  359. priv->speed = 100;
  360. if (lpa & PHY_ANLPAR_TXFD)
  361. priv->duplexity = 1;
  362. } else if (lpa & PHY_ANLPAR_10FD)
  363. priv->duplexity = 1;
  364. } else {
  365. uint bmcr = read_phy_reg(priv, PHY_BMCR);
  366. priv->speed = 10;
  367. priv->duplexity = 0;
  368. if (bmcr & PHY_BMCR_DPLX)
  369. priv->duplexity = 1;
  370. if (bmcr & PHY_BMCR_1000_MBPS)
  371. priv->speed = 1000;
  372. else if (bmcr & PHY_BMCR_100_MBPS)
  373. priv->speed = 100;
  374. }
  375. return 0;
  376. }
  377. /*
  378. * Parse the BCM54xx status register for speed and duplex information.
  379. * The linux sungem_phy has this information, but in a table format.
  380. */
  381. uint mii_parse_BCM54xx_sr(uint mii_reg, struct tsec_private *priv)
  382. {
  383. switch((mii_reg & MIIM_BCM54xx_AUXSTATUS_LINKMODE_MASK) >> MIIM_BCM54xx_AUXSTATUS_LINKMODE_SHIFT){
  384. case 1:
  385. printf("Enet starting in 10BT/HD\n");
  386. priv->duplexity = 0;
  387. priv->speed = 10;
  388. break;
  389. case 2:
  390. printf("Enet starting in 10BT/FD\n");
  391. priv->duplexity = 1;
  392. priv->speed = 10;
  393. break;
  394. case 3:
  395. printf("Enet starting in 100BT/HD\n");
  396. priv->duplexity = 0;
  397. priv->speed = 100;
  398. break;
  399. case 5:
  400. printf("Enet starting in 100BT/FD\n");
  401. priv->duplexity = 1;
  402. priv->speed = 100;
  403. break;
  404. case 6:
  405. printf("Enet starting in 1000BT/HD\n");
  406. priv->duplexity = 0;
  407. priv->speed = 1000;
  408. break;
  409. case 7:
  410. printf("Enet starting in 1000BT/FD\n");
  411. priv->duplexity = 1;
  412. priv->speed = 1000;
  413. break;
  414. default:
  415. printf("Auto-neg error, defaulting to 10BT/HD\n");
  416. priv->duplexity = 0;
  417. priv->speed = 10;
  418. break;
  419. }
  420. return 0;
  421. }
  422. /* Parse the 88E1011's status register for speed and duplex
  423. * information
  424. */
  425. uint mii_parse_88E1011_psr(uint mii_reg, struct tsec_private * priv)
  426. {
  427. uint speed;
  428. mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
  429. if (!((mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE) &&
  430. (mii_reg & MIIM_88E1011_PHYSTAT_LINK))) {
  431. int i = 0;
  432. puts("Waiting for PHY realtime link");
  433. while (!((mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE) &&
  434. (mii_reg & MIIM_88E1011_PHYSTAT_LINK))) {
  435. /*
  436. * Timeout reached ?
  437. */
  438. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  439. puts(" TIMEOUT !\n");
  440. priv->link = 0;
  441. break;
  442. }
  443. if ((i++ % 1000) == 0) {
  444. putc('.');
  445. }
  446. udelay(1000); /* 1 ms */
  447. mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
  448. }
  449. puts(" done\n");
  450. udelay(500000); /* another 500 ms (results in faster booting) */
  451. }
  452. if (mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
  453. priv->duplexity = 1;
  454. else
  455. priv->duplexity = 0;
  456. speed = (mii_reg & MIIM_88E1011_PHYSTAT_SPEED);
  457. switch (speed) {
  458. case MIIM_88E1011_PHYSTAT_GBIT:
  459. priv->speed = 1000;
  460. break;
  461. case MIIM_88E1011_PHYSTAT_100:
  462. priv->speed = 100;
  463. break;
  464. default:
  465. priv->speed = 10;
  466. }
  467. return 0;
  468. }
  469. /* Parse the cis8201's status register for speed and duplex
  470. * information
  471. */
  472. uint mii_parse_cis8201(uint mii_reg, struct tsec_private * priv)
  473. {
  474. uint speed;
  475. if (mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX)
  476. priv->duplexity = 1;
  477. else
  478. priv->duplexity = 0;
  479. speed = mii_reg & MIIM_CIS8201_AUXCONSTAT_SPEED;
  480. switch (speed) {
  481. case MIIM_CIS8201_AUXCONSTAT_GBIT:
  482. priv->speed = 1000;
  483. break;
  484. case MIIM_CIS8201_AUXCONSTAT_100:
  485. priv->speed = 100;
  486. break;
  487. default:
  488. priv->speed = 10;
  489. break;
  490. }
  491. return 0;
  492. }
  493. /* Parse the vsc8244's status register for speed and duplex
  494. * information
  495. */
  496. uint mii_parse_vsc8244(uint mii_reg, struct tsec_private * priv)
  497. {
  498. uint speed;
  499. if (mii_reg & MIIM_VSC8244_AUXCONSTAT_DUPLEX)
  500. priv->duplexity = 1;
  501. else
  502. priv->duplexity = 0;
  503. speed = mii_reg & MIIM_VSC8244_AUXCONSTAT_SPEED;
  504. switch (speed) {
  505. case MIIM_VSC8244_AUXCONSTAT_GBIT:
  506. priv->speed = 1000;
  507. break;
  508. case MIIM_VSC8244_AUXCONSTAT_100:
  509. priv->speed = 100;
  510. break;
  511. default:
  512. priv->speed = 10;
  513. break;
  514. }
  515. return 0;
  516. }
  517. /* Parse the DM9161's status register for speed and duplex
  518. * information
  519. */
  520. uint mii_parse_dm9161_scsr(uint mii_reg, struct tsec_private * priv)
  521. {
  522. if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_100H))
  523. priv->speed = 100;
  524. else
  525. priv->speed = 10;
  526. if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_10F))
  527. priv->duplexity = 1;
  528. else
  529. priv->duplexity = 0;
  530. return 0;
  531. }
  532. /*
  533. * Hack to write all 4 PHYs with the LED values
  534. */
  535. uint mii_cis8204_fixled(uint mii_reg, struct tsec_private * priv)
  536. {
  537. uint phyid;
  538. volatile tsec_t *regbase = priv->phyregs;
  539. int timeout = 1000000;
  540. for (phyid = 0; phyid < 4; phyid++) {
  541. regbase->miimadd = (phyid << 8) | mii_reg;
  542. regbase->miimcon = MIIM_CIS8204_SLEDCON_INIT;
  543. asm("sync");
  544. timeout = 1000000;
  545. while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
  546. }
  547. return MIIM_CIS8204_SLEDCON_INIT;
  548. }
  549. uint mii_cis8204_setmode(uint mii_reg, struct tsec_private * priv)
  550. {
  551. if (priv->flags & TSEC_REDUCED)
  552. return MIIM_CIS8204_EPHYCON_INIT | MIIM_CIS8204_EPHYCON_RGMII;
  553. else
  554. return MIIM_CIS8204_EPHYCON_INIT;
  555. }
  556. /* Initialized required registers to appropriate values, zeroing
  557. * those we don't care about (unless zero is bad, in which case,
  558. * choose a more appropriate value)
  559. */
  560. static void init_registers(volatile tsec_t * regs)
  561. {
  562. /* Clear IEVENT */
  563. regs->ievent = IEVENT_INIT_CLEAR;
  564. regs->imask = IMASK_INIT_CLEAR;
  565. regs->hash.iaddr0 = 0;
  566. regs->hash.iaddr1 = 0;
  567. regs->hash.iaddr2 = 0;
  568. regs->hash.iaddr3 = 0;
  569. regs->hash.iaddr4 = 0;
  570. regs->hash.iaddr5 = 0;
  571. regs->hash.iaddr6 = 0;
  572. regs->hash.iaddr7 = 0;
  573. regs->hash.gaddr0 = 0;
  574. regs->hash.gaddr1 = 0;
  575. regs->hash.gaddr2 = 0;
  576. regs->hash.gaddr3 = 0;
  577. regs->hash.gaddr4 = 0;
  578. regs->hash.gaddr5 = 0;
  579. regs->hash.gaddr6 = 0;
  580. regs->hash.gaddr7 = 0;
  581. regs->rctrl = 0x00000000;
  582. /* Init RMON mib registers */
  583. memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
  584. regs->rmon.cam1 = 0xffffffff;
  585. regs->rmon.cam2 = 0xffffffff;
  586. regs->mrblr = MRBLR_INIT_SETTINGS;
  587. regs->minflr = MINFLR_INIT_SETTINGS;
  588. regs->attr = ATTR_INIT_SETTINGS;
  589. regs->attreli = ATTRELI_INIT_SETTINGS;
  590. }
  591. /* Configure maccfg2 based on negotiated speed and duplex
  592. * reported by PHY handling code
  593. */
  594. static void adjust_link(struct eth_device *dev)
  595. {
  596. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  597. volatile tsec_t *regs = priv->regs;
  598. if (priv->link) {
  599. if (priv->duplexity != 0)
  600. regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
  601. else
  602. regs->maccfg2 &= ~(MACCFG2_FULL_DUPLEX);
  603. switch (priv->speed) {
  604. case 1000:
  605. regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
  606. | MACCFG2_GMII);
  607. break;
  608. case 100:
  609. case 10:
  610. regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
  611. | MACCFG2_MII);
  612. /* Set R100 bit in all modes although
  613. * it is only used in RGMII mode
  614. */
  615. if (priv->speed == 100)
  616. regs->ecntrl |= ECNTRL_R100;
  617. else
  618. regs->ecntrl &= ~(ECNTRL_R100);
  619. break;
  620. default:
  621. printf("%s: Speed was bad\n", dev->name);
  622. break;
  623. }
  624. printf("Speed: %d, %s duplex\n", priv->speed,
  625. (priv->duplexity) ? "full" : "half");
  626. } else {
  627. printf("%s: No link.\n", dev->name);
  628. }
  629. }
  630. /* Set up the buffers and their descriptors, and bring up the
  631. * interface
  632. */
  633. static void startup_tsec(struct eth_device *dev)
  634. {
  635. int i;
  636. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  637. volatile tsec_t *regs = priv->regs;
  638. /* Point to the buffer descriptors */
  639. regs->tbase = (unsigned int)(&rtx.txbd[txIdx]);
  640. regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
  641. /* Initialize the Rx Buffer descriptors */
  642. for (i = 0; i < PKTBUFSRX; i++) {
  643. rtx.rxbd[i].status = RXBD_EMPTY;
  644. rtx.rxbd[i].length = 0;
  645. rtx.rxbd[i].bufPtr = (uint) NetRxPackets[i];
  646. }
  647. rtx.rxbd[PKTBUFSRX - 1].status |= RXBD_WRAP;
  648. /* Initialize the TX Buffer Descriptors */
  649. for (i = 0; i < TX_BUF_CNT; i++) {
  650. rtx.txbd[i].status = 0;
  651. rtx.txbd[i].length = 0;
  652. rtx.txbd[i].bufPtr = 0;
  653. }
  654. rtx.txbd[TX_BUF_CNT - 1].status |= TXBD_WRAP;
  655. /* Start up the PHY */
  656. if(priv->phyinfo)
  657. phy_run_commands(priv, priv->phyinfo->startup);
  658. adjust_link(dev);
  659. /* Enable Transmit and Receive */
  660. regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
  661. /* Tell the DMA it is clear to go */
  662. regs->dmactrl |= DMACTRL_INIT_SETTINGS;
  663. regs->tstat = TSTAT_CLEAR_THALT;
  664. regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
  665. }
  666. /* This returns the status bits of the device. The return value
  667. * is never checked, and this is what the 8260 driver did, so we
  668. * do the same. Presumably, this would be zero if there were no
  669. * errors
  670. */
  671. static int tsec_send(struct eth_device *dev, volatile void *packet, int length)
  672. {
  673. int i;
  674. int result = 0;
  675. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  676. volatile tsec_t *regs = priv->regs;
  677. /* Find an empty buffer descriptor */
  678. for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
  679. if (i >= TOUT_LOOP) {
  680. debug("%s: tsec: tx buffers full\n", dev->name);
  681. return result;
  682. }
  683. }
  684. rtx.txbd[txIdx].bufPtr = (uint) packet;
  685. rtx.txbd[txIdx].length = length;
  686. rtx.txbd[txIdx].status |=
  687. (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
  688. /* Tell the DMA to go */
  689. regs->tstat = TSTAT_CLEAR_THALT;
  690. /* Wait for buffer to be transmitted */
  691. for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
  692. if (i >= TOUT_LOOP) {
  693. debug("%s: tsec: tx error\n", dev->name);
  694. return result;
  695. }
  696. }
  697. txIdx = (txIdx + 1) % TX_BUF_CNT;
  698. result = rtx.txbd[txIdx].status & TXBD_STATS;
  699. return result;
  700. }
  701. static int tsec_recv(struct eth_device *dev)
  702. {
  703. int length;
  704. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  705. volatile tsec_t *regs = priv->regs;
  706. while (!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
  707. length = rtx.rxbd[rxIdx].length;
  708. /* Send the packet up if there were no errors */
  709. if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
  710. NetReceive(NetRxPackets[rxIdx], length - 4);
  711. } else {
  712. printf("Got error %x\n",
  713. (rtx.rxbd[rxIdx].status & RXBD_STATS));
  714. }
  715. rtx.rxbd[rxIdx].length = 0;
  716. /* Set the wrap bit if this is the last element in the list */
  717. rtx.rxbd[rxIdx].status =
  718. RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
  719. rxIdx = (rxIdx + 1) % PKTBUFSRX;
  720. }
  721. if (regs->ievent & IEVENT_BSY) {
  722. regs->ievent = IEVENT_BSY;
  723. regs->rstat = RSTAT_CLEAR_RHALT;
  724. }
  725. return -1;
  726. }
  727. /* Stop the interface */
  728. static void tsec_halt(struct eth_device *dev)
  729. {
  730. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  731. volatile tsec_t *regs = priv->regs;
  732. regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
  733. regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS);
  734. while (!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC))) ;
  735. regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
  736. /* Shut down the PHY, as needed */
  737. if(priv->phyinfo)
  738. phy_run_commands(priv, priv->phyinfo->shutdown);
  739. }
  740. struct phy_info phy_info_M88E1149S = {
  741. 0x1410ca,
  742. "Marvell 88E1149S",
  743. 4,
  744. (struct phy_cmd[]){ /* config */
  745. /* Reset and configure the PHY */
  746. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  747. {0x1d, 0x1f, NULL},
  748. {0x1e, 0x200c, NULL},
  749. {0x1d, 0x5, NULL},
  750. {0x1e, 0x0, NULL},
  751. {0x1e, 0x100, NULL},
  752. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  753. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  754. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  755. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  756. {miim_end,}
  757. },
  758. (struct phy_cmd[]){ /* startup */
  759. /* Status is read once to clear old link state */
  760. {MIIM_STATUS, miim_read, NULL},
  761. /* Auto-negotiate */
  762. {MIIM_STATUS, miim_read, &mii_parse_sr},
  763. /* Read the status */
  764. {MIIM_88E1011_PHY_STATUS, miim_read,
  765. &mii_parse_88E1011_psr},
  766. {miim_end,}
  767. },
  768. (struct phy_cmd[]){ /* shutdown */
  769. {miim_end,}
  770. },
  771. };
  772. /* The 5411 id is 0x206070, the 5421 is 0x2060e0 */
  773. struct phy_info phy_info_BCM5461S = {
  774. 0x02060c1, /* 5461 ID */
  775. "Broadcom BCM5461S",
  776. 0, /* not clear to me what minor revisions we can shift away */
  777. (struct phy_cmd[]) { /* config */
  778. /* Reset and configure the PHY */
  779. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  780. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  781. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  782. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  783. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  784. {miim_end,}
  785. },
  786. (struct phy_cmd[]) { /* startup */
  787. /* Status is read once to clear old link state */
  788. {MIIM_STATUS, miim_read, NULL},
  789. /* Auto-negotiate */
  790. {MIIM_STATUS, miim_read, &mii_parse_sr},
  791. /* Read the status */
  792. {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
  793. {miim_end,}
  794. },
  795. (struct phy_cmd[]) { /* shutdown */
  796. {miim_end,}
  797. },
  798. };
  799. struct phy_info phy_info_BCM5464S = {
  800. 0x02060b1, /* 5464 ID */
  801. "Broadcom BCM5464S",
  802. 0, /* not clear to me what minor revisions we can shift away */
  803. (struct phy_cmd[]) { /* config */
  804. /* Reset and configure the PHY */
  805. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  806. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  807. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  808. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  809. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  810. {miim_end,}
  811. },
  812. (struct phy_cmd[]) { /* startup */
  813. /* Status is read once to clear old link state */
  814. {MIIM_STATUS, miim_read, NULL},
  815. /* Auto-negotiate */
  816. {MIIM_STATUS, miim_read, &mii_parse_sr},
  817. /* Read the status */
  818. {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
  819. {miim_end,}
  820. },
  821. (struct phy_cmd[]) { /* shutdown */
  822. {miim_end,}
  823. },
  824. };
  825. struct phy_info phy_info_M88E1011S = {
  826. 0x01410c6,
  827. "Marvell 88E1011S",
  828. 4,
  829. (struct phy_cmd[]){ /* config */
  830. /* Reset and configure the PHY */
  831. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  832. {0x1d, 0x1f, NULL},
  833. {0x1e, 0x200c, NULL},
  834. {0x1d, 0x5, NULL},
  835. {0x1e, 0x0, NULL},
  836. {0x1e, 0x100, NULL},
  837. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  838. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  839. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  840. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  841. {miim_end,}
  842. },
  843. (struct phy_cmd[]){ /* startup */
  844. /* Status is read once to clear old link state */
  845. {MIIM_STATUS, miim_read, NULL},
  846. /* Auto-negotiate */
  847. {MIIM_STATUS, miim_read, &mii_parse_sr},
  848. /* Read the status */
  849. {MIIM_88E1011_PHY_STATUS, miim_read,
  850. &mii_parse_88E1011_psr},
  851. {miim_end,}
  852. },
  853. (struct phy_cmd[]){ /* shutdown */
  854. {miim_end,}
  855. },
  856. };
  857. struct phy_info phy_info_M88E1111S = {
  858. 0x01410cc,
  859. "Marvell 88E1111S",
  860. 4,
  861. (struct phy_cmd[]){ /* config */
  862. /* Reset and configure the PHY */
  863. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  864. {0x14, 0x0cd2, NULL}, /* Delay RGMII TX and RX */
  865. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  866. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  867. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  868. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  869. {miim_end,}
  870. },
  871. (struct phy_cmd[]){ /* startup */
  872. /* Status is read once to clear old link state */
  873. {MIIM_STATUS, miim_read, NULL},
  874. /* Auto-negotiate */
  875. {MIIM_STATUS, miim_read, &mii_parse_sr},
  876. /* Read the status */
  877. {MIIM_88E1011_PHY_STATUS, miim_read,
  878. &mii_parse_88E1011_psr},
  879. {miim_end,}
  880. },
  881. (struct phy_cmd[]){ /* shutdown */
  882. {miim_end,}
  883. },
  884. };
  885. static unsigned int m88e1145_setmode(uint mii_reg, struct tsec_private *priv)
  886. {
  887. uint mii_data = read_phy_reg(priv, mii_reg);
  888. /* Setting MIIM_88E1145_PHY_EXT_CR */
  889. if (priv->flags & TSEC_REDUCED)
  890. return mii_data |
  891. MIIM_M88E1145_RGMII_RX_DELAY | MIIM_M88E1145_RGMII_TX_DELAY;
  892. else
  893. return mii_data;
  894. }
  895. static struct phy_info phy_info_M88E1145 = {
  896. 0x01410cd,
  897. "Marvell 88E1145",
  898. 4,
  899. (struct phy_cmd[]){ /* config */
  900. /* Reset the PHY */
  901. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  902. /* Errata E0, E1 */
  903. {29, 0x001b, NULL},
  904. {30, 0x418f, NULL},
  905. {29, 0x0016, NULL},
  906. {30, 0xa2da, NULL},
  907. /* Configure the PHY */
  908. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  909. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  910. {MIIM_88E1011_PHY_SCR, MIIM_88E1011_PHY_MDI_X_AUTO,
  911. NULL},
  912. {MIIM_88E1145_PHY_EXT_CR, 0, &m88e1145_setmode},
  913. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  914. {MIIM_CONTROL, MIIM_CONTROL_INIT, NULL},
  915. {miim_end,}
  916. },
  917. (struct phy_cmd[]){ /* startup */
  918. /* Status is read once to clear old link state */
  919. {MIIM_STATUS, miim_read, NULL},
  920. /* Auto-negotiate */
  921. {MIIM_STATUS, miim_read, &mii_parse_sr},
  922. {MIIM_88E1111_PHY_LED_CONTROL,
  923. MIIM_88E1111_PHY_LED_DIRECT, NULL},
  924. /* Read the Status */
  925. {MIIM_88E1011_PHY_STATUS, miim_read,
  926. &mii_parse_88E1011_psr},
  927. {miim_end,}
  928. },
  929. (struct phy_cmd[]){ /* shutdown */
  930. {miim_end,}
  931. },
  932. };
  933. struct phy_info phy_info_cis8204 = {
  934. 0x3f11,
  935. "Cicada Cis8204",
  936. 6,
  937. (struct phy_cmd[]){ /* config */
  938. /* Override PHY config settings */
  939. {MIIM_CIS8201_AUX_CONSTAT,
  940. MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
  941. /* Configure some basic stuff */
  942. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  943. {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT,
  944. &mii_cis8204_fixled},
  945. {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT,
  946. &mii_cis8204_setmode},
  947. {miim_end,}
  948. },
  949. (struct phy_cmd[]){ /* startup */
  950. /* Read the Status (2x to make sure link is right) */
  951. {MIIM_STATUS, miim_read, NULL},
  952. /* Auto-negotiate */
  953. {MIIM_STATUS, miim_read, &mii_parse_sr},
  954. /* Read the status */
  955. {MIIM_CIS8201_AUX_CONSTAT, miim_read,
  956. &mii_parse_cis8201},
  957. {miim_end,}
  958. },
  959. (struct phy_cmd[]){ /* shutdown */
  960. {miim_end,}
  961. },
  962. };
  963. /* Cicada 8201 */
  964. struct phy_info phy_info_cis8201 = {
  965. 0xfc41,
  966. "CIS8201",
  967. 4,
  968. (struct phy_cmd[]){ /* config */
  969. /* Override PHY config settings */
  970. {MIIM_CIS8201_AUX_CONSTAT,
  971. MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
  972. /* Set up the interface mode */
  973. {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT,
  974. NULL},
  975. /* Configure some basic stuff */
  976. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  977. {miim_end,}
  978. },
  979. (struct phy_cmd[]){ /* startup */
  980. /* Read the Status (2x to make sure link is right) */
  981. {MIIM_STATUS, miim_read, NULL},
  982. /* Auto-negotiate */
  983. {MIIM_STATUS, miim_read, &mii_parse_sr},
  984. /* Read the status */
  985. {MIIM_CIS8201_AUX_CONSTAT, miim_read,
  986. &mii_parse_cis8201},
  987. {miim_end,}
  988. },
  989. (struct phy_cmd[]){ /* shutdown */
  990. {miim_end,}
  991. },
  992. };
  993. struct phy_info phy_info_VSC8244 = {
  994. 0x3f1b,
  995. "Vitesse VSC8244",
  996. 6,
  997. (struct phy_cmd[]){ /* config */
  998. /* Override PHY config settings */
  999. /* Configure some basic stuff */
  1000. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  1001. {miim_end,}
  1002. },
  1003. (struct phy_cmd[]){ /* startup */
  1004. /* Read the Status (2x to make sure link is right) */
  1005. {MIIM_STATUS, miim_read, NULL},
  1006. /* Auto-negotiate */
  1007. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1008. /* Read the status */
  1009. {MIIM_VSC8244_AUX_CONSTAT, miim_read,
  1010. &mii_parse_vsc8244},
  1011. {miim_end,}
  1012. },
  1013. (struct phy_cmd[]){ /* shutdown */
  1014. {miim_end,}
  1015. },
  1016. };
  1017. struct phy_info phy_info_dm9161 = {
  1018. 0x0181b88,
  1019. "Davicom DM9161E",
  1020. 4,
  1021. (struct phy_cmd[]){ /* config */
  1022. {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
  1023. /* Do not bypass the scrambler/descrambler */
  1024. {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
  1025. /* Clear 10BTCSR to default */
  1026. {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT,
  1027. NULL},
  1028. /* Configure some basic stuff */
  1029. {MIIM_CONTROL, MIIM_CR_INIT, NULL},
  1030. /* Restart Auto Negotiation */
  1031. {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
  1032. {miim_end,}
  1033. },
  1034. (struct phy_cmd[]){ /* startup */
  1035. /* Status is read once to clear old link state */
  1036. {MIIM_STATUS, miim_read, NULL},
  1037. /* Auto-negotiate */
  1038. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1039. /* Read the status */
  1040. {MIIM_DM9161_SCSR, miim_read,
  1041. &mii_parse_dm9161_scsr},
  1042. {miim_end,}
  1043. },
  1044. (struct phy_cmd[]){ /* shutdown */
  1045. {miim_end,}
  1046. },
  1047. };
  1048. /* a generic flavor. */
  1049. struct phy_info phy_info_generic = {
  1050. 0,
  1051. "Unknown/Generic PHY",
  1052. 32,
  1053. (struct phy_cmd[]) { /* config */
  1054. {PHY_BMCR, PHY_BMCR_RESET, NULL},
  1055. {PHY_BMCR, PHY_BMCR_AUTON|PHY_BMCR_RST_NEG, NULL},
  1056. {miim_end,}
  1057. },
  1058. (struct phy_cmd[]) { /* startup */
  1059. {PHY_BMSR, miim_read, NULL},
  1060. {PHY_BMSR, miim_read, &mii_parse_sr},
  1061. {PHY_BMSR, miim_read, &mii_parse_link},
  1062. {miim_end,}
  1063. },
  1064. (struct phy_cmd[]) { /* shutdown */
  1065. {miim_end,}
  1066. }
  1067. };
  1068. uint mii_parse_lxt971_sr2(uint mii_reg, struct tsec_private *priv)
  1069. {
  1070. unsigned int speed;
  1071. if (priv->link) {
  1072. speed = mii_reg & MIIM_LXT971_SR2_SPEED_MASK;
  1073. switch (speed) {
  1074. case MIIM_LXT971_SR2_10HDX:
  1075. priv->speed = 10;
  1076. priv->duplexity = 0;
  1077. break;
  1078. case MIIM_LXT971_SR2_10FDX:
  1079. priv->speed = 10;
  1080. priv->duplexity = 1;
  1081. break;
  1082. case MIIM_LXT971_SR2_100HDX:
  1083. priv->speed = 100;
  1084. priv->duplexity = 0;
  1085. default:
  1086. priv->speed = 100;
  1087. priv->duplexity = 1;
  1088. break;
  1089. }
  1090. } else {
  1091. priv->speed = 0;
  1092. priv->duplexity = 0;
  1093. }
  1094. return 0;
  1095. }
  1096. static struct phy_info phy_info_lxt971 = {
  1097. 0x0001378e,
  1098. "LXT971",
  1099. 4,
  1100. (struct phy_cmd[]){ /* config */
  1101. {MIIM_CR, MIIM_CR_INIT, mii_cr_init}, /* autonegotiate */
  1102. {miim_end,}
  1103. },
  1104. (struct phy_cmd[]){ /* startup - enable interrupts */
  1105. /* { 0x12, 0x00f2, NULL }, */
  1106. {MIIM_STATUS, miim_read, NULL},
  1107. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1108. {MIIM_LXT971_SR2, miim_read, &mii_parse_lxt971_sr2},
  1109. {miim_end,}
  1110. },
  1111. (struct phy_cmd[]){ /* shutdown - disable interrupts */
  1112. {miim_end,}
  1113. },
  1114. };
  1115. /* Parse the DP83865's link and auto-neg status register for speed and duplex
  1116. * information
  1117. */
  1118. uint mii_parse_dp83865_lanr(uint mii_reg, struct tsec_private *priv)
  1119. {
  1120. switch (mii_reg & MIIM_DP83865_SPD_MASK) {
  1121. case MIIM_DP83865_SPD_1000:
  1122. priv->speed = 1000;
  1123. break;
  1124. case MIIM_DP83865_SPD_100:
  1125. priv->speed = 100;
  1126. break;
  1127. default:
  1128. priv->speed = 10;
  1129. break;
  1130. }
  1131. if (mii_reg & MIIM_DP83865_DPX_FULL)
  1132. priv->duplexity = 1;
  1133. else
  1134. priv->duplexity = 0;
  1135. return 0;
  1136. }
  1137. struct phy_info phy_info_dp83865 = {
  1138. 0x20005c7,
  1139. "NatSemi DP83865",
  1140. 4,
  1141. (struct phy_cmd[]){ /* config */
  1142. {MIIM_CONTROL, MIIM_DP83865_CR_INIT, NULL},
  1143. {miim_end,}
  1144. },
  1145. (struct phy_cmd[]){ /* startup */
  1146. /* Status is read once to clear old link state */
  1147. {MIIM_STATUS, miim_read, NULL},
  1148. /* Auto-negotiate */
  1149. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1150. /* Read the link and auto-neg status */
  1151. {MIIM_DP83865_LANR, miim_read,
  1152. &mii_parse_dp83865_lanr},
  1153. {miim_end,}
  1154. },
  1155. (struct phy_cmd[]){ /* shutdown */
  1156. {miim_end,}
  1157. },
  1158. };
  1159. struct phy_info *phy_info[] = {
  1160. &phy_info_cis8204,
  1161. &phy_info_cis8201,
  1162. &phy_info_BCM5461S,
  1163. &phy_info_BCM5464S,
  1164. &phy_info_M88E1011S,
  1165. &phy_info_M88E1111S,
  1166. &phy_info_M88E1145,
  1167. &phy_info_M88E1149S,
  1168. &phy_info_dm9161,
  1169. &phy_info_lxt971,
  1170. &phy_info_VSC8244,
  1171. &phy_info_dp83865,
  1172. &phy_info_generic,
  1173. NULL
  1174. };
  1175. /* Grab the identifier of the device's PHY, and search through
  1176. * all of the known PHYs to see if one matches. If so, return
  1177. * it, if not, return NULL
  1178. */
  1179. struct phy_info *get_phy_info(struct eth_device *dev)
  1180. {
  1181. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  1182. uint phy_reg, phy_ID;
  1183. int i;
  1184. struct phy_info *theInfo = NULL;
  1185. /* Grab the bits from PHYIR1, and put them in the upper half */
  1186. phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
  1187. phy_ID = (phy_reg & 0xffff) << 16;
  1188. /* Grab the bits from PHYIR2, and put them in the lower half */
  1189. phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
  1190. phy_ID |= (phy_reg & 0xffff);
  1191. /* loop through all the known PHY types, and find one that */
  1192. /* matches the ID we read from the PHY. */
  1193. for (i = 0; phy_info[i]; i++) {
  1194. if (phy_info[i]->id == (phy_ID >> phy_info[i]->shift)) {
  1195. theInfo = phy_info[i];
  1196. break;
  1197. }
  1198. }
  1199. if (theInfo == NULL) {
  1200. printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
  1201. return NULL;
  1202. } else {
  1203. debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID);
  1204. }
  1205. return theInfo;
  1206. }
  1207. /* Execute the given series of commands on the given device's
  1208. * PHY, running functions as necessary
  1209. */
  1210. void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
  1211. {
  1212. int i;
  1213. uint result;
  1214. volatile tsec_t *phyregs = priv->phyregs;
  1215. phyregs->miimcfg = MIIMCFG_RESET;
  1216. phyregs->miimcfg = MIIMCFG_INIT_VALUE;
  1217. while (phyregs->miimind & MIIMIND_BUSY) ;
  1218. for (i = 0; cmd->mii_reg != miim_end; i++) {
  1219. if (cmd->mii_data == miim_read) {
  1220. result = read_phy_reg(priv, cmd->mii_reg);
  1221. if (cmd->funct != NULL)
  1222. (*(cmd->funct)) (result, priv);
  1223. } else {
  1224. if (cmd->funct != NULL)
  1225. result = (*(cmd->funct)) (cmd->mii_reg, priv);
  1226. else
  1227. result = cmd->mii_data;
  1228. write_phy_reg(priv, cmd->mii_reg, result);
  1229. }
  1230. cmd++;
  1231. }
  1232. }
  1233. /* Relocate the function pointers in the phy cmd lists */
  1234. static void relocate_cmds(void)
  1235. {
  1236. struct phy_cmd **cmdlistptr;
  1237. struct phy_cmd *cmd;
  1238. int i, j, k;
  1239. for (i = 0; phy_info[i]; i++) {
  1240. /* First thing's first: relocate the pointers to the
  1241. * PHY command structures (the structs were done) */
  1242. phy_info[i] = (struct phy_info *)((uint) phy_info[i]
  1243. + gd->reloc_off);
  1244. phy_info[i]->name += gd->reloc_off;
  1245. phy_info[i]->config =
  1246. (struct phy_cmd *)((uint) phy_info[i]->config
  1247. + gd->reloc_off);
  1248. phy_info[i]->startup =
  1249. (struct phy_cmd *)((uint) phy_info[i]->startup
  1250. + gd->reloc_off);
  1251. phy_info[i]->shutdown =
  1252. (struct phy_cmd *)((uint) phy_info[i]->shutdown
  1253. + gd->reloc_off);
  1254. cmdlistptr = &phy_info[i]->config;
  1255. j = 0;
  1256. for (; cmdlistptr <= &phy_info[i]->shutdown; cmdlistptr++) {
  1257. k = 0;
  1258. for (cmd = *cmdlistptr;
  1259. cmd->mii_reg != miim_end;
  1260. cmd++) {
  1261. /* Only relocate non-NULL pointers */
  1262. if (cmd->funct)
  1263. cmd->funct += gd->reloc_off;
  1264. k++;
  1265. }
  1266. j++;
  1267. }
  1268. }
  1269. relocated = 1;
  1270. }
  1271. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
  1272. && !defined(BITBANGMII)
  1273. struct tsec_private *get_priv_for_phy(unsigned char phyaddr)
  1274. {
  1275. int i;
  1276. for (i = 0; i < MAXCONTROLLERS; i++) {
  1277. if (privlist[i]->phyaddr == phyaddr)
  1278. return privlist[i];
  1279. }
  1280. return NULL;
  1281. }
  1282. /*
  1283. * Read a MII PHY register.
  1284. *
  1285. * Returns:
  1286. * 0 on success
  1287. */
  1288. static int tsec_miiphy_read(char *devname, unsigned char addr,
  1289. unsigned char reg, unsigned short *value)
  1290. {
  1291. unsigned short ret;
  1292. struct tsec_private *priv = get_priv_for_phy(addr);
  1293. if (NULL == priv) {
  1294. printf("Can't read PHY at address %d\n", addr);
  1295. return -1;
  1296. }
  1297. ret = (unsigned short)read_phy_reg(priv, reg);
  1298. *value = ret;
  1299. return 0;
  1300. }
  1301. /*
  1302. * Write a MII PHY register.
  1303. *
  1304. * Returns:
  1305. * 0 on success
  1306. */
  1307. static int tsec_miiphy_write(char *devname, unsigned char addr,
  1308. unsigned char reg, unsigned short value)
  1309. {
  1310. struct tsec_private *priv = get_priv_for_phy(addr);
  1311. if (NULL == priv) {
  1312. printf("Can't write PHY at address %d\n", addr);
  1313. return -1;
  1314. }
  1315. write_phy_reg(priv, reg, value);
  1316. return 0;
  1317. }
  1318. #endif
  1319. #ifdef CONFIG_MCAST_TFTP
  1320. /* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
  1321. /* Set the appropriate hash bit for the given addr */
  1322. /* The algorithm works like so:
  1323. * 1) Take the Destination Address (ie the multicast address), and
  1324. * do a CRC on it (little endian), and reverse the bits of the
  1325. * result.
  1326. * 2) Use the 8 most significant bits as a hash into a 256-entry
  1327. * table. The table is controlled through 8 32-bit registers:
  1328. * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
  1329. * gaddr7. This means that the 3 most significant bits in the
  1330. * hash index which gaddr register to use, and the 5 other bits
  1331. * indicate which bit (assuming an IBM numbering scheme, which
  1332. * for PowerPC (tm) is usually the case) in the tregister holds
  1333. * the entry. */
  1334. static int
  1335. tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set)
  1336. {
  1337. struct tsec_private *priv = privlist[1];
  1338. volatile tsec_t *regs = priv->regs;
  1339. volatile u32 *reg_array, value;
  1340. u8 result, whichbit, whichreg;
  1341. result = (u8)((ether_crc(MAC_ADDR_LEN,mcast_mac) >> 24) & 0xff);
  1342. whichbit = result & 0x1f; /* the 5 LSB = which bit to set */
  1343. whichreg = result >> 5; /* the 3 MSB = which reg to set it in */
  1344. value = (1 << (31-whichbit));
  1345. reg_array = &(regs->hash.gaddr0);
  1346. if (set) {
  1347. reg_array[whichreg] |= value;
  1348. } else {
  1349. reg_array[whichreg] &= ~value;
  1350. }
  1351. return 0;
  1352. }
  1353. #endif /* Multicast TFTP ? */
  1354. #endif /* CONFIG_TSEC_ENET */