blob: b0ba4be0a6e427c0f91c3fab5f5f0fba71eced44 [file] [log] [blame]
developerac95e9f2024-03-06 21:54:37 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Airoha AN8855 DSA Switch driver
4 * Copyright (C) 2023 Min Yao <min.yao@airoha.com>
5 */
6#include <linux/etherdevice.h>
7#include <linux/if_bridge.h>
8#include <linux/iopoll.h>
9#include <linux/mdio.h>
10#include <linux/mfd/syscon.h>
11#include <linux/module.h>
12#include <linux/netdevice.h>
13#include <linux/of_mdio.h>
14#include <linux/of_net.h>
15#include <linux/of_platform.h>
16#include <linux/phylink.h>
17#include <linux/regmap.h>
18#include <linux/regulator/consumer.h>
19#include <linux/reset.h>
20#include <linux/gpio/consumer.h>
21#include <net/dsa.h>
22#include <linux/of_address.h>
23
24#include "an8855.h"
25#include "an8855_nl.h"
26
27/* AN8855 driver version */
28#define ARHT_AN8855_DSA_DRIVER_VER "1.0.1-L5.4"
29
30/* String, offset, and register size in bytes if different from 4 bytes */
31static const struct an8855_mib_desc an8855_mib[] = {
32 MIB_DESC(1, 0x00, "TxDrop"),
33 MIB_DESC(1, 0x04, "TxCrcErr"),
34 MIB_DESC(1, 0x08, "TxUnicast"),
35 MIB_DESC(1, 0x0c, "TxMulticast"),
36 MIB_DESC(1, 0x10, "TxBroadcast"),
37 MIB_DESC(1, 0x14, "TxCollision"),
38 MIB_DESC(1, 0x18, "TxSingleCollision"),
39 MIB_DESC(1, 0x1c, "TxMultipleCollision"),
40 MIB_DESC(1, 0x20, "TxDeferred"),
41 MIB_DESC(1, 0x24, "TxLateCollision"),
42 MIB_DESC(1, 0x28, "TxExcessiveCollistion"),
43 MIB_DESC(1, 0x2c, "TxPause"),
44 MIB_DESC(1, 0x30, "TxPktSz64"),
45 MIB_DESC(1, 0x34, "TxPktSz65To127"),
46 MIB_DESC(1, 0x38, "TxPktSz128To255"),
47 MIB_DESC(1, 0x3c, "TxPktSz256To511"),
48 MIB_DESC(1, 0x40, "TxPktSz512To1023"),
49 MIB_DESC(1, 0x44, "TxPktSz1024To1518"),
50 MIB_DESC(1, 0x48, "TxPktSz1519ToMax"),
51 MIB_DESC(2, 0x4c, "TxBytes"),
52 MIB_DESC(1, 0x54, "TxOversizeDrop"),
53 MIB_DESC(2, 0x58, "TxBadPktBytes"),
54 MIB_DESC(1, 0x80, "RxDrop"),
55 MIB_DESC(1, 0x84, "RxFiltering"),
56 MIB_DESC(1, 0x88, "RxUnicast"),
57 MIB_DESC(1, 0x8c, "RxMulticast"),
58 MIB_DESC(1, 0x90, "RxBroadcast"),
59 MIB_DESC(1, 0x94, "RxAlignErr"),
60 MIB_DESC(1, 0x98, "RxCrcErr"),
61 MIB_DESC(1, 0x9c, "RxUnderSizeErr"),
62 MIB_DESC(1, 0xa0, "RxFragErr"),
63 MIB_DESC(1, 0xa4, "RxOverSzErr"),
64 MIB_DESC(1, 0xa8, "RxJabberErr"),
65 MIB_DESC(1, 0xac, "RxPause"),
66 MIB_DESC(1, 0xb0, "RxPktSz64"),
67 MIB_DESC(1, 0xb4, "RxPktSz65To127"),
68 MIB_DESC(1, 0xb8, "RxPktSz128To255"),
69 MIB_DESC(1, 0xbc, "RxPktSz256To511"),
70 MIB_DESC(1, 0xc0, "RxPktSz512To1023"),
71 MIB_DESC(1, 0xc4, "RxPktSz1024To1518"),
72 MIB_DESC(1, 0xc8, "RxPktSz1519ToMax"),
73 MIB_DESC(2, 0xcc, "RxBytes"),
74 MIB_DESC(1, 0xd4, "RxCtrlDrop"),
75 MIB_DESC(1, 0xd8, "RxIngressDrop"),
76 MIB_DESC(1, 0xdc, "RxArlDrop"),
77 MIB_DESC(1, 0xe0, "FlowControlDrop"),
78 MIB_DESC(1, 0xe4, "WredDrop"),
79 MIB_DESC(1, 0xe8, "MirrorDrop"),
80 MIB_DESC(2, 0xec, "RxBadPktBytes"),
81 MIB_DESC(1, 0xf4, "RxsFlowSamplingPktDrop"),
82 MIB_DESC(1, 0xf8, "RxsFlowTotalPktDrop"),
83 MIB_DESC(1, 0xfc, "PortControlDrop"),
84};
85
86static int
87an8855_mii_write(struct an8855_priv *priv, u32 reg, u32 val)
88{
89 struct mii_bus *bus = priv->bus;
90 int ret = 0;
91
92 ret = bus->write(bus, priv->phy_base, 0x1f, 0x4);
93 ret = bus->write(bus, priv->phy_base, 0x10, 0);
94
95 ret = bus->write(bus, priv->phy_base, 0x11, ((reg >> 16) & 0xFFFF));
96 ret = bus->write(bus, priv->phy_base, 0x12, (reg & 0xFFFF));
97
98 ret = bus->write(bus, priv->phy_base, 0x13, ((val >> 16) & 0xFFFF));
99 ret = bus->write(bus, priv->phy_base, 0x14, (val & 0xFFFF));
100
101 ret = bus->write(bus, priv->phy_base, 0x1f, 0);
102 ret = bus->write(bus, priv->phy_base, 0x10, 0);
103
104 if (ret < 0) {
105 dev_err(&bus->dev, "failed to write an8855 register\n");
106 return ret;
107 }
108
109 return ret;
110}
111
112static u32
113an8855_mii_read(struct an8855_priv *priv, u32 reg)
114{
115 struct mii_bus *bus = priv->bus;
116 u16 lo, hi;
117 int ret;
118
119 ret = bus->write(bus, priv->phy_base, 0x1f, 0x4);
120 ret = bus->write(bus, priv->phy_base, 0x10, 0);
121
122 ret = bus->write(bus, priv->phy_base, 0x15, ((reg >> 16) & 0xFFFF));
123 ret = bus->write(bus, priv->phy_base, 0x16, (reg & 0xFFFF));
124 if (ret < 0) {
125 dev_err(&bus->dev, "failed to read an8855 register\n");
126 return ret;
127 }
128
129 lo = bus->read(bus, priv->phy_base, 0x18);
130 hi = bus->read(bus, priv->phy_base, 0x17);
131
132 ret = bus->write(bus, priv->phy_base, 0x1f, 0);
133 ret = bus->write(bus, priv->phy_base, 0x10, 0);
134 if (ret < 0) {
135 dev_err(&bus->dev, "failed to read an8855 register\n");
136 return ret;
137 }
138
139 return (hi << 16) | (lo & 0xffff);
140}
141
142void
143an8855_write(struct an8855_priv *priv, u32 reg, u32 val)
144{
145 struct mii_bus *bus = priv->bus;
146
147 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
148
149 an8855_mii_write(priv, reg, val);
150
151 mutex_unlock(&bus->mdio_lock);
152}
153
154static u32
155_an8855_read(struct an8855_dummy_poll *p)
156{
157 struct mii_bus *bus = p->priv->bus;
158 u32 val;
159
160 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
161
162 val = an8855_mii_read(p->priv, p->reg);
163
164 mutex_unlock(&bus->mdio_lock);
165
166 return val;
167}
168
169u32
170an8855_read(struct an8855_priv *priv, u32 reg)
171{
172 struct an8855_dummy_poll p;
173
174 INIT_AN8855_DUMMY_POLL(&p, priv, reg);
175 return _an8855_read(&p);
176}
177
178static void
179an8855_rmw(struct an8855_priv *priv, u32 reg, u32 mask, u32 set)
180{
181 struct mii_bus *bus = priv->bus;
182 u32 val;
183
184 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
185
186 val = an8855_mii_read(priv, reg);
187 val &= ~mask;
188 val |= set;
189 an8855_mii_write(priv, reg, val);
190
191 mutex_unlock(&bus->mdio_lock);
192}
193
194static void
195an8855_set(struct an8855_priv *priv, u32 reg, u32 val)
196{
197 an8855_rmw(priv, reg, 0, val);
198}
199
200static void
201an8855_clear(struct an8855_priv *priv, u32 reg, u32 val)
202{
203 an8855_rmw(priv, reg, val, 0);
204}
205
206static int
207an8855_fdb_cmd(struct an8855_priv *priv, u32 cmd, u32 *rsp)
208{
209 u32 val;
210 int ret;
211 struct an8855_dummy_poll p;
212
213 /* Set the command operating upon the MAC address entries */
214 val = ATC_BUSY | cmd;
215 an8855_write(priv, AN8855_ATC, val);
216
217 INIT_AN8855_DUMMY_POLL(&p, priv, AN8855_ATC);
218 ret = readx_poll_timeout(_an8855_read, &p, val,
219 !(val & ATC_BUSY), 20, 200000);
220 if (ret < 0) {
221 dev_err(priv->dev, "reset timeout\n");
222 return ret;
223 }
224
225 if (rsp)
226 *rsp = val;
227
228 return 0;
229}
230
231static void
232an8855_fdb_read(struct an8855_priv *priv, struct an8855_fdb *fdb)
233{
234 u32 reg[4];
235 int i;
236
237 /* Read from ARL table into an array */
238 for (i = 0; i < 4; i++)
239 reg[i] = an8855_read(priv, AN8855_ATRD0 + (i * 4));
240
241 fdb->live = reg[0] & 0x1;
242 fdb->type = (reg[0] >> 3) & 0x3;
243 fdb->ivl = (reg[0] >> 9) & 0x1;
244 fdb->vid = (reg[0] >> 10) & 0xfff;
245 fdb->fid = (reg[0] >> 25) & 0xf;
246 fdb->aging = (reg[1] >> 3) & 0x1ff;
247 fdb->port_mask = reg[3] & 0xff;
248 fdb->mac[0] = (reg[2] >> MAC_BYTE_0) & MAC_BYTE_MASK;
249 fdb->mac[1] = (reg[2] >> MAC_BYTE_1) & MAC_BYTE_MASK;
250 fdb->mac[2] = (reg[2] >> MAC_BYTE_2) & MAC_BYTE_MASK;
251 fdb->mac[3] = (reg[2] >> MAC_BYTE_3) & MAC_BYTE_MASK;
252 fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK;
253 fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK;
254 fdb->noarp = !!((reg[0] >> 1) & 0x3);
255}
256
257static void
258an8855_fdb_write(struct an8855_priv *priv, u16 vid,
259 u8 port_mask, const u8 *mac, u8 add)
260{
261 u32 reg = 0;
262
263 reg |= mac[3] << MAC_BYTE_3;
264 reg |= mac[2] << MAC_BYTE_2;
265 reg |= mac[1] << MAC_BYTE_1;
266 reg |= mac[0] << MAC_BYTE_0;
267 an8855_write(priv, AN8855_ATA1, reg);
268 reg = 0;
269 reg |= mac[5] << MAC_BYTE_5;
270 reg |= mac[4] << MAC_BYTE_4;
271 an8855_write(priv, AN8855_ATA2, reg);
272 reg = 0;
273 if (add)
274 reg |= 0x1;
275 reg |= 0x1 << 15;
276 reg |= vid << 16;
277 an8855_write(priv, AN8855_ATWD, reg);
278 an8855_write(priv, AN8855_ATWD2, port_mask);
279}
280
281static int
282an8855_pad_setup(struct dsa_switch *ds, phy_interface_t interface)
283{
284 return 0;
285}
286
287static void
288an8855_mib_reset(struct dsa_switch *ds)
289{
290 struct an8855_priv *priv = ds->priv;
291
292 an8855_write(priv, AN8855_MIB_CCR, CCR_MIB_FLUSH);
293 an8855_write(priv, AN8855_MIB_CCR, CCR_MIB_ACTIVATE);
294}
295
296static int
297an8855_cl22_read(struct an8855_priv *priv, int port, int regnum)
298{
299 return mdiobus_read_nested(priv->bus, port, regnum);
300}
301
302static int
303an8855_cl22_write(struct an8855_priv *priv, int port, int regnum, u16 val)
304{
305 return mdiobus_write_nested(priv->bus, port, regnum, val);
306}
307
308static int
309an8855_phy_read(struct dsa_switch *ds, int port, int regnum)
310{
311 struct an8855_priv *priv = ds->priv;
312
313 port += priv->phy_base;
314 return an8855_cl22_read(ds->priv, port, regnum);
315}
316
317static int
318an8855_phy_write(struct dsa_switch *ds, int port, int regnum,
319 u16 val)
320{
321 struct an8855_priv *priv = ds->priv;
322
323 port += priv->phy_base;
324 return an8855_cl22_write(ds->priv, port, regnum, val);
325}
326
327static int
328an8855_cl45_read(struct an8855_priv *priv, int port, int devad, int regnum)
329{
330 regnum = MII_ADDR_C45 | (devad << 16) | regnum;
331 return mdiobus_read_nested(priv->bus, port, regnum);
332}
333
334static int
335an8855_cl45_write(struct an8855_priv *priv, int port, int devad, int regnum,
336 u16 val)
337{
338 regnum = MII_ADDR_C45 | (devad << 16) | regnum;
339 return mdiobus_write_nested(priv->bus, port, regnum, val);
340}
341
342int
343an8855_phy_cl22_read(struct an8855_priv *priv, int port, int regnum)
344{
345 port += priv->phy_base;
346 return an8855_cl22_read(priv, port, regnum);
347}
348
349int
350an8855_phy_cl22_write(struct an8855_priv *priv, int port, int regnum,
351 u16 val)
352{
353 port += priv->phy_base;
354 return an8855_cl22_write(priv, port, regnum, val);
355}
356
357int
358an8855_phy_cl45_read(struct an8855_priv *priv, int port, int devad, int regnum)
359{
360 port += priv->phy_base;
361 return an8855_cl45_read(priv, port, devad, regnum);
362}
363
364int
365an8855_phy_cl45_write(struct an8855_priv *priv, int port, int devad, int regnum,
366 u16 val)
367{
368 port += priv->phy_base;
369 return an8855_cl45_write(priv, port, devad, regnum, val);
370}
371
372static void
373an8855_get_strings(struct dsa_switch *ds, int port, u32 stringset,
374 uint8_t *data)
375{
376 int i;
377
378 if (stringset != ETH_SS_STATS)
379 return;
380
381 for (i = 0; i < ARRAY_SIZE(an8855_mib); i++)
382 strncpy(data + i * ETH_GSTRING_LEN, an8855_mib[i].name,
383 ETH_GSTRING_LEN);
384}
385
386static void
387an8855_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
388{
389 struct an8855_priv *priv = ds->priv;
390 const struct an8855_mib_desc *mib;
391 u32 reg, i;
392 u64 hi;
393
394 for (i = 0; i < ARRAY_SIZE(an8855_mib); i++) {
395 mib = &an8855_mib[i];
396 reg = AN8855_PORT_MIB_COUNTER(port) + mib->offset;
397
398 data[i] = an8855_read(priv, reg);
399 if (mib->size == 2) {
400 hi = an8855_read(priv, reg + 4);
401 data[i] |= hi << 32;
402 }
403 }
404}
405
406static int
407an8855_get_sset_count(struct dsa_switch *ds, int port, int sset)
408{
409 if (sset != ETH_SS_STATS)
410 return 0;
411
412 return ARRAY_SIZE(an8855_mib);
413}
414
415static int
416an8855_cpu_port_enable(struct dsa_switch *ds, int port)
417{
418 struct an8855_priv *priv = ds->priv;
419
420 /* Setup max capability of CPU port at first */
421 if (priv->info->cpu_port_config)
422 priv->info->cpu_port_config(ds, port);
423
424 /* Enable Airoha header mode on the cpu port */
425 an8855_write(priv, AN8855_PVC_P(port),
426 PORT_SPEC_REPLACE_MODE | PORT_SPEC_TAG);
427
428 /* Unknown multicast frame forwarding to the cpu port */
429 an8855_write(priv, AN8855_UNMF, BIT(port));
430
431 /* Set CPU port number */
432 an8855_rmw(priv, AN8855_MFC, CPU_MASK, CPU_EN | CPU_PORT(port));
433
434 /* CPU port gets connected to all user ports of
435 * the switch.
436 */
437 an8855_write(priv, AN8855_PORTMATRIX_P(port),
438 PORTMATRIX_MATRIX(dsa_user_ports(priv->ds)));
439
440 return 0;
441}
442
443static int
444an8855_port_enable(struct dsa_switch *ds, int port, struct phy_device *phy)
445{
446 struct an8855_priv *priv = ds->priv;
447
448 if (!dsa_is_user_port(ds, port))
449 return 0;
450
451 mutex_lock(&priv->reg_mutex);
452
453 /* Allow the user port gets connected to the cpu port and also
454 * restore the port matrix if the port is the member of a certain
455 * bridge.
456 */
457 priv->ports[port].pm |= PORTMATRIX_MATRIX(BIT(AN8855_CPU_PORT));
458 priv->ports[port].enable = true;
459 an8855_write(priv, AN8855_PORTMATRIX_P(port), priv->ports[port].pm);
460 an8855_clear(priv, AN8855_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
461
462 mutex_unlock(&priv->reg_mutex);
463
464 return 0;
465}
466
467static void
468an8855_port_disable(struct dsa_switch *ds, int port)
469{
470 struct an8855_priv *priv = ds->priv;
471
472 if (!dsa_is_user_port(ds, port))
473 return;
474
475 mutex_lock(&priv->reg_mutex);
476
477 /* Clear up all port matrix which could be restored in the next
478 * enablement for the port.
479 */
480 priv->ports[port].enable = false;
481 an8855_write(priv, AN8855_PORTMATRIX_P(port), PORTMATRIX_CLR);
482 an8855_clear(priv, AN8855_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
483
484 mutex_unlock(&priv->reg_mutex);
485}
486
487static void
488an8855_stp_state_set(struct dsa_switch *ds, int port, u8 state)
489{
490 struct an8855_priv *priv = ds->priv;
491 u32 stp_state;
492
493 if (dsa_is_unused_port(ds, port))
494 return;
495
496 switch (state) {
497 case BR_STATE_DISABLED:
498 stp_state = AN8855_STP_DISABLED;
499 break;
500 case BR_STATE_BLOCKING:
501 stp_state = AN8855_STP_BLOCKING;
502 break;
503 case BR_STATE_LISTENING:
504 stp_state = AN8855_STP_LISTENING;
505 break;
506 case BR_STATE_LEARNING:
507 stp_state = AN8855_STP_LEARNING;
508 break;
509 case BR_STATE_FORWARDING:
510 default:
511 stp_state = AN8855_STP_FORWARDING;
512 break;
513 }
514
515 an8855_rmw(priv, AN8855_SSP_P(port), FID_PST_MASK, stp_state);
516}
517
518static int
519an8855_port_bridge_join(struct dsa_switch *ds, int port,
520 struct net_device *bridge)
521{
522 struct an8855_priv *priv = ds->priv;
523 u32 port_bitmap = BIT(AN8855_CPU_PORT);
524 int i;
525
526 mutex_lock(&priv->reg_mutex);
527
528 for (i = 0; i < AN8855_NUM_PORTS; i++) {
529 /* Add this port to the port matrix of the other ports in the
530 * same bridge. If the port is disabled, port matrix is kept
531 * and not being setup until the port becomes enabled.
532 */
533 if (dsa_is_user_port(ds, i) && i != port) {
534 if (dsa_to_port(ds, i)->bridge_dev != bridge)
535 continue;
536 if (priv->ports[i].enable)
537 an8855_set(priv, AN8855_PORTMATRIX_P(i),
538 PORTMATRIX_MATRIX(BIT(port)));
539 priv->ports[i].pm |= PORTMATRIX_MATRIX(BIT(port));
540
541 port_bitmap |= BIT(i);
542 }
543 }
544
545 /* Add the all other ports to this port matrix. */
546 if (priv->ports[port].enable)
547 an8855_rmw(priv, AN8855_PORTMATRIX_P(port),
548 PORTMATRIX_MASK, PORTMATRIX_MATRIX(port_bitmap));
549 priv->ports[port].pm |= PORTMATRIX_MATRIX(port_bitmap);
550
551 mutex_unlock(&priv->reg_mutex);
552
553 return 0;
554}
555
556static void
557an8855_port_set_vlan_unaware(struct dsa_switch *ds, int port)
558{
559 struct an8855_priv *priv = ds->priv;
560 bool all_user_ports_removed = true;
561 int i;
562
563 /* When a port is removed from the bridge, the port would be set up
564 * back to the default as is at initial boot which is a VLAN-unaware
565 * port.
566 */
567 an8855_rmw(priv, AN8855_PCR_P(port), PCR_PORT_VLAN_MASK,
568 AN8855_PORT_MATRIX_MODE);
569 an8855_rmw(priv, AN8855_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
570 VLAN_ATTR(AN8855_VLAN_TRANSPARENT) |
571 PVC_EG_TAG(AN8855_VLAN_EG_CONSISTENT));
572
573 for (i = 0; i < AN8855_NUM_PORTS; i++) {
574 if (dsa_is_user_port(ds, i) &&
575 dsa_port_is_vlan_filtering(&ds->ports[i])) {
576 all_user_ports_removed = false;
577 break;
578 }
579 }
580
581 /* CPU port also does the same thing until all user ports belonging to
582 * the CPU port get out of VLAN filtering mode.
583 */
584 if (all_user_ports_removed) {
585 an8855_write(priv, AN8855_PORTMATRIX_P(AN8855_CPU_PORT),
586 PORTMATRIX_MATRIX(dsa_user_ports(priv->ds)));
587 an8855_write(priv, AN8855_PVC_P(AN8855_CPU_PORT),
588 PORT_SPEC_REPLACE_MODE | PORT_SPEC_TAG |
589 PVC_EG_TAG(AN8855_VLAN_EG_CONSISTENT));
590 }
591}
592
593static void
594an8855_port_set_vlan_aware(struct dsa_switch *ds, int port)
595{
596 struct an8855_priv *priv = ds->priv;
597
598 /* Trapped into security mode allows packet forwarding through VLAN
599 * table lookup. CPU port is set to fallback mode to let untagged
600 * frames pass through.
601 */
602 if (dsa_is_cpu_port(ds, port))
603 an8855_rmw(priv, AN8855_PCR_P(port), PCR_PORT_VLAN_MASK,
604 AN8855_PORT_FALLBACK_MODE);
605 else
606 an8855_rmw(priv, AN8855_PCR_P(port), PCR_PORT_VLAN_MASK,
607 AN8855_PORT_SECURITY_MODE);
608
609 /* Set the port as a user port which is to be able to recognize VID
610 * from incoming packets before fetching entry within the VLAN table.
611 */
612 an8855_rmw(priv, AN8855_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK,
613 VLAN_ATTR(AN8855_VLAN_USER) |
614 PVC_EG_TAG(AN8855_VLAN_EG_DISABLED));
615}
616
617static void
618an8855_port_bridge_leave(struct dsa_switch *ds, int port,
619 struct net_device *bridge)
620{
621 struct an8855_priv *priv = ds->priv;
622 int i;
623
624 mutex_lock(&priv->reg_mutex);
625
626 for (i = 0; i < AN8855_NUM_PORTS; i++) {
627 /* Remove this port from the port matrix of the other ports
628 * in the same bridge. If the port is disabled, port matrix
629 * is kept and not being setup until the port becomes enabled.
630 */
631 if (dsa_is_user_port(ds, i) && i != port) {
632 if (dsa_to_port(ds, i)->bridge_dev != bridge)
633 continue;
634 if (priv->ports[i].enable)
635 an8855_clear(priv, AN8855_PORTMATRIX_P(i),
636 PORTMATRIX_MATRIX(BIT(port)));
637 priv->ports[i].pm &= PORTMATRIX_MATRIX(BIT(port));
638 }
639 }
640
641 /* Set the cpu port to be the only one in the port matrix of
642 * this port.
643 */
644 if (priv->ports[port].enable)
645 an8855_rmw(priv, AN8855_PORTMATRIX_P(port), PORTMATRIX_MASK,
646 PORTMATRIX_MATRIX(BIT(AN8855_CPU_PORT)));
647 priv->ports[port].pm = PORTMATRIX_MATRIX(BIT(AN8855_CPU_PORT));
648
649 mutex_unlock(&priv->reg_mutex);
650}
651
652static int
653an8855_port_fdb_add(struct dsa_switch *ds, int port,
654 const unsigned char *addr, u16 vid)
655{
656 struct an8855_priv *priv = ds->priv;
657 int ret;
658 u8 port_mask = BIT(port);
659
660 mutex_lock(&priv->reg_mutex);
661 an8855_fdb_write(priv, vid, port_mask, addr, 1);
662 ret = an8855_fdb_cmd(priv, AN8855_FDB_WRITE, NULL);
663 mutex_unlock(&priv->reg_mutex);
664
665 return ret;
666}
667
668static int
669an8855_port_fdb_del(struct dsa_switch *ds, int port,
670 const unsigned char *addr, u16 vid)
671{
672 struct an8855_priv *priv = ds->priv;
673 int ret;
674 u8 port_mask = BIT(port);
675
676 mutex_lock(&priv->reg_mutex);
677 an8855_fdb_write(priv, vid, port_mask, addr, 0);
678 ret = an8855_fdb_cmd(priv, AN8855_FDB_WRITE, NULL);
679 mutex_unlock(&priv->reg_mutex);
680
681 return ret;
682}
683
684static int
685an8855_port_fdb_dump(struct dsa_switch *ds, int port,
686 dsa_fdb_dump_cb_t *cb, void *data)
687{
688 struct an8855_priv *priv = ds->priv;
689 struct an8855_fdb _fdb = { 0 };
690 int cnt = 512;
691 int num = 4;
692 int index = 0;
693 bool flag = false;
694 int banks = 0;
695 int i = 0;
696 int ret = 0;
697 u32 rsp = 0;
698
699 mutex_lock(&priv->reg_mutex);
700
701 an8855_write(priv, AN8855_ATWD2, (0x1 << port));
702 ret = an8855_fdb_cmd(priv, ATC_MAT(0xc) | AN8855_FDB_START, &rsp);
703 if (ret < 0)
704 goto err;
705
706 index = (rsp >> ATC_HASH) & ATC_HASH_MASK;
707 if (index == (cnt - 1))
708 flag = true;
709 else
710 flag = false;
711
712 banks = (rsp >> ATC_HIT) & ATC_HIT_MASK;
713 if (banks == 0) {
714 mutex_unlock(&priv->reg_mutex);
715 return 0;
716 }
717 for (i = 0; i < num; i++) {
718 if ((banks >> i) & 0x1) {
719 an8855_write(priv, AN8855_ATRDS, i);
720 udelay(1000);
721 an8855_fdb_read(priv, &_fdb);
722 if (!_fdb.live)
723 continue;
724 if (_fdb.port_mask & BIT(port)) {
725 ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp, data);
726 if (ret < 0)
727 continue;
728 }
729 }
730 }
731 while (1) {
732 if (flag == true)
733 break;
734
735 ret =
736 an8855_fdb_cmd(priv, ATC_MAT(0xc) | AN8855_FDB_NEXT, &rsp);
737 index = (rsp >> ATC_HASH) & ATC_HASH_MASK;
738 if (index == (cnt - 1))
739 flag = true;
740 else
741 flag = false;
742
743 banks = (rsp >> ATC_HIT) & ATC_HIT_MASK;
744 if (banks == 0) {
745 mutex_unlock(&priv->reg_mutex);
746 return 0;
747 }
748 for (i = 0; i < num; i++) {
749 if ((banks >> i) & 0x1) {
750 an8855_write(priv, AN8855_ATRDS, i);
751 udelay(1000);
752 an8855_fdb_read(priv, &_fdb);
753 if (!_fdb.live)
754 continue;
755 if (_fdb.port_mask & BIT(port)) {
756 ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp,
757 data);
758 if (ret < 0)
759 continue;
760 }
761 }
762 }
763 }
764
765err:
766 mutex_unlock(&priv->reg_mutex);
767 return 0;
768}
769
770static int
771an8855_vlan_cmd(struct an8855_priv *priv, enum an8855_vlan_cmd cmd, u16 vid)
772{
773 struct an8855_dummy_poll p;
774 u32 val;
775 int ret;
776
777 if (vid > 0xFFF) {
778 dev_err(priv->dev, "vid number invalid\n");
779 return -EINVAL;
780 }
781
782 val = VTCR_BUSY | VTCR_FUNC(cmd) | vid;
783 an8855_write(priv, AN8855_VTCR, val);
784
785 INIT_AN8855_DUMMY_POLL(&p, priv, AN8855_VTCR);
786 ret = readx_poll_timeout(_an8855_read, &p, val,
787 !(val & VTCR_BUSY), 20, 200000);
788 if (ret < 0) {
789 dev_err(priv->dev, "poll timeout\n");
790 return ret;
791 }
792
793 return 0;
794}
795
796static int
797an8855_port_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering)
798{
799 if (vlan_filtering) {
800 /* The port is being kept as VLAN-unaware port when bridge is
801 * set up with vlan_filtering not being set, Otherwise, the
802 * port and the corresponding CPU port is required the setup
803 * for becoming a VLAN-aware port.
804 */
805 an8855_port_set_vlan_aware(ds, port);
806 an8855_port_set_vlan_aware(ds, AN8855_CPU_PORT);
807 } else {
808 an8855_port_set_vlan_unaware(ds, port);
809 }
810
811 return 0;
812}
813
814static int
815an8855_port_vlan_prepare(struct dsa_switch *ds, int port,
816 const struct switchdev_obj_port_vlan *vlan)
817{
818 /* nothing needed */
819 return 0;
820}
821
822static void
823an8855_hw_vlan_add(struct an8855_priv *priv, struct an8855_hw_vlan_entry *entry)
824{
825 u8 new_members;
826 u32 val;
827
828 new_members = entry->old_members | BIT(entry->port) |
829 BIT(AN8855_CPU_PORT);
830
831 /* Validate the entry with independent learning, create egress tag per
832 * VLAN and joining the port as one of the port members.
833 */
834 val =
835 an8855_read(priv,
836 AN8855_VARD0) & (ETAG_CTRL_MASK << PORT_EG_CTRL_SHIFT);
837 val |= (IVL_MAC | VTAG_EN | PORT_MEM(new_members) | VLAN_VALID);
838 an8855_write(priv, AN8855_VAWD0, val);
839 an8855_write(priv, AN8855_VAWD1, 0);
840
841 /* Decide whether adding tag or not for those outgoing packets from the
842 * port inside the VLAN.
843 */
844 val =
845 entry->untagged ? AN8855_VLAN_EGRESS_UNTAG : AN8855_VLAN_EGRESS_TAG;
846 an8855_rmw(priv, AN8855_VAWD0,
847 ETAG_CTRL_P_MASK(entry->port) << PORT_EG_CTRL_SHIFT,
848 ETAG_CTRL_P(entry->port, val) << PORT_EG_CTRL_SHIFT);
849
850 /* CPU port is always taken as a tagged port for serving more than one
851 * VLANs across and also being applied with egress type stack mode for
852 * that VLAN tags would be appended after hardware special tag used as
853 * DSA tag.
854 */
855 an8855_rmw(priv, AN8855_VAWD0,
856 ETAG_CTRL_P_MASK(AN8855_CPU_PORT) << PORT_EG_CTRL_SHIFT,
857 ETAG_CTRL_P(AN8855_CPU_PORT,
858 AN8855_VLAN_EGRESS_STACK) << PORT_EG_CTRL_SHIFT);
859}
860
861static void
862an8855_hw_vlan_del(struct an8855_priv *priv, struct an8855_hw_vlan_entry *entry)
863{
864 u8 new_members;
865 u32 val;
866
867 new_members = entry->old_members & ~BIT(entry->port);
868
869 val = an8855_read(priv, AN8855_VARD0);
870 if (!(val & VLAN_VALID)) {
871 dev_err(priv->dev, "Cannot be deleted due to invalid entry\n");
872 return;
873 }
874
875 /* If certain member apart from CPU port is still alive in the VLAN,
876 * the entry would be kept valid. Otherwise, the entry is got to be
877 * disabled.
878 */
879 if (new_members && new_members != BIT(AN8855_CPU_PORT)) {
880 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | VLAN_VALID;
881 an8855_write(priv, AN8855_VAWD0, val);
882 } else {
883 an8855_write(priv, AN8855_VAWD0, 0);
884 an8855_write(priv, AN8855_VAWD1, 0);
885 }
886}
887
888static void
889an8855_hw_vlan_update(struct an8855_priv *priv, u16 vid,
890 struct an8855_hw_vlan_entry *entry,
891 an8855_vlan_op vlan_op)
892{
893 u32 val;
894
895 /* Fetch entry */
896 an8855_vlan_cmd(priv, AN8855_VTCR_RD_VID, vid);
897
898 val = an8855_read(priv, AN8855_VARD0);
899
900 entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK;
901
902 /* Manipulate entry */
903 vlan_op(priv, entry);
904
905 /* Flush result to hardware */
906 an8855_vlan_cmd(priv, AN8855_VTCR_WR_VID, vid);
907}
908
909static void
910an8855_port_vlan_add(struct dsa_switch *ds, int port,
911 const struct switchdev_obj_port_vlan *vlan)
912{
913 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
914 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
915 struct an8855_hw_vlan_entry new_entry;
916 struct an8855_priv *priv = ds->priv;
917 u16 vid;
918
919 mutex_lock(&priv->reg_mutex);
920
921 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
922 an8855_hw_vlan_entry_init(&new_entry, port, untagged);
923 an8855_hw_vlan_update(priv, vid, &new_entry,
924 an8855_hw_vlan_add);
925 }
926
927 if (pvid) {
928 an8855_rmw(priv, AN8855_PVID_P(port), G0_PORT_VID_MASK,
929 G0_PORT_VID(vlan->vid_end));
930 priv->ports[port].pvid = vlan->vid_end;
931 }
932
933 mutex_unlock(&priv->reg_mutex);
934}
935
936static int
937an8855_port_vlan_del(struct dsa_switch *ds, int port,
938 const struct switchdev_obj_port_vlan *vlan)
939{
940 struct an8855_hw_vlan_entry target_entry;
941 struct an8855_priv *priv = ds->priv;
942 u16 vid, pvid;
943
944 mutex_lock(&priv->reg_mutex);
945
946 pvid = priv->ports[port].pvid;
947 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
948 an8855_hw_vlan_entry_init(&target_entry, port, 0);
949 an8855_hw_vlan_update(priv, vid, &target_entry,
950 an8855_hw_vlan_del);
951
952 /* PVID is being restored to the default whenever the PVID port
953 * is being removed from the VLAN.
954 */
955 if (pvid == vid)
956 pvid = G0_PORT_VID_DEF;
957 }
958
959 an8855_rmw(priv, AN8855_PVID_P(port), G0_PORT_VID_MASK, pvid);
960 priv->ports[port].pvid = pvid;
961
962 mutex_unlock(&priv->reg_mutex);
963
964 return 0;
965}
966
967static int an8855_port_mirror_add(struct dsa_switch *ds, int port,
968 struct dsa_mall_mirror_tc_entry *mirror,
969 bool ingress)
970{
971 struct an8855_priv *priv = ds->priv;
972 int monitor_port;
973 u32 val;
974
975 /* Check for existent entry */
976 if ((ingress ? priv->mirror_rx : priv->mirror_tx) & BIT(port))
977 return -EEXIST;
978
979 val = an8855_read(priv, AN8855_MIR);
980
981 /* AN8855 supports 4 monitor port, but only use first group */
982 monitor_port = AN8855_MIRROR_PORT_GET(val);
983 if (val & AN8855_MIRROR_EN && monitor_port != mirror->to_local_port)
984 return -EEXIST;
985
986 val |= AN8855_MIRROR_EN;
987 val &= ~AN8855_MIRROR_MASK;
988 val |= AN8855_MIRROR_PORT_SET(mirror->to_local_port);
989 an8855_write(priv, AN8855_MIR, val);
990
991 val = an8855_read(priv, AN8855_PCR_P(port));
992 if (ingress) {
993 val |= PORT_RX_MIR;
994 priv->mirror_rx |= BIT(port);
995 } else {
996 val |= PORT_TX_MIR;
997 priv->mirror_tx |= BIT(port);
998 }
999 an8855_write(priv, AN8855_PCR_P(port), val);
1000
1001 return 0;
1002}
1003
1004static void an8855_port_mirror_del(struct dsa_switch *ds, int port,
1005 struct dsa_mall_mirror_tc_entry *mirror)
1006{
1007 struct an8855_priv *priv = ds->priv;
1008 u32 val;
1009
1010 val = an8855_read(priv, AN8855_PCR_P(port));
1011 if (mirror->ingress) {
1012 val &= ~PORT_RX_MIR;
1013 priv->mirror_rx &= ~BIT(port);
1014 } else {
1015 val &= ~PORT_TX_MIR;
1016 priv->mirror_tx &= ~BIT(port);
1017 }
1018 an8855_write(priv, AN8855_PCR_P(port), val);
1019
1020 if (!priv->mirror_rx && !priv->mirror_tx) {
1021 val = an8855_read(priv, AN8855_MIR);
1022 val &= ~AN8855_MIRROR_EN;
1023 an8855_write(priv, AN8855_MIR, val);
1024 }
1025}
1026
1027static enum dsa_tag_protocol
1028air_get_tag_protocol(struct dsa_switch *ds, int port, enum dsa_tag_protocol mp)
1029{
1030 struct an8855_priv *priv = ds->priv;
1031
1032 if (port != AN8855_CPU_PORT) {
1033 dev_warn(priv->dev, "port not matched with tagging CPU port\n");
1034 return DSA_TAG_PROTO_NONE;
1035 } else {
1036 return DSA_TAG_PROTO_ARHT;
1037 }
1038}
1039
1040static int
1041setup_unused_ports(struct dsa_switch *ds, u32 pm)
1042{
1043 struct an8855_priv *priv = ds->priv;
1044 u32 egtag_mask = 0;
1045 u32 egtag_val = 0;
1046 int i;
1047
1048 if (!pm)
1049 return 0;
1050
1051 for (i = 0; i < AN8855_NUM_PORTS; i++) {
1052 if (!dsa_is_unused_port(ds, i))
1053 continue;
1054
1055 /* Setup MAC port with maximum capability. */
1056 if (i == 5)
1057 if (priv->info->cpu_port_config)
1058 priv->info->cpu_port_config(ds, i);
1059
1060 an8855_rmw(priv, AN8855_PORTMATRIX_P(i), PORTMATRIX_MASK,
1061 AN8855_PORTMATRIX_P(pm));
1062 an8855_rmw(priv, AN8855_PCR_P(i), PCR_PORT_VLAN_MASK,
1063 AN8855_PORT_SECURITY_MODE);
1064 egtag_mask |= ETAG_CTRL_P_MASK(i);
1065 egtag_val |= ETAG_CTRL_P(i, AN8855_VLAN_EGRESS_UNTAG);
1066 }
1067
1068 /* Add unused ports to VLAN2 group for using IVL fdb. */
1069 an8855_write(priv, AN8855_VAWD0,
1070 IVL_MAC | VTAG_EN | PORT_MEM(pm) | VLAN_VALID);
1071 an8855_rmw(priv, AN8855_VAWD0, egtag_mask << PORT_EG_CTRL_SHIFT,
1072 egtag_val << PORT_EG_CTRL_SHIFT);
1073 an8855_write(priv, AN8855_VAWD1, 0);
1074 an8855_vlan_cmd(priv, AN8855_VTCR_WR_VID, AN8855_RESERVED_VLAN);
1075
1076 for (i = 0; i < AN8855_NUM_PORTS; i++) {
1077 if (!dsa_is_unused_port(ds, i))
1078 continue;
1079
1080 an8855_rmw(priv, AN8855_PVID_P(i), G0_PORT_VID_MASK,
1081 G0_PORT_VID(AN8855_RESERVED_VLAN));
1082 an8855_rmw(priv, AN8855_SSP_P(i), FID_PST_MASK,
1083 AN8855_STP_FORWARDING);
1084
1085 dev_dbg(ds->dev, "Add unused port%d to reserved VLAN%d group\n",
1086 i, AN8855_RESERVED_VLAN);
1087 }
1088
1089 return 0;
1090}
1091
1092static int
1093an8855_setup(struct dsa_switch *ds)
1094{
1095 struct an8855_priv *priv = ds->priv;
1096 struct an8855_dummy_poll p;
1097 u32 unused_pm = 0;
1098 u32 val, id;
1099 int ret, i;
1100
1101 /* Reset whole chip through gpio pin or memory-mapped registers for
1102 * different type of hardware
1103 */
1104 gpiod_set_value_cansleep(priv->reset, 0);
1105 usleep_range(100000, 150000);
1106 gpiod_set_value_cansleep(priv->reset, 1);
1107 usleep_range(100000, 150000);
1108
1109 /* Waiting for AN8855 got to stable */
1110 INIT_AN8855_DUMMY_POLL(&p, priv, 0x1000009c);
1111 ret = readx_poll_timeout(_an8855_read, &p, val, val != 0, 20, 1000000);
1112 if (ret < 0) {
1113 dev_err(priv->dev, "reset timeout\n");
1114 return ret;
1115 }
1116
1117 id = an8855_read(priv, AN8855_CREV);
1118 if (id != AN8855_ID) {
1119 dev_err(priv->dev, "chip %x can't be supported\n", id);
1120 return -ENODEV;
1121 }
1122
1123 /* Reset the switch through internal reset */
1124 an8855_write(priv, AN8855_RST_CTRL, SYS_CTRL_SYS_RST);
1125 usleep_range(100000, 110000);
1126
1127 /* change gphy smi address */
1128 if (priv->phy_base_new > 0) {
1129 an8855_write(priv, RG_GPHY_SMI_ADDR, priv->phy_base_new);
1130 priv->phy_base = priv->phy_base_new;
1131 }
1132
1133 /* Let phylink decide the interface later. */
1134 priv->p5_interface = PHY_INTERFACE_MODE_NA;
1135
1136 /* BPDU to CPU port */
1137 //an8855_rmw(priv, AN8855_CFC, AN8855_CPU_PMAP_MASK,
1138 // BIT(AN8855_CPU_PORT));
1139 an8855_rmw(priv, AN8855_BPC, AN8855_BPDU_PORT_FW_MASK,
1140 AN8855_BPDU_CPU_ONLY);
1141
1142 /* Enable and reset MIB counters */
1143 an8855_mib_reset(ds);
1144
1145 for (i = 0; i < AN8855_NUM_PORTS; i++) {
1146 /* Disable forwarding by default on all ports */
1147 an8855_rmw(priv, AN8855_PORTMATRIX_P(i), PORTMATRIX_MASK,
1148 PORTMATRIX_CLR);
1149 if (dsa_is_unused_port(ds, i))
1150 unused_pm |= BIT(i);
1151 else if (dsa_is_cpu_port(ds, i))
1152 an8855_cpu_port_enable(ds, i);
1153 else
1154 an8855_port_disable(ds, i);
1155 /* Enable consistent egress tag */
1156 an8855_rmw(priv, AN8855_PVC_P(i), PVC_EG_TAG_MASK,
1157 PVC_EG_TAG(AN8855_VLAN_EG_CONSISTENT));
1158 }
1159
1160 an8855_phy_setup(ds);
1161
1162 /* Group and enable unused ports as a standalone dumb switch. */
1163 setup_unused_ports(ds, unused_pm);
1164
1165 ds->configure_vlan_while_not_filtering = true;
1166
1167 /* Flush the FDB table */
1168 ret = an8855_fdb_cmd(priv, AN8855_FDB_FLUSH, NULL);
1169 if (ret < 0)
1170 return ret;
1171
1172 return 0;
1173}
1174
1175static bool
1176an8855_phy_supported(struct dsa_switch *ds, int port,
1177 const struct phylink_link_state *state)
1178{
1179 struct an8855_priv *priv = ds->priv;
1180
1181 switch (port) {
1182 case 0: /* Internal phy */
1183 case 1:
1184 case 2:
1185 case 3:
1186 case 4:
1187 if (state->interface != PHY_INTERFACE_MODE_GMII)
1188 goto unsupported;
1189 break;
1190 case 5:
1191 if (state->interface != PHY_INTERFACE_MODE_SGMII
1192 && state->interface != PHY_INTERFACE_MODE_RGMII
1193 && state->interface != PHY_INTERFACE_MODE_2500BASEX)
1194 goto unsupported;
1195 break;
1196 default:
1197 dev_err(priv->dev, "%s: unsupported port: %i\n", __func__,
1198 port);
1199 goto unsupported;
1200 }
1201
1202 return true;
1203
1204unsupported:
1205 return false;
1206}
1207
1208static bool
1209an8855_phy_mode_supported(struct dsa_switch *ds, int port,
1210 const struct phylink_link_state *state)
1211{
1212 struct an8855_priv *priv = ds->priv;
1213
1214 return priv->info->phy_mode_supported(ds, port, state);
1215}
1216
1217static int
1218an8855_rgmii_setup(struct an8855_priv *priv, u32 port,
1219 phy_interface_t interface,
1220 struct phy_device *phydev)
1221{
1222 return 0;
1223}
1224
1225static void
1226an8855_sgmii_validate(struct an8855_priv *priv, int port,
1227 unsigned long *supported)
1228{
1229 switch (port) {
1230 case 5:
1231 phylink_set(supported, 1000baseX_Full);
1232 phylink_set(supported, 2500baseX_Full);
1233 }
1234}
1235
1236static void
1237an8855_sgmii_link_up_force(struct dsa_switch *ds, int port,
1238 unsigned int mode, phy_interface_t interface,
1239 int speed, int duplex)
1240{
1241 /* For adjusting speed and duplex of SGMII force mode. */
1242 if (interface != PHY_INTERFACE_MODE_SGMII ||
1243 phylink_autoneg_inband(mode))
1244 return;
1245}
1246
1247static bool
1248an8855_is_mac_port(u32 port)
1249{
1250 return (port == 5);
1251}
1252
1253static int
1254an8855_set_hsgmii_mode(struct an8855_priv *priv)
1255{
1256 u32 val = 0;
1257
1258 /* PLL */
1259 val = an8855_read(priv, QP_DIG_MODE_CTRL_1);
1260 val &= ~(0x3 << 2);
1261 val |= (0x1 << 2);
1262 an8855_write(priv, QP_DIG_MODE_CTRL_1, val);
1263
1264 /* PLL - LPF */
1265 val = an8855_read(priv, PLL_CTRL_2);
1266 val &= ~(0x3 << 0);
1267 val |= (0x1 << 0);
1268 val &= ~(0x7 << 2);
1269 val |= (0x5 << 2);
1270 val &= ~BITS(6, 7);
1271 val &= ~(0x7 << 8);
1272 val |= (0x3 << 8);
1273 val |= BIT(29);
1274 val &= ~BITS(12, 13);
1275 an8855_write(priv, PLL_CTRL_2, val);
1276
1277 /* PLL - ICO */
1278 val = an8855_read(priv, PLL_CTRL_4);
1279 val |= BIT(2);
1280 an8855_write(priv, PLL_CTRL_4, val);
1281
1282 val = an8855_read(priv, PLL_CTRL_2);
1283 val &= ~BIT(14);
1284 an8855_write(priv, PLL_CTRL_2, val);
1285
1286 /* PLL - CHP */
1287 val = an8855_read(priv, PLL_CTRL_2);
1288 val &= ~(0xf << 16);
1289 val |= (0x6 << 16);
1290 an8855_write(priv, PLL_CTRL_2, val);
1291
1292 /* PLL - PFD */
1293 val = an8855_read(priv, PLL_CTRL_2);
1294 val &= ~(0x3 << 20);
1295 val |= (0x1 << 20);
1296 val &= ~(0x3 << 24);
1297 val |= (0x1 << 24);
1298 val &= ~BIT(26);
1299 an8855_write(priv, PLL_CTRL_2, val);
1300
1301 /* PLL - POSTDIV */
1302 val = an8855_read(priv, PLL_CTRL_2);
1303 val |= BIT(22);
1304 val &= ~BIT(27);
1305 val &= ~BIT(28);
1306 an8855_write(priv, PLL_CTRL_2, val);
1307
1308 /* PLL - SDM */
1309 val = an8855_read(priv, PLL_CTRL_4);
1310 val &= ~BITS(3, 4);
1311 an8855_write(priv, PLL_CTRL_4, val);
1312
1313 val = an8855_read(priv, PLL_CTRL_2);
1314 val &= ~BIT(30);
1315 an8855_write(priv, PLL_CTRL_2, val);
1316
1317 val = an8855_read(priv, SS_LCPLL_PWCTL_SETTING_2);
1318 val &= ~(0x3 << 16);
1319 val |= (0x1 << 16);
1320 an8855_write(priv, SS_LCPLL_PWCTL_SETTING_2, val);
1321
1322 an8855_write(priv, SS_LCPLL_TDC_FLT_2, 0x7a000000);
1323 an8855_write(priv, SS_LCPLL_TDC_PCW_1, 0x7a000000);
1324
1325 val = an8855_read(priv, SS_LCPLL_TDC_FLT_5);
1326 val &= ~BIT(24);
1327 an8855_write(priv, SS_LCPLL_TDC_FLT_5, val);
1328
1329 val = an8855_read(priv, PLL_CK_CTRL_0);
1330 val &= ~BIT(8);
1331 an8855_write(priv, PLL_CK_CTRL_0, val);
1332
1333 /* PLL - SS */
1334 val = an8855_read(priv, PLL_CTRL_3);
1335 val &= ~BITS(0, 15);
1336 an8855_write(priv, PLL_CTRL_3, val);
1337
1338 val = an8855_read(priv, PLL_CTRL_4);
1339 val &= ~BITS(0, 1);
1340 an8855_write(priv, PLL_CTRL_4, val);
1341
1342 val = an8855_read(priv, PLL_CTRL_3);
1343 val &= ~BITS(16, 31);
1344 an8855_write(priv, PLL_CTRL_3, val);
1345
1346 /* PLL - TDC */
1347 val = an8855_read(priv, PLL_CK_CTRL_0);
1348 val &= ~BIT(9);
1349 an8855_write(priv, PLL_CK_CTRL_0, val);
1350
1351 val = an8855_read(priv, RG_QP_PLL_SDM_ORD);
1352 val |= BIT(3);
1353 val |= BIT(4);
1354 an8855_write(priv, RG_QP_PLL_SDM_ORD, val);
1355
1356 val = an8855_read(priv, RG_QP_RX_DAC_EN);
1357 val &= ~(0x3 << 16);
1358 val |= (0x2 << 16);
1359 an8855_write(priv, RG_QP_RX_DAC_EN, val);
1360
1361 /* TCL Disable (only for Co-SIM) */
1362 val = an8855_read(priv, PON_RXFEDIG_CTRL_0);
1363 val &= ~BIT(12);
1364 an8855_write(priv, PON_RXFEDIG_CTRL_0, val);
1365
1366 /* TX Init */
1367 val = an8855_read(priv, RG_QP_TX_MODE_16B_EN);
1368 val &= ~BIT(0);
1369 val &= ~(0xffff << 16);
1370 val |= (0x4 << 16);
1371 an8855_write(priv, RG_QP_TX_MODE_16B_EN, val);
1372
1373 /* RX Control */
1374 val = an8855_read(priv, RG_QP_RXAFE_RESERVE);
1375 val |= BIT(11);
1376 an8855_write(priv, RG_QP_RXAFE_RESERVE, val);
1377
1378 val = an8855_read(priv, RG_QP_CDR_LPF_MJV_LIM);
1379 val &= ~(0x3 << 4);
1380 val |= (0x1 << 4);
1381 an8855_write(priv, RG_QP_CDR_LPF_MJV_LIM, val);
1382
1383 val = an8855_read(priv, RG_QP_CDR_LPF_SETVALUE);
1384 val &= ~(0xf << 25);
1385 val |= (0x1 << 25);
1386 val &= ~(0x7 << 29);
1387 val |= (0x3 << 29);
1388 an8855_write(priv, RG_QP_CDR_LPF_SETVALUE, val);
1389
1390 val = an8855_read(priv, RG_QP_CDR_PR_CKREF_DIV1);
1391 val &= ~(0x1f << 8);
1392 val |= (0xf << 8);
1393 an8855_write(priv, RG_QP_CDR_PR_CKREF_DIV1, val);
1394
1395 val = an8855_read(priv, RG_QP_CDR_PR_KBAND_DIV_PCIE);
1396 val &= ~(0x3f << 0);
1397 val |= (0x19 << 0);
1398 val &= ~BIT(6);
1399 an8855_write(priv, RG_QP_CDR_PR_KBAND_DIV_PCIE, val);
1400
1401 val = an8855_read(priv, RG_QP_CDR_FORCE_IBANDLPF_R_OFF);
1402 val &= ~(0x7f << 6);
1403 val |= (0x21 << 6);
1404 val &= ~(0x3 << 16);
1405 val |= (0x2 << 16);
1406 val &= ~BIT(13);
1407 an8855_write(priv, RG_QP_CDR_FORCE_IBANDLPF_R_OFF, val);
1408
1409 val = an8855_read(priv, RG_QP_CDR_PR_KBAND_DIV_PCIE);
1410 val &= ~BIT(30);
1411 an8855_write(priv, RG_QP_CDR_PR_KBAND_DIV_PCIE, val);
1412
1413 val = an8855_read(priv, RG_QP_CDR_PR_CKREF_DIV1);
1414 val &= ~(0x7 << 24);
1415 val |= (0x4 << 24);
1416 an8855_write(priv, RG_QP_CDR_PR_CKREF_DIV1, val);
1417
1418 val = an8855_read(priv, PLL_CTRL_0);
1419 val |= BIT(0);
1420 an8855_write(priv, PLL_CTRL_0, val);
1421
1422 val = an8855_read(priv, RX_CTRL_26);
1423 val &= ~BIT(23);
1424 val |= BIT(26);
1425 an8855_write(priv, RX_CTRL_26, val);
1426
1427 val = an8855_read(priv, RX_DLY_0);
1428 val &= ~(0xff << 0);
1429 val |= (0x6f << 0);
1430 val |= BITS(8, 13);
1431 an8855_write(priv, RX_DLY_0, val);
1432
1433 val = an8855_read(priv, RX_CTRL_42);
1434 val &= ~(0x1fff << 0);
1435 val |= (0x150 << 0);
1436 an8855_write(priv, RX_CTRL_42, val);
1437
1438 val = an8855_read(priv, RX_CTRL_2);
1439 val &= ~(0x1fff << 16);
1440 val |= (0x150 << 16);
1441 an8855_write(priv, RX_CTRL_2, val);
1442
1443 val = an8855_read(priv, PON_RXFEDIG_CTRL_9);
1444 val &= ~(0x7 << 0);
1445 val |= (0x1 << 0);
1446 an8855_write(priv, PON_RXFEDIG_CTRL_9, val);
1447
1448 val = an8855_read(priv, RX_CTRL_8);
1449 val &= ~(0xfff << 16);
1450 val |= (0x200 << 16);
1451 val &= ~(0x7fff << 14);
1452 val |= (0xfff << 14);
1453 an8855_write(priv, RX_CTRL_8, val);
1454
1455 /* Frequency memter */
1456 val = an8855_read(priv, RX_CTRL_5);
1457 val &= ~(0xfffff << 10);
1458 val |= (0x10 << 10);
1459 an8855_write(priv, RX_CTRL_5, val);
1460
1461 val = an8855_read(priv, RX_CTRL_6);
1462 val &= ~(0xfffff << 0);
1463 val |= (0x64 << 0);
1464 an8855_write(priv, RX_CTRL_6, val);
1465
1466 val = an8855_read(priv, RX_CTRL_7);
1467 val &= ~(0xfffff << 0);
1468 val |= (0x2710 << 0);
1469 an8855_write(priv, RX_CTRL_7, val);
1470
1471 /* PCS Init */
1472 val = an8855_read(priv, RG_HSGMII_PCS_CTROL_1);
1473 val &= ~BIT(30);
1474 an8855_write(priv, RG_HSGMII_PCS_CTROL_1, val);
1475
1476 /* Rate Adaption */
1477 val = an8855_read(priv, RATE_ADP_P0_CTRL_0);
1478 val &= ~BIT(31);
1479 an8855_write(priv, RATE_ADP_P0_CTRL_0, val);
1480
1481 val = an8855_read(priv, RG_RATE_ADAPT_CTRL_0);
1482 val |= BIT(0);
1483 val |= BIT(4);
1484 val |= BITS(26, 27);
1485 an8855_write(priv, RG_RATE_ADAPT_CTRL_0, val);
1486
1487 /* Disable AN */
1488 val = an8855_read(priv, SGMII_REG_AN0);
1489 val &= ~BIT(12);
1490 an8855_write(priv, SGMII_REG_AN0, val);
1491
1492 /* Force Speed */
1493 val = an8855_read(priv, SGMII_STS_CTRL_0);
1494 val |= BIT(2);
1495 val |= BITS(4, 5);
1496 an8855_write(priv, SGMII_STS_CTRL_0, val);
1497
1498 /* bypass flow control to MAC */
1499 an8855_write(priv, MSG_RX_LIK_STS_0, 0x01010107);
1500 an8855_write(priv, MSG_RX_LIK_STS_2, 0x00000EEF);
1501
1502 return 0;
1503}
1504
1505static int
1506an8855_sgmii_setup(struct an8855_priv *priv, int mode)
1507{
1508 u32 val = 0;
1509
1510 /* PMA Init */
1511 /* PLL */
1512 val = an8855_read(priv, QP_DIG_MODE_CTRL_1);
1513 val &= ~BITS(2, 3);
1514 an8855_write(priv, QP_DIG_MODE_CTRL_1, val);
1515
1516 /* PLL - LPF */
1517 val = an8855_read(priv, PLL_CTRL_2);
1518 val &= ~(0x3 << 0);
1519 val |= (0x1 << 0);
1520 val &= ~(0x7 << 2);
1521 val |= (0x5 << 2);
1522 val &= ~BITS(6, 7);
1523 val &= ~(0x7 << 8);
1524 val |= (0x3 << 8);
1525 val |= BIT(29);
1526 val &= ~BITS(12, 13);
1527 an8855_write(priv, PLL_CTRL_2, val);
1528
1529 /* PLL - ICO */
1530 val = an8855_read(priv, PLL_CTRL_4);
1531 val |= BIT(2);
1532 an8855_write(priv, PLL_CTRL_4, val);
1533
1534 val = an8855_read(priv, PLL_CTRL_2);
1535 val &= ~BIT(14);
1536 an8855_write(priv, PLL_CTRL_2, val);
1537
1538 /* PLL - CHP */
1539 val = an8855_read(priv, PLL_CTRL_2);
1540 val &= ~(0xf << 16);
1541 val |= (0x4 << 16);
1542 an8855_write(priv, PLL_CTRL_2, val);
1543
1544 /* PLL - PFD */
1545 val = an8855_read(priv, PLL_CTRL_2);
1546 val &= ~(0x3 << 20);
1547 val |= (0x1 << 20);
1548 val &= ~(0x3 << 24);
1549 val |= (0x1 << 24);
1550 val &= ~BIT(26);
1551 an8855_write(priv, PLL_CTRL_2, val);
1552
1553 /* PLL - POSTDIV */
1554 val = an8855_read(priv, PLL_CTRL_2);
1555 val |= BIT(22);
1556 val &= ~BIT(27);
1557 val &= ~BIT(28);
1558 an8855_write(priv, PLL_CTRL_2, val);
1559
1560 /* PLL - SDM */
1561 val = an8855_read(priv, PLL_CTRL_4);
1562 val &= ~BITS(3, 4);
1563 an8855_write(priv, PLL_CTRL_4, val);
1564
1565 val = an8855_read(priv, PLL_CTRL_2);
1566 val &= ~BIT(30);
1567 an8855_write(priv, PLL_CTRL_2, val);
1568
1569 val = an8855_read(priv, SS_LCPLL_PWCTL_SETTING_2);
1570 val &= ~(0x3 << 16);
1571 val |= (0x1 << 16);
1572 an8855_write(priv, SS_LCPLL_PWCTL_SETTING_2, val);
1573
1574 an8855_write(priv, SS_LCPLL_TDC_FLT_2, 0x48000000);
1575 an8855_write(priv, SS_LCPLL_TDC_PCW_1, 0x48000000);
1576
1577 val = an8855_read(priv, SS_LCPLL_TDC_FLT_5);
1578 val &= ~BIT(24);
1579 an8855_write(priv, SS_LCPLL_TDC_FLT_5, val);
1580
1581 val = an8855_read(priv, PLL_CK_CTRL_0);
1582 val &= ~BIT(8);
1583 an8855_write(priv, PLL_CK_CTRL_0, val);
1584
1585 /* PLL - SS */
1586 val = an8855_read(priv, PLL_CTRL_3);
1587 val &= ~BITS(0, 15);
1588 an8855_write(priv, PLL_CTRL_3, val);
1589
1590 val = an8855_read(priv, PLL_CTRL_4);
1591 val &= ~BITS(0, 1);
1592 an8855_write(priv, PLL_CTRL_4, val);
1593
1594 val = an8855_read(priv, PLL_CTRL_3);
1595 val &= ~BITS(16, 31);
1596 an8855_write(priv, PLL_CTRL_3, val);
1597
1598 /* PLL - TDC */
1599 val = an8855_read(priv, PLL_CK_CTRL_0);
1600 val &= ~BIT(9);
1601 an8855_write(priv, PLL_CK_CTRL_0, val);
1602
1603 val = an8855_read(priv, RG_QP_PLL_SDM_ORD);
1604 val |= BIT(3);
1605 val |= BIT(4);
1606 an8855_write(priv, RG_QP_PLL_SDM_ORD, val);
1607
1608 val = an8855_read(priv, RG_QP_RX_DAC_EN);
1609 val &= ~(0x3 << 16);
1610 val |= (0x2 << 16);
1611 an8855_write(priv, RG_QP_RX_DAC_EN, val);
1612
1613 /* PLL - TCL Disable (only for Co-SIM) */
1614 val = an8855_read(priv, PON_RXFEDIG_CTRL_0);
1615 val &= ~BIT(12);
1616 an8855_write(priv, PON_RXFEDIG_CTRL_0, val);
1617
1618 /* TX Init */
1619 val = an8855_read(priv, RG_QP_TX_MODE_16B_EN);
1620 val &= ~BIT(0);
1621 val &= ~BITS(16, 31);
1622 an8855_write(priv, RG_QP_TX_MODE_16B_EN, val);
1623
1624 /* RX Init */
1625 val = an8855_read(priv, RG_QP_RXAFE_RESERVE);
1626 val |= BIT(11);
1627 an8855_write(priv, RG_QP_RXAFE_RESERVE, val);
1628
1629 val = an8855_read(priv, RG_QP_CDR_LPF_MJV_LIM);
1630 val &= ~(0x3 << 4);
1631 val |= (0x2 << 4);
1632 an8855_write(priv, RG_QP_CDR_LPF_MJV_LIM, val);
1633
1634 val = an8855_read(priv, RG_QP_CDR_LPF_SETVALUE);
1635 val &= ~(0xf << 25);
1636 val |= (0x1 << 25);
1637 val &= ~(0x7 << 29);
1638 val |= (0x6 << 29);
1639 an8855_write(priv, RG_QP_CDR_LPF_SETVALUE, val);
1640
1641 val = an8855_read(priv, RG_QP_CDR_PR_CKREF_DIV1);
1642 val &= ~(0x1f << 8);
1643 val |= (0xc << 8);
1644 an8855_write(priv, RG_QP_CDR_PR_CKREF_DIV1, val);
1645
1646 val = an8855_read(priv, RG_QP_CDR_PR_KBAND_DIV_PCIE);
1647 val &= ~(0x3f << 0);
1648 val |= (0x19 << 0);
1649 val &= ~BIT(6);
1650 an8855_write(priv, RG_QP_CDR_PR_KBAND_DIV_PCIE, val);
1651
1652 val = an8855_read(priv, RG_QP_CDR_FORCE_IBANDLPF_R_OFF);
1653 val &= ~(0x7f << 6);
1654 val |= (0x21 << 6);
1655 val &= ~(0x3 << 16);
1656 val |= (0x2 << 16);
1657 val &= ~BIT(13);
1658 an8855_write(priv, RG_QP_CDR_FORCE_IBANDLPF_R_OFF, val);
1659
1660 val = an8855_read(priv, RG_QP_CDR_PR_KBAND_DIV_PCIE);
1661 val &= ~BIT(30);
1662 an8855_write(priv, RG_QP_CDR_PR_KBAND_DIV_PCIE, val);
1663
1664 val = an8855_read(priv, RG_QP_CDR_PR_CKREF_DIV1);
1665 val &= ~(0x7 << 24);
1666 val |= (0x4 << 24);
1667 an8855_write(priv, RG_QP_CDR_PR_CKREF_DIV1, val);
1668
1669 val = an8855_read(priv, PLL_CTRL_0);
1670 val |= BIT(0);
1671 an8855_write(priv, PLL_CTRL_0, val);
1672
1673 val = an8855_read(priv, RX_CTRL_26);
1674 val &= ~BIT(23);
1675 if (mode == SGMII_MODE_AN)
1676 val |= BIT(26);
1677
1678 an8855_write(priv, RX_CTRL_26, val);
1679
1680 val = an8855_read(priv, RX_DLY_0);
1681 val &= ~(0xff << 0);
1682 val |= (0x6f << 0);
1683 val |= BITS(8, 13);
1684 an8855_write(priv, RX_DLY_0, val);
1685
1686 val = an8855_read(priv, RX_CTRL_42);
1687 val &= ~(0x1fff << 0);
1688 val |= (0x150 << 0);
1689 an8855_write(priv, RX_CTRL_42, val);
1690
1691 val = an8855_read(priv, RX_CTRL_2);
1692 val &= ~(0x1fff << 16);
1693 val |= (0x150 << 16);
1694 an8855_write(priv, RX_CTRL_2, val);
1695
1696 val = an8855_read(priv, PON_RXFEDIG_CTRL_9);
1697 val &= ~(0x7 << 0);
1698 val |= (0x1 << 0);
1699 an8855_write(priv, PON_RXFEDIG_CTRL_9, val);
1700
1701 val = an8855_read(priv, RX_CTRL_8);
1702 val &= ~(0xfff << 16);
1703 val |= (0x200 << 16);
1704 val &= ~(0x7fff << 0);
1705 val |= (0xfff << 0);
1706 an8855_write(priv, RX_CTRL_8, val);
1707
1708 /* Frequency memter */
1709 val = an8855_read(priv, RX_CTRL_5);
1710 val &= ~(0xfffff << 10);
1711 val |= (0x28 << 10);
1712 an8855_write(priv, RX_CTRL_5, val);
1713
1714 val = an8855_read(priv, RX_CTRL_6);
1715 val &= ~(0xfffff << 0);
1716 val |= (0x64 << 0);
1717 an8855_write(priv, RX_CTRL_6, val);
1718
1719 val = an8855_read(priv, RX_CTRL_7);
1720 val &= ~(0xfffff << 0);
1721 val |= (0x2710 << 0);
1722 an8855_write(priv, RX_CTRL_7, val);
1723
1724 if (mode == SGMII_MODE_FORCE) {
1725 /* PCS Init */
1726 val = an8855_read(priv, QP_DIG_MODE_CTRL_0);
1727 val &= ~BIT(0);
1728 val &= ~BITS(4, 5);
1729 an8855_write(priv, QP_DIG_MODE_CTRL_0, val);
1730
1731 val = an8855_read(priv, RG_HSGMII_PCS_CTROL_1);
1732 val &= ~BIT(30);
1733 an8855_write(priv, RG_HSGMII_PCS_CTROL_1, val);
1734
1735 /* Rate Adaption - GMII path config. */
1736 val = an8855_read(priv, RG_AN_SGMII_MODE_FORCE);
1737 val |= BIT(0);
1738 val &= ~BITS(4, 5);
1739 an8855_write(priv, RG_AN_SGMII_MODE_FORCE, val);
1740
1741 val = an8855_read(priv, SGMII_STS_CTRL_0);
1742 val |= BIT(2);
1743 val &= ~(0x3 << 4);
1744 val |= (0x2 << 4);
1745 an8855_write(priv, SGMII_STS_CTRL_0, val);
1746
1747 val = an8855_read(priv, SGMII_REG_AN0);
1748 val &= ~BIT(12);
1749 an8855_write(priv, SGMII_REG_AN0, val);
1750
1751 val = an8855_read(priv, PHY_RX_FORCE_CTRL_0);
1752 val |= BIT(4);
1753 an8855_write(priv, PHY_RX_FORCE_CTRL_0, val);
1754
1755 val = an8855_read(priv, RATE_ADP_P0_CTRL_0);
1756 val &= ~BITS(0, 3);
1757 val |= BIT(28);
1758 an8855_write(priv, RATE_ADP_P0_CTRL_0, val);
1759
1760 val = an8855_read(priv, RG_RATE_ADAPT_CTRL_0);
1761 val |= BIT(0);
1762 val |= BIT(4);
1763 val |= BITS(26, 27);
1764 an8855_write(priv, RG_RATE_ADAPT_CTRL_0, val);
1765 } else {
1766 /* PCS Init */
1767 val = an8855_read(priv, RG_HSGMII_PCS_CTROL_1);
1768 val &= ~BIT(30);
1769 an8855_write(priv, RG_HSGMII_PCS_CTROL_1, val);
1770
1771 /* Set AN Ability - Interrupt */
1772 val = an8855_read(priv, SGMII_REG_AN_FORCE_CL37);
1773 val |= BIT(0);
1774 an8855_write(priv, SGMII_REG_AN_FORCE_CL37, val);
1775
1776 val = an8855_read(priv, SGMII_REG_AN_13);
1777 val &= ~(0x3f << 0);
1778 val |= (0xb << 0);
1779 val |= BIT(8);
1780 an8855_write(priv, SGMII_REG_AN_13, val);
1781
1782 /* Rate Adaption - GMII path config. */
1783 val = an8855_read(priv, SGMII_REG_AN0);
1784 val |= BIT(12);
1785 an8855_write(priv, SGMII_REG_AN0, val);
1786
1787 val = an8855_read(priv, MII_RA_AN_ENABLE);
1788 val |= BIT(0);
1789 an8855_write(priv, MII_RA_AN_ENABLE, val);
1790
1791 val = an8855_read(priv, RATE_ADP_P0_CTRL_0);
1792 val |= BIT(28);
1793 an8855_write(priv, RATE_ADP_P0_CTRL_0, val);
1794
1795 val = an8855_read(priv, RG_RATE_ADAPT_CTRL_0);
1796 val |= BIT(0);
1797 val |= BIT(4);
1798 val |= BITS(26, 27);
1799 an8855_write(priv, RG_RATE_ADAPT_CTRL_0, val);
1800
1801 /* Only for Co-SIM */
1802
1803 /* AN Speed up (Only for Co-SIM) */
1804
1805 /* Restart AN */
1806 val = an8855_read(priv, SGMII_REG_AN0);
1807 val |= BIT(9);
1808 val |= BIT(15);
1809 an8855_write(priv, SGMII_REG_AN0, val);
1810 }
1811
1812 /* bypass flow control to MAC */
1813 an8855_write(priv, MSG_RX_LIK_STS_0, 0x01010107);
1814 an8855_write(priv, MSG_RX_LIK_STS_2, 0x00000EEF);
1815
1816 return 0;
1817}
1818
1819static int
1820an8855_sgmii_setup_mode_force(struct an8855_priv *priv, u32 port,
1821 phy_interface_t interface)
1822{
1823 return an8855_sgmii_setup(priv, SGMII_MODE_FORCE);
1824}
1825
1826static int
1827an8855_sgmii_setup_mode_an(struct an8855_priv *priv, int port,
1828 phy_interface_t interface)
1829{
1830 return an8855_sgmii_setup(priv, SGMII_MODE_AN);
1831}
1832
1833static int
1834an8855_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
1835 phy_interface_t interface)
1836{
1837 struct an8855_priv *priv = ds->priv;
1838 struct phy_device *phydev;
1839 const struct dsa_port *dp;
1840
1841 if (!an8855_is_mac_port(port)) {
1842 dev_err(priv->dev, "port %d is not a MAC port\n", port);
1843 return -EINVAL;
1844 }
1845
1846 switch (interface) {
1847 case PHY_INTERFACE_MODE_RGMII:
1848 dp = dsa_to_port(ds, port);
1849 phydev = (dp->slave) ? dp->slave->phydev : NULL;
1850 return an8855_rgmii_setup(priv, port, interface, phydev);
1851 case PHY_INTERFACE_MODE_SGMII:
1852 return an8855_sgmii_setup_mode_an(priv, port, interface);
1853 case PHY_INTERFACE_MODE_2500BASEX:
1854 if (phylink_autoneg_inband(mode))
1855 return -EINVAL;
1856 return an8855_set_hsgmii_mode(priv);
1857 default:
1858 return -EINVAL;
1859 }
1860
1861 return -EINVAL;
1862}
1863
1864static int
1865an8855_sw_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
1866 const struct phylink_link_state *state)
1867{
1868 struct an8855_priv *priv = ds->priv;
1869
1870 return priv->info->mac_port_config(ds, port, mode, state->interface);
1871}
1872
1873static void
1874an8855_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
1875 const struct phylink_link_state *state)
1876{
1877 struct an8855_priv *priv = ds->priv;
1878 u32 mcr_cur, mcr_new;
1879
1880 if (!an8855_phy_mode_supported(ds, port, state))
1881 goto unsupported;
1882
1883 switch (port) {
1884 case 0: /* Internal phy */
1885 case 1:
1886 case 2:
1887 case 3:
1888 case 4:
1889 if (state->interface != PHY_INTERFACE_MODE_GMII)
1890 goto unsupported;
1891 break;
1892 case 5:
1893 if (priv->p5_interface == state->interface)
1894 break;
1895
1896 if (an8855_sw_mac_config(ds, port, mode, state) < 0)
1897 goto unsupported;
1898
1899 priv->p5_interface = state->interface;
1900 break;
1901 default:
1902unsupported:
1903 dev_err(ds->dev, "%s: unsupported %s port: %i\n",
1904 __func__, phy_modes(state->interface), port);
1905 return;
1906 }
1907
1908 if (phylink_autoneg_inband(mode) &&
1909 state->interface != PHY_INTERFACE_MODE_SGMII) {
1910 dev_err(ds->dev, "%s: in-band negotiation unsupported\n",
1911 __func__);
1912 return;
1913 }
1914
1915 mcr_cur = an8855_read(priv, AN8855_PMCR_P(port));
1916 mcr_new = mcr_cur;
1917 mcr_new &= ~PMCR_LINK_SETTINGS_MASK;
1918 mcr_new |= PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | PMCR_BACKOFF_EN |
1919 PMCR_BACKPR_EN | AN8855_FORCE_MODE;
1920
1921 if (mcr_new != mcr_cur)
1922 an8855_write(priv, AN8855_PMCR_P(port), mcr_new);
1923}
1924
1925static void
1926an8855_phylink_mac_link_down(struct dsa_switch *ds, int port,
1927 unsigned int mode,
1928 phy_interface_t interface)
1929{
1930 struct an8855_priv *priv = ds->priv;
1931
1932 an8855_clear(priv, AN8855_PMCR_P(port), PMCR_LINK_SETTINGS_MASK);
1933}
1934
1935static void
1936an8855_phylink_mac_link_up(struct dsa_switch *ds, int port,
1937 unsigned int mode,
1938 phy_interface_t interface,
1939 struct phy_device *phydev,
1940 int speed, int duplex,
1941 bool tx_pause, bool rx_pause)
1942{
1943 struct an8855_priv *priv = ds->priv;
1944 u32 mcr;
1945
1946 mcr = an8855_read(priv, AN8855_PMCR_P(port));
1947 mcr |= PMCR_RX_EN | PMCR_TX_EN | PMCR_FORCE_LNK;
1948 mcr &=
1949 ~(PMCR_FORCE_FDX | PMCR_SPEED_MASK | PMCR_TX_FC_EN | PMCR_RX_FC_EN);
1950
1951 if (interface == PHY_INTERFACE_MODE_RGMII
1952 || interface == PHY_INTERFACE_MODE_SGMII) {
1953 speed = SPEED_1000;
1954 duplex = DUPLEX_FULL;
1955 } else if (interface == PHY_INTERFACE_MODE_2500BASEX) {
1956 speed = SPEED_2500;
1957 duplex = DUPLEX_FULL;
1958 }
1959
1960 switch (speed) {
1961 case SPEED_2500:
1962 mcr |= PMCR_FORCE_SPEED_2500;
1963 break;
1964 case SPEED_1000:
1965 mcr |= PMCR_FORCE_SPEED_1000;
1966 if (priv->eee_enable & BIT(port))
1967 mcr |= PMCR_FORCE_EEE1G;
1968 break;
1969 case SPEED_100:
1970 mcr |= PMCR_FORCE_SPEED_100;
1971 if (priv->eee_enable & BIT(port))
1972 mcr |= PMCR_FORCE_EEE100;
1973 break;
1974 }
1975 if (duplex == DUPLEX_FULL) {
1976 mcr |= PMCR_FORCE_FDX;
1977 if (tx_pause)
1978 mcr |= PMCR_TX_FC_EN;
1979 if (rx_pause)
1980 mcr |= PMCR_RX_FC_EN;
1981 }
1982
1983 an8855_write(priv, AN8855_PMCR_P(port), mcr);
1984}
1985
1986static int
1987an8855_cpu_port_config(struct dsa_switch *ds, int port)
1988{
1989 struct an8855_priv *priv = ds->priv;
1990 phy_interface_t interface = PHY_INTERFACE_MODE_NA;
1991 int speed;
1992
1993 switch (port) {
1994 case 5:
1995 interface = PHY_INTERFACE_MODE_2500BASEX;
1996
1997 priv->p5_interface = interface;
1998 break;
1999 };
2000 if (interface == PHY_INTERFACE_MODE_NA)
2001 dev_err(priv->dev, "invalid interface\n");
2002
2003 if (interface == PHY_INTERFACE_MODE_2500BASEX)
2004 speed = SPEED_2500;
2005 else
2006 speed = SPEED_1000;
2007
2008 an8855_mac_config(ds, port, MLO_AN_FIXED, interface);
2009 an8855_write(priv, AN8855_PMCR_P(port),
2010 PMCR_CPU_PORT_SETTING(priv->id));
2011 an8855_phylink_mac_link_up(ds, port, MLO_AN_FIXED, interface, NULL,
2012 speed, DUPLEX_FULL, true, true);
2013
2014 return 0;
2015}
2016
2017static void
2018an8855_mac_port_validate(struct dsa_switch *ds, int port,
2019 unsigned long *supported)
2020{
2021 struct an8855_priv *priv = ds->priv;
2022
2023 an8855_sgmii_validate(priv, port, supported);
2024}
2025
2026static void
2027an8855_phylink_validate(struct dsa_switch *ds, int port,
2028 unsigned long *supported,
2029 struct phylink_link_state *state)
2030{
2031 struct an8855_priv *priv = ds->priv;
2032 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = {0,};
2033
2034 if (state->interface != PHY_INTERFACE_MODE_NA &&
2035 !an8855_phy_mode_supported(ds, port, state)) {
2036 linkmode_zero(supported);
2037 return;
2038 }
2039
2040 phylink_set_port_modes(mask);
2041
2042 if (state->interface != PHY_INTERFACE_MODE_TRGMII
2043 || state->interface != PHY_INTERFACE_MODE_USXGMII
2044 || state->interface != PHY_INTERFACE_MODE_10GKR
2045 || !phy_interface_mode_is_8023z(state->interface)) {
2046 phylink_set(mask, 10baseT_Half);
2047 phylink_set(mask, 10baseT_Full);
2048 phylink_set(mask, 100baseT_Half);
2049 phylink_set(mask, 100baseT_Full);
2050 phylink_set(mask, Autoneg);
2051 }
2052
2053 /* This switch only supports 1G full-duplex. */
2054 if (state->interface != PHY_INTERFACE_MODE_MII)
2055 phylink_set(mask, 1000baseT_Full);
2056
2057 priv->info->mac_port_validate(ds, port, mask);
2058
2059 phylink_set(mask, Pause);
2060 phylink_set(mask, Asym_Pause);
2061
2062 linkmode_and(supported, supported, mask);
2063 linkmode_and(state->advertising, state->advertising, mask);
2064
2065 /* We can only operate at 2500BaseX or 1000BaseX. If requested
2066 * to advertise both, only report advertising at 2500BaseX.
2067 */
2068 phylink_helper_basex_speed(state);
2069}
2070
2071static int
2072an8855_get_mac_eee(struct dsa_switch *ds, int port,
2073 struct ethtool_eee *e)
2074{
2075 struct an8855_priv *priv = ds->priv;
2076 u32 eeecr, pmsr, ckgcr;
2077
2078 e->eee_enabled = !!(priv->eee_enable & BIT(port));
2079
2080 if (e->eee_enabled) {
2081 eeecr = an8855_read(priv, AN8855_PMEEECR_P(port));
2082 e->tx_lpi_enabled = !(eeecr & LPI_MODE_EN);
2083 ckgcr = an8855_read(priv, AN8855_CKGCR);
2084 e->tx_lpi_timer =
2085 ((ckgcr & LPI_TXIDLE_THD_MASK) >> LPI_TXIDLE_THD) / 500;
2086 pmsr = an8855_read(priv, AN8855_PMSR_P(port));
2087 e->eee_active = e->eee_enabled
2088 && !!(pmsr & (PMSR_EEE1G | PMSR_EEE100M));
2089 } else {
2090 e->tx_lpi_enabled = 0;
2091 e->tx_lpi_timer = 0;
2092 e->eee_active = 0;
2093 }
2094 return 0;
2095}
2096
2097static int
2098an8855_set_mac_eee(struct dsa_switch *ds, int port,
2099 struct ethtool_eee *e)
2100{
2101 struct an8855_priv *priv = ds->priv;
2102 u32 eeecr;
2103
2104 if (e->eee_enabled) {
2105 priv->eee_enable |= BIT(port);
2106 eeecr = an8855_read(priv, AN8855_PMEEECR_P(port));
2107 eeecr &= ~LPI_MODE_EN;
2108 if (e->tx_lpi_enabled)
2109 eeecr |= LPI_MODE_EN;
2110 an8855_write(priv, AN8855_PMEEECR_P(port), eeecr);
2111 } else {
2112 priv->eee_enable &= ~(BIT(port));
2113 eeecr = an8855_read(priv, AN8855_PMEEECR_P(port));
2114 eeecr &= ~LPI_MODE_EN;
2115 an8855_write(priv, AN8855_PMEEECR_P(port), eeecr);
2116 }
2117
2118 return 0;
2119}
2120
2121static int
2122an8855_phylink_mac_link_state(struct dsa_switch *ds, int port,
2123 struct phylink_link_state *state)
2124{
2125 struct an8855_priv *priv = ds->priv;
2126 u32 pmsr;
2127
2128 if (port < 0 || port >= AN8855_NUM_PORTS)
2129 return -EINVAL;
2130
2131 pmsr = an8855_read(priv, AN8855_PMSR_P(port));
2132
2133 state->link = (pmsr & PMSR_LINK);
2134 state->an_complete = state->link;
2135 state->duplex = !!(pmsr & PMSR_DPX);
2136
2137 switch (pmsr & PMSR_SPEED_MASK) {
2138 case PMSR_SPEED_10:
2139 state->speed = SPEED_10;
2140 break;
2141 case PMSR_SPEED_100:
2142 state->speed = SPEED_100;
2143 break;
2144 case PMSR_SPEED_1000:
2145 state->speed = SPEED_1000;
2146 break;
2147 case PMSR_SPEED_2500:
2148 state->speed = SPEED_2500;
2149 break;
2150 default:
2151 state->speed = SPEED_UNKNOWN;
2152 break;
2153 }
2154
2155 state->pause &= ~(MLO_PAUSE_RX | MLO_PAUSE_TX);
2156 if (pmsr & PMSR_RX_FC)
2157 state->pause |= MLO_PAUSE_RX;
2158 if (pmsr & PMSR_TX_FC)
2159 state->pause |= MLO_PAUSE_TX;
2160
2161 return 1;
2162}
2163
2164static int
2165an8855_sw_phylink_mac_link_state(struct dsa_switch *ds, int port,
2166 struct phylink_link_state *state)
2167{
2168 struct an8855_priv *priv = ds->priv;
2169
2170 return priv->info->mac_port_get_state(ds, port, state);
2171}
2172
2173static int
2174an8855_sw_setup(struct dsa_switch *ds)
2175{
2176 struct an8855_priv *priv = ds->priv;
2177
2178 return priv->info->sw_setup(ds);
2179}
2180
2181static int
2182an8855_sw_phy_read(struct dsa_switch *ds, int port, int regnum)
2183{
2184 struct an8855_priv *priv = ds->priv;
2185
2186 return priv->info->phy_read(ds, port, regnum);
2187}
2188
2189static int
2190an8855_sw_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
2191{
2192 struct an8855_priv *priv = ds->priv;
2193
2194 return priv->info->phy_write(ds, port, regnum, val);
2195}
2196
2197static const struct dsa_switch_ops an8855_switch_ops = {
2198 .get_tag_protocol = air_get_tag_protocol,
2199 .setup = an8855_sw_setup,
2200 .get_strings = an8855_get_strings,
2201 .phy_read = an8855_sw_phy_read,
2202 .phy_write = an8855_sw_phy_write,
2203 .get_ethtool_stats = an8855_get_ethtool_stats,
2204 .get_sset_count = an8855_get_sset_count,
2205 .port_enable = an8855_port_enable,
2206 .port_disable = an8855_port_disable,
2207 .port_stp_state_set = an8855_stp_state_set,
2208 .port_bridge_join = an8855_port_bridge_join,
2209 .port_bridge_leave = an8855_port_bridge_leave,
2210 .port_fdb_add = an8855_port_fdb_add,
2211 .port_fdb_del = an8855_port_fdb_del,
2212 .port_fdb_dump = an8855_port_fdb_dump,
2213 .port_vlan_filtering = an8855_port_vlan_filtering,
2214 .port_vlan_prepare = an8855_port_vlan_prepare,
2215 .port_vlan_add = an8855_port_vlan_add,
2216 .port_vlan_del = an8855_port_vlan_del,
2217 .port_mirror_add = an8855_port_mirror_add,
2218 .port_mirror_del = an8855_port_mirror_del,
2219 .phylink_validate = an8855_phylink_validate,
2220 .phylink_mac_link_state = an8855_sw_phylink_mac_link_state,
2221 .phylink_mac_config = an8855_phylink_mac_config,
2222 .phylink_mac_link_down = an8855_phylink_mac_link_down,
2223 .phylink_mac_link_up = an8855_phylink_mac_link_up,
2224 .get_mac_eee = an8855_get_mac_eee,
2225 .set_mac_eee = an8855_set_mac_eee,
2226};
2227
2228static const struct an8855_dev_info an8855_table[] = {
2229 [ID_AN8855] = {
2230 .id = ID_AN8855,
2231 .sw_setup = an8855_setup,
2232 .phy_read = an8855_phy_read,
2233 .phy_write = an8855_phy_write,
2234 .pad_setup = an8855_pad_setup,
2235 .cpu_port_config = an8855_cpu_port_config,
2236 .phy_mode_supported = an8855_phy_supported,
2237 .mac_port_validate = an8855_mac_port_validate,
2238 .mac_port_get_state = an8855_phylink_mac_link_state,
2239 .mac_port_config = an8855_mac_config,
2240 },
2241};
2242
2243static const struct of_device_id an8855_of_match[] = {
2244 {.compatible = "airoha,an8855", .data = &an8855_table[ID_AN8855],
2245 },
2246 { /* sentinel */ },
2247};
2248
2249MODULE_DEVICE_TABLE(of, an8855_of_match);
2250
2251static int
2252an8855_probe(struct mdio_device *mdiodev)
2253{
2254 struct an8855_priv *priv;
2255 struct device_node *dn;
2256 struct device_node *switch_node = NULL;
2257 int ret;
2258
2259 dn = mdiodev->dev.of_node;
2260
2261 priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
2262 if (!priv)
2263 return -ENOMEM;
2264
2265 priv->ds = dsa_switch_alloc(&mdiodev->dev, AN8855_NUM_PORTS);
2266 if (!priv->ds)
2267 return -ENOMEM;
2268
2269 /* Get the hardware identifier from the devicetree node.
2270 * We will need it for some of the clock and regulator setup.
2271 */
2272 priv->info = of_device_get_match_data(&mdiodev->dev);
2273 if (!priv->info)
2274 return -EINVAL;
2275
2276 /* Sanity check if these required device operations are filled
2277 * properly.
2278 */
2279 if (!priv->info->sw_setup || !priv->info->pad_setup ||
2280 !priv->info->phy_read || !priv->info->phy_write ||
2281 !priv->info->phy_mode_supported ||
2282 !priv->info->mac_port_validate ||
2283 !priv->info->mac_port_get_state || !priv->info->mac_port_config)
2284 return -EINVAL;
2285
2286 dev_info(&mdiodev->dev, "Airoha AN8855 DSA driver, version %s\n",
2287 ARHT_AN8855_DSA_DRIVER_VER);
2288 priv->phy_base = AN8855_GPHY_SMI_ADDR_DEFAULT;
2289 priv->id = priv->info->id;
2290
2291 priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset",
2292 GPIOD_OUT_LOW);
2293 if (IS_ERR(priv->reset)) {
2294 dev_err(&mdiodev->dev, "Couldn't get our reset line\n");
2295 return PTR_ERR(priv->reset);
2296 }
2297
2298 switch_node = of_find_node_by_name(NULL, "switch0");
2299 if (switch_node) {
2300 priv->base = of_iomap(switch_node, 0);
2301 if (priv->base == NULL) {
2302 dev_err(&mdiodev->dev, "of_iomap failed\n");
2303 return -ENOMEM;
2304 }
2305 }
2306
2307 ret = of_property_read_u32(dn, "changesmiaddr", &priv->phy_base_new);
2308 if ((ret < 0) || (priv->phy_base_new > 0x1f))
2309 priv->phy_base_new = -1;
2310
2311 priv->bus = mdiodev->bus;
2312 priv->dev = &mdiodev->dev;
2313 priv->ds->priv = priv;
2314 priv->ds->ops = &an8855_switch_ops;
2315 mutex_init(&priv->reg_mutex);
2316 dev_set_drvdata(&mdiodev->dev, priv);
2317
2318 ret = dsa_register_switch(priv->ds);
2319 if (ret) {
2320 if (priv->base)
2321 iounmap(priv->base);
2322
2323 return ret;
2324 }
2325 an8855_nl_init(&priv);
2326
2327 return 0;
2328}
2329
2330static void
2331an8855_remove(struct mdio_device *mdiodev)
2332{
2333 struct an8855_priv *priv = dev_get_drvdata(&mdiodev->dev);
2334
2335 dsa_unregister_switch(priv->ds);
2336 mutex_destroy(&priv->reg_mutex);
2337
2338 if (priv->base)
2339 iounmap(priv->base);
2340
2341 an8855_nl_exit();
2342}
2343
2344static struct mdio_driver an8855_mdio_driver = {
2345 .probe = an8855_probe,
2346 .remove = an8855_remove,
2347 .mdiodrv.driver = {
2348 .name = "an8855",
2349 .of_match_table = an8855_of_match,
2350 },
2351};
2352
2353mdio_module_driver(an8855_mdio_driver);
2354
2355MODULE_AUTHOR("Min Yao <min.yao@airoha.com>");
2356MODULE_DESCRIPTION("Driver for Airoha AN8855 Switch");
2357MODULE_VERSION(ARHT_AN8855_DSA_DRIVER_VER);
2358MODULE_LICENSE("GPL");