aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/brcm63xx-2.6/files/arch/mips/cfe/cfe.c
blob: 6d16111e1c9df2f234c327a7b8203e84deb3d030 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
 * Broadcom Common Firmware Environment (CFE) support
 *
 * Copyright 2000, 2001, 2002
 * Broadcom Corporation. All rights reserved.
 *
 * Copyright (C) 2006 Michael Buesch
 *
 * Original Authors:  Mitch Lichtenberg, Chris Demetriou
 *
 * This software is furnished under license and may be used and copied only
 * in accordance with the following terms and conditions.  Subject to these
 * conditions, you may download, copy, install, use, modify and distribute
 * modified or unmodified copies of this software in source and/or binary
 * form. No title or ownership is transferred hereby.
 *
 * 1) Any source code used, modified or distributed must reproduce and
 *    retain this copyright notice and list of conditions as they appear in
 *    the source file.
 *
 * 2) No right is granted to use any trade name, trademark, or logo of
 *    Broadcom Corporation.  The "Broadcom Corporation" name may not be
 *    used to endorse or promote products derived from this software
 *    without the prior written permission of Broadcom Corporation.
 *
 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED
 *    WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
 *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 *    NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE
 *    FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE
 *    LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 *    OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <linux/init.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/spinlock.h>
#include <asm/cfe.h>

#include "cfe_private.h"


static cfe_uint_t cfe_handle;
static int (*cfe_trampoline)(long handle, long iocb);


#include <linux/kernel.h>

void __init cfe_setup(unsigned long fwarg0, unsigned long fwarg1,
		      unsigned long fwarg2, unsigned long fwarg3)
{
	if (fwarg3 == 0x80300000) {
		/* WRT54G workaround */
		fwarg3 = CFE_EPTSEAL;
		fwarg2 = 0xBFC00500;
	}
	if (fwarg3 != CFE_EPTSEAL) {
		/* We are not booted from CFE */
		return;
	}
	if (fwarg1 == 0) {
		/* We are on the boot CPU */
		cfe_handle = (cfe_uint_t)fwarg0;
		cfe_trampoline = CFE_TO_PTR(fwarg2);
	}
}

int cfe_vprintk(const char *fmt, va_list args)
{
	static char buffer[1024];
	static DEFINE_SPINLOCK(lock);
	static const char pfx[] = "CFE-console: ";
	static const size_t pfx_len = sizeof(pfx) - 1;
	unsigned long flags;
	int len, cnt, pos;
	int handle;
	int res;

	if (!cfe_present())
		return -ENODEV;

	spin_lock_irqsave(&lock, flags);
	handle = cfe_getstdhandle(CFE_STDHANDLE_CONSOLE);
	if (CFE_ISERR(handle)) {
		len = -EIO;
		goto out;
	}
	strcpy(buffer, pfx);
	len = vscnprintf(buffer + pfx_len,
			 sizeof(buffer) - pfx_len - 2,
			 fmt, args);
	len += pfx_len;
	/* The CFE console requires CR-LF line-ends.
	 * Add a CR, if we only terminate lines with a LF.
	 * This does only fix CR-LF at the end of the string.
	 * So for multiple lines, use multiple cfe_vprintk calls.
	 */
	if (len > 1 &&
	    buffer[len - 1] == '\n' && buffer[len - 2] != '\r') {
		buffer[len - 1] = '\r';
		buffer[len] = '\n';
		len += 1;
	}
	cnt = len;
	pos = 0;
	while (cnt > 0) {
		res = cfe_write(handle, buffer + pos, len - pos);
		if (CFE_ISERR(res)) {
			len = -EIO;
			goto out;
		}
		cnt -= res;
		pos += res;
	}
out:
	spin_unlock_irqrestore(&lock, flags);

	return len;
}

int cfe_printk(const char *fmt, ...)
{
	va_list args;
	int res;

	va_start(args, fmt);
	res = cfe_vprintk(fmt, args);
	va_end(args);

	return res;
}

static int cfe_iocb_dispatch(struct cfe_iocb *iocb)
{
	if (!cfe_present())
		return CFE_ERR_UNSUPPORTED;
	return cfe_trampoline((long)cfe_handle, (long)iocb);
}

int cfe_present(void)
{
	return (cfe_trampoline != NULL);
}

int cfe_close(int handle)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_DEV_CLOSE;
	iocb.handle = handle;

	err = cfe_iocb_dispatch(&iocb);

	return (CFE_ISERR(err)) ? err : iocb.status;
}

