Commit | Line | Data |
---|---|---|
e190d6b1 BW |
1 | /* |
2 | * File: drivers/net/bfin_mac.c | |
3 | * Based on: | |
4 | * Maintainer: | |
5 | * Bryan Wu <bryan.wu@analog.com> | |
6 | * | |
7 | * Original author: | |
8 | * Luke Yang <luke.yang@analog.com> | |
9 | * | |
10 | * Created: | |
11 | * Description: | |
12 | * | |
13 | * Modified: | |
14 | * Copyright 2004-2006 Analog Devices Inc. | |
15 | * | |
16 | * Bugs: Enter bugs at http://blackfin.uclinux.org/ | |
17 | * | |
18 | * This program is free software ; you can redistribute it and/or modify | |
19 | * it under the terms of the GNU General Public License as published by | |
20 | * the Free Software Foundation ; either version 2, or (at your option) | |
21 | * any later version. | |
22 | * | |
23 | * This program is distributed in the hope that it will be useful, | |
24 | * but WITHOUT ANY WARRANTY ; without even the implied warranty of | |
25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
26 | * GNU General Public License for more details. | |
27 | * | |
28 | * You should have received a copy of the GNU General Public License | |
29 | * along with this program ; see the file COPYING. | |
30 | * If not, write to the Free Software Foundation, | |
31 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
32 | */ | |
33 | ||
34 | #include <linux/init.h> | |
35 | #include <linux/module.h> | |
36 | #include <linux/kernel.h> | |
37 | #include <linux/sched.h> | |
38 | #include <linux/slab.h> | |
39 | #include <linux/delay.h> | |
40 | #include <linux/timer.h> | |
41 | #include <linux/errno.h> | |
42 | #include <linux/irq.h> | |
43 | #include <linux/io.h> | |
44 | #include <linux/ioport.h> | |
45 | #include <linux/crc32.h> | |
46 | #include <linux/device.h> | |
47 | #include <linux/spinlock.h> | |
48 | #include <linux/ethtool.h> | |
49 | #include <linux/mii.h> | |
e190d6b1 BW |
50 | #include <linux/netdevice.h> |
51 | #include <linux/etherdevice.h> | |
52 | #include <linux/skbuff.h> | |
e190d6b1 | 53 | #include <linux/platform_device.h> |
e190d6b1 BW |
54 | |
55 | #include <asm/dma.h> | |
56 | #include <linux/dma-mapping.h> | |
57 | ||
58 | #include <asm/blackfin.h> | |
59 | #include <asm/cacheflush.h> | |
60 | #include <asm/portmux.h> | |
61 | ||
62 | #include "bfin_mac.h" | |
63 | ||
64 | #define DRV_NAME "bfin_mac" | |
65 | #define DRV_VERSION "1.1" | |
66 | #define DRV_AUTHOR "Bryan Wu, Luke Yang" | |
67 | #define DRV_DESC "Blackfin BF53[67] on-chip Ethernet MAC driver" | |
68 | ||
69 | MODULE_AUTHOR(DRV_AUTHOR); | |
70 | MODULE_LICENSE("GPL"); | |
71 | MODULE_DESCRIPTION(DRV_DESC); | |
72 | ||
73 | #if defined(CONFIG_BFIN_MAC_USE_L1) | |
74 | # define bfin_mac_alloc(dma_handle, size) l1_data_sram_zalloc(size) | |
75 | # define bfin_mac_free(dma_handle, ptr) l1_data_sram_free(ptr) | |
76 | #else | |
77 | # define bfin_mac_alloc(dma_handle, size) \ | |
78 | dma_alloc_coherent(NULL, size, dma_handle, GFP_KERNEL) | |
79 | # define bfin_mac_free(dma_handle, ptr) \ | |
80 | dma_free_coherent(NULL, sizeof(*ptr), ptr, dma_handle) | |
81 | #endif | |
82 | ||
83 | #define PKT_BUF_SZ 1580 | |
84 | ||
85 | #define MAX_TIMEOUT_CNT 500 | |
86 | ||
87 | /* pointers to maintain transmit list */ | |
88 | static struct net_dma_desc_tx *tx_list_head; | |
89 | static struct net_dma_desc_tx *tx_list_tail; | |
90 | static struct net_dma_desc_rx *rx_list_head; | |
91 | static struct net_dma_desc_rx *rx_list_tail; | |
92 | static struct net_dma_desc_rx *current_rx_ptr; | |
93 | static struct net_dma_desc_tx *current_tx_ptr; | |
94 | static struct net_dma_desc_tx *tx_desc; | |
95 | static struct net_dma_desc_rx *rx_desc; | |
96 | ||
97 | static void desc_list_free(void) | |
98 | { | |
99 | struct net_dma_desc_rx *r; | |
100 | struct net_dma_desc_tx *t; | |
101 | int i; | |
102 | #if !defined(CONFIG_BFIN_MAC_USE_L1) | |
103 | dma_addr_t dma_handle = 0; | |
104 | #endif | |
105 | ||
106 | if (tx_desc) { | |
107 | t = tx_list_head; | |
108 | for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) { | |
109 | if (t) { | |
110 | if (t->skb) { | |
111 | dev_kfree_skb(t->skb); | |
112 | t->skb = NULL; | |
113 | } | |
114 | t = t->next; | |
115 | } | |
116 | } | |
117 | bfin_mac_free(dma_handle, tx_desc); | |
118 | } | |
119 | ||
120 | if (rx_desc) { | |
121 | r = rx_list_head; | |
122 | for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) { | |
123 | if (r) { | |
124 | if (r->skb) { | |
125 | dev_kfree_skb(r->skb); | |
126 | r->skb = NULL; | |
127 | } | |
128 | r = r->next; | |
129 | } | |
130 | } | |
131 | bfin_mac_free(dma_handle, rx_desc); | |
132 | } | |
133 | } | |
134 | ||
135 | static int desc_list_init(void) | |
136 | { | |
137 | int i; | |
138 | struct sk_buff *new_skb; | |
139 | #if !defined(CONFIG_BFIN_MAC_USE_L1) | |
140 | /* | |
141 | * This dma_handle is useless in Blackfin dma_alloc_coherent(). | |
142 | * The real dma handler is the return value of dma_alloc_coherent(). | |
143 | */ | |
144 | dma_addr_t dma_handle; | |
145 | #endif | |
146 | ||
147 | tx_desc = bfin_mac_alloc(&dma_handle, | |
148 | sizeof(struct net_dma_desc_tx) * | |
149 | CONFIG_BFIN_TX_DESC_NUM); | |
150 | if (tx_desc == NULL) | |
151 | goto init_error; | |
152 | ||
153 | rx_desc = bfin_mac_alloc(&dma_handle, | |
154 | sizeof(struct net_dma_desc_rx) * | |
155 | CONFIG_BFIN_RX_DESC_NUM); | |
156 | if (rx_desc == NULL) | |
157 | goto init_error; | |
158 | ||
159 | /* init tx_list */ | |
160 | tx_list_head = tx_list_tail = tx_desc; | |
161 | ||
162 | for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) { | |
163 | struct net_dma_desc_tx *t = tx_desc + i; | |
164 | struct dma_descriptor *a = &(t->desc_a); | |
165 | struct dma_descriptor *b = &(t->desc_b); | |
166 | ||
167 | /* | |
168 | * disable DMA | |
169 | * read from memory WNR = 0 | |
170 | * wordsize is 32 bits | |
171 | * 6 half words is desc size | |
172 | * large desc flow | |
173 | */ | |
174 | a->config = WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE; | |
175 | a->start_addr = (unsigned long)t->packet; | |
176 | a->x_count = 0; | |
177 | a->next_dma_desc = b; | |
178 | ||
179 | /* | |
180 | * enabled DMA | |
181 | * write to memory WNR = 1 | |
182 | * wordsize is 32 bits | |
183 | * disable interrupt | |
184 | * 6 half words is desc size | |
185 | * large desc flow | |
186 | */ | |
187 | b->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE; | |
188 | b->start_addr = (unsigned long)(&(t->status)); | |
189 | b->x_count = 0; | |
190 | ||
191 | t->skb = NULL; | |
192 | tx_list_tail->desc_b.next_dma_desc = a; | |
193 | tx_list_tail->next = t; | |
194 | tx_list_tail = t; | |
195 | } | |
196 | tx_list_tail->next = tx_list_head; /* tx_list is a circle */ | |
197 | tx_list_tail->desc_b.next_dma_desc = &(tx_list_head->desc_a); | |
198 | current_tx_ptr = tx_list_head; | |
199 | ||
200 | /* init rx_list */ | |
201 | rx_list_head = rx_list_tail = rx_desc; | |
202 | ||
203 | for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) { | |
204 | struct net_dma_desc_rx *r = rx_desc + i; | |
205 | struct dma_descriptor *a = &(r->desc_a); | |
206 | struct dma_descriptor *b = &(r->desc_b); | |
207 | ||
208 | /* allocate a new skb for next time receive */ | |
209 | new_skb = dev_alloc_skb(PKT_BUF_SZ + 2); | |
210 | if (!new_skb) { | |
211 | printk(KERN_NOTICE DRV_NAME | |
212 | ": init: low on mem - packet dropped\n"); | |
213 | goto init_error; | |
214 | } | |
215 | skb_reserve(new_skb, 2); | |
216 | r->skb = new_skb; | |
217 | ||
218 | /* | |
219 | * enabled DMA | |
220 | * write to memory WNR = 1 | |
221 | * wordsize is 32 bits | |
222 | * disable interrupt | |
223 | * 6 half words is desc size | |
224 | * large desc flow | |
225 | */ | |
226 | a->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE; | |
227 | /* since RXDWA is enabled */ | |
228 | a->start_addr = (unsigned long)new_skb->data - 2; | |
229 | a->x_count = 0; | |
230 | a->next_dma_desc = b; | |
231 | ||
232 | /* | |
233 | * enabled DMA | |
234 | * write to memory WNR = 1 | |
235 | * wordsize is 32 bits | |
236 | * enable interrupt | |
237 | * 6 half words is desc size | |
238 | * large desc flow | |
239 | */ | |
240 | b->config = DMAEN | WNR | WDSIZE_32 | DI_EN | | |
241 | NDSIZE_6 | DMAFLOW_LARGE; | |
242 | b->start_addr = (unsigned long)(&(r->status)); | |
243 | b->x_count = 0; | |
244 | ||
245 | rx_list_tail->desc_b.next_dma_desc = a; | |
246 | rx_list_tail->next = r; | |
247 | rx_list_tail = r; | |
248 | } | |
249 | rx_list_tail->next = rx_list_head; /* rx_list is a circle */ | |
250 | rx_list_tail->desc_b.next_dma_desc = &(rx_list_head->desc_a); | |
251 | current_rx_ptr = rx_list_head; | |
252 | ||
253 | return 0; | |
254 | ||
255 | init_error: | |
256 | desc_list_free(); | |
257 | printk(KERN_ERR DRV_NAME ": kmalloc failed\n"); | |
258 | return -ENOMEM; | |
259 | } | |
260 | ||
261 | ||
262 | /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/ | |
263 | ||
264 | /* Set FER regs to MUX in Ethernet pins */ | |
265 | static int setup_pin_mux(int action) | |
266 | { | |
267 | #if defined(CONFIG_BFIN_MAC_RMII) | |
268 | u16 pin_req[] = P_RMII0; | |
269 | #else | |
270 | u16 pin_req[] = P_MII0; | |
271 | #endif | |
272 | ||
273 | if (action) { | |
274 | if (peripheral_request_list(pin_req, DRV_NAME)) { | |
275 | printk(KERN_ERR DRV_NAME | |
276 | ": Requesting Peripherals failed\n"); | |
277 | return -EFAULT; | |
278 | } | |
279 | } else | |
280 | peripheral_free_list(pin_req); | |
281 | ||
282 | return 0; | |
283 | } | |
284 | ||
285 | /* Wait until the previous MDC/MDIO transaction has completed */ | |
286 | static void poll_mdc_done(void) | |
287 | { | |
288 | int timeout_cnt = MAX_TIMEOUT_CNT; | |
289 | ||
290 | /* poll the STABUSY bit */ | |
291 | while ((bfin_read_EMAC_STAADD()) & STABUSY) { | |
292 | mdelay(10); | |
293 | if (timeout_cnt-- < 0) { | |
294 | printk(KERN_ERR DRV_NAME | |
295 | ": wait MDC/MDIO transaction to complete timeout\n"); | |
296 | break; | |
297 | } | |
298 | } | |
299 | } | |
300 | ||
301 | /* Read an off-chip register in a PHY through the MDC/MDIO port */ | |
302 | static u16 read_phy_reg(u16 PHYAddr, u16 RegAddr) | |
303 | { | |
304 | poll_mdc_done(); | |
305 | /* read mode */ | |
306 | bfin_write_EMAC_STAADD(SET_PHYAD(PHYAddr) | | |
307 | SET_REGAD(RegAddr) | | |
308 | STABUSY); | |
309 | poll_mdc_done(); | |
310 | ||
311 | return (u16) bfin_read_EMAC_STADAT(); | |
312 | } | |
313 | ||
314 | /* Write an off-chip register in a PHY through the MDC/MDIO port */ | |
315 | static void raw_write_phy_reg(u16 PHYAddr, u16 RegAddr, u32 Data) | |
316 | { | |
317 | bfin_write_EMAC_STADAT(Data); | |
318 | ||
319 | /* write mode */ | |
320 | bfin_write_EMAC_STAADD(SET_PHYAD(PHYAddr) | | |
321 | SET_REGAD(RegAddr) | | |
322 | STAOP | | |
323 | STABUSY); | |
324 | ||
325 | poll_mdc_done(); | |
326 | } | |
327 | ||
328 | static void write_phy_reg(u16 PHYAddr, u16 RegAddr, u32 Data) | |
329 | { | |
330 | poll_mdc_done(); | |
331 | raw_write_phy_reg(PHYAddr, RegAddr, Data); | |
332 | } | |
333 | ||
334 | /* set up the phy */ | |
335 | static void bf537mac_setphy(struct net_device *dev) | |
336 | { | |
337 | u16 phydat; | |
338 | struct bf537mac_local *lp = netdev_priv(dev); | |
339 | ||
340 | /* Program PHY registers */ | |
341 | pr_debug("start setting up phy\n"); | |
342 | ||
343 | /* issue a reset */ | |
344 | raw_write_phy_reg(lp->PhyAddr, PHYREG_MODECTL, 0x8000); | |
345 | ||
346 | /* wait half a second */ | |
347 | msleep(500); | |
348 | ||
349 | phydat = read_phy_reg(lp->PhyAddr, PHYREG_MODECTL); | |
350 | ||
351 | /* advertise flow control supported */ | |
352 | phydat = read_phy_reg(lp->PhyAddr, PHYREG_ANAR); | |
353 | phydat |= (1 << 10); | |
354 | write_phy_reg(lp->PhyAddr, PHYREG_ANAR, phydat); | |
355 | ||
356 | phydat = 0; | |
357 | if (lp->Negotiate) | |
358 | phydat |= 0x1000; /* enable auto negotiation */ | |
359 | else { | |
360 | if (lp->FullDuplex) | |
361 | phydat |= (1 << 8); /* full duplex */ | |
362 | else | |
363 | phydat &= (~(1 << 8)); /* half duplex */ | |
364 | ||
365 | if (!lp->Port10) | |
366 | phydat |= (1 << 13); /* 100 Mbps */ | |
367 | else | |
368 | phydat &= (~(1 << 13)); /* 10 Mbps */ | |
369 | } | |
370 | ||
371 | if (lp->Loopback) | |
372 | phydat |= (1 << 14); /* enable TX->RX loopback */ | |
373 | ||
374 | write_phy_reg(lp->PhyAddr, PHYREG_MODECTL, phydat); | |
375 | msleep(500); | |
376 | ||
377 | phydat = read_phy_reg(lp->PhyAddr, PHYREG_MODECTL); | |
378 | /* check for SMSC PHY */ | |
379 | if ((read_phy_reg(lp->PhyAddr, PHYREG_PHYID1) == 0x7) && | |
380 | ((read_phy_reg(lp->PhyAddr, PHYREG_PHYID2) & 0xfff0) == 0xC0A0)) { | |
381 | /* | |
382 | * we have SMSC PHY so reqest interrupt | |
383 | * on link down condition | |
384 | */ | |
385 | ||
386 | /* enable interrupts */ | |
387 | write_phy_reg(lp->PhyAddr, 30, 0x0ff); | |
388 | } | |
389 | } | |
390 | ||
391 | /**************************************************************************/ | |
392 | void setup_system_regs(struct net_device *dev) | |
393 | { | |
394 | int phyaddr; | |
395 | unsigned short sysctl, phydat; | |
396 | u32 opmode; | |
397 | struct bf537mac_local *lp = netdev_priv(dev); | |
398 | int count = 0; | |
399 | ||
400 | phyaddr = lp->PhyAddr; | |
401 | ||
402 | /* Enable PHY output */ | |
403 | if (!(bfin_read_VR_CTL() & PHYCLKOE)) | |
404 | bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE); | |
405 | ||
406 | /* MDC = 2.5 MHz */ | |
407 | sysctl = SET_MDCDIV(24); | |
408 | /* Odd word alignment for Receive Frame DMA word */ | |
409 | /* Configure checksum support and rcve frame word alignment */ | |
410 | #if defined(BFIN_MAC_CSUM_OFFLOAD) | |
411 | sysctl |= RXDWA | RXCKS; | |
412 | #else | |
413 | sysctl |= RXDWA; | |
414 | #endif | |
415 | bfin_write_EMAC_SYSCTL(sysctl); | |
416 | /* auto negotiation on */ | |
417 | /* full duplex */ | |
418 | /* 100 Mbps */ | |
419 | phydat = PHY_ANEG_EN | PHY_DUPLEX | PHY_SPD_SET; | |
420 | write_phy_reg(phyaddr, PHYREG_MODECTL, phydat); | |
421 | ||
422 | /* test if full duplex supported */ | |
423 | do { | |
424 | msleep(100); | |
425 | phydat = read_phy_reg(phyaddr, PHYREG_MODESTAT); | |
426 | if (count > 30) { | |
427 | printk(KERN_NOTICE DRV_NAME ": Link is down\n"); | |
428 | printk(KERN_NOTICE DRV_NAME | |
429 | "please check your network connection\n"); | |
430 | break; | |
431 | } | |
432 | count++; | |
433 | } while (!(phydat & 0x0004)); | |
434 | ||
435 | phydat = read_phy_reg(phyaddr, PHYREG_ANLPAR); | |
436 | ||
437 | if ((phydat & 0x0100) || (phydat & 0x0040)) { | |
438 | opmode = FDMODE; | |
439 | } else { | |
440 | opmode = 0; | |
441 | printk(KERN_INFO DRV_NAME | |
442 | ": Network is set to half duplex\n"); | |
443 | } | |
444 | ||
445 | #if defined(CONFIG_BFIN_MAC_RMII) | |
446 | opmode |= RMII; /* For Now only 100MBit are supported */ | |
447 | #endif | |
448 | ||
449 | bfin_write_EMAC_OPMODE(opmode); | |
450 | ||
451 | bfin_write_EMAC_MMC_CTL(RSTC | CROLL); | |
452 | ||
453 | /* Initialize the TX DMA channel registers */ | |
454 | bfin_write_DMA2_X_COUNT(0); | |
455 | bfin_write_DMA2_X_MODIFY(4); | |
456 | bfin_write_DMA2_Y_COUNT(0); | |
457 | bfin_write_DMA2_Y_MODIFY(0); | |
458 | ||
459 | /* Initialize the RX DMA channel registers */ | |
460 | bfin_write_DMA1_X_COUNT(0); | |
461 | bfin_write_DMA1_X_MODIFY(4); | |
462 | bfin_write_DMA1_Y_COUNT(0); | |
463 | bfin_write_DMA1_Y_MODIFY(0); | |
464 | } | |
465 | ||
73f83182 | 466 | static void setup_mac_addr(u8 *mac_addr) |
e190d6b1 BW |
467 | { |
468 | u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]); | |
469 | u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]); | |
470 | ||
471 | /* this depends on a little-endian machine */ | |
472 | bfin_write_EMAC_ADDRLO(addr_low); | |
473 | bfin_write_EMAC_ADDRHI(addr_hi); | |
474 | } | |
475 | ||
73f83182 AL |
476 | static int bf537mac_set_mac_address(struct net_device *dev, void *p) |
477 | { | |
478 | struct sockaddr *addr = p; | |
479 | if (netif_running(dev)) | |
480 | return -EBUSY; | |
481 | memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); | |
482 | setup_mac_addr(dev->dev_addr); | |
483 | return 0; | |
484 | } | |
485 | ||
e190d6b1 BW |
486 | static void adjust_tx_list(void) |
487 | { | |
488 | int timeout_cnt = MAX_TIMEOUT_CNT; | |
489 | ||
490 | if (tx_list_head->status.status_word != 0 | |
491 | && current_tx_ptr != tx_list_head) { | |
492 | goto adjust_head; /* released something, just return; */ | |
493 | } | |
494 | ||
495 | /* | |
496 | * if nothing released, check wait condition | |
497 | * current's next can not be the head, | |
498 | * otherwise the dma will not stop as we want | |
499 | */ | |
500 | if (current_tx_ptr->next->next == tx_list_head) { | |
501 | while (tx_list_head->status.status_word == 0) { | |
502 | mdelay(10); | |
503 | if (tx_list_head->status.status_word != 0 | |
504 | || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) { | |
505 | goto adjust_head; | |
506 | } | |
507 | if (timeout_cnt-- < 0) { | |
508 | printk(KERN_ERR DRV_NAME | |
509 | ": wait for adjust tx list head timeout\n"); | |
510 | break; | |
511 | } | |
512 | } | |
513 | if (tx_list_head->status.status_word != 0) { | |
514 | goto adjust_head; | |
515 | } | |
516 | } | |
517 | ||
518 | return; | |
519 | ||
520 | adjust_head: | |
521 | do { | |
522 | tx_list_head->desc_a.config &= ~DMAEN; | |
523 | tx_list_head->status.status_word = 0; | |
524 | if (tx_list_head->skb) { | |
525 | dev_kfree_skb(tx_list_head->skb); | |
526 | tx_list_head->skb = NULL; | |
527 | } else { | |
528 | printk(KERN_ERR DRV_NAME | |
529 | ": no sk_buff in a transmitted frame!\n"); | |
530 | } | |
531 | tx_list_head = tx_list_head->next; | |
532 | } while (tx_list_head->status.status_word != 0 | |
533 | && current_tx_ptr != tx_list_head); | |
534 | return; | |
535 | ||
536 | } | |
537 | ||
538 | static int bf537mac_hard_start_xmit(struct sk_buff *skb, | |
539 | struct net_device *dev) | |
540 | { | |
541 | struct bf537mac_local *lp = netdev_priv(dev); | |
542 | unsigned int data; | |
543 | ||
544 | current_tx_ptr->skb = skb; | |
545 | ||
546 | /* | |
547 | * Is skb->data always 16-bit aligned? | |
548 | * Do we need to memcpy((char *)(tail->packet + 2), skb->data, len)? | |
549 | */ | |
550 | if ((((unsigned int)(skb->data)) & 0x02) == 2) { | |
551 | /* move skb->data to current_tx_ptr payload */ | |
552 | data = (unsigned int)(skb->data) - 2; | |
553 | *((unsigned short *)data) = (unsigned short)(skb->len); | |
554 | current_tx_ptr->desc_a.start_addr = (unsigned long)data; | |
555 | /* this is important! */ | |
556 | blackfin_dcache_flush_range(data, (data + (skb->len)) + 2); | |
557 | ||
558 | } else { | |
559 | *((unsigned short *)(current_tx_ptr->packet)) = | |
560 | (unsigned short)(skb->len); | |
561 | memcpy((char *)(current_tx_ptr->packet + 2), skb->data, | |
562 | (skb->len)); | |
563 | current_tx_ptr->desc_a.start_addr = | |
564 | (unsigned long)current_tx_ptr->packet; | |
565 | if (current_tx_ptr->status.status_word != 0) | |
566 | current_tx_ptr->status.status_word = 0; | |
567 | blackfin_dcache_flush_range((unsigned int)current_tx_ptr-> | |
568 | packet, | |
569 | (unsigned int)(current_tx_ptr-> | |
570 | packet + skb->len) + | |
571 | 2); | |
572 | } | |
573 | ||
574 | /* enable this packet's dma */ | |
575 | current_tx_ptr->desc_a.config |= DMAEN; | |
576 | ||
577 | /* tx dma is running, just return */ | |
578 | if (bfin_read_DMA2_IRQ_STATUS() & 0x08) | |
579 | goto out; | |
580 | ||
581 | /* tx dma is not running */ | |
582 | bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a)); | |
583 | /* dma enabled, read from memory, size is 6 */ | |
584 | bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config); | |
585 | /* Turn on the EMAC tx */ | |
586 | bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE); | |
587 | ||
588 | out: | |
589 | adjust_tx_list(); | |
590 | current_tx_ptr = current_tx_ptr->next; | |
591 | dev->trans_start = jiffies; | |
09f75cd7 JG |
592 | dev->stats.tx_packets++; |
593 | dev->stats.tx_bytes += (skb->len); | |
e190d6b1 BW |
594 | return 0; |
595 | } | |
596 | ||
597 | static void bf537mac_rx(struct net_device *dev) | |
598 | { | |
599 | struct sk_buff *skb, *new_skb; | |
600 | struct bf537mac_local *lp = netdev_priv(dev); | |
601 | unsigned short len; | |
602 | ||
603 | /* allocate a new skb for next time receive */ | |
604 | skb = current_rx_ptr->skb; | |
605 | new_skb = dev_alloc_skb(PKT_BUF_SZ + 2); | |
606 | if (!new_skb) { | |
607 | printk(KERN_NOTICE DRV_NAME | |
608 | ": rx: low on mem - packet dropped\n"); | |
09f75cd7 | 609 | dev->stats.rx_dropped++; |
e190d6b1 BW |
610 | goto out; |
611 | } | |
612 | /* reserve 2 bytes for RXDWA padding */ | |
613 | skb_reserve(new_skb, 2); | |
614 | current_rx_ptr->skb = new_skb; | |
615 | current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2; | |
616 | ||
617 | len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN); | |
618 | skb_put(skb, len); | |
619 | blackfin_dcache_invalidate_range((unsigned long)skb->head, | |
620 | (unsigned long)skb->tail); | |
621 | ||
622 | dev->last_rx = jiffies; | |
623 | skb->dev = dev; | |
624 | skb->protocol = eth_type_trans(skb, dev); | |
625 | #if defined(BFIN_MAC_CSUM_OFFLOAD) | |
626 | skb->csum = current_rx_ptr->status.ip_payload_csum; | |
627 | skb->ip_summed = CHECKSUM_PARTIAL; | |
628 | #endif | |
629 | ||
630 | netif_rx(skb); | |
09f75cd7 JG |
631 | dev->stats.rx_packets++; |
632 | dev->stats.rx_bytes += len; | |
e190d6b1 BW |
633 | current_rx_ptr->status.status_word = 0x00000000; |
634 | current_rx_ptr = current_rx_ptr->next; | |
635 | ||
636 | out: | |
637 | return; | |
638 | } | |
639 | ||
640 | /* interrupt routine to handle rx and error signal */ | |
641 | static irqreturn_t bf537mac_interrupt(int irq, void *dev_id) | |
642 | { | |
643 | struct net_device *dev = dev_id; | |
644 | int number = 0; | |
645 | ||
646 | get_one_packet: | |
647 | if (current_rx_ptr->status.status_word == 0) { | |
648 | /* no more new packet received */ | |
649 | if (number == 0) { | |
650 | if (current_rx_ptr->next->status.status_word != 0) { | |
651 | current_rx_ptr = current_rx_ptr->next; | |
652 | goto real_rx; | |
653 | } | |
654 | } | |
655 | bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() | | |
656 | DMA_DONE | DMA_ERR); | |
657 | return IRQ_HANDLED; | |
658 | } | |
659 | ||
660 | real_rx: | |
661 | bf537mac_rx(dev); | |
662 | number++; | |
663 | goto get_one_packet; | |
664 | } | |
665 | ||
666 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
667 | static void bf537mac_poll(struct net_device *dev) | |
668 | { | |
669 | disable_irq(IRQ_MAC_RX); | |
670 | bf537mac_interrupt(IRQ_MAC_RX, dev); | |
671 | enable_irq(IRQ_MAC_RX); | |
672 | } | |
673 | #endif /* CONFIG_NET_POLL_CONTROLLER */ | |
674 | ||
496a34c2 | 675 | static void bf537mac_disable(void) |
e190d6b1 BW |
676 | { |
677 | unsigned int opmode; | |
678 | ||
679 | opmode = bfin_read_EMAC_OPMODE(); | |
680 | opmode &= (~RE); | |
681 | opmode &= (~TE); | |
682 | /* Turn off the EMAC */ | |
683 | bfin_write_EMAC_OPMODE(opmode); | |
684 | } | |
685 | ||
686 | /* | |
687 | * Enable Interrupts, Receive, and Transmit | |
688 | */ | |
689 | static int bf537mac_enable(struct net_device *dev) | |
690 | { | |
691 | u32 opmode; | |
692 | ||
693 | pr_debug("%s: %s\n", dev->name, __FUNCTION__); | |
694 | ||
695 | /* Set RX DMA */ | |
696 | bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a)); | |
697 | bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config); | |
698 | ||
699 | /* Wait MII done */ | |
700 | poll_mdc_done(); | |
701 | ||
702 | /* We enable only RX here */ | |
703 | /* ASTP : Enable Automatic Pad Stripping | |
704 | PR : Promiscuous Mode for test | |
705 | PSF : Receive frames with total length less than 64 bytes. | |
706 | FDMODE : Full Duplex Mode | |
707 | LB : Internal Loopback for test | |
708 | RE : Receiver Enable */ | |
709 | opmode = bfin_read_EMAC_OPMODE(); | |
710 | if (opmode & FDMODE) | |
711 | opmode |= PSF; | |
712 | else | |
713 | opmode |= DRO | DC | PSF; | |
714 | opmode |= RE; | |
715 | ||
716 | #if defined(CONFIG_BFIN_MAC_RMII) | |
717 | opmode |= RMII; /* For Now only 100MBit are supported */ | |
718 | #ifdef CONFIG_BF_REV_0_2 | |
719 | opmode |= TE; | |
720 | #endif | |
721 | #endif | |
722 | /* Turn on the EMAC rx */ | |
723 | bfin_write_EMAC_OPMODE(opmode); | |
724 | ||
725 | return 0; | |
726 | } | |
727 | ||
728 | /* Our watchdog timed out. Called by the networking layer */ | |
729 | static void bf537mac_timeout(struct net_device *dev) | |
730 | { | |
731 | pr_debug("%s: %s\n", dev->name, __FUNCTION__); | |
732 | ||
496a34c2 | 733 | bf537mac_disable(); |
e190d6b1 BW |
734 | |
735 | /* reset tx queue */ | |
736 | tx_list_tail = tx_list_head->next; | |
737 | ||
738 | bf537mac_enable(dev); | |
739 | ||
740 | /* We can accept TX packets again */ | |
741 | dev->trans_start = jiffies; | |
742 | netif_wake_queue(dev); | |
743 | } | |
744 | ||
e190d6b1 BW |
745 | /* |
746 | * This routine will, depending on the values passed to it, | |
747 | * either make it accept multicast packets, go into | |
748 | * promiscuous mode (for TCPDUMP and cousins) or accept | |
749 | * a select set of multicast packets | |
750 | */ | |
751 | static void bf537mac_set_multicast_list(struct net_device *dev) | |
752 | { | |
753 | u32 sysctl; | |
754 | ||
755 | if (dev->flags & IFF_PROMISC) { | |
756 | printk(KERN_INFO "%s: set to promisc mode\n", dev->name); | |
757 | sysctl = bfin_read_EMAC_OPMODE(); | |
758 | sysctl |= RAF; | |
759 | bfin_write_EMAC_OPMODE(sysctl); | |
760 | } else if (dev->flags & IFF_ALLMULTI || dev->mc_count) { | |
761 | /* accept all multicast */ | |
762 | sysctl = bfin_read_EMAC_OPMODE(); | |
763 | sysctl |= PAM; | |
764 | bfin_write_EMAC_OPMODE(sysctl); | |
765 | } else { | |
766 | /* clear promisc or multicast mode */ | |
767 | sysctl = bfin_read_EMAC_OPMODE(); | |
768 | sysctl &= ~(RAF | PAM); | |
769 | bfin_write_EMAC_OPMODE(sysctl); | |
770 | } | |
771 | } | |
772 | ||
773 | /* | |
774 | * this puts the device in an inactive state | |
775 | */ | |
776 | static void bf537mac_shutdown(struct net_device *dev) | |
777 | { | |
778 | /* Turn off the EMAC */ | |
779 | bfin_write_EMAC_OPMODE(0x00000000); | |
780 | /* Turn off the EMAC RX DMA */ | |
781 | bfin_write_DMA1_CONFIG(0x0000); | |
782 | bfin_write_DMA2_CONFIG(0x0000); | |
783 | } | |
784 | ||
785 | /* | |
786 | * Open and Initialize the interface | |
787 | * | |
788 | * Set up everything, reset the card, etc.. | |
789 | */ | |
790 | static int bf537mac_open(struct net_device *dev) | |
791 | { | |
4af4b840 | 792 | int retval; |
e190d6b1 BW |
793 | pr_debug("%s: %s\n", dev->name, __FUNCTION__); |
794 | ||
795 | /* | |
796 | * Check that the address is valid. If its not, refuse | |
797 | * to bring the device up. The user must specify an | |
798 | * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx | |
799 | */ | |
800 | if (!is_valid_ether_addr(dev->dev_addr)) { | |
801 | printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n"); | |
802 | return -EINVAL; | |
803 | } | |
804 | ||
805 | /* initial rx and tx list */ | |
4af4b840 MH |
806 | retval = desc_list_init(); |
807 | ||
808 | if (retval) | |
809 | return retval; | |
e190d6b1 BW |
810 | |
811 | bf537mac_setphy(dev); | |
812 | setup_system_regs(dev); | |
496a34c2 | 813 | bf537mac_disable(); |
e190d6b1 BW |
814 | bf537mac_enable(dev); |
815 | ||
816 | pr_debug("hardware init finished\n"); | |
817 | netif_start_queue(dev); | |
818 | netif_carrier_on(dev); | |
819 | ||
820 | return 0; | |
821 | } | |
822 | ||
823 | /* | |
824 | * | |
825 | * this makes the board clean up everything that it can | |
826 | * and not talk to the outside world. Caused by | |
827 | * an 'ifconfig ethX down' | |
828 | */ | |
829 | static int bf537mac_close(struct net_device *dev) | |
830 | { | |
831 | pr_debug("%s: %s\n", dev->name, __FUNCTION__); | |
832 | ||
833 | netif_stop_queue(dev); | |
834 | netif_carrier_off(dev); | |
835 | ||
836 | /* clear everything */ | |
837 | bf537mac_shutdown(dev); | |
838 | ||
839 | /* free the rx/tx buffers */ | |
840 | desc_list_free(); | |
841 | ||
842 | return 0; | |
843 | } | |
844 | ||
845 | static int __init bf537mac_probe(struct net_device *dev) | |
846 | { | |
847 | struct bf537mac_local *lp = netdev_priv(dev); | |
848 | int retval; | |
849 | ||
850 | /* Grab the MAC address in the MAC */ | |
851 | *(__le32 *) (&(dev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO()); | |
852 | *(__le16 *) (&(dev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI()); | |
853 | ||
854 | /* probe mac */ | |
855 | /*todo: how to proble? which is revision_register */ | |
856 | bfin_write_EMAC_ADDRLO(0x12345678); | |
857 | if (bfin_read_EMAC_ADDRLO() != 0x12345678) { | |
858 | pr_debug("can't detect bf537 mac!\n"); | |
859 | retval = -ENODEV; | |
860 | goto err_out; | |
861 | } | |
862 | ||
863 | /* set the GPIO pins to Ethernet mode */ | |
864 | retval = setup_pin_mux(1); | |
865 | ||
866 | if (retval) | |
867 | return retval; | |
868 | ||
869 | /*Is it valid? (Did bootloader initialize it?) */ | |
870 | if (!is_valid_ether_addr(dev->dev_addr)) { | |
871 | /* Grab the MAC from the board somehow - this is done in the | |
872 | arch/blackfin/mach-bf537/boards/eth_mac.c */ | |
873 | get_bf537_ether_addr(dev->dev_addr); | |
874 | } | |
875 | ||
876 | /* If still not valid, get a random one */ | |
877 | if (!is_valid_ether_addr(dev->dev_addr)) { | |
878 | random_ether_addr(dev->dev_addr); | |
879 | } | |
880 | ||
881 | setup_mac_addr(dev->dev_addr); | |
882 | ||
883 | /* Fill in the fields of the device structure with ethernet values. */ | |
884 | ether_setup(dev); | |
885 | ||
886 | dev->open = bf537mac_open; | |
887 | dev->stop = bf537mac_close; | |
888 | dev->hard_start_xmit = bf537mac_hard_start_xmit; | |
73f83182 | 889 | dev->set_mac_address = bf537mac_set_mac_address; |
e190d6b1 | 890 | dev->tx_timeout = bf537mac_timeout; |
e190d6b1 BW |
891 | dev->set_multicast_list = bf537mac_set_multicast_list; |
892 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
893 | dev->poll_controller = bf537mac_poll; | |
894 | #endif | |
895 | ||
896 | /* fill in some of the fields */ | |
897 | lp->version = 1; | |
898 | lp->PhyAddr = 0x01; | |
899 | lp->CLKIN = 25; | |
900 | lp->FullDuplex = 0; | |
901 | lp->Negotiate = 1; | |
902 | lp->FlowControl = 0; | |
903 | spin_lock_init(&lp->lock); | |
904 | ||
905 | /* now, enable interrupts */ | |
906 | /* register irq handler */ | |
907 | if (request_irq | |
908 | (IRQ_MAC_RX, bf537mac_interrupt, IRQF_DISABLED | IRQF_SHARED, | |
909 | "BFIN537_MAC_RX", dev)) { | |
910 | printk(KERN_WARNING DRV_NAME | |
911 | ": Unable to attach BlackFin MAC RX interrupt\n"); | |
912 | return -EBUSY; | |
913 | } | |
914 | ||
915 | /* Enable PHY output early */ | |
916 | if (!(bfin_read_VR_CTL() & PHYCLKOE)) | |
917 | bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE); | |
918 | ||
919 | retval = register_netdev(dev); | |
920 | if (retval == 0) { | |
921 | /* now, print out the card info, in a short format.. */ | |
922 | printk(KERN_INFO "%s: Version %s, %s\n", | |
923 | DRV_NAME, DRV_VERSION, DRV_DESC); | |
924 | } | |
925 | ||
926 | err_out: | |
927 | return retval; | |
928 | } | |
929 | ||
930 | static int bfin_mac_probe(struct platform_device *pdev) | |
931 | { | |
932 | struct net_device *ndev; | |
933 | ||
934 | ndev = alloc_etherdev(sizeof(struct bf537mac_local)); | |
935 | if (!ndev) { | |
936 | printk(KERN_WARNING DRV_NAME ": could not allocate device\n"); | |
937 | return -ENOMEM; | |
938 | } | |
939 | ||
e190d6b1 BW |
940 | SET_NETDEV_DEV(ndev, &pdev->dev); |
941 | ||
942 | platform_set_drvdata(pdev, ndev); | |
943 | ||
944 | if (bf537mac_probe(ndev) != 0) { | |
945 | platform_set_drvdata(pdev, NULL); | |
946 | free_netdev(ndev); | |
947 | printk(KERN_WARNING DRV_NAME ": not found\n"); | |
948 | return -ENODEV; | |
949 | } | |
950 | ||
951 | return 0; | |
952 | } | |
953 | ||
954 | static int bfin_mac_remove(struct platform_device *pdev) | |
955 | { | |
956 | struct net_device *ndev = platform_get_drvdata(pdev); | |
957 | ||
958 | platform_set_drvdata(pdev, NULL); | |
959 | ||
960 | unregister_netdev(ndev); | |
961 | ||
962 | free_irq(IRQ_MAC_RX, ndev); | |
963 | ||
964 | free_netdev(ndev); | |
965 | ||
966 | setup_pin_mux(0); | |
967 | ||
968 | return 0; | |
969 | } | |
970 | ||
496a34c2 BW |
971 | #ifdef CONFIG_PM |
972 | static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg) | |
e190d6b1 | 973 | { |
496a34c2 BW |
974 | struct net_device *net_dev = platform_get_drvdata(pdev); |
975 | ||
976 | if (netif_running(net_dev)) | |
977 | bf537mac_close(net_dev); | |
978 | ||
e190d6b1 BW |
979 | return 0; |
980 | } | |
981 | ||
982 | static int bfin_mac_resume(struct platform_device *pdev) | |
983 | { | |
496a34c2 BW |
984 | struct net_device *net_dev = platform_get_drvdata(pdev); |
985 | ||
986 | if (netif_running(net_dev)) | |
987 | bf537mac_open(net_dev); | |
988 | ||
e190d6b1 BW |
989 | return 0; |
990 | } | |
496a34c2 BW |
991 | #else |
992 | #define bfin_mac_suspend NULL | |
993 | #define bfin_mac_resume NULL | |
994 | #endif /* CONFIG_PM */ | |
e190d6b1 BW |
995 | |
996 | static struct platform_driver bfin_mac_driver = { | |
997 | .probe = bfin_mac_probe, | |
998 | .remove = bfin_mac_remove, | |
999 | .resume = bfin_mac_resume, | |
1000 | .suspend = bfin_mac_suspend, | |
1001 | .driver = { | |
1002 | .name = DRV_NAME, | |
1003 | }, | |
1004 | }; | |
1005 | ||
1006 | static int __init bfin_mac_init(void) | |
1007 | { | |
1008 | return platform_driver_register(&bfin_mac_driver); | |
1009 | } | |
1010 | ||
1011 | module_init(bfin_mac_init); | |
1012 | ||
1013 | static void __exit bfin_mac_cleanup(void) | |
1014 | { | |
1015 | platform_driver_unregister(&bfin_mac_driver); | |
1016 | } | |
1017 | ||
1018 | module_exit(bfin_mac_cleanup); |