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_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
43 DECLARE_GLOBAL_DATA_PTR;
44
45 #ifdef CONFIG_PCA9554
46 static const char * const pca9554_gpio_list[] = {
47 "pca9554",
48 "nxp,pca9554",
49 "ti,pca9554",
50 NULL,
51 };
52 #endif
53
54 #ifdef CONFIG_PCA9555
55 static 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 */
64 static 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 */
71 static 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
82 static 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 */
90 static 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 *
124 * @return 0 for success.
125 */
__octeon_fdt_patch(void * fdt,const char * fdt_key,const char * trim_name)126 int __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
133 int 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 *
167 * @return 0 for success.
168 */
169 int 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
__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)175 int __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
octeon_set_one_fdt_mac(int node,uint64_t * mac)322 static 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
convert_mac(const u8 mac_addr[6])339 static 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 */
355 void octeon_fixup_fdt_mac_addr(void) __attribute__((weak, alias("__octeon_fixup_fdt_mac_addr")));
356
__octeon_fixup_fdt_mac_addr(void)357 void __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. */
427 node = fdt_node_offset_by_compatible(working_fdt, -1, "cavium,octeon-7890-bgx-port");
428 while (node != -FDT_ERR_NOTFOUND) {
429 octeon_set_one_fdt_mac(node, &mac);
430 node = fdt_node_offset_by_compatible(working_fdt, node,
431 "cavium,octeon-7890-bgx-port");
432 }
433 }
434 #endif
435
436 /**
437 * This function fixes the clock-frequency in the flat device tree for the UART.
438 *
439 * NOTE: This function is weak and an alias for __octeon_fixup_fdt_uart.
440 */
441 void octeon_fixup_fdt_uart(void) __attribute__((weak, alias("__octeon_fixup_fdt_uart")));
442
__octeon_fixup_fdt_uart(void)443 void __octeon_fixup_fdt_uart(void)
444 {
445 u32 clk;
446 int node;
447
448 clk = gd->bus_clk;
449
450 /* Device trees already have good values for fast simulator
451 * output, real boards need the correct value.
452 */
453 node = fdt_node_offset_by_compatible(working_fdt, -1, "cavium,octeon-3860-uart");
454 while (node != -FDT_ERR_NOTFOUND) {
455 fdt_setprop_inplace_cell(working_fdt, node, "clock-frequency", clk);
456 node = fdt_node_offset_by_compatible(working_fdt, node, "cavium,octeon-3860-uart");
457 }
458 }
459
460 /**
461 * This function fills in the /memory portion of the flat device tree.
462 *
463 * NOTE: This function is weak and aliased to __octeon_fixup_fdt_memory.
464 */
465 void octeon_fixup_fdt_memory(void) __attribute__((weak, alias("__octeon_fixup_fdt_memory")));
466
__octeon_fixup_fdt_memory(void)467 void __octeon_fixup_fdt_memory(void)
468 {
469 u64 sizes[3], addresses[3];
470 u64 size_left = gd->ram_size;
471 int num_addresses = 0;
472 int rc;
473 int node;
474
475 size_left = gd->ram_size;
476 sizes[num_addresses] = min_t(u64, size_left, 256 * 1024 * 1024);
477 size_left -= sizes[num_addresses];
478 addresses[num_addresses] = 0;
479 num_addresses++;
480
481 if (size_left > 0) {
482 sizes[num_addresses] = size_left;
483 addresses[num_addresses] = 0x20000000ULL;
484 num_addresses++;
485 }
486
487 node = fdt_path_offset(working_fdt, "/memory");
488 if (node < 0)
489 node = fdt_add_subnode(working_fdt, fdt_path_offset(working_fdt, "/"), "memory");
490 if (node < 0) {
491 printf("Could not add memory section to fdt: %s\n", fdt_strerror(node));
492 return;
493 }
494 rc = fdt_fixup_memory_banks(working_fdt, addresses, sizes, num_addresses);
495 if (rc != 0)
496 printf("%s: fdt_fixup_memory_banks returned %d when adding %d addresses\n",
497 __func__, rc, num_addresses);
498 }
499
500 void octeon_fixup_fdt(void) __attribute__((weak, alias("__octeon_fixup_fdt")));
501
__octeon_fixup_fdt(void)502 void __octeon_fixup_fdt(void)
503 {
504 if (!working_fdt)
505 return;
506
507 #ifdef CONFIG_CMD_NET
508 octeon_fixup_fdt_mac_addr();
509 #endif /* CONFIG_CMD_NET */
510
511 #if !CONFIG_OCTEON_SIM_SPEED
512 octeon_fixup_fdt_uart();
513 #endif
514
515 octeon_fixup_fdt_memory();
516 }
517
__board_fixup_fdt(void)518 int __board_fixup_fdt(void)
519 {
520 /*
521 * Nothing to do in this dummy implementation
522 */
523 return 0;
524 }
525
526 int board_fixup_fdt(void) __attribute__((weak, alias("__board_fixup_fdt")));
527
528 /**
529 * This is a helper function to find the offset of a PHY device given
530 * an Ethernet device.
531 *
532 * @param[in] eth - Ethernet device to search for PHY offset
533 *
534 * @returns offset of phy info in device tree or -1 if not found
535 */
536 //int octeon_fdt_find_phy(const struct eth_device *eth)
octeon_fdt_find_phy(const struct udevice * eth)537 int octeon_fdt_find_phy(const struct udevice *eth)
538 {
539 int aliases;
540 const void *fdt = gd->fdt_blob;
541 const char *pip_path;
542 int pip;
543 char buffer[64];
544 #if 0
545 struct octeon_eth_info *oct_eth_info =
546 (struct octeon_eth_info *)eth->priv;
547 #else
548 struct octeon_eth_info *oct_eth_info = dev_get_priv(eth);
549 #endif
550 int interface, index;
551 int phandle;
552 int phy;
553 u32 *phy_handle;
554
555 aliases = fdt_path_offset(fdt, "/aliases");
556 if (aliases < 0) {
557 puts("/aliases not found in device tree!\n");
558 return -1;
559 }
560 pip_path = fdt_getprop(fdt, aliases, "pip", NULL);
561 if (!pip_path) {
562 puts("pip not found in aliases in device tree\n");
563 return -1;
564 }
565 pip = fdt_path_offset(fdt, pip_path);
566 if (pip < 0) {
567 puts("pip not found in device tree\n");
568 return -1;
569 }
570 snprintf(buffer, sizeof(buffer), "interface@%d", oct_eth_info->interface);
571 interface = fdt_subnode_offset(fdt, pip, buffer);
572 if (interface < 0) {
573 printf("%s: interface@%d not found in device tree for %s\n", __func__,
574 oct_eth_info->interface, eth->name);
575 return -1;
576 }
577 snprintf(buffer, sizeof(buffer), "ethernet@%x", oct_eth_info->index);
578 index = fdt_subnode_offset(fdt, interface, buffer);
579 if (index < 0) {
580 printf("%s: ethernet@%x not found in device tree for %s\n", __func__,
581 oct_eth_info->index, eth->name);
582 return -1;
583 }
584 phy_handle = (uint32_t *)fdt_getprop(fdt, index, "phy-handle", NULL);
585 if (phy_handle < 0) {
586 printf("%s: phy-handle not found for %s\n", __func__, eth->name);
587 return -1;
588 }
589 phandle = fdt32_to_cpu(*phy_handle);
590 phy = fdt_node_offset_by_phandle(fdt, phandle);
591 if (phy < 0) {
592 printf("%s: phy not found for %s\n", __func__, eth->name);
593 return -1;
594 }
595
596 return phy;
597 }
598
599 /**
600 * This helper function returns if a node contains the specified vendor name.
601 *
602 * @param[in] fdt pointer to device tree blob
603 * @param nodeoffset offset of the tree node
604 * @param[in] vendor name of vendor to check
605 *
606 * returns:
607 * 0, if the node has a compatible vendor string property
608 * 1, if the node does not contain the vendor string property
609 * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
610 * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
611 * -FDT_ERR_BADMAGIC,
612 * -FDT_ERR_BADVERSION,
613 * -FDT_BADSTATE,
614 * -FDT_ERR_BADSTRUCTURE, standard meanings
615 */
octeon_fdt_compat_vendor(const void * fdt,int nodeoffset,const char * vendor)616 int octeon_fdt_compat_vendor(const void *fdt, int nodeoffset, const char *vendor)
617 {
618 const char *strlist;
619 const char *p;
620 int len;
621 int listlen;
622
623 strlist = fdt_getprop(fdt, nodeoffset, "compatible", &listlen);
624 if (!strlist)
625 return listlen;
626
627 len = strlen(vendor);
628
629 debug("%s(%p, %d, %s (%p)) strlist: %s (%p), len: %d\n", __func__, fdt, nodeoffset, vendor,
630 vendor, strlist, strlist, len);
631 while (listlen >= len) {
632 debug(" Comparing %d bytes of %s and %s\n", len, vendor, strlist);
633 if ((memcmp(vendor, strlist, len) == 0) &&
634 ((strlist[len] == ',') || (strlist[len] == '\0')))
635 return 0;
636 p = memchr(strlist, '\0', listlen);
637 if (!p)
638 return 1; /* malformed strlist.. */
639 listlen -= (p - strlist) + 1;
640 strlist = p + 1;
641 }
642 return 1;
643 }
644
645 /**
646 * Given a node in the device tree get the OCTEON OCX node number
647 *
648 * @param fdt pointer to flat device tree
649 * @param nodeoffset node offset to get OCX node for
650 *
651 * @return the Octeon OCX node number
652 */
octeon_fdt_get_soc_node(const void * fdt,int nodeoffset)653 int octeon_fdt_get_soc_node(const void *fdt, int nodeoffset)
654 {
655 return 0;
656 }
657
658 /**
659 * Given a FDT node, check if it is compatible with a list of devices
660 *
661 * @param[in] fdt Flat device tree pointer
662 * @param node_offset Node offset in device tree
663 * @param[in] strlist Array of FDT devices to check, end must be NULL
664 *
665 * @return 0 if at least one device is compatible, 1 if not compatible.
666 */
octeon_fdt_node_check_compatible(const void * fdt,int node_offset,const char * const * strlist)667 int octeon_fdt_node_check_compatible(const void *fdt, int node_offset,
668 const char *const *strlist)
669 {
670 while (*strlist && **strlist) {
671 debug("%s: Checking %s\n", __func__, *strlist);
672 if (!fdt_node_check_compatible(fdt, node_offset, *strlist)) {
673 debug("%s: match found\n", __func__);
674 return 0;
675 }
676 strlist++;
677 }
678 debug("%s: No match found\n", __func__);
679 return 1;
680 }
681
682 /**
683 * Given a node offset, find the i2c bus number for that node
684 *
685 * @param[in] fdt Pointer to flat device tree
686 * @param node_offset Node offset in device tree
687 *
688 * @return i2c bus number or -1 if error
689 */
octeon_fdt_i2c_get_bus(const void * fdt,int node_offset)690 int octeon_fdt_i2c_get_bus(const void *fdt, int node_offset)
691 {
692 const char *compat;
693 const u64 addresses[] = { 0x1180000001000, 0x1180000001200 };
694 u64 reg;
695 int i;
696 int bus = -1;
697 bool found = false;
698
699 if (octeon_has_feature(OCTEON_FEATURE_CIU3))
700 compat = "cavium,octeon-7890-twsi";
701 else
702 compat = "cavium,octeon-3860-twsi";
703
704 while (node_offset > 0 &&
705 !(found = !fdt_node_check_compatible(fdt, node_offset, compat))) {
706 node_offset = fdt_parent_offset(fdt, node_offset);
707 #ifdef CONFIG_OCTEON_I2C_FDT
708 bus = i2c_get_bus_num_fdt(node_offset);
709 if (bus >= 0) {
710 debug("%s: Found bus 0x%x\n", __func__, bus);
711 return bus;
712 }
713 #endif
714 }
715 if (!found) {
716 printf("Error: node %d in device tree is not a child of the I2C bus\n",
717 node_offset);
718 return -1;
719 }
720
721 reg = fdtdec_get_addr(fdt, node_offset, "reg");
722 if (reg == FDT_ADDR_T_NONE) {
723 printf("%s: Error: invalid reg address for TWSI bus\n", __func__);
724 return -1;
725 }
726
727 for (i = 0; i < ARRAY_SIZE(addresses); i++)
728 if (reg == addresses[i]) {
729 bus = i;
730 break;
731 }
732
733 debug("%s: bus 0x%x\n", __func__, bus);
734 return bus;
735 }
736
737 /**
738 * Given an offset into the fdt, output the i2c bus and address of the device
739 *
740 * @param[in] fdt fdt blob pointer
741 * @param node offset in FDT of device
742 * @param[out] bus i2c bus number of device
743 * @param[out] addr address of device on i2c bus
744 *
745 * @return 0 for success, -1 on error
746 */
octeon_fdt_get_i2c_bus_addr(const void * fdt,int node,int * bus,int * addr)747 int octeon_fdt_get_i2c_bus_addr(const void *fdt, int node, int *bus, int *addr)
748 {
749 *bus = octeon_fdt_i2c_get_bus(fdt, fdt_parent_offset(fdt, node));
750 if (*bus < 0) {
751 printf("%s: Could not get parent i2c bus\n", __func__);
752 return -1;
753 }
754 *addr = fdtdec_get_int(fdt, node, "reg", -1);
755 if (*addr < 0)
756 return -1;
757 return 0;
758 }
759
760 /**
761 * Reads a GPIO pin given the node of the GPIO device in the device tree and
762 * the pin number.
763 *
764 * @param[in] fdt fdt blob pointer
765 * @param phandle phandle of GPIO node
766 * @param pin pin number to read
767 *
768 * @return 0 = pin is low, 1 = pin is high, -1 = error
769 */
octeon_fdt_read_gpio(const void * fdt,int phandle,int pin)770 int octeon_fdt_read_gpio(const void *fdt, int phandle, int pin)
771 {
772 enum cvmx_gpio_type type;
773 __maybe_unused int node;
774 __maybe_unused int addr;
775 __maybe_unused int bus;
776 __maybe_unused int old_bus;
777 int num_pins;
778 int value;
779
780 type = cvmx_fdt_get_gpio_type(fdt, phandle, &num_pins);
781 if ((pin & 0xff) >= num_pins) {
782 debug("%s: pin number %d out of range\n", __func__, pin);
783 return -1;
784 }
785 switch (type) {
786 #ifdef CONFIG_PCA953X
787 case CVMX_GPIO_PIN_PCA953X:
788 node = fdt_node_offset_by_phandle(fdt, phandle);
789 if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
790 printf("%s: Could not get gpio bus and/or address\n", __func__);
791 return -1;
792 }
793 value = pca953x_get_val(bus, addr);
794 if (value < 0) {
795 printf("%s: Error reading PCA953X GPIO at 0x%x:0x%x\n", __func__, bus,
796 addr);
797 return -1;
798 }
799 value = (value >> pin) & 1;
800 break;
801 #endif
802 #ifdef CONFIG_PCF857X
803 case CVMX_GPIO_PIN_PCF857X:
804 node = fdt_node_offset_by_phandle(fdt, phandle);
805 if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
806 printf("%s: Could not get gpio bus and/or address\n", __func__);
807 return -1;
808 }
809 value = pcf857x_get_val(bus, addr);
810 if (value < 0) {
811 printf("%s: Error reading PCF857X GPIO at 0x%x:0x%x\n", __func__, bus,
812 addr);
813 return -1;
814 }
815 value = (value >> pin) & 1;
816 break;
817 #endif
818 #ifdef CONFIG_PCA9698
819 case CVMX_GPIO_PIN_PCA9698:
820 node = fdt_node_offset_by_phandle(fdt, phandle);
821 if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
822 printf("%s: Could not get gpio bus and/or address\n", __func__);
823 return -1;
824 }
825 old_bus = i2c_get_bus_num();
826 i2c_set_bus_num(bus);
827 value = pca9698_get_value(addr, pin);
828 i2c_set_bus_num(old_bus);
829 break;
830 #endif
831 case CVMX_GPIO_PIN_OCTEON:
832 value = gpio_get_value(pin);
833 break;
834 default:
835 printf("%s: Unknown GPIO type %d\n", __func__, type);
836 return -1;
837 }
838 return value;
839 }
840
841 /**
842 * Reads a GPIO pin given the node of the GPIO device in the device tree and
843 * the pin number.
844 *
845 * @param[in] fdt fdt blob pointer
846 * @param phandle phandle of GPIO node
847 * @param pin pin number to read
848 * @param val value to write (1 = high, 0 = low)
849 *
850 * @return 0 = success, -1 = error
851 */
octeon_fdt_set_gpio(const void * fdt,int phandle,int pin,int val)852 int octeon_fdt_set_gpio(const void *fdt, int phandle, int pin, int val)
853 {
854 enum cvmx_gpio_type type;
855 int node;
856 int num_pins;
857 __maybe_unused int addr;
858 __maybe_unused int bus;
859 __maybe_unused int old_bus;
860 __maybe_unused int rc;
861
862 node = fdt_node_offset_by_phandle(fdt, phandle);
863 if (node < 0) {
864 printf("%s: Invalid phandle\n", __func__);
865 return -1;
866 }
867
868 type = cvmx_fdt_get_gpio_type(fdt, phandle, &num_pins);
869 if ((pin & 0xff) >= num_pins) {
870 debug("%s: pin number %d out of range\n", __func__, pin);
871 return -1;
872 }
873 switch (type) {
874 #ifdef CONFIG_PCA953X
875 case CVMX_GPIO_PIN_PCA953X:
876 if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
877 printf("%s: Could not get gpio bus and/or address\n", __func__);
878 return -1;
879 }
880
881 return pca953x_set_val(bus, addr, 1 << pin, val << pin);
882 #endif
883 #ifdef CONFIG_PCF857X
884 case CVMX_GPIO_PIN_PCF857X:
885 if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
886 printf("%s: Could not get gpio bus and/or address\n", __func__);
887 return -1;
888 }
889 return pcf957x_set_val(bus, addr, 1 << pin, val << pin);
890 #endif
891 #ifdef CONFIG_PCA9698
892 case CVMX_GPIO_PIN_PCA9698:
893 if (octeon_fdt_get_i2c_bus_addr(fdt, node, &bus, &addr)) {
894 printf("%s: Could not get gpio bus and/or address\n", __func__);
895 return -1;
896 }
897 old_bus = i2c_get_bus_num();
898 i2c_set_bus_num(bus);
899 rc = pca9698_set_value(addr, pin, val);
900 i2c_set_bus_num(old_bus);
901 return rc;
902 #endif
903 case CVMX_GPIO_PIN_OCTEON:
904 return gpio_set_value(pin, val);
905 default:
906 printf("%s: Unknown GPIO type %d\n", __func__, type);
907 return -1;
908 }
909 }
910
911 /**
912 * Given the node of a GPIO entry output the GPIO type, i2c bus and i2c
913 * address.
914 *
915 * @param fdt_node node of GPIO in device tree, generally
916 * derived from a phandle.
917 * @param[out] type Type of GPIO detected
918 * @param[out] i2c_bus For i2c GPIO expanders, the i2c bus number
919 * @param[out] i2c_addr For i2c GPIO expanders, the i2c address
920 *
921 * @return 0 for success, -1 for errors
922 *
923 * NOTE: It is up to the caller to determine the pin number.
924 */
octeon_fdt_get_gpio_info(int fdt_node,enum octeon_gpio_type * type,int * i2c_bus,int * i2c_addr)925 int octeon_fdt_get_gpio_info(int fdt_node, enum octeon_gpio_type *type,
926 int *i2c_bus, int *i2c_addr)
927 {
928 const void *fdt = gd->fdt_blob;
929
930 int i2c_bus_node __attribute__((unused));
931
932 *type = GPIO_TYPE_UNKNOWN;
933
934 if (!octeon_fdt_node_check_compatible(fdt, fdt_node, octeon_gpio_list)) {
935 debug("%s: Found Octeon compatible GPIO\n", __func__);
936 *type = GPIO_TYPE_OCTEON;
937 if (i2c_bus)
938 *i2c_bus = -1;
939 if (i2c_addr)
940 *i2c_addr = -1;
941 return 0;
942 }
943 #ifdef CONFIG_PCA9555
944 if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca9555_gpio_list)) {
945 debug("%s: Found PCA9555 type compatible GPIO\n", __func__);
946 *type = GPIO_TYPE_PCA9555;
947 }
948 #endif
949 #ifdef CONFIG_PCA9554
950 if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca9554_gpio_list)) {
951 debug("%s: Found PCA9555 type compatible GPIO\n", __func__);
952 *type = GPIO_TYPE_PCA9554;
953 }
954 #endif
955 #ifdef CONFIG_PCA953X
956 if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca953x_gpio_list)) {
957 debug("%s: Found PCA953x compatible GPIO", __func__);
958 *type = GPIO_TYPE_PCA953X;
959 }
960 #endif
961 #ifdef CONFIG_PCA9698
962 if (!octeon_fdt_node_check_compatible(fdt, fdt_node, pca9698_gpio_list)) {
963 debug("%s: Found PCA9698 compatible GPIO", __func__);
964 *type = GPIO_TYPE_PCA9698;
965 }
966 #endif
967 #if defined(CONFIG_PCA953X) || defined(CONFIG_PCA9698) || \
968 defined(CONFIG_PCA9555) || defined(CONFIG_PCA9554)
969 if (!i2c_addr || !i2c_bus) {
970 printf("%s: Error: i2c_addr or i2c_bus is NULL\n", __func__);
971 return -1;
972 }
973
974 *i2c_addr = fdtdec_get_int(fdt, fdt_node, "reg", -1);
975 i2c_bus_node = fdt_parent_offset(fdt, fdt_node);
976 if (i2c_bus_node < 0) {
977 printf("%s: Invalid parent\n", __func__);
978 return -1;
979 }
980 *i2c_bus = i2c_get_bus_num_fdt(i2c_bus_node);
981 #endif
982 return (*type != GPIO_TYPE_UNKNOWN) ? 0 : -1;
983 }
984
985 #ifdef CONFIG_PHY_VITESSE
986 /**
987 * Given a node in the flat device tree, return the matching PHY device
988 *
989 * @param fdt_node FDT node in device tree
990 *
991 * @return pointer to PHY device or NULL if none found.
992 */
octeon_fdt_get_phy_device_from_node(int fdt_node)993 static struct phy_device *octeon_fdt_get_phy_device_from_node(int fdt_node)
994 {
995 struct eth_device *dev;
996 int i = 0;
997 struct octeon_eth_info *ethinfo = NULL;
998
999 do {
1000 dev = eth_get_dev_by_index(i++);
1001 if (!dev)
1002 return NULL;
1003 ethinfo = dev->priv;
1004 if (ethinfo->phy_offset == fdt_node)
1005 return ethinfo->phydev;
1006 } while (dev);
1007 return NULL;
1008 }
1009 #endif
1010
1011 /**
1012 * Get the PHY data structure for the specified FDT node and output the type
1013 *
1014 * @param fdt_node FDT node of phy
1015 * @param[out] type Type of GPIO
1016 *
1017 * @return pointer to phy device or NULL if no match found.
1018 */
octeon_fdt_get_phy_gpio_info(int fdt_node,enum octeon_gpio_type * type)1019 struct phy_device *octeon_fdt_get_phy_gpio_info(int fdt_node, enum octeon_gpio_type *type)
1020 {
1021 #ifdef CONFIG_PHY_VITESSE
1022 struct phy_device *phydev;
1023
1024 if (!octeon_fdt_node_check_compatible(gd->fdt_blob, fdt_node,
1025 vitesse_vsc8488_gpio_list)) {
1026 phydev = octeon_fdt_get_phy_device_from_node(fdt_node);
1027 if (phydev) {
1028 debug("%s: Found Vitesse VSC848X compatible GPIO\n", __func__);
1029 *type = GPIO_TYPE_VSC8488;
1030 return phydev;
1031 }
1032
1033 debug("%s: Error: phy device not found!\n", __func__);
1034 return NULL;
1035 }
1036
1037 debug("%s: No compatible Vitesse PHY type found\n", __func__);
1038 #endif
1039 return NULL;
1040 }
1041