| Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 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 |  | 
| Anton Vorontsov | 24c30db | 2009-07-16 21:31:31 +0000 | [diff] [blame] | 12 | #include <linux/kernel.h> | 
 | 13 | #include <linux/device.h> | 
 | 14 | #include <linux/netdevice.h> | 
 | 15 | #include <linux/err.h> | 
| Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 16 | #include <linux/phy.h> | 
 | 17 | #include <linux/of.h> | 
| Grant Likely | e387344 | 2010-06-18 11:09:59 -0600 | [diff] [blame] | 18 | #include <linux/of_irq.h> | 
| Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 19 | #include <linux/of_mdio.h> | 
 | 20 | #include <linux/module.h> | 
 | 21 |  | 
 | 22 | MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>"); | 
 | 23 | MODULE_LICENSE("GPL"); | 
 | 24 |  | 
 | 25 | /** | 
 | 26 |  * of_mdiobus_register - Register mii_bus and create PHYs from the device tree | 
 | 27 |  * @mdio: pointer to mii_bus structure | 
 | 28 |  * @np: pointer to device_node of MDIO bus. | 
 | 29 |  * | 
 | 30 |  * This function registers the mii_bus structure and registers a phy_device | 
 | 31 |  * for each child node of @np. | 
 | 32 |  */ | 
 | 33 | int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np) | 
 | 34 | { | 
 | 35 | 	struct phy_device *phy; | 
 | 36 | 	struct device_node *child; | 
 | 37 | 	int rc, i; | 
 | 38 |  | 
 | 39 | 	/* Mask out all PHYs from auto probing.  Instead the PHYs listed in | 
 | 40 | 	 * the device tree are populated after the bus has been registered */ | 
 | 41 | 	mdio->phy_mask = ~0; | 
 | 42 |  | 
 | 43 | 	/* Clear all the IRQ properties */ | 
 | 44 | 	if (mdio->irq) | 
 | 45 | 		for (i=0; i<PHY_MAX_ADDR; i++) | 
 | 46 | 			mdio->irq[i] = PHY_POLL; | 
 | 47 |  | 
 | 48 | 	/* Register the MDIO bus */ | 
 | 49 | 	rc = mdiobus_register(mdio); | 
 | 50 | 	if (rc) | 
 | 51 | 		return rc; | 
 | 52 |  | 
 | 53 | 	/* Loop over the child nodes and register a phy_device for each one */ | 
 | 54 | 	for_each_child_of_node(np, child) { | 
| David Daney | 1945886 | 2010-10-27 18:03:47 -0700 | [diff] [blame] | 55 | 		const __be32 *paddr; | 
 | 56 | 		u32 addr; | 
| Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 57 | 		int len; | 
 | 58 |  | 
 | 59 | 		/* A PHY must have a reg property in the range [0-31] */ | 
| David Daney | 1945886 | 2010-10-27 18:03:47 -0700 | [diff] [blame] | 60 | 		paddr = of_get_property(child, "reg", &len); | 
 | 61 | 		if (!paddr || len < sizeof(*paddr)) { | 
| Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 62 | 			dev_err(&mdio->dev, "%s has invalid PHY address\n", | 
 | 63 | 				child->full_name); | 
 | 64 | 			continue; | 
 | 65 | 		} | 
 | 66 |  | 
| David Daney | 1945886 | 2010-10-27 18:03:47 -0700 | [diff] [blame] | 67 | 		addr = be32_to_cpup(paddr); | 
 | 68 | 		if (addr >= 32) { | 
 | 69 | 			dev_err(&mdio->dev, "%s PHY address %i is too large\n", | 
 | 70 | 				child->full_name, addr); | 
 | 71 | 			continue; | 
| Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 72 | 		} | 
 | 73 |  | 
| David Daney | 1945886 | 2010-10-27 18:03:47 -0700 | [diff] [blame] | 74 | 		if (mdio->irq) { | 
 | 75 | 			mdio->irq[addr] = irq_of_parse_and_map(child, 0); | 
 | 76 | 			if (!mdio->irq[addr]) | 
 | 77 | 				mdio->irq[addr] = PHY_POLL; | 
 | 78 | 		} | 
 | 79 |  | 
 | 80 | 		phy = get_phy_device(mdio, addr); | 
| Dan Carpenter | 9bd7371 | 2010-04-28 01:07:29 -0600 | [diff] [blame] | 81 | 		if (!phy || IS_ERR(phy)) { | 
| Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 82 | 			dev_err(&mdio->dev, "error probing PHY at address %i\n", | 
| David Daney | 1945886 | 2010-10-27 18:03:47 -0700 | [diff] [blame] | 83 | 				addr); | 
| Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 84 | 			continue; | 
 | 85 | 		} | 
 | 86 | 		phy_scan_fixups(phy); | 
 | 87 |  | 
 | 88 | 		/* Associate the OF node with the device structure so it | 
 | 89 | 		 * can be looked up later */ | 
 | 90 | 		of_node_get(child); | 
| Grant Likely | d706c1b | 2010-04-13 16:12:28 -0700 | [diff] [blame] | 91 | 		phy->dev.of_node = child; | 
| Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 92 |  | 
 | 93 | 		/* All data is now stored in the phy struct; register it */ | 
 | 94 | 		rc = phy_device_register(phy); | 
 | 95 | 		if (rc) { | 
 | 96 | 			phy_device_free(phy); | 
 | 97 | 			of_node_put(child); | 
 | 98 | 			continue; | 
 | 99 | 		} | 
 | 100 |  | 
 | 101 | 		dev_dbg(&mdio->dev, "registered phy %s at address %i\n", | 
| David Daney | 1945886 | 2010-10-27 18:03:47 -0700 | [diff] [blame] | 102 | 			child->name, addr); | 
| Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 103 | 	} | 
 | 104 |  | 
 | 105 | 	return 0; | 
 | 106 | } | 
 | 107 | EXPORT_SYMBOL(of_mdiobus_register); | 
 | 108 |  | 
