1 /* Renesas Ethernet AVB device driver
3 * Copyright (C) 2014-2015 Renesas Electronics Corporation
4 * Copyright (C) 2015 Renesas Solutions Corp.
5 * Copyright (C) 2015-2016 Cogent Embedded, Inc. <source@cogentembedded.com>
7 * Based on the SuperH Ethernet driver
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License version 2,
11 * as published by the Free Software Foundation.
17 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/mdio-bitbang.h>
21 #include <linux/netdevice.h>
22 #include <linux/phy.h>
23 #include <linux/platform_device.h>
24 #include <linux/ptp_clock_kernel.h>
26 #define BE_TX_RING_SIZE 64 /* TX ring size for Best Effort */
27 #define BE_RX_RING_SIZE 1024 /* RX ring size for Best Effort */
28 #define NC_TX_RING_SIZE 64 /* TX ring size for Network Control */
29 #define NC_RX_RING_SIZE 64 /* RX ring size for Network Control */
30 #define BE_TX_RING_MIN 64
31 #define BE_RX_RING_MIN 64
32 #define BE_TX_RING_MAX 1024
33 #define BE_RX_RING_MAX 2048
35 #define PKT_BUF_SZ 1538
37 /* Driver's parameters */
38 #define RAVB_ALIGN 128
40 /* Hardware time stamp */
41 #define RAVB_TXTSTAMP_VALID 0x00000001 /* TX timestamp valid */
42 #define RAVB_TXTSTAMP_ENABLED 0x00000010 /* Enable TX timestamping */
44 #define RAVB_RXTSTAMP_VALID 0x00000001 /* RX timestamp valid */
45 #define RAVB_RXTSTAMP_TYPE 0x00000006 /* RX type mask */
46 #define RAVB_RXTSTAMP_TYPE_V2_L2_EVENT 0x00000002
47 #define RAVB_RXTSTAMP_TYPE_ALL 0x00000006
48 #define RAVB_RXTSTAMP_ENABLED 0x00000010 /* Enable RX timestamping */
51 /* AVB-DMAC registers */
169 GCPT
= 0x03B4, /* Undocumented? */
174 /* E-MAC registers */
188 TROCR
= 0x0700, /* Undocumented? */
189 CDCR
= 0x0708, /* Undocumented? */
190 LCCR
= 0x0710, /* Undocumented? */
196 CERCR
= 0x0768, /* Undocumented? */
197 CEECR
= 0x0770, /* Undocumented? */
202 /* Register bits of the Ethernet AVB */
205 CCC_OPC
= 0x00000003,
206 CCC_OPC_RESET
= 0x00000000,
207 CCC_OPC_CONFIG
= 0x00000001,
208 CCC_OPC_OPERATION
= 0x00000002,
209 CCC_GAC
= 0x00000080,
210 CCC_DTSR
= 0x00000100,
211 CCC_CSEL
= 0x00030000,
212 CCC_CSEL_HPB
= 0x00010000,
213 CCC_CSEL_ETH_TX
= 0x00020000,
214 CCC_CSEL_GMII_REF
= 0x00030000,
215 CCC_BOC
= 0x00100000, /* Undocumented? */
216 CCC_LBME
= 0x01000000,
221 CSR_OPS
= 0x0000000F,
222 CSR_OPS_RESET
= 0x00000001,
223 CSR_OPS_CONFIG
= 0x00000002,
224 CSR_OPS_OPERATION
= 0x00000004,
225 CSR_OPS_STANDBY
= 0x00000008, /* Undocumented? */
226 CSR_DTS
= 0x00000100,
227 CSR_TPO0
= 0x00010000,
228 CSR_TPO1
= 0x00020000,
229 CSR_TPO2
= 0x00040000,
230 CSR_TPO3
= 0x00080000,
231 CSR_RPO
= 0x00100000,
236 ESR_EQN
= 0x0000001F,
238 ESR_EIL
= 0x00001000,
243 RCR_EFFS
= 0x00000001,
244 RCR_ENCF
= 0x00000002,
245 RCR_ESF
= 0x0000000C,
246 RCR_ETS0
= 0x00000010,
247 RCR_ETS2
= 0x00000020,
248 RCR_RFCL
= 0x1FFF0000,
253 RQC_RSM0
= 0x00000003,
254 RQC_UFCC0
= 0x00000030,
255 RQC_RSM1
= 0x00000300,
256 RQC_UFCC1
= 0x00003000,
257 RQC_RSM2
= 0x00030000,
258 RQC_UFCC2
= 0x00300000,
259 RQC_RSM3
= 0x03000000,
260 RQC_UFCC3
= 0x30000000,
265 RPC_PCNT
= 0x00000700,
266 RPC_DCNT
= 0x00FF0000,
271 UFCW_WL0
= 0x0000003F,
272 UFCW_WL1
= 0x00003F00,
273 UFCW_WL2
= 0x003F0000,
274 UFCW_WL3
= 0x3F000000,
279 UFCS_SL0
= 0x0000003F,
280 UFCS_SL1
= 0x00003F00,
281 UFCS_SL2
= 0x003F0000,
282 UFCS_SL3
= 0x3F000000,
287 UFCV_CV0
= 0x0000003F,
288 UFCV_CV1
= 0x00003F00,
289 UFCV_CV2
= 0x003F0000,
290 UFCV_CV3
= 0x3F000000,
295 UFCD_DV0
= 0x0000003F,
296 UFCD_DV1
= 0x00003F00,
297 UFCD_DV2
= 0x003F0000,
298 UFCD_DV3
= 0x3F000000,
303 SFO_FPB
= 0x0000003F,
308 RTC_MFL0
= 0x00000FFF,
309 RTC_MFL1
= 0x0FFF0000,
314 TGC_TSM0
= 0x00000001,
315 TGC_TSM1
= 0x00000002,
316 TGC_TSM2
= 0x00000004,
317 TGC_TSM3
= 0x00000008,
318 TGC_TQP
= 0x00000030,
319 TGC_TQP_NONAVB
= 0x00000000,
320 TGC_TQP_AVBMODE1
= 0x00000010,
321 TGC_TQP_AVBMODE2
= 0x00000030,
322 TGC_TBD0
= 0x00000300,
323 TGC_TBD1
= 0x00003000,
324 TGC_TBD2
= 0x00030000,
325 TGC_TBD3
= 0x00300000,
330 TCCR_TSRQ0
= 0x00000001,
331 TCCR_TSRQ1
= 0x00000002,
332 TCCR_TSRQ2
= 0x00000004,
333 TCCR_TSRQ3
= 0x00000008,
334 TCCR_TFEN
= 0x00000100,
335 TCCR_TFR
= 0x00000200,
340 TSR_CCS0
= 0x00000003,
341 TSR_CCS1
= 0x0000000C,
342 TSR_TFFL
= 0x00000700,
347 TFA2_TSV
= 0x0000FFFF,
348 TFA2_TST
= 0x03FF0000,
353 DIC_DPE1
= 0x00000002,
354 DIC_DPE2
= 0x00000004,
355 DIC_DPE3
= 0x00000008,
356 DIC_DPE4
= 0x00000010,
357 DIC_DPE5
= 0x00000020,
358 DIC_DPE6
= 0x00000040,
359 DIC_DPE7
= 0x00000080,
360 DIC_DPE8
= 0x00000100,
361 DIC_DPE9
= 0x00000200,
362 DIC_DPE10
= 0x00000400,
363 DIC_DPE11
= 0x00000800,
364 DIC_DPE12
= 0x00001000,
365 DIC_DPE13
= 0x00002000,
366 DIC_DPE14
= 0x00004000,
367 DIC_DPE15
= 0x00008000,
372 DIS_DPF1
= 0x00000002,
373 DIS_DPF2
= 0x00000004,
374 DIS_DPF3
= 0x00000008,
375 DIS_DPF4
= 0x00000010,
376 DIS_DPF5
= 0x00000020,
377 DIS_DPF6
= 0x00000040,
378 DIS_DPF7
= 0x00000080,
379 DIS_DPF8
= 0x00000100,
380 DIS_DPF9
= 0x00000200,
381 DIS_DPF10
= 0x00000400,
382 DIS_DPF11
= 0x00000800,
383 DIS_DPF12
= 0x00001000,
384 DIS_DPF13
= 0x00002000,
385 DIS_DPF14
= 0x00004000,
386 DIS_DPF15
= 0x00008000,
391 EIC_MREE
= 0x00000001,
392 EIC_MTEE
= 0x00000002,
393 EIC_QEE
= 0x00000004,
394 EIC_SEE
= 0x00000008,
395 EIC_CLLE0
= 0x00000010,
396 EIC_CLLE1
= 0x00000020,
397 EIC_CULE0
= 0x00000040,
398 EIC_CULE1
= 0x00000080,
399 EIC_TFFE
= 0x00000100,
404 EIS_MREF
= 0x00000001,
405 EIS_MTEF
= 0x00000002,
406 EIS_QEF
= 0x00000004,
407 EIS_SEF
= 0x00000008,
408 EIS_CLLF0
= 0x00000010,
409 EIS_CLLF1
= 0x00000020,
410 EIS_CULF0
= 0x00000040,
411 EIS_CULF1
= 0x00000080,
412 EIS_TFFF
= 0x00000100,
413 EIS_QFS
= 0x00010000,
418 RIC0_FRE0
= 0x00000001,
419 RIC0_FRE1
= 0x00000002,
420 RIC0_FRE2
= 0x00000004,
421 RIC0_FRE3
= 0x00000008,
422 RIC0_FRE4
= 0x00000010,
423 RIC0_FRE5
= 0x00000020,
424 RIC0_FRE6
= 0x00000040,
425 RIC0_FRE7
= 0x00000080,
426 RIC0_FRE8
= 0x00000100,
427 RIC0_FRE9
= 0x00000200,
428 RIC0_FRE10
= 0x00000400,
429 RIC0_FRE11
= 0x00000800,
430 RIC0_FRE12
= 0x00001000,
431 RIC0_FRE13
= 0x00002000,
432 RIC0_FRE14
= 0x00004000,
433 RIC0_FRE15
= 0x00008000,
434 RIC0_FRE16
= 0x00010000,
435 RIC0_FRE17
= 0x00020000,
440 RIS0_FRF0
= 0x00000001,
441 RIS0_FRF1
= 0x00000002,
442 RIS0_FRF2
= 0x00000004,
443 RIS0_FRF3
= 0x00000008,
444 RIS0_FRF4
= 0x00000010,
445 RIS0_FRF5
= 0x00000020,
446 RIS0_FRF6
= 0x00000040,
447 RIS0_FRF7
= 0x00000080,
448 RIS0_FRF8
= 0x00000100,
449 RIS0_FRF9
= 0x00000200,
450 RIS0_FRF10
= 0x00000400,
451 RIS0_FRF11
= 0x00000800,
452 RIS0_FRF12
= 0x00001000,
453 RIS0_FRF13
= 0x00002000,
454 RIS0_FRF14
= 0x00004000,
455 RIS0_FRF15
= 0x00008000,
456 RIS0_FRF16
= 0x00010000,
457 RIS0_FRF17
= 0x00020000,
462 RIC1_RFWE
= 0x80000000,
467 RIS1_RFWF
= 0x80000000,
472 RIC2_QFE0
= 0x00000001,
473 RIC2_QFE1
= 0x00000002,
474 RIC2_QFE2
= 0x00000004,
475 RIC2_QFE3
= 0x00000008,
476 RIC2_QFE4
= 0x00000010,
477 RIC2_QFE5
= 0x00000020,
478 RIC2_QFE6
= 0x00000040,
479 RIC2_QFE7
= 0x00000080,
480 RIC2_QFE8
= 0x00000100,
481 RIC2_QFE9
= 0x00000200,
482 RIC2_QFE10
= 0x00000400,
483 RIC2_QFE11
= 0x00000800,
484 RIC2_QFE12
= 0x00001000,
485 RIC2_QFE13
= 0x00002000,
486 RIC2_QFE14
= 0x00004000,
487 RIC2_QFE15
= 0x00008000,
488 RIC2_QFE16
= 0x00010000,
489 RIC2_QFE17
= 0x00020000,
490 RIC2_RFFE
= 0x80000000,
495 RIS2_QFF0
= 0x00000001,
496 RIS2_QFF1
= 0x00000002,
497 RIS2_QFF2
= 0x00000004,
498 RIS2_QFF3
= 0x00000008,
499 RIS2_QFF4
= 0x00000010,
500 RIS2_QFF5
= 0x00000020,
501 RIS2_QFF6
= 0x00000040,
502 RIS2_QFF7
= 0x00000080,
503 RIS2_QFF8
= 0x00000100,
504 RIS2_QFF9
= 0x00000200,
505 RIS2_QFF10
= 0x00000400,
506 RIS2_QFF11
= 0x00000800,
507 RIS2_QFF12
= 0x00001000,
508 RIS2_QFF13
= 0x00002000,
509 RIS2_QFF14
= 0x00004000,
510 RIS2_QFF15
= 0x00008000,
511 RIS2_QFF16
= 0x00010000,
512 RIS2_QFF17
= 0x00020000,
513 RIS2_RFFF
= 0x80000000,
518 TIC_FTE0
= 0x00000001, /* Undocumented? */
519 TIC_FTE1
= 0x00000002, /* Undocumented? */
520 TIC_TFUE
= 0x00000100,
521 TIC_TFWE
= 0x00000200,
526 TIS_FTF0
= 0x00000001, /* Undocumented? */
527 TIS_FTF1
= 0x00000002, /* Undocumented? */
528 TIS_TFUF
= 0x00000100,
529 TIS_TFWF
= 0x00000200,
534 ISS_FRS
= 0x00000001, /* Undocumented? */
535 ISS_FTS
= 0x00000004, /* Undocumented? */
538 ISS_TFUS
= 0x00000100,
539 ISS_TFWS
= 0x00000200,
540 ISS_RFWS
= 0x00001000,
541 ISS_CGIS
= 0x00002000,
542 ISS_DPS1
= 0x00020000,
543 ISS_DPS2
= 0x00040000,
544 ISS_DPS3
= 0x00080000,
545 ISS_DPS4
= 0x00100000,
546 ISS_DPS5
= 0x00200000,
547 ISS_DPS6
= 0x00400000,
548 ISS_DPS7
= 0x00800000,
549 ISS_DPS8
= 0x01000000,
550 ISS_DPS9
= 0x02000000,
551 ISS_DPS10
= 0x04000000,
552 ISS_DPS11
= 0x08000000,
553 ISS_DPS12
= 0x10000000,
554 ISS_DPS13
= 0x20000000,
555 ISS_DPS14
= 0x40000000,
556 ISS_DPS15
= 0x80000000,
561 GCCR_TCR
= 0x00000003,
562 GCCR_TCR_NOREQ
= 0x00000000, /* No request */
563 GCCR_TCR_RESET
= 0x00000001, /* gPTP/AVTP presentation timer reset */
564 GCCR_TCR_CAPTURE
= 0x00000003, /* Capture value set in GCCR.TCSS */
565 GCCR_LTO
= 0x00000004,
566 GCCR_LTI
= 0x00000008,
567 GCCR_LPTC
= 0x00000010,
568 GCCR_LMTT
= 0x00000020,
569 GCCR_TCSS
= 0x00000300,
570 GCCR_TCSS_GPTP
= 0x00000000, /* gPTP timer value */
571 GCCR_TCSS_ADJGPTP
= 0x00000100, /* Adjusted gPTP timer value */
572 GCCR_TCSS_AVTP
= 0x00000200, /* AVTP presentation time value */
577 GTI_TIV
= 0x0FFFFFFF,
580 #define GTI_TIV_MAX GTI_TIV
581 #define GTI_TIV_MIN 0x20
585 GIC_PTCE
= 0x00000001, /* Undocumented? */
586 GIC_PTME
= 0x00000004,
591 GIS_PTCF
= 0x00000001, /* Undocumented? */
592 GIS_PTMF
= 0x00000004,
597 ECMR_PRM
= 0x00000001,
598 ECMR_DM
= 0x00000002,
599 ECMR_TE
= 0x00000020,
600 ECMR_RE
= 0x00000040,
601 ECMR_MPDE
= 0x00000200,
602 ECMR_TXF
= 0x00010000, /* Undocumented? */
603 ECMR_RXF
= 0x00020000,
604 ECMR_PFR
= 0x00040000,
605 ECMR_ZPF
= 0x00080000, /* Undocumented? */
606 ECMR_RZPF
= 0x00100000,
607 ECMR_DPAD
= 0x00200000,
608 ECMR_RCSC
= 0x00800000,
609 ECMR_TRCCM
= 0x04000000,
614 ECSR_ICD
= 0x00000001,
615 ECSR_MPD
= 0x00000002,
616 ECSR_LCHNG
= 0x00000004,
617 ECSR_PHYI
= 0x00000008,
622 ECSIPR_ICDIP
= 0x00000001,
623 ECSIPR_MPDIP
= 0x00000002,
624 ECSIPR_LCHNGIP
= 0x00000004, /* Undocumented? */
629 PIR_MDC
= 0x00000001,
630 PIR_MMD
= 0x00000002,
631 PIR_MDO
= 0x00000004,
632 PIR_MDI
= 0x00000008,
637 PSR_LMON
= 0x00000001,
642 PIPR_PHYIP
= 0x00000001,
652 GECMR_SPEED
= 0x00000001,
653 GECMR_SPEED_100
= 0x00000000,
654 GECMR_SPEED_1000
= 0x00000001,
657 /* The Ethernet AVB descriptor definitions. */
659 __le16 ds
; /* Descriptor size */
660 u8 cc
; /* Content control MSBs (reserved) */
661 u8 die_dt
; /* Descriptor interrupt enable and type */
662 __le32 dptr
; /* Descriptor pointer */
665 #define DPTR_ALIGN 4 /* Required descriptor pointer alignment */
677 /* HW/SW arbitration */
686 struct ravb_rx_desc
{
687 __le16 ds_cc
; /* Descriptor size and content control LSBs */
688 u8 msc
; /* MAC status code */
689 u8 die_dt
; /* Descriptor interrupt enable and type */
690 __le32 dptr
; /* Descpriptor pointer */
693 struct ravb_ex_rx_desc
{
694 __le16 ds_cc
; /* Descriptor size and content control lower bits */
695 u8 msc
; /* MAC status code */
696 u8 die_dt
; /* Descriptor interrupt enable and type */
697 __le32 dptr
; /* Descpriptor pointer */
698 __le32 ts_n
; /* Timestampe nsec */
699 __le32 ts_sl
; /* Timestamp low */
700 __le16 ts_sh
; /* Timestamp high */
701 __le16 res
; /* Reserved bits */
705 RX_DS
= 0x0fff, /* Data size */
706 RX_TR
= 0x1000, /* Truncation indication */
707 RX_EI
= 0x2000, /* Error indication */
708 RX_PS
= 0xc000, /* Padding selection */
711 /* E-MAC status code */
713 MSC_CRC
= 0x01, /* Frame CRC error */
714 MSC_RFE
= 0x02, /* Frame reception error (flagged by PHY) */
715 MSC_RTSF
= 0x04, /* Frame length error (frame too short) */
716 MSC_RTLF
= 0x08, /* Frame length error (frame too long) */
717 MSC_FRE
= 0x10, /* Fraction error (not a multiple of 8 bits) */
718 MSC_CRL
= 0x20, /* Carrier lost */
719 MSC_CEEF
= 0x40, /* Carrier extension error */
720 MSC_MC
= 0x80, /* Multicast frame reception */
723 struct ravb_tx_desc
{
724 __le16 ds_tagl
; /* Descriptor size and frame tag LSBs */
725 u8 tagh_tsr
; /* Frame tag MSBs and timestamp storage request bit */
726 u8 die_dt
; /* Descriptor interrupt enable and type */
727 __le32 dptr
; /* Descpriptor pointer */
730 enum TX_DS_TAGL_BIT
{
731 TX_DS
= 0x0fff, /* Data size */
732 TX_TAGL
= 0xf000, /* Frame tag LSBs */
735 enum TX_TAGH_TSR_BIT
{
736 TX_TAGH
= 0x3f, /* Frame tag MSBs */
737 TX_TSR
= 0x40, /* Timestamp storage request */
740 RAVB_BE
= 0, /* Best Effort Queue */
741 RAVB_NC
, /* Network Control Queue */
744 #define DBAT_ENTRY_NUM 22
745 #define RX_QUEUE_OFFSET 4
746 #define NUM_RX_QUEUE 2
747 #define NUM_TX_QUEUE 2
748 #define NUM_TX_DESC 2 /* TX descriptors per packet */
750 struct ravb_tstamp_skb
{
751 struct list_head list
;
756 struct ravb_ptp_perout
{
765 struct ptp_clock
*clock
;
766 struct ptp_clock_info info
;
770 struct ravb_ptp_perout perout
[N_PER_OUT
];
778 struct ravb_private
{
779 struct net_device
*ndev
;
780 struct platform_device
*pdev
;
782 struct mdiobb_ctrl mdiobb
;
783 u32 num_rx_ring
[NUM_RX_QUEUE
];
784 u32 num_tx_ring
[NUM_TX_QUEUE
];
786 dma_addr_t desc_bat_dma
;
787 struct ravb_desc
*desc_bat
;
788 dma_addr_t rx_desc_dma
[NUM_RX_QUEUE
];
789 dma_addr_t tx_desc_dma
[NUM_TX_QUEUE
];
790 struct ravb_ex_rx_desc
*rx_ring
[NUM_RX_QUEUE
];
791 struct ravb_tx_desc
*tx_ring
[NUM_TX_QUEUE
];
792 void *tx_align
[NUM_TX_QUEUE
];
793 struct sk_buff
**rx_skb
[NUM_RX_QUEUE
];
794 struct sk_buff
**tx_skb
[NUM_TX_QUEUE
];
797 struct net_device_stats stats
[NUM_RX_QUEUE
];
800 struct list_head ts_skb_list
;
803 spinlock_t lock
; /* Register access lock */
804 u32 cur_rx
[NUM_RX_QUEUE
]; /* Consumer ring indices */
805 u32 dirty_rx
[NUM_RX_QUEUE
]; /* Producer ring indices */
806 u32 cur_tx
[NUM_TX_QUEUE
];
807 u32 dirty_tx
[NUM_TX_QUEUE
];
808 struct napi_struct napi
[NUM_RX_QUEUE
];
809 struct work_struct work
;
810 /* MII transceiver section. */
811 struct mii_bus
*mii_bus
; /* MDIO bus control */
812 struct phy_device
*phydev
; /* PHY device control */
814 phy_interface_t phy_interface
;
819 enum ravb_chip_id chip_id
;
821 unsigned no_avb_link
:1;
822 unsigned avb_link_active_low
:1;
825 static inline u32
ravb_read(struct net_device
*ndev
, enum ravb_reg reg
)
827 struct ravb_private
*priv
= netdev_priv(ndev
);
829 return ioread32(priv
->addr
+ reg
);
832 static inline void ravb_write(struct net_device
*ndev
, u32 data
,
835 struct ravb_private
*priv
= netdev_priv(ndev
);
837 iowrite32(data
, priv
->addr
+ reg
);
840 void ravb_modify(struct net_device
*ndev
, enum ravb_reg reg
, u32 clear
,
842 int ravb_wait(struct net_device
*ndev
, enum ravb_reg reg
, u32 mask
, u32 value
);
844 irqreturn_t
ravb_ptp_interrupt(struct net_device
*ndev
);
845 void ravb_ptp_init(struct net_device
*ndev
, struct platform_device
*pdev
);
846 void ravb_ptp_stop(struct net_device
*ndev
);
848 #endif /* #ifndef __RAVB_H__ */