common.c 10 KB

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