1 /*        $NetBSD: sh_boot.cpp,v 1.10 2008/04/28 20:23:20 martin Exp $          */
2 
3 /*-
4  * Copyright (c) 2001, 2002, 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by UCHIYAMA Yasushi.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <hpcboot.h>
33 
34 #include <arch.h>
35 #include <memory.h>
36 
37 #include <sh3/sh_arch.h>
38 #include <sh3/sh_mmu.h>
39 #include <sh3/sh_console.h>
40 #include <sh3/sh_boot.h>
41 
SHBoot()42 SHBoot::SHBoot()
43 {
44 }
45 
~SHBoot()46 SHBoot::~SHBoot()
47 {
48           if (_mem)
49                     delete _mem;
50           if (_arch)
51                     delete _arch;
52 
53           SHConsole::Destroy();
54 }
55 
56 BOOL
setup()57 SHBoot::setup()
58 {
59           struct HpcMenuInterface::HpcMenuPreferences &pref = HPC_PREFERENCE;
60 
61           platid_t platid;
62           platid.dw.dw0 = pref.platid_hi;
63           platid.dw.dw1 = pref.platid_lo;
64 
65           if (platid_match(&platid, &platid_mask_CPU_SH_3_7707)) {
66                     args.architecture = ARCHITECTURE_SH3_7707;
67           } else if (platid_match(&platid, &platid_mask_CPU_SH_3_7709)) {
68                     args.architecture = ARCHITECTURE_SH3_7709;
69           } else if (platid_match(&platid, &platid_mask_CPU_SH_3_7709A)) {
70                     args.architecture = ARCHITECTURE_SH3_7709A;
71           } else if (platid_match(&platid, &platid_mask_CPU_SH_4_7750)) {
72                     args.architecture = ARCHITECTURE_SH4_7750;
73           } else {
74                     DPRINTF((TEXT("CPU not supported.")));
75                     return FALSE;
76           }
77 
78           return super::setup();
79 }
80 
81 BOOL
create()82 SHBoot::create()
83 {
84           BOOL(*lock_pages)(LPVOID, DWORD, PDWORD, int);
85           BOOL(*unlock_pages)(LPVOID, DWORD);
86           size_t page_size;
87 
88           // Setup console. this setting is passed to kernel bootinfo.
89           if (args.console == CONSOLE_SERIAL) {
90                     _cons = new SHConsole;
91                     if (!_cons->init()) {
92                               _cons = Console::Instance();
93                               DPRINTF((TEXT("use LCD console instead.\n")));
94                     }
95           } else {
96                     _cons = Console::Instance();
97                     SHConsole::selectBootConsole(*_cons, SHConsole::VIDEO);
98           }
99 
100           // Architecture dependent ops.
101           switch(args.architecture) {
102           default:
103                     DPRINTF((TEXT("unsupported architecture.\n")));
104                     return FALSE;
105           case ARCHITECTURE_SH3_7707:
106                     _arch = new SH7707(_cons, _mem, SH7707::boot_func);
107                     page_size = SH3_PAGE_SIZE;
108                     if (SHArchitecture::cpu_type() != 3)
109                               goto cpu_mismatch;
110                     break;
111           case ARCHITECTURE_SH3_7709:
112                     _arch = new SH7709(_cons, _mem, SH7709::boot_func);
113                     page_size = SH3_PAGE_SIZE;
114                     if (SHArchitecture::cpu_type() != 3)
115                               goto cpu_mismatch;
116                     break;
117           case ARCHITECTURE_SH3_7709A:
118                     _arch = new SH7709A(_cons, _mem, SH7709A::boot_func);
119                     page_size = SH3_PAGE_SIZE;
120                     if (SHArchitecture::cpu_type() != 3)
121                               goto cpu_mismatch;
122                     break;
123           case ARCHITECTURE_SH4_7750:
124                     _arch = new SH7750(_cons, _mem, SH7750::boot_func);
125                     page_size = SH4_PAGE_SIZE;
126                     if (SHArchitecture::cpu_type() != 4)
127                               goto cpu_mismatch;
128                     break;
129           }
130           _arch->setDebug() = args.architectureDebug;
131 
132           lock_pages = _arch->_load_LockPages();
133           unlock_pages = _arch->_load_UnlockPages();
134           if (lock_pages == 0 || unlock_pages == 0)
135                     args.memory = MEMORY_MANAGER_HARDMMU;
136           else
137                     args.memory = MEMORY_MANAGER_LOCKPAGES;
138 
139           // Memory manager.
140           switch(args.memory) {
141           default:
142           case MEMORY_MANAGER_VIRTUALCOPY:
143                     // VirtualCopy method causes Windows CE unstable.
144                     /* FALLTHROUGH */
145           case MEMORY_MANAGER_SOFTMMU:
146                     DPRINTF((TEXT("unsupported address detection method.\n")));
147                     return FALSE;
148           case MEMORY_MANAGER_HARDMMU:
149                     if (args.architecture == ARCHITECTURE_SH4_7750) {
150                               DPRINTF((TEXT("No SH4 MMU code.\n")));
151                               return FALSE;
152                     }
153 
154                     _mem = new MemoryManager_SHMMU(_cons, page_size);
155                     break;
156           case MEMORY_MANAGER_LOCKPAGES:
157                     _mem = new MemoryManager_LockPages(lock_pages, unlock_pages,
158                         _cons, page_size);
159                     break;
160           }
161           _mem->setDebug() = args.memorymanagerDebug;
162 
163           // File Manager, Loader
164           return super::create();
165 
166  cpu_mismatch:
167           DPRINTF((TEXT("CPU mismatch. CPU is SH%d\n"),
168               SHArchitecture::cpu_type()));
169 
170           return FALSE;
171 }
172