recovery: usbd: remove unused defines
[kernel/u-boot.git] / recovery / usbd.c
1 /*
2  * USB Downloader for SAMSUNG Platform
3  *
4  * Copyright (C) 2007-2010 Samsung Electronics
5  * Minkyu Kang <mk7.kang@samsung.com>
6  *
7  */
8
9 #include <common.h>
10 #include "usbd.h"
11 #include "onenand.h"
12
13 static struct usbd_ops usbd_ops;
14 static unsigned long down_ram_addr;
15
16 int update_boot_image(void)
17 {
18         struct onenand_op *onenand_ops = onenand_get_interface();
19         ulong len, offset;
20
21 #if 1
22         /* case: IPL, Recovery, u-boot are one file */
23         offset = 0;
24         len = CONFIG_RECOVERY_SIZE + CONFIG_RECOVERY_ADDR;
25 #else
26         /* case: IPL, Recover are one file and u-boot is another */
27         offset = CONFIG_RECOVERY_ADDR;
28         len = CONFIG_RECOVERY_SIZE;
29 #endif
30         /* Erase */
31         onenand_ops->erase(offset, len, 0);
32         /* Write */
33         onenand_ops->write(offset, len, NULL, (u_char *)down_ram_addr);
34
35         return 0;
36 }
37
38 /* Parsing received data packet and Process data */
39 static int process_data(struct usbd_ops *usbd)
40 {
41         ulong cmd = 0, arg = 0, len = 0, flag = 0;
42         int recvlen = 0;
43         int ret = 0;
44         int img_type;
45
46         /* Parse command */
47         cmd  = *((ulong *) usbd->rx_data + 0);
48         arg  = *((ulong *) usbd->rx_data + 1);
49         len  = *((ulong *) usbd->rx_data + 2);
50         flag = *((ulong *) usbd->rx_data + 3);
51
52         /* Reset tx buffer */
53         *((ulong *) usbd->tx_data) = 0;
54
55         switch (cmd) {
56         case COMMAND_DOWNLOAD_IMAGE:
57                 usbd->recv_setup((char *)down_ram_addr, (int)len);
58
59                 /* response */
60                 usbd->send_data(usbd->tx_data, usbd->tx_len);
61
62                 /* Receive image */
63                 recvlen = usbd->recv_data();
64
65                 /* Retry this commad */
66                 if (recvlen < len)
67                         *((ulong *) usbd->tx_data) = STATUS_RETRY;
68                 else
69                         *((ulong *) usbd->tx_data) = STATUS_DONE;
70
71                 usbd->send_data(usbd->tx_data, usbd->tx_len);
72                 return 1;
73
74         case COMMAND_PARTITION_SYNC:
75                 *((ulong *) usbd->tx_data) = CONFIG_RECOVERY_BOOT_BLOCKS - 1;
76                 usbd->send_data(usbd->tx_data, usbd->tx_len);
77                 return 1;
78
79         case COMMAND_WRITE_PART_1:
80                 img_type = IMG_BOOT;
81                 break;
82
83         /* Download complete -> reset */
84         case COMMAND_RESET_PDA:
85                 /* Stop USB */
86                 usbd->usb_stop();
87
88                 if (usbd->cpu_reset)
89                         usbd->cpu_reset();
90
91                 return 0;
92
93         /* Error */
94         case COMMAND_RESET_USB:
95                 /* Stop USB */
96                 usbd->usb_stop();
97                 return 0;
98
99         default:
100                 return 1;
101         }
102
103         /* Erase and Write to NAND */
104         switch (img_type) {
105         case IMG_BOOT:
106                 update_boot_image();
107                 break;
108
109         default:
110                 /* Retry? */
111                 break;
112         }
113
114         if (ret) {
115                 /* Retry this commad */
116                 *((ulong *) usbd->tx_data) = STATUS_RETRY;
117                 usbd->send_data(usbd->tx_data, usbd->tx_len);
118                 return 1;
119         } else
120                 *((ulong *) usbd->tx_data) = STATUS_DONE;
121
122         /* Write image success -> Report status */
123         usbd->send_data(usbd->tx_data, usbd->tx_len);
124
125         return 1;
126 }
127
128 int do_usbd_down(void)
129 {
130         struct usbd_ops *usbd;
131
132         /* interface setting */
133         usbd = usbd_set_interface(&usbd_ops);
134         down_ram_addr = usbd->ram_addr;
135
136         /* init the usb controller */
137         usbd->usb_init();
138
139         /* receive setting */
140         usbd->recv_setup(usbd->rx_data, usbd->rx_len);
141
142         /* detect the download request from Host PC */
143         if (usbd->recv_data())
144                 usbd->send_data(usbd->tx_data, usbd->tx_len);
145         else
146                 return 0;
147
148         /* receive the data from Host PC */
149         while (1) {
150                 usbd->recv_setup(usbd->rx_data, usbd->rx_len);
151
152                 if (usbd->recv_data()) {
153                         if (process_data(usbd) == 0)
154                                 return 0;
155                 }
156         }
157
158         return 0;
159 }