1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#ifndef __PHY_H
17#define __PHY_H
18
19#include <linux/spinlock.h>
20#include <linux/ethtool.h>
21#include <linux/mii.h>
22#include <linux/module.h>
23#include <linux/timer.h>
24#include <linux/workqueue.h>
25#include <linux/mod_devicetable.h>
26
27#include <linux/atomic.h>
28
29#define PHY_DEFAULT_FEATURES (SUPPORTED_Autoneg | \
30 SUPPORTED_TP | \
31 SUPPORTED_MII)
32
33#define PHY_10BT_FEATURES (SUPPORTED_10baseT_Half | \
34 SUPPORTED_10baseT_Full)
35
36#define PHY_100BT_FEATURES (SUPPORTED_100baseT_Half | \
37 SUPPORTED_100baseT_Full)
38
39#define PHY_1000BT_FEATURES (SUPPORTED_1000baseT_Half | \
40 SUPPORTED_1000baseT_Full)
41
42#define PHY_BASIC_FEATURES (PHY_10BT_FEATURES | \
43 PHY_100BT_FEATURES | \
44 PHY_DEFAULT_FEATURES)
45
46#define PHY_GBIT_FEATURES (PHY_BASIC_FEATURES | \
47 PHY_1000BT_FEATURES)
48
49
50
51
52
53
54
55#define PHY_POLL -1
56#define PHY_IGNORE_INTERRUPT -2
57
58#define PHY_HAS_INTERRUPT 0x00000001
59#define PHY_HAS_MAGICANEG 0x00000002
60#define PHY_IS_INTERNAL 0x00000004
61
62
63typedef enum {
64 PHY_INTERFACE_MODE_NA,
65 PHY_INTERFACE_MODE_MII,
66 PHY_INTERFACE_MODE_GMII,
67 PHY_INTERFACE_MODE_SGMII,
68 PHY_INTERFACE_MODE_TBI,
69 PHY_INTERFACE_MODE_REVMII,
70 PHY_INTERFACE_MODE_RMII,
71 PHY_INTERFACE_MODE_RGMII,
72 PHY_INTERFACE_MODE_RGMII_ID,
73 PHY_INTERFACE_MODE_RGMII_RXID,
74 PHY_INTERFACE_MODE_RGMII_TXID,
75 PHY_INTERFACE_MODE_RTBI,
76 PHY_INTERFACE_MODE_SMII,
77 PHY_INTERFACE_MODE_XGMII,
78 PHY_INTERFACE_MODE_MOCA,
79 PHY_INTERFACE_MODE_QSGMII,
80 PHY_INTERFACE_MODE_MAX,
81} phy_interface_t;
82
83
84
85
86
87
88static inline const char *phy_modes(phy_interface_t interface)
89{
90 switch (interface) {
91 case PHY_INTERFACE_MODE_NA:
92 return "";
93 case PHY_INTERFACE_MODE_MII:
94 return "mii";
95 case PHY_INTERFACE_MODE_GMII:
96 return "gmii";
97 case PHY_INTERFACE_MODE_SGMII:
98 return "sgmii";
99 case PHY_INTERFACE_MODE_TBI:
100 return "tbi";
101 case PHY_INTERFACE_MODE_REVMII:
102 return "rev-mii";
103 case PHY_INTERFACE_MODE_RMII:
104 return "rmii";
105 case PHY_INTERFACE_MODE_RGMII:
106 return "rgmii";
107 case PHY_INTERFACE_MODE_RGMII_ID:
108 return "rgmii-id";
109 case PHY_INTERFACE_MODE_RGMII_RXID:
110 return "rgmii-rxid";
111 case PHY_INTERFACE_MODE_RGMII_TXID:
112 return "rgmii-txid";
113 case PHY_INTERFACE_MODE_RTBI:
114 return "rtbi";
115 case PHY_INTERFACE_MODE_SMII:
116 return "smii";
117 case PHY_INTERFACE_MODE_XGMII:
118 return "xgmii";
119 case PHY_INTERFACE_MODE_MOCA:
120 return "moca";
121 case PHY_INTERFACE_MODE_QSGMII:
122 return "qsgmii";
123 default:
124 return "unknown";
125 }
126}
127
128
129#define PHY_INIT_TIMEOUT 100000
130#define PHY_STATE_TIME 1
131#define PHY_FORCE_TIMEOUT 10
132#define PHY_AN_TIMEOUT 10
133
134#define PHY_MAX_ADDR 32
135
136
137#define PHY_ID_FMT "%s:%02x"
138
139
140
141
142
143#define MII_BUS_ID_SIZE (20 - 3)
144
145
146
147#define MII_ADDR_C45 (1<<30)
148
149struct device;
150struct sk_buff;
151
152
153
154
155
156struct mii_bus {
157 struct module *owner;
158 const char *name;
159 char id[MII_BUS_ID_SIZE];
160 void *priv;
161 int (*read)(struct mii_bus *bus, int phy_id, int regnum);
162 int (*write)(struct mii_bus *bus, int phy_id, int regnum, u16 val);
163 int (*reset)(struct mii_bus *bus);
164
165
166
167
168
169 struct mutex mdio_lock;
170
171 struct device *parent;
172 enum {
173 MDIOBUS_ALLOCATED = 1,
174 MDIOBUS_REGISTERED,
175 MDIOBUS_UNREGISTERED,
176 MDIOBUS_RELEASED,
177 } state;
178 struct device dev;
179
180
181 struct phy_device *phy_map[PHY_MAX_ADDR];
182
183
184 u32 phy_mask;
185
186
187 u32 phy_ignore_ta_mask;
188
189
190
191
192
193 int *irq;
194};
195#define to_mii_bus(d) container_of(d, struct mii_bus, dev)
196
197struct mii_bus *mdiobus_alloc_size(size_t);
198static inline struct mii_bus *mdiobus_alloc(void)
199{
200 return mdiobus_alloc_size(0);
201}
202
203int __mdiobus_register(struct mii_bus *bus, struct module *owner);
204#define mdiobus_register(bus) __mdiobus_register(bus, THIS_MODULE)
205void mdiobus_unregister(struct mii_bus *bus);
206void mdiobus_free(struct mii_bus *bus);
207struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv);
208static inline struct mii_bus *devm_mdiobus_alloc(struct device *dev)
209{
210 return devm_mdiobus_alloc_size(dev, 0);
211}
212
213void devm_mdiobus_free(struct device *dev, struct mii_bus *bus);
214struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
215int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
216int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
217int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
218int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
219
220
221#define PHY_INTERRUPT_DISABLED 0x0
222#define PHY_INTERRUPT_ENABLED 0x80000000
223
224
225
226
227
228
229
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304enum phy_state {
305 PHY_DOWN = 0,
306 PHY_STARTING,
307 PHY_READY,
308 PHY_PENDING,
309 PHY_UP,
310 PHY_AN,
311 PHY_RUNNING,
312 PHY_NOLINK,
313 PHY_FORCING,
314 PHY_CHANGELINK,
315 PHY_HALTED,
316 PHY_RESUMING
317};
318
319
320
321
322
323
324struct phy_c45_device_ids {
325 u32 devices_in_package;
326 u32 device_ids[8];
327};
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363struct phy_device {
364
365
366 struct phy_driver *drv;
367
368 struct mii_bus *bus;
369
370 struct device dev;
371
372 u32 phy_id;
373
374 struct phy_c45_device_ids c45_ids;
375 bool is_c45;
376 bool is_internal;
377 bool is_pseudo_fixed_link;
378 bool has_fixups;
379 bool suspended;
380
381 enum phy_state state;
382
383 u32 dev_flags;
384
385 phy_interface_t interface;
386
387
388 int addr;
389
390
391
392
393
394 int speed;
395 int duplex;
396 int pause;
397 int asym_pause;
398
399
400 int link;
401
402
403 u32 interrupts;
404
405
406
407 u32 supported;
408 u32 advertising;
409 u32 lp_advertising;
410
411 int autoneg;
412
413 int link_timeout;
414
415
416
417
418
419 int irq;
420
421
422
423 void *priv;
424
425
426 struct work_struct phy_queue;
427 struct delayed_work state_queue;
428 atomic_t irq_disable;
429
430 struct mutex lock;
431
432 struct net_device *attached_dev;
433
434 u8 mdix;
435
436 void (*adjust_link)(struct net_device *dev);
437};
438#define to_phy_device(d) container_of(d, struct phy_device, dev)
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461struct phy_driver {
462 u32 phy_id;
463 char *name;
464 unsigned int phy_id_mask;
465 u32 features;
466 u32 flags;
467 const void *driver_data;
468
469
470
471
472 int (*soft_reset)(struct phy_device *phydev);
473
474
475
476
477
478 int (*config_init)(struct phy_device *phydev);
479
480
481
482
483
484 int (*probe)(struct phy_device *phydev);
485
486
487 int (*suspend)(struct phy_device *phydev);
488 int (*resume)(struct phy_device *phydev);
489
490
491
492
493
494
495
496 int (*config_aneg)(struct phy_device *phydev);
497
498
499 int (*aneg_done)(struct phy_device *phydev);
500
501
502 int (*read_status)(struct phy_device *phydev);
503
504
505 int (*ack_interrupt)(struct phy_device *phydev);
506
507
508 int (*config_intr)(struct phy_device *phydev);
509
510
511
512
513
514 int (*did_interrupt)(struct phy_device *phydev);
515
516
517 void (*remove)(struct phy_device *phydev);
518
519
520
521
522
523 int (*match_phy_device)(struct phy_device *phydev);
524
525
526 int (*ts_info)(struct phy_device *phydev, struct ethtool_ts_info *ti);
527
528
529 int (*hwtstamp)(struct phy_device *phydev, struct ifreq *ifr);
530
531
532
533
534
535
536
537
538 bool (*rxtstamp)(struct phy_device *dev, struct sk_buff *skb, int type);
539
540
541
542
543
544
545
546 void (*txtstamp)(struct phy_device *dev, struct sk_buff *skb, int type);
547
548
549
550
551 int (*set_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
552
553
554 void (*get_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
555
556
557
558
559
560
561
562
563 void (*link_change_notify)(struct phy_device *dev);
564
565
566
567
568
569
570
571 int (*read_mmd_indirect)(struct phy_device *dev, int ptrad,
572 int devnum, int regnum);
573
574
575
576
577
578
579
580 void (*write_mmd_indirect)(struct phy_device *dev, int ptrad,
581 int devnum, int regnum, u32 val);
582
583
584
585 int (*module_info)(struct phy_device *dev,
586 struct ethtool_modinfo *modinfo);
587
588
589 int (*module_eeprom)(struct phy_device *dev,
590 struct ethtool_eeprom *ee, u8 *data);
591
592 struct device_driver driver;
593};
594#define to_phy_driver(d) container_of(d, struct phy_driver, driver)
595
596#define PHY_ANY_ID "MATCH ANY PHY"
597#define PHY_ANY_UID 0xffffffff
598
599
600struct phy_fixup {
601 struct list_head list;
602 char bus_id[20];
603 u32 phy_uid;
604 u32 phy_uid_mask;
605 int (*run)(struct phy_device *phydev);
606};
607
608
609
610
611
612
613
614
615
616
617static inline int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum)
618{
619 if (!phydev->is_c45)
620 return -EOPNOTSUPP;
621
622 return mdiobus_read(phydev->bus, phydev->addr,
623 MII_ADDR_C45 | (devad << 16) | (regnum & 0xffff));
624}
625
626
627
628
629
630
631
632
633
634
635
636int phy_read_mmd_indirect(struct phy_device *phydev, int prtad,
637 int devad, int addr);
638
639
640
641
642
643
644
645
646
647
648static inline int phy_read(struct phy_device *phydev, u32 regnum)
649{
650 return mdiobus_read(phydev->bus, phydev->addr, regnum);
651}
652
653
654
655
656
657
658
659
660
661
662
663static inline int phy_write(struct phy_device *phydev, u32 regnum, u16 val)
664{
665 return mdiobus_write(phydev->bus, phydev->addr, regnum, val);
666}
667
668
669
670
671
672
673
674
675static inline bool phy_interrupt_is_valid(struct phy_device *phydev)
676{
677 return phydev->irq != PHY_POLL && phydev->irq != PHY_IGNORE_INTERRUPT;
678}
679
680
681
682
683
684static inline bool phy_is_internal(struct phy_device *phydev)
685{
686 return phydev->is_internal;
687}
688
689
690
691
692
693
694static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
695{
696 return phydev->interface >= PHY_INTERFACE_MODE_RGMII &&
697 phydev->interface <= PHY_INTERFACE_MODE_RGMII_TXID;
698};
699
700
701
702
703
704
705static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev)
706{
707 return phydev->is_pseudo_fixed_link;
708}
709
710
711
712
713
714
715
716
717
718
719
720static inline int phy_write_mmd(struct phy_device *phydev, int devad,
721 u32 regnum, u16 val)
722{
723 if (!phydev->is_c45)
724 return -EOPNOTSUPP;
725
726 regnum = MII_ADDR_C45 | ((devad & 0x1f) << 16) | (regnum & 0xffff);
727
728 return mdiobus_write(phydev->bus, phydev->addr, regnum, val);
729}
730
731
732
733
734
735
736
737
738
739
740
741
742void phy_write_mmd_indirect(struct phy_device *phydev, int prtad,
743 int devad, int addr, u32 data);
744
745struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
746 bool is_c45,
747 struct phy_c45_device_ids *c45_ids);
748struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
749int phy_device_register(struct phy_device *phy);
750void phy_device_remove(struct phy_device *phydev);
751int phy_init_hw(struct phy_device *phydev);
752int phy_suspend(struct phy_device *phydev);
753int phy_resume(struct phy_device *phydev);
754struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
755 phy_interface_t interface);
756struct phy_device *phy_find_first(struct mii_bus *bus);
757int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
758 u32 flags, phy_interface_t interface);
759int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
760 void (*handler)(struct net_device *),
761 phy_interface_t interface);
762struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
763 void (*handler)(struct net_device *),
764 phy_interface_t interface);
765void phy_disconnect(struct phy_device *phydev);
766void phy_detach(struct phy_device *phydev);
767void phy_start(struct phy_device *phydev);
768void phy_stop(struct phy_device *phydev);
769int phy_start_aneg(struct phy_device *phydev);
770
771int phy_stop_interrupts(struct phy_device *phydev);
772
773static inline int phy_read_status(struct phy_device *phydev)
774{
775 return phydev->drv->read_status(phydev);
776}
777
778int genphy_config_init(struct phy_device *phydev);
779int genphy_setup_forced(struct phy_device *phydev);
780int genphy_restart_aneg(struct phy_device *phydev);
781int genphy_config_aneg(struct phy_device *phydev);
782int genphy_aneg_done(struct phy_device *phydev);
783int genphy_update_link(struct phy_device *phydev);
784int genphy_read_status(struct phy_device *phydev);
785int genphy_suspend(struct phy_device *phydev);
786int genphy_resume(struct phy_device *phydev);
787int genphy_soft_reset(struct phy_device *phydev);
788void phy_driver_unregister(struct phy_driver *drv);
789void phy_drivers_unregister(struct phy_driver *drv, int n);
790int phy_driver_register(struct phy_driver *new_driver);
791int phy_drivers_register(struct phy_driver *new_driver, int n);
792void phy_state_machine(struct work_struct *work);
793void phy_change(struct work_struct *work);
794void phy_mac_interrupt(struct phy_device *phydev, int new_link);
795void phy_start_machine(struct phy_device *phydev);
796void phy_stop_machine(struct phy_device *phydev);
797int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd);
798int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd);
799int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd);
800int phy_start_interrupts(struct phy_device *phydev);
801void phy_print_status(struct phy_device *phydev);
802void phy_device_free(struct phy_device *phydev);
803int phy_set_max_speed(struct phy_device *phydev, u32 max_speed);
804
805int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
806 int (*run)(struct phy_device *));
807int phy_register_fixup_for_id(const char *bus_id,
808 int (*run)(struct phy_device *));
809int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
810 int (*run)(struct phy_device *));
811
812int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable);
813int phy_get_eee_err(struct phy_device *phydev);
814int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data);
815int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data);
816int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol);
817void phy_ethtool_get_wol(struct phy_device *phydev,
818 struct ethtool_wolinfo *wol);
819
820int __init mdio_bus_init(void);
821void mdio_bus_exit(void);
822
823extern struct bus_type mdio_bus_type;
824
825
826
827
828
829
830
831
832
833#define phy_module_driver(__phy_drivers, __count) \
834static int __init phy_module_init(void) \
835{ \
836 return phy_drivers_register(__phy_drivers, __count); \
837} \
838module_init(phy_module_init); \
839static void __exit phy_module_exit(void) \
840{ \
841 phy_drivers_unregister(__phy_drivers, __count); \
842} \
843module_exit(phy_module_exit)
844
845#define module_phy_driver(__phy_drivers) \
846 phy_module_driver(__phy_drivers, ARRAY_SIZE(__phy_drivers))
847
848#endif
849