aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/of
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/of')
-rw-r--r--drivers/of/Kconfig6
-rw-r--r--drivers/of/Makefile1
-rw-r--r--drivers/of/base.c24
-rw-r--r--drivers/of/of_mdio.c139
4 files changed, 170 insertions, 0 deletions
diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
index 27f3b81333de..d2fa27c5c1b2 100644
--- a/drivers/of/Kconfig
+++ b/drivers/of/Kconfig
@@ -19,3 +19,9 @@ config OF_SPI
19 depends on OF && (PPC_OF || MICROBLAZE) && SPI 19 depends on OF && (PPC_OF || MICROBLAZE) && SPI
20 help 20 help
21 OpenFirmware SPI accessors 21 OpenFirmware SPI accessors
22
23config OF_MDIO
24 def_tristate PHYLIB
25 depends on OF && PHYLIB
26 help
27 OpenFirmware MDIO bus (Ethernet PHY) accessors
diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index 4c3c6f8e36f5..bdfb5f5d4b06 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -3,3 +3,4 @@ obj-$(CONFIG_OF_DEVICE) += device.o platform.o
3obj-$(CONFIG_OF_GPIO) += gpio.o 3obj-$(CONFIG_OF_GPIO) += gpio.o
4obj-$(CONFIG_OF_I2C) += of_i2c.o 4obj-$(CONFIG_OF_I2C) += of_i2c.o
5obj-$(CONFIG_OF_SPI) += of_spi.o 5obj-$(CONFIG_OF_SPI) += of_spi.o
6obj-$(CONFIG_OF_MDIO) += of_mdio.o
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 391f91c0bf55..69f85c07d17f 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -496,6 +496,30 @@ int of_modalias_node(struct device_node *node, char *modalias, int len)
496EXPORT_SYMBOL_GPL(of_modalias_node); 496EXPORT_SYMBOL_GPL(of_modalias_node);
497 497
498/** 498/**
499 * of_parse_phandle - Resolve a phandle property to a device_node pointer
500 * @np: Pointer to device node holding phandle property
501 * @phandle_name: Name of property holding a phandle value
502 * @index: For properties holding a table of phandles, this is the index into
503 * the table
504 *
505 * Returns the device_node pointer with refcount incremented. Use
506 * of_node_put() on it when done.
507 */
508struct device_node *
509of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
510{
511 const phandle *phandle;
512 int size;
513
514 phandle = of_get_property(np, phandle_name, &size);
515 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
516 return NULL;
517
518 return of_find_node_by_phandle(phandle[index]);
519}
520EXPORT_SYMBOL(of_parse_phandle);
521
522/**
499 * of_parse_phandles_with_args - Find a node pointed by phandle in a list 523 * of_parse_phandles_with_args - Find a node pointed by phandle in a list
500 * @np: pointer to a device tree node containing a list 524 * @np: pointer to a device tree node containing a list
501 * @list_name: property name that contains a list 525 * @list_name: property name that contains a list
diff --git a/drivers/of/of_mdio.c b/drivers/of/of_mdio.c
new file mode 100644
index 000000000000..aee967d7f760
--- /dev/null
+++ b/drivers/of/of_mdio.c
@@ -0,0 +1,139 @@
1/*
2 * OF helpers for the MDIO (Ethernet PHY) API
3 *
4 * Copyright (c) 2009 Secret Lab Technologies, Ltd.
5 *
6 * This file is released under the GPLv2
7 *
8 * This file provides helper functions for extracting PHY device information
9 * out of the OpenFirmware device tree and using it to populate an mii_bus.
10 */
11
12#include <linux/phy.h>
13#include <linux/of.h>
14#include <linux/of_mdio.h>
15#include <linux/module.h>
16
17MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
18MODULE_LICENSE("GPL");
19
20/**
21 * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
22 * @mdio: pointer to mii_bus structure
23 * @np: pointer to device_node of MDIO bus.
24 *
25 * This function registers the mii_bus structure and registers a phy_device
26 * for each child node of @np.
27 */
28int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
29{
30 struct phy_device *phy;
31 struct device_node *child;
32 int rc, i;
33
34 /* Mask out all PHYs from auto probing. Instead the PHYs listed in
35 * the device tree are populated after the bus has been registered */
36 mdio->phy_mask = ~0;
37
38 /* Clear all the IRQ properties */
39 if (mdio->irq)
40 for (i=0; i<PHY_MAX_ADDR; i++)
41 mdio->irq[i] = PHY_POLL;
42
43 /* Register the MDIO bus */
44 rc = mdiobus_register(mdio);
45 if (rc)
46 return rc;
47
48 /* Loop over the child nodes and register a phy_device for each one */
49 for_each_child_of_node(np, child) {
50 const u32 *addr;
51 int len;
52
53 /* A PHY must have a reg property in the range [0-31] */
54 addr = of_get_property(child, "reg", &len);
55 if (!addr || len < sizeof(*addr) || *addr >= 32 || *addr < 0) {
56 dev_err(&mdio->dev, "%s has invalid PHY address\n",
57 child->full_name);
58 continue;
59 }
60
61 if (mdio->irq) {
62 mdio->irq[*addr] = irq_of_parse_and_map(child, 0);
63 if (!mdio->irq[*addr])
64 mdio->irq[*addr] = PHY_POLL;
65 }
66
67 phy = get_phy_device(mdio, *addr);
68 if (!phy) {
69 dev_err(&mdio->dev, "error probing PHY at address %i\n",
70 *addr);
71 continue;
72 }
73 phy_scan_fixups(phy);
74
75 /* Associate the OF node with the device structure so it
76 * can be looked up later */
77 of_node_get(child);
78 dev_archdata_set_node(&phy->dev.archdata, child);
79
80 /* All data is now stored in the phy struct; register it */
81 rc = phy_device_register(phy);
82 if (rc) {
83 phy_device_free(phy);
84 of_node_put(child);
85 continue;
86 }
87
88 dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
89 child->name, *addr);
90 }
91
92 return 0;
93}
94EXPORT_SYMBOL(of_mdiobus_register);
95
96/**
97 * of_phy_find_device - Give a PHY node, find the phy_device
98 * @phy_np: Pointer to the phy's device tree node
99 *
100 * Returns a pointer to the phy_device.
101 */
102struct phy_device *of_phy_find_device(struct device_node *phy_np)
103{
104 struct device *d;
105 int match(struct device *dev, void *phy_np)
106 {
107 return dev_archdata_get_node(&dev->archdata) == phy_np;
108 }
109
110 if (!phy_np)
111 return NULL;
112
113 d = bus_find_device(&mdio_bus_type, NULL, phy_np, match);
114 return d ? to_phy_device(d) : NULL;
115}
116EXPORT_SYMBOL(of_phy_find_device);
117
118/**
119 * of_phy_connect - Connect to the phy described in the device tree
120 * @dev: pointer to net_device claiming the phy
121 * @phy_np: Pointer to device tree node for the PHY
122 * @hndlr: Link state callback for the network device
123 * @iface: PHY data interface type
124 *
125 * Returns a pointer to the phy_device if successfull. NULL otherwise
126 */
127struct phy_device *of_phy_connect(struct net_device *dev,
128 struct device_node *phy_np,
129 void (*hndlr)(struct net_device *), u32 flags,
130 phy_interface_t iface)
131{
132 struct phy_device *phy = of_phy_find_device(phy_np);
133
134 if (!phy)
135 return NULL;
136
137 return phy_connect_direct(dev, phy, hndlr, flags, iface) ? NULL : phy;
138}
139EXPORT_SYMBOL(of_phy_connect);