summaryrefslogtreecommitdiff
path: root/arch/mips/mach-octeon/octeon_fdt.c
blob: 199f69251668da04cff2a6a95ccc5b29bae1a1de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2020 Marvell International Ltd.
 */

#include <env.h>
#include <log.h>
#include <i2c.h>
#include <net.h>
#include <dm/device.h>
#include <linux/delay.h>

#include <mach/cvmx-regs.h>
#include <mach/cvmx-csr.h>
#include <mach/cvmx-bootmem.h>
#include <mach/octeon-model.h>
#include <mach/octeon_eth.h>
#include <mach/octeon_fdt.h>
#include <mach/cvmx-helper-fdt.h>
#include <mach/cvmx-helper-gpio.h>
#include <mach/cvmx-fuse.h>
#include <mach/octeon-feature.h>
#include <mach/cvmx-qlm.h>
#include <mach/octeon_qlm.h>
#include <asm/gpio.h>

#ifdef CONFIG_PCA953X
#include <pca953x.h>
#endif
#ifdef CONFIG_PCF857X
#include <pcf857x.h>
#endif
#ifdef CONFIG_PCA9698
#include <pca9698.h>
#endif
#ifdef CONFIG_PCA9554
#include <pca9554.h>
#endif
#ifdef CONFIG_PCA9555
#include <pca9555.h>
#endif

DECLARE_GLOBAL_DATA_PTR;

#ifdef CONFIG_PCA9554
static const char * const pca9554_gpio_list[] = {
	"pca9554",
	"nxp,pca9554",
	"ti,pca9554",
	NULL,
};
#endif

#ifdef CONFIG_PCA9555
static const char * const pca9555_gpio_list[] = {
	"pca9535",    "nxp,pca9535", "pca9539", "nxp,pca9539", "pca9555",
	"nxp,pca9555", "ti,pca9555", "max7312", "maxim,max7312", "max7313",
	"maxim,max7313", "tca6416", "tca9539",    NULL,
};
#endif

#ifdef CONFIG_PCA9698
/** List of compatible strings supported by pca9698 driver */
static const char * const pca9698_gpio_list[] = {
	"nxp,pca9505", "pca9505", "nxp,pca9698", "pca9698", NULL,
};
#endif

#ifdef CONFIG_PCA953X
/** List of compatible strings supported by pca953x driver */
static const char * const pca953x_gpio_list[] = {
	"nxp,pca9534", "nxp,pca9535", "nxp,pca9536", "nxp,pca9537", "nxp,pca9538", "nxp,pca9539",
	"nxp,pca953x", "nxp,pca9554", "nxp,pca9555", "nxp,pca9556", "nxp,pca9557", "nxp,pca6107",
	"pca9534",     "pca9535",     "pca9536",     "pca9537",	    "pca9538",	   "pca9539",
	"pca953x",     "pca9554",     "pca9555",     "pca9556",	    "pca9557",	   "max7310",
	"max7312",     "max7313",     "max7315",     "pca6107",	    "tca6408",	   "tca6416",
	"tca9555",     NULL
};
#endif

#ifdef CONFIG_PHY_VITESSE
static const char * const vitesse_vsc8488_gpio_list[] = {
	"vitesse,vsc8486",   "microsemi,vsc8486", "vitesse,vsc8488",
	"microsemi,vsc8488", "vitesse,vsc8489",	  "microsemi,vsc8489",
	"vitesse,vsc8490",   "microsemi,vsc8490", NULL
};
#endif

/** List of compatible strings supported by Octeon driver */
static const char * const octeon_gpio_list[] = {
	"cavium,octeon-7890-gpio",
	"cavium,octeon-3860-gpio",
	NULL
};

/**
 * Trims nodes from the flat device tree.
 *
 * @param fdt - pointer to working FDT, usually in gd->fdt_blob
 * @param fdt_key - key to preserve.  All non-matching keys are removed
 * @param trim_name - name of property to look for.  If NULL use
 *		      'cavium,qlm-trim'
 *
 * The key should look something like device #, type where device # is a
 * number from 0-9 and type is a string describing the type.  For QLM
 * operations this would typically contain the QLM number followed by
 * the type in the device tree, like "0,xaui", "0,sgmii", etc.  This function
 * will trim all items in the device tree which match the device number but
 * have a type which does not match.  For example, if a QLM has a xaui module
 * installed on QLM 0 and "0,xaui" is passed as a key, then all FDT nodes that
 * have "0,xaui" will be preserved but all others, i.e. "0,sgmii" will be
 * removed.
 *
 * Note that the trim_name must also match.  If trim_name is NULL then it
 * looks for the property "cavium,qlm-trim".
 *
 * Also, when the trim_name is "cavium,qlm-trim" or NULL that the interfaces
 * will also be renamed based on their register values.
 *
 * For example, if a PIP interface is named "interface@W" and has the property
 * reg = <0> then the interface will be renamed after this function to
 * interface@0.
 *
 * @return 0 for success.
 */
