ksz_common.h 13.6 KB
Newer Older
1 2
/* SPDX-License-Identifier: GPL-2.0 */
/* Microchip switch driver common header
3
 *
4
 * Copyright (C) 2017-2019 Microchip Technology Inc.
5 6 7 8 9
 */

#ifndef __KSZ_COMMON_H
#define __KSZ_COMMON_H

10 11 12 13
#include <linux/etherdevice.h>
#include <linux/kernel.h>
#include <linux/mutex.h>
#include <linux/phy.h>
14
#include <linux/regmap.h>
15 16
#include <net/dsa.h>

17 18
#define KSZ_MAX_NUM_PORTS 8

19 20 21 22 23 24 25 26
struct vlan_table {
	u32 table[3];
};

struct ksz_port_mib {
	struct mutex cnt_mutex;		/* structure access */
	u8 cnt_ptr;
	u64 *counters;
27
	struct rtnl_link_stats64 stats64;
28
	struct ethtool_pause_stats pause_stats;
29
	struct spinlock stats64_lock;
30 31
};

32 33 34 35 36
struct ksz_mib_names {
	int index;
	char string[ETH_GSTRING_LEN];
};

37 38 39 40 41 42 43 44
struct ksz_chip_data {
	u32 chip_id;
	const char *dev_name;
	int num_vlans;
	int num_alus;
	int num_statics;
	int cpu_ports;
	int port_cnt;
45
	const struct ksz_dev_ops *ops;
46 47
	bool phy_errata_9477;
	bool ksz87xx_eee_link_erratum;
48 49 50
	const struct ksz_mib_names *mib_names;
	int mib_cnt;
	u8 reg_mib_cnt;
51
	const u16 *regs;
52
	const u32 *masks;
53
	const u8 *shifts;
54
	const u8 *xmii_ctrl0;
55
	const u8 *xmii_ctrl1;
56
	int stp_ctrl_reg;
57
	int broadcast_ctrl_reg;
58
	int multicast_ctrl_reg;
59
	int start_ctrl_reg;
60 61 62 63
	bool supports_mii[KSZ_MAX_NUM_PORTS];
	bool supports_rmii[KSZ_MAX_NUM_PORTS];
	bool supports_rgmii[KSZ_MAX_NUM_PORTS];
	bool internal_phy[KSZ_MAX_NUM_PORTS];
64 65
};

66
struct ksz_port {
67
	bool remove_tag;		/* Remove Tag flag set, for ksz8795 only */
68 69 70 71 72 73 74 75 76 77 78 79
	int stp_state;
	struct phy_device phydev;

	u32 on:1;			/* port is not disabled by hardware */
	u32 phy:1;			/* port has a PHY */
	u32 fiber:1;			/* port is fiber */
	u32 sgmii:1;			/* port is SGMII */
	u32 force:1;
	u32 read:1;			/* read MIB counters in background */
	u32 freeze:1;			/* MIB counter freeze is enabled */

	struct ksz_port_mib mib;
80
	phy_interface_t interface;
81
	u16 max_frame;
82 83
	u32 rgmii_tx_val;
	u32 rgmii_rx_val;
84 85 86 87 88
};

struct ksz_device {
	struct dsa_switch *ds;
	struct ksz_platform_data *pdata;
89
	const struct ksz_chip_data *info;
90 91

	struct mutex dev_mutex;		/* device access */
92
	struct mutex regmap_mutex;	/* regmap access */
93 94 95 96 97 98 99 100 101 102 103 104 105
	struct mutex alu_mutex;		/* ALU access */
	struct mutex vlan_mutex;	/* vlan access */
	const struct ksz_dev_ops *dev_ops;

	struct device *dev;
	struct regmap *regmap[3];

	void *priv;

	struct gpio_desc *reset_gpio;	/* Optional reset GPIO */

	/* chip specific data */
	u32 chip_id;
106
	u8 chip_rev;
107 108
	int cpu_port;			/* port connected to CPU */
	int phy_port_cnt;
109
	phy_interface_t compat_interface;
110
	bool synclko_125;
111
	bool synclko_disable;
112 113 114 115

	struct vlan_table *vlan_cache;

