aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/generic/files/drivers/net/phy/psb6970.c
blob: 2fcd29901330f3f959509ccb35dc122ac5660241 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
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
430
431
432
433
434
435
436
437
438
/*
 * Lantiq PSB6970 (Tantos) Switch driver
 *
 * Copyright (c) 2009,2010 Team Embedded.
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of the GNU General Public License v2 as published by the
 * Free Software Foundation.
 *
 * The switch programming done in this driver follows the 
 * "Ethernet Traffic Separation using VLAN" Application Note as
 * published by Lantiq.
 */

#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/switch.h>
#include <linux/phy.h>

#define PSB6970_MAX_VLANS		16
#define PSB6970_NUM_PORTS		7
#define PSB6970_DEFAULT_PORT_CPU	6
#define PSB6970_IS_CPU_PORT(x)		((x) > 4)

#define PHYADDR(_reg)		((_reg >> 5) & 0xff), (_reg & 0x1f)

/* --- Identification --- */
#define PSB6970_CI0		0x0100
#define PSB6970_CI0_MASK	0x000f
#define PSB6970_CI1		0x0101
#define PSB6970_CI1_VAL		0x2599
#define PSB6970_CI1_MASK	0xffff

/* --- VLAN filter table --- */
#define PSB6970_VFxL(i)		((i)*2+0x10)	/* VLAN Filter Low */
#define PSB6970_VFxL_VV		(1 << 15)	/* VLAN_Valid */

#define PSB6970_VFxH(i)		((i)*2+0x11)	/* VLAN Filter High */
#define PSB6970_VFxH_TM_SHIFT	7		/* Tagged Member */

/* --- Port registers --- */
#define PSB6970_EC(p)		((p)*0x20+2)	/* Extended Control */
#define PSB6970_EC_IFNTE	(1 << 1)	/* Input Force No Tag Enable */

#define PSB6970_PBVM(p)		((p)*0x20+3)	/* Port Base VLAN Map */
#define PSB6970_PBVM_VMCE	(1 << 8)
#define PSB6970_PBVM_AOVTP	(1 << 9)
#define PSB6970_PBVM_VSD	(1 << 10)
#define PSB6970_PBVM_VC		(1 << 11)	/* VID Check with VID table */
#define PSB6970_PBVM_TBVE	(1 << 13)	/* Tag-Based VLAN enable */

#define PSB6970_DVID(p)		((p)*0x20+4)	/* Default VLAN ID & Priority */

struct psb6970_priv {
	struct switch_dev dev;
	struct phy_device *phy;
	u16 (*read) (struct phy_device* phydev, int reg);
	void (*write) (struct phy_device* phydev, int reg, u16 val);
	struct mutex reg_mutex;

	/* all fields below are cleared on reset */
	bool vlan;
	u16 vlan_id[PSB6970_MAX_VLANS];
	u8 vlan_table[PSB6970_MAX_VLANS];
	u8 vlan_tagged;
	u16 pvid[PSB6970_NUM_PORTS];
};

#define to_psb6970(_dev) container_of(_dev, struct psb6970_priv, dev)

static u16 psb6970_mii_read(struct phy_device *phydev, int reg)
{
	return phydev->bus->read(phydev->bus, PHYADDR(reg));
}

static void psb6970_mii_write(struct phy_device *phydev, int reg, u16 val)
{
	phydev->bus->write(phydev->bus, PHYADDR(reg), val);
}

static int
psb6970_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
		 struct switch_val *val)
{
	struct psb6970_priv *priv = to_psb6970(dev);
	priv->vlan = !!val->value.i;
	return 0;
}

static int
psb6970_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
		 struct switch_val *val)
{
	struct psb6970_priv *priv = to_psb6970(dev);
	val->value.i = priv->vlan;
	return 0;
}

static int psb6970_set_pvid(struct switch_dev *dev, int port, int vlan)
{
	struct psb6970_priv *priv = to_psb6970(dev);

	/* make sure no invalid PVIDs get set */
	if (vlan >= dev->vlans)
		return -EINVAL;

	priv->pvid[port] = vlan;
	return 0;
}

static int psb6970_get_pvid(struct switch_dev *dev, int port, int *vlan)
{
	struct psb6970_priv *priv = to_psb6970(dev);
	*vlan = priv->pvid[port];
	return 0;
}

static int
psb6970_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
		struct switch_val *val)
{
	struct psb6970_priv *priv = to_psb6970(dev);
	priv->vlan_id[val->port_vlan] = val->value.i;
	return 0;
}

static int
psb6970_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
		struct switch_val *val)
{
	struct psb6970_priv *priv = to_psb6970(dev);
	val->value.i = priv->vlan_id[val->port_vlan];
	return 0;
}

