blob: 93b50d674b1edd51645add0ec022aba8068823f1 [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>
16#include <linux/io.h>
17#include <linux/mii.h>
18#include <linux/phy.h>
19#include <linux/platform_device.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040020#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Jonas Bonne0f42582010-11-25 02:30:25 +000022#include <linux/of.h>
Thierry Redinga1702852009-03-27 00:12:24 -070023#include <net/ethoc.h>
24
Thomas Chou0baa0802009-10-04 23:33:20 +000025static int buffer_size = 0x8000; /* 32 KBytes */
26module_param(buffer_size, int, 0);
27MODULE_PARM_DESC(buffer_size, "DMA buffer allocation size");
28
Thierry Redinga1702852009-03-27 00:12:24 -070029/* register offsets */
30#define MODER 0x00
31#define INT_SOURCE 0x04
32#define INT_MASK 0x08
33#define IPGT 0x0c
34#define IPGR1 0x10
35#define IPGR2 0x14
36#define PACKETLEN 0x18
37#define COLLCONF 0x1c
38#define TX_BD_NUM 0x20
39#define CTRLMODER 0x24
40#define MIIMODER 0x28
41#define MIICOMMAND 0x2c
42#define MIIADDRESS 0x30
43#define MIITX_DATA 0x34
44#define MIIRX_DATA 0x38
45#define MIISTATUS 0x3c
46#define MAC_ADDR0 0x40
47#define MAC_ADDR1 0x44
48#define ETH_HASH0 0x48
49#define ETH_HASH1 0x4c
50#define ETH_TXCTRL 0x50
51
52/* mode register */
53#define MODER_RXEN (1 << 0) /* receive enable */
54#define MODER_TXEN (1 << 1) /* transmit enable */
55#define MODER_NOPRE (1 << 2) /* no preamble */
56#define MODER_BRO (1 << 3) /* broadcast address */
57#define MODER_IAM (1 << 4) /* individual address mode */
58#define MODER_PRO (1 << 5) /* promiscuous mode */
59#define MODER_IFG (1 << 6) /* interframe gap for incoming frames */
60#define MODER_LOOP (1 << 7) /* loopback */
61#define MODER_NBO (1 << 8) /* no back-off */
62#define MODER_EDE (1 << 9) /* excess defer enable */
63#define MODER_FULLD (1 << 10) /* full duplex */
64#define MODER_RESET (1 << 11) /* FIXME: reset (undocumented) */
65#define MODER_DCRC (1 << 12) /* delayed CRC enable */
66#define MODER_CRC (1 << 13) /* CRC enable */
67#define MODER_HUGE (1 << 14) /* huge packets enable */
68#define MODER_PAD (1 << 15) /* padding enabled */
69#define MODER_RSM (1 << 16) /* receive small packets */
70
71/* interrupt source and mask registers */
72#define INT_MASK_TXF (1 << 0) /* transmit frame */
73#define INT_MASK_TXE (1 << 1) /* transmit error */
74#define INT_MASK_RXF (1 << 2) /* receive frame */
75#define INT_MASK_RXE (1 << 3) /* receive error */
76#define INT_MASK_BUSY (1 << 4)
77#define INT_MASK_TXC (1 << 5) /* transmit control frame */
78#define INT_MASK_RXC (1 << 6) /* receive control frame */
79
80#define INT_MASK_TX (INT_MASK_TXF | INT_MASK_TXE)
81#define INT_MASK_RX (INT_MASK_RXF | INT_MASK_RXE)
82
83#define INT_MASK_ALL ( \
84 INT_MASK_TXF | INT_MASK_TXE | \
85 INT_MASK_RXF | INT_MASK_RXE | \
86 INT_MASK_TXC | INT_MASK_RXC | \
87 INT_MASK_BUSY \
88 )
89
90/* packet length register */
91#define PACKETLEN_MIN(min) (((min) & 0xffff) << 16)
92#define PACKETLEN_MAX(max) (((max) & 0xffff) << 0)
93#define PACKETLEN_MIN_MAX(min, max) (PACKETLEN_MIN(min) | \
94 PACKETLEN_MAX(max))
95
96/* transmit buffer number register */
97#define TX_BD_NUM_VAL(x) (((x) <= 0x80) ? (x) : 0x80)
98
99/* control module mode register */
100#define CTRLMODER_PASSALL (1 << 0) /* pass all receive frames */
101#define CTRLMODER_RXFLOW (1 << 1) /* receive control flow */
102#define CTRLMODER_TXFLOW (1 << 2) /* transmit control flow */
103
104/* MII mode register */
105#define MIIMODER_CLKDIV(x) ((x) & 0xfe) /* needs to be an even number */
106#define MIIMODER_NOPRE (1 << 8) /* no preamble */
107
108/* MII command register */
109#define MIICOMMAND_SCAN (1 << 0) /* scan status */
110#define MIICOMMAND_READ (1 << 1) /* read status */
111#define MIICOMMAND_WRITE (1 << 2) /* write control data */
112
113/* MII address register */
114#define MIIADDRESS_FIAD(x) (((x) & 0x1f) << 0)
115#define MIIADDRESS_RGAD(x) (((x) & 0x1f) << 8)
116#define MIIADDRESS_ADDR(phy, reg) (MIIADDRESS_FIAD(phy) | \
117 MIIADDRESS_RGAD(reg))
118
119/* MII transmit data register */
120#define MIITX_DATA_VAL(x) ((x) & 0xffff)
121
122/* MII receive data register */
123#define MIIRX_DATA_VAL(x) ((x) & 0xffff)
124
125/* MII status register */
126#define MIISTATUS_LINKFAIL (1 << 0)
127#define MIISTATUS_BUSY (1 << 1)
128#define MIISTATUS_INVALID (1 << 2)
129
130/* TX buffer descriptor */
131#define TX_BD_CS (1 << 0) /* carrier sense lost */
132#define TX_BD_DF (1 << 1) /* defer indication */
133#define TX_BD_LC (1 << 2) /* late collision */
134#define TX_BD_RL (1 << 3) /* retransmission limit */
135#define TX_BD_RETRY_MASK (0x00f0)
136#define TX_BD_RETRY(x) (((x) & 0x00f0) >> 4)
137#define TX_BD_UR (1 << 8) /* transmitter underrun */
138#define TX_BD_CRC (1 << 11) /* TX CRC enable */
139#define TX_BD_PAD (1 << 12) /* pad enable for short packets */
140#define TX_BD_WRAP (1 << 13)
141#define TX_BD_IRQ (1 << 14) /* interrupt request enable */
142#define TX_BD_READY (1 << 15) /* TX buffer ready */
143#define TX_BD_LEN(x) (((x) & 0xffff) << 16)
144#define TX_BD_LEN_MASK (0xffff << 16)
145
146#define TX_BD_STATS (TX_BD_CS | TX_BD_DF | TX_BD_LC | \
147 TX_BD_RL | TX_BD_RETRY_MASK | TX_BD_UR)
148
149/* RX buffer descriptor */
150#define RX_BD_LC (1 << 0) /* late collision */
151#define RX_BD_CRC (1 << 1) /* RX CRC error */
152#define RX_BD_SF (1 << 2) /* short frame */
153#define RX_BD_TL (1 << 3) /* too long */
154#define RX_BD_DN (1 << 4) /* dribble nibble */
155#define RX_BD_IS (1 << 5) /* invalid symbol */
156#define RX_BD_OR (1 << 6) /* receiver overrun */
157#define RX_BD_MISS (1 << 7)
158#define RX_BD_CF (1 << 8) /* control frame */
159#define RX_BD_WRAP (1 << 13)
160#define RX_BD_IRQ (1 << 14) /* interrupt request enable */
161#define RX_BD_EMPTY (1 << 15)
162#define RX_BD_LEN(x) (((x) & 0xffff) << 16)
163
164#define RX_BD_STATS (RX_BD_LC | RX_BD_CRC | RX_BD_SF | RX_BD_TL | \
165 RX_BD_DN | RX_BD_IS | RX_BD_OR | RX_BD_MISS)
166
167#define ETHOC_BUFSIZ 1536
168#define ETHOC_ZLEN 64
169#define ETHOC_BD_BASE 0x400
170#define ETHOC_TIMEOUT (HZ / 2)
171#define ETHOC_MII_TIMEOUT (1 + (HZ / 5))
172
173/**
174 * struct ethoc - driver-private device structure
175 * @iobase: pointer to I/O memory region
176 * @membase: pointer to buffer memory region
Thomas Chou0baa0802009-10-04 23:33:20 +0000177 * @dma_alloc: dma allocated buffer size
Thomas Chouee02a4e2010-05-23 16:44:02 +0000178 * @io_region_size: I/O memory region size
Thierry Redinga1702852009-03-27 00:12:24 -0700179 * @num_tx: number of send buffers
180 * @cur_tx: last send buffer written
181 * @dty_tx: last buffer actually sent
182 * @num_rx: number of receive buffers
183 * @cur_rx: current receive buffer
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000184 * @vma: pointer to array of virtual memory addresses for buffers
Thierry Redinga1702852009-03-27 00:12:24 -0700185 * @netdev: pointer to network device structure
186 * @napi: NAPI structure
Thierry Redinga1702852009-03-27 00:12:24 -0700187 * @msg_enable: device state flags
Thierry Redinga1702852009-03-27 00:12:24 -0700188 * @lock: device lock
189 * @phy: attached PHY
190 * @mdio: MDIO bus for PHY access
191 * @phy_id: address of attached PHY
192 */
193struct ethoc {
194 void __iomem *iobase;
195 void __iomem *membase;
Thomas Chou0baa0802009-10-04 23:33:20 +0000196 int dma_alloc;
Thomas Chouee02a4e2010-05-23 16:44:02 +0000197 resource_size_t io_region_size;
Thierry Redinga1702852009-03-27 00:12:24 -0700198
199 unsigned int num_tx;
200 unsigned int cur_tx;
201 unsigned int dty_tx;
202
203 unsigned int num_rx;
204 unsigned int cur_rx;
205
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000206 void** vma;
207
Thierry Redinga1702852009-03-27 00:12:24 -0700208 struct net_device *netdev;
209 struct napi_struct napi;
Thierry Redinga1702852009-03-27 00:12:24 -0700210 u32 msg_enable;
211
Thierry Redinga1702852009-03-27 00:12:24 -0700212 spinlock_t lock;
213
214 struct phy_device *phy;
215 struct mii_bus *mdio;
216 s8 phy_id;
217};
218
219/**
220 * struct ethoc_bd - buffer descriptor
221 * @stat: buffer statistics
222 * @addr: physical memory address
223 */
224struct ethoc_bd {
225 u32 stat;
226 u32 addr;
227};
228
Thomas Chou16dd18b2009-10-07 14:16:42 +0000229static inline u32 ethoc_read(struct ethoc *dev, loff_t offset)
Thierry Redinga1702852009-03-27 00:12:24 -0700230{
231 return ioread32(dev->iobase + offset);
232}
233
Thomas Chou16dd18b2009-10-07 14:16:42 +0000234static inline void ethoc_write(struct ethoc *dev, loff_t offset, u32 data)
Thierry Redinga1702852009-03-27 00:12:24 -0700235{
236 iowrite32(data, dev->iobase + offset);
237}
238
Thomas Chou16dd18b2009-10-07 14:16:42 +0000239static inline void ethoc_read_bd(struct ethoc *dev, int index,
240 struct ethoc_bd *bd)
Thierry Redinga1702852009-03-27 00:12:24 -0700241{
242 loff_t offset = ETHOC_BD_BASE + (index * sizeof(struct ethoc_bd));
243 bd->stat = ethoc_read(dev, offset + 0);
244 bd->addr = ethoc_read(dev, offset + 4);
245}
246
Thomas Chou16dd18b2009-10-07 14:16:42 +0000247static inline void ethoc_write_bd(struct ethoc *dev, int index,
Thierry Redinga1702852009-03-27 00:12:24 -0700248 const struct ethoc_bd *bd)
249{
250 loff_t offset = ETHOC_BD_BASE + (index * sizeof(struct ethoc_bd));
251 ethoc_write(dev, offset + 0, bd->stat);
252 ethoc_write(dev, offset + 4, bd->addr);
253}
254
Thomas Chou16dd18b2009-10-07 14:16:42 +0000255static inline void ethoc_enable_irq(struct ethoc *dev, u32 mask)
Thierry Redinga1702852009-03-27 00:12:24 -0700256{
257 u32 imask = ethoc_read(dev, INT_MASK);
258 imask |= mask;
259 ethoc_write(dev, INT_MASK, imask);
260}
261
Thomas Chou16dd18b2009-10-07 14:16:42 +0000262static inline void ethoc_disable_irq(struct ethoc *dev, u32 mask)
Thierry Redinga1702852009-03-27 00:12:24 -0700263{
264 u32 imask = ethoc_read(dev, INT_MASK);
265 imask &= ~mask;
266 ethoc_write(dev, INT_MASK, imask);
267}
268
Thomas Chou16dd18b2009-10-07 14:16:42 +0000269static inline void ethoc_ack_irq(struct ethoc *dev, u32 mask)
Thierry Redinga1702852009-03-27 00:12:24 -0700270{
271 ethoc_write(dev, INT_SOURCE, mask);
272}
273
Thomas Chou16dd18b2009-10-07 14:16:42 +0000274static inline void ethoc_enable_rx_and_tx(struct ethoc *dev)
Thierry Redinga1702852009-03-27 00:12:24 -0700275{
276 u32 mode = ethoc_read(dev, MODER);
277 mode |= MODER_RXEN | MODER_TXEN;
278 ethoc_write(dev, MODER, mode);
279}
280
Thomas Chou16dd18b2009-10-07 14:16:42 +0000281static inline void ethoc_disable_rx_and_tx(struct ethoc *dev)
Thierry Redinga1702852009-03-27 00:12:24 -0700282{
283 u32 mode = ethoc_read(dev, MODER);
284 mode &= ~(MODER_RXEN | MODER_TXEN);
285 ethoc_write(dev, MODER, mode);
286}
287
David S. Miller5cf3e032010-07-07 18:23:19 -0700288static int ethoc_init_ring(struct ethoc *dev, unsigned long mem_start)
Thierry Redinga1702852009-03-27 00:12:24 -0700289{
290 struct ethoc_bd bd;
291 int i;
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000292 void* vma;
Thierry Redinga1702852009-03-27 00:12:24 -0700293
294 dev->cur_tx = 0;
295 dev->dty_tx = 0;
296 dev->cur_rx = 0;
297
Jonas Bonnee4f56b2010-06-11 02:47:36 +0000298 ethoc_write(dev, TX_BD_NUM, dev->num_tx);
299
Thierry Redinga1702852009-03-27 00:12:24 -0700300 /* setup transmission buffers */
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000301 bd.addr = mem_start;
Thierry Redinga1702852009-03-27 00:12:24 -0700302 bd.stat = TX_BD_IRQ | TX_BD_CRC;
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000303 vma = dev->membase;
Thierry Redinga1702852009-03-27 00:12:24 -0700304
305 for (i = 0; i < dev->num_tx; i++) {
306 if (i == dev->num_tx - 1)
307 bd.stat |= TX_BD_WRAP;
308
309 ethoc_write_bd(dev, i, &bd);
310 bd.addr += ETHOC_BUFSIZ;
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000311
312 dev->vma[i] = vma;
313 vma += ETHOC_BUFSIZ;
Thierry Redinga1702852009-03-27 00:12:24 -0700314 }
315
Thierry Redinga1702852009-03-27 00:12:24 -0700316 bd.stat = RX_BD_EMPTY | RX_BD_IRQ;
317
318 for (i = 0; i < dev->num_rx; i++) {
319 if (i == dev->num_rx - 1)
320 bd.stat |= RX_BD_WRAP;
321
322 ethoc_write_bd(dev, dev->num_tx + i, &bd);
323 bd.addr += ETHOC_BUFSIZ;
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000324
325 dev->vma[dev->num_tx + i] = vma;
326 vma += ETHOC_BUFSIZ;
Thierry Redinga1702852009-03-27 00:12:24 -0700327 }
328
329 return 0;
330}
331
332static int ethoc_reset(struct ethoc *dev)
333{
334 u32 mode;
335
336 /* TODO: reset controller? */
337
338 ethoc_disable_rx_and_tx(dev);
339
340 /* TODO: setup registers */
341
342 /* enable FCS generation and automatic padding */
343 mode = ethoc_read(dev, MODER);
344 mode |= MODER_CRC | MODER_PAD;
345 ethoc_write(dev, MODER, mode);
346
347 /* set full-duplex mode */
348 mode = ethoc_read(dev, MODER);
349 mode |= MODER_FULLD;
350 ethoc_write(dev, MODER, mode);
351 ethoc_write(dev, IPGT, 0x15);
352
353 ethoc_ack_irq(dev, INT_MASK_ALL);
354 ethoc_enable_irq(dev, INT_MASK_ALL);
355 ethoc_enable_rx_and_tx(dev);
356 return 0;
357}
358
359static unsigned int ethoc_update_rx_stats(struct ethoc *dev,
360 struct ethoc_bd *bd)
361{
362 struct net_device *netdev = dev->netdev;
363 unsigned int ret = 0;
364
365 if (bd->stat & RX_BD_TL) {
366 dev_err(&netdev->dev, "RX: frame too long\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000367 netdev->stats.rx_length_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700368 ret++;
369 }
370
371 if (bd->stat & RX_BD_SF) {
372 dev_err(&netdev->dev, "RX: frame too short\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000373 netdev->stats.rx_length_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700374 ret++;
375 }
376
377 if (bd->stat & RX_BD_DN) {
378 dev_err(&netdev->dev, "RX: dribble nibble\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000379 netdev->stats.rx_frame_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700380 }
381
382 if (bd->stat & RX_BD_CRC) {
383 dev_err(&netdev->dev, "RX: wrong CRC\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000384 netdev->stats.rx_crc_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700385 ret++;
386 }
387
388 if (bd->stat & RX_BD_OR) {
389 dev_err(&netdev->dev, "RX: overrun\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000390 netdev->stats.rx_over_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700391 ret++;
392 }
393
394 if (bd->stat & RX_BD_MISS)
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000395 netdev->stats.rx_missed_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700396
397 if (bd->stat & RX_BD_LC) {
398 dev_err(&netdev->dev, "RX: late collision\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000399 netdev->stats.collisions++;
Thierry Redinga1702852009-03-27 00:12:24 -0700400 ret++;
401 }
402
403 return ret;
404}
405
406static int ethoc_rx(struct net_device *dev, int limit)
407{
408 struct ethoc *priv = netdev_priv(dev);
409 int count;
410
411 for (count = 0; count < limit; ++count) {
412 unsigned int entry;
413 struct ethoc_bd bd;
414
415 entry = priv->num_tx + (priv->cur_rx % priv->num_rx);
416 ethoc_read_bd(priv, entry, &bd);
Jonas Bonn20f70dd2010-11-25 02:30:28 +0000417 if (bd.stat & RX_BD_EMPTY) {
418 ethoc_ack_irq(priv, INT_MASK_RX);
419 /* If packet (interrupt) came in between checking
420 * BD_EMTPY and clearing the interrupt source, then we
421 * risk missing the packet as the RX interrupt won't
422 * trigger right away when we reenable it; hence, check
423 * BD_EMTPY here again to make sure there isn't such a
424 * packet waiting for us...
425 */
426 ethoc_read_bd(priv, entry, &bd);
427 if (bd.stat & RX_BD_EMPTY)
428 break;
429 }
Thierry Redinga1702852009-03-27 00:12:24 -0700430
431 if (ethoc_update_rx_stats(priv, &bd) == 0) {
432 int size = bd.stat >> 16;
Eric Dumazet89d71a62009-10-13 05:34:20 +0000433 struct sk_buff *skb;
Thomas Chou050f91d2009-10-04 23:33:19 +0000434
435 size -= 4; /* strip the CRC */
Eric Dumazet89d71a62009-10-13 05:34:20 +0000436 skb = netdev_alloc_skb_ip_align(dev, size);
Thomas Chou050f91d2009-10-04 23:33:19 +0000437
Thierry Redinga1702852009-03-27 00:12:24 -0700438 if (likely(skb)) {
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000439 void *src = priv->vma[entry];
Thierry Redinga1702852009-03-27 00:12:24 -0700440 memcpy_fromio(skb_put(skb, size), src, size);
441 skb->protocol = eth_type_trans(skb, dev);
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000442 dev->stats.rx_packets++;
443 dev->stats.rx_bytes += size;
Thierry Redinga1702852009-03-27 00:12:24 -0700444 netif_receive_skb(skb);
445 } else {
446 if (net_ratelimit())
447 dev_warn(&dev->dev, "low on memory - "
448 "packet dropped\n");
449
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000450 dev->stats.rx_dropped++;
Thierry Redinga1702852009-03-27 00:12:24 -0700451 break;
452 }
453 }
454
455 /* clear the buffer descriptor so it can be reused */
456 bd.stat &= ~RX_BD_STATS;
457 bd.stat |= RX_BD_EMPTY;
458 ethoc_write_bd(priv, entry, &bd);
459 priv->cur_rx++;
460 }
461
462 return count;
463}
464
Jonas Bonn4f64bcb2010-11-25 02:30:31 +0000465static void ethoc_update_tx_stats(struct ethoc *dev, struct ethoc_bd *bd)
Thierry Redinga1702852009-03-27 00:12:24 -0700466{
467 struct net_device *netdev = dev->netdev;
468
469 if (bd->stat & TX_BD_LC) {
470 dev_err(&netdev->dev, "TX: late collision\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000471 netdev->stats.tx_window_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700472 }
473
474 if (bd->stat & TX_BD_RL) {
475 dev_err(&netdev->dev, "TX: retransmit limit\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000476 netdev->stats.tx_aborted_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700477 }
478
479 if (bd->stat & TX_BD_UR) {
480 dev_err(&netdev->dev, "TX: underrun\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000481 netdev->stats.tx_fifo_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700482 }
483
484 if (bd->stat & TX_BD_CS) {
485 dev_err(&netdev->dev, "TX: carrier sense lost\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000486 netdev->stats.tx_carrier_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700487 }
488
489 if (bd->stat & TX_BD_STATS)
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000490 netdev->stats.tx_errors++;
Thierry Redinga1702852009-03-27 00:12:24 -0700491
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000492 netdev->stats.collisions += (bd->stat >> 4) & 0xf;
493 netdev->stats.tx_bytes += bd->stat >> 16;
494 netdev->stats.tx_packets++;
Thierry Redinga1702852009-03-27 00:12:24 -0700495}
496
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000497static int ethoc_tx(struct net_device *dev, int limit)
Thierry Redinga1702852009-03-27 00:12:24 -0700498{
499 struct ethoc *priv = netdev_priv(dev);
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000500 int count;
501 struct ethoc_bd bd;
Thierry Redinga1702852009-03-27 00:12:24 -0700502
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000503 for (count = 0; count < limit; ++count) {
504 unsigned int entry;
Thierry Redinga1702852009-03-27 00:12:24 -0700505
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000506 entry = priv->dty_tx % priv->num_tx;
Thierry Redinga1702852009-03-27 00:12:24 -0700507
508 ethoc_read_bd(priv, entry, &bd);
Thierry Redinga1702852009-03-27 00:12:24 -0700509
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000510 if (bd.stat & TX_BD_READY || (priv->dty_tx == priv->cur_tx)) {
511 ethoc_ack_irq(priv, INT_MASK_TX);
512 /* If interrupt came in between reading in the BD
513 * and clearing the interrupt source, then we risk
514 * missing the event as the TX interrupt won't trigger
515 * right away when we reenable it; hence, check
516 * BD_EMPTY here again to make sure there isn't such an
517 * event pending...
518 */
519 ethoc_read_bd(priv, entry, &bd);
520 if (bd.stat & TX_BD_READY ||
521 (priv->dty_tx == priv->cur_tx))
522 break;
523 }
524
Jonas Bonn4f64bcb2010-11-25 02:30:31 +0000525 ethoc_update_tx_stats(priv, &bd);
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000526 priv->dty_tx++;
Thierry Redinga1702852009-03-27 00:12:24 -0700527 }
528
529 if ((priv->cur_tx - priv->dty_tx) <= (priv->num_tx / 2))
530 netif_wake_queue(dev);
531
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000532 return count;
Thierry Redinga1702852009-03-27 00:12:24 -0700533}
534
535static irqreturn_t ethoc_interrupt(int irq, void *dev_id)
536{
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000537 struct net_device *dev = dev_id;
Thierry Redinga1702852009-03-27 00:12:24 -0700538 struct ethoc *priv = netdev_priv(dev);
539 u32 pending;
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000540 u32 mask;
Thierry Redinga1702852009-03-27 00:12:24 -0700541
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000542 /* Figure out what triggered the interrupt...
543 * The tricky bit here is that the interrupt source bits get
544 * set in INT_SOURCE for an event irregardless of whether that
545 * event is masked or not. Thus, in order to figure out what
546 * triggered the interrupt, we need to remove the sources
547 * for all events that are currently masked. This behaviour
548 * is not particularly well documented but reasonable...
549 */
550 mask = ethoc_read(priv, INT_MASK);
Thierry Redinga1702852009-03-27 00:12:24 -0700551 pending = ethoc_read(priv, INT_SOURCE);
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000552 pending &= mask;
553
Thierry Redinga1702852009-03-27 00:12:24 -0700554 if (unlikely(pending == 0)) {
Thierry Redinga1702852009-03-27 00:12:24 -0700555 return IRQ_NONE;
556 }
557
Thomas Chou50c54a52009-10-07 14:16:43 +0000558 ethoc_ack_irq(priv, pending);
Thierry Redinga1702852009-03-27 00:12:24 -0700559
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000560 /* We always handle the dropped packet interrupt */
Thierry Redinga1702852009-03-27 00:12:24 -0700561 if (pending & INT_MASK_BUSY) {
562 dev_err(&dev->dev, "packet dropped\n");
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000563 dev->stats.rx_dropped++;
Thierry Redinga1702852009-03-27 00:12:24 -0700564 }
565
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000566 /* Handle receive/transmit event by switching to polling */
567 if (pending & (INT_MASK_TX | INT_MASK_RX)) {
568 ethoc_disable_irq(priv, INT_MASK_TX | INT_MASK_RX);
569 napi_schedule(&priv->napi);
Thierry Redinga1702852009-03-27 00:12:24 -0700570 }
571
Thierry Redinga1702852009-03-27 00:12:24 -0700572 return IRQ_HANDLED;
573}
574
575static int ethoc_get_mac_address(struct net_device *dev, void *addr)
576{
577 struct ethoc *priv = netdev_priv(dev);
578 u8 *mac = (u8 *)addr;
579 u32 reg;
580
581 reg = ethoc_read(priv, MAC_ADDR0);
582 mac[2] = (reg >> 24) & 0xff;
583 mac[3] = (reg >> 16) & 0xff;
584 mac[4] = (reg >> 8) & 0xff;
585 mac[5] = (reg >> 0) & 0xff;
586
587 reg = ethoc_read(priv, MAC_ADDR1);
588 mac[0] = (reg >> 8) & 0xff;
589 mac[1] = (reg >> 0) & 0xff;
590
591 return 0;
592}
593
594static int ethoc_poll(struct napi_struct *napi, int budget)
595{
596 struct ethoc *priv = container_of(napi, struct ethoc, napi);
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000597 int rx_work_done = 0;
598 int tx_work_done = 0;
Thierry Redinga1702852009-03-27 00:12:24 -0700599
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000600 rx_work_done = ethoc_rx(priv->netdev, budget);
601 tx_work_done = ethoc_tx(priv->netdev, budget);
602
603 if (rx_work_done < budget && tx_work_done < budget) {
Thierry Redinga1702852009-03-27 00:12:24 -0700604 napi_complete(napi);
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000605 ethoc_enable_irq(priv, INT_MASK_TX | INT_MASK_RX);
Thierry Redinga1702852009-03-27 00:12:24 -0700606 }
607
Jonas Bonnfa98eb02010-11-25 02:30:29 +0000608 return rx_work_done;
Thierry Redinga1702852009-03-27 00:12:24 -0700609}
610
611static int ethoc_mdio_read(struct mii_bus *bus, int phy, int reg)
612{
Thierry Redinga1702852009-03-27 00:12:24 -0700613 struct ethoc *priv = bus->priv;
Jonas Bonn8dac4282010-11-25 02:30:30 +0000614 int i;
Thierry Redinga1702852009-03-27 00:12:24 -0700615
616 ethoc_write(priv, MIIADDRESS, MIIADDRESS_ADDR(phy, reg));
617 ethoc_write(priv, MIICOMMAND, MIICOMMAND_READ);
618
Jonas Bonn8dac4282010-11-25 02:30:30 +0000619 for (i=0; i < 5; i++) {
Thierry Redinga1702852009-03-27 00:12:24 -0700620 u32 status = ethoc_read(priv, MIISTATUS);
621 if (!(status & MIISTATUS_BUSY)) {
622 u32 data = ethoc_read(priv, MIIRX_DATA);
623 /* reset MII command register */
624 ethoc_write(priv, MIICOMMAND, 0);
625 return data;
626 }
Jonas Bonn8dac4282010-11-25 02:30:30 +0000627 usleep_range(100,200);
Thierry Redinga1702852009-03-27 00:12:24 -0700628 }
629
630 return -EBUSY;
631}
632
633static int ethoc_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
634{
Thierry Redinga1702852009-03-27 00:12:24 -0700635 struct ethoc *priv = bus->priv;
Jonas Bonn8dac4282010-11-25 02:30:30 +0000636 int i;
Thierry Redinga1702852009-03-27 00:12:24 -0700637
638 ethoc_write(priv, MIIADDRESS, MIIADDRESS_ADDR(phy, reg));
639 ethoc_write(priv, MIITX_DATA, val);
640 ethoc_write(priv, MIICOMMAND, MIICOMMAND_WRITE);
641
Jonas Bonn8dac4282010-11-25 02:30:30 +0000642 for (i=0; i < 5; i++) {
Thierry Redinga1702852009-03-27 00:12:24 -0700643 u32 stat = ethoc_read(priv, MIISTATUS);
Jonas Bonnb46773d2010-06-11 02:47:39 +0000644 if (!(stat & MIISTATUS_BUSY)) {
645 /* reset MII command register */
646 ethoc_write(priv, MIICOMMAND, 0);
Thierry Redinga1702852009-03-27 00:12:24 -0700647 return 0;
Jonas Bonnb46773d2010-06-11 02:47:39 +0000648 }
Jonas Bonn8dac4282010-11-25 02:30:30 +0000649 usleep_range(100,200);
Thierry Redinga1702852009-03-27 00:12:24 -0700650 }
651
652 return -EBUSY;
653}
654
655static int ethoc_mdio_reset(struct mii_bus *bus)
656{
657 return 0;
658}
659
660static void ethoc_mdio_poll(struct net_device *dev)
661{
662}
663
Jonas Bonnf78f09f2010-07-26 18:45:05 -0700664static int __devinit ethoc_mdio_probe(struct net_device *dev)
Thierry Redinga1702852009-03-27 00:12:24 -0700665{
666 struct ethoc *priv = netdev_priv(dev);
667 struct phy_device *phy;
Jonas Bonn637f33b82010-06-11 02:47:37 +0000668 int err;
Thierry Redinga1702852009-03-27 00:12:24 -0700669
Jonas Bonn637f33b82010-06-11 02:47:37 +0000670 if (priv->phy_id != -1) {
671 phy = priv->mdio->phy_map[priv->phy_id];
672 } else {
673 phy = phy_find_first(priv->mdio);
Thierry Redinga1702852009-03-27 00:12:24 -0700674 }
675
676 if (!phy) {
677 dev_err(&dev->dev, "no PHY found\n");
678 return -ENXIO;
679 }
680
Jonas Bonn637f33b82010-06-11 02:47:37 +0000681 err = phy_connect_direct(dev, phy, ethoc_mdio_poll, 0,
Thierry Redinga1702852009-03-27 00:12:24 -0700682 PHY_INTERFACE_MODE_GMII);
Jonas Bonn637f33b82010-06-11 02:47:37 +0000683 if (err) {
Thierry Redinga1702852009-03-27 00:12:24 -0700684 dev_err(&dev->dev, "could not attach to PHY\n");
Jonas Bonn637f33b82010-06-11 02:47:37 +0000685 return err;
Thierry Redinga1702852009-03-27 00:12:24 -0700686 }
687
688 priv->phy = phy;
689 return 0;
690}
691
692static int ethoc_open(struct net_device *dev)
693{
694 struct ethoc *priv = netdev_priv(dev);
Thierry Redinga1702852009-03-27 00:12:24 -0700695 int ret;
696
697 ret = request_irq(dev->irq, ethoc_interrupt, IRQF_SHARED,
698 dev->name, dev);
699 if (ret)
700 return ret;
701
David S. Miller5cf3e032010-07-07 18:23:19 -0700702 ethoc_init_ring(priv, dev->mem_start);
Thierry Redinga1702852009-03-27 00:12:24 -0700703 ethoc_reset(priv);
704
705 if (netif_queue_stopped(dev)) {
706 dev_dbg(&dev->dev, " resuming queue\n");
707 netif_wake_queue(dev);
708 } else {
709 dev_dbg(&dev->dev, " starting queue\n");
710 netif_start_queue(dev);
711 }
712
713 phy_start(priv->phy);
714 napi_enable(&priv->napi);
715
716 if (netif_msg_ifup(priv)) {
717 dev_info(&dev->dev, "I/O: %08lx Memory: %08lx-%08lx\n",
718 dev->base_addr, dev->mem_start, dev->mem_end);
719 }
720
721 return 0;
722}
723
724static int ethoc_stop(struct net_device *dev)
725{
726 struct ethoc *priv = netdev_priv(dev);
727
728 napi_disable(&priv->napi);
729
730 if (priv->phy)
731 phy_stop(priv->phy);
732
733 ethoc_disable_rx_and_tx(priv);
734 free_irq(dev->irq, dev);
735
736 if (!netif_queue_stopped(dev))
737 netif_stop_queue(dev);
738
739 return 0;
740}
741
742static int ethoc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
743{
744 struct ethoc *priv = netdev_priv(dev);
745 struct mii_ioctl_data *mdio = if_mii(ifr);
746 struct phy_device *phy = NULL;
747
748 if (!netif_running(dev))
749 return -EINVAL;
750
751 if (cmd != SIOCGMIIPHY) {
752 if (mdio->phy_id >= PHY_MAX_ADDR)
753 return -ERANGE;
754
755 phy = priv->mdio->phy_map[mdio->phy_id];
756 if (!phy)
757 return -ENODEV;
758 } else {
759 phy = priv->phy;
760 }
761
Richard Cochran28b04112010-07-17 08:48:55 +0000762 return phy_mii_ioctl(phy, ifr, cmd);
Thierry Redinga1702852009-03-27 00:12:24 -0700763}
764
765static int ethoc_config(struct net_device *dev, struct ifmap *map)
766{
767 return -ENOSYS;
768}
769
770static int ethoc_set_mac_address(struct net_device *dev, void *addr)
771{
772 struct ethoc *priv = netdev_priv(dev);
773 u8 *mac = (u8 *)addr;
774
775 ethoc_write(priv, MAC_ADDR0, (mac[2] << 24) | (mac[3] << 16) |
776 (mac[4] << 8) | (mac[5] << 0));
777 ethoc_write(priv, MAC_ADDR1, (mac[0] << 8) | (mac[1] << 0));
778
779 return 0;
780}
781
782static void ethoc_set_multicast_list(struct net_device *dev)
783{
784 struct ethoc *priv = netdev_priv(dev);
785 u32 mode = ethoc_read(priv, MODER);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000786 struct netdev_hw_addr *ha;
Thierry Redinga1702852009-03-27 00:12:24 -0700787 u32 hash[2] = { 0, 0 };
788
789 /* set loopback mode if requested */
790 if (dev->flags & IFF_LOOPBACK)
791 mode |= MODER_LOOP;
792 else
793 mode &= ~MODER_LOOP;
794
795 /* receive broadcast frames if requested */
796 if (dev->flags & IFF_BROADCAST)
797 mode &= ~MODER_BRO;
798 else
799 mode |= MODER_BRO;
800
801 /* enable promiscuous mode if requested */
802 if (dev->flags & IFF_PROMISC)
803 mode |= MODER_PRO;
804 else
805 mode &= ~MODER_PRO;
806
807 ethoc_write(priv, MODER, mode);
808
809 /* receive multicast frames */
810 if (dev->flags & IFF_ALLMULTI) {
811 hash[0] = 0xffffffff;
812 hash[1] = 0xffffffff;
813 } else {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000814 netdev_for_each_mc_addr(ha, dev) {
815 u32 crc = ether_crc(ETH_ALEN, ha->addr);
Thierry Redinga1702852009-03-27 00:12:24 -0700816 int bit = (crc >> 26) & 0x3f;
817 hash[bit >> 5] |= 1 << (bit & 0x1f);
818 }
819 }
820
821 ethoc_write(priv, ETH_HASH0, hash[0]);
822 ethoc_write(priv, ETH_HASH1, hash[1]);
823}
824
825static int ethoc_change_mtu(struct net_device *dev, int new_mtu)
826{
827 return -ENOSYS;
828}
829
830static void ethoc_tx_timeout(struct net_device *dev)
831{
832 struct ethoc *priv = netdev_priv(dev);
833 u32 pending = ethoc_read(priv, INT_SOURCE);
834 if (likely(pending))
835 ethoc_interrupt(dev->irq, dev);
836}
837
Stephen Hemminger613573252009-08-31 19:50:58 +0000838static netdev_tx_t ethoc_start_xmit(struct sk_buff *skb, struct net_device *dev)
Thierry Redinga1702852009-03-27 00:12:24 -0700839{
840 struct ethoc *priv = netdev_priv(dev);
841 struct ethoc_bd bd;
842 unsigned int entry;
843 void *dest;
844
845 if (unlikely(skb->len > ETHOC_BUFSIZ)) {
Kulikov Vasiliy57616ee2010-07-05 02:13:31 +0000846 dev->stats.tx_errors++;
Patrick McHardy3790c8c2009-06-12 03:00:35 +0000847 goto out;
Thierry Redinga1702852009-03-27 00:12:24 -0700848 }
849
850 entry = priv->cur_tx % priv->num_tx;
851 spin_lock_irq(&priv->lock);
852 priv->cur_tx++;
853
854 ethoc_read_bd(priv, entry, &bd);
855 if (unlikely(skb->len < ETHOC_ZLEN))
856 bd.stat |= TX_BD_PAD;
857 else
858 bd.stat &= ~TX_BD_PAD;
859
Jonas Bonnf8555ad02010-06-11 02:47:35 +0000860 dest = priv->vma[entry];
Thierry Redinga1702852009-03-27 00:12:24 -0700861 memcpy_toio(dest, skb->data, skb->len);
862
863 bd.stat &= ~(TX_BD_STATS | TX_BD_LEN_MASK);
864 bd.stat |= TX_BD_LEN(skb->len);
865 ethoc_write_bd(priv, entry, &bd);
866
867 bd.stat |= TX_BD_READY;
868 ethoc_write_bd(priv, entry, &bd);
869
870 if (priv->cur_tx == (priv->dty_tx + priv->num_tx)) {
871 dev_dbg(&dev->dev, "stopping queue\n");
872 netif_stop_queue(dev);
873 }
874
Thierry Redinga1702852009-03-27 00:12:24 -0700875 spin_unlock_irq(&priv->lock);
Patrick McHardy3790c8c2009-06-12 03:00:35 +0000876out:
877 dev_kfree_skb(skb);
Thierry Redinga1702852009-03-27 00:12:24 -0700878 return NETDEV_TX_OK;
879}
880
881static const struct net_device_ops ethoc_netdev_ops = {
882 .ndo_open = ethoc_open,
883 .ndo_stop = ethoc_stop,
884 .ndo_do_ioctl = ethoc_ioctl,
885 .ndo_set_config = ethoc_config,
886 .ndo_set_mac_address = ethoc_set_mac_address,
887 .ndo_set_multicast_list = ethoc_set_multicast_list,
888 .ndo_change_mtu = ethoc_change_mtu,
889 .ndo_tx_timeout = ethoc_tx_timeout,
Thierry Redinga1702852009-03-27 00:12:24 -0700890 .ndo_start_xmit = ethoc_start_xmit,
891};
892
893/**
894 * ethoc_probe() - initialize OpenCores ethernet MAC
895 * pdev: platform device
896 */
Jonas Bonnf78f09f2010-07-26 18:45:05 -0700897static int __devinit ethoc_probe(struct platform_device *pdev)
Thierry Redinga1702852009-03-27 00:12:24 -0700898{
899 struct net_device *netdev = NULL;
900 struct resource *res = NULL;
901 struct resource *mmio = NULL;
902 struct resource *mem = NULL;
903 struct ethoc *priv = NULL;
904 unsigned int phy;
Jonas Bonnc527f812010-06-11 02:47:34 +0000905 int num_bd;
Thierry Redinga1702852009-03-27 00:12:24 -0700906 int ret = 0;
907
908 /* allocate networking device */
909 netdev = alloc_etherdev(sizeof(struct ethoc));
910 if (!netdev) {
911 dev_err(&pdev->dev, "cannot allocate network device\n");
912 ret = -ENOMEM;
913 goto out;
914 }
915
916 SET_NETDEV_DEV(netdev, &pdev->dev);
917 platform_set_drvdata(pdev, netdev);
918
919 /* obtain I/O memory space */
920 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
921 if (!res) {
922 dev_err(&pdev->dev, "cannot obtain I/O memory space\n");
923 ret = -ENXIO;
924 goto free;
925 }
926
927 mmio = devm_request_mem_region(&pdev->dev, res->start,
Tobias Klauserd8645842010-01-15 01:48:22 -0800928 resource_size(res), res->name);
Julia Lawall463889e2009-07-27 06:13:30 +0000929 if (!mmio) {
Thierry Redinga1702852009-03-27 00:12:24 -0700930 dev_err(&pdev->dev, "cannot request I/O memory space\n");
931 ret = -ENXIO;
932 goto free;
933 }
934
935 netdev->base_addr = mmio->start;
936
937 /* obtain buffer memory space */
938 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Thomas Chou0baa0802009-10-04 23:33:20 +0000939 if (res) {
940 mem = devm_request_mem_region(&pdev->dev, res->start,
Tobias Klauserd8645842010-01-15 01:48:22 -0800941 resource_size(res), res->name);
Thomas Chou0baa0802009-10-04 23:33:20 +0000942 if (!mem) {
943 dev_err(&pdev->dev, "cannot request memory space\n");
944 ret = -ENXIO;
945 goto free;
946 }
947
948 netdev->mem_start = mem->start;
949 netdev->mem_end = mem->end;
Thierry Redinga1702852009-03-27 00:12:24 -0700950 }
951
Thierry Redinga1702852009-03-27 00:12:24 -0700952
953 /* obtain device IRQ number */
954 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
955 if (!res) {
956 dev_err(&pdev->dev, "cannot obtain IRQ\n");
957 ret = -ENXIO;
958 goto free;
959 }
960
961 netdev->irq = res->start;
962
963 /* setup driver-private data */
964 priv = netdev_priv(netdev);
965 priv->netdev = netdev;
Thomas Chou0baa0802009-10-04 23:33:20 +0000966 priv->dma_alloc = 0;
Thomas Chouee02a4e2010-05-23 16:44:02 +0000967 priv->io_region_size = mmio->end - mmio->start + 1;
Thierry Redinga1702852009-03-27 00:12:24 -0700968
969 priv->iobase = devm_ioremap_nocache(&pdev->dev, netdev->base_addr,
Tobias Klauserd8645842010-01-15 01:48:22 -0800970 resource_size(mmio));
Thierry Redinga1702852009-03-27 00:12:24 -0700971 if (!priv->iobase) {
972 dev_err(&pdev->dev, "cannot remap I/O memory space\n");
973 ret = -ENXIO;
974 goto error;
975 }
976
Thomas Chou0baa0802009-10-04 23:33:20 +0000977 if (netdev->mem_end) {
978 priv->membase = devm_ioremap_nocache(&pdev->dev,
Tobias Klauserd8645842010-01-15 01:48:22 -0800979 netdev->mem_start, resource_size(mem));
Thomas Chou0baa0802009-10-04 23:33:20 +0000980 if (!priv->membase) {
981 dev_err(&pdev->dev, "cannot remap memory space\n");
982 ret = -ENXIO;
983 goto error;
984 }
985 } else {
986 /* Allocate buffer memory */
Jonas Bonna71fba92010-06-11 02:47:40 +0000987 priv->membase = dmam_alloc_coherent(&pdev->dev,
Thomas Chou0baa0802009-10-04 23:33:20 +0000988 buffer_size, (void *)&netdev->mem_start,
989 GFP_KERNEL);
990 if (!priv->membase) {
991 dev_err(&pdev->dev, "cannot allocate %dB buffer\n",
992 buffer_size);
993 ret = -ENOMEM;
994 goto error;
995 }
996 netdev->mem_end = netdev->mem_start + buffer_size;
997 priv->dma_alloc = buffer_size;
Thierry Redinga1702852009-03-27 00:12:24 -0700998 }
999
Jonas Bonnc527f812010-06-11 02:47:34 +00001000 /* calculate the number of TX/RX buffers, maximum 128 supported */
1001 num_bd = min_t(unsigned int,
1002 128, (netdev->mem_end - netdev->mem_start + 1) / ETHOC_BUFSIZ);
1003 priv->num_tx = max(2, num_bd / 4);
1004 priv->num_rx = num_bd - priv->num_tx;
1005
Jonas Bonnf8555ad02010-06-11 02:47:35 +00001006 priv->vma = devm_kzalloc(&pdev->dev, num_bd*sizeof(void*), GFP_KERNEL);
1007 if (!priv->vma) {
1008 ret = -ENOMEM;
1009 goto error;
1010 }
1011
Thierry Redinga1702852009-03-27 00:12:24 -07001012 /* Allow the platform setup code to pass in a MAC address. */
1013 if (pdev->dev.platform_data) {
Jonas Bonne0f42582010-11-25 02:30:25 +00001014 struct ethoc_platform_data *pdata = pdev->dev.platform_data;
Thierry Redinga1702852009-03-27 00:12:24 -07001015 memcpy(netdev->dev_addr, pdata->hwaddr, IFHWADDRLEN);
1016 priv->phy_id = pdata->phy_id;
Jonas Bonne0f42582010-11-25 02:30:25 +00001017 } else {
1018 priv->phy_id = -1;
1019
1020#ifdef CONFIG_OF
1021 {
1022 const uint8_t* mac;
1023
1024 mac = of_get_property(pdev->dev.of_node,
1025 "local-mac-address",
1026 NULL);
1027 if (mac)
1028 memcpy(netdev->dev_addr, mac, IFHWADDRLEN);
1029 }
1030#endif
Thierry Redinga1702852009-03-27 00:12:24 -07001031 }
1032
1033 /* Check that the given MAC address is valid. If it isn't, read the
1034 * current MAC from the controller. */
1035 if (!is_valid_ether_addr(netdev->dev_addr))
1036 ethoc_get_mac_address(netdev, netdev->dev_addr);
1037
1038 /* Check the MAC again for validity, if it still isn't choose and
1039 * program a random one. */
1040 if (!is_valid_ether_addr(netdev->dev_addr))
1041 random_ether_addr(netdev->dev_addr);
1042
1043 ethoc_set_mac_address(netdev, netdev->dev_addr);
1044
1045 /* register MII bus */
1046 priv->mdio = mdiobus_alloc();
1047 if (!priv->mdio) {
1048 ret = -ENOMEM;
1049 goto free;
1050 }
1051
1052 priv->mdio->name = "ethoc-mdio";
1053 snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "%s-%d",
1054 priv->mdio->name, pdev->id);
1055 priv->mdio->read = ethoc_mdio_read;
1056 priv->mdio->write = ethoc_mdio_write;
1057 priv->mdio->reset = ethoc_mdio_reset;
1058 priv->mdio->priv = priv;
1059
1060 priv->mdio->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
1061 if (!priv->mdio->irq) {
1062 ret = -ENOMEM;
1063 goto free_mdio;
1064 }
1065
1066 for (phy = 0; phy < PHY_MAX_ADDR; phy++)
1067 priv->mdio->irq[phy] = PHY_POLL;
1068
1069 ret = mdiobus_register(priv->mdio);
1070 if (ret) {
1071 dev_err(&netdev->dev, "failed to register MDIO bus\n");
1072 goto free_mdio;
1073 }
1074
1075 ret = ethoc_mdio_probe(netdev);
1076 if (ret) {
1077 dev_err(&netdev->dev, "failed to probe MDIO bus\n");
1078 goto error;
1079 }
1080
1081 ether_setup(netdev);
1082
1083 /* setup the net_device structure */
1084 netdev->netdev_ops = &ethoc_netdev_ops;
1085 netdev->watchdog_timeo = ETHOC_TIMEOUT;
1086 netdev->features |= 0;
1087
1088 /* setup NAPI */
Thierry Redinga1702852009-03-27 00:12:24 -07001089 netif_napi_add(netdev, &priv->napi, ethoc_poll, 64);
1090
Thierry Redinga1702852009-03-27 00:12:24 -07001091 spin_lock_init(&priv->lock);
1092
1093 ret = register_netdev(netdev);
1094 if (ret < 0) {
1095 dev_err(&netdev->dev, "failed to register interface\n");
Thomas Chouee02a4e2010-05-23 16:44:02 +00001096 goto error2;
Thierry Redinga1702852009-03-27 00:12:24 -07001097 }
1098
1099 goto out;
1100
Thomas Chouee02a4e2010-05-23 16:44:02 +00001101error2:
1102 netif_napi_del(&priv->napi);
Thierry Redinga1702852009-03-27 00:12:24 -07001103error:
1104 mdiobus_unregister(priv->mdio);
1105free_mdio:
1106 kfree(priv->mdio->irq);
1107 mdiobus_free(priv->mdio);
1108free:
1109 free_netdev(netdev);
1110out:
1111 return ret;
1112}
1113
1114/**
1115 * ethoc_remove() - shutdown OpenCores ethernet MAC
1116 * @pdev: platform device
1117 */
Jonas Bonnf78f09f2010-07-26 18:45:05 -07001118static int __devexit ethoc_remove(struct platform_device *pdev)
Thierry Redinga1702852009-03-27 00:12:24 -07001119{
1120 struct net_device *netdev = platform_get_drvdata(pdev);
1121 struct ethoc *priv = netdev_priv(netdev);
1122
1123 platform_set_drvdata(pdev, NULL);
1124
1125 if (netdev) {
Thomas Chouee02a4e2010-05-23 16:44:02 +00001126 netif_napi_del(&priv->napi);
Thierry Redinga1702852009-03-27 00:12:24 -07001127 phy_disconnect(priv->phy);
1128 priv->phy = NULL;
1129
1130 if (priv->mdio) {
1131 mdiobus_unregister(priv->mdio);
1132 kfree(priv->mdio->irq);
1133 mdiobus_free(priv->mdio);
1134 }
Thierry Redinga1702852009-03-27 00:12:24 -07001135 unregister_netdev(netdev);
1136 free_netdev(netdev);
1137 }
1138
1139 return 0;
1140}
1141
1142#ifdef CONFIG_PM
1143static int ethoc_suspend(struct platform_device *pdev, pm_message_t state)
1144{
1145 return -ENOSYS;
1146}
1147
1148static int ethoc_resume(struct platform_device *pdev)
1149{
1150 return -ENOSYS;
1151}
1152#else
1153# define ethoc_suspend NULL
1154# define ethoc_resume NULL
1155#endif
1156
Jonas Bonne0f42582010-11-25 02:30:25 +00001157#ifdef CONFIG_OF
1158static struct of_device_id ethoc_match[] = {
1159 {
1160 .compatible = "opencores,ethoc",
1161 },
1162 {},
1163};
1164MODULE_DEVICE_TABLE(of, ethoc_match);
1165#endif
1166
Thierry Redinga1702852009-03-27 00:12:24 -07001167static struct platform_driver ethoc_driver = {
1168 .probe = ethoc_probe,
Jonas Bonnf78f09f2010-07-26 18:45:05 -07001169 .remove = __devexit_p(ethoc_remove),
Thierry Redinga1702852009-03-27 00:12:24 -07001170 .suspend = ethoc_suspend,
1171 .resume = ethoc_resume,
1172 .driver = {
1173 .name = "ethoc",
Jonas Bonne0f42582010-11-25 02:30:25 +00001174 .owner = THIS_MODULE,
1175#ifdef CONFIG_OF
1176 .of_match_table = ethoc_match,
1177#endif
Thierry Redinga1702852009-03-27 00:12:24 -07001178 },
1179};
1180
1181static int __init ethoc_init(void)
1182{
1183 return platform_driver_register(&ethoc_driver);
1184}
1185
1186static void __exit ethoc_exit(void)
1187{
1188 platform_driver_unregister(&ethoc_driver);
1189}
1190
1191module_init(ethoc_init);
1192module_exit(ethoc_exit);
1193
1194MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
1195MODULE_DESCRIPTION("OpenCores Ethernet MAC driver");
1196MODULE_LICENSE("GPL v2");
1197