3 #include <system_def.h>
9 #include <usb/usb_loader.h>
10 #include <usb/usbdebug.h>
14 #include "appl_defs.h"
15 #include "usb/usb_defs.h"
16 #include "appl_fpga.h"
21 #include <lpciap_kvpb.h>
22 #include <hal_machperiph.h>
23 #endif /*CONFIG_KEYVAL*/
26 #if __BYTE_ORDER == __BIG_ENDIAN
28 #define SWAP(x) ((((x) & 0xFF) << 8) | (((x) >> 8) & 0xFF))
29 #else /*__LITTLE_ENDIAN*/
33 #define USB_VENDOR_TARGET_TUMBL 0x03
35 #define USB_CMD_FPGA_CONFIGURE 0xF000
36 #define USB_CMD_FPGA_MEASURE_READ 0xF001
37 #define USB_CMD_FPGA_MEASURE_WRITE 0xF002
39 #define USB_CMD_FPGA_IRC_SET_RESET 0xF010
40 #define USB_CMD_FPGA_IRC_DUMP 0xF011
42 #define USB_CMD_FPGA_LXMASTER_WRITE 0xF020
43 #define USB_CMD_FPGA_LXMASTER_DUMP 0xF021
45 #define USB_CMD_FPGA_TUMBL_SET_RESET 0xF100
46 #define USB_CMD_FPGA_TUMBL_SET_HALT 0xF101
47 #define USB_CMD_FPGA_TUMBL_SET_TRACE 0xF102
48 #define USB_CMD_FPGA_TUMBL_KICK_TRACE 0xF103
49 #define USB_CMD_FPGA_TUMBL_GET_PC 0xF104
51 #define USB_CMD_FPGA_TUMBL_DUMP_IMEM 0xF200
52 #define USB_CMD_FPGA_TUMBL_DUMP_DMEM 0xF201
54 #define USB_CMD_FPGA_RESET 0xFFFF
56 usb_device_t usb_device;
57 usb_ep_t eps[NUM_ENDPOINTS];
59 #define MASK_EP1RX 0x01
60 #define MASK_EP1TX 0x02
62 unsigned char ep1_rx_buff[USB_MAX_PACKET];
63 unsigned char ep1_tx_buff[USB_MAX_PACKET];
64 unsigned char ep0_buffer[USB_MAX_PACKET0];
66 int ep1_rx_index = 0, ep1_rx_ready = 1;
67 int ep1_tx_index = 0, ep1_tx_chars = 0;
69 #ifdef CONFIG_OC_CMDPROC
71 cmd_io_t cmd_io_usbcon_dev;
73 #define ED_LINE_CHARS 512
75 char ed_line_chars_usbcon_in[ED_LINE_CHARS + 1];
76 char ed_line_chars_usbcon_out[ED_LINE_CHARS + 1];
78 ed_line_buf_t ed_line_buf_usbcon_in =
84 sizeof(ed_line_chars_usbcon_in),
88 ed_line_chars_usbcon_in
91 ed_line_buf_t ed_line_buf_usbcon_out =
97 sizeof(ed_line_chars_usbcon_out),
101 ed_line_chars_usbcon_out
104 cmd_io_t cmd_io_usbcon =
119 &ed_line_buf_usbcon_in,
121 &ed_line_buf_usbcon_out,
130 static int usb_flash_pkt_wr(struct usb_ep_t *ep, int len, int code)
132 unsigned char *ptr = ep->ptr - len;
135 lpcisp_kvpb_copy(NULL, (void *)ep->user_data, ptr, len);
136 #endif /*CONFIG_KEYVAL*/
138 ep->user_data += len;
139 ep->ptr = ep0_buffer;
140 return USB_COMPLETE_OK;
143 static int usb_tumbl_pkt_wr(struct usb_ep_t *ep, int len, int code)
145 unsigned char *ptr = ep->ptr - len;
147 appl_fpga_tumbl_write(ep->user_data, ptr, len);
149 ep->user_data += len;
150 ep->ptr = ep0_buffer;
151 return USB_COMPLETE_OK;
154 static int usb_flash_erase(unsigned addr, unsigned len)
157 lpcisp_erase((void *)addr, len);
158 #endif /*CONFIG_KEYVAL*/
162 static void usb_goto(unsigned address)
165 lpc_watchdog_init(1, 10);
171 #endif /*CONFIG_KEYVAL*/
175 uint16_t vendor_call_ret = 0xFFFF;
177 uint16_t appl_usb_vendor_call(uint16_t command, uint16_t argument)
183 case USB_CMD_FPGA_CONFIGURE:
184 return appl_fpga_configure();
186 case USB_CMD_FPGA_MEASURE_READ:
187 return appl_fpga_measure_bus_read();
189 case USB_CMD_FPGA_MEASURE_WRITE:
190 return appl_fpga_measure_bus_write();
192 case USB_CMD_FPGA_IRC_SET_RESET:
193 /* When starting, zero out counters */
194 *irc_reset = argument & 0x0001;
197 case USB_CMD_FPGA_IRC_DUMP:
198 printf("IRC1: count = %d, count index = %d, state = 0x%02x\n", (unsigned int) irc1->count, (unsigned int) irc1->count_index, (unsigned int)(*irc1_state));
199 printf("IRC2: count = %d, count index = %d, state = 0x%02x\n", (unsigned int) irc2->count, (unsigned int) irc2->count_index, (unsigned int)(*irc2_state));
200 printf("IRC3: count = %d, count index = %d, state = 0x%02x\n", (unsigned int) irc3->count, (unsigned int) irc3->count_index, (unsigned int)(*irc3_state));
201 printf("IRC4: count = %d, count index = %d, state = 0x%02x\n", (unsigned int) irc4->count, (unsigned int) irc4->count_index, (unsigned int)(*irc4_state));
204 case USB_CMD_FPGA_LXMASTER_WRITE:
205 for (i = 0; i < (argument / 4); i++)
206 lx_master_base[i] = lx_master_conf[i];
209 case USB_CMD_FPGA_LXMASTER_DUMP:
210 printf("LX MASTER MEM:\n");
211 for (i = 0; i < 16; i++)
213 for (j = 0; j < 16; j++)
214 printf("%04X ", (unsigned int) (lx_master_base[i*16 + j] & 0xFFFF));
220 case USB_CMD_FPGA_TUMBL_SET_RESET:
221 return appl_fpga_tumbl_set_reset(argument);
223 case USB_CMD_FPGA_TUMBL_SET_HALT:
224 return appl_fpga_tumbl_set_halt(argument);
226 case USB_CMD_FPGA_TUMBL_SET_TRACE:
227 return appl_fpga_tumbl_set_trace(argument);
229 case USB_CMD_FPGA_TUMBL_KICK_TRACE:
230 return appl_fpga_tumbl_kick_trace();
232 case USB_CMD_FPGA_TUMBL_GET_PC:
233 printf("Tubml PC: 0x%08X\n", (unsigned int) *tumbl_pc);
236 case USB_CMD_FPGA_TUMBL_DUMP_IMEM:
237 printf("TUMBL IMEM:\n");
238 for (i = 0; i < 64; i++)
240 for (j = 0; j < 8; j++)
241 printf("%08X ", (unsigned int) tumbl_imem[i*8 + j]);
247 case USB_CMD_FPGA_TUMBL_DUMP_DMEM:
248 printf("TUMBL DMEM:\n");
249 for (i = 0; i < 128; i++)
251 for (j = 0; j < 8; j++)
252 printf("%08X ", (unsigned int) tumbl_dmem[i*8 + j]);
258 case USB_CMD_FPGA_RESET:
259 hal_gpio_direction_output(XC_INIT_PIN, 0);
261 for (i = 0; i < 128; i++)
264 hal_gpio_direction_output(XC_INIT_PIN, 1);
271 int appl_usb_vendor(usb_device_t *udev)
275 USB_DEVICE_REQUEST *dreq;
278 dreq = &udev->request;
282 if (dreq->bRequest != (USB_VENDOR_GET_SET_MEMORY |
283 USB_DATA_DIR_FROM_HOST | USB_VENDOR_TARGET_FLASH))
284 lpcisp_kvpb_flush(NULL);
286 #endif /*CONFIG_KEYVAL*/
288 switch (dreq->bRequest & USB_VENDOR_MASK)
290 case USB_VENDOR_GET_CAPABILITIES:
291 ep0_buffer[0] = 0xAA; // test
292 usb_send_control_data(udev, ep0_buffer, 1);
295 case USB_VENDOR_RESET_DEVICE:
296 usb_send_control_data(udev, NULL, 0);
298 lpc_watchdog_init(1, 10);
304 #endif /*CONFIG_KEYVAL*/
307 case USB_VENDOR_GOTO:
308 usb_send_control_data(udev, NULL, 0);
309 usb_goto(dreq->wValue);
312 case USB_VENDOR_ERASE_MEMORY:
313 usb_send_control_data(udev, NULL, 0);
314 usb_flash_erase(dreq->wValue, dreq->wIndex);
317 case USB_VENDOR_ERASE_1KB_MEMORY: /* erase memory for 1 KB */
318 usb_send_control_data(udev, NULL, 0);
319 usb_flash_erase((uint32_t)dreq->wValue << 10, dreq->wIndex << 10);
322 case USB_VENDOR_CALL:
323 vendor_call_ret = SWAP(appl_usb_vendor_call(dreq->wIndex, dreq->wValue));
324 usb_send_control_data(udev, (unsigned char *) &vendor_call_ret, sizeof(uint16_t));
327 case USB_VENDOR_GET_SET_MEMORY:
328 addr = (dreq->wValue & 0xffff) | (((unsigned long)dreq->wIndex & 0xffff) << 16);
331 if ((dreq->bmRequestType & USB_DATA_DIR_MASK) == USB_DATA_DIR_FROM_HOST)
333 switch (dreq->bRequest & USB_VENDOR_TARGET_MASK)
335 case USB_VENDOR_TARGET_RAM:
336 udev->ep0.ptr = (void *)addr;
339 case USB_VENDOR_TARGET_FLASH:
340 udev->ep0.next_pkt_fnc = usb_flash_pkt_wr;
341 udev->ep0.user_data = addr;
342 udev->ep0.ptr = ep0_buffer;
345 case USB_VENDOR_TARGET_TUMBL:
346 udev->ep0.next_pkt_fnc = usb_tumbl_pkt_wr;
347 udev->ep0.user_data = addr;
348 udev->ep0.ptr = ep0_buffer;
356 usb_set_control_endfnc(udev, usb_ack_setup);
358 usb_send_control_data(udev, NULL, 0);
364 switch (dreq->bRequest & USB_VENDOR_TARGET_MASK)
366 case USB_VENDOR_TARGET_RAM:
367 usb_send_control_data(udev, (void *)addr, len);
383 int usb_app_fill_serial_number(uint32_t ul_sn)
385 char *p = usb_devdes_serial_number;
386 int len_max = sizeof(usb_devdes_serial_number);
389 while ((len_max -= 2) >= 2)
392 c = ((ul_sn >> (32 - 4)) & 0xf) + '0';
404 int usb_app_init(void)
406 memset(&usb_device, 0, sizeof(usb_device));
408 eps[0].max_packet_size = USB_MAX_PACKET;
409 eps[1].max_packet_size = USB_MAX_PACKET;
412 eps[0].event_mask = 0x08;
413 eps[1].event_mask = 0x04;
414 eps[0].udev = &usb_device;
415 eps[1].udev = &usb_device;
418 usb_device.devdes_table = &usb_devdes_table;
419 usb_device.init = usb_lpc_init;
420 usb_debug_set_level(DEBUG_LEVEL_NONE);
421 usb_device.cntep = NUM_ENDPOINTS;
423 usb_device.vendor_fnc = appl_usb_vendor;
425 usb_init(&usb_device);
426 usb_connect(&usb_device);
433 if (usb_device.ep_events & MASK_EP1RX)
435 usb_device.ep_events &= ~MASK_EP1RX;
436 //TODO: Use some field in the structure, probably flags
440 /* Respond if there is something to send and RX is ready */
441 if (ep1_rx_ready && ep1_rx_index != 0)
443 usb_udev_write_endpoint(&eps[0], ep1_rx_buff, ep1_rx_index);
446 hal_gpio_set_value(LED2_PIN, 0);
453 int usb_app_poll(void)
455 int active = usb_active;
458 usb_check_events(&usb_device);
459 usb_control_response(&usb_device);
461 /* Check TX endpoint */
462 if (usb_device.ep_events & MASK_EP1TX)
464 ep1_tx_chars = usb_udev_read_endpoint(&eps[1], ep1_tx_buff, USB_MAX_PACKET);
466 usb_device.ep_events &= ~MASK_EP1TX;
467 hal_gpio_set_value(LED2_PIN, 0);
471 /* Check RX endpoint */
472 usb_active |= usb_check_ep1();
477 int usb_app_stop(void)
479 usb_disconnect(&usb_device);
483 #ifdef CONFIG_OC_CMDPROC
485 int cmd_io_getc_usbcon(struct cmd_io *cmd_io)
487 if (ep1_tx_index >= ep1_tx_chars)
490 return ep1_tx_buff[ep1_tx_index++];
493 int cmd_io_putc_usbcon(struct cmd_io *cmd_io, int ch)
495 if (ep1_rx_index >= USB_MAX_PACKET)
497 /* Check EP1 status and return -1 if unavailable */
498 usb_check_events(&usb_device);
501 /* Check again if it wasn't emptied */
502 if (ep1_rx_index >= USB_MAX_PACKET)
506 ep1_rx_buff[ep1_rx_index++] = (unsigned char)ch;
510 cmd_io_t cmd_io_usbcon_dev =
512 .putc = cmd_io_putc_usbcon,
513 .getc = cmd_io_getc_usbcon,
514 .write = cmd_io_write_bychar,
515 .read = cmd_io_read_bychar,