]>
Commit | Line | Data |
---|---|---|
8aa9ebcc VO |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH | |
3 | * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com> | |
4 | */ | |
5 | ||
6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
7 | ||
8 | #include <linux/delay.h> | |
9 | #include <linux/module.h> | |
10 | #include <linux/printk.h> | |
11 | #include <linux/spi/spi.h> | |
12 | #include <linux/errno.h> | |
13 | #include <linux/gpio/consumer.h> | |
14 | #include <linux/of.h> | |
15 | #include <linux/of_net.h> | |
16 | #include <linux/of_mdio.h> | |
17 | #include <linux/of_device.h> | |
18 | #include <linux/netdev_features.h> | |
19 | #include <linux/netdevice.h> | |
20 | #include <linux/if_bridge.h> | |
21 | #include <linux/if_ether.h> | |
22 | #include "sja1105.h" | |
23 | ||
24 | static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len, | |
25 | unsigned int startup_delay) | |
26 | { | |
27 | gpiod_set_value_cansleep(gpio, 1); | |
28 | /* Wait for minimum reset pulse length */ | |
29 | msleep(pulse_len); | |
30 | gpiod_set_value_cansleep(gpio, 0); | |
31 | /* Wait until chip is ready after reset */ | |
32 | msleep(startup_delay); | |
33 | } | |
34 | ||
35 | static void | |
36 | sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd, | |
37 | int from, int to, bool allow) | |
38 | { | |
39 | if (allow) { | |
40 | l2_fwd[from].bc_domain |= BIT(to); | |
41 | l2_fwd[from].reach_port |= BIT(to); | |
42 | l2_fwd[from].fl_domain |= BIT(to); | |
43 | } else { | |
44 | l2_fwd[from].bc_domain &= ~BIT(to); | |
45 | l2_fwd[from].reach_port &= ~BIT(to); | |
46 | l2_fwd[from].fl_domain &= ~BIT(to); | |
47 | } | |
48 | } | |
49 | ||
50 | /* Structure used to temporarily transport device tree | |
51 | * settings into sja1105_setup | |
52 | */ | |
53 | struct sja1105_dt_port { | |
54 | phy_interface_t phy_mode; | |
55 | sja1105_mii_role_t role; | |
56 | }; | |
57 | ||
58 | static int sja1105_init_mac_settings(struct sja1105_private *priv) | |
59 | { | |
60 | struct sja1105_mac_config_entry default_mac = { | |
61 | /* Enable all 8 priority queues on egress. | |
62 | * Every queue i holds top[i] - base[i] frames. | |
63 | * Sum of top[i] - base[i] is 511 (max hardware limit). | |
64 | */ | |
65 | .top = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF}, | |
66 | .base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0}, | |
67 | .enabled = {true, true, true, true, true, true, true, true}, | |
68 | /* Keep standard IFG of 12 bytes on egress. */ | |
69 | .ifg = 0, | |
70 | /* Always put the MAC speed in automatic mode, where it can be | |
71 | * retrieved from the PHY object through phylib and | |
72 | * sja1105_adjust_port_config. | |
73 | */ | |
74 | .speed = SJA1105_SPEED_AUTO, | |
75 | /* No static correction for 1-step 1588 events */ | |
76 | .tp_delin = 0, | |
77 | .tp_delout = 0, | |
78 | /* Disable aging for critical TTEthernet traffic */ | |
79 | .maxage = 0xFF, | |
80 | /* Internal VLAN (pvid) to apply to untagged ingress */ | |
81 | .vlanprio = 0, | |
82 | .vlanid = 0, | |
83 | .ing_mirr = false, | |
84 | .egr_mirr = false, | |
85 | /* Don't drop traffic with other EtherType than ETH_P_IP */ | |
86 | .drpnona664 = false, | |
87 | /* Don't drop double-tagged traffic */ | |
88 | .drpdtag = false, | |
89 | /* Don't drop untagged traffic */ | |
90 | .drpuntag = false, | |
91 | /* Don't retag 802.1p (VID 0) traffic with the pvid */ | |
92 | .retag = false, | |
93 | /* Enable learning and I/O on user ports by default. */ | |
94 | .dyn_learn = true, | |
95 | .egress = false, | |
96 | .ingress = false, | |
97 | }; | |
98 | struct sja1105_mac_config_entry *mac; | |
99 | struct sja1105_table *table; | |
100 | int i; | |
101 | ||
102 | table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG]; | |
103 | ||
104 | /* Discard previous MAC Configuration Table */ | |
105 | if (table->entry_count) { | |
106 | kfree(table->entries); | |
107 | table->entry_count = 0; | |
108 | } | |
109 | ||
110 | table->entries = kcalloc(SJA1105_NUM_PORTS, | |
111 | table->ops->unpacked_entry_size, GFP_KERNEL); | |
112 | if (!table->entries) | |
113 | return -ENOMEM; | |
114 | ||
115 | /* Override table based on phylib DT bindings */ | |
116 | table->entry_count = SJA1105_NUM_PORTS; | |
117 | ||
118 | mac = table->entries; | |
119 | ||
120 | for (i = 0; i < SJA1105_NUM_PORTS; i++) | |
121 | mac[i] = default_mac; | |
122 | ||
123 | return 0; | |
124 | } | |
125 | ||
126 | static int sja1105_init_mii_settings(struct sja1105_private *priv, | |
127 | struct sja1105_dt_port *ports) | |
128 | { | |
129 | struct device *dev = &priv->spidev->dev; | |
130 | struct sja1105_xmii_params_entry *mii; | |
131 | struct sja1105_table *table; | |
132 | int i; | |
133 | ||
134 | table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS]; | |
135 | ||
136 | /* Discard previous xMII Mode Parameters Table */ | |
137 | if (table->entry_count) { | |
138 | kfree(table->entries); | |
139 | table->entry_count = 0; | |
140 | } | |
141 | ||
142 | table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT, | |
143 | table->ops->unpacked_entry_size, GFP_KERNEL); | |
144 | if (!table->entries) | |
145 | return -ENOMEM; | |
146 | ||
147 | /* Override table based on phylib DT bindings */ | |
148 | table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT; | |
149 | ||
150 | mii = table->entries; | |
151 | ||
152 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
153 | switch (ports[i].phy_mode) { | |
154 | case PHY_INTERFACE_MODE_MII: | |
155 | mii->xmii_mode[i] = XMII_MODE_MII; | |
156 | break; | |
157 | case PHY_INTERFACE_MODE_RMII: | |
158 | mii->xmii_mode[i] = XMII_MODE_RMII; | |
159 | break; | |
160 | case PHY_INTERFACE_MODE_RGMII: | |
161 | case PHY_INTERFACE_MODE_RGMII_ID: | |
162 | case PHY_INTERFACE_MODE_RGMII_RXID: | |
163 | case PHY_INTERFACE_MODE_RGMII_TXID: | |
164 | mii->xmii_mode[i] = XMII_MODE_RGMII; | |
165 | break; | |
166 | default: | |
167 | dev_err(dev, "Unsupported PHY mode %s!\n", | |
168 | phy_modes(ports[i].phy_mode)); | |
169 | } | |
170 | ||
171 | mii->phy_mac[i] = ports[i].role; | |
172 | } | |
173 | return 0; | |
174 | } | |
175 | ||
176 | static int sja1105_init_static_fdb(struct sja1105_private *priv) | |
177 | { | |
178 | struct sja1105_table *table; | |
179 | ||
180 | table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; | |
181 | ||
291d1e72 VO |
182 | /* We only populate the FDB table through dynamic |
183 | * L2 Address Lookup entries | |
184 | */ | |
8aa9ebcc VO |
185 | if (table->entry_count) { |
186 | kfree(table->entries); | |
187 | table->entry_count = 0; | |
188 | } | |
189 | return 0; | |
190 | } | |
191 | ||
192 | static int sja1105_init_l2_lookup_params(struct sja1105_private *priv) | |
193 | { | |
194 | struct sja1105_table *table; | |
195 | struct sja1105_l2_lookup_params_entry default_l2_lookup_params = { | |
196 | /* TODO Learned FDB entries are never forgotten */ | |
197 | .maxage = 0, | |
198 | /* All entries within a FDB bin are available for learning */ | |
199 | .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE, | |
200 | /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */ | |
201 | .poly = 0x97, | |
202 | /* This selects between Independent VLAN Learning (IVL) and | |
203 | * Shared VLAN Learning (SVL) | |
204 | */ | |
205 | .shared_learn = false, | |
206 | /* Don't discard management traffic based on ENFPORT - | |
207 | * we don't perform SMAC port enforcement anyway, so | |
208 | * what we are setting here doesn't matter. | |
209 | */ | |
210 | .no_enf_hostprt = false, | |
211 | /* Don't learn SMAC for mac_fltres1 and mac_fltres0. | |
212 | * Maybe correlate with no_linklocal_learn from bridge driver? | |
213 | */ | |
214 | .no_mgmt_learn = true, | |
215 | }; | |
216 | ||
217 | table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; | |
218 | ||
219 | if (table->entry_count) { | |
220 | kfree(table->entries); | |
221 | table->entry_count = 0; | |
222 | } | |
223 | ||
224 | table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT, | |
225 | table->ops->unpacked_entry_size, GFP_KERNEL); | |
226 | if (!table->entries) | |
227 | return -ENOMEM; | |
228 | ||
229 | table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT; | |
230 | ||
231 | /* This table only has a single entry */ | |
232 | ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] = | |
233 | default_l2_lookup_params; | |
234 | ||
235 | return 0; | |
236 | } | |
237 | ||
238 | static int sja1105_init_static_vlan(struct sja1105_private *priv) | |
239 | { | |
240 | struct sja1105_table *table; | |
241 | struct sja1105_vlan_lookup_entry pvid = { | |
242 | .ving_mirr = 0, | |
243 | .vegr_mirr = 0, | |
244 | .vmemb_port = 0, | |
245 | .vlan_bc = 0, | |
246 | .tag_port = 0, | |
247 | .vlanid = 0, | |
248 | }; | |
249 | int i; | |
250 | ||
251 | table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; | |
252 | ||
253 | /* The static VLAN table will only contain the initial pvid of 0. | |
6666cebc VO |
254 | * All other VLANs are to be configured through dynamic entries, |
255 | * and kept in the static configuration table as backing memory. | |
256 | * The pvid of 0 is sufficient to pass traffic while the ports are | |
257 | * standalone and when vlan_filtering is disabled. When filtering | |
258 | * gets enabled, the switchdev core sets up the VLAN ID 1 and sets | |
259 | * it as the new pvid. Actually 'pvid 1' still comes up in 'bridge | |
260 | * vlan' even when vlan_filtering is off, but it has no effect. | |
8aa9ebcc VO |
261 | */ |
262 | if (table->entry_count) { | |
263 | kfree(table->entries); | |
264 | table->entry_count = 0; | |
265 | } | |
266 | ||
267 | table->entries = kcalloc(1, table->ops->unpacked_entry_size, | |
268 | GFP_KERNEL); | |
269 | if (!table->entries) | |
270 | return -ENOMEM; | |
271 | ||
272 | table->entry_count = 1; | |
273 | ||
274 | /* VLAN ID 0: all DT-defined ports are members; no restrictions on | |
275 | * forwarding; always transmit priority-tagged frames as untagged. | |
276 | */ | |
277 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
278 | pvid.vmemb_port |= BIT(i); | |
279 | pvid.vlan_bc |= BIT(i); | |
280 | pvid.tag_port &= ~BIT(i); | |
281 | } | |
282 | ||
283 | ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid; | |
284 | return 0; | |
285 | } | |
286 | ||
287 | static int sja1105_init_l2_forwarding(struct sja1105_private *priv) | |
288 | { | |
289 | struct sja1105_l2_forwarding_entry *l2fwd; | |
290 | struct sja1105_table *table; | |
291 | int i, j; | |
292 | ||
293 | table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING]; | |
294 | ||
295 | if (table->entry_count) { | |
296 | kfree(table->entries); | |
297 | table->entry_count = 0; | |
298 | } | |
299 | ||
300 | table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT, | |
301 | table->ops->unpacked_entry_size, GFP_KERNEL); | |
302 | if (!table->entries) | |
303 | return -ENOMEM; | |
304 | ||
305 | table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT; | |
306 | ||
307 | l2fwd = table->entries; | |
308 | ||
309 | /* First 5 entries define the forwarding rules */ | |
310 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
311 | unsigned int upstream = dsa_upstream_port(priv->ds, i); | |
312 | ||
313 | for (j = 0; j < SJA1105_NUM_TC; j++) | |
314 | l2fwd[i].vlan_pmap[j] = j; | |
315 | ||
316 | if (i == upstream) | |
317 | continue; | |
318 | ||
319 | sja1105_port_allow_traffic(l2fwd, i, upstream, true); | |
320 | sja1105_port_allow_traffic(l2fwd, upstream, i, true); | |
321 | } | |
322 | /* Next 8 entries define VLAN PCP mapping from ingress to egress. | |
323 | * Create a one-to-one mapping. | |
324 | */ | |
325 | for (i = 0; i < SJA1105_NUM_TC; i++) | |
326 | for (j = 0; j < SJA1105_NUM_PORTS; j++) | |
327 | l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i; | |
328 | ||
329 | return 0; | |
330 | } | |
331 | ||
332 | static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv) | |
333 | { | |
334 | struct sja1105_l2_forwarding_params_entry default_l2fwd_params = { | |
335 | /* Disallow dynamic reconfiguration of vlan_pmap */ | |
336 | .max_dynp = 0, | |
337 | /* Use a single memory partition for all ingress queues */ | |
338 | .part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 }, | |
339 | }; | |
340 | struct sja1105_table *table; | |
341 | ||
342 | table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; | |
343 | ||
344 | if (table->entry_count) { | |
345 | kfree(table->entries); | |
346 | table->entry_count = 0; | |
347 | } | |
348 | ||
349 | table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT, | |
350 | table->ops->unpacked_entry_size, GFP_KERNEL); | |
351 | if (!table->entries) | |
352 | return -ENOMEM; | |
353 | ||
354 | table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT; | |
355 | ||
356 | /* This table only has a single entry */ | |
357 | ((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] = | |
358 | default_l2fwd_params; | |
359 | ||
360 | return 0; | |
361 | } | |
362 | ||
363 | static int sja1105_init_general_params(struct sja1105_private *priv) | |
364 | { | |
365 | struct sja1105_general_params_entry default_general_params = { | |
366 | /* Disallow dynamic changing of the mirror port */ | |
367 | .mirr_ptacu = 0, | |
368 | .switchid = priv->ds->index, | |
369 | /* Priority queue for link-local frames trapped to CPU */ | |
370 | .hostprio = 0, | |
371 | .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, | |
372 | .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, | |
373 | .incl_srcpt1 = true, | |
374 | .send_meta1 = false, | |
375 | .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, | |
376 | .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, | |
377 | .incl_srcpt0 = true, | |
378 | .send_meta0 = false, | |
379 | /* The destination for traffic matching mac_fltres1 and | |
380 | * mac_fltres0 on all ports except host_port. Such traffic | |
381 | * receieved on host_port itself would be dropped, except | |
382 | * by installing a temporary 'management route' | |
383 | */ | |
384 | .host_port = dsa_upstream_port(priv->ds, 0), | |
385 | /* Same as host port */ | |
386 | .mirr_port = dsa_upstream_port(priv->ds, 0), | |
387 | /* Link-local traffic received on casc_port will be forwarded | |
388 | * to host_port without embedding the source port and device ID | |
389 | * info in the destination MAC address (presumably because it | |
390 | * is a cascaded port and a downstream SJA switch already did | |
391 | * that). Default to an invalid port (to disable the feature) | |
392 | * and overwrite this if we find any DSA (cascaded) ports. | |
393 | */ | |
394 | .casc_port = SJA1105_NUM_PORTS, | |
395 | /* No TTEthernet */ | |
396 | .vllupformat = 0, | |
397 | .vlmarker = 0, | |
398 | .vlmask = 0, | |
399 | /* Only update correctionField for 1-step PTP (L2 transport) */ | |
400 | .ignore2stf = 0, | |
6666cebc VO |
401 | /* Forcefully disable VLAN filtering by telling |
402 | * the switch that VLAN has a different EtherType. | |
403 | */ | |
404 | .tpid = ETH_P_SJA1105, | |
405 | .tpid2 = ETH_P_SJA1105, | |
8aa9ebcc VO |
406 | }; |
407 | struct sja1105_table *table; | |
408 | int i; | |
409 | ||
410 | for (i = 0; i < SJA1105_NUM_PORTS; i++) | |
411 | if (dsa_is_dsa_port(priv->ds, i)) | |
412 | default_general_params.casc_port = i; | |
413 | ||
414 | table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; | |
415 | ||
416 | if (table->entry_count) { | |
417 | kfree(table->entries); | |
418 | table->entry_count = 0; | |
419 | } | |
420 | ||
421 | table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT, | |
422 | table->ops->unpacked_entry_size, GFP_KERNEL); | |
423 | if (!table->entries) | |
424 | return -ENOMEM; | |
425 | ||
426 | table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT; | |
427 | ||
428 | /* This table only has a single entry */ | |
429 | ((struct sja1105_general_params_entry *)table->entries)[0] = | |
430 | default_general_params; | |
431 | ||
432 | return 0; | |
433 | } | |
434 | ||
435 | #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000) | |
436 | ||
437 | static inline void | |
438 | sja1105_setup_policer(struct sja1105_l2_policing_entry *policing, | |
439 | int index) | |
440 | { | |
441 | policing[index].sharindx = index; | |
442 | policing[index].smax = 65535; /* Burst size in bytes */ | |
443 | policing[index].rate = SJA1105_RATE_MBPS(1000); | |
444 | policing[index].maxlen = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN; | |
445 | policing[index].partition = 0; | |
446 | } | |
447 | ||
448 | static int sja1105_init_l2_policing(struct sja1105_private *priv) | |
449 | { | |
450 | struct sja1105_l2_policing_entry *policing; | |
451 | struct sja1105_table *table; | |
452 | int i, j, k; | |
453 | ||
454 | table = &priv->static_config.tables[BLK_IDX_L2_POLICING]; | |
455 | ||
456 | /* Discard previous L2 Policing Table */ | |
457 | if (table->entry_count) { | |
458 | kfree(table->entries); | |
459 | table->entry_count = 0; | |
460 | } | |
461 | ||
462 | table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT, | |
463 | table->ops->unpacked_entry_size, GFP_KERNEL); | |
464 | if (!table->entries) | |
465 | return -ENOMEM; | |
466 | ||
467 | table->entry_count = SJA1105_MAX_L2_POLICING_COUNT; | |
468 | ||
469 | policing = table->entries; | |
470 | ||
471 | /* k sweeps through all unicast policers (0-39). | |
472 | * bcast sweeps through policers 40-44. | |
473 | */ | |
474 | for (i = 0, k = 0; i < SJA1105_NUM_PORTS; i++) { | |
475 | int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + i; | |
476 | ||
477 | for (j = 0; j < SJA1105_NUM_TC; j++, k++) | |
478 | sja1105_setup_policer(policing, k); | |
479 | ||
480 | /* Set up this port's policer for broadcast traffic */ | |
481 | sja1105_setup_policer(policing, bcast); | |
482 | } | |
483 | return 0; | |
484 | } | |
485 | ||
486 | static int sja1105_static_config_load(struct sja1105_private *priv, | |
487 | struct sja1105_dt_port *ports) | |
488 | { | |
489 | int rc; | |
490 | ||
491 | sja1105_static_config_free(&priv->static_config); | |
492 | rc = sja1105_static_config_init(&priv->static_config, | |
493 | priv->info->static_ops, | |
494 | priv->info->device_id); | |
495 | if (rc) | |
496 | return rc; | |
497 | ||
498 | /* Build static configuration */ | |
499 | rc = sja1105_init_mac_settings(priv); | |
500 | if (rc < 0) | |
501 | return rc; | |
502 | rc = sja1105_init_mii_settings(priv, ports); | |
503 | if (rc < 0) | |
504 | return rc; | |
505 | rc = sja1105_init_static_fdb(priv); | |
506 | if (rc < 0) | |
507 | return rc; | |
508 | rc = sja1105_init_static_vlan(priv); | |
509 | if (rc < 0) | |
510 | return rc; | |
511 | rc = sja1105_init_l2_lookup_params(priv); | |
512 | if (rc < 0) | |
513 | return rc; | |
514 | rc = sja1105_init_l2_forwarding(priv); | |
515 | if (rc < 0) | |
516 | return rc; | |
517 | rc = sja1105_init_l2_forwarding_params(priv); | |
518 | if (rc < 0) | |
519 | return rc; | |
520 | rc = sja1105_init_l2_policing(priv); | |
521 | if (rc < 0) | |
522 | return rc; | |
523 | rc = sja1105_init_general_params(priv); | |
524 | if (rc < 0) | |
525 | return rc; | |
526 | ||
527 | /* Send initial configuration to hardware via SPI */ | |
528 | return sja1105_static_config_upload(priv); | |
529 | } | |
530 | ||
f5b8631c VO |
531 | static int sja1105_parse_rgmii_delays(struct sja1105_private *priv, |
532 | const struct sja1105_dt_port *ports) | |
533 | { | |
534 | int i; | |
535 | ||
536 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
537 | if (ports->role == XMII_MAC) | |
538 | continue; | |
539 | ||
540 | if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_RXID || | |
541 | ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID) | |
542 | priv->rgmii_rx_delay[i] = true; | |
543 | ||
544 | if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_TXID || | |
545 | ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID) | |
546 | priv->rgmii_tx_delay[i] = true; | |
547 | ||
548 | if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) && | |
549 | !priv->info->setup_rgmii_delay) | |
550 | return -EINVAL; | |
551 | } | |
552 | return 0; | |
553 | } | |
554 | ||
8aa9ebcc VO |
555 | static int sja1105_parse_ports_node(struct sja1105_private *priv, |
556 | struct sja1105_dt_port *ports, | |
557 | struct device_node *ports_node) | |
558 | { | |
559 | struct device *dev = &priv->spidev->dev; | |
560 | struct device_node *child; | |
561 | ||
562 | for_each_child_of_node(ports_node, child) { | |
563 | struct device_node *phy_node; | |
564 | int phy_mode; | |
565 | u32 index; | |
566 | ||
567 | /* Get switch port number from DT */ | |
568 | if (of_property_read_u32(child, "reg", &index) < 0) { | |
569 | dev_err(dev, "Port number not defined in device tree " | |
570 | "(property \"reg\")\n"); | |
571 | return -ENODEV; | |
572 | } | |
573 | ||
574 | /* Get PHY mode from DT */ | |
575 | phy_mode = of_get_phy_mode(child); | |
576 | if (phy_mode < 0) { | |
577 | dev_err(dev, "Failed to read phy-mode or " | |
578 | "phy-interface-type property for port %d\n", | |
579 | index); | |
580 | return -ENODEV; | |
581 | } | |
582 | ports[index].phy_mode = phy_mode; | |
583 | ||
584 | phy_node = of_parse_phandle(child, "phy-handle", 0); | |
585 | if (!phy_node) { | |
586 | if (!of_phy_is_fixed_link(child)) { | |
587 | dev_err(dev, "phy-handle or fixed-link " | |
588 | "properties missing!\n"); | |
589 | return -ENODEV; | |
590 | } | |
591 | /* phy-handle is missing, but fixed-link isn't. | |
592 | * So it's a fixed link. Default to PHY role. | |
593 | */ | |
594 | ports[index].role = XMII_PHY; | |
595 | } else { | |
596 | /* phy-handle present => put port in MAC role */ | |
597 | ports[index].role = XMII_MAC; | |
598 | of_node_put(phy_node); | |
599 | } | |
600 | ||
601 | /* The MAC/PHY role can be overridden with explicit bindings */ | |
602 | if (of_property_read_bool(child, "sja1105,role-mac")) | |
603 | ports[index].role = XMII_MAC; | |
604 | else if (of_property_read_bool(child, "sja1105,role-phy")) | |
605 | ports[index].role = XMII_PHY; | |
606 | } | |
607 | ||
608 | return 0; | |
609 | } | |
610 | ||
611 | static int sja1105_parse_dt(struct sja1105_private *priv, | |
612 | struct sja1105_dt_port *ports) | |
613 | { | |
614 | struct device *dev = &priv->spidev->dev; | |
615 | struct device_node *switch_node = dev->of_node; | |
616 | struct device_node *ports_node; | |
617 | int rc; | |
618 | ||
619 | ports_node = of_get_child_by_name(switch_node, "ports"); | |
620 | if (!ports_node) { | |
621 | dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); | |
622 | return -ENODEV; | |
623 | } | |
624 | ||
625 | rc = sja1105_parse_ports_node(priv, ports, ports_node); | |
626 | of_node_put(ports_node); | |
627 | ||
628 | return rc; | |
629 | } | |
630 | ||
631 | /* Convert back and forth MAC speed from Mbps to SJA1105 encoding */ | |
632 | static int sja1105_speed[] = { | |
633 | [SJA1105_SPEED_AUTO] = 0, | |
634 | [SJA1105_SPEED_10MBPS] = 10, | |
635 | [SJA1105_SPEED_100MBPS] = 100, | |
636 | [SJA1105_SPEED_1000MBPS] = 1000, | |
637 | }; | |
638 | ||
639 | static sja1105_speed_t sja1105_get_speed_cfg(unsigned int speed_mbps) | |
640 | { | |
641 | int i; | |
642 | ||
643 | for (i = SJA1105_SPEED_AUTO; i <= SJA1105_SPEED_1000MBPS; i++) | |
644 | if (sja1105_speed[i] == speed_mbps) | |
645 | return i; | |
646 | return -EINVAL; | |
647 | } | |
648 | ||
649 | /* Set link speed and enable/disable traffic I/O in the MAC configuration | |
650 | * for a specific port. | |
651 | * | |
652 | * @speed_mbps: If 0, leave the speed unchanged, else adapt MAC to PHY speed. | |
653 | * @enabled: Manage Rx and Tx settings for this port. Overrides the static | |
654 | * configuration settings. | |
655 | */ | |
656 | static int sja1105_adjust_port_config(struct sja1105_private *priv, int port, | |
657 | int speed_mbps, bool enabled) | |
658 | { | |
659 | struct sja1105_xmii_params_entry *mii; | |
660 | struct sja1105_mac_config_entry *mac; | |
661 | struct device *dev = priv->ds->dev; | |
662 | sja1105_phy_interface_t phy_mode; | |
663 | sja1105_speed_t speed; | |
664 | int rc; | |
665 | ||
666 | mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; | |
667 | mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; | |
668 | ||
669 | speed = sja1105_get_speed_cfg(speed_mbps); | |
670 | if (speed_mbps && speed < 0) { | |
671 | dev_err(dev, "Invalid speed %iMbps\n", speed_mbps); | |
672 | return -EINVAL; | |
673 | } | |
674 | ||
675 | /* If requested, overwrite SJA1105_SPEED_AUTO from the static MAC | |
676 | * configuration table, since this will be used for the clocking setup, | |
677 | * and we no longer need to store it in the static config (already told | |
678 | * hardware we want auto during upload phase). | |
679 | */ | |
680 | if (speed_mbps) | |
681 | mac[port].speed = speed; | |
682 | else | |
683 | mac[port].speed = SJA1105_SPEED_AUTO; | |
684 | ||
685 | /* On P/Q/R/S, one can read from the device via the MAC reconfiguration | |
686 | * tables. On E/T, MAC reconfig tables are not readable, only writable. | |
687 | * We have to *know* what the MAC looks like. For the sake of keeping | |
688 | * the code common, we'll use the static configuration tables as a | |
689 | * reasonable approximation for both E/T and P/Q/R/S. | |
690 | */ | |
691 | mac[port].ingress = enabled; | |
692 | mac[port].egress = enabled; | |
693 | ||
694 | /* Write to the dynamic reconfiguration tables */ | |
695 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, | |
696 | port, &mac[port], true); | |
697 | if (rc < 0) { | |
698 | dev_err(dev, "Failed to write MAC config: %d\n", rc); | |
699 | return rc; | |
700 | } | |
701 | ||
702 | /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at | |
703 | * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and | |
704 | * RMII no change of the clock setup is required. Actually, changing | |
705 | * the clock setup does interrupt the clock signal for a certain time | |
706 | * which causes trouble for all PHYs relying on this signal. | |
707 | */ | |
708 | if (!enabled) | |
709 | return 0; | |
710 | ||
711 | phy_mode = mii->xmii_mode[port]; | |
712 | if (phy_mode != XMII_MODE_RGMII) | |
713 | return 0; | |
714 | ||
715 | return sja1105_clocking_setup_port(priv, port); | |
716 | } | |
717 | ||
718 | static void sja1105_adjust_link(struct dsa_switch *ds, int port, | |
719 | struct phy_device *phydev) | |
720 | { | |
721 | struct sja1105_private *priv = ds->priv; | |
722 | ||
723 | if (!phydev->link) | |
724 | sja1105_adjust_port_config(priv, port, 0, false); | |
725 | else | |
726 | sja1105_adjust_port_config(priv, port, phydev->speed, true); | |
727 | } | |
728 | ||
291d1e72 VO |
729 | /* First-generation switches have a 4-way set associative TCAM that |
730 | * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of | |
731 | * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin). | |
732 | * For the placement of a newly learnt FDB entry, the switch selects the bin | |
733 | * based on a hash function, and the way within that bin incrementally. | |
734 | */ | |
735 | static inline int sja1105et_fdb_index(int bin, int way) | |
736 | { | |
737 | return bin * SJA1105ET_FDB_BIN_SIZE + way; | |
738 | } | |
739 | ||
740 | static int sja1105_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin, | |
741 | const u8 *addr, u16 vid, | |
742 | struct sja1105_l2_lookup_entry *match, | |
743 | int *last_unused) | |
744 | { | |
745 | int way; | |
746 | ||
747 | for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) { | |
748 | struct sja1105_l2_lookup_entry l2_lookup = {0}; | |
749 | int index = sja1105et_fdb_index(bin, way); | |
750 | ||
751 | /* Skip unused entries, optionally marking them | |
752 | * into the return value | |
753 | */ | |
754 | if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, | |
755 | index, &l2_lookup)) { | |
756 | if (last_unused) | |
757 | *last_unused = way; | |
758 | continue; | |
759 | } | |
760 | ||
761 | if (l2_lookup.macaddr == ether_addr_to_u64(addr) && | |
762 | l2_lookup.vlanid == vid) { | |
763 | if (match) | |
764 | *match = l2_lookup; | |
765 | return way; | |
766 | } | |
767 | } | |
768 | /* Return an invalid entry index if not found */ | |
769 | return -1; | |
770 | } | |
771 | ||
772 | static int sja1105_fdb_add(struct dsa_switch *ds, int port, | |
773 | const unsigned char *addr, u16 vid) | |
774 | { | |
775 | struct sja1105_l2_lookup_entry l2_lookup = {0}; | |
776 | struct sja1105_private *priv = ds->priv; | |
777 | struct device *dev = ds->dev; | |
778 | int last_unused = -1; | |
779 | int bin, way; | |
780 | ||
781 | bin = sja1105_fdb_hash(priv, addr, vid); | |
782 | ||
783 | way = sja1105_is_fdb_entry_in_bin(priv, bin, addr, vid, | |
784 | &l2_lookup, &last_unused); | |
785 | if (way >= 0) { | |
786 | /* We have an FDB entry. Is our port in the destination | |
787 | * mask? If yes, we need to do nothing. If not, we need | |
788 | * to rewrite the entry by adding this port to it. | |
789 | */ | |
790 | if (l2_lookup.destports & BIT(port)) | |
791 | return 0; | |
792 | l2_lookup.destports |= BIT(port); | |
793 | } else { | |
794 | int index = sja1105et_fdb_index(bin, way); | |
795 | ||
796 | /* We don't have an FDB entry. We construct a new one and | |
797 | * try to find a place for it within the FDB table. | |
798 | */ | |
799 | l2_lookup.macaddr = ether_addr_to_u64(addr); | |
800 | l2_lookup.destports = BIT(port); | |
801 | l2_lookup.vlanid = vid; | |
802 | ||
803 | if (last_unused >= 0) { | |
804 | way = last_unused; | |
805 | } else { | |
806 | /* Bin is full, need to evict somebody. | |
807 | * Choose victim at random. If you get these messages | |
808 | * often, you may need to consider changing the | |
809 | * distribution function: | |
810 | * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly | |
811 | */ | |
812 | get_random_bytes(&way, sizeof(u8)); | |
813 | way %= SJA1105ET_FDB_BIN_SIZE; | |
814 | dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n", | |
815 | bin, addr, way); | |
816 | /* Evict entry */ | |
817 | sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, | |
818 | index, NULL, false); | |
819 | } | |
820 | } | |
821 | l2_lookup.index = sja1105et_fdb_index(bin, way); | |
822 | ||
823 | return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, | |
824 | l2_lookup.index, &l2_lookup, | |
825 | true); | |
826 | } | |
827 | ||
828 | static int sja1105_fdb_del(struct dsa_switch *ds, int port, | |
829 | const unsigned char *addr, u16 vid) | |
830 | { | |
831 | struct sja1105_l2_lookup_entry l2_lookup = {0}; | |
832 | struct sja1105_private *priv = ds->priv; | |
833 | int index, bin, way; | |
834 | bool keep; | |
835 | ||
836 | bin = sja1105_fdb_hash(priv, addr, vid); | |
837 | way = sja1105_is_fdb_entry_in_bin(priv, bin, addr, vid, | |
838 | &l2_lookup, NULL); | |
839 | if (way < 0) | |
840 | return 0; | |
841 | index = sja1105et_fdb_index(bin, way); | |
842 | ||
843 | /* We have an FDB entry. Is our port in the destination mask? If yes, | |
844 | * we need to remove it. If the resulting port mask becomes empty, we | |
845 | * need to completely evict the FDB entry. | |
846 | * Otherwise we just write it back. | |
847 | */ | |
848 | if (l2_lookup.destports & BIT(port)) | |
849 | l2_lookup.destports &= ~BIT(port); | |
850 | if (l2_lookup.destports) | |
851 | keep = true; | |
852 | else | |
853 | keep = false; | |
854 | ||
855 | return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, | |
856 | index, &l2_lookup, keep); | |
857 | } | |
858 | ||
859 | static int sja1105_fdb_dump(struct dsa_switch *ds, int port, | |
860 | dsa_fdb_dump_cb_t *cb, void *data) | |
861 | { | |
862 | struct sja1105_private *priv = ds->priv; | |
863 | struct device *dev = ds->dev; | |
864 | int i; | |
865 | ||
866 | for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { | |
867 | struct sja1105_l2_lookup_entry l2_lookup = {0}; | |
868 | u8 macaddr[ETH_ALEN]; | |
869 | int rc; | |
870 | ||
871 | rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, | |
872 | i, &l2_lookup); | |
873 | /* No fdb entry at i, not an issue */ | |
874 | if (rc == -EINVAL) | |
875 | continue; | |
876 | if (rc) { | |
877 | dev_err(dev, "Failed to dump FDB: %d\n", rc); | |
878 | return rc; | |
879 | } | |
880 | ||
881 | /* FDB dump callback is per port. This means we have to | |
882 | * disregard a valid entry if it's not for this port, even if | |
883 | * only to revisit it later. This is inefficient because the | |
884 | * 1024-sized FDB table needs to be traversed 4 times through | |
885 | * SPI during a 'bridge fdb show' command. | |
886 | */ | |
887 | if (!(l2_lookup.destports & BIT(port))) | |
888 | continue; | |
889 | u64_to_ether_addr(l2_lookup.macaddr, macaddr); | |
890 | cb(macaddr, l2_lookup.vlanid, false, data); | |
891 | } | |
892 | return 0; | |
893 | } | |
894 | ||
895 | /* This callback needs to be present */ | |
896 | static int sja1105_mdb_prepare(struct dsa_switch *ds, int port, | |
897 | const struct switchdev_obj_port_mdb *mdb) | |
898 | { | |
899 | return 0; | |
900 | } | |
901 | ||
902 | static void sja1105_mdb_add(struct dsa_switch *ds, int port, | |
903 | const struct switchdev_obj_port_mdb *mdb) | |
904 | { | |
905 | sja1105_fdb_add(ds, port, mdb->addr, mdb->vid); | |
906 | } | |
907 | ||
908 | static int sja1105_mdb_del(struct dsa_switch *ds, int port, | |
909 | const struct switchdev_obj_port_mdb *mdb) | |
910 | { | |
911 | return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid); | |
912 | } | |
913 | ||
8aa9ebcc VO |
914 | static int sja1105_bridge_member(struct dsa_switch *ds, int port, |
915 | struct net_device *br, bool member) | |
916 | { | |
917 | struct sja1105_l2_forwarding_entry *l2_fwd; | |
918 | struct sja1105_private *priv = ds->priv; | |
919 | int i, rc; | |
920 | ||
921 | l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; | |
922 | ||
923 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
924 | /* Add this port to the forwarding matrix of the | |
925 | * other ports in the same bridge, and viceversa. | |
926 | */ | |
927 | if (!dsa_is_user_port(ds, i)) | |
928 | continue; | |
929 | /* For the ports already under the bridge, only one thing needs | |
930 | * to be done, and that is to add this port to their | |
931 | * reachability domain. So we can perform the SPI write for | |
932 | * them immediately. However, for this port itself (the one | |
933 | * that is new to the bridge), we need to add all other ports | |
934 | * to its reachability domain. So we do that incrementally in | |
935 | * this loop, and perform the SPI write only at the end, once | |
936 | * the domain contains all other bridge ports. | |
937 | */ | |
938 | if (i == port) | |
939 | continue; | |
940 | if (dsa_to_port(ds, i)->bridge_dev != br) | |
941 | continue; | |
942 | sja1105_port_allow_traffic(l2_fwd, i, port, member); | |
943 | sja1105_port_allow_traffic(l2_fwd, port, i, member); | |
944 | ||
945 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, | |
946 | i, &l2_fwd[i], true); | |
947 | if (rc < 0) | |
948 | return rc; | |
949 | } | |
950 | ||
951 | return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, | |
952 | port, &l2_fwd[port], true); | |
953 | } | |
954 | ||
955 | static int sja1105_bridge_join(struct dsa_switch *ds, int port, | |
956 | struct net_device *br) | |
957 | { | |
958 | return sja1105_bridge_member(ds, port, br, true); | |
959 | } | |
960 | ||
961 | static void sja1105_bridge_leave(struct dsa_switch *ds, int port, | |
962 | struct net_device *br) | |
963 | { | |
964 | sja1105_bridge_member(ds, port, br, false); | |
965 | } | |
966 | ||
6666cebc VO |
967 | /* For situations where we need to change a setting at runtime that is only |
968 | * available through the static configuration, resetting the switch in order | |
969 | * to upload the new static config is unavoidable. Back up the settings we | |
970 | * modify at runtime (currently only MAC) and restore them after uploading, | |
971 | * such that this operation is relatively seamless. | |
972 | */ | |
973 | static int sja1105_static_config_reload(struct sja1105_private *priv) | |
974 | { | |
975 | struct sja1105_mac_config_entry *mac; | |
976 | int speed_mbps[SJA1105_NUM_PORTS]; | |
977 | int rc, i; | |
978 | ||
979 | mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; | |
980 | ||
981 | /* Back up settings changed by sja1105_adjust_port_config and | |
982 | * and restore their defaults. | |
983 | */ | |
984 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
985 | speed_mbps[i] = sja1105_speed[mac[i].speed]; | |
986 | mac[i].speed = SJA1105_SPEED_AUTO; | |
987 | } | |
988 | ||
989 | /* Reset switch and send updated static configuration */ | |
990 | rc = sja1105_static_config_upload(priv); | |
991 | if (rc < 0) | |
992 | goto out; | |
993 | ||
994 | /* Configure the CGU (PLLs) for MII and RMII PHYs. | |
995 | * For these interfaces there is no dynamic configuration | |
996 | * needed, since PLLs have same settings at all speeds. | |
997 | */ | |
998 | rc = sja1105_clocking_setup(priv); | |
999 | if (rc < 0) | |
1000 | goto out; | |
1001 | ||
1002 | for (i = 0; i < SJA1105_NUM_PORTS; i++) { | |
1003 | bool enabled = (speed_mbps[i] != 0); | |
1004 | ||
1005 | rc = sja1105_adjust_port_config(priv, i, speed_mbps[i], | |
1006 | enabled); | |
1007 | if (rc < 0) | |
1008 | goto out; | |
1009 | } | |
1010 | out: | |
1011 | return rc; | |
1012 | } | |
1013 | ||
1014 | /* The TPID setting belongs to the General Parameters table, | |
1015 | * which can only be partially reconfigured at runtime (and not the TPID). | |
1016 | * So a switch reset is required. | |
1017 | */ | |
1018 | static int sja1105_change_tpid(struct sja1105_private *priv, | |
1019 | u16 tpid, u16 tpid2) | |
1020 | { | |
1021 | struct sja1105_general_params_entry *general_params; | |
1022 | struct sja1105_table *table; | |
1023 | ||
1024 | table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; | |
1025 | general_params = table->entries; | |
1026 | general_params->tpid = tpid; | |
1027 | general_params->tpid2 = tpid2; | |
1028 | return sja1105_static_config_reload(priv); | |
1029 | } | |
1030 | ||
1031 | static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid) | |
1032 | { | |
1033 | struct sja1105_mac_config_entry *mac; | |
1034 | ||
1035 | mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; | |
1036 | ||
1037 | mac[port].vlanid = pvid; | |
1038 | ||
1039 | return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, | |
1040 | &mac[port], true); | |
1041 | } | |
1042 | ||
1043 | static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid) | |
1044 | { | |
1045 | struct sja1105_vlan_lookup_entry *vlan; | |
1046 | int count, i; | |
1047 | ||
1048 | vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; | |
1049 | count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count; | |
1050 | ||
1051 | for (i = 0; i < count; i++) | |
1052 | if (vlan[i].vlanid == vid) | |
1053 | return i; | |
1054 | ||
1055 | /* Return an invalid entry index if not found */ | |
1056 | return -1; | |
1057 | } | |
1058 | ||
1059 | static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid, | |
1060 | bool enabled, bool untagged) | |
1061 | { | |
1062 | struct sja1105_vlan_lookup_entry *vlan; | |
1063 | struct sja1105_table *table; | |
1064 | bool keep = true; | |
1065 | int match, rc; | |
1066 | ||
1067 | table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; | |
1068 | ||
1069 | match = sja1105_is_vlan_configured(priv, vid); | |
1070 | if (match < 0) { | |
1071 | /* Can't delete a missing entry. */ | |
1072 | if (!enabled) | |
1073 | return 0; | |
1074 | rc = sja1105_table_resize(table, table->entry_count + 1); | |
1075 | if (rc) | |
1076 | return rc; | |
1077 | match = table->entry_count - 1; | |
1078 | } | |
1079 | /* Assign pointer after the resize (it's new memory) */ | |
1080 | vlan = table->entries; | |
1081 | vlan[match].vlanid = vid; | |
1082 | if (enabled) { | |
1083 | vlan[match].vlan_bc |= BIT(port); | |
1084 | vlan[match].vmemb_port |= BIT(port); | |
1085 | } else { | |
1086 | vlan[match].vlan_bc &= ~BIT(port); | |
1087 | vlan[match].vmemb_port &= ~BIT(port); | |
1088 | } | |
1089 | /* Also unset tag_port if removing this VLAN was requested, | |
1090 | * just so we don't have a confusing bitmap (no practical purpose). | |
1091 | */ | |
1092 | if (untagged || !enabled) | |
1093 | vlan[match].tag_port &= ~BIT(port); | |
1094 | else | |
1095 | vlan[match].tag_port |= BIT(port); | |
1096 | /* If there's no port left as member of this VLAN, | |
1097 | * it's time for it to go. | |
1098 | */ | |
1099 | if (!vlan[match].vmemb_port) | |
1100 | keep = false; | |
1101 | ||
1102 | dev_dbg(priv->ds->dev, | |
1103 | "%s: port %d, vid %llu, broadcast domain 0x%llx, " | |
1104 | "port members 0x%llx, tagged ports 0x%llx, keep %d\n", | |
1105 | __func__, port, vlan[match].vlanid, vlan[match].vlan_bc, | |
1106 | vlan[match].vmemb_port, vlan[match].tag_port, keep); | |
1107 | ||
1108 | rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid, | |
1109 | &vlan[match], keep); | |
1110 | if (rc < 0) | |
1111 | return rc; | |
1112 | ||
1113 | if (!keep) | |
1114 | return sja1105_table_delete_entry(table, match); | |
1115 | ||
1116 | return 0; | |
1117 | } | |
1118 | ||
8aa9ebcc VO |
1119 | static enum dsa_tag_protocol |
1120 | sja1105_get_tag_protocol(struct dsa_switch *ds, int port) | |
1121 | { | |
1122 | return DSA_TAG_PROTO_NONE; | |
1123 | } | |
1124 | ||
6666cebc VO |
1125 | /* This callback needs to be present */ |
1126 | static int sja1105_vlan_prepare(struct dsa_switch *ds, int port, | |
1127 | const struct switchdev_obj_port_vlan *vlan) | |
1128 | { | |
1129 | return 0; | |
1130 | } | |
1131 | ||
1132 | static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled) | |
1133 | { | |
1134 | struct sja1105_private *priv = ds->priv; | |
1135 | int rc; | |
1136 | ||
1137 | if (enabled) | |
1138 | /* Enable VLAN filtering. */ | |
1139 | rc = sja1105_change_tpid(priv, ETH_P_8021Q, ETH_P_8021AD); | |
1140 | else | |
1141 | /* Disable VLAN filtering. */ | |
1142 | rc = sja1105_change_tpid(priv, ETH_P_SJA1105, ETH_P_SJA1105); | |
1143 | if (rc) | |
1144 | dev_err(ds->dev, "Failed to change VLAN Ethertype\n"); | |
1145 | ||
1146 | return rc; | |
1147 | } | |
1148 | ||
1149 | static void sja1105_vlan_add(struct dsa_switch *ds, int port, | |
1150 | const struct switchdev_obj_port_vlan *vlan) | |
1151 | { | |
1152 | struct sja1105_private *priv = ds->priv; | |
1153 | u16 vid; | |
1154 | int rc; | |
1155 | ||
1156 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { | |
1157 | rc = sja1105_vlan_apply(priv, port, vid, true, vlan->flags & | |
1158 | BRIDGE_VLAN_INFO_UNTAGGED); | |
1159 | if (rc < 0) { | |
1160 | dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n", | |
1161 | vid, port, rc); | |
1162 | return; | |
1163 | } | |
1164 | if (vlan->flags & BRIDGE_VLAN_INFO_PVID) { | |
1165 | rc = sja1105_pvid_apply(ds->priv, port, vid); | |
1166 | if (rc < 0) { | |
1167 | dev_err(ds->dev, "Failed to set pvid %d on port %d: %d\n", | |
1168 | vid, port, rc); | |
1169 | return; | |
1170 | } | |
1171 | } | |
1172 | } | |
1173 | } | |
1174 | ||
1175 | static int sja1105_vlan_del(struct dsa_switch *ds, int port, | |
1176 | const struct switchdev_obj_port_vlan *vlan) | |
1177 | { | |
1178 | struct sja1105_private *priv = ds->priv; | |
1179 | u16 vid; | |
1180 | int rc; | |
1181 | ||
1182 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { | |
1183 | rc = sja1105_vlan_apply(priv, port, vid, false, vlan->flags & | |
1184 | BRIDGE_VLAN_INFO_UNTAGGED); | |
1185 | if (rc < 0) { | |
1186 | dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n", | |
1187 | vid, port, rc); | |
1188 | return rc; | |
1189 | } | |
1190 | } | |
1191 | return 0; | |
1192 | } | |
1193 | ||
8aa9ebcc VO |
1194 | /* The programming model for the SJA1105 switch is "all-at-once" via static |
1195 | * configuration tables. Some of these can be dynamically modified at runtime, | |
1196 | * but not the xMII mode parameters table. | |
1197 | * Furthermode, some PHYs may not have crystals for generating their clocks | |
1198 | * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's | |
1199 | * ref_clk pin. So port clocking needs to be initialized early, before | |
1200 | * connecting to PHYs is attempted, otherwise they won't respond through MDIO. | |
1201 | * Setting correct PHY link speed does not matter now. | |
1202 | * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY | |
1203 | * bindings are not yet parsed by DSA core. We need to parse early so that we | |
1204 | * can populate the xMII mode parameters table. | |
1205 | */ | |
1206 | static int sja1105_setup(struct dsa_switch *ds) | |
1207 | { | |
1208 | struct sja1105_dt_port ports[SJA1105_NUM_PORTS]; | |
1209 | struct sja1105_private *priv = ds->priv; | |
1210 | int rc; | |
1211 | ||
1212 | rc = sja1105_parse_dt(priv, ports); | |
1213 | if (rc < 0) { | |
1214 | dev_err(ds->dev, "Failed to parse DT: %d\n", rc); | |
1215 | return rc; | |
1216 | } | |
f5b8631c VO |
1217 | |
1218 | /* Error out early if internal delays are required through DT | |
1219 | * and we can't apply them. | |
1220 | */ | |
1221 | rc = sja1105_parse_rgmii_delays(priv, ports); | |
1222 | if (rc < 0) { | |
1223 | dev_err(ds->dev, "RGMII delay not supported\n"); | |
1224 | return rc; | |
1225 | } | |
1226 | ||
8aa9ebcc VO |
1227 | /* Create and send configuration down to device */ |
1228 | rc = sja1105_static_config_load(priv, ports); | |
1229 | if (rc < 0) { | |
1230 | dev_err(ds->dev, "Failed to load static config: %d\n", rc); | |
1231 | return rc; | |
1232 | } | |
1233 | /* Configure the CGU (PHY link modes and speeds) */ | |
1234 | rc = sja1105_clocking_setup(priv); | |
1235 | if (rc < 0) { | |
1236 | dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc); | |
1237 | return rc; | |
1238 | } | |
6666cebc VO |
1239 | /* On SJA1105, VLAN filtering per se is always enabled in hardware. |
1240 | * The only thing we can do to disable it is lie about what the 802.1Q | |
1241 | * EtherType is. | |
1242 | * So it will still try to apply VLAN filtering, but all ingress | |
1243 | * traffic (except frames received with EtherType of ETH_P_SJA1105) | |
1244 | * will be internally tagged with a distorted VLAN header where the | |
1245 | * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid. | |
1246 | */ | |
1247 | ds->vlan_filtering_is_global = true; | |
8aa9ebcc VO |
1248 | |
1249 | return 0; | |
1250 | } | |
1251 | ||
1252 | static const struct dsa_switch_ops sja1105_switch_ops = { | |
1253 | .get_tag_protocol = sja1105_get_tag_protocol, | |
1254 | .setup = sja1105_setup, | |
1255 | .adjust_link = sja1105_adjust_link, | |
52c34e6e VO |
1256 | .get_strings = sja1105_get_strings, |
1257 | .get_ethtool_stats = sja1105_get_ethtool_stats, | |
1258 | .get_sset_count = sja1105_get_sset_count, | |
291d1e72 VO |
1259 | .port_fdb_dump = sja1105_fdb_dump, |
1260 | .port_fdb_add = sja1105_fdb_add, | |
1261 | .port_fdb_del = sja1105_fdb_del, | |
8aa9ebcc VO |
1262 | .port_bridge_join = sja1105_bridge_join, |
1263 | .port_bridge_leave = sja1105_bridge_leave, | |
6666cebc VO |
1264 | .port_vlan_prepare = sja1105_vlan_prepare, |
1265 | .port_vlan_filtering = sja1105_vlan_filtering, | |
1266 | .port_vlan_add = sja1105_vlan_add, | |
1267 | .port_vlan_del = sja1105_vlan_del, | |
291d1e72 VO |
1268 | .port_mdb_prepare = sja1105_mdb_prepare, |
1269 | .port_mdb_add = sja1105_mdb_add, | |
1270 | .port_mdb_del = sja1105_mdb_del, | |
8aa9ebcc VO |
1271 | }; |
1272 | ||
1273 | static int sja1105_check_device_id(struct sja1105_private *priv) | |
1274 | { | |
1275 | const struct sja1105_regs *regs = priv->info->regs; | |
1276 | u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0}; | |
1277 | struct device *dev = &priv->spidev->dev; | |
1278 | u64 device_id; | |
1279 | u64 part_no; | |
1280 | int rc; | |
1281 | ||
1282 | rc = sja1105_spi_send_int(priv, SPI_READ, regs->device_id, | |
1283 | &device_id, SJA1105_SIZE_DEVICE_ID); | |
1284 | if (rc < 0) | |
1285 | return rc; | |
1286 | ||
1287 | if (device_id != priv->info->device_id) { | |
1288 | dev_err(dev, "Expected device ID 0x%llx but read 0x%llx\n", | |
1289 | priv->info->device_id, device_id); | |
1290 | return -ENODEV; | |
1291 | } | |
1292 | ||
1293 | rc = sja1105_spi_send_packed_buf(priv, SPI_READ, regs->prod_id, | |
1294 | prod_id, SJA1105_SIZE_DEVICE_ID); | |
1295 | if (rc < 0) | |
1296 | return rc; | |
1297 | ||
1298 | sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID); | |
1299 | ||
1300 | if (part_no != priv->info->part_no) { | |
1301 | dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n", | |
1302 | priv->info->part_no, part_no); | |
1303 | return -ENODEV; | |
1304 | } | |
1305 | ||
1306 | return 0; | |
1307 | } | |
1308 | ||
1309 | static int sja1105_probe(struct spi_device *spi) | |
1310 | { | |
1311 | struct device *dev = &spi->dev; | |
1312 | struct sja1105_private *priv; | |
1313 | struct dsa_switch *ds; | |
1314 | int rc; | |
1315 | ||
1316 | if (!dev->of_node) { | |
1317 | dev_err(dev, "No DTS bindings for SJA1105 driver\n"); | |
1318 | return -EINVAL; | |
1319 | } | |
1320 | ||
1321 | priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL); | |
1322 | if (!priv) | |
1323 | return -ENOMEM; | |
1324 | ||
1325 | /* Configure the optional reset pin and bring up switch */ | |
1326 | priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); | |
1327 | if (IS_ERR(priv->reset_gpio)) | |
1328 | dev_dbg(dev, "reset-gpios not defined, ignoring\n"); | |
1329 | else | |
1330 | sja1105_hw_reset(priv->reset_gpio, 1, 1); | |
1331 | ||
1332 | /* Populate our driver private structure (priv) based on | |
1333 | * the device tree node that was probed (spi) | |
1334 | */ | |
1335 | priv->spidev = spi; | |
1336 | spi_set_drvdata(spi, priv); | |
1337 | ||
1338 | /* Configure the SPI bus */ | |
1339 | spi->bits_per_word = 8; | |
1340 | rc = spi_setup(spi); | |
1341 | if (rc < 0) { | |
1342 | dev_err(dev, "Could not init SPI\n"); | |
1343 | return rc; | |
1344 | } | |
1345 | ||
1346 | priv->info = of_device_get_match_data(dev); | |
1347 | ||
1348 | /* Detect hardware device */ | |
1349 | rc = sja1105_check_device_id(priv); | |
1350 | if (rc < 0) { | |
1351 | dev_err(dev, "Device ID check failed: %d\n", rc); | |
1352 | return rc; | |
1353 | } | |
1354 | ||
1355 | dev_info(dev, "Probed switch chip: %s\n", priv->info->name); | |
1356 | ||
1357 | ds = dsa_switch_alloc(dev, SJA1105_NUM_PORTS); | |
1358 | if (!ds) | |
1359 | return -ENOMEM; | |
1360 | ||
1361 | ds->ops = &sja1105_switch_ops; | |
1362 | ds->priv = priv; | |
1363 | priv->ds = ds; | |
1364 | ||
1365 | return dsa_register_switch(priv->ds); | |
1366 | } | |
1367 | ||
1368 | static int sja1105_remove(struct spi_device *spi) | |
1369 | { | |
1370 | struct sja1105_private *priv = spi_get_drvdata(spi); | |
1371 | ||
1372 | dsa_unregister_switch(priv->ds); | |
1373 | sja1105_static_config_free(&priv->static_config); | |
1374 | return 0; | |
1375 | } | |
1376 | ||
1377 | static const struct of_device_id sja1105_dt_ids[] = { | |
1378 | { .compatible = "nxp,sja1105e", .data = &sja1105e_info }, | |
1379 | { .compatible = "nxp,sja1105t", .data = &sja1105t_info }, | |
1380 | { .compatible = "nxp,sja1105p", .data = &sja1105p_info }, | |
1381 | { .compatible = "nxp,sja1105q", .data = &sja1105q_info }, | |
1382 | { .compatible = "nxp,sja1105r", .data = &sja1105r_info }, | |
1383 | { .compatible = "nxp,sja1105s", .data = &sja1105s_info }, | |
1384 | { /* sentinel */ }, | |
1385 | }; | |
1386 | MODULE_DEVICE_TABLE(of, sja1105_dt_ids); | |
1387 | ||
1388 | static struct spi_driver sja1105_driver = { | |
1389 | .driver = { | |
1390 | .name = "sja1105", | |
1391 | .owner = THIS_MODULE, | |
1392 | .of_match_table = of_match_ptr(sja1105_dt_ids), | |
1393 | }, | |
1394 | .probe = sja1105_probe, | |
1395 | .remove = sja1105_remove, | |
1396 | }; | |
1397 | ||
1398 | module_spi_driver(sja1105_driver); | |
1399 | ||
1400 | MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); | |
1401 | MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>"); | |
1402 | MODULE_DESCRIPTION("SJA1105 Driver"); | |
1403 | MODULE_LICENSE("GPL v2"); |