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 282915609aSAndy Fleming /* 292915609aSAndy Fleming * Given the following ... 302915609aSAndy Fleming * 312915609aSAndy Fleming * 1) A pointer to an Fman Ethernet node (as identified by the 'compat' 322915609aSAndy Fleming * compatible string and 'addr' physical address) 332915609aSAndy Fleming * 342915609aSAndy Fleming * 2) The name of an alias that points to the ethernet-phy node (usually inside 352915609aSAndy Fleming * a virtual MDIO node) 362915609aSAndy Fleming * 372915609aSAndy Fleming * ... update that Ethernet node's phy-handle property to point to the 382915609aSAndy Fleming * ethernet-phy node. This is how we link an Ethernet node to its PHY, so each 392915609aSAndy Fleming * PHY in a virtual MDIO node must have an alias. 40*1fc0d594STimur Tabi * 41*1fc0d594STimur Tabi * Returns 0 on success, or a negative FDT error code on error. 422915609aSAndy Fleming */ 43*1fc0d594STimur Tabi int fdt_set_phy_handle(void *fdt, char *compat, phys_addr_t addr, 442915609aSAndy Fleming const char *alias) 452915609aSAndy Fleming { 46*1fc0d594STimur Tabi int offset; 47*1fc0d594STimur Tabi unsigned int ph; 482915609aSAndy Fleming const char *path; 492915609aSAndy Fleming 502915609aSAndy Fleming /* Get a path to the node that 'alias' points to */ 512915609aSAndy Fleming path = fdt_get_alias(fdt, alias); 52*1fc0d594STimur Tabi if (!path) 53*1fc0d594STimur Tabi return -FDT_ERR_BADPATH; 542915609aSAndy Fleming 55*1fc0d594STimur Tabi /* Get the offset of that node */ 56*1fc0d594STimur Tabi offset = fdt_path_offset(fdt, path); 57*1fc0d594STimur Tabi if (offset < 0) 58*1fc0d594STimur Tabi return offset; 59*1fc0d594STimur Tabi 60*1fc0d594STimur Tabi ph = fdt_create_phandle(fdt, offset); 61*1fc0d594STimur Tabi if (!ph) 62*1fc0d594STimur Tabi return -FDT_ERR_BADPHANDLE; 632915609aSAndy Fleming 642915609aSAndy Fleming offset = fdt_node_offset_by_compat_reg(fdt, compat, addr); 65*1fc0d594STimur Tabi if (offset < 0) 66*1fc0d594STimur Tabi return offset; 67*1fc0d594STimur Tabi 68*1fc0d594STimur Tabi return fdt_setprop(fdt, offset, "phy-handle", &ph, sizeof(ph)); 692915609aSAndy Fleming } 70