2 * drivers/net/ethernet/ec_bhf.c
4 * Copyright (C) 2014 Darek Marcinkiewicz <reksio@newterm.pl>
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 /* This is a driver for EtherCAT master module present on CCAT FPGA.
18 * Those can be found on Bechhoff CX50xx industrial PCs.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/pci.h>
25 #include <linux/init.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/hrtimer.h>
32 #include <linux/interrupt.h>
33 #include <linux/stat.h>
35 #define TIMER_INTERVAL_NSEC 20000
37 #define INFO_BLOCK_SIZE 0x10
38 #define INFO_BLOCK_TYPE 0x0
39 #define INFO_BLOCK_REV 0x2
40 #define INFO_BLOCK_BLK_CNT 0x4
41 #define INFO_BLOCK_TX_CHAN 0x4
42 #define INFO_BLOCK_RX_CHAN 0x5
43 #define INFO_BLOCK_OFFSET 0x8
45 #define EC_MII_OFFSET 0x4
46 #define EC_FIFO_OFFSET 0x8
47 #define EC_MAC_OFFSET 0xc
49 #define MAC_FRAME_ERR_CNT 0x0
50 #define MAC_RX_ERR_CNT 0x1
51 #define MAC_CRC_ERR_CNT 0x2
52 #define MAC_LNK_LST_ERR_CNT 0x3
53 #define MAC_TX_FRAME_CNT 0x10
54 #define MAC_RX_FRAME_CNT 0x14
55 #define MAC_TX_FIFO_LVL 0x20
56 #define MAC_DROPPED_FRMS 0x28
57 #define MAC_CONNECTED_CCAT_FLAG 0x78
59 #define MII_MAC_ADDR 0x8
60 #define MII_MAC_FILT_FLAG 0xe
61 #define MII_LINK_STATUS 0xf
63 #define FIFO_TX_REG 0x0
64 #define FIFO_TX_RESET 0x8
65 #define FIFO_RX_REG 0x10
66 #define FIFO_RX_ADDR_VALID (1u << 31)
67 #define FIFO_RX_RESET 0x18
69 #define DMA_CHAN_OFFSET 0x1000
70 #define DMA_CHAN_SIZE 0x8
72 #define DMA_WINDOW_SIZE_MASK 0xfffffffc
74 #define ETHERCAT_MASTER_ID 0x14
76 static const struct pci_device_id ids
[] = {
77 { PCI_DEVICE(0x15ec, 0x5000), },
80 MODULE_DEVICE_TABLE(pci
, ids
);
83 #define RXHDR_NEXT_ADDR_MASK 0xffffffu
84 #define RXHDR_NEXT_VALID (1u << 31)
86 #define RXHDR_NEXT_RECV_FLAG 0x1
88 #define RXHDR_LEN_MASK 0xfffu
95 #define PKT_PAYLOAD_SIZE 0x7e8
97 struct rx_header header
;
98 u8 data
[PKT_PAYLOAD_SIZE
];
103 #define TX_HDR_PORT_0 0x1
104 #define TX_HDR_PORT_1 0x2
107 #define TX_HDR_SENT 0x1
113 struct tx_header header
;
114 u8 data
[PKT_PAYLOAD_SIZE
];
119 static long polling_frequency
= TIMER_INTERVAL_NSEC
;
128 dma_addr_t alloc_phys
;
132 struct net_device
*net_dev
;
136 void __iomem
*dma_io
;
138 struct hrtimer hrtimer
;
143 void __iomem
*fifo_io
;
144 void __iomem
*mii_io
;
145 void __iomem
*mac_io
;
147 struct bhf_dma rx_buf
;
148 struct rx_desc
*rx_descs
;
152 struct bhf_dma tx_buf
;
153 struct tx_desc
*tx_descs
;
161 #define PRIV_TO_DEV(priv) (&(priv)->dev->dev)
163 static void ec_bhf_reset(struct ec_bhf_priv
*priv
)
165 iowrite8(0, priv
->mac_io
+ MAC_FRAME_ERR_CNT
);
166 iowrite8(0, priv
->mac_io
+ MAC_RX_ERR_CNT
);
167 iowrite8(0, priv
->mac_io
+ MAC_CRC_ERR_CNT
);
168 iowrite8(0, priv
->mac_io
+ MAC_LNK_LST_ERR_CNT
);
169 iowrite32(0, priv
->mac_io
+ MAC_TX_FRAME_CNT
);
170 iowrite32(0, priv
->mac_io
+ MAC_RX_FRAME_CNT
);
171 iowrite8(0, priv
->mac_io
+ MAC_DROPPED_FRMS
);
173 iowrite8(0, priv
->fifo_io
+ FIFO_TX_RESET
);
174 iowrite8(0, priv
->fifo_io
+ FIFO_RX_RESET
);
176 iowrite8(0, priv
->mac_io
+ MAC_TX_FIFO_LVL
);
179 static void ec_bhf_send_packet(struct ec_bhf_priv
*priv
, struct tx_desc
*desc
)
181 u32 len
= le16_to_cpu(desc
->header
.len
) + sizeof(desc
->header
);
182 u32 addr
= (u8
*)desc
- priv
->tx_buf
.buf
;
184 iowrite32((ALIGN(len
, 8) << 24) | addr
, priv
->fifo_io
+ FIFO_TX_REG
);
187 static int ec_bhf_desc_sent(struct tx_desc
*desc
)
189 return le32_to_cpu(desc
->header
.sent
) & TX_HDR_SENT
;
192 static void ec_bhf_process_tx(struct ec_bhf_priv
*priv
)
194 if (unlikely(netif_queue_stopped(priv
->net_dev
))) {
195 /* Make sure that we perceive changes to tx_dnext. */
198 if (ec_bhf_desc_sent(&priv
->tx_descs
[priv
->tx_dnext
]))
199 netif_wake_queue(priv
->net_dev
);
203 static int ec_bhf_pkt_received(struct rx_desc
*desc
)
205 return le32_to_cpu(desc
->header
.recv
) & RXHDR_NEXT_RECV_FLAG
;
208 static void ec_bhf_add_rx_desc(struct ec_bhf_priv
*priv
, struct rx_desc
*desc
)
210 iowrite32(FIFO_RX_ADDR_VALID
| ((u8
*)(desc
) - priv
->rx_buf
.buf
),
211 priv
->fifo_io
+ FIFO_RX_REG
);
214 static void ec_bhf_process_rx(struct ec_bhf_priv
*priv
)
216 struct rx_desc
*desc
= &priv
->rx_descs
[priv
->rx_dnext
];
218 while (ec_bhf_pkt_received(desc
)) {
219 int pkt_size
= (le16_to_cpu(desc
->header
.len
) &
220 RXHDR_LEN_MASK
) - sizeof(struct rx_header
) - 4;
221 u8
*data
= desc
->data
;
224 skb
= netdev_alloc_skb_ip_align(priv
->net_dev
, pkt_size
);
226 skb_put_data(skb
, data
, pkt_size
);
227 skb
->protocol
= eth_type_trans(skb
, priv
->net_dev
);
228 priv
->stat_rx_bytes
+= pkt_size
;
232 dev_err_ratelimited(PRIV_TO_DEV(priv
),
233 "Couldn't allocate a skb_buff for a packet of size %u\n",
237 desc
->header
.recv
= 0;
239 ec_bhf_add_rx_desc(priv
, desc
);
241 priv
->rx_dnext
= (priv
->rx_dnext
+ 1) % priv
->rx_dcount
;
242 desc
= &priv
->rx_descs
[priv
->rx_dnext
];
246 static enum hrtimer_restart
ec_bhf_timer_fun(struct hrtimer
*timer
)
248 struct ec_bhf_priv
*priv
= container_of(timer
, struct ec_bhf_priv
,
250 ec_bhf_process_rx(priv
);
251 ec_bhf_process_tx(priv
);
253 if (!netif_running(priv
->net_dev
))
254 return HRTIMER_NORESTART
;
256 hrtimer_forward_now(timer
, polling_frequency
);
257 return HRTIMER_RESTART
;
260 static int ec_bhf_setup_offsets(struct ec_bhf_priv
*priv
)
262 struct device
*dev
= PRIV_TO_DEV(priv
);
263 unsigned block_count
, i
;
264 void __iomem
*ec_info
;
266 block_count
= ioread8(priv
->io
+ INFO_BLOCK_BLK_CNT
);
267 for (i
= 0; i
< block_count
; i
++) {
268 u16 type
= ioread16(priv
->io
+ i
* INFO_BLOCK_SIZE
+
270 if (type
== ETHERCAT_MASTER_ID
)
273 if (i
== block_count
) {
274 dev_err(dev
, "EtherCAT master with DMA block not found\n");
278 ec_info
= priv
->io
+ i
* INFO_BLOCK_SIZE
;
280 priv
->tx_dma_chan
= ioread8(ec_info
+ INFO_BLOCK_TX_CHAN
);
281 priv
->rx_dma_chan
= ioread8(ec_info
+ INFO_BLOCK_RX_CHAN
);
283 priv
->ec_io
= priv
->io
+ ioread32(ec_info
+ INFO_BLOCK_OFFSET
);
284 priv
->mii_io
= priv
->ec_io
+ ioread32(priv
->ec_io
+ EC_MII_OFFSET
);
285 priv
->fifo_io
= priv
->ec_io
+ ioread32(priv
->ec_io
+ EC_FIFO_OFFSET
);
286 priv
->mac_io
= priv
->ec_io
+ ioread32(priv
->ec_io
+ EC_MAC_OFFSET
);
291 static netdev_tx_t
ec_bhf_start_xmit(struct sk_buff
*skb
,
292 struct net_device
*net_dev
)
294 struct ec_bhf_priv
*priv
= netdev_priv(net_dev
);
295 struct tx_desc
*desc
;
298 desc
= &priv
->tx_descs
[priv
->tx_dnext
];
300 skb_copy_and_csum_dev(skb
, desc
->data
);
303 memset(&desc
->header
, 0, sizeof(desc
->header
));
304 desc
->header
.len
= cpu_to_le16(len
);
305 desc
->header
.port
= TX_HDR_PORT_0
;
307 ec_bhf_send_packet(priv
, desc
);
309 priv
->tx_dnext
= (priv
->tx_dnext
+ 1) % priv
->tx_dcount
;
311 if (!ec_bhf_desc_sent(&priv
->tx_descs
[priv
->tx_dnext
])) {
312 /* Make sure that updates to tx_dnext are perceived
317 netif_stop_queue(net_dev
);
320 priv
->stat_tx_bytes
+= len
;
327 static int ec_bhf_alloc_dma_mem(struct ec_bhf_priv
*priv
,
332 int offset
= channel
* DMA_CHAN_SIZE
+ DMA_CHAN_OFFSET
;
333 struct device
*dev
= PRIV_TO_DEV(priv
);
336 iowrite32(0xffffffff, priv
->dma_io
+ offset
);
338 mask
= ioread32(priv
->dma_io
+ offset
);
339 mask
&= DMA_WINDOW_SIZE_MASK
;
341 /* We want to allocate a chunk of memory that is:
342 * - aligned to the mask we just read
343 * - is of size 2^mask bytes (at most)
344 * In order to ensure that we will allocate buffer of
347 buf
->len
= min_t(int, ~mask
+ 1, size
);
348 buf
->alloc_len
= 2 * buf
->len
;
350 buf
->alloc
= dma_alloc_coherent(dev
, buf
->alloc_len
, &buf
->alloc_phys
,
352 if (buf
->alloc
== NULL
) {
353 dev_err(dev
, "Failed to allocate buffer\n");
357 buf
->buf_phys
= (buf
->alloc_phys
+ buf
->len
) & mask
;
358 buf
->buf
= buf
->alloc
+ (buf
->buf_phys
- buf
->alloc_phys
);
360 iowrite32(0, priv
->dma_io
+ offset
+ 4);
361 iowrite32(buf
->buf_phys
, priv
->dma_io
+ offset
);
366 static void ec_bhf_setup_tx_descs(struct ec_bhf_priv
*priv
)
370 priv
->tx_dcount
= priv
->tx_buf
.len
/ sizeof(struct tx_desc
);
371 priv
->tx_descs
= (struct tx_desc
*)priv
->tx_buf
.buf
;
374 for (i
= 0; i
< priv
->tx_dcount
; i
++)
375 priv
->tx_descs
[i
].header
.sent
= cpu_to_le32(TX_HDR_SENT
);
378 static void ec_bhf_setup_rx_descs(struct ec_bhf_priv
*priv
)
382 priv
->rx_dcount
= priv
->rx_buf
.len
/ sizeof(struct rx_desc
);
383 priv
->rx_descs
= (struct rx_desc
*)priv
->rx_buf
.buf
;
386 for (i
= 0; i
< priv
->rx_dcount
; i
++) {
387 struct rx_desc
*desc
= &priv
->rx_descs
[i
];
390 if (i
!= priv
->rx_dcount
- 1)
391 next
= (u8
*)(desc
+ 1) - priv
->rx_buf
.buf
;
394 next
|= RXHDR_NEXT_VALID
;
395 desc
->header
.next
= cpu_to_le32(next
);
396 desc
->header
.recv
= 0;
397 ec_bhf_add_rx_desc(priv
, desc
);
401 static int ec_bhf_open(struct net_device
*net_dev
)
403 struct ec_bhf_priv
*priv
= netdev_priv(net_dev
);
404 struct device
*dev
= PRIV_TO_DEV(priv
);
409 err
= ec_bhf_alloc_dma_mem(priv
, &priv
->rx_buf
, priv
->rx_dma_chan
,
410 FIFO_SIZE
* sizeof(struct rx_desc
));
412 dev_err(dev
, "Failed to allocate rx buffer\n");
415 ec_bhf_setup_rx_descs(priv
);
417 err
= ec_bhf_alloc_dma_mem(priv
, &priv
->tx_buf
, priv
->tx_dma_chan
,
418 FIFO_SIZE
* sizeof(struct tx_desc
));
420 dev_err(dev
, "Failed to allocate tx buffer\n");
423 iowrite8(0, priv
->mii_io
+ MII_MAC_FILT_FLAG
);
424 ec_bhf_setup_tx_descs(priv
);
426 netif_start_queue(net_dev
);
428 hrtimer_init(&priv
->hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
429 priv
->hrtimer
.function
= ec_bhf_timer_fun
;
430 hrtimer_start(&priv
->hrtimer
, polling_frequency
, HRTIMER_MODE_REL
);
435 dma_free_coherent(dev
, priv
->rx_buf
.alloc_len
, priv
->rx_buf
.alloc
,
436 priv
->rx_buf
.alloc_len
);
441 static int ec_bhf_stop(struct net_device
*net_dev
)
443 struct ec_bhf_priv
*priv
= netdev_priv(net_dev
);
444 struct device
*dev
= PRIV_TO_DEV(priv
);
446 hrtimer_cancel(&priv
->hrtimer
);
450 netif_tx_disable(net_dev
);
452 dma_free_coherent(dev
, priv
->tx_buf
.alloc_len
,
453 priv
->tx_buf
.alloc
, priv
->tx_buf
.alloc_phys
);
454 dma_free_coherent(dev
, priv
->rx_buf
.alloc_len
,
455 priv
->rx_buf
.alloc
, priv
->rx_buf
.alloc_phys
);
461 ec_bhf_get_stats(struct net_device
*net_dev
,
462 struct rtnl_link_stats64
*stats
)
464 struct ec_bhf_priv
*priv
= netdev_priv(net_dev
);
466 stats
->rx_errors
= ioread8(priv
->mac_io
+ MAC_RX_ERR_CNT
) +
467 ioread8(priv
->mac_io
+ MAC_CRC_ERR_CNT
) +
468 ioread8(priv
->mac_io
+ MAC_FRAME_ERR_CNT
);
469 stats
->rx_packets
= ioread32(priv
->mac_io
+ MAC_RX_FRAME_CNT
);
470 stats
->tx_packets
= ioread32(priv
->mac_io
+ MAC_TX_FRAME_CNT
);
471 stats
->rx_dropped
= ioread8(priv
->mac_io
+ MAC_DROPPED_FRMS
);
473 stats
->tx_bytes
= priv
->stat_tx_bytes
;
474 stats
->rx_bytes
= priv
->stat_rx_bytes
;
477 static const struct net_device_ops ec_bhf_netdev_ops
= {
478 .ndo_start_xmit
= ec_bhf_start_xmit
,
479 .ndo_open
= ec_bhf_open
,
480 .ndo_stop
= ec_bhf_stop
,
481 .ndo_get_stats64
= ec_bhf_get_stats
,
482 .ndo_validate_addr
= eth_validate_addr
,
483 .ndo_set_mac_address
= eth_mac_addr
486 static int ec_bhf_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
488 struct net_device
*net_dev
;
489 struct ec_bhf_priv
*priv
;
490 void __iomem
*dma_io
;
494 err
= pci_enable_device(dev
);
500 err
= pci_set_dma_mask(dev
, DMA_BIT_MASK(32));
503 "Required dma mask not supported, failed to initialize device\n");
505 goto err_disable_dev
;
508 err
= pci_set_consistent_dma_mask(dev
, DMA_BIT_MASK(32));
511 "Required dma mask not supported, failed to initialize device\n");
512 goto err_disable_dev
;
515 err
= pci_request_regions(dev
, "ec_bhf");
517 dev_err(&dev
->dev
, "Failed to request pci memory regions\n");
518 goto err_disable_dev
;
521 io
= pci_iomap(dev
, 0, 0);
523 dev_err(&dev
->dev
, "Failed to map pci card memory bar 0");
525 goto err_release_regions
;
528 dma_io
= pci_iomap(dev
, 2, 0);
530 dev_err(&dev
->dev
, "Failed to map pci card memory bar 2");
535 net_dev
= alloc_etherdev(sizeof(struct ec_bhf_priv
));
536 if (net_dev
== NULL
) {
538 goto err_unmap_dma_io
;
541 pci_set_drvdata(dev
, net_dev
);
542 SET_NETDEV_DEV(net_dev
, &dev
->dev
);
544 net_dev
->features
= 0;
545 net_dev
->flags
|= IFF_NOARP
;
547 net_dev
->netdev_ops
= &ec_bhf_netdev_ops
;
549 priv
= netdev_priv(net_dev
);
550 priv
->net_dev
= net_dev
;
552 priv
->dma_io
= dma_io
;
555 err
= ec_bhf_setup_offsets(priv
);
557 goto err_free_net_dev
;
559 memcpy_fromio(net_dev
->dev_addr
, priv
->mii_io
+ MII_MAC_ADDR
, 6);
561 err
= register_netdev(net_dev
);
563 goto err_free_net_dev
;
568 free_netdev(net_dev
);
570 pci_iounmap(dev
, dma_io
);
572 pci_iounmap(dev
, io
);
574 pci_release_regions(dev
);
576 pci_clear_master(dev
);
577 pci_disable_device(dev
);
582 static void ec_bhf_remove(struct pci_dev
*dev
)
584 struct net_device
*net_dev
= pci_get_drvdata(dev
);
585 struct ec_bhf_priv
*priv
= netdev_priv(net_dev
);
587 unregister_netdev(net_dev
);
588 free_netdev(net_dev
);
590 pci_iounmap(dev
, priv
->dma_io
);
591 pci_iounmap(dev
, priv
->io
);
592 pci_release_regions(dev
);
593 pci_clear_master(dev
);
594 pci_disable_device(dev
);
597 static struct pci_driver pci_driver
= {
600 .probe
= ec_bhf_probe
,
601 .remove
= ec_bhf_remove
,
603 module_pci_driver(pci_driver
);
605 module_param(polling_frequency
, long, S_IRUGO
);
606 MODULE_PARM_DESC(polling_frequency
, "Polling timer frequency in ns");
608 MODULE_LICENSE("GPL");
609 MODULE_AUTHOR("Dariusz Marcinkiewicz <reksio@newterm.pl>");