int __octeon_fdt_patch(void *fdt, const char *fdt_key, const char *trim_name)
{
	bool rename = !trim_name || !strcmp(trim_name, "cavium,qlm-trim");

	return octeon_fdt_patch_rename(fdt, fdt_key, trim_name, rename, NULL, NULL);
}

int octeon_fdt_patch(void *fdt, const char *fdt_key, const char *trim_name)
	__attribute__((weak, alias("__octeon_fdt_patch")));

/**
 * Trims nodes from the flat device tree.
 *
 * @param fdt - pointer to working FDT, usually in gd->fdt_blob
 * @param fdt_key - key to preserve.  All non-matching keys are removed
 * @param trim_name - name of property to look for.  If NULL use
 *		      'cavium,qlm-trim'
 * @param rename - set to TRUE to rename interfaces.
 * @param callback - function to call on matched nodes.
 * @param cbarg - passed to callback.
 *
 * The key should look something like device #, type where device # is a
 * number from 0-9 and type is a string describing the type.  For QLM
 * operations this would typically contain the QLM number followed by
 * the type in the device tree, like "0,xaui", "0,sgmii", etc.  This function
 * will trim all items in the device tree which match the device number but
 * have a type which does not match.  For example, if a QLM has a xaui module
 * installed on QLM 0 and "0,xaui" is passed as a key, then all FDT nodes that
 * have "0,xaui" will be preserved but all others, i.e. "0,sgmii" will be
 * removed.
 *
 * Note that the trim_name must also match.  If trim_name is NULL then it
 * looks for the property "cavium,qlm-trim".
 *
 * Also, when the trim_name is "cavium,qlm-trim" or NULL that the interfaces
 * will also be renamed based on their register values.
 *
 * For example, if a PIP interface is named "interface@W" and has the property
 * reg = <0> then the interface will be renamed after this function to
 * interface@0.
 *
 * @return 0 for success.
 */
int octeon_fdt_patch_rename(void *fdt, const char *fdt_key,
			    const char *trim_name, bool rename,
			    void (*callback)(void *fdt, int offset, void *arg),
			    void *cbarg)
	__attribute__((weak, alias("__octeon_fdt_patch_rename")));

