qca8k.c 64.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4
/*
 * Copyright (C) 2009 Felix Fietkau <nbd@nbd.name>
 * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
5
 * Copyright (c) 2015, 2019, The Linux Foundation. All rights reserved.
6 7 8 9 10 11
 * Copyright (c) 2016 John Crispin <john@phrozen.org>
 */

#include <linux/module.h>
#include <linux/phy.h>
#include <linux/netdevice.h>
12
#include <linux/bitfield.h>
13
#include <linux/regmap.h>
14 15
#include <net/dsa.h>
#include <linux/of_net.h>
16
#include <linux/of_mdio.h>
17 18 19
#include <linux/of_platform.h>
#include <linux/if_bridge.h>
#include <linux/mdio.h>
20
#include <linux/phylink.h>
21
#include <linux/gpio/consumer.h>
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include <linux/etherdevice.h>

#include "qca8k.h"

#define MIB_DESC(_s, _o, _n)	\
	{			\
		.size = (_s),	\
		.offset = (_o),	\
		.name = (_n),	\
	}

static const struct qca8k_mib_desc ar8327_mib[] = {
	MIB_DESC(1, 0x00, "RxBroad"),
	MIB_DESC(1, 0x04, "RxPause"),
	MIB_DESC(1, 0x08, "RxMulti"),
	MIB_DESC(1, 0x0c, "RxFcsErr"),
	MIB_DESC(1, 0x10, "RxAlignErr"),
	MIB_DESC(1, 0x14, "RxRunt"),
	MIB_DESC(1, 0x18, "RxFragment"),
	MIB_DESC(1, 0x1c, "Rx64Byte"),
	MIB_DESC(1, 0x20, "Rx128Byte"),
	MIB_DESC(1, 0x24, "Rx256Byte"),
	MIB_DESC(1, 0x28, "Rx512Byte"),
	MIB_DESC(1, 0x2c, "Rx1024Byte"),
	MIB_DESC(1, 0x30, "Rx1518Byte"),
	MIB_DESC(1, 0x34, "RxMaxByte"),
	MIB_DESC(1, 0x38, "RxTooLong"),
	MIB_DESC(2, 0x3c, "RxGoodByte"),
	MIB_DESC(2, 0x44, "RxBadByte"),
	MIB_DESC(1, 0x4c, "RxOverFlow"),
	MIB_DESC(1, 0x50, "Filtered"),
	MIB_DESC(1, 0x54, "TxBroad"),
	MIB_DESC(1, 0x58, "TxPause"),
	MIB_DESC(1, 0x5c, "TxMulti"),
	MIB_DESC(1, 0x60, "TxUnderRun"),
	MIB_DESC(1, 0x64, "Tx64Byte"),
	MIB_DESC(1, 0x68, "Tx128Byte"),
	MIB_DESC(1, 0x6c, "Tx256Byte"),
	MIB_DESC(1, 0x70, "Tx512Byte"),
	MIB_DESC(1, 0x74, "Tx1024Byte"),
	MIB_DESC(1, 0x78, "Tx1518Byte"),
	MIB_DESC(1, 0x7c, "TxMaxByte"),
	MIB_DESC(1, 0x80, "TxOverSize"),
	MIB_DESC(2, 0x84, "TxByte"),
	MIB_DESC(1, 0x8c, "TxCollision"),
	MIB_DESC(1, 0x90, "TxAbortCol"),
	MIB_DESC(1, 0x94, "TxMultiCol"),
	MIB_DESC(1, 0x98, "TxSingleCol"),
	MIB_DESC(1, 0x9c, "TxExcDefer"),
	MIB_DESC(1, 0xa0, "TxDefer"),
	MIB_DESC(1, 0xa4, "TxLateCol"),
73 74
	MIB_DESC(1, 0xa8, "RXUnicast"),
	MIB_DESC(1, 0xac, "TXUnicast"),
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
};

/* The 32bit switch registers are accessed indirectly. To achieve this we need
 * to set the page of the register. Track the last page that was set to reduce
 * mdio writes
 */
static u16 qca8k_current_page = 0xffff;

static void
qca8k_split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
{
	regaddr >>= 1;
	*r1 = regaddr & 0x1e;

	regaddr >>= 5;
	*r2 = regaddr & 0x7;

	regaddr >>= 3;
	*page = regaddr & 0x3ff;
}

96 97
static int
qca8k_mii_read32(struct mii_bus *bus, int phy_id, u32 regnum, u32 *val)
98 99 100 101 102
{
	int ret;

	ret = bus->read(bus, phy_id, regnum);
	if (ret >= 0) {
103
		*val = ret;
104
		ret = bus->read(bus, phy_id, regnum + 1);
105
		*val |= ret << 16;
106 107 108 109 110
	}

	if (ret < 0) {
		dev_err_ratelimited(&bus->dev,
				    "failed to read qca8k 32bit register\n");
111
		*val = 0;
112 113 114
		return ret;
	}

115
	return 0;
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
}

static void
qca8k_mii_write32(struct mii_bus *bus, int phy_id, u32 regnum, u32 val)
{
	u16 lo, hi;
	int ret;

	lo = val & 0xffff;
	hi = (u16)(val >> 16);

	ret = bus->write(bus, phy_id, regnum, lo);
	if (ret >= 0)
		ret = bus->write(bus, phy_id, regnum + 1, hi);
	if (ret < 0)
		dev_err_ratelimited(&bus->dev,
				    "failed to write qca8k 32bit register\n");
}

135
static int
136 137
qca8k_set_page(struct mii_bus *bus, u16 page)
{
138 139
	int ret;

140
	if (page == qca8k_current_page)
141
		return 0;
142

143 144
	ret = bus->write(bus, 0x18, 0, page);
	if (ret < 0) {
145 146
		dev_err_ratelimited(&bus->dev,
				    "failed to set qca8k page\n");
147 148 149
		return ret;
	}

150
	qca8k_current_page = page;
151
	usleep_range(1000, 2000);
152
	return 0;
153 154
}

155 156
static int
qca8k_read(struct qca8k_priv *priv, u32 reg, u32 *val)
157
{
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	return regmap_read(priv->regmap, reg, val);
}

static int
qca8k_write(struct qca8k_priv *priv, u32 reg, u32 val)
{
	return regmap_write(priv->regmap, reg, val);
}

static int
qca8k_rmw(struct qca8k_priv *priv, u32 reg, u32 mask, u32 write_val)
{
	return regmap_update_bits(priv->regmap, reg, mask, write_val);
}

static int
qca8k_regmap_read(void *ctx, uint32_t reg, uint32_t *val)
{
	struct qca8k_priv *priv = (struct qca8k_priv *)ctx;
177
	struct mii_bus *bus = priv->bus;
178
	u16 r1, r2, page;
179
	int ret;
180 181 182

	qca8k_split_addr(reg, &r1, &r2, &page);

183
	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
184

185 186
	ret = qca8k_set_page(bus, page);
	if (ret < 0)
187 188
		goto exit;

189
	ret = qca8k_mii_read32(bus, 0x10 | r2, r1, val);
190

191
exit:
192
	mutex_unlock(&bus->mdio_lock);
193
	return ret;
194 195
}

196
static int
197
qca8k_regmap_write(void *ctx, uint32_t reg, uint32_t val)
198
{
199
	struct qca8k_priv *priv = (struct qca8k_priv *)ctx;
200
	struct mii_bus *bus = priv->bus;
201
	u16 r1, r2, page;
202
	int ret;
203 204 205

	qca8k_split_addr(reg, &r1, &r2, &page);

206
	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
207

208 209 210 211
	ret = qca8k_set_page(bus, page);
	if (ret < 0)
		goto exit;

212
	qca8k_mii_write32(bus, 0x10 | r2, r1, val);
213

214
exit:
215
	mutex_unlock(&bus->mdio_lock);
216
	return ret;
217 218
}

219
static int
220
qca8k_regmap_update_bits(void *ctx, uint32_t reg, uint32_t mask, uint32_t write_val)
221
{
222
	struct qca8k_priv *priv = (struct qca8k_priv *)ctx;
223
	struct mii_bus *bus = priv->bus;
224
	u16 r1, r2, page;
225 226
	u32 val;
	int ret;
227 228 229

	qca8k_split_addr(reg, &r1, &r2, &page);

230
	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
231

232 233 234 235
	ret = qca8k_set_page(bus, page);
	if (ret < 0)
		goto exit;

236 237
	ret = qca8k_mii_read32(bus, 0x10 | r2, r1, &val);
	if (ret < 0)
238 239 240 241 242
		goto exit;

	val &= ~mask;
	val |= write_val;
	qca8k_mii_write32(bus, 0x10 | r2, r1, val);
243

244
exit:
245
	mutex_unlock(&bus->mdio_lock);
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

	return ret;
}

static const struct regmap_range qca8k_readable_ranges[] = {
	regmap_reg_range(0x0000, 0x00e4), /* Global control */
	regmap_reg_range(0x0100, 0x0168), /* EEE control */
	regmap_reg_range(0x0200, 0x0270), /* Parser control */
	regmap_reg_range(0x0400, 0x0454), /* ACL */
	regmap_reg_range(0x0600, 0x0718), /* Lookup */
	regmap_reg_range(0x0800, 0x0b70), /* QM */
	regmap_reg_range(0x0c00, 0x0c80), /* PKT */
	regmap_reg_range(0x0e00, 0x0e98), /* L3 */
	regmap_reg_range(0x1000, 0x10ac), /* MIB - Port0 */
	regmap_reg_range(0x1100, 0x11ac), /* MIB - Port1 */
	regmap_reg_range(0x1200, 0x12ac), /* MIB - Port2 */
	regmap_reg_range(0x1300, 0x13ac), /* MIB - Port3 */
	regmap_reg_range(0x1400, 0x14ac), /* MIB - Port4 */
	regmap_reg_range(0x1500, 0x15ac), /* MIB - Port5 */
	regmap_reg_range(0x1600, 0x16ac), /* MIB - Port6 */

};

269
static const struct regmap_access_table qca8k_readable_table = {
270 271 272 273
	.yes_ranges = qca8k_readable_ranges,
	.n_yes_ranges = ARRAY_SIZE(qca8k_readable_ranges),
};

274
static struct regmap_config qca8k_regmap_config = {
275 276 277 278 279 280
	.reg_bits = 16,
	.val_bits = 32,
	.reg_stride = 4,
	.max_register = 0x16ac, /* end MIB - Port6 range */
	.reg_read = qca8k_regmap_read,
	.reg_write = qca8k_regmap_write,
281
	.reg_update_bits = qca8k_regmap_update_bits,
282
	.rd_table = &qca8k_readable_table,
283 284
	.disable_locking = true, /* Locking is handled by qca8k read/write */
	.cache_type = REGCACHE_NONE, /* Explicitly disable CACHE */
285 286 287 288 289
};

static int
qca8k_busy_wait(struct qca8k_priv *priv, u32 reg, u32 mask)
{
290
	u32 val;
291

292 293
	return regmap_read_poll_timeout(priv->regmap, reg, val, !(val & mask), 0,
				       QCA8K_BUSY_WAIT_TIMEOUT * USEC_PER_MSEC);
294 295
}

296
static int
297 298
qca8k_fdb_read(struct qca8k_priv *priv, struct qca8k_fdb *fdb)
{
299
	u32 reg[4], val;
300
	int i, ret;
301 302

	/* load the ARL table into an array */
303
	for (i = 0; i < 4; i++) {
304 305 306
		ret = qca8k_read(priv, QCA8K_REG_ATU_DATA0 + (i * 4), &val);
		if (ret < 0)
			return ret;
307 308 309

		reg[i] = val;
	}
310 311

	/* vid - 83:72 */
312
	fdb->vid = FIELD_GET(QCA8K_ATU_VID_MASK, reg[2]);
313
	/* aging - 67:64 */
314
	fdb->aging = FIELD_GET(QCA8K_ATU_STATUS_MASK, reg[2]);
315
	/* portmask - 54:48 */
316
	fdb->port_mask = FIELD_GET(QCA8K_ATU_PORT_MASK, reg[1]);
317
	/* mac - 47:0 */
318 319 320 321 322 323
	fdb->mac[0] = FIELD_GET(QCA8K_ATU_ADDR0_MASK, reg[1]);
	fdb->mac[1] = FIELD_GET(QCA8K_ATU_ADDR1_MASK, reg[1]);
	fdb->mac[2] = FIELD_GET(QCA8K_ATU_ADDR2_MASK, reg[0]);
	fdb->mac[3] = FIELD_GET(QCA8K_ATU_ADDR3_MASK, reg[0]);
	fdb->mac[4] = FIELD_GET(QCA8K_ATU_ADDR4_MASK, reg[0]);
	fdb->mac[5] = FIELD_GET(QCA8K_ATU_ADDR5_MASK, reg[0]);
324 325

	return 0;
326 327 328 329 330 331 332 333 334 335
}

