blob: a1a7d4d7c8940aaa293412f1563ae942c1f93dc5 [file] [log] [blame]
Andy Shevchenkod31315d2017-07-06 14:41:53 +03001/*
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 <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
19DECLARE_GLOBAL_DATA_PTR;
20
21static struct dwc3_device dwc3_device_data = {
22 .maximum_speed = USB_SPEED_HIGH,
23 .base = CONFIG_SYS_USB_OTG_BASE,
24 .dr_mode = USB_DR_MODE_PERIPHERAL,
25 .index = 0,
26};
27
28int usb_gadget_handle_interrupts(int controller_index)
29{
30 dwc3_uboot_handle_interrupt(controller_index);
31 WATCHDOG_RESET();
32 return 0;
33}
34
35int board_usb_init(int index, enum usb_init_type init)
36{
37 if (index == 0 && init == USB_INIT_DEVICE)
38 return dwc3_uboot_init(&dwc3_device_data);
39 return -EINVAL;
40}
41
42int board_usb_cleanup(int index, enum usb_init_type init)
43{
44 if (index == 0 && init == USB_INIT_DEVICE) {
45 dwc3_uboot_exit(index);
46 return 0;
47 }
48 return -EINVAL;
49}
50
51static void assign_serial(void)
52{
53 struct mmc *mmc = find_mmc_device(0);
54 unsigned char ssn[16];
55 char usb0addr[18];
56 char serial[33];
57 int i;
58
59 if (!mmc)
60 return;
61
62 md5((unsigned char *)mmc->cid, sizeof(mmc->cid), ssn);
63
64 snprintf(usb0addr, sizeof(usb0addr), "02:00:86:%02x:%02x:%02x",
65 ssn[13], ssn[14], ssn[15]);
66 setenv("usb0addr", usb0addr);
67
68 for (i = 0; i < 16; i++)
69 snprintf(&serial[2 * i], 3, "%02x", ssn[i]);
70 setenv("serial#", serial);
71
72#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
73 saveenv();
74#endif
75}
76
77static void assign_hardware_id(void)
78{
79 struct ipc_ifwi_version v;
80 char hardware_id[4];
81 int ret;
82
83 ret = scu_ipc_command(IPCMSG_GET_FW_REVISION, 1, NULL, 0, (u32 *)&v, 4);
84 if (ret < 0)
85 printf("Can't retrieve hardware revision\n");
86
87 snprintf(hardware_id, sizeof(hardware_id), "%02X", v.hardware_id);
88 setenv("hardware_id", hardware_id);
89
90#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
91 saveenv();
92#endif
93}
94
95int board_late_init(void)
96{
97 if (!getenv("serial#"))
98 assign_serial();
99
100 if (!getenv("hardware_id"))
101 assign_hardware_id();
102
103 return 0;
104}