| Jérôme Pouiller | 08a7963 | 2009-10-15 09:58:27 -0600 | [diff] [blame] | 109 | /* Helper function for of_phy_find_device */ | 
 | 110 | static int of_phy_match(struct device *dev, void *phy_np) | 
 | 111 | { | 
| Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 112 | 	return dev->of_node == phy_np; | 
| Jérôme Pouiller | 08a7963 | 2009-10-15 09:58:27 -0600 | [diff] [blame] | 113 | } | 
 | 114 |  | 
| Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 115 | /** | 
 | 116 |  * of_phy_find_device - Give a PHY node, find the phy_device | 
 | 117 |  * @phy_np: Pointer to the phy's device tree node | 
 | 118 |  * | 
 | 119 |  * Returns a pointer to the phy_device. | 
 | 120 |  */ | 
 | 121 | struct phy_device *of_phy_find_device(struct device_node *phy_np) | 
 | 122 | { | 
 | 123 | 	struct device *d; | 
| Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 124 | 	if (!phy_np) | 
 | 125 | 		return NULL; | 
 | 126 |  | 
| Jérôme Pouiller | 08a7963 | 2009-10-15 09:58:27 -0600 | [diff] [blame] | 127 | 	d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match); | 
| Grant Likely | 8bc487d | 2009-04-25 12:52:56 +0000 | [diff] [blame] | 128 | 	return d ? to_phy_device(d) : NULL; | 
 | 129 | } | 
 | 130 | EXPORT_SYMBOL(of_phy_find_device); | 
 | 131 |  | 
 | 132 | /** | 
 | 133 |  * of_phy_connect - Connect to the phy described in the device tree | 
 | 134 |  * @dev: pointer to net_device claiming the phy | 
 | 135 |  * @phy_np: Pointer to device tree node for the PHY | 
 | 136 |  * @hndlr: Link state callback for the network device | 
 | 137 |  * @iface: PHY data interface type | 
 | 138 |  * | 
 | 139 |  * Returns a pointer to the phy_device if successfull.  NULL otherwise | 
 | 140 |  */ | 
 | 141 | struct phy_device *of_phy_connect(struct net_device *dev, | 
 | 142 | 				  struct device_node *phy_np, | 
 | 143 | 				  void (*hndlr)(struct net_device *), u32 flags, | 
 | 144 | 				  phy_interface_t iface) | 
 | 145 | { | 
 | 146 | 	struct phy_device *phy = of_phy_find_device(phy_np); | 
 | 147 |  | 
 | 148 | 	if (!phy) | 
 | 149 | 		return NULL; | 
 | 150 |  | 
 | 151 | 	return phy_connect_direct(dev, phy, hndlr, flags, iface) ? NULL : phy; | 
 | 152 | } | 
 | 153 | EXPORT_SYMBOL(of_phy_connect); | 
| Anton Vorontsov | 24c30db | 2009-07-16 21:31:31 +0000 | [diff] [blame] | 154 |  | 
 | 155 | /** | 
 | 156 |  * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy | 
 | 157 |  * @dev: pointer to net_device claiming the phy | 
 | 158 |  * @hndlr: Link state callback for the network device | 
 | 159 |  * @iface: PHY data interface type | 
 | 160 |  * | 
 | 161 |  * This function is a temporary stop-gap and will be removed soon.  It is | 
 | 162 |  * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers.  Do | 
 | 163 |  * not call this function from new drivers. | 
 | 164 |  */ | 
 | 165 | struct phy_device *of_phy_connect_fixed_link(struct net_device *dev, | 
 | 166 | 					     void (*hndlr)(struct net_device *), | 
 | 167 | 					     phy_interface_t iface) | 
 | 168 | { | 
 | 169 | 	struct device_node *net_np; | 
 | 170 | 	char bus_id[MII_BUS_ID_SIZE + 3]; | 
 | 171 | 	struct phy_device *phy; | 
| Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 172 | 	const __be32 *phy_id; | 
| Anton Vorontsov | 24c30db | 2009-07-16 21:31:31 +0000 | [diff] [blame] | 173 | 	int sz; | 
 | 174 |  | 
 | 175 | 	if (!dev->dev.parent) | 
 | 176 | 		return NULL; | 
 | 177 |  | 
| Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 178 | 	net_np = dev->dev.parent->of_node; | 
| Anton Vorontsov | 24c30db | 2009-07-16 21:31:31 +0000 | [diff] [blame] | 179 | 	if (!net_np) | 
 | 180 | 		return NULL; | 
 | 181 |  | 
 | 182 | 	phy_id = of_get_property(net_np, "fixed-link", &sz); | 
 | 183 | 	if (!phy_id || sz < sizeof(*phy_id)) | 
 | 184 | 		return NULL; | 
 | 185 |  | 
| Jeremy Kerr | 3371488 | 2010-01-30 01:45:26 -0700 | [diff] [blame] | 186 | 	sprintf(bus_id, PHY_ID_FMT, "0", be32_to_cpu(phy_id[0])); | 
| Anton Vorontsov | 24c30db | 2009-07-16 21:31:31 +0000 | [diff] [blame] | 187 |  | 
 | 188 | 	phy = phy_connect(dev, bus_id, hndlr, 0, iface); | 
 | 189 | 	return IS_ERR(phy) ? NULL : phy; | 
 | 190 | } | 
 | 191 | EXPORT_SYMBOL(of_phy_connect_fixed_link); |