int __octeon_fdt_patch_rename(void *fdt, const char *fdt_key,
			      const char *trim_name, bool rename,
			      void (*callback)(void *fdt, int offset, void *arg),
			      void *cbarg)
{
	int fdt_key_len;
	int offset, next_offset;
	int aliases;
	const void *aprop;
	char qlm[32];
	char *mode;
	int qlm_key_len;
	int rc;
	int cpu_node;

	if (!trim_name)
		trim_name = "cavium,qlm-trim";

	strncpy(qlm, fdt_key, sizeof(qlm));
	mode = qlm;
	strsep(&mode, ",");
	qlm_key_len = strlen(qlm);

	debug("In %s: Patching FDT header at 0x%p with key \"%s\"\n", __func__, fdt, fdt_key);
	if (!fdt || fdt_check_header(fdt) != 0) {
		printf("%s: Invalid device tree\n", __func__);
		return -1;
	}

	fdt_key_len = strlen(fdt_key) + 1;

	/* Prune out the unwanted parts based on the QLM mode.  */
	offset = 0;
	for (offset = fdt_next_node(fdt, offset, NULL); offset >= 0; offset = next_offset) {
		int len;
		const char *val;
		const char *val_comma;

		next_offset = fdt_next_node(fdt, offset, NULL);

		val = fdt_getprop(fdt, offset, trim_name, &len);
		if (!val)
			continue;

		debug("fdt found trim name %s, comparing key \"%s\"(%d) with \"%s\"(%d)\n",
		      trim_name, fdt_key, fdt_key_len, val, len);
		val_comma = strchr(val, ',');
		if (!val_comma || (val_comma - val) != qlm_key_len)
			continue;
		if (strncmp(val, qlm, qlm_key_len) != 0)
			continue; /* Not this QLM. */

		debug("fdt key number \"%s\" matches\n", val);
		if (!fdt_stringlist_contains(val, len, fdt_key)) {
			debug("Key \"%s\" does not match \"%s\"\n", val, fdt_key);
			/* This QLM, but wrong mode.  Delete it. */
			/* See if there's an alias that needs deleting */
			val = fdt_getprop(fdt, offset, "cavium,qlm-trim-alias", NULL);
			if (val) {
				debug("Trimming alias \"%s\"\n", val);
				aliases = fdt_path_offset(fdt, "/aliases");
				if (aliases) {
					aprop = fdt_getprop(fdt, aliases, val, NULL);
					if (aprop) {
						rc = fdt_nop_property(fdt, aliases, val);
						if (rc) {
							printf("Error: Could not NOP alias %s in fdt\n",
							       val);
						}
					} else {
						printf("Error: could not find /aliases/%s in device tree\n",
						       val);
					}
				} else {
					puts("Error: could not find /aliases in device tree\n");
				}
			}
			debug("fdt trimming matching key %s\n", fdt_key);
			next_offset = fdt_parent_offset(fdt, offset);
			rc = fdt_nop_node(fdt, offset);
			if (rc)
				printf("Error %d noping node in device tree\n", rc);
		}
	}

	debug("%s: Starting pass 2 for key %s\n", __func__, fdt_key);
	/* Second pass: Rewrite names and remove key properties.  */
	offset = -1;
	for (offset = fdt_next_node(fdt, offset, NULL); offset >= 0; offset = next_offset) {
		int len;
		const char *val = fdt_getprop(fdt, offset, trim_name, &len);

		next_offset = fdt_next_node(fdt, offset, NULL);

		if (!val)
			continue;
		debug("Searching stringlist %s for %s\n", val, fdt_key);
		if (fdt_stringlist_contains(val, len, fdt_key)) {
			char new_name[64];
			const char *name;
			const char *at;
			int reg;

			debug("Found key %s at offset 0x%x\n", fdt_key, offset);
			fdt_nop_property(fdt, offset, trim_name);

			if (rename) {
				name = fdt_get_name(fdt, offset, NULL);
				debug("  name: %s\n", name);
				if (!name)
					continue;
				at = strchr(name, '@');
				if (!at)
					continue;

				reg = fdtdec_get_int(fdt, offset, "reg", -1);
				if (reg == -1)
					continue;

				debug("  reg: %d\n", reg);
				len = at - name + 1;
				debug("  len: %d\n", len);
				if (len + 9 >= sizeof(new_name))
					continue;

				memcpy(new_name, name, len);
				cpu_node = cvmx_fdt_get_cpu_node(fdt, offset);
				if (cpu_node > 0)
					snprintf(new_name + len, sizeof(new_name) - len, "%x_%x",
						 cpu_node, reg);
				else
					sprintf(new_name + len, "%x", reg);
				debug("Renaming cpu node %d %s to %s\n", cpu_node, name, new_name);
				fdt_set_name(fdt, offset, new_name);
			}
			if (callback)
				callback(fdt, offset, cbarg);

			/* Structure may have changed, start at the beginning. */
			next_offset = 0;
		}
	}

	return 0;
}

#ifdef CONFIG_CMD_NET
static void octeon_set_one_fdt_mac(int node, uint64_t *mac)
{
	u8 mac_addr[6];
	int r;

	mac_addr[5] = *mac & 0xff;
	mac_addr[4] = (*mac >> 8) & 0xff;
	mac_addr[3] = (*mac >> 16) & 0xff;
	mac_addr[2] = (*mac >> 24) & 0xff;
	mac_addr[1] = (*mac >> 32) & 0xff;
	mac_addr[0] = (*mac >> 40) & 0xff;

	r = fdt_setprop_inplace(working_fdt, node, "local-mac-address", mac_addr, 6);
	if (r == 0)
		*mac = *mac + 1;
}

static uint64_t convert_mac(const u8 mac_addr[6])
{
	int i;
	u64 mac = 0;

	for (i = 0; i < 6; i++)
		mac = (mac << 8) | mac_addr[i];
	return mac;
}

/**
 * Fix up the MAC address in the flat device tree based on the MAC address
 * stored in ethaddr or in the board descriptor.
 *
 * NOTE: This function is weak and an alias for __octeon_fixup_fdt_mac_addr.
 */
void octeon_fixup_fdt_mac_addr(void) __attribute__((weak, alias("__octeon_fixup_fdt_mac_addr")));