static void
qca8k_fdb_write(struct qca8k_priv *priv, u16 vid, u8 port_mask, const u8 *mac,
		u8 aging)
{
	u32 reg[3] = { 0 };
	int i;

	/* vid - 83:72 */
336
	reg[2] = FIELD_PREP(QCA8K_ATU_VID_MASK, vid);
337
	/* aging - 67:64 */
338
	reg[2] |= FIELD_PREP(QCA8K_ATU_STATUS_MASK, aging);
339
	/* portmask - 54:48 */
340
	reg[1] = FIELD_PREP(QCA8K_ATU_PORT_MASK, port_mask);
341
	/* mac - 47:0 */
342 343 344 345 346 347
	reg[1] |= FIELD_PREP(QCA8K_ATU_ADDR0_MASK, mac[0]);
	reg[1] |= FIELD_PREP(QCA8K_ATU_ADDR1_MASK, mac[1]);
	reg[0] |= FIELD_PREP(QCA8K_ATU_ADDR2_MASK, mac[2]);
	reg[0] |= FIELD_PREP(QCA8K_ATU_ADDR3_MASK, mac[3]);
	reg[0] |= FIELD_PREP(QCA8K_ATU_ADDR4_MASK, mac[4]);
	reg[0] |= FIELD_PREP(QCA8K_ATU_ADDR5_MASK, mac[5]);
348 349 350 351 352 353 354 355 356 357

	/* load the array into the ARL table */
	for (i = 0; i < 3; i++)
		qca8k_write(priv, QCA8K_REG_ATU_DATA0 + (i * 4), reg[i]);
}

static int
qca8k_fdb_access(struct qca8k_priv *priv, enum qca8k_fdb_cmd cmd, int port)
{
	u32 reg;
358
	int ret;
359 360 361 362 363 364

	/* Set the command and FDB index */
	reg = QCA8K_ATU_FUNC_BUSY;
	reg |= cmd;
	if (port >= 0) {
		reg |= QCA8K_ATU_FUNC_PORT_EN;
365
		reg |= FIELD_PREP(QCA8K_ATU_FUNC_PORT_MASK, port);
366 367 368
	}

	/* Write the function register triggering the table access */
369 370 371
	ret = qca8k_write(priv, QCA8K_REG_ATU_FUNC, reg);
	if (ret)
		return ret;
372 373

	/* wait for completion */
374 375 376
	ret = qca8k_busy_wait(priv, QCA8K_REG_ATU_FUNC, QCA8K_ATU_FUNC_BUSY);
	if (ret)
		return ret;
377 378 379

	/* Check for table full violation when adding an entry */
	if (cmd == QCA8K_FDB_LOAD) {
380 381 382
		ret = qca8k_read(priv, QCA8K_REG_ATU_FUNC, &reg);
		if (ret < 0)
			return ret;
383 384 385 386 387 388 389 390 391 392 393 394 395 396
		if (reg & QCA8K_ATU_FUNC_FULL)
			return -1;
	}

	return 0;
}

static int
qca8k_fdb_next(struct qca8k_priv *priv, struct qca8k_fdb *fdb, int port)
{
	int ret;

	qca8k_fdb_write(priv, fdb->vid, fdb->port_mask, fdb->mac, fdb->aging);
	ret = qca8k_fdb_access(priv, QCA8K_FDB_NEXT, port);
397 398
	if (ret < 0)
		return ret;
399

400
	return qca8k_fdb_read(priv, fdb);
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
}

static int
qca8k_fdb_add(struct qca8k_priv *priv, const u8 *mac, u16 port_mask,
	      u16 vid, u8 aging)
{
	int ret;

	mutex_lock(&priv->reg_mutex);
	qca8k_fdb_write(priv, vid, port_mask, mac, aging);
	ret = qca8k_fdb_access(priv, QCA8K_FDB_LOAD, -1);
	mutex_unlock(&priv->reg_mutex);

	return ret;
}

static int
qca8k_fdb_del(struct qca8k_priv *priv, const u8 *mac, u16 port_mask, u16 vid)
{
	int ret;

	mutex_lock(&priv->reg_mutex);
	qca8k_fdb_write(priv, vid, port_mask, mac, 0);
	ret = qca8k_fdb_access(priv, QCA8K_FDB_PURGE, -1);
	mutex_unlock(&priv->reg_mutex);

	return ret;
}

static void
qca8k_fdb_flush(struct qca8k_priv *priv)
{
	mutex_lock(&priv->reg_mutex);
	qca8k_fdb_access(priv, QCA8K_FDB_FLUSH, -1);
	mutex_unlock(&priv->reg_mutex);
}

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
static int
qca8k_fdb_search_and_insert(struct qca8k_priv *priv, u8 port_mask,
			    const u8 *mac, u16 vid)
{
	struct qca8k_fdb fdb = { 0 };
	int ret;

	mutex_lock(&priv->reg_mutex);

	qca8k_fdb_write(priv, vid, 0, mac, 0);
	ret = qca8k_fdb_access(priv, QCA8K_FDB_SEARCH, -1);
	if (ret < 0)
		goto exit;

	ret = qca8k_fdb_read(priv, &fdb);
	if (ret < 0)
		goto exit;

	/* Rule exist. Delete first */
	if (!fdb.aging) {
		ret = qca8k_fdb_access(priv, QCA8K_FDB_PURGE, -1);
		if (ret)
			goto exit;
	}

	/* Add port to fdb portmask */
	fdb.port_mask |= port_mask;

	qca8k_fdb_write(priv, vid, fdb.port_mask, mac, fdb.aging);
	ret = qca8k_fdb_access(priv, QCA8K_FDB_LOAD, -1);

exit:
	mutex_unlock(&priv->reg_mutex);
	return ret;
}

static int
qca8k_fdb_search_and_del(struct qca8k_priv *priv, u8 port_mask,
			 const u8 *mac, u16 vid)
{
	struct qca8k_fdb fdb = { 0 };
	int ret;

	mutex_lock(&priv->reg_mutex);

	qca8k_fdb_write(priv, vid, 0, mac, 0);
	ret = qca8k_fdb_access(priv, QCA8K_FDB_SEARCH, -1);
	if (ret < 0)
		goto exit;

	/* Rule doesn't exist. Why delete? */
	if (!fdb.aging) {
		ret = -EINVAL;
		goto exit;
	}

	ret = qca8k_fdb_access(priv, QCA8K_FDB_PURGE, -1);
	if (ret)
		goto exit;

	/* Only port in the rule is this port. Don't re insert */
	if (fdb.port_mask == port_mask)
		goto exit;

	/* Remove port from port mask */
	fdb.port_mask &= ~port_mask;

	qca8k_fdb_write(priv, vid, fdb.port_mask, mac, fdb.aging);
	ret = qca8k_fdb_access(priv, QCA8K_FDB_LOAD, -1);

exit:
	mutex_unlock(&priv->reg_mutex);
	return ret;
}

513 514 515 516
static int
qca8k_vlan_access(struct qca8k_priv *priv, enum qca8k_vlan_cmd cmd, u16 vid)
{
	u32 reg;
517
	int ret;
518 519 520 521

	/* Set the command and VLAN index */
	reg = QCA8K_VTU_FUNC1_BUSY;
	reg |= cmd;
522
	reg |= FIELD_PREP(QCA8K_VTU_FUNC1_VID_MASK, vid);
523 524

	/* Write the function register triggering the table access */
525 526 527
	ret = qca8k_write(priv, QCA8K_REG_VTU_FUNC1, reg);
	if (ret)
		return ret;
528 529

	/* wait for completion */
530 531 532
	ret = qca8k_busy_wait(priv, QCA8K_REG_VTU_FUNC1, QCA8K_VTU_FUNC1_BUSY);
	if (ret)
		return ret;
533 534 535

	/* Check for table full violation when adding an entry */
	if (cmd == QCA8K_VLAN_LOAD) {
536 537 538
		ret = qca8k_read(priv, QCA8K_REG_VTU_FUNC1, &reg);
		if (ret < 0)
			return ret;
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
		if (reg & QCA8K_VTU_FUNC1_FULL)
			return -ENOMEM;
	}

	return 0;
}

static int
qca8k_vlan_add(struct qca8k_priv *priv, u8 port, u16 vid, bool untagged)
{
	u32 reg;
	int ret;

	/*
	   We do the right thing with VLAN 0 and treat it as untagged while
	   preserving the tag on egress.
	 */
	if (vid == 0)
		return 0;

	mutex_lock(&priv->reg_mutex);
	ret = qca8k_vlan_access(priv, QCA8K_VLAN_READ, vid);
	if (ret < 0)
		goto out;

564 565
	ret = qca8k_read(priv, QCA8K_REG_VTU_FUNC0, &reg);
	if (ret < 0)
566
		goto out;
567
	reg |= QCA8K_VTU_FUNC0_VALID | QCA8K_VTU_FUNC0_IVL_EN;
568
	reg &= ~QCA8K_VTU_FUNC0_EG_MODE_PORT_MASK(port);
569
	if (untagged)
570
		reg |= QCA8K_VTU_FUNC0_EG_MODE_PORT_UNTAG(port);
571
	else
572
		reg |= QCA8K_VTU_FUNC0_EG_MODE_PORT_TAG(port);
573

574 575
	ret = qca8k_write(priv, QCA8K_REG_VTU_FUNC0, reg);
	if (ret)
576
		goto out;
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
	ret = qca8k_vlan_access(priv, QCA8K_VLAN_LOAD, vid);

out:
	mutex_unlock(&priv->reg_mutex);

	return ret;
}

static int
qca8k_vlan_del(struct qca8k_priv *priv, u8 port, u16 vid)
{
	u32 reg, mask;
	int ret, i;
	bool del;

	mutex_lock(&priv->reg_mutex);
	ret = qca8k_vlan_access(priv, QCA8K_VLAN_READ, vid);
	if (ret < 0)
		goto out;

597 598
	ret = qca8k_read(priv, QCA8K_REG_VTU_FUNC0, &reg);
	if (ret < 0)
599
		goto out;
600 601
	reg &= ~QCA8K_VTU_FUNC0_EG_MODE_PORT_MASK(port);
	reg |= QCA8K_VTU_FUNC0_EG_MODE_PORT_NOT(port);
602 603 604 605

	/* Check if we're the last member to be removed */
	del = true;
	for (i = 0; i < QCA8K_NUM_PORTS; i++) {
606
		mask = QCA8K_VTU_FUNC0_EG_MODE_PORT_NOT(i);
607 608 609 610 611 612 613 614 615 616

		if ((reg & mask) != mask) {
			del = false;
			break;
		}
	}

	if (del) {
		ret = qca8k_vlan_access(priv, QCA8K_VLAN_PURGE, vid);
	} else {
617 618
		ret = qca8k_write(priv, QCA8K_REG_VTU_FUNC0, reg);
		if (ret)
619
			goto out;
620 621 622 623 624 625 626 627 628
		ret = qca8k_vlan_access(priv, QCA8K_VLAN_LOAD, vid);
	}

out:
	mutex_unlock(&priv->reg_mutex);

	return ret;
}

629
static int
630 631
qca8k_mib_init(struct qca8k_priv *priv)
{
632 633
	int ret;

634
	mutex_lock(&priv->reg_mutex);
635
	ret = regmap_set_bits(priv->regmap, QCA8K_REG_MIB, QCA8K_MIB_FLUSH | QCA8K_MIB_BUSY);
636 637 638
	if (ret)
		goto exit;

639 640 641
	ret = qca8k_busy_wait(priv, QCA8K_REG_MIB, QCA8K_MIB_BUSY);
	if (ret)
		goto exit;
642

643
	ret = regmap_set_bits(priv->regmap, QCA8K_REG_MIB, QCA8K_MIB_CPU_KEEP);
644 645
	if (ret)
		goto exit;
646 647 648

	ret = qca8k_write(priv, QCA8K_REG_MODULE_EN, QCA8K_MODULE_EN_MIB);

649
exit:
650
	mutex_unlock(&priv->reg_mutex);
651
	return ret;
652 653 654 655 656
}

static void
qca8k_port_set_status(struct qca8k_priv *priv, int port, int enable)
{
657
	u32 mask = QCA8K_PORT_STATUS_TXMAC | QCA8K_PORT_STATUS_RXMAC;
658 659

	/* Port 0 and 6 have no internal PHY */
660
	if (port > 0 && port < 6)
661 662 663
		mask |= QCA8K_PORT_STATUS_LINK_AUTO;

	if (enable)
664
		regmap_set_bits(priv->regmap, QCA8K_REG_PORT_STATUS(port), mask);
665
	else
666
		regmap_clear_bits(priv->regmap, QCA8K_REG_PORT_STATUS(port), mask);
667 668
}

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
static u32
qca8k_port_to_phy(int port)
{
	/* From Andrew Lunn:
	 * Port 0 has no internal phy.
	 * Port 1 has an internal PHY at MDIO address 0.
	 * Port 2 has an internal PHY at MDIO address 1.
	 * ...
	 * Port 5 has an internal PHY at MDIO address 4.
	 * Port 6 has no internal PHY.
	 */

	return port - 1;
}

