summaryrefslogtreecommitdiff
path: root/bsp/hpmicro/libraries/drivers/drv_tsw_phy.c
blob: 00fd3eb7aac8c0aceec17cdc748e5101d3f8e5c5 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * Copyright (c) 2025 HPMicro
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Change Logs:
 * Date         Author      Notes
 * 2025-3-31    Jiading     Initial version
 * 2026-9-18    Jiading     Wait for tsw_phy_speed_valid before announcing link-up
 */

#include "rtthread.h"

#ifdef RT_USING_PHY
#include <rtdevice.h>
#include <rtdbg.h>
#include "hpm_tsw_drv.h"
#include "drv_tsw_phy.h"
#include "hpm_tsw_phy.h"
#include "netif/ethernetif.h"
#include "board.h"
#include "hpm_soc.h"

typedef struct {
    TSW_Type *instance;
    uint8_t port;
} tsw_phy_instance_t;

typedef struct
{
    char *mdio_name;
    tsw_phy_instance_t *phy_ins;
    struct eth_device *eth_dev;
    phy_device_t *phy_dev;
    struct rt_mdio_bus *mdio_bus;
} tsw_phy_handle_t;

typedef struct
{
    uint8_t phy_handle_cnt;
    tsw_phy_handle_t **phy_handle;
} tsw_phy_monitor_handle_t;

extern struct eth_device eth0_dev;
static phy_device_t phy0_dev;
static struct rt_mdio_bus_ops mdio0_bus_ops;
static struct rt_mdio_bus mdio0_bus = {.ops = &mdio0_bus_ops};
static tsw_phy_instance_t instance = {.instance = BOARD_TSW,
                                      .port = BOARD_TSW_PORT
                                        };

static tsw_phy_handle_t tsw_phy_handle = {
    .phy_ins   = &instance,
    .eth_dev   = &eth0_dev,
    .phy_dev   = &phy0_dev,
    .mdio_name = "MDIO0",
    .mdio_bus  = &mdio0_bus,
};

static tsw_phy_handle_t *s_gphys[] = {
    &tsw_phy_handle
};

tsw_phy_monitor_handle_t phy_monitor_handle = {
    .phy_handle_cnt = ARRAY_SIZE(s_gphys),
    .phy_handle     = s_gphys
};

static struct rt_phy_ops phy_ops;

static rt_phy_status phy_query_status(rt_phy_t *phy, tsw_phy_status_t *phy_status)
{
    tsw_phy_instance_t *phy_ins = phy->bus->hw_obj;
    hpm_stat_t stat = status_fail;

    *phy_status = (tsw_phy_status_t){0};

#if defined(__USE_RTL8211) && __USE_RTL8211
    stat = rtl8211_get_phy_status(phy_ins->instance, phy_ins->port, RTL8211_ADDR, phy_status);
#endif

    if (stat != status_success) {
        return PHY_STATUS_FAIL;
    }

    return PHY_STATUS_OK;
}

static void phy_apply_mac(tsw_phy_instance_t *phy_ins, const phy_info_t *phy_info)
{
    tsw_port_speed_t port_speed[] = {tsw_port_speed_10mbps, tsw_port_speed_100mbps, tsw_port_speed_1000mbps};

    if (phy_info->phy_speed >= ARRAY_SIZE(port_speed)) {
        return;
    }

    tsw_set_port_speed(phy_ins->instance, phy_ins->port, port_speed[phy_info->phy_speed]);
}

static rt_phy_status phy_init(void *object, rt_uint32_t phy_addr, rt_uint32_t src_clock_hz)
{
    tsw_phy_instance_t *phy_ins = (tsw_phy_instance_t *)object;
    rtl8211_config_t phy_config;

    /* Set MDC clock frequency */
    tsw_ep_set_mdio_config(phy_ins->instance, phy_ins->port, src_clock_hz / (2 * PHY_MDIO_CLK_FREQ) - 1);

    /* PHY Reset */
    rtl8211_reset(phy_ins->instance, phy_ins->port, RTL8211_ADDR);
    rtl8211_basic_mode_default_config(phy_ins->instance, &phy_config);
    if (rtl8211_basic_mode_init(phy_ins->instance, phy_ins->port, RTL8211_ADDR, &phy_config) == true) {
        return PHY_STATUS_OK;
    } else {
        return PHY_STATUS_FAIL;
    }
}

