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