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
240
241
242
243
244
245
246
247
248
249
250
251
|
#include <common.h>
#include <asm/u-boot.h>
#include <asm/global_data.h>
#include <linux/libfdt.h>
#include <asm/arch/cpu.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <dm.h>
#include "bcm_bootstate.h"
DECLARE_GLOBAL_DATA_PTR;
#define CHECK_NULL(ptr) ptr != NULL //|| init_regs()
typedef void (*set_boot_reason_p)(uint32_t value);
typedef void (*clear_boot_reason_p)(void);
typedef uint32_t (*get_boot_reason_p)(void);
struct spi_reset_reason
{
uint32_t *glb_cntrl;
uint32_t *flash_cntrl;
uint32_t *profile;
};
typedef struct boot_state_data
{
uint32_t *reset_status;
union
{
volatile uint32_t *reset_reason;
struct spi_reset_reason srr;
};
set_boot_reason_p set_boot_reason;
get_boot_reason_p get_boot_reason;
clear_boot_reason_p clear_boot_reason;
}boot_state_data;
boot_state_data __attribute__((section(".data"))) b_state_data;
int bcmbca_get_reset_status(void)
{
int resetStatus = -1;
if(CHECK_NULL((b_state_data.reset_status)))
{
resetStatus = *b_state_data.reset_status & RESET_STATUS_MASK;
}
return resetStatus;
}
unsigned long i=0;
static void bcmbca_set_boot_reason_v1(uint32_t value)
{
if(CHECK_NULL((b_state_data.srr.glb_cntrl)))
{
*b_state_data.srr.glb_cntrl |= DO_NOT_RESET_ON_WATCHDOG;
if(CHECK_NULL((b_state_data.srr.profile)))
{
*b_state_data.srr.profile = value;
}
}
}
static void bcmbca_set_boot_reason_v2(uint32_t value)
{
uint32_t tmp_val;
int retries=0;
if(CHECK_NULL((b_state_data.reset_reason)))
{
while(retries++ < 255)
{
tmp_val=*b_state_data.reset_reason;
dsb();
*b_state_data.reset_reason=value;
dsb();
tmp_val=*b_state_data.reset_reason;
if(tmp_val == value)
break;
}
if(retries > 1)
printf("current value %d value to write %d retried [%d] times to write the reset_reason %s\n", tmp_val, value, retries, (tmp_val == value) ? "success":"fail");
}
}
void bcmbca_set_boot_reason(uint32_t value)
{
if(CHECK_NULL(b_state_data.set_boot_reason))
{
b_state_data.set_boot_reason(value);
}
}
static void bcmbca_clear_boot_reason_v1(void)
{
if(CHECK_NULL((b_state_data.srr.glb_cntrl)))
{
*b_state_data.srr.glb_cntrl &= ~DO_NOT_RESET_ON_WATCHDOG;
if(CHECK_NULL((b_state_data.srr.flash_cntrl)))
{
*b_state_data.srr.flash_cntrl = FLASH_CNTRL_RESET_VAL;
}
if(CHECK_NULL((b_state_data.srr.profile)))
{
*b_state_data.srr.profile = 0;
}
}
}
static void bcmbca_clear_boot_reason_v2(void)
{
uint32_t tmp_val;
int retries=0;
if(CHECK_NULL((b_state_data.reset_reason)))
{
while(retries++ < 255)
{
tmp_val=*b_state_data.reset_reason;
dsb();
*b_state_data.reset_reason &= ~(0x1ffff);
dsb();
tmp_val=*b_state_data.reset_reason;
if((tmp_val & (0x1ffff)) == 0)
break;
}
if(retries > 1)
printf("retried [%d] times to clear the reset_reason %s\n", retries, ((tmp_val & (0x1ffff)) == 0) ? "success":"fail");
}
}
void bcmbca_clear_boot_reason(void)
{
if(CHECK_NULL((b_state_data.clear_boot_reason)))
{
b_state_data.clear_boot_reason();
}
}
static uint32_t bcmbca_get_boot_reason_v1(void)
{
uint32_t rc=-1;
if(CHECK_NULL((b_state_data.srr.profile)))
{
rc=(*b_state_data.reset_status & SW_RESET_STATUS) != 0 ? *b_state_data.srr.profile & 0x1ffff:-1;
}
return rc;
}
static uint32_t bcmbca_get_boot_reason_v2(void)
{
uint32_t rc=-1;
//boot reason is only good in case of sw reset
if((b_state_data.reset_reason != NULL && b_state_data.reset_status != NULL))
{
rc=(*b_state_data.reset_status & SW_RESET_STATUS) != 0 ? *b_state_data.reset_reason & 0x1ffff:-1;
}
return rc;
}
uint32_t bcmbca_get_boot_reason(void)
{
int rc=-1;
if(CHECK_NULL((b_state_data.get_boot_reason)))
{
rc = b_state_data.get_boot_reason();
}
return rc;
}
static int bootstate_v1_probe(struct udevice *dev)
{
struct resource res;
int ret=0;
b_state_data.clear_boot_reason = bcmbca_clear_boot_reason_v1;
b_state_data.set_boot_reason = bcmbca_set_boot_reason_v1;
b_state_data.get_boot_reason = bcmbca_get_boot_reason_v1;
ret = dev_read_resource_byname(dev, "reset_status", &res);
if (!ret) {
b_state_data.reset_status=devm_ioremap(dev, res.start, resource_size(&res));
}
ret = dev_read_resource_byname(dev, "global_control", &res);
if (!ret) {
b_state_data.srr.glb_cntrl=devm_ioremap(dev, res.start, resource_size(&res));
}
ret = dev_read_resource_byname(dev, "flash_control", &res);
if (!ret) {
b_state_data.srr.flash_cntrl=devm_ioremap(dev, res.start, resource_size(&res));
}
ret = dev_read_resource_byname(dev, "mode_control", &res);
if (!ret) {
b_state_data.srr.profile=devm_ioremap(dev, res.start, resource_size(&res));
}
return 0;
}
static int bootstate_v2_probe(struct udevice *dev)
{
struct resource res;
int ret=0;
b_state_data.clear_boot_reason = bcmbca_clear_boot_reason_v2;
b_state_data.set_boot_reason = bcmbca_set_boot_reason_v2;
b_state_data.get_boot_reason = bcmbca_get_boot_reason_v2;
ret = dev_read_resource_byname(dev, "reset_status", &res);
if (!ret) {
b_state_data.reset_status=devm_ioremap(dev, res.start, resource_size(&res));
}
ret = dev_read_resource_byname(dev, "reset_reason", &res);
if (!ret) {
b_state_data.reset_reason=devm_ioremap(dev, res.start, resource_size(&res));
}
return 0;
}
static const struct udevice_id bootstate_v1_ids[] = {
{ .compatible = "brcm,bcmbca-bootstate-v1" },
{ }
};
static const struct udevice_id bootstate_v2_ids[] = {
{ .compatible = "brcm,bcmbca-bootstate-v2" },
{ }
};
U_BOOT_DRIVER(bootstate_v1_drv) = {
.name = "bcm_bootsate_v1",
.id = UCLASS_NOP,
.of_match = bootstate_v1_ids,
.probe = bootstate_v1_probe,
};
U_BOOT_DRIVER(bootstate_v2_drv) = {
.name = "bcm_bootsate_v2",
.id = UCLASS_NOP,
.of_match = bootstate_v2_ids,
.probe = bootstate_v2_probe,
};
void bca_bootstate_probe(void)
{
struct udevice *dev;
memset(&b_state_data, '\0', sizeof(b_state_data));
//for (uclass_first_device_check(UCLASS_MISC, &dev); dev; uclass_next_device_check(&dev));
uclass_get_device_by_driver(UCLASS_NOP, DM_GET_DRIVER(bootstate_v2_drv), &dev);
uclass_get_device_by_driver(UCLASS_NOP, DM_GET_DRIVER(bootstate_v1_drv), &dev);
}
|