summaryrefslogtreecommitdiffstats
path: root/net/econet
ModeNameSize
-rw-r--r--Kconfig1206logstatsplain
-rw-r--r--Makefile102logstatsplain
-rw-r--r--af_econet.c26177logstatsplain
='#n101'>101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2020 Marvell International Ltd.
 *
 * FDT Helper functions similar to those provided to U-Boot.
 */

#include <log.h>
#include <malloc.h>
#include <net.h>
#include <linux/delay.h>

#include <mach/cvmx-regs.h>
#include <mach/cvmx-csr.h>
#include <mach/cvmx-bootmem.h>
#include <mach/octeon-model.h>
#include <mach/octeon_fdt.h>
#include <mach/cvmx-helper.h>
#include <mach/cvmx-helper-board.h>
#include <mach/cvmx-helper-cfg.h>
#include <mach/cvmx-helper-fdt.h>
#include <mach/cvmx-helper-gpio.h>

/** Structure used to get type of GPIO from device tree */
struct gpio_compat {
	char *compatible;	  /** Compatible string */
	enum cvmx_gpio_type type; /** Type */
	int8_t size;		  /** (max) Number of pins */
};

#define GPIO_REG_PCA953X_IN	0
#define GPIO_REG_PCA953X_OUT	1
#define GPIO_REG_PCA953X_INVERT 2
#define GPIO_REG_PCA953X_DIR	3

#define GPIO_REG_PCA957X_IN	0
#define GPIO_REG_PCA957X_INVERT 1
#define GPIO_REG_PCA957X_CFG	4
#define GPIO_REG_PCA957X_OUT	5

enum cvmx_i2c_mux_type { I2C_MUX, I2C_SWITCH };

/** Structure used to get type of GPIO from device tree */
struct mux_compat {
	char *compatible;		 /** Compatible string */
	enum cvmx_i2c_bus_type type;	 /** Mux chip type */
	enum cvmx_i2c_mux_type mux_type; /** Type of mux */
	u8 enable;			 /** Enable bit for mux */
	u8 size;			 /** (max) Number of channels */
};

/**
 * Local allocator to handle both SE and U-Boot that also zeroes out memory
 *
 * @param	size	number of bytes to allocate
 *
 * @return	pointer to allocated memory or NULL if out of memory.
 *		Alignment is set to 8-bytes.
 */
void *__cvmx_fdt_alloc(size_t size)
{
	return calloc(size, 1);
}

/**
 * Free allocated memory.
 *
 * @param	ptr	pointer to memory to free
 *
 * NOTE: This only works in U-Boot since SE does not really have a freeing
 *	 mechanism.  In SE the memory is zeroed out.
 */
void __cvmx_fdt_free(void *ptr, size_t size)
{
	free(ptr);
}

/**
 * Look up a phandle and follow it to its node then return the offset of that
 * node.
 *
 * @param[in]	fdt_addr	pointer to FDT blob
 * @param	node		node to read phandle from
 * @param[in]	prop_name	name of property to find
 * @param[in,out] lenp		Number of phandles, input max number
 * @param[out]	nodes		Array of phandle nodes
 *
 * @return	-ve error code on error or 0 for success
 */
int cvmx_fdt_lookup_phandles(const void *fdt_addr, int node,
			     const char *prop_name, int *lenp,
			     int *nodes)
{
	const u32 *phandles;
	int count;
	int i;

	phandles = fdt_getprop(fdt_addr, node, prop_name, &count);
	if (!phandles || count < 0)
		return -FDT_ERR_NOTFOUND;

	count /= 4;
	if (count > *lenp)
		count = *lenp;

	for (i = 0; i < count; i++)
		nodes[i] = fdt_node_offset_by_phandle(fdt_addr,
						      fdt32_to_cpu(phandles[i]));
	*lenp = count;
	return 0;
}

/**
 * Given a FDT node return the CPU node number
 *
 * @param[in]	fdt_addr	Address of FDT
 * @param	node		FDT node number
 *
 * @return	CPU node number or error if negative
 */
int cvmx_fdt_get_cpu_node(const void *fdt_addr, int node)
{
	int parent = node;
	const u32 *ranges;
	int len = 0;

	while (fdt_node_check_compatible(fdt_addr, parent, "simple-bus") != 0) {
		parent = fdt_parent_offset(fdt_addr, parent);
		if (parent < 0)
			return parent;
	}
	ranges = fdt_getprop(fdt_addr, parent, "ranges", &len);
	if (!ranges)
		return len;

	if (len == 0)
		return 0;

	if (len < 24)
		return -FDT_ERR_TRUNCATED;

	return fdt32_to_cpu(ranges[2]) / 0x10;
}

/**
 * Get the total size of the flat device tree
 *
 * @param[in]	fdt_addr	Address of FDT
 *
 * @return	Size of flat device tree in bytes or error if negative.
 */
