blob: 0ba84a4496f5628199c21c0f43153582ed3ab703 [file] [log] [blame]
Vladimir Oltean67bfc852021-09-29 18:04:41 +03001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2016-2018 NXP
4 * Copyright 2018, Sensor-Technik Wiedemann GmbH
5 * Copyright 2018-2019, Vladimir Oltean <olteanv@gmail.com>
6 * Copyright 2020-2021 NXP
7 *
8 * Ported from Linux (drivers/net/dsa/sja1105/).
9 */
10
Vladimir Oltean67bfc852021-09-29 18:04:41 +030011#include <dm/device_compat.h>
12#include <linux/bitops.h>
13#include <linux/bitrev.h>
14#include <linux/errno.h>
15#include <linux/delay.h>
16#include <linux/if_ether.h>
17#include <linux/if_vlan.h>
18#include <linux/types.h>
19#include <net/dsa.h>
20#include <stdlib.h>
21#include <spi.h>
22#include <miiphy.h>
23#include <dm/of_extra.h>
24
25enum packing_op {
26 PACK,
27 UNPACK,
28};
29
30#define ETHER_CRC32_POLY 0x04C11DB7
31#define ETH_P_SJA1105 0xdadb
32#define SJA1105_NUM_PORTS 5
33#define SJA1110_NUM_PORTS 11
34#define SJA1105_MAX_NUM_PORTS SJA1110_NUM_PORTS
35#define SJA1105_NUM_TC 8
36#define SJA1105ET_FDB_BIN_SIZE 4
37#define SJA1105_SIZE_CGU_CMD 4
38#define SJA1105_SIZE_RESET_CMD 4
Vladimir Oltean6892d6a2021-09-29 18:04:42 +030039#define SJA1105_SIZE_MDIO_CMD 4
Vladimir Oltean67bfc852021-09-29 18:04:41 +030040#define SJA1105_SIZE_SPI_MSG_HEADER 4
41#define SJA1105_SIZE_SPI_MSG_MAXLEN (64 * 4)
42#define SJA1105_SIZE_DEVICE_ID 4
43#define SJA1105_SIZE_TABLE_HEADER 12
44#define SJA1105_SIZE_L2_POLICING_ENTRY 8
45#define SJA1105_SIZE_VLAN_LOOKUP_ENTRY 8
46#define SJA1110_SIZE_VLAN_LOOKUP_ENTRY 12
47#define SJA1105_SIZE_L2_FORWARDING_ENTRY 8
48#define SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY 12
49#define SJA1105_SIZE_XMII_PARAMS_ENTRY 4
50#define SJA1110_SIZE_XMII_PARAMS_ENTRY 8
51#define SJA1105ET_SIZE_MAC_CONFIG_ENTRY 28
52#define SJA1105ET_SIZE_GENERAL_PARAMS_ENTRY 40
53#define SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY 32
54#define SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY 44
55#define SJA1110_SIZE_GENERAL_PARAMS_ENTRY 56
56
57#define SJA1105_MAX_L2_LOOKUP_COUNT 1024
58#define SJA1105_MAX_L2_POLICING_COUNT 45
59#define SJA1110_MAX_L2_POLICING_COUNT 110
60#define SJA1105_MAX_VLAN_LOOKUP_COUNT 4096
61#define SJA1105_MAX_L2_FORWARDING_COUNT 13
62#define SJA1110_MAX_L2_FORWARDING_COUNT 19
63#define SJA1105_MAX_MAC_CONFIG_COUNT 5
64#define SJA1110_MAX_MAC_CONFIG_COUNT 11
65#define SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT 1
66#define SJA1105_MAX_GENERAL_PARAMS_COUNT 1
67#define SJA1105_MAX_XMII_PARAMS_COUNT 1
68
69#define SJA1105_MAX_FRAME_MEMORY 929
70
71#define SJA1105E_DEVICE_ID 0x9C00000Cull
72#define SJA1105T_DEVICE_ID 0x9E00030Eull
73#define SJA1105PR_DEVICE_ID 0xAF00030Eull
74#define SJA1105QS_DEVICE_ID 0xAE00030Eull
75#define SJA1110_DEVICE_ID 0xB700030Full
76
77#define SJA1105ET_PART_NO 0x9A83
78#define SJA1105P_PART_NO 0x9A84
79#define SJA1105Q_PART_NO 0x9A85
80#define SJA1105R_PART_NO 0x9A86
81#define SJA1105S_PART_NO 0x9A87
82#define SJA1110A_PART_NO 0x1110
83#define SJA1110B_PART_NO 0x1111
84#define SJA1110C_PART_NO 0x1112
85#define SJA1110D_PART_NO 0x1113
86
87#define SJA1110_ACU 0x1c4400
88#define SJA1110_RGU 0x1c6000
89#define SJA1110_CGU 0x1c6400
90
91#define SJA1110_SPI_ADDR(x) ((x) / 4)
92#define SJA1110_ACU_ADDR(x) (SJA1110_ACU + SJA1110_SPI_ADDR(x))
93#define SJA1110_CGU_ADDR(x) (SJA1110_CGU + SJA1110_SPI_ADDR(x))
94#define SJA1110_RGU_ADDR(x) (SJA1110_RGU + SJA1110_SPI_ADDR(x))
95
96#define SJA1105_RSV_ADDR 0xffffffffffffffffull
97
Vladimir Oltean6892d6a2021-09-29 18:04:42 +030098#define SJA1110_PCS_BANK_REG SJA1110_SPI_ADDR(0x3fc)
99
Vladimir Oltean67bfc852021-09-29 18:04:41 +0300100#define DSA_8021Q_DIR_TX BIT(11)
101#define DSA_8021Q_PORT_SHIFT 0
102#define DSA_8021Q_PORT_MASK GENMASK(3, 0)
103#define DSA_8021Q_PORT(x) (((x) << DSA_8021Q_PORT_SHIFT) & \
104 DSA_8021Q_PORT_MASK)
105
106#define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
107
Vladimir Oltean6892d6a2021-09-29 18:04:42 +0300108/* XPCS registers */
109
110/* VR MII MMD registers offsets */
111#define DW_VR_MII_DIG_CTRL1 0x8000
112#define DW_VR_MII_AN_CTRL 0x8001
113#define DW_VR_MII_DIG_CTRL2 0x80e1
114
115/* VR_MII_DIG_CTRL1 */
116#define DW_VR_MII_DIG_CTRL1_MAC_AUTO_SW BIT(9)
117
118/* VR_MII_DIG_CTRL2 */
119#define DW_VR_MII_DIG_CTRL2_TX_POL_INV BIT(4)
120
121/* VR_MII_AN_CTRL */
122#define DW_VR_MII_AN_CTRL_TX_CONFIG_SHIFT 3
123#define DW_VR_MII_TX_CONFIG_MASK BIT(3)
124#define DW_VR_MII_TX_CONFIG_MAC_SIDE_SGMII 0x0
125#define DW_VR_MII_AN_CTRL_PCS_MODE_SHIFT 1
126#define DW_VR_MII_PCS_MODE_MASK GENMASK(2, 1)
127#define DW_VR_MII_PCS_MODE_C37_SGMII 0x2
128
129/* PMA registers */
130
131/* LANE_DRIVER1_0 register */
132#define SJA1110_LANE_DRIVER1_0 0x8038
133#define SJA1110_TXDRV(x) (((x) << 12) & GENMASK(14, 12))
134
135/* LANE_DRIVER2_0 register */
136#define SJA1110_LANE_DRIVER2_0 0x803a
137#define SJA1110_TXDRVTRIM_LSB(x) ((x) & GENMASK_ULL(15, 0))
138
139/* LANE_DRIVER2_1 register */
140#define SJA1110_LANE_DRIVER2_1 0x803b
141#define SJA1110_LANE_DRIVER2_1_RSV BIT(9)
142#define SJA1110_TXDRVTRIM_MSB(x) (((x) & GENMASK_ULL(23, 16)) >> 16)
143
144/* LANE_TRIM register */
145#define SJA1110_LANE_TRIM 0x8040
146#define SJA1110_TXTEN BIT(11)
147#define SJA1110_TXRTRIM(x) (((x) << 8) & GENMASK(10, 8))
148#define SJA1110_TXPLL_BWSEL BIT(7)
149#define SJA1110_RXTEN BIT(6)
150#define SJA1110_RXRTRIM(x) (((x) << 3) & GENMASK(5, 3))
151#define SJA1110_CDR_GAIN BIT(2)
152#define SJA1110_ACCOUPLE_RXVCM_EN BIT(0)
153
154/* LANE_DATAPATH_1 register */
155#define SJA1110_LANE_DATAPATH_1 0x8037
156
157/* POWERDOWN_ENABLE register */
158#define SJA1110_POWERDOWN_ENABLE 0x8041
159#define SJA1110_TXPLL_PD BIT(12)
160#define SJA1110_TXPD BIT(11)
161#define SJA1110_RXPKDETEN BIT(10)
162#define SJA1110_RXCH_PD BIT(9)
163#define SJA1110_RXBIAS_PD BIT(8)
164#define SJA1110_RESET_SER_EN BIT(7)
165#define SJA1110_RESET_SER BIT(6)
166#define SJA1110_RESET_DES BIT(5)
167#define SJA1110_RCVEN BIT(4)
168
169/* RXPLL_CTRL0 register */
170#define SJA1110_RXPLL_CTRL0 0x8065
171#define SJA1110_RXPLL_FBDIV(x) (((x) << 2) & GENMASK(9, 2))
172
173/* RXPLL_CTRL1 register */
174#define SJA1110_RXPLL_CTRL1 0x8066
175#define SJA1110_RXPLL_REFDIV(x) ((x) & GENMASK(4, 0))
176
177/* TXPLL_CTRL0 register */
178#define SJA1110_TXPLL_CTRL0 0x806d
179#define SJA1110_TXPLL_FBDIV(x) ((x) & GENMASK(11, 0))
180
181/* TXPLL_CTRL1 register */
182#define SJA1110_TXPLL_CTRL1 0x806e
183#define SJA1110_TXPLL_REFDIV(x) ((x) & GENMASK(5, 0))
184
185/* RX_DATA_DETECT register */
186#define SJA1110_RX_DATA_DETECT 0x8045
187
188/* RX_CDR_CTLE register */
189#define SJA1110_RX_CDR_CTLE 0x8042
190
Vladimir Oltean67bfc852021-09-29 18:04:41 +0300191/* UM10944.pdf Page 11, Table 2. Configuration Blocks */
192enum {
193 BLKID_L2_POLICING = 0x06,
194 BLKID_VLAN_LOOKUP = 0x07,
195 BLKID_L2_FORWARDING = 0x08,
196 BLKID_MAC_CONFIG = 0x09,
197 BLKID_L2_FORWARDING_PARAMS = 0x0E,
198 BLKID_GENERAL_PARAMS = 0x11,
199 BLKID_XMII_PARAMS = 0x4E,
200};
201
202enum sja1105_blk_idx {
203 BLK_IDX_L2_POLICING = 0,
204 BLK_IDX_VLAN_LOOKUP,
205 BLK_IDX_L2_FORWARDING,
206 BLK_IDX_MAC_CONFIG,
207 BLK_IDX_L2_FORWARDING_PARAMS,
208 BLK_IDX_GENERAL_PARAMS,
209 BLK_IDX_XMII_PARAMS,
210 BLK_IDX_MAX,
211};
212
213struct sja1105_general_params_entry {
214 u64 mac_fltres1;
215 u64 mac_fltres0;
216 u64 mac_flt1;
217 u64 mac_flt0;
218 u64 casc_port;
219 u64 host_port;
220 u64 mirr_port;
221 u64 tpid;
222 u64 tpid2;
223};
224
225struct sja1105_vlan_lookup_entry {
226 u64 vmemb_port;
227 u64 vlan_bc;
228 u64 tag_port;
229 u64 vlanid;
230 u64 type_entry; /* SJA1110 only */
231};
232
233struct sja1105_l2_forwarding_entry {
234 u64 bc_domain;
235 u64 reach_port;
236 u64 fl_domain;
237};
238
239struct sja1105_l2_forwarding_params_entry {
240 u64 part_spc[SJA1105_NUM_TC];
241};
242
243struct sja1105_l2_policing_entry {
244 u64 sharindx;
245 u64 smax;
246 u64 rate;
247 u64 maxlen;
248 u64 partition;
249};
250
251struct sja1105_mac_config_entry {
252 u64 top[SJA1105_NUM_TC];
253 u64 base[SJA1105_NUM_TC];
254 u64 enabled[SJA1105_NUM_TC];
255 u64 speed;
256 u64 vlanid;
257 u64 egress;
258 u64 ingress;
259};
260
261struct sja1105_xmii_params_entry {
262 u64 phy_mac[SJA1105_MAX_NUM_PORTS];
263 u64 xmii_mode[SJA1105_MAX_NUM_PORTS];
264 u64 special[SJA1105_MAX_NUM_PORTS];
265};
266
267struct sja1105_table_header {
268 u64 block_id;
269 u64 len;
270 u64 crc;
271};
272
273struct sja1105_table_ops {
274 size_t (*packing)(void *buf, void *entry_ptr, enum packing_op op);
275 size_t unpacked_entry_size;
276 size_t packed_entry_size;
277 size_t max_entry_count;
278};
279
280struct sja1105_table {
281 const struct sja1105_table_ops *ops;
282 size_t entry_count;
283 void *entries;
284};
285
286struct sja1105_static_config {
287 u64 device_id;
288 struct sja1105_table tables[BLK_IDX_MAX];
289};
290
Vladimir Oltean6892d6a2021-09-29 18:04:42 +0300291struct sja1105_xpcs_cfg {
292 bool inband_an;
293 int speed;
294};
295
Vladimir Oltean67bfc852021-09-29 18:04:41 +0300296struct sja1105_private {
297 struct sja1105_static_config static_config;
298 bool rgmii_rx_delay[SJA1105_MAX_NUM_PORTS];
299 bool rgmii_tx_delay[SJA1105_MAX_NUM_PORTS];
300 u16 pvid[SJA1105_MAX_NUM_PORTS];
Vladimir Oltean6892d6a2021-09-29 18:04:42 +0300301 struct sja1105_xpcs_cfg xpcs_cfg[SJA1105_MAX_NUM_PORTS];
302 struct mii_dev *mdio_pcs;
Vladimir Oltean67bfc852021-09-29 18:04:41 +0300303 const struct sja1105_info *info;
304 struct udevice *dev;
305};
306
307typedef enum {
308 SPI_READ = 0,
309 SPI_WRITE = 1,
310} sja1105_spi_rw_mode_t;
311
312typedef enum {
313 XMII_MAC = 0,
314 XMII_PHY = 1,
315} sja1105_mii_role_t;
316
317typedef enum {
318 XMII_MODE_MII = 0,
319 XMII_MODE_RMII = 1,
320 XMII_MODE_RGMII = 2,
Vladimir Oltean6892d6a2021-09-29 18:04:42 +0300321 XMII_MODE_SGMII = 3,
Vladimir Oltean67bfc852021-09-29 18:04:41 +0300322} sja1105_phy_interface_t;
323
324enum {
325 SJA1105_SPEED_AUTO,
326 SJA1105_SPEED_10MBPS,
327 SJA1105_SPEED_100MBPS,
328 SJA1105_SPEED_1000MBPS,
329 SJA1105_SPEED_MAX,
330};
331
332enum sja1110_vlan_type {
333 SJA1110_VLAN_INVALID = 0,
334 SJA1110_VLAN_C_TAG = 1, /* Single inner VLAN tag */
335 SJA1110_VLAN_S_TAG = 2, /* Single outer VLAN tag */
336 SJA1110_VLAN_D_TAG = 3, /* Double tagged, use outer tag for lookup */
337};
338
339/* Keeps the different addresses between E/T and P/Q/R/S */
340struct sja1105_regs {
341 u64 device_id;
342 u64 prod_id;
343 u64 status;
344 u64 port_control;
345 u64 rgu;
346 u64 config;
347 u64 rmii_pll1;
348 u64 pad_mii_tx[SJA1105_MAX_NUM_PORTS];
349 u64 pad_mii_rx[SJA1105_MAX_NUM_PORTS];
350 u64 pad_mii_id[SJA1105_MAX_NUM_PORTS];
351 u64 cgu_idiv[SJA1105_MAX_NUM_PORTS];
352 u64 mii_tx_clk[SJA1105_MAX_NUM_PORTS];
353 u64 mii_rx_clk[SJA1105_MAX_NUM_PORTS];
354 u64 mii_ext_tx_clk[SJA1105_MAX_NUM_PORTS];
355 u64 mii_ext_rx_clk[SJA1105_MAX_NUM_PORTS];
356 u64 rgmii_tx_clk[SJA1105_MAX_NUM_PORTS];
357 u64 rmii_ref_clk[SJA1105_MAX_NUM_PORTS];
358 u64 rmii_ext_tx_clk[SJA1105_MAX_NUM_PORTS];
Vladimir Oltean6892d6a2021-09-29 18:04:42 +0300359 u64 pcs_base[SJA1105_MAX_NUM_PORTS];
Vladimir Oltean67bfc852021-09-29 18:04:41 +0300360};
361
362struct sja1105_info {
363 u64 device_id;
364 u64 part_no;
365 const struct sja1105_table_ops *static_ops;
366 const struct sja1105_regs *regs;
367 int (*reset_cmd)(struct sja1105_private *priv);
368 int (*setup_rgmii_delay)(struct sja1105_private *priv, int port);
Vladimir Oltean6892d6a2021-09-29 18:04:42 +0300369 int (*pcs_mdio_read)(struct mii_dev *bus, int phy, int mmd, int reg);
370 int (*pcs_mdio_write)(struct mii_dev *bus, int phy, int mmd, int reg,
371 u16 val);
372 int (*pma_config)(struct sja1105_private *priv, int port);
Vladimir Oltean67bfc852021-09-29 18:04:41 +0300373 const char *name;
374 bool supports_mii[SJA1105_MAX_NUM_PORTS];
375 bool supports_rmii[SJA1105_MAX_NUM_PORTS];
376 bool supports_rgmii[SJA1105_MAX_NUM_PORTS];
Vladimir Oltean6892d6a2021-09-29 18:04:42 +0300377 bool supports_sgmii[SJA1105_MAX_NUM_PORTS];
Vladimir Oltean67bfc852021-09-29 18:04:41 +0300378 const u64 port_speed[SJA1105_SPEED_MAX];
379};
380
381struct sja1105_chunk {
382 u8 *buf;
383 size_t len;
384 u64 reg_addr;
385};
386
387struct sja1105_spi_message {
388 u64 access;
389 u64 read_count;
390 u64 address;
391};
392
393/* Common structure for CFG_PAD_MIIx_RX and CFG_PAD_MIIx_TX */
394struct sja1105_cfg_pad_mii {
395 u64 d32_os;
396 u64 d32_ih;
397 u64 d32_ipud;
398 u64 d10_ih;
399 u64 d10_os;
400 u64 d10_ipud;
401 u64 ctrl_os;
402 u64 ctrl_ih;
403 u64 ctrl_ipud;
404 u64 clk_os;
405 u64 clk_ih;
406 u64 clk_ipud;
407};
408
409struct sja1105_cfg_pad_mii_id {
410 u64 rxc_stable_ovr;
411 u64 rxc_delay;
412 u64 rxc_bypass;
413 u64 rxc_pd;
414 u64 txc_stable_ovr;
415 u64 txc_delay;
416 u64 txc_bypass;
417 u64 txc_pd;
418};
419
420struct sja1105_cgu_idiv {
421 u64 clksrc;
422 u64 autoblock;
423 u64 idiv;
424 u64 pd;
425};
426
427struct sja1105_cgu_pll_ctrl {
428 u64 pllclksrc;
429 u64 msel;
430 u64 autoblock;
431 u64 psel;
432 u64 direct;
433 u64 fbsel;
434 u64 bypass;
435 u64 pd;
436};
437
438enum {
439 CLKSRC_MII0_TX_CLK = 0x00,
440 CLKSRC_MII0_RX_CLK = 0x01,
441 CLKSRC_MII1_TX_CLK = 0x02,
442 CLKSRC_MII1_RX_CLK = 0x03,
443 CLKSRC_MII2_TX_CLK = 0x04,
444 CLKSRC_MII2_RX_CLK = 0x05,
445 CLKSRC_MII3_TX_CLK = 0x06,
446 CLKSRC_MII3_RX_CLK = 0x07,
447 CLKSRC_MII4_TX_CLK = 0x08,
448 CLKSRC_MII4_RX_CLK = 0x09,
449 CLKSRC_PLL0 = 0x0B,
450 CLKSRC_PLL1 = 0x0E,
451 CLKSRC_IDIV0 = 0x11,
452 CLKSRC_IDIV1 = 0x12,
453 CLKSRC_IDIV2 = 0x13,
454 CLKSRC_IDIV3 = 0x14,
455 CLKSRC_IDIV4 = 0x15,
456};
457
458struct sja1105_cgu_mii_ctrl {
459 u64 clksrc;
460 u64 autoblock;
461 u64 pd;
462};
463
464static int get_reverse_lsw32_offset(int offset, size_t len)
465{
466 int closest_multiple_of_4;
467 int word_index;
468
469 word_index = offset / 4;
470 closest_multiple_of_4 = word_index * 4;
471 offset -= closest_multiple_of_4;
472 word_index = (len / 4) - word_index - 1;
473 return word_index * 4 + offset;
474}
475
476/* Simplified version of the "packing" function from Linux, adapted
477 * to support only sja1105's quirk: QUIRK_LSW32_IS_FIRST
478 */
479static void sja1105_packing(void *pbuf, u64 *uval, int startbit, int endbit,
480 size_t pbuflen, enum packing_op op)
481{
482 int plogical_first_u8, plogical_last_u8, box;
483
484 if (op == UNPACK)
485 *uval = 0;
486
487 plogical_first_u8 = startbit / 8;
488 plogical_last_u8 = endbit / 8;
489
490 for (box = plogical_first_u8; box >= plogical_last_u8; box--) {
491 int box_start_bit, box_end_bit, box_addr;
492 int proj_start_bit, proj_end_bit;
493 u64 proj_mask;
494 u8 box_mask;
495
496 if (box == plogical_first_u8)
497 box_start_bit = startbit % 8;
498 else
499 box_start_bit = 7;
500 if (box == plogical_last_u8)
501 box_end_bit = endbit % 8;
502 else
503 box_end_bit = 0;
504
505 proj_start_bit = ((box * 8) + box_start_bit) - endbit;
506 proj_end_bit = ((box * 8) + box_end_bit) - endbit;
507 proj_mask = GENMASK_ULL(proj_start_bit, proj_end_bit);
508 box_mask = GENMASK_ULL(box_start_bit, box_end_bit);
509
510 box_addr = pbuflen - box - 1;
511 box_addr = get_reverse_lsw32_offset(box_addr, pbuflen);
512
513 if (op == UNPACK) {
514 u64 pval;
515
516 /* Read from pbuf, write to uval */
517 pval = ((u8 *)pbuf)[box_addr] & box_mask;
518
519 pval >>= box_end_bit;
520 pval <<= proj_end_bit;
521 *uval &= ~proj_mask;
522 *uval |= pval;
523 } else {
524 u64 pval;
525
526 /* Write to pbuf, read from uval */
527 pval = (*uval) & proj_mask;
528 pval >>= proj_end_bit;
529
530 pval <<= box_end_bit;
531 ((u8 *)pbuf)[box_addr] &= ~box_mask;
532 ((u8 *)pbuf)[box_addr] |= pval;
533 }
534 }
535}
536
537static u32 crc32_add(u32 crc, u8 byte)
538{
539 u32 byte32 = bitrev32(byte);
540 int i;
541
542 for (i = 0; i < 8; i++) {
543 if ((crc ^ byte32) & BIT(31)) {
544 crc <<= 1;
545 crc ^= ETHER_CRC32_POLY;
546 } else {
547 crc <<= 1;
548 }
549 byte32 <<= 1;
550 }
551 return crc;
552}
553
554/* Little-endian Ethernet CRC32 of data packed as big-endian u32 words */
555static uint32_t sja1105_crc32(void *buf, size_t len)
556{
557 unsigned int i;
558 u64 chunk;
559 u32 crc;
560
561 /* seed */
562 crc = 0xFFFFFFFF;
563 for (i = 0; i < len; i += 4) {
564 sja1105_packing(buf + i, &chunk, 31, 0, 4, UNPACK);
565 crc = crc32_add(crc, chunk & 0xFF);
566 crc = crc32_add(crc, (chunk >> 8) & 0xFF);
567 crc = crc32_add(crc, (chunk >> 16) & 0xFF);
568 crc = crc32_add(crc, (chunk >> 24) & 0xFF);
569 }
570 return bitrev32(~crc);
571}
572
573static void sja1105_spi_message_pack(void *buf, struct sja1105_spi_message *msg)
574{
575 const int size = SJA1105_SIZE_SPI_MSG_HEADER;
576
577 memset(buf, 0, size);
578
579 sja1105_packing(buf, &msg->access, 31, 31, size, PACK);
580 sja1105_packing(buf, &msg->read_count, 30, 25, size, PACK);
581 sja1105_packing(buf, &msg->address, 24, 4, size, PACK);
582}
583
584static int sja1105_xfer_buf(const struct sja1105_private *priv,
585 sja1105_spi_rw_mode_t rw, u64 reg_addr,
586 u8 *buf, size_t len)
587{
588 struct udevice *dev = priv->dev;
589 struct sja1105_chunk chunk = {
590 .len = min_t(size_t, len, SJA1105_SIZE_SPI_MSG_MAXLEN),
591 .reg_addr = reg_addr,
592 .buf = buf,
593 };
594 int num_chunks;
595 int rc, i;
596
597 rc = dm_spi_claim_bus(dev);
598 if (rc)
599 return rc;
600
601 num_chunks = DIV_ROUND_UP(len, SJA1105_SIZE_SPI_MSG_MAXLEN);
602
603 for (i = 0; i < num_chunks; i++) {
604 u8 hdr_buf[SJA1105_SIZE_SPI_MSG_HEADER];
605 struct sja1105_spi_message msg;
606 u8 *rx_buf = NULL;
607 u8 *tx_buf = NULL;
608
609 /* Populate the transfer's header buffer */
610 msg.address = chunk.reg_addr;
611 msg.access = rw;
612 if (rw == SPI_READ)
613 msg.read_count = chunk.len / 4;
614 else
615 /* Ignored */
616 msg.read_count = 0;
617 sja1105_spi_message_pack(hdr_buf, &msg);
618 rc = dm_spi_xfer(dev, SJA1105_SIZE_SPI_MSG_HEADER * 8, hdr_buf,
619 NULL, SPI_XFER_BEGIN);
620 if (rc)
621 goto out;
622
623 /* Populate the transfer's data buffer */
624 if (rw == SPI_READ)
625 rx_buf = chunk.buf;
626 else
627 tx_buf = chunk.buf;
628 rc = dm_spi_xfer(dev, chunk.len * 8, tx_buf, rx_buf,
629 SPI_XFER_END);
630 if (rc)
631 goto out;
632
633 /* Calculate next chunk */
634 chunk.buf += chunk.len;
635 chunk.reg_addr += chunk.len / 4;
636 chunk.len = min_t(size_t, (ptrdiff_t)(buf + len - chunk.buf),
637 SJA1105_SIZE_SPI_MSG_MAXLEN);
638 }
639
640out:
641 dm_spi_release_bus(dev);
642
643 return rc;
644}
645
646static int sja1105et_reset_cmd(struct sja1105_private *priv)
647{
648 const struct sja1105_regs *regs = priv->info->regs;
649 u8 packed_buf[SJA1105_SIZE_RESET_CMD] = {0};
650 const int size = SJA1105_SIZE_RESET_CMD;
651 u64 cold_rst = 1;
652
653 sja1105_packing(packed_buf, &cold_rst, 3, 3, size, PACK);
654
655 return sja1105_xfer_buf(priv, SPI_WRITE, regs->rgu, packed_buf,
656 SJA1105_SIZE_RESET_CMD);
657}
658
659static int sja1105pqrs_reset_cmd(struct sja1105_private *priv)
660{
661 const struct sja1105_regs *regs = priv->info->regs;
662 u8 packed_buf[SJA1105_SIZE_RESET_CMD] = {0};
663 const int size = SJA1105_SIZE_RESET_CMD;
664 u64 cold_rst = 1;
665
666 sja1105_packing(packed_buf, &cold_rst, 2, 2, size, PACK);
667
668 return sja1105_xfer_buf(priv, SPI_WRITE, regs->rgu, packed_buf,
669 SJA1105_SIZE_RESET_CMD);
670}
671
672static int sja1110_reset_cmd(struct sja1105_private *priv)
673{
674 const struct sja1105_regs *regs = priv->info->regs;
675 u8 packed_buf[SJA1105_SIZE_RESET_CMD] = {0};
676 const int size = SJA1105_SIZE_RESET_CMD;
677 u64 switch_rst = 1;
678
679 /* Only reset the switch core.
680 * A full cold reset would re-enable the BASE_MCSS_CLOCK PLL which
681 * would turn on the microcontroller, potentially letting it execute
682 * code which could interfere with our configuration.
683 */
684 sja1105_packing(packed_buf, &switch_rst, 20, 20, size, PACK);
685
686 return sja1105_xfer_buf(priv, SPI_WRITE, regs->rgu, packed_buf,
687 SJA1105_SIZE_RESET_CMD);
688}
689
690static size_t sja1105et_general_params_entry_packing(void *buf, void *entry_ptr,
691 enum packing_op op)
692{
693 const size_t size = SJA1105ET_SIZE_GENERAL_PARAMS_ENTRY;
694 struct sja1105_general_params_entry *entry = entry_ptr;
695
696 sja1105_packing(buf, &entry->mac_fltres1, 311, 264, size, op);
697 sja1105_packing(buf, &entry->mac_fltres0, 263, 216, size, op);
698 sja1105_packing(buf, &entry->mac_flt1, 215, 168, size, op);
699 sja1105_packing(buf, &entry->mac_flt0, 167, 120, size, op);
700 sja1105_packing(buf, &entry->casc_port, 115, 113, size, op);
701 sja1105_packing(buf, &entry->host_port, 112, 110, size, op);
702 sja1105_packing(buf, &entry->mirr_port, 109, 107, size, op);
703 sja1105_packing(buf, &entry->tpid, 42, 27, size, op);
704 sja1105_packing(buf, &entry->tpid2, 25, 10, size, op);
705 return size;
706}
707
708static size_t sja1110_general_params_entry_packing(void *buf, void *entry_ptr,
709 enum packing_op op)
710{
711 struct sja1105_general_params_entry *entry = entry_ptr;
712 const size_t size = SJA1110_SIZE_GENERAL_PARAMS_ENTRY;
713
714 sja1105_packing(buf, &entry->mac_fltres1, 438, 391, size, op);
715 sja1105_packing(buf, &entry->mac_fltres0, 390, 343, size, op);
716 sja1105_packing(buf, &entry->mac_flt1, 342, 295, size, op);
717 sja1105_packing(buf, &entry->mac_flt0, 294, 247, size, op);
718 sja1105_packing(buf, &entry->casc_port, 242, 232, size, op);
719 sja1105_packing(buf, &entry->host_port, 231, 228, size, op);
720 sja1105_packing(buf, &entry->mirr_port, 227, 224, size, op);
721 sja1105_packing(buf, &entry->tpid2, 159, 144, size, op);
722 sja1105_packing(buf, &entry->tpid, 142, 127, size, op);
723 return size;
724}
725
726static size_t
727sja1105pqrs_general_params_entry_packing(void *buf, void *entry_ptr,
728 enum packing_op op)
729{
730 const size_t size = SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY;
731 struct sja1105_general_params_entry *entry = entry_ptr;
732
733 sja1105_packing(buf, &entry->mac_fltres1, 343, 296, size, op);
734 sja1105_packing(buf, &entry->mac_fltres0, 295, 248, size, op);
735 sja1105_packing(buf, &entry->mac_flt1, 247, 200, size, op);
736 sja1105_packing(buf, &entry->mac_flt0, 199, 152, size, op);
737 sja1105_packing(buf, &entry->casc_port, 147, 145, size, op);
738 sja1105_packing(buf, &entry->host_port, 144, 142, size, op);
739 sja1105_packing(buf, &entry->mirr_port, 141, 139, size, op);
740 sja1105_packing(buf, &entry->tpid, 74, 59, size, op);
741 sja1105_packing(buf, &entry->tpid2, 57, 42, size, op);
742 return size;
743}
744
745static size_t
746sja1105_l2_forwarding_params_entry_packing(void *buf, void *entry_ptr,
747 enum packing_op op)
748{
749 const size_t size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY;
750 struct sja1105_l2_forwarding_params_entry *entry = entry_ptr;
751 int offset, i;
752
753 for (i = 0, offset = 13; i < SJA1105_NUM_TC; i++, offset += 10)
754 sja1105_packing(buf, &entry->part_spc[i],
755 offset + 9, offset + 0, size, op);
756 return size;
757}
758
759static size_t
760sja1110_l2_forwarding_params_entry_packing(void *buf, void *entry_ptr,
761 enum packing_op op)
762{
763 struct sja1105_l2_forwarding_params_entry *entry = entry_ptr;
764 const size_t size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY;
765 int offset, i;
766
767 for (i = 0, offset = 5; i < 8; i++, offset += 11)
768 sja1105_packing(buf, &entry->part_spc[i],
769 offset + 10, offset + 0, size, op);
770 return size;
771}
772
773static size_t sja1105_l2_forwarding_entry_packing(void *buf, void *entry_ptr,
774 enum packing_op op)
775{
776 const size_t size = SJA1105_SIZE_L2_FORWARDING_ENTRY;
777 struct sja1105_l2_forwarding_entry *entry = entry_ptr;
778
779 sja1105_packing(buf, &entry->bc_domain, 63, 59, size, op);
780 sja1105_packing(buf, &entry->reach_port, 58, 54, size, op);
781 sja1105_packing(buf, &entry->fl_domain, 53, 49, size, op);
782 return size;
783}
784
785static size_t sja1110_l2_forwarding_entry_packing(void *buf, void *entry_ptr,
786 enum packing_op op)
787{
788 struct sja1105_l2_forwarding_entry *entry = entry_ptr;
789 const size_t size = SJA1105_SIZE_L2_FORWARDING_ENTRY;
790
791 sja1105_packing(buf, &entry->bc_domain, 63, 53, size, op);
792 sja1105_packing(buf, &entry->reach_port, 52, 42, size, op);
793 sja1105_packing(buf, &entry->fl_domain, 41, 31, size, op);
794 return size;
795}
796
797static size_t sja1105_l2_policing_entry_packing(void *buf, void *entry_ptr,
798 enum packing_op op)
799{
800 struct sja1105_l2_policing_entry *entry = entry_ptr;
801 const size_t size = SJA1105_SIZE_L2_POLICING_ENTRY;
802
803 sja1105_packing(buf, &entry->sharindx, 63, 58, size, op);
804 sja1105_packing(buf, &entry->smax, 57, 42, size, op);
805 sja1105_packing(buf, &entry->rate, 41, 26, size, op);
806 sja1105_packing(buf, &entry->maxlen, 25, 15, size, op);
807 sja1105_packing(buf, &entry->partition, 14, 12, size, op);
808 return size;
809}
810
811static size_t sja1110_l2_policing_entry_packing(void *buf, void *entry_ptr,
812 enum packing_op op)
813{
814 struct sja1105_l2_policing_entry *entry = entry_ptr;
815 const size_t size = SJA1105_SIZE_L2_POLICING_ENTRY;
816
817 sja1105_packing(buf, &entry->sharindx, 63, 57, size, op);
818 sja1105_packing(buf, &entry->smax, 56, 39, size, op);
819 sja1105_packing(buf, &entry->rate, 38, 21, size, op);
820 sja1105_packing(buf, &entry->maxlen, 20, 10, size, op);
821 sja1105_packing(buf, &entry->partition, 9, 7, size, op);
822 return size;
823}
824
825static size_t sja1105et_mac_config_entry_packing(void *buf, void *entry_ptr,
826 enum packing_op op)
827{
828 const size_t size = SJA1105ET_SIZE_MAC_CONFIG_ENTRY;
829 struct sja1105_mac_config_entry *entry = entry_ptr;
830 int offset, i;
831
832 for (i = 0, offset = 72; i < SJA1105_NUM_TC; i++, offset += 19) {
833 sja1105_packing(buf, &entry->enabled[i],
834 offset + 0, offset + 0, size, op);
835 sja1105_packing(buf, &entry->base[i],
836 offset + 9, offset + 1, size, op);
837 sja1105_packing(buf, &entry->top[i],
838 offset + 18, offset + 10, size, op);
839 }
840 sja1105_packing(buf, &entry->speed, 66, 65, size, op);
841 sja1105_packing(buf, &entry->vlanid, 21, 10, size, op);
842 sja1105_packing(buf, &entry->egress, 2, 2, size, op);
843 sja1105_packing(buf, &entry->ingress, 1, 1, size, op);
844 return size;
845}
846
847static size_t sja1105pqrs_mac_config_entry_packing(void *buf, void *entry_ptr,
848 enum packing_op op)
849{
850 const size_t size = SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY;
851 struct sja1105_mac_config_entry *entry = entry_ptr;
852 int offset, i;
853
854 for (i = 0, offset = 104; i < SJA1105_NUM_TC; i++, offset += 19) {
855 sja1105_packing(buf, &entry->enabled[i],
856 offset + 0, offset + 0, size, op);
857 sja1105_packing(buf, &entry->base[i],
858 offset + 9, offset + 1, size, op);
859 sja1105_packing(buf, &entry->top[i],
860 offset + 18, offset + 10, size, op);
861 }
862 sja1105_packing(buf, &entry->speed, 98, 97, size, op);
863 sja1105_packing(buf, &entry->vlanid, 53, 42, size, op);
864 sja1105_packing(buf, &entry->egress, 32, 32, size, op);
865 sja1105_packing(buf, &entry->ingress, 31, 31, size, op);
866 return size;
867}
868
869static size_t sja1110_mac_config_entry_packing(void *buf, void *entry_ptr,
870 enum packing_op op)
871{
872 const size_t size = SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY;
873 struct sja1105_mac_config_entry *entry = entry_ptr;
874 int offset, i;
875
876 for (i = 0, offset = 104; i < 8; i++, offset += 19) {
877 sja1105_packing(buf, &entry->enabled[i],
878 offset + 0, offset + 0, size, op);
879 sja1105_packing(buf, &entry->base[i],
880 offset + 9, offset + 1, size, op);
881 sja1105_packing(buf, &entry->top[i],
882 offset + 18, offset + 10, size, op);
883 }
884 sja1105_packing(buf, &entry->speed, 98, 96, size, op);
885 sja1105_packing(buf, &entry->vlanid, 52, 41, size, op);
886 sja1105_packing(buf, &entry->egress, 31, 31, size, op);
887 sja1105_packing(buf, &entry->ingress, 30, 30, size, op);
888 return size;
889}
890
891static size_t sja1105_vlan_lookup_entry_packing(void *buf, void *entry_ptr,
892 enum packing_op op)
893{
894 const size_t size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY;
895 struct sja1105_vlan_lookup_entry *entry = entry_ptr;
896
897 sja1105_packing(buf, &entry->vmemb_port, 53, 49, size, op);
898 sja1105_packing(buf, &entry->vlan_bc, 48, 44, size, op);
899 sja1105_packing(buf, &entry->tag_port, 43, 39, size, op);
900 sja1105_packing(buf, &entry->vlanid, 38, 27, size, op);
901 return size;
902}
903
904static size_t sja1110_vlan_lookup_entry_packing(void *buf, void *entry_ptr,
905 enum packing_op op)
906{
907 struct sja1105_vlan_lookup_entry *entry = entry_ptr;
908 const size_t size = SJA1110_SIZE_VLAN_LOOKUP_ENTRY;
909
910 sja1105_packing(buf, &entry->vmemb_port, 73, 63, size, op);
911 sja1105_packing(buf, &entry->vlan_bc, 62, 52, size, op);
912 sja1105_packing(buf, &entry->tag_port, 51, 41, size, op);
913 sja1105_packing(buf, &entry->type_entry, 40, 39, size, op);
914 sja1105_packing(buf, &entry->vlanid, 38, 27, size, op);
915 return size;
916}
917
918static size_t sja1105_xmii_params_entry_packing(void *buf, void *entry_ptr,
919 enum packing_op op)
920{
921 const size_t size = SJA1105_SIZE_XMII_PARAMS_ENTRY;
922 struct sja1105_xmii_params_entry *entry = entry_ptr;
923 int offset, i;
924
925 for (i = 0, offset = 17; i < SJA1105_NUM_PORTS; i++, offset += 3) {
926 sja1105_packing(buf, &entry->xmii_mode[i],
927 offset + 1, offset + 0, size, op);
928 sja1105_packing(buf, &entry->phy_mac[i],
929 offset + 2, offset + 2, size, op);
930 }
931 return size;
932}
933
934static size_t sja1110_xmii_params_entry_packing(void *buf, void *entry_ptr,
935 enum packing_op op)
936{
937 const size_t size = SJA1110_SIZE_XMII_PARAMS_ENTRY;
938 struct sja1105_xmii_params_entry *entry = entry_ptr;
939 int offset, i;
940
941 for (i = 0, offset = 20; i < SJA1110_NUM_PORTS; i++, offset += 4) {
942 sja1105_packing(buf, &entry->xmii_mode[i],
943 offset + 1, offset + 0, size, op);
944 sja1105_packing(buf, &entry->phy_mac[i],
945 offset + 2, offset + 2, size, op);
946 sja1105_packing(buf, &entry->special[i],
947 offset + 3, offset + 3, size, op);
948 }
949 return size;
950}
951
952static size_t sja1105_table_header_packing(void *buf, void *entry_ptr,
953 enum packing_op op)
954{
955 const size_t size = SJA1105_SIZE_TABLE_HEADER;
956 struct sja1105_table_header *entry = entry_ptr;
957
958 sja1105_packing(buf, &entry->block_id, 31, 24, size, op);
959 sja1105_packing(buf, &entry->len, 55, 32, size, op);
960 sja1105_packing(buf, &entry->crc, 95, 64, size, op);
961 return size;
962}
963
964static void
965sja1105_table_header_pack_with_crc(void *buf, struct sja1105_table_header *hdr)
966{
967 /* First pack the table as-is, then calculate the CRC, and
968 * finally put the proper CRC into the packed buffer
969 */
970 memset(buf, 0, SJA1105_SIZE_TABLE_HEADER);
971 sja1105_table_header_packing(buf, hdr, PACK);
972 hdr->crc = sja1105_crc32(buf, SJA1105_SIZE_TABLE_HEADER - 4);
973 sja1105_packing(buf + SJA1105_SIZE_TABLE_HEADER - 4, &hdr->crc,
974 31, 0, 4, PACK);
975}
976
977static void sja1105_table_write_crc(u8 *table_start, u8 *crc_ptr)
978{
979 u64 computed_crc;
980 int len_bytes;
981
982 len_bytes = (uintptr_t)(crc_ptr - table_start);
983 computed_crc = sja1105_crc32(table_start, len_bytes);
984 sja1105_packing(crc_ptr, &computed_crc, 31, 0, 4, PACK);
985}
986
987/* The block IDs that the switches support are unfortunately sparse, so keep a
988 * mapping table to "block indices" and translate back and forth.
989 */
990static const u64 blk_id_map[BLK_IDX_MAX] = {
991 [BLK_IDX_L2_POLICING] = BLKID_L2_POLICING,
992 [BLK_IDX_VLAN_LOOKUP] = BLKID_VLAN_LOOKUP,
993 [BLK_IDX_L2_FORWARDING] = BLKID_L2_FORWARDING,
994 [BLK_IDX_MAC_CONFIG] = BLKID_MAC_CONFIG,
995 [BLK_IDX_L2_FORWARDING_PARAMS] = BLKID_L2_FORWARDING_PARAMS,
996 [BLK_IDX_GENERAL_PARAMS] = BLKID_GENERAL_PARAMS,
997 [BLK_IDX_XMII_PARAMS] = BLKID_XMII_PARAMS,
998};
999
1000static void
1001sja1105_static_config_pack(void *buf, struct sja1105_static_config *config)
1002{
1003 struct sja1105_table_header header = {0};
1004 enum sja1105_blk_idx i;
1005 u8 *p = buf;
1006 int j;
1007
1008 sja1105_packing(p, &config->device_id, 31, 0, 4, PACK);
1009 p += SJA1105_SIZE_DEVICE_ID;
1010
1011 for (i = 0; i < BLK_IDX_MAX; i++) {
1012 const struct sja1105_table *table;
1013 u8 *table_start;
1014
1015 table = &config->tables[i];
1016 if (!table->entry_count)
1017 continue;
1018
1019 header.block_id = blk_id_map[i];
1020 header.len = table->entry_count *
1021 table->ops->packed_entry_size / 4;
1022 sja1105_table_header_pack_with_crc(p, &header);
1023 p += SJA1105_SIZE_TABLE_HEADER;
1024 table_start = p;
1025 for (j = 0; j < table->entry_count; j++) {
1026 u8 *entry_ptr = table->entries;
1027
1028 entry_ptr += j * table->ops->unpacked_entry_size;
1029 memset(p, 0, table->ops->packed_entry_size);
1030 table->ops->packing(p, entry_ptr, PACK);
1031 p += table->ops->packed_entry_size;
1032 }
1033 sja1105_table_write_crc(table_start, p);
1034 p += 4;
1035 }
1036 /* Final header:
1037 * Block ID does not matter
1038 * Length of 0 marks that header is final
1039 * CRC will be replaced on-the-fly
1040 */
1041 header.block_id = 0;
1042 header.len = 0;
1043 header.crc = 0xDEADBEEF;
1044 memset(p, 0, SJA1105_SIZE_TABLE_HEADER);
1045 sja1105_table_header_packing(p, &header, PACK);
1046}
1047
1048static size_t
1049sja1105_static_config_get_length(const struct sja1105_static_config *config)
1050{
1051 unsigned int header_count;
1052 enum sja1105_blk_idx i;
1053 unsigned int sum;
1054
1055 /* Ending header */
1056 header_count = 1;
1057 sum = SJA1105_SIZE_DEVICE_ID;
1058
1059 /* Tables (headers and entries) */
1060 for (i = 0; i < BLK_IDX_MAX; i++) {
1061 const struct sja1105_table *table;
1062
1063 table = &config->tables[i];
1064 if (table->entry_count)
1065 header_count++;
1066
1067 sum += table->ops->packed_entry_size * table->entry_count;
1068 }
1069 /* Headers have an additional CRC at the end */
1070 sum += header_count * (SJA1105_SIZE_TABLE_HEADER + 4);
1071 /* Last header does not have an extra CRC because there is no data */
1072 sum -= 4;
1073
1074 return sum;
1075}
1076
1077/* Compatibility matrices */
1078static const struct sja1105_table_ops sja1105et_table_ops[BLK_IDX_MAX] = {
1079 [BLK_IDX_L2_POLICING] = {
1080 .packing = sja1105_l2_policing_entry_packing,
1081 .unpacked_entry_size = sizeof(struct sja1105_l2_policing_entry),
1082 .packed_entry_size = SJA1105_SIZE_L2_POLICING_ENTRY,
1083 .max_entry_count = SJA1105_MAX_L2_POLICING_COUNT,
1084 },
1085 [BLK_IDX_VLAN_LOOKUP] = {
1086 .packing = sja1105_vlan_lookup_entry_packing,
1087 .unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
1088 .packed_entry_size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY,
1089 .max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
1090 },
1091 [BLK_IDX_L2_FORWARDING] = {
1092 .packing = sja1105_l2_forwarding_entry_packing,
1093 .unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_entry),
1094 .packed_entry_size = SJA1105_SIZE_L2_FORWARDING_ENTRY,
1095 .max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
1096 },
1097 [BLK_IDX_MAC_CONFIG] = {
1098 .packing = sja1105et_mac_config_entry_packing,
1099 .unpacked_entry_size = sizeof(struct sja1105_mac_config_entry),
1100 .packed_entry_size = SJA1105ET_SIZE_MAC_CONFIG_ENTRY,
1101 .max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
1102 },
1103 [BLK_IDX_L2_FORWARDING_PARAMS] = {
1104 .packing = sja1105_l2_forwarding_params_entry_packing,
1105 .unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry),
1106 .packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY,
1107 .max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
1108 },
1109 [BLK_IDX_GENERAL_PARAMS] = {
1110 .packing = sja1105et_general_params_entry_packing,
1111 .unpacked_entry_size = sizeof(struct sja1105_general_params_entry),
1112 .packed_entry_size = SJA1105ET_SIZE_GENERAL_PARAMS_ENTRY,
1113 .max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
1114 },
1115 [BLK_IDX_XMII_PARAMS] = {
1116 .packing = sja1105_xmii_params_entry_packing,
1117 .unpacked_entry_size = sizeof(struct sja1105_xmii_params_entry),
1118 .packed_entry_size = SJA1105_SIZE_XMII_PARAMS_ENTRY,
1119 .max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
1120 },
1121};
1122
1123static const struct sja1105_table_ops sja1105pqrs_table_ops[BLK_IDX_MAX] = {
1124 [BLK_IDX_L2_POLICING] = {
1125 .packing = sja1105_l2_policing_entry_packing,
1126 .unpacked_entry_size = sizeof(struct sja1105_l2_policing_entry),
1127 .packed_entry_size = SJA1105_SIZE_L2_POLICING_ENTRY,
1128 .max_entry_count = SJA1105_MAX_L2_POLICING_COUNT,
1129 },
1130 [BLK_IDX_VLAN_LOOKUP] = {
1131 .packing = sja1105_vlan_lookup_entry_packing,
1132 .unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
1133 .packed_entry_size = SJA1105_SIZE_VLAN_LOOKUP_ENTRY,
1134 .max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
1135 },
1136 [BLK_IDX_L2_FORWARDING] = {
1137 .packing = sja1105_l2_forwarding_entry_packing,
1138 .unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_entry),
1139 .packed_entry_size = SJA1105_SIZE_L2_FORWARDING_ENTRY,
1140 .max_entry_count = SJA1105_MAX_L2_FORWARDING_COUNT,
1141 },
1142 [BLK_IDX_MAC_CONFIG] = {
1143 .packing = sja1105pqrs_mac_config_entry_packing,
1144 .unpacked_entry_size = sizeof(struct sja1105_mac_config_entry),
1145 .packed_entry_size = SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY,
1146 .max_entry_count = SJA1105_MAX_MAC_CONFIG_COUNT,
1147 },
1148 [BLK_IDX_L2_FORWARDING_PARAMS] = {
1149 .packing = sja1105_l2_forwarding_params_entry_packing,
1150 .unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry),
1151 .packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY,
1152 .max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
1153 },
1154 [BLK_IDX_GENERAL_PARAMS] = {
1155 .packing = sja1105pqrs_general_params_entry_packing,
1156 .unpacked_entry_size = sizeof(struct sja1105_general_params_entry),
1157 .packed_entry_size = SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY,
1158 .max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
1159 },
1160 [BLK_IDX_XMII_PARAMS] = {
1161 .packing = sja1105_xmii_params_entry_packing,
1162 .unpacked_entry_size = sizeof(struct sja1105_xmii_params_entry),
1163 .packed_entry_size = SJA1105_SIZE_XMII_PARAMS_ENTRY,
1164 .max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
1165 },
1166};
1167
1168static const struct sja1105_table_ops sja1110_table_ops[BLK_IDX_MAX] = {
1169 [BLK_IDX_L2_POLICING] = {
1170 .packing = sja1110_l2_policing_entry_packing,
1171 .unpacked_entry_size = sizeof(struct sja1105_l2_policing_entry),
1172 .packed_entry_size = SJA1105_SIZE_L2_POLICING_ENTRY,
1173 .max_entry_count = SJA1110_MAX_L2_POLICING_COUNT,
1174 },
1175 [BLK_IDX_VLAN_LOOKUP] = {
1176 .packing = sja1110_vlan_lookup_entry_packing,
1177 .unpacked_entry_size = sizeof(struct sja1105_vlan_lookup_entry),
1178 .packed_entry_size = SJA1110_SIZE_VLAN_LOOKUP_ENTRY,
1179 .max_entry_count = SJA1105_MAX_VLAN_LOOKUP_COUNT,
1180 },
1181 [BLK_IDX_L2_FORWARDING] = {
1182 .packing = sja1110_l2_forwarding_entry_packing,
1183 .unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_entry),
1184 .packed_entry_size = SJA1105_SIZE_L2_FORWARDING_ENTRY,
1185 .max_entry_count = SJA1110_MAX_L2_FORWARDING_COUNT,
1186 },
1187 [BLK_IDX_MAC_CONFIG] = {
1188 .packing = sja1110_mac_config_entry_packing,
1189 .unpacked_entry_size = sizeof(struct sja1105_mac_config_entry),
1190 .packed_entry_size = SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY,
1191 .max_entry_count = SJA1110_MAX_MAC_CONFIG_COUNT,
1192 },
1193 [BLK_IDX_L2_FORWARDING_PARAMS] = {
1194 .packing = sja1110_l2_forwarding_params_entry_packing,
1195 .unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry),
1196 .packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY,
1197 .max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
1198 },
1199 [BLK_IDX_GENERAL_PARAMS] = {
1200 .packing = sja1110_general_params_entry_packing,
1201 .unpacked_entry_size = sizeof(struct sja1105_general_params_entry),
1202 .packed_entry_size = SJA1110_SIZE_GENERAL_PARAMS_ENTRY,
1203 .max_entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT,
1204 },
1205 [BLK_IDX_XMII_PARAMS] = {
1206 .packing = sja1110_xmii_params_entry_packing,
1207 .unpacked_entry_size = sizeof(struct sja1105_xmii_params_entry),
1208 .packed_entry_size = SJA1110_SIZE_XMII_PARAMS_ENTRY,
1209 .max_entry_count = SJA1105_MAX_XMII_PARAMS_COUNT,
1210 },
1211};
1212
1213static int sja1105_init_mii_settings(struct sja1105_private *priv)
1214{
1215 struct sja1105_table *table;
1216
1217 table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
1218
1219 table->entries = calloc(SJA1105_MAX_XMII_PARAMS_COUNT,
1220 table->ops->unpacked_entry_size);
1221 if (!table->entries)
1222 return -ENOMEM;
1223
1224 /* Table will be populated at runtime */
1225 table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT;
1226
1227 return 0;
1228}
1229
1230static void sja1105_setup_tagging(struct sja1105_private *priv, int port)
1231{
1232 struct dsa_pdata *pdata = dev_get_uclass_plat(priv->dev);
1233 struct sja1105_vlan_lookup_entry *vlan;
1234 int cpu = pdata->cpu_port;
1235
1236 /* The CPU port is implicitly configured by
1237 * configuring the front-panel ports
1238 */
1239 if (port == cpu)
1240 return;
1241
1242 vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
1243
1244 priv->pvid[port] = DSA_8021Q_DIR_TX | DSA_8021Q_PORT(port);
1245
1246 vlan[port].vmemb_port = BIT(port) | BIT(cpu);
1247 vlan[port].vlan_bc = BIT(port) | BIT(cpu);
1248 vlan[port].tag_port = BIT(cpu);
1249 vlan[port].vlanid = priv->pvid[port];
1250 vlan[port].type_entry = SJA1110_VLAN_D_TAG;
1251}
1252
1253static int sja1105_init_vlan(struct sja1105_private *priv)
1254{
1255 struct dsa_pdata *pdata = dev_get_uclass_plat(priv->dev);
1256 struct sja1105_table *table;
1257 int port;
1258
1259 table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
1260
1261 table->entries = calloc(pdata->num_ports,
1262 table->ops->unpacked_entry_size);
1263 if (!table->entries)
1264 return -ENOMEM;
1265
1266 table->entry_count = pdata->num_ports;
1267
1268 for (port = 0; port < pdata->num_ports; port++)
1269 sja1105_setup_tagging(priv, port);
1270
1271 return 0;
1272}
1273
1274static void
1275sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
1276 int from, int to)
1277{
1278 l2_fwd[from].bc_domain |= BIT(to);
1279 l2_fwd[from].reach_port |= BIT(to);
1280 l2_fwd[from].fl_domain |= BIT(to);
1281}
1282
1283static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
1284{
1285 struct dsa_pdata *pdata = dev_get_uclass_plat(priv->dev);
1286 struct sja1105_l2_forwarding_entry *l2fwd;
1287 struct sja1105_table *table;
1288 int cpu = pdata->cpu_port;
1289 int i;
1290
1291 table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
1292
1293 table->entries = calloc(SJA1105_MAX_L2_FORWARDING_COUNT,
1294 table->ops->unpacked_entry_size);
1295 if (!table->entries)
1296 return -ENOMEM;
1297
1298 table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT;
1299
1300 l2fwd = table->entries;
1301
1302 /* First 5 entries define the forwarding rules */
1303 for (i = 0; i < pdata->num_ports; i++) {
1304 if (i == cpu)
1305 continue;
1306
1307 sja1105_port_allow_traffic(l2fwd, i, cpu);
1308 sja1105_port_allow_traffic(l2fwd, cpu, i);
1309 }
1310 /* Next 8 entries define VLAN PCP mapping from ingress to egress.
1311 * Leave them unpopulated (implicitly 0) but present.
1312 */
1313 return 0;
1314}
1315
1316static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
1317{
1318 struct sja1105_l2_forwarding_params_entry default_l2fwd_params = {
1319 /* Use a single memory partition for all ingress queues */
1320 .part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 },
1321 };
1322 struct sja1105_table *table;
1323
1324 table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
1325
1326 table->entries = calloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
1327 table->ops->unpacked_entry_size);
1328 if (!table->entries)
1329 return -ENOMEM;
1330
1331 table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT;
1332
1333 /* This table only has a single entry */
1334 ((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] =
1335 default_l2fwd_params;
1336
1337 return 0;
1338}
1339
1340static int sja1105_init_general_params(struct sja1105_private *priv)
1341{
1342 struct dsa_pdata *pdata = dev_get_uclass_plat(priv->dev);
1343 struct sja1105_general_params_entry default_general_params = {
1344 /* No frame trapping */
1345 .mac_fltres1 = 0x0,
1346 .mac_flt1 = 0xffffffffffff,
1347 .mac_fltres0 = 0x0,
1348 .mac_flt0 = 0xffffffffffff,
1349 .host_port = pdata->num_ports,
1350 /* No mirroring => specify an out-of-range port value */
1351 .mirr_port = pdata->num_ports,
1352 /* No link-local trapping => specify an out-of-range port value
1353 */
1354 .casc_port = pdata->num_ports,
1355 /* Force the switch to see all traffic as untagged. */
1356 .tpid = ETH_P_SJA1105,
1357 .tpid2 = ETH_P_SJA1105,
1358 };
1359 struct sja1105_table *table;
1360
1361 table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
1362
1363 table->entries = calloc(SJA1105_MAX_GENERAL_PARAMS_COUNT,
1364 table->ops->unpacked_entry_size);
1365 if (!table->entries)
1366 return -ENOMEM;
1367
1368 table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT;
1369
1370 /* This table only has a single entry */
1371 ((struct sja1105_general_params_entry *)table->entries)[0] =
1372 default_general_params;
1373
1374 return 0;
1375}
1376
1377static void sja1105_setup_policer(struct sja1105_l2_policing_entry *policing,
1378 int index, int mtu)
1379{
1380 policing[index].sharindx = index;
1381 policing[index].smax = 65535; /* Burst size in bytes */
1382 policing[index].rate = SJA1105_RATE_MBPS(1000);
1383 policing[index].maxlen = mtu;
1384 policing[index].partition = 0;
1385}
1386
1387static int sja1105_init_l2_policing(struct sja1105_private *priv)
1388{
1389 struct dsa_pdata *pdata = dev_get_uclass_plat(priv->dev);
1390 struct sja1105_l2_policing_entry *policing;
1391 struct sja1105_table *table;
1392 int cpu = pdata->cpu_port;
1393 int i, j, k;
1394
1395 table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
1396
1397 table->entries = calloc(SJA1105_MAX_L2_POLICING_COUNT,
1398 table->ops->unpacked_entry_size);
1399 if (!table->entries)
1400 return -ENOMEM;
1401
1402 table->entry_count = SJA1105_MAX_L2_POLICING_COUNT;
1403
1404 policing = table->entries;
1405
1406 /* k sweeps through all unicast policers (0-39).
1407 * bcast sweeps through policers 40-44.
1408 */
1409 for (i = 0, k = 0; i < pdata->num_ports; i++) {
1410 int bcast = (pdata->num_ports * SJA1105_NUM_TC) + i;
1411 int mtu = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
1412
1413 if (i == cpu)
1414 mtu += VLAN_HLEN;
1415
1416 for (j = 0; j < SJA1105_NUM_TC; j++, k++)
1417 sja1105_setup_policer(policing, k, mtu);
1418
1419 /* Set up this port's policer for broadcast traffic */
1420 sja1105_setup_policer(policing, bcast, mtu);
1421 }
1422 return 0;
1423}
1424
1425static int sja1105_init_mac_settings(struct sja1105_private *priv)
1426{
1427 struct sja1105_mac_config_entry default_mac = {
1428 /* Enable 1 priority queue on egress. */
1429 .top = {0x1FF, 0, 0, 0, 0, 0, 0},
1430 .base = {0x0, 0, 0, 0, 0, 0, 0, 0},
1431 .enabled = {1, 0, 0, 0, 0, 0, 0, 0},
1432 /* Will be overridden in sja1105_port_enable. */
1433 .speed = priv->info->port_speed[SJA1105_SPEED_AUTO],
1434 .egress = true,
1435 .ingress = true,
1436 };
1437 struct dsa_pdata *pdata = dev_get_uclass_plat(priv->dev);
1438 struct sja1105_mac_config_entry *mac;
1439 struct sja1105_table *table;
1440 int port;
1441
1442 table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
1443
1444 table->entries = calloc(pdata->num_ports,
1445 table->ops->unpacked_entry_size);
1446 if (!table->entries)
1447 return -ENOMEM;
1448
1449 table->entry_count = pdata->num_ports;
1450
1451 mac = table->entries;
1452
1453 for (port = 0; port < pdata->num_ports; port++) {
1454 mac[port] = default_mac;
1455 /* Internal VLAN (pvid) to apply to untagged ingress */
1456 mac[port].vlanid = priv->pvid[port];
1457 }
1458
1459 return 0;
1460}
1461
1462static int sja1105_static_config_init(struct sja1105_private *priv)
1463{
1464 struct sja1105_static_config *config = &priv->static_config;
1465 const struct sja1105_table_ops *static_ops = priv->info->static_ops;
1466 u64 device_id = priv->info->device_id;
1467 enum sja1105_blk_idx i;
1468 int rc;
1469
1470 *config = (struct sja1105_static_config) {0};
1471
1472 /* Transfer static_ops array from priv into per-table ops
1473 * for handier access
1474 */
1475 for (i = 0; i < BLK_IDX_MAX; i++)
1476 config->tables[i].ops = &static_ops[i];
1477
1478 config->device_id = device_id;
1479
1480 /* Build initial static configuration, to be fixed up during runtime */
1481 rc = sja1105_init_vlan(priv);
1482 if (rc < 0)
1483 return rc;
1484 rc = sja1105_init_mac_settings(priv);
1485 if (rc < 0)
1486 return rc;
1487 rc = sja1105_init_mii_settings(priv);
1488 if (rc < 0)
1489 return rc;
1490 rc = sja1105_init_l2_forwarding(priv);
1491 if (rc < 0)
1492 return rc;
1493 rc = sja1105_init_l2_forwarding_params(priv);
1494 if (rc < 0)
1495 return rc;
1496 rc = sja1105_init_l2_policing(priv);
1497 if (rc < 0)
1498 return rc;
1499 rc = sja1105_init_general_params(priv);
1500 if (rc < 0)
1501 return rc;
1502
1503 return 0;
1504}
1505
1506static void sja1105_static_config_free(struct sja1105_static_config *config)
1507{
1508 enum sja1105_blk_idx i;
1509
1510 for (i = 0; i < BLK_IDX_MAX; i++) {
1511 if (config->tables[i].entry_count) {
1512 free(config->tables[i].entries);
1513 config->tables[i].entry_count = 0;
1514 }
1515 }
1516}
1517
1518static void sja1105_cgu_idiv_packing(void *buf, struct sja1105_cgu_idiv *idiv,
1519 enum packing_op op)
1520{
1521 const int size = 4;
1522
1523 sja1105_packing(buf, &idiv->clksrc, 28, 24, size, op);
1524 sja1105_packing(buf, &idiv->autoblock, 11, 11, size, op);
1525 sja1105_packing(buf, &idiv->idiv, 5, 2, size, op);
1526 sja1105_packing(buf, &idiv->pd, 0, 0, size, op);
1527}
1528
1529static int sja1105_cgu_idiv_config(struct sja1105_private *priv, int port,
1530 bool enabled, int factor)
1531{
1532 const struct sja1105_regs *regs = priv->info->regs;
1533 u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
1534 struct sja1105_cgu_idiv idiv;
1535
1536 if (regs->cgu_idiv[port] == SJA1105_RSV_ADDR)
1537 return 0;
1538
1539 if (enabled && factor != 1 && factor != 10)
1540 return -ERANGE;
1541
1542 /* Payload for packed_buf */
1543 idiv.clksrc = 0x0A; /* 25MHz */
1544 idiv.autoblock = 1; /* Block clk automatically */
1545 idiv.idiv = factor - 1; /* Divide by 1 or 10 */
1546 idiv.pd = enabled ? 0 : 1; /* Power down? */
1547 sja1105_cgu_idiv_packing(packed_buf, &idiv, PACK);
1548
1549 return sja1105_xfer_buf(priv, SPI_WRITE, regs->cgu_idiv[port],
1550 packed_buf, SJA1105_SIZE_CGU_CMD);
1551}
1552
1553static void
1554sja1105_cgu_mii_control_packing(void *buf, struct sja1105_cgu_mii_ctrl *cmd,
1555 enum packing_op op)
1556{
1557 const int size = 4;
1558
1559 sja1105_packing(buf, &cmd->clksrc, 28, 24, size, op);
1560 sja1105_packing(buf, &cmd->autoblock, 11, 11, size, op);
1561 sja1105_packing(buf, &cmd->pd, 0, 0, size, op);
1562}
1563
1564static int sja1105_cgu_mii_tx_clk_config(struct sja1105_private *priv,
1565 int port, sja1105_mii_role_t role)
1566{
1567 const struct sja1105_regs *regs = priv->info->regs;
1568 struct sja1105_cgu_mii_ctrl mii_tx_clk;
1569 const int mac_clk_sources[] = {
1570 CLKSRC_MII0_TX_CLK,
1571 CLKSRC_MII1_TX_CLK,
1572 CLKSRC_MII2_TX_CLK,
1573 CLKSRC_MII3_TX_CLK,
1574 CLKSRC_MII4_TX_CLK,
1575 };
1576 const int phy_clk_sources[] = {
1577 CLKSRC_IDIV0,
1578 CLKSRC_IDIV1,
1579 CLKSRC_IDIV2,
1580 CLKSRC_IDIV3,
1581 CLKSRC_IDIV4,
1582 };
1583 u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
1584 int clksrc;
1585
1586 if (regs->mii_tx_clk[port] == SJA1105_RSV_ADDR)
1587 return 0;
1588
1589 if (role == XMII_MAC)
1590 clksrc = mac_clk_sources[port];
1591 else
1592 clksrc = phy_clk_sources[port];
1593
1594 /* Payload for packed_buf */
1595 mii_tx_clk.clksrc = clksrc;
1596 mii_tx_clk.autoblock = 1; /* Autoblock clk while changing clksrc */
1597 mii_tx_clk.pd = 0; /* Power Down off => enabled */
1598 sja1105_cgu_mii_control_packing(packed_buf, &mii_tx_clk, PACK);
1599
1600 return sja1105_xfer_buf(priv, SPI_WRITE, regs->mii_tx_clk[port],
1601 packed_buf, SJA1105_SIZE_CGU_CMD);
1602}
1603
1604static int
1605sja1105_cgu_mii_rx_clk_config(struct sja1105_private *priv, int port)
1606{
1607 const struct sja1105_regs *regs = priv->info->regs;
1608 u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
1609 struct sja1105_cgu_mii_ctrl mii_rx_clk;
1610 const int clk_sources[] = {
1611 CLKSRC_MII0_RX_CLK,
1612 CLKSRC_MII1_RX_CLK,
1613 CLKSRC_MII2_RX_CLK,
1614 CLKSRC_MII3_RX_CLK,
1615 CLKSRC_MII4_RX_CLK,
1616 };
1617
1618 if (regs->mii_rx_clk[port] == SJA1105_RSV_ADDR)
1619 return 0;
1620
1621 /* Payload for packed_buf */
1622 mii_rx_clk.clksrc = clk_sources[port];
1623 mii_rx_clk.autoblock = 1; /* Autoblock clk while changing clksrc */
1624 mii_rx_clk.pd = 0; /* Power Down off => enabled */
1625 sja1105_cgu_mii_control_packing(packed_buf, &mii_rx_clk, PACK);
1626
1627 return sja1105_xfer_buf(priv, SPI_WRITE, regs->mii_rx_clk[port],
1628 packed_buf, SJA1105_SIZE_CGU_CMD);
1629}
1630
1631static int
1632sja1105_cgu_mii_ext_tx_clk_config(struct sja1105_private *priv, int port)
1633{
1634 const struct sja1105_regs *regs = priv->info->regs;
1635 struct sja1105_cgu_mii_ctrl mii_ext_tx_clk;
1636 u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
1637 const int clk_sources[] = {
1638 CLKSRC_IDIV0,
1639 CLKSRC_IDIV1,
1640 CLKSRC_IDIV2,
1641 CLKSRC_IDIV3,
1642 CLKSRC_IDIV4,
1643 };
1644
1645 if (regs->mii_ext_tx_clk[port] == SJA1105_RSV_ADDR)
1646 return 0;
1647
1648 /* Payload for packed_buf */
1649 mii_ext_tx_clk.clksrc = clk_sources[port];
1650 mii_ext_tx_clk.autoblock = 1; /* Autoblock clk while changing clksrc */
1651 mii_ext_tx_clk.pd = 0; /* Power Down off => enabled */
1652 sja1105_cgu_mii_control_packing(packed_buf, &mii_ext_tx_clk, PACK);
1653
1654 return sja1105_xfer_buf(priv, SPI_WRITE, regs->mii_ext_tx_clk[port],
1655 packed_buf, SJA1105_SIZE_CGU_CMD);
1656}
1657
1658static int
1659sja1105_cgu_mii_ext_rx_clk_config(struct sja1105_private *priv, int port)
1660{
1661 const struct sja1105_regs *regs = priv->info->regs;
1662 struct sja1105_cgu_mii_ctrl mii_ext_rx_clk;
1663 u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
1664 const int clk_sources[] = {
1665 CLKSRC_IDIV0,
1666 CLKSRC_IDIV1,
1667 CLKSRC_IDIV2,
1668 CLKSRC_IDIV3,
1669 CLKSRC_IDIV4,
1670 };
1671
1672 if (regs->mii_ext_rx_clk[port] == SJA1105_RSV_ADDR)
1673 return 0;
1674
1675 /* Payload for packed_buf */
1676 mii_ext_rx_clk.clksrc = clk_sources[port];
1677 mii_ext_rx_clk.autoblock = 1; /* Autoblock clk while changing clksrc */
1678 mii_ext_rx_clk.pd = 0; /* Power Down off => enabled */
1679 sja1105_cgu_mii_control_packing(packed_buf, &mii_ext_rx_clk, PACK);
1680
1681 return sja1105_xfer_buf(priv, SPI_WRITE, regs->mii_ext_rx_clk[port],
1682 packed_buf, SJA1105_SIZE_CGU_CMD);
1683}
1684
1685static int sja1105_mii_clocking_setup(struct sja1105_private *priv, int port,
1686 sja1105_mii_role_t role)
1687{
1688 int rc;
1689
1690 rc = sja1105_cgu_idiv_config(priv, port, (role == XMII_PHY), 1);
1691 if (rc < 0)
1692 return rc;
1693
1694 rc = sja1105_cgu_mii_tx_clk_config(priv, port, role);
1695 if (rc < 0)
1696 return rc;
1697
1698 rc = sja1105_cgu_mii_rx_clk_config(priv, port);
1699 if (rc < 0)
1700 return rc;
1701
1702 if (role == XMII_PHY) {
1703 rc = sja1105_cgu_mii_ext_tx_clk_config(priv, port);
1704 if (rc < 0)
1705 return rc;
1706
1707 rc = sja1105_cgu_mii_ext_rx_clk_config(priv, port);
1708 if (rc < 0)
1709 return rc;
1710 }
1711 return 0;
1712}
1713
1714static void
1715sja1105_cgu_pll_control_packing(void *buf, struct sja1105_cgu_pll_ctrl *cmd,
1716 enum packing_op op)
1717{
1718 const int size = 4;
1719
1720 sja1105_packing(buf, &cmd->pllclksrc, 28, 24, size, op);
1721 sja1105_packing(buf, &cmd->msel, 23, 16, size, op);
1722 sja1105_packing(buf, &cmd->autoblock, 11, 11, size, op);
1723 sja1105_packing(buf, &cmd->psel, 9, 8, size, op);
1724 sja1105_packing(buf, &cmd->direct, 7, 7, size, op);
1725 sja1105_packing(buf, &cmd->fbsel, 6, 6, size, op);
1726 sja1105_packing(buf, &cmd->bypass, 1, 1, size, op);
1727 sja1105_packing(buf, &cmd->pd, 0, 0, size, op);
1728}
1729
1730static int sja1105_cgu_rgmii_tx_clk_config(struct sja1105_private *priv,
1731 int port, u64 speed)
1732{
1733 const struct sja1105_regs *regs = priv->info->regs;
1734 struct sja1105_cgu_mii_ctrl txc;
1735 u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
1736 int clksrc;
1737
1738 if (regs->rgmii_tx_clk[port] == SJA1105_RSV_ADDR)
1739 return 0;
1740
1741 if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS]) {
1742 clksrc = CLKSRC_PLL0;
1743 } else {
1744 int clk_sources[] = {CLKSRC_IDIV0, CLKSRC_IDIV1, CLKSRC_IDIV2,
1745 CLKSRC_IDIV3, CLKSRC_IDIV4};
1746 clksrc = clk_sources[port];
1747 }
1748
1749 /* RGMII: 125MHz for 1000, 25MHz for 100, 2.5MHz for 10 */
1750 txc.clksrc = clksrc;
1751 /* Autoblock clk while changing clksrc */
1752 txc.autoblock = 1;
1753 /* Power Down off => enabled */
1754 txc.pd = 0;
1755 sja1105_cgu_mii_control_packing(packed_buf, &txc, PACK);
1756
1757 return sja1105_xfer_buf(priv, SPI_WRITE, regs->rgmii_tx_clk[port],
1758 packed_buf, SJA1105_SIZE_CGU_CMD);
1759}
1760
1761/* AGU */
1762static void
1763sja1105_cfg_pad_mii_packing(void *buf, struct sja1105_cfg_pad_mii *cmd,
1764 enum packing_op op)
1765{
1766 const int size = 4;
1767
1768 sja1105_packing(buf, &cmd->d32_os, 28, 27, size, op);
1769 sja1105_packing(buf, &cmd->d32_ih, 26, 26, size, op);
1770 sja1105_packing(buf, &cmd->d32_ipud, 25, 24, size, op);
1771 sja1105_packing(buf, &cmd->d10_os, 20, 19, size, op);
1772 sja1105_packing(buf, &cmd->d10_ih, 18, 18, size, op);
1773 sja1105_packing(buf, &cmd->d10_ipud, 17, 16, size, op);
1774 sja1105_packing(buf, &cmd->ctrl_os, 12, 11, size, op);
1775 sja1105_packing(buf, &cmd->ctrl_ih, 10, 10, size, op);
1776 sja1105_packing(buf, &cmd->ctrl_ipud, 9, 8, size, op);
1777 sja1105_packing(buf, &cmd->clk_os, 4, 3, size, op);
1778 sja1105_packing(buf, &cmd->clk_ih, 2, 2, size, op);
1779 sja1105_packing(buf, &cmd->clk_ipud, 1, 0, size, op);
1780}
1781
1782static void
1783sja1110_cfg_pad_mii_id_packing(void *buf, struct sja1105_cfg_pad_mii_id *cmd,
1784 enum packing_op op)
1785{
1786 const int size = SJA1105_SIZE_CGU_CMD;
1787 u64 range = 4;
1788
1789 /* Fields RXC_RANGE and TXC_RANGE select the input frequency range:
1790 * 0 = 2.5MHz
1791 * 1 = 25MHz
1792 * 2 = 50MHz
1793 * 3 = 125MHz
1794 * 4 = Automatically determined by port speed.
1795 * There's no point in defining a structure different than the one for
1796 * SJA1105, so just hardcode the frequency range to automatic, just as
1797 * before.
1798 */
1799 sja1105_packing(buf, &cmd->rxc_stable_ovr, 26, 26, size, op);
1800 sja1105_packing(buf, &cmd->rxc_delay, 25, 21, size, op);
1801 sja1105_packing(buf, &range, 20, 18, size, op);
1802 sja1105_packing(buf, &cmd->rxc_bypass, 17, 17, size, op);
1803 sja1105_packing(buf, &cmd->rxc_pd, 16, 16, size, op);
1804 sja1105_packing(buf, &cmd->txc_stable_ovr, 10, 10, size, op);
1805 sja1105_packing(buf, &cmd->txc_delay, 9, 5, size, op);
1806 sja1105_packing(buf, &range, 4, 2, size, op);
1807 sja1105_packing(buf, &cmd->txc_bypass, 1, 1, size, op);
1808 sja1105_packing(buf, &cmd->txc_pd, 0, 0, size, op);
1809}
1810
1811static int sja1105_rgmii_cfg_pad_tx_config(struct sja1105_private *priv,
1812 int port)
1813{
1814 const struct sja1105_regs *regs = priv->info->regs;
1815 struct sja1105_cfg_pad_mii pad_mii_tx = {0};
1816 u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
1817
1818 if (regs->pad_mii_tx[port] == SJA1105_RSV_ADDR)
1819 return 0;
1820
1821 /* Payload */
1822 pad_mii_tx.d32_os = 3; /* TXD[3:2] output stage: */
1823 /* high noise/high speed */
1824 pad_mii_tx.d10_os = 3; /* TXD[1:0] output stage: */
1825 /* high noise/high speed */
1826 pad_mii_tx.d32_ipud = 2; /* TXD[3:2] input stage: */
1827 /* plain input (default) */
1828 pad_mii_tx.d10_ipud = 2; /* TXD[1:0] input stage: */
1829 /* plain input (default) */
1830 pad_mii_tx.ctrl_os = 3; /* TX_CTL / TX_ER output stage */
1831 pad_mii_tx.ctrl_ipud = 2; /* TX_CTL / TX_ER input stage (default) */
1832 pad_mii_tx.clk_os = 3; /* TX_CLK output stage */
1833 pad_mii_tx.clk_ih = 0; /* TX_CLK input hysteresis (default) */
1834 pad_mii_tx.clk_ipud = 2; /* TX_CLK input stage (default) */
1835 sja1105_cfg_pad_mii_packing(packed_buf, &pad_mii_tx, PACK);
1836
1837 return sja1105_xfer_buf(priv, SPI_WRITE, regs->pad_mii_tx[port],
1838 packed_buf, SJA1105_SIZE_CGU_CMD);
1839}
1840
1841static int sja1105_cfg_pad_rx_config(struct sja1105_private *priv, int port)
1842{
1843 const struct sja1105_regs *regs = priv->info->regs;
1844 struct sja1105_cfg_pad_mii pad_mii_rx = {0};
1845 u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
1846
1847 if (regs->pad_mii_rx[port] == SJA1105_RSV_ADDR)
1848 return 0;
1849
1850 /* Payload */
1851 pad_mii_rx.d32_ih = 0; /* RXD[3:2] input stage hysteresis: */
1852 /* non-Schmitt (default) */
1853 pad_mii_rx.d32_ipud = 2; /* RXD[3:2] input weak pull-up/down */
1854 /* plain input (default) */
1855 pad_mii_rx.d10_ih = 0; /* RXD[1:0] input stage hysteresis: */
1856 /* non-Schmitt (default) */
1857 pad_mii_rx.d10_ipud = 2; /* RXD[1:0] input weak pull-up/down */
1858 /* plain input (default) */
1859 pad_mii_rx.ctrl_ih = 0; /* RX_DV/CRS_DV/RX_CTL and RX_ER */
1860 /* input stage hysteresis: */
1861 /* non-Schmitt (default) */
1862 pad_mii_rx.ctrl_ipud = 3; /* RX_DV/CRS_DV/RX_CTL and RX_ER */
1863 /* input stage weak pull-up/down: */
1864 /* pull-down */
1865 pad_mii_rx.clk_os = 2; /* RX_CLK/RXC output stage: */
1866 /* medium noise/fast speed (default) */
1867 pad_mii_rx.clk_ih = 0; /* RX_CLK/RXC input hysteresis: */
1868 /* non-Schmitt (default) */
1869 pad_mii_rx.clk_ipud = 2; /* RX_CLK/RXC input pull-up/down: */
1870 /* plain input (default) */
1871 sja1105_cfg_pad_mii_packing(packed_buf, &pad_mii_rx, PACK);
1872
1873 return sja1105_xfer_buf(priv, SPI_WRITE, regs->pad_mii_rx[port],
1874 packed_buf, SJA1105_SIZE_CGU_CMD);
1875}
1876
1877static void
1878sja1105_cfg_pad_mii_id_packing(void *buf, struct sja1105_cfg_pad_mii_id *cmd,
1879 enum packing_op op)
1880{
1881 const int size = SJA1105_SIZE_CGU_CMD;
1882
1883 sja1105_packing(buf, &cmd->rxc_stable_ovr, 15, 15, size, op);
1884 sja1105_packing(buf, &cmd->rxc_delay, 14, 10, size, op);
1885 sja1105_packing(buf, &cmd->rxc_bypass, 9, 9, size, op);
1886 sja1105_packing(buf, &cmd->rxc_pd, 8, 8, size, op);
1887 sja1105_packing(buf, &cmd->txc_stable_ovr, 7, 7, size, op);
1888 sja1105_packing(buf, &cmd->txc_delay, 6, 2, size, op);
1889 sja1105_packing(buf, &cmd->txc_bypass, 1, 1, size, op);
1890 sja1105_packing(buf, &cmd->txc_pd, 0, 0, size, op);
1891}
1892
1893/* Valid range in degrees is an integer between 73.8 and 101.7 */
1894static u64 sja1105_rgmii_delay(u64 phase)
1895{
1896 /* UM11040.pdf: The delay in degree phase is 73.8 + delay_tune * 0.9.
1897 * To avoid floating point operations we'll multiply by 10
1898 * and get 1 decimal point precision.
1899 */
1900 phase *= 10;
1901 return (phase - 738) / 9;
1902}
1903
1904static int sja1105pqrs_setup_rgmii_delay(struct sja1105_private *priv, int port)
1905{
1906 const struct sja1105_regs *regs = priv->info->regs;
1907 struct sja1105_cfg_pad_mii_id pad_mii_id = {0};
1908 u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
1909 int rc;
1910
1911 if (priv->rgmii_rx_delay[port])
1912 pad_mii_id.rxc_delay = sja1105_rgmii_delay(90);
1913 if (priv->rgmii_tx_delay[port])
1914 pad_mii_id.txc_delay = sja1105_rgmii_delay(90);
1915
1916 /* Stage 1: Turn the RGMII delay lines off. */
1917 pad_mii_id.rxc_bypass = 1;
1918 pad_mii_id.rxc_pd = 1;
1919 pad_mii_id.txc_bypass = 1;
1920 pad_mii_id.txc_pd = 1;
1921 sja1105_cfg_pad_mii_id_packing(packed_buf, &pad_mii_id, PACK);
1922
1923 rc = sja1105_xfer_buf(priv, SPI_WRITE, regs->pad_mii_id[port],
1924 packed_buf, SJA1105_SIZE_CGU_CMD);
1925 if (rc < 0)
1926 return rc;
1927
1928 /* Stage 2: Turn the RGMII delay lines on. */
1929 if (priv->rgmii_rx_delay[port]) {
1930 pad_mii_id.rxc_bypass = 0;
1931 pad_mii_id.rxc_pd = 0;
1932 }
1933 if (priv->rgmii_tx_delay[port]) {
1934 pad_mii_id.txc_bypass = 0;
1935 pad_mii_id.txc_pd = 0;
1936 }
1937 sja1105_cfg_pad_mii_id_packing(packed_buf, &pad_mii_id, PACK);
1938
1939 return sja1105_xfer_buf(priv, SPI_WRITE, regs->pad_mii_id[port],
1940 packed_buf, SJA1105_SIZE_CGU_CMD);
1941}
1942
1943static int sja1110_setup_rgmii_delay(struct sja1105_private *priv, int port)
1944{
1945 const struct sja1105_regs *regs = priv->info->regs;
1946 struct sja1105_cfg_pad_mii_id pad_mii_id = {0};
1947 u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
1948
1949 pad_mii_id.rxc_pd = 1;
1950 pad_mii_id.txc_pd = 1;
1951
1952 if (priv->rgmii_rx_delay[port]) {
1953 pad_mii_id.rxc_delay = sja1105_rgmii_delay(90);
1954 /* The "BYPASS" bit in SJA1110 is actually a "don't bypass" */
1955 pad_mii_id.rxc_bypass = 1;
1956 pad_mii_id.rxc_pd = 0;
1957 }
1958
1959 if (priv->rgmii_tx_delay[port]) {
1960 pad_mii_id.txc_delay = sja1105_rgmii_delay(90);
1961 pad_mii_id.txc_bypass = 1;
1962 pad_mii_id.txc_pd = 0;
1963 }
1964
1965 sja1110_cfg_pad_mii_id_packing(packed_buf, &pad_mii_id, PACK);
1966
1967 return sja1105_xfer_buf(priv, SPI_WRITE, regs->pad_mii_id[port],
1968 packed_buf, SJA1105_SIZE_CGU_CMD);
1969}
1970
1971static int sja1105_rgmii_clocking_setup(struct sja1105_private *priv, int port,
1972 sja1105_mii_role_t role)
1973{
1974 struct sja1105_mac_config_entry *mac;
1975 struct udevice *dev = priv->dev;
1976 u64 speed;
1977 int rc;
1978
1979 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1980 speed = mac[port].speed;
1981
1982 if (speed == priv->info->port_speed[SJA1105_SPEED_1000MBPS]) {
1983 /* 1000Mbps, IDIV disabled (125 MHz) */
1984 rc = sja1105_cgu_idiv_config(priv, port, false, 1);
1985 } else if (speed == priv->info->port_speed[SJA1105_SPEED_100MBPS]) {
1986 /* 100Mbps, IDIV enabled, divide by 1 (25 MHz) */
1987 rc = sja1105_cgu_idiv_config(priv, port, true, 1);
1988 } else if (speed == priv->info->port_speed[SJA1105_SPEED_10MBPS]) {
1989 /* 10Mbps, IDIV enabled, divide by 10 (2.5 MHz) */
1990 rc = sja1105_cgu_idiv_config(priv, port, true, 10);
1991 } else if (speed == priv->info->port_speed[SJA1105_SPEED_AUTO]) {
1992 /* Skip CGU configuration if there is no speed available
1993 * (e.g. link is not established yet)
1994 */
1995 dev_dbg(dev, "Speed not available, skipping CGU config\n");
1996 return 0;
1997 } else {
1998 rc = -EINVAL;
1999 }
2000
2001 if (rc < 0) {
2002 dev_err(dev, "Failed to configure idiv\n");
2003 return rc;
2004 }
2005 rc = sja1105_cgu_rgmii_tx_clk_config(priv, port, speed);
2006 if (rc < 0) {
2007 dev_err(dev, "Failed to configure RGMII Tx clock\n");
2008 return rc;
2009 }
2010 rc = sja1105_rgmii_cfg_pad_tx_config(priv, port);
2011 if (rc < 0) {
2012 dev_err(dev, "Failed to configure Tx pad registers\n");
2013 return rc;
2014 }
2015
2016 if (!priv->info->setup_rgmii_delay)
2017 return 0;
2018
2019 return priv->info->setup_rgmii_delay(priv, port);
2020}
2021
2022static int sja1105_cgu_rmii_ref_clk_config(struct sja1105_private *priv,
2023 int port)
2024{
2025 const struct sja1105_regs *regs = priv->info->regs;
2026 u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
2027 struct sja1105_cgu_mii_ctrl ref_clk;
2028 const int clk_sources[] = {
2029 CLKSRC_MII0_TX_CLK,
2030 CLKSRC_MII1_TX_CLK,
2031 CLKSRC_MII2_TX_CLK,
2032 CLKSRC_MII3_TX_CLK,
2033 CLKSRC_MII4_TX_CLK,
2034 };
2035
2036 if (regs->rmii_ref_clk[port] == SJA1105_RSV_ADDR)
2037 return 0;
2038
2039 /* Payload for packed_buf */
2040 ref_clk.clksrc = clk_sources[port];
2041 ref_clk.autoblock = 1; /* Autoblock clk while changing clksrc */
2042 ref_clk.pd = 0; /* Power Down off => enabled */
2043 sja1105_cgu_mii_control_packing(packed_buf, &ref_clk, PACK);
2044
2045 return sja1105_xfer_buf(priv, SPI_WRITE, regs->rmii_ref_clk[port],
2046 packed_buf, SJA1105_SIZE_CGU_CMD);
2047}
2048
2049static int
2050sja1105_cgu_rmii_ext_tx_clk_config(struct sja1105_private *priv, int port)
2051{
2052 const struct sja1105_regs *regs = priv->info->regs;
2053 struct sja1105_cgu_mii_ctrl ext_tx_clk;
2054 u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
2055
2056 if (regs->rmii_ext_tx_clk[port] == SJA1105_RSV_ADDR)
2057 return 0;
2058
2059 /* Payload for packed_buf */
2060 ext_tx_clk.clksrc = CLKSRC_PLL1;
2061 ext_tx_clk.autoblock = 1; /* Autoblock clk while changing clksrc */
2062 ext_tx_clk.pd = 0; /* Power Down off => enabled */
2063 sja1105_cgu_mii_control_packing(packed_buf, &ext_tx_clk, PACK);
2064
2065 return sja1105_xfer_buf(priv, SPI_WRITE, regs->rmii_ext_tx_clk[port],
2066 packed_buf, SJA1105_SIZE_CGU_CMD);
2067}
2068
2069static int sja1105_cgu_rmii_pll_config(struct sja1105_private *priv)
2070{
2071 const struct sja1105_regs *regs = priv->info->regs;
2072 u8 packed_buf[SJA1105_SIZE_CGU_CMD] = {0};
2073 struct sja1105_cgu_pll_ctrl pll = {0};
2074 int rc;
2075
2076 if (regs->rmii_pll1 == SJA1105_RSV_ADDR)
2077 return 0;
2078
2079 /* Step 1: PLL1 setup for 50Mhz */
2080 pll.pllclksrc = 0xA;
2081 pll.msel = 0x1;
2082 pll.autoblock = 0x1;
2083 pll.psel = 0x1;
2084 pll.direct = 0x0;
2085 pll.fbsel = 0x1;
2086 pll.bypass = 0x0;
2087 pll.pd = 0x1;
2088
2089 sja1105_cgu_pll_control_packing(packed_buf, &pll, PACK);
2090 rc = sja1105_xfer_buf(priv, SPI_WRITE, regs->rmii_pll1, packed_buf,
2091 SJA1105_SIZE_CGU_CMD);
2092 if (rc < 0)
2093 return rc;
2094
2095 /* Step 2: Enable PLL1 */
2096 pll.pd = 0x0;
2097
2098 sja1105_cgu_pll_control_packing(packed_buf, &pll, PACK);
2099 rc = sja1105_xfer_buf(priv, SPI_WRITE, regs->rmii_pll1, packed_buf,
2100 SJA1105_SIZE_CGU_CMD);
2101 return rc;
2102}
2103
2104static int sja1105_rmii_clocking_setup(struct sja1105_private *priv, int port,
2105 sja1105_mii_role_t role)
2106{
2107 int rc;
2108
2109 /* AH1601.pdf chapter 2.5.1. Sources */
2110 if (role == XMII_MAC) {
2111 /* Configure and enable PLL1 for 50Mhz output */
2112 rc = sja1105_cgu_rmii_pll_config(priv);
2113 if (rc < 0)
2114 return rc;
2115 }
2116 /* Disable IDIV for this port */
2117 rc = sja1105_cgu_idiv_config(priv, port, false, 1);
2118 if (rc < 0)
2119 return rc;
2120 /* Source to sink mappings */
2121 rc = sja1105_cgu_rmii_ref_clk_config(priv, port);
2122 if (rc < 0)
2123 return rc;
2124 if (role == XMII_MAC) {
2125 rc = sja1105_cgu_rmii_ext_tx_clk_config(priv, port);
2126 if (rc < 0)
2127 return rc;
2128 }
2129 return 0;
2130}
2131
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002132static int sja1105_pcs_read(struct sja1105_private *priv, int addr,
2133 int devad, int regnum)
2134{
2135 return priv->mdio_pcs->read(priv->mdio_pcs, addr, devad, regnum);
2136}
2137
2138static int sja1105_pcs_write(struct sja1105_private *priv, int addr,
2139 int devad, int regnum, u16 val)
2140{
2141 return priv->mdio_pcs->write(priv->mdio_pcs, addr, devad, regnum, val);
2142}
2143
2144/* In NXP SJA1105, the PCS is integrated with a PMA that has the TX lane
2145 * polarity inverted by default (PLUS is MINUS, MINUS is PLUS). To obtain
2146 * normal non-inverted behavior, the TX lane polarity must be inverted in the
2147 * PCS, via the DIGITAL_CONTROL_2 register.
2148 */
2149static int sja1105_pma_config(struct sja1105_private *priv, int port)
2150{
2151 return sja1105_pcs_write(priv, port, MDIO_MMD_VEND2,
2152 DW_VR_MII_DIG_CTRL2,
2153 DW_VR_MII_DIG_CTRL2_TX_POL_INV);
2154}
2155
2156static int sja1110_pma_config(struct sja1105_private *priv, int port)
2157{
2158 u16 txpll_fbdiv = 0x19, txpll_refdiv = 0x1;
2159 u16 rxpll_fbdiv = 0x19, rxpll_refdiv = 0x1;
2160 u16 rx_cdr_ctle = 0x212a;
2161 u16 val;
2162 int rc;
2163
2164 /* Program TX PLL feedback divider and reference divider settings for
2165 * correct oscillation frequency.
2166 */
2167 rc = sja1105_pcs_write(priv, port, MDIO_MMD_VEND2, SJA1110_TXPLL_CTRL0,
2168 SJA1110_TXPLL_FBDIV(txpll_fbdiv));
2169 if (rc < 0)
2170 return rc;
2171
2172 rc = sja1105_pcs_write(priv, port, MDIO_MMD_VEND2, SJA1110_TXPLL_CTRL1,
2173 SJA1110_TXPLL_REFDIV(txpll_refdiv));
2174 if (rc < 0)
2175 return rc;
2176
2177 /* Program transmitter amplitude and disable amplitude trimming */
2178 rc = sja1105_pcs_write(priv, port, MDIO_MMD_VEND2,
2179 SJA1110_LANE_DRIVER1_0, SJA1110_TXDRV(0x5));
2180 if (rc < 0)
2181 return rc;
2182
2183 val = SJA1110_TXDRVTRIM_LSB(0xffffffull);
2184
2185 rc = sja1105_pcs_write(priv, port, MDIO_MMD_VEND2,
2186 SJA1110_LANE_DRIVER2_0, val);
2187 if (rc < 0)
2188 return rc;
2189
2190 val = SJA1110_TXDRVTRIM_MSB(0xffffffull) | SJA1110_LANE_DRIVER2_1_RSV;
2191
2192 rc = sja1105_pcs_write(priv, port, MDIO_MMD_VEND2,
2193 SJA1110_LANE_DRIVER2_1, val);
2194 if (rc < 0)
2195 return rc;
2196
2197 /* Enable input and output resistor terminations for low BER. */
2198 val = SJA1110_ACCOUPLE_RXVCM_EN | SJA1110_CDR_GAIN |
2199 SJA1110_RXRTRIM(4) | SJA1110_RXTEN | SJA1110_TXPLL_BWSEL |
2200 SJA1110_TXRTRIM(3) | SJA1110_TXTEN;
2201
2202 rc = sja1105_pcs_write(priv, port, MDIO_MMD_VEND2, SJA1110_LANE_TRIM,
2203 val);
2204 if (rc < 0)
2205 return rc;
2206
2207 /* Select PCS as transmitter data source. */
2208 rc = sja1105_pcs_write(priv, port, MDIO_MMD_VEND2,
2209 SJA1110_LANE_DATAPATH_1, 0);
2210 if (rc < 0)
2211 return rc;
2212
2213 /* Program RX PLL feedback divider and reference divider for correct
2214 * oscillation frequency.
2215 */
2216 rc = sja1105_pcs_write(priv, port, MDIO_MMD_VEND2, SJA1110_RXPLL_CTRL0,
2217 SJA1110_RXPLL_FBDIV(rxpll_fbdiv));
2218 if (rc < 0)
2219 return rc;
2220
2221 rc = sja1105_pcs_write(priv, port, MDIO_MMD_VEND2, SJA1110_RXPLL_CTRL1,
2222 SJA1110_RXPLL_REFDIV(rxpll_refdiv));
2223 if (rc < 0)
2224 return rc;
2225
2226 /* Program threshold for receiver signal detector.
2227 * Enable control of RXPLL by receiver signal detector to disable RXPLL
2228 * when an input signal is not present.
2229 */
2230 rc = sja1105_pcs_write(priv, port, MDIO_MMD_VEND2,
2231 SJA1110_RX_DATA_DETECT, 0x0005);
2232 if (rc < 0)
2233 return rc;
2234
2235 /* Enable TX and RX PLLs and circuits.
2236 * Release reset of PMA to enable data flow to/from PCS.
2237 */
2238 rc = sja1105_pcs_read(priv, port, MDIO_MMD_VEND2,
2239 SJA1110_POWERDOWN_ENABLE);
2240 if (rc < 0)
2241 return rc;
2242
2243 val = rc & ~(SJA1110_TXPLL_PD | SJA1110_TXPD | SJA1110_RXCH_PD |
2244 SJA1110_RXBIAS_PD | SJA1110_RESET_SER_EN |
2245 SJA1110_RESET_SER | SJA1110_RESET_DES);
2246 val |= SJA1110_RXPKDETEN | SJA1110_RCVEN;
2247
2248 rc = sja1105_pcs_write(priv, port, MDIO_MMD_VEND2,
2249 SJA1110_POWERDOWN_ENABLE, val);
2250 if (rc < 0)
2251 return rc;
2252
2253 /* Program continuous-time linear equalizer (CTLE) settings. */
2254 rc = sja1105_pcs_write(priv, port, MDIO_MMD_VEND2, SJA1110_RX_CDR_CTLE,
2255 rx_cdr_ctle);
2256 if (rc < 0)
2257 return rc;
2258
2259 return 0;
2260}
2261
2262static int sja1105_xpcs_config_aneg_c37_sgmii(struct sja1105_private *priv,
2263 int port)
2264{
2265 int rc;
2266
2267 rc = sja1105_pcs_read(priv, port, MDIO_MMD_VEND2, MDIO_CTRL1);
2268 if (rc < 0)
2269 return rc;
2270 rc &= ~MDIO_AN_CTRL1_ENABLE;
2271 rc = sja1105_pcs_write(priv, port, MDIO_MMD_VEND2, MDIO_CTRL1,
2272 rc);
2273 if (rc < 0)
2274 return rc;
2275
2276 rc = sja1105_pcs_read(priv, port, MDIO_MMD_VEND2, DW_VR_MII_AN_CTRL);
2277 if (rc < 0)
2278 return rc;
2279
2280 rc &= ~(DW_VR_MII_PCS_MODE_MASK | DW_VR_MII_TX_CONFIG_MASK);
2281 rc |= (DW_VR_MII_PCS_MODE_C37_SGMII <<
2282 DW_VR_MII_AN_CTRL_PCS_MODE_SHIFT &
2283 DW_VR_MII_PCS_MODE_MASK);
2284 rc |= (DW_VR_MII_TX_CONFIG_MAC_SIDE_SGMII <<
2285 DW_VR_MII_AN_CTRL_TX_CONFIG_SHIFT &
2286 DW_VR_MII_TX_CONFIG_MASK);
2287 rc = sja1105_pcs_write(priv, port, MDIO_MMD_VEND2, DW_VR_MII_AN_CTRL,
2288 rc);
2289 if (rc < 0)
2290 return rc;
2291
2292 rc = sja1105_pcs_read(priv, port, MDIO_MMD_VEND2, DW_VR_MII_DIG_CTRL1);
2293 if (rc < 0)
2294 return rc;
2295
2296 if (priv->xpcs_cfg[port].inband_an)
2297 rc |= DW_VR_MII_DIG_CTRL1_MAC_AUTO_SW;
2298 else
2299 rc &= ~DW_VR_MII_DIG_CTRL1_MAC_AUTO_SW;
2300
2301 rc = sja1105_pcs_write(priv, port, MDIO_MMD_VEND2, DW_VR_MII_DIG_CTRL1,
2302 rc);
2303 if (rc < 0)
2304 return rc;
2305
2306 rc = sja1105_pcs_read(priv, port, MDIO_MMD_VEND2, MDIO_CTRL1);
2307 if (rc < 0)
2308 return rc;
2309
2310 if (priv->xpcs_cfg[port].inband_an)
2311 rc |= MDIO_AN_CTRL1_ENABLE;
2312 else
2313 rc &= ~MDIO_AN_CTRL1_ENABLE;
2314
2315 return sja1105_pcs_write(priv, port, MDIO_MMD_VEND2, MDIO_CTRL1, rc);
2316}
2317
2318static int sja1105_xpcs_link_up_sgmii(struct sja1105_private *priv, int port)
2319{
2320 int val = BMCR_FULLDPLX;
2321
2322 if (priv->xpcs_cfg[port].inband_an)
2323 return 0;
2324
2325 switch (priv->xpcs_cfg[port].speed) {
2326 case SPEED_1000:
2327 val = BMCR_SPEED1000;
2328 break;
2329 case SPEED_100:
2330 val = BMCR_SPEED100;
2331 break;
2332 case SPEED_10:
2333 val = BMCR_SPEED10;
2334 break;
2335 default:
2336 dev_err(priv->dev, "Invalid PCS speed %d\n",
2337 priv->xpcs_cfg[port].speed);
2338 return -EINVAL;
2339 }
2340
2341 return sja1105_pcs_write(priv, port, MDIO_MMD_VEND2, MDIO_CTRL1, val);
2342}
2343
2344static int sja1105_sgmii_setup(struct sja1105_private *priv, int port)
2345{
2346 int rc;
2347
2348 rc = sja1105_xpcs_config_aneg_c37_sgmii(priv, port);
2349 if (rc)
2350 return rc;
2351
2352 rc = sja1105_xpcs_link_up_sgmii(priv, port);
2353 if (rc)
2354 return rc;
2355
2356 return priv->info->pma_config(priv, port);
2357}
2358
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002359static int sja1105_clocking_setup_port(struct sja1105_private *priv, int port)
2360{
2361 struct sja1105_xmii_params_entry *mii;
2362 sja1105_phy_interface_t phy_mode;
2363 sja1105_mii_role_t role;
2364 int rc;
2365
2366 mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
2367
2368 /* RGMII etc */
2369 phy_mode = mii->xmii_mode[port];
2370 /* MAC or PHY, for applicable types (not RGMII) */
2371 role = mii->phy_mac[port];
2372
2373 switch (phy_mode) {
2374 case XMII_MODE_MII:
2375 rc = sja1105_mii_clocking_setup(priv, port, role);
2376 break;
2377 case XMII_MODE_RMII:
2378 rc = sja1105_rmii_clocking_setup(priv, port, role);
2379 break;
2380 case XMII_MODE_RGMII:
2381 rc = sja1105_rgmii_clocking_setup(priv, port, role);
2382 break;
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002383 case XMII_MODE_SGMII:
2384 rc = sja1105_sgmii_setup(priv, port);
2385 break;
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002386 default:
2387 return -EINVAL;
2388 }
2389 if (rc)
2390 return rc;
2391
2392 /* Internally pull down the RX_DV/CRS_DV/RX_CTL and RX_ER inputs */
2393 return sja1105_cfg_pad_rx_config(priv, port);
2394}
2395
2396static int sja1105_clocking_setup(struct sja1105_private *priv)
2397{
2398 struct dsa_pdata *pdata = dev_get_uclass_plat(priv->dev);
2399 int port, rc;
2400
2401 for (port = 0; port < pdata->num_ports; port++) {
2402 rc = sja1105_clocking_setup_port(priv, port);
2403 if (rc < 0)
2404 return rc;
2405 }
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002406 return 0;
2407}
2408
2409static int sja1105_pcs_mdio_read(struct mii_dev *bus, int phy, int mmd, int reg)
2410{
2411 u8 packed_buf[SJA1105_SIZE_MDIO_CMD] = {0};
2412 struct sja1105_private *priv = bus->priv;
2413 const int size = SJA1105_SIZE_MDIO_CMD;
2414 u64 addr, tmp;
2415 int rc;
2416
2417 if (mmd == MDIO_DEVAD_NONE)
2418 return -ENODEV;
2419
2420 if (!priv->info->supports_sgmii[phy])
2421 return -ENODEV;
2422
2423 addr = (mmd << 16) | (reg & GENMASK(15, 0));
2424
2425 if (mmd != MDIO_MMD_VEND1 && mmd != MDIO_MMD_VEND2)
2426 return 0xffff;
2427
2428 rc = sja1105_xfer_buf(priv, SPI_READ, addr, packed_buf, size);
2429 if (rc < 0)
2430 return rc;
2431
2432 sja1105_packing(packed_buf, &tmp, 31, 0, size, UNPACK);
2433
2434 return tmp & 0xffff;
2435}
2436
2437static int sja1105_pcs_mdio_write(struct mii_dev *bus, int phy, int mmd,
2438 int reg, u16 val)
2439{
2440 u8 packed_buf[SJA1105_SIZE_MDIO_CMD] = {0};
2441 struct sja1105_private *priv = bus->priv;
2442 const int size = SJA1105_SIZE_MDIO_CMD;
2443 u64 addr, tmp;
2444
2445 if (mmd == MDIO_DEVAD_NONE)
2446 return -ENODEV;
2447
2448 if (!priv->info->supports_sgmii[phy])
2449 return -ENODEV;
2450
2451 addr = (mmd << 16) | (reg & GENMASK(15, 0));
2452 tmp = val;
2453
2454 if (mmd != MDIO_MMD_VEND1 && mmd != MDIO_MMD_VEND2)
2455 return -ENODEV;
2456
2457 sja1105_packing(packed_buf, &tmp, 31, 0, size, PACK);
2458
2459 return sja1105_xfer_buf(priv, SPI_WRITE, addr, packed_buf, size);
2460}
2461
2462static int sja1110_pcs_mdio_read(struct mii_dev *bus, int phy, int mmd, int reg)
2463{
2464 struct sja1105_private *priv = bus->priv;
2465 const struct sja1105_regs *regs = priv->info->regs;
2466 u8 packed_buf[SJA1105_SIZE_MDIO_CMD] = {0};
2467 const int size = SJA1105_SIZE_MDIO_CMD;
2468 int offset, bank;
2469 u64 addr, tmp;
2470 int rc;
2471
2472 if (mmd == MDIO_DEVAD_NONE)
2473 return -ENODEV;
2474
2475 if (regs->pcs_base[phy] == SJA1105_RSV_ADDR)
2476 return -ENODEV;
2477
2478 addr = (mmd << 16) | (reg & GENMASK(15, 0));
2479
2480 bank = addr >> 8;
2481 offset = addr & GENMASK(7, 0);
2482
2483 /* This addressing scheme reserves register 0xff for the bank address
2484 * register, so that can never be addressed.
2485 */
2486 if (offset == 0xff)
2487 return -ENODEV;
2488
2489 tmp = bank;
2490
2491 sja1105_packing(packed_buf, &tmp, 31, 0, size, PACK);
2492
2493 rc = sja1105_xfer_buf(priv, SPI_WRITE,
2494 regs->pcs_base[phy] + SJA1110_PCS_BANK_REG,
2495 packed_buf, size);
2496 if (rc < 0)
2497 return rc;
2498
2499 rc = sja1105_xfer_buf(priv, SPI_READ, regs->pcs_base[phy] + offset,
2500 packed_buf, size);
2501 if (rc < 0)
2502 return rc;
2503
2504 sja1105_packing(packed_buf, &tmp, 31, 0, size, UNPACK);
2505
2506 return tmp & 0xffff;
2507}
2508
2509static int sja1110_pcs_mdio_write(struct mii_dev *bus, int phy, int mmd,
2510 int reg, u16 val)
2511{
2512 struct sja1105_private *priv = bus->priv;
2513 const struct sja1105_regs *regs = priv->info->regs;
2514 u8 packed_buf[SJA1105_SIZE_MDIO_CMD] = {0};
2515 const int size = SJA1105_SIZE_MDIO_CMD;
2516 int offset, bank;
2517 u64 addr, tmp;
2518 int rc;
2519
2520 if (mmd == MDIO_DEVAD_NONE)
2521 return -ENODEV;
2522
2523 if (regs->pcs_base[phy] == SJA1105_RSV_ADDR)
2524 return -ENODEV;
2525
2526 addr = (mmd << 16) | (reg & GENMASK(15, 0));
2527
2528 bank = addr >> 8;
2529 offset = addr & GENMASK(7, 0);
2530
2531 /* This addressing scheme reserves register 0xff for the bank address
2532 * register, so that can never be addressed.
2533 */
2534 if (offset == 0xff)
2535 return -ENODEV;
2536
2537 tmp = bank;
2538 sja1105_packing(packed_buf, &tmp, 31, 0, size, PACK);
2539
2540 rc = sja1105_xfer_buf(priv, SPI_WRITE,
2541 regs->pcs_base[phy] + SJA1110_PCS_BANK_REG,
2542 packed_buf, size);
2543 if (rc < 0)
2544 return rc;
2545
2546 tmp = val;
2547 sja1105_packing(packed_buf, &tmp, 31, 0, size, PACK);
2548
2549 return sja1105_xfer_buf(priv, SPI_WRITE, regs->pcs_base[phy] + offset,
2550 packed_buf, size);
2551}
2552
2553static int sja1105_mdiobus_register(struct sja1105_private *priv)
2554{
2555 struct udevice *dev = priv->dev;
2556 struct mii_dev *bus;
2557 int rc;
2558
2559 if (!priv->info->pcs_mdio_read || !priv->info->pcs_mdio_write)
2560 return 0;
2561
2562 bus = mdio_alloc();
2563 if (!bus)
2564 return -ENOMEM;
2565
2566 snprintf(bus->name, MDIO_NAME_LEN, "%s-pcs", dev->name);
2567 bus->read = priv->info->pcs_mdio_read;
2568 bus->write = priv->info->pcs_mdio_write;
2569 bus->priv = priv;
2570
2571 rc = mdio_register(bus);
2572 if (rc) {
2573 mdio_free(bus);
2574 return rc;
2575 }
2576
2577 priv->mdio_pcs = bus;
2578
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002579 return 0;
2580}
2581
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002582static void sja1105_mdiobus_unregister(struct sja1105_private *priv)
2583{
2584 if (!priv->mdio_pcs)
2585 return;
2586
2587 mdio_unregister(priv->mdio_pcs);
2588 mdio_free(priv->mdio_pcs);
2589}
2590
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002591static const struct sja1105_regs sja1105et_regs = {
2592 .device_id = 0x0,
2593 .prod_id = 0x100BC3,
2594 .status = 0x1,
2595 .port_control = 0x11,
2596 .config = 0x020000,
2597 .rgu = 0x100440,
2598 /* UM10944.pdf, Table 86, ACU Register overview */
2599 .pad_mii_tx = {0x100800, 0x100802, 0x100804, 0x100806, 0x100808},
2600 .pad_mii_rx = {0x100801, 0x100803, 0x100805, 0x100807, 0x100809},
2601 .rmii_pll1 = 0x10000A,
2602 .cgu_idiv = {0x10000B, 0x10000C, 0x10000D, 0x10000E, 0x10000F},
2603 /* UM10944.pdf, Table 78, CGU Register overview */
2604 .mii_tx_clk = {0x100013, 0x10001A, 0x100021, 0x100028, 0x10002F},
2605 .mii_rx_clk = {0x100014, 0x10001B, 0x100022, 0x100029, 0x100030},
2606 .mii_ext_tx_clk = {0x100018, 0x10001F, 0x100026, 0x10002D, 0x100034},
2607 .mii_ext_rx_clk = {0x100019, 0x100020, 0x100027, 0x10002E, 0x100035},
2608 .rgmii_tx_clk = {0x100016, 0x10001D, 0x100024, 0x10002B, 0x100032},
2609 .rmii_ref_clk = {0x100015, 0x10001C, 0x100023, 0x10002A, 0x100031},
2610 .rmii_ext_tx_clk = {0x100018, 0x10001F, 0x100026, 0x10002D, 0x100034},
2611};
2612
2613static const struct sja1105_regs sja1105pqrs_regs = {
2614 .device_id = 0x0,
2615 .prod_id = 0x100BC3,
2616 .status = 0x1,
2617 .port_control = 0x12,
2618 .config = 0x020000,
2619 .rgu = 0x100440,
2620 /* UM10944.pdf, Table 86, ACU Register overview */
2621 .pad_mii_tx = {0x100800, 0x100802, 0x100804, 0x100806, 0x100808},
2622 .pad_mii_rx = {0x100801, 0x100803, 0x100805, 0x100807, 0x100809},
2623 .pad_mii_id = {0x100810, 0x100811, 0x100812, 0x100813, 0x100814},
2624 .rmii_pll1 = 0x10000A,
2625 .cgu_idiv = {0x10000B, 0x10000C, 0x10000D, 0x10000E, 0x10000F},
2626 /* UM11040.pdf, Table 114 */
2627 .mii_tx_clk = {0x100013, 0x100019, 0x10001F, 0x100025, 0x10002B},
2628 .mii_rx_clk = {0x100014, 0x10001A, 0x100020, 0x100026, 0x10002C},
2629 .mii_ext_tx_clk = {0x100017, 0x10001D, 0x100023, 0x100029, 0x10002F},
2630 .mii_ext_rx_clk = {0x100018, 0x10001E, 0x100024, 0x10002A, 0x100030},
2631 .rgmii_tx_clk = {0x100016, 0x10001C, 0x100022, 0x100028, 0x10002E},
2632 .rmii_ref_clk = {0x100015, 0x10001B, 0x100021, 0x100027, 0x10002D},
2633 .rmii_ext_tx_clk = {0x100017, 0x10001D, 0x100023, 0x100029, 0x10002F},
2634};
2635
2636static const struct sja1105_regs sja1110_regs = {
2637 .device_id = SJA1110_SPI_ADDR(0x0),
2638 .prod_id = SJA1110_ACU_ADDR(0xf00),
2639 .status = SJA1110_SPI_ADDR(0x4),
2640 .port_control = SJA1110_SPI_ADDR(0x50), /* actually INHIB_TX */
2641 .config = 0x020000,
2642 .rgu = SJA1110_RGU_ADDR(0x100), /* Reset Control Register 0 */
2643 /* Ports 2 and 3 are capable of xMII, but there isn't anything to
2644 * configure in the CGU/ACU for them.
2645 */
2646 .pad_mii_tx = {SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2647 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2648 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2649 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2650 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2651 SJA1105_RSV_ADDR},
2652 .pad_mii_rx = {SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2653 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2654 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2655 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2656 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2657 SJA1105_RSV_ADDR},
2658 .pad_mii_id = {SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2659 SJA1110_ACU_ADDR(0x18), SJA1110_ACU_ADDR(0x28),
2660 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2661 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2662 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2663 SJA1105_RSV_ADDR},
2664 .rmii_pll1 = SJA1105_RSV_ADDR,
2665 .cgu_idiv = {SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2666 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2667 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2668 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR},
2669 .mii_tx_clk = {SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2670 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2671 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2672 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR},
2673 .mii_rx_clk = {SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2674 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2675 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2676 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR},
2677 .mii_ext_tx_clk = {SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2678 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2679 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2680 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR},
2681 .mii_ext_rx_clk = {SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2682 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2683 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2684 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR},
2685 .rgmii_tx_clk = {SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2686 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2687 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2688 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR},
2689 .rmii_ref_clk = {SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2690 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2691 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2692 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR},
2693 .rmii_ext_tx_clk = {SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2694 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2695 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2696 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2697 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2698 SJA1105_RSV_ADDR},
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002699 .pcs_base = {SJA1105_RSV_ADDR, 0x1c1400, 0x1c1800, 0x1c1c00, 0x1c2000,
2700 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR,
2701 SJA1105_RSV_ADDR, SJA1105_RSV_ADDR, SJA1105_RSV_ADDR},
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002702};
2703
2704enum sja1105_switch_id {
2705 SJA1105E = 0,
2706 SJA1105T,
2707 SJA1105P,
2708 SJA1105Q,
2709 SJA1105R,
2710 SJA1105S,
2711 SJA1110A,
2712 SJA1110B,
2713 SJA1110C,
2714 SJA1110D,
2715 SJA1105_MAX_SWITCH_ID,
2716};
2717
2718static const struct sja1105_info sja1105_info[] = {
2719 [SJA1105E] = {
2720 .device_id = SJA1105E_DEVICE_ID,
2721 .part_no = SJA1105ET_PART_NO,
2722 .static_ops = sja1105et_table_ops,
2723 .reset_cmd = sja1105et_reset_cmd,
2724 .regs = &sja1105et_regs,
2725 .port_speed = {
2726 [SJA1105_SPEED_AUTO] = 0,
2727 [SJA1105_SPEED_10MBPS] = 3,
2728 [SJA1105_SPEED_100MBPS] = 2,
2729 [SJA1105_SPEED_1000MBPS] = 1,
2730 },
2731 .supports_mii = {true, true, true, true, true},
2732 .supports_rmii = {true, true, true, true, true},
2733 .supports_rgmii = {true, true, true, true, true},
2734 .name = "SJA1105E",
2735 },
2736 [SJA1105T] = {
2737 .device_id = SJA1105T_DEVICE_ID,
2738 .part_no = SJA1105ET_PART_NO,
2739 .static_ops = sja1105et_table_ops,
2740 .reset_cmd = sja1105et_reset_cmd,
2741 .regs = &sja1105et_regs,
2742 .port_speed = {
2743 [SJA1105_SPEED_AUTO] = 0,
2744 [SJA1105_SPEED_10MBPS] = 3,
2745 [SJA1105_SPEED_100MBPS] = 2,
2746 [SJA1105_SPEED_1000MBPS] = 1,
2747 },
2748 .supports_mii = {true, true, true, true, true},
2749 .supports_rmii = {true, true, true, true, true},
2750 .supports_rgmii = {true, true, true, true, true},
2751 .name = "SJA1105T",
2752 },
2753 [SJA1105P] = {
2754 .device_id = SJA1105PR_DEVICE_ID,
2755 .part_no = SJA1105P_PART_NO,
2756 .static_ops = sja1105pqrs_table_ops,
2757 .setup_rgmii_delay = sja1105pqrs_setup_rgmii_delay,
2758 .reset_cmd = sja1105pqrs_reset_cmd,
2759 .regs = &sja1105pqrs_regs,
2760 .port_speed = {
2761 [SJA1105_SPEED_AUTO] = 0,
2762 [SJA1105_SPEED_10MBPS] = 3,
2763 [SJA1105_SPEED_100MBPS] = 2,
2764 [SJA1105_SPEED_1000MBPS] = 1,
2765 },
2766 .supports_mii = {true, true, true, true, true},
2767 .supports_rmii = {true, true, true, true, true},
2768 .supports_rgmii = {true, true, true, true, true},
2769 .name = "SJA1105P",
2770 },
2771 [SJA1105Q] = {
2772 .device_id = SJA1105QS_DEVICE_ID,
2773 .part_no = SJA1105Q_PART_NO,
2774 .static_ops = sja1105pqrs_table_ops,
2775 .setup_rgmii_delay = sja1105pqrs_setup_rgmii_delay,
2776 .reset_cmd = sja1105pqrs_reset_cmd,
2777 .regs = &sja1105pqrs_regs,
2778 .port_speed = {
2779 [SJA1105_SPEED_AUTO] = 0,
2780 [SJA1105_SPEED_10MBPS] = 3,
2781 [SJA1105_SPEED_100MBPS] = 2,
2782 [SJA1105_SPEED_1000MBPS] = 1,
2783 },
2784 .supports_mii = {true, true, true, true, true},
2785 .supports_rmii = {true, true, true, true, true},
2786 .supports_rgmii = {true, true, true, true, true},
2787 .name = "SJA1105Q",
2788 },
2789 [SJA1105R] = {
2790 .device_id = SJA1105PR_DEVICE_ID,
2791 .part_no = SJA1105R_PART_NO,
2792 .static_ops = sja1105pqrs_table_ops,
2793 .setup_rgmii_delay = sja1105pqrs_setup_rgmii_delay,
2794 .reset_cmd = sja1105pqrs_reset_cmd,
2795 .regs = &sja1105pqrs_regs,
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002796 .pcs_mdio_read = sja1105_pcs_mdio_read,
2797 .pcs_mdio_write = sja1105_pcs_mdio_write,
2798 .pma_config = sja1105_pma_config,
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002799 .port_speed = {
2800 [SJA1105_SPEED_AUTO] = 0,
2801 [SJA1105_SPEED_10MBPS] = 3,
2802 [SJA1105_SPEED_100MBPS] = 2,
2803 [SJA1105_SPEED_1000MBPS] = 1,
2804 },
2805 .supports_mii = {true, true, true, true, true},
2806 .supports_rmii = {true, true, true, true, true},
2807 .supports_rgmii = {true, true, true, true, true},
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002808 .supports_sgmii = {false, false, false, false, true},
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002809 .name = "SJA1105R",
2810 },
2811 [SJA1105S] = {
2812 .device_id = SJA1105QS_DEVICE_ID,
2813 .part_no = SJA1105S_PART_NO,
2814 .static_ops = sja1105pqrs_table_ops,
2815 .setup_rgmii_delay = sja1105pqrs_setup_rgmii_delay,
2816 .reset_cmd = sja1105pqrs_reset_cmd,
2817 .regs = &sja1105pqrs_regs,
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002818 .pcs_mdio_read = sja1105_pcs_mdio_read,
2819 .pcs_mdio_write = sja1105_pcs_mdio_write,
2820 .pma_config = sja1105_pma_config,
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002821 .port_speed = {
2822 [SJA1105_SPEED_AUTO] = 0,
2823 [SJA1105_SPEED_10MBPS] = 3,
2824 [SJA1105_SPEED_100MBPS] = 2,
2825 [SJA1105_SPEED_1000MBPS] = 1,
2826 },
2827 .supports_mii = {true, true, true, true, true},
2828 .supports_rmii = {true, true, true, true, true},
2829 .supports_rgmii = {true, true, true, true, true},
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002830 .supports_sgmii = {false, false, false, false, true},
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002831 .name = "SJA1105S",
2832 },
2833 [SJA1110A] = {
2834 .device_id = SJA1110_DEVICE_ID,
2835 .part_no = SJA1110A_PART_NO,
2836 .static_ops = sja1110_table_ops,
2837 .setup_rgmii_delay = sja1110_setup_rgmii_delay,
2838 .reset_cmd = sja1110_reset_cmd,
2839 .regs = &sja1110_regs,
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002840 .pcs_mdio_read = sja1110_pcs_mdio_read,
2841 .pcs_mdio_write = sja1110_pcs_mdio_write,
2842 .pma_config = sja1110_pma_config,
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002843 .port_speed = {
2844 [SJA1105_SPEED_AUTO] = 0,
2845 [SJA1105_SPEED_10MBPS] = 4,
2846 [SJA1105_SPEED_100MBPS] = 3,
2847 [SJA1105_SPEED_1000MBPS] = 2,
2848 },
2849 .supports_mii = {true, true, true, true, false,
2850 true, true, true, true, true, true},
2851 .supports_rmii = {false, false, true, true, false,
2852 false, false, false, false, false, false},
2853 .supports_rgmii = {false, false, true, true, false,
2854 false, false, false, false, false, false},
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002855 .supports_sgmii = {false, true, true, true, true,
2856 false, false, false, false, false, false},
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002857 .name = "SJA1110A",
2858 },
2859 [SJA1110B] = {
2860 .device_id = SJA1110_DEVICE_ID,
2861 .part_no = SJA1110B_PART_NO,
2862 .static_ops = sja1110_table_ops,
2863 .setup_rgmii_delay = sja1110_setup_rgmii_delay,
2864 .reset_cmd = sja1110_reset_cmd,
2865 .regs = &sja1110_regs,
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002866 .pcs_mdio_read = sja1110_pcs_mdio_read,
2867 .pcs_mdio_write = sja1110_pcs_mdio_write,
2868 .pma_config = sja1110_pma_config,
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002869 .port_speed = {
2870 [SJA1105_SPEED_AUTO] = 0,
2871 [SJA1105_SPEED_10MBPS] = 4,
2872 [SJA1105_SPEED_100MBPS] = 3,
2873 [SJA1105_SPEED_1000MBPS] = 2,
2874 },
2875 .supports_mii = {true, true, true, true, false,
2876 true, true, true, true, true, false},
2877 .supports_rmii = {false, false, true, true, false,
2878 false, false, false, false, false, false},
2879 .supports_rgmii = {false, false, true, true, false,
2880 false, false, false, false, false, false},
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002881 .supports_sgmii = {false, false, false, true, true,
2882 false, false, false, false, false, false},
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002883 .name = "SJA1110B",
2884 },
2885 [SJA1110C] = {
2886 .device_id = SJA1110_DEVICE_ID,
2887 .part_no = SJA1110C_PART_NO,
2888 .static_ops = sja1110_table_ops,
2889 .setup_rgmii_delay = sja1110_setup_rgmii_delay,
2890 .reset_cmd = sja1110_reset_cmd,
2891 .regs = &sja1110_regs,
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002892 .pcs_mdio_read = sja1110_pcs_mdio_read,
2893 .pcs_mdio_write = sja1110_pcs_mdio_write,
2894 .pma_config = sja1110_pma_config,
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002895 .port_speed = {
2896 [SJA1105_SPEED_AUTO] = 0,
2897 [SJA1105_SPEED_10MBPS] = 4,
2898 [SJA1105_SPEED_100MBPS] = 3,
2899 [SJA1105_SPEED_1000MBPS] = 2,
2900 },
2901 .supports_mii = {true, true, true, true, false,
2902 true, true, true, false, false, false},
2903 .supports_rmii = {false, false, true, true, false,
2904 false, false, false, false, false, false},
2905 .supports_rgmii = {false, false, true, true, false,
2906 false, false, false, false, false, false},
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002907 .supports_sgmii = {false, false, false, false, true,
2908 false, false, false, false, false, false},
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002909 .name = "SJA1110C",
2910 },
2911 [SJA1110D] = {
2912 .device_id = SJA1110_DEVICE_ID,
2913 .part_no = SJA1110D_PART_NO,
2914 .static_ops = sja1110_table_ops,
2915 .setup_rgmii_delay = sja1110_setup_rgmii_delay,
2916 .reset_cmd = sja1110_reset_cmd,
2917 .regs = &sja1110_regs,
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002918 .pcs_mdio_read = sja1110_pcs_mdio_read,
2919 .pcs_mdio_write = sja1110_pcs_mdio_write,
2920 .pma_config = sja1110_pma_config,
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002921 .port_speed = {
2922 [SJA1105_SPEED_AUTO] = 0,
2923 [SJA1105_SPEED_10MBPS] = 4,
2924 [SJA1105_SPEED_100MBPS] = 3,
2925 [SJA1105_SPEED_1000MBPS] = 2,
2926 },
2927 .supports_mii = {true, false, true, false, false,
2928 true, true, true, false, false, false},
2929 .supports_rmii = {false, false, true, false, false,
2930 false, false, false, false, false, false},
2931 .supports_rgmii = {false, false, true, false, false,
2932 false, false, false, false, false, false},
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03002933 .supports_sgmii = {false, true, true, true, true,
2934 false, false, false, false, false, false},
Vladimir Oltean67bfc852021-09-29 18:04:41 +03002935 .name = "SJA1110D",
2936 },
2937};
2938
2939struct sja1105_status {
2940 u64 configs;
2941 u64 crcchkl;
2942 u64 ids;
2943 u64 crcchkg;
2944};
2945
2946static void sja1105_status_unpack(void *buf, struct sja1105_status *status)
2947{
2948 sja1105_packing(buf, &status->configs, 31, 31, 4, UNPACK);
2949 sja1105_packing(buf, &status->crcchkl, 30, 30, 4, UNPACK);
2950 sja1105_packing(buf, &status->ids, 29, 29, 4, UNPACK);
2951 sja1105_packing(buf, &status->crcchkg, 28, 28, 4, UNPACK);
2952}
2953
2954static int sja1105_status_get(struct sja1105_private *priv,
2955 struct sja1105_status *status)
2956{
2957 const struct sja1105_regs *regs = priv->info->regs;
2958 u8 packed_buf[4];
2959 int rc;
2960
2961 rc = sja1105_xfer_buf(priv, SPI_READ, regs->status, packed_buf, 4);
2962 if (rc < 0)
2963 return rc;
2964
2965 sja1105_status_unpack(packed_buf, status);
2966
2967 return 0;
2968}
2969
2970/* Not const because unpacking priv->static_config into buffers and preparing
2971 * for upload requires the recalculation of table CRCs and updating the
2972 * structures with these.
2973 */
2974static int
2975static_config_buf_prepare_for_upload(struct sja1105_private *priv,
2976 void *config_buf, int buf_len)
2977{
2978 struct sja1105_static_config *config = &priv->static_config;
2979 struct sja1105_table_header final_header;
2980 char *final_header_ptr;
2981 int crc_len;
2982
2983 /* Write Device ID and config tables to config_buf */
2984 sja1105_static_config_pack(config_buf, config);
2985 /* Recalculate CRC of the last header (right now 0xDEADBEEF).
2986 * Don't include the CRC field itself.
2987 */
2988 crc_len = buf_len - 4;
2989 /* Read the whole table header */
2990 final_header_ptr = config_buf + buf_len - SJA1105_SIZE_TABLE_HEADER;
2991 sja1105_table_header_packing(final_header_ptr, &final_header, UNPACK);
2992 /* Modify */
2993 final_header.crc = sja1105_crc32(config_buf, crc_len);
2994 /* Rewrite */
2995 sja1105_table_header_packing(final_header_ptr, &final_header, PACK);
2996
2997 return 0;
2998}
2999
3000static int sja1105_static_config_upload(struct sja1105_private *priv)
3001{
3002 struct sja1105_static_config *config = &priv->static_config;
3003 const struct sja1105_regs *regs = priv->info->regs;
3004 struct sja1105_status status;
3005 u8 *config_buf;
3006 int buf_len;
3007 int rc;
3008
3009 buf_len = sja1105_static_config_get_length(config);
3010 config_buf = calloc(buf_len, sizeof(char));
3011 if (!config_buf)
3012 return -ENOMEM;
3013
3014 rc = static_config_buf_prepare_for_upload(priv, config_buf, buf_len);
3015 if (rc < 0) {
3016 printf("Invalid config, cannot upload\n");
3017 rc = -EINVAL;
3018 goto out;
3019 }
3020 /* Put the SJA1105 in programming mode */
3021 rc = priv->info->reset_cmd(priv);
3022 if (rc < 0) {
3023 printf("Failed to reset switch\n");
3024 goto out;
3025 }
3026 /* Wait for the switch to come out of reset */
3027 udelay(1000);
3028 /* Upload the static config to the device */
3029 rc = sja1105_xfer_buf(priv, SPI_WRITE, regs->config,
3030 config_buf, buf_len);
3031 if (rc < 0) {
3032 printf("Failed to upload config\n");
3033 goto out;
3034 }
3035 /* Check that SJA1105 responded well to the config upload */
3036 rc = sja1105_status_get(priv, &status);
3037 if (rc < 0)
3038 goto out;
3039
3040 if (status.ids == 1) {
3041 printf("Mismatch between hardware and static config device id. "
3042 "Wrote 0x%llx, wants 0x%llx\n",
3043 config->device_id, priv->info->device_id);
3044 rc = -EIO;
3045 goto out;
3046 }
3047 if (status.crcchkl == 1 || status.crcchkg == 1) {
3048 printf("Switch reported invalid CRC on static config\n");
3049 rc = -EIO;
3050 goto out;
3051 }
3052 if (status.configs == 0) {
3053 printf("Switch reported that config is invalid\n");
3054 rc = -EIO;
3055 goto out;
3056 }
3057
3058out:
3059 free(config_buf);
3060 return rc;
3061}
3062
3063static int sja1105_static_config_reload(struct sja1105_private *priv)
3064{
3065 int rc;
3066
3067 rc = sja1105_static_config_upload(priv);
3068 if (rc < 0) {
3069 printf("Failed to load static config: %d\n", rc);
3070 return rc;
3071 }
3072
3073 /* Configure the CGU (PHY link modes and speeds) */
3074 rc = sja1105_clocking_setup(priv);
3075 if (rc < 0) {
3076 printf("Failed to configure MII clocking: %d\n", rc);
3077 return rc;
3078 }
3079
3080 return 0;
3081}
3082
3083static int sja1105_port_probe(struct udevice *dev, int port,
3084 struct phy_device *phy)
3085{
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03003086 struct sja1105_private *priv = dev_get_priv(dev);
3087 ofnode node = dsa_port_get_ofnode(dev, port);
Vladimir Oltean67bfc852021-09-29 18:04:41 +03003088 phy_interface_t phy_mode = phy->interface;
3089
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03003090 priv->xpcs_cfg[port].inband_an = ofnode_eth_uses_inband_aneg(node);
3091
Vladimir Oltean67bfc852021-09-29 18:04:41 +03003092 if (phy_mode == PHY_INTERFACE_MODE_MII ||
3093 phy_mode == PHY_INTERFACE_MODE_RMII) {
3094 phy->supported &= PHY_BASIC_FEATURES;
3095 phy->advertising &= PHY_BASIC_FEATURES;
3096 } else {
3097 phy->supported &= PHY_GBIT_FEATURES;
3098 phy->advertising &= PHY_GBIT_FEATURES;
3099 }
3100
3101 return phy_config(phy);
3102}
3103
3104static int sja1105_port_enable(struct udevice *dev, int port,
3105 struct phy_device *phy)
3106{
3107 struct sja1105_private *priv = dev_get_priv(dev);
3108 phy_interface_t phy_mode = phy->interface;
3109 struct sja1105_xmii_params_entry *mii;
3110 struct sja1105_mac_config_entry *mac;
3111 int rc;
3112
3113 rc = phy_startup(phy);
3114 if (rc)
3115 return rc;
3116
3117 mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
3118 mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
3119
3120 switch (phy_mode) {
3121 case PHY_INTERFACE_MODE_MII:
3122 if (!priv->info->supports_mii[port])
3123 goto unsupported;
3124
3125 mii->xmii_mode[port] = XMII_MODE_MII;
3126 break;
3127 case PHY_INTERFACE_MODE_RMII:
3128 if (!priv->info->supports_rmii[port])
3129 goto unsupported;
3130
3131 mii->xmii_mode[port] = XMII_MODE_RMII;
3132 break;
3133 case PHY_INTERFACE_MODE_RGMII:
3134 case PHY_INTERFACE_MODE_RGMII_ID:
3135 case PHY_INTERFACE_MODE_RGMII_RXID:
3136 case PHY_INTERFACE_MODE_RGMII_TXID:
3137 if (!priv->info->supports_rgmii[port])
3138 goto unsupported;
3139
3140 mii->xmii_mode[port] = XMII_MODE_RGMII;
3141 break;
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03003142 case PHY_INTERFACE_MODE_SGMII:
3143 if (!priv->info->supports_sgmii[port])
3144 goto unsupported;
3145
3146 mii->xmii_mode[port] = XMII_MODE_SGMII;
3147 mii->special[port] = true;
3148 break;
Vladimir Oltean67bfc852021-09-29 18:04:41 +03003149unsupported:
3150 default:
3151 dev_err(dev, "Unsupported PHY mode %d on port %d!\n",
3152 phy_mode, port);
3153 return -EINVAL;
3154 }
3155
3156 /* RevMII, RevRMII not supported */
3157 mii->phy_mac[port] = XMII_MAC;
3158
3159 /* Let the PHY handle the RGMII delays, if present. */
3160 if (phy->phy_id == PHY_FIXED_ID) {
3161 if (phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
3162 phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
3163 priv->rgmii_rx_delay[port] = true;
3164
3165 if (phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
3166 phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
3167 priv->rgmii_tx_delay[port] = true;
3168
3169 if ((priv->rgmii_rx_delay[port] ||
3170 priv->rgmii_tx_delay[port]) &&
3171 !priv->info->setup_rgmii_delay) {
3172 printf("Chip does not support internal RGMII delays\n");
3173 return -EINVAL;
3174 }
3175 }
3176
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03003177 if (mii->xmii_mode[port] == XMII_MODE_SGMII) {
3178 mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
3179 priv->xpcs_cfg[port].speed = phy->speed;
3180 } else if (phy->speed == SPEED_1000) {
Vladimir Oltean67bfc852021-09-29 18:04:41 +03003181 mac[port].speed = priv->info->port_speed[SJA1105_SPEED_1000MBPS];
3182 } else if (phy->speed == SPEED_100) {
3183 mac[port].speed = priv->info->port_speed[SJA1105_SPEED_100MBPS];
3184 } else if (phy->speed == SPEED_10) {
3185 mac[port].speed = priv->info->port_speed[SJA1105_SPEED_10MBPS];
3186 } else {
3187 printf("Invalid PHY speed %d on port %d\n", phy->speed, port);
3188 return -EINVAL;
3189 }
3190
3191 return sja1105_static_config_reload(priv);
3192}
3193
3194static void sja1105_port_disable(struct udevice *dev, int port,
3195 struct phy_device *phy)
3196{
3197 phy_shutdown(phy);
3198}
3199
3200static int sja1105_xmit(struct udevice *dev, int port, void *packet, int length)
3201{
3202 struct sja1105_private *priv = dev_get_priv(dev);
3203 u8 *from = (u8 *)packet + VLAN_HLEN;
3204 struct vlan_ethhdr *hdr = packet;
3205 u8 *dest = (u8 *)packet;
3206
3207 memmove(dest, from, 2 * ETH_ALEN);
3208 hdr->h_vlan_proto = htons(ETH_P_SJA1105);
3209 hdr->h_vlan_TCI = htons(priv->pvid[port]);
3210
3211 return 0;
3212}
3213
3214static int sja1105_rcv(struct udevice *dev, int *port, void *packet, int length)
3215{
3216 struct vlan_ethhdr *hdr = packet;
3217 u8 *dest = packet + VLAN_HLEN;
3218 u8 *from = packet;
3219
3220 if (ntohs(hdr->h_vlan_proto) != ETH_P_SJA1105)
3221 return -EINVAL;
3222
3223 *port = ntohs(hdr->h_vlan_TCI) & DSA_8021Q_PORT_MASK;
3224 memmove(dest, from, 2 * ETH_ALEN);
3225
3226 return 0;
3227}
3228
3229static const struct dsa_ops sja1105_dsa_ops = {
3230 .port_probe = sja1105_port_probe,
3231 .port_enable = sja1105_port_enable,
3232 .port_disable = sja1105_port_disable,
3233 .xmit = sja1105_xmit,
3234 .rcv = sja1105_rcv,
3235};
3236
3237static int sja1105_init(struct sja1105_private *priv)
3238{
3239 int rc;
3240
3241 rc = sja1105_static_config_init(priv);
3242 if (rc) {
3243 printf("Failed to initialize static config: %d\n", rc);
3244 return rc;
3245 }
3246
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03003247 rc = sja1105_mdiobus_register(priv);
3248 if (rc) {
3249 printf("Failed to register MDIO bus: %d\n", rc);
3250 goto err_mdiobus_register;
3251 }
3252
Vladimir Oltean67bfc852021-09-29 18:04:41 +03003253 return 0;
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03003254
3255err_mdiobus_register:
3256 sja1105_static_config_free(&priv->static_config);
3257
3258 return rc;
Vladimir Oltean67bfc852021-09-29 18:04:41 +03003259}
3260
3261static int sja1105_check_device_id(struct sja1105_private *priv)
3262{
3263 const struct sja1105_regs *regs = priv->info->regs;
3264 u8 packed_buf[SJA1105_SIZE_DEVICE_ID] = {0};
3265 enum sja1105_switch_id id;
3266 u64 device_id;
3267 u64 part_no;
3268 int rc;
3269
3270 rc = sja1105_xfer_buf(priv, SPI_READ, regs->device_id, packed_buf,
3271 SJA1105_SIZE_DEVICE_ID);
3272 if (rc < 0)
3273 return rc;
3274
3275 sja1105_packing(packed_buf, &device_id, 31, 0, SJA1105_SIZE_DEVICE_ID,
3276 UNPACK);
3277
Vladimir Oltean67bfc852021-09-29 18:04:41 +03003278 rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, packed_buf,
3279 SJA1105_SIZE_DEVICE_ID);
3280 if (rc < 0)
3281 return rc;
3282
3283 sja1105_packing(packed_buf, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID,
3284 UNPACK);
3285
3286 for (id = 0; id < SJA1105_MAX_SWITCH_ID; id++) {
3287 const struct sja1105_info *info = &sja1105_info[id];
3288
3289 /* Is what's been probed in our match table at all? */
3290 if (info->device_id != device_id || info->part_no != part_no)
3291 continue;
3292
3293 /* But is it what's in the device tree? */
3294 if (priv->info->device_id != device_id ||
3295 priv->info->part_no != part_no) {
3296 printf("Device tree specifies chip %s but found %s, please fix it!\n",
3297 priv->info->name, info->name);
3298 /* It isn't. No problem, pick that up. */
3299 priv->info = info;
3300 }
3301
3302 return 0;
3303 }
3304
3305 printf("Unexpected {device ID, part number}: 0x%llx 0x%llx\n",
3306 device_id, part_no);
3307
3308 return -ENODEV;
3309}
3310
3311static int sja1105_probe(struct udevice *dev)
3312{
3313 enum sja1105_switch_id id = dev_get_driver_data(dev);
3314 struct sja1105_private *priv = dev_get_priv(dev);
3315 int rc;
3316
3317 if (ofnode_valid(dev_ofnode(dev)) &&
Simon Glass2e4938b2022-09-06 20:27:17 -06003318 !ofnode_is_enabled(dev_ofnode(dev))) {
Vladimir Oltean67bfc852021-09-29 18:04:41 +03003319 dev_dbg(dev, "switch disabled\n");
3320 return -ENODEV;
3321 }
3322
3323 priv->info = &sja1105_info[id];
3324 priv->dev = dev;
3325
3326 rc = sja1105_check_device_id(priv);
3327 if (rc < 0) {
3328 dev_err(dev, "Device ID check failed: %d\n", rc);
3329 return rc;
3330 }
3331
3332 dsa_set_tagging(dev, VLAN_HLEN, 0);
3333
3334 return sja1105_init(priv);
3335}
3336
3337static int sja1105_remove(struct udevice *dev)
3338{
3339 struct sja1105_private *priv = dev_get_priv(dev);
3340
Vladimir Oltean6892d6a2021-09-29 18:04:42 +03003341 sja1105_mdiobus_unregister(priv);
Vladimir Oltean67bfc852021-09-29 18:04:41 +03003342 sja1105_static_config_free(&priv->static_config);
3343
3344 return 0;
3345}
3346
3347static const struct udevice_id sja1105_ids[] = {
3348 { .compatible = "nxp,sja1105e", .data = SJA1105E },
3349 { .compatible = "nxp,sja1105t", .data = SJA1105T },
3350 { .compatible = "nxp,sja1105p", .data = SJA1105P },
3351 { .compatible = "nxp,sja1105q", .data = SJA1105Q },
3352 { .compatible = "nxp,sja1105r", .data = SJA1105R },
3353 { .compatible = "nxp,sja1105s", .data = SJA1105S },
3354 { .compatible = "nxp,sja1110a", .data = SJA1110A },
3355 { .compatible = "nxp,sja1110b", .data = SJA1110B },
3356 { .compatible = "nxp,sja1110c", .data = SJA1110C },
3357 { .compatible = "nxp,sja1110d", .data = SJA1110D },
3358 { }
3359};
3360
3361U_BOOT_DRIVER(sja1105) = {
3362 .name = "sja1105",
3363 .id = UCLASS_DSA,
3364 .of_match = sja1105_ids,
3365 .probe = sja1105_probe,
3366 .remove = sja1105_remove,
3367 .ops = &sja1105_dsa_ops,
3368 .priv_auto = sizeof(struct sja1105_private),
3369};