| Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1 | /* | 
|  | 2 | * drivers/net/phy/mdio_bus.c | 
|  | 3 | * | 
|  | 4 | * MDIO Bus interface | 
|  | 5 | * | 
|  | 6 | * Author: Andy Fleming | 
|  | 7 | * | 
|  | 8 | * Copyright (c) 2004 Freescale Semiconductor, Inc. | 
|  | 9 | * | 
|  | 10 | * This program is free software; you can redistribute  it and/or modify it | 
|  | 11 | * under  the terms of  the GNU General  Public License as published by the | 
|  | 12 | * Free Software Foundation;  either version 2 of the  License, or (at your | 
|  | 13 | * option) any later version. | 
|  | 14 | * | 
|  | 15 | */ | 
| Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 16 | #include <linux/kernel.h> | 
|  | 17 | #include <linux/sched.h> | 
|  | 18 | #include <linux/string.h> | 
|  | 19 | #include <linux/errno.h> | 
|  | 20 | #include <linux/unistd.h> | 
|  | 21 | #include <linux/slab.h> | 
|  | 22 | #include <linux/interrupt.h> | 
|  | 23 | #include <linux/init.h> | 
|  | 24 | #include <linux/delay.h> | 
|  | 25 | #include <linux/netdevice.h> | 
|  | 26 | #include <linux/etherdevice.h> | 
|  | 27 | #include <linux/skbuff.h> | 
|  | 28 | #include <linux/spinlock.h> | 
|  | 29 | #include <linux/mm.h> | 
|  | 30 | #include <linux/module.h> | 
| Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 31 | #include <linux/mii.h> | 
|  | 32 | #include <linux/ethtool.h> | 
|  | 33 | #include <linux/phy.h> | 
|  | 34 |  | 
|  | 35 | #include <asm/io.h> | 
|  | 36 | #include <asm/irq.h> | 
|  | 37 | #include <asm/uaccess.h> | 
|  | 38 |  | 
| Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 39 | /* mdiobus_register | 
|  | 40 | * | 
|  | 41 | * description: Called by a bus driver to bring up all the PHYs | 
|  | 42 | *   on a given bus, and attach them to the bus | 
|  | 43 | */ | 
|  | 44 | int mdiobus_register(struct mii_bus *bus) | 
|  | 45 | { | 
|  | 46 | int i; | 
|  | 47 | int err = 0; | 
|  | 48 |  | 
|  | 49 | spin_lock_init(&bus->mdio_lock); | 
|  | 50 |  | 
|  | 51 | if (NULL == bus || NULL == bus->name || | 
|  | 52 | NULL == bus->read || | 
|  | 53 | NULL == bus->write) | 
|  | 54 | return -EINVAL; | 
|  | 55 |  | 
|  | 56 | if (bus->reset) | 
|  | 57 | bus->reset(bus); | 
|  | 58 |  | 
|  | 59 | for (i = 0; i < PHY_MAX_ADDR; i++) { | 
|  | 60 | struct phy_device *phydev; | 
|  | 61 |  | 
| Herbert Valerio Riedel | 64b1c2b | 2006-05-10 12:12:57 -0400 | [diff] [blame] | 62 | if (bus->phy_mask & (1 << i)) { | 
|  | 63 | bus->phy_map[i] = NULL; | 
| Matt Porter | f896424 | 2005-11-02 16:13:06 -0700 | [diff] [blame] | 64 | continue; | 
| Herbert Valerio Riedel | 64b1c2b | 2006-05-10 12:12:57 -0400 | [diff] [blame] | 65 | } | 
| Matt Porter | f896424 | 2005-11-02 16:13:06 -0700 | [diff] [blame] | 66 |  | 
| Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 67 | phydev = get_phy_device(bus, i); | 
|  | 68 |  | 
|  | 69 | if (IS_ERR(phydev)) | 
|  | 70 | return PTR_ERR(phydev); | 
|  | 71 |  | 
|  | 72 | /* There's a PHY at this address | 
|  | 73 | * We need to set: | 
|  | 74 | * 1) IRQ | 
|  | 75 | * 2) bus_id | 
|  | 76 | * 3) parent | 
|  | 77 | * 4) bus | 
|  | 78 | * 5) mii_bus | 
|  | 79 | * And, we need to register it */ | 
|  | 80 | if (phydev) { | 
|  | 81 | phydev->irq = bus->irq[i]; | 
|  | 82 |  | 
|  | 83 | phydev->dev.parent = bus->dev; | 
|  | 84 | phydev->dev.bus = &mdio_bus_type; | 
| Kumar Gala | a4d00f1 | 2006-01-11 11:27:33 -0800 | [diff] [blame] | 85 | snprintf(phydev->dev.bus_id, BUS_ID_SIZE, PHY_ID_FMT, bus->id, i); | 
| Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 86 |  | 
|  | 87 | phydev->bus = bus; | 
|  | 88 |  | 
|  | 89 | err = device_register(&phydev->dev); | 
|  | 90 |  | 
|  | 91 | if (err) | 
|  | 92 | printk(KERN_ERR "phy %d failed to register\n", | 
|  | 93 | i); | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | bus->phy_map[i] = phydev; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | pr_info("%s: probed\n", bus->name); | 
|  | 100 |  | 
|  | 101 | return err; | 
|  | 102 | } | 
|  | 103 | EXPORT_SYMBOL(mdiobus_register); | 
|  | 104 |  | 
|  | 105 | void mdiobus_unregister(struct mii_bus *bus) | 
|  | 106 | { | 
|  | 107 | int i; | 
|  | 108 |  | 
|  | 109 | for (i = 0; i < PHY_MAX_ADDR; i++) { | 
|  | 110 | if (bus->phy_map[i]) { | 
|  | 111 | device_unregister(&bus->phy_map[i]->dev); | 
|  | 112 | kfree(bus->phy_map[i]); | 
|  | 113 | } | 
|  | 114 | } | 
|  | 115 | } | 
|  | 116 | EXPORT_SYMBOL(mdiobus_unregister); | 
|  | 117 |  | 
| Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 118 | /* mdio_bus_match | 
|  | 119 | * | 
|  | 120 | * description: Given a PHY device, and a PHY driver, return 1 if | 
|  | 121 | *   the driver supports the device.  Otherwise, return 0 | 
|  | 122 | */ | 
|  | 123 | static int mdio_bus_match(struct device *dev, struct device_driver *drv) | 
|  | 124 | { | 
|  | 125 | struct phy_device *phydev = to_phy_device(dev); | 
|  | 126 | struct phy_driver *phydrv = to_phy_driver(drv); | 
|  | 127 |  | 
|  | 128 | return (phydrv->phy_id == (phydev->phy_id & phydrv->phy_id_mask)); | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | /* Suspend and resume.  Copied from platform_suspend and | 
|  | 132 | * platform_resume | 
|  | 133 | */ | 
| Pavel Machek | 829ca9a | 2005-09-03 15:56:56 -0700 | [diff] [blame] | 134 | static int mdio_bus_suspend(struct device * dev, pm_message_t state) | 
| Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 135 | { | 
|  | 136 | int ret = 0; | 
|  | 137 | struct device_driver *drv = dev->driver; | 
|  | 138 |  | 
| Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 139 | if (drv && drv->suspend) | 
|  | 140 | ret = drv->suspend(dev, state); | 
|  | 141 |  | 
| Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 142 | return ret; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | static int mdio_bus_resume(struct device * dev) | 
|  | 146 | { | 
|  | 147 | int ret = 0; | 
|  | 148 | struct device_driver *drv = dev->driver; | 
|  | 149 |  | 
| Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 150 | if (drv && drv->resume) | 
|  | 151 | ret = drv->resume(dev); | 
|  | 152 |  | 
| Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 153 | return ret; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | struct bus_type mdio_bus_type = { | 
|  | 157 | .name		= "mdio_bus", | 
|  | 158 | .match		= mdio_bus_match, | 
|  | 159 | .suspend	= mdio_bus_suspend, | 
|  | 160 | .resume		= mdio_bus_resume, | 
|  | 161 | }; | 
| Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 162 | EXPORT_SYMBOL(mdio_bus_type); | 
| Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 163 |  | 
| Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 164 | int __init mdio_bus_init(void) | 
| Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 165 | { | 
|  | 166 | return bus_register(&mdio_bus_type); | 
|  | 167 | } | 
|  | 168 |  | 
| Peter Chubb | dc85dec | 2005-09-03 14:05:06 -0700 | [diff] [blame] | 169 | void mdio_bus_exit(void) | 
| Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 170 | { | 
|  | 171 | bus_unregister(&mdio_bus_type); | 
|  | 172 | } |