xref: /openbmc/linux/drivers/net/ethernet/ti/cpsw.c (revision 5c473ed2)
1df828598SMugunthan V N /*
2df828598SMugunthan V N  * Texas Instruments Ethernet Switch Driver
3df828598SMugunthan V N  *
4df828598SMugunthan V N  * Copyright (C) 2012 Texas Instruments
5df828598SMugunthan V N  *
6df828598SMugunthan V N  * This program is free software; you can redistribute it and/or
7df828598SMugunthan V N  * modify it under the terms of the GNU General Public License as
8df828598SMugunthan V N  * published by the Free Software Foundation version 2.
9df828598SMugunthan V N  *
10df828598SMugunthan V N  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11df828598SMugunthan V N  * kind, whether express or implied; without even the implied warranty
12df828598SMugunthan V N  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13df828598SMugunthan V N  * GNU General Public License for more details.
14df828598SMugunthan V N  */
15df828598SMugunthan V N 
16df828598SMugunthan V N #include <linux/kernel.h>
17df828598SMugunthan V N #include <linux/io.h>
18df828598SMugunthan V N #include <linux/clk.h>
19df828598SMugunthan V N #include <linux/timer.h>
20df828598SMugunthan V N #include <linux/module.h>
21df828598SMugunthan V N #include <linux/platform_device.h>
22df828598SMugunthan V N #include <linux/irqreturn.h>
23df828598SMugunthan V N #include <linux/interrupt.h>
24df828598SMugunthan V N #include <linux/if_ether.h>
25df828598SMugunthan V N #include <linux/etherdevice.h>
26df828598SMugunthan V N #include <linux/netdevice.h>
27df828598SMugunthan V N #include <linux/phy.h>
28df828598SMugunthan V N #include <linux/workqueue.h>
29df828598SMugunthan V N #include <linux/delay.h>
30df828598SMugunthan V N 
31df828598SMugunthan V N #include <linux/platform_data/cpsw.h>
32df828598SMugunthan V N 
33df828598SMugunthan V N #include "cpsw_ale.h"
34df828598SMugunthan V N #include "davinci_cpdma.h"
35df828598SMugunthan V N 
36df828598SMugunthan V N #define CPSW_DEBUG	(NETIF_MSG_HW		| NETIF_MSG_WOL		| \
37df828598SMugunthan V N 			 NETIF_MSG_DRV		| NETIF_MSG_LINK	| \
38df828598SMugunthan V N 			 NETIF_MSG_IFUP		| NETIF_MSG_INTR	| \
39df828598SMugunthan V N 			 NETIF_MSG_PROBE	| NETIF_MSG_TIMER	| \
40df828598SMugunthan V N 			 NETIF_MSG_IFDOWN	| NETIF_MSG_RX_ERR	| \
41df828598SMugunthan V N 			 NETIF_MSG_TX_ERR	| NETIF_MSG_TX_DONE	| \
42df828598SMugunthan V N 			 NETIF_MSG_PKTDATA	| NETIF_MSG_TX_QUEUED	| \
43df828598SMugunthan V N 			 NETIF_MSG_RX_STATUS)
44df828598SMugunthan V N 
45df828598SMugunthan V N #define cpsw_info(priv, type, format, ...)		\
46df828598SMugunthan V N do {								\
47df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
48df828598SMugunthan V N 		dev_info(priv->dev, format, ## __VA_ARGS__);	\
49df828598SMugunthan V N } while (0)
50df828598SMugunthan V N 
51df828598SMugunthan V N #define cpsw_err(priv, type, format, ...)		\
52df828598SMugunthan V N do {								\
53df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
54df828598SMugunthan V N 		dev_err(priv->dev, format, ## __VA_ARGS__);	\
55df828598SMugunthan V N } while (0)
56df828598SMugunthan V N 
57df828598SMugunthan V N #define cpsw_dbg(priv, type, format, ...)		\
58df828598SMugunthan V N do {								\
59df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
60df828598SMugunthan V N 		dev_dbg(priv->dev, format, ## __VA_ARGS__);	\
61df828598SMugunthan V N } while (0)
62df828598SMugunthan V N 
63df828598SMugunthan V N #define cpsw_notice(priv, type, format, ...)		\
64df828598SMugunthan V N do {								\
65df828598SMugunthan V N 	if (netif_msg_##type(priv) && net_ratelimit())		\
66df828598SMugunthan V N 		dev_notice(priv->dev, format, ## __VA_ARGS__);	\
67df828598SMugunthan V N } while (0)
68df828598SMugunthan V N 
69df828598SMugunthan V N #define CPSW_MAJOR_VERSION(reg)		(reg >> 8 & 0x7)
70df828598SMugunthan V N #define CPSW_MINOR_VERSION(reg)		(reg & 0xff)
71df828598SMugunthan V N #define CPSW_RTL_VERSION(reg)		((reg >> 11) & 0x1f)
72df828598SMugunthan V N 
73df828598SMugunthan V N #define CPDMA_RXTHRESH		0x0c0
74df828598SMugunthan V N #define CPDMA_RXFREE		0x0e0
75df828598SMugunthan V N #define CPDMA_TXHDP		0x00
76df828598SMugunthan V N #define CPDMA_RXHDP		0x20
77df828598SMugunthan V N #define CPDMA_TXCP		0x40
78df828598SMugunthan V N #define CPDMA_RXCP		0x60
79df828598SMugunthan V N 
80df828598SMugunthan V N #define cpsw_dma_regs(base, offset)		\
81df828598SMugunthan V N 	(void __iomem *)((base) + (offset))
82df828598SMugunthan V N #define cpsw_dma_rxthresh(base, offset)		\
83df828598SMugunthan V N 	(void __iomem *)((base) + (offset) + CPDMA_RXTHRESH)
84df828598SMugunthan V N #define cpsw_dma_rxfree(base, offset)		\
85df828598SMugunthan V N 	(void __iomem *)((base) + (offset) + CPDMA_RXFREE)
86df828598SMugunthan V N #define cpsw_dma_txhdp(base, offset)		\
87df828598SMugunthan V N 	(void __iomem *)((base) + (offset) + CPDMA_TXHDP)
88df828598SMugunthan V N #define cpsw_dma_rxhdp(base, offset)		\
89df828598SMugunthan V N 	(void __iomem *)((base) + (offset) + CPDMA_RXHDP)
90df828598SMugunthan V N #define cpsw_dma_txcp(base, offset)		\
91df828598SMugunthan V N 	(void __iomem *)((base) + (offset) + CPDMA_TXCP)
92df828598SMugunthan V N #define cpsw_dma_rxcp(base, offset)		\
93df828598SMugunthan V N 	(void __iomem *)((base) + (offset) + CPDMA_RXCP)
94df828598SMugunthan V N 
95df828598SMugunthan V N #define CPSW_POLL_WEIGHT	64
96df828598SMugunthan V N #define CPSW_MIN_PACKET_SIZE	60
97df828598SMugunthan V N #define CPSW_MAX_PACKET_SIZE	(1500 + 14 + 4 + 4)
98df828598SMugunthan V N 
99df828598SMugunthan V N #define RX_PRIORITY_MAPPING	0x76543210
100df828598SMugunthan V N #define TX_PRIORITY_MAPPING	0x33221100
101df828598SMugunthan V N #define CPDMA_TX_PRIORITY_MAP	0x76543210
102df828598SMugunthan V N 
103df828598SMugunthan V N #define cpsw_enable_irq(priv)	\
104df828598SMugunthan V N 	do {			\
105df828598SMugunthan V N 		u32 i;		\
106df828598SMugunthan V N 		for (i = 0; i < priv->num_irqs; i++) \
107df828598SMugunthan V N 			enable_irq(priv->irqs_table[i]); \
108df828598SMugunthan V N 	} while (0);
109df828598SMugunthan V N #define cpsw_disable_irq(priv)	\
110df828598SMugunthan V N 	do {			\
111df828598SMugunthan V N 		u32 i;		\
112df828598SMugunthan V N 		for (i = 0; i < priv->num_irqs; i++) \
113df828598SMugunthan V N 			disable_irq_nosync(priv->irqs_table[i]); \
114df828598SMugunthan V N 	} while (0);
115df828598SMugunthan V N 
116df828598SMugunthan V N static int debug_level;
117df828598SMugunthan V N module_param(debug_level, int, 0);
118df828598SMugunthan V N MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
119df828598SMugunthan V N 
120df828598SMugunthan V N static int ale_ageout = 10;
121df828598SMugunthan V N module_param(ale_ageout, int, 0);
122df828598SMugunthan V N MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
123df828598SMugunthan V N 
124df828598SMugunthan V N static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
125df828598SMugunthan V N module_param(rx_packet_max, int, 0);
126df828598SMugunthan V N MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
127df828598SMugunthan V N 
128df828598SMugunthan V N struct cpsw_ss_regs {
129df828598SMugunthan V N 	u32	id_ver;
130df828598SMugunthan V N 	u32	soft_reset;
131df828598SMugunthan V N 	u32	control;
132df828598SMugunthan V N 	u32	int_control;
133df828598SMugunthan V N 	u32	rx_thresh_en;
134df828598SMugunthan V N 	u32	rx_en;
135df828598SMugunthan V N 	u32	tx_en;
136df828598SMugunthan V N 	u32	misc_en;
137df828598SMugunthan V N };
138df828598SMugunthan V N 
139df828598SMugunthan V N struct cpsw_regs {
140df828598SMugunthan V N 	u32	id_ver;
141df828598SMugunthan V N 	u32	control;
142df828598SMugunthan V N 	u32	soft_reset;
143df828598SMugunthan V N 	u32	stat_port_en;
144df828598SMugunthan V N 	u32	ptype;
145df828598SMugunthan V N };
146df828598SMugunthan V N 
147df828598SMugunthan V N struct cpsw_slave_regs {
148df828598SMugunthan V N 	u32	max_blks;
149df828598SMugunthan V N 	u32	blk_cnt;
150df828598SMugunthan V N 	u32	flow_thresh;
151df828598SMugunthan V N 	u32	port_vlan;
152df828598SMugunthan V N 	u32	tx_pri_map;
153df828598SMugunthan V N 	u32	ts_ctl;
154df828598SMugunthan V N 	u32	ts_seq_ltype;
155df828598SMugunthan V N 	u32	ts_vlan;
156df828598SMugunthan V N 	u32	sa_lo;
157df828598SMugunthan V N 	u32	sa_hi;
158df828598SMugunthan V N };
159df828598SMugunthan V N 
160df828598SMugunthan V N struct cpsw_host_regs {
161df828598SMugunthan V N 	u32	max_blks;
162df828598SMugunthan V N 	u32	blk_cnt;
163df828598SMugunthan V N 	u32	flow_thresh;
164df828598SMugunthan V N 	u32	port_vlan;
165df828598SMugunthan V N 	u32	tx_pri_map;
166df828598SMugunthan V N 	u32	cpdma_tx_pri_map;
167df828598SMugunthan V N 	u32	cpdma_rx_chan_map;
168df828598SMugunthan V N };
169df828598SMugunthan V N 
170df828598SMugunthan V N struct cpsw_sliver_regs {
171df828598SMugunthan V N 	u32	id_ver;
172df828598SMugunthan V N 	u32	mac_control;
173df828598SMugunthan V N 	u32	mac_status;
174df828598SMugunthan V N 	u32	soft_reset;
175df828598SMugunthan V N 	u32	rx_maxlen;
176df828598SMugunthan V N 	u32	__reserved_0;
177df828598SMugunthan V N 	u32	rx_pause;
178df828598SMugunthan V N 	u32	tx_pause;
179df828598SMugunthan V N 	u32	__reserved_1;
180df828598SMugunthan V N 	u32	rx_pri_map;
181df828598SMugunthan V N };
182df828598SMugunthan V N 
183df828598SMugunthan V N struct cpsw_slave {
184df828598SMugunthan V N 	struct cpsw_slave_regs __iomem	*regs;
185df828598SMugunthan V N 	struct cpsw_sliver_regs __iomem	*sliver;
186df828598SMugunthan V N 	int				slave_num;
187df828598SMugunthan V N 	u32				mac_control;
188df828598SMugunthan V N 	struct cpsw_slave_data		*data;
189df828598SMugunthan V N 	struct phy_device		*phy;
190df828598SMugunthan V N };
191df828598SMugunthan V N 
192df828598SMugunthan V N struct cpsw_priv {
193df828598SMugunthan V N 	spinlock_t			lock;
194df828598SMugunthan V N 	struct platform_device		*pdev;
195df828598SMugunthan V N 	struct net_device		*ndev;
196df828598SMugunthan V N 	struct resource			*cpsw_res;
197df828598SMugunthan V N 	struct resource			*cpsw_ss_res;
198df828598SMugunthan V N 	struct napi_struct		napi;
199df828598SMugunthan V N 	struct device			*dev;
200df828598SMugunthan V N 	struct cpsw_platform_data	data;
201df828598SMugunthan V N 	struct cpsw_regs __iomem	*regs;
202df828598SMugunthan V N 	struct cpsw_ss_regs __iomem	*ss_regs;
203df828598SMugunthan V N 	struct cpsw_host_regs __iomem	*host_port_regs;
204df828598SMugunthan V N 	u32				msg_enable;
205df828598SMugunthan V N 	struct net_device_stats		stats;
206df828598SMugunthan V N 	int				rx_packet_max;
207df828598SMugunthan V N 	int				host_port;
208df828598SMugunthan V N 	struct clk			*clk;
209df828598SMugunthan V N 	u8				mac_addr[ETH_ALEN];
210df828598SMugunthan V N 	struct cpsw_slave		*slaves;
211df828598SMugunthan V N 	struct cpdma_ctlr		*dma;
212df828598SMugunthan V N 	struct cpdma_chan		*txch, *rxch;
213df828598SMugunthan V N 	struct cpsw_ale			*ale;
214df828598SMugunthan V N 	/* snapshot of IRQ numbers */
215df828598SMugunthan V N 	u32 irqs_table[4];
216df828598SMugunthan V N 	u32 num_irqs;
217df828598SMugunthan V N };
218df828598SMugunthan V N 
219df828598SMugunthan V N #define napi_to_priv(napi)	container_of(napi, struct cpsw_priv, napi)
220df828598SMugunthan V N #define for_each_slave(priv, func, arg...)			\
221df828598SMugunthan V N 	do {							\
222df828598SMugunthan V N 		int idx;					\
223df828598SMugunthan V N 		for (idx = 0; idx < (priv)->data.slaves; idx++)	\
224df828598SMugunthan V N 			(func)((priv)->slaves + idx, ##arg);	\
225df828598SMugunthan V N 	} while (0)
226df828598SMugunthan V N 
227df828598SMugunthan V N static void cpsw_intr_enable(struct cpsw_priv *priv)
228df828598SMugunthan V N {
229df828598SMugunthan V N 	__raw_writel(0xFF, &priv->ss_regs->tx_en);
230df828598SMugunthan V N 	__raw_writel(0xFF, &priv->ss_regs->rx_en);
231df828598SMugunthan V N 
232df828598SMugunthan V N 	cpdma_ctlr_int_ctrl(priv->dma, true);
233df828598SMugunthan V N 	return;
234df828598SMugunthan V N }
235df828598SMugunthan V N 
236df828598SMugunthan V N static void cpsw_intr_disable(struct cpsw_priv *priv)
237df828598SMugunthan V N {
238df828598SMugunthan V N 	__raw_writel(0, &priv->ss_regs->tx_en);
239df828598SMugunthan V N 	__raw_writel(0, &priv->ss_regs->rx_en);
240df828598SMugunthan V N 
241df828598SMugunthan V N 	cpdma_ctlr_int_ctrl(priv->dma, false);
242df828598SMugunthan V N 	return;
243df828598SMugunthan V N }
244df828598SMugunthan V N 
245df828598SMugunthan V N void cpsw_tx_handler(void *token, int len, int status)
246df828598SMugunthan V N {
247df828598SMugunthan V N 	struct sk_buff		*skb = token;
248df828598SMugunthan V N 	struct net_device	*ndev = skb->dev;
249df828598SMugunthan V N 	struct cpsw_priv	*priv = netdev_priv(ndev);
250df828598SMugunthan V N 
251df828598SMugunthan V N 	if (unlikely(netif_queue_stopped(ndev)))
252df828598SMugunthan V N 		netif_start_queue(ndev);
253df828598SMugunthan V N 	priv->stats.tx_packets++;
254df828598SMugunthan V N 	priv->stats.tx_bytes += len;
255df828598SMugunthan V N 	dev_kfree_skb_any(skb);
256df828598SMugunthan V N }
257df828598SMugunthan V N 
258df828598SMugunthan V N void cpsw_rx_handler(void *token, int len, int status)
259df828598SMugunthan V N {
260df828598SMugunthan V N 	struct sk_buff		*skb = token;
261df828598SMugunthan V N 	struct net_device	*ndev = skb->dev;
262df828598SMugunthan V N 	struct cpsw_priv	*priv = netdev_priv(ndev);
263df828598SMugunthan V N 	int			ret = 0;
264df828598SMugunthan V N 
265df828598SMugunthan V N 	/* free and bail if we are shutting down */
266df828598SMugunthan V N 	if (unlikely(!netif_running(ndev)) ||
267df828598SMugunthan V N 			unlikely(!netif_carrier_ok(ndev))) {
268df828598SMugunthan V N 		dev_kfree_skb_any(skb);
269df828598SMugunthan V N 		return;
270df828598SMugunthan V N 	}
271df828598SMugunthan V N 	if (likely(status >= 0)) {
272df828598SMugunthan V N 		skb_put(skb, len);
273df828598SMugunthan V N 		skb->protocol = eth_type_trans(skb, ndev);
274df828598SMugunthan V N 		netif_receive_skb(skb);
275df828598SMugunthan V N 		priv->stats.rx_bytes += len;
276df828598SMugunthan V N 		priv->stats.rx_packets++;
277df828598SMugunthan V N 		skb = NULL;
278df828598SMugunthan V N 	}
279df828598SMugunthan V N 
280df828598SMugunthan V N 	if (unlikely(!netif_running(ndev))) {
281df828598SMugunthan V N 		if (skb)
282df828598SMugunthan V N 			dev_kfree_skb_any(skb);
283df828598SMugunthan V N 		return;
284df828598SMugunthan V N 	}
285df828598SMugunthan V N 
286df828598SMugunthan V N 	if (likely(!skb)) {
287df828598SMugunthan V N 		skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
288df828598SMugunthan V N 		if (WARN_ON(!skb))
289df828598SMugunthan V N 			return;
290df828598SMugunthan V N 
291df828598SMugunthan V N 		ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
292df828598SMugunthan V N 					skb_tailroom(skb), GFP_KERNEL);
293df828598SMugunthan V N 	}
294df828598SMugunthan V N 	WARN_ON(ret < 0);
295df828598SMugunthan V N }
296df828598SMugunthan V N 
297df828598SMugunthan V N static irqreturn_t cpsw_interrupt(int irq, void *dev_id)
298df828598SMugunthan V N {
299df828598SMugunthan V N 	struct cpsw_priv *priv = dev_id;
300df828598SMugunthan V N 
301df828598SMugunthan V N 	if (likely(netif_running(priv->ndev))) {
302df828598SMugunthan V N 		cpsw_intr_disable(priv);
303df828598SMugunthan V N 		cpsw_disable_irq(priv);
304df828598SMugunthan V N 		napi_schedule(&priv->napi);
305df828598SMugunthan V N 	}
306df828598SMugunthan V N 	return IRQ_HANDLED;
307df828598SMugunthan V N }
308df828598SMugunthan V N 
309df828598SMugunthan V N static inline int cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
310df828598SMugunthan V N {
311df828598SMugunthan V N 	if (priv->host_port == 0)
312df828598SMugunthan V N 		return slave_num + 1;
313df828598SMugunthan V N 	else
314df828598SMugunthan V N 		return slave_num;
315df828598SMugunthan V N }
316df828598SMugunthan V N 
317df828598SMugunthan V N static int cpsw_poll(struct napi_struct *napi, int budget)
318df828598SMugunthan V N {
319df828598SMugunthan V N 	struct cpsw_priv	*priv = napi_to_priv(napi);
320df828598SMugunthan V N 	int			num_tx, num_rx;
321df828598SMugunthan V N 
322df828598SMugunthan V N 	num_tx = cpdma_chan_process(priv->txch, 128);
323df828598SMugunthan V N 	num_rx = cpdma_chan_process(priv->rxch, budget);
324df828598SMugunthan V N 
325df828598SMugunthan V N 	if (num_rx || num_tx)
326df828598SMugunthan V N 		cpsw_dbg(priv, intr, "poll %d rx, %d tx pkts\n",
327df828598SMugunthan V N 			 num_rx, num_tx);
328df828598SMugunthan V N 
329df828598SMugunthan V N 	if (num_rx < budget) {
330df828598SMugunthan V N 		napi_complete(napi);
331df828598SMugunthan V N 		cpsw_intr_enable(priv);
332df828598SMugunthan V N 		cpdma_ctlr_eoi(priv->dma);
333df828598SMugunthan V N 		cpsw_enable_irq(priv);
334df828598SMugunthan V N 	}
335df828598SMugunthan V N 
336df828598SMugunthan V N 	return num_rx;
337df828598SMugunthan V N }
338df828598SMugunthan V N 
339df828598SMugunthan V N static inline void soft_reset(const char *module, void __iomem *reg)
340df828598SMugunthan V N {
341df828598SMugunthan V N 	unsigned long timeout = jiffies + HZ;
342df828598SMugunthan V N 
343df828598SMugunthan V N 	__raw_writel(1, reg);
344df828598SMugunthan V N 	do {
345df828598SMugunthan V N 		cpu_relax();
346df828598SMugunthan V N 	} while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
347df828598SMugunthan V N 
348df828598SMugunthan V N 	WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
349df828598SMugunthan V N }
350df828598SMugunthan V N 
351df828598SMugunthan V N #define mac_hi(mac)	(((mac)[0] << 0) | ((mac)[1] << 8) |	\
352df828598SMugunthan V N 			 ((mac)[2] << 16) | ((mac)[3] << 24))
353df828598SMugunthan V N #define mac_lo(mac)	(((mac)[4] << 0) | ((mac)[5] << 8))
354df828598SMugunthan V N 
355df828598SMugunthan V N static void cpsw_set_slave_mac(struct cpsw_slave *slave,
356df828598SMugunthan V N 			       struct cpsw_priv *priv)
357df828598SMugunthan V N {
358df828598SMugunthan V N 	__raw_writel(mac_hi(priv->mac_addr), &slave->regs->sa_hi);
359df828598SMugunthan V N 	__raw_writel(mac_lo(priv->mac_addr), &slave->regs->sa_lo);
360df828598SMugunthan V N }
361df828598SMugunthan V N 
362df828598SMugunthan V N static void _cpsw_adjust_link(struct cpsw_slave *slave,
363df828598SMugunthan V N 			      struct cpsw_priv *priv, bool *link)
364df828598SMugunthan V N {
365df828598SMugunthan V N 	struct phy_device	*phy = slave->phy;
366df828598SMugunthan V N 	u32			mac_control = 0;
367df828598SMugunthan V N 	u32			slave_port;
368df828598SMugunthan V N 
369df828598SMugunthan V N 	if (!phy)
370df828598SMugunthan V N 		return;
371df828598SMugunthan V N 
372df828598SMugunthan V N 	slave_port = cpsw_get_slave_port(priv, slave->slave_num);
373df828598SMugunthan V N 
374df828598SMugunthan V N 	if (phy->link) {
375df828598SMugunthan V N 		mac_control = priv->data.mac_control;
376df828598SMugunthan V N 
377df828598SMugunthan V N 		/* enable forwarding */
378df828598SMugunthan V N 		cpsw_ale_control_set(priv->ale, slave_port,
379df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
380df828598SMugunthan V N 
381df828598SMugunthan V N 		if (phy->speed == 1000)
382df828598SMugunthan V N 			mac_control |= BIT(7);	/* GIGABITEN	*/
383df828598SMugunthan V N 		if (phy->duplex)
384df828598SMugunthan V N 			mac_control |= BIT(0);	/* FULLDUPLEXEN	*/
385df828598SMugunthan V N 		*link = true;
386df828598SMugunthan V N 	} else {
387df828598SMugunthan V N 		mac_control = 0;
388df828598SMugunthan V N 		/* disable forwarding */
389df828598SMugunthan V N 		cpsw_ale_control_set(priv->ale, slave_port,
390df828598SMugunthan V N 				     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
391df828598SMugunthan V N 	}
392df828598SMugunthan V N 
393df828598SMugunthan V N 	if (mac_control != slave->mac_control) {
394df828598SMugunthan V N 		phy_print_status(phy);
395df828598SMugunthan V N 		__raw_writel(mac_control, &slave->sliver->mac_control);
396df828598SMugunthan V N 	}
397df828598SMugunthan V N 
398df828598SMugunthan V N 	slave->mac_control = mac_control;
399df828598SMugunthan V N }
400df828598SMugunthan V N 
401df828598SMugunthan V N static void cpsw_adjust_link(struct net_device *ndev)
402df828598SMugunthan V N {
403df828598SMugunthan V N 	struct cpsw_priv	*priv = netdev_priv(ndev);
404df828598SMugunthan V N 	bool			link = false;
405df828598SMugunthan V N 
406df828598SMugunthan V N 	for_each_slave(priv, _cpsw_adjust_link, priv, &link);
407df828598SMugunthan V N 
408df828598SMugunthan V N 	if (link) {
409df828598SMugunthan V N 		netif_carrier_on(ndev);
410df828598SMugunthan V N 		if (netif_running(ndev))
411df828598SMugunthan V N 			netif_wake_queue(ndev);
412df828598SMugunthan V N 	} else {
413df828598SMugunthan V N 		netif_carrier_off(ndev);
414df828598SMugunthan V N 		netif_stop_queue(ndev);
415df828598SMugunthan V N 	}
416df828598SMugunthan V N }
417df828598SMugunthan V N 
418df828598SMugunthan V N static inline int __show_stat(char *buf, int maxlen, const char *name, u32 val)
419df828598SMugunthan V N {
420df828598SMugunthan V N 	static char *leader = "........................................";
421df828598SMugunthan V N 
422df828598SMugunthan V N 	if (!val)
423df828598SMugunthan V N 		return 0;
424df828598SMugunthan V N 	else
425df828598SMugunthan V N 		return snprintf(buf, maxlen, "%s %s %10d\n", name,
426df828598SMugunthan V N 				leader + strlen(name), val);
427df828598SMugunthan V N }
428df828598SMugunthan V N 
429df828598SMugunthan V N static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
430df828598SMugunthan V N {
431df828598SMugunthan V N 	char name[32];
432df828598SMugunthan V N 	u32 slave_port;
433df828598SMugunthan V N 
434df828598SMugunthan V N 	sprintf(name, "slave-%d", slave->slave_num);
435df828598SMugunthan V N 
436df828598SMugunthan V N 	soft_reset(name, &slave->sliver->soft_reset);
437df828598SMugunthan V N 
438df828598SMugunthan V N 	/* setup priority mapping */
439df828598SMugunthan V N 	__raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
440df828598SMugunthan V N 	__raw_writel(TX_PRIORITY_MAPPING, &slave->regs->tx_pri_map);
441df828598SMugunthan V N 
442df828598SMugunthan V N 	/* setup max packet size, and mac address */
443df828598SMugunthan V N 	__raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
444df828598SMugunthan V N 	cpsw_set_slave_mac(slave, priv);
445df828598SMugunthan V N 
446df828598SMugunthan V N 	slave->mac_control = 0;	/* no link yet */
447df828598SMugunthan V N 
448df828598SMugunthan V N 	slave_port = cpsw_get_slave_port(priv, slave->slave_num);
449df828598SMugunthan V N 
450df828598SMugunthan V N 	cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
451df828598SMugunthan V N 			   1 << slave_port, 0, ALE_MCAST_FWD_2);
452df828598SMugunthan V N 
453df828598SMugunthan V N 	slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
454df828598SMugunthan V N 				 &cpsw_adjust_link, 0, slave->data->phy_if);
455df828598SMugunthan V N 	if (IS_ERR(slave->phy)) {
456df828598SMugunthan V N 		dev_err(priv->dev, "phy %s not found on slave %d\n",
457df828598SMugunthan V N 			slave->data->phy_id, slave->slave_num);
458df828598SMugunthan V N 		slave->phy = NULL;
459df828598SMugunthan V N 	} else {
460df828598SMugunthan V N 		dev_info(priv->dev, "phy found : id is : 0x%x\n",
461df828598SMugunthan V N 			 slave->phy->phy_id);
462df828598SMugunthan V N 		phy_start(slave->phy);
463df828598SMugunthan V N 	}
464df828598SMugunthan V N }
465df828598SMugunthan V N 
466df828598SMugunthan V N static void cpsw_init_host_port(struct cpsw_priv *priv)
467df828598SMugunthan V N {
468df828598SMugunthan V N 	/* soft reset the controller and initialize ale */
469df828598SMugunthan V N 	soft_reset("cpsw", &priv->regs->soft_reset);
470df828598SMugunthan V N 	cpsw_ale_start(priv->ale);
471df828598SMugunthan V N 
472df828598SMugunthan V N 	/* switch to vlan unaware mode */
473df828598SMugunthan V N 	cpsw_ale_control_set(priv->ale, 0, ALE_VLAN_AWARE, 0);
474df828598SMugunthan V N 
475df828598SMugunthan V N 	/* setup host port priority mapping */
476df828598SMugunthan V N 	__raw_writel(CPDMA_TX_PRIORITY_MAP,
477df828598SMugunthan V N 		     &priv->host_port_regs->cpdma_tx_pri_map);
478df828598SMugunthan V N 	__raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
479df828598SMugunthan V N 
480df828598SMugunthan V N 	cpsw_ale_control_set(priv->ale, priv->host_port,
481df828598SMugunthan V N 			     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
482df828598SMugunthan V N 
483df828598SMugunthan V N 	cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port, 0);
484df828598SMugunthan V N 	cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
485df828598SMugunthan V N 			   1 << priv->host_port, 0, ALE_MCAST_FWD_2);
486df828598SMugunthan V N }
487df828598SMugunthan V N 
488df828598SMugunthan V N static int cpsw_ndo_open(struct net_device *ndev)
489df828598SMugunthan V N {
490df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
491df828598SMugunthan V N 	int i, ret;
492df828598SMugunthan V N 	u32 reg;
493df828598SMugunthan V N 
494df828598SMugunthan V N 	cpsw_intr_disable(priv);
495df828598SMugunthan V N 	netif_carrier_off(ndev);
496df828598SMugunthan V N 
497df828598SMugunthan V N 	ret = clk_enable(priv->clk);
498df828598SMugunthan V N 	if (ret < 0) {
499df828598SMugunthan V N 		dev_err(priv->dev, "unable to turn on device clock\n");
500df828598SMugunthan V N 		return ret;
501df828598SMugunthan V N 	}
502df828598SMugunthan V N 
503df828598SMugunthan V N 	reg = __raw_readl(&priv->regs->id_ver);
504df828598SMugunthan V N 
505df828598SMugunthan V N 	dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
506df828598SMugunthan V N 		 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
507df828598SMugunthan V N 		 CPSW_RTL_VERSION(reg));
508df828598SMugunthan V N 
509df828598SMugunthan V N 	/* initialize host and slave ports */
510df828598SMugunthan V N 	cpsw_init_host_port(priv);
511df828598SMugunthan V N 	for_each_slave(priv, cpsw_slave_open, priv);
512df828598SMugunthan V N 
513df828598SMugunthan V N 	/* setup tx dma to fixed prio and zero offset */
514df828598SMugunthan V N 	cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1);
515df828598SMugunthan V N 	cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0);
516df828598SMugunthan V N 
517df828598SMugunthan V N 	/* disable priority elevation and enable statistics on all ports */
518df828598SMugunthan V N 	__raw_writel(0, &priv->regs->ptype);
519df828598SMugunthan V N 
520df828598SMugunthan V N 	/* enable statistics collection only on the host port */
521df828598SMugunthan V N 	__raw_writel(0x7, &priv->regs->stat_port_en);
522df828598SMugunthan V N 
523df828598SMugunthan V N 	if (WARN_ON(!priv->data.rx_descs))
524df828598SMugunthan V N 		priv->data.rx_descs = 128;
525df828598SMugunthan V N 
526df828598SMugunthan V N 	for (i = 0; i < priv->data.rx_descs; i++) {
527df828598SMugunthan V N 		struct sk_buff *skb;
528df828598SMugunthan V N 
529df828598SMugunthan V N 		ret = -ENOMEM;
530df828598SMugunthan V N 		skb = netdev_alloc_skb_ip_align(priv->ndev,
531df828598SMugunthan V N 						priv->rx_packet_max);
532df828598SMugunthan V N 		if (!skb)
533df828598SMugunthan V N 			break;
534df828598SMugunthan V N 		ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
535df828598SMugunthan V N 					skb_tailroom(skb), GFP_KERNEL);
536df828598SMugunthan V N 		if (WARN_ON(ret < 0))
537df828598SMugunthan V N 			break;
538df828598SMugunthan V N 	}
539df828598SMugunthan V N 	/* continue even if we didn't manage to submit all receive descs */
540df828598SMugunthan V N 	cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i);
541df828598SMugunthan V N 
542df828598SMugunthan V N 	cpdma_ctlr_start(priv->dma);
543df828598SMugunthan V N 	cpsw_intr_enable(priv);
544df828598SMugunthan V N 	napi_enable(&priv->napi);
545df828598SMugunthan V N 	cpdma_ctlr_eoi(priv->dma);
546df828598SMugunthan V N 
547df828598SMugunthan V N 	return 0;
548df828598SMugunthan V N }
549df828598SMugunthan V N 
550df828598SMugunthan V N static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv)
551df828598SMugunthan V N {
552df828598SMugunthan V N 	if (!slave->phy)
553df828598SMugunthan V N 		return;
554df828598SMugunthan V N 	phy_stop(slave->phy);
555df828598SMugunthan V N 	phy_disconnect(slave->phy);
556df828598SMugunthan V N 	slave->phy = NULL;
557df828598SMugunthan V N }
558df828598SMugunthan V N 
559df828598SMugunthan V N static int cpsw_ndo_stop(struct net_device *ndev)
560df828598SMugunthan V N {
561df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
562df828598SMugunthan V N 
563df828598SMugunthan V N 	cpsw_info(priv, ifdown, "shutting down cpsw device\n");
564df828598SMugunthan V N 	cpsw_intr_disable(priv);
565df828598SMugunthan V N 	cpdma_ctlr_int_ctrl(priv->dma, false);
566df828598SMugunthan V N 	cpdma_ctlr_stop(priv->dma);
567df828598SMugunthan V N 	netif_stop_queue(priv->ndev);
568df828598SMugunthan V N 	napi_disable(&priv->napi);
569df828598SMugunthan V N 	netif_carrier_off(priv->ndev);
570df828598SMugunthan V N 	cpsw_ale_stop(priv->ale);
571df828598SMugunthan V N 	for_each_slave(priv, cpsw_slave_stop, priv);
572df828598SMugunthan V N 	clk_disable(priv->clk);
573df828598SMugunthan V N 	return 0;
574df828598SMugunthan V N }
575df828598SMugunthan V N 
576df828598SMugunthan V N static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
577df828598SMugunthan V N 				       struct net_device *ndev)
578df828598SMugunthan V N {
579df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
580df828598SMugunthan V N 	int ret;
581df828598SMugunthan V N 
582df828598SMugunthan V N 	ndev->trans_start = jiffies;
583df828598SMugunthan V N 
584df828598SMugunthan V N 	if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
585df828598SMugunthan V N 		cpsw_err(priv, tx_err, "packet pad failed\n");
586df828598SMugunthan V N 		priv->stats.tx_dropped++;
587df828598SMugunthan V N 		return NETDEV_TX_OK;
588df828598SMugunthan V N 	}
589df828598SMugunthan V N 
590df828598SMugunthan V N 	ret = cpdma_chan_submit(priv->txch, skb, skb->data,
591df828598SMugunthan V N 				skb->len, GFP_KERNEL);
592df828598SMugunthan V N 	if (unlikely(ret != 0)) {
593df828598SMugunthan V N 		cpsw_err(priv, tx_err, "desc submit failed\n");
594df828598SMugunthan V N 		goto fail;
595df828598SMugunthan V N 	}
596df828598SMugunthan V N 
597df828598SMugunthan V N 	return NETDEV_TX_OK;
598df828598SMugunthan V N fail:
599df828598SMugunthan V N 	priv->stats.tx_dropped++;
600df828598SMugunthan V N 	netif_stop_queue(ndev);
601df828598SMugunthan V N 	return NETDEV_TX_BUSY;
602df828598SMugunthan V N }
603df828598SMugunthan V N 
604df828598SMugunthan V N static void cpsw_ndo_change_rx_flags(struct net_device *ndev, int flags)
605df828598SMugunthan V N {
606df828598SMugunthan V N 	/*
607df828598SMugunthan V N 	 * The switch cannot operate in promiscuous mode without substantial
608df828598SMugunthan V N 	 * headache.  For promiscuous mode to work, we would need to put the
609df828598SMugunthan V N 	 * ALE in bypass mode and route all traffic to the host port.
610df828598SMugunthan V N 	 * Subsequently, the host will need to operate as a "bridge", learn,
611df828598SMugunthan V N 	 * and flood as needed.  For now, we simply complain here and
612df828598SMugunthan V N 	 * do nothing about it :-)
613df828598SMugunthan V N 	 */
614df828598SMugunthan V N 	if ((flags & IFF_PROMISC) && (ndev->flags & IFF_PROMISC))
615df828598SMugunthan V N 		dev_err(&ndev->dev, "promiscuity ignored!\n");
616df828598SMugunthan V N 
617df828598SMugunthan V N 	/*
618df828598SMugunthan V N 	 * The switch cannot filter multicast traffic unless it is configured
619df828598SMugunthan V N 	 * in "VLAN Aware" mode.  Unfortunately, VLAN awareness requires a
620df828598SMugunthan V N 	 * whole bunch of additional logic that this driver does not implement
621df828598SMugunthan V N 	 * at present.
622df828598SMugunthan V N 	 */
623df828598SMugunthan V N 	if ((flags & IFF_ALLMULTI) && !(ndev->flags & IFF_ALLMULTI))
624df828598SMugunthan V N 		dev_err(&ndev->dev, "multicast traffic cannot be filtered!\n");
625df828598SMugunthan V N }
626df828598SMugunthan V N 
627df828598SMugunthan V N static void cpsw_ndo_tx_timeout(struct net_device *ndev)
628df828598SMugunthan V N {
629df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
630df828598SMugunthan V N 
631df828598SMugunthan V N 	cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
632df828598SMugunthan V N 	priv->stats.tx_errors++;
633df828598SMugunthan V N 	cpsw_intr_disable(priv);
634df828598SMugunthan V N 	cpdma_ctlr_int_ctrl(priv->dma, false);
635df828598SMugunthan V N 	cpdma_chan_stop(priv->txch);
636df828598SMugunthan V N 	cpdma_chan_start(priv->txch);
637df828598SMugunthan V N 	cpdma_ctlr_int_ctrl(priv->dma, true);
638df828598SMugunthan V N 	cpsw_intr_enable(priv);
639df828598SMugunthan V N 	cpdma_ctlr_eoi(priv->dma);
640df828598SMugunthan V N }
641df828598SMugunthan V N 
642df828598SMugunthan V N static struct net_device_stats *cpsw_ndo_get_stats(struct net_device *ndev)
643df828598SMugunthan V N {
644df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
645df828598SMugunthan V N 	return &priv->stats;
646df828598SMugunthan V N }
647df828598SMugunthan V N 
648df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER
649df828598SMugunthan V N static void cpsw_ndo_poll_controller(struct net_device *ndev)
650df828598SMugunthan V N {
651df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
652df828598SMugunthan V N 
653df828598SMugunthan V N 	cpsw_intr_disable(priv);
654df828598SMugunthan V N 	cpdma_ctlr_int_ctrl(priv->dma, false);
655df828598SMugunthan V N 	cpsw_interrupt(ndev->irq, priv);
656df828598SMugunthan V N 	cpdma_ctlr_int_ctrl(priv->dma, true);
657df828598SMugunthan V N 	cpsw_intr_enable(priv);
658df828598SMugunthan V N 	cpdma_ctlr_eoi(priv->dma);
659df828598SMugunthan V N }
660df828598SMugunthan V N #endif
661df828598SMugunthan V N 
662df828598SMugunthan V N static const struct net_device_ops cpsw_netdev_ops = {
663df828598SMugunthan V N 	.ndo_open		= cpsw_ndo_open,
664df828598SMugunthan V N 	.ndo_stop		= cpsw_ndo_stop,
665df828598SMugunthan V N 	.ndo_start_xmit		= cpsw_ndo_start_xmit,
666df828598SMugunthan V N 	.ndo_change_rx_flags	= cpsw_ndo_change_rx_flags,
667df828598SMugunthan V N 	.ndo_validate_addr	= eth_validate_addr,
6685c473ed2SDavid S. Miller 	.ndo_change_mtu		= eth_change_mtu,
669df828598SMugunthan V N 	.ndo_tx_timeout		= cpsw_ndo_tx_timeout,
670df828598SMugunthan V N 	.ndo_get_stats		= cpsw_ndo_get_stats,
671df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER
672df828598SMugunthan V N 	.ndo_poll_controller	= cpsw_ndo_poll_controller,
673df828598SMugunthan V N #endif
674df828598SMugunthan V N };
675df828598SMugunthan V N 
676df828598SMugunthan V N static void cpsw_get_drvinfo(struct net_device *ndev,
677df828598SMugunthan V N 			     struct ethtool_drvinfo *info)
678df828598SMugunthan V N {
679df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
680df828598SMugunthan V N 	strcpy(info->driver, "TI CPSW Driver v1.0");
681df828598SMugunthan V N 	strcpy(info->version, "1.0");
682df828598SMugunthan V N 	strcpy(info->bus_info, priv->pdev->name);
683df828598SMugunthan V N }
684df828598SMugunthan V N 
685df828598SMugunthan V N static u32 cpsw_get_msglevel(struct net_device *ndev)
686df828598SMugunthan V N {
687df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
688df828598SMugunthan V N 	return priv->msg_enable;
689df828598SMugunthan V N }
690df828598SMugunthan V N 
691df828598SMugunthan V N static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
692df828598SMugunthan V N {
693df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
694df828598SMugunthan V N 	priv->msg_enable = value;
695df828598SMugunthan V N }
696df828598SMugunthan V N 
697df828598SMugunthan V N static const struct ethtool_ops cpsw_ethtool_ops = {
698df828598SMugunthan V N 	.get_drvinfo	= cpsw_get_drvinfo,
699df828598SMugunthan V N 	.get_msglevel	= cpsw_get_msglevel,
700df828598SMugunthan V N 	.set_msglevel	= cpsw_set_msglevel,
701df828598SMugunthan V N 	.get_link	= ethtool_op_get_link,
702df828598SMugunthan V N };
703df828598SMugunthan V N 
704df828598SMugunthan V N static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv)
705df828598SMugunthan V N {
706df828598SMugunthan V N 	void __iomem		*regs = priv->regs;
707df828598SMugunthan V N 	int			slave_num = slave->slave_num;
708df828598SMugunthan V N 	struct cpsw_slave_data	*data = priv->data.slave_data + slave_num;
709df828598SMugunthan V N 
710df828598SMugunthan V N 	slave->data	= data;
711df828598SMugunthan V N 	slave->regs	= regs + data->slave_reg_ofs;
712df828598SMugunthan V N 	slave->sliver	= regs + data->sliver_reg_ofs;
713df828598SMugunthan V N }
714df828598SMugunthan V N 
715df828598SMugunthan V N static int __devinit cpsw_probe(struct platform_device *pdev)
716df828598SMugunthan V N {
717df828598SMugunthan V N 	struct cpsw_platform_data	*data = pdev->dev.platform_data;
718df828598SMugunthan V N 	struct net_device		*ndev;
719df828598SMugunthan V N 	struct cpsw_priv		*priv;
720df828598SMugunthan V N 	struct cpdma_params		dma_params;
721df828598SMugunthan V N 	struct cpsw_ale_params		ale_params;
722df828598SMugunthan V N 	void __iomem			*regs;
723df828598SMugunthan V N 	struct resource			*res;
724df828598SMugunthan V N 	int ret = 0, i, k = 0;
725df828598SMugunthan V N 
726df828598SMugunthan V N 	if (!data) {
727df828598SMugunthan V N 		pr_err("platform data missing\n");
728df828598SMugunthan V N 		return -ENODEV;
729df828598SMugunthan V N 	}
730df828598SMugunthan V N 
731df828598SMugunthan V N 	ndev = alloc_etherdev(sizeof(struct cpsw_priv));
732df828598SMugunthan V N 	if (!ndev) {
733df828598SMugunthan V N 		pr_err("error allocating net_device\n");
734df828598SMugunthan V N 		return -ENOMEM;
735df828598SMugunthan V N 	}
736df828598SMugunthan V N 
737df828598SMugunthan V N 	platform_set_drvdata(pdev, ndev);
738df828598SMugunthan V N 	priv = netdev_priv(ndev);
739df828598SMugunthan V N 	spin_lock_init(&priv->lock);
740df828598SMugunthan V N 	priv->data = *data;
741df828598SMugunthan V N 	priv->pdev = pdev;
742df828598SMugunthan V N 	priv->ndev = ndev;
743df828598SMugunthan V N 	priv->dev  = &ndev->dev;
744df828598SMugunthan V N 	priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
745df828598SMugunthan V N 	priv->rx_packet_max = max(rx_packet_max, 128);
746df828598SMugunthan V N 
747df828598SMugunthan V N 	if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
748df828598SMugunthan V N 		memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
749df828598SMugunthan V N 		pr_info("Detected MACID = %pM", priv->mac_addr);
750df828598SMugunthan V N 	} else {
751df828598SMugunthan V N 		random_ether_addr(priv->mac_addr);
752df828598SMugunthan V N 		pr_info("Random MACID = %pM", priv->mac_addr);
753df828598SMugunthan V N 	}
754df828598SMugunthan V N 
755df828598SMugunthan V N 	memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
756df828598SMugunthan V N 
757df828598SMugunthan V N 	priv->slaves = kzalloc(sizeof(struct cpsw_slave) * data->slaves,
758df828598SMugunthan V N 			       GFP_KERNEL);
759df828598SMugunthan V N 	if (!priv->slaves) {
760df828598SMugunthan V N 		ret = -EBUSY;
761df828598SMugunthan V N 		goto clean_ndev_ret;
762df828598SMugunthan V N 	}
763df828598SMugunthan V N 	for (i = 0; i < data->slaves; i++)
764df828598SMugunthan V N 		priv->slaves[i].slave_num = i;
765df828598SMugunthan V N 
766df828598SMugunthan V N 	priv->clk = clk_get(&pdev->dev, NULL);
767df828598SMugunthan V N 	if (IS_ERR(priv->clk)) {
768df828598SMugunthan V N 		dev_err(priv->dev, "failed to get device clock)\n");
769df828598SMugunthan V N 		ret = -EBUSY;
770df828598SMugunthan V N 	}
771df828598SMugunthan V N 
772df828598SMugunthan V N 	priv->cpsw_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
773df828598SMugunthan V N 	if (!priv->cpsw_res) {
774df828598SMugunthan V N 		dev_err(priv->dev, "error getting i/o resource\n");
775df828598SMugunthan V N 		ret = -ENOENT;
776df828598SMugunthan V N 		goto clean_clk_ret;
777df828598SMugunthan V N 	}
778df828598SMugunthan V N 
779df828598SMugunthan V N 	if (!request_mem_region(priv->cpsw_res->start,
780df828598SMugunthan V N 				resource_size(priv->cpsw_res), ndev->name)) {
781df828598SMugunthan V N 		dev_err(priv->dev, "failed request i/o region\n");
782df828598SMugunthan V N 		ret = -ENXIO;
783df828598SMugunthan V N 		goto clean_clk_ret;
784df828598SMugunthan V N 	}
785df828598SMugunthan V N 
786df828598SMugunthan V N 	regs = ioremap(priv->cpsw_res->start, resource_size(priv->cpsw_res));
787df828598SMugunthan V N 	if (!regs) {
788df828598SMugunthan V N 		dev_err(priv->dev, "unable to map i/o region\n");
789df828598SMugunthan V N 		goto clean_cpsw_iores_ret;
790df828598SMugunthan V N 	}
791df828598SMugunthan V N 	priv->regs = regs;
792df828598SMugunthan V N 	priv->host_port = data->host_port_num;
793df828598SMugunthan V N 	priv->host_port_regs = regs + data->host_port_reg_ofs;
794df828598SMugunthan V N 
795df828598SMugunthan V N 	priv->cpsw_ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
796df828598SMugunthan V N 	if (!priv->cpsw_ss_res) {
797df828598SMugunthan V N 		dev_err(priv->dev, "error getting i/o resource\n");
798df828598SMugunthan V N 		ret = -ENOENT;
799df828598SMugunthan V N 		goto clean_clk_ret;
800df828598SMugunthan V N 	}
801df828598SMugunthan V N 
802df828598SMugunthan V N 	if (!request_mem_region(priv->cpsw_ss_res->start,
803df828598SMugunthan V N 			resource_size(priv->cpsw_ss_res), ndev->name)) {
804df828598SMugunthan V N 		dev_err(priv->dev, "failed request i/o region\n");
805df828598SMugunthan V N 		ret = -ENXIO;
806df828598SMugunthan V N 		goto clean_clk_ret;
807df828598SMugunthan V N 	}
808df828598SMugunthan V N 
809df828598SMugunthan V N 	regs = ioremap(priv->cpsw_ss_res->start,
810df828598SMugunthan V N 				resource_size(priv->cpsw_ss_res));
811df828598SMugunthan V N 	if (!regs) {
812df828598SMugunthan V N 		dev_err(priv->dev, "unable to map i/o region\n");
813df828598SMugunthan V N 		goto clean_cpsw_ss_iores_ret;
814df828598SMugunthan V N 	}
815df828598SMugunthan V N 	priv->ss_regs = regs;
816df828598SMugunthan V N 
817df828598SMugunthan V N 	for_each_slave(priv, cpsw_slave_init, priv);
818df828598SMugunthan V N 
819df828598SMugunthan V N 	memset(&dma_params, 0, sizeof(dma_params));
820df828598SMugunthan V N 	dma_params.dev		= &pdev->dev;
821df828598SMugunthan V N 	dma_params.dmaregs	= cpsw_dma_regs((u32)priv->regs,
822df828598SMugunthan V N 						data->cpdma_reg_ofs);
823df828598SMugunthan V N 	dma_params.rxthresh	= cpsw_dma_rxthresh((u32)priv->regs,
824df828598SMugunthan V N 						    data->cpdma_reg_ofs);
825df828598SMugunthan V N 	dma_params.rxfree	= cpsw_dma_rxfree((u32)priv->regs,
826df828598SMugunthan V N 						  data->cpdma_reg_ofs);
827df828598SMugunthan V N 	dma_params.txhdp	= cpsw_dma_txhdp((u32)priv->regs,
828df828598SMugunthan V N 						 data->cpdma_sram_ofs);
829df828598SMugunthan V N 	dma_params.rxhdp	= cpsw_dma_rxhdp((u32)priv->regs,
830df828598SMugunthan V N 						 data->cpdma_sram_ofs);
831df828598SMugunthan V N 	dma_params.txcp		= cpsw_dma_txcp((u32)priv->regs,
832df828598SMugunthan V N 						data->cpdma_sram_ofs);
833df828598SMugunthan V N 	dma_params.rxcp		= cpsw_dma_rxcp((u32)priv->regs,
834df828598SMugunthan V N 						data->cpdma_sram_ofs);
835df828598SMugunthan V N 
836df828598SMugunthan V N 	dma_params.num_chan		= data->channels;
837df828598SMugunthan V N 	dma_params.has_soft_reset	= true;
838df828598SMugunthan V N 	dma_params.min_packet_size	= CPSW_MIN_PACKET_SIZE;
839df828598SMugunthan V N 	dma_params.desc_mem_size	= data->bd_ram_size;
840df828598SMugunthan V N 	dma_params.desc_align		= 16;
841df828598SMugunthan V N 	dma_params.has_ext_regs		= true;
842df828598SMugunthan V N 	dma_params.desc_mem_phys        = data->no_bd_ram ? 0 :
843df828598SMugunthan V N 			(u32 __force)priv->cpsw_res->start + data->bd_ram_ofs;
844df828598SMugunthan V N 	dma_params.desc_hw_addr         = data->hw_ram_addr ?
845df828598SMugunthan V N 			data->hw_ram_addr : dma_params.desc_mem_phys ;
846df828598SMugunthan V N 
847df828598SMugunthan V N 	priv->dma = cpdma_ctlr_create(&dma_params);
848df828598SMugunthan V N 	if (!priv->dma) {
849df828598SMugunthan V N 		dev_err(priv->dev, "error initializing dma\n");
850df828598SMugunthan V N 		ret = -ENOMEM;
851df828598SMugunthan V N 		goto clean_iomap_ret;
852df828598SMugunthan V N 	}
853df828598SMugunthan V N 
854df828598SMugunthan V N 	priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
855df828598SMugunthan V N 				       cpsw_tx_handler);
856df828598SMugunthan V N 	priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
857df828598SMugunthan V N 				       cpsw_rx_handler);
858df828598SMugunthan V N 
859df828598SMugunthan V N 	if (WARN_ON(!priv->txch || !priv->rxch)) {
860df828598SMugunthan V N 		dev_err(priv->dev, "error initializing dma channels\n");
861df828598SMugunthan V N 		ret = -ENOMEM;
862df828598SMugunthan V N 		goto clean_dma_ret;
863df828598SMugunthan V N 	}
864df828598SMugunthan V N 
865df828598SMugunthan V N 	memset(&ale_params, 0, sizeof(ale_params));
866df828598SMugunthan V N 	ale_params.dev			= &ndev->dev;
867df828598SMugunthan V N 	ale_params.ale_regs		= (void *)((u32)priv->regs) +
868df828598SMugunthan V N 						((u32)data->ale_reg_ofs);
869df828598SMugunthan V N 	ale_params.ale_ageout		= ale_ageout;
870df828598SMugunthan V N 	ale_params.ale_entries		= data->ale_entries;
871df828598SMugunthan V N 	ale_params.ale_ports		= data->slaves;
872df828598SMugunthan V N 
873df828598SMugunthan V N 	priv->ale = cpsw_ale_create(&ale_params);
874df828598SMugunthan V N 	if (!priv->ale) {
875df828598SMugunthan V N 		dev_err(priv->dev, "error initializing ale engine\n");
876df828598SMugunthan V N 		ret = -ENODEV;
877df828598SMugunthan V N 		goto clean_dma_ret;
878df828598SMugunthan V N 	}
879df828598SMugunthan V N 
880df828598SMugunthan V N 	ndev->irq = platform_get_irq(pdev, 0);
881df828598SMugunthan V N 	if (ndev->irq < 0) {
882df828598SMugunthan V N 		dev_err(priv->dev, "error getting irq resource\n");
883df828598SMugunthan V N 		ret = -ENOENT;
884df828598SMugunthan V N 		goto clean_ale_ret;
885df828598SMugunthan V N 	}
886df828598SMugunthan V N 
887df828598SMugunthan V N 	while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
888df828598SMugunthan V N 		for (i = res->start; i <= res->end; i++) {
889df828598SMugunthan V N 			if (request_irq(i, cpsw_interrupt, IRQF_DISABLED,
890df828598SMugunthan V N 					dev_name(&pdev->dev), priv)) {
891df828598SMugunthan V N 				dev_err(priv->dev, "error attaching irq\n");
892df828598SMugunthan V N 				goto clean_ale_ret;
893df828598SMugunthan V N 			}
894df828598SMugunthan V N 			priv->irqs_table[k] = i;
895df828598SMugunthan V N 			priv->num_irqs = k;
896df828598SMugunthan V N 		}
897df828598SMugunthan V N 		k++;
898df828598SMugunthan V N 	}
899df828598SMugunthan V N 
900df828598SMugunthan V N 	ndev->flags |= IFF_ALLMULTI;	/* see cpsw_ndo_change_rx_flags() */
901df828598SMugunthan V N 
902df828598SMugunthan V N 	ndev->netdev_ops = &cpsw_netdev_ops;
903df828598SMugunthan V N 	SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
904df828598SMugunthan V N 	netif_napi_add(ndev, &priv->napi, cpsw_poll, CPSW_POLL_WEIGHT);
905df828598SMugunthan V N 
906df828598SMugunthan V N 	/* register the network device */
907df828598SMugunthan V N 	SET_NETDEV_DEV(ndev, &pdev->dev);
908df828598SMugunthan V N 	ret = register_netdev(ndev);
909df828598SMugunthan V N 	if (ret) {
910df828598SMugunthan V N 		dev_err(priv->dev, "error registering net device\n");
911df828598SMugunthan V N 		ret = -ENODEV;
912df828598SMugunthan V N 		goto clean_irq_ret;
913df828598SMugunthan V N 	}
914df828598SMugunthan V N 
915df828598SMugunthan V N 	cpsw_notice(priv, probe, "initialized device (regs %x, irq %d)\n",
916df828598SMugunthan V N 		  priv->cpsw_res->start, ndev->irq);
917df828598SMugunthan V N 
918df828598SMugunthan V N 	return 0;
919df828598SMugunthan V N 
920df828598SMugunthan V N clean_irq_ret:
921df828598SMugunthan V N 	free_irq(ndev->irq, priv);
922df828598SMugunthan V N clean_ale_ret:
923df828598SMugunthan V N 	cpsw_ale_destroy(priv->ale);
924df828598SMugunthan V N clean_dma_ret:
925df828598SMugunthan V N 	cpdma_chan_destroy(priv->txch);
926df828598SMugunthan V N 	cpdma_chan_destroy(priv->rxch);
927df828598SMugunthan V N 	cpdma_ctlr_destroy(priv->dma);
928df828598SMugunthan V N clean_iomap_ret:
929df828598SMugunthan V N 	iounmap(priv->regs);
930df828598SMugunthan V N clean_cpsw_ss_iores_ret:
931df828598SMugunthan V N 	release_mem_region(priv->cpsw_ss_res->start,
932df828598SMugunthan V N 			   resource_size(priv->cpsw_ss_res));
933df828598SMugunthan V N clean_cpsw_iores_ret:
934df828598SMugunthan V N 	release_mem_region(priv->cpsw_res->start,
935df828598SMugunthan V N 			   resource_size(priv->cpsw_res));
936df828598SMugunthan V N clean_clk_ret:
937df828598SMugunthan V N 	clk_put(priv->clk);
938df828598SMugunthan V N 	kfree(priv->slaves);
939df828598SMugunthan V N clean_ndev_ret:
940df828598SMugunthan V N 	free_netdev(ndev);
941df828598SMugunthan V N 	return ret;
942df828598SMugunthan V N }
943df828598SMugunthan V N 
944df828598SMugunthan V N static int __devexit cpsw_remove(struct platform_device *pdev)
945df828598SMugunthan V N {
946df828598SMugunthan V N 	struct net_device *ndev = platform_get_drvdata(pdev);
947df828598SMugunthan V N 	struct cpsw_priv *priv = netdev_priv(ndev);
948df828598SMugunthan V N 
949df828598SMugunthan V N 	pr_info("removing device");
950df828598SMugunthan V N 	platform_set_drvdata(pdev, NULL);
951df828598SMugunthan V N 
952df828598SMugunthan V N 	free_irq(ndev->irq, priv);
953df828598SMugunthan V N 	cpsw_ale_destroy(priv->ale);
954df828598SMugunthan V N 	cpdma_chan_destroy(priv->txch);
955df828598SMugunthan V N 	cpdma_chan_destroy(priv->rxch);
956df828598SMugunthan V N 	cpdma_ctlr_destroy(priv->dma);
957df828598SMugunthan V N 	iounmap(priv->regs);
958df828598SMugunthan V N 	release_mem_region(priv->cpsw_res->start,
959df828598SMugunthan V N 			   resource_size(priv->cpsw_res));
960df828598SMugunthan V N 	release_mem_region(priv->cpsw_ss_res->start,
961df828598SMugunthan V N 			   resource_size(priv->cpsw_ss_res));
962df828598SMugunthan V N 	clk_put(priv->clk);
963df828598SMugunthan V N 	kfree(priv->slaves);
964df828598SMugunthan V N 	free_netdev(ndev);
965df828598SMugunthan V N 
966df828598SMugunthan V N 	return 0;
967df828598SMugunthan V N }
968df828598SMugunthan V N 
969df828598SMugunthan V N static int cpsw_suspend(struct device *dev)
970df828598SMugunthan V N {
971df828598SMugunthan V N 	struct platform_device	*pdev = to_platform_device(dev);
972df828598SMugunthan V N 	struct net_device	*ndev = platform_get_drvdata(pdev);
973df828598SMugunthan V N 
974df828598SMugunthan V N 	if (netif_running(ndev))
975df828598SMugunthan V N 		cpsw_ndo_stop(ndev);
976df828598SMugunthan V N 	return 0;
977df828598SMugunthan V N }
978df828598SMugunthan V N 
979df828598SMugunthan V N static int cpsw_resume(struct device *dev)
980df828598SMugunthan V N {
981df828598SMugunthan V N 	struct platform_device	*pdev = to_platform_device(dev);
982df828598SMugunthan V N 	struct net_device	*ndev = platform_get_drvdata(pdev);
983df828598SMugunthan V N 
984df828598SMugunthan V N 	if (netif_running(ndev))
985df828598SMugunthan V N 		cpsw_ndo_open(ndev);
986df828598SMugunthan V N 	return 0;
987df828598SMugunthan V N }
988df828598SMugunthan V N 
989df828598SMugunthan V N static const struct dev_pm_ops cpsw_pm_ops = {
990df828598SMugunthan V N 	.suspend	= cpsw_suspend,
991df828598SMugunthan V N 	.resume		= cpsw_resume,
992df828598SMugunthan V N };
993df828598SMugunthan V N 
994df828598SMugunthan V N static struct platform_driver cpsw_driver = {
995df828598SMugunthan V N 	.driver = {
996df828598SMugunthan V N 		.name	 = "cpsw",
997df828598SMugunthan V N 		.owner	 = THIS_MODULE,
998df828598SMugunthan V N 		.pm	 = &cpsw_pm_ops,
999df828598SMugunthan V N 	},
1000df828598SMugunthan V N 	.probe = cpsw_probe,
1001df828598SMugunthan V N 	.remove = __devexit_p(cpsw_remove),
1002df828598SMugunthan V N };
1003df828598SMugunthan V N 
1004df828598SMugunthan V N static int __init cpsw_init(void)
1005df828598SMugunthan V N {
1006df828598SMugunthan V N 	return platform_driver_register(&cpsw_driver);
1007df828598SMugunthan V N }
1008df828598SMugunthan V N late_initcall(cpsw_init);
1009df828598SMugunthan V N 
1010df828598SMugunthan V N static void __exit cpsw_exit(void)
1011df828598SMugunthan V N {
1012df828598SMugunthan V N 	platform_driver_unregister(&cpsw_driver);
1013df828598SMugunthan V N }
1014df828598SMugunthan V N module_exit(cpsw_exit);
1015df828598SMugunthan V N 
1016df828598SMugunthan V N MODULE_LICENSE("GPL");
1017df828598SMugunthan V N MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
1018df828598SMugunthan V N MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
1019df828598SMugunthan V N MODULE_DESCRIPTION("TI CPSW Ethernet driver");
1020