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 (20 - 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 delayed_work state_queue;
319 atomic_t irq_disable;
320
321 struct mutex lock;
322
323 struct net_device *attached_dev;
324
325 void (*adjust_link)(struct net_device *dev);
326
327 void (*adjust_state)(struct net_device *dev);
328};
329#define to_phy_device(d) container_of(d, struct phy_device, dev)
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351struct phy_driver {
352 u32 phy_id;
353 char *name;
354 unsigned int phy_id_mask;
355 u32 features;
356 u32 flags;
357
358
359
360
361
362 int (*config_init)(struct phy_device *phydev);
363
364
365
366
367
368 int (*probe)(struct phy_device *phydev);
369
370
371 int (*suspend)(struct phy_device *phydev);
372 int (*resume)(struct phy_device *phydev);
373
374
375
376
377
378
379
380 int (*config_aneg)(struct phy_device *phydev);
381
382
383 int (*read_status)(struct phy_device *phydev);
384
385
386 int (*ack_interrupt)(struct phy_device *phydev);
387
388
389 int (*config_intr)(struct phy_device *phydev);
390
391
392
393
394
395 int (*did_interrupt)(struct phy_device *phydev);
396
397
398 void (*remove)(struct phy_device *phydev);
399
400 struct device_driver driver;
401};
402#define to_phy_driver(d) container_of(d, struct phy_driver, driver)
403
404#define PHY_ANY_ID "MATCH ANY PHY"
405#define PHY_ANY_UID 0xffffffff
406
407
408struct phy_fixup {
409 struct list_head list;
410 char bus_id[20];
411 u32 phy_uid;
412 u32 phy_uid_mask;
413 int (*run)(struct phy_device *phydev);
414};
415
416
417
418
419
420
421
422
423
424
425static inline int phy_read(struct phy_device *phydev, u16 regnum)
426{
427 return mdiobus_read(phydev->bus, phydev->addr, regnum);
428}
429
430
431
432
433
434
435
436
437
438
439
440static inline int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
441{
442 return mdiobus_write(phydev->bus, phydev->addr, regnum, val);
443}
444
445int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id);
446struct phy_device* get_phy_device(struct mii_bus *bus, int addr);
447int phy_device_register(struct phy_device *phy);
448int phy_clear_interrupt(struct phy_device *phydev);
449int phy_config_interrupt(struct phy_device *phydev, u32 interrupts);
450int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
451 u32 flags, phy_interface_t interface);
452struct phy_device * phy_attach(struct net_device *dev,
453 const char *bus_id, u32 flags, phy_interface_t interface);
454int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
455 void (*handler)(struct net_device *), u32 flags,
456 phy_interface_t interface);
457struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
458 void (*handler)(struct net_device *), u32 flags,
459 phy_interface_t interface);
460void phy_disconnect(struct phy_device *phydev);
461void phy_detach(struct phy_device *phydev);
462void phy_start(struct phy_device *phydev);
463void phy_stop(struct phy_device *phydev);
464int phy_start_aneg(struct phy_device *phydev);
465
466void phy_sanitize_settings(struct phy_device *phydev);
467int phy_stop_interrupts(struct phy_device *phydev);
468int phy_enable_interrupts(struct phy_device *phydev);
469int phy_disable_interrupts(struct phy_device *phydev);
470
471static inline int phy_read_status(struct phy_device *phydev) {
472 return phydev->drv->read_status(phydev);
473}
474
475int genphy_config_advert(struct phy_device *phydev);
476int genphy_setup_forced(struct phy_device *phydev);
477int genphy_restart_aneg(struct phy_device *phydev);
478int genphy_config_aneg(struct phy_device *phydev);
479int genphy_update_link(struct phy_device *phydev);
480int genphy_read_status(struct phy_device *phydev);
481int genphy_suspend(struct phy_device *phydev);
482int genphy_resume(struct phy_device *phydev);
483void phy_driver_unregister(struct phy_driver *drv);
484int phy_driver_register(struct phy_driver *new_driver);
485void phy_prepare_link(struct phy_device *phydev,
486 void (*adjust_link)(struct net_device *));
487void phy_start_machine(struct phy_device *phydev,
488 void (*handler)(struct net_device *));
489void phy_stop_machine(struct phy_device *phydev);
490int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd);
491int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd);
492int phy_mii_ioctl(struct phy_device *phydev,
493 struct mii_ioctl_data *mii_data, int cmd);
494int phy_start_interrupts(struct phy_device *phydev);
495void phy_print_status(struct phy_device *phydev);
496struct phy_device* phy_device_create(struct mii_bus *bus, int addr, int phy_id);
497void phy_device_free(struct phy_device *phydev);
498
499int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
500 int (*run)(struct phy_device *));
501int phy_register_fixup_for_id(const char *bus_id,
502 int (*run)(struct phy_device *));
503int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
504 int (*run)(struct phy_device *));
505int phy_scan_fixups(struct phy_device *phydev);
506
507int __init mdio_bus_init(void);
508void mdio_bus_exit(void);
509
510extern struct bus_type mdio_bus_type;
511#endif
512