684
static int
685
qca8k_mdio_busy_wait(struct mii_bus *bus, u32 reg, u32 mask)
686 687 688
{
	u16 r1, r2, page;
	u32 val;
689
	int ret, ret1;
690 691 692

	qca8k_split_addr(reg, &r1, &r2, &page);

693
	ret = read_poll_timeout(qca8k_mii_read32, ret1, !(val & mask), 0,
694
				QCA8K_BUSY_WAIT_TIMEOUT * USEC_PER_MSEC, false,
695
				bus, 0x10 | r2, r1, &val);
696 697 698 699

	/* Check if qca8k_read has failed for a different reason
	 * before returnting -ETIMEDOUT
	 */
700 701
	if (ret < 0 && ret1 < 0)
		return ret1;
702 703 704 705

	return ret;
}

706
static int
707
qca8k_mdio_write(struct mii_bus *bus, int phy, int regnum, u16 data)
708
{
709
	u16 r1, r2, page;
710
	u32 val;
711
	int ret;
712 713 714 715 716 717 718 719 720

	if (regnum >= QCA8K_MDIO_MASTER_MAX_REG)
		return -EINVAL;

	val = QCA8K_MDIO_MASTER_BUSY | QCA8K_MDIO_MASTER_EN |
	      QCA8K_MDIO_MASTER_WRITE | QCA8K_MDIO_MASTER_PHY_ADDR(phy) |
	      QCA8K_MDIO_MASTER_REG_ADDR(regnum) |
	      QCA8K_MDIO_MASTER_DATA(data);

721 722
	qca8k_split_addr(QCA8K_MDIO_MASTER_CTRL, &r1, &r2, &page);

723
	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
724

725
	ret = qca8k_set_page(bus, page);
726
	if (ret)
727 728
		goto exit;

729
	qca8k_mii_write32(bus, 0x10 | r2, r1, val);
730

731
	ret = qca8k_mdio_busy_wait(bus, QCA8K_MDIO_MASTER_CTRL,
732 733 734
				   QCA8K_MDIO_MASTER_BUSY);

exit:
735
	/* even if the busy_wait timeouts try to clear the MASTER_EN */
736
	qca8k_mii_write32(bus, 0x10 | r2, r1, 0);
737

738
	mutex_unlock(&bus->mdio_lock);
739 740

	return ret;
741 742 743
}

static int
744
qca8k_mdio_read(struct mii_bus *bus, int phy, int regnum)
745
{
746
	u16 r1, r2, page;
747
	u32 val;
748
	int ret;
749 750 751 752 753 754 755 756

	if (regnum >= QCA8K_MDIO_MASTER_MAX_REG)
		return -EINVAL;

	val = QCA8K_MDIO_MASTER_BUSY | QCA8K_MDIO_MASTER_EN |
	      QCA8K_MDIO_MASTER_READ | QCA8K_MDIO_MASTER_PHY_ADDR(phy) |
	      QCA8K_MDIO_MASTER_REG_ADDR(regnum);

757
	qca8k_split_addr(QCA8K_MDIO_MASTER_CTRL, &r1, &r2, &page);
758

759
	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
760

761
	ret = qca8k_set_page(bus, page);
762
	if (ret)
763
		goto exit;
764

765
	qca8k_mii_write32(bus, 0x10 | r2, r1, val);
766

767
	ret = qca8k_mdio_busy_wait(bus, QCA8K_MDIO_MASTER_CTRL,
768 769 770 771
				   QCA8K_MDIO_MASTER_BUSY);
	if (ret)
		goto exit;

772
	ret = qca8k_mii_read32(bus, 0x10 | r2, r1, &val);
773

774
exit:
775
	/* even if the busy_wait timeouts try to clear the MASTER_EN */
776
	qca8k_mii_write32(bus, 0x10 | r2, r1, 0);
777

778
	mutex_unlock(&bus->mdio_lock);
779

780 781
	if (ret >= 0)
		ret = val & QCA8K_MDIO_MASTER_DATA_MASK;
782

783
	return ret;
784 785
}

786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
static int
qca8k_internal_mdio_write(struct mii_bus *slave_bus, int phy, int regnum, u16 data)
{
	struct qca8k_priv *priv = slave_bus->priv;
	struct mii_bus *bus = priv->bus;

	return qca8k_mdio_write(bus, phy, regnum, data);
}

static int
qca8k_internal_mdio_read(struct mii_bus *slave_bus, int phy, int regnum)
{
	struct qca8k_priv *priv = slave_bus->priv;
	struct mii_bus *bus = priv->bus;

	return qca8k_mdio_read(bus, phy, regnum);
}

804 805 806 807 808
static int
qca8k_phy_write(struct dsa_switch *ds, int port, int regnum, u16 data)
{
	struct qca8k_priv *priv = ds->priv;

809 810 811 812 813 814 815 816
	/* Check if the legacy mapping should be used and the
	 * port is not correctly mapped to the right PHY in the
	 * devicetree
	 */
	if (priv->legacy_phy_port_mapping)
		port = qca8k_port_to_phy(port) % PHY_MAX_ADDR;

	return qca8k_mdio_write(priv->bus, port, regnum, data);
817 818 819 820 821 822 823 824
}

static int
qca8k_phy_read(struct dsa_switch *ds, int port, int regnum)
{
	struct qca8k_priv *priv = ds->priv;
	int ret;

825 826 827 828 829 830 831 832
	/* Check if the legacy mapping should be used and the
	 * port is not correctly mapped to the right PHY in the
	 * devicetree
	 */
	if (priv->legacy_phy_port_mapping)
		port = qca8k_port_to_phy(port) % PHY_MAX_ADDR;

	ret = qca8k_mdio_read(priv->bus, port, regnum);
833 834 835 836 837 838 839

	if (ret < 0)
		return 0xffff;

	return ret;
}

840 841 842 843 844 845 846 847 848 849 850 851 852
static int
qca8k_mdio_register(struct qca8k_priv *priv, struct device_node *mdio)
{
	struct dsa_switch *ds = priv->ds;
	struct mii_bus *bus;

	bus = devm_mdiobus_alloc(ds->dev);

	if (!bus)
		return -ENOMEM;

	bus->priv = (void *)priv;
	bus->name = "qca8k slave mii";
853 854
	bus->read = qca8k_internal_mdio_read;
	bus->write = qca8k_internal_mdio_write;
855 856 857 858 859 860 861 862 863 864 865
	snprintf(bus->id, MII_BUS_ID_SIZE, "qca8k-%d",
		 ds->index);

	bus->parent = ds->dev;
	bus->phy_mask = ~ds->phys_mii_mask;

	ds->slave_mii_bus = bus;

	return devm_of_mdiobus_register(priv->dev, bus, mdio);
}

866 867 868 869
static int
qca8k_setup_mdio_bus(struct qca8k_priv *priv)
{
	u32 internal_mdio_mask = 0, external_mdio_mask = 0, reg;
870 871
	struct device_node *ports, *port, *mdio;
	phy_interface_t mode;
872 873 874
	int err;

	ports = of_get_child_by_name(priv->dev->of_node, "ports");
875 876 877
	if (!ports)
		ports = of_get_child_by_name(priv->dev->of_node, "ethernet-ports");

878 879 880 881 882
	if (!ports)
		return -EINVAL;

	for_each_available_child_of_node(ports, port) {
		err = of_property_read_u32(port, "reg", &reg);
883 884 885
		if (err) {
			of_node_put(port);
			of_node_put(ports);
886
			return err;
887
		}
888 889 890 891

		if (!dsa_is_user_port(priv->ds, reg))
			continue;

892 893 894 895
		of_get_phy_mode(port, &mode);

		if (of_property_read_bool(port, "phy-handle") &&
		    mode != PHY_INTERFACE_MODE_INTERNAL)
896 897 898 899 900
			external_mdio_mask |= BIT(reg);
		else
			internal_mdio_mask |= BIT(reg);
	}

901
	of_node_put(ports);
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
	if (!external_mdio_mask && !internal_mdio_mask) {
		dev_err(priv->dev, "no PHYs are defined.\n");
		return -EINVAL;
	}

	/* The QCA8K_MDIO_MASTER_EN Bit, which grants access to PHYs through
	 * the MDIO_MASTER register also _disconnects_ the external MDC
	 * passthrough to the internal PHYs. It's not possible to use both
	 * configurations at the same time!
	 *
	 * Because this came up during the review process:
	 * If the external mdio-bus driver is capable magically disabling
	 * the QCA8K_MDIO_MASTER_EN and mutex/spin-locking out the qca8k's
	 * accessors for the time being, it would be possible to pull this
	 * off.
	 */
	if (!!external_mdio_mask && !!internal_mdio_mask) {
		dev_err(priv->dev, "either internal or external mdio bus configuration is supported.\n");
		return -EINVAL;
	}

	if (external_mdio_mask) {
		/* Make sure to disable the internal mdio bus in cases
		 * a dt-overlay and driver reload changed the configuration
		 */

928 929
		return regmap_clear_bits(priv->regmap, QCA8K_MDIO_MASTER_CTRL,
					 QCA8K_MDIO_MASTER_EN);
930 931
	}

932 933 934 935 936 937 938 939 940 941 942 943 944 945
	/* Check if the devicetree declare the port:phy mapping */
	mdio = of_get_child_by_name(priv->dev->of_node, "mdio");
	if (of_device_is_available(mdio)) {
		err = qca8k_mdio_register(priv, mdio);
		if (err)
			of_node_put(mdio);

		return err;
	}

	/* If a mapping can't be found the legacy mapping is used,
	 * using the qca8k_port_to_phy function
	 */
	priv->legacy_phy_port_mapping = true;
946 947
	priv->ops.phy_read = qca8k_phy_read;
	priv->ops.phy_write = qca8k_phy_write;
948

949 950 951
	return 0;
}

952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
static int
qca8k_setup_mac_pwr_sel(struct qca8k_priv *priv)
{
	u32 mask = 0;
	int ret = 0;

	/* SoC specific settings for ipq8064.
	 * If more device require this consider adding
	 * a dedicated binding.
	 */
	if (of_machine_is_compatible("qcom,ipq8064"))
		mask |= QCA8K_MAC_PWR_RGMII0_1_8V;

	/* SoC specific settings for ipq8065 */
	if (of_machine_is_compatible("qcom,ipq8065"))
		mask |= QCA8K_MAC_PWR_RGMII1_1_8V;

	if (mask) {
		ret = qca8k_rmw(priv, QCA8K_REG_MAC_PWR_SEL,
				QCA8K_MAC_PWR_RGMII0_1_8V |
				QCA8K_MAC_PWR_RGMII1_1_8V,
				mask);
	}

	return ret;
}

979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
static int qca8k_find_cpu_port(struct dsa_switch *ds)
{
	struct qca8k_priv *priv = ds->priv;

	/* Find the connected cpu port. Valid port are 0 or 6 */
	if (dsa_is_cpu_port(ds, 0))
		return 0;

	dev_dbg(priv->dev, "port 0 is not the CPU port. Checking port 6");

	if (dsa_is_cpu_port(ds, 6))
		return 6;

	return -EINVAL;
}

995 996 997 998
static int
qca8k_setup_of_pws_reg(struct qca8k_priv *priv)
{
	struct device_node *node = priv->dev->of_node;
999
	const struct qca8k_match_data *data;
1000 1001 1002 1003 1004 1005 1006 1007
	u32 val = 0;
	int ret;

	/* QCA8327 require to set to the correct mode.
	 * His bigger brother QCA8328 have the 172 pin layout.
	 * Should be applied by default but we set this just to make sure.
	 */
	if (priv->switch_id == QCA8K_ID_QCA8327) {
1008 1009 1010 1011 1012 1013
		data = of_device_get_match_data(priv->dev);

		/* Set the correct package of 148 pin for QCA8327 */
		if (data->reduced_package)
			val |= QCA8327_PWS_PACKAGE148_EN;

1014
		ret = qca8k_rmw(priv, QCA8K_REG_PWS, QCA8327_PWS_PACKAGE148_EN,
1015
				val);
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
		if (ret)
			return ret;
	}

	if (of_property_read_bool(node, "qca,ignore-power-on-sel"))
		val |= QCA8K_PWS_POWER_ON_SEL;

	if (of_property_read_bool(node, "qca,led-open-drain")) {
		if (!(val & QCA8K_PWS_POWER_ON_SEL)) {
			dev_err(priv->dev, "qca,led-open-drain require qca,ignore-power-on-sel to be set.");
			return -EINVAL;
		}

		val |= QCA8K_PWS_LED_OPEN_EN_CSR;
	}

	return qca8k_rmw(priv, QCA8K_REG_PWS,
			QCA8K_PWS_LED_OPEN_EN_CSR | QCA8K_PWS_POWER_ON_SEL,
			val);
}

