xref: /openbmc/u-boot/board/BuR/common/common.c (revision 5396b2a9)
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 <version.h>
12 #include <common.h>
13 #include <environment.h>
14 #include <errno.h>
15 #include <asm/arch/cpu.h>
16 #include <asm/arch/hardware.h>
17 #include <asm/arch/omap.h>
18 #include <asm/arch/clock.h>
19 #include <asm/arch/gpio.h>
20 #include <asm/arch/sys_proto.h>
21 #include <asm/arch/mmc_host_def.h>
22 #include <asm/io.h>
23 #include <asm/gpio.h>
24 #include <i2c.h>
25 #include <power/tps65217.h>
26 #include <lcd.h>
27 #include "bur_common.h"
28 #include "../../../drivers/video/am335x-fb.h"
29 
30 DECLARE_GLOBAL_DATA_PTR;
31 
32 /* --------------------------------------------------------------------------*/
33 #if defined(CONFIG_LCD) && defined(CONFIG_AM335X_LCD) && \
34 	!defined(CONFIG_SPL_BUILD)
35 void lcdbacklight(int on)
36 {
37 	unsigned int driver = env_get_ulong("ds1_bright_drv", 16, 0UL);
38 	unsigned int bright = env_get_ulong("ds1_bright_def", 10, 50);
39 	unsigned int pwmfrq = env_get_ulong("ds1_pwmfreq", 10, ~0UL);
40 	unsigned int tmp;
41 	struct gptimer *timerhw;
42 
43 	if (on)
44 		bright = bright != ~0UL ? bright : 50;
45 	else
46 		bright = 0;
47 
48 	switch (driver) {
49 	case 2:
50 		timerhw = (struct gptimer *)DM_TIMER5_BASE;
51 		break;
52 	default:
53 		timerhw = (struct gptimer *)DM_TIMER6_BASE;
54 	}
55 
56 	switch (driver) {
57 	case 0:	/* PMIC LED-Driver */
58 		/* brightness level */
59 		tps65217_reg_write(TPS65217_PROT_LEVEL_NONE,
60 				   TPS65217_WLEDCTRL2, bright, 0xFF);
61 		/* current sink */
62 		tps65217_reg_write(TPS65217_PROT_LEVEL_NONE,
63 				   TPS65217_WLEDCTRL1,
64 				   bright != 0 ? 0x0A : 0x02,
65 				   0xFF);
66 		break;
67 	case 1:
68 	case 2: /* PWM using timer */
69 		if (pwmfrq != ~0UL) {
70 			timerhw->tiocp_cfg = TCFG_RESET;
71 			udelay(10);
72 			while (timerhw->tiocp_cfg & TCFG_RESET)
73 				;
74 			tmp = ~0UL-(V_OSCK/pwmfrq);	/* bottom value */
75 			timerhw->tldr = tmp;
76 			timerhw->tcrr = tmp;
77 			tmp = tmp + ((V_OSCK/pwmfrq)/100) * bright;
78 			timerhw->tmar = tmp;
79 			timerhw->tclr = (TCLR_PT | (2 << TCLR_TRG_SHIFT) |
80 					TCLR_CE | TCLR_AR | TCLR_ST);
81 		} else {
82 			puts("invalid pwmfrq in env/dtb! skip PWM-setup.\n");
83 		}
84 		break;
85 	default:
86 		puts("no suitable backlightdriver in env/dtb!\n");
87 		break;
88 	}
89 }
90 
91 int load_lcdtiming(struct am335x_lcdpanel *panel)
92 {
93 	struct am335x_lcdpanel pnltmp;
94 
95 	pnltmp.hactive = env_get_ulong("ds1_hactive", 10, ~0UL);
96 	pnltmp.vactive = env_get_ulong("ds1_vactive", 10, ~0UL);
97 	pnltmp.bpp = env_get_ulong("ds1_bpp", 10, ~0UL);
98 	pnltmp.hfp = env_get_ulong("ds1_hfp", 10, ~0UL);
99 	pnltmp.hbp = env_get_ulong("ds1_hbp", 10, ~0UL);
100 	pnltmp.hsw = env_get_ulong("ds1_hsw", 10, ~0UL);
101 	pnltmp.vfp = env_get_ulong("ds1_vfp", 10, ~0UL);
102 	pnltmp.vbp = env_get_ulong("ds1_vbp", 10, ~0UL);
103 	pnltmp.vsw = env_get_ulong("ds1_vsw", 10, ~0UL);
104 	pnltmp.pxl_clk = env_get_ulong("ds1_pxlclk", 10, ~0UL);
105 	pnltmp.pol = env_get_ulong("ds1_pol", 16, ~0UL);
106 	pnltmp.pup_delay = env_get_ulong("ds1_pupdelay", 10, ~0UL);
107 	pnltmp.pon_delay = env_get_ulong("ds1_tondelay", 10, ~0UL);
108 	panel_info.vl_rot = env_get_ulong("ds1_rotation", 10, 0);
109 
110 	if (
111 	   ~0UL == (pnltmp.hactive) ||
112 	   ~0UL == (pnltmp.vactive) ||
113 	   ~0UL == (pnltmp.bpp) ||
114 	   ~0UL == (pnltmp.hfp) ||
115 	   ~0UL == (pnltmp.hbp) ||
116 	   ~0UL == (pnltmp.hsw) ||
117 	   ~0UL == (pnltmp.vfp) ||
118 	   ~0UL == (pnltmp.vbp) ||
119 	   ~0UL == (pnltmp.vsw) ||
120 	   ~0UL == (pnltmp.pxl_clk) ||
121 	   ~0UL == (pnltmp.pol) ||
122 	   ~0UL == (pnltmp.pup_delay) ||
123 	   ~0UL == (pnltmp.pon_delay)
124 	   ) {
125 		puts("lcd-settings in env/dtb incomplete!\n");
126 		printf("display-timings:\n"
127 			"================\n"
128 			"hactive: %d\n"
129 			"vactive: %d\n"
130 			"bpp    : %d\n"
131 			"hfp    : %d\n"
132 			"hbp    : %d\n"
133 			"hsw    : %d\n"
134 			"vfp    : %d\n"
135 			"vbp    : %d\n"
136 			"vsw    : %d\n"
137 			"pxlclk : %d\n"
138 			"pol    : 0x%08x\n"
139 			"pondly : %d\n",
140 			pnltmp.hactive, pnltmp.vactive, pnltmp.bpp,
141 			pnltmp.hfp, pnltmp.hbp, pnltmp.hsw,
142 			pnltmp.vfp, pnltmp.vbp, pnltmp.vsw,
143 			pnltmp.pxl_clk, pnltmp.pol, pnltmp.pon_delay);
144 
145 		return -1;
146 	}
147 	debug("lcd-settings in env complete, taking over.\n");
148 	memcpy((void *)panel,
149 	       (void *)&pnltmp,
150 	       sizeof(struct am335x_lcdpanel));
151 
152 	return 0;
153 }
154 
155 static void br_summaryscreen_printenv(char *prefix,
156 				       char *name, char *altname,
157 				       char *suffix)
158 {
159 	char *envval = env_get(name);
160 	if (0 != envval) {
161 		lcd_printf("%s %s %s", prefix, envval, suffix);
162 	} else if (0 != altname) {
163 		envval = env_get(altname);
164 		if (0 != envval)
165 			lcd_printf("%s %s %s", prefix, envval, suffix);
166 	} else {
167 		lcd_printf("\n");
168 	}
169 }
170 
171 void br_summaryscreen(void)
172 {
173 	br_summaryscreen_printenv(" - B&R -", "br_orderno", 0, "-\n");
174 	br_summaryscreen_printenv(" Serial/Rev :", "br_serial", 0, "\n");
175 	br_summaryscreen_printenv(" MAC1       :", "br_mac1", "ethaddr", "\n");
176 	br_summaryscreen_printenv(" MAC2       :", "br_mac2", 0, "\n");
177 	lcd_puts(" Bootloader : " PLAIN_VERSION "\n");
178 	lcd_puts("\n");
179 }
180 
181 void lcdpower(int on)
182 {
183 	u32 pin, swval, i;
184 	char buf[16] = { 0 };
185 
186 	pin = env_get_ulong("ds1_pwr", 16, ~0UL);
187 
188 	if (pin == ~0UL) {
189 		puts("no pwrpin in dtb/env, cannot powerup display!\n");
190 		return;
191 	}
192 
193 	for (i = 0; i < 3; i++) {
194 		if (pin != 0) {
195 			snprintf(buf, sizeof(buf), "ds1_pwr#%d", i);
196 			if (gpio_request(pin & 0x7F, buf) != 0) {
197 				printf("%s: not able to request gpio %s",
198 				       __func__, buf);
199 				continue;
200 			}
201 			swval = pin & 0x80 ? 0 : 1;
202 			if (on)
203 				gpio_direction_output(pin & 0x7F, swval);
204 			else
205 				gpio_direction_output(pin & 0x7F, !swval);
206 
207 			debug("switched pin %d to %d\n", pin & 0x7F, swval);
208 		}
209 		pin >>= 8;
210 	}
211 }
212 
213 vidinfo_t	panel_info = {
214 		.vl_col = 1366,	/*
215 				 * give full resolution for allocating enough
216 				 * memory
217 				 */
218 		.vl_row = 768,
219 		.vl_bpix = 5,
220 		.priv = 0
221 };
222 
223 void lcd_ctrl_init(void *lcdbase)
224 {
225 	struct am335x_lcdpanel lcd_panel;
226 
227 	memset(&lcd_panel, 0, sizeof(struct am335x_lcdpanel));
228 	if (load_lcdtiming(&lcd_panel) != 0)
229 		return;
230 
231 	lcd_panel.panel_power_ctrl = &lcdpower;
232 
233 	if (0 != am335xfb_init(&lcd_panel))
234 		printf("ERROR: failed to initialize video!");
235 	/*
236 	 * modifiy panel info to 'real' resolution, to operate correct with
237 	 * lcd-framework.
238 	 */
239 	panel_info.vl_col = lcd_panel.hactive;
240 	panel_info.vl_row = lcd_panel.vactive;
241 
242 	lcd_set_flush_dcache(1);
243 }
244 
245 void lcd_enable(void)
246 {
247 	br_summaryscreen();
248 	lcdbacklight(1);
249 }
250 #endif /* CONFIG_LCD */
251 
252 int ft_board_setup(void *blob, bd_t *bd)
253 {
254 	int nodeoffset;
255 
256 	nodeoffset = fdt_path_offset(blob, "/factory-settings");
257 	if (nodeoffset < 0) {
258 		printf("%s: cannot find /factory-settings, trying /fset\n",
259 		       __func__);
260 		nodeoffset = fdt_path_offset(blob, "/fset");
261 		if (nodeoffset < 0) {
262 			printf("%s: cannot find /fset.\n", __func__);
263 			return 0;
264 		}
265 	}
266 
267 	if (fdt_setprop(blob, nodeoffset, "bl-version",
268 			PLAIN_VERSION, strlen(PLAIN_VERSION)) != 0) {
269 		printf("%s: no 'bl-version' prop in fdt!\n", __func__);
270 		return 0;
271 	}
272 	return 0;
273 }
274 
275 #ifdef CONFIG_SPL_BUILD
276 
277 static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
278 
279 void pmicsetup(u32 mpupll, unsigned int bus)
280 {
281 	int mpu_vdd;
282 	int usb_cur_lim;
283 
284 	if (power_tps65217_init(bus)) {
285 		printf("WARN: cannot setup PMIC 0x24 @ bus #%d, not found!.\n",
286 		       bus);
287 		return;
288 	}
289 
290 	/* Get the frequency which is defined by device fuses */
291 	dpll_mpu_opp100.m = am335x_get_efuse_mpu_max_freq(cdev);
292 	printf("detected max. frequency: %d - ", dpll_mpu_opp100.m);
293 
294 	if (0 != mpupll) {
295 		dpll_mpu_opp100.m = mpupll;
296 		printf("retuning MPU-PLL to: %d MHz.\n", dpll_mpu_opp100.m);
297 	} else {
298 		puts("ok.\n");
299 	}
300 	/*
301 	 * Increase USB current limit to 1300mA or 1800mA and set
302 	 * the MPU voltage controller as needed.
303 	 */
304 	if (dpll_mpu_opp100.m == MPUPLL_M_1000) {
305 		usb_cur_lim = TPS65217_USB_INPUT_CUR_LIMIT_1800MA;
306 		mpu_vdd = TPS65217_DCDC_VOLT_SEL_1325MV;
307 	} else {
308 		usb_cur_lim = TPS65217_USB_INPUT_CUR_LIMIT_1300MA;
309 		mpu_vdd = TPS65217_DCDC_VOLT_SEL_1275MV;
310 	}
311 
312 	if (tps65217_reg_write(TPS65217_PROT_LEVEL_NONE, TPS65217_POWER_PATH,
313 			       usb_cur_lim, TPS65217_USB_INPUT_CUR_LIMIT_MASK))
314 		puts("tps65217_reg_write failure\n");
315 
316 	/* Set DCDC3 (CORE) voltage to 1.125V */
317 	if (tps65217_voltage_update(TPS65217_DEFDCDC3,
318 				    TPS65217_DCDC_VOLT_SEL_1125MV)) {
319 		puts("tps65217_voltage_update failure\n");
320 		return;
321 	}
322 
323 	/* Set CORE Frequencies to OPP100 */
324 	do_setup_dpll(&dpll_core_regs, &dpll_core_opp100);
325 
326 	/* Set DCDC2 (MPU) voltage */
327 	if (tps65217_voltage_update(TPS65217_DEFDCDC2, mpu_vdd)) {
328 		puts("tps65217_voltage_update failure\n");
329 		return;
330 	}
331 
332 	/* Set LDO3 to 1.8V */
333 	if (tps65217_reg_write(TPS65217_PROT_LEVEL_2,
334 			       TPS65217_DEFLS1,
335 			       TPS65217_LDO_VOLTAGE_OUT_1_8,
336 			       TPS65217_LDO_MASK))
337 		puts("tps65217_reg_write failure\n");
338 	/* Set LDO4 to 3.3V */
339 	if (tps65217_reg_write(TPS65217_PROT_LEVEL_2,
340 			       TPS65217_DEFLS2,
341 			       TPS65217_LDO_VOLTAGE_OUT_3_3,
342 			       TPS65217_LDO_MASK))
343 		puts("tps65217_reg_write failure\n");
344 
345 	/* Set MPU Frequency to what we detected now that voltages are set */
346 	do_setup_dpll(&dpll_mpu_regs, &dpll_mpu_opp100);
347 	/* Set PWR_EN bit in Status Register */
348 	tps65217_reg_write(TPS65217_PROT_LEVEL_NONE,
349 			   TPS65217_STATUS, TPS65217_PWR_OFF, TPS65217_PWR_OFF);
350 }
351 
352 void set_uart_mux_conf(void)
353 {
354 	enable_uart0_pin_mux();
355 }
356 
357 void set_mux_conf_regs(void)
358 {
359 	enable_board_pin_mux();
360 }
361 
362 #endif /* CONFIG_SPL_BUILD */
363 
364 int overwrite_console(void)
365 {
366 	return 1;
367 }
368