Merge branch '2019-08-11-master-imports'
[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 <version.h>
12 #include <common.h>
13 #include <env.h>
14 #include <fdtdec.h>
15 #include <i2c.h>
16 #include <lcd.h>
17 #include "bur_common.h"
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 /* --------------------------------------------------------------------------*/
22 #if defined(CONFIG_LCD) && defined(CONFIG_AM335X_LCD) && \
23         !defined(CONFIG_SPL_BUILD)
24 #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
30 void lcdbacklight(int on)
31 {
32         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);
35         unsigned int tmp;
36         struct gptimer *timerhw;
37
38         if (on)
39                 bright = bright != ~0UL ? bright : 50;
40         else
41                 bright = 0;
42
43         switch (driver) {
44         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) {
52         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;
62         case 1:
63         case 2: /* PWM using timer */
64                 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
86 int load_lcdtiming(struct am335x_lcdpanel *panel)
87 {
88         struct am335x_lcdpanel pnltmp;
89
90         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);
99         pnltmp.pxl_clk = env_get_ulong("ds1_pxlclk", 10, ~0UL);
100         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);
104
105         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) ||
115            ~0UL == (pnltmp.pxl_clk) ||
116            ~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,
138                         pnltmp.pxl_clk, pnltmp.pol, pnltmp.pon_delay);
139
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
150 static void br_summaryscreen_printenv(char *prefix,
151                                        char *name, char *altname,
152                                        char *suffix)
153 {
154         char *envval = env_get(name);
155         if (0 != envval) {
156                 lcd_printf("%s %s %s", prefix, envval, suffix);
157         } else if (0 != altname) {
158                 envval = env_get(altname);
159                 if (0 != envval)
160                         lcd_printf("%s %s %s", prefix, envval, suffix);
161         } else {
162                 lcd_printf("\n");
163         }
164 }
165
166 void br_summaryscreen(void)
167 {
168         br_summaryscreen_printenv(" - B&R -", "br_orderno", 0, "-\n");
169         br_summaryscreen_printenv(" Serial/Rev :", "br_serial", 0, "\n");
170         br_summaryscreen_printenv(" MAC1       :", "br_mac1", "ethaddr", "\n");
171         br_summaryscreen_printenv(" MAC2       :", "br_mac2", 0, "\n");
172         lcd_puts(" Bootloader : " PLAIN_VERSION "\n");
173         lcd_puts("\n");
174 }
175
176 void lcdpower(int on)
177 {
178         u32 pin, swval, i;
179         char buf[16] = { 0 };
180
181         pin = env_get_ulong("ds1_pwr", 16, ~0UL);
182
183         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) {
190                         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                         }
196                         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
208 vidinfo_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
218 void lcd_ctrl_init(void *lcdbase)
219 {
220         struct am335x_lcdpanel lcd_panel;
221
222         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
240 void lcd_enable(void)
241 {
242         br_summaryscreen();
243         lcdbacklight(1);
244 }
245 #endif /* CONFIG_LCD */
246
247 int 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) {
253                 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                 }
260         }
261
262         if (fdt_setprop(blob, nodeoffset, "bl-version",
263                         PLAIN_VERSION, strlen(PLAIN_VERSION)) != 0) {
264                 printf("%s: no 'bl-version' prop in fdt!\n", __func__);
265                 return 0;
266         }
267         return 0;
268 }
269
270 int 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
304 int overwrite_console(void)
305 {
306         return 1;
307 }
308
309 #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>
315
316 static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
317
318 void pmicsetup(u32 mpupll, unsigned int bus)
319 {
320         int mpu_vdd;
321         int usb_cur_lim;
322
323         if (power_tps65217_init(bus)) {
324                 printf("WARN: cannot setup PMIC 0x24 @ bus #%d, not found!.\n",
325                        bus);
326                 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) {
334                 dpll_mpu_opp100.m = mpupll;
335                 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);
386         /* Set PWR_EN bit in Status Register */
387         tps65217_reg_write(TPS65217_PROT_LEVEL_NONE,
388                            TPS65217_STATUS, TPS65217_PWR_OFF, TPS65217_PWR_OFF);
389 }
390
391 void set_uart_mux_conf(void)
392 {
393         enable_uart0_pin_mux();
394 }
395
396 void set_mux_conf_regs(void)
397 {
398         enable_board_pin_mux();
399 }
400
401 #endif /* CONFIG_SPL_BUILD && CONFIG_AM33XX */