1037 1038 1039
static int
qca8k_parse_port_config(struct qca8k_priv *priv)
{
1040
	int port, cpu_port_index = -1, ret;
1041 1042 1043
	struct device_node *port_dn;
	phy_interface_t mode;
	struct dsa_port *dp;
1044
	u32 delay;
1045 1046

	/* We have 2 CPU port. Check them */
1047
	for (port = 0; port < QCA8K_NUM_PORTS; port++) {
1048 1049 1050 1051 1052 1053
		/* Skip every other port */
		if (port != 0 && port != 6)
			continue;

		dp = dsa_to_port(priv->ds, port);
		port_dn = dp->dn;
1054
		cpu_port_index++;
1055 1056 1057 1058 1059 1060 1061 1062

		if (!of_device_is_available(port_dn))
			continue;

		ret = of_get_phy_mode(port_dn, &mode);
		if (ret)
			continue;

1063 1064 1065 1066 1067
		switch (mode) {
		case PHY_INTERFACE_MODE_RGMII:
		case PHY_INTERFACE_MODE_RGMII_ID:
		case PHY_INTERFACE_MODE_RGMII_TXID:
		case PHY_INTERFACE_MODE_RGMII_RXID:
1068
		case PHY_INTERFACE_MODE_SGMII:
1069 1070 1071 1072 1073 1074 1075 1076 1077
			delay = 0;

			if (!of_property_read_u32(port_dn, "tx-internal-delay-ps", &delay))
				/* Switch regs accept value in ns, convert ps to ns */
				delay = delay / 1000;
			else if (mode == PHY_INTERFACE_MODE_RGMII_ID ||
				 mode == PHY_INTERFACE_MODE_RGMII_TXID)
				delay = 1;

1078
			if (!FIELD_FIT(QCA8K_PORT_PAD_RGMII_TX_DELAY_MASK, delay)) {
1079 1080 1081 1082
				dev_err(priv->dev, "rgmii tx delay is limited to a max value of 3ns, setting to the max value");
				delay = 3;
			}

1083
			priv->ports_config.rgmii_tx_delay[cpu_port_index] = delay;
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093

			delay = 0;

			if (!of_property_read_u32(port_dn, "rx-internal-delay-ps", &delay))
				/* Switch regs accept value in ns, convert ps to ns */
				delay = delay / 1000;
			else if (mode == PHY_INTERFACE_MODE_RGMII_ID ||
				 mode == PHY_INTERFACE_MODE_RGMII_RXID)
				delay = 2;

1094
			if (!FIELD_FIT(QCA8K_PORT_PAD_RGMII_RX_DELAY_MASK, delay)) {
1095 1096 1097 1098
				dev_err(priv->dev, "rgmii rx delay is limited to a max value of 3ns, setting to the max value");
				delay = 3;
			}

1099
			priv->ports_config.rgmii_rx_delay[cpu_port_index] = delay;
1100

1101 1102 1103 1104 1105 1106 1107
			/* Skip sgmii parsing for rgmii* mode */
			if (mode == PHY_INTERFACE_MODE_RGMII ||
			    mode == PHY_INTERFACE_MODE_RGMII_ID ||
			    mode == PHY_INTERFACE_MODE_RGMII_TXID ||
			    mode == PHY_INTERFACE_MODE_RGMII_RXID)
				break;

1108
			if (of_property_read_bool(port_dn, "qca,sgmii-txclk-falling-edge"))
1109
				priv->ports_config.sgmii_tx_clk_falling_edge = true;
1110 1111

			if (of_property_read_bool(port_dn, "qca,sgmii-rxclk-falling-edge"))
1112
				priv->ports_config.sgmii_rx_clk_falling_edge = true;
1113

1114
			if (of_property_read_bool(port_dn, "qca,sgmii-enable-pll")) {
1115
				priv->ports_config.sgmii_enable_pll = true;
1116 1117 1118

				if (priv->switch_id == QCA8K_ID_QCA8327) {
					dev_err(priv->dev, "SGMII PLL should NOT be enabled for qca8327. Aborting enabling");
1119
					priv->ports_config.sgmii_enable_pll = false;
1120 1121 1122 1123 1124 1125
				}

				if (priv->switch_revision < 2)
					dev_warn(priv->dev, "SGMII PLL should NOT be enabled for qca8337 with revision 2 or more.");
			}

1126 1127 1128
			break;
		default:
			continue;
1129 1130 1131 1132 1133 1134
		}
	}

	return 0;
}

1135 1136 1137 1138
static int
qca8k_setup(struct dsa_switch *ds)
{
	struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
1139
	int cpu_port, ret, i;
1140
	u32 mask;
1141

1142 1143 1144 1145
	cpu_port = qca8k_find_cpu_port(ds);
	if (cpu_port < 0) {
		dev_err(priv->dev, "No cpu port configured in both cpu port0 and port6");
		return cpu_port;
1146 1147
	}

1148 1149 1150 1151 1152
	/* Parse CPU port config to be later used in phy_link mac_config */
	ret = qca8k_parse_port_config(priv);
	if (ret)
		return ret;

1153 1154 1155 1156
	ret = qca8k_setup_mdio_bus(priv);
	if (ret)
		return ret;

1157 1158 1159 1160
	ret = qca8k_setup_of_pws_reg(priv);
	if (ret)
		return ret;

1161 1162 1163 1164
	ret = qca8k_setup_mac_pwr_sel(priv);
	if (ret)
		return ret;

1165
	/* Make sure MAC06 is disabled */
1166 1167
	ret = regmap_clear_bits(priv->regmap, QCA8K_REG_PORT0_PAD_CTRL,
				QCA8K_PORT0_PAD_MAC06_EXCHANGE_EN);
1168 1169 1170 1171 1172
	if (ret) {
		dev_err(priv->dev, "failed disabling MAC06 exchange");
		return ret;
	}

1173
	/* Enable CPU Port */
1174 1175
	ret = regmap_set_bits(priv->regmap, QCA8K_REG_GLOBAL_FW_CTRL0,
			      QCA8K_GLOBAL_FW_CTRL0_CPU_PORT_EN);
1176 1177 1178 1179
	if (ret) {
		dev_err(priv->dev, "failed enabling CPU port");
		return ret;
	}
1180 1181

	/* Enable MIB counters */
1182 1183 1184
	ret = qca8k_mib_init(priv);
	if (ret)
		dev_warn(priv->dev, "mib init failed");
1185

1186
	/* Initial setup of all ports */
1187
	for (i = 0; i < QCA8K_NUM_PORTS; i++) {
1188
		/* Disable forwarding by default on all ports */
1189 1190 1191 1192
		ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(i),
				QCA8K_PORT_LOOKUP_MEMBER, 0);
		if (ret)
			return ret;
1193

1194 1195 1196
		/* Enable QCA header mode on all cpu ports */
		if (dsa_is_cpu_port(ds, i)) {
			ret = qca8k_write(priv, QCA8K_REG_PORT_HDR_CTRL(i),
1197 1198
					  FIELD_PREP(QCA8K_PORT_HDR_CTRL_TX_MASK, QCA8K_PORT_HDR_CTRL_ALL) |
					  FIELD_PREP(QCA8K_PORT_HDR_CTRL_RX_MASK, QCA8K_PORT_HDR_CTRL_ALL));
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
			if (ret) {
				dev_err(priv->dev, "failed enabling QCA header mode");
				return ret;
			}
		}

		/* Disable MAC by default on all user ports */
		if (dsa_is_user_port(ds, i))
			qca8k_port_set_status(priv, i, 0);
	}
1209

1210 1211 1212 1213
	/* Forward all unknown frames to CPU port for Linux processing
	 * Notice that in multi-cpu config only one port should be set
	 * for igmp, unknown, multicast and broadcast packet
	 */
1214
	ret = qca8k_write(priv, QCA8K_REG_GLOBAL_FW_CTRL1,
1215 1216 1217 1218
			  FIELD_PREP(QCA8K_GLOBAL_FW_CTRL1_IGMP_DP_MASK, BIT(cpu_port)) |
			  FIELD_PREP(QCA8K_GLOBAL_FW_CTRL1_BC_DP_MASK, BIT(cpu_port)) |
			  FIELD_PREP(QCA8K_GLOBAL_FW_CTRL1_MC_DP_MASK, BIT(cpu_port)) |
			  FIELD_PREP(QCA8K_GLOBAL_FW_CTRL1_UC_DP_MASK, BIT(cpu_port)));
1219 1220
	if (ret)
		return ret;
1221

1222 1223 1224
	/* Setup connection between CPU port & user ports
	 * Configure specific switch configuration for ports
	 */
1225
	for (i = 0; i < QCA8K_NUM_PORTS; i++) {
1226 1227
		/* CPU port gets connected to all user ports of the switch */
		if (dsa_is_cpu_port(ds, i)) {
1228
			ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(i),
1229 1230 1231
					QCA8K_PORT_LOOKUP_MEMBER, dsa_user_ports(ds));
			if (ret)
				return ret;
1232 1233
		}

1234
		/* Individual user ports get connected to CPU port only */
1235
		if (dsa_is_user_port(ds, i)) {
1236 1237
			ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(i),
					QCA8K_PORT_LOOKUP_MEMBER,
1238
					BIT(cpu_port));
1239 1240
			if (ret)
				return ret;
1241 1242

			/* Enable ARP Auto-learning by default */
1243 1244
			ret = regmap_set_bits(priv->regmap, QCA8K_PORT_LOOKUP_CTRL(i),
					      QCA8K_PORT_LOOKUP_LEARN);
1245 1246
			if (ret)
				return ret;
1247 1248 1249 1250

			/* For port based vlans to work we need to set the
			 * default egress vid
			 */
1251
			ret = qca8k_rmw(priv, QCA8K_EGRESS_VLAN(i),
1252 1253
					QCA8K_EGREES_VLAN_PORT_MASK(i),
					QCA8K_EGREES_VLAN_PORT(i, QCA8K_PORT_VID_DEF));
1254 1255 1256
			if (ret)
				return ret;

1257 1258 1259 1260 1261
			ret = qca8k_write(priv, QCA8K_REG_PORT_VLAN_CTRL0(i),
					  QCA8K_PORT_VLAN_CVID(QCA8K_PORT_VID_DEF) |
					  QCA8K_PORT_VLAN_SVID(QCA8K_PORT_VID_DEF));
			if (ret)
				return ret;
1262 1263
		}

1264 1265 1266 1267 1268 1269 1270
		/* The port 5 of the qca8337 have some problem in flood condition. The
		 * original legacy driver had some specific buffer and priority settings
		 * for the different port suggested by the QCA switch team. Add this
		 * missing settings to improve switch stability under load condition.
		 * This problem is limited to qca8337 and other qca8k switch are not affected.
		 */
		if (priv->switch_id == QCA8K_ID_QCA8337) {
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
			switch (i) {
			/* The 2 CPU port and port 5 requires some different
			 * priority than any other ports.
			 */
			case 0:
			case 5:
			case 6:
				mask = QCA8K_PORT_HOL_CTRL0_EG_PRI0(0x3) |
					QCA8K_PORT_HOL_CTRL0_EG_PRI1(0x4) |
					QCA8K_PORT_HOL_CTRL0_EG_PRI2(0x4) |
					QCA8K_PORT_HOL_CTRL0_EG_PRI3(0x4) |
					QCA8K_PORT_HOL_CTRL0_EG_PRI4(0x6) |
					QCA8K_PORT_HOL_CTRL0_EG_PRI5(0x8) |
					QCA8K_PORT_HOL_CTRL0_EG_PORT(0x1e);
				break;
			default:
				mask = QCA8K_PORT_HOL_CTRL0_EG_PRI0(0x3) |
					QCA8K_PORT_HOL_CTRL0_EG_PRI1(0x4) |
					QCA8K_PORT_HOL_CTRL0_EG_PRI2(0x6) |
					QCA8K_PORT_HOL_CTRL0_EG_PRI3(0x8) |
					QCA8K_PORT_HOL_CTRL0_EG_PORT(0x19);
			}
			qca8k_write(priv, QCA8K_REG_PORT_HOL_CTRL0(i), mask);

			mask = QCA8K_PORT_HOL_CTRL1_ING(0x6) |
			QCA8K_PORT_HOL_CTRL1_EG_PRI_BUF_EN |
			QCA8K_PORT_HOL_CTRL1_EG_PORT_BUF_EN |
			QCA8K_PORT_HOL_CTRL1_WRED_EN;
			qca8k_rmw(priv, QCA8K_REG_PORT_HOL_CTRL1(i),
1300
				  QCA8K_PORT_HOL_CTRL1_ING_BUF_MASK |
1301 1302 1303 1304 1305
				  QCA8K_PORT_HOL_CTRL1_EG_PRI_BUF_EN |
				  QCA8K_PORT_HOL_CTRL1_EG_PORT_BUF_EN |
				  QCA8K_PORT_HOL_CTRL1_WRED_EN,
				  mask);
		}
