Andy Shevchenko | 495f377 | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 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> |
Simon Glass | 0151009 | 2017-08-03 12:22:08 -0600 | [diff] [blame] | 8 | #include <environment.h> |
Andy Shevchenko | 495f377 | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 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 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
| 22 | static 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 | |
| 29 | int usb_gadget_handle_interrupts(int controller_index) |
| 30 | { |
| 31 | dwc3_uboot_handle_interrupt(controller_index); |
| 32 | WATCHDOG_RESET(); |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | int 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 | |
| 43 | int 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 | |
| 52 | static 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 Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 67 | env_set("usb0addr", usb0addr); |
Andy Shevchenko | 495f377 | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 68 | |
| 69 | for (i = 0; i < 16; i++) |
| 70 | snprintf(&serial[2 * i], 3, "%02x", ssn[i]); |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 71 | env_set("serial#", serial); |
Andy Shevchenko | 495f377 | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 72 | |
| 73 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
Simon Glass | 0151009 | 2017-08-03 12:22:08 -0600 | [diff] [blame] | 74 | env_save(); |
Andy Shevchenko | 495f377 | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 75 | #endif |
| 76 | } |
| 77 | |
| 78 | static 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 Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 89 | env_set("hardware_id", hardware_id); |
Andy Shevchenko | 495f377 | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 90 | |
| 91 | #if defined(CONFIG_CMD_SAVEENV) && !defined(CONFIG_ENV_IS_NOWHERE) |
Simon Glass | 0151009 | 2017-08-03 12:22:08 -0600 | [diff] [blame] | 92 | env_save(); |
Andy Shevchenko | 495f377 | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 93 | #endif |
| 94 | } |
| 95 | |
| 96 | int board_late_init(void) |
| 97 | { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 98 | if (!env_get("serial#")) |
Andy Shevchenko | 495f377 | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 99 | assign_serial(); |
| 100 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 101 | if (!env_get("hardware_id")) |
Andy Shevchenko | 495f377 | 2017-07-06 14:41:53 +0300 | [diff] [blame] | 102 | assign_hardware_id(); |
| 103 | |
| 104 | return 0; |
| 105 | } |