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