blob: 732bda1d3bb2b4ce36abbc7c38b46d490ba6a84c [file] [log] [blame]
developerfd40db22021-04-29 10:08:25 +08001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (c) 2018 MediaTek Inc.
4 * Author: Weijie Gao <weijie.gao@mediatek.com>
5 */
6
7#ifndef _MT753X_H_
8#define _MT753X_H_
9
10#include <linux/list.h>
11#include <linux/mutex.h>
12#include <linux/netdevice.h>
13#include <linux/of_mdio.h>
14#include <linux/workqueue.h>
15#include <linux/gpio/consumer.h>
16
17#ifdef CONFIG_SWCONFIG
18#include <linux/switch.h>
19#endif
20
21#include "mt753x_vlan.h"
22
23#define MT753X_DFL_CPU_PORT 6
24#define MT753X_NUM_PHYS 5
25
26#define MT753X_DFL_SMI_ADDR 0x1f
27#define MT753X_SMI_ADDR_MASK 0x1f
28
29struct gsw_mt753x;
30
31enum mt753x_model {
32 MT7530 = 0x7530,
33 MT7531 = 0x7531
34};
35
36struct mt753x_port_cfg {
37 struct device_node *np;
38 int phy_mode;
39 u32 enabled: 1;
40 u32 force_link: 1;
41 u32 speed: 2;
42 u32 duplex: 1;
43 bool ssc_on;
44 bool stag_on;
45};
46
47struct mt753x_phy {
48 struct gsw_mt753x *gsw;
49 struct net_device netdev;
50 struct phy_device *phydev;
51};
52
53struct gsw_mt753x {
54 u32 id;
55
56 struct device *dev;
57 struct mii_bus *host_bus;
58 struct mii_bus *gphy_bus;
59 struct mutex mii_lock; /* MII access lock */
60 u32 smi_addr;
61 u32 phy_base;
62 int direct_phy_access;
63
64 enum mt753x_model model;
65 const char *name;
66
67 struct mt753x_port_cfg port5_cfg;
68 struct mt753x_port_cfg port6_cfg;
69
70 bool hw_phy_cal;
71 bool phy_status_poll;
72 struct mt753x_phy phys[MT753X_NUM_PHYS];
73// int phy_irqs[PHY_MAX_ADDR]; //FIXME
74
75 int phy_link_sts;
76
77 int irq;
78 int reset_pin;
79 struct work_struct irq_worker;
80
81#ifdef CONFIG_SWCONFIG
82 struct switch_dev swdev;
83 u32 cpu_port;
84#endif
85
86 int global_vlan_enable;
87 struct mt753x_vlan_entry vlan_entries[MT753X_NUM_VLANS];
88 struct mt753x_port_entry port_entries[MT753X_NUM_PORTS];
89
90 int (*mii_read)(struct gsw_mt753x *gsw, int phy, int reg);
91 void (*mii_write)(struct gsw_mt753x *gsw, int phy, int reg, u16 val);
92
93 int (*mmd_read)(struct gsw_mt753x *gsw, int addr, int devad, u16 reg);
94 void (*mmd_write)(struct gsw_mt753x *gsw, int addr, int devad, u16 reg,
95 u16 val);
96
97 struct list_head list;
98};
99
100struct chip_rev {
101 const char *name;
102 u32 rev;
103};
104
105struct mt753x_sw_id {
106 enum mt753x_model model;
107 int (*detect)(struct gsw_mt753x *gsw, struct chip_rev *crev);
108 int (*init)(struct gsw_mt753x *gsw);
109 int (*post_init)(struct gsw_mt753x *gsw);
110};
111
112extern struct list_head mt753x_devs;
113
114struct gsw_mt753x *mt753x_get_gsw(u32 id);
115struct gsw_mt753x *mt753x_get_first_gsw(void);
116void mt753x_put_gsw(void);
117void mt753x_lock_gsw(void);
118
119u32 mt753x_reg_read(struct gsw_mt753x *gsw, u32 reg);
120void mt753x_reg_write(struct gsw_mt753x *gsw, u32 reg, u32 val);
121
122int mt753x_mii_read(struct gsw_mt753x *gsw, int phy, int reg);
123void mt753x_mii_write(struct gsw_mt753x *gsw, int phy, int reg, u16 val);
124
125int mt753x_mmd_read(struct gsw_mt753x *gsw, int addr, int devad, u16 reg);
126void mt753x_mmd_write(struct gsw_mt753x *gsw, int addr, int devad, u16 reg,
127 u16 val);
128
129int mt753x_mmd_ind_read(struct gsw_mt753x *gsw, int addr, int devad, u16 reg);
130void mt753x_mmd_ind_write(struct gsw_mt753x *gsw, int addr, int devad, u16 reg,
131 u16 val);
132
133int mt753x_tr_read(struct gsw_mt753x *gsw, int addr, u8 ch, u8 node, u8 daddr);
134void mt753x_tr_write(struct gsw_mt753x *gsw, int addr, u8 ch, u8 node, u8 daddr,
135 u32 data);
136
137void mt753x_irq_worker(struct work_struct *work);
138void mt753x_irq_enable(struct gsw_mt753x *gsw);
139
140int mt753x_phy_calibration(struct gsw_mt753x *gsw, u8 phyaddr);
141int extphy_init(struct gsw_mt753x *gsw, int addr);
142
143/* MDIO Indirect Access Registers */
144#define MII_MMD_ACC_CTL_REG 0x0d
145#define MMD_CMD_S 14
146#define MMD_CMD_M 0xc000
147#define MMD_DEVAD_S 0
148#define MMD_DEVAD_M 0x1f
149
150/* MMD_CMD: MMD commands */
151#define MMD_ADDR 0
152#define MMD_DATA 1
153
154#define MII_MMD_ADDR_DATA_REG 0x0e
155
156/* Procedure of MT753x Internal Register Access
157 *
158 * 1. Internal Register Address
159 *
160 * The MT753x has a 16-bit register address and each register is 32-bit.
161 * This means the lowest two bits are not used as the register address is
162 * 4-byte aligned.
163 *
164 * Rest of the valid bits are divided into two parts:
165 * Bit 15..6 is the Page address
166 * Bit 5..2 is the low address
167 *
168 * -------------------------------------------------------------------
169 * | 15 14 13 12 11 10 9 8 7 6 | 5 4 3 2 | 1 0 |
170 * |----------------------------------------|---------------|--------|
171 * | Page Address | Address | Unused |
172 * -------------------------------------------------------------------
173 *
174 * 2. MDIO access timing
175 *
176 * The MT753x uses the following MDIO timing for a single register read
177 *
178 * Phase 1: Write Page Address
179 * -------------------------------------------------------------------
180 * | ST | OP | PHY_ADDR | TYPE | RSVD | TA | RSVD | PAGE_ADDR |
181 * -------------------------------------------------------------------
182 * | 01 | 01 | 11111 | 1 | 1111 | xx | 00000 | REG_ADDR[15..6] |
183 * -------------------------------------------------------------------
184 *
185 * Phase 2: Write low Address & Read low word
186 * -------------------------------------------------------------------
187 * | ST | OP | PHY_ADDR | TYPE | LOW_ADDR | TA | DATA |
188 * -------------------------------------------------------------------
189 * | 01 | 10 | 11111 | 0 | REG_ADDR[5..2] | xx | DATA[15..0] |
190 * -------------------------------------------------------------------
191 *
192 * Phase 3: Read high word
193 * -------------------------------------------------------------------
194 * | ST | OP | PHY_ADDR | TYPE | RSVD | TA | DATA |
195 * -------------------------------------------------------------------
196 * | 01 | 10 | 11111 | 1 | 0000 | xx | DATA[31..16] |
197 * -------------------------------------------------------------------
198 *
199 * The MT753x uses the following MDIO timing for a single register write
200 *
201 * Phase 1: Write Page Address (The same as read)
202 *
203 * Phase 2: Write low Address and low word
204 * -------------------------------------------------------------------
205 * | ST | OP | PHY_ADDR | TYPE | LOW_ADDR | TA | DATA |
206 * -------------------------------------------------------------------
207 * | 01 | 01 | 11111 | 0 | REG_ADDR[5..2] | xx | DATA[15..0] |
208 * -------------------------------------------------------------------
209 *
210 * Phase 3: write high word
211 * -------------------------------------------------------------------
212 * | ST | OP | PHY_ADDR | TYPE | RSVD | TA | DATA |
213 * -------------------------------------------------------------------
214 * | 01 | 01 | 11111 | 1 | 0000 | xx | DATA[31..16] |
215 * -------------------------------------------------------------------
216 *
217 */
218
219/* Internal Register Address fields */
220#define MT753X_REG_PAGE_ADDR_S 6
221#define MT753X_REG_PAGE_ADDR_M 0xffc0
222#define MT753X_REG_ADDR_S 2
223#define MT753X_REG_ADDR_M 0x3c
224#endif /* _MT753X_H_ */