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#include <linux/printk.h>
14
15static int spl_sdp_load_image(struct spl_image_info *spl_image,
16			      struct spl_boot_device *bootdev)
17{
18	const int controller_index = CONFIG_SPL_SDP_USB_DEV;
19	struct udevice *udc;
20	int ret;
21
22	ret = udc_device_get_by_index(controller_index, &udc);
23	if (ret)
24		return ret;
25
26	board_usb_init(controller_index, USB_INIT_DEVICE);
27
28	g_dnl_clear_detach();
29	ret = g_dnl_register("usb_dnl_sdp");
30	if (ret) {
31		pr_err("SDP dnl register failed: %d\n", ret);
32		goto err_detach;
33	}
34
35	ret = sdp_init(udc);
36	if (ret) {
37		pr_err("SDP init failed: %d\n", ret);
38		goto err_unregister;
39	}
40
41	/*
42	 * This command either loads a legacy image, jumps and never returns,
43	 * or it loads a FIT image and returns it to be handled by the SPL
44	 * code.
45	 */
46	ret = spl_sdp_handle(udc, spl_image, bootdev);
47	debug("SDP ended\n");
48
49err_unregister:
50	g_dnl_unregister();
51err_detach:
52	udc_device_put(udc);
53	return ret;
54}
55SPL_LOAD_IMAGE_METHOD("USB SDP", 0, BOOT_DEVICE_BOARD, spl_sdp_load_image);
56