int cvmx_fdt_get_fdt_size(const void *fdt_addr)
{
	int rc;

	rc = fdt_check_header(fdt_addr);
	if (rc)
		return rc;
	return fdt_totalsize(fdt_addr);
}

/**
 * Returns if a node is compatible with one of the items in the string list
 *
 * @param[in]	fdt_addr	Pointer to flat device tree
 * @param	node		Node offset to check
 * @param[in]	strlist		Array of FDT device compatibility strings,
 *				must end with NULL or empty string.
 *
 * @return	0 if at least one item matches, 1 if no matches
 */
int cvmx_fdt_node_check_compatible_list(const void *fdt_addr, int node, const char *const *strlist)
{
	while (*strlist && **strlist) {
		if (!fdt_node_check_compatible(fdt_addr, node, *strlist))
			return 0;
		strlist++;
	}
	return 1;
}

/**
 * Given a FDT node, return the next compatible node.
 *
 * @param[in]	fdt_addr	Pointer to flat device tree
 * @param	start_offset	Starting node offset or -1 to find the first
 * @param	strlist		Array of FDT device compatibility strings, must
 *				end with NULL or empty string.
 *
 * @return	next matching node or -1 if no more matches.
 */
int cvmx_fdt_node_offset_by_compatible_list(const void *fdt_addr, int startoffset,
					    const char *const *strlist)
{
	int offset;

	for (offset = fdt_next_node(fdt_addr, startoffset, NULL); offset >= 0;
	     offset = fdt_next_node(fdt_addr, offset, NULL)) {
		if (!cvmx_fdt_node_check_compatible_list(fdt_addr, offset, strlist))
			return offset;
	}
	return -1;
}

/**
 * Attaches a PHY to a SFP or QSFP.
 *
 * @param	sfp		sfp to attach PHY to
 * @param	phy_info	phy descriptor to attach or NULL to detach
 */
void cvmx_sfp_attach_phy(struct cvmx_fdt_sfp_info *sfp, struct cvmx_phy_info *phy_info)
{
	sfp->phy_info = phy_info;
	if (phy_info)
		phy_info->sfp_info = sfp;
}

/**
 * Assigns an IPD port to a SFP slot
 *
 * @param	sfp		Handle to SFP data structure
 * @param	ipd_port	Port to assign it to
 *
 * @return	0 for success, -1 on error
 */
int cvmx_sfp_set_ipd_port(struct cvmx_fdt_sfp_info *sfp, int ipd_port)
{
	int i;

	if (sfp->is_qsfp) {
		int xiface;
		cvmx_helper_interface_mode_t mode;

		xiface = cvmx_helper_get_interface_num(ipd_port);
		mode = cvmx_helper_interface_get_mode(xiface);
		sfp->ipd_port[0] = ipd_port;

		switch (mode) {
		case CVMX_HELPER_INTERFACE_MODE_SGMII:
		case CVMX_HELPER_INTERFACE_MODE_XFI:
		case CVMX_HELPER_INTERFACE_MODE_10G_KR:
			for (i = 1; i < 4; i++)
				sfp->ipd_port[i] = cvmx_helper_get_ipd_port(xiface, i);
			break;
		case CVMX_HELPER_INTERFACE_MODE_XLAUI:
		case CVMX_HELPER_INTERFACE_MODE_40G_KR4:
			sfp->ipd_port[0] = ipd_port;
			for (i = 1; i < 4; i++)
				sfp->ipd_port[i] = -1;
			break;
		default:
			debug("%s: Interface mode %s for interface 0x%x, ipd_port %d not supported for QSFP\n",
			      __func__, cvmx_helper_interface_mode_to_string(mode), xiface,
			      ipd_port);
			return -1;
		}
	} else {
		sfp->ipd_port[0] = ipd_port;
		for (i = 1; i < 4; i++)
			sfp->ipd_port[i] = -1;
	}
	return 0;
}

/**
 * Parses all of the channels assigned to a VSC7224 device
 *
 * @param[in]		fdt_addr	Address of flat device tree
 * @param		of_offset	Offset of vsc7224 node
 * @param[in,out]	vsc7224		Data structure to hold the data
 *
 * @return	0 for success, -1 on error
 */
static int cvmx_fdt_parse_vsc7224_channels(const void *fdt_addr, int of_offset,
					   struct cvmx_vsc7224 *vsc7224)
{
	int parent_offset = of_offset;
	int err = 0;
	int reg;
	int num_chan = 0;
	struct cvmx_vsc7224_chan *channel;
	struct cvmx_fdt_sfp_info *sfp_info;
	int len;
	int num_taps;
	int i;
	const u32 *tap_values;
	int of_mac;
	int xiface, index;
	bool is_tx;
	bool is_qsfp;
	const char *mac_str;

