1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020-2022 Marvell International Ltd.
5 * FDT Helper functions similar to those provided to U-Boot.
13 #include <linux/delay.h>
14 #include <asm-generic/gpio.h>
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>
27 * Local allocator to handle both SE and U-Boot that also zeroes out memory
29 * @param size number of bytes to allocate
31 * Return: pointer to allocated memory or NULL if out of memory.
32 * Alignment is set to 8-bytes.
34 static void *cvmx_fdt_alloc(size_t size)
36 return calloc(size, 1);
39 int cvmx_ofnode_lookup_phandles(ofnode node, const char *prop_name, int *lenp,
46 phandles = ofnode_get_property(node, prop_name, &count);
47 if (!phandles || count < 0)
48 return -FDT_ERR_NOTFOUND;
54 for (i = 0; i < count; i++)
55 nodes[i] = ofnode_get_by_phandle(fdt32_to_cpu(phandles[i]));
62 * Given a FDT node return the CPU node number
64 * @param[in] fdt_addr Address of FDT
65 * @param node FDT node number
67 * Return: CPU node number or error if negative
69 int cvmx_fdt_get_cpu_node(const void *fdt_addr, int node)
75 while (fdt_node_check_compatible(fdt_addr, parent, "simple-bus") != 0) {
76 parent = fdt_parent_offset(fdt_addr, parent);
80 ranges = fdt_getprop(fdt_addr, parent, "ranges", &len);
88 return -FDT_ERR_TRUNCATED;
90 return fdt32_to_cpu(ranges[2]) / 0x10;
94 * Parses all of the channels assigned to a VSC7224 device
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
100 * Return: 0 for success, -1 on error
102 static int cvmx_fdt_parse_vsc7224_channels(ofnode node,
103 struct cvmx_vsc7224 *vsc7224)
105 struct ofnode_phandle_args phandle;
109 struct cvmx_vsc7224_chan *channel;
110 struct cvmx_fdt_sfp_info *sfp_info;
114 const u32 *tap_values;
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));
130 if (ofnode_to_offset(ofnode_get_parent(node_chan)) !=
131 ofnode_to_offset(node))
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",
141 is_tx = ofnode_read_bool(node_chan, "direction-tx");
143 debug("%s(%s): Adding %cx channel %d\n",
144 __func__, vsc7224->name, is_tx ? 't' : 'r',
146 tap_values = ofnode_get_property(node_chan, "taps", &len);
148 debug("%s: Error: no taps defined for vsc7224 channel %d\n",
154 if (vsc7224->channel[reg]) {
155 debug("%s: Error: channel %d already assigned at %p\n",
157 vsc7224->channel[reg]);
162 debug("%s: Error: tap format error for channel %d\n",
168 debug("%s: Adding %d taps\n", __func__, num_taps);
170 channel = cvmx_fdt_alloc(sizeof(*channel) +
171 num_taps * sizeof(struct cvmx_vsc7224_tap));
173 debug("%s: Out of memory\n", __func__);
177 vsc7224->channel[reg] = channel;
178 channel->num_taps = num_taps;
180 channel->of_offset = ofnode_to_offset(node_chan);
181 channel->is_tx = is_tx;
182 channel->pretap_disable = ofnode_read_bool(node_chan,
184 channel->posttap_disable = ofnode_read_bool(node_chan,
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);
197 /* Now find out which interface it's mapped to */
198 channel->ipd_port = -1;
201 if (ofnode_get_property(node_chan, mac_str, NULL)) {
203 } else if (ofnode_get_property(node_chan, "qsfp-mac", NULL)) {
205 mac_str = "qsfp-mac";
207 debug("%s: Error: MAC not found for %s channel %d\n", __func__,
212 err = ofnode_parse_phandle_with_args(node_chan, mac_str, NULL,
215 debug("%s: Error %d with MAC %s phandle for %s\n", __func__, of_mac,
216 mac_str, vsc7224->name);
220 debug("%s: Found mac at %s\n", __func__,
221 ofnode_get_name(phandle.node));
223 xiface = (ofnode_get_addr(ofnode_get_parent(phandle.node))
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);
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);
237 sfp_info = cvmx_helper_cfg_get_sfp_info(xiface, index);
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);
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(
257 &cvmx_sfp_vsc7224_mod_abs_changed,
270 * Parses all instances of the Vitesse VSC7224 reclocking chip
272 * @param[in] fdt_addr Address of flat device tree
274 * Return: 0 for success, error otherwise
276 int __cvmx_fdt_parse_vsc7224(const void *fdt_addr)
278 struct cvmx_vsc7224 *vsc7224 = NULL;
282 const int *init_array;
290 debug("%s(%p)\n", __func__, fdt_addr);
293 debug("%s: Already parsed\n", __func__);
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));
304 vsc7224 = cvmx_fdt_alloc(sizeof(*vsc7224));
306 debug("%s: Out of memory!\n", __func__);
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__);
318 if (!vsc7224->i2c_bus) {
319 debug("%s: Error getting i2c bus\n", __func__);
323 vsc7224->name = ofnode_get_name(node);
324 debug("%s: Adding %s\n", __func__, vsc7224->name);
326 err = gpio_request_by_name_nodev(node, "reset", 0,
327 &vsc7224->reset_gpio,
330 printf("%s: reset GPIO not found in DT!\n", __func__);
334 err = gpio_request_by_name_nodev(node, "los", 0,
338 printf("%s: los GPIO not found in DT!\n", __func__);
343 * This code was taken from the NIC23 board specific code
344 * but should be better placed here in the common code
346 debug("%s: Putting device in reset\n", __func__);
347 dm_gpio_set_value(&vsc7224->reset_gpio, 1);
349 debug("%s: Taking device out of reset\n", __func__);
350 dm_gpio_set_value(&vsc7224->reset_gpio, 0);
353 init_array = ofnode_get_property(node, "vitesse,reg-init",
356 debug("%s: No initialization array\n", __func__);
359 if ((len % 8) != 0) {
360 printf("%s: Error: register init string should be an array of reg number followed by value\n",
365 ret = i2c_get_chip(vsc7224->i2c_bus->i2c_bus,
366 vsc7224->i2c_addr, 1, &dev);
368 debug("Cannot find I2C device: %d\n", ret);
372 for (i = 0; i < len / sizeof(int); i += 2) {
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);
381 debug("Cannot write I2C device: %d\n", ret);
385 debug(" Wrote 0x%02x <= 0x%02x%02x\n", reg,
386 buffer[0], buffer[1]);
389 debug("%s: Parsing channels\n", __func__);
390 err = cvmx_fdt_parse_vsc7224_channels(node, vsc7224);
392 debug("%s: Error parsing VSC7224 channels\n", __func__);
398 debug("%s(): Error\n", __func__);
400 dm_gpio_free(vsc7224->reset_gpio.dev,
401 &vsc7224->reset_gpio);
402 dm_gpio_free(vsc7224->los_gpio.dev,
404 if (vsc7224->i2c_bus)
405 cvmx_fdt_free_i2c_bus(vsc7224->i2c_bus);
416 * Given the parent offset of an i2c device build up a list describing the bus
417 * which can contain i2c muxes and switches.
419 * @param[in] node ofnode of the parent node of a GPIO device in
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.
426 * @see cvmx_fdt_free_i2c_bus()
428 struct cvmx_fdt_i2c_bus_info *cvmx_ofnode_get_i2c_bus(ofnode node)
430 struct cvmx_fdt_i2c_bus_info *businfo = NULL;
434 businfo = cvmx_fdt_alloc(sizeof(*businfo));
436 debug("Out of memory\n");
440 debug("%s: Found node %s\n", __func__, ofnode_get_name(node));
441 businfo->of_offset = ofnode_to_offset(node);
444 * Get I2C bus and probe it automatically - needed for later use
446 ret = device_get_global_by_ofnode(node, &bus);
448 printf("Cannot find a I2C bus\n");
452 businfo->i2c_bus = bus;
458 * Return the Octeon bus number for a bus descriptor
460 * @param[in] bus bus descriptor
462 * @return Octeon twsi bus number or -1 on error
464 int cvmx_fdt_i2c_get_root_bus(const struct cvmx_fdt_i2c_bus_info *bus)
466 if (bus->type != CVMX_I2C_BUS_OCTEON)
472 * Frees all entries for an i2c bus descriptor
474 * @param bus bus to free
478 int cvmx_fdt_free_i2c_bus(struct cvmx_fdt_i2c_bus_info *bus)
480 struct cvmx_fdt_i2c_bus_info *last;