blob: 4b1e6d0f471f6f7d7fdeb14cb24dd97cc989eee5 [file] [log] [blame]
Andy Shevchenko495f3772017-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>
Simon Glass01510092017-08-03 12:22:08 -06008#include <environment.h>
Andy Shevchenko495f3772017-07-06 14:41:53 +03009#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
20DECLARE_GLOBAL_DATA_PTR;
21
22static 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
29int usb_gadget_handle_interrupts(int controller_index)
30{
31 dwc3_uboot_handle_interrupt(controller_index);
32 WATCHDOG_RESET();
33 return 0;
34}
35
36int 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
43int 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
52static 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]);
Simon Glass382bee52017-08-03 12:22:09 -060067 env_set("usb0addr", usb0addr);
Andy Shevchenko495f3772017-07-06 14:41:53 +030068
69 for (i = 0; i < 16; i++)
70 snprintf(&serial[2 * i], 3, "%02x", ssn[i]);
Simon Glass382bee52017-08-03 12:22:09 -060071 env_set("serial#", serial);
Andy Shevchenko495f3772017-07-06 14:41:53 +030072
73#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Simon Glass01510092017-08-03 12:22:08 -060074 env_save();
Andy Shevchenko495f3772017-07-06 14:41:53 +030075#endif
76}
77
78static 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);
Simon Glass382bee52017-08-03 12:22:09 -060089 env_set("hardware_id", hardware_id);
Andy Shevchenko495f3772017-07-06 14:41:53 +030090
91#if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE)
Simon Glass01510092017-08-03 12:22:08 -060092 env_save();
Andy Shevchenko495f3772017-07-06 14:41:53 +030093#endif
94}
95
96int board_late_init(void)
97{
Simon Glass00caae62017-08-03 12:22:12 -060098 if (!env_get("serial#"))
Andy Shevchenko495f3772017-07-06 14:41:53 +030099 assign_serial();
100
Simon Glass00caae62017-08-03 12:22:12 -0600101 if (!env_get("hardware_id"))
Andy Shevchenko495f3772017-07-06 14:41:53 +0300102 assign_hardware_id();
103
104 return 0;
105}