	debug("%s(%p, %d, %s)\n", __func__, fdt_addr, of_offset, vsc7224->name);
	do {
		/* Walk through all channels */
		of_offset = fdt_node_offset_by_compatible(fdt_addr, of_offset,
							  "vitesse,vsc7224-channel");
		if (of_offset == -FDT_ERR_NOTFOUND) {
			break;
		} else if (of_offset < 0) {
			debug("%s: Failed finding compatible channel\n",
			      __func__);
			err = -1;
			break;
		}
		if (fdt_parent_offset(fdt_addr, of_offset) != parent_offset)
			break;
		reg = cvmx_fdt_get_int(fdt_addr, of_offset, "reg", -1);
		if (reg < 0 || reg > 3) {
			debug("%s: channel reg is either not present or out of range\n",
			      __func__);
			err = -1;
			break;
		}
		is_tx = cvmx_fdt_get_bool(fdt_addr, of_offset, "direction-tx");

		debug("%s(%s): Adding %cx channel %d\n",
		      __func__, vsc7224->name, is_tx ? 't' : 'r',
		      reg);
		tap_values = (const uint32_t *)fdt_getprop(fdt_addr, of_offset, "taps", &len);
		if (!tap_values) {
			debug("%s: Error: no taps defined for vsc7224 channel %d\n",
			      __func__, reg);
			err = -1;
			break;
		}

		if (vsc7224->channel[reg]) {
			debug("%s: Error: channel %d already assigned at %p\n",
			      __func__, reg,
			      vsc7224->channel[reg]);
			err = -1;
			break;
		}
		if (len % 16) {
			debug("%s: Error: tap format error for channel %d\n",
			      __func__, reg);
			err = -1;
			break;
		}
		num_taps = len / 16;
		debug("%s: Adding %d taps\n", __func__, num_taps);

		channel = __cvmx_fdt_alloc(sizeof(*channel) +
					   num_taps * sizeof(struct cvmx_vsc7224_tap));
		if (!channel) {
			debug("%s: Out of memory\n", __func__);
			err = -1;
			break;
		}
		vsc7224->channel[reg] = channel;
		channel->num_taps = num_taps;
		channel->lane = reg;
		channel->of_offset = of_offset;
		channel->is_tx = is_tx;
		channel->pretap_disable = cvmx_fdt_get_bool(fdt_addr, of_offset, "pretap-disable");
		channel->posttap_disable =
			cvmx_fdt_get_bool(fdt_addr, of_offset, "posttap-disable");
		channel->vsc7224 = vsc7224;
		/* Read all the tap values */
		for (i = 0; i < num_taps; i++) {
			channel->taps[i].len = fdt32_to_cpu(tap_values[i * 4 + 0]);
			channel->taps[i].main_tap = fdt32_to_cpu(tap_values[i * 4 + 1]);
			channel->taps[i].pre_tap = fdt32_to_cpu(tap_values[i * 4 + 2]);
			channel->taps[i].post_tap = fdt32_to_cpu(tap_values[i * 4 + 3]);
			debug("%s: tap %d: len: %d, main_tap: 0x%x, pre_tap: 0x%x, post_tap: 0x%x\n",
			      __func__, i, channel->taps[i].len, channel->taps[i].main_tap,
			      channel->taps[i].pre_tap, channel->taps[i].post_tap);
		}
		/* Now find out which interface it's mapped to */
		channel->ipd_port = -1;

		mac_str = "sfp-mac";
		if (fdt_getprop(fdt_addr, of_offset, mac_str, NULL)) {
			is_qsfp = false;
		} else if (fdt_getprop(fdt_addr, of_offset, "qsfp-mac", NULL)) {
			is_qsfp = true;
			mac_str = "qsfp-mac";
		} else {
			debug("%s: Error: MAC not found for %s channel %d\n", __func__,
			      vsc7224->name, reg);
			return -1;
		}
		of_mac = cvmx_fdt_lookup_phandle(fdt_addr, of_offset, mac_str);
		if (of_mac < 0) {
			debug("%s: Error %d with MAC %s phandle for %s\n", __func__, of_mac,
			      mac_str, vsc7224->name);
			return -1;
		}

		debug("%s: Found mac at offset %d\n", __func__, of_mac);
		err = cvmx_helper_cfg_get_xiface_index_by_fdt_node_offset(of_mac, &xiface, &index);
		if (!err) {
			channel->xiface = xiface;
			channel->index = index;
			channel->ipd_port = cvmx_helper_get_ipd_port(xiface, index);

			debug("%s: Found MAC, xiface: 0x%x, index: %d, ipd port: %d\n", __func__,
			      xiface, index, channel->ipd_port);
			if (channel->ipd_port >= 0) {
				cvmx_helper_cfg_set_vsc7224_chan_info(xiface, index, channel);
				debug("%s: Storing config channel for xiface 0x%x, index %d\n",
				      __func__, xiface, index);
			}
			sfp_info = cvmx_helper_cfg_get_sfp_info(xiface, index);
			if (!sfp_info) {
				debug("%s: Warning: no (Q)SFP+ slot found for xinterface 0x%x, index %d for channel %d\n",
				      __func__, xiface, index, channel->lane);
				continue;
			}

			/* Link it */
			channel->next = sfp_info->vsc7224_chan;
			if (sfp_info->vsc7224_chan)
				sfp_info->vsc7224_chan->prev = channel;
			sfp_info->vsc7224_chan = channel;
			sfp_info->is_vsc7224 = true;
			debug("%s: Registering VSC7224 %s channel %d with SFP %s\n", __func__,
			      vsc7224->name, channel->lane, sfp_info->name);
			if (!sfp_info->mod_abs_changed) {
				debug("%s: Registering cvmx_sfp_vsc7224_mod_abs_changed at %p for xinterface 0x%x, index %d\n",
				      __func__, &cvmx_sfp_vsc7224_mod_abs_changed, xiface, index);
				cvmx_sfp_register_mod_abs_changed(
					sfp_info,
					&cvmx_sfp_vsc7224_mod_abs_changed,
					NULL);
			}
		}
	} while (!err && num_chan < 4);

