b447e13461d24b9c0771e819b2836bfbc7463a8e
[oweals/u-boot.git] / board / k+p / kp_imx53 / kp_imx53.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2018
4  * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
5  */
6
7 #include <common.h>
8 #include <asm/io.h>
9 #include <asm/arch/imx-regs.h>
10 #include <asm/arch/sys_proto.h>
11 #include <asm/arch/crm_regs.h>
12 #include <asm/arch/clock.h>
13 #include <asm/arch/iomux-mx53.h>
14 #include <asm/arch/clock.h>
15 #include <asm/gpio.h>
16 #include <power/pmic.h>
17 #include <fsl_pmic.h>
18 #include "kp_id_rev.h"
19
20 #define BOOSTER_OFF IMX_GPIO_NR(2, 23)
21 #define LCD_BACKLIGHT IMX_GPIO_NR(1, 1)
22 #define KEY1 IMX_GPIO_NR(2, 26)
23
24 DECLARE_GLOBAL_DATA_PTR;
25
26 int dram_init(void)
27 {
28         u32 size;
29
30         size = get_ram_size((void *)PHYS_SDRAM_1, PHYS_SDRAM_1_SIZE);
31         gd->ram_size = size;
32
33         return 0;
34 }
35
36 int dram_init_banksize(void)
37 {
38         gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
39         gd->bd->bi_dram[0].size = PHYS_SDRAM_1_SIZE;
40
41         return 0;
42 }
43
44 static int power_init(void)
45 {
46         struct udevice *dev;
47         int ret;
48
49         ret = pmic_get("mc34708", &dev);
50         if (ret) {
51                 printf("%s: mc34708 not found !\n", __func__);
52                 return ret;
53         }
54
55         /* Set VDDGP to 1.110V for 800 MHz on SW1 */
56         pmic_clrsetbits(dev, REG_SW_0, SWx_VOLT_MASK_MC34708,
57                         SWx_1_110V_MC34708);
58
59         /* Set VCC as 1.30V on SW2 */
60         pmic_clrsetbits(dev, REG_SW_1, SWx_VOLT_MASK_MC34708,
61                         SWx_1_300V_MC34708);
62
63         /* Set global reset timer to 4s */
64         pmic_clrsetbits(dev, REG_POWER_CTL2, TIMER_MASK_MC34708,
65                         TIMER_4S_MC34708);
66
67         return ret;
68 }
69
70 static void setup_clocks(void)
71 {
72         int ret;
73         u32 ref_clk = MXC_HCLK;
74         /*
75          * CPU clock set to 800MHz and DDR to 400MHz
76          */
77         ret = mxc_set_clock(ref_clk, 800, MXC_ARM_CLK);
78         if (ret)
79                 printf("CPU:   Switch CPU clock to 800MHZ failed\n");
80
81         ret = mxc_set_clock(ref_clk, 400, MXC_PERIPH_CLK);
82         ret |= mxc_set_clock(ref_clk, 400, MXC_DDR_CLK);
83         if (ret)
84                 printf("CPU:   Switch DDR clock to 400MHz failed\n");
85 }
86
87 static void setup_ups(void)
88 {
89         gpio_request(BOOSTER_OFF, "BOOSTER_OFF");
90         gpio_direction_output(BOOSTER_OFF, 0);
91 }
92
93 int board_early_init_f(void)
94 {
95         return 0;
96 }
97
98 /*
99  * Do not overwrite the console
100  * Use always serial for U-Boot console
101  */
102 int overwrite_console(void)
103 {
104         return 1;
105 }
106
107 int board_init(void)
108 {
109         gd->bd->bi_boot_params = PHYS_SDRAM_1 + 0x100;
110
111         return 0;
112 }
113
114 void board_disable_display(void)
115 {
116         gpio_request(LCD_BACKLIGHT, "LCD_BACKLIGHT");
117         gpio_direction_output(LCD_BACKLIGHT, 0);
118 }
119
120 void board_misc_setup(void)
121 {
122         gpio_request(KEY1, "KEY1_GPIO");
123         gpio_direction_input(KEY1);
124
125         if (gpio_get_value(KEY1))
126                 env_set("key1", "off");
127         else
128                 env_set("key1", "on");
129 }
130
131 int board_late_init(void)
132 {
133         int ret = 0;
134
135         board_disable_display();
136         setup_ups();
137
138         if (!power_init())
139                 setup_clocks();
140
141         ret = read_eeprom();
142         if (ret)
143                 printf("Error %d reading EEPROM content!\n", ret);
144
145         show_eeprom();
146         read_board_id();
147
148         board_misc_setup();
149
150         return ret;
151 }