script_uboot.c 5.97 KB
Newer Older
Alejandro Mery's avatar
Alejandro Mery committed
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
/*
 * Copyright (C) 2012 Alejandro Mery <amery@geeks.cl>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "common.h"

#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "script.h"
#include "script_uboot.h"

#define pr_info(...)	errf("fexc-uboot: " __VA_ARGS__)
#define pr_err(...)	errf("E: fexc-uboot: " __VA_ARGS__)

#ifdef DEBUG
#define pr_debug(...)	errf("D: fexc-uboot: " __VA_ARGS__)
#else
#define pr_debug(...)
#endif

Alejandro Mery's avatar
Alejandro Mery committed
35
36
static inline void out_u32_member(FILE *out, const char *key, int hexa,
				  struct script_single_entry *val)
Alejandro Mery's avatar
Alejandro Mery committed
37
38
{
	const char *fmt;
39
	if (hexa)
Alejandro Mery's avatar
Alejandro Mery committed
40
41
42
43
		fmt = "\t.%s = %#x,\n";
	else
		fmt = "\t.%s = %u,\n";

Alejandro Mery's avatar
Alejandro Mery committed
44
	fprintf(out, fmt, key, val->value);
Alejandro Mery's avatar
Alejandro Mery committed
45
46
}

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
static inline void out_gpio_member(FILE *out, const char *key,
				   struct script_gpio_entry *gpio)
{
	fprintf(out, "\t.%s = ", key);

	if (gpio->port == 0xffff)
		fprintf(out, "GPIO_AXP_CFG(%u", gpio->port_num);
	else
		fprintf(out, "GPIO_CFG(%u, %u", gpio->port, gpio->port_num);

	for (const int *p = gpio->data, *pe = p+4; p != pe; p++) {
		if (*p == -1)
			fputs(", 0xff", out);
		else
			fprintf(out, ", %u", *p);
	}

	fputs("),\n", out);
}

Alejandro Mery's avatar
Alejandro Mery committed
67
68
69
70
71
static inline void out_null_member(FILE *out, const char *key)
{
	fprintf(out, "\t/* %s is NULL */\n", key);
}

72
73
74
/*
 * DRAM
 */
Alejandro Mery's avatar
Alejandro Mery committed
75
76
77
78
79
static int generate_dram_struct(FILE *out, struct script_section *sp)
{
	struct list_entry *le;
	struct script_entry *ep;
	const char *key;
80
	int ret = 1, hexa;
Alejandro Mery's avatar
Alejandro Mery committed
81

82
	fprintf(out, "static struct dram_para dram_para = {\n");
Alejandro Mery's avatar
Alejandro Mery committed
83
84
85
86
87
88
89
90
	for (le = list_first(&sp->entries); le;
	     le = list_next(&sp->entries, le)) {
		ep = container_of(le, struct script_entry, entries);

		if (strncmp(ep->name, "dram_", 5) != 0)
			goto invalid_field;

		key = ep->name + 5;
91
		if (key[0] == '\0')
Alejandro Mery's avatar
Alejandro Mery committed
92
93
94
95
96
			goto invalid_field;
		else if (strcmp(key, "baseaddr") == 0)
			continue; /* skip */
		else if (strcmp(key, "clk") == 0)
			key = "clock";
97
98
		else if (strcmp(key, "chip_density") == 0)
			key = "density";
Alejandro Mery's avatar
Alejandro Mery committed
99

100
101
102
103
104
105
		if (strncmp(key, "tpr", 3) == 0 ||
		    strncmp(key, "emr", 3) == 0)
			hexa = 1;
		else
			hexa = 0;

Alejandro Mery's avatar
Alejandro Mery committed
106
107
		switch (ep->type) {
		case SCRIPT_VALUE_TYPE_SINGLE_WORD:
Alejandro Mery's avatar
Alejandro Mery committed
108
109
110
			out_u32_member(out, key, hexa,
			       container_of(ep, struct script_single_entry, entry));
			break;
Alejandro Mery's avatar
Alejandro Mery committed
111
		case SCRIPT_VALUE_TYPE_NULL:
Alejandro Mery's avatar
Alejandro Mery committed
112
113
114
115
116
			out_null_member(out, key);
			break;
		case SCRIPT_VALUE_TYPE_GPIO:
			out_gpio_member(out, key,
				container_of(ep, struct script_gpio_entry, entry));
Alejandro Mery's avatar
Alejandro Mery committed
117
118
119
120
121
122
123
124
		default:
invalid_field:
			pr_err("dram_para: %s: invalid field\n", ep->name);
			ret = 0;
		}

	}
	fprintf(out, "};\n");
125
126
127
128
129
130
131
132
133
134
	fputs("\nint sunxi_dram_init(void)\n"
	      "{\n\treturn DRAMC_init(&dram_para);\n}\n",
	      out);

	return ret;
}