	return err;
}

/**
 * @INTERNAL
 * Parses all instances of the Vitesse VSC7224 reclocking chip
 *
 * @param[in]	fdt_addr	Address of flat device tree
 *
 * @return	0 for success, error otherwise
 */
int __cvmx_fdt_parse_vsc7224(const void *fdt_addr)
{
	int of_offset = -1;
	struct cvmx_vsc7224 *vsc7224 = NULL;
	struct cvmx_fdt_gpio_info *gpio_info = NULL;
	int err = 0;
	int of_parent;
	static bool parsed;

	debug("%s(%p)\n", __func__, fdt_addr);

	if (parsed) {
		debug("%s: Already parsed\n", __func__);
		return 0;
	}
	do {
		of_offset = fdt_node_offset_by_compatible(fdt_addr, of_offset,
							  "vitesse,vsc7224");
		debug("%s: of_offset: %d\n", __func__, of_offset);
		if (of_offset == -FDT_ERR_NOTFOUND) {
			break;
		} else if (of_offset < 0) {
			err = -1;
			debug("%s: Error %d parsing FDT\n",
			      __func__, of_offset);
			break;
		}

		vsc7224 = __cvmx_fdt_alloc(sizeof(*vsc7224));

		if (!vsc7224) {
			debug("%s: Out of memory!\n", __func__);
			return -1;
		}
		vsc7224->of_offset = of_offset;
		vsc7224->i2c_addr = cvmx_fdt_get_int(fdt_addr, of_offset,
						     "reg", -1);
		of_parent = fdt_parent_offset(fdt_addr, of_offset);
		vsc7224->i2c_bus = cvmx_fdt_get_i2c_bus(fdt_addr, of_parent);
		if (vsc7224->i2c_addr < 0) {
			debug("%s: Error: reg field missing\n", __func__);
			err = -1;
			break;
		}
		if (!vsc7224->i2c_bus) {
			debug("%s: Error getting i2c bus\n", __func__);
			err = -1;
			break;
		}
		vsc7224->name = fdt_get_name(fdt_addr, of_offset, NULL);
		debug("%s: Adding %s\n", __func__, vsc7224->name);
		if (fdt_getprop(fdt_addr, of_offset, "reset", NULL)) {
			gpio_info = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "reset");
			vsc7224->reset_gpio = gpio_info;
		}
		if (fdt_getprop(fdt_addr, of_offset, "los", NULL)) {
			gpio_info = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "los");
			vsc7224->los_gpio = gpio_info;
		}
		debug("%s: Parsing channels\n", __func__);
		err = cvmx_fdt_parse_vsc7224_channels(fdt_addr, of_offset, vsc7224);
		if (err) {
			debug("%s: Error parsing VSC7224 channels\n", __func__);
			break;
		}
	} while (of_offset > 0);

	if (err) {
		debug("%s(): Error\n", __func__);
		if (vsc7224) {
			if (vsc7224->reset_gpio)
				__cvmx_fdt_free(vsc7224->reset_gpio, sizeof(*vsc7224->reset_gpio));
			if (vsc7224->los_gpio)
				__cvmx_fdt_free(vsc7224->los_gpio, sizeof(*vsc7224->los_gpio));
			if (vsc7224->i2c_bus)
				cvmx_fdt_free_i2c_bus(vsc7224->i2c_bus);
			__cvmx_fdt_free(vsc7224, sizeof(*vsc7224));
		}
	}
	if (!err)
		parsed = true;

	return err;
}

/**
 * @INTERNAL
 * Parses all instances of the Avago AVSP5410 gearbox phy
 *
 * @param[in]	fdt_addr	Address of flat device tree
 *
 * @return	0 for success, error otherwise
 */