void __octeon_fixup_fdt_mac_addr(void)
{
	int node, pip, interface, ethernet;
	int i, e;
	u64 mac = 0;
	uchar mac_addr[6];
	char name[20];
	bool env_mac_addr_valid;
	const char *p;

	debug("%s: env ethaddr: %s\n", __func__, (p = env_get("ethaddr")) ? p : "not set");
	if (eth_env_get_enetaddr("ethaddr", mac_addr)) {
		mac = convert_mac(mac_addr);
		env_mac_addr_valid = true;
	} else {
		mac = convert_mac((uint8_t *)gd->arch.mac_desc.mac_addr_base);
		env_mac_addr_valid = false;
	}

	debug("%s: mac_addr: %pM, board mac: %pM, env valid: %s\n", __func__, mac_addr,
	      gd->arch.mac_desc.mac_addr_base, env_mac_addr_valid ? "true" : "false");

	if (env_mac_addr_valid && memcmp(mac_addr, (void *)gd->arch.mac_desc.mac_addr_base, 6))
		printf("Warning: the environment variable ethaddr is set to %pM\n"
		       "which does not match the board descriptor MAC address %pM.\n"
		       "Please clear the ethaddr environment variable with the command\n"
		       "\"setenv -f ethaddr; saveenv\" or change the board MAC address with the command\n"
		       "\"tlv_eeprom set mac %pM\" to change the board MAC address so that it matches\n"
		       "the environment address.\n"
		       "Note: the correct MAC address is usually the one stored in the tlv EEPROM.\n",
		       mac_addr, gd->arch.mac_desc.mac_addr_base, mac_addr);

	for (i = 0; i < 2; i++) {
		sprintf(name, "mix%x", i);
		p = fdt_get_alias(working_fdt, name);
		if (p) {
			node = fdt_path_offset(working_fdt, p);
			if (node > 0)
				octeon_set_one_fdt_mac(node, &mac);
		}
	}

	for (i = 0; i < 2; i++) {
		sprintf(name, "rgmii%x", i);
		p = fdt_get_alias(working_fdt, name);
		if (p) {
			node = fdt_path_offset(working_fdt, p);
			if (node > 0)
				octeon_set_one_fdt_mac(node, &mac);
		}
	}

	pip = fdt_node_offset_by_compatible(working_fdt, -1, "cavium,octeon-3860-pip");

	if (pip > 0)
		for (i = 0; i < 8; i++) {
			sprintf(name, "interface@%d", i);
			interface = fdt_subnode_offset(working_fdt, pip, name);
			if (interface <= 0)
				continue;
			for (e = 0; e < 16; e++) {
				sprintf(name, "ethernet@%d", e);
				ethernet = fdt_subnode_offset(working_fdt, interface, name);
				if (ethernet <= 0)
					continue;
				octeon_set_one_fdt_mac(ethernet, &mac);
			}
		}

	/* Assign 78XX addresses in the order they appear in the device tree. */
	node = fdt_node_offset_by_compatible(working_fdt, -1, "cavium,octeon-7890-bgx-port");
	while (node != -FDT_ERR_NOTFOUND) {
		octeon_set_one_fdt_mac(node, &mac);
		node = fdt_node_offset_by_compatible(working_fdt, node,
						     "cavium,octeon-7890-bgx-port");
	}
}
#endif

/**
 * This function fixes the clock-frequency in the flat device tree for the UART.
 *
 * NOTE: This function is weak and an alias for __octeon_fixup_fdt_uart.
 */
void octeon_fixup_fdt_uart(void) __attribute__((weak, alias("__octeon_fixup_fdt_uart")));

void __octeon_fixup_fdt_uart(void)
{
	u32 clk;
	int node;

	clk = gd->bus_clk;

	/* Device trees already have good values for fast simulator
	 * output, real boards need the correct value.
	 */
	node = fdt_node_offset_by_compatible(working_fdt, -1, "cavium,octeon-3860-uart");
	while (node != -FDT_ERR_NOTFOUND) {
		fdt_setprop_inplace_cell(working_fdt, node, "clock-frequency", clk);
		node = fdt_node_offset_by_compatible(working_fdt, node, "cavium,octeon-3860-uart");
	}
}

/**
 * This function fills in the /memory portion of the flat device tree.
 *
 * NOTE: This function is weak and aliased to __octeon_fixup_fdt_memory.
 */
void octeon_fixup_fdt_memory(void) __attribute__((weak, alias("__octeon_fixup_fdt_memory")));

void __octeon_fixup_fdt_memory(void)
{
	u64 sizes[3], addresses[3];
	u64 size_left = gd->ram_size;
	int num_addresses = 0;
	int rc;
	int node;

	size_left = gd->ram_size;
	sizes[num_addresses] = min_t(u64, size_left, 256 * 1024 * 1024);
	size_left -= sizes[num_addresses];
	addresses[num_addresses] = 0;
	num_addresses++;

	if (size_left > 0) {
		sizes[num_addresses] = size_left;
		addresses[num_addresses] = 0x20000000ULL;
		num_addresses++;
	}

	node = fdt_path_offset(working_fdt, "/memory");
	if (node < 0)
		node = fdt_add_subnode(working_fdt, fdt_path_offset(working_fdt, "/"), "memory");
	if (node < 0) {
		printf("Could not add memory section to fdt: %s\n", fdt_strerror(node));
		return;
	}
	rc = fdt_fixup_memory_banks(working_fdt, addresses, sizes, num_addresses);
	if (rc != 0)
		printf("%s: fdt_fixup_memory_banks returned %d when adding %d addresses\n",
		       __func__, rc, num_addresses);
}

