summaryrefslogtreecommitdiffstats
path: root/drivers/misc/jtag.c
blob: 9accefa34276b2bf836b05da0ec96153711a90a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
 * drivers/misc/jtag.c - More infos in include/jtag.h
 *
 * Written Aug 2009 by Davide Rizzo <elpa.rizzo@gmail.com>
 *
 * Ported to barebox Jul 2012 by
 *       Wjatscheslaw Stoljarski <wjatscheslaw.stoljarski@kiwigrid.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <common.h>
#include <errno.h>
#include <jtag.h>
#include <gpio.h>
#include <driver.h>
#include <malloc.h>
#include <init.h>
#include <ioctl.h>

#define VERSION_MAJ 1
#define VERSION_MIN 0

/* Max devices in the jtag chain */
#define MAX_DEVICES 16

struct jtag_info {
	struct jtag_platdata *pdata;
	struct cdev cdev;
	unsigned int devices; /* Number of devices found in the jtag chain */
	/* Instruction register length of every device in the chain */
	unsigned int ir_len[MAX_DEVICES];	/* [devices] */
};

static const unsigned long bypass = 0xFFFFFFFF;

static void pulse_tms0(const struct jtag_platdata *pdata)
{
	gpio_set_value(pdata->gpio_tms, 0);
	gpio_set_value(pdata->gpio_tclk, 0);
	gpio_set_value(pdata->gpio_tclk, 1);
}

static void pulse_tms1(const struct jtag_platdata *pdata)
{
	gpio_set_value(pdata->gpio_tms, 1);
	gpio_set_value(pdata->gpio_tclk, 0);
	gpio_set_value(pdata->gpio_tclk, 1);
}

static void jtag_reset(const struct jtag_platdata *pdata)
{
	gpio_set_value(pdata->gpio_tms, 1);
	gpio_set_value(pdata->gpio_tclk, 0);
	gpio_set_value(pdata->gpio_tclk, 1);
	gpio_set_value(pdata->gpio_tclk, 0);
	gpio_set_value(pdata->gpio_tclk, 1);
	gpio_set_value(pdata->gpio_tclk, 0);
	gpio_set_value(pdata->gpio_tclk, 1);
	gpio_set_value(pdata->gpio_tclk, 0);
	gpio_set_value(pdata->gpio_tclk, 1);
	gpio_set_value(pdata->gpio_tclk, 0);
	gpio_set_value(pdata->gpio_tclk, 1);
}

static void jtag_output(const struct jtag_platdata *pdata,
	const unsigned long *data, unsigned int bitlen, int notlast)
{
	unsigned int a;
	unsigned long mask;
	gpio_set_value(pdata->gpio_tms, 0);
	while (bitlen > 0) {
		for (a = *data++, mask = 0x00000001; mask != 0 && bitlen > 0;
		      mask <<= 1, bitlen--) {
			gpio_set_value(pdata->gpio_tdo, (a & mask) ? 1 : 0);
			if ((bitlen == 1) && !notlast)
				gpio_set_value(pdata->gpio_tms, 1);
			gpio_set_value(pdata->gpio_tclk, 0);
			gpio_set_value(pdata->gpio_tclk, 1);
		}
	}
}

