blob: 67dbcfb5e894dc47ff049705640f1b10040df009 [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
Anant Golea6286ee2009-05-18 15:19:01 -070032#include <linux/module.h>
33#include <linux/kernel.h>
34#include <linux/sched.h>
35#include <linux/string.h>
36#include <linux/timer.h>
37#include <linux/errno.h>
38#include <linux/in.h>
39#include <linux/ioport.h>
40#include <linux/slab.h>
41#include <linux/mm.h>
42#include <linux/interrupt.h>
43#include <linux/init.h>
44#include <linux/netdevice.h>
45#include <linux/etherdevice.h>
46#include <linux/skbuff.h>
47#include <linux/ethtool.h>
48#include <linux/highmem.h>
49#include <linux/proc_fs.h>
50#include <linux/ctype.h>
51#include <linux/version.h>
52#include <linux/spinlock.h>
53#include <linux/dma-mapping.h>
54#include <linux/clk.h>
55#include <linux/platform_device.h>
56#include <linux/semaphore.h>
57#include <linux/phy.h>
58#include <linux/bitops.h>
59#include <linux/io.h>
60#include <linux/uaccess.h>
Sriramakrishnan8ee2bf92009-11-19 15:58:25 +053061#include <linux/davinci_emac.h>
Anant Golea6286ee2009-05-18 15:19:01 -070062
63#include <asm/irq.h>
64#include <asm/page.h>
65
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -040066#include "davinci_cpdma.h"
67
Anant Golea6286ee2009-05-18 15:19:01 -070068static int debug_level;
69module_param(debug_level, int, 0);
70MODULE_PARM_DESC(debug_level, "DaVinci EMAC debug level (NETIF_MSG bits)");
71
72/* Netif debug messages possible */
73#define DAVINCI_EMAC_DEBUG (NETIF_MSG_DRV | \
74 NETIF_MSG_PROBE | \
75 NETIF_MSG_LINK | \
76 NETIF_MSG_TIMER | \
77 NETIF_MSG_IFDOWN | \
78 NETIF_MSG_IFUP | \
79 NETIF_MSG_RX_ERR | \
80 NETIF_MSG_TX_ERR | \
81 NETIF_MSG_TX_QUEUED | \
82 NETIF_MSG_INTR | \
83 NETIF_MSG_TX_DONE | \
84 NETIF_MSG_RX_STATUS | \
85 NETIF_MSG_PKTDATA | \
86 NETIF_MSG_HW | \
87 NETIF_MSG_WOL)
88
89/* version info */
90#define EMAC_MAJOR_VERSION 6
91#define EMAC_MINOR_VERSION 1
92#define EMAC_MODULE_VERSION "6.1"
93MODULE_VERSION(EMAC_MODULE_VERSION);
94static const char emac_version_string[] = "TI DaVinci EMAC Linux v6.1";
95
96/* Configuration items */
97#define EMAC_DEF_PASS_CRC (0) /* Do not pass CRC upto frames */
98#define EMAC_DEF_QOS_EN (0) /* EMAC proprietary QoS disabled */
99#define EMAC_DEF_NO_BUFF_CHAIN (0) /* No buffer chain */
100#define EMAC_DEF_MACCTRL_FRAME_EN (0) /* Discard Maccontrol frames */
101#define EMAC_DEF_SHORT_FRAME_EN (0) /* Discard short frames */
102#define EMAC_DEF_ERROR_FRAME_EN (0) /* Discard error frames */
103#define EMAC_DEF_PROM_EN (0) /* Promiscous disabled */
104#define EMAC_DEF_PROM_CH (0) /* Promiscous channel is 0 */
105#define EMAC_DEF_BCAST_EN (1) /* Broadcast enabled */
106#define EMAC_DEF_BCAST_CH (0) /* Broadcast channel is 0 */
107#define EMAC_DEF_MCAST_EN (1) /* Multicast enabled */
108#define EMAC_DEF_MCAST_CH (0) /* Multicast channel is 0 */
109
110#define EMAC_DEF_TXPRIO_FIXED (1) /* TX Priority is fixed */
111#define EMAC_DEF_TXPACING_EN (0) /* TX pacing NOT supported*/
112
113#define EMAC_DEF_BUFFER_OFFSET (0) /* Buffer offset to DMA (future) */
114#define EMAC_DEF_MIN_ETHPKTSIZE (60) /* Minimum ethernet pkt size */
115#define EMAC_DEF_MAX_FRAME_SIZE (1500 + 14 + 4 + 4)
116#define EMAC_DEF_TX_CH (0) /* Default 0th channel */
117#define EMAC_DEF_RX_CH (0) /* Default 0th channel */
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -0400118#define EMAC_DEF_RX_NUM_DESC (128)
Anant Golea6286ee2009-05-18 15:19:01 -0700119#define EMAC_DEF_MAX_TX_CH (1) /* Max TX channels configured */
120#define EMAC_DEF_MAX_RX_CH (1) /* Max RX channels configured */
121#define EMAC_POLL_WEIGHT (64) /* Default NAPI poll weight */
122
123/* Buffer descriptor parameters */
124#define EMAC_DEF_TX_MAX_SERVICE (32) /* TX max service BD's */
125#define EMAC_DEF_RX_MAX_SERVICE (64) /* should = netdev->weight */
126
127/* EMAC register related defines */
128#define EMAC_ALL_MULTI_REG_VALUE (0xFFFFFFFF)
129#define EMAC_NUM_MULTICAST_BITS (64)
130#define EMAC_TEARDOWN_VALUE (0xFFFFFFFC)
131#define EMAC_TX_CONTROL_TX_ENABLE_VAL (0x1)
132#define EMAC_RX_CONTROL_RX_ENABLE_VAL (0x1)
133#define EMAC_MAC_HOST_ERR_INTMASK_VAL (0x2)
134#define EMAC_RX_UNICAST_CLEAR_ALL (0xFF)
135#define EMAC_INT_MASK_CLEAR (0xFF)
136
137/* RX MBP register bit positions */
138#define EMAC_RXMBP_PASSCRC_MASK BIT(30)
139#define EMAC_RXMBP_QOSEN_MASK BIT(29)
140#define EMAC_RXMBP_NOCHAIN_MASK BIT(28)
141#define EMAC_RXMBP_CMFEN_MASK BIT(24)
142#define EMAC_RXMBP_CSFEN_MASK BIT(23)
143#define EMAC_RXMBP_CEFEN_MASK BIT(22)
144#define EMAC_RXMBP_CAFEN_MASK BIT(21)
145#define EMAC_RXMBP_PROMCH_SHIFT (16)
146#define EMAC_RXMBP_PROMCH_MASK (0x7 << 16)
147#define EMAC_RXMBP_BROADEN_MASK BIT(13)
148#define EMAC_RXMBP_BROADCH_SHIFT (8)
149#define EMAC_RXMBP_BROADCH_MASK (0x7 << 8)
150#define EMAC_RXMBP_MULTIEN_MASK BIT(5)
151#define EMAC_RXMBP_MULTICH_SHIFT (0)
152#define EMAC_RXMBP_MULTICH_MASK (0x7)
153#define EMAC_RXMBP_CHMASK (0x7)
154
155/* EMAC register definitions/bit maps used */
156# define EMAC_MBP_RXPROMISC (0x00200000)
157# define EMAC_MBP_PROMISCCH(ch) (((ch) & 0x7) << 16)
158# define EMAC_MBP_RXBCAST (0x00002000)
159# define EMAC_MBP_BCASTCHAN(ch) (((ch) & 0x7) << 8)
160# define EMAC_MBP_RXMCAST (0x00000020)
161# define EMAC_MBP_MCASTCHAN(ch) ((ch) & 0x7)
162
163/* EMAC mac_control register */
chaithrika@ti.com69ef9692009-10-01 10:25:19 +0000164#define EMAC_MACCONTROL_TXPTYPE BIT(9)
165#define EMAC_MACCONTROL_TXPACEEN BIT(6)
166#define EMAC_MACCONTROL_GMIIEN BIT(5)
167#define EMAC_MACCONTROL_GIGABITEN BIT(7)
168#define EMAC_MACCONTROL_FULLDUPLEXEN BIT(0)
Anant Golea6286ee2009-05-18 15:19:01 -0700169#define EMAC_MACCONTROL_RMIISPEED_MASK BIT(15)
170
171/* GIGABIT MODE related bits */
Anant Golea6286ee2009-05-18 15:19:01 -0700172#define EMAC_DM646X_MACCONTORL_GIG BIT(7)
173#define EMAC_DM646X_MACCONTORL_GIGFORCE BIT(17)
174
175/* EMAC mac_status register */
176#define EMAC_MACSTATUS_TXERRCODE_MASK (0xF00000)
177#define EMAC_MACSTATUS_TXERRCODE_SHIFT (20)
178#define EMAC_MACSTATUS_TXERRCH_MASK (0x7)
179#define EMAC_MACSTATUS_TXERRCH_SHIFT (16)
180#define EMAC_MACSTATUS_RXERRCODE_MASK (0xF000)
181#define EMAC_MACSTATUS_RXERRCODE_SHIFT (12)
182#define EMAC_MACSTATUS_RXERRCH_MASK (0x7)
183#define EMAC_MACSTATUS_RXERRCH_SHIFT (8)
184
185/* EMAC RX register masks */
186#define EMAC_RX_MAX_LEN_MASK (0xFFFF)
187#define EMAC_RX_BUFFER_OFFSET_MASK (0xFFFF)
188
189/* MAC_IN_VECTOR (0x180) register bit fields */
chaithrika@ti.com69ef9692009-10-01 10:25:19 +0000190#define EMAC_DM644X_MAC_IN_VECTOR_HOST_INT BIT(17)
191#define EMAC_DM644X_MAC_IN_VECTOR_STATPEND_INT BIT(16)
192#define EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC BIT(8)
193#define EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC BIT(0)
Anant Golea6286ee2009-05-18 15:19:01 -0700194
195/** NOTE:: For DM646x the IN_VECTOR has changed */
196#define EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC BIT(EMAC_DEF_RX_CH)
197#define EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC BIT(16 + EMAC_DEF_TX_CH)
Sriram43c2ed82009-09-24 19:15:18 +0000198#define EMAC_DM646X_MAC_IN_VECTOR_HOST_INT BIT(26)
199#define EMAC_DM646X_MAC_IN_VECTOR_STATPEND_INT BIT(27)
200
Anant Golea6286ee2009-05-18 15:19:01 -0700201/* CPPI bit positions */
202#define EMAC_CPPI_SOP_BIT BIT(31)
203#define EMAC_CPPI_EOP_BIT BIT(30)
204#define EMAC_CPPI_OWNERSHIP_BIT BIT(29)
205#define EMAC_CPPI_EOQ_BIT BIT(28)
206#define EMAC_CPPI_TEARDOWN_COMPLETE_BIT BIT(27)
207#define EMAC_CPPI_PASS_CRC_BIT BIT(26)
208#define EMAC_RX_BD_BUF_SIZE (0xFFFF)
209#define EMAC_BD_LENGTH_FOR_CACHE (16) /* only CPPI bytes */
210#define EMAC_RX_BD_PKT_LENGTH_MASK (0xFFFF)
211
212/* Max hardware defines */
213#define EMAC_MAX_TXRX_CHANNELS (8) /* Max hardware channels */
214#define EMAC_DEF_MAX_MULTICAST_ADDRESSES (64) /* Max mcast addr's */
215
216/* EMAC Peripheral Device Register Memory Layout structure */
217#define EMAC_TXIDVER 0x0
218#define EMAC_TXCONTROL 0x4
219#define EMAC_TXTEARDOWN 0x8
220#define EMAC_RXIDVER 0x10
221#define EMAC_RXCONTROL 0x14
222#define EMAC_RXTEARDOWN 0x18
223#define EMAC_TXINTSTATRAW 0x80
224#define EMAC_TXINTSTATMASKED 0x84
225#define EMAC_TXINTMASKSET 0x88
226#define EMAC_TXINTMASKCLEAR 0x8C
227#define EMAC_MACINVECTOR 0x90
228
229#define EMAC_DM646X_MACEOIVECTOR 0x94
230
231#define EMAC_RXINTSTATRAW 0xA0
232#define EMAC_RXINTSTATMASKED 0xA4
233#define EMAC_RXINTMASKSET 0xA8
234#define EMAC_RXINTMASKCLEAR 0xAC
235#define EMAC_MACINTSTATRAW 0xB0
236#define EMAC_MACINTSTATMASKED 0xB4
237#define EMAC_MACINTMASKSET 0xB8
238#define EMAC_MACINTMASKCLEAR 0xBC
239
240#define EMAC_RXMBPENABLE 0x100
241#define EMAC_RXUNICASTSET 0x104
242#define EMAC_RXUNICASTCLEAR 0x108
243#define EMAC_RXMAXLEN 0x10C
244#define EMAC_RXBUFFEROFFSET 0x110
245#define EMAC_RXFILTERLOWTHRESH 0x114
246
247#define EMAC_MACCONTROL 0x160
248#define EMAC_MACSTATUS 0x164
249#define EMAC_EMCONTROL 0x168
250#define EMAC_FIFOCONTROL 0x16C
251#define EMAC_MACCONFIG 0x170
252#define EMAC_SOFTRESET 0x174
253#define EMAC_MACSRCADDRLO 0x1D0
254#define EMAC_MACSRCADDRHI 0x1D4
255#define EMAC_MACHASH1 0x1D8
256#define EMAC_MACHASH2 0x1DC
257#define EMAC_MACADDRLO 0x500
258#define EMAC_MACADDRHI 0x504
259#define EMAC_MACINDEX 0x508
260
261/* EMAC HDP and Completion registors */
262#define EMAC_TXHDP(ch) (0x600 + (ch * 4))
263#define EMAC_RXHDP(ch) (0x620 + (ch * 4))
264#define EMAC_TXCP(ch) (0x640 + (ch * 4))
265#define EMAC_RXCP(ch) (0x660 + (ch * 4))
266
267/* EMAC statistics registers */
268#define EMAC_RXGOODFRAMES 0x200
269#define EMAC_RXBCASTFRAMES 0x204
270#define EMAC_RXMCASTFRAMES 0x208
271#define EMAC_RXPAUSEFRAMES 0x20C
272#define EMAC_RXCRCERRORS 0x210
273#define EMAC_RXALIGNCODEERRORS 0x214
274#define EMAC_RXOVERSIZED 0x218
275#define EMAC_RXJABBER 0x21C
276#define EMAC_RXUNDERSIZED 0x220
277#define EMAC_RXFRAGMENTS 0x224
278#define EMAC_RXFILTERED 0x228
279#define EMAC_RXQOSFILTERED 0x22C
280#define EMAC_RXOCTETS 0x230
281#define EMAC_TXGOODFRAMES 0x234
282#define EMAC_TXBCASTFRAMES 0x238
283#define EMAC_TXMCASTFRAMES 0x23C
284#define EMAC_TXPAUSEFRAMES 0x240
285#define EMAC_TXDEFERRED 0x244
286#define EMAC_TXCOLLISION 0x248
287#define EMAC_TXSINGLECOLL 0x24C
288#define EMAC_TXMULTICOLL 0x250
289#define EMAC_TXEXCESSIVECOLL 0x254
290#define EMAC_TXLATECOLL 0x258
291#define EMAC_TXUNDERRUN 0x25C
292#define EMAC_TXCARRIERSENSE 0x260
293#define EMAC_TXOCTETS 0x264
294#define EMAC_NETOCTETS 0x280
295#define EMAC_RXSOFOVERRUNS 0x284
296#define EMAC_RXMOFOVERRUNS 0x288
297#define EMAC_RXDMAOVERRUNS 0x28C
298
299/* EMAC DM644x control registers */
300#define EMAC_CTRL_EWCTL (0x4)
301#define EMAC_CTRL_EWINTTCNT (0x8)
302
Sriram84da2652010-07-29 02:33:58 +0000303/* EMAC DM644x control module masks */
304#define EMAC_DM644X_EWINTCNT_MASK 0x1FFFF
305#define EMAC_DM644X_INTMIN_INTVL 0x1
306#define EMAC_DM644X_INTMAX_INTVL (EMAC_DM644X_EWINTCNT_MASK)
307
Anant Golea6286ee2009-05-18 15:19:01 -0700308/* EMAC DM646X control module registers */
Sriram84da2652010-07-29 02:33:58 +0000309#define EMAC_DM646X_CMINTCTRL 0x0C
310#define EMAC_DM646X_CMRXINTEN 0x14
311#define EMAC_DM646X_CMTXINTEN 0x18
312#define EMAC_DM646X_CMRXINTMAX 0x70
313#define EMAC_DM646X_CMTXINTMAX 0x74
314
315/* EMAC DM646X control module masks */
316#define EMAC_DM646X_INTPACEEN (0x3 << 16)
317#define EMAC_DM646X_INTPRESCALE_MASK (0x7FF << 0)
318#define EMAC_DM646X_CMINTMAX_CNT 63
319#define EMAC_DM646X_CMINTMIN_CNT 2
320#define EMAC_DM646X_CMINTMAX_INTVL (1000 / EMAC_DM646X_CMINTMIN_CNT)
321#define EMAC_DM646X_CMINTMIN_INTVL ((1000 / EMAC_DM646X_CMINTMAX_CNT) + 1)
322
Anant Golea6286ee2009-05-18 15:19:01 -0700323
324/* EMAC EOI codes for C0 */
325#define EMAC_DM646X_MAC_EOI_C0_RXEN (0x01)
326#define EMAC_DM646X_MAC_EOI_C0_TXEN (0x02)
327
Sriram0fe74632009-10-07 02:44:30 +0000328/* EMAC Stats Clear Mask */
329#define EMAC_STATS_CLR_MASK (0xFFFFFFFF)
330
Anant Golea6286ee2009-05-18 15:19:01 -0700331/** net_buf_obj: EMAC network bufferdata structure
332 *
333 * EMAC network buffer data structure
334 */
335struct emac_netbufobj {
336 void *buf_token;
337 char *data_ptr;
338 int length;
339};
340
341/** net_pkt_obj: EMAC network packet data structure
342 *
343 * EMAC network packet data structure - supports buffer list (for future)
344 */
345struct emac_netpktobj {
346 void *pkt_token; /* data token may hold tx/rx chan id */
347 struct emac_netbufobj *buf_list; /* array of network buffer objects */
348 int num_bufs;
349 int pkt_length;
350};
351
352/** emac_tx_bd: EMAC TX Buffer descriptor data structure
353 *
354 * EMAC TX Buffer descriptor data structure
355 */
356struct emac_tx_bd {
357 int h_next;
358 int buff_ptr;
359 int off_b_len;
360 int mode; /* SOP, EOP, ownership, EOQ, teardown,Qstarv, length */
361 struct emac_tx_bd __iomem *next;
362 void *buf_token;
363};
364
365/** emac_txch: EMAC TX Channel data structure
366 *
367 * EMAC TX Channel data structure
368 */
369struct emac_txch {
370 /* Config related */
371 u32 num_bd;
372 u32 service_max;
373
374 /* CPPI specific */
375 u32 alloc_size;
376 void __iomem *bd_mem;
377 struct emac_tx_bd __iomem *bd_pool_head;
378 struct emac_tx_bd __iomem *active_queue_head;
379 struct emac_tx_bd __iomem *active_queue_tail;
380 struct emac_tx_bd __iomem *last_hw_bdprocessed;
381 u32 queue_active;
382 u32 teardown_pending;
383 u32 *tx_complete;
384
385 /** statistics */
386 u32 proc_count; /* TX: # of times emac_tx_bdproc is called */
387 u32 mis_queued_packets;
388 u32 queue_reinit;
389 u32 end_of_queue_add;
390 u32 out_of_tx_bd;
391 u32 no_active_pkts; /* IRQ when there were no packets to process */
392 u32 active_queue_count;
393};
394
395/** emac_rx_bd: EMAC RX Buffer descriptor data structure
396 *
397 * EMAC RX Buffer descriptor data structure
398 */
399struct emac_rx_bd {
400 int h_next;
401 int buff_ptr;
402 int off_b_len;
403 int mode;
404 struct emac_rx_bd __iomem *next;
405 void *data_ptr;
406 void *buf_token;
407};
408
409/** emac_rxch: EMAC RX Channel data structure
410 *
411 * EMAC RX Channel data structure
412 */
413struct emac_rxch {
414 /* configuration info */
415 u32 num_bd;
416 u32 service_max;
417 u32 buf_size;
418 char mac_addr[6];
419
420 /** CPPI specific */
421 u32 alloc_size;
422 void __iomem *bd_mem;
423 struct emac_rx_bd __iomem *bd_pool_head;
424 struct emac_rx_bd __iomem *active_queue_head;
425 struct emac_rx_bd __iomem *active_queue_tail;
426 u32 queue_active;
427 u32 teardown_pending;
428
429 /* packet and buffer objects */
430 struct emac_netpktobj pkt_queue;
431 struct emac_netbufobj buf_queue;
432
433 /** statistics */
434 u32 proc_count; /* number of times emac_rx_bdproc is called */
435 u32 processed_bd;
436 u32 recycled_bd;
437 u32 out_of_rx_bd;
438 u32 out_of_rx_buffers;
439 u32 queue_reinit;
440 u32 end_of_queue_add;
441 u32 end_of_queue;
442 u32 mis_queued_packets;
443};
444
445/* emac_priv: EMAC private data structure
446 *
447 * EMAC adapter private data structure
448 */
449struct emac_priv {
450 u32 msg_enable;
451 struct net_device *ndev;
452 struct platform_device *pdev;
453 struct napi_struct napi;
454 char mac_addr[6];
455 spinlock_t tx_lock;
456 spinlock_t rx_lock;
457 void __iomem *remap_addr;
458 u32 emac_base_phys;
459 void __iomem *emac_base;
460 void __iomem *ctrl_base;
461 void __iomem *emac_ctrl_ram;
462 u32 ctrl_ram_size;
Sriramakrishnanad021ae2009-11-19 15:58:27 +0530463 u32 hw_ram_addr;
Anant Golea6286ee2009-05-18 15:19:01 -0700464 struct emac_txch *txch[EMAC_DEF_MAX_TX_CH];
465 struct emac_rxch *rxch[EMAC_DEF_MAX_RX_CH];
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -0400466 struct cpdma_ctlr *dma;
467 struct cpdma_chan *txchan;
468 struct cpdma_chan *rxchan;
Anant Golea6286ee2009-05-18 15:19:01 -0700469 u32 link; /* 1=link on, 0=link off */
470 u32 speed; /* 0=Auto Neg, 1=No PHY, 10,100, 1000 - mbps */
471 u32 duplex; /* Link duplex: 0=Half, 1=Full */
472 u32 rx_buf_size;
473 u32 isr_count;
Sriram84da2652010-07-29 02:33:58 +0000474 u32 coal_intvl;
475 u32 bus_freq_mhz;
Anant Golea6286ee2009-05-18 15:19:01 -0700476 u8 rmii_en;
477 u8 version;
Anant Golea6286ee2009-05-18 15:19:01 -0700478 u32 mac_hash1;
479 u32 mac_hash2;
480 u32 multicast_hash_cnt[EMAC_NUM_MULTICAST_BITS];
481 u32 rx_addr_type;
Cyril Chemparathy5d69e002010-09-15 10:11:24 -0400482 const char *phy_id;
Anant Golea6286ee2009-05-18 15:19:01 -0700483 struct phy_device *phydev;
484 spinlock_t lock;
Sriramakrishnan01a9af32009-11-19 15:58:26 +0530485 /*platform specific members*/
486 void (*int_enable) (void);
487 void (*int_disable) (void);
Anant Golea6286ee2009-05-18 15:19:01 -0700488};
489
490/* clock frequency for EMAC */
491static struct clk *emac_clk;
492static unsigned long emac_bus_frequency;
Anant Golea6286ee2009-05-18 15:19:01 -0700493
Sriramakrishnanad021ae2009-11-19 15:58:27 +0530494#define emac_virt_to_phys(addr, priv) \
495 (((u32 __force)(addr) - (u32 __force)(priv->emac_ctrl_ram)) \
496 + priv->hw_ram_addr)
Anant Golea6286ee2009-05-18 15:19:01 -0700497
498/* Cache macros - Packet buffers would be from skb pool which is cached */
499#define EMAC_VIRT_NOCACHE(addr) (addr)
Anant Golea6286ee2009-05-18 15:19:01 -0700500
501/* DM644x does not have BD's in cached memory - so no cache functions */
502#define BD_CACHE_INVALIDATE(addr, size)
503#define BD_CACHE_WRITEBACK(addr, size)
504#define BD_CACHE_WRITEBACK_INVALIDATE(addr, size)
505
506/* EMAC TX Host Error description strings */
507static char *emac_txhost_errcodes[16] = {
508 "No error", "SOP error", "Ownership bit not set in SOP buffer",
509 "Zero Next Buffer Descriptor Pointer Without EOP",
510 "Zero Buffer Pointer", "Zero Buffer Length", "Packet Length Error",
511 "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
512 "Reserved", "Reserved", "Reserved", "Reserved"
513};
514
515/* EMAC RX Host Error description strings */
516static char *emac_rxhost_errcodes[16] = {
517 "No error", "Reserved", "Ownership bit not set in input buffer",
518 "Reserved", "Zero Buffer Pointer", "Reserved", "Reserved",
519 "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
520 "Reserved", "Reserved", "Reserved", "Reserved"
521};
522
523/* Helper macros */
524#define emac_read(reg) ioread32(priv->emac_base + (reg))
525#define emac_write(reg, val) iowrite32(val, priv->emac_base + (reg))
526
527#define emac_ctrl_read(reg) ioread32((priv->ctrl_base + (reg)))
528#define emac_ctrl_write(reg, val) iowrite32(val, (priv->ctrl_base + (reg)))
529
Anant Golea6286ee2009-05-18 15:19:01 -0700530/**
531 * emac_dump_regs: Dump important EMAC registers to debug terminal
532 * @priv: The DaVinci EMAC private adapter structure
533 *
534 * Executes ethtool set cmd & sets phy mode
535 *
536 */
537static void emac_dump_regs(struct emac_priv *priv)
538{
539 struct device *emac_dev = &priv->ndev->dev;
540
541 /* Print important registers in EMAC */
542 dev_info(emac_dev, "EMAC Basic registers\n");
Srirame9947622010-07-29 02:34:00 +0000543 if (priv->version == EMAC_VERSION_1) {
544 dev_info(emac_dev, "EMAC: EWCTL: %08X, EWINTTCNT: %08X\n",
545 emac_ctrl_read(EMAC_CTRL_EWCTL),
546 emac_ctrl_read(EMAC_CTRL_EWINTTCNT));
547 }
Anant Golea6286ee2009-05-18 15:19:01 -0700548 dev_info(emac_dev, "EMAC: TXID: %08X %s, RXID: %08X %s\n",
549 emac_read(EMAC_TXIDVER),
550 ((emac_read(EMAC_TXCONTROL)) ? "enabled" : "disabled"),
551 emac_read(EMAC_RXIDVER),
552 ((emac_read(EMAC_RXCONTROL)) ? "enabled" : "disabled"));
553 dev_info(emac_dev, "EMAC: TXIntRaw:%08X, TxIntMasked: %08X, "\
554 "TxIntMasSet: %08X\n", emac_read(EMAC_TXINTSTATRAW),
555 emac_read(EMAC_TXINTSTATMASKED), emac_read(EMAC_TXINTMASKSET));
556 dev_info(emac_dev, "EMAC: RXIntRaw:%08X, RxIntMasked: %08X, "\
557 "RxIntMasSet: %08X\n", emac_read(EMAC_RXINTSTATRAW),
558 emac_read(EMAC_RXINTSTATMASKED), emac_read(EMAC_RXINTMASKSET));
559 dev_info(emac_dev, "EMAC: MacIntRaw:%08X, MacIntMasked: %08X, "\
560 "MacInVector=%08X\n", emac_read(EMAC_MACINTSTATRAW),
561 emac_read(EMAC_MACINTSTATMASKED), emac_read(EMAC_MACINVECTOR));
562 dev_info(emac_dev, "EMAC: EmuControl:%08X, FifoControl: %08X\n",
563 emac_read(EMAC_EMCONTROL), emac_read(EMAC_FIFOCONTROL));
564 dev_info(emac_dev, "EMAC: MBPEnable:%08X, RXUnicastSet: %08X, "\
565 "RXMaxLen=%08X\n", emac_read(EMAC_RXMBPENABLE),
566 emac_read(EMAC_RXUNICASTSET), emac_read(EMAC_RXMAXLEN));
567 dev_info(emac_dev, "EMAC: MacControl:%08X, MacStatus: %08X, "\
568 "MacConfig=%08X\n", emac_read(EMAC_MACCONTROL),
569 emac_read(EMAC_MACSTATUS), emac_read(EMAC_MACCONFIG));
570 dev_info(emac_dev, "EMAC: TXHDP[0]:%08X, RXHDP[0]: %08X\n",
571 emac_read(EMAC_TXHDP(0)), emac_read(EMAC_RXHDP(0)));
572 dev_info(emac_dev, "EMAC Statistics\n");
573 dev_info(emac_dev, "EMAC: rx_good_frames:%d\n",
574 emac_read(EMAC_RXGOODFRAMES));
575 dev_info(emac_dev, "EMAC: rx_broadcast_frames:%d\n",
576 emac_read(EMAC_RXBCASTFRAMES));
577 dev_info(emac_dev, "EMAC: rx_multicast_frames:%d\n",
578 emac_read(EMAC_RXMCASTFRAMES));
579 dev_info(emac_dev, "EMAC: rx_pause_frames:%d\n",
580 emac_read(EMAC_RXPAUSEFRAMES));
581 dev_info(emac_dev, "EMAC: rx_crcerrors:%d\n",
582 emac_read(EMAC_RXCRCERRORS));
583 dev_info(emac_dev, "EMAC: rx_align_code_errors:%d\n",
584 emac_read(EMAC_RXALIGNCODEERRORS));
585 dev_info(emac_dev, "EMAC: rx_oversized_frames:%d\n",
586 emac_read(EMAC_RXOVERSIZED));
587 dev_info(emac_dev, "EMAC: rx_jabber_frames:%d\n",
588 emac_read(EMAC_RXJABBER));
589 dev_info(emac_dev, "EMAC: rx_undersized_frames:%d\n",
590 emac_read(EMAC_RXUNDERSIZED));
591 dev_info(emac_dev, "EMAC: rx_fragments:%d\n",
592 emac_read(EMAC_RXFRAGMENTS));
593 dev_info(emac_dev, "EMAC: rx_filtered_frames:%d\n",
594 emac_read(EMAC_RXFILTERED));
595 dev_info(emac_dev, "EMAC: rx_qos_filtered_frames:%d\n",
596 emac_read(EMAC_RXQOSFILTERED));
597 dev_info(emac_dev, "EMAC: rx_octets:%d\n",
598 emac_read(EMAC_RXOCTETS));
599 dev_info(emac_dev, "EMAC: tx_goodframes:%d\n",
600 emac_read(EMAC_TXGOODFRAMES));
601 dev_info(emac_dev, "EMAC: tx_bcastframes:%d\n",
602 emac_read(EMAC_TXBCASTFRAMES));
603 dev_info(emac_dev, "EMAC: tx_mcastframes:%d\n",
604 emac_read(EMAC_TXMCASTFRAMES));
605 dev_info(emac_dev, "EMAC: tx_pause_frames:%d\n",
606 emac_read(EMAC_TXPAUSEFRAMES));
607 dev_info(emac_dev, "EMAC: tx_deferred_frames:%d\n",
608 emac_read(EMAC_TXDEFERRED));
609 dev_info(emac_dev, "EMAC: tx_collision_frames:%d\n",
610 emac_read(EMAC_TXCOLLISION));
611 dev_info(emac_dev, "EMAC: tx_single_coll_frames:%d\n",
612 emac_read(EMAC_TXSINGLECOLL));
613 dev_info(emac_dev, "EMAC: tx_mult_coll_frames:%d\n",
614 emac_read(EMAC_TXMULTICOLL));
615 dev_info(emac_dev, "EMAC: tx_excessive_collisions:%d\n",
616 emac_read(EMAC_TXEXCESSIVECOLL));
617 dev_info(emac_dev, "EMAC: tx_late_collisions:%d\n",
618 emac_read(EMAC_TXLATECOLL));
619 dev_info(emac_dev, "EMAC: tx_underrun:%d\n",
620 emac_read(EMAC_TXUNDERRUN));
621 dev_info(emac_dev, "EMAC: tx_carrier_sense_errors:%d\n",
622 emac_read(EMAC_TXCARRIERSENSE));
623 dev_info(emac_dev, "EMAC: tx_octets:%d\n",
624 emac_read(EMAC_TXOCTETS));
625 dev_info(emac_dev, "EMAC: net_octets:%d\n",
626 emac_read(EMAC_NETOCTETS));
627 dev_info(emac_dev, "EMAC: rx_sof_overruns:%d\n",
628 emac_read(EMAC_RXSOFOVERRUNS));
629 dev_info(emac_dev, "EMAC: rx_mof_overruns:%d\n",
630 emac_read(EMAC_RXMOFOVERRUNS));
631 dev_info(emac_dev, "EMAC: rx_dma_overruns:%d\n",
632 emac_read(EMAC_RXDMAOVERRUNS));
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -0400633
634 cpdma_ctlr_dump(priv->dma);
Anant Golea6286ee2009-05-18 15:19:01 -0700635}
636
Anant Golea6286ee2009-05-18 15:19:01 -0700637/**
638 * emac_get_drvinfo: Get EMAC driver information
639 * @ndev: The DaVinci EMAC network adapter
640 * @info: ethtool info structure containing name and version
641 *
642 * Returns EMAC driver information (name and version)
643 *
644 */
645static void emac_get_drvinfo(struct net_device *ndev,
646 struct ethtool_drvinfo *info)
647{
648 strcpy(info->driver, emac_version_string);
649 strcpy(info->version, EMAC_MODULE_VERSION);
650}
651
652/**
653 * emac_get_settings: Get EMAC settings
654 * @ndev: The DaVinci EMAC network adapter
655 * @ecmd: ethtool command
656 *
657 * Executes ethool get command
658 *
659 */
660static int emac_get_settings(struct net_device *ndev,
661 struct ethtool_cmd *ecmd)
662{
663 struct emac_priv *priv = netdev_priv(ndev);
Cyril Chemparathy5d69e002010-09-15 10:11:24 -0400664 if (priv->phydev)
Anant Golea6286ee2009-05-18 15:19:01 -0700665 return phy_ethtool_gset(priv->phydev, ecmd);
666 else
667 return -EOPNOTSUPP;
668
669}
670
671/**
672 * emac_set_settings: Set EMAC settings
673 * @ndev: The DaVinci EMAC network adapter
674 * @ecmd: ethtool command
675 *
676 * Executes ethool set command
677 *
678 */
679static int emac_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
680{
681 struct emac_priv *priv = netdev_priv(ndev);
Cyril Chemparathy5d69e002010-09-15 10:11:24 -0400682 if (priv->phydev)
Anant Golea6286ee2009-05-18 15:19:01 -0700683 return phy_ethtool_sset(priv->phydev, ecmd);
684 else
685 return -EOPNOTSUPP;
686
687}
688
689/**
Sriram84da2652010-07-29 02:33:58 +0000690 * emac_get_coalesce : Get interrupt coalesce settings for this device
691 * @ndev : The DaVinci EMAC network adapter
692 * @coal : ethtool coalesce settings structure
693 *
694 * Fetch the current interrupt coalesce settings
695 *
696 */
697static int emac_get_coalesce(struct net_device *ndev,
698 struct ethtool_coalesce *coal)
699{
700 struct emac_priv *priv = netdev_priv(ndev);
701
702 coal->rx_coalesce_usecs = priv->coal_intvl;
703 return 0;
704
705}
706
707/**
708 * emac_set_coalesce : Set interrupt coalesce settings for this device
709 * @ndev : The DaVinci EMAC network adapter
710 * @coal : ethtool coalesce settings structure
711 *
712 * Set interrupt coalesce parameters
713 *
714 */
715static int emac_set_coalesce(struct net_device *ndev,
716 struct ethtool_coalesce *coal)
717{
718 struct emac_priv *priv = netdev_priv(ndev);
719 u32 int_ctrl, num_interrupts = 0;
720 u32 prescale = 0, addnl_dvdr = 1, coal_intvl = 0;
721
722 if (!coal->rx_coalesce_usecs)
723 return -EINVAL;
724
725 coal_intvl = coal->rx_coalesce_usecs;
726
727 switch (priv->version) {
728 case EMAC_VERSION_2:
729 int_ctrl = emac_ctrl_read(EMAC_DM646X_CMINTCTRL);
730 prescale = priv->bus_freq_mhz * 4;
731
732 if (coal_intvl < EMAC_DM646X_CMINTMIN_INTVL)
733 coal_intvl = EMAC_DM646X_CMINTMIN_INTVL;
734
735 if (coal_intvl > EMAC_DM646X_CMINTMAX_INTVL) {
736 /*
737 * Interrupt pacer works with 4us Pulse, we can
738 * throttle further by dilating the 4us pulse.
739 */
740 addnl_dvdr = EMAC_DM646X_INTPRESCALE_MASK / prescale;
741
742 if (addnl_dvdr > 1) {
743 prescale *= addnl_dvdr;
744 if (coal_intvl > (EMAC_DM646X_CMINTMAX_INTVL
745 * addnl_dvdr))
746 coal_intvl = (EMAC_DM646X_CMINTMAX_INTVL
747 * addnl_dvdr);
748 } else {
749 addnl_dvdr = 1;
750 coal_intvl = EMAC_DM646X_CMINTMAX_INTVL;
751 }
752 }
753
754 num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
755
756 int_ctrl |= EMAC_DM646X_INTPACEEN;
757 int_ctrl &= (~EMAC_DM646X_INTPRESCALE_MASK);
758 int_ctrl |= (prescale & EMAC_DM646X_INTPRESCALE_MASK);
759 emac_ctrl_write(EMAC_DM646X_CMINTCTRL, int_ctrl);
760
761 emac_ctrl_write(EMAC_DM646X_CMRXINTMAX, num_interrupts);
762 emac_ctrl_write(EMAC_DM646X_CMTXINTMAX, num_interrupts);
763
764 break;
765 default:
766 int_ctrl = emac_ctrl_read(EMAC_CTRL_EWINTTCNT);
767 int_ctrl &= (~EMAC_DM644X_EWINTCNT_MASK);
768 prescale = coal_intvl * priv->bus_freq_mhz;
769 if (prescale > EMAC_DM644X_EWINTCNT_MASK) {
770 prescale = EMAC_DM644X_EWINTCNT_MASK;
771 coal_intvl = prescale / priv->bus_freq_mhz;
772 }
773 emac_ctrl_write(EMAC_CTRL_EWINTTCNT, (int_ctrl | prescale));
774
775 break;
776 }
777
778 printk(KERN_INFO"Set coalesce to %d usecs.\n", coal_intvl);
779 priv->coal_intvl = coal_intvl;
780
781 return 0;
782
783}
784
785
786/**
Anant Golea6286ee2009-05-18 15:19:01 -0700787 * ethtool_ops: DaVinci EMAC Ethtool structure
788 *
789 * Ethtool support for EMAC adapter
790 *
791 */
792static const struct ethtool_ops ethtool_ops = {
793 .get_drvinfo = emac_get_drvinfo,
794 .get_settings = emac_get_settings,
795 .set_settings = emac_set_settings,
796 .get_link = ethtool_op_get_link,
Sriram84da2652010-07-29 02:33:58 +0000797 .get_coalesce = emac_get_coalesce,
798 .set_coalesce = emac_set_coalesce,
Anant Golea6286ee2009-05-18 15:19:01 -0700799};
800
801/**
802 * emac_update_phystatus: Update Phy status
803 * @priv: The DaVinci EMAC private adapter structure
804 *
805 * Updates phy status and takes action for network queue if required
806 * based upon link status
807 *
808 */
809static void emac_update_phystatus(struct emac_priv *priv)
810{
811 u32 mac_control;
812 u32 new_duplex;
813 u32 cur_duplex;
814 struct net_device *ndev = priv->ndev;
815
816 mac_control = emac_read(EMAC_MACCONTROL);
817 cur_duplex = (mac_control & EMAC_MACCONTROL_FULLDUPLEXEN) ?
818 DUPLEX_FULL : DUPLEX_HALF;
Cyril Chemparathy5d69e002010-09-15 10:11:24 -0400819 if (priv->phydev)
Anant Golea6286ee2009-05-18 15:19:01 -0700820 new_duplex = priv->phydev->duplex;
821 else
822 new_duplex = DUPLEX_FULL;
823
824 /* We get called only if link has changed (speed/duplex/status) */
825 if ((priv->link) && (new_duplex != cur_duplex)) {
826 priv->duplex = new_duplex;
827 if (DUPLEX_FULL == priv->duplex)
828 mac_control |= (EMAC_MACCONTROL_FULLDUPLEXEN);
829 else
830 mac_control &= ~(EMAC_MACCONTROL_FULLDUPLEXEN);
831 }
832
833 if (priv->speed == SPEED_1000 && (priv->version == EMAC_VERSION_2)) {
834 mac_control = emac_read(EMAC_MACCONTROL);
chaithrika@ti.com69ef9692009-10-01 10:25:19 +0000835 mac_control |= (EMAC_DM646X_MACCONTORL_GIG |
Anant Golea6286ee2009-05-18 15:19:01 -0700836 EMAC_DM646X_MACCONTORL_GIGFORCE);
837 } else {
838 /* Clear the GIG bit and GIGFORCE bit */
839 mac_control &= ~(EMAC_DM646X_MACCONTORL_GIGFORCE |
840 EMAC_DM646X_MACCONTORL_GIG);
841
842 if (priv->rmii_en && (priv->speed == SPEED_100))
843 mac_control |= EMAC_MACCONTROL_RMIISPEED_MASK;
844 else
845 mac_control &= ~EMAC_MACCONTROL_RMIISPEED_MASK;
846 }
847
848 /* Update mac_control if changed */
849 emac_write(EMAC_MACCONTROL, mac_control);
850
851 if (priv->link) {
852 /* link ON */
853 if (!netif_carrier_ok(ndev))
854 netif_carrier_on(ndev);
855 /* reactivate the transmit queue if it is stopped */
856 if (netif_running(ndev) && netif_queue_stopped(ndev))
857 netif_wake_queue(ndev);
858 } else {
859 /* link OFF */
860 if (netif_carrier_ok(ndev))
861 netif_carrier_off(ndev);
862 if (!netif_queue_stopped(ndev))
863 netif_stop_queue(ndev);
864 }
865}
866
867/**
868 * hash_get: Calculate hash value from mac address
869 * @addr: mac address to delete from hash table
870 *
871 * Calculates hash value from mac address
872 *
873 */
874static u32 hash_get(u8 *addr)
875{
876 u32 hash;
877 u8 tmpval;
878 int cnt;
879 hash = 0;
880
881 for (cnt = 0; cnt < 2; cnt++) {
882 tmpval = *addr++;
883 hash ^= (tmpval >> 2) ^ (tmpval << 4);
884 tmpval = *addr++;
885 hash ^= (tmpval >> 4) ^ (tmpval << 2);
886 tmpval = *addr++;
887 hash ^= (tmpval >> 6) ^ (tmpval);
888 }
889
890 return hash & 0x3F;
891}
892
893/**
894 * hash_add: Hash function to add mac addr from hash table
895 * @priv: The DaVinci EMAC private adapter structure
896 * mac_addr: mac address to delete from hash table
897 *
898 * Adds mac address to the internal hash table
899 *
900 */
901static int hash_add(struct emac_priv *priv, u8 *mac_addr)
902{
903 struct device *emac_dev = &priv->ndev->dev;
904 u32 rc = 0;
905 u32 hash_bit;
906 u32 hash_value = hash_get(mac_addr);
907
908 if (hash_value >= EMAC_NUM_MULTICAST_BITS) {
909 if (netif_msg_drv(priv)) {
910 dev_err(emac_dev, "DaVinci EMAC: hash_add(): Invalid "\
911 "Hash %08x, should not be greater than %08x",
912 hash_value, (EMAC_NUM_MULTICAST_BITS - 1));
913 }
914 return -1;
915 }
916
917 /* set the hash bit only if not previously set */
918 if (priv->multicast_hash_cnt[hash_value] == 0) {
919 rc = 1; /* hash value changed */
920 if (hash_value < 32) {
921 hash_bit = BIT(hash_value);
922 priv->mac_hash1 |= hash_bit;
923 } else {
924 hash_bit = BIT((hash_value - 32));
925 priv->mac_hash2 |= hash_bit;
926 }
927 }
928
929 /* incr counter for num of mcast addr's mapped to "this" hash bit */
930 ++priv->multicast_hash_cnt[hash_value];
931
932 return rc;
933}
934
935/**
936 * hash_del: Hash function to delete mac addr from hash table
937 * @priv: The DaVinci EMAC private adapter structure
938 * mac_addr: mac address to delete from hash table
939 *
940 * Removes mac address from the internal hash table
941 *
942 */
943static int hash_del(struct emac_priv *priv, u8 *mac_addr)
944{
945 u32 hash_value;
946 u32 hash_bit;
947
948 hash_value = hash_get(mac_addr);
949 if (priv->multicast_hash_cnt[hash_value] > 0) {
950 /* dec cntr for num of mcast addr's mapped to this hash bit */
951 --priv->multicast_hash_cnt[hash_value];
952 }
953
954 /* if counter still > 0, at least one multicast address refers
955 * to this hash bit. so return 0 */
956 if (priv->multicast_hash_cnt[hash_value] > 0)
957 return 0;
958
959 if (hash_value < 32) {
960 hash_bit = BIT(hash_value);
961 priv->mac_hash1 &= ~hash_bit;
962 } else {
963 hash_bit = BIT((hash_value - 32));
964 priv->mac_hash2 &= ~hash_bit;
965 }
966
967 /* return 1 to indicate change in mac_hash registers reqd */
968 return 1;
969}
970
971/* EMAC multicast operation */
972#define EMAC_MULTICAST_ADD 0
973#define EMAC_MULTICAST_DEL 1
974#define EMAC_ALL_MULTI_SET 2
975#define EMAC_ALL_MULTI_CLR 3
976
977/**
978 * emac_add_mcast: Set multicast address in the EMAC adapter (Internal)
979 * @priv: The DaVinci EMAC private adapter structure
980 * @action: multicast operation to perform
981 * mac_addr: mac address to set
982 *
983 * Set multicast addresses in EMAC adapter - internal function
984 *
985 */
986static void emac_add_mcast(struct emac_priv *priv, u32 action, u8 *mac_addr)
987{
988 struct device *emac_dev = &priv->ndev->dev;
989 int update = -1;
990
991 switch (action) {
992 case EMAC_MULTICAST_ADD:
993 update = hash_add(priv, mac_addr);
994 break;
995 case EMAC_MULTICAST_DEL:
996 update = hash_del(priv, mac_addr);
997 break;
998 case EMAC_ALL_MULTI_SET:
999 update = 1;
1000 priv->mac_hash1 = EMAC_ALL_MULTI_REG_VALUE;
1001 priv->mac_hash2 = EMAC_ALL_MULTI_REG_VALUE;
1002 break;
1003 case EMAC_ALL_MULTI_CLR:
1004 update = 1;
1005 priv->mac_hash1 = 0;
1006 priv->mac_hash2 = 0;
1007 memset(&(priv->multicast_hash_cnt[0]), 0,
1008 sizeof(priv->multicast_hash_cnt[0]) *
1009 EMAC_NUM_MULTICAST_BITS);
1010 break;
1011 default:
1012 if (netif_msg_drv(priv))
1013 dev_err(emac_dev, "DaVinci EMAC: add_mcast"\
1014 ": bad operation %d", action);
1015 break;
1016 }
1017
1018 /* write to the hardware only if the register status chances */
1019 if (update > 0) {
1020 emac_write(EMAC_MACHASH1, priv->mac_hash1);
1021 emac_write(EMAC_MACHASH2, priv->mac_hash2);
1022 }
1023}
1024
1025/**
1026 * emac_dev_mcast_set: Set multicast address in the EMAC adapter
1027 * @ndev: The DaVinci EMAC network adapter
1028 *
1029 * Set multicast addresses in EMAC adapter
1030 *
1031 */
1032static void emac_dev_mcast_set(struct net_device *ndev)
1033{
1034 u32 mbp_enable;
1035 struct emac_priv *priv = netdev_priv(ndev);
1036
1037 mbp_enable = emac_read(EMAC_RXMBPENABLE);
1038 if (ndev->flags & IFF_PROMISC) {
1039 mbp_enable &= (~EMAC_MBP_PROMISCCH(EMAC_DEF_PROM_CH));
1040 mbp_enable |= (EMAC_MBP_RXPROMISC);
1041 } else {
1042 mbp_enable = (mbp_enable & ~EMAC_MBP_RXPROMISC);
1043 if ((ndev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001044 netdev_mc_count(ndev) > EMAC_DEF_MAX_MULTICAST_ADDRESSES) {
Anant Golea6286ee2009-05-18 15:19:01 -07001045 mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
1046 emac_add_mcast(priv, EMAC_ALL_MULTI_SET, NULL);
1047 }
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001048 if (!netdev_mc_empty(ndev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001049 struct netdev_hw_addr *ha;
1050
Anant Golea6286ee2009-05-18 15:19:01 -07001051 mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
1052 emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
1053 /* program multicast address list into EMAC hardware */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001054 netdev_for_each_mc_addr(ha, ndev) {
Anant Golea6286ee2009-05-18 15:19:01 -07001055 emac_add_mcast(priv, EMAC_MULTICAST_ADD,
Jiri Pirko22bedad32010-04-01 21:22:57 +00001056 (u8 *) ha->addr);
Anant Golea6286ee2009-05-18 15:19:01 -07001057 }
1058 } else {
1059 mbp_enable = (mbp_enable & ~EMAC_MBP_RXMCAST);
1060 emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
1061 }
1062 }
1063 /* Set mbp config register */
1064 emac_write(EMAC_RXMBPENABLE, mbp_enable);
1065}
1066
1067/*************************************************************************
1068 * EMAC Hardware manipulation
1069 *************************************************************************/
1070
1071/**
1072 * emac_int_disable: Disable EMAC module interrupt (from adapter)
1073 * @priv: The DaVinci EMAC private adapter structure
1074 *
1075 * Disable EMAC interrupt on the adapter
1076 *
1077 */
1078static void emac_int_disable(struct emac_priv *priv)
1079{
1080 if (priv->version == EMAC_VERSION_2) {
1081 unsigned long flags;
1082
1083 local_irq_save(flags);
1084
1085 /* Program C0_Int_En to zero to turn off
1086 * interrupts to the CPU */
1087 emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0x0);
1088 emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0x0);
1089 /* NOTE: Rx Threshold and Misc interrupts are not disabled */
Sriramakrishnan01a9af32009-11-19 15:58:26 +05301090 if (priv->int_disable)
1091 priv->int_disable();
Anant Golea6286ee2009-05-18 15:19:01 -07001092
1093 local_irq_restore(flags);
1094
1095 } else {
1096 /* Set DM644x control registers for interrupt control */
1097 emac_ctrl_write(EMAC_CTRL_EWCTL, 0x0);
1098 }
1099}
1100
1101/**
1102 * emac_int_enable: Enable EMAC module interrupt (from adapter)
1103 * @priv: The DaVinci EMAC private adapter structure
1104 *
1105 * Enable EMAC interrupt on the adapter
1106 *
1107 */
1108static void emac_int_enable(struct emac_priv *priv)
1109{
1110 if (priv->version == EMAC_VERSION_2) {
Sriramakrishnan01a9af32009-11-19 15:58:26 +05301111 if (priv->int_enable)
1112 priv->int_enable();
1113
Anant Golea6286ee2009-05-18 15:19:01 -07001114 emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0xff);
1115 emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0xff);
1116
1117 /* In addition to turning on interrupt Enable, we need
1118 * ack by writing appropriate values to the EOI
1119 * register */
1120
1121 /* NOTE: Rx Threshold and Misc interrupts are not enabled */
1122
1123 /* ack rxen only then a new pulse will be generated */
1124 emac_write(EMAC_DM646X_MACEOIVECTOR,
1125 EMAC_DM646X_MAC_EOI_C0_RXEN);
1126
1127 /* ack txen- only then a new pulse will be generated */
1128 emac_write(EMAC_DM646X_MACEOIVECTOR,
1129 EMAC_DM646X_MAC_EOI_C0_TXEN);
1130
1131 } else {
1132 /* Set DM644x control registers for interrupt control */
1133 emac_ctrl_write(EMAC_CTRL_EWCTL, 0x1);
1134 }
1135}
1136
1137/**
1138 * emac_irq: EMAC interrupt handler
1139 * @irq: interrupt number
1140 * @dev_id: EMAC network adapter data structure ptr
1141 *
1142 * EMAC Interrupt handler - we only schedule NAPI and not process any packets
1143 * here. EVen the interrupt status is checked (TX/RX/Err) in NAPI poll function
1144 *
1145 * Returns interrupt handled condition
1146 */
1147static irqreturn_t emac_irq(int irq, void *dev_id)
1148{
1149 struct net_device *ndev = (struct net_device *)dev_id;
1150 struct emac_priv *priv = netdev_priv(ndev);
1151
1152 ++priv->isr_count;
1153 if (likely(netif_running(priv->ndev))) {
1154 emac_int_disable(priv);
1155 napi_schedule(&priv->napi);
1156 } else {
1157 /* we are closing down, so dont process anything */
1158 }
1159 return IRQ_HANDLED;
1160}
1161
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001162static struct sk_buff *emac_rx_alloc(struct emac_priv *priv)
1163{
1164 struct sk_buff *skb = dev_alloc_skb(priv->rx_buf_size);
1165 if (WARN_ON(!skb))
1166 return NULL;
1167 skb->dev = priv->ndev;
1168 skb_reserve(skb, NET_IP_ALIGN);
1169 return skb;
1170}
1171
1172static void emac_rx_handler(void *token, int len, int status)
1173{
1174 struct sk_buff *skb = token;
1175 struct net_device *ndev = skb->dev;
1176 struct emac_priv *priv = netdev_priv(ndev);
1177 struct device *emac_dev = &ndev->dev;
1178 int ret;
1179
1180 /* free and bail if we are shutting down */
1181 if (unlikely(!netif_running(ndev))) {
1182 dev_kfree_skb_any(skb);
1183 return;
1184 }
1185
1186 /* recycle on recieve error */
1187 if (status < 0) {
1188 ndev->stats.rx_errors++;
1189 goto recycle;
1190 }
1191
1192 /* feed received packet up the stack */
1193 skb_put(skb, len);
1194 skb->protocol = eth_type_trans(skb, ndev);
1195 netif_receive_skb(skb);
1196 ndev->stats.rx_bytes += len;
1197 ndev->stats.rx_packets++;
1198
1199 /* alloc a new packet for receive */
1200 skb = emac_rx_alloc(priv);
1201 if (!skb) {
1202 if (netif_msg_rx_err(priv) && net_ratelimit())
1203 dev_err(emac_dev, "failed rx buffer alloc\n");
1204 return;
1205 }
1206
1207recycle:
1208 ret = cpdma_chan_submit(priv->rxchan, skb, skb->data,
1209 skb_tailroom(skb), GFP_KERNEL);
1210 if (WARN_ON(ret < 0))
1211 dev_kfree_skb_any(skb);
1212}
1213
1214static void emac_tx_handler(void *token, int len, int status)
1215{
1216 struct sk_buff *skb = token;
1217 struct net_device *ndev = skb->dev;
1218
1219 if (unlikely(netif_queue_stopped(ndev)))
1220 netif_start_queue(ndev);
1221 ndev->stats.tx_packets++;
1222 ndev->stats.tx_bytes += len;
1223 dev_kfree_skb_any(skb);
1224}
1225
Anant Golea6286ee2009-05-18 15:19:01 -07001226/** EMAC on-chip buffer descriptor memory
1227 *
1228 * WARNING: Please note that the on chip memory is used for both TX and RX
1229 * buffer descriptor queues and is equally divided between TX and RX desc's
1230 * If the number of TX or RX descriptors change this memory pointers need
1231 * to be adjusted. If external memory is allocated then these pointers can
1232 * pointer to the memory
1233 *
1234 */
1235#define EMAC_TX_BD_MEM(priv) ((priv)->emac_ctrl_ram)
1236#define EMAC_RX_BD_MEM(priv) ((priv)->emac_ctrl_ram + \
1237 (((priv)->ctrl_ram_size) >> 1))
1238
1239/**
1240 * emac_init_txch: TX channel initialization
1241 * @priv: The DaVinci EMAC private adapter structure
1242 * @ch: RX channel number
1243 *
1244 * Called during device init to setup a TX channel (allocate buffer desc
1245 * create free pool and keep ready for transmission
1246 *
1247 * Returns success(0) or mem alloc failures error code
1248 */
1249static int emac_init_txch(struct emac_priv *priv, u32 ch)
1250{
1251 struct device *emac_dev = &priv->ndev->dev;
1252 u32 cnt, bd_size;
1253 void __iomem *mem;
1254 struct emac_tx_bd __iomem *curr_bd;
1255 struct emac_txch *txch = NULL;
1256
1257 txch = kzalloc(sizeof(struct emac_txch), GFP_KERNEL);
1258 if (NULL == txch) {
1259 dev_err(emac_dev, "DaVinci EMAC: TX Ch mem alloc failed");
1260 return -ENOMEM;
1261 }
1262 priv->txch[ch] = txch;
1263 txch->service_max = EMAC_DEF_TX_MAX_SERVICE;
1264 txch->active_queue_head = NULL;
1265 txch->active_queue_tail = NULL;
1266 txch->queue_active = 0;
1267 txch->teardown_pending = 0;
1268
1269 /* allocate memory for TX CPPI channel on a 4 byte boundry */
1270 txch->tx_complete = kzalloc(txch->service_max * sizeof(u32),
1271 GFP_KERNEL);
1272 if (NULL == txch->tx_complete) {
1273 dev_err(emac_dev, "DaVinci EMAC: Tx service mem alloc failed");
1274 kfree(txch);
1275 return -ENOMEM;
1276 }
1277
1278 /* allocate buffer descriptor pool align every BD on four word
1279 * boundry for future requirements */
1280 bd_size = (sizeof(struct emac_tx_bd) + 0xF) & ~0xF;
1281 txch->num_bd = (priv->ctrl_ram_size >> 1) / bd_size;
1282 txch->alloc_size = (((bd_size * txch->num_bd) + 0xF) & ~0xF);
1283
1284 /* alloc TX BD memory */
1285 txch->bd_mem = EMAC_TX_BD_MEM(priv);
1286 __memzero((void __force *)txch->bd_mem, txch->alloc_size);
1287
1288 /* initialize the BD linked list */
1289 mem = (void __force __iomem *)
1290 (((u32 __force) txch->bd_mem + 0xF) & ~0xF);
1291 txch->bd_pool_head = NULL;
1292 for (cnt = 0; cnt < txch->num_bd; cnt++) {
1293 curr_bd = mem + (cnt * bd_size);
1294 curr_bd->next = txch->bd_pool_head;
1295 txch->bd_pool_head = curr_bd;
1296 }
1297
1298 /* reset statistics counters */
1299 txch->out_of_tx_bd = 0;
1300 txch->no_active_pkts = 0;
1301 txch->active_queue_count = 0;
1302
1303 return 0;
1304}
1305
1306/**
1307 * emac_cleanup_txch: Book-keep function to clean TX channel resources
1308 * @priv: The DaVinci EMAC private adapter structure
1309 * @ch: TX channel number
1310 *
1311 * Called to clean up TX channel resources
1312 *
1313 */
1314static void emac_cleanup_txch(struct emac_priv *priv, u32 ch)
1315{
1316 struct emac_txch *txch = priv->txch[ch];
1317
1318 if (txch) {
1319 if (txch->bd_mem)
1320 txch->bd_mem = NULL;
1321 kfree(txch->tx_complete);
1322 kfree(txch);
1323 priv->txch[ch] = NULL;
1324 }
1325}
1326
1327/**
1328 * emac_net_tx_complete: TX packet completion function
1329 * @priv: The DaVinci EMAC private adapter structure
1330 * @net_data_tokens: packet token - skb pointer
1331 * @num_tokens: number of skb's to free
1332 * @ch: TX channel number
1333 *
1334 * Frees the skb once packet is transmitted
1335 *
1336 */
1337static int emac_net_tx_complete(struct emac_priv *priv,
1338 void **net_data_tokens,
1339 int num_tokens, u32 ch)
1340{
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00001341 struct net_device *ndev = priv->ndev;
Anant Golea6286ee2009-05-18 15:19:01 -07001342 u32 cnt;
1343
Henrique Camargoeabd8ba2010-08-02 17:10:42 +00001344 if (unlikely(num_tokens && netif_queue_stopped(ndev)))
1345 netif_start_queue(ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07001346 for (cnt = 0; cnt < num_tokens; cnt++) {
1347 struct sk_buff *skb = (struct sk_buff *)net_data_tokens[cnt];
1348 if (skb == NULL)
1349 continue;
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00001350 ndev->stats.tx_packets++;
1351 ndev->stats.tx_bytes += skb->len;
Anant Golea6286ee2009-05-18 15:19:01 -07001352 dev_kfree_skb_any(skb);
1353 }
1354 return 0;
1355}
1356
1357/**
1358 * emac_txch_teardown: TX channel teardown
1359 * @priv: The DaVinci EMAC private adapter structure
1360 * @ch: TX channel number
1361 *
1362 * Called to teardown TX channel
1363 *
1364 */
1365static void emac_txch_teardown(struct emac_priv *priv, u32 ch)
1366{
1367 struct device *emac_dev = &priv->ndev->dev;
1368 u32 teardown_cnt = 0xFFFFFFF0; /* Some high value */
1369 struct emac_txch *txch = priv->txch[ch];
1370 struct emac_tx_bd __iomem *curr_bd;
1371
1372 while ((emac_read(EMAC_TXCP(ch)) & EMAC_TEARDOWN_VALUE) !=
1373 EMAC_TEARDOWN_VALUE) {
1374 /* wait till tx teardown complete */
1375 cpu_relax(); /* TODO: check if this helps ... */
1376 --teardown_cnt;
1377 if (0 == teardown_cnt) {
1378 dev_err(emac_dev, "EMAC: TX teardown aborted\n");
1379 break;
1380 }
1381 }
1382 emac_write(EMAC_TXCP(ch), EMAC_TEARDOWN_VALUE);
1383
1384 /* process sent packets and return skb's to upper layer */
1385 if (1 == txch->queue_active) {
1386 curr_bd = txch->active_queue_head;
1387 while (curr_bd != NULL) {
Sekhar Noribe5bce22010-03-09 01:20:37 +00001388 dma_unmap_single(emac_dev, curr_bd->buff_ptr,
1389 curr_bd->off_b_len & EMAC_RX_BD_BUF_SIZE,
1390 DMA_TO_DEVICE);
1391
Anant Golea6286ee2009-05-18 15:19:01 -07001392 emac_net_tx_complete(priv, (void __force *)
1393 &curr_bd->buf_token, 1, ch);
1394 if (curr_bd != txch->active_queue_tail)
1395 curr_bd = curr_bd->next;
1396 else
1397 break;
1398 }
1399 txch->bd_pool_head = txch->active_queue_head;
1400 txch->active_queue_head =
1401 txch->active_queue_tail = NULL;
1402 }
1403}
1404
1405/**
1406 * emac_stop_txch: Stop TX channel operation
1407 * @priv: The DaVinci EMAC private adapter structure
1408 * @ch: TX channel number
1409 *
1410 * Called to stop TX channel operation
1411 *
1412 */
1413static void emac_stop_txch(struct emac_priv *priv, u32 ch)
1414{
1415 struct emac_txch *txch = priv->txch[ch];
1416
1417 if (txch) {
1418 txch->teardown_pending = 1;
1419 emac_write(EMAC_TXTEARDOWN, 0);
1420 emac_txch_teardown(priv, ch);
1421 txch->teardown_pending = 0;
1422 emac_write(EMAC_TXINTMASKCLEAR, BIT(ch));
1423 }
1424}
1425
1426/**
1427 * emac_tx_bdproc: TX buffer descriptor (packet) processing
1428 * @priv: The DaVinci EMAC private adapter structure
1429 * @ch: TX channel number to process buffer descriptors for
1430 * @budget: number of packets allowed to process
1431 * @pending: indication to caller that packets are pending to process
1432 *
1433 * Processes TX buffer descriptors after packets are transmitted - checks
1434 * ownership bit on the TX * descriptor and requeues it to free pool & frees
1435 * the SKB buffer. Only "budget" number of packets are processed and
1436 * indication of pending packets provided to the caller
1437 *
1438 * Returns number of packets processed
1439 */
1440static int emac_tx_bdproc(struct emac_priv *priv, u32 ch, u32 budget)
1441{
1442 struct device *emac_dev = &priv->ndev->dev;
1443 unsigned long flags;
1444 u32 frame_status;
1445 u32 pkts_processed = 0;
1446 u32 tx_complete_cnt = 0;
1447 struct emac_tx_bd __iomem *curr_bd;
1448 struct emac_txch *txch = priv->txch[ch];
1449 u32 *tx_complete_ptr = txch->tx_complete;
1450
1451 if (unlikely(1 == txch->teardown_pending)) {
1452 if (netif_msg_tx_err(priv) && net_ratelimit()) {
1453 dev_err(emac_dev, "DaVinci EMAC:emac_tx_bdproc: "\
1454 "teardown pending\n");
1455 }
1456 return 0; /* dont handle any pkt completions */
1457 }
1458
1459 ++txch->proc_count;
1460 spin_lock_irqsave(&priv->tx_lock, flags);
1461 curr_bd = txch->active_queue_head;
1462 if (NULL == curr_bd) {
1463 emac_write(EMAC_TXCP(ch),
Sriramakrishnanad021ae2009-11-19 15:58:27 +05301464 emac_virt_to_phys(txch->last_hw_bdprocessed, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07001465 txch->no_active_pkts++;
1466 spin_unlock_irqrestore(&priv->tx_lock, flags);
1467 return 0;
1468 }
1469 BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1470 frame_status = curr_bd->mode;
1471 while ((curr_bd) &&
1472 ((frame_status & EMAC_CPPI_OWNERSHIP_BIT) == 0) &&
1473 (pkts_processed < budget)) {
Sriramakrishnanad021ae2009-11-19 15:58:27 +05301474 emac_write(EMAC_TXCP(ch), emac_virt_to_phys(curr_bd, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07001475 txch->active_queue_head = curr_bd->next;
1476 if (frame_status & EMAC_CPPI_EOQ_BIT) {
1477 if (curr_bd->next) { /* misqueued packet */
1478 emac_write(EMAC_TXHDP(ch), curr_bd->h_next);
1479 ++txch->mis_queued_packets;
1480 } else {
1481 txch->queue_active = 0; /* end of queue */
1482 }
1483 }
Sekhar Noribe5bce22010-03-09 01:20:37 +00001484
1485 dma_unmap_single(emac_dev, curr_bd->buff_ptr,
1486 curr_bd->off_b_len & EMAC_RX_BD_BUF_SIZE,
1487 DMA_TO_DEVICE);
1488
Anant Golea6286ee2009-05-18 15:19:01 -07001489 *tx_complete_ptr = (u32) curr_bd->buf_token;
1490 ++tx_complete_ptr;
1491 ++tx_complete_cnt;
1492 curr_bd->next = txch->bd_pool_head;
1493 txch->bd_pool_head = curr_bd;
1494 --txch->active_queue_count;
1495 pkts_processed++;
1496 txch->last_hw_bdprocessed = curr_bd;
1497 curr_bd = txch->active_queue_head;
1498 if (curr_bd) {
1499 BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1500 frame_status = curr_bd->mode;
1501 }
1502 } /* end of pkt processing loop */
1503
1504 emac_net_tx_complete(priv,
1505 (void *)&txch->tx_complete[0],
1506 tx_complete_cnt, ch);
1507 spin_unlock_irqrestore(&priv->tx_lock, flags);
1508 return pkts_processed;
1509}
1510
1511#define EMAC_ERR_TX_OUT_OF_BD -1
1512
1513/**
1514 * emac_send: EMAC Transmit function (internal)
1515 * @priv: The DaVinci EMAC private adapter structure
1516 * @pkt: packet pointer (contains skb ptr)
1517 * @ch: TX channel number
1518 *
1519 * Called by the transmit function to queue the packet in EMAC hardware queue
1520 *
1521 * Returns success(0) or error code (typically out of desc's)
1522 */
1523static int emac_send(struct emac_priv *priv, struct emac_netpktobj *pkt, u32 ch)
1524{
1525 unsigned long flags;
1526 struct emac_tx_bd __iomem *curr_bd;
1527 struct emac_txch *txch;
1528 struct emac_netbufobj *buf_list;
1529
1530 txch = priv->txch[ch];
1531 buf_list = pkt->buf_list; /* get handle to the buffer array */
1532
1533 /* check packet size and pad if short */
1534 if (pkt->pkt_length < EMAC_DEF_MIN_ETHPKTSIZE) {
1535 buf_list->length += (EMAC_DEF_MIN_ETHPKTSIZE - pkt->pkt_length);
1536 pkt->pkt_length = EMAC_DEF_MIN_ETHPKTSIZE;
1537 }
1538
1539 spin_lock_irqsave(&priv->tx_lock, flags);
1540 curr_bd = txch->bd_pool_head;
1541 if (curr_bd == NULL) {
1542 txch->out_of_tx_bd++;
1543 spin_unlock_irqrestore(&priv->tx_lock, flags);
1544 return EMAC_ERR_TX_OUT_OF_BD;
1545 }
1546
1547 txch->bd_pool_head = curr_bd->next;
1548 curr_bd->buf_token = buf_list->buf_token;
Sekhar Noribe5bce22010-03-09 01:20:37 +00001549 curr_bd->buff_ptr = dma_map_single(&priv->ndev->dev, buf_list->data_ptr,
1550 buf_list->length, DMA_TO_DEVICE);
Anant Golea6286ee2009-05-18 15:19:01 -07001551 curr_bd->off_b_len = buf_list->length;
1552 curr_bd->h_next = 0;
1553 curr_bd->next = NULL;
1554 curr_bd->mode = (EMAC_CPPI_SOP_BIT | EMAC_CPPI_OWNERSHIP_BIT |
1555 EMAC_CPPI_EOP_BIT | pkt->pkt_length);
1556
1557 /* flush the packet from cache if write back cache is present */
1558 BD_CACHE_WRITEBACK_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1559
1560 /* send the packet */
1561 if (txch->active_queue_head == NULL) {
1562 txch->active_queue_head = curr_bd;
1563 txch->active_queue_tail = curr_bd;
1564 if (1 != txch->queue_active) {
1565 emac_write(EMAC_TXHDP(ch),
Sriramakrishnanad021ae2009-11-19 15:58:27 +05301566 emac_virt_to_phys(curr_bd, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07001567 txch->queue_active = 1;
1568 }
1569 ++txch->queue_reinit;
1570 } else {
1571 register struct emac_tx_bd __iomem *tail_bd;
1572 register u32 frame_status;
1573
1574 tail_bd = txch->active_queue_tail;
1575 tail_bd->next = curr_bd;
1576 txch->active_queue_tail = curr_bd;
1577 tail_bd = EMAC_VIRT_NOCACHE(tail_bd);
Sriramakrishnanad021ae2009-11-19 15:58:27 +05301578 tail_bd->h_next = (int)emac_virt_to_phys(curr_bd, priv);
Anant Golea6286ee2009-05-18 15:19:01 -07001579 frame_status = tail_bd->mode;
1580 if (frame_status & EMAC_CPPI_EOQ_BIT) {
Sriramakrishnanad021ae2009-11-19 15:58:27 +05301581 emac_write(EMAC_TXHDP(ch),
1582 emac_virt_to_phys(curr_bd, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07001583 frame_status &= ~(EMAC_CPPI_EOQ_BIT);
1584 tail_bd->mode = frame_status;
1585 ++txch->end_of_queue_add;
1586 }
1587 }
1588 txch->active_queue_count++;
1589 spin_unlock_irqrestore(&priv->tx_lock, flags);
1590 return 0;
1591}
1592
1593/**
1594 * emac_dev_xmit: EMAC Transmit function
1595 * @skb: SKB pointer
1596 * @ndev: The DaVinci EMAC network adapter
1597 *
1598 * Called by the system to transmit a packet - we queue the packet in
1599 * EMAC hardware transmit queue
1600 *
1601 * Returns success(NETDEV_TX_OK) or error code (typically out of desc's)
1602 */
1603static int emac_dev_xmit(struct sk_buff *skb, struct net_device *ndev)
1604{
1605 struct device *emac_dev = &ndev->dev;
1606 int ret_code;
Anant Golea6286ee2009-05-18 15:19:01 -07001607 struct emac_priv *priv = netdev_priv(ndev);
1608
1609 /* If no link, return */
1610 if (unlikely(!priv->link)) {
1611 if (netif_msg_tx_err(priv) && net_ratelimit())
1612 dev_err(emac_dev, "DaVinci EMAC: No link to transmit");
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001613 goto fail_tx;
Anant Golea6286ee2009-05-18 15:19:01 -07001614 }
1615
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001616 ret_code = skb_padto(skb, EMAC_DEF_MIN_ETHPKTSIZE);
1617 if (unlikely(ret_code < 0)) {
1618 if (netif_msg_tx_err(priv) && net_ratelimit())
1619 dev_err(emac_dev, "DaVinci EMAC: packet pad failed");
1620 goto fail_tx;
1621 }
1622
1623 ret_code = cpdma_chan_submit(priv->txchan, skb, skb->data, skb->len,
1624 GFP_KERNEL);
Anant Golea6286ee2009-05-18 15:19:01 -07001625 if (unlikely(ret_code != 0)) {
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001626 if (netif_msg_tx_err(priv) && net_ratelimit())
1627 dev_err(emac_dev, "DaVinci EMAC: desc submit failed");
1628 goto fail_tx;
Anant Golea6286ee2009-05-18 15:19:01 -07001629 }
1630
1631 return NETDEV_TX_OK;
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001632
1633fail_tx:
1634 ndev->stats.tx_dropped++;
1635 netif_stop_queue(ndev);
1636 return NETDEV_TX_BUSY;
Anant Golea6286ee2009-05-18 15:19:01 -07001637}
1638
1639/**
1640 * emac_dev_tx_timeout: EMAC Transmit timeout function
1641 * @ndev: The DaVinci EMAC network adapter
1642 *
1643 * Called when system detects that a skb timeout period has expired
1644 * potentially due to a fault in the adapter in not being able to send
1645 * it out on the wire. We teardown the TX channel assuming a hardware
1646 * error and re-initialize the TX channel for hardware operation
1647 *
1648 */
1649static void emac_dev_tx_timeout(struct net_device *ndev)
1650{
1651 struct emac_priv *priv = netdev_priv(ndev);
1652 struct device *emac_dev = &ndev->dev;
1653
1654 if (netif_msg_tx_err(priv))
1655 dev_err(emac_dev, "DaVinci EMAC: xmit timeout, restarting TX");
1656
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001657 emac_dump_regs(priv);
1658
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00001659 ndev->stats.tx_errors++;
Anant Golea6286ee2009-05-18 15:19:01 -07001660 emac_int_disable(priv);
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001661 cpdma_chan_stop(priv->txchan);
1662 cpdma_chan_start(priv->txchan);
Anant Golea6286ee2009-05-18 15:19:01 -07001663 emac_int_enable(priv);
1664}
1665
1666/**
1667 * emac_net_alloc_rx_buf: Allocate a skb for RX
1668 * @priv: The DaVinci EMAC private adapter structure
1669 * @buf_size: size of SKB data buffer to allocate
1670 * @data_token: data token returned (skb handle for storing in buffer desc)
1671 * @ch: RX channel number
1672 *
1673 * Called during RX channel setup - allocates skb buffer of required size
1674 * and provides the skb handle and allocated buffer data pointer to caller
1675 *
1676 * Returns skb data pointer or 0 on failure to alloc skb
1677 */
1678static void *emac_net_alloc_rx_buf(struct emac_priv *priv, int buf_size,
1679 void **data_token, u32 ch)
1680{
1681 struct net_device *ndev = priv->ndev;
1682 struct device *emac_dev = &ndev->dev;
1683 struct sk_buff *p_skb;
1684
1685 p_skb = dev_alloc_skb(buf_size);
1686 if (unlikely(NULL == p_skb)) {
1687 if (netif_msg_rx_err(priv) && net_ratelimit())
1688 dev_err(emac_dev, "DaVinci EMAC: failed to alloc skb");
1689 return NULL;
1690 }
1691
1692 /* set device pointer in skb and reserve space for extra bytes */
1693 p_skb->dev = ndev;
1694 skb_reserve(p_skb, NET_IP_ALIGN);
1695 *data_token = (void *) p_skb;
Anant Golea6286ee2009-05-18 15:19:01 -07001696 return p_skb->data;
1697}
1698
1699/**
1700 * emac_init_rxch: RX channel initialization
1701 * @priv: The DaVinci EMAC private adapter structure
1702 * @ch: RX channel number
1703 * @param: mac address for RX channel
1704 *
1705 * Called during device init to setup a RX channel (allocate buffers and
1706 * buffer descriptors, create queue and keep ready for reception
1707 *
1708 * Returns success(0) or mem alloc failures error code
1709 */
1710static int emac_init_rxch(struct emac_priv *priv, u32 ch, char *param)
1711{
1712 struct device *emac_dev = &priv->ndev->dev;
1713 u32 cnt, bd_size;
1714 void __iomem *mem;
1715 struct emac_rx_bd __iomem *curr_bd;
1716 struct emac_rxch *rxch = NULL;
1717
1718 rxch = kzalloc(sizeof(struct emac_rxch), GFP_KERNEL);
1719 if (NULL == rxch) {
1720 dev_err(emac_dev, "DaVinci EMAC: RX Ch mem alloc failed");
1721 return -ENOMEM;
1722 }
1723 priv->rxch[ch] = rxch;
1724 rxch->buf_size = priv->rx_buf_size;
1725 rxch->service_max = EMAC_DEF_RX_MAX_SERVICE;
1726 rxch->queue_active = 0;
1727 rxch->teardown_pending = 0;
1728
1729 /* save mac address */
1730 for (cnt = 0; cnt < 6; cnt++)
1731 rxch->mac_addr[cnt] = param[cnt];
1732
1733 /* allocate buffer descriptor pool align every BD on four word
1734 * boundry for future requirements */
1735 bd_size = (sizeof(struct emac_rx_bd) + 0xF) & ~0xF;
1736 rxch->num_bd = (priv->ctrl_ram_size >> 1) / bd_size;
1737 rxch->alloc_size = (((bd_size * rxch->num_bd) + 0xF) & ~0xF);
1738 rxch->bd_mem = EMAC_RX_BD_MEM(priv);
1739 __memzero((void __force *)rxch->bd_mem, rxch->alloc_size);
1740 rxch->pkt_queue.buf_list = &rxch->buf_queue;
1741
1742 /* allocate RX buffer and initialize the BD linked list */
1743 mem = (void __force __iomem *)
1744 (((u32 __force) rxch->bd_mem + 0xF) & ~0xF);
1745 rxch->active_queue_head = NULL;
1746 rxch->active_queue_tail = mem;
1747 for (cnt = 0; cnt < rxch->num_bd; cnt++) {
1748 curr_bd = mem + (cnt * bd_size);
1749 /* for future use the last parameter contains the BD ptr */
1750 curr_bd->data_ptr = emac_net_alloc_rx_buf(priv,
1751 rxch->buf_size,
1752 (void __force **)&curr_bd->buf_token,
1753 EMAC_DEF_RX_CH);
1754 if (curr_bd->data_ptr == NULL) {
1755 dev_err(emac_dev, "DaVinci EMAC: RX buf mem alloc " \
1756 "failed for ch %d\n", ch);
1757 kfree(rxch);
1758 return -ENOMEM;
1759 }
1760
1761 /* populate the hardware descriptor */
Sriramakrishnanad021ae2009-11-19 15:58:27 +05301762 curr_bd->h_next = emac_virt_to_phys(rxch->active_queue_head,
1763 priv);
Sekhar Noribe5bce22010-03-09 01:20:37 +00001764 curr_bd->buff_ptr = dma_map_single(emac_dev, curr_bd->data_ptr,
1765 rxch->buf_size, DMA_FROM_DEVICE);
Anant Golea6286ee2009-05-18 15:19:01 -07001766 curr_bd->off_b_len = rxch->buf_size;
1767 curr_bd->mode = EMAC_CPPI_OWNERSHIP_BIT;
1768
1769 /* write back to hardware memory */
1770 BD_CACHE_WRITEBACK_INVALIDATE((u32) curr_bd,
1771 EMAC_BD_LENGTH_FOR_CACHE);
1772 curr_bd->next = rxch->active_queue_head;
1773 rxch->active_queue_head = curr_bd;
1774 }
1775
1776 /* At this point rxCppi->activeQueueHead points to the first
1777 RX BD ready to be given to RX HDP and rxch->active_queue_tail
1778 points to the last RX BD
1779 */
1780 return 0;
1781}
1782
1783/**
1784 * emac_rxch_teardown: RX channel teardown
1785 * @priv: The DaVinci EMAC private adapter structure
1786 * @ch: RX channel number
1787 *
1788 * Called during device stop to teardown RX channel
1789 *
1790 */
1791static void emac_rxch_teardown(struct emac_priv *priv, u32 ch)
1792{
1793 struct device *emac_dev = &priv->ndev->dev;
1794 u32 teardown_cnt = 0xFFFFFFF0; /* Some high value */
1795
1796 while ((emac_read(EMAC_RXCP(ch)) & EMAC_TEARDOWN_VALUE) !=
1797 EMAC_TEARDOWN_VALUE) {
1798 /* wait till tx teardown complete */
1799 cpu_relax(); /* TODO: check if this helps ... */
1800 --teardown_cnt;
1801 if (0 == teardown_cnt) {
1802 dev_err(emac_dev, "EMAC: RX teardown aborted\n");
1803 break;
1804 }
1805 }
1806 emac_write(EMAC_RXCP(ch), EMAC_TEARDOWN_VALUE);
1807}
1808
1809/**
1810 * emac_stop_rxch: Stop RX channel operation
1811 * @priv: The DaVinci EMAC private adapter structure
1812 * @ch: RX channel number
1813 *
1814 * Called during device stop to stop RX channel operation
1815 *
1816 */
1817static void emac_stop_rxch(struct emac_priv *priv, u32 ch)
1818{
1819 struct emac_rxch *rxch = priv->rxch[ch];
1820
1821 if (rxch) {
1822 rxch->teardown_pending = 1;
1823 emac_write(EMAC_RXTEARDOWN, ch);
1824 /* wait for teardown complete */
1825 emac_rxch_teardown(priv, ch);
1826 rxch->teardown_pending = 0;
1827 emac_write(EMAC_RXINTMASKCLEAR, BIT(ch));
1828 }
1829}
1830
1831/**
1832 * emac_cleanup_rxch: Book-keep function to clean RX channel resources
1833 * @priv: The DaVinci EMAC private adapter structure
1834 * @ch: RX channel number
1835 *
1836 * Called during device stop to clean up RX channel resources
1837 *
1838 */
1839static void emac_cleanup_rxch(struct emac_priv *priv, u32 ch)
1840{
1841 struct emac_rxch *rxch = priv->rxch[ch];
1842 struct emac_rx_bd __iomem *curr_bd;
1843
1844 if (rxch) {
1845 /* free the receive buffers previously allocated */
1846 curr_bd = rxch->active_queue_head;
1847 while (curr_bd) {
1848 if (curr_bd->buf_token) {
Sekhar Noribe5bce22010-03-09 01:20:37 +00001849 dma_unmap_single(&priv->ndev->dev,
1850 curr_bd->buff_ptr,
1851 curr_bd->off_b_len
1852 & EMAC_RX_BD_BUF_SIZE,
1853 DMA_FROM_DEVICE);
1854
Anant Golea6286ee2009-05-18 15:19:01 -07001855 dev_kfree_skb_any((struct sk_buff *)\
1856 curr_bd->buf_token);
1857 }
1858 curr_bd = curr_bd->next;
1859 }
1860 if (rxch->bd_mem)
1861 rxch->bd_mem = NULL;
1862 kfree(rxch);
1863 priv->rxch[ch] = NULL;
1864 }
1865}
1866
1867/**
1868 * emac_set_type0addr: Set EMAC Type0 mac address
1869 * @priv: The DaVinci EMAC private adapter structure
1870 * @ch: RX channel number
1871 * @mac_addr: MAC address to set in device
1872 *
1873 * Called internally to set Type0 mac address of the adapter (Device)
1874 *
1875 * Returns success (0) or appropriate error code (none as of now)
1876 */
1877static void emac_set_type0addr(struct emac_priv *priv, u32 ch, char *mac_addr)
1878{
1879 u32 val;
1880 val = ((mac_addr[5] << 8) | (mac_addr[4]));
1881 emac_write(EMAC_MACSRCADDRLO, val);
1882
1883 val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1884 (mac_addr[1] << 8) | (mac_addr[0]));
1885 emac_write(EMAC_MACSRCADDRHI, val);
1886 val = emac_read(EMAC_RXUNICASTSET);
1887 val |= BIT(ch);
1888 emac_write(EMAC_RXUNICASTSET, val);
1889 val = emac_read(EMAC_RXUNICASTCLEAR);
1890 val &= ~BIT(ch);
1891 emac_write(EMAC_RXUNICASTCLEAR, val);
1892}
1893
1894/**
1895 * emac_set_type1addr: Set EMAC Type1 mac address
1896 * @priv: The DaVinci EMAC private adapter structure
1897 * @ch: RX channel number
1898 * @mac_addr: MAC address to set in device
1899 *
1900 * Called internally to set Type1 mac address of the adapter (Device)
1901 *
1902 * Returns success (0) or appropriate error code (none as of now)
1903 */
1904static void emac_set_type1addr(struct emac_priv *priv, u32 ch, char *mac_addr)
1905{
1906 u32 val;
1907 emac_write(EMAC_MACINDEX, ch);
1908 val = ((mac_addr[5] << 8) | mac_addr[4]);
1909 emac_write(EMAC_MACADDRLO, val);
1910 val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1911 (mac_addr[1] << 8) | (mac_addr[0]));
1912 emac_write(EMAC_MACADDRHI, val);
1913 emac_set_type0addr(priv, ch, mac_addr);
1914}
1915
1916/**
1917 * emac_set_type2addr: Set EMAC Type2 mac address
1918 * @priv: The DaVinci EMAC private adapter structure
1919 * @ch: RX channel number
1920 * @mac_addr: MAC address to set in device
1921 * @index: index into RX address entries
1922 * @match: match parameter for RX address matching logic
1923 *
1924 * Called internally to set Type2 mac address of the adapter (Device)
1925 *
1926 * Returns success (0) or appropriate error code (none as of now)
1927 */
1928static void emac_set_type2addr(struct emac_priv *priv, u32 ch,
1929 char *mac_addr, int index, int match)
1930{
1931 u32 val;
1932 emac_write(EMAC_MACINDEX, index);
1933 val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1934 (mac_addr[1] << 8) | (mac_addr[0]));
1935 emac_write(EMAC_MACADDRHI, val);
1936 val = ((mac_addr[5] << 8) | mac_addr[4] | ((ch & 0x7) << 16) | \
1937 (match << 19) | BIT(20));
1938 emac_write(EMAC_MACADDRLO, val);
1939 emac_set_type0addr(priv, ch, mac_addr);
1940}
1941
1942/**
1943 * emac_setmac: Set mac address in the adapter (internal function)
1944 * @priv: The DaVinci EMAC private adapter structure
1945 * @ch: RX channel number
1946 * @mac_addr: MAC address to set in device
1947 *
1948 * Called internally to set the mac address of the adapter (Device)
1949 *
1950 * Returns success (0) or appropriate error code (none as of now)
1951 */
1952static void emac_setmac(struct emac_priv *priv, u32 ch, char *mac_addr)
1953{
1954 struct device *emac_dev = &priv->ndev->dev;
1955
1956 if (priv->rx_addr_type == 0) {
1957 emac_set_type0addr(priv, ch, mac_addr);
1958 } else if (priv->rx_addr_type == 1) {
1959 u32 cnt;
1960 for (cnt = 0; cnt < EMAC_MAX_TXRX_CHANNELS; cnt++)
1961 emac_set_type1addr(priv, ch, mac_addr);
1962 } else if (priv->rx_addr_type == 2) {
1963 emac_set_type2addr(priv, ch, mac_addr, ch, 1);
1964 emac_set_type0addr(priv, ch, mac_addr);
1965 } else {
1966 if (netif_msg_drv(priv))
1967 dev_err(emac_dev, "DaVinci EMAC: Wrong addressing\n");
1968 }
1969}
1970
1971/**
1972 * emac_dev_setmac_addr: Set mac address in the adapter
1973 * @ndev: The DaVinci EMAC network adapter
1974 * @addr: MAC address to set in device
1975 *
1976 * Called by the system to set the mac address of the adapter (Device)
1977 *
1978 * Returns success (0) or appropriate error code (none as of now)
1979 */
1980static int emac_dev_setmac_addr(struct net_device *ndev, void *addr)
1981{
1982 struct emac_priv *priv = netdev_priv(ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07001983 struct device *emac_dev = &priv->ndev->dev;
1984 struct sockaddr *sa = addr;
Anant Golea6286ee2009-05-18 15:19:01 -07001985
Pablo Bitton64c81652009-07-07 19:11:10 -07001986 if (!is_valid_ether_addr(sa->sa_data))
1987 return -EINVAL;
1988
Anant Golea6286ee2009-05-18 15:19:01 -07001989 /* Store mac addr in priv and rx channel and set it in EMAC hw */
1990 memcpy(priv->mac_addr, sa->sa_data, ndev->addr_len);
Anant Golea6286ee2009-05-18 15:19:01 -07001991 memcpy(ndev->dev_addr, sa->sa_data, ndev->addr_len);
Pablo Bitton64c81652009-07-07 19:11:10 -07001992
Pablo Bitton64c81652009-07-07 19:11:10 -07001993 /* MAC address is configured only after the interface is enabled. */
1994 if (netif_running(ndev)) {
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001995 memcpy(priv->mac_addr, sa->sa_data, ndev->addr_len);
1996 emac_setmac(priv, EMAC_DEF_RX_CH, priv->mac_addr);
Pablo Bitton64c81652009-07-07 19:11:10 -07001997 }
Anant Golea6286ee2009-05-18 15:19:01 -07001998
1999 if (netif_msg_drv(priv))
Chaithrika U S5c726162009-06-03 21:54:29 -07002000 dev_notice(emac_dev, "DaVinci EMAC: emac_dev_setmac_addr %pM\n",
2001 priv->mac_addr);
Anant Golea6286ee2009-05-18 15:19:01 -07002002
2003 return 0;
2004}
2005
2006/**
2007 * emac_addbd_to_rx_queue: Recycle RX buffer descriptor
2008 * @priv: The DaVinci EMAC private adapter structure
2009 * @ch: RX channel number to process buffer descriptors for
2010 * @curr_bd: current buffer descriptor
2011 * @buffer: buffer pointer for descriptor
2012 * @buf_token: buffer token (stores skb information)
2013 *
2014 * Prepares the recycled buffer descriptor and addes it to hardware
2015 * receive queue - if queue empty this descriptor becomes the head
2016 * else addes the descriptor to end of queue
2017 *
2018 */
2019static void emac_addbd_to_rx_queue(struct emac_priv *priv, u32 ch,
2020 struct emac_rx_bd __iomem *curr_bd,
2021 char *buffer, void *buf_token)
2022{
2023 struct emac_rxch *rxch = priv->rxch[ch];
2024
2025 /* populate the hardware descriptor */
2026 curr_bd->h_next = 0;
Sekhar Noribe5bce22010-03-09 01:20:37 +00002027 curr_bd->buff_ptr = dma_map_single(&priv->ndev->dev, buffer,
2028 rxch->buf_size, DMA_FROM_DEVICE);
Anant Golea6286ee2009-05-18 15:19:01 -07002029 curr_bd->off_b_len = rxch->buf_size;
2030 curr_bd->mode = EMAC_CPPI_OWNERSHIP_BIT;
2031 curr_bd->next = NULL;
2032 curr_bd->data_ptr = buffer;
2033 curr_bd->buf_token = buf_token;
2034
2035 /* write back */
2036 BD_CACHE_WRITEBACK_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
2037 if (rxch->active_queue_head == NULL) {
2038 rxch->active_queue_head = curr_bd;
2039 rxch->active_queue_tail = curr_bd;
2040 if (0 != rxch->queue_active) {
2041 emac_write(EMAC_RXHDP(ch),
Sriramakrishnanad021ae2009-11-19 15:58:27 +05302042 emac_virt_to_phys(rxch->active_queue_head, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07002043 rxch->queue_active = 1;
2044 }
2045 } else {
2046 struct emac_rx_bd __iomem *tail_bd;
2047 u32 frame_status;
2048
2049 tail_bd = rxch->active_queue_tail;
2050 rxch->active_queue_tail = curr_bd;
2051 tail_bd->next = curr_bd;
2052 tail_bd = EMAC_VIRT_NOCACHE(tail_bd);
Sriramakrishnanad021ae2009-11-19 15:58:27 +05302053 tail_bd->h_next = emac_virt_to_phys(curr_bd, priv);
Anant Golea6286ee2009-05-18 15:19:01 -07002054 frame_status = tail_bd->mode;
2055 if (frame_status & EMAC_CPPI_EOQ_BIT) {
2056 emac_write(EMAC_RXHDP(ch),
Sriramakrishnanad021ae2009-11-19 15:58:27 +05302057 emac_virt_to_phys(curr_bd, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07002058 frame_status &= ~(EMAC_CPPI_EOQ_BIT);
2059 tail_bd->mode = frame_status;
2060 ++rxch->end_of_queue_add;
2061 }
2062 }
2063 ++rxch->recycled_bd;
2064}
2065
2066/**
2067 * emac_net_rx_cb: Prepares packet and sends to upper layer
2068 * @priv: The DaVinci EMAC private adapter structure
2069 * @net_pkt_list: Network packet list (received packets)
2070 *
2071 * Invalidates packet buffer memory and sends the received packet to upper
2072 * layer
2073 *
2074 * Returns success or appropriate error code (none as of now)
2075 */
2076static int emac_net_rx_cb(struct emac_priv *priv,
2077 struct emac_netpktobj *net_pkt_list)
2078{
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002079 struct net_device *ndev = priv->ndev;
2080 struct sk_buff *p_skb = net_pkt_list->pkt_token;
Anant Golea6286ee2009-05-18 15:19:01 -07002081 /* set length of packet */
2082 skb_put(p_skb, net_pkt_list->pkt_length);
Anant Golea6286ee2009-05-18 15:19:01 -07002083 p_skb->protocol = eth_type_trans(p_skb, priv->ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07002084 netif_receive_skb(p_skb);
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002085 ndev->stats.rx_bytes += net_pkt_list->pkt_length;
2086 ndev->stats.rx_packets++;
Anant Golea6286ee2009-05-18 15:19:01 -07002087 return 0;
2088}
2089
2090/**
2091 * emac_rx_bdproc: RX buffer descriptor (packet) processing
2092 * @priv: The DaVinci EMAC private adapter structure
2093 * @ch: RX channel number to process buffer descriptors for
2094 * @budget: number of packets allowed to process
2095 * @pending: indication to caller that packets are pending to process
2096 *
2097 * Processes RX buffer descriptors - checks ownership bit on the RX buffer
2098 * descriptor, sends the receive packet to upper layer, allocates a new SKB
2099 * and recycles the buffer descriptor (requeues it in hardware RX queue).
2100 * Only "budget" number of packets are processed and indication of pending
2101 * packets provided to the caller.
2102 *
2103 * Returns number of packets processed (and indication of pending packets)
2104 */
2105static int emac_rx_bdproc(struct emac_priv *priv, u32 ch, u32 budget)
2106{
2107 unsigned long flags;
2108 u32 frame_status;
2109 u32 pkts_processed = 0;
2110 char *new_buffer;
2111 struct emac_rx_bd __iomem *curr_bd;
2112 struct emac_rx_bd __iomem *last_bd;
2113 struct emac_netpktobj *curr_pkt, pkt_obj;
2114 struct emac_netbufobj buf_obj;
2115 struct emac_netbufobj *rx_buf_obj;
2116 void *new_buf_token;
2117 struct emac_rxch *rxch = priv->rxch[ch];
2118
2119 if (unlikely(1 == rxch->teardown_pending))
2120 return 0;
2121 ++rxch->proc_count;
2122 spin_lock_irqsave(&priv->rx_lock, flags);
2123 pkt_obj.buf_list = &buf_obj;
2124 curr_pkt = &pkt_obj;
2125 curr_bd = rxch->active_queue_head;
2126 BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
2127 frame_status = curr_bd->mode;
2128
2129 while ((curr_bd) &&
2130 ((frame_status & EMAC_CPPI_OWNERSHIP_BIT) == 0) &&
2131 (pkts_processed < budget)) {
2132
2133 new_buffer = emac_net_alloc_rx_buf(priv, rxch->buf_size,
2134 &new_buf_token, EMAC_DEF_RX_CH);
2135 if (unlikely(NULL == new_buffer)) {
2136 ++rxch->out_of_rx_buffers;
2137 goto end_emac_rx_bdproc;
2138 }
2139
2140 /* populate received packet data structure */
2141 rx_buf_obj = &curr_pkt->buf_list[0];
2142 rx_buf_obj->data_ptr = (char *)curr_bd->data_ptr;
2143 rx_buf_obj->length = curr_bd->off_b_len & EMAC_RX_BD_BUF_SIZE;
2144 rx_buf_obj->buf_token = curr_bd->buf_token;
Sekhar Noribe5bce22010-03-09 01:20:37 +00002145
2146 dma_unmap_single(&priv->ndev->dev, curr_bd->buff_ptr,
2147 curr_bd->off_b_len & EMAC_RX_BD_BUF_SIZE,
2148 DMA_FROM_DEVICE);
2149
Anant Golea6286ee2009-05-18 15:19:01 -07002150 curr_pkt->pkt_token = curr_pkt->buf_list->buf_token;
2151 curr_pkt->num_bufs = 1;
2152 curr_pkt->pkt_length =
2153 (frame_status & EMAC_RX_BD_PKT_LENGTH_MASK);
Sriramakrishnanad021ae2009-11-19 15:58:27 +05302154 emac_write(EMAC_RXCP(ch), emac_virt_to_phys(curr_bd, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07002155 ++rxch->processed_bd;
2156 last_bd = curr_bd;
2157 curr_bd = last_bd->next;
2158 rxch->active_queue_head = curr_bd;
2159
2160 /* check if end of RX queue ? */
2161 if (frame_status & EMAC_CPPI_EOQ_BIT) {
2162 if (curr_bd) {
2163 ++rxch->mis_queued_packets;
2164 emac_write(EMAC_RXHDP(ch),
Sriramakrishnanad021ae2009-11-19 15:58:27 +05302165 emac_virt_to_phys(curr_bd, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07002166 } else {
2167 ++rxch->end_of_queue;
2168 rxch->queue_active = 0;
2169 }
2170 }
2171
2172 /* recycle BD */
2173 emac_addbd_to_rx_queue(priv, ch, last_bd, new_buffer,
2174 new_buf_token);
2175
2176 /* return the packet to the user - BD ptr passed in
2177 * last parameter for potential *future* use */
2178 spin_unlock_irqrestore(&priv->rx_lock, flags);
2179 emac_net_rx_cb(priv, curr_pkt);
2180 spin_lock_irqsave(&priv->rx_lock, flags);
2181 curr_bd = rxch->active_queue_head;
2182 if (curr_bd) {
2183 BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
2184 frame_status = curr_bd->mode;
2185 }
2186 ++pkts_processed;
2187 }
2188
2189end_emac_rx_bdproc:
2190 spin_unlock_irqrestore(&priv->rx_lock, flags);
2191 return pkts_processed;
2192}
2193
2194/**
2195 * emac_hw_enable: Enable EMAC hardware for packet transmission/reception
2196 * @priv: The DaVinci EMAC private adapter structure
2197 *
2198 * Enables EMAC hardware for packet processing - enables PHY, enables RX
2199 * for packet reception and enables device interrupts and then NAPI
2200 *
2201 * Returns success (0) or appropriate error code (none right now)
2202 */
2203static int emac_hw_enable(struct emac_priv *priv)
2204{
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002205 u32 val, mbp_enable, mac_control;
Anant Golea6286ee2009-05-18 15:19:01 -07002206
2207 /* Soft reset */
2208 emac_write(EMAC_SOFTRESET, 1);
2209 while (emac_read(EMAC_SOFTRESET))
2210 cpu_relax();
2211
2212 /* Disable interrupt & Set pacing for more interrupts initially */
2213 emac_int_disable(priv);
2214
2215 /* Full duplex enable bit set when auto negotiation happens */
2216 mac_control =
2217 (((EMAC_DEF_TXPRIO_FIXED) ? (EMAC_MACCONTROL_TXPTYPE) : 0x0) |
2218 ((priv->speed == 1000) ? EMAC_MACCONTROL_GIGABITEN : 0x0) |
2219 ((EMAC_DEF_TXPACING_EN) ? (EMAC_MACCONTROL_TXPACEEN) : 0x0) |
2220 ((priv->duplex == DUPLEX_FULL) ? 0x1 : 0));
2221 emac_write(EMAC_MACCONTROL, mac_control);
2222
2223 mbp_enable =
2224 (((EMAC_DEF_PASS_CRC) ? (EMAC_RXMBP_PASSCRC_MASK) : 0x0) |
2225 ((EMAC_DEF_QOS_EN) ? (EMAC_RXMBP_QOSEN_MASK) : 0x0) |
2226 ((EMAC_DEF_NO_BUFF_CHAIN) ? (EMAC_RXMBP_NOCHAIN_MASK) : 0x0) |
2227 ((EMAC_DEF_MACCTRL_FRAME_EN) ? (EMAC_RXMBP_CMFEN_MASK) : 0x0) |
2228 ((EMAC_DEF_SHORT_FRAME_EN) ? (EMAC_RXMBP_CSFEN_MASK) : 0x0) |
2229 ((EMAC_DEF_ERROR_FRAME_EN) ? (EMAC_RXMBP_CEFEN_MASK) : 0x0) |
2230 ((EMAC_DEF_PROM_EN) ? (EMAC_RXMBP_CAFEN_MASK) : 0x0) |
2231 ((EMAC_DEF_PROM_CH & EMAC_RXMBP_CHMASK) << \
2232 EMAC_RXMBP_PROMCH_SHIFT) |
2233 ((EMAC_DEF_BCAST_EN) ? (EMAC_RXMBP_BROADEN_MASK) : 0x0) |
2234 ((EMAC_DEF_BCAST_CH & EMAC_RXMBP_CHMASK) << \
2235 EMAC_RXMBP_BROADCH_SHIFT) |
2236 ((EMAC_DEF_MCAST_EN) ? (EMAC_RXMBP_MULTIEN_MASK) : 0x0) |
2237 ((EMAC_DEF_MCAST_CH & EMAC_RXMBP_CHMASK) << \
2238 EMAC_RXMBP_MULTICH_SHIFT));
2239 emac_write(EMAC_RXMBPENABLE, mbp_enable);
2240 emac_write(EMAC_RXMAXLEN, (EMAC_DEF_MAX_FRAME_SIZE &
2241 EMAC_RX_MAX_LEN_MASK));
2242 emac_write(EMAC_RXBUFFEROFFSET, (EMAC_DEF_BUFFER_OFFSET &
2243 EMAC_RX_BUFFER_OFFSET_MASK));
2244 emac_write(EMAC_RXFILTERLOWTHRESH, 0);
2245 emac_write(EMAC_RXUNICASTCLEAR, EMAC_RX_UNICAST_CLEAR_ALL);
2246 priv->rx_addr_type = (emac_read(EMAC_MACCONFIG) >> 8) & 0xFF;
2247
Anant Golea6286ee2009-05-18 15:19:01 -07002248 emac_write(EMAC_MACINTMASKSET, EMAC_MAC_HOST_ERR_INTMASK_VAL);
2249
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002250 emac_setmac(priv, EMAC_DEF_RX_CH, priv->mac_addr);
Anant Golea6286ee2009-05-18 15:19:01 -07002251
2252 /* Enable MII */
2253 val = emac_read(EMAC_MACCONTROL);
chaithrika@ti.com69ef9692009-10-01 10:25:19 +00002254 val |= (EMAC_MACCONTROL_GMIIEN);
Anant Golea6286ee2009-05-18 15:19:01 -07002255 emac_write(EMAC_MACCONTROL, val);
2256
2257 /* Enable NAPI and interrupts */
2258 napi_enable(&priv->napi);
2259 emac_int_enable(priv);
2260 return 0;
2261
2262}
2263
2264/**
2265 * emac_poll: EMAC NAPI Poll function
2266 * @ndev: The DaVinci EMAC network adapter
2267 * @budget: Number of receive packets to process (as told by NAPI layer)
2268 *
2269 * NAPI Poll function implemented to process packets as per budget. We check
2270 * the type of interrupt on the device and accordingly call the TX or RX
2271 * packet processing functions. We follow the budget for RX processing and
2272 * also put a cap on number of TX pkts processed through config param. The
2273 * NAPI schedule function is called if more packets pending.
2274 *
2275 * Returns number of packets received (in most cases; else TX pkts - rarely)
2276 */
2277static int emac_poll(struct napi_struct *napi, int budget)
2278{
2279 unsigned int mask;
2280 struct emac_priv *priv = container_of(napi, struct emac_priv, napi);
2281 struct net_device *ndev = priv->ndev;
2282 struct device *emac_dev = &ndev->dev;
2283 u32 status = 0;
Sriram3725b1f2010-07-29 02:33:59 +00002284 u32 num_tx_pkts = 0, num_rx_pkts = 0;
Anant Golea6286ee2009-05-18 15:19:01 -07002285
Anant Golea6286ee2009-05-18 15:19:01 -07002286 /* Check interrupt vectors and call packet processing */
2287 status = emac_read(EMAC_MACINVECTOR);
2288
2289 mask = EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC;
2290
2291 if (priv->version == EMAC_VERSION_2)
2292 mask = EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC;
2293
2294 if (status & mask) {
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002295 num_tx_pkts = cpdma_chan_process(priv->txchan,
2296 EMAC_DEF_TX_MAX_SERVICE);
Anant Golea6286ee2009-05-18 15:19:01 -07002297 } /* TX processing */
2298
Anant Golea6286ee2009-05-18 15:19:01 -07002299 mask = EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC;
2300
2301 if (priv->version == EMAC_VERSION_2)
2302 mask = EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC;
2303
2304 if (status & mask) {
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002305 num_rx_pkts = cpdma_chan_process(priv->rxchan, budget);
Anant Golea6286ee2009-05-18 15:19:01 -07002306 } /* RX processing */
2307
Sriram43c2ed82009-09-24 19:15:18 +00002308 mask = EMAC_DM644X_MAC_IN_VECTOR_HOST_INT;
2309 if (priv->version == EMAC_VERSION_2)
2310 mask = EMAC_DM646X_MAC_IN_VECTOR_HOST_INT;
2311
2312 if (unlikely(status & mask)) {
Anant Golea6286ee2009-05-18 15:19:01 -07002313 u32 ch, cause;
2314 dev_err(emac_dev, "DaVinci EMAC: Fatal Hardware Error\n");
2315 netif_stop_queue(ndev);
2316 napi_disable(&priv->napi);
2317
2318 status = emac_read(EMAC_MACSTATUS);
2319 cause = ((status & EMAC_MACSTATUS_TXERRCODE_MASK) >>
2320 EMAC_MACSTATUS_TXERRCODE_SHIFT);
2321 if (cause) {
2322 ch = ((status & EMAC_MACSTATUS_TXERRCH_MASK) >>
2323 EMAC_MACSTATUS_TXERRCH_SHIFT);
2324 if (net_ratelimit()) {
2325 dev_err(emac_dev, "TX Host error %s on ch=%d\n",
2326 &emac_txhost_errcodes[cause][0], ch);
2327 }
2328 }
2329 cause = ((status & EMAC_MACSTATUS_RXERRCODE_MASK) >>
2330 EMAC_MACSTATUS_RXERRCODE_SHIFT);
2331 if (cause) {
2332 ch = ((status & EMAC_MACSTATUS_RXERRCH_MASK) >>
2333 EMAC_MACSTATUS_RXERRCH_SHIFT);
2334 if (netif_msg_hw(priv) && net_ratelimit())
2335 dev_err(emac_dev, "RX Host error %s on ch=%d\n",
2336 &emac_rxhost_errcodes[cause][0], ch);
2337 }
Sriram3725b1f2010-07-29 02:33:59 +00002338 } else if (num_rx_pkts < budget) {
2339 napi_complete(napi);
2340 emac_int_enable(priv);
2341 }
Anant Golea6286ee2009-05-18 15:19:01 -07002342
Sriram3725b1f2010-07-29 02:33:59 +00002343 return num_rx_pkts;
Anant Golea6286ee2009-05-18 15:19:01 -07002344}
2345
2346#ifdef CONFIG_NET_POLL_CONTROLLER
2347/**
2348 * emac_poll_controller: EMAC Poll controller function
2349 * @ndev: The DaVinci EMAC network adapter
2350 *
2351 * Polled functionality used by netconsole and others in non interrupt mode
2352 *
2353 */
2354void emac_poll_controller(struct net_device *ndev)
2355{
2356 struct emac_priv *priv = netdev_priv(ndev);
2357
2358 emac_int_disable(priv);
Tonyliuc8ee5532009-11-04 05:45:02 -08002359 emac_irq(ndev->irq, ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07002360 emac_int_enable(priv);
2361}
2362#endif
2363
Anant Golea6286ee2009-05-18 15:19:01 -07002364static void emac_adjust_link(struct net_device *ndev)
2365{
2366 struct emac_priv *priv = netdev_priv(ndev);
2367 struct phy_device *phydev = priv->phydev;
2368 unsigned long flags;
2369 int new_state = 0;
2370
2371 spin_lock_irqsave(&priv->lock, flags);
2372
2373 if (phydev->link) {
2374 /* check the mode of operation - full/half duplex */
2375 if (phydev->duplex != priv->duplex) {
2376 new_state = 1;
2377 priv->duplex = phydev->duplex;
2378 }
2379 if (phydev->speed != priv->speed) {
2380 new_state = 1;
2381 priv->speed = phydev->speed;
2382 }
2383 if (!priv->link) {
2384 new_state = 1;
2385 priv->link = 1;
2386 }
2387
2388 } else if (priv->link) {
2389 new_state = 1;
2390 priv->link = 0;
2391 priv->speed = 0;
2392 priv->duplex = ~0;
2393 }
2394 if (new_state) {
2395 emac_update_phystatus(priv);
2396 phy_print_status(priv->phydev);
2397 }
2398
2399 spin_unlock_irqrestore(&priv->lock, flags);
2400}
2401
2402/*************************************************************************
2403 * Linux Driver Model
2404 *************************************************************************/
2405
2406/**
2407 * emac_devioctl: EMAC adapter ioctl
2408 * @ndev: The DaVinci EMAC network adapter
2409 * @ifrq: request parameter
2410 * @cmd: command parameter
2411 *
2412 * EMAC driver ioctl function
2413 *
2414 * Returns success(0) or appropriate error code
2415 */
2416static int emac_devioctl(struct net_device *ndev, struct ifreq *ifrq, int cmd)
2417{
2418 dev_warn(&ndev->dev, "DaVinci EMAC: ioctl not supported\n");
2419
2420 if (!(netif_running(ndev)))
2421 return -EINVAL;
2422
2423 /* TODO: Add phy read and write and private statistics get feature */
2424
2425 return -EOPNOTSUPP;
2426}
2427
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002428static int match_first_device(struct device *dev, void *data)
2429{
2430 return 1;
2431}
2432
Anant Golea6286ee2009-05-18 15:19:01 -07002433/**
2434 * emac_dev_open: EMAC device open
2435 * @ndev: The DaVinci EMAC network adapter
2436 *
2437 * Called when system wants to start the interface. We init TX/RX channels
2438 * and enable the hardware for packet reception/transmission and start the
2439 * network queue.
2440 *
2441 * Returns 0 for a successful open, or appropriate error code
2442 */
2443static int emac_dev_open(struct net_device *ndev)
2444{
2445 struct device *emac_dev = &ndev->dev;
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002446 u32 cnt;
Anant Golea6286ee2009-05-18 15:19:01 -07002447 struct resource *res;
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002448 int q, m, ret;
Anant Golea6286ee2009-05-18 15:19:01 -07002449 int i = 0;
2450 int k = 0;
2451 struct emac_priv *priv = netdev_priv(ndev);
2452
2453 netif_carrier_off(ndev);
Dan Carpenter4d27b872010-03-02 21:07:24 +00002454 for (cnt = 0; cnt < ETH_ALEN; cnt++)
Anant Golea6286ee2009-05-18 15:19:01 -07002455 ndev->dev_addr[cnt] = priv->mac_addr[cnt];
2456
2457 /* Configuration items */
2458 priv->rx_buf_size = EMAC_DEF_MAX_FRAME_SIZE + NET_IP_ALIGN;
2459
Anant Golea6286ee2009-05-18 15:19:01 -07002460 priv->mac_hash1 = 0;
2461 priv->mac_hash2 = 0;
2462 emac_write(EMAC_MACHASH1, 0);
2463 emac_write(EMAC_MACHASH2, 0);
2464
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002465 for (i = 0; i < EMAC_DEF_RX_NUM_DESC; i++) {
2466 struct sk_buff *skb = emac_rx_alloc(priv);
2467
2468 if (!skb)
2469 break;
2470
2471 ret = cpdma_chan_submit(priv->rxchan, skb, skb->data,
2472 skb_tailroom(skb), GFP_KERNEL);
2473 if (WARN_ON(ret < 0))
2474 break;
Anant Golea6286ee2009-05-18 15:19:01 -07002475 }
2476
2477 /* Request IRQ */
2478
2479 while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
2480 for (i = res->start; i <= res->end; i++) {
2481 if (request_irq(i, emac_irq, IRQF_DISABLED,
2482 ndev->name, ndev))
2483 goto rollback;
2484 }
2485 k++;
2486 }
2487
2488 /* Start/Enable EMAC hardware */
2489 emac_hw_enable(priv);
2490
Sriram84da2652010-07-29 02:33:58 +00002491 /* Enable Interrupt pacing if configured */
2492 if (priv->coal_intvl != 0) {
2493 struct ethtool_coalesce coal;
2494
2495 coal.rx_coalesce_usecs = (priv->coal_intvl << 4);
2496 emac_set_coalesce(ndev, &coal);
2497 }
2498
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002499 cpdma_ctlr_start(priv->dma);
2500
Anant Golea6286ee2009-05-18 15:19:01 -07002501 priv->phydev = NULL;
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002502 /* use the first phy on the bus if pdata did not give us a phy id */
2503 if (!priv->phy_id) {
2504 struct device *phy;
Anant Golea6286ee2009-05-18 15:19:01 -07002505
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002506 phy = bus_find_device(&mdio_bus_type, NULL, NULL,
2507 match_first_device);
2508 if (phy)
2509 priv->phy_id = dev_name(phy);
2510 }
Anant Golea6286ee2009-05-18 15:19:01 -07002511
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002512 if (priv->phy_id && *priv->phy_id) {
2513 priv->phydev = phy_connect(ndev, priv->phy_id,
2514 &emac_adjust_link, 0,
2515 PHY_INTERFACE_MODE_MII);
Anant Golea6286ee2009-05-18 15:19:01 -07002516
2517 if (IS_ERR(priv->phydev)) {
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002518 dev_err(emac_dev, "could not connect to phy %s\n",
2519 priv->phy_id);
2520 priv->phydev = NULL;
Anant Golea6286ee2009-05-18 15:19:01 -07002521 return PTR_ERR(priv->phydev);
2522 }
2523
2524 priv->link = 0;
2525 priv->speed = 0;
2526 priv->duplex = ~0;
2527
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002528 dev_info(emac_dev, "attached PHY driver [%s] "
2529 "(mii_bus:phy_addr=%s, id=%x)\n",
Anant Golea6286ee2009-05-18 15:19:01 -07002530 priv->phydev->drv->name, dev_name(&priv->phydev->dev),
2531 priv->phydev->phy_id);
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002532 } else {
Anant Golea6286ee2009-05-18 15:19:01 -07002533 /* No PHY , fix the link, speed and duplex settings */
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002534 dev_notice(emac_dev, "no phy, defaulting to 100/full\n");
Anant Golea6286ee2009-05-18 15:19:01 -07002535 priv->link = 1;
2536 priv->speed = SPEED_100;
2537 priv->duplex = DUPLEX_FULL;
2538 emac_update_phystatus(priv);
2539 }
2540
2541 if (!netif_running(ndev)) /* debug only - to avoid compiler warning */
2542 emac_dump_regs(priv);
2543
2544 if (netif_msg_drv(priv))
2545 dev_notice(emac_dev, "DaVinci EMAC: Opened %s\n", ndev->name);
2546
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002547 if (priv->phydev)
Anant Golea6286ee2009-05-18 15:19:01 -07002548 phy_start(priv->phydev);
2549
2550 return 0;
2551
2552rollback:
2553
2554 dev_err(emac_dev, "DaVinci EMAC: request_irq() failed");
2555
2556 for (q = k; k >= 0; k--) {
2557 for (m = i; m >= res->start; m--)
2558 free_irq(m, ndev);
2559 res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k-1);
2560 m = res->end;
2561 }
2562 return -EBUSY;
2563}
2564
2565/**
2566 * emac_dev_stop: EMAC device stop
2567 * @ndev: The DaVinci EMAC network adapter
2568 *
2569 * Called when system wants to stop or down the interface. We stop the network
2570 * queue, disable interrupts and cleanup TX/RX channels.
2571 *
2572 * We return the statistics in net_device_stats structure pulled from emac
2573 */
2574static int emac_dev_stop(struct net_device *ndev)
2575{
2576 struct resource *res;
2577 int i = 0;
2578 int irq_num;
2579 struct emac_priv *priv = netdev_priv(ndev);
2580 struct device *emac_dev = &ndev->dev;
2581
2582 /* inform the upper layers. */
2583 netif_stop_queue(ndev);
2584 napi_disable(&priv->napi);
2585
2586 netif_carrier_off(ndev);
2587 emac_int_disable(priv);
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002588 cpdma_ctlr_stop(priv->dma);
Anant Golea6286ee2009-05-18 15:19:01 -07002589 emac_write(EMAC_SOFTRESET, 1);
2590
2591 if (priv->phydev)
2592 phy_disconnect(priv->phydev);
2593
2594 /* Free IRQ */
2595 while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, i))) {
2596 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2597 free_irq(irq_num, priv->ndev);
2598 i++;
2599 }
2600
2601 if (netif_msg_drv(priv))
2602 dev_notice(emac_dev, "DaVinci EMAC: %s stopped\n", ndev->name);
2603
2604 return 0;
2605}
2606
2607/**
2608 * emac_dev_getnetstats: EMAC get statistics function
2609 * @ndev: The DaVinci EMAC network adapter
2610 *
2611 * Called when system wants to get statistics from the device.
2612 *
2613 * We return the statistics in net_device_stats structure pulled from emac
2614 */
2615static struct net_device_stats *emac_dev_getnetstats(struct net_device *ndev)
2616{
2617 struct emac_priv *priv = netdev_priv(ndev);
Sriram0fe74632009-10-07 02:44:30 +00002618 u32 mac_control;
2619 u32 stats_clear_mask;
Anant Golea6286ee2009-05-18 15:19:01 -07002620
2621 /* update emac hardware stats and reset the registers*/
2622
Sriram0fe74632009-10-07 02:44:30 +00002623 mac_control = emac_read(EMAC_MACCONTROL);
2624
2625 if (mac_control & EMAC_MACCONTROL_GMIIEN)
2626 stats_clear_mask = EMAC_STATS_CLR_MASK;
2627 else
2628 stats_clear_mask = 0;
2629
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002630 ndev->stats.multicast += emac_read(EMAC_RXMCASTFRAMES);
Sriram0fe74632009-10-07 02:44:30 +00002631 emac_write(EMAC_RXMCASTFRAMES, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002632
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002633 ndev->stats.collisions += (emac_read(EMAC_TXCOLLISION) +
Anant Golea6286ee2009-05-18 15:19:01 -07002634 emac_read(EMAC_TXSINGLECOLL) +
2635 emac_read(EMAC_TXMULTICOLL));
Sriram0fe74632009-10-07 02:44:30 +00002636 emac_write(EMAC_TXCOLLISION, stats_clear_mask);
2637 emac_write(EMAC_TXSINGLECOLL, stats_clear_mask);
2638 emac_write(EMAC_TXMULTICOLL, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002639
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002640 ndev->stats.rx_length_errors += (emac_read(EMAC_RXOVERSIZED) +
Anant Golea6286ee2009-05-18 15:19:01 -07002641 emac_read(EMAC_RXJABBER) +
2642 emac_read(EMAC_RXUNDERSIZED));
Sriram0fe74632009-10-07 02:44:30 +00002643 emac_write(EMAC_RXOVERSIZED, stats_clear_mask);
2644 emac_write(EMAC_RXJABBER, stats_clear_mask);
2645 emac_write(EMAC_RXUNDERSIZED, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002646
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002647 ndev->stats.rx_over_errors += (emac_read(EMAC_RXSOFOVERRUNS) +
Anant Golea6286ee2009-05-18 15:19:01 -07002648 emac_read(EMAC_RXMOFOVERRUNS));
Sriram0fe74632009-10-07 02:44:30 +00002649 emac_write(EMAC_RXSOFOVERRUNS, stats_clear_mask);
2650 emac_write(EMAC_RXMOFOVERRUNS, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002651
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002652 ndev->stats.rx_fifo_errors += emac_read(EMAC_RXDMAOVERRUNS);
Sriram0fe74632009-10-07 02:44:30 +00002653 emac_write(EMAC_RXDMAOVERRUNS, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002654
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002655 ndev->stats.tx_carrier_errors +=
Anant Golea6286ee2009-05-18 15:19:01 -07002656 emac_read(EMAC_TXCARRIERSENSE);
Sriram0fe74632009-10-07 02:44:30 +00002657 emac_write(EMAC_TXCARRIERSENSE, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002658
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002659 ndev->stats.tx_fifo_errors = emac_read(EMAC_TXUNDERRUN);
Sriram0fe74632009-10-07 02:44:30 +00002660 emac_write(EMAC_TXUNDERRUN, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002661
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002662 return &ndev->stats;
Anant Golea6286ee2009-05-18 15:19:01 -07002663}
2664
2665static const struct net_device_ops emac_netdev_ops = {
2666 .ndo_open = emac_dev_open,
2667 .ndo_stop = emac_dev_stop,
2668 .ndo_start_xmit = emac_dev_xmit,
2669 .ndo_set_multicast_list = emac_dev_mcast_set,
2670 .ndo_set_mac_address = emac_dev_setmac_addr,
2671 .ndo_do_ioctl = emac_devioctl,
2672 .ndo_tx_timeout = emac_dev_tx_timeout,
2673 .ndo_get_stats = emac_dev_getnetstats,
2674#ifdef CONFIG_NET_POLL_CONTROLLER
2675 .ndo_poll_controller = emac_poll_controller,
2676#endif
2677};
2678
2679/**
2680 * davinci_emac_probe: EMAC device probe
2681 * @pdev: The DaVinci EMAC device that we are removing
2682 *
2683 * Called when probing for emac devicesr. We get details of instances and
2684 * resource information from platform init and register a network device
2685 * and allocate resources necessary for driver to perform
2686 */
2687static int __devinit davinci_emac_probe(struct platform_device *pdev)
2688{
2689 int rc = 0;
2690 struct resource *res;
2691 struct net_device *ndev;
2692 struct emac_priv *priv;
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002693 unsigned long size, hw_ram_addr;
Anant Golea6286ee2009-05-18 15:19:01 -07002694 struct emac_platform_data *pdata;
2695 struct device *emac_dev;
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002696 struct cpdma_params dma_params;
Anant Golea6286ee2009-05-18 15:19:01 -07002697
2698 /* obtain emac clock from kernel */
2699 emac_clk = clk_get(&pdev->dev, NULL);
2700 if (IS_ERR(emac_clk)) {
2701 printk(KERN_ERR "DaVinci EMAC: Failed to get EMAC clock\n");
2702 return -EBUSY;
2703 }
2704 emac_bus_frequency = clk_get_rate(emac_clk);
2705 /* TODO: Probe PHY here if possible */
2706
2707 ndev = alloc_etherdev(sizeof(struct emac_priv));
2708 if (!ndev) {
2709 printk(KERN_ERR "DaVinci EMAC: Error allocating net_device\n");
2710 clk_put(emac_clk);
2711 return -ENOMEM;
2712 }
2713
2714 platform_set_drvdata(pdev, ndev);
2715 priv = netdev_priv(ndev);
2716 priv->pdev = pdev;
2717 priv->ndev = ndev;
2718 priv->msg_enable = netif_msg_init(debug_level, DAVINCI_EMAC_DEBUG);
2719
2720 spin_lock_init(&priv->tx_lock);
2721 spin_lock_init(&priv->rx_lock);
2722 spin_lock_init(&priv->lock);
2723
2724 pdata = pdev->dev.platform_data;
2725 if (!pdata) {
Stefan Weil0747e3b2010-01-07 00:44:08 +01002726 printk(KERN_ERR "DaVinci EMAC: No platform data\n");
Anant Golea6286ee2009-05-18 15:19:01 -07002727 return -ENODEV;
2728 }
2729
2730 /* MAC addr and PHY mask , RMII enable info from platform_data */
2731 memcpy(priv->mac_addr, pdata->mac_addr, 6);
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002732 priv->phy_id = pdata->phy_id;
Anant Golea6286ee2009-05-18 15:19:01 -07002733 priv->rmii_en = pdata->rmii_en;
2734 priv->version = pdata->version;
Sriramakrishnan01a9af32009-11-19 15:58:26 +05302735 priv->int_enable = pdata->interrupt_enable;
2736 priv->int_disable = pdata->interrupt_disable;
2737
Sriram84da2652010-07-29 02:33:58 +00002738 priv->coal_intvl = 0;
2739 priv->bus_freq_mhz = (u32)(emac_bus_frequency / 1000000);
2740
Anant Golea6286ee2009-05-18 15:19:01 -07002741 emac_dev = &ndev->dev;
2742 /* Get EMAC platform data */
2743 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2744 if (!res) {
2745 dev_err(emac_dev, "DaVinci EMAC: Error getting res\n");
2746 rc = -ENOENT;
2747 goto probe_quit;
2748 }
2749
2750 priv->emac_base_phys = res->start + pdata->ctrl_reg_offset;
2751 size = res->end - res->start + 1;
2752 if (!request_mem_region(res->start, size, ndev->name)) {
Joe Perches235ecb12010-02-01 21:22:11 +00002753 dev_err(emac_dev, "DaVinci EMAC: failed request_mem_region() for regs\n");
Anant Golea6286ee2009-05-18 15:19:01 -07002754 rc = -ENXIO;
2755 goto probe_quit;
2756 }
2757
2758 priv->remap_addr = ioremap(res->start, size);
2759 if (!priv->remap_addr) {
2760 dev_err(emac_dev, "Unable to map IO\n");
2761 rc = -ENOMEM;
2762 release_mem_region(res->start, size);
2763 goto probe_quit;
2764 }
2765 priv->emac_base = priv->remap_addr + pdata->ctrl_reg_offset;
2766 ndev->base_addr = (unsigned long)priv->remap_addr;
2767
2768 priv->ctrl_base = priv->remap_addr + pdata->ctrl_mod_reg_offset;
2769 priv->ctrl_ram_size = pdata->ctrl_ram_size;
2770 priv->emac_ctrl_ram = priv->remap_addr + pdata->ctrl_ram_offset;
2771
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002772 hw_ram_addr = pdata->hw_ram_addr;
2773 if (!hw_ram_addr)
2774 hw_ram_addr = (u32 __force)res->start + pdata->ctrl_ram_offset;
2775
2776 memset(&dma_params, 0, sizeof(dma_params));
2777 dma_params.dev = emac_dev;
2778 dma_params.dmaregs = priv->emac_base;
2779 dma_params.rxthresh = priv->emac_base + 0x120;
2780 dma_params.rxfree = priv->emac_base + 0x140;
2781 dma_params.txhdp = priv->emac_base + 0x600;
2782 dma_params.rxhdp = priv->emac_base + 0x620;
2783 dma_params.txcp = priv->emac_base + 0x640;
2784 dma_params.rxcp = priv->emac_base + 0x660;
2785 dma_params.num_chan = EMAC_MAX_TXRX_CHANNELS;
2786 dma_params.min_packet_size = EMAC_DEF_MIN_ETHPKTSIZE;
2787 dma_params.desc_mem_phys = hw_ram_addr;
2788 dma_params.desc_mem_size = pdata->ctrl_ram_size;
2789 dma_params.desc_align = 16;
2790
2791 priv->dma = cpdma_ctlr_create(&dma_params);
2792 if (!priv->dma) {
2793 dev_err(emac_dev, "DaVinci EMAC: Error initializing DMA\n");
2794 rc = -ENOMEM;
2795 goto no_dma;
2796 }
2797
2798 priv->txchan = cpdma_chan_create(priv->dma, tx_chan_num(EMAC_DEF_TX_CH),
2799 emac_tx_handler);
2800 priv->rxchan = cpdma_chan_create(priv->dma, rx_chan_num(EMAC_DEF_RX_CH),
2801 emac_rx_handler);
2802 if (WARN_ON(!priv->txchan || !priv->rxchan)) {
2803 rc = -ENOMEM;
2804 goto no_irq_res;
2805 }
Sriramakrishnanad021ae2009-11-19 15:58:27 +05302806
Anant Golea6286ee2009-05-18 15:19:01 -07002807 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2808 if (!res) {
2809 dev_err(emac_dev, "DaVinci EMAC: Error getting irq res\n");
2810 rc = -ENOENT;
2811 goto no_irq_res;
2812 }
2813 ndev->irq = res->start;
2814
2815 if (!is_valid_ether_addr(priv->mac_addr)) {
Anant Golea6286ee2009-05-18 15:19:01 -07002816 /* Use random MAC if none passed */
2817 random_ether_addr(priv->mac_addr);
Chaithrika U S5c726162009-06-03 21:54:29 -07002818 printk(KERN_WARNING "%s: using random MAC addr: %pM\n",
2819 __func__, priv->mac_addr);
Anant Golea6286ee2009-05-18 15:19:01 -07002820 }
2821
2822 ndev->netdev_ops = &emac_netdev_ops;
2823 SET_ETHTOOL_OPS(ndev, &ethtool_ops);
2824 netif_napi_add(ndev, &priv->napi, emac_poll, EMAC_POLL_WEIGHT);
2825
Sriram1ca518b2010-01-07 00:22:37 +00002826 clk_enable(emac_clk);
2827
Anant Golea6286ee2009-05-18 15:19:01 -07002828 /* register the network device */
2829 SET_NETDEV_DEV(ndev, &pdev->dev);
2830 rc = register_netdev(ndev);
2831 if (rc) {
2832 dev_err(emac_dev, "DaVinci EMAC: Error in register_netdev\n");
2833 rc = -ENODEV;
2834 goto netdev_reg_err;
2835 }
2836
Anant Golea6286ee2009-05-18 15:19:01 -07002837
Anant Golea6286ee2009-05-18 15:19:01 -07002838 if (netif_msg_probe(priv)) {
2839 dev_notice(emac_dev, "DaVinci EMAC Probe found device "\
2840 "(regs: %p, irq: %d)\n",
2841 (void *)priv->emac_base_phys, ndev->irq);
2842 }
2843 return 0;
2844
Anant Golea6286ee2009-05-18 15:19:01 -07002845netdev_reg_err:
Sriram1ca518b2010-01-07 00:22:37 +00002846 clk_disable(emac_clk);
Anant Golea6286ee2009-05-18 15:19:01 -07002847no_irq_res:
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002848 if (priv->txchan)
2849 cpdma_chan_destroy(priv->txchan);
2850 if (priv->rxchan)
2851 cpdma_chan_destroy(priv->rxchan);
2852 cpdma_ctlr_destroy(priv->dma);
2853no_dma:
Anant Golea6286ee2009-05-18 15:19:01 -07002854 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2855 release_mem_region(res->start, res->end - res->start + 1);
2856 iounmap(priv->remap_addr);
2857
2858probe_quit:
2859 clk_put(emac_clk);
2860 free_netdev(ndev);
2861 return rc;
2862}
2863
2864/**
2865 * davinci_emac_remove: EMAC device remove
2866 * @pdev: The DaVinci EMAC device that we are removing
2867 *
2868 * Called when removing the device driver. We disable clock usage and release
2869 * the resources taken up by the driver and unregister network device
2870 */
2871static int __devexit davinci_emac_remove(struct platform_device *pdev)
2872{
2873 struct resource *res;
2874 struct net_device *ndev = platform_get_drvdata(pdev);
2875 struct emac_priv *priv = netdev_priv(ndev);
2876
2877 dev_notice(&ndev->dev, "DaVinci EMAC: davinci_emac_remove()\n");
2878
Anant Golea6286ee2009-05-18 15:19:01 -07002879 platform_set_drvdata(pdev, NULL);
2880 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Anant Golea6286ee2009-05-18 15:19:01 -07002881
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002882 if (priv->txchan)
2883 cpdma_chan_destroy(priv->txchan);
2884 if (priv->rxchan)
2885 cpdma_chan_destroy(priv->rxchan);
2886 cpdma_ctlr_destroy(priv->dma);
2887
Anant Golea6286ee2009-05-18 15:19:01 -07002888 release_mem_region(res->start, res->end - res->start + 1);
2889
2890 unregister_netdev(ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07002891 iounmap(priv->remap_addr);
Stefan Weil2a1bc0d2010-08-03 08:53:45 +00002892 free_netdev(ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07002893
2894 clk_disable(emac_clk);
2895 clk_put(emac_clk);
2896
2897 return 0;
2898}
2899
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002900static int davinci_emac_suspend(struct device *dev)
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002901{
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002902 struct platform_device *pdev = to_platform_device(dev);
2903 struct net_device *ndev = platform_get_drvdata(pdev);
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002904
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002905 if (netif_running(ndev))
2906 emac_dev_stop(ndev);
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002907
2908 clk_disable(emac_clk);
2909
2910 return 0;
2911}
2912
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002913static int davinci_emac_resume(struct device *dev)
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002914{
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002915 struct platform_device *pdev = to_platform_device(dev);
2916 struct net_device *ndev = platform_get_drvdata(pdev);
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002917
2918 clk_enable(emac_clk);
2919
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002920 if (netif_running(ndev))
2921 emac_dev_open(ndev);
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002922
2923 return 0;
2924}
2925
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002926static const struct dev_pm_ops davinci_emac_pm_ops = {
2927 .suspend = davinci_emac_suspend,
2928 .resume = davinci_emac_resume,
2929};
2930
Anant Golea6286ee2009-05-18 15:19:01 -07002931/**
2932 * davinci_emac_driver: EMAC platform driver structure
Anant Golea6286ee2009-05-18 15:19:01 -07002933 */
2934static struct platform_driver davinci_emac_driver = {
2935 .driver = {
2936 .name = "davinci_emac",
2937 .owner = THIS_MODULE,
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002938 .pm = &davinci_emac_pm_ops,
Anant Golea6286ee2009-05-18 15:19:01 -07002939 },
2940 .probe = davinci_emac_probe,
2941 .remove = __devexit_p(davinci_emac_remove),
2942};
2943
2944/**
2945 * davinci_emac_init: EMAC driver module init
2946 *
2947 * Called when initializing the driver. We register the driver with
2948 * the platform.
2949 */
2950static int __init davinci_emac_init(void)
2951{
2952 return platform_driver_register(&davinci_emac_driver);
2953}
Rajashekhara, Sudhakar2db95172009-08-19 10:39:55 +00002954late_initcall(davinci_emac_init);
Anant Golea6286ee2009-05-18 15:19:01 -07002955
2956/**
2957 * davinci_emac_exit: EMAC driver module exit
2958 *
2959 * Called when exiting the driver completely. We unregister the driver with
2960 * the platform and exit
2961 */
2962static void __exit davinci_emac_exit(void)
2963{
2964 platform_driver_unregister(&davinci_emac_driver);
2965}
2966module_exit(davinci_emac_exit);
2967
2968MODULE_LICENSE("GPL");
2969MODULE_AUTHOR("DaVinci EMAC Maintainer: Anant Gole <anantgole@ti.com>");
2970MODULE_AUTHOR("DaVinci EMAC Maintainer: Chaithrika U S <chaithrika@ti.com>");
2971MODULE_DESCRIPTION("DaVinci EMAC Ethernet driver");