void octeon_fixup_fdt(void) __attribute__((weak, alias("__octeon_fixup_fdt")));

void __octeon_fixup_fdt(void)
{
	if (!working_fdt)
		return;

#ifdef CONFIG_CMD_NET
	octeon_fixup_fdt_mac_addr();
#endif /* CONFIG_CMD_NET */

#if !CONFIG_OCTEON_SIM_SPEED
	octeon_fixup_fdt_uart();
#endif

	octeon_fixup_fdt_memory();
}

int __board_fixup_fdt(void)
{
	/*
	 * Nothing to do in this dummy implementation
	 */
	return 0;
}

int board_fixup_fdt(void) __attribute__((weak, alias("__board_fixup_fdt")));

/**
 * This is a helper function to find the offset of a PHY device given
 * an Ethernet device.
 *
 * @param[in] eth - Ethernet device to search for PHY offset
 *
 * @returns offset of phy info in device tree or -1 if not found
 */
//int octeon_fdt_find_phy(const struct eth_device *eth)
int octeon_fdt_find_phy(const struct udevice *eth)
{
	int aliases;
	const void *fdt = gd->fdt_blob;
	const char *pip_path;
	int pip;
	char buffer[64];
#if 0
	struct octeon_eth_info *oct_eth_info =
				 (struct octeon_eth_info *)eth->priv;
#else
	struct octeon_eth_info *oct_eth_info = dev_get_priv(eth);
#endif
	int interface, index;
	int phandle;
	int phy;
	u32 *phy_handle;

	aliases = fdt_path_offset(fdt, "/aliases");
	if (aliases < 0) {
		puts("/aliases not found in device tree!\n");
		return -1;
	}
	pip_path = fdt_getprop(fdt, aliases, "pip", NULL);
	if (!pip_path) {
		puts("pip not found in aliases in device tree\n");
		return -1;
	}
	pip = fdt_path_offset(fdt, pip_path);
	if (pip < 0) {
		puts("pip not found in device tree\n");
		return -1;
	}
	snprintf(buffer, sizeof(buffer), "interface@%d", oct_eth_info->interface);
	interface = fdt_subnode_offset(fdt, pip, buffer);
	if (interface < 0) {
		printf("%s: interface@%d not found in device tree for %s\n", __func__,
		       oct_eth_info->interface, eth->name);
		return -1;
	}
	snprintf(buffer, sizeof(buffer), "ethernet@%x", oct_eth_info->index);
	index = fdt_subnode_offset(fdt, interface, buffer);
	if (index < 0) {
		printf("%s: ethernet@%x not found in device tree for %s\n", __func__,
		       oct_eth_info->index, eth->name);
		return -1;
	}
	phy_handle = (uint32_t *)fdt_getprop(fdt, index, "phy-handle", NULL);
	if (phy_handle < 0) {
		printf("%s: phy-handle not found for %s\n", __func__, eth->name);
		return -1;
	}
	phandle = fdt32_to_cpu(*phy_handle);
	phy = fdt_node_offset_by_phandle(fdt, phandle);
	if (phy < 0) {
		printf("%s: phy not found for %s\n", __func__, eth->name);
		return -1;
	}

	return phy;
}

/**
 * This helper function returns if a node contains the specified vendor name.
 *
 * @param[in]	fdt		pointer to device tree blob
 * @param	nodeoffset	offset of the tree node
 * @param[in]	vendor		name of vendor to check
 *
 * returns:
 *	0, if the node has a compatible vendor string property
 *	1, if the node does not contain the vendor string property
 *	-FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
 *	-FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
 *	-FDT_ERR_BADMAGIC,
 *	-FDT_ERR_BADVERSION,
 *	-FDT_BADSTATE,
 *	-FDT_ERR_BADSTRUCTURE, standard meanings
 */
