xref: /rk3399_rockchip-uboot/board/freescale/common/fman.c (revision 0e00a84cdedf7a1949486746225b35984b351eca)
12915609aSAndy Fleming /*
2e8297341SShaohui Xie  * Copyright 2011-2015 Freescale Semiconductor, Inc.
32915609aSAndy Fleming  *
41a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
52915609aSAndy Fleming  */
62915609aSAndy Fleming 
72915609aSAndy Fleming #include <common.h>
8*0e00a84cSMasahiro Yamada #include <linux/libfdt.h>
9*0e00a84cSMasahiro Yamada #include <linux/libfdt_env.h>
102915609aSAndy Fleming #include <fdt_support.h>
112915609aSAndy Fleming 
1245b092d3STimur Tabi #include <fm_eth.h>
13e8297341SShaohui Xie #ifdef CONFIG_FSL_LAYERSCAPE
14e8297341SShaohui Xie #include <asm/arch/fsl_serdes.h>
15e8297341SShaohui Xie #else
1645b092d3STimur Tabi #include <asm/fsl_serdes.h>
17e8297341SShaohui Xie #endif
1845b092d3STimur Tabi 
192915609aSAndy Fleming /*
202915609aSAndy Fleming  * Given the following ...
212915609aSAndy Fleming  *
222915609aSAndy Fleming  * 1) A pointer to an Fman Ethernet node (as identified by the 'compat'
232915609aSAndy Fleming  * compatible string and 'addr' physical address)
242915609aSAndy Fleming  *
252915609aSAndy Fleming  * 2) The name of an alias that points to the ethernet-phy node (usually inside
262915609aSAndy Fleming  * a virtual MDIO node)
272915609aSAndy Fleming  *
282915609aSAndy Fleming  * ... update that Ethernet node's phy-handle property to point to the
292915609aSAndy Fleming  * ethernet-phy node.  This is how we link an Ethernet node to its PHY, so each
302915609aSAndy Fleming  * PHY in a virtual MDIO node must have an alias.
311fc0d594STimur Tabi  *
321fc0d594STimur Tabi  * Returns 0 on success, or a negative FDT error code on error.
332915609aSAndy Fleming  */
fdt_set_phy_handle(void * fdt,char * compat,phys_addr_t addr,const char * alias)341fc0d594STimur Tabi int fdt_set_phy_handle(void *fdt, char *compat, phys_addr_t addr,
352915609aSAndy Fleming 			const char *alias)
362915609aSAndy Fleming {
371fc0d594STimur Tabi 	int offset;
381fc0d594STimur Tabi 	unsigned int ph;
392915609aSAndy Fleming 	const char *path;
402915609aSAndy Fleming 
412915609aSAndy Fleming 	/* Get a path to the node that 'alias' points to */
422915609aSAndy Fleming 	path = fdt_get_alias(fdt, alias);
431fc0d594STimur Tabi 	if (!path)
441fc0d594STimur Tabi 		return -FDT_ERR_BADPATH;
452915609aSAndy Fleming 
461fc0d594STimur Tabi 	/* Get the offset of that node */
471fc0d594STimur Tabi 	offset = fdt_path_offset(fdt, path);
481fc0d594STimur Tabi 	if (offset < 0)
491fc0d594STimur Tabi 		return offset;
501fc0d594STimur Tabi 
511fc0d594STimur Tabi 	ph = fdt_create_phandle(fdt, offset);
521fc0d594STimur Tabi 	if (!ph)
531fc0d594STimur Tabi 		return -FDT_ERR_BADPHANDLE;
542915609aSAndy Fleming 
551aaf3f9aSShaohui Xie 	ph = cpu_to_fdt32(ph);
561aaf3f9aSShaohui Xie 
572915609aSAndy Fleming 	offset = fdt_node_offset_by_compat_reg(fdt, compat, addr);
581fc0d594STimur Tabi 	if (offset < 0)
591fc0d594STimur Tabi 		return offset;
601fc0d594STimur Tabi 
611fc0d594STimur Tabi 	return fdt_setprop(fdt, offset, "phy-handle", &ph, sizeof(ph));
622915609aSAndy Fleming }
6345b092d3STimur Tabi 
6445b092d3STimur Tabi /*
6545b092d3STimur Tabi  * Return the SerDes device enum for a given Fman port
6645b092d3STimur Tabi  *
6745b092d3STimur Tabi  * This function just maps the fm_port namespace to the srds_prtcl namespace.
6845b092d3STimur Tabi  */
serdes_device_from_fm_port(enum fm_port port)6945b092d3STimur Tabi enum srds_prtcl serdes_device_from_fm_port(enum fm_port port)
7045b092d3STimur Tabi {
7145b092d3STimur Tabi 	static const enum srds_prtcl srds_table[] = {
7245b092d3STimur Tabi 		[FM1_DTSEC1] = SGMII_FM1_DTSEC1,
7345b092d3STimur Tabi 		[FM1_DTSEC2] = SGMII_FM1_DTSEC2,
7445b092d3STimur Tabi 		[FM1_DTSEC3] = SGMII_FM1_DTSEC3,
7545b092d3STimur Tabi 		[FM1_DTSEC4] = SGMII_FM1_DTSEC4,
7645b092d3STimur Tabi 		[FM1_DTSEC5] = SGMII_FM1_DTSEC5,
7745b092d3STimur Tabi 		[FM1_10GEC1] = XAUI_FM1,
7845b092d3STimur Tabi 		[FM2_DTSEC1] = SGMII_FM2_DTSEC1,
7945b092d3STimur Tabi 		[FM2_DTSEC2] = SGMII_FM2_DTSEC2,
8045b092d3STimur Tabi 		[FM2_DTSEC3] = SGMII_FM2_DTSEC3,
8145b092d3STimur Tabi 		[FM2_DTSEC4] = SGMII_FM2_DTSEC4,
8245b092d3STimur Tabi 		[FM2_DTSEC5] = SGMII_FM2_DTSEC5,
8345b092d3STimur Tabi 		[FM2_10GEC1] = XAUI_FM2,
8445b092d3STimur Tabi 	};
8545b092d3STimur Tabi 
8645b092d3STimur Tabi 	if ((port < FM1_DTSEC1) || (port > FM2_10GEC1))
8745b092d3STimur Tabi 		return NONE;
8845b092d3STimur Tabi 	else
8945b092d3STimur Tabi 		return srds_table[port];
9045b092d3STimur Tabi }
91