1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include <linux/module.h>
37#include <linux/kernel.h>
38#include <linux/sched.h>
39#include <linux/string.h>
40#include <linux/timer.h>
41#include <linux/errno.h>
42#include <linux/in.h>
43#include <linux/ioport.h>
44#include <linux/slab.h>
45#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/init.h>
48#include <linux/netdevice.h>
49#include <linux/etherdevice.h>
50#include <linux/skbuff.h>
51#include <linux/ethtool.h>
52#include <linux/highmem.h>
53#include <linux/proc_fs.h>
54#include <linux/ctype.h>
55#include <linux/version.h>
56#include <linux/spinlock.h>
57#include <linux/dma-mapping.h>
58#include <linux/clk.h>
59#include <linux/platform_device.h>
60#include <linux/semaphore.h>
61#include <linux/phy.h>
62#include <linux/bitops.h>
63#include <linux/io.h>
64#include <linux/uaccess.h>
65
66#include <asm/irq.h>
67#include <asm/page.h>
68
69#include <mach/emac.h>
70
71static int debug_level;
72module_param(debug_level, int, 0);
73MODULE_PARM_DESC(debug_level, "DaVinci EMAC debug level (NETIF_MSG bits)");
74
75
76#define DAVINCI_EMAC_DEBUG (NETIF_MSG_DRV | \
77 NETIF_MSG_PROBE | \
78 NETIF_MSG_LINK | \
79 NETIF_MSG_TIMER | \
80 NETIF_MSG_IFDOWN | \
81 NETIF_MSG_IFUP | \
82 NETIF_MSG_RX_ERR | \
83 NETIF_MSG_TX_ERR | \
84 NETIF_MSG_TX_QUEUED | \
85 NETIF_MSG_INTR | \
86 NETIF_MSG_TX_DONE | \
87 NETIF_MSG_RX_STATUS | \
88 NETIF_MSG_PKTDATA | \
89 NETIF_MSG_HW | \
90 NETIF_MSG_WOL)
91
92
93#define EMAC_MAJOR_VERSION 6
94#define EMAC_MINOR_VERSION 1
95#define EMAC_MODULE_VERSION "6.1"
96MODULE_VERSION(EMAC_MODULE_VERSION);
97static const char emac_version_string[] = "TI DaVinci EMAC Linux v6.1";
98
99
100#define EMAC_DEF_PASS_CRC (0)
101#define EMAC_DEF_QOS_EN (0)
102#define EMAC_DEF_NO_BUFF_CHAIN (0)
103#define EMAC_DEF_MACCTRL_FRAME_EN (0)
104#define EMAC_DEF_SHORT_FRAME_EN (0)
105#define EMAC_DEF_ERROR_FRAME_EN (0)
106#define EMAC_DEF_PROM_EN (0)
107#define EMAC_DEF_PROM_CH (0)
108#define EMAC_DEF_BCAST_EN (1)
109#define EMAC_DEF_BCAST_CH (0)
110#define EMAC_DEF_MCAST_EN (1)
111#define EMAC_DEF_MCAST_CH (0)
112
113#define EMAC_DEF_TXPRIO_FIXED (1)
114#define EMAC_DEF_TXPACING_EN (0)
115
116#define EMAC_DEF_BUFFER_OFFSET (0)
117#define EMAC_DEF_MIN_ETHPKTSIZE (60)
118#define EMAC_DEF_MAX_FRAME_SIZE (1500 + 14 + 4 + 4)
119#define EMAC_DEF_TX_CH (0)
120#define EMAC_DEF_RX_CH (0)
121#define EMAC_DEF_MDIO_TICK_MS (10)
122#define EMAC_DEF_MAX_TX_CH (1)
123#define EMAC_DEF_MAX_RX_CH (1)
124#define EMAC_POLL_WEIGHT (64)
125
126
127#define EMAC_DEF_TX_MAX_SERVICE (32)
128#define EMAC_DEF_RX_MAX_SERVICE (64)
129
130
131#define EMAC_ALL_MULTI_REG_VALUE (0xFFFFFFFF)
132#define EMAC_NUM_MULTICAST_BITS (64)
133#define EMAC_TEARDOWN_VALUE (0xFFFFFFFC)
134#define EMAC_TX_CONTROL_TX_ENABLE_VAL (0x1)
135#define EMAC_RX_CONTROL_RX_ENABLE_VAL (0x1)
136#define EMAC_MAC_HOST_ERR_INTMASK_VAL (0x2)
137#define EMAC_RX_UNICAST_CLEAR_ALL (0xFF)
138#define EMAC_INT_MASK_CLEAR (0xFF)
139
140
141#define EMAC_RXMBP_PASSCRC_MASK BIT(30)
142#define EMAC_RXMBP_QOSEN_MASK BIT(29)
143#define EMAC_RXMBP_NOCHAIN_MASK BIT(28)
144#define EMAC_RXMBP_CMFEN_MASK BIT(24)
145#define EMAC_RXMBP_CSFEN_MASK BIT(23)
146#define EMAC_RXMBP_CEFEN_MASK BIT(22)
147#define EMAC_RXMBP_CAFEN_MASK BIT(21)
148#define EMAC_RXMBP_PROMCH_SHIFT (16)
149#define EMAC_RXMBP_PROMCH_MASK (0x7 << 16)
150#define EMAC_RXMBP_BROADEN_MASK BIT(13)
151#define EMAC_RXMBP_BROADCH_SHIFT (8)
152#define EMAC_RXMBP_BROADCH_MASK (0x7 << 8)
153#define EMAC_RXMBP_MULTIEN_MASK BIT(5)
154#define EMAC_RXMBP_MULTICH_SHIFT (0)
155#define EMAC_RXMBP_MULTICH_MASK (0x7)
156#define EMAC_RXMBP_CHMASK (0x7)
157
158
159# define EMAC_MBP_RXPROMISC (0x00200000)
160# define EMAC_MBP_PROMISCCH(ch) (((ch) & 0x7) << 16)
161# define EMAC_MBP_RXBCAST (0x00002000)
162# define EMAC_MBP_BCASTCHAN(ch) (((ch) & 0x7) << 8)
163# define EMAC_MBP_RXMCAST (0x00000020)
164# define EMAC_MBP_MCASTCHAN(ch) ((ch) & 0x7)
165
166
167#define EMAC_MACCONTROL_TXPTYPE BIT(9)
168#define EMAC_MACCONTROL_TXPACEEN BIT(6)
169#define EMAC_MACCONTROL_GMIIEN BIT(5)
170#define EMAC_MACCONTROL_GIGABITEN BIT(7)
171#define EMAC_MACCONTROL_FULLDUPLEXEN BIT(0)
172#define EMAC_MACCONTROL_RMIISPEED_MASK BIT(15)
173
174
175#define EMAC_DM646X_MACCONTORL_GIG BIT(7)
176#define EMAC_DM646X_MACCONTORL_GIGFORCE BIT(17)
177
178
179#define EMAC_MACSTATUS_TXERRCODE_MASK (0xF00000)
180#define EMAC_MACSTATUS_TXERRCODE_SHIFT (20)
181#define EMAC_MACSTATUS_TXERRCH_MASK (0x7)
182#define EMAC_MACSTATUS_TXERRCH_SHIFT (16)
183#define EMAC_MACSTATUS_RXERRCODE_MASK (0xF000)
184#define EMAC_MACSTATUS_RXERRCODE_SHIFT (12)
185#define EMAC_MACSTATUS_RXERRCH_MASK (0x7)
186#define EMAC_MACSTATUS_RXERRCH_SHIFT (8)
187
188
189#define EMAC_RX_MAX_LEN_MASK (0xFFFF)
190#define EMAC_RX_BUFFER_OFFSET_MASK (0xFFFF)
191
192
193#define EMAC_DM644X_MAC_IN_VECTOR_HOST_INT BIT(17)
194#define EMAC_DM644X_MAC_IN_VECTOR_STATPEND_INT BIT(16)
195#define EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC BIT(8)
196#define EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC BIT(0)
197
198
199#define EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC BIT(EMAC_DEF_RX_CH)
200#define EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC BIT(16 + EMAC_DEF_TX_CH)
201#define EMAC_DM646X_MAC_IN_VECTOR_HOST_INT BIT(26)
202#define EMAC_DM646X_MAC_IN_VECTOR_STATPEND_INT BIT(27)
203
204
205#define EMAC_CPPI_SOP_BIT BIT(31)
206#define EMAC_CPPI_EOP_BIT BIT(30)
207#define EMAC_CPPI_OWNERSHIP_BIT BIT(29)
208#define EMAC_CPPI_EOQ_BIT BIT(28)
209#define EMAC_CPPI_TEARDOWN_COMPLETE_BIT BIT(27)
210#define EMAC_CPPI_PASS_CRC_BIT BIT(26)
211#define EMAC_RX_BD_BUF_SIZE (0xFFFF)
212#define EMAC_BD_LENGTH_FOR_CACHE (16)
213#define EMAC_RX_BD_PKT_LENGTH_MASK (0xFFFF)
214
215
216#define EMAC_MAX_TXRX_CHANNELS (8)
217#define EMAC_DEF_MAX_MULTICAST_ADDRESSES (64)
218
219
220#define EMAC_TXIDVER 0x0
221#define EMAC_TXCONTROL 0x4
222#define EMAC_TXTEARDOWN 0x8
223#define EMAC_RXIDVER 0x10
224#define EMAC_RXCONTROL 0x14
225#define EMAC_RXTEARDOWN 0x18
226#define EMAC_TXINTSTATRAW 0x80
227#define EMAC_TXINTSTATMASKED 0x84
228#define EMAC_TXINTMASKSET 0x88
229#define EMAC_TXINTMASKCLEAR 0x8C
230#define EMAC_MACINVECTOR 0x90
231
232#define EMAC_DM646X_MACEOIVECTOR 0x94
233
234#define EMAC_RXINTSTATRAW 0xA0
235#define EMAC_RXINTSTATMASKED 0xA4
236#define EMAC_RXINTMASKSET 0xA8
237#define EMAC_RXINTMASKCLEAR 0xAC
238#define EMAC_MACINTSTATRAW 0xB0
239#define EMAC_MACINTSTATMASKED 0xB4
240#define EMAC_MACINTMASKSET 0xB8
241#define EMAC_MACINTMASKCLEAR 0xBC
242
243#define EMAC_RXMBPENABLE 0x100
244#define EMAC_RXUNICASTSET 0x104
245#define EMAC_RXUNICASTCLEAR 0x108
246#define EMAC_RXMAXLEN 0x10C
247#define EMAC_RXBUFFEROFFSET 0x110
248#define EMAC_RXFILTERLOWTHRESH 0x114
249
250#define EMAC_MACCONTROL 0x160
251#define EMAC_MACSTATUS 0x164
252#define EMAC_EMCONTROL 0x168
253#define EMAC_FIFOCONTROL 0x16C
254#define EMAC_MACCONFIG 0x170
255#define EMAC_SOFTRESET 0x174
256#define EMAC_MACSRCADDRLO 0x1D0
257#define EMAC_MACSRCADDRHI 0x1D4
258#define EMAC_MACHASH1 0x1D8
259#define EMAC_MACHASH2 0x1DC
260#define EMAC_MACADDRLO 0x500
261#define EMAC_MACADDRHI 0x504
262#define EMAC_MACINDEX 0x508
263
264
265#define EMAC_TXHDP(ch) (0x600 + (ch * 4))
266#define EMAC_RXHDP(ch) (0x620 + (ch * 4))
267#define EMAC_TXCP(ch) (0x640 + (ch * 4))
268#define EMAC_RXCP(ch) (0x660 + (ch * 4))
269
270
271#define EMAC_RXGOODFRAMES 0x200
272#define EMAC_RXBCASTFRAMES 0x204
273#define EMAC_RXMCASTFRAMES 0x208
274#define EMAC_RXPAUSEFRAMES 0x20C
275#define EMAC_RXCRCERRORS 0x210
276#define EMAC_RXALIGNCODEERRORS 0x214
277#define EMAC_RXOVERSIZED 0x218
278#define EMAC_RXJABBER 0x21C
279#define EMAC_RXUNDERSIZED 0x220
280#define EMAC_RXFRAGMENTS 0x224
281#define EMAC_RXFILTERED 0x228
282#define EMAC_RXQOSFILTERED 0x22C
283#define EMAC_RXOCTETS 0x230
284#define EMAC_TXGOODFRAMES 0x234
285#define EMAC_TXBCASTFRAMES 0x238
286#define EMAC_TXMCASTFRAMES 0x23C
287#define EMAC_TXPAUSEFRAMES 0x240
288#define EMAC_TXDEFERRED 0x244
289#define EMAC_TXCOLLISION 0x248
290#define EMAC_TXSINGLECOLL 0x24C
291#define EMAC_TXMULTICOLL 0x250
292#define EMAC_TXEXCESSIVECOLL 0x254
293#define EMAC_TXLATECOLL 0x258
294#define EMAC_TXUNDERRUN 0x25C
295#define EMAC_TXCARRIERSENSE 0x260
296#define EMAC_TXOCTETS 0x264
297#define EMAC_NETOCTETS 0x280
298#define EMAC_RXSOFOVERRUNS 0x284
299#define EMAC_RXMOFOVERRUNS 0x288
300#define EMAC_RXDMAOVERRUNS 0x28C
301
302
303#define EMAC_CTRL_EWCTL (0x4)
304#define EMAC_CTRL_EWINTTCNT (0x8)
305
306
307
308#define MDIO_CONTROL_CLKDIV (0xFF)
309#define MDIO_CONTROL_ENABLE BIT(30)
310#define MDIO_USERACCESS_GO BIT(31)
311#define MDIO_USERACCESS_WRITE BIT(30)
312#define MDIO_USERACCESS_READ (0)
313#define MDIO_USERACCESS_REGADR (0x1F << 21)
314#define MDIO_USERACCESS_PHYADR (0x1F << 16)
315#define MDIO_USERACCESS_DATA (0xFFFF)
316#define MDIO_USERPHYSEL_LINKSEL BIT(7)
317#define MDIO_VER_MODID (0xFFFF << 16)
318#define MDIO_VER_REVMAJ (0xFF << 8)
319#define MDIO_VER_REVMIN (0xFF)
320
321#define MDIO_USERACCESS(inst) (0x80 + (inst * 8))
322#define MDIO_USERPHYSEL(inst) (0x84 + (inst * 8))
323#define MDIO_CONTROL (0x04)
324
325
326#define EMAC_DM646X_CMRXINTEN (0x14)
327#define EMAC_DM646X_CMTXINTEN (0x18)
328
329
330#define EMAC_DM646X_MAC_EOI_C0_RXEN (0x01)
331#define EMAC_DM646X_MAC_EOI_C0_TXEN (0x02)
332
333
334#define EMAC_STATS_CLR_MASK (0xFFFFFFFF)
335
336
337
338
339
340struct emac_netbufobj {
341 void *buf_token;
342 char *data_ptr;
343 int length;
344};
345
346
347
348
349
350struct emac_netpktobj {
351 void *pkt_token;
352 struct emac_netbufobj *buf_list;
353 int num_bufs;
354 int pkt_length;
355};
356
357
358
359
360
361struct emac_tx_bd {
362 int h_next;
363 int buff_ptr;
364 int off_b_len;
365 int mode;
366 struct emac_tx_bd __iomem *next;
367 void *buf_token;
368};
369
370
371
372
373
374struct emac_txch {
375
376 u32 num_bd;
377 u32 service_max;
378
379
380 u32 alloc_size;
381 void __iomem *bd_mem;
382 struct emac_tx_bd __iomem *bd_pool_head;
383 struct emac_tx_bd __iomem *active_queue_head;
384 struct emac_tx_bd __iomem *active_queue_tail;
385 struct emac_tx_bd __iomem *last_hw_bdprocessed;
386 u32 queue_active;
387 u32 teardown_pending;
388 u32 *tx_complete;
389
390
391 u32 proc_count;
392 u32 mis_queued_packets;
393 u32 queue_reinit;
394 u32 end_of_queue_add;
395 u32 out_of_tx_bd;
396 u32 no_active_pkts;
397 u32 active_queue_count;
398};
399
400
401
402
403
404struct emac_rx_bd {
405 int h_next;
406 int buff_ptr;
407 int off_b_len;
408 int mode;
409 struct emac_rx_bd __iomem *next;
410 void *data_ptr;
411 void *buf_token;
412};
413
414
415
416
417
418struct emac_rxch {
419
420 u32 num_bd;
421 u32 service_max;
422 u32 buf_size;
423 char mac_addr[6];
424
425
426 u32 alloc_size;
427 void __iomem *bd_mem;
428 struct emac_rx_bd __iomem *bd_pool_head;
429 struct emac_rx_bd __iomem *active_queue_head;
430 struct emac_rx_bd __iomem *active_queue_tail;
431 u32 queue_active;
432 u32 teardown_pending;
433
434
435 struct emac_netpktobj pkt_queue;
436 struct emac_netbufobj buf_queue;
437
438
439 u32 proc_count;
440 u32 processed_bd;
441 u32 recycled_bd;
442 u32 out_of_rx_bd;
443 u32 out_of_rx_buffers;
444 u32 queue_reinit;
445 u32 end_of_queue_add;
446 u32 end_of_queue;
447 u32 mis_queued_packets;
448};
449
450
451
452
453
454struct emac_priv {
455 u32 msg_enable;
456 struct net_device *ndev;
457 struct platform_device *pdev;
458 struct napi_struct napi;
459 char mac_addr[6];
460 spinlock_t tx_lock;
461 spinlock_t rx_lock;
462 void __iomem *remap_addr;
463 u32 emac_base_phys;
464 void __iomem *emac_base;
465 void __iomem *ctrl_base;
466 void __iomem *emac_ctrl_ram;
467 u32 ctrl_ram_size;
468 struct emac_txch *txch[EMAC_DEF_MAX_TX_CH];
469 struct emac_rxch *rxch[EMAC_DEF_MAX_RX_CH];
470 u32 link;
471 u32 speed;
472 u32 duplex;
473 u32 rx_buf_size;
474 u32 isr_count;
475 u8 rmii_en;
476 u8 version;
477 struct net_device_stats net_dev_stats;
478 u32 mac_hash1;
479 u32 mac_hash2;
480 u32 multicast_hash_cnt[EMAC_NUM_MULTICAST_BITS];
481 u32 rx_addr_type;
482
483 struct timer_list periodic_timer;
484 u32 periodic_ticks;
485 u32 timer_active;
486 u32 phy_mask;
487
488 struct mii_bus *mii_bus;
489 struct phy_device *phydev;
490 spinlock_t lock;
491};
492
493
494static struct clk *emac_clk;
495static unsigned long emac_bus_frequency;
496static unsigned long mdio_max_freq;
497
498
499static inline u32 emac_virt_to_phys(void __iomem *addr)
500{
501 return (u32 __force) io_v2p(addr);
502}
503
504
505#define EMAC_VIRT_NOCACHE(addr) (addr)
506#define EMAC_CACHE_INVALIDATE(addr, size) \
507 dma_cache_maint((void *)addr, size, DMA_FROM_DEVICE)
508#define EMAC_CACHE_WRITEBACK(addr, size) \
509 dma_cache_maint((void *)addr, size, DMA_TO_DEVICE)
510#define EMAC_CACHE_WRITEBACK_INVALIDATE(addr, size) \
511 dma_cache_maint((void *)addr, size, DMA_BIDIRECTIONAL)
512
513
514#define BD_CACHE_INVALIDATE(addr, size)
515#define BD_CACHE_WRITEBACK(addr, size)
516#define BD_CACHE_WRITEBACK_INVALIDATE(addr, size)
517
518
519static char *emac_txhost_errcodes[16] = {
520 "No error", "SOP error", "Ownership bit not set in SOP buffer",
521 "Zero Next Buffer Descriptor Pointer Without EOP",
522 "Zero Buffer Pointer", "Zero Buffer Length", "Packet Length Error",
523 "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
524 "Reserved", "Reserved", "Reserved", "Reserved"
525};
526
527
528static char *emac_rxhost_errcodes[16] = {
529 "No error", "Reserved", "Ownership bit not set in input buffer",
530 "Reserved", "Zero Buffer Pointer", "Reserved", "Reserved",
531 "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
532 "Reserved", "Reserved", "Reserved", "Reserved"
533};
534
535
536#define emac_read(reg) ioread32(priv->emac_base + (reg))
537#define emac_write(reg, val) iowrite32(val, priv->emac_base + (reg))
538
539#define emac_ctrl_read(reg) ioread32((priv->ctrl_base + (reg)))
540#define emac_ctrl_write(reg, val) iowrite32(val, (priv->ctrl_base + (reg)))
541
542#define emac_mdio_read(reg) ioread32(bus->priv + (reg))
543#define emac_mdio_write(reg, val) iowrite32(val, (bus->priv + (reg)))
544
545
546
547
548
549
550
551
552static void emac_dump_regs(struct emac_priv *priv)
553{
554 struct device *emac_dev = &priv->ndev->dev;
555
556
557 dev_info(emac_dev, "EMAC Basic registers\n");
558 dev_info(emac_dev, "EMAC: EWCTL: %08X, EWINTTCNT: %08X\n",
559 emac_ctrl_read(EMAC_CTRL_EWCTL),
560 emac_ctrl_read(EMAC_CTRL_EWINTTCNT));
561 dev_info(emac_dev, "EMAC: TXID: %08X %s, RXID: %08X %s\n",
562 emac_read(EMAC_TXIDVER),
563 ((emac_read(EMAC_TXCONTROL)) ? "enabled" : "disabled"),
564 emac_read(EMAC_RXIDVER),
565 ((emac_read(EMAC_RXCONTROL)) ? "enabled" : "disabled"));
566 dev_info(emac_dev, "EMAC: TXIntRaw:%08X, TxIntMasked: %08X, "\
567 "TxIntMasSet: %08X\n", emac_read(EMAC_TXINTSTATRAW),
568 emac_read(EMAC_TXINTSTATMASKED), emac_read(EMAC_TXINTMASKSET));
569 dev_info(emac_dev, "EMAC: RXIntRaw:%08X, RxIntMasked: %08X, "\
570 "RxIntMasSet: %08X\n", emac_read(EMAC_RXINTSTATRAW),
571 emac_read(EMAC_RXINTSTATMASKED), emac_read(EMAC_RXINTMASKSET));
572 dev_info(emac_dev, "EMAC: MacIntRaw:%08X, MacIntMasked: %08X, "\
573 "MacInVector=%08X\n", emac_read(EMAC_MACINTSTATRAW),
574 emac_read(EMAC_MACINTSTATMASKED), emac_read(EMAC_MACINVECTOR));
575 dev_info(emac_dev, "EMAC: EmuControl:%08X, FifoControl: %08X\n",
576 emac_read(EMAC_EMCONTROL), emac_read(EMAC_FIFOCONTROL));
577 dev_info(emac_dev, "EMAC: MBPEnable:%08X, RXUnicastSet: %08X, "\
578 "RXMaxLen=%08X\n", emac_read(EMAC_RXMBPENABLE),
579 emac_read(EMAC_RXUNICASTSET), emac_read(EMAC_RXMAXLEN));
580 dev_info(emac_dev, "EMAC: MacControl:%08X, MacStatus: %08X, "\
581 "MacConfig=%08X\n", emac_read(EMAC_MACCONTROL),
582 emac_read(EMAC_MACSTATUS), emac_read(EMAC_MACCONFIG));
583 dev_info(emac_dev, "EMAC: TXHDP[0]:%08X, RXHDP[0]: %08X\n",
584 emac_read(EMAC_TXHDP(0)), emac_read(EMAC_RXHDP(0)));
585 dev_info(emac_dev, "EMAC Statistics\n");
586 dev_info(emac_dev, "EMAC: rx_good_frames:%d\n",
587 emac_read(EMAC_RXGOODFRAMES));
588 dev_info(emac_dev, "EMAC: rx_broadcast_frames:%d\n",
589 emac_read(EMAC_RXBCASTFRAMES));
590 dev_info(emac_dev, "EMAC: rx_multicast_frames:%d\n",
591 emac_read(EMAC_RXMCASTFRAMES));
592 dev_info(emac_dev, "EMAC: rx_pause_frames:%d\n",
593 emac_read(EMAC_RXPAUSEFRAMES));
594 dev_info(emac_dev, "EMAC: rx_crcerrors:%d\n",
595 emac_read(EMAC_RXCRCERRORS));
596 dev_info(emac_dev, "EMAC: rx_align_code_errors:%d\n",
597 emac_read(EMAC_RXALIGNCODEERRORS));
598 dev_info(emac_dev, "EMAC: rx_oversized_frames:%d\n",
599 emac_read(EMAC_RXOVERSIZED));
600 dev_info(emac_dev, "EMAC: rx_jabber_frames:%d\n",
601 emac_read(EMAC_RXJABBER));
602 dev_info(emac_dev, "EMAC: rx_undersized_frames:%d\n",
603 emac_read(EMAC_RXUNDERSIZED));
604 dev_info(emac_dev, "EMAC: rx_fragments:%d\n",
605 emac_read(EMAC_RXFRAGMENTS));
606 dev_info(emac_dev, "EMAC: rx_filtered_frames:%d\n",
607 emac_read(EMAC_RXFILTERED));
608 dev_info(emac_dev, "EMAC: rx_qos_filtered_frames:%d\n",
609 emac_read(EMAC_RXQOSFILTERED));
610 dev_info(emac_dev, "EMAC: rx_octets:%d\n",
611 emac_read(EMAC_RXOCTETS));
612 dev_info(emac_dev, "EMAC: tx_goodframes:%d\n",
613 emac_read(EMAC_TXGOODFRAMES));
614 dev_info(emac_dev, "EMAC: tx_bcastframes:%d\n",
615 emac_read(EMAC_TXBCASTFRAMES));
616 dev_info(emac_dev, "EMAC: tx_mcastframes:%d\n",
617 emac_read(EMAC_TXMCASTFRAMES));
618 dev_info(emac_dev, "EMAC: tx_pause_frames:%d\n",
619 emac_read(EMAC_TXPAUSEFRAMES));
620 dev_info(emac_dev, "EMAC: tx_deferred_frames:%d\n",
621 emac_read(EMAC_TXDEFERRED));
622 dev_info(emac_dev, "EMAC: tx_collision_frames:%d\n",
623 emac_read(EMAC_TXCOLLISION));
624 dev_info(emac_dev, "EMAC: tx_single_coll_frames:%d\n",
625 emac_read(EMAC_TXSINGLECOLL));
626 dev_info(emac_dev, "EMAC: tx_mult_coll_frames:%d\n",
627 emac_read(EMAC_TXMULTICOLL));
628 dev_info(emac_dev, "EMAC: tx_excessive_collisions:%d\n",
629 emac_read(EMAC_TXEXCESSIVECOLL));
630 dev_info(emac_dev, "EMAC: tx_late_collisions:%d\n",
631 emac_read(EMAC_TXLATECOLL));
632 dev_info(emac_dev, "EMAC: tx_underrun:%d\n",
633 emac_read(EMAC_TXUNDERRUN));
634 dev_info(emac_dev, "EMAC: tx_carrier_sense_errors:%d\n",
635 emac_read(EMAC_TXCARRIERSENSE));
636 dev_info(emac_dev, "EMAC: tx_octets:%d\n",
637 emac_read(EMAC_TXOCTETS));
638 dev_info(emac_dev, "EMAC: net_octets:%d\n",
639 emac_read(EMAC_NETOCTETS));
640 dev_info(emac_dev, "EMAC: rx_sof_overruns:%d\n",
641 emac_read(EMAC_RXSOFOVERRUNS));
642 dev_info(emac_dev, "EMAC: rx_mof_overruns:%d\n",
643 emac_read(EMAC_RXMOFOVERRUNS));
644 dev_info(emac_dev, "EMAC: rx_dma_overruns:%d\n",
645 emac_read(EMAC_RXDMAOVERRUNS));
646}
647
648
649
650
651
652
653
654
655
656
657
658
659static void emac_get_drvinfo(struct net_device *ndev,
660 struct ethtool_drvinfo *info)
661{
662 strcpy(info->driver, emac_version_string);
663 strcpy(info->version, EMAC_MODULE_VERSION);
664}
665
666
667
668
669
670
671
672
673
674static int emac_get_settings(struct net_device *ndev,
675 struct ethtool_cmd *ecmd)
676{
677 struct emac_priv *priv = netdev_priv(ndev);
678 if (priv->phy_mask)
679 return phy_ethtool_gset(priv->phydev, ecmd);
680 else
681 return -EOPNOTSUPP;
682
683}
684
685
686
687
688
689
690
691
692
693static int emac_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
694{
695 struct emac_priv *priv = netdev_priv(ndev);
696 if (priv->phy_mask)
697 return phy_ethtool_sset(priv->phydev, ecmd);
698 else
699 return -EOPNOTSUPP;
700
701}
702
703
704
705
706
707
708
709static const struct ethtool_ops ethtool_ops = {
710 .get_drvinfo = emac_get_drvinfo,
711 .get_settings = emac_get_settings,
712 .set_settings = emac_set_settings,
713 .get_link = ethtool_op_get_link,
714};
715
716
717
718
719
720
721
722
723
724static void emac_update_phystatus(struct emac_priv *priv)
725{
726 u32 mac_control;
727 u32 new_duplex;
728 u32 cur_duplex;
729 struct net_device *ndev = priv->ndev;
730
731 mac_control = emac_read(EMAC_MACCONTROL);
732 cur_duplex = (mac_control & EMAC_MACCONTROL_FULLDUPLEXEN) ?
733 DUPLEX_FULL : DUPLEX_HALF;
734 if (priv->phy_mask)
735 new_duplex = priv->phydev->duplex;
736 else
737 new_duplex = DUPLEX_FULL;
738
739
740 if ((priv->link) && (new_duplex != cur_duplex)) {
741 priv->duplex = new_duplex;
742 if (DUPLEX_FULL == priv->duplex)
743 mac_control |= (EMAC_MACCONTROL_FULLDUPLEXEN);
744 else
745 mac_control &= ~(EMAC_MACCONTROL_FULLDUPLEXEN);
746 }
747
748 if (priv->speed == SPEED_1000 && (priv->version == EMAC_VERSION_2)) {
749 mac_control = emac_read(EMAC_MACCONTROL);
750 mac_control |= (EMAC_DM646X_MACCONTORL_GIG |
751 EMAC_DM646X_MACCONTORL_GIGFORCE);
752 } else {
753
754 mac_control &= ~(EMAC_DM646X_MACCONTORL_GIGFORCE |
755 EMAC_DM646X_MACCONTORL_GIG);
756
757 if (priv->rmii_en && (priv->speed == SPEED_100))
758 mac_control |= EMAC_MACCONTROL_RMIISPEED_MASK;
759 else
760 mac_control &= ~EMAC_MACCONTROL_RMIISPEED_MASK;
761 }
762
763
764 emac_write(EMAC_MACCONTROL, mac_control);
765
766 if (priv->link) {
767
768 if (!netif_carrier_ok(ndev))
769 netif_carrier_on(ndev);
770
771 if (netif_running(ndev) && netif_queue_stopped(ndev))
772 netif_wake_queue(ndev);
773 } else {
774
775 if (netif_carrier_ok(ndev))
776 netif_carrier_off(ndev);
777 if (!netif_queue_stopped(ndev))
778 netif_stop_queue(ndev);
779 }
780}
781
782
783
784
785
786
787
788
789static u32 hash_get(u8 *addr)
790{
791 u32 hash;
792 u8 tmpval;
793 int cnt;
794 hash = 0;
795
796 for (cnt = 0; cnt < 2; cnt++) {
797 tmpval = *addr++;
798 hash ^= (tmpval >> 2) ^ (tmpval << 4);
799 tmpval = *addr++;
800 hash ^= (tmpval >> 4) ^ (tmpval << 2);
801 tmpval = *addr++;
802 hash ^= (tmpval >> 6) ^ (tmpval);
803 }
804
805 return hash & 0x3F;
806}
807
808
809
810
811
812
813
814
815
816static int hash_add(struct emac_priv *priv, u8 *mac_addr)
817{
818 struct device *emac_dev = &priv->ndev->dev;
819 u32 rc = 0;
820 u32 hash_bit;
821 u32 hash_value = hash_get(mac_addr);
822
823 if (hash_value >= EMAC_NUM_MULTICAST_BITS) {
824 if (netif_msg_drv(priv)) {
825 dev_err(emac_dev, "DaVinci EMAC: hash_add(): Invalid "\
826 "Hash %08x, should not be greater than %08x",
827 hash_value, (EMAC_NUM_MULTICAST_BITS - 1));
828 }
829 return -1;
830 }
831
832
833 if (priv->multicast_hash_cnt[hash_value] == 0) {
834 rc = 1;
835 if (hash_value < 32) {
836 hash_bit = BIT(hash_value);
837 priv->mac_hash1 |= hash_bit;
838 } else {
839 hash_bit = BIT((hash_value - 32));
840 priv->mac_hash2 |= hash_bit;
841 }
842 }
843
844
845 ++priv->multicast_hash_cnt[hash_value];
846
847 return rc;
848}
849
850
851
852
853
854
855
856
857
858static int hash_del(struct emac_priv *priv, u8 *mac_addr)
859{
860 u32 hash_value;
861 u32 hash_bit;
862
863 hash_value = hash_get(mac_addr);
864 if (priv->multicast_hash_cnt[hash_value] > 0) {
865
866 --priv->multicast_hash_cnt[hash_value];
867 }
868
869
870
871 if (priv->multicast_hash_cnt[hash_value] > 0)
872 return 0;
873
874 if (hash_value < 32) {
875 hash_bit = BIT(hash_value);
876 priv->mac_hash1 &= ~hash_bit;
877 } else {
878 hash_bit = BIT((hash_value - 32));
879 priv->mac_hash2 &= ~hash_bit;
880 }
881
882
883 return 1;
884}
885
886
887#define EMAC_MULTICAST_ADD 0
888#define EMAC_MULTICAST_DEL 1
889#define EMAC_ALL_MULTI_SET 2
890#define EMAC_ALL_MULTI_CLR 3
891
892
893
894
895
896
897
898
899
900
901static void emac_add_mcast(struct emac_priv *priv, u32 action, u8 *mac_addr)
902{
903 struct device *emac_dev = &priv->ndev->dev;
904 int update = -1;
905
906 switch (action) {
907 case EMAC_MULTICAST_ADD:
908 update = hash_add(priv, mac_addr);
909 break;
910 case EMAC_MULTICAST_DEL:
911 update = hash_del(priv, mac_addr);
912 break;
913 case EMAC_ALL_MULTI_SET:
914 update = 1;
915 priv->mac_hash1 = EMAC_ALL_MULTI_REG_VALUE;
916 priv->mac_hash2 = EMAC_ALL_MULTI_REG_VALUE;
917 break;
918 case EMAC_ALL_MULTI_CLR:
919 update = 1;
920 priv->mac_hash1 = 0;
921 priv->mac_hash2 = 0;
922 memset(&(priv->multicast_hash_cnt[0]), 0,
923 sizeof(priv->multicast_hash_cnt[0]) *
924 EMAC_NUM_MULTICAST_BITS);
925 break;
926 default:
927 if (netif_msg_drv(priv))
928 dev_err(emac_dev, "DaVinci EMAC: add_mcast"\
929 ": bad operation %d", action);
930 break;
931 }
932
933
934 if (update > 0) {
935 emac_write(EMAC_MACHASH1, priv->mac_hash1);
936 emac_write(EMAC_MACHASH2, priv->mac_hash2);
937 }
938}
939
940
941
942
943
944
945
946
947static void emac_dev_mcast_set(struct net_device *ndev)
948{
949 u32 mbp_enable;
950 struct emac_priv *priv = netdev_priv(ndev);
951
952 mbp_enable = emac_read(EMAC_RXMBPENABLE);
953 if (ndev->flags & IFF_PROMISC) {
954 mbp_enable &= (~EMAC_MBP_PROMISCCH(EMAC_DEF_PROM_CH));
955 mbp_enable |= (EMAC_MBP_RXPROMISC);
956 } else {
957 mbp_enable = (mbp_enable & ~EMAC_MBP_RXPROMISC);
958 if ((ndev->flags & IFF_ALLMULTI) ||
959 (ndev->mc_count > EMAC_DEF_MAX_MULTICAST_ADDRESSES)) {
960 mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
961 emac_add_mcast(priv, EMAC_ALL_MULTI_SET, NULL);
962 }
963 if (ndev->mc_count > 0) {
964 struct dev_mc_list *mc_ptr;
965 mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
966 emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
967
968 for (mc_ptr = ndev->mc_list; mc_ptr;
969 mc_ptr = mc_ptr->next) {
970 emac_add_mcast(priv, EMAC_MULTICAST_ADD,
971 (u8 *)mc_ptr->dmi_addr);
972 }
973 } else {
974 mbp_enable = (mbp_enable & ~EMAC_MBP_RXMCAST);
975 emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
976 }
977 }
978
979 emac_write(EMAC_RXMBPENABLE, mbp_enable);
980}
981
982
983
984
985
986
987
988
989
990
991
992
993static void emac_int_disable(struct emac_priv *priv)
994{
995 if (priv->version == EMAC_VERSION_2) {
996 unsigned long flags;
997
998 local_irq_save(flags);
999
1000
1001
1002 emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0x0);
1003 emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0x0);
1004
1005
1006 local_irq_restore(flags);
1007
1008 } else {
1009
1010 emac_ctrl_write(EMAC_CTRL_EWCTL, 0x0);
1011 }
1012}
1013
1014
1015
1016
1017
1018
1019
1020
1021static void emac_int_enable(struct emac_priv *priv)
1022{
1023 if (priv->version == EMAC_VERSION_2) {
1024 emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0xff);
1025 emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0xff);
1026
1027
1028
1029
1030
1031
1032
1033
1034 emac_write(EMAC_DM646X_MACEOIVECTOR,
1035 EMAC_DM646X_MAC_EOI_C0_RXEN);
1036
1037
1038 emac_write(EMAC_DM646X_MACEOIVECTOR,
1039 EMAC_DM646X_MAC_EOI_C0_TXEN);
1040
1041 } else {
1042
1043 emac_ctrl_write(EMAC_CTRL_EWCTL, 0x1);
1044 }
1045}
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057static irqreturn_t emac_irq(int irq, void *dev_id)
1058{
1059 struct net_device *ndev = (struct net_device *)dev_id;
1060 struct emac_priv *priv = netdev_priv(ndev);
1061
1062 ++priv->isr_count;
1063 if (likely(netif_running(priv->ndev))) {
1064 emac_int_disable(priv);
1065 napi_schedule(&priv->napi);
1066 } else {
1067
1068 }
1069 return IRQ_HANDLED;
1070}
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081#define EMAC_TX_BD_MEM(priv) ((priv)->emac_ctrl_ram)
1082#define EMAC_RX_BD_MEM(priv) ((priv)->emac_ctrl_ram + \
1083 (((priv)->ctrl_ram_size) >> 1))
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095static int emac_init_txch(struct emac_priv *priv, u32 ch)
1096{
1097 struct device *emac_dev = &priv->ndev->dev;
1098 u32 cnt, bd_size;
1099 void __iomem *mem;
1100 struct emac_tx_bd __iomem *curr_bd;
1101 struct emac_txch *txch = NULL;
1102
1103 txch = kzalloc(sizeof(struct emac_txch), GFP_KERNEL);
1104 if (NULL == txch) {
1105 dev_err(emac_dev, "DaVinci EMAC: TX Ch mem alloc failed");
1106 return -ENOMEM;
1107 }
1108 priv->txch[ch] = txch;
1109 txch->service_max = EMAC_DEF_TX_MAX_SERVICE;
1110 txch->active_queue_head = NULL;
1111 txch->active_queue_tail = NULL;
1112 txch->queue_active = 0;
1113 txch->teardown_pending = 0;
1114
1115
1116 txch->tx_complete = kzalloc(txch->service_max * sizeof(u32),
1117 GFP_KERNEL);
1118 if (NULL == txch->tx_complete) {
1119 dev_err(emac_dev, "DaVinci EMAC: Tx service mem alloc failed");
1120 kfree(txch);
1121 return -ENOMEM;
1122 }
1123
1124
1125
1126 bd_size = (sizeof(struct emac_tx_bd) + 0xF) & ~0xF;
1127 txch->num_bd = (priv->ctrl_ram_size >> 1) / bd_size;
1128 txch->alloc_size = (((bd_size * txch->num_bd) + 0xF) & ~0xF);
1129
1130
1131 txch->bd_mem = EMAC_TX_BD_MEM(priv);
1132 __memzero((void __force *)txch->bd_mem, txch->alloc_size);
1133
1134
1135 mem = (void __force __iomem *)
1136 (((u32 __force) txch->bd_mem + 0xF) & ~0xF);
1137 txch->bd_pool_head = NULL;
1138 for (cnt = 0; cnt < txch->num_bd; cnt++) {
1139 curr_bd = mem + (cnt * bd_size);
1140 curr_bd->next = txch->bd_pool_head;
1141 txch->bd_pool_head = curr_bd;
1142 }
1143
1144
1145 txch->out_of_tx_bd = 0;
1146 txch->no_active_pkts = 0;
1147 txch->active_queue_count = 0;
1148
1149 return 0;
1150}
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160static void emac_cleanup_txch(struct emac_priv *priv, u32 ch)
1161{
1162 struct emac_txch *txch = priv->txch[ch];
1163
1164 if (txch) {
1165 if (txch->bd_mem)
1166 txch->bd_mem = NULL;
1167 kfree(txch->tx_complete);
1168 kfree(txch);
1169 priv->txch[ch] = NULL;
1170 }
1171}
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183static int emac_net_tx_complete(struct emac_priv *priv,
1184 void **net_data_tokens,
1185 int num_tokens, u32 ch)
1186{
1187 u32 cnt;
1188
1189 if (unlikely(num_tokens && netif_queue_stopped(priv->ndev)))
1190 netif_start_queue(priv->ndev);
1191 for (cnt = 0; cnt < num_tokens; cnt++) {
1192 struct sk_buff *skb = (struct sk_buff *)net_data_tokens[cnt];
1193 if (skb == NULL)
1194 continue;
1195 priv->net_dev_stats.tx_packets++;
1196 priv->net_dev_stats.tx_bytes += skb->len;
1197 dev_kfree_skb_any(skb);
1198 }
1199 return 0;
1200}
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210static void emac_txch_teardown(struct emac_priv *priv, u32 ch)
1211{
1212 struct device *emac_dev = &priv->ndev->dev;
1213 u32 teardown_cnt = 0xFFFFFFF0;
1214 struct emac_txch *txch = priv->txch[ch];
1215 struct emac_tx_bd __iomem *curr_bd;
1216
1217 while ((emac_read(EMAC_TXCP(ch)) & EMAC_TEARDOWN_VALUE) !=
1218 EMAC_TEARDOWN_VALUE) {
1219
1220 cpu_relax();
1221 --teardown_cnt;
1222 if (0 == teardown_cnt) {
1223 dev_err(emac_dev, "EMAC: TX teardown aborted\n");
1224 break;
1225 }
1226 }
1227 emac_write(EMAC_TXCP(ch), EMAC_TEARDOWN_VALUE);
1228
1229
1230 if (1 == txch->queue_active) {
1231 curr_bd = txch->active_queue_head;
1232 while (curr_bd != NULL) {
1233 emac_net_tx_complete(priv, (void __force *)
1234 &curr_bd->buf_token, 1, ch);
1235 if (curr_bd != txch->active_queue_tail)
1236 curr_bd = curr_bd->next;
1237 else
1238 break;
1239 }
1240 txch->bd_pool_head = txch->active_queue_head;
1241 txch->active_queue_head =
1242 txch->active_queue_tail = NULL;
1243 }
1244}
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254static void emac_stop_txch(struct emac_priv *priv, u32 ch)
1255{
1256 struct emac_txch *txch = priv->txch[ch];
1257
1258 if (txch) {
1259 txch->teardown_pending = 1;
1260 emac_write(EMAC_TXTEARDOWN, 0);
1261 emac_txch_teardown(priv, ch);
1262 txch->teardown_pending = 0;
1263 emac_write(EMAC_TXINTMASKCLEAR, BIT(ch));
1264 }
1265}
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281static int emac_tx_bdproc(struct emac_priv *priv, u32 ch, u32 budget)
1282{
1283 struct device *emac_dev = &priv->ndev->dev;
1284 unsigned long flags;
1285 u32 frame_status;
1286 u32 pkts_processed = 0;
1287 u32 tx_complete_cnt = 0;
1288 struct emac_tx_bd __iomem *curr_bd;
1289 struct emac_txch *txch = priv->txch[ch];
1290 u32 *tx_complete_ptr = txch->tx_complete;
1291
1292 if (unlikely(1 == txch->teardown_pending)) {
1293 if (netif_msg_tx_err(priv) && net_ratelimit()) {
1294 dev_err(emac_dev, "DaVinci EMAC:emac_tx_bdproc: "\
1295 "teardown pending\n");
1296 }
1297 return 0;
1298 }
1299
1300 ++txch->proc_count;
1301 spin_lock_irqsave(&priv->tx_lock, flags);
1302 curr_bd = txch->active_queue_head;
1303 if (NULL == curr_bd) {
1304 emac_write(EMAC_TXCP(ch),
1305 emac_virt_to_phys(txch->last_hw_bdprocessed));
1306 txch->no_active_pkts++;
1307 spin_unlock_irqrestore(&priv->tx_lock, flags);
1308 return 0;
1309 }
1310 BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1311 frame_status = curr_bd->mode;
1312 while ((curr_bd) &&
1313 ((frame_status & EMAC_CPPI_OWNERSHIP_BIT) == 0) &&
1314 (pkts_processed < budget)) {
1315 emac_write(EMAC_TXCP(ch), emac_virt_to_phys(curr_bd));
1316 txch->active_queue_head = curr_bd->next;
1317 if (frame_status & EMAC_CPPI_EOQ_BIT) {
1318 if (curr_bd->next) {
1319 emac_write(EMAC_TXHDP(ch), curr_bd->h_next);
1320 ++txch->mis_queued_packets;
1321 } else {
1322 txch->queue_active = 0;
1323 }
1324 }
1325 *tx_complete_ptr = (u32) curr_bd->buf_token;
1326 ++tx_complete_ptr;
1327 ++tx_complete_cnt;
1328 curr_bd->next = txch->bd_pool_head;
1329 txch->bd_pool_head = curr_bd;
1330 --txch->active_queue_count;
1331 pkts_processed++;
1332 txch->last_hw_bdprocessed = curr_bd;
1333 curr_bd = txch->active_queue_head;
1334 if (curr_bd) {
1335 BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1336 frame_status = curr_bd->mode;
1337 }
1338 }
1339
1340 emac_net_tx_complete(priv,
1341 (void *)&txch->tx_complete[0],
1342 tx_complete_cnt, ch);
1343 spin_unlock_irqrestore(&priv->tx_lock, flags);
1344 return pkts_processed;
1345}
1346
1347#define EMAC_ERR_TX_OUT_OF_BD -1
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359static int emac_send(struct emac_priv *priv, struct emac_netpktobj *pkt, u32 ch)
1360{
1361 unsigned long flags;
1362 struct emac_tx_bd __iomem *curr_bd;
1363 struct emac_txch *txch;
1364 struct emac_netbufobj *buf_list;
1365
1366 txch = priv->txch[ch];
1367 buf_list = pkt->buf_list;
1368
1369
1370 if (pkt->pkt_length < EMAC_DEF_MIN_ETHPKTSIZE) {
1371 buf_list->length += (EMAC_DEF_MIN_ETHPKTSIZE - pkt->pkt_length);
1372 pkt->pkt_length = EMAC_DEF_MIN_ETHPKTSIZE;
1373 }
1374
1375 spin_lock_irqsave(&priv->tx_lock, flags);
1376 curr_bd = txch->bd_pool_head;
1377 if (curr_bd == NULL) {
1378 txch->out_of_tx_bd++;
1379 spin_unlock_irqrestore(&priv->tx_lock, flags);
1380 return EMAC_ERR_TX_OUT_OF_BD;
1381 }
1382
1383 txch->bd_pool_head = curr_bd->next;
1384 curr_bd->buf_token = buf_list->buf_token;
1385
1386 curr_bd->buff_ptr = virt_to_phys(buf_list->data_ptr);
1387 curr_bd->off_b_len = buf_list->length;
1388 curr_bd->h_next = 0;
1389 curr_bd->next = NULL;
1390 curr_bd->mode = (EMAC_CPPI_SOP_BIT | EMAC_CPPI_OWNERSHIP_BIT |
1391 EMAC_CPPI_EOP_BIT | pkt->pkt_length);
1392
1393
1394 BD_CACHE_WRITEBACK_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1395
1396
1397 if (txch->active_queue_head == NULL) {
1398 txch->active_queue_head = curr_bd;
1399 txch->active_queue_tail = curr_bd;
1400 if (1 != txch->queue_active) {
1401 emac_write(EMAC_TXHDP(ch),
1402 emac_virt_to_phys(curr_bd));
1403 txch->queue_active = 1;
1404 }
1405 ++txch->queue_reinit;
1406 } else {
1407 register struct emac_tx_bd __iomem *tail_bd;
1408 register u32 frame_status;
1409
1410 tail_bd = txch->active_queue_tail;
1411 tail_bd->next = curr_bd;
1412 txch->active_queue_tail = curr_bd;
1413 tail_bd = EMAC_VIRT_NOCACHE(tail_bd);
1414 tail_bd->h_next = (int)emac_virt_to_phys(curr_bd);
1415 frame_status = tail_bd->mode;
1416 if (frame_status & EMAC_CPPI_EOQ_BIT) {
1417 emac_write(EMAC_TXHDP(ch), emac_virt_to_phys(curr_bd));
1418 frame_status &= ~(EMAC_CPPI_EOQ_BIT);
1419 tail_bd->mode = frame_status;
1420 ++txch->end_of_queue_add;
1421 }
1422 }
1423 txch->active_queue_count++;
1424 spin_unlock_irqrestore(&priv->tx_lock, flags);
1425 return 0;
1426}
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438static int emac_dev_xmit(struct sk_buff *skb, struct net_device *ndev)
1439{
1440 struct device *emac_dev = &ndev->dev;
1441 int ret_code;
1442 struct emac_netbufobj tx_buf;
1443 struct emac_netpktobj tx_packet;
1444 struct emac_priv *priv = netdev_priv(ndev);
1445
1446
1447 if (unlikely(!priv->link)) {
1448 if (netif_msg_tx_err(priv) && net_ratelimit())
1449 dev_err(emac_dev, "DaVinci EMAC: No link to transmit");
1450 return NETDEV_TX_BUSY;
1451 }
1452
1453
1454
1455
1456
1457
1458 tx_packet.buf_list = &tx_buf;
1459 tx_packet.num_bufs = 1;
1460 tx_packet.pkt_length = skb->len;
1461 tx_packet.pkt_token = (void *)skb;
1462 tx_buf.length = skb->len;
1463 tx_buf.buf_token = (void *)skb;
1464 tx_buf.data_ptr = skb->data;
1465 EMAC_CACHE_WRITEBACK((unsigned long)skb->data, skb->len);
1466 ndev->trans_start = jiffies;
1467 ret_code = emac_send(priv, &tx_packet, EMAC_DEF_TX_CH);
1468 if (unlikely(ret_code != 0)) {
1469 if (ret_code == EMAC_ERR_TX_OUT_OF_BD) {
1470 if (netif_msg_tx_err(priv) && net_ratelimit())
1471 dev_err(emac_dev, "DaVinci EMAC: xmit() fatal"\
1472 " err. Out of TX BD's");
1473 netif_stop_queue(priv->ndev);
1474 }
1475 priv->net_dev_stats.tx_dropped++;
1476 return NETDEV_TX_BUSY;
1477 }
1478
1479 return NETDEV_TX_OK;
1480}
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492static void emac_dev_tx_timeout(struct net_device *ndev)
1493{
1494 struct emac_priv *priv = netdev_priv(ndev);
1495 struct device *emac_dev = &ndev->dev;
1496
1497 if (netif_msg_tx_err(priv))
1498 dev_err(emac_dev, "DaVinci EMAC: xmit timeout, restarting TX");
1499
1500 priv->net_dev_stats.tx_errors++;
1501 emac_int_disable(priv);
1502 emac_stop_txch(priv, EMAC_DEF_TX_CH);
1503 emac_cleanup_txch(priv, EMAC_DEF_TX_CH);
1504 emac_init_txch(priv, EMAC_DEF_TX_CH);
1505 emac_write(EMAC_TXHDP(0), 0);
1506 emac_write(EMAC_TXINTMASKSET, BIT(EMAC_DEF_TX_CH));
1507 emac_int_enable(priv);
1508}
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522static void *emac_net_alloc_rx_buf(struct emac_priv *priv, int buf_size,
1523 void **data_token, u32 ch)
1524{
1525 struct net_device *ndev = priv->ndev;
1526 struct device *emac_dev = &ndev->dev;
1527 struct sk_buff *p_skb;
1528
1529 p_skb = dev_alloc_skb(buf_size);
1530 if (unlikely(NULL == p_skb)) {
1531 if (netif_msg_rx_err(priv) && net_ratelimit())
1532 dev_err(emac_dev, "DaVinci EMAC: failed to alloc skb");
1533 return NULL;
1534 }
1535
1536
1537 p_skb->dev = ndev;
1538 skb_reserve(p_skb, NET_IP_ALIGN);
1539 *data_token = (void *) p_skb;
1540 EMAC_CACHE_WRITEBACK_INVALIDATE((unsigned long)p_skb->data, buf_size);
1541 return p_skb->data;
1542}
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555static int emac_init_rxch(struct emac_priv *priv, u32 ch, char *param)
1556{
1557 struct device *emac_dev = &priv->ndev->dev;
1558 u32 cnt, bd_size;
1559 void __iomem *mem;
1560 struct emac_rx_bd __iomem *curr_bd;
1561 struct emac_rxch *rxch = NULL;
1562
1563 rxch = kzalloc(sizeof(struct emac_rxch), GFP_KERNEL);
1564 if (NULL == rxch) {
1565 dev_err(emac_dev, "DaVinci EMAC: RX Ch mem alloc failed");
1566 return -ENOMEM;
1567 }
1568 priv->rxch[ch] = rxch;
1569 rxch->buf_size = priv->rx_buf_size;
1570 rxch->service_max = EMAC_DEF_RX_MAX_SERVICE;
1571 rxch->queue_active = 0;
1572 rxch->teardown_pending = 0;
1573
1574
1575 for (cnt = 0; cnt < 6; cnt++)
1576 rxch->mac_addr[cnt] = param[cnt];
1577
1578
1579
1580 bd_size = (sizeof(struct emac_rx_bd) + 0xF) & ~0xF;
1581 rxch->num_bd = (priv->ctrl_ram_size >> 1) / bd_size;
1582 rxch->alloc_size = (((bd_size * rxch->num_bd) + 0xF) & ~0xF);
1583 rxch->bd_mem = EMAC_RX_BD_MEM(priv);
1584 __memzero((void __force *)rxch->bd_mem, rxch->alloc_size);
1585 rxch->pkt_queue.buf_list = &rxch->buf_queue;
1586
1587
1588 mem = (void __force __iomem *)
1589 (((u32 __force) rxch->bd_mem + 0xF) & ~0xF);
1590 rxch->active_queue_head = NULL;
1591 rxch->active_queue_tail = mem;
1592 for (cnt = 0; cnt < rxch->num_bd; cnt++) {
1593 curr_bd = mem + (cnt * bd_size);
1594
1595 curr_bd->data_ptr = emac_net_alloc_rx_buf(priv,
1596 rxch->buf_size,
1597 (void __force **)&curr_bd->buf_token,
1598 EMAC_DEF_RX_CH);
1599 if (curr_bd->data_ptr == NULL) {
1600 dev_err(emac_dev, "DaVinci EMAC: RX buf mem alloc " \
1601 "failed for ch %d\n", ch);
1602 kfree(rxch);
1603 return -ENOMEM;
1604 }
1605
1606
1607 curr_bd->h_next = emac_virt_to_phys(rxch->active_queue_head);
1608
1609 curr_bd->buff_ptr = virt_to_phys(curr_bd->data_ptr);
1610 curr_bd->off_b_len = rxch->buf_size;
1611 curr_bd->mode = EMAC_CPPI_OWNERSHIP_BIT;
1612
1613
1614 BD_CACHE_WRITEBACK_INVALIDATE((u32) curr_bd,
1615 EMAC_BD_LENGTH_FOR_CACHE);
1616 curr_bd->next = rxch->active_queue_head;
1617 rxch->active_queue_head = curr_bd;
1618 }
1619
1620
1621
1622
1623
1624 return 0;
1625}
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635static void emac_rxch_teardown(struct emac_priv *priv, u32 ch)
1636{
1637 struct device *emac_dev = &priv->ndev->dev;
1638 u32 teardown_cnt = 0xFFFFFFF0;
1639
1640 while ((emac_read(EMAC_RXCP(ch)) & EMAC_TEARDOWN_VALUE) !=
1641 EMAC_TEARDOWN_VALUE) {
1642
1643 cpu_relax();
1644 --teardown_cnt;
1645 if (0 == teardown_cnt) {
1646 dev_err(emac_dev, "EMAC: RX teardown aborted\n");
1647 break;
1648 }
1649 }
1650 emac_write(EMAC_RXCP(ch), EMAC_TEARDOWN_VALUE);
1651}
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661static void emac_stop_rxch(struct emac_priv *priv, u32 ch)
1662{
1663 struct emac_rxch *rxch = priv->rxch[ch];
1664
1665 if (rxch) {
1666 rxch->teardown_pending = 1;
1667 emac_write(EMAC_RXTEARDOWN, ch);
1668
1669 emac_rxch_teardown(priv, ch);
1670 rxch->teardown_pending = 0;
1671 emac_write(EMAC_RXINTMASKCLEAR, BIT(ch));
1672 }
1673}
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683static void emac_cleanup_rxch(struct emac_priv *priv, u32 ch)
1684{
1685 struct emac_rxch *rxch = priv->rxch[ch];
1686 struct emac_rx_bd __iomem *curr_bd;
1687
1688 if (rxch) {
1689
1690 curr_bd = rxch->active_queue_head;
1691 while (curr_bd) {
1692 if (curr_bd->buf_token) {
1693 dev_kfree_skb_any((struct sk_buff *)\
1694 curr_bd->buf_token);
1695 }
1696 curr_bd = curr_bd->next;
1697 }
1698 if (rxch->bd_mem)
1699 rxch->bd_mem = NULL;
1700 kfree(rxch);
1701 priv->rxch[ch] = NULL;
1702 }
1703}
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715static void emac_set_type0addr(struct emac_priv *priv, u32 ch, char *mac_addr)
1716{
1717 u32 val;
1718 val = ((mac_addr[5] << 8) | (mac_addr[4]));
1719 emac_write(EMAC_MACSRCADDRLO, val);
1720
1721 val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1722 (mac_addr[1] << 8) | (mac_addr[0]));
1723 emac_write(EMAC_MACSRCADDRHI, val);
1724 val = emac_read(EMAC_RXUNICASTSET);
1725 val |= BIT(ch);
1726 emac_write(EMAC_RXUNICASTSET, val);
1727 val = emac_read(EMAC_RXUNICASTCLEAR);
1728 val &= ~BIT(ch);
1729 emac_write(EMAC_RXUNICASTCLEAR, val);
1730}
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742static void emac_set_type1addr(struct emac_priv *priv, u32 ch, char *mac_addr)
1743{
1744 u32 val;
1745 emac_write(EMAC_MACINDEX, ch);
1746 val = ((mac_addr[5] << 8) | mac_addr[4]);
1747 emac_write(EMAC_MACADDRLO, val);
1748 val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1749 (mac_addr[1] << 8) | (mac_addr[0]));
1750 emac_write(EMAC_MACADDRHI, val);
1751 emac_set_type0addr(priv, ch, mac_addr);
1752}
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766static void emac_set_type2addr(struct emac_priv *priv, u32 ch,
1767 char *mac_addr, int index, int match)
1768{
1769 u32 val;
1770 emac_write(EMAC_MACINDEX, index);
1771 val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1772 (mac_addr[1] << 8) | (mac_addr[0]));
1773 emac_write(EMAC_MACADDRHI, val);
1774 val = ((mac_addr[5] << 8) | mac_addr[4] | ((ch & 0x7) << 16) | \
1775 (match << 19) | BIT(20));
1776 emac_write(EMAC_MACADDRLO, val);
1777 emac_set_type0addr(priv, ch, mac_addr);
1778}
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790static void emac_setmac(struct emac_priv *priv, u32 ch, char *mac_addr)
1791{
1792 struct device *emac_dev = &priv->ndev->dev;
1793
1794 if (priv->rx_addr_type == 0) {
1795 emac_set_type0addr(priv, ch, mac_addr);
1796 } else if (priv->rx_addr_type == 1) {
1797 u32 cnt;
1798 for (cnt = 0; cnt < EMAC_MAX_TXRX_CHANNELS; cnt++)
1799 emac_set_type1addr(priv, ch, mac_addr);
1800 } else if (priv->rx_addr_type == 2) {
1801 emac_set_type2addr(priv, ch, mac_addr, ch, 1);
1802 emac_set_type0addr(priv, ch, mac_addr);
1803 } else {
1804 if (netif_msg_drv(priv))
1805 dev_err(emac_dev, "DaVinci EMAC: Wrong addressing\n");
1806 }
1807}
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818static int emac_dev_setmac_addr(struct net_device *ndev, void *addr)
1819{
1820 struct emac_priv *priv = netdev_priv(ndev);
1821 struct emac_rxch *rxch = priv->rxch[EMAC_DEF_RX_CH];
1822 struct device *emac_dev = &priv->ndev->dev;
1823 struct sockaddr *sa = addr;
1824
1825 if (!is_valid_ether_addr(sa->sa_data))
1826 return -EINVAL;
1827
1828
1829 memcpy(priv->mac_addr, sa->sa_data, ndev->addr_len);
1830 memcpy(ndev->dev_addr, sa->sa_data, ndev->addr_len);
1831
1832
1833
1834 if (netif_running(ndev)) {
1835 memcpy(rxch->mac_addr, sa->sa_data, ndev->addr_len);
1836 emac_setmac(priv, EMAC_DEF_RX_CH, rxch->mac_addr);
1837 }
1838
1839 if (netif_msg_drv(priv))
1840 dev_notice(emac_dev, "DaVinci EMAC: emac_dev_setmac_addr %pM\n",
1841 priv->mac_addr);
1842
1843 return 0;
1844}
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859static void emac_addbd_to_rx_queue(struct emac_priv *priv, u32 ch,
1860 struct emac_rx_bd __iomem *curr_bd,
1861 char *buffer, void *buf_token)
1862{
1863 struct emac_rxch *rxch = priv->rxch[ch];
1864
1865
1866 curr_bd->h_next = 0;
1867
1868 curr_bd->buff_ptr = virt_to_phys(buffer);
1869 curr_bd->off_b_len = rxch->buf_size;
1870 curr_bd->mode = EMAC_CPPI_OWNERSHIP_BIT;
1871 curr_bd->next = NULL;
1872 curr_bd->data_ptr = buffer;
1873 curr_bd->buf_token = buf_token;
1874
1875
1876 BD_CACHE_WRITEBACK_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1877 if (rxch->active_queue_head == NULL) {
1878 rxch->active_queue_head = curr_bd;
1879 rxch->active_queue_tail = curr_bd;
1880 if (0 != rxch->queue_active) {
1881 emac_write(EMAC_RXHDP(ch),
1882 emac_virt_to_phys(rxch->active_queue_head));
1883 rxch->queue_active = 1;
1884 }
1885 } else {
1886 struct emac_rx_bd __iomem *tail_bd;
1887 u32 frame_status;
1888
1889 tail_bd = rxch->active_queue_tail;
1890 rxch->active_queue_tail = curr_bd;
1891 tail_bd->next = curr_bd;
1892 tail_bd = EMAC_VIRT_NOCACHE(tail_bd);
1893 tail_bd->h_next = emac_virt_to_phys(curr_bd);
1894 frame_status = tail_bd->mode;
1895 if (frame_status & EMAC_CPPI_EOQ_BIT) {
1896 emac_write(EMAC_RXHDP(ch),
1897 emac_virt_to_phys(curr_bd));
1898 frame_status &= ~(EMAC_CPPI_EOQ_BIT);
1899 tail_bd->mode = frame_status;
1900 ++rxch->end_of_queue_add;
1901 }
1902 }
1903 ++rxch->recycled_bd;
1904}
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916static int emac_net_rx_cb(struct emac_priv *priv,
1917 struct emac_netpktobj *net_pkt_list)
1918{
1919 struct sk_buff *p_skb;
1920 p_skb = (struct sk_buff *)net_pkt_list->pkt_token;
1921
1922 skb_put(p_skb, net_pkt_list->pkt_length);
1923 EMAC_CACHE_INVALIDATE((unsigned long)p_skb->data, p_skb->len);
1924 p_skb->protocol = eth_type_trans(p_skb, priv->ndev);
1925 netif_receive_skb(p_skb);
1926 priv->net_dev_stats.rx_bytes += net_pkt_list->pkt_length;
1927 priv->net_dev_stats.rx_packets++;
1928 return 0;
1929}
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946static int emac_rx_bdproc(struct emac_priv *priv, u32 ch, u32 budget)
1947{
1948 unsigned long flags;
1949 u32 frame_status;
1950 u32 pkts_processed = 0;
1951 char *new_buffer;
1952 struct emac_rx_bd __iomem *curr_bd;
1953 struct emac_rx_bd __iomem *last_bd;
1954 struct emac_netpktobj *curr_pkt, pkt_obj;
1955 struct emac_netbufobj buf_obj;
1956 struct emac_netbufobj *rx_buf_obj;
1957 void *new_buf_token;
1958 struct emac_rxch *rxch = priv->rxch[ch];
1959
1960 if (unlikely(1 == rxch->teardown_pending))
1961 return 0;
1962 ++rxch->proc_count;
1963 spin_lock_irqsave(&priv->rx_lock, flags);
1964 pkt_obj.buf_list = &buf_obj;
1965 curr_pkt = &pkt_obj;
1966 curr_bd = rxch->active_queue_head;
1967 BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1968 frame_status = curr_bd->mode;
1969
1970 while ((curr_bd) &&
1971 ((frame_status & EMAC_CPPI_OWNERSHIP_BIT) == 0) &&
1972 (pkts_processed < budget)) {
1973
1974 new_buffer = emac_net_alloc_rx_buf(priv, rxch->buf_size,
1975 &new_buf_token, EMAC_DEF_RX_CH);
1976 if (unlikely(NULL == new_buffer)) {
1977 ++rxch->out_of_rx_buffers;
1978 goto end_emac_rx_bdproc;
1979 }
1980
1981
1982 rx_buf_obj = &curr_pkt->buf_list[0];
1983 rx_buf_obj->data_ptr = (char *)curr_bd->data_ptr;
1984 rx_buf_obj->length = curr_bd->off_b_len & EMAC_RX_BD_BUF_SIZE;
1985 rx_buf_obj->buf_token = curr_bd->buf_token;
1986 curr_pkt->pkt_token = curr_pkt->buf_list->buf_token;
1987 curr_pkt->num_bufs = 1;
1988 curr_pkt->pkt_length =
1989 (frame_status & EMAC_RX_BD_PKT_LENGTH_MASK);
1990 emac_write(EMAC_RXCP(ch), emac_virt_to_phys(curr_bd));
1991 ++rxch->processed_bd;
1992 last_bd = curr_bd;
1993 curr_bd = last_bd->next;
1994 rxch->active_queue_head = curr_bd;
1995
1996
1997 if (frame_status & EMAC_CPPI_EOQ_BIT) {
1998 if (curr_bd) {
1999 ++rxch->mis_queued_packets;
2000 emac_write(EMAC_RXHDP(ch),
2001 emac_virt_to_phys(curr_bd));
2002 } else {
2003 ++rxch->end_of_queue;
2004 rxch->queue_active = 0;
2005 }
2006 }
2007
2008
2009 emac_addbd_to_rx_queue(priv, ch, last_bd, new_buffer,
2010 new_buf_token);
2011
2012
2013
2014 spin_unlock_irqrestore(&priv->rx_lock, flags);
2015 emac_net_rx_cb(priv, curr_pkt);
2016 spin_lock_irqsave(&priv->rx_lock, flags);
2017 curr_bd = rxch->active_queue_head;
2018 if (curr_bd) {
2019 BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
2020 frame_status = curr_bd->mode;
2021 }
2022 ++pkts_processed;
2023 }
2024
2025end_emac_rx_bdproc:
2026 spin_unlock_irqrestore(&priv->rx_lock, flags);
2027 return pkts_processed;
2028}
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039static int emac_hw_enable(struct emac_priv *priv)
2040{
2041 u32 ch, val, mbp_enable, mac_control;
2042
2043
2044 emac_write(EMAC_SOFTRESET, 1);
2045 while (emac_read(EMAC_SOFTRESET))
2046 cpu_relax();
2047
2048
2049 emac_int_disable(priv);
2050
2051
2052 mac_control =
2053 (((EMAC_DEF_TXPRIO_FIXED) ? (EMAC_MACCONTROL_TXPTYPE) : 0x0) |
2054 ((priv->speed == 1000) ? EMAC_MACCONTROL_GIGABITEN : 0x0) |
2055 ((EMAC_DEF_TXPACING_EN) ? (EMAC_MACCONTROL_TXPACEEN) : 0x0) |
2056 ((priv->duplex == DUPLEX_FULL) ? 0x1 : 0));
2057 emac_write(EMAC_MACCONTROL, mac_control);
2058
2059 mbp_enable =
2060 (((EMAC_DEF_PASS_CRC) ? (EMAC_RXMBP_PASSCRC_MASK) : 0x0) |
2061 ((EMAC_DEF_QOS_EN) ? (EMAC_RXMBP_QOSEN_MASK) : 0x0) |
2062 ((EMAC_DEF_NO_BUFF_CHAIN) ? (EMAC_RXMBP_NOCHAIN_MASK) : 0x0) |
2063 ((EMAC_DEF_MACCTRL_FRAME_EN) ? (EMAC_RXMBP_CMFEN_MASK) : 0x0) |
2064 ((EMAC_DEF_SHORT_FRAME_EN) ? (EMAC_RXMBP_CSFEN_MASK) : 0x0) |
2065 ((EMAC_DEF_ERROR_FRAME_EN) ? (EMAC_RXMBP_CEFEN_MASK) : 0x0) |
2066 ((EMAC_DEF_PROM_EN) ? (EMAC_RXMBP_CAFEN_MASK) : 0x0) |
2067 ((EMAC_DEF_PROM_CH & EMAC_RXMBP_CHMASK) << \
2068 EMAC_RXMBP_PROMCH_SHIFT) |
2069 ((EMAC_DEF_BCAST_EN) ? (EMAC_RXMBP_BROADEN_MASK) : 0x0) |
2070 ((EMAC_DEF_BCAST_CH & EMAC_RXMBP_CHMASK) << \
2071 EMAC_RXMBP_BROADCH_SHIFT) |
2072 ((EMAC_DEF_MCAST_EN) ? (EMAC_RXMBP_MULTIEN_MASK) : 0x0) |
2073 ((EMAC_DEF_MCAST_CH & EMAC_RXMBP_CHMASK) << \
2074 EMAC_RXMBP_MULTICH_SHIFT));
2075 emac_write(EMAC_RXMBPENABLE, mbp_enable);
2076 emac_write(EMAC_RXMAXLEN, (EMAC_DEF_MAX_FRAME_SIZE &
2077 EMAC_RX_MAX_LEN_MASK));
2078 emac_write(EMAC_RXBUFFEROFFSET, (EMAC_DEF_BUFFER_OFFSET &
2079 EMAC_RX_BUFFER_OFFSET_MASK));
2080 emac_write(EMAC_RXFILTERLOWTHRESH, 0);
2081 emac_write(EMAC_RXUNICASTCLEAR, EMAC_RX_UNICAST_CLEAR_ALL);
2082 priv->rx_addr_type = (emac_read(EMAC_MACCONFIG) >> 8) & 0xFF;
2083
2084 val = emac_read(EMAC_TXCONTROL);
2085 val |= EMAC_TX_CONTROL_TX_ENABLE_VAL;
2086 emac_write(EMAC_TXCONTROL, val);
2087 val = emac_read(EMAC_RXCONTROL);
2088 val |= EMAC_RX_CONTROL_RX_ENABLE_VAL;
2089 emac_write(EMAC_RXCONTROL, val);
2090 emac_write(EMAC_MACINTMASKSET, EMAC_MAC_HOST_ERR_INTMASK_VAL);
2091
2092 for (ch = 0; ch < EMAC_DEF_MAX_TX_CH; ch++) {
2093 emac_write(EMAC_TXHDP(ch), 0);
2094 emac_write(EMAC_TXINTMASKSET, BIT(ch));
2095 }
2096 for (ch = 0; ch < EMAC_DEF_MAX_RX_CH; ch++) {
2097 struct emac_rxch *rxch = priv->rxch[ch];
2098 emac_setmac(priv, ch, rxch->mac_addr);
2099 emac_write(EMAC_RXINTMASKSET, BIT(ch));
2100 rxch->queue_active = 1;
2101 emac_write(EMAC_RXHDP(ch),
2102 emac_virt_to_phys(rxch->active_queue_head));
2103 }
2104
2105
2106 val = emac_read(EMAC_MACCONTROL);
2107 val |= (EMAC_MACCONTROL_GMIIEN);
2108 emac_write(EMAC_MACCONTROL, val);
2109
2110
2111 napi_enable(&priv->napi);
2112 emac_int_enable(priv);
2113 return 0;
2114
2115}
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130static int emac_poll(struct napi_struct *napi, int budget)
2131{
2132 unsigned int mask;
2133 struct emac_priv *priv = container_of(napi, struct emac_priv, napi);
2134 struct net_device *ndev = priv->ndev;
2135 struct device *emac_dev = &ndev->dev;
2136 u32 status = 0;
2137 u32 num_pkts = 0;
2138
2139
2140 status = emac_read(EMAC_MACINVECTOR);
2141
2142 mask = EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC;
2143
2144 if (priv->version == EMAC_VERSION_2)
2145 mask = EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC;
2146
2147 if (status & mask) {
2148 num_pkts = emac_tx_bdproc(priv, EMAC_DEF_TX_CH,
2149 EMAC_DEF_TX_MAX_SERVICE);
2150 }
2151
2152 if (num_pkts)
2153 return budget;
2154
2155 mask = EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC;
2156
2157 if (priv->version == EMAC_VERSION_2)
2158 mask = EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC;
2159
2160 if (status & mask) {
2161 num_pkts = emac_rx_bdproc(priv, EMAC_DEF_RX_CH, budget);
2162 }
2163
2164 if (num_pkts < budget) {
2165 napi_complete(napi);
2166 emac_int_enable(priv);
2167 }
2168
2169 mask = EMAC_DM644X_MAC_IN_VECTOR_HOST_INT;
2170 if (priv->version == EMAC_VERSION_2)
2171 mask = EMAC_DM646X_MAC_IN_VECTOR_HOST_INT;
2172
2173 if (unlikely(status & mask)) {
2174 u32 ch, cause;
2175 dev_err(emac_dev, "DaVinci EMAC: Fatal Hardware Error\n");
2176 netif_stop_queue(ndev);
2177 napi_disable(&priv->napi);
2178
2179 status = emac_read(EMAC_MACSTATUS);
2180 cause = ((status & EMAC_MACSTATUS_TXERRCODE_MASK) >>
2181 EMAC_MACSTATUS_TXERRCODE_SHIFT);
2182 if (cause) {
2183 ch = ((status & EMAC_MACSTATUS_TXERRCH_MASK) >>
2184 EMAC_MACSTATUS_TXERRCH_SHIFT);
2185 if (net_ratelimit()) {
2186 dev_err(emac_dev, "TX Host error %s on ch=%d\n",
2187 &emac_txhost_errcodes[cause][0], ch);
2188 }
2189 }
2190 cause = ((status & EMAC_MACSTATUS_RXERRCODE_MASK) >>
2191 EMAC_MACSTATUS_RXERRCODE_SHIFT);
2192 if (cause) {
2193 ch = ((status & EMAC_MACSTATUS_RXERRCH_MASK) >>
2194 EMAC_MACSTATUS_RXERRCH_SHIFT);
2195 if (netif_msg_hw(priv) && net_ratelimit())
2196 dev_err(emac_dev, "RX Host error %s on ch=%d\n",
2197 &emac_rxhost_errcodes[cause][0], ch);
2198 }
2199 }
2200
2201 return num_pkts;
2202}
2203
2204#ifdef CONFIG_NET_POLL_CONTROLLER
2205
2206
2207
2208
2209
2210
2211
2212void emac_poll_controller(struct net_device *ndev)
2213{
2214 struct emac_priv *priv = netdev_priv(ndev);
2215
2216 emac_int_disable(priv);
2217 emac_irq(ndev->irq, ndev);
2218 emac_int_enable(priv);
2219}
2220#endif
2221
2222
2223
2224
2225#define MDIO_WAIT_FOR_USER_ACCESS\
2226 while ((emac_mdio_read((MDIO_USERACCESS(0))) &\
2227 MDIO_USERACCESS_GO) != 0)
2228
2229static int emac_mii_read(struct mii_bus *bus, int phy_id, int phy_reg)
2230{
2231 unsigned int phy_data = 0;
2232 unsigned int phy_control;
2233
2234
2235 MDIO_WAIT_FOR_USER_ACCESS;
2236
2237 phy_control = (MDIO_USERACCESS_GO |
2238 MDIO_USERACCESS_READ |
2239 ((phy_reg << 21) & MDIO_USERACCESS_REGADR) |
2240 ((phy_id << 16) & MDIO_USERACCESS_PHYADR) |
2241 (phy_data & MDIO_USERACCESS_DATA));
2242 emac_mdio_write(MDIO_USERACCESS(0), phy_control);
2243
2244
2245 MDIO_WAIT_FOR_USER_ACCESS;
2246
2247 return emac_mdio_read(MDIO_USERACCESS(0)) & MDIO_USERACCESS_DATA;
2248
2249}
2250
2251static int emac_mii_write(struct mii_bus *bus, int phy_id,
2252 int phy_reg, u16 phy_data)
2253{
2254
2255 unsigned int control;
2256
2257
2258 MDIO_WAIT_FOR_USER_ACCESS;
2259
2260 control = (MDIO_USERACCESS_GO |
2261 MDIO_USERACCESS_WRITE |
2262 ((phy_reg << 21) & MDIO_USERACCESS_REGADR) |
2263 ((phy_id << 16) & MDIO_USERACCESS_PHYADR) |
2264 (phy_data & MDIO_USERACCESS_DATA));
2265 emac_mdio_write(MDIO_USERACCESS(0), control);
2266
2267 return 0;
2268}
2269
2270static int emac_mii_reset(struct mii_bus *bus)
2271{
2272 unsigned int clk_div;
2273 int mdio_bus_freq = emac_bus_frequency;
2274
2275 if (mdio_max_freq & mdio_bus_freq)
2276 clk_div = ((mdio_bus_freq / mdio_max_freq) - 1);
2277 else
2278 clk_div = 0xFF;
2279
2280 clk_div &= MDIO_CONTROL_CLKDIV;
2281
2282
2283 emac_mdio_write(MDIO_CONTROL, (clk_div | MDIO_CONTROL_ENABLE));
2284
2285 return 0;
2286
2287}
2288
2289static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, PHY_POLL };
2290
2291
2292
2293static struct mii_bus *emac_mii;
2294
2295static void emac_adjust_link(struct net_device *ndev)
2296{
2297 struct emac_priv *priv = netdev_priv(ndev);
2298 struct phy_device *phydev = priv->phydev;
2299 unsigned long flags;
2300 int new_state = 0;
2301
2302 spin_lock_irqsave(&priv->lock, flags);
2303
2304 if (phydev->link) {
2305
2306 if (phydev->duplex != priv->duplex) {
2307 new_state = 1;
2308 priv->duplex = phydev->duplex;
2309 }
2310 if (phydev->speed != priv->speed) {
2311 new_state = 1;
2312 priv->speed = phydev->speed;
2313 }
2314 if (!priv->link) {
2315 new_state = 1;
2316 priv->link = 1;
2317 }
2318
2319 } else if (priv->link) {
2320 new_state = 1;
2321 priv->link = 0;
2322 priv->speed = 0;
2323 priv->duplex = ~0;
2324 }
2325 if (new_state) {
2326 emac_update_phystatus(priv);
2327 phy_print_status(priv->phydev);
2328 }
2329
2330 spin_unlock_irqrestore(&priv->lock, flags);
2331}
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347static int emac_devioctl(struct net_device *ndev, struct ifreq *ifrq, int cmd)
2348{
2349 dev_warn(&ndev->dev, "DaVinci EMAC: ioctl not supported\n");
2350
2351 if (!(netif_running(ndev)))
2352 return -EINVAL;
2353
2354
2355
2356 return -EOPNOTSUPP;
2357}
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369static int emac_dev_open(struct net_device *ndev)
2370{
2371 struct device *emac_dev = &ndev->dev;
2372 u32 rc, cnt, ch;
2373 int phy_addr;
2374 struct resource *res;
2375 int q, m;
2376 int i = 0;
2377 int k = 0;
2378 struct emac_priv *priv = netdev_priv(ndev);
2379
2380 netif_carrier_off(ndev);
2381 for (cnt = 0; cnt <= ETH_ALEN; cnt++)
2382 ndev->dev_addr[cnt] = priv->mac_addr[cnt];
2383
2384
2385 priv->rx_buf_size = EMAC_DEF_MAX_FRAME_SIZE + NET_IP_ALIGN;
2386
2387
2388 for (ch = 0; ch < EMAC_MAX_TXRX_CHANNELS; ch++) {
2389 emac_write(EMAC_TXHDP(ch), 0);
2390 emac_write(EMAC_RXHDP(ch), 0);
2391 emac_write(EMAC_RXHDP(ch), 0);
2392 emac_write(EMAC_RXINTMASKCLEAR, EMAC_INT_MASK_CLEAR);
2393 emac_write(EMAC_TXINTMASKCLEAR, EMAC_INT_MASK_CLEAR);
2394 }
2395 priv->mac_hash1 = 0;
2396 priv->mac_hash2 = 0;
2397 emac_write(EMAC_MACHASH1, 0);
2398 emac_write(EMAC_MACHASH2, 0);
2399
2400
2401 rc = emac_init_txch(priv, EMAC_DEF_TX_CH);
2402 if (0 != rc) {
2403 dev_err(emac_dev, "DaVinci EMAC: emac_init_txch() failed");
2404 return rc;
2405 }
2406 rc = emac_init_rxch(priv, EMAC_DEF_RX_CH, priv->mac_addr);
2407 if (0 != rc) {
2408 dev_err(emac_dev, "DaVinci EMAC: emac_init_rxch() failed");
2409 return rc;
2410 }
2411
2412
2413
2414 while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
2415 for (i = res->start; i <= res->end; i++) {
2416 if (request_irq(i, emac_irq, IRQF_DISABLED,
2417 ndev->name, ndev))
2418 goto rollback;
2419 }
2420 k++;
2421 }
2422
2423
2424 emac_hw_enable(priv);
2425
2426
2427 priv->phydev = NULL;
2428 if (priv->phy_mask) {
2429 emac_mii_reset(priv->mii_bus);
2430 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
2431 if (priv->mii_bus->phy_map[phy_addr]) {
2432 priv->phydev = priv->mii_bus->phy_map[phy_addr];
2433 break;
2434 }
2435 }
2436
2437 if (!priv->phydev) {
2438 printk(KERN_ERR "%s: no PHY found\n", ndev->name);
2439 return -1;
2440 }
2441
2442 priv->phydev = phy_connect(ndev, dev_name(&priv->phydev->dev),
2443 &emac_adjust_link, 0, PHY_INTERFACE_MODE_MII);
2444
2445 if (IS_ERR(priv->phydev)) {
2446 printk(KERN_ERR "%s: Could not attach to PHY\n",
2447 ndev->name);
2448 return PTR_ERR(priv->phydev);
2449 }
2450
2451 priv->link = 0;
2452 priv->speed = 0;
2453 priv->duplex = ~0;
2454
2455 printk(KERN_INFO "%s: attached PHY driver [%s] "
2456 "(mii_bus:phy_addr=%s, id=%x)\n", ndev->name,
2457 priv->phydev->drv->name, dev_name(&priv->phydev->dev),
2458 priv->phydev->phy_id);
2459 } else{
2460
2461 priv->link = 1;
2462 priv->speed = SPEED_100;
2463 priv->duplex = DUPLEX_FULL;
2464 emac_update_phystatus(priv);
2465 }
2466
2467 if (!netif_running(ndev))
2468 emac_dump_regs(priv);
2469
2470 if (netif_msg_drv(priv))
2471 dev_notice(emac_dev, "DaVinci EMAC: Opened %s\n", ndev->name);
2472
2473 if (priv->phy_mask)
2474 phy_start(priv->phydev);
2475
2476 return 0;
2477
2478rollback:
2479
2480 dev_err(emac_dev, "DaVinci EMAC: request_irq() failed");
2481
2482 for (q = k; k >= 0; k--) {
2483 for (m = i; m >= res->start; m--)
2484 free_irq(m, ndev);
2485 res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k-1);
2486 m = res->end;
2487 }
2488 return -EBUSY;
2489}
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500static int emac_dev_stop(struct net_device *ndev)
2501{
2502 struct resource *res;
2503 int i = 0;
2504 int irq_num;
2505 struct emac_priv *priv = netdev_priv(ndev);
2506 struct device *emac_dev = &ndev->dev;
2507
2508
2509 netif_stop_queue(ndev);
2510 napi_disable(&priv->napi);
2511
2512 netif_carrier_off(ndev);
2513 emac_int_disable(priv);
2514 emac_stop_txch(priv, EMAC_DEF_TX_CH);
2515 emac_stop_rxch(priv, EMAC_DEF_RX_CH);
2516 emac_cleanup_txch(priv, EMAC_DEF_TX_CH);
2517 emac_cleanup_rxch(priv, EMAC_DEF_RX_CH);
2518 emac_write(EMAC_SOFTRESET, 1);
2519
2520 if (priv->phydev)
2521 phy_disconnect(priv->phydev);
2522
2523
2524 while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, i))) {
2525 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2526 free_irq(irq_num, priv->ndev);
2527 i++;
2528 }
2529
2530 if (netif_msg_drv(priv))
2531 dev_notice(emac_dev, "DaVinci EMAC: %s stopped\n", ndev->name);
2532
2533 return 0;
2534}
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544static struct net_device_stats *emac_dev_getnetstats(struct net_device *ndev)
2545{
2546 struct emac_priv *priv = netdev_priv(ndev);
2547 u32 mac_control;
2548 u32 stats_clear_mask;
2549
2550
2551
2552 mac_control = emac_read(EMAC_MACCONTROL);
2553
2554 if (mac_control & EMAC_MACCONTROL_GMIIEN)
2555 stats_clear_mask = EMAC_STATS_CLR_MASK;
2556 else
2557 stats_clear_mask = 0;
2558
2559 priv->net_dev_stats.multicast += emac_read(EMAC_RXMCASTFRAMES);
2560 emac_write(EMAC_RXMCASTFRAMES, stats_clear_mask);
2561
2562 priv->net_dev_stats.collisions += (emac_read(EMAC_TXCOLLISION) +
2563 emac_read(EMAC_TXSINGLECOLL) +
2564 emac_read(EMAC_TXMULTICOLL));
2565 emac_write(EMAC_TXCOLLISION, stats_clear_mask);
2566 emac_write(EMAC_TXSINGLECOLL, stats_clear_mask);
2567 emac_write(EMAC_TXMULTICOLL, stats_clear_mask);
2568
2569 priv->net_dev_stats.rx_length_errors += (emac_read(EMAC_RXOVERSIZED) +
2570 emac_read(EMAC_RXJABBER) +
2571 emac_read(EMAC_RXUNDERSIZED));
2572 emac_write(EMAC_RXOVERSIZED, stats_clear_mask);
2573 emac_write(EMAC_RXJABBER, stats_clear_mask);
2574 emac_write(EMAC_RXUNDERSIZED, stats_clear_mask);
2575
2576 priv->net_dev_stats.rx_over_errors += (emac_read(EMAC_RXSOFOVERRUNS) +
2577 emac_read(EMAC_RXMOFOVERRUNS));
2578 emac_write(EMAC_RXSOFOVERRUNS, stats_clear_mask);
2579 emac_write(EMAC_RXMOFOVERRUNS, stats_clear_mask);
2580
2581 priv->net_dev_stats.rx_fifo_errors += emac_read(EMAC_RXDMAOVERRUNS);
2582 emac_write(EMAC_RXDMAOVERRUNS, stats_clear_mask);
2583
2584 priv->net_dev_stats.tx_carrier_errors +=
2585 emac_read(EMAC_TXCARRIERSENSE);
2586 emac_write(EMAC_TXCARRIERSENSE, stats_clear_mask);
2587
2588 priv->net_dev_stats.tx_fifo_errors = emac_read(EMAC_TXUNDERRUN);
2589 emac_write(EMAC_TXUNDERRUN, stats_clear_mask);
2590
2591 return &priv->net_dev_stats;
2592}
2593
2594static const struct net_device_ops emac_netdev_ops = {
2595 .ndo_open = emac_dev_open,
2596 .ndo_stop = emac_dev_stop,
2597 .ndo_start_xmit = emac_dev_xmit,
2598 .ndo_set_multicast_list = emac_dev_mcast_set,
2599 .ndo_set_mac_address = emac_dev_setmac_addr,
2600 .ndo_do_ioctl = emac_devioctl,
2601 .ndo_tx_timeout = emac_dev_tx_timeout,
2602 .ndo_get_stats = emac_dev_getnetstats,
2603#ifdef CONFIG_NET_POLL_CONTROLLER
2604 .ndo_poll_controller = emac_poll_controller,
2605#endif
2606};
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616static int __devinit davinci_emac_probe(struct platform_device *pdev)
2617{
2618 int rc = 0;
2619 struct resource *res;
2620 struct net_device *ndev;
2621 struct emac_priv *priv;
2622 unsigned long size;
2623 struct emac_platform_data *pdata;
2624 struct device *emac_dev;
2625
2626
2627 emac_clk = clk_get(&pdev->dev, NULL);
2628 if (IS_ERR(emac_clk)) {
2629 printk(KERN_ERR "DaVinci EMAC: Failed to get EMAC clock\n");
2630 return -EBUSY;
2631 }
2632 emac_bus_frequency = clk_get_rate(emac_clk);
2633
2634
2635 ndev = alloc_etherdev(sizeof(struct emac_priv));
2636 if (!ndev) {
2637 printk(KERN_ERR "DaVinci EMAC: Error allocating net_device\n");
2638 clk_put(emac_clk);
2639 return -ENOMEM;
2640 }
2641
2642 platform_set_drvdata(pdev, ndev);
2643 priv = netdev_priv(ndev);
2644 priv->pdev = pdev;
2645 priv->ndev = ndev;
2646 priv->msg_enable = netif_msg_init(debug_level, DAVINCI_EMAC_DEBUG);
2647
2648 spin_lock_init(&priv->tx_lock);
2649 spin_lock_init(&priv->rx_lock);
2650 spin_lock_init(&priv->lock);
2651
2652 pdata = pdev->dev.platform_data;
2653 if (!pdata) {
2654 printk(KERN_ERR "DaVinci EMAC: No platfrom data\n");
2655 return -ENODEV;
2656 }
2657
2658
2659 memcpy(priv->mac_addr, pdata->mac_addr, 6);
2660 priv->phy_mask = pdata->phy_mask;
2661 priv->rmii_en = pdata->rmii_en;
2662 priv->version = pdata->version;
2663 emac_dev = &ndev->dev;
2664
2665 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2666 if (!res) {
2667 dev_err(emac_dev, "DaVinci EMAC: Error getting res\n");
2668 rc = -ENOENT;
2669 goto probe_quit;
2670 }
2671
2672 priv->emac_base_phys = res->start + pdata->ctrl_reg_offset;
2673 size = res->end - res->start + 1;
2674 if (!request_mem_region(res->start, size, ndev->name)) {
2675 dev_err(emac_dev, "DaVinci EMAC: failed request_mem_region() \
2676 for regs\n");
2677 rc = -ENXIO;
2678 goto probe_quit;
2679 }
2680
2681 priv->remap_addr = ioremap(res->start, size);
2682 if (!priv->remap_addr) {
2683 dev_err(emac_dev, "Unable to map IO\n");
2684 rc = -ENOMEM;
2685 release_mem_region(res->start, size);
2686 goto probe_quit;
2687 }
2688 priv->emac_base = priv->remap_addr + pdata->ctrl_reg_offset;
2689 ndev->base_addr = (unsigned long)priv->remap_addr;
2690
2691 priv->ctrl_base = priv->remap_addr + pdata->ctrl_mod_reg_offset;
2692 priv->ctrl_ram_size = pdata->ctrl_ram_size;
2693 priv->emac_ctrl_ram = priv->remap_addr + pdata->ctrl_ram_offset;
2694
2695 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2696 if (!res) {
2697 dev_err(emac_dev, "DaVinci EMAC: Error getting irq res\n");
2698 rc = -ENOENT;
2699 goto no_irq_res;
2700 }
2701 ndev->irq = res->start;
2702
2703 if (!is_valid_ether_addr(priv->mac_addr)) {
2704
2705 random_ether_addr(priv->mac_addr);
2706 printk(KERN_WARNING "%s: using random MAC addr: %pM\n",
2707 __func__, priv->mac_addr);
2708 }
2709
2710 ndev->netdev_ops = &emac_netdev_ops;
2711 SET_ETHTOOL_OPS(ndev, ðtool_ops);
2712 netif_napi_add(ndev, &priv->napi, emac_poll, EMAC_POLL_WEIGHT);
2713
2714
2715 SET_NETDEV_DEV(ndev, &pdev->dev);
2716 rc = register_netdev(ndev);
2717 if (rc) {
2718 dev_err(emac_dev, "DaVinci EMAC: Error in register_netdev\n");
2719 rc = -ENODEV;
2720 goto netdev_reg_err;
2721 }
2722
2723 clk_enable(emac_clk);
2724
2725
2726 emac_mii = mdiobus_alloc();
2727 if (emac_mii == NULL) {
2728 dev_err(emac_dev, "DaVinci EMAC: Error allocating mii_bus\n");
2729 rc = -ENOMEM;
2730 goto mdio_alloc_err;
2731 }
2732
2733 priv->mii_bus = emac_mii;
2734 emac_mii->name = "emac-mii",
2735 emac_mii->read = emac_mii_read,
2736 emac_mii->write = emac_mii_write,
2737 emac_mii->reset = emac_mii_reset,
2738 emac_mii->irq = mii_irqs,
2739 emac_mii->phy_mask = ~(priv->phy_mask);
2740 emac_mii->parent = &pdev->dev;
2741 emac_mii->priv = priv->remap_addr + pdata->mdio_reg_offset;
2742 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%x", priv->pdev->id);
2743 mdio_max_freq = pdata->mdio_max_freq;
2744 emac_mii->reset(emac_mii);
2745
2746
2747 rc = mdiobus_register(emac_mii);
2748 if (rc)
2749 goto mdiobus_quit;
2750
2751 if (netif_msg_probe(priv)) {
2752 dev_notice(emac_dev, "DaVinci EMAC Probe found device "\
2753 "(regs: %p, irq: %d)\n",
2754 (void *)priv->emac_base_phys, ndev->irq);
2755 }
2756 return 0;
2757
2758mdiobus_quit:
2759 mdiobus_free(emac_mii);
2760
2761netdev_reg_err:
2762mdio_alloc_err:
2763no_irq_res:
2764 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2765 release_mem_region(res->start, res->end - res->start + 1);
2766 iounmap(priv->remap_addr);
2767
2768probe_quit:
2769 clk_put(emac_clk);
2770 free_netdev(ndev);
2771 return rc;
2772}
2773
2774
2775
2776
2777
2778
2779
2780
2781static int __devexit davinci_emac_remove(struct platform_device *pdev)
2782{
2783 struct resource *res;
2784 struct net_device *ndev = platform_get_drvdata(pdev);
2785 struct emac_priv *priv = netdev_priv(ndev);
2786
2787 dev_notice(&ndev->dev, "DaVinci EMAC: davinci_emac_remove()\n");
2788
2789 platform_set_drvdata(pdev, NULL);
2790 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2791 mdiobus_unregister(priv->mii_bus);
2792 mdiobus_free(priv->mii_bus);
2793
2794 release_mem_region(res->start, res->end - res->start + 1);
2795
2796 unregister_netdev(ndev);
2797 free_netdev(ndev);
2798 iounmap(priv->remap_addr);
2799
2800 clk_disable(emac_clk);
2801 clk_put(emac_clk);
2802
2803 return 0;
2804}
2805
2806
2807
2808
2809
2810
2811
2812static struct platform_driver davinci_emac_driver = {
2813 .driver = {
2814 .name = "davinci_emac",
2815 .owner = THIS_MODULE,
2816 },
2817 .probe = davinci_emac_probe,
2818 .remove = __devexit_p(davinci_emac_remove),
2819};
2820
2821
2822
2823
2824
2825
2826
2827static int __init davinci_emac_init(void)
2828{
2829 return platform_driver_register(&davinci_emac_driver);
2830}
2831late_initcall(davinci_emac_init);
2832
2833
2834
2835
2836
2837
2838
2839static void __exit davinci_emac_exit(void)
2840{
2841 platform_driver_unregister(&davinci_emac_driver);
2842}
2843module_exit(davinci_emac_exit);
2844
2845MODULE_LICENSE("GPL");
2846MODULE_AUTHOR("DaVinci EMAC Maintainer: Anant Gole <anantgole@ti.com>");
2847MODULE_AUTHOR("DaVinci EMAC Maintainer: Chaithrika U S <chaithrika@ti.com>");
2848MODULE_DESCRIPTION("DaVinci EMAC Ethernet driver");
2849