aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/mediatek/files-5.10/drivers/net/phy/rtk/rtl8367c/rtl8367c_asicdrv_i2c.c
blob: 69f20a0188b5aac7d14ef09ee24d9d65f4558f54 (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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
 * Copyright (C) 2013 Realtek Semiconductor Corp.
 * All Rights Reserved.
 *
 * Unless you and Realtek execute a separate written software license
 * agreement governing use of this software, this software is licensed
 * to you under the terms of the GNU General Public License version 2,
 * available at https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 *
 * $Revision: 38651 $
 * $Date: 2016-02-27 14:32:56 +0800 (周三, 17 四月 2016) $
 *
 * Purpose : RTL8367C switch high-level API for RTL8367C
 * Feature : I2C related functions
 *
 */


#include <rtl8367c_asicdrv_i2c.h>
#include <rtk_error.h>
#include <rtk_types.h>



/* Function Name:
 *      rtl8367c_setAsicI2C_checkBusIdle
 * Description:
 *      Check i2c bus status idle or not
 * Input:
 *      None
 * Output:
 *      None
 * Return:
 *      RT_ERR_OK                - Success
 *      RT_ERR_BUSYWAIT_TIMEOUT  - i2c bus is busy
 * Note:
 *      This API can check i2c bus status.
 */
ret_t rtl8367c_setAsicI2C_checkBusIdle(void)
{
    rtk_uint32 regData;
    ret_t retVal;

    if ((retVal = rtl8367c_getAsicRegBit(RTL8367C_REG_M_I2C_CTL_STA_REG, RTL8367C_M_I2C_BUS_IDLE_OFFSET, &regData)) != RT_ERR_OK)
        return retVal;

    if(regData == 0x0001)
        return RT_ERR_OK; /*i2c is idle*/
    else
        return RT_ERR_BUSYWAIT_TIMEOUT; /*i2c is busy*/
}


/* Function Name:
 *      rtl8367c_setAsicI2CStartCmd
 * Description:
 *      Set I2C start command
 * Input:
 *      None
 * Output:
 *      None
 * Return:
 *      RT_ERR_OK                - Success
 * Note:
 *      This API can set i2c start command ,start a i2c traffic  .
 */
ret_t rtl8367c_setAsicI2CStartCmd(void)
{
    rtk_uint32 regData;
    ret_t retVal;

    /* Bits [4-1] = 0b0000, Start Command; Bit [0] = 1, Trigger the Command */
    if ((retVal = rtl8367c_getAsicReg(RTL8367C_REG_M_I2C_CTL_STA_REG, &regData)) != RT_ERR_OK)
        return retVal;
    regData &= 0xFFE0;
    regData |= 0x0001;

    if ((retVal = rtl8367c_setAsicReg(RTL8367C_REG_M_I2C_CTL_STA_REG, regData)) != RT_ERR_OK)
        return retVal;

    /* wait for command finished */
    do{
       if ((retVal = rtl8367c_getAsicRegBit(RTL8367C_REG_M_I2C_CTL_STA_REG, RTL8367C_I2C_CMD_EXEC_OFFSET, &regData)) != RT_ERR_OK)
            return retVal;
    }while( regData != 0x0);

    return RT_ERR_OK ;
}

/* Function Name:
 *      rtl8367c_setAsicI2CStopCmd
 * Description:
 *      Set I2C stop command
 * Input:
 *      None
 * Output:
 *      None
 * Return:
 *      RT_ERR_OK                - Success
 * Note:
 *      This API can set i2c stop command ,stop a i2c traffic.
 */
ret_t rtl8367c_setAsicI2CStopCmd(void)
{

    rtk_uint32 regData;
    ret_t retVal;

    /* Bits [4-1] = 0b0001, Stop Command; Bit [0] = 1, Trigger the Command */
    if ((retVal = rtl8367c_getAsicReg(RTL8367C_REG_M_I2C_CTL_STA_REG, &regData)) != RT_ERR_OK)
        return retVal;
    regData &= 0xFFE0;
    regData |= 0x0003;

    if ((retVal = rtl8367c_setAsicReg(RTL8367C_REG_M_I2C_CTL_STA_REG, regData)) != RT_ERR_OK)
        return retVal;


    /* wait for command finished */
    do{
       if ((retVal = rtl8367c_getAsicRegBit(RTL8367C_REG_M_I2C_CTL_STA_REG, RTL8367C_I2C_CMD_EXEC_OFFSET, &regData)) != RT_ERR_OK)
            return retVal;
    }while( regData != 0x0);

    return RT_ERR_OK ;
}

/* Function Name:
 *      rtl8367c_setAsicI2CTxOneCharCmd
 * Description:
 *      Set I2C Tx a char command, with a 8-bit data
 * Input:
 *      oneChar - 8-bit data
 * Output:
 *      None
 * Return:
 *      RT_ERR_OK                - Success
 * Note:
 *      This API can set i2c Tx command and with a 8-bit data.
 */
ret_t rtl8367c_setAsicI2CTxOneCharCmd(rtk_uint8 oneChar)
{
    rtk_uint32 regData;
    ret_t retVal;

    /* Bits [4-1] = 0b0010, tx one char; Bit [0] = 1, Trigger the Command */
    if ((retVal = rtl8367c_getAsicReg(RTL8367C_REG_M_I2C_CTL_STA_REG, &regData)) != RT_ERR_OK)
        return retVal;

    regData &= 0xFFE0;
    regData |= 0x0005;
    regData &= 0x00FF;
    regData |= (rtk_uint16) (oneChar << 8);

    if ((retVal = rtl8367c_setAsicReg(RTL8367C_REG_M_I2C_CTL_STA_REG, regData)) != RT_ERR_OK)
        return retVal;


   /* wait for command finished */
    do{
       if ((retVal = rtl8367c_getAsicRegBit(RTL8367C_REG_M_I2C_CTL_STA_REG, RTL8367C_I2C_CMD_EXEC_OFFSET, &regData)) != RT_ERR_OK)
            return retVal;
    }while( regData != 0x0);

    return RT_ERR_OK ;
}


/* Function Name:
 *      rtl8367c_setAsicI2CcheckRxAck
 * Description:
 *      Check if rx an Ack
 * Input:
 *      None
 * Output:
 *      None
 * Return:
 *      RT_ERR_OK                - Success
 * Note:
 *      This API can check if rx an ack from i2c slave.
 */
ret_t rtl8367c_setAsicI2CcheckRxAck(void)
{
    rtk_uint32 regData;
    ret_t retVal;
    rtk_uint32 count = 0;

    do{
         count++;
         if ((retVal = rtl8367c_getAsicRegBit(RTL8367C_REG_M_I2C_CTL_STA_REG, RTL8367C_SLV_ACK_FLAG_OFFSET, &regData)) != RT_ERR_OK)
            return retVal;
    }while( (regData != 0x1) && (count < TIMEROUT_FOR_MICROSEMI) );

    if(regData != 0x1)
        return RT_ERR_FAILED;
    else
        return RT_ERR_OK;
}


/* Function Name:
 *      rtl8367c_setAsicI2CRxOneCharCmd
 * Description:
 *      Set I2C Rx command and get 8-bit data
 * Input:
 *      None
 * Output:
 *      pValue - 8bit-data
 * Return:
 *      RT_ERR_OK                - Success
 * Note:
 *      This API can set I2C Rx command and get 8-bit data.
 */
ret_t rtl8367c_setAsicI2CRxOneCharCmd(rtk_uint8 *pValue)
{
    rtk_uint32 regData;
    ret_t retVal;

    /* Bits [4-1] = 0b0011, Rx one char; Bit [0] = 1, Trigger the Command */
    if ((retVal = rtl8367c_getAsicReg(RTL8367C_REG_M_I2C_CTL_STA_REG, &regData)) != RT_ERR_OK)
        return retVal;
    regData &= 0xFFE0;
    regData |= 0x0007;
    if ((retVal = rtl8367c_setAsicReg(RTL8367C_REG_M_I2C_CTL_STA_REG, regData)) != RT_ERR_OK)
        return retVal;

    /* wait for command finished */
     do{
        if ((retVal = rtl8367c_getAsicReg(RTL8367C_REG_M_I2C_CTL_STA_REG, &regData)) != RT_ERR_OK)
             return retVal;
     }while( (regData & 0x1) != 0x0);

    *pValue = (rtk_uint8)(regData >> 8);
     return RT_ERR_OK ;

}

/* Function Name:
 *      rtl8367c_setAsicI2CTxAckCmd
 * Description:
 *      Set I2C Tx ACK command
 * Input:
 *      None
 * Output:
 *      None
 * Return:
 *      RT_ERR_OK                - Success
 * Note:
 *      This API can set I2C Tx ack command.
 */
ret_t rtl8367c_setAsicI2CTxAckCmd(void)
{
    rtk_uint32 regData;
    ret_t retVal;

    /* Bits [4-1] = 0b0100, tx ACK Command; Bit [0] = 1, Trigger the Command */
    if ((retVal = rtl8367c_getAsicReg(RTL8367C_REG_M_I2C_CTL_STA_REG, &regData)) != RT_ERR_OK)
        return retVal;
    regData &= 0xFFE0;
    regData |= 0x0009;
    if ((retVal = rtl8367c_setAsicReg(RTL8367C_REG_M_I2C_CTL_STA_REG, regData)) != RT_ERR_OK)
        return retVal;

     /* wait for command finished */
    do{
       if ((retVal = rtl8367c_getAsicRegBit(RTL8367C_REG_M_I2C_CTL_STA_REG, RTL8367C_I2C_CMD_EXEC_OFFSET, &regData)) != RT_ERR_OK)
            return retVal;
    }while( regData != 0x0);

    return RT_ERR_OK ;

}


/* Function Name:
 *      rtl8367c_setAsicI2CTxNoAckCmd
 * Description:
 *      Set I2C master Tx noACK command
 * Input:
 *      None
 * Output:
 *      None
 * Return:
 *      RT_ERR_OK                - Success
 * Note:
 *      This API can set I2C master Tx noACK command.
 */
ret_t rtl8367c_setAsicI2CTxNoAckCmd(void)
{
    rtk_uint32 regData;
    ret_t retVal;

    /* Bits [4-1] = 0b0101, tx noACK Command; Bit [0] = 1, Trigger the Command */
    if ((retVal = rtl8367c_getAsicReg(RTL8367C_REG_M_I2C_CTL_STA_REG, &regData)) != RT_ERR_OK)
        return retVal;
    regData &= 0xFFE0;
    regData |= 0x000b;
    if ((retVal = rtl8367c_setAsicReg(RTL8367C_REG_M_I2C_CTL_STA_REG, regData)) != RT_ERR_OK)
        return retVal;

     /* wait for command finished */
    do{
       if ((retVal = rtl8367c_getAsicRegBit(RTL8367C_REG_M_I2C_CTL_STA_REG, RTL8367C_I2C_CMD_EXEC_OFFSET, &regData)) != RT_ERR_OK)
            return retVal;
    }while( regData != 0x0);

    return RT_ERR_OK ;

}

/* Function Name:
 *      rtl8367c_setAsicI2CSoftRSTseqCmd
 * Description:
 *      set I2C master tx soft reset command
 * Input:
 *      None
 * Output:
 *      None
 * Return:
 *      RT_ERR_OK                - Success
 * Note:
 *      This API can set I2C master tx soft reset command.
 */
ret_t rtl8367c_setAsicI2CSoftRSTseqCmd(void)
{

    rtk_uint32 regData;
    ret_t retVal;

    /*Bits [4-1] = 0b0110, tx soft reset Command;  Bit [0] = 1, Trigger the Command */
    if ((retVal = rtl8367c_getAsicReg(RTL8367C_REG_M_I2C_CTL_STA_REG, &regData)) != RT_ERR_OK)
        return retVal;
    regData &= 0xFFE0;
    regData |= 0x000d;

    if ((retVal = rtl8367c_setAsicReg(RTL8367C_REG_M_I2C_CTL_STA_REG, regData)) != RT_ERR_OK)
        return retVal;


    /* wait for command finished */
    do{
       if ((retVal = rtl8367c_getAsicRegBit(RTL8367C_REG_M_I2C_CTL_STA_REG, RTL8367C_I2C_CMD_EXEC_OFFSET, &regData)) != RT_ERR_OK)
            return retVal;
    }while( regData != 0x0);

    return RT_ERR_OK ;
}


/* Function Name:
 *      rtl8367c_setAsicI2CGpioPinGroup
 * Description:
 *      set I2C function used gpio pins
 * Input:
 *      pinGroup_ID - gpio pins group
 * Output:
 *      None
 * Return:
 *      RT_ERR_OK                - Success
 *      RT_ERR_INPUT             _ Invalid input parameter
 * Note:
 *      This API can set I2C function used gpio pins.
 *      There are three group gpio pins
 */
ret_t rtl8367c_setAsicI2CGpioPinGroup(rtk_uint32 pinGroup_ID)
{
    rtk_uint32 regData;
    ret_t retVal;

    if ((retVal = rtl8367c_getAsicReg(RTL8367C_REG_M_I2C_SYS_CTL, &regData)) != RT_ERR_OK)
         return retVal;
    if( pinGroup_ID==0 )
    {
        regData &= 0x0FFF;
        regData |= 0x5000;

        if ((retVal = rtl8367c_setAsicReg(RTL8367C_REG_M_I2C_SYS_CTL, regData)) != RT_ERR_OK)
             return retVal;
    }

    else if( pinGroup_ID==1 )
    {
        regData &= 0x0FFF;
        regData |= 0xA000;

        if ((retVal = rtl8367c_setAsicReg(RTL8367C_REG_M_I2C_SYS_CTL, regData)) != RT_ERR_OK)
             return retVal;
    }

    else if( pinGroup_ID==2 )
    {
        regData &= 0x0FFF;
        regData |= 0xF000;

        if ((retVal = rtl8367c_setAsicReg(RTL8367C_REG_M_I2C_SYS_CTL, regData)) != RT_ERR_OK)
             return retVal;
    }
    else
        return RT_ERR_INPUT;

    return RT_ERR_OK ;

}

/* Function Name:
 *      rtl8367c_setAsicI2CGpioPinGroup
 * Description:
 *      set I2C function used gpio pins
 * Input:
 *      pinGroup_ID - gpio pins group
 * Output:
 *      None
 * Return:
 *      RT_ERR_OK                - Success
 *      RT_ERR_INPUT             _ Invalid input parameter
 * Note:
 *      This API can set I2C function used gpio pins.
 *      There are three group gpio pins
 */
ret_t rtl8367c_getAsicI2CGpioPinGroup(rtk_uint32 * pPinGroup_ID)
{

    rtk_uint32 regData;
    ret_t retVal;
    if( (retVal = rtl8367c_getAsicReg(RTL8367C_REG_M_I2C_SYS_CTL, &regData)) != RT_ERR_OK)
        return retVal;
    regData &= 0xF000 ;
    regData = (regData >> 12);

    if( regData == 0x5 )
        *pPinGroup_ID = 0;
    else if(regData == 0xA)
        *pPinGroup_ID = 1;
    else if(regData == 0xF)
        *pPinGroup_ID = 2;
    else
       return RT_ERR_FAILED;
    return RT_ERR_OK ;
}