xref: /openbmc/u-boot/board/intel/edison/edison.c (revision 78a88f79)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2017 Intel Corporation
4  */
5 #include <common.h>
6 #include <dwc3-uboot.h>
7 #include <environment.h>
8 #include <mmc.h>
9 #include <u-boot/md5.h>
10 #include <usb.h>
11 #include <watchdog.h>
12 
13 #include <linux/usb/gadget.h>
14 
15 #include <asm/cache.h>
16 #include <asm/scu.h>
17 #include <asm/u-boot-x86.h>
18 
19 static struct dwc3_device dwc3_device_data = {
20 	.maximum_speed = USB_SPEED_HIGH,
21 	.base = CONFIG_SYS_USB_OTG_BASE,
22 	.dr_mode = USB_DR_MODE_PERIPHERAL,
23 	.index = 0,
24 };
25 
26 int usb_gadget_handle_interrupts(int controller_index)
27 {
28 	dwc3_uboot_handle_interrupt(controller_index);
29 	WATCHDOG_RESET();
30 	return 0;
31 }
32 
33 int board_usb_init(int index, enum usb_init_type init)
34 {
35 	if (index == 0 && init == USB_INIT_DEVICE)
36 		return dwc3_uboot_init(&dwc3_device_data);
37 	return -EINVAL;
38 }
39 
40 int board_usb_cleanup(int index, enum usb_init_type init)
41 {
42 	if (index == 0 && init == USB_INIT_DEVICE) {
43 		dwc3_uboot_exit(index);
44 		return 0;
45 	}
46 	return -EINVAL;
47 }
48 
49 static void assign_serial(void)
50 {
51 	struct mmc *mmc = find_mmc_device(0);
52 	unsigned char ssn[16];
53 	char usb0addr[18];
54 	char serial[33];
55 	int i;
56 
57 	if (!mmc)
58 		return;
59 
60 	md5((unsigned char *)mmc->cid, sizeof(mmc->cid), ssn);
61 
62 	snprintf(usb0addr, sizeof(usb0addr), "02:00:86:%02x:%02x:%02x",
63 		 ssn[13], ssn[14], ssn[15]);
64 	env_set("usb0addr", usb0addr);
65 
66 	for (i = 0; i < 16; i++)
67 		snprintf(&serial[2 * i], 3, "%02x", ssn[i]);
68 	env_set("serial#", serial);
69 
70 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
71 	env_save();
72 #endif
73 }
74 
75 static void assign_hardware_id(void)
76 {
77 	struct ipc_ifwi_version v;
78 	char hardware_id[4];
79 	int ret;
80 
81 	ret = scu_ipc_command(IPCMSG_GET_FW_REVISION, 1, NULL, 0, (u32 *)&v, 4);
82 	if (ret < 0)
83 		printf("Can't retrieve hardware revision\n");
84 
85 	snprintf(hardware_id, sizeof(hardware_id), "%02X", v.hardware_id);
86 	env_set("hardware_id", hardware_id);
87 
88 #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
89 	env_save();
90 #endif
91 }
92 
93 int board_late_init(void)
94 {
95 	if (!env_get("serial#"))
96 		assign_serial();
97 
98 	if (!env_get("hardware_id"))
99 		assign_hardware_id();
100 
101 	return 0;
102 }
103