blob: 397ae7162682f36633b1b8d9d448335d94d30067 [file] [log] [blame]
wdenk42d1f032003-10-15 23:53:47 +00001/*
wdenk97d80fc2004-06-09 00:34:46 +00002 * Freescale Three Speed Ethernet Controller driver
wdenk42d1f032003-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 Fleming81f481c2007-04-23 02:24:28 -05008 * Copyright 2004, 2007 Freescale Semiconductor, Inc.
wdenk42d1f032003-10-15 23:53:47 +00009 * (C) Copyright 2003, Motorola, Inc.
wdenk42d1f032003-10-15 23:53:47 +000010 * author Andy Fleming
11 *
12 */
13
14#include <config.h>
wdenk42d1f032003-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 Balakowicz63ff0042005-10-28 22:30:33 +020022#include "miiphy.h"
wdenk42d1f032003-10-15 23:53:47 +000023
Wolfgang Denkd87080b2006-03-31 18:32:53 +020024DECLARE_GLOBAL_DATA_PTR;
25
Marian Balakowicz63ff0042005-10-28 22:30:33 +020026#define TX_BUF_CNT 2
wdenk42d1f032003-10-15 23:53:47 +000027
Jon Loeliger89875e92006-10-10 17:03:43 -050028static uint rxIdx; /* index of the current RX buffer */
29static uint txIdx; /* index of the current TX buffer */
wdenk42d1f032003-10-15 23:53:47 +000030
31typedef volatile struct rtxbd {
32 txbd8_t txbd[TX_BUF_CNT];
33 rxbd8_t rxbd[PKTBUFSRX];
Jon Loeliger89875e92006-10-10 17:03:43 -050034} RTXBD;
wdenk42d1f032003-10-15 23:53:47 +000035
wdenk97d80fc2004-06-09 00:34:46 +000036struct tsec_info_struct {
37 unsigned int phyaddr;
Jon Loeligerd9b94f22005-07-25 14:05:07 -050038 u32 flags;
wdenk97d80fc2004-06-09 00:34:46 +000039 unsigned int phyregidx;
40};
41
wdenk97d80fc2004-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 Fleming09f3e092006-09-13 10:34:18 -050044 * device. The information needed is:
wdenk97d80fc2004-06-09 00:34:46 +000045 * phyaddr - The address of the PHY which is attached to
wdenk9d46ea42005-03-14 23:56:42 +000046 * the given device.
wdenk97d80fc2004-06-09 00:34:46 +000047 *
Jon Loeligerd9b94f22005-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.
wdenk97d80fc2004-06-09 00:34:46 +000051 *
52 * phyregidx - This variable specifies which ethernet device
wdenk9d46ea42005-03-14 23:56:42 +000053 * controls the MII Management registers which are connected
Andy Fleming09f3e092006-09-13 10:34:18 -050054 * to the PHY. For now, only TSEC1 (index 0) has
wdenk9d46ea42005-03-14 23:56:42 +000055 * access to the PHYs, so all of the entries have "0".
wdenk97d80fc2004-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 Fleming09f3e092006-09-13 10:34:18 -050060 * TSECn_PHY_ADDR
61 * TSECn_PHYIDX
wdenk97d80fc2004-06-09 00:34:46 +000062 *
Andy Fleming09f3e092006-09-13 10:34:18 -050063 * for n = 1,2,3, etc. And for FEC:
wdenk97d80fc2004-06-09 00:34:46 +000064 * FEC_PHY_ADDR
65 * FEC_PHYIDX
66 */
67static struct tsec_info_struct tsec_info[] = {
Andy Fleming3a790132007-08-15 20:03:25 -050068#ifdef CONFIG_TSEC1
69 {TSEC1_PHY_ADDR, TSEC1_FLAGS, TSEC1_PHYIDX},
Zach Sadeckied810642007-07-31 12:27:25 -050070#else
Jon Loeliger89875e92006-10-10 17:03:43 -050071 {0, 0, 0},
wdenk97d80fc2004-06-09 00:34:46 +000072#endif
Andy Fleming3a790132007-08-15 20:03:25 -050073#ifdef CONFIG_TSEC2
74 {TSEC2_PHY_ADDR, TSEC2_FLAGS, TSEC2_PHYIDX},
Zach Sadeckied810642007-07-31 12:27:25 -050075#else
Jon Loeliger89875e92006-10-10 17:03:43 -050076 {0, 0, 0},
wdenk97d80fc2004-06-09 00:34:46 +000077#endif
78#ifdef CONFIG_MPC85XX_FEC
Andy Fleming3a790132007-08-15 20:03:25 -050079 {FEC_PHY_ADDR, FEC_FLAGS, FEC_PHYIDX},
wdenk9d46ea42005-03-14 23:56:42 +000080#else
Andy Fleming3a790132007-08-15 20:03:25 -050081#ifdef CONFIG_TSEC3
82 {TSEC3_PHY_ADDR, TSEC3_FLAGS, TSEC3_PHYIDX},
Jon Loeligerdebb7352006-04-26 17:58:56 -050083#else
Jon Loeliger89875e92006-10-10 17:03:43 -050084 {0, 0, 0},
Jon Loeligerdebb7352006-04-26 17:58:56 -050085#endif
Andy Fleming3a790132007-08-15 20:03:25 -050086#ifdef CONFIG_TSEC4
87 {TSEC4_PHY_ADDR, TSEC4_FLAGS, TSEC4_PHYIDX},
Jon Loeligerdebb7352006-04-26 17:58:56 -050088#else
Jon Loeliger89875e92006-10-10 17:03:43 -050089 {0, 0, 0},
Andy Fleming3a790132007-08-15 20:03:25 -050090#endif /* CONFIG_TSEC4 */
91#endif /* CONFIG_MPC85XX_FEC */
wdenk97d80fc2004-06-09 00:34:46 +000092};
93
Jon Loeligerd9b94f22005-07-25 14:05:07 -050094#define MAXCONTROLLERS (4)
wdenk97d80fc2004-06-09 00:34:46 +000095
96static int relocated = 0;
97
98static struct tsec_private *privlist[MAXCONTROLLERS];
99
wdenk42d1f032003-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 Loeliger89875e92006-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);
wdenk97d80fc2004-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 Loeliger89875e92006-10-10 17:03:43 -0500116struct phy_info *get_phy_info(struct eth_device *dev);
wdenk97d80fc2004-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);
Wolfgang Denk409ecdc2007-11-18 16:36:27 +0100120#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
121 && !defined(BITBANGMII)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200122static int tsec_miiphy_write(char *devname, unsigned char addr,
Jon Loeliger89875e92006-10-10 17:03:43 -0500123 unsigned char reg, unsigned short value);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200124static int tsec_miiphy_read(char *devname, unsigned char addr,
Jon Loeliger89875e92006-10-10 17:03:43 -0500125 unsigned char reg, unsigned short *value);
Wolfgang Denk409ecdc2007-11-18 16:36:27 +0100126#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500127#ifdef CONFIG_MCAST_TFTP
128static int tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set);
129#endif
wdenk7abf0c52004-04-18 21:45:42 +0000130
wdenk97d80fc2004-06-09 00:34:46 +0000131/* Initialize device structure. Returns success if PHY
132 * initialization succeeded (i.e. if it recognizes the PHY)
133 */
Jon Loeliger89875e92006-10-10 17:03:43 -0500134int tsec_initialize(bd_t * bis, int index, char *devname)
wdenk42d1f032003-10-15 23:53:47 +0000135{
Jon Loeliger89875e92006-10-10 17:03:43 -0500136 struct eth_device *dev;
wdenk42d1f032003-10-15 23:53:47 +0000137 int i;
wdenk97d80fc2004-06-09 00:34:46 +0000138 struct tsec_private *priv;
wdenk42d1f032003-10-15 23:53:47 +0000139
Jon Loeliger89875e92006-10-10 17:03:43 -0500140 dev = (struct eth_device *)malloc(sizeof *dev);
wdenk42d1f032003-10-15 23:53:47 +0000141
Jon Loeliger89875e92006-10-10 17:03:43 -0500142 if (NULL == dev)
wdenk42d1f032003-10-15 23:53:47 +0000143 return 0;
144
145 memset(dev, 0, sizeof *dev);
146
Jon Loeliger89875e92006-10-10 17:03:43 -0500147 priv = (struct tsec_private *)malloc(sizeof(*priv));
wdenk97d80fc2004-06-09 00:34:46 +0000148
Jon Loeliger89875e92006-10-10 17:03:43 -0500149 if (NULL == priv)
wdenk97d80fc2004-06-09 00:34:46 +0000150 return 0;
151
152 privlist[index] = priv;
Jon Loeliger89875e92006-10-10 17:03:43 -0500153 priv->regs = (volatile tsec_t *)(TSEC_BASE_ADDR + index * TSEC_SIZE);
wdenk97d80fc2004-06-09 00:34:46 +0000154 priv->phyregs = (volatile tsec_t *)(TSEC_BASE_ADDR +
Jon Loeliger89875e92006-10-10 17:03:43 -0500155 tsec_info[index].phyregidx *
156 TSEC_SIZE);
wdenk97d80fc2004-06-09 00:34:46 +0000157
158 priv->phyaddr = tsec_info[index].phyaddr;
Jon Loeligerd9b94f22005-07-25 14:05:07 -0500159 priv->flags = tsec_info[index].flags;
wdenk97d80fc2004-06-09 00:34:46 +0000160
Jon Loeligerd9b94f22005-07-25 14:05:07 -0500161 sprintf(dev->name, devname);
wdenk42d1f032003-10-15 23:53:47 +0000162 dev->iobase = 0;
Jon Loeliger89875e92006-10-10 17:03:43 -0500163 dev->priv = priv;
164 dev->init = tsec_init;
165 dev->halt = tsec_halt;
166 dev->send = tsec_send;
167 dev->recv = tsec_recv;
David Updegraff53a5c422007-06-11 10:41:07 -0500168#ifdef CONFIG_MCAST_TFTP
169 dev->mcast = tsec_mcast_addr;
170#endif
wdenk42d1f032003-10-15 23:53:47 +0000171
172 /* Tell u-boot to get the addr from the env */
Jon Loeliger89875e92006-10-10 17:03:43 -0500173 for (i = 0; i < 6; i++)
wdenk42d1f032003-10-15 23:53:47 +0000174 dev->enetaddr[i] = 0;
175
176 eth_register(dev);
177
wdenk97d80fc2004-06-09 00:34:46 +0000178 /* Reset the MAC */
179 priv->regs->maccfg1 |= MACCFG1_SOFT_RESET;
180 priv->regs->maccfg1 &= ~(MACCFG1_SOFT_RESET);
wdenk7abf0c52004-04-18 21:45:42 +0000181
Jon Loeligercb51c0b2007-07-09 17:39:42 -0500182#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200183 && !defined(BITBANGMII)
184 miiphy_register(dev->name, tsec_miiphy_read, tsec_miiphy_write);
185#endif
186
wdenk97d80fc2004-06-09 00:34:46 +0000187 /* Try to initialize PHY here, and return */
188 return init_phy(dev);
wdenk42d1f032003-10-15 23:53:47 +0000189}
190
wdenk42d1f032003-10-15 23:53:47 +0000191/* Initializes data structures and registers for the controller,
wdenk9d46ea42005-03-14 23:56:42 +0000192 * and brings the interface up. Returns the link status, meaning
wdenk97d80fc2004-06-09 00:34:46 +0000193 * that it returns success if the link is up, failure otherwise.
Jon Loeliger89875e92006-10-10 17:03:43 -0500194 * This allows u-boot to find the first active controller.
195 */
196int tsec_init(struct eth_device *dev, bd_t * bd)
wdenk42d1f032003-10-15 23:53:47 +0000197{
wdenk42d1f032003-10-15 23:53:47 +0000198 uint tempval;
199 char tmpbuf[MAC_ADDR_LEN];
200 int i;
wdenk97d80fc2004-06-09 00:34:46 +0000201 struct tsec_private *priv = (struct tsec_private *)dev->priv;
202 volatile tsec_t *regs = priv->regs;
wdenk42d1f032003-10-15 23:53:47 +0000203
204 /* Make sure the controller is stopped */
205 tsec_halt(dev);
206
wdenk97d80fc2004-06-09 00:34:46 +0000207 /* Init MACCFG2. Defaults to GMII */
wdenk42d1f032003-10-15 23:53:47 +0000208 regs->maccfg2 = MACCFG2_INIT_SETTINGS;
209
210 /* Init ECNTRL */
211 regs->ecntrl = ECNTRL_INIT_SETTINGS;
212
213 /* Copy the station address into the address registers.
214 * Backwards, because little endian MACS are dumb */
Jon Loeliger89875e92006-10-10 17:03:43 -0500215 for (i = 0; i < MAC_ADDR_LEN; i++) {
wdenk97d80fc2004-06-09 00:34:46 +0000216 tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i];
wdenk42d1f032003-10-15 23:53:47 +0000217 }
Jon Loeliger89875e92006-10-10 17:03:43 -0500218 regs->macstnaddr1 = *((uint *) (tmpbuf));
wdenk42d1f032003-10-15 23:53:47 +0000219
Jon Loeliger89875e92006-10-10 17:03:43 -0500220 tempval = *((uint *) (tmpbuf + 4));
wdenk42d1f032003-10-15 23:53:47 +0000221
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200222 regs->macstnaddr2 = tempval;
wdenk42d1f032003-10-15 23:53:47 +0000223
wdenk42d1f032003-10-15 23:53:47 +0000224 /* reset the indices to zero */
225 rxIdx = 0;
226 txIdx = 0;
227
228 /* Clear out (for the most part) the other registers */
229 init_registers(regs);
230
231 /* Ready the device for tx/rx */
wdenk97d80fc2004-06-09 00:34:46 +0000232 startup_tsec(dev);
wdenk42d1f032003-10-15 23:53:47 +0000233
wdenk97d80fc2004-06-09 00:34:46 +0000234 /* If there's no link, fail */
Ben Warren422b1a02008-01-09 18:15:53 -0500235 return (priv->link ? 0 : -1);
wdenk42d1f032003-10-15 23:53:47 +0000236
237}
238
wdenk97d80fc2004-06-09 00:34:46 +0000239/* Write value to the device's PHY through the registers
240 * specified in priv, modifying the register specified in regnum.
241 * It will wait for the write to be done (or for a timeout to
242 * expire) before exiting
243 */
michael.firth@bt.com55fe7c52008-01-16 11:40:51 +0000244void write_any_phy_reg(struct tsec_private *priv, uint phyid, uint regnum, uint value)
wdenk97d80fc2004-06-09 00:34:46 +0000245{
246 volatile tsec_t *regbase = priv->phyregs;
Jon Loeliger89875e92006-10-10 17:03:43 -0500247 int timeout = 1000000;
wdenk97d80fc2004-06-09 00:34:46 +0000248
249 regbase->miimadd = (phyid << 8) | regnum;
250 regbase->miimcon = value;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500251 asm("sync");
wdenk97d80fc2004-06-09 00:34:46 +0000252
Jon Loeliger89875e92006-10-10 17:03:43 -0500253 timeout = 1000000;
254 while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
wdenk97d80fc2004-06-09 00:34:46 +0000255}
256
michael.firth@bt.com55fe7c52008-01-16 11:40:51 +0000257/* #define to provide old write_phy_reg functionality without duplicating code */
258#define write_phy_reg(priv, regnum, value) write_any_phy_reg(priv,priv->phyaddr,regnum,value)
259
wdenk97d80fc2004-06-09 00:34:46 +0000260/* Reads register regnum on the device's PHY through the
wdenk9d46ea42005-03-14 23:56:42 +0000261 * registers specified in priv. It lowers and raises the read
wdenk97d80fc2004-06-09 00:34:46 +0000262 * command, and waits for the data to become valid (miimind
263 * notvalid bit cleared), and the bus to cease activity (miimind
264 * busy bit cleared), and then returns the value
265 */
michael.firth@bt.com55fe7c52008-01-16 11:40:51 +0000266uint read_any_phy_reg(struct tsec_private *priv, uint phyid, uint regnum)
wdenk42d1f032003-10-15 23:53:47 +0000267{
268 uint value;
wdenk97d80fc2004-06-09 00:34:46 +0000269 volatile tsec_t *regbase = priv->phyregs;
wdenk42d1f032003-10-15 23:53:47 +0000270
wdenk97d80fc2004-06-09 00:34:46 +0000271 /* Put the address of the phy, and the register
272 * number into MIIMADD */
273 regbase->miimadd = (phyid << 8) | regnum;
wdenk42d1f032003-10-15 23:53:47 +0000274
275 /* Clear the command register, and wait */
276 regbase->miimcom = 0;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500277 asm("sync");
wdenk42d1f032003-10-15 23:53:47 +0000278
279 /* Initiate a read command, and wait */
280 regbase->miimcom = MIIM_READ_COMMAND;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500281 asm("sync");
wdenk42d1f032003-10-15 23:53:47 +0000282
283 /* Wait for the the indication that the read is done */
Jon Loeliger89875e92006-10-10 17:03:43 -0500284 while ((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY))) ;
wdenk42d1f032003-10-15 23:53:47 +0000285
286 /* Grab the value read from the PHY */
287 value = regbase->miimstat;
288
289 return value;
290}
291
michael.firth@bt.com55fe7c52008-01-16 11:40:51 +0000292/* #define to provide old read_phy_reg functionality without duplicating code */
293#define read_phy_reg(priv,regnum) read_any_phy_reg(priv,priv->phyaddr,regnum)
294
wdenk97d80fc2004-06-09 00:34:46 +0000295/* Discover which PHY is attached to the device, and configure it
296 * properly. If the PHY is not recognized, then return 0
297 * (failure). Otherwise, return 1
298 */
299static int init_phy(struct eth_device *dev)
wdenk42d1f032003-10-15 23:53:47 +0000300{
wdenk97d80fc2004-06-09 00:34:46 +0000301 struct tsec_private *priv = (struct tsec_private *)dev->priv;
302 struct phy_info *curphy;
Jon Loeliger89875e92006-10-10 17:03:43 -0500303 volatile tsec_t *regs = (volatile tsec_t *)(TSEC_BASE_ADDR);
wdenk42d1f032003-10-15 23:53:47 +0000304
305 /* Assign a Physical address to the TBI */
Joe Hammandcb84b72007-08-09 09:08:18 -0500306 regs->tbipa = CFG_TBIPA_VALUE;
Jon Loeliger89875e92006-10-10 17:03:43 -0500307 regs = (volatile tsec_t *)(TSEC_BASE_ADDR + TSEC_SIZE);
Joe Hammandcb84b72007-08-09 09:08:18 -0500308 regs->tbipa = CFG_TBIPA_VALUE;
Jon Loeliger89875e92006-10-10 17:03:43 -0500309 asm("sync");
wdenk3dd7f0f2005-04-04 23:43:44 +0000310
311 /* Reset MII (due to new addresses) */
312 priv->phyregs->miimcfg = MIIMCFG_RESET;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500313 asm("sync");
wdenk3dd7f0f2005-04-04 23:43:44 +0000314 priv->phyregs->miimcfg = MIIMCFG_INIT_VALUE;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500315 asm("sync");
Jon Loeliger89875e92006-10-10 17:03:43 -0500316 while (priv->phyregs->miimind & MIIMIND_BUSY) ;
wdenk42d1f032003-10-15 23:53:47 +0000317
Jon Loeliger89875e92006-10-10 17:03:43 -0500318 if (0 == relocated)
wdenk97d80fc2004-06-09 00:34:46 +0000319 relocate_cmds();
wdenk42d1f032003-10-15 23:53:47 +0000320
wdenk97d80fc2004-06-09 00:34:46 +0000321 /* Get the cmd structure corresponding to the attached
322 * PHY */
323 curphy = get_phy_info(dev);
wdenk42d1f032003-10-15 23:53:47 +0000324
Ben Warren4653f912006-10-26 14:38:25 -0400325 if (curphy == NULL) {
326 priv->phyinfo = NULL;
wdenk97d80fc2004-06-09 00:34:46 +0000327 printf("%s: No PHY found\n", dev->name);
wdenk42d1f032003-10-15 23:53:47 +0000328
wdenk97d80fc2004-06-09 00:34:46 +0000329 return 0;
wdenk42d1f032003-10-15 23:53:47 +0000330 }
331
wdenk97d80fc2004-06-09 00:34:46 +0000332 priv->phyinfo = curphy;
wdenk42d1f032003-10-15 23:53:47 +0000333
wdenk97d80fc2004-06-09 00:34:46 +0000334 phy_run_commands(priv, priv->phyinfo->config);
wdenk42d1f032003-10-15 23:53:47 +0000335
wdenk97d80fc2004-06-09 00:34:46 +0000336 return 1;
wdenk42d1f032003-10-15 23:53:47 +0000337}
338
Jon Loeliger89875e92006-10-10 17:03:43 -0500339/*
340 * Returns which value to write to the control register.
341 * For 10/100, the value is slightly different
342 */
343uint mii_cr_init(uint mii_reg, struct tsec_private * priv)
wdenk97d80fc2004-06-09 00:34:46 +0000344{
Jon Loeliger89875e92006-10-10 17:03:43 -0500345 if (priv->flags & TSEC_GIGABIT)
wdenk97d80fc2004-06-09 00:34:46 +0000346 return MIIM_CONTROL_INIT;
347 else
348 return MIIM_CR_INIT;
349}
350
wdenk97d80fc2004-06-09 00:34:46 +0000351/* Parse the status register for link, and then do
Jon Loeliger89875e92006-10-10 17:03:43 -0500352 * auto-negotiation
353 */
354uint mii_parse_sr(uint mii_reg, struct tsec_private * priv)
wdenk97d80fc2004-06-09 00:34:46 +0000355{
Stefan Roese5810dc32005-09-21 18:20:22 +0200356 /*
Andy Fleming7613afd2007-08-15 20:03:44 -0500357 * Wait if the link is up, and autonegotiation is in progress
358 * (ie - we're capable and it's not done)
Stefan Roese5810dc32005-09-21 18:20:22 +0200359 */
360 mii_reg = read_phy_reg(priv, MIIM_STATUS);
Andy Fleming7613afd2007-08-15 20:03:44 -0500361 if ((mii_reg & MIIM_STATUS_LINK) && (mii_reg & PHY_BMSR_AUTN_ABLE)
Jon Loeliger89875e92006-10-10 17:03:43 -0500362 && !(mii_reg & PHY_BMSR_AUTN_COMP)) {
Stefan Roese5810dc32005-09-21 18:20:22 +0200363 int i = 0;
wdenk97d80fc2004-06-09 00:34:46 +0000364
Jon Loeliger89875e92006-10-10 17:03:43 -0500365 puts("Waiting for PHY auto negotiation to complete");
Andy Fleming7613afd2007-08-15 20:03:44 -0500366 while (!(mii_reg & PHY_BMSR_AUTN_COMP)) {
Stefan Roese5810dc32005-09-21 18:20:22 +0200367 /*
368 * Timeout reached ?
369 */
370 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
Jon Loeliger89875e92006-10-10 17:03:43 -0500371 puts(" TIMEOUT !\n");
Stefan Roese5810dc32005-09-21 18:20:22 +0200372 priv->link = 0;
Jin Zhengxiong-R64188fcfb9a52006-06-27 18:12:23 +0800373 return 0;
Stefan Roese5810dc32005-09-21 18:20:22 +0200374 }
wdenk97d80fc2004-06-09 00:34:46 +0000375
Stefan Roese5810dc32005-09-21 18:20:22 +0200376 if ((i++ % 1000) == 0) {
Jon Loeliger89875e92006-10-10 17:03:43 -0500377 putc('.');
Stefan Roese5810dc32005-09-21 18:20:22 +0200378 }
Jon Loeliger89875e92006-10-10 17:03:43 -0500379 udelay(1000); /* 1 ms */
wdenk97d80fc2004-06-09 00:34:46 +0000380 mii_reg = read_phy_reg(priv, MIIM_STATUS);
Stefan Roese5810dc32005-09-21 18:20:22 +0200381 }
Jon Loeliger89875e92006-10-10 17:03:43 -0500382 puts(" done\n");
Stefan Roese5810dc32005-09-21 18:20:22 +0200383 priv->link = 1;
Jon Loeliger89875e92006-10-10 17:03:43 -0500384 udelay(500000); /* another 500 ms (results in faster booting) */
Stefan Roese5810dc32005-09-21 18:20:22 +0200385 } else {
Andy Fleming7613afd2007-08-15 20:03:44 -0500386 if (mii_reg & MIIM_STATUS_LINK)
387 priv->link = 1;
388 else
389 priv->link = 0;
wdenk97d80fc2004-06-09 00:34:46 +0000390 }
391
392 return 0;
393}
394
David Updegraffaf1c2b82007-04-20 14:34:48 -0500395/* Generic function which updates the speed and duplex. If
396 * autonegotiation is enabled, it uses the AND of the link
397 * partner's advertised capabilities and our advertised
398 * capabilities. If autonegotiation is disabled, we use the
399 * appropriate bits in the control register.
400 *
401 * Stolen from Linux's mii.c and phy_device.c
402 */
403uint mii_parse_link(uint mii_reg, struct tsec_private *priv)
404{
405 /* We're using autonegotiation */
406 if (mii_reg & PHY_BMSR_AUTN_ABLE) {
407 uint lpa = 0;
408 uint gblpa = 0;
409
410 /* Check for gigabit capability */
411 if (mii_reg & PHY_BMSR_EXT) {
412 /* We want a list of states supported by
413 * both PHYs in the link
414 */
415 gblpa = read_phy_reg(priv, PHY_1000BTSR);
416 gblpa &= read_phy_reg(priv, PHY_1000BTCR) << 2;
417 }
418
419 /* Set the baseline so we only have to set them
420 * if they're different
421 */
422 priv->speed = 10;
423 priv->duplexity = 0;
424
425 /* Check the gigabit fields */
426 if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
427 priv->speed = 1000;
428
429 if (gblpa & PHY_1000BTSR_1000FD)
430 priv->duplexity = 1;
431
432 /* We're done! */
433 return 0;
434 }
435
436 lpa = read_phy_reg(priv, PHY_ANAR);
437 lpa &= read_phy_reg(priv, PHY_ANLPAR);
438
439 if (lpa & (PHY_ANLPAR_TXFD | PHY_ANLPAR_TX)) {
440 priv->speed = 100;
441
442 if (lpa & PHY_ANLPAR_TXFD)
443 priv->duplexity = 1;
444
445 } else if (lpa & PHY_ANLPAR_10FD)
446 priv->duplexity = 1;
447 } else {
448 uint bmcr = read_phy_reg(priv, PHY_BMCR);
449
450 priv->speed = 10;
451 priv->duplexity = 0;
452
453 if (bmcr & PHY_BMCR_DPLX)
454 priv->duplexity = 1;
455
456 if (bmcr & PHY_BMCR_1000_MBPS)
457 priv->speed = 1000;
458 else if (bmcr & PHY_BMCR_100_MBPS)
459 priv->speed = 100;
460 }
461
462 return 0;
463}
464
Paul Gortmaker91e25762007-01-16 11:38:14 -0500465/*
466 * Parse the BCM54xx status register for speed and duplex information.
467 * The linux sungem_phy has this information, but in a table format.
468 */
469uint mii_parse_BCM54xx_sr(uint mii_reg, struct tsec_private *priv)
470{
471
472 switch((mii_reg & MIIM_BCM54xx_AUXSTATUS_LINKMODE_MASK) >> MIIM_BCM54xx_AUXSTATUS_LINKMODE_SHIFT){
473
474 case 1:
475 printf("Enet starting in 10BT/HD\n");
476 priv->duplexity = 0;
477 priv->speed = 10;
478 break;
479
480 case 2:
481 printf("Enet starting in 10BT/FD\n");
482 priv->duplexity = 1;
483 priv->speed = 10;
484 break;
485
486 case 3:
487 printf("Enet starting in 100BT/HD\n");
488 priv->duplexity = 0;
489 priv->speed = 100;
490 break;
491
492 case 5:
493 printf("Enet starting in 100BT/FD\n");
494 priv->duplexity = 1;
495 priv->speed = 100;
496 break;
497
498 case 6:
499 printf("Enet starting in 1000BT/HD\n");
500 priv->duplexity = 0;
501 priv->speed = 1000;
502 break;
503
504 case 7:
505 printf("Enet starting in 1000BT/FD\n");
506 priv->duplexity = 1;
507 priv->speed = 1000;
508 break;
509
510 default:
511 printf("Auto-neg error, defaulting to 10BT/HD\n");
512 priv->duplexity = 0;
513 priv->speed = 10;
514 break;
515 }
516
517 return 0;
518
519}
wdenk97d80fc2004-06-09 00:34:46 +0000520/* Parse the 88E1011's status register for speed and duplex
Jon Loeliger89875e92006-10-10 17:03:43 -0500521 * information
522 */
523uint mii_parse_88E1011_psr(uint mii_reg, struct tsec_private * priv)
wdenk97d80fc2004-06-09 00:34:46 +0000524{
525 uint speed;
526
Stefan Roese5810dc32005-09-21 18:20:22 +0200527 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
528
Andy Fleming7613afd2007-08-15 20:03:44 -0500529 if ((mii_reg & MIIM_88E1011_PHYSTAT_LINK) &&
530 !(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
Stefan Roese5810dc32005-09-21 18:20:22 +0200531 int i = 0;
532
Jon Loeliger89875e92006-10-10 17:03:43 -0500533 puts("Waiting for PHY realtime link");
Andy Fleming7613afd2007-08-15 20:03:44 -0500534 while (!(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
535 /* Timeout reached ? */
Stefan Roese5810dc32005-09-21 18:20:22 +0200536 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
Jon Loeliger89875e92006-10-10 17:03:43 -0500537 puts(" TIMEOUT !\n");
Stefan Roese5810dc32005-09-21 18:20:22 +0200538 priv->link = 0;
539 break;
540 }
541
542 if ((i++ % 1000) == 0) {
Jon Loeliger89875e92006-10-10 17:03:43 -0500543 putc('.');
Stefan Roese5810dc32005-09-21 18:20:22 +0200544 }
Jon Loeliger89875e92006-10-10 17:03:43 -0500545 udelay(1000); /* 1 ms */
Stefan Roese5810dc32005-09-21 18:20:22 +0200546 mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
547 }
Jon Loeliger89875e92006-10-10 17:03:43 -0500548 puts(" done\n");
549 udelay(500000); /* another 500 ms (results in faster booting) */
Andy Fleming7613afd2007-08-15 20:03:44 -0500550 } else {
551 if (mii_reg & MIIM_88E1011_PHYSTAT_LINK)
552 priv->link = 1;
553 else
554 priv->link = 0;
Stefan Roese5810dc32005-09-21 18:20:22 +0200555 }
556
Jon Loeliger89875e92006-10-10 17:03:43 -0500557 if (mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
wdenk97d80fc2004-06-09 00:34:46 +0000558 priv->duplexity = 1;
559 else
560 priv->duplexity = 0;
561
Jon Loeliger89875e92006-10-10 17:03:43 -0500562 speed = (mii_reg & MIIM_88E1011_PHYSTAT_SPEED);
wdenk97d80fc2004-06-09 00:34:46 +0000563
Jon Loeliger89875e92006-10-10 17:03:43 -0500564 switch (speed) {
565 case MIIM_88E1011_PHYSTAT_GBIT:
566 priv->speed = 1000;
567 break;
568 case MIIM_88E1011_PHYSTAT_100:
569 priv->speed = 100;
570 break;
571 default:
572 priv->speed = 10;
wdenk97d80fc2004-06-09 00:34:46 +0000573 }
574
575 return 0;
576}
577
Dave Liu18ee3202008-01-11 18:45:28 +0800578/* Parse the RTL8211B's status register for speed and duplex
579 * information
580 */
581uint mii_parse_RTL8211B_sr(uint mii_reg, struct tsec_private * priv)
582{
583 uint speed;
584
585 mii_reg = read_phy_reg(priv, MIIM_RTL8211B_PHY_STATUS);
Anton Vorontsovc7604782008-03-14 23:20:30 +0300586 if (!(mii_reg & MIIM_RTL8211B_PHYSTAT_SPDDONE)) {
Dave Liu18ee3202008-01-11 18:45:28 +0800587 int i = 0;
588
Anton Vorontsovc7604782008-03-14 23:20:30 +0300589 /* in case of timeout ->link is cleared */
590 priv->link = 1;
Dave Liu18ee3202008-01-11 18:45:28 +0800591 puts("Waiting for PHY realtime link");
592 while (!(mii_reg & MIIM_RTL8211B_PHYSTAT_SPDDONE)) {
593 /* Timeout reached ? */
594 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
595 puts(" TIMEOUT !\n");
596 priv->link = 0;
597 break;
598 }
599
600 if ((i++ % 1000) == 0) {
601 putc('.');
602 }
603 udelay(1000); /* 1 ms */
604 mii_reg = read_phy_reg(priv, MIIM_RTL8211B_PHY_STATUS);
605 }
606 puts(" done\n");
607 udelay(500000); /* another 500 ms (results in faster booting) */
608 } else {
609 if (mii_reg & MIIM_RTL8211B_PHYSTAT_LINK)
610 priv->link = 1;
611 else
612 priv->link = 0;
613 }
614
615 if (mii_reg & MIIM_RTL8211B_PHYSTAT_DUPLEX)
616 priv->duplexity = 1;
617 else
618 priv->duplexity = 0;
619
620 speed = (mii_reg & MIIM_RTL8211B_PHYSTAT_SPEED);
621
622 switch (speed) {
623 case MIIM_RTL8211B_PHYSTAT_GBIT:
624 priv->speed = 1000;
625 break;
626 case MIIM_RTL8211B_PHYSTAT_100:
627 priv->speed = 100;
628 break;
629 default:
630 priv->speed = 10;
631 }
632
633 return 0;
634}
635
wdenk97d80fc2004-06-09 00:34:46 +0000636/* Parse the cis8201's status register for speed and duplex
Jon Loeliger89875e92006-10-10 17:03:43 -0500637 * information
638 */
639uint mii_parse_cis8201(uint mii_reg, struct tsec_private * priv)
wdenk97d80fc2004-06-09 00:34:46 +0000640{
641 uint speed;
642
Jon Loeliger89875e92006-10-10 17:03:43 -0500643 if (mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX)
wdenk97d80fc2004-06-09 00:34:46 +0000644 priv->duplexity = 1;
645 else
646 priv->duplexity = 0;
647
648 speed = mii_reg & MIIM_CIS8201_AUXCONSTAT_SPEED;
Jon Loeliger89875e92006-10-10 17:03:43 -0500649 switch (speed) {
650 case MIIM_CIS8201_AUXCONSTAT_GBIT:
651 priv->speed = 1000;
652 break;
653 case MIIM_CIS8201_AUXCONSTAT_100:
654 priv->speed = 100;
655 break;
656 default:
657 priv->speed = 10;
658 break;
wdenk97d80fc2004-06-09 00:34:46 +0000659 }
660
661 return 0;
662}
Jon Loeliger89875e92006-10-10 17:03:43 -0500663
Jon Loeligerdebb7352006-04-26 17:58:56 -0500664/* Parse the vsc8244's status register for speed and duplex
Jon Loeliger89875e92006-10-10 17:03:43 -0500665 * information
666 */
667uint mii_parse_vsc8244(uint mii_reg, struct tsec_private * priv)
Jon Loeligerdebb7352006-04-26 17:58:56 -0500668{
Jon Loeliger89875e92006-10-10 17:03:43 -0500669 uint speed;
670
671 if (mii_reg & MIIM_VSC8244_AUXCONSTAT_DUPLEX)
672 priv->duplexity = 1;
673 else
674 priv->duplexity = 0;
675
676 speed = mii_reg & MIIM_VSC8244_AUXCONSTAT_SPEED;
677 switch (speed) {
678 case MIIM_VSC8244_AUXCONSTAT_GBIT:
679 priv->speed = 1000;
680 break;
681 case MIIM_VSC8244_AUXCONSTAT_100:
682 priv->speed = 100;
683 break;
684 default:
685 priv->speed = 10;
686 break;
687 }
688
689 return 0;
Jon Loeligerdebb7352006-04-26 17:58:56 -0500690}
wdenk97d80fc2004-06-09 00:34:46 +0000691
wdenk97d80fc2004-06-09 00:34:46 +0000692/* Parse the DM9161's status register for speed and duplex
Jon Loeliger89875e92006-10-10 17:03:43 -0500693 * information
694 */
695uint mii_parse_dm9161_scsr(uint mii_reg, struct tsec_private * priv)
wdenk97d80fc2004-06-09 00:34:46 +0000696{
Jon Loeliger89875e92006-10-10 17:03:43 -0500697 if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_100H))
wdenk97d80fc2004-06-09 00:34:46 +0000698 priv->speed = 100;
699 else
700 priv->speed = 10;
701
Jon Loeliger89875e92006-10-10 17:03:43 -0500702 if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_10F))
wdenk97d80fc2004-06-09 00:34:46 +0000703 priv->duplexity = 1;
704 else
705 priv->duplexity = 0;
706
707 return 0;
708}
709
Jon Loeliger89875e92006-10-10 17:03:43 -0500710/*
711 * Hack to write all 4 PHYs with the LED values
712 */
713uint mii_cis8204_fixled(uint mii_reg, struct tsec_private * priv)
wdenk97d80fc2004-06-09 00:34:46 +0000714{
715 uint phyid;
716 volatile tsec_t *regbase = priv->phyregs;
Jon Loeliger89875e92006-10-10 17:03:43 -0500717 int timeout = 1000000;
wdenk97d80fc2004-06-09 00:34:46 +0000718
Jon Loeliger89875e92006-10-10 17:03:43 -0500719 for (phyid = 0; phyid < 4; phyid++) {
wdenk97d80fc2004-06-09 00:34:46 +0000720 regbase->miimadd = (phyid << 8) | mii_reg;
721 regbase->miimcon = MIIM_CIS8204_SLEDCON_INIT;
Eran Libertyf046ccd2005-07-28 10:08:46 -0500722 asm("sync");
wdenk97d80fc2004-06-09 00:34:46 +0000723
Jon Loeliger89875e92006-10-10 17:03:43 -0500724 timeout = 1000000;
725 while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
wdenk97d80fc2004-06-09 00:34:46 +0000726 }
727
728 return MIIM_CIS8204_SLEDCON_INIT;
729}
730
Jon Loeliger89875e92006-10-10 17:03:43 -0500731uint mii_cis8204_setmode(uint mii_reg, struct tsec_private * priv)
Jon Loeligerd9b94f22005-07-25 14:05:07 -0500732{
733 if (priv->flags & TSEC_REDUCED)
734 return MIIM_CIS8204_EPHYCON_INIT | MIIM_CIS8204_EPHYCON_RGMII;
735 else
736 return MIIM_CIS8204_EPHYCON_INIT;
737}
wdenk97d80fc2004-06-09 00:34:46 +0000738
Dave Liu19580e62007-09-18 12:37:57 +0800739uint mii_m88e1111s_setmode(uint mii_reg, struct tsec_private *priv)
740{
741 uint mii_data = read_phy_reg(priv, mii_reg);
742
743 if (priv->flags & TSEC_REDUCED)
744 mii_data = (mii_data & 0xfff0) | 0x000b;
745 return mii_data;
746}
747
wdenk97d80fc2004-06-09 00:34:46 +0000748/* Initialized required registers to appropriate values, zeroing
749 * those we don't care about (unless zero is bad, in which case,
Jon Loeliger89875e92006-10-10 17:03:43 -0500750 * choose a more appropriate value)
751 */
752static void init_registers(volatile tsec_t * regs)
wdenk42d1f032003-10-15 23:53:47 +0000753{
754 /* Clear IEVENT */
755 regs->ievent = IEVENT_INIT_CLEAR;
756
757 regs->imask = IMASK_INIT_CLEAR;
758
759 regs->hash.iaddr0 = 0;
760 regs->hash.iaddr1 = 0;
761 regs->hash.iaddr2 = 0;
762 regs->hash.iaddr3 = 0;
763 regs->hash.iaddr4 = 0;
764 regs->hash.iaddr5 = 0;
765 regs->hash.iaddr6 = 0;
766 regs->hash.iaddr7 = 0;
767
768 regs->hash.gaddr0 = 0;
769 regs->hash.gaddr1 = 0;
770 regs->hash.gaddr2 = 0;
771 regs->hash.gaddr3 = 0;
772 regs->hash.gaddr4 = 0;
773 regs->hash.gaddr5 = 0;
774 regs->hash.gaddr6 = 0;
775 regs->hash.gaddr7 = 0;
776
777 regs->rctrl = 0x00000000;
778
779 /* Init RMON mib registers */
780 memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
781
782 regs->rmon.cam1 = 0xffffffff;
783 regs->rmon.cam2 = 0xffffffff;
784
785 regs->mrblr = MRBLR_INIT_SETTINGS;
786
787 regs->minflr = MINFLR_INIT_SETTINGS;
788
789 regs->attr = ATTR_INIT_SETTINGS;
790 regs->attreli = ATTRELI_INIT_SETTINGS;
791
792}
793
wdenk97d80fc2004-06-09 00:34:46 +0000794/* Configure maccfg2 based on negotiated speed and duplex
Jon Loeliger89875e92006-10-10 17:03:43 -0500795 * reported by PHY handling code
796 */
wdenk97d80fc2004-06-09 00:34:46 +0000797static void adjust_link(struct eth_device *dev)
798{
799 struct tsec_private *priv = (struct tsec_private *)dev->priv;
800 volatile tsec_t *regs = priv->regs;
801
Jon Loeliger89875e92006-10-10 17:03:43 -0500802 if (priv->link) {
803 if (priv->duplexity != 0)
wdenk97d80fc2004-06-09 00:34:46 +0000804 regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
805 else
806 regs->maccfg2 &= ~(MACCFG2_FULL_DUPLEX);
807
Jon Loeliger89875e92006-10-10 17:03:43 -0500808 switch (priv->speed) {
809 case 1000:
810 regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
811 | MACCFG2_GMII);
812 break;
813 case 100:
814 case 10:
815 regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
816 | MACCFG2_MII);
Jon Loeligerd9b94f22005-07-25 14:05:07 -0500817
Nick Spencef484dc72006-09-07 07:39:46 -0700818 /* Set R100 bit in all modes although
819 * it is only used in RGMII mode
Jon Loeliger89875e92006-10-10 17:03:43 -0500820 */
Nick Spencef484dc72006-09-07 07:39:46 -0700821 if (priv->speed == 100)
Jon Loeliger89875e92006-10-10 17:03:43 -0500822 regs->ecntrl |= ECNTRL_R100;
823 else
824 regs->ecntrl &= ~(ECNTRL_R100);
825 break;
826 default:
827 printf("%s: Speed was bad\n", dev->name);
828 break;
wdenk97d80fc2004-06-09 00:34:46 +0000829 }
830
831 printf("Speed: %d, %s duplex\n", priv->speed,
Jon Loeliger89875e92006-10-10 17:03:43 -0500832 (priv->duplexity) ? "full" : "half");
wdenk97d80fc2004-06-09 00:34:46 +0000833
834 } else {
835 printf("%s: No link.\n", dev->name);
836 }
837}
838
wdenk97d80fc2004-06-09 00:34:46 +0000839/* Set up the buffers and their descriptors, and bring up the
Jon Loeliger89875e92006-10-10 17:03:43 -0500840 * interface
841 */
wdenk97d80fc2004-06-09 00:34:46 +0000842static void startup_tsec(struct eth_device *dev)
wdenk42d1f032003-10-15 23:53:47 +0000843{
844 int i;
wdenk97d80fc2004-06-09 00:34:46 +0000845 struct tsec_private *priv = (struct tsec_private *)dev->priv;
846 volatile tsec_t *regs = priv->regs;
wdenk42d1f032003-10-15 23:53:47 +0000847
848 /* Point to the buffer descriptors */
849 regs->tbase = (unsigned int)(&rtx.txbd[txIdx]);
850 regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
851
852 /* Initialize the Rx Buffer descriptors */
853 for (i = 0; i < PKTBUFSRX; i++) {
854 rtx.rxbd[i].status = RXBD_EMPTY;
855 rtx.rxbd[i].length = 0;
Jon Loeliger89875e92006-10-10 17:03:43 -0500856 rtx.rxbd[i].bufPtr = (uint) NetRxPackets[i];
wdenk42d1f032003-10-15 23:53:47 +0000857 }
Jon Loeliger89875e92006-10-10 17:03:43 -0500858 rtx.rxbd[PKTBUFSRX - 1].status |= RXBD_WRAP;
wdenk42d1f032003-10-15 23:53:47 +0000859
860 /* Initialize the TX Buffer Descriptors */
Jon Loeliger89875e92006-10-10 17:03:43 -0500861 for (i = 0; i < TX_BUF_CNT; i++) {
wdenk42d1f032003-10-15 23:53:47 +0000862 rtx.txbd[i].status = 0;
863 rtx.txbd[i].length = 0;
864 rtx.txbd[i].bufPtr = 0;
865 }
Jon Loeliger89875e92006-10-10 17:03:43 -0500866 rtx.txbd[TX_BUF_CNT - 1].status |= TXBD_WRAP;
wdenk42d1f032003-10-15 23:53:47 +0000867
wdenk97d80fc2004-06-09 00:34:46 +0000868 /* Start up the PHY */
Ben Warren4653f912006-10-26 14:38:25 -0400869 if(priv->phyinfo)
870 phy_run_commands(priv, priv->phyinfo->startup);
David Updegraffaf1c2b82007-04-20 14:34:48 -0500871
wdenk97d80fc2004-06-09 00:34:46 +0000872 adjust_link(dev);
873
wdenk42d1f032003-10-15 23:53:47 +0000874 /* Enable Transmit and Receive */
875 regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
876
877 /* Tell the DMA it is clear to go */
878 regs->dmactrl |= DMACTRL_INIT_SETTINGS;
879 regs->tstat = TSTAT_CLEAR_THALT;
Dan Wilson5c7ea642007-10-19 11:33:48 -0500880 regs->rstat = RSTAT_CLEAR_RHALT;
wdenk42d1f032003-10-15 23:53:47 +0000881 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
882}
883
wdenk9d46ea42005-03-14 23:56:42 +0000884/* This returns the status bits of the device. The return value
wdenk42d1f032003-10-15 23:53:47 +0000885 * is never checked, and this is what the 8260 driver did, so we
wdenk9d46ea42005-03-14 23:56:42 +0000886 * do the same. Presumably, this would be zero if there were no
Jon Loeliger89875e92006-10-10 17:03:43 -0500887 * errors
888 */
889static int tsec_send(struct eth_device *dev, volatile void *packet, int length)
wdenk42d1f032003-10-15 23:53:47 +0000890{
891 int i;
892 int result = 0;
wdenk97d80fc2004-06-09 00:34:46 +0000893 struct tsec_private *priv = (struct tsec_private *)dev->priv;
894 volatile tsec_t *regs = priv->regs;
wdenk42d1f032003-10-15 23:53:47 +0000895
896 /* Find an empty buffer descriptor */
Jon Loeliger89875e92006-10-10 17:03:43 -0500897 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
wdenk42d1f032003-10-15 23:53:47 +0000898 if (i >= TOUT_LOOP) {
Jon Loeliger89875e92006-10-10 17:03:43 -0500899 debug("%s: tsec: tx buffers full\n", dev->name);
wdenk42d1f032003-10-15 23:53:47 +0000900 return result;
901 }
902 }
903
Jon Loeliger89875e92006-10-10 17:03:43 -0500904 rtx.txbd[txIdx].bufPtr = (uint) packet;
wdenk42d1f032003-10-15 23:53:47 +0000905 rtx.txbd[txIdx].length = length;
Jon Loeliger89875e92006-10-10 17:03:43 -0500906 rtx.txbd[txIdx].status |=
907 (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
wdenk42d1f032003-10-15 23:53:47 +0000908
909 /* Tell the DMA to go */
910 regs->tstat = TSTAT_CLEAR_THALT;
911
912 /* Wait for buffer to be transmitted */
Jon Loeliger89875e92006-10-10 17:03:43 -0500913 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
wdenk42d1f032003-10-15 23:53:47 +0000914 if (i >= TOUT_LOOP) {
Jon Loeliger89875e92006-10-10 17:03:43 -0500915 debug("%s: tsec: tx error\n", dev->name);
wdenk42d1f032003-10-15 23:53:47 +0000916 return result;
917 }
918 }
919
920 txIdx = (txIdx + 1) % TX_BUF_CNT;
921 result = rtx.txbd[txIdx].status & TXBD_STATS;
922
923 return result;
924}
925
Jon Loeliger89875e92006-10-10 17:03:43 -0500926static int tsec_recv(struct eth_device *dev)
wdenk42d1f032003-10-15 23:53:47 +0000927{
928 int length;
wdenk97d80fc2004-06-09 00:34:46 +0000929 struct tsec_private *priv = (struct tsec_private *)dev->priv;
930 volatile tsec_t *regs = priv->regs;
wdenk42d1f032003-10-15 23:53:47 +0000931
Jon Loeliger89875e92006-10-10 17:03:43 -0500932 while (!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
wdenk42d1f032003-10-15 23:53:47 +0000933
934 length = rtx.rxbd[rxIdx].length;
935
936 /* Send the packet up if there were no errors */
937 if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
938 NetReceive(NetRxPackets[rxIdx], length - 4);
wdenk97d80fc2004-06-09 00:34:46 +0000939 } else {
940 printf("Got error %x\n",
Jon Loeliger89875e92006-10-10 17:03:43 -0500941 (rtx.rxbd[rxIdx].status & RXBD_STATS));
wdenk42d1f032003-10-15 23:53:47 +0000942 }
943
944 rtx.rxbd[rxIdx].length = 0;
945
946 /* Set the wrap bit if this is the last element in the list */
Jon Loeliger89875e92006-10-10 17:03:43 -0500947 rtx.rxbd[rxIdx].status =
948 RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
wdenk42d1f032003-10-15 23:53:47 +0000949
950 rxIdx = (rxIdx + 1) % PKTBUFSRX;
951 }
952
Jon Loeliger89875e92006-10-10 17:03:43 -0500953 if (regs->ievent & IEVENT_BSY) {
wdenk42d1f032003-10-15 23:53:47 +0000954 regs->ievent = IEVENT_BSY;
955 regs->rstat = RSTAT_CLEAR_RHALT;
956 }
957
958 return -1;
959
960}
961
wdenk97d80fc2004-06-09 00:34:46 +0000962/* Stop the interface */
Jon Loeliger89875e92006-10-10 17:03:43 -0500963static void tsec_halt(struct eth_device *dev)
wdenk42d1f032003-10-15 23:53:47 +0000964{
wdenk97d80fc2004-06-09 00:34:46 +0000965 struct tsec_private *priv = (struct tsec_private *)dev->priv;
966 volatile tsec_t *regs = priv->regs;
wdenk42d1f032003-10-15 23:53:47 +0000967
968 regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
969 regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS);
970
Jon Loeliger89875e92006-10-10 17:03:43 -0500971 while (!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC))) ;
wdenk42d1f032003-10-15 23:53:47 +0000972
973 regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
974
wdenk97d80fc2004-06-09 00:34:46 +0000975 /* Shut down the PHY, as needed */
Ben Warren4653f912006-10-26 14:38:25 -0400976 if(priv->phyinfo)
977 phy_run_commands(priv, priv->phyinfo->shutdown);
wdenk42d1f032003-10-15 23:53:47 +0000978}
wdenk7abf0c52004-04-18 21:45:42 +0000979
Andy Flemingc7e717e2007-08-03 04:05:25 -0500980struct phy_info phy_info_M88E1149S = {
Wolfgang Denk5728be32007-08-06 01:01:49 +0200981 0x1410ca,
982 "Marvell 88E1149S",
983 4,
984 (struct phy_cmd[]){ /* config */
985 /* Reset and configure the PHY */
986 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
987 {0x1d, 0x1f, NULL},
988 {0x1e, 0x200c, NULL},
989 {0x1d, 0x5, NULL},
990 {0x1e, 0x0, NULL},
991 {0x1e, 0x100, NULL},
992 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
993 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
994 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
995 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
996 {miim_end,}
997 },
998 (struct phy_cmd[]){ /* startup */
999 /* Status is read once to clear old link state */
1000 {MIIM_STATUS, miim_read, NULL},
1001 /* Auto-negotiate */
1002 {MIIM_STATUS, miim_read, &mii_parse_sr},
1003 /* Read the status */
1004 {MIIM_88E1011_PHY_STATUS, miim_read,
1005 &mii_parse_88E1011_psr},
1006 {miim_end,}
1007 },
1008 (struct phy_cmd[]){ /* shutdown */
1009 {miim_end,}
1010 },
Andy Flemingc7e717e2007-08-03 04:05:25 -05001011};
1012
Paul Gortmaker91e25762007-01-16 11:38:14 -05001013/* The 5411 id is 0x206070, the 5421 is 0x2060e0 */
1014struct phy_info phy_info_BCM5461S = {
1015 0x02060c1, /* 5461 ID */
1016 "Broadcom BCM5461S",
1017 0, /* not clear to me what minor revisions we can shift away */
1018 (struct phy_cmd[]) { /* config */
1019 /* Reset and configure the PHY */
1020 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1021 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1022 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1023 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1024 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1025 {miim_end,}
1026 },
1027 (struct phy_cmd[]) { /* startup */
1028 /* Status is read once to clear old link state */
1029 {MIIM_STATUS, miim_read, NULL},
1030 /* Auto-negotiate */
1031 {MIIM_STATUS, miim_read, &mii_parse_sr},
1032 /* Read the status */
1033 {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
1034 {miim_end,}
1035 },
1036 (struct phy_cmd[]) { /* shutdown */
1037 {miim_end,}
1038 },
1039};
1040
Joe Hammanc3243cf2007-04-30 16:47:28 -05001041struct phy_info phy_info_BCM5464S = {
1042 0x02060b1, /* 5464 ID */
1043 "Broadcom BCM5464S",
1044 0, /* not clear to me what minor revisions we can shift away */
1045 (struct phy_cmd[]) { /* config */
1046 /* Reset and configure the PHY */
1047 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1048 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1049 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1050 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1051 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1052 {miim_end,}
1053 },
1054 (struct phy_cmd[]) { /* startup */
1055 /* Status is read once to clear old link state */
1056 {MIIM_STATUS, miim_read, NULL},
1057 /* Auto-negotiate */
1058 {MIIM_STATUS, miim_read, &mii_parse_sr},
1059 /* Read the status */
1060 {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
1061 {miim_end,}
1062 },
1063 (struct phy_cmd[]) { /* shutdown */
1064 {miim_end,}
1065 },
1066};
1067
wdenk97d80fc2004-06-09 00:34:46 +00001068struct phy_info phy_info_M88E1011S = {
1069 0x01410c6,
1070 "Marvell 88E1011S",
1071 4,
Jon Loeliger89875e92006-10-10 17:03:43 -05001072 (struct phy_cmd[]){ /* config */
1073 /* Reset and configure the PHY */
1074 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1075 {0x1d, 0x1f, NULL},
1076 {0x1e, 0x200c, NULL},
1077 {0x1d, 0x5, NULL},
1078 {0x1e, 0x0, NULL},
1079 {0x1e, 0x100, NULL},
1080 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1081 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1082 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1083 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1084 {miim_end,}
1085 },
1086 (struct phy_cmd[]){ /* startup */
1087 /* Status is read once to clear old link state */
1088 {MIIM_STATUS, miim_read, NULL},
1089 /* Auto-negotiate */
1090 {MIIM_STATUS, miim_read, &mii_parse_sr},
1091 /* Read the status */
1092 {MIIM_88E1011_PHY_STATUS, miim_read,
1093 &mii_parse_88E1011_psr},
1094 {miim_end,}
1095 },
1096 (struct phy_cmd[]){ /* shutdown */
1097 {miim_end,}
1098 },
wdenk97d80fc2004-06-09 00:34:46 +00001099};
1100
wdenk9d46ea42005-03-14 23:56:42 +00001101struct phy_info phy_info_M88E1111S = {
1102 0x01410cc,
1103 "Marvell 88E1111S",
1104 4,
Jon Loeliger89875e92006-10-10 17:03:43 -05001105 (struct phy_cmd[]){ /* config */
1106 /* Reset and configure the PHY */
1107 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
Dave Liu19580e62007-09-18 12:37:57 +08001108 {0x1b, 0x848f, &mii_m88e1111s_setmode},
Nick Spencef484dc72006-09-07 07:39:46 -07001109 {0x14, 0x0cd2, NULL}, /* Delay RGMII TX and RX */
Jon Loeliger89875e92006-10-10 17:03:43 -05001110 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1111 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1112 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1113 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1114 {miim_end,}
1115 },
1116 (struct phy_cmd[]){ /* startup */
1117 /* Status is read once to clear old link state */
1118 {MIIM_STATUS, miim_read, NULL},
1119 /* Auto-negotiate */
1120 {MIIM_STATUS, miim_read, &mii_parse_sr},
1121 /* Read the status */
1122 {MIIM_88E1011_PHY_STATUS, miim_read,
1123 &mii_parse_88E1011_psr},
1124 {miim_end,}
1125 },
1126 (struct phy_cmd[]){ /* shutdown */
1127 {miim_end,}
1128 },
wdenk9d46ea42005-03-14 23:56:42 +00001129};
1130
Ron Madrid290ef642008-05-23 15:37:05 -07001131struct phy_info phy_info_M88E1118 = {
1132 0x01410e1,
1133 "Marvell 88E1118",
1134 4,
1135 (struct phy_cmd[]){ /* config */
1136 /* Reset and configure the PHY */
1137 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1138 {0x16, 0x0002, NULL}, /* Change Page Number */
1139 {0x15, 0x1070, NULL}, /* Delay RGMII TX and RX */
1140 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1141 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1142 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1143 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1144 {miim_end,}
1145 },
1146 (struct phy_cmd[]){ /* startup */
1147 {0x16, 0x0000, NULL}, /* Change Page Number */
1148 /* Status is read once to clear old link state */
1149 {MIIM_STATUS, miim_read, NULL},
1150 /* Auto-negotiate */
1151 /* Read the status */
1152 {MIIM_88E1011_PHY_STATUS, miim_read,
1153 &mii_parse_88E1011_psr},
1154 {miim_end,}
1155 },
1156 (struct phy_cmd[]){ /* shutdown */
1157 {miim_end,}
1158 },
1159};
1160
Andy Fleming09f3e092006-09-13 10:34:18 -05001161static unsigned int m88e1145_setmode(uint mii_reg, struct tsec_private *priv)
1162{
Andy Fleming09f3e092006-09-13 10:34:18 -05001163 uint mii_data = read_phy_reg(priv, mii_reg);
1164
Andy Fleming09f3e092006-09-13 10:34:18 -05001165 /* Setting MIIM_88E1145_PHY_EXT_CR */
1166 if (priv->flags & TSEC_REDUCED)
1167 return mii_data |
Jon Loeliger89875e92006-10-10 17:03:43 -05001168 MIIM_M88E1145_RGMII_RX_DELAY | MIIM_M88E1145_RGMII_TX_DELAY;
Andy Fleming09f3e092006-09-13 10:34:18 -05001169 else
1170 return mii_data;
1171}
1172
1173static struct phy_info phy_info_M88E1145 = {
1174 0x01410cd,
1175 "Marvell 88E1145",
1176 4,
Jon Loeliger89875e92006-10-10 17:03:43 -05001177 (struct phy_cmd[]){ /* config */
Andy Fleming7507d562007-05-08 17:23:02 -05001178 /* Reset the PHY */
1179 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1180
Jon Loeliger89875e92006-10-10 17:03:43 -05001181 /* Errata E0, E1 */
1182 {29, 0x001b, NULL},
1183 {30, 0x418f, NULL},
1184 {29, 0x0016, NULL},
1185 {30, 0xa2da, NULL},
Andy Fleming09f3e092006-09-13 10:34:18 -05001186
Andy Fleming7507d562007-05-08 17:23:02 -05001187 /* Configure the PHY */
Jon Loeliger89875e92006-10-10 17:03:43 -05001188 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1189 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1190 {MIIM_88E1011_PHY_SCR, MIIM_88E1011_PHY_MDI_X_AUTO,
1191 NULL},
1192 {MIIM_88E1145_PHY_EXT_CR, 0, &m88e1145_setmode},
1193 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1194 {MIIM_CONTROL, MIIM_CONTROL_INIT, NULL},
1195 {miim_end,}
1196 },
1197 (struct phy_cmd[]){ /* startup */
1198 /* Status is read once to clear old link state */
1199 {MIIM_STATUS, miim_read, NULL},
1200 /* Auto-negotiate */
1201 {MIIM_STATUS, miim_read, &mii_parse_sr},
1202 {MIIM_88E1111_PHY_LED_CONTROL,
1203 MIIM_88E1111_PHY_LED_DIRECT, NULL},
1204 /* Read the Status */
1205 {MIIM_88E1011_PHY_STATUS, miim_read,
1206 &mii_parse_88E1011_psr},
1207 {miim_end,}
1208 },
1209 (struct phy_cmd[]){ /* shutdown */
1210 {miim_end,}
1211 },
Andy Fleming09f3e092006-09-13 10:34:18 -05001212};
1213
wdenk97d80fc2004-06-09 00:34:46 +00001214struct phy_info phy_info_cis8204 = {
1215 0x3f11,
1216 "Cicada Cis8204",
1217 6,
Jon Loeliger89875e92006-10-10 17:03:43 -05001218 (struct phy_cmd[]){ /* config */
1219 /* Override PHY config settings */
1220 {MIIM_CIS8201_AUX_CONSTAT,
1221 MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
1222 /* Configure some basic stuff */
1223 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1224 {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT,
1225 &mii_cis8204_fixled},
1226 {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT,
1227 &mii_cis8204_setmode},
1228 {miim_end,}
1229 },
1230 (struct phy_cmd[]){ /* startup */
1231 /* Read the Status (2x to make sure link is right) */
1232 {MIIM_STATUS, miim_read, NULL},
1233 /* Auto-negotiate */
1234 {MIIM_STATUS, miim_read, &mii_parse_sr},
1235 /* Read the status */
1236 {MIIM_CIS8201_AUX_CONSTAT, miim_read,
1237 &mii_parse_cis8201},
1238 {miim_end,}
1239 },
1240 (struct phy_cmd[]){ /* shutdown */
1241 {miim_end,}
1242 },
wdenk97d80fc2004-06-09 00:34:46 +00001243};
1244
1245/* Cicada 8201 */
1246struct phy_info phy_info_cis8201 = {
1247 0xfc41,
1248 "CIS8201",
1249 4,
Jon Loeliger89875e92006-10-10 17:03:43 -05001250 (struct phy_cmd[]){ /* config */
1251 /* Override PHY config settings */
1252 {MIIM_CIS8201_AUX_CONSTAT,
1253 MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
1254 /* Set up the interface mode */
1255 {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT,
1256 NULL},
1257 /* Configure some basic stuff */
1258 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1259 {miim_end,}
1260 },
1261 (struct phy_cmd[]){ /* startup */
1262 /* Read the Status (2x to make sure link is right) */
1263 {MIIM_STATUS, miim_read, NULL},
1264 /* Auto-negotiate */
1265 {MIIM_STATUS, miim_read, &mii_parse_sr},
1266 /* Read the status */
1267 {MIIM_CIS8201_AUX_CONSTAT, miim_read,
1268 &mii_parse_cis8201},
1269 {miim_end,}
1270 },
1271 (struct phy_cmd[]){ /* shutdown */
1272 {miim_end,}
1273 },
wdenk97d80fc2004-06-09 00:34:46 +00001274};
Jon Loeligerdebb7352006-04-26 17:58:56 -05001275struct phy_info phy_info_VSC8244 = {
Jon Loeliger89875e92006-10-10 17:03:43 -05001276 0x3f1b,
1277 "Vitesse VSC8244",
1278 6,
1279 (struct phy_cmd[]){ /* config */
1280 /* Override PHY config settings */
1281 /* Configure some basic stuff */
1282 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1283 {miim_end,}
1284 },
1285 (struct phy_cmd[]){ /* startup */
1286 /* Read the Status (2x to make sure link is right) */
1287 {MIIM_STATUS, miim_read, NULL},
1288 /* Auto-negotiate */
1289 {MIIM_STATUS, miim_read, &mii_parse_sr},
1290 /* Read the status */
1291 {MIIM_VSC8244_AUX_CONSTAT, miim_read,
1292 &mii_parse_vsc8244},
1293 {miim_end,}
1294 },
1295 (struct phy_cmd[]){ /* shutdown */
1296 {miim_end,}
1297 },
Jon Loeligerdebb7352006-04-26 17:58:56 -05001298};
wdenk97d80fc2004-06-09 00:34:46 +00001299
Tor Krill2d934ea2008-03-28 15:29:45 +01001300struct phy_info phy_info_VSC8601 = {
1301 0x00007042,
1302 "Vitesse VSC8601",
1303 4,
1304 (struct phy_cmd[]){ /* config */
1305 /* Override PHY config settings */
1306 /* Configure some basic stuff */
1307 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1308#ifdef CFG_VSC8601_SKEWFIX
1309 {MIIM_VSC8601_EPHY_CON,MIIM_VSC8601_EPHY_CON_INIT_SKEW,NULL},
Wolfgang Denk7c0773f2008-05-04 00:35:15 +02001310#if defined(CFG_VSC8601_SKEW_TX) && defined(CFG_VSC8601_SKEW_RX)
Andre Schwarz9acde122008-04-29 19:18:32 +02001311 {MIIM_EXT_PAGE_ACCESS,1,NULL},
1312#define VSC8101_SKEW (CFG_VSC8601_SKEW_TX<<14)|(CFG_VSC8601_SKEW_RX<<12)
1313 {MIIM_VSC8601_SKEW_CTRL,VSC8101_SKEW,NULL},
1314 {MIIM_EXT_PAGE_ACCESS,0,NULL},
1315#endif
Tor Krill2d934ea2008-03-28 15:29:45 +01001316#endif
1317 {miim_end,}
1318 },
1319 (struct phy_cmd[]){ /* startup */
1320 /* Read the Status (2x to make sure link is right) */
1321 {MIIM_STATUS, miim_read, NULL},
1322 /* Auto-negotiate */
1323 {MIIM_STATUS, miim_read, &mii_parse_sr},
1324 /* Read the status */
1325 {MIIM_VSC8244_AUX_CONSTAT, miim_read,
1326 &mii_parse_vsc8244},
1327 {miim_end,}
1328 },
1329 (struct phy_cmd[]){ /* shutdown */
1330 {miim_end,}
1331 },
1332};
1333
1334
wdenk97d80fc2004-06-09 00:34:46 +00001335struct phy_info phy_info_dm9161 = {
1336 0x0181b88,
1337 "Davicom DM9161E",
1338 4,
Jon Loeliger89875e92006-10-10 17:03:43 -05001339 (struct phy_cmd[]){ /* config */
1340 {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
1341 /* Do not bypass the scrambler/descrambler */
1342 {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
1343 /* Clear 10BTCSR to default */
1344 {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT,
1345 NULL},
1346 /* Configure some basic stuff */
1347 {MIIM_CONTROL, MIIM_CR_INIT, NULL},
1348 /* Restart Auto Negotiation */
1349 {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
1350 {miim_end,}
1351 },
1352 (struct phy_cmd[]){ /* startup */
1353 /* Status is read once to clear old link state */
1354 {MIIM_STATUS, miim_read, NULL},
1355 /* Auto-negotiate */
1356 {MIIM_STATUS, miim_read, &mii_parse_sr},
1357 /* Read the status */
1358 {MIIM_DM9161_SCSR, miim_read,
1359 &mii_parse_dm9161_scsr},
1360 {miim_end,}
1361 },
1362 (struct phy_cmd[]){ /* shutdown */
1363 {miim_end,}
1364 },
wdenk97d80fc2004-06-09 00:34:46 +00001365};
David Updegraffaf1c2b82007-04-20 14:34:48 -05001366/* a generic flavor. */
1367struct phy_info phy_info_generic = {
1368 0,
1369 "Unknown/Generic PHY",
1370 32,
1371 (struct phy_cmd[]) { /* config */
1372 {PHY_BMCR, PHY_BMCR_RESET, NULL},
1373 {PHY_BMCR, PHY_BMCR_AUTON|PHY_BMCR_RST_NEG, NULL},
1374 {miim_end,}
1375 },
1376 (struct phy_cmd[]) { /* startup */
1377 {PHY_BMSR, miim_read, NULL},
1378 {PHY_BMSR, miim_read, &mii_parse_sr},
1379 {PHY_BMSR, miim_read, &mii_parse_link},
1380 {miim_end,}
1381 },
1382 (struct phy_cmd[]) { /* shutdown */
1383 {miim_end,}
1384 }
1385};
1386
wdenk97d80fc2004-06-09 00:34:46 +00001387
wdenk3dd7f0f2005-04-04 23:43:44 +00001388uint mii_parse_lxt971_sr2(uint mii_reg, struct tsec_private *priv)
1389{
wdenk3c2b3d42005-04-05 23:32:21 +00001390 unsigned int speed;
1391 if (priv->link) {
1392 speed = mii_reg & MIIM_LXT971_SR2_SPEED_MASK;
wdenk3dd7f0f2005-04-04 23:43:44 +00001393
wdenk3c2b3d42005-04-05 23:32:21 +00001394 switch (speed) {
1395 case MIIM_LXT971_SR2_10HDX:
1396 priv->speed = 10;
1397 priv->duplexity = 0;
1398 break;
1399 case MIIM_LXT971_SR2_10FDX:
1400 priv->speed = 10;
1401 priv->duplexity = 1;
1402 break;
1403 case MIIM_LXT971_SR2_100HDX:
1404 priv->speed = 100;
1405 priv->duplexity = 0;
urwithsughosh@gmail.comcd2d1602007-09-10 14:54:56 -04001406 break;
wdenk3c2b3d42005-04-05 23:32:21 +00001407 default:
1408 priv->speed = 100;
1409 priv->duplexity = 1;
wdenk3c2b3d42005-04-05 23:32:21 +00001410 }
1411 } else {
1412 priv->speed = 0;
1413 priv->duplexity = 0;
1414 }
wdenk3dd7f0f2005-04-04 23:43:44 +00001415
wdenk3c2b3d42005-04-05 23:32:21 +00001416 return 0;
wdenk3dd7f0f2005-04-04 23:43:44 +00001417}
1418
wdenk9d46ea42005-03-14 23:56:42 +00001419static struct phy_info phy_info_lxt971 = {
1420 0x0001378e,
1421 "LXT971",
1422 4,
Jon Loeliger89875e92006-10-10 17:03:43 -05001423 (struct phy_cmd[]){ /* config */
1424 {MIIM_CR, MIIM_CR_INIT, mii_cr_init}, /* autonegotiate */
1425 {miim_end,}
1426 },
1427 (struct phy_cmd[]){ /* startup - enable interrupts */
1428 /* { 0x12, 0x00f2, NULL }, */
1429 {MIIM_STATUS, miim_read, NULL},
1430 {MIIM_STATUS, miim_read, &mii_parse_sr},
1431 {MIIM_LXT971_SR2, miim_read, &mii_parse_lxt971_sr2},
1432 {miim_end,}
1433 },
1434 (struct phy_cmd[]){ /* shutdown - disable interrupts */
1435 {miim_end,}
1436 },
wdenk9d46ea42005-03-14 23:56:42 +00001437};
1438
Wolfgang Denkbe5048f2006-03-12 22:50:55 +01001439/* Parse the DP83865's link and auto-neg status register for speed and duplex
Jon Loeliger89875e92006-10-10 17:03:43 -05001440 * information
1441 */
Wolfgang Denkbe5048f2006-03-12 22:50:55 +01001442uint mii_parse_dp83865_lanr(uint mii_reg, struct tsec_private *priv)
1443{
1444 switch (mii_reg & MIIM_DP83865_SPD_MASK) {
1445
1446 case MIIM_DP83865_SPD_1000:
1447 priv->speed = 1000;
1448 break;
1449
1450 case MIIM_DP83865_SPD_100:
1451 priv->speed = 100;
1452 break;
1453
1454 default:
1455 priv->speed = 10;
1456 break;
1457
1458 }
1459
1460 if (mii_reg & MIIM_DP83865_DPX_FULL)
1461 priv->duplexity = 1;
1462 else
1463 priv->duplexity = 0;
1464
1465 return 0;
1466}
1467
1468struct phy_info phy_info_dp83865 = {
1469 0x20005c7,
1470 "NatSemi DP83865",
1471 4,
Jon Loeliger89875e92006-10-10 17:03:43 -05001472 (struct phy_cmd[]){ /* config */
1473 {MIIM_CONTROL, MIIM_DP83865_CR_INIT, NULL},
1474 {miim_end,}
1475 },
1476 (struct phy_cmd[]){ /* startup */
1477 /* Status is read once to clear old link state */
1478 {MIIM_STATUS, miim_read, NULL},
1479 /* Auto-negotiate */
1480 {MIIM_STATUS, miim_read, &mii_parse_sr},
1481 /* Read the link and auto-neg status */
1482 {MIIM_DP83865_LANR, miim_read,
1483 &mii_parse_dp83865_lanr},
1484 {miim_end,}
1485 },
1486 (struct phy_cmd[]){ /* shutdown */
1487 {miim_end,}
1488 },
Wolfgang Denkbe5048f2006-03-12 22:50:55 +01001489};
1490
Dave Liu18ee3202008-01-11 18:45:28 +08001491struct phy_info phy_info_rtl8211b = {
1492 0x001cc91,
1493 "RealTek RTL8211B",
1494 4,
1495 (struct phy_cmd[]){ /* config */
1496 /* Reset and configure the PHY */
1497 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1498 {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
1499 {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
1500 {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
1501 {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
1502 {miim_end,}
1503 },
1504 (struct phy_cmd[]){ /* startup */
1505 /* Status is read once to clear old link state */
1506 {MIIM_STATUS, miim_read, NULL},
1507 /* Auto-negotiate */
1508 {MIIM_STATUS, miim_read, &mii_parse_sr},
1509 /* Read the status */
1510 {MIIM_RTL8211B_PHY_STATUS, miim_read, &mii_parse_RTL8211B_sr},
1511 {miim_end,}
1512 },
1513 (struct phy_cmd[]){ /* shutdown */
1514 {miim_end,}
1515 },
1516};
1517
wdenk97d80fc2004-06-09 00:34:46 +00001518struct phy_info *phy_info[] = {
wdenk97d80fc2004-06-09 00:34:46 +00001519 &phy_info_cis8204,
Timur Tabi2ad6b512006-10-31 18:44:42 -06001520 &phy_info_cis8201,
Paul Gortmaker91e25762007-01-16 11:38:14 -05001521 &phy_info_BCM5461S,
Joe Hammanc3243cf2007-04-30 16:47:28 -05001522 &phy_info_BCM5464S,
wdenk97d80fc2004-06-09 00:34:46 +00001523 &phy_info_M88E1011S,
wdenk9d46ea42005-03-14 23:56:42 +00001524 &phy_info_M88E1111S,
Ron Madrid290ef642008-05-23 15:37:05 -07001525 &phy_info_M88E1118,
Andy Fleming09f3e092006-09-13 10:34:18 -05001526 &phy_info_M88E1145,
Wolfgang Denk5728be32007-08-06 01:01:49 +02001527 &phy_info_M88E1149S,
wdenk97d80fc2004-06-09 00:34:46 +00001528 &phy_info_dm9161,
wdenk9d46ea42005-03-14 23:56:42 +00001529 &phy_info_lxt971,
Jon Loeligerdebb7352006-04-26 17:58:56 -05001530 &phy_info_VSC8244,
Tor Krill2d934ea2008-03-28 15:29:45 +01001531 &phy_info_VSC8601,
Wolfgang Denkbe5048f2006-03-12 22:50:55 +01001532 &phy_info_dp83865,
Dave Liu18ee3202008-01-11 18:45:28 +08001533 &phy_info_rtl8211b,
David Updegraffaf1c2b82007-04-20 14:34:48 -05001534 &phy_info_generic,
wdenk97d80fc2004-06-09 00:34:46 +00001535 NULL
1536};
1537
wdenk97d80fc2004-06-09 00:34:46 +00001538/* Grab the identifier of the device's PHY, and search through
wdenk9d46ea42005-03-14 23:56:42 +00001539 * all of the known PHYs to see if one matches. If so, return
Jon Loeliger89875e92006-10-10 17:03:43 -05001540 * it, if not, return NULL
1541 */
1542struct phy_info *get_phy_info(struct eth_device *dev)
wdenk97d80fc2004-06-09 00:34:46 +00001543{
1544 struct tsec_private *priv = (struct tsec_private *)dev->priv;
1545 uint phy_reg, phy_ID;
1546 int i;
1547 struct phy_info *theInfo = NULL;
1548
1549 /* Grab the bits from PHYIR1, and put them in the upper half */
1550 phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
1551 phy_ID = (phy_reg & 0xffff) << 16;
1552
1553 /* Grab the bits from PHYIR2, and put them in the lower half */
1554 phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
1555 phy_ID |= (phy_reg & 0xffff);
1556
1557 /* loop through all the known PHY types, and find one that */
1558 /* matches the ID we read from the PHY. */
Jon Loeliger89875e92006-10-10 17:03:43 -05001559 for (i = 0; phy_info[i]; i++) {
Andy Fleming2a3cee42007-05-09 00:54:20 -05001560 if (phy_info[i]->id == (phy_ID >> phy_info[i]->shift)) {
wdenk97d80fc2004-06-09 00:34:46 +00001561 theInfo = phy_info[i];
Andy Fleming2a3cee42007-05-09 00:54:20 -05001562 break;
1563 }
wdenk97d80fc2004-06-09 00:34:46 +00001564 }
1565
Jon Loeliger89875e92006-10-10 17:03:43 -05001566 if (theInfo == NULL) {
wdenk97d80fc2004-06-09 00:34:46 +00001567 printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
1568 return NULL;
1569 } else {
Stefan Roese5810dc32005-09-21 18:20:22 +02001570 debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID);
wdenk97d80fc2004-06-09 00:34:46 +00001571 }
1572
1573 return theInfo;
1574}
1575
wdenk97d80fc2004-06-09 00:34:46 +00001576/* Execute the given series of commands on the given device's
Jon Loeliger89875e92006-10-10 17:03:43 -05001577 * PHY, running functions as necessary
1578 */
wdenk97d80fc2004-06-09 00:34:46 +00001579void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
1580{
1581 int i;
1582 uint result;
1583 volatile tsec_t *phyregs = priv->phyregs;
1584
1585 phyregs->miimcfg = MIIMCFG_RESET;
1586
1587 phyregs->miimcfg = MIIMCFG_INIT_VALUE;
1588
Jon Loeliger89875e92006-10-10 17:03:43 -05001589 while (phyregs->miimind & MIIMIND_BUSY) ;
wdenk97d80fc2004-06-09 00:34:46 +00001590
Jon Loeliger89875e92006-10-10 17:03:43 -05001591 for (i = 0; cmd->mii_reg != miim_end; i++) {
1592 if (cmd->mii_data == miim_read) {
wdenk97d80fc2004-06-09 00:34:46 +00001593 result = read_phy_reg(priv, cmd->mii_reg);
1594
Jon Loeliger89875e92006-10-10 17:03:43 -05001595 if (cmd->funct != NULL)
1596 (*(cmd->funct)) (result, priv);
wdenk97d80fc2004-06-09 00:34:46 +00001597
1598 } else {
Jon Loeliger89875e92006-10-10 17:03:43 -05001599 if (cmd->funct != NULL)
1600 result = (*(cmd->funct)) (cmd->mii_reg, priv);
wdenk97d80fc2004-06-09 00:34:46 +00001601 else
1602 result = cmd->mii_data;
1603
1604 write_phy_reg(priv, cmd->mii_reg, result);
1605
1606 }
1607 cmd++;
1608 }
1609}
1610
wdenk97d80fc2004-06-09 00:34:46 +00001611/* Relocate the function pointers in the phy cmd lists */
1612static void relocate_cmds(void)
1613{
1614 struct phy_cmd **cmdlistptr;
1615 struct phy_cmd *cmd;
Jon Loeliger89875e92006-10-10 17:03:43 -05001616 int i, j, k;
wdenk97d80fc2004-06-09 00:34:46 +00001617
Jon Loeliger89875e92006-10-10 17:03:43 -05001618 for (i = 0; phy_info[i]; i++) {
wdenk97d80fc2004-06-09 00:34:46 +00001619 /* First thing's first: relocate the pointers to the
1620 * PHY command structures (the structs were done) */
Jon Loeliger89875e92006-10-10 17:03:43 -05001621 phy_info[i] = (struct phy_info *)((uint) phy_info[i]
1622 + gd->reloc_off);
wdenk97d80fc2004-06-09 00:34:46 +00001623 phy_info[i]->name += gd->reloc_off;
1624 phy_info[i]->config =
Jon Loeliger89875e92006-10-10 17:03:43 -05001625 (struct phy_cmd *)((uint) phy_info[i]->config
1626 + gd->reloc_off);
wdenk97d80fc2004-06-09 00:34:46 +00001627 phy_info[i]->startup =
Jon Loeliger89875e92006-10-10 17:03:43 -05001628 (struct phy_cmd *)((uint) phy_info[i]->startup
1629 + gd->reloc_off);
wdenk97d80fc2004-06-09 00:34:46 +00001630 phy_info[i]->shutdown =
Jon Loeliger89875e92006-10-10 17:03:43 -05001631 (struct phy_cmd *)((uint) phy_info[i]->shutdown
1632 + gd->reloc_off);
wdenk97d80fc2004-06-09 00:34:46 +00001633
1634 cmdlistptr = &phy_info[i]->config;
Jon Loeliger89875e92006-10-10 17:03:43 -05001635 j = 0;
1636 for (; cmdlistptr <= &phy_info[i]->shutdown; cmdlistptr++) {
1637 k = 0;
1638 for (cmd = *cmdlistptr;
1639 cmd->mii_reg != miim_end;
1640 cmd++) {
wdenk97d80fc2004-06-09 00:34:46 +00001641 /* Only relocate non-NULL pointers */
Jon Loeliger89875e92006-10-10 17:03:43 -05001642 if (cmd->funct)
wdenk97d80fc2004-06-09 00:34:46 +00001643 cmd->funct += gd->reloc_off;
1644
1645 k++;
1646 }
1647 j++;
1648 }
1649 }
1650
1651 relocated = 1;
1652}
1653
Jon Loeligercb51c0b2007-07-09 17:39:42 -05001654#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001655 && !defined(BITBANGMII)
wdenk97d80fc2004-06-09 00:34:46 +00001656
wdenk7abf0c52004-04-18 21:45:42 +00001657/*
1658 * Read a MII PHY register.
1659 *
1660 * Returns:
wdenk97d80fc2004-06-09 00:34:46 +00001661 * 0 on success
wdenk7abf0c52004-04-18 21:45:42 +00001662 */
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001663static int tsec_miiphy_read(char *devname, unsigned char addr,
Jon Loeliger89875e92006-10-10 17:03:43 -05001664 unsigned char reg, unsigned short *value)
wdenk7abf0c52004-04-18 21:45:42 +00001665{
wdenk97d80fc2004-06-09 00:34:46 +00001666 unsigned short ret;
michael.firth@bt.com55fe7c52008-01-16 11:40:51 +00001667 struct tsec_private *priv = privlist[0];
wdenk7abf0c52004-04-18 21:45:42 +00001668
Jon Loeliger89875e92006-10-10 17:03:43 -05001669 if (NULL == priv) {
wdenk97d80fc2004-06-09 00:34:46 +00001670 printf("Can't read PHY at address %d\n", addr);
1671 return -1;
1672 }
1673
michael.firth@bt.com55fe7c52008-01-16 11:40:51 +00001674 ret = (unsigned short)read_any_phy_reg(priv, addr, reg);
wdenk97d80fc2004-06-09 00:34:46 +00001675 *value = ret;
wdenk7abf0c52004-04-18 21:45:42 +00001676
1677 return 0;
1678}
1679
1680/*
1681 * Write a MII PHY register.
1682 *
1683 * Returns:
wdenk97d80fc2004-06-09 00:34:46 +00001684 * 0 on success
wdenk7abf0c52004-04-18 21:45:42 +00001685 */
Marian Balakowicz63ff0042005-10-28 22:30:33 +02001686static int tsec_miiphy_write(char *devname, unsigned char addr,
Jon Loeliger89875e92006-10-10 17:03:43 -05001687 unsigned char reg, unsigned short value)
wdenk7abf0c52004-04-18 21:45:42 +00001688{
michael.firth@bt.com55fe7c52008-01-16 11:40:51 +00001689 struct tsec_private *priv = privlist[0];
wdenk7abf0c52004-04-18 21:45:42 +00001690
Jon Loeliger89875e92006-10-10 17:03:43 -05001691 if (NULL == priv) {
wdenk97d80fc2004-06-09 00:34:46 +00001692 printf("Can't write PHY at address %d\n", addr);
1693 return -1;
1694 }
1695
michael.firth@bt.com55fe7c52008-01-16 11:40:51 +00001696 write_any_phy_reg(priv, addr, reg, value);
wdenk7abf0c52004-04-18 21:45:42 +00001697
1698 return 0;
1699}
wdenk97d80fc2004-06-09 00:34:46 +00001700
Jon Loeligercb51c0b2007-07-09 17:39:42 -05001701#endif
wdenk97d80fc2004-06-09 00:34:46 +00001702
David Updegraff53a5c422007-06-11 10:41:07 -05001703#ifdef CONFIG_MCAST_TFTP
1704
1705/* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
1706
1707/* Set the appropriate hash bit for the given addr */
1708
1709/* The algorithm works like so:
1710 * 1) Take the Destination Address (ie the multicast address), and
1711 * do a CRC on it (little endian), and reverse the bits of the
1712 * result.
1713 * 2) Use the 8 most significant bits as a hash into a 256-entry
1714 * table. The table is controlled through 8 32-bit registers:
1715 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
1716 * gaddr7. This means that the 3 most significant bits in the
1717 * hash index which gaddr register to use, and the 5 other bits
1718 * indicate which bit (assuming an IBM numbering scheme, which
1719 * for PowerPC (tm) is usually the case) in the tregister holds
1720 * the entry. */
1721static int
1722tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set)
1723{
1724 struct tsec_private *priv = privlist[1];
1725 volatile tsec_t *regs = priv->regs;
1726 volatile u32 *reg_array, value;
1727 u8 result, whichbit, whichreg;
1728
1729 result = (u8)((ether_crc(MAC_ADDR_LEN,mcast_mac) >> 24) & 0xff);
1730 whichbit = result & 0x1f; /* the 5 LSB = which bit to set */
1731 whichreg = result >> 5; /* the 3 MSB = which reg to set it in */
1732 value = (1 << (31-whichbit));
1733
1734 reg_array = &(regs->hash.gaddr0);
1735
1736 if (set) {
1737 reg_array[whichreg] |= value;
1738 } else {
1739 reg_array[whichreg] &= ~value;
1740 }
1741 return 0;
1742}
1743#endif /* Multicast TFTP ? */
1744
wdenk42d1f032003-10-15 23:53:47 +00001745#endif /* CONFIG_TSEC_ENET */