blob: 148fc9075e4fffb098b6e61d599ff66d0bcc71c3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hannes Petermaierfb003662014-02-07 08:07:36 +01002/*
3 * common.c
4 *
5 * common board functions for B&R boards
6 *
Hannes Schmelzer7935f032015-05-28 15:41:12 +02007 * Copyright (C) 2013 Hannes Schmelzer <oe5hpm@oevsv.at>
Hannes Petermaierfb003662014-02-07 08:07:36 +01008 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
9 *
Hannes Petermaierfb003662014-02-07 08:07:36 +010010 */
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +010011#include <version.h>
Hannes Petermaierfb003662014-02-07 08:07:36 +010012#include <common.h>
Simon Glass313112a2019-08-01 09:46:46 -060013#include <env.h>
Hannes Schmelzer1386d412019-04-10 14:13:11 +020014#include <fdtdec.h>
Hannes Petermaierfb003662014-02-07 08:07:36 +010015#include <i2c.h>
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +010016#include <lcd.h>
Hannes Petermaierfb003662014-02-07 08:07:36 +010017#include "bur_common.h"
18
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +010019DECLARE_GLOBAL_DATA_PTR;
20
Hannes Petermaierfb003662014-02-07 08:07:36 +010021/* --------------------------------------------------------------------------*/
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +010022#if defined(CONFIG_LCD) && defined(CONFIG_AM335X_LCD) && \
23 !defined(CONFIG_SPL_BUILD)
Hannes Schmelzer1386d412019-04-10 14:13:11 +020024#include <asm/arch/hardware.h>
25#include <asm/arch/cpu.h>
26#include <asm/gpio.h>
27#include <power/tps65217.h>
28#include "../../../drivers/video/am335x-fb.h"
29
Hannes Petermaier1df983f2015-04-24 14:49:37 +020030void lcdbacklight(int on)
31{
Simon Glass22c34c22017-08-03 12:22:13 -060032 unsigned int driver = env_get_ulong("ds1_bright_drv", 16, 0UL);
33 unsigned int bright = env_get_ulong("ds1_bright_def", 10, 50);
34 unsigned int pwmfrq = env_get_ulong("ds1_pwmfreq", 10, ~0UL);
Hannes Petermaier1df983f2015-04-24 14:49:37 +020035 unsigned int tmp;
Hannes Petermaier129fe972015-06-11 12:25:43 +020036 struct gptimer *timerhw;
Hannes Petermaier1df983f2015-04-24 14:49:37 +020037
38 if (on)
39 bright = bright != ~0UL ? bright : 50;
40 else
41 bright = 0;
42
43 switch (driver) {
Hannes Petermaier129fe972015-06-11 12:25:43 +020044 case 2:
45 timerhw = (struct gptimer *)DM_TIMER5_BASE;
46 break;
47 default:
48 timerhw = (struct gptimer *)DM_TIMER6_BASE;
49 }
50
51 switch (driver) {
Hannes Petermaier1df983f2015-04-24 14:49:37 +020052 case 0: /* PMIC LED-Driver */
53 /* brightness level */
54 tps65217_reg_write(TPS65217_PROT_LEVEL_NONE,
55 TPS65217_WLEDCTRL2, bright, 0xFF);
56 /* current sink */
57 tps65217_reg_write(TPS65217_PROT_LEVEL_NONE,
58 TPS65217_WLEDCTRL1,
59 bright != 0 ? 0x0A : 0x02,
60 0xFF);
61 break;
Hannes Petermaier129fe972015-06-11 12:25:43 +020062 case 1:
63 case 2: /* PWM using timer */
Hannes Petermaier1df983f2015-04-24 14:49:37 +020064 if (pwmfrq != ~0UL) {
65 timerhw->tiocp_cfg = TCFG_RESET;
66 udelay(10);
67 while (timerhw->tiocp_cfg & TCFG_RESET)
68 ;
69 tmp = ~0UL-(V_OSCK/pwmfrq); /* bottom value */
70 timerhw->tldr = tmp;
71 timerhw->tcrr = tmp;
72 tmp = tmp + ((V_OSCK/pwmfrq)/100) * bright;
73 timerhw->tmar = tmp;
74 timerhw->tclr = (TCLR_PT | (2 << TCLR_TRG_SHIFT) |
75 TCLR_CE | TCLR_AR | TCLR_ST);
76 } else {
77 puts("invalid pwmfrq in env/dtb! skip PWM-setup.\n");
78 }
79 break;
80 default:
81 puts("no suitable backlightdriver in env/dtb!\n");
82 break;
83 }
84}
85
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +010086int load_lcdtiming(struct am335x_lcdpanel *panel)
87{
88 struct am335x_lcdpanel pnltmp;
Hannes Petermaierd35cb792015-04-24 14:49:35 +020089
Simon Glass22c34c22017-08-03 12:22:13 -060090 pnltmp.hactive = env_get_ulong("ds1_hactive", 10, ~0UL);
91 pnltmp.vactive = env_get_ulong("ds1_vactive", 10, ~0UL);
92 pnltmp.bpp = env_get_ulong("ds1_bpp", 10, ~0UL);
93 pnltmp.hfp = env_get_ulong("ds1_hfp", 10, ~0UL);
94 pnltmp.hbp = env_get_ulong("ds1_hbp", 10, ~0UL);
95 pnltmp.hsw = env_get_ulong("ds1_hsw", 10, ~0UL);
96 pnltmp.vfp = env_get_ulong("ds1_vfp", 10, ~0UL);
97 pnltmp.vbp = env_get_ulong("ds1_vbp", 10, ~0UL);
98 pnltmp.vsw = env_get_ulong("ds1_vsw", 10, ~0UL);
Hannes Schmelzerd6ace8e2018-01-09 19:01:35 +010099 pnltmp.pxl_clk = env_get_ulong("ds1_pxlclk", 10, ~0UL);
Simon Glass22c34c22017-08-03 12:22:13 -0600100 pnltmp.pol = env_get_ulong("ds1_pol", 16, ~0UL);
101 pnltmp.pup_delay = env_get_ulong("ds1_pupdelay", 10, ~0UL);
102 pnltmp.pon_delay = env_get_ulong("ds1_tondelay", 10, ~0UL);
103 panel_info.vl_rot = env_get_ulong("ds1_rotation", 10, 0);
Hannes Schmelzer90168e82018-07-06 15:41:18 +0200104
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100105 if (
106 ~0UL == (pnltmp.hactive) ||
107 ~0UL == (pnltmp.vactive) ||
108 ~0UL == (pnltmp.bpp) ||
109 ~0UL == (pnltmp.hfp) ||
110 ~0UL == (pnltmp.hbp) ||
111 ~0UL == (pnltmp.hsw) ||
112 ~0UL == (pnltmp.vfp) ||
113 ~0UL == (pnltmp.vbp) ||
114 ~0UL == (pnltmp.vsw) ||
Hannes Schmelzerd6ace8e2018-01-09 19:01:35 +0100115 ~0UL == (pnltmp.pxl_clk) ||
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100116 ~0UL == (pnltmp.pol) ||
117 ~0UL == (pnltmp.pup_delay) ||
118 ~0UL == (pnltmp.pon_delay)
119 ) {
120 puts("lcd-settings in env/dtb incomplete!\n");
121 printf("display-timings:\n"
122 "================\n"
123 "hactive: %d\n"
124 "vactive: %d\n"
125 "bpp : %d\n"
126 "hfp : %d\n"
127 "hbp : %d\n"
128 "hsw : %d\n"
129 "vfp : %d\n"
130 "vbp : %d\n"
131 "vsw : %d\n"
132 "pxlclk : %d\n"
133 "pol : 0x%08x\n"
134 "pondly : %d\n",
135 pnltmp.hactive, pnltmp.vactive, pnltmp.bpp,
136 pnltmp.hfp, pnltmp.hbp, pnltmp.hsw,
137 pnltmp.vfp, pnltmp.vbp, pnltmp.vsw,
Hannes Schmelzerd6ace8e2018-01-09 19:01:35 +0100138 pnltmp.pxl_clk, pnltmp.pol, pnltmp.pon_delay);
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100139
140 return -1;
141 }
142 debug("lcd-settings in env complete, taking over.\n");
143 memcpy((void *)panel,
144 (void *)&pnltmp,
145 sizeof(struct am335x_lcdpanel));
146
147 return 0;
148}
149
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100150static void br_summaryscreen_printenv(char *prefix,
151 char *name, char *altname,
152 char *suffix)
153{
Simon Glass64b723f2017-08-03 12:22:12 -0600154 char *envval = env_get(name);
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100155 if (0 != envval) {
156 lcd_printf("%s %s %s", prefix, envval, suffix);
157 } else if (0 != altname) {
Simon Glass64b723f2017-08-03 12:22:12 -0600158 envval = env_get(altname);
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100159 if (0 != envval)
160 lcd_printf("%s %s %s", prefix, envval, suffix);
161 } else {
162 lcd_printf("\n");
163 }
164}
Hannes Schmelzer90168e82018-07-06 15:41:18 +0200165
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100166void br_summaryscreen(void)
167{
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100168 br_summaryscreen_printenv(" - B&R -", "br_orderno", 0, "-\n");
169 br_summaryscreen_printenv(" Serial/Rev :", "br_serial", 0, "\n");
Hannes Schmelzer7d352312018-07-06 15:41:22 +0200170 br_summaryscreen_printenv(" MAC1 :", "br_mac1", "ethaddr", "\n");
171 br_summaryscreen_printenv(" MAC2 :", "br_mac2", 0, "\n");
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100172 lcd_puts(" Bootloader : " PLAIN_VERSION "\n");
173 lcd_puts("\n");
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100174}
175
176void lcdpower(int on)
177{
178 u32 pin, swval, i;
Hannes Schmelzer008c6b52019-02-06 13:25:59 +0100179 char buf[16] = { 0 };
Hannes Schmelzer90168e82018-07-06 15:41:18 +0200180
Simon Glass22c34c22017-08-03 12:22:13 -0600181 pin = env_get_ulong("ds1_pwr", 16, ~0UL);
Hannes Schmelzer90168e82018-07-06 15:41:18 +0200182
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100183 if (pin == ~0UL) {
184 puts("no pwrpin in dtb/env, cannot powerup display!\n");
185 return;
186 }
187
188 for (i = 0; i < 3; i++) {
189 if (pin != 0) {
Hannes Schmelzer008c6b52019-02-06 13:25:59 +0100190 snprintf(buf, sizeof(buf), "ds1_pwr#%d", i);
191 if (gpio_request(pin & 0x7F, buf) != 0) {
192 printf("%s: not able to request gpio %s",
193 __func__, buf);
194 continue;
195 }
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100196 swval = pin & 0x80 ? 0 : 1;
197 if (on)
198 gpio_direction_output(pin & 0x7F, swval);
199 else
200 gpio_direction_output(pin & 0x7F, !swval);
201
202 debug("switched pin %d to %d\n", pin & 0x7F, swval);
203 }
204 pin >>= 8;
205 }
206}
207
208vidinfo_t panel_info = {
209 .vl_col = 1366, /*
210 * give full resolution for allocating enough
211 * memory
212 */
213 .vl_row = 768,
214 .vl_bpix = 5,
215 .priv = 0
216};
217
218void lcd_ctrl_init(void *lcdbase)
219{
220 struct am335x_lcdpanel lcd_panel;
Hannes Schmelzer90168e82018-07-06 15:41:18 +0200221
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100222 memset(&lcd_panel, 0, sizeof(struct am335x_lcdpanel));
223 if (load_lcdtiming(&lcd_panel) != 0)
224 return;
225
226 lcd_panel.panel_power_ctrl = &lcdpower;
227
228 if (0 != am335xfb_init(&lcd_panel))
229 printf("ERROR: failed to initialize video!");
230 /*
231 * modifiy panel info to 'real' resolution, to operate correct with
232 * lcd-framework.
233 */
234 panel_info.vl_col = lcd_panel.hactive;
235 panel_info.vl_row = lcd_panel.vactive;
236
237 lcd_set_flush_dcache(1);
238}
239
240void lcd_enable(void)
241{
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100242 br_summaryscreen();
Hannes Petermaier1df983f2015-04-24 14:49:37 +0200243 lcdbacklight(1);
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100244}
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100245#endif /* CONFIG_LCD */
246
Hannes Schmelzera21ed272018-07-06 15:41:20 +0200247int ft_board_setup(void *blob, bd_t *bd)
248{
249 int nodeoffset;
250
251 nodeoffset = fdt_path_offset(blob, "/factory-settings");
252 if (nodeoffset < 0) {
Hannes Schmelzerde4225b2018-07-06 15:41:25 +0200253 printf("%s: cannot find /factory-settings, trying /fset\n",
254 __func__);
255 nodeoffset = fdt_path_offset(blob, "/fset");
256 if (nodeoffset < 0) {
257 printf("%s: cannot find /fset.\n", __func__);
258 return 0;
259 }
Hannes Schmelzera21ed272018-07-06 15:41:20 +0200260 }
Hannes Schmelzerde4225b2018-07-06 15:41:25 +0200261
Hannes Schmelzera21ed272018-07-06 15:41:20 +0200262 if (fdt_setprop(blob, nodeoffset, "bl-version",
263 PLAIN_VERSION, strlen(PLAIN_VERSION)) != 0) {
Hannes Schmelzerde4225b2018-07-06 15:41:25 +0200264 printf("%s: no 'bl-version' prop in fdt!\n", __func__);
265 return 0;
Hannes Schmelzera21ed272018-07-06 15:41:20 +0200266 }
Hannes Schmelzera21ed272018-07-06 15:41:20 +0200267 return 0;
268}
269
Hannes Schmelzer6cd12ce2019-04-10 14:13:13 +0200270int brdefaultip_setup(int bus, int chip)
271{
272 int rc;
273 struct udevice *i2cdev;
274 u8 u8buf = 0;
275 char defip[256] = { 0 };
276
277 rc = i2c_get_chip_for_busnum(bus, chip, 2, &i2cdev);
278 if (rc != 0) {
279 printf("WARN: cannot probe baseboard EEPROM!\n");
280 return -1;
281 }
282
283 rc = dm_i2c_read(i2cdev, 0, &u8buf, 1);
284 if (rc != 0) {
285 printf("WARN: cannot read baseboard EEPROM!\n");
286 return -1;
287 }
288
289 if (u8buf != 0xFF)
290 snprintf(defip, sizeof(defip),
291 "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;",
292 u8buf);
293 else
294 strncpy(defip,
295 "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;",
296 sizeof(defip));
297
298 env_set("brdefaultip", defip);
299 env_set_hex("board_id", u8buf);
300
301 return 0;
302}
303
Hannes Schmelzer9df7d732019-04-10 14:13:12 +0200304int overwrite_console(void)
305{
306 return 1;
307}
308
Hannes Schmelzer1386d412019-04-10 14:13:11 +0200309#if defined(CONFIG_SPL_BUILD) && defined(CONFIG_AM33XX)
310#include <asm/arch/hardware.h>
311#include <asm/arch/omap.h>
312#include <asm/arch/clock.h>
313#include <asm/arch/sys_proto.h>
314#include <power/tps65217.h>
Hannes Schmelzer5639eeb2018-07-06 15:41:28 +0200315
316static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
317
Hannes Schmelzer717ee362019-01-31 09:24:45 +0100318void pmicsetup(u32 mpupll, unsigned int bus)
Hannes Petermaierfb003662014-02-07 08:07:36 +0100319{
320 int mpu_vdd;
321 int usb_cur_lim;
322
Hannes Schmelzer717ee362019-01-31 09:24:45 +0100323 if (power_tps65217_init(bus)) {
324 printf("WARN: cannot setup PMIC 0x24 @ bus #%d, not found!.\n",
325 bus);
Hannes Petermaierfb003662014-02-07 08:07:36 +0100326 return;
327 }
328
329 /* Get the frequency which is defined by device fuses */
330 dpll_mpu_opp100.m = am335x_get_efuse_mpu_max_freq(cdev);
331 printf("detected max. frequency: %d - ", dpll_mpu_opp100.m);
332
333 if (0 != mpupll) {
Hannes Schmelzer29bea942018-07-06 15:41:23 +0200334 dpll_mpu_opp100.m = mpupll;
Hannes Petermaierfb003662014-02-07 08:07:36 +0100335 printf("retuning MPU-PLL to: %d MHz.\n", dpll_mpu_opp100.m);
336 } else {
337 puts("ok.\n");
338 }
339 /*
340 * Increase USB current limit to 1300mA or 1800mA and set
341 * the MPU voltage controller as needed.
342 */
343 if (dpll_mpu_opp100.m == MPUPLL_M_1000) {
344 usb_cur_lim = TPS65217_USB_INPUT_CUR_LIMIT_1800MA;
345 mpu_vdd = TPS65217_DCDC_VOLT_SEL_1325MV;
346 } else {
347 usb_cur_lim = TPS65217_USB_INPUT_CUR_LIMIT_1300MA;
348 mpu_vdd = TPS65217_DCDC_VOLT_SEL_1275MV;
349 }
350
351 if (tps65217_reg_write(TPS65217_PROT_LEVEL_NONE, TPS65217_POWER_PATH,
352 usb_cur_lim, TPS65217_USB_INPUT_CUR_LIMIT_MASK))
353 puts("tps65217_reg_write failure\n");
354
355 /* Set DCDC3 (CORE) voltage to 1.125V */
356 if (tps65217_voltage_update(TPS65217_DEFDCDC3,
357 TPS65217_DCDC_VOLT_SEL_1125MV)) {
358 puts("tps65217_voltage_update failure\n");
359 return;
360 }
361
362 /* Set CORE Frequencies to OPP100 */
363 do_setup_dpll(&dpll_core_regs, &dpll_core_opp100);
364
365 /* Set DCDC2 (MPU) voltage */
366 if (tps65217_voltage_update(TPS65217_DEFDCDC2, mpu_vdd)) {
367 puts("tps65217_voltage_update failure\n");
368 return;
369 }
370
371 /* Set LDO3 to 1.8V */
372 if (tps65217_reg_write(TPS65217_PROT_LEVEL_2,
373 TPS65217_DEFLS1,
374 TPS65217_LDO_VOLTAGE_OUT_1_8,
375 TPS65217_LDO_MASK))
376 puts("tps65217_reg_write failure\n");
377 /* Set LDO4 to 3.3V */
378 if (tps65217_reg_write(TPS65217_PROT_LEVEL_2,
379 TPS65217_DEFLS2,
380 TPS65217_LDO_VOLTAGE_OUT_3_3,
381 TPS65217_LDO_MASK))
382 puts("tps65217_reg_write failure\n");
383
384 /* Set MPU Frequency to what we detected now that voltages are set */
385 do_setup_dpll(&dpll_mpu_regs, &dpll_mpu_opp100);
Hannes Petermaier69d0d7d2015-02-03 13:22:26 +0100386 /* Set PWR_EN bit in Status Register */
387 tps65217_reg_write(TPS65217_PROT_LEVEL_NONE,
388 TPS65217_STATUS, TPS65217_PWR_OFF, TPS65217_PWR_OFF);
Hannes Petermaierfb003662014-02-07 08:07:36 +0100389}
390
391void set_uart_mux_conf(void)
392{
393 enable_uart0_pin_mux();
394}
395
396void set_mux_conf_regs(void)
397{
398 enable_board_pin_mux();
399}
400
Hannes Schmelzer1386d412019-04-10 14:13:11 +0200401#endif /* CONFIG_SPL_BUILD && CONFIG_AM33XX */