1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2049de79dSDirk Eibach /*
3049de79dSDirk Eibach * (C) Copyright 2012
4049de79dSDirk Eibach * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
5049de79dSDirk Eibach */
6049de79dSDirk Eibach
7049de79dSDirk Eibach #include <common.h>
8049de79dSDirk Eibach #include <asm/io.h>
9049de79dSDirk Eibach #include <errno.h>
10049de79dSDirk Eibach
11049de79dSDirk Eibach #include <gdsys_fpga.h>
12049de79dSDirk Eibach
13049de79dSDirk Eibach enum {
14049de79dSDirk Eibach MCINT_SLAVE_LINK_CHANGED_EV = 1 << 7,
15049de79dSDirk Eibach MCINT_TX_ERROR_EV = 1 << 9,
16049de79dSDirk Eibach MCINT_TX_BUFFER_FREE = 1 << 10,
17049de79dSDirk Eibach MCINT_TX_PACKET_TRANSMITTED_EV = 1 << 11,
18049de79dSDirk Eibach MCINT_RX_ERROR_EV = 1 << 13,
19049de79dSDirk Eibach MCINT_RX_CONTENT_AVAILABLE = 1 << 14,
20049de79dSDirk Eibach MCINT_RX_PACKET_RECEIVED_EV = 1 << 15,
21049de79dSDirk Eibach };
22049de79dSDirk Eibach
mclink_probe(void)23049de79dSDirk Eibach int mclink_probe(void)
24049de79dSDirk Eibach {
25049de79dSDirk Eibach unsigned int k;
26049de79dSDirk Eibach int slaves = 0;
27049de79dSDirk Eibach
28049de79dSDirk Eibach for (k = 0; k < CONFIG_SYS_MCLINK_MAX; ++k) {
29049de79dSDirk Eibach int timeout = 0;
30049de79dSDirk Eibach unsigned int ctr = 0;
31049de79dSDirk Eibach u16 mc_status;
32049de79dSDirk Eibach
33049de79dSDirk Eibach FPGA_GET_REG(k, mc_status, &mc_status);
34049de79dSDirk Eibach
35049de79dSDirk Eibach if (!(mc_status & (1 << 15)))
36049de79dSDirk Eibach break;
37049de79dSDirk Eibach
38049de79dSDirk Eibach FPGA_SET_REG(k, mc_control, 0x8000);
39049de79dSDirk Eibach
40049de79dSDirk Eibach FPGA_GET_REG(k, mc_status, &mc_status);
41049de79dSDirk Eibach while (!(mc_status & (1 << 14))) {
42049de79dSDirk Eibach udelay(100);
43049de79dSDirk Eibach if (ctr++ > 500) {
44049de79dSDirk Eibach timeout = 1;
45049de79dSDirk Eibach break;
46049de79dSDirk Eibach }
47049de79dSDirk Eibach FPGA_GET_REG(k, mc_status, &mc_status);
48049de79dSDirk Eibach }
49049de79dSDirk Eibach if (timeout)
50049de79dSDirk Eibach break;
51049de79dSDirk Eibach
52049de79dSDirk Eibach printf("waited %d us for mclink %d to come up\n", ctr * 100, k);
53049de79dSDirk Eibach
54049de79dSDirk Eibach slaves++;
55049de79dSDirk Eibach }
56049de79dSDirk Eibach
57049de79dSDirk Eibach return slaves;
58049de79dSDirk Eibach }
59049de79dSDirk Eibach
mclink_send(u8 slave,u16 addr,u16 data)60049de79dSDirk Eibach int mclink_send(u8 slave, u16 addr, u16 data)
61049de79dSDirk Eibach {
62049de79dSDirk Eibach unsigned int ctr = 0;
63049de79dSDirk Eibach u16 int_status;
64049de79dSDirk Eibach u16 rx_cmd_status;
65049de79dSDirk Eibach u16 rx_cmd;
66049de79dSDirk Eibach
67049de79dSDirk Eibach /* reset interrupt status */
68049de79dSDirk Eibach FPGA_GET_REG(0, mc_int, &int_status);
69049de79dSDirk Eibach FPGA_SET_REG(0, mc_int, int_status);
70049de79dSDirk Eibach
71049de79dSDirk Eibach /* send */
72049de79dSDirk Eibach FPGA_SET_REG(0, mc_tx_address, addr);
73049de79dSDirk Eibach FPGA_SET_REG(0, mc_tx_data, data);
74049de79dSDirk Eibach FPGA_SET_REG(0, mc_tx_cmd, (slave & 0x03) << 14);
75049de79dSDirk Eibach FPGA_SET_REG(0, mc_control, 0x8001);
76049de79dSDirk Eibach
77049de79dSDirk Eibach /* wait for reply */
78049de79dSDirk Eibach FPGA_GET_REG(0, mc_int, &int_status);
79049de79dSDirk Eibach while (!(int_status & MCINT_RX_PACKET_RECEIVED_EV)) {
80049de79dSDirk Eibach udelay(100);
81049de79dSDirk Eibach if (ctr++ > 3)
82049de79dSDirk Eibach return -ETIMEDOUT;
83049de79dSDirk Eibach FPGA_GET_REG(0, mc_int, &int_status);
84049de79dSDirk Eibach }
85049de79dSDirk Eibach
86049de79dSDirk Eibach FPGA_GET_REG(0, mc_rx_cmd_status, &rx_cmd_status);
87049de79dSDirk Eibach rx_cmd = (rx_cmd_status >> 12) & 0x03;
88049de79dSDirk Eibach if (rx_cmd != 0)
89049de79dSDirk Eibach printf("mclink_send: received cmd %d, expected %d\n", rx_cmd,
90049de79dSDirk Eibach 0);
91049de79dSDirk Eibach
92049de79dSDirk Eibach return 0;
93049de79dSDirk Eibach }
94049de79dSDirk Eibach
mclink_receive(u8 slave,u16 addr,u16 * data)95049de79dSDirk Eibach int mclink_receive(u8 slave, u16 addr, u16 *data)
96049de79dSDirk Eibach {
97049de79dSDirk Eibach u16 rx_cmd_status;
98049de79dSDirk Eibach u16 rx_cmd;
99049de79dSDirk Eibach u16 int_status;
100049de79dSDirk Eibach unsigned int ctr = 0;
101049de79dSDirk Eibach
102049de79dSDirk Eibach /* send read request */
103049de79dSDirk Eibach FPGA_SET_REG(0, mc_tx_address, addr);
104049de79dSDirk Eibach FPGA_SET_REG(0, mc_tx_cmd,
105049de79dSDirk Eibach ((slave & 0x03) << 14) | (1 << 12) | (1 << 0));
106049de79dSDirk Eibach FPGA_SET_REG(0, mc_control, 0x8001);
107049de79dSDirk Eibach
108049de79dSDirk Eibach
109049de79dSDirk Eibach /* wait for reply */
110049de79dSDirk Eibach FPGA_GET_REG(0, mc_int, &int_status);
111049de79dSDirk Eibach while (!(int_status & MCINT_RX_CONTENT_AVAILABLE)) {
112049de79dSDirk Eibach udelay(100);
113049de79dSDirk Eibach if (ctr++ > 3)
114049de79dSDirk Eibach return -ETIMEDOUT;
115049de79dSDirk Eibach FPGA_GET_REG(0, mc_int, &int_status);
116049de79dSDirk Eibach }
117049de79dSDirk Eibach
118049de79dSDirk Eibach /* check reply */
119049de79dSDirk Eibach FPGA_GET_REG(0, mc_rx_cmd_status, &rx_cmd_status);
120049de79dSDirk Eibach if ((rx_cmd_status >> 14) != slave) {
121049de79dSDirk Eibach printf("mclink_receive: reply from slave %d, expected %d\n",
122049de79dSDirk Eibach rx_cmd_status >> 14, slave);
123049de79dSDirk Eibach return -EINVAL;
124049de79dSDirk Eibach }
125049de79dSDirk Eibach
126049de79dSDirk Eibach rx_cmd = (rx_cmd_status >> 12) & 0x03;
127049de79dSDirk Eibach if (rx_cmd != 1) {
128049de79dSDirk Eibach printf("mclink_send: received cmd %d, expected %d\n",
129049de79dSDirk Eibach rx_cmd, 1);
130049de79dSDirk Eibach return -EIO;
131049de79dSDirk Eibach }
132049de79dSDirk Eibach
133049de79dSDirk Eibach FPGA_GET_REG(0, mc_rx_data, data);
134049de79dSDirk Eibach
135049de79dSDirk Eibach return 0;
136049de79dSDirk Eibach }
137