1306 1307 1308 1309 1310 1311

		/* Set initial MTU for every port.
		 * We have only have a general MTU setting. So track
		 * every port and set the max across all port.
		 */
		priv->port_mtu[i] = ETH_FRAME_LEN + ETH_FCS_LEN;
1312 1313
	}

1314 1315 1316 1317 1318
	/* Special GLOBAL_FC_THRESH value are needed for ar8327 switch */
	if (priv->switch_id == QCA8K_ID_QCA8327) {
		mask = QCA8K_GLOBAL_FC_GOL_XON_THRES(288) |
		       QCA8K_GLOBAL_FC_GOL_XOFF_THRES(496);
		qca8k_rmw(priv, QCA8K_REG_GLOBAL_FC_THRESH,
1319 1320
			  QCA8K_GLOBAL_FC_GOL_XON_THRES_MASK |
			  QCA8K_GLOBAL_FC_GOL_XOFF_THRES_MASK,
1321 1322 1323
			  mask);
	}

1324
	/* Setup our port MTUs to match power on defaults */
1325 1326 1327
	ret = qca8k_write(priv, QCA8K_MAX_FRAME_SIZE, ETH_FRAME_LEN + ETH_FCS_LEN);
	if (ret)
		dev_warn(priv->dev, "failed setting MTU settings");
1328

1329 1330 1331
	/* Flush the FDB table */
	qca8k_fdb_flush(priv);

1332 1333 1334
	/* We don't have interrupts for link changes, so we need to poll */
	ds->pcs_poll = true;

1335 1336 1337 1338
	/* Set min a max ageing value supported */
	ds->ageing_time_min = 7000;
	ds->ageing_time_max = 458745000;

A
Ansuel Smith 已提交
1339 1340 1341
	/* Set max number of LAGs supported */
	ds->num_lag_ids = QCA8K_NUM_LAGS;

1342 1343 1344
	return 0;
}

1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
static void
qca8k_mac_config_setup_internal_delay(struct qca8k_priv *priv, int cpu_port_index,
				      u32 reg)
{
	u32 delay, val = 0;
	int ret;

	/* Delay can be declared in 3 different way.
	 * Mode to rgmii and internal-delay standard binding defined
	 * rgmii-id or rgmii-tx/rx phy mode set.
	 * The parse logic set a delay different than 0 only when one
	 * of the 3 different way is used. In all other case delay is
	 * not enabled. With ID or TX/RXID delay is enabled and set
	 * to the default and recommended value.
	 */
1360 1361
	if (priv->ports_config.rgmii_tx_delay[cpu_port_index]) {
		delay = priv->ports_config.rgmii_tx_delay[cpu_port_index];
1362 1363 1364 1365 1366

		val |= QCA8K_PORT_PAD_RGMII_TX_DELAY(delay) |
			QCA8K_PORT_PAD_RGMII_TX_DELAY_EN;
	}

1367 1368
	if (priv->ports_config.rgmii_rx_delay[cpu_port_index]) {
		delay = priv->ports_config.rgmii_rx_delay[cpu_port_index];
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385

		val |= QCA8K_PORT_PAD_RGMII_RX_DELAY(delay) |
			QCA8K_PORT_PAD_RGMII_RX_DELAY_EN;
	}

	/* Set RGMII delay based on the selected values */
	ret = qca8k_rmw(priv, reg,
			QCA8K_PORT_PAD_RGMII_TX_DELAY_MASK |
			QCA8K_PORT_PAD_RGMII_RX_DELAY_MASK |
			QCA8K_PORT_PAD_RGMII_TX_DELAY_EN |
			QCA8K_PORT_PAD_RGMII_RX_DELAY_EN,
			val);
	if (ret)
		dev_err(priv->dev, "Failed to set internal delay for CPU port%d",
			cpu_port_index == QCA8K_CPU_PORT0 ? 0 : 6);
}

1386
static void
1387 1388
qca8k_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
			 const struct phylink_link_state *state)
1389 1390
{
	struct qca8k_priv *priv = ds->priv;
1391
	int cpu_port_index, ret;
1392
	u32 reg, val;
1393

1394 1395 1396 1397
	switch (port) {
	case 0: /* 1st CPU port */
		if (state->interface != PHY_INTERFACE_MODE_RGMII &&
		    state->interface != PHY_INTERFACE_MODE_RGMII_ID &&
1398 1399
		    state->interface != PHY_INTERFACE_MODE_RGMII_TXID &&
		    state->interface != PHY_INTERFACE_MODE_RGMII_RXID &&
1400 1401 1402 1403
		    state->interface != PHY_INTERFACE_MODE_SGMII)
			return;

		reg = QCA8K_REG_PORT0_PAD_CTRL;
1404
		cpu_port_index = QCA8K_CPU_PORT0;
1405 1406 1407 1408 1409 1410 1411
		break;
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
		/* Internal PHY, nothing to do */
1412
		return;
1413 1414 1415
	case 6: /* 2nd CPU port / external PHY */
		if (state->interface != PHY_INTERFACE_MODE_RGMII &&
		    state->interface != PHY_INTERFACE_MODE_RGMII_ID &&
1416 1417
		    state->interface != PHY_INTERFACE_MODE_RGMII_TXID &&
		    state->interface != PHY_INTERFACE_MODE_RGMII_RXID &&
1418 1419 1420
		    state->interface != PHY_INTERFACE_MODE_SGMII &&
		    state->interface != PHY_INTERFACE_MODE_1000BASEX)
			return;
1421

1422
		reg = QCA8K_REG_PORT6_PAD_CTRL;
1423
		cpu_port_index = QCA8K_CPU_PORT6;
1424
		break;
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
	default:
		dev_err(ds->dev, "%s: unsupported port: %i\n", __func__, port);
		return;
	}

	if (port != 6 && phylink_autoneg_inband(mode)) {
		dev_err(ds->dev, "%s: in-band negotiation unsupported\n",
			__func__);
		return;
	}

	switch (state->interface) {
	case PHY_INTERFACE_MODE_RGMII:
	case PHY_INTERFACE_MODE_RGMII_ID:
1439 1440
	case PHY_INTERFACE_MODE_RGMII_TXID:
	case PHY_INTERFACE_MODE_RGMII_RXID:
1441
		qca8k_write(priv, reg, QCA8K_PORT_PAD_RGMII_EN);
1442

1443 1444
		/* Configure rgmii delay */
		qca8k_mac_config_setup_internal_delay(priv, cpu_port_index, reg);
1445 1446 1447 1448

		/* QCA8337 requires to set rgmii rx delay for all ports.
		 * This is enabled through PORT5_PAD_CTRL for all ports,
		 * rather than individual port registers.
1449
		 */
1450 1451 1452
		if (priv->switch_id == QCA8K_ID_QCA8337)
			qca8k_write(priv, QCA8K_REG_PORT5_PAD_CTRL,
				    QCA8K_PORT_PAD_RGMII_RX_DELAY_EN);
1453 1454 1455 1456 1457
		break;
	case PHY_INTERFACE_MODE_SGMII:
	case PHY_INTERFACE_MODE_1000BASEX:
		/* Enable SGMII on the port */
		qca8k_write(priv, reg, QCA8K_PORT_PAD_SGMII_EN);
1458 1459

		/* Enable/disable SerDes auto-negotiation as necessary */
1460 1461 1462
		ret = qca8k_read(priv, QCA8K_REG_PWS, &val);
		if (ret)
			return;
1463 1464 1465 1466 1467 1468 1469
		if (phylink_autoneg_inband(mode))
			val &= ~QCA8K_PWS_SERDES_AEN_DIS;
		else
			val |= QCA8K_PWS_SERDES_AEN_DIS;
		qca8k_write(priv, QCA8K_REG_PWS, val);

		/* Configure the SGMII parameters */
1470 1471 1472
		ret = qca8k_read(priv, QCA8K_REG_SGMII_CTRL, &val);
		if (ret)
			return;
1473

1474 1475
		val |= QCA8K_SGMII_EN_SD;

1476
		if (priv->ports_config.sgmii_enable_pll)
1477 1478
			val |= QCA8K_SGMII_EN_PLL | QCA8K_SGMII_EN_RX |
			       QCA8K_SGMII_EN_TX;
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492

		if (dsa_is_cpu_port(ds, port)) {
			/* CPU port, we're talking to the CPU MAC, be a PHY */
			val &= ~QCA8K_SGMII_MODE_CTRL_MASK;
			val |= QCA8K_SGMII_MODE_CTRL_PHY;
		} else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
			val &= ~QCA8K_SGMII_MODE_CTRL_MASK;
			val |= QCA8K_SGMII_MODE_CTRL_MAC;
		} else if (state->interface == PHY_INTERFACE_MODE_1000BASEX) {
			val &= ~QCA8K_SGMII_MODE_CTRL_MASK;
			val |= QCA8K_SGMII_MODE_CTRL_BASEX;
		}

		qca8k_write(priv, QCA8K_REG_SGMII_CTRL, val);
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503

		/* For qca8327/qca8328/qca8334/qca8338 sgmii is unique and
		 * falling edge is set writing in the PORT0 PAD reg
		 */
		if (priv->switch_id == QCA8K_ID_QCA8327 ||
		    priv->switch_id == QCA8K_ID_QCA8337)
			reg = QCA8K_REG_PORT0_PAD_CTRL;

		val = 0;

		/* SGMII Clock phase configuration */
1504
		if (priv->ports_config.sgmii_rx_clk_falling_edge)
1505 1506
			val |= QCA8K_PORT0_PAD_SGMII_RXCLK_FALLING_EDGE;

1507
		if (priv->ports_config.sgmii_tx_clk_falling_edge)
1508 1509 1510 1511 1512 1513 1514
			val |= QCA8K_PORT0_PAD_SGMII_TXCLK_FALLING_EDGE;

		if (val)
			ret = qca8k_rmw(priv, reg,
					QCA8K_PORT0_PAD_SGMII_RXCLK_FALLING_EDGE |
					QCA8K_PORT0_PAD_SGMII_TXCLK_FALLING_EDGE,
					val);
1515 1516 1517 1518 1519 1520 1521

		/* From original code is reported port instability as SGMII also
		 * require delay set. Apply advised values here or take them from DT.
		 */
		if (state->interface == PHY_INTERFACE_MODE_SGMII)
			qca8k_mac_config_setup_internal_delay(priv, cpu_port_index, reg);

1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
		break;
	default:
		dev_err(ds->dev, "xMII mode %s not supported for port %d\n",
			phy_modes(state->interface), port);
		return;
	}
}

static void
qca8k_phylink_validate(struct dsa_switch *ds, int port,
		       unsigned long *supported,
		       struct phylink_link_state *state)
{
	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };

	switch (port) {
	case 0: /* 1st CPU port */
		if (state->interface != PHY_INTERFACE_MODE_NA &&
		    state->interface != PHY_INTERFACE_MODE_RGMII &&
		    state->interface != PHY_INTERFACE_MODE_RGMII_ID &&
1542 1543
		    state->interface != PHY_INTERFACE_MODE_RGMII_TXID &&
		    state->interface != PHY_INTERFACE_MODE_RGMII_RXID &&
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
		    state->interface != PHY_INTERFACE_MODE_SGMII)
			goto unsupported;
		break;
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
		/* Internal PHY */
		if (state->interface != PHY_INTERFACE_MODE_NA &&
1554 1555
		    state->interface != PHY_INTERFACE_MODE_GMII &&
		    state->interface != PHY_INTERFACE_MODE_INTERNAL)
1556 1557 1558 1559 1560 1561
			goto unsupported;
		break;
	case 6: /* 2nd CPU port / external PHY */
		if (state->interface != PHY_INTERFACE_MODE_NA &&
		    state->interface != PHY_INTERFACE_MODE_RGMII &&
		    state->interface != PHY_INTERFACE_MODE_RGMII_ID &&
1562 1563
		    state->interface != PHY_INTERFACE_MODE_RGMII_TXID &&
		    state->interface != PHY_INTERFACE_MODE_RGMII_RXID &&
1564 1565 1566
		    state->interface != PHY_INTERFACE_MODE_SGMII &&
		    state->interface != PHY_INTERFACE_MODE_1000BASEX)
			goto unsupported;
1567 1568
		break;
	default:
1569 1570
unsupported:
		linkmode_zero(supported);
1571 1572 1573
		return;
	}

