blob: 6e1f74e4d471daf982f10517bde7191a13530114 [file] [log] [blame]
Florian Fainelli246d7f72014-08-27 17:04:56 -07001/*
2 * Broadcom Starfighter2 private context
3 *
4 * Copyright (C) 2014, Broadcom Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#ifndef __BCM_SF2_H
13#define __BCM_SF2_H
14
15#include <linux/platform_device.h>
16#include <linux/kernel.h>
17#include <linux/io.h>
18#include <linux/spinlock.h>
19#include <linux/mutex.h>
20#include <linux/mii.h>
Florian Fainelli450b05c2014-09-24 17:05:22 -070021#include <linux/ethtool.h>
Florian Fainelli680060d2015-10-23 11:38:07 -070022#include <linux/types.h>
23#include <linux/bitops.h>
Florian Fainelli9c57a772016-06-09 17:42:08 -070024#include <linux/if_vlan.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070025
26#include <net/dsa.h>
27
28#include "bcm_sf2_regs.h"
Florian Fainellif4589952016-08-26 12:18:33 -070029#include "b53/b53_priv.h"
Florian Fainelli246d7f72014-08-27 17:04:56 -070030
31struct bcm_sf2_hw_params {
32 u16 top_rev;
33 u16 core_rev;
Florian Fainelliaa9aef72014-09-19 13:07:55 -070034 u16 gphy_rev;
Florian Fainelli246d7f72014-08-27 17:04:56 -070035 u32 num_gphy;
36 u8 num_acb_queue;
37 u8 num_rgmii;
38 u8 num_ports;
39 u8 fcb_pause_override:1;
40 u8 acb_packets_inflight:1;
41};
42
43#define BCM_SF2_REGS_NAME {\
44 "core", "reg", "intrl2_0", "intrl2_1", "fcb", "acb" \
45}
46
47#define BCM_SF2_REGS_NUM 6
48
49struct bcm_sf2_port_status {
50 unsigned int link;
Florian Fainelli450b05c2014-09-24 17:05:22 -070051
52 struct ethtool_eee eee;
Florian Fainelli246d7f72014-08-27 17:04:56 -070053};
54
55struct bcm_sf2_priv {
56 /* Base registers, keep those in order with BCM_SF2_REGS_NAME */
57 void __iomem *core;
58 void __iomem *reg;
59 void __iomem *intrl2_0;
60 void __iomem *intrl2_1;
61 void __iomem *fcb;
62 void __iomem *acb;
63
Florian Fainellia78e86e2017-01-20 12:36:29 -080064 /* Register offsets indirection tables */
65 u32 type;
66 const u16 *reg_offsets;
67 unsigned int core_reg_align;
68
Florian Fainelli246d7f72014-08-27 17:04:56 -070069 /* spinlock protecting access to the indirect registers */
70 spinlock_t indir_lock;
71
72 int irq0;
73 int irq1;
74 u32 irq0_stat;
75 u32 irq0_mask;
76 u32 irq1_stat;
77 u32 irq1_mask;
78
Florian Fainellif4589952016-08-26 12:18:33 -070079 /* Backing b53_device */
80 struct b53_device *dev;
81
Florian Fainelli246d7f72014-08-27 17:04:56 -070082 /* Mutex protecting access to the MIB counters */
83 struct mutex stats_mutex;
84
85 struct bcm_sf2_hw_params hw_params;
86
87 struct bcm_sf2_port_status port_sts[DSA_MAX_PORTS];
Florian Fainelli96e65d72014-09-18 17:31:25 -070088
89 /* Mask of ports enabled for Wake-on-LAN */
90 u32 wol_ports_mask;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -070091
92 /* MoCA port location */
93 int moca_port;
94
95 /* Bitmask of ports having an integrated PHY */
96 unsigned int int_phy_mask;
Florian Fainelli461cd1b02016-06-07 16:32:43 -070097
98 /* Master and slave MDIO bus controller */
99 unsigned int indir_phy_mask;
100 struct device_node *master_mii_dn;
101 struct mii_bus *slave_mii_bus;
102 struct mii_bus *master_mii_bus;
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800103
104 /* Bitmask of ports needing BRCM tags */
105 unsigned int brcm_tag_mask;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700106};
107
Florian Fainellif4589952016-08-26 12:18:33 -0700108static inline struct bcm_sf2_priv *bcm_sf2_to_priv(struct dsa_switch *ds)
109{
Vivien Didelot04bed142016-08-31 18:06:13 -0400110 struct b53_device *dev = ds->priv;
Florian Fainellif4589952016-08-26 12:18:33 -0700111
112 return dev->priv;
113}
114
Florian Fainellia78e86e2017-01-20 12:36:29 -0800115static inline u32 bcm_sf2_mangle_addr(struct bcm_sf2_priv *priv, u32 off)
116{
117 return off << priv->core_reg_align;
118}
119
Florian Fainelli246d7f72014-08-27 17:04:56 -0700120#define SF2_IO_MACRO(name) \
121static inline u32 name##_readl(struct bcm_sf2_priv *priv, u32 off) \
122{ \
123 return __raw_readl(priv->name + off); \
124} \
125static inline void name##_writel(struct bcm_sf2_priv *priv, \
126 u32 val, u32 off) \
127{ \
128 __raw_writel(val, priv->name + off); \
129} \
130
131/* Accesses to 64-bits register requires us to latch the hi/lo pairs
132 * using the REG_DIR_DATA_{READ,WRITE} ancillary registers. The 'indir_lock'
133 * spinlock is automatically grabbed and released to provide relative
134 * atomiticy with latched reads/writes.
135 */
136#define SF2_IO64_MACRO(name) \
137static inline u64 name##_readq(struct bcm_sf2_priv *priv, u32 off) \
138{ \
139 u32 indir, dir; \
140 spin_lock(&priv->indir_lock); \
Florian Fainelli329b5c52017-01-20 12:36:28 -0800141 dir = name##_readl(priv, off); \
Florian Fainelliddede6d2015-02-19 11:09:27 -0800142 indir = reg_readl(priv, REG_DIR_DATA_READ); \
Florian Fainelli246d7f72014-08-27 17:04:56 -0700143 spin_unlock(&priv->indir_lock); \
144 return (u64)indir << 32 | dir; \
145} \
Florian Fainelli03679a12015-09-08 20:06:41 -0700146static inline void name##_writeq(struct bcm_sf2_priv *priv, u64 val, \
147 u32 off) \
Florian Fainelli246d7f72014-08-27 17:04:56 -0700148{ \
149 spin_lock(&priv->indir_lock); \
150 reg_writel(priv, upper_32_bits(val), REG_DIR_DATA_WRITE); \
Florian Fainelli329b5c52017-01-20 12:36:28 -0800151 name##_writel(priv, lower_32_bits(val), off); \
Florian Fainelli246d7f72014-08-27 17:04:56 -0700152 spin_unlock(&priv->indir_lock); \
153}
154
155#define SWITCH_INTR_L2(which) \
156static inline void intrl2_##which##_mask_clear(struct bcm_sf2_priv *priv, \
157 u32 mask) \
158{ \
Florian Fainelli246d7f72014-08-27 17:04:56 -0700159 priv->irq##which##_mask &= ~(mask); \
Florian Fainelli4f101c42016-08-24 11:01:20 -0700160 intrl2_##which##_writel(priv, mask, INTRL2_CPU_MASK_CLEAR); \
Florian Fainelli246d7f72014-08-27 17:04:56 -0700161} \
162static inline void intrl2_##which##_mask_set(struct bcm_sf2_priv *priv, \
163 u32 mask) \
164{ \
165 intrl2_## which##_writel(priv, mask, INTRL2_CPU_MASK_SET); \
166 priv->irq##which##_mask |= (mask); \
167} \
168
Florian Fainellia78e86e2017-01-20 12:36:29 -0800169static inline u32 core_readl(struct bcm_sf2_priv *priv, u32 off)
170{
171 u32 tmp = bcm_sf2_mangle_addr(priv, off);
172 return __raw_readl(priv->core + tmp);
173}
174
175static inline void core_writel(struct bcm_sf2_priv *priv, u32 val, u32 off)
176{
177 u32 tmp = bcm_sf2_mangle_addr(priv, off);
178 __raw_writel(val, priv->core + tmp);
179}
180
181static inline u32 reg_readl(struct bcm_sf2_priv *priv, u16 off)
182{
183 return __raw_readl(priv->reg + priv->reg_offsets[off]);
184}
185
186static inline void reg_writel(struct bcm_sf2_priv *priv, u32 val, u16 off)
187{
188 __raw_writel(val, priv->reg + priv->reg_offsets[off]);
189}
190
Florian Fainelli246d7f72014-08-27 17:04:56 -0700191SF2_IO64_MACRO(core);
192SF2_IO_MACRO(intrl2_0);
193SF2_IO_MACRO(intrl2_1);
194SF2_IO_MACRO(fcb);
195SF2_IO_MACRO(acb);
196
197SWITCH_INTR_L2(0);
198SWITCH_INTR_L2(1);
199
200#endif /* __BCM_SF2_H */