xref: /rk3399_rockchip-uboot/board/freescale/common/fman.c (revision 45b092d3010caa8d9a1f8c4f89afcbb6c5e5bee6)
12915609aSAndy Fleming /*
22915609aSAndy Fleming  * Copyright 2011 Freescale Semiconductor, Inc.
32915609aSAndy Fleming  *
42915609aSAndy Fleming  * See file CREDITS for list of people who contributed to this
52915609aSAndy Fleming  * project.
62915609aSAndy Fleming  *
72915609aSAndy Fleming  * This program is free software; you can redistribute it and/or
82915609aSAndy Fleming  * modify it under the terms of the GNU General Public License as
92915609aSAndy Fleming  * published by the Free Software Foundation; either version 2 of
102915609aSAndy Fleming  * the License, or (at your option) any later version.
112915609aSAndy Fleming  *
122915609aSAndy Fleming  * This program is distributed in the hope that it will be useful,
132915609aSAndy Fleming  * but WITHOUT ANY WARRANTY; without even the implied warranty of
142915609aSAndy Fleming  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
152915609aSAndy Fleming  * GNU General Public License for more details.
162915609aSAndy Fleming  *
172915609aSAndy Fleming  * You should have received a copy of the GNU General Public License
182915609aSAndy Fleming  * along with this program; if not, write to the Free Software
192915609aSAndy Fleming  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
202915609aSAndy Fleming  * MA 02111-1307 USA
212915609aSAndy Fleming  */
222915609aSAndy Fleming 
232915609aSAndy Fleming #include <common.h>
242915609aSAndy Fleming #include <libfdt.h>
252915609aSAndy Fleming #include <libfdt_env.h>
262915609aSAndy Fleming #include <fdt_support.h>
272915609aSAndy Fleming 
28*45b092d3STimur Tabi #include <fm_eth.h>
29*45b092d3STimur Tabi #include <asm/fsl_serdes.h>
30*45b092d3STimur Tabi 
312915609aSAndy Fleming /*
322915609aSAndy Fleming  * Given the following ...
332915609aSAndy Fleming  *
342915609aSAndy Fleming  * 1) A pointer to an Fman Ethernet node (as identified by the 'compat'
352915609aSAndy Fleming  * compatible string and 'addr' physical address)
362915609aSAndy Fleming  *
372915609aSAndy Fleming  * 2) The name of an alias that points to the ethernet-phy node (usually inside
382915609aSAndy Fleming  * a virtual MDIO node)
392915609aSAndy Fleming  *
402915609aSAndy Fleming  * ... update that Ethernet node's phy-handle property to point to the
412915609aSAndy Fleming  * ethernet-phy node.  This is how we link an Ethernet node to its PHY, so each
422915609aSAndy Fleming  * PHY in a virtual MDIO node must have an alias.
431fc0d594STimur Tabi  *
441fc0d594STimur Tabi  * Returns 0 on success, or a negative FDT error code on error.
452915609aSAndy Fleming  */
461fc0d594STimur Tabi int fdt_set_phy_handle(void *fdt, char *compat, phys_addr_t addr,
472915609aSAndy Fleming 			const char *alias)
482915609aSAndy Fleming {
491fc0d594STimur Tabi 	int offset;
501fc0d594STimur Tabi 	unsigned int ph;
512915609aSAndy Fleming 	const char *path;
522915609aSAndy Fleming 
532915609aSAndy Fleming 	/* Get a path to the node that 'alias' points to */
542915609aSAndy Fleming 	path = fdt_get_alias(fdt, alias);
551fc0d594STimur Tabi 	if (!path)
561fc0d594STimur Tabi 		return -FDT_ERR_BADPATH;
572915609aSAndy Fleming 
581fc0d594STimur Tabi 	/* Get the offset of that node */
591fc0d594STimur Tabi 	offset = fdt_path_offset(fdt, path);
601fc0d594STimur Tabi 	if (offset < 0)
611fc0d594STimur Tabi 		return offset;
621fc0d594STimur Tabi 
631fc0d594STimur Tabi 	ph = fdt_create_phandle(fdt, offset);
641fc0d594STimur Tabi 	if (!ph)
651fc0d594STimur Tabi 		return -FDT_ERR_BADPHANDLE;
662915609aSAndy Fleming 
672915609aSAndy Fleming 	offset = fdt_node_offset_by_compat_reg(fdt, compat, addr);
681fc0d594STimur Tabi 	if (offset < 0)
691fc0d594STimur Tabi 		return offset;
701fc0d594STimur Tabi 
711fc0d594STimur Tabi 	return fdt_setprop(fdt, offset, "phy-handle", &ph, sizeof(ph));
722915609aSAndy Fleming }
73*45b092d3STimur Tabi 
74*45b092d3STimur Tabi /*
75*45b092d3STimur Tabi  * Return the SerDes device enum for a given Fman port
76*45b092d3STimur Tabi  *
77*45b092d3STimur Tabi  * This function just maps the fm_port namespace to the srds_prtcl namespace.
78*45b092d3STimur Tabi  */
79*45b092d3STimur Tabi enum srds_prtcl serdes_device_from_fm_port(enum fm_port port)
80*45b092d3STimur Tabi {
81*45b092d3STimur Tabi 	static const enum srds_prtcl srds_table[] = {
82*45b092d3STimur Tabi 		[FM1_DTSEC1] = SGMII_FM1_DTSEC1,
83*45b092d3STimur Tabi 		[FM1_DTSEC2] = SGMII_FM1_DTSEC2,
84*45b092d3STimur Tabi 		[FM1_DTSEC3] = SGMII_FM1_DTSEC3,
85*45b092d3STimur Tabi 		[FM1_DTSEC4] = SGMII_FM1_DTSEC4,
86*45b092d3STimur Tabi 		[FM1_DTSEC5] = SGMII_FM1_DTSEC5,
87*45b092d3STimur Tabi 		[FM1_10GEC1] = XAUI_FM1,
88*45b092d3STimur Tabi 		[FM2_DTSEC1] = SGMII_FM2_DTSEC1,
89*45b092d3STimur Tabi 		[FM2_DTSEC2] = SGMII_FM2_DTSEC2,
90*45b092d3STimur Tabi 		[FM2_DTSEC3] = SGMII_FM2_DTSEC3,
91*45b092d3STimur Tabi 		[FM2_DTSEC4] = SGMII_FM2_DTSEC4,
92*45b092d3STimur Tabi 		[FM2_DTSEC5] = SGMII_FM2_DTSEC5,
93*45b092d3STimur Tabi 		[FM2_10GEC1] = XAUI_FM2,
94*45b092d3STimur Tabi 	};
95*45b092d3STimur Tabi 
96*45b092d3STimur Tabi 	if ((port < FM1_DTSEC1) || (port > FM2_10GEC1))
97*45b092d3STimur Tabi 		return NONE;
98*45b092d3STimur Tabi 	else
99*45b092d3STimur Tabi 		return srds_table[port];
100*45b092d3STimur Tabi }
101