blob: 79ce8e857eabd1fdc11da74ea0b3ec095becdcbb [file] [log] [blame]
Anant Golea6286ee2009-05-18 15:19:01 -07001/*
2 * DaVinci Ethernet Medium Access Controller
3 *
4 * DaVinci EMAC is based upon CPPI 3.0 TI DMA engine
5 *
6 * Copyright (C) 2009 Texas Instruments.
7 *
8 * ---------------------------------------------------------------------------
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * ---------------------------------------------------------------------------
24 * History:
25 * 0-5 A number of folks worked on this driver in bits and pieces but the major
26 * contribution came from Suraj Iyer and Anant Gole
27 * 6.0 Anant Gole - rewrote the driver as per Linux conventions
28 * 6.1 Chaithrika U S - added support for Gigabit and RMII features,
29 * PHY layer usage
30 */
31
32/** Pending Items in this driver:
33 * 1. Use Linux cache infrastcture for DMA'ed memory (dma_xxx functions)
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/* Netif debug messages possible */
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/* version info */
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/* Configuration items */
100#define EMAC_DEF_PASS_CRC (0) /* Do not pass CRC upto frames */
101#define EMAC_DEF_QOS_EN (0) /* EMAC proprietary QoS disabled */
102#define EMAC_DEF_NO_BUFF_CHAIN (0) /* No buffer chain */
103#define EMAC_DEF_MACCTRL_FRAME_EN (0) /* Discard Maccontrol frames */
104#define EMAC_DEF_SHORT_FRAME_EN (0) /* Discard short frames */
105#define EMAC_DEF_ERROR_FRAME_EN (0) /* Discard error frames */
106#define EMAC_DEF_PROM_EN (0) /* Promiscous disabled */
107#define EMAC_DEF_PROM_CH (0) /* Promiscous channel is 0 */
108#define EMAC_DEF_BCAST_EN (1) /* Broadcast enabled */
109#define EMAC_DEF_BCAST_CH (0) /* Broadcast channel is 0 */
110#define EMAC_DEF_MCAST_EN (1) /* Multicast enabled */
111#define EMAC_DEF_MCAST_CH (0) /* Multicast channel is 0 */
112
113#define EMAC_DEF_TXPRIO_FIXED (1) /* TX Priority is fixed */
114#define EMAC_DEF_TXPACING_EN (0) /* TX pacing NOT supported*/
115
116#define EMAC_DEF_BUFFER_OFFSET (0) /* Buffer offset to DMA (future) */
117#define EMAC_DEF_MIN_ETHPKTSIZE (60) /* Minimum ethernet pkt size */
118#define EMAC_DEF_MAX_FRAME_SIZE (1500 + 14 + 4 + 4)
119#define EMAC_DEF_TX_CH (0) /* Default 0th channel */
120#define EMAC_DEF_RX_CH (0) /* Default 0th channel */
121#define EMAC_DEF_MDIO_TICK_MS (10) /* typically 1 tick=1 ms) */
122#define EMAC_DEF_MAX_TX_CH (1) /* Max TX channels configured */
123#define EMAC_DEF_MAX_RX_CH (1) /* Max RX channels configured */
124#define EMAC_POLL_WEIGHT (64) /* Default NAPI poll weight */
125
126/* Buffer descriptor parameters */
127#define EMAC_DEF_TX_MAX_SERVICE (32) /* TX max service BD's */
128#define EMAC_DEF_RX_MAX_SERVICE (64) /* should = netdev->weight */
129
130/* EMAC register related defines */
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/* RX MBP register bit positions */
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/* EMAC register definitions/bit maps used */
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/* EMAC mac_control register */
chaithrika@ti.com69ef9692009-10-01 10:25:19 +0000167#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)
Anant Golea6286ee2009-05-18 15:19:01 -0700172#define EMAC_MACCONTROL_RMIISPEED_MASK BIT(15)
173
174/* GIGABIT MODE related bits */
Anant Golea6286ee2009-05-18 15:19:01 -0700175#define EMAC_DM646X_MACCONTORL_GIG BIT(7)
176#define EMAC_DM646X_MACCONTORL_GIGFORCE BIT(17)
177
178/* EMAC mac_status register */
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/* EMAC RX register masks */
189#define EMAC_RX_MAX_LEN_MASK (0xFFFF)
190#define EMAC_RX_BUFFER_OFFSET_MASK (0xFFFF)
191
192/* MAC_IN_VECTOR (0x180) register bit fields */
chaithrika@ti.com69ef9692009-10-01 10:25:19 +0000193#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)
Anant Golea6286ee2009-05-18 15:19:01 -0700197
198/** NOTE:: For DM646x the IN_VECTOR has changed */
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)
Sriram43c2ed82009-09-24 19:15:18 +0000201#define EMAC_DM646X_MAC_IN_VECTOR_HOST_INT BIT(26)
202#define EMAC_DM646X_MAC_IN_VECTOR_STATPEND_INT BIT(27)
203
Anant Golea6286ee2009-05-18 15:19:01 -0700204/* CPPI bit positions */
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) /* only CPPI bytes */
213#define EMAC_RX_BD_PKT_LENGTH_MASK (0xFFFF)
214
215/* Max hardware defines */
216#define EMAC_MAX_TXRX_CHANNELS (8) /* Max hardware channels */
217#define EMAC_DEF_MAX_MULTICAST_ADDRESSES (64) /* Max mcast addr's */
218
219/* EMAC Peripheral Device Register Memory Layout structure */
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/* EMAC HDP and Completion registors */
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/* EMAC statistics registers */
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/* EMAC DM644x control registers */
303#define EMAC_CTRL_EWCTL (0x4)
304#define EMAC_CTRL_EWINTTCNT (0x8)
305
306/* EMAC MDIO related */
307/* Mask & Control defines */
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/* EMAC DM646X control module registers */
326#define EMAC_DM646X_CMRXINTEN (0x14)
327#define EMAC_DM646X_CMTXINTEN (0x18)
328
329/* EMAC EOI codes for C0 */
330#define EMAC_DM646X_MAC_EOI_C0_RXEN (0x01)
331#define EMAC_DM646X_MAC_EOI_C0_TXEN (0x02)
332
Sriram0fe74632009-10-07 02:44:30 +0000333/* EMAC Stats Clear Mask */
334#define EMAC_STATS_CLR_MASK (0xFFFFFFFF)
335
Anant Golea6286ee2009-05-18 15:19:01 -0700336/** net_buf_obj: EMAC network bufferdata structure
337 *
338 * EMAC network buffer data structure
339 */
340struct emac_netbufobj {
341 void *buf_token;
342 char *data_ptr;
343 int length;
344};
345
346/** net_pkt_obj: EMAC network packet data structure
347 *
348 * EMAC network packet data structure - supports buffer list (for future)
349 */
350struct emac_netpktobj {
351 void *pkt_token; /* data token may hold tx/rx chan id */
352 struct emac_netbufobj *buf_list; /* array of network buffer objects */
353 int num_bufs;
354 int pkt_length;
355};
356
357/** emac_tx_bd: EMAC TX Buffer descriptor data structure
358 *
359 * EMAC TX Buffer descriptor data structure
360 */
361struct emac_tx_bd {
362 int h_next;
363 int buff_ptr;
364 int off_b_len;
365 int mode; /* SOP, EOP, ownership, EOQ, teardown,Qstarv, length */
366 struct emac_tx_bd __iomem *next;
367 void *buf_token;
368};
369
370/** emac_txch: EMAC TX Channel data structure
371 *
372 * EMAC TX Channel data structure
373 */
374struct emac_txch {
375 /* Config related */
376 u32 num_bd;
377 u32 service_max;
378
379 /* CPPI specific */
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 /** statistics */
391 u32 proc_count; /* TX: # of times emac_tx_bdproc is called */
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; /* IRQ when there were no packets to process */
397 u32 active_queue_count;
398};
399
400/** emac_rx_bd: EMAC RX Buffer descriptor data structure
401 *
402 * EMAC RX Buffer descriptor data structure
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/** emac_rxch: EMAC RX Channel data structure
415 *
416 * EMAC RX Channel data structure
417 */
418struct emac_rxch {
419 /* configuration info */
420 u32 num_bd;
421 u32 service_max;
422 u32 buf_size;
423 char mac_addr[6];
424
425 /** CPPI specific */
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 /* packet and buffer objects */
435 struct emac_netpktobj pkt_queue;
436 struct emac_netbufobj buf_queue;
437
438 /** statistics */
439 u32 proc_count; /* number of times emac_rx_bdproc is called */
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/* emac_priv: EMAC private data structure
451 *
452 * EMAC adapter private data structure
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; /* 1=link on, 0=link off */
471 u32 speed; /* 0=Auto Neg, 1=No PHY, 10,100, 1000 - mbps */
472 u32 duplex; /* Link duplex: 0=Half, 1=Full */
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 /* periodic timer required for MDIO polling */
483 struct timer_list periodic_timer;
484 u32 periodic_ticks;
485 u32 timer_active;
486 u32 phy_mask;
487 /* mii_bus,phy members */
488 struct mii_bus *mii_bus;
489 struct phy_device *phydev;
490 spinlock_t lock;
491};
492
493/* clock frequency for EMAC */
494static struct clk *emac_clk;
495static unsigned long emac_bus_frequency;
496static unsigned long mdio_max_freq;
497
498/* EMAC internal utility function */
499static inline u32 emac_virt_to_phys(void __iomem *addr)
500{
501 return (u32 __force) io_v2p(addr);
502}
503
504/* Cache macros - Packet buffers would be from skb pool which is cached */
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/* DM644x does not have BD's in cached memory - so no cache functions */
514#define BD_CACHE_INVALIDATE(addr, size)
515#define BD_CACHE_WRITEBACK(addr, size)
516#define BD_CACHE_WRITEBACK_INVALIDATE(addr, size)
517
518/* EMAC TX Host Error description strings */
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/* EMAC RX Host Error description strings */
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/* Helper macros */
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 * emac_dump_regs: Dump important EMAC registers to debug terminal
547 * @priv: The DaVinci EMAC private adapter structure
548 *
549 * Executes ethtool set cmd & sets phy mode
550 *
551 */
552static void emac_dump_regs(struct emac_priv *priv)
553{
554 struct device *emac_dev = &priv->ndev->dev;
555
556 /* Print important registers in EMAC */
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 * EMAC MDIO/Phy Functionality
650 *************************************************************************/
651/**
652 * emac_get_drvinfo: Get EMAC driver information
653 * @ndev: The DaVinci EMAC network adapter
654 * @info: ethtool info structure containing name and version
655 *
656 * Returns EMAC driver information (name and version)
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 * emac_get_settings: Get EMAC settings
668 * @ndev: The DaVinci EMAC network adapter
669 * @ecmd: ethtool command
670 *
671 * Executes ethool get command
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 * emac_set_settings: Set EMAC settings
687 * @ndev: The DaVinci EMAC network adapter
688 * @ecmd: ethtool command
689 *
690 * Executes ethool set command
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 * ethtool_ops: DaVinci EMAC Ethtool structure
705 *
706 * Ethtool support for EMAC adapter
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 * emac_update_phystatus: Update Phy status
718 * @priv: The DaVinci EMAC private adapter structure
719 *
720 * Updates phy status and takes action for network queue if required
721 * based upon link status
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 /* We get called only if link has changed (speed/duplex/status) */
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);
chaithrika@ti.com69ef9692009-10-01 10:25:19 +0000750 mac_control |= (EMAC_DM646X_MACCONTORL_GIG |
Anant Golea6286ee2009-05-18 15:19:01 -0700751 EMAC_DM646X_MACCONTORL_GIGFORCE);
752 } else {
753 /* Clear the GIG bit and GIGFORCE bit */
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 /* Update mac_control if changed */
764 emac_write(EMAC_MACCONTROL, mac_control);
765
766 if (priv->link) {
767 /* link ON */
768 if (!netif_carrier_ok(ndev))
769 netif_carrier_on(ndev);
770 /* reactivate the transmit queue if it is stopped */
771 if (netif_running(ndev) && netif_queue_stopped(ndev))
772 netif_wake_queue(ndev);
773 } else {
774 /* link OFF */
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 * hash_get: Calculate hash value from mac address
784 * @addr: mac address to delete from hash table
785 *
786 * Calculates hash value from mac address
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 * hash_add: Hash function to add mac addr from hash table
810 * @priv: The DaVinci EMAC private adapter structure
811 * mac_addr: mac address to delete from hash table
812 *
813 * Adds mac address to the internal hash table
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 /* set the hash bit only if not previously set */
833 if (priv->multicast_hash_cnt[hash_value] == 0) {
834 rc = 1; /* hash value changed */
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 /* incr counter for num of mcast addr's mapped to "this" hash bit */
845 ++priv->multicast_hash_cnt[hash_value];
846
847 return rc;
848}
849
850/**
851 * hash_del: Hash function to delete mac addr from hash table
852 * @priv: The DaVinci EMAC private adapter structure
853 * mac_addr: mac address to delete from hash table
854 *
855 * Removes mac address from the internal hash table
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 /* dec cntr for num of mcast addr's mapped to this hash bit */
866 --priv->multicast_hash_cnt[hash_value];
867 }
868
869 /* if counter still > 0, at least one multicast address refers
870 * to this hash bit. so return 0 */
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 /* return 1 to indicate change in mac_hash registers reqd */
883 return 1;
884}
885
886/* EMAC multicast operation */
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 * emac_add_mcast: Set multicast address in the EMAC adapter (Internal)
894 * @priv: The DaVinci EMAC private adapter structure
895 * @action: multicast operation to perform
896 * mac_addr: mac address to set
897 *
898 * Set multicast addresses in EMAC adapter - internal function
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 /* write to the hardware only if the register status chances */
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 * emac_dev_mcast_set: Set multicast address in the EMAC adapter
942 * @ndev: The DaVinci EMAC network adapter
943 *
944 * Set multicast addresses in EMAC adapter
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 /* program multicast address list into EMAC hardware */
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 /* Set mbp config register */
979 emac_write(EMAC_RXMBPENABLE, mbp_enable);
980}
981
982/*************************************************************************
983 * EMAC Hardware manipulation
984 *************************************************************************/
985
986/**
987 * emac_int_disable: Disable EMAC module interrupt (from adapter)
988 * @priv: The DaVinci EMAC private adapter structure
989 *
990 * Disable EMAC interrupt on the adapter
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 /* Program C0_Int_En to zero to turn off
1001 * interrupts to the CPU */
1002 emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0x0);
1003 emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0x0);
1004 /* NOTE: Rx Threshold and Misc interrupts are not disabled */
1005
1006 local_irq_restore(flags);
1007
1008 } else {
1009 /* Set DM644x control registers for interrupt control */
1010 emac_ctrl_write(EMAC_CTRL_EWCTL, 0x0);
1011 }
1012}
1013
1014/**
1015 * emac_int_enable: Enable EMAC module interrupt (from adapter)
1016 * @priv: The DaVinci EMAC private adapter structure
1017 *
1018 * Enable EMAC interrupt on the adapter
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 /* In addition to turning on interrupt Enable, we need
1028 * ack by writing appropriate values to the EOI
1029 * register */
1030
1031 /* NOTE: Rx Threshold and Misc interrupts are not enabled */
1032
1033 /* ack rxen only then a new pulse will be generated */
1034 emac_write(EMAC_DM646X_MACEOIVECTOR,
1035 EMAC_DM646X_MAC_EOI_C0_RXEN);
1036
1037 /* ack txen- only then a new pulse will be generated */
1038 emac_write(EMAC_DM646X_MACEOIVECTOR,
1039 EMAC_DM646X_MAC_EOI_C0_TXEN);
1040
1041 } else {
1042 /* Set DM644x control registers for interrupt control */
1043 emac_ctrl_write(EMAC_CTRL_EWCTL, 0x1);
1044 }
1045}
1046
1047/**
1048 * emac_irq: EMAC interrupt handler
1049 * @irq: interrupt number
1050 * @dev_id: EMAC network adapter data structure ptr
1051 *
1052 * EMAC Interrupt handler - we only schedule NAPI and not process any packets
1053 * here. EVen the interrupt status is checked (TX/RX/Err) in NAPI poll function
1054 *
1055 * Returns interrupt handled condition
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 /* we are closing down, so dont process anything */
1068 }
1069 return IRQ_HANDLED;
1070}
1071
1072/** EMAC on-chip buffer descriptor memory
1073 *
1074 * WARNING: Please note that the on chip memory is used for both TX and RX
1075 * buffer descriptor queues and is equally divided between TX and RX desc's
1076 * If the number of TX or RX descriptors change this memory pointers need
1077 * to be adjusted. If external memory is allocated then these pointers can
1078 * pointer to the memory
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 * emac_init_txch: TX channel initialization
1087 * @priv: The DaVinci EMAC private adapter structure
1088 * @ch: RX channel number
1089 *
1090 * Called during device init to setup a TX channel (allocate buffer desc
1091 * create free pool and keep ready for transmission
1092 *
1093 * Returns success(0) or mem alloc failures error code
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 /* allocate memory for TX CPPI channel on a 4 byte boundry */
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 /* allocate buffer descriptor pool align every BD on four word
1125 * boundry for future requirements */
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 /* alloc TX BD memory */
1131 txch->bd_mem = EMAC_TX_BD_MEM(priv);
1132 __memzero((void __force *)txch->bd_mem, txch->alloc_size);
1133
1134 /* initialize the BD linked list */
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 /* reset statistics counters */
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 * emac_cleanup_txch: Book-keep function to clean TX channel resources
1154 * @priv: The DaVinci EMAC private adapter structure
1155 * @ch: TX channel number
1156 *
1157 * Called to clean up TX channel resources
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 * emac_net_tx_complete: TX packet completion function
1175 * @priv: The DaVinci EMAC private adapter structure
1176 * @net_data_tokens: packet token - skb pointer
1177 * @num_tokens: number of skb's to free
1178 * @ch: TX channel number
1179 *
1180 * Frees the skb once packet is transmitted
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 * emac_txch_teardown: TX channel teardown
1204 * @priv: The DaVinci EMAC private adapter structure
1205 * @ch: TX channel number
1206 *
1207 * Called to teardown TX channel
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; /* Some high value */
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 /* wait till tx teardown complete */
1220 cpu_relax(); /* TODO: check if this helps ... */
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 /* process sent packets and return skb's to upper layer */
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 * emac_stop_txch: Stop TX channel operation
1248 * @priv: The DaVinci EMAC private adapter structure
1249 * @ch: TX channel number
1250 *
1251 * Called to stop TX channel operation
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 * emac_tx_bdproc: TX buffer descriptor (packet) processing
1269 * @priv: The DaVinci EMAC private adapter structure
1270 * @ch: TX channel number to process buffer descriptors for
1271 * @budget: number of packets allowed to process
1272 * @pending: indication to caller that packets are pending to process
1273 *
1274 * Processes TX buffer descriptors after packets are transmitted - checks
1275 * ownership bit on the TX * descriptor and requeues it to free pool & frees
1276 * the SKB buffer. Only "budget" number of packets are processed and
1277 * indication of pending packets provided to the caller
1278 *
1279 * Returns number of packets processed
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; /* dont handle any pkt completions */
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) { /* misqueued packet */
1319 emac_write(EMAC_TXHDP(ch), curr_bd->h_next);
1320 ++txch->mis_queued_packets;
1321 } else {
1322 txch->queue_active = 0; /* end of queue */
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 } /* end of pkt processing loop */
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 * emac_send: EMAC Transmit function (internal)
1351 * @priv: The DaVinci EMAC private adapter structure
1352 * @pkt: packet pointer (contains skb ptr)
1353 * @ch: TX channel number
1354 *
1355 * Called by the transmit function to queue the packet in EMAC hardware queue
1356 *
1357 * Returns success(0) or error code (typically out of desc's)
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; /* get handle to the buffer array */
1368
1369 /* check packet size and pad if short */
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 /* FIXME buff_ptr = dma_map_single(... data_ptr ...) */
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 /* flush the packet from cache if write back cache is present */
1394 BD_CACHE_WRITEBACK_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1395
1396 /* send the packet */
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 * emac_dev_xmit: EMAC Transmit function
1430 * @skb: SKB pointer
1431 * @ndev: The DaVinci EMAC network adapter
1432 *
1433 * Called by the system to transmit a packet - we queue the packet in
1434 * EMAC hardware transmit queue
1435 *
1436 * Returns success(NETDEV_TX_OK) or error code (typically out of desc's)
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; /* buffer obj-only single frame support */
1443 struct emac_netpktobj tx_packet; /* packet object */
1444 struct emac_priv *priv = netdev_priv(ndev);
1445
1446 /* If no link, return */
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 /* Build the buffer and packet objects - Since only single fragment is
1454 * supported, need not set length and token in both packet & object.
1455 * Doing so for completeness sake & to show that this needs to be done
1456 * in multifragment case
1457 */
1458 tx_packet.buf_list = &tx_buf;
1459 tx_packet.num_bufs = 1; /* only single fragment supported */
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 * emac_dev_tx_timeout: EMAC Transmit timeout function
1484 * @ndev: The DaVinci EMAC network adapter
1485 *
1486 * Called when system detects that a skb timeout period has expired
1487 * potentially due to a fault in the adapter in not being able to send
1488 * it out on the wire. We teardown the TX channel assuming a hardware
1489 * error and re-initialize the TX channel for hardware operation
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 * emac_net_alloc_rx_buf: Allocate a skb for RX
1512 * @priv: The DaVinci EMAC private adapter structure
1513 * @buf_size: size of SKB data buffer to allocate
1514 * @data_token: data token returned (skb handle for storing in buffer desc)
1515 * @ch: RX channel number
1516 *
1517 * Called during RX channel setup - allocates skb buffer of required size
1518 * and provides the skb handle and allocated buffer data pointer to caller
1519 *
1520 * Returns skb data pointer or 0 on failure to alloc skb
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 /* set device pointer in skb and reserve space for extra bytes */
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 * emac_init_rxch: RX channel initialization
1546 * @priv: The DaVinci EMAC private adapter structure
1547 * @ch: RX channel number
1548 * @param: mac address for RX channel
1549 *
1550 * Called during device init to setup a RX channel (allocate buffers and
1551 * buffer descriptors, create queue and keep ready for reception
1552 *
1553 * Returns success(0) or mem alloc failures error code
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 /* save mac address */
1575 for (cnt = 0; cnt < 6; cnt++)
1576 rxch->mac_addr[cnt] = param[cnt];
1577
1578 /* allocate buffer descriptor pool align every BD on four word
1579 * boundry for future requirements */
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 /* allocate RX buffer and initialize the BD linked list */
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 /* for future use the last parameter contains the BD ptr */
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 /* populate the hardware descriptor */
1607 curr_bd->h_next = emac_virt_to_phys(rxch->active_queue_head);
1608 /* FIXME buff_ptr = dma_map_single(... data_ptr ...) */
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 /* write back to hardware memory */
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 /* At this point rxCppi->activeQueueHead points to the first
1621 RX BD ready to be given to RX HDP and rxch->active_queue_tail
1622 points to the last RX BD
1623 */
1624 return 0;
1625}
1626
1627/**
1628 * emac_rxch_teardown: RX channel teardown
1629 * @priv: The DaVinci EMAC private adapter structure
1630 * @ch: RX channel number
1631 *
1632 * Called during device stop to teardown RX channel
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; /* Some high value */
1639
1640 while ((emac_read(EMAC_RXCP(ch)) & EMAC_TEARDOWN_VALUE) !=
1641 EMAC_TEARDOWN_VALUE) {
1642 /* wait till tx teardown complete */
1643 cpu_relax(); /* TODO: check if this helps ... */
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 * emac_stop_rxch: Stop RX channel operation
1655 * @priv: The DaVinci EMAC private adapter structure
1656 * @ch: RX channel number
1657 *
1658 * Called during device stop to stop RX channel operation
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 /* wait for teardown complete */
1669 emac_rxch_teardown(priv, ch);
1670 rxch->teardown_pending = 0;
1671 emac_write(EMAC_RXINTMASKCLEAR, BIT(ch));
1672 }
1673}
1674
1675/**
1676 * emac_cleanup_rxch: Book-keep function to clean RX channel resources
1677 * @priv: The DaVinci EMAC private adapter structure
1678 * @ch: RX channel number
1679 *
1680 * Called during device stop to clean up RX channel resources
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 /* free the receive buffers previously allocated */
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 * emac_set_type0addr: Set EMAC Type0 mac address
1707 * @priv: The DaVinci EMAC private adapter structure
1708 * @ch: RX channel number
1709 * @mac_addr: MAC address to set in device
1710 *
1711 * Called internally to set Type0 mac address of the adapter (Device)
1712 *
1713 * Returns success (0) or appropriate error code (none as of now)
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 * emac_set_type1addr: Set EMAC Type1 mac address
1734 * @priv: The DaVinci EMAC private adapter structure
1735 * @ch: RX channel number
1736 * @mac_addr: MAC address to set in device
1737 *
1738 * Called internally to set Type1 mac address of the adapter (Device)
1739 *
1740 * Returns success (0) or appropriate error code (none as of now)
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 * emac_set_type2addr: Set EMAC Type2 mac address
1756 * @priv: The DaVinci EMAC private adapter structure
1757 * @ch: RX channel number
1758 * @mac_addr: MAC address to set in device
1759 * @index: index into RX address entries
1760 * @match: match parameter for RX address matching logic
1761 *
1762 * Called internally to set Type2 mac address of the adapter (Device)
1763 *
1764 * Returns success (0) or appropriate error code (none as of now)
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 * emac_setmac: Set mac address in the adapter (internal function)
1782 * @priv: The DaVinci EMAC private adapter structure
1783 * @ch: RX channel number
1784 * @mac_addr: MAC address to set in device
1785 *
1786 * Called internally to set the mac address of the adapter (Device)
1787 *
1788 * Returns success (0) or appropriate error code (none as of now)
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 * emac_dev_setmac_addr: Set mac address in the adapter
1811 * @ndev: The DaVinci EMAC network adapter
1812 * @addr: MAC address to set in device
1813 *
1814 * Called by the system to set the mac address of the adapter (Device)
1815 *
1816 * Returns success (0) or appropriate error code (none as of now)
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;
Anant Golea6286ee2009-05-18 15:19:01 -07001824
Pablo Bitton64c81652009-07-07 19:11:10 -07001825 if (!is_valid_ether_addr(sa->sa_data))
1826 return -EINVAL;
1827
Anant Golea6286ee2009-05-18 15:19:01 -07001828 /* Store mac addr in priv and rx channel and set it in EMAC hw */
1829 memcpy(priv->mac_addr, sa->sa_data, ndev->addr_len);
Anant Golea6286ee2009-05-18 15:19:01 -07001830 memcpy(ndev->dev_addr, sa->sa_data, ndev->addr_len);
Pablo Bitton64c81652009-07-07 19:11:10 -07001831
1832 /* If the interface is down - rxch is NULL. */
1833 /* MAC address is configured only after the interface is enabled. */
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 }
Anant Golea6286ee2009-05-18 15:19:01 -07001838
1839 if (netif_msg_drv(priv))
Chaithrika U S5c726162009-06-03 21:54:29 -07001840 dev_notice(emac_dev, "DaVinci EMAC: emac_dev_setmac_addr %pM\n",
1841 priv->mac_addr);
Anant Golea6286ee2009-05-18 15:19:01 -07001842
1843 return 0;
1844}
1845
1846/**
1847 * emac_addbd_to_rx_queue: Recycle RX buffer descriptor
1848 * @priv: The DaVinci EMAC private adapter structure
1849 * @ch: RX channel number to process buffer descriptors for
1850 * @curr_bd: current buffer descriptor
1851 * @buffer: buffer pointer for descriptor
1852 * @buf_token: buffer token (stores skb information)
1853 *
1854 * Prepares the recycled buffer descriptor and addes it to hardware
1855 * receive queue - if queue empty this descriptor becomes the head
1856 * else addes the descriptor to end of queue
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 /* populate the hardware descriptor */
1866 curr_bd->h_next = 0;
1867 /* FIXME buff_ptr = dma_map_single(... buffer ...) */
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 /* write back */
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 * emac_net_rx_cb: Prepares packet and sends to upper layer
1908 * @priv: The DaVinci EMAC private adapter structure
1909 * @net_pkt_list: Network packet list (received packets)
1910 *
1911 * Invalidates packet buffer memory and sends the received packet to upper
1912 * layer
1913 *
1914 * Returns success or appropriate error code (none as of now)
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 /* set length of packet */
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);
Anant Golea6286ee2009-05-18 15:19:01 -07001925 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 * emac_rx_bdproc: RX buffer descriptor (packet) processing
1933 * @priv: The DaVinci EMAC private adapter structure
1934 * @ch: RX channel number to process buffer descriptors for
1935 * @budget: number of packets allowed to process
1936 * @pending: indication to caller that packets are pending to process
1937 *
1938 * Processes RX buffer descriptors - checks ownership bit on the RX buffer
1939 * descriptor, sends the receive packet to upper layer, allocates a new SKB
1940 * and recycles the buffer descriptor (requeues it in hardware RX queue).
1941 * Only "budget" number of packets are processed and indication of pending
1942 * packets provided to the caller.
1943 *
1944 * Returns number of packets processed (and indication of pending packets)
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 /* populate received packet data structure */
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 /* check if end of RX queue ? */
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 /* recycle BD */
2009 emac_addbd_to_rx_queue(priv, ch, last_bd, new_buffer,
2010 new_buf_token);
2011
2012 /* return the packet to the user - BD ptr passed in
2013 * last parameter for potential *future* use */
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 * emac_hw_enable: Enable EMAC hardware for packet transmission/reception
2032 * @priv: The DaVinci EMAC private adapter structure
2033 *
2034 * Enables EMAC hardware for packet processing - enables PHY, enables RX
2035 * for packet reception and enables device interrupts and then NAPI
2036 *
2037 * Returns success (0) or appropriate error code (none right now)
2038 */
2039static int emac_hw_enable(struct emac_priv *priv)
2040{
2041 u32 ch, val, mbp_enable, mac_control;
2042
2043 /* Soft reset */
2044 emac_write(EMAC_SOFTRESET, 1);
2045 while (emac_read(EMAC_SOFTRESET))
2046 cpu_relax();
2047
2048 /* Disable interrupt & Set pacing for more interrupts initially */
2049 emac_int_disable(priv);
2050
2051 /* Full duplex enable bit set when auto negotiation happens */
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 /* Enable MII */
2106 val = emac_read(EMAC_MACCONTROL);
chaithrika@ti.com69ef9692009-10-01 10:25:19 +00002107 val |= (EMAC_MACCONTROL_GMIIEN);
Anant Golea6286ee2009-05-18 15:19:01 -07002108 emac_write(EMAC_MACCONTROL, val);
2109
2110 /* Enable NAPI and interrupts */
2111 napi_enable(&priv->napi);
2112 emac_int_enable(priv);
2113 return 0;
2114
2115}
2116
2117/**
2118 * emac_poll: EMAC NAPI Poll function
2119 * @ndev: The DaVinci EMAC network adapter
2120 * @budget: Number of receive packets to process (as told by NAPI layer)
2121 *
2122 * NAPI Poll function implemented to process packets as per budget. We check
2123 * the type of interrupt on the device and accordingly call the TX or RX
2124 * packet processing functions. We follow the budget for RX processing and
2125 * also put a cap on number of TX pkts processed through config param. The
2126 * NAPI schedule function is called if more packets pending.
2127 *
2128 * Returns number of packets received (in most cases; else TX pkts - rarely)
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 if (!netif_running(ndev))
2140 return 0;
2141
2142 /* Check interrupt vectors and call packet processing */
2143 status = emac_read(EMAC_MACINVECTOR);
2144
2145 mask = EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC;
2146
2147 if (priv->version == EMAC_VERSION_2)
2148 mask = EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC;
2149
2150 if (status & mask) {
2151 num_pkts = emac_tx_bdproc(priv, EMAC_DEF_TX_CH,
2152 EMAC_DEF_TX_MAX_SERVICE);
2153 } /* TX processing */
2154
2155 if (num_pkts)
2156 return budget;
2157
2158 mask = EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC;
2159
2160 if (priv->version == EMAC_VERSION_2)
2161 mask = EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC;
2162
2163 if (status & mask) {
2164 num_pkts = emac_rx_bdproc(priv, EMAC_DEF_RX_CH, budget);
2165 } /* RX processing */
2166
2167 if (num_pkts < budget) {
2168 napi_complete(napi);
2169 emac_int_enable(priv);
2170 }
2171
Sriram43c2ed82009-09-24 19:15:18 +00002172 mask = EMAC_DM644X_MAC_IN_VECTOR_HOST_INT;
2173 if (priv->version == EMAC_VERSION_2)
2174 mask = EMAC_DM646X_MAC_IN_VECTOR_HOST_INT;
2175
2176 if (unlikely(status & mask)) {
Anant Golea6286ee2009-05-18 15:19:01 -07002177 u32 ch, cause;
2178 dev_err(emac_dev, "DaVinci EMAC: Fatal Hardware Error\n");
2179 netif_stop_queue(ndev);
2180 napi_disable(&priv->napi);
2181
2182 status = emac_read(EMAC_MACSTATUS);
2183 cause = ((status & EMAC_MACSTATUS_TXERRCODE_MASK) >>
2184 EMAC_MACSTATUS_TXERRCODE_SHIFT);
2185 if (cause) {
2186 ch = ((status & EMAC_MACSTATUS_TXERRCH_MASK) >>
2187 EMAC_MACSTATUS_TXERRCH_SHIFT);
2188 if (net_ratelimit()) {
2189 dev_err(emac_dev, "TX Host error %s on ch=%d\n",
2190 &emac_txhost_errcodes[cause][0], ch);
2191 }
2192 }
2193 cause = ((status & EMAC_MACSTATUS_RXERRCODE_MASK) >>
2194 EMAC_MACSTATUS_RXERRCODE_SHIFT);
2195 if (cause) {
2196 ch = ((status & EMAC_MACSTATUS_RXERRCH_MASK) >>
2197 EMAC_MACSTATUS_RXERRCH_SHIFT);
2198 if (netif_msg_hw(priv) && net_ratelimit())
2199 dev_err(emac_dev, "RX Host error %s on ch=%d\n",
2200 &emac_rxhost_errcodes[cause][0], ch);
2201 }
2202 } /* Host error processing */
2203
2204 return num_pkts;
2205}
2206
2207#ifdef CONFIG_NET_POLL_CONTROLLER
2208/**
2209 * emac_poll_controller: EMAC Poll controller function
2210 * @ndev: The DaVinci EMAC network adapter
2211 *
2212 * Polled functionality used by netconsole and others in non interrupt mode
2213 *
2214 */
2215void emac_poll_controller(struct net_device *ndev)
2216{
2217 struct emac_priv *priv = netdev_priv(ndev);
2218
2219 emac_int_disable(priv);
Tonyliuc8ee5532009-11-04 05:45:02 -08002220 emac_irq(ndev->irq, ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07002221 emac_int_enable(priv);
2222}
2223#endif
2224
2225/* PHY/MII bus related */
2226
2227/* Wait until mdio is ready for next command */
2228#define MDIO_WAIT_FOR_USER_ACCESS\
2229 while ((emac_mdio_read((MDIO_USERACCESS(0))) &\
2230 MDIO_USERACCESS_GO) != 0)
2231
2232static int emac_mii_read(struct mii_bus *bus, int phy_id, int phy_reg)
2233{
2234 unsigned int phy_data = 0;
2235 unsigned int phy_control;
2236
2237 /* Wait until mdio is ready for next command */
2238 MDIO_WAIT_FOR_USER_ACCESS;
2239
2240 phy_control = (MDIO_USERACCESS_GO |
2241 MDIO_USERACCESS_READ |
2242 ((phy_reg << 21) & MDIO_USERACCESS_REGADR) |
2243 ((phy_id << 16) & MDIO_USERACCESS_PHYADR) |
2244 (phy_data & MDIO_USERACCESS_DATA));
2245 emac_mdio_write(MDIO_USERACCESS(0), phy_control);
2246
2247 /* Wait until mdio is ready for next command */
2248 MDIO_WAIT_FOR_USER_ACCESS;
2249
2250 return emac_mdio_read(MDIO_USERACCESS(0)) & MDIO_USERACCESS_DATA;
2251
2252}
2253
2254static int emac_mii_write(struct mii_bus *bus, int phy_id,
2255 int phy_reg, u16 phy_data)
2256{
2257
2258 unsigned int control;
2259
2260 /* until mdio is ready for next command */
2261 MDIO_WAIT_FOR_USER_ACCESS;
2262
2263 control = (MDIO_USERACCESS_GO |
2264 MDIO_USERACCESS_WRITE |
2265 ((phy_reg << 21) & MDIO_USERACCESS_REGADR) |
2266 ((phy_id << 16) & MDIO_USERACCESS_PHYADR) |
2267 (phy_data & MDIO_USERACCESS_DATA));
2268 emac_mdio_write(MDIO_USERACCESS(0), control);
2269
2270 return 0;
2271}
2272
2273static int emac_mii_reset(struct mii_bus *bus)
2274{
2275 unsigned int clk_div;
2276 int mdio_bus_freq = emac_bus_frequency;
2277
2278 if (mdio_max_freq & mdio_bus_freq)
2279 clk_div = ((mdio_bus_freq / mdio_max_freq) - 1);
2280 else
2281 clk_div = 0xFF;
2282
2283 clk_div &= MDIO_CONTROL_CLKDIV;
2284
2285 /* Set enable and clock divider in MDIOControl */
2286 emac_mdio_write(MDIO_CONTROL, (clk_div | MDIO_CONTROL_ENABLE));
2287
2288 return 0;
2289
2290}
2291
2292static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, PHY_POLL };
2293
2294/* emac_driver: EMAC MII bus structure */
2295
2296static struct mii_bus *emac_mii;
2297
2298static void emac_adjust_link(struct net_device *ndev)
2299{
2300 struct emac_priv *priv = netdev_priv(ndev);
2301 struct phy_device *phydev = priv->phydev;
2302 unsigned long flags;
2303 int new_state = 0;
2304
2305 spin_lock_irqsave(&priv->lock, flags);
2306
2307 if (phydev->link) {
2308 /* check the mode of operation - full/half duplex */
2309 if (phydev->duplex != priv->duplex) {
2310 new_state = 1;
2311 priv->duplex = phydev->duplex;
2312 }
2313 if (phydev->speed != priv->speed) {
2314 new_state = 1;
2315 priv->speed = phydev->speed;
2316 }
2317 if (!priv->link) {
2318 new_state = 1;
2319 priv->link = 1;
2320 }
2321
2322 } else if (priv->link) {
2323 new_state = 1;
2324 priv->link = 0;
2325 priv->speed = 0;
2326 priv->duplex = ~0;
2327 }
2328 if (new_state) {
2329 emac_update_phystatus(priv);
2330 phy_print_status(priv->phydev);
2331 }
2332
2333 spin_unlock_irqrestore(&priv->lock, flags);
2334}
2335
2336/*************************************************************************
2337 * Linux Driver Model
2338 *************************************************************************/
2339
2340/**
2341 * emac_devioctl: EMAC adapter ioctl
2342 * @ndev: The DaVinci EMAC network adapter
2343 * @ifrq: request parameter
2344 * @cmd: command parameter
2345 *
2346 * EMAC driver ioctl function
2347 *
2348 * Returns success(0) or appropriate error code
2349 */
2350static int emac_devioctl(struct net_device *ndev, struct ifreq *ifrq, int cmd)
2351{
2352 dev_warn(&ndev->dev, "DaVinci EMAC: ioctl not supported\n");
2353
2354 if (!(netif_running(ndev)))
2355 return -EINVAL;
2356
2357 /* TODO: Add phy read and write and private statistics get feature */
2358
2359 return -EOPNOTSUPP;
2360}
2361
2362/**
2363 * emac_dev_open: EMAC device open
2364 * @ndev: The DaVinci EMAC network adapter
2365 *
2366 * Called when system wants to start the interface. We init TX/RX channels
2367 * and enable the hardware for packet reception/transmission and start the
2368 * network queue.
2369 *
2370 * Returns 0 for a successful open, or appropriate error code
2371 */
2372static int emac_dev_open(struct net_device *ndev)
2373{
2374 struct device *emac_dev = &ndev->dev;
2375 u32 rc, cnt, ch;
2376 int phy_addr;
2377 struct resource *res;
2378 int q, m;
2379 int i = 0;
2380 int k = 0;
2381 struct emac_priv *priv = netdev_priv(ndev);
2382
2383 netif_carrier_off(ndev);
2384 for (cnt = 0; cnt <= ETH_ALEN; cnt++)
2385 ndev->dev_addr[cnt] = priv->mac_addr[cnt];
2386
2387 /* Configuration items */
2388 priv->rx_buf_size = EMAC_DEF_MAX_FRAME_SIZE + NET_IP_ALIGN;
2389
2390 /* Clear basic hardware */
2391 for (ch = 0; ch < EMAC_MAX_TXRX_CHANNELS; ch++) {
2392 emac_write(EMAC_TXHDP(ch), 0);
2393 emac_write(EMAC_RXHDP(ch), 0);
2394 emac_write(EMAC_RXHDP(ch), 0);
2395 emac_write(EMAC_RXINTMASKCLEAR, EMAC_INT_MASK_CLEAR);
2396 emac_write(EMAC_TXINTMASKCLEAR, EMAC_INT_MASK_CLEAR);
2397 }
2398 priv->mac_hash1 = 0;
2399 priv->mac_hash2 = 0;
2400 emac_write(EMAC_MACHASH1, 0);
2401 emac_write(EMAC_MACHASH2, 0);
2402
2403 /* multi ch not supported - open 1 TX, 1RX ch by default */
2404 rc = emac_init_txch(priv, EMAC_DEF_TX_CH);
2405 if (0 != rc) {
2406 dev_err(emac_dev, "DaVinci EMAC: emac_init_txch() failed");
2407 return rc;
2408 }
2409 rc = emac_init_rxch(priv, EMAC_DEF_RX_CH, priv->mac_addr);
2410 if (0 != rc) {
2411 dev_err(emac_dev, "DaVinci EMAC: emac_init_rxch() failed");
2412 return rc;
2413 }
2414
2415 /* Request IRQ */
2416
2417 while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
2418 for (i = res->start; i <= res->end; i++) {
2419 if (request_irq(i, emac_irq, IRQF_DISABLED,
2420 ndev->name, ndev))
2421 goto rollback;
2422 }
2423 k++;
2424 }
2425
2426 /* Start/Enable EMAC hardware */
2427 emac_hw_enable(priv);
2428
2429 /* find the first phy */
2430 priv->phydev = NULL;
2431 if (priv->phy_mask) {
2432 emac_mii_reset(priv->mii_bus);
2433 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
2434 if (priv->mii_bus->phy_map[phy_addr]) {
2435 priv->phydev = priv->mii_bus->phy_map[phy_addr];
2436 break;
2437 }
2438 }
2439
2440 if (!priv->phydev) {
2441 printk(KERN_ERR "%s: no PHY found\n", ndev->name);
2442 return -1;
2443 }
2444
2445 priv->phydev = phy_connect(ndev, dev_name(&priv->phydev->dev),
2446 &emac_adjust_link, 0, PHY_INTERFACE_MODE_MII);
2447
2448 if (IS_ERR(priv->phydev)) {
2449 printk(KERN_ERR "%s: Could not attach to PHY\n",
2450 ndev->name);
2451 return PTR_ERR(priv->phydev);
2452 }
2453
2454 priv->link = 0;
2455 priv->speed = 0;
2456 priv->duplex = ~0;
2457
2458 printk(KERN_INFO "%s: attached PHY driver [%s] "
2459 "(mii_bus:phy_addr=%s, id=%x)\n", ndev->name,
2460 priv->phydev->drv->name, dev_name(&priv->phydev->dev),
2461 priv->phydev->phy_id);
2462 } else{
2463 /* No PHY , fix the link, speed and duplex settings */
2464 priv->link = 1;
2465 priv->speed = SPEED_100;
2466 priv->duplex = DUPLEX_FULL;
2467 emac_update_phystatus(priv);
2468 }
2469
2470 if (!netif_running(ndev)) /* debug only - to avoid compiler warning */
2471 emac_dump_regs(priv);
2472
2473 if (netif_msg_drv(priv))
2474 dev_notice(emac_dev, "DaVinci EMAC: Opened %s\n", ndev->name);
2475
2476 if (priv->phy_mask)
2477 phy_start(priv->phydev);
2478
2479 return 0;
2480
2481rollback:
2482
2483 dev_err(emac_dev, "DaVinci EMAC: request_irq() failed");
2484
2485 for (q = k; k >= 0; k--) {
2486 for (m = i; m >= res->start; m--)
2487 free_irq(m, ndev);
2488 res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k-1);
2489 m = res->end;
2490 }
2491 return -EBUSY;
2492}
2493
2494/**
2495 * emac_dev_stop: EMAC device stop
2496 * @ndev: The DaVinci EMAC network adapter
2497 *
2498 * Called when system wants to stop or down the interface. We stop the network
2499 * queue, disable interrupts and cleanup TX/RX channels.
2500 *
2501 * We return the statistics in net_device_stats structure pulled from emac
2502 */
2503static int emac_dev_stop(struct net_device *ndev)
2504{
2505 struct resource *res;
2506 int i = 0;
2507 int irq_num;
2508 struct emac_priv *priv = netdev_priv(ndev);
2509 struct device *emac_dev = &ndev->dev;
2510
2511 /* inform the upper layers. */
2512 netif_stop_queue(ndev);
2513 napi_disable(&priv->napi);
2514
2515 netif_carrier_off(ndev);
2516 emac_int_disable(priv);
2517 emac_stop_txch(priv, EMAC_DEF_TX_CH);
2518 emac_stop_rxch(priv, EMAC_DEF_RX_CH);
2519 emac_cleanup_txch(priv, EMAC_DEF_TX_CH);
2520 emac_cleanup_rxch(priv, EMAC_DEF_RX_CH);
2521 emac_write(EMAC_SOFTRESET, 1);
2522
2523 if (priv->phydev)
2524 phy_disconnect(priv->phydev);
2525
2526 /* Free IRQ */
2527 while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, i))) {
2528 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2529 free_irq(irq_num, priv->ndev);
2530 i++;
2531 }
2532
2533 if (netif_msg_drv(priv))
2534 dev_notice(emac_dev, "DaVinci EMAC: %s stopped\n", ndev->name);
2535
2536 return 0;
2537}
2538
2539/**
2540 * emac_dev_getnetstats: EMAC get statistics function
2541 * @ndev: The DaVinci EMAC network adapter
2542 *
2543 * Called when system wants to get statistics from the device.
2544 *
2545 * We return the statistics in net_device_stats structure pulled from emac
2546 */
2547static struct net_device_stats *emac_dev_getnetstats(struct net_device *ndev)
2548{
2549 struct emac_priv *priv = netdev_priv(ndev);
Sriram0fe74632009-10-07 02:44:30 +00002550 u32 mac_control;
2551 u32 stats_clear_mask;
Anant Golea6286ee2009-05-18 15:19:01 -07002552
2553 /* update emac hardware stats and reset the registers*/
2554
Sriram0fe74632009-10-07 02:44:30 +00002555 mac_control = emac_read(EMAC_MACCONTROL);
2556
2557 if (mac_control & EMAC_MACCONTROL_GMIIEN)
2558 stats_clear_mask = EMAC_STATS_CLR_MASK;
2559 else
2560 stats_clear_mask = 0;
2561
Anant Golea6286ee2009-05-18 15:19:01 -07002562 priv->net_dev_stats.multicast += emac_read(EMAC_RXMCASTFRAMES);
Sriram0fe74632009-10-07 02:44:30 +00002563 emac_write(EMAC_RXMCASTFRAMES, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002564
2565 priv->net_dev_stats.collisions += (emac_read(EMAC_TXCOLLISION) +
2566 emac_read(EMAC_TXSINGLECOLL) +
2567 emac_read(EMAC_TXMULTICOLL));
Sriram0fe74632009-10-07 02:44:30 +00002568 emac_write(EMAC_TXCOLLISION, stats_clear_mask);
2569 emac_write(EMAC_TXSINGLECOLL, stats_clear_mask);
2570 emac_write(EMAC_TXMULTICOLL, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002571
2572 priv->net_dev_stats.rx_length_errors += (emac_read(EMAC_RXOVERSIZED) +
2573 emac_read(EMAC_RXJABBER) +
2574 emac_read(EMAC_RXUNDERSIZED));
Sriram0fe74632009-10-07 02:44:30 +00002575 emac_write(EMAC_RXOVERSIZED, stats_clear_mask);
2576 emac_write(EMAC_RXJABBER, stats_clear_mask);
2577 emac_write(EMAC_RXUNDERSIZED, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002578
2579 priv->net_dev_stats.rx_over_errors += (emac_read(EMAC_RXSOFOVERRUNS) +
2580 emac_read(EMAC_RXMOFOVERRUNS));
Sriram0fe74632009-10-07 02:44:30 +00002581 emac_write(EMAC_RXSOFOVERRUNS, stats_clear_mask);
2582 emac_write(EMAC_RXMOFOVERRUNS, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002583
2584 priv->net_dev_stats.rx_fifo_errors += emac_read(EMAC_RXDMAOVERRUNS);
Sriram0fe74632009-10-07 02:44:30 +00002585 emac_write(EMAC_RXDMAOVERRUNS, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002586
2587 priv->net_dev_stats.tx_carrier_errors +=
2588 emac_read(EMAC_TXCARRIERSENSE);
Sriram0fe74632009-10-07 02:44:30 +00002589 emac_write(EMAC_TXCARRIERSENSE, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002590
2591 priv->net_dev_stats.tx_fifo_errors = emac_read(EMAC_TXUNDERRUN);
Sriram0fe74632009-10-07 02:44:30 +00002592 emac_write(EMAC_TXUNDERRUN, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002593
2594 return &priv->net_dev_stats;
2595}
2596
2597static const struct net_device_ops emac_netdev_ops = {
2598 .ndo_open = emac_dev_open,
2599 .ndo_stop = emac_dev_stop,
2600 .ndo_start_xmit = emac_dev_xmit,
2601 .ndo_set_multicast_list = emac_dev_mcast_set,
2602 .ndo_set_mac_address = emac_dev_setmac_addr,
2603 .ndo_do_ioctl = emac_devioctl,
2604 .ndo_tx_timeout = emac_dev_tx_timeout,
2605 .ndo_get_stats = emac_dev_getnetstats,
2606#ifdef CONFIG_NET_POLL_CONTROLLER
2607 .ndo_poll_controller = emac_poll_controller,
2608#endif
2609};
2610
2611/**
2612 * davinci_emac_probe: EMAC device probe
2613 * @pdev: The DaVinci EMAC device that we are removing
2614 *
2615 * Called when probing for emac devicesr. We get details of instances and
2616 * resource information from platform init and register a network device
2617 * and allocate resources necessary for driver to perform
2618 */
2619static int __devinit davinci_emac_probe(struct platform_device *pdev)
2620{
2621 int rc = 0;
2622 struct resource *res;
2623 struct net_device *ndev;
2624 struct emac_priv *priv;
2625 unsigned long size;
2626 struct emac_platform_data *pdata;
2627 struct device *emac_dev;
2628
2629 /* obtain emac clock from kernel */
2630 emac_clk = clk_get(&pdev->dev, NULL);
2631 if (IS_ERR(emac_clk)) {
2632 printk(KERN_ERR "DaVinci EMAC: Failed to get EMAC clock\n");
2633 return -EBUSY;
2634 }
2635 emac_bus_frequency = clk_get_rate(emac_clk);
2636 /* TODO: Probe PHY here if possible */
2637
2638 ndev = alloc_etherdev(sizeof(struct emac_priv));
2639 if (!ndev) {
2640 printk(KERN_ERR "DaVinci EMAC: Error allocating net_device\n");
2641 clk_put(emac_clk);
2642 return -ENOMEM;
2643 }
2644
2645 platform_set_drvdata(pdev, ndev);
2646 priv = netdev_priv(ndev);
2647 priv->pdev = pdev;
2648 priv->ndev = ndev;
2649 priv->msg_enable = netif_msg_init(debug_level, DAVINCI_EMAC_DEBUG);
2650
2651 spin_lock_init(&priv->tx_lock);
2652 spin_lock_init(&priv->rx_lock);
2653 spin_lock_init(&priv->lock);
2654
2655 pdata = pdev->dev.platform_data;
2656 if (!pdata) {
2657 printk(KERN_ERR "DaVinci EMAC: No platfrom data\n");
2658 return -ENODEV;
2659 }
2660
2661 /* MAC addr and PHY mask , RMII enable info from platform_data */
2662 memcpy(priv->mac_addr, pdata->mac_addr, 6);
2663 priv->phy_mask = pdata->phy_mask;
2664 priv->rmii_en = pdata->rmii_en;
2665 priv->version = pdata->version;
2666 emac_dev = &ndev->dev;
2667 /* Get EMAC platform data */
2668 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2669 if (!res) {
2670 dev_err(emac_dev, "DaVinci EMAC: Error getting res\n");
2671 rc = -ENOENT;
2672 goto probe_quit;
2673 }
2674
2675 priv->emac_base_phys = res->start + pdata->ctrl_reg_offset;
2676 size = res->end - res->start + 1;
2677 if (!request_mem_region(res->start, size, ndev->name)) {
2678 dev_err(emac_dev, "DaVinci EMAC: failed request_mem_region() \
2679 for regs\n");
2680 rc = -ENXIO;
2681 goto probe_quit;
2682 }
2683
2684 priv->remap_addr = ioremap(res->start, size);
2685 if (!priv->remap_addr) {
2686 dev_err(emac_dev, "Unable to map IO\n");
2687 rc = -ENOMEM;
2688 release_mem_region(res->start, size);
2689 goto probe_quit;
2690 }
2691 priv->emac_base = priv->remap_addr + pdata->ctrl_reg_offset;
2692 ndev->base_addr = (unsigned long)priv->remap_addr;
2693
2694 priv->ctrl_base = priv->remap_addr + pdata->ctrl_mod_reg_offset;
2695 priv->ctrl_ram_size = pdata->ctrl_ram_size;
2696 priv->emac_ctrl_ram = priv->remap_addr + pdata->ctrl_ram_offset;
2697
2698 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2699 if (!res) {
2700 dev_err(emac_dev, "DaVinci EMAC: Error getting irq res\n");
2701 rc = -ENOENT;
2702 goto no_irq_res;
2703 }
2704 ndev->irq = res->start;
2705
2706 if (!is_valid_ether_addr(priv->mac_addr)) {
Anant Golea6286ee2009-05-18 15:19:01 -07002707 /* Use random MAC if none passed */
2708 random_ether_addr(priv->mac_addr);
Chaithrika U S5c726162009-06-03 21:54:29 -07002709 printk(KERN_WARNING "%s: using random MAC addr: %pM\n",
2710 __func__, priv->mac_addr);
Anant Golea6286ee2009-05-18 15:19:01 -07002711 }
2712
2713 ndev->netdev_ops = &emac_netdev_ops;
2714 SET_ETHTOOL_OPS(ndev, &ethtool_ops);
2715 netif_napi_add(ndev, &priv->napi, emac_poll, EMAC_POLL_WEIGHT);
2716
2717 /* register the network device */
2718 SET_NETDEV_DEV(ndev, &pdev->dev);
2719 rc = register_netdev(ndev);
2720 if (rc) {
2721 dev_err(emac_dev, "DaVinci EMAC: Error in register_netdev\n");
2722 rc = -ENODEV;
2723 goto netdev_reg_err;
2724 }
2725
2726 clk_enable(emac_clk);
2727
2728 /* MII/Phy intialisation, mdio bus registration */
2729 emac_mii = mdiobus_alloc();
2730 if (emac_mii == NULL) {
2731 dev_err(emac_dev, "DaVinci EMAC: Error allocating mii_bus\n");
2732 rc = -ENOMEM;
2733 goto mdio_alloc_err;
2734 }
2735
2736 priv->mii_bus = emac_mii;
2737 emac_mii->name = "emac-mii",
2738 emac_mii->read = emac_mii_read,
2739 emac_mii->write = emac_mii_write,
2740 emac_mii->reset = emac_mii_reset,
2741 emac_mii->irq = mii_irqs,
2742 emac_mii->phy_mask = ~(priv->phy_mask);
2743 emac_mii->parent = &pdev->dev;
2744 emac_mii->priv = priv->remap_addr + pdata->mdio_reg_offset;
2745 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%x", priv->pdev->id);
2746 mdio_max_freq = pdata->mdio_max_freq;
2747 emac_mii->reset(emac_mii);
2748
2749 /* Register the MII bus */
2750 rc = mdiobus_register(emac_mii);
2751 if (rc)
2752 goto mdiobus_quit;
2753
2754 if (netif_msg_probe(priv)) {
2755 dev_notice(emac_dev, "DaVinci EMAC Probe found device "\
2756 "(regs: %p, irq: %d)\n",
2757 (void *)priv->emac_base_phys, ndev->irq);
2758 }
2759 return 0;
2760
2761mdiobus_quit:
2762 mdiobus_free(emac_mii);
2763
2764netdev_reg_err:
2765mdio_alloc_err:
2766no_irq_res:
2767 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2768 release_mem_region(res->start, res->end - res->start + 1);
2769 iounmap(priv->remap_addr);
2770
2771probe_quit:
2772 clk_put(emac_clk);
2773 free_netdev(ndev);
2774 return rc;
2775}
2776
2777/**
2778 * davinci_emac_remove: EMAC device remove
2779 * @pdev: The DaVinci EMAC device that we are removing
2780 *
2781 * Called when removing the device driver. We disable clock usage and release
2782 * the resources taken up by the driver and unregister network device
2783 */
2784static int __devexit davinci_emac_remove(struct platform_device *pdev)
2785{
2786 struct resource *res;
2787 struct net_device *ndev = platform_get_drvdata(pdev);
2788 struct emac_priv *priv = netdev_priv(ndev);
2789
2790 dev_notice(&ndev->dev, "DaVinci EMAC: davinci_emac_remove()\n");
2791
Anant Golea6286ee2009-05-18 15:19:01 -07002792 platform_set_drvdata(pdev, NULL);
2793 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2794 mdiobus_unregister(priv->mii_bus);
2795 mdiobus_free(priv->mii_bus);
2796
2797 release_mem_region(res->start, res->end - res->start + 1);
2798
2799 unregister_netdev(ndev);
2800 free_netdev(ndev);
2801 iounmap(priv->remap_addr);
2802
2803 clk_disable(emac_clk);
2804 clk_put(emac_clk);
2805
2806 return 0;
2807}
2808
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002809static
2810int davinci_emac_suspend(struct platform_device *pdev, pm_message_t state)
2811{
2812 struct net_device *dev = platform_get_drvdata(pdev);
2813
2814 if (netif_running(dev))
2815 emac_dev_stop(dev);
2816
2817 clk_disable(emac_clk);
2818
2819 return 0;
2820}
2821
2822static int davinci_emac_resume(struct platform_device *pdev)
2823{
2824 struct net_device *dev = platform_get_drvdata(pdev);
2825
2826 clk_enable(emac_clk);
2827
2828 if (netif_running(dev))
2829 emac_dev_open(dev);
2830
2831 return 0;
2832}
2833
Anant Golea6286ee2009-05-18 15:19:01 -07002834/**
2835 * davinci_emac_driver: EMAC platform driver structure
Anant Golea6286ee2009-05-18 15:19:01 -07002836 */
2837static struct platform_driver davinci_emac_driver = {
2838 .driver = {
2839 .name = "davinci_emac",
2840 .owner = THIS_MODULE,
2841 },
2842 .probe = davinci_emac_probe,
2843 .remove = __devexit_p(davinci_emac_remove),
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002844 .suspend = davinci_emac_suspend,
2845 .resume = davinci_emac_resume,
Anant Golea6286ee2009-05-18 15:19:01 -07002846};
2847
2848/**
2849 * davinci_emac_init: EMAC driver module init
2850 *
2851 * Called when initializing the driver. We register the driver with
2852 * the platform.
2853 */
2854static int __init davinci_emac_init(void)
2855{
2856 return platform_driver_register(&davinci_emac_driver);
2857}
Rajashekhara, Sudhakar2db95172009-08-19 10:39:55 +00002858late_initcall(davinci_emac_init);
Anant Golea6286ee2009-05-18 15:19:01 -07002859
2860/**
2861 * davinci_emac_exit: EMAC driver module exit
2862 *
2863 * Called when exiting the driver completely. We unregister the driver with
2864 * the platform and exit
2865 */
2866static void __exit davinci_emac_exit(void)
2867{
2868 platform_driver_unregister(&davinci_emac_driver);
2869}
2870module_exit(davinci_emac_exit);
2871
2872MODULE_LICENSE("GPL");
2873MODULE_AUTHOR("DaVinci EMAC Maintainer: Anant Gole <anantgole@ti.com>");
2874MODULE_AUTHOR("DaVinci EMAC Maintainer: Chaithrika U S <chaithrika@ti.com>");
2875MODULE_DESCRIPTION("DaVinci EMAC Ethernet driver");