blob: 3177dfb6e5aef1464d8ffd0f5f4655223a8ff9b1 [file] [log] [blame]
Aaron Williams3ec0aa92020-12-11 17:05:59 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020 Marvell International Ltd.
4 *
5 * FDT Helper functions similar to those provided to U-Boot.
6 */
7
8#include <log.h>
9#include <malloc.h>
10#include <net.h>
11#include <linux/delay.h>
12
13#include <mach/cvmx-regs.h>
14#include <mach/cvmx-csr.h>
15#include <mach/cvmx-bootmem.h>
16#include <mach/octeon-model.h>
17#include <mach/octeon_fdt.h>
18#include <mach/cvmx-helper.h>
19#include <mach/cvmx-helper-board.h>
20#include <mach/cvmx-helper-cfg.h>
21#include <mach/cvmx-helper-fdt.h>
22#include <mach/cvmx-helper-gpio.h>
23
24/** Structure used to get type of GPIO from device tree */
25struct gpio_compat {
26 char *compatible; /** Compatible string */
27 enum cvmx_gpio_type type; /** Type */
28 int8_t size; /** (max) Number of pins */
29};
30
31#define GPIO_REG_PCA953X_IN 0
32#define GPIO_REG_PCA953X_OUT 1
33#define GPIO_REG_PCA953X_INVERT 2
34#define GPIO_REG_PCA953X_DIR 3
35
36#define GPIO_REG_PCA957X_IN 0
37#define GPIO_REG_PCA957X_INVERT 1
38#define GPIO_REG_PCA957X_CFG 4
39#define GPIO_REG_PCA957X_OUT 5
40
41enum cvmx_i2c_mux_type { I2C_MUX, I2C_SWITCH };
42
43/** Structure used to get type of GPIO from device tree */
44struct mux_compat {
45 char *compatible; /** Compatible string */
46 enum cvmx_i2c_bus_type type; /** Mux chip type */
47 enum cvmx_i2c_mux_type mux_type; /** Type of mux */
48 u8 enable; /** Enable bit for mux */
49 u8 size; /** (max) Number of channels */
50};
51
52/**
53 * Local allocator to handle both SE and U-Boot that also zeroes out memory
54 *
55 * @param size number of bytes to allocate
56 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010057 * Return: pointer to allocated memory or NULL if out of memory.
Aaron Williams3ec0aa92020-12-11 17:05:59 +010058 * Alignment is set to 8-bytes.
59 */
60void *__cvmx_fdt_alloc(size_t size)
61{
62 return calloc(size, 1);
63}
64
65/**
66 * Free allocated memory.
67 *
68 * @param ptr pointer to memory to free
69 *
70 * NOTE: This only works in U-Boot since SE does not really have a freeing
71 * mechanism. In SE the memory is zeroed out.
72 */
73void __cvmx_fdt_free(void *ptr, size_t size)
74{
75 free(ptr);
76}
77
78/**
79 * Look up a phandle and follow it to its node then return the offset of that
80 * node.
81 *
82 * @param[in] fdt_addr pointer to FDT blob
83 * @param node node to read phandle from
84 * @param[in] prop_name name of property to find
85 * @param[in,out] lenp Number of phandles, input max number
86 * @param[out] nodes Array of phandle nodes
87 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010088 * Return: -ve error code on error or 0 for success
Aaron Williams3ec0aa92020-12-11 17:05:59 +010089 */
90int cvmx_fdt_lookup_phandles(const void *fdt_addr, int node,
91 const char *prop_name, int *lenp,
92 int *nodes)
93{
94 const u32 *phandles;
95 int count;
96 int i;
97
98 phandles = fdt_getprop(fdt_addr, node, prop_name, &count);
99 if (!phandles || count < 0)
100 return -FDT_ERR_NOTFOUND;
101
102 count /= 4;
103 if (count > *lenp)
104 count = *lenp;
105
106 for (i = 0; i < count; i++)
107 nodes[i] = fdt_node_offset_by_phandle(fdt_addr,
108 fdt32_to_cpu(phandles[i]));
109 *lenp = count;
110 return 0;
111}
112
113/**
114 * Given a FDT node return the CPU node number
115 *
116 * @param[in] fdt_addr Address of FDT
117 * @param node FDT node number
118 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100119 * Return: CPU node number or error if negative
Aaron Williams3ec0aa92020-12-11 17:05:59 +0100120 */
121int cvmx_fdt_get_cpu_node(const void *fdt_addr, int node)
122{
123 int parent = node;
124 const u32 *ranges;
125 int len = 0;
126
127 while (fdt_node_check_compatible(fdt_addr, parent, "simple-bus") != 0) {
128 parent = fdt_parent_offset(fdt_addr, parent);
129 if (parent < 0)
130 return parent;
131 }
132 ranges = fdt_getprop(fdt_addr, parent, "ranges", &len);
133 if (!ranges)
134 return len;
135
136 if (len == 0)
137 return 0;
138
139 if (len < 24)
140 return -FDT_ERR_TRUNCATED;
141
142 return fdt32_to_cpu(ranges[2]) / 0x10;
143}
144
145/**
146 * Get the total size of the flat device tree
147 *
148 * @param[in] fdt_addr Address of FDT
149 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100150 * Return: Size of flat device tree in bytes or error if negative.
Aaron Williams3ec0aa92020-12-11 17:05:59 +0100151 */
152int cvmx_fdt_get_fdt_size(const void *fdt_addr)
153{
154 int rc;
155
156 rc = fdt_check_header(fdt_addr);
157 if (rc)
158 return rc;
159 return fdt_totalsize(fdt_addr);
160}
161
162/**
163 * Returns if a node is compatible with one of the items in the string list
164 *
165 * @param[in] fdt_addr Pointer to flat device tree
166 * @param node Node offset to check
167 * @param[in] strlist Array of FDT device compatibility strings,
168 * must end with NULL or empty string.
169 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100170 * Return: 0 if at least one item matches, 1 if no matches
Aaron Williams3ec0aa92020-12-11 17:05:59 +0100171 */
172int cvmx_fdt_node_check_compatible_list(const void *fdt_addr, int node, const char *const *strlist)
173{
174 while (*strlist && **strlist) {
175 if (!fdt_node_check_compatible(fdt_addr, node, *strlist))
176 return 0;
177 strlist++;
178 }
179 return 1;
180}
181
182/**
183 * Given a FDT node, return the next compatible node.
184 *
185 * @param[in] fdt_addr Pointer to flat device tree
186 * @param start_offset Starting node offset or -1 to find the first
187 * @param strlist Array of FDT device compatibility strings, must
188 * end with NULL or empty string.
189 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100190 * Return: next matching node or -1 if no more matches.
Aaron Williams3ec0aa92020-12-11 17:05:59 +0100191 */
192int cvmx_fdt_node_offset_by_compatible_list(const void *fdt_addr, int startoffset,
193 const char *const *strlist)
194{
195 int offset;
196
197 for (offset = fdt_next_node(fdt_addr, startoffset, NULL); offset >= 0;
198 offset = fdt_next_node(fdt_addr, offset, NULL)) {
199 if (!cvmx_fdt_node_check_compatible_list(fdt_addr, offset, strlist))
200 return offset;
201 }
202 return -1;
203}
204
205/**
206 * Attaches a PHY to a SFP or QSFP.
207 *
208 * @param sfp sfp to attach PHY to
209 * @param phy_info phy descriptor to attach or NULL to detach
210 */
211void cvmx_sfp_attach_phy(struct cvmx_fdt_sfp_info *sfp, struct cvmx_phy_info *phy_info)
212{
213 sfp->phy_info = phy_info;
214 if (phy_info)
215 phy_info->sfp_info = sfp;
216}
217
218/**
219 * Assigns an IPD port to a SFP slot
220 *
221 * @param sfp Handle to SFP data structure
222 * @param ipd_port Port to assign it to
223 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100224 * Return: 0 for success, -1 on error
Aaron Williams3ec0aa92020-12-11 17:05:59 +0100225 */
226int cvmx_sfp_set_ipd_port(struct cvmx_fdt_sfp_info *sfp, int ipd_port)
227{
228 int i;
229
230 if (sfp->is_qsfp) {
231 int xiface;
232 cvmx_helper_interface_mode_t mode;
233
234 xiface = cvmx_helper_get_interface_num(ipd_port);
235 mode = cvmx_helper_interface_get_mode(xiface);
236 sfp->ipd_port[0] = ipd_port;
237
238 switch (mode) {
239 case CVMX_HELPER_INTERFACE_MODE_SGMII:
240 case CVMX_HELPER_INTERFACE_MODE_XFI:
241 case CVMX_HELPER_INTERFACE_MODE_10G_KR:
242 for (i = 1; i < 4; i++)
243 sfp->ipd_port[i] = cvmx_helper_get_ipd_port(xiface, i);
244 break;
245 case CVMX_HELPER_INTERFACE_MODE_XLAUI:
246 case CVMX_HELPER_INTERFACE_MODE_40G_KR4:
247 sfp->ipd_port[0] = ipd_port;
248 for (i = 1; i < 4; i++)
249 sfp->ipd_port[i] = -1;
250 break;
251 default:
252 debug("%s: Interface mode %s for interface 0x%x, ipd_port %d not supported for QSFP\n",
253 __func__, cvmx_helper_interface_mode_to_string(mode), xiface,
254 ipd_port);
255 return -1;
256 }
257 } else {
258 sfp->ipd_port[0] = ipd_port;
259 for (i = 1; i < 4; i++)
260 sfp->ipd_port[i] = -1;
261 }
262 return 0;
263}
264
265/**
266 * Parses all of the channels assigned to a VSC7224 device
267 *
268 * @param[in] fdt_addr Address of flat device tree
269 * @param of_offset Offset of vsc7224 node
270 * @param[in,out] vsc7224 Data structure to hold the data
271 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100272 * Return: 0 for success, -1 on error
Aaron Williams3ec0aa92020-12-11 17:05:59 +0100273 */
274static int cvmx_fdt_parse_vsc7224_channels(const void *fdt_addr, int of_offset,
275 struct cvmx_vsc7224 *vsc7224)
276{
277 int parent_offset = of_offset;
278 int err = 0;
279 int reg;
280 int num_chan = 0;
281 struct cvmx_vsc7224_chan *channel;
282 struct cvmx_fdt_sfp_info *sfp_info;
283 int len;
284 int num_taps;
285 int i;
286 const u32 *tap_values;
287 int of_mac;
288 int xiface, index;
289 bool is_tx;
290 bool is_qsfp;
291 const char *mac_str;
292
293 debug("%s(%p, %d, %s)\n", __func__, fdt_addr, of_offset, vsc7224->name);
294 do {
295 /* Walk through all channels */
296 of_offset = fdt_node_offset_by_compatible(fdt_addr, of_offset,
297 "vitesse,vsc7224-channel");
298 if (of_offset == -FDT_ERR_NOTFOUND) {
299 break;
300 } else if (of_offset < 0) {
301 debug("%s: Failed finding compatible channel\n",
302 __func__);
303 err = -1;
304 break;
305 }
306 if (fdt_parent_offset(fdt_addr, of_offset) != parent_offset)
307 break;
308 reg = cvmx_fdt_get_int(fdt_addr, of_offset, "reg", -1);
309 if (reg < 0 || reg > 3) {
310 debug("%s: channel reg is either not present or out of range\n",
311 __func__);
312 err = -1;
313 break;
314 }
315 is_tx = cvmx_fdt_get_bool(fdt_addr, of_offset, "direction-tx");
316
317 debug("%s(%s): Adding %cx channel %d\n",
318 __func__, vsc7224->name, is_tx ? 't' : 'r',
319 reg);
320 tap_values = (const uint32_t *)fdt_getprop(fdt_addr, of_offset, "taps", &len);
321 if (!tap_values) {
322 debug("%s: Error: no taps defined for vsc7224 channel %d\n",
323 __func__, reg);
324 err = -1;
325 break;
326 }
327
328 if (vsc7224->channel[reg]) {
329 debug("%s: Error: channel %d already assigned at %p\n",
330 __func__, reg,
331 vsc7224->channel[reg]);
332 err = -1;
333 break;
334 }
335 if (len % 16) {
336 debug("%s: Error: tap format error for channel %d\n",
337 __func__, reg);
338 err = -1;
339 break;
340 }
341 num_taps = len / 16;
342 debug("%s: Adding %d taps\n", __func__, num_taps);
343
344 channel = __cvmx_fdt_alloc(sizeof(*channel) +
345 num_taps * sizeof(struct cvmx_vsc7224_tap));
346 if (!channel) {
347 debug("%s: Out of memory\n", __func__);
348 err = -1;
349 break;
350 }
351 vsc7224->channel[reg] = channel;
352 channel->num_taps = num_taps;
353 channel->lane = reg;
354 channel->of_offset = of_offset;
355 channel->is_tx = is_tx;
356 channel->pretap_disable = cvmx_fdt_get_bool(fdt_addr, of_offset, "pretap-disable");
357 channel->posttap_disable =
358 cvmx_fdt_get_bool(fdt_addr, of_offset, "posttap-disable");
359 channel->vsc7224 = vsc7224;
360 /* Read all the tap values */
361 for (i = 0; i < num_taps; i++) {
362 channel->taps[i].len = fdt32_to_cpu(tap_values[i * 4 + 0]);
363 channel->taps[i].main_tap = fdt32_to_cpu(tap_values[i * 4 + 1]);
364 channel->taps[i].pre_tap = fdt32_to_cpu(tap_values[i * 4 + 2]);
365 channel->taps[i].post_tap = fdt32_to_cpu(tap_values[i * 4 + 3]);
366 debug("%s: tap %d: len: %d, main_tap: 0x%x, pre_tap: 0x%x, post_tap: 0x%x\n",
367 __func__, i, channel->taps[i].len, channel->taps[i].main_tap,
368 channel->taps[i].pre_tap, channel->taps[i].post_tap);
369 }
370 /* Now find out which interface it's mapped to */
371 channel->ipd_port = -1;
372
373 mac_str = "sfp-mac";
374 if (fdt_getprop(fdt_addr, of_offset, mac_str, NULL)) {
375 is_qsfp = false;
376 } else if (fdt_getprop(fdt_addr, of_offset, "qsfp-mac", NULL)) {
377 is_qsfp = true;
378 mac_str = "qsfp-mac";
379 } else {
380 debug("%s: Error: MAC not found for %s channel %d\n", __func__,
381 vsc7224->name, reg);
382 return -1;
383 }
384 of_mac = cvmx_fdt_lookup_phandle(fdt_addr, of_offset, mac_str);
385 if (of_mac < 0) {
386 debug("%s: Error %d with MAC %s phandle for %s\n", __func__, of_mac,
387 mac_str, vsc7224->name);
388 return -1;
389 }
390
391 debug("%s: Found mac at offset %d\n", __func__, of_mac);
392 err = cvmx_helper_cfg_get_xiface_index_by_fdt_node_offset(of_mac, &xiface, &index);
393 if (!err) {
394 channel->xiface = xiface;
395 channel->index = index;
396 channel->ipd_port = cvmx_helper_get_ipd_port(xiface, index);
397
398 debug("%s: Found MAC, xiface: 0x%x, index: %d, ipd port: %d\n", __func__,
399 xiface, index, channel->ipd_port);
400 if (channel->ipd_port >= 0) {
401 cvmx_helper_cfg_set_vsc7224_chan_info(xiface, index, channel);
402 debug("%s: Storing config channel for xiface 0x%x, index %d\n",
403 __func__, xiface, index);
404 }
405 sfp_info = cvmx_helper_cfg_get_sfp_info(xiface, index);
406 if (!sfp_info) {
407 debug("%s: Warning: no (Q)SFP+ slot found for xinterface 0x%x, index %d for channel %d\n",
408 __func__, xiface, index, channel->lane);
409 continue;
410 }
411
412 /* Link it */
413 channel->next = sfp_info->vsc7224_chan;
414 if (sfp_info->vsc7224_chan)
415 sfp_info->vsc7224_chan->prev = channel;
416 sfp_info->vsc7224_chan = channel;
417 sfp_info->is_vsc7224 = true;
418 debug("%s: Registering VSC7224 %s channel %d with SFP %s\n", __func__,
419 vsc7224->name, channel->lane, sfp_info->name);
420 if (!sfp_info->mod_abs_changed) {
421 debug("%s: Registering cvmx_sfp_vsc7224_mod_abs_changed at %p for xinterface 0x%x, index %d\n",
422 __func__, &cvmx_sfp_vsc7224_mod_abs_changed, xiface, index);
423 cvmx_sfp_register_mod_abs_changed(
424 sfp_info,
425 &cvmx_sfp_vsc7224_mod_abs_changed,
426 NULL);
427 }
428 }
429 } while (!err && num_chan < 4);
430
431 return err;
432}
433
434/**
435 * @INTERNAL
436 * Parses all instances of the Vitesse VSC7224 reclocking chip
437 *
438 * @param[in] fdt_addr Address of flat device tree
439 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100440 * Return: 0 for success, error otherwise
Aaron Williams3ec0aa92020-12-11 17:05:59 +0100441 */
442int __cvmx_fdt_parse_vsc7224(const void *fdt_addr)
443{
444 int of_offset = -1;
445 struct cvmx_vsc7224 *vsc7224 = NULL;
446 struct cvmx_fdt_gpio_info *gpio_info = NULL;
447 int err = 0;
448 int of_parent;
449 static bool parsed;
450
451 debug("%s(%p)\n", __func__, fdt_addr);
452
453 if (parsed) {
454 debug("%s: Already parsed\n", __func__);
455 return 0;
456 }
457 do {
458 of_offset = fdt_node_offset_by_compatible(fdt_addr, of_offset,
459 "vitesse,vsc7224");
460 debug("%s: of_offset: %d\n", __func__, of_offset);
461 if (of_offset == -FDT_ERR_NOTFOUND) {
462 break;
463 } else if (of_offset < 0) {
464 err = -1;
465 debug("%s: Error %d parsing FDT\n",
466 __func__, of_offset);
467 break;
468 }
469
470 vsc7224 = __cvmx_fdt_alloc(sizeof(*vsc7224));
471
472 if (!vsc7224) {
473 debug("%s: Out of memory!\n", __func__);
474 return -1;
475 }
476 vsc7224->of_offset = of_offset;
477 vsc7224->i2c_addr = cvmx_fdt_get_int(fdt_addr, of_offset,
478 "reg", -1);
479 of_parent = fdt_parent_offset(fdt_addr, of_offset);
480 vsc7224->i2c_bus = cvmx_fdt_get_i2c_bus(fdt_addr, of_parent);
481 if (vsc7224->i2c_addr < 0) {
482 debug("%s: Error: reg field missing\n", __func__);
483 err = -1;
484 break;
485 }
486 if (!vsc7224->i2c_bus) {
487 debug("%s: Error getting i2c bus\n", __func__);
488 err = -1;
489 break;
490 }
491 vsc7224->name = fdt_get_name(fdt_addr, of_offset, NULL);
492 debug("%s: Adding %s\n", __func__, vsc7224->name);
493 if (fdt_getprop(fdt_addr, of_offset, "reset", NULL)) {
494 gpio_info = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "reset");
495 vsc7224->reset_gpio = gpio_info;
496 }
497 if (fdt_getprop(fdt_addr, of_offset, "los", NULL)) {
498 gpio_info = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "los");
499 vsc7224->los_gpio = gpio_info;
500 }
501 debug("%s: Parsing channels\n", __func__);
502 err = cvmx_fdt_parse_vsc7224_channels(fdt_addr, of_offset, vsc7224);
503 if (err) {
504 debug("%s: Error parsing VSC7224 channels\n", __func__);
505 break;
506 }
507 } while (of_offset > 0);
508
509 if (err) {
510 debug("%s(): Error\n", __func__);
511 if (vsc7224) {
512 if (vsc7224->reset_gpio)
513 __cvmx_fdt_free(vsc7224->reset_gpio, sizeof(*vsc7224->reset_gpio));
514 if (vsc7224->los_gpio)
515 __cvmx_fdt_free(vsc7224->los_gpio, sizeof(*vsc7224->los_gpio));
516 if (vsc7224->i2c_bus)
517 cvmx_fdt_free_i2c_bus(vsc7224->i2c_bus);
518 __cvmx_fdt_free(vsc7224, sizeof(*vsc7224));
519 }
520 }
521 if (!err)
522 parsed = true;
523
524 return err;
525}
526
527/**
528 * @INTERNAL
529 * Parses all instances of the Avago AVSP5410 gearbox phy
530 *
531 * @param[in] fdt_addr Address of flat device tree
532 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100533 * Return: 0 for success, error otherwise
Aaron Williams3ec0aa92020-12-11 17:05:59 +0100534 */
535int __cvmx_fdt_parse_avsp5410(const void *fdt_addr)
536{
537 int of_offset = -1;
538 struct cvmx_avsp5410 *avsp5410 = NULL;
539 struct cvmx_fdt_sfp_info *sfp_info;
540 int err = 0;
541 int of_parent;
542 static bool parsed;
543 int of_mac;
544 int xiface, index;
545 bool is_qsfp;
546 const char *mac_str;
547
548 debug("%s(%p)\n", __func__, fdt_addr);
549
550 if (parsed) {
551 debug("%s: Already parsed\n", __func__);
552 return 0;
553 }
554
555 do {
556 of_offset = fdt_node_offset_by_compatible(fdt_addr, of_offset,
557 "avago,avsp-5410");
558 debug("%s: of_offset: %d\n", __func__, of_offset);
559 if (of_offset == -FDT_ERR_NOTFOUND) {
560 break;
561 } else if (of_offset < 0) {
562 err = -1;
563 debug("%s: Error %d parsing FDT\n", __func__, of_offset);
564 break;
565 }
566
567 avsp5410 = __cvmx_fdt_alloc(sizeof(*avsp5410));
568
569 if (!avsp5410) {
570 debug("%s: Out of memory!\n", __func__);
571 return -1;
572 }
573 avsp5410->of_offset = of_offset;
574 avsp5410->i2c_addr = cvmx_fdt_get_int(fdt_addr, of_offset,
575 "reg", -1);
576 of_parent = fdt_parent_offset(fdt_addr, of_offset);
577 avsp5410->i2c_bus = cvmx_fdt_get_i2c_bus(fdt_addr, of_parent);
578 if (avsp5410->i2c_addr < 0) {
579 debug("%s: Error: reg field missing\n", __func__);
580 err = -1;
581 break;
582 }
583 if (!avsp5410->i2c_bus) {
584 debug("%s: Error getting i2c bus\n", __func__);
585 err = -1;
586 break;
587 }
588 avsp5410->name = fdt_get_name(fdt_addr, of_offset, NULL);
589 debug("%s: Adding %s\n", __func__, avsp5410->name);
590
591 /* Now find out which interface it's mapped to */
592 avsp5410->ipd_port = -1;
593
594 mac_str = "sfp-mac";
595 if (fdt_getprop(fdt_addr, of_offset, mac_str, NULL)) {
596 is_qsfp = false;
597 } else if (fdt_getprop(fdt_addr, of_offset, "qsfp-mac", NULL)) {
598 is_qsfp = true;
599 mac_str = "qsfp-mac";
600 } else {
601 debug("%s: Error: MAC not found for %s\n", __func__, avsp5410->name);
602 return -1;
603 }
604 of_mac = cvmx_fdt_lookup_phandle(fdt_addr, of_offset, mac_str);
605 if (of_mac < 0) {
606 debug("%s: Error %d with MAC %s phandle for %s\n", __func__, of_mac,
607 mac_str, avsp5410->name);
608 return -1;
609 }
610
611 debug("%s: Found mac at offset %d\n", __func__, of_mac);
612 err = cvmx_helper_cfg_get_xiface_index_by_fdt_node_offset(of_mac, &xiface, &index);
613 if (!err) {
614 avsp5410->xiface = xiface;
615 avsp5410->index = index;
616 avsp5410->ipd_port = cvmx_helper_get_ipd_port(xiface, index);
617
618 debug("%s: Found MAC, xiface: 0x%x, index: %d, ipd port: %d\n", __func__,
619 xiface, index, avsp5410->ipd_port);
620 if (avsp5410->ipd_port >= 0) {
621 cvmx_helper_cfg_set_avsp5410_info(xiface, index, avsp5410);
622 debug("%s: Storing config phy for xiface 0x%x, index %d\n",
623 __func__, xiface, index);
624 }
625 sfp_info = cvmx_helper_cfg_get_sfp_info(xiface, index);
626 if (!sfp_info) {
627 debug("%s: Warning: no (Q)SFP+ slot found for xinterface 0x%x, index %d\n",
628 __func__, xiface, index);
629 continue;
630 }
631
632 sfp_info->is_avsp5410 = true;
633 sfp_info->avsp5410 = avsp5410;
634 debug("%s: Registering AVSP5410 %s with SFP %s\n", __func__, avsp5410->name,
635 sfp_info->name);
636 if (!sfp_info->mod_abs_changed) {
637 debug("%s: Registering cvmx_sfp_avsp5410_mod_abs_changed at %p for xinterface 0x%x, index %d\n",
638 __func__, &cvmx_sfp_avsp5410_mod_abs_changed, xiface, index);
639 cvmx_sfp_register_mod_abs_changed(
640 sfp_info,
641 &cvmx_sfp_avsp5410_mod_abs_changed,
642 NULL);
643 }
644 }
645 } while (of_offset > 0);
646
647 if (err) {
648 debug("%s(): Error\n", __func__);
649 if (avsp5410) {
650 if (avsp5410->i2c_bus)
651 cvmx_fdt_free_i2c_bus(avsp5410->i2c_bus);
652 __cvmx_fdt_free(avsp5410, sizeof(*avsp5410));
653 }
654 }
655 if (!err)
656 parsed = true;
657
658 return err;
659}
660
661/**
662 * Parse QSFP GPIOs for SFP
663 *
664 * @param[in] fdt_addr Pointer to flat device tree
665 * @param of_offset Offset of QSFP node
666 * @param[out] sfp_info Pointer to sfp info to fill in
667 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100668 * Return: 0 for success
Aaron Williams3ec0aa92020-12-11 17:05:59 +0100669 */
670static int cvmx_parse_qsfp(const void *fdt_addr, int of_offset, struct cvmx_fdt_sfp_info *sfp_info)
671{
672 sfp_info->select = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "select");
673 sfp_info->mod_abs = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "mod_prs");
674 sfp_info->reset = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "reset");
675 sfp_info->interrupt = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "interrupt");
676 sfp_info->lp_mode = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "lp_mode");
677 return 0;
678}
679
680/**
681 * Parse SFP GPIOs for SFP
682 *
683 * @param[in] fdt_addr Pointer to flat device tree
684 * @param of_offset Offset of SFP node
685 * @param[out] sfp_info Pointer to sfp info to fill in
686 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100687 * Return: 0 for success
Aaron Williams3ec0aa92020-12-11 17:05:59 +0100688 */
689static int cvmx_parse_sfp(const void *fdt_addr, int of_offset, struct cvmx_fdt_sfp_info *sfp_info)
690{
691 sfp_info->mod_abs = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "mod_abs");
692 sfp_info->rx_los = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "rx_los");
693 sfp_info->tx_disable = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "tx_disable");
694 sfp_info->tx_error = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "tx_error");
695 return 0;
696}
697
698/**
699 * Parse SFP/QSFP EEPROM and diag
700 *
701 * @param[in] fdt_addr Pointer to flat device tree
702 * @param of_offset Offset of SFP node
703 * @param[out] sfp_info Pointer to sfp info to fill in
704 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100705 * Return: 0 for success, -1 on error
Aaron Williams3ec0aa92020-12-11 17:05:59 +0100706 */
707static int cvmx_parse_sfp_eeprom(const void *fdt_addr, int of_offset,
708 struct cvmx_fdt_sfp_info *sfp_info)
709{
710 int of_eeprom;
711 int of_diag;
712
713 debug("%s(%p, %d, %s)\n", __func__, fdt_addr, of_offset, sfp_info->name);
714 of_eeprom = cvmx_fdt_lookup_phandle(fdt_addr, of_offset, "eeprom");
715 if (of_eeprom < 0) {
716 debug("%s: Missing \"eeprom\" from device tree for %s\n", __func__, sfp_info->name);
717 return -1;
718 }
719
720 sfp_info->i2c_bus = cvmx_fdt_get_i2c_bus(fdt_addr, fdt_parent_offset(fdt_addr, of_eeprom));
721 sfp_info->i2c_eeprom_addr = cvmx_fdt_get_int(fdt_addr, of_eeprom, "reg", 0x50);
722
723 debug("%s(%p, %d, %s, %d)\n", __func__, fdt_addr, of_offset, sfp_info->name,
724 sfp_info->i2c_eeprom_addr);
725
726 if (!sfp_info->i2c_bus) {
727 debug("%s: Error: could not determine i2c bus for eeprom for %s\n", __func__,
728 sfp_info->name);
729 return -1;
730 }
731 of_diag = cvmx_fdt_lookup_phandle(fdt_addr, of_offset, "diag");
732 if (of_diag >= 0)
733 sfp_info->i2c_diag_addr = cvmx_fdt_get_int(fdt_addr, of_diag, "reg", 0x51);
734 else
735 sfp_info->i2c_diag_addr = 0x51;
736 return 0;
737}
738
739/**
740 * Parse SFP information from device tree
741 *
742 * @param[in] fdt_addr Address of flat device tree
743 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100744 * Return: pointer to sfp info or NULL if error
Aaron Williams3ec0aa92020-12-11 17:05:59 +0100745 */
746struct cvmx_fdt_sfp_info *cvmx_helper_fdt_parse_sfp_info(const void *fdt_addr, int of_offset)
747{
748 struct cvmx_fdt_sfp_info *sfp_info = NULL;
749 int err = -1;
750 bool is_qsfp;
751
752 if (!fdt_node_check_compatible(fdt_addr, of_offset, "ethernet,sfp-slot")) {
753 is_qsfp = false;
754 } else if (!fdt_node_check_compatible(fdt_addr, of_offset, "ethernet,qsfp-slot")) {
755 is_qsfp = true;
756 } else {
757 debug("%s: Error: incompatible sfp/qsfp slot, compatible=%s\n", __func__,
758 (char *)fdt_getprop(fdt_addr, of_offset, "compatible", NULL));
759 goto error_exit;
760 }
761
762 debug("%s: %ssfp module found at offset %d\n", __func__, is_qsfp ? "q" : "", of_offset);
763 sfp_info = __cvmx_fdt_alloc(sizeof(*sfp_info));
764 if (!sfp_info) {
765 debug("%s: Error: out of memory\n", __func__);
766 goto error_exit;
767 }
768 sfp_info->name = fdt_get_name(fdt_addr, of_offset, NULL);
769 sfp_info->of_offset = of_offset;
770 sfp_info->is_qsfp = is_qsfp;
771 sfp_info->last_mod_abs = -1;
772 sfp_info->last_rx_los = -1;
773
774 if (is_qsfp)
775 err = cvmx_parse_qsfp(fdt_addr, of_offset, sfp_info);
776 else
777 err = cvmx_parse_sfp(fdt_addr, of_offset, sfp_info);
778 if (err) {
779 debug("%s: Error in %s parsing %ssfp GPIO info\n", __func__, sfp_info->name,
780 is_qsfp ? "q" : "");
781 goto error_exit;
782 }
783 debug("%s: Parsing %ssfp module eeprom\n", __func__, is_qsfp ? "q" : "");
784 err = cvmx_parse_sfp_eeprom(fdt_addr, of_offset, sfp_info);
785 if (err) {
786 debug("%s: Error parsing eeprom info for %s\n", __func__, sfp_info->name);
787 goto error_exit;
788 }
789
790 /* Register default check for mod_abs changed */
791 if (!err)
792 cvmx_sfp_register_check_mod_abs(sfp_info, cvmx_sfp_check_mod_abs, NULL);
793
794error_exit:
795 /* Note: we don't free any data structures on error since it gets
796 * rather complicated with i2c buses and whatnot.
797 */
798 return err ? NULL : sfp_info;
799}
800
801/**
802 * @INTERNAL
803 * Parse a slice of the Inphi/Cortina CS4343 in the device tree
804 *
805 * @param[in] fdt_addr Address of flat device tree
806 * @param of_offset fdt offset of slice
807 * @param phy_info phy_info data structure
808 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100809 * Return: slice number if non-negative, otherwise error
Aaron Williams3ec0aa92020-12-11 17:05:59 +0100810 */
811static int cvmx_fdt_parse_cs4343_slice(const void *fdt_addr, int of_offset,
812 struct cvmx_phy_info *phy_info)
813{
814 struct cvmx_cs4343_slice_info *slice;
815 int reg;
816 int reg_offset;
817
818 reg = cvmx_fdt_get_int(fdt_addr, of_offset, "reg", -1);
819 reg_offset = cvmx_fdt_get_int(fdt_addr, of_offset, "slice_offset", -1);
820
821 if (reg < 0 || reg >= 4) {
822 debug("%s(%p, %d, %p): Error: reg %d undefined or out of range\n", __func__,
823 fdt_addr, of_offset, phy_info, reg);
824 return -1;
825 }
826 if (reg_offset % 0x1000 || reg_offset > 0x3000 || reg_offset < 0) {
827 debug("%s(%p, %d, %p): Error: reg_offset 0x%x undefined or out of range\n",
828 __func__, fdt_addr, of_offset, phy_info, reg_offset);
829 return -1;
830 }
831 if (!phy_info->cs4343_info) {
832 debug("%s: Error: phy info cs4343 datastructure is NULL\n", __func__);
833 return -1;
834 }
835 debug("%s(%p, %d, %p): %s, reg: %d, slice offset: 0x%x\n", __func__, fdt_addr, of_offset,
836 phy_info, fdt_get_name(fdt_addr, of_offset, NULL), reg, reg_offset);
837 slice = &phy_info->cs4343_info->slice[reg];
838 slice->name = fdt_get_name(fdt_addr, of_offset, NULL);
839 slice->mphy = phy_info->cs4343_info;
840 slice->phy_info = phy_info;
841 slice->of_offset = of_offset;
842 slice->slice_no = reg;
843 slice->reg_offset = reg_offset;
844 /* SR settings */
845 slice->sr_stx_cmode_res = cvmx_fdt_get_int(fdt_addr, of_offset, "sr-stx-cmode-res", 3);
846 slice->sr_stx_drv_lower_cm =
847 cvmx_fdt_get_int(fdt_addr, of_offset, "sr-stx-drv-lower-cm", 8);
848 slice->sr_stx_level = cvmx_fdt_get_int(fdt_addr, of_offset, "sr-stx-level", 0x1c);
849 slice->sr_stx_pre_peak = cvmx_fdt_get_int(fdt_addr, of_offset, "sr-stx-pre-peak", 1);
850 slice->sr_stx_muxsubrate_sel =
851 cvmx_fdt_get_int(fdt_addr, of_offset, "sr-stx-muxsubrate-sel", 0);
852 slice->sr_stx_post_peak = cvmx_fdt_get_int(fdt_addr, of_offset, "sr-stx-post-peak", 8);
853 /* CX settings */
854 slice->cx_stx_cmode_res = cvmx_fdt_get_int(fdt_addr, of_offset, "cx-stx-cmode-res", 3);
855 slice->cx_stx_drv_lower_cm =
856 cvmx_fdt_get_int(fdt_addr, of_offset, "cx-stx-drv-lower-cm", 8);
857 slice->cx_stx_level = cvmx_fdt_get_int(fdt_addr, of_offset, "cx-stx-level", 0x1c);
858 slice->cx_stx_pre_peak = cvmx_fdt_get_int(fdt_addr, of_offset, "cx-stx-pre-peak", 1);
859 slice->cx_stx_muxsubrate_sel =
860 cvmx_fdt_get_int(fdt_addr, of_offset, "cx-stx-muxsubrate-sel", 0);
861 slice->cx_stx_post_peak = cvmx_fdt_get_int(fdt_addr, of_offset, "cx-stx-post-peak", 0xC);
862 /* 1000Base-X settings */
863 /* CX settings */
864 slice->basex_stx_cmode_res =
865 cvmx_fdt_get_int(fdt_addr, of_offset, "basex-stx-cmode-res", 3);
866 slice->basex_stx_drv_lower_cm =
867 cvmx_fdt_get_int(fdt_addr, of_offset, "basex-stx-drv-lower-cm", 8);
868 slice->basex_stx_level = cvmx_fdt_get_int(fdt_addr, of_offset,
869 "basex-stx-level", 0x1c);
870 slice->basex_stx_pre_peak = cvmx_fdt_get_int(fdt_addr, of_offset,
871 "basex-stx-pre-peak", 1);
872 slice->basex_stx_muxsubrate_sel =
873 cvmx_fdt_get_int(fdt_addr, of_offset,
874 "basex-stx-muxsubrate-sel", 0);
875 slice->basex_stx_post_peak =
876 cvmx_fdt_get_int(fdt_addr, of_offset, "basex-stx-post-peak", 8);
877 /* Get the link LED gpio pin */
878 slice->link_gpio = cvmx_fdt_get_int(fdt_addr, of_offset,
879 "link-led-gpio", -1);
880 slice->error_gpio = cvmx_fdt_get_int(fdt_addr, of_offset,
881 "error-led-gpio", -1);
882 slice->los_gpio = cvmx_fdt_get_int(fdt_addr, of_offset,
883 "los-input-gpio", -1);
884 slice->link_inverted = cvmx_fdt_get_bool(fdt_addr, of_offset,
885 "link-led-gpio-inverted");
886 slice->error_inverted = cvmx_fdt_get_bool(fdt_addr, of_offset,
887 "error-led-gpio-inverted");
888 slice->los_inverted = cvmx_fdt_get_bool(fdt_addr, of_offset,
889 "los-input-gpio-inverted");
890 /* Convert GPIOs to be die based if they're not already */
891 if (slice->link_gpio > 4 && slice->link_gpio <= 8)
892 slice->link_gpio -= 4;
893 if (slice->error_gpio > 4 && slice->error_gpio <= 8)
894 slice->error_gpio -= 4;
895 if (slice->los_gpio > 4 && slice->los_gpio <= 8)
896 slice->los_gpio -= 4;
897
898 return reg;
899}
900
901/**
902 * @INTERNAL
903 * Parses either a CS4343 phy or a slice of the phy from the device tree
904 * @param[in] fdt_addr Address of FDT
905 * @param of_offset offset of slice or phy in device tree
906 * @param phy_info phy_info data structure to fill in
907 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100908 * Return: 0 for success, -1 on error
Aaron Williams3ec0aa92020-12-11 17:05:59 +0100909 */
910int cvmx_fdt_parse_cs4343(const void *fdt_addr, int of_offset, struct cvmx_phy_info *phy_info)
911{
912 int of_slice = -1;
913 struct cvmx_cs4343_info *cs4343;
914 int err = -1;
915 int reg;
916
917 debug("%s(%p, %d, %p): %s (%s)\n", __func__,
918 fdt_addr, of_offset, phy_info,
919 fdt_get_name(fdt_addr, of_offset, NULL),
920 (const char *)fdt_getprop(fdt_addr, of_offset, "compatible", NULL));
921
922 if (!phy_info->cs4343_info)
923 phy_info->cs4343_info = __cvmx_fdt_alloc(sizeof(struct cvmx_cs4343_info));
924 if (!phy_info->cs4343_info) {
925 debug("%s: Error: out of memory!\n", __func__);
926 return -1;
927 }
928 cs4343 = phy_info->cs4343_info;
929 /* If we're passed to a slice then process only that slice */
930 if (!fdt_node_check_compatible(fdt_addr, of_offset, "cortina,cs4343-slice")) {
931 err = 0;
932 of_slice = of_offset;
933 of_offset = fdt_parent_offset(fdt_addr, of_offset);
934 reg = cvmx_fdt_parse_cs4343_slice(fdt_addr, of_slice, phy_info);
935 if (reg >= 0)
936 phy_info->cs4343_slice_info = &cs4343->slice[reg];
937 else
938 err = reg;
939 } else if (!fdt_node_check_compatible(fdt_addr, of_offset,
940 "cortina,cs4343")) {
941 /* Walk through and process all of the slices */
942 of_slice =
943 fdt_node_offset_by_compatible(fdt_addr, of_offset, "cortina,cs4343-slice");
944 while (of_slice > 0 && fdt_parent_offset(fdt_addr, of_slice) ==
945 of_offset) {
946 debug("%s: Parsing slice %s\n", __func__,
947 fdt_get_name(fdt_addr, of_slice, NULL));
948 err = cvmx_fdt_parse_cs4343_slice(fdt_addr, of_slice,
949 phy_info);
950 if (err < 0)
951 break;
952 of_slice = fdt_node_offset_by_compatible(fdt_addr,
953 of_slice,
954 "cortina,cs4343-slice");
955 }
956 } else {
957 debug("%s: Error: unknown compatible string %s for %s\n", __func__,
958 (const char *)fdt_getprop(fdt_addr, of_offset,
959 "compatible", NULL),
960 fdt_get_name(fdt_addr, of_offset, NULL));
961 }
962
963 if (err >= 0) {
964 cs4343->name = fdt_get_name(fdt_addr, of_offset, NULL);
965 cs4343->phy_info = phy_info;
966 cs4343->of_offset = of_offset;
967 }
968
969 return err < 0 ? -1 : 0;
970}