huang lin | a6dbfb5 | 2016-03-02 18:38:40 +0800 | [diff] [blame] | 1 | /* |
| 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 Werner | 4177679 | 2017-12-04 16:47:53 -0800 | [diff] [blame] | 18 | #include <gpio.h> |
huang lin | a6dbfb5 | 2016-03-02 18:38:40 +0800 | [diff] [blame] | 19 | #include <stdlib.h> |
Vadim Bendebury | 93649b1 | 2016-04-22 07:03:43 -0700 | [diff] [blame] | 20 | #include <soc/saradc.h> |
| 21 | |
Julius Werner | 916fc80 | 2017-12-14 18:03:45 -0800 | [diff] [blame] | 22 | static const int id_readings[] = { |
| 23 | /* ID : Volts : ADC value : Bucket */ |
| 24 | /* == ===== ========= ========== */ |
Julius Werner | 621b022 | 2017-12-14 18:12:56 -0800 | [diff] [blame] | 25 | #if IS_ENABLED(CONFIG_BOARD_GOOGLE_KEVIN) |
| 26 | /* 0 : 0.109V: 62 : 0 - 91 */ 91, |
| 27 | #else |
Julius Werner | 916fc80 | 2017-12-14 18:03:45 -0800 | [diff] [blame] | 28 | /* 0 : 0.074V: 42 : 0 - 81 */ 81, |
Julius Werner | 621b022 | 2017-12-14 18:12:56 -0800 | [diff] [blame] | 29 | #endif |
Julius Werner | 916fc80 | 2017-12-14 18:03:45 -0800 | [diff] [blame] | 30 | /* 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 Werner | 0b2cf17 | 2016-08-01 14:33:26 -0700 | [diff] [blame] | 46 | _Static_assert(ARRAY_SIZE(id_readings) == 16, "Yo' messed up da table, bruh!"); |
Shelley Chen | 5d49b4a | 2016-06-27 18:21:34 -0700 | [diff] [blame] | 47 | static int cached_board_id = -1; |
| 48 | static int cached_ram_id = -1; |
Vadim Bendebury | 93649b1 | 2016-04-22 07:03:43 -0700 | [diff] [blame] | 49 | |
Shelley Chen | 5d49b4a | 2016-06-27 18:21:34 -0700 | [diff] [blame] | 50 | static uint32_t get_index(uint32_t channel, int *cached_id) |
huang lin | a6dbfb5 | 2016-03-02 18:38:40 +0800 | [diff] [blame] | 51 | { |
Vadim Bendebury | 93649b1 | 2016-04-22 07:03:43 -0700 | [diff] [blame] | 52 | int i; |
| 53 | int adc_reading; |
| 54 | |
Shelley Chen | 5d49b4a | 2016-06-27 18:21:34 -0700 | [diff] [blame] | 55 | if (*cached_id != -1) |
| 56 | return *cached_id; |
Vadim Bendebury | 93649b1 | 2016-04-22 07:03:43 -0700 | [diff] [blame] | 57 | |
Shelley Chen | 5d49b4a | 2016-06-27 18:21:34 -0700 | [diff] [blame] | 58 | 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 Bendebury | 93649b1 | 2016-04-22 07:03:43 -0700 | [diff] [blame] | 64 | return i; |
| 65 | } |
| 66 | } |
| 67 | |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 68 | die("Read impossible value (> 1023) from 10-bit ADC!"); |
huang lin | a6dbfb5 | 2016-03-02 18:38:40 +0800 | [diff] [blame] | 69 | } |
Shelley Chen | 5d49b4a | 2016-06-27 18:21:34 -0700 | [diff] [blame] | 70 | |
Julius Werner | e2f17f7 | 2017-12-05 13:39:10 -0800 | [diff] [blame] | 71 | uint32_t board_id(void) |
Shelley Chen | 5d49b4a | 2016-06-27 18:21:34 -0700 | [diff] [blame] | 72 | { |
| 73 | return get_index(1, &cached_board_id); |
| 74 | } |
| 75 | |
| 76 | uint32_t ram_code(void) |
| 77 | { |
| 78 | return get_index(0, &cached_ram_id); |
| 79 | } |
Julius Werner | 4177679 | 2017-12-04 16:47:53 -0800 | [diff] [blame] | 80 | |
| 81 | uint32_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 Werner | ef935f0 | 2017-12-07 12:59:33 -0800 | [diff] [blame] | 92 | |
| 93 | sku_id = gpio_pullup_base2_value(pins, ARRAY_SIZE(pins)); |
Julius Werner | 4177679 | 2017-12-04 16:47:53 -0800 | [diff] [blame] | 94 | return sku_id; |
| 95 | } |