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; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 842 | |
Yegor Yefremov | cdd0729 | 2012-11-26 04:03:16 +0000 | [diff] [blame] | 843 | phydev = phy_connect(priv->bus, |
Mugunthan V N | 9c653aa | 2014-02-18 07:31:52 -0500 | [diff] [blame] | 844 | slave->data->phy_addr, |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 845 | priv->dev, |
Yegor Yefremov | cdd0729 | 2012-11-26 04:03:16 +0000 | [diff] [blame] | 846 | slave->data->phy_if); |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 847 | |
Heiko Schocher | 93ff255 | 2013-09-05 11:50:41 +0200 | [diff] [blame] | 848 | if (!phydev) |
| 849 | return -1; |
| 850 | |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 851 | phydev->supported &= supported; |
| 852 | phydev->advertising = phydev->supported; |
| 853 | |
Dan Murphy | cb38622 | 2016-05-02 15:45:56 -0500 | [diff] [blame] | 854 | #ifdef CONFIG_DM_ETH |
| 855 | if (slave->data->phy_of_handle) |
Grygorii Strashko | d4bb981 | 2018-07-05 12:02:51 -0500 | [diff] [blame] | 856 | phydev->node = offset_to_ofnode(slave->data->phy_of_handle); |
Dan Murphy | cb38622 | 2016-05-02 15:45:56 -0500 | [diff] [blame] | 857 | #endif |
| 858 | |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 859 | priv->phydev = phydev; |
| 860 | phy_config(phydev); |
| 861 | |
| 862 | return 1; |
| 863 | } |
| 864 | |
Sekhar Nori | e2597be | 2018-08-23 17:11:29 +0530 | [diff] [blame] | 865 | static void cpsw_phy_addr_update(struct cpsw_priv *priv) |
| 866 | { |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 867 | struct cpsw_platform_data *data = priv->data; |
Grygorii Strashko | 4f41cd9 | 2018-10-31 16:21:44 -0500 | [diff] [blame] | 868 | u16 alive = cpsw_mdio_get_alive(priv->bus); |
Sekhar Nori | e2597be | 2018-08-23 17:11:29 +0530 | [diff] [blame] | 869 | int active = data->active_slave; |
| 870 | int new_addr = ffs(alive) - 1; |
| 871 | |
| 872 | /* |
| 873 | * If there is only one phy alive and its address does not match |
| 874 | * that of active slave, then phy address can safely be updated. |
| 875 | */ |
| 876 | if (hweight16(alive) == 1 && |
| 877 | data->slave_data[active].phy_addr != new_addr) { |
| 878 | printf("Updated phy address for CPSW#%d, old: %d, new: %d\n", |
| 879 | active, data->slave_data[active].phy_addr, new_addr); |
| 880 | data->slave_data[active].phy_addr = new_addr; |
| 881 | } |
| 882 | } |
| 883 | |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 884 | int _cpsw_register(struct cpsw_priv *priv) |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 885 | { |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 886 | struct cpsw_slave *slave; |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 887 | struct cpsw_platform_data *data = priv->data; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 888 | void *regs = (void *)data->cpsw_base; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 889 | |
| 890 | priv->slaves = malloc(sizeof(struct cpsw_slave) * data->slaves); |
| 891 | if (!priv->slaves) { |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 892 | return -ENOMEM; |
| 893 | } |
| 894 | |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 895 | priv->host_port = data->host_port_num; |
| 896 | priv->regs = regs; |
| 897 | priv->host_port_regs = regs + data->host_port_reg_ofs; |
| 898 | priv->dma_regs = regs + data->cpdma_reg_ofs; |
| 899 | priv->ale_regs = regs + data->ale_reg_ofs; |
Mugunthan V N | 2bf36ac | 2013-07-08 16:04:37 +0530 | [diff] [blame] | 900 | priv->descs = (void *)regs + data->bd_ram_ofs; |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 901 | |
| 902 | int idx = 0; |
| 903 | |
| 904 | for_each_slave(slave, priv) { |
| 905 | cpsw_slave_setup(slave, idx, priv); |
| 906 | idx = idx + 1; |
| 907 | } |
| 908 | |
Grygorii Strashko | 4f41cd9 | 2018-10-31 16:21:44 -0500 | [diff] [blame] | 909 | priv->bus = cpsw_mdio_init(priv->dev->name, data->mdio_base, 0, 0); |
| 910 | if (!priv->bus) |
| 911 | return -EFAULT; |
Sekhar Nori | e2597be | 2018-08-23 17:11:29 +0530 | [diff] [blame] | 912 | |
| 913 | cpsw_phy_addr_update(priv); |
| 914 | |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 915 | for_active_slave(slave, priv) |
| 916 | cpsw_phy_init(priv, slave); |
| 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 921 | #ifndef CONFIG_DM_ETH |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 922 | static int cpsw_init(struct eth_device *dev, bd_t *bis) |
| 923 | { |
| 924 | struct cpsw_priv *priv = dev->priv; |
| 925 | |
| 926 | return _cpsw_init(priv, dev->enetaddr); |
| 927 | } |
| 928 | |
| 929 | static void cpsw_halt(struct eth_device *dev) |
| 930 | { |
| 931 | struct cpsw_priv *priv = dev->priv; |
| 932 | |
| 933 | return _cpsw_halt(priv); |
| 934 | } |
| 935 | |
| 936 | static int cpsw_send(struct eth_device *dev, void *packet, int length) |
| 937 | { |
| 938 | struct cpsw_priv *priv = dev->priv; |
| 939 | |
| 940 | return _cpsw_send(priv, packet, length); |
| 941 | } |
| 942 | |
| 943 | static int cpsw_recv(struct eth_device *dev) |
| 944 | { |
| 945 | struct cpsw_priv *priv = dev->priv; |
| 946 | uchar *pkt = NULL; |
| 947 | int len; |
| 948 | |
| 949 | len = _cpsw_recv(priv, &pkt); |
| 950 | |
| 951 | if (len > 0) { |
| 952 | net_process_received_packet(pkt, len); |
| 953 | cpdma_submit(priv, &priv->rx_chan, pkt, PKTSIZE); |
| 954 | } |
| 955 | |
| 956 | return len; |
| 957 | } |
| 958 | |
| 959 | int cpsw_register(struct cpsw_platform_data *data) |
| 960 | { |
| 961 | struct cpsw_priv *priv; |
| 962 | struct eth_device *dev; |
| 963 | int ret; |
| 964 | |
| 965 | dev = calloc(sizeof(*dev), 1); |
| 966 | if (!dev) |
| 967 | return -ENOMEM; |
| 968 | |
| 969 | priv = calloc(sizeof(*priv), 1); |
| 970 | if (!priv) { |
| 971 | free(dev); |
| 972 | return -ENOMEM; |
| 973 | } |
| 974 | |
| 975 | priv->dev = dev; |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 976 | priv->data = data; |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 977 | |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 978 | strcpy(dev->name, "cpsw"); |
| 979 | dev->iobase = 0; |
| 980 | dev->init = cpsw_init; |
| 981 | dev->halt = cpsw_halt; |
| 982 | dev->send = cpsw_send; |
| 983 | dev->recv = cpsw_recv; |
| 984 | dev->priv = priv; |
| 985 | |
| 986 | eth_register(dev); |
| 987 | |
Mugunthan V N | bcd5eed | 2015-09-07 14:22:20 +0530 | [diff] [blame] | 988 | ret = _cpsw_register(priv); |
| 989 | if (ret < 0) { |
| 990 | eth_unregister(dev); |
| 991 | free(dev); |
| 992 | free(priv); |
| 993 | return ret; |
| 994 | } |
Cyril Chemparathy | 2b62997 | 2012-07-24 12:22:16 +0000 | [diff] [blame] | 995 | |
| 996 | return 1; |
| 997 | } |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 998 | #else |
| 999 | static int cpsw_eth_start(struct udevice *dev) |
| 1000 | { |
| 1001 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 1002 | struct cpsw_priv *priv = dev_get_priv(dev); |
| 1003 | |
| 1004 | return _cpsw_init(priv, pdata->enetaddr); |
| 1005 | } |
| 1006 | |
| 1007 | static int cpsw_eth_send(struct udevice *dev, void *packet, int length) |
| 1008 | { |
| 1009 | struct cpsw_priv *priv = dev_get_priv(dev); |
| 1010 | |
| 1011 | return _cpsw_send(priv, packet, length); |
| 1012 | } |
| 1013 | |
| 1014 | static int cpsw_eth_recv(struct udevice *dev, int flags, uchar **packetp) |
| 1015 | { |
| 1016 | struct cpsw_priv *priv = dev_get_priv(dev); |
| 1017 | |
| 1018 | return _cpsw_recv(priv, packetp); |
| 1019 | } |
| 1020 | |
| 1021 | static int cpsw_eth_free_pkt(struct udevice *dev, uchar *packet, |
| 1022 | int length) |
| 1023 | { |
| 1024 | struct cpsw_priv *priv = dev_get_priv(dev); |
| 1025 | |
| 1026 | return cpdma_submit(priv, &priv->rx_chan, packet, PKTSIZE); |
| 1027 | } |
| 1028 | |
| 1029 | static void cpsw_eth_stop(struct udevice *dev) |
| 1030 | { |
| 1031 | struct cpsw_priv *priv = dev_get_priv(dev); |
| 1032 | |
| 1033 | return _cpsw_halt(priv); |
| 1034 | } |
| 1035 | |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1036 | static const struct eth_ops cpsw_eth_ops = { |
| 1037 | .start = cpsw_eth_start, |
| 1038 | .send = cpsw_eth_send, |
| 1039 | .recv = cpsw_eth_recv, |
| 1040 | .free_pkt = cpsw_eth_free_pkt, |
| 1041 | .stop = cpsw_eth_stop, |
| 1042 | }; |
| 1043 | |
Mugunthan V N | 66e740c | 2016-04-28 15:36:06 +0530 | [diff] [blame] | 1044 | static inline fdt_addr_t cpsw_get_addr_by_node(const void *fdt, int node) |
| 1045 | { |
Stephen Warren | 6e06acb | 2016-08-05 09:47:51 -0600 | [diff] [blame] | 1046 | return fdtdec_get_addr_size_auto_noparent(fdt, node, "reg", 0, NULL, |
| 1047 | false); |
Mugunthan V N | 66e740c | 2016-04-28 15:36:06 +0530 | [diff] [blame] | 1048 | } |
| 1049 | |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1050 | static void cpsw_gmii_sel_am3352(struct cpsw_priv *priv, |
| 1051 | phy_interface_t phy_mode) |
| 1052 | { |
| 1053 | u32 reg; |
| 1054 | u32 mask; |
| 1055 | u32 mode = 0; |
| 1056 | bool rgmii_id = false; |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1057 | int slave = priv->data->active_slave; |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1058 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1059 | reg = readl(priv->data->gmii_sel); |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1060 | |
| 1061 | switch (phy_mode) { |
| 1062 | case PHY_INTERFACE_MODE_RMII: |
| 1063 | mode = AM33XX_GMII_SEL_MODE_RMII; |
| 1064 | break; |
| 1065 | |
| 1066 | case PHY_INTERFACE_MODE_RGMII: |
| 1067 | mode = AM33XX_GMII_SEL_MODE_RGMII; |
| 1068 | break; |
| 1069 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 1070 | case PHY_INTERFACE_MODE_RGMII_RXID: |
| 1071 | case PHY_INTERFACE_MODE_RGMII_TXID: |
| 1072 | mode = AM33XX_GMII_SEL_MODE_RGMII; |
| 1073 | rgmii_id = true; |
| 1074 | break; |
| 1075 | |
| 1076 | case PHY_INTERFACE_MODE_MII: |
| 1077 | default: |
| 1078 | mode = AM33XX_GMII_SEL_MODE_MII; |
| 1079 | break; |
| 1080 | }; |
| 1081 | |
| 1082 | mask = GMII_SEL_MODE_MASK << (slave * 2) | BIT(slave + 6); |
| 1083 | mode <<= slave * 2; |
| 1084 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1085 | if (priv->data->rmii_clock_external) { |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1086 | if (slave == 0) |
| 1087 | mode |= AM33XX_GMII_SEL_RMII1_IO_CLK_EN; |
| 1088 | else |
| 1089 | mode |= AM33XX_GMII_SEL_RMII2_IO_CLK_EN; |
| 1090 | } |
| 1091 | |
| 1092 | if (rgmii_id) { |
| 1093 | if (slave == 0) |
| 1094 | mode |= AM33XX_GMII_SEL_RGMII1_IDMODE; |
| 1095 | else |
| 1096 | mode |= AM33XX_GMII_SEL_RGMII2_IDMODE; |
| 1097 | } |
| 1098 | |
| 1099 | reg &= ~mask; |
| 1100 | reg |= mode; |
| 1101 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1102 | writel(reg, priv->data->gmii_sel); |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | static void cpsw_gmii_sel_dra7xx(struct cpsw_priv *priv, |
| 1106 | phy_interface_t phy_mode) |
| 1107 | { |
| 1108 | u32 reg; |
| 1109 | u32 mask; |
| 1110 | u32 mode = 0; |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1111 | int slave = priv->data->active_slave; |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1112 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1113 | reg = readl(priv->data->gmii_sel); |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1114 | |
| 1115 | switch (phy_mode) { |
| 1116 | case PHY_INTERFACE_MODE_RMII: |
| 1117 | mode = AM33XX_GMII_SEL_MODE_RMII; |
| 1118 | break; |
| 1119 | |
| 1120 | case PHY_INTERFACE_MODE_RGMII: |
| 1121 | case PHY_INTERFACE_MODE_RGMII_ID: |
| 1122 | case PHY_INTERFACE_MODE_RGMII_RXID: |
| 1123 | case PHY_INTERFACE_MODE_RGMII_TXID: |
| 1124 | mode = AM33XX_GMII_SEL_MODE_RGMII; |
| 1125 | break; |
| 1126 | |
| 1127 | case PHY_INTERFACE_MODE_MII: |
| 1128 | default: |
| 1129 | mode = AM33XX_GMII_SEL_MODE_MII; |
| 1130 | break; |
| 1131 | }; |
| 1132 | |
| 1133 | switch (slave) { |
| 1134 | case 0: |
| 1135 | mask = GMII_SEL_MODE_MASK; |
| 1136 | break; |
| 1137 | case 1: |
| 1138 | mask = GMII_SEL_MODE_MASK << 4; |
| 1139 | mode <<= 4; |
| 1140 | break; |
| 1141 | default: |
| 1142 | dev_err(priv->dev, "invalid slave number...\n"); |
| 1143 | return; |
| 1144 | } |
| 1145 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1146 | if (priv->data->rmii_clock_external) |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1147 | dev_err(priv->dev, "RMII External clock is not supported\n"); |
| 1148 | |
| 1149 | reg &= ~mask; |
| 1150 | reg |= mode; |
| 1151 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1152 | writel(reg, priv->data->gmii_sel); |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | static void cpsw_phy_sel(struct cpsw_priv *priv, const char *compat, |
| 1156 | phy_interface_t phy_mode) |
| 1157 | { |
| 1158 | if (!strcmp(compat, "ti,am3352-cpsw-phy-sel")) |
| 1159 | cpsw_gmii_sel_am3352(priv, phy_mode); |
| 1160 | if (!strcmp(compat, "ti,am43xx-cpsw-phy-sel")) |
| 1161 | cpsw_gmii_sel_am3352(priv, phy_mode); |
| 1162 | else if (!strcmp(compat, "ti,dra7xx-cpsw-phy-sel")) |
| 1163 | cpsw_gmii_sel_dra7xx(priv, phy_mode); |
| 1164 | } |
| 1165 | |
Faiz Abbas | e50f878 | 2019-03-18 13:54:32 +0530 | [diff] [blame] | 1166 | static int cpsw_eth_probe(struct udevice *dev) |
| 1167 | { |
| 1168 | struct cpsw_priv *priv = dev_get_priv(dev); |
| 1169 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 1170 | |
| 1171 | priv->dev = dev; |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1172 | priv->data = pdata->priv_pdata; |
Faiz Abbas | a58d222 | 2019-03-18 13:54:34 +0530 | [diff] [blame] | 1173 | ti_cm_get_macid(dev, priv->data, pdata->enetaddr); |
Faiz Abbas | e50f878 | 2019-03-18 13:54:32 +0530 | [diff] [blame] | 1174 | /* Select phy interface in control module */ |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1175 | cpsw_phy_sel(priv, priv->data->phy_sel_compat, |
Faiz Abbas | e50f878 | 2019-03-18 13:54:32 +0530 | [diff] [blame] | 1176 | pdata->phy_interface); |
| 1177 | |
| 1178 | return _cpsw_register(priv); |
| 1179 | } |
| 1180 | |
Faiz Abbas | c3b460a | 2019-03-18 13:54:35 +0530 | [diff] [blame] | 1181 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1182 | static int cpsw_eth_ofdata_to_platdata(struct udevice *dev) |
| 1183 | { |
| 1184 | struct eth_pdata *pdata = dev_get_platdata(dev); |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1185 | struct cpsw_platform_data *data; |
Vignesh R | 2e205ef | 2016-08-02 10:14:27 +0530 | [diff] [blame] | 1186 | struct gpio_desc *mode_gpios; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1187 | const char *phy_mode; |
| 1188 | const void *fdt = gd->fdt_blob; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 1189 | int node = dev_of_offset(dev); |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1190 | int subnode; |
| 1191 | int slave_index = 0; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1192 | int active_slave; |
Vignesh R | 2e205ef | 2016-08-02 10:14:27 +0530 | [diff] [blame] | 1193 | int num_mode_gpios; |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 1194 | int ret; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1195 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1196 | data = calloc(1, sizeof(struct cpsw_platform_data)); |
| 1197 | pdata->priv_pdata = data; |
Simon Glass | a821c4a | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 1198 | pdata->iobase = devfdt_get_addr(dev); |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1199 | data->version = CPSW_CTRL_VERSION_2; |
| 1200 | data->bd_ram_ofs = CPSW_BD_OFFSET; |
| 1201 | data->ale_reg_ofs = CPSW_ALE_OFFSET; |
| 1202 | data->cpdma_reg_ofs = CPSW_CPDMA_OFFSET; |
| 1203 | data->mdio_div = CPSW_MDIO_DIV; |
| 1204 | data->host_port_reg_ofs = CPSW_HOST_PORT_OFFSET, |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1205 | |
| 1206 | pdata->phy_interface = -1; |
| 1207 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1208 | data->cpsw_base = pdata->iobase; |
| 1209 | data->channels = fdtdec_get_int(fdt, node, "cpdma_channels", -1); |
| 1210 | if (data->channels <= 0) { |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1211 | printf("error: cpdma_channels not found in dt\n"); |
| 1212 | return -ENOENT; |
| 1213 | } |
| 1214 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1215 | data->slaves = fdtdec_get_int(fdt, node, "slaves", -1); |
| 1216 | if (data->slaves <= 0) { |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1217 | printf("error: slaves not found in dt\n"); |
| 1218 | return -ENOENT; |
| 1219 | } |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1220 | data->slave_data = malloc(sizeof(struct cpsw_slave_data) * |
| 1221 | data->slaves); |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1222 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1223 | data->ale_entries = fdtdec_get_int(fdt, node, "ale_entries", -1); |
| 1224 | if (data->ale_entries <= 0) { |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1225 | printf("error: ale_entries not found in dt\n"); |
| 1226 | return -ENOENT; |
| 1227 | } |
| 1228 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1229 | data->bd_ram_ofs = fdtdec_get_int(fdt, node, "bd_ram_size", -1); |
| 1230 | if (data->bd_ram_ofs <= 0) { |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1231 | printf("error: bd_ram_size not found in dt\n"); |
| 1232 | return -ENOENT; |
| 1233 | } |
| 1234 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1235 | data->mac_control = fdtdec_get_int(fdt, node, "mac_control", -1); |
| 1236 | if (data->mac_control <= 0) { |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1237 | printf("error: ale_entries not found in dt\n"); |
| 1238 | return -ENOENT; |
| 1239 | } |
| 1240 | |
Vignesh R | 2e205ef | 2016-08-02 10:14:27 +0530 | [diff] [blame] | 1241 | num_mode_gpios = gpio_get_list_count(dev, "mode-gpios"); |
| 1242 | if (num_mode_gpios > 0) { |
| 1243 | mode_gpios = malloc(sizeof(struct gpio_desc) * |
| 1244 | num_mode_gpios); |
| 1245 | gpio_request_list_by_name(dev, "mode-gpios", mode_gpios, |
| 1246 | num_mode_gpios, GPIOD_IS_OUT); |
| 1247 | free(mode_gpios); |
| 1248 | } |
| 1249 | |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1250 | active_slave = fdtdec_get_int(fdt, node, "active_slave", 0); |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1251 | data->active_slave = active_slave; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1252 | |
Simon Glass | df87e6b | 2016-10-02 17:59:29 -0600 | [diff] [blame] | 1253 | fdt_for_each_subnode(subnode, fdt, node) { |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1254 | int len; |
| 1255 | const char *name; |
| 1256 | |
| 1257 | name = fdt_get_name(fdt, subnode, &len); |
| 1258 | if (!strncmp(name, "mdio", 4)) { |
Mugunthan V N | 66e740c | 2016-04-28 15:36:06 +0530 | [diff] [blame] | 1259 | u32 mdio_base; |
| 1260 | |
| 1261 | mdio_base = cpsw_get_addr_by_node(fdt, subnode); |
| 1262 | if (mdio_base == FDT_ADDR_T_NONE) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1263 | pr_err("Not able to get MDIO address space\n"); |
Mugunthan V N | 66e740c | 2016-04-28 15:36:06 +0530 | [diff] [blame] | 1264 | return -ENOENT; |
| 1265 | } |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1266 | data->mdio_base = mdio_base; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | if (!strncmp(name, "slave", 5)) { |
| 1270 | u32 phy_id[2]; |
| 1271 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1272 | if (slave_index >= data->slaves) |
Mugunthan V N | b2003c5 | 2016-04-28 15:36:04 +0530 | [diff] [blame] | 1273 | continue; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1274 | phy_mode = fdt_getprop(fdt, subnode, "phy-mode", NULL); |
| 1275 | if (phy_mode) |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1276 | data->slave_data[slave_index].phy_if = |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1277 | phy_get_interface_by_name(phy_mode); |
Dan Murphy | cb38622 | 2016-05-02 15:45:56 -0500 | [diff] [blame] | 1278 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1279 | data->slave_data[slave_index].phy_of_handle = |
Dan Murphy | cb38622 | 2016-05-02 15:45:56 -0500 | [diff] [blame] | 1280 | fdtdec_lookup_phandle(fdt, subnode, |
| 1281 | "phy-handle"); |
| 1282 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1283 | if (data->slave_data[slave_index].phy_of_handle >= 0) { |
| 1284 | data->slave_data[slave_index].phy_addr = |
Dan Murphy | cb38622 | 2016-05-02 15:45:56 -0500 | [diff] [blame] | 1285 | fdtdec_get_int(gd->fdt_blob, |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1286 | data->slave_data[slave_index].phy_of_handle, |
Dan Murphy | cb38622 | 2016-05-02 15:45:56 -0500 | [diff] [blame] | 1287 | "reg", -1); |
| 1288 | } else { |
| 1289 | fdtdec_get_int_array(fdt, subnode, "phy_id", |
| 1290 | phy_id, 2); |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1291 | data->slave_data[slave_index].phy_addr = |
Dan Murphy | cb38622 | 2016-05-02 15:45:56 -0500 | [diff] [blame] | 1292 | phy_id[1]; |
| 1293 | } |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1294 | slave_index++; |
| 1295 | } |
| 1296 | |
| 1297 | if (!strncmp(name, "cpsw-phy-sel", 12)) { |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1298 | data->gmii_sel = cpsw_get_addr_by_node(fdt, subnode); |
Mugunthan V N | 66e740c | 2016-04-28 15:36:06 +0530 | [diff] [blame] | 1299 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1300 | if (data->gmii_sel == FDT_ADDR_T_NONE) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1301 | pr_err("Not able to get gmii_sel reg address\n"); |
Mugunthan V N | 66e740c | 2016-04-28 15:36:06 +0530 | [diff] [blame] | 1302 | return -ENOENT; |
| 1303 | } |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1304 | |
| 1305 | if (fdt_get_property(fdt, subnode, "rmii-clock-ext", |
| 1306 | NULL)) |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1307 | data->rmii_clock_external = true; |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1308 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1309 | data->phy_sel_compat = fdt_getprop(fdt, subnode, |
| 1310 | "compatible", NULL); |
| 1311 | if (!data->phy_sel_compat) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1312 | pr_err("Not able to get gmii_sel compatible\n"); |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1313 | return -ENOENT; |
| 1314 | } |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1318 | data->slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET; |
| 1319 | data->slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1320 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1321 | if (data->slaves == 2) { |
| 1322 | data->slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET; |
| 1323 | data->slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1324 | } |
| 1325 | |
Faiz Abbas | a58d222 | 2019-03-18 13:54:34 +0530 | [diff] [blame] | 1326 | ret = ti_cm_get_macid_addr(dev, active_slave, data); |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 1327 | if (ret < 0) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 1328 | pr_err("cpsw read efuse mac failed\n"); |
Mugunthan V N | e431056 | 2016-04-28 15:36:07 +0530 | [diff] [blame] | 1329 | return ret; |
| 1330 | } |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1331 | |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1332 | pdata->phy_interface = data->slave_data[active_slave].phy_if; |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1333 | if (pdata->phy_interface == -1) { |
| 1334 | debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode); |
| 1335 | return -EINVAL; |
| 1336 | } |
Mugunthan V N | ab97153 | 2016-10-13 19:33:38 +0530 | [diff] [blame] | 1337 | |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1338 | return 0; |
| 1339 | } |
| 1340 | |
Faiz Abbas | c3b460a | 2019-03-18 13:54:35 +0530 | [diff] [blame] | 1341 | static const struct udevice_id cpsw_eth_ids[] = { |
| 1342 | { .compatible = "ti,cpsw" }, |
| 1343 | { .compatible = "ti,am335x-cpsw" }, |
| 1344 | { } |
| 1345 | }; |
| 1346 | #endif |
| 1347 | |
Sekhar Nori | e2597be | 2018-08-23 17:11:29 +0530 | [diff] [blame] | 1348 | int cpsw_get_slave_phy_addr(struct udevice *dev, int slave) |
| 1349 | { |
| 1350 | struct cpsw_priv *priv = dev_get_priv(dev); |
Faiz Abbas | f32a816 | 2019-03-18 13:54:33 +0530 | [diff] [blame] | 1351 | struct cpsw_platform_data *data = priv->data; |
Sekhar Nori | e2597be | 2018-08-23 17:11:29 +0530 | [diff] [blame] | 1352 | |
| 1353 | return data->slave_data[slave].phy_addr; |
| 1354 | } |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1355 | |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1356 | U_BOOT_DRIVER(eth_cpsw) = { |
| 1357 | .name = "eth_cpsw", |
| 1358 | .id = UCLASS_ETH, |
Faiz Abbas | c3b460a | 2019-03-18 13:54:35 +0530 | [diff] [blame] | 1359 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1360 | .of_match = cpsw_eth_ids, |
| 1361 | .ofdata_to_platdata = cpsw_eth_ofdata_to_platdata, |
Faiz Abbas | c3b460a | 2019-03-18 13:54:35 +0530 | [diff] [blame] | 1362 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 1363 | #endif |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1364 | .probe = cpsw_eth_probe, |
| 1365 | .ops = &cpsw_eth_ops, |
| 1366 | .priv_auto_alloc_size = sizeof(struct cpsw_priv), |
Faiz Abbas | 8a616cc | 2019-03-18 13:54:36 +0530 | [diff] [blame] | 1367 | .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_PRE_RELOC, |
Mugunthan V N | 4cc7789 | 2015-09-07 14:22:21 +0530 | [diff] [blame] | 1368 | }; |
| 1369 | #endif /* CONFIG_DM_ETH */ |