1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#ifndef __PHY_H
19#define __PHY_H
20
21#include <linux/spinlock.h>
22#include <linux/device.h>
23#include <linux/ethtool.h>
24#include <linux/mii.h>
25#include <linux/timer.h>
26#include <linux/workqueue.h>
27
28#include <asm/atomic.h>
29
30#define PHY_BASIC_FEATURES (SUPPORTED_10baseT_Half | \
31 SUPPORTED_10baseT_Full | \
32 SUPPORTED_100baseT_Half | \
33 SUPPORTED_100baseT_Full | \
34 SUPPORTED_Autoneg | \
35 SUPPORTED_TP | \
36 SUPPORTED_MII)
37
38#define PHY_GBIT_FEATURES (PHY_BASIC_FEATURES | \
39 SUPPORTED_1000baseT_Half | \
40 SUPPORTED_1000baseT_Full)
41
42
43
44
45
46
47#define PHY_POLL -1
48#define PHY_IGNORE_INTERRUPT -2
49
50#define PHY_HAS_INTERRUPT 0x00000001
51#define PHY_HAS_MAGICANEG 0x00000002
52
53
54typedef enum {
55 PHY_INTERFACE_MODE_MII,
56 PHY_INTERFACE_MODE_GMII,
57 PHY_INTERFACE_MODE_SGMII,
58 PHY_INTERFACE_MODE_TBI,
59 PHY_INTERFACE_MODE_RMII,
60 PHY_INTERFACE_MODE_RGMII,
61 PHY_INTERFACE_MODE_RGMII_ID,
62 PHY_INTERFACE_MODE_RGMII_RXID,
63 PHY_INTERFACE_MODE_RGMII_TXID,
64 PHY_INTERFACE_MODE_RTBI
65} phy_interface_t;
66
67
68#define PHY_INIT_TIMEOUT 100000
69#define PHY_STATE_TIME 1
70#define PHY_FORCE_TIMEOUT 10
71#define PHY_AN_TIMEOUT 10
72
73#define PHY_MAX_ADDR 32
74
75
76#define PHY_ID_FMT "%s:%02x"
77
78
79
80
81
82#define MII_BUS_ID_SIZE (BUS_ID_SIZE - 3)
83
84
85
86
87
88struct mii_bus {
89 const char *name;
90 char id[MII_BUS_ID_SIZE];
91 void *priv;
92 int (*read)(struct mii_bus *bus, int phy_id, int regnum);
93 int (*write)(struct mii_bus *bus, int phy_id, int regnum, u16 val);
94 int (*reset)(struct mii_bus *bus);
95
96
97
98
99
100 struct mutex mdio_lock;
101
102 struct device *parent;
103 enum {
104 MDIOBUS_ALLOCATED = 1,
105 MDIOBUS_REGISTERED,
106 MDIOBUS_UNREGISTERED,
107 MDIOBUS_RELEASED,
108 } state;
109 struct device dev;
110
111
112 struct phy_device *phy_map[PHY_MAX_ADDR];
113
114
115 u32 phy_mask;
116
117
118
119
120
121 int *irq;
122};
123#define to_mii_bus(d) container_of(d, struct mii_bus, dev)
124
125struct mii_bus *mdiobus_alloc(void);
126int mdiobus_register(struct mii_bus *bus);
127void mdiobus_unregister(struct mii_bus *bus);
128void mdiobus_free(struct mii_bus *bus);
129struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
130int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum);
131int mdiobus_write(struct mii_bus *bus, int addr, u16 regnum, u16 val);
132
133
134#define PHY_INTERRUPT_DISABLED 0x0
135#define PHY_INTERRUPT_ENABLED 0x80000000
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
217enum phy_state {
218 PHY_DOWN=0,
219 PHY_STARTING,
220 PHY_READY,
221 PHY_PENDING,
222 PHY_UP,
223 PHY_AN,
224 PHY_RUNNING,
225 PHY_NOLINK,
226 PHY_FORCING,
227 PHY_CHANGELINK,
228 PHY_HALTED,
229 PHY_RESUMING
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
262struct phy_device {
263
264
265 struct phy_driver *drv;
266
267 struct mii_bus *bus;
268
269 struct device dev;
270
271 u32 phy_id;
272
273 enum phy_state state;
274
275 u32 dev_flags;
276
277 phy_interface_t interface;
278
279
280 int addr;
281
282
283
284
285
286 int speed;
287 int duplex;
288 int pause;
289 int asym_pause;
290
291
292 int link;
293
294
295 u32 interrupts;
296
297
298
299 u32 supported;
300 u32 advertising;
301
302 int autoneg;
303
304 int link_timeout;
305
306
307
308
309
310 int irq;
311
312
313
314 void *priv;
315
316
317 struct work_struct phy_queue;
318 struct work_struct state_queue;
319 struct timer_list phy_timer;
320 atomic_t irq_disable;
321
322 struct mutex lock;
323
324 struct net_device *attached_dev;
325
326 void (*adjust_link)(struct net_device *dev);
327
328 void (*adjust_state)(struct net_device *dev);
329};
330#define to_phy_device(d) container_of(d, struct phy_device, dev)
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352struct phy_driver {
353 u32 phy_id;
354 char *name;
355 unsigned int phy_id_mask;
356 u32 features;
357 u32 flags;
358
359
360
361
362
363 int (*config_init)(struct phy_device *phydev);
364
365
366
367
368
369 int (*probe)(struct phy_device *phydev);
370
371
372 int (*suspend)(struct phy_device *phydev);
373 int (*resume)(struct phy_device *phydev);
374
375
376
377
378
379
380
381 int (*config_aneg)(struct phy_device *phydev);
382
383
384 int (*read_status)(struct phy_device *phydev);
385
386
387 int (*ack_interrupt)(struct phy_device *phydev);
388
389
390 int (*config_intr)(struct phy_device *phydev);
391
392
393 void (*remove)(struct phy_device *phydev);
394
395 struct device_driver driver;
396};
397#define to_phy_driver(d) container_of(d, struct phy_driver, driver)
398
399#define PHY_ANY_ID "MATCH ANY PHY"
400#define PHY_ANY_UID 0xffffffff
401
402
403struct phy_fixup {
404 struct list_head list;
405 char bus_id[BUS_ID_SIZE];
406 u32 phy_uid;
407 u32 phy_uid_mask;
408 int (*run)(struct phy_device *phydev);
409};
410
411
412
413
414
415
416
417
418
419
420static inline int phy_read(struct phy_device *phydev, u16 regnum)
421{
422 return mdiobus_read(phydev->bus, phydev->addr, regnum);
423}
424
425
426
427
428
429
430
431
432
433
434
435static inline int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
436{
437 return mdiobus_write(phydev->bus, phydev->addr, regnum, val);
438}
439
440int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id);
441struct phy_device* get_phy_device(struct mii_bus *bus, int addr);
442int phy_clear_interrupt(struct phy_device *phydev);
443int phy_config_interrupt(struct phy_device *phydev, u32 interrupts);
444struct phy_device * phy_attach(struct net_device *dev,
445 const char *bus_id, u32 flags, phy_interface_t interface);
446struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
447 void (*handler)(struct net_device *), u32 flags,
448 phy_interface_t interface);
449void phy_disconnect(struct phy_device *phydev);
450void phy_detach(struct phy_device *phydev);
451void phy_start(struct phy_device *phydev);
452void phy_stop(struct phy_device *phydev);
453int phy_start_aneg(struct phy_device *phydev);
454
455void phy_sanitize_settings(struct phy_device *phydev);
456int phy_stop_interrupts(struct phy_device *phydev);
457int phy_enable_interrupts(struct phy_device *phydev);
458int phy_disable_interrupts(struct phy_device *phydev);
459
460static inline int phy_read_status(struct phy_device *phydev) {
461 return phydev->drv->read_status(phydev);
462}
463
464int genphy_config_advert(struct phy_device *phydev);
465int genphy_setup_forced(struct phy_device *phydev);
466int genphy_restart_aneg(struct phy_device *phydev);
467int genphy_config_aneg(struct phy_device *phydev);
468int genphy_update_link(struct phy_device *phydev);
469int genphy_read_status(struct phy_device *phydev);
470int genphy_suspend(struct phy_device *phydev);
471int genphy_resume(struct phy_device *phydev);
472void phy_driver_unregister(struct phy_driver *drv);
473int phy_driver_register(struct phy_driver *new_driver);
474void phy_prepare_link(struct phy_device *phydev,
475 void (*adjust_link)(struct net_device *));
476void phy_start_machine(struct phy_device *phydev,
477 void (*handler)(struct net_device *));
478void phy_stop_machine(struct phy_device *phydev);
479int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd);
480int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd);
481int phy_mii_ioctl(struct phy_device *phydev,
482 struct mii_ioctl_data *mii_data, int cmd);
483int phy_start_interrupts(struct phy_device *phydev);
484void phy_print_status(struct phy_device *phydev);
485struct phy_device* phy_device_create(struct mii_bus *bus, int addr, int phy_id);
486void phy_device_free(struct phy_device *phydev);
487
488int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
489 int (*run)(struct phy_device *));
490int phy_register_fixup_for_id(const char *bus_id,
491 int (*run)(struct phy_device *));
492int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
493 int (*run)(struct phy_device *));
494int phy_scan_fixups(struct phy_device *phydev);
495
496int __init mdio_bus_init(void);
497void mdio_bus_exit(void);
498
499extern struct bus_type mdio_bus_type;
500#endif
501