summaryrefslogtreecommitdiff
path: root/bsp/phytium/libraries/standalone/drivers/eth/fxmac/fxmac_phy.c
blob: 4af6752555c336c9d73f32b7b716e6d74e3f95b0 (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * Copyright : (C) 2022 Phytium Information Technology, Inc.
 * All Rights Reserved.
 *
 * This program is OPEN SOURCE software: you can redistribute it and/or modify it
 * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
 * either version 1.0 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the Phytium Public License for more details.
 *
 *
 * FilePath: fxmac_phy.c
 * Date: 2022-04-06 14:46:52
 * LastEditTime: 2022-04-06 14:46:58
 * Description:  This file is for
 *
 * Modify History:
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 */

#include "fxmac.h"
#include "eth_ieee_reg.h"
#include "fdebug.h"

#if defined(CONFIG_FXMAC_PHY_YT)
    #include "phy_yt.h"
#endif


#define FXMAC_DEBUG_TAG "FXMAC_PHY"
#define FXMAC_ERROR(format, ...) FT_DEBUG_PRINT_E(FXMAC_DEBUG_TAG, format, ##__VA_ARGS__)
#define FXMAC_INFO(format, ...) FT_DEBUG_PRINT_I(FXMAC_DEBUG_TAG, format, ##__VA_ARGS__)
#define FXMAC_DEBUG(format, ...) FT_DEBUG_PRINT_D(FXMAC_DEBUG_TAG, format, ##__VA_ARGS__)
#define FXMAC_WARN(format, ...) FT_DEBUG_PRINT_W(FXMAC_DEBUG_TAG, format, ##__VA_ARGS__)

static FXmac *instance_b;
static u32 phy_addr_b;

static FError FXmacDetect(FXmac *instance_p, u32 *phy_addr_p)
{
    u32 phy_addr = 0, i = 0, index;
    u16 phy_reg = 0, phy_id1_reg, phy_id2_reg;
    FError ret;
    instance_b = instance_p;

    for (phy_addr = 0; phy_addr < FT_XMAC_PHY_MAX_NUM; phy_addr++)
    {
        ret = FXmacPhyRead(instance_p, phy_addr, PHY_STATUS_REG_OFFSET, &phy_reg);
        if (ret != FT_SUCCESS)
        {
            FXMAC_ERROR("%s,  PHY operation is busy", __func__);
            return ret;
        }
        FXMAC_INFO("PHY_STATUS_REG_OFFSET is %x \r\n", phy_reg);
        if (phy_reg != 0xffff)
        {
            ret = FXmacPhyRead(instance_p, phy_addr, PHY_IDENTIFIER_1_REG, &phy_id1_reg);
            ret |= FXmacPhyRead(instance_p, phy_addr, PHY_IDENTIFIER_2_REG, &phy_id2_reg);
            FXMAC_INFO("phy_id1_reg is 0x%x \r\n", phy_id1_reg);
            FXMAC_INFO("phy_id2_reg is 0x%x \r\n", phy_id2_reg);
            if ((ret == FT_SUCCESS) && (phy_id2_reg != 0) && (phy_id1_reg != 0xffff) && (phy_id1_reg != 0xffff))
            {
                *phy_addr_p = phy_addr;
                phy_addr_b = phy_addr;
                FXMAC_INFO("phy_addr is 0x%x \r\n", phy_addr);
                return FT_SUCCESS;
            }
        }
    }

    return FT_SUCCESS;
}

static FError FXmacGetIeeePhySpeed(FXmac *instance_p, u32 phy_addr)
{
    u16 temp, temp2;
    u16 control;
    u16 status;
    u16 partner_capabilities;
    u32 negotitation_timeout_cnt = 0;
    FError ret;
    volatile s32 wait;

    FXMAC_INFO("Start PHY autonegotiation ");

    ret = FXmacPhyRead(instance_p, phy_addr, PHY_CONTROL_REG_OFFSET, &control);
    if (ret != FT_SUCCESS)
    {
        FXMAC_ERROR("%s line is %d,read PHY_CONTROL_REG_OFFSET is error", __func__, __LINE__);
        return ret;
    }
    control |= PHY_CONTROL_RESET_MASK;

    ret = FXmacPhyWrite(instance_p, phy_addr, PHY_CONTROL_REG_OFFSET, control);
    if (ret != FT_SUCCESS)
    {
        FXMAC_ERROR("%s line is %d,write PHY_CONTROL_REG_OFFSET is error", __func__, __LINE__);
        return ret;
    }
    for (wait = 0; wait < 100000; wait++)
        ;
    FXMAC_INFO(" PHY reset end ");
    ret = FXmacPhyRead(instance_p, phy_addr, PHY_CONTROL_REG_OFFSET, &control);
    if (ret != FT_SUCCESS)
    {
        FXMAC_ERROR("%s line is %d,read PHY_CONTROL_REG_OFFSET is error", __func__, __LINE__);
        return ret;
    }

    control |= PHY_CONTROL_AUTONEGOTIATE_ENABLE;
    control |= PHY_CONTROL_AUTONEGOTIATE_RESTART;
    ret = FXmacPhyWrite(instance_p, phy_addr, PHY_CONTROL_REG_OFFSET, control);
    if (ret != FT_SUCCESS)
    {
        FXMAC_ERROR("%s line is %d,write PHY_CONTROL_REG_OFFSET is error", __func__, __LINE__);
        return ret;
    }

    FXMAC_INFO("Waiting for PHY to complete autonegotiation.");

    ret = FXmacPhyRead(instance_p, phy_addr, PHY_STATUS_REG_OFFSET, &status);
    if (ret != FT_SUCCESS)
    {
        FXMAC_ERROR("%s line is %d,read PHY_CONTROL_REG_OFFSET is error", __func__, __LINE__);
        return ret;
    }


    while (!(status & PHY_STATUS_AUTONEGOTIATE_COMPLETE))
    {
        for (wait = 0; wait < 1000000; wait++)
            ;
        ret = FXmacPhyRead(instance_p, phy_addr, PHY_STATUS_REG_OFFSET, &status);
        if (ret != FT_SUCCESS)
        {
            FXMAC_ERROR("%s line is %d,read PHY_STATUS_REG_OFFSET is error", __func__, __LINE__);
            return ret;
        }


        if (negotitation_timeout_cnt++ >= 0xfff)
        {
            FXMAC_ERROR("autonegotiation is error ");
            return FXMAC_PHY_AUTO_AUTONEGOTIATION_FAILED;
        }
    }
    FXMAC_INFO("autonegotiation complete  ");

    ret = FXmacPhyRead(instance_p, phy_addr, PHY_SPECIFIC_STATUS_REG, &temp);
    if (ret != FT_SUCCESS)
    {
        FXMAC_ERROR("%s line is %d,read PHY_SPECIFIC_STATUS_REG is error", __func__, __LINE__);
        return ret;
    }

    FXMAC_INFO("temp is %x \r\n", temp);
    ret = FXmacPhyRead(instance_p, phy_addr, PHY_STATUS_REG_OFFSET, &temp2);
    if (ret != FT_SUCCESS)
    {
        FXMAC_ERROR("%s line is %d,read PHY_STATUS_REG_OFFSET is error", __func__, __LINE__);
        return ret;
    }

    FXMAC_INFO("temp2 is %x \r\n", temp2);

    if (temp & (1 << 13))
    {
        FXMAC_INFO("duplex is full \r\n");
        instance_p->config.duplex = 1;
    }
    else
    {
        FXMAC_INFO("duplex is half \r\n");
        instance_p->config.duplex = 0;
    }

    if ((temp & 0xC000) == PHY_SPECIFIC_STATUS_SPEED_1000M)
    {
        FXMAC_INFO("speed is 1000\r\n");
        instance_p->config.speed = 1000;
    }
    else if ((temp & 0xC000) == PHY_SPECIFIC_STATUS_SPEED_100M)
    {
        FXMAC_INFO("speed is 100\r\n");
        instance_p->config.speed = 100;
    }
    else
    {
        FXMAC_INFO("speed is 10\r\n");
        instance_p->config.speed = 10;
    }

    return FT_SUCCESS;
}

void FxmaxLinkupCheck(void)
{
    u16 temp;
    FXmacPhyRead(instance_b, phy_addr_b, PHY_SPECIFIC_STATUS_REG, &temp);
    FXMAC_INFO("0x17 value is %x \r\n", temp);
    FXMAC_INFO("linkup status is   %x \r\n", temp & (1 << 10));
}


static FError FXmacConfigureIeeePhySpeed(FXmac *instance_p, u32 phy_addr, u32 speed, u32 duplex_mode)
{
    u16 control;
    u16 autonereg;
    volatile s32 wait;
    FError ret;
    u16 specific_reg = 0;

    FXMAC_INFO("manual setting ,phy_addr is %d,speed %d, duplex_mode is %d \r\n", phy_addr, speed, duplex_mode);

    ret = FXmacPhyRead(instance_p, phy_addr, PHY_AUTONEGO_ADVERTISE_REG, &autonereg);
    if (ret != FT_SUCCESS)
    {
        FXMAC_ERROR("%s line is %d,read PHY_AUTONEGO_ADVERTISE_REG is error", __func__, __LINE__);
        return ret;
    }

    autonereg |= PHY_AUTOADVERTISE_ASYMMETRIC_PAUSE_MASK;
    autonereg |= PHY_AUTOADVERTISE_PAUSE_MASK;
    ret = FXmacPhyWrite(instance_p, phy_addr, PHY_AUTONEGO_ADVERTISE_REG, autonereg);
    if (ret != FT_SUCCESS)
    {
        FXMAC_ERROR("%s line is %d,write PHY_AUTONEGO_ADVERTISE_REG is error", __func__, __LINE__);
        return ret;
    }


    ret = FXmacPhyRead(instance_p, phy_addr, PHY_CONTROL_REG_OFFSET, &control);
    if (ret != FT_SUCCESS)
    {
        FXMAC_ERROR("%s line is %d,read PHY_AUTONEGO_ADVERTISE_REG is error", __func__, __LINE__);
        return ret;
    }
    FXMAC_INFO("PHY_CONTROL_REG_OFFSET is %x \r\n", control);


    control &= ~PHY_CONTROL_LINKSPEED_1000M;
    control &= ~PHY_CONTROL_LINKSPEED_100M;
    control &= ~PHY_CONTROL_LINKSPEED_10M;

    if (speed == 1000)
    {
        control |= PHY_CONTROL_LINKSPEED_1000M;
    }
    else if (speed == 100)
    {
        control |= PHY_CONTROL_LINKSPEED_100M;
    }
    else if (speed == 10)
    {
        control |= PHY_CONTROL_LINKSPEED_10M;
    }

    if (duplex_mode == 1)
    {
        control |= PHY_CONTROL_FULL_DUPLEX_MASK;
    }
    else
    {
        control &= ~PHY_CONTROL_FULL_DUPLEX_MASK;
    }

    /* disable auto-negotiation */
    control &= ~(PHY_CONTROL_AUTONEGOTIATE_ENABLE);
    control &= ~(PHY_CONTROL_AUTONEGOTIATE_RESTART);

    ret = FXmacPhyWrite(instance_p, phy_addr, PHY_CONTROL_REG_OFFSET, control); /* Technology Ability Field */
    if (ret != FT_SUCCESS)
    {
        FXMAC_ERROR("%s line is %d,write PHY_AUTONEGO_ADVERTISE_REG is error", __func__, __LINE__);
        return ret;
    }

    for (wait = 0; wait < 100000; wait++)
        ;

    FXMAC_INFO("Manual selection completed \r\n");

    ret = FXmacPhyRead(instance_p, phy_addr, PHY_SPECIFIC_STATUS_REG, &specific_reg);
    if (ret != FT_SUCCESS)
    {
        FXMAC_ERROR("%s line is %d,read PHY_SPECIFIC_STATUS_REG is error", __func__, __LINE__);
        return ret;
    }

    FXMAC_INFO("specific_reg is %x \r\n", specific_reg);

    if (specific_reg & (1 << 13))
    {
        FXMAC_INFO("duplex is full \r\n");
        instance_p->config.duplex = 1;
    }
    else
    {
        FXMAC_INFO("duplex is half \r\n");
        instance_p->config.duplex = 0;
    }

    if ((specific_reg & 0xC000) == PHY_SPECIFIC_STATUS_SPEED_1000M)
    {
        FXMAC_INFO("speed is 1000\r\n");
        instance_p->config.speed = 1000;
    }
    else if ((specific_reg & 0xC000) == PHY_SPECIFIC_STATUS_SPEED_100M)
    {
        FXMAC_INFO("speed is 100\r\n");
        instance_p->config.speed = 100;
    }
    else
    {
        FXMAC_INFO("speed is 10\r\n");
        instance_p->config.speed = 10;
    }

    return FT_SUCCESS;
}

/**
 * @name: FXmacPhyInit
 * @msg:  setup the PHYs for proper speed setting.
 * @param {FXmac} *instance_p is a pointer to the instance to be worked on.
 * @param {u32} speed is phy operating speed
 * @param {u32} phy_addr is the address of the PHY to be read (supports multiple PHYs)
 * @param {u32} duplex_mode is The duplex mode can be selected via either the Auto-Negotiation process or manual duplex selection.
 * @param {u32} autonegotiation_en is an auto-negotiated flag . 1 is enable auto ,0 is manual
 * @return {FError}
 */
FError FXmacPhyInit(FXmac *instance_p, u32 speed, u32 duplex_mode, u32 autonegotiation_en)
{
    FError ret;
    u32 index = 0, phy_mask;
    u16 phy_identity;
    u32 phy_addr;

    if (FXmacDetect(instance_p, &phy_addr) != FT_SUCCESS)
    {
        FXMAC_ERROR("phy is not found");
        return FXMAC_PHY_IS_NOT_FOUND;
    }
    FXMAC_INFO("settings phy_addr is %d\n", phy_addr);
    instance_p->phy_address = phy_addr;
    if (autonegotiation_en)
    {
        ret = FXmacGetIeeePhySpeed(instance_p, phy_addr);
        if (ret != FT_SUCCESS)
        {
            return ret;
        }
    }
    else
    {
        FXMAC_INFO("Set the communication speed manually");
        ret = FXmacConfigureIeeePhySpeed(instance_p, phy_addr, speed, duplex_mode);
        if (ret != FT_SUCCESS)
        {
            FXMAC_ERROR("Failed to manually set the PHY");
            return ret;
        }

    }

    instance_p->link_status = FXMAC_LINKUP;
    return FT_SUCCESS;
}