summaryrefslogtreecommitdiff
path: root/board/gdsys/common/ihs_mdio.c
blob: b17e8db91bfa9cb263769e1d327cb4acf342b228 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2014
 * Dirk Eibach,  Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
 */

#include <common.h>

#include <miiphy.h>
#ifdef CONFIG_GDSYS_LEGACY_DRIVERS
#include <gdsys_fpga.h>
#else
#include <fdtdec.h>
#include <regmap.h>
#endif

#include "ihs_mdio.h"

#ifndef CONFIG_GDSYS_LEGACY_DRIVERS
enum {
	REG_MDIO_CONTROL = 0x0,
	REG_MDIO_ADDR_DATA = 0x2,
	REG_MDIO_RX_DATA = 0x4,
};

static inline u16 read_reg(struct udevice *fpga, uint base, uint addr)
{
	struct regmap *map;
	u8 *ptr;

	regmap_init_mem(fpga, &map);
	ptr = regmap_get_range(map, 0);

	return in_le16((u16 *)(ptr + base + addr));
}

static inline void write_reg(struct udevice *fpga, uint base, uint addr,
			     u16 val)
{
	struct regmap *map;
	u8 *ptr;

	regmap_init_mem(fpga, &map);
	ptr = regmap_get_range(map, 0);

	out_le16((u16 *)(ptr + base + addr), val);
}
#endif

static inline u16 read_control(struct ihs_mdio_info *info)
{
	u16 val;
#ifdef CONFIG_GDSYS_LEGACY_DRIVERS
	FPGA_GET_REG(info->fpga, mdio.control, &val);
#else
	val = read_reg(info->fpga, info->base, REG_MDIO_CONTROL);
#endif
	return val;
}

static inline void write_control(struct ihs_mdio_info *info, u16 val)
{
#ifdef CONFIG_GDSYS_LEGACY_DRIVERS
	FPGA_SET_REG(info->fpga, mdio.control, val);
#else
	write_reg(info->fpga, info->base, REG_MDIO_CONTROL, val);
#endif
}

static inline void write_addr_data(struct ihs_mdio_info *info, u16 val)
{
#ifdef CONFIG_GDSYS_LEGACY_DRIVERS
	FPGA_SET_REG(info->fpga, mdio.address_data, val);
#else
	write_reg(info->fpga, info->base, REG_MDIO_ADDR_DATA, val);
#endif
}

static inline u16 read_rx_data(struct ihs_mdio_info *info)
{
	u16 val;
#ifdef CONFIG_GDSYS_LEGACY_DRIVERS
	FPGA_GET_REG(info->fpga, mdio.rx_data, &val);
#else
	val = read_reg(info->fpga, info->base, REG_MDIO_RX_DATA);
#endif
	return val;
}

static int ihs_mdio_idle(struct mii_dev *bus)
{
	struct ihs_mdio_info *info = bus->priv;
	u16 val;
	unsigned int ctr = 0;

	do {
		val = read_control(info);
		udelay(100);
		if (ctr++ > 10)
			return -1;
	} while (!(val & (1 << 12)));

	return 0;
}

static int ihs_mdio_reset(struct mii_dev *bus)
{
	ihs_mdio_idle(bus);

	return 0;
}

static int ihs_mdio_read(struct mii_dev *bus, int addr, int dev_addr,
			 int regnum)
{
	struct ihs_mdio_info *info = bus->priv;
	u16 val;

	ihs_mdio_idle(bus);

	write_control(info,
		      ((addr & 0x1f) << 5) | (regnum & 0x1f) | (2 << 10));

	/* wait for rx data available */
	udelay(100);

	val = read_rx_data(info);

	return val;
}

static int ihs_mdio_write(struct mii_dev *bus, int addr, int dev_addr,
			  int regnum, u16 value)
{
	struct ihs_mdio_info *info = bus->priv;

	ihs_mdio_idle(bus);

	write_addr_data(info, value);
	write_control(info, ((addr & 0x1f) << 5) | (regnum & 0x1f) | (1 << 10));

	return 0;
}

int ihs_mdio_init(struct ihs_mdio_info *info)
{
	struct mii_dev *bus = mdio_alloc();

	if (!bus) {
		printf("Failed to allocate FSL MDIO bus\n");
		return -1;
	}

	bus->read = ihs_mdio_read;
	bus->write = ihs_mdio_write;
	bus->reset = ihs_mdio_reset;
	strcpy(bus->name, info->name);

	bus->priv = info;

	return mdio_register(bus);
}