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 | ||
466 | void setup_mac_addr(u8 * mac_addr) | |
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 | ||
476 | static void adjust_tx_list(void) | |
477 | { | |
478 | int timeout_cnt = MAX_TIMEOUT_CNT; | |
479 | ||
480 | if (tx_list_head->status.status_word != 0 | |
481 | && current_tx_ptr != tx_list_head) { | |
482 | goto adjust_head; /* released something, just return; */ | |
483 | } | |
484 | ||
485 | /* | |
486 | * if nothing released, check wait condition | |
487 | * current's next can not be the head, | |
488 | * otherwise the dma will not stop as we want | |
489 | */ | |
490 | if (current_tx_ptr->next->next == tx_list_head) { | |
491 | while (tx_list_head->status.status_word == 0) { | |
492 | mdelay(10); | |
493 | if (tx_list_head->status.status_word != 0 | |
494 | || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) { | |
495 | goto adjust_head; | |
496 | } | |
497 | if (timeout_cnt-- < 0) { | |
498 | printk(KERN_ERR DRV_NAME | |
499 | ": wait for adjust tx list head timeout\n"); | |
500 | break; | |
501 | } | |
502 | } | |
503 | if (tx_list_head->status.status_word != 0) { | |
504 | goto adjust_head; | |
505 | } | |
506 | } | |
507 | ||
508 | return; | |
509 | ||
510 | adjust_head: | |
511 | do { | |
512 | tx_list_head->desc_a.config &= ~DMAEN; | |
513 | tx_list_head->status.status_word = 0; | |
514 | if (tx_list_head->skb) { | |
515 | dev_kfree_skb(tx_list_head->skb); | |
516 | tx_list_head->skb = NULL; | |
517 | } else { | |
518 | printk(KERN_ERR DRV_NAME | |
519 | ": no sk_buff in a transmitted frame!\n"); | |
520 | } | |
521 | tx_list_head = tx_list_head->next; | |
522 | } while (tx_list_head->status.status_word != 0 | |
523 | && current_tx_ptr != tx_list_head); | |
524 | return; | |
525 | ||
526 | } | |
527 | ||
528 | static int bf537mac_hard_start_xmit(struct sk_buff *skb, | |
529 | struct net_device *dev) | |
530 | { | |
531 | struct bf537mac_local *lp = netdev_priv(dev); | |
532 | unsigned int data; | |
533 | ||
534 | current_tx_ptr->skb = skb; | |
535 | ||
536 | /* | |
537 | * Is skb->data always 16-bit aligned? | |
538 | * Do we need to memcpy((char *)(tail->packet + 2), skb->data, len)? | |
539 | */ | |
540 | if ((((unsigned int)(skb->data)) & 0x02) == 2) { | |
541 | /* move skb->data to current_tx_ptr payload */ | |
542 | data = (unsigned int)(skb->data) - 2; | |
543 | *((unsigned short *)data) = (unsigned short)(skb->len); | |
544 | current_tx_ptr->desc_a.start_addr = (unsigned long)data; | |
545 | /* this is important! */ | |
546 | blackfin_dcache_flush_range(data, (data + (skb->len)) + 2); | |
547 | ||
548 | } else { | |
549 | *((unsigned short *)(current_tx_ptr->packet)) = | |
550 | (unsigned short)(skb->len); | |
551 | memcpy((char *)(current_tx_ptr->packet + 2), skb->data, | |
552 | (skb->len)); | |
553 | current_tx_ptr->desc_a.start_addr = | |
554 | (unsigned long)current_tx_ptr->packet; | |
555 | if (current_tx_ptr->status.status_word != 0) | |
556 | current_tx_ptr->status.status_word = 0; | |
557 | blackfin_dcache_flush_range((unsigned int)current_tx_ptr-> | |
558 | packet, | |
559 | (unsigned int)(current_tx_ptr-> | |
560 | packet + skb->len) + | |
561 | 2); | |
562 | } | |
563 | ||
564 | /* enable this packet's dma */ | |
565 | current_tx_ptr->desc_a.config |= DMAEN; | |
566 | ||
567 | /* tx dma is running, just return */ | |
568 | if (bfin_read_DMA2_IRQ_STATUS() & 0x08) | |
569 | goto out; | |
570 | ||
571 | /* tx dma is not running */ | |
572 | bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a)); | |
573 | /* dma enabled, read from memory, size is 6 */ | |
574 | bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config); | |
575 | /* Turn on the EMAC tx */ | |
576 | bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE); | |
577 | ||
578 | out: | |
579 | adjust_tx_list(); | |
580 | current_tx_ptr = current_tx_ptr->next; | |
581 | dev->trans_start = jiffies; | |
582 | lp->stats.tx_packets++; | |
583 | lp->stats.tx_bytes += (skb->len); | |
584 | return 0; | |
585 | } | |
586 | ||
587 | static void bf537mac_rx(struct net_device *dev) | |
588 | { | |
589 | struct sk_buff *skb, *new_skb; | |
590 | struct bf537mac_local *lp = netdev_priv(dev); | |
591 | unsigned short len; | |
592 | ||
593 | /* allocate a new skb for next time receive */ | |
594 | skb = current_rx_ptr->skb; | |
595 | new_skb = dev_alloc_skb(PKT_BUF_SZ + 2); | |
596 | if (!new_skb) { | |
597 | printk(KERN_NOTICE DRV_NAME | |
598 | ": rx: low on mem - packet dropped\n"); | |
599 | lp->stats.rx_dropped++; | |
600 | goto out; | |
601 | } | |
602 | /* reserve 2 bytes for RXDWA padding */ | |
603 | skb_reserve(new_skb, 2); | |
604 | current_rx_ptr->skb = new_skb; | |
605 | current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2; | |
606 | ||
607 | len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN); | |
608 | skb_put(skb, len); | |
609 | blackfin_dcache_invalidate_range((unsigned long)skb->head, | |
610 | (unsigned long)skb->tail); | |
611 | ||
612 | dev->last_rx = jiffies; | |
613 | skb->dev = dev; | |
614 | skb->protocol = eth_type_trans(skb, dev); | |
615 | #if defined(BFIN_MAC_CSUM_OFFLOAD) | |
616 | skb->csum = current_rx_ptr->status.ip_payload_csum; | |
617 | skb->ip_summed = CHECKSUM_PARTIAL; | |
618 | #endif | |
619 | ||
620 | netif_rx(skb); | |
621 | lp->stats.rx_packets++; | |
622 | lp->stats.rx_bytes += len; | |
623 | current_rx_ptr->status.status_word = 0x00000000; | |
624 | current_rx_ptr = current_rx_ptr->next; | |
625 | ||
626 | out: | |
627 | return; | |
628 | } | |
629 | ||
630 | /* interrupt routine to handle rx and error signal */ | |
631 | static irqreturn_t bf537mac_interrupt(int irq, void *dev_id) | |
632 | { | |
633 | struct net_device *dev = dev_id; | |
634 | int number = 0; | |
635 | ||
636 | get_one_packet: | |
637 | if (current_rx_ptr->status.status_word == 0) { | |
638 | /* no more new packet received */ | |
639 | if (number == 0) { | |
640 | if (current_rx_ptr->next->status.status_word != 0) { | |
641 | current_rx_ptr = current_rx_ptr->next; | |
642 | goto real_rx; | |
643 | } | |
644 | } | |
645 | bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() | | |
646 | DMA_DONE | DMA_ERR); | |
647 | return IRQ_HANDLED; | |
648 | } | |
649 | ||
650 | real_rx: | |
651 | bf537mac_rx(dev); | |
652 | number++; | |
653 | goto get_one_packet; | |
654 | } | |
655 | ||
656 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
657 | static void bf537mac_poll(struct net_device *dev) | |
658 | { | |
659 | disable_irq(IRQ_MAC_RX); | |
660 | bf537mac_interrupt(IRQ_MAC_RX, dev); | |
661 | enable_irq(IRQ_MAC_RX); | |
662 | } | |
663 | #endif /* CONFIG_NET_POLL_CONTROLLER */ | |
664 | ||
665 | static void bf537mac_reset(void) | |
666 | { | |
667 | unsigned int opmode; | |
668 | ||
669 | opmode = bfin_read_EMAC_OPMODE(); | |
670 | opmode &= (~RE); | |
671 | opmode &= (~TE); | |
672 | /* Turn off the EMAC */ | |
673 | bfin_write_EMAC_OPMODE(opmode); | |
674 | } | |
675 | ||
676 | /* | |
677 | * Enable Interrupts, Receive, and Transmit | |
678 | */ | |
679 | static int bf537mac_enable(struct net_device *dev) | |
680 | { | |
681 | u32 opmode; | |
682 | ||
683 | pr_debug("%s: %s\n", dev->name, __FUNCTION__); | |
684 | ||
685 | /* Set RX DMA */ | |
686 | bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a)); | |
687 | bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config); | |
688 | ||
689 | /* Wait MII done */ | |
690 | poll_mdc_done(); | |
691 | ||
692 | /* We enable only RX here */ | |
693 | /* ASTP : Enable Automatic Pad Stripping | |
694 | PR : Promiscuous Mode for test | |
695 | PSF : Receive frames with total length less than 64 bytes. | |
696 | FDMODE : Full Duplex Mode | |
697 | LB : Internal Loopback for test | |
698 | RE : Receiver Enable */ | |
699 | opmode = bfin_read_EMAC_OPMODE(); | |
700 | if (opmode & FDMODE) | |
701 | opmode |= PSF; | |
702 | else | |
703 | opmode |= DRO | DC | PSF; | |
704 | opmode |= RE; | |
705 | ||
706 | #if defined(CONFIG_BFIN_MAC_RMII) | |
707 | opmode |= RMII; /* For Now only 100MBit are supported */ | |
708 | #ifdef CONFIG_BF_REV_0_2 | |
709 | opmode |= TE; | |
710 | #endif | |
711 | #endif | |
712 | /* Turn on the EMAC rx */ | |
713 | bfin_write_EMAC_OPMODE(opmode); | |
714 | ||
715 | return 0; | |
716 | } | |
717 | ||
718 | /* Our watchdog timed out. Called by the networking layer */ | |
719 | static void bf537mac_timeout(struct net_device *dev) | |
720 | { | |
721 | pr_debug("%s: %s\n", dev->name, __FUNCTION__); | |
722 | ||
723 | bf537mac_reset(); | |
724 | ||
725 | /* reset tx queue */ | |
726 | tx_list_tail = tx_list_head->next; | |
727 | ||
728 | bf537mac_enable(dev); | |
729 | ||
730 | /* We can accept TX packets again */ | |
731 | dev->trans_start = jiffies; | |
732 | netif_wake_queue(dev); | |
733 | } | |
734 | ||
735 | /* | |
736 | * Get the current statistics. | |
737 | * This may be called with the card open or closed. | |
738 | */ | |
739 | static struct net_device_stats *bf537mac_query_statistics(struct net_device | |
740 | *dev) | |
741 | { | |
742 | struct bf537mac_local *lp = netdev_priv(dev); | |
743 | ||
744 | pr_debug("%s: %s\n", dev->name, __FUNCTION__); | |
745 | ||
746 | return &lp->stats; | |
747 | } | |
748 | ||
749 | /* | |
750 | * This routine will, depending on the values passed to it, | |
751 | * either make it accept multicast packets, go into | |
752 | * promiscuous mode (for TCPDUMP and cousins) or accept | |
753 | * a select set of multicast packets | |
754 | */ | |
755 | static void bf537mac_set_multicast_list(struct net_device *dev) | |
756 | { | |
757 | u32 sysctl; | |
758 | ||
759 | if (dev->flags & IFF_PROMISC) { | |
760 | printk(KERN_INFO "%s: set to promisc mode\n", dev->name); | |
761 | sysctl = bfin_read_EMAC_OPMODE(); | |
762 | sysctl |= RAF; | |
763 | bfin_write_EMAC_OPMODE(sysctl); | |
764 | } else if (dev->flags & IFF_ALLMULTI || dev->mc_count) { | |
765 | /* accept all multicast */ | |
766 | sysctl = bfin_read_EMAC_OPMODE(); | |
767 | sysctl |= PAM; | |
768 | bfin_write_EMAC_OPMODE(sysctl); | |
769 | } else { | |
770 | /* clear promisc or multicast mode */ | |
771 | sysctl = bfin_read_EMAC_OPMODE(); | |
772 | sysctl &= ~(RAF | PAM); | |
773 | bfin_write_EMAC_OPMODE(sysctl); | |
774 | } | |
775 | } | |
776 | ||
777 | /* | |
778 | * this puts the device in an inactive state | |
779 | */ | |
780 | static void bf537mac_shutdown(struct net_device *dev) | |
781 | { | |
782 | /* Turn off the EMAC */ | |
783 | bfin_write_EMAC_OPMODE(0x00000000); | |
784 | /* Turn off the EMAC RX DMA */ | |
785 | bfin_write_DMA1_CONFIG(0x0000); | |
786 | bfin_write_DMA2_CONFIG(0x0000); | |
787 | } | |
788 | ||
789 | /* | |
790 | * Open and Initialize the interface | |
791 | * | |
792 | * Set up everything, reset the card, etc.. | |
793 | */ | |
794 | static int bf537mac_open(struct net_device *dev) | |
795 | { | |
4af4b840 | 796 | int retval; |
e190d6b1 BW |
797 | pr_debug("%s: %s\n", dev->name, __FUNCTION__); |
798 | ||
799 | /* | |
800 | * Check that the address is valid. If its not, refuse | |
801 | * to bring the device up. The user must specify an | |
802 | * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx | |
803 | */ | |
804 | if (!is_valid_ether_addr(dev->dev_addr)) { | |
805 | printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n"); | |
806 | return -EINVAL; | |
807 | } | |
808 | ||
809 | /* initial rx and tx list */ | |
4af4b840 MH |
810 | retval = desc_list_init(); |
811 | ||
812 | if (retval) | |
813 | return retval; | |
e190d6b1 BW |
814 | |
815 | bf537mac_setphy(dev); | |
816 | setup_system_regs(dev); | |
817 | bf537mac_reset(); | |
818 | bf537mac_enable(dev); | |
819 | ||
820 | pr_debug("hardware init finished\n"); | |
821 | netif_start_queue(dev); | |
822 | netif_carrier_on(dev); | |
823 | ||
824 | return 0; | |
825 | } | |
826 | ||
827 | /* | |
828 | * | |
829 | * this makes the board clean up everything that it can | |
830 | * and not talk to the outside world. Caused by | |
831 | * an 'ifconfig ethX down' | |
832 | */ | |
833 | static int bf537mac_close(struct net_device *dev) | |
834 | { | |
835 | pr_debug("%s: %s\n", dev->name, __FUNCTION__); | |
836 | ||
837 | netif_stop_queue(dev); | |
838 | netif_carrier_off(dev); | |
839 | ||
840 | /* clear everything */ | |
841 | bf537mac_shutdown(dev); | |
842 | ||
843 | /* free the rx/tx buffers */ | |
844 | desc_list_free(); | |
845 | ||
846 | return 0; | |
847 | } | |
848 | ||
849 | static int __init bf537mac_probe(struct net_device *dev) | |
850 | { | |
851 | struct bf537mac_local *lp = netdev_priv(dev); | |
852 | int retval; | |
853 | ||
854 | /* Grab the MAC address in the MAC */ | |
855 | *(__le32 *) (&(dev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO()); | |
856 | *(__le16 *) (&(dev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI()); | |
857 | ||
858 | /* probe mac */ | |
859 | /*todo: how to proble? which is revision_register */ | |
860 | bfin_write_EMAC_ADDRLO(0x12345678); | |
861 | if (bfin_read_EMAC_ADDRLO() != 0x12345678) { | |
862 | pr_debug("can't detect bf537 mac!\n"); | |
863 | retval = -ENODEV; | |
864 | goto err_out; | |
865 | } | |
866 | ||
867 | /* set the GPIO pins to Ethernet mode */ | |
868 | retval = setup_pin_mux(1); | |
869 | ||
870 | if (retval) | |
871 | return retval; | |
872 | ||
873 | /*Is it valid? (Did bootloader initialize it?) */ | |
874 | if (!is_valid_ether_addr(dev->dev_addr)) { | |
875 | /* Grab the MAC from the board somehow - this is done in the | |
876 | arch/blackfin/mach-bf537/boards/eth_mac.c */ | |
877 | get_bf537_ether_addr(dev->dev_addr); | |
878 | } | |
879 | ||
880 | /* If still not valid, get a random one */ | |
881 | if (!is_valid_ether_addr(dev->dev_addr)) { | |
882 | random_ether_addr(dev->dev_addr); | |
883 | } | |
884 | ||
885 | setup_mac_addr(dev->dev_addr); | |
886 | ||
887 | /* Fill in the fields of the device structure with ethernet values. */ | |
888 | ether_setup(dev); | |
889 | ||
890 | dev->open = bf537mac_open; | |
891 | dev->stop = bf537mac_close; | |
892 | dev->hard_start_xmit = bf537mac_hard_start_xmit; | |
893 | dev->tx_timeout = bf537mac_timeout; | |
894 | dev->get_stats = bf537mac_query_statistics; | |
895 | dev->set_multicast_list = bf537mac_set_multicast_list; | |
896 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
897 | dev->poll_controller = bf537mac_poll; | |
898 | #endif | |
899 | ||
900 | /* fill in some of the fields */ | |
901 | lp->version = 1; | |
902 | lp->PhyAddr = 0x01; | |
903 | lp->CLKIN = 25; | |
904 | lp->FullDuplex = 0; | |
905 | lp->Negotiate = 1; | |
906 | lp->FlowControl = 0; | |
907 | spin_lock_init(&lp->lock); | |
908 | ||
909 | /* now, enable interrupts */ | |
910 | /* register irq handler */ | |
911 | if (request_irq | |
912 | (IRQ_MAC_RX, bf537mac_interrupt, IRQF_DISABLED | IRQF_SHARED, | |
913 | "BFIN537_MAC_RX", dev)) { | |
914 | printk(KERN_WARNING DRV_NAME | |
915 | ": Unable to attach BlackFin MAC RX interrupt\n"); | |
916 | return -EBUSY; | |
917 | } | |
918 | ||
919 | /* Enable PHY output early */ | |
920 | if (!(bfin_read_VR_CTL() & PHYCLKOE)) | |
921 | bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE); | |
922 | ||
923 | retval = register_netdev(dev); | |
924 | if (retval == 0) { | |
925 | /* now, print out the card info, in a short format.. */ | |
926 | printk(KERN_INFO "%s: Version %s, %s\n", | |
927 | DRV_NAME, DRV_VERSION, DRV_DESC); | |
928 | } | |
929 | ||
930 | err_out: | |
931 | return retval; | |
932 | } | |
933 | ||
934 | static int bfin_mac_probe(struct platform_device *pdev) | |
935 | { | |
936 | struct net_device *ndev; | |
937 | ||
938 | ndev = alloc_etherdev(sizeof(struct bf537mac_local)); | |
939 | if (!ndev) { | |
940 | printk(KERN_WARNING DRV_NAME ": could not allocate device\n"); | |
941 | return -ENOMEM; | |
942 | } | |
943 | ||
e190d6b1 BW |
944 | SET_NETDEV_DEV(ndev, &pdev->dev); |
945 | ||
946 | platform_set_drvdata(pdev, ndev); | |
947 | ||
948 | if (bf537mac_probe(ndev) != 0) { | |
949 | platform_set_drvdata(pdev, NULL); | |
950 | free_netdev(ndev); | |
951 | printk(KERN_WARNING DRV_NAME ": not found\n"); | |
952 | return -ENODEV; | |
953 | } | |
954 | ||
955 | return 0; | |
956 | } | |
957 | ||
958 | static int bfin_mac_remove(struct platform_device *pdev) | |
959 | { | |
960 | struct net_device *ndev = platform_get_drvdata(pdev); | |
961 | ||
962 | platform_set_drvdata(pdev, NULL); | |
963 | ||
964 | unregister_netdev(ndev); | |
965 | ||
966 | free_irq(IRQ_MAC_RX, ndev); | |
967 | ||
968 | free_netdev(ndev); | |
969 | ||
970 | setup_pin_mux(0); | |
971 | ||
972 | return 0; | |
973 | } | |
974 | ||
975 | static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t state) | |
976 | { | |
977 | return 0; | |
978 | } | |
979 | ||
980 | static int bfin_mac_resume(struct platform_device *pdev) | |
981 | { | |
982 | return 0; | |
983 | } | |
984 | ||
985 | static struct platform_driver bfin_mac_driver = { | |
986 | .probe = bfin_mac_probe, | |
987 | .remove = bfin_mac_remove, | |
988 | .resume = bfin_mac_resume, | |
989 | .suspend = bfin_mac_suspend, | |
990 | .driver = { | |
991 | .name = DRV_NAME, | |
992 | }, | |
993 | }; | |
994 | ||
995 | static int __init bfin_mac_init(void) | |
996 | { | |
997 | return platform_driver_register(&bfin_mac_driver); | |
998 | } | |
999 | ||
1000 | module_init(bfin_mac_init); | |
1001 | ||
1002 | static void __exit bfin_mac_cleanup(void) | |
1003 | { | |
1004 | platform_driver_unregister(&bfin_mac_driver); | |
1005 | } | |
1006 | ||
1007 | module_exit(bfin_mac_cleanup); |