blob: 0ccfe149dccaf76f6067067f3ee5e352a95eeac5 [file] [log] [blame]
Aaron Williams810d1682020-12-11 17:06:05 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2020 Marvell International Ltd.
4 */
5
6#include <env.h>
7#include <log.h>
8#include <i2c.h>
9#include <net.h>
10#include <dm/device.h>
11#include <linux/delay.h>
12
13#include <mach/cvmx-regs.h>
14#include <mach/cvmx-csr.h>
15#include <mach/cvmx-bootmem.h>
16#include <mach/octeon-model.h>
17#include <mach/octeon_eth.h>
18#include <mach/octeon_fdt.h>
19#include <mach/cvmx-helper-fdt.h>
20#include <mach/cvmx-helper-gpio.h>
21#include <mach/cvmx-fuse.h>
22#include <mach/octeon-feature.h>
23#include <mach/cvmx-qlm.h>
24#include <mach/octeon_qlm.h>
25#include <asm/gpio.h>
26
27#ifdef CONFIG_PCA953X
28#include <pca953x.h>
29#endif
30#ifdef CONFIG_PCF857X
31#include <pcf857x.h>
32#endif
33#ifdef CONFIG_PCA9698
34#include <pca9698.h>
35#endif
36#ifdef CONFIG_PCA9554
37#include <pca9554.h>
38#endif
39#ifdef CONFIG_PCA9555
40#include <pca9555.h>
41#endif
42
43DECLARE_GLOBAL_DATA_PTR;
44
45#ifdef CONFIG_PCA9554
46static const char * const pca9554_gpio_list[] = {
47 "pca9554",
48 "nxp,pca9554",
49 "ti,pca9554",
50 NULL,
51};
52#endif
53
54#ifdef CONFIG_PCA9555
55static const char * const pca9555_gpio_list[] = {
56 "pca9535", "nxp,pca9535", "pca9539", "nxp,pca9539", "pca9555",
57 "nxp,pca9555", "ti,pca9555", "max7312", "maxim,max7312", "max7313",
58 "maxim,max7313", "tca6416", "tca9539", NULL,
59};
60#endif
61
62#ifdef CONFIG_PCA9698
63/** List of compatible strings supported by pca9698 driver */
64static const char * const pca9698_gpio_list[] = {
65 "nxp,pca9505", "pca9505", "nxp,pca9698", "pca9698", NULL,
66};
67#endif
68
69#ifdef CONFIG_PCA953X
70/** List of compatible strings supported by pca953x driver */
71static const char * const pca953x_gpio_list[] = {
72 "nxp,pca9534", "nxp,pca9535", "nxp,pca9536", "nxp,pca9537", "nxp,pca9538", "nxp,pca9539",
73 "nxp,pca953x", "nxp,pca9554", "nxp,pca9555", "nxp,pca9556", "nxp,pca9557", "nxp,pca6107",
74 "pca9534", "pca9535", "pca9536", "pca9537", "pca9538", "pca9539",
75 "pca953x", "pca9554", "pca9555", "pca9556", "pca9557", "max7310",
76 "max7312", "max7313", "max7315", "pca6107", "tca6408", "tca6416",
77 "tca9555", NULL
78};
79#endif
80
81#ifdef CONFIG_PHY_VITESSE
82static const char * const vitesse_vsc8488_gpio_list[] = {
83 "vitesse,vsc8486", "microsemi,vsc8486", "vitesse,vsc8488",
84 "microsemi,vsc8488", "vitesse,vsc8489", "microsemi,vsc8489",
85 "vitesse,vsc8490", "microsemi,vsc8490", NULL
86};
87#endif
88
89/** List of compatible strings supported by Octeon driver */
90static const char * const octeon_gpio_list[] = {
91 "cavium,octeon-7890-gpio",
92 "cavium,octeon-3860-gpio",
93 NULL
94};
95
96/**
97 * Trims nodes from the flat device tree.
98 *
99 * @param fdt - pointer to working FDT, usually in gd->fdt_blob
100 * @param fdt_key - key to preserve. All non-matching keys are removed
101 * @param trim_name - name of property to look for. If NULL use
102 * 'cavium,qlm-trim'
103 *
104 * The key should look something like device #, type where device # is a
105 * number from 0-9 and type is a string describing the type. For QLM
106 * operations this would typically contain the QLM number followed by
107 * the type in the device tree, like "0,xaui", "0,sgmii", etc. This function
108 * will trim all items in the device tree which match the device number but
109 * have a type which does not match. For example, if a QLM has a xaui module
110 * installed on QLM 0 and "0,xaui" is passed as a key, then all FDT nodes that
111 * have "0,xaui" will be preserved but all others, i.e. "0,sgmii" will be
112 * removed.
113 *
114 * Note that the trim_name must also match. If trim_name is NULL then it
115 * looks for the property "cavium,qlm-trim".
116 *
117 * Also, when the trim_name is "cavium,qlm-trim" or NULL that the interfaces
118 * will also be renamed based on their register values.
119 *
120 * For example, if a PIP interface is named "interface@W" and has the property
121 * reg = <0> then the interface will be renamed after this function to
122 * interface@0.
123 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100124 * Return: 0 for success.
Aaron Williams810d1682020-12-11 17:06:05 +0100125 */
126int __octeon_fdt_patch(void *fdt, const char *fdt_key, const char *trim_name)
127{
128 bool rename = !trim_name || !strcmp(trim_name, "cavium,qlm-trim");
129
130 return octeon_fdt_patch_rename(fdt, fdt_key, trim_name, rename, NULL, NULL);
131}
132
133int octeon_fdt_patch(void *fdt, const char *fdt_key, const char *trim_name)
134 __attribute__((weak, alias("__octeon_fdt_patch")));
135
136/**
137 * Trims nodes from the flat device tree.
138 *
139 * @param fdt - pointer to working FDT, usually in gd->fdt_blob
140 * @param fdt_key - key to preserve. All non-matching keys are removed
141 * @param trim_name - name of property to look for. If NULL use
142 * 'cavium,qlm-trim'
143 * @param rename - set to TRUE to rename interfaces.
144 * @param callback - function to call on matched nodes.
145 * @param cbarg - passed to callback.
146 *
147 * The key should look something like device #, type where device # is a
148 * number from 0-9 and type is a string describing the type. For QLM
149 * operations this would typically contain the QLM number followed by
150 * the type in the device tree, like "0,xaui", "0,sgmii", etc. This function
151 * will trim all items in the device tree which match the device number but
152 * have a type which does not match. For example, if a QLM has a xaui module
153 * installed on QLM 0 and "0,xaui" is passed as a key, then all FDT nodes that
154 * have "0,xaui" will be preserved but all others, i.e. "0,sgmii" will be
155 * removed.
156 *
157 * Note that the trim_name must also match. If trim_name is NULL then it
158 * looks for the property "cavium,qlm-trim".
159 *
160 * Also, when the trim_name is "cavium,qlm-trim" or NULL that the interfaces
161 * will also be renamed based on their register values.
162 *
163 * For example, if a PIP interface is named "interface@W" and has the property
164 * reg = <0> then the interface will be renamed after this function to
165 * interface@0.
166 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100167 * Return: 0 for success.
Aaron Williams810d1682020-12-11 17:06:05 +0100168 */
169int octeon_fdt_patch_rename(void *fdt, const char *fdt_key,
170 const char *trim_name, bool rename,
171 void (*callback)(void *fdt, int offset, void *arg),
172 void *cbarg)
173 __attribute__((weak, alias("__octeon_fdt_patch_rename")));
174
175int __octeon_fdt_patch_rename(void *fdt, const char *fdt_key,
176 const char *trim_name, bool rename,
177 void (*callback)(void *fdt, int offset, void *arg),
178 void *cbarg)
179{
180 int fdt_key_len;
181 int offset, next_offset;
182 int aliases;
183 const void *aprop;
184 char qlm[32];
185 char *mode;
186 int qlm_key_len;
187 int rc;
188 int cpu_node;
189
190 if (!trim_name)
191 trim_name = "cavium,qlm-trim";
192
193 strncpy(qlm, fdt_key, sizeof(qlm));
194 mode = qlm;
195 strsep(&mode, ",");
196 qlm_key_len = strlen(qlm);
197
198 debug("In %s: Patching FDT header at 0x%p with key \"%s\"\n", __func__, fdt, fdt_key);
199 if (!fdt || fdt_check_header(fdt) != 0) {
200 printf("%s: Invalid device tree\n", __func__);
201 return -1;
202 }
203
204 fdt_key_len = strlen(fdt_key) + 1;
205
206 /* Prune out the unwanted parts based on the QLM mode. */
207 offset = 0;
208 for (offset = fdt_next_node(fdt, offset, NULL); offset >= 0; offset = next_offset) {
209 int len;
210 const char *val;
211 const char *val_comma;
212
213 next_offset = fdt_next_node(fdt, offset, NULL);
214
215 val = fdt_getprop(fdt, offset, trim_name, &len);
216 if (!val)
217 continue;
218
219 debug("fdt found trim name %s, comparing key \"%s\"(%d) with \"%s\"(%d)\n",
220 trim_name, fdt_key, fdt_key_len, val, len);
221 val_comma = strchr(val, ',');
222 if (!val_comma || (val_comma - val) != qlm_key_len)
223 continue;
224 if (strncmp(val, qlm, qlm_key_len) != 0)
225 continue; /* Not this QLM. */
226
227 debug("fdt key number \"%s\" matches\n", val);
228 if (!fdt_stringlist_contains(val, len, fdt_key)) {
229 debug("Key \"%s\" does not match \"%s\"\n", val, fdt_key);
230 /* This QLM, but wrong mode. Delete it. */
231 /* See if there's an alias that needs deleting */
232 val = fdt_getprop(fdt, offset, "cavium,qlm-trim-alias", NULL);
233 if (val) {
234 debug("Trimming alias \"%s\"\n", val);
235 aliases = fdt_path_offset(fdt, "/aliases");
236 if (aliases) {
237 aprop = fdt_getprop(fdt, aliases, val, NULL);
238 if (aprop) {
239 rc = fdt_nop_property(fdt, aliases, val);
240 if (rc) {
241 printf("Error: Could not NOP alias %s in fdt\n",
242 val);
243 }
244 } else {
245 printf("Error: could not find /aliases/%s in device tree\n",
246 val);
247 }
248 } else {
249 puts("Error: could not find /aliases in device tree\n");
250 }
251 }
252 debug("fdt trimming matching key %s\n", fdt_key);
253 next_offset = fdt_parent_offset(fdt, offset);
254 rc = fdt_nop_node(fdt, offset);
255 if (rc)
256 printf("Error %d noping node in device tree\n", rc);
257 }
258 }
259
260 debug("%s: Starting pass 2 for key %s\n", __func__, fdt_key);
261 /* Second pass: Rewrite names and remove key properties. */
262 offset = -1;
263 for (offset = fdt_next_node(fdt, offset, NULL); offset >= 0; offset = next_offset) {
264 int len;
265 const char *val = fdt_getprop(fdt, offset, trim_name, &len);
266
267 next_offset = fdt_next_node(fdt, offset, NULL);
268
269 if (!val)
270 continue;
271 debug("Searching stringlist %s for %s\n", val, fdt_key);
272 if (fdt_stringlist_contains(val, len, fdt_key)) {
273 char new_name[64];
274 const char *name;
275 const char *at;
276 int reg;
277
278 debug("Found key %s at offset 0x%x\n", fdt_key, offset);
279 fdt_nop_property(fdt, offset, trim_name);
280
281 if (rename) {
282 name = fdt_get_name(fdt, offset, NULL);
283 debug(" name: %s\n", name);
284 if (!name)
285 continue;
286 at = strchr(name, '@');
287 if (!at)
288 continue;
289
290 reg = fdtdec_get_int(fdt, offset, "reg", -1);
291 if (reg == -1)
292 continue;
293
294 debug(" reg: %d\n", reg);
295 len = at - name + 1;
296 debug(" len: %d\n", len);
297 if (len + 9 >= sizeof(new_name))
298 continue;
299
300 memcpy(new_name, name, len);
301 cpu_node = cvmx_fdt_get_cpu_node(fdt, offset);
302 if (cpu_node > 0)
303 snprintf(new_name + len, sizeof(new_name) - len, "%x_%x",
304 cpu_node, reg);
305 else
306 sprintf(new_name + len, "%x", reg);
307 debug("Renaming cpu node %d %s to %s\n", cpu_node, name, new_name);
308 fdt_set_name(fdt, offset, new_name);
309 }
310 if (callback)
311 callback(fdt, offset, cbarg);
312
313 /* Structure may have changed, start at the beginning. */
314 next_offset = 0;
315 }
316 }
317
318 return 0;
319}
320
321#ifdef CONFIG_CMD_NET
322static void octeon_set_one_fdt_mac(int node, uint64_t *mac)
323{
324 u8 mac_addr[6];
325 int r;
326
327 mac_addr[5] = *mac & 0xff;
328 mac_addr[4] = (*mac >> 8) & 0xff;
329 mac_addr[3] = (*mac >> 16) & 0xff;
330 mac_addr[2] = (*mac >> 24) & 0xff;
331 mac_addr[1] = (*mac >> 32) & 0xff;
332 mac_addr[0] = (*mac >> 40) & 0xff;
333
334 r = fdt_setprop_inplace(working_fdt, node, "local-mac-address", mac_addr, 6);
335 if (r == 0)
336 *mac = *mac + 1;
337}
338
339static uint64_t convert_mac(const u8 mac_addr[6])
340{
341 int i;
342 u64 mac = 0;
343
344 for (i = 0; i < 6; i++)
345 mac = (mac << 8) | mac_addr[i];
346 return mac;
347}
348
349/**
350 * Fix up the MAC address in the flat device tree based on the MAC address
351 * stored in ethaddr or in the board descriptor.
352 *
353 * NOTE: This function is weak and an alias for __octeon_fixup_fdt_mac_addr.
354 */
355void octeon_fixup_fdt_mac_addr(void) __attribute__((weak, alias("__octeon_fixup_fdt_mac_addr")));
356
357void __octeon_fixup_fdt_mac_addr(void)
358{
359 int node, pip, interface, ethernet;
360 int i, e;
361 u64 mac = 0;
362 uchar mac_addr[6];
363 char name[20];
364 bool env_mac_addr_valid;
365 const char *p;
366
367 debug("%s: env ethaddr: %s\n", __func__, (p = env_get("ethaddr")) ? p : "not set");
368 if (eth_env_get_enetaddr("ethaddr", mac_addr)) {
369 mac = convert_mac(mac_addr);
370 env_mac_addr_valid = true;
371 } else {
372 mac = convert_mac((uint8_t *)gd->arch.mac_desc.mac_addr_base);
373 env_mac_addr_valid = false;
374 }
375
376 debug("%s: mac_addr: %pM, board mac: %pM, env valid: %s\n", __func__, mac_addr,
377 gd->arch.mac_desc.mac_addr_base, env_mac_addr_valid ? "true" : "false");
378
379 if (env_mac_addr_valid && memcmp(mac_addr, (void *)gd->arch.mac_desc.mac_addr_base, 6))
380 printf("Warning: the environment variable ethaddr is set to %pM\n"
381 "which does not match the board descriptor MAC address %pM.\n"
382 "Please clear the ethaddr environment variable with the command\n"
383 "\"setenv -f ethaddr; saveenv\" or change the board MAC address with the command\n"
384 "\"tlv_eeprom set mac %pM\" to change the board MAC address so that it matches\n"
385 "the environment address.\n"
386 "Note: the correct MAC address is usually the one stored in the tlv EEPROM.\n",
387 mac_addr, gd->arch.mac_desc.mac_addr_base, mac_addr);
388
389 for (i = 0; i < 2; i++) {
390 sprintf(name, "mix%x", i);
391 p = fdt_get_alias(working_fdt, name);
392 if (p) {
393 node = fdt_path_offset(working_fdt, p);
394 if (node > 0)
395 octeon_set_one_fdt_mac(node, &mac);
396 }
397 }
398
399 for (i = 0; i < 2; i++) {
400 sprintf(name, "rgmii%x", i);
401 p = fdt_get_alias(working_fdt, name);
402 if (p) {
403 node = fdt_path_offset(working_fdt, p);
404 if (node > 0)
405 octeon_set_one_fdt_mac(node, &mac);
406 }
407 }
408
409 pip = fdt_node_offset_by_compatible(working_fdt, -1, "cavium,octeon-3860-pip");
410
411 if (pip > 0)
412 for (i = 0; i < 8; i++) {
413 sprintf(name, "interface@%d", i);
414 interface = fdt_subnode_offset(working_fdt, pip, name);
415 if (interface <= 0)
416 continue;
417 for (e = 0; e < 16; e++) {
418 sprintf(name, "ethernet@%d", e);
419 ethernet = fdt_subnode_offset(working_fdt, interface, name);
420 if (ethernet <= 0)
421 continue;
422 octeon_set_one_fdt_mac(ethernet, &mac);
423 }
424 }
425
426 /* Assign 78XX addresses in the order they appear in the device tree. */
Marek Behún5d6b4482022-01-20 01:04:42 +0100427 fdt_for_each_node_by_compatible(node, working_fdt, -1, "cavium,octeon-7890-bgx-port")
Aaron Williams810d1682020-12-11 17:06:05 +0100428 octeon_set_one_fdt_mac(node, &mac);
Aaron Williams810d1682020-12-11 17:06:05 +0100429}
430#endif
431
432/**
433 * This function fixes the clock-frequency in the flat device tree for the UART.
434 *
435 * NOTE: This function is weak and an alias for __octeon_fixup_fdt_uart.
436 */
437void octeon_fixup_fdt_uart(void) __attribute__((weak, alias("__octeon_fixup_fdt_uart")));
438
439void __octeon_fixup_fdt_uart(void)
440{
441 u32 clk;
442 int node;
443
444 clk = gd->bus_clk;
445
446 /* Device trees already have good values for fast simulator
447 * output, real boards need the correct value.
448 */
Marek Behún5d6b4482022-01-20 01:04:42 +0100449 fdt_for_each_node_by_compatible(node, working_fdt, -1, "cavium,octeon-3860-uart")
Aaron Williams810d1682020-12-11 17:06:05 +0100450 fdt_setprop_inplace_cell(working_fdt, node, "clock-frequency", clk);
Aaron Williams810d1682020-12-11 17:06:05 +0100451}
452
453/**
454 * This function fills in the /memory portion of the flat device tree.
455 *
456 * NOTE: This function is weak and aliased to __octeon_fixup_fdt_memory.
457 */
458void octeon_fixup_fdt_memory(void) __attribute__((weak, alias("__octeon_fixup_fdt_memory")));
459
460void __octeon_fixup_fdt_memory(void)
461{
462 u64 sizes[3], addresses[3];
463 u64 size_left = gd->ram_size;
464 int num_addresses = 0;
465 int rc;
466 int node;
467
468 size_left = gd->ram_size;
469 sizes[num_addresses] = min_t(u64, size_left, 256 * 1024 * 1024);
470 size_left -= sizes[num_addresses];
471 addresses[num_addresses] = 0;
472 num_addresses++;
473
474 if (size_left > 0) {
475 sizes[num_addresses] = size_left;
476 addresses[num_addresses] = 0x20000000ULL;
477 num_addresses++;
478 }
479
480 node = fdt_path_offset(working_fdt, "/memory");
481 if (node < 0)
482 node = fdt_add_subnode(working_fdt, fdt_path_offset(working_fdt, "/"), "memory");
483 if (node < 0) {
484 printf("Could not add memory section to fdt: %s\n", fdt_strerror(node));
485 return;
486 }
487 rc = fdt_fixup_memory_banks(working_fdt, addresses, sizes, num_addresses);
488 if (rc != 0)
489 printf("%s: fdt_fixup_memory_banks returned %d when adding %d addresses\n",
490 __func__, rc, num_addresses);
491}
492
493void octeon_fixup_fdt(void) __attribute__((weak, alias("__octeon_fixup_fdt")));
494
495void __octeon_fixup_fdt(void)
496{
497 if (!working_fdt)
498 return;
499
500#ifdef CONFIG_CMD_NET
501 octeon_fixup_fdt_mac_addr();
502#endif /* CONFIG_CMD_NET */
503
504#if !CONFIG_OCTEON_SIM_SPEED
505 octeon_fixup_fdt_uart();
506#endif
507
508 octeon_fixup_fdt_memory();
509}
510
511int __board_fixup_fdt(void)
512{
513 /*
514 * Nothing to do in this dummy implementation
515 */
516 return 0;
517}
518
519int board_fixup_fdt(void) __attribute__((weak, alias("__board_fixup_fdt")));
520
521/**
522 * This is a helper function to find the offset of a PHY device given
523 * an Ethernet device.
524 *
525 * @param[in] eth - Ethernet device to search for PHY offset
526 *
527 * @returns offset of phy info in device tree or -1 if not found
528 */
529//int octeon_fdt_find_phy(const struct eth_device *eth)
530int octeon_fdt_find_phy(const struct udevice *eth)
531{
532 int aliases;
533 const void *fdt = gd->fdt_blob;
534 const char *pip_path;
535 int pip;
536 char buffer[64];
537#if 0
538 struct octeon_eth_info *oct_eth_info =
539 (struct octeon_eth_info *)eth->priv;
540#else
541 struct octeon_eth_info *oct_eth_info = dev_get_priv(eth);
542#endif
543 int interface, index;
544 int phandle;
545 int phy;
546 u32 *phy_handle;
547
548 aliases = fdt_path_offset(fdt, "/aliases");
549 if (aliases < 0) {
550 puts("/aliases not found in device tree!\n");
551 return -1;
552 }
553 pip_path = fdt_getprop(fdt, aliases, "pip", NULL);
554 if (!pip_path) {
555 puts("pip not found in aliases in device tree\n");
556 return -1;
557 }
558 pip = fdt_path_offset(fdt, pip_path);
559 if (pip < 0) {
560 puts("pip not found in device tree\n");
561 return -1;
562 }
563 snprintf(buffer, sizeof(buffer), "interface@%d", oct_eth_info->interface);
564 interface = fdt_subnode_offset(fdt, pip, buffer);
565 if (interface < 0) {
566 printf("%s: interface@%d not found in device tree for %s\n", __func__,
567 oct_eth_info->interface, eth->name);
568 return -1;
569 }
570 snprintf(buffer, sizeof(buffer), "ethernet@%x", oct_eth_info->index);
571 index = fdt_subnode_offset(fdt, interface, buffer);
572 if (index < 0) {
573 printf("%s: ethernet@%x not found in device tree for %s\n", __func__,
574 oct_eth_info->index, eth->name);
575 return -1;
576 }
577 phy_handle = (uint32_t *)fdt_getprop(fdt, index, "phy-handle", NULL);
578 if (phy_handle < 0) {
579 printf("%s: phy-handle not found for %s\n", __func__, eth->name);
580 return -1;
581 }
582 phandle = fdt32_to_cpu(*phy_handle);
583 phy = fdt_node_offset_by_phandle(fdt, phandle);
584 if (phy < 0) {
585 printf("%s: phy not found for %s\n", __func__, eth->name);
586 return -1;
587 }
588
589 return phy;
590}
591
592/**
593 * This helper function returns if a node contains the specified vendor name.
594 *
595 * @param[in] fdt pointer to device tree blob
596 * @param nodeoffset offset of the tree node
597 * @param[in] vendor name of vendor to check
598 *
599 * returns:
600 * 0, if the node has a compatible vendor string property
601 * 1, if the node does not contain the vendor string property
602 * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
603 * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
604 * -FDT_ERR_BADMAGIC,
605 * -FDT_ERR_BADVERSION,
606 * -FDT_BADSTATE,
607 * -FDT_ERR_BADSTRUCTURE, standard meanings
608 */
609int octeon_fdt_compat_vendor(const void *fdt, int nodeoffset, const char *vendor)
610{
611 const char *strlist;
612 const char *p;
613 int len;
614 int listlen;
615
616 strlist = fdt_getprop(fdt, nodeoffset, "compatible", &listlen);
617 if (!strlist)
618 return listlen;
619
620 len = strlen(vendor);
621
622 debug("%s(%p, %d, %s (%p)) strlist: %s (%p), len: %d\n", __func__, fdt, nodeoffset, vendor,
623 vendor, strlist, strlist, len);
624 while (listlen >= len) {
625 debug(" Comparing %d bytes of %s and %s\n", len, vendor, strlist);
626 if ((memcmp(vendor, strlist, len) == 0) &&
627 ((strlist[len] == ',') || (strlist[len] == '\0')))
628 return 0;
629 p = memchr(strlist, '\0', listlen);
630 if (!p)
631 return 1; /* malformed strlist.. */
632 listlen -= (p - strlist) + 1;
633 strlist = p + 1;
634 }
635 return 1;
636}
637
638/**
639 * Given a node in the device tree get the OCTEON OCX node number
640 *
641 * @param fdt pointer to flat device tree
642 * @param nodeoffset node offset to get OCX node for
643 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100644 * Return: the Octeon OCX node number
Aaron Williams810d1682020-12-11 17:06:05 +0100645 */
646int octeon_fdt_get_soc_node(const void *fdt, int nodeoffset)
647{
648 return 0;
649}
650
651/**
652 * Given a FDT node, check if it is compatible with a list of devices
653 *
654 * @param[in] fdt Flat device tree pointer
655 * @param node_offset Node offset in device tree
656 * @param[in] strlist Array of FDT devices to check, end must be NULL
657 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100658 * Return: 0 if at least one device is compatible, 1 if not compatible.
Aaron Williams810d1682020-12-11 17:06:05 +0100659 */
660int octeon_fdt_node_check_compatible(const void *fdt, int node_offset,
661 const char *const *strlist)
662{
663 while (*strlist && **strlist) {
664 debug("%s: Checking %s\n", __func__, *strlist);
665 if (!fdt_node_check_compatible(fdt, node_offset, *strlist)) {
666 debug("%s: match found\n", __func__);
667 return 0;
668 }
669 strlist++;
670 }
671 debug("%s: No match found\n", __func__);
672 return 1;
673}
674
675/**
676 * Given a node offset, find the i2c bus number for that node
677 *
678 * @param[in] fdt Pointer to flat device tree
679 * @param node_offset Node offset in device tree
680 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100681 * Return: i2c bus number or -1 if error
Aaron Williams810d1682020-12-11 17:06:05 +0100682 */
683int octeon_fdt_i2c_get_bus(const void *fdt, int node_offset)
684{
685 const char *compat;
686 const u64 addresses[] = { 0x1180000001000, 0x1180000001200 };
687 u64 reg;
688 int i;
689 int bus = -1;
690 bool found = false;
691
692 if (octeon_has_feature(OCTEON_FEATURE_CIU3))
693 compat = "cavium,octeon-7890-twsi";
694 else
695 compat = "cavium,octeon-3860-twsi";
696
697 while (node_offset > 0 &&
698 !(found = !fdt_node_check_compatible(fdt, node_offset, compat))) {
699 node_offset = fdt_parent_offset(fdt, node_offset);
700#ifdef CONFIG_OCTEON_I2C_FDT
701 bus = i2c_get_bus_num_fdt(node_offset);
702 if (bus >= 0) {
703 debug("%s: Found bus 0x%x\n", __func__, bus);
704 return bus;
705 }
706#endif
707 }
708 if (!found) {
709 printf("Error: node %d in device tree is not a child of the I2C bus\n",
710 node_offset);
711 return -1;
712 }
713
714 reg = fdtdec_get_addr(fdt, node_offset, "reg");
715 if (reg == FDT_ADDR_T_NONE) {
716 printf("%s: Error: invalid reg address for TWSI bus\n", __func__);
717 return -1;
718 }
719
720 for (i = 0; i < ARRAY_SIZE(addresses); i++)
721 if (reg == addresses[i]) {
722 bus = i;
723 break;
724 }
725
726 debug("%s: bus 0x%x\n", __func__, bus);
727 return bus;
728}
729
730/**
731 * Given an offset into the fdt, output the i2c bus and address of the device
732 *
733 * @param[in] fdt fdt blob pointer
734 * @param node offset in FDT of device
735 * @param[out] bus i2c bus number of device
736 * @param[out] addr address of device on i2c bus
737 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100738 * Return: 0 for success, -1 on error
Aaron Williams810d1682020-12-11 17:06:05 +0100739 */
740int octeon_fdt_get_i2c_bus_addr(const void *fdt, int node, int *bus, int *addr)
741{
742 *bus = octeon_fdt_i2c_get_bus(fdt, fdt_parent_offset(fdt, node));
743 if (*bus < 0) {
744 printf("%s: Could not get parent i2c bus\n", __func__);
745 return -1;
746 }
747 *addr = fdtdec_get_int(fdt, node, "reg", -1);
748 if (*addr < 0)
749 return -1;
750 return 0;
751}
752
753/**
754 * Reads a GPIO pin given the node of the GPIO device in the device tree and
755 * the pin number.
756 *
757 * @param[in] fdt fdt blob pointer
758 * @param phandle phandle of GPIO node
759 * @param pin pin number to read
760 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100761 * Return: 0 = pin is low, 1 = pin is high, -1 = error
Aaron Williams810d1682020-12-11 17:06:05 +0100762 */
763int octeon_fdt_read_gpio(const void *fdt, int phandle, int pin)
764{
765 enum cvmx_gpio_type type;
766 __maybe_unused int node;
767 __maybe_unused int addr;
768 __maybe_unused int bus;
769 __maybe_unused int old_bus;
770 int num_pins;
771 int value;
772
773 type = cvmx_fdt_get_gpio_type(fdt, phandle, &num_pins);
774 if ((pin & 0xff) >= num_pins) {
775 debug("%s: pin number %d out of range\n", __func__, pin);
776 return -1;
777 }
778 switch (type) {
779#ifdef CONFIG_PCA953X
780 case CVMX_GPIO_PIN_PCA953X:
781 node = fdt_node_offset_by_phandle(fdt, phandle);
782 if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
783 printf("%s: Could not get gpio bus and/or address\n", __func__);
784 return -1;
785 }
786 value = pca953x_get_val(bus, addr);
787 if (value < 0) {
788 printf("%s: Error reading PCA953X GPIO at 0x%x:0x%x\n", __func__, bus,
789 addr);
790 return -1;
791 }
792 value = (value >> pin) & 1;
793 break;
794#endif
795#ifdef CONFIG_PCF857X
796 case CVMX_GPIO_PIN_PCF857X:
797 node = fdt_node_offset_by_phandle(fdt, phandle);
798 if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
799 printf("%s: Could not get gpio bus and/or address\n", __func__);
800 return -1;
801 }
802 value = pcf857x_get_val(bus, addr);
803 if (value < 0) {
804 printf("%s: Error reading PCF857X GPIO at 0x%x:0x%x\n", __func__, bus,
805 addr);
806 return -1;
807 }
808 value = (value >> pin) & 1;
809 break;
810#endif
811#ifdef CONFIG_PCA9698
812 case CVMX_GPIO_PIN_PCA9698:
813 node = fdt_node_offset_by_phandle(fdt, phandle);
814 if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
815 printf("%s: Could not get gpio bus and/or address\n", __func__);
816 return -1;
817 }
818 old_bus = i2c_get_bus_num();
819 i2c_set_bus_num(bus);
820 value = pca9698_get_value(addr, pin);
821 i2c_set_bus_num(old_bus);
822 break;
823#endif
824 case CVMX_GPIO_PIN_OCTEON:
825 value = gpio_get_value(pin);
826 break;
827 default:
828 printf("%s: Unknown GPIO type %d\n", __func__, type);
829 return -1;
830 }
831 return value;
832}
833
834/**
835 * Reads a GPIO pin given the node of the GPIO device in the device tree and
836 * the pin number.
837 *
838 * @param[in] fdt fdt blob pointer
839 * @param phandle phandle of GPIO node
840 * @param pin pin number to read
841 * @param val value to write (1 = high, 0 = low)
842 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100843 * Return: 0 = success, -1 = error
Aaron Williams810d1682020-12-11 17:06:05 +0100844 */
845int octeon_fdt_set_gpio(const void *fdt, int phandle, int pin, int val)
846{
847 enum cvmx_gpio_type type;
848 int node;
849 int num_pins;
850 __maybe_unused int addr;
851 __maybe_unused int bus;
852 __maybe_unused int old_bus;
853 __maybe_unused int rc;
854
855 node = fdt_node_offset_by_phandle(fdt, phandle);
856 if (node < 0) {
857 printf("%s: Invalid phandle\n", __func__);
858 return -1;
859 }
860
861 type = cvmx_fdt_get_gpio_type(fdt, phandle, &num_pins);
862 if ((pin & 0xff) >= num_pins) {
863 debug("%s: pin number %d out of range\n", __func__, pin);
864 return -1;
865 }
866 switch (type) {
867#ifdef CONFIG_PCA953X
868 case CVMX_GPIO_PIN_PCA953X:
869 if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
870 printf("%s: Could not get gpio bus and/or address\n", __func__);
871 return -1;
872 }
873
874 return pca953x_set_val(bus, addr, 1 << pin, val << pin);
875#endif
876#ifdef CONFIG_PCF857X
877 case CVMX_GPIO_PIN_PCF857X:
878 if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
879 printf("%s: Could not get gpio bus and/or address\n", __func__);
880 return -1;
881 }
882 return pcf957x_set_val(bus, addr, 1 << pin, val << pin);
883#endif
884#ifdef CONFIG_PCA9698
885 case CVMX_GPIO_PIN_PCA9698:
886 if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
887 printf("%s: Could not get gpio bus and/or address\n", __func__);
888 return -1;
889 }
890 old_bus = i2c_get_bus_num();
891 i2c_set_bus_num(bus);
892 rc = pca9698_set_value(addr, pin, val);
893 i2c_set_bus_num(old_bus);
894 return rc;
895#endif
896 case CVMX_GPIO_PIN_OCTEON:
897 return gpio_set_value(pin, val);
898 default:
899 printf("%s: Unknown GPIO type %d\n", __func__, type);
900 return -1;
901 }
902}
903
904/**
905 * Given the node of a GPIO entry output the GPIO type, i2c bus and i2c
906 * address.
907 *
908 * @param fdt_node node of GPIO in device tree, generally
909 * derived from a phandle.
910 * @param[out] type Type of GPIO detected
911 * @param[out] i2c_bus For i2c GPIO expanders, the i2c bus number
912 * @param[out] i2c_addr For i2c GPIO expanders, the i2c address
913 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100914 * Return: 0 for success, -1 for errors
Aaron Williams810d1682020-12-11 17:06:05 +0100915 *
916 * NOTE: It is up to the caller to determine the pin number.
917 */
918int octeon_fdt_get_gpio_info(int fdt_node, enum octeon_gpio_type *type,
919 int *i2c_bus, int *i2c_addr)
920{
921 const void *fdt = gd->fdt_blob;
922
923 int i2c_bus_node __attribute__((unused));
924
925 *type = GPIO_TYPE_UNKNOWN;
926
927 if (!octeon_fdt_node_check_compatible(fdt, fdt_node, octeon_gpio_list)) {
928 debug("%s: Found Octeon compatible GPIO\n", __func__);
929 *type = GPIO_TYPE_OCTEON;
930 if (i2c_bus)
931 *i2c_bus = -1;
932 if (i2c_addr)
933 *i2c_addr = -1;
934 return 0;
935 }
936#ifdef CONFIG_PCA9555
937 if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca9555_gpio_list)) {
938 debug("%s: Found PCA9555 type compatible GPIO\n", __func__);
939 *type = GPIO_TYPE_PCA9555;
940 }
941#endif
942#ifdef CONFIG_PCA9554
943 if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca9554_gpio_list)) {
944 debug("%s: Found PCA9555 type compatible GPIO\n", __func__);
945 *type = GPIO_TYPE_PCA9554;
946 }
947#endif
948#ifdef CONFIG_PCA953X
949 if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca953x_gpio_list)) {
950 debug("%s: Found PCA953x compatible GPIO", __func__);
951 *type = GPIO_TYPE_PCA953X;
952 }
953#endif
954#ifdef CONFIG_PCA9698
955 if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca9698_gpio_list)) {
956 debug("%s: Found PCA9698 compatible GPIO", __func__);
957 *type = GPIO_TYPE_PCA9698;
958 }
959#endif
960#if defined(CONFIG_PCA953X) || defined(CONFIG_PCA9698) || \
961 defined(CONFIG_PCA9555) || defined(CONFIG_PCA9554)
962 if (!i2c_addr || !i2c_bus) {
963 printf("%s: Error: i2c_addr or i2c_bus is NULL\n", __func__);
964 return -1;
965 }
966
967 *i2c_addr = fdtdec_get_int(fdt, fdt_node, "reg", -1);
968 i2c_bus_node = fdt_parent_offset(fdt, fdt_node);
969 if (i2c_bus_node < 0) {
970 printf("%s: Invalid parent\n", __func__);
971 return -1;
972 }
973 *i2c_bus = i2c_get_bus_num_fdt(i2c_bus_node);
974#endif
975 return (*type != GPIO_TYPE_UNKNOWN) ? 0 : -1;
976}
977
978#ifdef CONFIG_PHY_VITESSE
979/**
980 * Given a node in the flat device tree, return the matching PHY device
981 *
982 * @param fdt_node FDT node in device tree
983 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100984 * Return: pointer to PHY device or NULL if none found.
Aaron Williams810d1682020-12-11 17:06:05 +0100985 */
986static struct phy_device *octeon_fdt_get_phy_device_from_node(int fdt_node)
987{
988 struct eth_device *dev;
989 int i = 0;
990 struct octeon_eth_info *ethinfo = NULL;
991
992 do {
993 dev = eth_get_dev_by_index(i++);
994 if (!dev)
995 return NULL;
996 ethinfo = dev->priv;
997 if (ethinfo->phy_offset == fdt_node)
998 return ethinfo->phydev;
999 } while (dev);
1000 return NULL;
1001}
1002#endif
1003
1004/**
1005 * Get the PHY data structure for the specified FDT node and output the type
1006 *
1007 * @param fdt_node FDT node of phy
1008 * @param[out] type Type of GPIO
1009 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001010 * Return: pointer to phy device or NULL if no match found.
Aaron Williams810d1682020-12-11 17:06:05 +01001011 */
1012struct phy_device *octeon_fdt_get_phy_gpio_info(int fdt_node, enum octeon_gpio_type *type)
1013{
1014#ifdef CONFIG_PHY_VITESSE
1015 struct phy_device *phydev;
1016
1017 if (!octeon_fdt_node_check_compatible(gd->fdt_blob, fdt_node,
1018 vitesse_vsc8488_gpio_list)) {
1019 phydev = octeon_fdt_get_phy_device_from_node(fdt_node);
1020 if (phydev) {
1021 debug("%s: Found Vitesse VSC848X compatible GPIO\n", __func__);
1022 *type = GPIO_TYPE_VSC8488;
1023 return phydev;
1024 }
1025
1026 debug("%s: Error: phy device not found!\n", __func__);
1027 return NULL;
1028 }
1029
1030 debug("%s: No compatible Vitesse PHY type found\n", __func__);
1031#endif
1032 return NULL;
1033}