summaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-omap/omap_generic.c
blob: 99e14fb540216816ebe935e943b4707c92e03802 (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
/*
 * (C) Copyright 2013 Teresa Gámez, Phytec Messtechnik GmbH
 *
 * 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.
 *
 */
#include <common.h>
#include <bootsource.h>
#include <envfs.h>
#include <bootm.h>
#include <init.h>
#include <io.h>
#include <fs.h>
#include <malloc.h>
#include <libfile.h>
#include <linux/stat.h>
#include <mach/omap/gpmc.h>
#include <mach/omap/generic.h>
#include <mach/omap/am33xx-silicon.h>
#include <mach/omap/omap3-silicon.h>
#include <mach/omap/omap4-silicon.h>
#include <mach/omap/am33xx-generic.h>
#include <mach/omap/omap3-generic.h>
#include <mach/omap/omap4-generic.h>

void __iomem *omap_gpmc_base;

unsigned int __omap_cpu_type;

static void *omap_sram_start(void)
{
	if (cpu_is_am33xx())
		return (void *)AM33XX_SRAM0_START;
	if (cpu_is_omap3())
		return (void *)OMAP3_SRAM_BASE;
	if (cpu_is_omap4())
		return (void *)OMAP44XX_SRAM_BASE;
	return NULL;
}

static void *omap_scratch_space_start(void)
{
	if (cpu_is_am33xx())
		return (void *)AM33XX_SRAM_SCRATCH_SPACE;
	if (cpu_is_omap3())
		return (void *)OMAP3_SRAM_SCRATCH_SPACE;
	if (cpu_is_omap4())
		return (void *)OMAP44XX_SRAM_SCRATCH_SPACE;
	return NULL;
}

void __noreturn omap_start_barebox(void *barebox)
{
	int (*func)(void *) = barebox;
	void *sramadr = omap_sram_start();
	void *scratch = omap_scratch_space_start();

	memcpy(sramadr, scratch, sizeof(uint32_t) * 3);

	shutdown_barebox();
	func(sramadr);
	hang();
}

#define OMAP_WDT_WWPS		0x34
#define OMAP_WDT_WSPR		0x48
#define WDT_DISABLE_CODE1	0xaaaa
#define WDT_DISABLE_CODE2	0x5555

void omap_watchdog_disable(const void __iomem *wdt)
{
	/* WDT is already running when the bootloader gets control
	 * Disable it to avoid "random" resets
	 */
	__raw_writel(WDT_DISABLE_CODE1, wdt + OMAP_WDT_WSPR);

	do {
	} while (__raw_readl(wdt + OMAP_WDT_WWPS));

	__raw_writel(WDT_DISABLE_CODE2, wdt + OMAP_WDT_WSPR);
}

#ifdef CONFIG_BOOTM
static int do_bootm_omap_barebox(struct image_data *data)
{
	void (*barebox)(uint32_t);

	barebox = read_file(data->os_file, NULL);
	if (!barebox)
		return -EINVAL;

	if (data->dryrun) {
		free(barebox);
		return 0;
	}

	omap_start_barebox(barebox);
}

static struct image_handler omap_barebox_handler = {
	.name = "OMAP barebox",
	.bootm = do_bootm_omap_barebox,
	.filetype = filetype_arm_barebox,
};

static int omap_bootm_barebox(void)
{
	if (!cpu_is_omap())
		return 0;

	return register_image_handler(&omap_barebox_handler);
}
device_initcall(omap_bootm_barebox);
#endif

const static char *omap_bootmmc_dev;

void omap_set_bootmmc_devname(const char *devname)
{
	omap_bootmmc_dev = devname;
}

const char *omap_get_bootmmc_devname(void)
{
	return omap_bootmmc_dev;
}

#if defined(CONFIG_ENV_HANDLING)
static char *envpath = "/mnt/mmc0.0/barebox.env";

static int omap_env_init(void)
{
	int ret;
	const char *diskdev;
	char *partname;
	struct cdev *cdev;
	const char *rootpath;

	if (!cpu_is_omap())
		return 0;

	if (bootsource_get() != BOOTSOURCE_MMC)
		return 0;

	if (omap_bootmmc_dev)
		diskdev = omap_bootmmc_dev;
	else
		diskdev = "disk0";

	device_detect_by_name(diskdev);
	partname = basprintf("%s.0", diskdev);
	cdev = cdev_by_name(partname);
	if (cdev == NULL) {
		pr_err("Failed to get device %s\n", partname);
		goto out;
	}

	rootpath = cdev_mount_default(cdev, NULL);
	if (IS_ERR(rootpath)) {
		pr_err("Failed to load environment: mount %s failed (%ld)\n",
						cdev->name, PTR_ERR(rootpath));
		goto out;
	}
	ret = symlink(rootpath, "/boot");
	if (ret < 0)
		pr_warn("Failed to create symlink from %s to %s\n", rootpath
								, "/boot");

	envpath = basprintf("%s/barebox.env", rootpath);

	pr_debug("Loading default env from %s on device %s\n", envpath,
								partname);
	default_environment_path_set(envpath);

	free(envpath);
out:
	free(partname);

	return 0;
}
late_initcall(omap_env_init);
#endif

static int omap_soc_from_dt(void)
{
        if (of_machine_is_compatible("ti,am33xx"))
		return OMAP_CPU_AM33XX;
        if (of_machine_is_compatible("ti,omap4"))
		return OMAP_CPU_OMAP4;
        if (of_machine_is_compatible("ti,omap3"))
		return OMAP_CPU_OMAP3;

	return 0;
}

static int omap_init(void)
{
	int ret;
	struct device_node *root;

	root = of_get_root_node();
	if (root) {
		__omap_cpu_type = omap_soc_from_dt();
		if (!__omap_cpu_type)
			return 0;
	}

	if (cpu_is_omap3())
		ret = omap3_init();
	else if (cpu_is_omap4())
		ret = omap4_init();
	else if (cpu_is_am33xx())
		ret = am33xx_init();
	else
		return -EINVAL;

	if (root)
		return ret;

	if (cpu_is_omap3())
		ret = omap3_devices_init();
	else if (cpu_is_omap4())
		ret = omap4_devices_init();
	else if (cpu_is_am33xx())
		ret = am33xx_devices_init();
	else
		return -EINVAL;

	return ret;
}
postcore_initcall(omap_init);