	struct ksz_port *ports;
116
	struct delayed_work mib_read;
117 118 119 120 121 122 123
	unsigned long mib_read_interval;
	u16 mirror_rx;
	u16 mirror_tx;
	u32 features;			/* chip specific features */
	u16 port_mask;
};

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/* List of supported models */
enum ksz_model {
	KSZ8795,
	KSZ8794,
	KSZ8765,
	KSZ8830,
	KSZ9477,
	KSZ9897,
	KSZ9893,
	KSZ9567,
	LAN9370,
	LAN9371,
	LAN9372,
	LAN9373,
	LAN9374,
};

enum ksz_chip_id {
	KSZ8795_CHIP_ID = 0x8795,
	KSZ8794_CHIP_ID = 0x8794,
	KSZ8765_CHIP_ID = 0x8765,
	KSZ8830_CHIP_ID = 0x8830,
	KSZ9477_CHIP_ID = 0x00947700,
	KSZ9897_CHIP_ID = 0x00989700,
	KSZ9893_CHIP_ID = 0x00989300,
	KSZ9567_CHIP_ID = 0x00956700,
	LAN9370_CHIP_ID = 0x00937000,
	LAN9371_CHIP_ID = 0x00937100,
	LAN9372_CHIP_ID = 0x00937200,
	LAN9373_CHIP_ID = 0x00937300,
	LAN9374_CHIP_ID = 0x00937400,
};

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
enum ksz_regs {
	REG_IND_CTRL_0,
	REG_IND_DATA_8,
	REG_IND_DATA_CHECK,
	REG_IND_DATA_HI,
	REG_IND_DATA_LO,
	REG_IND_MIB_CHECK,
	REG_IND_BYTE,
	P_FORCE_CTRL,
	P_LINK_STATUS,
	P_LOCAL_CTRL,
	P_NEG_RESTART_CTRL,
	P_REMOTE_STATUS,
	P_SPEED_STATUS,
	S_TAIL_TAG_CTRL,
172
	P_STP_CTRL,
173 174 175
	S_START_CTRL,
	S_BROADCAST_CTRL,
	S_MULTICAST_CTRL,
176
	P_XMII_CTRL_0,
177
	P_XMII_CTRL_1,
178 179
};

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
enum ksz_masks {
	PORT_802_1P_REMAPPING,
	SW_TAIL_TAG_ENABLE,
	MIB_COUNTER_OVERFLOW,
	MIB_COUNTER_VALID,
	VLAN_TABLE_FID,
	VLAN_TABLE_MEMBERSHIP,
	VLAN_TABLE_VALID,
	STATIC_MAC_TABLE_VALID,
	STATIC_MAC_TABLE_USE_FID,
	STATIC_MAC_TABLE_FID,
	STATIC_MAC_TABLE_OVERRIDE,
	STATIC_MAC_TABLE_FWD_PORTS,
	DYNAMIC_MAC_TABLE_ENTRIES_H,
	DYNAMIC_MAC_TABLE_MAC_EMPTY,
	DYNAMIC_MAC_TABLE_NOT_READY,
	DYNAMIC_MAC_TABLE_ENTRIES,
	DYNAMIC_MAC_TABLE_FID,
	DYNAMIC_MAC_TABLE_SRC_PORT,
	DYNAMIC_MAC_TABLE_TIMESTAMP,
200 201
	ALU_STAT_WRITE,
	ALU_STAT_READ,
202 203
	P_MII_TX_FLOW_CTRL,
	P_MII_RX_FLOW_CTRL,
204 205
};

206 207 208 209 210 211 212 213 214 215
enum ksz_shifts {
	VLAN_TABLE_MEMBERSHIP_S,
	VLAN_TABLE,
	STATIC_MAC_FWD_PORTS,
	STATIC_MAC_FID,
	DYNAMIC_MAC_ENTRIES_H,
	DYNAMIC_MAC_ENTRIES,
	DYNAMIC_MAC_FID,
	DYNAMIC_MAC_TIMESTAMP,
	DYNAMIC_MAC_SRC_PORT,
216
	ALU_STAT_INDEX,
217 218
};

219 220 221
enum ksz_xmii_ctrl0 {
	P_MII_100MBIT,
	P_MII_10MBIT,
222 223
	P_MII_FULL_DUPLEX,
	P_MII_HALF_DUPLEX,
224 225
};

