phy: Add support for the NC-SI protocol
[platform/kernel/u-boot.git] / include / phy.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright 2011 Freescale Semiconductor, Inc.
4  *      Andy Fleming <afleming@gmail.com>
5  *
6  * This file pretty much stolen from Linux's mii.h/ethtool.h/phy.h
7  */
8
9 #ifndef _PHY_H
10 #define _PHY_H
11
12 #include <dm.h>
13 #include <linux/errno.h>
14 #include <linux/list.h>
15 #include <linux/mii.h>
16 #include <linux/ethtool.h>
17 #include <linux/mdio.h>
18 #include <phy_interface.h>
19
20 #define PHY_FIXED_ID            0xa5a55a5a
21 #define PHY_NCSI_ID            0xbeefcafe
22
23 /*
24  * There is no actual id for this.
25  * This is just a dummy id for gmii2rgmmi converter.
26  */
27 #define PHY_GMII2RGMII_ID       0x5a5a5a5a
28
29 #define PHY_MAX_ADDR 32
30
31 #define PHY_FLAG_BROKEN_RESET   (1 << 0) /* soft reset not supported */
32
33 #define PHY_DEFAULT_FEATURES    (SUPPORTED_Autoneg | \
34                                  SUPPORTED_TP | \
35                                  SUPPORTED_MII)
36
37 #define PHY_10BT_FEATURES       (SUPPORTED_10baseT_Half | \
38                                  SUPPORTED_10baseT_Full)
39
40 #define PHY_100BT_FEATURES      (SUPPORTED_100baseT_Half | \
41                                  SUPPORTED_100baseT_Full)
42
43 #define PHY_1000BT_FEATURES     (SUPPORTED_1000baseT_Half | \
44                                  SUPPORTED_1000baseT_Full)
45
46 #define PHY_BASIC_FEATURES      (PHY_10BT_FEATURES | \
47                                  PHY_100BT_FEATURES | \
48                                  PHY_DEFAULT_FEATURES)
49
50 #define PHY_GBIT_FEATURES       (PHY_BASIC_FEATURES | \
51                                  PHY_1000BT_FEATURES)
52
53 #define PHY_10G_FEATURES        (PHY_GBIT_FEATURES | \
54                                 SUPPORTED_10000baseT_Full)
55
56 #ifndef PHY_ANEG_TIMEOUT
57 #define PHY_ANEG_TIMEOUT        4000
58 #endif
59
60
61 struct phy_device;
62
63 #define MDIO_NAME_LEN 32
64
65 struct mii_dev {
66         struct list_head link;
67         char name[MDIO_NAME_LEN];
68         void *priv;
69         int (*read)(struct mii_dev *bus, int addr, int devad, int reg);
70         int (*write)(struct mii_dev *bus, int addr, int devad, int reg,
71                         u16 val);
72         int (*reset)(struct mii_dev *bus);
73         struct phy_device *phymap[PHY_MAX_ADDR];
74         u32 phy_mask;
75 };
76
77 /* struct phy_driver: a structure which defines PHY behavior
78  *
79  * uid will contain a number which represents the PHY.  During
80  * startup, the driver will poll the PHY to find out what its
81  * UID--as defined by registers 2 and 3--is.  The 32-bit result
82  * gotten from the PHY will be masked to
83  * discard any bits which may change based on revision numbers
84  * unimportant to functionality
85  *
86  */
87 struct phy_driver {
88         char *name;
89         unsigned int uid;
90         unsigned int mask;
91         unsigned int mmds;
92
93         u32 features;
94
95         /* Called to do any driver startup necessities */
96         /* Will be called during phy_connect */
97         int (*probe)(struct phy_device *phydev);
98
99         /* Called to configure the PHY, and modify the controller
100          * based on the results.  Should be called after phy_connect */
101         int (*config)(struct phy_device *phydev);
102
103         /* Called when starting up the controller */
104         int (*startup)(struct phy_device *phydev);
105
106         /* Called when bringing down the controller */
107         int (*shutdown)(struct phy_device *phydev);
108
109         int (*readext)(struct phy_device *phydev, int addr, int devad, int reg);
110         int (*writeext)(struct phy_device *phydev, int addr, int devad, int reg,
111                         u16 val);
112
113         /* Phy specific driver override for reading a MMD register */
114         int (*read_mmd)(struct phy_device *phydev, int devad, int reg);
115
116         /* Phy specific driver override for writing a MMD register */
117         int (*write_mmd)(struct phy_device *phydev, int devad, int reg,
118                          u16 val);
119
120         struct list_head list;
121
122         /* driver private data */
123         ulong data;
124 };
125
126 struct phy_device {
127         /* Information about the PHY type */
128         /* And management functions */
129         struct mii_dev *bus;
130         struct phy_driver *drv;
131         void *priv;
132
133 #ifdef CONFIG_DM_ETH
134         struct udevice *dev;
135         ofnode node;
136 #else
137         struct eth_device *dev;
138 #endif
139
140         /* forced speed & duplex (no autoneg)
141          * partner speed & duplex & pause (autoneg)
142          */
143         int speed;
144         int duplex;
145
146         /* The most recently read link state */
147         int link;
148         int port;
149         phy_interface_t interface;
150
151         u32 advertising;
152         u32 supported;
153         u32 mmds;
154
155         int autoneg;
156         int addr;
157         int pause;
158         int asym_pause;
159         u32 phy_id;
160         bool is_c45;
161         u32 flags;
162 };
163
164 struct fixed_link {
165         int phy_id;
166         int duplex;
167         int link_speed;
168         int pause;
169         int asym_pause;
170 };
171
172 static inline int phy_read(struct phy_device *phydev, int devad, int regnum)
173 {
174         struct mii_dev *bus = phydev->bus;
175
176         return bus->read(bus, phydev->addr, devad, regnum);
177 }
178
179 static inline int phy_write(struct phy_device *phydev, int devad, int regnum,
180                         u16 val)
181 {
182         struct mii_dev *bus = phydev->bus;
183
184         return bus->write(bus, phydev->addr, devad, regnum, val);
185 }
186
187 static inline void phy_mmd_start_indirect(struct phy_device *phydev, int devad,
188                                           int regnum)
189 {
190         /* Write the desired MMD Devad */
191         phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_CTRL, devad);
192
193         /* Write the desired MMD register address */
194         phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA, regnum);
195
196         /* Select the Function : DATA with no post increment */
197         phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_CTRL,
198                   (devad | MII_MMD_CTRL_NOINCR));
199 }
200
201 static inline int phy_read_mmd(struct phy_device *phydev, int devad,
202                                int regnum)
203 {
204         struct phy_driver *drv = phydev->drv;
205
206         if (regnum > (u16)~0 || devad > 32)
207                 return -EINVAL;
208
209         /* driver-specific access */
210         if (drv->read_mmd)
211                 return drv->read_mmd(phydev, devad, regnum);
212
213         /* direct C45 / C22 access */
214         if ((drv->features & PHY_10G_FEATURES) == PHY_10G_FEATURES ||
215             devad == MDIO_DEVAD_NONE || !devad)
216                 return phy_read(phydev, devad, regnum);
217
218         /* indirect C22 access */
219         phy_mmd_start_indirect(phydev, devad, regnum);
220
221         /* Read the content of the MMD's selected register */
222         return phy_read(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA);
223 }
224
225 static inline int phy_write_mmd(struct phy_device *phydev, int devad,
226                                 int regnum, u16 val)
227 {
228         struct phy_driver *drv = phydev->drv;
229
230         if (regnum > (u16)~0 || devad > 32)
231                 return -EINVAL;
232
233         /* driver-specific access */
234         if (drv->write_mmd)
235                 return drv->write_mmd(phydev, devad, regnum, val);
236
237         /* direct C45 / C22 access */
238         if ((drv->features & PHY_10G_FEATURES) == PHY_10G_FEATURES ||
239             devad == MDIO_DEVAD_NONE || !devad)
240                 return phy_write(phydev, devad, regnum, val);
241
242         /* indirect C22 access */
243         phy_mmd_start_indirect(phydev, devad, regnum);
244
245         /* Write the data into MMD's selected register */
246         return phy_write(phydev, MDIO_DEVAD_NONE, MII_MMD_DATA, val);
247 }
248
249 #ifdef CONFIG_PHYLIB_10G
250 extern struct phy_driver gen10g_driver;
251
252 /* For now, XGMII is the only 10G interface */
253 static inline int is_10g_interface(phy_interface_t interface)
254 {
255         return interface == PHY_INTERFACE_MODE_XGMII;
256 }
257
258 #endif
259
260 /**
261  * phy_init() - Initializes the PHY drivers
262  *
263  * This function registers all available PHY drivers
264  *
265  * @return 0 if OK, -ve on error
266  */
267 int phy_init(void);
268
269 /**
270  * phy_reset() - Resets the specified PHY
271  *
272  * Issues a reset of the PHY and waits for it to complete
273  *
274  * @phydev:     PHY to reset
275  * @return 0 if OK, -ve on error
276  */
277 int phy_reset(struct phy_device *phydev);
278
279 /**
280  * phy_find_by_mask() - Searches for a PHY on the specified MDIO bus
281  *
282  * The function checks the PHY addresses flagged in phy_mask and returns a
283  * phy_device pointer if it detects a PHY.
284  * This function should only be called if just one PHY is expected to be present
285  * in the set of addresses flagged in phy_mask.  If multiple PHYs are present,
286  * it is undefined which of these PHYs is returned.
287  *
288  * @bus:        MII/MDIO bus to scan
289  * @phy_mask:   bitmap of PYH addresses to scan
290  * @interface:  type of MAC-PHY interface
291  * @return pointer to phy_device if a PHY is found, or NULL otherwise
292  */
293 struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
294                 phy_interface_t interface);
295
296 #ifdef CONFIG_DM_ETH
297
298 /**
299  * phy_connect_dev() - Associates the given pair of PHY and Ethernet devices
300  * @phydev:     PHY device
301  * @dev:        Ethernet device
302  */
303 void phy_connect_dev(struct phy_device *phydev, struct udevice *dev);
304
305 /**
306  * phy_connect() - Creates a PHY device for the Ethernet interface
307  *
308  * Creates a PHY device for the PHY at the given address, if one doesn't exist
309  * already, and associates it with the Ethernet device.
310  * The function may be called with addr <= 0, in this case addr value is ignored
311  * and the bus is scanned to detect a PHY.  Scanning should only be used if only
312  * one PHY is expected to be present on the MDIO bus, otherwise it is undefined
313  * which PHY is returned.
314  *
315  * @bus:        MII/MDIO bus that hosts the PHY
316  * @addr:       PHY address on MDIO bus
317  * @dev:        Ethernet device to associate to the PHY
318  * @interface:  type of MAC-PHY interface
319  * @return pointer to phy_device if a PHY is found, or NULL otherwise
320  */
321 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
322                                 struct udevice *dev,
323                                 phy_interface_t interface);
324
325 static inline ofnode phy_get_ofnode(struct phy_device *phydev)
326 {
327         if (ofnode_valid(phydev->node))
328                 return phydev->node;
329         else
330                 return dev_ofnode(phydev->dev);
331 }
332 #else
333
334 /**
335  * phy_connect_dev() - Associates the given pair of PHY and Ethernet devices
336  * @phydev:     PHY device
337  * @dev:        Ethernet device
338  */
339 void phy_connect_dev(struct phy_device *phydev, struct eth_device *dev);
340
341 /**
342  * phy_connect() - Creates a PHY device for the Ethernet interface
343  *
344  * Creates a PHY device for the PHY at the given address, if one doesn't exist
345  * already, and associates it with the Ethernet device.
346  * The function may be called with addr <= 0, in this case addr value is ignored
347  * and the bus is scanned to detect a PHY.  Scanning should only be used if only
348  * one PHY is expected to be present on the MDIO bus, otherwise it is undefined
349  * which PHY is returned.
350  *
351  * @bus:        MII/MDIO bus that hosts the PHY
352  * @addr:       PHY address on MDIO bus
353  * @dev:        Ethernet device to associate to the PHY
354  * @interface:  type of MAC-PHY interface
355  * @return pointer to phy_device if a PHY is found, or NULL otherwise
356  */
357 struct phy_device *phy_connect(struct mii_dev *bus, int addr,
358                                 struct eth_device *dev,
359                                 phy_interface_t interface);
360
361 static inline ofnode phy_get_ofnode(struct phy_device *phydev)
362 {
363         return ofnode_null();
364 }
365 #endif
366 int phy_startup(struct phy_device *phydev);
367 int phy_config(struct phy_device *phydev);
368 int phy_shutdown(struct phy_device *phydev);
369 int phy_register(struct phy_driver *drv);
370 int phy_set_supported(struct phy_device *phydev, u32 max_speed);
371 int genphy_config_aneg(struct phy_device *phydev);
372 int genphy_restart_aneg(struct phy_device *phydev);
373 int genphy_update_link(struct phy_device *phydev);
374 int genphy_parse_link(struct phy_device *phydev);
375 int genphy_config(struct phy_device *phydev);
376 int genphy_startup(struct phy_device *phydev);
377 int genphy_shutdown(struct phy_device *phydev);
378 int gen10g_config(struct phy_device *phydev);
379 int gen10g_startup(struct phy_device *phydev);
380 int gen10g_shutdown(struct phy_device *phydev);
381 int gen10g_discover_mmds(struct phy_device *phydev);
382
383 int phy_b53_init(void);
384 int phy_mv88e61xx_init(void);
385 int phy_aquantia_init(void);
386 int phy_atheros_init(void);
387 int phy_broadcom_init(void);
388 int phy_cortina_init(void);
389 int phy_davicom_init(void);
390 int phy_et1011c_init(void);
391 int phy_lxt_init(void);
392 int phy_marvell_init(void);
393 int phy_micrel_ksz8xxx_init(void);
394 int phy_micrel_ksz90x1_init(void);
395 int phy_meson_gxl_init(void);
396 int phy_natsemi_init(void);
397 int phy_realtek_init(void);
398 int phy_smsc_init(void);
399 int phy_teranetics_init(void);
400 int phy_ti_init(void);
401 int phy_vitesse_init(void);
402 int phy_xilinx_init(void);
403 int phy_mscc_init(void);
404 int phy_fixed_init(void);
405 int phy_xilinx_gmii2rgmii_init(void);
406
407 int board_phy_config(struct phy_device *phydev);
408 int get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id);
409
410 /**
411  * phy_get_interface_by_name() - Look up a PHY interface name
412  *
413  * @str:        PHY interface name, e.g. "mii"
414  * @return PHY_INTERFACE_MODE_... value, or -1 if not found
415  */
416 int phy_get_interface_by_name(const char *str);
417
418 /**
419  * phy_interface_is_rgmii - Convenience function for testing if a PHY interface
420  * is RGMII (all variants)
421  * @phydev: the phy_device struct
422  */
423 static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
424 {
425         return phydev->interface >= PHY_INTERFACE_MODE_RGMII &&
426                 phydev->interface <= PHY_INTERFACE_MODE_RGMII_TXID;
427 }
428
429 /**
430  * phy_interface_is_sgmii - Convenience function for testing if a PHY interface
431  * is SGMII (all variants)
432  * @phydev: the phy_device struct
433  */
434 static inline bool phy_interface_is_sgmii(struct phy_device *phydev)
435 {
436         return phydev->interface >= PHY_INTERFACE_MODE_SGMII &&
437                 phydev->interface <= PHY_INTERFACE_MODE_QSGMII;
438 }
439
440 /* PHY UIDs for various PHYs that are referenced in external code */
441 #define PHY_UID_CS4340          0x13e51002
442 #define PHY_UID_CS4223          0x03e57003
443 #define PHY_UID_TN2020          0x00a19410
444 #define PHY_UID_IN112525_S03    0x02107440
445
446 #endif