2 * ar8216.c: AR8216 switch driver
4 * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/list.h>
21 #include <linux/if_ether.h>
22 #include <linux/skbuff.h>
23 #include <linux/netdevice.h>
24 #include <linux/netlink.h>
25 #include <linux/bitops.h>
26 #include <net/genetlink.h>
27 #include <linux/switch.h>
28 #include <linux/delay.h>
29 #include <linux/phy.h>
32 #define AR8216_REG_PORT_RATE(_i) (AR8216_PORT_OFFSET(_i) + 0x000c)
33 #define AR8216_REG_PORT_PRIO(_i) (AR8216_PORT_OFFSET(_i) + 0x0010)
36 struct switch_dev dev
;
37 struct phy_device
*phy
;
38 u32 (*read
)(struct ar8216_priv
*priv
, int reg
);
39 void (*write
)(struct ar8216_priv
*priv
, int reg
, u32 val
);
40 /* all fields below are cleared on reset */
42 u8 vlan_id
[AR8216_NUM_VLANS
];
43 u8 vlan_table
[AR8216_NUM_VLANS
];
45 u16 pvid
[AR8216_NUM_PORTS
];
47 static struct switch_dev athdev
;
49 #define to_ar8216(_dev) container_of(_dev, struct ar8216_priv, dev)
52 split_addr(u32 regaddr
, u16
*r1
, u16
*r2
, u16
*page
)
61 *page
= regaddr
& 0x1ff;
65 ar8216_mii_read(struct ar8216_priv
*priv
, int reg
)
67 struct phy_device
*phy
= priv
->phy
;
71 split_addr((u32
) reg
, &r1
, &r2
, &page
);
72 phy
->bus
->write(phy
->bus
, 0x18, 0, page
);
73 lo
= phy
->bus
->read(phy
->bus
, 0x10 | r2
, r1
);
74 hi
= phy
->bus
->read(phy
->bus
, 0x10 | r2
, r1
+ 1);
76 return (hi
<< 16) | lo
;
80 ar8216_mii_write(struct ar8216_priv
*priv
, int reg
, u32 val
)
82 struct phy_device
*phy
= priv
->phy
;
86 split_addr((u32
) reg
, &r1
, &r2
, &r3
);
87 phy
->bus
->write(phy
->bus
, 0x18, 0, r3
);
90 hi
= (u16
) (val
>> 16);
91 phy
->bus
->write(phy
->bus
, 0x10 | r2
, r1
+ 1, hi
);
92 phy
->bus
->write(phy
->bus
, 0x10 | r2
, r1
, lo
);
96 ar8216_rmw(struct ar8216_priv
*priv
, int reg
, u32 mask
, u32 val
)
100 v
= priv
->read(priv
, reg
);
103 priv
->write(priv
, reg
, v
);
109 ar8216_set_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
,
110 struct switch_val
*val
)
112 struct ar8216_priv
*priv
= to_ar8216(dev
);
113 priv
->vlan
= !!val
->value
.i
;
118 ar8216_get_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
,
119 struct switch_val
*val
)
121 struct ar8216_priv
*priv
= to_ar8216(dev
);
122 val
->value
.i
= priv
->vlan
;
128 ar8216_set_pvid(struct switch_dev
*dev
, int port
, int vlan
)
130 struct ar8216_priv
*priv
= to_ar8216(dev
);
131 priv
->pvid
[port
] = vlan
;
136 ar8216_get_pvid(struct switch_dev
*dev
, int port
, int *vlan
)
138 struct ar8216_priv
*priv
= to_ar8216(dev
);
139 *vlan
= priv
->pvid
[port
];
144 ar8216_set_vid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
145 struct switch_val
*val
)
147 struct ar8216_priv
*priv
= to_ar8216(dev
);
148 priv
->vlan_id
[val
->port_vlan
] = val
->value
.i
;
153 ar8216_get_vid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
154 struct switch_val
*val
)
156 struct ar8216_priv
*priv
= to_ar8216(dev
);
157 val
->value
.i
= priv
->vlan_id
[val
->port_vlan
];
162 static struct switch_attr ar8216_globals
[] = {
164 .type
= SWITCH_TYPE_INT
,
166 .description
= "Enable VLAN mode",
167 .set
= ar8216_set_vlan
,
168 .get
= ar8216_get_vlan
,
173 static struct switch_attr ar8216_port
[] = {
176 static struct switch_attr ar8216_vlan
[] = {
178 .type
= SWITCH_TYPE_INT
,
180 .description
= "VLAN ID",
181 .set
= ar8216_set_vid
,
182 .get
= ar8216_get_vid
,
189 ar8216_get_ports(struct switch_dev
*dev
, struct switch_val
*val
)
191 struct ar8216_priv
*priv
= to_ar8216(dev
);
192 u8 ports
= priv
->vlan_table
[val
->port_vlan
];
196 for (i
= 0; i
< AR8216_NUM_PORTS
; i
++) {
197 struct switch_port
*p
;
199 if (!(ports
& (1 << i
)))
202 p
= &val
->value
.ports
[val
->len
++];
204 if (priv
->vlan_tagged
& (1 << i
))
205 p
->flags
= (1 << SWITCH_PORT_FLAG_TAGGED
);
213 ar8216_set_ports(struct switch_dev
*dev
, struct switch_val
*val
)
215 struct ar8216_priv
*priv
= to_ar8216(dev
);
216 u8
*vt
= &priv
->vlan_table
[val
->port_vlan
];
220 for (i
= 0; i
< val
->len
; i
++) {
221 struct switch_port
*p
= &val
->value
.ports
[i
];
223 if (p
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
))
224 priv
->vlan_tagged
|= (1 << p
->id
);
226 priv
->vlan_tagged
&= ~(1 << p
->id
);
227 priv
->pvid
[p
->id
] = val
->port_vlan
;
229 /* make sure that an untagged port does not
230 * appear in other vlans */
231 for (j
= 0; j
< AR8216_NUM_VLANS
; j
++) {
232 if (j
== val
->port_vlan
)
234 priv
->vlan_table
[j
] &= ~(1 << p
->id
);
244 ar8216_wait_bit(struct ar8216_priv
*priv
, int reg
, u32 mask
, u32 val
)
248 while ((priv
->read(priv
, reg
) & mask
) != val
) {
249 if (timeout
-- <= 0) {
250 printk(KERN_ERR
"ar8216: timeout waiting for operation to complete\n");
258 ar8216_vtu_op(struct ar8216_priv
*priv
, u32 op
, u32 val
)
260 if (ar8216_wait_bit(priv
, AR8216_REG_VTU
, AR8216_VTU_ACTIVE
, 0))
262 if ((op
& AR8216_VTU_OP
) == AR8216_VTU_OP_LOAD
) {
263 val
&= AR8216_VTUDATA_MEMBER
;
264 val
|= AR8216_VTUDATA_VALID
;
265 priv
->write(priv
, AR8216_REG_VTU_DATA
, val
);
267 op
|= AR8216_VTU_ACTIVE
;
268 priv
->write(priv
, AR8216_REG_VTU
, op
);
272 ar8216_hw_apply(struct switch_dev
*dev
)
274 struct ar8216_priv
*priv
= to_ar8216(dev
);
275 u8 portmask
[AR8216_NUM_PORTS
];
278 /* flush all vlan translation unit entries */
279 ar8216_vtu_op(priv
, AR8216_VTU_OP_FLUSH
, 0);
281 memset(portmask
, 0, sizeof(portmask
));
283 /* calculate the port destination masks and load vlans
284 * into the vlan translation unit */
285 for (j
= 0; j
< AR8216_NUM_VLANS
; j
++) {
286 u8 vp
= priv
->vlan_table
[j
];
291 for (i
= 0; i
< AR8216_NUM_PORTS
; i
++) {
294 portmask
[i
] |= vp
& ~mask
;
297 if (!priv
->vlan_table
[j
])
302 (priv
->vlan_id
[j
] << AR8216_VTU_VID_S
),
303 priv
->vlan_table
[j
]);
307 * isolate all ports, but connect them to the cpu port */
308 for (i
= 0; i
< AR8216_NUM_PORTS
; i
++) {
309 if (i
== AR8216_PORT_CPU
)
312 portmask
[i
] = 1 << AR8216_PORT_CPU
;
313 portmask
[AR8216_PORT_CPU
] |= (1 << i
);
317 /* update the port destination mask registers and tag settings */
318 for (i
= 0; i
< AR8216_NUM_PORTS
; i
++) {
323 pvid
= priv
->vlan_id
[priv
->pvid
[i
]];
328 if (priv
->vlan
&& (priv
->vlan_tagged
& (1 << i
))) {
329 egress
= AR8216_OUT_ADD_VLAN
;
330 ingress
= AR8216_IN_PORT_FALLBACK
;
332 egress
= AR8216_OUT_STRIP_VLAN
;
333 ingress
= AR8216_IN_SECURE
;
336 ar8216_rmw(priv
, AR8216_REG_PORT_CTRL(i
),
337 AR8216_PORT_CTRL_LEARN
| AR8216_PORT_CTRL_VLAN_MODE
|
338 AR8216_PORT_CTRL_SINGLE_VLAN
| AR8216_PORT_CTRL_STATE
|
339 AR8216_PORT_CTRL_HEADER
| AR8216_PORT_CTRL_LEARN_LOCK
,
340 AR8216_PORT_CTRL_LEARN
|
341 (egress
<< AR8216_PORT_CTRL_VLAN_MODE_S
) |
342 (priv
->vlan
? AR8216_PORT_CTRL_SINGLE_VLAN
: 0) |
343 (AR8216_PORT_STATE_FORWARD
<< AR8216_PORT_CTRL_STATE_S
));
345 ar8216_rmw(priv
, AR8216_REG_PORT_VLAN(i
),
346 AR8216_PORT_VLAN_DEST_PORTS
| AR8216_PORT_VLAN_MODE
|
347 AR8216_PORT_VLAN_DEFAULT_ID
,
348 (portmask
[i
] << AR8216_PORT_VLAN_DEST_PORTS_S
) |
349 (ingress
<< AR8216_PORT_VLAN_MODE_S
) |
350 (pvid
<< AR8216_PORT_VLAN_DEFAULT_ID_S
));
357 ar8216_reset_switch(struct switch_dev
*dev
)
359 struct ar8216_priv
*priv
= to_ar8216(dev
);
362 memset(&priv
->vlan
, 0, sizeof(struct ar8216_priv
) -
363 offsetof(struct ar8216_priv
, vlan
));
364 for (i
= 0; i
< AR8216_NUM_VLANS
; i
++) {
365 priv
->vlan_id
[i
] = i
;
367 for (i
= 0; i
< AR8216_NUM_PORTS
; i
++) {
368 /* Enable port learning and tx */
369 priv
->write(priv
, AR8216_REG_PORT_CTRL(i
),
370 AR8216_PORT_CTRL_LEARN
|
371 (4 << AR8216_PORT_CTRL_STATE_S
));
373 priv
->write(priv
, AR8216_REG_PORT_VLAN(i
), 0);
375 /* Configure all PHYs */
376 if (i
== AR8216_PORT_CPU
) {
377 priv
->write(priv
, AR8216_REG_PORT_STATUS(i
),
378 AR8216_PORT_STATUS_LINK_UP
|
379 AR8216_PORT_STATUS_SPEED
|
380 AR8216_PORT_STATUS_TXMAC
|
381 AR8216_PORT_STATUS_RXMAC
|
382 AR8216_PORT_STATUS_DUPLEX
);
384 priv
->write(priv
, AR8216_REG_PORT_STATUS(i
),
385 AR8216_PORT_STATUS_LINK_AUTO
);
388 /* XXX: undocumented magic from atheros, required! */
389 priv
->write(priv
, 0x38, 0xc000050e);
390 return ar8216_hw_apply(dev
);
394 ar8216_config_init(struct phy_device
*pdev
)
396 struct ar8216_priv
*priv
;
399 printk("%s: AR8216 PHY driver attached.\n", pdev
->attached_dev
->name
);
400 pdev
->supported
= ADVERTISED_100baseT_Full
;
401 pdev
->advertising
= ADVERTISED_100baseT_Full
;
403 priv
= kzalloc(sizeof(struct ar8216_priv
), GFP_KERNEL
);
408 priv
->read
= ar8216_mii_read
;
409 priv
->write
= ar8216_mii_write
;
410 memcpy(&priv
->dev
, &athdev
, sizeof(struct switch_dev
));
412 if ((ret
= register_switch(&priv
->dev
, pdev
->attached_dev
)) < 0) {
417 ret
= ar8216_reset_switch(&priv
->dev
);
423 ar8216_read_status(struct phy_device
*phydev
)
425 struct ar8216_priv
*priv
= phydev
->priv
;
427 phydev
->speed
= SPEED_100
;
428 phydev
->duplex
= DUPLEX_FULL
;
429 phydev
->state
= PHY_UP
;
431 /* flush the address translation unit */
432 if (ar8216_wait_bit(priv
, AR8216_REG_ATU
, AR8216_ATU_ACTIVE
, 0))
435 priv
->write(priv
, AR8216_REG_ATU
, AR8216_ATU_OP_FLUSH
);
441 ar8216_config_aneg(struct phy_device
*phydev
)
447 ar8216_probe(struct phy_device
*pdev
)
449 struct ar8216_priv priv
;
455 val
= ar8216_mii_read(&priv
, AR8216_REG_CTRL
);
457 id
= (val
>> 8) & 0xff;
458 if ((id
!= 1) || (rev
!= 1))
465 ar8216_remove(struct phy_device
*pdev
)
467 struct ar8216_priv
*priv
= pdev
->priv
;
472 unregister_switch(&priv
->dev
);
477 static struct switch_dev athdev
= {
478 .name
= "Atheros AR8216",
479 .cpu_port
= AR8216_PORT_CPU
,
480 .ports
= AR8216_NUM_PORTS
,
481 .vlans
= AR8216_NUM_VLANS
,
483 .attr
= ar8216_globals
,
484 .n_attr
= ARRAY_SIZE(ar8216_globals
),
488 .n_attr
= ARRAY_SIZE(ar8216_port
),
492 .n_attr
= ARRAY_SIZE(ar8216_vlan
),
494 .get_port_pvid
= ar8216_get_pvid
,
495 .set_port_pvid
= ar8216_set_pvid
,
496 .get_vlan_ports
= ar8216_get_ports
,
497 .set_vlan_ports
= ar8216_set_ports
,
498 .apply_config
= ar8216_hw_apply
,
499 .reset_switch
= ar8216_reset_switch
,
502 static struct phy_driver ar8216_driver
= {
503 .name
= "Atheros AR8216",
504 .features
= PHY_BASIC_FEATURES
,
505 .probe
= ar8216_probe
,
506 .remove
= ar8216_remove
,
507 .config_init
= &ar8216_config_init
,
508 .config_aneg
= &ar8216_config_aneg
,
509 .read_status
= &ar8216_read_status
,
510 .driver
= { .owner
= THIS_MODULE
},
516 return phy_driver_register(&ar8216_driver
);
522 phy_driver_unregister(&ar8216_driver
);
525 module_init(ar8216_init
);
526 module_exit(ar8216_exit
);
527 MODULE_LICENSE("GPL");