static rt_size_t phy_read(void *bus, rt_uint32_t addr, rt_uint32_t reg, void *data, rt_uint32_t size)
{
    hpm_stat_t stat;
    tsw_phy_instance_t *phy_ins = ((struct rt_mdio_bus *)bus)->hw_obj;

    stat = tsw_ep_mdio_read(phy_ins->instance, phy_ins->port, addr, reg, (uint16_t *)data);

    if (stat == status_success) {
        return size;
    } else {
        return 0;
    }
}

static rt_size_t phy_write(void *bus, rt_uint32_t addr, rt_uint32_t reg, void *data, rt_uint32_t size)
{
    hpm_stat_t stat;
    tsw_phy_instance_t *phy_ins = ((struct rt_mdio_bus *)bus)->hw_obj;

    stat = tsw_ep_mdio_write(phy_ins->instance, phy_ins->port, addr, reg, *(uint16_t *)data);

    if (stat == status_success) {
        return size;
    } else {
        return 0;
    }
}

static rt_phy_status phy_get_link_status(rt_phy_t *phy, rt_bool_t *status)
{
    tsw_phy_status_t phy_status;

    if (phy_query_status(phy, &phy_status) != PHY_STATUS_OK) {
        *status = RT_FALSE;
        return PHY_STATUS_FAIL;
    }

    *status = (phy_status.tsw_phy_link == tsw_phy_link_up) ? RT_TRUE : RT_FALSE;

    return PHY_STATUS_OK;
}

static rt_phy_status phy_get_link_speed_duplex(rt_phy_t *phy, rt_uint32_t *speed, rt_uint32_t *duplex)
{
    tsw_phy_status_t phy_status;

    if (phy_query_status(phy, &phy_status) != PHY_STATUS_OK) {
        return PHY_STATUS_FAIL;
    }

    if (!tsw_phy_link_up_with_valid_speed(&phy_status)) {
        return PHY_STATUS_FAIL;
    }

    *speed  = phy_status.tsw_phy_speed;
    *duplex = phy_status.tsw_phy_duplex;

    return PHY_STATUS_OK;
}

static void phy_poll_status(void *parameter)
{
    int ret;
    phy_info_t phy_info;
    tsw_phy_status_t phy_status;
    phy_device_t *phy_dev;
    struct eth_device *eth_dev;
    char const *ps[] = {"10Mbps", "100Mbps", "1000Mbps"};
    tsw_phy_monitor_handle_t *phy_monitor_handle = (tsw_phy_monitor_handle_t *)parameter;

    for (uint32_t i = 0; i < phy_monitor_handle->phy_handle_cnt; i++) {
        eth_dev = phy_monitor_handle->phy_handle[i]->eth_dev;
        phy_dev = phy_monitor_handle->phy_handle[i]->phy_dev;

        if (phy_query_status(&phy_dev->phy, &phy_status) != PHY_STATUS_OK) {
            continue;
        }

        if (phy_status.tsw_phy_link == tsw_phy_link_down) {
            if (phy_dev->phy_link != PHY_LINK_DOWN) {
                phy_dev->phy_link = PHY_LINK_DOWN;
                eth_device_linkchange(eth_dev, RT_FALSE);
                LOG_I("PHY Status: %s", "Link down\n");
            }
            continue;
        }

        if (!tsw_phy_link_up_with_valid_speed(&phy_status)) {
            continue;
        }

        phy_info.phy_speed = phy_status.tsw_phy_speed;
        phy_info.phy_duplex = phy_status.tsw_phy_duplex;

        ret = memcmp(&phy_dev->phy_info, &phy_info, sizeof(phy_info_t));
        if (ret != 0) {
            memcpy(&phy_dev->phy_info, &phy_info, sizeof(phy_info_t));
            if (phy_dev->phy_link == PHY_LINK_UP) {
                phy_apply_mac(phy_monitor_handle->phy_handle[i]->phy_ins, &phy_dev->phy_info);
                LOG_I("PHY Speed: %s", ps[phy_dev->phy_info.phy_speed]);
                LOG_I("PHY Duplex: %s\n", phy_dev->phy_info.phy_duplex & PHY_FULL_DUPLEX ? "full duplex" : "half duplex");
            }
        }

        if (phy_dev->phy_link != PHY_LINK_UP) {
            phy_dev->phy_link = PHY_LINK_UP;
            eth_device_linkchange(eth_dev, RT_TRUE);
            LOG_I("PHY Status: %s", "Link up");
            LOG_I("PHY Speed: %s", ps[phy_dev->phy_info.phy_speed]);
            LOG_I("PHY Duplex: %s\n", phy_dev->phy_info.phy_duplex & PHY_FULL_DUPLEX ? "full duplex" : "half duplex");
            phy_apply_mac(phy_monitor_handle->phy_handle[i]->phy_ins, &phy_dev->phy_info);
            if (!(phy_dev->phy_info.phy_duplex & PHY_FULL_DUPLEX)) {
                LOG_E("Error: PHY is in half duplex now, but TSW MAC supports only full duplex mode!\n");
                return;
            }
        }
    }
}