static struct switch_attr psb6970_globals[] = {
	{
	 .type = SWITCH_TYPE_INT,
	 .name = "enable_vlan",
	 .description = "Enable VLAN mode",
	 .set = psb6970_set_vlan,
	 .get = psb6970_get_vlan,
	 .max = 1},
};

static struct switch_attr psb6970_port[] = {
};

static struct switch_attr psb6970_vlan[] = {
	{
	 .type = SWITCH_TYPE_INT,
	 .name = "vid",
	 .description = "VLAN ID (0-4094)",
	 .set = psb6970_set_vid,
	 .get = psb6970_get_vid,
	 .max = 4094,
	 },
};

static int psb6970_get_ports(struct switch_dev *dev, struct switch_val *val)
{
	struct psb6970_priv *priv = to_psb6970(dev);
	u8 ports = priv->vlan_table[val->port_vlan];
	int i;

	val->len = 0;
	for (i = 0; i < PSB6970_NUM_PORTS; i++) {
		struct switch_port *p;

		if (!(ports & (1 << i)))
			continue;

		p = &val->value.ports[val->len++];
		p->id = i;
		if (priv->vlan_tagged & (1 << i))
			p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
		else
			p->flags = 0;
	}
	return 0;
}

static int psb6970_set_ports(struct switch_dev *dev, struct switch_val *val)
{
	struct psb6970_priv *priv = to_psb6970(dev);
	u8 *vt = &priv->vlan_table[val->port_vlan];
	int i, j;

	*vt = 0;
	for (i = 0; i < val->len; i++) {
		struct switch_port *p = &val->value.ports[i];

		if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
			priv->vlan_tagged |= (1 << p->id);
		else {
			priv->vlan_tagged &= ~(1 << p->id);
			priv->pvid[p->id] = val->port_vlan;

			/* make sure that an untagged port does not
			 * appear in other vlans */
			for (j = 0; j < PSB6970_MAX_VLANS; j++) {
				if (j == val->port_vlan)
					continue;
				priv->vlan_table[j] &= ~(1 << p->id);
			}
		}

		*vt |= 1 << p->id;
	}
	return 0;
}

static int psb6970_hw_apply(struct switch_dev *dev)
{
	struct psb6970_priv *priv = to_psb6970(dev);
	int i, j;

	mutex_lock(&priv->reg_mutex);

	if (priv->vlan) {
		/* into the vlan translation unit */
		for (j = 0; j < PSB6970_MAX_VLANS; j++) {
			u8 vp = priv->vlan_table[j];

			if (vp) {
				priv->write(priv->phy, PSB6970_VFxL(j),
					    PSB6970_VFxL_VV | priv->vlan_id[j]);
				priv->write(priv->phy, PSB6970_VFxH(j),
					    ((vp & priv->
					      vlan_tagged) <<
					     PSB6970_VFxH_TM_SHIFT) | vp);
			} else	/* clear VLAN Valid flag for unused vlans */
				priv->write(priv->phy, PSB6970_VFxL(j), 0);

		}
	}

	/* update the port destination mask registers and tag settings */
	for (i = 0; i < PSB6970_NUM_PORTS; i++) {
		int dvid = 1, pbvm = 0x7f | PSB6970_PBVM_VSD, ec = 0;

		if (priv->vlan) {
			ec = PSB6970_EC_IFNTE;
			dvid = priv->vlan_id[priv->pvid[i]];
			pbvm |= PSB6970_PBVM_TBVE | PSB6970_PBVM_VMCE;

			if ((i << 1) & priv->vlan_tagged)
				pbvm |= PSB6970_PBVM_AOVTP | PSB6970_PBVM_VC;
		}

		priv->write(priv->phy, PSB6970_PBVM(i), pbvm);

		if (!PSB6970_IS_CPU_PORT(i)) {
			priv->write(priv->phy, PSB6970_EC(i), ec);
			priv->write(priv->phy, PSB6970_DVID(i), dvid);
		}
	}

	mutex_unlock(&priv->reg_mutex);
	return 0;
}

static int psb6970_reset_switch(struct switch_dev *dev)
{
	struct psb6970_priv *priv = to_psb6970(dev);
	int i;

	mutex_lock(&priv->reg_mutex);

	memset(&priv->vlan, 0, sizeof(struct psb6970_priv) -
	       offsetof(struct psb6970_priv, vlan));

	for (i = 0; i < PSB6970_MAX_VLANS; i++)
		priv->vlan_id[i] = i;

	mutex_unlock(&priv->reg_mutex);

	return psb6970_hw_apply(dev);
}