static int jtag_ioctl(struct cdev *inode, int cmd, void *arg)
{
	int ret = 0;
	struct jtag_info *info = (struct jtag_info *)inode->priv;
	int devices = info->devices;
	struct jtag_cmd *jcmd = (struct jtag_cmd *)arg;
	struct jtag_platdata *pdata = info->pdata;

	if (_IOC_TYPE(cmd) != JTAG_IOC_MAGIC) return -ENOTTY;
	if (_IOC_NR(cmd) > JTAG_IOC_MAXNR) return -ENOTTY;

	switch (cmd) {

	case JTAG_GET_DEVICES:
		/* Returns how many devices found in the chain */
		ret = info->devices;
		break;

	case JTAG_GET_ID:
		/* Returns ID register of selected device */
		if ((((struct jtag_rd_id *)arg)->device < 0) ||
			(((struct jtag_rd_id *)arg)->device >= devices)) {
			ret = -EINVAL;
			break;
		}
		jtag_reset(pdata);
		pulse_tms0(pdata);
		pulse_tms1(pdata);
		pulse_tms0(pdata);
		pulse_tms0(pdata);
		while (devices-- > 0) {
			unsigned long id = 0;
			pulse_tms0(pdata);
			if (gpio_get_value(pdata->gpio_tdi)) {
				unsigned long mask;
				for (id = 1, mask = 0x00000002; (mask != 0);
				      mask <<= 1) {
					pulse_tms0(pdata);
					if (gpio_get_value(pdata->gpio_tdi))
						id |= mask;
				}
			}
			if (devices == ((struct jtag_rd_id *)arg)->device) {
				((struct jtag_rd_id *)arg)->id = id;
				ret = 0;
				break;
			}
		}
		jtag_reset(pdata);
		break;

	case JTAG_SET_IR_LENGTH:
		/* Sets up IR length of one device */
		if ((jcmd->device >= 0) && (jcmd->device < devices))
			info->ir_len[jcmd->device] = jcmd->bitlen;
		else
			ret = -EINVAL;
		break;

	case JTAG_RESET:
		/* Resets all JTAG states */
		jtag_reset(pdata);
		break;

	case JTAG_IR_WR:
		/*
		 * Writes Instruction Register
		 * If device == -1 writes same Instruction Register in
		 * all devices.
		 * If device >= 0 writes Instruction Register in selected
		 * device and loads BYPASS instruction in all others.
		 */
		if ((jcmd->device < -1) || (jcmd->device >= devices)) {
			ret = -EINVAL;
			break;
		}
		pulse_tms0(pdata);
		pulse_tms1(pdata);
		pulse_tms1(pdata);
		pulse_tms0(pdata);
		pulse_tms0(pdata);
		while (devices-- > 0) {
			if ((jcmd->device == -1) || (jcmd->device == devices))
				/* Loads desired instruction */
				jtag_output(pdata, jcmd->data,
					info->ir_len[devices], devices);
			else
				/* Loads BYPASS instruction */
				jtag_output(pdata, &bypass,
					info->ir_len[devices], devices);
		}
		pulse_tms1(pdata);
		pulse_tms0(pdata);
		break;

	case JTAG_DR_WR:
		/*
		 * Writes Data Register of all devices
		 * If device == -1 writes same Data Register in all devices.
		 * If device >= 0 writes Data Register in selected device
		 * and loads BYPASS instruction in all others.
		 */
		if ((jcmd->device < -1) || (jcmd->device >= devices)) {
			ret = -EINVAL;
			break;
		}
		pulse_tms0(pdata);
		pulse_tms1(pdata);
		pulse_tms0(pdata);
		pulse_tms0(pdata);
		while (devices-- > 0) {
			if ((jcmd->device == -1) || (devices == jcmd->device))
				/* Loads desired data */
				jtag_output(pdata, jcmd->data, jcmd->bitlen,
					devices);
			else
				/* Loads 1 dummy bit in BYPASS data register */
				jtag_output(pdata, &bypass, 1, devices);
		}
		pulse_tms1(pdata);
		pulse_tms0(pdata);
		break;

	case JTAG_DR_RD:
		/* Reads data register of selected device */
		if ((jcmd->device < 0) || (jcmd->device >= devices))
			ret = -EINVAL;
		else {
			unsigned long mask;
			int bitlen = jcmd->bitlen;
			unsigned long *data = jcmd->data;
			pulse_tms0(pdata);
			pulse_tms1(pdata);
			pulse_tms0(pdata);
			pulse_tms0(pdata);
			devices -= (jcmd->device + 1);
			while (devices-- > 0)
				pulse_tms0(pdata);
			while (bitlen > 0) {
				for (*data = 0, mask = 0x00000001;
				      (mask != 0) && (bitlen > 0);
				      mask <<= 1, bitlen--) {
					if (bitlen == 1)
						pulse_tms1(pdata);
					else
						pulse_tms0(pdata);
					if (gpio_get_value(pdata->gpio_tdi))
						*data |= mask;
				}
				data++;
			}
			pulse_tms1(pdata);
			pulse_tms0(pdata);
		}
		break;

	case JTAG_CLK:
		/* Generates arg clock pulses */
		gpio_set_value(pdata->gpio_tms, 0);
		while ((*(unsigned int *) arg)--) {
			gpio_set_value(pdata->gpio_tclk, 0);
			gpio_set_value(pdata->gpio_tclk, 1);
		}
		break;

	default:
		ret = -EFAULT;
	}

	return ret;
}