int __cvmx_fdt_parse_avsp5410(const void *fdt_addr)
{
	int of_offset = -1;
	struct cvmx_avsp5410 *avsp5410 = NULL;
	struct cvmx_fdt_sfp_info *sfp_info;
	int err = 0;
	int of_parent;
	static bool parsed;
	int of_mac;
	int xiface, index;
	bool is_qsfp;
	const char *mac_str;

	debug("%s(%p)\n", __func__, fdt_addr);

	if (parsed) {
		debug("%s: Already parsed\n", __func__);
		return 0;
	}

	do {
		of_offset = fdt_node_offset_by_compatible(fdt_addr, of_offset,
							  "avago,avsp-5410");
		debug("%s: of_offset: %d\n", __func__, of_offset);
		if (of_offset == -FDT_ERR_NOTFOUND) {
			break;
		} else if (of_offset < 0) {
			err = -1;
			debug("%s: Error %d parsing FDT\n", __func__, of_offset);
			break;
		}

		avsp5410 = __cvmx_fdt_alloc(sizeof(*avsp5410));

		if (!avsp5410) {
			debug("%s: Out of memory!\n", __func__);
			return -1;
		}
		avsp5410->of_offset = of_offset;
		avsp5410->i2c_addr = cvmx_fdt_get_int(fdt_addr, of_offset,
						      "reg", -1);
		of_parent = fdt_parent_offset(fdt_addr, of_offset);
		avsp5410->i2c_bus = cvmx_fdt_get_i2c_bus(fdt_addr, of_parent);
		if (avsp5410->i2c_addr < 0) {
			debug("%s: Error: reg field missing\n", __func__);
			err = -1;
			break;
		}
		if (!avsp5410->i2c_bus) {
			debug("%s: Error getting i2c bus\n", __func__);
			err = -1;
			break;
		}
		avsp5410->name = fdt_get_name(fdt_addr, of_offset, NULL);
		debug("%s: Adding %s\n", __func__, avsp5410->name);

		/* Now find out which interface it's mapped to */
		avsp5410->ipd_port = -1;

		mac_str = "sfp-mac";
		if (fdt_getprop(fdt_addr, of_offset, mac_str, NULL)) {
			is_qsfp = false;
		} else if (fdt_getprop(fdt_addr, of_offset, "qsfp-mac", NULL)) {
			is_qsfp = true;
			mac_str = "qsfp-mac";
		} else {
			debug("%s: Error: MAC not found for %s\n", __func__, avsp5410->name);
			return -1;
		}
		of_mac = cvmx_fdt_lookup_phandle(fdt_addr, of_offset, mac_str);
		if (of_mac < 0) {
			debug("%s: Error %d with MAC %s phandle for %s\n", __func__, of_mac,
			      mac_str, avsp5410->name);
			return -1;
		}

		debug("%s: Found mac at offset %d\n", __func__, of_mac);
		err = cvmx_helper_cfg_get_xiface_index_by_fdt_node_offset(of_mac, &xiface, &index);
		if (!err) {
			avsp5410->xiface = xiface;
			avsp5410->index = index;
			avsp5410->ipd_port = cvmx_helper_get_ipd_port(xiface, index);

			debug("%s: Found MAC, xiface: 0x%x, index: %d, ipd port: %d\n", __func__,
			      xiface, index, avsp5410->ipd_port);
			if (avsp5410->ipd_port >= 0) {
				cvmx_helper_cfg_set_avsp5410_info(xiface, index, avsp5410);
				debug("%s: Storing config phy for xiface 0x%x, index %d\n",
				      __func__, xiface, index);
			}
			sfp_info = cvmx_helper_cfg_get_sfp_info(xiface, index);
			if (!sfp_info) {
				debug("%s: Warning: no (Q)SFP+ slot found for xinterface 0x%x, index %d\n",
				      __func__, xiface, index);
				continue;
			}

			sfp_info->is_avsp5410 = true;
			sfp_info->avsp5410 = avsp5410;
			debug("%s: Registering AVSP5410 %s with SFP %s\n", __func__, avsp5410->name,
			      sfp_info->name);
			if (!sfp_info->mod_abs_changed) {
				debug("%s: Registering cvmx_sfp_avsp5410_mod_abs_changed at %p for xinterface 0x%x, index %d\n",
				      __func__, &cvmx_sfp_avsp5410_mod_abs_changed, xiface, index);
				cvmx_sfp_register_mod_abs_changed(
					sfp_info,
					&cvmx_sfp_avsp5410_mod_abs_changed,
					NULL);
			}
		}
	} while (of_offset > 0);

	if (err) {
		debug("%s(): Error\n", __func__);
		if (avsp5410) {
			if (avsp5410->i2c_bus)
				cvmx_fdt_free_i2c_bus(avsp5410->i2c_bus);
			__cvmx_fdt_free(avsp5410, sizeof(*avsp5410));
		}
	}
	if (!err)
		parsed = true;

	return err;
}

