blob: 6ddf816201219f14efc37fa83c907d54f8ee5f3e [file] [log] [blame]
Andy Fleming29156092010-10-20 15:35:16 -05001/*
2 * Copyright 2011 Freescale Semiconductor, Inc.
3 *
4 * See file CREDITS for list of people who contributed to this
5 * project.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
23#include <common.h>
24#include <libfdt.h>
25#include <libfdt_env.h>
26#include <fdt_support.h>
27
28/*
29 * Given the following ...
30 *
31 * 1) A pointer to an Fman Ethernet node (as identified by the 'compat'
32 * compatible string and 'addr' physical address)
33 *
34 * 2) The name of an alias that points to the ethernet-phy node (usually inside
35 * a virtual MDIO node)
36 *
37 * ... update that Ethernet node's phy-handle property to point to the
38 * ethernet-phy node. This is how we link an Ethernet node to its PHY, so each
39 * PHY in a virtual MDIO node must have an alias.
Timur Tabi1fc0d592012-05-04 12:21:28 +000040 *
41 * Returns 0 on success, or a negative FDT error code on error.
Andy Fleming29156092010-10-20 15:35:16 -050042 */
Timur Tabi1fc0d592012-05-04 12:21:28 +000043int fdt_set_phy_handle(void *fdt, char *compat, phys_addr_t addr,
Andy Fleming29156092010-10-20 15:35:16 -050044 const char *alias)
45{
Timur Tabi1fc0d592012-05-04 12:21:28 +000046 int offset;
47 unsigned int ph;
Andy Fleming29156092010-10-20 15:35:16 -050048 const char *path;
49
50 /* Get a path to the node that 'alias' points to */
51 path = fdt_get_alias(fdt, alias);
Timur Tabi1fc0d592012-05-04 12:21:28 +000052 if (!path)
53 return -FDT_ERR_BADPATH;
Andy Fleming29156092010-10-20 15:35:16 -050054
Timur Tabi1fc0d592012-05-04 12:21:28 +000055 /* Get the offset of that node */
56 offset = fdt_path_offset(fdt, path);
57 if (offset < 0)
58 return offset;
59
60 ph = fdt_create_phandle(fdt, offset);
61 if (!ph)
62 return -FDT_ERR_BADPHANDLE;
Andy Fleming29156092010-10-20 15:35:16 -050063
64 offset = fdt_node_offset_by_compat_reg(fdt, compat, addr);
Timur Tabi1fc0d592012-05-04 12:21:28 +000065 if (offset < 0)
66 return offset;
67
68 return fdt_setprop(fdt, offset, "phy-handle", &ph, sizeof(ph));
Andy Fleming29156092010-10-20 15:35:16 -050069}