summaryrefslogtreecommitdiffstats
path: root/drivers/net/phy/ar8327.c
blob: 5f3a2e2cf2dc2deef1482bb160c51c1a4a370866 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2017 Oleksij Rempel <linux@rempel-privat.de>
 */

#include <common.h>
#include <init.h>
#include <linux/phy.h>
#include <linux/string.h>

#define ATHR_PHY_MAX	5

/*****************/
/* PHY Registers */
/*****************/
#define ATHR_PHY_CONTROL			0x00
#define ATHR_PHY_STATUS				0x01
#define ATHR_PHY_ID1				0x02
#define ATHR_PHY_ID2				0x03
#define ATHR_AUTONEG_ADVERT			0x04
#define ATHR_LINK_PARTNER_ABILITY		0x05
#define ATHR_AUTONEG_EXPANSION			0x06
#define ATHR_NEXT_PAGE_TRANSMIT			0x07
#define ATHR_LINK_PARTNER_NEXT_PAGE		0x08
#define ATHR_1000BASET_CONTROL			0x09
#define ATHR_1000BASET_STATUS			0x0a
#define ATHR_PHY_SPEC_CONTROL			0x10
#define ATHR_PHY_SPEC_STATUS			0x11
#define ATHR_DEBUG_PORT_ADDRESS			0x1d
#define ATHR_DEBUG_PORT_DATA			0x1e

/* Advertisement register. */
#define ATHR_ADVERTISE_ASYM_PAUSE		0x0800
#define ATHR_ADVERTISE_PAUSE			0x0400
#define ATHR_ADVERTISE_100FULL			0x0100
#define ATHR_ADVERTISE_100HALF			0x0080
#define ATHR_ADVERTISE_10FULL			0x0040
#define ATHR_ADVERTISE_10HALF			0x0020

#define ATHR_ADVERTISE_ALL (ATHR_ADVERTISE_ASYM_PAUSE | ATHR_ADVERTISE_PAUSE | \
                            ATHR_ADVERTISE_10HALF | ATHR_ADVERTISE_10FULL | \
                            ATHR_ADVERTISE_100HALF | ATHR_ADVERTISE_100FULL)

/* ATHR_PHY_CONTROL fields */
#define ATHR_CTRL_SOFTWARE_RESET		0x8000
#define ATHR_CTRL_AUTONEGOTIATION_ENABLE	0x1000

/* 1000BASET_CONTROL */
#define ATHR_ADVERTISE_1000FULL			0x0200

/* Phy Specific status fields */
#define ATHR_STATUS_LINK_PASS			0x0400

static u32 ar8327n_reg_read(struct phy_device *phydev, u32 reg_addr)
{
	u32 reg_word_addr;
	u32 phy_addr, tmp_val, reg_val;
	u16 phy_val;
	u8 phy_reg;

	/* change reg_addr to 16-bit word address, 32-bit aligned */
	reg_word_addr = (reg_addr & 0xfffffffc) >> 1;

	/* configure register high address */
	phy_addr = 0x18;
	phy_reg = 0x0;
	phy_val = (u16) ((reg_word_addr >> 8) & 0x1ff);  /* bit16-8 of reg address */
	mdiobus_write(phydev->bus,  phy_addr, phy_reg, phy_val);

	/* For some registers such as MIBs, since it is read/clear, we should */
	/* read the lower 16-bit register then the higher one */

	/* read register in lower address */
	phy_addr = 0x10 | ((reg_word_addr >> 5) & 0x7); /* bit7-5 of reg address */
	phy_reg = (u8) (reg_word_addr & 0x1f);   /* bit4-0 of reg address */
	reg_val = (u32) mdiobus_read(phydev->bus, phy_addr, phy_reg);

	/* read register in higher address */
	reg_word_addr++;
	phy_addr = 0x10 | ((reg_word_addr >> 5) & 0x7); /* bit7-5 of reg address */
	phy_reg = (u8) (reg_word_addr & 0x1f);   /* bit4-0 of reg address */
	tmp_val = (u32) mdiobus_read(phydev->bus, phy_addr, phy_reg);
	reg_val |= (tmp_val << 16);

	return reg_val;
}

static void ar8327n_reg_write(struct phy_device *phydev, u32 reg_addr,
			      u32 reg_val)
{
	u32 reg_word_addr;
	u32 phy_addr;
	u16 phy_val;
	u8 phy_reg;

	/* change reg_addr to 16-bit word address, 32-bit aligned */
	reg_word_addr = (reg_addr & 0xfffffffc) >> 1;

	/* configure register high address */
	phy_addr = 0x18;
	phy_reg = 0x0;
	phy_val = (u16) ((reg_word_addr >> 8) & 0x1ff);  /* bit16-8 of reg address */
	mdiobus_write(phydev->bus,  phy_addr, phy_reg, phy_val);

	/* For some registers such as ARL and VLAN, since they include BUSY bit */
	/* in lower address, we should write the higher 16-bit register then the */
	/* lower one */

	/* read register in higher address */
	reg_word_addr++;
	phy_addr = 0x10 | ((reg_word_addr >> 5) & 0x7); /* bit7-5 of reg address */
	phy_reg = (u8) (reg_word_addr & 0x1f);   /* bit4-0 of reg address */
	phy_val = (u16) ((reg_val >> 16) & 0xffff);
	mdiobus_write(phydev->bus,  phy_addr, phy_reg, phy_val);

	/* write register in lower address */
	reg_word_addr--;
	phy_addr = 0x10 | ((reg_word_addr >> 5) & 0x7); /* bit7-5 of reg address */
	phy_reg = (u8) (reg_word_addr & 0x1f);   /* bit4-0 of reg address */
	phy_val = (u16) (reg_val & 0xffff);
	mdiobus_write(phydev->bus,  phy_addr, phy_reg, phy_val);
}

