blob: d92a364365c00599eb340d877aec8620c9dda4a4 [file] [log] [blame]
Siva Durga Prasad Paladugud5c4e1e2018-11-27 11:49:11 +05301// SPDX-License-Identifier: GPL-2.0
2/*
3 * Xilinx GMII2RGMII phy driver
4 *
5 * Copyright (C) 2018 Xilinx, Inc.
6 */
7
Simon Glass3ba929a2020-10-30 21:38:53 -06008#include <common.h>
Siva Durga Prasad Paladugud5c4e1e2018-11-27 11:49:11 +05309#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Siva Durga Prasad Paladugud5c4e1e2018-11-27 11:49:11 +053011#include <phy.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Siva Durga Prasad Paladugud5c4e1e2018-11-27 11:49:11 +053013
14DECLARE_GLOBAL_DATA_PTR;
15
16#define ZYNQ_GMII2RGMII_REG 0x10
17#define ZYNQ_GMII2RGMII_SPEED_MASK (BMCR_SPEED1000 | BMCR_SPEED100)
18
19static int xilinxgmiitorgmii_config(struct phy_device *phydev)
20{
Bin Meng021e7e72021-03-14 20:14:50 +080021 ofnode node = phy_get_ofnode(phydev);
22 struct phy_device *ext_phydev;
23 struct ofnode_phandle_args phandle;
24 int ext_phyaddr = -1;
25 int ret;
Siva Durga Prasad Paladugud5c4e1e2018-11-27 11:49:11 +053026
27 debug("%s\n", __func__);
Bin Meng021e7e72021-03-14 20:14:50 +080028
Marek BehĂșn2141ad42022-04-07 00:33:05 +020029 if (phydev->interface != PHY_INTERFACE_MODE_GMII) {
30 printf("Incorrect interface type\n");
31 return -EINVAL;
32 }
33
Bin Meng021e7e72021-03-14 20:14:50 +080034 if (!ofnode_valid(node))
35 return -EINVAL;
36
37 phydev->addr = ofnode_read_u32_default(node, "reg", -1);
38 ret = ofnode_parse_phandle_with_args(node, "phy-handle",
39 NULL, 0, 0, &phandle);
40 if (ret)
41 return ret;
42
43 ext_phyaddr = ofnode_read_u32_default(phandle.node, "reg", -1);
44 ext_phydev = phy_find_by_mask(phydev->bus,
45 1 << ext_phyaddr,
46 PHY_INTERFACE_MODE_RGMII);
47 if (!ext_phydev) {
48 printf("%s, No external phy device found\n", __func__);
49 return -EINVAL;
50 }
51
52 ext_phydev->node = phandle.node;
53 phydev->priv = ext_phydev;
54
55 debug("%s, gmii2rgmmi:0x%x, extphy:0x%x\n", __func__, phydev->addr,
56 ext_phyaddr);
57
Siva Durga Prasad Paladugud5c4e1e2018-11-27 11:49:11 +053058 if (ext_phydev->drv->config)
59 ext_phydev->drv->config(ext_phydev);
60
61 return 0;
62}
63
64static int xilinxgmiitorgmii_extread(struct phy_device *phydev, int addr,
65 int devaddr, int regnum)
66{
67 struct phy_device *ext_phydev = phydev->priv;
68
69 debug("%s\n", __func__);
70 if (ext_phydev->drv->readext)
71 ext_phydev->drv->readext(ext_phydev, addr, devaddr, regnum);
72
73 return 0;
74}
75
76static int xilinxgmiitorgmii_extwrite(struct phy_device *phydev, int addr,
77 int devaddr, int regnum, u16 val)
78
79{
80 struct phy_device *ext_phydev = phydev->priv;
81
82 debug("%s\n", __func__);
83 if (ext_phydev->drv->writeext)
84 ext_phydev->drv->writeext(ext_phydev, addr, devaddr, regnum,
85 val);
86
87 return 0;
88}
89
90static int xilinxgmiitorgmii_startup(struct phy_device *phydev)
91{
92 u16 val = 0;
93 struct phy_device *ext_phydev = phydev->priv;
94
95 debug("%s\n", __func__);
96 ext_phydev->dev = phydev->dev;
97 if (ext_phydev->drv->startup)
98 ext_phydev->drv->startup(ext_phydev);
99
100 val = phy_read(phydev, phydev->addr, ZYNQ_GMII2RGMII_REG);
101 val &= ~ZYNQ_GMII2RGMII_SPEED_MASK;
102
103 if (ext_phydev->speed == SPEED_1000)
104 val |= BMCR_SPEED1000;
105 else if (ext_phydev->speed == SPEED_100)
106 val |= BMCR_SPEED100;
107
108 phy_write(phydev, phydev->addr, ZYNQ_GMII2RGMII_REG, val |
109 BMCR_FULLDPLX);
110
111 phydev->duplex = ext_phydev->duplex;
112 phydev->speed = ext_phydev->speed;
113 phydev->link = ext_phydev->link;
114
115 return 0;
116}
117
118static int xilinxgmiitorgmii_probe(struct phy_device *phydev)
119{
Siva Durga Prasad Paladugud5c4e1e2018-11-27 11:49:11 +0530120 debug("%s\n", __func__);
121
Siva Durga Prasad Paladugud5c4e1e2018-11-27 11:49:11 +0530122 phydev->flags |= PHY_FLAG_BROKEN_RESET;
123
124 return 0;
125}
126
127static struct phy_driver gmii2rgmii_driver = {
128 .name = "XILINX GMII2RGMII",
129 .uid = PHY_GMII2RGMII_ID,
130 .mask = 0xffffffff,
131 .features = PHY_GBIT_FEATURES,
132 .probe = xilinxgmiitorgmii_probe,
133 .config = xilinxgmiitorgmii_config,
134 .startup = xilinxgmiitorgmii_startup,
135 .writeext = xilinxgmiitorgmii_extwrite,
136 .readext = xilinxgmiitorgmii_extread,
137};
138
139int phy_xilinx_gmii2rgmii_init(void)
140{
141 phy_register(&gmii2rgmii_driver);
142
143 return 0;
144}