blob: 997f5995fce1dfb46a45631f264c429e60265174 [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
Anant Golea6286ee2009-05-18 15:19:01 -070066static int debug_level;
67module_param(debug_level, int, 0);
68MODULE_PARM_DESC(debug_level, "DaVinci EMAC debug level (NETIF_MSG bits)");
69
70/* Netif debug messages possible */
71#define DAVINCI_EMAC_DEBUG (NETIF_MSG_DRV | \
72 NETIF_MSG_PROBE | \
73 NETIF_MSG_LINK | \
74 NETIF_MSG_TIMER | \
75 NETIF_MSG_IFDOWN | \
76 NETIF_MSG_IFUP | \
77 NETIF_MSG_RX_ERR | \
78 NETIF_MSG_TX_ERR | \
79 NETIF_MSG_TX_QUEUED | \
80 NETIF_MSG_INTR | \
81 NETIF_MSG_TX_DONE | \
82 NETIF_MSG_RX_STATUS | \
83 NETIF_MSG_PKTDATA | \
84 NETIF_MSG_HW | \
85 NETIF_MSG_WOL)
86
87/* version info */
88#define EMAC_MAJOR_VERSION 6
89#define EMAC_MINOR_VERSION 1
90#define EMAC_MODULE_VERSION "6.1"
91MODULE_VERSION(EMAC_MODULE_VERSION);
92static const char emac_version_string[] = "TI DaVinci EMAC Linux v6.1";
93
94/* Configuration items */
95#define EMAC_DEF_PASS_CRC (0) /* Do not pass CRC upto frames */
96#define EMAC_DEF_QOS_EN (0) /* EMAC proprietary QoS disabled */
97#define EMAC_DEF_NO_BUFF_CHAIN (0) /* No buffer chain */
98#define EMAC_DEF_MACCTRL_FRAME_EN (0) /* Discard Maccontrol frames */
99#define EMAC_DEF_SHORT_FRAME_EN (0) /* Discard short frames */
100#define EMAC_DEF_ERROR_FRAME_EN (0) /* Discard error frames */
101#define EMAC_DEF_PROM_EN (0) /* Promiscous disabled */
102#define EMAC_DEF_PROM_CH (0) /* Promiscous channel is 0 */
103#define EMAC_DEF_BCAST_EN (1) /* Broadcast enabled */
104#define EMAC_DEF_BCAST_CH (0) /* Broadcast channel is 0 */
105#define EMAC_DEF_MCAST_EN (1) /* Multicast enabled */
106#define EMAC_DEF_MCAST_CH (0) /* Multicast channel is 0 */
107
108#define EMAC_DEF_TXPRIO_FIXED (1) /* TX Priority is fixed */
109#define EMAC_DEF_TXPACING_EN (0) /* TX pacing NOT supported*/
110
111#define EMAC_DEF_BUFFER_OFFSET (0) /* Buffer offset to DMA (future) */
112#define EMAC_DEF_MIN_ETHPKTSIZE (60) /* Minimum ethernet pkt size */
113#define EMAC_DEF_MAX_FRAME_SIZE (1500 + 14 + 4 + 4)
114#define EMAC_DEF_TX_CH (0) /* Default 0th channel */
115#define EMAC_DEF_RX_CH (0) /* Default 0th channel */
116#define EMAC_DEF_MDIO_TICK_MS (10) /* typically 1 tick=1 ms) */
117#define EMAC_DEF_MAX_TX_CH (1) /* Max TX channels configured */
118#define EMAC_DEF_MAX_RX_CH (1) /* Max RX channels configured */
119#define EMAC_POLL_WEIGHT (64) /* Default NAPI poll weight */
120
121/* Buffer descriptor parameters */
122#define EMAC_DEF_TX_MAX_SERVICE (32) /* TX max service BD's */
123#define EMAC_DEF_RX_MAX_SERVICE (64) /* should = netdev->weight */
124
125/* EMAC register related defines */
126#define EMAC_ALL_MULTI_REG_VALUE (0xFFFFFFFF)
127#define EMAC_NUM_MULTICAST_BITS (64)
128#define EMAC_TEARDOWN_VALUE (0xFFFFFFFC)
129#define EMAC_TX_CONTROL_TX_ENABLE_VAL (0x1)
130#define EMAC_RX_CONTROL_RX_ENABLE_VAL (0x1)
131#define EMAC_MAC_HOST_ERR_INTMASK_VAL (0x2)
132#define EMAC_RX_UNICAST_CLEAR_ALL (0xFF)
133#define EMAC_INT_MASK_CLEAR (0xFF)
134
135/* RX MBP register bit positions */
136#define EMAC_RXMBP_PASSCRC_MASK BIT(30)
137#define EMAC_RXMBP_QOSEN_MASK BIT(29)
138#define EMAC_RXMBP_NOCHAIN_MASK BIT(28)
139#define EMAC_RXMBP_CMFEN_MASK BIT(24)
140#define EMAC_RXMBP_CSFEN_MASK BIT(23)
141#define EMAC_RXMBP_CEFEN_MASK BIT(22)
142#define EMAC_RXMBP_CAFEN_MASK BIT(21)
143#define EMAC_RXMBP_PROMCH_SHIFT (16)
144#define EMAC_RXMBP_PROMCH_MASK (0x7 << 16)
145#define EMAC_RXMBP_BROADEN_MASK BIT(13)
146#define EMAC_RXMBP_BROADCH_SHIFT (8)
147#define EMAC_RXMBP_BROADCH_MASK (0x7 << 8)
148#define EMAC_RXMBP_MULTIEN_MASK BIT(5)
149#define EMAC_RXMBP_MULTICH_SHIFT (0)
150#define EMAC_RXMBP_MULTICH_MASK (0x7)
151#define EMAC_RXMBP_CHMASK (0x7)
152
153/* EMAC register definitions/bit maps used */
154# define EMAC_MBP_RXPROMISC (0x00200000)
155# define EMAC_MBP_PROMISCCH(ch) (((ch) & 0x7) << 16)
156# define EMAC_MBP_RXBCAST (0x00002000)
157# define EMAC_MBP_BCASTCHAN(ch) (((ch) & 0x7) << 8)
158# define EMAC_MBP_RXMCAST (0x00000020)
159# define EMAC_MBP_MCASTCHAN(ch) ((ch) & 0x7)
160
161/* EMAC mac_control register */
chaithrika@ti.com69ef9692009-10-01 10:25:19 +0000162#define EMAC_MACCONTROL_TXPTYPE BIT(9)
163#define EMAC_MACCONTROL_TXPACEEN BIT(6)
164#define EMAC_MACCONTROL_GMIIEN BIT(5)
165#define EMAC_MACCONTROL_GIGABITEN BIT(7)
166#define EMAC_MACCONTROL_FULLDUPLEXEN BIT(0)
Anant Golea6286ee2009-05-18 15:19:01 -0700167#define EMAC_MACCONTROL_RMIISPEED_MASK BIT(15)
168
169/* GIGABIT MODE related bits */
Anant Golea6286ee2009-05-18 15:19:01 -0700170#define EMAC_DM646X_MACCONTORL_GIG BIT(7)
171#define EMAC_DM646X_MACCONTORL_GIGFORCE BIT(17)
172
173/* EMAC mac_status register */
174#define EMAC_MACSTATUS_TXERRCODE_MASK (0xF00000)
175#define EMAC_MACSTATUS_TXERRCODE_SHIFT (20)
176#define EMAC_MACSTATUS_TXERRCH_MASK (0x7)
177#define EMAC_MACSTATUS_TXERRCH_SHIFT (16)
178#define EMAC_MACSTATUS_RXERRCODE_MASK (0xF000)
179#define EMAC_MACSTATUS_RXERRCODE_SHIFT (12)
180#define EMAC_MACSTATUS_RXERRCH_MASK (0x7)
181#define EMAC_MACSTATUS_RXERRCH_SHIFT (8)
182
183/* EMAC RX register masks */
184#define EMAC_RX_MAX_LEN_MASK (0xFFFF)
185#define EMAC_RX_BUFFER_OFFSET_MASK (0xFFFF)
186
187/* MAC_IN_VECTOR (0x180) register bit fields */
chaithrika@ti.com69ef9692009-10-01 10:25:19 +0000188#define EMAC_DM644X_MAC_IN_VECTOR_HOST_INT BIT(17)
189#define EMAC_DM644X_MAC_IN_VECTOR_STATPEND_INT BIT(16)
190#define EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC BIT(8)
191#define EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC BIT(0)
Anant Golea6286ee2009-05-18 15:19:01 -0700192
193/** NOTE:: For DM646x the IN_VECTOR has changed */
194#define EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC BIT(EMAC_DEF_RX_CH)
195#define EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC BIT(16 + EMAC_DEF_TX_CH)
Sriram43c2ed82009-09-24 19:15:18 +0000196#define EMAC_DM646X_MAC_IN_VECTOR_HOST_INT BIT(26)
197#define EMAC_DM646X_MAC_IN_VECTOR_STATPEND_INT BIT(27)
198
Anant Golea6286ee2009-05-18 15:19:01 -0700199/* CPPI bit positions */
200#define EMAC_CPPI_SOP_BIT BIT(31)
201#define EMAC_CPPI_EOP_BIT BIT(30)
202#define EMAC_CPPI_OWNERSHIP_BIT BIT(29)
203#define EMAC_CPPI_EOQ_BIT BIT(28)
204#define EMAC_CPPI_TEARDOWN_COMPLETE_BIT BIT(27)
205#define EMAC_CPPI_PASS_CRC_BIT BIT(26)
206#define EMAC_RX_BD_BUF_SIZE (0xFFFF)
207#define EMAC_BD_LENGTH_FOR_CACHE (16) /* only CPPI bytes */
208#define EMAC_RX_BD_PKT_LENGTH_MASK (0xFFFF)
209
210/* Max hardware defines */
211#define EMAC_MAX_TXRX_CHANNELS (8) /* Max hardware channels */
212#define EMAC_DEF_MAX_MULTICAST_ADDRESSES (64) /* Max mcast addr's */
213
214/* EMAC Peripheral Device Register Memory Layout structure */
215#define EMAC_TXIDVER 0x0
216#define EMAC_TXCONTROL 0x4
217#define EMAC_TXTEARDOWN 0x8
218#define EMAC_RXIDVER 0x10
219#define EMAC_RXCONTROL 0x14
220#define EMAC_RXTEARDOWN 0x18
221#define EMAC_TXINTSTATRAW 0x80
222#define EMAC_TXINTSTATMASKED 0x84
223#define EMAC_TXINTMASKSET 0x88
224#define EMAC_TXINTMASKCLEAR 0x8C
225#define EMAC_MACINVECTOR 0x90
226
227#define EMAC_DM646X_MACEOIVECTOR 0x94
228
229#define EMAC_RXINTSTATRAW 0xA0
230#define EMAC_RXINTSTATMASKED 0xA4
231#define EMAC_RXINTMASKSET 0xA8
232#define EMAC_RXINTMASKCLEAR 0xAC
233#define EMAC_MACINTSTATRAW 0xB0
234#define EMAC_MACINTSTATMASKED 0xB4
235#define EMAC_MACINTMASKSET 0xB8
236#define EMAC_MACINTMASKCLEAR 0xBC
237
238#define EMAC_RXMBPENABLE 0x100
239#define EMAC_RXUNICASTSET 0x104
240#define EMAC_RXUNICASTCLEAR 0x108
241#define EMAC_RXMAXLEN 0x10C
242#define EMAC_RXBUFFEROFFSET 0x110
243#define EMAC_RXFILTERLOWTHRESH 0x114
244
245#define EMAC_MACCONTROL 0x160
246#define EMAC_MACSTATUS 0x164
247#define EMAC_EMCONTROL 0x168
248#define EMAC_FIFOCONTROL 0x16C
249#define EMAC_MACCONFIG 0x170
250#define EMAC_SOFTRESET 0x174
251#define EMAC_MACSRCADDRLO 0x1D0
252#define EMAC_MACSRCADDRHI 0x1D4
253#define EMAC_MACHASH1 0x1D8
254#define EMAC_MACHASH2 0x1DC
255#define EMAC_MACADDRLO 0x500
256#define EMAC_MACADDRHI 0x504
257#define EMAC_MACINDEX 0x508
258
259/* EMAC HDP and Completion registors */
260#define EMAC_TXHDP(ch) (0x600 + (ch * 4))
261#define EMAC_RXHDP(ch) (0x620 + (ch * 4))
262#define EMAC_TXCP(ch) (0x640 + (ch * 4))
263#define EMAC_RXCP(ch) (0x660 + (ch * 4))
264
265/* EMAC statistics registers */
266#define EMAC_RXGOODFRAMES 0x200
267#define EMAC_RXBCASTFRAMES 0x204
268#define EMAC_RXMCASTFRAMES 0x208
269#define EMAC_RXPAUSEFRAMES 0x20C
270#define EMAC_RXCRCERRORS 0x210
271#define EMAC_RXALIGNCODEERRORS 0x214
272#define EMAC_RXOVERSIZED 0x218
273#define EMAC_RXJABBER 0x21C
274#define EMAC_RXUNDERSIZED 0x220
275#define EMAC_RXFRAGMENTS 0x224
276#define EMAC_RXFILTERED 0x228
277#define EMAC_RXQOSFILTERED 0x22C
278#define EMAC_RXOCTETS 0x230
279#define EMAC_TXGOODFRAMES 0x234
280#define EMAC_TXBCASTFRAMES 0x238
281#define EMAC_TXMCASTFRAMES 0x23C
282#define EMAC_TXPAUSEFRAMES 0x240
283#define EMAC_TXDEFERRED 0x244
284#define EMAC_TXCOLLISION 0x248
285#define EMAC_TXSINGLECOLL 0x24C
286#define EMAC_TXMULTICOLL 0x250
287#define EMAC_TXEXCESSIVECOLL 0x254
288#define EMAC_TXLATECOLL 0x258
289#define EMAC_TXUNDERRUN 0x25C
290#define EMAC_TXCARRIERSENSE 0x260
291#define EMAC_TXOCTETS 0x264
292#define EMAC_NETOCTETS 0x280
293#define EMAC_RXSOFOVERRUNS 0x284
294#define EMAC_RXMOFOVERRUNS 0x288
295#define EMAC_RXDMAOVERRUNS 0x28C
296
297/* EMAC DM644x control registers */
298#define EMAC_CTRL_EWCTL (0x4)
299#define EMAC_CTRL_EWINTTCNT (0x8)
300
Sriram84da2652010-07-29 02:33:58 +0000301/* EMAC DM644x control module masks */
302#define EMAC_DM644X_EWINTCNT_MASK 0x1FFFF
303#define EMAC_DM644X_INTMIN_INTVL 0x1
304#define EMAC_DM644X_INTMAX_INTVL (EMAC_DM644X_EWINTCNT_MASK)
305
Anant Golea6286ee2009-05-18 15:19:01 -0700306/* EMAC MDIO related */
307/* Mask & Control defines */
308#define MDIO_CONTROL_CLKDIV (0xFF)
309#define MDIO_CONTROL_ENABLE BIT(30)
310#define MDIO_USERACCESS_GO BIT(31)
311#define MDIO_USERACCESS_WRITE BIT(30)
312#define MDIO_USERACCESS_READ (0)
313#define MDIO_USERACCESS_REGADR (0x1F << 21)
314#define MDIO_USERACCESS_PHYADR (0x1F << 16)
315#define MDIO_USERACCESS_DATA (0xFFFF)
316#define MDIO_USERPHYSEL_LINKSEL BIT(7)
317#define MDIO_VER_MODID (0xFFFF << 16)
318#define MDIO_VER_REVMAJ (0xFF << 8)
319#define MDIO_VER_REVMIN (0xFF)
320
321#define MDIO_USERACCESS(inst) (0x80 + (inst * 8))
322#define MDIO_USERPHYSEL(inst) (0x84 + (inst * 8))
323#define MDIO_CONTROL (0x04)
324
325/* EMAC DM646X control module registers */
Sriram84da2652010-07-29 02:33:58 +0000326#define EMAC_DM646X_CMINTCTRL 0x0C
327#define EMAC_DM646X_CMRXINTEN 0x14
328#define EMAC_DM646X_CMTXINTEN 0x18
329#define EMAC_DM646X_CMRXINTMAX 0x70
330#define EMAC_DM646X_CMTXINTMAX 0x74
331
332/* EMAC DM646X control module masks */
333#define EMAC_DM646X_INTPACEEN (0x3 << 16)
334#define EMAC_DM646X_INTPRESCALE_MASK (0x7FF << 0)
335#define EMAC_DM646X_CMINTMAX_CNT 63
336#define EMAC_DM646X_CMINTMIN_CNT 2
337#define EMAC_DM646X_CMINTMAX_INTVL (1000 / EMAC_DM646X_CMINTMIN_CNT)
338#define EMAC_DM646X_CMINTMIN_INTVL ((1000 / EMAC_DM646X_CMINTMAX_CNT) + 1)
339
Anant Golea6286ee2009-05-18 15:19:01 -0700340
341/* EMAC EOI codes for C0 */
342#define EMAC_DM646X_MAC_EOI_C0_RXEN (0x01)
343#define EMAC_DM646X_MAC_EOI_C0_TXEN (0x02)
344
Sriram0fe74632009-10-07 02:44:30 +0000345/* EMAC Stats Clear Mask */
346#define EMAC_STATS_CLR_MASK (0xFFFFFFFF)
347
Anant Golea6286ee2009-05-18 15:19:01 -0700348/** net_buf_obj: EMAC network bufferdata structure
349 *
350 * EMAC network buffer data structure
351 */
352struct emac_netbufobj {
353 void *buf_token;
354 char *data_ptr;
355 int length;
356};
357
358/** net_pkt_obj: EMAC network packet data structure
359 *
360 * EMAC network packet data structure - supports buffer list (for future)
361 */
362struct emac_netpktobj {
363 void *pkt_token; /* data token may hold tx/rx chan id */
364 struct emac_netbufobj *buf_list; /* array of network buffer objects */
365 int num_bufs;
366 int pkt_length;
367};
368
369/** emac_tx_bd: EMAC TX Buffer descriptor data structure
370 *
371 * EMAC TX Buffer descriptor data structure
372 */
373struct emac_tx_bd {
374 int h_next;
375 int buff_ptr;
376 int off_b_len;
377 int mode; /* SOP, EOP, ownership, EOQ, teardown,Qstarv, length */
378 struct emac_tx_bd __iomem *next;
379 void *buf_token;
380};
381
382/** emac_txch: EMAC TX Channel data structure
383 *
384 * EMAC TX Channel data structure
385 */
386struct emac_txch {
387 /* Config related */
388 u32 num_bd;
389 u32 service_max;
390
391 /* CPPI specific */
392 u32 alloc_size;
393 void __iomem *bd_mem;
394 struct emac_tx_bd __iomem *bd_pool_head;
395 struct emac_tx_bd __iomem *active_queue_head;
396 struct emac_tx_bd __iomem *active_queue_tail;
397 struct emac_tx_bd __iomem *last_hw_bdprocessed;
398 u32 queue_active;
399 u32 teardown_pending;
400 u32 *tx_complete;
401
402 /** statistics */
403 u32 proc_count; /* TX: # of times emac_tx_bdproc is called */
404 u32 mis_queued_packets;
405 u32 queue_reinit;
406 u32 end_of_queue_add;
407 u32 out_of_tx_bd;
408 u32 no_active_pkts; /* IRQ when there were no packets to process */
409 u32 active_queue_count;
410};
411
412/** emac_rx_bd: EMAC RX Buffer descriptor data structure
413 *
414 * EMAC RX Buffer descriptor data structure
415 */
416struct emac_rx_bd {
417 int h_next;
418 int buff_ptr;
419 int off_b_len;
420 int mode;
421 struct emac_rx_bd __iomem *next;
422 void *data_ptr;
423 void *buf_token;
424};
425
426/** emac_rxch: EMAC RX Channel data structure
427 *
428 * EMAC RX Channel data structure
429 */
430struct emac_rxch {
431 /* configuration info */
432 u32 num_bd;
433 u32 service_max;
434 u32 buf_size;
435 char mac_addr[6];
436
437 /** CPPI specific */
438 u32 alloc_size;
439 void __iomem *bd_mem;
440 struct emac_rx_bd __iomem *bd_pool_head;
441 struct emac_rx_bd __iomem *active_queue_head;
442 struct emac_rx_bd __iomem *active_queue_tail;
443 u32 queue_active;
444 u32 teardown_pending;
445
446 /* packet and buffer objects */
447 struct emac_netpktobj pkt_queue;
448 struct emac_netbufobj buf_queue;
449
450 /** statistics */
451 u32 proc_count; /* number of times emac_rx_bdproc is called */
452 u32 processed_bd;
453 u32 recycled_bd;
454 u32 out_of_rx_bd;
455 u32 out_of_rx_buffers;
456 u32 queue_reinit;
457 u32 end_of_queue_add;
458 u32 end_of_queue;
459 u32 mis_queued_packets;
460};
461
462/* emac_priv: EMAC private data structure
463 *
464 * EMAC adapter private data structure
465 */
466struct emac_priv {
467 u32 msg_enable;
468 struct net_device *ndev;
469 struct platform_device *pdev;
470 struct napi_struct napi;
471 char mac_addr[6];
472 spinlock_t tx_lock;
473 spinlock_t rx_lock;
474 void __iomem *remap_addr;
475 u32 emac_base_phys;
476 void __iomem *emac_base;
477 void __iomem *ctrl_base;
478 void __iomem *emac_ctrl_ram;
479 u32 ctrl_ram_size;
Sriramakrishnanad021ae2009-11-19 15:58:27 +0530480 u32 hw_ram_addr;
Anant Golea6286ee2009-05-18 15:19:01 -0700481 struct emac_txch *txch[EMAC_DEF_MAX_TX_CH];
482 struct emac_rxch *rxch[EMAC_DEF_MAX_RX_CH];
483 u32 link; /* 1=link on, 0=link off */
484 u32 speed; /* 0=Auto Neg, 1=No PHY, 10,100, 1000 - mbps */
485 u32 duplex; /* Link duplex: 0=Half, 1=Full */
486 u32 rx_buf_size;
487 u32 isr_count;
Sriram84da2652010-07-29 02:33:58 +0000488 u32 coal_intvl;
489 u32 bus_freq_mhz;
Anant Golea6286ee2009-05-18 15:19:01 -0700490 u8 rmii_en;
491 u8 version;
Anant Golea6286ee2009-05-18 15:19:01 -0700492 u32 mac_hash1;
493 u32 mac_hash2;
494 u32 multicast_hash_cnt[EMAC_NUM_MULTICAST_BITS];
495 u32 rx_addr_type;
496 /* periodic timer required for MDIO polling */
497 struct timer_list periodic_timer;
498 u32 periodic_ticks;
499 u32 timer_active;
500 u32 phy_mask;
501 /* mii_bus,phy members */
502 struct mii_bus *mii_bus;
Cyril Chemparathy5d69e002010-09-15 10:11:24 -0400503 const char *phy_id;
Anant Golea6286ee2009-05-18 15:19:01 -0700504 struct phy_device *phydev;
505 spinlock_t lock;
Sriramakrishnan01a9af32009-11-19 15:58:26 +0530506 /*platform specific members*/
507 void (*int_enable) (void);
508 void (*int_disable) (void);
Anant Golea6286ee2009-05-18 15:19:01 -0700509};
510
511/* clock frequency for EMAC */
512static struct clk *emac_clk;
513static unsigned long emac_bus_frequency;
514static unsigned long mdio_max_freq;
515
Sriramakrishnanad021ae2009-11-19 15:58:27 +0530516#define emac_virt_to_phys(addr, priv) \
517 (((u32 __force)(addr) - (u32 __force)(priv->emac_ctrl_ram)) \
518 + priv->hw_ram_addr)
Anant Golea6286ee2009-05-18 15:19:01 -0700519
520/* Cache macros - Packet buffers would be from skb pool which is cached */
521#define EMAC_VIRT_NOCACHE(addr) (addr)
Anant Golea6286ee2009-05-18 15:19:01 -0700522
523/* DM644x does not have BD's in cached memory - so no cache functions */
524#define BD_CACHE_INVALIDATE(addr, size)
525#define BD_CACHE_WRITEBACK(addr, size)
526#define BD_CACHE_WRITEBACK_INVALIDATE(addr, size)
527
528/* EMAC TX Host Error description strings */
529static char *emac_txhost_errcodes[16] = {
530 "No error", "SOP error", "Ownership bit not set in SOP buffer",
531 "Zero Next Buffer Descriptor Pointer Without EOP",
532 "Zero Buffer Pointer", "Zero Buffer Length", "Packet Length Error",
533 "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
534 "Reserved", "Reserved", "Reserved", "Reserved"
535};
536
537/* EMAC RX Host Error description strings */
538static char *emac_rxhost_errcodes[16] = {
539 "No error", "Reserved", "Ownership bit not set in input buffer",
540 "Reserved", "Zero Buffer Pointer", "Reserved", "Reserved",
541 "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
542 "Reserved", "Reserved", "Reserved", "Reserved"
543};
544
545/* Helper macros */
546#define emac_read(reg) ioread32(priv->emac_base + (reg))
547#define emac_write(reg, val) iowrite32(val, priv->emac_base + (reg))
548
549#define emac_ctrl_read(reg) ioread32((priv->ctrl_base + (reg)))
550#define emac_ctrl_write(reg, val) iowrite32(val, (priv->ctrl_base + (reg)))
551
552#define emac_mdio_read(reg) ioread32(bus->priv + (reg))
553#define emac_mdio_write(reg, val) iowrite32(val, (bus->priv + (reg)))
554
555/**
556 * emac_dump_regs: Dump important EMAC registers to debug terminal
557 * @priv: The DaVinci EMAC private adapter structure
558 *
559 * Executes ethtool set cmd & sets phy mode
560 *
561 */
562static void emac_dump_regs(struct emac_priv *priv)
563{
564 struct device *emac_dev = &priv->ndev->dev;
565
566 /* Print important registers in EMAC */
567 dev_info(emac_dev, "EMAC Basic registers\n");
Srirame9947622010-07-29 02:34:00 +0000568 if (priv->version == EMAC_VERSION_1) {
569 dev_info(emac_dev, "EMAC: EWCTL: %08X, EWINTTCNT: %08X\n",
570 emac_ctrl_read(EMAC_CTRL_EWCTL),
571 emac_ctrl_read(EMAC_CTRL_EWINTTCNT));
572 }
Anant Golea6286ee2009-05-18 15:19:01 -0700573 dev_info(emac_dev, "EMAC: TXID: %08X %s, RXID: %08X %s\n",
574 emac_read(EMAC_TXIDVER),
575 ((emac_read(EMAC_TXCONTROL)) ? "enabled" : "disabled"),
576 emac_read(EMAC_RXIDVER),
577 ((emac_read(EMAC_RXCONTROL)) ? "enabled" : "disabled"));
578 dev_info(emac_dev, "EMAC: TXIntRaw:%08X, TxIntMasked: %08X, "\
579 "TxIntMasSet: %08X\n", emac_read(EMAC_TXINTSTATRAW),
580 emac_read(EMAC_TXINTSTATMASKED), emac_read(EMAC_TXINTMASKSET));
581 dev_info(emac_dev, "EMAC: RXIntRaw:%08X, RxIntMasked: %08X, "\
582 "RxIntMasSet: %08X\n", emac_read(EMAC_RXINTSTATRAW),
583 emac_read(EMAC_RXINTSTATMASKED), emac_read(EMAC_RXINTMASKSET));
584 dev_info(emac_dev, "EMAC: MacIntRaw:%08X, MacIntMasked: %08X, "\
585 "MacInVector=%08X\n", emac_read(EMAC_MACINTSTATRAW),
586 emac_read(EMAC_MACINTSTATMASKED), emac_read(EMAC_MACINVECTOR));
587 dev_info(emac_dev, "EMAC: EmuControl:%08X, FifoControl: %08X\n",
588 emac_read(EMAC_EMCONTROL), emac_read(EMAC_FIFOCONTROL));
589 dev_info(emac_dev, "EMAC: MBPEnable:%08X, RXUnicastSet: %08X, "\
590 "RXMaxLen=%08X\n", emac_read(EMAC_RXMBPENABLE),
591 emac_read(EMAC_RXUNICASTSET), emac_read(EMAC_RXMAXLEN));
592 dev_info(emac_dev, "EMAC: MacControl:%08X, MacStatus: %08X, "\
593 "MacConfig=%08X\n", emac_read(EMAC_MACCONTROL),
594 emac_read(EMAC_MACSTATUS), emac_read(EMAC_MACCONFIG));
595 dev_info(emac_dev, "EMAC: TXHDP[0]:%08X, RXHDP[0]: %08X\n",
596 emac_read(EMAC_TXHDP(0)), emac_read(EMAC_RXHDP(0)));
597 dev_info(emac_dev, "EMAC Statistics\n");
598 dev_info(emac_dev, "EMAC: rx_good_frames:%d\n",
599 emac_read(EMAC_RXGOODFRAMES));
600 dev_info(emac_dev, "EMAC: rx_broadcast_frames:%d\n",
601 emac_read(EMAC_RXBCASTFRAMES));
602 dev_info(emac_dev, "EMAC: rx_multicast_frames:%d\n",
603 emac_read(EMAC_RXMCASTFRAMES));
604 dev_info(emac_dev, "EMAC: rx_pause_frames:%d\n",
605 emac_read(EMAC_RXPAUSEFRAMES));
606 dev_info(emac_dev, "EMAC: rx_crcerrors:%d\n",
607 emac_read(EMAC_RXCRCERRORS));
608 dev_info(emac_dev, "EMAC: rx_align_code_errors:%d\n",
609 emac_read(EMAC_RXALIGNCODEERRORS));
610 dev_info(emac_dev, "EMAC: rx_oversized_frames:%d\n",
611 emac_read(EMAC_RXOVERSIZED));
612 dev_info(emac_dev, "EMAC: rx_jabber_frames:%d\n",
613 emac_read(EMAC_RXJABBER));
614 dev_info(emac_dev, "EMAC: rx_undersized_frames:%d\n",
615 emac_read(EMAC_RXUNDERSIZED));
616 dev_info(emac_dev, "EMAC: rx_fragments:%d\n",
617 emac_read(EMAC_RXFRAGMENTS));
618 dev_info(emac_dev, "EMAC: rx_filtered_frames:%d\n",
619 emac_read(EMAC_RXFILTERED));
620 dev_info(emac_dev, "EMAC: rx_qos_filtered_frames:%d\n",
621 emac_read(EMAC_RXQOSFILTERED));
622 dev_info(emac_dev, "EMAC: rx_octets:%d\n",
623 emac_read(EMAC_RXOCTETS));
624 dev_info(emac_dev, "EMAC: tx_goodframes:%d\n",
625 emac_read(EMAC_TXGOODFRAMES));
626 dev_info(emac_dev, "EMAC: tx_bcastframes:%d\n",
627 emac_read(EMAC_TXBCASTFRAMES));
628 dev_info(emac_dev, "EMAC: tx_mcastframes:%d\n",
629 emac_read(EMAC_TXMCASTFRAMES));
630 dev_info(emac_dev, "EMAC: tx_pause_frames:%d\n",
631 emac_read(EMAC_TXPAUSEFRAMES));
632 dev_info(emac_dev, "EMAC: tx_deferred_frames:%d\n",
633 emac_read(EMAC_TXDEFERRED));
634 dev_info(emac_dev, "EMAC: tx_collision_frames:%d\n",
635 emac_read(EMAC_TXCOLLISION));
636 dev_info(emac_dev, "EMAC: tx_single_coll_frames:%d\n",
637 emac_read(EMAC_TXSINGLECOLL));
638 dev_info(emac_dev, "EMAC: tx_mult_coll_frames:%d\n",
639 emac_read(EMAC_TXMULTICOLL));
640 dev_info(emac_dev, "EMAC: tx_excessive_collisions:%d\n",
641 emac_read(EMAC_TXEXCESSIVECOLL));
642 dev_info(emac_dev, "EMAC: tx_late_collisions:%d\n",
643 emac_read(EMAC_TXLATECOLL));
644 dev_info(emac_dev, "EMAC: tx_underrun:%d\n",
645 emac_read(EMAC_TXUNDERRUN));
646 dev_info(emac_dev, "EMAC: tx_carrier_sense_errors:%d\n",
647 emac_read(EMAC_TXCARRIERSENSE));
648 dev_info(emac_dev, "EMAC: tx_octets:%d\n",
649 emac_read(EMAC_TXOCTETS));
650 dev_info(emac_dev, "EMAC: net_octets:%d\n",
651 emac_read(EMAC_NETOCTETS));
652 dev_info(emac_dev, "EMAC: rx_sof_overruns:%d\n",
653 emac_read(EMAC_RXSOFOVERRUNS));
654 dev_info(emac_dev, "EMAC: rx_mof_overruns:%d\n",
655 emac_read(EMAC_RXMOFOVERRUNS));
656 dev_info(emac_dev, "EMAC: rx_dma_overruns:%d\n",
657 emac_read(EMAC_RXDMAOVERRUNS));
658}
659
660/*************************************************************************
661 * EMAC MDIO/Phy Functionality
662 *************************************************************************/
663/**
664 * emac_get_drvinfo: Get EMAC driver information
665 * @ndev: The DaVinci EMAC network adapter
666 * @info: ethtool info structure containing name and version
667 *
668 * Returns EMAC driver information (name and version)
669 *
670 */
671static void emac_get_drvinfo(struct net_device *ndev,
672 struct ethtool_drvinfo *info)
673{
674 strcpy(info->driver, emac_version_string);
675 strcpy(info->version, EMAC_MODULE_VERSION);
676}
677
678/**
679 * emac_get_settings: Get EMAC settings
680 * @ndev: The DaVinci EMAC network adapter
681 * @ecmd: ethtool command
682 *
683 * Executes ethool get command
684 *
685 */
686static int emac_get_settings(struct net_device *ndev,
687 struct ethtool_cmd *ecmd)
688{
689 struct emac_priv *priv = netdev_priv(ndev);
Cyril Chemparathy5d69e002010-09-15 10:11:24 -0400690 if (priv->phydev)
Anant Golea6286ee2009-05-18 15:19:01 -0700691 return phy_ethtool_gset(priv->phydev, ecmd);
692 else
693 return -EOPNOTSUPP;
694
695}
696
697/**
698 * emac_set_settings: Set EMAC settings
699 * @ndev: The DaVinci EMAC network adapter
700 * @ecmd: ethtool command
701 *
702 * Executes ethool set command
703 *
704 */
705static int emac_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
706{
707 struct emac_priv *priv = netdev_priv(ndev);
Cyril Chemparathy5d69e002010-09-15 10:11:24 -0400708 if (priv->phydev)
Anant Golea6286ee2009-05-18 15:19:01 -0700709 return phy_ethtool_sset(priv->phydev, ecmd);
710 else
711 return -EOPNOTSUPP;
712
713}
714
715/**
Sriram84da2652010-07-29 02:33:58 +0000716 * emac_get_coalesce : Get interrupt coalesce settings for this device
717 * @ndev : The DaVinci EMAC network adapter
718 * @coal : ethtool coalesce settings structure
719 *
720 * Fetch the current interrupt coalesce settings
721 *
722 */
723static int emac_get_coalesce(struct net_device *ndev,
724 struct ethtool_coalesce *coal)
725{
726 struct emac_priv *priv = netdev_priv(ndev);
727
728 coal->rx_coalesce_usecs = priv->coal_intvl;
729 return 0;
730
731}
732
733/**
734 * emac_set_coalesce : Set interrupt coalesce settings for this device
735 * @ndev : The DaVinci EMAC network adapter
736 * @coal : ethtool coalesce settings structure
737 *
738 * Set interrupt coalesce parameters
739 *
740 */
741static int emac_set_coalesce(struct net_device *ndev,
742 struct ethtool_coalesce *coal)
743{
744 struct emac_priv *priv = netdev_priv(ndev);
745 u32 int_ctrl, num_interrupts = 0;
746 u32 prescale = 0, addnl_dvdr = 1, coal_intvl = 0;
747
748 if (!coal->rx_coalesce_usecs)
749 return -EINVAL;
750
751 coal_intvl = coal->rx_coalesce_usecs;
752
753 switch (priv->version) {
754 case EMAC_VERSION_2:
755 int_ctrl = emac_ctrl_read(EMAC_DM646X_CMINTCTRL);
756 prescale = priv->bus_freq_mhz * 4;
757
758 if (coal_intvl < EMAC_DM646X_CMINTMIN_INTVL)
759 coal_intvl = EMAC_DM646X_CMINTMIN_INTVL;
760
761 if (coal_intvl > EMAC_DM646X_CMINTMAX_INTVL) {
762 /*
763 * Interrupt pacer works with 4us Pulse, we can
764 * throttle further by dilating the 4us pulse.
765 */
766 addnl_dvdr = EMAC_DM646X_INTPRESCALE_MASK / prescale;
767
768 if (addnl_dvdr > 1) {
769 prescale *= addnl_dvdr;
770 if (coal_intvl > (EMAC_DM646X_CMINTMAX_INTVL
771 * addnl_dvdr))
772 coal_intvl = (EMAC_DM646X_CMINTMAX_INTVL
773 * addnl_dvdr);
774 } else {
775 addnl_dvdr = 1;
776 coal_intvl = EMAC_DM646X_CMINTMAX_INTVL;
777 }
778 }
779
780 num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
781
782 int_ctrl |= EMAC_DM646X_INTPACEEN;
783 int_ctrl &= (~EMAC_DM646X_INTPRESCALE_MASK);
784 int_ctrl |= (prescale & EMAC_DM646X_INTPRESCALE_MASK);
785 emac_ctrl_write(EMAC_DM646X_CMINTCTRL, int_ctrl);
786
787 emac_ctrl_write(EMAC_DM646X_CMRXINTMAX, num_interrupts);
788 emac_ctrl_write(EMAC_DM646X_CMTXINTMAX, num_interrupts);
789
790 break;
791 default:
792 int_ctrl = emac_ctrl_read(EMAC_CTRL_EWINTTCNT);
793 int_ctrl &= (~EMAC_DM644X_EWINTCNT_MASK);
794 prescale = coal_intvl * priv->bus_freq_mhz;
795 if (prescale > EMAC_DM644X_EWINTCNT_MASK) {
796 prescale = EMAC_DM644X_EWINTCNT_MASK;
797 coal_intvl = prescale / priv->bus_freq_mhz;
798 }
799 emac_ctrl_write(EMAC_CTRL_EWINTTCNT, (int_ctrl | prescale));
800
801 break;
802 }
803
804 printk(KERN_INFO"Set coalesce to %d usecs.\n", coal_intvl);
805 priv->coal_intvl = coal_intvl;
806
807 return 0;
808
809}
810
811
812/**
Anant Golea6286ee2009-05-18 15:19:01 -0700813 * ethtool_ops: DaVinci EMAC Ethtool structure
814 *
815 * Ethtool support for EMAC adapter
816 *
817 */
818static const struct ethtool_ops ethtool_ops = {
819 .get_drvinfo = emac_get_drvinfo,
820 .get_settings = emac_get_settings,
821 .set_settings = emac_set_settings,
822 .get_link = ethtool_op_get_link,
Sriram84da2652010-07-29 02:33:58 +0000823 .get_coalesce = emac_get_coalesce,
824 .set_coalesce = emac_set_coalesce,
Anant Golea6286ee2009-05-18 15:19:01 -0700825};
826
827/**
828 * emac_update_phystatus: Update Phy status
829 * @priv: The DaVinci EMAC private adapter structure
830 *
831 * Updates phy status and takes action for network queue if required
832 * based upon link status
833 *
834 */
835static void emac_update_phystatus(struct emac_priv *priv)
836{
837 u32 mac_control;
838 u32 new_duplex;
839 u32 cur_duplex;
840 struct net_device *ndev = priv->ndev;
841
842 mac_control = emac_read(EMAC_MACCONTROL);
843 cur_duplex = (mac_control & EMAC_MACCONTROL_FULLDUPLEXEN) ?
844 DUPLEX_FULL : DUPLEX_HALF;
Cyril Chemparathy5d69e002010-09-15 10:11:24 -0400845 if (priv->phydev)
Anant Golea6286ee2009-05-18 15:19:01 -0700846 new_duplex = priv->phydev->duplex;
847 else
848 new_duplex = DUPLEX_FULL;
849
850 /* We get called only if link has changed (speed/duplex/status) */
851 if ((priv->link) && (new_duplex != cur_duplex)) {
852 priv->duplex = new_duplex;
853 if (DUPLEX_FULL == priv->duplex)
854 mac_control |= (EMAC_MACCONTROL_FULLDUPLEXEN);
855 else
856 mac_control &= ~(EMAC_MACCONTROL_FULLDUPLEXEN);
857 }
858
859 if (priv->speed == SPEED_1000 && (priv->version == EMAC_VERSION_2)) {
860 mac_control = emac_read(EMAC_MACCONTROL);
chaithrika@ti.com69ef9692009-10-01 10:25:19 +0000861 mac_control |= (EMAC_DM646X_MACCONTORL_GIG |
Anant Golea6286ee2009-05-18 15:19:01 -0700862 EMAC_DM646X_MACCONTORL_GIGFORCE);
863 } else {
864 /* Clear the GIG bit and GIGFORCE bit */
865 mac_control &= ~(EMAC_DM646X_MACCONTORL_GIGFORCE |
866 EMAC_DM646X_MACCONTORL_GIG);
867
868 if (priv->rmii_en && (priv->speed == SPEED_100))
869 mac_control |= EMAC_MACCONTROL_RMIISPEED_MASK;
870 else
871 mac_control &= ~EMAC_MACCONTROL_RMIISPEED_MASK;
872 }
873
874 /* Update mac_control if changed */
875 emac_write(EMAC_MACCONTROL, mac_control);
876
877 if (priv->link) {
878 /* link ON */
879 if (!netif_carrier_ok(ndev))
880 netif_carrier_on(ndev);
881 /* reactivate the transmit queue if it is stopped */
882 if (netif_running(ndev) && netif_queue_stopped(ndev))
883 netif_wake_queue(ndev);
884 } else {
885 /* link OFF */
886 if (netif_carrier_ok(ndev))
887 netif_carrier_off(ndev);
888 if (!netif_queue_stopped(ndev))
889 netif_stop_queue(ndev);
890 }
891}
892
893/**
894 * hash_get: Calculate hash value from mac address
895 * @addr: mac address to delete from hash table
896 *
897 * Calculates hash value from mac address
898 *
899 */
900static u32 hash_get(u8 *addr)
901{
902 u32 hash;
903 u8 tmpval;
904 int cnt;
905 hash = 0;
906
907 for (cnt = 0; cnt < 2; cnt++) {
908 tmpval = *addr++;
909 hash ^= (tmpval >> 2) ^ (tmpval << 4);
910 tmpval = *addr++;
911 hash ^= (tmpval >> 4) ^ (tmpval << 2);
912 tmpval = *addr++;
913 hash ^= (tmpval >> 6) ^ (tmpval);
914 }
915
916 return hash & 0x3F;
917}
918
919/**
920 * hash_add: Hash function to add mac addr from hash table
921 * @priv: The DaVinci EMAC private adapter structure
922 * mac_addr: mac address to delete from hash table
923 *
924 * Adds mac address to the internal hash table
925 *
926 */
927static int hash_add(struct emac_priv *priv, u8 *mac_addr)
928{
929 struct device *emac_dev = &priv->ndev->dev;
930 u32 rc = 0;
931 u32 hash_bit;
932 u32 hash_value = hash_get(mac_addr);
933
934 if (hash_value >= EMAC_NUM_MULTICAST_BITS) {
935 if (netif_msg_drv(priv)) {
936 dev_err(emac_dev, "DaVinci EMAC: hash_add(): Invalid "\
937 "Hash %08x, should not be greater than %08x",
938 hash_value, (EMAC_NUM_MULTICAST_BITS - 1));
939 }
940 return -1;
941 }
942
943 /* set the hash bit only if not previously set */
944 if (priv->multicast_hash_cnt[hash_value] == 0) {
945 rc = 1; /* hash value changed */
946 if (hash_value < 32) {
947 hash_bit = BIT(hash_value);
948 priv->mac_hash1 |= hash_bit;
949 } else {
950 hash_bit = BIT((hash_value - 32));
951 priv->mac_hash2 |= hash_bit;
952 }
953 }
954
955 /* incr counter for num of mcast addr's mapped to "this" hash bit */
956 ++priv->multicast_hash_cnt[hash_value];
957
958 return rc;
959}
960
961/**
962 * hash_del: Hash function to delete mac addr from hash table
963 * @priv: The DaVinci EMAC private adapter structure
964 * mac_addr: mac address to delete from hash table
965 *
966 * Removes mac address from the internal hash table
967 *
968 */
969static int hash_del(struct emac_priv *priv, u8 *mac_addr)
970{
971 u32 hash_value;
972 u32 hash_bit;
973
974 hash_value = hash_get(mac_addr);
975 if (priv->multicast_hash_cnt[hash_value] > 0) {
976 /* dec cntr for num of mcast addr's mapped to this hash bit */
977 --priv->multicast_hash_cnt[hash_value];
978 }
979
980 /* if counter still > 0, at least one multicast address refers
981 * to this hash bit. so return 0 */
982 if (priv->multicast_hash_cnt[hash_value] > 0)
983 return 0;
984
985 if (hash_value < 32) {
986 hash_bit = BIT(hash_value);
987 priv->mac_hash1 &= ~hash_bit;
988 } else {
989 hash_bit = BIT((hash_value - 32));
990 priv->mac_hash2 &= ~hash_bit;
991 }
992
993 /* return 1 to indicate change in mac_hash registers reqd */
994 return 1;
995}
996
997/* EMAC multicast operation */
998#define EMAC_MULTICAST_ADD 0
999#define EMAC_MULTICAST_DEL 1
1000#define EMAC_ALL_MULTI_SET 2
1001#define EMAC_ALL_MULTI_CLR 3
1002
1003/**
1004 * emac_add_mcast: Set multicast address in the EMAC adapter (Internal)
1005 * @priv: The DaVinci EMAC private adapter structure
1006 * @action: multicast operation to perform
1007 * mac_addr: mac address to set
1008 *
1009 * Set multicast addresses in EMAC adapter - internal function
1010 *
1011 */
1012static void emac_add_mcast(struct emac_priv *priv, u32 action, u8 *mac_addr)
1013{
1014 struct device *emac_dev = &priv->ndev->dev;
1015 int update = -1;
1016
1017 switch (action) {
1018 case EMAC_MULTICAST_ADD:
1019 update = hash_add(priv, mac_addr);
1020 break;
1021 case EMAC_MULTICAST_DEL:
1022 update = hash_del(priv, mac_addr);
1023 break;
1024 case EMAC_ALL_MULTI_SET:
1025 update = 1;
1026 priv->mac_hash1 = EMAC_ALL_MULTI_REG_VALUE;
1027 priv->mac_hash2 = EMAC_ALL_MULTI_REG_VALUE;
1028 break;
1029 case EMAC_ALL_MULTI_CLR:
1030 update = 1;
1031 priv->mac_hash1 = 0;
1032 priv->mac_hash2 = 0;
1033 memset(&(priv->multicast_hash_cnt[0]), 0,
1034 sizeof(priv->multicast_hash_cnt[0]) *
1035 EMAC_NUM_MULTICAST_BITS);
1036 break;
1037 default:
1038 if (netif_msg_drv(priv))
1039 dev_err(emac_dev, "DaVinci EMAC: add_mcast"\
1040 ": bad operation %d", action);
1041 break;
1042 }
1043
1044 /* write to the hardware only if the register status chances */
1045 if (update > 0) {
1046 emac_write(EMAC_MACHASH1, priv->mac_hash1);
1047 emac_write(EMAC_MACHASH2, priv->mac_hash2);
1048 }
1049}
1050
1051/**
1052 * emac_dev_mcast_set: Set multicast address in the EMAC adapter
1053 * @ndev: The DaVinci EMAC network adapter
1054 *
1055 * Set multicast addresses in EMAC adapter
1056 *
1057 */
1058static void emac_dev_mcast_set(struct net_device *ndev)
1059{
1060 u32 mbp_enable;
1061 struct emac_priv *priv = netdev_priv(ndev);
1062
1063 mbp_enable = emac_read(EMAC_RXMBPENABLE);
1064 if (ndev->flags & IFF_PROMISC) {
1065 mbp_enable &= (~EMAC_MBP_PROMISCCH(EMAC_DEF_PROM_CH));
1066 mbp_enable |= (EMAC_MBP_RXPROMISC);
1067 } else {
1068 mbp_enable = (mbp_enable & ~EMAC_MBP_RXPROMISC);
1069 if ((ndev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001070 netdev_mc_count(ndev) > EMAC_DEF_MAX_MULTICAST_ADDRESSES) {
Anant Golea6286ee2009-05-18 15:19:01 -07001071 mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
1072 emac_add_mcast(priv, EMAC_ALL_MULTI_SET, NULL);
1073 }
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001074 if (!netdev_mc_empty(ndev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001075 struct netdev_hw_addr *ha;
1076
Anant Golea6286ee2009-05-18 15:19:01 -07001077 mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
1078 emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
1079 /* program multicast address list into EMAC hardware */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001080 netdev_for_each_mc_addr(ha, ndev) {
Anant Golea6286ee2009-05-18 15:19:01 -07001081 emac_add_mcast(priv, EMAC_MULTICAST_ADD,
Jiri Pirko22bedad32010-04-01 21:22:57 +00001082 (u8 *) ha->addr);
Anant Golea6286ee2009-05-18 15:19:01 -07001083 }
1084 } else {
1085 mbp_enable = (mbp_enable & ~EMAC_MBP_RXMCAST);
1086 emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
1087 }
1088 }
1089 /* Set mbp config register */
1090 emac_write(EMAC_RXMBPENABLE, mbp_enable);
1091}
1092
1093/*************************************************************************
1094 * EMAC Hardware manipulation
1095 *************************************************************************/
1096
1097/**
1098 * emac_int_disable: Disable EMAC module interrupt (from adapter)
1099 * @priv: The DaVinci EMAC private adapter structure
1100 *
1101 * Disable EMAC interrupt on the adapter
1102 *
1103 */
1104static void emac_int_disable(struct emac_priv *priv)
1105{
1106 if (priv->version == EMAC_VERSION_2) {
1107 unsigned long flags;
1108
1109 local_irq_save(flags);
1110
1111 /* Program C0_Int_En to zero to turn off
1112 * interrupts to the CPU */
1113 emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0x0);
1114 emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0x0);
1115 /* NOTE: Rx Threshold and Misc interrupts are not disabled */
Sriramakrishnan01a9af32009-11-19 15:58:26 +05301116 if (priv->int_disable)
1117 priv->int_disable();
Anant Golea6286ee2009-05-18 15:19:01 -07001118
1119 local_irq_restore(flags);
1120
1121 } else {
1122 /* Set DM644x control registers for interrupt control */
1123 emac_ctrl_write(EMAC_CTRL_EWCTL, 0x0);
1124 }
1125}
1126
1127/**
1128 * emac_int_enable: Enable EMAC module interrupt (from adapter)
1129 * @priv: The DaVinci EMAC private adapter structure
1130 *
1131 * Enable EMAC interrupt on the adapter
1132 *
1133 */
1134static void emac_int_enable(struct emac_priv *priv)
1135{
1136 if (priv->version == EMAC_VERSION_2) {
Sriramakrishnan01a9af32009-11-19 15:58:26 +05301137 if (priv->int_enable)
1138 priv->int_enable();
1139
Anant Golea6286ee2009-05-18 15:19:01 -07001140 emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0xff);
1141 emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0xff);
1142
1143 /* In addition to turning on interrupt Enable, we need
1144 * ack by writing appropriate values to the EOI
1145 * register */
1146
1147 /* NOTE: Rx Threshold and Misc interrupts are not enabled */
1148
1149 /* ack rxen only then a new pulse will be generated */
1150 emac_write(EMAC_DM646X_MACEOIVECTOR,
1151 EMAC_DM646X_MAC_EOI_C0_RXEN);
1152
1153 /* ack txen- only then a new pulse will be generated */
1154 emac_write(EMAC_DM646X_MACEOIVECTOR,
1155 EMAC_DM646X_MAC_EOI_C0_TXEN);
1156
1157 } else {
1158 /* Set DM644x control registers for interrupt control */
1159 emac_ctrl_write(EMAC_CTRL_EWCTL, 0x1);
1160 }
1161}
1162
1163/**
1164 * emac_irq: EMAC interrupt handler
1165 * @irq: interrupt number
1166 * @dev_id: EMAC network adapter data structure ptr
1167 *
1168 * EMAC Interrupt handler - we only schedule NAPI and not process any packets
1169 * here. EVen the interrupt status is checked (TX/RX/Err) in NAPI poll function
1170 *
1171 * Returns interrupt handled condition
1172 */
1173static irqreturn_t emac_irq(int irq, void *dev_id)
1174{
1175 struct net_device *ndev = (struct net_device *)dev_id;
1176 struct emac_priv *priv = netdev_priv(ndev);
1177
1178 ++priv->isr_count;
1179 if (likely(netif_running(priv->ndev))) {
1180 emac_int_disable(priv);
1181 napi_schedule(&priv->napi);
1182 } else {
1183 /* we are closing down, so dont process anything */
1184 }
1185 return IRQ_HANDLED;
1186}
1187
1188/** EMAC on-chip buffer descriptor memory
1189 *
1190 * WARNING: Please note that the on chip memory is used for both TX and RX
1191 * buffer descriptor queues and is equally divided between TX and RX desc's
1192 * If the number of TX or RX descriptors change this memory pointers need
1193 * to be adjusted. If external memory is allocated then these pointers can
1194 * pointer to the memory
1195 *
1196 */
1197#define EMAC_TX_BD_MEM(priv) ((priv)->emac_ctrl_ram)
1198#define EMAC_RX_BD_MEM(priv) ((priv)->emac_ctrl_ram + \
1199 (((priv)->ctrl_ram_size) >> 1))
1200
1201/**
1202 * emac_init_txch: TX channel initialization
1203 * @priv: The DaVinci EMAC private adapter structure
1204 * @ch: RX channel number
1205 *
1206 * Called during device init to setup a TX channel (allocate buffer desc
1207 * create free pool and keep ready for transmission
1208 *
1209 * Returns success(0) or mem alloc failures error code
1210 */
1211static int emac_init_txch(struct emac_priv *priv, u32 ch)
1212{
1213 struct device *emac_dev = &priv->ndev->dev;
1214 u32 cnt, bd_size;
1215 void __iomem *mem;
1216 struct emac_tx_bd __iomem *curr_bd;
1217 struct emac_txch *txch = NULL;
1218
1219 txch = kzalloc(sizeof(struct emac_txch), GFP_KERNEL);
1220 if (NULL == txch) {
1221 dev_err(emac_dev, "DaVinci EMAC: TX Ch mem alloc failed");
1222 return -ENOMEM;
1223 }
1224 priv->txch[ch] = txch;
1225 txch->service_max = EMAC_DEF_TX_MAX_SERVICE;
1226 txch->active_queue_head = NULL;
1227 txch->active_queue_tail = NULL;
1228 txch->queue_active = 0;
1229 txch->teardown_pending = 0;
1230
1231 /* allocate memory for TX CPPI channel on a 4 byte boundry */
1232 txch->tx_complete = kzalloc(txch->service_max * sizeof(u32),
1233 GFP_KERNEL);
1234 if (NULL == txch->tx_complete) {
1235 dev_err(emac_dev, "DaVinci EMAC: Tx service mem alloc failed");
1236 kfree(txch);
1237 return -ENOMEM;
1238 }
1239
1240 /* allocate buffer descriptor pool align every BD on four word
1241 * boundry for future requirements */
1242 bd_size = (sizeof(struct emac_tx_bd) + 0xF) & ~0xF;
1243 txch->num_bd = (priv->ctrl_ram_size >> 1) / bd_size;
1244 txch->alloc_size = (((bd_size * txch->num_bd) + 0xF) & ~0xF);
1245
1246 /* alloc TX BD memory */
1247 txch->bd_mem = EMAC_TX_BD_MEM(priv);
1248 __memzero((void __force *)txch->bd_mem, txch->alloc_size);
1249
1250 /* initialize the BD linked list */
1251 mem = (void __force __iomem *)
1252 (((u32 __force) txch->bd_mem + 0xF) & ~0xF);
1253 txch->bd_pool_head = NULL;
1254 for (cnt = 0; cnt < txch->num_bd; cnt++) {
1255 curr_bd = mem + (cnt * bd_size);
1256 curr_bd->next = txch->bd_pool_head;
1257 txch->bd_pool_head = curr_bd;
1258 }
1259
1260 /* reset statistics counters */
1261 txch->out_of_tx_bd = 0;
1262 txch->no_active_pkts = 0;
1263 txch->active_queue_count = 0;
1264
1265 return 0;
1266}
1267
1268/**
1269 * emac_cleanup_txch: Book-keep function to clean TX channel resources
1270 * @priv: The DaVinci EMAC private adapter structure
1271 * @ch: TX channel number
1272 *
1273 * Called to clean up TX channel resources
1274 *
1275 */
1276static void emac_cleanup_txch(struct emac_priv *priv, u32 ch)
1277{
1278 struct emac_txch *txch = priv->txch[ch];
1279
1280 if (txch) {
1281 if (txch->bd_mem)
1282 txch->bd_mem = NULL;
1283 kfree(txch->tx_complete);
1284 kfree(txch);
1285 priv->txch[ch] = NULL;
1286 }
1287}
1288
1289/**
1290 * emac_net_tx_complete: TX packet completion function
1291 * @priv: The DaVinci EMAC private adapter structure
1292 * @net_data_tokens: packet token - skb pointer
1293 * @num_tokens: number of skb's to free
1294 * @ch: TX channel number
1295 *
1296 * Frees the skb once packet is transmitted
1297 *
1298 */
1299static int emac_net_tx_complete(struct emac_priv *priv,
1300 void **net_data_tokens,
1301 int num_tokens, u32 ch)
1302{
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00001303 struct net_device *ndev = priv->ndev;
Anant Golea6286ee2009-05-18 15:19:01 -07001304 u32 cnt;
1305
Henrique Camargoeabd8ba2010-08-02 17:10:42 +00001306 if (unlikely(num_tokens && netif_queue_stopped(ndev)))
1307 netif_start_queue(ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07001308 for (cnt = 0; cnt < num_tokens; cnt++) {
1309 struct sk_buff *skb = (struct sk_buff *)net_data_tokens[cnt];
1310 if (skb == NULL)
1311 continue;
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00001312 ndev->stats.tx_packets++;
1313 ndev->stats.tx_bytes += skb->len;
Anant Golea6286ee2009-05-18 15:19:01 -07001314 dev_kfree_skb_any(skb);
1315 }
1316 return 0;
1317}
1318
1319/**
1320 * emac_txch_teardown: TX channel teardown
1321 * @priv: The DaVinci EMAC private adapter structure
1322 * @ch: TX channel number
1323 *
1324 * Called to teardown TX channel
1325 *
1326 */
1327static void emac_txch_teardown(struct emac_priv *priv, u32 ch)
1328{
1329 struct device *emac_dev = &priv->ndev->dev;
1330 u32 teardown_cnt = 0xFFFFFFF0; /* Some high value */
1331 struct emac_txch *txch = priv->txch[ch];
1332 struct emac_tx_bd __iomem *curr_bd;
1333
1334 while ((emac_read(EMAC_TXCP(ch)) & EMAC_TEARDOWN_VALUE) !=
1335 EMAC_TEARDOWN_VALUE) {
1336 /* wait till tx teardown complete */
1337 cpu_relax(); /* TODO: check if this helps ... */
1338 --teardown_cnt;
1339 if (0 == teardown_cnt) {
1340 dev_err(emac_dev, "EMAC: TX teardown aborted\n");
1341 break;
1342 }
1343 }
1344 emac_write(EMAC_TXCP(ch), EMAC_TEARDOWN_VALUE);
1345
1346 /* process sent packets and return skb's to upper layer */
1347 if (1 == txch->queue_active) {
1348 curr_bd = txch->active_queue_head;
1349 while (curr_bd != NULL) {
Sekhar Noribe5bce22010-03-09 01:20:37 +00001350 dma_unmap_single(emac_dev, curr_bd->buff_ptr,
1351 curr_bd->off_b_len & EMAC_RX_BD_BUF_SIZE,
1352 DMA_TO_DEVICE);
1353
Anant Golea6286ee2009-05-18 15:19:01 -07001354 emac_net_tx_complete(priv, (void __force *)
1355 &curr_bd->buf_token, 1, ch);
1356 if (curr_bd != txch->active_queue_tail)
1357 curr_bd = curr_bd->next;
1358 else
1359 break;
1360 }
1361 txch->bd_pool_head = txch->active_queue_head;
1362 txch->active_queue_head =
1363 txch->active_queue_tail = NULL;
1364 }
1365}
1366
1367/**
1368 * emac_stop_txch: Stop TX channel operation
1369 * @priv: The DaVinci EMAC private adapter structure
1370 * @ch: TX channel number
1371 *
1372 * Called to stop TX channel operation
1373 *
1374 */
1375static void emac_stop_txch(struct emac_priv *priv, u32 ch)
1376{
1377 struct emac_txch *txch = priv->txch[ch];
1378
1379 if (txch) {
1380 txch->teardown_pending = 1;
1381 emac_write(EMAC_TXTEARDOWN, 0);
1382 emac_txch_teardown(priv, ch);
1383 txch->teardown_pending = 0;
1384 emac_write(EMAC_TXINTMASKCLEAR, BIT(ch));
1385 }
1386}
1387
1388/**
1389 * emac_tx_bdproc: TX buffer descriptor (packet) processing
1390 * @priv: The DaVinci EMAC private adapter structure
1391 * @ch: TX channel number to process buffer descriptors for
1392 * @budget: number of packets allowed to process
1393 * @pending: indication to caller that packets are pending to process
1394 *
1395 * Processes TX buffer descriptors after packets are transmitted - checks
1396 * ownership bit on the TX * descriptor and requeues it to free pool & frees
1397 * the SKB buffer. Only "budget" number of packets are processed and
1398 * indication of pending packets provided to the caller
1399 *
1400 * Returns number of packets processed
1401 */
1402static int emac_tx_bdproc(struct emac_priv *priv, u32 ch, u32 budget)
1403{
1404 struct device *emac_dev = &priv->ndev->dev;
1405 unsigned long flags;
1406 u32 frame_status;
1407 u32 pkts_processed = 0;
1408 u32 tx_complete_cnt = 0;
1409 struct emac_tx_bd __iomem *curr_bd;
1410 struct emac_txch *txch = priv->txch[ch];
1411 u32 *tx_complete_ptr = txch->tx_complete;
1412
1413 if (unlikely(1 == txch->teardown_pending)) {
1414 if (netif_msg_tx_err(priv) && net_ratelimit()) {
1415 dev_err(emac_dev, "DaVinci EMAC:emac_tx_bdproc: "\
1416 "teardown pending\n");
1417 }
1418 return 0; /* dont handle any pkt completions */
1419 }
1420
1421 ++txch->proc_count;
1422 spin_lock_irqsave(&priv->tx_lock, flags);
1423 curr_bd = txch->active_queue_head;
1424 if (NULL == curr_bd) {
1425 emac_write(EMAC_TXCP(ch),
Sriramakrishnanad021ae2009-11-19 15:58:27 +05301426 emac_virt_to_phys(txch->last_hw_bdprocessed, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07001427 txch->no_active_pkts++;
1428 spin_unlock_irqrestore(&priv->tx_lock, flags);
1429 return 0;
1430 }
1431 BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1432 frame_status = curr_bd->mode;
1433 while ((curr_bd) &&
1434 ((frame_status & EMAC_CPPI_OWNERSHIP_BIT) == 0) &&
1435 (pkts_processed < budget)) {
Sriramakrishnanad021ae2009-11-19 15:58:27 +05301436 emac_write(EMAC_TXCP(ch), emac_virt_to_phys(curr_bd, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07001437 txch->active_queue_head = curr_bd->next;
1438 if (frame_status & EMAC_CPPI_EOQ_BIT) {
1439 if (curr_bd->next) { /* misqueued packet */
1440 emac_write(EMAC_TXHDP(ch), curr_bd->h_next);
1441 ++txch->mis_queued_packets;
1442 } else {
1443 txch->queue_active = 0; /* end of queue */
1444 }
1445 }
Sekhar Noribe5bce22010-03-09 01:20:37 +00001446
1447 dma_unmap_single(emac_dev, curr_bd->buff_ptr,
1448 curr_bd->off_b_len & EMAC_RX_BD_BUF_SIZE,
1449 DMA_TO_DEVICE);
1450
Anant Golea6286ee2009-05-18 15:19:01 -07001451 *tx_complete_ptr = (u32) curr_bd->buf_token;
1452 ++tx_complete_ptr;
1453 ++tx_complete_cnt;
1454 curr_bd->next = txch->bd_pool_head;
1455 txch->bd_pool_head = curr_bd;
1456 --txch->active_queue_count;
1457 pkts_processed++;
1458 txch->last_hw_bdprocessed = curr_bd;
1459 curr_bd = txch->active_queue_head;
1460 if (curr_bd) {
1461 BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1462 frame_status = curr_bd->mode;
1463 }
1464 } /* end of pkt processing loop */
1465
1466 emac_net_tx_complete(priv,
1467 (void *)&txch->tx_complete[0],
1468 tx_complete_cnt, ch);
1469 spin_unlock_irqrestore(&priv->tx_lock, flags);
1470 return pkts_processed;
1471}
1472
1473#define EMAC_ERR_TX_OUT_OF_BD -1
1474
1475/**
1476 * emac_send: EMAC Transmit function (internal)
1477 * @priv: The DaVinci EMAC private adapter structure
1478 * @pkt: packet pointer (contains skb ptr)
1479 * @ch: TX channel number
1480 *
1481 * Called by the transmit function to queue the packet in EMAC hardware queue
1482 *
1483 * Returns success(0) or error code (typically out of desc's)
1484 */
1485static int emac_send(struct emac_priv *priv, struct emac_netpktobj *pkt, u32 ch)
1486{
1487 unsigned long flags;
1488 struct emac_tx_bd __iomem *curr_bd;
1489 struct emac_txch *txch;
1490 struct emac_netbufobj *buf_list;
1491
1492 txch = priv->txch[ch];
1493 buf_list = pkt->buf_list; /* get handle to the buffer array */
1494
1495 /* check packet size and pad if short */
1496 if (pkt->pkt_length < EMAC_DEF_MIN_ETHPKTSIZE) {
1497 buf_list->length += (EMAC_DEF_MIN_ETHPKTSIZE - pkt->pkt_length);
1498 pkt->pkt_length = EMAC_DEF_MIN_ETHPKTSIZE;
1499 }
1500
1501 spin_lock_irqsave(&priv->tx_lock, flags);
1502 curr_bd = txch->bd_pool_head;
1503 if (curr_bd == NULL) {
1504 txch->out_of_tx_bd++;
1505 spin_unlock_irqrestore(&priv->tx_lock, flags);
1506 return EMAC_ERR_TX_OUT_OF_BD;
1507 }
1508
1509 txch->bd_pool_head = curr_bd->next;
1510 curr_bd->buf_token = buf_list->buf_token;
Sekhar Noribe5bce22010-03-09 01:20:37 +00001511 curr_bd->buff_ptr = dma_map_single(&priv->ndev->dev, buf_list->data_ptr,
1512 buf_list->length, DMA_TO_DEVICE);
Anant Golea6286ee2009-05-18 15:19:01 -07001513 curr_bd->off_b_len = buf_list->length;
1514 curr_bd->h_next = 0;
1515 curr_bd->next = NULL;
1516 curr_bd->mode = (EMAC_CPPI_SOP_BIT | EMAC_CPPI_OWNERSHIP_BIT |
1517 EMAC_CPPI_EOP_BIT | pkt->pkt_length);
1518
1519 /* flush the packet from cache if write back cache is present */
1520 BD_CACHE_WRITEBACK_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
1521
1522 /* send the packet */
1523 if (txch->active_queue_head == NULL) {
1524 txch->active_queue_head = curr_bd;
1525 txch->active_queue_tail = curr_bd;
1526 if (1 != txch->queue_active) {
1527 emac_write(EMAC_TXHDP(ch),
Sriramakrishnanad021ae2009-11-19 15:58:27 +05301528 emac_virt_to_phys(curr_bd, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07001529 txch->queue_active = 1;
1530 }
1531 ++txch->queue_reinit;
1532 } else {
1533 register struct emac_tx_bd __iomem *tail_bd;
1534 register u32 frame_status;
1535
1536 tail_bd = txch->active_queue_tail;
1537 tail_bd->next = curr_bd;
1538 txch->active_queue_tail = curr_bd;
1539 tail_bd = EMAC_VIRT_NOCACHE(tail_bd);
Sriramakrishnanad021ae2009-11-19 15:58:27 +05301540 tail_bd->h_next = (int)emac_virt_to_phys(curr_bd, priv);
Anant Golea6286ee2009-05-18 15:19:01 -07001541 frame_status = tail_bd->mode;
1542 if (frame_status & EMAC_CPPI_EOQ_BIT) {
Sriramakrishnanad021ae2009-11-19 15:58:27 +05301543 emac_write(EMAC_TXHDP(ch),
1544 emac_virt_to_phys(curr_bd, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07001545 frame_status &= ~(EMAC_CPPI_EOQ_BIT);
1546 tail_bd->mode = frame_status;
1547 ++txch->end_of_queue_add;
1548 }
1549 }
1550 txch->active_queue_count++;
1551 spin_unlock_irqrestore(&priv->tx_lock, flags);
1552 return 0;
1553}
1554
1555/**
1556 * emac_dev_xmit: EMAC Transmit function
1557 * @skb: SKB pointer
1558 * @ndev: The DaVinci EMAC network adapter
1559 *
1560 * Called by the system to transmit a packet - we queue the packet in
1561 * EMAC hardware transmit queue
1562 *
1563 * Returns success(NETDEV_TX_OK) or error code (typically out of desc's)
1564 */
1565static int emac_dev_xmit(struct sk_buff *skb, struct net_device *ndev)
1566{
1567 struct device *emac_dev = &ndev->dev;
1568 int ret_code;
1569 struct emac_netbufobj tx_buf; /* buffer obj-only single frame support */
1570 struct emac_netpktobj tx_packet; /* packet object */
1571 struct emac_priv *priv = netdev_priv(ndev);
1572
1573 /* If no link, return */
1574 if (unlikely(!priv->link)) {
1575 if (netif_msg_tx_err(priv) && net_ratelimit())
1576 dev_err(emac_dev, "DaVinci EMAC: No link to transmit");
1577 return NETDEV_TX_BUSY;
1578 }
1579
1580 /* Build the buffer and packet objects - Since only single fragment is
1581 * supported, need not set length and token in both packet & object.
1582 * Doing so for completeness sake & to show that this needs to be done
1583 * in multifragment case
1584 */
1585 tx_packet.buf_list = &tx_buf;
1586 tx_packet.num_bufs = 1; /* only single fragment supported */
1587 tx_packet.pkt_length = skb->len;
1588 tx_packet.pkt_token = (void *)skb;
1589 tx_buf.length = skb->len;
1590 tx_buf.buf_token = (void *)skb;
1591 tx_buf.data_ptr = skb->data;
Anant Golea6286ee2009-05-18 15:19:01 -07001592 ret_code = emac_send(priv, &tx_packet, EMAC_DEF_TX_CH);
1593 if (unlikely(ret_code != 0)) {
1594 if (ret_code == EMAC_ERR_TX_OUT_OF_BD) {
1595 if (netif_msg_tx_err(priv) && net_ratelimit())
1596 dev_err(emac_dev, "DaVinci EMAC: xmit() fatal"\
1597 " err. Out of TX BD's");
1598 netif_stop_queue(priv->ndev);
1599 }
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00001600 ndev->stats.tx_dropped++;
Anant Golea6286ee2009-05-18 15:19:01 -07001601 return NETDEV_TX_BUSY;
1602 }
1603
1604 return NETDEV_TX_OK;
1605}
1606
1607/**
1608 * emac_dev_tx_timeout: EMAC Transmit timeout function
1609 * @ndev: The DaVinci EMAC network adapter
1610 *
1611 * Called when system detects that a skb timeout period has expired
1612 * potentially due to a fault in the adapter in not being able to send
1613 * it out on the wire. We teardown the TX channel assuming a hardware
1614 * error and re-initialize the TX channel for hardware operation
1615 *
1616 */
1617static void emac_dev_tx_timeout(struct net_device *ndev)
1618{
1619 struct emac_priv *priv = netdev_priv(ndev);
1620 struct device *emac_dev = &ndev->dev;
1621
1622 if (netif_msg_tx_err(priv))
1623 dev_err(emac_dev, "DaVinci EMAC: xmit timeout, restarting TX");
1624
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00001625 ndev->stats.tx_errors++;
Anant Golea6286ee2009-05-18 15:19:01 -07001626 emac_int_disable(priv);
1627 emac_stop_txch(priv, EMAC_DEF_TX_CH);
1628 emac_cleanup_txch(priv, EMAC_DEF_TX_CH);
1629 emac_init_txch(priv, EMAC_DEF_TX_CH);
1630 emac_write(EMAC_TXHDP(0), 0);
1631 emac_write(EMAC_TXINTMASKSET, BIT(EMAC_DEF_TX_CH));
1632 emac_int_enable(priv);
1633}
1634
1635/**
1636 * emac_net_alloc_rx_buf: Allocate a skb for RX
1637 * @priv: The DaVinci EMAC private adapter structure
1638 * @buf_size: size of SKB data buffer to allocate
1639 * @data_token: data token returned (skb handle for storing in buffer desc)
1640 * @ch: RX channel number
1641 *
1642 * Called during RX channel setup - allocates skb buffer of required size
1643 * and provides the skb handle and allocated buffer data pointer to caller
1644 *
1645 * Returns skb data pointer or 0 on failure to alloc skb
1646 */
1647static void *emac_net_alloc_rx_buf(struct emac_priv *priv, int buf_size,
1648 void **data_token, u32 ch)
1649{
1650 struct net_device *ndev = priv->ndev;
1651 struct device *emac_dev = &ndev->dev;
1652 struct sk_buff *p_skb;
1653
1654 p_skb = dev_alloc_skb(buf_size);
1655 if (unlikely(NULL == p_skb)) {
1656 if (netif_msg_rx_err(priv) && net_ratelimit())
1657 dev_err(emac_dev, "DaVinci EMAC: failed to alloc skb");
1658 return NULL;
1659 }
1660
1661 /* set device pointer in skb and reserve space for extra bytes */
1662 p_skb->dev = ndev;
1663 skb_reserve(p_skb, NET_IP_ALIGN);
1664 *data_token = (void *) p_skb;
Anant Golea6286ee2009-05-18 15:19:01 -07001665 return p_skb->data;
1666}
1667
1668/**
1669 * emac_init_rxch: RX channel initialization
1670 * @priv: The DaVinci EMAC private adapter structure
1671 * @ch: RX channel number
1672 * @param: mac address for RX channel
1673 *
1674 * Called during device init to setup a RX channel (allocate buffers and
1675 * buffer descriptors, create queue and keep ready for reception
1676 *
1677 * Returns success(0) or mem alloc failures error code
1678 */
1679static int emac_init_rxch(struct emac_priv *priv, u32 ch, char *param)
1680{
1681 struct device *emac_dev = &priv->ndev->dev;
1682 u32 cnt, bd_size;
1683 void __iomem *mem;
1684 struct emac_rx_bd __iomem *curr_bd;
1685 struct emac_rxch *rxch = NULL;
1686
1687 rxch = kzalloc(sizeof(struct emac_rxch), GFP_KERNEL);
1688 if (NULL == rxch) {
1689 dev_err(emac_dev, "DaVinci EMAC: RX Ch mem alloc failed");
1690 return -ENOMEM;
1691 }
1692 priv->rxch[ch] = rxch;
1693 rxch->buf_size = priv->rx_buf_size;
1694 rxch->service_max = EMAC_DEF_RX_MAX_SERVICE;
1695 rxch->queue_active = 0;
1696 rxch->teardown_pending = 0;
1697
1698 /* save mac address */
1699 for (cnt = 0; cnt < 6; cnt++)
1700 rxch->mac_addr[cnt] = param[cnt];
1701
1702 /* allocate buffer descriptor pool align every BD on four word
1703 * boundry for future requirements */
1704 bd_size = (sizeof(struct emac_rx_bd) + 0xF) & ~0xF;
1705 rxch->num_bd = (priv->ctrl_ram_size >> 1) / bd_size;
1706 rxch->alloc_size = (((bd_size * rxch->num_bd) + 0xF) & ~0xF);
1707 rxch->bd_mem = EMAC_RX_BD_MEM(priv);
1708 __memzero((void __force *)rxch->bd_mem, rxch->alloc_size);
1709 rxch->pkt_queue.buf_list = &rxch->buf_queue;
1710
1711 /* allocate RX buffer and initialize the BD linked list */
1712 mem = (void __force __iomem *)
1713 (((u32 __force) rxch->bd_mem + 0xF) & ~0xF);
1714 rxch->active_queue_head = NULL;
1715 rxch->active_queue_tail = mem;
1716 for (cnt = 0; cnt < rxch->num_bd; cnt++) {
1717 curr_bd = mem + (cnt * bd_size);
1718 /* for future use the last parameter contains the BD ptr */
1719 curr_bd->data_ptr = emac_net_alloc_rx_buf(priv,
1720 rxch->buf_size,
1721 (void __force **)&curr_bd->buf_token,
1722 EMAC_DEF_RX_CH);
1723 if (curr_bd->data_ptr == NULL) {
1724 dev_err(emac_dev, "DaVinci EMAC: RX buf mem alloc " \
1725 "failed for ch %d\n", ch);
1726 kfree(rxch);
1727 return -ENOMEM;
1728 }
1729
1730 /* populate the hardware descriptor */
Sriramakrishnanad021ae2009-11-19 15:58:27 +05301731 curr_bd->h_next = emac_virt_to_phys(rxch->active_queue_head,
1732 priv);
Sekhar Noribe5bce22010-03-09 01:20:37 +00001733 curr_bd->buff_ptr = dma_map_single(emac_dev, curr_bd->data_ptr,
1734 rxch->buf_size, DMA_FROM_DEVICE);
Anant Golea6286ee2009-05-18 15:19:01 -07001735 curr_bd->off_b_len = rxch->buf_size;
1736 curr_bd->mode = EMAC_CPPI_OWNERSHIP_BIT;
1737
1738 /* write back to hardware memory */
1739 BD_CACHE_WRITEBACK_INVALIDATE((u32) curr_bd,
1740 EMAC_BD_LENGTH_FOR_CACHE);
1741 curr_bd->next = rxch->active_queue_head;
1742 rxch->active_queue_head = curr_bd;
1743 }
1744
1745 /* At this point rxCppi->activeQueueHead points to the first
1746 RX BD ready to be given to RX HDP and rxch->active_queue_tail
1747 points to the last RX BD
1748 */
1749 return 0;
1750}
1751
1752/**
1753 * emac_rxch_teardown: RX channel teardown
1754 * @priv: The DaVinci EMAC private adapter structure
1755 * @ch: RX channel number
1756 *
1757 * Called during device stop to teardown RX channel
1758 *
1759 */
1760static void emac_rxch_teardown(struct emac_priv *priv, u32 ch)
1761{
1762 struct device *emac_dev = &priv->ndev->dev;
1763 u32 teardown_cnt = 0xFFFFFFF0; /* Some high value */
1764
1765 while ((emac_read(EMAC_RXCP(ch)) & EMAC_TEARDOWN_VALUE) !=
1766 EMAC_TEARDOWN_VALUE) {
1767 /* wait till tx teardown complete */
1768 cpu_relax(); /* TODO: check if this helps ... */
1769 --teardown_cnt;
1770 if (0 == teardown_cnt) {
1771 dev_err(emac_dev, "EMAC: RX teardown aborted\n");
1772 break;
1773 }
1774 }
1775 emac_write(EMAC_RXCP(ch), EMAC_TEARDOWN_VALUE);
1776}
1777
1778/**
1779 * emac_stop_rxch: Stop RX channel operation
1780 * @priv: The DaVinci EMAC private adapter structure
1781 * @ch: RX channel number
1782 *
1783 * Called during device stop to stop RX channel operation
1784 *
1785 */
1786static void emac_stop_rxch(struct emac_priv *priv, u32 ch)
1787{
1788 struct emac_rxch *rxch = priv->rxch[ch];
1789
1790 if (rxch) {
1791 rxch->teardown_pending = 1;
1792 emac_write(EMAC_RXTEARDOWN, ch);
1793 /* wait for teardown complete */
1794 emac_rxch_teardown(priv, ch);
1795 rxch->teardown_pending = 0;
1796 emac_write(EMAC_RXINTMASKCLEAR, BIT(ch));
1797 }
1798}
1799
1800/**
1801 * emac_cleanup_rxch: Book-keep function to clean RX channel resources
1802 * @priv: The DaVinci EMAC private adapter structure
1803 * @ch: RX channel number
1804 *
1805 * Called during device stop to clean up RX channel resources
1806 *
1807 */
1808static void emac_cleanup_rxch(struct emac_priv *priv, u32 ch)
1809{
1810 struct emac_rxch *rxch = priv->rxch[ch];
1811 struct emac_rx_bd __iomem *curr_bd;
1812
1813 if (rxch) {
1814 /* free the receive buffers previously allocated */
1815 curr_bd = rxch->active_queue_head;
1816 while (curr_bd) {
1817 if (curr_bd->buf_token) {
Sekhar Noribe5bce22010-03-09 01:20:37 +00001818 dma_unmap_single(&priv->ndev->dev,
1819 curr_bd->buff_ptr,
1820 curr_bd->off_b_len
1821 & EMAC_RX_BD_BUF_SIZE,
1822 DMA_FROM_DEVICE);
1823
Anant Golea6286ee2009-05-18 15:19:01 -07001824 dev_kfree_skb_any((struct sk_buff *)\
1825 curr_bd->buf_token);
1826 }
1827 curr_bd = curr_bd->next;
1828 }
1829 if (rxch->bd_mem)
1830 rxch->bd_mem = NULL;
1831 kfree(rxch);
1832 priv->rxch[ch] = NULL;
1833 }
1834}
1835
1836/**
1837 * emac_set_type0addr: Set EMAC Type0 mac address
1838 * @priv: The DaVinci EMAC private adapter structure
1839 * @ch: RX channel number
1840 * @mac_addr: MAC address to set in device
1841 *
1842 * Called internally to set Type0 mac address of the adapter (Device)
1843 *
1844 * Returns success (0) or appropriate error code (none as of now)
1845 */
1846static void emac_set_type0addr(struct emac_priv *priv, u32 ch, char *mac_addr)
1847{
1848 u32 val;
1849 val = ((mac_addr[5] << 8) | (mac_addr[4]));
1850 emac_write(EMAC_MACSRCADDRLO, val);
1851
1852 val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1853 (mac_addr[1] << 8) | (mac_addr[0]));
1854 emac_write(EMAC_MACSRCADDRHI, val);
1855 val = emac_read(EMAC_RXUNICASTSET);
1856 val |= BIT(ch);
1857 emac_write(EMAC_RXUNICASTSET, val);
1858 val = emac_read(EMAC_RXUNICASTCLEAR);
1859 val &= ~BIT(ch);
1860 emac_write(EMAC_RXUNICASTCLEAR, val);
1861}
1862
1863/**
1864 * emac_set_type1addr: Set EMAC Type1 mac address
1865 * @priv: The DaVinci EMAC private adapter structure
1866 * @ch: RX channel number
1867 * @mac_addr: MAC address to set in device
1868 *
1869 * Called internally to set Type1 mac address of the adapter (Device)
1870 *
1871 * Returns success (0) or appropriate error code (none as of now)
1872 */
1873static void emac_set_type1addr(struct emac_priv *priv, u32 ch, char *mac_addr)
1874{
1875 u32 val;
1876 emac_write(EMAC_MACINDEX, ch);
1877 val = ((mac_addr[5] << 8) | mac_addr[4]);
1878 emac_write(EMAC_MACADDRLO, val);
1879 val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1880 (mac_addr[1] << 8) | (mac_addr[0]));
1881 emac_write(EMAC_MACADDRHI, val);
1882 emac_set_type0addr(priv, ch, mac_addr);
1883}
1884
1885/**
1886 * emac_set_type2addr: Set EMAC Type2 mac address
1887 * @priv: The DaVinci EMAC private adapter structure
1888 * @ch: RX channel number
1889 * @mac_addr: MAC address to set in device
1890 * @index: index into RX address entries
1891 * @match: match parameter for RX address matching logic
1892 *
1893 * Called internally to set Type2 mac address of the adapter (Device)
1894 *
1895 * Returns success (0) or appropriate error code (none as of now)
1896 */
1897static void emac_set_type2addr(struct emac_priv *priv, u32 ch,
1898 char *mac_addr, int index, int match)
1899{
1900 u32 val;
1901 emac_write(EMAC_MACINDEX, index);
1902 val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1903 (mac_addr[1] << 8) | (mac_addr[0]));
1904 emac_write(EMAC_MACADDRHI, val);
1905 val = ((mac_addr[5] << 8) | mac_addr[4] | ((ch & 0x7) << 16) | \
1906 (match << 19) | BIT(20));
1907 emac_write(EMAC_MACADDRLO, val);
1908 emac_set_type0addr(priv, ch, mac_addr);
1909}
1910
1911/**
1912 * emac_setmac: Set mac address in the adapter (internal function)
1913 * @priv: The DaVinci EMAC private adapter structure
1914 * @ch: RX channel number
1915 * @mac_addr: MAC address to set in device
1916 *
1917 * Called internally to set the mac address of the adapter (Device)
1918 *
1919 * Returns success (0) or appropriate error code (none as of now)
1920 */
1921static void emac_setmac(struct emac_priv *priv, u32 ch, char *mac_addr)
1922{
1923 struct device *emac_dev = &priv->ndev->dev;
1924
1925 if (priv->rx_addr_type == 0) {
1926 emac_set_type0addr(priv, ch, mac_addr);
1927 } else if (priv->rx_addr_type == 1) {
1928 u32 cnt;
1929 for (cnt = 0; cnt < EMAC_MAX_TXRX_CHANNELS; cnt++)
1930 emac_set_type1addr(priv, ch, mac_addr);
1931 } else if (priv->rx_addr_type == 2) {
1932 emac_set_type2addr(priv, ch, mac_addr, ch, 1);
1933 emac_set_type0addr(priv, ch, mac_addr);
1934 } else {
1935 if (netif_msg_drv(priv))
1936 dev_err(emac_dev, "DaVinci EMAC: Wrong addressing\n");
1937 }
1938}
1939
1940/**
1941 * emac_dev_setmac_addr: Set mac address in the adapter
1942 * @ndev: The DaVinci EMAC network adapter
1943 * @addr: MAC address to set in device
1944 *
1945 * Called by the system to set the mac address of the adapter (Device)
1946 *
1947 * Returns success (0) or appropriate error code (none as of now)
1948 */
1949static int emac_dev_setmac_addr(struct net_device *ndev, void *addr)
1950{
1951 struct emac_priv *priv = netdev_priv(ndev);
1952 struct emac_rxch *rxch = priv->rxch[EMAC_DEF_RX_CH];
1953 struct device *emac_dev = &priv->ndev->dev;
1954 struct sockaddr *sa = addr;
Anant Golea6286ee2009-05-18 15:19:01 -07001955
Pablo Bitton64c81652009-07-07 19:11:10 -07001956 if (!is_valid_ether_addr(sa->sa_data))
1957 return -EINVAL;
1958
Anant Golea6286ee2009-05-18 15:19:01 -07001959 /* Store mac addr in priv and rx channel and set it in EMAC hw */
1960 memcpy(priv->mac_addr, sa->sa_data, ndev->addr_len);
Anant Golea6286ee2009-05-18 15:19:01 -07001961 memcpy(ndev->dev_addr, sa->sa_data, ndev->addr_len);
Pablo Bitton64c81652009-07-07 19:11:10 -07001962
1963 /* If the interface is down - rxch is NULL. */
1964 /* MAC address is configured only after the interface is enabled. */
1965 if (netif_running(ndev)) {
1966 memcpy(rxch->mac_addr, sa->sa_data, ndev->addr_len);
1967 emac_setmac(priv, EMAC_DEF_RX_CH, rxch->mac_addr);
1968 }
Anant Golea6286ee2009-05-18 15:19:01 -07001969
1970 if (netif_msg_drv(priv))
Chaithrika U S5c726162009-06-03 21:54:29 -07001971 dev_notice(emac_dev, "DaVinci EMAC: emac_dev_setmac_addr %pM\n",
1972 priv->mac_addr);
Anant Golea6286ee2009-05-18 15:19:01 -07001973
1974 return 0;
1975}
1976
1977/**
1978 * emac_addbd_to_rx_queue: Recycle RX buffer descriptor
1979 * @priv: The DaVinci EMAC private adapter structure
1980 * @ch: RX channel number to process buffer descriptors for
1981 * @curr_bd: current buffer descriptor
1982 * @buffer: buffer pointer for descriptor
1983 * @buf_token: buffer token (stores skb information)
1984 *
1985 * Prepares the recycled buffer descriptor and addes it to hardware
1986 * receive queue - if queue empty this descriptor becomes the head
1987 * else addes the descriptor to end of queue
1988 *
1989 */
1990static void emac_addbd_to_rx_queue(struct emac_priv *priv, u32 ch,
1991 struct emac_rx_bd __iomem *curr_bd,
1992 char *buffer, void *buf_token)
1993{
1994 struct emac_rxch *rxch = priv->rxch[ch];
1995
1996 /* populate the hardware descriptor */
1997 curr_bd->h_next = 0;
Sekhar Noribe5bce22010-03-09 01:20:37 +00001998 curr_bd->buff_ptr = dma_map_single(&priv->ndev->dev, buffer,
1999 rxch->buf_size, DMA_FROM_DEVICE);
Anant Golea6286ee2009-05-18 15:19:01 -07002000 curr_bd->off_b_len = rxch->buf_size;
2001 curr_bd->mode = EMAC_CPPI_OWNERSHIP_BIT;
2002 curr_bd->next = NULL;
2003 curr_bd->data_ptr = buffer;
2004 curr_bd->buf_token = buf_token;
2005
2006 /* write back */
2007 BD_CACHE_WRITEBACK_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
2008 if (rxch->active_queue_head == NULL) {
2009 rxch->active_queue_head = curr_bd;
2010 rxch->active_queue_tail = curr_bd;
2011 if (0 != rxch->queue_active) {
2012 emac_write(EMAC_RXHDP(ch),
Sriramakrishnanad021ae2009-11-19 15:58:27 +05302013 emac_virt_to_phys(rxch->active_queue_head, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07002014 rxch->queue_active = 1;
2015 }
2016 } else {
2017 struct emac_rx_bd __iomem *tail_bd;
2018 u32 frame_status;
2019
2020 tail_bd = rxch->active_queue_tail;
2021 rxch->active_queue_tail = curr_bd;
2022 tail_bd->next = curr_bd;
2023 tail_bd = EMAC_VIRT_NOCACHE(tail_bd);
Sriramakrishnanad021ae2009-11-19 15:58:27 +05302024 tail_bd->h_next = emac_virt_to_phys(curr_bd, priv);
Anant Golea6286ee2009-05-18 15:19:01 -07002025 frame_status = tail_bd->mode;
2026 if (frame_status & EMAC_CPPI_EOQ_BIT) {
2027 emac_write(EMAC_RXHDP(ch),
Sriramakrishnanad021ae2009-11-19 15:58:27 +05302028 emac_virt_to_phys(curr_bd, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07002029 frame_status &= ~(EMAC_CPPI_EOQ_BIT);
2030 tail_bd->mode = frame_status;
2031 ++rxch->end_of_queue_add;
2032 }
2033 }
2034 ++rxch->recycled_bd;
2035}
2036
2037/**
2038 * emac_net_rx_cb: Prepares packet and sends to upper layer
2039 * @priv: The DaVinci EMAC private adapter structure
2040 * @net_pkt_list: Network packet list (received packets)
2041 *
2042 * Invalidates packet buffer memory and sends the received packet to upper
2043 * layer
2044 *
2045 * Returns success or appropriate error code (none as of now)
2046 */
2047static int emac_net_rx_cb(struct emac_priv *priv,
2048 struct emac_netpktobj *net_pkt_list)
2049{
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002050 struct net_device *ndev = priv->ndev;
2051 struct sk_buff *p_skb = net_pkt_list->pkt_token;
Anant Golea6286ee2009-05-18 15:19:01 -07002052 /* set length of packet */
2053 skb_put(p_skb, net_pkt_list->pkt_length);
Anant Golea6286ee2009-05-18 15:19:01 -07002054 p_skb->protocol = eth_type_trans(p_skb, priv->ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07002055 netif_receive_skb(p_skb);
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002056 ndev->stats.rx_bytes += net_pkt_list->pkt_length;
2057 ndev->stats.rx_packets++;
Anant Golea6286ee2009-05-18 15:19:01 -07002058 return 0;
2059}
2060
2061/**
2062 * emac_rx_bdproc: RX buffer descriptor (packet) processing
2063 * @priv: The DaVinci EMAC private adapter structure
2064 * @ch: RX channel number to process buffer descriptors for
2065 * @budget: number of packets allowed to process
2066 * @pending: indication to caller that packets are pending to process
2067 *
2068 * Processes RX buffer descriptors - checks ownership bit on the RX buffer
2069 * descriptor, sends the receive packet to upper layer, allocates a new SKB
2070 * and recycles the buffer descriptor (requeues it in hardware RX queue).
2071 * Only "budget" number of packets are processed and indication of pending
2072 * packets provided to the caller.
2073 *
2074 * Returns number of packets processed (and indication of pending packets)
2075 */
2076static int emac_rx_bdproc(struct emac_priv *priv, u32 ch, u32 budget)
2077{
2078 unsigned long flags;
2079 u32 frame_status;
2080 u32 pkts_processed = 0;
2081 char *new_buffer;
2082 struct emac_rx_bd __iomem *curr_bd;
2083 struct emac_rx_bd __iomem *last_bd;
2084 struct emac_netpktobj *curr_pkt, pkt_obj;
2085 struct emac_netbufobj buf_obj;
2086 struct emac_netbufobj *rx_buf_obj;
2087 void *new_buf_token;
2088 struct emac_rxch *rxch = priv->rxch[ch];
2089
2090 if (unlikely(1 == rxch->teardown_pending))
2091 return 0;
2092 ++rxch->proc_count;
2093 spin_lock_irqsave(&priv->rx_lock, flags);
2094 pkt_obj.buf_list = &buf_obj;
2095 curr_pkt = &pkt_obj;
2096 curr_bd = rxch->active_queue_head;
2097 BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
2098 frame_status = curr_bd->mode;
2099
2100 while ((curr_bd) &&
2101 ((frame_status & EMAC_CPPI_OWNERSHIP_BIT) == 0) &&
2102 (pkts_processed < budget)) {
2103
2104 new_buffer = emac_net_alloc_rx_buf(priv, rxch->buf_size,
2105 &new_buf_token, EMAC_DEF_RX_CH);
2106 if (unlikely(NULL == new_buffer)) {
2107 ++rxch->out_of_rx_buffers;
2108 goto end_emac_rx_bdproc;
2109 }
2110
2111 /* populate received packet data structure */
2112 rx_buf_obj = &curr_pkt->buf_list[0];
2113 rx_buf_obj->data_ptr = (char *)curr_bd->data_ptr;
2114 rx_buf_obj->length = curr_bd->off_b_len & EMAC_RX_BD_BUF_SIZE;
2115 rx_buf_obj->buf_token = curr_bd->buf_token;
Sekhar Noribe5bce22010-03-09 01:20:37 +00002116
2117 dma_unmap_single(&priv->ndev->dev, curr_bd->buff_ptr,
2118 curr_bd->off_b_len & EMAC_RX_BD_BUF_SIZE,
2119 DMA_FROM_DEVICE);
2120
Anant Golea6286ee2009-05-18 15:19:01 -07002121 curr_pkt->pkt_token = curr_pkt->buf_list->buf_token;
2122 curr_pkt->num_bufs = 1;
2123 curr_pkt->pkt_length =
2124 (frame_status & EMAC_RX_BD_PKT_LENGTH_MASK);
Sriramakrishnanad021ae2009-11-19 15:58:27 +05302125 emac_write(EMAC_RXCP(ch), emac_virt_to_phys(curr_bd, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07002126 ++rxch->processed_bd;
2127 last_bd = curr_bd;
2128 curr_bd = last_bd->next;
2129 rxch->active_queue_head = curr_bd;
2130
2131 /* check if end of RX queue ? */
2132 if (frame_status & EMAC_CPPI_EOQ_BIT) {
2133 if (curr_bd) {
2134 ++rxch->mis_queued_packets;
2135 emac_write(EMAC_RXHDP(ch),
Sriramakrishnanad021ae2009-11-19 15:58:27 +05302136 emac_virt_to_phys(curr_bd, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07002137 } else {
2138 ++rxch->end_of_queue;
2139 rxch->queue_active = 0;
2140 }
2141 }
2142
2143 /* recycle BD */
2144 emac_addbd_to_rx_queue(priv, ch, last_bd, new_buffer,
2145 new_buf_token);
2146
2147 /* return the packet to the user - BD ptr passed in
2148 * last parameter for potential *future* use */
2149 spin_unlock_irqrestore(&priv->rx_lock, flags);
2150 emac_net_rx_cb(priv, curr_pkt);
2151 spin_lock_irqsave(&priv->rx_lock, flags);
2152 curr_bd = rxch->active_queue_head;
2153 if (curr_bd) {
2154 BD_CACHE_INVALIDATE(curr_bd, EMAC_BD_LENGTH_FOR_CACHE);
2155 frame_status = curr_bd->mode;
2156 }
2157 ++pkts_processed;
2158 }
2159
2160end_emac_rx_bdproc:
2161 spin_unlock_irqrestore(&priv->rx_lock, flags);
2162 return pkts_processed;
2163}
2164
2165/**
2166 * emac_hw_enable: Enable EMAC hardware for packet transmission/reception
2167 * @priv: The DaVinci EMAC private adapter structure
2168 *
2169 * Enables EMAC hardware for packet processing - enables PHY, enables RX
2170 * for packet reception and enables device interrupts and then NAPI
2171 *
2172 * Returns success (0) or appropriate error code (none right now)
2173 */
2174static int emac_hw_enable(struct emac_priv *priv)
2175{
2176 u32 ch, val, mbp_enable, mac_control;
2177
2178 /* Soft reset */
2179 emac_write(EMAC_SOFTRESET, 1);
2180 while (emac_read(EMAC_SOFTRESET))
2181 cpu_relax();
2182
2183 /* Disable interrupt & Set pacing for more interrupts initially */
2184 emac_int_disable(priv);
2185
2186 /* Full duplex enable bit set when auto negotiation happens */
2187 mac_control =
2188 (((EMAC_DEF_TXPRIO_FIXED) ? (EMAC_MACCONTROL_TXPTYPE) : 0x0) |
2189 ((priv->speed == 1000) ? EMAC_MACCONTROL_GIGABITEN : 0x0) |
2190 ((EMAC_DEF_TXPACING_EN) ? (EMAC_MACCONTROL_TXPACEEN) : 0x0) |
2191 ((priv->duplex == DUPLEX_FULL) ? 0x1 : 0));
2192 emac_write(EMAC_MACCONTROL, mac_control);
2193
2194 mbp_enable =
2195 (((EMAC_DEF_PASS_CRC) ? (EMAC_RXMBP_PASSCRC_MASK) : 0x0) |
2196 ((EMAC_DEF_QOS_EN) ? (EMAC_RXMBP_QOSEN_MASK) : 0x0) |
2197 ((EMAC_DEF_NO_BUFF_CHAIN) ? (EMAC_RXMBP_NOCHAIN_MASK) : 0x0) |
2198 ((EMAC_DEF_MACCTRL_FRAME_EN) ? (EMAC_RXMBP_CMFEN_MASK) : 0x0) |
2199 ((EMAC_DEF_SHORT_FRAME_EN) ? (EMAC_RXMBP_CSFEN_MASK) : 0x0) |
2200 ((EMAC_DEF_ERROR_FRAME_EN) ? (EMAC_RXMBP_CEFEN_MASK) : 0x0) |
2201 ((EMAC_DEF_PROM_EN) ? (EMAC_RXMBP_CAFEN_MASK) : 0x0) |
2202 ((EMAC_DEF_PROM_CH & EMAC_RXMBP_CHMASK) << \
2203 EMAC_RXMBP_PROMCH_SHIFT) |
2204 ((EMAC_DEF_BCAST_EN) ? (EMAC_RXMBP_BROADEN_MASK) : 0x0) |
2205 ((EMAC_DEF_BCAST_CH & EMAC_RXMBP_CHMASK) << \
2206 EMAC_RXMBP_BROADCH_SHIFT) |
2207 ((EMAC_DEF_MCAST_EN) ? (EMAC_RXMBP_MULTIEN_MASK) : 0x0) |
2208 ((EMAC_DEF_MCAST_CH & EMAC_RXMBP_CHMASK) << \
2209 EMAC_RXMBP_MULTICH_SHIFT));
2210 emac_write(EMAC_RXMBPENABLE, mbp_enable);
2211 emac_write(EMAC_RXMAXLEN, (EMAC_DEF_MAX_FRAME_SIZE &
2212 EMAC_RX_MAX_LEN_MASK));
2213 emac_write(EMAC_RXBUFFEROFFSET, (EMAC_DEF_BUFFER_OFFSET &
2214 EMAC_RX_BUFFER_OFFSET_MASK));
2215 emac_write(EMAC_RXFILTERLOWTHRESH, 0);
2216 emac_write(EMAC_RXUNICASTCLEAR, EMAC_RX_UNICAST_CLEAR_ALL);
2217 priv->rx_addr_type = (emac_read(EMAC_MACCONFIG) >> 8) & 0xFF;
2218
2219 val = emac_read(EMAC_TXCONTROL);
2220 val |= EMAC_TX_CONTROL_TX_ENABLE_VAL;
2221 emac_write(EMAC_TXCONTROL, val);
2222 val = emac_read(EMAC_RXCONTROL);
2223 val |= EMAC_RX_CONTROL_RX_ENABLE_VAL;
2224 emac_write(EMAC_RXCONTROL, val);
2225 emac_write(EMAC_MACINTMASKSET, EMAC_MAC_HOST_ERR_INTMASK_VAL);
2226
2227 for (ch = 0; ch < EMAC_DEF_MAX_TX_CH; ch++) {
2228 emac_write(EMAC_TXHDP(ch), 0);
2229 emac_write(EMAC_TXINTMASKSET, BIT(ch));
2230 }
2231 for (ch = 0; ch < EMAC_DEF_MAX_RX_CH; ch++) {
2232 struct emac_rxch *rxch = priv->rxch[ch];
2233 emac_setmac(priv, ch, rxch->mac_addr);
2234 emac_write(EMAC_RXINTMASKSET, BIT(ch));
2235 rxch->queue_active = 1;
2236 emac_write(EMAC_RXHDP(ch),
Sriramakrishnanad021ae2009-11-19 15:58:27 +05302237 emac_virt_to_phys(rxch->active_queue_head, priv));
Anant Golea6286ee2009-05-18 15:19:01 -07002238 }
2239
2240 /* Enable MII */
2241 val = emac_read(EMAC_MACCONTROL);
chaithrika@ti.com69ef9692009-10-01 10:25:19 +00002242 val |= (EMAC_MACCONTROL_GMIIEN);
Anant Golea6286ee2009-05-18 15:19:01 -07002243 emac_write(EMAC_MACCONTROL, val);
2244
2245 /* Enable NAPI and interrupts */
2246 napi_enable(&priv->napi);
2247 emac_int_enable(priv);
2248 return 0;
2249
2250}
2251
2252/**
2253 * emac_poll: EMAC NAPI Poll function
2254 * @ndev: The DaVinci EMAC network adapter
2255 * @budget: Number of receive packets to process (as told by NAPI layer)
2256 *
2257 * NAPI Poll function implemented to process packets as per budget. We check
2258 * the type of interrupt on the device and accordingly call the TX or RX
2259 * packet processing functions. We follow the budget for RX processing and
2260 * also put a cap on number of TX pkts processed through config param. The
2261 * NAPI schedule function is called if more packets pending.
2262 *
2263 * Returns number of packets received (in most cases; else TX pkts - rarely)
2264 */
2265static int emac_poll(struct napi_struct *napi, int budget)
2266{
2267 unsigned int mask;
2268 struct emac_priv *priv = container_of(napi, struct emac_priv, napi);
2269 struct net_device *ndev = priv->ndev;
2270 struct device *emac_dev = &ndev->dev;
2271 u32 status = 0;
Sriram3725b1f2010-07-29 02:33:59 +00002272 u32 num_tx_pkts = 0, num_rx_pkts = 0;
Anant Golea6286ee2009-05-18 15:19:01 -07002273
Anant Golea6286ee2009-05-18 15:19:01 -07002274 /* Check interrupt vectors and call packet processing */
2275 status = emac_read(EMAC_MACINVECTOR);
2276
2277 mask = EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC;
2278
2279 if (priv->version == EMAC_VERSION_2)
2280 mask = EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC;
2281
2282 if (status & mask) {
Sriram3725b1f2010-07-29 02:33:59 +00002283 num_tx_pkts = emac_tx_bdproc(priv, EMAC_DEF_TX_CH,
Anant Golea6286ee2009-05-18 15:19:01 -07002284 EMAC_DEF_TX_MAX_SERVICE);
2285 } /* TX processing */
2286
Anant Golea6286ee2009-05-18 15:19:01 -07002287 mask = EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC;
2288
2289 if (priv->version == EMAC_VERSION_2)
2290 mask = EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC;
2291
2292 if (status & mask) {
Sriram3725b1f2010-07-29 02:33:59 +00002293 num_rx_pkts = emac_rx_bdproc(priv, EMAC_DEF_RX_CH, budget);
Anant Golea6286ee2009-05-18 15:19:01 -07002294 } /* RX processing */
2295
Sriram43c2ed82009-09-24 19:15:18 +00002296 mask = EMAC_DM644X_MAC_IN_VECTOR_HOST_INT;
2297 if (priv->version == EMAC_VERSION_2)
2298 mask = EMAC_DM646X_MAC_IN_VECTOR_HOST_INT;
2299
2300 if (unlikely(status & mask)) {
Anant Golea6286ee2009-05-18 15:19:01 -07002301 u32 ch, cause;
2302 dev_err(emac_dev, "DaVinci EMAC: Fatal Hardware Error\n");
2303 netif_stop_queue(ndev);
2304 napi_disable(&priv->napi);
2305
2306 status = emac_read(EMAC_MACSTATUS);
2307 cause = ((status & EMAC_MACSTATUS_TXERRCODE_MASK) >>
2308 EMAC_MACSTATUS_TXERRCODE_SHIFT);
2309 if (cause) {
2310 ch = ((status & EMAC_MACSTATUS_TXERRCH_MASK) >>
2311 EMAC_MACSTATUS_TXERRCH_SHIFT);
2312 if (net_ratelimit()) {
2313 dev_err(emac_dev, "TX Host error %s on ch=%d\n",
2314 &emac_txhost_errcodes[cause][0], ch);
2315 }
2316 }
2317 cause = ((status & EMAC_MACSTATUS_RXERRCODE_MASK) >>
2318 EMAC_MACSTATUS_RXERRCODE_SHIFT);
2319 if (cause) {
2320 ch = ((status & EMAC_MACSTATUS_RXERRCH_MASK) >>
2321 EMAC_MACSTATUS_RXERRCH_SHIFT);
2322 if (netif_msg_hw(priv) && net_ratelimit())
2323 dev_err(emac_dev, "RX Host error %s on ch=%d\n",
2324 &emac_rxhost_errcodes[cause][0], ch);
2325 }
Sriram3725b1f2010-07-29 02:33:59 +00002326 } else if (num_rx_pkts < budget) {
2327 napi_complete(napi);
2328 emac_int_enable(priv);
2329 }
Anant Golea6286ee2009-05-18 15:19:01 -07002330
Sriram3725b1f2010-07-29 02:33:59 +00002331 return num_rx_pkts;
Anant Golea6286ee2009-05-18 15:19:01 -07002332}
2333
2334#ifdef CONFIG_NET_POLL_CONTROLLER
2335/**
2336 * emac_poll_controller: EMAC Poll controller function
2337 * @ndev: The DaVinci EMAC network adapter
2338 *
2339 * Polled functionality used by netconsole and others in non interrupt mode
2340 *
2341 */
2342void emac_poll_controller(struct net_device *ndev)
2343{
2344 struct emac_priv *priv = netdev_priv(ndev);
2345
2346 emac_int_disable(priv);
Tonyliuc8ee5532009-11-04 05:45:02 -08002347 emac_irq(ndev->irq, ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07002348 emac_int_enable(priv);
2349}
2350#endif
2351
2352/* PHY/MII bus related */
2353
2354/* Wait until mdio is ready for next command */
2355#define MDIO_WAIT_FOR_USER_ACCESS\
2356 while ((emac_mdio_read((MDIO_USERACCESS(0))) &\
2357 MDIO_USERACCESS_GO) != 0)
2358
2359static int emac_mii_read(struct mii_bus *bus, int phy_id, int phy_reg)
2360{
2361 unsigned int phy_data = 0;
2362 unsigned int phy_control;
2363
2364 /* Wait until mdio is ready for next command */
2365 MDIO_WAIT_FOR_USER_ACCESS;
2366
2367 phy_control = (MDIO_USERACCESS_GO |
2368 MDIO_USERACCESS_READ |
2369 ((phy_reg << 21) & MDIO_USERACCESS_REGADR) |
2370 ((phy_id << 16) & MDIO_USERACCESS_PHYADR) |
2371 (phy_data & MDIO_USERACCESS_DATA));
2372 emac_mdio_write(MDIO_USERACCESS(0), phy_control);
2373
2374 /* Wait until mdio is ready for next command */
2375 MDIO_WAIT_FOR_USER_ACCESS;
2376
2377 return emac_mdio_read(MDIO_USERACCESS(0)) & MDIO_USERACCESS_DATA;
2378
2379}
2380
2381static int emac_mii_write(struct mii_bus *bus, int phy_id,
2382 int phy_reg, u16 phy_data)
2383{
2384
2385 unsigned int control;
2386
2387 /* until mdio is ready for next command */
2388 MDIO_WAIT_FOR_USER_ACCESS;
2389
2390 control = (MDIO_USERACCESS_GO |
2391 MDIO_USERACCESS_WRITE |
2392 ((phy_reg << 21) & MDIO_USERACCESS_REGADR) |
2393 ((phy_id << 16) & MDIO_USERACCESS_PHYADR) |
2394 (phy_data & MDIO_USERACCESS_DATA));
2395 emac_mdio_write(MDIO_USERACCESS(0), control);
2396
2397 return 0;
2398}
2399
2400static int emac_mii_reset(struct mii_bus *bus)
2401{
2402 unsigned int clk_div;
2403 int mdio_bus_freq = emac_bus_frequency;
2404
Nageswari Srinivasanf9c41712009-12-18 20:21:21 -08002405 if (mdio_max_freq && mdio_bus_freq)
Anant Golea6286ee2009-05-18 15:19:01 -07002406 clk_div = ((mdio_bus_freq / mdio_max_freq) - 1);
2407 else
2408 clk_div = 0xFF;
2409
2410 clk_div &= MDIO_CONTROL_CLKDIV;
2411
2412 /* Set enable and clock divider in MDIOControl */
2413 emac_mdio_write(MDIO_CONTROL, (clk_div | MDIO_CONTROL_ENABLE));
2414
2415 return 0;
2416
2417}
2418
2419static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, PHY_POLL };
2420
2421/* emac_driver: EMAC MII bus structure */
2422
2423static struct mii_bus *emac_mii;
2424
2425static void emac_adjust_link(struct net_device *ndev)
2426{
2427 struct emac_priv *priv = netdev_priv(ndev);
2428 struct phy_device *phydev = priv->phydev;
2429 unsigned long flags;
2430 int new_state = 0;
2431
2432 spin_lock_irqsave(&priv->lock, flags);
2433
2434 if (phydev->link) {
2435 /* check the mode of operation - full/half duplex */
2436 if (phydev->duplex != priv->duplex) {
2437 new_state = 1;
2438 priv->duplex = phydev->duplex;
2439 }
2440 if (phydev->speed != priv->speed) {
2441 new_state = 1;
2442 priv->speed = phydev->speed;
2443 }
2444 if (!priv->link) {
2445 new_state = 1;
2446 priv->link = 1;
2447 }
2448
2449 } else if (priv->link) {
2450 new_state = 1;
2451 priv->link = 0;
2452 priv->speed = 0;
2453 priv->duplex = ~0;
2454 }
2455 if (new_state) {
2456 emac_update_phystatus(priv);
2457 phy_print_status(priv->phydev);
2458 }
2459
2460 spin_unlock_irqrestore(&priv->lock, flags);
2461}
2462
2463/*************************************************************************
2464 * Linux Driver Model
2465 *************************************************************************/
2466
2467/**
2468 * emac_devioctl: EMAC adapter ioctl
2469 * @ndev: The DaVinci EMAC network adapter
2470 * @ifrq: request parameter
2471 * @cmd: command parameter
2472 *
2473 * EMAC driver ioctl function
2474 *
2475 * Returns success(0) or appropriate error code
2476 */
2477static int emac_devioctl(struct net_device *ndev, struct ifreq *ifrq, int cmd)
2478{
2479 dev_warn(&ndev->dev, "DaVinci EMAC: ioctl not supported\n");
2480
2481 if (!(netif_running(ndev)))
2482 return -EINVAL;
2483
2484 /* TODO: Add phy read and write and private statistics get feature */
2485
2486 return -EOPNOTSUPP;
2487}
2488
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002489static int match_first_device(struct device *dev, void *data)
2490{
2491 return 1;
2492}
2493
Anant Golea6286ee2009-05-18 15:19:01 -07002494/**
2495 * emac_dev_open: EMAC device open
2496 * @ndev: The DaVinci EMAC network adapter
2497 *
2498 * Called when system wants to start the interface. We init TX/RX channels
2499 * and enable the hardware for packet reception/transmission and start the
2500 * network queue.
2501 *
2502 * Returns 0 for a successful open, or appropriate error code
2503 */
2504static int emac_dev_open(struct net_device *ndev)
2505{
2506 struct device *emac_dev = &ndev->dev;
2507 u32 rc, cnt, ch;
Anant Golea6286ee2009-05-18 15:19:01 -07002508 struct resource *res;
2509 int q, m;
2510 int i = 0;
2511 int k = 0;
2512 struct emac_priv *priv = netdev_priv(ndev);
2513
2514 netif_carrier_off(ndev);
Dan Carpenter4d27b872010-03-02 21:07:24 +00002515 for (cnt = 0; cnt < ETH_ALEN; cnt++)
Anant Golea6286ee2009-05-18 15:19:01 -07002516 ndev->dev_addr[cnt] = priv->mac_addr[cnt];
2517
2518 /* Configuration items */
2519 priv->rx_buf_size = EMAC_DEF_MAX_FRAME_SIZE + NET_IP_ALIGN;
2520
2521 /* Clear basic hardware */
2522 for (ch = 0; ch < EMAC_MAX_TXRX_CHANNELS; ch++) {
2523 emac_write(EMAC_TXHDP(ch), 0);
2524 emac_write(EMAC_RXHDP(ch), 0);
2525 emac_write(EMAC_RXHDP(ch), 0);
2526 emac_write(EMAC_RXINTMASKCLEAR, EMAC_INT_MASK_CLEAR);
2527 emac_write(EMAC_TXINTMASKCLEAR, EMAC_INT_MASK_CLEAR);
2528 }
2529 priv->mac_hash1 = 0;
2530 priv->mac_hash2 = 0;
2531 emac_write(EMAC_MACHASH1, 0);
2532 emac_write(EMAC_MACHASH2, 0);
2533
2534 /* multi ch not supported - open 1 TX, 1RX ch by default */
2535 rc = emac_init_txch(priv, EMAC_DEF_TX_CH);
2536 if (0 != rc) {
2537 dev_err(emac_dev, "DaVinci EMAC: emac_init_txch() failed");
2538 return rc;
2539 }
2540 rc = emac_init_rxch(priv, EMAC_DEF_RX_CH, priv->mac_addr);
2541 if (0 != rc) {
2542 dev_err(emac_dev, "DaVinci EMAC: emac_init_rxch() failed");
2543 return rc;
2544 }
2545
2546 /* Request IRQ */
2547
2548 while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
2549 for (i = res->start; i <= res->end; i++) {
2550 if (request_irq(i, emac_irq, IRQF_DISABLED,
2551 ndev->name, ndev))
2552 goto rollback;
2553 }
2554 k++;
2555 }
2556
2557 /* Start/Enable EMAC hardware */
2558 emac_hw_enable(priv);
2559
Sriram84da2652010-07-29 02:33:58 +00002560 /* Enable Interrupt pacing if configured */
2561 if (priv->coal_intvl != 0) {
2562 struct ethtool_coalesce coal;
2563
2564 coal.rx_coalesce_usecs = (priv->coal_intvl << 4);
2565 emac_set_coalesce(ndev, &coal);
2566 }
2567
Anant Golea6286ee2009-05-18 15:19:01 -07002568 priv->phydev = NULL;
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002569 /* use the first phy on the bus if pdata did not give us a phy id */
2570 if (!priv->phy_id) {
2571 struct device *phy;
Anant Golea6286ee2009-05-18 15:19:01 -07002572
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002573 phy = bus_find_device(&mdio_bus_type, NULL, NULL,
2574 match_first_device);
2575 if (phy)
2576 priv->phy_id = dev_name(phy);
2577 }
Anant Golea6286ee2009-05-18 15:19:01 -07002578
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002579 if (priv->phy_id && *priv->phy_id) {
2580 priv->phydev = phy_connect(ndev, priv->phy_id,
2581 &emac_adjust_link, 0,
2582 PHY_INTERFACE_MODE_MII);
Anant Golea6286ee2009-05-18 15:19:01 -07002583
2584 if (IS_ERR(priv->phydev)) {
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002585 dev_err(emac_dev, "could not connect to phy %s\n",
2586 priv->phy_id);
2587 priv->phydev = NULL;
Anant Golea6286ee2009-05-18 15:19:01 -07002588 return PTR_ERR(priv->phydev);
2589 }
2590
2591 priv->link = 0;
2592 priv->speed = 0;
2593 priv->duplex = ~0;
2594
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002595 dev_info(emac_dev, "attached PHY driver [%s] "
2596 "(mii_bus:phy_addr=%s, id=%x)\n",
Anant Golea6286ee2009-05-18 15:19:01 -07002597 priv->phydev->drv->name, dev_name(&priv->phydev->dev),
2598 priv->phydev->phy_id);
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002599 } else {
Anant Golea6286ee2009-05-18 15:19:01 -07002600 /* No PHY , fix the link, speed and duplex settings */
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002601 dev_notice(emac_dev, "no phy, defaulting to 100/full\n");
Anant Golea6286ee2009-05-18 15:19:01 -07002602 priv->link = 1;
2603 priv->speed = SPEED_100;
2604 priv->duplex = DUPLEX_FULL;
2605 emac_update_phystatus(priv);
2606 }
2607
2608 if (!netif_running(ndev)) /* debug only - to avoid compiler warning */
2609 emac_dump_regs(priv);
2610
2611 if (netif_msg_drv(priv))
2612 dev_notice(emac_dev, "DaVinci EMAC: Opened %s\n", ndev->name);
2613
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002614 if (priv->phydev)
Anant Golea6286ee2009-05-18 15:19:01 -07002615 phy_start(priv->phydev);
2616
2617 return 0;
2618
2619rollback:
2620
2621 dev_err(emac_dev, "DaVinci EMAC: request_irq() failed");
2622
2623 for (q = k; k >= 0; k--) {
2624 for (m = i; m >= res->start; m--)
2625 free_irq(m, ndev);
2626 res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k-1);
2627 m = res->end;
2628 }
2629 return -EBUSY;
2630}
2631
2632/**
2633 * emac_dev_stop: EMAC device stop
2634 * @ndev: The DaVinci EMAC network adapter
2635 *
2636 * Called when system wants to stop or down the interface. We stop the network
2637 * queue, disable interrupts and cleanup TX/RX channels.
2638 *
2639 * We return the statistics in net_device_stats structure pulled from emac
2640 */
2641static int emac_dev_stop(struct net_device *ndev)
2642{
2643 struct resource *res;
2644 int i = 0;
2645 int irq_num;
2646 struct emac_priv *priv = netdev_priv(ndev);
2647 struct device *emac_dev = &ndev->dev;
2648
2649 /* inform the upper layers. */
2650 netif_stop_queue(ndev);
2651 napi_disable(&priv->napi);
2652
2653 netif_carrier_off(ndev);
2654 emac_int_disable(priv);
2655 emac_stop_txch(priv, EMAC_DEF_TX_CH);
2656 emac_stop_rxch(priv, EMAC_DEF_RX_CH);
2657 emac_cleanup_txch(priv, EMAC_DEF_TX_CH);
2658 emac_cleanup_rxch(priv, EMAC_DEF_RX_CH);
2659 emac_write(EMAC_SOFTRESET, 1);
2660
2661 if (priv->phydev)
2662 phy_disconnect(priv->phydev);
2663
2664 /* Free IRQ */
2665 while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, i))) {
2666 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2667 free_irq(irq_num, priv->ndev);
2668 i++;
2669 }
2670
2671 if (netif_msg_drv(priv))
2672 dev_notice(emac_dev, "DaVinci EMAC: %s stopped\n", ndev->name);
2673
2674 return 0;
2675}
2676
2677/**
2678 * emac_dev_getnetstats: EMAC get statistics function
2679 * @ndev: The DaVinci EMAC network adapter
2680 *
2681 * Called when system wants to get statistics from the device.
2682 *
2683 * We return the statistics in net_device_stats structure pulled from emac
2684 */
2685static struct net_device_stats *emac_dev_getnetstats(struct net_device *ndev)
2686{
2687 struct emac_priv *priv = netdev_priv(ndev);
Sriram0fe74632009-10-07 02:44:30 +00002688 u32 mac_control;
2689 u32 stats_clear_mask;
Anant Golea6286ee2009-05-18 15:19:01 -07002690
2691 /* update emac hardware stats and reset the registers*/
2692
Sriram0fe74632009-10-07 02:44:30 +00002693 mac_control = emac_read(EMAC_MACCONTROL);
2694
2695 if (mac_control & EMAC_MACCONTROL_GMIIEN)
2696 stats_clear_mask = EMAC_STATS_CLR_MASK;
2697 else
2698 stats_clear_mask = 0;
2699
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002700 ndev->stats.multicast += emac_read(EMAC_RXMCASTFRAMES);
Sriram0fe74632009-10-07 02:44:30 +00002701 emac_write(EMAC_RXMCASTFRAMES, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002702
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002703 ndev->stats.collisions += (emac_read(EMAC_TXCOLLISION) +
Anant Golea6286ee2009-05-18 15:19:01 -07002704 emac_read(EMAC_TXSINGLECOLL) +
2705 emac_read(EMAC_TXMULTICOLL));
Sriram0fe74632009-10-07 02:44:30 +00002706 emac_write(EMAC_TXCOLLISION, stats_clear_mask);
2707 emac_write(EMAC_TXSINGLECOLL, stats_clear_mask);
2708 emac_write(EMAC_TXMULTICOLL, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002709
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002710 ndev->stats.rx_length_errors += (emac_read(EMAC_RXOVERSIZED) +
Anant Golea6286ee2009-05-18 15:19:01 -07002711 emac_read(EMAC_RXJABBER) +
2712 emac_read(EMAC_RXUNDERSIZED));
Sriram0fe74632009-10-07 02:44:30 +00002713 emac_write(EMAC_RXOVERSIZED, stats_clear_mask);
2714 emac_write(EMAC_RXJABBER, stats_clear_mask);
2715 emac_write(EMAC_RXUNDERSIZED, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002716
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002717 ndev->stats.rx_over_errors += (emac_read(EMAC_RXSOFOVERRUNS) +
Anant Golea6286ee2009-05-18 15:19:01 -07002718 emac_read(EMAC_RXMOFOVERRUNS));
Sriram0fe74632009-10-07 02:44:30 +00002719 emac_write(EMAC_RXSOFOVERRUNS, stats_clear_mask);
2720 emac_write(EMAC_RXMOFOVERRUNS, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002721
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002722 ndev->stats.rx_fifo_errors += emac_read(EMAC_RXDMAOVERRUNS);
Sriram0fe74632009-10-07 02:44:30 +00002723 emac_write(EMAC_RXDMAOVERRUNS, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002724
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002725 ndev->stats.tx_carrier_errors +=
Anant Golea6286ee2009-05-18 15:19:01 -07002726 emac_read(EMAC_TXCARRIERSENSE);
Sriram0fe74632009-10-07 02:44:30 +00002727 emac_write(EMAC_TXCARRIERSENSE, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002728
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002729 ndev->stats.tx_fifo_errors = emac_read(EMAC_TXUNDERRUN);
Sriram0fe74632009-10-07 02:44:30 +00002730 emac_write(EMAC_TXUNDERRUN, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07002731
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00002732 return &ndev->stats;
Anant Golea6286ee2009-05-18 15:19:01 -07002733}
2734
2735static const struct net_device_ops emac_netdev_ops = {
2736 .ndo_open = emac_dev_open,
2737 .ndo_stop = emac_dev_stop,
2738 .ndo_start_xmit = emac_dev_xmit,
2739 .ndo_set_multicast_list = emac_dev_mcast_set,
2740 .ndo_set_mac_address = emac_dev_setmac_addr,
2741 .ndo_do_ioctl = emac_devioctl,
2742 .ndo_tx_timeout = emac_dev_tx_timeout,
2743 .ndo_get_stats = emac_dev_getnetstats,
2744#ifdef CONFIG_NET_POLL_CONTROLLER
2745 .ndo_poll_controller = emac_poll_controller,
2746#endif
2747};
2748
2749/**
2750 * davinci_emac_probe: EMAC device probe
2751 * @pdev: The DaVinci EMAC device that we are removing
2752 *
2753 * Called when probing for emac devicesr. We get details of instances and
2754 * resource information from platform init and register a network device
2755 * and allocate resources necessary for driver to perform
2756 */
2757static int __devinit davinci_emac_probe(struct platform_device *pdev)
2758{
2759 int rc = 0;
2760 struct resource *res;
2761 struct net_device *ndev;
2762 struct emac_priv *priv;
2763 unsigned long size;
2764 struct emac_platform_data *pdata;
2765 struct device *emac_dev;
2766
2767 /* obtain emac clock from kernel */
2768 emac_clk = clk_get(&pdev->dev, NULL);
2769 if (IS_ERR(emac_clk)) {
2770 printk(KERN_ERR "DaVinci EMAC: Failed to get EMAC clock\n");
2771 return -EBUSY;
2772 }
2773 emac_bus_frequency = clk_get_rate(emac_clk);
2774 /* TODO: Probe PHY here if possible */
2775
2776 ndev = alloc_etherdev(sizeof(struct emac_priv));
2777 if (!ndev) {
2778 printk(KERN_ERR "DaVinci EMAC: Error allocating net_device\n");
2779 clk_put(emac_clk);
2780 return -ENOMEM;
2781 }
2782
2783 platform_set_drvdata(pdev, ndev);
2784 priv = netdev_priv(ndev);
2785 priv->pdev = pdev;
2786 priv->ndev = ndev;
2787 priv->msg_enable = netif_msg_init(debug_level, DAVINCI_EMAC_DEBUG);
2788
2789 spin_lock_init(&priv->tx_lock);
2790 spin_lock_init(&priv->rx_lock);
2791 spin_lock_init(&priv->lock);
2792
2793 pdata = pdev->dev.platform_data;
2794 if (!pdata) {
Stefan Weil0747e3b2010-01-07 00:44:08 +01002795 printk(KERN_ERR "DaVinci EMAC: No platform data\n");
Anant Golea6286ee2009-05-18 15:19:01 -07002796 return -ENODEV;
2797 }
2798
2799 /* MAC addr and PHY mask , RMII enable info from platform_data */
2800 memcpy(priv->mac_addr, pdata->mac_addr, 6);
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04002801 priv->phy_id = pdata->phy_id;
Anant Golea6286ee2009-05-18 15:19:01 -07002802 priv->rmii_en = pdata->rmii_en;
2803 priv->version = pdata->version;
Sriramakrishnan01a9af32009-11-19 15:58:26 +05302804 priv->int_enable = pdata->interrupt_enable;
2805 priv->int_disable = pdata->interrupt_disable;
2806
Sriram84da2652010-07-29 02:33:58 +00002807 priv->coal_intvl = 0;
2808 priv->bus_freq_mhz = (u32)(emac_bus_frequency / 1000000);
2809
Anant Golea6286ee2009-05-18 15:19:01 -07002810 emac_dev = &ndev->dev;
2811 /* Get EMAC platform data */
2812 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2813 if (!res) {
2814 dev_err(emac_dev, "DaVinci EMAC: Error getting res\n");
2815 rc = -ENOENT;
2816 goto probe_quit;
2817 }
2818
2819 priv->emac_base_phys = res->start + pdata->ctrl_reg_offset;
2820 size = res->end - res->start + 1;
2821 if (!request_mem_region(res->start, size, ndev->name)) {
Joe Perches235ecb12010-02-01 21:22:11 +00002822 dev_err(emac_dev, "DaVinci EMAC: failed request_mem_region() for regs\n");
Anant Golea6286ee2009-05-18 15:19:01 -07002823 rc = -ENXIO;
2824 goto probe_quit;
2825 }
2826
2827 priv->remap_addr = ioremap(res->start, size);
2828 if (!priv->remap_addr) {
2829 dev_err(emac_dev, "Unable to map IO\n");
2830 rc = -ENOMEM;
2831 release_mem_region(res->start, size);
2832 goto probe_quit;
2833 }
2834 priv->emac_base = priv->remap_addr + pdata->ctrl_reg_offset;
2835 ndev->base_addr = (unsigned long)priv->remap_addr;
2836
2837 priv->ctrl_base = priv->remap_addr + pdata->ctrl_mod_reg_offset;
2838 priv->ctrl_ram_size = pdata->ctrl_ram_size;
2839 priv->emac_ctrl_ram = priv->remap_addr + pdata->ctrl_ram_offset;
2840
Sriramakrishnanad021ae2009-11-19 15:58:27 +05302841 if (pdata->hw_ram_addr)
2842 priv->hw_ram_addr = pdata->hw_ram_addr;
2843 else
2844 priv->hw_ram_addr = (u32 __force)res->start +
2845 pdata->ctrl_ram_offset;
2846
Anant Golea6286ee2009-05-18 15:19:01 -07002847 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2848 if (!res) {
2849 dev_err(emac_dev, "DaVinci EMAC: Error getting irq res\n");
2850 rc = -ENOENT;
2851 goto no_irq_res;
2852 }
2853 ndev->irq = res->start;
2854
2855 if (!is_valid_ether_addr(priv->mac_addr)) {
Anant Golea6286ee2009-05-18 15:19:01 -07002856 /* Use random MAC if none passed */
2857 random_ether_addr(priv->mac_addr);
Chaithrika U S5c726162009-06-03 21:54:29 -07002858 printk(KERN_WARNING "%s: using random MAC addr: %pM\n",
2859 __func__, priv->mac_addr);
Anant Golea6286ee2009-05-18 15:19:01 -07002860 }
2861
2862 ndev->netdev_ops = &emac_netdev_ops;
2863 SET_ETHTOOL_OPS(ndev, &ethtool_ops);
2864 netif_napi_add(ndev, &priv->napi, emac_poll, EMAC_POLL_WEIGHT);
2865
Sriram1ca518b2010-01-07 00:22:37 +00002866 clk_enable(emac_clk);
2867
Anant Golea6286ee2009-05-18 15:19:01 -07002868 /* register the network device */
2869 SET_NETDEV_DEV(ndev, &pdev->dev);
2870 rc = register_netdev(ndev);
2871 if (rc) {
2872 dev_err(emac_dev, "DaVinci EMAC: Error in register_netdev\n");
2873 rc = -ENODEV;
2874 goto netdev_reg_err;
2875 }
2876
Anant Golea6286ee2009-05-18 15:19:01 -07002877
Anant Golea6286ee2009-05-18 15:19:01 -07002878 if (netif_msg_probe(priv)) {
2879 dev_notice(emac_dev, "DaVinci EMAC Probe found device "\
2880 "(regs: %p, irq: %d)\n",
2881 (void *)priv->emac_base_phys, ndev->irq);
2882 }
2883 return 0;
2884
Anant Golea6286ee2009-05-18 15:19:01 -07002885netdev_reg_err:
Sriram1ca518b2010-01-07 00:22:37 +00002886 clk_disable(emac_clk);
Anant Golea6286ee2009-05-18 15:19:01 -07002887no_irq_res:
2888 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2889 release_mem_region(res->start, res->end - res->start + 1);
2890 iounmap(priv->remap_addr);
2891
2892probe_quit:
2893 clk_put(emac_clk);
2894 free_netdev(ndev);
2895 return rc;
2896}
2897
2898/**
2899 * davinci_emac_remove: EMAC device remove
2900 * @pdev: The DaVinci EMAC device that we are removing
2901 *
2902 * Called when removing the device driver. We disable clock usage and release
2903 * the resources taken up by the driver and unregister network device
2904 */
2905static int __devexit davinci_emac_remove(struct platform_device *pdev)
2906{
2907 struct resource *res;
2908 struct net_device *ndev = platform_get_drvdata(pdev);
2909 struct emac_priv *priv = netdev_priv(ndev);
2910
2911 dev_notice(&ndev->dev, "DaVinci EMAC: davinci_emac_remove()\n");
2912
Anant Golea6286ee2009-05-18 15:19:01 -07002913 platform_set_drvdata(pdev, NULL);
2914 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Anant Golea6286ee2009-05-18 15:19:01 -07002915
2916 release_mem_region(res->start, res->end - res->start + 1);
2917
2918 unregister_netdev(ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07002919 iounmap(priv->remap_addr);
Stefan Weil2a1bc0d2010-08-03 08:53:45 +00002920 free_netdev(ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07002921
2922 clk_disable(emac_clk);
2923 clk_put(emac_clk);
2924
2925 return 0;
2926}
2927
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002928static int davinci_emac_suspend(struct device *dev)
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002929{
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002930 struct platform_device *pdev = to_platform_device(dev);
2931 struct net_device *ndev = platform_get_drvdata(pdev);
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002932
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002933 if (netif_running(ndev))
2934 emac_dev_stop(ndev);
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002935
2936 clk_disable(emac_clk);
2937
2938 return 0;
2939}
2940
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002941static int davinci_emac_resume(struct device *dev)
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002942{
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002943 struct platform_device *pdev = to_platform_device(dev);
2944 struct net_device *ndev = platform_get_drvdata(pdev);
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002945
2946 clk_enable(emac_clk);
2947
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002948 if (netif_running(ndev))
2949 emac_dev_open(ndev);
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002950
2951 return 0;
2952}
2953
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002954static const struct dev_pm_ops davinci_emac_pm_ops = {
2955 .suspend = davinci_emac_suspend,
2956 .resume = davinci_emac_resume,
2957};
2958
Anant Golea6286ee2009-05-18 15:19:01 -07002959/**
2960 * davinci_emac_driver: EMAC platform driver structure
Anant Golea6286ee2009-05-18 15:19:01 -07002961 */
2962static struct platform_driver davinci_emac_driver = {
2963 .driver = {
2964 .name = "davinci_emac",
2965 .owner = THIS_MODULE,
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002966 .pm = &davinci_emac_pm_ops,
Anant Golea6286ee2009-05-18 15:19:01 -07002967 },
2968 .probe = davinci_emac_probe,
2969 .remove = __devexit_p(davinci_emac_remove),
2970};
2971
2972/**
2973 * davinci_emac_init: EMAC driver module init
2974 *
2975 * Called when initializing the driver. We register the driver with
2976 * the platform.
2977 */
2978static int __init davinci_emac_init(void)
2979{
2980 return platform_driver_register(&davinci_emac_driver);
2981}
Rajashekhara, Sudhakar2db95172009-08-19 10:39:55 +00002982late_initcall(davinci_emac_init);
Anant Golea6286ee2009-05-18 15:19:01 -07002983
2984/**
2985 * davinci_emac_exit: EMAC driver module exit
2986 *
2987 * Called when exiting the driver completely. We unregister the driver with
2988 * the platform and exit
2989 */
2990static void __exit davinci_emac_exit(void)
2991{
2992 platform_driver_unregister(&davinci_emac_driver);
2993}
2994module_exit(davinci_emac_exit);
2995
2996MODULE_LICENSE("GPL");
2997MODULE_AUTHOR("DaVinci EMAC Maintainer: Anant Gole <anantgole@ti.com>");
2998MODULE_AUTHOR("DaVinci EMAC Maintainer: Chaithrika U S <chaithrika@ti.com>");
2999MODULE_DESCRIPTION("DaVinci EMAC Ethernet driver");