1/*  *********************************************************************
2    *  Broadcom Common Firmware Environment (CFE)
3    *
4    *  SB1250 PCI Device-mode downloader	File: cfe_device_ldr.c
5    *
6    *  This invokes the CFE loader to retrieve an elf binary from the
7    *  PCI host using the host0 device.
8    *
9    *********************************************************************
10    *
11    *  Copyright 2000,2001,2002,2003
12    *  Broadcom Corporation. All rights reserved.
13    *
14    *  This software is furnished under license and may be used and
15    *  copied only in accordance with the following terms and
16    *  conditions.  Subject to these conditions, you may download,
17    *  copy, install, use, modify and distribute modified or unmodified
18    *  copies of this software in source and/or binary form.  No title
19    *  or ownership is transferred hereby.
20    *
21    *  1) Any source code used, modified or distributed must reproduce
22    *     and retain this copyright notice and list of conditions
23    *     as they appear in the source file.
24    *
25    *  2) No right is granted to use any trade name, trademark, or
26    *     logo of Broadcom Corporation.  The "Broadcom Corporation"
27    *     name may not be used to endorse or promote products derived
28    *     from this software without the prior written permission of
29    *     Broadcom Corporation.
30    *
31    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
32    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
33    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
34    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
35    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
36    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
37    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
39    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
40    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
41    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
42    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
43    *     THE POSSIBILITY OF SUCH DAMAGE.
44    ********************************************************************* */
45
46#undef SIBYTE_HDR_FEATURES		/* not the right thing to do! */
47
48#include "cfe.h"
49#include "sbmips.h"
50
51#include "cfe_loader.h"
52#include "sb1250_defs.h"
53#include "sb1250_regs.h"
54#define K1_PTR64(pa) ((uint64_t *)(PHYS_TO_K1(pa)))
55
56extern cfe_loadargs_t cfe_loadargs;
57
58#define MBOX_ARG_WORD  0
59#define MBOX_CMD_WORD  1
60
61int cfe_device_download(int boot, char *options);
62int
63cfe_device_download(int boot, char *options)
64{
65    cfe_loadargs_t *la = &cfe_loadargs;
66    int res;
67    uint64_t *mbox_p = K1_PTR64 (A_IMR_REGISTER(0, R_IMR_MAILBOX_CPU));
68    uint64_t *mbox_clr = K1_PTR64 (A_IMR_REGISTER(0, R_IMR_MAILBOX_CLR_CPU));
69    uint64_t *mbox_set = K1_PTR64 (A_IMR_REGISTER(0, R_IMR_MAILBOX_SET_CPU));
70    uint32_t *cmd_p = ((uint32_t *)mbox_p) + MBOX_CMD_WORD;
71
72    xprintf("Loader:elf Filesys:raw Dev:host0");
73
74    /* Wait for the host to supply any host-selected options.  There
75       is no timeout here because we don't know the host's timing and,
76       on a real device card, probably wouldn't have anything to do
77       until downloaded. */
78
79    while ((*cmd_p & 0x3) == 0) {
80	POLL();
81	}
82
83    /* Fill out the loadargs */
84
85    la->la_flags = LOADFLG_NOISY;
86
87    la->la_device = "host0";
88    la->la_filesys = "raw";
89    la->la_filename = NULL;
90
91    if ((*cmd_p & 0x2) != 0) la->la_flags |= LOADFLG_COMPRESSED;
92    la->la_options = options;
93
94    xprintf(" Options:%s\n", la->la_options);
95
96    res = cfe_boot("elf", la);
97
98    /* First supply the result code */
99    *((uint32_t *)mbox_clr + MBOX_ARG_WORD) = 0xffffffff;
100    *((uint32_t *)mbox_set + MBOX_ARG_WORD) = (uint32_t)res;
101
102    /* Then the ack (0x0) */
103    *((uint32_t *)mbox_clr + MBOX_CMD_WORD) = 0xffffff;
104
105    if (res < 0) {
106	xprintf("Could not download from %s:\n", la->la_device);
107	return res;
108	}
109
110    if (boot && la->la_entrypt != 0) {
111	cfe_go(la);
112	/* We won't actually return to here */
113	}
114
115    return res;
116}
117