/**
 * Parse QSFP GPIOs for SFP
 *
 * @param[in]	fdt_addr	Pointer to flat device tree
 * @param	of_offset	Offset of QSFP node
 * @param[out]	sfp_info	Pointer to sfp info to fill in
 *
 * @return	0 for success
 */
static int cvmx_parse_qsfp(const void *fdt_addr, int of_offset, struct cvmx_fdt_sfp_info *sfp_info)
{
	sfp_info->select = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "select");
	sfp_info->mod_abs = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "mod_prs");
	sfp_info->reset = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "reset");
	sfp_info->interrupt = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "interrupt");
	sfp_info->lp_mode = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "lp_mode");
	return 0;
}

/**
 * Parse SFP GPIOs for SFP
 *
 * @param[in]	fdt_addr	Pointer to flat device tree
 * @param	of_offset	Offset of SFP node
 * @param[out]	sfp_info	Pointer to sfp info to fill in
 *
 * @return	0 for success
 */
static int cvmx_parse_sfp(const void *fdt_addr, int of_offset, struct cvmx_fdt_sfp_info *sfp_info)
{
	sfp_info->mod_abs = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "mod_abs");
	sfp_info->rx_los = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "rx_los");
	sfp_info->tx_disable = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "tx_disable");
	sfp_info->tx_error = cvmx_fdt_gpio_get_info_phandle(fdt_addr, of_offset, "tx_error");
	return 0;
}

/**
 * Parse SFP/QSFP EEPROM and diag
 *
 * @param[in]	fdt_addr	Pointer to flat device tree
 * @param	of_offset	Offset of SFP node
 * @param[out]	sfp_info	Pointer to sfp info to fill in
 *
 * @return	0 for success, -1 on error
 */
static int cvmx_parse_sfp_eeprom(const void *fdt_addr, int of_offset,
				 struct cvmx_fdt_sfp_info *sfp_info)
{
	int of_eeprom;
	int of_diag;

	debug("%s(%p, %d, %s)\n", __func__, fdt_addr, of_offset, sfp_info->name);
	of_eeprom = cvmx_fdt_lookup_phandle(fdt_addr, of_offset, "eeprom");
	if (of_eeprom < 0) {
		debug("%s: Missing \"eeprom\" from device tree for %s\n", __func__, sfp_info->name);
		return -1;
	}

	sfp_info->i2c_bus = cvmx_fdt_get_i2c_bus(fdt_addr, fdt_parent_offset(fdt_addr, of_eeprom));
	sfp_info->i2c_eeprom_addr = cvmx_fdt_get_int(fdt_addr, of_eeprom, "reg", 0x50);

	debug("%s(%p, %d, %s, %d)\n", __func__, fdt_addr, of_offset, sfp_info->name,
	      sfp_info->i2c_eeprom_addr);

	if (!sfp_info->i2c_bus) {
		debug("%s: Error: could not determine i2c bus for eeprom for %s\n", __func__,
		      sfp_info->name);
		return -1;
	}
	of_diag = cvmx_fdt_lookup_phandle(fdt_addr, of_offset, "diag");
	if (of_diag >= 0)
		sfp_info->i2c_diag_addr = cvmx_fdt_get_int(fdt_addr, of_diag, "reg", 0x51);
	else
		sfp_info->i2c_diag_addr = 0x51;
	return 0;
}

/**
 * Parse SFP information from device tree
 *
 * @param[in]	fdt_addr	Address of flat device tree
 *
 * @return pointer to sfp info or NULL if error
 */
struct cvmx_fdt_sfp_info *cvmx_helper_fdt_parse_sfp_info(const void *fdt_addr, int of_offset)
{
	struct cvmx_fdt_sfp_info *sfp_info = NULL;
	int err = -1;
	bool is_qsfp;

	if (!fdt_node_check_compatible(fdt_addr, of_offset, "ethernet,sfp-slot")) {
		is_qsfp = false;
	} else if (!fdt_node_check_compatible(fdt_addr, of_offset, "ethernet,qsfp-slot")) {
		is_qsfp = true;
	} else {
		debug("%s: Error: incompatible sfp/qsfp slot, compatible=%s\n", __func__,
		      (char *)fdt_getprop(fdt_addr, of_offset, "compatible", NULL));
		goto error_exit;
	}

	debug("%s: %ssfp module found at offset %d\n", __func__, is_qsfp ? "q" : "", of_offset);
	sfp_info = __cvmx_fdt_alloc(sizeof(*sfp_info));
	if (!sfp_info) {
		debug("%s: Error: out of memory\n", __func__);
		goto error_exit;
	}
	sfp_info->name = fdt_get_name(fdt_addr, of_offset, NULL);
	sfp_info->of_offset = of_offset;
	sfp_info->is_qsfp = is_qsfp;
	sfp_info->last_mod_abs = -1;
	sfp_info->last_rx_los = -1;

