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