2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-11-13 18:21:18 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2014
|
2018-03-06 07:04:58 +00:00
|
|
|
* Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
|
2014-11-13 18:21:18 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2020-05-10 17:40:11 +00:00
|
|
|
#include <linux/delay.h>
|
2014-11-13 18:21:18 +00:00
|
|
|
|
|
|
|
#include <miiphy.h>
|
2018-04-27 12:52:10 +00:00
|
|
|
#ifdef CONFIG_GDSYS_LEGACY_DRIVERS
|
|
|
|
#include <gdsys_fpga.h>
|
|
|
|
#else
|
|
|
|
#include <fdtdec.h>
|
2019-01-28 08:49:33 +00:00
|
|
|
#include <dm.h>
|
2018-04-27 12:52:10 +00:00
|
|
|
#include <regmap.h>
|
|
|
|
#endif
|
2014-11-13 18:21:18 +00:00
|
|
|
|
|
|
|
#include "ihs_mdio.h"
|
|
|
|
|
2018-04-27 12:52:10 +00:00
|
|
|
#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;
|
|
|
|
|
2019-01-28 08:49:33 +00:00
|
|
|
regmap_init_mem(dev_ofnode(fpga), &map);
|
2018-04-27 12:52:10 +00:00
|
|
|
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;
|
|
|
|
|
2019-01-28 08:49:33 +00:00
|
|
|
regmap_init_mem(dev_ofnode(fpga), &map);
|
2018-04-27 12:52:10 +00:00
|
|
|
ptr = regmap_get_range(map, 0);
|
|
|
|
|
|
|
|
out_le16((u16 *)(ptr + base + addr), val);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-04-27 12:52:09 +00:00
|
|
|
static inline u16 read_control(struct ihs_mdio_info *info)
|
|
|
|
{
|
|
|
|
u16 val;
|
2018-04-27 12:52:10 +00:00
|
|
|
#ifdef CONFIG_GDSYS_LEGACY_DRIVERS
|
2018-04-27 12:52:09 +00:00
|
|
|
FPGA_GET_REG(info->fpga, mdio.control, &val);
|
2018-04-27 12:52:10 +00:00
|
|
|
#else
|
|
|
|
val = read_reg(info->fpga, info->base, REG_MDIO_CONTROL);
|
|
|
|
#endif
|
2018-04-27 12:52:09 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void write_control(struct ihs_mdio_info *info, u16 val)
|
|
|
|
{
|
2018-04-27 12:52:10 +00:00
|
|
|
#ifdef CONFIG_GDSYS_LEGACY_DRIVERS
|
2018-04-27 12:52:09 +00:00
|
|
|
FPGA_SET_REG(info->fpga, mdio.control, val);
|
2018-04-27 12:52:10 +00:00
|
|
|
#else
|
|
|
|
write_reg(info->fpga, info->base, REG_MDIO_CONTROL, val);
|
|
|
|
#endif
|
2018-04-27 12:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void write_addr_data(struct ihs_mdio_info *info, u16 val)
|
|
|
|
{
|
2018-04-27 12:52:10 +00:00
|
|
|
#ifdef CONFIG_GDSYS_LEGACY_DRIVERS
|
2018-04-27 12:52:09 +00:00
|
|
|
FPGA_SET_REG(info->fpga, mdio.address_data, val);
|
2018-04-27 12:52:10 +00:00
|
|
|
#else
|
|
|
|
write_reg(info->fpga, info->base, REG_MDIO_ADDR_DATA, val);
|
|
|
|
#endif
|
2018-04-27 12:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline u16 read_rx_data(struct ihs_mdio_info *info)
|
|
|
|
{
|
|
|
|
u16 val;
|
2018-04-27 12:52:10 +00:00
|
|
|
#ifdef CONFIG_GDSYS_LEGACY_DRIVERS
|
2018-04-27 12:52:09 +00:00
|
|
|
FPGA_GET_REG(info->fpga, mdio.rx_data, &val);
|
2018-04-27 12:52:10 +00:00
|
|
|
#else
|
|
|
|
val = read_reg(info->fpga, info->base, REG_MDIO_RX_DATA);
|
|
|
|
#endif
|
2018-04-27 12:52:09 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2014-11-13 18:21:18 +00:00
|
|
|
static int ihs_mdio_idle(struct mii_dev *bus)
|
|
|
|
{
|
|
|
|
struct ihs_mdio_info *info = bus->priv;
|
|
|
|
u16 val;
|
|
|
|
unsigned int ctr = 0;
|
|
|
|
|
|
|
|
do {
|
2018-04-27 12:52:09 +00:00
|
|
|
val = read_control(info);
|
2014-11-13 18:21:18 +00:00
|
|
|
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);
|
|
|
|
|
2018-04-27 12:52:09 +00:00
|
|
|
write_control(info,
|
|
|
|
((addr & 0x1f) << 5) | (regnum & 0x1f) | (2 << 10));
|
2014-11-13 18:21:18 +00:00
|
|
|
|
|
|
|
/* wait for rx data available */
|
|
|
|
udelay(100);
|
|
|
|
|
2018-04-27 12:52:09 +00:00
|
|
|
val = read_rx_data(info);
|
2014-11-13 18:21:18 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2018-04-27 12:52:09 +00:00
|
|
|
write_addr_data(info, value);
|
|
|
|
write_control(info, ((addr & 0x1f) << 5) | (regnum & 0x1f) | (1 << 10));
|
2014-11-13 18:21:18 +00:00
|
|
|
|
|
|
|
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;
|
2015-12-30 13:05:58 +00:00
|
|
|
strcpy(bus->name, info->name);
|
2014-11-13 18:21:18 +00:00
|
|
|
|
|
|
|
bus->priv = info;
|
|
|
|
|
|
|
|
return mdio_register(bus);
|
|
|
|
}
|