xref: /aosp_15_r20/external/coreboot/src/vendorcode/cavium/bdk/libbdk-driver/bdk-driver-mdio.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /***********************license start***********************************
2 * Copyright (c) 2003-2017  Cavium Inc. ([email protected]). All rights
3 * reserved.
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *   * Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 *
13 *   * Redistributions in binary form must reproduce the above
14 *     copyright notice, this list of conditions and the following
15 *     disclaimer in the documentation and/or other materials provided
16 *     with the distribution.
17 *
18 *   * Neither the name of Cavium Inc. nor the names of
19 *     its contributors may be used to endorse or promote products
20 *     derived from this software without specific prior written
21 *     permission.
22 *
23 * This Software, including technical data, may be subject to U.S. export
24 * control laws, including the U.S. Export Administration Act and its
25 * associated regulations, and may be subject to export or import
26 * regulations in other countries.
27 *
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT
31 * TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
32 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
33 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
34 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
35 * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT,
36 * QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK
37 * ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
39 #include <bdk.h>
40 #include <libbdk-arch/bdk-csrs-pccpf.h>
41 #include <libbdk-arch/bdk-csrs-smi.h>
42 #include <libbdk-hal/device/bdk-device.h>
43 #include <libbdk-hal/bdk-mdio.h>
44 
45 /* This code is an optional part of the BDK. It is only linked in
46     if BDK_REQUIRE() needs it */
47 BDK_REQUIRE_DEFINE(MDIO);
48 
49 /* To maintain backwards compatibility for the old MDIO API we need
50    to lookup the MDIO device on the ECAM bus by ID. This defines
51    the ID */
52 #define MDIO_DEVID ((BDK_PCC_PROD_E_GEN << 24) | BDK_PCC_VENDOR_E_CAVIUM | (BDK_PCC_DEV_IDL_E_SMI << 16))
53 
54 #define BDK_MDIO_TIMEOUT   100000 /* 100 millisec */
55 
56 /* Operating request encodings. */
57 #define MDIO_CLAUSE_22_WRITE    0
58 #define MDIO_CLAUSE_22_READ     1
59 
60 #define MDIO_CLAUSE_45_ADDRESS  0
61 #define MDIO_CLAUSE_45_WRITE    1
62 #define MDIO_CLAUSE_45_READ_INC 2
63 #define MDIO_CLAUSE_45_READ     3
64 
65 /**
66  * Helper function to put MDIO interface into clause 45 mode
67  *
68  * @param bus_id
69  */
__bdk_mdio_set_clause45_mode(const bdk_device_t * device,int bus_id)70 static void __bdk_mdio_set_clause45_mode(const bdk_device_t *device, int bus_id)
71 {
72     bdk_smi_x_clk_t smi_clk;
73     /* Put bus into clause 45 mode */
74     smi_clk.u = BDK_BAR_READ(device, BDK_SMI_X_CLK(bus_id));
75     if (smi_clk.s.mode != 1)
76     {
77         smi_clk.s.mode = 1;
78         smi_clk.s.preamble = 1;
79         BDK_BAR_WRITE(device, BDK_SMI_X_CLK(bus_id), smi_clk.u);
80     }
81 }
82 
83 /**
84  * Helper function to put MDIO interface into clause 22 mode
85  *
86  * @param bus_id
87  */
__bdk_mdio_set_clause22_mode(const bdk_device_t * device,int bus_id)88 static void __bdk_mdio_set_clause22_mode(const bdk_device_t *device, int bus_id)
89 {
90     bdk_smi_x_clk_t smi_clk;
91     /* Put bus into clause 22 mode */
92     smi_clk.u = BDK_BAR_READ(device, BDK_SMI_X_CLK(bus_id));
93     if (smi_clk.s.mode != 0)
94     {
95         smi_clk.s.mode = 0;
96         BDK_BAR_WRITE(device, BDK_SMI_X_CLK(bus_id), smi_clk.u);
97     }
98 }
99 
100 /**
101  * @INTERNAL
102  * Function to read SMIX_RD_DAT and check for timeouts. This
103  * code sequence is done fairly often, so put in in one spot.
104  *
105  * @param bus_id SMI/MDIO bus to read
106  *
107  * @return Value of SMIX_RD_DAT. pending will be set on
108  *         a timeout.
109  */
__bdk_mdio_read_rd_dat(const bdk_device_t * device,int bus_id)110 static bdk_smi_x_rd_dat_t __bdk_mdio_read_rd_dat(const bdk_device_t *device, int bus_id)
111 {
112     bdk_smi_x_rd_dat_t smi_rd;
113     uint64_t done = bdk_clock_get_count(BDK_CLOCK_TIME) + (uint64_t)BDK_MDIO_TIMEOUT *
114                        bdk_clock_get_rate(bdk_numa_local(), BDK_CLOCK_TIME) / 1000000;
115     do
116     {
117         smi_rd.u = BDK_BAR_READ(device, BDK_SMI_X_RD_DAT(bus_id));
118     } while (smi_rd.s.pending && (bdk_clock_get_count(BDK_CLOCK_TIME) < done));
119     return smi_rd;
120 }
121 
122 
123 /**
124  * Perform an MII read. This function is used to read PHY
125  * registers controlling auto negotiation.
126  *
127  * @param bus_id   MDIO bus number. Zero on most chips, but some chips (ex CN56XX)
128  *                 support multiple busses.
129  * @param phy_id   The MII phy id
130  * @param location Register location to read
131  *
132  * @return Result from the read or -1 on failure
133  */
bdk_mdio_read(bdk_node_t node,int bus_id,int phy_id,int location)134 int bdk_mdio_read(bdk_node_t node, int bus_id, int phy_id, int location)
135 {
136     const bdk_device_t *device = bdk_device_lookup(node, MDIO_DEVID, 0);
137     if (!device)
138     {
139         bdk_error("MDIO: ECAM device not found\n");
140         return -1;
141     }
142     bdk_smi_x_cmd_t smi_cmd;
143     bdk_smi_x_rd_dat_t smi_rd;
144 
145     __bdk_mdio_set_clause22_mode(device, bus_id);
146 
147     smi_cmd.u = 0;
148     smi_cmd.s.phy_op = MDIO_CLAUSE_22_READ;
149     smi_cmd.s.phy_adr = phy_id;
150     smi_cmd.s.reg_adr = location;
151     BDK_BAR_WRITE(device, BDK_SMI_X_CMD(bus_id), smi_cmd.u);
152 
153     smi_rd = __bdk_mdio_read_rd_dat(device, bus_id);
154     if (smi_rd.s.val)
155         return smi_rd.s.dat;
156     else
157         return -1;
158 }
159 
160 
161 /**
162  * Perform an MII write. This function is used to write PHY
163  * registers controlling auto negotiation.
164  *
165  * @param bus_id   MDIO bus number. Zero on most chips, but some chips (ex CN56XX)
166  *                 support multiple busses.
167  * @param phy_id   The MII phy id
168  * @param location Register location to write
169  * @param val      Value to write
170  *
171  * @return -1 on error
172  *         0 on success
173  */
bdk_mdio_write(bdk_node_t node,int bus_id,int phy_id,int location,int val)174 int bdk_mdio_write(bdk_node_t node, int bus_id, int phy_id, int location, int val)
175 {
176     const bdk_device_t *device = bdk_device_lookup(node, MDIO_DEVID, 0);
177     if (!device)
178     {
179         bdk_error("MDIO: ECAM device not found\n");
180         return -1;
181     }
182     bdk_smi_x_cmd_t smi_cmd;
183     bdk_smi_x_wr_dat_t smi_wr;
184 
185     __bdk_mdio_set_clause22_mode(device, bus_id);
186 
187     smi_wr.u = 0;
188     smi_wr.s.dat = val;
189     BDK_BAR_WRITE(device, BDK_SMI_X_WR_DAT(bus_id), smi_wr.u);
190 
191     smi_cmd.u = 0;
192     smi_cmd.s.phy_op = MDIO_CLAUSE_22_WRITE;
193     smi_cmd.s.phy_adr = phy_id;
194     smi_cmd.s.reg_adr = location;
195     BDK_BAR_WRITE(device, BDK_SMI_X_CMD(bus_id), smi_cmd.u);
196 
197     if (BDK_BAR_WAIT_FOR_FIELD(device, BDK_SMI_X_WR_DAT(bus_id), pending, ==, 0, BDK_MDIO_TIMEOUT))
198         return -1;
199 
200     return 0;
201 }
202 
203 /**
204  * Perform an IEEE 802.3 clause 45 MII read. This function is used to read PHY
205  * registers controlling auto negotiation.
206  *
207  * @param bus_id   MDIO bus number. Zero on most chips, but some chips (ex CN56XX)
208  *                 support multiple busses.
209  * @param phy_id   The MII phy id
210  * @param device   MDIO Manageable Device (MMD) id
211  * @param location Register location to read
212  *
213  * @return Result from the read or -1 on failure
214  */
215 
bdk_mdio_45_read(bdk_node_t node,int bus_id,int phy_id,int device,int location)216 int bdk_mdio_45_read(bdk_node_t node, int bus_id, int phy_id, int device, int location)
217 {
218     const bdk_device_t *ecam_device = bdk_device_lookup(node, MDIO_DEVID, 0);
219     if (!ecam_device)
220     {
221         bdk_error("MDIO: ECAM device not found\n");
222         return -1;
223     }
224     bdk_smi_x_cmd_t smi_cmd;
225     bdk_smi_x_rd_dat_t smi_rd;
226     bdk_smi_x_wr_dat_t smi_wr;
227 
228     __bdk_mdio_set_clause45_mode(ecam_device, bus_id);
229 
230     smi_wr.u = 0;
231     smi_wr.s.dat = location;
232     BDK_BAR_WRITE(ecam_device, BDK_SMI_X_WR_DAT(bus_id), smi_wr.u);
233 
234     smi_cmd.u = 0;
235     smi_cmd.s.phy_op = MDIO_CLAUSE_45_ADDRESS;
236     smi_cmd.s.phy_adr = phy_id;
237     smi_cmd.s.reg_adr = device;
238     BDK_BAR_WRITE(ecam_device, BDK_SMI_X_CMD(bus_id), smi_cmd.u);
239 
240     if (BDK_BAR_WAIT_FOR_FIELD(ecam_device, BDK_SMI_X_WR_DAT(bus_id), pending, ==, 0, BDK_MDIO_TIMEOUT))
241     {
242         bdk_error("bdk_mdio_45_read: bus_id %d phy_id %2d device %2d register %2d   TIME OUT(address)\n", bus_id, phy_id, device, location);
243         return -1;
244     }
245 
246     smi_cmd.u = 0;
247     smi_cmd.s.phy_op = MDIO_CLAUSE_45_READ;
248     smi_cmd.s.phy_adr = phy_id;
249     smi_cmd.s.reg_adr = device;
250     BDK_BAR_WRITE(ecam_device, BDK_SMI_X_CMD(bus_id), smi_cmd.u);
251 
252     smi_rd = __bdk_mdio_read_rd_dat(ecam_device, bus_id);
253     if (smi_rd.s.pending)
254     {
255         bdk_error("bdk_mdio_45_read: bus_id %d phy_id %2d device %2d register %2d   TIME OUT(data)\n", bus_id, phy_id, device, location);
256         return -1;
257     }
258 
259     if (smi_rd.s.val)
260         return smi_rd.s.dat;
261     else
262     {
263         bdk_error("bdk_mdio_45_read: bus_id %d phy_id %2d device %2d register %2d   INVALID READ\n", bus_id, phy_id, device, location);
264         return -1;
265     }
266 }
267 
268 /**
269  * Perform an IEEE 802.3 clause 45 MII write. This function is used to write PHY
270  * registers controlling auto negotiation.
271  *
272  * @param bus_id   MDIO bus number. Zero on most chips, but some chips (ex CN56XX)
273  *                 support multiple busses.
274  * @param phy_id   The MII phy id
275  * @param device   MDIO Manageable Device (MMD) id
276  * @param location Register location to write
277  * @param val      Value to write
278  *
279  * @return -1 on error
280  *         0 on success
281  */
bdk_mdio_45_write(bdk_node_t node,int bus_id,int phy_id,int device,int location,int val)282 int bdk_mdio_45_write(bdk_node_t node, int bus_id, int phy_id, int device, int location,
283                                      int val)
284 {
285     const bdk_device_t *ecam_device = bdk_device_lookup(node, MDIO_DEVID, 0);
286     if (!ecam_device)
287     {
288         bdk_error("MDIO: ECAM device not found\n");
289         return -1;
290     }
291     bdk_smi_x_cmd_t smi_cmd;
292     bdk_smi_x_wr_dat_t smi_wr;
293 
294     __bdk_mdio_set_clause45_mode(ecam_device, bus_id);
295 
296     smi_wr.u = 0;
297     smi_wr.s.dat = location;
298     BDK_BAR_WRITE(ecam_device, BDK_SMI_X_WR_DAT(bus_id), smi_wr.u);
299 
300     smi_cmd.u = 0;
301     smi_cmd.s.phy_op = MDIO_CLAUSE_45_ADDRESS;
302     smi_cmd.s.phy_adr = phy_id;
303     smi_cmd.s.reg_adr = device;
304     BDK_BAR_WRITE(ecam_device, BDK_SMI_X_CMD(bus_id), smi_cmd.u);
305 
306     if (BDK_BAR_WAIT_FOR_FIELD(ecam_device, BDK_SMI_X_WR_DAT(bus_id), pending, ==, 0, BDK_MDIO_TIMEOUT))
307         return -1;
308 
309     smi_wr.u = 0;
310     smi_wr.s.dat = val;
311     BDK_BAR_WRITE(ecam_device, BDK_SMI_X_WR_DAT(bus_id), smi_wr.u);
312 
313     smi_cmd.u = 0;
314     smi_cmd.s.phy_op = MDIO_CLAUSE_45_WRITE;
315     smi_cmd.s.phy_adr = phy_id;
316     smi_cmd.s.reg_adr = device;
317     BDK_BAR_WRITE(ecam_device, BDK_SMI_X_CMD(bus_id), smi_cmd.u);
318 
319     if (BDK_BAR_WAIT_FOR_FIELD(ecam_device, BDK_SMI_X_WR_DAT(bus_id), pending, ==, 0, BDK_MDIO_TIMEOUT))
320         return -1;
321 
322     return 0;
323 }
324 
325 /**
326  * MDIO init() function
327  *
328  * @param device MDIO/SMI to initialize
329  *
330  * @return Zero on success, negative on failure
331  */
bdk_mdio_init(bdk_node_t node)332 int bdk_mdio_init(bdk_node_t node)
333 {
334     const bdk_device_t *device = bdk_device_lookup(node, MDIO_DEVID, 0);
335     if (!device)
336     {
337         bdk_error("MDIO: ECAM device not found\n");
338         return -1;
339     }
340     /* Change drive strength bits to fix issues when a QLM cable
341        is connected, creating a long spur path */
342     BDK_CSR_MODIFY(c, device->node, BDK_SMI_DRV_CTL,
343         c.s.pctl = 7; /* 30 ohm */
344         c.s.nctl = 7); /* 30 ohm */
345 
346     for (int i = 0; i < 2; i++)
347         BDK_BAR_MODIFY(c, device, BDK_SMI_X_EN(i), c.s.en = 1);
348 
349     return 0;
350 }
351 
352