blob: 6bca4dc0f333a7a2dc4a703065372a38f8eb87ff [file] [log] [blame]
wdenk9c53f402003-10-15 23:53:47 +00001/*
wdenka445ddf2004-06-09 00:34:46 +00002 * Freescale Three Speed Ethernet Controller driver
wdenk9c53f402003-10-15 23:53:47 +00003 *
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 *
Andy Fleming2fffa052007-04-23 02:24:28 -05008 * Copyright 2004, 2007 Freescale Semiconductor, Inc.
wdenk9c53f402003-10-15 23:53:47 +00009 * (C) Copyright 2003, Motorola, Inc.
wdenk9c53f402003-10-15 23:53:47 +000010 * author Andy Fleming
11 *
12 */
13
14#include <config.h>
wdenk9c53f402003-10-15 23:53:47 +000015#include <common.h>
16#include <malloc.h>
17#include <net.h>
18#include <command.h>
19
20#if defined(CONFIG_TSEC_ENET)
21#include "tsec.h"
Marian Balakowiczaab8c492005-10-28 22:30:33 +020022#include "miiphy.h"
wdenk9c53f402003-10-15 23:53:47 +000023
Wolfgang Denk6405a152006-03-31 18:32:53 +020024DECLARE_GLOBAL_DATA_PTR;
25
Marian Balakowiczaab8c492005-10-28 22:30:33 +020026#define TX_BUF_CNT 2
wdenk9c53f402003-10-15 23:53:47 +000027
Jon Loeligerb7ced082006-10-10 17:03:43 -050028static uint rxIdx; /* index of the current RX buffer */
29static uint txIdx; /* index of the current TX buffer */
wdenk9c53f402003-10-15 23:53:47 +000030
31typedef volatile struct rtxbd {
32 txbd8_t txbd[TX_BUF_CNT];
33 rxbd8_t rxbd[PKTBUFSRX];
Jon Loeligerb7ced082006-10-10 17:03:43 -050034} RTXBD;
wdenk9c53f402003-10-15 23:53:47 +000035
wdenka445ddf2004-06-09 00:34:46 +000036struct tsec_info_struct {
37 unsigned int phyaddr;
Jon Loeliger77a4f6e2005-07-25 14:05:07 -050038 u32 flags;
wdenka445ddf2004-06-09 00:34:46 +000039 unsigned int phyregidx;
40};
41
wdenka445ddf2004-06-09 00:34:46 +000042/* The tsec_info structure contains 3 values which the
43 * driver uses to determine how to operate a given ethernet
Andy Fleming239e75f2006-09-13 10:34:18 -050044 * device. The information needed is:
wdenka445ddf2004-06-09 00:34:46 +000045 * phyaddr - The address of the PHY which is attached to
wdenkbfad55d2005-03-14 23:56:42 +000046 * the given device.
wdenka445ddf2004-06-09 00:34:46 +000047 *
Jon Loeliger77a4f6e2005-07-25 14:05:07 -050048 * flags - This variable indicates whether the device
49 * supports gigabit speed ethernet, and whether it should be
50 * in reduced mode.
wdenka445ddf2004-06-09 00:34:46 +000051 *
52 * phyregidx - This variable specifies which ethernet device
wdenkbfad55d2005-03-14 23:56:42 +000053 * controls the MII Management registers which are connected
Andy Fleming239e75f2006-09-13 10:34:18 -050054 * to the PHY. For now, only TSEC1 (index 0) has
wdenkbfad55d2005-03-14 23:56:42 +000055 * access to the PHYs, so all of the entries have "0".
wdenka445ddf2004-06-09 00:34:46 +000056 *
57 * The values specified in the table are taken from the board's
58 * config file in include/configs/. When implementing a new
59 * board with ethernet capability, it is necessary to define:
Andy Fleming239e75f2006-09-13 10:34:18 -050060 * TSECn_PHY_ADDR
61 * TSECn_PHYIDX
wdenka445ddf2004-06-09 00:34:46 +000062 *
Andy Fleming239e75f2006-09-13 10:34:18 -050063 * for n = 1,2,3, etc. And for FEC:
wdenka445ddf2004-06-09 00:34:46 +000064 * FEC_PHY_ADDR
65 * FEC_PHYIDX
66 */
67static struct tsec_info_struct tsec_info[] = {
Andy Fleming09b88df2007-08-15 20:03:25 -050068#ifdef CONFIG_TSEC1
69 {TSEC1_PHY_ADDR, TSEC1_FLAGS, TSEC1_PHYIDX},
Zach Sadeckif5dd2992007-07-31 12:27:25 -050070#else
Jon Loeligerb7ced082006-10-10 17:03:43 -050071 {0, 0, 0},
wdenka445ddf2004-06-09 00:34:46 +000072#endif
Andy Fleming09b88df2007-08-15 20:03:25 -050073#ifdef CONFIG_TSEC2
74 {TSEC2_PHY_ADDR, TSEC2_FLAGS, TSEC2_PHYIDX},
Zach Sadeckif5dd2992007-07-31 12:27:25 -050075#else
Jon Loeligerb7ced082006-10-10 17:03:43 -050076 {0, 0, 0},
wdenka445ddf2004-06-09 00:34:46 +000077#endif
78#ifdef CONFIG_MPC85XX_FEC
Andy Fleming09b88df2007-08-15 20:03:25 -050079 {FEC_PHY_ADDR, FEC_FLAGS, FEC_PHYIDX},
wdenkbfad55d2005-03-14 23:56:42 +000080#else
Andy Fleming09b88df2007-08-15 20:03:25 -050081#ifdef CONFIG_TSEC3
82 {TSEC3_PHY_ADDR, TSEC3_FLAGS, TSEC3_PHYIDX},
Jon Loeliger5c8aa972006-04-26 17:58:56 -050083#else
Jon Loeligerb7ced082006-10-10 17:03:43 -050084 {0, 0, 0},
Jon Loeliger5c8aa972006-04-26 17:58:56 -050085#endif
Andy Fleming09b88df2007-08-15 20:03:25 -050086#ifdef CONFIG_TSEC4
87 {TSEC4_PHY_ADDR, TSEC4_FLAGS, TSEC4_PHYIDX},
Jon Loeliger5c8aa972006-04-26 17:58:56 -050088#else
Jon Loeligerb7ced082006-10-10 17:03:43 -050089 {0, 0, 0},
Andy Fleming09b88df2007-08-15 20:03:25 -050090#endif /* CONFIG_TSEC4 */
91#endif /* CONFIG_MPC85XX_FEC */
wdenka445ddf2004-06-09 00:34:46 +000092};
93
Jon Loeliger77a4f6e2005-07-25 14:05:07 -050094#define MAXCONTROLLERS (4)
wdenka445ddf2004-06-09 00:34:46 +000095
96static int relocated = 0;
97
98static struct tsec_private *privlist[MAXCONTROLLERS];
99
wdenk9c53f402003-10-15 23:53:47 +0000100#ifdef __GNUC__
101static RTXBD rtx __attribute__ ((aligned(8)));
102#else
103#error "rtx must be 64-bit aligned"
104#endif
105
Jon Loeligerb7ced082006-10-10 17:03:43 -0500106static int tsec_send(struct eth_device *dev,
107 volatile void *packet, int length);
108static int tsec_recv(struct eth_device *dev);
109static int tsec_init(struct eth_device *dev, bd_t * bd);
110static void tsec_halt(struct eth_device *dev);
111static void init_registers(volatile tsec_t * regs);
wdenka445ddf2004-06-09 00:34:46 +0000112static void startup_tsec(struct eth_device *dev);
113static int init_phy(struct eth_device *dev);
114void write_phy_reg(struct tsec_private *priv, uint regnum, uint value);
115uint read_phy_reg(struct tsec_private *priv, uint regnum);
Jon Loeligerb7ced082006-10-10 17:03:43 -0500116struct phy_info *get_phy_info(struct eth_device *dev);
wdenka445ddf2004-06-09 00:34:46 +0000117void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd);
118static void adjust_link(struct eth_device *dev);
119static void relocate_cmds(void);
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200120static int tsec_miiphy_write(char *devname, unsigned char addr,
Jon Loeligerb7ced082006-10-10 17:03:43 -0500121 unsigned char reg, unsigned short value);
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200122static int tsec_miiphy_read(char *devname, unsigned char addr,
Jon Loeligerb7ced082006-10-10 17:03:43 -0500123 unsigned char reg, unsigned short *value);
David Updegraff7280da72007-06-11 10:41:07 -0500124#ifdef CONFIG_MCAST_TFTP
125static int tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set);
126#endif
wdenk78924a72004-04-18 21:45:42 +0000127
wdenka445ddf2004-06-09 00:34:46 +0000128/* Initialize device structure. Returns success if PHY
129 * initialization succeeded (i.e. if it recognizes the PHY)
130 */
Jon Loeligerb7ced082006-10-10 17:03:43 -0500131int tsec_initialize(bd_t * bis, int index, char *devname)
wdenk9c53f402003-10-15 23:53:47 +0000132{
Jon Loeligerb7ced082006-10-10 17:03:43 -0500133 struct eth_device *dev;
wdenk9c53f402003-10-15 23:53:47 +0000134 int i;
wdenka445ddf2004-06-09 00:34:46 +0000135 struct tsec_private *priv;
wdenk9c53f402003-10-15 23:53:47 +0000136
Jon Loeligerb7ced082006-10-10 17:03:43 -0500137 dev = (struct eth_device *)malloc(sizeof *dev);
wdenk9c53f402003-10-15 23:53:47 +0000138
Jon Loeligerb7ced082006-10-10 17:03:43 -0500139 if (NULL == dev)
wdenk9c53f402003-10-15 23:53:47 +0000140 return 0;
141
142 memset(dev, 0, sizeof *dev);
143
Jon Loeligerb7ced082006-10-10 17:03:43 -0500144 priv = (struct tsec_private *)malloc(sizeof(*priv));
wdenka445ddf2004-06-09 00:34:46 +0000145
Jon Loeligerb7ced082006-10-10 17:03:43 -0500146 if (NULL == priv)
wdenka445ddf2004-06-09 00:34:46 +0000147 return 0;
148
149 privlist[index] = priv;
Jon Loeligerb7ced082006-10-10 17:03:43 -0500150 priv->regs = (volatile tsec_t *)(TSEC_BASE_ADDR + index * TSEC_SIZE);
wdenka445ddf2004-06-09 00:34:46 +0000151 priv->phyregs = (volatile tsec_t *)(TSEC_BASE_ADDR +
Jon Loeligerb7ced082006-10-10 17:03:43 -0500152 tsec_info[index].phyregidx *
153 TSEC_SIZE);
wdenka445ddf2004-06-09 00:34:46 +0000154
155 priv->phyaddr = tsec_info[index].phyaddr;
Jon Loeliger77a4f6e2005-07-25 14:05:07 -0500156 priv->flags = tsec_info[index].flags;
wdenka445ddf2004-06-09 00:34:46 +0000157
Jon Loeliger77a4f6e2005-07-25 14:05:07 -0500158 sprintf(dev->name, devname);
wdenk9c53f402003-10-15 23:53:47 +0000159 dev->iobase = 0;
Jon Loeligerb7ced082006-10-10 17:03:43 -0500160 dev->priv = priv;
161 dev->init = tsec_init;
162 dev->halt = tsec_halt;
163 dev->send = tsec_send;
164 dev->recv = tsec_recv;
David Updegraff7280da72007-06-11 10:41:07 -0500165#ifdef CONFIG_MCAST_TFTP
166 dev->mcast = tsec_mcast_addr;
167#endif
wdenk9c53f402003-10-15 23:53:47 +0000168
169 /* Tell u-boot to get the addr from the env */
Jon Loeligerb7ced082006-10-10 17:03:43 -0500170 for (i = 0; i < 6; i++)
wdenk9c53f402003-10-15 23:53:47 +0000171 dev->enetaddr[i] = 0;
172
173 eth_register(dev);
174
wdenka445ddf2004-06-09 00:34:46 +0000175 /* Reset the MAC */
176 priv->regs->maccfg1 |= MACCFG1_SOFT_RESET;
177 priv->regs->maccfg1 &= ~(MACCFG1_SOFT_RESET);
wdenk78924a72004-04-18 21:45:42 +0000178
Jon Loeliger82ecaad2007-07-09 17:39:42 -0500179#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200180 && !defined(BITBANGMII)
181 miiphy_register(dev->name, tsec_miiphy_read, tsec_miiphy_write);
182#endif
183
wdenka445ddf2004-06-09 00:34:46 +0000184 /* Try to initialize PHY here, and return */
185 return init_phy(dev);
wdenk9c53f402003-10-15 23:53:47 +0000186}
187
wdenk9c53f402003-10-15 23:53:47 +0000188/* Initializes data structures and registers for the controller,
wdenkbfad55d2005-03-14 23:56:42 +0000189 * and brings the interface up. Returns the link status, meaning
wdenka445ddf2004-06-09 00:34:46 +0000190 * that it returns success if the link is up, failure otherwise.
Jon Loeligerb7ced082006-10-10 17:03:43 -0500191 * This allows u-boot to find the first active controller.
192 */
193int tsec_init(struct eth_device *dev, bd_t * bd)
wdenk9c53f402003-10-15 23:53:47 +0000194{
wdenk9c53f402003-10-15 23:53:47 +0000195 uint tempval;
196 char tmpbuf[MAC_ADDR_LEN];
197 int i;
wdenka445ddf2004-06-09 00:34:46 +0000198 struct tsec_private *priv = (struct tsec_private *)dev->priv;
199 volatile tsec_t *regs = priv->regs;
wdenk9c53f402003-10-15 23:53:47 +0000200
201 /* Make sure the controller is stopped */
202 tsec_halt(dev);
203
wdenka445ddf2004-06-09 00:34:46 +0000204 /* Init MACCFG2. Defaults to GMII */
wdenk9c53f402003-10-15 23:53:47 +0000205 regs->maccfg2 = MACCFG2_INIT_SETTINGS;
206
207 /* Init ECNTRL */
208 regs->ecntrl = ECNTRL_INIT_SETTINGS;
209
210 /* Copy the station address into the address registers.
211 * Backwards, because little endian MACS are dumb */
Jon Loeligerb7ced082006-10-10 17:03:43 -0500212 for (i = 0; i < MAC_ADDR_LEN; i++) {
wdenka445ddf2004-06-09 00:34:46 +0000213 tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i];
wdenk9c53f402003-10-15 23:53:47 +0000214 }
Jon Loeligerb7ced082006-10-10 17:03:43 -0500215 regs->macstnaddr1 = *((uint *) (tmpbuf));
wdenk9c53f402003-10-15 23:53:47 +0000216
Jon Loeligerb7ced082006-10-10 17:03:43 -0500217 tempval = *((uint *) (tmpbuf + 4));
wdenk9c53f402003-10-15 23:53:47 +0000218
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200219 regs->macstnaddr2 = tempval;
wdenk9c53f402003-10-15 23:53:47 +0000220
wdenk9c53f402003-10-15 23:53:47 +0000221 /* reset the indices to zero */
222 rxIdx = 0;
223 txIdx = 0;
224
225 /* Clear out (for the most part) the other registers */
226 init_registers(regs);
227
228 /* Ready the device for tx/rx */
wdenka445ddf2004-06-09 00:34:46 +0000229 startup_tsec(dev);
wdenk9c53f402003-10-15 23:53:47 +0000230
wdenka445ddf2004-06-09 00:34:46 +0000231 /* If there's no link, fail */
232 return priv->link;
233
234}
wdenk9c53f402003-10-15 23:53:47 +0000235
wdenka445ddf2004-06-09 00:34:46 +0000236/* Write value to the device's PHY through the registers
237 * specified in priv, modifying the register specified in regnum.
238 * It will wait for the write to be done (or for a timeout to
239 * expire) before exiting
240 */
241void write_phy_reg(struct tsec_private *priv, uint regnum, uint value)
242{
243 volatile tsec_t *regbase = priv->phyregs;
244 uint phyid = priv->phyaddr;
Jon Loeligerb7ced082006-10-10 17:03:43 -0500245 int timeout = 1000000;
wdenka445ddf2004-06-09 00:34:46 +0000246
247 regbase->miimadd = (phyid << 8) | regnum;
248 regbase->miimcon = value;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500249 asm("sync");
wdenka445ddf2004-06-09 00:34:46 +0000250
Jon Loeligerb7ced082006-10-10 17:03:43 -0500251 timeout = 1000000;
252 while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
wdenk9c53f402003-10-15 23:53:47 +0000253}
254
wdenka445ddf2004-06-09 00:34:46 +0000255/* Reads register regnum on the device's PHY through the
wdenkbfad55d2005-03-14 23:56:42 +0000256 * registers specified in priv. It lowers and raises the read
wdenka445ddf2004-06-09 00:34:46 +0000257 * command, and waits for the data to become valid (miimind
258 * notvalid bit cleared), and the bus to cease activity (miimind
259 * busy bit cleared), and then returns the value
260 */
261uint read_phy_reg(struct tsec_private *priv, uint regnum)
wdenk9c53f402003-10-15 23:53:47 +0000262{
263 uint value;
wdenka445ddf2004-06-09 00:34:46 +0000264 volatile tsec_t *regbase = priv->phyregs;
265 uint phyid = priv->phyaddr;
wdenk9c53f402003-10-15 23:53:47 +0000266
wdenka445ddf2004-06-09 00:34:46 +0000267 /* Put the address of the phy, and the register
268 * number into MIIMADD */
269 regbase->miimadd = (phyid << 8) | regnum;
wdenk9c53f402003-10-15 23:53:47 +0000270
271 /* Clear the command register, and wait */
272 regbase->miimcom = 0;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500273 asm("sync");
wdenk9c53f402003-10-15 23:53:47 +0000274
275 /* Initiate a read command, and wait */
276 regbase->miimcom = MIIM_READ_COMMAND;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500277 asm("sync");
wdenk9c53f402003-10-15 23:53:47 +0000278
279 /* Wait for the the indication that the read is done */
Jon Loeligerb7ced082006-10-10 17:03:43 -0500280 while ((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY))) ;
wdenk9c53f402003-10-15 23:53:47 +0000281
282 /* Grab the value read from the PHY */
283 value = regbase->miimstat;
284
285 return value;
286}
287
wdenka445ddf2004-06-09 00:34:46 +0000288/* Discover which PHY is attached to the device, and configure it
289 * properly. If the PHY is not recognized, then return 0
290 * (failure). Otherwise, return 1
291 */
292static int init_phy(struct eth_device *dev)
wdenk9c53f402003-10-15 23:53:47 +0000293{
wdenka445ddf2004-06-09 00:34:46 +0000294 struct tsec_private *priv = (struct tsec_private *)dev->priv;
295 struct phy_info *curphy;
Jon Loeligerb7ced082006-10-10 17:03:43 -0500296 volatile tsec_t *regs = (volatile tsec_t *)(TSEC_BASE_ADDR);
wdenk9c53f402003-10-15 23:53:47 +0000297
298 /* Assign a Physical address to the TBI */
Joe Hamman4290d4c2007-08-09 09:08:18 -0500299 regs->tbipa = CFG_TBIPA_VALUE;
Jon Loeligerb7ced082006-10-10 17:03:43 -0500300 regs = (volatile tsec_t *)(TSEC_BASE_ADDR + TSEC_SIZE);
Joe Hamman4290d4c2007-08-09 09:08:18 -0500301 regs->tbipa = CFG_TBIPA_VALUE;
Jon Loeligerb7ced082006-10-10 17:03:43 -0500302 asm("sync");
wdenkf41ff3b2005-04-04 23:43:44 +0000303
304 /* Reset MII (due to new addresses) */
305 priv->phyregs->miimcfg = MIIMCFG_RESET;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500306 asm("sync");
wdenkf41ff3b2005-04-04 23:43:44 +0000307 priv->phyregs->miimcfg = MIIMCFG_INIT_VALUE;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500308 asm("sync");
Jon Loeligerb7ced082006-10-10 17:03:43 -0500309 while (priv->phyregs->miimind & MIIMIND_BUSY) ;
wdenk9c53f402003-10-15 23:53:47 +0000310
Jon Loeligerb7ced082006-10-10 17:03:43 -0500311 if (0 == relocated)
wdenka445ddf2004-06-09 00:34:46 +0000312 relocate_cmds();
wdenk9c53f402003-10-15 23:53:47 +0000313
wdenka445ddf2004-06-09 00:34:46 +0000314 /* Get the cmd structure corresponding to the attached
315 * PHY */
316 curphy = get_phy_info(dev);
wdenk9c53f402003-10-15 23:53:47 +0000317
Ben Warrenf11eefb2006-10-26 14:38:25 -0400318 if (curphy == NULL) {
319 priv->phyinfo = NULL;
wdenka445ddf2004-06-09 00:34:46 +0000320 printf("%s: No PHY found\n", dev->name);
wdenk9c53f402003-10-15 23:53:47 +0000321
wdenka445ddf2004-06-09 00:34:46 +0000322 return 0;
323 }
wdenk9c53f402003-10-15 23:53:47 +0000324
wdenka445ddf2004-06-09 00:34:46 +0000325 priv->phyinfo = curphy;
wdenk9c53f402003-10-15 23:53:47 +0000326
wdenka445ddf2004-06-09 00:34:46 +0000327 phy_run_commands(priv, priv->phyinfo->config);
wdenk9c53f402003-10-15 23:53:47 +0000328
wdenka445ddf2004-06-09 00:34:46 +0000329 return 1;
330}
wdenk9c53f402003-10-15 23:53:47 +0000331
Jon Loeligerb7ced082006-10-10 17:03:43 -0500332/*
333 * Returns which value to write to the control register.
334 * For 10/100, the value is slightly different
335 */
336uint mii_cr_init(uint mii_reg, struct tsec_private * priv)
wdenka445ddf2004-06-09 00:34:46 +0000337{
Jon Loeligerb7ced082006-10-10 17:03:43 -0500338 if (priv->flags & TSEC_GIGABIT)
wdenka445ddf2004-06-09 00:34:46 +0000339 return MIIM_CONTROL_INIT;
wdenk9c53f402003-10-15 23:53:47 +0000340 else
wdenka445ddf2004-06-09 00:34:46 +0000341 return MIIM_CR_INIT;
342}
wdenk9c53f402003-10-15 23:53:47 +0000343
wdenka445ddf2004-06-09 00:34:46 +0000344/* Parse the status register for link, and then do
Jon Loeligerb7ced082006-10-10 17:03:43 -0500345 * auto-negotiation
346 */
347uint mii_parse_sr(uint mii_reg, struct tsec_private * priv)
wdenka445ddf2004-06-09 00:34:46 +0000348{
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200349 /*
Andy Fleming4eb3dcf2007-08-15 20:03:44 -0500350 * Wait if the link is up, and autonegotiation is in progress
351 * (ie - we're capable and it's not done)
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200352 */
353 mii_reg = read_phy_reg(priv, MIIM_STATUS);
Andy Fleming4eb3dcf2007-08-15 20:03:44 -0500354 if ((mii_reg & MIIM_STATUS_LINK) && (mii_reg & PHY_BMSR_AUTN_ABLE)
Jon Loeligerb7ced082006-10-10 17:03:43 -0500355 && !(mii_reg & PHY_BMSR_AUTN_COMP)) {
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200356 int i = 0;
wdenk9c53f402003-10-15 23:53:47 +0000357
Jon Loeligerb7ced082006-10-10 17:03:43 -0500358 puts("Waiting for PHY auto negotiation to complete");
Andy Fleming4eb3dcf2007-08-15 20:03:44 -0500359 while (!(mii_reg & PHY_BMSR_AUTN_COMP)) {
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200360 /*
361 * Timeout reached ?
362 */
363 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
Jon Loeligerb7ced082006-10-10 17:03:43 -0500364 puts(" TIMEOUT !\n");
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200365 priv->link = 0;
Jin Zhengxiong-R64188487d2232006-06-27 18:12:23 +0800366 return 0;
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200367 }
wdenk9c53f402003-10-15 23:53:47 +0000368
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200369 if ((i++ % 1000) == 0) {
Jon Loeligerb7ced082006-10-10 17:03:43 -0500370 putc('.');
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200371 }
Jon Loeligerb7ced082006-10-10 17:03:43 -0500372 udelay(1000); /* 1 ms */
wdenka445ddf2004-06-09 00:34:46 +0000373 mii_reg = read_phy_reg(priv, MIIM_STATUS);
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200374 }
Jon Loeligerb7ced082006-10-10 17:03:43 -0500375 puts(" done\n");
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200376 priv->link = 1;
Jon Loeligerb7ced082006-10-10 17:03:43 -0500377 udelay(500000); /* another 500 ms (results in faster booting) */
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200378 } else {
Andy Fleming4eb3dcf2007-08-15 20:03:44 -0500379 if (mii_reg & MIIM_STATUS_LINK)
380 priv->link = 1;
381 else
382 priv->link = 0;
wdenk9c53f402003-10-15 23:53:47 +0000383 }
384
wdenka445ddf2004-06-09 00:34:46 +0000385 return 0;
386}
387
David Updegraff0451b012007-04-20 14:34:48 -0500388/* Generic function which updates the speed and duplex. If
389 * autonegotiation is enabled, it uses the AND of the link
390 * partner's advertised capabilities and our advertised
391 * capabilities. If autonegotiation is disabled, we use the
392 * appropriate bits in the control register.
393 *
394 * Stolen from Linux's mii.c and phy_device.c
395 */
396uint mii_parse_link(uint mii_reg, struct tsec_private *priv)
397{
398 /* We're using autonegotiation */
399 if (mii_reg & PHY_BMSR_AUTN_ABLE) {
400 uint lpa = 0;
401 uint gblpa = 0;
402
403 /* Check for gigabit capability */
404 if (mii_reg & PHY_BMSR_EXT) {
405 /* We want a list of states supported by
406 * both PHYs in the link
407 */
408 gblpa = read_phy_reg(priv, PHY_1000BTSR);
409 gblpa &= read_phy_reg(priv, PHY_1000BTCR) << 2;
410 }
411
412 /* Set the baseline so we only have to set them
413 * if they're different
414 */
415 priv->speed = 10;
416 priv->duplexity = 0;
417
418 /* Check the gigabit fields */
419 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
420 priv->speed = 1000;
421
422 if (gblpa & PHY_1000BTSR_1000FD)
423 priv->duplexity = 1;
424
425 /* We're done! */
426 return 0;
427 }
428
429 lpa = read_phy_reg(priv, PHY_ANAR);
430 lpa &= read_phy_reg(priv, PHY_ANLPAR);
431
432 if (lpa & (PHY_ANLPAR_TXFD | PHY_ANLPAR_TX)) {
433 priv->speed = 100;
434
435 if (lpa & PHY_ANLPAR_TXFD)
436 priv->duplexity = 1;
437
438 } else if (lpa & PHY_ANLPAR_10FD)
439 priv->duplexity = 1;
440 } else {
441 uint bmcr = read_phy_reg(priv, PHY_BMCR);
442
443 priv->speed = 10;
444 priv->duplexity = 0;
445
446 if (bmcr & PHY_BMCR_DPLX)
447 priv->duplexity = 1;
448
449 if (bmcr & PHY_BMCR_1000_MBPS)
450 priv->speed = 1000;
451 else if (bmcr & PHY_BMCR_100_MBPS)
452 priv->speed = 100;
453 }
454
455 return 0;
456}
457
Paul Gortmaker2bd9f1b2007-01-16 11:38:14 -0500458/*
459 * Parse the BCM54xx status register for speed and duplex information.
460 * The linux sungem_phy has this information, but in a table format.
461 */
462uint mii_parse_BCM54xx_sr(uint mii_reg, struct tsec_private *priv)
463{
464
465 switch((mii_reg & MIIM_BCM54xx_AUXSTATUS_LINKMODE_MASK) >> MIIM_BCM54xx_AUXSTATUS_LINKMODE_SHIFT){
466
467 case 1:
468 printf("Enet starting in 10BT/HD\n");
469 priv->duplexity = 0;
470 priv->speed = 10;
471 break;
472
473 case 2:
474 printf("Enet starting in 10BT/FD\n");
475 priv->duplexity = 1;
476 priv->speed = 10;
477 break;
478
479 case 3:
480 printf("Enet starting in 100BT/HD\n");
481 priv->duplexity = 0;
482 priv->speed = 100;
483 break;
484
485 case 5:
486 printf("Enet starting in 100BT/FD\n");
487 priv->duplexity = 1;
488 priv->speed = 100;
489 break;
490
491 case 6:
492 printf("Enet starting in 1000BT/HD\n");
493 priv->duplexity = 0;
494 priv->speed = 1000;
495 break;
496
497 case 7:
498 printf("Enet starting in 1000BT/FD\n");
499 priv->duplexity = 1;
500 priv->speed = 1000;
501 break;
502
503 default:
504 printf("Auto-neg error, defaulting to 10BT/HD\n");
505 priv->duplexity = 0;
506 priv->speed = 10;
507 break;
508 }
509
510 return 0;
511
512}
wdenka445ddf2004-06-09 00:34:46 +0000513/* Parse the 88E1011's status register for speed and duplex
Jon Loeligerb7ced082006-10-10 17:03:43 -0500514 * information
515 */
516uint mii_parse_88E1011_psr(uint mii_reg, struct tsec_private * priv)
wdenka445ddf2004-06-09 00:34:46 +0000517{
518 uint speed;
wdenk9c53f402003-10-15 23:53:47 +0000519
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200520 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
521
Andy Fleming4eb3dcf2007-08-15 20:03:44 -0500522 if ((mii_reg & MIIM_88E1011_PHYSTAT_LINK) &&
523 !(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200524 int i = 0;
525
Jon Loeligerb7ced082006-10-10 17:03:43 -0500526 puts("Waiting for PHY realtime link");
Andy Fleming4eb3dcf2007-08-15 20:03:44 -0500527 while (!(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
528 /* Timeout reached ? */
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200529 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
Jon Loeligerb7ced082006-10-10 17:03:43 -0500530 puts(" TIMEOUT !\n");
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200531 priv->link = 0;
532 break;
533 }
534
535 if ((i++ % 1000) == 0) {
Jon Loeligerb7ced082006-10-10 17:03:43 -0500536 putc('.');
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200537 }
Jon Loeligerb7ced082006-10-10 17:03:43 -0500538 udelay(1000); /* 1 ms */
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200539 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
540 }
Jon Loeligerb7ced082006-10-10 17:03:43 -0500541 puts(" done\n");
542 udelay(500000); /* another 500 ms (results in faster booting) */
Andy Fleming4eb3dcf2007-08-15 20:03:44 -0500543 } else {
544 if (mii_reg & MIIM_88E1011_PHYSTAT_LINK)
545 priv->link = 1;
546 else
547 priv->link = 0;
Stefan Roesec0dc34f2005-09-21 18:20:22 +0200548 }
549
Jon Loeligerb7ced082006-10-10 17:03:43 -0500550 if (mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
wdenka445ddf2004-06-09 00:34:46 +0000551 priv->duplexity = 1;
552 else
553 priv->duplexity = 0;
554
Jon Loeligerb7ced082006-10-10 17:03:43 -0500555 speed = (mii_reg & MIIM_88E1011_PHYSTAT_SPEED);
wdenka445ddf2004-06-09 00:34:46 +0000556
Jon Loeligerb7ced082006-10-10 17:03:43 -0500557 switch (speed) {
558 case MIIM_88E1011_PHYSTAT_GBIT:
559 priv->speed = 1000;
560 break;
561 case MIIM_88E1011_PHYSTAT_100:
562 priv->speed = 100;
563 break;
564 default:
565 priv->speed = 10;
wdenk9c53f402003-10-15 23:53:47 +0000566 }
567
wdenka445ddf2004-06-09 00:34:46 +0000568 return 0;
569}
570
wdenka445ddf2004-06-09 00:34:46 +0000571/* Parse the cis8201's status register for speed and duplex
Jon Loeligerb7ced082006-10-10 17:03:43 -0500572 * information
573 */
574uint mii_parse_cis8201(uint mii_reg, struct tsec_private * priv)
wdenka445ddf2004-06-09 00:34:46 +0000575{
576 uint speed;
577
Jon Loeligerb7ced082006-10-10 17:03:43 -0500578 if (mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX)
wdenka445ddf2004-06-09 00:34:46 +0000579 priv->duplexity = 1;
580 else
581 priv->duplexity = 0;
582
583 speed = mii_reg & MIIM_CIS8201_AUXCONSTAT_SPEED;
Jon Loeligerb7ced082006-10-10 17:03:43 -0500584 switch (speed) {
585 case MIIM_CIS8201_AUXCONSTAT_GBIT:
586 priv->speed = 1000;
587 break;
588 case MIIM_CIS8201_AUXCONSTAT_100:
589 priv->speed = 100;
590 break;
591 default:
592 priv->speed = 10;
593 break;
wdenk9c53f402003-10-15 23:53:47 +0000594 }
595
wdenka445ddf2004-06-09 00:34:46 +0000596 return 0;
597}
Jon Loeligerb7ced082006-10-10 17:03:43 -0500598
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500599/* Parse the vsc8244's status register for speed and duplex
Jon Loeligerb7ced082006-10-10 17:03:43 -0500600 * information
601 */
602uint mii_parse_vsc8244(uint mii_reg, struct tsec_private * priv)
Jon Loeliger5c8aa972006-04-26 17:58:56 -0500603{
Jon Loeligerb7ced082006-10-10 17:03:43 -0500604 uint speed;
wdenk9c53f402003-10-15 23:53:47 +0000605
Jon Loeligerb7ced082006-10-10 17:03:43 -0500606 if (mii_reg & MIIM_VSC8244_AUXCONSTAT_DUPLEX)
607 priv->duplexity = 1;
608 else
609 priv->duplexity = 0;
610
611 speed = mii_reg & MIIM_VSC8244_AUXCONSTAT_SPEED;
612 switch (speed) {
613 case MIIM_VSC8244_AUXCONSTAT_GBIT:
614 priv->speed = 1000;
615 break;
616 case MIIM_VSC8244_AUXCONSTAT_100:
617 priv->speed = 100;
618 break;
619 default:
620 priv->speed = 10;
621 break;
622 }
623
624 return 0;
625}
wdenka445ddf2004-06-09 00:34:46 +0000626
627/* Parse the DM9161's status register for speed and duplex
Jon Loeligerb7ced082006-10-10 17:03:43 -0500628 * information
629 */
630uint mii_parse_dm9161_scsr(uint mii_reg, struct tsec_private * priv)
wdenka445ddf2004-06-09 00:34:46 +0000631{
Jon Loeligerb7ced082006-10-10 17:03:43 -0500632 if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_100H))
wdenka445ddf2004-06-09 00:34:46 +0000633 priv->speed = 100;
634 else
635 priv->speed = 10;
636
Jon Loeligerb7ced082006-10-10 17:03:43 -0500637 if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_10F))
wdenka445ddf2004-06-09 00:34:46 +0000638 priv->duplexity = 1;
639 else
640 priv->duplexity = 0;
641
642 return 0;
643}
644
Jon Loeligerb7ced082006-10-10 17:03:43 -0500645/*
646 * Hack to write all 4 PHYs with the LED values
647 */
648uint mii_cis8204_fixled(uint mii_reg, struct tsec_private * priv)
wdenka445ddf2004-06-09 00:34:46 +0000649{
650 uint phyid;
651 volatile tsec_t *regbase = priv->phyregs;
Jon Loeligerb7ced082006-10-10 17:03:43 -0500652 int timeout = 1000000;
wdenka445ddf2004-06-09 00:34:46 +0000653
Jon Loeligerb7ced082006-10-10 17:03:43 -0500654 for (phyid = 0; phyid < 4; phyid++) {
wdenka445ddf2004-06-09 00:34:46 +0000655 regbase->miimadd = (phyid << 8) | mii_reg;
656 regbase->miimcon = MIIM_CIS8204_SLEDCON_INIT;
Eran Liberty9095d4a2005-07-28 10:08:46 -0500657 asm("sync");
wdenka445ddf2004-06-09 00:34:46 +0000658
Jon Loeligerb7ced082006-10-10 17:03:43 -0500659 timeout = 1000000;
660 while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
wdenk9c53f402003-10-15 23:53:47 +0000661 }
wdenk9c53f402003-10-15 23:53:47 +0000662
wdenka445ddf2004-06-09 00:34:46 +0000663 return MIIM_CIS8204_SLEDCON_INIT;
wdenk9c53f402003-10-15 23:53:47 +0000664}
665
Jon Loeligerb7ced082006-10-10 17:03:43 -0500666uint mii_cis8204_setmode(uint mii_reg, struct tsec_private * priv)
Jon Loeliger77a4f6e2005-07-25 14:05:07 -0500667{
668 if (priv->flags & TSEC_REDUCED)
669 return MIIM_CIS8204_EPHYCON_INIT | MIIM_CIS8204_EPHYCON_RGMII;
670 else
671 return MIIM_CIS8204_EPHYCON_INIT;
672}
wdenk9c53f402003-10-15 23:53:47 +0000673
wdenka445ddf2004-06-09 00:34:46 +0000674/* Initialized required registers to appropriate values, zeroing
675 * those we don't care about (unless zero is bad, in which case,
Jon Loeligerb7ced082006-10-10 17:03:43 -0500676 * choose a more appropriate value)
677 */
678static void init_registers(volatile tsec_t * regs)
wdenk9c53f402003-10-15 23:53:47 +0000679{
680 /* Clear IEVENT */
681 regs->ievent = IEVENT_INIT_CLEAR;
682
683 regs->imask = IMASK_INIT_CLEAR;
684
685 regs->hash.iaddr0 = 0;
686 regs->hash.iaddr1 = 0;
687 regs->hash.iaddr2 = 0;
688 regs->hash.iaddr3 = 0;
689 regs->hash.iaddr4 = 0;
690 regs->hash.iaddr5 = 0;
691 regs->hash.iaddr6 = 0;
692 regs->hash.iaddr7 = 0;
693
694 regs->hash.gaddr0 = 0;
695 regs->hash.gaddr1 = 0;
696 regs->hash.gaddr2 = 0;
697 regs->hash.gaddr3 = 0;
698 regs->hash.gaddr4 = 0;
699 regs->hash.gaddr5 = 0;
700 regs->hash.gaddr6 = 0;
701 regs->hash.gaddr7 = 0;
702
703 regs->rctrl = 0x00000000;
704
705 /* Init RMON mib registers */
706 memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
707
708 regs->rmon.cam1 = 0xffffffff;
709 regs->rmon.cam2 = 0xffffffff;
710
711 regs->mrblr = MRBLR_INIT_SETTINGS;
712
713 regs->minflr = MINFLR_INIT_SETTINGS;
714
715 regs->attr = ATTR_INIT_SETTINGS;
716 regs->attreli = ATTRELI_INIT_SETTINGS;
717
wdenka445ddf2004-06-09 00:34:46 +0000718}
719
wdenka445ddf2004-06-09 00:34:46 +0000720/* Configure maccfg2 based on negotiated speed and duplex
Jon Loeligerb7ced082006-10-10 17:03:43 -0500721 * reported by PHY handling code
722 */
wdenka445ddf2004-06-09 00:34:46 +0000723static void adjust_link(struct eth_device *dev)
724{
725 struct tsec_private *priv = (struct tsec_private *)dev->priv;
726 volatile tsec_t *regs = priv->regs;
727
Jon Loeligerb7ced082006-10-10 17:03:43 -0500728 if (priv->link) {
729 if (priv->duplexity != 0)
wdenka445ddf2004-06-09 00:34:46 +0000730 regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
731 else
732 regs->maccfg2 &= ~(MACCFG2_FULL_DUPLEX);
733
Jon Loeligerb7ced082006-10-10 17:03:43 -0500734 switch (priv->speed) {
735 case 1000:
736 regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
737 | MACCFG2_GMII);
738 break;
739 case 100:
740 case 10:
741 regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
742 | MACCFG2_MII);
Jon Loeliger77a4f6e2005-07-25 14:05:07 -0500743
Nick Spenceec9670b2006-09-07 07:39:46 -0700744 /* Set R100 bit in all modes although
745 * it is only used in RGMII mode
Jon Loeligerb7ced082006-10-10 17:03:43 -0500746 */
Nick Spenceec9670b2006-09-07 07:39:46 -0700747 if (priv->speed == 100)
Jon Loeligerb7ced082006-10-10 17:03:43 -0500748 regs->ecntrl |= ECNTRL_R100;
749 else
750 regs->ecntrl &= ~(ECNTRL_R100);
751 break;
752 default:
753 printf("%s: Speed was bad\n", dev->name);
754 break;
wdenka445ddf2004-06-09 00:34:46 +0000755 }
756
757 printf("Speed: %d, %s duplex\n", priv->speed,
Jon Loeligerb7ced082006-10-10 17:03:43 -0500758 (priv->duplexity) ? "full" : "half");
wdenka445ddf2004-06-09 00:34:46 +0000759
760 } else {
761 printf("%s: No link.\n", dev->name);
762 }
wdenk9c53f402003-10-15 23:53:47 +0000763}
764
wdenka445ddf2004-06-09 00:34:46 +0000765/* Set up the buffers and their descriptors, and bring up the
Jon Loeligerb7ced082006-10-10 17:03:43 -0500766 * interface
767 */
wdenka445ddf2004-06-09 00:34:46 +0000768static void startup_tsec(struct eth_device *dev)
wdenk9c53f402003-10-15 23:53:47 +0000769{
770 int i;
wdenka445ddf2004-06-09 00:34:46 +0000771 struct tsec_private *priv = (struct tsec_private *)dev->priv;
772 volatile tsec_t *regs = priv->regs;
wdenk9c53f402003-10-15 23:53:47 +0000773
774 /* Point to the buffer descriptors */
775 regs->tbase = (unsigned int)(&rtx.txbd[txIdx]);
776 regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
777
778 /* Initialize the Rx Buffer descriptors */
779 for (i = 0; i < PKTBUFSRX; i++) {
780 rtx.rxbd[i].status = RXBD_EMPTY;
781 rtx.rxbd[i].length = 0;
Jon Loeligerb7ced082006-10-10 17:03:43 -0500782 rtx.rxbd[i].bufPtr = (uint) NetRxPackets[i];
wdenk9c53f402003-10-15 23:53:47 +0000783 }
Jon Loeligerb7ced082006-10-10 17:03:43 -0500784 rtx.rxbd[PKTBUFSRX - 1].status |= RXBD_WRAP;
wdenk9c53f402003-10-15 23:53:47 +0000785
786 /* Initialize the TX Buffer Descriptors */
Jon Loeligerb7ced082006-10-10 17:03:43 -0500787 for (i = 0; i < TX_BUF_CNT; i++) {
wdenk9c53f402003-10-15 23:53:47 +0000788 rtx.txbd[i].status = 0;
789 rtx.txbd[i].length = 0;
790 rtx.txbd[i].bufPtr = 0;
791 }
Jon Loeligerb7ced082006-10-10 17:03:43 -0500792 rtx.txbd[TX_BUF_CNT - 1].status |= TXBD_WRAP;
wdenk9c53f402003-10-15 23:53:47 +0000793
wdenka445ddf2004-06-09 00:34:46 +0000794 /* Start up the PHY */
Ben Warrenf11eefb2006-10-26 14:38:25 -0400795 if(priv->phyinfo)
796 phy_run_commands(priv, priv->phyinfo->startup);
David Updegraff0451b012007-04-20 14:34:48 -0500797
wdenka445ddf2004-06-09 00:34:46 +0000798 adjust_link(dev);
799
wdenk9c53f402003-10-15 23:53:47 +0000800 /* Enable Transmit and Receive */
801 regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
802
803 /* Tell the DMA it is clear to go */
804 regs->dmactrl |= DMACTRL_INIT_SETTINGS;
805 regs->tstat = TSTAT_CLEAR_THALT;
806 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
807}
808
wdenkbfad55d2005-03-14 23:56:42 +0000809/* This returns the status bits of the device. The return value
wdenk9c53f402003-10-15 23:53:47 +0000810 * is never checked, and this is what the 8260 driver did, so we
wdenkbfad55d2005-03-14 23:56:42 +0000811 * do the same. Presumably, this would be zero if there were no
Jon Loeligerb7ced082006-10-10 17:03:43 -0500812 * errors
813 */
814static int tsec_send(struct eth_device *dev, volatile void *packet, int length)
wdenk9c53f402003-10-15 23:53:47 +0000815{
816 int i;
817 int result = 0;
wdenka445ddf2004-06-09 00:34:46 +0000818 struct tsec_private *priv = (struct tsec_private *)dev->priv;
819 volatile tsec_t *regs = priv->regs;
wdenk9c53f402003-10-15 23:53:47 +0000820
821 /* Find an empty buffer descriptor */
Jon Loeligerb7ced082006-10-10 17:03:43 -0500822 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
wdenk9c53f402003-10-15 23:53:47 +0000823 if (i >= TOUT_LOOP) {
Jon Loeligerb7ced082006-10-10 17:03:43 -0500824 debug("%s: tsec: tx buffers full\n", dev->name);
wdenk9c53f402003-10-15 23:53:47 +0000825 return result;
826 }
827 }
828
Jon Loeligerb7ced082006-10-10 17:03:43 -0500829 rtx.txbd[txIdx].bufPtr = (uint) packet;
wdenk9c53f402003-10-15 23:53:47 +0000830 rtx.txbd[txIdx].length = length;
Jon Loeligerb7ced082006-10-10 17:03:43 -0500831 rtx.txbd[txIdx].status |=
832 (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
wdenk9c53f402003-10-15 23:53:47 +0000833
834 /* Tell the DMA to go */
835 regs->tstat = TSTAT_CLEAR_THALT;
836
837 /* Wait for buffer to be transmitted */
Jon Loeligerb7ced082006-10-10 17:03:43 -0500838 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
wdenk9c53f402003-10-15 23:53:47 +0000839 if (i >= TOUT_LOOP) {
Jon Loeligerb7ced082006-10-10 17:03:43 -0500840 debug("%s: tsec: tx error\n", dev->name);
wdenk9c53f402003-10-15 23:53:47 +0000841 return result;
842 }
843 }
844
845 txIdx = (txIdx + 1) % TX_BUF_CNT;
846 result = rtx.txbd[txIdx].status & TXBD_STATS;
847
848 return result;
849}
850
Jon Loeligerb7ced082006-10-10 17:03:43 -0500851static int tsec_recv(struct eth_device *dev)
wdenk9c53f402003-10-15 23:53:47 +0000852{
853 int length;
wdenka445ddf2004-06-09 00:34:46 +0000854 struct tsec_private *priv = (struct tsec_private *)dev->priv;
855 volatile tsec_t *regs = priv->regs;
wdenk9c53f402003-10-15 23:53:47 +0000856
Jon Loeligerb7ced082006-10-10 17:03:43 -0500857 while (!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
wdenk9c53f402003-10-15 23:53:47 +0000858
859 length = rtx.rxbd[rxIdx].length;
860
861 /* Send the packet up if there were no errors */
862 if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
863 NetReceive(NetRxPackets[rxIdx], length - 4);
wdenka445ddf2004-06-09 00:34:46 +0000864 } else {
865 printf("Got error %x\n",
Jon Loeligerb7ced082006-10-10 17:03:43 -0500866 (rtx.rxbd[rxIdx].status & RXBD_STATS));
wdenk9c53f402003-10-15 23:53:47 +0000867 }
868
869 rtx.rxbd[rxIdx].length = 0;
870
871 /* Set the wrap bit if this is the last element in the list */
Jon Loeligerb7ced082006-10-10 17:03:43 -0500872 rtx.rxbd[rxIdx].status =
873 RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
wdenk9c53f402003-10-15 23:53:47 +0000874
875 rxIdx = (rxIdx + 1) % PKTBUFSRX;
876 }
877
Jon Loeligerb7ced082006-10-10 17:03:43 -0500878 if (regs->ievent & IEVENT_BSY) {
wdenk9c53f402003-10-15 23:53:47 +0000879 regs->ievent = IEVENT_BSY;
880 regs->rstat = RSTAT_CLEAR_RHALT;
881 }
882
883 return -1;
884
885}
886
wdenka445ddf2004-06-09 00:34:46 +0000887/* Stop the interface */
Jon Loeligerb7ced082006-10-10 17:03:43 -0500888static void tsec_halt(struct eth_device *dev)
wdenk9c53f402003-10-15 23:53:47 +0000889{
wdenka445ddf2004-06-09 00:34:46 +0000890 struct tsec_private *priv = (struct tsec_private *)dev->priv;
891 volatile tsec_t *regs = priv->regs;
wdenk9c53f402003-10-15 23:53:47 +0000892
893 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
894 regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS);
895
Jon Loeligerb7ced082006-10-10 17:03:43 -0500896 while (!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC))) ;
wdenk9c53f402003-10-15 23:53:47 +0000897
898 regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
899
wdenka445ddf2004-06-09 00:34:46 +0000900 /* Shut down the PHY, as needed */
Ben Warrenf11eefb2006-10-26 14:38:25 -0400901 if(priv->phyinfo)
902 phy_run_commands(priv, priv->phyinfo->shutdown);
wdenka445ddf2004-06-09 00:34:46 +0000903}
904
Andy Flemingbee67002007-08-03 04:05:25 -0500905struct phy_info phy_info_M88E1149S = {
Wolfgang Denk15e87572007-08-06 01:01:49 +0200906 0x1410ca,
907 "Marvell 88E1149S",
908 4,
909 (struct phy_cmd[]){ /* config */
910 /* Reset and configure the PHY */
911 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
912 {0x1d, 0x1f, NULL},
913 {0x1e, 0x200c, NULL},
914 {0x1d, 0x5, NULL},
915 {0x1e, 0x0, NULL},
916 {0x1e, 0x100, NULL},
917 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
918 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
919 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
920 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
921 {miim_end,}
922 },
923 (struct phy_cmd[]){ /* startup */
924 /* Status is read once to clear old link state */
925 {MIIM_STATUS, miim_read, NULL},
926 /* Auto-negotiate */
927 {MIIM_STATUS, miim_read, &mii_parse_sr},
928 /* Read the status */
929 {MIIM_88E1011_PHY_STATUS, miim_read,
930 &mii_parse_88E1011_psr},
931 {miim_end,}
932 },
933 (struct phy_cmd[]){ /* shutdown */
934 {miim_end,}
935 },
Andy Flemingbee67002007-08-03 04:05:25 -0500936};
937
Paul Gortmaker2bd9f1b2007-01-16 11:38:14 -0500938/* The 5411 id is 0x206070, the 5421 is 0x2060e0 */
939struct phy_info phy_info_BCM5461S = {
940 0x02060c1, /* 5461 ID */
941 "Broadcom BCM5461S",
942 0, /* not clear to me what minor revisions we can shift away */
943 (struct phy_cmd[]) { /* config */
944 /* Reset and configure the PHY */
945 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
946 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
947 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
948 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
949 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
950 {miim_end,}
951 },
952 (struct phy_cmd[]) { /* startup */
953 /* Status is read once to clear old link state */
954 {MIIM_STATUS, miim_read, NULL},
955 /* Auto-negotiate */
956 {MIIM_STATUS, miim_read, &mii_parse_sr},
957 /* Read the status */
958 {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
959 {miim_end,}
960 },
961 (struct phy_cmd[]) { /* shutdown */
962 {miim_end,}
963 },
964};
965
Joe Hammaned7ad4e2007-04-30 16:47:28 -0500966struct phy_info phy_info_BCM5464S = {
967 0x02060b1, /* 5464 ID */
968 "Broadcom BCM5464S",
969 0, /* not clear to me what minor revisions we can shift away */
970 (struct phy_cmd[]) { /* config */
971 /* Reset and configure the PHY */
972 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
973 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
974 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
975 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
976 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
977 {miim_end,}
978 },
979 (struct phy_cmd[]) { /* startup */
980 /* Status is read once to clear old link state */
981 {MIIM_STATUS, miim_read, NULL},
982 /* Auto-negotiate */
983 {MIIM_STATUS, miim_read, &mii_parse_sr},
984 /* Read the status */
985 {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
986 {miim_end,}
987 },
988 (struct phy_cmd[]) { /* shutdown */
989 {miim_end,}
990 },
991};
992
wdenka445ddf2004-06-09 00:34:46 +0000993struct phy_info phy_info_M88E1011S = {
994 0x01410c6,
995 "Marvell 88E1011S",
996 4,
Jon Loeligerb7ced082006-10-10 17:03:43 -0500997 (struct phy_cmd[]){ /* config */
998 /* Reset and configure the PHY */
999 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1000 {0x1d, 0x1f, NULL},
1001 {0x1e, 0x200c, NULL},
1002 {0x1d, 0x5, NULL},
1003 {0x1e, 0x0, NULL},
1004 {0x1e, 0x100, NULL},
1005 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1006 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1007 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1008 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1009 {miim_end,}
1010 },
1011 (struct phy_cmd[]){ /* startup */
1012 /* Status is read once to clear old link state */
1013 {MIIM_STATUS, miim_read, NULL},
1014 /* Auto-negotiate */
1015 {MIIM_STATUS, miim_read, &mii_parse_sr},
1016 /* Read the status */
1017 {MIIM_88E1011_PHY_STATUS, miim_read,
1018 &mii_parse_88E1011_psr},
1019 {miim_end,}
1020 },
1021 (struct phy_cmd[]){ /* shutdown */
1022 {miim_end,}
1023 },
wdenka445ddf2004-06-09 00:34:46 +00001024};
1025
wdenkbfad55d2005-03-14 23:56:42 +00001026struct phy_info phy_info_M88E1111S = {
1027 0x01410cc,
1028 "Marvell 88E1111S",
1029 4,
Jon Loeligerb7ced082006-10-10 17:03:43 -05001030 (struct phy_cmd[]){ /* config */
1031 /* Reset and configure the PHY */
1032 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
Nick Spenceec9670b2006-09-07 07:39:46 -07001033 {0x14, 0x0cd2, NULL}, /* Delay RGMII TX and RX */
Jon Loeligerb7ced082006-10-10 17:03:43 -05001034 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1035 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1036 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1037 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1038 {miim_end,}
1039 },
1040 (struct phy_cmd[]){ /* startup */
1041 /* Status is read once to clear old link state */
1042 {MIIM_STATUS, miim_read, NULL},
1043 /* Auto-negotiate */
1044 {MIIM_STATUS, miim_read, &mii_parse_sr},
1045 /* Read the status */
1046 {MIIM_88E1011_PHY_STATUS, miim_read,
1047 &mii_parse_88E1011_psr},
1048 {miim_end,}
1049 },
1050 (struct phy_cmd[]){ /* shutdown */
1051 {miim_end,}
1052 },
wdenkbfad55d2005-03-14 23:56:42 +00001053};
1054
Andy Fleming239e75f2006-09-13 10:34:18 -05001055static unsigned int m88e1145_setmode(uint mii_reg, struct tsec_private *priv)
1056{
Andy Fleming239e75f2006-09-13 10:34:18 -05001057 uint mii_data = read_phy_reg(priv, mii_reg);
1058
Andy Fleming239e75f2006-09-13 10:34:18 -05001059 /* Setting MIIM_88E1145_PHY_EXT_CR */
1060 if (priv->flags & TSEC_REDUCED)
1061 return mii_data |
Jon Loeligerb7ced082006-10-10 17:03:43 -05001062 MIIM_M88E1145_RGMII_RX_DELAY | MIIM_M88E1145_RGMII_TX_DELAY;
Andy Fleming239e75f2006-09-13 10:34:18 -05001063 else
1064 return mii_data;
1065}
1066
1067static struct phy_info phy_info_M88E1145 = {
1068 0x01410cd,
1069 "Marvell 88E1145",
1070 4,
Jon Loeligerb7ced082006-10-10 17:03:43 -05001071 (struct phy_cmd[]){ /* config */
Andy Fleming180d03a2007-05-08 17:23:02 -05001072 /* Reset the PHY */
1073 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1074
Jon Loeligerb7ced082006-10-10 17:03:43 -05001075 /* Errata E0, E1 */
1076 {29, 0x001b, NULL},
1077 {30, 0x418f, NULL},
1078 {29, 0x0016, NULL},
1079 {30, 0xa2da, NULL},
Andy Fleming239e75f2006-09-13 10:34:18 -05001080
Andy Fleming180d03a2007-05-08 17:23:02 -05001081 /* Configure the PHY */
Jon Loeligerb7ced082006-10-10 17:03:43 -05001082 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1083 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1084 {MIIM_88E1011_PHY_SCR, MIIM_88E1011_PHY_MDI_X_AUTO,
1085 NULL},
1086 {MIIM_88E1145_PHY_EXT_CR, 0, &m88e1145_setmode},
1087 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1088 {MIIM_CONTROL, MIIM_CONTROL_INIT, NULL},
1089 {miim_end,}
1090 },
1091 (struct phy_cmd[]){ /* startup */
1092 /* Status is read once to clear old link state */
1093 {MIIM_STATUS, miim_read, NULL},
1094 /* Auto-negotiate */
1095 {MIIM_STATUS, miim_read, &mii_parse_sr},
1096 {MIIM_88E1111_PHY_LED_CONTROL,
1097 MIIM_88E1111_PHY_LED_DIRECT, NULL},
1098 /* Read the Status */
1099 {MIIM_88E1011_PHY_STATUS, miim_read,
1100 &mii_parse_88E1011_psr},
1101 {miim_end,}
1102 },
1103 (struct phy_cmd[]){ /* shutdown */
1104 {miim_end,}
1105 },
Andy Fleming239e75f2006-09-13 10:34:18 -05001106};
1107
wdenka445ddf2004-06-09 00:34:46 +00001108struct phy_info phy_info_cis8204 = {
1109 0x3f11,
1110 "Cicada Cis8204",
1111 6,
Jon Loeligerb7ced082006-10-10 17:03:43 -05001112 (struct phy_cmd[]){ /* config */
1113 /* Override PHY config settings */
1114 {MIIM_CIS8201_AUX_CONSTAT,
1115 MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
1116 /* Configure some basic stuff */
1117 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1118 {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT,
1119 &mii_cis8204_fixled},
1120 {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT,
1121 &mii_cis8204_setmode},
1122 {miim_end,}
1123 },
1124 (struct phy_cmd[]){ /* startup */
1125 /* Read the Status (2x to make sure link is right) */
1126 {MIIM_STATUS, miim_read, NULL},
1127 /* Auto-negotiate */
1128 {MIIM_STATUS, miim_read, &mii_parse_sr},
1129 /* Read the status */
1130 {MIIM_CIS8201_AUX_CONSTAT, miim_read,
1131 &mii_parse_cis8201},
1132 {miim_end,}
1133 },
1134 (struct phy_cmd[]){ /* shutdown */
1135 {miim_end,}
1136 },
wdenka445ddf2004-06-09 00:34:46 +00001137};
1138
1139/* Cicada 8201 */
1140struct phy_info phy_info_cis8201 = {
1141 0xfc41,
1142 "CIS8201",
1143 4,
Jon Loeligerb7ced082006-10-10 17:03:43 -05001144 (struct phy_cmd[]){ /* config */
1145 /* Override PHY config settings */
1146 {MIIM_CIS8201_AUX_CONSTAT,
1147 MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
1148 /* Set up the interface mode */
1149 {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT,
1150 NULL},
1151 /* Configure some basic stuff */
1152 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1153 {miim_end,}
1154 },
1155 (struct phy_cmd[]){ /* startup */
1156 /* Read the Status (2x to make sure link is right) */
1157 {MIIM_STATUS, miim_read, NULL},
1158 /* Auto-negotiate */
1159 {MIIM_STATUS, miim_read, &mii_parse_sr},
1160 /* Read the status */
1161 {MIIM_CIS8201_AUX_CONSTAT, miim_read,
1162 &mii_parse_cis8201},
1163 {miim_end,}
1164 },
1165 (struct phy_cmd[]){ /* shutdown */
1166 {miim_end,}
1167 },
wdenka445ddf2004-06-09 00:34:46 +00001168};
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001169struct phy_info phy_info_VSC8244 = {
Jon Loeligerb7ced082006-10-10 17:03:43 -05001170 0x3f1b,
1171 "Vitesse VSC8244",
1172 6,
1173 (struct phy_cmd[]){ /* config */
1174 /* Override PHY config settings */
1175 /* Configure some basic stuff */
1176 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1177 {miim_end,}
1178 },
1179 (struct phy_cmd[]){ /* startup */
1180 /* Read the Status (2x to make sure link is right) */
1181 {MIIM_STATUS, miim_read, NULL},
1182 /* Auto-negotiate */
1183 {MIIM_STATUS, miim_read, &mii_parse_sr},
1184 /* Read the status */
1185 {MIIM_VSC8244_AUX_CONSTAT, miim_read,
1186 &mii_parse_vsc8244},
1187 {miim_end,}
1188 },
1189 (struct phy_cmd[]){ /* shutdown */
1190 {miim_end,}
1191 },
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001192};
wdenka445ddf2004-06-09 00:34:46 +00001193
wdenka445ddf2004-06-09 00:34:46 +00001194struct phy_info phy_info_dm9161 = {
1195 0x0181b88,
1196 "Davicom DM9161E",
1197 4,
Jon Loeligerb7ced082006-10-10 17:03:43 -05001198 (struct phy_cmd[]){ /* config */
1199 {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
1200 /* Do not bypass the scrambler/descrambler */
1201 {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
1202 /* Clear 10BTCSR to default */
1203 {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT,
1204 NULL},
1205 /* Configure some basic stuff */
1206 {MIIM_CONTROL, MIIM_CR_INIT, NULL},
1207 /* Restart Auto Negotiation */
1208 {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
1209 {miim_end,}
1210 },
1211 (struct phy_cmd[]){ /* startup */
1212 /* Status is read once to clear old link state */
1213 {MIIM_STATUS, miim_read, NULL},
1214 /* Auto-negotiate */
1215 {MIIM_STATUS, miim_read, &mii_parse_sr},
1216 /* Read the status */
1217 {MIIM_DM9161_SCSR, miim_read,
1218 &mii_parse_dm9161_scsr},
1219 {miim_end,}
1220 },
1221 (struct phy_cmd[]){ /* shutdown */
1222 {miim_end,}
1223 },
wdenka445ddf2004-06-09 00:34:46 +00001224};
David Updegraff0451b012007-04-20 14:34:48 -05001225/* a generic flavor. */
1226struct phy_info phy_info_generic = {
1227 0,
1228 "Unknown/Generic PHY",
1229 32,
1230 (struct phy_cmd[]) { /* config */
1231 {PHY_BMCR, PHY_BMCR_RESET, NULL},
1232 {PHY_BMCR, PHY_BMCR_AUTON|PHY_BMCR_RST_NEG, NULL},
1233 {miim_end,}
1234 },
1235 (struct phy_cmd[]) { /* startup */
1236 {PHY_BMSR, miim_read, NULL},
1237 {PHY_BMSR, miim_read, &mii_parse_sr},
1238 {PHY_BMSR, miim_read, &mii_parse_link},
1239 {miim_end,}
1240 },
1241 (struct phy_cmd[]) { /* shutdown */
1242 {miim_end,}
1243 }
1244};
1245
wdenka445ddf2004-06-09 00:34:46 +00001246
wdenkf41ff3b2005-04-04 23:43:44 +00001247uint mii_parse_lxt971_sr2(uint mii_reg, struct tsec_private *priv)
1248{
wdenke085e5b2005-04-05 23:32:21 +00001249 unsigned int speed;
1250 if (priv->link) {
1251 speed = mii_reg & MIIM_LXT971_SR2_SPEED_MASK;
wdenkf41ff3b2005-04-04 23:43:44 +00001252
wdenke085e5b2005-04-05 23:32:21 +00001253 switch (speed) {
1254 case MIIM_LXT971_SR2_10HDX:
1255 priv->speed = 10;
1256 priv->duplexity = 0;
1257 break;
1258 case MIIM_LXT971_SR2_10FDX:
1259 priv->speed = 10;
1260 priv->duplexity = 1;
1261 break;
1262 case MIIM_LXT971_SR2_100HDX:
1263 priv->speed = 100;
1264 priv->duplexity = 0;
1265 default:
1266 priv->speed = 100;
1267 priv->duplexity = 1;
1268 break;
1269 }
1270 } else {
1271 priv->speed = 0;
1272 priv->duplexity = 0;
1273 }
wdenkf41ff3b2005-04-04 23:43:44 +00001274
wdenke085e5b2005-04-05 23:32:21 +00001275 return 0;
wdenkf41ff3b2005-04-04 23:43:44 +00001276}
1277
wdenkbfad55d2005-03-14 23:56:42 +00001278static struct phy_info phy_info_lxt971 = {
1279 0x0001378e,
1280 "LXT971",
1281 4,
Jon Loeligerb7ced082006-10-10 17:03:43 -05001282 (struct phy_cmd[]){ /* config */
1283 {MIIM_CR, MIIM_CR_INIT, mii_cr_init}, /* autonegotiate */
1284 {miim_end,}
1285 },
1286 (struct phy_cmd[]){ /* startup - enable interrupts */
1287 /* { 0x12, 0x00f2, NULL }, */
1288 {MIIM_STATUS, miim_read, NULL},
1289 {MIIM_STATUS, miim_read, &mii_parse_sr},
1290 {MIIM_LXT971_SR2, miim_read, &mii_parse_lxt971_sr2},
1291 {miim_end,}
1292 },
1293 (struct phy_cmd[]){ /* shutdown - disable interrupts */
1294 {miim_end,}
1295 },
wdenkbfad55d2005-03-14 23:56:42 +00001296};
1297
Wolfgang Denkf0c4e462006-03-12 22:50:55 +01001298/* Parse the DP83865's link and auto-neg status register for speed and duplex
Jon Loeligerb7ced082006-10-10 17:03:43 -05001299 * information
1300 */
Wolfgang Denkf0c4e462006-03-12 22:50:55 +01001301uint mii_parse_dp83865_lanr(uint mii_reg, struct tsec_private *priv)
1302{
1303 switch (mii_reg & MIIM_DP83865_SPD_MASK) {
1304
1305 case MIIM_DP83865_SPD_1000:
1306 priv->speed = 1000;
1307 break;
1308
1309 case MIIM_DP83865_SPD_100:
1310 priv->speed = 100;
1311 break;
1312
1313 default:
1314 priv->speed = 10;
1315 break;
1316
1317 }
1318
1319 if (mii_reg & MIIM_DP83865_DPX_FULL)
1320 priv->duplexity = 1;
1321 else
1322 priv->duplexity = 0;
1323
1324 return 0;
1325}
1326
1327struct phy_info phy_info_dp83865 = {
1328 0x20005c7,
1329 "NatSemi DP83865",
1330 4,
Jon Loeligerb7ced082006-10-10 17:03:43 -05001331 (struct phy_cmd[]){ /* config */
1332 {MIIM_CONTROL, MIIM_DP83865_CR_INIT, NULL},
1333 {miim_end,}
1334 },
1335 (struct phy_cmd[]){ /* startup */
1336 /* Status is read once to clear old link state */
1337 {MIIM_STATUS, miim_read, NULL},
1338 /* Auto-negotiate */
1339 {MIIM_STATUS, miim_read, &mii_parse_sr},
1340 /* Read the link and auto-neg status */
1341 {MIIM_DP83865_LANR, miim_read,
1342 &mii_parse_dp83865_lanr},
1343 {miim_end,}
1344 },
1345 (struct phy_cmd[]){ /* shutdown */
1346 {miim_end,}
1347 },
Wolfgang Denkf0c4e462006-03-12 22:50:55 +01001348};
1349
wdenka445ddf2004-06-09 00:34:46 +00001350struct phy_info *phy_info[] = {
wdenka445ddf2004-06-09 00:34:46 +00001351 &phy_info_cis8204,
Timur Tabi054838e2006-10-31 18:44:42 -06001352 &phy_info_cis8201,
Paul Gortmaker2bd9f1b2007-01-16 11:38:14 -05001353 &phy_info_BCM5461S,
Joe Hammaned7ad4e2007-04-30 16:47:28 -05001354 &phy_info_BCM5464S,
wdenka445ddf2004-06-09 00:34:46 +00001355 &phy_info_M88E1011S,
wdenkbfad55d2005-03-14 23:56:42 +00001356 &phy_info_M88E1111S,
Andy Fleming239e75f2006-09-13 10:34:18 -05001357 &phy_info_M88E1145,
Wolfgang Denk15e87572007-08-06 01:01:49 +02001358 &phy_info_M88E1149S,
wdenka445ddf2004-06-09 00:34:46 +00001359 &phy_info_dm9161,
wdenkbfad55d2005-03-14 23:56:42 +00001360 &phy_info_lxt971,
Jon Loeliger5c8aa972006-04-26 17:58:56 -05001361 &phy_info_VSC8244,
Wolfgang Denkf0c4e462006-03-12 22:50:55 +01001362 &phy_info_dp83865,
David Updegraff0451b012007-04-20 14:34:48 -05001363 &phy_info_generic,
wdenka445ddf2004-06-09 00:34:46 +00001364 NULL
1365};
1366
wdenka445ddf2004-06-09 00:34:46 +00001367/* Grab the identifier of the device's PHY, and search through
wdenkbfad55d2005-03-14 23:56:42 +00001368 * all of the known PHYs to see if one matches. If so, return
Jon Loeligerb7ced082006-10-10 17:03:43 -05001369 * it, if not, return NULL
1370 */
1371struct phy_info *get_phy_info(struct eth_device *dev)
wdenka445ddf2004-06-09 00:34:46 +00001372{
1373 struct tsec_private *priv = (struct tsec_private *)dev->priv;
1374 uint phy_reg, phy_ID;
1375 int i;
1376 struct phy_info *theInfo = NULL;
1377
1378 /* Grab the bits from PHYIR1, and put them in the upper half */
1379 phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
1380 phy_ID = (phy_reg & 0xffff) << 16;
1381
1382 /* Grab the bits from PHYIR2, and put them in the lower half */
1383 phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
1384 phy_ID |= (phy_reg & 0xffff);
1385
1386 /* loop through all the known PHY types, and find one that */
1387 /* matches the ID we read from the PHY. */
Jon Loeligerb7ced082006-10-10 17:03:43 -05001388 for (i = 0; phy_info[i]; i++) {
Andy Flemingb2d14f42007-05-09 00:54:20 -05001389 if (phy_info[i]->id == (phy_ID >> phy_info[i]->shift)) {
wdenka445ddf2004-06-09 00:34:46 +00001390 theInfo = phy_info[i];
Andy Flemingb2d14f42007-05-09 00:54:20 -05001391 break;
1392 }
wdenka445ddf2004-06-09 00:34:46 +00001393 }
1394
Jon Loeligerb7ced082006-10-10 17:03:43 -05001395 if (theInfo == NULL) {
wdenka445ddf2004-06-09 00:34:46 +00001396 printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
1397 return NULL;
1398 } else {
Stefan Roesec0dc34f2005-09-21 18:20:22 +02001399 debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID);
wdenka445ddf2004-06-09 00:34:46 +00001400 }
1401
1402 return theInfo;
1403}
1404
wdenka445ddf2004-06-09 00:34:46 +00001405/* Execute the given series of commands on the given device's
Jon Loeligerb7ced082006-10-10 17:03:43 -05001406 * PHY, running functions as necessary
1407 */
wdenka445ddf2004-06-09 00:34:46 +00001408void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
1409{
1410 int i;
1411 uint result;
1412 volatile tsec_t *phyregs = priv->phyregs;
1413
1414 phyregs->miimcfg = MIIMCFG_RESET;
1415
1416 phyregs->miimcfg = MIIMCFG_INIT_VALUE;
1417
Jon Loeligerb7ced082006-10-10 17:03:43 -05001418 while (phyregs->miimind & MIIMIND_BUSY) ;
wdenka445ddf2004-06-09 00:34:46 +00001419
Jon Loeligerb7ced082006-10-10 17:03:43 -05001420 for (i = 0; cmd->mii_reg != miim_end; i++) {
1421 if (cmd->mii_data == miim_read) {
wdenka445ddf2004-06-09 00:34:46 +00001422 result = read_phy_reg(priv, cmd->mii_reg);
1423
Jon Loeligerb7ced082006-10-10 17:03:43 -05001424 if (cmd->funct != NULL)
1425 (*(cmd->funct)) (result, priv);
wdenka445ddf2004-06-09 00:34:46 +00001426
1427 } else {
Jon Loeligerb7ced082006-10-10 17:03:43 -05001428 if (cmd->funct != NULL)
1429 result = (*(cmd->funct)) (cmd->mii_reg, priv);
wdenka445ddf2004-06-09 00:34:46 +00001430 else
1431 result = cmd->mii_data;
1432
1433 write_phy_reg(priv, cmd->mii_reg, result);
1434
1435 }
1436 cmd++;
1437 }
1438}
1439
wdenka445ddf2004-06-09 00:34:46 +00001440/* Relocate the function pointers in the phy cmd lists */
1441static void relocate_cmds(void)
1442{
1443 struct phy_cmd **cmdlistptr;
1444 struct phy_cmd *cmd;
Jon Loeligerb7ced082006-10-10 17:03:43 -05001445 int i, j, k;
wdenka445ddf2004-06-09 00:34:46 +00001446
Jon Loeligerb7ced082006-10-10 17:03:43 -05001447 for (i = 0; phy_info[i]; i++) {
wdenka445ddf2004-06-09 00:34:46 +00001448 /* First thing's first: relocate the pointers to the
1449 * PHY command structures (the structs were done) */
Jon Loeligerb7ced082006-10-10 17:03:43 -05001450 phy_info[i] = (struct phy_info *)((uint) phy_info[i]
1451 + gd->reloc_off);
wdenka445ddf2004-06-09 00:34:46 +00001452 phy_info[i]->name += gd->reloc_off;
1453 phy_info[i]->config =
Jon Loeligerb7ced082006-10-10 17:03:43 -05001454 (struct phy_cmd *)((uint) phy_info[i]->config
1455 + gd->reloc_off);
wdenka445ddf2004-06-09 00:34:46 +00001456 phy_info[i]->startup =
Jon Loeligerb7ced082006-10-10 17:03:43 -05001457 (struct phy_cmd *)((uint) phy_info[i]->startup
1458 + gd->reloc_off);
wdenka445ddf2004-06-09 00:34:46 +00001459 phy_info[i]->shutdown =
Jon Loeligerb7ced082006-10-10 17:03:43 -05001460 (struct phy_cmd *)((uint) phy_info[i]->shutdown
1461 + gd->reloc_off);
wdenka445ddf2004-06-09 00:34:46 +00001462
1463 cmdlistptr = &phy_info[i]->config;
Jon Loeligerb7ced082006-10-10 17:03:43 -05001464 j = 0;
1465 for (; cmdlistptr <= &phy_info[i]->shutdown; cmdlistptr++) {
1466 k = 0;
1467 for (cmd = *cmdlistptr;
1468 cmd->mii_reg != miim_end;
1469 cmd++) {
wdenka445ddf2004-06-09 00:34:46 +00001470 /* Only relocate non-NULL pointers */
Jon Loeligerb7ced082006-10-10 17:03:43 -05001471 if (cmd->funct)
wdenka445ddf2004-06-09 00:34:46 +00001472 cmd->funct += gd->reloc_off;
1473
1474 k++;
1475 }
1476 j++;
1477 }
1478 }
1479
1480 relocated = 1;
wdenk78924a72004-04-18 21:45:42 +00001481}
1482
Jon Loeliger82ecaad2007-07-09 17:39:42 -05001483#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
Marian Balakowiczaab8c492005-10-28 22:30:33 +02001484 && !defined(BITBANGMII)
wdenka445ddf2004-06-09 00:34:46 +00001485
Jon Loeligerb7ced082006-10-10 17:03:43 -05001486struct tsec_private *get_priv_for_phy(unsigned char phyaddr)
wdenka445ddf2004-06-09 00:34:46 +00001487{
1488 int i;
1489
Jon Loeligerb7ced082006-10-10 17:03:43 -05001490 for (i = 0; i < MAXCONTROLLERS; i++) {
1491 if (privlist[i]->phyaddr == phyaddr)
wdenka445ddf2004-06-09 00:34:46 +00001492 return privlist[i];
1493 }
1494
1495 return NULL;
1496}
1497
wdenk78924a72004-04-18 21:45:42 +00001498/*
1499 * Read a MII PHY register.
1500 *
1501 * Returns:
wdenka445ddf2004-06-09 00:34:46 +00001502 * 0 on success
wdenk78924a72004-04-18 21:45:42 +00001503 */
Marian Balakowiczaab8c492005-10-28 22:30:33 +02001504static int tsec_miiphy_read(char *devname, unsigned char addr,
Jon Loeligerb7ced082006-10-10 17:03:43 -05001505 unsigned char reg, unsigned short *value)
wdenk78924a72004-04-18 21:45:42 +00001506{
wdenka445ddf2004-06-09 00:34:46 +00001507 unsigned short ret;
1508 struct tsec_private *priv = get_priv_for_phy(addr);
wdenk78924a72004-04-18 21:45:42 +00001509
Jon Loeligerb7ced082006-10-10 17:03:43 -05001510 if (NULL == priv) {
wdenka445ddf2004-06-09 00:34:46 +00001511 printf("Can't read PHY at address %d\n", addr);
1512 return -1;
1513 }
1514
1515 ret = (unsigned short)read_phy_reg(priv, reg);
1516 *value = ret;
wdenk78924a72004-04-18 21:45:42 +00001517
1518 return 0;
1519}
1520
1521/*
1522 * Write a MII PHY register.
1523 *
1524 * Returns:
wdenka445ddf2004-06-09 00:34:46 +00001525 * 0 on success
wdenk78924a72004-04-18 21:45:42 +00001526 */
Marian Balakowiczaab8c492005-10-28 22:30:33 +02001527static int tsec_miiphy_write(char *devname, unsigned char addr,
Jon Loeligerb7ced082006-10-10 17:03:43 -05001528 unsigned char reg, unsigned short value)
wdenk78924a72004-04-18 21:45:42 +00001529{
wdenka445ddf2004-06-09 00:34:46 +00001530 struct tsec_private *priv = get_priv_for_phy(addr);
1531
Jon Loeligerb7ced082006-10-10 17:03:43 -05001532 if (NULL == priv) {
wdenka445ddf2004-06-09 00:34:46 +00001533 printf("Can't write PHY at address %d\n", addr);
1534 return -1;
1535 }
wdenk78924a72004-04-18 21:45:42 +00001536
wdenka445ddf2004-06-09 00:34:46 +00001537 write_phy_reg(priv, reg, value);
wdenk78924a72004-04-18 21:45:42 +00001538
1539 return 0;
wdenk9c53f402003-10-15 23:53:47 +00001540}
wdenka445ddf2004-06-09 00:34:46 +00001541
Jon Loeliger82ecaad2007-07-09 17:39:42 -05001542#endif
wdenka445ddf2004-06-09 00:34:46 +00001543
David Updegraff7280da72007-06-11 10:41:07 -05001544#ifdef CONFIG_MCAST_TFTP
1545
1546/* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
1547
1548/* Set the appropriate hash bit for the given addr */
1549
1550/* The algorithm works like so:
1551 * 1) Take the Destination Address (ie the multicast address), and
1552 * do a CRC on it (little endian), and reverse the bits of the
1553 * result.
1554 * 2) Use the 8 most significant bits as a hash into a 256-entry
1555 * table. The table is controlled through 8 32-bit registers:
1556 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1557 * gaddr7. This means that the 3 most significant bits in the
1558 * hash index which gaddr register to use, and the 5 other bits
1559 * indicate which bit (assuming an IBM numbering scheme, which
1560 * for PowerPC (tm) is usually the case) in the tregister holds
1561 * the entry. */
1562static int
1563tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set)
1564{
1565 struct tsec_private *priv = privlist[1];
1566 volatile tsec_t *regs = priv->regs;
1567 volatile u32 *reg_array, value;
1568 u8 result, whichbit, whichreg;
1569
1570 result = (u8)((ether_crc(MAC_ADDR_LEN,mcast_mac) >> 24) & 0xff);
1571 whichbit = result & 0x1f; /* the 5 LSB = which bit to set */
1572 whichreg = result >> 5; /* the 3 MSB = which reg to set it in */
1573 value = (1 << (31-whichbit));
1574
1575 reg_array = &(regs->hash.gaddr0);
1576
1577 if (set) {
1578 reg_array[whichreg] |= value;
1579 } else {
1580 reg_array[whichreg] &= ~value;
1581 }
1582 return 0;
1583}
1584#endif /* Multicast TFTP ? */
1585
wdenk9c53f402003-10-15 23:53:47 +00001586#endif /* CONFIG_TSEC_ENET */