static int ar8327n_phy_is_link_alive(struct phy_device *phydev, int phy_addr)
{
	u16 val;

	val = mdiobus_read(phydev->bus, phy_addr, ATHR_PHY_SPEC_STATUS);

	return !!(val & ATHR_STATUS_LINK_PASS);
}

static int ar8327n_phy_setup(struct phy_device *phydev)
{
	struct device_d *dev = &phydev->dev;
	int phy_addr;

	/* start auto negotiation on each phy */
	for (phy_addr = 0; phy_addr < ATHR_PHY_MAX; phy_addr++) {
		mdiobus_write(phydev->bus, phy_addr, ATHR_AUTONEG_ADVERT,
			      ATHR_ADVERTISE_ALL);

		mdiobus_write(phydev->bus, phy_addr, ATHR_1000BASET_CONTROL,
			      ATHR_ADVERTISE_1000FULL);

		/* Reset PHYs*/
		mdiobus_write(phydev->bus, phy_addr, ATHR_PHY_CONTROL,
			      ATHR_CTRL_AUTONEGOTIATION_ENABLE
			      | ATHR_CTRL_SOFTWARE_RESET);
	}

	/*
	 * After the phy is reset, it takes a little while before
	 * it can respond properly.
	 */
	mdelay(1000);

	for (phy_addr = 0; phy_addr < ATHR_PHY_MAX; phy_addr++) {
		int count;

		for (count = 20; count > 0; count--) {
			u16 val;
			val = mdiobus_read(phydev->bus, phy_addr,
					   ATHR_PHY_CONTROL);

			if (!(val & ATHR_CTRL_SOFTWARE_RESET))
				break;

			mdelay(150);
		}

		if (!count) {
			dev_err(dev, "error: port %d, negotiation timeout.\n",
				phy_addr);
			return -ETIMEDOUT;
		}
	}

	return 0;
}

static int ar8327n_get_link(struct phy_device *phydev)
{
	int phy_addr;
	int live_links = 0;

	for (phy_addr = 0; phy_addr < ATHR_PHY_MAX; phy_addr++) {
		if (ar8327n_phy_is_link_alive(phydev, phy_addr))
			live_links++;
	}

	return (live_links > 0);
}

static int ar8327n_config_init(struct phy_device *phydev)
{
	struct device_d *dev = &phydev->dev;
	int phy_addr = 0;

	if (phydev->interface != PHY_INTERFACE_MODE_RGMII_TXID)
		return 0;

	/* if using header for register configuration, we have to     */
	/* configure s17 register after frame transmission is enabled */

	/* configure the RGMII */
	ar8327n_reg_write(phydev, 0x624, 0x7f7f7f7f);
	ar8327n_reg_write(phydev, 0x10, 0x40000000);
	ar8327n_reg_write(phydev, 0x4, 0x07600000);
	ar8327n_reg_write(phydev, 0xc, 0x01000000);
	ar8327n_reg_write(phydev, 0x7c, 0x0000007e);

	/* AR8327/AR8328 v1.0 fixup */
	if ((ar8327n_reg_read(phydev, 0x0) & 0xffff) == 0x1201) {
		dev_warn(dev, "warning: untested device. PHY v1.0\n");
		for (phy_addr = 0x0; phy_addr <= ATHR_PHY_MAX; phy_addr++) {
			/* For 100M waveform */
			mdiobus_write(phydev->bus, phy_addr, 0x1d, 0x0);
			mdiobus_write(phydev->bus, phy_addr, 0x1e, 0x02ea);
			/* Turn On Gigabit Clock */
			mdiobus_write(phydev->bus, phy_addr, 0x1d, 0x3d);
			mdiobus_write(phydev->bus, phy_addr, 0x1e, 0x68a0);
		}
	}

	/*
	 * set the WAN Port(Port1) Disable Mode so
	 * it can not receive or transmit any frames.
	 */
	ar8327n_reg_write(phydev, 0x066c,
			  ar8327n_reg_read(phydev, 0x066c) & 0xfff8ffff);

	ar8327n_phy_setup(phydev);

	return 0;
}

static int ar8327n_read_status(struct phy_device *phydev)
{
	/* for GMAC0 we have only one static mode */
	phydev->speed = SPEED_1000;
	phydev->duplex = DUPLEX_FULL;
	phydev->pause = phydev->asym_pause = 0;
	phydev->link = ar8327n_get_link(phydev);
	return 0;
}

static int ar8327n_config_aneg(struct phy_device *phydev)
{
	return 0;
}

static int ar8327n_aneg_done(struct phy_device *phydev)
{
	return BMSR_ANEGCOMPLETE;
}

static struct phy_driver ar8327n_driver[] = {
{
	/* QCA AR8327N */
	.phy_id		= 0x004dd034,
	.phy_id_mask	= 0xffffffef,
	.drv.name	= "QCA AR8327N switch",
	.config_init	= ar8327n_config_init,
	.features	= PHY_GBIT_FEATURES,
	.config_aneg	= &ar8327n_config_aneg,
	.read_status	= &ar8327n_read_status,
	.aneg_done	= &ar8327n_aneg_done,
}};

static int atheros_phy_init(void)
{
	return phy_drivers_register(ar8327n_driver,
				    ARRAY_SIZE(ar8327n_driver));
}
fs_initcall(atheros_phy_init);