Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dirk Eibach | f74a027 | 2014-11-13 19:21:18 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2014 |
Mario Six | b489358 | 2018-03-06 08:04:58 +0100 | [diff] [blame] | 4 | * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc |
Dirk Eibach | f74a027 | 2014-11-13 19:21:18 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | |
Dirk Eibach | f74a027 | 2014-11-13 19:21:18 +0100 | [diff] [blame] | 9 | #include <miiphy.h> |
Mario Six | 2963721 | 2018-04-27 14:52:10 +0200 | [diff] [blame] | 10 | #ifdef CONFIG_GDSYS_LEGACY_DRIVERS |
| 11 | #include <gdsys_fpga.h> |
| 12 | #else |
| 13 | #include <fdtdec.h> |
Mario Six | 7720ba0 | 2019-01-28 09:49:33 +0100 | [diff] [blame] | 14 | #include <dm.h> |
Mario Six | 2963721 | 2018-04-27 14:52:10 +0200 | [diff] [blame] | 15 | #include <regmap.h> |
| 16 | #endif |
Dirk Eibach | f74a027 | 2014-11-13 19:21:18 +0100 | [diff] [blame] | 17 | |
| 18 | #include "ihs_mdio.h" |
| 19 | |
Mario Six | 2963721 | 2018-04-27 14:52:10 +0200 | [diff] [blame] | 20 | #ifndef CONFIG_GDSYS_LEGACY_DRIVERS |
| 21 | enum { |
| 22 | REG_MDIO_CONTROL = 0x0, |
| 23 | REG_MDIO_ADDR_DATA = 0x2, |
| 24 | REG_MDIO_RX_DATA = 0x4, |
| 25 | }; |
| 26 | |
| 27 | static inline u16 read_reg(struct udevice *fpga, uint base, uint addr) |
| 28 | { |
| 29 | struct regmap *map; |
| 30 | u8 *ptr; |
| 31 | |
Mario Six | 7720ba0 | 2019-01-28 09:49:33 +0100 | [diff] [blame] | 32 | regmap_init_mem(dev_ofnode(fpga), &map); |
Mario Six | 2963721 | 2018-04-27 14:52:10 +0200 | [diff] [blame] | 33 | ptr = regmap_get_range(map, 0); |
| 34 | |
| 35 | return in_le16((u16 *)(ptr + base + addr)); |
| 36 | } |
| 37 | |
| 38 | static inline void write_reg(struct udevice *fpga, uint base, uint addr, |
| 39 | u16 val) |
| 40 | { |
| 41 | struct regmap *map; |
| 42 | u8 *ptr; |
| 43 | |
Mario Six | 7720ba0 | 2019-01-28 09:49:33 +0100 | [diff] [blame] | 44 | regmap_init_mem(dev_ofnode(fpga), &map); |
Mario Six | 2963721 | 2018-04-27 14:52:10 +0200 | [diff] [blame] | 45 | ptr = regmap_get_range(map, 0); |
| 46 | |
| 47 | out_le16((u16 *)(ptr + base + addr), val); |
| 48 | } |
| 49 | #endif |
| 50 | |
Mario Six | 9a95ba6 | 2018-04-27 14:52:09 +0200 | [diff] [blame] | 51 | static inline u16 read_control(struct ihs_mdio_info *info) |
| 52 | { |
| 53 | u16 val; |
Mario Six | 2963721 | 2018-04-27 14:52:10 +0200 | [diff] [blame] | 54 | #ifdef CONFIG_GDSYS_LEGACY_DRIVERS |
Mario Six | 9a95ba6 | 2018-04-27 14:52:09 +0200 | [diff] [blame] | 55 | FPGA_GET_REG(info->fpga, mdio.control, &val); |
Mario Six | 2963721 | 2018-04-27 14:52:10 +0200 | [diff] [blame] | 56 | #else |
| 57 | val = read_reg(info->fpga, info->base, REG_MDIO_CONTROL); |
| 58 | #endif |
Mario Six | 9a95ba6 | 2018-04-27 14:52:09 +0200 | [diff] [blame] | 59 | return val; |
| 60 | } |
| 61 | |
| 62 | static inline void write_control(struct ihs_mdio_info *info, u16 val) |
| 63 | { |
Mario Six | 2963721 | 2018-04-27 14:52:10 +0200 | [diff] [blame] | 64 | #ifdef CONFIG_GDSYS_LEGACY_DRIVERS |
Mario Six | 9a95ba6 | 2018-04-27 14:52:09 +0200 | [diff] [blame] | 65 | FPGA_SET_REG(info->fpga, mdio.control, val); |
Mario Six | 2963721 | 2018-04-27 14:52:10 +0200 | [diff] [blame] | 66 | #else |
| 67 | write_reg(info->fpga, info->base, REG_MDIO_CONTROL, val); |
| 68 | #endif |
Mario Six | 9a95ba6 | 2018-04-27 14:52:09 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static inline void write_addr_data(struct ihs_mdio_info *info, u16 val) |
| 72 | { |
Mario Six | 2963721 | 2018-04-27 14:52:10 +0200 | [diff] [blame] | 73 | #ifdef CONFIG_GDSYS_LEGACY_DRIVERS |
Mario Six | 9a95ba6 | 2018-04-27 14:52:09 +0200 | [diff] [blame] | 74 | FPGA_SET_REG(info->fpga, mdio.address_data, val); |
Mario Six | 2963721 | 2018-04-27 14:52:10 +0200 | [diff] [blame] | 75 | #else |
| 76 | write_reg(info->fpga, info->base, REG_MDIO_ADDR_DATA, val); |
| 77 | #endif |
Mario Six | 9a95ba6 | 2018-04-27 14:52:09 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static inline u16 read_rx_data(struct ihs_mdio_info *info) |
| 81 | { |
| 82 | u16 val; |
Mario Six | 2963721 | 2018-04-27 14:52:10 +0200 | [diff] [blame] | 83 | #ifdef CONFIG_GDSYS_LEGACY_DRIVERS |
Mario Six | 9a95ba6 | 2018-04-27 14:52:09 +0200 | [diff] [blame] | 84 | FPGA_GET_REG(info->fpga, mdio.rx_data, &val); |
Mario Six | 2963721 | 2018-04-27 14:52:10 +0200 | [diff] [blame] | 85 | #else |
| 86 | val = read_reg(info->fpga, info->base, REG_MDIO_RX_DATA); |
| 87 | #endif |
Mario Six | 9a95ba6 | 2018-04-27 14:52:09 +0200 | [diff] [blame] | 88 | return val; |
| 89 | } |
| 90 | |
Dirk Eibach | f74a027 | 2014-11-13 19:21:18 +0100 | [diff] [blame] | 91 | static int ihs_mdio_idle(struct mii_dev *bus) |
| 92 | { |
| 93 | struct ihs_mdio_info *info = bus->priv; |
| 94 | u16 val; |
| 95 | unsigned int ctr = 0; |
| 96 | |
| 97 | do { |
Mario Six | 9a95ba6 | 2018-04-27 14:52:09 +0200 | [diff] [blame] | 98 | val = read_control(info); |
Dirk Eibach | f74a027 | 2014-11-13 19:21:18 +0100 | [diff] [blame] | 99 | udelay(100); |
| 100 | if (ctr++ > 10) |
| 101 | return -1; |
| 102 | } while (!(val & (1 << 12))); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int ihs_mdio_reset(struct mii_dev *bus) |
| 108 | { |
| 109 | ihs_mdio_idle(bus); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int ihs_mdio_read(struct mii_dev *bus, int addr, int dev_addr, |
| 115 | int regnum) |
| 116 | { |
| 117 | struct ihs_mdio_info *info = bus->priv; |
| 118 | u16 val; |
| 119 | |
| 120 | ihs_mdio_idle(bus); |
| 121 | |
Mario Six | 9a95ba6 | 2018-04-27 14:52:09 +0200 | [diff] [blame] | 122 | write_control(info, |
| 123 | ((addr & 0x1f) << 5) | (regnum & 0x1f) | (2 << 10)); |
Dirk Eibach | f74a027 | 2014-11-13 19:21:18 +0100 | [diff] [blame] | 124 | |
| 125 | /* wait for rx data available */ |
| 126 | udelay(100); |
| 127 | |
Mario Six | 9a95ba6 | 2018-04-27 14:52:09 +0200 | [diff] [blame] | 128 | val = read_rx_data(info); |
Dirk Eibach | f74a027 | 2014-11-13 19:21:18 +0100 | [diff] [blame] | 129 | |
| 130 | return val; |
| 131 | } |
| 132 | |
| 133 | static int ihs_mdio_write(struct mii_dev *bus, int addr, int dev_addr, |
| 134 | int regnum, u16 value) |
| 135 | { |
| 136 | struct ihs_mdio_info *info = bus->priv; |
| 137 | |
| 138 | ihs_mdio_idle(bus); |
| 139 | |
Mario Six | 9a95ba6 | 2018-04-27 14:52:09 +0200 | [diff] [blame] | 140 | write_addr_data(info, value); |
| 141 | write_control(info, ((addr & 0x1f) << 5) | (regnum & 0x1f) | (1 << 10)); |
Dirk Eibach | f74a027 | 2014-11-13 19:21:18 +0100 | [diff] [blame] | 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | int ihs_mdio_init(struct ihs_mdio_info *info) |
| 147 | { |
| 148 | struct mii_dev *bus = mdio_alloc(); |
| 149 | |
| 150 | if (!bus) { |
| 151 | printf("Failed to allocate FSL MDIO bus\n"); |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | bus->read = ihs_mdio_read; |
| 156 | bus->write = ihs_mdio_write; |
| 157 | bus->reset = ihs_mdio_reset; |
Ben Whitten | 34fd6c9 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 158 | strcpy(bus->name, info->name); |
Dirk Eibach | f74a027 | 2014-11-13 19:21:18 +0100 | [diff] [blame] | 159 | |
| 160 | bus->priv = info; |
| 161 | |
| 162 | return mdio_register(bus); |
| 163 | } |