blob: 0e8cc75bb5f41899d5736317225b83f61e16bd52 [file] [log] [blame]
Thierry Redinga1702852009-03-27 00:12:24 -07001/*
2 * linux/drivers/net/ethoc.c
3 *
4 * Copyright (C) 2007-2008 Avionic Design Development GmbH
5 * Copyright (C) 2008-2009 Avionic Design GmbH
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Written by Thierry Reding <thierry.reding@avionic-design.de>
12 */
13
14#include <linux/etherdevice.h>
15#include <linux/crc32.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000016#include <linux/interrupt.h>
Thierry Redinga1702852009-03-27 00:12:24 -070017#include <linux/io.h>
18#include <linux/mii.h>
19#include <linux/phy.h>
20#include <linux/platform_device.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040021#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Jonas Bonne0f42582010-11-25 02:30:25 +000023#include <linux/of.h>
Thierry Redinga1702852009-03-27 00:12:24 -070024#include <net/ethoc.h>
25
Thomas Chou0baa0802009-10-04 23:33:20 +000026static int buffer_size = 0x8000; /* 32 KBytes */
27module_param(buffer_size, int, 0);
28MODULE_PARM_DESC(buffer_size, "DMA buffer allocation size");
29
Thierry Redinga1702852009-03-27 00:12:24 -070030/* register offsets */
31#define MODER 0x00
32#define INT_SOURCE 0x04
33#define INT_MASK 0x08
34#define IPGT 0x0c
35#define IPGR1 0x10
36#define IPGR2 0x14
37#define PACKETLEN 0x18
38#define COLLCONF 0x1c
39#define TX_BD_NUM 0x20
40#define CTRLMODER 0x24
41#define MIIMODER 0x28
42#define MIICOMMAND 0x2c
43#define MIIADDRESS 0x30
44#define MIITX_DATA 0x34
45#define MIIRX_DATA 0x38
46#define MIISTATUS 0x3c
47#define MAC_ADDR0 0x40
48#define MAC_ADDR1 0x44
49#define ETH_HASH0 0x48
50#define ETH_HASH1 0x4c
51#define ETH_TXCTRL 0x50
52
53/* mode register */
54#define MODER_RXEN (1 << 0) /* receive enable */
55#define MODER_TXEN (1 << 1) /* transmit enable */
56#define MODER_NOPRE (1 << 2) /* no preamble */
57#define MODER_BRO (1 << 3) /* broadcast address */
58#define MODER_IAM (1 << 4) /* individual address mode */
59#define MODER_PRO (1 << 5) /* promiscuous mode */
60#define MODER_IFG (1 << 6) /* interframe gap for incoming frames */
61#define MODER_LOOP (1 << 7) /* loopback */
62#define MODER_NBO (1 << 8) /* no back-off */
63#define MODER_EDE (1 << 9) /* excess defer enable */
64#define MODER_FULLD (1 << 10) /* full duplex */
65#define MODER_RESET (1 << 11) /* FIXME: reset (undocumented) */
66#define MODER_DCRC (1 << 12) /* delayed CRC enable */
67#define MODER_CRC (1 << 13) /* CRC enable */
68#define MODER_HUGE (1 << 14) /* huge packets enable */
69#define MODER_PAD (1 << 15) /* padding enabled */
70#define MODER_RSM (1 << 16) /* receive small packets */
71
72/* interrupt source and mask registers */
73#define INT_MASK_TXF (1 << 0) /* transmit frame */
74#define INT_MASK_TXE (1 << 1) /* transmit error */
75#define INT_MASK_RXF (1 << 2) /* receive frame */
76#define INT_MASK_RXE (1 << 3) /* receive error */
77#define INT_MASK_BUSY (1 << 4)
78#define INT_MASK_TXC (1 << 5) /* transmit control frame */
79#define INT_MASK_RXC (1 << 6) /* receive control frame */
80
81#define INT_MASK_TX (INT_MASK_TXF | INT_MASK_TXE)
82#define INT_MASK_RX (INT_MASK_RXF | INT_MASK_RXE)
83
84#define INT_MASK_ALL ( \
85 INT_MASK_TXF | INT_MASK_TXE | \
86 INT_MASK_RXF | INT_MASK_RXE | \
87 INT_MASK_TXC | INT_MASK_RXC | \
88 INT_MASK_BUSY \
89 )
90
91/* packet length register */
92#define PACKETLEN_MIN(min) (((min) & 0xffff) << 16)
93#define PACKETLEN_MAX(max) (((max) & 0xffff) << 0)
94#define PACKETLEN_MIN_MAX(min, max) (PACKETLEN_MIN(min) | \
95 PACKETLEN_MAX(max))
96
97/* transmit buffer number register */
98#define TX_BD_NUM_VAL(x) (((x) <= 0x80) ? (x) : 0x80)
99
100/* control module mode register */
101#define CTRLMODER_PASSALL (1 << 0) /* pass all receive frames */
102#define CTRLMODER_RXFLOW (1 << 1) /* receive control flow */
103#define CTRLMODER_TXFLOW (1 << 2) /* transmit control flow */
104
105/* MII mode register */
106#define MIIMODER_CLKDIV(x) ((x) & 0xfe) /* needs to be an even number */
107#define MIIMODER_NOPRE (1 << 8) /* no preamble */
108
109/* MII command register */
110#define MIICOMMAND_SCAN (1 << 0) /* scan status */
111#define MIICOMMAND_READ (1 << 1) /* read status */
112#define MIICOMMAND_WRITE (1 << 2) /* write control data */
113
114/* MII address register */
115#define MIIADDRESS_FIAD(x) (((x) & 0x1f) << 0)
116#define MIIADDRESS_RGAD(x) (((x) & 0x1f) << 8)
117#define MIIADDRESS_ADDR(phy, reg) (MIIADDRESS_FIAD(phy) | \
118 MIIADDRESS_RGAD(reg))
119
120/* MII transmit data register */
121#define MIITX_DATA_VAL(x) ((x) & 0xffff)
122
123/* MII receive data register */
124#define MIIRX_DATA_VAL(x) ((x) & 0xffff)
125
126/* MII status register */
127#define MIISTATUS_LINKFAIL (1 << 0)
128#define MIISTATUS_BUSY (1 << 1)
129#define MIISTATUS_INVALID (1 << 2)
130
131/* TX buffer descriptor */
132#define TX_BD_CS (1 << 0) /* carrier sense lost */
133#define TX_BD_DF (1 << 1) /* defer indication */
134#define TX_BD_LC (1 << 2) /* late collision */
135#define TX_BD_RL (1 << 3) /* retransmission limit */
136#define TX_BD_RETRY_MASK (0x00f0)
137#define TX_BD_RETRY(x) (((x) & 0x00f0) >> 4)
138#define TX_BD_UR (1 << 8) /* transmitter underrun */
139#define TX_BD_CRC (1 << 11) /* TX CRC enable */
140#define TX_BD_PAD (1 << 12) /* pad enable for short packets */
141#define TX_BD_WRAP (1 << 13)
142#define TX_BD_IRQ (1 << 14) /* interrupt request enable */
143#define TX_BD_READY (1 << 15) /* TX buffer ready */
144#define TX_BD_LEN(x) (((x) & 0xffff) << 16)
145#define TX_BD_LEN_MASK (0xffff << 16)
146
147#define TX_BD_STATS (TX_BD_CS | TX_BD_DF | TX_BD_LC | \
148 TX_BD_RL | TX_BD_RETRY_MASK | TX_BD_UR)
149
150/* RX buffer descriptor */
151#define RX_BD_LC (1 << 0) /* late collision */
152#define RX_BD_CRC (1 << 1) /* RX CRC error */
153#define RX_BD_SF (1 << 2) /* short frame */
154#define RX_BD_TL (1 << 3) /* too long */
155#define RX_BD_DN (1 << 4) /* dribble nibble */
156#define RX_BD_IS (1 << 5) /* invalid symbol */
157#define RX_BD_OR (1 << 6) /* receiver overrun */
158#define RX_BD_MISS (1 << 7)
159#define RX_BD_CF (1 << 8) /* control frame */
160#define RX_BD_WRAP (1 << 13)
161#define RX_BD_IRQ (1 << 14) /* interrupt request enable */
162#define RX_BD_EMPTY (1 << 15)
163#define RX_BD_LEN(x) (((x) & 0xffff) << 16)
164
165#define RX_BD_STATS (RX_BD_LC | RX_BD_CRC | RX_BD_SF | RX_BD_TL | \
166 RX_BD_DN | RX_BD_IS | RX_BD_OR | RX_BD_MISS)
167
168#define ETHOC_BUFSIZ 1536
169#define ETHOC_ZLEN 64
170#define ETHOC_BD_BASE 0x400
171#define ETHOC_TIMEOUT (HZ / 2)
172#define ETHOC_MII_TIMEOUT (1 + (HZ / 5))
173
174/**
175 * struct ethoc - driver-private device structure
176 * @iobase: pointer to I/O memory region
177 * @membase: pointer to buffer memory region
Thomas Chou0baa0802009-10-04 23:33:20 +0000178 * @dma_alloc: dma allocated buffer size
Thomas Chouee02a4e2010-05-23 16:44:02 +0000179 * @io_region_size: I/O memory region size
Thierry Redinga1702852009-03-27 00:12:24 -0700180 * @num_tx: number of send buffers
181 * @cur_tx: last send buffer written
182 * @dty_tx: last buffer actually sent
183 * @num_rx: number of receive buffers
184 * @cur_rx: current receive buffer
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000185 * @vma: pointer to array of virtual memory addresses for buffers
Thierry Redinga1702852009-03-27 00:12:24 -0700186 * @netdev: pointer to network device structure
187 * @napi: NAPI structure
Thierry Redinga1702852009-03-27 00:12:24 -0700188 * @msg_enable: device state flags
Thierry Redinga1702852009-03-27 00:12:24 -0700189 * @lock: device lock
190 * @phy: attached PHY
191 * @mdio: MDIO bus for PHY access
192 * @phy_id: address of attached PHY
193 */
194struct ethoc {
195 void __iomem *iobase;
196 void __iomem *membase;
Thomas Chou0baa0802009-10-04 23:33:20 +0000197 int dma_alloc;
Thomas Chouee02a4e2010-05-23 16:44:02 +0000198 resource_size_t io_region_size;
Thierry Redinga1702852009-03-27 00:12:24 -0700199
200 unsigned int num_tx;
201 unsigned int cur_tx;
202 unsigned int dty_tx;
203
204 unsigned int num_rx;
205 unsigned int cur_rx;
206
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000207 void** vma;
208
Thierry Redinga1702852009-03-27 00:12:24 -0700209 struct net_device *netdev;
210 struct napi_struct napi;
Thierry Redinga1702852009-03-27 00:12:24 -0700211 u32 msg_enable;
212
Thierry Redinga1702852009-03-27 00:12:24 -0700213 spinlock_t lock;
214
215 struct phy_device *phy;
216 struct mii_bus *mdio;
217 s8 phy_id;
218};
219
220/**
221 * struct ethoc_bd - buffer descriptor
222 * @stat: buffer statistics
223 * @addr: physical memory address
224 */
225struct ethoc_bd {
226 u32 stat;
227 u32 addr;
228};
229
Thomas Chou16dd18b2009-10-07 14:16:42 +0000230static inline u32 ethoc_read(struct ethoc *dev, loff_t offset)
Thierry Redinga1702852009-03-27 00:12:24 -0700231{
232 return ioread32(dev->iobase + offset);
233}
234
Thomas Chou16dd18b2009-10-07 14:16:42 +0000235static inline void ethoc_write(struct ethoc *dev, loff_t offset, u32 data)
Thierry Redinga1702852009-03-27 00:12:24 -0700236{
237 iowrite32(data, dev->iobase + offset);
238}
239
Thomas Chou16dd18b2009-10-07 14:16:42 +0000240static inline void ethoc_read_bd(struct ethoc *dev, int index,
241 struct ethoc_bd *bd)
Thierry Redinga1702852009-03-27 00:12:24 -0700242{
243 loff_t offset = ETHOC_BD_BASE + (index * sizeof(struct ethoc_bd));
244 bd->stat = ethoc_read(dev, offset + 0);
245 bd->addr = ethoc_read(dev, offset + 4);
246}
247
Thomas Chou16dd18b2009-10-07 14:16:42 +0000248static inline void ethoc_write_bd(struct ethoc *dev, int index,
Thierry Redinga1702852009-03-27 00:12:24 -0700249 const struct ethoc_bd *bd)
250{
251 loff_t offset = ETHOC_BD_BASE + (index * sizeof(struct ethoc_bd));
252 ethoc_write(dev, offset + 0, bd->stat);
253 ethoc_write(dev, offset + 4, bd->addr);
254}
255
Thomas Chou16dd18b2009-10-07 14:16:42 +0000256static inline void ethoc_enable_irq(struct ethoc *dev, u32 mask)
Thierry Redinga1702852009-03-27 00:12:24 -0700257{
258 u32 imask = ethoc_read(dev, INT_MASK);
259 imask |= mask;
260 ethoc_write(dev, INT_MASK, imask);
261}
262
Thomas Chou16dd18b2009-10-07 14:16:42 +0000263static inline void ethoc_disable_irq(struct ethoc *dev, u32 mask)
Thierry Redinga1702852009-03-27 00:12:24 -0700264{
265 u32 imask = ethoc_read(dev, INT_MASK);
266 imask &= ~mask;
267 ethoc_write(dev, INT_MASK, imask);
268}
269
Thomas Chou16dd18b2009-10-07 14:16:42 +0000270static inline void ethoc_ack_irq(struct ethoc *dev, u32 mask)
Thierry Redinga1702852009-03-27 00:12:24 -0700271{
272 ethoc_write(dev, INT_SOURCE, mask);
273}
274
Thomas Chou16dd18b2009-10-07 14:16:42 +0000275static inline void ethoc_enable_rx_and_tx(struct ethoc *dev)
Thierry Redinga1702852009-03-27 00:12:24 -0700276{
277 u32 mode = ethoc_read(dev, MODER);
278 mode |= MODER_RXEN | MODER_TXEN;
279 ethoc_write(dev, MODER, mode);
280}
281
Thomas Chou16dd18b2009-10-07 14:16:42 +0000282static inline void ethoc_disable_rx_and_tx(struct ethoc *dev)
Thierry Redinga1702852009-03-27 00:12:24 -0700283{
284 u32 mode = ethoc_read(dev, MODER);
285 mode &= ~(MODER_RXEN | MODER_TXEN);
286 ethoc_write(dev, MODER, mode);
287}
288
David S. Miller5cf3e032010-07-07 18:23:19 -0700289static int ethoc_init_ring(struct ethoc *dev, unsigned long mem_start)
Thierry Redinga1702852009-03-27 00:12:24 -0700290{
291 struct ethoc_bd bd;
292 int i;
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000293 void* vma;
Thierry Redinga1702852009-03-27 00:12:24 -0700294
295 dev->cur_tx = 0;
296 dev->dty_tx = 0;
297 dev->cur_rx = 0;
298
Jonas Bonnee4f56b2010-06-11 02:47:36 +0000299 ethoc_write(dev, TX_BD_NUM, dev->num_tx);
300
Thierry Redinga1702852009-03-27 00:12:24 -0700301 /* setup transmission buffers */
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000302 bd.addr = mem_start;
Thierry Redinga1702852009-03-27 00:12:24 -0700303 bd.stat = TX_BD_IRQ | TX_BD_CRC;
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000304 vma = dev->membase;
Thierry Redinga1702852009-03-27 00:12:24 -0700305
306 for (i = 0; i < dev->num_tx; i++) {
307 if (i == dev->num_tx - 1)
308 bd.stat |= TX_BD_WRAP;
309
310 ethoc_write_bd(dev, i, &bd);
311 bd.addr += ETHOC_BUFSIZ;
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000312
313 dev->vma[i] = vma;
314 vma += ETHOC_BUFSIZ;
Thierry Redinga1702852009-03-27 00:12:24 -0700315 }
316
Thierry Redinga1702852009-03-27 00:12:24 -0700317 bd.stat = RX_BD_EMPTY | RX_BD_IRQ;
318
319 for (i = 0; i < dev->num_rx; i++) {
320 if (i == dev->num_rx - 1)
321 bd.stat |= RX_BD_WRAP;
322
323 ethoc_write_bd(dev, dev->num_tx + i, &bd);
324 bd.addr += ETHOC_BUFSIZ;
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000325
326 dev->vma[dev->num_tx + i] = vma;
327 vma += ETHOC_BUFSIZ;
Thierry Redinga1702852009-03-27 00:12:24 -0700328 }
329
330 return 0;
331}
332
333static int ethoc_reset(struct ethoc *dev)
334{
335 u32 mode;
336
337 /* TODO: reset controller? */
338
339 ethoc_disable_rx_and_tx(dev);
340
341 /* TODO: setup registers */
342
343 /* enable FCS generation and automatic padding */
344 mode = ethoc_read(dev, MODER);
345 mode |= MODER_CRC | MODER_PAD;
346 ethoc_write(dev, MODER, mode);
347
348 /* set full-duplex mode */
349 mode = ethoc_read(dev, MODER);
350 mode |= MODER_FULLD;
351 ethoc_write(dev, MODER, mode);
352 ethoc_write(dev, IPGT, 0x15);
353
354 ethoc_ack_irq(dev, INT_MASK_ALL);
355 ethoc_enable_irq(dev, INT_MASK_ALL);
356 ethoc_enable_rx_and_tx(dev);
357 return 0;
358}
359
360static unsigned int ethoc_update_rx_stats(struct ethoc *dev,
361 struct ethoc_bd *bd)
362{
363 struct net_device *netdev = dev->netdev;
364 unsigned int ret = 0;
365
366 if (bd->stat & RX_BD_TL) {
367 dev_err(&netdev->dev, "RX: frame too long\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000368 netdev->stats.rx_length_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700369 ret++;
370 }
371
372 if (bd->stat & RX_BD_SF) {
373 dev_err(&netdev->dev, "RX: frame too short\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000374 netdev->stats.rx_length_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700375 ret++;
376 }
377
378 if (bd->stat & RX_BD_DN) {
379 dev_err(&netdev->dev, "RX: dribble nibble\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000380 netdev->stats.rx_frame_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700381 }
382
383 if (bd->stat & RX_BD_CRC) {
384 dev_err(&netdev->dev, "RX: wrong CRC\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000385 netdev->stats.rx_crc_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700386 ret++;
387 }
388
389 if (bd->stat & RX_BD_OR) {
390 dev_err(&netdev->dev, "RX: overrun\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000391 netdev->stats.rx_over_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700392 ret++;
393 }
394
395 if (bd->stat & RX_BD_MISS)
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000396 netdev->stats.rx_missed_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700397
398 if (bd->stat & RX_BD_LC) {
399 dev_err(&netdev->dev, "RX: late collision\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000400 netdev->stats.collisions++;
Thierry Redinga1702852009-03-27 00:12:24 -0700401 ret++;
402 }
403
404 return ret;
405}
406
407static int ethoc_rx(struct net_device *dev, int limit)
408{
409 struct ethoc *priv = netdev_priv(dev);
410 int count;
411
412 for (count = 0; count < limit; ++count) {
413 unsigned int entry;
414 struct ethoc_bd bd;
415
Jonas Bonn6a632622010-11-25 02:30:32 +0000416 entry = priv->num_tx + priv->cur_rx;
Thierry Redinga1702852009-03-27 00:12:24 -0700417 ethoc_read_bd(priv, entry, &bd);
Jonas Bonn20f70dd2010-11-25 02:30:28 +0000418 if (bd.stat & RX_BD_EMPTY) {
419 ethoc_ack_irq(priv, INT_MASK_RX);
420 /* If packet (interrupt) came in between checking
421 * BD_EMTPY and clearing the interrupt source, then we
422 * risk missing the packet as the RX interrupt won't
423 * trigger right away when we reenable it; hence, check
424 * BD_EMTPY here again to make sure there isn't such a
425 * packet waiting for us...
426 */
427 ethoc_read_bd(priv, entry, &bd);
428 if (bd.stat & RX_BD_EMPTY)
429 break;
430 }
Thierry Redinga1702852009-03-27 00:12:24 -0700431
432 if (ethoc_update_rx_stats(priv, &bd) == 0) {
433 int size = bd.stat >> 16;
Eric Dumazet89d71a62009-10-13 05:34:20 +0000434 struct sk_buff *skb;
Thomas Chou050f91d2009-10-04 23:33:19 +0000435
436 size -= 4; /* strip the CRC */
Eric Dumazet89d71a62009-10-13 05:34:20 +0000437 skb = netdev_alloc_skb_ip_align(dev, size);
Thomas Chou050f91d2009-10-04 23:33:19 +0000438
Thierry Redinga1702852009-03-27 00:12:24 -0700439 if (likely(skb)) {
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000440 void *src = priv->vma[entry];
Thierry Redinga1702852009-03-27 00:12:24 -0700441 memcpy_fromio(skb_put(skb, size), src, size);
442 skb->protocol = eth_type_trans(skb, dev);
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000443 dev->stats.rx_packets++;
444 dev->stats.rx_bytes += size;
Thierry Redinga1702852009-03-27 00:12:24 -0700445 netif_receive_skb(skb);
446 } else {
447 if (net_ratelimit())
448 dev_warn(&dev->dev, "low on memory - "
449 "packet dropped\n");
450
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000451 dev->stats.rx_dropped++;
Thierry Redinga1702852009-03-27 00:12:24 -0700452 break;
453 }
454 }
455
456 /* clear the buffer descriptor so it can be reused */
457 bd.stat &= ~RX_BD_STATS;
458 bd.stat |= RX_BD_EMPTY;
459 ethoc_write_bd(priv, entry, &bd);
Jonas Bonn6a632622010-11-25 02:30:32 +0000460 if (++priv->cur_rx == priv->num_rx)
461 priv->cur_rx = 0;
Thierry Redinga1702852009-03-27 00:12:24 -0700462 }
463
464 return count;
465}
466
Jonas Bonn4f64bcb2010-11-25 02:30:31 +0000467static void ethoc_update_tx_stats(struct ethoc *dev, struct ethoc_bd *bd)
Thierry Redinga1702852009-03-27 00:12:24 -0700468{
469 struct net_device *netdev = dev->netdev;
470
471 if (bd->stat & TX_BD_LC) {
472 dev_err(&netdev->dev, "TX: late collision\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000473 netdev->stats.tx_window_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700474 }
475
476 if (bd->stat & TX_BD_RL) {
477 dev_err(&netdev->dev, "TX: retransmit limit\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000478 netdev->stats.tx_aborted_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700479 }
480
481 if (bd->stat & TX_BD_UR) {
482 dev_err(&netdev->dev, "TX: underrun\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000483 netdev->stats.tx_fifo_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700484 }
485
486 if (bd->stat & TX_BD_CS) {
487 dev_err(&netdev->dev, "TX: carrier sense lost\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000488 netdev->stats.tx_carrier_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700489 }
490
491 if (bd->stat & TX_BD_STATS)
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000492 netdev->stats.tx_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700493
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000494 netdev->stats.collisions += (bd->stat >> 4) & 0xf;
495 netdev->stats.tx_bytes += bd->stat >> 16;
496 netdev->stats.tx_packets++;
Thierry Redinga1702852009-03-27 00:12:24 -0700497}
498
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000499static int ethoc_tx(struct net_device *dev, int limit)
Thierry Redinga1702852009-03-27 00:12:24 -0700500{
501 struct ethoc *priv = netdev_priv(dev);
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000502 int count;
503 struct ethoc_bd bd;
Thierry Redinga1702852009-03-27 00:12:24 -0700504
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000505 for (count = 0; count < limit; ++count) {
506 unsigned int entry;
Thierry Redinga1702852009-03-27 00:12:24 -0700507
Jonas Bonn6a632622010-11-25 02:30:32 +0000508 entry = priv->dty_tx & (priv->num_tx-1);
Thierry Redinga1702852009-03-27 00:12:24 -0700509
510 ethoc_read_bd(priv, entry, &bd);
Thierry Redinga1702852009-03-27 00:12:24 -0700511
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000512 if (bd.stat & TX_BD_READY || (priv->dty_tx == priv->cur_tx)) {
513 ethoc_ack_irq(priv, INT_MASK_TX);
514 /* If interrupt came in between reading in the BD
515 * and clearing the interrupt source, then we risk
516 * missing the event as the TX interrupt won't trigger
517 * right away when we reenable it; hence, check
518 * BD_EMPTY here again to make sure there isn't such an
519 * event pending...
520 */
521 ethoc_read_bd(priv, entry, &bd);
522 if (bd.stat & TX_BD_READY ||
523 (priv->dty_tx == priv->cur_tx))
524 break;
525 }
526
Jonas Bonn4f64bcb2010-11-25 02:30:31 +0000527 ethoc_update_tx_stats(priv, &bd);
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000528 priv->dty_tx++;
Thierry Redinga1702852009-03-27 00:12:24 -0700529 }
530
531 if ((priv->cur_tx - priv->dty_tx) <= (priv->num_tx / 2))
532 netif_wake_queue(dev);
533
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000534 return count;
Thierry Redinga1702852009-03-27 00:12:24 -0700535}
536
537static irqreturn_t ethoc_interrupt(int irq, void *dev_id)
538{
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000539 struct net_device *dev = dev_id;
Thierry Redinga1702852009-03-27 00:12:24 -0700540 struct ethoc *priv = netdev_priv(dev);
541 u32 pending;
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000542 u32 mask;
Thierry Redinga1702852009-03-27 00:12:24 -0700543
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000544 /* Figure out what triggered the interrupt...
545 * The tricky bit here is that the interrupt source bits get
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300546 * set in INT_SOURCE for an event regardless of whether that
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000547 * event is masked or not. Thus, in order to figure out what
548 * triggered the interrupt, we need to remove the sources
549 * for all events that are currently masked. This behaviour
550 * is not particularly well documented but reasonable...
551 */
552 mask = ethoc_read(priv, INT_MASK);
Thierry Redinga1702852009-03-27 00:12:24 -0700553 pending = ethoc_read(priv, INT_SOURCE);
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000554 pending &= mask;
555
Thierry Redinga1702852009-03-27 00:12:24 -0700556 if (unlikely(pending == 0)) {
Thierry Redinga1702852009-03-27 00:12:24 -0700557 return IRQ_NONE;
558 }
559
Thomas Chou50c54a52009-10-07 14:16:43 +0000560 ethoc_ack_irq(priv, pending);
Thierry Redinga1702852009-03-27 00:12:24 -0700561
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000562 /* We always handle the dropped packet interrupt */
Thierry Redinga1702852009-03-27 00:12:24 -0700563 if (pending & INT_MASK_BUSY) {
564 dev_err(&dev->dev, "packet dropped\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000565 dev->stats.rx_dropped++;
Thierry Redinga1702852009-03-27 00:12:24 -0700566 }
567
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000568 /* Handle receive/transmit event by switching to polling */
569 if (pending & (INT_MASK_TX | INT_MASK_RX)) {
570 ethoc_disable_irq(priv, INT_MASK_TX | INT_MASK_RX);
571 napi_schedule(&priv->napi);
Thierry Redinga1702852009-03-27 00:12:24 -0700572 }
573
Thierry Redinga1702852009-03-27 00:12:24 -0700574 return IRQ_HANDLED;
575}
576
577static int ethoc_get_mac_address(struct net_device *dev, void *addr)
578{
579 struct ethoc *priv = netdev_priv(dev);
580 u8 *mac = (u8 *)addr;
581 u32 reg;
582
583 reg = ethoc_read(priv, MAC_ADDR0);
584 mac[2] = (reg >> 24) & 0xff;
585 mac[3] = (reg >> 16) & 0xff;
586 mac[4] = (reg >> 8) & 0xff;
587 mac[5] = (reg >> 0) & 0xff;
588
589 reg = ethoc_read(priv, MAC_ADDR1);
590 mac[0] = (reg >> 8) & 0xff;
591 mac[1] = (reg >> 0) & 0xff;
592
593 return 0;
594}
595
596static int ethoc_poll(struct napi_struct *napi, int budget)
597{
598 struct ethoc *priv = container_of(napi, struct ethoc, napi);
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000599 int rx_work_done = 0;
600 int tx_work_done = 0;
Thierry Redinga1702852009-03-27 00:12:24 -0700601
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000602 rx_work_done = ethoc_rx(priv->netdev, budget);
603 tx_work_done = ethoc_tx(priv->netdev, budget);
604
605 if (rx_work_done < budget && tx_work_done < budget) {
Thierry Redinga1702852009-03-27 00:12:24 -0700606 napi_complete(napi);
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000607 ethoc_enable_irq(priv, INT_MASK_TX | INT_MASK_RX);
Thierry Redinga1702852009-03-27 00:12:24 -0700608 }
609
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000610 return rx_work_done;
Thierry Redinga1702852009-03-27 00:12:24 -0700611}
612
613static int ethoc_mdio_read(struct mii_bus *bus, int phy, int reg)
614{
Thierry Redinga1702852009-03-27 00:12:24 -0700615 struct ethoc *priv = bus->priv;
Jonas Bonn8dac4282010-11-25 02:30:30 +0000616 int i;
Thierry Redinga1702852009-03-27 00:12:24 -0700617
618 ethoc_write(priv, MIIADDRESS, MIIADDRESS_ADDR(phy, reg));
619 ethoc_write(priv, MIICOMMAND, MIICOMMAND_READ);
620
Jonas Bonn8dac4282010-11-25 02:30:30 +0000621 for (i=0; i < 5; i++) {
Thierry Redinga1702852009-03-27 00:12:24 -0700622 u32 status = ethoc_read(priv, MIISTATUS);
623 if (!(status & MIISTATUS_BUSY)) {
624 u32 data = ethoc_read(priv, MIIRX_DATA);
625 /* reset MII command register */
626 ethoc_write(priv, MIICOMMAND, 0);
627 return data;
628 }
Jonas Bonn8dac4282010-11-25 02:30:30 +0000629 usleep_range(100,200);
Thierry Redinga1702852009-03-27 00:12:24 -0700630 }
631
632 return -EBUSY;
633}
634
635static int ethoc_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
636{
Thierry Redinga1702852009-03-27 00:12:24 -0700637 struct ethoc *priv = bus->priv;
Jonas Bonn8dac4282010-11-25 02:30:30 +0000638 int i;
Thierry Redinga1702852009-03-27 00:12:24 -0700639
640 ethoc_write(priv, MIIADDRESS, MIIADDRESS_ADDR(phy, reg));
641 ethoc_write(priv, MIITX_DATA, val);
642 ethoc_write(priv, MIICOMMAND, MIICOMMAND_WRITE);
643
Jonas Bonn8dac4282010-11-25 02:30:30 +0000644 for (i=0; i < 5; i++) {
Thierry Redinga1702852009-03-27 00:12:24 -0700645 u32 stat = ethoc_read(priv, MIISTATUS);
Jonas Bonnb46773d2010-06-11 02:47:39 +0000646 if (!(stat & MIISTATUS_BUSY)) {
647 /* reset MII command register */
648 ethoc_write(priv, MIICOMMAND, 0);
Thierry Redinga1702852009-03-27 00:12:24 -0700649 return 0;
Jonas Bonnb46773d2010-06-11 02:47:39 +0000650 }
Jonas Bonn8dac4282010-11-25 02:30:30 +0000651 usleep_range(100,200);
Thierry Redinga1702852009-03-27 00:12:24 -0700652 }
653
654 return -EBUSY;
655}
656
657static int ethoc_mdio_reset(struct mii_bus *bus)
658{
659 return 0;
660}
661
662static void ethoc_mdio_poll(struct net_device *dev)
663{
664}
665
Jonas Bonnf78f09f2010-07-26 18:45:05 -0700666static int __devinit ethoc_mdio_probe(struct net_device *dev)
Thierry Redinga1702852009-03-27 00:12:24 -0700667{
668 struct ethoc *priv = netdev_priv(dev);
669 struct phy_device *phy;
Jonas Bonn637f33b82010-06-11 02:47:37 +0000670 int err;
Thierry Redinga1702852009-03-27 00:12:24 -0700671
Jonas Bonn637f33b82010-06-11 02:47:37 +0000672 if (priv->phy_id != -1) {
673 phy = priv->mdio->phy_map[priv->phy_id];
674 } else {
675 phy = phy_find_first(priv->mdio);
Thierry Redinga1702852009-03-27 00:12:24 -0700676 }
677
678 if (!phy) {
679 dev_err(&dev->dev, "no PHY found\n");
680 return -ENXIO;
681 }
682
Jonas Bonn637f33b82010-06-11 02:47:37 +0000683 err = phy_connect_direct(dev, phy, ethoc_mdio_poll, 0,
Thierry Redinga1702852009-03-27 00:12:24 -0700684 PHY_INTERFACE_MODE_GMII);
Jonas Bonn637f33b82010-06-11 02:47:37 +0000685 if (err) {
Thierry Redinga1702852009-03-27 00:12:24 -0700686 dev_err(&dev->dev, "could not attach to PHY\n");
Jonas Bonn637f33b82010-06-11 02:47:37 +0000687 return err;
Thierry Redinga1702852009-03-27 00:12:24 -0700688 }
689
690 priv->phy = phy;
691 return 0;
692}
693
694static int ethoc_open(struct net_device *dev)
695{
696 struct ethoc *priv = netdev_priv(dev);
Thierry Redinga1702852009-03-27 00:12:24 -0700697 int ret;
698
699 ret = request_irq(dev->irq, ethoc_interrupt, IRQF_SHARED,
700 dev->name, dev);
701 if (ret)
702 return ret;
703
David S. Miller5cf3e032010-07-07 18:23:19 -0700704 ethoc_init_ring(priv, dev->mem_start);
Thierry Redinga1702852009-03-27 00:12:24 -0700705 ethoc_reset(priv);
706
707 if (netif_queue_stopped(dev)) {
708 dev_dbg(&dev->dev, " resuming queue\n");
709 netif_wake_queue(dev);
710 } else {
711 dev_dbg(&dev->dev, " starting queue\n");
712 netif_start_queue(dev);
713 }
714
715 phy_start(priv->phy);
716 napi_enable(&priv->napi);
717
718 if (netif_msg_ifup(priv)) {
719 dev_info(&dev->dev, "I/O: %08lx Memory: %08lx-%08lx\n",
720 dev->base_addr, dev->mem_start, dev->mem_end);
721 }
722
723 return 0;
724}
725
726static int ethoc_stop(struct net_device *dev)
727{
728 struct ethoc *priv = netdev_priv(dev);
729
730 napi_disable(&priv->napi);
731
732 if (priv->phy)
733 phy_stop(priv->phy);
734
735 ethoc_disable_rx_and_tx(priv);
736 free_irq(dev->irq, dev);
737
738 if (!netif_queue_stopped(dev))
739 netif_stop_queue(dev);
740
741 return 0;
742}
743
744static int ethoc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
745{
746 struct ethoc *priv = netdev_priv(dev);
747 struct mii_ioctl_data *mdio = if_mii(ifr);
748 struct phy_device *phy = NULL;
749
750 if (!netif_running(dev))
751 return -EINVAL;
752
753 if (cmd != SIOCGMIIPHY) {
754 if (mdio->phy_id >= PHY_MAX_ADDR)
755 return -ERANGE;
756
757 phy = priv->mdio->phy_map[mdio->phy_id];
758 if (!phy)
759 return -ENODEV;
760 } else {
761 phy = priv->phy;
762 }
763
Richard Cochran28b04112010-07-17 08:48:55 +0000764 return phy_mii_ioctl(phy, ifr, cmd);
Thierry Redinga1702852009-03-27 00:12:24 -0700765}
766
767static int ethoc_config(struct net_device *dev, struct ifmap *map)
768{
769 return -ENOSYS;
770}
771
772static int ethoc_set_mac_address(struct net_device *dev, void *addr)
773{
774 struct ethoc *priv = netdev_priv(dev);
775 u8 *mac = (u8 *)addr;
776
777 ethoc_write(priv, MAC_ADDR0, (mac[2] << 24) | (mac[3] << 16) |
778 (mac[4] << 8) | (mac[5] << 0));
779 ethoc_write(priv, MAC_ADDR1, (mac[0] << 8) | (mac[1] << 0));
780
781 return 0;
782}
783
784static void ethoc_set_multicast_list(struct net_device *dev)
785{
786 struct ethoc *priv = netdev_priv(dev);
787 u32 mode = ethoc_read(priv, MODER);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000788 struct netdev_hw_addr *ha;
Thierry Redinga1702852009-03-27 00:12:24 -0700789 u32 hash[2] = { 0, 0 };
790
791 /* set loopback mode if requested */
792 if (dev->flags & IFF_LOOPBACK)
793 mode |= MODER_LOOP;
794 else
795 mode &= ~MODER_LOOP;
796
797 /* receive broadcast frames if requested */
798 if (dev->flags & IFF_BROADCAST)
799 mode &= ~MODER_BRO;
800 else
801 mode |= MODER_BRO;
802
803 /* enable promiscuous mode if requested */
804 if (dev->flags & IFF_PROMISC)
805 mode |= MODER_PRO;
806 else
807 mode &= ~MODER_PRO;
808
809 ethoc_write(priv, MODER, mode);
810
811 /* receive multicast frames */
812 if (dev->flags & IFF_ALLMULTI) {
813 hash[0] = 0xffffffff;
814 hash[1] = 0xffffffff;
815 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000816 netdev_for_each_mc_addr(ha, dev) {
817 u32 crc = ether_crc(ETH_ALEN, ha->addr);
Thierry Redinga1702852009-03-27 00:12:24 -0700818 int bit = (crc >> 26) & 0x3f;
819 hash[bit >> 5] |= 1 << (bit & 0x1f);
820 }
821 }
822
823 ethoc_write(priv, ETH_HASH0, hash[0]);
824 ethoc_write(priv, ETH_HASH1, hash[1]);
825}
826
827static int ethoc_change_mtu(struct net_device *dev, int new_mtu)
828{
829 return -ENOSYS;
830}
831
832static void ethoc_tx_timeout(struct net_device *dev)
833{
834 struct ethoc *priv = netdev_priv(dev);
835 u32 pending = ethoc_read(priv, INT_SOURCE);
836 if (likely(pending))
837 ethoc_interrupt(dev->irq, dev);
838}
839
Stephen Hemminger613573252009-08-31 19:50:58 +0000840static netdev_tx_t ethoc_start_xmit(struct sk_buff *skb, struct net_device *dev)
Thierry Redinga1702852009-03-27 00:12:24 -0700841{
842 struct ethoc *priv = netdev_priv(dev);
843 struct ethoc_bd bd;
844 unsigned int entry;
845 void *dest;
846
847 if (unlikely(skb->len > ETHOC_BUFSIZ)) {
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000848 dev->stats.tx_errors++;
Patrick McHardy3790c8c2009-06-12 03:00:35 +0000849 goto out;
Thierry Redinga1702852009-03-27 00:12:24 -0700850 }
851
852 entry = priv->cur_tx % priv->num_tx;
853 spin_lock_irq(&priv->lock);
854 priv->cur_tx++;
855
856 ethoc_read_bd(priv, entry, &bd);
857 if (unlikely(skb->len < ETHOC_ZLEN))
858 bd.stat |= TX_BD_PAD;
859 else
860 bd.stat &= ~TX_BD_PAD;
861
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000862 dest = priv->vma[entry];
Thierry Redinga1702852009-03-27 00:12:24 -0700863 memcpy_toio(dest, skb->data, skb->len);
864
865 bd.stat &= ~(TX_BD_STATS | TX_BD_LEN_MASK);
866 bd.stat |= TX_BD_LEN(skb->len);
867 ethoc_write_bd(priv, entry, &bd);
868
869 bd.stat |= TX_BD_READY;
870 ethoc_write_bd(priv, entry, &bd);
871
872 if (priv->cur_tx == (priv->dty_tx + priv->num_tx)) {
873 dev_dbg(&dev->dev, "stopping queue\n");
874 netif_stop_queue(dev);
875 }
876
Thierry Redinga1702852009-03-27 00:12:24 -0700877 spin_unlock_irq(&priv->lock);
Richard Cochran68f51392011-06-12 02:19:04 +0000878 skb_tx_timestamp(skb);
Patrick McHardy3790c8c2009-06-12 03:00:35 +0000879out:
880 dev_kfree_skb(skb);
Thierry Redinga1702852009-03-27 00:12:24 -0700881 return NETDEV_TX_OK;
882}
883
884static const struct net_device_ops ethoc_netdev_ops = {
885 .ndo_open = ethoc_open,
886 .ndo_stop = ethoc_stop,
887 .ndo_do_ioctl = ethoc_ioctl,
888 .ndo_set_config = ethoc_config,
889 .ndo_set_mac_address = ethoc_set_mac_address,
890 .ndo_set_multicast_list = ethoc_set_multicast_list,
891 .ndo_change_mtu = ethoc_change_mtu,
892 .ndo_tx_timeout = ethoc_tx_timeout,
Thierry Redinga1702852009-03-27 00:12:24 -0700893 .ndo_start_xmit = ethoc_start_xmit,
894};
895
896/**
897 * ethoc_probe() - initialize OpenCores ethernet MAC
898 * pdev: platform device
899 */
Jonas Bonnf78f09f2010-07-26 18:45:05 -0700900static int __devinit ethoc_probe(struct platform_device *pdev)
Thierry Redinga1702852009-03-27 00:12:24 -0700901{
902 struct net_device *netdev = NULL;
903 struct resource *res = NULL;
904 struct resource *mmio = NULL;
905 struct resource *mem = NULL;
906 struct ethoc *priv = NULL;
907 unsigned int phy;
Jonas Bonnc527f812010-06-11 02:47:34 +0000908 int num_bd;
Thierry Redinga1702852009-03-27 00:12:24 -0700909 int ret = 0;
910
911 /* allocate networking device */
912 netdev = alloc_etherdev(sizeof(struct ethoc));
913 if (!netdev) {
914 dev_err(&pdev->dev, "cannot allocate network device\n");
915 ret = -ENOMEM;
916 goto out;
917 }
918
919 SET_NETDEV_DEV(netdev, &pdev->dev);
920 platform_set_drvdata(pdev, netdev);
921
922 /* obtain I/O memory space */
923 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
924 if (!res) {
925 dev_err(&pdev->dev, "cannot obtain I/O memory space\n");
926 ret = -ENXIO;
927 goto free;
928 }
929
930 mmio = devm_request_mem_region(&pdev->dev, res->start,
Tobias Klauserd8645842010-01-15 01:48:22 -0800931 resource_size(res), res->name);
Julia Lawall463889e2009-07-27 06:13:30 +0000932 if (!mmio) {
Thierry Redinga1702852009-03-27 00:12:24 -0700933 dev_err(&pdev->dev, "cannot request I/O memory space\n");
934 ret = -ENXIO;
935 goto free;
936 }
937
938 netdev->base_addr = mmio->start;
939
940 /* obtain buffer memory space */
941 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Thomas Chou0baa0802009-10-04 23:33:20 +0000942 if (res) {
943 mem = devm_request_mem_region(&pdev->dev, res->start,
Tobias Klauserd8645842010-01-15 01:48:22 -0800944 resource_size(res), res->name);
Thomas Chou0baa0802009-10-04 23:33:20 +0000945 if (!mem) {
946 dev_err(&pdev->dev, "cannot request memory space\n");
947 ret = -ENXIO;
948 goto free;
949 }
950
951 netdev->mem_start = mem->start;
952 netdev->mem_end = mem->end;
Thierry Redinga1702852009-03-27 00:12:24 -0700953 }
954
Thierry Redinga1702852009-03-27 00:12:24 -0700955
956 /* obtain device IRQ number */
957 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
958 if (!res) {
959 dev_err(&pdev->dev, "cannot obtain IRQ\n");
960 ret = -ENXIO;
961 goto free;
962 }
963
964 netdev->irq = res->start;
965
966 /* setup driver-private data */
967 priv = netdev_priv(netdev);
968 priv->netdev = netdev;
Thomas Chou0baa0802009-10-04 23:33:20 +0000969 priv->dma_alloc = 0;
Thomas Chouee02a4e2010-05-23 16:44:02 +0000970 priv->io_region_size = mmio->end - mmio->start + 1;
Thierry Redinga1702852009-03-27 00:12:24 -0700971
972 priv->iobase = devm_ioremap_nocache(&pdev->dev, netdev->base_addr,
Tobias Klauserd8645842010-01-15 01:48:22 -0800973 resource_size(mmio));
Thierry Redinga1702852009-03-27 00:12:24 -0700974 if (!priv->iobase) {
975 dev_err(&pdev->dev, "cannot remap I/O memory space\n");
976 ret = -ENXIO;
977 goto error;
978 }
979
Thomas Chou0baa0802009-10-04 23:33:20 +0000980 if (netdev->mem_end) {
981 priv->membase = devm_ioremap_nocache(&pdev->dev,
Tobias Klauserd8645842010-01-15 01:48:22 -0800982 netdev->mem_start, resource_size(mem));
Thomas Chou0baa0802009-10-04 23:33:20 +0000983 if (!priv->membase) {
984 dev_err(&pdev->dev, "cannot remap memory space\n");
985 ret = -ENXIO;
986 goto error;
987 }
988 } else {
989 /* Allocate buffer memory */
Jonas Bonna71fba92010-06-11 02:47:40 +0000990 priv->membase = dmam_alloc_coherent(&pdev->dev,
Thomas Chou0baa0802009-10-04 23:33:20 +0000991 buffer_size, (void *)&netdev->mem_start,
992 GFP_KERNEL);
993 if (!priv->membase) {
994 dev_err(&pdev->dev, "cannot allocate %dB buffer\n",
995 buffer_size);
996 ret = -ENOMEM;
997 goto error;
998 }
999 netdev->mem_end = netdev->mem_start + buffer_size;
1000 priv->dma_alloc = buffer_size;
Thierry Redinga1702852009-03-27 00:12:24 -07001001 }
1002
Jonas Bonnc527f812010-06-11 02:47:34 +00001003 /* calculate the number of TX/RX buffers, maximum 128 supported */
1004 num_bd = min_t(unsigned int,
1005 128, (netdev->mem_end - netdev->mem_start + 1) / ETHOC_BUFSIZ);
Jonas Bonn6a632622010-11-25 02:30:32 +00001006 if (num_bd < 4) {
1007 ret = -ENODEV;
1008 goto error;
1009 }
1010 /* num_tx must be a power of two */
1011 priv->num_tx = rounddown_pow_of_two(num_bd >> 1);
Jonas Bonnc527f812010-06-11 02:47:34 +00001012 priv->num_rx = num_bd - priv->num_tx;
1013
Jonas Bonn6a632622010-11-25 02:30:32 +00001014 dev_dbg(&pdev->dev, "ethoc: num_tx: %d num_rx: %d\n",
1015 priv->num_tx, priv->num_rx);
1016
Jonas Bonnf8555ad02010-06-11 02:47:35 +00001017 priv->vma = devm_kzalloc(&pdev->dev, num_bd*sizeof(void*), GFP_KERNEL);
1018 if (!priv->vma) {
1019 ret = -ENOMEM;
1020 goto error;
1021 }
1022
Thierry Redinga1702852009-03-27 00:12:24 -07001023 /* Allow the platform setup code to pass in a MAC address. */
1024 if (pdev->dev.platform_data) {
Jonas Bonne0f42582010-11-25 02:30:25 +00001025 struct ethoc_platform_data *pdata = pdev->dev.platform_data;
Thierry Redinga1702852009-03-27 00:12:24 -07001026 memcpy(netdev->dev_addr, pdata->hwaddr, IFHWADDRLEN);
1027 priv->phy_id = pdata->phy_id;
Jonas Bonne0f42582010-11-25 02:30:25 +00001028 } else {
1029 priv->phy_id = -1;
1030
1031#ifdef CONFIG_OF
1032 {
1033 const uint8_t* mac;
1034
1035 mac = of_get_property(pdev->dev.of_node,
1036 "local-mac-address",
1037 NULL);
1038 if (mac)
1039 memcpy(netdev->dev_addr, mac, IFHWADDRLEN);
1040 }
1041#endif
Thierry Redinga1702852009-03-27 00:12:24 -07001042 }
1043
1044 /* Check that the given MAC address is valid. If it isn't, read the
1045 * current MAC from the controller. */
1046 if (!is_valid_ether_addr(netdev->dev_addr))
1047 ethoc_get_mac_address(netdev, netdev->dev_addr);
1048
1049 /* Check the MAC again for validity, if it still isn't choose and
1050 * program a random one. */
1051 if (!is_valid_ether_addr(netdev->dev_addr))
1052 random_ether_addr(netdev->dev_addr);
1053
1054 ethoc_set_mac_address(netdev, netdev->dev_addr);
1055
1056 /* register MII bus */
1057 priv->mdio = mdiobus_alloc();
1058 if (!priv->mdio) {
1059 ret = -ENOMEM;
1060 goto free;
1061 }
1062
1063 priv->mdio->name = "ethoc-mdio";
1064 snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "%s-%d",
1065 priv->mdio->name, pdev->id);
1066 priv->mdio->read = ethoc_mdio_read;
1067 priv->mdio->write = ethoc_mdio_write;
1068 priv->mdio->reset = ethoc_mdio_reset;
1069 priv->mdio->priv = priv;
1070
1071 priv->mdio->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1072 if (!priv->mdio->irq) {
1073 ret = -ENOMEM;
1074 goto free_mdio;
1075 }
1076
1077 for (phy = 0; phy < PHY_MAX_ADDR; phy++)
1078 priv->mdio->irq[phy] = PHY_POLL;
1079
1080 ret = mdiobus_register(priv->mdio);
1081 if (ret) {
1082 dev_err(&netdev->dev, "failed to register MDIO bus\n");
1083 goto free_mdio;
1084 }
1085
1086 ret = ethoc_mdio_probe(netdev);
1087 if (ret) {
1088 dev_err(&netdev->dev, "failed to probe MDIO bus\n");
1089 goto error;
1090 }
1091
1092 ether_setup(netdev);
1093
1094 /* setup the net_device structure */
1095 netdev->netdev_ops = &ethoc_netdev_ops;
1096 netdev->watchdog_timeo = ETHOC_TIMEOUT;
1097 netdev->features |= 0;
1098
1099 /* setup NAPI */
Thierry Redinga1702852009-03-27 00:12:24 -07001100 netif_napi_add(netdev, &priv->napi, ethoc_poll, 64);
1101
Thierry Redinga1702852009-03-27 00:12:24 -07001102 spin_lock_init(&priv->lock);
1103
1104 ret = register_netdev(netdev);
1105 if (ret < 0) {
1106 dev_err(&netdev->dev, "failed to register interface\n");
Thomas Chouee02a4e2010-05-23 16:44:02 +00001107 goto error2;
Thierry Redinga1702852009-03-27 00:12:24 -07001108 }
1109
1110 goto out;
1111
Thomas Chouee02a4e2010-05-23 16:44:02 +00001112error2:
1113 netif_napi_del(&priv->napi);
Thierry Redinga1702852009-03-27 00:12:24 -07001114error:
1115 mdiobus_unregister(priv->mdio);
1116free_mdio:
1117 kfree(priv->mdio->irq);
1118 mdiobus_free(priv->mdio);
1119free:
1120 free_netdev(netdev);
1121out:
1122 return ret;
1123}
1124
1125/**
1126 * ethoc_remove() - shutdown OpenCores ethernet MAC
1127 * @pdev: platform device
1128 */
Jonas Bonnf78f09f2010-07-26 18:45:05 -07001129static int __devexit ethoc_remove(struct platform_device *pdev)
Thierry Redinga1702852009-03-27 00:12:24 -07001130{
1131 struct net_device *netdev = platform_get_drvdata(pdev);
1132 struct ethoc *priv = netdev_priv(netdev);
1133
1134 platform_set_drvdata(pdev, NULL);
1135
1136 if (netdev) {
Thomas Chouee02a4e2010-05-23 16:44:02 +00001137 netif_napi_del(&priv->napi);
Thierry Redinga1702852009-03-27 00:12:24 -07001138 phy_disconnect(priv->phy);
1139 priv->phy = NULL;
1140
1141 if (priv->mdio) {
1142 mdiobus_unregister(priv->mdio);
1143 kfree(priv->mdio->irq);
1144 mdiobus_free(priv->mdio);
1145 }
Thierry Redinga1702852009-03-27 00:12:24 -07001146 unregister_netdev(netdev);
1147 free_netdev(netdev);
1148 }
1149
1150 return 0;
1151}
1152
1153#ifdef CONFIG_PM
1154static int ethoc_suspend(struct platform_device *pdev, pm_message_t state)
1155{
1156 return -ENOSYS;
1157}
1158
1159static int ethoc_resume(struct platform_device *pdev)
1160{
1161 return -ENOSYS;
1162}
1163#else
1164# define ethoc_suspend NULL
1165# define ethoc_resume NULL
1166#endif
1167
Jonas Bonne0f42582010-11-25 02:30:25 +00001168static struct of_device_id ethoc_match[] = {
Grant Likelyc9e358d2011-01-21 09:24:48 -07001169 { .compatible = "opencores,ethoc", },
Jonas Bonne0f42582010-11-25 02:30:25 +00001170 {},
1171};
1172MODULE_DEVICE_TABLE(of, ethoc_match);
Jonas Bonne0f42582010-11-25 02:30:25 +00001173
Thierry Redinga1702852009-03-27 00:12:24 -07001174static struct platform_driver ethoc_driver = {
1175 .probe = ethoc_probe,
Jonas Bonnf78f09f2010-07-26 18:45:05 -07001176 .remove = __devexit_p(ethoc_remove),
Thierry Redinga1702852009-03-27 00:12:24 -07001177 .suspend = ethoc_suspend,
1178 .resume = ethoc_resume,
1179 .driver = {
1180 .name = "ethoc",
Jonas Bonne0f42582010-11-25 02:30:25 +00001181 .owner = THIS_MODULE,
Jonas Bonne0f42582010-11-25 02:30:25 +00001182 .of_match_table = ethoc_match,
Thierry Redinga1702852009-03-27 00:12:24 -07001183 },
1184};
1185
1186static int __init ethoc_init(void)
1187{
1188 return platform_driver_register(&ethoc_driver);
1189}
1190
1191static void __exit ethoc_exit(void)
1192{
1193 platform_driver_unregister(&ethoc_driver);
1194}
1195
1196module_init(ethoc_init);
1197module_exit(ethoc_exit);
1198
1199MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
1200MODULE_DESCRIPTION("OpenCores Ethernet MAC driver");
1201MODULE_LICENSE("GPL v2");
1202