Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2012 Freescale Semiconductor, Inc. |
| 4 | * Roy Zang <tie-fei.zang@freescale.com> |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* MAXFRM - maximum frame length */ |
| 8 | #define MAXFRM_MASK 0x0000ffff |
| 9 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 11 | #include <phy.h> |
| 12 | #include <asm/types.h> |
| 13 | #include <asm/io.h> |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 14 | #include <fsl_memac.h> |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 15 | |
| 16 | #include "fm.h" |
| 17 | |
| 18 | static void memac_init_mac(struct fsl_enet_mac *mac) |
| 19 | { |
| 20 | struct memac *regs = mac->base; |
| 21 | |
| 22 | /* mask all interrupt */ |
| 23 | out_be32(®s->imask, IMASK_MASK_ALL); |
| 24 | |
| 25 | /* clear all events */ |
| 26 | out_be32(®s->ievent, IEVENT_CLEAR_ALL); |
| 27 | |
| 28 | /* set the max receive length */ |
| 29 | out_be32(®s->maxfrm, mac->max_rx_len & MAXFRM_MASK); |
| 30 | |
| 31 | /* multicast frame reception for the hash entry disable */ |
| 32 | out_be32(®s->hashtable_ctrl, 0); |
| 33 | } |
| 34 | |
| 35 | static void memac_enable_mac(struct fsl_enet_mac *mac) |
| 36 | { |
| 37 | struct memac *regs = mac->base; |
| 38 | |
Shaohui Xie | 182fea2 | 2014-08-13 18:32:19 +0800 | [diff] [blame] | 39 | setbits_be32(®s->command_config, |
| 40 | MEMAC_CMD_CFG_RXTX_EN | MEMAC_CMD_CFG_NO_LEN_CHK); |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static void memac_disable_mac(struct fsl_enet_mac *mac) |
| 44 | { |
| 45 | struct memac *regs = mac->base; |
| 46 | |
| 47 | clrbits_be32(®s->command_config, MEMAC_CMD_CFG_RXTX_EN); |
| 48 | } |
| 49 | |
| 50 | static void memac_set_mac_addr(struct fsl_enet_mac *mac, u8 *mac_addr) |
| 51 | { |
| 52 | struct memac *regs = mac->base; |
| 53 | u32 mac_addr0, mac_addr1; |
| 54 | |
| 55 | /* |
| 56 | * if a station address of 0x12345678ABCD, perform a write to |
| 57 | * MAC_ADDR0 of 0x78563412, MAC_ADDR1 of 0x0000CDAB |
| 58 | */ |
| 59 | mac_addr0 = (mac_addr[3] << 24) | (mac_addr[2] << 16) | \ |
| 60 | (mac_addr[1] << 8) | (mac_addr[0]); |
| 61 | out_be32(®s->mac_addr_0, mac_addr0); |
| 62 | |
| 63 | mac_addr1 = ((mac_addr[5] << 8) | mac_addr[4]) & 0x0000ffff; |
| 64 | out_be32(®s->mac_addr_1, mac_addr1); |
| 65 | } |
| 66 | |
| 67 | static void memac_set_interface_mode(struct fsl_enet_mac *mac, |
| 68 | phy_interface_t type, int speed) |
| 69 | { |
| 70 | /* Roy need more work here */ |
| 71 | |
| 72 | struct memac *regs = mac->base; |
| 73 | u32 if_mode, if_status; |
| 74 | |
| 75 | /* clear all bits relative with interface mode */ |
| 76 | if_mode = in_be32(®s->if_mode); |
| 77 | if_status = in_be32(®s->if_status); |
| 78 | |
| 79 | /* set interface mode */ |
| 80 | switch (type) { |
| 81 | case PHY_INTERFACE_MODE_GMII: |
| 82 | if_mode &= ~IF_MODE_MASK; |
| 83 | if_mode |= IF_MODE_GMII; |
| 84 | break; |
| 85 | case PHY_INTERFACE_MODE_RGMII: |
Madalin Bucur | fa8f6b4 | 2020-03-12 14:53:44 +0200 | [diff] [blame] | 86 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 87 | case PHY_INTERFACE_MODE_RGMII_RXID: |
Madalin Bucur | 25122b4 | 2017-08-04 09:14:53 +0300 | [diff] [blame] | 88 | case PHY_INTERFACE_MODE_RGMII_TXID: |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 89 | if_mode |= (IF_MODE_GMII | IF_MODE_RG); |
| 90 | break; |
| 91 | case PHY_INTERFACE_MODE_RMII: |
| 92 | if_mode |= (IF_MODE_GMII | IF_MODE_RM); |
| 93 | break; |
| 94 | case PHY_INTERFACE_MODE_SGMII: |
Vladimir Oltean | 6caef97 | 2021-09-18 15:32:35 +0300 | [diff] [blame] | 95 | case PHY_INTERFACE_MODE_2500BASEX: |
Shaohui Xie | c218d29 | 2013-08-19 18:58:52 +0800 | [diff] [blame] | 96 | case PHY_INTERFACE_MODE_QSGMII: |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 97 | if_mode &= ~IF_MODE_MASK; |
| 98 | if_mode |= (IF_MODE_GMII); |
| 99 | break; |
Vladimir Oltean | 6a6e402 | 2021-09-18 15:32:34 +0300 | [diff] [blame] | 100 | case PHY_INTERFACE_MODE_10GBASER: |
Shaohui Xie | 182fea2 | 2014-08-13 18:32:19 +0800 | [diff] [blame] | 101 | case PHY_INTERFACE_MODE_XGMII: |
| 102 | if_mode &= ~IF_MODE_MASK; |
| 103 | if_mode |= IF_MODE_XGMII; |
| 104 | break; |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 105 | default: |
| 106 | break; |
| 107 | } |
Shaohui Xie | 182fea2 | 2014-08-13 18:32:19 +0800 | [diff] [blame] | 108 | /* Enable automatic speed selection for Non-XGMII */ |
Vladimir Oltean | 6a6e402 | 2021-09-18 15:32:34 +0300 | [diff] [blame] | 109 | if (type != PHY_INTERFACE_MODE_XGMII && type != PHY_INTERFACE_MODE_10GBASER) |
Shaohui Xie | 182fea2 | 2014-08-13 18:32:19 +0800 | [diff] [blame] | 110 | if_mode |= IF_MODE_EN_AUTO; |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 111 | |
Madalin Bucur | 25122b4 | 2017-08-04 09:14:53 +0300 | [diff] [blame] | 112 | if (type == PHY_INTERFACE_MODE_RGMII || |
Madalin Bucur | fa8f6b4 | 2020-03-12 14:53:44 +0200 | [diff] [blame] | 113 | type == PHY_INTERFACE_MODE_RGMII_ID || |
| 114 | type == PHY_INTERFACE_MODE_RGMII_RXID || |
Madalin Bucur | 25122b4 | 2017-08-04 09:14:53 +0300 | [diff] [blame] | 115 | type == PHY_INTERFACE_MODE_RGMII_TXID) { |
Zang Roy-R61911 | d6615fe | 2013-03-04 03:59:20 +0000 | [diff] [blame] | 116 | if_mode &= ~IF_MODE_EN_AUTO; |
| 117 | if_mode &= ~IF_MODE_SETSP_MASK; |
| 118 | switch (speed) { |
| 119 | case SPEED_1000: |
| 120 | if_mode |= IF_MODE_SETSP_1000M; |
| 121 | break; |
| 122 | case SPEED_100: |
| 123 | if_mode |= IF_MODE_SETSP_100M; |
| 124 | break; |
| 125 | case SPEED_10: |
| 126 | if_mode |= IF_MODE_SETSP_10M; |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 132 | debug(" %s, if_mode = %x\n", __func__, if_mode); |
| 133 | debug(" %s, if_status = %x\n", __func__, if_status); |
| 134 | out_be32(®s->if_mode, if_mode); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | void init_memac(struct fsl_enet_mac *mac, void *base, |
| 139 | void *phyregs, int max_rx_len) |
| 140 | { |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 141 | debug("%s: @ %p, mdio @ %p\n", __func__, base, phyregs); |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 142 | mac->base = base; |
| 143 | mac->phyregs = phyregs; |
| 144 | mac->max_rx_len = max_rx_len; |
| 145 | mac->init_mac = memac_init_mac; |
| 146 | mac->enable_mac = memac_enable_mac; |
| 147 | mac->disable_mac = memac_disable_mac; |
| 148 | mac->set_mac_addr = memac_set_mac_addr; |
| 149 | mac->set_if_mode = memac_set_interface_mode; |
| 150 | } |