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