3 * Marvell Semiconductor <www.marvell.com>
4 * Prafulla Wadaskar <prafulla@marvell.com>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 #include "mv88e61xx.h"
29 #ifdef CONFIG_MV88E61XX_MULTICHIP_ADRMODE
31 * The Switch support two modes of operation
32 * 1. single chip mode and
34 * Refer section 9.2 &9.3 in chip datasheet-02 for more details
36 * By default single chip mode is configured
37 * multichip mode operation can be configured in board header
39 static int mv88e61xx_busychk_multic(char *name, u32 devaddr)
42 u32 timeout = MV88E61XX_PHY_TIMEOUT;
44 /* Poll till SMIBusy bit is clear */
46 miiphy_read(name, devaddr, 0x0, ®);
48 printf("SMI busy timeout\n");
51 } while (reg & (1 << 15));
55 static void mv88e61xx_wr_phy(char *name, u32 phy_adr, u32 reg_ofs, u16 data)
59 /* command to read PHY dev address */
60 if (miiphy_read(name, 0xEE, 0xEE, &mii_dev_addr)) {
61 printf("Error..could not read PHY dev address\n");
64 mv88e61xx_busychk_multic(name, mii_dev_addr);
65 /* Write data to Switch indirect data register */
66 miiphy_write(name, mii_dev_addr, 0x1, data);
67 /* Write command to Switch indirect command register (write) */
68 miiphy_write(name, mii_dev_addr, 0x0,
69 reg_ofs | (phy_adr << 5) | (1 << 10) | (1 << 12) | (1 <<
73 static void mv88e61xx_rd_phy(char *name, u32 phy_adr, u32 reg_ofs, u16 * data)
77 /* command to read PHY dev address */
78 if (miiphy_read(name, 0xEE, 0xEE, &mii_dev_addr)) {
79 printf("Error..could not read PHY dev address\n");
82 mv88e61xx_busychk_multic(name, mii_dev_addr);
83 /* Write command to Switch indirect command register (read) */
84 miiphy_write(name, mii_dev_addr, 0x0,
85 reg_ofs | (phy_adr << 5) | (1 << 11) | (1 << 12) | (1 <<
87 mv88e61xx_busychk_multic(name, mii_dev_addr);
88 /* Read data from Switch indirect data register */
89 miiphy_read(name, mii_dev_addr, 0x1, data);
91 #endif /* CONFIG_MV88E61XX_MULTICHIP_ADRMODE */
93 static void mv88e61xx_port_vlan_config(struct mv88e61xx_config *swconfig,
94 u32 max_prtnum, u32 ports_ofs)
98 char *name = swconfig->name;
99 u32 cpu_port = swconfig->cpuport;
100 u32 port_mask = swconfig->ports_enabled;
101 enum mv88e61xx_cfg_vlan vlancfg = swconfig->vlancfg;
103 /* be sure all ports are disabled */
104 for (prt = 0; prt < max_prtnum; prt++) {
105 RD_PHY(name, ports_ofs + prt, MV88E61XX_PRT_CTRL_REG, ®);
107 WR_PHY(name, ports_ofs + prt, MV88E61XX_PRT_CTRL_REG, reg);
109 if (!(cpu_port & (1 << prt)))
111 /* Set CPU port VID to 0x1 */
112 RD_PHY(name, (ports_ofs + prt), MV88E61XX_PRT_VID_REG, ®);
115 WR_PHY(name, (ports_ofs + prt), MV88E61XX_PRT_VID_REG, reg);
118 /* Setting Port default priority for all ports to zero */
119 for (prt = 0; prt < max_prtnum; prt++) {
120 RD_PHY(name, ports_ofs + prt, MV88E61XX_PRT_VID_REG, ®);
122 WR_PHY(name, ports_ofs + prt, MV88E61XX_PRT_VID_REG, reg);
124 /* Setting VID and VID map for all ports except CPU port */
125 for (prt = 0; prt < max_prtnum; prt++) {
126 /* only for enabled ports */
127 if ((1 << prt) & port_mask) {
129 if ((1 << prt) & cpu_port) {
131 * Set Vlan map table for cpu_port to see
134 RD_PHY(name, (ports_ofs + prt),
135 MV88E61XX_PRT_VMAP_REG, ®);
136 reg &= ~((1 << max_prtnum) - 1);
137 reg |= port_mask & ~(1 << prt);
138 WR_PHY(name, (ports_ofs + prt),
139 MV88E61XX_PRT_VMAP_REG, reg);
143 * set Ports VLAN Mapping.
144 * port prt <--> cpu_port VLAN #prt+1.
146 RD_PHY(name, ports_ofs + prt,
147 MV88E61XX_PRT_VID_REG, ®);
150 WR_PHY(name, ports_ofs + prt,
151 MV88E61XX_PRT_VID_REG, reg);
153 RD_PHY(name, ports_ofs + prt,
154 MV88E61XX_PRT_VMAP_REG, ®);
155 if (vlancfg == MV88E61XX_VLANCFG_DEFAULT) {
157 * all any port can send frames to all other ports
158 * ref: sec 3.2.1.1 of datasheet
162 } else if (vlancfg == MV88E61XX_VLANCFG_ROUTER) {
164 * all other ports can send frames to CPU port only
165 * ref: sec 3.2.1.2 of datasheet
167 reg &= ~((1 << max_prtnum) - 1);
170 WR_PHY(name, ports_ofs + prt,
171 MV88E61XX_PRT_VMAP_REG, reg);
177 * enable only appropriate ports to forwarding mode
178 * and disable the others
180 for (prt = 0; prt < max_prtnum; prt++) {
181 if ((1 << prt) & port_mask) {
182 RD_PHY(name, ports_ofs + prt,
183 MV88E61XX_PRT_CTRL_REG, ®);
185 WR_PHY(name, ports_ofs + prt,
186 MV88E61XX_PRT_CTRL_REG, reg);
189 RD_PHY(name, ports_ofs + prt,
190 MV88E61XX_PRT_CTRL_REG, ®);
192 WR_PHY(name, ports_ofs + prt,
193 MV88E61XX_PRT_CTRL_REG, reg);
199 * Make sure SMIBusy bit cleared before another
200 * SMI operation can take place
202 static int mv88e61xx_busychk(char *name)
205 u32 timeout = MV88E61XX_PHY_TIMEOUT;
207 RD_PHY(name, MV88E61XX_GLB2REG_DEVADR,
208 MV88E61XX_PHY_CMD, ®);
209 if (timeout-- == 0) {
210 printf("SMI busy timeout\n");
213 } while (reg & 1 << 15); /* busy mask */
218 * Power up the specified port and reset PHY
220 static int mv88361xx_powerup(struct mv88e61xx_config *swconfig, u32 prt)
222 char *name = swconfig->name;
224 /* Write Copper Specific control reg1 (0x14) for-
225 * Enable Phy power up
226 * Energy Detect on (sense&Xmit NLP Periodically
227 * reset other settings default
229 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, 0x3360);
230 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
231 MV88E61XX_PHY_CMD, (0x9410 | (prt << 5)));
233 if (mv88e61xx_busychk(name))
236 /* Write PHY ctrl reg (0x0) to apply
237 * Phy reset (set bit 15 low)
238 * reset other default values
240 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, 0x1140);
241 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
242 MV88E61XX_PHY_CMD, (0x9400 | (prt << 5)));
244 if (mv88e61xx_busychk(name))
251 * Default Setup for LED[0]_Control (ref: Table 46 Datasheet-3)
252 * is set to "On-1000Mb/s Link, Off Else"
253 * This function sets it to "On-Link, Blink-Activity, Off-NoLink"
255 * This is optional settings may be needed on some boards
256 * to setup PHY LEDs default configuration to detect 10/100/1000Mb/s
259 static int mv88361xx_led_init(struct mv88e61xx_config *swconfig, u32 prt)
261 char *name = swconfig->name;
264 if (swconfig->led_init != MV88E61XX_LED_INIT_EN)
267 /* set page address to 3 */
269 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
270 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
271 MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
272 1 << MV88E61XX_MODE_OFST |
273 1 << MV88E61XX_OP_OFST |
274 prt << MV88E61XX_ADDR_OFST | 22));
276 if (mv88e61xx_busychk(name))
279 /* set LED Func Ctrl reg */
280 reg = 1; /* LED[0] On-Link, Blink-Activity, Off-NoLink */
281 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
282 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
283 MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
284 1 << MV88E61XX_MODE_OFST |
285 1 << MV88E61XX_OP_OFST |
286 prt << MV88E61XX_ADDR_OFST | 16));
288 if (mv88e61xx_busychk(name))
291 /* set page address to 0 */
293 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
294 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
295 MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
296 1 << MV88E61XX_MODE_OFST |
297 1 << MV88E61XX_OP_OFST |
298 prt << MV88E61XX_ADDR_OFST | 22));
300 if (mv88e61xx_busychk(name))
307 * Reverse Transmit polarity for Media Dependent Interface
308 * Pins (MDIP) bits in Copper Specific Control Register 3
309 * (Page 0, Reg 20 for each phy (except cpu port)
310 * Reference: Section 1.1 Switch datasheet-3
312 * This is optional settings may be needed on some boards
313 * for PHY<->magnetics h/w tuning
315 static int mv88361xx_reverse_mdipn(struct mv88e61xx_config *swconfig, u32 prt)
317 char *name = swconfig->name;
320 if (swconfig->mdip != MV88E61XX_MDIP_REVERSE)
323 reg = 0x0f; /*Reverse MDIP/N[3:0] bits */
324 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, reg);
325 WR_PHY(name, MV88E61XX_GLB2REG_DEVADR,
326 MV88E61XX_PHY_CMD, (1 << MV88E61XX_BUSY_OFST |
327 1 << MV88E61XX_MODE_OFST |
328 1 << MV88E61XX_OP_OFST |
329 prt << MV88E61XX_ADDR_OFST | 20));
331 if (mv88e61xx_busychk(name))
338 * Marvell 88E61XX Switch initialization
340 int mv88e61xx_switch_initialize(struct mv88e61xx_config *swconfig)
345 char *name = swconfig->name;
347 if (miiphy_set_current_dev(name)) {
348 printf("%s failed\n", __FUNCTION__);
352 if (!(swconfig->cpuport & ((1 << 4) | (1 << 5)))) {
353 swconfig->cpuport = (1 << 5);
354 printf("Invalid cpu port config, using default port5\n");
357 RD_PHY(name, MV88E61XX_PRT_OFST, MII_PHYSID2, ®);
358 switch (reg &= 0xfff0) {
367 /* ports 2,3,4 not available */
368 swconfig->ports_enabled &= 0x023;
371 /* Could not detect switch id */
376 /* Port based VLANs configuration */
377 if ((swconfig->vlancfg == MV88E61XX_VLANCFG_DEFAULT)
378 || (swconfig->vlancfg == MV88E61XX_VLANCFG_ROUTER))
379 mv88e61xx_port_vlan_config(swconfig, MV88E61XX_MAX_PORTS_NUM,
382 printf("Unsupported mode %s failed\n", __FUNCTION__);
386 if (swconfig->rgmii_delay == MV88E61XX_RGMII_DELAY_EN) {
388 * Enable RGMII delay on Tx and Rx for CPU port
389 * Ref: sec 9.5 of chip datasheet-02
391 WR_PHY(name, MV88E61XX_PRT_OFST + 5,
392 MV88E61XX_RGMII_TIMECTRL_REG, 0x18);
393 WR_PHY(name, MV88E61XX_PRT_OFST + 4,
394 MV88E61XX_RGMII_TIMECTRL_REG, 0xc1e7);
397 for (prt = 0; prt < MV88E61XX_MAX_PORTS_NUM; prt++) {
398 if (!((1 << prt) & swconfig->cpuport)) {
400 if (mv88361xx_led_init(swconfig, prt))
402 if (mv88361xx_reverse_mdipn(swconfig, prt))
404 if (mv88361xx_powerup(swconfig, prt))
408 /*Program port state */
409 RD_PHY(name, MV88E61XX_PRT_OFST + prt,
410 MV88E61XX_PRT_CTRL_REG, ®);
411 WR_PHY(name, MV88E61XX_PRT_OFST + prt,
412 MV88E61XX_PRT_CTRL_REG,
413 reg | (swconfig->portstate & 0x03));
416 printf("%s Initialized on %s\n", idstr, name);