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