1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include "pitx_misc.h"
4 #include <common.h>
5 #include <efi.h>
6 #include <efi_loader.h>
7 #include <init.h>
8 #include <mmc.h>
9 #include <miiphy.h>
10 #include <asm/arch/clock.h>
11 #include <asm/arch/imx8mq_pins.h>
12 #include <asm/arch/sys_proto.h>
13 #include <asm-generic/gpio.h>
14 #include <asm/mach-imx/gpio.h>
15 #include <asm/mach-imx/iomux-v3.h>
16 #include <linux/delay.h>
17 #include <linux/kernel.h>
18 
19 DECLARE_GLOBAL_DATA_PTR;
20 
21 #define UART_PAD_CTRL	(PAD_CTL_DSE6 | PAD_CTL_FSEL1)
22 #define WDOG_PAD_CTRL	(PAD_CTL_DSE6 | PAD_CTL_HYS | PAD_CTL_PUE)
23 
24 static iomux_v3_cfg_t const wdog_pads[] = {
25 	IMX8MQ_PAD_GPIO1_IO02__WDOG1_WDOG_B | MUX_PAD_CTRL(WDOG_PAD_CTRL),
26 };
27 
28 static iomux_v3_cfg_t const uart_pads[] = {
29 	IMX8MQ_PAD_UART3_RXD__UART3_RX | MUX_PAD_CTRL(UART_PAD_CTRL),
30 	IMX8MQ_PAD_UART3_TXD__UART3_TX | MUX_PAD_CTRL(UART_PAD_CTRL),
31 	IMX8MQ_PAD_ECSPI1_SS0__UART3_RTS_B | MUX_PAD_CTRL(UART_PAD_CTRL),
32 	IMX8MQ_PAD_ECSPI1_MISO__UART3_CTS_B | MUX_PAD_CTRL(UART_PAD_CTRL),
33 };
34 
35 #if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
36 struct efi_fw_image fw_images[] = {
37 	{
38 		.image_type_id = KONTRON_PITX_IMX8M_FIT_IMAGE_GUID,
39 		.fw_name = u"KONTRON-PITX-IMX8M-UBOOT",
40 		.image_index = 1,
41 	},
42 };
43 
44 struct efi_capsule_update_info update_info = {
45 	.dfu_string = "mmc 0=flash-bin raw 0x42 0x1000 mmcpart 1",
46 	.images = fw_images,
47 };
48 
49 u8 num_image_type_guids = ARRAY_SIZE(fw_images);
50 #endif /* EFI_HAVE_CAPSULE_SUPPORT */
51 
board_early_init_f(void)52 int board_early_init_f(void)
53 {
54 	struct wdog_regs *wdog = (struct wdog_regs *)WDOG1_BASE_ADDR;
55 
56 	imx_iomux_v3_setup_multiple_pads(wdog_pads, ARRAY_SIZE(wdog_pads));
57 	set_wdog_reset(wdog);
58 
59 	imx_iomux_v3_setup_multiple_pads(uart_pads, ARRAY_SIZE(uart_pads));
60 
61 	return 0;
62 }
63 
board_phys_sdram_size(phys_size_t * memsize)64 int board_phys_sdram_size(phys_size_t *memsize)
65 {
66 	int variant = 0;
67 
68 	variant = get_pitx_board_variant();
69 
70 	switch(variant) {
71 	case 2:
72 		*memsize = 0x80000000;
73 		break;
74 	case 3:
75 		*memsize = 0x100000000;
76 		break;
77 	default:
78 		printf("Unknown DDR type!!!\n");
79 		*memsize = 0x40000000;
80 		break;
81 	}
82 
83 	debug("Memsize: %d MiB\n", (int)(*memsize >> 20));
84 
85 	return 0;
86 }
87 
88 
89 #ifdef CONFIG_FEC_MXC
90 #define FEC_RST_PAD IMX_GPIO_NR(1, 11)
91 static iomux_v3_cfg_t const fec1_rst_pads[] = {
92 	IMX8MQ_PAD_GPIO1_IO11__GPIO1_IO11 | MUX_PAD_CTRL(NO_PAD_CTRL),
93 };
94 
setup_fec(void)95 static void setup_fec(void)
96 {
97 	imx_iomux_v3_setup_multiple_pads(fec1_rst_pads,
98 					 ARRAY_SIZE(fec1_rst_pads));
99 }
100 
board_phy_config(struct phy_device * phydev)101 int board_phy_config(struct phy_device *phydev)
102 {
103 	unsigned int val;
104 
105 	/*
106 	 * Set LED configuration register 1:
107 	 * LED2_SEL: 0b1011 (link established, blink on activity)
108 	 */
109 	val = phy_read(phydev, MDIO_DEVAD_NONE, 0x18);
110 	val &= 0xf0ff;
111 	phy_write(phydev, MDIO_DEVAD_NONE, 0x18, val | (0xb << 8));
112 
113 	if (phydev->drv->config)
114 		phydev->drv->config(phydev);
115 	return 0;
116 }
117 #endif
118 
board_init(void)119 int board_init(void)
120 {
121 #ifdef CONFIG_FEC_MXC
122 	setup_fec();
123 #endif
124 
125 #if defined(CONFIG_USB_DWC3) || defined(CONFIG_USB_XHCI_DWC3)
126 	init_usb_clk();
127 #endif
128 
129 	return 0;
130 }
131 
132 #ifdef CONFIG_MISC_INIT_R
133 #define TPM_RESET    IMX_GPIO_NR(3, 2)
134 #define USBHUB_RESET IMX_GPIO_NR(3, 4)
135 
reset_device_by_gpio(const char * label,int pin,int delay_ms)136 static void reset_device_by_gpio(const char *label, int pin, int delay_ms)
137 {
138 	gpio_request(pin, label);
139 	gpio_direction_output(pin, 0);
140 	mdelay(delay_ms);
141 	gpio_direction_output(pin, 1);
142 }
143 
misc_init_r(void)144 int misc_init_r(void)
145 {
146 	/*
147 	 * reset TPM chip (Infineon SLB9670) as required by datasheet
148 	 * (60ms minimum Reset Inactive Time, 70ms implemented)
149 	 */
150 	reset_device_by_gpio("tpm_reset", TPM_RESET, 70);
151 
152 	/*
153 	 * reset USB hub as required by datasheet
154 	 * (3ms minimum reset duration, 10ms implemented)
155 	 */
156 	reset_device_by_gpio("usbhub_reset", USBHUB_RESET, 10);
157 
158 	return 0;
159 }
160 #endif
161 
board_mmc_get_env_dev(int devno)162 int board_mmc_get_env_dev(int devno)
163 {
164 	return devno;
165 }
166 
mmc_get_env_part(struct mmc * mmc)167 uint mmc_get_env_part(struct mmc *mmc)
168 {
169 	/* part 1 for eMMC, part 1 for SD card */
170 	return (mmc_get_env_dev() == 0) ? 1 : 0;
171 }
172 
board_late_init(void)173 int board_late_init(void)
174 {
175 	return 0;
176 }
177