Grygorii Strashko | cbec53b | 2018-10-31 16:21:42 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 2 | /* |
| 3 | * CPSW Ethernet Switch Driver |
| 4 | * |
Grygorii Strashko | cbec53b | 2018-10-31 16:21:42 -0500 | [diff] [blame] | 5 | * Copyright (C) 2010-2018 Texas Instruments Incorporated - http://www.ti.com/ |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
| 10 | #include <net.h> |
| 11 | #include <miiphy.h> |
| 12 | #include <malloc.h> |
| 13 | #include <net.h> |
| 14 | #include <netdev.h> |
| 15 | #include <cpsw.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 16 | #include <linux/errno.h> |
Vignesh R | 2e205ef | 2016-08-02 10:14:27 +0530 | [diff] [blame] | 17 | #include <asm/gpio.h> |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 18 | #include <asm/io.h> |
| 19 | #include <phy.h> |
Tom Rini | 98f9200 | 2013-03-14 11:15:25 +0000 | [diff] [blame] | 20 | #include <asm/arch/cpu.h> |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 21 | #include <dm.h> |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 22 | #include <fdt_support.h> |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 23 | |
Grygorii Strashko | 4f41cd9 | 2018-10-31 16:21:44 -0500 | [diff] [blame] | 24 | #include "cpsw_mdio.h" |
| 25 | |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 26 | DECLARE_GLOBAL_DATA_PTR; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 27 | |
| 28 | #define BITMASK(bits) (BIT(bits) - 1) |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 29 | #define NUM_DESCS (PKTBUFSRX * 2) |
| 30 | #define PKT_MIN 60 |
| 31 | #define PKT_MAX (1500 + 14 + 4 + 4) |
| 32 | #define CLEAR_BIT 1 |
| 33 | #define GIGABITEN BIT(7) |
| 34 | #define FULLDUPLEXEN BIT(0) |
| 35 | #define MIIEN BIT(15) |
Grygorii Strashko | 60e81d0 | 2019-09-19 11:16:37 +0300 | [diff] [blame] | 36 | #define CTL_EXT_EN BIT(18) |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 37 | /* DMA Registers */ |
| 38 | #define CPDMA_TXCONTROL 0x004 |
| 39 | #define CPDMA_RXCONTROL 0x014 |
| 40 | #define CPDMA_SOFTRESET 0x01c |
| 41 | #define CPDMA_RXFREE 0x0e0 |
| 42 | #define CPDMA_TXHDP_VER1 0x100 |
| 43 | #define CPDMA_TXHDP_VER2 0x200 |
| 44 | #define CPDMA_RXHDP_VER1 0x120 |
| 45 | #define CPDMA_RXHDP_VER2 0x220 |
| 46 | #define CPDMA_TXCP_VER1 0x140 |
| 47 | #define CPDMA_TXCP_VER2 0x240 |
| 48 | #define CPDMA_RXCP_VER1 0x160 |
| 49 | #define CPDMA_RXCP_VER2 0x260 |
| 50 | |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 51 | /* Descriptor mode bits */ |
| 52 | #define CPDMA_DESC_SOP BIT(31) |
| 53 | #define CPDMA_DESC_EOP BIT(30) |
| 54 | #define CPDMA_DESC_OWNER BIT(29) |
| 55 | #define CPDMA_DESC_EOQ BIT(28) |
| 56 | |
| 57 | /* |
| 58 | * This timeout definition is a worst-case ultra defensive measure against |
| 59 | * unexpected controller lock ups. Ideally, we should never ever hit this |
| 60 | * scenario in practice. |
| 61 | */ |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 62 | #define CPDMA_TIMEOUT 100 /* msecs */ |
| 63 | |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 64 | struct cpsw_regs { |
| 65 | u32 id_ver; |
| 66 | u32 control; |
| 67 | u32 soft_reset; |
| 68 | u32 stat_port_en; |
| 69 | u32 ptype; |
| 70 | }; |
| 71 | |
| 72 | struct cpsw_slave_regs { |
| 73 | u32 max_blks; |
| 74 | u32 blk_cnt; |
| 75 | u32 flow_thresh; |
| 76 | u32 port_vlan; |
| 77 | u32 tx_pri_map; |
Matt Porter | f6f86a6 | 2013-03-20 05:38:12 +0000 | [diff] [blame] | 78 | #ifdef CONFIG_AM33XX |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 79 | u32 gap_thresh; |
Matt Porter | f6f86a6 | 2013-03-20 05:38:12 +0000 | [diff] [blame] | 80 | #elif defined(CONFIG_TI814X) |
| 81 | u32 ts_ctl; |
| 82 | u32 ts_seq_ltype; |
| 83 | u32 ts_vlan; |
| 84 | #endif |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 85 | u32 sa_lo; |
| 86 | u32 sa_hi; |
| 87 | }; |
| 88 | |
| 89 | struct cpsw_host_regs { |
| 90 | u32 max_blks; |
| 91 | u32 blk_cnt; |
| 92 | u32 flow_thresh; |
| 93 | u32 port_vlan; |
| 94 | u32 tx_pri_map; |
| 95 | u32 cpdma_tx_pri_map; |
| 96 | u32 cpdma_rx_chan_map; |
| 97 | }; |
| 98 | |
| 99 | struct cpsw_sliver_regs { |
| 100 | u32 id_ver; |
| 101 | u32 mac_control; |
| 102 | u32 mac_status; |
| 103 | u32 soft_reset; |
| 104 | u32 rx_maxlen; |
| 105 | u32 __reserved_0; |
| 106 | u32 rx_pause; |
| 107 | u32 tx_pause; |
| 108 | u32 __reserved_1; |
| 109 | u32 rx_pri_map; |
| 110 | }; |
| 111 | |
| 112 | #define ALE_ENTRY_BITS 68 |
| 113 | #define ALE_ENTRY_WORDS DIV_ROUND_UP(ALE_ENTRY_BITS, 32) |
| 114 | |
| 115 | /* ALE Registers */ |
| 116 | #define ALE_CONTROL 0x08 |
| 117 | #define ALE_UNKNOWNVLAN 0x18 |
| 118 | #define ALE_TABLE_CONTROL 0x20 |
| 119 | #define ALE_TABLE 0x34 |
| 120 | #define ALE_PORTCTL 0x40 |
| 121 | |
| 122 | #define ALE_TABLE_WRITE BIT(31) |
| 123 | |
| 124 | #define ALE_TYPE_FREE 0 |
| 125 | #define ALE_TYPE_ADDR 1 |
| 126 | #define ALE_TYPE_VLAN 2 |
| 127 | #define ALE_TYPE_VLAN_ADDR 3 |
| 128 | |
| 129 | #define ALE_UCAST_PERSISTANT 0 |
| 130 | #define ALE_UCAST_UNTOUCHED 1 |
| 131 | #define ALE_UCAST_OUI 2 |
| 132 | #define ALE_UCAST_TOUCHED 3 |
| 133 | |
| 134 | #define ALE_MCAST_FWD 0 |
| 135 | #define ALE_MCAST_BLOCK_LEARN_FWD 1 |
| 136 | #define ALE_MCAST_FWD_LEARN 2 |
| 137 | #define ALE_MCAST_FWD_2 3 |
| 138 | |
| 139 | enum cpsw_ale_port_state { |
| 140 | ALE_PORT_STATE_DISABLE = 0x00, |
| 141 | ALE_PORT_STATE_BLOCK = 0x01, |
| 142 | ALE_PORT_STATE_LEARN = 0x02, |
| 143 | ALE_PORT_STATE_FORWARD = 0x03, |
| 144 | }; |
| 145 | |
| 146 | /* ALE unicast entry flags - passed into cpsw_ale_add_ucast() */ |
| 147 | #define ALE_SECURE 1 |
| 148 | #define ALE_BLOCKED 2 |
| 149 | |
| 150 | struct cpsw_slave { |
| 151 | struct cpsw_slave_regs *regs; |
| 152 | struct cpsw_sliver_regs *sliver; |
| 153 | int slave_num; |
| 154 | u32 mac_control; |
| 155 | struct cpsw_slave_data *data; |
| 156 | }; |
| 157 | |
| 158 | struct cpdma_desc { |
| 159 | /* hardware fields */ |
| 160 | u32 hw_next; |
| 161 | u32 hw_buffer; |
| 162 | u32 hw_len; |
| 163 | u32 hw_mode; |
| 164 | /* software fields */ |
| 165 | u32 sw_buffer; |
| 166 | u32 sw_len; |
| 167 | }; |
| 168 | |
| 169 | struct cpdma_chan { |
| 170 | struct cpdma_desc *head, *tail; |
| 171 | void *hdp, *cp, *rxfree; |
| 172 | }; |
| 173 | |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 174 | /* AM33xx SoC specific definitions for the CONTROL port */ |
| 175 | #define AM33XX_GMII_SEL_MODE_MII 0 |
| 176 | #define AM33XX_GMII_SEL_MODE_RMII 1 |
| 177 | #define AM33XX_GMII_SEL_MODE_RGMII 2 |
| 178 | |
| 179 | #define AM33XX_GMII_SEL_RGMII1_IDMODE BIT(4) |
| 180 | #define AM33XX_GMII_SEL_RGMII2_IDMODE BIT(5) |
| 181 | #define AM33XX_GMII_SEL_RMII1_IO_CLK_EN BIT(6) |
| 182 | #define AM33XX_GMII_SEL_RMII2_IO_CLK_EN BIT(7) |
| 183 | |
| 184 | #define GMII_SEL_MODE_MASK 0x3 |
| 185 | |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 186 | #define desc_write(desc, fld, val) __raw_writel((u32)(val), &(desc)->fld) |
| 187 | #define desc_read(desc, fld) __raw_readl(&(desc)->fld) |
| 188 | #define desc_read_ptr(desc, fld) ((void *)__raw_readl(&(desc)->fld)) |
| 189 | |
| 190 | #define chan_write(chan, fld, val) __raw_writel((u32)(val), (chan)->fld) |
| 191 | #define chan_read(chan, fld) __raw_readl((chan)->fld) |
| 192 | #define chan_read_ptr(chan, fld) ((void *)__raw_readl((chan)->fld)) |
| 193 | |
Mugunthan V N | 7a02275 | 2014-05-22 14:37:10 +0530 | [diff] [blame] | 194 | #define for_active_slave(slave, priv) \ |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 195 | slave = (priv)->slaves + ((priv)->data)->active_slave; if (slave) |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 196 | #define for_each_slave(slave, priv) \ |
| 197 | for (slave = (priv)->slaves; slave != (priv)->slaves + \ |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 198 | ((priv)->data)->slaves; slave++) |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 199 | |
| 200 | struct cpsw_priv { |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 201 | #ifdef CONFIG_DM_ETH |
| 202 | struct udevice *dev; |
| 203 | #else |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 204 | struct eth_device *dev; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 205 | #endif |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 206 | struct cpsw_platform_data *data; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 207 | int host_port; |
| 208 | |
| 209 | struct cpsw_regs *regs; |
| 210 | void *dma_regs; |
| 211 | struct cpsw_host_regs *host_port_regs; |
| 212 | void *ale_regs; |
| 213 | |
| 214 | struct cpdma_desc *descs; |
| 215 | struct cpdma_desc *desc_free; |
| 216 | struct cpdma_chan rx_chan, tx_chan; |
| 217 | |
| 218 | struct cpsw_slave *slaves; |
| 219 | struct phy_device *phydev; |
| 220 | struct mii_dev *bus; |
Mugunthan V N | 48ec529 | 2013-02-19 21:34:44 +0000 | [diff] [blame] | 221 | |
Mugunthan V N | 48ec529 | 2013-02-19 21:34:44 +0000 | [diff] [blame] | 222 | u32 phy_mask; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits) |
| 226 | { |
| 227 | int idx; |
| 228 | |
| 229 | idx = start / 32; |
| 230 | start -= idx * 32; |
| 231 | idx = 2 - idx; /* flip */ |
| 232 | return (ale_entry[idx] >> start) & BITMASK(bits); |
| 233 | } |
| 234 | |
| 235 | static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits, |
| 236 | u32 value) |
| 237 | { |
| 238 | int idx; |
| 239 | |
| 240 | value &= BITMASK(bits); |
| 241 | idx = start / 32; |
| 242 | start -= idx * 32; |
| 243 | idx = 2 - idx; /* flip */ |
| 244 | ale_entry[idx] &= ~(BITMASK(bits) << start); |
| 245 | ale_entry[idx] |= (value << start); |
| 246 | } |
| 247 | |
| 248 | #define DEFINE_ALE_FIELD(name, start, bits) \ |
| 249 | static inline int cpsw_ale_get_##name(u32 *ale_entry) \ |
| 250 | { \ |
| 251 | return cpsw_ale_get_field(ale_entry, start, bits); \ |
| 252 | } \ |
| 253 | static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value) \ |
| 254 | { \ |
| 255 | cpsw_ale_set_field(ale_entry, start, bits, value); \ |
| 256 | } |
| 257 | |
| 258 | DEFINE_ALE_FIELD(entry_type, 60, 2) |
| 259 | DEFINE_ALE_FIELD(mcast_state, 62, 2) |
| 260 | DEFINE_ALE_FIELD(port_mask, 66, 3) |
| 261 | DEFINE_ALE_FIELD(ucast_type, 62, 2) |
| 262 | DEFINE_ALE_FIELD(port_num, 66, 2) |
| 263 | DEFINE_ALE_FIELD(blocked, 65, 1) |
| 264 | DEFINE_ALE_FIELD(secure, 64, 1) |
| 265 | DEFINE_ALE_FIELD(mcast, 40, 1) |
| 266 | |
| 267 | /* The MAC address field in the ALE entry cannot be macroized as above */ |
| 268 | static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr) |
| 269 | { |
| 270 | int i; |
| 271 | |
| 272 | for (i = 0; i < 6; i++) |
| 273 | addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8); |
| 274 | } |
| 275 | |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 276 | static inline void cpsw_ale_set_addr(u32 *ale_entry, const u8 *addr) |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 277 | { |
| 278 | int i; |
| 279 | |
| 280 | for (i = 0; i < 6; i++) |
| 281 | cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]); |
| 282 | } |
| 283 | |
| 284 | static int cpsw_ale_read(struct cpsw_priv *priv, int idx, u32 *ale_entry) |
| 285 | { |
| 286 | int i; |
| 287 | |
| 288 | __raw_writel(idx, priv->ale_regs + ALE_TABLE_CONTROL); |
| 289 | |
| 290 | for (i = 0; i < ALE_ENTRY_WORDS; i++) |
| 291 | ale_entry[i] = __raw_readl(priv->ale_regs + ALE_TABLE + 4 * i); |
| 292 | |
| 293 | return idx; |
| 294 | } |
| 295 | |
| 296 | static int cpsw_ale_write(struct cpsw_priv *priv, int idx, u32 *ale_entry) |
| 297 | { |
| 298 | int i; |
| 299 | |
| 300 | for (i = 0; i < ALE_ENTRY_WORDS; i++) |
| 301 | __raw_writel(ale_entry[i], priv->ale_regs + ALE_TABLE + 4 * i); |
| 302 | |
| 303 | __raw_writel(idx | ALE_TABLE_WRITE, priv->ale_regs + ALE_TABLE_CONTROL); |
| 304 | |
| 305 | return idx; |
| 306 | } |
| 307 | |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 308 | static int cpsw_ale_match_addr(struct cpsw_priv *priv, const u8 *addr) |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 309 | { |
| 310 | u32 ale_entry[ALE_ENTRY_WORDS]; |
| 311 | int type, idx; |
| 312 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 313 | for (idx = 0; idx < priv->data->ale_entries; idx++) { |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 314 | u8 entry_addr[6]; |
| 315 | |
| 316 | cpsw_ale_read(priv, idx, ale_entry); |
| 317 | type = cpsw_ale_get_entry_type(ale_entry); |
| 318 | if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR) |
| 319 | continue; |
| 320 | cpsw_ale_get_addr(ale_entry, entry_addr); |
| 321 | if (memcmp(entry_addr, addr, 6) == 0) |
| 322 | return idx; |
| 323 | } |
| 324 | return -ENOENT; |
| 325 | } |
| 326 | |
| 327 | static int cpsw_ale_match_free(struct cpsw_priv *priv) |
| 328 | { |
| 329 | u32 ale_entry[ALE_ENTRY_WORDS]; |
| 330 | int type, idx; |
| 331 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 332 | for (idx = 0; idx < priv->data->ale_entries; idx++) { |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 333 | cpsw_ale_read(priv, idx, ale_entry); |
| 334 | type = cpsw_ale_get_entry_type(ale_entry); |
| 335 | if (type == ALE_TYPE_FREE) |
| 336 | return idx; |
| 337 | } |
| 338 | return -ENOENT; |
| 339 | } |
| 340 | |
| 341 | static int cpsw_ale_find_ageable(struct cpsw_priv *priv) |
| 342 | { |
| 343 | u32 ale_entry[ALE_ENTRY_WORDS]; |
| 344 | int type, idx; |
| 345 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 346 | for (idx = 0; idx < priv->data->ale_entries; idx++) { |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 347 | cpsw_ale_read(priv, idx, ale_entry); |
| 348 | type = cpsw_ale_get_entry_type(ale_entry); |
| 349 | if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR) |
| 350 | continue; |
| 351 | if (cpsw_ale_get_mcast(ale_entry)) |
| 352 | continue; |
| 353 | type = cpsw_ale_get_ucast_type(ale_entry); |
| 354 | if (type != ALE_UCAST_PERSISTANT && |
| 355 | type != ALE_UCAST_OUI) |
| 356 | return idx; |
| 357 | } |
| 358 | return -ENOENT; |
| 359 | } |
| 360 | |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 361 | static int cpsw_ale_add_ucast(struct cpsw_priv *priv, const u8 *addr, |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 362 | int port, int flags) |
| 363 | { |
| 364 | u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0}; |
| 365 | int idx; |
| 366 | |
| 367 | cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR); |
| 368 | cpsw_ale_set_addr(ale_entry, addr); |
| 369 | cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT); |
| 370 | cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0); |
| 371 | cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0); |
| 372 | cpsw_ale_set_port_num(ale_entry, port); |
| 373 | |
| 374 | idx = cpsw_ale_match_addr(priv, addr); |
| 375 | if (idx < 0) |
| 376 | idx = cpsw_ale_match_free(priv); |
| 377 | if (idx < 0) |
| 378 | idx = cpsw_ale_find_ageable(priv); |
| 379 | if (idx < 0) |
| 380 | return -ENOMEM; |
| 381 | |
| 382 | cpsw_ale_write(priv, idx, ale_entry); |
| 383 | return 0; |
| 384 | } |
| 385 | |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 386 | static int cpsw_ale_add_mcast(struct cpsw_priv *priv, const u8 *addr, |
| 387 | int port_mask) |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 388 | { |
| 389 | u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0}; |
| 390 | int idx, mask; |
| 391 | |
| 392 | idx = cpsw_ale_match_addr(priv, addr); |
| 393 | if (idx >= 0) |
| 394 | cpsw_ale_read(priv, idx, ale_entry); |
| 395 | |
| 396 | cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR); |
| 397 | cpsw_ale_set_addr(ale_entry, addr); |
| 398 | cpsw_ale_set_mcast_state(ale_entry, ALE_MCAST_FWD_2); |
| 399 | |
| 400 | mask = cpsw_ale_get_port_mask(ale_entry); |
| 401 | port_mask |= mask; |
| 402 | cpsw_ale_set_port_mask(ale_entry, port_mask); |
| 403 | |
| 404 | if (idx < 0) |
| 405 | idx = cpsw_ale_match_free(priv); |
| 406 | if (idx < 0) |
| 407 | idx = cpsw_ale_find_ageable(priv); |
| 408 | if (idx < 0) |
| 409 | return -ENOMEM; |
| 410 | |
| 411 | cpsw_ale_write(priv, idx, ale_entry); |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static inline void cpsw_ale_control(struct cpsw_priv *priv, int bit, int val) |
| 416 | { |
| 417 | u32 tmp, mask = BIT(bit); |
| 418 | |
| 419 | tmp = __raw_readl(priv->ale_regs + ALE_CONTROL); |
| 420 | tmp &= ~mask; |
| 421 | tmp |= val ? mask : 0; |
| 422 | __raw_writel(tmp, priv->ale_regs + ALE_CONTROL); |
| 423 | } |
| 424 | |
| 425 | #define cpsw_ale_enable(priv, val) cpsw_ale_control(priv, 31, val) |
| 426 | #define cpsw_ale_clear(priv, val) cpsw_ale_control(priv, 30, val) |
| 427 | #define cpsw_ale_vlan_aware(priv, val) cpsw_ale_control(priv, 2, val) |
| 428 | |
| 429 | static inline void cpsw_ale_port_state(struct cpsw_priv *priv, int port, |
| 430 | int val) |
| 431 | { |
| 432 | int offset = ALE_PORTCTL + 4 * port; |
| 433 | u32 tmp, mask = 0x3; |
| 434 | |
| 435 | tmp = __raw_readl(priv->ale_regs + offset); |
| 436 | tmp &= ~mask; |
| 437 | tmp |= val & mask; |
| 438 | __raw_writel(tmp, priv->ale_regs + offset); |
| 439 | } |
| 440 | |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 441 | /* Set a self-clearing bit in a register, and wait for it to clear */ |
| 442 | static inline void setbit_and_wait_for_clear32(void *addr) |
| 443 | { |
| 444 | __raw_writel(CLEAR_BIT, addr); |
| 445 | while (__raw_readl(addr) & CLEAR_BIT) |
| 446 | ; |
| 447 | } |
| 448 | |
| 449 | #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \ |
| 450 | ((mac)[2] << 16) | ((mac)[3] << 24)) |
| 451 | #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8)) |
| 452 | |
| 453 | static void cpsw_set_slave_mac(struct cpsw_slave *slave, |
| 454 | struct cpsw_priv *priv) |
| 455 | { |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 456 | #ifdef CONFIG_DM_ETH |
| 457 | struct eth_pdata *pdata = dev_get_platdata(priv->dev); |
| 458 | |
| 459 | writel(mac_hi(pdata->enetaddr), &slave->regs->sa_hi); |
| 460 | writel(mac_lo(pdata->enetaddr), &slave->regs->sa_lo); |
| 461 | #else |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 462 | __raw_writel(mac_hi(priv->dev->enetaddr), &slave->regs->sa_hi); |
| 463 | __raw_writel(mac_lo(priv->dev->enetaddr), &slave->regs->sa_lo); |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 464 | #endif |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Sekhar Nori | 96d1d84 | 2017-05-08 20:49:56 +0530 | [diff] [blame] | 467 | static int cpsw_slave_update_link(struct cpsw_slave *slave, |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 468 | struct cpsw_priv *priv, int *link) |
| 469 | { |
Heiko Schocher | 93ff255 | 2013-09-05 11:50:41 +0200 | [diff] [blame] | 470 | struct phy_device *phy; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 471 | u32 mac_control = 0; |
Sekhar Nori | 96d1d84 | 2017-05-08 20:49:56 +0530 | [diff] [blame] | 472 | int ret = -ENODEV; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 473 | |
Heiko Schocher | 93ff255 | 2013-09-05 11:50:41 +0200 | [diff] [blame] | 474 | phy = priv->phydev; |
Heiko Schocher | 93ff255 | 2013-09-05 11:50:41 +0200 | [diff] [blame] | 475 | if (!phy) |
Sekhar Nori | 96d1d84 | 2017-05-08 20:49:56 +0530 | [diff] [blame] | 476 | goto out; |
Heiko Schocher | 93ff255 | 2013-09-05 11:50:41 +0200 | [diff] [blame] | 477 | |
Sekhar Nori | 96d1d84 | 2017-05-08 20:49:56 +0530 | [diff] [blame] | 478 | ret = phy_startup(phy); |
| 479 | if (ret) |
| 480 | goto out; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 481 | |
Sekhar Nori | 96d1d84 | 2017-05-08 20:49:56 +0530 | [diff] [blame] | 482 | if (link) |
| 483 | *link = phy->link; |
| 484 | |
| 485 | if (phy->link) { /* link up */ |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 486 | mac_control = priv->data->mac_control; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 487 | if (phy->speed == 1000) |
| 488 | mac_control |= GIGABITEN; |
| 489 | if (phy->duplex == DUPLEX_FULL) |
| 490 | mac_control |= FULLDUPLEXEN; |
| 491 | if (phy->speed == 100) |
| 492 | mac_control |= MIIEN; |
Grygorii Strashko | 60e81d0 | 2019-09-19 11:16:37 +0300 | [diff] [blame] | 493 | if (phy->speed == 10 && phy_interface_is_rgmii(phy)) |
| 494 | mac_control |= CTL_EXT_EN; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | if (mac_control == slave->mac_control) |
Sekhar Nori | 96d1d84 | 2017-05-08 20:49:56 +0530 | [diff] [blame] | 498 | goto out; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 499 | |
| 500 | if (mac_control) { |
| 501 | printf("link up on port %d, speed %d, %s duplex\n", |
| 502 | slave->slave_num, phy->speed, |
| 503 | (phy->duplex == DUPLEX_FULL) ? "full" : "half"); |
| 504 | } else { |
| 505 | printf("link down on port %d\n", slave->slave_num); |
| 506 | } |
| 507 | |
| 508 | __raw_writel(mac_control, &slave->sliver->mac_control); |
| 509 | slave->mac_control = mac_control; |
Sekhar Nori | 96d1d84 | 2017-05-08 20:49:56 +0530 | [diff] [blame] | 510 | |
| 511 | out: |
| 512 | return ret; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | static int cpsw_update_link(struct cpsw_priv *priv) |
| 516 | { |
Sekhar Nori | 96d1d84 | 2017-05-08 20:49:56 +0530 | [diff] [blame] | 517 | int ret = -ENODEV; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 518 | struct cpsw_slave *slave; |
| 519 | |
Mugunthan V N | 7a02275 | 2014-05-22 14:37:10 +0530 | [diff] [blame] | 520 | for_active_slave(slave, priv) |
Sekhar Nori | 96d1d84 | 2017-05-08 20:49:56 +0530 | [diff] [blame] | 521 | ret = cpsw_slave_update_link(slave, priv, NULL); |
Stefan Roese | 5a834c1 | 2014-08-25 11:26:19 +0200 | [diff] [blame] | 522 | |
Sekhar Nori | 96d1d84 | 2017-05-08 20:49:56 +0530 | [diff] [blame] | 523 | return ret; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | static inline u32 cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num) |
| 527 | { |
| 528 | if (priv->host_port == 0) |
| 529 | return slave_num + 1; |
| 530 | else |
| 531 | return slave_num; |
| 532 | } |
| 533 | |
| 534 | static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv) |
| 535 | { |
| 536 | u32 slave_port; |
| 537 | |
| 538 | setbit_and_wait_for_clear32(&slave->sliver->soft_reset); |
| 539 | |
| 540 | /* setup priority mapping */ |
| 541 | __raw_writel(0x76543210, &slave->sliver->rx_pri_map); |
| 542 | __raw_writel(0x33221100, &slave->regs->tx_pri_map); |
| 543 | |
| 544 | /* setup max packet size, and mac address */ |
| 545 | __raw_writel(PKT_MAX, &slave->sliver->rx_maxlen); |
| 546 | cpsw_set_slave_mac(slave, priv); |
| 547 | |
| 548 | slave->mac_control = 0; /* no link yet */ |
| 549 | |
| 550 | /* enable forwarding */ |
| 551 | slave_port = cpsw_get_slave_port(priv, slave->slave_num); |
| 552 | cpsw_ale_port_state(priv, slave_port, ALE_PORT_STATE_FORWARD); |
| 553 | |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 554 | cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << slave_port); |
Mugunthan V N | 48ec529 | 2013-02-19 21:34:44 +0000 | [diff] [blame] | 555 | |
Mugunthan V N | 9c653aa | 2014-02-18 07:31:52 -0500 | [diff] [blame] | 556 | priv->phy_mask |= 1 << slave->data->phy_addr; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | static struct cpdma_desc *cpdma_desc_alloc(struct cpsw_priv *priv) |
| 560 | { |
| 561 | struct cpdma_desc *desc = priv->desc_free; |
| 562 | |
| 563 | if (desc) |
| 564 | priv->desc_free = desc_read_ptr(desc, hw_next); |
| 565 | return desc; |
| 566 | } |
| 567 | |
| 568 | static void cpdma_desc_free(struct cpsw_priv *priv, struct cpdma_desc *desc) |
| 569 | { |
| 570 | if (desc) { |
| 571 | desc_write(desc, hw_next, priv->desc_free); |
| 572 | priv->desc_free = desc; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | static int cpdma_submit(struct cpsw_priv *priv, struct cpdma_chan *chan, |
| 577 | void *buffer, int len) |
| 578 | { |
| 579 | struct cpdma_desc *desc, *prev; |
| 580 | u32 mode; |
| 581 | |
| 582 | desc = cpdma_desc_alloc(priv); |
| 583 | if (!desc) |
| 584 | return -ENOMEM; |
| 585 | |
| 586 | if (len < PKT_MIN) |
| 587 | len = PKT_MIN; |
| 588 | |
| 589 | mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP; |
| 590 | |
| 591 | desc_write(desc, hw_next, 0); |
| 592 | desc_write(desc, hw_buffer, buffer); |
| 593 | desc_write(desc, hw_len, len); |
| 594 | desc_write(desc, hw_mode, mode | len); |
| 595 | desc_write(desc, sw_buffer, buffer); |
| 596 | desc_write(desc, sw_len, len); |
| 597 | |
| 598 | if (!chan->head) { |
| 599 | /* simple case - first packet enqueued */ |
| 600 | chan->head = desc; |
| 601 | chan->tail = desc; |
| 602 | chan_write(chan, hdp, desc); |
| 603 | goto done; |
| 604 | } |
| 605 | |
| 606 | /* not the first packet - enqueue at the tail */ |
| 607 | prev = chan->tail; |
| 608 | desc_write(prev, hw_next, desc); |
| 609 | chan->tail = desc; |
| 610 | |
| 611 | /* next check if EOQ has been triggered already */ |
| 612 | if (desc_read(prev, hw_mode) & CPDMA_DESC_EOQ) |
| 613 | chan_write(chan, hdp, desc); |
| 614 | |
| 615 | done: |
| 616 | if (chan->rxfree) |
| 617 | chan_write(chan, rxfree, 1); |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | static int cpdma_process(struct cpsw_priv *priv, struct cpdma_chan *chan, |
| 622 | void **buffer, int *len) |
| 623 | { |
| 624 | struct cpdma_desc *desc = chan->head; |
| 625 | u32 status; |
| 626 | |
| 627 | if (!desc) |
| 628 | return -ENOENT; |
| 629 | |
| 630 | status = desc_read(desc, hw_mode); |
| 631 | |
| 632 | if (len) |
| 633 | *len = status & 0x7ff; |
| 634 | |
| 635 | if (buffer) |
| 636 | *buffer = desc_read_ptr(desc, sw_buffer); |
| 637 | |
| 638 | if (status & CPDMA_DESC_OWNER) { |
| 639 | if (chan_read(chan, hdp) == 0) { |
| 640 | if (desc_read(desc, hw_mode) & CPDMA_DESC_OWNER) |
| 641 | chan_write(chan, hdp, desc); |
| 642 | } |
| 643 | |
| 644 | return -EBUSY; |
| 645 | } |
| 646 | |
| 647 | chan->head = desc_read_ptr(desc, hw_next); |
| 648 | chan_write(chan, cp, desc); |
| 649 | |
| 650 | cpdma_desc_free(priv, desc); |
| 651 | return 0; |
| 652 | } |
| 653 | |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 654 | static int _cpsw_init(struct cpsw_priv *priv, u8 *enetaddr) |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 655 | { |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 656 | struct cpsw_slave *slave; |
| 657 | int i, ret; |
| 658 | |
| 659 | /* soft reset the controller and initialize priv */ |
| 660 | setbit_and_wait_for_clear32(&priv->regs->soft_reset); |
| 661 | |
| 662 | /* initialize and reset the address lookup engine */ |
| 663 | cpsw_ale_enable(priv, 1); |
| 664 | cpsw_ale_clear(priv, 1); |
| 665 | cpsw_ale_vlan_aware(priv, 0); /* vlan unaware mode */ |
| 666 | |
| 667 | /* setup host port priority mapping */ |
| 668 | __raw_writel(0x76543210, &priv->host_port_regs->cpdma_tx_pri_map); |
| 669 | __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map); |
| 670 | |
| 671 | /* disable priority elevation and enable statistics on all ports */ |
| 672 | __raw_writel(0, &priv->regs->ptype); |
| 673 | |
| 674 | /* enable statistics collection only on the host port */ |
| 675 | __raw_writel(BIT(priv->host_port), &priv->regs->stat_port_en); |
Mugunthan V N | 454ac63 | 2013-07-08 16:04:38 +0530 | [diff] [blame] | 676 | __raw_writel(0x7, &priv->regs->stat_port_en); |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 677 | |
| 678 | cpsw_ale_port_state(priv, priv->host_port, ALE_PORT_STATE_FORWARD); |
| 679 | |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 680 | cpsw_ale_add_ucast(priv, enetaddr, priv->host_port, ALE_SECURE); |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 681 | cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << priv->host_port); |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 682 | |
Mugunthan V N | 7a02275 | 2014-05-22 14:37:10 +0530 | [diff] [blame] | 683 | for_active_slave(slave, priv) |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 684 | cpsw_slave_init(slave, priv); |
| 685 | |
Sekhar Nori | 96d1d84 | 2017-05-08 20:49:56 +0530 | [diff] [blame] | 686 | ret = cpsw_update_link(priv); |
| 687 | if (ret) |
| 688 | goto out; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 689 | |
| 690 | /* init descriptor pool */ |
| 691 | for (i = 0; i < NUM_DESCS; i++) { |
| 692 | desc_write(&priv->descs[i], hw_next, |
| 693 | (i == (NUM_DESCS - 1)) ? 0 : &priv->descs[i+1]); |
| 694 | } |
| 695 | priv->desc_free = &priv->descs[0]; |
| 696 | |
| 697 | /* initialize channels */ |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 698 | if (priv->data->version == CPSW_CTRL_VERSION_2) { |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 699 | memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan)); |
| 700 | priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER2; |
| 701 | priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER2; |
| 702 | priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE; |
| 703 | |
| 704 | memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan)); |
| 705 | priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER2; |
| 706 | priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER2; |
| 707 | } else { |
| 708 | memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan)); |
| 709 | priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER1; |
| 710 | priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER1; |
| 711 | priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE; |
| 712 | |
| 713 | memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan)); |
| 714 | priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER1; |
| 715 | priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER1; |
| 716 | } |
| 717 | |
| 718 | /* clear dma state */ |
| 719 | setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET); |
| 720 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 721 | if (priv->data->version == CPSW_CTRL_VERSION_2) { |
| 722 | for (i = 0; i < priv->data->channels; i++) { |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 723 | __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER2 + 4 |
| 724 | * i); |
| 725 | __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4 |
| 726 | * i); |
| 727 | __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER2 + 4 |
| 728 | * i); |
| 729 | __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER2 + 4 |
| 730 | * i); |
| 731 | __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER2 + 4 |
| 732 | * i); |
| 733 | } |
| 734 | } else { |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 735 | for (i = 0; i < priv->data->channels; i++) { |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 736 | __raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER1 + 4 |
| 737 | * i); |
| 738 | __raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4 |
| 739 | * i); |
| 740 | __raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER1 + 4 |
| 741 | * i); |
| 742 | __raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER1 + 4 |
| 743 | * i); |
| 744 | __raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER1 + 4 |
| 745 | * i); |
| 746 | |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | __raw_writel(1, priv->dma_regs + CPDMA_TXCONTROL); |
| 751 | __raw_writel(1, priv->dma_regs + CPDMA_RXCONTROL); |
| 752 | |
| 753 | /* submit rx descs */ |
| 754 | for (i = 0; i < PKTBUFSRX; i++) { |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 755 | ret = cpdma_submit(priv, &priv->rx_chan, net_rx_packets[i], |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 756 | PKTSIZE); |
| 757 | if (ret < 0) { |
| 758 | printf("error %d submitting rx desc\n", ret); |
| 759 | break; |
| 760 | } |
| 761 | } |
| 762 | |
Sekhar Nori | 96d1d84 | 2017-05-08 20:49:56 +0530 | [diff] [blame] | 763 | out: |
| 764 | return ret; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Alex Kiernan | 286bea2 | 2018-05-12 07:30:02 +0000 | [diff] [blame] | 767 | static int cpsw_reap_completed_packets(struct cpsw_priv *priv) |
| 768 | { |
| 769 | int timeout = CPDMA_TIMEOUT; |
| 770 | |
| 771 | /* reap completed packets */ |
| 772 | while (timeout-- && |
| 773 | (cpdma_process(priv, &priv->tx_chan, NULL, NULL) >= 0)) |
| 774 | ; |
| 775 | |
| 776 | return timeout; |
| 777 | } |
| 778 | |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 779 | static void _cpsw_halt(struct cpsw_priv *priv) |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 780 | { |
Alex Kiernan | 286bea2 | 2018-05-12 07:30:02 +0000 | [diff] [blame] | 781 | cpsw_reap_completed_packets(priv); |
| 782 | |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 783 | writel(0, priv->dma_regs + CPDMA_TXCONTROL); |
| 784 | writel(0, priv->dma_regs + CPDMA_RXCONTROL); |
| 785 | |
| 786 | /* soft reset the controller and initialize priv */ |
| 787 | setbit_and_wait_for_clear32(&priv->regs->soft_reset); |
| 788 | |
| 789 | /* clear dma state */ |
| 790 | setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET); |
| 791 | |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 794 | static int _cpsw_send(struct cpsw_priv *priv, void *packet, int length) |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 795 | { |
Alex Kiernan | 286bea2 | 2018-05-12 07:30:02 +0000 | [diff] [blame] | 796 | int timeout; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 797 | |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 798 | flush_dcache_range((unsigned long)packet, |
Lokesh Vutla | 1f01962 | 2016-08-11 13:00:59 +0530 | [diff] [blame] | 799 | (unsigned long)packet + ALIGN(length, PKTALIGN)); |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 800 | |
Alex Kiernan | 286bea2 | 2018-05-12 07:30:02 +0000 | [diff] [blame] | 801 | timeout = cpsw_reap_completed_packets(priv); |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 802 | if (timeout == -1) { |
| 803 | printf("cpdma_process timeout\n"); |
| 804 | return -ETIMEDOUT; |
| 805 | } |
| 806 | |
| 807 | return cpdma_submit(priv, &priv->tx_chan, packet, length); |
| 808 | } |
| 809 | |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 810 | static int _cpsw_recv(struct cpsw_priv *priv, uchar **pkt) |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 811 | { |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 812 | void *buffer; |
| 813 | int len; |
Heinrich Schuchardt | 4b23d3c8 | 2018-03-18 11:24:38 +0100 | [diff] [blame] | 814 | int ret; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 815 | |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 816 | ret = cpdma_process(priv, &priv->rx_chan, &buffer, &len); |
| 817 | if (ret < 0) |
| 818 | return ret; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 819 | |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 820 | invalidate_dcache_range((unsigned long)buffer, |
| 821 | (unsigned long)buffer + PKTSIZE_ALIGN); |
| 822 | *pkt = buffer; |
| 823 | |
| 824 | return len; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | static void cpsw_slave_setup(struct cpsw_slave *slave, int slave_num, |
| 828 | struct cpsw_priv *priv) |
| 829 | { |
| 830 | void *regs = priv->regs; |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 831 | struct cpsw_slave_data *data = priv->data->slave_data + slave_num; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 832 | slave->slave_num = slave_num; |
| 833 | slave->data = data; |
| 834 | slave->regs = regs + data->slave_reg_ofs; |
| 835 | slave->sliver = regs + data->sliver_reg_ofs; |
| 836 | } |
| 837 | |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 838 | static int cpsw_phy_init(struct cpsw_priv *priv, struct cpsw_slave *slave) |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 839 | { |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 840 | struct phy_device *phydev; |
Ilya Ledvich | ef59bb7 | 2014-03-12 11:26:30 +0200 | [diff] [blame] | 841 | u32 supported = PHY_GBIT_FEATURES; |
Grygorii Strashko | 3c57b62 | 2019-09-19 11:16:39 +0300 | [diff] [blame] | 842 | int ret; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 843 | |
Yegor Yefremov | cdd0729 | 2012-11-26 04:03:16 +0000 | [diff] [blame] | 844 | phydev = phy_connect(priv->bus, |
Mugunthan V N | 9c653aa | 2014-02-18 07:31:52 -0500 | [diff] [blame] | 845 | slave->data->phy_addr, |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 846 | priv->dev, |
Yegor Yefremov | cdd0729 | 2012-11-26 04:03:16 +0000 | [diff] [blame] | 847 | slave->data->phy_if); |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 848 | |
Heiko Schocher | 93ff255 | 2013-09-05 11:50:41 +0200 | [diff] [blame] | 849 | if (!phydev) |
| 850 | return -1; |
| 851 | |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 852 | phydev->supported &= supported; |
Grygorii Strashko | 3c57b62 | 2019-09-19 11:16:39 +0300 | [diff] [blame] | 853 | if (slave->data->max_speed) { |
| 854 | ret = phy_set_supported(phydev, slave->data->max_speed); |
| 855 | if (ret) |
| 856 | return ret; |
| 857 | dev_dbg(priv->dev, "Port %u speed forced to %uMbit\n", |
| 858 | slave->slave_num + 1, slave->data->max_speed); |
| 859 | } |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 860 | phydev->advertising = phydev->supported; |
| 861 | |
Dan Murphy | cb38622 | 2016-05-02 15:45:56 -0500 | [diff] [blame] | 862 | #ifdef CONFIG_DM_ETH |
| 863 | if (slave->data->phy_of_handle) |
Grygorii Strashko | d4bb981 | 2018-07-05 12:02:51 -0500 | [diff] [blame] | 864 | phydev->node = offset_to_ofnode(slave->data->phy_of_handle); |
Dan Murphy | cb38622 | 2016-05-02 15:45:56 -0500 | [diff] [blame] | 865 | #endif |
| 866 | |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 867 | priv->phydev = phydev; |
| 868 | phy_config(phydev); |
| 869 | |
| 870 | return 1; |
| 871 | } |
| 872 | |
Sekhar Nori | e2597be | 2018-08-23 17:11:29 +0530 | [diff] [blame] | 873 | static void cpsw_phy_addr_update(struct cpsw_priv *priv) |
| 874 | { |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 875 | struct cpsw_platform_data *data = priv->data; |
Grygorii Strashko | 4f41cd9 | 2018-10-31 16:21:44 -0500 | [diff] [blame] | 876 | u16 alive = cpsw_mdio_get_alive(priv->bus); |
Sekhar Nori | e2597be | 2018-08-23 17:11:29 +0530 | [diff] [blame] | 877 | int active = data->active_slave; |
| 878 | int new_addr = ffs(alive) - 1; |
| 879 | |
| 880 | /* |
| 881 | * If there is only one phy alive and its address does not match |
| 882 | * that of active slave, then phy address can safely be updated. |
| 883 | */ |
| 884 | if (hweight16(alive) == 1 && |
| 885 | data->slave_data[active].phy_addr != new_addr) { |
| 886 | printf("Updated phy address for CPSW#%d, old: %d, new: %d\n", |
| 887 | active, data->slave_data[active].phy_addr, new_addr); |
| 888 | data->slave_data[active].phy_addr = new_addr; |
| 889 | } |
| 890 | } |
| 891 | |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 892 | int _cpsw_register(struct cpsw_priv *priv) |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 893 | { |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 894 | struct cpsw_slave *slave; |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 895 | struct cpsw_platform_data *data = priv->data; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 896 | void *regs = (void *)data->cpsw_base; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 897 | |
| 898 | priv->slaves = malloc(sizeof(struct cpsw_slave) * data->slaves); |
| 899 | if (!priv->slaves) { |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 900 | return -ENOMEM; |
| 901 | } |
| 902 | |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 903 | priv->host_port = data->host_port_num; |
| 904 | priv->regs = regs; |
| 905 | priv->host_port_regs = regs + data->host_port_reg_ofs; |
| 906 | priv->dma_regs = regs + data->cpdma_reg_ofs; |
| 907 | priv->ale_regs = regs + data->ale_reg_ofs; |
Mugunthan V N | 2bf36ac | 2013-07-08 16:04:37 +0530 | [diff] [blame] | 908 | priv->descs = (void *)regs + data->bd_ram_ofs; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 909 | |
| 910 | int idx = 0; |
| 911 | |
| 912 | for_each_slave(slave, priv) { |
| 913 | cpsw_slave_setup(slave, idx, priv); |
| 914 | idx = idx + 1; |
| 915 | } |
| 916 | |
Grygorii Strashko | 4f41cd9 | 2018-10-31 16:21:44 -0500 | [diff] [blame] | 917 | priv->bus = cpsw_mdio_init(priv->dev->name, data->mdio_base, 0, 0); |
| 918 | if (!priv->bus) |
| 919 | return -EFAULT; |
Sekhar Nori | e2597be | 2018-08-23 17:11:29 +0530 | [diff] [blame] | 920 | |
| 921 | cpsw_phy_addr_update(priv); |
| 922 | |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 923 | for_active_slave(slave, priv) |
| 924 | cpsw_phy_init(priv, slave); |
| 925 | |
| 926 | return 0; |
| 927 | } |
| 928 | |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 929 | #ifndef CONFIG_DM_ETH |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 930 | static int cpsw_init(struct eth_device *dev, bd_t *bis) |
| 931 | { |
| 932 | struct cpsw_priv *priv = dev->priv; |
| 933 | |
| 934 | return _cpsw_init(priv, dev->enetaddr); |
| 935 | } |
| 936 | |
| 937 | static void cpsw_halt(struct eth_device *dev) |
| 938 | { |
| 939 | struct cpsw_priv *priv = dev->priv; |
| 940 | |
| 941 | return _cpsw_halt(priv); |
| 942 | } |
| 943 | |
| 944 | static int cpsw_send(struct eth_device *dev, void *packet, int length) |
| 945 | { |
| 946 | struct cpsw_priv *priv = dev->priv; |
| 947 | |
| 948 | return _cpsw_send(priv, packet, length); |
| 949 | } |
| 950 | |
| 951 | static int cpsw_recv(struct eth_device *dev) |
| 952 | { |
| 953 | struct cpsw_priv *priv = dev->priv; |
| 954 | uchar *pkt = NULL; |
| 955 | int len; |
| 956 | |
| 957 | len = _cpsw_recv(priv, &pkt); |
| 958 | |
| 959 | if (len > 0) { |
| 960 | net_process_received_packet(pkt, len); |
| 961 | cpdma_submit(priv, &priv->rx_chan, pkt, PKTSIZE); |
| 962 | } |
| 963 | |
| 964 | return len; |
| 965 | } |
| 966 | |
| 967 | int cpsw_register(struct cpsw_platform_data *data) |
| 968 | { |
| 969 | struct cpsw_priv *priv; |
| 970 | struct eth_device *dev; |
| 971 | int ret; |
| 972 | |
| 973 | dev = calloc(sizeof(*dev), 1); |
| 974 | if (!dev) |
| 975 | return -ENOMEM; |
| 976 | |
| 977 | priv = calloc(sizeof(*priv), 1); |
| 978 | if (!priv) { |
| 979 | free(dev); |
| 980 | return -ENOMEM; |
| 981 | } |
| 982 | |
| 983 | priv->dev = dev; |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 984 | priv->data = data; |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 985 | |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 986 | strcpy(dev->name, "cpsw"); |
| 987 | dev->iobase = 0; |
| 988 | dev->init = cpsw_init; |
| 989 | dev->halt = cpsw_halt; |
| 990 | dev->send = cpsw_send; |
| 991 | dev->recv = cpsw_recv; |
| 992 | dev->priv = priv; |
| 993 | |
| 994 | eth_register(dev); |
| 995 | |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 996 | ret = _cpsw_register(priv); |
| 997 | if (ret < 0) { |
| 998 | eth_unregister(dev); |
| 999 | free(dev); |
| 1000 | free(priv); |
| 1001 | return ret; |
| 1002 | } |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 1003 | |
| 1004 | return 1; |
| 1005 | } |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1006 | #else |
| 1007 | static int cpsw_eth_start(struct udevice *dev) |
| 1008 | { |
| 1009 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 1010 | struct cpsw_priv *priv = dev_get_priv(dev); |
| 1011 | |
| 1012 | return _cpsw_init(priv, pdata->enetaddr); |
| 1013 | } |
| 1014 | |
| 1015 | static int cpsw_eth_send(struct udevice *dev, void *packet, int length) |
| 1016 | { |
| 1017 | struct cpsw_priv *priv = dev_get_priv(dev); |
| 1018 | |
| 1019 | return _cpsw_send(priv, packet, length); |
| 1020 | } |
| 1021 | |
| 1022 | static int cpsw_eth_recv(struct udevice *dev, int flags, uchar **packetp) |
| 1023 | { |
| 1024 | struct cpsw_priv *priv = dev_get_priv(dev); |
| 1025 | |
| 1026 | return _cpsw_recv(priv, packetp); |
| 1027 | } |
| 1028 | |
| 1029 | static int cpsw_eth_free_pkt(struct udevice *dev, uchar *packet, |
| 1030 | int length) |
| 1031 | { |
| 1032 | struct cpsw_priv *priv = dev_get_priv(dev); |
| 1033 | |
| 1034 | return cpdma_submit(priv, &priv->rx_chan, packet, PKTSIZE); |
| 1035 | } |
| 1036 | |
| 1037 | static void cpsw_eth_stop(struct udevice *dev) |
| 1038 | { |
| 1039 | struct cpsw_priv *priv = dev_get_priv(dev); |
| 1040 | |
| 1041 | return _cpsw_halt(priv); |
| 1042 | } |
| 1043 | |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1044 | static const struct eth_ops cpsw_eth_ops = { |
| 1045 | .start = cpsw_eth_start, |
| 1046 | .send = cpsw_eth_send, |
| 1047 | .recv = cpsw_eth_recv, |
| 1048 | .free_pkt = cpsw_eth_free_pkt, |
| 1049 | .stop = cpsw_eth_stop, |
| 1050 | }; |
| 1051 | |
Mugunthan V N | 66e740c | 2016-04-28 15:36:06 +0530 | [diff] [blame] | 1052 | static inline fdt_addr_t cpsw_get_addr_by_node(const void *fdt, int node) |
| 1053 | { |
Stephen Warren | 6e06acb | 2016-08-05 09:47:51 -0600 | [diff] [blame] | 1054 | return fdtdec_get_addr_size_auto_noparent(fdt, node, "reg", 0, NULL, |
| 1055 | false); |
Mugunthan V N | 66e740c | 2016-04-28 15:36:06 +0530 | [diff] [blame] | 1056 | } |
| 1057 | |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1058 | static void cpsw_gmii_sel_am3352(struct cpsw_priv *priv, |
| 1059 | phy_interface_t phy_mode) |
| 1060 | { |
| 1061 | u32 reg; |
| 1062 | u32 mask; |
| 1063 | u32 mode = 0; |
| 1064 | bool rgmii_id = false; |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1065 | int slave = priv->data->active_slave; |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1066 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1067 | reg = readl(priv->data->gmii_sel); |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1068 | |
| 1069 | switch (phy_mode) { |
| 1070 | case PHY_INTERFACE_MODE_RMII: |
| 1071 | mode = AM33XX_GMII_SEL_MODE_RMII; |
| 1072 | break; |
| 1073 | |
| 1074 | case PHY_INTERFACE_MODE_RGMII: |
Grygorii Strashko | a3c867a | 2019-09-19 11:16:40 +0300 | [diff] [blame^] | 1075 | case PHY_INTERFACE_MODE_RGMII_RXID: |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1076 | mode = AM33XX_GMII_SEL_MODE_RGMII; |
| 1077 | break; |
| 1078 | case PHY_INTERFACE_MODE_RGMII_ID: |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1079 | case PHY_INTERFACE_MODE_RGMII_TXID: |
| 1080 | mode = AM33XX_GMII_SEL_MODE_RGMII; |
| 1081 | rgmii_id = true; |
| 1082 | break; |
| 1083 | |
| 1084 | case PHY_INTERFACE_MODE_MII: |
| 1085 | default: |
| 1086 | mode = AM33XX_GMII_SEL_MODE_MII; |
| 1087 | break; |
| 1088 | }; |
| 1089 | |
| 1090 | mask = GMII_SEL_MODE_MASK << (slave * 2) | BIT(slave + 6); |
| 1091 | mode <<= slave * 2; |
| 1092 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1093 | if (priv->data->rmii_clock_external) { |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1094 | if (slave == 0) |
| 1095 | mode |= AM33XX_GMII_SEL_RMII1_IO_CLK_EN; |
| 1096 | else |
| 1097 | mode |= AM33XX_GMII_SEL_RMII2_IO_CLK_EN; |
| 1098 | } |
| 1099 | |
| 1100 | if (rgmii_id) { |
| 1101 | if (slave == 0) |
| 1102 | mode |= AM33XX_GMII_SEL_RGMII1_IDMODE; |
| 1103 | else |
| 1104 | mode |= AM33XX_GMII_SEL_RGMII2_IDMODE; |
| 1105 | } |
| 1106 | |
| 1107 | reg &= ~mask; |
| 1108 | reg |= mode; |
| 1109 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1110 | writel(reg, priv->data->gmii_sel); |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | static void cpsw_gmii_sel_dra7xx(struct cpsw_priv *priv, |
| 1114 | phy_interface_t phy_mode) |
| 1115 | { |
| 1116 | u32 reg; |
| 1117 | u32 mask; |
| 1118 | u32 mode = 0; |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1119 | int slave = priv->data->active_slave; |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1120 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1121 | reg = readl(priv->data->gmii_sel); |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1122 | |
| 1123 | switch (phy_mode) { |
| 1124 | case PHY_INTERFACE_MODE_RMII: |
| 1125 | mode = AM33XX_GMII_SEL_MODE_RMII; |
| 1126 | break; |
| 1127 | |
| 1128 | case PHY_INTERFACE_MODE_RGMII: |
| 1129 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 1130 | case PHY_INTERFACE_MODE_RGMII_RXID: |
| 1131 | case PHY_INTERFACE_MODE_RGMII_TXID: |
| 1132 | mode = AM33XX_GMII_SEL_MODE_RGMII; |
| 1133 | break; |
| 1134 | |
| 1135 | case PHY_INTERFACE_MODE_MII: |
| 1136 | default: |
| 1137 | mode = AM33XX_GMII_SEL_MODE_MII; |
| 1138 | break; |
| 1139 | }; |
| 1140 | |
| 1141 | switch (slave) { |
| 1142 | case 0: |
| 1143 | mask = GMII_SEL_MODE_MASK; |
| 1144 | break; |
| 1145 | case 1: |
| 1146 | mask = GMII_SEL_MODE_MASK << 4; |
| 1147 | mode <<= 4; |
| 1148 | break; |
| 1149 | default: |
| 1150 | dev_err(priv->dev, "invalid slave number...\n"); |
| 1151 | return; |
| 1152 | } |
| 1153 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1154 | if (priv->data->rmii_clock_external) |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1155 | dev_err(priv->dev, "RMII External clock is not supported\n"); |
| 1156 | |
| 1157 | reg &= ~mask; |
| 1158 | reg |= mode; |
| 1159 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1160 | writel(reg, priv->data->gmii_sel); |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | static void cpsw_phy_sel(struct cpsw_priv *priv, const char *compat, |
| 1164 | phy_interface_t phy_mode) |
| 1165 | { |
| 1166 | if (!strcmp(compat, "ti,am3352-cpsw-phy-sel")) |
| 1167 | cpsw_gmii_sel_am3352(priv, phy_mode); |
| 1168 | if (!strcmp(compat, "ti,am43xx-cpsw-phy-sel")) |
| 1169 | cpsw_gmii_sel_am3352(priv, phy_mode); |
| 1170 | else if (!strcmp(compat, "ti,dra7xx-cpsw-phy-sel")) |
| 1171 | cpsw_gmii_sel_dra7xx(priv, phy_mode); |
| 1172 | } |
| 1173 | |
Faiz Abbas | e50f878 | 2019-03-18 13:54:32 +0530 | [diff] [blame] | 1174 | static int cpsw_eth_probe(struct udevice *dev) |
| 1175 | { |
| 1176 | struct cpsw_priv *priv = dev_get_priv(dev); |
| 1177 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 1178 | |
| 1179 | priv->dev = dev; |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1180 | priv->data = pdata->priv_pdata; |
Faiz Abbas | a58d222 | 2019-03-18 13:54:34 +0530 | [diff] [blame] | 1181 | ti_cm_get_macid(dev, priv->data, pdata->enetaddr); |
Faiz Abbas | e50f878 | 2019-03-18 13:54:32 +0530 | [diff] [blame] | 1182 | /* Select phy interface in control module */ |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1183 | cpsw_phy_sel(priv, priv->data->phy_sel_compat, |
Faiz Abbas | e50f878 | 2019-03-18 13:54:32 +0530 | [diff] [blame] | 1184 | pdata->phy_interface); |
| 1185 | |
| 1186 | return _cpsw_register(priv); |
| 1187 | } |
| 1188 | |
Faiz Abbas | c3b460a | 2019-03-18 13:54:35 +0530 | [diff] [blame] | 1189 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Grygorii Strashko | 4040148 | 2019-09-19 11:16:38 +0300 | [diff] [blame] | 1190 | static void cpsw_eth_of_parse_slave(struct cpsw_platform_data *data, |
| 1191 | int slave_index, int subnode) |
| 1192 | { |
| 1193 | struct cpsw_slave_data *slave_data; |
| 1194 | const void *fdt = gd->fdt_blob; |
| 1195 | const char *phy_mode; |
Grygorii Strashko | 3c57b62 | 2019-09-19 11:16:39 +0300 | [diff] [blame] | 1196 | int max_speed = -1; |
Grygorii Strashko | 4040148 | 2019-09-19 11:16:38 +0300 | [diff] [blame] | 1197 | u32 phy_id[2]; |
| 1198 | |
| 1199 | slave_data = &data->slave_data[slave_index]; |
| 1200 | |
| 1201 | phy_mode = fdt_getprop(fdt, subnode, "phy-mode", NULL); |
| 1202 | if (phy_mode) |
| 1203 | slave_data->phy_if = |
| 1204 | phy_get_interface_by_name(phy_mode); |
| 1205 | |
| 1206 | slave_data->phy_of_handle = fdtdec_lookup_phandle(fdt, subnode, |
| 1207 | "phy-handle"); |
| 1208 | |
| 1209 | if (data->slave_data[slave_index].phy_of_handle >= 0) { |
| 1210 | slave_data->phy_addr = |
| 1211 | fdtdec_get_int(fdt, slave_data->phy_of_handle, |
| 1212 | "reg", -1); |
| 1213 | } else { |
| 1214 | fdtdec_get_int_array(fdt, subnode, "phy_id", |
| 1215 | phy_id, 2); |
| 1216 | slave_data->phy_addr = phy_id[1]; |
| 1217 | } |
Grygorii Strashko | 3c57b62 | 2019-09-19 11:16:39 +0300 | [diff] [blame] | 1218 | |
| 1219 | slave_data->max_speed = 0; |
| 1220 | max_speed = fdtdec_get_int(fdt, subnode, |
| 1221 | "max-speed", max_speed); |
| 1222 | if (max_speed > 0) |
| 1223 | slave_data->max_speed = max_speed; |
Grygorii Strashko | 4040148 | 2019-09-19 11:16:38 +0300 | [diff] [blame] | 1224 | } |
| 1225 | |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1226 | static int cpsw_eth_ofdata_to_platdata(struct udevice *dev) |
| 1227 | { |
| 1228 | struct eth_pdata *pdata = dev_get_platdata(dev); |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1229 | struct cpsw_platform_data *data; |
Vignesh R | 2e205ef | 2016-08-02 10:14:27 +0530 | [diff] [blame] | 1230 | struct gpio_desc *mode_gpios; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1231 | const void *fdt = gd->fdt_blob; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 1232 | int node = dev_of_offset(dev); |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1233 | int subnode; |
| 1234 | int slave_index = 0; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1235 | int active_slave; |
Vignesh R | 2e205ef | 2016-08-02 10:14:27 +0530 | [diff] [blame] | 1236 | int num_mode_gpios; |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 1237 | int ret; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1238 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1239 | data = calloc(1, sizeof(struct cpsw_platform_data)); |
| 1240 | pdata->priv_pdata = data; |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 1241 | pdata->iobase = devfdt_get_addr(dev); |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1242 | data->version = CPSW_CTRL_VERSION_2; |
| 1243 | data->bd_ram_ofs = CPSW_BD_OFFSET; |
| 1244 | data->ale_reg_ofs = CPSW_ALE_OFFSET; |
| 1245 | data->cpdma_reg_ofs = CPSW_CPDMA_OFFSET; |
| 1246 | data->mdio_div = CPSW_MDIO_DIV; |
| 1247 | data->host_port_reg_ofs = CPSW_HOST_PORT_OFFSET, |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1248 | |
| 1249 | pdata->phy_interface = -1; |
| 1250 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1251 | data->cpsw_base = pdata->iobase; |
| 1252 | data->channels = fdtdec_get_int(fdt, node, "cpdma_channels", -1); |
| 1253 | if (data->channels <= 0) { |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1254 | printf("error: cpdma_channels not found in dt\n"); |
| 1255 | return -ENOENT; |
| 1256 | } |
| 1257 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1258 | data->slaves = fdtdec_get_int(fdt, node, "slaves", -1); |
| 1259 | if (data->slaves <= 0) { |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1260 | printf("error: slaves not found in dt\n"); |
| 1261 | return -ENOENT; |
| 1262 | } |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1263 | data->slave_data = malloc(sizeof(struct cpsw_slave_data) * |
| 1264 | data->slaves); |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1265 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1266 | data->ale_entries = fdtdec_get_int(fdt, node, "ale_entries", -1); |
| 1267 | if (data->ale_entries <= 0) { |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1268 | printf("error: ale_entries not found in dt\n"); |
| 1269 | return -ENOENT; |
| 1270 | } |
| 1271 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1272 | data->bd_ram_ofs = fdtdec_get_int(fdt, node, "bd_ram_size", -1); |
| 1273 | if (data->bd_ram_ofs <= 0) { |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1274 | printf("error: bd_ram_size not found in dt\n"); |
| 1275 | return -ENOENT; |
| 1276 | } |
| 1277 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1278 | data->mac_control = fdtdec_get_int(fdt, node, "mac_control", -1); |
| 1279 | if (data->mac_control <= 0) { |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1280 | printf("error: ale_entries not found in dt\n"); |
| 1281 | return -ENOENT; |
| 1282 | } |
| 1283 | |
Vignesh R | 2e205ef | 2016-08-02 10:14:27 +0530 | [diff] [blame] | 1284 | num_mode_gpios = gpio_get_list_count(dev, "mode-gpios"); |
| 1285 | if (num_mode_gpios > 0) { |
| 1286 | mode_gpios = malloc(sizeof(struct gpio_desc) * |
| 1287 | num_mode_gpios); |
| 1288 | gpio_request_list_by_name(dev, "mode-gpios", mode_gpios, |
| 1289 | num_mode_gpios, GPIOD_IS_OUT); |
| 1290 | free(mode_gpios); |
| 1291 | } |
| 1292 | |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1293 | active_slave = fdtdec_get_int(fdt, node, "active_slave", 0); |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1294 | data->active_slave = active_slave; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1295 | |
Simon Glass | df87e6b | 2016-10-02 17:59:29 -0600 | [diff] [blame] | 1296 | fdt_for_each_subnode(subnode, fdt, node) { |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1297 | int len; |
| 1298 | const char *name; |
| 1299 | |
| 1300 | name = fdt_get_name(fdt, subnode, &len); |
| 1301 | if (!strncmp(name, "mdio", 4)) { |
Mugunthan V N | 66e740c | 2016-04-28 15:36:06 +0530 | [diff] [blame] | 1302 | u32 mdio_base; |
| 1303 | |
| 1304 | mdio_base = cpsw_get_addr_by_node(fdt, subnode); |
| 1305 | if (mdio_base == FDT_ADDR_T_NONE) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1306 | pr_err("Not able to get MDIO address space\n"); |
Mugunthan V N | 66e740c | 2016-04-28 15:36:06 +0530 | [diff] [blame] | 1307 | return -ENOENT; |
| 1308 | } |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1309 | data->mdio_base = mdio_base; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | if (!strncmp(name, "slave", 5)) { |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1313 | if (slave_index >= data->slaves) |
Mugunthan V N | b2003c5 | 2016-04-28 15:36:04 +0530 | [diff] [blame] | 1314 | continue; |
Dan Murphy | cb38622 | 2016-05-02 15:45:56 -0500 | [diff] [blame] | 1315 | |
Grygorii Strashko | 4040148 | 2019-09-19 11:16:38 +0300 | [diff] [blame] | 1316 | cpsw_eth_of_parse_slave(data, slave_index, subnode); |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1317 | slave_index++; |
| 1318 | } |
| 1319 | |
| 1320 | if (!strncmp(name, "cpsw-phy-sel", 12)) { |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1321 | data->gmii_sel = cpsw_get_addr_by_node(fdt, subnode); |
Mugunthan V N | 66e740c | 2016-04-28 15:36:06 +0530 | [diff] [blame] | 1322 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1323 | if (data->gmii_sel == FDT_ADDR_T_NONE) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1324 | pr_err("Not able to get gmii_sel reg address\n"); |
Mugunthan V N | 66e740c | 2016-04-28 15:36:06 +0530 | [diff] [blame] | 1325 | return -ENOENT; |
| 1326 | } |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1327 | |
| 1328 | if (fdt_get_property(fdt, subnode, "rmii-clock-ext", |
| 1329 | NULL)) |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1330 | data->rmii_clock_external = true; |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1331 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1332 | data->phy_sel_compat = fdt_getprop(fdt, subnode, |
| 1333 | "compatible", NULL); |
| 1334 | if (!data->phy_sel_compat) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1335 | pr_err("Not able to get gmii_sel compatible\n"); |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1336 | return -ENOENT; |
| 1337 | } |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1341 | data->slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET; |
| 1342 | data->slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1343 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1344 | if (data->slaves == 2) { |
| 1345 | data->slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET; |
| 1346 | data->slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1347 | } |
| 1348 | |
Faiz Abbas | a58d222 | 2019-03-18 13:54:34 +0530 | [diff] [blame] | 1349 | ret = ti_cm_get_macid_addr(dev, active_slave, data); |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 1350 | if (ret < 0) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1351 | pr_err("cpsw read efuse mac failed\n"); |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 1352 | return ret; |
| 1353 | } |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1354 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1355 | pdata->phy_interface = data->slave_data[active_slave].phy_if; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1356 | if (pdata->phy_interface == -1) { |
Grygorii Strashko | 4040148 | 2019-09-19 11:16:38 +0300 | [diff] [blame] | 1357 | debug("%s: Invalid PHY interface '%s'\n", __func__, |
| 1358 | phy_string_for_interface(pdata->phy_interface)); |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1359 | return -EINVAL; |
| 1360 | } |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1361 | |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1362 | return 0; |
| 1363 | } |
| 1364 | |
Faiz Abbas | c3b460a | 2019-03-18 13:54:35 +0530 | [diff] [blame] | 1365 | static const struct udevice_id cpsw_eth_ids[] = { |
| 1366 | { .compatible = "ti,cpsw" }, |
| 1367 | { .compatible = "ti,am335x-cpsw" }, |
| 1368 | { } |
| 1369 | }; |
| 1370 | #endif |
| 1371 | |
Sekhar Nori | e2597be | 2018-08-23 17:11:29 +0530 | [diff] [blame] | 1372 | int cpsw_get_slave_phy_addr(struct udevice *dev, int slave) |
| 1373 | { |
| 1374 | struct cpsw_priv *priv = dev_get_priv(dev); |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1375 | struct cpsw_platform_data *data = priv->data; |
Sekhar Nori | e2597be | 2018-08-23 17:11:29 +0530 | [diff] [blame] | 1376 | |
| 1377 | return data->slave_data[slave].phy_addr; |
| 1378 | } |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1379 | |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1380 | U_BOOT_DRIVER(eth_cpsw) = { |
| 1381 | .name = "eth_cpsw", |
| 1382 | .id = UCLASS_ETH, |
Faiz Abbas | c3b460a | 2019-03-18 13:54:35 +0530 | [diff] [blame] | 1383 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1384 | .of_match = cpsw_eth_ids, |
| 1385 | .ofdata_to_platdata = cpsw_eth_ofdata_to_platdata, |
Faiz Abbas | c3b460a | 2019-03-18 13:54:35 +0530 | [diff] [blame] | 1386 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 1387 | #endif |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1388 | .probe = cpsw_eth_probe, |
| 1389 | .ops = &cpsw_eth_ops, |
| 1390 | .priv_auto_alloc_size = sizeof(struct cpsw_priv), |
Faiz Abbas | 8a616cc | 2019-03-18 13:54:36 +0530 | [diff] [blame] | 1391 | .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_PRE_RELOC, |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1392 | }; |
| 1393 | #endif /* CONFIG_DM_ETH */ |