1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2016 Toradex
4  * Author: Stefan Agner <stefan.agner@toradex.com>
5  */
6 
7 #include <common.h>
8 #include <log.h>
9 #include <spl.h>
10 #include <usb.h>
11 #include <g_dnl.h>
12 #include <sdp.h>
13 
spl_sdp_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)14 static int spl_sdp_load_image(struct spl_image_info *spl_image,
15 			      struct spl_boot_device *bootdev)
16 {
17 	int ret;
18 	const int controller_index = CONFIG_SPL_SDP_USB_DEV;
19 
20 	usb_gadget_initialize(controller_index);
21 
22 	board_usb_init(0, USB_INIT_DEVICE);
23 
24 	g_dnl_clear_detach();
25 	ret = g_dnl_register("usb_dnl_sdp");
26 	if (ret) {
27 		pr_err("SDP dnl register failed: %d\n", ret);
28 		return ret;
29 	}
30 
31 	ret = sdp_init(controller_index);
32 	if (ret) {
33 		pr_err("SDP init failed: %d\n", ret);
34 		return -ENODEV;
35 	}
36 
37 	/*
38 	 * This command either loads a legacy image, jumps and never returns,
39 	 * or it loads a FIT image and returns it to be handled by the SPL
40 	 * code.
41 	 */
42 	ret = spl_sdp_handle(controller_index, spl_image);
43 	debug("SDP ended\n");
44 
45 	usb_gadget_release(controller_index);
46 	return ret;
47 }
48 SPL_LOAD_IMAGE_METHOD("USB SDP", 0, BOOT_DEVICE_BOARD, spl_sdp_load_image);
49