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