Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / board / compulab / cm_t54 / cm_t54.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Board functions for Compulab CM-T54 board
4  *
5  * Copyright (C) 2014, Compulab Ltd - http://compulab.co.il/
6  *
7  * Author: Dmitry Lifshitz <lifshitz@compulab.co.il>
8  */
9
10 #include <common.h>
11 #include <env.h>
12 #include <fdt_support.h>
13 #include <net.h>
14 #include <usb.h>
15 #include <mmc.h>
16 #include <palmas.h>
17 #include <spl.h>
18 #include <linux/delay.h>
19
20 #include <asm/gpio.h>
21 #include <asm/arch/sys_proto.h>
22 #include <asm/arch/mmc_host_def.h>
23 #include <asm/arch/clock.h>
24 #include <asm/arch/ehci.h>
25 #include <asm/ehci-omap.h>
26
27 #include "../common/eeprom.h"
28
29 #define DIE_ID_REG_BASE         (OMAP54XX_L4_CORE_BASE + 0x2000)
30 #define DIE_ID_REG_OFFSET       0x200
31
32 DECLARE_GLOBAL_DATA_PTR;
33
34 #if !defined(CONFIG_SPL_BUILD)
35 inline void set_muxconf_regs(void){};
36 #endif
37
38 const struct omap_sysinfo sysinfo = {
39         "Board: CM-T54\n"
40 };
41
42 /*
43  * Routine: board_init
44  * Description: hardware init.
45  */
46 int board_init(void)
47 {
48         gd->bd->bi_boot_params = (CONFIG_SYS_SDRAM_BASE + 0x100);
49
50         return 0;
51 }
52
53 /*
54  * Routine: cm_t54_palmas_regulator_set
55  * Description:  select voltage and turn on/off Palmas PMIC regulator.
56  */
57 static int cm_t54_palmas_regulator_set(u8 vreg, u8 vval, u8 creg, u8 cval)
58 {
59         int err;
60
61         /* Setup voltage */
62         err = palmas_i2c_write_u8(TWL603X_CHIP_P1, vreg, vval);
63         if (err) {
64                 printf("cm_t54: could not set regulator 0x%02x voltage : %d\n",
65                        vreg, err);
66                 return err;
67         }
68
69         /* Turn on/off regulator */
70         err = palmas_i2c_write_u8(TWL603X_CHIP_P1, creg, cval);
71         if (err) {
72                 printf("cm_t54: could not turn on/off regulator 0x%02x : %d\n",
73                        creg, err);
74                 return err;
75         }
76
77         return 0;
78 }
79
80 /*
81  * Routine: mmc_get_env_part
82  * Description:  setup environment storage device partition.
83  */
84 #ifdef CONFIG_SYS_MMC_ENV_PART
85 uint mmc_get_env_part(struct mmc *mmc)
86 {
87         u32 bootmode = gd->arch.omap_boot_mode;
88         uint bootpart = CONFIG_SYS_MMC_ENV_PART;
89
90         /*
91          * If booted from eMMC boot partition then force eMMC
92          * FIRST boot partition to be env storage
93          */
94         if (bootmode == BOOT_DEVICE_MMC2)
95                 bootpart = 1;
96
97         return bootpart;
98 }
99 #endif
100
101 #if defined(CONFIG_MMC)
102 #define SB_T54_CD_GPIO 228
103 #define SB_T54_WP_GPIO 229
104
105 int board_mmc_init(bd_t *bis)
106 {
107         int ret0, ret1;
108
109         ret0 = omap_mmc_init(0, 0, 0, SB_T54_CD_GPIO, SB_T54_WP_GPIO);
110         if (ret0)
111                 printf("cm_t54: failed to initialize mmc0\n");
112
113         ret1 = omap_mmc_init(1, 0, 0, -1, -1);
114         if (ret1)
115                 printf("cm_t54: failed to initialize mmc1\n");
116
117         if (ret0 && ret1)
118                 return -1;
119
120         return 0;
121 }
122 #endif
123
124 #ifdef CONFIG_USB_HOST_ETHER
125
126 int ft_board_setup(void *blob, bd_t *bd)
127 {
128         uint8_t enetaddr[6];
129
130         /* MAC addr */
131         if (eth_env_get_enetaddr("usbethaddr", enetaddr)) {
132                 fdt_find_and_setprop(blob, "/smsc95xx@0", "mac-address",
133                                      enetaddr, 6, 1);
134         }
135
136         return 0;
137 }
138
139 static void generate_mac_addr(uint8_t *enetaddr)
140 {
141         int reg;
142
143         reg = DIE_ID_REG_BASE + DIE_ID_REG_OFFSET;
144
145         /*
146          * create a fake MAC address from the processor ID code.
147          * first byte is 0x02 to signify locally administered.
148          */
149         enetaddr[0] = 0x02;
150         enetaddr[1] = readl(reg + 0x10) & 0xff;
151         enetaddr[2] = readl(reg + 0xC) & 0xff;
152         enetaddr[3] = readl(reg + 0x8) & 0xff;
153         enetaddr[4] = readl(reg) & 0xff;
154         enetaddr[5] = (readl(reg) >> 8) & 0xff;
155 }
156
157 /*
158  * Routine: handle_mac_address
159  * Description: prepare MAC address for on-board Ethernet.
160  */
161 static int handle_mac_address(void)
162 {
163         uint8_t enetaddr[6];
164         int ret;
165
166         ret = eth_env_get_enetaddr("usbethaddr", enetaddr);
167         if (ret)
168                 return 0;
169
170         ret = cl_eeprom_read_mac_addr(enetaddr, CONFIG_SYS_I2C_EEPROM_BUS);
171         if (ret || !is_valid_ethaddr(enetaddr))
172                 generate_mac_addr(enetaddr);
173
174         if (!is_valid_ethaddr(enetaddr))
175                 return -1;
176
177         return eth_env_set_enetaddr("usbethaddr", enetaddr);
178 }
179
180 int board_eth_init(bd_t *bis)
181 {
182         return handle_mac_address();
183 }
184 #endif
185
186 #ifdef CONFIG_USB_EHCI_HCD
187 static struct omap_usbhs_board_data usbhs_bdata = {
188         .port_mode[0] = OMAP_USBHS_PORT_MODE_UNUSED,
189         .port_mode[1] = OMAP_EHCI_PORT_MODE_HSIC,
190         .port_mode[2] = OMAP_EHCI_PORT_MODE_HSIC,
191 };
192
193 static void setup_host_clocks(bool enable)
194 {
195         int usbhost_clk = OPTFCLKEN_HSIC60M_P3_CLK |
196                           OPTFCLKEN_HSIC480M_P3_CLK |
197                           OPTFCLKEN_HSIC60M_P2_CLK |
198                           OPTFCLKEN_HSIC480M_P2_CLK |
199                           OPTFCLKEN_UTMI_P3_CLK |
200                           OPTFCLKEN_UTMI_P2_CLK;
201
202         int usbtll_clk = OPTFCLKEN_USB_CH1_CLK_ENABLE |
203                          OPTFCLKEN_USB_CH2_CLK_ENABLE;
204
205         int usbhub_clk = CKOBUFFER_CLK_ENABLE_MASK;
206
207         if (enable) {
208                 /* Enable port 2 and 3 clocks*/
209                 setbits_le32((*prcm)->cm_l3init_hsusbhost_clkctrl, usbhost_clk);
210                 /* Enable port 2 and 3 usb host ports tll clocks*/
211                 setbits_le32((*prcm)->cm_l3init_hsusbtll_clkctrl, usbtll_clk);
212                 /* Request FREF_XTAL_CLK clock for HSIC USB Hub */
213                 setbits_le32((*ctrl)->control_ckobuffer, usbhub_clk);
214         } else {
215                 clrbits_le32((*ctrl)->control_ckobuffer, usbhub_clk);
216                 clrbits_le32((*prcm)->cm_l3init_hsusbtll_clkctrl, usbtll_clk);
217                 clrbits_le32((*prcm)->cm_l3init_hsusbhost_clkctrl, usbhost_clk);
218         }
219 }
220
221 int ehci_hcd_init(int index, enum usb_init_type init,
222                 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
223 {
224         int ret;
225
226         /* VCC_3V3_ETH */
227         cm_t54_palmas_regulator_set(SMPS9_VOLTAGE, SMPS_VOLT_3V3, SMPS9_CTRL,
228                                     SMPS_MODE_SLP_AUTO | SMPS_MODE_ACT_AUTO);
229
230         setup_host_clocks(true);
231
232         ret = omap_ehci_hcd_init(index, &usbhs_bdata, hccr, hcor);
233         if (ret < 0)
234                 printf("cm_t54: Failed to initialize ehci : %d\n", ret);
235
236         return ret;
237 }
238
239 int ehci_hcd_stop(void)
240 {
241         int ret = omap_ehci_hcd_stop();
242
243         setup_host_clocks(false);
244
245         cm_t54_palmas_regulator_set(SMPS9_VOLTAGE, SMPS_VOLT_OFF,
246                                     SMPS9_CTRL, SMPS_MODE_SLP_AUTO);
247
248         return ret;
249 }
250
251 void usb_hub_reset_devices(struct usb_hub_device *hub, int port)
252 {
253         /* The LAN9730 needs to be reset after the port power has been set. */
254         if (port == 3) {
255                 gpio_direction_output(CONFIG_OMAP_EHCI_PHY3_RESET_GPIO, 0);
256                 udelay(10);
257                 gpio_direction_output(CONFIG_OMAP_EHCI_PHY3_RESET_GPIO, 1);
258         }
259 }
260 #endif
261