summaryrefslogtreecommitdiff
path: root/src/mainboard/momentum/apache/boardutil.c
blob: 370d1a47e5677a0f1dd85658a18c991116b86d8a (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
/*
 * Copyright (C) 2003, Greg Watson <gwatson@lanl.gov>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 * MA 02110-1301 USA
 */

/*
 * Do very early board initialization:
 *
 * - Configure External Bus (EBC)
 * - Setup Flash
 * - Setup NVRTC
 * - Setup Board Control and Status Registers (BCSR)
 * - Enable UART0 for debugging
 */

#include "boardutil.h"
#include "ppc970lib.h"
#include "ppc970.h"
#include "stddef.h"
#include "string.h"

/*----------------------------------------------------------------------------+
| What_platform.
+----------------------------------------------------------------------------*/
int what_platform()
{

   #ifdef PPC970FX_EVB_LITE
      return(PLATFORM_EVB_LITE);
   #endif
   #ifdef PPC970FX_EVB
      return(PLATFORM_EVB_FINAL);
   #endif
}

/*----------------------------------------------------------------------------+
| Get_system_info.  Cannot access any global variables in this function.
+----------------------------------------------------------------------------*/
void get_system_info(board_cfg_data_t *board_cfg)
{

   unsigned long	msr;
   unsigned long	data;

   if (board_cfg==NULL) {
      (void)ppcHalt();
   }
   msr=ppcAndMsr((unsigned long)~MSR_EE);
   board_cfg->usr_config_ver[0]='1';
   board_cfg->usr_config_ver[1]='.';
   board_cfg->usr_config_ver[2]='0';
   board_cfg->usr_config_ver[3]='\0';
   /*-------------------------------------------------------------------------+
   | Read power status register.
   +-------------------------------------------------------------------------*/
   data=read_psr()&SCOM_PSR_FREQ_MASK;
   if (data==SCOM_PSR_FREQ_FULL) {
      board_cfg->freq_ratio=1;
   } else if (data==SCOM_PSR_FREQ_HALF) {
      board_cfg->freq_ratio=2;
   } else if (data==SCOM_PSR_FREQ_QUARTER) {
      board_cfg->freq_ratio=4;
   } else {
      board_cfg->freq_ratio=0;
   }
   /*-------------------------------------------------------------------------+
   | Read information passed from service processor.
   +-------------------------------------------------------------------------*/
   if (get_ei_ratio(&data)==0) {
      board_cfg->ei_ratio=data;
   } else {
      board_cfg->ei_ratio=0;
   }
   if (get_sys_clk(&data)==0) {
      board_cfg->sys_freq=data;
   } else {
      board_cfg->sys_freq=0;
   }
   if (get_pll_mult(&data)==0) {
      if (board_cfg->freq_ratio!=0) {
         board_cfg->cpu_freq=(board_cfg->sys_freq* data)/ board_cfg->freq_ratio;
      } else {
         board_cfg->cpu_freq=0;
      }
   } else {
      data=0;
      board_cfg->cpu_freq=0;
   }
   /*-------------------------------------------------------------------------+
   | On some boards we have to execute with timers running on internal clock.
   +-------------------------------------------------------------------------*/
   if ((ppcMfspr_any(SPR_HID0)&HID0_EXT_TB_EN)==0) {
      board_cfg->tmr_freq=(board_cfg->sys_freq* data)/ PPC970_TB_RATIO;
   } else {
      board_cfg->tmr_freq=EXT_TIME_BASE_FREQ;
   }
   /*-------------------------------------------------------------------------+
   | If the above calculation did not yield valid timer speed try to estimate
   | it.
   +-------------------------------------------------------------------------*/
   if (board_cfg->tmr_freq==0) {
      board_cfg->tmr_freq=timebase_speed_calc(UART1_MMIO_BASE);
   }
   /*-------------------------------------------------------------------------+
   | Read information passed from service processor.
   +-------------------------------------------------------------------------*/
   board_cfg->mem_size=sdram_size();
   /*-------------------------------------------------------------------------+
   | Assign rest of the information.
   +-------------------------------------------------------------------------*/
   board_cfg->ser_freq=UART_INPUT_CLOCK;
   board_cfg->procver=ppcMfspr_any(SPR_PVR);
   board_cfg->hid0=ppcMfspr_any(SPR_HID0);
   board_cfg->hid1=ppcMfspr_any(SPR_HID1);
   board_cfg->hid4=ppcMfspr_any(SPR_HID4);
   board_cfg->hid5=ppcMfspr_any(SPR_HID5);
   board_cfg->hior=ppcMfspr_any(SPR_HIOR);
   board_cfg->sdr1=ppcMfspr_any(SPR_SDR1);
   board_cfg->procstr[0]='9';
   board_cfg->procstr[1]='7';
   board_cfg->procstr[2]='0';
   board_cfg->procstr[3]='F';
   board_cfg->procstr[4]='X';
   board_cfg->procstr[5]='\0';
   board_cfg->reserved[0]='\0';
   (void)get_hwd_addr((char *)board_cfg->hwaddr0, 0);
   (void)ppcMtmsr(msr);
   return;
}

/*----------------------------------------------------------------------------+
| Get_hwd_addr.
+----------------------------------------------------------------------------*/
int get_hwd_addr(char *dest,
    int ethernet_num)
{

   bios_data_struct_t   *bios_data;
   char                 *src;
   unsigned char        nc;
   int                  len;
   int                  num;

   bios_data=(bios_data_struct_t *)PIBS_DATABASE_ADDR;
   if (ethernet_num!=0) {
      for(len=0;len<ETHERNET_HW_ADDR_LEN;len++) {
         dest[len]=(char)0xFF;
      }
      return(-1);
   } else {
      src=bios_data->bios_eth_hwd0;
   }
   len=0;
   while((src[len]!='\0') && (len<(ETHERNET_HW_ADDR_LEN* 3))) {
      len++;
   }
   if (len!=(ETHERNET_HW_ADDR_LEN* 2)) {
      for(len=0;len<ETHERNET_HW_ADDR_LEN;len++) {
         dest[len]=(char)0xFF;
      }
      return(-1);
   }
   for(len=0;len<(ETHERNET_HW_ADDR_LEN* 2);len++) {
      nc=toupper((int)src[len]);
      if ((nc>='0') && (nc<='9')) {
         num=nc- '0';
      } else if ((nc>='A') && (nc<='F')) {
         num=nc- 'A'+ 0xA;
      } else {
         for(len=0;len<ETHERNET_HW_ADDR_LEN;len++) {
            dest[len]=(char)0xFF;
         }
         return(-1);
      }
      if ((len%2)==0) {
         dest[len/ 2]=(char)num;
      } else {
         dest[len/ 2]=(char)((dest[len/ 2]* 0x10)+ num);
      }
   }
   return(0);
}

/*----------------------------------------------------------------------------+
| Get_sys_clk.
+----------------------------------------------------------------------------*/
int get_sys_clk(unsigned long *value)
{

   unsigned long	data;

   if (read_sp_data(SUPER_IO_NVRAM_DATA_VALID, 4, &data)!=0) {
      return(-1);
   }
   if (data!=SUPER_IO_VALID_VALUE) {
      return(-1);
   }
   if (read_sp_data(SUPER_IO_NVRAM_SYS_CLK, 4, &data)!=0) {
      return(-1);
   }
   *value=data;
   return(0);
}

/*----------------------------------------------------------------------------+
| Get_pll_mult.
+----------------------------------------------------------------------------*/
int get_pll_mult(unsigned long *value)
{

   unsigned long	data;

   if (read_sp_data(SUPER_IO_NVRAM_DATA_VALID, 4, &data)!=0) {
      return(-1);
   }
   if (data!=SUPER_IO_VALID_VALUE) {
      return(-1);
   }
   if (read_sp_data(SUPER_IO_NVRAM_CLK_MULT, 1, value)!=0) {
      return(-1);
   }
   return(0);
}

/*----------------------------------------------------------------------------+
| Get_ei_ratio.
+----------------------------------------------------------------------------*/
int get_ei_ratio(unsigned long *value)
{

   unsigned long	data;

   if (read_sp_data(SUPER_IO_NVRAM_DATA_VALID, 4, &data)!=0) {
      return(-1);
   }
   if (data!=SUPER_IO_VALID_VALUE) {
      return(-1);
   }
   if (read_sp_data(SUPER_IO_NVRAM_EI_RATIO, 1, &data)!=0) {
      return(-1);
   }

   if (data==0x0000000000000000) data=PPC970_EI_RATIO_000;
   else if (data==0x0000000000000001) data=PPC970_EI_RATIO_001;
   else if (data==0x0000000000000002) data=PPC970_EI_RATIO_010;
   else if (data==0x0000000000000003) data=PPC970_EI_RATIO_011;
   else if (data==0x0000000000000004) data=PPC970_EI_RATIO_100;
   else if (data==0x0000000000000005) data=PPC970_EI_RATIO_101;
   else if (data==0x0000000000000006) data=PPC970_EI_RATIO_110;
   else return(-1);

   *value=data;
   return(0);
}

/*----------------------------------------------------------------------------+
| Read_sp_data.
+----------------------------------------------------------------------------*/
int read_sp_data(unsigned int offset,
    unsigned int count, unsigned long *data)
{

   unsigned long	addr_index;
   unsigned long	addr_data;
   unsigned long	addr;
   unsigned int		new_data;
   unsigned int		i;

   /*-------------------------------------------------------------------------+
   | If this is not a JS20 or EVB platform then just return.
   +-------------------------------------------------------------------------*/
   if (what_platform()==PLATFORM_EVB_FINAL) {
      addr_index=NB_HT_IO_BASE_CPU+ SUPER_IO_ADDR_NVRAM;
      addr_data=NB_HT_IO_BASE_CPU+ SUPER_IO_ADDR_NVRAM+ 1;
      *data=0x0000000000000000;
      for(i=0;i<count;i++) {
         (void)outbyte(addr_index, offset+ i);
         new_data=inbyte(addr_data);
         *data|=new_data<<((count- i- 1)* 8);
      }
      return(0);
   } else if (what_platform()==PLATFORM_EVB_LITE) {
      addr=SB_NVRAM_ADDR;
      *data=0x0000000000000000;
      for(i=0;i<count;i++) {
         new_data=inbyte(addr+ i+ offset);
         *data|=new_data<<((count- i- 1)* 8);
      }
      return(0);
   }
   return(-1);
}

/*----------------------------------------------------------------------------+
| Write_sp_data.
+----------------------------------------------------------------------------*/
int write_sp_data(unsigned int offset,
    unsigned int data)
{

   unsigned long	addr_index;
   unsigned long	addr_data;
   unsigned long	addr;

   /*-------------------------------------------------------------------------+
   | If this is not a JS20 or EVB platform then just return.
   +-------------------------------------------------------------------------*/
   if (what_platform()==PLATFORM_EVB_FINAL) {
      addr_index=NB_HT_IO_BASE_CPU+ SUPER_IO_ADDR_NVRAM;
      addr_data=NB_HT_IO_BASE_CPU+ SUPER_IO_ADDR_NVRAM+ 1;
      (void)outbyte(addr_index, offset);
      (void)outbyte(addr_data, data);
      return(0);
   } else if (what_platform()==PLATFORM_EVB_LITE) {
      addr=SB_NVRAM_ADDR;
      (void)outbyte(addr+ offset, data);
      return(0);
   }
   return(-1);
}

/*----------------------------------------------------------------------------+
| Read_psr.
+----------------------------------------------------------------------------*/
unsigned long read_psr()
{

   unsigned long	msr;
   unsigned long	value;

   msr=ppcAndMsr((unsigned long)~MSR_EE);
   (void)ppcMtspr_any(SPR_SCOMC, SCOM_ADDR_PSR_READ);
   (void)ppcIsync();
   value=ppcMfspr_any(SPR_SCOMD);
   (void)ppcIsync();
   (void)ppcMtmsr(msr);
   return(value);
}

/*----------------------------------------------------------------------------+
| Write_pcr_pcrh.
+----------------------------------------------------------------------------*/
void write_pcr_pcrh(unsigned long data)
{

   unsigned long	msr;

   msr=ppcAndMsr((unsigned long)~MSR_EE);
   /*-------------------------------------------------------------------------+
   | First write to PCR with all 0 (errata).
   +-------------------------------------------------------------------------*/
   (void)ppcMtspr_any(SPR_SCOMD, SCOM_ADDR_PCR_DATA_MASK);
   (void)ppcIsync();
   (void)ppcMtspr_any(SPR_SCOMC, SCOM_ADDR_PCR_WRITE);
   (void)ppcIsync();
   /*-------------------------------------------------------------------------+
   | Write to PCRH.
   +-------------------------------------------------------------------------*/
   (void)ppcMtspr_any(SPR_SCOMD, 0x0000000000000000UL);
   (void)ppcIsync();
   (void)ppcMtspr_any(SPR_SCOMC, SCOM_ADDR_PCR_WRITE);
   (void)ppcIsync();
   /*-------------------------------------------------------------------------+
   | Write to PCR.
   +-------------------------------------------------------------------------*/
   (void)ppcMtspr_any(SPR_SCOMD, data|SCOM_ADDR_PCR_DATA_MASK);
   (void)ppcIsync();
   (void)ppcMtspr_any(SPR_SCOMC, SCOM_ADDR_PCR_WRITE);
   (void)ppcIsync();
   (void)ppcMtmsr(msr);
   return;
}

/*----------------------------------------------------------------------------+
| Is_writable.
+----------------------------------------------------------------------------*/
int is_writable(unsigned long addr,
    unsigned long len)
{

   if ((addr>=BOOT_BASE) && (addr<SDRAM_UPPER_BASE)) {
      return(0);
   }
   if ((addr+ len)>=BOOT_BASE) {
      return(0);
   }
   return(1);
}