226
enum ksz_xmii_ctrl1 {
227 228 229 230
	P_RGMII_SEL,
	P_RMII_SEL,
	P_GMII_SEL,
	P_MII_SEL,
231 232 233 234
	P_GMII_1GBIT,
	P_GMII_NOT_1GBIT,
};

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
struct alu_struct {
	/* entry 1 */
	u8	is_static:1;
	u8	is_src_filter:1;
	u8	is_dst_filter:1;
	u8	prio_age:3;
	u32	_reserv_0_1:23;
	u8	mstp:3;
	/* entry 2 */
	u8	is_override:1;
	u8	is_use_fid:1;
	u32	_reserv_1_1:23;
	u8	port_forward:7;
	/* entry 3 & 4*/
	u32	_reserv_2_1:9;
	u8	fid:7;
	u8	mac[ETH_ALEN];
};

struct ksz_dev_ops {
255
	int (*setup)(struct dsa_switch *ds);
256 257 258 259 260 261 262 263 264 265 266
	u32 (*get_port_addr)(int port, int offset);
	void (*cfg_port_member)(struct ksz_device *dev, int port, u8 member);
	void (*flush_dyn_mac_table)(struct ksz_device *dev, int port);
	void (*port_cleanup)(struct ksz_device *dev, int port);
	void (*port_setup)(struct ksz_device *dev, int port, bool cpu_port);
	void (*r_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 *val);
	void (*w_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 val);
	void (*r_mib_cnt)(struct ksz_device *dev, int port, u16 addr,
			  u64 *cnt);
	void (*r_mib_pkt)(struct ksz_device *dev, int port, u16 addr,
			  u64 *dropped, u64 *cnt);
267
	void (*r_mib_stat64)(struct ksz_device *dev, int port);
268 269 270 271 272 273 274
	int  (*vlan_filtering)(struct ksz_device *dev, int port,
			       bool flag, struct netlink_ext_ack *extack);
	int  (*vlan_add)(struct ksz_device *dev, int port,
			 const struct switchdev_obj_port_vlan *vlan,
			 struct netlink_ext_ack *extack);
	int  (*vlan_del)(struct ksz_device *dev, int port,
			 const struct switchdev_obj_port_vlan *vlan);
275 276 277 278 279
	int (*mirror_add)(struct ksz_device *dev, int port,
			  struct dsa_mall_mirror_tc_entry *mirror,
			  bool ingress, struct netlink_ext_ack *extack);
	void (*mirror_del)(struct ksz_device *dev, int port,
			   struct dsa_mall_mirror_tc_entry *mirror);
280 281 282 283 284 285
	int (*fdb_add)(struct ksz_device *dev, int port,
		       const unsigned char *addr, u16 vid, struct dsa_db db);
	int (*fdb_del)(struct ksz_device *dev, int port,
		       const unsigned char *addr, u16 vid, struct dsa_db db);
	int (*fdb_dump)(struct ksz_device *dev, int port,
			dsa_fdb_dump_cb_t *cb, void *data);
286 287 288 289 290 291
	int (*mdb_add)(struct ksz_device *dev, int port,
		       const struct switchdev_obj_port_mdb *mdb,
		       struct dsa_db db);
	int (*mdb_del)(struct ksz_device *dev, int port,
		       const struct switchdev_obj_port_mdb *mdb,
		       struct dsa_db db);
292 293
	void (*get_caps)(struct ksz_device *dev, int port,
			 struct phylink_config *config);
294 295
	int (*change_mtu)(struct ksz_device *dev, int port, int mtu);
	int (*max_mtu)(struct ksz_device *dev, int port);
296 297
	void (*freeze_mib)(struct ksz_device *dev, int port, bool freeze);
	void (*port_init_cnt)(struct ksz_device *dev, int port);
298 299 300
	void (*phylink_mac_config)(struct ksz_device *dev, int port,
				   unsigned int mode,
				   const struct phylink_link_state *state);
301 302 303 304 305
	void (*phylink_mac_link_up)(struct ksz_device *dev, int port,
				    unsigned int mode,
				    phy_interface_t interface,
				    struct phy_device *phydev, int speed,
				    int duplex, bool tx_pause, bool rx_pause);
306
	void (*setup_rgmii_delay)(struct ksz_device *dev, int port);
307
	void (*config_cpu_port)(struct dsa_switch *ds);
308
	int (*enable_stp_addr)(struct ksz_device *dev);
309
	int (*reset)(struct ksz_device *dev);
310 311 312 313 314
	int (*init)(struct ksz_device *dev);
	void (*exit)(struct ksz_device *dev);
};

struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
315
int ksz_switch_register(struct ksz_device *dev);
316 317
void ksz_switch_remove(struct ksz_device *dev);

318
void ksz_init_mib_timer(struct ksz_device *dev);
319
void ksz_r_mib_stats64(struct ksz_device *dev, int port);
320
void ksz_port_stp_state_set(struct dsa_switch *ds, int port, u8 state);
321 322
bool ksz_get_gbit(struct ksz_device *dev, int port);
void ksz_set_gbit(struct ksz_device *dev, int port, bool gbit);
323
void ksz_set_xmii(struct ksz_device *dev, int port, phy_interface_t interface);
324
extern const struct ksz_chip_data ksz_switch_chips[];
325 326 327 328 329

/* Common register access functions */

static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
{
330 331
	unsigned int value;
	int ret = regmap_read(dev->regmap[0], reg, &value);
332

333
	*val = value;
334 335 336 337 338
	return ret;
}

static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
{
339 340
	unsigned int value;
	int ret = regmap_read(dev->regmap[1], reg, &value);
341

342
	*val = value;
343 344 345 346 347
	return ret;
}

static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
{
348 349
	unsigned int value;
	int ret = regmap_read(dev->regmap[2], reg, &value);
350

351
	*val = value;
352 353 354
	return ret;
}

355 356 357 358 359 360
static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val)
{
	u32 value[2];
	int ret;

	ret = regmap_bulk_read(dev->regmap[2], reg, value, 2);
361 362
	if (!ret)
		*val = (u64)value[0] << 32 | value[1];
363 364 365 366

	return ret;
}

367 368
static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
{
369
	return regmap_write(dev->regmap[0], reg, value);
370 371 372 373
}

static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
{
374
	return regmap_write(dev->regmap[1], reg, value);
375 376 377 378
}

static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
{
379
	return regmap_write(dev->regmap[2], reg, value);
380 381
}

382 383 384 385 386 387 388 389 390 391 392 393
static inline int ksz_write64(struct ksz_device *dev, u32 reg, u64 value)
{
	u32 val[2];

	/* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */
	value = swab64(value);
	val[0] = swab32(value & 0xffffffffULL);
	val[1] = swab32(value >> 32ULL);

	return regmap_bulk_write(dev->regmap[2], reg, val, 2);
}

394 395 396 397 398 399 400 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
static inline void ksz_pread8(struct ksz_device *dev, int port, int offset,
			      u8 *data)
{
	ksz_read8(dev, dev->dev_ops->get_port_addr(port, offset), data);
}

static inline void ksz_pread16(struct ksz_device *dev, int port, int offset,
			       u16 *data)
{
	ksz_read16(dev, dev->dev_ops->get_port_addr(port, offset), data);
}

static inline void ksz_pread32(struct ksz_device *dev, int port, int offset,
			       u32 *data)
{
	ksz_read32(dev, dev->dev_ops->get_port_addr(port, offset), data);
}

static inline void ksz_pwrite8(struct ksz_device *dev, int port, int offset,
			       u8 data)
{
	ksz_write8(dev, dev->dev_ops->get_port_addr(port, offset), data);
}

static inline void ksz_pwrite16(struct ksz_device *dev, int port, int offset,
				u16 data)
{
	ksz_write16(dev, dev->dev_ops->get_port_addr(port, offset), data);
}

static inline void ksz_pwrite32(struct ksz_device *dev, int port, int offset,
				u32 data)
{
	ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset), data);
}

430 431 432 433 434 435 436 437
static inline void ksz_prmw8(struct ksz_device *dev, int port, int offset,
			     u8 mask, u8 val)
{
	regmap_update_bits(dev->regmap[0],
			   dev->dev_ops->get_port_addr(port, offset),
			   mask, val);
}

438 439 440 441 442 443 444 445 446 447 448 449
static inline void ksz_regmap_lock(void *__mtx)
{
	struct mutex *mtx = __mtx;
	mutex_lock(mtx);
}