static void phy_detection(void *parameter)
{
    phy_device_t *phy_dev = (phy_device_t *)parameter;

    if (phy_dev->phy.ops->init(phy_dev->phy.bus->hw_obj, 0, PHY_MDIO_CSR_CLK_FREQ) != PHY_STATUS_OK) {
        LOG_E("No any PHY device is detected! Please check your hardware!\n");
    }

    return;

}

static void phy_monitor_thread_entry(void *args)
{
    rt_timer_t phy_status_timer;

    tsw_phy_monitor_handle_t *phy_monitor_handle = (tsw_phy_monitor_handle_t *)args;

    for (uint32_t i = 0; i < phy_monitor_handle->phy_handle_cnt; i++) {
        LOG_D("Detect a PHY%d\n", i);
        phy_detection(phy_monitor_handle->phy_handle[i]->phy_dev);
    }

    phy_status_timer = rt_timer_create("PHY_Monitor", phy_poll_status, phy_monitor_handle, RT_TICK_PER_SECOND, RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);

    if (!phy_status_timer || rt_timer_start(phy_status_timer) != RT_EOK) {
        LOG_E("Failed to start link change detection timer\n");
    }
}

int phy_device_register(void)
{
    rt_err_t err = -RT_ERROR;
    rt_thread_t thread_phy_monitor;

    /* Set ops for PHY */
    phy_ops.init = phy_init;
    phy_ops.get_link_status = phy_get_link_status;
    phy_ops.get_link_speed_duplex = phy_get_link_speed_duplex;

    for (uint32_t i = 0; i < ARRAY_SIZE(s_gphys); i++) {
        /* Set PHY address */
        s_gphys[i]->phy_dev->phy.addr = 0xffff;

        /* Set MIDO bus */
        s_gphys[i]->mdio_bus->hw_obj     = s_gphys[i]->phy_ins;
        s_gphys[i]->mdio_bus->name       = s_gphys[i]->mdio_name;
        s_gphys[i]->mdio_bus->ops->read  = phy_read;
        s_gphys[i]->mdio_bus->ops->write = phy_write;
        s_gphys[i]->phy_dev->phy.bus     = s_gphys[i]->mdio_bus;
        s_gphys[i]->phy_dev->phy.ops     = &phy_ops;

        rt_hw_phy_register(&s_gphys[i]->phy_dev->phy, NULL);
    }

    /* Start PHY monitor */
    thread_phy_monitor = rt_thread_create("PHY Monitor", phy_monitor_thread_entry, &phy_monitor_handle, 4096, RT_THREAD_PRIORITY_MAX - 2, 2);

    if (thread_phy_monitor != RT_NULL) {
        rt_thread_startup(thread_phy_monitor);
    } else {
        err = -RT_ERROR;
    }

    return err;
}
INIT_PREV_EXPORT(phy_device_register);
#endif /* RT_USING_PHY */