blob: e701787c41a3978d1d9d6b47cf2c950c2dcf6069 [file] [log] [blame]
Dave Liu7737d5c2006-11-03 12:11:15 -06001/*
Kumar Gala2b21ec92011-01-19 03:36:40 -06002 * Copyright (C) 2005,2010-2011 Freescale Semiconductor, Inc.
Dave Liu7737d5c2006-11-03 12:11:15 -06003 *
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 Denk1a459662013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Dave Liu7737d5c2006-11-03 12:11:15 -060011 */
12
13#include "common.h"
14#include "net.h"
15#include "malloc.h"
16#include "asm/errno.h"
Zhao Qiang38d67a4e2014-06-03 16:27:07 +080017#include "linux/immap_qe.h"
Dave Liu7737d5c2006-11-03 12:11:15 -060018#include "asm/io.h"
19#include "qe.h"
20#include "uccf.h"
21#include "uec.h"
22#include "uec_phy.h"
23#include "miiphy.h"
Andy Fleming865ff852011-04-13 00:37:12 -050024#include <phy.h>
Dave Liu7737d5c2006-11-03 12:11:15 -060025
Dave Liu7737d5c2006-11-03 12:11:15 -060026#define ugphy_printk(format, arg...) \
Wolfgang Denkdd520bf2006-11-30 18:02:20 +010027 printf(format "\n", ## arg)
Dave Liu7737d5c2006-11-03 12:11:15 -060028
Wolfgang Denkdd520bf2006-11-30 18:02:20 +010029#define ugphy_dbg(format, arg...) \
30 ugphy_printk(format , ## arg)
31#define ugphy_err(format, arg...) \
32 ugphy_printk(format , ## arg)
33#define ugphy_info(format, arg...) \
34 ugphy_printk(format , ## arg)
35#define ugphy_warn(format, arg...) \
36 ugphy_printk(format , ## arg)
Dave Liu7737d5c2006-11-03 12:11:15 -060037
38#ifdef UEC_VERBOSE_DEBUG
39#define ugphy_vdbg ugphy_dbg
40#else
41#define ugphy_vdbg(ugeth, fmt, args...) do { } while (0)
42#endif /* UEC_VERBOSE_DEBUG */
43
Richard Retanubunedf3fe72008-10-23 09:08:18 -040044/*--------------------------------------------------------------------+
45 * Fixed PHY (PHY-less) support for Ethernet Ports.
46 *
Stefan Roesea47a12b2010-04-15 16:07:28 +020047 * Copied from arch/powerpc/cpu/ppc4xx/4xx_enet.c
Richard Retanubunedf3fe72008-10-23 09:08:18 -040048 *--------------------------------------------------------------------*/
49
50/*
Richard Retanubun1443cd72009-07-01 14:04:05 -040051 * Some boards do not have a PHY for each ethernet port. These ports are known
52 * as Fixed PHY (or PHY-less) ports. For such ports, set the appropriate
53 * CONFIG_SYS_UECx_PHY_ADDR equal to CONFIG_FIXED_PHY_ADDR (an unused address)
54 * When the drver tries to identify the PHYs, CONFIG_FIXED_PHY will be returned
55 * and the driver will search CONFIG_SYS_FIXED_PHY_PORTS to find what network
56 * speed and duplex should be for the port.
Richard Retanubunedf3fe72008-10-23 09:08:18 -040057 *
Richard Retanubun1443cd72009-07-01 14:04:05 -040058 * Example board header configuration file:
Richard Retanubunedf3fe72008-10-23 09:08:18 -040059 * #define CONFIG_FIXED_PHY 0xFFFFFFFF
Richard Retanubun1443cd72009-07-01 14:04:05 -040060 * #define CONFIG_SYS_FIXED_PHY_ADDR 0x1E (pick an unused phy address)
Richard Retanubunedf3fe72008-10-23 09:08:18 -040061 *
Richard Retanubun1443cd72009-07-01 14:04:05 -040062 * #define CONFIG_SYS_UEC1_PHY_ADDR CONFIG_SYS_FIXED_PHY_ADDR
63 * #define CONFIG_SYS_UEC2_PHY_ADDR 0x02
64 * #define CONFIG_SYS_UEC3_PHY_ADDR CONFIG_SYS_FIXED_PHY_ADDR
65 * #define CONFIG_SYS_UEC4_PHY_ADDR 0x04
Richard Retanubunedf3fe72008-10-23 09:08:18 -040066 *
Richard Retanubun1443cd72009-07-01 14:04:05 -040067 * #define CONFIG_SYS_FIXED_PHY_PORT(name,speed,duplex) \
68 * {name, speed, duplex},
Richard Retanubunedf3fe72008-10-23 09:08:18 -040069 *
70 * #define CONFIG_SYS_FIXED_PHY_PORTS \
Kim Phillips78b7a8e2010-07-26 18:34:57 -050071 * CONFIG_SYS_FIXED_PHY_PORT("UEC0",SPEED_100,DUPLEX_FULL) \
72 * CONFIG_SYS_FIXED_PHY_PORT("UEC2",SPEED_100,DUPLEX_HALF)
Richard Retanubunedf3fe72008-10-23 09:08:18 -040073 */
74
75#ifndef CONFIG_FIXED_PHY
76#define CONFIG_FIXED_PHY 0xFFFFFFFF /* Fixed PHY (PHY-less) */
77#endif
78
79#ifndef CONFIG_SYS_FIXED_PHY_PORTS
80#define CONFIG_SYS_FIXED_PHY_PORTS /* default is an empty array */
81#endif
82
83struct fixed_phy_port {
Mike Frysingerf6add132011-11-10 14:11:04 +000084 char name[16]; /* ethernet port name */
Richard Retanubunedf3fe72008-10-23 09:08:18 -040085 unsigned int speed; /* specified speed 10,100 or 1000 */
86 unsigned int duplex; /* specified duplex FULL or HALF */
87};
88
89static const struct fixed_phy_port fixed_phy_port[] = {
90 CONFIG_SYS_FIXED_PHY_PORTS /* defined in board configuration file */
91};
92
Richard Retanubun23c34af2009-06-17 16:00:41 -040093/*--------------------------------------------------------------------+
94 * BitBang MII support for ethernet ports
95 *
96 * Based from MPC8560ADS implementation
97 *--------------------------------------------------------------------*/
98/*
99 * Example board header file to define bitbang ethernet ports:
100 *
101 * #define CONFIG_SYS_BITBANG_PHY_PORT(name) name,
Kim Phillips78b7a8e2010-07-26 18:34:57 -0500102 * #define CONFIG_SYS_BITBANG_PHY_PORTS CONFIG_SYS_BITBANG_PHY_PORT("UEC0")
Richard Retanubun23c34af2009-06-17 16:00:41 -0400103*/
104#ifndef CONFIG_SYS_BITBANG_PHY_PORTS
105#define CONFIG_SYS_BITBANG_PHY_PORTS /* default is an empty array */
106#endif
107
108#if defined(CONFIG_BITBANGMII)
109static const char *bitbang_phy_port[] = {
110 CONFIG_SYS_BITBANG_PHY_PORTS /* defined in board configuration file */
111};
112#endif /* CONFIG_BITBANGMII */
113
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100114static void config_genmii_advert (struct uec_mii_info *mii_info);
115static void genmii_setup_forced (struct uec_mii_info *mii_info);
116static void genmii_restart_aneg (struct uec_mii_info *mii_info);
117static int gbit_config_aneg (struct uec_mii_info *mii_info);
118static int genmii_config_aneg (struct uec_mii_info *mii_info);
119static int genmii_update_link (struct uec_mii_info *mii_info);
120static int genmii_read_status (struct uec_mii_info *mii_info);
Andy Fleming09c04c22011-03-22 22:49:13 -0500121u16 uec_phy_read(struct uec_mii_info *mii_info, u16 regnum);
122void uec_phy_write(struct uec_mii_info *mii_info, u16 regnum, u16 val);
Dave Liu7737d5c2006-11-03 12:11:15 -0600123
124/* Write value to the PHY for this device to the register at regnum, */
125/* waiting until the write is done before it returns. All PHY */
126/* configuration has to be done through the TSEC1 MIIM regs */
Andy Flemingda9d4612007-08-14 00:14:25 -0500127void uec_write_phy_reg (struct eth_device *dev, int mii_id, int regnum, int value)
Dave Liu7737d5c2006-11-03 12:11:15 -0600128{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100129 uec_private_t *ugeth = (uec_private_t *) dev->priv;
Andy Flemingda9d4612007-08-14 00:14:25 -0500130 uec_mii_t *ug_regs;
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100131 enet_tbi_mii_reg_e mii_reg = (enet_tbi_mii_reg_e) regnum;
132 u32 tmp_reg;
Dave Liu7737d5c2006-11-03 12:11:15 -0600133
Richard Retanubun23c34af2009-06-17 16:00:41 -0400134
135#if defined(CONFIG_BITBANGMII)
136 u32 i = 0;
137
138 for (i = 0; i < ARRAY_SIZE(bitbang_phy_port); i++) {
139 if (strncmp(dev->name, bitbang_phy_port[i],
140 sizeof(dev->name)) == 0) {
141 (void)bb_miiphy_write(NULL, mii_id, regnum, value);
142 return;
143 }
144 }
145#endif /* CONFIG_BITBANGMII */
146
Andy Flemingda9d4612007-08-14 00:14:25 -0500147 ug_regs = ugeth->uec_mii_regs;
Dave Liu7737d5c2006-11-03 12:11:15 -0600148
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100149 /* Stop the MII management read cycle */
150 out_be32 (&ug_regs->miimcom, 0);
151 /* Setting up the MII Mangement Address Register */
152 tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
153 out_be32 (&ug_regs->miimadd, tmp_reg);
Dave Liu7737d5c2006-11-03 12:11:15 -0600154
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100155 /* Setting up the MII Mangement Control Register with the value */
156 out_be32 (&ug_regs->miimcon, (u32) value);
Kim Phillipsee62ed32008-01-15 14:11:00 -0600157 sync();
Dave Liu7737d5c2006-11-03 12:11:15 -0600158
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100159 /* Wait till MII management write is complete */
160 while ((in_be32 (&ug_regs->miimind)) & MIIMIND_BUSY);
Dave Liu7737d5c2006-11-03 12:11:15 -0600161}
162
163/* Reads from register regnum in the PHY for device dev, */
164/* returning the value. Clears miimcom first. All PHY */
165/* configuration has to be done through the TSEC1 MIIM regs */
Andy Flemingda9d4612007-08-14 00:14:25 -0500166int uec_read_phy_reg (struct eth_device *dev, int mii_id, int regnum)
Dave Liu7737d5c2006-11-03 12:11:15 -0600167{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100168 uec_private_t *ugeth = (uec_private_t *) dev->priv;
Andy Flemingda9d4612007-08-14 00:14:25 -0500169 uec_mii_t *ug_regs;
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100170 enet_tbi_mii_reg_e mii_reg = (enet_tbi_mii_reg_e) regnum;
171 u32 tmp_reg;
172 u16 value;
Dave Liu7737d5c2006-11-03 12:11:15 -0600173
Richard Retanubun23c34af2009-06-17 16:00:41 -0400174
175#if defined(CONFIG_BITBANGMII)
176 u32 i = 0;
177
178 for (i = 0; i < ARRAY_SIZE(bitbang_phy_port); i++) {
179 if (strncmp(dev->name, bitbang_phy_port[i],
180 sizeof(dev->name)) == 0) {
181 (void)bb_miiphy_read(NULL, mii_id, regnum, &value);
182 return (value);
183 }
184 }
185#endif /* CONFIG_BITBANGMII */
186
Andy Flemingda9d4612007-08-14 00:14:25 -0500187 ug_regs = ugeth->uec_mii_regs;
Dave Liu7737d5c2006-11-03 12:11:15 -0600188
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100189 /* Setting up the MII Mangement Address Register */
190 tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
191 out_be32 (&ug_regs->miimadd, tmp_reg);
Dave Liu7737d5c2006-11-03 12:11:15 -0600192
Kim Phillipsee62ed32008-01-15 14:11:00 -0600193 /* clear MII management command cycle */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100194 out_be32 (&ug_regs->miimcom, 0);
Kim Phillipsee62ed32008-01-15 14:11:00 -0600195 sync();
196
197 /* Perform an MII management read cycle */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100198 out_be32 (&ug_regs->miimcom, MIIMCOM_READ_CYCLE);
Dave Liu7737d5c2006-11-03 12:11:15 -0600199
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100200 /* Wait till MII management write is complete */
201 while ((in_be32 (&ug_regs->miimind)) &
202 (MIIMIND_NOT_VALID | MIIMIND_BUSY));
Dave Liu7737d5c2006-11-03 12:11:15 -0600203
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100204 /* Read MII management status */
205 value = (u16) in_be32 (&ug_regs->miimstat);
206 if (value == 0xffff)
Joakim Tjernlund84a30472008-01-16 09:40:41 +0100207 ugphy_vdbg
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100208 ("read wrong value : mii_id %d,mii_reg %d, base %08x",
209 mii_id, mii_reg, (u32) & (ug_regs->miimcfg));
Dave Liu7737d5c2006-11-03 12:11:15 -0600210
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100211 return (value);
Dave Liu7737d5c2006-11-03 12:11:15 -0600212}
213
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100214void mii_clear_phy_interrupt (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600215{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100216 if (mii_info->phyinfo->ack_interrupt)
217 mii_info->phyinfo->ack_interrupt (mii_info);
Dave Liu7737d5c2006-11-03 12:11:15 -0600218}
219
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100220void mii_configure_phy_interrupt (struct uec_mii_info *mii_info,
221 u32 interrupts)
Dave Liu7737d5c2006-11-03 12:11:15 -0600222{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100223 mii_info->interrupts = interrupts;
224 if (mii_info->phyinfo->config_intr)
225 mii_info->phyinfo->config_intr (mii_info);
Dave Liu7737d5c2006-11-03 12:11:15 -0600226}
227
228/* Writes MII_ADVERTISE with the appropriate values, after
229 * sanitizing advertise to make sure only supported features
230 * are advertised
231 */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100232static void config_genmii_advert (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600233{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100234 u32 advertise;
235 u16 adv;
Dave Liu7737d5c2006-11-03 12:11:15 -0600236
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100237 /* Only allow advertising what this PHY supports */
238 mii_info->advertising &= mii_info->phyinfo->features;
239 advertise = mii_info->advertising;
Dave Liu7737d5c2006-11-03 12:11:15 -0600240
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100241 /* Setup standard advertisement */
Andy Fleming09c04c22011-03-22 22:49:13 -0500242 adv = uec_phy_read(mii_info, MII_ADVERTISE);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100243 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
244 if (advertise & ADVERTISED_10baseT_Half)
245 adv |= ADVERTISE_10HALF;
246 if (advertise & ADVERTISED_10baseT_Full)
247 adv |= ADVERTISE_10FULL;
248 if (advertise & ADVERTISED_100baseT_Half)
249 adv |= ADVERTISE_100HALF;
250 if (advertise & ADVERTISED_100baseT_Full)
251 adv |= ADVERTISE_100FULL;
Andy Fleming09c04c22011-03-22 22:49:13 -0500252 uec_phy_write(mii_info, MII_ADVERTISE, adv);
Dave Liu7737d5c2006-11-03 12:11:15 -0600253}
254
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100255static void genmii_setup_forced (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600256{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100257 u16 ctrl;
258 u32 features = mii_info->phyinfo->features;
Dave Liu7737d5c2006-11-03 12:11:15 -0600259
Andy Fleming09c04c22011-03-22 22:49:13 -0500260 ctrl = uec_phy_read(mii_info, MII_BMCR);
Dave Liu7737d5c2006-11-03 12:11:15 -0600261
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500262 ctrl &= ~(BMCR_FULLDPLX | BMCR_SPEED100 |
263 BMCR_SPEED1000 | BMCR_ANENABLE);
264 ctrl |= BMCR_RESET;
Dave Liu7737d5c2006-11-03 12:11:15 -0600265
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100266 switch (mii_info->speed) {
267 case SPEED_1000:
268 if (features & (SUPPORTED_1000baseT_Half
269 | SUPPORTED_1000baseT_Full)) {
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500270 ctrl |= BMCR_SPEED1000;
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100271 break;
272 }
273 mii_info->speed = SPEED_100;
274 case SPEED_100:
275 if (features & (SUPPORTED_100baseT_Half
276 | SUPPORTED_100baseT_Full)) {
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500277 ctrl |= BMCR_SPEED100;
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100278 break;
279 }
280 mii_info->speed = SPEED_10;
281 case SPEED_10:
282 if (features & (SUPPORTED_10baseT_Half
283 | SUPPORTED_10baseT_Full))
284 break;
285 default: /* Unsupported speed! */
286 ugphy_err ("%s: Bad speed!", mii_info->dev->name);
287 break;
288 }
Dave Liu7737d5c2006-11-03 12:11:15 -0600289
Andy Fleming09c04c22011-03-22 22:49:13 -0500290 uec_phy_write(mii_info, MII_BMCR, ctrl);
Dave Liu7737d5c2006-11-03 12:11:15 -0600291}
292
293/* Enable and Restart Autonegotiation */
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100294static void genmii_restart_aneg (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600295{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100296 u16 ctl;
Dave Liu7737d5c2006-11-03 12:11:15 -0600297
Andy Fleming09c04c22011-03-22 22:49:13 -0500298 ctl = uec_phy_read(mii_info, MII_BMCR);
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500299 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
Andy Fleming09c04c22011-03-22 22:49:13 -0500300 uec_phy_write(mii_info, MII_BMCR, ctl);
Dave Liu7737d5c2006-11-03 12:11:15 -0600301}
302
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100303static int gbit_config_aneg (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600304{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100305 u16 adv;
306 u32 advertise;
Dave Liu7737d5c2006-11-03 12:11:15 -0600307
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100308 if (mii_info->autoneg) {
309 /* Configure the ADVERTISE register */
310 config_genmii_advert (mii_info);
311 advertise = mii_info->advertising;
Dave Liu7737d5c2006-11-03 12:11:15 -0600312
Andy Fleming09c04c22011-03-22 22:49:13 -0500313 adv = uec_phy_read(mii_info, MII_CTRL1000);
Kumar Gala2b21ec92011-01-19 03:36:40 -0600314 adv &= ~(ADVERTISE_1000FULL |
315 ADVERTISE_1000HALF);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100316 if (advertise & SUPPORTED_1000baseT_Half)
Kumar Gala2b21ec92011-01-19 03:36:40 -0600317 adv |= ADVERTISE_1000HALF;
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100318 if (advertise & SUPPORTED_1000baseT_Full)
Kumar Gala2b21ec92011-01-19 03:36:40 -0600319 adv |= ADVERTISE_1000FULL;
Andy Fleming09c04c22011-03-22 22:49:13 -0500320 uec_phy_write(mii_info, MII_CTRL1000, adv);
Dave Liu7737d5c2006-11-03 12:11:15 -0600321
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100322 /* Start/Restart aneg */
323 genmii_restart_aneg (mii_info);
324 } else
325 genmii_setup_forced (mii_info);
Dave Liu7737d5c2006-11-03 12:11:15 -0600326
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100327 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600328}
329
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100330static int marvell_config_aneg (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600331{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100332 /* The Marvell PHY has an errata which requires
333 * that certain registers get written in order
334 * to restart autonegotiation */
Andy Fleming09c04c22011-03-22 22:49:13 -0500335 uec_phy_write(mii_info, MII_BMCR, BMCR_RESET);
Dave Liu7737d5c2006-11-03 12:11:15 -0600336
Andy Fleming09c04c22011-03-22 22:49:13 -0500337 uec_phy_write(mii_info, 0x1d, 0x1f);
338 uec_phy_write(mii_info, 0x1e, 0x200c);
339 uec_phy_write(mii_info, 0x1d, 0x5);
340 uec_phy_write(mii_info, 0x1e, 0);
341 uec_phy_write(mii_info, 0x1e, 0x100);
Dave Liu7737d5c2006-11-03 12:11:15 -0600342
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100343 gbit_config_aneg (mii_info);
Dave Liu7737d5c2006-11-03 12:11:15 -0600344
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100345 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600346}
347
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100348static int genmii_config_aneg (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600349{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100350 if (mii_info->autoneg) {
Joakim Tjernlundf29c1812010-08-10 16:36:49 +0200351 /* Speed up the common case, if link is already up, speed and
352 duplex match, skip auto neg as it already matches */
353 if (!genmii_read_status(mii_info) && mii_info->link)
354 if (mii_info->duplex == DUPLEX_FULL &&
355 mii_info->speed == SPEED_100)
356 if (mii_info->advertising &
357 ADVERTISED_100baseT_Full)
358 return 0;
359
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100360 config_genmii_advert (mii_info);
361 genmii_restart_aneg (mii_info);
362 } else
363 genmii_setup_forced (mii_info);
Dave Liu7737d5c2006-11-03 12:11:15 -0600364
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100365 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600366}
367
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100368static int genmii_update_link (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600369{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100370 u16 status;
Dave Liu7737d5c2006-11-03 12:11:15 -0600371
Kim Phillipsee62ed32008-01-15 14:11:00 -0600372 /* Status is read once to clear old link state */
Andy Fleming09c04c22011-03-22 22:49:13 -0500373 uec_phy_read(mii_info, MII_BMSR);
Dave Liu7737d5c2006-11-03 12:11:15 -0600374
Kim Phillipsee62ed32008-01-15 14:11:00 -0600375 /*
376 * Wait if the link is up, and autonegotiation is in progress
377 * (ie - we're capable and it's not done)
378 */
Andy Fleming09c04c22011-03-22 22:49:13 -0500379 status = uec_phy_read(mii_info, MII_BMSR);
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500380 if ((status & BMSR_LSTATUS) && (status & BMSR_ANEGCAPABLE)
381 && !(status & BMSR_ANEGCOMPLETE)) {
Kim Phillipsee62ed32008-01-15 14:11:00 -0600382 int i = 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600383
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500384 while (!(status & BMSR_ANEGCOMPLETE)) {
Kim Phillipsee62ed32008-01-15 14:11:00 -0600385 /*
386 * Timeout reached ?
387 */
388 if (i > UGETH_AN_TIMEOUT) {
389 mii_info->link = 0;
390 return 0;
391 }
392
Kim Phillipsf30b61542008-02-27 16:08:22 -0600393 i++;
Kim Phillipsee62ed32008-01-15 14:11:00 -0600394 udelay(1000); /* 1 ms */
Andy Fleming09c04c22011-03-22 22:49:13 -0500395 status = uec_phy_read(mii_info, MII_BMSR);
Kim Phillipsee62ed32008-01-15 14:11:00 -0600396 }
397 mii_info->link = 1;
Kim Phillipsee62ed32008-01-15 14:11:00 -0600398 } else {
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500399 if (status & BMSR_LSTATUS)
Kim Phillipsee62ed32008-01-15 14:11:00 -0600400 mii_info->link = 1;
401 else
402 mii_info->link = 0;
403 }
Dave Liu7737d5c2006-11-03 12:11:15 -0600404
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100405 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600406}
407
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100408static int genmii_read_status (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600409{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100410 u16 status;
411 int err;
Dave Liu7737d5c2006-11-03 12:11:15 -0600412
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100413 /* Update the link, but return if there
414 * was an error */
415 err = genmii_update_link (mii_info);
416 if (err)
417 return err;
Dave Liu7737d5c2006-11-03 12:11:15 -0600418
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100419 if (mii_info->autoneg) {
Andy Fleming09c04c22011-03-22 22:49:13 -0500420 status = uec_phy_read(mii_info, MII_STAT1000);
Dave Liu7737d5c2006-11-03 12:11:15 -0600421
Anton Vorontsov91cdaa32008-03-24 20:46:24 +0300422 if (status & (LPA_1000FULL | LPA_1000HALF)) {
423 mii_info->speed = SPEED_1000;
424 if (status & LPA_1000FULL)
425 mii_info->duplex = DUPLEX_FULL;
426 else
427 mii_info->duplex = DUPLEX_HALF;
428 } else {
Andy Fleming09c04c22011-03-22 22:49:13 -0500429 status = uec_phy_read(mii_info, MII_LPA);
Anton Vorontsov91cdaa32008-03-24 20:46:24 +0300430
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500431 if (status & (LPA_10FULL | LPA_100FULL))
Anton Vorontsov91cdaa32008-03-24 20:46:24 +0300432 mii_info->duplex = DUPLEX_FULL;
433 else
434 mii_info->duplex = DUPLEX_HALF;
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500435 if (status & (LPA_100FULL | LPA_100HALF))
Anton Vorontsov91cdaa32008-03-24 20:46:24 +0300436 mii_info->speed = SPEED_100;
437 else
438 mii_info->speed = SPEED_10;
439 }
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100440 mii_info->pause = 0;
441 }
442 /* On non-aneg, we assume what we put in BMCR is the speed,
443 * though magic-aneg shouldn't prevent this case from occurring
444 */
Dave Liu7737d5c2006-11-03 12:11:15 -0600445
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100446 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600447}
448
Anton Vorontsov300615d2008-03-24 20:46:34 +0300449static int bcm_init(struct uec_mii_info *mii_info)
450{
451 struct eth_device *edev = mii_info->dev;
452 uec_private_t *uec = edev->priv;
453
454 gbit_config_aneg(mii_info);
455
Andy Fleming865ff852011-04-13 00:37:12 -0500456 if ((uec->uec_info->enet_interface_type ==
457 PHY_INTERFACE_MODE_RGMII_RXID) &&
458 (uec->uec_info->speed == SPEED_1000)) {
Anton Vorontsov300615d2008-03-24 20:46:34 +0300459 u16 val;
460 int cnt = 50;
461
462 /* Wait for aneg to complete. */
463 do
Andy Fleming09c04c22011-03-22 22:49:13 -0500464 val = uec_phy_read(mii_info, MII_BMSR);
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500465 while (--cnt && !(val & BMSR_ANEGCOMPLETE));
Anton Vorontsov300615d2008-03-24 20:46:34 +0300466
467 /* Set RDX clk delay. */
Andy Fleming09c04c22011-03-22 22:49:13 -0500468 uec_phy_write(mii_info, 0x18, 0x7 | (7 << 12));
Anton Vorontsov300615d2008-03-24 20:46:34 +0300469
Andy Fleming09c04c22011-03-22 22:49:13 -0500470 val = uec_phy_read(mii_info, 0x18);
Anton Vorontsov300615d2008-03-24 20:46:34 +0300471 /* Set RDX-RXC skew. */
472 val |= (1 << 8);
473 val |= (7 | (7 << 12));
474 /* Write bits 14:0. */
475 val |= (1 << 15);
Andy Fleming09c04c22011-03-22 22:49:13 -0500476 uec_phy_write(mii_info, 0x18, val);
Anton Vorontsov300615d2008-03-24 20:46:34 +0300477 }
478
479 return 0;
480}
481
Andy Fleming09c04c22011-03-22 22:49:13 -0500482static int uec_marvell_init(struct uec_mii_info *mii_info)
Haiying Wang41410ee2008-09-24 11:42:12 -0500483{
484 struct eth_device *edev = mii_info->dev;
485 uec_private_t *uec = edev->priv;
Andy Fleming865ff852011-04-13 00:37:12 -0500486 phy_interface_t iface = uec->uec_info->enet_interface_type;
Heiko Schocher582c55a2010-01-20 09:04:28 +0100487 int speed = uec->uec_info->speed;
Haiying Wang41410ee2008-09-24 11:42:12 -0500488
Andy Fleming865ff852011-04-13 00:37:12 -0500489 if ((speed == SPEED_1000) &&
490 (iface == PHY_INTERFACE_MODE_RGMII_ID ||
491 iface == PHY_INTERFACE_MODE_RGMII_RXID ||
492 iface == PHY_INTERFACE_MODE_RGMII_TXID)) {
Haiying Wang41410ee2008-09-24 11:42:12 -0500493 int temp;
494
Andy Fleming09c04c22011-03-22 22:49:13 -0500495 temp = uec_phy_read(mii_info, MII_M1111_PHY_EXT_CR);
Andy Fleming865ff852011-04-13 00:37:12 -0500496 if (iface == PHY_INTERFACE_MODE_RGMII_ID) {
Anton Vorontsov6185f802009-09-16 23:21:53 +0400497 temp |= MII_M1111_RX_DELAY | MII_M1111_TX_DELAY;
Andy Fleming865ff852011-04-13 00:37:12 -0500498 } else if (iface == PHY_INTERFACE_MODE_RGMII_RXID) {
Anton Vorontsov6185f802009-09-16 23:21:53 +0400499 temp &= ~MII_M1111_TX_DELAY;
500 temp |= MII_M1111_RX_DELAY;
Andy Fleming865ff852011-04-13 00:37:12 -0500501 } else if (iface == PHY_INTERFACE_MODE_RGMII_TXID) {
Anton Vorontsov6185f802009-09-16 23:21:53 +0400502 temp &= ~MII_M1111_RX_DELAY;
503 temp |= MII_M1111_TX_DELAY;
504 }
Andy Fleming09c04c22011-03-22 22:49:13 -0500505 uec_phy_write(mii_info, MII_M1111_PHY_EXT_CR, temp);
Haiying Wang41410ee2008-09-24 11:42:12 -0500506
Andy Fleming09c04c22011-03-22 22:49:13 -0500507 temp = uec_phy_read(mii_info, MII_M1111_PHY_EXT_SR);
Haiying Wang41410ee2008-09-24 11:42:12 -0500508 temp &= ~MII_M1111_HWCFG_MODE_MASK;
509 temp |= MII_M1111_HWCFG_MODE_RGMII;
Andy Fleming09c04c22011-03-22 22:49:13 -0500510 uec_phy_write(mii_info, MII_M1111_PHY_EXT_SR, temp);
Haiying Wang41410ee2008-09-24 11:42:12 -0500511
Andy Fleming09c04c22011-03-22 22:49:13 -0500512 uec_phy_write(mii_info, MII_BMCR, BMCR_RESET);
Haiying Wang41410ee2008-09-24 11:42:12 -0500513 }
514
515 return 0;
516}
517
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100518static int marvell_read_status (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600519{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100520 u16 status;
521 int err;
Dave Liu7737d5c2006-11-03 12:11:15 -0600522
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100523 /* Update the link, but return if there
524 * was an error */
525 err = genmii_update_link (mii_info);
526 if (err)
527 return err;
Dave Liu7737d5c2006-11-03 12:11:15 -0600528
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100529 /* If the link is up, read the speed and duplex */
530 /* If we aren't autonegotiating, assume speeds
531 * are as set */
532 if (mii_info->autoneg && mii_info->link) {
533 int speed;
Dave Liu7737d5c2006-11-03 12:11:15 -0600534
Andy Fleming09c04c22011-03-22 22:49:13 -0500535 status = uec_phy_read(mii_info, MII_M1011_PHY_SPEC_STATUS);
Dave Liu7737d5c2006-11-03 12:11:15 -0600536
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100537 /* Get the duplexity */
538 if (status & MII_M1011_PHY_SPEC_STATUS_FULLDUPLEX)
539 mii_info->duplex = DUPLEX_FULL;
540 else
541 mii_info->duplex = DUPLEX_HALF;
Dave Liu7737d5c2006-11-03 12:11:15 -0600542
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100543 /* Get the speed */
544 speed = status & MII_M1011_PHY_SPEC_STATUS_SPD_MASK;
545 switch (speed) {
546 case MII_M1011_PHY_SPEC_STATUS_1000:
547 mii_info->speed = SPEED_1000;
548 break;
549 case MII_M1011_PHY_SPEC_STATUS_100:
550 mii_info->speed = SPEED_100;
551 break;
552 default:
553 mii_info->speed = SPEED_10;
554 break;
555 }
556 mii_info->pause = 0;
557 }
558
559 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600560}
561
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100562static int marvell_ack_interrupt (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600563{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100564 /* Clear the interrupts by reading the reg */
Andy Fleming09c04c22011-03-22 22:49:13 -0500565 uec_phy_read(mii_info, MII_M1011_IEVENT);
Dave Liu7737d5c2006-11-03 12:11:15 -0600566
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100567 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600568}
569
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100570static int marvell_config_intr (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600571{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100572 if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
Andy Fleming09c04c22011-03-22 22:49:13 -0500573 uec_phy_write(mii_info, MII_M1011_IMASK, MII_M1011_IMASK_INIT);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100574 else
Andy Fleming09c04c22011-03-22 22:49:13 -0500575 uec_phy_write(mii_info, MII_M1011_IMASK,
576 MII_M1011_IMASK_CLEAR);
Dave Liu7737d5c2006-11-03 12:11:15 -0600577
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100578 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600579}
580
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100581static int dm9161_init (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600582{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100583 /* Reset the PHY */
Andy Fleming09c04c22011-03-22 22:49:13 -0500584 uec_phy_write(mii_info, MII_BMCR, uec_phy_read(mii_info, MII_BMCR) |
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500585 BMCR_RESET);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100586 /* PHY and MAC connect */
Andy Fleming09c04c22011-03-22 22:49:13 -0500587 uec_phy_write(mii_info, MII_BMCR, uec_phy_read(mii_info, MII_BMCR) &
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500588 ~BMCR_ISOLATE);
Kim Phillipsee62ed32008-01-15 14:11:00 -0600589
Andy Fleming09c04c22011-03-22 22:49:13 -0500590 uec_phy_write(mii_info, MII_DM9161_SCR, MII_DM9161_SCR_INIT);
Kim Phillipsee62ed32008-01-15 14:11:00 -0600591
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100592 config_genmii_advert (mii_info);
593 /* Start/restart aneg */
594 genmii_config_aneg (mii_info);
Dave Liu7737d5c2006-11-03 12:11:15 -0600595
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100596 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600597}
598
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100599static int dm9161_config_aneg (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600600{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100601 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600602}
603
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100604static int dm9161_read_status (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600605{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100606 u16 status;
607 int err;
Dave Liu7737d5c2006-11-03 12:11:15 -0600608
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100609 /* Update the link, but return if there was an error */
610 err = genmii_update_link (mii_info);
611 if (err)
612 return err;
613 /* If the link is up, read the speed and duplex
614 If we aren't autonegotiating assume speeds are as set */
615 if (mii_info->autoneg && mii_info->link) {
Andy Fleming09c04c22011-03-22 22:49:13 -0500616 status = uec_phy_read(mii_info, MII_DM9161_SCSR);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100617 if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_100H))
618 mii_info->speed = SPEED_100;
619 else
620 mii_info->speed = SPEED_10;
Dave Liu7737d5c2006-11-03 12:11:15 -0600621
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100622 if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_10F))
623 mii_info->duplex = DUPLEX_FULL;
624 else
625 mii_info->duplex = DUPLEX_HALF;
626 }
Dave Liu7737d5c2006-11-03 12:11:15 -0600627
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100628 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600629}
630
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100631static int dm9161_ack_interrupt (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600632{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100633 /* Clear the interrupt by reading the reg */
Andy Fleming09c04c22011-03-22 22:49:13 -0500634 uec_phy_read(mii_info, MII_DM9161_INTR);
Dave Liu7737d5c2006-11-03 12:11:15 -0600635
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100636 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600637}
638
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100639static int dm9161_config_intr (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600640{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100641 if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
Andy Fleming09c04c22011-03-22 22:49:13 -0500642 uec_phy_write(mii_info, MII_DM9161_INTR, MII_DM9161_INTR_INIT);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100643 else
Andy Fleming09c04c22011-03-22 22:49:13 -0500644 uec_phy_write(mii_info, MII_DM9161_INTR, MII_DM9161_INTR_STOP);
Dave Liu7737d5c2006-11-03 12:11:15 -0600645
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100646 return 0;
Dave Liu7737d5c2006-11-03 12:11:15 -0600647}
648
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100649static void dm9161_close (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600650{
651}
652
Richard Retanubunedf3fe72008-10-23 09:08:18 -0400653static int fixed_phy_aneg (struct uec_mii_info *mii_info)
654{
655 mii_info->autoneg = 0; /* Turn off auto negotiation for fixed phy */
656 return 0;
657}
658
659static int fixed_phy_read_status (struct uec_mii_info *mii_info)
660{
661 int i = 0;
662
663 for (i = 0; i < ARRAY_SIZE(fixed_phy_port); i++) {
Richard Retanubun1443cd72009-07-01 14:04:05 -0400664 if (strncmp(mii_info->dev->name, fixed_phy_port[i].name,
665 strlen(mii_info->dev->name)) == 0) {
Richard Retanubunedf3fe72008-10-23 09:08:18 -0400666 mii_info->speed = fixed_phy_port[i].speed;
667 mii_info->duplex = fixed_phy_port[i].duplex;
668 mii_info->link = 1; /* Link is always UP */
669 mii_info->pause = 0;
670 break;
671 }
672 }
673 return 0;
674}
675
Heiko Schocher8b69b562008-11-20 09:57:14 +0100676static int smsc_config_aneg (struct uec_mii_info *mii_info)
677{
678 return 0;
679}
680
681static int smsc_read_status (struct uec_mii_info *mii_info)
682{
683 u16 status;
684 int err;
685
686 /* Update the link, but return if there
687 * was an error */
688 err = genmii_update_link (mii_info);
689 if (err)
690 return err;
691
692 /* If the link is up, read the speed and duplex */
693 /* If we aren't autonegotiating, assume speeds
694 * are as set */
695 if (mii_info->autoneg && mii_info->link) {
696 int val;
697
Andy Fleming09c04c22011-03-22 22:49:13 -0500698 status = uec_phy_read(mii_info, 0x1f);
Heiko Schocher8b69b562008-11-20 09:57:14 +0100699 val = (status & 0x1c) >> 2;
700
701 switch (val) {
702 case 1:
703 mii_info->duplex = DUPLEX_HALF;
704 mii_info->speed = SPEED_10;
705 break;
706 case 5:
707 mii_info->duplex = DUPLEX_FULL;
708 mii_info->speed = SPEED_10;
709 break;
710 case 2:
711 mii_info->duplex = DUPLEX_HALF;
712 mii_info->speed = SPEED_100;
713 break;
714 case 6:
715 mii_info->duplex = DUPLEX_FULL;
716 mii_info->speed = SPEED_100;
717 break;
718 }
719 mii_info->pause = 0;
720 }
721
722 return 0;
723}
724
Dave Liu7737d5c2006-11-03 12:11:15 -0600725static struct phy_info phy_info_dm9161 = {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100726 .phy_id = 0x0181b880,
727 .phy_id_mask = 0x0ffffff0,
728 .name = "Davicom DM9161E",
729 .init = dm9161_init,
730 .config_aneg = dm9161_config_aneg,
731 .read_status = dm9161_read_status,
732 .close = dm9161_close,
Dave Liu7737d5c2006-11-03 12:11:15 -0600733};
734
735static struct phy_info phy_info_dm9161a = {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100736 .phy_id = 0x0181b8a0,
737 .phy_id_mask = 0x0ffffff0,
738 .name = "Davicom DM9161A",
739 .features = MII_BASIC_FEATURES,
740 .init = dm9161_init,
741 .config_aneg = dm9161_config_aneg,
742 .read_status = dm9161_read_status,
743 .ack_interrupt = dm9161_ack_interrupt,
744 .config_intr = dm9161_config_intr,
745 .close = dm9161_close,
Dave Liu7737d5c2006-11-03 12:11:15 -0600746};
747
748static struct phy_info phy_info_marvell = {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100749 .phy_id = 0x01410c00,
750 .phy_id_mask = 0xffffff00,
751 .name = "Marvell 88E11x1",
752 .features = MII_GBIT_FEATURES,
Andy Fleming09c04c22011-03-22 22:49:13 -0500753 .init = &uec_marvell_init,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100754 .config_aneg = &marvell_config_aneg,
755 .read_status = &marvell_read_status,
756 .ack_interrupt = &marvell_ack_interrupt,
757 .config_intr = &marvell_config_intr,
Dave Liu7737d5c2006-11-03 12:11:15 -0600758};
759
Anton Vorontsov300615d2008-03-24 20:46:34 +0300760static struct phy_info phy_info_bcm5481 = {
761 .phy_id = 0x0143bca0,
762 .phy_id_mask = 0xffffff0,
763 .name = "Broadcom 5481",
764 .features = MII_GBIT_FEATURES,
765 .read_status = genmii_read_status,
766 .init = bcm_init,
767};
768
Richard Retanubunedf3fe72008-10-23 09:08:18 -0400769static struct phy_info phy_info_fixedphy = {
770 .phy_id = CONFIG_FIXED_PHY,
771 .phy_id_mask = CONFIG_FIXED_PHY,
772 .name = "Fixed PHY",
773 .config_aneg = fixed_phy_aneg,
774 .read_status = fixed_phy_read_status,
775};
776
Heiko Schocher8b69b562008-11-20 09:57:14 +0100777static struct phy_info phy_info_smsclan8700 = {
778 .phy_id = 0x0007c0c0,
779 .phy_id_mask = 0xfffffff0,
780 .name = "SMSC LAN8700",
781 .features = MII_BASIC_FEATURES,
782 .config_aneg = smsc_config_aneg,
783 .read_status = smsc_read_status,
784};
785
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100786static struct phy_info phy_info_genmii = {
787 .phy_id = 0x00000000,
788 .phy_id_mask = 0x00000000,
789 .name = "Generic MII",
790 .features = MII_BASIC_FEATURES,
791 .config_aneg = genmii_config_aneg,
792 .read_status = genmii_read_status,
Dave Liu7737d5c2006-11-03 12:11:15 -0600793};
794
795static struct phy_info *phy_info[] = {
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100796 &phy_info_dm9161,
797 &phy_info_dm9161a,
798 &phy_info_marvell,
Anton Vorontsov300615d2008-03-24 20:46:34 +0300799 &phy_info_bcm5481,
Heiko Schocher8b69b562008-11-20 09:57:14 +0100800 &phy_info_smsclan8700,
Richard Retanubunedf3fe72008-10-23 09:08:18 -0400801 &phy_info_fixedphy,
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100802 &phy_info_genmii,
803 NULL
Dave Liu7737d5c2006-11-03 12:11:15 -0600804};
805
Andy Fleming09c04c22011-03-22 22:49:13 -0500806u16 uec_phy_read(struct uec_mii_info *mii_info, u16 regnum)
Dave Liu7737d5c2006-11-03 12:11:15 -0600807{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100808 return mii_info->mdio_read (mii_info->dev, mii_info->mii_id, regnum);
Dave Liu7737d5c2006-11-03 12:11:15 -0600809}
810
Andy Fleming09c04c22011-03-22 22:49:13 -0500811void uec_phy_write(struct uec_mii_info *mii_info, u16 regnum, u16 val)
Dave Liu7737d5c2006-11-03 12:11:15 -0600812{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100813 mii_info->mdio_write (mii_info->dev, mii_info->mii_id, regnum, val);
Dave Liu7737d5c2006-11-03 12:11:15 -0600814}
815
816/* Use the PHY ID registers to determine what type of PHY is attached
817 * to device dev. return a struct phy_info structure describing that PHY
818 */
Andy Flemingda9d4612007-08-14 00:14:25 -0500819struct phy_info *uec_get_phy_info (struct uec_mii_info *mii_info)
Dave Liu7737d5c2006-11-03 12:11:15 -0600820{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100821 u16 phy_reg;
822 u32 phy_ID;
823 int i;
824 struct phy_info *theInfo = NULL;
Dave Liu7737d5c2006-11-03 12:11:15 -0600825
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100826 /* Grab the bits from PHYIR1, and put them in the upper half */
Andy Fleming09c04c22011-03-22 22:49:13 -0500827 phy_reg = uec_phy_read(mii_info, MII_PHYSID1);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100828 phy_ID = (phy_reg & 0xffff) << 16;
Dave Liu7737d5c2006-11-03 12:11:15 -0600829
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100830 /* Grab the bits from PHYIR2, and put them in the lower half */
Andy Fleming09c04c22011-03-22 22:49:13 -0500831 phy_reg = uec_phy_read(mii_info, MII_PHYSID2);
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100832 phy_ID |= (phy_reg & 0xffff);
Dave Liu7737d5c2006-11-03 12:11:15 -0600833
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100834 /* loop through all the known PHY types, and find one that */
835 /* matches the ID we read from the PHY. */
836 for (i = 0; phy_info[i]; i++)
837 if (phy_info[i]->phy_id ==
838 (phy_ID & phy_info[i]->phy_id_mask)) {
839 theInfo = phy_info[i];
840 break;
841 }
Dave Liu7737d5c2006-11-03 12:11:15 -0600842
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100843 /* This shouldn't happen, as we have generic PHY support */
844 if (theInfo == NULL) {
845 ugphy_info ("UEC: PHY id %x is not supported!", phy_ID);
846 return NULL;
847 } else {
848 ugphy_info ("UEC: PHY is %s (%x)", theInfo->name, phy_ID);
849 }
Dave Liu7737d5c2006-11-03 12:11:15 -0600850
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100851 return theInfo;
Dave Liu7737d5c2006-11-03 12:11:15 -0600852}
853
Andy Fleming865ff852011-04-13 00:37:12 -0500854void marvell_phy_interface_mode(struct eth_device *dev, phy_interface_t type,
855 int speed)
Dave Liu7737d5c2006-11-03 12:11:15 -0600856{
Wolfgang Denkdd520bf2006-11-30 18:02:20 +0100857 uec_private_t *uec = (uec_private_t *) dev->priv;
858 struct uec_mii_info *mii_info;
Kim Phillipsf655ade2008-02-27 15:06:39 -0600859 u16 status;
Dave Liu7737d5c2006-11-03 12:11:15 -0600860
861 if (!uec->mii_info) {
Kim Phillipsf30b61542008-02-27 16:08:22 -0600862 printf ("%s: the PHY not initialized\n", __FUNCTION__);
Dave Liu7737d5c2006-11-03 12:11:15 -0600863 return;
864 }
865 mii_info = uec->mii_info;
866
Andy Fleming865ff852011-04-13 00:37:12 -0500867 if (type == PHY_INTERFACE_MODE_RGMII) {
868 if (speed == SPEED_100) {
Andy Fleming09c04c22011-03-22 22:49:13 -0500869 uec_phy_write(mii_info, 0x00, 0x9140);
870 uec_phy_write(mii_info, 0x1d, 0x001f);
871 uec_phy_write(mii_info, 0x1e, 0x200c);
872 uec_phy_write(mii_info, 0x1d, 0x0005);
873 uec_phy_write(mii_info, 0x1e, 0x0000);
874 uec_phy_write(mii_info, 0x1e, 0x0100);
875 uec_phy_write(mii_info, 0x09, 0x0e00);
876 uec_phy_write(mii_info, 0x04, 0x01e1);
877 uec_phy_write(mii_info, 0x00, 0x9140);
878 uec_phy_write(mii_info, 0x00, 0x1000);
Heiko Schocher582c55a2010-01-20 09:04:28 +0100879 udelay (100000);
Andy Fleming09c04c22011-03-22 22:49:13 -0500880 uec_phy_write(mii_info, 0x00, 0x2900);
881 uec_phy_write(mii_info, 0x14, 0x0cd2);
882 uec_phy_write(mii_info, 0x00, 0xa100);
883 uec_phy_write(mii_info, 0x09, 0x0000);
884 uec_phy_write(mii_info, 0x1b, 0x800b);
885 uec_phy_write(mii_info, 0x04, 0x05e1);
886 uec_phy_write(mii_info, 0x00, 0xa100);
887 uec_phy_write(mii_info, 0x00, 0x2100);
Heiko Schocher582c55a2010-01-20 09:04:28 +0100888 udelay (1000000);
Andy Fleming865ff852011-04-13 00:37:12 -0500889 } else if (speed == SPEED_10) {
Andy Fleming09c04c22011-03-22 22:49:13 -0500890 uec_phy_write(mii_info, 0x14, 0x8e40);
891 uec_phy_write(mii_info, 0x1b, 0x800b);
892 uec_phy_write(mii_info, 0x14, 0x0c82);
893 uec_phy_write(mii_info, 0x00, 0x8100);
Heiko Schocher582c55a2010-01-20 09:04:28 +0100894 udelay (1000000);
895 }
Dave Liu7737d5c2006-11-03 12:11:15 -0600896 }
Kim Phillipsf655ade2008-02-27 15:06:39 -0600897
898 /* handle 88e1111 rev.B2 erratum 5.6 */
899 if (mii_info->autoneg) {
Andy Fleming09c04c22011-03-22 22:49:13 -0500900 status = uec_phy_read(mii_info, MII_BMCR);
901 uec_phy_write(mii_info, MII_BMCR, status | BMCR_ANENABLE);
Kim Phillipsf655ade2008-02-27 15:06:39 -0600902 }
903 /* now the B2 will correctly report autoneg completion status */
Dave Liu7737d5c2006-11-03 12:11:15 -0600904}
905
Heiko Schocher582c55a2010-01-20 09:04:28 +0100906void change_phy_interface_mode (struct eth_device *dev,
Andy Fleming865ff852011-04-13 00:37:12 -0500907 phy_interface_t type, int speed)
Dave Liu7737d5c2006-11-03 12:11:15 -0600908{
909#ifdef CONFIG_PHY_MODE_NEED_CHANGE
Heiko Schocher582c55a2010-01-20 09:04:28 +0100910 marvell_phy_interface_mode (dev, type, speed);
Dave Liu7737d5c2006-11-03 12:11:15 -0600911#endif
912}