Prepare v2023.10
[platform/kernel/u-boot.git] / arch / mips / mach-octeon / cvmx-helper-fdt.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020-2022 Marvell International Ltd.
4  *
5  * FDT Helper functions similar to those provided to U-Boot.
6  */
7
8 #include <dm.h>
9 #include <i2c.h>
10 #include <log.h>
11 #include <malloc.h>
12 #include <net.h>
13 #include <linux/delay.h>
14 #include <asm-generic/gpio.h>
15
16 #include <mach/cvmx-regs.h>
17 #include <mach/cvmx-csr.h>
18 #include <mach/cvmx-bootmem.h>
19 #include <mach/octeon-model.h>
20 #include <mach/octeon_fdt.h>
21 #include <mach/cvmx-helper.h>
22 #include <mach/cvmx-helper-board.h>
23 #include <mach/cvmx-helper-cfg.h>
24 #include <mach/cvmx-helper-fdt.h>
25
26 /**
27  * Local allocator to handle both SE and U-Boot that also zeroes out memory
28  *
29  * @param       size    number of bytes to allocate
30  *
31  * Return:      pointer to allocated memory or NULL if out of memory.
32  *              Alignment is set to 8-bytes.
33  */
34 static void *cvmx_fdt_alloc(size_t size)
35 {
36         return calloc(size, 1);
37 }
38
39 int cvmx_ofnode_lookup_phandles(ofnode node, const char *prop_name, int *lenp,
40                                 ofnode *nodes)
41 {
42         const u32 *phandles;
43         int count;
44         int i;
45
46         phandles = ofnode_get_property(node, prop_name, &count);
47         if (!phandles || count < 0)
48                 return -FDT_ERR_NOTFOUND;
49
50         count /= 4;
51         if (count > *lenp)
52                 count = *lenp;
53
54         for (i = 0; i < count; i++)
55                 nodes[i] = ofnode_get_by_phandle(fdt32_to_cpu(phandles[i]));
56
57         *lenp = count;
58         return 0;
59 }
60
61 /**
62  * Given a FDT node return the CPU node number
63  *
64  * @param[in]   fdt_addr        Address of FDT
65  * @param       node            FDT node number
66  *
67  * Return:      CPU node number or error if negative
68  */
69 int cvmx_fdt_get_cpu_node(const void *fdt_addr, int node)
70 {
71         int parent = node;
72         const u32 *ranges;
73         int len = 0;
74
75         while (fdt_node_check_compatible(fdt_addr, parent, "simple-bus") != 0) {
76                 parent = fdt_parent_offset(fdt_addr, parent);
77                 if (parent < 0)
78                         return parent;
79         }
80         ranges = fdt_getprop(fdt_addr, parent, "ranges", &len);
81         if (!ranges)
82                 return len;
83
84         if (len == 0)
85                 return 0;
86
87         if (len < 24)
88                 return -FDT_ERR_TRUNCATED;
89
90         return fdt32_to_cpu(ranges[2]) / 0x10;
91 }
92
93 /**
94  * Parses all of the channels assigned to a VSC7224 device
95  *
96  * @param[in]           fdt_addr        Address of flat device tree
97  * @param               of_offset       Offset of vsc7224 node
98  * @param[in,out]       vsc7224         Data structure to hold the data
99  *
100  * Return:      0 for success, -1 on error
101  */
102 static int cvmx_fdt_parse_vsc7224_channels(ofnode node,
103                                            struct cvmx_vsc7224 *vsc7224)
104 {
105         struct ofnode_phandle_args phandle;
106         int err = 0;
107         int reg;
108         int num_chan = 0;
109         struct cvmx_vsc7224_chan *channel;
110         struct cvmx_fdt_sfp_info *sfp_info;
111         int len;
112         int num_taps;
113         int i;
114         const u32 *tap_values;
115         int of_mac;
116         int xiface, index;
117         bool is_tx;
118         bool is_qsfp;
119         const char *mac_str;
120         ofnode node_chan;
121
122         debug("%s(%x, %s)\n", __func__, ofnode_to_offset(node), vsc7224->name);
123         ofnode_for_each_compatible_node(node_chan, "vitesse,vsc7224-channel") {
124                 if (!ofnode_valid(node_chan)) {
125                         debug("%s: Error parsing FDT node %s\n",
126                               __func__, ofnode_get_name(node));
127                         break;
128                 }
129
130                 if (ofnode_to_offset(ofnode_get_parent(node_chan)) !=
131                     ofnode_to_offset(node))
132                         break;
133
134                 reg = ofnode_get_addr(node_chan);
135                 if (reg < 0 || reg > 3) {
136                         debug("%s: channel reg is either not present or out of range\n",
137                               __func__);
138                         err = -1;
139                         break;
140                 }
141                 is_tx = ofnode_read_bool(node_chan, "direction-tx");
142
143                 debug("%s(%s): Adding %cx channel %d\n",
144                       __func__, vsc7224->name, is_tx ? 't' : 'r',
145                       reg);
146                 tap_values = ofnode_get_property(node_chan, "taps", &len);
147                 if (!tap_values) {
148                         debug("%s: Error: no taps defined for vsc7224 channel %d\n",
149                               __func__, reg);
150                         err = -1;
151                         break;
152                 }
153
154                 if (vsc7224->channel[reg]) {
155                         debug("%s: Error: channel %d already assigned at %p\n",
156                               __func__, reg,
157                               vsc7224->channel[reg]);
158                         err = -1;
159                         break;
160                 }
161                 if (len % 16) {
162                         debug("%s: Error: tap format error for channel %d\n",
163                               __func__, reg);
164                         err = -1;
165                         break;
166                 }
167                 num_taps = len / 16;
168                 debug("%s: Adding %d taps\n", __func__, num_taps);
169
170                 channel = cvmx_fdt_alloc(sizeof(*channel) +
171                                          num_taps * sizeof(struct cvmx_vsc7224_tap));
172                 if (!channel) {
173                         debug("%s: Out of memory\n", __func__);
174                         err = -1;
175                         break;
176                 }
177                 vsc7224->channel[reg] = channel;
178                 channel->num_taps = num_taps;
179                 channel->lane = reg;
180                 channel->of_offset = ofnode_to_offset(node_chan);
181                 channel->is_tx = is_tx;
182                 channel->pretap_disable = ofnode_read_bool(node_chan,
183                                                            "pretap-disable");
184                 channel->posttap_disable = ofnode_read_bool(node_chan,
185                                                             "posttap-disable");
186                 channel->vsc7224 = vsc7224;
187                 /* Read all the tap values */
188                 for (i = 0; i < num_taps; i++) {
189                         channel->taps[i].len = fdt32_to_cpu(tap_values[i * 4 + 0]);
190                         channel->taps[i].main_tap = fdt32_to_cpu(tap_values[i * 4 + 1]);
191                         channel->taps[i].pre_tap = fdt32_to_cpu(tap_values[i * 4 + 2]);
192                         channel->taps[i].post_tap = fdt32_to_cpu(tap_values[i * 4 + 3]);
193                         debug("%s: tap %d: len: %d, main_tap: 0x%x, pre_tap: 0x%x, post_tap: 0x%x\n",
194                               __func__, i, channel->taps[i].len, channel->taps[i].main_tap,
195                               channel->taps[i].pre_tap, channel->taps[i].post_tap);
196                 }
197                 /* Now find out which interface it's mapped to */
198                 channel->ipd_port = -1;
199
200                 mac_str = "sfp-mac";
201                 if (ofnode_get_property(node_chan, mac_str, NULL)) {
202                         is_qsfp = false;
203                 } else if (ofnode_get_property(node_chan, "qsfp-mac", NULL)) {
204                         is_qsfp = true;
205                         mac_str = "qsfp-mac";
206                 } else {
207                         debug("%s: Error: MAC not found for %s channel %d\n", __func__,
208                               vsc7224->name, reg);
209                         return -1;
210                 }
211
212                 err = ofnode_parse_phandle_with_args(node_chan, mac_str, NULL,
213                                                      0, 0, &phandle);
214                 if (err) {
215                         debug("%s: Error %d with MAC %s phandle for %s\n", __func__, of_mac,
216                               mac_str, vsc7224->name);
217                         return -1;
218                 }
219
220                 debug("%s: Found mac at %s\n", __func__,
221                       ofnode_get_name(phandle.node));
222
223                 xiface = (ofnode_get_addr(ofnode_get_parent(phandle.node))
224                           >> 24) & 0x0f;
225                 index = ofnode_get_addr(phandle.node);
226                 channel->xiface = xiface;
227                 channel->index = index;
228                 channel->ipd_port = cvmx_helper_get_ipd_port(xiface, index);
229
230                 debug("%s: Found MAC, xiface: 0x%x, index: %d, ipd port: %d\n", __func__,
231                       xiface, index, channel->ipd_port);
232                 if (channel->ipd_port >= 0) {
233                         cvmx_helper_cfg_set_vsc7224_chan_info(xiface, index, channel);
234                         debug("%s: Storing config channel for xiface 0x%x, index %d\n",
235                               __func__, xiface, index);
236                 }
237                 sfp_info = cvmx_helper_cfg_get_sfp_info(xiface, index);
238                 if (!sfp_info) {
239                         debug("%s: Warning: no (Q)SFP+ slot found for xinterface 0x%x, index %d for channel %d\n",
240                               __func__, xiface, index, channel->lane);
241                         continue;
242                 }
243
244                 /* Link it */
245                 channel->next = sfp_info->vsc7224_chan;
246                 if (sfp_info->vsc7224_chan)
247                         sfp_info->vsc7224_chan->prev = channel;
248                 sfp_info->vsc7224_chan = channel;
249                 sfp_info->is_vsc7224 = true;
250                 debug("%s: Registering VSC7224 %s channel %d with SFP %s\n", __func__,
251                       vsc7224->name, channel->lane, sfp_info->name);
252                 if (!sfp_info->mod_abs_changed) {
253                         debug("%s: Registering cvmx_sfp_vsc7224_mod_abs_changed at %p for xinterface 0x%x, index %d\n",
254                               __func__, &cvmx_sfp_vsc7224_mod_abs_changed, xiface, index);
255                         cvmx_sfp_register_mod_abs_changed(
256                                 sfp_info,
257                                 &cvmx_sfp_vsc7224_mod_abs_changed,
258                                 NULL);
259                 }
260
261                 if (num_chan >= 4)
262                         break;
263         }
264
265         return err;
266 }
267
268 /**
269  * @INTERNAL
270  * Parses all instances of the Vitesse VSC7224 reclocking chip
271  *
272  * @param[in]   fdt_addr        Address of flat device tree
273  *
274  * Return:      0 for success, error otherwise
275  */
276 int __cvmx_fdt_parse_vsc7224(const void *fdt_addr)
277 {
278         struct cvmx_vsc7224 *vsc7224 = NULL;
279         ofnode node;
280         int err = 0;
281         static bool parsed;
282         const int *init_array;
283         struct udevice *dev;
284         u16 value;
285         int reg;
286         int len;
287         int ret;
288         int i;
289
290         debug("%s(%p)\n", __func__, fdt_addr);
291
292         if (parsed) {
293                 debug("%s: Already parsed\n", __func__);
294                 return 0;
295         }
296
297         ofnode_for_each_compatible_node(node, "vitesse,vsc7224") {
298                 if (!ofnode_valid(node)) {
299                         debug("%s: Error parsing FDT node %s\n",
300                               __func__, ofnode_get_name(node));
301                         break;
302                 }
303
304                 vsc7224 = cvmx_fdt_alloc(sizeof(*vsc7224));
305                 if (!vsc7224) {
306                         debug("%s: Out of memory!\n", __func__);
307                         return -1;
308                 }
309
310                 vsc7224->of_offset = ofnode_to_offset(node);
311                 vsc7224->i2c_addr = ofnode_get_addr(node);
312                 vsc7224->i2c_bus = cvmx_ofnode_get_i2c_bus(ofnode_get_parent(node));
313                 if (vsc7224->i2c_addr < 0) {
314                         debug("%s: Error: reg field missing\n", __func__);
315                         err = -1;
316                         break;
317                 }
318                 if (!vsc7224->i2c_bus) {
319                         debug("%s: Error getting i2c bus\n", __func__);
320                         err = -1;
321                         break;
322                 }
323                 vsc7224->name = ofnode_get_name(node);
324                 debug("%s: Adding %s\n", __func__, vsc7224->name);
325
326                 err = gpio_request_by_name_nodev(node, "reset", 0,
327                                                  &vsc7224->reset_gpio,
328                                                  GPIOD_IS_OUT);
329                 if (err) {
330                         printf("%s: reset GPIO not found in DT!\n", __func__);
331                         return -ENODEV;
332                 }
333
334                 err = gpio_request_by_name_nodev(node, "los", 0,
335                                                  &vsc7224->los_gpio,
336                                                  GPIOD_IS_IN);
337                 if (err) {
338                         printf("%s: los GPIO not found in DT!\n", __func__);
339                         return -ENODEV;
340                 }
341
342                 /*
343                  * This code was taken from the NIC23 board specific code
344                  * but should be better placed here in the common code
345                  */
346                 debug("%s: Putting device in reset\n", __func__);
347                 dm_gpio_set_value(&vsc7224->reset_gpio, 1);
348                 mdelay(10);
349                 debug("%s: Taking device out of reset\n", __func__);
350                 dm_gpio_set_value(&vsc7224->reset_gpio, 0);
351                 mdelay(50);
352
353                 init_array = ofnode_get_property(node, "vitesse,reg-init",
354                                                  &len);
355                 if (!init_array) {
356                         debug("%s: No initialization array\n", __func__);
357                         continue;
358                 }
359                 if ((len % 8) != 0) {
360                         printf("%s: Error: register init string should be an array of reg number followed by value\n",
361                                __func__);
362                         return -1;
363                 }
364
365                 ret = i2c_get_chip(vsc7224->i2c_bus->i2c_bus,
366                                    vsc7224->i2c_addr, 1, &dev);
367                 if (ret) {
368                         debug("Cannot find I2C device: %d\n", ret);
369                         return -1;
370                 }
371
372                 for (i = 0; i < len / sizeof(int); i += 2) {
373                         u8 buffer[2];
374
375                         reg = fdt32_to_cpu(init_array[i]);
376                         value = fdt32_to_cpu(init_array[i + 1]);
377                         buffer[0] = value >> 8;
378                         buffer[1] = value & 0xff;
379                         ret = dm_i2c_write(dev, reg, buffer, 2);
380                         if (ret) {
381                                 debug("Cannot write I2C device: %d\n", ret);
382                                 return -1;
383                         }
384
385                         debug("  Wrote 0x%02x <= 0x%02x%02x\n", reg,
386                               buffer[0], buffer[1]);
387                 }
388
389                 debug("%s: Parsing channels\n", __func__);
390                 err = cvmx_fdt_parse_vsc7224_channels(node, vsc7224);
391                 if (err) {
392                         debug("%s: Error parsing VSC7224 channels\n", __func__);
393                         break;
394                 }
395         }
396
397         if (err) {
398                 debug("%s(): Error\n", __func__);
399                 if (vsc7224) {
400                         dm_gpio_free(vsc7224->reset_gpio.dev,
401                                      &vsc7224->reset_gpio);
402                         dm_gpio_free(vsc7224->los_gpio.dev,
403                                      &vsc7224->los_gpio);
404                         if (vsc7224->i2c_bus)
405                                 cvmx_fdt_free_i2c_bus(vsc7224->i2c_bus);
406                         free(vsc7224);
407                 }
408         }
409         if (!err)
410                 parsed = true;
411
412         return err;
413 }
414
415 /**
416  * Given the parent offset of an i2c device build up a list describing the bus
417  * which can contain i2c muxes and switches.
418  *
419  * @param[in]   node            ofnode of the parent node of a GPIO device in
420  *                              the device tree.
421  *
422  * @return      pointer to list of i2c devices starting from the root which
423  *              can include i2c muxes and switches or NULL if error.  Note that
424  *              all entries are allocated on the heap.
425  *
426  * @see cvmx_fdt_free_i2c_bus()
427  */
428 struct cvmx_fdt_i2c_bus_info *cvmx_ofnode_get_i2c_bus(ofnode node)
429 {
430         struct cvmx_fdt_i2c_bus_info *businfo = NULL;
431         struct udevice *bus;
432         int ret;
433
434         businfo = cvmx_fdt_alloc(sizeof(*businfo));
435         if (!businfo) {
436                 debug("Out of memory\n");
437                 return NULL;
438         }
439
440         debug("%s: Found node %s\n", __func__, ofnode_get_name(node));
441         businfo->of_offset = ofnode_to_offset(node);
442
443         /*
444          * Get I2C bus and probe it automatically - needed for later use
445          */
446         ret = device_get_global_by_ofnode(node, &bus);
447         if (!bus || ret) {
448                 printf("Cannot find a I2C bus\n");
449                 return NULL;
450         }
451
452         businfo->i2c_bus = bus;
453
454         return businfo;
455 }
456
457 /**
458  * Return the Octeon bus number for a bus descriptor
459  *
460  * @param[in]   bus     bus descriptor
461  *
462  * @return      Octeon twsi bus number or -1 on error
463  */
464 int cvmx_fdt_i2c_get_root_bus(const struct cvmx_fdt_i2c_bus_info *bus)
465 {
466         if (bus->type != CVMX_I2C_BUS_OCTEON)
467                 return -1;
468         return bus->channel;
469 }
470
471 /**
472  * Frees all entries for an i2c bus descriptor
473  *
474  * @param       bus     bus to free
475  *
476  * @return      0
477  */
478 int cvmx_fdt_free_i2c_bus(struct cvmx_fdt_i2c_bus_info *bus)
479 {
480         struct cvmx_fdt_i2c_bus_info *last;
481
482         while (bus) {
483                 last = bus;
484                 bus = bus->child;
485                 free(last);
486         }
487         return 0;
488 }