1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
3 * Copyright 2008 - 2015 Freescale Semiconductor Inc.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include "fman_memac.h"
12 #include <linux/slab.h>
14 #include <linux/pcs-lynx.h>
15 #include <linux/phy.h>
16 #include <linux/phy_fixed.h>
17 #include <linux/phy/phy.h>
18 #include <linux/of_mdio.h>
20 /* Num of additional exact match MAC adr regs */
21 #define MEMAC_NUM_OF_PADDRS 7
23 /* Control and Configuration Register (COMMAND_CONFIG) */
24 #define CMD_CFG_REG_LOWP_RXETY 0x01000000 /* 07 Rx low power indication */
25 #define CMD_CFG_TX_LOWP_ENA 0x00800000 /* 08 Tx Low Power Idle Enable */
26 #define CMD_CFG_PFC_MODE 0x00080000 /* 12 Enable PFC */
27 #define CMD_CFG_NO_LEN_CHK 0x00020000 /* 14 Payload length check disable */
28 #define CMD_CFG_SW_RESET 0x00001000 /* 19 S/W Reset, self clearing bit */
29 #define CMD_CFG_TX_PAD_EN 0x00000800 /* 20 Enable Tx padding of frames */
30 #define CMD_CFG_PAUSE_IGNORE 0x00000100 /* 23 Ignore Pause frame quanta */
31 #define CMD_CFG_CRC_FWD 0x00000040 /* 25 Terminate/frwd CRC of frames */
32 #define CMD_CFG_PAD_EN 0x00000020 /* 26 Frame padding removal */
33 #define CMD_CFG_PROMIS_EN 0x00000010 /* 27 Promiscuous operation enable */
34 #define CMD_CFG_RX_EN 0x00000002 /* 30 MAC receive path enable */
35 #define CMD_CFG_TX_EN 0x00000001 /* 31 MAC transmit path enable */
37 /* Transmit FIFO Sections Register (TX_FIFO_SECTIONS) */
38 #define TX_FIFO_SECTIONS_TX_EMPTY_MASK 0xFFFF0000
39 #define TX_FIFO_SECTIONS_TX_AVAIL_MASK 0x0000FFFF
40 #define TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_10G 0x00400000
41 #define TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_1G 0x00100000
42 #define TX_FIFO_SECTIONS_TX_AVAIL_10G 0x00000019
43 #define TX_FIFO_SECTIONS_TX_AVAIL_1G 0x00000020
44 #define TX_FIFO_SECTIONS_TX_AVAIL_SLOW_10G 0x00000060
46 #define GET_TX_EMPTY_DEFAULT_VALUE(_val) \
48 _val &= ~TX_FIFO_SECTIONS_TX_EMPTY_MASK; \
49 ((_val == TX_FIFO_SECTIONS_TX_AVAIL_10G) ? \
50 (_val |= TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_10G) :\
51 (_val |= TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_1G));\
54 /* Interface Mode Register (IF_MODE) */
56 #define IF_MODE_MASK 0x00000003 /* 30-31 Mask on i/f mode bits */
57 #define IF_MODE_10G 0x00000000 /* 30-31 10G interface */
58 #define IF_MODE_MII 0x00000001 /* 30-31 MII interface */
59 #define IF_MODE_GMII 0x00000002 /* 30-31 GMII (1G) interface */
60 #define IF_MODE_RGMII 0x00000004
61 #define IF_MODE_RGMII_AUTO 0x00008000
62 #define IF_MODE_RGMII_1000 0x00004000 /* 10 - 1000Mbps RGMII */
63 #define IF_MODE_RGMII_100 0x00000000 /* 00 - 100Mbps RGMII */
64 #define IF_MODE_RGMII_10 0x00002000 /* 01 - 10Mbps RGMII */
65 #define IF_MODE_RGMII_SP_MASK 0x00006000 /* Setsp mask bits */
66 #define IF_MODE_RGMII_FD 0x00001000 /* Full duplex RGMII */
67 #define IF_MODE_HD 0x00000040 /* Half duplex operation */
69 /* Hash table Control Register (HASHTABLE_CTRL) */
70 #define HASH_CTRL_MCAST_EN 0x00000100
71 /* 26-31 Hash table address code */
72 #define HASH_CTRL_ADDR_MASK 0x0000003F
73 /* MAC mcast indication */
74 #define GROUP_ADDRESS 0x0000010000000000LL
75 #define HASH_TABLE_SIZE 64 /* Hash tbl size */
77 /* Interrupt Mask Register (IMASK) */
78 #define MEMAC_IMASK_MGI 0x40000000 /* 1 Magic pkt detect indication */
79 #define MEMAC_IMASK_TSECC_ER 0x20000000 /* 2 Timestamp FIFO ECC error evnt */
80 #define MEMAC_IMASK_TECC_ER 0x02000000 /* 6 Transmit frame ECC error evnt */
81 #define MEMAC_IMASK_RECC_ER 0x01000000 /* 7 Receive frame ECC error evnt */
83 #define MEMAC_ALL_ERRS_IMASK \
84 ((u32)(MEMAC_IMASK_TSECC_ER | \
85 MEMAC_IMASK_TECC_ER | \
86 MEMAC_IMASK_RECC_ER | \
89 #define MEMAC_IEVNT_PCS 0x80000000 /* PCS (XG). Link sync (G) */
90 #define MEMAC_IEVNT_AN 0x40000000 /* Auto-negotiation */
91 #define MEMAC_IEVNT_LT 0x20000000 /* Link Training/New page */
92 #define MEMAC_IEVNT_MGI 0x00004000 /* Magic pkt detection */
93 #define MEMAC_IEVNT_TS_ECC_ER 0x00002000 /* Timestamp FIFO ECC error*/
94 #define MEMAC_IEVNT_RX_FIFO_OVFL 0x00001000 /* Rx FIFO overflow */
95 #define MEMAC_IEVNT_TX_FIFO_UNFL 0x00000800 /* Tx FIFO underflow */
96 #define MEMAC_IEVNT_TX_FIFO_OVFL 0x00000400 /* Tx FIFO overflow */
97 #define MEMAC_IEVNT_TX_ECC_ER 0x00000200 /* Tx frame ECC error */
98 #define MEMAC_IEVNT_RX_ECC_ER 0x00000100 /* Rx frame ECC error */
99 #define MEMAC_IEVNT_LI_FAULT 0x00000080 /* Link Interruption flt */
100 #define MEMAC_IEVNT_RX_EMPTY 0x00000040 /* Rx FIFO empty */
101 #define MEMAC_IEVNT_TX_EMPTY 0x00000020 /* Tx FIFO empty */
102 #define MEMAC_IEVNT_RX_LOWP 0x00000010 /* Low Power Idle */
103 #define MEMAC_IEVNT_PHY_LOS 0x00000004 /* Phy loss of signal */
104 #define MEMAC_IEVNT_REM_FAULT 0x00000002 /* Remote fault (XGMII) */
105 #define MEMAC_IEVNT_LOC_FAULT 0x00000001 /* Local fault (XGMII) */
107 #define DEFAULT_PAUSE_QUANTA 0xf000
108 #define DEFAULT_FRAME_LENGTH 0x600
109 #define DEFAULT_TX_IPG_LENGTH 12
111 #define CLXY_PAUSE_QUANTA_CLX_PQNT 0x0000FFFF
112 #define CLXY_PAUSE_QUANTA_CLY_PQNT 0xFFFF0000
113 #define CLXY_PAUSE_THRESH_CLX_QTH 0x0000FFFF
114 #define CLXY_PAUSE_THRESH_CLY_QTH 0xFFFF0000
117 /* Lower 32 bits of 48-bit MAC address */
119 /* Upper 16 bits of 48-bit MAC address */
125 u32 res0000[2]; /* General Control and Status */
126 u32 command_config; /* 0x008 Ctrl and cfg */
127 struct mac_addr mac_addr0; /* 0x00C-0x010 MAC_ADDR_0...1 */
128 u32 maxfrm; /* 0x014 Max frame length */
130 u32 rx_fifo_sections; /* Receive FIFO configuration reg */
131 u32 tx_fifo_sections; /* Transmit FIFO configuration reg */
133 u32 hashtable_ctrl; /* 0x02C Hash table control */
135 u32 ievent; /* 0x040 Interrupt event */
136 u32 tx_ipg_length; /* 0x044 Transmitter inter-packet-gap */
138 u32 imask; /* 0x04C Interrupt mask */
140 u32 pause_quanta[4]; /* 0x054 Pause quanta */
141 u32 pause_thresh[4]; /* 0x064 Pause quanta threshold */
142 u32 rx_pause_status; /* 0x074 Receive pause status */
144 struct mac_addr mac_addr[MEMAC_NUM_OF_PADDRS];/* 0x80-0x0B4 mac padr */
145 u32 lpwake_timer; /* 0x0B8 Low Power Wakeup Timer */
146 u32 sleep_timer; /* 0x0BC Transmit EEE Low Power Timer */
148 u32 statn_config; /* 0x0E0 Statistics configuration */
150 /* Rx Statistics Counter */
204 /* Tx Statistics Counter */
249 /* Line Interface Control */
250 u32 if_mode; /* 0x300 Interface Mode Control */
251 u32 if_status; /* 0x304 Interface Status */
254 u32 hg_config; /* 0x340 Control and cfg */
256 u32 hg_pause_quanta; /* 0x350 Pause quanta */
258 u32 hg_pause_thresh; /* 0x360 Pause quanta threshold */
260 u32 hgrx_pause_status; /* 0x370 Receive pause status */
261 u32 hg_fifos_status; /* 0x374 fifos status */
262 u32 rhm; /* 0x378 rx messages counter */
263 u32 thm; /* 0x37C tx messages counter */
269 bool promiscuous_mode_enable;
270 u16 max_frame_length;
276 /* Pointer to MAC memory mapped registers */
277 struct memac_regs __iomem *regs;
278 /* MAC address of device */
280 struct mac_device *dev_id; /* device cookie used by the exception cbs */
281 fman_mac_exception_cb *exception_cb;
282 fman_mac_exception_cb *event_cb;
283 /* Pointer to driver's global address hash table */
284 struct eth_hash_t *multicast_addr_hash;
285 /* Pointer to driver's individual address hash table */
286 struct eth_hash_t *unicast_addr_hash;
289 struct memac_cfg *memac_drv_param;
291 struct fman_rev_info fm_rev_info;
293 struct phylink_pcs *sgmii_pcs;
294 struct phylink_pcs *qsgmii_pcs;
295 struct phylink_pcs *xfi_pcs;
296 bool allmulti_enabled;
297 bool rgmii_no_half_duplex;
300 static void add_addr_in_paddr(struct memac_regs __iomem *regs, const u8 *adr,
305 tmp0 = (u32)(adr[0] | adr[1] << 8 | adr[2] << 16 | adr[3] << 24);
306 tmp1 = (u32)(adr[4] | adr[5] << 8);
308 if (paddr_num == 0) {
309 iowrite32be(tmp0, ®s->mac_addr0.mac_addr_l);
310 iowrite32be(tmp1, ®s->mac_addr0.mac_addr_u);
312 iowrite32be(tmp0, ®s->mac_addr[paddr_num - 1].mac_addr_l);
313 iowrite32be(tmp1, ®s->mac_addr[paddr_num - 1].mac_addr_u);
317 static int reset(struct memac_regs __iomem *regs)
322 tmp = ioread32be(®s->command_config);
324 tmp |= CMD_CFG_SW_RESET;
326 iowrite32be(tmp, ®s->command_config);
331 } while ((ioread32be(®s->command_config) & CMD_CFG_SW_RESET) &&
340 static void set_exception(struct memac_regs __iomem *regs, u32 val,
345 tmp = ioread32be(®s->imask);
351 iowrite32be(tmp, ®s->imask);
354 static int init(struct memac_regs __iomem *regs, struct memac_cfg *cfg,
361 if (cfg->promiscuous_mode_enable)
362 tmp |= CMD_CFG_PROMIS_EN;
363 if (cfg->pause_ignore)
364 tmp |= CMD_CFG_PAUSE_IGNORE;
366 /* Payload length check disable */
367 tmp |= CMD_CFG_NO_LEN_CHK;
368 /* Enable padding of frames in transmit direction */
369 tmp |= CMD_CFG_TX_PAD_EN;
371 tmp |= CMD_CFG_CRC_FWD;
373 iowrite32be(tmp, ®s->command_config);
375 /* Max Frame Length */
376 iowrite32be((u32)cfg->max_frame_length, ®s->maxfrm);
379 iowrite32be((u32)cfg->pause_quanta, ®s->pause_quanta[0]);
380 iowrite32be((u32)0, ®s->pause_thresh[0]);
382 /* clear all pending events and set-up interrupts */
383 iowrite32be(0xffffffff, ®s->ievent);
384 set_exception(regs, exceptions, true);
389 static void set_dflts(struct memac_cfg *cfg)
391 cfg->reset_on_init = false;
392 cfg->promiscuous_mode_enable = false;
393 cfg->pause_ignore = false;
394 cfg->tx_ipg_length = DEFAULT_TX_IPG_LENGTH;
395 cfg->max_frame_length = DEFAULT_FRAME_LENGTH;
396 cfg->pause_quanta = DEFAULT_PAUSE_QUANTA;
399 static u32 get_mac_addr_hash_code(u64 eth_addr)
405 for (i = 0; i < 6; i++) {
406 mask1 = eth_addr & (u64)0x01;
409 for (j = 0; j < 7; j++) {
410 mask2 = eth_addr & (u64)0x01;
415 xor_val |= (mask1 << (5 - i));
421 static int check_init_parameters(struct fman_mac *memac)
423 if (!memac->exception_cb) {
424 pr_err("Uninitialized exception handler\n");
427 if (!memac->event_cb) {
428 pr_warn("Uninitialize event handler\n");
435 static int get_exception_flag(enum fman_mac_exceptions exception)
440 case FM_MAC_EX_10G_TX_ECC_ER:
441 bit_mask = MEMAC_IMASK_TECC_ER;
443 case FM_MAC_EX_10G_RX_ECC_ER:
444 bit_mask = MEMAC_IMASK_RECC_ER;
446 case FM_MAC_EX_TS_FIFO_ECC_ERR:
447 bit_mask = MEMAC_IMASK_TSECC_ER;
449 case FM_MAC_EX_MAGIC_PACKET_INDICATION:
450 bit_mask = MEMAC_IMASK_MGI;
460 static void memac_err_exception(void *handle)
462 struct fman_mac *memac = (struct fman_mac *)handle;
463 struct memac_regs __iomem *regs = memac->regs;
466 event = ioread32be(®s->ievent);
467 imask = ioread32be(®s->imask);
469 /* Imask include both error and notification/event bits.
470 * Leaving only error bits enabled by imask.
471 * The imask error bits are shifted by 16 bits offset from
472 * their corresponding location in the ievent - hence the >> 16
474 event &= ((imask & MEMAC_ALL_ERRS_IMASK) >> 16);
476 iowrite32be(event, ®s->ievent);
478 if (event & MEMAC_IEVNT_TS_ECC_ER)
479 memac->exception_cb(memac->dev_id, FM_MAC_EX_TS_FIFO_ECC_ERR);
480 if (event & MEMAC_IEVNT_TX_ECC_ER)
481 memac->exception_cb(memac->dev_id, FM_MAC_EX_10G_TX_ECC_ER);
482 if (event & MEMAC_IEVNT_RX_ECC_ER)
483 memac->exception_cb(memac->dev_id, FM_MAC_EX_10G_RX_ECC_ER);
486 static void memac_exception(void *handle)
488 struct fman_mac *memac = (struct fman_mac *)handle;
489 struct memac_regs __iomem *regs = memac->regs;
492 event = ioread32be(®s->ievent);
493 imask = ioread32be(®s->imask);
495 /* Imask include both error and notification/event bits.
496 * Leaving only error bits enabled by imask.
497 * The imask error bits are shifted by 16 bits offset from
498 * their corresponding location in the ievent - hence the >> 16
500 event &= ((imask & MEMAC_ALL_ERRS_IMASK) >> 16);
502 iowrite32be(event, ®s->ievent);
504 if (event & MEMAC_IEVNT_MGI)
505 memac->exception_cb(memac->dev_id,
506 FM_MAC_EX_MAGIC_PACKET_INDICATION);
509 static void free_init_resources(struct fman_mac *memac)
511 fman_unregister_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
514 fman_unregister_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
515 FMAN_INTR_TYPE_NORMAL);
517 /* release the driver's group hash table */
518 free_hash_table(memac->multicast_addr_hash);
519 memac->multicast_addr_hash = NULL;
521 /* release the driver's individual hash table */
522 free_hash_table(memac->unicast_addr_hash);
523 memac->unicast_addr_hash = NULL;
526 static int memac_enable(struct fman_mac *memac)
530 ret = phy_init(memac->serdes);
532 dev_err(memac->dev_id->dev,
533 "could not initialize serdes: %pe\n", ERR_PTR(ret));
537 ret = phy_power_on(memac->serdes);
539 dev_err(memac->dev_id->dev,
540 "could not power on serdes: %pe\n", ERR_PTR(ret));
541 phy_exit(memac->serdes);
547 static void memac_disable(struct fman_mac *memac)
549 phy_power_off(memac->serdes);
550 phy_exit(memac->serdes);
553 static int memac_set_promiscuous(struct fman_mac *memac, bool new_val)
555 struct memac_regs __iomem *regs = memac->regs;
558 tmp = ioread32be(®s->command_config);
560 tmp |= CMD_CFG_PROMIS_EN;
562 tmp &= ~CMD_CFG_PROMIS_EN;
564 iowrite32be(tmp, ®s->command_config);
569 static int memac_set_tx_pause_frames(struct fman_mac *memac, u8 priority,
570 u16 pause_time, u16 thresh_time)
572 struct memac_regs __iomem *regs = memac->regs;
575 tmp = ioread32be(®s->tx_fifo_sections);
577 GET_TX_EMPTY_DEFAULT_VALUE(tmp);
578 iowrite32be(tmp, ®s->tx_fifo_sections);
580 tmp = ioread32be(®s->command_config);
581 tmp &= ~CMD_CFG_PFC_MODE;
583 iowrite32be(tmp, ®s->command_config);
585 tmp = ioread32be(®s->pause_quanta[priority / 2]);
587 tmp &= CLXY_PAUSE_QUANTA_CLX_PQNT;
589 tmp &= CLXY_PAUSE_QUANTA_CLY_PQNT;
590 tmp |= ((u32)pause_time << (16 * (priority % 2)));
591 iowrite32be(tmp, ®s->pause_quanta[priority / 2]);
593 tmp = ioread32be(®s->pause_thresh[priority / 2]);
595 tmp &= CLXY_PAUSE_THRESH_CLX_QTH;
597 tmp &= CLXY_PAUSE_THRESH_CLY_QTH;
598 tmp |= ((u32)thresh_time << (16 * (priority % 2)));
599 iowrite32be(tmp, ®s->pause_thresh[priority / 2]);
604 static int memac_accept_rx_pause_frames(struct fman_mac *memac, bool en)
606 struct memac_regs __iomem *regs = memac->regs;
609 tmp = ioread32be(®s->command_config);
611 tmp &= ~CMD_CFG_PAUSE_IGNORE;
613 tmp |= CMD_CFG_PAUSE_IGNORE;
615 iowrite32be(tmp, ®s->command_config);
620 static unsigned long memac_get_caps(struct phylink_config *config,
621 phy_interface_t interface)
623 struct fman_mac *memac = fman_config_to_mac(config)->fman_mac;
624 unsigned long caps = config->mac_capabilities;
626 if (phy_interface_mode_is_rgmii(interface) &&
627 memac->rgmii_no_half_duplex)
628 caps &= ~(MAC_10HD | MAC_100HD);
634 * memac_if_mode() - Convert an interface mode into an IF_MODE config
635 * @interface: A phy interface mode
637 * Return: A configuration word, suitable for programming into the lower bits
640 static u32 memac_if_mode(phy_interface_t interface)
643 case PHY_INTERFACE_MODE_MII:
645 case PHY_INTERFACE_MODE_RGMII:
646 case PHY_INTERFACE_MODE_RGMII_ID:
647 case PHY_INTERFACE_MODE_RGMII_RXID:
648 case PHY_INTERFACE_MODE_RGMII_TXID:
649 return IF_MODE_GMII | IF_MODE_RGMII;
650 case PHY_INTERFACE_MODE_SGMII:
651 case PHY_INTERFACE_MODE_1000BASEX:
652 case PHY_INTERFACE_MODE_QSGMII:
654 case PHY_INTERFACE_MODE_10GBASER:
662 static struct phylink_pcs *memac_select_pcs(struct phylink_config *config,
663 phy_interface_t iface)
665 struct fman_mac *memac = fman_config_to_mac(config)->fman_mac;
668 case PHY_INTERFACE_MODE_SGMII:
669 case PHY_INTERFACE_MODE_1000BASEX:
670 return memac->sgmii_pcs;
671 case PHY_INTERFACE_MODE_QSGMII:
672 return memac->qsgmii_pcs;
673 case PHY_INTERFACE_MODE_10GBASER:
674 return memac->xfi_pcs;
680 static int memac_prepare(struct phylink_config *config, unsigned int mode,
681 phy_interface_t iface)
683 struct fman_mac *memac = fman_config_to_mac(config)->fman_mac;
686 case PHY_INTERFACE_MODE_SGMII:
687 case PHY_INTERFACE_MODE_1000BASEX:
688 case PHY_INTERFACE_MODE_QSGMII:
689 case PHY_INTERFACE_MODE_10GBASER:
690 return phy_set_mode_ext(memac->serdes, PHY_MODE_ETHERNET,
697 static void memac_mac_config(struct phylink_config *config, unsigned int mode,
698 const struct phylink_link_state *state)
700 struct mac_device *mac_dev = fman_config_to_mac(config);
701 struct memac_regs __iomem *regs = mac_dev->fman_mac->regs;
702 u32 tmp = ioread32be(®s->if_mode);
704 tmp &= ~(IF_MODE_MASK | IF_MODE_RGMII);
705 tmp |= memac_if_mode(state->interface);
706 if (phylink_autoneg_inband(mode))
707 tmp |= IF_MODE_RGMII_AUTO;
708 iowrite32be(tmp, ®s->if_mode);
711 static void memac_link_up(struct phylink_config *config, struct phy_device *phy,
712 unsigned int mode, phy_interface_t interface,
713 int speed, int duplex, bool tx_pause, bool rx_pause)
715 struct mac_device *mac_dev = fman_config_to_mac(config);
716 struct fman_mac *memac = mac_dev->fman_mac;
717 struct memac_regs __iomem *regs = memac->regs;
718 u32 tmp = memac_if_mode(interface);
719 u16 pause_time = tx_pause ? FSL_FM_PAUSE_TIME_ENABLE :
720 FSL_FM_PAUSE_TIME_DISABLE;
722 memac_set_tx_pause_frames(memac, 0, pause_time, 0);
723 memac_accept_rx_pause_frames(memac, rx_pause);
725 if (duplex == DUPLEX_HALF)
730 tmp |= IF_MODE_RGMII_1000;
733 tmp |= IF_MODE_RGMII_100;
736 tmp |= IF_MODE_RGMII_10;
739 iowrite32be(tmp, ®s->if_mode);
743 if (speed == SPEED_10000) {
744 if (memac->fm_rev_info.major == 6 &&
745 memac->fm_rev_info.minor == 4)
746 tmp = TX_FIFO_SECTIONS_TX_AVAIL_SLOW_10G;
748 tmp = TX_FIFO_SECTIONS_TX_AVAIL_10G;
749 tmp |= TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_10G;
751 tmp = TX_FIFO_SECTIONS_TX_AVAIL_1G |
752 TX_FIFO_SECTIONS_TX_EMPTY_DEFAULT_1G;
754 iowrite32be(tmp, ®s->tx_fifo_sections);
756 mac_dev->update_speed(mac_dev, speed);
758 tmp = ioread32be(®s->command_config);
759 tmp |= CMD_CFG_RX_EN | CMD_CFG_TX_EN;
760 iowrite32be(tmp, ®s->command_config);
763 static void memac_link_down(struct phylink_config *config, unsigned int mode,
764 phy_interface_t interface)
766 struct fman_mac *memac = fman_config_to_mac(config)->fman_mac;
767 struct memac_regs __iomem *regs = memac->regs;
771 tmp = ioread32be(®s->command_config);
772 tmp &= ~(CMD_CFG_RX_EN | CMD_CFG_TX_EN);
773 iowrite32be(tmp, ®s->command_config);
776 static const struct phylink_mac_ops memac_mac_ops = {
777 .mac_get_caps = memac_get_caps,
778 .mac_select_pcs = memac_select_pcs,
779 .mac_prepare = memac_prepare,
780 .mac_config = memac_mac_config,
781 .mac_link_up = memac_link_up,
782 .mac_link_down = memac_link_down,
785 static int memac_modify_mac_address(struct fman_mac *memac,
786 const enet_addr_t *enet_addr)
788 add_addr_in_paddr(memac->regs, (const u8 *)(*enet_addr), 0);
793 static int memac_add_hash_mac_address(struct fman_mac *memac,
794 enet_addr_t *eth_addr)
796 struct memac_regs __iomem *regs = memac->regs;
797 struct eth_hash_entry *hash_entry;
801 addr = ENET_ADDR_TO_UINT64(*eth_addr);
803 if (!(addr & GROUP_ADDRESS)) {
804 /* Unicast addresses not supported in hash */
805 pr_err("Unicast Address\n");
808 hash = get_mac_addr_hash_code(addr) & HASH_CTRL_ADDR_MASK;
810 /* Create element to be added to the driver hash table */
811 hash_entry = kmalloc(sizeof(*hash_entry), GFP_ATOMIC);
814 hash_entry->addr = addr;
815 INIT_LIST_HEAD(&hash_entry->node);
817 list_add_tail(&hash_entry->node,
818 &memac->multicast_addr_hash->lsts[hash]);
819 iowrite32be(hash | HASH_CTRL_MCAST_EN, ®s->hashtable_ctrl);
824 static int memac_set_allmulti(struct fman_mac *memac, bool enable)
827 struct memac_regs __iomem *regs = memac->regs;
830 for (entry = 0; entry < HASH_TABLE_SIZE; entry++)
831 iowrite32be(entry | HASH_CTRL_MCAST_EN,
832 ®s->hashtable_ctrl);
834 for (entry = 0; entry < HASH_TABLE_SIZE; entry++)
835 iowrite32be(entry & ~HASH_CTRL_MCAST_EN,
836 ®s->hashtable_ctrl);
839 memac->allmulti_enabled = enable;
844 static int memac_set_tstamp(struct fman_mac *memac, bool enable)
846 return 0; /* Always enabled. */
849 static int memac_del_hash_mac_address(struct fman_mac *memac,
850 enet_addr_t *eth_addr)
852 struct memac_regs __iomem *regs = memac->regs;
853 struct eth_hash_entry *hash_entry = NULL;
854 struct list_head *pos;
858 addr = ENET_ADDR_TO_UINT64(*eth_addr);
860 hash = get_mac_addr_hash_code(addr) & HASH_CTRL_ADDR_MASK;
862 list_for_each(pos, &memac->multicast_addr_hash->lsts[hash]) {
863 hash_entry = ETH_HASH_ENTRY_OBJ(pos);
864 if (hash_entry && hash_entry->addr == addr) {
865 list_del_init(&hash_entry->node);
871 if (!memac->allmulti_enabled) {
872 if (list_empty(&memac->multicast_addr_hash->lsts[hash]))
873 iowrite32be(hash & ~HASH_CTRL_MCAST_EN,
874 ®s->hashtable_ctrl);
880 static int memac_set_exception(struct fman_mac *memac,
881 enum fman_mac_exceptions exception, bool enable)
885 bit_mask = get_exception_flag(exception);
888 memac->exceptions |= bit_mask;
890 memac->exceptions &= ~bit_mask;
892 pr_err("Undefined exception\n");
895 set_exception(memac->regs, bit_mask, enable);
900 static int memac_init(struct fman_mac *memac)
902 struct memac_cfg *memac_drv_param;
903 enet_addr_t eth_addr;
907 err = check_init_parameters(memac);
911 memac_drv_param = memac->memac_drv_param;
913 /* First, reset the MAC if desired. */
914 if (memac_drv_param->reset_on_init) {
915 err = reset(memac->regs);
917 pr_err("mEMAC reset failed\n");
923 if (memac->addr != 0) {
924 MAKE_ENET_ADDR_FROM_UINT64(memac->addr, eth_addr);
925 add_addr_in_paddr(memac->regs, (const u8 *)eth_addr, 0);
928 init(memac->regs, memac->memac_drv_param, memac->exceptions);
930 /* FM_RX_FIFO_CORRUPT_ERRATA_10GMAC_A006320 errata workaround
931 * Exists only in FMan 6.0 and 6.3.
933 if ((memac->fm_rev_info.major == 6) &&
934 ((memac->fm_rev_info.minor == 0) ||
935 (memac->fm_rev_info.minor == 3))) {
936 /* MAC strips CRC from received frames - this workaround
937 * should decrease the likelihood of bug appearance
939 reg32 = ioread32be(&memac->regs->command_config);
940 reg32 &= ~CMD_CFG_CRC_FWD;
941 iowrite32be(reg32, &memac->regs->command_config);
944 /* Max Frame Length */
945 err = fman_set_mac_max_frame(memac->fm, memac->mac_id,
946 memac_drv_param->max_frame_length);
948 pr_err("settings Mac max frame length is FAILED\n");
952 memac->multicast_addr_hash = alloc_hash_table(HASH_TABLE_SIZE);
953 if (!memac->multicast_addr_hash) {
954 free_init_resources(memac);
955 pr_err("allocation hash table is FAILED\n");
959 memac->unicast_addr_hash = alloc_hash_table(HASH_TABLE_SIZE);
960 if (!memac->unicast_addr_hash) {
961 free_init_resources(memac);
962 pr_err("allocation hash table is FAILED\n");
966 fman_register_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
967 FMAN_INTR_TYPE_ERR, memac_err_exception, memac);
969 fman_register_intr(memac->fm, FMAN_MOD_MAC, memac->mac_id,
970 FMAN_INTR_TYPE_NORMAL, memac_exception, memac);
975 static void pcs_put(struct phylink_pcs *pcs)
977 if (IS_ERR_OR_NULL(pcs))
980 lynx_pcs_destroy(pcs);
983 static int memac_free(struct fman_mac *memac)
985 free_init_resources(memac);
987 pcs_put(memac->sgmii_pcs);
988 pcs_put(memac->qsgmii_pcs);
989 pcs_put(memac->xfi_pcs);
990 kfree(memac->memac_drv_param);
996 static struct fman_mac *memac_config(struct mac_device *mac_dev,
997 struct fman_mac_params *params)
999 struct fman_mac *memac;
1000 struct memac_cfg *memac_drv_param;
1002 /* allocate memory for the m_emac data structure */
1003 memac = kzalloc(sizeof(*memac), GFP_KERNEL);
1007 /* allocate memory for the m_emac driver parameters data structure */
1008 memac_drv_param = kzalloc(sizeof(*memac_drv_param), GFP_KERNEL);
1009 if (!memac_drv_param) {
1014 /* Plant parameter structure pointer */
1015 memac->memac_drv_param = memac_drv_param;
1017 set_dflts(memac_drv_param);
1019 memac->addr = ENET_ADDR_TO_UINT64(mac_dev->addr);
1021 memac->regs = mac_dev->vaddr;
1022 memac->mac_id = params->mac_id;
1023 memac->exceptions = (MEMAC_IMASK_TSECC_ER | MEMAC_IMASK_TECC_ER |
1024 MEMAC_IMASK_RECC_ER | MEMAC_IMASK_MGI);
1025 memac->exception_cb = params->exception_cb;
1026 memac->event_cb = params->event_cb;
1027 memac->dev_id = mac_dev;
1028 memac->fm = params->fm;
1030 /* Save FMan revision */
1031 fman_get_revision(memac->fm, &memac->fm_rev_info);
1036 static struct phylink_pcs *memac_pcs_create(struct device_node *mac_node,
1039 struct device_node *node;
1040 struct phylink_pcs *pcs;
1042 node = of_parse_phandle(mac_node, "pcsphy-handle", index);
1044 return ERR_PTR(-ENODEV);
1046 pcs = lynx_pcs_create_fwnode(of_fwnode_handle(node));
1052 static bool memac_supports(struct mac_device *mac_dev, phy_interface_t iface)
1054 /* If there's no serdes device, assume that it's been configured for
1055 * whatever the default interface mode is.
1057 if (!mac_dev->fman_mac->serdes)
1058 return mac_dev->phy_if == iface;
1059 /* Otherwise, ask the serdes */
1060 return !phy_validate(mac_dev->fman_mac->serdes, PHY_MODE_ETHERNET,
1064 int memac_initialization(struct mac_device *mac_dev,
1065 struct device_node *mac_node,
1066 struct fman_mac_params *params)
1069 struct phylink_pcs *pcs;
1070 struct fman_mac *memac;
1071 unsigned long capabilities;
1072 unsigned long *supported;
1074 /* The internal connection to the serdes is XGMII, but this isn't
1075 * really correct for the phy mode (which is the external connection).
1076 * However, this is how all older device trees say that they want
1077 * 10GBASE-R (aka XFI), so just convert it for them.
1079 if (mac_dev->phy_if == PHY_INTERFACE_MODE_XGMII)
1080 mac_dev->phy_if = PHY_INTERFACE_MODE_10GBASER;
1082 mac_dev->phylink_ops = &memac_mac_ops;
1083 mac_dev->set_promisc = memac_set_promiscuous;
1084 mac_dev->change_addr = memac_modify_mac_address;
1085 mac_dev->add_hash_mac_addr = memac_add_hash_mac_address;
1086 mac_dev->remove_hash_mac_addr = memac_del_hash_mac_address;
1087 mac_dev->set_exception = memac_set_exception;
1088 mac_dev->set_allmulti = memac_set_allmulti;
1089 mac_dev->set_tstamp = memac_set_tstamp;
1090 mac_dev->enable = memac_enable;
1091 mac_dev->disable = memac_disable;
1093 mac_dev->fman_mac = memac_config(mac_dev, params);
1094 if (!mac_dev->fman_mac)
1097 memac = mac_dev->fman_mac;
1098 memac->memac_drv_param->max_frame_length = fman_get_max_frm();
1099 memac->memac_drv_param->reset_on_init = true;
1101 err = of_property_match_string(mac_node, "pcs-handle-names", "xfi");
1103 memac->xfi_pcs = memac_pcs_create(mac_node, err);
1104 if (IS_ERR(memac->xfi_pcs)) {
1105 err = PTR_ERR(memac->xfi_pcs);
1106 dev_err_probe(mac_dev->dev, err, "missing xfi pcs\n");
1107 goto _return_fm_mac_free;
1109 } else if (err != -EINVAL && err != -ENODATA) {
1110 goto _return_fm_mac_free;
1113 err = of_property_match_string(mac_node, "pcs-handle-names", "qsgmii");
1115 memac->qsgmii_pcs = memac_pcs_create(mac_node, err);
1116 if (IS_ERR(memac->qsgmii_pcs)) {
1117 err = PTR_ERR(memac->qsgmii_pcs);
1118 dev_err_probe(mac_dev->dev, err,
1119 "missing qsgmii pcs\n");
1120 goto _return_fm_mac_free;
1122 } else if (err != -EINVAL && err != -ENODATA) {
1123 goto _return_fm_mac_free;
1126 /* For compatibility, if pcs-handle-names is missing, we assume this
1127 * phy is the first one in pcsphy-handle
1129 err = of_property_match_string(mac_node, "pcs-handle-names", "sgmii");
1130 if (err == -EINVAL || err == -ENODATA)
1131 pcs = memac_pcs_create(mac_node, 0);
1133 goto _return_fm_mac_free;
1135 pcs = memac_pcs_create(mac_node, err);
1139 dev_err_probe(mac_dev->dev, err, "missing pcs\n");
1140 goto _return_fm_mac_free;
1143 /* If err is set here, it means that pcs-handle-names was missing above
1144 * (and therefore that xfi_pcs cannot be set). If we are defaulting to
1145 * XGMII, assume this is for XFI. Otherwise, assume it is for SGMII.
1147 if (err && mac_dev->phy_if == PHY_INTERFACE_MODE_10GBASER)
1148 memac->xfi_pcs = pcs;
1150 memac->sgmii_pcs = pcs;
1152 memac->serdes = devm_of_phy_optional_get(mac_dev->dev, mac_node,
1154 if (!memac->serdes) {
1155 dev_dbg(mac_dev->dev, "could not get (optional) serdes\n");
1156 } else if (IS_ERR(memac->serdes)) {
1157 err = PTR_ERR(memac->serdes);
1158 goto _return_fm_mac_free;
1161 /* TODO: The following interface modes are supported by (some) hardware
1162 * but not by this driver:
1167 supported = mac_dev->phylink_config.supported_interfaces;
1169 /* Note that half duplex is only supported on 10/100M interfaces. */
1171 if (memac->sgmii_pcs &&
1172 (memac_supports(mac_dev, PHY_INTERFACE_MODE_SGMII) ||
1173 memac_supports(mac_dev, PHY_INTERFACE_MODE_1000BASEX))) {
1174 __set_bit(PHY_INTERFACE_MODE_SGMII, supported);
1175 __set_bit(PHY_INTERFACE_MODE_1000BASEX, supported);
1178 if (memac->sgmii_pcs &&
1179 memac_supports(mac_dev, PHY_INTERFACE_MODE_2500BASEX))
1180 __set_bit(PHY_INTERFACE_MODE_2500BASEX, supported);
1182 if (memac->qsgmii_pcs &&
1183 memac_supports(mac_dev, PHY_INTERFACE_MODE_QSGMII))
1184 __set_bit(PHY_INTERFACE_MODE_QSGMII, supported);
1185 else if (mac_dev->phy_if == PHY_INTERFACE_MODE_QSGMII)
1186 dev_warn(mac_dev->dev, "no QSGMII pcs specified\n");
1188 if (memac->xfi_pcs &&
1189 memac_supports(mac_dev, PHY_INTERFACE_MODE_10GBASER)) {
1190 __set_bit(PHY_INTERFACE_MODE_10GBASER, supported);
1192 /* From what I can tell, no 10g macs support RGMII. */
1193 phy_interface_set_rgmii(supported);
1194 __set_bit(PHY_INTERFACE_MODE_MII, supported);
1197 capabilities = MAC_SYM_PAUSE | MAC_ASYM_PAUSE | MAC_10 | MAC_100;
1198 capabilities |= MAC_1000FD | MAC_2500FD | MAC_10000FD;
1200 /* These SoCs don't support half duplex at all; there's no different
1201 * FMan version or compatible, so we just have to check the machine
1202 * compatible instead
1204 if (of_machine_is_compatible("fsl,ls1043a") ||
1205 of_machine_is_compatible("fsl,ls1046a") ||
1206 of_machine_is_compatible("fsl,B4QDS"))
1207 capabilities &= ~(MAC_10HD | MAC_100HD);
1209 mac_dev->phylink_config.mac_capabilities = capabilities;
1211 /* The T2080 and T4240 don't support half duplex RGMII. There is no
1212 * other way to identify these SoCs, so just use the machine
1215 if (of_machine_is_compatible("fsl,T2080QDS") ||
1216 of_machine_is_compatible("fsl,T2080RDB") ||
1217 of_machine_is_compatible("fsl,T2081QDS") ||
1218 of_machine_is_compatible("fsl,T4240QDS") ||
1219 of_machine_is_compatible("fsl,T4240RDB"))
1220 memac->rgmii_no_half_duplex = true;
1222 /* Most boards should use MLO_AN_INBAND, but existing boards don't have
1223 * a managed property. Default to MLO_AN_INBAND rather than MLO_AN_PHY.
1224 * Phylink will allow this to be overriden by a fixed link. We need to
1225 * be careful and not enable this if we are using MII or RGMII, since
1226 * those configurations modes don't use in-band autonegotiation.
1228 if (!of_property_read_bool(mac_node, "managed") &&
1229 mac_dev->phy_if != PHY_INTERFACE_MODE_MII &&
1230 !phy_interface_mode_is_rgmii(mac_dev->phy_if))
1231 mac_dev->phylink_config.default_an_inband = true;
1233 err = memac_init(mac_dev->fman_mac);
1235 goto _return_fm_mac_free;
1237 dev_info(mac_dev->dev, "FMan MEMAC\n");
1241 _return_fm_mac_free:
1242 memac_free(mac_dev->fman_mac);