Remove FSF address from GPL notices
[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, see <http://www.gnu.org/licenses/>. *
16  ***************************************************************************/
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "../versaloon_include.h"
26 #include "../versaloon.h"
27 #include "../versaloon_internal.h"
28 #include "usbtoxxx.h"
29 #include "usbtoxxx_internal.h"
30
31 RESULT usbtoswd_read_callback(void *p, uint8_t *src, uint8_t *processed)
32 {
33         struct versaloon_pending_t *pending = (struct versaloon_pending_t *)p;
34
35         if (pending->extra_data != NULL)
36                 *((uint8_t *)pending->extra_data) = src[0];
37
38         return ERROR_OK;
39 }
40
41 RESULT usbtoswd_write_callback(void *p, uint8_t *src, uint8_t *processed)
42 {
43         struct versaloon_pending_t *pending = (struct versaloon_pending_t *)p;
44
45         if (pending->extra_data != NULL)
46                 *((uint8_t *)pending->extra_data) = src[0];
47
48         /* mark it processed to ignore other input data */
49         *processed = 1;
50         return ERROR_OK;
51 }
52
53 RESULT usbtoswd_init(uint8_t interface_index)
54 {
55         return usbtoxxx_init_command(USB_TO_SWD, interface_index);
56 }
57
58 RESULT usbtoswd_fini(uint8_t interface_index)
59 {
60         return usbtoxxx_fini_command(USB_TO_SWD, interface_index);
61 }
62
63 RESULT usbtoswd_config(uint8_t interface_index, uint8_t trn, uint16_t retry,
64         uint16_t dly)
65 {
66         uint8_t cfg_buf[5];
67
68 #if PARAM_CHECK
69         if (interface_index > 7) {
70                 LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
71                 return ERROR_FAIL;
72         }
73 #endif
74
75         cfg_buf[0] = trn;
76         SET_LE_U16(&cfg_buf[1], retry);
77         SET_LE_U16(&cfg_buf[3], dly);
78
79         return usbtoxxx_conf_command(USB_TO_SWD, interface_index, cfg_buf, 5);
80 }
81
82 RESULT usbtoswd_seqout(uint8_t interface_index, const uint8_t *data,
83         uint16_t bitlen)
84 {
85         uint16_t bytelen = (bitlen + 7) >> 3;
86
87 #if PARAM_CHECK
88         if (interface_index > 7) {
89                 LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
90                 return ERROR_FAIL;
91         }
92 #endif
93
94         SET_LE_U16(&versaloon_cmd_buf[0], bitlen);
95         memcpy(versaloon_cmd_buf + 2, data, bytelen);
96
97         return usbtoxxx_out_command(USB_TO_SWD, interface_index,
98                 versaloon_cmd_buf, bytelen + 2, 0);
99 }
100
101 RESULT usbtoswd_seqin(uint8_t interface_index, uint8_t *data, uint16_t bitlen)
102 {
103         uint16_t bytelen = (bitlen + 7) >> 3;
104         uint8_t buff[2];
105
106 #if PARAM_CHECK
107         if (interface_index > 7) {
108                 LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
109                 return ERROR_FAIL;
110         }
111 #endif
112
113         SET_LE_U16(&buff[0], bitlen);
114
115         return usbtoxxx_in_command(USB_TO_SWD, interface_index, buff, 2, bytelen,
116                 data, 0, bytelen, 0);
117 }
118
119 RESULT usbtoswd_transact(uint8_t interface_index, uint8_t request,
120         uint32_t *data, uint8_t *ack)
121 {
122         uint8_t parity;
123         uint8_t buff[5];
124
125 #if PARAM_CHECK
126         if (interface_index > 7) {
127                 LOG_BUG(ERRMSG_INVALID_INTERFACE_NUM, interface_index);
128                 return ERROR_FAIL;
129         }
130 #endif
131
132         parity = (request >> 1) & 1;
133         parity += (request >> 2) & 1;
134         parity += (request >> 3) & 1;
135         parity += (request >> 4) & 1;
136         parity &= 1;
137         buff[0] = (request | 0x81 | (parity << 5)) & ~0x40;
138         if (data != NULL)
139                 SET_LE_U32(&buff[1], *data);
140         else
141                 memset(buff + 1, 0, 4);
142
143         versaloon_set_extra_data(ack);
144         if (request & 0x04) {
145                 /* read */
146                 versaloon_set_callback(usbtoswd_read_callback);
147         } else {
148                 /* write */
149                 versaloon_set_callback(usbtoswd_write_callback);
150         }
151
152         /* Input buffer must be passed even for write operations. Otherwise
153          * the callback function is not called and ack value is not set. */
154         return usbtoxxx_inout_command(USB_TO_SWD, interface_index, buff, 5, 5,
155                 (uint8_t *)data, 1, 4, 0);
156 }