6d16111e1c9df2f234c327a7b8203e84deb3d030
[openwrt.git] / target / linux / brcm47xx / files / arch / mips / cfe / cfe.c
1 /*
2 * Broadcom Common Firmware Environment (CFE) support
3 *
4 * Copyright 2000, 2001, 2002
5 * Broadcom Corporation. All rights reserved.
6 *
7 * Copyright (C) 2006 Michael Buesch
8 *
9 * Original Authors: Mitch Lichtenberg, Chris Demetriou
10 *
11 * This software is furnished under license and may be used and copied only
12 * in accordance with the following terms and conditions. Subject to these
13 * conditions, you may download, copy, install, use, modify and distribute
14 * modified or unmodified copies of this software in source and/or binary
15 * form. No title or ownership is transferred hereby.
16 *
17 * 1) Any source code used, modified or distributed must reproduce and
18 * retain this copyright notice and list of conditions as they appear in
19 * the source file.
20 *
21 * 2) No right is granted to use any trade name, trademark, or logo of
22 * Broadcom Corporation. The "Broadcom Corporation" name may not be
23 * used to endorse or promote products derived from this software
24 * without the prior written permission of Broadcom Corporation.
25 *
26 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED
27 * WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
29 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE
30 * FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE
31 * LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
34 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
36 * OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <linux/init.h>
40 #include <linux/string.h>
41 #include <linux/errno.h>
42 #include <linux/spinlock.h>
43 #include <asm/cfe.h>
44
45 #include "cfe_private.h"
46
47
48 static cfe_uint_t cfe_handle;
49 static int (*cfe_trampoline)(long handle, long iocb);
50
51
52 #include <linux/kernel.h>
53
54 void __init cfe_setup(unsigned long fwarg0, unsigned long fwarg1,
55 unsigned long fwarg2, unsigned long fwarg3)
56 {
57 if (fwarg3 == 0x80300000) {
58 /* WRT54G workaround */
59 fwarg3 = CFE_EPTSEAL;
60 fwarg2 = 0xBFC00500;
61 }
62 if (fwarg3 != CFE_EPTSEAL) {
63 /* We are not booted from CFE */
64 return;
65 }
66 if (fwarg1 == 0) {
67 /* We are on the boot CPU */
68 cfe_handle = (cfe_uint_t)fwarg0;
69 cfe_trampoline = CFE_TO_PTR(fwarg2);
70 }
71 }
72
73 int cfe_vprintk(const char *fmt, va_list args)
74 {
75 static char buffer[1024];
76 static DEFINE_SPINLOCK(lock);
77 static const char pfx[] = "CFE-console: ";
78 static const size_t pfx_len = sizeof(pfx) - 1;
79 unsigned long flags;
80 int len, cnt, pos;
81 int handle;
82 int res;
83
84 if (!cfe_present())
85 return -ENODEV;
86
87 spin_lock_irqsave(&lock, flags);
88 handle = cfe_getstdhandle(CFE_STDHANDLE_CONSOLE);
89 if (CFE_ISERR(handle)) {
90 len = -EIO;
91 goto out;
92 }
93 strcpy(buffer, pfx);
94 len = vscnprintf(buffer + pfx_len,
95 sizeof(buffer) - pfx_len - 2,
96 fmt, args);
97 len += pfx_len;
98 /* The CFE console requires CR-LF line-ends.
99 * Add a CR, if we only terminate lines with a LF.
100 * This does only fix CR-LF at the end of the string.
101 * So for multiple lines, use multiple cfe_vprintk calls.
102 */
103 if (len > 1 &&
104 buffer[len - 1] == '\n' && buffer[len - 2] != '\r') {
105 buffer[len - 1] = '\r';
106 buffer[len] = '\n';
107 len += 1;
108 }
109 cnt = len;
110 pos = 0;
111 while (cnt > 0) {
112 res = cfe_write(handle, buffer + pos, len - pos);
113 if (CFE_ISERR(res)) {
114 len = -EIO;
115 goto out;
116 }
117 cnt -= res;
118 pos += res;
119 }
120 out:
121 spin_unlock_irqrestore(&lock, flags);
122
123 return len;
124 }
125
126 int cfe_printk(const char *fmt, ...)
127 {
128 va_list args;
129 int res;
130
131 va_start(args, fmt);
132 res = cfe_vprintk(fmt, args);
133 va_end(args);
134
135 return res;
136 }
137
138 static int cfe_iocb_dispatch(struct cfe_iocb *iocb)
139 {
140 if (!cfe_present())
141 return CFE_ERR_UNSUPPORTED;
142 return cfe_trampoline((long)cfe_handle, (long)iocb);
143 }
144
145 int cfe_present(void)
146 {
147 return (cfe_trampoline != NULL);
148 }
149
150 int cfe_close(int handle)
151 {
152 struct cfe_iocb iocb;
153 int err;
154
155 memset(&iocb, 0, sizeof(iocb));
156 iocb.fcode = CFE_CMD_DEV_CLOSE;
157 iocb.handle = handle;
158
159 err = cfe_iocb_dispatch(&iocb);
160
161 return (CFE_ISERR(err)) ? err : iocb.status;
162 }
163
164 int cfe_cpu_start(int cpu, void (*fn)(void), long sp, long gp, long a1)
165 {
166 struct cfe_iocb iocb;
167 int err;
168
169 memset(&iocb, 0, sizeof(iocb));
170 iocb.fcode = CFE_CMD_FW_CPUCTL;
171 iocb.psize = sizeof(struct cfe_iocb_cpuctl);
172 iocb.cpuctl.number = cpu;
173 iocb.cpuctl.command = CFE_CPU_CMD_START;
174 iocb.cpuctl.gp = gp;
175 iocb.cpuctl.sp = sp;
176 iocb.cpuctl.a1 = a1;
177 iocb.cpuctl.start_addr = (long)fn;
178
179 err = cfe_iocb_dispatch(&iocb);
180
181 return (CFE_ISERR(err)) ? err : iocb.status;
182 }
183
184 int cfe_cpu_stop(int cpu)
185 {
186 struct cfe_iocb iocb;
187 int err;
188
189 memset(&iocb, 0, sizeof(iocb));
190 iocb.fcode = CFE_CMD_FW_CPUCTL;
191 iocb.psize = sizeof(struct cfe_iocb_cpuctl);
192 iocb.cpuctl.number = cpu;
193 iocb.cpuctl.command = CFE_CPU_CMD_STOP;
194
195 err = cfe_iocb_dispatch(&iocb);
196
197 return (CFE_ISERR(err)) ? err : iocb.status;
198 }
199
200 int cfe_enumenv(int idx, char *name, int namelen, char *val, int vallen)
201 {
202 struct cfe_iocb iocb;
203 int err;
204
205 memset(&iocb, 0, sizeof(iocb));
206 iocb.fcode = CFE_CMD_ENV_ENUM;
207 iocb.psize = sizeof(struct cfe_iocb_envbuf);
208 iocb.envbuf.index = idx;
209 iocb.envbuf.name = PTR_TO_CFE(name);
210 iocb.envbuf.name_len = namelen;
211 iocb.envbuf.val = PTR_TO_CFE(val);
212 iocb.envbuf.val_len = vallen;
213
214 err = cfe_iocb_dispatch(&iocb);
215
216 return (CFE_ISERR(err)) ? err : iocb.status;
217 }
218
219 int cfe_enumdev(int idx, char *name, int namelen)
220 {
221 struct cfe_iocb iocb;
222 int err;
223
224 memset(&iocb, 0, sizeof(iocb));
225
226 iocb.fcode = CFE_CMD_DEV_ENUM;
227 iocb.psize = sizeof(struct cfe_iocb_envbuf);
228 iocb.envbuf.index = idx;
229 iocb.envbuf.name = PTR_TO_CFE(name);
230 iocb.envbuf.name_len = namelen;
231
232 err = cfe_iocb_dispatch(&iocb);
233
234 return (CFE_ISERR(err)) ? err : iocb.status;
235 }
236
237 int cfe_enummem(int idx, int flags, u64 *start, u64 *length,
238 u64 *type)
239 {
240 struct cfe_iocb iocb;
241 int err;
242
243 memset(&iocb, 0, sizeof(iocb));
244
245 iocb.fcode = CFE_CMD_FW_MEMENUM;
246 iocb.flags = flags;
247 iocb.psize = sizeof(struct cfe_iocb_meminfo);
248 iocb.meminfo.index = idx;
249
250 err = cfe_iocb_dispatch(&iocb);
251 if (CFE_ISERR(err))
252 return err;
253 if (!CFE_ISERR(iocb.status)) {
254 *start = iocb.meminfo.addr;
255 *length = iocb.meminfo.size;
256 *type = iocb.meminfo.type;
257 }
258
259 return iocb.status;
260 }
261
262 int cfe_exit(int warm, int status)
263 {
264 struct cfe_iocb iocb;
265 int err;
266
267 printk("CFE REBOOT\n");
268 memset(&iocb, 0, sizeof(iocb));
269 iocb.fcode = CFE_CMD_FW_RESTART;
270 if (warm)
271 iocb.flags = CFE_FLG_WARMSTART;
272 iocb.psize = sizeof(struct cfe_iocb_exitstat);
273 iocb.exitstat.status = status;
274
275 printk("CALL\n");
276 err = cfe_iocb_dispatch(&iocb);
277 printk("DONE\n");
278
279 return (CFE_ISERR(err)) ? err : iocb.status;
280 }
281
282 int cfe_flushcache(int flags)
283 {
284 struct cfe_iocb iocb;
285 int err;
286
287 memset(&iocb, 0, sizeof(iocb));
288 iocb.fcode = CFE_CMD_FW_FLUSHCACHE;
289 iocb.flags = flags;
290
291 err = cfe_iocb_dispatch(&iocb);
292
293 return (CFE_ISERR(err)) ? err : iocb.status;
294 }
295
296 int cfe_getdevinfo(char *name)
297 {
298 struct cfe_iocb iocb;
299 int err;
300
301 memset(&iocb, 0, sizeof(iocb));
302 iocb.fcode = CFE_CMD_DEV_GETINFO;
303 iocb.psize = sizeof(struct cfe_iocb_buf);
304 iocb.buffer.ptr = PTR_TO_CFE(name);
305 iocb.buffer.length = strlen(name);
306
307 err = cfe_iocb_dispatch(&iocb);
308 if (CFE_ISERR(err))
309 return err;
310 if (CFE_ISERR(iocb.status))
311 return iocb.status;
312
313 return iocb.buffer.devflags;
314 }
315
316 int cfe_getenv(char *name, char *dest, int destlen)
317 {
318 struct cfe_iocb iocb;
319 int err;
320
321 dest[0] = '\0';
322 memset(&iocb, 0, sizeof(iocb));
323 iocb.fcode = CFE_CMD_ENV_GET;
324 iocb.psize = sizeof(struct cfe_iocb_envbuf);
325 iocb.envbuf.name = PTR_TO_CFE(name);
326 iocb.envbuf.name_len = strlen(name);
327 iocb.envbuf.val = PTR_TO_CFE(dest);
328 iocb.envbuf.val_len = destlen;
329
330 err = cfe_iocb_dispatch(&iocb);
331
332 return (CFE_ISERR(err)) ? err : iocb.status;
333 }
334
335 int cfe_getfwinfo(struct cfe_fwinfo *info)
336 {
337 struct cfe_iocb iocb;
338 int err;
339
340 memset(&iocb, 0, sizeof(iocb));
341 iocb.fcode = CFE_CMD_FW_GETINFO;
342 iocb.psize = sizeof(struct cfe_iocb_fwinfo);
343
344 err = cfe_iocb_dispatch(&iocb);
345 if (CFE_ISERR(err))
346 return err;
347 if (CFE_ISERR(iocb.status))
348 return err;
349
350 info->version = iocb.fwinfo.version;
351 info->totalmem = iocb.fwinfo.totalmem;
352 info->flags = iocb.fwinfo.flags;
353 info->boardid = iocb.fwinfo.boardid;
354 info->bootarea_va = iocb.fwinfo.bootarea_va;
355 info->bootarea_pa = iocb.fwinfo.bootarea_pa;
356 info->bootarea_size = iocb.fwinfo.bootarea_size;
357
358 return iocb.status;
359 }
360
361 int cfe_getstdhandle(int handletype)
362 {
363 struct cfe_iocb iocb;
364 int err;
365
366 memset(&iocb, 0, sizeof(iocb));
367 iocb.fcode = CFE_CMD_DEV_GETHANDLE;
368 iocb.flags = handletype;
369
370 err = cfe_iocb_dispatch(&iocb);
371 if (CFE_ISERR(err))
372 return err;
373 if (CFE_ISERR(iocb.status))
374 return iocb.status;
375
376 return iocb.handle;
377 }
378
379 int cfe_getticks(s64 *ticks)
380 {
381 struct cfe_iocb iocb;
382 int err;
383
384 memset(&iocb, 0, sizeof(iocb));
385 iocb.fcode = CFE_CMD_FW_GETTIME;
386 iocb.psize = sizeof(struct cfe_iocb_time);
387
388 err = cfe_iocb_dispatch(&iocb);
389 if (CFE_ISERR(err))
390 return err;
391 if (!CFE_ISERR(iocb.status))
392 *ticks = iocb.time.ticks;
393
394 return iocb.status;
395 }
396
397 int cfe_inpstat(int handle)
398 {
399 struct cfe_iocb iocb;
400 int err;
401
402 memset(&iocb, 0, sizeof(iocb));
403 iocb.fcode = CFE_CMD_DEV_INPSTAT;
404 iocb.handle = handle;
405 iocb.psize = sizeof(struct cfe_iocb_inpstat);
406
407 err = cfe_iocb_dispatch(&iocb);
408 if (CFE_ISERR(err))
409 return err;
410 if (CFE_ISERR(iocb.status))
411 return iocb.status;
412
413 return iocb.inpstat.status;
414 }
415
416 int cfe_ioctl(int handle, unsigned int ioctlnum,
417 unsigned char *buffer, int length,
418 int *retlen, u64 offset)
419 {
420 struct cfe_iocb iocb;
421 int err;
422
423 memset(&iocb, 0, sizeof(iocb));
424 iocb.fcode = CFE_CMD_DEV_IOCTL;
425 iocb.handle = handle;
426 iocb.psize = sizeof(struct cfe_iocb_buf);
427 iocb.buffer.offset = offset;
428 iocb.buffer.ioctlcmd = ioctlnum;
429 iocb.buffer.ptr = PTR_TO_CFE(buffer);
430 iocb.buffer.length = length;
431
432 err = cfe_iocb_dispatch(&iocb);
433 if (CFE_ISERR(err))
434 return err;
435 if (CFE_ISERR(iocb.status))
436 return iocb.status;
437 if (retlen)
438 *retlen = iocb.buffer.retlen;
439
440 return iocb.status;
441 }
442
443 int cfe_open(char *name)
444 {
445 struct cfe_iocb iocb;
446 int err;
447
448 memset(&iocb, 0, sizeof(iocb));
449 iocb.fcode = CFE_CMD_DEV_OPEN;
450 iocb.psize = sizeof(struct cfe_iocb_buf);
451 iocb.buffer.ptr = PTR_TO_CFE(name);
452 iocb.buffer.length = strlen(name);
453
454 err = cfe_iocb_dispatch(&iocb);
455 if (CFE_ISERR(err))
456 return err;
457 if (CFE_ISERR(iocb.status))
458 return iocb.status;
459
460 return iocb.handle;
461 }
462
463 int cfe_read(int handle, unsigned char *buffer, int length)
464 {
465 return cfe_readblk(handle, 0, buffer, length);
466 }
467
468 int cfe_readblk(int handle, s64 offset, unsigned char *buffer, int length)
469 {
470 struct cfe_iocb iocb;
471 int err;
472
473 memset(&iocb, 0, sizeof(iocb));
474 iocb.fcode = CFE_CMD_DEV_READ;
475 iocb.handle = handle;
476 iocb.psize = sizeof(struct cfe_iocb_buf);
477 iocb.buffer.offset = offset;
478 iocb.buffer.ptr = PTR_TO_CFE(buffer);
479 iocb.buffer.length = length;
480
481 err = cfe_iocb_dispatch(&iocb);
482 if (CFE_ISERR(err))
483 return err;
484 if (CFE_ISERR(iocb.status))
485 return iocb.status;
486
487 return iocb.buffer.retlen;
488 }
489
490 int cfe_setenv(char *name, char *val)
491 {
492 struct cfe_iocb iocb;
493 int err;
494
495 memset(&iocb, 0, sizeof(iocb));
496 iocb.fcode = CFE_CMD_ENV_SET;
497 iocb.psize = sizeof(struct cfe_iocb_envbuf);
498 iocb.envbuf.name = PTR_TO_CFE(name);
499 iocb.envbuf.name_len = strlen(name);
500 iocb.envbuf.val = PTR_TO_CFE(val);
501 iocb.envbuf.val_len = strlen(val);
502
503 err = cfe_iocb_dispatch(&iocb);
504
505 return (CFE_ISERR(err)) ? err : iocb.status;
506 }
507
508 int cfe_write(int handle, unsigned char *buffer, int length)
509 {
510 return cfe_writeblk(handle, 0, buffer, length);
511 }
512
513 int cfe_writeblk(int handle, s64 offset, unsigned char *buffer, int length)
514 {
515 struct cfe_iocb iocb;
516 int err;
517
518 memset(&iocb, 0, sizeof(iocb));
519 iocb.fcode = CFE_CMD_DEV_WRITE;
520 iocb.handle = handle;
521 iocb.psize = sizeof(struct cfe_iocb_buf);
522 iocb.buffer.offset = offset;
523 iocb.buffer.ptr = PTR_TO_CFE(buffer);
524 iocb.buffer.length = length;
525
526 err = cfe_iocb_dispatch(&iocb);
527 if (CFE_ISERR(err))
528 return err;
529 if (CFE_ISERR(iocb.status))
530 return iocb.status;
531
532 return iocb.buffer.retlen;
533 }
This page took 0.069778 seconds and 3 git commands to generate.