]>
Commit | Line | Data |
---|---|---|
e190d6b1 | 1 | /* |
2fb9d6f5 | 2 | * Blackfin On-Chip MAC Driver |
e190d6b1 | 3 | * |
2fb9d6f5 | 4 | * Copyright 2004-2007 Analog Devices Inc. |
e190d6b1 | 5 | * |
2fb9d6f5 | 6 | * Enter bugs at http://blackfin.uclinux.org/ |
e190d6b1 | 7 | * |
2fb9d6f5 | 8 | * Licensed under the GPL-2 or later. |
e190d6b1 BW |
9 | */ |
10 | ||
11 | #include <linux/init.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/kernel.h> | |
14 | #include <linux/sched.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/delay.h> | |
17 | #include <linux/timer.h> | |
18 | #include <linux/errno.h> | |
19 | #include <linux/irq.h> | |
20 | #include <linux/io.h> | |
21 | #include <linux/ioport.h> | |
22 | #include <linux/crc32.h> | |
23 | #include <linux/device.h> | |
24 | #include <linux/spinlock.h> | |
e190d6b1 | 25 | #include <linux/mii.h> |
4ae5a3ad | 26 | #include <linux/phy.h> |
e190d6b1 BW |
27 | #include <linux/netdevice.h> |
28 | #include <linux/etherdevice.h> | |
679dce39 | 29 | #include <linux/ethtool.h> |
e190d6b1 | 30 | #include <linux/skbuff.h> |
e190d6b1 | 31 | #include <linux/platform_device.h> |
e190d6b1 BW |
32 | |
33 | #include <asm/dma.h> | |
34 | #include <linux/dma-mapping.h> | |
35 | ||
fe92afed | 36 | #include <asm/div64.h> |
98f672ca | 37 | #include <asm/dpmc.h> |
e190d6b1 BW |
38 | #include <asm/blackfin.h> |
39 | #include <asm/cacheflush.h> | |
40 | #include <asm/portmux.h> | |
41 | ||
42 | #include "bfin_mac.h" | |
43 | ||
44 | #define DRV_NAME "bfin_mac" | |
45 | #define DRV_VERSION "1.1" | |
46 | #define DRV_AUTHOR "Bryan Wu, Luke Yang" | |
7ef0a7ee | 47 | #define DRV_DESC "Blackfin on-chip Ethernet MAC driver" |
e190d6b1 BW |
48 | |
49 | MODULE_AUTHOR(DRV_AUTHOR); | |
50 | MODULE_LICENSE("GPL"); | |
51 | MODULE_DESCRIPTION(DRV_DESC); | |
72abb461 | 52 | MODULE_ALIAS("platform:bfin_mac"); |
e190d6b1 BW |
53 | |
54 | #if defined(CONFIG_BFIN_MAC_USE_L1) | |
55 | # define bfin_mac_alloc(dma_handle, size) l1_data_sram_zalloc(size) | |
56 | # define bfin_mac_free(dma_handle, ptr) l1_data_sram_free(ptr) | |
57 | #else | |
58 | # define bfin_mac_alloc(dma_handle, size) \ | |
59 | dma_alloc_coherent(NULL, size, dma_handle, GFP_KERNEL) | |
60 | # define bfin_mac_free(dma_handle, ptr) \ | |
61 | dma_free_coherent(NULL, sizeof(*ptr), ptr, dma_handle) | |
62 | #endif | |
63 | ||
64 | #define PKT_BUF_SZ 1580 | |
65 | ||
66 | #define MAX_TIMEOUT_CNT 500 | |
67 | ||
68 | /* pointers to maintain transmit list */ | |
69 | static struct net_dma_desc_tx *tx_list_head; | |
70 | static struct net_dma_desc_tx *tx_list_tail; | |
71 | static struct net_dma_desc_rx *rx_list_head; | |
72 | static struct net_dma_desc_rx *rx_list_tail; | |
73 | static struct net_dma_desc_rx *current_rx_ptr; | |
74 | static struct net_dma_desc_tx *current_tx_ptr; | |
75 | static struct net_dma_desc_tx *tx_desc; | |
76 | static struct net_dma_desc_rx *rx_desc; | |
77 | ||
7ef0a7ee BW |
78 | #if defined(CONFIG_BFIN_MAC_RMII) |
79 | static u16 pin_req[] = P_RMII0; | |
80 | #else | |
81 | static u16 pin_req[] = P_MII0; | |
82 | #endif | |
83 | ||
84 | static void bfin_mac_disable(void); | |
85 | static void bfin_mac_enable(void); | |
4ae5a3ad | 86 | |
e190d6b1 BW |
87 | static void desc_list_free(void) |
88 | { | |
89 | struct net_dma_desc_rx *r; | |
90 | struct net_dma_desc_tx *t; | |
91 | int i; | |
92 | #if !defined(CONFIG_BFIN_MAC_USE_L1) | |
93 | dma_addr_t dma_handle = 0; | |
94 | #endif | |
95 | ||
96 | if (tx_desc) { | |
97 | t = tx_list_head; | |
98 | for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) { | |
99 | if (t) { | |
100 | if (t->skb) { | |
101 | dev_kfree_skb(t->skb); | |
102 | t->skb = NULL; | |
103 | } | |
104 | t = t->next; | |
105 | } | |
106 | } | |
107 | bfin_mac_free(dma_handle, tx_desc); | |
108 | } | |
109 | ||
110 | if (rx_desc) { | |
111 | r = rx_list_head; | |
112 | for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) { | |
113 | if (r) { | |
114 | if (r->skb) { | |
115 | dev_kfree_skb(r->skb); | |
116 | r->skb = NULL; | |
117 | } | |
118 | r = r->next; | |
119 | } | |
120 | } | |
121 | bfin_mac_free(dma_handle, rx_desc); | |
122 | } | |
123 | } | |
124 | ||
125 | static int desc_list_init(void) | |
126 | { | |
127 | int i; | |
128 | struct sk_buff *new_skb; | |
129 | #if !defined(CONFIG_BFIN_MAC_USE_L1) | |
130 | /* | |
131 | * This dma_handle is useless in Blackfin dma_alloc_coherent(). | |
132 | * The real dma handler is the return value of dma_alloc_coherent(). | |
133 | */ | |
134 | dma_addr_t dma_handle; | |
135 | #endif | |
136 | ||
137 | tx_desc = bfin_mac_alloc(&dma_handle, | |
138 | sizeof(struct net_dma_desc_tx) * | |
139 | CONFIG_BFIN_TX_DESC_NUM); | |
140 | if (tx_desc == NULL) | |
141 | goto init_error; | |
142 | ||
143 | rx_desc = bfin_mac_alloc(&dma_handle, | |
144 | sizeof(struct net_dma_desc_rx) * | |
145 | CONFIG_BFIN_RX_DESC_NUM); | |
146 | if (rx_desc == NULL) | |
147 | goto init_error; | |
148 | ||
149 | /* init tx_list */ | |
150 | tx_list_head = tx_list_tail = tx_desc; | |
151 | ||
152 | for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) { | |
153 | struct net_dma_desc_tx *t = tx_desc + i; | |
154 | struct dma_descriptor *a = &(t->desc_a); | |
155 | struct dma_descriptor *b = &(t->desc_b); | |
156 | ||
157 | /* | |
158 | * disable DMA | |
159 | * read from memory WNR = 0 | |
160 | * wordsize is 32 bits | |
161 | * 6 half words is desc size | |
162 | * large desc flow | |
163 | */ | |
164 | a->config = WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE; | |
165 | a->start_addr = (unsigned long)t->packet; | |
166 | a->x_count = 0; | |
167 | a->next_dma_desc = b; | |
168 | ||
169 | /* | |
170 | * enabled DMA | |
171 | * write to memory WNR = 1 | |
172 | * wordsize is 32 bits | |
173 | * disable interrupt | |
174 | * 6 half words is desc size | |
175 | * large desc flow | |
176 | */ | |
177 | b->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE; | |
178 | b->start_addr = (unsigned long)(&(t->status)); | |
179 | b->x_count = 0; | |
180 | ||
181 | t->skb = NULL; | |
182 | tx_list_tail->desc_b.next_dma_desc = a; | |
183 | tx_list_tail->next = t; | |
184 | tx_list_tail = t; | |
185 | } | |
186 | tx_list_tail->next = tx_list_head; /* tx_list is a circle */ | |
187 | tx_list_tail->desc_b.next_dma_desc = &(tx_list_head->desc_a); | |
188 | current_tx_ptr = tx_list_head; | |
189 | ||
190 | /* init rx_list */ | |
191 | rx_list_head = rx_list_tail = rx_desc; | |
192 | ||
193 | for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) { | |
194 | struct net_dma_desc_rx *r = rx_desc + i; | |
195 | struct dma_descriptor *a = &(r->desc_a); | |
196 | struct dma_descriptor *b = &(r->desc_b); | |
197 | ||
198 | /* allocate a new skb for next time receive */ | |
015dac88 | 199 | new_skb = dev_alloc_skb(PKT_BUF_SZ + NET_IP_ALIGN); |
e190d6b1 BW |
200 | if (!new_skb) { |
201 | printk(KERN_NOTICE DRV_NAME | |
202 | ": init: low on mem - packet dropped\n"); | |
203 | goto init_error; | |
204 | } | |
015dac88 | 205 | skb_reserve(new_skb, NET_IP_ALIGN); |
f6e1e4f3 SZ |
206 | /* Invidate the data cache of skb->data range when it is write back |
207 | * cache. It will prevent overwritting the new data from DMA | |
208 | */ | |
209 | blackfin_dcache_invalidate_range((unsigned long)new_skb->head, | |
210 | (unsigned long)new_skb->end); | |
e190d6b1 BW |
211 | r->skb = new_skb; |
212 | ||
213 | /* | |
214 | * enabled DMA | |
215 | * write to memory WNR = 1 | |
216 | * wordsize is 32 bits | |
217 | * disable interrupt | |
218 | * 6 half words is desc size | |
219 | * large desc flow | |
220 | */ | |
221 | a->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE; | |
222 | /* since RXDWA is enabled */ | |
223 | a->start_addr = (unsigned long)new_skb->data - 2; | |
224 | a->x_count = 0; | |
225 | a->next_dma_desc = b; | |
226 | ||
227 | /* | |
228 | * enabled DMA | |
229 | * write to memory WNR = 1 | |
230 | * wordsize is 32 bits | |
231 | * enable interrupt | |
232 | * 6 half words is desc size | |
233 | * large desc flow | |
234 | */ | |
235 | b->config = DMAEN | WNR | WDSIZE_32 | DI_EN | | |
236 | NDSIZE_6 | DMAFLOW_LARGE; | |
237 | b->start_addr = (unsigned long)(&(r->status)); | |
238 | b->x_count = 0; | |
239 | ||
240 | rx_list_tail->desc_b.next_dma_desc = a; | |
241 | rx_list_tail->next = r; | |
242 | rx_list_tail = r; | |
243 | } | |
244 | rx_list_tail->next = rx_list_head; /* rx_list is a circle */ | |
245 | rx_list_tail->desc_b.next_dma_desc = &(rx_list_head->desc_a); | |
246 | current_rx_ptr = rx_list_head; | |
247 | ||
248 | return 0; | |
249 | ||
250 | init_error: | |
251 | desc_list_free(); | |
252 | printk(KERN_ERR DRV_NAME ": kmalloc failed\n"); | |
253 | return -ENOMEM; | |
254 | } | |
255 | ||
256 | ||
257 | /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/ | |
258 | ||
4ae5a3ad BW |
259 | /* |
260 | * MII operations | |
261 | */ | |
e190d6b1 | 262 | /* Wait until the previous MDC/MDIO transaction has completed */ |
0ed0563e | 263 | static void bfin_mdio_poll(void) |
e190d6b1 BW |
264 | { |
265 | int timeout_cnt = MAX_TIMEOUT_CNT; | |
266 | ||
267 | /* poll the STABUSY bit */ | |
268 | while ((bfin_read_EMAC_STAADD()) & STABUSY) { | |
6db9e461 | 269 | udelay(1); |
e190d6b1 BW |
270 | if (timeout_cnt-- < 0) { |
271 | printk(KERN_ERR DRV_NAME | |
272 | ": wait MDC/MDIO transaction to complete timeout\n"); | |
273 | break; | |
274 | } | |
275 | } | |
276 | } | |
277 | ||
278 | /* Read an off-chip register in a PHY through the MDC/MDIO port */ | |
0ed0563e | 279 | static int bfin_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum) |
e190d6b1 | 280 | { |
0ed0563e | 281 | bfin_mdio_poll(); |
4ae5a3ad | 282 | |
e190d6b1 | 283 | /* read mode */ |
4ae5a3ad BW |
284 | bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) | |
285 | SET_REGAD((u16) regnum) | | |
e190d6b1 | 286 | STABUSY); |
e190d6b1 | 287 | |
0ed0563e | 288 | bfin_mdio_poll(); |
4ae5a3ad BW |
289 | |
290 | return (int) bfin_read_EMAC_STADAT(); | |
e190d6b1 BW |
291 | } |
292 | ||
293 | /* Write an off-chip register in a PHY through the MDC/MDIO port */ | |
0ed0563e AB |
294 | static int bfin_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum, |
295 | u16 value) | |
e190d6b1 | 296 | { |
0ed0563e | 297 | bfin_mdio_poll(); |
4ae5a3ad BW |
298 | |
299 | bfin_write_EMAC_STADAT((u32) value); | |
e190d6b1 BW |
300 | |
301 | /* write mode */ | |
4ae5a3ad BW |
302 | bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) | |
303 | SET_REGAD((u16) regnum) | | |
e190d6b1 BW |
304 | STAOP | |
305 | STABUSY); | |
306 | ||
0ed0563e | 307 | bfin_mdio_poll(); |
4ae5a3ad BW |
308 | |
309 | return 0; | |
e190d6b1 BW |
310 | } |
311 | ||
0ed0563e | 312 | static int bfin_mdiobus_reset(struct mii_bus *bus) |
e190d6b1 | 313 | { |
4ae5a3ad | 314 | return 0; |
e190d6b1 BW |
315 | } |
316 | ||
7ef0a7ee | 317 | static void bfin_mac_adjust_link(struct net_device *dev) |
e190d6b1 | 318 | { |
7ef0a7ee | 319 | struct bfin_mac_local *lp = netdev_priv(dev); |
4ae5a3ad BW |
320 | struct phy_device *phydev = lp->phydev; |
321 | unsigned long flags; | |
322 | int new_state = 0; | |
323 | ||
324 | spin_lock_irqsave(&lp->lock, flags); | |
325 | if (phydev->link) { | |
326 | /* Now we make sure that we can be in full duplex mode. | |
327 | * If not, we operate in half-duplex mode. */ | |
328 | if (phydev->duplex != lp->old_duplex) { | |
329 | u32 opmode = bfin_read_EMAC_OPMODE(); | |
330 | new_state = 1; | |
331 | ||
332 | if (phydev->duplex) | |
333 | opmode |= FDMODE; | |
334 | else | |
335 | opmode &= ~(FDMODE); | |
336 | ||
337 | bfin_write_EMAC_OPMODE(opmode); | |
338 | lp->old_duplex = phydev->duplex; | |
339 | } | |
e190d6b1 | 340 | |
4ae5a3ad BW |
341 | if (phydev->speed != lp->old_speed) { |
342 | #if defined(CONFIG_BFIN_MAC_RMII) | |
343 | u32 opmode = bfin_read_EMAC_OPMODE(); | |
4ae5a3ad BW |
344 | switch (phydev->speed) { |
345 | case 10: | |
346 | opmode |= RMII_10; | |
347 | break; | |
348 | case 100: | |
349 | opmode &= ~(RMII_10); | |
350 | break; | |
351 | default: | |
352 | printk(KERN_WARNING | |
353 | "%s: Ack! Speed (%d) is not 10/100!\n", | |
354 | DRV_NAME, phydev->speed); | |
355 | break; | |
356 | } | |
357 | bfin_write_EMAC_OPMODE(opmode); | |
4ae5a3ad | 358 | #endif |
e190d6b1 | 359 | |
4ae5a3ad BW |
360 | new_state = 1; |
361 | lp->old_speed = phydev->speed; | |
362 | } | |
e190d6b1 | 363 | |
4ae5a3ad BW |
364 | if (!lp->old_link) { |
365 | new_state = 1; | |
366 | lp->old_link = 1; | |
4ae5a3ad BW |
367 | } |
368 | } else if (lp->old_link) { | |
369 | new_state = 1; | |
370 | lp->old_link = 0; | |
371 | lp->old_speed = 0; | |
372 | lp->old_duplex = -1; | |
e190d6b1 BW |
373 | } |
374 | ||
4ae5a3ad BW |
375 | if (new_state) { |
376 | u32 opmode = bfin_read_EMAC_OPMODE(); | |
377 | phy_print_status(phydev); | |
378 | pr_debug("EMAC_OPMODE = 0x%08x\n", opmode); | |
e190d6b1 | 379 | } |
4ae5a3ad BW |
380 | |
381 | spin_unlock_irqrestore(&lp->lock, flags); | |
e190d6b1 BW |
382 | } |
383 | ||
7cc8f381 BW |
384 | /* MDC = 2.5 MHz */ |
385 | #define MDC_CLK 2500000 | |
386 | ||
4ae5a3ad | 387 | static int mii_probe(struct net_device *dev) |
e190d6b1 | 388 | { |
7ef0a7ee | 389 | struct bfin_mac_local *lp = netdev_priv(dev); |
4ae5a3ad BW |
390 | struct phy_device *phydev = NULL; |
391 | unsigned short sysctl; | |
392 | int i; | |
7cc8f381 | 393 | u32 sclk, mdc_div; |
e190d6b1 | 394 | |
4ae5a3ad | 395 | /* Enable PHY output early */ |
98f672ca MF |
396 | if (!(bfin_read_VR_CTL() & CLKBUFOE)) |
397 | bfin_write_VR_CTL(bfin_read_VR_CTL() | CLKBUFOE); | |
e190d6b1 | 398 | |
7cc8f381 BW |
399 | sclk = get_sclk(); |
400 | mdc_div = ((sclk / MDC_CLK) / 2) - 1; | |
401 | ||
4ae5a3ad | 402 | sysctl = bfin_read_EMAC_SYSCTL(); |
9dc7f30e | 403 | sysctl = (sysctl & ~MDCDIV) | SET_MDCDIV(mdc_div); |
e190d6b1 | 404 | bfin_write_EMAC_SYSCTL(sysctl); |
e190d6b1 | 405 | |
4ae5a3ad BW |
406 | /* search for connect PHY device */ |
407 | for (i = 0; i < PHY_MAX_ADDR; i++) { | |
298cf9be | 408 | struct phy_device *const tmp_phydev = lp->mii_bus->phy_map[i]; |
e190d6b1 | 409 | |
4ae5a3ad BW |
410 | if (!tmp_phydev) |
411 | continue; /* no PHY here... */ | |
e190d6b1 | 412 | |
4ae5a3ad BW |
413 | phydev = tmp_phydev; |
414 | break; /* found it */ | |
415 | } | |
416 | ||
417 | /* now we are supposed to have a proper phydev, to attach to... */ | |
418 | if (!phydev) { | |
419 | printk(KERN_INFO "%s: Don't found any phy device at all\n", | |
420 | dev->name); | |
421 | return -ENODEV; | |
e190d6b1 BW |
422 | } |
423 | ||
424 | #if defined(CONFIG_BFIN_MAC_RMII) | |
c2313557 KS |
425 | phydev = phy_connect(dev, dev_name(&phydev->dev), &bfin_mac_adjust_link, |
426 | 0, PHY_INTERFACE_MODE_RMII); | |
4ae5a3ad | 427 | #else |
c2313557 KS |
428 | phydev = phy_connect(dev, dev_name(&phydev->dev), &bfin_mac_adjust_link, |
429 | 0, PHY_INTERFACE_MODE_MII); | |
e190d6b1 BW |
430 | #endif |
431 | ||
4ae5a3ad BW |
432 | if (IS_ERR(phydev)) { |
433 | printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name); | |
434 | return PTR_ERR(phydev); | |
435 | } | |
436 | ||
437 | /* mask with MAC supported features */ | |
438 | phydev->supported &= (SUPPORTED_10baseT_Half | |
439 | | SUPPORTED_10baseT_Full | |
440 | | SUPPORTED_100baseT_Half | |
441 | | SUPPORTED_100baseT_Full | |
442 | | SUPPORTED_Autoneg | |
443 | | SUPPORTED_Pause | SUPPORTED_Asym_Pause | |
444 | | SUPPORTED_MII | |
445 | | SUPPORTED_TP); | |
446 | ||
447 | phydev->advertising = phydev->supported; | |
448 | ||
449 | lp->old_link = 0; | |
450 | lp->old_speed = 0; | |
451 | lp->old_duplex = -1; | |
452 | lp->phydev = phydev; | |
453 | ||
454 | printk(KERN_INFO "%s: attached PHY driver [%s] " | |
7cc8f381 BW |
455 | "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)" |
456 | "@sclk=%dMHz)\n", | |
c2313557 | 457 | DRV_NAME, phydev->drv->name, dev_name(&phydev->dev), phydev->irq, |
7cc8f381 | 458 | MDC_CLK, mdc_div, sclk/1000000); |
4ae5a3ad BW |
459 | |
460 | return 0; | |
461 | } | |
462 | ||
679dce39 BW |
463 | /* |
464 | * Ethtool support | |
465 | */ | |
466 | ||
467 | static int | |
468 | bfin_mac_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd) | |
469 | { | |
470 | struct bfin_mac_local *lp = netdev_priv(dev); | |
471 | ||
472 | if (lp->phydev) | |
473 | return phy_ethtool_gset(lp->phydev, cmd); | |
474 | ||
475 | return -EINVAL; | |
476 | } | |
477 | ||
478 | static int | |
479 | bfin_mac_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd) | |
480 | { | |
481 | struct bfin_mac_local *lp = netdev_priv(dev); | |
482 | ||
483 | if (!capable(CAP_NET_ADMIN)) | |
484 | return -EPERM; | |
485 | ||
486 | if (lp->phydev) | |
487 | return phy_ethtool_sset(lp->phydev, cmd); | |
488 | ||
489 | return -EINVAL; | |
490 | } | |
491 | ||
492 | static void bfin_mac_ethtool_getdrvinfo(struct net_device *dev, | |
493 | struct ethtool_drvinfo *info) | |
494 | { | |
495 | strcpy(info->driver, DRV_NAME); | |
496 | strcpy(info->version, DRV_VERSION); | |
497 | strcpy(info->fw_version, "N/A"); | |
c2313557 | 498 | strcpy(info->bus_info, dev_name(&dev->dev)); |
679dce39 BW |
499 | } |
500 | ||
0fc0b732 | 501 | static const struct ethtool_ops bfin_mac_ethtool_ops = { |
679dce39 BW |
502 | .get_settings = bfin_mac_ethtool_getsettings, |
503 | .set_settings = bfin_mac_ethtool_setsettings, | |
504 | .get_link = ethtool_op_get_link, | |
505 | .get_drvinfo = bfin_mac_ethtool_getdrvinfo, | |
506 | }; | |
507 | ||
4ae5a3ad BW |
508 | /**************************************************************************/ |
509 | void setup_system_regs(struct net_device *dev) | |
510 | { | |
511 | unsigned short sysctl; | |
512 | ||
513 | /* | |
514 | * Odd word alignment for Receive Frame DMA word | |
515 | * Configure checksum support and rcve frame word alignment | |
516 | */ | |
517 | sysctl = bfin_read_EMAC_SYSCTL(); | |
518 | #if defined(BFIN_MAC_CSUM_OFFLOAD) | |
519 | sysctl |= RXDWA | RXCKS; | |
520 | #else | |
521 | sysctl |= RXDWA; | |
522 | #endif | |
523 | bfin_write_EMAC_SYSCTL(sysctl); | |
e190d6b1 BW |
524 | |
525 | bfin_write_EMAC_MMC_CTL(RSTC | CROLL); | |
526 | ||
527 | /* Initialize the TX DMA channel registers */ | |
528 | bfin_write_DMA2_X_COUNT(0); | |
529 | bfin_write_DMA2_X_MODIFY(4); | |
530 | bfin_write_DMA2_Y_COUNT(0); | |
531 | bfin_write_DMA2_Y_MODIFY(0); | |
532 | ||
533 | /* Initialize the RX DMA channel registers */ | |
534 | bfin_write_DMA1_X_COUNT(0); | |
535 | bfin_write_DMA1_X_MODIFY(4); | |
536 | bfin_write_DMA1_Y_COUNT(0); | |
537 | bfin_write_DMA1_Y_MODIFY(0); | |
538 | } | |
539 | ||
73f83182 | 540 | static void setup_mac_addr(u8 *mac_addr) |
e190d6b1 BW |
541 | { |
542 | u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]); | |
543 | u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]); | |
544 | ||
545 | /* this depends on a little-endian machine */ | |
546 | bfin_write_EMAC_ADDRLO(addr_low); | |
547 | bfin_write_EMAC_ADDRHI(addr_hi); | |
548 | } | |
549 | ||
7ef0a7ee | 550 | static int bfin_mac_set_mac_address(struct net_device *dev, void *p) |
73f83182 AL |
551 | { |
552 | struct sockaddr *addr = p; | |
553 | if (netif_running(dev)) | |
554 | return -EBUSY; | |
555 | memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); | |
556 | setup_mac_addr(dev->dev_addr); | |
557 | return 0; | |
558 | } | |
559 | ||
fe92afed BS |
560 | #ifdef CONFIG_BFIN_MAC_USE_HWSTAMP |
561 | #define bfin_mac_hwtstamp_is_none(cfg) ((cfg) == HWTSTAMP_FILTER_NONE) | |
562 | ||
563 | static int bfin_mac_hwtstamp_ioctl(struct net_device *netdev, | |
564 | struct ifreq *ifr, int cmd) | |
565 | { | |
566 | struct hwtstamp_config config; | |
567 | struct bfin_mac_local *lp = netdev_priv(netdev); | |
568 | u16 ptpctl; | |
569 | u32 ptpfv1, ptpfv2, ptpfv3, ptpfoff; | |
570 | ||
571 | if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) | |
572 | return -EFAULT; | |
573 | ||
574 | pr_debug("%s config flag:0x%x, tx_type:0x%x, rx_filter:0x%x\n", | |
575 | __func__, config.flags, config.tx_type, config.rx_filter); | |
576 | ||
577 | /* reserved for future extensions */ | |
578 | if (config.flags) | |
579 | return -EINVAL; | |
580 | ||
581 | if ((config.tx_type != HWTSTAMP_TX_OFF) && | |
582 | (config.tx_type != HWTSTAMP_TX_ON)) | |
583 | return -ERANGE; | |
584 | ||
585 | ptpctl = bfin_read_EMAC_PTP_CTL(); | |
586 | ||
587 | switch (config.rx_filter) { | |
588 | case HWTSTAMP_FILTER_NONE: | |
589 | /* | |
590 | * Dont allow any timestamping | |
591 | */ | |
592 | ptpfv3 = 0xFFFFFFFF; | |
593 | bfin_write_EMAC_PTP_FV3(ptpfv3); | |
594 | break; | |
595 | case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: | |
596 | case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: | |
597 | case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: | |
598 | /* | |
599 | * Clear the five comparison mask bits (bits[12:8]) in EMAC_PTP_CTL) | |
600 | * to enable all the field matches. | |
601 | */ | |
602 | ptpctl &= ~0x1F00; | |
603 | bfin_write_EMAC_PTP_CTL(ptpctl); | |
604 | /* | |
605 | * Keep the default values of the EMAC_PTP_FOFF register. | |
606 | */ | |
607 | ptpfoff = 0x4A24170C; | |
608 | bfin_write_EMAC_PTP_FOFF(ptpfoff); | |
609 | /* | |
610 | * Keep the default values of the EMAC_PTP_FV1 and EMAC_PTP_FV2 | |
611 | * registers. | |
612 | */ | |
613 | ptpfv1 = 0x11040800; | |
614 | bfin_write_EMAC_PTP_FV1(ptpfv1); | |
615 | ptpfv2 = 0x0140013F; | |
616 | bfin_write_EMAC_PTP_FV2(ptpfv2); | |
617 | /* | |
618 | * The default value (0xFFFC) allows the timestamping of both | |
619 | * received Sync messages and Delay_Req messages. | |
620 | */ | |
621 | ptpfv3 = 0xFFFFFFFC; | |
622 | bfin_write_EMAC_PTP_FV3(ptpfv3); | |
623 | ||
624 | config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT; | |
625 | break; | |
626 | case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: | |
627 | case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: | |
628 | case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: | |
629 | /* Clear all five comparison mask bits (bits[12:8]) in the | |
630 | * EMAC_PTP_CTL register to enable all the field matches. | |
631 | */ | |
632 | ptpctl &= ~0x1F00; | |
633 | bfin_write_EMAC_PTP_CTL(ptpctl); | |
634 | /* | |
635 | * Keep the default values of the EMAC_PTP_FOFF register, except set | |
636 | * the PTPCOF field to 0x2A. | |
637 | */ | |
638 | ptpfoff = 0x2A24170C; | |
639 | bfin_write_EMAC_PTP_FOFF(ptpfoff); | |
640 | /* | |
641 | * Keep the default values of the EMAC_PTP_FV1 and EMAC_PTP_FV2 | |
642 | * registers. | |
643 | */ | |
644 | ptpfv1 = 0x11040800; | |
645 | bfin_write_EMAC_PTP_FV1(ptpfv1); | |
646 | ptpfv2 = 0x0140013F; | |
647 | bfin_write_EMAC_PTP_FV2(ptpfv2); | |
648 | /* | |
649 | * To allow the timestamping of Pdelay_Req and Pdelay_Resp, set | |
650 | * the value to 0xFFF0. | |
651 | */ | |
652 | ptpfv3 = 0xFFFFFFF0; | |
653 | bfin_write_EMAC_PTP_FV3(ptpfv3); | |
654 | ||
655 | config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT; | |
656 | break; | |
657 | case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: | |
658 | case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: | |
659 | case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: | |
660 | /* | |
661 | * Clear bits 8 and 12 of the EMAC_PTP_CTL register to enable only the | |
662 | * EFTM and PTPCM field comparison. | |
663 | */ | |
664 | ptpctl &= ~0x1100; | |
665 | bfin_write_EMAC_PTP_CTL(ptpctl); | |
666 | /* | |
667 | * Keep the default values of all the fields of the EMAC_PTP_FOFF | |
668 | * register, except set the PTPCOF field to 0x0E. | |
669 | */ | |
670 | ptpfoff = 0x0E24170C; | |
671 | bfin_write_EMAC_PTP_FOFF(ptpfoff); | |
672 | /* | |
673 | * Program bits [15:0] of the EMAC_PTP_FV1 register to 0x88F7, which | |
674 | * corresponds to PTP messages on the MAC layer. | |
675 | */ | |
676 | ptpfv1 = 0x110488F7; | |
677 | bfin_write_EMAC_PTP_FV1(ptpfv1); | |
678 | ptpfv2 = 0x0140013F; | |
679 | bfin_write_EMAC_PTP_FV2(ptpfv2); | |
680 | /* | |
681 | * To allow the timestamping of Pdelay_Req and Pdelay_Resp | |
682 | * messages, set the value to 0xFFF0. | |
683 | */ | |
684 | ptpfv3 = 0xFFFFFFF0; | |
685 | bfin_write_EMAC_PTP_FV3(ptpfv3); | |
686 | ||
687 | config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT; | |
688 | break; | |
689 | default: | |
690 | return -ERANGE; | |
691 | } | |
692 | ||
693 | if (config.tx_type == HWTSTAMP_TX_OFF && | |
694 | bfin_mac_hwtstamp_is_none(config.rx_filter)) { | |
695 | ptpctl &= ~PTP_EN; | |
696 | bfin_write_EMAC_PTP_CTL(ptpctl); | |
697 | ||
698 | SSYNC(); | |
699 | } else { | |
700 | ptpctl |= PTP_EN; | |
701 | bfin_write_EMAC_PTP_CTL(ptpctl); | |
702 | ||
703 | /* | |
704 | * clear any existing timestamp | |
705 | */ | |
706 | bfin_read_EMAC_PTP_RXSNAPLO(); | |
707 | bfin_read_EMAC_PTP_RXSNAPHI(); | |
708 | ||
709 | bfin_read_EMAC_PTP_TXSNAPLO(); | |
710 | bfin_read_EMAC_PTP_TXSNAPHI(); | |
711 | ||
712 | /* | |
713 | * Set registers so that rollover occurs soon to test this. | |
714 | */ | |
715 | bfin_write_EMAC_PTP_TIMELO(0x00000000); | |
716 | bfin_write_EMAC_PTP_TIMEHI(0xFF800000); | |
717 | ||
718 | SSYNC(); | |
719 | ||
720 | lp->compare.last_update = 0; | |
721 | timecounter_init(&lp->clock, | |
722 | &lp->cycles, | |
723 | ktime_to_ns(ktime_get_real())); | |
724 | timecompare_update(&lp->compare, 0); | |
725 | } | |
726 | ||
727 | lp->stamp_cfg = config; | |
728 | return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? | |
729 | -EFAULT : 0; | |
730 | } | |
731 | ||
732 | static void bfin_dump_hwtamp(char *s, ktime_t *hw, ktime_t *ts, struct timecompare *cmp) | |
733 | { | |
734 | ktime_t sys = ktime_get_real(); | |
735 | ||
736 | pr_debug("%s %s hardware:%d,%d transform system:%d,%d system:%d,%d, cmp:%lld, %lld\n", | |
737 | __func__, s, hw->tv.sec, hw->tv.nsec, ts->tv.sec, ts->tv.nsec, sys.tv.sec, | |
738 | sys.tv.nsec, cmp->offset, cmp->skew); | |
739 | } | |
740 | ||
741 | static void bfin_tx_hwtstamp(struct net_device *netdev, struct sk_buff *skb) | |
742 | { | |
743 | struct bfin_mac_local *lp = netdev_priv(netdev); | |
744 | union skb_shared_tx *shtx = skb_tx(skb); | |
745 | ||
746 | if (shtx->hardware) { | |
747 | int timeout_cnt = MAX_TIMEOUT_CNT; | |
748 | ||
749 | /* When doing time stamping, keep the connection to the socket | |
750 | * a while longer | |
751 | */ | |
752 | shtx->in_progress = 1; | |
753 | ||
754 | /* | |
755 | * The timestamping is done at the EMAC module's MII/RMII interface | |
756 | * when the module sees the Start of Frame of an event message packet. This | |
757 | * interface is the closest possible place to the physical Ethernet transmission | |
758 | * medium, providing the best timing accuracy. | |
759 | */ | |
760 | while ((!(bfin_read_EMAC_PTP_ISTAT() & TXTL)) && (--timeout_cnt)) | |
761 | udelay(1); | |
762 | if (timeout_cnt == 0) | |
763 | printk(KERN_ERR DRV_NAME | |
764 | ": fails to timestamp the TX packet\n"); | |
765 | else { | |
766 | struct skb_shared_hwtstamps shhwtstamps; | |
767 | u64 ns; | |
768 | u64 regval; | |
769 | ||
770 | regval = bfin_read_EMAC_PTP_TXSNAPLO(); | |
771 | regval |= (u64)bfin_read_EMAC_PTP_TXSNAPHI() << 32; | |
772 | memset(&shhwtstamps, 0, sizeof(shhwtstamps)); | |
773 | ns = timecounter_cyc2time(&lp->clock, | |
774 | regval); | |
775 | timecompare_update(&lp->compare, ns); | |
776 | shhwtstamps.hwtstamp = ns_to_ktime(ns); | |
777 | shhwtstamps.syststamp = | |
778 | timecompare_transform(&lp->compare, ns); | |
779 | skb_tstamp_tx(skb, &shhwtstamps); | |
780 | ||
781 | bfin_dump_hwtamp("TX", &shhwtstamps.hwtstamp, &shhwtstamps.syststamp, &lp->compare); | |
782 | } | |
783 | } | |
784 | } | |
785 | ||
786 | static void bfin_rx_hwtstamp(struct net_device *netdev, struct sk_buff *skb) | |
787 | { | |
788 | struct bfin_mac_local *lp = netdev_priv(netdev); | |
789 | u32 valid; | |
790 | u64 regval, ns; | |
791 | struct skb_shared_hwtstamps *shhwtstamps; | |
792 | ||
793 | if (bfin_mac_hwtstamp_is_none(lp->stamp_cfg.rx_filter)) | |
794 | return; | |
795 | ||
796 | valid = bfin_read_EMAC_PTP_ISTAT() & RXEL; | |
797 | if (!valid) | |
798 | return; | |
799 | ||
800 | shhwtstamps = skb_hwtstamps(skb); | |
801 | ||
802 | regval = bfin_read_EMAC_PTP_RXSNAPLO(); | |
803 | regval |= (u64)bfin_read_EMAC_PTP_RXSNAPHI() << 32; | |
804 | ns = timecounter_cyc2time(&lp->clock, regval); | |
805 | timecompare_update(&lp->compare, ns); | |
806 | memset(shhwtstamps, 0, sizeof(*shhwtstamps)); | |
807 | shhwtstamps->hwtstamp = ns_to_ktime(ns); | |
808 | shhwtstamps->syststamp = timecompare_transform(&lp->compare, ns); | |
809 | ||
810 | bfin_dump_hwtamp("RX", &shhwtstamps->hwtstamp, &shhwtstamps->syststamp, &lp->compare); | |
811 | } | |
812 | ||
813 | /* | |
814 | * bfin_read_clock - read raw cycle counter (to be used by time counter) | |
815 | */ | |
816 | static cycle_t bfin_read_clock(const struct cyclecounter *tc) | |
817 | { | |
818 | u64 stamp; | |
819 | ||
820 | stamp = bfin_read_EMAC_PTP_TIMELO(); | |
821 | stamp |= (u64)bfin_read_EMAC_PTP_TIMEHI() << 32ULL; | |
822 | ||
823 | return stamp; | |
824 | } | |
825 | ||
826 | #define PTP_CLK 25000000 | |
827 | ||
828 | static void bfin_mac_hwtstamp_init(struct net_device *netdev) | |
829 | { | |
830 | struct bfin_mac_local *lp = netdev_priv(netdev); | |
831 | u64 append; | |
832 | ||
833 | /* Initialize hardware timer */ | |
834 | append = PTP_CLK * (1ULL << 32); | |
835 | do_div(append, get_sclk()); | |
836 | bfin_write_EMAC_PTP_ADDEND((u32)append); | |
837 | ||
838 | memset(&lp->cycles, 0, sizeof(lp->cycles)); | |
839 | lp->cycles.read = bfin_read_clock; | |
840 | lp->cycles.mask = CLOCKSOURCE_MASK(64); | |
841 | lp->cycles.mult = 1000000000 / PTP_CLK; | |
842 | lp->cycles.shift = 0; | |
843 | ||
844 | /* Synchronize our NIC clock against system wall clock */ | |
845 | memset(&lp->compare, 0, sizeof(lp->compare)); | |
846 | lp->compare.source = &lp->clock; | |
847 | lp->compare.target = ktime_get_real; | |
848 | lp->compare.num_samples = 10; | |
849 | ||
850 | /* Initialize hwstamp config */ | |
851 | lp->stamp_cfg.rx_filter = HWTSTAMP_FILTER_NONE; | |
852 | lp->stamp_cfg.tx_type = HWTSTAMP_TX_OFF; | |
853 | } | |
854 | ||
855 | #else | |
856 | # define bfin_mac_hwtstamp_is_none(cfg) 0 | |
857 | # define bfin_mac_hwtstamp_init(dev) | |
858 | # define bfin_mac_hwtstamp_ioctl(dev, ifr, cmd) (-EOPNOTSUPP) | |
859 | # define bfin_rx_hwtstamp(dev, skb) | |
860 | # define bfin_tx_hwtstamp(dev, skb) | |
861 | #endif | |
862 | ||
e190d6b1 BW |
863 | static void adjust_tx_list(void) |
864 | { | |
865 | int timeout_cnt = MAX_TIMEOUT_CNT; | |
866 | ||
8e95a202 JP |
867 | if (tx_list_head->status.status_word != 0 && |
868 | current_tx_ptr != tx_list_head) { | |
e190d6b1 BW |
869 | goto adjust_head; /* released something, just return; */ |
870 | } | |
871 | ||
872 | /* | |
873 | * if nothing released, check wait condition | |
874 | * current's next can not be the head, | |
875 | * otherwise the dma will not stop as we want | |
876 | */ | |
877 | if (current_tx_ptr->next->next == tx_list_head) { | |
878 | while (tx_list_head->status.status_word == 0) { | |
015dac88 | 879 | udelay(10); |
8e95a202 JP |
880 | if (tx_list_head->status.status_word != 0 || |
881 | !(bfin_read_DMA2_IRQ_STATUS() & DMA_RUN)) { | |
e190d6b1 BW |
882 | goto adjust_head; |
883 | } | |
884 | if (timeout_cnt-- < 0) { | |
885 | printk(KERN_ERR DRV_NAME | |
886 | ": wait for adjust tx list head timeout\n"); | |
887 | break; | |
888 | } | |
889 | } | |
890 | if (tx_list_head->status.status_word != 0) { | |
891 | goto adjust_head; | |
892 | } | |
893 | } | |
894 | ||
895 | return; | |
896 | ||
897 | adjust_head: | |
898 | do { | |
899 | tx_list_head->desc_a.config &= ~DMAEN; | |
900 | tx_list_head->status.status_word = 0; | |
901 | if (tx_list_head->skb) { | |
902 | dev_kfree_skb(tx_list_head->skb); | |
903 | tx_list_head->skb = NULL; | |
904 | } else { | |
905 | printk(KERN_ERR DRV_NAME | |
906 | ": no sk_buff in a transmitted frame!\n"); | |
907 | } | |
908 | tx_list_head = tx_list_head->next; | |
8e95a202 JP |
909 | } while (tx_list_head->status.status_word != 0 && |
910 | current_tx_ptr != tx_list_head); | |
e190d6b1 BW |
911 | return; |
912 | ||
913 | } | |
914 | ||
7ef0a7ee | 915 | static int bfin_mac_hard_start_xmit(struct sk_buff *skb, |
e190d6b1 BW |
916 | struct net_device *dev) |
917 | { | |
a50c0c05 | 918 | u16 *data; |
015dac88 | 919 | u32 data_align = (unsigned long)(skb->data) & 0x3; |
fe92afed BS |
920 | union skb_shared_tx *shtx = skb_tx(skb); |
921 | ||
e190d6b1 BW |
922 | current_tx_ptr->skb = skb; |
923 | ||
015dac88 MH |
924 | if (data_align == 0x2) { |
925 | /* move skb->data to current_tx_ptr payload */ | |
926 | data = (u16 *)(skb->data) - 1; | |
fe92afed BS |
927 | *data = (u16)(skb->len); |
928 | /* | |
929 | * When transmitting an Ethernet packet, the PTP_TSYNC module requires | |
930 | * a DMA_Length_Word field associated with the packet. The lower 12 bits | |
931 | * of this field are the length of the packet payload in bytes and the higher | |
932 | * 4 bits are the timestamping enable field. | |
933 | */ | |
934 | if (shtx->hardware) | |
935 | *data |= 0x1000; | |
936 | ||
015dac88 MH |
937 | current_tx_ptr->desc_a.start_addr = (u32)data; |
938 | /* this is important! */ | |
939 | blackfin_dcache_flush_range((u32)data, | |
940 | (u32)((u8 *)data + skb->len + 4)); | |
e190d6b1 | 941 | } else { |
015dac88 | 942 | *((u16 *)(current_tx_ptr->packet)) = (u16)(skb->len); |
fe92afed BS |
943 | /* enable timestamping for the sent packet */ |
944 | if (shtx->hardware) | |
945 | *((u16 *)(current_tx_ptr->packet)) |= 0x1000; | |
015dac88 MH |
946 | memcpy((u8 *)(current_tx_ptr->packet + 2), skb->data, |
947 | skb->len); | |
948 | current_tx_ptr->desc_a.start_addr = | |
949 | (u32)current_tx_ptr->packet; | |
950 | if (current_tx_ptr->status.status_word != 0) | |
951 | current_tx_ptr->status.status_word = 0; | |
952 | blackfin_dcache_flush_range( | |
953 | (u32)current_tx_ptr->packet, | |
954 | (u32)(current_tx_ptr->packet + skb->len + 2)); | |
e190d6b1 BW |
955 | } |
956 | ||
805a8ab3 SZ |
957 | /* make sure the internal data buffers in the core are drained |
958 | * so that the DMA descriptors are completely written when the | |
959 | * DMA engine goes to fetch them below | |
960 | */ | |
961 | SSYNC(); | |
962 | ||
e190d6b1 BW |
963 | /* enable this packet's dma */ |
964 | current_tx_ptr->desc_a.config |= DMAEN; | |
965 | ||
966 | /* tx dma is running, just return */ | |
015dac88 | 967 | if (bfin_read_DMA2_IRQ_STATUS() & DMA_RUN) |
e190d6b1 BW |
968 | goto out; |
969 | ||
970 | /* tx dma is not running */ | |
971 | bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a)); | |
972 | /* dma enabled, read from memory, size is 6 */ | |
973 | bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config); | |
974 | /* Turn on the EMAC tx */ | |
975 | bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE); | |
976 | ||
977 | out: | |
978 | adjust_tx_list(); | |
fe92afed BS |
979 | |
980 | bfin_tx_hwtstamp(dev, skb); | |
981 | ||
e190d6b1 | 982 | current_tx_ptr = current_tx_ptr->next; |
09f75cd7 JG |
983 | dev->stats.tx_packets++; |
984 | dev->stats.tx_bytes += (skb->len); | |
6ed10654 | 985 | return NETDEV_TX_OK; |
e190d6b1 BW |
986 | } |
987 | ||
ad2864d8 | 988 | #define IP_HEADER_OFF 0 |
ec497b32 PM |
989 | #define RX_ERROR_MASK (RX_LONG | RX_ALIGN | RX_CRC | RX_LEN | \ |
990 | RX_FRAG | RX_ADDR | RX_DMAO | RX_PHY | RX_LATE | RX_RANGE) | |
991 | ||
7ef0a7ee | 992 | static void bfin_mac_rx(struct net_device *dev) |
e190d6b1 BW |
993 | { |
994 | struct sk_buff *skb, *new_skb; | |
e190d6b1 | 995 | unsigned short len; |
fe92afed | 996 | struct bfin_mac_local *lp __maybe_unused = netdev_priv(dev); |
ad2864d8 SZ |
997 | #if defined(BFIN_MAC_CSUM_OFFLOAD) |
998 | unsigned int i; | |
999 | unsigned char fcs[ETH_FCS_LEN + 1]; | |
1000 | #endif | |
e190d6b1 | 1001 | |
ec497b32 PM |
1002 | /* check if frame status word reports an error condition |
1003 | * we which case we simply drop the packet | |
1004 | */ | |
1005 | if (current_rx_ptr->status.status_word & RX_ERROR_MASK) { | |
1006 | printk(KERN_NOTICE DRV_NAME | |
1007 | ": rx: receive error - packet dropped\n"); | |
1008 | dev->stats.rx_dropped++; | |
1009 | goto out; | |
1010 | } | |
1011 | ||
e190d6b1 BW |
1012 | /* allocate a new skb for next time receive */ |
1013 | skb = current_rx_ptr->skb; | |
fe92afed | 1014 | |
015dac88 | 1015 | new_skb = dev_alloc_skb(PKT_BUF_SZ + NET_IP_ALIGN); |
e190d6b1 BW |
1016 | if (!new_skb) { |
1017 | printk(KERN_NOTICE DRV_NAME | |
1018 | ": rx: low on mem - packet dropped\n"); | |
09f75cd7 | 1019 | dev->stats.rx_dropped++; |
e190d6b1 BW |
1020 | goto out; |
1021 | } | |
1022 | /* reserve 2 bytes for RXDWA padding */ | |
015dac88 | 1023 | skb_reserve(new_skb, NET_IP_ALIGN); |
6e01d1a4 AD |
1024 | /* Invidate the data cache of skb->data range when it is write back |
1025 | * cache. It will prevent overwritting the new data from DMA | |
1026 | */ | |
1027 | blackfin_dcache_invalidate_range((unsigned long)new_skb->head, | |
1028 | (unsigned long)new_skb->end); | |
1029 | ||
f6e1e4f3 SZ |
1030 | current_rx_ptr->skb = new_skb; |
1031 | current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2; | |
1032 | ||
e190d6b1 | 1033 | len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN); |
ad2864d8 SZ |
1034 | /* Deduce Ethernet FCS length from Ethernet payload length */ |
1035 | len -= ETH_FCS_LEN; | |
e190d6b1 | 1036 | skb_put(skb, len); |
e190d6b1 | 1037 | |
e190d6b1 | 1038 | skb->protocol = eth_type_trans(skb, dev); |
fe92afed BS |
1039 | |
1040 | bfin_rx_hwtstamp(dev, skb); | |
1041 | ||
e190d6b1 | 1042 | #if defined(BFIN_MAC_CSUM_OFFLOAD) |
ad2864d8 SZ |
1043 | /* Checksum offloading only works for IPv4 packets with the standard IP header |
1044 | * length of 20 bytes, because the blackfin MAC checksum calculation is | |
1045 | * based on that assumption. We must NOT use the calculated checksum if our | |
1046 | * IP version or header break that assumption. | |
1047 | */ | |
1048 | if (skb->data[IP_HEADER_OFF] == 0x45) { | |
1049 | skb->csum = current_rx_ptr->status.ip_payload_csum; | |
1050 | /* | |
1051 | * Deduce Ethernet FCS from hardware generated IP payload checksum. | |
1052 | * IP checksum is based on 16-bit one's complement algorithm. | |
1053 | * To deduce a value from checksum is equal to add its inversion. | |
1054 | * If the IP payload len is odd, the inversed FCS should also | |
1055 | * begin from odd address and leave first byte zero. | |
1056 | */ | |
1057 | if (skb->len % 2) { | |
1058 | fcs[0] = 0; | |
1059 | for (i = 0; i < ETH_FCS_LEN; i++) | |
1060 | fcs[i + 1] = ~skb->data[skb->len + i]; | |
1061 | skb->csum = csum_partial(fcs, ETH_FCS_LEN + 1, skb->csum); | |
1062 | } else { | |
1063 | for (i = 0; i < ETH_FCS_LEN; i++) | |
1064 | fcs[i] = ~skb->data[skb->len + i]; | |
1065 | skb->csum = csum_partial(fcs, ETH_FCS_LEN, skb->csum); | |
1066 | } | |
1067 | skb->ip_summed = CHECKSUM_COMPLETE; | |
1068 | } | |
e190d6b1 BW |
1069 | #endif |
1070 | ||
1071 | netif_rx(skb); | |
09f75cd7 JG |
1072 | dev->stats.rx_packets++; |
1073 | dev->stats.rx_bytes += len; | |
ec497b32 | 1074 | out: |
e190d6b1 BW |
1075 | current_rx_ptr->status.status_word = 0x00000000; |
1076 | current_rx_ptr = current_rx_ptr->next; | |
e190d6b1 BW |
1077 | } |
1078 | ||
1079 | /* interrupt routine to handle rx and error signal */ | |
7ef0a7ee | 1080 | static irqreturn_t bfin_mac_interrupt(int irq, void *dev_id) |
e190d6b1 BW |
1081 | { |
1082 | struct net_device *dev = dev_id; | |
1083 | int number = 0; | |
1084 | ||
1085 | get_one_packet: | |
1086 | if (current_rx_ptr->status.status_word == 0) { | |
1087 | /* no more new packet received */ | |
1088 | if (number == 0) { | |
1089 | if (current_rx_ptr->next->status.status_word != 0) { | |
1090 | current_rx_ptr = current_rx_ptr->next; | |
1091 | goto real_rx; | |
1092 | } | |
1093 | } | |
1094 | bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() | | |
1095 | DMA_DONE | DMA_ERR); | |
1096 | return IRQ_HANDLED; | |
1097 | } | |
1098 | ||
1099 | real_rx: | |
7ef0a7ee | 1100 | bfin_mac_rx(dev); |
e190d6b1 BW |
1101 | number++; |
1102 | goto get_one_packet; | |
1103 | } | |
1104 | ||
1105 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
7ef0a7ee | 1106 | static void bfin_mac_poll(struct net_device *dev) |
e190d6b1 BW |
1107 | { |
1108 | disable_irq(IRQ_MAC_RX); | |
7ef0a7ee | 1109 | bfin_mac_interrupt(IRQ_MAC_RX, dev); |
e190d6b1 BW |
1110 | enable_irq(IRQ_MAC_RX); |
1111 | } | |
1112 | #endif /* CONFIG_NET_POLL_CONTROLLER */ | |
1113 | ||
7ef0a7ee | 1114 | static void bfin_mac_disable(void) |
e190d6b1 BW |
1115 | { |
1116 | unsigned int opmode; | |
1117 | ||
1118 | opmode = bfin_read_EMAC_OPMODE(); | |
1119 | opmode &= (~RE); | |
1120 | opmode &= (~TE); | |
1121 | /* Turn off the EMAC */ | |
1122 | bfin_write_EMAC_OPMODE(opmode); | |
1123 | } | |
1124 | ||
1125 | /* | |
1126 | * Enable Interrupts, Receive, and Transmit | |
1127 | */ | |
7ef0a7ee | 1128 | static void bfin_mac_enable(void) |
e190d6b1 BW |
1129 | { |
1130 | u32 opmode; | |
1131 | ||
b39d66a8 | 1132 | pr_debug("%s: %s\n", DRV_NAME, __func__); |
e190d6b1 BW |
1133 | |
1134 | /* Set RX DMA */ | |
1135 | bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a)); | |
1136 | bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config); | |
1137 | ||
1138 | /* Wait MII done */ | |
0ed0563e | 1139 | bfin_mdio_poll(); |
e190d6b1 BW |
1140 | |
1141 | /* We enable only RX here */ | |
1142 | /* ASTP : Enable Automatic Pad Stripping | |
1143 | PR : Promiscuous Mode for test | |
1144 | PSF : Receive frames with total length less than 64 bytes. | |
1145 | FDMODE : Full Duplex Mode | |
1146 | LB : Internal Loopback for test | |
1147 | RE : Receiver Enable */ | |
1148 | opmode = bfin_read_EMAC_OPMODE(); | |
1149 | if (opmode & FDMODE) | |
1150 | opmode |= PSF; | |
1151 | else | |
1152 | opmode |= DRO | DC | PSF; | |
1153 | opmode |= RE; | |
1154 | ||
1155 | #if defined(CONFIG_BFIN_MAC_RMII) | |
1156 | opmode |= RMII; /* For Now only 100MBit are supported */ | |
6893ff1c | 1157 | #if (defined(CONFIG_BF537) || defined(CONFIG_BF536)) && CONFIG_BF_REV_0_2 |
e190d6b1 BW |
1158 | opmode |= TE; |
1159 | #endif | |
1160 | #endif | |
1161 | /* Turn on the EMAC rx */ | |
1162 | bfin_write_EMAC_OPMODE(opmode); | |
e190d6b1 BW |
1163 | } |
1164 | ||
1165 | /* Our watchdog timed out. Called by the networking layer */ | |
7ef0a7ee | 1166 | static void bfin_mac_timeout(struct net_device *dev) |
e190d6b1 | 1167 | { |
b39d66a8 | 1168 | pr_debug("%s: %s\n", dev->name, __func__); |
e190d6b1 | 1169 | |
7ef0a7ee | 1170 | bfin_mac_disable(); |
e190d6b1 BW |
1171 | |
1172 | /* reset tx queue */ | |
1173 | tx_list_tail = tx_list_head->next; | |
1174 | ||
7ef0a7ee | 1175 | bfin_mac_enable(); |
e190d6b1 BW |
1176 | |
1177 | /* We can accept TX packets again */ | |
1ae5dc34 | 1178 | dev->trans_start = jiffies; /* prevent tx timeout */ |
e190d6b1 BW |
1179 | netif_wake_queue(dev); |
1180 | } | |
1181 | ||
7ef0a7ee | 1182 | static void bfin_mac_multicast_hash(struct net_device *dev) |
775919bc AW |
1183 | { |
1184 | u32 emac_hashhi, emac_hashlo; | |
22bedad3 | 1185 | struct netdev_hw_addr *ha; |
775919bc | 1186 | char *addrs; |
775919bc AW |
1187 | u32 crc; |
1188 | ||
1189 | emac_hashhi = emac_hashlo = 0; | |
1190 | ||
22bedad3 JP |
1191 | netdev_for_each_mc_addr(ha, dev) { |
1192 | addrs = ha->addr; | |
775919bc AW |
1193 | |
1194 | /* skip non-multicast addresses */ | |
1195 | if (!(*addrs & 1)) | |
1196 | continue; | |
1197 | ||
1198 | crc = ether_crc(ETH_ALEN, addrs); | |
1199 | crc >>= 26; | |
1200 | ||
1201 | if (crc & 0x20) | |
1202 | emac_hashhi |= 1 << (crc & 0x1f); | |
1203 | else | |
1204 | emac_hashlo |= 1 << (crc & 0x1f); | |
1205 | } | |
1206 | ||
1207 | bfin_write_EMAC_HASHHI(emac_hashhi); | |
1208 | bfin_write_EMAC_HASHLO(emac_hashlo); | |
775919bc AW |
1209 | } |
1210 | ||
e190d6b1 BW |
1211 | /* |
1212 | * This routine will, depending on the values passed to it, | |
1213 | * either make it accept multicast packets, go into | |
1214 | * promiscuous mode (for TCPDUMP and cousins) or accept | |
1215 | * a select set of multicast packets | |
1216 | */ | |
7ef0a7ee | 1217 | static void bfin_mac_set_multicast_list(struct net_device *dev) |
e190d6b1 BW |
1218 | { |
1219 | u32 sysctl; | |
1220 | ||
1221 | if (dev->flags & IFF_PROMISC) { | |
1222 | printk(KERN_INFO "%s: set to promisc mode\n", dev->name); | |
1223 | sysctl = bfin_read_EMAC_OPMODE(); | |
1224 | sysctl |= RAF; | |
1225 | bfin_write_EMAC_OPMODE(sysctl); | |
775919bc | 1226 | } else if (dev->flags & IFF_ALLMULTI) { |
e190d6b1 BW |
1227 | /* accept all multicast */ |
1228 | sysctl = bfin_read_EMAC_OPMODE(); | |
1229 | sysctl |= PAM; | |
1230 | bfin_write_EMAC_OPMODE(sysctl); | |
4cd24eaf | 1231 | } else if (!netdev_mc_empty(dev)) { |
775919bc AW |
1232 | /* set up multicast hash table */ |
1233 | sysctl = bfin_read_EMAC_OPMODE(); | |
1234 | sysctl |= HM; | |
1235 | bfin_write_EMAC_OPMODE(sysctl); | |
7ef0a7ee | 1236 | bfin_mac_multicast_hash(dev); |
e190d6b1 BW |
1237 | } else { |
1238 | /* clear promisc or multicast mode */ | |
1239 | sysctl = bfin_read_EMAC_OPMODE(); | |
1240 | sysctl &= ~(RAF | PAM); | |
1241 | bfin_write_EMAC_OPMODE(sysctl); | |
1242 | } | |
1243 | } | |
1244 | ||
fe92afed BS |
1245 | static int bfin_mac_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd) |
1246 | { | |
1247 | switch (cmd) { | |
1248 | case SIOCSHWTSTAMP: | |
1249 | return bfin_mac_hwtstamp_ioctl(netdev, ifr, cmd); | |
1250 | default: | |
1251 | return -EOPNOTSUPP; | |
1252 | } | |
1253 | } | |
1254 | ||
e190d6b1 BW |
1255 | /* |
1256 | * this puts the device in an inactive state | |
1257 | */ | |
7ef0a7ee | 1258 | static void bfin_mac_shutdown(struct net_device *dev) |
e190d6b1 BW |
1259 | { |
1260 | /* Turn off the EMAC */ | |
1261 | bfin_write_EMAC_OPMODE(0x00000000); | |
1262 | /* Turn off the EMAC RX DMA */ | |
1263 | bfin_write_DMA1_CONFIG(0x0000); | |
1264 | bfin_write_DMA2_CONFIG(0x0000); | |
1265 | } | |
1266 | ||
1267 | /* | |
1268 | * Open and Initialize the interface | |
1269 | * | |
1270 | * Set up everything, reset the card, etc.. | |
1271 | */ | |
7ef0a7ee | 1272 | static int bfin_mac_open(struct net_device *dev) |
e190d6b1 | 1273 | { |
7ef0a7ee | 1274 | struct bfin_mac_local *lp = netdev_priv(dev); |
4af4b840 | 1275 | int retval; |
b39d66a8 | 1276 | pr_debug("%s: %s\n", dev->name, __func__); |
e190d6b1 BW |
1277 | |
1278 | /* | |
1279 | * Check that the address is valid. If its not, refuse | |
1280 | * to bring the device up. The user must specify an | |
1281 | * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx | |
1282 | */ | |
1283 | if (!is_valid_ether_addr(dev->dev_addr)) { | |
1284 | printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n"); | |
1285 | return -EINVAL; | |
1286 | } | |
1287 | ||
1288 | /* initial rx and tx list */ | |
4af4b840 MH |
1289 | retval = desc_list_init(); |
1290 | ||
1291 | if (retval) | |
1292 | return retval; | |
e190d6b1 | 1293 | |
4ae5a3ad | 1294 | phy_start(lp->phydev); |
136492b2 | 1295 | phy_write(lp->phydev, MII_BMCR, BMCR_RESET); |
e190d6b1 | 1296 | setup_system_regs(dev); |
ee02fee8 | 1297 | setup_mac_addr(dev->dev_addr); |
7ef0a7ee BW |
1298 | bfin_mac_disable(); |
1299 | bfin_mac_enable(); | |
e190d6b1 BW |
1300 | pr_debug("hardware init finished\n"); |
1301 | netif_start_queue(dev); | |
1302 | netif_carrier_on(dev); | |
1303 | ||
1304 | return 0; | |
1305 | } | |
1306 | ||
1307 | /* | |
e190d6b1 BW |
1308 | * this makes the board clean up everything that it can |
1309 | * and not talk to the outside world. Caused by | |
1310 | * an 'ifconfig ethX down' | |
1311 | */ | |
7ef0a7ee | 1312 | static int bfin_mac_close(struct net_device *dev) |
e190d6b1 | 1313 | { |
7ef0a7ee | 1314 | struct bfin_mac_local *lp = netdev_priv(dev); |
b39d66a8 | 1315 | pr_debug("%s: %s\n", dev->name, __func__); |
e190d6b1 BW |
1316 | |
1317 | netif_stop_queue(dev); | |
1318 | netif_carrier_off(dev); | |
1319 | ||
4ae5a3ad | 1320 | phy_stop(lp->phydev); |
136492b2 | 1321 | phy_write(lp->phydev, MII_BMCR, BMCR_PDOWN); |
4ae5a3ad | 1322 | |
e190d6b1 | 1323 | /* clear everything */ |
7ef0a7ee | 1324 | bfin_mac_shutdown(dev); |
e190d6b1 BW |
1325 | |
1326 | /* free the rx/tx buffers */ | |
1327 | desc_list_free(); | |
1328 | ||
1329 | return 0; | |
1330 | } | |
1331 | ||
b63dc8fe MF |
1332 | static const struct net_device_ops bfin_mac_netdev_ops = { |
1333 | .ndo_open = bfin_mac_open, | |
1334 | .ndo_stop = bfin_mac_close, | |
1335 | .ndo_start_xmit = bfin_mac_hard_start_xmit, | |
1336 | .ndo_set_mac_address = bfin_mac_set_mac_address, | |
1337 | .ndo_tx_timeout = bfin_mac_timeout, | |
1338 | .ndo_set_multicast_list = bfin_mac_set_multicast_list, | |
fe92afed | 1339 | .ndo_do_ioctl = bfin_mac_ioctl, |
b63dc8fe MF |
1340 | .ndo_validate_addr = eth_validate_addr, |
1341 | .ndo_change_mtu = eth_change_mtu, | |
1342 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
1343 | .ndo_poll_controller = bfin_mac_poll, | |
1344 | #endif | |
1345 | }; | |
1346 | ||
d7b843d3 | 1347 | static int __devinit bfin_mac_probe(struct platform_device *pdev) |
e190d6b1 | 1348 | { |
7ef0a7ee BW |
1349 | struct net_device *ndev; |
1350 | struct bfin_mac_local *lp; | |
080c8255 GY |
1351 | struct platform_device *pd; |
1352 | int rc; | |
7ef0a7ee BW |
1353 | |
1354 | ndev = alloc_etherdev(sizeof(struct bfin_mac_local)); | |
1355 | if (!ndev) { | |
1356 | dev_err(&pdev->dev, "Cannot allocate net device!\n"); | |
1357 | return -ENOMEM; | |
1358 | } | |
1359 | ||
1360 | SET_NETDEV_DEV(ndev, &pdev->dev); | |
1361 | platform_set_drvdata(pdev, ndev); | |
1362 | lp = netdev_priv(ndev); | |
e190d6b1 BW |
1363 | |
1364 | /* Grab the MAC address in the MAC */ | |
7ef0a7ee BW |
1365 | *(__le32 *) (&(ndev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO()); |
1366 | *(__le16 *) (&(ndev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI()); | |
e190d6b1 BW |
1367 | |
1368 | /* probe mac */ | |
1369 | /*todo: how to proble? which is revision_register */ | |
1370 | bfin_write_EMAC_ADDRLO(0x12345678); | |
1371 | if (bfin_read_EMAC_ADDRLO() != 0x12345678) { | |
7ef0a7ee BW |
1372 | dev_err(&pdev->dev, "Cannot detect Blackfin on-chip ethernet MAC controller!\n"); |
1373 | rc = -ENODEV; | |
1374 | goto out_err_probe_mac; | |
e190d6b1 BW |
1375 | } |
1376 | ||
e190d6b1 | 1377 | |
7ef0a7ee BW |
1378 | /* |
1379 | * Is it valid? (Did bootloader initialize it?) | |
1380 | * Grab the MAC from the board somehow | |
1381 | * this is done in the arch/blackfin/mach-bfxxx/boards/eth_mac.c | |
1382 | */ | |
1383 | if (!is_valid_ether_addr(ndev->dev_addr)) | |
1384 | bfin_get_ether_addr(ndev->dev_addr); | |
1385 | ||
e190d6b1 | 1386 | /* If still not valid, get a random one */ |
7ef0a7ee BW |
1387 | if (!is_valid_ether_addr(ndev->dev_addr)) |
1388 | random_ether_addr(ndev->dev_addr); | |
e190d6b1 | 1389 | |
7ef0a7ee | 1390 | setup_mac_addr(ndev->dev_addr); |
e190d6b1 | 1391 | |
080c8255 GY |
1392 | if (!pdev->dev.platform_data) { |
1393 | dev_err(&pdev->dev, "Cannot get platform device bfin_mii_bus!\n"); | |
1394 | rc = -ENODEV; | |
1395 | goto out_err_probe_mac; | |
7ef0a7ee | 1396 | } |
080c8255 GY |
1397 | pd = pdev->dev.platform_data; |
1398 | lp->mii_bus = platform_get_drvdata(pd); | |
1399 | lp->mii_bus->priv = ndev; | |
4ae5a3ad | 1400 | |
7ef0a7ee BW |
1401 | rc = mii_probe(ndev); |
1402 | if (rc) { | |
1403 | dev_err(&pdev->dev, "MII Probe failed!\n"); | |
1404 | goto out_err_mii_probe; | |
1405 | } | |
4ae5a3ad | 1406 | |
e190d6b1 | 1407 | /* Fill in the fields of the device structure with ethernet values. */ |
7ef0a7ee BW |
1408 | ether_setup(ndev); |
1409 | ||
149da651 | 1410 | ndev->netdev_ops = &bfin_mac_netdev_ops; |
679dce39 | 1411 | ndev->ethtool_ops = &bfin_mac_ethtool_ops; |
e190d6b1 | 1412 | |
e190d6b1 BW |
1413 | spin_lock_init(&lp->lock); |
1414 | ||
1415 | /* now, enable interrupts */ | |
1416 | /* register irq handler */ | |
7ef0a7ee | 1417 | rc = request_irq(IRQ_MAC_RX, bfin_mac_interrupt, |
91a455f0 | 1418 | IRQF_DISABLED, "EMAC_RX", ndev); |
7ef0a7ee BW |
1419 | if (rc) { |
1420 | dev_err(&pdev->dev, "Cannot request Blackfin MAC RX IRQ!\n"); | |
1421 | rc = -EBUSY; | |
1422 | goto out_err_request_irq; | |
e190d6b1 BW |
1423 | } |
1424 | ||
7ef0a7ee BW |
1425 | rc = register_netdev(ndev); |
1426 | if (rc) { | |
1427 | dev_err(&pdev->dev, "Cannot register net device!\n"); | |
1428 | goto out_err_reg_ndev; | |
e190d6b1 BW |
1429 | } |
1430 | ||
fe92afed BS |
1431 | bfin_mac_hwtstamp_init(ndev); |
1432 | ||
7ef0a7ee BW |
1433 | /* now, print out the card info, in a short format.. */ |
1434 | dev_info(&pdev->dev, "%s, Version %s\n", DRV_DESC, DRV_VERSION); | |
e190d6b1 | 1435 | |
7ef0a7ee | 1436 | return 0; |
e190d6b1 | 1437 | |
7ef0a7ee BW |
1438 | out_err_reg_ndev: |
1439 | free_irq(IRQ_MAC_RX, ndev); | |
1440 | out_err_request_irq: | |
1441 | out_err_mii_probe: | |
298cf9be | 1442 | mdiobus_unregister(lp->mii_bus); |
298cf9be | 1443 | mdiobus_free(lp->mii_bus); |
7ef0a7ee | 1444 | peripheral_free_list(pin_req); |
7ef0a7ee BW |
1445 | out_err_probe_mac: |
1446 | platform_set_drvdata(pdev, NULL); | |
1447 | free_netdev(ndev); | |
e190d6b1 | 1448 | |
7ef0a7ee | 1449 | return rc; |
e190d6b1 BW |
1450 | } |
1451 | ||
d7b843d3 | 1452 | static int __devexit bfin_mac_remove(struct platform_device *pdev) |
e190d6b1 BW |
1453 | { |
1454 | struct net_device *ndev = platform_get_drvdata(pdev); | |
7ef0a7ee | 1455 | struct bfin_mac_local *lp = netdev_priv(ndev); |
e190d6b1 BW |
1456 | |
1457 | platform_set_drvdata(pdev, NULL); | |
1458 | ||
080c8255 | 1459 | lp->mii_bus->priv = NULL; |
7ef0a7ee | 1460 | |
e190d6b1 BW |
1461 | unregister_netdev(ndev); |
1462 | ||
1463 | free_irq(IRQ_MAC_RX, ndev); | |
1464 | ||
1465 | free_netdev(ndev); | |
1466 | ||
7ef0a7ee | 1467 | peripheral_free_list(pin_req); |
e190d6b1 BW |
1468 | |
1469 | return 0; | |
1470 | } | |
1471 | ||
496a34c2 BW |
1472 | #ifdef CONFIG_PM |
1473 | static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg) | |
e190d6b1 | 1474 | { |
496a34c2 BW |
1475 | struct net_device *net_dev = platform_get_drvdata(pdev); |
1476 | ||
1477 | if (netif_running(net_dev)) | |
7ef0a7ee | 1478 | bfin_mac_close(net_dev); |
496a34c2 | 1479 | |
e190d6b1 BW |
1480 | return 0; |
1481 | } | |
1482 | ||
1483 | static int bfin_mac_resume(struct platform_device *pdev) | |
1484 | { | |
496a34c2 BW |
1485 | struct net_device *net_dev = platform_get_drvdata(pdev); |
1486 | ||
1487 | if (netif_running(net_dev)) | |
7ef0a7ee | 1488 | bfin_mac_open(net_dev); |
496a34c2 | 1489 | |
e190d6b1 BW |
1490 | return 0; |
1491 | } | |
496a34c2 BW |
1492 | #else |
1493 | #define bfin_mac_suspend NULL | |
1494 | #define bfin_mac_resume NULL | |
1495 | #endif /* CONFIG_PM */ | |
e190d6b1 | 1496 | |
080c8255 GY |
1497 | static int __devinit bfin_mii_bus_probe(struct platform_device *pdev) |
1498 | { | |
1499 | struct mii_bus *miibus; | |
1500 | int rc, i; | |
1501 | ||
1502 | /* | |
1503 | * We are setting up a network card, | |
1504 | * so set the GPIO pins to Ethernet mode | |
1505 | */ | |
1506 | rc = peripheral_request_list(pin_req, DRV_NAME); | |
1507 | if (rc) { | |
1508 | dev_err(&pdev->dev, "Requesting peripherals failed!\n"); | |
1509 | return rc; | |
1510 | } | |
1511 | ||
1512 | rc = -ENOMEM; | |
1513 | miibus = mdiobus_alloc(); | |
1514 | if (miibus == NULL) | |
1515 | goto out_err_alloc; | |
1516 | miibus->read = bfin_mdiobus_read; | |
1517 | miibus->write = bfin_mdiobus_write; | |
1518 | miibus->reset = bfin_mdiobus_reset; | |
1519 | ||
1520 | miibus->parent = &pdev->dev; | |
1521 | miibus->name = "bfin_mii_bus"; | |
1522 | snprintf(miibus->id, MII_BUS_ID_SIZE, "0"); | |
1523 | miibus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL); | |
1524 | if (miibus->irq == NULL) | |
1525 | goto out_err_alloc; | |
1526 | for (i = 0; i < PHY_MAX_ADDR; ++i) | |
1527 | miibus->irq[i] = PHY_POLL; | |
1528 | ||
1529 | rc = mdiobus_register(miibus); | |
1530 | if (rc) { | |
1531 | dev_err(&pdev->dev, "Cannot register MDIO bus!\n"); | |
1532 | goto out_err_mdiobus_register; | |
1533 | } | |
1534 | ||
1535 | platform_set_drvdata(pdev, miibus); | |
1536 | return 0; | |
1537 | ||
1538 | out_err_mdiobus_register: | |
1539 | mdiobus_free(miibus); | |
1540 | out_err_alloc: | |
1541 | peripheral_free_list(pin_req); | |
1542 | ||
1543 | return rc; | |
1544 | } | |
1545 | ||
1546 | static int __devexit bfin_mii_bus_remove(struct platform_device *pdev) | |
1547 | { | |
1548 | struct mii_bus *miibus = platform_get_drvdata(pdev); | |
1549 | platform_set_drvdata(pdev, NULL); | |
1550 | mdiobus_unregister(miibus); | |
1551 | mdiobus_free(miibus); | |
1552 | peripheral_free_list(pin_req); | |
1553 | return 0; | |
1554 | } | |
1555 | ||
1556 | static struct platform_driver bfin_mii_bus_driver = { | |
1557 | .probe = bfin_mii_bus_probe, | |
1558 | .remove = __devexit_p(bfin_mii_bus_remove), | |
1559 | .driver = { | |
1560 | .name = "bfin_mii_bus", | |
1561 | .owner = THIS_MODULE, | |
1562 | }, | |
1563 | }; | |
1564 | ||
e190d6b1 BW |
1565 | static struct platform_driver bfin_mac_driver = { |
1566 | .probe = bfin_mac_probe, | |
d7b843d3 | 1567 | .remove = __devexit_p(bfin_mac_remove), |
e190d6b1 BW |
1568 | .resume = bfin_mac_resume, |
1569 | .suspend = bfin_mac_suspend, | |
1570 | .driver = { | |
72abb461 KS |
1571 | .name = DRV_NAME, |
1572 | .owner = THIS_MODULE, | |
1573 | }, | |
e190d6b1 BW |
1574 | }; |
1575 | ||
1576 | static int __init bfin_mac_init(void) | |
1577 | { | |
080c8255 GY |
1578 | int ret; |
1579 | ret = platform_driver_register(&bfin_mii_bus_driver); | |
1580 | if (!ret) | |
1581 | return platform_driver_register(&bfin_mac_driver); | |
1582 | return -ENODEV; | |
e190d6b1 BW |
1583 | } |
1584 | ||
1585 | module_init(bfin_mac_init); | |
1586 | ||
1587 | static void __exit bfin_mac_cleanup(void) | |
1588 | { | |
1589 | platform_driver_unregister(&bfin_mac_driver); | |
080c8255 | 1590 | platform_driver_unregister(&bfin_mii_bus_driver); |
e190d6b1 BW |
1591 | } |
1592 | ||
1593 | module_exit(bfin_mac_cleanup); | |
72abb461 | 1594 |