Skip to content

Commit b9b3771

Browse files
viviendavem330
authored andcommitted
net: dsa: mv88e6xxx: lookup switch name
All the mv88e6xxx drivers use the exact same code in their probe function to lookup the switch name given its ID. Thus introduce a mv88e6xxx_switch_id structure and a mv88e6xxx_lookup_name function in the common mv88e6xxx code. In the meantime make __mv88e6xxx_reg_{read,write} static since we do not need to expose these low-level r/w routines anymore. Signed-off-by: Vivien Didelot <vivien.didelot@savoirfairelinux.com> Acked-by: Andrew Lunn <andrew@lunn.ch> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 3996a4f commit b9b3771

File tree

6 files changed

+95
-112
lines changed

6 files changed

+95
-112
lines changed

drivers/net/dsa/mv88e6123_61_65.c

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,22 @@
1717
#include <net/dsa.h>
1818
#include "mv88e6xxx.h"
1919

20+
static const struct mv88e6xxx_switch_id mv88e6123_61_65_table[] = {
21+
{ PORT_SWITCH_ID_6123, "Marvell 88E6123" },
22+
{ PORT_SWITCH_ID_6123_A1, "Marvell 88E6123 (A1)" },
23+
{ PORT_SWITCH_ID_6123_A2, "Marvell 88E6123 (A2)" },
24+
{ PORT_SWITCH_ID_6161, "Marvell 88E6161" },
25+
{ PORT_SWITCH_ID_6161_A1, "Marvell 88E6161 (A1)" },
26+
{ PORT_SWITCH_ID_6161_A2, "Marvell 88E6161 (A2)" },
27+
{ PORT_SWITCH_ID_6165, "Marvell 88E6165" },
28+
{ PORT_SWITCH_ID_6165_A1, "Marvell 88E6165 (A1)" },
29+
{ PORT_SWITCH_ID_6165_A2, "Marvell 88e6165 (A2)" },
30+
};
31+
2032
static char *mv88e6123_61_65_probe(struct device *host_dev, int sw_addr)
2133
{
22-
struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
23-
int ret;
24-
25-
if (bus == NULL)
26-
return NULL;
27-
28-
ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
29-
if (ret >= 0) {
30-
if (ret == PORT_SWITCH_ID_6123_A1)
31-
return "Marvell 88E6123 (A1)";
32-
if (ret == PORT_SWITCH_ID_6123_A2)
33-
return "Marvell 88E6123 (A2)";
34-
if ((ret & 0xfff0) == PORT_SWITCH_ID_6123)
35-
return "Marvell 88E6123";
36-
37-
if (ret == PORT_SWITCH_ID_6161_A1)
38-
return "Marvell 88E6161 (A1)";
39-
if (ret == PORT_SWITCH_ID_6161_A2)
40-
return "Marvell 88E6161 (A2)";
41-
if ((ret & 0xfff0) == PORT_SWITCH_ID_6161)
42-
return "Marvell 88E6161";
43-
44-
if (ret == PORT_SWITCH_ID_6165_A1)
45-
return "Marvell 88E6165 (A1)";
46-
if (ret == PORT_SWITCH_ID_6165_A2)
47-
return "Marvell 88e6165 (A2)";
48-
if ((ret & 0xfff0) == PORT_SWITCH_ID_6165)
49-
return "Marvell 88E6165";
50-
}
51-
52-
return NULL;
34+
return mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6123_61_65_table,
35+
ARRAY_SIZE(mv88e6123_61_65_table));
5336
}
5437

5538
static int mv88e6123_61_65_setup_global(struct dsa_switch *ds)

drivers/net/dsa/mv88e6131.c

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,18 @@
1717
#include <net/dsa.h>
1818
#include "mv88e6xxx.h"
1919

20+
static const struct mv88e6xxx_switch_id mv88e6131_table[] = {
21+
{ PORT_SWITCH_ID_6085, "Marvell 88E6085" },
22+
{ PORT_SWITCH_ID_6095, "Marvell 88E6095/88E6095F" },
23+
{ PORT_SWITCH_ID_6131, "Marvell 88E6131" },
24+
{ PORT_SWITCH_ID_6131_B2, "Marvell 88E6131 (B2)" },
25+
{ PORT_SWITCH_ID_6185, "Marvell 88E6185" },
26+
};
27+
2028
static char *mv88e6131_probe(struct device *host_dev, int sw_addr)
2129
{
22-
struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
23-
int ret;
24-
25-
if (bus == NULL)
26-
return NULL;
27-
28-
ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
29-
if (ret >= 0) {
30-
int ret_masked = ret & 0xfff0;
31-
32-
if (ret_masked == PORT_SWITCH_ID_6085)
33-
return "Marvell 88E6085";
34-
if (ret_masked == PORT_SWITCH_ID_6095)
35-
return "Marvell 88E6095/88E6095F";
36-
if (ret == PORT_SWITCH_ID_6131_B2)
37-
return "Marvell 88E6131 (B2)";
38-
if (ret_masked == PORT_SWITCH_ID_6131)
39-
return "Marvell 88E6131";
40-
if (ret_masked == PORT_SWITCH_ID_6185)
41-
return "Marvell 88E6185";
42-
}
43-
44-
return NULL;
30+
return mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6131_table,
31+
ARRAY_SIZE(mv88e6131_table));
4532
}
4633