int octeon_fdt_compat_vendor(const void *fdt, int nodeoffset, const char *vendor)
{
	const char *strlist;
	const char *p;
	int len;
	int listlen;

	strlist = fdt_getprop(fdt, nodeoffset, "compatible", &listlen);
	if (!strlist)
		return listlen;

	len = strlen(vendor);

	debug("%s(%p, %d, %s (%p)) strlist: %s (%p), len: %d\n", __func__, fdt, nodeoffset, vendor,
	      vendor, strlist, strlist, len);
	while (listlen >= len) {
		debug("  Comparing %d bytes of %s and %s\n", len, vendor, strlist);
		if ((memcmp(vendor, strlist, len) == 0) &&
		    ((strlist[len] == ',') || (strlist[len] == '\0')))
			return 0;
		p = memchr(strlist, '\0', listlen);
		if (!p)
			return 1; /* malformed strlist.. */
		listlen -= (p - strlist) + 1;
		strlist = p + 1;
	}
	return 1;
}

/**
 * Given a node in the device tree get the OCTEON OCX node number
 *
 * @param fdt		pointer to flat device tree
 * @param nodeoffset	node offset to get OCX node for
 *
 * @return the Octeon OCX node number
 */
int octeon_fdt_get_soc_node(const void *fdt, int nodeoffset)
{
	return 0;
}

/**
 * Given a FDT node, check if it is compatible with a list of devices
 *
 * @param[in]	fdt		Flat device tree pointer
 * @param	node_offset	Node offset in device tree
 * @param[in]	strlist		Array of FDT devices to check, end must be NULL
 *
 * @return	0 if at least one device is compatible, 1 if not compatible.
 */
int octeon_fdt_node_check_compatible(const void *fdt, int node_offset,
				     const char *const *strlist)
{
	while (*strlist && **strlist) {
		debug("%s: Checking %s\n", __func__, *strlist);
		if (!fdt_node_check_compatible(fdt, node_offset, *strlist)) {
			debug("%s: match found\n", __func__);
			return 0;
		}
		strlist++;
	}
	debug("%s: No match found\n", __func__);
	return 1;
}

/**
 * Given a node offset, find the i2c bus number for that node
 *
 * @param[in]	fdt	Pointer to flat device tree
 * @param	node_offset	Node offset in device tree
 *
 * @return	i2c bus number or -1 if error
 */
int octeon_fdt_i2c_get_bus(const void *fdt, int node_offset)
{
	const char *compat;
	const u64 addresses[] = { 0x1180000001000, 0x1180000001200 };
	u64 reg;
	int i;
	int bus = -1;
	bool found = false;

	if (octeon_has_feature(OCTEON_FEATURE_CIU3))
		compat = "cavium,octeon-7890-twsi";
	else
		compat = "cavium,octeon-3860-twsi";

	while (node_offset > 0 &&
	       !(found = !fdt_node_check_compatible(fdt, node_offset, compat))) {
		node_offset = fdt_parent_offset(fdt, node_offset);
#ifdef CONFIG_OCTEON_I2C_FDT
		bus = i2c_get_bus_num_fdt(node_offset);
		if (bus >= 0) {
			debug("%s: Found bus 0x%x\n", __func__, bus);
			return bus;
		}
#endif
	}
	if (!found) {
		printf("Error: node %d in device tree is not a child of the I2C bus\n",
		       node_offset);
		return -1;
	}

	reg = fdtdec_get_addr(fdt, node_offset, "reg");
	if (reg == FDT_ADDR_T_NONE) {
		printf("%s: Error: invalid reg address for TWSI bus\n", __func__);
		return -1;
	}

	for (i = 0; i < ARRAY_SIZE(addresses); i++)
		if (reg == addresses[i]) {
			bus = i;
			break;
		}

	debug("%s: bus 0x%x\n", __func__, bus);
	return bus;
}

/**
 * Given an offset into the fdt, output the i2c bus and address of the device
 *
 * @param[in]	fdt	fdt blob pointer
 * @param	node	offset in FDT of device
 * @param[out]	bus	i2c bus number of device
 * @param[out]	addr	address of device on i2c bus
 *
 * @return	0 for success, -1 on error
 */
int octeon_fdt_get_i2c_bus_addr(const void *fdt, int node, int *bus, int *addr)
{
	*bus = octeon_fdt_i2c_get_bus(fdt, fdt_parent_offset(fdt, node));
	if (*bus < 0) {
		printf("%s: Could not get parent i2c bus\n", __func__);
		return -1;
	}
	*addr = fdtdec_get_int(fdt, node, "reg", -1);
	if (*addr < 0)
		return -1;
	return 0;
}

/**
 * Reads a GPIO pin given the node of the GPIO device in the device tree and
 * the pin number.
 *
 * @param[in]	fdt	fdt blob pointer
 * @param	phandle	phandle of GPIO node
 * @param	pin	pin number to read
 *
 * @return	0 = pin is low, 1 = pin is high, -1 = error
 */