1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
	phylink_set_port_modes(mask);
	phylink_set(mask, Autoneg);

	phylink_set(mask, 1000baseT_Full);
	phylink_set(mask, 10baseT_Half);
	phylink_set(mask, 10baseT_Full);
	phylink_set(mask, 100baseT_Half);
	phylink_set(mask, 100baseT_Full);

	if (state->interface == PHY_INTERFACE_MODE_1000BASEX)
		phylink_set(mask, 1000baseX_Full);

	phylink_set(mask, Pause);
	phylink_set(mask, Asym_Pause);

	linkmode_and(supported, supported, mask);
	linkmode_and(state->advertising, state->advertising, mask);
}

static int
qca8k_phylink_mac_link_state(struct dsa_switch *ds, int port,
			     struct phylink_link_state *state)
{
	struct qca8k_priv *priv = ds->priv;
	u32 reg;
1599
	int ret;
1600

1601 1602 1603
	ret = qca8k_read(priv, QCA8K_REG_PORT_STATUS(port), &reg);
	if (ret < 0)
		return ret;
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633

	state->link = !!(reg & QCA8K_PORT_STATUS_LINK_UP);
	state->an_complete = state->link;
	state->an_enabled = !!(reg & QCA8K_PORT_STATUS_LINK_AUTO);
	state->duplex = (reg & QCA8K_PORT_STATUS_DUPLEX) ? DUPLEX_FULL :
							   DUPLEX_HALF;

	switch (reg & QCA8K_PORT_STATUS_SPEED) {
	case QCA8K_PORT_STATUS_SPEED_10:
		state->speed = SPEED_10;
		break;
	case QCA8K_PORT_STATUS_SPEED_100:
		state->speed = SPEED_100;
		break;
	case QCA8K_PORT_STATUS_SPEED_1000:
		state->speed = SPEED_1000;
		break;
	default:
		state->speed = SPEED_UNKNOWN;
		break;
	}

	state->pause = MLO_PAUSE_NONE;
	if (reg & QCA8K_PORT_STATUS_RXFLOW)
		state->pause |= MLO_PAUSE_RX;
	if (reg & QCA8K_PORT_STATUS_TXFLOW)
		state->pause |= MLO_PAUSE_TX;

	return 1;
}
1634

1635 1636 1637 1638 1639
static void
qca8k_phylink_mac_link_down(struct dsa_switch *ds, int port, unsigned int mode,
			    phy_interface_t interface)
{
	struct qca8k_priv *priv = ds->priv;
1640 1641

	qca8k_port_set_status(priv, port, 0);
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
}

static void
qca8k_phylink_mac_link_up(struct dsa_switch *ds, int port, unsigned int mode,
			  phy_interface_t interface, struct phy_device *phydev,
			  int speed, int duplex, bool tx_pause, bool rx_pause)
{
	struct qca8k_priv *priv = ds->priv;
	u32 reg;

	if (phylink_autoneg_inband(mode)) {
		reg = QCA8K_PORT_STATUS_LINK_AUTO;
	} else {
		switch (speed) {
		case SPEED_10:
			reg = QCA8K_PORT_STATUS_SPEED_10;
			break;
		case SPEED_100:
			reg = QCA8K_PORT_STATUS_SPEED_100;
			break;
		case SPEED_1000:
			reg = QCA8K_PORT_STATUS_SPEED_1000;
			break;
		default:
			reg = QCA8K_PORT_STATUS_LINK_AUTO;
			break;
		}

		if (duplex == DUPLEX_FULL)
			reg |= QCA8K_PORT_STATUS_DUPLEX;

		if (rx_pause || dsa_is_cpu_port(ds, port))
			reg |= QCA8K_PORT_STATUS_RXFLOW;

		if (tx_pause || dsa_is_cpu_port(ds, port))
			reg |= QCA8K_PORT_STATUS_TXFLOW;
	}

	reg |= QCA8K_PORT_STATUS_TXMAC | QCA8K_PORT_STATUS_RXMAC;

1682 1683 1684
	qca8k_write(priv, QCA8K_REG_PORT_STATUS(port), reg);
}

1685
static void
1686
qca8k_get_strings(struct dsa_switch *ds, int port, u32 stringset, uint8_t *data)
1687
{
1688 1689
	const struct qca8k_match_data *match_data;
	struct qca8k_priv *priv = ds->priv;
1690 1691
	int i;

1692 1693 1694
	if (stringset != ETH_SS_STATS)
		return;

1695 1696 1697
	match_data = of_device_get_match_data(priv->dev);

	for (i = 0; i < match_data->mib_count; i++)
1698 1699 1700 1701 1702 1703 1704 1705 1706
		strncpy(data + i * ETH_GSTRING_LEN, ar8327_mib[i].name,
			ETH_GSTRING_LEN);
}

static void
qca8k_get_ethtool_stats(struct dsa_switch *ds, int port,
			uint64_t *data)
{
	struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
1707
	const struct qca8k_match_data *match_data;
1708
	const struct qca8k_mib_desc *mib;
1709
	u32 reg, i, val;
1710
	u32 hi = 0;
1711
	int ret;
1712

1713 1714 1715
	match_data = of_device_get_match_data(priv->dev);

	for (i = 0; i < match_data->mib_count; i++) {
1716 1717 1718
		mib = &ar8327_mib[i];
		reg = QCA8K_PORT_MIB_COUNTER(port) + mib->offset;

1719 1720
		ret = qca8k_read(priv, reg, &val);
		if (ret < 0)
1721 1722
			continue;

1723
		if (mib->size == 2) {
1724
			ret = qca8k_read(priv, reg + 4, &hi);
1725
			if (ret < 0)
1726
				continue;
1727
		}
1728 1729 1730

		data[i] = val;
		if (mib->size == 2)
1731
			data[i] |= (u64)hi << 32;
1732 1733 1734 1735
	}
}

static int
1736
qca8k_get_sset_count(struct dsa_switch *ds, int port, int sset)
1737
{
1738 1739 1740
	const struct qca8k_match_data *match_data;
	struct qca8k_priv *priv = ds->priv;

1741 1742 1743
	if (sset != ETH_SS_STATS)
		return 0;

1744 1745 1746
	match_data = of_device_get_match_data(priv->dev);

	return match_data->mib_count;
1747 1748
}

1749
static int
V
Vivien Didelot 已提交
1750
qca8k_set_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *eee)
1751 1752 1753 1754
{
	struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
	u32 lpi_en = QCA8K_REG_EEE_CTRL_LPI_EN(port);
	u32 reg;
1755
	int ret;
1756 1757

	mutex_lock(&priv->reg_mutex);
1758
	ret = qca8k_read(priv, QCA8K_REG_EEE_CTRL, &reg);
1759
	if (ret < 0)
1760 1761
		goto exit;

1762
	if (eee->eee_enabled)
1763 1764 1765
		reg |= lpi_en;
	else
		reg &= ~lpi_en;
1766
	ret = qca8k_write(priv, QCA8K_REG_EEE_CTRL, reg);
1767

1768 1769 1770
exit:
	mutex_unlock(&priv->reg_mutex);
	return ret;
1771 1772 1773
}

static int
V
Vivien Didelot 已提交
1774
qca8k_get_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
1775
{
1776 1777
	/* Nothing to do on the port's MAC */
	return 0;
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809
}

static void
qca8k_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
{
	struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
	u32 stp_state;

	switch (state) {
	case BR_STATE_DISABLED:
		stp_state = QCA8K_PORT_LOOKUP_STATE_DISABLED;
		break;
	case BR_STATE_BLOCKING:
		stp_state = QCA8K_PORT_LOOKUP_STATE_BLOCKING;
		break;
	case BR_STATE_LISTENING:
		stp_state = QCA8K_PORT_LOOKUP_STATE_LISTENING;
		break;
	case BR_STATE_LEARNING:
		stp_state = QCA8K_PORT_LOOKUP_STATE_LEARNING;
		break;
	case BR_STATE_FORWARDING:
	default:
		stp_state = QCA8K_PORT_LOOKUP_STATE_FORWARD;
		break;
	}

	qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
		  QCA8K_PORT_LOOKUP_STATE_MASK, stp_state);
}

static int
1810
qca8k_port_bridge_join(struct dsa_switch *ds, int port, struct net_device *br)
1811 1812
{
	struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
1813
	int port_mask, cpu_port;
1814
	int i, ret;
1815

1816 1817 1818
	cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
	port_mask = BIT(cpu_port);

1819 1820 1821
	for (i = 0; i < QCA8K_NUM_PORTS; i++) {
		if (dsa_is_cpu_port(ds, i))
			continue;
V
Vivien Didelot 已提交
1822
		if (dsa_to_port(ds, i)->bridge_dev != br)
1823 1824 1825 1826
			continue;
		/* Add this port to the portvlan mask of the other ports
		 * in the bridge
		 */
1827 1828 1829
		ret = regmap_set_bits(priv->regmap,
				      QCA8K_PORT_LOOKUP_CTRL(i),
				      BIT(port));
1830 1831
		if (ret)
			return ret;
1832 1833 1834
		if (i != port)
			port_mask |= BIT(i);
	}
1835

1836
	/* Add all other ports to this ports portvlan mask */
1837 1838
	ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
			QCA8K_PORT_LOOKUP_MEMBER, port_mask);
1839

1840
	return ret;
1841 1842 1843
}

static void
1844
qca8k_port_bridge_leave(struct dsa_switch *ds, int port, struct net_device *br)
1845 1846
{
	struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
1847 1848 1849
	int cpu_port, i;

	cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
1850

1851 1852 1853
	for (i = 0; i < QCA8K_NUM_PORTS; i++) {
		if (dsa_is_cpu_port(ds, i))
			continue;
V
Vivien Didelot 已提交
1854
		if (dsa_to_port(ds, i)->bridge_dev != br)
1855 1856 1857 1858
			continue;
		/* Remove this port to the portvlan mask of the other ports
		 * in the bridge
		 */
1859 1860 1861
		regmap_clear_bits(priv->regmap,
				  QCA8K_PORT_LOOKUP_CTRL(i),
				  BIT(port));
1862
	}
1863

1864 1865 1866 1867
	/* Set the cpu port to be the only one in the portvlan mask of
	 * this port
	 */
	qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
1868
		  QCA8K_PORT_LOOKUP_MEMBER, BIT(cpu_port));
1869 1870
}

1871 1872 1873 1874 1875 1876 1877 1878 1879 1880
static void
qca8k_port_fast_age(struct dsa_switch *ds, int port)
{
	struct qca8k_priv *priv = ds->priv;

	mutex_lock(&priv->reg_mutex);
	qca8k_fdb_access(priv, QCA8K_FDB_FLUSH_PORT, port);
	mutex_unlock(&priv->reg_mutex);
}

1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
static int
qca8k_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
{
	struct qca8k_priv *priv = ds->priv;
	unsigned int secs = msecs / 1000;
	u32 val;

	/* AGE_TIME reg is set in 7s step */
	val = secs / 7;

	/* Handle case with 0 as val to NOT disable
	 * learning
	 */
	if (!val)
		val = 1;

	return regmap_update_bits(priv->regmap, QCA8K_REG_ATU_CTRL, QCA8K_ATU_AGE_TIME_MASK,
				  QCA8K_ATU_AGE_TIME(val));
}

1901 1902 1903 1904 1905 1906 1907 1908 1909
static int
qca8k_port_enable(struct dsa_switch *ds, int port,
		  struct phy_device *phy)
{
	struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;

	qca8k_port_set_status(priv, port, 1);
	priv->port_sts[port].enabled = 1;

1910 1911
	if (dsa_is_user_port(ds, port))
		phy_support_asym_pause(phy);
1912

1913 1914 1915 1916
	return 0;
}

static void
1917
qca8k_port_disable(struct dsa_switch *ds, int port)
1918 1919 1920 1921 1922 1923 1924
{
	struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;

	qca8k_port_set_status(priv, port, 0);
	priv->port_sts[port].enabled = 0;
}

1925 1926 1927 1928 1929 1930 1931 1932 1933
static int
qca8k_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
{
	struct qca8k_priv *priv = ds->priv;
	int i, mtu = 0;

	priv->port_mtu[port] = new_mtu;

	for (i = 0; i < QCA8K_NUM_PORTS; i++)
1934 1935
		if (priv->port_mtu[i] > mtu)
			mtu = priv->port_mtu[i];
1936 1937

	/* Include L2 header / FCS length */
1938
	return qca8k_write(priv, QCA8K_MAX_FRAME_SIZE, mtu + ETH_HLEN + ETH_FCS_LEN);
1939 1940 1941 1942 1943 1944 1945 1946
}

