blob: 91870e92073220f7174a88051beb384098b462b8 [file] [log] [blame]
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001/*
Jamie Ilesf75ba502011-11-08 10:12:32 +00002 * Cadence MACB/GEM Ethernet Controller driver
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01003 *
4 * Copyright (C) 2004-2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Jamie Ilesc220f8c2011-03-08 20:27:08 +000011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010012#include <linux/clk.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/kernel.h>
16#include <linux/types.h>
Nicolas Ferre909a8582012-11-19 06:00:21 +000017#include <linux/circ_buf.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010018#include <linux/slab.h>
19#include <linux/init.h>
Soren Brinkmann60fe7162013-12-10 16:07:21 -080020#include <linux/io.h>
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +000021#include <linux/gpio.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000022#include <linux/interrupt.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010023#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010025#include <linux/dma-mapping.h>
Jamie Iles84e0cdb2011-03-08 20:17:06 +000026#include <linux/platform_data/macb.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010027#include <linux/platform_device.h>
frederic RODO6c36a702007-07-12 19:07:24 +020028#include <linux/phy.h>
Olof Johanssonb17471f2011-12-20 13:13:07 -080029#include <linux/of.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010030#include <linux/of_device.h>
Boris BREZILLON148cbb52013-08-22 17:57:28 +020031#include <linux/of_mdio.h>
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +010032#include <linux/of_net.h>
Jean-Christophe PLAGNIOL-VILLARD8ef29f8a2012-10-31 06:04:59 +000033#include <linux/pinctrl/consumer.h>
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010034
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010035#include "macb.h"
36
Nicolas Ferre1b447912013-06-04 21:57:11 +000037#define MACB_RX_BUFFER_SIZE 128
Nicolas Ferre1b447912013-06-04 21:57:11 +000038#define RX_BUFFER_MULTIPLE 64 /* bytes */
Havard Skinnemoen55054a12012-10-31 06:04:55 +000039#define RX_RING_SIZE 512 /* must be power of 2 */
40#define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010041
Havard Skinnemoen55054a12012-10-31 06:04:55 +000042#define TX_RING_SIZE 128 /* must be power of 2 */
43#define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010044
Nicolas Ferre909a8582012-11-19 06:00:21 +000045/* level of occupied TX descriptors under which we wake up TX process */
46#define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010047
48#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
49 | MACB_BIT(ISR_ROVR))
Nicolas Ferree86cd532012-10-31 06:04:57 +000050#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
51 | MACB_BIT(ISR_RLE) \
52 | MACB_BIT(TXERR))
53#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
54
55/*
56 * Graceful stop timeouts in us. We should allow up to
57 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
58 */
59#define MACB_HALT_TIMEOUT 1230
Haavard Skinnemoen89e57852006-11-09 14:51:17 +010060
Havard Skinnemoen55054a12012-10-31 06:04:55 +000061/* Ring buffer accessors */
62static unsigned int macb_tx_ring_wrap(unsigned int index)
63{
64 return index & (TX_RING_SIZE - 1);
65}
66
Havard Skinnemoen55054a12012-10-31 06:04:55 +000067static struct macb_dma_desc *macb_tx_desc(struct macb *bp, unsigned int index)
68{
69 return &bp->tx_ring[macb_tx_ring_wrap(index)];
70}
71
72static struct macb_tx_skb *macb_tx_skb(struct macb *bp, unsigned int index)
73{
74 return &bp->tx_skb[macb_tx_ring_wrap(index)];
75}
76
77static dma_addr_t macb_tx_dma(struct macb *bp, unsigned int index)
78{
79 dma_addr_t offset;
80
81 offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
82
83 return bp->tx_ring_dma + offset;
84}
85
86static unsigned int macb_rx_ring_wrap(unsigned int index)
87{
88 return index & (RX_RING_SIZE - 1);
89}
90
91static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
92{
93 return &bp->rx_ring[macb_rx_ring_wrap(index)];
94}
95
96static void *macb_rx_buffer(struct macb *bp, unsigned int index)
97{
Nicolas Ferre1b447912013-06-04 21:57:11 +000098 return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index);
Havard Skinnemoen55054a12012-10-31 06:04:55 +000099}
100
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000101void macb_set_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100102{
103 u32 bottom;
104 u16 top;
105
106 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000107 macb_or_gem_writel(bp, SA1B, bottom);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100108 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
Jamie Ilesf75ba502011-11-08 10:12:32 +0000109 macb_or_gem_writel(bp, SA1T, top);
Joachim Eastwood3629a6c2012-11-11 13:56:28 +0000110
111 /* Clear unused address register sets */
112 macb_or_gem_writel(bp, SA2B, 0);
113 macb_or_gem_writel(bp, SA2T, 0);
114 macb_or_gem_writel(bp, SA3B, 0);
115 macb_or_gem_writel(bp, SA3T, 0);
116 macb_or_gem_writel(bp, SA4B, 0);
117 macb_or_gem_writel(bp, SA4T, 0);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100118}
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000119EXPORT_SYMBOL_GPL(macb_set_hwaddr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100120
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000121void macb_get_hwaddr(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100122{
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000123 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100124 u32 bottom;
125 u16 top;
126 u8 addr[6];
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000127 int i;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100128
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900129 pdata = dev_get_platdata(&bp->pdev->dev);
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000130
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000131 /* Check all 4 address register for vaild address */
132 for (i = 0; i < 4; i++) {
133 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
134 top = macb_or_gem_readl(bp, SA1T + i * 8);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100135
Joachim Eastwoodd25e78a2012-11-07 08:14:51 +0000136 if (pdata && pdata->rev_eth_addr) {
137 addr[5] = bottom & 0xff;
138 addr[4] = (bottom >> 8) & 0xff;
139 addr[3] = (bottom >> 16) & 0xff;
140 addr[2] = (bottom >> 24) & 0xff;
141 addr[1] = top & 0xff;
142 addr[0] = (top & 0xff00) >> 8;
143 } else {
144 addr[0] = bottom & 0xff;
145 addr[1] = (bottom >> 8) & 0xff;
146 addr[2] = (bottom >> 16) & 0xff;
147 addr[3] = (bottom >> 24) & 0xff;
148 addr[4] = top & 0xff;
149 addr[5] = (top >> 8) & 0xff;
150 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100151
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000152 if (is_valid_ether_addr(addr)) {
153 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
154 return;
155 }
Sven Schnelled1d57412008-06-09 16:33:57 -0700156 }
Joachim Eastwood17b8bb32012-11-07 08:14:50 +0000157
158 netdev_info(bp->dev, "invalid hw address, using random\n");
159 eth_hw_addr_random(bp->dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100160}
Joachim Eastwood314bccc2012-11-07 08:14:52 +0000161EXPORT_SYMBOL_GPL(macb_get_hwaddr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100162
frederic RODO6c36a702007-07-12 19:07:24 +0200163static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100164{
frederic RODO6c36a702007-07-12 19:07:24 +0200165 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100166 int value;
167
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100168 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
169 | MACB_BF(RW, MACB_MAN_READ)
frederic RODO6c36a702007-07-12 19:07:24 +0200170 | MACB_BF(PHYA, mii_id)
171 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100172 | MACB_BF(CODE, MACB_MAN_CODE)));
173
frederic RODO6c36a702007-07-12 19:07:24 +0200174 /* wait for end of transfer */
175 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
176 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100177
178 value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100179
180 return value;
181}
182
frederic RODO6c36a702007-07-12 19:07:24 +0200183static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
184 u16 value)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100185{
frederic RODO6c36a702007-07-12 19:07:24 +0200186 struct macb *bp = bus->priv;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100187
188 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
189 | MACB_BF(RW, MACB_MAN_WRITE)
frederic RODO6c36a702007-07-12 19:07:24 +0200190 | MACB_BF(PHYA, mii_id)
191 | MACB_BF(REGA, regnum)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100192 | MACB_BF(CODE, MACB_MAN_CODE)
frederic RODO6c36a702007-07-12 19:07:24 +0200193 | MACB_BF(DATA, value)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100194
frederic RODO6c36a702007-07-12 19:07:24 +0200195 /* wait for end of transfer */
196 while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
197 cpu_relax();
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100198
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100199 return 0;
200}
201
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800202/**
203 * macb_set_tx_clk() - Set a clock to a new frequency
204 * @clk Pointer to the clock to change
205 * @rate New frequency in Hz
206 * @dev Pointer to the struct net_device
207 */
208static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
209{
210 long ferr, rate, rate_rounded;
211
212 switch (speed) {
213 case SPEED_10:
214 rate = 2500000;
215 break;
216 case SPEED_100:
217 rate = 25000000;
218 break;
219 case SPEED_1000:
220 rate = 125000000;
221 break;
222 default:
Soren Brinkmann9319e472013-12-10 20:57:57 -0800223 return;
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800224 }
225
226 rate_rounded = clk_round_rate(clk, rate);
227 if (rate_rounded < 0)
228 return;
229
230 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
231 * is not satisfied.
232 */
233 ferr = abs(rate_rounded - rate);
234 ferr = DIV_ROUND_UP(ferr, rate / 100000);
235 if (ferr > 5)
236 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
237 rate);
238
239 if (clk_set_rate(clk, rate_rounded))
240 netdev_err(dev, "adjusting tx_clk failed.\n");
241}
242
frederic RODO6c36a702007-07-12 19:07:24 +0200243static void macb_handle_link_change(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100244{
frederic RODO6c36a702007-07-12 19:07:24 +0200245 struct macb *bp = netdev_priv(dev);
246 struct phy_device *phydev = bp->phy_dev;
247 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100248
frederic RODO6c36a702007-07-12 19:07:24 +0200249 int status_change = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100250
frederic RODO6c36a702007-07-12 19:07:24 +0200251 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100252
frederic RODO6c36a702007-07-12 19:07:24 +0200253 if (phydev->link) {
254 if ((bp->speed != phydev->speed) ||
255 (bp->duplex != phydev->duplex)) {
256 u32 reg;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100257
frederic RODO6c36a702007-07-12 19:07:24 +0200258 reg = macb_readl(bp, NCFGR);
259 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
Patrice Vilchez140b7552012-10-31 06:04:50 +0000260 if (macb_is_gem(bp))
261 reg &= ~GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200262
263 if (phydev->duplex)
264 reg |= MACB_BIT(FD);
Atsushi Nemoto179956f2008-02-21 22:50:54 +0900265 if (phydev->speed == SPEED_100)
frederic RODO6c36a702007-07-12 19:07:24 +0200266 reg |= MACB_BIT(SPD);
Nicolas Ferree1755872014-07-24 13:50:58 +0200267 if (phydev->speed == SPEED_1000 &&
268 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000269 reg |= GEM_BIT(GBE);
frederic RODO6c36a702007-07-12 19:07:24 +0200270
Patrice Vilchez140b7552012-10-31 06:04:50 +0000271 macb_or_gem_writel(bp, NCFGR, reg);
frederic RODO6c36a702007-07-12 19:07:24 +0200272
273 bp->speed = phydev->speed;
274 bp->duplex = phydev->duplex;
275 status_change = 1;
276 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100277 }
278
frederic RODO6c36a702007-07-12 19:07:24 +0200279 if (phydev->link != bp->link) {
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700280 if (!phydev->link) {
frederic RODO6c36a702007-07-12 19:07:24 +0200281 bp->speed = 0;
282 bp->duplex = -1;
283 }
284 bp->link = phydev->link;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100285
frederic RODO6c36a702007-07-12 19:07:24 +0200286 status_change = 1;
287 }
288
289 spin_unlock_irqrestore(&bp->lock, flags);
290
Soren Brinkmanne1824df2013-12-10 16:07:23 -0800291 if (!IS_ERR(bp->tx_clk))
292 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
293
frederic RODO6c36a702007-07-12 19:07:24 +0200294 if (status_change) {
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000295 if (phydev->link) {
296 netif_carrier_on(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000297 netdev_info(dev, "link up (%d/%s)\n",
298 phydev->speed,
299 phydev->duplex == DUPLEX_FULL ?
300 "Full" : "Half");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000301 } else {
302 netif_carrier_off(dev);
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000303 netdev_info(dev, "link down\n");
Nicolas Ferre03fc4722012-07-03 23:14:13 +0000304 }
frederic RODO6c36a702007-07-12 19:07:24 +0200305 }
306}
307
308/* based on au1000_eth. c*/
309static int macb_mii_probe(struct net_device *dev)
310{
311 struct macb *bp = netdev_priv(dev);
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000312 struct macb_platform_data *pdata;
Jiri Pirko7455a762010-02-08 05:12:08 +0000313 struct phy_device *phydev;
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000314 int phy_irq;
Jiri Pirko7455a762010-02-08 05:12:08 +0000315 int ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200316
Jiri Pirko7455a762010-02-08 05:12:08 +0000317 phydev = phy_find_first(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200318 if (!phydev) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000319 netdev_err(dev, "no PHY found\n");
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200320 return -ENXIO;
frederic RODO6c36a702007-07-12 19:07:24 +0200321 }
322
Joachim Eastwood2dbfdbb92012-11-11 13:56:27 +0000323 pdata = dev_get_platdata(&bp->pdev->dev);
324 if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
325 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
326 if (!ret) {
327 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
328 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
329 }
330 }
frederic RODO6c36a702007-07-12 19:07:24 +0200331
332 /* attach the mac to the phy */
Florian Fainellif9a8f832013-01-14 00:52:52 +0000333 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +0100334 bp->phy_interface);
Jiri Pirko7455a762010-02-08 05:12:08 +0000335 if (ret) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000336 netdev_err(dev, "Could not attach to PHY\n");
Jiri Pirko7455a762010-02-08 05:12:08 +0000337 return ret;
frederic RODO6c36a702007-07-12 19:07:24 +0200338 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100339
frederic RODO6c36a702007-07-12 19:07:24 +0200340 /* mask with MAC supported features */
Nicolas Ferree1755872014-07-24 13:50:58 +0200341 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
Patrice Vilchez140b7552012-10-31 06:04:50 +0000342 phydev->supported &= PHY_GBIT_FEATURES;
343 else
344 phydev->supported &= PHY_BASIC_FEATURES;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100345
frederic RODO6c36a702007-07-12 19:07:24 +0200346 phydev->advertising = phydev->supported;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100347
frederic RODO6c36a702007-07-12 19:07:24 +0200348 bp->link = 0;
349 bp->speed = 0;
350 bp->duplex = -1;
351 bp->phy_dev = phydev;
352
353 return 0;
354}
355
Joachim Eastwood0005f542012-10-18 11:01:12 +0000356int macb_mii_init(struct macb *bp)
frederic RODO6c36a702007-07-12 19:07:24 +0200357{
Jamie Iles84e0cdb2011-03-08 20:17:06 +0000358 struct macb_platform_data *pdata;
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200359 struct device_node *np;
frederic RODO6c36a702007-07-12 19:07:24 +0200360 int err = -ENXIO, i;
361
Uwe Kleine-Koenig3dbda772009-07-23 08:31:31 +0200362 /* Enable management port */
frederic RODO6c36a702007-07-12 19:07:24 +0200363 macb_writel(bp, NCR, MACB_BIT(MPE));
364
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700365 bp->mii_bus = mdiobus_alloc();
366 if (bp->mii_bus == NULL) {
frederic RODO6c36a702007-07-12 19:07:24 +0200367 err = -ENOMEM;
368 goto err_out;
369 }
370
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700371 bp->mii_bus->name = "MACB_mii_bus";
372 bp->mii_bus->read = &macb_mdio_read;
373 bp->mii_bus->write = &macb_mdio_write;
Florian Fainelli98d5e572012-01-09 23:59:11 +0000374 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
375 bp->pdev->name, bp->pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700376 bp->mii_bus->priv = bp;
377 bp->mii_bus->parent = &bp->dev->dev;
Jingoo Hanc607a0d2013-08-30 14:12:21 +0900378 pdata = dev_get_platdata(&bp->pdev->dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700379
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700380 bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
381 if (!bp->mii_bus->irq) {
382 err = -ENOMEM;
383 goto err_out_free_mdiobus;
384 }
385
Jamie Iles91523942011-02-28 04:05:25 +0000386 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200387
Boris BREZILLON148cbb52013-08-22 17:57:28 +0200388 np = bp->pdev->dev.of_node;
389 if (np) {
390 /* try dt phy registration */
391 err = of_mdiobus_register(bp->mii_bus, np);
392
393 /* fallback to standard phy registration if no phy were
394 found during dt phy registration */
395 if (!err && !phy_find_first(bp->mii_bus)) {
396 for (i = 0; i < PHY_MAX_ADDR; i++) {
397 struct phy_device *phydev;
398
399 phydev = mdiobus_scan(bp->mii_bus, i);
400 if (IS_ERR(phydev)) {
401 err = PTR_ERR(phydev);
402 break;
403 }
404 }
405
406 if (err)
407 goto err_out_unregister_bus;
408 }
409 } else {
410 for (i = 0; i < PHY_MAX_ADDR; i++)
411 bp->mii_bus->irq[i] = PHY_POLL;
412
413 if (pdata)
414 bp->mii_bus->phy_mask = pdata->phy_mask;
415
416 err = mdiobus_register(bp->mii_bus);
417 }
418
419 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200420 goto err_out_free_mdio_irq;
421
Boris BREZILLON7daa78e2013-08-27 14:36:14 +0200422 err = macb_mii_probe(bp->dev);
423 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +0200424 goto err_out_unregister_bus;
frederic RODO6c36a702007-07-12 19:07:24 +0200425
426 return 0;
427
428err_out_unregister_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700429 mdiobus_unregister(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200430err_out_free_mdio_irq:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700431 kfree(bp->mii_bus->irq);
432err_out_free_mdiobus:
433 mdiobus_free(bp->mii_bus);
frederic RODO6c36a702007-07-12 19:07:24 +0200434err_out:
435 return err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100436}
Joachim Eastwood0005f542012-10-18 11:01:12 +0000437EXPORT_SYMBOL_GPL(macb_mii_init);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100438
439static void macb_update_stats(struct macb *bp)
440{
441 u32 __iomem *reg = bp->regs + MACB_PFR;
Jamie Ilesa494ed82011-03-09 16:26:35 +0000442 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
443 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100444
445 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
446
447 for(; p < end; p++, reg++)
Haavard Skinnemoen0f0d84e2006-12-08 14:38:30 +0100448 *p += __raw_readl(reg);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100449}
450
Nicolas Ferree86cd532012-10-31 06:04:57 +0000451static int macb_halt_tx(struct macb *bp)
452{
453 unsigned long halt_time, timeout;
454 u32 status;
455
456 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
457
458 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
459 do {
460 halt_time = jiffies;
461 status = macb_readl(bp, TSR);
462 if (!(status & MACB_BIT(TGO)))
463 return 0;
464
465 usleep_range(10, 250);
466 } while (time_before(halt_time, timeout));
467
468 return -ETIMEDOUT;
469}
470
471static void macb_tx_error_task(struct work_struct *work)
472{
473 struct macb *bp = container_of(work, struct macb, tx_error_task);
474 struct macb_tx_skb *tx_skb;
475 struct sk_buff *skb;
476 unsigned int tail;
477
478 netdev_vdbg(bp->dev, "macb_tx_error_task: t = %u, h = %u\n",
479 bp->tx_tail, bp->tx_head);
480
481 /* Make sure nobody is trying to queue up new packets */
482 netif_stop_queue(bp->dev);
483
484 /*
485 * Stop transmission now
486 * (in case we have just queued new packets)
487 */
488 if (macb_halt_tx(bp))
489 /* Just complain for now, reinitializing TX path can be good */
490 netdev_err(bp->dev, "BUG: halt tx timed out\n");
491
492 /* No need for the lock here as nobody will interrupt us anymore */
493
494 /*
495 * Treat frames in TX queue including the ones that caused the error.
496 * Free transmit buffers in upper layer.
497 */
498 for (tail = bp->tx_tail; tail != bp->tx_head; tail++) {
499 struct macb_dma_desc *desc;
500 u32 ctrl;
501
502 desc = macb_tx_desc(bp, tail);
503 ctrl = desc->ctrl;
504 tx_skb = macb_tx_skb(bp, tail);
505 skb = tx_skb->skb;
506
507 if (ctrl & MACB_BIT(TX_USED)) {
508 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
509 macb_tx_ring_wrap(tail), skb->data);
510 bp->stats.tx_packets++;
511 bp->stats.tx_bytes += skb->len;
512 } else {
513 /*
514 * "Buffers exhausted mid-frame" errors may only happen
515 * if the driver is buggy, so complain loudly about those.
516 * Statistics are updated by hardware.
517 */
518 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
519 netdev_err(bp->dev,
520 "BUG: TX buffers exhausted mid-frame\n");
521
522 desc->ctrl = ctrl | MACB_BIT(TX_USED);
523 }
524
525 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
526 DMA_TO_DEVICE);
527 tx_skb->skb = NULL;
528 dev_kfree_skb(skb);
529 }
530
531 /* Make descriptor updates visible to hardware */
532 wmb();
533
534 /* Reinitialize the TX desc queue */
535 macb_writel(bp, TBQP, bp->tx_ring_dma);
536 /* Make TX ring reflect state of hardware */
537 bp->tx_head = bp->tx_tail = 0;
538
539 /* Now we are ready to start transmission again */
540 netif_wake_queue(bp->dev);
541
542 /* Housework before enabling TX IRQ */
543 macb_writel(bp, TSR, macb_readl(bp, TSR));
544 macb_writel(bp, IER, MACB_TX_INT_FLAGS);
545}
546
547static void macb_tx_interrupt(struct macb *bp)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100548{
549 unsigned int tail;
550 unsigned int head;
551 u32 status;
552
553 status = macb_readl(bp, TSR);
554 macb_writel(bp, TSR, status);
555
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000556 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
557 macb_writel(bp, ISR, MACB_BIT(TCOMP));
Steffen Trumtrar749a2b62013-03-27 23:07:05 +0000558
Nicolas Ferree86cd532012-10-31 06:04:57 +0000559 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
560 (unsigned long)status);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100561
562 head = bp->tx_head;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000563 for (tail = bp->tx_tail; tail != head; tail++) {
564 struct macb_tx_skb *tx_skb;
565 struct sk_buff *skb;
566 struct macb_dma_desc *desc;
567 u32 ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100568
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000569 desc = macb_tx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100570
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000571 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100572 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000573
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000574 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100575
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000576 if (!(ctrl & MACB_BIT(TX_USED)))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100577 break;
578
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000579 tx_skb = macb_tx_skb(bp, tail);
580 skb = tx_skb->skb;
581
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000582 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000583 macb_tx_ring_wrap(tail), skb->data);
584 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100585 DMA_TO_DEVICE);
586 bp->stats.tx_packets++;
587 bp->stats.tx_bytes += skb->len;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000588 tx_skb->skb = NULL;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100589 dev_kfree_skb_irq(skb);
590 }
591
592 bp->tx_tail = tail;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000593 if (netif_queue_stopped(bp->dev)
Nicolas Ferre909a8582012-11-19 06:00:21 +0000594 && CIRC_CNT(bp->tx_head, bp->tx_tail,
595 TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100596 netif_wake_queue(bp->dev);
597}
598
Nicolas Ferre4df95132013-06-04 21:57:12 +0000599static void gem_rx_refill(struct macb *bp)
600{
601 unsigned int entry;
602 struct sk_buff *skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000603 dma_addr_t paddr;
604
605 while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
Nicolas Ferre4df95132013-06-04 21:57:12 +0000606 entry = macb_rx_ring_wrap(bp->rx_prepared_head);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000607
608 /* Make hw descriptor updates visible to CPU */
609 rmb();
610
Nicolas Ferre4df95132013-06-04 21:57:12 +0000611 bp->rx_prepared_head++;
612
Nicolas Ferre4df95132013-06-04 21:57:12 +0000613 if (bp->rx_skbuff[entry] == NULL) {
614 /* allocate sk_buff for this free entry in ring */
615 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
616 if (unlikely(skb == NULL)) {
617 netdev_err(bp->dev,
618 "Unable to allocate sk_buff\n");
619 break;
620 }
Nicolas Ferre4df95132013-06-04 21:57:12 +0000621
622 /* now fill corresponding descriptor entry */
623 paddr = dma_map_single(&bp->pdev->dev, skb->data,
624 bp->rx_buffer_size, DMA_FROM_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -0800625 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
626 dev_kfree_skb(skb);
627 break;
628 }
629
630 bp->rx_skbuff[entry] = skb;
Nicolas Ferre4df95132013-06-04 21:57:12 +0000631
632 if (entry == RX_RING_SIZE - 1)
633 paddr |= MACB_BIT(RX_WRAP);
634 bp->rx_ring[entry].addr = paddr;
635 bp->rx_ring[entry].ctrl = 0;
636
637 /* properly align Ethernet header */
638 skb_reserve(skb, NET_IP_ALIGN);
639 }
640 }
641
642 /* Make descriptor updates visible to hardware */
643 wmb();
644
645 netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
646 bp->rx_prepared_head, bp->rx_tail);
647}
648
649/* Mark DMA descriptors from begin up to and not including end as unused */
650static void discard_partial_frame(struct macb *bp, unsigned int begin,
651 unsigned int end)
652{
653 unsigned int frag;
654
655 for (frag = begin; frag != end; frag++) {
656 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
657 desc->addr &= ~MACB_BIT(RX_USED);
658 }
659
660 /* Make descriptor updates visible to hardware */
661 wmb();
662
663 /*
664 * When this happens, the hardware stats registers for
665 * whatever caused this is updated, so we don't have to record
666 * anything.
667 */
668}
669
670static int gem_rx(struct macb *bp, int budget)
671{
672 unsigned int len;
673 unsigned int entry;
674 struct sk_buff *skb;
675 struct macb_dma_desc *desc;
676 int count = 0;
677
678 while (count < budget) {
679 u32 addr, ctrl;
680
681 entry = macb_rx_ring_wrap(bp->rx_tail);
682 desc = &bp->rx_ring[entry];
683
684 /* Make hw descriptor updates visible to CPU */
685 rmb();
686
687 addr = desc->addr;
688 ctrl = desc->ctrl;
689
690 if (!(addr & MACB_BIT(RX_USED)))
691 break;
692
Nicolas Ferre4df95132013-06-04 21:57:12 +0000693 bp->rx_tail++;
694 count++;
695
696 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
697 netdev_err(bp->dev,
698 "not whole frame pointed by descriptor\n");
699 bp->stats.rx_dropped++;
700 break;
701 }
702 skb = bp->rx_skbuff[entry];
703 if (unlikely(!skb)) {
704 netdev_err(bp->dev,
705 "inconsistent Rx descriptor chain\n");
706 bp->stats.rx_dropped++;
707 break;
708 }
709 /* now everything is ready for receiving packet */
710 bp->rx_skbuff[entry] = NULL;
711 len = MACB_BFEXT(RX_FRMLEN, ctrl);
712
713 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
714
715 skb_put(skb, len);
716 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
717 dma_unmap_single(&bp->pdev->dev, addr,
Soren Brinkmann48330e082014-03-04 08:46:40 -0800718 bp->rx_buffer_size, DMA_FROM_DEVICE);
Nicolas Ferre4df95132013-06-04 21:57:12 +0000719
720 skb->protocol = eth_type_trans(skb, bp->dev);
721 skb_checksum_none_assert(skb);
722
723 bp->stats.rx_packets++;
724 bp->stats.rx_bytes += skb->len;
725
726#if defined(DEBUG) && defined(VERBOSE_DEBUG)
727 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
728 skb->len, skb->csum);
729 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
730 skb->mac_header, 16, true);
731 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
732 skb->data, 32, true);
733#endif
734
735 netif_receive_skb(skb);
736 }
737
738 gem_rx_refill(bp);
739
740 return count;
741}
742
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100743static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
744 unsigned int last_frag)
745{
746 unsigned int len;
747 unsigned int frag;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000748 unsigned int offset;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100749 struct sk_buff *skb;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000750 struct macb_dma_desc *desc;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100751
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000752 desc = macb_rx_desc(bp, last_frag);
753 len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100754
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000755 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000756 macb_rx_ring_wrap(first_frag),
757 macb_rx_ring_wrap(last_frag), len);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100758
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000759 /*
760 * The ethernet header starts NET_IP_ALIGN bytes into the
761 * first buffer. Since the header is 14 bytes, this makes the
762 * payload word-aligned.
763 *
764 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
765 * the two padding bytes into the skb so that we avoid hitting
766 * the slowpath in memcpy(), and pull them off afterwards.
767 */
768 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100769 if (!skb) {
770 bp->stats.rx_dropped++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000771 for (frag = first_frag; ; frag++) {
772 desc = macb_rx_desc(bp, frag);
773 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100774 if (frag == last_frag)
775 break;
776 }
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000777
778 /* Make descriptor updates visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100779 wmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000780
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100781 return 1;
782 }
783
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000784 offset = 0;
785 len += NET_IP_ALIGN;
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700786 skb_checksum_none_assert(skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100787 skb_put(skb, len);
788
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000789 for (frag = first_frag; ; frag++) {
Nicolas Ferre1b447912013-06-04 21:57:11 +0000790 unsigned int frag_len = bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100791
792 if (offset + frag_len > len) {
793 BUG_ON(frag != last_frag);
794 frag_len = len - offset;
795 }
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300796 skb_copy_to_linear_data_offset(skb, offset,
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000797 macb_rx_buffer(bp, frag), frag_len);
Nicolas Ferre1b447912013-06-04 21:57:11 +0000798 offset += bp->rx_buffer_size;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000799 desc = macb_rx_desc(bp, frag);
800 desc->addr &= ~MACB_BIT(RX_USED);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100801
802 if (frag == last_frag)
803 break;
804 }
805
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000806 /* Make descriptor updates visible to hardware */
807 wmb();
808
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000809 __skb_pull(skb, NET_IP_ALIGN);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100810 skb->protocol = eth_type_trans(skb, bp->dev);
811
812 bp->stats.rx_packets++;
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +0000813 bp->stats.rx_bytes += skb->len;
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000814 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000815 skb->len, skb->csum);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100816 netif_receive_skb(skb);
817
818 return 0;
819}
820
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100821static int macb_rx(struct macb *bp, int budget)
822{
823 int received = 0;
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000824 unsigned int tail;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100825 int first_frag = -1;
826
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000827 for (tail = bp->rx_tail; budget > 0; tail++) {
828 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100829 u32 addr, ctrl;
830
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000831 /* Make hw descriptor updates visible to CPU */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100832 rmb();
Havard Skinnemoen03dbe052012-10-31 06:04:51 +0000833
Havard Skinnemoen55054a12012-10-31 06:04:55 +0000834 addr = desc->addr;
835 ctrl = desc->ctrl;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100836
837 if (!(addr & MACB_BIT(RX_USED)))
838 break;
839
840 if (ctrl & MACB_BIT(RX_SOF)) {
841 if (first_frag != -1)
842 discard_partial_frame(bp, first_frag, tail);
843 first_frag = tail;
844 }
845
846 if (ctrl & MACB_BIT(RX_EOF)) {
847 int dropped;
848 BUG_ON(first_frag == -1);
849
850 dropped = macb_rx_frame(bp, first_frag, tail);
851 first_frag = -1;
852 if (!dropped) {
853 received++;
854 budget--;
855 }
856 }
857 }
858
859 if (first_frag != -1)
860 bp->rx_tail = first_frag;
861 else
862 bp->rx_tail = tail;
863
864 return received;
865}
866
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700867static int macb_poll(struct napi_struct *napi, int budget)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100868{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700869 struct macb *bp = container_of(napi, struct macb, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700870 int work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100871 u32 status;
872
873 status = macb_readl(bp, RSR);
874 macb_writel(bp, RSR, status);
875
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700876 work_done = 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100877
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000878 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000879 (unsigned long)status, budget);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100880
Nicolas Ferre4df95132013-06-04 21:57:12 +0000881 work_done = bp->macbgem_ops.mog_rx(bp, budget);
Joshua Hokeb3363692010-10-25 01:44:22 +0000882 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800883 napi_complete(napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100884
Nicolas Ferre8770e912013-02-12 11:08:48 +0100885 /* Packets received while interrupts were disabled */
886 status = macb_readl(bp, RSR);
Soren Brinkmann504ad982014-05-04 15:43:01 -0700887 if (status) {
Soren Brinkmann02f7a342014-05-04 15:43:00 -0700888 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
889 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Nicolas Ferre8770e912013-02-12 11:08:48 +0100890 napi_reschedule(napi);
Soren Brinkmann02f7a342014-05-04 15:43:00 -0700891 } else {
892 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
893 }
Joshua Hokeb3363692010-10-25 01:44:22 +0000894 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100895
896 /* TODO: Handle errors */
897
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700898 return work_done;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100899}
900
901static irqreturn_t macb_interrupt(int irq, void *dev_id)
902{
903 struct net_device *dev = dev_id;
904 struct macb *bp = netdev_priv(dev);
905 u32 status;
906
907 status = macb_readl(bp, ISR);
908
909 if (unlikely(!status))
910 return IRQ_NONE;
911
912 spin_lock(&bp->lock);
913
914 while (status) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100915 /* close possible race with dev_close */
916 if (unlikely(!netif_running(dev))) {
Joachim Eastwood95ebcea2012-10-22 08:45:31 +0000917 macb_writel(bp, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100918 break;
919 }
920
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000921 netdev_vdbg(bp->dev, "isr = 0x%08lx\n", (unsigned long)status);
922
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100923 if (status & MACB_RX_INT_FLAGS) {
Joshua Hokeb3363692010-10-25 01:44:22 +0000924 /*
925 * There's no point taking any more interrupts
926 * until we have processed the buffers. The
927 * scheduling call may fail if the poll routine
928 * is already scheduled, so disable interrupts
929 * now.
930 */
931 macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
Nicolas Ferre581df9e2013-05-14 03:00:16 +0000932 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
933 macb_writel(bp, ISR, MACB_BIT(RCOMP));
Joshua Hokeb3363692010-10-25 01:44:22 +0000934
Ben Hutchings288379f2009-01-19 16:43:59 -0800935 if (napi_schedule_prep(&bp->napi)) {
Havard Skinnemoena268adb2012-10-31 06:04:52 +0000936 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
Ben Hutchings288379f2009-01-19 16:43:59 -0800937 __napi_schedule(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100938 }
939 }
940
Nicolas Ferree86cd532012-10-31 06:04:57 +0000941 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
942 macb_writel(bp, IDR, MACB_TX_INT_FLAGS);
943 schedule_work(&bp->tx_error_task);
Soren Brinkmann6a027b72014-05-04 15:42:59 -0700944
945 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
946 macb_writel(bp, ISR, MACB_TX_ERR_FLAGS);
947
Nicolas Ferree86cd532012-10-31 06:04:57 +0000948 break;
949 }
950
951 if (status & MACB_BIT(TCOMP))
952 macb_tx_interrupt(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100953
954 /*
955 * Link change detection isn't possible with RMII, so we'll
956 * add that if/when we get our hands on a full-blown MII PHY.
957 */
958
Alexander Steinb19f7f72011-04-13 05:03:24 +0000959 if (status & MACB_BIT(ISR_ROVR)) {
960 /* We missed at least one packet */
Jamie Ilesf75ba502011-11-08 10:12:32 +0000961 if (macb_is_gem(bp))
962 bp->hw_stats.gem.rx_overruns++;
963 else
964 bp->hw_stats.macb.rx_overruns++;
Soren Brinkmann6a027b72014-05-04 15:42:59 -0700965
966 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
967 macb_writel(bp, ISR, MACB_BIT(ISR_ROVR));
Alexander Steinb19f7f72011-04-13 05:03:24 +0000968 }
969
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100970 if (status & MACB_BIT(HRESP)) {
971 /*
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000972 * TODO: Reset the hardware, and maybe move the
973 * netdev_err to a lower-priority context as well
974 * (work queue?)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100975 */
Jamie Ilesc220f8c2011-03-08 20:27:08 +0000976 netdev_err(dev, "DMA bus error: HRESP not OK\n");
Soren Brinkmann6a027b72014-05-04 15:42:59 -0700977
978 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
979 macb_writel(bp, ISR, MACB_BIT(HRESP));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +0100980 }
981
982 status = macb_readl(bp, ISR);
983 }
984
985 spin_unlock(&bp->lock);
986
987 return IRQ_HANDLED;
988}
989
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -0700990#ifdef CONFIG_NET_POLL_CONTROLLER
991/*
992 * Polling receive - used by netconsole and other diagnostic tools
993 * to allow network i/o with interrupts disabled.
994 */
995static void macb_poll_controller(struct net_device *dev)
996{
997 unsigned long flags;
998
999 local_irq_save(flags);
1000 macb_interrupt(dev->irq, dev);
1001 local_irq_restore(flags);
1002}
1003#endif
1004
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001005static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1006{
1007 struct macb *bp = netdev_priv(dev);
1008 dma_addr_t mapping;
1009 unsigned int len, entry;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001010 struct macb_dma_desc *desc;
1011 struct macb_tx_skb *tx_skb;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001012 u32 ctrl;
Dongdong Deng48719532009-08-23 19:49:07 -07001013 unsigned long flags;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001014
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001015#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1016 netdev_vdbg(bp->dev,
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001017 "start_xmit: len %u head %p data %p tail %p end %p\n",
1018 skb->len, skb->head, skb->data,
1019 skb_tail_pointer(skb), skb_end_pointer(skb));
1020 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1021 skb->data, 16, true);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001022#endif
1023
1024 len = skb->len;
Dongdong Deng48719532009-08-23 19:49:07 -07001025 spin_lock_irqsave(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001026
1027 /* This is a hard error, log it. */
Nicolas Ferre909a8582012-11-19 06:00:21 +00001028 if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1) {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001029 netif_stop_queue(dev);
Dongdong Deng48719532009-08-23 19:49:07 -07001030 spin_unlock_irqrestore(&bp->lock, flags);
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001031 netdev_err(bp->dev, "BUG! Tx Ring full when queue awake!\n");
1032 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
1033 bp->tx_head, bp->tx_tail);
Patrick McHardy5b548142009-06-12 06:22:29 +00001034 return NETDEV_TX_BUSY;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001035 }
1036
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001037 entry = macb_tx_ring_wrap(bp->tx_head);
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001038 netdev_vdbg(bp->dev, "Allocated ring entry %u\n", entry);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001039 mapping = dma_map_single(&bp->pdev->dev, skb->data,
1040 len, DMA_TO_DEVICE);
Soren Brinkmann92030902014-03-04 08:46:39 -08001041 if (dma_mapping_error(&bp->pdev->dev, mapping)) {
Eric W. Biedermanc88b5b62014-03-15 16:08:27 -07001042 dev_kfree_skb_any(skb);
Soren Brinkmann92030902014-03-04 08:46:39 -08001043 goto unlock;
1044 }
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001045
Soren Brinkmann92030902014-03-04 08:46:39 -08001046 bp->tx_head++;
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001047 tx_skb = &bp->tx_skb[entry];
1048 tx_skb->skb = skb;
1049 tx_skb->mapping = mapping;
Havard Skinnemoena268adb2012-10-31 06:04:52 +00001050 netdev_vdbg(bp->dev, "Mapped skb data %p to DMA addr %08lx\n",
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001051 skb->data, (unsigned long)mapping);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001052
1053 ctrl = MACB_BF(TX_FRMLEN, len);
1054 ctrl |= MACB_BIT(TX_LAST);
1055 if (entry == (TX_RING_SIZE - 1))
1056 ctrl |= MACB_BIT(TX_WRAP);
1057
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001058 desc = &bp->tx_ring[entry];
1059 desc->addr = mapping;
1060 desc->ctrl = ctrl;
Havard Skinnemoen03dbe052012-10-31 06:04:51 +00001061
1062 /* Make newly initialized descriptor visible to hardware */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001063 wmb();
1064
Richard Cochrane0720922011-06-19 21:51:28 +00001065 skb_tx_timestamp(skb);
1066
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001067 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1068
Nicolas Ferre909a8582012-11-19 06:00:21 +00001069 if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001070 netif_stop_queue(dev);
1071
Soren Brinkmann92030902014-03-04 08:46:39 -08001072unlock:
Dongdong Deng48719532009-08-23 19:49:07 -07001073 spin_unlock_irqrestore(&bp->lock, flags);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001074
Patrick McHardy6ed10652009-06-23 06:03:08 +00001075 return NETDEV_TX_OK;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001076}
1077
Nicolas Ferre4df95132013-06-04 21:57:12 +00001078static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
Nicolas Ferre1b447912013-06-04 21:57:11 +00001079{
1080 if (!macb_is_gem(bp)) {
1081 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1082 } else {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001083 bp->rx_buffer_size = size;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001084
Nicolas Ferre1b447912013-06-04 21:57:11 +00001085 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
Nicolas Ferre4df95132013-06-04 21:57:12 +00001086 netdev_dbg(bp->dev,
1087 "RX buffer must be multiple of %d bytes, expanding\n",
Nicolas Ferre1b447912013-06-04 21:57:11 +00001088 RX_BUFFER_MULTIPLE);
1089 bp->rx_buffer_size =
Nicolas Ferre4df95132013-06-04 21:57:12 +00001090 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001091 }
Nicolas Ferre1b447912013-06-04 21:57:11 +00001092 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001093
1094 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1095 bp->dev->mtu, bp->rx_buffer_size);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001096}
1097
Nicolas Ferre4df95132013-06-04 21:57:12 +00001098static void gem_free_rx_buffers(struct macb *bp)
1099{
1100 struct sk_buff *skb;
1101 struct macb_dma_desc *desc;
1102 dma_addr_t addr;
1103 int i;
1104
1105 if (!bp->rx_skbuff)
1106 return;
1107
1108 for (i = 0; i < RX_RING_SIZE; i++) {
1109 skb = bp->rx_skbuff[i];
1110
1111 if (skb == NULL)
1112 continue;
1113
1114 desc = &bp->rx_ring[i];
1115 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
Soren Brinkmannccd6d0a2014-05-04 15:42:58 -07001116 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
Nicolas Ferre4df95132013-06-04 21:57:12 +00001117 DMA_FROM_DEVICE);
1118 dev_kfree_skb_any(skb);
1119 skb = NULL;
1120 }
1121
1122 kfree(bp->rx_skbuff);
1123 bp->rx_skbuff = NULL;
1124}
1125
1126static void macb_free_rx_buffers(struct macb *bp)
1127{
1128 if (bp->rx_buffers) {
1129 dma_free_coherent(&bp->pdev->dev,
1130 RX_RING_SIZE * bp->rx_buffer_size,
1131 bp->rx_buffers, bp->rx_buffers_dma);
1132 bp->rx_buffers = NULL;
1133 }
1134}
Nicolas Ferre1b447912013-06-04 21:57:11 +00001135
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001136static void macb_free_consistent(struct macb *bp)
1137{
1138 if (bp->tx_skb) {
1139 kfree(bp->tx_skb);
1140 bp->tx_skb = NULL;
1141 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001142 bp->macbgem_ops.mog_free_rx_buffers(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001143 if (bp->rx_ring) {
1144 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1145 bp->rx_ring, bp->rx_ring_dma);
1146 bp->rx_ring = NULL;
1147 }
1148 if (bp->tx_ring) {
1149 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1150 bp->tx_ring, bp->tx_ring_dma);
1151 bp->tx_ring = NULL;
1152 }
Nicolas Ferre4df95132013-06-04 21:57:12 +00001153}
1154
1155static int gem_alloc_rx_buffers(struct macb *bp)
1156{
1157 int size;
1158
1159 size = RX_RING_SIZE * sizeof(struct sk_buff *);
1160 bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1161 if (!bp->rx_skbuff)
1162 return -ENOMEM;
1163 else
1164 netdev_dbg(bp->dev,
1165 "Allocated %d RX struct sk_buff entries at %p\n",
1166 RX_RING_SIZE, bp->rx_skbuff);
1167 return 0;
1168}
1169
1170static int macb_alloc_rx_buffers(struct macb *bp)
1171{
1172 int size;
1173
1174 size = RX_RING_SIZE * bp->rx_buffer_size;
1175 bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1176 &bp->rx_buffers_dma, GFP_KERNEL);
1177 if (!bp->rx_buffers)
1178 return -ENOMEM;
1179 else
1180 netdev_dbg(bp->dev,
1181 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1182 size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
1183 return 0;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001184}
1185
1186static int macb_alloc_consistent(struct macb *bp)
1187{
1188 int size;
1189
Havard Skinnemoen55054a12012-10-31 06:04:55 +00001190 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001191 bp->tx_skb = kmalloc(size, GFP_KERNEL);
1192 if (!bp->tx_skb)
1193 goto out_err;
1194
1195 size = RX_RING_BYTES;
1196 bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1197 &bp->rx_ring_dma, GFP_KERNEL);
1198 if (!bp->rx_ring)
1199 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001200 netdev_dbg(bp->dev,
1201 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1202 size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001203
1204 size = TX_RING_BYTES;
1205 bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1206 &bp->tx_ring_dma, GFP_KERNEL);
1207 if (!bp->tx_ring)
1208 goto out_err;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001209 netdev_dbg(bp->dev,
1210 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
1211 size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001212
Nicolas Ferre4df95132013-06-04 21:57:12 +00001213 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001214 goto out_err;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001215
1216 return 0;
1217
1218out_err:
1219 macb_free_consistent(bp);
1220 return -ENOMEM;
1221}
1222
Nicolas Ferre4df95132013-06-04 21:57:12 +00001223static void gem_init_rings(struct macb *bp)
1224{
1225 int i;
1226
1227 for (i = 0; i < TX_RING_SIZE; i++) {
1228 bp->tx_ring[i].addr = 0;
1229 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1230 }
1231 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1232
1233 bp->rx_tail = bp->rx_prepared_head = bp->tx_head = bp->tx_tail = 0;
1234
1235 gem_rx_refill(bp);
1236}
1237
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001238static void macb_init_rings(struct macb *bp)
1239{
1240 int i;
1241 dma_addr_t addr;
1242
1243 addr = bp->rx_buffers_dma;
1244 for (i = 0; i < RX_RING_SIZE; i++) {
1245 bp->rx_ring[i].addr = addr;
1246 bp->rx_ring[i].ctrl = 0;
Nicolas Ferre1b447912013-06-04 21:57:11 +00001247 addr += bp->rx_buffer_size;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001248 }
1249 bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
1250
1251 for (i = 0; i < TX_RING_SIZE; i++) {
1252 bp->tx_ring[i].addr = 0;
1253 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1254 }
1255 bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1256
1257 bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
1258}
1259
1260static void macb_reset_hw(struct macb *bp)
1261{
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001262 /*
1263 * Disable RX and TX (XXX: Should we halt the transmission
1264 * more gracefully?)
1265 */
1266 macb_writel(bp, NCR, 0);
1267
1268 /* Clear the stats registers (XXX: Update stats first?) */
1269 macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1270
1271 /* Clear all status flags */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001272 macb_writel(bp, TSR, -1);
1273 macb_writel(bp, RSR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001274
1275 /* Disable all interrupts */
Joachim Eastwood95ebcea2012-10-22 08:45:31 +00001276 macb_writel(bp, IDR, -1);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001277 macb_readl(bp, ISR);
1278}
1279
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001280static u32 gem_mdc_clk_div(struct macb *bp)
1281{
1282 u32 config;
1283 unsigned long pclk_hz = clk_get_rate(bp->pclk);
1284
1285 if (pclk_hz <= 20000000)
1286 config = GEM_BF(CLK, GEM_CLK_DIV8);
1287 else if (pclk_hz <= 40000000)
1288 config = GEM_BF(CLK, GEM_CLK_DIV16);
1289 else if (pclk_hz <= 80000000)
1290 config = GEM_BF(CLK, GEM_CLK_DIV32);
1291 else if (pclk_hz <= 120000000)
1292 config = GEM_BF(CLK, GEM_CLK_DIV48);
1293 else if (pclk_hz <= 160000000)
1294 config = GEM_BF(CLK, GEM_CLK_DIV64);
1295 else
1296 config = GEM_BF(CLK, GEM_CLK_DIV96);
1297
1298 return config;
1299}
1300
1301static u32 macb_mdc_clk_div(struct macb *bp)
1302{
1303 u32 config;
1304 unsigned long pclk_hz;
1305
1306 if (macb_is_gem(bp))
1307 return gem_mdc_clk_div(bp);
1308
1309 pclk_hz = clk_get_rate(bp->pclk);
1310 if (pclk_hz <= 20000000)
1311 config = MACB_BF(CLK, MACB_CLK_DIV8);
1312 else if (pclk_hz <= 40000000)
1313 config = MACB_BF(CLK, MACB_CLK_DIV16);
1314 else if (pclk_hz <= 80000000)
1315 config = MACB_BF(CLK, MACB_CLK_DIV32);
1316 else
1317 config = MACB_BF(CLK, MACB_CLK_DIV64);
1318
1319 return config;
1320}
1321
Jamie Iles757a03c2011-03-09 16:29:59 +00001322/*
1323 * Get the DMA bus width field of the network configuration register that we
1324 * should program. We find the width from decoding the design configuration
1325 * register to find the maximum supported data bus width.
1326 */
1327static u32 macb_dbw(struct macb *bp)
1328{
1329 if (!macb_is_gem(bp))
1330 return 0;
1331
1332 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1333 case 4:
1334 return GEM_BF(DBW, GEM_DBW128);
1335 case 2:
1336 return GEM_BF(DBW, GEM_DBW64);
1337 case 1:
1338 default:
1339 return GEM_BF(DBW, GEM_DBW32);
1340 }
1341}
1342
Jamie Iles0116da42011-03-14 17:38:30 +00001343/*
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001344 * Configure the receive DMA engine
1345 * - use the correct receive buffer size
Nicolas Ferree1755872014-07-24 13:50:58 +02001346 * - set best burst length for DMA operations
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001347 * (if not supported by FIFO, it will fallback to default)
1348 * - set both rx/tx packet buffers to full memory size
1349 * These are configurable parameters for GEM.
Jamie Iles0116da42011-03-14 17:38:30 +00001350 */
1351static void macb_configure_dma(struct macb *bp)
1352{
1353 u32 dmacfg;
1354
1355 if (macb_is_gem(bp)) {
1356 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001357 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
Nicolas Ferree1755872014-07-24 13:50:58 +02001358 if (bp->dma_burst_length)
1359 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
Nicolas Ferreb3e3bd712012-11-23 03:49:01 +00001360 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
Steffen Trumtrara1ae3852013-03-27 23:07:06 +00001361 dmacfg &= ~GEM_BIT(ENDIA);
Nicolas Ferree1755872014-07-24 13:50:58 +02001362 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1363 dmacfg);
Jamie Iles0116da42011-03-14 17:38:30 +00001364 gem_writel(bp, DMACFG, dmacfg);
1365 }
1366}
1367
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001368static void macb_init_hw(struct macb *bp)
1369{
1370 u32 config;
1371
1372 macb_reset_hw(bp);
Joachim Eastwood314bccc2012-11-07 08:14:52 +00001373 macb_set_hwaddr(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001374
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001375 config = macb_mdc_clk_div(bp);
Havard Skinnemoen29bc2e12012-10-31 06:04:58 +00001376 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001377 config |= MACB_BIT(PAE); /* PAuse Enable */
1378 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
Peter Korsgaard8dd4bd02010-04-07 21:53:41 -07001379 config |= MACB_BIT(BIG); /* Receive oversized frames */
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001380 if (bp->dev->flags & IFF_PROMISC)
1381 config |= MACB_BIT(CAF); /* Copy All Frames */
1382 if (!(bp->dev->flags & IFF_BROADCAST))
1383 config |= MACB_BIT(NBC); /* No BroadCast */
Jamie Iles757a03c2011-03-09 16:29:59 +00001384 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001385 macb_writel(bp, NCFGR, config);
Vitalii Demianets26cdfb42012-11-02 07:09:24 +00001386 bp->speed = SPEED_10;
1387 bp->duplex = DUPLEX_HALF;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001388
Jamie Iles0116da42011-03-14 17:38:30 +00001389 macb_configure_dma(bp);
1390
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001391 /* Initialize TX and RX buffers */
1392 macb_writel(bp, RBQP, bp->rx_ring_dma);
1393 macb_writel(bp, TBQP, bp->tx_ring_dma);
1394
1395 /* Enable TX and RX */
frederic RODO6c36a702007-07-12 19:07:24 +02001396 macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001397
1398 /* Enable interrupts */
Nicolas Ferree86cd532012-10-31 06:04:57 +00001399 macb_writel(bp, IER, (MACB_RX_INT_FLAGS
1400 | MACB_TX_INT_FLAGS
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001401 | MACB_BIT(HRESP)));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001402
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001403}
1404
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001405/*
1406 * The hash address register is 64 bits long and takes up two
1407 * locations in the memory map. The least significant bits are stored
1408 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1409 *
1410 * The unicast hash enable and the multicast hash enable bits in the
1411 * network configuration register enable the reception of hash matched
1412 * frames. The destination address is reduced to a 6 bit index into
1413 * the 64 bit hash register using the following hash function. The
1414 * hash function is an exclusive or of every sixth bit of the
1415 * destination address.
1416 *
1417 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1418 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1419 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1420 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1421 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1422 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1423 *
1424 * da[0] represents the least significant bit of the first byte
1425 * received, that is, the multicast/unicast indicator, and da[47]
1426 * represents the most significant bit of the last byte received. If
1427 * the hash index, hi[n], points to a bit that is set in the hash
1428 * register then the frame will be matched according to whether the
1429 * frame is multicast or unicast. A multicast match will be signalled
1430 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1431 * index points to a bit set in the hash register. A unicast match
1432 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1433 * and the hash index points to a bit set in the hash register. To
1434 * receive all multicast frames, the hash register should be set with
1435 * all ones and the multicast hash enable bit should be set in the
1436 * network configuration register.
1437 */
1438
1439static inline int hash_bit_value(int bitnr, __u8 *addr)
1440{
1441 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1442 return 1;
1443 return 0;
1444}
1445
1446/*
1447 * Return the hash index value for the specified address.
1448 */
1449static int hash_get_index(__u8 *addr)
1450{
1451 int i, j, bitval;
1452 int hash_index = 0;
1453
1454 for (j = 0; j < 6; j++) {
1455 for (i = 0, bitval = 0; i < 8; i++)
1456 bitval ^= hash_bit_value(i*6 + j, addr);
1457
1458 hash_index |= (bitval << j);
1459 }
1460
1461 return hash_index;
1462}
1463
1464/*
1465 * Add multicast addresses to the internal multicast-hash table.
1466 */
1467static void macb_sethashtable(struct net_device *dev)
1468{
Jiri Pirko22bedad32010-04-01 21:22:57 +00001469 struct netdev_hw_addr *ha;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001470 unsigned long mc_filter[2];
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +00001471 unsigned int bitnr;
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001472 struct macb *bp = netdev_priv(dev);
1473
1474 mc_filter[0] = mc_filter[1] = 0;
1475
Jiri Pirko22bedad32010-04-01 21:22:57 +00001476 netdev_for_each_mc_addr(ha, dev) {
1477 bitnr = hash_get_index(ha->addr);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001478 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1479 }
1480
Jamie Ilesf75ba502011-11-08 10:12:32 +00001481 macb_or_gem_writel(bp, HRB, mc_filter[0]);
1482 macb_or_gem_writel(bp, HRT, mc_filter[1]);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001483}
1484
1485/*
1486 * Enable/Disable promiscuous and multicast modes.
1487 */
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001488void macb_set_rx_mode(struct net_device *dev)
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001489{
1490 unsigned long cfg;
1491 struct macb *bp = netdev_priv(dev);
1492
1493 cfg = macb_readl(bp, NCFGR);
1494
1495 if (dev->flags & IFF_PROMISC)
1496 /* Enable promiscuous mode */
1497 cfg |= MACB_BIT(CAF);
1498 else if (dev->flags & (~IFF_PROMISC))
1499 /* Disable promiscuous mode */
1500 cfg &= ~MACB_BIT(CAF);
1501
1502 if (dev->flags & IFF_ALLMULTI) {
1503 /* Enable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001504 macb_or_gem_writel(bp, HRB, -1);
1505 macb_or_gem_writel(bp, HRT, -1);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001506 cfg |= MACB_BIT(NCFGR_MTI);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001507 } else if (!netdev_mc_empty(dev)) {
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001508 /* Enable specific multicasts */
1509 macb_sethashtable(dev);
1510 cfg |= MACB_BIT(NCFGR_MTI);
1511 } else if (dev->flags & (~IFF_ALLMULTI)) {
1512 /* Disable all multicast mode */
Jamie Ilesf75ba502011-11-08 10:12:32 +00001513 macb_or_gem_writel(bp, HRB, 0);
1514 macb_or_gem_writel(bp, HRT, 0);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001515 cfg &= ~MACB_BIT(NCFGR_MTI);
1516 }
1517
1518 macb_writel(bp, NCFGR, cfg);
1519}
Joachim Eastwoode0da1f12012-10-18 11:01:15 +00001520EXPORT_SYMBOL_GPL(macb_set_rx_mode);
Patrice Vilchez446ebd02007-07-12 19:07:25 +02001521
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001522static int macb_open(struct net_device *dev)
1523{
1524 struct macb *bp = netdev_priv(dev);
Nicolas Ferre4df95132013-06-04 21:57:12 +00001525 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001526 int err;
1527
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001528 netdev_dbg(bp->dev, "open\n");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001529
Nicolas Ferre03fc4722012-07-03 23:14:13 +00001530 /* carrier starts down */
1531 netif_carrier_off(dev);
1532
frederic RODO6c36a702007-07-12 19:07:24 +02001533 /* if the phy is not yet register, retry later*/
1534 if (!bp->phy_dev)
1535 return -EAGAIN;
1536
Nicolas Ferre1b447912013-06-04 21:57:11 +00001537 /* RX buffers initialization */
Nicolas Ferre4df95132013-06-04 21:57:12 +00001538 macb_init_rx_buffer_size(bp, bufsz);
Nicolas Ferre1b447912013-06-04 21:57:11 +00001539
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001540 err = macb_alloc_consistent(bp);
1541 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001542 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1543 err);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001544 return err;
1545 }
1546
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001547 napi_enable(&bp->napi);
1548
Nicolas Ferre4df95132013-06-04 21:57:12 +00001549 bp->macbgem_ops.mog_init_rings(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001550 macb_init_hw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001551
frederic RODO6c36a702007-07-12 19:07:24 +02001552 /* schedule a link state check */
1553 phy_start(bp->phy_dev);
1554
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001555 netif_start_queue(dev);
1556
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001557 return 0;
1558}
1559
1560static int macb_close(struct net_device *dev)
1561{
1562 struct macb *bp = netdev_priv(dev);
1563 unsigned long flags;
1564
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001565 netif_stop_queue(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001566 napi_disable(&bp->napi);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001567
frederic RODO6c36a702007-07-12 19:07:24 +02001568 if (bp->phy_dev)
1569 phy_stop(bp->phy_dev);
1570
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001571 spin_lock_irqsave(&bp->lock, flags);
1572 macb_reset_hw(bp);
1573 netif_carrier_off(dev);
1574 spin_unlock_irqrestore(&bp->lock, flags);
1575
1576 macb_free_consistent(bp);
1577
1578 return 0;
1579}
1580
Jamie Ilesa494ed82011-03-09 16:26:35 +00001581static void gem_update_stats(struct macb *bp)
1582{
1583 u32 __iomem *reg = bp->regs + GEM_OTX;
1584 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
1585 u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1;
1586
1587 for (; p < end; p++, reg++)
1588 *p += __raw_readl(reg);
1589}
1590
1591static struct net_device_stats *gem_get_stats(struct macb *bp)
1592{
1593 struct gem_stats *hwstat = &bp->hw_stats.gem;
1594 struct net_device_stats *nstat = &bp->stats;
1595
1596 gem_update_stats(bp);
1597
1598 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1599 hwstat->rx_alignment_errors +
1600 hwstat->rx_resource_errors +
1601 hwstat->rx_overruns +
1602 hwstat->rx_oversize_frames +
1603 hwstat->rx_jabbers +
1604 hwstat->rx_undersized_frames +
1605 hwstat->rx_length_field_frame_errors);
1606 nstat->tx_errors = (hwstat->tx_late_collisions +
1607 hwstat->tx_excessive_collisions +
1608 hwstat->tx_underrun +
1609 hwstat->tx_carrier_sense_errors);
1610 nstat->multicast = hwstat->rx_multicast_frames;
1611 nstat->collisions = (hwstat->tx_single_collision_frames +
1612 hwstat->tx_multiple_collision_frames +
1613 hwstat->tx_excessive_collisions);
1614 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1615 hwstat->rx_jabbers +
1616 hwstat->rx_undersized_frames +
1617 hwstat->rx_length_field_frame_errors);
1618 nstat->rx_over_errors = hwstat->rx_resource_errors;
1619 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1620 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1621 nstat->rx_fifo_errors = hwstat->rx_overruns;
1622 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1623 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1624 nstat->tx_fifo_errors = hwstat->tx_underrun;
1625
1626 return nstat;
1627}
1628
Joachim Eastwood2ea32ee2012-11-07 08:14:54 +00001629struct net_device_stats *macb_get_stats(struct net_device *dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001630{
1631 struct macb *bp = netdev_priv(dev);
1632 struct net_device_stats *nstat = &bp->stats;
Jamie Ilesa494ed82011-03-09 16:26:35 +00001633 struct macb_stats *hwstat = &bp->hw_stats.macb;
1634
1635 if (macb_is_gem(bp))
1636 return gem_get_stats(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001637
frederic RODO6c36a702007-07-12 19:07:24 +02001638 /* read stats from hardware */
1639 macb_update_stats(bp);
1640
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001641 /* Convert HW stats into netdevice stats */
1642 nstat->rx_errors = (hwstat->rx_fcs_errors +
1643 hwstat->rx_align_errors +
1644 hwstat->rx_resource_errors +
1645 hwstat->rx_overruns +
1646 hwstat->rx_oversize_pkts +
1647 hwstat->rx_jabbers +
1648 hwstat->rx_undersize_pkts +
1649 hwstat->sqe_test_errors +
1650 hwstat->rx_length_mismatch);
1651 nstat->tx_errors = (hwstat->tx_late_cols +
1652 hwstat->tx_excessive_cols +
1653 hwstat->tx_underruns +
1654 hwstat->tx_carrier_errors);
1655 nstat->collisions = (hwstat->tx_single_cols +
1656 hwstat->tx_multiple_cols +
1657 hwstat->tx_excessive_cols);
1658 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1659 hwstat->rx_jabbers +
1660 hwstat->rx_undersize_pkts +
1661 hwstat->rx_length_mismatch);
Alexander Steinb19f7f72011-04-13 05:03:24 +00001662 nstat->rx_over_errors = hwstat->rx_resource_errors +
1663 hwstat->rx_overruns;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001664 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1665 nstat->rx_frame_errors = hwstat->rx_align_errors;
1666 nstat->rx_fifo_errors = hwstat->rx_overruns;
1667 /* XXX: What does "missed" mean? */
1668 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1669 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1670 nstat->tx_fifo_errors = hwstat->tx_underruns;
1671 /* Don't know about heartbeat or window errors... */
1672
1673 return nstat;
1674}
Joachim Eastwood2ea32ee2012-11-07 08:14:54 +00001675EXPORT_SYMBOL_GPL(macb_get_stats);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001676
1677static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1678{
1679 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001680 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001681
frederic RODO6c36a702007-07-12 19:07:24 +02001682 if (!phydev)
1683 return -ENODEV;
1684
1685 return phy_ethtool_gset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001686}
1687
1688static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1689{
1690 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001691 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001692
frederic RODO6c36a702007-07-12 19:07:24 +02001693 if (!phydev)
1694 return -ENODEV;
1695
1696 return phy_ethtool_sset(phydev, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001697}
1698
Nicolas Ferred1d1b532012-10-31 06:04:56 +00001699static int macb_get_regs_len(struct net_device *netdev)
1700{
1701 return MACB_GREGS_NBR * sizeof(u32);
1702}
1703
1704static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1705 void *p)
1706{
1707 struct macb *bp = netdev_priv(dev);
1708 unsigned int tail, head;
1709 u32 *regs_buff = p;
1710
1711 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
1712 | MACB_GREGS_VERSION;
1713
1714 tail = macb_tx_ring_wrap(bp->tx_tail);
1715 head = macb_tx_ring_wrap(bp->tx_head);
1716
1717 regs_buff[0] = macb_readl(bp, NCR);
1718 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
1719 regs_buff[2] = macb_readl(bp, NSR);
1720 regs_buff[3] = macb_readl(bp, TSR);
1721 regs_buff[4] = macb_readl(bp, RBQP);
1722 regs_buff[5] = macb_readl(bp, TBQP);
1723 regs_buff[6] = macb_readl(bp, RSR);
1724 regs_buff[7] = macb_readl(bp, IMR);
1725
1726 regs_buff[8] = tail;
1727 regs_buff[9] = head;
1728 regs_buff[10] = macb_tx_dma(bp, tail);
1729 regs_buff[11] = macb_tx_dma(bp, head);
1730
1731 if (macb_is_gem(bp)) {
1732 regs_buff[12] = gem_readl(bp, USRIO);
1733 regs_buff[13] = gem_readl(bp, DMACFG);
1734 }
1735}
1736
Joachim Eastwood0005f542012-10-18 11:01:12 +00001737const struct ethtool_ops macb_ethtool_ops = {
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001738 .get_settings = macb_get_settings,
1739 .set_settings = macb_set_settings,
Nicolas Ferred1d1b532012-10-31 06:04:56 +00001740 .get_regs_len = macb_get_regs_len,
1741 .get_regs = macb_get_regs,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001742 .get_link = ethtool_op_get_link,
Richard Cochran17f393e2012-04-03 22:59:31 +00001743 .get_ts_info = ethtool_op_get_ts_info,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001744};
Joachim Eastwood0005f542012-10-18 11:01:12 +00001745EXPORT_SYMBOL_GPL(macb_ethtool_ops);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001746
Joachim Eastwood0005f542012-10-18 11:01:12 +00001747int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001748{
1749 struct macb *bp = netdev_priv(dev);
frederic RODO6c36a702007-07-12 19:07:24 +02001750 struct phy_device *phydev = bp->phy_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001751
1752 if (!netif_running(dev))
1753 return -EINVAL;
1754
frederic RODO6c36a702007-07-12 19:07:24 +02001755 if (!phydev)
1756 return -ENODEV;
1757
Richard Cochran28b04112010-07-17 08:48:55 +00001758 return phy_mii_ioctl(phydev, rq, cmd);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001759}
Joachim Eastwood0005f542012-10-18 11:01:12 +00001760EXPORT_SYMBOL_GPL(macb_ioctl);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001761
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001762static const struct net_device_ops macb_netdev_ops = {
1763 .ndo_open = macb_open,
1764 .ndo_stop = macb_close,
1765 .ndo_start_xmit = macb_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001766 .ndo_set_rx_mode = macb_set_rx_mode,
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001767 .ndo_get_stats = macb_get_stats,
1768 .ndo_do_ioctl = macb_ioctl,
1769 .ndo_validate_addr = eth_validate_addr,
1770 .ndo_change_mtu = eth_change_mtu,
1771 .ndo_set_mac_address = eth_mac_addr,
Thomas Petazzoni6e8cf5c2009-05-04 11:08:41 -07001772#ifdef CONFIG_NET_POLL_CONTROLLER
1773 .ndo_poll_controller = macb_poll_controller,
1774#endif
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001775};
1776
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001777#if defined(CONFIG_OF)
Nicolas Ferree1755872014-07-24 13:50:58 +02001778static struct macb_config pc302gem_config = {
1779 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE,
1780 .dma_burst_length = 16,
1781};
1782
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001783static const struct of_device_id macb_dt_ids[] = {
1784 { .compatible = "cdns,at32ap7000-macb" },
1785 { .compatible = "cdns,at91sam9260-macb" },
1786 { .compatible = "cdns,macb" },
Nicolas Ferree1755872014-07-24 13:50:58 +02001787 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
1788 { .compatible = "cdns,gem", .data = &pc302gem_config },
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001789 { /* sentinel */ }
1790};
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001791MODULE_DEVICE_TABLE(of, macb_dt_ids);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001792#endif
1793
Nicolas Ferree1755872014-07-24 13:50:58 +02001794/*
1795 * Configure peripheral capacities according to device tree
1796 * and integration options used
1797 */
1798static void macb_configure_caps(struct macb *bp)
1799{
1800 u32 dcfg;
1801 const struct of_device_id *match;
1802 const struct macb_config *config;
1803
1804 if (bp->pdev->dev.of_node) {
1805 match = of_match_node(macb_dt_ids, bp->pdev->dev.of_node);
1806 if (match && match->data) {
1807 config = (const struct macb_config *)match->data;
1808
1809 bp->caps = config->caps;
1810 /*
1811 * As we have access to the matching node, configure
1812 * DMA burst length as well
1813 */
1814 bp->dma_burst_length = config->dma_burst_length;
1815 }
1816 }
1817
1818 if (MACB_BFEXT(IDNUM, macb_readl(bp, MID)) == 0x2)
1819 bp->caps |= MACB_CAPS_MACB_IS_GEM;
1820
1821 if (macb_is_gem(bp)) {
1822 dcfg = gem_readl(bp, DCFG1);
1823 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
1824 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
1825 dcfg = gem_readl(bp, DCFG2);
1826 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
1827 bp->caps |= MACB_CAPS_FIFO_MODE;
1828 }
1829
1830 netdev_dbg(bp->dev, "Cadence caps 0x%08x\n", bp->caps);
1831}
1832
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01001833static int __init macb_probe(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001834{
Jamie Iles84e0cdb2011-03-08 20:17:06 +00001835 struct macb_platform_data *pdata;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001836 struct resource *regs;
1837 struct net_device *dev;
1838 struct macb *bp;
frederic RODO6c36a702007-07-12 19:07:24 +02001839 struct phy_device *phydev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001840 u32 config;
1841 int err = -ENXIO;
Jean-Christophe PLAGNIOL-VILLARD8ef29f8a2012-10-31 06:04:59 +00001842 struct pinctrl *pinctrl;
Guenter Roeck50907042013-04-02 09:35:09 +00001843 const char *mac;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001844
1845 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1846 if (!regs) {
1847 dev_err(&pdev->dev, "no mmio resource defined\n");
1848 goto err_out;
1849 }
1850
Jean-Christophe PLAGNIOL-VILLARD8ef29f8a2012-10-31 06:04:59 +00001851 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1852 if (IS_ERR(pinctrl)) {
1853 err = PTR_ERR(pinctrl);
1854 if (err == -EPROBE_DEFER)
1855 goto err_out;
1856
1857 dev_warn(&pdev->dev, "No pinctrl provided\n");
1858 }
1859
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001860 err = -ENOMEM;
1861 dev = alloc_etherdev(sizeof(*bp));
Joe Perches41de8d42012-01-29 13:47:52 +00001862 if (!dev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001863 goto err_out;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001864
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001865 SET_NETDEV_DEV(dev, &pdev->dev);
1866
1867 /* TODO: Actually, we have some interesting features... */
1868 dev->features |= 0;
1869
1870 bp = netdev_priv(dev);
1871 bp->pdev = pdev;
1872 bp->dev = dev;
1873
1874 spin_lock_init(&bp->lock);
Nicolas Ferree86cd532012-10-31 06:04:57 +00001875 INIT_WORK(&bp->tx_error_task, macb_tx_error_task);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001876
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08001877 bp->pclk = devm_clk_get(&pdev->dev, "pclk");
Andrew Victor0cc86742007-02-07 16:40:44 +01001878 if (IS_ERR(bp->pclk)) {
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08001879 err = PTR_ERR(bp->pclk);
1880 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
Andrew Victor0cc86742007-02-07 16:40:44 +01001881 goto err_out_free_dev;
1882 }
Jamie Iles461845d2011-03-08 20:19:23 +00001883
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08001884 bp->hclk = devm_clk_get(&pdev->dev, "hclk");
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001885 if (IS_ERR(bp->hclk)) {
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08001886 err = PTR_ERR(bp->hclk);
1887 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
1888 goto err_out_free_dev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001889 }
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08001890
Soren Brinkmanne1824df2013-12-10 16:07:23 -08001891 bp->tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
1892
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08001893 err = clk_prepare_enable(bp->pclk);
1894 if (err) {
1895 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
1896 goto err_out_free_dev;
1897 }
1898
1899 err = clk_prepare_enable(bp->hclk);
1900 if (err) {
1901 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
1902 goto err_out_disable_pclk;
1903 }
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001904
Soren Brinkmanne1824df2013-12-10 16:07:23 -08001905 if (!IS_ERR(bp->tx_clk)) {
1906 err = clk_prepare_enable(bp->tx_clk);
1907 if (err) {
1908 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n",
1909 err);
1910 goto err_out_disable_hclk;
1911 }
1912 }
1913
Soren Brinkmann60fe7162013-12-10 16:07:21 -08001914 bp->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001915 if (!bp->regs) {
1916 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1917 err = -ENOMEM;
1918 goto err_out_disable_clocks;
1919 }
1920
1921 dev->irq = platform_get_irq(pdev, 0);
Soren Brinkmann0a4acf02013-12-10 16:07:22 -08001922 err = devm_request_irq(&pdev->dev, dev->irq, macb_interrupt, 0,
1923 dev->name, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001924 if (err) {
Jamie Ilesc220f8c2011-03-08 20:27:08 +00001925 dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
1926 dev->irq, err);
Soren Brinkmann60fe7162013-12-10 16:07:21 -08001927 goto err_out_disable_clocks;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001928 }
1929
Alexander Beregalov5f1fa992009-04-11 07:42:26 +00001930 dev->netdev_ops = &macb_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001931 netif_napi_add(dev, &bp->napi, macb_poll, 64);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001932 dev->ethtool_ops = &macb_ethtool_ops;
1933
1934 dev->base_addr = regs->start;
1935
Nicolas Ferree1755872014-07-24 13:50:58 +02001936 /* setup capacities */
1937 macb_configure_caps(bp);
1938
Nicolas Ferre4df95132013-06-04 21:57:12 +00001939 /* setup appropriated routines according to adapter type */
1940 if (macb_is_gem(bp)) {
1941 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
1942 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
1943 bp->macbgem_ops.mog_init_rings = gem_init_rings;
1944 bp->macbgem_ops.mog_rx = gem_rx;
1945 } else {
1946 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
1947 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
1948 bp->macbgem_ops.mog_init_rings = macb_init_rings;
1949 bp->macbgem_ops.mog_rx = macb_rx;
1950 }
1951
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001952 /* Set MII management clock divider */
Jamie Iles70c9f3d2011-03-09 16:22:54 +00001953 config = macb_mdc_clk_div(bp);
Jamie Iles757a03c2011-03-09 16:29:59 +00001954 config |= macb_dbw(bp);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001955 macb_writel(bp, NCFGR, config);
1956
Guenter Roeck50907042013-04-02 09:35:09 +00001957 mac = of_get_mac_address(pdev->dev.of_node);
1958 if (mac)
1959 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
1960 else
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001961 macb_get_hwaddr(bp);
frederic RODO6c36a702007-07-12 19:07:24 +02001962
Guenter Roeck50907042013-04-02 09:35:09 +00001963 err = of_get_phy_mode(pdev->dev.of_node);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001964 if (err < 0) {
Jingoo Hanc607a0d2013-08-30 14:12:21 +09001965 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01001966 if (pdata && pdata->is_rmii)
1967 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
1968 else
1969 bp->phy_interface = PHY_INTERFACE_MODE_MII;
1970 } else {
1971 bp->phy_interface = err;
1972 }
1973
Patrice Vilchez140b7552012-10-31 06:04:50 +00001974 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
1975 macb_or_gem_writel(bp, USRIO, GEM_BIT(RGMII));
1976 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
Andrew Victor0cc86742007-02-07 16:40:44 +01001977#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001978 macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
1979 MACB_BIT(CLKEN)));
Andrew Victor0cc86742007-02-07 16:40:44 +01001980#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001981 macb_or_gem_writel(bp, USRIO, 0);
Andrew Victor0cc86742007-02-07 16:40:44 +01001982#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001983 else
Andrew Victor0cc86742007-02-07 16:40:44 +01001984#if defined(CONFIG_ARCH_AT91)
Jamie Ilesf75ba502011-11-08 10:12:32 +00001985 macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
Andrew Victor0cc86742007-02-07 16:40:44 +01001986#else
Jamie Ilesf75ba502011-11-08 10:12:32 +00001987 macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
Andrew Victor0cc86742007-02-07 16:40:44 +01001988#endif
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001989
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001990 err = register_netdev(dev);
1991 if (err) {
1992 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
Soren Brinkmann0a4acf02013-12-10 16:07:22 -08001993 goto err_out_disable_clocks;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001994 }
1995
Nicolas Ferre72ca8202013-04-14 22:04:33 +00001996 err = macb_mii_init(bp);
1997 if (err)
frederic RODO6c36a702007-07-12 19:07:24 +02001998 goto err_out_unregister_netdev;
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01001999
frederic RODO6c36a702007-07-12 19:07:24 +02002000 platform_set_drvdata(pdev, dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002001
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002002 netif_carrier_off(dev);
2003
Jamie Ilesf75ba502011-11-08 10:12:32 +00002004 netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n",
2005 macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr,
2006 dev->irq, dev->dev_addr);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002007
frederic RODO6c36a702007-07-12 19:07:24 +02002008 phydev = bp->phy_dev;
Jamie Ilesc220f8c2011-03-08 20:27:08 +00002009 netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
2010 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
frederic RODO6c36a702007-07-12 19:07:24 +02002011
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002012 return 0;
2013
frederic RODO6c36a702007-07-12 19:07:24 +02002014err_out_unregister_netdev:
2015 unregister_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002016err_out_disable_clocks:
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002017 if (!IS_ERR(bp->tx_clk))
2018 clk_disable_unprepare(bp->tx_clk);
2019err_out_disable_hclk:
Steffen Trumtrarace58012013-03-27 23:07:07 +00002020 clk_disable_unprepare(bp->hclk);
Soren Brinkmannb48e0ba2013-12-10 16:07:20 -08002021err_out_disable_pclk:
Steffen Trumtrarace58012013-03-27 23:07:07 +00002022 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002023err_out_free_dev:
2024 free_netdev(dev);
2025err_out:
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002026 return err;
2027}
2028
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01002029static int __exit macb_remove(struct platform_device *pdev)
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002030{
2031 struct net_device *dev;
2032 struct macb *bp;
2033
2034 dev = platform_get_drvdata(pdev);
2035
2036 if (dev) {
2037 bp = netdev_priv(dev);
Atsushi Nemoto84b79012008-04-10 23:30:07 +09002038 if (bp->phy_dev)
2039 phy_disconnect(bp->phy_dev);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002040 mdiobus_unregister(bp->mii_bus);
2041 kfree(bp->mii_bus->irq);
2042 mdiobus_free(bp->mii_bus);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002043 unregister_netdev(dev);
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002044 if (!IS_ERR(bp->tx_clk))
2045 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002046 clk_disable_unprepare(bp->hclk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002047 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002048 free_netdev(dev);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002049 }
2050
2051 return 0;
2052}
2053
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002054#ifdef CONFIG_PM
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002055static int macb_suspend(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002056{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002057 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002058 struct net_device *netdev = platform_get_drvdata(pdev);
2059 struct macb *bp = netdev_priv(netdev);
2060
Nicolas Ferre03fc4722012-07-03 23:14:13 +00002061 netif_carrier_off(netdev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002062 netif_device_detach(netdev);
2063
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002064 if (!IS_ERR(bp->tx_clk))
2065 clk_disable_unprepare(bp->tx_clk);
Steffen Trumtrarace58012013-03-27 23:07:07 +00002066 clk_disable_unprepare(bp->hclk);
2067 clk_disable_unprepare(bp->pclk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002068
2069 return 0;
2070}
2071
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002072static int macb_resume(struct device *dev)
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002073{
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002074 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002075 struct net_device *netdev = platform_get_drvdata(pdev);
2076 struct macb *bp = netdev_priv(netdev);
2077
Steffen Trumtrarace58012013-03-27 23:07:07 +00002078 clk_prepare_enable(bp->pclk);
2079 clk_prepare_enable(bp->hclk);
Soren Brinkmanne1824df2013-12-10 16:07:23 -08002080 if (!IS_ERR(bp->tx_clk))
2081 clk_prepare_enable(bp->tx_clk);
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002082
2083 netif_device_attach(netdev);
2084
2085 return 0;
2086}
Haavard Skinnemoenc1f598f2008-03-04 13:39:29 +01002087#endif
2088
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002089static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
2090
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002091static struct platform_driver macb_driver = {
Haavard Skinnemoen06c3fd62008-01-31 13:10:22 +01002092 .remove = __exit_p(macb_remove),
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002093 .driver = {
2094 .name = "macb",
Kay Sievers72abb462008-04-18 13:50:44 -07002095 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARDfb97a842011-11-18 15:29:25 +01002096 .of_match_table = of_match_ptr(macb_dt_ids),
Soren Brinkmann0dfc3e12013-12-10 16:07:19 -08002097 .pm = &macb_pm_ops,
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002098 },
2099};
2100
Jingoo Hanb543a8d2013-03-04 16:43:18 +00002101module_platform_driver_probe(macb_driver, macb_probe);
Haavard Skinnemoen89e57852006-11-09 14:51:17 +01002102
2103MODULE_LICENSE("GPL");
Jamie Ilesf75ba502011-11-08 10:12:32 +00002104MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02002105MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Kay Sievers72abb462008-04-18 13:50:44 -07002106MODULE_ALIAS("platform:macb");