blob: 5faf3c57f2c71ca023cf3a20f18ca582b80ead60 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Andy Shevchenko495f3772017-07-06 14:41:53 +03002/*
3 * Copyright (c) 2017 Intel Corporation
Andy Shevchenko495f3772017-07-06 14:41:53 +03004 */
5#include <common.h>
6#include <dwc3-uboot.h>
Simon Glass01510092017-08-03 12:22:08 -06007#include <environment.h>
Andy Shevchenko495f3772017-07-06 14:41:53 +03008#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
Andy Shevchenko495f3772017-07-06 14:41:53 +030019static 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
26int usb_gadget_handle_interrupts(int controller_index)
27{
28 dwc3_uboot_handle_interrupt(controller_index);
29 WATCHDOG_RESET();
30 return 0;
31}
32
33int 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
40int 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
49static 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]);
Simon Glass382bee52017-08-03 12:22:09 -060064 env_set("usb0addr", usb0addr);
Andy Shevchenko495f3772017-07-06 14:41:53 +030065
66 for (i = 0; i < 16; i++)
67 snprintf(&serial[2 * i], 3, "%02x", ssn[i]);
Simon Glass382bee52017-08-03 12:22:09 -060068 env_set("serial#", serial);
Andy Shevchenko495f3772017-07-06 14:41:53 +030069
70#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Simon Glass01510092017-08-03 12:22:08 -060071 env_save();
Andy Shevchenko495f3772017-07-06 14:41:53 +030072#endif
73}
74
75static 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);
Simon Glass382bee52017-08-03 12:22:09 -060086 env_set("hardware_id", hardware_id);
Andy Shevchenko495f3772017-07-06 14:41:53 +030087
88#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Simon Glass01510092017-08-03 12:22:08 -060089 env_save();
Andy Shevchenko495f3772017-07-06 14:41:53 +030090#endif
91}
92
93int board_late_init(void)
94{
Simon Glass00caae62017-08-03 12:22:12 -060095 if (!env_get("serial#"))
Andy Shevchenko495f3772017-07-06 14:41:53 +030096 assign_serial();
97
Simon Glass00caae62017-08-03 12:22:12 -060098 if (!env_get("hardware_id"))
Andy Shevchenko495f3772017-07-06 14:41:53 +030099 assign_hardware_id();
100
101 return 0;
102}