1 /*
2 Copyright (c) 2019, Raspberry Pi (Trading) Ltd
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holder nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32
33 #include "revision.h"
34
get_revision_code()35 static unsigned int get_revision_code()
36 {
37 FILE *fin;
38 char str[256];
39 unsigned int revision_num = 0;
40
41 fin = fopen("/proc/cpuinfo", "rt");
42
43 if (fin == NULL)
44 return 0;
45
46 while (fgets(str, sizeof(str), fin) != NULL)
47 {
48 if (sscanf(str, "Revision : %x", &revision_num) == 1)
49 break;
50 }
51
52 fclose(fin);
53
54 return revision_num;
55 }
56
57 /* Returns the type of the Pi being used
58 */
get_model_type(void)59 int get_model_type(void)
60 {
61 unsigned int revision_num = get_revision_code();
62
63 if (!revision_num)
64 return 0;
65
66 // Check for old/new style revision code. Bit 23 will be guaranteed one for new style
67 if (revision_num & 0x800000)
68 return (revision_num & 0xff0) >> 4;
69 else
70 {
71 // Mask off warrantee and overclock bits.
72 revision_num &= 0xffffff;
73
74 // Map old style to new Type code
75 if (revision_num < 2 || revision_num > 21)
76 return 0;
77
78 static const unsigned char type_map[] =
79 {
80 1, // B rev 1.0 2
81 1, // B rev 1.0 3
82 1, // B rev 2.0 4
83 1, // B rev 2.0 5
84 1, // B rev 2.0 6
85 0, // A rev 2 7
86 0, // A rev 2 8
87 0, // A rev 2 9
88 0, 0, 0, // unused a,b,c
89 1, // B rev 2.0 d
90 1, // B rev 2.0 e
91 1, // B rev 2.0 f
92 3, // B+ rev 1.2 10
93 6, // CM1 11
94 2, // A+ rev1.1 12
95 3, // B+ rev 1.2 13
96 6, // CM1 14
97 2 // A+ 15
98 };
99 return type_map[revision_num-2];
100 }
101
102 return 0;
103 }
104
105 /* Returns the type of the Pi being used
106 */
is_model_pi4(void)107 int is_model_pi4(void)
108 {
109 return get_model_type() == 0x11 ? 1 : 0;
110 }
111
112 /* returns the processor ID
113 */
get_processor_id(void)114 int get_processor_id(void)
115 {
116 unsigned int revision_num = get_revision_code();
117
118 if (revision_num & 0x800000)
119 {
120 return (revision_num & 0xf000) >> 12;
121 }
122 else
123 {
124 // Old style number only used 2835
125 return PROCESSOR_BCM2835;
126 }
127 }
128
129