vsllink: Add SWD support
[fw/openocd] / src / jtag / drivers / versaloon / usbtoxxx / usbtoswd.c
1 /***************************************************************************
2  *   Copyright (C) 2009 - 2010 by Simon Qian <SimonQian@SimonQian.com>     *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
18  ***************************************************************************/
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "../versaloon_include.h"
28 #include "../versaloon.h"
29 #include "../versaloon_internal.h"
30 #include "usbtoxxx.h"
31 #include "usbtoxxx_internal.h"
32
33 RESULT usbtoswd_read_callback(void *p, uint8_t *src, uint8_t *processed)
34 {
35         struct versaloon_pending_t *pending = (struct versaloon_pending_t *)p;
36
37         if (pending->extra_data != NULL)
38                 *((uint8_t *)pending->extra_data) = src[0];
39
40         return ERROR_OK;
41 }
42
43 RESULT usbtoswd_write_callback(void *p, uint8_t *src, uint8_t *processed)
44 {
45         struct versaloon_pending_t *pending = (struct versaloon_pending_t *)p;
46
47         if (pending->extra_data != NULL)
48                 *((uint8_t *)pending->extra_data) = src[0];
49
50         /* mark it processed to ignore other input data */
51         *processed = 1;
52         return ERROR_OK;
53 }
54
55 RESULT usbtoswd_init(uint8_t interface_index)
56 {
57         return usbtoxxx_init_command(USB_TO_SWD, interface_index);
58 }
59
60 RESULT usbtoswd_fini(uint8_t interface_index)
61 {
62         return usbtoxxx_fini_command(USB_TO_SWD, interface_index);
63 }
64
65 RESULT usbtoswd_config(uint8_t interface_index, uint8_t trn, uint16_t retry,
66         uint16_t dly)
67 {
68         uint8_t cfg_buf[5];
69
70 #if PARAM_CHECK
71         if (interface_index > 7) {
72                 LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
73                 return ERROR_FAIL;
74         }
75 #endif
76
77         cfg_buf[0] = trn;
78         SET_LE_U16(&cfg_buf[1], retry);
79         SET_LE_U16(&cfg_buf[3], dly);
80
81         return usbtoxxx_conf_command(USB_TO_SWD, interface_index, cfg_buf, 5);
82 }
83
84 RESULT usbtoswd_seqout(uint8_t interface_index, const uint8_t *data,
85         uint16_t bitlen)
86 {
87         uint16_t bytelen = (bitlen + 7) >> 3;
88
89 #if PARAM_CHECK
90         if (interface_index > 7) {
91                 LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
92                 return ERROR_FAIL;
93         }
94 #endif
95
96         SET_LE_U16(&versaloon_cmd_buf[0], bitlen);
97         memcpy(versaloon_cmd_buf + 2, data, bytelen);
98
99         return usbtoxxx_out_command(USB_TO_SWD, interface_index,
100                 versaloon_cmd_buf, bytelen + 2, 0);
101 }
102
103 RESULT usbtoswd_seqin(uint8_t interface_index, uint8_t *data, uint16_t bitlen)
104 {
105         uint16_t bytelen = (bitlen + 7) >> 3;
106         uint8_t buff[2];
107
108 #if PARAM_CHECK
109         if (interface_index > 7) {
110                 LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
111                 return ERROR_FAIL;
112         }
113 #endif
114
115         SET_LE_U16(&buff[0], bitlen);
116
117         return usbtoxxx_in_command(USB_TO_SWD, interface_index, buff, 2, bytelen,
118                 data, 0, bytelen, 0);
119 }
120
121 RESULT usbtoswd_transact(uint8_t interface_index, uint8_t request,
122         uint32_t *data, uint8_t *ack)
123 {
124         uint8_t parity;
125         uint8_t buff[5];
126
127 #if PARAM_CHECK
128         if (interface_index > 7) {
129                 LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
130                 return ERROR_FAIL;
131         }
132 #endif
133
134         parity = (request >> 1) & 1;
135         parity += (request >> 2) & 1;
136         parity += (request >> 3) & 1;
137         parity += (request >> 4) & 1;
138         parity &= 1;
139         buff[0] = (request | 0x81 | (parity << 5)) & ~0x40;
140         if (data != NULL)
141                 SET_LE_U32(&buff[1], *data);
142         else
143                 memset(buff + 1, 0, 4);
144
145         versaloon_set_extra_data(ack);
146         if (request & 0x04) {
147                 /* read */
148                 versaloon_set_callback(usbtoswd_read_callback);
149         } else {
150                 /* write */
151                 versaloon_set_callback(usbtoswd_write_callback);
152         }
153
154         /* Input buffer must be passed even for write operations. Otherwise
155          * the callback function is not called and ack value is not set. */
156         return usbtoxxx_inout_command(USB_TO_SWD, interface_index, buff, 5, 5,
157                 (uint8_t *)data, 1, 4, 0);
158 }