1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Carl-Daniel Hailfinger
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 /*
17 * Contains the processor specific flash enables and system settings.
18 */
19
20 #include "flash.h"
21 #include "programmer.h"
22
23 #if defined (__MIPSEL__) && defined (__linux)
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
27
is_loongson(void)28 static int is_loongson(void)
29 {
30 FILE *cpuinfo;
31 cpuinfo = fopen("/proc/cpuinfo", "rb");
32 if (!cpuinfo)
33 return 0;
34 while (!feof(cpuinfo)) {
35 char line[512], *ptr;
36 if (fgets(line, sizeof(line), cpuinfo) == NULL)
37 break;
38 ptr = line;
39 while (*ptr && isspace((unsigned char)*ptr))
40 ptr++;
41 /* "cpu" part appears only with some Linux versions. */
42 if (strncmp(ptr, "cpu", strlen("cpu")) == 0)
43 ptr += strlen("cpu");
44 while (*ptr && isspace((unsigned char)*ptr))
45 ptr++;
46 if (strncmp(ptr, "model", strlen("model")) != 0)
47 continue;
48 ptr += strlen("model");
49 while (*ptr && isspace((unsigned char)*ptr))
50 ptr++;
51 if (*ptr != ':')
52 continue;
53 ptr++;
54 while (*ptr && isspace((unsigned char)*ptr))
55 ptr++;
56 (void)fclose(cpuinfo);
57 return (strncmp(ptr, "ICT Loongson-2 V0.3", strlen("ICT Loongson-2 V0.3")) == 0) ||
58 (strncmp(ptr, "Godson2 V0.3 FPU V0.1", strlen("Godson2 V0.3 FPU V0.1")) == 0);
59 }
60 (void)fclose(cpuinfo);
61 return 0;
62 }
63 #endif
64
processor_flash_enable(void)65 int processor_flash_enable(void)
66 {
67 /* Default to 1 to catch not implemented architectures. */
68 int ret = 1;
69
70 /* FIXME: detect loongson on FreeBSD and OpenBSD as well. */
71 #if defined (__MIPSEL__) && defined (__linux)
72 if (is_loongson()) {
73 flashbase = 0x1fc00000;
74 ret = 0;
75 }
76 #elif defined(__i386__) || defined(__x86_64__)
77 /* On x86, flash access is not processor specific except on
78 * AMD Elan SC520, AMD Geode and maybe other SoC-style CPUs.
79 * FIXME: Move enable_flash_cs5536 and get_flashbase_sc520 here.
80 */
81 ret = 0;
82 #endif
83 return ret;
84 }
85