4 * common board functions for B&R boards
6 * Copyright (C) 2013 Hannes Schmelzer <oe5hpm@oevsv.at>
7 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
9 * SPDX-License-Identifier: GPL-2.0+
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>
27 #include <power/tps65217.h>
31 #include <fdt_support.h>
33 #include "bur_common.h"
34 #include "../../../drivers/video/am335x-fb.h"
36 #include <fdt_simplefb.h>
38 static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
40 DECLARE_GLOBAL_DATA_PTR;
43 #define FDTPROP(b, c) fdt_getprop_u32_default(gd->fdt_blob, b, c, ~0UL)
44 #define PATHTIM "/panel/display-timings/default"
45 #define PATHINF "/panel/panel-info"
47 /* --------------------------------------------------------------------------*/
48 #if defined(CONFIG_LCD) && defined(CONFIG_AM335X_LCD) && \
49 !defined(CONFIG_SPL_BUILD)
50 void lcdbacklight(int on)
53 if (gd->fdt_blob == NULL) {
54 printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
57 unsigned int driver = FDTPROP(PATHINF, "brightdrv");
58 unsigned int bright = FDTPROP(PATHINF, "brightdef");
59 unsigned int pwmfrq = FDTPROP(PATHINF, "brightfdim");
61 unsigned int driver = getenv_ulong("ds1_bright_drv", 16, 0UL);
62 unsigned int bright = getenv_ulong("ds1_bright_def", 10, 50);
63 unsigned int pwmfrq = getenv_ulong("ds1_pwmfreq", 10, ~0UL);
66 struct gptimer *timerhw;
69 bright = bright != ~0UL ? bright : 50;
75 timerhw = (struct gptimer *)DM_TIMER5_BASE;
78 timerhw = (struct gptimer *)DM_TIMER6_BASE;
82 case 0: /* PMIC LED-Driver */
83 /* brightness level */
84 tps65217_reg_write(TPS65217_PROT_LEVEL_NONE,
85 TPS65217_WLEDCTRL2, bright, 0xFF);
87 tps65217_reg_write(TPS65217_PROT_LEVEL_NONE,
89 bright != 0 ? 0x0A : 0x02,
93 case 2: /* PWM using timer */
95 timerhw->tiocp_cfg = TCFG_RESET;
97 while (timerhw->tiocp_cfg & TCFG_RESET)
99 tmp = ~0UL-(V_OSCK/pwmfrq); /* bottom value */
102 tmp = tmp + ((V_OSCK/pwmfrq)/100) * bright;
104 timerhw->tclr = (TCLR_PT | (2 << TCLR_TRG_SHIFT) |
105 TCLR_CE | TCLR_AR | TCLR_ST);
107 puts("invalid pwmfrq in env/dtb! skip PWM-setup.\n");
111 puts("no suitable backlightdriver in env/dtb!\n");
116 int load_lcdtiming(struct am335x_lcdpanel *panel)
118 struct am335x_lcdpanel pnltmp;
119 #ifdef CONFIG_USE_FDT
122 const char *nodep = 0;
125 if (gd->fdt_blob == NULL) {
126 printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
129 memcpy(&pnltmp, (void *)panel, sizeof(struct am335x_lcdpanel));
131 pnltmp.hactive = FDTPROP(PATHTIM, "hactive");
132 pnltmp.vactive = FDTPROP(PATHTIM, "vactive");
133 pnltmp.bpp = FDTPROP(PATHINF, "bpp");
134 pnltmp.hfp = FDTPROP(PATHTIM, "hfront-porch");
135 pnltmp.hbp = FDTPROP(PATHTIM, "hback-porch");
136 pnltmp.hsw = FDTPROP(PATHTIM, "hsync-len");
137 pnltmp.vfp = FDTPROP(PATHTIM, "vfront-porch");
138 pnltmp.vbp = FDTPROP(PATHTIM, "vback-porch");
139 pnltmp.vsw = FDTPROP(PATHTIM, "vsync-len");
140 pnltmp.pup_delay = FDTPROP(PATHTIM, "pupdelay");
141 pnltmp.pon_delay = FDTPROP(PATHTIM, "pondelay");
143 /* calc. proper clk-divisor */
144 dtbprop = FDTPROP(PATHTIM, "clock-frequency");
146 pnltmp.pxl_clk_div = 192000000 / dtbprop;
148 pnltmp.pxl_clk_div = ~0UL;
150 /* check polarity of control-signals */
151 dtbprop = FDTPROP(PATHTIM, "hsync-active");
153 pnltmp.pol |= HSYNC_INVERT;
154 dtbprop = FDTPROP(PATHTIM, "vsync-active");
156 pnltmp.pol |= VSYNC_INVERT;
157 dtbprop = FDTPROP(PATHINF, "sync-ctrl");
159 pnltmp.pol |= HSVS_CONTROL;
160 dtbprop = FDTPROP(PATHINF, "sync-edge");
162 pnltmp.pol |= HSVS_RISEFALL;
163 dtbprop = FDTPROP(PATHTIM, "pixelclk-active");
165 pnltmp.pol |= PXCLK_INVERT;
166 dtbprop = FDTPROP(PATHTIM, "de-active");
168 pnltmp.pol |= DE_INVERT;
170 nodeoff = fdt_path_offset(gd->fdt_blob, "/factory-settings");
172 nodep = fdt_getprop(gd->fdt_blob, nodeoff, "rotation", NULL);
174 if (strcmp(nodep, "cw") == 0)
175 panel_info.vl_rot = 1;
176 else if (strcmp(nodep, "ud") == 0)
177 panel_info.vl_rot = 2;
178 else if (strcmp(nodep, "ccw") == 0)
179 panel_info.vl_rot = 3;
181 panel_info.vl_rot = 0;
184 puts("no 'factory-settings / rotation' in dtb!\n");
186 snprintf(buf, sizeof(buf), "fbcon=rotate:%d", panel_info.vl_rot);
187 setenv("optargs_rot", buf);
189 pnltmp.hactive = getenv_ulong("ds1_hactive", 10, ~0UL);
190 pnltmp.vactive = getenv_ulong("ds1_vactive", 10, ~0UL);
191 pnltmp.bpp = getenv_ulong("ds1_bpp", 10, ~0UL);
192 pnltmp.hfp = getenv_ulong("ds1_hfp", 10, ~0UL);
193 pnltmp.hbp = getenv_ulong("ds1_hbp", 10, ~0UL);
194 pnltmp.hsw = getenv_ulong("ds1_hsw", 10, ~0UL);
195 pnltmp.vfp = getenv_ulong("ds1_vfp", 10, ~0UL);
196 pnltmp.vbp = getenv_ulong("ds1_vbp", 10, ~0UL);
197 pnltmp.vsw = getenv_ulong("ds1_vsw", 10, ~0UL);
198 pnltmp.pxl_clk_div = getenv_ulong("ds1_pxlclkdiv", 10, ~0UL);
199 pnltmp.pol = getenv_ulong("ds1_pol", 16, ~0UL);
200 pnltmp.pup_delay = getenv_ulong("ds1_pupdelay", 10, ~0UL);
201 pnltmp.pon_delay = getenv_ulong("ds1_tondelay", 10, ~0UL);
202 panel_info.vl_rot = getenv_ulong("ds1_rotation", 10, 0);
205 ~0UL == (pnltmp.hactive) ||
206 ~0UL == (pnltmp.vactive) ||
207 ~0UL == (pnltmp.bpp) ||
208 ~0UL == (pnltmp.hfp) ||
209 ~0UL == (pnltmp.hbp) ||
210 ~0UL == (pnltmp.hsw) ||
211 ~0UL == (pnltmp.vfp) ||
212 ~0UL == (pnltmp.vbp) ||
213 ~0UL == (pnltmp.vsw) ||
214 ~0UL == (pnltmp.pxl_clk_div) ||
215 ~0UL == (pnltmp.pol) ||
216 ~0UL == (pnltmp.pup_delay) ||
217 ~0UL == (pnltmp.pon_delay)
219 puts("lcd-settings in env/dtb incomplete!\n");
220 printf("display-timings:\n"
234 pnltmp.hactive, pnltmp.vactive, pnltmp.bpp,
235 pnltmp.hfp, pnltmp.hbp, pnltmp.hsw,
236 pnltmp.vfp, pnltmp.vbp, pnltmp.vsw,
237 pnltmp.pxl_clk_div, pnltmp.pol, pnltmp.pon_delay);
241 debug("lcd-settings in env complete, taking over.\n");
242 memcpy((void *)panel,
244 sizeof(struct am335x_lcdpanel));
249 #ifdef CONFIG_USE_FDT
250 static int load_devicetree(void)
254 u32 dtbaddr = getenv_ulong("dtbaddr", 16, 0UL);
257 printf("%s: don't have a valid <dtbaddr> in env!\n", __func__);
262 rc = nand_read_skip_bad(nand_info[0], 0x40000, (size_t *)&dtbsize,
263 NULL, 0x20000, (u_char *)dtbaddr);
265 char *dtbname = getenv("dtb");
266 char *dtbdev = getenv("dtbdev");
267 char *dtppart = getenv("dtbpart");
268 if (!dtbdev || !dtbdev || !dtbname) {
269 printf("%s: <dtbdev>/<dtbpart>/<dtb> missing.\n", __func__);
273 if (fs_set_blk_dev(dtbdev, dtppart, FS_TYPE_EXT)) {
274 puts("load_devicetree: set_blk_dev failed.\n");
277 rc = fs_read(dtbname, (u32)dtbaddr, 0, 0, &dtbsize);
280 gd->fdt_blob = (void *)dtbaddr;
281 gd->fdt_size = dtbsize;
282 debug("loaded %d bytes of dtb onto 0x%08x\n",
283 (u32)dtbsize, (u32)gd->fdt_blob);
287 printf("%s: load dtb failed!\n", __func__);
291 static const char *dtbmacaddr(u32 ifno)
298 if (gd->fdt_blob == NULL) {
299 printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
303 node = fdt_path_offset(gd->fdt_blob, "/aliases");
307 sprintf(enet, "ethernet%d", ifno);
308 path = fdt_getprop(gd->fdt_blob, node, enet, NULL);
310 printf("no alias for %s\n", enet);
314 node = fdt_path_offset(gd->fdt_blob, path);
315 mac = fdt_getprop(gd->fdt_blob, node, "mac-address", &len);
316 if (mac && is_valid_ethaddr((u8 *)mac))
322 static void br_summaryscreen_printdtb(char *prefix,
326 char buf[32] = { 0 };
327 const char *nodep = buf;
332 if (gd->fdt_blob == NULL) {
333 printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
337 if (strcmp(name, "brmac1") == 0) {
338 mac = (char *)dtbmacaddr(0);
340 sprintf(buf, "%pM", mac);
341 } else if (strcmp(name, "brmac2") == 0) {
342 mac = (char *)dtbmacaddr(1);
344 sprintf(buf, "%pM", mac);
346 nodeoffset = fdt_path_offset(gd->fdt_blob,
347 "/factory-settings");
348 if (nodeoffset < 0) {
349 puts("no 'factory-settings' in dtb!\n");
352 nodep = fdt_getprop(gd->fdt_blob, nodeoffset, name, &len);
354 if (nodep && strlen(nodep) > 1)
355 lcd_printf("%s %s %s", prefix, nodep, suffix);
359 int ft_board_setup(void *blob, bd_t *bd)
363 nodeoffset = fdt_path_offset(blob, "/factory-settings");
364 if (nodeoffset < 0) {
365 puts("set bootloader version 'factory-settings' not in dtb!\n");
368 if (fdt_setprop(blob, nodeoffset, "bl-version",
369 PLAIN_VERSION, strlen(PLAIN_VERSION)) != 0) {
370 puts("set bootloader version 'bl-version' prop. not in dtb!\n");
374 * if no simplefb is requested through environment, we don't set up
375 * one, instead we turn off backlight.
377 if (getenv_ulong("simplefb", 10, 0) == 0) {
381 /* Setup simplefb devicetree node, also adapt memory-node,
382 * upper limit for kernel e.g. linux is memtop-framebuffer alligned
383 * to a full megabyte.
385 u64 start = gd->bd->bi_dram[0].start;
386 u64 size = (gd->fb_base - start) & ~0xFFFFF;
387 int rc = fdt_fixup_memory_banks(blob, &start, &size, 1);
390 puts("cannot setup simplefb: Error reserving memory!\n");
393 rc = lcd_dt_simplefb_enable_existing_node(blob);
395 puts("cannot setup simplefb: error enabling simplefb node!\n");
403 static void br_summaryscreen_printenv(char *prefix,
404 char *name, char *altname,
407 char *envval = getenv(name);
409 lcd_printf("%s %s %s", prefix, envval, suffix);
410 } else if (0 != altname) {
411 envval = getenv(altname);
413 lcd_printf("%s %s %s", prefix, envval, suffix);
419 void br_summaryscreen(void)
421 #ifdef CONFIG_USE_FDT
422 br_summaryscreen_printdtb(" - B&R -", "order-no", "-\n");
423 br_summaryscreen_printdtb(" Serial/Rev :", "serial-no", " /");
424 br_summaryscreen_printdtb(" ", "hw-revision", "\n");
425 br_summaryscreen_printdtb(" MAC (IF1) :", "brmac1", "\n");
426 br_summaryscreen_printdtb(" MAC (IF2) :", "brmac2", "\n");
427 lcd_puts(" Bootloader : " PLAIN_VERSION "\n");
430 br_summaryscreen_printenv(" - B&R -", "br_orderno", 0, "-\n");
431 br_summaryscreen_printenv(" Serial/Rev :", "br_serial", 0, "\n");
432 br_summaryscreen_printenv(" MAC (IF1) :", "br_mac1", "ethaddr", "\n");
433 br_summaryscreen_printenv(" MAC (IF2) :", "br_mac2", 0, "\n");
434 lcd_puts(" Bootloader : " PLAIN_VERSION "\n");
439 void lcdpower(int on)
442 #ifdef CONFIG_USE_FDT
443 if (gd->fdt_blob == NULL) {
444 printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
447 pin = FDTPROP(PATHINF, "pwrpin");
449 pin = getenv_ulong("ds1_pwr", 16, ~0UL);
452 puts("no pwrpin in dtb/env, cannot powerup display!\n");
456 for (i = 0; i < 3; i++) {
458 swval = pin & 0x80 ? 0 : 1;
460 gpio_direction_output(pin & 0x7F, swval);
462 gpio_direction_output(pin & 0x7F, !swval);
464 debug("switched pin %d to %d\n", pin & 0x7F, swval);
470 vidinfo_t panel_info = {
472 * give full resolution for allocating enough
480 void lcd_ctrl_init(void *lcdbase)
482 struct am335x_lcdpanel lcd_panel;
483 #ifdef CONFIG_USE_FDT
484 /* TODO: is there a better place to load the dtb ? */
487 memset(&lcd_panel, 0, sizeof(struct am335x_lcdpanel));
488 if (load_lcdtiming(&lcd_panel) != 0)
491 lcd_panel.panel_power_ctrl = &lcdpower;
493 if (0 != am335xfb_init(&lcd_panel))
494 printf("ERROR: failed to initialize video!");
496 * modifiy panel info to 'real' resolution, to operate correct with
499 panel_info.vl_col = lcd_panel.hactive;
500 panel_info.vl_row = lcd_panel.vactive;
502 lcd_set_flush_dcache(1);
505 void lcd_enable(void)
510 #elif CONFIG_SPL_BUILD
512 #error "LCD-support with a suitable FB-Driver is mandatory !"
513 #endif /* CONFIG_LCD */
515 #ifdef CONFIG_SPL_BUILD
516 void pmicsetup(u32 mpupll)
521 if (i2c_probe(TPS65217_CHIP_PM)) {
522 puts("PMIC (0x24) not found! skip further initalization.\n");
526 /* Get the frequency which is defined by device fuses */
527 dpll_mpu_opp100.m = am335x_get_efuse_mpu_max_freq(cdev);
528 printf("detected max. frequency: %d - ", dpll_mpu_opp100.m);
531 dpll_mpu_opp100.m = MPUPLL_M_1000;
532 printf("retuning MPU-PLL to: %d MHz.\n", dpll_mpu_opp100.m);
537 * Increase USB current limit to 1300mA or 1800mA and set
538 * the MPU voltage controller as needed.
540 if (dpll_mpu_opp100.m == MPUPLL_M_1000) {
541 usb_cur_lim = TPS65217_USB_INPUT_CUR_LIMIT_1800MA;
542 mpu_vdd = TPS65217_DCDC_VOLT_SEL_1325MV;
544 usb_cur_lim = TPS65217_USB_INPUT_CUR_LIMIT_1300MA;
545 mpu_vdd = TPS65217_DCDC_VOLT_SEL_1275MV;
548 if (tps65217_reg_write(TPS65217_PROT_LEVEL_NONE, TPS65217_POWER_PATH,
549 usb_cur_lim, TPS65217_USB_INPUT_CUR_LIMIT_MASK))
550 puts("tps65217_reg_write failure\n");
552 /* Set DCDC3 (CORE) voltage to 1.125V */
553 if (tps65217_voltage_update(TPS65217_DEFDCDC3,
554 TPS65217_DCDC_VOLT_SEL_1125MV)) {
555 puts("tps65217_voltage_update failure\n");
559 /* Set CORE Frequencies to OPP100 */
560 do_setup_dpll(&dpll_core_regs, &dpll_core_opp100);
562 /* Set DCDC2 (MPU) voltage */
563 if (tps65217_voltage_update(TPS65217_DEFDCDC2, mpu_vdd)) {
564 puts("tps65217_voltage_update failure\n");
568 /* Set LDO3 to 1.8V */
569 if (tps65217_reg_write(TPS65217_PROT_LEVEL_2,
571 TPS65217_LDO_VOLTAGE_OUT_1_8,
573 puts("tps65217_reg_write failure\n");
574 /* Set LDO4 to 3.3V */
575 if (tps65217_reg_write(TPS65217_PROT_LEVEL_2,
577 TPS65217_LDO_VOLTAGE_OUT_3_3,
579 puts("tps65217_reg_write failure\n");
581 /* Set MPU Frequency to what we detected now that voltages are set */
582 do_setup_dpll(&dpll_mpu_regs, &dpll_mpu_opp100);
583 /* Set PWR_EN bit in Status Register */
584 tps65217_reg_write(TPS65217_PROT_LEVEL_NONE,
585 TPS65217_STATUS, TPS65217_PWR_OFF, TPS65217_PWR_OFF);
588 void set_uart_mux_conf(void)
590 enable_uart0_pin_mux();
593 void set_mux_conf_regs(void)
595 enable_board_pin_mux();
598 #endif /* CONFIG_SPL_BUILD */
600 #if (defined(CONFIG_DRIVER_TI_CPSW) && !defined(CONFIG_SPL_BUILD)) || \
601 (defined(CONFIG_SPL_ETH_SUPPORT) && defined(CONFIG_SPL_BUILD))
602 static void cpsw_control(int enabled)
604 /* VTP can be added here */
608 /* describing port offsets of TI's CPSW block */
609 static struct cpsw_slave_data cpsw_slaves[] = {
611 .slave_reg_ofs = 0x208,
612 .sliver_reg_ofs = 0xd80,
616 .slave_reg_ofs = 0x308,
617 .sliver_reg_ofs = 0xdc0,
622 static struct cpsw_platform_data cpsw_data = {
623 .mdio_base = CPSW_MDIO_BASE,
624 .cpsw_base = CPSW_BASE,
627 .cpdma_reg_ofs = 0x800,
629 .slave_data = cpsw_slaves,
630 .ale_reg_ofs = 0xd00,
632 .host_port_reg_ofs = 0x108,
633 .hw_stats_reg_ofs = 0x900,
634 .bd_ram_ofs = 0x2000,
635 .mac_control = (1 << 5),
636 .control = cpsw_control,
638 .version = CPSW_CTRL_VERSION_2,
640 #endif /* CONFIG_DRIVER_TI_CPSW, ... */
642 #if defined(CONFIG_DRIVER_TI_CPSW) && !defined(CONFIG_SPL_BUILD)
643 int board_eth_init(bd_t *bis)
648 uint32_t mac_hi, mac_lo;
649 /* try reading mac address from efuse */
650 mac_lo = readl(&cdev->macid0l);
651 mac_hi = readl(&cdev->macid0h);
652 mac_addr[0] = mac_hi & 0xFF;
653 mac_addr[1] = (mac_hi & 0xFF00) >> 8;
654 mac_addr[2] = (mac_hi & 0xFF0000) >> 16;
655 mac_addr[3] = (mac_hi & 0xFF000000) >> 24;
656 mac_addr[4] = mac_lo & 0xFF;
657 mac_addr[5] = (mac_lo & 0xFF00) >> 8;
659 if (!getenv("ethaddr")) {
660 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_USE_FDT)
661 printf("<ethaddr> not set. trying DTB ... ");
665 printf("<ethaddr> not set. validating E-fuse MAC ... ");
666 if (is_valid_ethaddr((const u8 *)mac_addr))
667 mac = (const char *)mac_addr;
671 printf("using: %pM on ", mac);
672 eth_setenv_enetaddr("ethaddr", (const u8 *)mac);
675 writel(MII_MODE_ENABLE, &cdev->miisel);
676 cpsw_slaves[0].phy_if = PHY_INTERFACE_MODE_MII;
677 cpsw_slaves[1].phy_if = PHY_INTERFACE_MODE_MII;
679 rv = cpsw_register(&cpsw_data);
681 printf("Error %d registering CPSW switch\n", rv);
686 #endif /* defined(CONFIG_DRIVER_TI_CPSW) && !defined(CONFIG_SPL_BUILD) */
687 #if defined(CONFIG_GENERIC_MMC) && !defined(CONFIG_SPL_BUILD)
688 int board_mmc_init(bd_t *bis)
690 return omap_mmc_init(1, 0, 0, -1, -1);
693 int overwrite_console(void)