1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Marvell 88E6xxx System Management Interface (SMI) support
5 * Copyright (c) 2008 Marvell Semiconductor
7 * Copyright (c) 2019 Vivien Didelot <vivien.didelot@gmail.com>
13 /* The switch ADDR[4:1] configuration pins define the chip SMI device address
14 * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
16 * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
17 * is the only device connected to the SMI master. In this mode it responds to
18 * all 32 possible SMI addresses, and thus maps directly the internal devices.
20 * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
21 * multiple devices to share the SMI interface. In this mode it responds to only
22 * 2 registers, used to indirectly access the internal SMI devices.
24 * Some chips use a different scheme: Only the ADDR4 pin is used for
25 * configuration, and the device responds to 16 of the 32 SMI
26 * addresses, allowing two to coexist on the same SMI interface.
29 static int mv88e6xxx_smi_direct_read(struct mv88e6xxx_chip
*chip
,
30 int dev
, int reg
, u16
*data
)
34 ret
= mdiobus_read_nested(chip
->bus
, dev
, reg
);
43 static int mv88e6xxx_smi_direct_write(struct mv88e6xxx_chip
*chip
,
44 int dev
, int reg
, u16 data
)
48 ret
= mdiobus_write_nested(chip
->bus
, dev
, reg
, data
);
55 static int mv88e6xxx_smi_direct_wait(struct mv88e6xxx_chip
*chip
,
56 int dev
, int reg
, int bit
, int val
)
58 const unsigned long timeout
= jiffies
+ msecs_to_jiffies(50);
63 /* Even if the initial poll takes longer than 50ms, always do
64 * at least one more attempt.
66 for (i
= 0; time_before(jiffies
, timeout
) || (i
< 2); i
++) {
67 err
= mv88e6xxx_smi_direct_read(chip
, dev
, reg
, &data
);
71 if (!!(data
& BIT(bit
)) == !!val
)
77 usleep_range(1000, 2000);
83 static const struct mv88e6xxx_bus_ops mv88e6xxx_smi_direct_ops
= {
84 .read
= mv88e6xxx_smi_direct_read
,
85 .write
= mv88e6xxx_smi_direct_write
,
88 static int mv88e6xxx_smi_dual_direct_read(struct mv88e6xxx_chip
*chip
,
89 int dev
, int reg
, u16
*data
)
91 return mv88e6xxx_smi_direct_read(chip
, chip
->sw_addr
+ dev
, reg
, data
);
94 static int mv88e6xxx_smi_dual_direct_write(struct mv88e6xxx_chip
*chip
,
95 int dev
, int reg
, u16 data
)
97 return mv88e6xxx_smi_direct_write(chip
, chip
->sw_addr
+ dev
, reg
, data
);
100 static const struct mv88e6xxx_bus_ops mv88e6xxx_smi_dual_direct_ops
= {
101 .read
= mv88e6xxx_smi_dual_direct_read
,
102 .write
= mv88e6xxx_smi_dual_direct_write
,
105 /* Offset 0x00: SMI Command Register
106 * Offset 0x01: SMI Data Register
109 static int mv88e6xxx_smi_indirect_read(struct mv88e6xxx_chip
*chip
,
110 int dev
, int reg
, u16
*data
)
114 err
= mv88e6xxx_smi_direct_write(chip
, chip
->sw_addr
,
116 MV88E6XXX_SMI_CMD_BUSY
|
117 MV88E6XXX_SMI_CMD_MODE_22
|
118 MV88E6XXX_SMI_CMD_OP_22_READ
|
123 err
= mv88e6xxx_smi_direct_wait(chip
, chip
->sw_addr
,
124 MV88E6XXX_SMI_CMD
, 15, 0);
128 return mv88e6xxx_smi_direct_read(chip
, chip
->sw_addr
,
129 MV88E6XXX_SMI_DATA
, data
);
132 static int mv88e6xxx_smi_indirect_write(struct mv88e6xxx_chip
*chip
,
133 int dev
, int reg
, u16 data
)
137 err
= mv88e6xxx_smi_direct_write(chip
, chip
->sw_addr
,
138 MV88E6XXX_SMI_DATA
, data
);
142 err
= mv88e6xxx_smi_direct_write(chip
, chip
->sw_addr
,
144 MV88E6XXX_SMI_CMD_BUSY
|
145 MV88E6XXX_SMI_CMD_MODE_22
|
146 MV88E6XXX_SMI_CMD_OP_22_WRITE
|
151 return mv88e6xxx_smi_direct_wait(chip
, chip
->sw_addr
,
152 MV88E6XXX_SMI_CMD
, 15, 0);
155 static int mv88e6xxx_smi_indirect_init(struct mv88e6xxx_chip
*chip
)
157 /* Ensure that the chip starts out in the ready state. As both
158 * reads and writes always ensure this on return, they can
159 * safely depend on the chip not being busy on entry.
161 return mv88e6xxx_smi_direct_wait(chip
, chip
->sw_addr
,
162 MV88E6XXX_SMI_CMD
, 15, 0);
165 static const struct mv88e6xxx_bus_ops mv88e6xxx_smi_indirect_ops
= {
166 .read
= mv88e6xxx_smi_indirect_read
,
167 .write
= mv88e6xxx_smi_indirect_write
,
168 .init
= mv88e6xxx_smi_indirect_init
,
171 int mv88e6xxx_smi_init(struct mv88e6xxx_chip
*chip
,
172 struct mii_bus
*bus
, int sw_addr
)
174 if (chip
->info
->dual_chip
)
175 chip
->smi_ops
= &mv88e6xxx_smi_dual_direct_ops
;
176 else if (sw_addr
== 0)
177 chip
->smi_ops
= &mv88e6xxx_smi_direct_ops
;
178 else if (chip
->info
->multi_chip
)
179 chip
->smi_ops
= &mv88e6xxx_smi_indirect_ops
;
184 chip
->sw_addr
= sw_addr
;
186 if (chip
->smi_ops
->init
)
187 return chip
->smi_ops
->init(chip
);