1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2fec60470SScott Wood /*
3fec60470SScott Wood * PlanetCore configuration data support functions
4fec60470SScott Wood *
5fec60470SScott Wood * Author: Scott Wood <scottwood@freescale.com>
6fec60470SScott Wood *
7fec60470SScott Wood * Copyright (c) 2007 Freescale Semiconductor, Inc.
8fec60470SScott Wood */
9fec60470SScott Wood
10fec60470SScott Wood #include "stdio.h"
11fec60470SScott Wood #include "stdlib.h"
12fec60470SScott Wood #include "ops.h"
13fec60470SScott Wood #include "planetcore.h"
14fec60470SScott Wood #include "io.h"
15fec60470SScott Wood
16fec60470SScott Wood /* PlanetCore passes information to the OS in the form of
17fec60470SScott Wood * a table of key=value strings, separated by newlines.
18fec60470SScott Wood *
19fec60470SScott Wood * The list is terminated by an empty string (i.e. two
20fec60470SScott Wood * consecutive newlines).
21fec60470SScott Wood *
22fec60470SScott Wood * To make it easier to parse, we first convert all the
23fec60470SScott Wood * newlines into null bytes.
24fec60470SScott Wood */
25fec60470SScott Wood
planetcore_prepare_table(char * table)26fec60470SScott Wood void planetcore_prepare_table(char *table)
27fec60470SScott Wood {
28fec60470SScott Wood do {
29fec60470SScott Wood if (*table == '\n')
30fec60470SScott Wood *table = 0;
31fec60470SScott Wood
32fec60470SScott Wood table++;
33fec60470SScott Wood } while (*(table - 1) || *table != '\n');
34fec60470SScott Wood
35fec60470SScott Wood *table = 0;
36fec60470SScott Wood }
37fec60470SScott Wood
planetcore_get_key(const char * table,const char * key)38fec60470SScott Wood const char *planetcore_get_key(const char *table, const char *key)
39fec60470SScott Wood {
40fec60470SScott Wood int keylen = strlen(key);
41fec60470SScott Wood
42fec60470SScott Wood do {
43fec60470SScott Wood if (!strncmp(table, key, keylen) && table[keylen] == '=')
44fec60470SScott Wood return table + keylen + 1;
45fec60470SScott Wood
46fec60470SScott Wood table += strlen(table) + 1;
47fec60470SScott Wood } while (strlen(table) != 0);
48fec60470SScott Wood
49fec60470SScott Wood return NULL;
50fec60470SScott Wood }
51fec60470SScott Wood
planetcore_get_decimal(const char * table,const char * key,u64 * val)52fec60470SScott Wood int planetcore_get_decimal(const char *table, const char *key, u64 *val)
53fec60470SScott Wood {
54fec60470SScott Wood const char *str = planetcore_get_key(table, key);
55fec60470SScott Wood if (!str)
56fec60470SScott Wood return 0;
57fec60470SScott Wood
58fec60470SScott Wood *val = strtoull(str, NULL, 10);
59fec60470SScott Wood return 1;
60fec60470SScott Wood }
61fec60470SScott Wood
planetcore_get_hex(const char * table,const char * key,u64 * val)62fec60470SScott Wood int planetcore_get_hex(const char *table, const char *key, u64 *val)
63fec60470SScott Wood {
64fec60470SScott Wood const char *str = planetcore_get_key(table, key);
65fec60470SScott Wood if (!str)
66fec60470SScott Wood return 0;
67fec60470SScott Wood
68fec60470SScott Wood *val = strtoull(str, NULL, 16);
69fec60470SScott Wood return 1;
70fec60470SScott Wood }
71fec60470SScott Wood
72fec60470SScott Wood static u64 mac_table[4] = {
73fec60470SScott Wood 0x000000000000,
74fec60470SScott Wood 0x000000800000,
75fec60470SScott Wood 0x000000400000,
76fec60470SScott Wood 0x000000c00000,
77fec60470SScott Wood };
78fec60470SScott Wood
planetcore_set_mac_addrs(const char * table)79fec60470SScott Wood void planetcore_set_mac_addrs(const char *table)
80fec60470SScott Wood {
81fec60470SScott Wood u8 addr[4][6];
82fec60470SScott Wood u64 int_addr;
83fec60470SScott Wood u32 i;
84fec60470SScott Wood int j;
85fec60470SScott Wood
86fec60470SScott Wood if (!planetcore_get_hex(table, PLANETCORE_KEY_MAC_ADDR, &int_addr))
87fec60470SScott Wood return;
88fec60470SScott Wood
89fec60470SScott Wood for (i = 0; i < 4; i++) {
90fec60470SScott Wood u64 this_dev_addr = (int_addr & ~0x000000c00000) |
91fec60470SScott Wood mac_table[i];
92fec60470SScott Wood
93fec60470SScott Wood for (j = 5; j >= 0; j--) {
94fec60470SScott Wood addr[i][j] = this_dev_addr & 0xff;
95fec60470SScott Wood this_dev_addr >>= 8;
96fec60470SScott Wood }
97fec60470SScott Wood
98fec60470SScott Wood dt_fixup_mac_address(i, addr[i]);
99fec60470SScott Wood }
100fec60470SScott Wood }
101fec60470SScott Wood
102fec60470SScott Wood static char prop_buf[MAX_PROP_LEN];
103fec60470SScott Wood
planetcore_set_stdout_path(const char * table)104fec60470SScott Wood void planetcore_set_stdout_path(const char *table)
105fec60470SScott Wood {
106fec60470SScott Wood char *path;
107fec60470SScott Wood const char *label;
108fec60470SScott Wood void *node, *chosen;
109fec60470SScott Wood
110fec60470SScott Wood label = planetcore_get_key(table, PLANETCORE_KEY_SERIAL_PORT);
111fec60470SScott Wood if (!label)
112fec60470SScott Wood return;
113fec60470SScott Wood
114fec60470SScott Wood node = find_node_by_prop_value_str(NULL, "linux,planetcore-label",
115fec60470SScott Wood label);
116fec60470SScott Wood if (!node)
117fec60470SScott Wood return;
118fec60470SScott Wood
119fec60470SScott Wood path = get_path(node, prop_buf, MAX_PROP_LEN);
120fec60470SScott Wood if (!path)
121fec60470SScott Wood return;
122fec60470SScott Wood
123fec60470SScott Wood chosen = finddevice("/chosen");
124fec60470SScott Wood if (!chosen)
125fec60470SScott Wood chosen = create_node(NULL, "chosen");
126fec60470SScott Wood if (!chosen)
127fec60470SScott Wood return;
128fec60470SScott Wood
129fec60470SScott Wood setprop_str(chosen, "linux,stdout-path", path);
130fec60470SScott Wood }
131