static struct cdev_operations jtag_operations = {
	.ioctl = jtag_ioctl,
};

static void jtag_info(struct device_d *pdev)
{
	int dn, ret;
	struct jtag_rd_id jid;
	struct jtag_info *info = pdev->priv;

	printf(" JTAG:\n");
	printf("  Devices found: %u\n", info->devices);
	for (dn = 0; dn < info->devices; dn++) {
		jid.device = dn;
		ret = jtag_ioctl(&info->cdev, JTAG_GET_ID, &jid);
		printf("  Device number: %d\n", dn);
		if (ret == -1)
			printf("   JTAG_GET_ID failed: %s\n", strerror(errno));
		else
			printf("   ID: 0x%lX\n", jid.id);
	}
}

static int jtag_probe(struct device_d *pdev)
{
	int i, ret;
	struct jtag_info *info;
	struct jtag_platdata *pdata = pdev->platform_data;

	/* Setup gpio pins */
	gpio_direction_output(pdata->gpio_tms, 0);
	gpio_direction_output(pdata->gpio_tclk, 1);
	gpio_direction_output(pdata->gpio_tdo, 0);
	gpio_direction_input(pdata->gpio_tdi);
	if (pdata->use_gpio_trst) {
		/*
		 * Keep fixed at 1 because some devices in the chain could
		 * not use it, to reset chain use jtag_reset()
		 */
		gpio_direction_output(pdata->gpio_trst, 1);
	}

	/* Find how many devices in chain */
	jtag_reset(pdata);
	pulse_tms0(pdata);
	pulse_tms1(pdata);
	pulse_tms1(pdata);
	pulse_tms0(pdata);
	pulse_tms0(pdata);
	gpio_set_value(pdata->gpio_tdo, 1);
	/* Fills all IR with bypass instruction */
	for (i = 0; i < 32 * MAX_DEVICES; i++)
		pulse_tms0(pdata);
	pulse_tms1(pdata);
	pulse_tms1(pdata);
	pulse_tms1(pdata);
	pulse_tms0(pdata);
	pulse_tms0(pdata);
	gpio_set_value(pdata->gpio_tdo, 0);
	/* Fills all 1-bit bypass register with 0 */
	for (i = 0; i < MAX_DEVICES + 2; i++)
		pulse_tms0(pdata);
	gpio_set_value(pdata->gpio_tdo, 1);
	/* Counts chain's bit length */
	for (i = 0; i < MAX_DEVICES + 1; i++) {
		pulse_tms0(pdata);
		if (gpio_get_value(pdata->gpio_tdi))
			break;
	}
	dev_notice(pdev, "%d devices found in chain\n", i);

	/* Allocate structure with chain specific infos */
	info = xzalloc(sizeof(struct jtag_info) + sizeof(info->ir_len[0]) * i);

	info->devices = i;
	info->pdata = pdata;
	pdev->priv = info;
	pdev->info = jtag_info;

	info->cdev.name = JTAG_NAME;
	info->cdev.dev = pdev;
	info->cdev.ops = &jtag_operations;
	info->cdev.priv = info;
	ret = devfs_create(&info->cdev);

	if (ret)
		goto fail_devfs_create;

	return 0;

fail_devfs_create:
	pdev->priv = NULL;
	free(info);
	return ret;
}

static void jtag_remove(struct device_d *pdev)
{
	struct jtag_info *info = (struct jtag_info *) pdev->priv;

	devfs_remove(&info->cdev);
	pdev->priv = NULL;
	free(info);
	dev_notice(pdev, "Device removed\n");
}

static struct driver_d jtag_driver = {
	.name = JTAG_NAME,
	.probe = jtag_probe,
	.remove = jtag_remove,
};
device_platform_driver(jtag_driver);

MODULE_AUTHOR("Davide Rizzo <elpa.rizzo@gmail.com>");
MODULE_AUTHOR("Wjatscheslaw Stoljarski <wjatscheslaw.stoljarski@kiwigrid.com>");
MODULE_DESCRIPTION("JTAG bitbang driver");
MODULE_LICENSE("GPL");