static inline void ksz_regmap_unlock(void *__mtx)
{
	struct mutex *mtx = __mtx;
	mutex_unlock(mtx);
}

450 451 452 453 454 455 456 457 458
static inline int is_lan937x(struct ksz_device *dev)
{
	return dev->chip_id == LAN9370_CHIP_ID ||
		dev->chip_id == LAN9371_CHIP_ID ||
		dev->chip_id == LAN9372_CHIP_ID ||
		dev->chip_id == LAN9373_CHIP_ID ||
		dev->chip_id == LAN9374_CHIP_ID;
}

459 460 461 462 463
/* STP State Defines */
#define PORT_TX_ENABLE			BIT(2)
#define PORT_RX_ENABLE			BIT(1)
#define PORT_LEARN_DISABLE		BIT(0)

464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
/* Switch ID Defines */
#define REG_CHIP_ID0			0x00

#define SW_FAMILY_ID_M			GENMASK(15, 8)
#define KSZ87_FAMILY_ID			0x87
#define KSZ88_FAMILY_ID			0x88

#define KSZ8_PORT_STATUS_0		0x08
#define KSZ8_PORT_FIBER_MODE		BIT(7)

#define SW_CHIP_ID_M			GENMASK(7, 4)
#define KSZ87_CHIP_ID_94		0x6
#define KSZ87_CHIP_ID_95		0x9
#define KSZ88_CHIP_ID_63		0x3

#define SW_REV_ID_M			GENMASK(7, 4)

481 482 483 484 485 486 487 488 489 490
/* Driver set switch broadcast storm protection at 10% rate. */
#define BROADCAST_STORM_PROT_RATE	10

/* 148,800 frames * 67 ms / 100 */
#define BROADCAST_STORM_VALUE		9969

#define BROADCAST_STORM_RATE_HI		0x07
#define BROADCAST_STORM_RATE_LO		0xFF
#define BROADCAST_STORM_RATE		0x07FF

491 492
#define MULTICAST_STORM_DISABLE		BIT(6)

493 494
#define SW_START			0x01

495
/* xMII configuration */
496
#define P_MII_DUPLEX_M			BIT(6)
497 498
#define P_MII_100MBIT_M			BIT(4)

499
#define P_GMII_1GBIT_M			BIT(6)
500 501 502
#define P_RGMII_ID_IG_ENABLE		BIT(4)
#define P_RGMII_ID_EG_ENABLE		BIT(3)
#define P_MII_SEL_M			0x3
503

504 505 506 507
/* Regmap tables generation */
#define KSZ_SPI_OP_RD		3
#define KSZ_SPI_OP_WR		2

508 509
#define swabnot_used(x)		0

510 511 512 513 514
#define KSZ_SPI_OP_FLAG_MASK(opcode, swp, regbits, regpad)		\
	swab##swp((opcode) << ((regbits) + (regpad)))

#define KSZ_REGMAP_ENTRY(width, swp, regbits, regpad, regalign)		\
	{								\
515
		.name = #width,						\
516
		.val_bits = (width),					\
517
		.reg_stride = 1,					\
518 519 520 521 522 523 524 525 526 527
		.reg_bits = (regbits) + (regalign),			\
		.pad_bits = (regpad),					\
		.max_register = BIT(regbits) - 1,			\
		.cache_type = REGCACHE_NONE,				\
		.read_flag_mask =					\
			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_RD, swp,	\
					     regbits, regpad),		\
		.write_flag_mask =					\
			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_WR, swp,	\
					     regbits, regpad),		\
528 529
		.lock = ksz_regmap_lock,				\
		.unlock = ksz_regmap_unlock,				\
530 531 532 533 534 535 536 537 538 539 540
		.reg_format_endian = REGMAP_ENDIAN_BIG,			\
		.val_format_endian = REGMAP_ENDIAN_BIG			\
	}

#define KSZ_REGMAP_TABLE(ksz, swp, regbits, regpad, regalign)		\
	static const struct regmap_config ksz##_regmap_config[] = {	\
		KSZ_REGMAP_ENTRY(8, swp, (regbits), (regpad), (regalign)), \
		KSZ_REGMAP_ENTRY(16, swp, (regbits), (regpad), (regalign)), \
		KSZ_REGMAP_ENTRY(32, swp, (regbits), (regpad), (regalign)), \
	}

541
#endif