int cfe_cpu_start(int cpu, void (*fn)(void), long sp, long gp, long a1)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_FW_CPUCTL;
	iocb.psize = sizeof(struct cfe_iocb_cpuctl);
	iocb.cpuctl.number = cpu;
	iocb.cpuctl.command = CFE_CPU_CMD_START;
	iocb.cpuctl.gp = gp;
	iocb.cpuctl.sp = sp;
	iocb.cpuctl.a1 = a1;
	iocb.cpuctl.start_addr = (long)fn;

	err = cfe_iocb_dispatch(&iocb);

	return (CFE_ISERR(err)) ? err : iocb.status;
}

int cfe_cpu_stop(int cpu)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_FW_CPUCTL;
	iocb.psize = sizeof(struct cfe_iocb_cpuctl);
	iocb.cpuctl.number = cpu;
	iocb.cpuctl.command = CFE_CPU_CMD_STOP;

	err = cfe_iocb_dispatch(&iocb);

	return (CFE_ISERR(err)) ? err : iocb.status;
}

int cfe_enumenv(int idx, char *name, int namelen, char *val, int vallen)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_ENV_ENUM;
	iocb.psize = sizeof(struct cfe_iocb_envbuf);
	iocb.envbuf.index = idx;
	iocb.envbuf.name = PTR_TO_CFE(name);
	iocb.envbuf.name_len = namelen;
	iocb.envbuf.val = PTR_TO_CFE(val);
	iocb.envbuf.val_len = vallen;

	err = cfe_iocb_dispatch(&iocb);

	return (CFE_ISERR(err)) ? err : iocb.status;
}

int cfe_enumdev(int idx, char *name, int namelen)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));

	iocb.fcode = CFE_CMD_DEV_ENUM;
	iocb.psize = sizeof(struct cfe_iocb_envbuf);
	iocb.envbuf.index = idx;
	iocb.envbuf.name = PTR_TO_CFE(name);
	iocb.envbuf.name_len = namelen;

	err = cfe_iocb_dispatch(&iocb);

	return (CFE_ISERR(err)) ? err : iocb.status;
}

int cfe_enummem(int idx, int flags, u64 *start, u64 *length,
		u64 *type)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));

	iocb.fcode = CFE_CMD_FW_MEMENUM;
	iocb.flags = flags;
	iocb.psize = sizeof(struct cfe_iocb_meminfo);
	iocb.meminfo.index = idx;

	err = cfe_iocb_dispatch(&iocb);
	if (CFE_ISERR(err))
		return err;
	if (!CFE_ISERR(iocb.status)) {
		*start = iocb.meminfo.addr;
		*length = iocb.meminfo.size;
		*type = iocb.meminfo.type;
	}

	return iocb.status;
}

int cfe_exit(int warm, int status)
{
	struct cfe_iocb iocb;
	int err;

printk("CFE REBOOT\n");
	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_FW_RESTART;
	if (warm)
		iocb.flags = CFE_FLG_WARMSTART;
	iocb.psize = sizeof(struct cfe_iocb_exitstat);
	iocb.exitstat.status = status;

printk("CALL\n");
	err = cfe_iocb_dispatch(&iocb);
printk("DONE\n");

	return (CFE_ISERR(err)) ? err : iocb.status;
}

int cfe_flushcache(int flags)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_FW_FLUSHCACHE;
	iocb.flags = flags;

	err = cfe_iocb_dispatch(&iocb);

	return (CFE_ISERR(err)) ? err : iocb.status;
}

int cfe_getdevinfo(char *name)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_DEV_GETINFO;
	iocb.psize = sizeof(struct cfe_iocb_buf);
	iocb.buffer.ptr = PTR_TO_CFE(name);
	iocb.buffer.length = strlen(name);

	err = cfe_iocb_dispatch(&iocb);
	if (CFE_ISERR(err))
		return err;
	if (CFE_ISERR(iocb.status))
		return iocb.status;

	return iocb.buffer.devflags;
}

int cfe_getenv(char *name, char *dest, int destlen)
{
	struct cfe_iocb iocb;
	int err;

	dest[0] = '\0';
	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_ENV_GET;
	iocb.psize = sizeof(struct cfe_iocb_envbuf);
	iocb.envbuf.name = PTR_TO_CFE(name);
	iocb.envbuf.name_len = strlen(name);
	iocb.envbuf.val = PTR_TO_CFE(dest);
	iocb.envbuf.val_len = destlen;

	err = cfe_iocb_dispatch(&iocb);

	return (CFE_ISERR(err)) ? err : iocb.status;
}

int cfe_getfwinfo(struct cfe_fwinfo *info)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_FW_GETINFO;
	iocb.psize = sizeof(struct cfe_iocb_fwinfo);

	err = cfe_iocb_dispatch(&iocb);
	if (CFE_ISERR(err))
		return err;
	if (CFE_ISERR(iocb.status))
		return err;

	info->version = iocb.fwinfo.version;
	info->totalmem = iocb.fwinfo.totalmem;
	info->flags = iocb.fwinfo.flags;
	info->boardid = iocb.fwinfo.boardid;
	info->bootarea_va = iocb.fwinfo.bootarea_va;
	info->bootarea_pa = iocb.fwinfo.bootarea_pa;
	info->bootarea_size = iocb.fwinfo.bootarea_size;

	return iocb.status;
}