static int
qca8k_port_max_mtu(struct dsa_switch *ds, int port)
{
	return QCA8K_MAX_MTU;
}

1947 1948 1949 1950 1951 1952
static int
qca8k_port_fdb_insert(struct qca8k_priv *priv, const u8 *addr,
		      u16 port_mask, u16 vid)
{
	/* Set the vid to the port vlan id if no vid is set */
	if (!vid)
1953
		vid = QCA8K_PORT_VID_DEF;
1954 1955 1956 1957 1958 1959 1960

	return qca8k_fdb_add(priv, addr, port_mask, vid,
			     QCA8K_ATU_STATUS_STATIC);
}

static int
qca8k_port_fdb_add(struct dsa_switch *ds, int port,
1961
		   const unsigned char *addr, u16 vid)
1962 1963 1964 1965
{
	struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
	u16 port_mask = BIT(port);

1966
	return qca8k_port_fdb_insert(priv, addr, port_mask, vid);
1967 1968 1969 1970
}

static int
qca8k_port_fdb_del(struct dsa_switch *ds, int port,
1971
		   const unsigned char *addr, u16 vid)
1972 1973 1974 1975 1976
{
	struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
	u16 port_mask = BIT(port);

	if (!vid)
1977
		vid = QCA8K_PORT_VID_DEF;
1978

1979
	return qca8k_fdb_del(priv, addr, port_mask, vid);
1980 1981 1982 1983
}

static int
qca8k_port_fdb_dump(struct dsa_switch *ds, int port,
1984
		    dsa_fdb_dump_cb_t *cb, void *data)
1985 1986 1987 1988
{
	struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
	struct qca8k_fdb _fdb = { 0 };
	int cnt = QCA8K_NUM_FDB_RECORDS;
1989
	bool is_static;
1990 1991 1992 1993 1994 1995
	int ret = 0;

	mutex_lock(&priv->reg_mutex);
	while (cnt-- && !qca8k_fdb_next(priv, &_fdb, port)) {
		if (!_fdb.aging)
			break;
1996 1997
		is_static = (_fdb.aging == QCA8K_ATU_STATUS_STATIC);
		ret = cb(_fdb.mac, _fdb.vid, is_static, data);
1998 1999 2000 2001 2002 2003 2004 2005
		if (ret)
			break;
	}
	mutex_unlock(&priv->reg_mutex);

	return 0;
}

2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
static int
qca8k_port_mdb_add(struct dsa_switch *ds, int port,
		   const struct switchdev_obj_port_mdb *mdb)
{
	struct qca8k_priv *priv = ds->priv;
	const u8 *addr = mdb->addr;
	u16 vid = mdb->vid;

	return qca8k_fdb_search_and_insert(priv, BIT(port), addr, vid);
}

static int
qca8k_port_mdb_del(struct dsa_switch *ds, int port,
		   const struct switchdev_obj_port_mdb *mdb)
{
	struct qca8k_priv *priv = ds->priv;
	const u8 *addr = mdb->addr;
	u16 vid = mdb->vid;

	return qca8k_fdb_search_and_del(priv, BIT(port), addr, vid);
}

2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
static int
qca8k_port_mirror_add(struct dsa_switch *ds, int port,
		      struct dsa_mall_mirror_tc_entry *mirror,
		      bool ingress)
{
	struct qca8k_priv *priv = ds->priv;
	int monitor_port, ret;
	u32 reg, val;

	/* Check for existent entry */
	if ((ingress ? priv->mirror_rx : priv->mirror_tx) & BIT(port))
		return -EEXIST;

	ret = regmap_read(priv->regmap, QCA8K_REG_GLOBAL_FW_CTRL0, &val);
	if (ret)
		return ret;

	/* QCA83xx can have only one port set to mirror mode.
	 * Check that the correct port is requested and return error otherwise.
	 * When no mirror port is set, the values is set to 0xF
	 */
	monitor_port = FIELD_GET(QCA8K_GLOBAL_FW_CTRL0_MIRROR_PORT_NUM, val);
	if (monitor_port != 0xF && monitor_port != mirror->to_local_port)
		return -EEXIST;

	/* Set the monitor port */
	val = FIELD_PREP(QCA8K_GLOBAL_FW_CTRL0_MIRROR_PORT_NUM,
			 mirror->to_local_port);
	ret = regmap_update_bits(priv->regmap, QCA8K_REG_GLOBAL_FW_CTRL0,
				 QCA8K_GLOBAL_FW_CTRL0_MIRROR_PORT_NUM, val);
	if (ret)
		return ret;

	if (ingress) {
		reg = QCA8K_PORT_LOOKUP_CTRL(port);
		val = QCA8K_PORT_LOOKUP_ING_MIRROR_EN;
	} else {
		reg = QCA8K_REG_PORT_HOL_CTRL1(port);
		val = QCA8K_PORT_HOL_CTRL1_EG_MIRROR_EN;
	}

	ret = regmap_update_bits(priv->regmap, reg, val, val);
	if (ret)
		return ret;

	/* Track mirror port for tx and rx to decide when the
	 * mirror port has to be disabled.
	 */
	if (ingress)
		priv->mirror_rx |= BIT(port);
	else
		priv->mirror_tx |= BIT(port);

	return 0;
}

static void
qca8k_port_mirror_del(struct dsa_switch *ds, int port,
		      struct dsa_mall_mirror_tc_entry *mirror)
{
	struct qca8k_priv *priv = ds->priv;
	u32 reg, val;
	int ret;

	if (mirror->ingress) {
		reg = QCA8K_PORT_LOOKUP_CTRL(port);
		val = QCA8K_PORT_LOOKUP_ING_MIRROR_EN;
	} else {
		reg = QCA8K_REG_PORT_HOL_CTRL1(port);
		val = QCA8K_PORT_HOL_CTRL1_EG_MIRROR_EN;
	}

	ret = regmap_clear_bits(priv->regmap, reg, val);
	if (ret)
		goto err;

	if (mirror->ingress)
		priv->mirror_rx &= ~BIT(port);
	else
		priv->mirror_tx &= ~BIT(port);

	/* No port set to send packet to mirror port. Disable mirror port */
	if (!priv->mirror_rx && !priv->mirror_tx) {
		val = FIELD_PREP(QCA8K_GLOBAL_FW_CTRL0_MIRROR_PORT_NUM, 0xF);
		ret = regmap_update_bits(priv->regmap, QCA8K_REG_GLOBAL_FW_CTRL0,
					 QCA8K_GLOBAL_FW_CTRL0_MIRROR_PORT_NUM, val);
		if (ret)
			goto err;
	}
err:
	dev_err(priv->dev, "Failed to del mirror port from %d", port);
}

2121
static int
2122 2123
qca8k_port_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
			  struct netlink_ext_ack *extack)
2124 2125
{
	struct qca8k_priv *priv = ds->priv;
2126
	int ret;
2127 2128

	if (vlan_filtering) {
2129
		ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
2130
				QCA8K_PORT_LOOKUP_VLAN_MODE_MASK,
2131
				QCA8K_PORT_LOOKUP_VLAN_MODE_SECURE);
2132
	} else {
2133
		ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
2134
				QCA8K_PORT_LOOKUP_VLAN_MODE_MASK,
2135
				QCA8K_PORT_LOOKUP_VLAN_MODE_NONE);
2136 2137
	}

2138
	return ret;
2139 2140 2141 2142
}

static int
qca8k_port_vlan_add(struct dsa_switch *ds, int port,
2143 2144
		    const struct switchdev_obj_port_vlan *vlan,
		    struct netlink_ext_ack *extack)
2145 2146 2147 2148
{
	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
	struct qca8k_priv *priv = ds->priv;
2149
	int ret;
2150

2151
	ret = qca8k_vlan_add(priv, port, vlan->vid, untagged);
2152
	if (ret) {
2153
		dev_err(priv->dev, "Failed to add VLAN to port %d (%d)", port, ret);
2154 2155
		return ret;
	}
2156 2157

	if (pvid) {
2158
		ret = qca8k_rmw(priv, QCA8K_EGRESS_VLAN(port),
2159 2160
				QCA8K_EGREES_VLAN_PORT_MASK(port),
				QCA8K_EGREES_VLAN_PORT(port, vlan->vid));
2161 2162 2163
		if (ret)
			return ret;

2164 2165 2166
		ret = qca8k_write(priv, QCA8K_REG_PORT_VLAN_CTRL0(port),
				  QCA8K_PORT_VLAN_CVID(vlan->vid) |
				  QCA8K_PORT_VLAN_SVID(vlan->vid));
2167
	}
2168

2169
	return ret;
2170 2171 2172 2173 2174 2175 2176
}

static int
qca8k_port_vlan_del(struct dsa_switch *ds, int port,
		    const struct switchdev_obj_port_vlan *vlan)
{
	struct qca8k_priv *priv = ds->priv;
2177
	int ret;
2178

2179
	ret = qca8k_vlan_del(priv, port, vlan->vid);
2180 2181 2182 2183 2184 2185
	if (ret)
		dev_err(priv->dev, "Failed to delete VLAN from port %d (%d)", port, ret);

	return ret;
}

2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201
static u32 qca8k_get_phy_flags(struct dsa_switch *ds, int port)
{
	struct qca8k_priv *priv = ds->priv;

	/* Communicate to the phy internal driver the switch revision.
	 * Based on the switch revision different values needs to be
	 * set to the dbg and mmd reg on the phy.
	 * The first 2 bit are used to communicate the switch revision
	 * to the phy driver.
	 */
	if (port > 0 && port < 6)
		return priv->switch_revision;

	return 0;
}

2202
static enum dsa_tag_protocol
2203 2204
qca8k_get_tag_protocol(struct dsa_switch *ds, int port,
		       enum dsa_tag_protocol mp)
2205 2206 2207 2208
{
	return DSA_TAG_PROTO_QCA;
}

A
Ansuel Smith 已提交
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
static bool
qca8k_lag_can_offload(struct dsa_switch *ds,
		      struct net_device *lag,
		      struct netdev_lag_upper_info *info)
{
	struct dsa_port *dp;
	int id, members = 0;

	id = dsa_lag_id(ds->dst, lag);
	if (id < 0 || id >= ds->num_lag_ids)
		return false;

	dsa_lag_foreach_port(dp, ds->dst, lag)
		/* Includes the port joining the LAG */
		members++;

	if (members > QCA8K_NUM_PORTS_FOR_LAG)
		return false;

	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
		return false;

	if (info->hash_type != NETDEV_LAG_HASH_L2 ||
	    info->hash_type != NETDEV_LAG_HASH_L23)
		return false;

	return true;
}

static int
qca8k_lag_setup_hash(struct dsa_switch *ds,
		     struct net_device *lag,
		     struct netdev_lag_upper_info *info)
{
	struct qca8k_priv *priv = ds->priv;
	bool unique_lag = true;
	int i, id;
	u32 hash;

	id = dsa_lag_id(ds->dst, lag);

	switch (info->hash_type) {
	case NETDEV_LAG_HASH_L23:
		hash |= QCA8K_TRUNK_HASH_SIP_EN;
		hash |= QCA8K_TRUNK_HASH_DIP_EN;
		fallthrough;
	case NETDEV_LAG_HASH_L2:
		hash |= QCA8K_TRUNK_HASH_SA_EN;
		hash |= QCA8K_TRUNK_HASH_DA_EN;
		break;
	default: /* We should NEVER reach this */
		return -EOPNOTSUPP;
	}

	/* Check if we are the unique configured LAG */
	dsa_lags_foreach_id(i, ds->dst)
		if (i != id && dsa_lag_dev(ds->dst, i)) {
			unique_lag = false;
			break;
		}

	/* Hash Mode is global. Make sure the same Hash Mode
	 * is set to all the 4 possible lag.
	 * If we are the unique LAG we can set whatever hash
	 * mode we want.
	 * To change hash mode it's needed to remove all LAG
	 * and change the mode with the latest.
	 */
	if (unique_lag) {
		priv->lag_hash_mode = hash;
	} else if (priv->lag_hash_mode != hash) {
		netdev_err(lag, "Error: Mismateched Hash Mode across different lag is not supported\n");
		return -EOPNOTSUPP;
	}

	return regmap_update_bits(priv->regmap, QCA8K_TRUNK_HASH_EN_CTRL,
				  QCA8K_TRUNK_HASH_MASK, hash);
}

static int
qca8k_lag_refresh_portmap(struct dsa_switch *ds, int port,
			  struct net_device *lag, bool delete)
{
	struct qca8k_priv *priv = ds->priv;
	int ret, id, i;
	u32 val;

