blob: 5b2985a7c27fe6a56304e6f750a945a1b3f22f9e [file] [log] [blame]
huang lina6dbfb52016-03-02 18:38:40 +08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2016 Rockchip Inc.
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#include <boardid.h>
17#include <console/console.h>
Julius Werner41776792017-12-04 16:47:53 -080018#include <gpio.h>
huang lina6dbfb52016-03-02 18:38:40 +080019#include <stdlib.h>
Vadim Bendebury93649b12016-04-22 07:03:43 -070020#include <soc/saradc.h>
21
Julius Werner916fc802017-12-14 18:03:45 -080022static const int id_readings[] = {
23/* ID : Volts : ADC value : Bucket */
24/* == ===== ========= ========== */
Julius Werner621b0222017-12-14 18:12:56 -080025#if IS_ENABLED(CONFIG_BOARD_GOOGLE_KEVIN)
26/* 0 : 0.109V: 62 : 0 - 91 */ 91,
27#else
Julius Werner916fc802017-12-14 18:03:45 -080028/* 0 : 0.074V: 42 : 0 - 81 */ 81,
Julius Werner621b0222017-12-14 18:12:56 -080029#endif
Julius Werner916fc802017-12-14 18:03:45 -080030/* 1 : 0.211V: 120 : 82 - 150 */ 150,
31/* 2 : 0.319V: 181 : 151 - 211 */ 211,
32/* 3 : 0.427V: 242 : 212 - 274 */ 274,
33/* 4 : 0.542V: 307 : 275 - 342 */ 342,
34/* 5 : 0.666V: 378 : 343 - 411 */ 411,
35/* 6 : 0.781V: 444 : 412 - 477 */ 477,
36/* 7 : 0.900V: 511 : 478 - 545 */ 545,
37/* 8 : 1.023V: 581 : 546 - 613 */ 613,
38/* 9 : 1.137V: 646 : 614 - 675 */ 675,
39/* 10 : 1.240V: 704 : 676 - 733 */ 733,
40/* 11 : 1.343V: 763 : 734 - 795 */ 795,
41/* 12 : 1.457V: 828 : 796 - 861 */ 861,
42/* 13 : 1.576V: 895 : 862 - 925 */ 925,
43/* 14 : 1.684V: 956 : 926 - 989 */ 989,
44/* 15 : 1.800V: 1023 : 990 - 1023 */ 1023
45};
Julius Werner0b2cf172016-08-01 14:33:26 -070046_Static_assert(ARRAY_SIZE(id_readings) == 16, "Yo' messed up da table, bruh!");
Shelley Chen5d49b4a2016-06-27 18:21:34 -070047static int cached_board_id = -1;
48static int cached_ram_id = -1;
Vadim Bendebury93649b12016-04-22 07:03:43 -070049
Shelley Chen5d49b4a2016-06-27 18:21:34 -070050static uint32_t get_index(uint32_t channel, int *cached_id)
huang lina6dbfb52016-03-02 18:38:40 +080051{
Vadim Bendebury93649b12016-04-22 07:03:43 -070052 int i;
53 int adc_reading;
54
Shelley Chen5d49b4a2016-06-27 18:21:34 -070055 if (*cached_id != -1)
56 return *cached_id;
Vadim Bendebury93649b12016-04-22 07:03:43 -070057
Shelley Chen5d49b4a2016-06-27 18:21:34 -070058 adc_reading = get_saradc_value(channel);
59 for (i = 0; i < ARRAY_SIZE(id_readings); i++) {
60 if (adc_reading <= id_readings[i]) {
61 printk(BIOS_DEBUG, "ADC reading %d, ID %d\n",
62 adc_reading, i);
63 *cached_id = i;
Vadim Bendebury93649b12016-04-22 07:03:43 -070064 return i;
65 }
66 }
67
Elyes HAOUASa342f392018-10-17 10:56:26 +020068 die("Read impossible value (> 1023) from 10-bit ADC!");
huang lina6dbfb52016-03-02 18:38:40 +080069}
Shelley Chen5d49b4a2016-06-27 18:21:34 -070070
Julius Wernere2f17f72017-12-05 13:39:10 -080071uint32_t board_id(void)
Shelley Chen5d49b4a2016-06-27 18:21:34 -070072{
73 return get_index(1, &cached_board_id);
74}
75
76uint32_t ram_code(void)
77{
78 return get_index(0, &cached_ram_id);
79}
Julius Werner41776792017-12-04 16:47:53 -080080
81uint32_t sku_id(void)
82{
83 if (!IS_ENABLED(CONFIG_GRU_BASEBOARD_SCARLET))
84 return UNDEFINED_STRAPPING_ID;
85
86 static uint32_t sku_id = UNDEFINED_STRAPPING_ID;
87 if (sku_id != UNDEFINED_STRAPPING_ID)
88 return sku_id;
89
90 gpio_t pins[3] = {[2] = GPIO(3, D, 6), [1] = GPIO(3, D, 5),
91 [0] = GPIO(3, D, 4)}; /* GPIO3_D4 is LSB */
Julius Werneref935f02017-12-07 12:59:33 -080092
93 sku_id = gpio_pullup_base2_value(pins, ARRAY_SIZE(pins));
Julius Werner41776792017-12-04 16:47:53 -080094 return sku_id;
95}