]> err.no Git - linux-2.6/blob - include/linux/phy.h
892d6abe57e50f108f488e467dcb2541ad4401a7
[linux-2.6] / include / linux / phy.h
1 /*
2  * include/linux/phy.h
3  *
4  * Framework and drivers for configuring and reading different PHYs
5  * Based on code in sungem_phy.c and gianfar_phy.c
6  *
7  * Author: Andy Fleming
8  *
9  * Copyright (c) 2004 Freescale Semiconductor, Inc.
10  *
11  * This program is free software; you can redistribute  it and/or modify it
12  * under  the terms of  the GNU General  Public License as published by the
13  * Free Software Foundation;  either version 2 of the  License, or (at your
14  * option) any later version.
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 <asm/irq.h>
26
27 #define PHY_BASIC_FEATURES      (SUPPORTED_10baseT_Half | \
28                                  SUPPORTED_10baseT_Full | \
29                                  SUPPORTED_100baseT_Half | \
30                                  SUPPORTED_100baseT_Full | \
31                                  SUPPORTED_Autoneg | \
32                                  SUPPORTED_TP | \
33                                  SUPPORTED_MII)
34
35 #define PHY_GBIT_FEATURES       (PHY_BASIC_FEATURES | \
36                                  SUPPORTED_1000baseT_Half | \
37                                  SUPPORTED_1000baseT_Full)
38
39 /* Set phydev->irq to PHY_POLL if interrupts are not supported,
40  * or not desired for this PHY.  Set to PHY_IGNORE_INTERRUPT if
41  * the attached driver handles the interrupt
42  */
43 #define PHY_POLL                -1
44 #define PHY_IGNORE_INTERRUPT    -2
45
46 #define PHY_HAS_INTERRUPT       0x00000001
47 #define PHY_HAS_MAGICANEG       0x00000002
48
49 #define MII_BUS_MAX 4
50
51
52 #define PHY_INIT_TIMEOUT 100000
53 #define PHY_STATE_TIME          1
54 #define PHY_FORCE_TIMEOUT       10
55 #define PHY_AN_TIMEOUT          10
56
57 #define PHY_MAX_ADDR 32
58
59 /* Used when trying to connect to a specific phy (mii bus id:phy device id) */
60 #define PHY_ID_FMT "%x:%02x"
61
62 /* The Bus class for PHYs.  Devices which provide access to
63  * PHYs should register using this structure */
64 struct mii_bus {
65         const char *name;
66         int id;
67         void *priv;
68         int (*read)(struct mii_bus *bus, int phy_id, int regnum);
69         int (*write)(struct mii_bus *bus, int phy_id, int regnum, u16 val);
70         int (*reset)(struct mii_bus *bus);
71
72         /* A lock to ensure that only one thing can read/write
73          * the MDIO bus at a time */
74         spinlock_t mdio_lock;
75
76         struct device *dev;
77
78         /* list of all PHYs on bus */
79         struct phy_device *phy_map[PHY_MAX_ADDR];
80
81         /* Phy addresses to be ignored when probing */
82         u32 phy_mask;
83
84         /* Pointer to an array of interrupts, each PHY's
85          * interrupt at the index matching its address */
86         int *irq;
87 };
88
89 #define PHY_INTERRUPT_DISABLED 0x0
90 #define PHY_INTERRUPT_ENABLED 0x80000000
91
92 /* PHY state machine states:
93  *
94  * DOWN: PHY device and driver are not ready for anything.  probe
95  * should be called if and only if the PHY is in this state,
96  * given that the PHY device exists.
97  * - PHY driver probe function will, depending on the PHY, set
98  * the state to STARTING or READY
99  *
100  * STARTING:  PHY device is coming up, and the ethernet driver is
101  * not ready.  PHY drivers may set this in the probe function.
102  * If they do, they are responsible for making sure the state is
103  * eventually set to indicate whether the PHY is UP or READY,
104  * depending on the state when the PHY is done starting up.
105  * - PHY driver will set the state to READY
106  * - start will set the state to PENDING
107  *
108  * READY: PHY is ready to send and receive packets, but the
109  * controller is not.  By default, PHYs which do not implement
110  * probe will be set to this state by phy_probe().  If the PHY
111  * driver knows the PHY is ready, and the PHY state is STARTING,
112  * then it sets this STATE.
113  * - start will set the state to UP
114  *
115  * PENDING: PHY device is coming up, but the ethernet driver is
116  * ready.  phy_start will set this state if the PHY state is
117  * STARTING.
118  * - PHY driver will set the state to UP when the PHY is ready
119  *
120  * UP: The PHY and attached device are ready to do work.
121  * Interrupts should be started here.
122  * - timer moves to AN
123  *
124  * AN: The PHY is currently negotiating the link state.  Link is
125  * therefore down for now.  phy_timer will set this state when it
126  * detects the state is UP.  config_aneg will set this state
127  * whenever called with phydev->autoneg set to AUTONEG_ENABLE.
128  * - If autonegotiation finishes, but there's no link, it sets
129  *   the state to NOLINK.
130  * - If aneg finishes with link, it sets the state to RUNNING,
131  *   and calls adjust_link
132  * - If autonegotiation did not finish after an arbitrary amount
133  *   of time, autonegotiation should be tried again if the PHY
134  *   supports "magic" autonegotiation (back to AN)
135  * - If it didn't finish, and no magic_aneg, move to FORCING.
136  *
137  * NOLINK: PHY is up, but not currently plugged in.
138  * - If the timer notes that the link comes back, we move to RUNNING
139  * - config_aneg moves to AN
140  * - phy_stop moves to HALTED
141  *
142  * FORCING: PHY is being configured with forced settings
143  * - if link is up, move to RUNNING
144  * - If link is down, we drop to the next highest setting, and
145  *   retry (FORCING) after a timeout
146  * - phy_stop moves to HALTED
147  *
148  * RUNNING: PHY is currently up, running, and possibly sending
149  * and/or receiving packets
150  * - timer will set CHANGELINK if we're polling (this ensures the
151  *   link state is polled every other cycle of this state machine,
152  *   which makes it every other second)
153  * - irq will set CHANGELINK
154  * - config_aneg will set AN
155  * - phy_stop moves to HALTED
156  *
157  * CHANGELINK: PHY experienced a change in link state
158  * - timer moves to RUNNING if link
159  * - timer moves to NOLINK if the link is down
160  * - phy_stop moves to HALTED
161  *
162  * HALTED: PHY is up, but no polling or interrupts are done. Or
163  * PHY is in an error state.
164  *
165  * - phy_start moves to RESUMING
166  *
167  * RESUMING: PHY was halted, but now wants to run again.
168  * - If we are forcing, or aneg is done, timer moves to RUNNING
169  * - If aneg is not done, timer moves to AN
170  * - phy_stop moves to HALTED
171  */
172 enum phy_state {
173         PHY_DOWN=0,
174         PHY_STARTING,
175         PHY_READY,
176         PHY_PENDING,
177         PHY_UP,
178         PHY_AN,
179         PHY_RUNNING,
180         PHY_NOLINK,
181         PHY_FORCING,
182         PHY_CHANGELINK,
183         PHY_HALTED,
184         PHY_RESUMING
185 };
186
187 /* phy_device: An instance of a PHY
188  *
189  * drv: Pointer to the driver for this PHY instance
190  * bus: Pointer to the bus this PHY is on
191  * dev: driver model device structure for this PHY
192  * phy_id: UID for this device found during discovery
193  * state: state of the PHY for management purposes
194  * dev_flags: Device-specific flags used by the PHY driver.
195  * addr: Bus address of PHY
196  * link_timeout: The number of timer firings to wait before the
197  * giving up on the current attempt at acquiring a link
198  * irq: IRQ number of the PHY's interrupt (-1 if none)
199  * phy_timer: The timer for handling the state machine
200  * phy_queue: A work_queue for the interrupt
201  * attached_dev: The attached enet driver's device instance ptr
202  * adjust_link: Callback for the enet controller to respond to
203  * changes in the link state.
204  * adjust_state: Callback for the enet driver to respond to
205  * changes in the state machine.
206  *
207  * speed, duplex, pause, supported, advertising, and
208  * autoneg are used like in mii_if_info
209  *
210  * interrupts currently only supports enabled or disabled,
211  * but could be changed in the future to support enabling
212  * and disabling specific interrupts
213  *
214  * Contains some infrastructure for polling and interrupt
215  * handling, as well as handling shifts in PHY hardware state
216  */
217 struct phy_device {
218         /* Information about the PHY type */
219         /* And management functions */
220         struct phy_driver *drv;
221
222         struct mii_bus *bus;
223
224         struct device dev;
225
226         u32 phy_id;
227
228         enum phy_state state;
229
230         u32 dev_flags;
231
232         /* Bus address of the PHY (0-32) */
233         int addr;
234
235         /* forced speed & duplex (no autoneg)
236          * partner speed & duplex & pause (autoneg)
237          */
238         int speed;
239         int duplex;
240         int pause;
241         int asym_pause;
242
243         /* The most recently read link state */
244         int link;
245
246         /* Enabled Interrupts */
247         u32 interrupts;
248
249         /* Union of PHY and Attached devices' supported modes */
250         /* See mii.h for more info */
251         u32 supported;
252         u32 advertising;
253
254         int autoneg;
255
256         int link_timeout;
257
258         /* Interrupt number for this PHY
259          * -1 means no interrupt */
260         int irq;
261
262         /* private data pointer */
263         /* For use by PHYs to maintain extra state */
264         void *priv;
265
266         /* Interrupt and Polling infrastructure */
267         struct work_struct phy_queue;
268         struct timer_list phy_timer;
269
270         spinlock_t lock;
271
272         struct net_device *attached_dev;
273
274         void (*adjust_link)(struct net_device *dev);
275
276         void (*adjust_state)(struct net_device *dev);
277 };
278 #define to_phy_device(d) container_of(d, struct phy_device, dev)
279
280 /* struct phy_driver: Driver structure for a particular PHY type
281  *
282  * phy_id: The result of reading the UID registers of this PHY
283  *   type, and ANDing them with the phy_id_mask.  This driver
284  *   only works for PHYs with IDs which match this field
285  * name: The friendly name of this PHY type
286  * phy_id_mask: Defines the important bits of the phy_id
287  * features: A list of features (speed, duplex, etc) supported
288  *   by this PHY
289  * flags: A bitfield defining certain other features this PHY
290  *   supports (like interrupts)
291  *
292  * The drivers must implement config_aneg and read_status.  All
293  * other functions are optional. Note that none of these
294  * functions should be called from interrupt time.  The goal is
295  * for the bus read/write functions to be able to block when the
296  * bus transaction is happening, and be freed up by an interrupt
297  * (The MPC85xx has this ability, though it is not currently
298  * supported in the driver).
299  */
300 struct phy_driver {
301         u32 phy_id;
302         char *name;
303         unsigned int phy_id_mask;
304         u32 features;
305         u32 flags;
306
307         /* Called to initialize the PHY,
308          * including after a reset */
309         int (*config_init)(struct phy_device *phydev);
310
311         /* Called during discovery.  Used to set
312          * up device-specific structures, if any */
313         int (*probe)(struct phy_device *phydev);
314
315         /* PHY Power Management */
316         int (*suspend)(struct phy_device *phydev);
317         int (*resume)(struct phy_device *phydev);
318
319         /* Configures the advertisement and resets
320          * autonegotiation if phydev->autoneg is on,
321          * forces the speed to the current settings in phydev
322          * if phydev->autoneg is off */
323         int (*config_aneg)(struct phy_device *phydev);
324
325         /* Determines the negotiated speed and duplex */
326         int (*read_status)(struct phy_device *phydev);
327
328         /* Clears any pending interrupts */
329         int (*ack_interrupt)(struct phy_device *phydev);
330
331         /* Enables or disables interrupts */
332         int (*config_intr)(struct phy_device *phydev);
333
334         /* Clears up any memory if needed */
335         void (*remove)(struct phy_device *phydev);
336
337         struct device_driver driver;
338 };
339 #define to_phy_driver(d) container_of(d, struct phy_driver, driver)
340
341 int phy_read(struct phy_device *phydev, u16 regnum);
342 int phy_write(struct phy_device *phydev, u16 regnum, u16 val);
343 struct phy_device* get_phy_device(struct mii_bus *bus, int addr);
344 int phy_clear_interrupt(struct phy_device *phydev);
345 int phy_config_interrupt(struct phy_device *phydev, u32 interrupts);
346 struct phy_device * phy_attach(struct net_device *dev,
347                 const char *phy_id, u32 flags);
348 struct phy_device * phy_connect(struct net_device *dev, const char *phy_id,
349                 void (*handler)(struct net_device *), u32 flags);
350 void phy_disconnect(struct phy_device *phydev);
351 void phy_detach(struct phy_device *phydev);
352 void phy_start(struct phy_device *phydev);
353 void phy_stop(struct phy_device *phydev);
354 int phy_start_aneg(struct phy_device *phydev);
355
356 int mdiobus_register(struct mii_bus *bus);
357 void mdiobus_unregister(struct mii_bus *bus);
358 void phy_sanitize_settings(struct phy_device *phydev);
359 int phy_stop_interrupts(struct phy_device *phydev);
360
361 static inline int phy_read_status(struct phy_device *phydev) {
362         return phydev->drv->read_status(phydev);
363 }
364
365 int genphy_config_advert(struct phy_device *phydev);
366 int genphy_setup_forced(struct phy_device *phydev);
367 int genphy_restart_aneg(struct phy_device *phydev);
368 int genphy_config_aneg(struct phy_device *phydev);
369 int genphy_update_link(struct phy_device *phydev);
370 int genphy_read_status(struct phy_device *phydev);
371 void phy_driver_unregister(struct phy_driver *drv);
372 int phy_driver_register(struct phy_driver *new_driver);
373 void phy_prepare_link(struct phy_device *phydev,
374                 void (*adjust_link)(struct net_device *));
375 void phy_start_machine(struct phy_device *phydev,
376                 void (*handler)(struct net_device *));
377 void phy_stop_machine(struct phy_device *phydev);
378 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd);
379 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd);
380 int phy_mii_ioctl(struct phy_device *phydev,
381                 struct mii_ioctl_data *mii_data, int cmd);
382 int phy_start_interrupts(struct phy_device *phydev);
383 void phy_print_status(struct phy_device *phydev);
384 struct phy_device* phy_device_create(struct mii_bus *bus, int addr, int phy_id);
385
386 extern struct bus_type mdio_bus_type;
387 #endif /* __PHY_H */