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