blob: b17e8db91bfa9cb263769e1d327cb4acf342b228 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dirk Eibach50dcf892014-11-13 19:21:18 +01002/*
3 * (C) Copyright 2014
Mario Sixd38826a2018-03-06 08:04:58 +01004 * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
Dirk Eibach50dcf892014-11-13 19:21:18 +01005 */
6
7#include <common.h>
8
Dirk Eibach50dcf892014-11-13 19:21:18 +01009#include <miiphy.h>
Mario Six9a519dfe2018-04-27 14:52:10 +020010#ifdef CONFIG_GDSYS_LEGACY_DRIVERS
11#include <gdsys_fpga.h>
12#else
13#include <fdtdec.h>
14#include <regmap.h>
15#endif
Dirk Eibach50dcf892014-11-13 19:21:18 +010016
17#include "ihs_mdio.h"
18
Mario Six9a519dfe2018-04-27 14:52:10 +020019#ifndef CONFIG_GDSYS_LEGACY_DRIVERS
20enum {
21 REG_MDIO_CONTROL = 0x0,
22 REG_MDIO_ADDR_DATA = 0x2,
23 REG_MDIO_RX_DATA = 0x4,
24};
25
26static inline u16 read_reg(struct udevice *fpga, uint base, uint addr)
27{
28 struct regmap *map;
29 u8 *ptr;
30
31 regmap_init_mem(fpga, &map);
32 ptr = regmap_get_range(map, 0);
33
34 return in_le16((u16 *)(ptr + base + addr));
35}
36
37static inline void write_reg(struct udevice *fpga, uint base, uint addr,
38 u16 val)
39{
40 struct regmap *map;
41 u8 *ptr;
42
43 regmap_init_mem(fpga, &map);
44 ptr = regmap_get_range(map, 0);
45
46 out_le16((u16 *)(ptr + base + addr), val);
47}
48#endif
49
Mario Six9139ac92018-04-27 14:52:09 +020050static inline u16 read_control(struct ihs_mdio_info *info)
51{
52 u16 val;
Mario Six9a519dfe2018-04-27 14:52:10 +020053#ifdef CONFIG_GDSYS_LEGACY_DRIVERS
Mario Six9139ac92018-04-27 14:52:09 +020054 FPGA_GET_REG(info->fpga, mdio.control, &val);
Mario Six9a519dfe2018-04-27 14:52:10 +020055#else
56 val = read_reg(info->fpga, info->base, REG_MDIO_CONTROL);
57#endif
Mario Six9139ac92018-04-27 14:52:09 +020058 return val;
59}
60
61static inline void write_control(struct ihs_mdio_info *info, u16 val)
62{
Mario Six9a519dfe2018-04-27 14:52:10 +020063#ifdef CONFIG_GDSYS_LEGACY_DRIVERS
Mario Six9139ac92018-04-27 14:52:09 +020064 FPGA_SET_REG(info->fpga, mdio.control, val);
Mario Six9a519dfe2018-04-27 14:52:10 +020065#else
66 write_reg(info->fpga, info->base, REG_MDIO_CONTROL, val);
67#endif
Mario Six9139ac92018-04-27 14:52:09 +020068}
69
70static inline void write_addr_data(struct ihs_mdio_info *info, u16 val)
71{
Mario Six9a519dfe2018-04-27 14:52:10 +020072#ifdef CONFIG_GDSYS_LEGACY_DRIVERS
Mario Six9139ac92018-04-27 14:52:09 +020073 FPGA_SET_REG(info->fpga, mdio.address_data, val);
Mario Six9a519dfe2018-04-27 14:52:10 +020074#else
75 write_reg(info->fpga, info->base, REG_MDIO_ADDR_DATA, val);
76#endif
Mario Six9139ac92018-04-27 14:52:09 +020077}
78
79static inline u16 read_rx_data(struct ihs_mdio_info *info)
80{
81 u16 val;
Mario Six9a519dfe2018-04-27 14:52:10 +020082#ifdef CONFIG_GDSYS_LEGACY_DRIVERS
Mario Six9139ac92018-04-27 14:52:09 +020083 FPGA_GET_REG(info->fpga, mdio.rx_data, &val);
Mario Six9a519dfe2018-04-27 14:52:10 +020084#else
85 val = read_reg(info->fpga, info->base, REG_MDIO_RX_DATA);
86#endif
Mario Six9139ac92018-04-27 14:52:09 +020087 return val;
88}
89
Dirk Eibach50dcf892014-11-13 19:21:18 +010090static int ihs_mdio_idle(struct mii_dev *bus)
91{
92 struct ihs_mdio_info *info = bus->priv;
93 u16 val;
94 unsigned int ctr = 0;
95
96 do {
Mario Six9139ac92018-04-27 14:52:09 +020097 val = read_control(info);
Dirk Eibach50dcf892014-11-13 19:21:18 +010098 udelay(100);
99 if (ctr++ > 10)
100 return -1;
101 } while (!(val & (1 << 12)));
102
103 return 0;
104}
105
106static int ihs_mdio_reset(struct mii_dev *bus)
107{
108 ihs_mdio_idle(bus);
109
110 return 0;
111}
112
113static int ihs_mdio_read(struct mii_dev *bus, int addr, int dev_addr,
114 int regnum)
115{
116 struct ihs_mdio_info *info = bus->priv;
117 u16 val;
118
119 ihs_mdio_idle(bus);
120
Mario Six9139ac92018-04-27 14:52:09 +0200121 write_control(info,
122 ((addr & 0x1f) << 5) | (regnum & 0x1f) | (2 << 10));
Dirk Eibach50dcf892014-11-13 19:21:18 +0100123
124 /* wait for rx data available */
125 udelay(100);
126
Mario Six9139ac92018-04-27 14:52:09 +0200127 val = read_rx_data(info);
Dirk Eibach50dcf892014-11-13 19:21:18 +0100128
129 return val;
130}
131
132static int ihs_mdio_write(struct mii_dev *bus, int addr, int dev_addr,
133 int regnum, u16 value)
134{
135 struct ihs_mdio_info *info = bus->priv;
136
137 ihs_mdio_idle(bus);
138
Mario Six9139ac92018-04-27 14:52:09 +0200139 write_addr_data(info, value);
140 write_control(info, ((addr & 0x1f) << 5) | (regnum & 0x1f) | (1 << 10));
Dirk Eibach50dcf892014-11-13 19:21:18 +0100141
142 return 0;
143}
144
145int ihs_mdio_init(struct ihs_mdio_info *info)
146{
147 struct mii_dev *bus = mdio_alloc();
148
149 if (!bus) {
150 printf("Failed to allocate FSL MDIO bus\n");
151 return -1;
152 }
153
154 bus->read = ihs_mdio_read;
155 bus->write = ihs_mdio_write;
156 bus->reset = ihs_mdio_reset;
Ben Whitten192bc692015-12-30 13:05:58 +0000157 strcpy(bus->name, info->name);
Dirk Eibach50dcf892014-11-13 19:21:18 +0100158
159 bus->priv = info;
160
161 return mdio_register(bus);
162}