	id = dsa_lag_id(ds->dst, lag);

	/* Read current port member */
	ret = regmap_read(priv->regmap, QCA8K_REG_GOL_TRUNK_CTRL0, &val);
	if (ret)
		return ret;

	/* Shift val to the correct trunk */
	val >>= QCA8K_REG_GOL_TRUNK_SHIFT(id);
	val &= QCA8K_REG_GOL_TRUNK_MEMBER_MASK;
	if (delete)
		val &= ~BIT(port);
	else
		val |= BIT(port);

	/* Update port member. With empty portmap disable trunk */
	ret = regmap_update_bits(priv->regmap, QCA8K_REG_GOL_TRUNK_CTRL0,
				 QCA8K_REG_GOL_TRUNK_MEMBER(id) |
				 QCA8K_REG_GOL_TRUNK_EN(id),
				 !val << QCA8K_REG_GOL_TRUNK_SHIFT(id) |
				 val << QCA8K_REG_GOL_TRUNK_SHIFT(id));

	/* Search empty member if adding or port on deleting */
	for (i = 0; i < QCA8K_NUM_PORTS_FOR_LAG; i++) {
		ret = regmap_read(priv->regmap, QCA8K_REG_GOL_TRUNK_CTRL(id), &val);
		if (ret)
			return ret;

		val >>= QCA8K_REG_GOL_TRUNK_ID_MEM_ID_SHIFT(id, i);
		val &= QCA8K_REG_GOL_TRUNK_ID_MEM_ID_MASK;

		if (delete) {
			/* If port flagged to be disabled assume this member is
			 * empty
			 */
			if (val != QCA8K_REG_GOL_TRUNK_ID_MEM_ID_EN_MASK)
				continue;

			val &= QCA8K_REG_GOL_TRUNK_ID_MEM_ID_PORT_MASK;
			if (val != port)
				continue;
		} else {
			/* If port flagged to be enabled assume this member is
			 * already set
			 */
			if (val == QCA8K_REG_GOL_TRUNK_ID_MEM_ID_EN_MASK)
				continue;
		}

		/* We have found the member to add/remove */
		break;
	}

	/* Set port in the correct port mask or disable port if in delete mode */
	return regmap_update_bits(priv->regmap, QCA8K_REG_GOL_TRUNK_CTRL(id),
				  QCA8K_REG_GOL_TRUNK_ID_MEM_ID_EN(id, i) |
				  QCA8K_REG_GOL_TRUNK_ID_MEM_ID_PORT(id, i),
				  !delete << QCA8K_REG_GOL_TRUNK_ID_MEM_ID_SHIFT(id, i) |
				  port << QCA8K_REG_GOL_TRUNK_ID_MEM_ID_SHIFT(id, i));
}

static int
qca8k_port_lag_join(struct dsa_switch *ds, int port,
		    struct net_device *lag,
		    struct netdev_lag_upper_info *info)
{
	int ret;

	if (!qca8k_lag_can_offload(ds, lag, info))
		return -EOPNOTSUPP;

	ret = qca8k_lag_setup_hash(ds, lag, info);
	if (ret)
		return ret;

	return qca8k_lag_refresh_portmap(ds, port, lag, false);
}

static int
qca8k_port_lag_leave(struct dsa_switch *ds, int port,
		     struct net_device *lag)
{
	return qca8k_lag_refresh_portmap(ds, port, lag, true);
}

2381
static const struct dsa_switch_ops qca8k_switch_ops = {
2382 2383 2384 2385 2386
	.get_tag_protocol	= qca8k_get_tag_protocol,
	.setup			= qca8k_setup,
	.get_strings		= qca8k_get_strings,
	.get_ethtool_stats	= qca8k_get_ethtool_stats,
	.get_sset_count		= qca8k_get_sset_count,
2387
	.set_ageing_time	= qca8k_set_ageing_time,
V
Vivien Didelot 已提交
2388 2389
	.get_mac_eee		= qca8k_get_mac_eee,
	.set_mac_eee		= qca8k_set_mac_eee,
2390 2391
	.port_enable		= qca8k_port_enable,
	.port_disable		= qca8k_port_disable,
2392 2393
	.port_change_mtu	= qca8k_port_change_mtu,
	.port_max_mtu		= qca8k_port_max_mtu,
2394 2395 2396
	.port_stp_state_set	= qca8k_port_stp_state_set,
	.port_bridge_join	= qca8k_port_bridge_join,
	.port_bridge_leave	= qca8k_port_bridge_leave,
2397
	.port_fast_age		= qca8k_port_fast_age,
2398 2399 2400
	.port_fdb_add		= qca8k_port_fdb_add,
	.port_fdb_del		= qca8k_port_fdb_del,
	.port_fdb_dump		= qca8k_port_fdb_dump,
2401 2402
	.port_mdb_add		= qca8k_port_mdb_add,
	.port_mdb_del		= qca8k_port_mdb_del,
2403 2404
	.port_mirror_add	= qca8k_port_mirror_add,
	.port_mirror_del	= qca8k_port_mirror_del,
2405 2406 2407
	.port_vlan_filtering	= qca8k_port_vlan_filtering,
	.port_vlan_add		= qca8k_port_vlan_add,
	.port_vlan_del		= qca8k_port_vlan_del,
2408 2409 2410 2411 2412
	.phylink_validate	= qca8k_phylink_validate,
	.phylink_mac_link_state	= qca8k_phylink_mac_link_state,
	.phylink_mac_config	= qca8k_phylink_mac_config,
	.phylink_mac_link_down	= qca8k_phylink_mac_link_down,
	.phylink_mac_link_up	= qca8k_phylink_mac_link_up,
2413
	.get_phy_flags		= qca8k_get_phy_flags,
A
Ansuel Smith 已提交
2414 2415
	.port_lag_join		= qca8k_port_lag_join,
	.port_lag_leave		= qca8k_port_lag_leave,
2416 2417
};

2418 2419 2420 2421 2422
static int qca8k_read_switch_id(struct qca8k_priv *priv)
{
	const struct qca8k_match_data *data;
	u32 val;
	u8 id;
2423
	int ret;
2424 2425 2426 2427 2428 2429

	/* get the switches ID from the compatible */
	data = of_device_get_match_data(priv->dev);
	if (!data)
		return -ENODEV;

2430 2431
	ret = qca8k_read(priv, QCA8K_REG_MASK_CTRL, &val);
	if (ret < 0)
2432 2433
		return -ENODEV;

2434
	id = QCA8K_MASK_CTRL_DEVICE_ID(val);
2435 2436 2437 2438 2439 2440 2441 2442
	if (id != data->id) {
		dev_err(priv->dev, "Switch id detected %x but expected %x", id, data->id);
		return -ENODEV;
	}

	priv->switch_id = id;

	/* Save revision to communicate to the internal PHY driver */
2443
	priv->switch_revision = QCA8K_MASK_CTRL_REV_ID(val);
2444 2445 2446 2447

	return 0;
}

2448 2449 2450 2451
static int
qca8k_sw_probe(struct mdio_device *mdiodev)
{
	struct qca8k_priv *priv;
2452
	int ret;
2453 2454 2455 2456 2457 2458 2459 2460 2461

	/* allocate the private data struct so that we can probe the switches
	 * ID register
	 */
	priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->bus = mdiodev->bus;
2462
	priv->dev = &mdiodev->dev;
2463

2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
	priv->reset_gpio = devm_gpiod_get_optional(priv->dev, "reset",
						   GPIOD_ASIS);
	if (IS_ERR(priv->reset_gpio))
		return PTR_ERR(priv->reset_gpio);

	if (priv->reset_gpio) {
		gpiod_set_value_cansleep(priv->reset_gpio, 1);
		/* The active low duration must be greater than 10 ms
		 * and checkpatch.pl wants 20 ms.
		 */
		msleep(20);
		gpiod_set_value_cansleep(priv->reset_gpio, 0);
	}

2478 2479 2480 2481 2482 2483 2484 2485
	/* Start by setting up the register mapping */
	priv->regmap = devm_regmap_init(&mdiodev->dev, NULL, priv,
					&qca8k_regmap_config);
	if (IS_ERR(priv->regmap)) {
		dev_err(priv->dev, "regmap initialization failed");
		return PTR_ERR(priv->regmap);
	}

2486 2487 2488 2489
	/* Check the detected switch id */
	ret = qca8k_read_switch_id(priv);
	if (ret)
		return ret;
2490

2491
	priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds), GFP_KERNEL);
2492 2493 2494
	if (!priv->ds)
		return -ENOMEM;

2495
	priv->ds->dev = &mdiodev->dev;
2496
	priv->ds->num_ports = QCA8K_NUM_PORTS;
2497
	priv->ds->priv = priv;
2498 2499
	priv->ops = qca8k_switch_ops;
	priv->ds->ops = &priv->ops;
2500 2501 2502
	mutex_init(&priv->reg_mutex);
	dev_set_drvdata(&mdiodev->dev, priv);

2503
	return dsa_register_switch(priv->ds);
2504 2505 2506 2507 2508 2509 2510 2511
}

static void
qca8k_sw_remove(struct mdio_device *mdiodev)
{
	struct qca8k_priv *priv = dev_get_drvdata(&mdiodev->dev);
	int i;

2512 2513 2514
	if (!priv)
		return;

2515 2516 2517 2518
	for (i = 0; i < QCA8K_NUM_PORTS; i++)
		qca8k_port_set_status(priv, i, 0);

	dsa_unregister_switch(priv->ds);
2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532

	dev_set_drvdata(&mdiodev->dev, NULL);
}

static void qca8k_sw_shutdown(struct mdio_device *mdiodev)
{
	struct qca8k_priv *priv = dev_get_drvdata(&mdiodev->dev);

	if (!priv)
		return;

	dsa_switch_shutdown(priv->ds);

	dev_set_drvdata(&mdiodev->dev, NULL);
2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550
}

#ifdef CONFIG_PM_SLEEP
static void
qca8k_set_pm(struct qca8k_priv *priv, int enable)
{
	int i;

	for (i = 0; i < QCA8K_NUM_PORTS; i++) {
		if (!priv->port_sts[i].enabled)
			continue;

		qca8k_port_set_status(priv, i, enable);
	}
}

static int qca8k_suspend(struct device *dev)
{
2551
	struct qca8k_priv *priv = dev_get_drvdata(dev);
2552 2553 2554 2555 2556 2557 2558 2559

	qca8k_set_pm(priv, 0);

	return dsa_switch_suspend(priv->ds);
}

static int qca8k_resume(struct device *dev)
{
2560
	struct qca8k_priv *priv = dev_get_drvdata(dev);
2561 2562 2563 2564 2565 2566 2567 2568 2569 2570

	qca8k_set_pm(priv, 1);

	return dsa_switch_resume(priv->ds);
}
#endif /* CONFIG_PM_SLEEP */

static SIMPLE_DEV_PM_OPS(qca8k_pm_ops,
			 qca8k_suspend, qca8k_resume);

2571 2572 2573
static const struct qca8k_match_data qca8327 = {
	.id = QCA8K_ID_QCA8327,
	.reduced_package = true,
2574
	.mib_count = QCA8K_QCA832X_MIB_COUNT,
2575 2576 2577
};

static const struct qca8k_match_data qca8328 = {
2578
	.id = QCA8K_ID_QCA8327,
2579
	.mib_count = QCA8K_QCA832X_MIB_COUNT,
2580 2581 2582 2583
};

static const struct qca8k_match_data qca833x = {
	.id = QCA8K_ID_QCA8337,
2584
	.mib_count = QCA8K_QCA833X_MIB_COUNT,
2585 2586
};

2587
static const struct of_device_id qca8k_of_match[] = {
2588 2589
	{ .compatible = "qca,qca8327", .data = &qca8327 },
	{ .compatible = "qca,qca8328", .data = &qca8328 },
2590 2591
	{ .compatible = "qca,qca8334", .data = &qca833x },
	{ .compatible = "qca,qca8337", .data = &qca833x },
2592 2593 2594 2595 2596 2597
	{ /* sentinel */ },
};

static struct mdio_driver qca8kmdio_driver = {
	.probe  = qca8k_sw_probe,
	.remove = qca8k_sw_remove,
2598
	.shutdown = qca8k_sw_shutdown,
2599 2600 2601 2602 2603 2604 2605
	.mdiodrv.driver = {
		.name = "qca8k",
		.of_match_table = qca8k_of_match,
		.pm = &qca8k_pm_ops,
	},
};

2606
mdio_module_driver(qca8kmdio_driver);
2607 2608 2609 2610 2611

MODULE_AUTHOR("Mathieu Olivari, John Crispin <john@phrozen.org>");
MODULE_DESCRIPTION("Driver for QCA8K ethernet switch family");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:qca8k");