int octeon_fdt_read_gpio(const void *fdt, int phandle, int pin)
{
	enum cvmx_gpio_type type;
	__maybe_unused int node;
	__maybe_unused int addr;
	__maybe_unused int bus;
	__maybe_unused int old_bus;
	int num_pins;
	int value;

	type = cvmx_fdt_get_gpio_type(fdt, phandle, &num_pins);
	if ((pin & 0xff) >= num_pins) {
		debug("%s: pin number %d out of range\n", __func__, pin);
		return -1;
	}
	switch (type) {
#ifdef CONFIG_PCA953X
	case CVMX_GPIO_PIN_PCA953X:
		node = fdt_node_offset_by_phandle(fdt, phandle);
		if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
			printf("%s: Could not get gpio bus and/or address\n", __func__);
			return -1;
		}
		value = pca953x_get_val(bus, addr);
		if (value < 0) {
			printf("%s: Error reading PCA953X GPIO at 0x%x:0x%x\n", __func__, bus,
			       addr);
			return -1;
		}
		value = (value >> pin) & 1;
		break;
#endif
#ifdef CONFIG_PCF857X
	case CVMX_GPIO_PIN_PCF857X:
		node = fdt_node_offset_by_phandle(fdt, phandle);
		if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
			printf("%s: Could not get gpio bus and/or address\n", __func__);
			return -1;
		}
		value = pcf857x_get_val(bus, addr);
		if (value < 0) {
			printf("%s: Error reading PCF857X GPIO at 0x%x:0x%x\n", __func__, bus,
			       addr);
			return -1;
		}
		value = (value >> pin) & 1;
		break;
#endif
#ifdef CONFIG_PCA9698
	case CVMX_GPIO_PIN_PCA9698:
		node = fdt_node_offset_by_phandle(fdt, phandle);
		if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
			printf("%s: Could not get gpio bus and/or address\n", __func__);
			return -1;
		}
		old_bus = i2c_get_bus_num();
		i2c_set_bus_num(bus);
		value = pca9698_get_value(addr, pin);
		i2c_set_bus_num(old_bus);
		break;
#endif
	case CVMX_GPIO_PIN_OCTEON:
		value = gpio_get_value(pin);
		break;
	default:
		printf("%s: Unknown GPIO type %d\n", __func__, type);
		return -1;
	}
	return value;
}

/**
 * Reads a GPIO pin given the node of the GPIO device in the device tree and
 * the pin number.
 *
 * @param[in]	fdt	fdt blob pointer
 * @param	phandle	phandle of GPIO node
 * @param	pin	pin number to read
 * @param	val	value to write (1 = high, 0 = low)
 *
 * @return	0 = success, -1 = error
 */
int octeon_fdt_set_gpio(const void *fdt, int phandle, int pin, int val)
{
	enum cvmx_gpio_type type;
	int node;
	int num_pins;
	__maybe_unused int addr;
	__maybe_unused int bus;
	__maybe_unused int old_bus;
	__maybe_unused int rc;

	node = fdt_node_offset_by_phandle(fdt, phandle);
	if (node < 0) {
		printf("%s: Invalid phandle\n", __func__);
		return -1;
	}

	type = cvmx_fdt_get_gpio_type(fdt, phandle, &num_pins);
	if ((pin & 0xff) >= num_pins) {
		debug("%s: pin number %d out of range\n", __func__, pin);
		return -1;
	}
	switch (type) {
#ifdef CONFIG_PCA953X
	case CVMX_GPIO_PIN_PCA953X:
		if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
			printf("%s: Could not get gpio bus and/or address\n", __func__);
			return -1;
		}

		return pca953x_set_val(bus, addr, 1 << pin, val << pin);
#endif
#ifdef CONFIG_PCF857X
	case CVMX_GPIO_PIN_PCF857X:
		if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
			printf("%s: Could not get gpio bus and/or address\n", __func__);
			return -1;
		}
		return pcf957x_set_val(bus, addr, 1 << pin, val << pin);
#endif
#ifdef CONFIG_PCA9698
	case CVMX_GPIO_PIN_PCA9698:
		if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
			printf("%s: Could not get gpio bus and/or address\n", __func__);
			return -1;
		}
		old_bus = i2c_get_bus_num();
		i2c_set_bus_num(bus);
		rc = pca9698_set_value(addr, pin, val);
		i2c_set_bus_num(old_bus);
		return rc;
#endif
	case CVMX_GPIO_PIN_OCTEON:
		return gpio_set_value(pin, val);
	default:
		printf("%s: Unknown GPIO type %d\n", __func__, type);
		return -1;
	}
}

