blob: 9e63bff124fedd1a456af60407cb529017dda96e [file] [log] [blame]
Mugunthan V Ndf828592012-03-18 20:17:54 +00001/*
2 * Texas Instruments Ethernet Switch Driver
3 *
4 * Copyright (C) 2012 Texas Instruments
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
9 *
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/kernel.h>
17#include <linux/io.h>
18#include <linux/clk.h>
19#include <linux/timer.h>
20#include <linux/module.h>
21#include <linux/platform_device.h>
22#include <linux/irqreturn.h>
23#include <linux/interrupt.h>
24#include <linux/if_ether.h>
25#include <linux/etherdevice.h>
26#include <linux/netdevice.h>
Richard Cochran2e5b38a2012-10-29 08:45:20 +000027#include <linux/net_tstamp.h>
Mugunthan V Ndf828592012-03-18 20:17:54 +000028#include <linux/phy.h>
29#include <linux/workqueue.h>
30#include <linux/delay.h>
Mugunthan V Nf150bd72012-07-17 08:09:50 +000031#include <linux/pm_runtime.h>
Mugunthan V N2eb32b02012-07-30 10:17:14 +000032#include <linux/of.h>
33#include <linux/of_net.h>
34#include <linux/of_device.h>
Mugunthan V Ndf828592012-03-18 20:17:54 +000035
36#include <linux/platform_data/cpsw.h>
37
38#include "cpsw_ale.h"
Richard Cochran2e5b38a2012-10-29 08:45:20 +000039#include "cpts.h"
Mugunthan V Ndf828592012-03-18 20:17:54 +000040#include "davinci_cpdma.h"
41
42#define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
43 NETIF_MSG_DRV | NETIF_MSG_LINK | \
44 NETIF_MSG_IFUP | NETIF_MSG_INTR | \
45 NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
46 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
47 NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
48 NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
49 NETIF_MSG_RX_STATUS)
50
51#define cpsw_info(priv, type, format, ...) \
52do { \
53 if (netif_msg_##type(priv) && net_ratelimit()) \
54 dev_info(priv->dev, format, ## __VA_ARGS__); \
55} while (0)
56
57#define cpsw_err(priv, type, format, ...) \
58do { \
59 if (netif_msg_##type(priv) && net_ratelimit()) \
60 dev_err(priv->dev, format, ## __VA_ARGS__); \
61} while (0)
62
63#define cpsw_dbg(priv, type, format, ...) \
64do { \
65 if (netif_msg_##type(priv) && net_ratelimit()) \
66 dev_dbg(priv->dev, format, ## __VA_ARGS__); \
67} while (0)
68
69#define cpsw_notice(priv, type, format, ...) \
70do { \
71 if (netif_msg_##type(priv) && net_ratelimit()) \
72 dev_notice(priv->dev, format, ## __VA_ARGS__); \
73} while (0)
74
Mugunthan V N5c50a852012-10-29 08:45:11 +000075#define ALE_ALL_PORTS 0x7
76
Mugunthan V Ndf828592012-03-18 20:17:54 +000077#define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
78#define CPSW_MINOR_VERSION(reg) (reg & 0xff)
79#define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
80
Richard Cochrane90cfac2012-10-29 08:45:14 +000081#define CPSW_VERSION_1 0x19010a
82#define CPSW_VERSION_2 0x19010c
Richard Cochran549985e2012-11-14 09:07:56 +000083
84#define HOST_PORT_NUM 0
85#define SLIVER_SIZE 0x40
86
87#define CPSW1_HOST_PORT_OFFSET 0x028
88#define CPSW1_SLAVE_OFFSET 0x050
89#define CPSW1_SLAVE_SIZE 0x040
90#define CPSW1_CPDMA_OFFSET 0x100
91#define CPSW1_STATERAM_OFFSET 0x200
92#define CPSW1_CPTS_OFFSET 0x500
93#define CPSW1_ALE_OFFSET 0x600
94#define CPSW1_SLIVER_OFFSET 0x700
95
96#define CPSW2_HOST_PORT_OFFSET 0x108
97#define CPSW2_SLAVE_OFFSET 0x200
98#define CPSW2_SLAVE_SIZE 0x100
99#define CPSW2_CPDMA_OFFSET 0x800
100#define CPSW2_STATERAM_OFFSET 0xa00
101#define CPSW2_CPTS_OFFSET 0xc00
102#define CPSW2_ALE_OFFSET 0xd00
103#define CPSW2_SLIVER_OFFSET 0xd80
104#define CPSW2_BD_OFFSET 0x2000
105
Mugunthan V Ndf828592012-03-18 20:17:54 +0000106#define CPDMA_RXTHRESH 0x0c0
107#define CPDMA_RXFREE 0x0e0
108#define CPDMA_TXHDP 0x00
109#define CPDMA_RXHDP 0x20
110#define CPDMA_TXCP 0x40
111#define CPDMA_RXCP 0x60
112
Mugunthan V Ndf828592012-03-18 20:17:54 +0000113#define CPSW_POLL_WEIGHT 64
114#define CPSW_MIN_PACKET_SIZE 60
115#define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4)
116
117#define RX_PRIORITY_MAPPING 0x76543210
118#define TX_PRIORITY_MAPPING 0x33221100
119#define CPDMA_TX_PRIORITY_MAP 0x76543210
120
121#define cpsw_enable_irq(priv) \
122 do { \
123 u32 i; \
124 for (i = 0; i < priv->num_irqs; i++) \
125 enable_irq(priv->irqs_table[i]); \
126 } while (0);
127#define cpsw_disable_irq(priv) \
128 do { \
129 u32 i; \
130 for (i = 0; i < priv->num_irqs; i++) \
131 disable_irq_nosync(priv->irqs_table[i]); \
132 } while (0);
133
134static int debug_level;
135module_param(debug_level, int, 0);
136MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
137
138static int ale_ageout = 10;
139module_param(ale_ageout, int, 0);
140MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
141
142static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
143module_param(rx_packet_max, int, 0);
144MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
145
Richard Cochran996a5c22012-10-29 08:45:12 +0000146struct cpsw_wr_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000147 u32 id_ver;
148 u32 soft_reset;
149 u32 control;
150 u32 int_control;
151 u32 rx_thresh_en;
152 u32 rx_en;
153 u32 tx_en;
154 u32 misc_en;
155};
156
Richard Cochran996a5c22012-10-29 08:45:12 +0000157struct cpsw_ss_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000158 u32 id_ver;
159 u32 control;
160 u32 soft_reset;
161 u32 stat_port_en;
162 u32 ptype;
Richard Cochranbd357af2012-10-29 08:45:13 +0000163 u32 soft_idle;
164 u32 thru_rate;
165 u32 gap_thresh;
166 u32 tx_start_wds;
167 u32 flow_control;
168 u32 vlan_ltype;
169 u32 ts_ltype;
170 u32 dlr_ltype;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000171};
172
Richard Cochran9750a3a2012-10-29 08:45:15 +0000173/* CPSW_PORT_V1 */
174#define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
175#define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
176#define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
177#define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
178#define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
179#define CPSW1_TS_CTL 0x14 /* Time Sync Control */
180#define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
181#define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
182
183/* CPSW_PORT_V2 */
184#define CPSW2_CONTROL 0x00 /* Control Register */
185#define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
186#define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
187#define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
188#define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
189#define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
190#define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
191
192/* CPSW_PORT_V1 and V2 */
193#define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
194#define SA_HI 0x24 /* CPGMAC_SL Source Address High */
195#define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
196
197/* CPSW_PORT_V2 only */
198#define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
199#define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
200#define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
201#define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
202#define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
203#define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
204#define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
205#define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
206
207/* Bit definitions for the CPSW2_CONTROL register */
208#define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
209#define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
210#define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
211#define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
212#define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
213#define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
214#define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
215#define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
216#define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
217#define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
218#define TS_BIT8 (1<<8) /* ts_ttl_nonzero? */
219#define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
220#define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
221#define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
222#define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
223#define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
224
225#define CTRL_TS_BITS \
226 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 | TS_BIT8 | \
227 TS_ANNEX_D_EN | TS_LTYPE1_EN)
228
229#define CTRL_ALL_TS_MASK (CTRL_TS_BITS | TS_TX_EN | TS_RX_EN)
230#define CTRL_TX_TS_BITS (CTRL_TS_BITS | TS_TX_EN)
231#define CTRL_RX_TS_BITS (CTRL_TS_BITS | TS_RX_EN)
232
233/* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
234#define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
235#define TS_SEQ_ID_OFFSET_MASK (0x3f)
236#define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
237#define TS_MSG_TYPE_EN_MASK (0xffff)
238
239/* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
240#define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
Mugunthan V Ndf828592012-03-18 20:17:54 +0000241
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000242/* Bit definitions for the CPSW1_TS_CTL register */
243#define CPSW_V1_TS_RX_EN BIT(0)
244#define CPSW_V1_TS_TX_EN BIT(4)
245#define CPSW_V1_MSG_TYPE_OFS 16
246
247/* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
248#define CPSW_V1_SEQ_ID_OFS_SHIFT 16
249
Mugunthan V Ndf828592012-03-18 20:17:54 +0000250struct cpsw_host_regs {
251 u32 max_blks;
252 u32 blk_cnt;
253 u32 flow_thresh;
254 u32 port_vlan;
255 u32 tx_pri_map;
256 u32 cpdma_tx_pri_map;
257 u32 cpdma_rx_chan_map;
258};
259
260struct cpsw_sliver_regs {
261 u32 id_ver;
262 u32 mac_control;
263 u32 mac_status;
264 u32 soft_reset;
265 u32 rx_maxlen;
266 u32 __reserved_0;
267 u32 rx_pause;
268 u32 tx_pause;
269 u32 __reserved_1;
270 u32 rx_pri_map;
271};
272
273struct cpsw_slave {
Richard Cochran9750a3a2012-10-29 08:45:15 +0000274 void __iomem *regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000275 struct cpsw_sliver_regs __iomem *sliver;
276 int slave_num;
277 u32 mac_control;
278 struct cpsw_slave_data *data;
279 struct phy_device *phy;
280};
281
Richard Cochran9750a3a2012-10-29 08:45:15 +0000282static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
283{
284 return __raw_readl(slave->regs + offset);
285}
286
287static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
288{
289 __raw_writel(val, slave->regs + offset);
290}
291
Mugunthan V Ndf828592012-03-18 20:17:54 +0000292struct cpsw_priv {
293 spinlock_t lock;
294 struct platform_device *pdev;
295 struct net_device *ndev;
296 struct resource *cpsw_res;
Richard Cochrana65dd5b2012-11-02 22:25:29 +0000297 struct resource *cpsw_wr_res;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000298 struct napi_struct napi;
299 struct device *dev;
300 struct cpsw_platform_data data;
Richard Cochran996a5c22012-10-29 08:45:12 +0000301 struct cpsw_ss_regs __iomem *regs;
302 struct cpsw_wr_regs __iomem *wr_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000303 struct cpsw_host_regs __iomem *host_port_regs;
304 u32 msg_enable;
Richard Cochrane90cfac2012-10-29 08:45:14 +0000305 u32 version;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000306 struct net_device_stats stats;
307 int rx_packet_max;
308 int host_port;
309 struct clk *clk;
310 u8 mac_addr[ETH_ALEN];
311 struct cpsw_slave *slaves;
312 struct cpdma_ctlr *dma;
313 struct cpdma_chan *txch, *rxch;
314 struct cpsw_ale *ale;
315 /* snapshot of IRQ numbers */
316 u32 irqs_table[4];
317 u32 num_irqs;
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000318 struct cpts cpts;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000319};
320
321#define napi_to_priv(napi) container_of(napi, struct cpsw_priv, napi)
322#define for_each_slave(priv, func, arg...) \
323 do { \
324 int idx; \
325 for (idx = 0; idx < (priv)->data.slaves; idx++) \
326 (func)((priv)->slaves + idx, ##arg); \
327 } while (0)
328
Mugunthan V N5c50a852012-10-29 08:45:11 +0000329static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
330{
331 struct cpsw_priv *priv = netdev_priv(ndev);
332
333 if (ndev->flags & IFF_PROMISC) {
334 /* Enable promiscuous mode */
335 dev_err(priv->dev, "Ignoring Promiscuous mode\n");
336 return;
337 }
338
339 /* Clear all mcast from ALE */
340 cpsw_ale_flush_multicast(priv->ale, ALE_ALL_PORTS << priv->host_port);
341
342 if (!netdev_mc_empty(ndev)) {
343 struct netdev_hw_addr *ha;
344
345 /* program multicast address list into ALE register */
346 netdev_for_each_mc_addr(ha, ndev) {
347 cpsw_ale_add_mcast(priv->ale, (u8 *)ha->addr,
348 ALE_ALL_PORTS << priv->host_port, 0, 0);
349 }
350 }
351}
352
Mugunthan V Ndf828592012-03-18 20:17:54 +0000353static void cpsw_intr_enable(struct cpsw_priv *priv)
354{
Richard Cochran996a5c22012-10-29 08:45:12 +0000355 __raw_writel(0xFF, &priv->wr_regs->tx_en);
356 __raw_writel(0xFF, &priv->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000357
358 cpdma_ctlr_int_ctrl(priv->dma, true);
359 return;
360}
361
362static void cpsw_intr_disable(struct cpsw_priv *priv)
363{
Richard Cochran996a5c22012-10-29 08:45:12 +0000364 __raw_writel(0, &priv->wr_regs->tx_en);
365 __raw_writel(0, &priv->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000366
367 cpdma_ctlr_int_ctrl(priv->dma, false);
368 return;
369}
370
371void cpsw_tx_handler(void *token, int len, int status)
372{
373 struct sk_buff *skb = token;
374 struct net_device *ndev = skb->dev;
375 struct cpsw_priv *priv = netdev_priv(ndev);
376
Mugunthan V Nfae50822013-01-17 06:31:34 +0000377 /* Check whether the queue is stopped due to stalled tx dma, if the
378 * queue is stopped then start the queue as we have free desc for tx
379 */
Mugunthan V Ndf828592012-03-18 20:17:54 +0000380 if (unlikely(netif_queue_stopped(ndev)))
381 netif_start_queue(ndev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000382 cpts_tx_timestamp(&priv->cpts, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000383 priv->stats.tx_packets++;
384 priv->stats.tx_bytes += len;
385 dev_kfree_skb_any(skb);
386}
387
388void cpsw_rx_handler(void *token, int len, int status)
389{
390 struct sk_buff *skb = token;
391 struct net_device *ndev = skb->dev;
392 struct cpsw_priv *priv = netdev_priv(ndev);
393 int ret = 0;
394
395 /* free and bail if we are shutting down */
396 if (unlikely(!netif_running(ndev)) ||
397 unlikely(!netif_carrier_ok(ndev))) {
398 dev_kfree_skb_any(skb);
399 return;
400 }
401 if (likely(status >= 0)) {
402 skb_put(skb, len);
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000403 cpts_rx_timestamp(&priv->cpts, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000404 skb->protocol = eth_type_trans(skb, ndev);
405 netif_receive_skb(skb);
406 priv->stats.rx_bytes += len;
407 priv->stats.rx_packets++;
408 skb = NULL;
409 }
410
411 if (unlikely(!netif_running(ndev))) {
412 if (skb)
413 dev_kfree_skb_any(skb);
414 return;
415 }
416
417 if (likely(!skb)) {
418 skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
419 if (WARN_ON(!skb))
420 return;
421
422 ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
423 skb_tailroom(skb), GFP_KERNEL);
424 }
425 WARN_ON(ret < 0);
426}
427
428static irqreturn_t cpsw_interrupt(int irq, void *dev_id)
429{
430 struct cpsw_priv *priv = dev_id;
431
432 if (likely(netif_running(priv->ndev))) {
433 cpsw_intr_disable(priv);
434 cpsw_disable_irq(priv);
435 napi_schedule(&priv->napi);
436 }
437 return IRQ_HANDLED;
438}
439
440static inline int cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
441{
442 if (priv->host_port == 0)
443 return slave_num + 1;
444 else
445 return slave_num;
446}
447
448static int cpsw_poll(struct napi_struct *napi, int budget)
449{
450 struct cpsw_priv *priv = napi_to_priv(napi);
451 int num_tx, num_rx;
452
453 num_tx = cpdma_chan_process(priv->txch, 128);
454 num_rx = cpdma_chan_process(priv->rxch, budget);
455
456 if (num_rx || num_tx)
457 cpsw_dbg(priv, intr, "poll %d rx, %d tx pkts\n",
458 num_rx, num_tx);
459
460 if (num_rx < budget) {
461 napi_complete(napi);
462 cpsw_intr_enable(priv);
463 cpdma_ctlr_eoi(priv->dma);
464 cpsw_enable_irq(priv);
465 }
466
467 return num_rx;
468}
469
470static inline void soft_reset(const char *module, void __iomem *reg)
471{
472 unsigned long timeout = jiffies + HZ;
473
474 __raw_writel(1, reg);
475 do {
476 cpu_relax();
477 } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
478
479 WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
480}
481
482#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
483 ((mac)[2] << 16) | ((mac)[3] << 24))
484#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
485
486static void cpsw_set_slave_mac(struct cpsw_slave *slave,
487 struct cpsw_priv *priv)
488{
Richard Cochran9750a3a2012-10-29 08:45:15 +0000489 slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
490 slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000491}
492
493static void _cpsw_adjust_link(struct cpsw_slave *slave,
494 struct cpsw_priv *priv, bool *link)
495{
496 struct phy_device *phy = slave->phy;
497 u32 mac_control = 0;
498 u32 slave_port;
499
500 if (!phy)
501 return;
502
503 slave_port = cpsw_get_slave_port(priv, slave->slave_num);
504
505 if (phy->link) {
506 mac_control = priv->data.mac_control;
507
508 /* enable forwarding */
509 cpsw_ale_control_set(priv->ale, slave_port,
510 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
511
512 if (phy->speed == 1000)
513 mac_control |= BIT(7); /* GIGABITEN */
514 if (phy->duplex)
515 mac_control |= BIT(0); /* FULLDUPLEXEN */
Daniel Mack342b7b72012-09-27 09:19:34 +0000516
517 /* set speed_in input in case RMII mode is used in 100Mbps */
518 if (phy->speed == 100)
519 mac_control |= BIT(15);
520
Mugunthan V Ndf828592012-03-18 20:17:54 +0000521 *link = true;
522 } else {
523 mac_control = 0;
524 /* disable forwarding */
525 cpsw_ale_control_set(priv->ale, slave_port,
526 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
527 }
528
529 if (mac_control != slave->mac_control) {
530 phy_print_status(phy);
531 __raw_writel(mac_control, &slave->sliver->mac_control);
532 }
533
534 slave->mac_control = mac_control;
535}
536
537static void cpsw_adjust_link(struct net_device *ndev)
538{
539 struct cpsw_priv *priv = netdev_priv(ndev);
540 bool link = false;
541
542 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
543
544 if (link) {
545 netif_carrier_on(ndev);
546 if (netif_running(ndev))
547 netif_wake_queue(ndev);
548 } else {
549 netif_carrier_off(ndev);
550 netif_stop_queue(ndev);
551 }
552}
553
554static inline int __show_stat(char *buf, int maxlen, const char *name, u32 val)
555{
556 static char *leader = "........................................";
557
558 if (!val)
559 return 0;
560 else
561 return snprintf(buf, maxlen, "%s %s %10d\n", name,
562 leader + strlen(name), val);
563}
564
565static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
566{
567 char name[32];
568 u32 slave_port;
569
570 sprintf(name, "slave-%d", slave->slave_num);
571
572 soft_reset(name, &slave->sliver->soft_reset);
573
574 /* setup priority mapping */
575 __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
Richard Cochran9750a3a2012-10-29 08:45:15 +0000576
577 switch (priv->version) {
578 case CPSW_VERSION_1:
579 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
580 break;
581 case CPSW_VERSION_2:
582 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
583 break;
584 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000585
586 /* setup max packet size, and mac address */
587 __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
588 cpsw_set_slave_mac(slave, priv);
589
590 slave->mac_control = 0; /* no link yet */
591
592 slave_port = cpsw_get_slave_port(priv, slave->slave_num);
593
594 cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
595 1 << slave_port, 0, ALE_MCAST_FWD_2);
596
597 slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
Florian Fainellif9a8f832013-01-14 00:52:52 +0000598 &cpsw_adjust_link, slave->data->phy_if);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000599 if (IS_ERR(slave->phy)) {
600 dev_err(priv->dev, "phy %s not found on slave %d\n",
601 slave->data->phy_id, slave->slave_num);
602 slave->phy = NULL;
603 } else {
604 dev_info(priv->dev, "phy found : id is : 0x%x\n",
605 slave->phy->phy_id);
606 phy_start(slave->phy);
607 }
608}
609
610static void cpsw_init_host_port(struct cpsw_priv *priv)
611{
612 /* soft reset the controller and initialize ale */
613 soft_reset("cpsw", &priv->regs->soft_reset);
614 cpsw_ale_start(priv->ale);
615
616 /* switch to vlan unaware mode */
617 cpsw_ale_control_set(priv->ale, 0, ALE_VLAN_AWARE, 0);
618
619 /* setup host port priority mapping */
620 __raw_writel(CPDMA_TX_PRIORITY_MAP,
621 &priv->host_port_regs->cpdma_tx_pri_map);
622 __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
623
624 cpsw_ale_control_set(priv->ale, priv->host_port,
625 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
626
627 cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port, 0);
628 cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
629 1 << priv->host_port, 0, ALE_MCAST_FWD_2);
630}
631
632static int cpsw_ndo_open(struct net_device *ndev)
633{
634 struct cpsw_priv *priv = netdev_priv(ndev);
635 int i, ret;
636 u32 reg;
637
638 cpsw_intr_disable(priv);
639 netif_carrier_off(ndev);
640
Mugunthan V Nf150bd72012-07-17 08:09:50 +0000641 pm_runtime_get_sync(&priv->pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000642
Richard Cochran549985e2012-11-14 09:07:56 +0000643 reg = priv->version;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000644
645 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
646 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
647 CPSW_RTL_VERSION(reg));
648
649 /* initialize host and slave ports */
650 cpsw_init_host_port(priv);
651 for_each_slave(priv, cpsw_slave_open, priv);
652
653 /* setup tx dma to fixed prio and zero offset */
654 cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1);
655 cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0);
656
657 /* disable priority elevation and enable statistics on all ports */
658 __raw_writel(0, &priv->regs->ptype);
659
660 /* enable statistics collection only on the host port */
661 __raw_writel(0x7, &priv->regs->stat_port_en);
662
663 if (WARN_ON(!priv->data.rx_descs))
664 priv->data.rx_descs = 128;
665
666 for (i = 0; i < priv->data.rx_descs; i++) {
667 struct sk_buff *skb;
668
669 ret = -ENOMEM;
670 skb = netdev_alloc_skb_ip_align(priv->ndev,
671 priv->rx_packet_max);
672 if (!skb)
673 break;
674 ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
675 skb_tailroom(skb), GFP_KERNEL);
676 if (WARN_ON(ret < 0))
677 break;
678 }
679 /* continue even if we didn't manage to submit all receive descs */
680 cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i);
681
682 cpdma_ctlr_start(priv->dma);
683 cpsw_intr_enable(priv);
684 napi_enable(&priv->napi);
685 cpdma_ctlr_eoi(priv->dma);
686
687 return 0;
688}
689
690static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv)
691{
692 if (!slave->phy)
693 return;
694 phy_stop(slave->phy);
695 phy_disconnect(slave->phy);
696 slave->phy = NULL;
697}
698
699static int cpsw_ndo_stop(struct net_device *ndev)
700{
701 struct cpsw_priv *priv = netdev_priv(ndev);
702
703 cpsw_info(priv, ifdown, "shutting down cpsw device\n");
Mugunthan V Ndf828592012-03-18 20:17:54 +0000704 netif_stop_queue(priv->ndev);
705 napi_disable(&priv->napi);
706 netif_carrier_off(priv->ndev);
Mugunthan V N71380f9b2012-11-14 09:07:57 +0000707 cpsw_intr_disable(priv);
708 cpdma_ctlr_int_ctrl(priv->dma, false);
709 cpdma_ctlr_stop(priv->dma);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000710 cpsw_ale_stop(priv->ale);
711 for_each_slave(priv, cpsw_slave_stop, priv);
Mugunthan V Nf150bd72012-07-17 08:09:50 +0000712 pm_runtime_put_sync(&priv->pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000713 return 0;
714}
715
716static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
717 struct net_device *ndev)
718{
719 struct cpsw_priv *priv = netdev_priv(ndev);
720 int ret;
721
722 ndev->trans_start = jiffies;
723
724 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
725 cpsw_err(priv, tx_err, "packet pad failed\n");
726 priv->stats.tx_dropped++;
727 return NETDEV_TX_OK;
728 }
729
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000730 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && priv->cpts.tx_enable)
731 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
732
733 skb_tx_timestamp(skb);
734
Mugunthan V Ndf828592012-03-18 20:17:54 +0000735 ret = cpdma_chan_submit(priv->txch, skb, skb->data,
736 skb->len, GFP_KERNEL);
737 if (unlikely(ret != 0)) {
738 cpsw_err(priv, tx_err, "desc submit failed\n");
739 goto fail;
740 }
741
Mugunthan V Nfae50822013-01-17 06:31:34 +0000742 /* If there is no more tx desc left free then we need to
743 * tell the kernel to stop sending us tx frames.
744 */
745 if (unlikely(cpdma_check_free_tx_desc(priv->txch)))
746 netif_stop_queue(ndev);
747
Mugunthan V Ndf828592012-03-18 20:17:54 +0000748 return NETDEV_TX_OK;
749fail:
750 priv->stats.tx_dropped++;
751 netif_stop_queue(ndev);
752 return NETDEV_TX_BUSY;
753}
754
755static void cpsw_ndo_change_rx_flags(struct net_device *ndev, int flags)
756{
757 /*
758 * The switch cannot operate in promiscuous mode without substantial
759 * headache. For promiscuous mode to work, we would need to put the
760 * ALE in bypass mode and route all traffic to the host port.
761 * Subsequently, the host will need to operate as a "bridge", learn,
762 * and flood as needed. For now, we simply complain here and
763 * do nothing about it :-)
764 */
765 if ((flags & IFF_PROMISC) && (ndev->flags & IFF_PROMISC))
766 dev_err(&ndev->dev, "promiscuity ignored!\n");
767
768 /*
769 * The switch cannot filter multicast traffic unless it is configured
770 * in "VLAN Aware" mode. Unfortunately, VLAN awareness requires a
771 * whole bunch of additional logic that this driver does not implement
772 * at present.
773 */
774 if ((flags & IFF_ALLMULTI) && !(ndev->flags & IFF_ALLMULTI))
775 dev_err(&ndev->dev, "multicast traffic cannot be filtered!\n");
776}
777
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000778#ifdef CONFIG_TI_CPTS
779
780static void cpsw_hwtstamp_v1(struct cpsw_priv *priv)
781{
782 struct cpsw_slave *slave = &priv->slaves[priv->data.cpts_active_slave];
783 u32 ts_en, seq_id;
784
785 if (!priv->cpts.tx_enable && !priv->cpts.rx_enable) {
786 slave_write(slave, 0, CPSW1_TS_CTL);
787 return;
788 }
789
790 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
791 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
792
793 if (priv->cpts.tx_enable)
794 ts_en |= CPSW_V1_TS_TX_EN;
795
796 if (priv->cpts.rx_enable)
797 ts_en |= CPSW_V1_TS_RX_EN;
798
799 slave_write(slave, ts_en, CPSW1_TS_CTL);
800 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
801}
802
803static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
804{
805 struct cpsw_slave *slave = &priv->slaves[priv->data.cpts_active_slave];
806 u32 ctrl, mtype;
807
808 ctrl = slave_read(slave, CPSW2_CONTROL);
809 ctrl &= ~CTRL_ALL_TS_MASK;
810
811 if (priv->cpts.tx_enable)
812 ctrl |= CTRL_TX_TS_BITS;
813
814 if (priv->cpts.rx_enable)
815 ctrl |= CTRL_RX_TS_BITS;
816
817 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
818
819 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
820 slave_write(slave, ctrl, CPSW2_CONTROL);
821 __raw_writel(ETH_P_1588, &priv->regs->ts_ltype);
822}
823
Mugunthan V N3177bf62012-11-27 07:53:40 +0000824static int cpsw_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000825{
Mugunthan V N3177bf62012-11-27 07:53:40 +0000826 struct cpsw_priv *priv = netdev_priv(dev);
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000827 struct cpts *cpts = &priv->cpts;
828 struct hwtstamp_config cfg;
829
830 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
831 return -EFAULT;
832
833 /* reserved for future extensions */
834 if (cfg.flags)
835 return -EINVAL;
836
837 switch (cfg.tx_type) {
838 case HWTSTAMP_TX_OFF:
839 cpts->tx_enable = 0;
840 break;
841 case HWTSTAMP_TX_ON:
842 cpts->tx_enable = 1;
843 break;
844 default:
845 return -ERANGE;
846 }
847
848 switch (cfg.rx_filter) {
849 case HWTSTAMP_FILTER_NONE:
850 cpts->rx_enable = 0;
851 break;
852 case HWTSTAMP_FILTER_ALL:
853 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
854 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
855 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
856 return -ERANGE;
857 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
858 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
859 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
860 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
861 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
862 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
863 case HWTSTAMP_FILTER_PTP_V2_EVENT:
864 case HWTSTAMP_FILTER_PTP_V2_SYNC:
865 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
866 cpts->rx_enable = 1;
867 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
868 break;
869 default:
870 return -ERANGE;
871 }
872
873 switch (priv->version) {
874 case CPSW_VERSION_1:
875 cpsw_hwtstamp_v1(priv);
876 break;
877 case CPSW_VERSION_2:
878 cpsw_hwtstamp_v2(priv);
879 break;
880 default:
881 return -ENOTSUPP;
882 }
883
884 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
885}
886
887#endif /*CONFIG_TI_CPTS*/
888
889static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
890{
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000891 if (!netif_running(dev))
892 return -EINVAL;
893
894#ifdef CONFIG_TI_CPTS
895 if (cmd == SIOCSHWTSTAMP)
Mugunthan V N3177bf62012-11-27 07:53:40 +0000896 return cpsw_hwtstamp_ioctl(dev, req);
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000897#endif
898 return -ENOTSUPP;
899}
900
Mugunthan V Ndf828592012-03-18 20:17:54 +0000901static void cpsw_ndo_tx_timeout(struct net_device *ndev)
902{
903 struct cpsw_priv *priv = netdev_priv(ndev);
904
905 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
906 priv->stats.tx_errors++;
907 cpsw_intr_disable(priv);
908 cpdma_ctlr_int_ctrl(priv->dma, false);
909 cpdma_chan_stop(priv->txch);
910 cpdma_chan_start(priv->txch);
911 cpdma_ctlr_int_ctrl(priv->dma, true);
912 cpsw_intr_enable(priv);
913 cpdma_ctlr_eoi(priv->dma);
914}
915
916static struct net_device_stats *cpsw_ndo_get_stats(struct net_device *ndev)
917{
918 struct cpsw_priv *priv = netdev_priv(ndev);
919 return &priv->stats;
920}
921
922#ifdef CONFIG_NET_POLL_CONTROLLER
923static void cpsw_ndo_poll_controller(struct net_device *ndev)
924{
925 struct cpsw_priv *priv = netdev_priv(ndev);
926
927 cpsw_intr_disable(priv);
928 cpdma_ctlr_int_ctrl(priv->dma, false);
929 cpsw_interrupt(ndev->irq, priv);
930 cpdma_ctlr_int_ctrl(priv->dma, true);
931 cpsw_intr_enable(priv);
932 cpdma_ctlr_eoi(priv->dma);
933}
934#endif
935
936static const struct net_device_ops cpsw_netdev_ops = {
937 .ndo_open = cpsw_ndo_open,
938 .ndo_stop = cpsw_ndo_stop,
939 .ndo_start_xmit = cpsw_ndo_start_xmit,
940 .ndo_change_rx_flags = cpsw_ndo_change_rx_flags,
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000941 .ndo_do_ioctl = cpsw_ndo_ioctl,
Mugunthan V Ndf828592012-03-18 20:17:54 +0000942 .ndo_validate_addr = eth_validate_addr,
David S. Miller5c473ed2012-03-20 00:33:59 -0400943 .ndo_change_mtu = eth_change_mtu,
Mugunthan V Ndf828592012-03-18 20:17:54 +0000944 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
945 .ndo_get_stats = cpsw_ndo_get_stats,
Mugunthan V N5c50a852012-10-29 08:45:11 +0000946 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
Mugunthan V Ndf828592012-03-18 20:17:54 +0000947#ifdef CONFIG_NET_POLL_CONTROLLER
948 .ndo_poll_controller = cpsw_ndo_poll_controller,
949#endif
950};
951
952static void cpsw_get_drvinfo(struct net_device *ndev,
953 struct ethtool_drvinfo *info)
954{
955 struct cpsw_priv *priv = netdev_priv(ndev);
Jiri Pirko7826d432013-01-06 00:44:26 +0000956
957 strlcpy(info->driver, "TI CPSW Driver v1.0", sizeof(info->driver));
958 strlcpy(info->version, "1.0", sizeof(info->version));
959 strlcpy(info->bus_info, priv->pdev->name, sizeof(info->bus_info));
Mugunthan V Ndf828592012-03-18 20:17:54 +0000960}
961
962static u32 cpsw_get_msglevel(struct net_device *ndev)
963{
964 struct cpsw_priv *priv = netdev_priv(ndev);
965 return priv->msg_enable;
966}
967
968static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
969{
970 struct cpsw_priv *priv = netdev_priv(ndev);
971 priv->msg_enable = value;
972}
973
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000974static int cpsw_get_ts_info(struct net_device *ndev,
975 struct ethtool_ts_info *info)
976{
977#ifdef CONFIG_TI_CPTS
978 struct cpsw_priv *priv = netdev_priv(ndev);
979
980 info->so_timestamping =
981 SOF_TIMESTAMPING_TX_HARDWARE |
982 SOF_TIMESTAMPING_TX_SOFTWARE |
983 SOF_TIMESTAMPING_RX_HARDWARE |
984 SOF_TIMESTAMPING_RX_SOFTWARE |
985 SOF_TIMESTAMPING_SOFTWARE |
986 SOF_TIMESTAMPING_RAW_HARDWARE;
987 info->phc_index = priv->cpts.phc_index;
988 info->tx_types =
989 (1 << HWTSTAMP_TX_OFF) |
990 (1 << HWTSTAMP_TX_ON);
991 info->rx_filters =
992 (1 << HWTSTAMP_FILTER_NONE) |
993 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
994#else
995 info->so_timestamping =
996 SOF_TIMESTAMPING_TX_SOFTWARE |
997 SOF_TIMESTAMPING_RX_SOFTWARE |
998 SOF_TIMESTAMPING_SOFTWARE;
999 info->phc_index = -1;
1000 info->tx_types = 0;
1001 info->rx_filters = 0;
1002#endif
1003 return 0;
1004}
1005
Mugunthan V Ndf828592012-03-18 20:17:54 +00001006static const struct ethtool_ops cpsw_ethtool_ops = {
1007 .get_drvinfo = cpsw_get_drvinfo,
1008 .get_msglevel = cpsw_get_msglevel,
1009 .set_msglevel = cpsw_set_msglevel,
1010 .get_link = ethtool_op_get_link,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001011 .get_ts_info = cpsw_get_ts_info,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001012};
1013
Richard Cochran549985e2012-11-14 09:07:56 +00001014static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv,
1015 u32 slave_reg_ofs, u32 sliver_reg_ofs)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001016{
1017 void __iomem *regs = priv->regs;
1018 int slave_num = slave->slave_num;
1019 struct cpsw_slave_data *data = priv->data.slave_data + slave_num;
1020
1021 slave->data = data;
Richard Cochran549985e2012-11-14 09:07:56 +00001022 slave->regs = regs + slave_reg_ofs;
1023 slave->sliver = regs + sliver_reg_ofs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001024}
1025
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001026static int cpsw_probe_dt(struct cpsw_platform_data *data,
1027 struct platform_device *pdev)
1028{
1029 struct device_node *node = pdev->dev.of_node;
1030 struct device_node *slave_node;
1031 int i = 0, ret;
1032 u32 prop;
1033
1034 if (!node)
1035 return -EINVAL;
1036
1037 if (of_property_read_u32(node, "slaves", &prop)) {
1038 pr_err("Missing slaves property in the DT.\n");
1039 return -EINVAL;
1040 }
1041 data->slaves = prop;
1042
Richard Cochran78ca0b22012-10-29 08:45:18 +00001043 if (of_property_read_u32(node, "cpts_active_slave", &prop)) {
1044 pr_err("Missing cpts_active_slave property in the DT.\n");
1045 ret = -EINVAL;
1046 goto error_ret;
1047 }
1048 data->cpts_active_slave = prop;
1049
Richard Cochran00ab94e2012-10-29 08:45:19 +00001050 if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
1051 pr_err("Missing cpts_clock_mult property in the DT.\n");
1052 ret = -EINVAL;
1053 goto error_ret;
1054 }
1055 data->cpts_clock_mult = prop;
1056
1057 if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
1058 pr_err("Missing cpts_clock_shift property in the DT.\n");
1059 ret = -EINVAL;
1060 goto error_ret;
1061 }
1062 data->cpts_clock_shift = prop;
1063
Joe Perchesb2adaca2013-02-03 17:43:58 +00001064 data->slave_data = kcalloc(data->slaves, sizeof(struct cpsw_slave_data),
1065 GFP_KERNEL);
1066 if (!data->slave_data)
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001067 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001068
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001069 if (of_property_read_u32(node, "cpdma_channels", &prop)) {
1070 pr_err("Missing cpdma_channels property in the DT.\n");
1071 ret = -EINVAL;
1072 goto error_ret;
1073 }
1074 data->channels = prop;
1075
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001076 if (of_property_read_u32(node, "ale_entries", &prop)) {
1077 pr_err("Missing ale_entries property in the DT.\n");
1078 ret = -EINVAL;
1079 goto error_ret;
1080 }
1081 data->ale_entries = prop;
1082
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001083 if (of_property_read_u32(node, "bd_ram_size", &prop)) {
1084 pr_err("Missing bd_ram_size property in the DT.\n");
1085 ret = -EINVAL;
1086 goto error_ret;
1087 }
1088 data->bd_ram_size = prop;
1089
1090 if (of_property_read_u32(node, "rx_descs", &prop)) {
1091 pr_err("Missing rx_descs property in the DT.\n");
1092 ret = -EINVAL;
1093 goto error_ret;
1094 }
1095 data->rx_descs = prop;
1096
1097 if (of_property_read_u32(node, "mac_control", &prop)) {
1098 pr_err("Missing mac_control property in the DT.\n");
1099 ret = -EINVAL;
1100 goto error_ret;
1101 }
1102 data->mac_control = prop;
1103
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00001104 /*
1105 * Populate all the child nodes here...
1106 */
1107 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
1108 /* We do not want to force this, as in some cases may not have child */
1109 if (ret)
1110 pr_warn("Doesn't have any child node\n");
1111
Richard Cochran549985e2012-11-14 09:07:56 +00001112 for_each_node_by_name(slave_node, "slave") {
1113 struct cpsw_slave_data *slave_data = data->slave_data + i;
1114 const void *mac_addr = NULL;
1115 u32 phyid;
1116 int lenp;
1117 const __be32 *parp;
1118 struct device_node *mdio_node;
1119 struct platform_device *mdio;
1120
1121 parp = of_get_property(slave_node, "phy_id", &lenp);
1122 if ((parp == NULL) && (lenp != (sizeof(void *) * 2))) {
1123 pr_err("Missing slave[%d] phy_id property\n", i);
1124 ret = -EINVAL;
1125 goto error_ret;
1126 }
1127 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
1128 phyid = be32_to_cpup(parp+1);
1129 mdio = of_find_device_by_node(mdio_node);
1130 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
1131 PHY_ID_FMT, mdio->name, phyid);
1132
1133 mac_addr = of_get_mac_address(slave_node);
1134 if (mac_addr)
1135 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
1136
1137 i++;
1138 }
1139
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001140 return 0;
1141
1142error_ret:
1143 kfree(data->slave_data);
1144 return ret;
1145}
1146
Bill Pemberton663e12e2012-12-03 09:23:45 -05001147static int cpsw_probe(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001148{
1149 struct cpsw_platform_data *data = pdev->dev.platform_data;
1150 struct net_device *ndev;
1151 struct cpsw_priv *priv;
1152 struct cpdma_params dma_params;
1153 struct cpsw_ale_params ale_params;
Richard Cochran549985e2012-11-14 09:07:56 +00001154 void __iomem *ss_regs, *wr_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001155 struct resource *res;
Richard Cochran549985e2012-11-14 09:07:56 +00001156 u32 slave_offset, sliver_offset, slave_size;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001157 int ret = 0, i, k = 0;
1158
Mugunthan V Ndf828592012-03-18 20:17:54 +00001159 ndev = alloc_etherdev(sizeof(struct cpsw_priv));
1160 if (!ndev) {
1161 pr_err("error allocating net_device\n");
1162 return -ENOMEM;
1163 }
1164
1165 platform_set_drvdata(pdev, ndev);
1166 priv = netdev_priv(ndev);
1167 spin_lock_init(&priv->lock);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001168 priv->pdev = pdev;
1169 priv->ndev = ndev;
1170 priv->dev = &ndev->dev;
1171 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
1172 priv->rx_packet_max = max(rx_packet_max, 128);
1173
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00001174 /*
1175 * This may be required here for child devices.
1176 */
1177 pm_runtime_enable(&pdev->dev);
1178
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001179 if (cpsw_probe_dt(&priv->data, pdev)) {
1180 pr_err("cpsw: platform data missing\n");
1181 ret = -ENODEV;
1182 goto clean_ndev_ret;
1183 }
1184 data = &priv->data;
1185
Mugunthan V Ndf828592012-03-18 20:17:54 +00001186 if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
1187 memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
1188 pr_info("Detected MACID = %pM", priv->mac_addr);
1189 } else {
Joe Perches7efd26d2012-07-12 19:33:06 +00001190 eth_random_addr(priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001191 pr_info("Random MACID = %pM", priv->mac_addr);
1192 }
1193
1194 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
1195
1196 priv->slaves = kzalloc(sizeof(struct cpsw_slave) * data->slaves,
1197 GFP_KERNEL);
1198 if (!priv->slaves) {
1199 ret = -EBUSY;
1200 goto clean_ndev_ret;
1201 }
1202 for (i = 0; i < data->slaves; i++)
1203 priv->slaves[i].slave_num = i;
1204
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001205 priv->clk = clk_get(&pdev->dev, "fck");
Mugunthan V Ndf828592012-03-18 20:17:54 +00001206 if (IS_ERR(priv->clk)) {
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001207 dev_err(&pdev->dev, "fck is not found\n");
1208 ret = -ENODEV;
1209 goto clean_slave_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001210 }
1211
1212 priv->cpsw_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1213 if (!priv->cpsw_res) {
1214 dev_err(priv->dev, "error getting i/o resource\n");
1215 ret = -ENOENT;
1216 goto clean_clk_ret;
1217 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001218 if (!request_mem_region(priv->cpsw_res->start,
1219 resource_size(priv->cpsw_res), ndev->name)) {
1220 dev_err(priv->dev, "failed request i/o region\n");
1221 ret = -ENXIO;
1222 goto clean_clk_ret;
1223 }
Richard Cochran549985e2012-11-14 09:07:56 +00001224 ss_regs = ioremap(priv->cpsw_res->start, resource_size(priv->cpsw_res));
1225 if (!ss_regs) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00001226 dev_err(priv->dev, "unable to map i/o region\n");
1227 goto clean_cpsw_iores_ret;
1228 }
Richard Cochran549985e2012-11-14 09:07:56 +00001229 priv->regs = ss_regs;
1230 priv->version = __raw_readl(&priv->regs->id_ver);
1231 priv->host_port = HOST_PORT_NUM;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001232
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001233 priv->cpsw_wr_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1234 if (!priv->cpsw_wr_res) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00001235 dev_err(priv->dev, "error getting i/o resource\n");
1236 ret = -ENOENT;
Richard Cochran5250c962012-11-02 22:25:30 +00001237 goto clean_iomap_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001238 }
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001239 if (!request_mem_region(priv->cpsw_wr_res->start,
1240 resource_size(priv->cpsw_wr_res), ndev->name)) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00001241 dev_err(priv->dev, "failed request i/o region\n");
1242 ret = -ENXIO;
Richard Cochran5250c962012-11-02 22:25:30 +00001243 goto clean_iomap_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001244 }
Richard Cochran549985e2012-11-14 09:07:56 +00001245 wr_regs = ioremap(priv->cpsw_wr_res->start,
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001246 resource_size(priv->cpsw_wr_res));
Richard Cochran549985e2012-11-14 09:07:56 +00001247 if (!wr_regs) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00001248 dev_err(priv->dev, "unable to map i/o region\n");
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001249 goto clean_cpsw_wr_iores_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001250 }
Richard Cochran549985e2012-11-14 09:07:56 +00001251 priv->wr_regs = wr_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001252
1253 memset(&dma_params, 0, sizeof(dma_params));
Richard Cochran549985e2012-11-14 09:07:56 +00001254 memset(&ale_params, 0, sizeof(ale_params));
1255
1256 switch (priv->version) {
1257 case CPSW_VERSION_1:
1258 priv->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
1259 priv->cpts.reg = ss_regs + CPSW1_CPTS_OFFSET;
1260 dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
1261 dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
1262 ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
1263 slave_offset = CPSW1_SLAVE_OFFSET;
1264 slave_size = CPSW1_SLAVE_SIZE;
1265 sliver_offset = CPSW1_SLIVER_OFFSET;
1266 dma_params.desc_mem_phys = 0;
1267 break;
1268 case CPSW_VERSION_2:
1269 priv->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
1270 priv->cpts.reg = ss_regs + CPSW2_CPTS_OFFSET;
1271 dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
1272 dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
1273 ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
1274 slave_offset = CPSW2_SLAVE_OFFSET;
1275 slave_size = CPSW2_SLAVE_SIZE;
1276 sliver_offset = CPSW2_SLIVER_OFFSET;
1277 dma_params.desc_mem_phys =
1278 (u32 __force) priv->cpsw_res->start + CPSW2_BD_OFFSET;
1279 break;
1280 default:
1281 dev_err(priv->dev, "unknown version 0x%08x\n", priv->version);
1282 ret = -ENODEV;
1283 goto clean_cpsw_wr_iores_ret;
1284 }
1285 for (i = 0; i < priv->data.slaves; i++) {
1286 struct cpsw_slave *slave = &priv->slaves[i];
1287 cpsw_slave_init(slave, priv, slave_offset, sliver_offset);
1288 slave_offset += slave_size;
1289 sliver_offset += SLIVER_SIZE;
1290 }
1291
Mugunthan V Ndf828592012-03-18 20:17:54 +00001292 dma_params.dev = &pdev->dev;
Richard Cochran549985e2012-11-14 09:07:56 +00001293 dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
1294 dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
1295 dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
1296 dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
1297 dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001298
1299 dma_params.num_chan = data->channels;
1300 dma_params.has_soft_reset = true;
1301 dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
1302 dma_params.desc_mem_size = data->bd_ram_size;
1303 dma_params.desc_align = 16;
1304 dma_params.has_ext_regs = true;
Richard Cochran549985e2012-11-14 09:07:56 +00001305 dma_params.desc_hw_addr = dma_params.desc_mem_phys;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001306
1307 priv->dma = cpdma_ctlr_create(&dma_params);
1308 if (!priv->dma) {
1309 dev_err(priv->dev, "error initializing dma\n");
1310 ret = -ENOMEM;
Richard Cochran5250c962012-11-02 22:25:30 +00001311 goto clean_wr_iomap_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001312 }
1313
1314 priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
1315 cpsw_tx_handler);
1316 priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
1317 cpsw_rx_handler);
1318
1319 if (WARN_ON(!priv->txch || !priv->rxch)) {
1320 dev_err(priv->dev, "error initializing dma channels\n");
1321 ret = -ENOMEM;
1322 goto clean_dma_ret;
1323 }
1324
Mugunthan V Ndf828592012-03-18 20:17:54 +00001325 ale_params.dev = &ndev->dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001326 ale_params.ale_ageout = ale_ageout;
1327 ale_params.ale_entries = data->ale_entries;
1328 ale_params.ale_ports = data->slaves;
1329
1330 priv->ale = cpsw_ale_create(&ale_params);
1331 if (!priv->ale) {
1332 dev_err(priv->dev, "error initializing ale engine\n");
1333 ret = -ENODEV;
1334 goto clean_dma_ret;
1335 }
1336
1337 ndev->irq = platform_get_irq(pdev, 0);
1338 if (ndev->irq < 0) {
1339 dev_err(priv->dev, "error getting irq resource\n");
1340 ret = -ENOENT;
1341 goto clean_ale_ret;
1342 }
1343
1344 while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
1345 for (i = res->start; i <= res->end; i++) {
1346 if (request_irq(i, cpsw_interrupt, IRQF_DISABLED,
1347 dev_name(&pdev->dev), priv)) {
1348 dev_err(priv->dev, "error attaching irq\n");
1349 goto clean_ale_ret;
1350 }
1351 priv->irqs_table[k] = i;
1352 priv->num_irqs = k;
1353 }
1354 k++;
1355 }
1356
1357 ndev->flags |= IFF_ALLMULTI; /* see cpsw_ndo_change_rx_flags() */
1358
1359 ndev->netdev_ops = &cpsw_netdev_ops;
1360 SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
1361 netif_napi_add(ndev, &priv->napi, cpsw_poll, CPSW_POLL_WEIGHT);
1362
1363 /* register the network device */
1364 SET_NETDEV_DEV(ndev, &pdev->dev);
1365 ret = register_netdev(ndev);
1366 if (ret) {
1367 dev_err(priv->dev, "error registering net device\n");
1368 ret = -ENODEV;
1369 goto clean_irq_ret;
1370 }
1371
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001372 if (cpts_register(&pdev->dev, &priv->cpts,
1373 data->cpts_clock_mult, data->cpts_clock_shift))
1374 dev_err(priv->dev, "error registering cpts device\n");
1375
Mugunthan V Ndf828592012-03-18 20:17:54 +00001376 cpsw_notice(priv, probe, "initialized device (regs %x, irq %d)\n",
1377 priv->cpsw_res->start, ndev->irq);
1378
1379 return 0;
1380
1381clean_irq_ret:
1382 free_irq(ndev->irq, priv);
1383clean_ale_ret:
1384 cpsw_ale_destroy(priv->ale);
1385clean_dma_ret:
1386 cpdma_chan_destroy(priv->txch);
1387 cpdma_chan_destroy(priv->rxch);
1388 cpdma_ctlr_destroy(priv->dma);
Richard Cochran5250c962012-11-02 22:25:30 +00001389clean_wr_iomap_ret:
1390 iounmap(priv->wr_regs);
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001391clean_cpsw_wr_iores_ret:
1392 release_mem_region(priv->cpsw_wr_res->start,
1393 resource_size(priv->cpsw_wr_res));
Richard Cochran5250c962012-11-02 22:25:30 +00001394clean_iomap_ret:
1395 iounmap(priv->regs);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001396clean_cpsw_iores_ret:
1397 release_mem_region(priv->cpsw_res->start,
1398 resource_size(priv->cpsw_res));
1399clean_clk_ret:
1400 clk_put(priv->clk);
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001401clean_slave_ret:
1402 pm_runtime_disable(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001403 kfree(priv->slaves);
1404clean_ndev_ret:
1405 free_netdev(ndev);
1406 return ret;
1407}
1408
Bill Pemberton663e12e2012-12-03 09:23:45 -05001409static int cpsw_remove(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001410{
1411 struct net_device *ndev = platform_get_drvdata(pdev);
1412 struct cpsw_priv *priv = netdev_priv(ndev);
1413
1414 pr_info("removing device");
1415 platform_set_drvdata(pdev, NULL);
1416
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001417 cpts_unregister(&priv->cpts);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001418 free_irq(ndev->irq, priv);
1419 cpsw_ale_destroy(priv->ale);
1420 cpdma_chan_destroy(priv->txch);
1421 cpdma_chan_destroy(priv->rxch);
1422 cpdma_ctlr_destroy(priv->dma);
1423 iounmap(priv->regs);
1424 release_mem_region(priv->cpsw_res->start,
1425 resource_size(priv->cpsw_res));
Richard Cochran5250c962012-11-02 22:25:30 +00001426 iounmap(priv->wr_regs);
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001427 release_mem_region(priv->cpsw_wr_res->start,
1428 resource_size(priv->cpsw_wr_res));
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001429 pm_runtime_disable(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001430 clk_put(priv->clk);
1431 kfree(priv->slaves);
1432 free_netdev(ndev);
1433
1434 return 0;
1435}
1436
1437static int cpsw_suspend(struct device *dev)
1438{
1439 struct platform_device *pdev = to_platform_device(dev);
1440 struct net_device *ndev = platform_get_drvdata(pdev);
1441
1442 if (netif_running(ndev))
1443 cpsw_ndo_stop(ndev);
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001444 pm_runtime_put_sync(&pdev->dev);
1445
Mugunthan V Ndf828592012-03-18 20:17:54 +00001446 return 0;
1447}
1448
1449static int cpsw_resume(struct device *dev)
1450{
1451 struct platform_device *pdev = to_platform_device(dev);
1452 struct net_device *ndev = platform_get_drvdata(pdev);
1453
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001454 pm_runtime_get_sync(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001455 if (netif_running(ndev))
1456 cpsw_ndo_open(ndev);
1457 return 0;
1458}
1459
1460static const struct dev_pm_ops cpsw_pm_ops = {
1461 .suspend = cpsw_suspend,
1462 .resume = cpsw_resume,
1463};
1464
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001465static const struct of_device_id cpsw_of_mtable[] = {
1466 { .compatible = "ti,cpsw", },
1467 { /* sentinel */ },
1468};
1469
Mugunthan V Ndf828592012-03-18 20:17:54 +00001470static struct platform_driver cpsw_driver = {
1471 .driver = {
1472 .name = "cpsw",
1473 .owner = THIS_MODULE,
1474 .pm = &cpsw_pm_ops,
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001475 .of_match_table = of_match_ptr(cpsw_of_mtable),
Mugunthan V Ndf828592012-03-18 20:17:54 +00001476 },
1477 .probe = cpsw_probe,
Bill Pemberton663e12e2012-12-03 09:23:45 -05001478 .remove = cpsw_remove,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001479};
1480
1481static int __init cpsw_init(void)
1482{
1483 return platform_driver_register(&cpsw_driver);
1484}
1485late_initcall(cpsw_init);
1486
1487static void __exit cpsw_exit(void)
1488{
1489 platform_driver_unregister(&cpsw_driver);
1490}
1491module_exit(cpsw_exit);
1492
1493MODULE_LICENSE("GPL");
1494MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
1495MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
1496MODULE_DESCRIPTION("TI CPSW Ethernet driver");