1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 Marvell International Ltd.
5 * FDT Helper functions similar to those provided to U-Boot.
11 #include <linux/delay.h>
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>
24 /** Structure used to get type of GPIO from device tree */
26 char *compatible; /** Compatible string */
27 enum cvmx_gpio_type type; /** Type */
28 int8_t size; /** (max) Number of pins */
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
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
41 enum cvmx_i2c_mux_type { I2C_MUX, I2C_SWITCH };
43 /** Structure used to get type of GPIO from device tree */
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 */
53 * Local allocator to handle both SE and U-Boot that also zeroes out memory
55 * @param size number of bytes to allocate
57 * @return pointer to allocated memory or NULL if out of memory.
58 * Alignment is set to 8-bytes.
60 void *__cvmx_fdt_alloc(size_t size)
62 return calloc(size, 1);
66 * Free allocated memory.
68 * @param ptr pointer to memory to free
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.
73 void __cvmx_fdt_free(void *ptr, size_t size)
79 * Look up a phandle and follow it to its node then return the offset of that
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
88 * @return -ve error code on error or 0 for success
90 int cvmx_fdt_lookup_phandles(const void *fdt_addr, int node,
91 const char *prop_name, int *lenp,
98 phandles = fdt_getprop(fdt_addr, node, prop_name, &count);
99 if (!phandles || count < 0)
100 return -FDT_ERR_NOTFOUND;
106 for (i = 0; i < count; i++)
107 nodes[i] = fdt_node_offset_by_phandle(fdt_addr,
108 fdt32_to_cpu(phandles[i]));
114 * Given a FDT node return the CPU node number
116 * @param[in] fdt_addr Address of FDT
117 * @param node FDT node number
119 * @return CPU node number or error if negative
121 int cvmx_fdt_get_cpu_node(const void *fdt_addr, int node)
127 while (fdt_node_check_compatible(fdt_addr, parent, "simple-bus") != 0) {
128 parent = fdt_parent_offset(fdt_addr, parent);
132 ranges = fdt_getprop(fdt_addr, parent, "ranges", &len);
140 return -FDT_ERR_TRUNCATED;
142 return fdt32_to_cpu(ranges[2]) / 0x10;
146 * Get the total size of the flat device tree
148 * @param[in] fdt_addr Address of FDT
150 * @return Size of flat device tree in bytes or error if negative.
152 int cvmx_fdt_get_fdt_size(const void *fdt_addr)
156 rc = fdt_check_header(fdt_addr);
159 return fdt_totalsize(fdt_addr);
163 * Returns if a node is compatible with one of the items in the string list
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.
170 * @return 0 if at least one item matches, 1 if no matches
172 int cvmx_fdt_node_check_compatible_list(const void *fdt_addr, int node, const char *const *strlist)
174 while (*strlist && **strlist) {
175 if (!fdt_node_check_compatible(fdt_addr, node, *strlist))
183 * Given a FDT node, return the next compatible node.
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.
190 * @return next matching node or -1 if no more matches.
192 int cvmx_fdt_node_offset_by_compatible_list(const void *fdt_addr, int startoffset,
193 const char *const *strlist)
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))
206 * Attaches a PHY to a SFP or QSFP.
208 * @param sfp sfp to attach PHY to
209 * @param phy_info phy descriptor to attach or NULL to detach
211 void cvmx_sfp_attach_phy(struct cvmx_fdt_sfp_info *sfp, struct cvmx_phy_info *phy_info)
213 sfp->phy_info = phy_info;
215 phy_info->sfp_info = sfp;
219 * Assigns an IPD port to a SFP slot
221 * @param sfp Handle to SFP data structure
222 * @param ipd_port Port to assign it to
224 * @return 0 for success, -1 on error
226 int cvmx_sfp_set_ipd_port(struct cvmx_fdt_sfp_info *sfp, int ipd_port)
232 cvmx_helper_interface_mode_t mode;
234 xiface = cvmx_helper_get_interface_num(ipd_port);
235 mode = cvmx_helper_interface_get_mode(xiface);
236 sfp->ipd_port[0] = ipd_port;
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);
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;
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,
258 sfp->ipd_port[0] = ipd_port;
259 for (i = 1; i < 4; i++)
260 sfp->ipd_port[i] = -1;
266 * Parses all of the channels assigned to a VSC7224 device
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
272 * @return 0 for success, -1 on error
274 static int cvmx_fdt_parse_vsc7224_channels(const void *fdt_addr, int of_offset,
275 struct cvmx_vsc7224 *vsc7224)
277 int parent_offset = of_offset;
281 struct cvmx_vsc7224_chan *channel;
282 struct cvmx_fdt_sfp_info *sfp_info;
286 const u32 *tap_values;
293 debug("%s(%p, %d, %s)\n", __func__, fdt_addr, of_offset, vsc7224->name);
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) {
300 } else if (of_offset < 0) {
301 debug("%s: Failed finding compatible channel\n",
306 if (fdt_parent_offset(fdt_addr, of_offset) != parent_offset)
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",
315 is_tx = cvmx_fdt_get_bool(fdt_addr, of_offset, "direction-tx");
317 debug("%s(%s): Adding %cx channel %d\n",
318 __func__, vsc7224->name, is_tx ? 't' : 'r',
320 tap_values = (const uint32_t *)fdt_getprop(fdt_addr, of_offset, "taps", &len);
322 debug("%s: Error: no taps defined for vsc7224 channel %d\n",
328 if (vsc7224->channel[reg]) {
329 debug("%s: Error: channel %d already assigned at %p\n",
331 vsc7224->channel[reg]);
336 debug("%s: Error: tap format error for channel %d\n",
342 debug("%s: Adding %d taps\n", __func__, num_taps);
344 channel = __cvmx_fdt_alloc(sizeof(*channel) +
345 num_taps * sizeof(struct cvmx_vsc7224_tap));
347 debug("%s: Out of memory\n", __func__);
351 vsc7224->channel[reg] = channel;
352 channel->num_taps = num_taps;
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);
370 /* Now find out which interface it's mapped to */
371 channel->ipd_port = -1;
374 if (fdt_getprop(fdt_addr, of_offset, mac_str, NULL)) {
376 } else if (fdt_getprop(fdt_addr, of_offset, "qsfp-mac", NULL)) {
378 mac_str = "qsfp-mac";
380 debug("%s: Error: MAC not found for %s channel %d\n", __func__,
384 of_mac = cvmx_fdt_lookup_phandle(fdt_addr, of_offset, mac_str);
386 debug("%s: Error %d with MAC %s phandle for %s\n", __func__, of_mac,
387 mac_str, vsc7224->name);
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);
394 channel->xiface = xiface;
395 channel->index = index;
396 channel->ipd_port = cvmx_helper_get_ipd_port(xiface, index);
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);
405 sfp_info = cvmx_helper_cfg_get_sfp_info(xiface, index);
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);
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(
425 &cvmx_sfp_vsc7224_mod_abs_changed,
429 } while (!err && num_chan < 4);
436 * Parses all instances of the Vitesse VSC7224 reclocking chip
438 * @param[in] fdt_addr Address of flat device tree
440 * @return 0 for success, error otherwise
442 int __cvmx_fdt_parse_vsc7224(const void *fdt_addr)
445 struct cvmx_vsc7224 *vsc7224 = NULL;
446 struct cvmx_fdt_gpio_info *gpio_info = NULL;
451 debug("%s(%p)\n", __func__, fdt_addr);
454 debug("%s: Already parsed\n", __func__);
458 of_offset = fdt_node_offset_by_compatible(fdt_addr, of_offset,
460 debug("%s: of_offset: %d\n", __func__, of_offset);
461 if (of_offset == -FDT_ERR_NOTFOUND) {
463 } else if (of_offset < 0) {
465 debug("%s: Error %d parsing FDT\n",
466 __func__, of_offset);
470 vsc7224 = __cvmx_fdt_alloc(sizeof(*vsc7224));
473 debug("%s: Out of memory!\n", __func__);
476 vsc7224->of_offset = of_offset;
477 vsc7224->i2c_addr = cvmx_fdt_get_int(fdt_addr, of_offset,
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__);
486 if (!vsc7224->i2c_bus) {
487 debug("%s: Error getting i2c bus\n", __func__);
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;
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;
501 debug("%s: Parsing channels\n", __func__);
502 err = cvmx_fdt_parse_vsc7224_channels(fdt_addr, of_offset, vsc7224);
504 debug("%s: Error parsing VSC7224 channels\n", __func__);
507 } while (of_offset > 0);
510 debug("%s(): Error\n", __func__);
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));
529 * Parses all instances of the Avago AVSP5410 gearbox phy
531 * @param[in] fdt_addr Address of flat device tree
533 * @return 0 for success, error otherwise
535 int __cvmx_fdt_parse_avsp5410(const void *fdt_addr)
538 struct cvmx_avsp5410 *avsp5410 = NULL;
539 struct cvmx_fdt_sfp_info *sfp_info;
548 debug("%s(%p)\n", __func__, fdt_addr);
551 debug("%s: Already parsed\n", __func__);
556 of_offset = fdt_node_offset_by_compatible(fdt_addr, of_offset,
558 debug("%s: of_offset: %d\n", __func__, of_offset);
559 if (of_offset == -FDT_ERR_NOTFOUND) {
561 } else if (of_offset < 0) {
563 debug("%s: Error %d parsing FDT\n", __func__, of_offset);
567 avsp5410 = __cvmx_fdt_alloc(sizeof(*avsp5410));
570 debug("%s: Out of memory!\n", __func__);
573 avsp5410->of_offset = of_offset;
574 avsp5410->i2c_addr = cvmx_fdt_get_int(fdt_addr, of_offset,
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__);
583 if (!avsp5410->i2c_bus) {
584 debug("%s: Error getting i2c bus\n", __func__);
588 avsp5410->name = fdt_get_name(fdt_addr, of_offset, NULL);
589 debug("%s: Adding %s\n", __func__, avsp5410->name);
591 /* Now find out which interface it's mapped to */
592 avsp5410->ipd_port = -1;
595 if (fdt_getprop(fdt_addr, of_offset, mac_str, NULL)) {
597 } else if (fdt_getprop(fdt_addr, of_offset, "qsfp-mac", NULL)) {
599 mac_str = "qsfp-mac";
601 debug("%s: Error: MAC not found for %s\n", __func__, avsp5410->name);
604 of_mac = cvmx_fdt_lookup_phandle(fdt_addr, of_offset, mac_str);
606 debug("%s: Error %d with MAC %s phandle for %s\n", __func__, of_mac,
607 mac_str, avsp5410->name);
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);
614 avsp5410->xiface = xiface;
615 avsp5410->index = index;
616 avsp5410->ipd_port = cvmx_helper_get_ipd_port(xiface, index);
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);
625 sfp_info = cvmx_helper_cfg_get_sfp_info(xiface, index);
627 debug("%s: Warning: no (Q)SFP+ slot found for xinterface 0x%x, index %d\n",
628 __func__, xiface, index);
632 sfp_info->is_avsp5410 = true;
633 sfp_info->avsp5410 = avsp5410;
634 debug("%s: Registering AVSP5410 %s with SFP %s\n", __func__, avsp5410->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(
641 &cvmx_sfp_avsp5410_mod_abs_changed,
645 } while (of_offset > 0);
648 debug("%s(): Error\n", __func__);
650 if (avsp5410->i2c_bus)
651 cvmx_fdt_free_i2c_bus(avsp5410->i2c_bus);
652 __cvmx_fdt_free(avsp5410, sizeof(*avsp5410));
662 * Parse QSFP GPIOs for SFP
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
668 * @return 0 for success
670 static int cvmx_parse_qsfp(const void *fdt_addr, int of_offset, struct cvmx_fdt_sfp_info *sfp_info)
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");
681 * Parse SFP GPIOs for SFP
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
687 * @return 0 for success
689 static int cvmx_parse_sfp(const void *fdt_addr, int of_offset, struct cvmx_fdt_sfp_info *sfp_info)
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");
699 * Parse SFP/QSFP EEPROM and diag
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
705 * @return 0 for success, -1 on error
707 static int cvmx_parse_sfp_eeprom(const void *fdt_addr, int of_offset,
708 struct cvmx_fdt_sfp_info *sfp_info)
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");
716 debug("%s: Missing \"eeprom\" from device tree for %s\n", __func__, sfp_info->name);
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);
723 debug("%s(%p, %d, %s, %d)\n", __func__, fdt_addr, of_offset, sfp_info->name,
724 sfp_info->i2c_eeprom_addr);
726 if (!sfp_info->i2c_bus) {
727 debug("%s: Error: could not determine i2c bus for eeprom for %s\n", __func__,
731 of_diag = cvmx_fdt_lookup_phandle(fdt_addr, of_offset, "diag");
733 sfp_info->i2c_diag_addr = cvmx_fdt_get_int(fdt_addr, of_diag, "reg", 0x51);
735 sfp_info->i2c_diag_addr = 0x51;
740 * Parse SFP information from device tree
742 * @param[in] fdt_addr Address of flat device tree
744 * @return pointer to sfp info or NULL if error
746 struct cvmx_fdt_sfp_info *cvmx_helper_fdt_parse_sfp_info(const void *fdt_addr, int of_offset)
748 struct cvmx_fdt_sfp_info *sfp_info = NULL;
752 if (!fdt_node_check_compatible(fdt_addr, of_offset, "ethernet,sfp-slot")) {
754 } else if (!fdt_node_check_compatible(fdt_addr, of_offset, "ethernet,qsfp-slot")) {
757 debug("%s: Error: incompatible sfp/qsfp slot, compatible=%s\n", __func__,
758 (char *)fdt_getprop(fdt_addr, of_offset, "compatible", NULL));
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));
765 debug("%s: Error: out of memory\n", __func__);
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;
775 err = cvmx_parse_qsfp(fdt_addr, of_offset, sfp_info);
777 err = cvmx_parse_sfp(fdt_addr, of_offset, sfp_info);
779 debug("%s: Error in %s parsing %ssfp GPIO info\n", __func__, sfp_info->name,
783 debug("%s: Parsing %ssfp module eeprom\n", __func__, is_qsfp ? "q" : "");
784 err = cvmx_parse_sfp_eeprom(fdt_addr, of_offset, sfp_info);
786 debug("%s: Error parsing eeprom info for %s\n", __func__, sfp_info->name);
790 /* Register default check for mod_abs changed */
792 cvmx_sfp_register_check_mod_abs(sfp_info, cvmx_sfp_check_mod_abs, NULL);
795 /* Note: we don't free any data structures on error since it gets
796 * rather complicated with i2c buses and whatnot.
798 return err ? NULL : sfp_info;
803 * Parse a slice of the Inphi/Cortina CS4343 in the device tree
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
809 * @return slice number if non-negative, otherwise error
811 static int cvmx_fdt_parse_cs4343_slice(const void *fdt_addr, int of_offset,
812 struct cvmx_phy_info *phy_info)
814 struct cvmx_cs4343_slice_info *slice;
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);
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);
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);
831 if (!phy_info->cs4343_info) {
832 debug("%s: Error: phy info cs4343 datastructure is NULL\n", __func__);
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;
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);
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 */
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;
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
908 * @return 0 for success, -1 on error
910 int cvmx_fdt_parse_cs4343(const void *fdt_addr, int of_offset, struct cvmx_phy_info *phy_info)
913 struct cvmx_cs4343_info *cs4343;
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));
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__);
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")) {
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);
936 phy_info->cs4343_slice_info = &cs4343->slice[reg];
939 } else if (!fdt_node_check_compatible(fdt_addr, of_offset,
941 /* Walk through and process all of the slices */
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) ==
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,
952 of_slice = fdt_node_offset_by_compatible(fdt_addr,
954 "cortina,cs4343-slice");
957 debug("%s: Error: unknown compatible string %s for %s\n", __func__,
958 (const char *)fdt_getprop(fdt_addr, of_offset,
960 fdt_get_name(fdt_addr, of_offset, NULL));
964 cs4343->name = fdt_get_name(fdt_addr, of_offset, NULL);
965 cs4343->phy_info = phy_info;
966 cs4343->of_offset = of_offset;
969 return err < 0 ? -1 : 0;