	if (is_qsfp)
		err = cvmx_parse_qsfp(fdt_addr, of_offset, sfp_info);
	else
		err = cvmx_parse_sfp(fdt_addr, of_offset, sfp_info);
	if (err) {
		debug("%s: Error in %s parsing %ssfp GPIO info\n", __func__, sfp_info->name,
		      is_qsfp ? "q" : "");
		goto error_exit;
	}
	debug("%s: Parsing %ssfp module eeprom\n", __func__, is_qsfp ? "q" : "");
	err = cvmx_parse_sfp_eeprom(fdt_addr, of_offset, sfp_info);
	if (err) {
		debug("%s: Error parsing eeprom info for %s\n", __func__, sfp_info->name);
		goto error_exit;
	}

	/* Register default check for mod_abs changed */
	if (!err)
		cvmx_sfp_register_check_mod_abs(sfp_info, cvmx_sfp_check_mod_abs, NULL);

error_exit:
	/* Note: we don't free any data structures on error since it gets
	 * rather complicated with i2c buses and whatnot.
	 */
	return err ? NULL : sfp_info;
}

/**
 * @INTERNAL
 * Parse a slice of the Inphi/Cortina CS4343 in the device tree
 *
 * @param[in]	fdt_addr	Address of flat device tree
 * @param	of_offset	fdt offset of slice
 * @param	phy_info	phy_info data structure
 *
 * @return	slice number if non-negative, otherwise error
 */
static int cvmx_fdt_parse_cs4343_slice(const void *fdt_addr, int of_offset,
				       struct cvmx_phy_info *phy_info)
{
	struct cvmx_cs4343_slice_info *slice;
	int reg;
	int reg_offset;

	reg = cvmx_fdt_get_int(fdt_addr, of_offset, "reg", -1);
	reg_offset = cvmx_fdt_get_int(fdt_addr, of_offset, "slice_offset", -1);

	if (reg < 0 || reg >= 4) {
		debug("%s(%p, %d, %p): Error: reg %d undefined or out of range\n", __func__,
		      fdt_addr, of_offset, phy_info, reg);
		return -1;
	}
	if (reg_offset % 0x1000 || reg_offset > 0x3000 || reg_offset < 0) {
		debug("%s(%p, %d, %p): Error: reg_offset 0x%x undefined or out of range\n",
		      __func__, fdt_addr, of_offset, phy_info, reg_offset);
		return -1;
	}
	if (!phy_info->cs4343_info) {
		debug("%s: Error: phy info cs4343 datastructure is NULL\n", __func__);
		return -1;
	}
	debug("%s(%p, %d, %p): %s, reg: %d, slice offset: 0x%x\n", __func__, fdt_addr, of_offset,
	      phy_info, fdt_get_name(fdt_addr, of_offset, NULL), reg, reg_offset);
	slice = &phy_info->cs4343_info->slice[reg];
	slice->name = fdt_get_name(fdt_addr, of_offset, NULL);
	slice->mphy = phy_info->cs4343_info;
	slice->phy_info = phy_info;
	slice->of_offset = of_offset;
	slice->slice_no = reg;
	slice->reg_offset = reg_offset;
	/* SR settings */
	slice->sr_stx_cmode_res = cvmx_fdt_get_int(fdt_addr, of_offset, "sr-stx-cmode-res", 3);
	slice->sr_stx_drv_lower_cm =
		cvmx_fdt_get_int(fdt_addr, of_offset, "sr-stx-drv-lower-cm", 8);
	slice->sr_stx_level = cvmx_fdt_get_int(fdt_addr, of_offset, "sr-stx-level", 0x1c);
	slice->sr_stx_pre_peak = cvmx_fdt_get_int(fdt_addr, of_offset, "sr-stx-pre-peak", 1);
	slice->sr_stx_muxsubrate_sel =
		cvmx_fdt_get_int(fdt_addr, of_offset, "sr-stx-muxsubrate-sel", 0);
	slice->sr_stx_post_peak = cvmx_fdt_get_int(fdt_addr, of_offset, "sr-stx-post-peak", 8);
	/* CX settings */
	slice->cx_stx_cmode_res = cvmx_fdt_get_int(fdt_addr, of_offset, "cx-stx-cmode-res", 3);
	slice->cx_stx_drv_lower_cm =
		cvmx_fdt_get_int(fdt_addr, of_offset, "cx-stx-drv-lower-cm", 8);
	slice->cx_stx_level = cvmx_fdt_get_int(fdt_addr, of_offset, "cx-stx-level", 0x1c);
	slice->cx_stx_pre_peak = cvmx_fdt_get_int(fdt_addr, of_offset, "cx-stx-pre-peak", 1);
	slice->cx_stx_muxsubrate_sel =
		cvmx_fdt_get_int(fdt_addr, of_offset, "cx-stx-muxsubrate-sel", 0);
	slice->cx_stx_post_peak = cvmx_fdt_get_int(fdt_addr, of_offset, "cx-stx-post-peak", 0xC);
	/* 1000Base-X settings */
	/* CX settings */
	slice->basex_stx_cmode_res =
		cvmx_fdt_get_int(fdt_addr, of_offset, "basex-stx-cmode-res", 3);
	slice->basex_stx_drv_lower_cm =
		cvmx_fdt_get_int(fdt_addr, of_offset, "basex-stx-drv-lower-cm", 8);
	slice->basex_stx_level = cvmx_fdt_get_int(fdt_addr, of_offset,
						  "basex-stx-level", 0x1c);
	slice->basex_stx_pre_peak = cvmx_fdt_get_int(fdt_addr, of_offset,
						     "basex-stx-pre-peak", 1);
	slice->basex_stx_muxsubrate_sel =
		cvmx_fdt_get_int(fdt_addr, of_offset,
				 "basex-stx-muxsubrate-sel", 0);
	slice->basex_stx_post_peak =
		cvmx_fdt_get_int(fdt_addr, of_offset, "basex-stx-post-peak", 8);
	/* Get the link LED gpio pin */
	slice->link_gpio = cvmx_fdt_get_int(fdt_addr, of_offset,
					    "link-led-gpio", -1);
	slice->error_gpio = cvmx_fdt_get_int(fdt_addr, of_offset,
					     "error-led-gpio", -1);
	slice->los_gpio = cvmx_fdt_get_int(fdt_addr, of_offset,
					   "los-input-gpio", -1);
	slice->link_inverted = cvmx_fdt_get_bool(fdt_addr, of_offset,
						 "link-led-gpio-inverted");
	slice->error_inverted = cvmx_fdt_get_bool(fdt_addr, of_offset,
						  "error-led-gpio-inverted");
	slice->los_inverted = cvmx_fdt_get_bool(fdt_addr, of_offset,
						"los-input-gpio-inverted");
	/* Convert GPIOs to be die based if they're not already */
	if (slice->link_gpio > 4 && slice->link_gpio <= 8)
		slice->link_gpio -= 4;
	if (slice->error_gpio > 4 && slice->error_gpio <= 8)
		slice->error_gpio -= 4;
	if (slice->los_gpio > 4 && slice->los_gpio <= 8)
		slice->los_gpio -= 4;

