ARM: mm: Recreate kernel mappings in early_paging_init()
[linux/fpc-iii.git] / drivers / net / phy / marvell.c
blob2e91477362d4d70b15df3db59ad41dec990a3c57
1 /*
2 * drivers/net/phy/marvell.c
4 * Driver for Marvell PHYs
6 * Author: Andy Fleming
8 * Copyright (c) 2004 Freescale Semiconductor, Inc.
10 * Copyright (c) 2013 Michael Stapelberg <michael@stapelberg.de>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/unistd.h>
22 #include <linux/interrupt.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/spinlock.h>
29 #include <linux/mm.h>
30 #include <linux/module.h>
31 #include <linux/mii.h>
32 #include <linux/ethtool.h>
33 #include <linux/phy.h>
34 #include <linux/marvell_phy.h>
35 #include <linux/of.h>
37 #include <asm/io.h>
38 #include <asm/irq.h>
39 #include <asm/uaccess.h>
41 #define MII_MARVELL_PHY_PAGE 22
43 #define MII_M1011_IEVENT 0x13
44 #define MII_M1011_IEVENT_CLEAR 0x0000
46 #define MII_M1011_IMASK 0x12
47 #define MII_M1011_IMASK_INIT 0x6400
48 #define MII_M1011_IMASK_CLEAR 0x0000
50 #define MII_M1011_PHY_SCR 0x10
51 #define MII_M1011_PHY_SCR_AUTO_CROSS 0x0060
53 #define MII_M1145_PHY_EXT_CR 0x14
54 #define MII_M1145_RGMII_RX_DELAY 0x0080
55 #define MII_M1145_RGMII_TX_DELAY 0x0002
57 #define MII_M1111_PHY_LED_CONTROL 0x18
58 #define MII_M1111_PHY_LED_DIRECT 0x4100
59 #define MII_M1111_PHY_LED_COMBINE 0x411c
60 #define MII_M1111_PHY_EXT_CR 0x14
61 #define MII_M1111_RX_DELAY 0x80
62 #define MII_M1111_TX_DELAY 0x2
63 #define MII_M1111_PHY_EXT_SR 0x1b
65 #define MII_M1111_HWCFG_MODE_MASK 0xf
66 #define MII_M1111_HWCFG_MODE_COPPER_RGMII 0xb
67 #define MII_M1111_HWCFG_MODE_FIBER_RGMII 0x3
68 #define MII_M1111_HWCFG_MODE_SGMII_NO_CLK 0x4
69 #define MII_M1111_HWCFG_MODE_COPPER_RTBI 0x9
70 #define MII_M1111_HWCFG_FIBER_COPPER_AUTO 0x8000
71 #define MII_M1111_HWCFG_FIBER_COPPER_RES 0x2000
73 #define MII_M1111_COPPER 0
74 #define MII_M1111_FIBER 1
76 #define MII_88E1121_PHY_MSCR_PAGE 2
77 #define MII_88E1121_PHY_MSCR_REG 21
78 #define MII_88E1121_PHY_MSCR_RX_DELAY BIT(5)
79 #define MII_88E1121_PHY_MSCR_TX_DELAY BIT(4)
80 #define MII_88E1121_PHY_MSCR_DELAY_MASK (~(0x3 << 4))
82 #define MII_88E1318S_PHY_MSCR1_REG 16
83 #define MII_88E1318S_PHY_MSCR1_PAD_ODD BIT(6)
85 /* Copper Specific Interrupt Enable Register */
86 #define MII_88E1318S_PHY_CSIER 0x12
87 /* WOL Event Interrupt Enable */
88 #define MII_88E1318S_PHY_CSIER_WOL_EIE BIT(7)
90 /* LED Timer Control Register */
91 #define MII_88E1318S_PHY_LED_PAGE 0x03
92 #define MII_88E1318S_PHY_LED_TCR 0x12
93 #define MII_88E1318S_PHY_LED_TCR_FORCE_INT BIT(15)
94 #define MII_88E1318S_PHY_LED_TCR_INTn_ENABLE BIT(7)
95 #define MII_88E1318S_PHY_LED_TCR_INT_ACTIVE_LOW BIT(11)
97 /* Magic Packet MAC address registers */
98 #define MII_88E1318S_PHY_MAGIC_PACKET_WORD2 0x17
99 #define MII_88E1318S_PHY_MAGIC_PACKET_WORD1 0x18
100 #define MII_88E1318S_PHY_MAGIC_PACKET_WORD0 0x19
102 #define MII_88E1318S_PHY_WOL_PAGE 0x11
103 #define MII_88E1318S_PHY_WOL_CTRL 0x10
104 #define MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS BIT(12)
105 #define MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE BIT(14)
107 #define MII_88E1121_PHY_LED_CTRL 16
108 #define MII_88E1121_PHY_LED_PAGE 3
109 #define MII_88E1121_PHY_LED_DEF 0x0030
111 #define MII_M1011_PHY_STATUS 0x11
112 #define MII_M1011_PHY_STATUS_1000 0x8000
113 #define MII_M1011_PHY_STATUS_100 0x4000
114 #define MII_M1011_PHY_STATUS_SPD_MASK 0xc000
115 #define MII_M1011_PHY_STATUS_FULLDUPLEX 0x2000
116 #define MII_M1011_PHY_STATUS_RESOLVED 0x0800
117 #define MII_M1011_PHY_STATUS_LINK 0x0400
119 #define MII_M1116R_CONTROL_REG_MAC 21
122 MODULE_DESCRIPTION("Marvell PHY driver");
123 MODULE_AUTHOR("Andy Fleming");
124 MODULE_LICENSE("GPL");
126 static int marvell_ack_interrupt(struct phy_device *phydev)
128 int err;
130 /* Clear the interrupts by reading the reg */
131 err = phy_read(phydev, MII_M1011_IEVENT);
133 if (err < 0)
134 return err;
136 return 0;
139 static int marvell_config_intr(struct phy_device *phydev)
141 int err;
143 if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
144 err = phy_write(phydev, MII_M1011_IMASK, MII_M1011_IMASK_INIT);
145 else
146 err = phy_write(phydev, MII_M1011_IMASK, MII_M1011_IMASK_CLEAR);
148 return err;
151 static int marvell_config_aneg(struct phy_device *phydev)
153 int err;
155 /* The Marvell PHY has an errata which requires
156 * that certain registers get written in order
157 * to restart autonegotiation */
158 err = phy_write(phydev, MII_BMCR, BMCR_RESET);
160 if (err < 0)
161 return err;
163 err = phy_write(phydev, 0x1d, 0x1f);
164 if (err < 0)
165 return err;
167 err = phy_write(phydev, 0x1e, 0x200c);
168 if (err < 0)
169 return err;
171 err = phy_write(phydev, 0x1d, 0x5);
172 if (err < 0)
173 return err;
175 err = phy_write(phydev, 0x1e, 0);
176 if (err < 0)
177 return err;
179 err = phy_write(phydev, 0x1e, 0x100);
180 if (err < 0)
181 return err;
183 err = phy_write(phydev, MII_M1011_PHY_SCR,
184 MII_M1011_PHY_SCR_AUTO_CROSS);
185 if (err < 0)
186 return err;
188 err = phy_write(phydev, MII_M1111_PHY_LED_CONTROL,
189 MII_M1111_PHY_LED_DIRECT);
190 if (err < 0)
191 return err;
193 err = genphy_config_aneg(phydev);
194 if (err < 0)
195 return err;
197 if (phydev->autoneg != AUTONEG_ENABLE) {
198 int bmcr;
201 * A write to speed/duplex bits (that is performed by
202 * genphy_config_aneg() call above) must be followed by
203 * a software reset. Otherwise, the write has no effect.
205 bmcr = phy_read(phydev, MII_BMCR);
206 if (bmcr < 0)
207 return bmcr;
209 err = phy_write(phydev, MII_BMCR, bmcr | BMCR_RESET);
210 if (err < 0)
211 return err;
214 return 0;
217 #ifdef CONFIG_OF_MDIO
219 * Set and/or override some configuration registers based on the
220 * marvell,reg-init property stored in the of_node for the phydev.
222 * marvell,reg-init = <reg-page reg mask value>,...;
224 * There may be one or more sets of <reg-page reg mask value>:
226 * reg-page: which register bank to use.
227 * reg: the register.
228 * mask: if non-zero, ANDed with existing register value.
229 * value: ORed with the masked value and written to the regiser.
232 static int marvell_of_reg_init(struct phy_device *phydev)
234 const __be32 *paddr;
235 int len, i, saved_page, current_page, page_changed, ret;
237 if (!phydev->dev.of_node)
238 return 0;
240 paddr = of_get_property(phydev->dev.of_node, "marvell,reg-init", &len);
241 if (!paddr || len < (4 * sizeof(*paddr)))
242 return 0;
244 saved_page = phy_read(phydev, MII_MARVELL_PHY_PAGE);
245 if (saved_page < 0)
246 return saved_page;
247 page_changed = 0;
248 current_page = saved_page;
250 ret = 0;
251 len /= sizeof(*paddr);
252 for (i = 0; i < len - 3; i += 4) {
253 u16 reg_page = be32_to_cpup(paddr + i);
254 u16 reg = be32_to_cpup(paddr + i + 1);
255 u16 mask = be32_to_cpup(paddr + i + 2);
256 u16 val_bits = be32_to_cpup(paddr + i + 3);
257 int val;
259 if (reg_page != current_page) {
260 current_page = reg_page;
261 page_changed = 1;
262 ret = phy_write(phydev, MII_MARVELL_PHY_PAGE, reg_page);
263 if (ret < 0)
264 goto err;
267 val = 0;
268 if (mask) {
269 val = phy_read(phydev, reg);
270 if (val < 0) {
271 ret = val;
272 goto err;
274 val &= mask;
276 val |= val_bits;
278 ret = phy_write(phydev, reg, val);
279 if (ret < 0)
280 goto err;
283 err:
284 if (page_changed) {
285 i = phy_write(phydev, MII_MARVELL_PHY_PAGE, saved_page);
286 if (ret == 0)
287 ret = i;
289 return ret;
291 #else
292 static int marvell_of_reg_init(struct phy_device *phydev)
294 return 0;
296 #endif /* CONFIG_OF_MDIO */
298 static int m88e1121_config_aneg(struct phy_device *phydev)
300 int err, oldpage, mscr;
302 oldpage = phy_read(phydev, MII_MARVELL_PHY_PAGE);
304 err = phy_write(phydev, MII_MARVELL_PHY_PAGE,
305 MII_88E1121_PHY_MSCR_PAGE);
306 if (err < 0)
307 return err;
309 if ((phydev->interface == PHY_INTERFACE_MODE_RGMII) ||
310 (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) ||
311 (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
312 (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
314 mscr = phy_read(phydev, MII_88E1121_PHY_MSCR_REG) &
315 MII_88E1121_PHY_MSCR_DELAY_MASK;
317 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
318 mscr |= (MII_88E1121_PHY_MSCR_RX_DELAY |
319 MII_88E1121_PHY_MSCR_TX_DELAY);
320 else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
321 mscr |= MII_88E1121_PHY_MSCR_RX_DELAY;
322 else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
323 mscr |= MII_88E1121_PHY_MSCR_TX_DELAY;
325 err = phy_write(phydev, MII_88E1121_PHY_MSCR_REG, mscr);
326 if (err < 0)
327 return err;
330 phy_write(phydev, MII_MARVELL_PHY_PAGE, oldpage);
332 err = phy_write(phydev, MII_BMCR, BMCR_RESET);
333 if (err < 0)
334 return err;
336 err = phy_write(phydev, MII_M1011_PHY_SCR,
337 MII_M1011_PHY_SCR_AUTO_CROSS);
338 if (err < 0)
339 return err;
341 oldpage = phy_read(phydev, MII_MARVELL_PHY_PAGE);
343 phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_88E1121_PHY_LED_PAGE);
344 phy_write(phydev, MII_88E1121_PHY_LED_CTRL, MII_88E1121_PHY_LED_DEF);
345 phy_write(phydev, MII_MARVELL_PHY_PAGE, oldpage);
347 err = genphy_config_aneg(phydev);
349 return err;
352 static int m88e1318_config_aneg(struct phy_device *phydev)
354 int err, oldpage, mscr;
356 oldpage = phy_read(phydev, MII_MARVELL_PHY_PAGE);
358 err = phy_write(phydev, MII_MARVELL_PHY_PAGE,
359 MII_88E1121_PHY_MSCR_PAGE);
360 if (err < 0)
361 return err;
363 mscr = phy_read(phydev, MII_88E1318S_PHY_MSCR1_REG);
364 mscr |= MII_88E1318S_PHY_MSCR1_PAD_ODD;
366 err = phy_write(phydev, MII_88E1318S_PHY_MSCR1_REG, mscr);
367 if (err < 0)
368 return err;
370 err = phy_write(phydev, MII_MARVELL_PHY_PAGE, oldpage);
371 if (err < 0)
372 return err;
374 return m88e1121_config_aneg(phydev);
377 static int m88e1510_config_aneg(struct phy_device *phydev)
379 int err;
381 err = m88e1318_config_aneg(phydev);
382 if (err < 0)
383 return err;
385 return marvell_of_reg_init(phydev);
388 static int m88e1116r_config_init(struct phy_device *phydev)
390 int temp;
391 int err;
393 temp = phy_read(phydev, MII_BMCR);
394 temp |= BMCR_RESET;
395 err = phy_write(phydev, MII_BMCR, temp);
396 if (err < 0)
397 return err;
399 mdelay(500);
401 err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0);
402 if (err < 0)
403 return err;
405 temp = phy_read(phydev, MII_M1011_PHY_SCR);
406 temp |= (7 << 12); /* max number of gigabit attempts */
407 temp |= (1 << 11); /* enable downshift */
408 temp |= MII_M1011_PHY_SCR_AUTO_CROSS;
409 err = phy_write(phydev, MII_M1011_PHY_SCR, temp);
410 if (err < 0)
411 return err;
413 err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 2);
414 if (err < 0)
415 return err;
416 temp = phy_read(phydev, MII_M1116R_CONTROL_REG_MAC);
417 temp |= (1 << 5);
418 temp |= (1 << 4);
419 err = phy_write(phydev, MII_M1116R_CONTROL_REG_MAC, temp);
420 if (err < 0)
421 return err;
422 err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0);
423 if (err < 0)
424 return err;
426 temp = phy_read(phydev, MII_BMCR);
427 temp |= BMCR_RESET;
428 err = phy_write(phydev, MII_BMCR, temp);
429 if (err < 0)
430 return err;
432 mdelay(500);
434 return 0;
437 static int m88e1111_config_init(struct phy_device *phydev)
439 int err;
440 int temp;
442 if ((phydev->interface == PHY_INTERFACE_MODE_RGMII) ||
443 (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) ||
444 (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
445 (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
447 temp = phy_read(phydev, MII_M1111_PHY_EXT_CR);
448 if (temp < 0)
449 return temp;
451 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) {
452 temp |= (MII_M1111_RX_DELAY | MII_M1111_TX_DELAY);
453 } else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) {
454 temp &= ~MII_M1111_TX_DELAY;
455 temp |= MII_M1111_RX_DELAY;
456 } else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) {
457 temp &= ~MII_M1111_RX_DELAY;
458 temp |= MII_M1111_TX_DELAY;
461 err = phy_write(phydev, MII_M1111_PHY_EXT_CR, temp);
462 if (err < 0)
463 return err;
465 temp = phy_read(phydev, MII_M1111_PHY_EXT_SR);
466 if (temp < 0)
467 return temp;
469 temp &= ~(MII_M1111_HWCFG_MODE_MASK);
471 if (temp & MII_M1111_HWCFG_FIBER_COPPER_RES)
472 temp |= MII_M1111_HWCFG_MODE_FIBER_RGMII;
473 else
474 temp |= MII_M1111_HWCFG_MODE_COPPER_RGMII;
476 err = phy_write(phydev, MII_M1111_PHY_EXT_SR, temp);
477 if (err < 0)
478 return err;
481 if (phydev->interface == PHY_INTERFACE_MODE_SGMII) {
482 temp = phy_read(phydev, MII_M1111_PHY_EXT_SR);
483 if (temp < 0)
484 return temp;
486 temp &= ~(MII_M1111_HWCFG_MODE_MASK);
487 temp |= MII_M1111_HWCFG_MODE_SGMII_NO_CLK;
488 temp |= MII_M1111_HWCFG_FIBER_COPPER_AUTO;
490 err = phy_write(phydev, MII_M1111_PHY_EXT_SR, temp);
491 if (err < 0)
492 return err;
495 if (phydev->interface == PHY_INTERFACE_MODE_RTBI) {
496 temp = phy_read(phydev, MII_M1111_PHY_EXT_CR);
497 if (temp < 0)
498 return temp;
499 temp |= (MII_M1111_RX_DELAY | MII_M1111_TX_DELAY);
500 err = phy_write(phydev, MII_M1111_PHY_EXT_CR, temp);
501 if (err < 0)
502 return err;
504 temp = phy_read(phydev, MII_M1111_PHY_EXT_SR);
505 if (temp < 0)
506 return temp;
507 temp &= ~(MII_M1111_HWCFG_MODE_MASK | MII_M1111_HWCFG_FIBER_COPPER_RES);
508 temp |= 0x7 | MII_M1111_HWCFG_FIBER_COPPER_AUTO;
509 err = phy_write(phydev, MII_M1111_PHY_EXT_SR, temp);
510 if (err < 0)
511 return err;
513 /* soft reset */
514 err = phy_write(phydev, MII_BMCR, BMCR_RESET);
515 if (err < 0)
516 return err;
518 temp = phy_read(phydev, MII_BMCR);
519 while (temp & BMCR_RESET);
521 temp = phy_read(phydev, MII_M1111_PHY_EXT_SR);
522 if (temp < 0)
523 return temp;
524 temp &= ~(MII_M1111_HWCFG_MODE_MASK | MII_M1111_HWCFG_FIBER_COPPER_RES);
525 temp |= MII_M1111_HWCFG_MODE_COPPER_RTBI | MII_M1111_HWCFG_FIBER_COPPER_AUTO;
526 err = phy_write(phydev, MII_M1111_PHY_EXT_SR, temp);
527 if (err < 0)
528 return err;
531 err = marvell_of_reg_init(phydev);
532 if (err < 0)
533 return err;
535 return phy_write(phydev, MII_BMCR, BMCR_RESET);
538 static int m88e1118_config_aneg(struct phy_device *phydev)
540 int err;
542 err = phy_write(phydev, MII_BMCR, BMCR_RESET);
543 if (err < 0)
544 return err;
546 err = phy_write(phydev, MII_M1011_PHY_SCR,
547 MII_M1011_PHY_SCR_AUTO_CROSS);
548 if (err < 0)
549 return err;
551 err = genphy_config_aneg(phydev);
552 return 0;
555 static int m88e1118_config_init(struct phy_device *phydev)
557 int err;
559 /* Change address */
560 err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0x0002);
561 if (err < 0)
562 return err;
564 /* Enable 1000 Mbit */
565 err = phy_write(phydev, 0x15, 0x1070);
566 if (err < 0)
567 return err;
569 /* Change address */
570 err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0x0003);
571 if (err < 0)
572 return err;
574 /* Adjust LED Control */
575 if (phydev->dev_flags & MARVELL_PHY_M1118_DNS323_LEDS)
576 err = phy_write(phydev, 0x10, 0x1100);
577 else
578 err = phy_write(phydev, 0x10, 0x021e);
579 if (err < 0)
580 return err;
582 err = marvell_of_reg_init(phydev);
583 if (err < 0)
584 return err;
586 /* Reset address */
587 err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0x0);
588 if (err < 0)
589 return err;
591 return phy_write(phydev, MII_BMCR, BMCR_RESET);
594 static int m88e1149_config_init(struct phy_device *phydev)
596 int err;
598 /* Change address */
599 err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0x0002);
600 if (err < 0)
601 return err;
603 /* Enable 1000 Mbit */
604 err = phy_write(phydev, 0x15, 0x1048);
605 if (err < 0)
606 return err;
608 err = marvell_of_reg_init(phydev);
609 if (err < 0)
610 return err;
612 /* Reset address */
613 err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0x0);
614 if (err < 0)
615 return err;
617 return phy_write(phydev, MII_BMCR, BMCR_RESET);
620 static int m88e1145_config_init(struct phy_device *phydev)
622 int err;
624 /* Take care of errata E0 & E1 */
625 err = phy_write(phydev, 0x1d, 0x001b);
626 if (err < 0)
627 return err;
629 err = phy_write(phydev, 0x1e, 0x418f);
630 if (err < 0)
631 return err;
633 err = phy_write(phydev, 0x1d, 0x0016);
634 if (err < 0)
635 return err;
637 err = phy_write(phydev, 0x1e, 0xa2da);
638 if (err < 0)
639 return err;
641 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) {
642 int temp = phy_read(phydev, MII_M1145_PHY_EXT_CR);
643 if (temp < 0)
644 return temp;
646 temp |= (MII_M1145_RGMII_RX_DELAY | MII_M1145_RGMII_TX_DELAY);
648 err = phy_write(phydev, MII_M1145_PHY_EXT_CR, temp);
649 if (err < 0)
650 return err;
652 if (phydev->dev_flags & MARVELL_PHY_M1145_FLAGS_RESISTANCE) {
653 err = phy_write(phydev, 0x1d, 0x0012);
654 if (err < 0)
655 return err;
657 temp = phy_read(phydev, 0x1e);
658 if (temp < 0)
659 return temp;
661 temp &= 0xf03f;
662 temp |= 2 << 9; /* 36 ohm */
663 temp |= 2 << 6; /* 39 ohm */
665 err = phy_write(phydev, 0x1e, temp);
666 if (err < 0)
667 return err;
669 err = phy_write(phydev, 0x1d, 0x3);
670 if (err < 0)
671 return err;
673 err = phy_write(phydev, 0x1e, 0x8000);
674 if (err < 0)
675 return err;
679 err = marvell_of_reg_init(phydev);
680 if (err < 0)
681 return err;
683 return 0;
686 /* marvell_read_status
688 * Generic status code does not detect Fiber correctly!
689 * Description:
690 * Check the link, then figure out the current state
691 * by comparing what we advertise with what the link partner
692 * advertises. Start by checking the gigabit possibilities,
693 * then move on to 10/100.
695 static int marvell_read_status(struct phy_device *phydev)
697 int adv;
698 int err;
699 int lpa;
700 int status = 0;
702 /* Update the link, but return if there
703 * was an error */
704 err = genphy_update_link(phydev);
705 if (err)
706 return err;
708 if (AUTONEG_ENABLE == phydev->autoneg) {
709 status = phy_read(phydev, MII_M1011_PHY_STATUS);
710 if (status < 0)
711 return status;
713 lpa = phy_read(phydev, MII_LPA);
714 if (lpa < 0)
715 return lpa;
717 adv = phy_read(phydev, MII_ADVERTISE);
718 if (adv < 0)
719 return adv;
721 lpa &= adv;
723 if (status & MII_M1011_PHY_STATUS_FULLDUPLEX)
724 phydev->duplex = DUPLEX_FULL;
725 else
726 phydev->duplex = DUPLEX_HALF;
728 status = status & MII_M1011_PHY_STATUS_SPD_MASK;
729 phydev->pause = phydev->asym_pause = 0;
731 switch (status) {
732 case MII_M1011_PHY_STATUS_1000:
733 phydev->speed = SPEED_1000;
734 break;
736 case MII_M1011_PHY_STATUS_100:
737 phydev->speed = SPEED_100;
738 break;
740 default:
741 phydev->speed = SPEED_10;
742 break;
745 if (phydev->duplex == DUPLEX_FULL) {
746 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
747 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
749 } else {
750 int bmcr = phy_read(phydev, MII_BMCR);
752 if (bmcr < 0)
753 return bmcr;
755 if (bmcr & BMCR_FULLDPLX)
756 phydev->duplex = DUPLEX_FULL;
757 else
758 phydev->duplex = DUPLEX_HALF;
760 if (bmcr & BMCR_SPEED1000)
761 phydev->speed = SPEED_1000;
762 else if (bmcr & BMCR_SPEED100)
763 phydev->speed = SPEED_100;
764 else
765 phydev->speed = SPEED_10;
767 phydev->pause = phydev->asym_pause = 0;
770 return 0;
773 static int m88e1121_did_interrupt(struct phy_device *phydev)
775 int imask;
777 imask = phy_read(phydev, MII_M1011_IEVENT);
779 if (imask & MII_M1011_IMASK_INIT)
780 return 1;
782 return 0;
785 static void m88e1318_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
787 wol->supported = WAKE_MAGIC;
788 wol->wolopts = 0;
790 if (phy_write(phydev, MII_MARVELL_PHY_PAGE,
791 MII_88E1318S_PHY_WOL_PAGE) < 0)
792 return;
794 if (phy_read(phydev, MII_88E1318S_PHY_WOL_CTRL) &
795 MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE)
796 wol->wolopts |= WAKE_MAGIC;
798 if (phy_write(phydev, MII_MARVELL_PHY_PAGE, 0x00) < 0)
799 return;
802 static int m88e1318_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
804 int err, oldpage, temp;
806 oldpage = phy_read(phydev, MII_MARVELL_PHY_PAGE);
808 if (wol->wolopts & WAKE_MAGIC) {
809 /* Explicitly switch to page 0x00, just to be sure */
810 err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0x00);
811 if (err < 0)
812 return err;
814 /* Enable the WOL interrupt */
815 temp = phy_read(phydev, MII_88E1318S_PHY_CSIER);
816 temp |= MII_88E1318S_PHY_CSIER_WOL_EIE;
817 err = phy_write(phydev, MII_88E1318S_PHY_CSIER, temp);
818 if (err < 0)
819 return err;
821 err = phy_write(phydev, MII_MARVELL_PHY_PAGE,
822 MII_88E1318S_PHY_LED_PAGE);
823 if (err < 0)
824 return err;
826 /* Setup LED[2] as interrupt pin (active low) */
827 temp = phy_read(phydev, MII_88E1318S_PHY_LED_TCR);
828 temp &= ~MII_88E1318S_PHY_LED_TCR_FORCE_INT;
829 temp |= MII_88E1318S_PHY_LED_TCR_INTn_ENABLE;
830 temp |= MII_88E1318S_PHY_LED_TCR_INT_ACTIVE_LOW;
831 err = phy_write(phydev, MII_88E1318S_PHY_LED_TCR, temp);
832 if (err < 0)
833 return err;
835 err = phy_write(phydev, MII_MARVELL_PHY_PAGE,
836 MII_88E1318S_PHY_WOL_PAGE);
837 if (err < 0)
838 return err;
840 /* Store the device address for the magic packet */
841 err = phy_write(phydev, MII_88E1318S_PHY_MAGIC_PACKET_WORD2,
842 ((phydev->attached_dev->dev_addr[5] << 8) |
843 phydev->attached_dev->dev_addr[4]));
844 if (err < 0)
845 return err;
846 err = phy_write(phydev, MII_88E1318S_PHY_MAGIC_PACKET_WORD1,
847 ((phydev->attached_dev->dev_addr[3] << 8) |
848 phydev->attached_dev->dev_addr[2]));
849 if (err < 0)
850 return err;
851 err = phy_write(phydev, MII_88E1318S_PHY_MAGIC_PACKET_WORD0,
852 ((phydev->attached_dev->dev_addr[1] << 8) |
853 phydev->attached_dev->dev_addr[0]));
854 if (err < 0)
855 return err;
857 /* Clear WOL status and enable magic packet matching */
858 temp = phy_read(phydev, MII_88E1318S_PHY_WOL_CTRL);
859 temp |= MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS;
860 temp |= MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE;
861 err = phy_write(phydev, MII_88E1318S_PHY_WOL_CTRL, temp);
862 if (err < 0)
863 return err;
864 } else {
865 err = phy_write(phydev, MII_MARVELL_PHY_PAGE,
866 MII_88E1318S_PHY_WOL_PAGE);
867 if (err < 0)
868 return err;
870 /* Clear WOL status and disable magic packet matching */
871 temp = phy_read(phydev, MII_88E1318S_PHY_WOL_CTRL);
872 temp |= MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS;
873 temp &= ~MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE;
874 err = phy_write(phydev, MII_88E1318S_PHY_WOL_CTRL, temp);
875 if (err < 0)
876 return err;
879 err = phy_write(phydev, MII_MARVELL_PHY_PAGE, oldpage);
880 if (err < 0)
881 return err;
883 return 0;
886 static struct phy_driver marvell_drivers[] = {
888 .phy_id = MARVELL_PHY_ID_88E1101,
889 .phy_id_mask = MARVELL_PHY_ID_MASK,
890 .name = "Marvell 88E1101",
891 .features = PHY_GBIT_FEATURES,
892 .flags = PHY_HAS_INTERRUPT,
893 .config_aneg = &marvell_config_aneg,
894 .read_status = &genphy_read_status,
895 .ack_interrupt = &marvell_ack_interrupt,
896 .config_intr = &marvell_config_intr,
897 .driver = { .owner = THIS_MODULE },
900 .phy_id = MARVELL_PHY_ID_88E1112,
901 .phy_id_mask = MARVELL_PHY_ID_MASK,
902 .name = "Marvell 88E1112",
903 .features = PHY_GBIT_FEATURES,
904 .flags = PHY_HAS_INTERRUPT,
905 .config_init = &m88e1111_config_init,
906 .config_aneg = &marvell_config_aneg,
907 .read_status = &genphy_read_status,
908 .ack_interrupt = &marvell_ack_interrupt,
909 .config_intr = &marvell_config_intr,
910 .driver = { .owner = THIS_MODULE },
913 .phy_id = MARVELL_PHY_ID_88E1111,
914 .phy_id_mask = MARVELL_PHY_ID_MASK,
915 .name = "Marvell 88E1111",
916 .features = PHY_GBIT_FEATURES,
917 .flags = PHY_HAS_INTERRUPT,
918 .config_init = &m88e1111_config_init,
919 .config_aneg = &marvell_config_aneg,
920 .read_status = &marvell_read_status,
921 .ack_interrupt = &marvell_ack_interrupt,
922 .config_intr = &marvell_config_intr,
923 .driver = { .owner = THIS_MODULE },
926 .phy_id = MARVELL_PHY_ID_88E1118,
927 .phy_id_mask = MARVELL_PHY_ID_MASK,
928 .name = "Marvell 88E1118",
929 .features = PHY_GBIT_FEATURES,
930 .flags = PHY_HAS_INTERRUPT,
931 .config_init = &m88e1118_config_init,
932 .config_aneg = &m88e1118_config_aneg,
933 .read_status = &genphy_read_status,
934 .ack_interrupt = &marvell_ack_interrupt,
935 .config_intr = &marvell_config_intr,
936 .driver = {.owner = THIS_MODULE,},
939 .phy_id = MARVELL_PHY_ID_88E1121R,
940 .phy_id_mask = MARVELL_PHY_ID_MASK,
941 .name = "Marvell 88E1121R",
942 .features = PHY_GBIT_FEATURES,
943 .flags = PHY_HAS_INTERRUPT,
944 .config_aneg = &m88e1121_config_aneg,
945 .read_status = &marvell_read_status,
946 .ack_interrupt = &marvell_ack_interrupt,
947 .config_intr = &marvell_config_intr,
948 .did_interrupt = &m88e1121_did_interrupt,
949 .driver = { .owner = THIS_MODULE },
952 .phy_id = MARVELL_PHY_ID_88E1318S,
953 .phy_id_mask = MARVELL_PHY_ID_MASK,
954 .name = "Marvell 88E1318S",
955 .features = PHY_GBIT_FEATURES,
956 .flags = PHY_HAS_INTERRUPT,
957 .config_aneg = &m88e1318_config_aneg,
958 .read_status = &marvell_read_status,
959 .ack_interrupt = &marvell_ack_interrupt,
960 .config_intr = &marvell_config_intr,
961 .did_interrupt = &m88e1121_did_interrupt,
962 .get_wol = &m88e1318_get_wol,
963 .set_wol = &m88e1318_set_wol,
964 .driver = { .owner = THIS_MODULE },
967 .phy_id = MARVELL_PHY_ID_88E1145,
968 .phy_id_mask = MARVELL_PHY_ID_MASK,
969 .name = "Marvell 88E1145",
970 .features = PHY_GBIT_FEATURES,
971 .flags = PHY_HAS_INTERRUPT,
972 .config_init = &m88e1145_config_init,
973 .config_aneg = &marvell_config_aneg,
974 .read_status = &genphy_read_status,
975 .ack_interrupt = &marvell_ack_interrupt,
976 .config_intr = &marvell_config_intr,
977 .driver = { .owner = THIS_MODULE },
980 .phy_id = MARVELL_PHY_ID_88E1149R,
981 .phy_id_mask = MARVELL_PHY_ID_MASK,
982 .name = "Marvell 88E1149R",
983 .features = PHY_GBIT_FEATURES,
984 .flags = PHY_HAS_INTERRUPT,
985 .config_init = &m88e1149_config_init,
986 .config_aneg = &m88e1118_config_aneg,
987 .read_status = &genphy_read_status,
988 .ack_interrupt = &marvell_ack_interrupt,
989 .config_intr = &marvell_config_intr,
990 .driver = { .owner = THIS_MODULE },
993 .phy_id = MARVELL_PHY_ID_88E1240,
994 .phy_id_mask = MARVELL_PHY_ID_MASK,
995 .name = "Marvell 88E1240",
996 .features = PHY_GBIT_FEATURES,
997 .flags = PHY_HAS_INTERRUPT,
998 .config_init = &m88e1111_config_init,
999 .config_aneg = &marvell_config_aneg,
1000 .read_status = &genphy_read_status,
1001 .ack_interrupt = &marvell_ack_interrupt,
1002 .config_intr = &marvell_config_intr,
1003 .driver = { .owner = THIS_MODULE },
1006 .phy_id = MARVELL_PHY_ID_88E1116R,
1007 .phy_id_mask = MARVELL_PHY_ID_MASK,
1008 .name = "Marvell 88E1116R",
1009 .features = PHY_GBIT_FEATURES,
1010 .flags = PHY_HAS_INTERRUPT,
1011 .config_init = &m88e1116r_config_init,
1012 .config_aneg = &genphy_config_aneg,
1013 .read_status = &genphy_read_status,
1014 .ack_interrupt = &marvell_ack_interrupt,
1015 .config_intr = &marvell_config_intr,
1016 .driver = { .owner = THIS_MODULE },
1019 .phy_id = MARVELL_PHY_ID_88E1510,
1020 .phy_id_mask = MARVELL_PHY_ID_MASK,
1021 .name = "Marvell 88E1510",
1022 .features = PHY_GBIT_FEATURES,
1023 .flags = PHY_HAS_INTERRUPT,
1024 .config_aneg = &m88e1510_config_aneg,
1025 .read_status = &marvell_read_status,
1026 .ack_interrupt = &marvell_ack_interrupt,
1027 .config_intr = &marvell_config_intr,
1028 .did_interrupt = &m88e1121_did_interrupt,
1029 .driver = { .owner = THIS_MODULE },
1033 static int __init marvell_init(void)
1035 return phy_drivers_register(marvell_drivers,
1036 ARRAY_SIZE(marvell_drivers));
1039 static void __exit marvell_exit(void)
1041 phy_drivers_unregister(marvell_drivers,
1042 ARRAY_SIZE(marvell_drivers));
1045 module_init(marvell_init);
1046 module_exit(marvell_exit);
1048 static struct mdio_device_id __maybe_unused marvell_tbl[] = {
1049 { MARVELL_PHY_ID_88E1101, MARVELL_PHY_ID_MASK },
1050 { MARVELL_PHY_ID_88E1112, MARVELL_PHY_ID_MASK },
1051 { MARVELL_PHY_ID_88E1111, MARVELL_PHY_ID_MASK },
1052 { MARVELL_PHY_ID_88E1118, MARVELL_PHY_ID_MASK },
1053 { MARVELL_PHY_ID_88E1121R, MARVELL_PHY_ID_MASK },
1054 { MARVELL_PHY_ID_88E1145, MARVELL_PHY_ID_MASK },
1055 { MARVELL_PHY_ID_88E1149R, MARVELL_PHY_ID_MASK },
1056 { MARVELL_PHY_ID_88E1240, MARVELL_PHY_ID_MASK },
1057 { MARVELL_PHY_ID_88E1318S, MARVELL_PHY_ID_MASK },
1058 { MARVELL_PHY_ID_88E1116R, MARVELL_PHY_ID_MASK },
1059 { MARVELL_PHY_ID_88E1510, MARVELL_PHY_ID_MASK },
1063 MODULE_DEVICE_TABLE(mdio, marvell_tbl);