blob: c549b6bb99cc39b47e3d12fb1b77885c879ef496 [file] [log] [blame]
Dave Liu7737d5c2006-11-03 12:11:15 -06001/*
2 * Copyright (C) 2005 Freescale Semiconductor, Inc.
3 *
4 * Author: Shlomi Gridish
5 *
6 * Description: UCC GETH Driver -- PHY handling
Wolfgang Denkdd520bf2006-11-30 18:02:20 +01007 * Driver for UEC on QE
8 * Based on 8260_io/fcc_enet.c
Dave Liu7737d5c2006-11-03 12:11:15 -06009 *
Wolfgang Denkdd520bf2006-11-30 18:02:20 +010010 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
Dave Liu7737d5c2006-11-03 12:11:15 -060012 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
16
17#include "common.h"
18#include "net.h"
19#include "malloc.h"
20#include "asm/errno.h"
21#include "asm/immap_qe.h"
22#include "asm/io.h"
23#include "qe.h"
24#include "uccf.h"
25#include "uec.h"
26#include "uec_phy.h"
27#include "miiphy.h"
28
29#if defined(CONFIG_QE)
30
Dave Liu7737d5c2006-11-03 12:11:15 -060031#define ugphy_printk(format, arg...) \
Wolfgang Denkdd520bf2006-11-30 18:02:20 +010032 printf(format "\n", ## arg)
Dave Liu7737d5c2006-11-03 12:11:15 -060033
Wolfgang Denkdd520bf2006-11-30 18:02:20 +010034#define ugphy_dbg(format, arg...) \
35 ugphy_printk(format , ## arg)
36#define ugphy_err(format, arg...) \
37 ugphy_printk(format , ## arg)
38#define ugphy_info(format, arg...) \
39 ugphy_printk(format , ## arg)
40#define ugphy_warn(format, arg...) \
41 ugphy_printk(format , ## arg)
Dave Liu7737d5c2006-11-03 12:11:15 -060042
43#ifdef UEC_VERBOSE_DEBUG
44#define ugphy_vdbg ugphy_dbg
45#else
46#define ugphy_vdbg(ugeth, fmt, args...) do { } while (0)
47#endif /* UEC_VERBOSE_DEBUG */
48
Wolfgang Denkdd520bf2006-11-30 18:02:20 +010049static void config_genmii_advert (struct uec_mii_info *mii_info);
50static void genmii_setup_forced (struct uec_mii_info *mii_info);
51static void genmii_restart_aneg (struct uec_mii_info *mii_info);
52static int gbit_config_aneg (struct uec_mii_info *mii_info);
53static int genmii_config_aneg (struct uec_mii_info *mii_info);
54static int genmii_update_link (struct uec_mii_info *mii_info);
55static int genmii_read_status (struct uec_mii_info *mii_info);
56u16 phy_read (struct uec_mii_info *mii_info, u16 regnum);
57void phy_write (struct uec_mii_info *mii_info, u16 regnum, u16 val);
Dave Liu7737d5c2006-11-03 12:11:15 -060058
59/* Write value to the PHY for this device to the register at regnum, */
60/* waiting until the write is done before it returns. All PHY */
61/* configuration has to be done through the TSEC1 MIIM regs */
Andy Flemingda9d4612007-08-14 00:14:25 -050062void uec_write_phy_reg (struct eth_device *dev, int mii_id, int regnum, int value)
Dave Liu7737d5c2006-11-03 12:11:15 -060063{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +010064 uec_private_t *ugeth = (uec_private_t *) dev->priv;
Andy Flemingda9d4612007-08-14 00:14:25 -050065 uec_mii_t *ug_regs;
Wolfgang Denkdd520bf2006-11-30 18:02:20 +010066 enet_tbi_mii_reg_e mii_reg = (enet_tbi_mii_reg_e) regnum;
67 u32 tmp_reg;
Dave Liu7737d5c2006-11-03 12:11:15 -060068
Andy Flemingda9d4612007-08-14 00:14:25 -050069 ug_regs = ugeth->uec_mii_regs;
Dave Liu7737d5c2006-11-03 12:11:15 -060070
Wolfgang Denkdd520bf2006-11-30 18:02:20 +010071 /* Stop the MII management read cycle */
72 out_be32 (&ug_regs->miimcom, 0);
73 /* Setting up the MII Mangement Address Register */
74 tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
75 out_be32 (&ug_regs->miimadd, tmp_reg);
Dave Liu7737d5c2006-11-03 12:11:15 -060076
Wolfgang Denkdd520bf2006-11-30 18:02:20 +010077 /* Setting up the MII Mangement Control Register with the value */
78 out_be32 (&ug_regs->miimcon, (u32) value);
Kim Phillipsee62ed32008-01-15 14:11:00 -060079 sync();
Dave Liu7737d5c2006-11-03 12:11:15 -060080
Wolfgang Denkdd520bf2006-11-30 18:02:20 +010081 /* Wait till MII management write is complete */
82 while ((in_be32 (&ug_regs->miimind)) & MIIMIND_BUSY);
Dave Liu7737d5c2006-11-03 12:11:15 -060083}
84
85/* Reads from register regnum in the PHY for device dev, */
86/* returning the value. Clears miimcom first. All PHY */
87/* configuration has to be done through the TSEC1 MIIM regs */
Andy Flemingda9d4612007-08-14 00:14:25 -050088int uec_read_phy_reg (struct eth_device *dev, int mii_id, int regnum)
Dave Liu7737d5c2006-11-03 12:11:15 -060089{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +010090 uec_private_t *ugeth = (uec_private_t *) dev->priv;
Andy Flemingda9d4612007-08-14 00:14:25 -050091 uec_mii_t *ug_regs;
Wolfgang Denkdd520bf2006-11-30 18:02:20 +010092 enet_tbi_mii_reg_e mii_reg = (enet_tbi_mii_reg_e) regnum;
93 u32 tmp_reg;
94 u16 value;
Dave Liu7737d5c2006-11-03 12:11:15 -060095
Andy Flemingda9d4612007-08-14 00:14:25 -050096 ug_regs = ugeth->uec_mii_regs;
Dave Liu7737d5c2006-11-03 12:11:15 -060097
Wolfgang Denkdd520bf2006-11-30 18:02:20 +010098 /* Setting up the MII Mangement Address Register */
99 tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
100 out_be32 (&ug_regs->miimadd, tmp_reg);
Dave Liu7737d5c2006-11-03 12:11:15 -0600101
Kim Phillipsee62ed32008-01-15 14:11:00 -0600102 /* clear MII management command cycle */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100103 out_be32 (&ug_regs->miimcom, 0);
Kim Phillipsee62ed32008-01-15 14:11:00 -0600104 sync();
105
106 /* Perform an MII management read cycle */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100107 out_be32 (&ug_regs->miimcom, MIIMCOM_READ_CYCLE);
Dave Liu7737d5c2006-11-03 12:11:15 -0600108
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100109 /* Wait till MII management write is complete */
110 while ((in_be32 (&ug_regs->miimind)) &
111 (MIIMIND_NOT_VALID | MIIMIND_BUSY));
Dave Liu7737d5c2006-11-03 12:11:15 -0600112
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100113 /* Read MII management status */
114 value = (u16) in_be32 (&ug_regs->miimstat);
115 if (value == 0xffff)
Joakim Tjernlund84a30472008-01-16 09:40:41 +0100116 ugphy_vdbg
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100117 ("read wrong value : mii_id %d,mii_reg %d, base %08x",
118 mii_id, mii_reg, (u32) & (ug_regs->miimcfg));
Dave Liu7737d5c2006-11-03 12:11:15 -0600119
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100120 return (value);
Dave Liu7737d5c2006-11-03 12:11:15 -0600121}
122
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100123void mii_clear_phy_interrupt (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600124{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100125 if (mii_info->phyinfo->ack_interrupt)
126 mii_info->phyinfo->ack_interrupt (mii_info);
Dave Liu7737d5c2006-11-03 12:11:15 -0600127}
128
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100129void mii_configure_phy_interrupt (struct uec_mii_info *mii_info,
130 u32 interrupts)
Dave Liu7737d5c2006-11-03 12:11:15 -0600131{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100132 mii_info->interrupts = interrupts;
133 if (mii_info->phyinfo->config_intr)
134 mii_info->phyinfo->config_intr (mii_info);
Dave Liu7737d5c2006-11-03 12:11:15 -0600135}
136
137/* Writes MII_ADVERTISE with the appropriate values, after
138 * sanitizing advertise to make sure only supported features
139 * are advertised
140 */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100141static void config_genmii_advert (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600142{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100143 u32 advertise;
144 u16 adv;
Dave Liu7737d5c2006-11-03 12:11:15 -0600145
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100146 /* Only allow advertising what this PHY supports */
147 mii_info->advertising &= mii_info->phyinfo->features;
148 advertise = mii_info->advertising;
Dave Liu7737d5c2006-11-03 12:11:15 -0600149
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100150 /* Setup standard advertisement */
151 adv = phy_read (mii_info, PHY_ANAR);
152 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
153 if (advertise & ADVERTISED_10baseT_Half)
154 adv |= ADVERTISE_10HALF;
155 if (advertise & ADVERTISED_10baseT_Full)
156 adv |= ADVERTISE_10FULL;
157 if (advertise & ADVERTISED_100baseT_Half)
158 adv |= ADVERTISE_100HALF;
159 if (advertise & ADVERTISED_100baseT_Full)
160 adv |= ADVERTISE_100FULL;
161 phy_write (mii_info, PHY_ANAR, adv);
Dave Liu7737d5c2006-11-03 12:11:15 -0600162}
163
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100164static void genmii_setup_forced (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600165{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100166 u16 ctrl;
167 u32 features = mii_info->phyinfo->features;
Dave Liu7737d5c2006-11-03 12:11:15 -0600168
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100169 ctrl = phy_read (mii_info, PHY_BMCR);
Dave Liu7737d5c2006-11-03 12:11:15 -0600170
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100171 ctrl &= ~(PHY_BMCR_DPLX | PHY_BMCR_100_MBPS |
172 PHY_BMCR_1000_MBPS | PHY_BMCR_AUTON);
173 ctrl |= PHY_BMCR_RESET;
Dave Liu7737d5c2006-11-03 12:11:15 -0600174
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100175 switch (mii_info->speed) {
176 case SPEED_1000:
177 if (features & (SUPPORTED_1000baseT_Half
178 | SUPPORTED_1000baseT_Full)) {
179 ctrl |= PHY_BMCR_1000_MBPS;
180 break;
181 }
182 mii_info->speed = SPEED_100;
183 case SPEED_100:
184 if (features & (SUPPORTED_100baseT_Half
185 | SUPPORTED_100baseT_Full)) {
186 ctrl |= PHY_BMCR_100_MBPS;
187 break;
188 }
189 mii_info->speed = SPEED_10;
190 case SPEED_10:
191 if (features & (SUPPORTED_10baseT_Half
192 | SUPPORTED_10baseT_Full))
193 break;
194 default: /* Unsupported speed! */
195 ugphy_err ("%s: Bad speed!", mii_info->dev->name);
196 break;
197 }
Dave Liu7737d5c2006-11-03 12:11:15 -0600198
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100199 phy_write (mii_info, PHY_BMCR, ctrl);
Dave Liu7737d5c2006-11-03 12:11:15 -0600200}
201
202/* Enable and Restart Autonegotiation */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100203static void genmii_restart_aneg (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600204{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100205 u16 ctl;
Dave Liu7737d5c2006-11-03 12:11:15 -0600206
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100207 ctl = phy_read (mii_info, PHY_BMCR);
208 ctl |= (PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
209 phy_write (mii_info, PHY_BMCR, ctl);
Dave Liu7737d5c2006-11-03 12:11:15 -0600210}
211
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100212static int gbit_config_aneg (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600213{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100214 u16 adv;
215 u32 advertise;
Dave Liu7737d5c2006-11-03 12:11:15 -0600216
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100217 if (mii_info->autoneg) {
218 /* Configure the ADVERTISE register */
219 config_genmii_advert (mii_info);
220 advertise = mii_info->advertising;
Dave Liu7737d5c2006-11-03 12:11:15 -0600221
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100222 adv = phy_read (mii_info, MII_1000BASETCONTROL);
223 adv &= ~(MII_1000BASETCONTROL_FULLDUPLEXCAP |
224 MII_1000BASETCONTROL_HALFDUPLEXCAP);
225 if (advertise & SUPPORTED_1000baseT_Half)
226 adv |= MII_1000BASETCONTROL_HALFDUPLEXCAP;
227 if (advertise & SUPPORTED_1000baseT_Full)
228 adv |= MII_1000BASETCONTROL_FULLDUPLEXCAP;
229 phy_write (mii_info, MII_1000BASETCONTROL, adv);
Dave Liu7737d5c2006-11-03 12:11:15 -0600230
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100231 /* Start/Restart aneg */
232 genmii_restart_aneg (mii_info);
233 } else
234 genmii_setup_forced (mii_info);
Dave Liu7737d5c2006-11-03 12:11:15 -0600235
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100236 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600237}
238
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100239static int marvell_config_aneg (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600240{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100241 /* The Marvell PHY has an errata which requires
242 * that certain registers get written in order
243 * to restart autonegotiation */
244 phy_write (mii_info, PHY_BMCR, PHY_BMCR_RESET);
Dave Liu7737d5c2006-11-03 12:11:15 -0600245
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100246 phy_write (mii_info, 0x1d, 0x1f);
247 phy_write (mii_info, 0x1e, 0x200c);
248 phy_write (mii_info, 0x1d, 0x5);
249 phy_write (mii_info, 0x1e, 0);
250 phy_write (mii_info, 0x1e, 0x100);
Dave Liu7737d5c2006-11-03 12:11:15 -0600251
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100252 gbit_config_aneg (mii_info);
Dave Liu7737d5c2006-11-03 12:11:15 -0600253
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100254 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600255}
256
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100257static int genmii_config_aneg (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600258{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100259 if (mii_info->autoneg) {
260 config_genmii_advert (mii_info);
261 genmii_restart_aneg (mii_info);
262 } else
263 genmii_setup_forced (mii_info);
Dave Liu7737d5c2006-11-03 12:11:15 -0600264
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100265 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600266}
267
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100268static int genmii_update_link (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600269{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100270 u16 status;
Dave Liu7737d5c2006-11-03 12:11:15 -0600271
Kim Phillipsee62ed32008-01-15 14:11:00 -0600272 /* Status is read once to clear old link state */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100273 phy_read (mii_info, PHY_BMSR);
Dave Liu7737d5c2006-11-03 12:11:15 -0600274
Kim Phillipsee62ed32008-01-15 14:11:00 -0600275 /*
276 * Wait if the link is up, and autonegotiation is in progress
277 * (ie - we're capable and it's not done)
278 */
279 status = phy_read(mii_info, PHY_BMSR);
280 if ((status & PHY_BMSR_LS) && (status & PHY_BMSR_AUTN_ABLE)
281 && !(status & PHY_BMSR_AUTN_COMP)) {
282 int i = 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600283
Kim Phillipsee62ed32008-01-15 14:11:00 -0600284 while (!(status & PHY_BMSR_AUTN_COMP)) {
285 /*
286 * Timeout reached ?
287 */
288 if (i > UGETH_AN_TIMEOUT) {
289 mii_info->link = 0;
290 return 0;
291 }
292
293 udelay(1000); /* 1 ms */
294 status = phy_read(mii_info, PHY_BMSR);
295 }
296 mii_info->link = 1;
297 udelay(500000); /* another 500 ms (results in faster booting) */
298 } else {
299 if (status & PHY_BMSR_LS)
300 mii_info->link = 1;
301 else
302 mii_info->link = 0;
303 }
Dave Liu7737d5c2006-11-03 12:11:15 -0600304
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100305 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600306}
307
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100308static int genmii_read_status (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600309{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100310 u16 status;
311 int err;
Dave Liu7737d5c2006-11-03 12:11:15 -0600312
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100313 /* Update the link, but return if there
314 * was an error */
315 err = genmii_update_link (mii_info);
316 if (err)
317 return err;
Dave Liu7737d5c2006-11-03 12:11:15 -0600318
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100319 if (mii_info->autoneg) {
320 status = phy_read (mii_info, PHY_ANLPAR);
Dave Liu7737d5c2006-11-03 12:11:15 -0600321
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100322 if (status & (PHY_ANLPAR_10FD | PHY_ANLPAR_TXFD))
323 mii_info->duplex = DUPLEX_FULL;
324 else
325 mii_info->duplex = DUPLEX_HALF;
326 if (status & (PHY_ANLPAR_TXFD | PHY_ANLPAR_TX))
327 mii_info->speed = SPEED_100;
328 else
329 mii_info->speed = SPEED_10;
330 mii_info->pause = 0;
331 }
332 /* On non-aneg, we assume what we put in BMCR is the speed,
333 * though magic-aneg shouldn't prevent this case from occurring
334 */
Dave Liu7737d5c2006-11-03 12:11:15 -0600335
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100336 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600337}
338
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100339static int marvell_read_status (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600340{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100341 u16 status;
342 int err;
Dave Liu7737d5c2006-11-03 12:11:15 -0600343
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100344 /* Update the link, but return if there
345 * was an error */
346 err = genmii_update_link (mii_info);
347 if (err)
348 return err;
Dave Liu7737d5c2006-11-03 12:11:15 -0600349
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100350 /* If the link is up, read the speed and duplex */
351 /* If we aren't autonegotiating, assume speeds
352 * are as set */
353 if (mii_info->autoneg && mii_info->link) {
354 int speed;
Dave Liu7737d5c2006-11-03 12:11:15 -0600355
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100356 status = phy_read (mii_info, MII_M1011_PHY_SPEC_STATUS);
Dave Liu7737d5c2006-11-03 12:11:15 -0600357
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100358 /* Get the duplexity */
359 if (status & MII_M1011_PHY_SPEC_STATUS_FULLDUPLEX)
360 mii_info->duplex = DUPLEX_FULL;
361 else
362 mii_info->duplex = DUPLEX_HALF;
Dave Liu7737d5c2006-11-03 12:11:15 -0600363
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100364 /* Get the speed */
365 speed = status & MII_M1011_PHY_SPEC_STATUS_SPD_MASK;
366 switch (speed) {
367 case MII_M1011_PHY_SPEC_STATUS_1000:
368 mii_info->speed = SPEED_1000;
369 break;
370 case MII_M1011_PHY_SPEC_STATUS_100:
371 mii_info->speed = SPEED_100;
372 break;
373 default:
374 mii_info->speed = SPEED_10;
375 break;
376 }
377 mii_info->pause = 0;
378 }
379
380 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600381}
382
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100383static int marvell_ack_interrupt (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600384{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100385 /* Clear the interrupts by reading the reg */
386 phy_read (mii_info, MII_M1011_IEVENT);
Dave Liu7737d5c2006-11-03 12:11:15 -0600387
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100388 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600389}
390
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100391static int marvell_config_intr (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600392{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100393 if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
394 phy_write (mii_info, MII_M1011_IMASK, MII_M1011_IMASK_INIT);
395 else
396 phy_write (mii_info, MII_M1011_IMASK, MII_M1011_IMASK_CLEAR);
Dave Liu7737d5c2006-11-03 12:11:15 -0600397
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100398 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600399}
400
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100401static int dm9161_init (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600402{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100403 /* Reset the PHY */
404 phy_write (mii_info, PHY_BMCR, phy_read (mii_info, PHY_BMCR) |
405 PHY_BMCR_RESET);
406 /* PHY and MAC connect */
407 phy_write (mii_info, PHY_BMCR, phy_read (mii_info, PHY_BMCR) &
408 ~PHY_BMCR_ISO);
Kim Phillipsee62ed32008-01-15 14:11:00 -0600409
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100410 phy_write (mii_info, MII_DM9161_SCR, MII_DM9161_SCR_INIT);
Kim Phillipsee62ed32008-01-15 14:11:00 -0600411
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100412 config_genmii_advert (mii_info);
413 /* Start/restart aneg */
414 genmii_config_aneg (mii_info);
Dave Liu7737d5c2006-11-03 12:11:15 -0600415
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100416 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600417}
418
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100419static int dm9161_config_aneg (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600420{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100421 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600422}
423
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100424static int dm9161_read_status (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600425{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100426 u16 status;
427 int err;
Dave Liu7737d5c2006-11-03 12:11:15 -0600428
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100429 /* Update the link, but return if there was an error */
430 err = genmii_update_link (mii_info);
431 if (err)
432 return err;
433 /* If the link is up, read the speed and duplex
434 If we aren't autonegotiating assume speeds are as set */
435 if (mii_info->autoneg && mii_info->link) {
436 status = phy_read (mii_info, MII_DM9161_SCSR);
437 if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_100H))
438 mii_info->speed = SPEED_100;
439 else
440 mii_info->speed = SPEED_10;
Dave Liu7737d5c2006-11-03 12:11:15 -0600441
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100442 if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_10F))
443 mii_info->duplex = DUPLEX_FULL;
444 else
445 mii_info->duplex = DUPLEX_HALF;
446 }
Dave Liu7737d5c2006-11-03 12:11:15 -0600447
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100448 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600449}
450
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100451static int dm9161_ack_interrupt (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600452{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100453 /* Clear the interrupt by reading the reg */
454 phy_read (mii_info, MII_DM9161_INTR);
Dave Liu7737d5c2006-11-03 12:11:15 -0600455
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100456 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600457}
458
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100459static int dm9161_config_intr (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600460{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100461 if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
462 phy_write (mii_info, MII_DM9161_INTR, MII_DM9161_INTR_INIT);
463 else
464 phy_write (mii_info, MII_DM9161_INTR, MII_DM9161_INTR_STOP);
Dave Liu7737d5c2006-11-03 12:11:15 -0600465
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100466 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600467}
468
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100469static void dm9161_close (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600470{
471}
472
473static struct phy_info phy_info_dm9161 = {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100474 .phy_id = 0x0181b880,
475 .phy_id_mask = 0x0ffffff0,
476 .name = "Davicom DM9161E",
477 .init = dm9161_init,
478 .config_aneg = dm9161_config_aneg,
479 .read_status = dm9161_read_status,
480 .close = dm9161_close,
Dave Liu7737d5c2006-11-03 12:11:15 -0600481};
482
483static struct phy_info phy_info_dm9161a = {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100484 .phy_id = 0x0181b8a0,
485 .phy_id_mask = 0x0ffffff0,
486 .name = "Davicom DM9161A",
487 .features = MII_BASIC_FEATURES,
488 .init = dm9161_init,
489 .config_aneg = dm9161_config_aneg,
490 .read_status = dm9161_read_status,
491 .ack_interrupt = dm9161_ack_interrupt,
492 .config_intr = dm9161_config_intr,
493 .close = dm9161_close,
Dave Liu7737d5c2006-11-03 12:11:15 -0600494};
495
496static struct phy_info phy_info_marvell = {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100497 .phy_id = 0x01410c00,
498 .phy_id_mask = 0xffffff00,
499 .name = "Marvell 88E11x1",
500 .features = MII_GBIT_FEATURES,
501 .config_aneg = &marvell_config_aneg,
502 .read_status = &marvell_read_status,
503 .ack_interrupt = &marvell_ack_interrupt,
504 .config_intr = &marvell_config_intr,
Dave Liu7737d5c2006-11-03 12:11:15 -0600505};
506
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100507static struct phy_info phy_info_genmii = {
508 .phy_id = 0x00000000,
509 .phy_id_mask = 0x00000000,
510 .name = "Generic MII",
511 .features = MII_BASIC_FEATURES,
512 .config_aneg = genmii_config_aneg,
513 .read_status = genmii_read_status,
Dave Liu7737d5c2006-11-03 12:11:15 -0600514};
515
516static struct phy_info *phy_info[] = {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100517 &phy_info_dm9161,
518 &phy_info_dm9161a,
519 &phy_info_marvell,
520 &phy_info_genmii,
521 NULL
Dave Liu7737d5c2006-11-03 12:11:15 -0600522};
523
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100524u16 phy_read (struct uec_mii_info *mii_info, u16 regnum)
Dave Liu7737d5c2006-11-03 12:11:15 -0600525{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100526 return mii_info->mdio_read (mii_info->dev, mii_info->mii_id, regnum);
Dave Liu7737d5c2006-11-03 12:11:15 -0600527}
528
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100529void phy_write (struct uec_mii_info *mii_info, u16 regnum, u16 val)
Dave Liu7737d5c2006-11-03 12:11:15 -0600530{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100531 mii_info->mdio_write (mii_info->dev, mii_info->mii_id, regnum, val);
Dave Liu7737d5c2006-11-03 12:11:15 -0600532}
533
534/* Use the PHY ID registers to determine what type of PHY is attached
535 * to device dev. return a struct phy_info structure describing that PHY
536 */
Andy Flemingda9d4612007-08-14 00:14:25 -0500537struct phy_info *uec_get_phy_info (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600538{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100539 u16 phy_reg;
540 u32 phy_ID;
541 int i;
542 struct phy_info *theInfo = NULL;
Dave Liu7737d5c2006-11-03 12:11:15 -0600543
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100544 /* Grab the bits from PHYIR1, and put them in the upper half */
545 phy_reg = phy_read (mii_info, PHY_PHYIDR1);
546 phy_ID = (phy_reg & 0xffff) << 16;
Dave Liu7737d5c2006-11-03 12:11:15 -0600547
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100548 /* Grab the bits from PHYIR2, and put them in the lower half */
549 phy_reg = phy_read (mii_info, PHY_PHYIDR2);
550 phy_ID |= (phy_reg & 0xffff);
Dave Liu7737d5c2006-11-03 12:11:15 -0600551
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100552 /* loop through all the known PHY types, and find one that */
553 /* matches the ID we read from the PHY. */
554 for (i = 0; phy_info[i]; i++)
555 if (phy_info[i]->phy_id ==
556 (phy_ID & phy_info[i]->phy_id_mask)) {
557 theInfo = phy_info[i];
558 break;
559 }
Dave Liu7737d5c2006-11-03 12:11:15 -0600560
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100561 /* This shouldn't happen, as we have generic PHY support */
562 if (theInfo == NULL) {
563 ugphy_info ("UEC: PHY id %x is not supported!", phy_ID);
564 return NULL;
565 } else {
566 ugphy_info ("UEC: PHY is %s (%x)", theInfo->name, phy_ID);
567 }
Dave Liu7737d5c2006-11-03 12:11:15 -0600568
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100569 return theInfo;
Dave Liu7737d5c2006-11-03 12:11:15 -0600570}
571
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100572void marvell_phy_interface_mode (struct eth_device *dev,
573 enet_interface_e mode)
Dave Liu7737d5c2006-11-03 12:11:15 -0600574{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100575 uec_private_t *uec = (uec_private_t *) dev->priv;
576 struct uec_mii_info *mii_info;
Dave Liu7737d5c2006-11-03 12:11:15 -0600577
578 if (!uec->mii_info) {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100579 printf ("%s: the PHY not intialized\n", __FUNCTION__);
Dave Liu7737d5c2006-11-03 12:11:15 -0600580 return;
581 }
582 mii_info = uec->mii_info;
583
584 if (mode == ENET_100_RGMII) {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100585 phy_write (mii_info, 0x00, 0x9140);
586 phy_write (mii_info, 0x1d, 0x001f);
587 phy_write (mii_info, 0x1e, 0x200c);
588 phy_write (mii_info, 0x1d, 0x0005);
589 phy_write (mii_info, 0x1e, 0x0000);
590 phy_write (mii_info, 0x1e, 0x0100);
591 phy_write (mii_info, 0x09, 0x0e00);
592 phy_write (mii_info, 0x04, 0x01e1);
593 phy_write (mii_info, 0x00, 0x9140);
594 phy_write (mii_info, 0x00, 0x1000);
595 udelay (100000);
596 phy_write (mii_info, 0x00, 0x2900);
597 phy_write (mii_info, 0x14, 0x0cd2);
598 phy_write (mii_info, 0x00, 0xa100);
599 phy_write (mii_info, 0x09, 0x0000);
600 phy_write (mii_info, 0x1b, 0x800b);
601 phy_write (mii_info, 0x04, 0x05e1);
602 phy_write (mii_info, 0x00, 0xa100);
603 phy_write (mii_info, 0x00, 0x2100);
604 udelay (1000000);
Dave Liu7737d5c2006-11-03 12:11:15 -0600605 } else if (mode == ENET_10_RGMII) {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100606 phy_write (mii_info, 0x14, 0x8e40);
607 phy_write (mii_info, 0x1b, 0x800b);
608 phy_write (mii_info, 0x14, 0x0c82);
609 phy_write (mii_info, 0x00, 0x8100);
610 udelay (1000000);
Dave Liu7737d5c2006-11-03 12:11:15 -0600611 }
612}
613
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100614void change_phy_interface_mode (struct eth_device *dev, enet_interface_e mode)
Dave Liu7737d5c2006-11-03 12:11:15 -0600615{
616#ifdef CONFIG_PHY_MODE_NEED_CHANGE
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100617 marvell_phy_interface_mode (dev, mode);
Dave Liu7737d5c2006-11-03 12:11:15 -0600618#endif
619}
620#endif /* CONFIG_QE */