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