	return reg;
}

/**
 * @INTERNAL
 * Parses either a CS4343 phy or a slice of the phy from the device tree
 * @param[in]	fdt_addr	Address of FDT
 * @param	of_offset	offset of slice or phy in device tree
 * @param	phy_info	phy_info data structure to fill in
 *
 * @return	0 for success, -1 on error
 */
int cvmx_fdt_parse_cs4343(const void *fdt_addr, int of_offset, struct cvmx_phy_info *phy_info)
{
	int of_slice = -1;
	struct cvmx_cs4343_info *cs4343;
	int err = -1;
	int reg;

	debug("%s(%p, %d, %p): %s (%s)\n", __func__,
	      fdt_addr, of_offset, phy_info,
	      fdt_get_name(fdt_addr, of_offset, NULL),
	      (const char *)fdt_getprop(fdt_addr, of_offset, "compatible", NULL));

	if (!phy_info->cs4343_info)
		phy_info->cs4343_info = __cvmx_fdt_alloc(sizeof(struct cvmx_cs4343_info));
	if (!phy_info->cs4343_info) {
		debug("%s: Error: out of memory!\n", __func__);
		return -1;
	}
	cs4343 = phy_info->cs4343_info;
	/* If we're passed to a slice then process only that slice */
	if (!fdt_node_check_compatible(fdt_addr, of_offset, "cortina,cs4343-slice")) {
		err = 0;
		of_slice = of_offset;
		of_offset = fdt_parent_offset(fdt_addr, of_offset);
		reg = cvmx_fdt_parse_cs4343_slice(fdt_addr, of_slice, phy_info);
		if (reg >= 0)
			phy_info->cs4343_slice_info = &cs4343->slice[reg];
		else
			err = reg;
	} else if (!fdt_node_check_compatible(fdt_addr, of_offset,
					      "cortina,cs4343")) {
		/* Walk through and process all of the slices */
		of_slice =
			fdt_node_offset_by_compatible(fdt_addr, of_offset, "cortina,cs4343-slice");
		while (of_slice > 0 && fdt_parent_offset(fdt_addr, of_slice) ==
		       of_offset) {
			debug("%s: Parsing slice %s\n", __func__,
			      fdt_get_name(fdt_addr, of_slice, NULL));
			err = cvmx_fdt_parse_cs4343_slice(fdt_addr, of_slice,
							  phy_info);
			if (err < 0)
				break;
			of_slice = fdt_node_offset_by_compatible(fdt_addr,
								 of_slice,
								 "cortina,cs4343-slice");
		}