summaryrefslogtreecommitdiffhomepage
path: root/source/ecp5_driver.c
blob: 99373cd9c9629adb628c424d2ce181e65bf9c634 (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
/*
 *       cmds.c
 *
 *       Copyright (C) 2018, 2019 Mind Chasers Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 *   function: Initialize ECP PCS block
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include "board.h"
#include "fsl_debug_console.h"
#include "fsl_i2c.h"
#include "ecp5_driver.h"
#include "cmds.h"
#include "utils.h"
#include "main.h"


/* set PCS/SERDES block to reset */
void ecp5_pcs_reset(i2c_master_transfer_t* pXfer) {
	// reset our sgmii_cont modules, but leave the PHYs out of reset
	i2c_write(pXfer,  I2C_ECP5_ADDR, (uint8_t*) &x_init0, sizeof(x_init0));

	// assert all channel resets
	i2c_write(pXfer,  I2C_ECP5_ADDR, (uint8_t*) &c_reset, sizeof(c_reset));

	// assert all DCU resets
	i2c_write(pXfer,  I2C_ECP5_ADDR, (uint8_t*) &u_reset, sizeof(u_reset));
}


int ecp5_pcs_init(i2c_master_transfer_t* pXfer, uint8_t* pBuff) {
	uint8_t errors = 0;


	// clear the I2C buffer of anything in the read FIFO
	i2c_read(pXfer, I2C_ECP5_ADDR, (uint8_t*) pBuff, 8);

	/*
	 * Check RX and TX status for health
	 * If PLL isn't locked, then we need a reset.
	 */
	PRINTF("Check PCS Status (p) \r\n");
	i2c_write(pXfer,I2C_ECP5_ADDR, (uint8_t*) &pcs_s, sizeof(pcs_s));
	i2c_read(pXfer, I2C_ECP5_ADDR, (uint8_t*) pBuff, 8);
	dump_data((uint8_t*) pBuff, 8, true);
	errors = ascii_to_bin(pBuff[2]) << 4 | ascii_to_bin(pBuff[3]);

	if (errors == 0 ) {
		return 0;
	}

	PRINTF("Channel 1, DCU1 Init (u00) \r\n");
	i2c_write(pXfer,  I2C_ECP5_ADDR, (uint8_t*) &u_init, sizeof(u_init));
	i2c_read(pXfer,   I2C_ECP5_ADDR, (uint8_t*) pBuff, 8);
	dump_data((uint8_t*) pBuff, 8, true);

	PRINTF("Remove SERDES Channel Resets (c555) :\r\n");
	i2c_write(pXfer,  I2C_ECP5_ADDR, (uint8_t*) &c_init0, sizeof(c_init0));
	i2c_read(pXfer,   I2C_ECP5_ADDR, (uint8_t*) pBuff, 8);
	dump_data((uint8_t*) pBuff, 8, true);

    for (int i=0; i<20; i++) {
            PRINTF("Check PCS Status (p) \r\n");
            i2c_write(pXfer, I2C_ECP5_ADDR, (uint8_t*) &pcs_s, sizeof(pcs_s));
            i2c_read(pXfer, I2C_ECP5_ADDR, (uint8_t*) pBuff, 8);
            dump_data((uint8_t*) pBuff, 8, true);

            if ( pBuff[3] & PLL_LOL_0 ) {
                    PRINTF("Keep Testing Until PLL Lock\r\n");
                    if (i==19) {
                            // give up
                            ecp5_pcs_reset(pXfer);
                            return -1;
                    }
            }
            else {
                    break;
            }
    }


	PRINTF("Remove PCS Channel Resets (c000) :\r\n");
	i2c_write(pXfer,  I2C_ECP5_ADDR, (uint8_t*) &c_init1, sizeof(c_init1));
	i2c_read(pXfer,   I2C_ECP5_ADDR, (uint8_t*) pBuff, 8);
	dump_data((uint8_t*) pBuff, 8, true);

	PRINTF("Remove SGMII Controller Resets (x000) :\r\n");
	i2c_write(pXfer,  I2C_ECP5_ADDR, (uint8_t*) &x_init1, sizeof(x_init1));
	i2c_read(pXfer,   I2C_ECP5_ADDR, (uint8_t*) pBuff, 8);
	dump_data((uint8_t*) pBuff, 8, true);

	PRINTF("Check PCS Status (p) \r\n");
	i2c_write(pXfer,  I2C_ECP5_ADDR, (uint8_t*) &pcs_s, sizeof(pcs_s));
	i2c_read(pXfer,   I2C_ECP5_ADDR, (uint8_t*) pBuff, 8);
	dump_data((uint8_t*) pBuff, 8, true);
	errors = ascii_to_bin(pBuff[2]) << 4 | ascii_to_bin(pBuff[3]);

	if ( errors == 0) {
		return 0;
	}
	else {
		// configure resets back to default, so we can try again
		ecp5_pcs_reset(pXfer);
		return -1;
	}

	return 0;
}