4734
static int mv88e6131_setup_global(struct dsa_switch *ds)

drivers/net/dsa/mv88e6171.c

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,17 @@
1717
#include <net/dsa.h>
1818
#include "mv88e6xxx.h"
1919

20+
static const struct mv88e6xxx_switch_id mv88e6171_table[] = {
21+
{ PORT_SWITCH_ID_6171, "Marvell 88E6171" },
22+
{ PORT_SWITCH_ID_6175, "Marvell 88E6175" },
23+
{ PORT_SWITCH_ID_6350, "Marvell 88E6350" },
24+
{ PORT_SWITCH_ID_6351, "Marvell 88E6351" },
25+
};
26+
2027
static char *mv88e6171_probe(struct device *host_dev, int sw_addr)
2128
{
22-
struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
23-
int ret;
24-
25-
if (bus == NULL)
26-
return NULL;
27-
28-
ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
29-
if (ret >= 0) {
30-
if ((ret & 0xfff0) == PORT_SWITCH_ID_6171)
31-
return "Marvell 88E6171";
32-
if ((ret & 0xfff0) == PORT_SWITCH_ID_6175)
33-
return "Marvell 88E6175";
34-
if ((ret & 0xfff0) == PORT_SWITCH_ID_6350)
35-
return "Marvell 88E6350";
36-
if ((ret & 0xfff0) == PORT_SWITCH_ID_6351)
37-
return "Marvell 88E6351";
38-
}
39-
40-
return NULL;
29+
return mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6171_table,
30+
ARRAY_SIZE(mv88e6171_table));
4131
}
4232

4333
static int mv88e6171_setup_global(struct dsa_switch *ds)

drivers/net/dsa/mv88e6352.c

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,24 @@
2222
#include <net/dsa.h>
2323
#include "mv88e6xxx.h"
2424

25+
static const struct mv88e6xxx_switch_id mv88e6352_table[] = {
26+
{ PORT_SWITCH_ID_6172, "Marvell 88E6172" },
27+
{ PORT_SWITCH_ID_6176, "Marvell 88E6176" },
28+
{ PORT_SWITCH_ID_6320, "Marvell 88E6320" },
29+
{ PORT_SWITCH_ID_6320_A1, "Marvell 88E6320 (A1)" },
30+
{ PORT_SWITCH_ID_6320_A2, "Marvell 88e6320 (A2)" },
31+
{ PORT_SWITCH_ID_6321, "Marvell 88E6321" },
32+
{ PORT_SWITCH_ID_6321_A1, "Marvell 88E6321 (A1)" },
33+
{ PORT_SWITCH_ID_6321_A2, "Marvell 88e6321 (A2)" },
34+
{ PORT_SWITCH_ID_6352, "Marvell 88E6352" },
35+
{ PORT_SWITCH_ID_6352_A0, "Marvell 88E6352 (A0)" },
36+
{ PORT_SWITCH_ID_6352_A1, "Marvell 88E6352 (A1)" },
37+
};
38+
2539
static char *mv88e6352_probe(struct device *host_dev, int sw_addr)
2640
{
27-
struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
28-
int ret;
29-
30-
if (bus == NULL)
31-
return NULL;
32-
33-
ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
34-
if (ret >= 0) {
35-
if ((ret & 0xfff0) == PORT_SWITCH_ID_6172)
36-
return "Marvell 88E6172";
37-
if ((ret & 0xfff0) == PORT_SWITCH_ID_6176)
38-
return "Marvell 88E6176";
39-
if (ret == PORT_SWITCH_ID_6320_A1)
40-
return "Marvell 88E6320 (A1)";
41-
if (ret == PORT_SWITCH_ID_6320_A2)
42-
return "Marvell 88e6320 (A2)";
43-
if ((ret & 0xfff0) == PORT_SWITCH_ID_6320)
44-
return "Marvell 88E6320";
45-
if (ret == PORT_SWITCH_ID_6321_A1)
46-
return "Marvell 88E6321 (A1)";
47-
if (ret == PORT_SWITCH_ID_6321_A2)
48-
return "Marvell 88e6321 (A2)";
49-
if ((ret & 0xfff0) == PORT_SWITCH_ID_6321)
50-
return "Marvell 88E6321";
51-
if (ret == PORT_SWITCH_ID_6352_A0)
52-
return "Marvell 88E6352 (A0)";
53-
if (ret == PORT_SWITCH_ID_6352_A1)
54-
return "Marvell 88E6352 (A1)";
55-
if ((ret & 0xfff0) == PORT_SWITCH_ID_6352)
56-
return "Marvell 88E6352";
57-
}
58-
59-
return NULL;
41+
return mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6352_table,
42+
ARRAY_SIZE(mv88e6352_table));
6043
}
6144

