aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/generic-2.6/files/drivers/net/phy/adm6996.c
blob: 972d20c6d956459c1acfad8e7a3e0ec56bbd8dd3 (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
/*
 * ADM6996 switch driver
 *
 * Copyright (c) 2008 Felix Fietkau <nbd@openwrt.org>
 *
 * 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
 */
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/unistd.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/spinlock.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/mii.h>
#include <linux/ethtool.h>
#include <linux/phy.h>

#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include "adm6996.h"

MODULE_DESCRIPTION("Infineon ADM6996 Switch");
MODULE_AUTHOR("Felix Fietkau");
MODULE_LICENSE("GPL");

struct adm6996_priv {
	/* use abstraction for regops, we want to add gpio support in the future */
	u16 (*read)(struct phy_device *phydev, enum admreg reg);
	void (*write)(struct phy_device *phydev, enum admreg reg, u16 val);
};

#define to_adm(_phy) ((struct adm6996_priv *) (_phy)->priv)


static inline u16
r16(struct phy_device *pdev, enum admreg reg)
{
	return to_adm(pdev)->read(pdev, reg);
}

static inline void
w16(struct phy_device *pdev, enum admreg reg, u16 val)
{
	to_adm(pdev)->write(pdev, reg, val);
}


static u16
adm6996_read_mii_reg(struct phy_device *phydev, enum admreg reg)
{
	return phydev->bus->read(phydev->bus, PHYADDR(reg));
}

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


static int adm6996_config_init(struct phy_device *pdev)
{
	int i;

	printk("%s: ADM6996 PHY driver attached.\n", pdev->attached_dev->name);
	pdev->supported = ADVERTISED_100baseT_Full;
	pdev->advertising = ADVERTISED_100baseT_Full;

	/* initialize port and vlan settings */
	for (i = 0; i < ADM_PHY_PORTS; i++) {
		w16(pdev, adm_portcfg[i], ADM_PORTCFG_INIT |
			ADM_PORTCFG_PVID((i == ADM_WAN_PORT) ? 1 : 0));
	}
	w16(pdev, adm_portcfg[5], ADM_PORTCFG_CPU);

	/* reset all ports */
	for (i = 0; i < ADM_PHY_PORTS; i++) {
		w16(pdev, ADM_PHY_PORT(i), ADM_PHYCFG_INIT);
	}

	return 0;
}

static int adm6996_read_status(struct phy_device *phydev)
{
	phydev->speed = SPEED_100;
	phydev->duplex = DUPLEX_FULL;
	phydev->state = PHY_UP;
	return 0;
}

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

static int adm6996_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(ADM_SIG0)) & ADM_SIG0_MASK;
	if (reg != ADM_SIG0_VAL)
		return 0;

	reg = bus->read(bus, PHYADDR(ADM_SIG1)) & ADM_SIG1_MASK;
	if (reg != ADM_SIG1_VAL)
		return 0;

	dev->phy_id = (ADM_SIG0_VAL << 16) | ADM_SIG1_VAL;
	return 0;
}

static int adm6996_probe(struct phy_device *pdev)
{
	struct adm6996_priv *priv;

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

	priv->read = adm6996_read_mii_reg;
	priv->write = adm6996_write_mii_reg;
	pdev->priv = priv;
	return 0;
}

static void adm6996_remove(struct phy_device *pdev)
{
	kfree(pdev->priv);
}


static struct phy_driver adm6996_driver = {
	.name		= "Infineon ADM6996",
	.phy_id		= (ADM_SIG0_VAL << 16) | ADM_SIG1_VAL,
	.phy_id_mask	= 0xffffffff,
	.features	= PHY_BASIC_FEATURES,
	.probe		= adm6996_probe,
	.remove		= adm6996_remove,
	.config_init	= &adm6996_config_init,
	.config_aneg	= &adm6996_config_aneg,
	.read_status	= &adm6996_read_status,
	.driver		= { .owner = THIS_MODULE,},
};

static int __init adm6996_init(void)
{
	phy_register_fixup_for_id(PHY_ANY_ID, adm6996_fixup);
	return phy_driver_register(&adm6996_driver);
}

static void __exit adm6996_exit(void)
{
	phy_driver_unregister(&adm6996_driver);
}

module_init(adm6996_init);
module_exit(adm6996_exit);