/*
 * PMU
 */
135
136
static int generate_pmu_struct(FILE *out, struct script_section *target,
			       struct script_section *pmu_para)
137
138
{
	struct list_entry *le;
139
	struct script_section *sp;
140
	struct script_entry *ep;
141
	const char *key;
142
143
144
	int ret = 1;

	fputs("\nstatic struct pmu_para pmu_para = {\n", out);
145
146

	sp = target;
147
148
149
150
	for (le = list_first(&sp->entries); le;
	     le = list_next(&sp->entries, le)) {
		ep = container_of(le, struct script_entry, entries);

Alejandro Mery's avatar
Alejandro Mery committed
151
152
		key = ep->name;

153
154
		switch (ep->type) {
		case SCRIPT_VALUE_TYPE_SINGLE_WORD:
Alejandro Mery's avatar
Alejandro Mery committed
155
156
157
			out_u32_member(out, key, 0,
			       container_of(ep, struct script_single_entry, entry));
			break;
158
		case SCRIPT_VALUE_TYPE_NULL:
Alejandro Mery's avatar
Alejandro Mery committed
159
160
161
162
163
164
			out_null_member(out, key);
			break;
		case SCRIPT_VALUE_TYPE_GPIO:
			out_gpio_member(out, key,
				container_of(ep, struct script_gpio_entry, entry));
			break;
165
		default:
166
			pr_err("target: %s: invalid field\n", ep->name);
167
168
169
170
			ret = 0;
		}

	}
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

	sp = pmu_para;
	for (le = list_first(&sp->entries); le;
	     le = list_next(&sp->entries, le)) {
		ep = container_of(le, struct script_entry, entries);

		if (strncmp(ep->name, "pmu_", 4) != 0)
			continue;
		key = ep->name+4;

		if (strcmp(key, "used2") == 0 ||
		    strcmp(key, "para") == 0 ||
		    strcmp(key, "adpdet") == 0 ||
		    strcmp(key, "shutdown_chgcur") == 0 ||
		    strcmp(key, "shutdown_chgcur2") == 0 ||
		    strcmp(key, "pwroff_vol") == 0 ||
		    strcmp(key, "pwron_vol") == 0) {

			switch(ep->type) {
			case SCRIPT_VALUE_TYPE_SINGLE_WORD:
Alejandro Mery's avatar
Alejandro Mery committed
191
192
				out_u32_member(out, key, 0,
				       container_of(ep, struct script_single_entry, entry));
193
194
				break;
			case SCRIPT_VALUE_TYPE_NULL:
Alejandro Mery's avatar
Alejandro Mery committed
195
				out_null_member(out, key);
196
197
198
199
200
201
202
203
204
205
206
				break;
			case SCRIPT_VALUE_TYPE_GPIO:
				out_gpio_member(out, key,
					container_of(ep, struct script_gpio_entry, entry));
				break;
			default:
				pr_err("pmu_para: %s: invalid field\n", ep->name);
			}
		}
	}

207
208
209
210
	fputs("};\n", out);
	fputs("\nint sunxi_pmu_init(void)\n"
	      "{\n\treturn PMU_init(&pmu_para);\n}\n",
	      out);
Alejandro Mery's avatar
Alejandro Mery committed
211
	return ret;
212
213

	(void) pmu_para;
Alejandro Mery's avatar
Alejandro Mery committed
214
215
216
217
218
}

int script_generate_uboot(FILE *out, const char *UNUSED(filename),
			  struct script *script)
{
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
	struct {
		const char *name;
		struct script_section *sp;
	} sections[] = {
		{ "dram_para", NULL },
		{ "target", NULL },
		{ "pmu_para", NULL },
	};

	for (unsigned i=0; i<ARRAY_SIZE(sections); i++) {
		struct script_section *sp;

		sp = script_find_section(script, sections[i].name);
		if (sp)
			sections[i].sp = sp;
		else {
			pr_err("%s: critical section missing",
			       sections[i].name);
			return 0;
		}
	}
Alejandro Mery's avatar
Alejandro Mery committed
240

241
242
	fputs("/* this file is generated, don't edit it yourself */\n\n"
	      "#include <common.h>\n"
243
	      "#include <asm/arch/pmu.h>\n"
244
245
	      "#include <asm/arch/dram.h>\n\n",
	      out);
Alejandro Mery's avatar
Alejandro Mery committed
246

247
248
	generate_dram_struct(out, sections[0].sp);
	generate_pmu_struct(out, sections[1].sp, sections[2].sp);
249

Alejandro Mery's avatar
Alejandro Mery committed
250
251
	return 1;
}