/**
 * Given the node of a GPIO entry output the GPIO type, i2c bus and i2c
 * address.
 *
 * @param	fdt_node	node of GPIO in device tree, generally
 *				derived from a phandle.
 * @param[out]	type		Type of GPIO detected
 * @param[out]	i2c_bus		For i2c GPIO expanders, the i2c bus number
 * @param[out]	i2c_addr	For i2c GPIO expanders, the i2c address
 *
 * @return	0 for success, -1 for errors
 *
 * NOTE: It is up to the caller to determine the pin number.
 */
int octeon_fdt_get_gpio_info(int fdt_node, enum octeon_gpio_type *type,
			     int *i2c_bus, int *i2c_addr)
{
	const void *fdt = gd->fdt_blob;

	int i2c_bus_node __attribute__((unused));

	*type = GPIO_TYPE_UNKNOWN;

	if (!octeon_fdt_node_check_compatible(fdt, fdt_node, octeon_gpio_list)) {
		debug("%s: Found Octeon compatible GPIO\n", __func__);
		*type = GPIO_TYPE_OCTEON;
		if (i2c_bus)
			*i2c_bus = -1;
		if (i2c_addr)
			*i2c_addr = -1;
		return 0;
	}
#ifdef CONFIG_PCA9555
	if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca9555_gpio_list)) {
		debug("%s: Found PCA9555 type compatible GPIO\n", __func__);
		*type = GPIO_TYPE_PCA9555;
	}
#endif
#ifdef CONFIG_PCA9554
	if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca9554_gpio_list)) {
		debug("%s: Found PCA9555 type compatible GPIO\n", __func__);
		*type = GPIO_TYPE_PCA9554;
	}
#endif
#ifdef CONFIG_PCA953X
	if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca953x_gpio_list)) {
		debug("%s: Found PCA953x compatible GPIO", __func__);
		*type = GPIO_TYPE_PCA953X;
	}
#endif
#ifdef CONFIG_PCA9698
	if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca9698_gpio_list)) {
		debug("%s: Found PCA9698 compatible GPIO", __func__);
		*type = GPIO_TYPE_PCA9698;
	}
#endif
#if defined(CONFIG_PCA953X) || defined(CONFIG_PCA9698) || \
	defined(CONFIG_PCA9555) || defined(CONFIG_PCA9554)
	if (!i2c_addr || !i2c_bus) {
		printf("%s: Error: i2c_addr or i2c_bus is NULL\n", __func__);
		return -1;
	}

	*i2c_addr = fdtdec_get_int(fdt, fdt_node, "reg", -1);
	i2c_bus_node = fdt_parent_offset(fdt, fdt_node);
	if (i2c_bus_node < 0) {
		printf("%s: Invalid parent\n", __func__);
		return -1;
	}
	*i2c_bus = i2c_get_bus_num_fdt(i2c_bus_node);
#endif
	return (*type != GPIO_TYPE_UNKNOWN) ? 0 : -1;
}

#ifdef CONFIG_PHY_VITESSE
/**
 * Given a node in the flat device tree, return the matching PHY device
 *
 * @param	fdt_node	FDT node in device tree
 *
 * @return	pointer to PHY device or NULL if none found.
 */
static struct phy_device *octeon_fdt_get_phy_device_from_node(int fdt_node)
{
	struct eth_device *dev;
	int i = 0;
	struct octeon_eth_info *ethinfo = NULL;

	do {
		dev = eth_get_dev_by_index(i++);
		if (!dev)
			return NULL;
		ethinfo = dev->priv;
		if (ethinfo->phy_offset == fdt_node)
			return ethinfo->phydev;
	} while (dev);
	return NULL;
}
#endif

/**
 * Get the PHY data structure for the specified FDT node and output the type
 *
 * @param	fdt_node	FDT node of phy
 * @param[out]	type		Type of GPIO
 *
 * @return	pointer to phy device or NULL if no match found.
 */
struct phy_device *octeon_fdt_get_phy_gpio_info(int fdt_node, enum octeon_gpio_type *type)
{
#ifdef CONFIG_PHY_VITESSE
	struct phy_device *phydev;

	if (!octeon_fdt_node_check_compatible(gd->fdt_blob, fdt_node,
					      vitesse_vsc8488_gpio_list)) {
		phydev = octeon_fdt_get_phy_device_from_node(fdt_node);
		if (phydev) {
			debug("%s: Found Vitesse VSC848X compatible GPIO\n", __func__);
			*type = GPIO_TYPE_VSC8488;
			return phydev;
		}

		debug("%s: Error: phy device not found!\n", __func__);
		return NULL;
	}

	debug("%s: No compatible Vitesse PHY type found\n", __func__);
#endif
	return NULL;
}