tsec.c 42 KB

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