Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 3 | * Copyright (C) 2020-2022 Marvell International Ltd. |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 4 | * |
| 5 | * FDT Helper functions similar to those provided to U-Boot. |
| 6 | */ |
| 7 | |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <i2c.h> |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 10 | #include <log.h> |
| 11 | #include <malloc.h> |
| 12 | #include <net.h> |
| 13 | #include <linux/delay.h> |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 14 | #include <asm-generic/gpio.h> |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 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> |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 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 | * |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 31 | * Return: pointer to allocated memory or NULL if out of memory. |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 32 | * Alignment is set to 8-bytes. |
| 33 | */ |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 34 | static void *cvmx_fdt_alloc(size_t size) |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 35 | { |
| 36 | return calloc(size, 1); |
| 37 | } |
| 38 | |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 39 | int cvmx_ofnode_lookup_phandles(ofnode node, const char *prop_name, int *lenp, |
| 40 | ofnode *nodes) |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 41 | { |
| 42 | const u32 *phandles; |
| 43 | int count; |
| 44 | int i; |
| 45 | |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 46 | phandles = ofnode_get_property(node, prop_name, &count); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 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++) |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 55 | nodes[i] = ofnode_get_by_phandle(fdt32_to_cpu(phandles[i])); |
| 56 | |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 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 | * |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 67 | * Return: CPU node number or error if negative |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 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 | /** |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 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 | * |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 100 | * Return: 0 for success, -1 on error |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 101 | */ |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 102 | static int cvmx_fdt_parse_vsc7224_channels(ofnode node, |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 103 | struct cvmx_vsc7224 *vsc7224) |
| 104 | { |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 105 | struct ofnode_phandle_args phandle; |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 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; |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 120 | ofnode node_chan; |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 121 | |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 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)); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 127 | break; |
| 128 | } |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 129 | |
| 130 | if (ofnode_to_offset(ofnode_get_parent(node_chan)) != |
| 131 | ofnode_to_offset(node)) |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 132 | break; |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 133 | |
| 134 | reg = ofnode_get_addr(node_chan); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 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 | } |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 141 | is_tx = ofnode_read_bool(node_chan, "direction-tx"); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 142 | |
| 143 | debug("%s(%s): Adding %cx channel %d\n", |
| 144 | __func__, vsc7224->name, is_tx ? 't' : 'r', |
| 145 | reg); |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 146 | tap_values = ofnode_get_property(node_chan, "taps", &len); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 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 | |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 170 | channel = cvmx_fdt_alloc(sizeof(*channel) + |
| 171 | num_taps * sizeof(struct cvmx_vsc7224_tap)); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 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; |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 180 | channel->of_offset = ofnode_to_offset(node_chan); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 181 | channel->is_tx = is_tx; |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 182 | channel->pretap_disable = ofnode_read_bool(node_chan, |
| 183 | "pretap-disable"); |
| 184 | channel->posttap_disable = ofnode_read_bool(node_chan, |
| 185 | "posttap-disable"); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 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"; |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 201 | if (ofnode_get_property(node_chan, mac_str, NULL)) { |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 202 | is_qsfp = false; |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 203 | } else if (ofnode_get_property(node_chan, "qsfp-mac", NULL)) { |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 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 | } |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 211 | |
| 212 | err = ofnode_parse_phandle_with_args(node_chan, mac_str, NULL, |
| 213 | 0, 0, &phandle); |
| 214 | if (err) { |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 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 | |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 220 | debug("%s: Found mac at %s\n", __func__, |
| 221 | ofnode_get_name(phandle.node)); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 222 | |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 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); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 229 | |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 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); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 236 | } |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 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 | } |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 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 | * |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 274 | * Return: 0 for success, error otherwise |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 275 | */ |
| 276 | int __cvmx_fdt_parse_vsc7224(const void *fdt_addr) |
| 277 | { |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 278 | struct cvmx_vsc7224 *vsc7224 = NULL; |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 279 | ofnode node; |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 280 | int err = 0; |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 281 | static bool parsed; |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 282 | const int *init_array; |
| 283 | struct udevice *dev; |
| 284 | u16 value; |
| 285 | int reg; |
| 286 | int len; |
| 287 | int ret; |
| 288 | int i; |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 289 | |
| 290 | debug("%s(%p)\n", __func__, fdt_addr); |
| 291 | |
| 292 | if (parsed) { |
| 293 | debug("%s: Already parsed\n", __func__); |
| 294 | return 0; |
| 295 | } |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 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)); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 301 | break; |
| 302 | } |
| 303 | |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 304 | vsc7224 = cvmx_fdt_alloc(sizeof(*vsc7224)); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 305 | if (!vsc7224) { |
| 306 | debug("%s: Out of memory!\n", __func__); |
| 307 | return -1; |
| 308 | } |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 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)); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 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 | } |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 323 | vsc7224->name = ofnode_get_name(node); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 324 | debug("%s: Adding %s\n", __func__, vsc7224->name); |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 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; |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 332 | } |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 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; |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 340 | } |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 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 | |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 389 | debug("%s: Parsing channels\n", __func__); |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 390 | err = cvmx_fdt_parse_vsc7224_channels(node, vsc7224); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 391 | if (err) { |
| 392 | debug("%s: Error parsing VSC7224 channels\n", __func__); |
| 393 | break; |
| 394 | } |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 395 | } |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 396 | |
| 397 | if (err) { |
| 398 | debug("%s(): Error\n", __func__); |
| 399 | if (vsc7224) { |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 400 | dm_gpio_free(vsc7224->reset_gpio.dev, |
| 401 | &vsc7224->reset_gpio); |
| 402 | dm_gpio_free(vsc7224->los_gpio.dev, |
| 403 | &vsc7224->los_gpio); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 404 | if (vsc7224->i2c_bus) |
| 405 | cvmx_fdt_free_i2c_bus(vsc7224->i2c_bus); |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 406 | free(vsc7224); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | if (!err) |
| 410 | parsed = true; |
| 411 | |
| 412 | return err; |
| 413 | } |
| 414 | |
| 415 | /** |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 416 | * Given the parent offset of an i2c device build up a list describing the bus |
| 417 | * which can contain i2c muxes and switches. |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 418 | * |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 419 | * @param[in] node ofnode of the parent node of a GPIO device in |
| 420 | * the device tree. |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 421 | * |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 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() |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 427 | */ |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 428 | struct cvmx_fdt_i2c_bus_info *cvmx_ofnode_get_i2c_bus(ofnode node) |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 429 | { |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 430 | struct cvmx_fdt_i2c_bus_info *businfo = NULL; |
| 431 | struct udevice *bus; |
| 432 | int ret; |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 433 | |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 434 | businfo = cvmx_fdt_alloc(sizeof(*businfo)); |
| 435 | if (!businfo) { |
| 436 | debug("Out of memory\n"); |
| 437 | return NULL; |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 438 | } |
| 439 | |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 440 | debug("%s: Found node %s\n", __func__, ofnode_get_name(node)); |
| 441 | businfo->of_offset = ofnode_to_offset(node); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 442 | |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 443 | /* |
| 444 | * Get I2C bus and probe it automatically - needed for later use |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 445 | */ |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 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; |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /** |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 458 | * Return the Octeon bus number for a bus descriptor |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 459 | * |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 460 | * @param[in] bus bus descriptor |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 461 | * |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 462 | * @return Octeon twsi bus number or -1 on error |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 463 | */ |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 464 | int cvmx_fdt_i2c_get_root_bus(const struct cvmx_fdt_i2c_bus_info *bus) |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 465 | { |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 466 | if (bus->type != CVMX_I2C_BUS_OCTEON) |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 467 | return -1; |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 468 | return bus->channel; |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /** |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 472 | * Frees all entries for an i2c bus descriptor |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 473 | * |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 474 | * @param bus bus to free |
| 475 | * |
| 476 | * @return 0 |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 477 | */ |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 478 | int cvmx_fdt_free_i2c_bus(struct cvmx_fdt_i2c_bus_info *bus) |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 479 | { |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 480 | struct cvmx_fdt_i2c_bus_info *last; |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 481 | |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 482 | while (bus) { |
| 483 | last = bus; |
| 484 | bus = bus->child; |
| 485 | free(last); |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 486 | } |
Stefan Roese | 1fb7e27 | 2022-04-07 09:11:44 +0200 | [diff] [blame] | 487 | return 0; |
Aaron Williams | 251d65d | 2020-12-11 17:05:59 +0100 | [diff] [blame] | 488 | } |