1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * common.c
4  *
5  * common board functions for B&R boards
6  *
7  * Copyright (C) 2013 Hannes Schmelzer <oe5hpm@oevsv.at>
8  * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
9  *
10  */
11 #include <log.h>
12 #include <version.h>
13 #include <common.h>
14 #include <env.h>
15 #include <fdtdec.h>
16 #include <i2c.h>
17 #include <asm/global_data.h>
18 #include <linux/delay.h>
19 #include "bur_common.h"
20 
21 DECLARE_GLOBAL_DATA_PTR;
22 
23 /* --------------------------------------------------------------------------*/
24 
ft_board_setup(void * blob,struct bd_info * bd)25 int ft_board_setup(void *blob, struct bd_info *bd)
26 {
27 	int nodeoffset;
28 
29 	nodeoffset = fdt_path_offset(blob, "/factory-settings");
30 	if (nodeoffset < 0) {
31 		printf("%s: cannot find /factory-settings, trying /fset\n",
32 		       __func__);
33 		nodeoffset = fdt_path_offset(blob, "/fset");
34 		if (nodeoffset < 0) {
35 			printf("%s: cannot find /fset.\n", __func__);
36 			return 0;
37 		}
38 	}
39 
40 	if (fdt_setprop(blob, nodeoffset, "bl-version",
41 			PLAIN_VERSION, strlen(PLAIN_VERSION)) != 0) {
42 		printf("%s: no 'bl-version' prop in fdt!\n", __func__);
43 		return 0;
44 	}
45 	return 0;
46 }
47 
brdefaultip_setup(int bus,int chip)48 int brdefaultip_setup(int bus, int chip)
49 {
50 	int rc;
51 	struct udevice *i2cdev;
52 	u8 u8buf = 0;
53 	char defip[256] = { 0 };
54 
55 	rc = i2c_get_chip_for_busnum(bus, chip, 2, &i2cdev);
56 	if (rc != 0) {
57 		printf("WARN: cannot probe baseboard EEPROM!\n");
58 		return -1;
59 	}
60 
61 	rc = dm_i2c_read(i2cdev, 0, &u8buf, 1);
62 	if (rc != 0) {
63 		printf("WARN: cannot read baseboard EEPROM!\n");
64 		return -1;
65 	}
66 
67 	if (u8buf != 0xFF)
68 		snprintf(defip, sizeof(defip),
69 			 "if test -r ${ipaddr}; then; else setenv ipaddr 192.168.60.%d; setenv serverip 192.168.60.254; setenv gatewayip 192.168.60.254; setenv netmask 255.255.255.0; fi;",
70 			 u8buf);
71 	else
72 		strncpy(defip,
73 			"if test -r ${ipaddr}; then; else setenv ipaddr 192.168.60.1; setenv serverip 192.168.60.254; setenv gatewayip 192.168.60.254; setenv netmask 255.255.255.0; fi;",
74 			sizeof(defip));
75 
76 	env_set("brdefaultip", defip);
77 	env_set_hex("board_id", u8buf);
78 
79 	return 0;
80 }
81 
overwrite_console(void)82 int overwrite_console(void)
83 {
84 	return 1;
85 }
86 
87 #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_AM33XX)
88 #include <asm/arch/hardware.h>
89 #include <asm/arch/omap.h>
90 #include <asm/arch/clock.h>
91 #include <asm/arch/sys_proto.h>
92 #include <power/tps65217.h>
93 
94 static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
95 
pmicsetup(u32 mpupll,unsigned int bus)96 void pmicsetup(u32 mpupll, unsigned int bus)
97 {
98 	int mpu_vdd;
99 	int usb_cur_lim;
100 
101 	if (power_tps65217_init(bus)) {
102 		printf("WARN: cannot setup PMIC 0x24 @ bus #%d, not found!.\n",
103 		       bus);
104 		return;
105 	}
106 
107 	/* Get the frequency which is defined by device fuses */
108 	dpll_mpu_opp100.m = am335x_get_efuse_mpu_max_freq(cdev);
109 	printf("detected max. frequency: %d - ", dpll_mpu_opp100.m);
110 
111 	if (0 != mpupll) {
112 		dpll_mpu_opp100.m = mpupll;
113 		printf("retuning MPU-PLL to: %d MHz.\n", dpll_mpu_opp100.m);
114 	} else {
115 		puts("ok.\n");
116 	}
117 	/*
118 	 * Increase USB current limit to 1300mA or 1800mA and set
119 	 * the MPU voltage controller as needed.
120 	 */
121 	if (dpll_mpu_opp100.m == MPUPLL_M_1000) {
122 		usb_cur_lim = TPS65217_USB_INPUT_CUR_LIMIT_1800MA;
123 		mpu_vdd = TPS65217_DCDC_VOLT_SEL_1325MV;
124 	} else {
125 		usb_cur_lim = TPS65217_USB_INPUT_CUR_LIMIT_1300MA;
126 		mpu_vdd = TPS65217_DCDC_VOLT_SEL_1275MV;
127 	}
128 
129 	if (tps65217_reg_write(TPS65217_PROT_LEVEL_NONE, TPS65217_POWER_PATH,
130 			       usb_cur_lim, TPS65217_USB_INPUT_CUR_LIMIT_MASK))
131 		puts("tps65217_reg_write failure\n");
132 
133 	/* Set DCDC3 (CORE) voltage to 1.125V */
134 	if (tps65217_voltage_update(TPS65217_DEFDCDC3,
135 				    TPS65217_DCDC_VOLT_SEL_1125MV)) {
136 		puts("tps65217_voltage_update failure\n");
137 		return;
138 	}
139 
140 	/* Set CORE Frequencies to OPP100 */
141 	do_setup_dpll(&dpll_core_regs, &dpll_core_opp100);
142 
143 	/* Set DCDC2 (MPU) voltage */
144 	if (tps65217_voltage_update(TPS65217_DEFDCDC2, mpu_vdd)) {
145 		puts("tps65217_voltage_update failure\n");
146 		return;
147 	}
148 
149 	/* Set LDO3 to 1.8V */
150 	if (tps65217_reg_write(TPS65217_PROT_LEVEL_2,
151 			       TPS65217_DEFLS1,
152 			       TPS65217_LDO_VOLTAGE_OUT_1_8,
153 			       TPS65217_LDO_MASK))
154 		puts("tps65217_reg_write failure\n");
155 	/* Set LDO4 to 3.3V */
156 	if (tps65217_reg_write(TPS65217_PROT_LEVEL_2,
157 			       TPS65217_DEFLS2,
158 			       TPS65217_LDO_VOLTAGE_OUT_3_3,
159 			       TPS65217_LDO_MASK))
160 		puts("tps65217_reg_write failure\n");
161 
162 	/* Set MPU Frequency to what we detected now that voltages are set */
163 	do_setup_dpll(&dpll_mpu_regs, &dpll_mpu_opp100);
164 	/* Set PWR_EN bit in Status Register */
165 	tps65217_reg_write(TPS65217_PROT_LEVEL_NONE,
166 			   TPS65217_STATUS, TPS65217_PWR_OFF, TPS65217_PWR_OFF);
167 }
168 
set_uart_mux_conf(void)169 void set_uart_mux_conf(void)
170 {
171 	enable_uart0_pin_mux();
172 }
173 
set_mux_conf_regs(void)174 void set_mux_conf_regs(void)
175 {
176 	enable_board_pin_mux();
177 }
178 
179 #endif /* CONFIG_SPL_BUILD && CONFIG_AM33XX */
180