int cfe_getstdhandle(int handletype)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_DEV_GETHANDLE;
	iocb.flags = handletype;

	err = cfe_iocb_dispatch(&iocb);
	if (CFE_ISERR(err))
		return err;
	if (CFE_ISERR(iocb.status))
		return iocb.status;

	return iocb.handle;
}

int cfe_getticks(s64 *ticks)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_FW_GETTIME;
	iocb.psize = sizeof(struct cfe_iocb_time);

	err = cfe_iocb_dispatch(&iocb);
	if (CFE_ISERR(err))
		return err;
	if (!CFE_ISERR(iocb.status))
		*ticks = iocb.time.ticks;

	return iocb.status;
}

int cfe_inpstat(int handle)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_DEV_INPSTAT;
	iocb.handle = handle;
	iocb.psize = sizeof(struct cfe_iocb_inpstat);

	err = cfe_iocb_dispatch(&iocb);
	if (CFE_ISERR(err))
		return err;
	if (CFE_ISERR(iocb.status))
		return iocb.status;

	return iocb.inpstat.status;
}

int cfe_ioctl(int handle, unsigned int ioctlnum,
	      unsigned char *buffer, int length,
	      int *retlen, u64 offset)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_DEV_IOCTL;
	iocb.handle = handle;
	iocb.psize = sizeof(struct cfe_iocb_buf);
	iocb.buffer.offset = offset;
	iocb.buffer.ioctlcmd = ioctlnum;
	iocb.buffer.ptr = PTR_TO_CFE(buffer);
	iocb.buffer.length = length;

	err = cfe_iocb_dispatch(&iocb);
	if (CFE_ISERR(err))
		return err;
	if (CFE_ISERR(iocb.status))
		return iocb.status;
	if (retlen)
		*retlen = iocb.buffer.retlen;

	return iocb.status;
}

int cfe_open(char *name)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_DEV_OPEN;
	iocb.psize = sizeof(struct cfe_iocb_buf);
	iocb.buffer.ptr = PTR_TO_CFE(name);
	iocb.buffer.length = strlen(name);

	err = cfe_iocb_dispatch(&iocb);
	if (CFE_ISERR(err))
		return err;
	if (CFE_ISERR(iocb.status))
		return iocb.status;

	return iocb.handle;
}

int cfe_read(int handle, unsigned char *buffer, int length)
{
	return cfe_readblk(handle, 0, buffer, length);
}

int cfe_readblk(int handle, s64 offset, unsigned char *buffer, int length)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_DEV_READ;
	iocb.handle = handle;
	iocb.psize = sizeof(struct cfe_iocb_buf);
	iocb.buffer.offset = offset;
	iocb.buffer.ptr = PTR_TO_CFE(buffer);
	iocb.buffer.length = length;

	err = cfe_iocb_dispatch(&iocb);
	if (CFE_ISERR(err))
		return err;
	if (CFE_ISERR(iocb.status))
		return iocb.status;

	return iocb.buffer.retlen;
}

int cfe_setenv(char *name, char *val)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_ENV_SET;
	iocb.psize = sizeof(struct cfe_iocb_envbuf);
	iocb.envbuf.name = PTR_TO_CFE(name);
	iocb.envbuf.name_len = strlen(name);
	iocb.envbuf.val = PTR_TO_CFE(val);
	iocb.envbuf.val_len = strlen(val);

	err = cfe_iocb_dispatch(&iocb);

	return (CFE_ISERR(err)) ? err : iocb.status;
}

int cfe_write(int handle, unsigned char *buffer, int length)
{
	return cfe_writeblk(handle, 0, buffer, length);
}

int cfe_writeblk(int handle, s64 offset, unsigned char *buffer, int length)
{
	struct cfe_iocb iocb;
	int err;

	memset(&iocb, 0, sizeof(iocb));
	iocb.fcode = CFE_CMD_DEV_WRITE;
	iocb.handle = handle;
	iocb.psize = sizeof(struct cfe_iocb_buf);
	iocb.buffer.offset = offset;
	iocb.buffer.ptr = PTR_TO_CFE(buffer);
	iocb.buffer.length = length;

	err = cfe_iocb_dispatch(&iocb);
	if (CFE_ISERR(err))
		return err;
	if (CFE_ISERR(iocb.status))
		return iocb.status;

	return iocb.buffer.retlen;
}