aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/macb.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/net/macb.c')
-rw-r--r--drivers/net/macb.c563
1 files changed, 316 insertions, 247 deletions
diff --git a/drivers/net/macb.c b/drivers/net/macb.c
index 0e04f7ac3f2e..a4bb0264180a 100644
--- a/drivers/net/macb.c
+++ b/drivers/net/macb.c
@@ -17,13 +17,12 @@
17#include <linux/init.h> 17#include <linux/init.h>
18#include <linux/netdevice.h> 18#include <linux/netdevice.h>
19#include <linux/etherdevice.h> 19#include <linux/etherdevice.h>
20#include <linux/mii.h>
21#include <linux/mutex.h>
22#include <linux/dma-mapping.h> 20#include <linux/dma-mapping.h>
23#include <linux/ethtool.h>
24#include <linux/platform_device.h> 21#include <linux/platform_device.h>
22#include <linux/phy.h>
25 23
26#include <asm/arch/board.h> 24#include <asm/arch/board.h>
25#include <asm/arch/cpu.h>
27 26
28#include "macb.h" 27#include "macb.h"
29 28
@@ -85,172 +84,202 @@ static void __init macb_get_hwaddr(struct macb *bp)
85 memcpy(bp->dev->dev_addr, addr, sizeof(addr)); 84 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
86} 85}
87 86
88static void macb_enable_mdio(struct macb *bp) 87static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
89{ 88{
90 unsigned long flags; 89 struct macb *bp = bus->priv;
91 u32 reg;
92
93 spin_lock_irqsave(&bp->lock, flags);
94 reg = macb_readl(bp, NCR);
95 reg |= MACB_BIT(MPE);
96 macb_writel(bp, NCR, reg);
97 macb_writel(bp, IER, MACB_BIT(MFD));
98 spin_unlock_irqrestore(&bp->lock, flags);
99}
100
101static void macb_disable_mdio(struct macb *bp)
102{
103 unsigned long flags;
104 u32 reg;
105
106 spin_lock_irqsave(&bp->lock, flags);
107 reg = macb_readl(bp, NCR);
108 reg &= ~MACB_BIT(MPE);
109 macb_writel(bp, NCR, reg);
110 macb_writel(bp, IDR, MACB_BIT(MFD));
111 spin_unlock_irqrestore(&bp->lock, flags);
112}
113
114static int macb_mdio_read(struct net_device *dev, int phy_id, int location)
115{
116 struct macb *bp = netdev_priv(dev);
117 int value; 90 int value;
118 91
119 mutex_lock(&bp->mdio_mutex);
120
121 macb_enable_mdio(bp);
122 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF) 92 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
123 | MACB_BF(RW, MACB_MAN_READ) 93 | MACB_BF(RW, MACB_MAN_READ)
124 | MACB_BF(PHYA, phy_id) 94 | MACB_BF(PHYA, mii_id)
125 | MACB_BF(REGA, location) 95 | MACB_BF(REGA, regnum)
126 | MACB_BF(CODE, MACB_MAN_CODE))); 96 | MACB_BF(CODE, MACB_MAN_CODE)));
127 97
128 wait_for_completion(&bp->mdio_complete); 98 /* wait for end of transfer */
99 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
100 cpu_relax();
129 101
130 value = MACB_BFEXT(DATA, macb_readl(bp, MAN)); 102 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
131 macb_disable_mdio(bp);
132 mutex_unlock(&bp->mdio_mutex);
133 103
134 return value; 104 return value;
135} 105}
136 106
137static void macb_mdio_write(struct net_device *dev, int phy_id, 107static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
138 int location, int val) 108 u16 value)
139{ 109{
140 struct macb *bp = netdev_priv(dev); 110 struct macb *bp = bus->priv;
141
142 dev_dbg(&bp->pdev->dev, "mdio_write %02x:%02x <- %04x\n",
143 phy_id, location, val);
144
145 mutex_lock(&bp->mdio_mutex);
146 macb_enable_mdio(bp);
147 111
148 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF) 112 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
149 | MACB_BF(RW, MACB_MAN_WRITE) 113 | MACB_BF(RW, MACB_MAN_WRITE)
150 | MACB_BF(PHYA, phy_id) 114 | MACB_BF(PHYA, mii_id)
151 | MACB_BF(REGA, location) 115 | MACB_BF(REGA, regnum)
152 | MACB_BF(CODE, MACB_MAN_CODE) 116 | MACB_BF(CODE, MACB_MAN_CODE)
153 | MACB_BF(DATA, val))); 117 | MACB_BF(DATA, value)));
154 118
155 wait_for_completion(&bp->mdio_complete); 119 /* wait for end of transfer */
120 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
121 cpu_relax();
156 122
157 macb_disable_mdio(bp); 123 return 0;
158 mutex_unlock(&bp->mdio_mutex);
159} 124}
160 125
161static int macb_phy_probe(struct macb *bp) 126static int macb_mdio_reset(struct mii_bus *bus)
162{ 127{
163 int phy_address; 128 return 0;
164 u16 phyid1, phyid2; 129}
165 130
166 for (phy_address = 0; phy_address < 32; phy_address++) { 131static void macb_handle_link_change(struct net_device *dev)
167 phyid1 = macb_mdio_read(bp->dev, phy_address, MII_PHYSID1); 132{
168 phyid2 = macb_mdio_read(bp->dev, phy_address, MII_PHYSID2); 133 struct macb *bp = netdev_priv(dev);
134 struct phy_device *phydev = bp->phy_dev;
135 unsigned long flags;
169 136
170 if (phyid1 != 0xffff && phyid1 != 0x0000 137 int status_change = 0;
171 && phyid2 != 0xffff && phyid2 != 0x0000) 138
172 break; 139 spin_lock_irqsave(&bp->lock, flags);
140
141 if (phydev->link) {
142 if ((bp->speed != phydev->speed) ||
143 (bp->duplex != phydev->duplex)) {
144 u32 reg;
145
146 reg = macb_readl(bp, NCFGR);
147 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
148
149 if (phydev->duplex)
150 reg |= MACB_BIT(FD);
151 if (phydev->speed)
152 reg |= MACB_BIT(SPD);
153
154 macb_writel(bp, NCFGR, reg);
155
156 bp->speed = phydev->speed;
157 bp->duplex = phydev->duplex;
158 status_change = 1;
159 }
173 } 160 }
174 161
175 if (phy_address == 32) 162 if (phydev->link != bp->link) {
176 return -ENODEV; 163 if (phydev->link)
164 netif_schedule(dev);
165 else {
166 bp->speed = 0;
167 bp->duplex = -1;
168 }
169 bp->link = phydev->link;
177 170
178 dev_info(&bp->pdev->dev, 171 status_change = 1;
179 "detected PHY at address %d (ID %04x:%04x)\n", 172 }
180 phy_address, phyid1, phyid2);
181 173
182 bp->mii.phy_id = phy_address; 174 spin_unlock_irqrestore(&bp->lock, flags);
183 return 0; 175
176 if (status_change) {
177 if (phydev->link)
178 printk(KERN_INFO "%s: link up (%d/%s)\n",
179 dev->name, phydev->speed,
180 DUPLEX_FULL == phydev->duplex ? "Full":"Half");
181 else
182 printk(KERN_INFO "%s: link down\n", dev->name);
183 }
184} 184}
185 185
186static void macb_set_media(struct macb *bp, int media) 186/* based on au1000_eth. c*/
187static int macb_mii_probe(struct net_device *dev)
187{ 188{
188 u32 reg; 189 struct macb *bp = netdev_priv(dev);
190 struct phy_device *phydev = NULL;
191 struct eth_platform_data *pdata;
192 int phy_addr;
189 193
190 spin_lock_irq(&bp->lock); 194 /* find the first phy */
191 reg = macb_readl(bp, NCFGR); 195 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
192 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD)); 196 if (bp->mii_bus.phy_map[phy_addr]) {
193 if (media & (ADVERTISE_100HALF | ADVERTISE_100FULL)) 197 phydev = bp->mii_bus.phy_map[phy_addr];
194 reg |= MACB_BIT(SPD); 198 break;
195 if (media & ADVERTISE_FULL) 199 }
196 reg |= MACB_BIT(FD); 200 }
197 macb_writel(bp, NCFGR, reg); 201
198 spin_unlock_irq(&bp->lock); 202 if (!phydev) {
203 printk (KERN_ERR "%s: no PHY found\n", dev->name);
204 return -1;
205 }
206
207 pdata = bp->pdev->dev.platform_data;
208 /* TODO : add pin_irq */
209
210 /* attach the mac to the phy */
211 if (pdata && pdata->is_rmii) {
212 phydev = phy_connect(dev, phydev->dev.bus_id,
213 &macb_handle_link_change, 0, PHY_INTERFACE_MODE_RMII);
214 } else {
215 phydev = phy_connect(dev, phydev->dev.bus_id,
216 &macb_handle_link_change, 0, PHY_INTERFACE_MODE_MII);
217 }
218
219 if (IS_ERR(phydev)) {
220 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
221 return PTR_ERR(phydev);
222 }
223
224 /* mask with MAC supported features */
225 phydev->supported &= PHY_BASIC_FEATURES;
226
227 phydev->advertising = phydev->supported;
228
229 bp->link = 0;
230 bp->speed = 0;
231 bp->duplex = -1;
232 bp->phy_dev = phydev;
233
234 return 0;
199} 235}
200 236
201static void macb_check_media(struct macb *bp, int ok_to_print, int init_media) 237static int macb_mii_init(struct macb *bp)
202{ 238{
203 struct mii_if_info *mii = &bp->mii; 239 struct eth_platform_data *pdata;
204 unsigned int old_carrier, new_carrier; 240 int err = -ENXIO, i;
205 int advertise, lpa, media, duplex;
206 241
207 /* if forced media, go no further */ 242 /* Enable managment port */
208 if (mii->force_media) 243 macb_writel(bp, NCR, MACB_BIT(MPE));
209 return;
210 244
211 /* check current and old link status */ 245 bp->mii_bus.name = "MACB_mii_bus",
212 old_carrier = netif_carrier_ok(mii->dev) ? 1 : 0; 246 bp->mii_bus.read = &macb_mdio_read,
213 new_carrier = (unsigned int) mii_link_ok(mii); 247 bp->mii_bus.write = &macb_mdio_write,
248 bp->mii_bus.reset = &macb_mdio_reset,
249 bp->mii_bus.id = bp->pdev->id,
250 bp->mii_bus.priv = bp,
251 bp->mii_bus.dev = &bp->dev->dev;
252 pdata = bp->pdev->dev.platform_data;
214 253
215 /* if carrier state did not change, assume nothing else did */ 254 if (pdata)
216 if (!init_media && old_carrier == new_carrier) 255 bp->mii_bus.phy_mask = pdata->phy_mask;
217 return;
218 256
219 /* no carrier, nothing much to do */ 257 bp->mii_bus.irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
220 if (!new_carrier) { 258 if (!bp->mii_bus.irq) {
221 netif_carrier_off(mii->dev); 259 err = -ENOMEM;
222 printk(KERN_INFO "%s: link down\n", mii->dev->name); 260 goto err_out;
223 return;
224 } 261 }
225 262
226 /* 263 for (i = 0; i < PHY_MAX_ADDR; i++)
227 * we have carrier, see who's on the other end 264 bp->mii_bus.irq[i] = PHY_POLL;
228 */
229 netif_carrier_on(mii->dev);
230 265
231 /* get MII advertise and LPA values */ 266 platform_set_drvdata(bp->dev, &bp->mii_bus);
232 if (!init_media && mii->advertising) {
233 advertise = mii->advertising;
234 } else {
235 advertise = mii->mdio_read(mii->dev, mii->phy_id, MII_ADVERTISE);
236 mii->advertising = advertise;
237 }
238 lpa = mii->mdio_read(mii->dev, mii->phy_id, MII_LPA);
239 267
240 /* figure out media and duplex from advertise and LPA values */ 268 if (mdiobus_register(&bp->mii_bus))
241 media = mii_nway_result(lpa & advertise); 269 goto err_out_free_mdio_irq;
242 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
243 270
244 if (ok_to_print) 271 if (macb_mii_probe(bp->dev) != 0) {
245 printk(KERN_INFO "%s: link up, %sMbps, %s-duplex, lpa 0x%04X\n", 272 goto err_out_unregister_bus;
246 mii->dev->name, 273 }
247 media & (ADVERTISE_100FULL | ADVERTISE_100HALF) ? "100" : "10",
248 duplex ? "full" : "half", lpa);
249 274
250 mii->full_duplex = duplex; 275 return 0;
251 276
252 /* Let the MAC know about the new link state */ 277err_out_unregister_bus:
253 macb_set_media(bp, media); 278 mdiobus_unregister(&bp->mii_bus);
279err_out_free_mdio_irq:
280 kfree(bp->mii_bus.irq);
281err_out:
282 return err;
254} 283}
255 284
256static void macb_update_stats(struct macb *bp) 285static void macb_update_stats(struct macb *bp)
@@ -265,16 +294,6 @@ static void macb_update_stats(struct macb *bp)
265 *p += __raw_readl(reg); 294 *p += __raw_readl(reg);
266} 295}
267 296
268static void macb_periodic_task(struct work_struct *work)
269{
270 struct macb *bp = container_of(work, struct macb, periodic_task.work);
271
272 macb_update_stats(bp);
273 macb_check_media(bp, 1, 0);
274
275 schedule_delayed_work(&bp->periodic_task, HZ);
276}
277
278static void macb_tx(struct macb *bp) 297static void macb_tx(struct macb *bp)
279{ 298{
280 unsigned int tail; 299 unsigned int tail;
@@ -519,9 +538,6 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
519 spin_lock(&bp->lock); 538 spin_lock(&bp->lock);
520 539
521 while (status) { 540 while (status) {
522 if (status & MACB_BIT(MFD))
523 complete(&bp->mdio_complete);
524
525 /* close possible race with dev_close */ 541 /* close possible race with dev_close */
526 if (unlikely(!netif_running(dev))) { 542 if (unlikely(!netif_running(dev))) {
527 macb_writel(bp, IDR, ~0UL); 543 macb_writel(bp, IDR, ~0UL);
@@ -535,7 +551,8 @@ static irqreturn_t macb_interrupt(int irq, void *dev_id)
535 * until we have processed the buffers 551 * until we have processed the buffers
536 */ 552 */
537 macb_writel(bp, IDR, MACB_RX_INT_FLAGS); 553 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
538 dev_dbg(&bp->pdev->dev, "scheduling RX softirq\n"); 554 dev_dbg(&bp->pdev->dev,
555 "scheduling RX softirq\n");
539 __netif_rx_schedule(dev); 556 __netif_rx_schedule(dev);
540 } 557 }
541 } 558 }
@@ -765,7 +782,7 @@ static void macb_init_hw(struct macb *bp)
765 macb_writel(bp, TBQP, bp->tx_ring_dma); 782 macb_writel(bp, TBQP, bp->tx_ring_dma);
766 783
767 /* Enable TX and RX */ 784 /* Enable TX and RX */
768 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE)); 785 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
769 786
770 /* Enable interrupts */ 787 /* Enable interrupts */
771 macb_writel(bp, IER, (MACB_BIT(RCOMP) 788 macb_writel(bp, IER, (MACB_BIT(RCOMP)
@@ -776,18 +793,126 @@ static void macb_init_hw(struct macb *bp)
776 | MACB_BIT(TCOMP) 793 | MACB_BIT(TCOMP)
777 | MACB_BIT(ISR_ROVR) 794 | MACB_BIT(ISR_ROVR)
778 | MACB_BIT(HRESP))); 795 | MACB_BIT(HRESP)));
796
779} 797}
780 798
781static void macb_init_phy(struct net_device *dev) 799/*
800 * The hash address register is 64 bits long and takes up two
801 * locations in the memory map. The least significant bits are stored
802 * in EMAC_HSL and the most significant bits in EMAC_HSH.
803 *
804 * The unicast hash enable and the multicast hash enable bits in the
805 * network configuration register enable the reception of hash matched
806 * frames. The destination address is reduced to a 6 bit index into
807 * the 64 bit hash register using the following hash function. The
808 * hash function is an exclusive or of every sixth bit of the
809 * destination address.
810 *
811 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
812 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
813 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
814 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
815 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
816 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
817 *
818 * da[0] represents the least significant bit of the first byte
819 * received, that is, the multicast/unicast indicator, and da[47]
820 * represents the most significant bit of the last byte received. If
821 * the hash index, hi[n], points to a bit that is set in the hash
822 * register then the frame will be matched according to whether the
823 * frame is multicast or unicast. A multicast match will be signalled
824 * if the multicast hash enable bit is set, da[0] is 1 and the hash
825 * index points to a bit set in the hash register. A unicast match
826 * will be signalled if the unicast hash enable bit is set, da[0] is 0
827 * and the hash index points to a bit set in the hash register. To
828 * receive all multicast frames, the hash register should be set with
829 * all ones and the multicast hash enable bit should be set in the
830 * network configuration register.
831 */
832
833static inline int hash_bit_value(int bitnr, __u8 *addr)
782{ 834{
835 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
836 return 1;
837 return 0;
838}
839
840/*
841 * Return the hash index value for the specified address.
842 */
843static int hash_get_index(__u8 *addr)
844{
845 int i, j, bitval;
846 int hash_index = 0;
847
848 for (j = 0; j < 6; j++) {
849 for (i = 0, bitval = 0; i < 8; i++)
850 bitval ^= hash_bit_value(i*6 + j, addr);
851
852 hash_index |= (bitval << j);
853 }
854
855 return hash_index;
856}
857
858/*
859 * Add multicast addresses to the internal multicast-hash table.
860 */
861static void macb_sethashtable(struct net_device *dev)
862{
863 struct dev_mc_list *curr;
864 unsigned long mc_filter[2];
865 unsigned int i, bitnr;
866 struct macb *bp = netdev_priv(dev);
867
868 mc_filter[0] = mc_filter[1] = 0;
869
870 curr = dev->mc_list;
871 for (i = 0; i < dev->mc_count; i++, curr = curr->next) {
872 if (!curr) break; /* unexpected end of list */
873
874 bitnr = hash_get_index(curr->dmi_addr);
875 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
876 }
877
878 macb_writel(bp, HRB, mc_filter[0]);
879 macb_writel(bp, HRT, mc_filter[1]);
880}
881
882/*
883 * Enable/Disable promiscuous and multicast modes.
884 */
885static void macb_set_rx_mode(struct net_device *dev)
886{
887 unsigned long cfg;
783 struct macb *bp = netdev_priv(dev); 888 struct macb *bp = netdev_priv(dev);
784 889
785 /* Set some reasonable default settings */ 890 cfg = macb_readl(bp, NCFGR);
786 macb_mdio_write(dev, bp->mii.phy_id, MII_ADVERTISE, 891
787 ADVERTISE_CSMA | ADVERTISE_ALL); 892 if (dev->flags & IFF_PROMISC)
788 macb_mdio_write(dev, bp->mii.phy_id, MII_BMCR, 893 /* Enable promiscuous mode */
789 (BMCR_SPEED100 | BMCR_ANENABLE 894 cfg |= MACB_BIT(CAF);
790 | BMCR_ANRESTART | BMCR_FULLDPLX)); 895 else if (dev->flags & (~IFF_PROMISC))
896 /* Disable promiscuous mode */
897 cfg &= ~MACB_BIT(CAF);
898
899 if (dev->flags & IFF_ALLMULTI) {
900 /* Enable all multicast mode */
901 macb_writel(bp, HRB, -1);
902 macb_writel(bp, HRT, -1);
903 cfg |= MACB_BIT(NCFGR_MTI);
904 } else if (dev->mc_count > 0) {
905 /* Enable specific multicasts */
906 macb_sethashtable(dev);
907 cfg |= MACB_BIT(NCFGR_MTI);
908 } else if (dev->flags & (~IFF_ALLMULTI)) {
909 /* Disable all multicast mode */
910 macb_writel(bp, HRB, 0);
911 macb_writel(bp, HRT, 0);
912 cfg &= ~MACB_BIT(NCFGR_MTI);
913 }
914
915 macb_writel(bp, NCFGR, cfg);
791} 916}
792 917
793static int macb_open(struct net_device *dev) 918static int macb_open(struct net_device *dev)
@@ -797,6 +922,10 @@ static int macb_open(struct net_device *dev)
797 922
798 dev_dbg(&bp->pdev->dev, "open\n"); 923 dev_dbg(&bp->pdev->dev, "open\n");
799 924
925 /* if the phy is not yet register, retry later*/
926 if (!bp->phy_dev)
927 return -EAGAIN;
928
800 if (!is_valid_ether_addr(dev->dev_addr)) 929 if (!is_valid_ether_addr(dev->dev_addr))
801 return -EADDRNOTAVAIL; 930 return -EADDRNOTAVAIL;
802 931
@@ -810,12 +939,11 @@ static int macb_open(struct net_device *dev)
810 939
811 macb_init_rings(bp); 940 macb_init_rings(bp);
812 macb_init_hw(bp); 941 macb_init_hw(bp);
813 macb_init_phy(dev);
814 942
815 macb_check_media(bp, 1, 1); 943 /* schedule a link state check */
816 netif_start_queue(dev); 944 phy_start(bp->phy_dev);
817 945
818 schedule_delayed_work(&bp->periodic_task, HZ); 946 netif_start_queue(dev);
819 947
820 return 0; 948 return 0;
821} 949}
@@ -825,10 +953,11 @@ static int macb_close(struct net_device *dev)
825 struct macb *bp = netdev_priv(dev); 953 struct macb *bp = netdev_priv(dev);
826 unsigned long flags; 954 unsigned long flags;
827 955
828 cancel_rearming_delayed_work(&bp->periodic_task);
829
830 netif_stop_queue(dev); 956 netif_stop_queue(dev);
831 957
958 if (bp->phy_dev)
959 phy_stop(bp->phy_dev);
960
832 spin_lock_irqsave(&bp->lock, flags); 961 spin_lock_irqsave(&bp->lock, flags);
833 macb_reset_hw(bp); 962 macb_reset_hw(bp);
834 netif_carrier_off(dev); 963 netif_carrier_off(dev);
@@ -845,6 +974,9 @@ static struct net_device_stats *macb_get_stats(struct net_device *dev)
845 struct net_device_stats *nstat = &bp->stats; 974 struct net_device_stats *nstat = &bp->stats;
846 struct macb_stats *hwstat = &bp->hw_stats; 975 struct macb_stats *hwstat = &bp->hw_stats;
847 976
977 /* read stats from hardware */
978 macb_update_stats(bp);
979
848 /* Convert HW stats into netdevice stats */ 980 /* Convert HW stats into netdevice stats */
849 nstat->rx_errors = (hwstat->rx_fcs_errors + 981 nstat->rx_errors = (hwstat->rx_fcs_errors +
850 hwstat->rx_align_errors + 982 hwstat->rx_align_errors +
@@ -882,18 +1014,27 @@ static struct net_device_stats *macb_get_stats(struct net_device *dev)
882static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 1014static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
883{ 1015{
884 struct macb *bp = netdev_priv(dev); 1016 struct macb *bp = netdev_priv(dev);
1017 struct phy_device *phydev = bp->phy_dev;
885 1018
886 return mii_ethtool_gset(&bp->mii, cmd); 1019 if (!phydev)
1020 return -ENODEV;
1021
1022 return phy_ethtool_gset(phydev, cmd);
887} 1023}
888 1024
889static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) 1025static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
890{ 1026{
891 struct macb *bp = netdev_priv(dev); 1027 struct macb *bp = netdev_priv(dev);
1028 struct phy_device *phydev = bp->phy_dev;
1029
1030 if (!phydev)
1031 return -ENODEV;
892 1032
893 return mii_ethtool_sset(&bp->mii, cmd); 1033 return phy_ethtool_sset(phydev, cmd);
894} 1034}
895 1035
896static void macb_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) 1036static void macb_get_drvinfo(struct net_device *dev,
1037 struct ethtool_drvinfo *info)
897{ 1038{
898 struct macb *bp = netdev_priv(dev); 1039 struct macb *bp = netdev_priv(dev);
899 1040
@@ -902,104 +1043,34 @@ static void macb_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *inf
902 strcpy(info->bus_info, bp->pdev->dev.bus_id); 1043 strcpy(info->bus_info, bp->pdev->dev.bus_id);
903} 1044}
904 1045
905static int macb_nway_reset(struct net_device *dev)
906{
907 struct macb *bp = netdev_priv(dev);
908 return mii_nway_restart(&bp->mii);
909}
910
911static struct ethtool_ops macb_ethtool_ops = { 1046static struct ethtool_ops macb_ethtool_ops = {
912 .get_settings = macb_get_settings, 1047 .get_settings = macb_get_settings,
913 .set_settings = macb_set_settings, 1048 .set_settings = macb_set_settings,
914 .get_drvinfo = macb_get_drvinfo, 1049 .get_drvinfo = macb_get_drvinfo,
915 .nway_reset = macb_nway_reset,
916 .get_link = ethtool_op_get_link, 1050 .get_link = ethtool_op_get_link,
917}; 1051};
918 1052
919static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 1053static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
920{ 1054{
921 struct macb *bp = netdev_priv(dev); 1055 struct macb *bp = netdev_priv(dev);
1056 struct phy_device *phydev = bp->phy_dev;
922 1057
923 if (!netif_running(dev)) 1058 if (!netif_running(dev))
924 return -EINVAL; 1059 return -EINVAL;
925 1060
926 return generic_mii_ioctl(&bp->mii, if_mii(rq), cmd, NULL); 1061 if (!phydev)
927} 1062 return -ENODEV;
928
929static ssize_t macb_mii_show(const struct device *_dev, char *buf,
930 unsigned long addr)
931{
932 struct net_device *dev = to_net_dev(_dev);
933 struct macb *bp = netdev_priv(dev);
934 ssize_t ret = -EINVAL;
935
936 if (netif_running(dev)) {
937 int value;
938 value = macb_mdio_read(dev, bp->mii.phy_id, addr);
939 ret = sprintf(buf, "0x%04x\n", (uint16_t)value);
940 }
941
942 return ret;
943}
944
945#define MII_ENTRY(name, addr) \
946static ssize_t show_##name(struct device *_dev, \
947 struct device_attribute *attr, \
948 char *buf) \
949{ \
950 return macb_mii_show(_dev, buf, addr); \
951} \
952static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
953
954MII_ENTRY(bmcr, MII_BMCR);
955MII_ENTRY(bmsr, MII_BMSR);
956MII_ENTRY(physid1, MII_PHYSID1);
957MII_ENTRY(physid2, MII_PHYSID2);
958MII_ENTRY(advertise, MII_ADVERTISE);
959MII_ENTRY(lpa, MII_LPA);
960MII_ENTRY(expansion, MII_EXPANSION);
961
962static struct attribute *macb_mii_attrs[] = {
963 &dev_attr_bmcr.attr,
964 &dev_attr_bmsr.attr,
965 &dev_attr_physid1.attr,
966 &dev_attr_physid2.attr,
967 &dev_attr_advertise.attr,
968 &dev_attr_lpa.attr,
969 &dev_attr_expansion.attr,
970 NULL,
971};
972
973static struct attribute_group macb_mii_group = {
974 .name = "mii",
975 .attrs = macb_mii_attrs,
976};
977
978static void macb_unregister_sysfs(struct net_device *net)
979{
980 struct device *_dev = &net->dev;
981 1063
982 sysfs_remove_group(&_dev->kobj, &macb_mii_group); 1064 return phy_mii_ioctl(phydev, if_mii(rq), cmd);
983} 1065}
984 1066
985static int macb_register_sysfs(struct net_device *net)
986{
987 struct device *_dev = &net->dev;
988 int ret;
989
990 ret = sysfs_create_group(&_dev->kobj, &macb_mii_group);
991 if (ret)
992 printk(KERN_WARNING
993 "%s: sysfs mii attribute registration failed: %d\n",
994 net->name, ret);
995 return ret;
996}
997static int __devinit macb_probe(struct platform_device *pdev) 1067static int __devinit macb_probe(struct platform_device *pdev)
998{ 1068{
999 struct eth_platform_data *pdata; 1069 struct eth_platform_data *pdata;
1000 struct resource *regs; 1070 struct resource *regs;
1001 struct net_device *dev; 1071 struct net_device *dev;
1002 struct macb *bp; 1072 struct macb *bp;
1073 struct phy_device *phydev;
1003 unsigned long pclk_hz; 1074 unsigned long pclk_hz;
1004 u32 config; 1075 u32 config;
1005 int err = -ENXIO; 1076 int err = -ENXIO;
@@ -1073,6 +1144,7 @@ static int __devinit macb_probe(struct platform_device *pdev)
1073 dev->stop = macb_close; 1144 dev->stop = macb_close;
1074 dev->hard_start_xmit = macb_start_xmit; 1145 dev->hard_start_xmit = macb_start_xmit;
1075 dev->get_stats = macb_get_stats; 1146 dev->get_stats = macb_get_stats;
1147 dev->set_multicast_list = macb_set_rx_mode;
1076 dev->do_ioctl = macb_ioctl; 1148 dev->do_ioctl = macb_ioctl;
1077 dev->poll = macb_poll; 1149 dev->poll = macb_poll;
1078 dev->weight = 64; 1150 dev->weight = 64;
@@ -1080,10 +1152,6 @@ static int __devinit macb_probe(struct platform_device *pdev)
1080 1152
1081 dev->base_addr = regs->start; 1153 dev->base_addr = regs->start;
1082 1154
1083 INIT_DELAYED_WORK(&bp->periodic_task, macb_periodic_task);
1084 mutex_init(&bp->mdio_mutex);
1085 init_completion(&bp->mdio_complete);
1086
1087 /* Set MII management clock divider */ 1155 /* Set MII management clock divider */
1088 pclk_hz = clk_get_rate(bp->pclk); 1156 pclk_hz = clk_get_rate(bp->pclk);
1089 if (pclk_hz <= 20000000) 1157 if (pclk_hz <= 20000000)
@@ -1096,20 +1164,9 @@ static int __devinit macb_probe(struct platform_device *pdev)
1096 config = MACB_BF(CLK, MACB_CLK_DIV64); 1164 config = MACB_BF(CLK, MACB_CLK_DIV64);
1097 macb_writel(bp, NCFGR, config); 1165 macb_writel(bp, NCFGR, config);
1098 1166
1099 bp->mii.dev = dev;
1100 bp->mii.mdio_read = macb_mdio_read;
1101 bp->mii.mdio_write = macb_mdio_write;
1102 bp->mii.phy_id_mask = 0x1f;
1103 bp->mii.reg_num_mask = 0x1f;
1104
1105 macb_get_hwaddr(bp); 1167 macb_get_hwaddr(bp);
1106 err = macb_phy_probe(bp);
1107 if (err) {
1108 dev_err(&pdev->dev, "Failed to detect PHY, aborting.\n");
1109 goto err_out_free_irq;
1110 }
1111
1112 pdata = pdev->dev.platform_data; 1168 pdata = pdev->dev.platform_data;
1169
1113 if (pdata && pdata->is_rmii) 1170 if (pdata && pdata->is_rmii)
1114#if defined(CONFIG_ARCH_AT91) 1171#if defined(CONFIG_ARCH_AT91)
1115 macb_writel(bp, USRIO, (MACB_BIT(RMII) | MACB_BIT(CLKEN)) ); 1172 macb_writel(bp, USRIO, (MACB_BIT(RMII) | MACB_BIT(CLKEN)) );
@@ -1131,9 +1188,11 @@ static int __devinit macb_probe(struct platform_device *pdev)
1131 goto err_out_free_irq; 1188 goto err_out_free_irq;
1132 } 1189 }
1133 1190
1134 platform_set_drvdata(pdev, dev); 1191 if (macb_mii_init(bp) != 0) {
1192 goto err_out_unregister_netdev;
1193 }
1135 1194
1136 macb_register_sysfs(dev); 1195 platform_set_drvdata(pdev, dev);
1137 1196
1138 printk(KERN_INFO "%s: Atmel MACB at 0x%08lx irq %d " 1197 printk(KERN_INFO "%s: Atmel MACB at 0x%08lx irq %d "
1139 "(%02x:%02x:%02x:%02x:%02x:%02x)\n", 1198 "(%02x:%02x:%02x:%02x:%02x:%02x)\n",
@@ -1141,8 +1200,15 @@ static int __devinit macb_probe(struct platform_device *pdev)
1141 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2], 1200 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1142 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]); 1201 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1143 1202
1203 phydev = bp->phy_dev;
1204 printk(KERN_INFO "%s: attached PHY driver [%s] "
1205 "(mii_bus:phy_addr=%s, irq=%d)\n",
1206 dev->name, phydev->drv->name, phydev->dev.bus_id, phydev->irq);
1207
1144 return 0; 1208 return 0;
1145 1209
1210err_out_unregister_netdev:
1211 unregister_netdev(dev);
1146err_out_free_irq: 1212err_out_free_irq:
1147 free_irq(dev->irq, dev); 1213 free_irq(dev->irq, dev);
1148err_out_iounmap: 1214err_out_iounmap:
@@ -1153,7 +1219,9 @@ err_out_disable_clocks:
1153 clk_put(bp->hclk); 1219 clk_put(bp->hclk);
1154#endif 1220#endif
1155 clk_disable(bp->pclk); 1221 clk_disable(bp->pclk);
1222#ifndef CONFIG_ARCH_AT91
1156err_out_put_pclk: 1223err_out_put_pclk:
1224#endif
1157 clk_put(bp->pclk); 1225 clk_put(bp->pclk);
1158err_out_free_dev: 1226err_out_free_dev:
1159 free_netdev(dev); 1227 free_netdev(dev);
@@ -1171,7 +1239,8 @@ static int __devexit macb_remove(struct platform_device *pdev)
1171 1239
1172 if (dev) { 1240 if (dev) {
1173 bp = netdev_priv(dev); 1241 bp = netdev_priv(dev);
1174 macb_unregister_sysfs(dev); 1242 mdiobus_unregister(&bp->mii_bus);
1243 kfree(bp->mii_bus.irq);
1175 unregister_netdev(dev); 1244 unregister_netdev(dev);
1176 free_irq(dev->irq, dev); 1245 free_irq(dev->irq, dev);
1177 iounmap(bp->regs); 1246 iounmap(bp->regs);