common.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. DECLARE_GLOBAL_DATA_PTR;
  30. /* --------------------------------------------------------------------------*/
  31. #if defined(CONFIG_LCD) && defined(CONFIG_AM335X_LCD) && \
  32. !defined(CONFIG_SPL_BUILD)
  33. void lcdbacklight(int on)
  34. {
  35. unsigned int driver = env_get_ulong("ds1_bright_drv", 16, 0UL);
  36. unsigned int bright = env_get_ulong("ds1_bright_def", 10, 50);
  37. unsigned int pwmfrq = env_get_ulong("ds1_pwmfreq", 10, ~0UL);
  38. unsigned int tmp;
  39. struct gptimer *timerhw;
  40. if (on)
  41. bright = bright != ~0UL ? bright : 50;
  42. else
  43. bright = 0;
  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. 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. int load_lcdtiming(struct am335x_lcdpanel *panel)
  86. {
  87. struct am335x_lcdpanel pnltmp;
  88. pnltmp.hactive = env_get_ulong("ds1_hactive", 10, ~0UL);
  89. pnltmp.vactive = env_get_ulong("ds1_vactive", 10, ~0UL);
  90. pnltmp.bpp = env_get_ulong("ds1_bpp", 10, ~0UL);
  91. pnltmp.hfp = env_get_ulong("ds1_hfp", 10, ~0UL);
  92. pnltmp.hbp = env_get_ulong("ds1_hbp", 10, ~0UL);
  93. pnltmp.hsw = env_get_ulong("ds1_hsw", 10, ~0UL);
  94. pnltmp.vfp = env_get_ulong("ds1_vfp", 10, ~0UL);
  95. pnltmp.vbp = env_get_ulong("ds1_vbp", 10, ~0UL);
  96. pnltmp.vsw = env_get_ulong("ds1_vsw", 10, ~0UL);
  97. pnltmp.pxl_clk = env_get_ulong("ds1_pxlclk", 10, ~0UL);
  98. pnltmp.pol = env_get_ulong("ds1_pol", 16, ~0UL);
  99. pnltmp.pup_delay = env_get_ulong("ds1_pupdelay", 10, ~0UL);
  100. pnltmp.pon_delay = env_get_ulong("ds1_tondelay", 10, ~0UL);
  101. panel_info.vl_rot = env_get_ulong("ds1_rotation", 10, 0);
  102. if (
  103. ~0UL == (pnltmp.hactive) ||
  104. ~0UL == (pnltmp.vactive) ||
  105. ~0UL == (pnltmp.bpp) ||
  106. ~0UL == (pnltmp.hfp) ||
  107. ~0UL == (pnltmp.hbp) ||
  108. ~0UL == (pnltmp.hsw) ||
  109. ~0UL == (pnltmp.vfp) ||
  110. ~0UL == (pnltmp.vbp) ||
  111. ~0UL == (pnltmp.vsw) ||
  112. ~0UL == (pnltmp.pxl_clk) ||
  113. ~0UL == (pnltmp.pol) ||
  114. ~0UL == (pnltmp.pup_delay) ||
  115. ~0UL == (pnltmp.pon_delay)
  116. ) {
  117. puts("lcd-settings in env/dtb incomplete!\n");
  118. printf("display-timings:\n"
  119. "================\n"
  120. "hactive: %d\n"
  121. "vactive: %d\n"
  122. "bpp : %d\n"
  123. "hfp : %d\n"
  124. "hbp : %d\n"
  125. "hsw : %d\n"
  126. "vfp : %d\n"
  127. "vbp : %d\n"
  128. "vsw : %d\n"
  129. "pxlclk : %d\n"
  130. "pol : 0x%08x\n"
  131. "pondly : %d\n",
  132. pnltmp.hactive, pnltmp.vactive, pnltmp.bpp,
  133. pnltmp.hfp, pnltmp.hbp, pnltmp.hsw,
  134. pnltmp.vfp, pnltmp.vbp, pnltmp.vsw,
  135. pnltmp.pxl_clk, pnltmp.pol, pnltmp.pon_delay);
  136. return -1;
  137. }
  138. debug("lcd-settings in env complete, taking over.\n");
  139. memcpy((void *)panel,
  140. (void *)&pnltmp,
  141. sizeof(struct am335x_lcdpanel));
  142. return 0;
  143. }
  144. static void br_summaryscreen_printenv(char *prefix,
  145. char *name, char *altname,
  146. char *suffix)
  147. {
  148. char *envval = env_get(name);
  149. if (0 != envval) {
  150. lcd_printf("%s %s %s", prefix, envval, suffix);
  151. } else if (0 != altname) {
  152. envval = env_get(altname);
  153. if (0 != envval)
  154. lcd_printf("%s %s %s", prefix, envval, suffix);
  155. } else {
  156. lcd_printf("\n");
  157. }
  158. }
  159. void br_summaryscreen(void)
  160. {
  161. br_summaryscreen_printenv(" - B&R -", "br_orderno", 0, "-\n");
  162. br_summaryscreen_printenv(" Serial/Rev :", "br_serial", 0, "\n");
  163. br_summaryscreen_printenv(" MAC1 :", "br_mac1", "ethaddr", "\n");
  164. br_summaryscreen_printenv(" MAC2 :", "br_mac2", 0, "\n");
  165. lcd_puts(" Bootloader : " PLAIN_VERSION "\n");
  166. lcd_puts("\n");
  167. }
  168. void lcdpower(int on)
  169. {
  170. u32 pin, swval, i;
  171. pin = env_get_ulong("ds1_pwr", 16, ~0UL);
  172. if (pin == ~0UL) {
  173. puts("no pwrpin in dtb/env, cannot powerup display!\n");
  174. return;
  175. }
  176. for (i = 0; i < 3; i++) {
  177. if (pin != 0) {
  178. swval = pin & 0x80 ? 0 : 1;
  179. if (on)
  180. gpio_direction_output(pin & 0x7F, swval);
  181. else
  182. gpio_direction_output(pin & 0x7F, !swval);
  183. debug("switched pin %d to %d\n", pin & 0x7F, swval);
  184. }
  185. pin >>= 8;
  186. }
  187. }
  188. vidinfo_t panel_info = {
  189. .vl_col = 1366, /*
  190. * give full resolution for allocating enough
  191. * memory
  192. */
  193. .vl_row = 768,
  194. .vl_bpix = 5,
  195. .priv = 0
  196. };
  197. void lcd_ctrl_init(void *lcdbase)
  198. {
  199. struct am335x_lcdpanel lcd_panel;
  200. memset(&lcd_panel, 0, sizeof(struct am335x_lcdpanel));
  201. if (load_lcdtiming(&lcd_panel) != 0)
  202. return;
  203. lcd_panel.panel_power_ctrl = &lcdpower;
  204. if (0 != am335xfb_init(&lcd_panel))
  205. printf("ERROR: failed to initialize video!");
  206. /*
  207. * modifiy panel info to 'real' resolution, to operate correct with
  208. * lcd-framework.
  209. */
  210. panel_info.vl_col = lcd_panel.hactive;
  211. panel_info.vl_row = lcd_panel.vactive;
  212. lcd_set_flush_dcache(1);
  213. }
  214. void lcd_enable(void)
  215. {
  216. br_summaryscreen();
  217. lcdbacklight(1);
  218. }
  219. #endif /* CONFIG_LCD */
  220. int ft_board_setup(void *blob, bd_t *bd)
  221. {
  222. int nodeoffset;
  223. nodeoffset = fdt_path_offset(blob, "/factory-settings");
  224. if (nodeoffset < 0) {
  225. printf("%s: cannot find /factory-settings, trying /fset\n",
  226. __func__);
  227. nodeoffset = fdt_path_offset(blob, "/fset");
  228. if (nodeoffset < 0) {
  229. printf("%s: cannot find /fset.\n", __func__);
  230. return 0;
  231. }
  232. }
  233. if (fdt_setprop(blob, nodeoffset, "bl-version",
  234. PLAIN_VERSION, strlen(PLAIN_VERSION)) != 0) {
  235. printf("%s: no 'bl-version' prop in fdt!\n", __func__);
  236. return 0;
  237. }
  238. return 0;
  239. }
  240. #ifdef CONFIG_SPL_BUILD
  241. static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
  242. void pmicsetup(u32 mpupll)
  243. {
  244. int mpu_vdd;
  245. int usb_cur_lim;
  246. if (i2c_probe(TPS65217_CHIP_PM)) {
  247. puts("PMIC (0x24) not found! skip further initalization.\n");
  248. return;
  249. }
  250. /* Get the frequency which is defined by device fuses */
  251. dpll_mpu_opp100.m = am335x_get_efuse_mpu_max_freq(cdev);
  252. printf("detected max. frequency: %d - ", dpll_mpu_opp100.m);
  253. if (0 != mpupll) {
  254. dpll_mpu_opp100.m = mpupll;
  255. printf("retuning MPU-PLL to: %d MHz.\n", dpll_mpu_opp100.m);
  256. } else {
  257. puts("ok.\n");
  258. }
  259. /*
  260. * Increase USB current limit to 1300mA or 1800mA and set
  261. * the MPU voltage controller as needed.
  262. */
  263. if (dpll_mpu_opp100.m == MPUPLL_M_1000) {
  264. usb_cur_lim = TPS65217_USB_INPUT_CUR_LIMIT_1800MA;
  265. mpu_vdd = TPS65217_DCDC_VOLT_SEL_1325MV;
  266. } else {
  267. usb_cur_lim = TPS65217_USB_INPUT_CUR_LIMIT_1300MA;
  268. mpu_vdd = TPS65217_DCDC_VOLT_SEL_1275MV;
  269. }
  270. if (tps65217_reg_write(TPS65217_PROT_LEVEL_NONE, TPS65217_POWER_PATH,
  271. usb_cur_lim, TPS65217_USB_INPUT_CUR_LIMIT_MASK))
  272. puts("tps65217_reg_write failure\n");
  273. /* Set DCDC3 (CORE) voltage to 1.125V */
  274. if (tps65217_voltage_update(TPS65217_DEFDCDC3,
  275. TPS65217_DCDC_VOLT_SEL_1125MV)) {
  276. puts("tps65217_voltage_update failure\n");
  277. return;
  278. }
  279. /* Set CORE Frequencies to OPP100 */
  280. do_setup_dpll(&dpll_core_regs, &dpll_core_opp100);
  281. /* Set DCDC2 (MPU) voltage */
  282. if (tps65217_voltage_update(TPS65217_DEFDCDC2, mpu_vdd)) {
  283. puts("tps65217_voltage_update failure\n");
  284. return;
  285. }
  286. /* Set LDO3 to 1.8V */
  287. if (tps65217_reg_write(TPS65217_PROT_LEVEL_2,
  288. TPS65217_DEFLS1,
  289. TPS65217_LDO_VOLTAGE_OUT_1_8,
  290. TPS65217_LDO_MASK))
  291. puts("tps65217_reg_write failure\n");
  292. /* Set LDO4 to 3.3V */
  293. if (tps65217_reg_write(TPS65217_PROT_LEVEL_2,
  294. TPS65217_DEFLS2,
  295. TPS65217_LDO_VOLTAGE_OUT_3_3,
  296. TPS65217_LDO_MASK))
  297. puts("tps65217_reg_write failure\n");
  298. /* Set MPU Frequency to what we detected now that voltages are set */
  299. do_setup_dpll(&dpll_mpu_regs, &dpll_mpu_opp100);
  300. /* Set PWR_EN bit in Status Register */
  301. tps65217_reg_write(TPS65217_PROT_LEVEL_NONE,
  302. TPS65217_STATUS, TPS65217_PWR_OFF, TPS65217_PWR_OFF);
  303. }
  304. void set_uart_mux_conf(void)
  305. {
  306. enable_uart0_pin_mux();
  307. }
  308. void set_mux_conf_regs(void)
  309. {
  310. enable_board_pin_mux();
  311. }
  312. #endif /* CONFIG_SPL_BUILD */
  313. int overwrite_console(void)
  314. {
  315. return 1;
  316. }