blob: f890d4fbfb21a6a83e67345522e8228c4442cfaa [file] [log] [blame]
Dave Liue732e9c2006-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 Denk87b3d4b2006-11-30 18:02:20 +01007 * Driver for UEC on QE
8 * Based on 8260_io/fcc_enet.c
Dave Liue732e9c2006-11-03 12:11:15 -06009 *
Wolfgang Denk87b3d4b2006-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 Liue732e9c2006-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
31#define UEC_VERBOSE_DEBUG
32#define ugphy_printk(format, arg...) \
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +010033 printf(format "\n", ## arg)
Dave Liue732e9c2006-11-03 12:11:15 -060034
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +010035#define ugphy_dbg(format, arg...) \
36 ugphy_printk(format , ## arg)
37#define ugphy_err(format, arg...) \
38 ugphy_printk(format , ## arg)
39#define ugphy_info(format, arg...) \
40 ugphy_printk(format , ## arg)
41#define ugphy_warn(format, arg...) \
42 ugphy_printk(format , ## arg)
Dave Liue732e9c2006-11-03 12:11:15 -060043
44#ifdef UEC_VERBOSE_DEBUG
45#define ugphy_vdbg ugphy_dbg
46#else
47#define ugphy_vdbg(ugeth, fmt, args...) do { } while (0)
48#endif /* UEC_VERBOSE_DEBUG */
49
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +010050static void config_genmii_advert (struct uec_mii_info *mii_info);
51static void genmii_setup_forced (struct uec_mii_info *mii_info);
52static void genmii_restart_aneg (struct uec_mii_info *mii_info);
53static int gbit_config_aneg (struct uec_mii_info *mii_info);
54static int genmii_config_aneg (struct uec_mii_info *mii_info);
55static int genmii_update_link (struct uec_mii_info *mii_info);
56static int genmii_read_status (struct uec_mii_info *mii_info);
57u16 phy_read (struct uec_mii_info *mii_info, u16 regnum);
58void phy_write (struct uec_mii_info *mii_info, u16 regnum, u16 val);
Dave Liue732e9c2006-11-03 12:11:15 -060059
60/* Write value to the PHY for this device to the register at regnum, */
61/* waiting until the write is done before it returns. All PHY */
62/* configuration has to be done through the TSEC1 MIIM regs */
Andy Flemingee0e9172007-08-14 00:14:25 -050063void uec_write_phy_reg (struct eth_device *dev, int mii_id, int regnum, int value)
Dave Liue732e9c2006-11-03 12:11:15 -060064{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +010065 uec_private_t *ugeth = (uec_private_t *) dev->priv;
Andy Flemingee0e9172007-08-14 00:14:25 -050066 uec_mii_t *ug_regs;
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +010067 enet_tbi_mii_reg_e mii_reg = (enet_tbi_mii_reg_e) regnum;
68 u32 tmp_reg;
Dave Liue732e9c2006-11-03 12:11:15 -060069
Andy Flemingee0e9172007-08-14 00:14:25 -050070 ug_regs = ugeth->uec_mii_regs;
Dave Liue732e9c2006-11-03 12:11:15 -060071
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +010072 /* Stop the MII management read cycle */
73 out_be32 (&ug_regs->miimcom, 0);
74 /* Setting up the MII Mangement Address Register */
75 tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
76 out_be32 (&ug_regs->miimadd, tmp_reg);
Dave Liue732e9c2006-11-03 12:11:15 -060077
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +010078 /* Setting up the MII Mangement Control Register with the value */
79 out_be32 (&ug_regs->miimcon, (u32) value);
Kim Phillipsd986cba2008-01-15 14:11:00 -060080 sync();
Dave Liue732e9c2006-11-03 12:11:15 -060081
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +010082 /* Wait till MII management write is complete */
83 while ((in_be32 (&ug_regs->miimind)) & MIIMIND_BUSY);
Dave Liue732e9c2006-11-03 12:11:15 -060084}
85
86/* Reads from register regnum in the PHY for device dev, */
87/* returning the value. Clears miimcom first. All PHY */
88/* configuration has to be done through the TSEC1 MIIM regs */
Andy Flemingee0e9172007-08-14 00:14:25 -050089int uec_read_phy_reg (struct eth_device *dev, int mii_id, int regnum)
Dave Liue732e9c2006-11-03 12:11:15 -060090{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +010091 uec_private_t *ugeth = (uec_private_t *) dev->priv;
Andy Flemingee0e9172007-08-14 00:14:25 -050092 uec_mii_t *ug_regs;
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +010093 enet_tbi_mii_reg_e mii_reg = (enet_tbi_mii_reg_e) regnum;
94 u32 tmp_reg;
95 u16 value;
Dave Liue732e9c2006-11-03 12:11:15 -060096
Andy Flemingee0e9172007-08-14 00:14:25 -050097 ug_regs = ugeth->uec_mii_regs;
Dave Liue732e9c2006-11-03 12:11:15 -060098
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +010099 /* Setting up the MII Mangement Address Register */
100 tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
101 out_be32 (&ug_regs->miimadd, tmp_reg);
Dave Liue732e9c2006-11-03 12:11:15 -0600102
Kim Phillipsd986cba2008-01-15 14:11:00 -0600103 /* clear MII management command cycle */
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100104 out_be32 (&ug_regs->miimcom, 0);
Kim Phillipsd986cba2008-01-15 14:11:00 -0600105 sync();
106
107 /* Perform an MII management read cycle */
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100108 out_be32 (&ug_regs->miimcom, MIIMCOM_READ_CYCLE);
Dave Liue732e9c2006-11-03 12:11:15 -0600109
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100110 /* Wait till MII management write is complete */
111 while ((in_be32 (&ug_regs->miimind)) &
112 (MIIMIND_NOT_VALID | MIIMIND_BUSY));
Dave Liue732e9c2006-11-03 12:11:15 -0600113
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100114 /* Read MII management status */
115 value = (u16) in_be32 (&ug_regs->miimstat);
116 if (value == 0xffff)
117 ugphy_warn
118 ("read wrong value : mii_id %d,mii_reg %d, base %08x",
119 mii_id, mii_reg, (u32) & (ug_regs->miimcfg));
Dave Liue732e9c2006-11-03 12:11:15 -0600120
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100121 return (value);
Dave Liue732e9c2006-11-03 12:11:15 -0600122}
123
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100124void mii_clear_phy_interrupt (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600125{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100126 if (mii_info->phyinfo->ack_interrupt)
127 mii_info->phyinfo->ack_interrupt (mii_info);
Dave Liue732e9c2006-11-03 12:11:15 -0600128}
129
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100130void mii_configure_phy_interrupt (struct uec_mii_info *mii_info,
131 u32 interrupts)
Dave Liue732e9c2006-11-03 12:11:15 -0600132{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100133 mii_info->interrupts = interrupts;
134 if (mii_info->phyinfo->config_intr)
135 mii_info->phyinfo->config_intr (mii_info);
Dave Liue732e9c2006-11-03 12:11:15 -0600136}
137
138/* Writes MII_ADVERTISE with the appropriate values, after
139 * sanitizing advertise to make sure only supported features
140 * are advertised
141 */
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100142static void config_genmii_advert (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600143{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100144 u32 advertise;
145 u16 adv;
Dave Liue732e9c2006-11-03 12:11:15 -0600146
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100147 /* Only allow advertising what this PHY supports */
148 mii_info->advertising &= mii_info->phyinfo->features;
149 advertise = mii_info->advertising;
Dave Liue732e9c2006-11-03 12:11:15 -0600150
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100151 /* Setup standard advertisement */
152 adv = phy_read (mii_info, PHY_ANAR);
153 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
154 if (advertise & ADVERTISED_10baseT_Half)
155 adv |= ADVERTISE_10HALF;
156 if (advertise & ADVERTISED_10baseT_Full)
157 adv |= ADVERTISE_10FULL;
158 if (advertise & ADVERTISED_100baseT_Half)
159 adv |= ADVERTISE_100HALF;
160 if (advertise & ADVERTISED_100baseT_Full)
161 adv |= ADVERTISE_100FULL;
162 phy_write (mii_info, PHY_ANAR, adv);
Dave Liue732e9c2006-11-03 12:11:15 -0600163}
164
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100165static void genmii_setup_forced (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600166{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100167 u16 ctrl;
168 u32 features = mii_info->phyinfo->features;
Dave Liue732e9c2006-11-03 12:11:15 -0600169
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100170 ctrl = phy_read (mii_info, PHY_BMCR);
Dave Liue732e9c2006-11-03 12:11:15 -0600171
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100172 ctrl &= ~(PHY_BMCR_DPLX | PHY_BMCR_100_MBPS |
173 PHY_BMCR_1000_MBPS | PHY_BMCR_AUTON);
174 ctrl |= PHY_BMCR_RESET;
Dave Liue732e9c2006-11-03 12:11:15 -0600175
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100176 switch (mii_info->speed) {
177 case SPEED_1000:
178 if (features & (SUPPORTED_1000baseT_Half
179 | SUPPORTED_1000baseT_Full)) {
180 ctrl |= PHY_BMCR_1000_MBPS;
181 break;
182 }
183 mii_info->speed = SPEED_100;
184 case SPEED_100:
185 if (features & (SUPPORTED_100baseT_Half
186 | SUPPORTED_100baseT_Full)) {
187 ctrl |= PHY_BMCR_100_MBPS;
188 break;
189 }
190 mii_info->speed = SPEED_10;
191 case SPEED_10:
192 if (features & (SUPPORTED_10baseT_Half
193 | SUPPORTED_10baseT_Full))
194 break;
195 default: /* Unsupported speed! */
196 ugphy_err ("%s: Bad speed!", mii_info->dev->name);
197 break;
198 }
Dave Liue732e9c2006-11-03 12:11:15 -0600199
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100200 phy_write (mii_info, PHY_BMCR, ctrl);
Dave Liue732e9c2006-11-03 12:11:15 -0600201}
202
203/* Enable and Restart Autonegotiation */
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100204static void genmii_restart_aneg (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600205{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100206 u16 ctl;
Dave Liue732e9c2006-11-03 12:11:15 -0600207
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100208 ctl = phy_read (mii_info, PHY_BMCR);
209 ctl |= (PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
210 phy_write (mii_info, PHY_BMCR, ctl);
Dave Liue732e9c2006-11-03 12:11:15 -0600211}
212
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100213static int gbit_config_aneg (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600214{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100215 u16 adv;
216 u32 advertise;
Dave Liue732e9c2006-11-03 12:11:15 -0600217
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100218 if (mii_info->autoneg) {
219 /* Configure the ADVERTISE register */
220 config_genmii_advert (mii_info);
221 advertise = mii_info->advertising;
Dave Liue732e9c2006-11-03 12:11:15 -0600222
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100223 adv = phy_read (mii_info, MII_1000BASETCONTROL);
224 adv &= ~(MII_1000BASETCONTROL_FULLDUPLEXCAP |
225 MII_1000BASETCONTROL_HALFDUPLEXCAP);
226 if (advertise & SUPPORTED_1000baseT_Half)
227 adv |= MII_1000BASETCONTROL_HALFDUPLEXCAP;
228 if (advertise & SUPPORTED_1000baseT_Full)
229 adv |= MII_1000BASETCONTROL_FULLDUPLEXCAP;
230 phy_write (mii_info, MII_1000BASETCONTROL, adv);
Dave Liue732e9c2006-11-03 12:11:15 -0600231
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100232 /* Start/Restart aneg */
233 genmii_restart_aneg (mii_info);
234 } else
235 genmii_setup_forced (mii_info);
Dave Liue732e9c2006-11-03 12:11:15 -0600236
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100237 return 0;
Dave Liue732e9c2006-11-03 12:11:15 -0600238}
239
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100240static int marvell_config_aneg (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600241{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100242 /* The Marvell PHY has an errata which requires
243 * that certain registers get written in order
244 * to restart autonegotiation */
245 phy_write (mii_info, PHY_BMCR, PHY_BMCR_RESET);
Dave Liue732e9c2006-11-03 12:11:15 -0600246
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100247 phy_write (mii_info, 0x1d, 0x1f);
248 phy_write (mii_info, 0x1e, 0x200c);
249 phy_write (mii_info, 0x1d, 0x5);
250 phy_write (mii_info, 0x1e, 0);
251 phy_write (mii_info, 0x1e, 0x100);
Dave Liue732e9c2006-11-03 12:11:15 -0600252
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100253 gbit_config_aneg (mii_info);
Dave Liue732e9c2006-11-03 12:11:15 -0600254
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100255 return 0;
Dave Liue732e9c2006-11-03 12:11:15 -0600256}
257
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100258static int genmii_config_aneg (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600259{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100260 if (mii_info->autoneg) {
261 config_genmii_advert (mii_info);
262 genmii_restart_aneg (mii_info);
263 } else
264 genmii_setup_forced (mii_info);
Dave Liue732e9c2006-11-03 12:11:15 -0600265
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100266 return 0;
Dave Liue732e9c2006-11-03 12:11:15 -0600267}
268
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100269static int genmii_update_link (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600270{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100271 u16 status;
Dave Liue732e9c2006-11-03 12:11:15 -0600272
Kim Phillipsd986cba2008-01-15 14:11:00 -0600273 /* Status is read once to clear old link state */
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100274 phy_read (mii_info, PHY_BMSR);
Dave Liue732e9c2006-11-03 12:11:15 -0600275
Kim Phillipsd986cba2008-01-15 14:11:00 -0600276 /*
277 * Wait if the link is up, and autonegotiation is in progress
278 * (ie - we're capable and it's not done)
279 */
280 status = phy_read(mii_info, PHY_BMSR);
281 if ((status & PHY_BMSR_LS) && (status & PHY_BMSR_AUTN_ABLE)
282 && !(status & PHY_BMSR_AUTN_COMP)) {
283 int i = 0;
Dave Liue732e9c2006-11-03 12:11:15 -0600284
Kim Phillipsd986cba2008-01-15 14:11:00 -0600285 while (!(status & PHY_BMSR_AUTN_COMP)) {
286 /*
287 * Timeout reached ?
288 */
289 if (i > UGETH_AN_TIMEOUT) {
290 mii_info->link = 0;
291 return 0;
292 }
293
294 udelay(1000); /* 1 ms */
295 status = phy_read(mii_info, PHY_BMSR);
296 }
297 mii_info->link = 1;
298 udelay(500000); /* another 500 ms (results in faster booting) */
299 } else {
300 if (status & PHY_BMSR_LS)
301 mii_info->link = 1;
302 else
303 mii_info->link = 0;
304 }
Dave Liue732e9c2006-11-03 12:11:15 -0600305
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100306 return 0;
Dave Liue732e9c2006-11-03 12:11:15 -0600307}
308
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100309static int genmii_read_status (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600310{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100311 u16 status;
312 int err;
Dave Liue732e9c2006-11-03 12:11:15 -0600313
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100314 /* Update the link, but return if there
315 * was an error */
316 err = genmii_update_link (mii_info);
317 if (err)
318 return err;
Dave Liue732e9c2006-11-03 12:11:15 -0600319
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100320 if (mii_info->autoneg) {
321 status = phy_read (mii_info, PHY_ANLPAR);
Dave Liue732e9c2006-11-03 12:11:15 -0600322
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100323 if (status & (PHY_ANLPAR_10FD | PHY_ANLPAR_TXFD))
324 mii_info->duplex = DUPLEX_FULL;
325 else
326 mii_info->duplex = DUPLEX_HALF;
327 if (status & (PHY_ANLPAR_TXFD | PHY_ANLPAR_TX))
328 mii_info->speed = SPEED_100;
329 else
330 mii_info->speed = SPEED_10;
331 mii_info->pause = 0;
332 }
333 /* On non-aneg, we assume what we put in BMCR is the speed,
334 * though magic-aneg shouldn't prevent this case from occurring
335 */
Dave Liue732e9c2006-11-03 12:11:15 -0600336
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100337 return 0;
Dave Liue732e9c2006-11-03 12:11:15 -0600338}
339
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100340static int marvell_read_status (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600341{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100342 u16 status;
343 int err;
344
345 /* Update the link, but return if there
346 * was an error */
347 err = genmii_update_link (mii_info);
348 if (err)
349 return err;
Dave Liue732e9c2006-11-03 12:11:15 -0600350
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100351 /* If the link is up, read the speed and duplex */
352 /* If we aren't autonegotiating, assume speeds
353 * are as set */
354 if (mii_info->autoneg && mii_info->link) {
355 int speed;
Dave Liue732e9c2006-11-03 12:11:15 -0600356
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100357 status = phy_read (mii_info, MII_M1011_PHY_SPEC_STATUS);
Dave Liue732e9c2006-11-03 12:11:15 -0600358
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100359 /* Get the duplexity */
360 if (status & MII_M1011_PHY_SPEC_STATUS_FULLDUPLEX)
361 mii_info->duplex = DUPLEX_FULL;
362 else
363 mii_info->duplex = DUPLEX_HALF;
Dave Liue732e9c2006-11-03 12:11:15 -0600364
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100365 /* Get the speed */
366 speed = status & MII_M1011_PHY_SPEC_STATUS_SPD_MASK;
367 switch (speed) {
368 case MII_M1011_PHY_SPEC_STATUS_1000:
369 mii_info->speed = SPEED_1000;
370 break;
371 case MII_M1011_PHY_SPEC_STATUS_100:
372 mii_info->speed = SPEED_100;
373 break;
374 default:
375 mii_info->speed = SPEED_10;
376 break;
377 }
378 mii_info->pause = 0;
379 }
Dave Liue732e9c2006-11-03 12:11:15 -0600380
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100381 return 0;
Dave Liue732e9c2006-11-03 12:11:15 -0600382}
383
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100384static int marvell_ack_interrupt (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600385{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100386 /* Clear the interrupts by reading the reg */
387 phy_read (mii_info, MII_M1011_IEVENT);
Dave Liue732e9c2006-11-03 12:11:15 -0600388
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100389 return 0;
Dave Liue732e9c2006-11-03 12:11:15 -0600390}
391
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100392static int marvell_config_intr (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600393{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100394 if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
395 phy_write (mii_info, MII_M1011_IMASK, MII_M1011_IMASK_INIT);
396 else
397 phy_write (mii_info, MII_M1011_IMASK, MII_M1011_IMASK_CLEAR);
Dave Liue732e9c2006-11-03 12:11:15 -0600398
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100399 return 0;
Dave Liue732e9c2006-11-03 12:11:15 -0600400}
401
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100402static int dm9161_init (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600403{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100404 /* Reset the PHY */
405 phy_write (mii_info, PHY_BMCR, phy_read (mii_info, PHY_BMCR) |
406 PHY_BMCR_RESET);
407 /* PHY and MAC connect */
408 phy_write (mii_info, PHY_BMCR, phy_read (mii_info, PHY_BMCR) &
409 ~PHY_BMCR_ISO);
Kim Phillipsd986cba2008-01-15 14:11:00 -0600410
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100411 phy_write (mii_info, MII_DM9161_SCR, MII_DM9161_SCR_INIT);
Kim Phillipsd986cba2008-01-15 14:11:00 -0600412
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100413 config_genmii_advert (mii_info);
414 /* Start/restart aneg */
415 genmii_config_aneg (mii_info);
Dave Liue732e9c2006-11-03 12:11:15 -0600416
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100417 return 0;
Dave Liue732e9c2006-11-03 12:11:15 -0600418}
419
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100420static int dm9161_config_aneg (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600421{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100422 return 0;
Dave Liue732e9c2006-11-03 12:11:15 -0600423}
424
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100425static int dm9161_read_status (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600426{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100427 u16 status;
428 int err;
Dave Liue732e9c2006-11-03 12:11:15 -0600429
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100430 /* Update the link, but return if there was an error */
431 err = genmii_update_link (mii_info);
432 if (err)
433 return err;
434 /* If the link is up, read the speed and duplex
435 If we aren't autonegotiating assume speeds are as set */
436 if (mii_info->autoneg && mii_info->link) {
437 status = phy_read (mii_info, MII_DM9161_SCSR);
438 if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_100H))
439 mii_info->speed = SPEED_100;
440 else
441 mii_info->speed = SPEED_10;
Dave Liue732e9c2006-11-03 12:11:15 -0600442
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100443 if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_10F))
444 mii_info->duplex = DUPLEX_FULL;
445 else
446 mii_info->duplex = DUPLEX_HALF;
447 }
Dave Liue732e9c2006-11-03 12:11:15 -0600448
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100449 return 0;
Dave Liue732e9c2006-11-03 12:11:15 -0600450}
451
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100452static int dm9161_ack_interrupt (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600453{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100454 /* Clear the interrupt by reading the reg */
455 phy_read (mii_info, MII_DM9161_INTR);
Dave Liue732e9c2006-11-03 12:11:15 -0600456
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100457 return 0;
Dave Liue732e9c2006-11-03 12:11:15 -0600458}
459
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100460static int dm9161_config_intr (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600461{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100462 if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
463 phy_write (mii_info, MII_DM9161_INTR, MII_DM9161_INTR_INIT);
464 else
465 phy_write (mii_info, MII_DM9161_INTR, MII_DM9161_INTR_STOP);
Dave Liue732e9c2006-11-03 12:11:15 -0600466
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100467 return 0;
Dave Liue732e9c2006-11-03 12:11:15 -0600468}
469
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100470static void dm9161_close (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600471{
472}
473
474static struct phy_info phy_info_dm9161 = {
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100475 .phy_id = 0x0181b880,
476 .phy_id_mask = 0x0ffffff0,
477 .name = "Davicom DM9161E",
478 .init = dm9161_init,
479 .config_aneg = dm9161_config_aneg,
480 .read_status = dm9161_read_status,
481 .close = dm9161_close,
Dave Liue732e9c2006-11-03 12:11:15 -0600482};
483
484static struct phy_info phy_info_dm9161a = {
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100485 .phy_id = 0x0181b8a0,
486 .phy_id_mask = 0x0ffffff0,
487 .name = "Davicom DM9161A",
488 .features = MII_BASIC_FEATURES,
489 .init = dm9161_init,
490 .config_aneg = dm9161_config_aneg,
491 .read_status = dm9161_read_status,
492 .ack_interrupt = dm9161_ack_interrupt,
493 .config_intr = dm9161_config_intr,
494 .close = dm9161_close,
Dave Liue732e9c2006-11-03 12:11:15 -0600495};
496
497static struct phy_info phy_info_marvell = {
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100498 .phy_id = 0x01410c00,
499 .phy_id_mask = 0xffffff00,
500 .name = "Marvell 88E11x1",
501 .features = MII_GBIT_FEATURES,
502 .config_aneg = &marvell_config_aneg,
503 .read_status = &marvell_read_status,
504 .ack_interrupt = &marvell_ack_interrupt,
505 .config_intr = &marvell_config_intr,
Dave Liue732e9c2006-11-03 12:11:15 -0600506};
507
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100508static struct phy_info phy_info_genmii = {
509 .phy_id = 0x00000000,
510 .phy_id_mask = 0x00000000,
511 .name = "Generic MII",
512 .features = MII_BASIC_FEATURES,
513 .config_aneg = genmii_config_aneg,
514 .read_status = genmii_read_status,
Dave Liue732e9c2006-11-03 12:11:15 -0600515};
516
517static struct phy_info *phy_info[] = {
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100518 &phy_info_dm9161,
519 &phy_info_dm9161a,
520 &phy_info_marvell,
521 &phy_info_genmii,
522 NULL
Dave Liue732e9c2006-11-03 12:11:15 -0600523};
524
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100525u16 phy_read (struct uec_mii_info *mii_info, u16 regnum)
Dave Liue732e9c2006-11-03 12:11:15 -0600526{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100527 return mii_info->mdio_read (mii_info->dev, mii_info->mii_id, regnum);
Dave Liue732e9c2006-11-03 12:11:15 -0600528}
529
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100530void phy_write (struct uec_mii_info *mii_info, u16 regnum, u16 val)
Dave Liue732e9c2006-11-03 12:11:15 -0600531{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100532 mii_info->mdio_write (mii_info->dev, mii_info->mii_id, regnum, val);
Dave Liue732e9c2006-11-03 12:11:15 -0600533}
534
535/* Use the PHY ID registers to determine what type of PHY is attached
536 * to device dev. return a struct phy_info structure describing that PHY
537 */
Andy Flemingee0e9172007-08-14 00:14:25 -0500538struct phy_info *uec_get_phy_info (struct uec_mii_info *mii_info)
Dave Liue732e9c2006-11-03 12:11:15 -0600539{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100540 u16 phy_reg;
541 u32 phy_ID;
542 int i;
543 struct phy_info *theInfo = NULL;
Dave Liue732e9c2006-11-03 12:11:15 -0600544
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100545 /* Grab the bits from PHYIR1, and put them in the upper half */
546 phy_reg = phy_read (mii_info, PHY_PHYIDR1);
547 phy_ID = (phy_reg & 0xffff) << 16;
Dave Liue732e9c2006-11-03 12:11:15 -0600548
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100549 /* Grab the bits from PHYIR2, and put them in the lower half */
550 phy_reg = phy_read (mii_info, PHY_PHYIDR2);
551 phy_ID |= (phy_reg & 0xffff);
Dave Liue732e9c2006-11-03 12:11:15 -0600552
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100553 /* loop through all the known PHY types, and find one that */
554 /* matches the ID we read from the PHY. */
555 for (i = 0; phy_info[i]; i++)
556 if (phy_info[i]->phy_id ==
557 (phy_ID & phy_info[i]->phy_id_mask)) {
558 theInfo = phy_info[i];
559 break;
560 }
Dave Liue732e9c2006-11-03 12:11:15 -0600561
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100562 /* This shouldn't happen, as we have generic PHY support */
563 if (theInfo == NULL) {
564 ugphy_info ("UEC: PHY id %x is not supported!", phy_ID);
565 return NULL;
566 } else {
567 ugphy_info ("UEC: PHY is %s (%x)", theInfo->name, phy_ID);
568 }
Dave Liue732e9c2006-11-03 12:11:15 -0600569
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100570 return theInfo;
Dave Liue732e9c2006-11-03 12:11:15 -0600571}
572
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100573void marvell_phy_interface_mode (struct eth_device *dev,
574 enet_interface_e mode)
Dave Liue732e9c2006-11-03 12:11:15 -0600575{
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100576 uec_private_t *uec = (uec_private_t *) dev->priv;
577 struct uec_mii_info *mii_info;
Dave Liue732e9c2006-11-03 12:11:15 -0600578
579 if (!uec->mii_info) {
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100580 printf ("%s: the PHY not intialized\n", __FUNCTION__);
Dave Liue732e9c2006-11-03 12:11:15 -0600581 return;
582 }
583 mii_info = uec->mii_info;
584
585 if (mode == ENET_100_RGMII) {
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100586 phy_write (mii_info, 0x00, 0x9140);
587 phy_write (mii_info, 0x1d, 0x001f);
588 phy_write (mii_info, 0x1e, 0x200c);
589 phy_write (mii_info, 0x1d, 0x0005);
590 phy_write (mii_info, 0x1e, 0x0000);
591 phy_write (mii_info, 0x1e, 0x0100);
592 phy_write (mii_info, 0x09, 0x0e00);
593 phy_write (mii_info, 0x04, 0x01e1);
594 phy_write (mii_info, 0x00, 0x9140);
595 phy_write (mii_info, 0x00, 0x1000);
596 udelay (100000);
597 phy_write (mii_info, 0x00, 0x2900);
598 phy_write (mii_info, 0x14, 0x0cd2);
599 phy_write (mii_info, 0x00, 0xa100);
600 phy_write (mii_info, 0x09, 0x0000);
601 phy_write (mii_info, 0x1b, 0x800b);
602 phy_write (mii_info, 0x04, 0x05e1);
603 phy_write (mii_info, 0x00, 0xa100);
604 phy_write (mii_info, 0x00, 0x2100);
605 udelay (1000000);
Dave Liue732e9c2006-11-03 12:11:15 -0600606 } else if (mode == ENET_10_RGMII) {
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100607 phy_write (mii_info, 0x14, 0x8e40);
608 phy_write (mii_info, 0x1b, 0x800b);
609 phy_write (mii_info, 0x14, 0x0c82);
610 phy_write (mii_info, 0x00, 0x8100);
611 udelay (1000000);
Dave Liue732e9c2006-11-03 12:11:15 -0600612 }
613}
614
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100615void change_phy_interface_mode (struct eth_device *dev, enet_interface_e mode)
Dave Liue732e9c2006-11-03 12:11:15 -0600616{
617#ifdef CONFIG_PHY_MODE_NEED_CHANGE
Wolfgang Denk87b3d4b2006-11-30 18:02:20 +0100618 marvell_phy_interface_mode (dev, mode);
Dave Liue732e9c2006-11-03 12:11:15 -0600619#endif
620}
621#endif /* CONFIG_QE */