You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
3.4 KiB
153 lines
3.4 KiB
/*
|
|
* Copyright (C) Guangzhou FriendlyARM Computer Tech. Co., Ltd.
|
|
* (http://www.friendlyarm.com)
|
|
*
|
|
* 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, you can access it online at
|
|
* http://www.gnu.org/licenses/gpl-2.0.html.
|
|
*/
|
|
|
|
#include <config.h>
|
|
#include <common.h>
|
|
#include <asm/io.h>
|
|
#include <asm/gpio.h>
|
|
|
|
/*
|
|
* ID info:
|
|
* ID : Volts : ADC value : Bucket
|
|
* == ===== ========= ===========
|
|
* 0 : 0.102V: 58 : 0 - 81
|
|
* 1 : 0.211V: 120 : 82 - 150
|
|
* 2 : 0.319V: 181 : 151 - 211
|
|
* 3 : 0.427V: 242 : 212 - 274
|
|
* 4 : 0.542V: 307 : 275 - 342
|
|
* 5 : 0.666V: 378 : 343 - 411
|
|
* 6 : 0.781V: 444 : 412 - 477
|
|
* 7 : 0.900V: 511 : 478 - 545
|
|
* 8 : 1.023V: 581 : 546 - 613
|
|
* 9 : 1.137V: 646 : 614 - 675
|
|
* 10 : 1.240V: 704 : 676 - 733
|
|
* 11 : 1.343V: 763 : 734 - 795
|
|
* 12 : 1.457V: 828 : 796 - 861
|
|
* 13 : 1.576V: 895 : 862 - 925
|
|
* 14 : 1.684V: 956 : 926 - 989
|
|
* 15 : 1.800V: 1023 : 990 - 1023
|
|
*/
|
|
static const int id_readings[] = {
|
|
81, 150, 211, 274, 342, 411, 477, 545,
|
|
613, 675, 733, 795, 861, 925, 989, 1023
|
|
};
|
|
|
|
static int cached_board_id = -1;
|
|
|
|
#define SARADC_BASE 0xFF280000
|
|
#define SARADC_DATA (SARADC_BASE + 0)
|
|
#define SARADC_CTRL (SARADC_BASE + 8)
|
|
|
|
static u32 get_saradc_value(int chn)
|
|
{
|
|
int timeout = 0;
|
|
u32 adc_value = 0;
|
|
|
|
writel(0, SARADC_CTRL);
|
|
udelay(2);
|
|
|
|
writel(0x28 | chn, SARADC_CTRL);
|
|
udelay(50);
|
|
|
|
timeout = 0;
|
|
do {
|
|
if (readl(SARADC_CTRL) & 0x40) {
|
|
adc_value = readl(SARADC_DATA) & 0x3FF;
|
|
goto stop_adc;
|
|
}
|
|
|
|
udelay(10);
|
|
} while (timeout++ < 100);
|
|
|
|
stop_adc:
|
|
writel(0, SARADC_CTRL);
|
|
|
|
return adc_value;
|
|
}
|
|
|
|
static uint32_t get_adc_index(int chn)
|
|
{
|
|
int i;
|
|
int adc_reading;
|
|
|
|
if (cached_board_id != -1)
|
|
return cached_board_id;
|
|
|
|
adc_reading = get_saradc_value(chn);
|
|
for (i = 0; i < ARRAY_SIZE(id_readings); i++) {
|
|
if (adc_reading <= id_readings[i]) {
|
|
debug("ADC reading %d, ID %d\n", adc_reading, i);
|
|
cached_board_id = i;
|
|
return i;
|
|
}
|
|
}
|
|
|
|
/* should die for impossible value */
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Board revision list: <GPIO2_B4 | GPIO2_A3>
|
|
* 0b10 - NanoPi-R2
|
|
*
|
|
* Extended by ADC_IN1
|
|
*/
|
|
static int pcb_rev = -1;
|
|
|
|
static void bd_hwrev_init(void)
|
|
{
|
|
int idx;
|
|
u32 val;
|
|
|
|
if (pcb_rev >= 0)
|
|
return;
|
|
|
|
#define GPIO2_BASE 0xff230000
|
|
struct rockchip_gpio_regs *regs = (void *)GPIO2_BASE;
|
|
|
|
/* B4, A3: input mode */
|
|
clrbits_le32(®s->swport_ddr, (1 << 12) | (1 << 3));
|
|
val = readl(®s->ext_port);
|
|
pcb_rev = ((val >> 3) & 1) << 4;
|
|
pcb_rev |= ((val >> 12) & 1) << 5;
|
|
|
|
idx = get_adc_index(1);
|
|
if (idx > 0)
|
|
pcb_rev += idx;
|
|
}
|
|
|
|
/* To override __weak symbols */
|
|
u32 get_board_rev(void)
|
|
{
|
|
return pcb_rev;
|
|
}
|
|
|
|
const char *get_board_name(void)
|
|
{
|
|
bd_hwrev_init();
|
|
|
|
switch (pcb_rev) {
|
|
case 0:
|
|
return "NanoPi R2";
|
|
default:
|
|
return "FriendlyELEC RK3328 board";
|
|
}
|
|
}
|
|
|