2 * Software PHY emulation
4 * Code taken from fixed_phy.c by Russell King <rmk+kernel@arm.linux.org.uk>
6 * Author: Vitaly Bordug <vbordug@ru.mvista.com>
7 * Anton Vorontsov <avorontsov@ru.mvista.com>
9 * Copyright (c) 2006-2007 MontaVista Software, Inc.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 #include <linux/export.h>
17 #include <linux/mii.h>
18 #include <linux/phy.h>
19 #include <linux/phy_fixed.h>
23 #define MII_REGS_NUM 29
36 SWMII_DUPLEX_HALF = 0,
41 * These two tables get bitwise-anded together to produce the final result.
42 * This means the speed table must contain both duplex settings, and the
43 * duplex table must contain all speed settings.
45 static const struct swmii_regs speed[] = {
47 .bmcr = BMCR_FULLDPLX,
48 .lpa = LPA_10FULL | LPA_10HALF,
51 .bmcr = BMCR_FULLDPLX | BMCR_SPEED100,
52 .bmsr = BMSR_100FULL | BMSR_100HALF,
53 .lpa = LPA_100FULL | LPA_100HALF,
55 [SWMII_SPEED_1000] = {
56 .bmcr = BMCR_FULLDPLX | BMCR_SPEED1000,
58 .lpagb = LPA_1000FULL | LPA_1000HALF,
62 static const struct swmii_regs duplex[] = {
63 [SWMII_DUPLEX_HALF] = {
64 .bmcr = ~BMCR_FULLDPLX,
65 .bmsr = BMSR_ESTATEN | BMSR_100HALF,
66 .lpa = LPA_10HALF | LPA_100HALF,
67 .lpagb = LPA_1000HALF,
69 [SWMII_DUPLEX_FULL] = {
71 .bmsr = BMSR_ESTATEN | BMSR_100FULL,
72 .lpa = LPA_10FULL | LPA_100FULL,
73 .lpagb = LPA_1000FULL,
77 static int swphy_decode_speed(int speed)
81 return SWMII_SPEED_1000;
83 return SWMII_SPEED_100;
85 return SWMII_SPEED_10;
92 * swphy_validate_state - validate the software phy status
93 * @state: software phy status
95 * This checks that we can represent the state stored in @state can be
96 * represented in the emulated MII registers. Returns 0 if it can,
97 * otherwise returns -EINVAL.
99 int swphy_validate_state(const struct fixed_phy_status *state)
104 err = swphy_decode_speed(state->speed);
106 pr_warn("swphy: unknown speed\n");
112 EXPORT_SYMBOL_GPL(swphy_validate_state);
115 * swphy_read_reg - return a MII register from the fixed phy state
117 * @state: fixed phy status
119 * Return the MII @reg register generated from the fixed phy state @state.
121 int swphy_read_reg(int reg, const struct fixed_phy_status *state)
123 int speed_index, duplex_index;
124 u16 bmsr = BMSR_ANEGCAPABLE;
129 if (reg > MII_REGS_NUM)
132 speed_index = swphy_decode_speed(state->speed);
133 if (WARN_ON(speed_index < 0))
136 duplex_index = state->duplex ? SWMII_DUPLEX_FULL : SWMII_DUPLEX_HALF;
138 bmsr |= speed[speed_index].bmsr & duplex[duplex_index].bmsr;
141 bmsr |= BMSR_LSTATUS | BMSR_ANEGCOMPLETE;
143 bmcr |= speed[speed_index].bmcr & duplex[duplex_index].bmcr;
144 lpa |= speed[speed_index].lpa & duplex[duplex_index].lpa;
145 lpagb |= speed[speed_index].lpagb & duplex[duplex_index].lpagb;
148 lpa |= LPA_PAUSE_CAP;
150 if (state->asym_pause)
151 lpa |= LPA_PAUSE_ASYM;
168 * We do not support emulating Clause 45 over Clause 22 register
169 * reads. Return an error instead of bogus data.
179 EXPORT_SYMBOL_GPL(swphy_read_reg);