blob: b298ab071e3d3852454d6de4f9e6a0e635d2ab92 [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>
Anant Golea6286ee2009-05-18 15:19:01 -070051#include <linux/spinlock.h>
52#include <linux/dma-mapping.h>
53#include <linux/clk.h>
54#include <linux/platform_device.h>
55#include <linux/semaphore.h>
56#include <linux/phy.h>
57#include <linux/bitops.h>
58#include <linux/io.h>
59#include <linux/uaccess.h>
Sriramakrishnan8ee2bf92009-11-19 15:58:25 +053060#include <linux/davinci_emac.h>
Heiko Schocher42f59962012-07-17 00:34:24 +000061#include <linux/of.h>
62#include <linux/of_address.h>
63#include <linux/of_irq.h>
64#include <linux/of_net.h>
65
66#include <mach/mux.h>
Anant Golea6286ee2009-05-18 15:19:01 -070067
68#include <asm/irq.h>
69#include <asm/page.h>
70
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -040071#include "davinci_cpdma.h"
72
Anant Golea6286ee2009-05-18 15:19:01 -070073static int debug_level;
74module_param(debug_level, int, 0);
75MODULE_PARM_DESC(debug_level, "DaVinci EMAC debug level (NETIF_MSG bits)");
76
77/* Netif debug messages possible */
78#define DAVINCI_EMAC_DEBUG (NETIF_MSG_DRV | \
79 NETIF_MSG_PROBE | \
80 NETIF_MSG_LINK | \
81 NETIF_MSG_TIMER | \
82 NETIF_MSG_IFDOWN | \
83 NETIF_MSG_IFUP | \
84 NETIF_MSG_RX_ERR | \
85 NETIF_MSG_TX_ERR | \
86 NETIF_MSG_TX_QUEUED | \
87 NETIF_MSG_INTR | \
88 NETIF_MSG_TX_DONE | \
89 NETIF_MSG_RX_STATUS | \
90 NETIF_MSG_PKTDATA | \
91 NETIF_MSG_HW | \
92 NETIF_MSG_WOL)
93
94/* version info */
95#define EMAC_MAJOR_VERSION 6
96#define EMAC_MINOR_VERSION 1
97#define EMAC_MODULE_VERSION "6.1"
98MODULE_VERSION(EMAC_MODULE_VERSION);
99static const char emac_version_string[] = "TI DaVinci EMAC Linux v6.1";
100
101/* Configuration items */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300102#define EMAC_DEF_PASS_CRC (0) /* Do not pass CRC up to frames */
Anant Golea6286ee2009-05-18 15:19:01 -0700103#define EMAC_DEF_QOS_EN (0) /* EMAC proprietary QoS disabled */
104#define EMAC_DEF_NO_BUFF_CHAIN (0) /* No buffer chain */
105#define EMAC_DEF_MACCTRL_FRAME_EN (0) /* Discard Maccontrol frames */
106#define EMAC_DEF_SHORT_FRAME_EN (0) /* Discard short frames */
107#define EMAC_DEF_ERROR_FRAME_EN (0) /* Discard error frames */
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300108#define EMAC_DEF_PROM_EN (0) /* Promiscuous disabled */
109#define EMAC_DEF_PROM_CH (0) /* Promiscuous channel is 0 */
Anant Golea6286ee2009-05-18 15:19:01 -0700110#define EMAC_DEF_BCAST_EN (1) /* Broadcast enabled */
111#define EMAC_DEF_BCAST_CH (0) /* Broadcast channel is 0 */
112#define EMAC_DEF_MCAST_EN (1) /* Multicast enabled */
113#define EMAC_DEF_MCAST_CH (0) /* Multicast channel is 0 */
114
115#define EMAC_DEF_TXPRIO_FIXED (1) /* TX Priority is fixed */
116#define EMAC_DEF_TXPACING_EN (0) /* TX pacing NOT supported*/
117
118#define EMAC_DEF_BUFFER_OFFSET (0) /* Buffer offset to DMA (future) */
119#define EMAC_DEF_MIN_ETHPKTSIZE (60) /* Minimum ethernet pkt size */
120#define EMAC_DEF_MAX_FRAME_SIZE (1500 + 14 + 4 + 4)
121#define EMAC_DEF_TX_CH (0) /* Default 0th channel */
122#define EMAC_DEF_RX_CH (0) /* Default 0th channel */
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -0400123#define EMAC_DEF_RX_NUM_DESC (128)
Sascha Hauer86d8c072012-01-03 05:27:47 +0000124#define EMAC_DEF_TX_NUM_DESC (128)
Anant Golea6286ee2009-05-18 15:19:01 -0700125#define EMAC_DEF_MAX_TX_CH (1) /* Max TX channels configured */
126#define EMAC_DEF_MAX_RX_CH (1) /* Max RX channels configured */
127#define EMAC_POLL_WEIGHT (64) /* Default NAPI poll weight */
128
129/* Buffer descriptor parameters */
130#define EMAC_DEF_TX_MAX_SERVICE (32) /* TX max service BD's */
131#define EMAC_DEF_RX_MAX_SERVICE (64) /* should = netdev->weight */
132
133/* EMAC register related defines */
134#define EMAC_ALL_MULTI_REG_VALUE (0xFFFFFFFF)
135#define EMAC_NUM_MULTICAST_BITS (64)
Anant Golea6286ee2009-05-18 15:19:01 -0700136#define EMAC_TX_CONTROL_TX_ENABLE_VAL (0x1)
137#define EMAC_RX_CONTROL_RX_ENABLE_VAL (0x1)
138#define EMAC_MAC_HOST_ERR_INTMASK_VAL (0x2)
139#define EMAC_RX_UNICAST_CLEAR_ALL (0xFF)
140#define EMAC_INT_MASK_CLEAR (0xFF)
141
142/* RX MBP register bit positions */
143#define EMAC_RXMBP_PASSCRC_MASK BIT(30)
144#define EMAC_RXMBP_QOSEN_MASK BIT(29)
145#define EMAC_RXMBP_NOCHAIN_MASK BIT(28)
146#define EMAC_RXMBP_CMFEN_MASK BIT(24)
147#define EMAC_RXMBP_CSFEN_MASK BIT(23)
148#define EMAC_RXMBP_CEFEN_MASK BIT(22)
149#define EMAC_RXMBP_CAFEN_MASK BIT(21)
150#define EMAC_RXMBP_PROMCH_SHIFT (16)
151#define EMAC_RXMBP_PROMCH_MASK (0x7 << 16)
152#define EMAC_RXMBP_BROADEN_MASK BIT(13)
153#define EMAC_RXMBP_BROADCH_SHIFT (8)
154#define EMAC_RXMBP_BROADCH_MASK (0x7 << 8)
155#define EMAC_RXMBP_MULTIEN_MASK BIT(5)
156#define EMAC_RXMBP_MULTICH_SHIFT (0)
157#define EMAC_RXMBP_MULTICH_MASK (0x7)
158#define EMAC_RXMBP_CHMASK (0x7)
159
160/* EMAC register definitions/bit maps used */
161# define EMAC_MBP_RXPROMISC (0x00200000)
162# define EMAC_MBP_PROMISCCH(ch) (((ch) & 0x7) << 16)
163# define EMAC_MBP_RXBCAST (0x00002000)
164# define EMAC_MBP_BCASTCHAN(ch) (((ch) & 0x7) << 8)
165# define EMAC_MBP_RXMCAST (0x00000020)
166# define EMAC_MBP_MCASTCHAN(ch) ((ch) & 0x7)
167
168/* EMAC mac_control register */
chaithrika@ti.com69ef9692009-10-01 10:25:19 +0000169#define EMAC_MACCONTROL_TXPTYPE BIT(9)
170#define EMAC_MACCONTROL_TXPACEEN BIT(6)
171#define EMAC_MACCONTROL_GMIIEN BIT(5)
172#define EMAC_MACCONTROL_GIGABITEN BIT(7)
173#define EMAC_MACCONTROL_FULLDUPLEXEN BIT(0)
Anant Golea6286ee2009-05-18 15:19:01 -0700174#define EMAC_MACCONTROL_RMIISPEED_MASK BIT(15)
175
176/* GIGABIT MODE related bits */
Anant Golea6286ee2009-05-18 15:19:01 -0700177#define EMAC_DM646X_MACCONTORL_GIG BIT(7)
178#define EMAC_DM646X_MACCONTORL_GIGFORCE BIT(17)
179
180/* EMAC mac_status register */
181#define EMAC_MACSTATUS_TXERRCODE_MASK (0xF00000)
182#define EMAC_MACSTATUS_TXERRCODE_SHIFT (20)
183#define EMAC_MACSTATUS_TXERRCH_MASK (0x7)
184#define EMAC_MACSTATUS_TXERRCH_SHIFT (16)
185#define EMAC_MACSTATUS_RXERRCODE_MASK (0xF000)
186#define EMAC_MACSTATUS_RXERRCODE_SHIFT (12)
187#define EMAC_MACSTATUS_RXERRCH_MASK (0x7)
188#define EMAC_MACSTATUS_RXERRCH_SHIFT (8)
189
190/* EMAC RX register masks */
191#define EMAC_RX_MAX_LEN_MASK (0xFFFF)
192#define EMAC_RX_BUFFER_OFFSET_MASK (0xFFFF)
193
194/* MAC_IN_VECTOR (0x180) register bit fields */
chaithrika@ti.com69ef9692009-10-01 10:25:19 +0000195#define EMAC_DM644X_MAC_IN_VECTOR_HOST_INT BIT(17)
196#define EMAC_DM644X_MAC_IN_VECTOR_STATPEND_INT BIT(16)
197#define EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC BIT(8)
198#define EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC BIT(0)
Anant Golea6286ee2009-05-18 15:19:01 -0700199
200/** NOTE:: For DM646x the IN_VECTOR has changed */
201#define EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC BIT(EMAC_DEF_RX_CH)
202#define EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC BIT(16 + EMAC_DEF_TX_CH)
Sriram43c2ed82009-09-24 19:15:18 +0000203#define EMAC_DM646X_MAC_IN_VECTOR_HOST_INT BIT(26)
204#define EMAC_DM646X_MAC_IN_VECTOR_STATPEND_INT BIT(27)
205
Anant Golea6286ee2009-05-18 15:19:01 -0700206/* CPPI bit positions */
207#define EMAC_CPPI_SOP_BIT BIT(31)
208#define EMAC_CPPI_EOP_BIT BIT(30)
209#define EMAC_CPPI_OWNERSHIP_BIT BIT(29)
210#define EMAC_CPPI_EOQ_BIT BIT(28)
211#define EMAC_CPPI_TEARDOWN_COMPLETE_BIT BIT(27)
212#define EMAC_CPPI_PASS_CRC_BIT BIT(26)
213#define EMAC_RX_BD_BUF_SIZE (0xFFFF)
214#define EMAC_BD_LENGTH_FOR_CACHE (16) /* only CPPI bytes */
215#define EMAC_RX_BD_PKT_LENGTH_MASK (0xFFFF)
216
217/* Max hardware defines */
218#define EMAC_MAX_TXRX_CHANNELS (8) /* Max hardware channels */
219#define EMAC_DEF_MAX_MULTICAST_ADDRESSES (64) /* Max mcast addr's */
220
221/* EMAC Peripheral Device Register Memory Layout structure */
Anant Golea6286ee2009-05-18 15:19:01 -0700222#define EMAC_MACINVECTOR 0x90
223
224#define EMAC_DM646X_MACEOIVECTOR 0x94
225
Anant Golea6286ee2009-05-18 15:19:01 -0700226#define EMAC_MACINTSTATRAW 0xB0
227#define EMAC_MACINTSTATMASKED 0xB4
228#define EMAC_MACINTMASKSET 0xB8
229#define EMAC_MACINTMASKCLEAR 0xBC
230
231#define EMAC_RXMBPENABLE 0x100
232#define EMAC_RXUNICASTSET 0x104
233#define EMAC_RXUNICASTCLEAR 0x108
234#define EMAC_RXMAXLEN 0x10C
235#define EMAC_RXBUFFEROFFSET 0x110
236#define EMAC_RXFILTERLOWTHRESH 0x114
237
238#define EMAC_MACCONTROL 0x160
239#define EMAC_MACSTATUS 0x164
240#define EMAC_EMCONTROL 0x168
241#define EMAC_FIFOCONTROL 0x16C
242#define EMAC_MACCONFIG 0x170
243#define EMAC_SOFTRESET 0x174
244#define EMAC_MACSRCADDRLO 0x1D0
245#define EMAC_MACSRCADDRHI 0x1D4
246#define EMAC_MACHASH1 0x1D8
247#define EMAC_MACHASH2 0x1DC
248#define EMAC_MACADDRLO 0x500
249#define EMAC_MACADDRHI 0x504
250#define EMAC_MACINDEX 0x508
251
Anant Golea6286ee2009-05-18 15:19:01 -0700252/* EMAC statistics registers */
253#define EMAC_RXGOODFRAMES 0x200
254#define EMAC_RXBCASTFRAMES 0x204
255#define EMAC_RXMCASTFRAMES 0x208
256#define EMAC_RXPAUSEFRAMES 0x20C
257#define EMAC_RXCRCERRORS 0x210
258#define EMAC_RXALIGNCODEERRORS 0x214
259#define EMAC_RXOVERSIZED 0x218
260#define EMAC_RXJABBER 0x21C
261#define EMAC_RXUNDERSIZED 0x220
262#define EMAC_RXFRAGMENTS 0x224
263#define EMAC_RXFILTERED 0x228
264#define EMAC_RXQOSFILTERED 0x22C
265#define EMAC_RXOCTETS 0x230
266#define EMAC_TXGOODFRAMES 0x234
267#define EMAC_TXBCASTFRAMES 0x238
268#define EMAC_TXMCASTFRAMES 0x23C
269#define EMAC_TXPAUSEFRAMES 0x240
270#define EMAC_TXDEFERRED 0x244
271#define EMAC_TXCOLLISION 0x248
272#define EMAC_TXSINGLECOLL 0x24C
273#define EMAC_TXMULTICOLL 0x250
274#define EMAC_TXEXCESSIVECOLL 0x254
275#define EMAC_TXLATECOLL 0x258
276#define EMAC_TXUNDERRUN 0x25C
277#define EMAC_TXCARRIERSENSE 0x260
278#define EMAC_TXOCTETS 0x264
279#define EMAC_NETOCTETS 0x280
280#define EMAC_RXSOFOVERRUNS 0x284
281#define EMAC_RXMOFOVERRUNS 0x288
282#define EMAC_RXDMAOVERRUNS 0x28C
283
284/* EMAC DM644x control registers */
285#define EMAC_CTRL_EWCTL (0x4)
286#define EMAC_CTRL_EWINTTCNT (0x8)
287
Sriram84da2652010-07-29 02:33:58 +0000288/* EMAC DM644x control module masks */
289#define EMAC_DM644X_EWINTCNT_MASK 0x1FFFF
290#define EMAC_DM644X_INTMIN_INTVL 0x1
291#define EMAC_DM644X_INTMAX_INTVL (EMAC_DM644X_EWINTCNT_MASK)
292
Anant Golea6286ee2009-05-18 15:19:01 -0700293/* EMAC DM646X control module registers */
Sriram84da2652010-07-29 02:33:58 +0000294#define EMAC_DM646X_CMINTCTRL 0x0C
295#define EMAC_DM646X_CMRXINTEN 0x14
296#define EMAC_DM646X_CMTXINTEN 0x18
297#define EMAC_DM646X_CMRXINTMAX 0x70
298#define EMAC_DM646X_CMTXINTMAX 0x74
299
300/* EMAC DM646X control module masks */
301#define EMAC_DM646X_INTPACEEN (0x3 << 16)
302#define EMAC_DM646X_INTPRESCALE_MASK (0x7FF << 0)
303#define EMAC_DM646X_CMINTMAX_CNT 63
304#define EMAC_DM646X_CMINTMIN_CNT 2
305#define EMAC_DM646X_CMINTMAX_INTVL (1000 / EMAC_DM646X_CMINTMIN_CNT)
306#define EMAC_DM646X_CMINTMIN_INTVL ((1000 / EMAC_DM646X_CMINTMAX_CNT) + 1)
307
Anant Golea6286ee2009-05-18 15:19:01 -0700308
309/* EMAC EOI codes for C0 */
310#define EMAC_DM646X_MAC_EOI_C0_RXEN (0x01)
311#define EMAC_DM646X_MAC_EOI_C0_TXEN (0x02)
312
Sriram0fe74632009-10-07 02:44:30 +0000313/* EMAC Stats Clear Mask */
314#define EMAC_STATS_CLR_MASK (0xFFFFFFFF)
315
Anant Golea6286ee2009-05-18 15:19:01 -0700316/* emac_priv: EMAC private data structure
317 *
318 * EMAC adapter private data structure
319 */
320struct emac_priv {
321 u32 msg_enable;
322 struct net_device *ndev;
323 struct platform_device *pdev;
324 struct napi_struct napi;
325 char mac_addr[6];
Anant Golea6286ee2009-05-18 15:19:01 -0700326 void __iomem *remap_addr;
327 u32 emac_base_phys;
328 void __iomem *emac_base;
329 void __iomem *ctrl_base;
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -0400330 struct cpdma_ctlr *dma;
331 struct cpdma_chan *txchan;
332 struct cpdma_chan *rxchan;
Anant Golea6286ee2009-05-18 15:19:01 -0700333 u32 link; /* 1=link on, 0=link off */
334 u32 speed; /* 0=Auto Neg, 1=No PHY, 10,100, 1000 - mbps */
335 u32 duplex; /* Link duplex: 0=Half, 1=Full */
336 u32 rx_buf_size;
337 u32 isr_count;
Sriram84da2652010-07-29 02:33:58 +0000338 u32 coal_intvl;
339 u32 bus_freq_mhz;
Anant Golea6286ee2009-05-18 15:19:01 -0700340 u8 rmii_en;
341 u8 version;
Anant Golea6286ee2009-05-18 15:19:01 -0700342 u32 mac_hash1;
343 u32 mac_hash2;
344 u32 multicast_hash_cnt[EMAC_NUM_MULTICAST_BITS];
345 u32 rx_addr_type;
Sascha Hauer86d8c072012-01-03 05:27:47 +0000346 atomic_t cur_tx;
Cyril Chemparathy5d69e002010-09-15 10:11:24 -0400347 const char *phy_id;
Heiko Schocher42f59962012-07-17 00:34:24 +0000348#ifdef CONFIG_OF
349 struct device_node *phy_node;
350#endif
Anant Golea6286ee2009-05-18 15:19:01 -0700351 struct phy_device *phydev;
352 spinlock_t lock;
Sriramakrishnan01a9af32009-11-19 15:58:26 +0530353 /*platform specific members*/
354 void (*int_enable) (void);
355 void (*int_disable) (void);
Anant Golea6286ee2009-05-18 15:19:01 -0700356};
357
358/* clock frequency for EMAC */
359static struct clk *emac_clk;
360static unsigned long emac_bus_frequency;
Anant Golea6286ee2009-05-18 15:19:01 -0700361
Anant Golea6286ee2009-05-18 15:19:01 -0700362/* EMAC TX Host Error description strings */
363static char *emac_txhost_errcodes[16] = {
364 "No error", "SOP error", "Ownership bit not set in SOP buffer",
365 "Zero Next Buffer Descriptor Pointer Without EOP",
366 "Zero Buffer Pointer", "Zero Buffer Length", "Packet Length Error",
367 "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
368 "Reserved", "Reserved", "Reserved", "Reserved"
369};
370
371/* EMAC RX Host Error description strings */
372static char *emac_rxhost_errcodes[16] = {
373 "No error", "Reserved", "Ownership bit not set in input buffer",
374 "Reserved", "Zero Buffer Pointer", "Reserved", "Reserved",
375 "Reserved", "Reserved", "Reserved", "Reserved", "Reserved",
376 "Reserved", "Reserved", "Reserved", "Reserved"
377};
378
379/* Helper macros */
380#define emac_read(reg) ioread32(priv->emac_base + (reg))
381#define emac_write(reg, val) iowrite32(val, priv->emac_base + (reg))
382
383#define emac_ctrl_read(reg) ioread32((priv->ctrl_base + (reg)))
384#define emac_ctrl_write(reg, val) iowrite32(val, (priv->ctrl_base + (reg)))
385
Anant Golea6286ee2009-05-18 15:19:01 -0700386/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000387 * emac_dump_regs - Dump important EMAC registers to debug terminal
Anant Golea6286ee2009-05-18 15:19:01 -0700388 * @priv: The DaVinci EMAC private adapter structure
389 *
390 * Executes ethtool set cmd & sets phy mode
391 *
392 */
393static void emac_dump_regs(struct emac_priv *priv)
394{
395 struct device *emac_dev = &priv->ndev->dev;
396
397 /* Print important registers in EMAC */
398 dev_info(emac_dev, "EMAC Basic registers\n");
Srirame9947622010-07-29 02:34:00 +0000399 if (priv->version == EMAC_VERSION_1) {
400 dev_info(emac_dev, "EMAC: EWCTL: %08X, EWINTTCNT: %08X\n",
401 emac_ctrl_read(EMAC_CTRL_EWCTL),
402 emac_ctrl_read(EMAC_CTRL_EWINTTCNT));
403 }
Anant Golea6286ee2009-05-18 15:19:01 -0700404 dev_info(emac_dev, "EMAC: EmuControl:%08X, FifoControl: %08X\n",
405 emac_read(EMAC_EMCONTROL), emac_read(EMAC_FIFOCONTROL));
406 dev_info(emac_dev, "EMAC: MBPEnable:%08X, RXUnicastSet: %08X, "\
407 "RXMaxLen=%08X\n", emac_read(EMAC_RXMBPENABLE),
408 emac_read(EMAC_RXUNICASTSET), emac_read(EMAC_RXMAXLEN));
409 dev_info(emac_dev, "EMAC: MacControl:%08X, MacStatus: %08X, "\
410 "MacConfig=%08X\n", emac_read(EMAC_MACCONTROL),
411 emac_read(EMAC_MACSTATUS), emac_read(EMAC_MACCONFIG));
Anant Golea6286ee2009-05-18 15:19:01 -0700412 dev_info(emac_dev, "EMAC Statistics\n");
413 dev_info(emac_dev, "EMAC: rx_good_frames:%d\n",
414 emac_read(EMAC_RXGOODFRAMES));
415 dev_info(emac_dev, "EMAC: rx_broadcast_frames:%d\n",
416 emac_read(EMAC_RXBCASTFRAMES));
417 dev_info(emac_dev, "EMAC: rx_multicast_frames:%d\n",
418 emac_read(EMAC_RXMCASTFRAMES));
419 dev_info(emac_dev, "EMAC: rx_pause_frames:%d\n",
420 emac_read(EMAC_RXPAUSEFRAMES));
421 dev_info(emac_dev, "EMAC: rx_crcerrors:%d\n",
422 emac_read(EMAC_RXCRCERRORS));
423 dev_info(emac_dev, "EMAC: rx_align_code_errors:%d\n",
424 emac_read(EMAC_RXALIGNCODEERRORS));
425 dev_info(emac_dev, "EMAC: rx_oversized_frames:%d\n",
426 emac_read(EMAC_RXOVERSIZED));
427 dev_info(emac_dev, "EMAC: rx_jabber_frames:%d\n",
428 emac_read(EMAC_RXJABBER));
429 dev_info(emac_dev, "EMAC: rx_undersized_frames:%d\n",
430 emac_read(EMAC_RXUNDERSIZED));
431 dev_info(emac_dev, "EMAC: rx_fragments:%d\n",
432 emac_read(EMAC_RXFRAGMENTS));
433 dev_info(emac_dev, "EMAC: rx_filtered_frames:%d\n",
434 emac_read(EMAC_RXFILTERED));
435 dev_info(emac_dev, "EMAC: rx_qos_filtered_frames:%d\n",
436 emac_read(EMAC_RXQOSFILTERED));
437 dev_info(emac_dev, "EMAC: rx_octets:%d\n",
438 emac_read(EMAC_RXOCTETS));
439 dev_info(emac_dev, "EMAC: tx_goodframes:%d\n",
440 emac_read(EMAC_TXGOODFRAMES));
441 dev_info(emac_dev, "EMAC: tx_bcastframes:%d\n",
442 emac_read(EMAC_TXBCASTFRAMES));
443 dev_info(emac_dev, "EMAC: tx_mcastframes:%d\n",
444 emac_read(EMAC_TXMCASTFRAMES));
445 dev_info(emac_dev, "EMAC: tx_pause_frames:%d\n",
446 emac_read(EMAC_TXPAUSEFRAMES));
447 dev_info(emac_dev, "EMAC: tx_deferred_frames:%d\n",
448 emac_read(EMAC_TXDEFERRED));
449 dev_info(emac_dev, "EMAC: tx_collision_frames:%d\n",
450 emac_read(EMAC_TXCOLLISION));
451 dev_info(emac_dev, "EMAC: tx_single_coll_frames:%d\n",
452 emac_read(EMAC_TXSINGLECOLL));
453 dev_info(emac_dev, "EMAC: tx_mult_coll_frames:%d\n",
454 emac_read(EMAC_TXMULTICOLL));
455 dev_info(emac_dev, "EMAC: tx_excessive_collisions:%d\n",
456 emac_read(EMAC_TXEXCESSIVECOLL));
457 dev_info(emac_dev, "EMAC: tx_late_collisions:%d\n",
458 emac_read(EMAC_TXLATECOLL));
459 dev_info(emac_dev, "EMAC: tx_underrun:%d\n",
460 emac_read(EMAC_TXUNDERRUN));
461 dev_info(emac_dev, "EMAC: tx_carrier_sense_errors:%d\n",
462 emac_read(EMAC_TXCARRIERSENSE));
463 dev_info(emac_dev, "EMAC: tx_octets:%d\n",
464 emac_read(EMAC_TXOCTETS));
465 dev_info(emac_dev, "EMAC: net_octets:%d\n",
466 emac_read(EMAC_NETOCTETS));
467 dev_info(emac_dev, "EMAC: rx_sof_overruns:%d\n",
468 emac_read(EMAC_RXSOFOVERRUNS));
469 dev_info(emac_dev, "EMAC: rx_mof_overruns:%d\n",
470 emac_read(EMAC_RXMOFOVERRUNS));
471 dev_info(emac_dev, "EMAC: rx_dma_overruns:%d\n",
472 emac_read(EMAC_RXDMAOVERRUNS));
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -0400473
474 cpdma_ctlr_dump(priv->dma);
Anant Golea6286ee2009-05-18 15:19:01 -0700475}
476
Anant Golea6286ee2009-05-18 15:19:01 -0700477/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000478 * emac_get_drvinfo - Get EMAC driver information
Anant Golea6286ee2009-05-18 15:19:01 -0700479 * @ndev: The DaVinci EMAC network adapter
480 * @info: ethtool info structure containing name and version
481 *
482 * Returns EMAC driver information (name and version)
483 *
484 */
485static void emac_get_drvinfo(struct net_device *ndev,
486 struct ethtool_drvinfo *info)
487{
488 strcpy(info->driver, emac_version_string);
489 strcpy(info->version, EMAC_MODULE_VERSION);
490}
491
492/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000493 * emac_get_settings - Get EMAC settings
Anant Golea6286ee2009-05-18 15:19:01 -0700494 * @ndev: The DaVinci EMAC network adapter
495 * @ecmd: ethtool command
496 *
497 * Executes ethool get command
498 *
499 */
500static int emac_get_settings(struct net_device *ndev,
501 struct ethtool_cmd *ecmd)
502{
503 struct emac_priv *priv = netdev_priv(ndev);
Cyril Chemparathy5d69e002010-09-15 10:11:24 -0400504 if (priv->phydev)
Anant Golea6286ee2009-05-18 15:19:01 -0700505 return phy_ethtool_gset(priv->phydev, ecmd);
506 else
507 return -EOPNOTSUPP;
508
509}
510
511/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000512 * emac_set_settings - Set EMAC settings
Anant Golea6286ee2009-05-18 15:19:01 -0700513 * @ndev: The DaVinci EMAC network adapter
514 * @ecmd: ethtool command
515 *
516 * Executes ethool set command
517 *
518 */
519static int emac_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
520{
521 struct emac_priv *priv = netdev_priv(ndev);
Cyril Chemparathy5d69e002010-09-15 10:11:24 -0400522 if (priv->phydev)
Anant Golea6286ee2009-05-18 15:19:01 -0700523 return phy_ethtool_sset(priv->phydev, ecmd);
524 else
525 return -EOPNOTSUPP;
526
527}
528
529/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000530 * emac_get_coalesce - Get interrupt coalesce settings for this device
Sriram84da2652010-07-29 02:33:58 +0000531 * @ndev : The DaVinci EMAC network adapter
532 * @coal : ethtool coalesce settings structure
533 *
534 * Fetch the current interrupt coalesce settings
535 *
536 */
537static int emac_get_coalesce(struct net_device *ndev,
538 struct ethtool_coalesce *coal)
539{
540 struct emac_priv *priv = netdev_priv(ndev);
541
542 coal->rx_coalesce_usecs = priv->coal_intvl;
543 return 0;
544
545}
546
547/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000548 * emac_set_coalesce - Set interrupt coalesce settings for this device
Sriram84da2652010-07-29 02:33:58 +0000549 * @ndev : The DaVinci EMAC network adapter
550 * @coal : ethtool coalesce settings structure
551 *
552 * Set interrupt coalesce parameters
553 *
554 */
555static int emac_set_coalesce(struct net_device *ndev,
556 struct ethtool_coalesce *coal)
557{
558 struct emac_priv *priv = netdev_priv(ndev);
559 u32 int_ctrl, num_interrupts = 0;
560 u32 prescale = 0, addnl_dvdr = 1, coal_intvl = 0;
561
562 if (!coal->rx_coalesce_usecs)
563 return -EINVAL;
564
565 coal_intvl = coal->rx_coalesce_usecs;
566
567 switch (priv->version) {
568 case EMAC_VERSION_2:
569 int_ctrl = emac_ctrl_read(EMAC_DM646X_CMINTCTRL);
570 prescale = priv->bus_freq_mhz * 4;
571
572 if (coal_intvl < EMAC_DM646X_CMINTMIN_INTVL)
573 coal_intvl = EMAC_DM646X_CMINTMIN_INTVL;
574
575 if (coal_intvl > EMAC_DM646X_CMINTMAX_INTVL) {
576 /*
577 * Interrupt pacer works with 4us Pulse, we can
578 * throttle further by dilating the 4us pulse.
579 */
580 addnl_dvdr = EMAC_DM646X_INTPRESCALE_MASK / prescale;
581
582 if (addnl_dvdr > 1) {
583 prescale *= addnl_dvdr;
584 if (coal_intvl > (EMAC_DM646X_CMINTMAX_INTVL
585 * addnl_dvdr))
586 coal_intvl = (EMAC_DM646X_CMINTMAX_INTVL
587 * addnl_dvdr);
588 } else {
589 addnl_dvdr = 1;
590 coal_intvl = EMAC_DM646X_CMINTMAX_INTVL;
591 }
592 }
593
594 num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
595
596 int_ctrl |= EMAC_DM646X_INTPACEEN;
597 int_ctrl &= (~EMAC_DM646X_INTPRESCALE_MASK);
598 int_ctrl |= (prescale & EMAC_DM646X_INTPRESCALE_MASK);
599 emac_ctrl_write(EMAC_DM646X_CMINTCTRL, int_ctrl);
600
601 emac_ctrl_write(EMAC_DM646X_CMRXINTMAX, num_interrupts);
602 emac_ctrl_write(EMAC_DM646X_CMTXINTMAX, num_interrupts);
603
604 break;
605 default:
606 int_ctrl = emac_ctrl_read(EMAC_CTRL_EWINTTCNT);
607 int_ctrl &= (~EMAC_DM644X_EWINTCNT_MASK);
608 prescale = coal_intvl * priv->bus_freq_mhz;
609 if (prescale > EMAC_DM644X_EWINTCNT_MASK) {
610 prescale = EMAC_DM644X_EWINTCNT_MASK;
611 coal_intvl = prescale / priv->bus_freq_mhz;
612 }
613 emac_ctrl_write(EMAC_CTRL_EWINTTCNT, (int_ctrl | prescale));
614
615 break;
616 }
617
618 printk(KERN_INFO"Set coalesce to %d usecs.\n", coal_intvl);
619 priv->coal_intvl = coal_intvl;
620
621 return 0;
622
623}
624
625
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000626/* ethtool_ops: DaVinci EMAC Ethtool structure
Anant Golea6286ee2009-05-18 15:19:01 -0700627 *
628 * Ethtool support for EMAC adapter
Anant Golea6286ee2009-05-18 15:19:01 -0700629 */
630static const struct ethtool_ops ethtool_ops = {
631 .get_drvinfo = emac_get_drvinfo,
632 .get_settings = emac_get_settings,
633 .set_settings = emac_set_settings,
634 .get_link = ethtool_op_get_link,
Sriram84da2652010-07-29 02:33:58 +0000635 .get_coalesce = emac_get_coalesce,
636 .set_coalesce = emac_set_coalesce,
Richard Cochran1fa68be2012-04-03 22:59:24 +0000637 .get_ts_info = ethtool_op_get_ts_info,
Anant Golea6286ee2009-05-18 15:19:01 -0700638};
639
640/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000641 * emac_update_phystatus - Update Phy status
Anant Golea6286ee2009-05-18 15:19:01 -0700642 * @priv: The DaVinci EMAC private adapter structure
643 *
644 * Updates phy status and takes action for network queue if required
645 * based upon link status
646 *
647 */
648static void emac_update_phystatus(struct emac_priv *priv)
649{
650 u32 mac_control;
651 u32 new_duplex;
652 u32 cur_duplex;
653 struct net_device *ndev = priv->ndev;
654
655 mac_control = emac_read(EMAC_MACCONTROL);
656 cur_duplex = (mac_control & EMAC_MACCONTROL_FULLDUPLEXEN) ?
657 DUPLEX_FULL : DUPLEX_HALF;
Cyril Chemparathy5d69e002010-09-15 10:11:24 -0400658 if (priv->phydev)
Anant Golea6286ee2009-05-18 15:19:01 -0700659 new_duplex = priv->phydev->duplex;
660 else
661 new_duplex = DUPLEX_FULL;
662
663 /* We get called only if link has changed (speed/duplex/status) */
664 if ((priv->link) && (new_duplex != cur_duplex)) {
665 priv->duplex = new_duplex;
666 if (DUPLEX_FULL == priv->duplex)
667 mac_control |= (EMAC_MACCONTROL_FULLDUPLEXEN);
668 else
669 mac_control &= ~(EMAC_MACCONTROL_FULLDUPLEXEN);
670 }
671
672 if (priv->speed == SPEED_1000 && (priv->version == EMAC_VERSION_2)) {
673 mac_control = emac_read(EMAC_MACCONTROL);
chaithrika@ti.com69ef9692009-10-01 10:25:19 +0000674 mac_control |= (EMAC_DM646X_MACCONTORL_GIG |
Anant Golea6286ee2009-05-18 15:19:01 -0700675 EMAC_DM646X_MACCONTORL_GIGFORCE);
676 } else {
677 /* Clear the GIG bit and GIGFORCE bit */
678 mac_control &= ~(EMAC_DM646X_MACCONTORL_GIGFORCE |
679 EMAC_DM646X_MACCONTORL_GIG);
680
681 if (priv->rmii_en && (priv->speed == SPEED_100))
682 mac_control |= EMAC_MACCONTROL_RMIISPEED_MASK;
683 else
684 mac_control &= ~EMAC_MACCONTROL_RMIISPEED_MASK;
685 }
686
687 /* Update mac_control if changed */
688 emac_write(EMAC_MACCONTROL, mac_control);
689
690 if (priv->link) {
691 /* link ON */
692 if (!netif_carrier_ok(ndev))
693 netif_carrier_on(ndev);
694 /* reactivate the transmit queue if it is stopped */
695 if (netif_running(ndev) && netif_queue_stopped(ndev))
696 netif_wake_queue(ndev);
697 } else {
698 /* link OFF */
699 if (netif_carrier_ok(ndev))
700 netif_carrier_off(ndev);
701 if (!netif_queue_stopped(ndev))
702 netif_stop_queue(ndev);
703 }
704}
705
706/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000707 * hash_get - Calculate hash value from mac address
Anant Golea6286ee2009-05-18 15:19:01 -0700708 * @addr: mac address to delete from hash table
709 *
710 * Calculates hash value from mac address
711 *
712 */
713static u32 hash_get(u8 *addr)
714{
715 u32 hash;
716 u8 tmpval;
717 int cnt;
718 hash = 0;
719
720 for (cnt = 0; cnt < 2; cnt++) {
721 tmpval = *addr++;
722 hash ^= (tmpval >> 2) ^ (tmpval << 4);
723 tmpval = *addr++;
724 hash ^= (tmpval >> 4) ^ (tmpval << 2);
725 tmpval = *addr++;
726 hash ^= (tmpval >> 6) ^ (tmpval);
727 }
728
729 return hash & 0x3F;
730}
731
732/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000733 * hash_add - Hash function to add mac addr from hash table
Anant Golea6286ee2009-05-18 15:19:01 -0700734 * @priv: The DaVinci EMAC private adapter structure
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000735 * @mac_addr: mac address to delete from hash table
Anant Golea6286ee2009-05-18 15:19:01 -0700736 *
737 * Adds mac address to the internal hash table
738 *
739 */
740static int hash_add(struct emac_priv *priv, u8 *mac_addr)
741{
742 struct device *emac_dev = &priv->ndev->dev;
743 u32 rc = 0;
744 u32 hash_bit;
745 u32 hash_value = hash_get(mac_addr);
746
747 if (hash_value >= EMAC_NUM_MULTICAST_BITS) {
748 if (netif_msg_drv(priv)) {
749 dev_err(emac_dev, "DaVinci EMAC: hash_add(): Invalid "\
750 "Hash %08x, should not be greater than %08x",
751 hash_value, (EMAC_NUM_MULTICAST_BITS - 1));
752 }
753 return -1;
754 }
755
756 /* set the hash bit only if not previously set */
757 if (priv->multicast_hash_cnt[hash_value] == 0) {
758 rc = 1; /* hash value changed */
759 if (hash_value < 32) {
760 hash_bit = BIT(hash_value);
761 priv->mac_hash1 |= hash_bit;
762 } else {
763 hash_bit = BIT((hash_value - 32));
764 priv->mac_hash2 |= hash_bit;
765 }
766 }
767
768 /* incr counter for num of mcast addr's mapped to "this" hash bit */
769 ++priv->multicast_hash_cnt[hash_value];
770
771 return rc;
772}
773
774/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000775 * hash_del - Hash function to delete mac addr from hash table
Anant Golea6286ee2009-05-18 15:19:01 -0700776 * @priv: The DaVinci EMAC private adapter structure
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000777 * @mac_addr: mac address to delete from hash table
Anant Golea6286ee2009-05-18 15:19:01 -0700778 *
779 * Removes mac address from the internal hash table
780 *
781 */
782static int hash_del(struct emac_priv *priv, u8 *mac_addr)
783{
784 u32 hash_value;
785 u32 hash_bit;
786
787 hash_value = hash_get(mac_addr);
788 if (priv->multicast_hash_cnt[hash_value] > 0) {
789 /* dec cntr for num of mcast addr's mapped to this hash bit */
790 --priv->multicast_hash_cnt[hash_value];
791 }
792
793 /* if counter still > 0, at least one multicast address refers
794 * to this hash bit. so return 0 */
795 if (priv->multicast_hash_cnt[hash_value] > 0)
796 return 0;
797
798 if (hash_value < 32) {
799 hash_bit = BIT(hash_value);
800 priv->mac_hash1 &= ~hash_bit;
801 } else {
802 hash_bit = BIT((hash_value - 32));
803 priv->mac_hash2 &= ~hash_bit;
804 }
805
806 /* return 1 to indicate change in mac_hash registers reqd */
807 return 1;
808}
809
810/* EMAC multicast operation */
811#define EMAC_MULTICAST_ADD 0
812#define EMAC_MULTICAST_DEL 1
813#define EMAC_ALL_MULTI_SET 2
814#define EMAC_ALL_MULTI_CLR 3
815
816/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000817 * emac_add_mcast - Set multicast address in the EMAC adapter (Internal)
Anant Golea6286ee2009-05-18 15:19:01 -0700818 * @priv: The DaVinci EMAC private adapter structure
819 * @action: multicast operation to perform
820 * mac_addr: mac address to set
821 *
822 * Set multicast addresses in EMAC adapter - internal function
823 *
824 */
825static void emac_add_mcast(struct emac_priv *priv, u32 action, u8 *mac_addr)
826{
827 struct device *emac_dev = &priv->ndev->dev;
828 int update = -1;
829
830 switch (action) {
831 case EMAC_MULTICAST_ADD:
832 update = hash_add(priv, mac_addr);
833 break;
834 case EMAC_MULTICAST_DEL:
835 update = hash_del(priv, mac_addr);
836 break;
837 case EMAC_ALL_MULTI_SET:
838 update = 1;
839 priv->mac_hash1 = EMAC_ALL_MULTI_REG_VALUE;
840 priv->mac_hash2 = EMAC_ALL_MULTI_REG_VALUE;
841 break;
842 case EMAC_ALL_MULTI_CLR:
843 update = 1;
844 priv->mac_hash1 = 0;
845 priv->mac_hash2 = 0;
846 memset(&(priv->multicast_hash_cnt[0]), 0,
847 sizeof(priv->multicast_hash_cnt[0]) *
848 EMAC_NUM_MULTICAST_BITS);
849 break;
850 default:
851 if (netif_msg_drv(priv))
852 dev_err(emac_dev, "DaVinci EMAC: add_mcast"\
853 ": bad operation %d", action);
854 break;
855 }
856
857 /* write to the hardware only if the register status chances */
858 if (update > 0) {
859 emac_write(EMAC_MACHASH1, priv->mac_hash1);
860 emac_write(EMAC_MACHASH2, priv->mac_hash2);
861 }
862}
863
864/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000865 * emac_dev_mcast_set - Set multicast address in the EMAC adapter
Anant Golea6286ee2009-05-18 15:19:01 -0700866 * @ndev: The DaVinci EMAC network adapter
867 *
868 * Set multicast addresses in EMAC adapter
869 *
870 */
871static void emac_dev_mcast_set(struct net_device *ndev)
872{
873 u32 mbp_enable;
874 struct emac_priv *priv = netdev_priv(ndev);
875
876 mbp_enable = emac_read(EMAC_RXMBPENABLE);
877 if (ndev->flags & IFF_PROMISC) {
878 mbp_enable &= (~EMAC_MBP_PROMISCCH(EMAC_DEF_PROM_CH));
879 mbp_enable |= (EMAC_MBP_RXPROMISC);
880 } else {
881 mbp_enable = (mbp_enable & ~EMAC_MBP_RXPROMISC);
882 if ((ndev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000883 netdev_mc_count(ndev) > EMAC_DEF_MAX_MULTICAST_ADDRESSES) {
Anant Golea6286ee2009-05-18 15:19:01 -0700884 mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
885 emac_add_mcast(priv, EMAC_ALL_MULTI_SET, NULL);
886 }
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000887 if (!netdev_mc_empty(ndev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000888 struct netdev_hw_addr *ha;
889
Anant Golea6286ee2009-05-18 15:19:01 -0700890 mbp_enable = (mbp_enable | EMAC_MBP_RXMCAST);
891 emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
892 /* program multicast address list into EMAC hardware */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000893 netdev_for_each_mc_addr(ha, ndev) {
Anant Golea6286ee2009-05-18 15:19:01 -0700894 emac_add_mcast(priv, EMAC_MULTICAST_ADD,
Jiri Pirko22bedad32010-04-01 21:22:57 +0000895 (u8 *) ha->addr);
Anant Golea6286ee2009-05-18 15:19:01 -0700896 }
897 } else {
898 mbp_enable = (mbp_enable & ~EMAC_MBP_RXMCAST);
899 emac_add_mcast(priv, EMAC_ALL_MULTI_CLR, NULL);
900 }
901 }
902 /* Set mbp config register */
903 emac_write(EMAC_RXMBPENABLE, mbp_enable);
904}
905
906/*************************************************************************
907 * EMAC Hardware manipulation
908 *************************************************************************/
909
910/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000911 * emac_int_disable - Disable EMAC module interrupt (from adapter)
Anant Golea6286ee2009-05-18 15:19:01 -0700912 * @priv: The DaVinci EMAC private adapter structure
913 *
914 * Disable EMAC interrupt on the adapter
915 *
916 */
917static void emac_int_disable(struct emac_priv *priv)
918{
919 if (priv->version == EMAC_VERSION_2) {
920 unsigned long flags;
921
922 local_irq_save(flags);
923
924 /* Program C0_Int_En to zero to turn off
925 * interrupts to the CPU */
926 emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0x0);
927 emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0x0);
928 /* NOTE: Rx Threshold and Misc interrupts are not disabled */
Sriramakrishnan01a9af32009-11-19 15:58:26 +0530929 if (priv->int_disable)
930 priv->int_disable();
Anant Golea6286ee2009-05-18 15:19:01 -0700931
932 local_irq_restore(flags);
933
934 } else {
935 /* Set DM644x control registers for interrupt control */
936 emac_ctrl_write(EMAC_CTRL_EWCTL, 0x0);
937 }
938}
939
940/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000941 * emac_int_enable - Enable EMAC module interrupt (from adapter)
Anant Golea6286ee2009-05-18 15:19:01 -0700942 * @priv: The DaVinci EMAC private adapter structure
943 *
944 * Enable EMAC interrupt on the adapter
945 *
946 */
947static void emac_int_enable(struct emac_priv *priv)
948{
949 if (priv->version == EMAC_VERSION_2) {
Sriramakrishnan01a9af32009-11-19 15:58:26 +0530950 if (priv->int_enable)
951 priv->int_enable();
952
Anant Golea6286ee2009-05-18 15:19:01 -0700953 emac_ctrl_write(EMAC_DM646X_CMRXINTEN, 0xff);
954 emac_ctrl_write(EMAC_DM646X_CMTXINTEN, 0xff);
955
956 /* In addition to turning on interrupt Enable, we need
957 * ack by writing appropriate values to the EOI
958 * register */
959
960 /* NOTE: Rx Threshold and Misc interrupts are not enabled */
961
962 /* ack rxen only then a new pulse will be generated */
963 emac_write(EMAC_DM646X_MACEOIVECTOR,
964 EMAC_DM646X_MAC_EOI_C0_RXEN);
965
966 /* ack txen- only then a new pulse will be generated */
967 emac_write(EMAC_DM646X_MACEOIVECTOR,
968 EMAC_DM646X_MAC_EOI_C0_TXEN);
969
970 } else {
971 /* Set DM644x control registers for interrupt control */
972 emac_ctrl_write(EMAC_CTRL_EWCTL, 0x1);
973 }
974}
975
976/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000977 * emac_irq - EMAC interrupt handler
Anant Golea6286ee2009-05-18 15:19:01 -0700978 * @irq: interrupt number
979 * @dev_id: EMAC network adapter data structure ptr
980 *
981 * EMAC Interrupt handler - we only schedule NAPI and not process any packets
982 * here. EVen the interrupt status is checked (TX/RX/Err) in NAPI poll function
983 *
984 * Returns interrupt handled condition
985 */
986static irqreturn_t emac_irq(int irq, void *dev_id)
987{
988 struct net_device *ndev = (struct net_device *)dev_id;
989 struct emac_priv *priv = netdev_priv(ndev);
990
991 ++priv->isr_count;
992 if (likely(netif_running(priv->ndev))) {
993 emac_int_disable(priv);
994 napi_schedule(&priv->napi);
995 } else {
996 /* we are closing down, so dont process anything */
997 }
998 return IRQ_HANDLED;
999}
1000
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001001static struct sk_buff *emac_rx_alloc(struct emac_priv *priv)
1002{
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +00001003 struct sk_buff *skb = netdev_alloc_skb(priv->ndev, priv->rx_buf_size);
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001004 if (WARN_ON(!skb))
1005 return NULL;
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001006 skb_reserve(skb, NET_IP_ALIGN);
1007 return skb;
1008}
1009
1010static void emac_rx_handler(void *token, int len, int status)
1011{
1012 struct sk_buff *skb = token;
1013 struct net_device *ndev = skb->dev;
1014 struct emac_priv *priv = netdev_priv(ndev);
1015 struct device *emac_dev = &ndev->dev;
1016 int ret;
1017
1018 /* free and bail if we are shutting down */
Christian Riesch5d697032012-02-23 01:14:17 +00001019 if (unlikely(!netif_running(ndev))) {
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001020 dev_kfree_skb_any(skb);
1021 return;
1022 }
1023
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001024 /* recycle on receive error */
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001025 if (status < 0) {
1026 ndev->stats.rx_errors++;
1027 goto recycle;
1028 }
1029
1030 /* feed received packet up the stack */
1031 skb_put(skb, len);
1032 skb->protocol = eth_type_trans(skb, ndev);
1033 netif_receive_skb(skb);
1034 ndev->stats.rx_bytes += len;
1035 ndev->stats.rx_packets++;
1036
1037 /* alloc a new packet for receive */
1038 skb = emac_rx_alloc(priv);
1039 if (!skb) {
1040 if (netif_msg_rx_err(priv) && net_ratelimit())
1041 dev_err(emac_dev, "failed rx buffer alloc\n");
1042 return;
1043 }
1044
1045recycle:
1046 ret = cpdma_chan_submit(priv->rxchan, skb, skb->data,
1047 skb_tailroom(skb), GFP_KERNEL);
Christian Riesch5d697032012-02-23 01:14:17 +00001048
1049 WARN_ON(ret == -ENOMEM);
1050 if (unlikely(ret < 0))
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001051 dev_kfree_skb_any(skb);
1052}
1053
1054static void emac_tx_handler(void *token, int len, int status)
1055{
1056 struct sk_buff *skb = token;
1057 struct net_device *ndev = skb->dev;
Sascha Hauer86d8c072012-01-03 05:27:47 +00001058 struct emac_priv *priv = netdev_priv(ndev);
1059
1060 atomic_dec(&priv->cur_tx);
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001061
1062 if (unlikely(netif_queue_stopped(ndev)))
1063 netif_start_queue(ndev);
1064 ndev->stats.tx_packets++;
1065 ndev->stats.tx_bytes += len;
1066 dev_kfree_skb_any(skb);
1067}
1068
Anant Golea6286ee2009-05-18 15:19:01 -07001069/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001070 * emac_dev_xmit - EMAC Transmit function
Anant Golea6286ee2009-05-18 15:19:01 -07001071 * @skb: SKB pointer
1072 * @ndev: The DaVinci EMAC network adapter
1073 *
1074 * Called by the system to transmit a packet - we queue the packet in
1075 * EMAC hardware transmit queue
1076 *
1077 * Returns success(NETDEV_TX_OK) or error code (typically out of desc's)
1078 */
1079static int emac_dev_xmit(struct sk_buff *skb, struct net_device *ndev)
1080{
1081 struct device *emac_dev = &ndev->dev;
1082 int ret_code;
Anant Golea6286ee2009-05-18 15:19:01 -07001083 struct emac_priv *priv = netdev_priv(ndev);
1084
1085 /* If no link, return */
1086 if (unlikely(!priv->link)) {
1087 if (netif_msg_tx_err(priv) && net_ratelimit())
1088 dev_err(emac_dev, "DaVinci EMAC: No link to transmit");
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001089 goto fail_tx;
Anant Golea6286ee2009-05-18 15:19:01 -07001090 }
1091
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001092 ret_code = skb_padto(skb, EMAC_DEF_MIN_ETHPKTSIZE);
1093 if (unlikely(ret_code < 0)) {
1094 if (netif_msg_tx_err(priv) && net_ratelimit())
1095 dev_err(emac_dev, "DaVinci EMAC: packet pad failed");
1096 goto fail_tx;
1097 }
1098
Richard Cochran5bf0c192011-06-19 03:31:45 +00001099 skb_tx_timestamp(skb);
1100
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001101 ret_code = cpdma_chan_submit(priv->txchan, skb, skb->data, skb->len,
1102 GFP_KERNEL);
Anant Golea6286ee2009-05-18 15:19:01 -07001103 if (unlikely(ret_code != 0)) {
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001104 if (netif_msg_tx_err(priv) && net_ratelimit())
1105 dev_err(emac_dev, "DaVinci EMAC: desc submit failed");
1106 goto fail_tx;
Anant Golea6286ee2009-05-18 15:19:01 -07001107 }
1108
Sascha Hauer86d8c072012-01-03 05:27:47 +00001109 if (atomic_inc_return(&priv->cur_tx) >= EMAC_DEF_TX_NUM_DESC)
1110 netif_stop_queue(ndev);
1111
Anant Golea6286ee2009-05-18 15:19:01 -07001112 return NETDEV_TX_OK;
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001113
1114fail_tx:
1115 ndev->stats.tx_dropped++;
1116 netif_stop_queue(ndev);
1117 return NETDEV_TX_BUSY;
Anant Golea6286ee2009-05-18 15:19:01 -07001118}
1119
1120/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001121 * emac_dev_tx_timeout - EMAC Transmit timeout function
Anant Golea6286ee2009-05-18 15:19:01 -07001122 * @ndev: The DaVinci EMAC network adapter
1123 *
1124 * Called when system detects that a skb timeout period has expired
1125 * potentially due to a fault in the adapter in not being able to send
1126 * it out on the wire. We teardown the TX channel assuming a hardware
1127 * error and re-initialize the TX channel for hardware operation
1128 *
1129 */
1130static void emac_dev_tx_timeout(struct net_device *ndev)
1131{
1132 struct emac_priv *priv = netdev_priv(ndev);
1133 struct device *emac_dev = &ndev->dev;
1134
1135 if (netif_msg_tx_err(priv))
1136 dev_err(emac_dev, "DaVinci EMAC: xmit timeout, restarting TX");
1137
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001138 emac_dump_regs(priv);
1139
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00001140 ndev->stats.tx_errors++;
Anant Golea6286ee2009-05-18 15:19:01 -07001141 emac_int_disable(priv);
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001142 cpdma_chan_stop(priv->txchan);
1143 cpdma_chan_start(priv->txchan);
Anant Golea6286ee2009-05-18 15:19:01 -07001144 emac_int_enable(priv);
1145}
1146
1147/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001148 * emac_set_type0addr - Set EMAC Type0 mac address
Anant Golea6286ee2009-05-18 15:19:01 -07001149 * @priv: The DaVinci EMAC private adapter structure
1150 * @ch: RX channel number
1151 * @mac_addr: MAC address to set in device
1152 *
1153 * Called internally to set Type0 mac address of the adapter (Device)
1154 *
1155 * Returns success (0) or appropriate error code (none as of now)
1156 */
1157static void emac_set_type0addr(struct emac_priv *priv, u32 ch, char *mac_addr)
1158{
1159 u32 val;
1160 val = ((mac_addr[5] << 8) | (mac_addr[4]));
1161 emac_write(EMAC_MACSRCADDRLO, val);
1162
1163 val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1164 (mac_addr[1] << 8) | (mac_addr[0]));
1165 emac_write(EMAC_MACSRCADDRHI, val);
1166 val = emac_read(EMAC_RXUNICASTSET);
1167 val |= BIT(ch);
1168 emac_write(EMAC_RXUNICASTSET, val);
1169 val = emac_read(EMAC_RXUNICASTCLEAR);
1170 val &= ~BIT(ch);
1171 emac_write(EMAC_RXUNICASTCLEAR, val);
1172}
1173
1174/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001175 * emac_set_type1addr - Set EMAC Type1 mac address
Anant Golea6286ee2009-05-18 15:19:01 -07001176 * @priv: The DaVinci EMAC private adapter structure
1177 * @ch: RX channel number
1178 * @mac_addr: MAC address to set in device
1179 *
1180 * Called internally to set Type1 mac address of the adapter (Device)
1181 *
1182 * Returns success (0) or appropriate error code (none as of now)
1183 */
1184static void emac_set_type1addr(struct emac_priv *priv, u32 ch, char *mac_addr)
1185{
1186 u32 val;
1187 emac_write(EMAC_MACINDEX, ch);
1188 val = ((mac_addr[5] << 8) | mac_addr[4]);
1189 emac_write(EMAC_MACADDRLO, val);
1190 val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1191 (mac_addr[1] << 8) | (mac_addr[0]));
1192 emac_write(EMAC_MACADDRHI, val);
1193 emac_set_type0addr(priv, ch, mac_addr);
1194}
1195
1196/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001197 * emac_set_type2addr - Set EMAC Type2 mac address
Anant Golea6286ee2009-05-18 15:19:01 -07001198 * @priv: The DaVinci EMAC private adapter structure
1199 * @ch: RX channel number
1200 * @mac_addr: MAC address to set in device
1201 * @index: index into RX address entries
1202 * @match: match parameter for RX address matching logic
1203 *
1204 * Called internally to set Type2 mac address of the adapter (Device)
1205 *
1206 * Returns success (0) or appropriate error code (none as of now)
1207 */
1208static void emac_set_type2addr(struct emac_priv *priv, u32 ch,
1209 char *mac_addr, int index, int match)
1210{
1211 u32 val;
1212 emac_write(EMAC_MACINDEX, index);
1213 val = ((mac_addr[3] << 24) | (mac_addr[2] << 16) | \
1214 (mac_addr[1] << 8) | (mac_addr[0]));
1215 emac_write(EMAC_MACADDRHI, val);
1216 val = ((mac_addr[5] << 8) | mac_addr[4] | ((ch & 0x7) << 16) | \
1217 (match << 19) | BIT(20));
1218 emac_write(EMAC_MACADDRLO, val);
1219 emac_set_type0addr(priv, ch, mac_addr);
1220}
1221
1222/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001223 * emac_setmac - Set mac address in the adapter (internal function)
Anant Golea6286ee2009-05-18 15:19:01 -07001224 * @priv: The DaVinci EMAC private adapter structure
1225 * @ch: RX channel number
1226 * @mac_addr: MAC address to set in device
1227 *
1228 * Called internally to set the mac address of the adapter (Device)
1229 *
1230 * Returns success (0) or appropriate error code (none as of now)
1231 */
1232static void emac_setmac(struct emac_priv *priv, u32 ch, char *mac_addr)
1233{
1234 struct device *emac_dev = &priv->ndev->dev;
1235
1236 if (priv->rx_addr_type == 0) {
1237 emac_set_type0addr(priv, ch, mac_addr);
1238 } else if (priv->rx_addr_type == 1) {
1239 u32 cnt;
1240 for (cnt = 0; cnt < EMAC_MAX_TXRX_CHANNELS; cnt++)
1241 emac_set_type1addr(priv, ch, mac_addr);
1242 } else if (priv->rx_addr_type == 2) {
1243 emac_set_type2addr(priv, ch, mac_addr, ch, 1);
1244 emac_set_type0addr(priv, ch, mac_addr);
1245 } else {
1246 if (netif_msg_drv(priv))
1247 dev_err(emac_dev, "DaVinci EMAC: Wrong addressing\n");
1248 }
1249}
1250
1251/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001252 * emac_dev_setmac_addr - Set mac address in the adapter
Anant Golea6286ee2009-05-18 15:19:01 -07001253 * @ndev: The DaVinci EMAC network adapter
1254 * @addr: MAC address to set in device
1255 *
1256 * Called by the system to set the mac address of the adapter (Device)
1257 *
1258 * Returns success (0) or appropriate error code (none as of now)
1259 */
1260static int emac_dev_setmac_addr(struct net_device *ndev, void *addr)
1261{
1262 struct emac_priv *priv = netdev_priv(ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07001263 struct device *emac_dev = &priv->ndev->dev;
1264 struct sockaddr *sa = addr;
Anant Golea6286ee2009-05-18 15:19:01 -07001265
Pablo Bitton64c81652009-07-07 19:11:10 -07001266 if (!is_valid_ether_addr(sa->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00001267 return -EADDRNOTAVAIL;
Pablo Bitton64c81652009-07-07 19:11:10 -07001268
Anant Golea6286ee2009-05-18 15:19:01 -07001269 /* Store mac addr in priv and rx channel and set it in EMAC hw */
1270 memcpy(priv->mac_addr, sa->sa_data, ndev->addr_len);
Anant Golea6286ee2009-05-18 15:19:01 -07001271 memcpy(ndev->dev_addr, sa->sa_data, ndev->addr_len);
Danny Kukawkabaf1d372012-02-17 05:43:24 +00001272 ndev->addr_assign_type &= ~NET_ADDR_RANDOM;
Pablo Bitton64c81652009-07-07 19:11:10 -07001273
Pablo Bitton64c81652009-07-07 19:11:10 -07001274 /* MAC address is configured only after the interface is enabled. */
1275 if (netif_running(ndev)) {
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001276 emac_setmac(priv, EMAC_DEF_RX_CH, priv->mac_addr);
Pablo Bitton64c81652009-07-07 19:11:10 -07001277 }
Anant Golea6286ee2009-05-18 15:19:01 -07001278
1279 if (netif_msg_drv(priv))
Chaithrika U S5c726162009-06-03 21:54:29 -07001280 dev_notice(emac_dev, "DaVinci EMAC: emac_dev_setmac_addr %pM\n",
1281 priv->mac_addr);
Anant Golea6286ee2009-05-18 15:19:01 -07001282
1283 return 0;
1284}
1285
1286/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001287 * emac_hw_enable - Enable EMAC hardware for packet transmission/reception
Anant Golea6286ee2009-05-18 15:19:01 -07001288 * @priv: The DaVinci EMAC private adapter structure
1289 *
1290 * Enables EMAC hardware for packet processing - enables PHY, enables RX
1291 * for packet reception and enables device interrupts and then NAPI
1292 *
1293 * Returns success (0) or appropriate error code (none right now)
1294 */
1295static int emac_hw_enable(struct emac_priv *priv)
1296{
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001297 u32 val, mbp_enable, mac_control;
Anant Golea6286ee2009-05-18 15:19:01 -07001298
1299 /* Soft reset */
1300 emac_write(EMAC_SOFTRESET, 1);
1301 while (emac_read(EMAC_SOFTRESET))
1302 cpu_relax();
1303
1304 /* Disable interrupt & Set pacing for more interrupts initially */
1305 emac_int_disable(priv);
1306
1307 /* Full duplex enable bit set when auto negotiation happens */
1308 mac_control =
1309 (((EMAC_DEF_TXPRIO_FIXED) ? (EMAC_MACCONTROL_TXPTYPE) : 0x0) |
1310 ((priv->speed == 1000) ? EMAC_MACCONTROL_GIGABITEN : 0x0) |
1311 ((EMAC_DEF_TXPACING_EN) ? (EMAC_MACCONTROL_TXPACEEN) : 0x0) |
1312 ((priv->duplex == DUPLEX_FULL) ? 0x1 : 0));
1313 emac_write(EMAC_MACCONTROL, mac_control);
1314
1315 mbp_enable =
1316 (((EMAC_DEF_PASS_CRC) ? (EMAC_RXMBP_PASSCRC_MASK) : 0x0) |
1317 ((EMAC_DEF_QOS_EN) ? (EMAC_RXMBP_QOSEN_MASK) : 0x0) |
1318 ((EMAC_DEF_NO_BUFF_CHAIN) ? (EMAC_RXMBP_NOCHAIN_MASK) : 0x0) |
1319 ((EMAC_DEF_MACCTRL_FRAME_EN) ? (EMAC_RXMBP_CMFEN_MASK) : 0x0) |
1320 ((EMAC_DEF_SHORT_FRAME_EN) ? (EMAC_RXMBP_CSFEN_MASK) : 0x0) |
1321 ((EMAC_DEF_ERROR_FRAME_EN) ? (EMAC_RXMBP_CEFEN_MASK) : 0x0) |
1322 ((EMAC_DEF_PROM_EN) ? (EMAC_RXMBP_CAFEN_MASK) : 0x0) |
1323 ((EMAC_DEF_PROM_CH & EMAC_RXMBP_CHMASK) << \
1324 EMAC_RXMBP_PROMCH_SHIFT) |
1325 ((EMAC_DEF_BCAST_EN) ? (EMAC_RXMBP_BROADEN_MASK) : 0x0) |
1326 ((EMAC_DEF_BCAST_CH & EMAC_RXMBP_CHMASK) << \
1327 EMAC_RXMBP_BROADCH_SHIFT) |
1328 ((EMAC_DEF_MCAST_EN) ? (EMAC_RXMBP_MULTIEN_MASK) : 0x0) |
1329 ((EMAC_DEF_MCAST_CH & EMAC_RXMBP_CHMASK) << \
1330 EMAC_RXMBP_MULTICH_SHIFT));
1331 emac_write(EMAC_RXMBPENABLE, mbp_enable);
1332 emac_write(EMAC_RXMAXLEN, (EMAC_DEF_MAX_FRAME_SIZE &
1333 EMAC_RX_MAX_LEN_MASK));
1334 emac_write(EMAC_RXBUFFEROFFSET, (EMAC_DEF_BUFFER_OFFSET &
1335 EMAC_RX_BUFFER_OFFSET_MASK));
1336 emac_write(EMAC_RXFILTERLOWTHRESH, 0);
1337 emac_write(EMAC_RXUNICASTCLEAR, EMAC_RX_UNICAST_CLEAR_ALL);
1338 priv->rx_addr_type = (emac_read(EMAC_MACCONFIG) >> 8) & 0xFF;
1339
Anant Golea6286ee2009-05-18 15:19:01 -07001340 emac_write(EMAC_MACINTMASKSET, EMAC_MAC_HOST_ERR_INTMASK_VAL);
1341
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001342 emac_setmac(priv, EMAC_DEF_RX_CH, priv->mac_addr);
Anant Golea6286ee2009-05-18 15:19:01 -07001343
1344 /* Enable MII */
1345 val = emac_read(EMAC_MACCONTROL);
chaithrika@ti.com69ef9692009-10-01 10:25:19 +00001346 val |= (EMAC_MACCONTROL_GMIIEN);
Anant Golea6286ee2009-05-18 15:19:01 -07001347 emac_write(EMAC_MACCONTROL, val);
1348
1349 /* Enable NAPI and interrupts */
1350 napi_enable(&priv->napi);
1351 emac_int_enable(priv);
1352 return 0;
1353
1354}
1355
1356/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001357 * emac_poll - EMAC NAPI Poll function
Anant Golea6286ee2009-05-18 15:19:01 -07001358 * @ndev: The DaVinci EMAC network adapter
1359 * @budget: Number of receive packets to process (as told by NAPI layer)
1360 *
1361 * NAPI Poll function implemented to process packets as per budget. We check
1362 * the type of interrupt on the device and accordingly call the TX or RX
1363 * packet processing functions. We follow the budget for RX processing and
1364 * also put a cap on number of TX pkts processed through config param. The
1365 * NAPI schedule function is called if more packets pending.
1366 *
1367 * Returns number of packets received (in most cases; else TX pkts - rarely)
1368 */
1369static int emac_poll(struct napi_struct *napi, int budget)
1370{
1371 unsigned int mask;
1372 struct emac_priv *priv = container_of(napi, struct emac_priv, napi);
1373 struct net_device *ndev = priv->ndev;
1374 struct device *emac_dev = &ndev->dev;
1375 u32 status = 0;
Sriram3725b1f2010-07-29 02:33:59 +00001376 u32 num_tx_pkts = 0, num_rx_pkts = 0;
Anant Golea6286ee2009-05-18 15:19:01 -07001377
Anant Golea6286ee2009-05-18 15:19:01 -07001378 /* Check interrupt vectors and call packet processing */
1379 status = emac_read(EMAC_MACINVECTOR);
1380
1381 mask = EMAC_DM644X_MAC_IN_VECTOR_TX_INT_VEC;
1382
1383 if (priv->version == EMAC_VERSION_2)
1384 mask = EMAC_DM646X_MAC_IN_VECTOR_TX_INT_VEC;
1385
1386 if (status & mask) {
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001387 num_tx_pkts = cpdma_chan_process(priv->txchan,
1388 EMAC_DEF_TX_MAX_SERVICE);
Anant Golea6286ee2009-05-18 15:19:01 -07001389 } /* TX processing */
1390
Anant Golea6286ee2009-05-18 15:19:01 -07001391 mask = EMAC_DM644X_MAC_IN_VECTOR_RX_INT_VEC;
1392
1393 if (priv->version == EMAC_VERSION_2)
1394 mask = EMAC_DM646X_MAC_IN_VECTOR_RX_INT_VEC;
1395
1396 if (status & mask) {
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001397 num_rx_pkts = cpdma_chan_process(priv->rxchan, budget);
Anant Golea6286ee2009-05-18 15:19:01 -07001398 } /* RX processing */
1399
Sriram43c2ed82009-09-24 19:15:18 +00001400 mask = EMAC_DM644X_MAC_IN_VECTOR_HOST_INT;
1401 if (priv->version == EMAC_VERSION_2)
1402 mask = EMAC_DM646X_MAC_IN_VECTOR_HOST_INT;
1403
1404 if (unlikely(status & mask)) {
Anant Golea6286ee2009-05-18 15:19:01 -07001405 u32 ch, cause;
1406 dev_err(emac_dev, "DaVinci EMAC: Fatal Hardware Error\n");
1407 netif_stop_queue(ndev);
1408 napi_disable(&priv->napi);
1409
1410 status = emac_read(EMAC_MACSTATUS);
1411 cause = ((status & EMAC_MACSTATUS_TXERRCODE_MASK) >>
1412 EMAC_MACSTATUS_TXERRCODE_SHIFT);
1413 if (cause) {
1414 ch = ((status & EMAC_MACSTATUS_TXERRCH_MASK) >>
1415 EMAC_MACSTATUS_TXERRCH_SHIFT);
1416 if (net_ratelimit()) {
1417 dev_err(emac_dev, "TX Host error %s on ch=%d\n",
1418 &emac_txhost_errcodes[cause][0], ch);
1419 }
1420 }
1421 cause = ((status & EMAC_MACSTATUS_RXERRCODE_MASK) >>
1422 EMAC_MACSTATUS_RXERRCODE_SHIFT);
1423 if (cause) {
1424 ch = ((status & EMAC_MACSTATUS_RXERRCH_MASK) >>
1425 EMAC_MACSTATUS_RXERRCH_SHIFT);
1426 if (netif_msg_hw(priv) && net_ratelimit())
1427 dev_err(emac_dev, "RX Host error %s on ch=%d\n",
1428 &emac_rxhost_errcodes[cause][0], ch);
1429 }
Sriram3725b1f2010-07-29 02:33:59 +00001430 } else if (num_rx_pkts < budget) {
1431 napi_complete(napi);
1432 emac_int_enable(priv);
1433 }
Anant Golea6286ee2009-05-18 15:19:01 -07001434
Sriram3725b1f2010-07-29 02:33:59 +00001435 return num_rx_pkts;
Anant Golea6286ee2009-05-18 15:19:01 -07001436}
1437
1438#ifdef CONFIG_NET_POLL_CONTROLLER
1439/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001440 * emac_poll_controller - EMAC Poll controller function
Anant Golea6286ee2009-05-18 15:19:01 -07001441 * @ndev: The DaVinci EMAC network adapter
1442 *
1443 * Polled functionality used by netconsole and others in non interrupt mode
1444 *
1445 */
1446void emac_poll_controller(struct net_device *ndev)
1447{
1448 struct emac_priv *priv = netdev_priv(ndev);
1449
1450 emac_int_disable(priv);
Tonyliuc8ee5532009-11-04 05:45:02 -08001451 emac_irq(ndev->irq, ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07001452 emac_int_enable(priv);
1453}
1454#endif
1455
Anant Golea6286ee2009-05-18 15:19:01 -07001456static void emac_adjust_link(struct net_device *ndev)
1457{
1458 struct emac_priv *priv = netdev_priv(ndev);
1459 struct phy_device *phydev = priv->phydev;
1460 unsigned long flags;
1461 int new_state = 0;
1462
1463 spin_lock_irqsave(&priv->lock, flags);
1464
1465 if (phydev->link) {
1466 /* check the mode of operation - full/half duplex */
1467 if (phydev->duplex != priv->duplex) {
1468 new_state = 1;
1469 priv->duplex = phydev->duplex;
1470 }
1471 if (phydev->speed != priv->speed) {
1472 new_state = 1;
1473 priv->speed = phydev->speed;
1474 }
1475 if (!priv->link) {
1476 new_state = 1;
1477 priv->link = 1;
1478 }
1479
1480 } else if (priv->link) {
1481 new_state = 1;
1482 priv->link = 0;
1483 priv->speed = 0;
1484 priv->duplex = ~0;
1485 }
1486 if (new_state) {
1487 emac_update_phystatus(priv);
1488 phy_print_status(priv->phydev);
1489 }
1490
1491 spin_unlock_irqrestore(&priv->lock, flags);
1492}
1493
1494/*************************************************************************
1495 * Linux Driver Model
1496 *************************************************************************/
1497
1498/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001499 * emac_devioctl - EMAC adapter ioctl
Anant Golea6286ee2009-05-18 15:19:01 -07001500 * @ndev: The DaVinci EMAC network adapter
1501 * @ifrq: request parameter
1502 * @cmd: command parameter
1503 *
1504 * EMAC driver ioctl function
1505 *
1506 * Returns success(0) or appropriate error code
1507 */
1508static int emac_devioctl(struct net_device *ndev, struct ifreq *ifrq, int cmd)
1509{
Richard Cochranfb290cd2011-06-12 02:19:00 +00001510 struct emac_priv *priv = netdev_priv(ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07001511
1512 if (!(netif_running(ndev)))
1513 return -EINVAL;
1514
1515 /* TODO: Add phy read and write and private statistics get feature */
1516
Richard Cochranfb290cd2011-06-12 02:19:00 +00001517 return phy_mii_ioctl(priv->phydev, ifrq, cmd);
Anant Golea6286ee2009-05-18 15:19:01 -07001518}
1519
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04001520static int match_first_device(struct device *dev, void *data)
1521{
Anatolij Gustschin1ab8be42012-04-23 12:06:43 +00001522 return !strncmp(dev_name(dev), "davinci_mdio", 12);
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04001523}
1524
Anant Golea6286ee2009-05-18 15:19:01 -07001525/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001526 * emac_dev_open - EMAC device open
Anant Golea6286ee2009-05-18 15:19:01 -07001527 * @ndev: The DaVinci EMAC network adapter
1528 *
1529 * Called when system wants to start the interface. We init TX/RX channels
1530 * and enable the hardware for packet reception/transmission and start the
1531 * network queue.
1532 *
1533 * Returns 0 for a successful open, or appropriate error code
1534 */
1535static int emac_dev_open(struct net_device *ndev)
1536{
1537 struct device *emac_dev = &ndev->dev;
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001538 u32 cnt;
Anant Golea6286ee2009-05-18 15:19:01 -07001539 struct resource *res;
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001540 int q, m, ret;
Anant Golea6286ee2009-05-18 15:19:01 -07001541 int i = 0;
1542 int k = 0;
1543 struct emac_priv *priv = netdev_priv(ndev);
1544
1545 netif_carrier_off(ndev);
Dan Carpenter4d27b872010-03-02 21:07:24 +00001546 for (cnt = 0; cnt < ETH_ALEN; cnt++)
Anant Golea6286ee2009-05-18 15:19:01 -07001547 ndev->dev_addr[cnt] = priv->mac_addr[cnt];
1548
1549 /* Configuration items */
1550 priv->rx_buf_size = EMAC_DEF_MAX_FRAME_SIZE + NET_IP_ALIGN;
1551
Anant Golea6286ee2009-05-18 15:19:01 -07001552 priv->mac_hash1 = 0;
1553 priv->mac_hash2 = 0;
1554 emac_write(EMAC_MACHASH1, 0);
1555 emac_write(EMAC_MACHASH2, 0);
1556
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001557 for (i = 0; i < EMAC_DEF_RX_NUM_DESC; i++) {
1558 struct sk_buff *skb = emac_rx_alloc(priv);
1559
1560 if (!skb)
1561 break;
1562
1563 ret = cpdma_chan_submit(priv->rxchan, skb, skb->data,
1564 skb_tailroom(skb), GFP_KERNEL);
1565 if (WARN_ON(ret < 0))
1566 break;
Anant Golea6286ee2009-05-18 15:19:01 -07001567 }
1568
1569 /* Request IRQ */
1570
1571 while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
1572 for (i = res->start; i <= res->end; i++) {
1573 if (request_irq(i, emac_irq, IRQF_DISABLED,
1574 ndev->name, ndev))
1575 goto rollback;
1576 }
1577 k++;
1578 }
1579
1580 /* Start/Enable EMAC hardware */
1581 emac_hw_enable(priv);
1582
Sriram84da2652010-07-29 02:33:58 +00001583 /* Enable Interrupt pacing if configured */
1584 if (priv->coal_intvl != 0) {
1585 struct ethtool_coalesce coal;
1586
1587 coal.rx_coalesce_usecs = (priv->coal_intvl << 4);
1588 emac_set_coalesce(ndev, &coal);
1589 }
1590
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001591 cpdma_ctlr_start(priv->dma);
1592
Anant Golea6286ee2009-05-18 15:19:01 -07001593 priv->phydev = NULL;
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04001594 /* use the first phy on the bus if pdata did not give us a phy id */
1595 if (!priv->phy_id) {
1596 struct device *phy;
Anant Golea6286ee2009-05-18 15:19:01 -07001597
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04001598 phy = bus_find_device(&mdio_bus_type, NULL, NULL,
1599 match_first_device);
1600 if (phy)
1601 priv->phy_id = dev_name(phy);
1602 }
Anant Golea6286ee2009-05-18 15:19:01 -07001603
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04001604 if (priv->phy_id && *priv->phy_id) {
1605 priv->phydev = phy_connect(ndev, priv->phy_id,
1606 &emac_adjust_link, 0,
1607 PHY_INTERFACE_MODE_MII);
Anant Golea6286ee2009-05-18 15:19:01 -07001608
1609 if (IS_ERR(priv->phydev)) {
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04001610 dev_err(emac_dev, "could not connect to phy %s\n",
1611 priv->phy_id);
Julia Lawallcb0a1782012-02-02 04:53:01 +00001612 ret = PTR_ERR(priv->phydev);
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04001613 priv->phydev = NULL;
Julia Lawallcb0a1782012-02-02 04:53:01 +00001614 return ret;
Anant Golea6286ee2009-05-18 15:19:01 -07001615 }
1616
1617 priv->link = 0;
1618 priv->speed = 0;
1619 priv->duplex = ~0;
1620
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04001621 dev_info(emac_dev, "attached PHY driver [%s] "
1622 "(mii_bus:phy_addr=%s, id=%x)\n",
Anant Golea6286ee2009-05-18 15:19:01 -07001623 priv->phydev->drv->name, dev_name(&priv->phydev->dev),
1624 priv->phydev->phy_id);
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04001625 } else {
Anant Golea6286ee2009-05-18 15:19:01 -07001626 /* No PHY , fix the link, speed and duplex settings */
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04001627 dev_notice(emac_dev, "no phy, defaulting to 100/full\n");
Anant Golea6286ee2009-05-18 15:19:01 -07001628 priv->link = 1;
1629 priv->speed = SPEED_100;
1630 priv->duplex = DUPLEX_FULL;
1631 emac_update_phystatus(priv);
1632 }
1633
1634 if (!netif_running(ndev)) /* debug only - to avoid compiler warning */
1635 emac_dump_regs(priv);
1636
1637 if (netif_msg_drv(priv))
1638 dev_notice(emac_dev, "DaVinci EMAC: Opened %s\n", ndev->name);
1639
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04001640 if (priv->phydev)
Anant Golea6286ee2009-05-18 15:19:01 -07001641 phy_start(priv->phydev);
1642
1643 return 0;
1644
1645rollback:
1646
1647 dev_err(emac_dev, "DaVinci EMAC: request_irq() failed");
1648
1649 for (q = k; k >= 0; k--) {
1650 for (m = i; m >= res->start; m--)
1651 free_irq(m, ndev);
1652 res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k-1);
1653 m = res->end;
1654 }
1655 return -EBUSY;
1656}
1657
1658/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001659 * emac_dev_stop - EMAC device stop
Anant Golea6286ee2009-05-18 15:19:01 -07001660 * @ndev: The DaVinci EMAC network adapter
1661 *
1662 * Called when system wants to stop or down the interface. We stop the network
1663 * queue, disable interrupts and cleanup TX/RX channels.
1664 *
1665 * We return the statistics in net_device_stats structure pulled from emac
1666 */
1667static int emac_dev_stop(struct net_device *ndev)
1668{
1669 struct resource *res;
1670 int i = 0;
1671 int irq_num;
1672 struct emac_priv *priv = netdev_priv(ndev);
1673 struct device *emac_dev = &ndev->dev;
1674
1675 /* inform the upper layers. */
1676 netif_stop_queue(ndev);
1677 napi_disable(&priv->napi);
1678
1679 netif_carrier_off(ndev);
1680 emac_int_disable(priv);
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001681 cpdma_ctlr_stop(priv->dma);
Anant Golea6286ee2009-05-18 15:19:01 -07001682 emac_write(EMAC_SOFTRESET, 1);
1683
1684 if (priv->phydev)
1685 phy_disconnect(priv->phydev);
1686
1687 /* Free IRQ */
1688 while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, i))) {
1689 for (irq_num = res->start; irq_num <= res->end; irq_num++)
1690 free_irq(irq_num, priv->ndev);
1691 i++;
1692 }
1693
1694 if (netif_msg_drv(priv))
1695 dev_notice(emac_dev, "DaVinci EMAC: %s stopped\n", ndev->name);
1696
1697 return 0;
1698}
1699
1700/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001701 * emac_dev_getnetstats - EMAC get statistics function
Anant Golea6286ee2009-05-18 15:19:01 -07001702 * @ndev: The DaVinci EMAC network adapter
1703 *
1704 * Called when system wants to get statistics from the device.
1705 *
1706 * We return the statistics in net_device_stats structure pulled from emac
1707 */
1708static struct net_device_stats *emac_dev_getnetstats(struct net_device *ndev)
1709{
1710 struct emac_priv *priv = netdev_priv(ndev);
Sriram0fe74632009-10-07 02:44:30 +00001711 u32 mac_control;
1712 u32 stats_clear_mask;
Anant Golea6286ee2009-05-18 15:19:01 -07001713
1714 /* update emac hardware stats and reset the registers*/
1715
Sriram0fe74632009-10-07 02:44:30 +00001716 mac_control = emac_read(EMAC_MACCONTROL);
1717
1718 if (mac_control & EMAC_MACCONTROL_GMIIEN)
1719 stats_clear_mask = EMAC_STATS_CLR_MASK;
1720 else
1721 stats_clear_mask = 0;
1722
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00001723 ndev->stats.multicast += emac_read(EMAC_RXMCASTFRAMES);
Sriram0fe74632009-10-07 02:44:30 +00001724 emac_write(EMAC_RXMCASTFRAMES, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07001725
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00001726 ndev->stats.collisions += (emac_read(EMAC_TXCOLLISION) +
Anant Golea6286ee2009-05-18 15:19:01 -07001727 emac_read(EMAC_TXSINGLECOLL) +
1728 emac_read(EMAC_TXMULTICOLL));
Sriram0fe74632009-10-07 02:44:30 +00001729 emac_write(EMAC_TXCOLLISION, stats_clear_mask);
1730 emac_write(EMAC_TXSINGLECOLL, stats_clear_mask);
1731 emac_write(EMAC_TXMULTICOLL, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07001732
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00001733 ndev->stats.rx_length_errors += (emac_read(EMAC_RXOVERSIZED) +
Anant Golea6286ee2009-05-18 15:19:01 -07001734 emac_read(EMAC_RXJABBER) +
1735 emac_read(EMAC_RXUNDERSIZED));
Sriram0fe74632009-10-07 02:44:30 +00001736 emac_write(EMAC_RXOVERSIZED, stats_clear_mask);
1737 emac_write(EMAC_RXJABBER, stats_clear_mask);
1738 emac_write(EMAC_RXUNDERSIZED, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07001739
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00001740 ndev->stats.rx_over_errors += (emac_read(EMAC_RXSOFOVERRUNS) +
Anant Golea6286ee2009-05-18 15:19:01 -07001741 emac_read(EMAC_RXMOFOVERRUNS));
Sriram0fe74632009-10-07 02:44:30 +00001742 emac_write(EMAC_RXSOFOVERRUNS, stats_clear_mask);
1743 emac_write(EMAC_RXMOFOVERRUNS, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07001744
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00001745 ndev->stats.rx_fifo_errors += emac_read(EMAC_RXDMAOVERRUNS);
Sriram0fe74632009-10-07 02:44:30 +00001746 emac_write(EMAC_RXDMAOVERRUNS, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07001747
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00001748 ndev->stats.tx_carrier_errors +=
Anant Golea6286ee2009-05-18 15:19:01 -07001749 emac_read(EMAC_TXCARRIERSENSE);
Sriram0fe74632009-10-07 02:44:30 +00001750 emac_write(EMAC_TXCARRIERSENSE, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07001751
Thomas Lange60aeba22011-03-09 04:41:16 +00001752 ndev->stats.tx_fifo_errors += emac_read(EMAC_TXUNDERRUN);
Sriram0fe74632009-10-07 02:44:30 +00001753 emac_write(EMAC_TXUNDERRUN, stats_clear_mask);
Anant Golea6286ee2009-05-18 15:19:01 -07001754
Kulikov Vasiliy78e8c532010-07-05 02:13:26 +00001755 return &ndev->stats;
Anant Golea6286ee2009-05-18 15:19:01 -07001756}
1757
1758static const struct net_device_ops emac_netdev_ops = {
1759 .ndo_open = emac_dev_open,
1760 .ndo_stop = emac_dev_stop,
1761 .ndo_start_xmit = emac_dev_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001762 .ndo_set_rx_mode = emac_dev_mcast_set,
Anant Golea6286ee2009-05-18 15:19:01 -07001763 .ndo_set_mac_address = emac_dev_setmac_addr,
1764 .ndo_do_ioctl = emac_devioctl,
1765 .ndo_tx_timeout = emac_dev_tx_timeout,
1766 .ndo_get_stats = emac_dev_getnetstats,
1767#ifdef CONFIG_NET_POLL_CONTROLLER
1768 .ndo_poll_controller = emac_poll_controller,
1769#endif
1770};
1771
Heiko Schocher42f59962012-07-17 00:34:24 +00001772#ifdef CONFIG_OF
1773static struct emac_platform_data
1774 *davinci_emac_of_get_pdata(struct platform_device *pdev,
1775 struct emac_priv *priv)
1776{
1777 struct device_node *np;
1778 struct emac_platform_data *pdata = NULL;
1779 const u8 *mac_addr;
1780 u32 data;
1781 int ret;
1782
1783 pdata = pdev->dev.platform_data;
1784 if (!pdata) {
1785 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1786 if (!pdata)
1787 goto nodata;
1788 }
1789
1790 np = pdev->dev.of_node;
1791 if (!np)
1792 goto nodata;
1793 else
1794 pdata->version = EMAC_VERSION_2;
1795
1796 if (!is_valid_ether_addr(pdata->mac_addr)) {
1797 mac_addr = of_get_mac_address(np);
1798 if (mac_addr)
1799 memcpy(pdata->mac_addr, mac_addr, ETH_ALEN);
1800 }
1801
1802 ret = of_property_read_u32(np, "ti,davinci-ctrl-reg-offset", &data);
1803 if (!ret)
1804 pdata->ctrl_reg_offset = data;
1805
1806 ret = of_property_read_u32(np, "ti,davinci-ctrl-mod-reg-offset",
1807 &data);
1808 if (!ret)
1809 pdata->ctrl_mod_reg_offset = data;
1810
1811 ret = of_property_read_u32(np, "ti,davinci-ctrl-ram-offset", &data);
1812 if (!ret)
1813 pdata->ctrl_ram_offset = data;
1814
1815 ret = of_property_read_u32(np, "ti,davinci-ctrl-ram-size", &data);
1816 if (!ret)
1817 pdata->ctrl_ram_size = data;
1818
1819 ret = of_property_read_u32(np, "ti,davinci-rmii-en", &data);
1820 if (!ret)
1821 pdata->rmii_en = data;
1822
1823 ret = of_property_read_u32(np, "ti,davinci-no-bd-ram", &data);
1824 if (!ret)
1825 pdata->no_bd_ram = data;
1826
1827 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
1828 if (!priv->phy_node)
1829 pdata->phy_id = "";
1830
1831 pdev->dev.platform_data = pdata;
1832nodata:
1833 return pdata;
1834}
1835#else
1836static struct emac_platform_data
1837 *davinci_emac_of_get_pdata(struct platform_device *pdev,
1838 struct emac_priv *priv)
1839{
1840 return pdev->dev.platform_data;
1841}
1842#endif
Anant Golea6286ee2009-05-18 15:19:01 -07001843/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001844 * davinci_emac_probe - EMAC device probe
Anant Golea6286ee2009-05-18 15:19:01 -07001845 * @pdev: The DaVinci EMAC device that we are removing
1846 *
1847 * Called when probing for emac devicesr. We get details of instances and
1848 * resource information from platform init and register a network device
1849 * and allocate resources necessary for driver to perform
1850 */
1851static int __devinit davinci_emac_probe(struct platform_device *pdev)
1852{
1853 int rc = 0;
1854 struct resource *res;
1855 struct net_device *ndev;
1856 struct emac_priv *priv;
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001857 unsigned long size, hw_ram_addr;
Anant Golea6286ee2009-05-18 15:19:01 -07001858 struct emac_platform_data *pdata;
1859 struct device *emac_dev;
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001860 struct cpdma_params dma_params;
Anant Golea6286ee2009-05-18 15:19:01 -07001861
1862 /* obtain emac clock from kernel */
1863 emac_clk = clk_get(&pdev->dev, NULL);
1864 if (IS_ERR(emac_clk)) {
Johan Hovold240b2622011-05-26 04:37:32 +00001865 dev_err(&pdev->dev, "failed to get EMAC clock\n");
Anant Golea6286ee2009-05-18 15:19:01 -07001866 return -EBUSY;
1867 }
1868 emac_bus_frequency = clk_get_rate(emac_clk);
1869 /* TODO: Probe PHY here if possible */
1870
1871 ndev = alloc_etherdev(sizeof(struct emac_priv));
1872 if (!ndev) {
Julia Lawallb722dbf2011-06-01 07:10:10 +00001873 rc = -ENOMEM;
1874 goto free_clk;
Anant Golea6286ee2009-05-18 15:19:01 -07001875 }
1876
1877 platform_set_drvdata(pdev, ndev);
1878 priv = netdev_priv(ndev);
1879 priv->pdev = pdev;
1880 priv->ndev = ndev;
1881 priv->msg_enable = netif_msg_init(debug_level, DAVINCI_EMAC_DEBUG);
1882
Anant Golea6286ee2009-05-18 15:19:01 -07001883 spin_lock_init(&priv->lock);
1884
Heiko Schocher42f59962012-07-17 00:34:24 +00001885 pdata = davinci_emac_of_get_pdata(pdev, priv);
Anant Golea6286ee2009-05-18 15:19:01 -07001886 if (!pdata) {
Johan Hovold240b2622011-05-26 04:37:32 +00001887 dev_err(&pdev->dev, "no platform data\n");
Julia Lawallb722dbf2011-06-01 07:10:10 +00001888 rc = -ENODEV;
1889 goto probe_quit;
Anant Golea6286ee2009-05-18 15:19:01 -07001890 }
1891
1892 /* MAC addr and PHY mask , RMII enable info from platform_data */
1893 memcpy(priv->mac_addr, pdata->mac_addr, 6);
Cyril Chemparathy5d69e002010-09-15 10:11:24 -04001894 priv->phy_id = pdata->phy_id;
Anant Golea6286ee2009-05-18 15:19:01 -07001895 priv->rmii_en = pdata->rmii_en;
1896 priv->version = pdata->version;
Sriramakrishnan01a9af32009-11-19 15:58:26 +05301897 priv->int_enable = pdata->interrupt_enable;
1898 priv->int_disable = pdata->interrupt_disable;
1899
Sriram84da2652010-07-29 02:33:58 +00001900 priv->coal_intvl = 0;
1901 priv->bus_freq_mhz = (u32)(emac_bus_frequency / 1000000);
1902
Anant Golea6286ee2009-05-18 15:19:01 -07001903 emac_dev = &ndev->dev;
1904 /* Get EMAC platform data */
1905 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1906 if (!res) {
Johan Hovold240b2622011-05-26 04:37:32 +00001907 dev_err(&pdev->dev,"error getting res\n");
Anant Golea6286ee2009-05-18 15:19:01 -07001908 rc = -ENOENT;
1909 goto probe_quit;
1910 }
1911
1912 priv->emac_base_phys = res->start + pdata->ctrl_reg_offset;
Joe Perches28f65c112011-06-09 09:13:32 -07001913 size = resource_size(res);
Anant Golea6286ee2009-05-18 15:19:01 -07001914 if (!request_mem_region(res->start, size, ndev->name)) {
Johan Hovold240b2622011-05-26 04:37:32 +00001915 dev_err(&pdev->dev, "failed request_mem_region() for regs\n");
Anant Golea6286ee2009-05-18 15:19:01 -07001916 rc = -ENXIO;
1917 goto probe_quit;
1918 }
1919
1920 priv->remap_addr = ioremap(res->start, size);
1921 if (!priv->remap_addr) {
Johan Hovold240b2622011-05-26 04:37:32 +00001922 dev_err(&pdev->dev, "unable to map IO\n");
Anant Golea6286ee2009-05-18 15:19:01 -07001923 rc = -ENOMEM;
1924 release_mem_region(res->start, size);
1925 goto probe_quit;
1926 }
1927 priv->emac_base = priv->remap_addr + pdata->ctrl_reg_offset;
1928 ndev->base_addr = (unsigned long)priv->remap_addr;
1929
1930 priv->ctrl_base = priv->remap_addr + pdata->ctrl_mod_reg_offset;
Anant Golea6286ee2009-05-18 15:19:01 -07001931
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001932 hw_ram_addr = pdata->hw_ram_addr;
1933 if (!hw_ram_addr)
1934 hw_ram_addr = (u32 __force)res->start + pdata->ctrl_ram_offset;
1935
1936 memset(&dma_params, 0, sizeof(dma_params));
1937 dma_params.dev = emac_dev;
1938 dma_params.dmaregs = priv->emac_base;
1939 dma_params.rxthresh = priv->emac_base + 0x120;
1940 dma_params.rxfree = priv->emac_base + 0x140;
1941 dma_params.txhdp = priv->emac_base + 0x600;
1942 dma_params.rxhdp = priv->emac_base + 0x620;
1943 dma_params.txcp = priv->emac_base + 0x640;
1944 dma_params.rxcp = priv->emac_base + 0x660;
1945 dma_params.num_chan = EMAC_MAX_TXRX_CHANNELS;
1946 dma_params.min_packet_size = EMAC_DEF_MIN_ETHPKTSIZE;
Sriram6a1fef62011-03-22 02:31:03 +00001947 dma_params.desc_hw_addr = hw_ram_addr;
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001948 dma_params.desc_mem_size = pdata->ctrl_ram_size;
1949 dma_params.desc_align = 16;
1950
Sriram6a1fef62011-03-22 02:31:03 +00001951 dma_params.desc_mem_phys = pdata->no_bd_ram ? 0 :
1952 (u32 __force)res->start + pdata->ctrl_ram_offset;
1953
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001954 priv->dma = cpdma_ctlr_create(&dma_params);
1955 if (!priv->dma) {
Johan Hovold240b2622011-05-26 04:37:32 +00001956 dev_err(&pdev->dev, "error initializing DMA\n");
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04001957 rc = -ENOMEM;
1958 goto no_dma;
1959 }
1960
1961 priv->txchan = cpdma_chan_create(priv->dma, tx_chan_num(EMAC_DEF_TX_CH),
1962 emac_tx_handler);
1963 priv->rxchan = cpdma_chan_create(priv->dma, rx_chan_num(EMAC_DEF_RX_CH),
1964 emac_rx_handler);
1965 if (WARN_ON(!priv->txchan || !priv->rxchan)) {
1966 rc = -ENOMEM;
1967 goto no_irq_res;
1968 }
Sriramakrishnanad021ae2009-11-19 15:58:27 +05301969
Anant Golea6286ee2009-05-18 15:19:01 -07001970 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1971 if (!res) {
Johan Hovold240b2622011-05-26 04:37:32 +00001972 dev_err(&pdev->dev, "error getting irq res\n");
Anant Golea6286ee2009-05-18 15:19:01 -07001973 rc = -ENOENT;
1974 goto no_irq_res;
1975 }
1976 ndev->irq = res->start;
1977
1978 if (!is_valid_ether_addr(priv->mac_addr)) {
Anant Golea6286ee2009-05-18 15:19:01 -07001979 /* Use random MAC if none passed */
Danny Kukawkabaf1d372012-02-17 05:43:24 +00001980 eth_hw_addr_random(ndev);
1981 memcpy(priv->mac_addr, ndev->dev_addr, ndev->addr_len);
Johan Hovold240b2622011-05-26 04:37:32 +00001982 dev_warn(&pdev->dev, "using random MAC addr: %pM\n",
1983 priv->mac_addr);
Anant Golea6286ee2009-05-18 15:19:01 -07001984 }
1985
1986 ndev->netdev_ops = &emac_netdev_ops;
1987 SET_ETHTOOL_OPS(ndev, &ethtool_ops);
1988 netif_napi_add(ndev, &priv->napi, emac_poll, EMAC_POLL_WEIGHT);
1989
Sriram1ca518b2010-01-07 00:22:37 +00001990 clk_enable(emac_clk);
1991
Anant Golea6286ee2009-05-18 15:19:01 -07001992 /* register the network device */
1993 SET_NETDEV_DEV(ndev, &pdev->dev);
1994 rc = register_netdev(ndev);
1995 if (rc) {
Johan Hovold240b2622011-05-26 04:37:32 +00001996 dev_err(&pdev->dev, "error in register_netdev\n");
Anant Golea6286ee2009-05-18 15:19:01 -07001997 rc = -ENODEV;
1998 goto netdev_reg_err;
1999 }
2000
Anant Golea6286ee2009-05-18 15:19:01 -07002001
Anant Golea6286ee2009-05-18 15:19:01 -07002002 if (netif_msg_probe(priv)) {
2003 dev_notice(emac_dev, "DaVinci EMAC Probe found device "\
2004 "(regs: %p, irq: %d)\n",
2005 (void *)priv->emac_base_phys, ndev->irq);
2006 }
2007 return 0;
2008
Anant Golea6286ee2009-05-18 15:19:01 -07002009netdev_reg_err:
Sriram1ca518b2010-01-07 00:22:37 +00002010 clk_disable(emac_clk);
Anant Golea6286ee2009-05-18 15:19:01 -07002011no_irq_res:
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002012 if (priv->txchan)
2013 cpdma_chan_destroy(priv->txchan);
2014 if (priv->rxchan)
2015 cpdma_chan_destroy(priv->rxchan);
2016 cpdma_ctlr_destroy(priv->dma);
2017no_dma:
Anant Golea6286ee2009-05-18 15:19:01 -07002018 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Joe Perches28f65c112011-06-09 09:13:32 -07002019 release_mem_region(res->start, resource_size(res));
Anant Golea6286ee2009-05-18 15:19:01 -07002020 iounmap(priv->remap_addr);
2021
2022probe_quit:
Anant Golea6286ee2009-05-18 15:19:01 -07002023 free_netdev(ndev);
Julia Lawallb722dbf2011-06-01 07:10:10 +00002024free_clk:
2025 clk_put(emac_clk);
Anant Golea6286ee2009-05-18 15:19:01 -07002026 return rc;
2027}
2028
2029/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002030 * davinci_emac_remove - EMAC device remove
Anant Golea6286ee2009-05-18 15:19:01 -07002031 * @pdev: The DaVinci EMAC device that we are removing
2032 *
2033 * Called when removing the device driver. We disable clock usage and release
2034 * the resources taken up by the driver and unregister network device
2035 */
2036static int __devexit davinci_emac_remove(struct platform_device *pdev)
2037{
2038 struct resource *res;
2039 struct net_device *ndev = platform_get_drvdata(pdev);
2040 struct emac_priv *priv = netdev_priv(ndev);
2041
2042 dev_notice(&ndev->dev, "DaVinci EMAC: davinci_emac_remove()\n");
2043
Anant Golea6286ee2009-05-18 15:19:01 -07002044 platform_set_drvdata(pdev, NULL);
2045 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Anant Golea6286ee2009-05-18 15:19:01 -07002046
Cyril Chemparathy3ef0fdb2010-09-15 10:11:29 -04002047 if (priv->txchan)
2048 cpdma_chan_destroy(priv->txchan);
2049 if (priv->rxchan)
2050 cpdma_chan_destroy(priv->rxchan);
2051 cpdma_ctlr_destroy(priv->dma);
2052
Joe Perches28f65c112011-06-09 09:13:32 -07002053 release_mem_region(res->start, resource_size(res));
Anant Golea6286ee2009-05-18 15:19:01 -07002054
2055 unregister_netdev(ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07002056 iounmap(priv->remap_addr);
Stefan Weil2a1bc0d2010-08-03 08:53:45 +00002057 free_netdev(ndev);
Anant Golea6286ee2009-05-18 15:19:01 -07002058
2059 clk_disable(emac_clk);
2060 clk_put(emac_clk);
2061
2062 return 0;
2063}
2064
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002065static int davinci_emac_suspend(struct device *dev)
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002066{
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002067 struct platform_device *pdev = to_platform_device(dev);
2068 struct net_device *ndev = platform_get_drvdata(pdev);
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002069
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002070 if (netif_running(ndev))
2071 emac_dev_stop(ndev);
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002072
2073 clk_disable(emac_clk);
2074
2075 return 0;
2076}
2077
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002078static int davinci_emac_resume(struct device *dev)
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002079{
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002080 struct platform_device *pdev = to_platform_device(dev);
2081 struct net_device *ndev = platform_get_drvdata(pdev);
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002082
2083 clk_enable(emac_clk);
2084
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002085 if (netif_running(ndev))
2086 emac_dev_open(ndev);
Ranjith Lohithakshan8d044fe2009-11-04 22:06:20 -08002087
2088 return 0;
2089}
2090
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002091static const struct dev_pm_ops davinci_emac_pm_ops = {
2092 .suspend = davinci_emac_suspend,
2093 .resume = davinci_emac_resume,
2094};
2095
Heiko Schocher42f59962012-07-17 00:34:24 +00002096static const struct of_device_id davinci_emac_of_match[] = {
2097 {.compatible = "ti,davinci-dm6467-emac", },
2098 {},
2099};
2100MODULE_DEVICE_TABLE(of, davinci_emac_of_match);
2101
Ben Hutchings1aa8b472012-07-10 10:56:59 +00002102/* davinci_emac_driver: EMAC platform driver structure */
Anant Golea6286ee2009-05-18 15:19:01 -07002103static struct platform_driver davinci_emac_driver = {
2104 .driver = {
2105 .name = "davinci_emac",
2106 .owner = THIS_MODULE,
chaithrika@ti.comd4fdcd92010-03-10 22:37:56 +00002107 .pm = &davinci_emac_pm_ops,
Heiko Schocher42f59962012-07-17 00:34:24 +00002108 .of_match_table = of_match_ptr(davinci_emac_of_match),
Anant Golea6286ee2009-05-18 15:19:01 -07002109 },
2110 .probe = davinci_emac_probe,
2111 .remove = __devexit_p(davinci_emac_remove),
2112};
2113
2114/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002115 * davinci_emac_init - EMAC driver module init
Anant Golea6286ee2009-05-18 15:19:01 -07002116 *
2117 * Called when initializing the driver. We register the driver with
2118 * the platform.
2119 */
2120static int __init davinci_emac_init(void)
2121{
2122 return platform_driver_register(&davinci_emac_driver);
2123}
Rajashekhara, Sudhakar2db95172009-08-19 10:39:55 +00002124late_initcall(davinci_emac_init);
Anant Golea6286ee2009-05-18 15:19:01 -07002125
2126/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +00002127 * davinci_emac_exit - EMAC driver module exit
Anant Golea6286ee2009-05-18 15:19:01 -07002128 *
2129 * Called when exiting the driver completely. We unregister the driver with
2130 * the platform and exit
2131 */
2132static void __exit davinci_emac_exit(void)
2133{
2134 platform_driver_unregister(&davinci_emac_driver);
2135}
2136module_exit(davinci_emac_exit);
2137
2138MODULE_LICENSE("GPL");
2139MODULE_AUTHOR("DaVinci EMAC Maintainer: Anant Gole <anantgole@ti.com>");
2140MODULE_AUTHOR("DaVinci EMAC Maintainer: Chaithrika U S <chaithrika@ti.com>");
2141MODULE_DESCRIPTION("DaVinci EMAC Ethernet driver");