Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Andy Shevchenko | d31315d | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017 Intel Corporation |
Andy Shevchenko | d31315d | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 4 | */ |
| 5 | #include <common.h> |
| 6 | #include <dwc3-uboot.h> |
Simon Glass | d49b889 | 2017-08-03 12:22:08 -0600 | [diff] [blame] | 7 | #include <environment.h> |
Andy Shevchenko | d31315d | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 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 | |
Andy Shevchenko | d31315d | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 19 | static 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 | |
| 26 | int usb_gadget_handle_interrupts(int controller_index) |
| 27 | { |
| 28 | dwc3_uboot_handle_interrupt(controller_index); |
| 29 | WATCHDOG_RESET(); |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | int 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 | |
| 40 | int 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 | |
| 49 | static 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 Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 64 | env_set("usb0addr", usb0addr); |
Andy Shevchenko | d31315d | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 65 | |
| 66 | for (i = 0; i < 16; i++) |
| 67 | snprintf(&serial[2 * i], 3, "%02x", ssn[i]); |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 68 | env_set("serial#", serial); |
Andy Shevchenko | d31315d | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 69 | |
| 70 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
Simon Glass | d49b889 | 2017-08-03 12:22:08 -0600 | [diff] [blame] | 71 | env_save(); |
Andy Shevchenko | d31315d | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 72 | #endif |
| 73 | } |
| 74 | |
| 75 | static 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 Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 86 | env_set("hardware_id", hardware_id); |
Andy Shevchenko | d31315d | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 87 | |
| 88 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
Simon Glass | d49b889 | 2017-08-03 12:22:08 -0600 | [diff] [blame] | 89 | env_save(); |
Andy Shevchenko | d31315d | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 90 | #endif |
| 91 | } |
| 92 | |
| 93 | int board_late_init(void) |
| 94 | { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 95 | if (!env_get("serial#")) |
Andy Shevchenko | d31315d | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 96 | assign_serial(); |
| 97 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 98 | if (!env_get("hardware_id")) |
Andy Shevchenko | d31315d | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 99 | assign_hardware_id(); |
| 100 | |
| 101 | return 0; |
| 102 | } |