summaryrefslogtreecommitdiff
path: root/drivers/net/usb/r815x.c
blob: 852392269718e1892119a3c42d45c635f768a77f (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
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/mii.h>
#include <linux/usb.h>
#include <linux/usb/cdc.h>
#include <linux/usb/usbnet.h>

#define RTL815x_REQT_READ	0xc0
#define RTL815x_REQT_WRITE	0x40
#define RTL815x_REQ_GET_REGS	0x05
#define RTL815x_REQ_SET_REGS	0x05

#define MCU_TYPE_PLA		0x0100
#define OCP_BASE		0xe86c
#define BASE_MII		0xa400

#define BYTE_EN_DWORD		0xff
#define BYTE_EN_WORD		0x33
#define BYTE_EN_BYTE		0x11

#define R815x_PHY_ID		32
#define REALTEK_VENDOR_ID	0x0bda


static int pla_read_word(struct usb_device *udev, u16 index)
{
	int data, ret;
	u8 shift = index & 2;
	__le32 ocp_data;

	index &= ~3;

	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
			      RTL815x_REQ_GET_REGS, RTL815x_REQT_READ,
			      index, MCU_TYPE_PLA, &ocp_data, sizeof(ocp_data),
			      500);
	if (ret < 0)
		return ret;

	data = __le32_to_cpu(ocp_data);
	data >>= (shift * 8);
	data &= 0xffff;

	return data;
}

static int pla_write_word(struct usb_device *udev, u16 index, u32 data)
{
	__le32 ocp_data;
	u32 mask = 0xffff;
	u16 byen = BYTE_EN_WORD;
	u8 shift = index & 2;
	int ret;

	data &= mask;

	if (shift) {
		byen <<= shift;
		mask <<= (shift * 8);
		data <<= (shift * 8);
		index &= ~3;
	}

	ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
			      RTL815x_REQ_GET_REGS, RTL815x_REQT_READ,
			      index, MCU_TYPE_PLA, &ocp_data, sizeof(ocp_data),
			      500);
	if (ret < 0)
		return ret;

	data |= __le32_to_cpu(ocp_data) & ~mask;
	ocp_data = __cpu_to_le32(data);

	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
			      RTL815x_REQ_SET_REGS, RTL815x_REQT_WRITE,
			      index, MCU_TYPE_PLA | byen, &ocp_data,
			      sizeof(ocp_data), 500);

	return ret;
}

static int ocp_reg_read(struct usbnet *dev, u16 addr)
{
	u16 ocp_base, ocp_index;
	int ret;

	ocp_base = addr & 0xf000;
	ret = pla_write_word(dev->udev, OCP_BASE, ocp_base);
	if (ret < 0)
		goto out;

	ocp_index = (addr & 0x0fff) | 0xb000;
	ret = pla_read_word(dev->udev, ocp_index);

out:
	return ret;
}

static int ocp_reg_write(struct usbnet *dev, u16 addr, u16 data)
{
	u16 ocp_base, ocp_index;
	int ret;

	ocp_base = addr & 0xf000;
	ret = pla_write_word(dev->udev, OCP_BASE, ocp_base);
	if (ret < 0)
		goto out1;

	ocp_index = (addr & 0x0fff) | 0xb000;
	ret = pla_write_word(dev->udev, ocp_index, data);

out1:
	return ret;
}

static int r815x_mdio_read(struct net_device *netdev, int phy_id, int reg)
{
	struct usbnet *dev = netdev_priv(netdev);

	if (phy_id != R815x_PHY_ID)
		return -EINVAL;

	return ocp_reg_read(dev, BASE_MII + reg * 2);
}

static
void r815x_mdio_write(struct net_device *netdev, int phy_id, int reg, int val)
{
	struct usbnet *dev = netdev_priv(netdev);

	if (phy_id != R815x_PHY_ID)
		return;

	ocp_reg_write(dev, BASE_MII + reg * 2, val);
}

static int r8153_bind(struct usbnet *dev, struct usb_interface *intf)
{
	int status;

	status = usbnet_cdc_bind(dev, intf);
	if (status < 0)
		return status;

	dev->mii.dev = dev->net;
	dev->mii.mdio_read = r815x_mdio_read;
	dev->mii.mdio_write = r815x_mdio_write;
	dev->mii.phy_id_mask = 0x3f;
	dev->mii.reg_num_mask = 0x1f;
	dev->mii.phy_id = R815x_PHY_ID;
	dev->mii.supports_gmii = 1;

	return 0;
}

static int r8152_bind(struct usbnet *dev, struct usb_interface *intf)
{
	int status;

	status = usbnet_cdc_bind(dev, intf);
	if (status < 0)
		return status;

	dev->mii.dev = dev->net;
	dev->mii.mdio_read = r815x_mdio_read;
	dev->mii.mdio_write = r815x_mdio_write;
	dev->mii.phy_id_mask = 0x3f;
	dev->mii.reg_num_mask = 0x1f;
	dev->mii.phy_id = R815x_PHY_ID;
	dev->mii.supports_gmii = 0;

	return 0;
}

static const struct driver_info r8152_info = {
	.description =	"RTL8152 ECM Device",
	.flags =	FLAG_ETHER | FLAG_POINTTOPOINT,
	.bind =		r8152_bind,
	.unbind =	usbnet_cdc_unbind,
	.status =	usbnet_cdc_status,
	.manage_power =	usbnet_manage_power,
};

static const struct driver_info r8153_info = {
	.description =	"RTL8153 ECM Device",
	.flags =	FLAG_ETHER | FLAG_POINTTOPOINT,
	.bind =		r8153_bind,
	.unbind =	usbnet_cdc_unbind,
	.status =	usbnet_cdc_status,
	.manage_power =	usbnet_manage_power,
};

static const struct usb_device_id products[] = {
{
	USB_DEVICE_AND_INTERFACE_INFO(REALTEK_VENDOR_ID, 0x8152, USB_CLASS_COMM,
			USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE),
#if defined(CONFIG_USB_RTL8152) || defined(CONFIG_USB_RTL8152_MODULE)
	.driver_info = 0,
#else
	.driver_info = (unsigned long) &r8152_info,
#endif
},

{
	USB_DEVICE_AND_INTERFACE_INFO(REALTEK_VENDOR_ID, 0x8153, USB_CLASS_COMM,
			USB_CDC_SUBCLASS_ETHERNET, USB_CDC_PROTO_NONE),
#if defined(CONFIG_USB_RTL8153) || defined(CONFIG_USB_RTL8153_MODULE)
	.driver_info = 0,
#else
	.driver_info = (unsigned long) &r8153_info,
#endif
},

	{ },		/* END */
};
MODULE_DEVICE_TABLE(usb, products);

static struct usb_driver r815x_driver = {
	.name =		"r815x",
	.id_table =	products,
	.probe =	usbnet_probe,
	.disconnect =	usbnet_disconnect,
	.suspend =	usbnet_suspend,
	.resume =	usbnet_resume,
	.reset_resume =	usbnet_resume,
	.supports_autosuspend = 1,
	.disable_hub_initiated_lpm = 1,
};

module_usb_driver(r815x_driver);

MODULE_AUTHOR("Hayes Wang");
MODULE_DESCRIPTION("Realtek USB ECM device");
MODULE_LICENSE("GPL");