tsec.c 39 KB

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