6245
static int mv88e6352_setup_global(struct dsa_switch *ds)

drivers/net/dsa/mv88e6xxx.c

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
5959
return -ETIMEDOUT;
6060
}
6161

62-
int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg)
62+
static int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr,
63+
int reg)
6364
{
6465
int ret;
6566

@@ -122,8 +123,8 @@ int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
122123
return ret;
123124
}
124125

125-
int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
126-
int reg, u16 val)
126+
static int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
127+
int reg, u16 val)
127128
{
128129
int ret;
129130

@@ -2546,6 +2547,38 @@ int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
25462547
}
25472548
#endif /* CONFIG_NET_DSA_HWMON */
25482549

2550+
char *mv88e6xxx_lookup_name(struct device *host_dev, int sw_addr,
2551+
const struct mv88e6xxx_switch_id *table,
2552+
unsigned int num)
2553+
{
2554+
struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
2555+
int i, ret;
2556+
2557+
if (!bus)
2558+
return NULL;
2559+
2560+
ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
2561+
if (ret < 0)
2562+
return NULL;
2563+
2564+
/* Look up the exact switch ID */
2565+
for (i = 0; i < num; ++i)
2566+
if (table[i].id == ret)
2567+
return table[i].name;
2568+
2569+
/* Look up only the product number */
2570+
for (i = 0; i < num; ++i) {
2571+
if (table[i].id == (ret & PORT_SWITCH_ID_PROD_NUM_MASK)) {
2572+
dev_warn(host_dev, "unknown revision %d, using base switch 0x%x\n",
2573+
ret & PORT_SWITCH_ID_REV_MASK,
2574+
ret & PORT_SWITCH_ID_PROD_NUM_MASK);
2575+
return table[i].name;
2576+
}
2577+
}
2578+
2579+
return NULL;
2580+
}
2581+
25492582
static int __init mv88e6xxx_init(void)
25502583
{
25512584
#if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)

drivers/net/dsa/mv88e6xxx.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@
6060
#define PORT_PCS_CTRL_UNFORCED 0x03
6161
#define PORT_PAUSE_CTRL 0x02
6262
#define PORT_SWITCH_ID 0x03
63+
#define PORT_SWITCH_ID_PROD_NUM_MASK 0xfff0
64+
#define PORT_SWITCH_ID_REV_MASK 0x000f
6365
#define PORT_SWITCH_ID_6031 0x0310
6466
#define PORT_SWITCH_ID_6035 0x0350
6567
#define PORT_SWITCH_ID_6046 0x0480
@@ -347,6 +349,11 @@
347349
#define GLOBAL2_QOS_WEIGHT 0x1c
348350
#define GLOBAL2_MISC 0x1d
349351

352+
struct mv88e6xxx_switch_id {
353+
u16 id;
354+
char *name;
355+
};
356+
350357
struct mv88e6xxx_atu_entry {
351358
u16 fid;
352359
u8 state;
@@ -415,13 +422,13 @@ struct mv88e6xxx_hw_stat {
415422
};
416423

417424
int mv88e6xxx_switch_reset(struct dsa_switch *ds, bool ppu_active);
425+
char *mv88e6xxx_lookup_name(struct device *host_dev, int sw_addr,
426+
const struct mv88e6xxx_switch_id *table,
427+
unsigned int num);
418428
int mv88e6xxx_setup_ports(struct dsa_switch *ds);
419429
int mv88e6xxx_setup_common(struct dsa_switch *ds);
420430
int mv88e6xxx_setup_global(struct dsa_switch *ds);
421-
int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg);
422431
int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg);
423-
int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
424-
int reg, u16 val);
425432
int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val);
426433
int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr);
427434
int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr);

0 commit comments

Comments
 (0)