static const struct switch_dev_ops psb6970_ops = {
	.attr_global = {
			.attr = psb6970_globals,
			.n_attr = ARRAY_SIZE(psb6970_globals),
			},
	.attr_port = {
		      .attr = psb6970_port,
		      .n_attr = ARRAY_SIZE(psb6970_port),
		      },
	.attr_vlan = {
		      .attr = psb6970_vlan,
		      .n_attr = ARRAY_SIZE(psb6970_vlan),
		      },
	.get_port_pvid = psb6970_get_pvid,
	.set_port_pvid = psb6970_set_pvid,
	.get_vlan_ports = psb6970_get_ports,
	.set_vlan_ports = psb6970_set_ports,
	.apply_config = psb6970_hw_apply,
	.reset_switch = psb6970_reset_switch,
};

static int psb6970_config_init(struct phy_device *pdev)
{
	struct psb6970_priv *priv;
	struct net_device *dev = pdev->attached_dev;
	struct switch_dev *swdev;
	int ret;

	priv = kzalloc(sizeof(struct psb6970_priv), GFP_KERNEL);
	if (priv == NULL)
		return -ENOMEM;

	priv->phy = pdev;

	if (pdev->addr == 0)
		printk(KERN_INFO "%s: psb6970 switch driver attached.\n",
		       pdev->attached_dev->name);

	if (pdev->addr != 0) {
		kfree(priv);
		return 0;
	}

	pdev->supported = pdev->advertising = SUPPORTED_100baseT_Full;

	mutex_init(&priv->reg_mutex);
	priv->read = psb6970_mii_read;
	priv->write = psb6970_mii_write;

	pdev->priv = priv;

	swdev = &priv->dev;
	swdev->cpu_port = PSB6970_DEFAULT_PORT_CPU;
	swdev->ops = &psb6970_ops;

	swdev->name = "Lantiq PSB6970";
	swdev->vlans = PSB6970_MAX_VLANS;
	swdev->ports = PSB6970_NUM_PORTS;

	if ((ret = register_switch(&priv->dev, pdev->attached_dev)) < 0) {
		kfree(priv);
		goto done;
	}

	ret = psb6970_reset_switch(&priv->dev);
	if (ret) {
		kfree(priv);
		goto done;
	}

	dev->phy_ptr = priv;

done:
	return ret;
}

static int psb6970_read_status(struct phy_device *phydev)
{
	phydev->speed = SPEED_100;
	phydev->duplex = DUPLEX_FULL;
	phydev->link = 1;

	phydev->state = PHY_RUNNING;
	netif_carrier_on(phydev->attached_dev);
	phydev->adjust_link(phydev->attached_dev);

	return 0;
}

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

static int psb6970_probe(struct phy_device *pdev)
{
	return 0;
}

static void psb6970_remove(struct phy_device *pdev)
{
	struct psb6970_priv *priv = pdev->priv;

	if (!priv)
		return;

	if (pdev->addr == 0)
		unregister_switch(&priv->dev);
	kfree(priv);
}

static int psb6970_fixup(struct phy_device *dev)
{
	struct mii_bus *bus = dev->bus;
	u16 reg;

	/* look for the switch on the bus */
	reg = bus->read(bus, PHYADDR(PSB6970_CI1)) & PSB6970_CI1_MASK;
	if (reg != PSB6970_CI1_VAL)
		return 0;

	dev->phy_id = (reg << 16);
	dev->phy_id |= bus->read(bus, PHYADDR(PSB6970_CI0)) & PSB6970_CI0_MASK;

	return 0;
}

static struct phy_driver psb6970_driver = {
	.name = "Lantiq PSB6970",
	.phy_id = PSB6970_CI1_VAL << 16,
	.phy_id_mask = 0xffff0000,
	.features = PHY_BASIC_FEATURES,
	.probe = psb6970_probe,
	.remove = psb6970_remove,
	.config_init = &psb6970_config_init,
	.config_aneg = &psb6970_config_aneg,
	.read_status = &psb6970_read_status,
	.driver = {.owner = THIS_MODULE},
};

int __init psb6970_init(void)
{
	phy_register_fixup_for_id(PHY_ANY_ID, psb6970_fixup);
	return phy_driver_register(&psb6970_driver);
}

module_init(psb6970_init);

void __exit psb6970_exit(void)
{
	phy_driver_unregister(&psb6970_driver);
}

module_exit(psb6970_exit);

MODULE_DESCRIPTION("Lantiq PSB6970 Switch");
MODULE_AUTHOR("Ithamar R. Adema <ithamar.adema@team-embedded.nl>");
MODULE_LICENSE("GPL");