summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/sysfs-dcc.c
blob: 80e1a3aeee59d0de34aded53aeb0c0631397e454 (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
/*
 * arch/arm/mach-tegra/sysfs-dcc.c
 *
 * Copyright (c) 2010-2013, NVIDIA CORPORATION, All rights reserved.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/spinlock.h>
#include <linux/sysfs.h>
#include <linux/workqueue.h>
#include <linux/kobject.h>
#include <linux/hrtimer.h>
#include <linux/slab.h>

#define DCC_TIMEOUT_US	    100000 	/* Delay time for DCC timeout (in uS) */
#define CP14_DSCR_WDTRFULL  0x20000000	/* Write Data Transfer Register Full */
#define SYSFS_DCC_DEBUG_PRINTS 0     	/* Set non-zero to enable debug prints */

#if SYSFS_DCC_DEBUG_PRINTS
#define DEBUG_DCC(x) printk x
#else
#define DEBUG_DCC(x)
#endif

static int DebuggerConnected = 0;  /* -1=not connected, 0=unknown, 1=connected */
static struct kobject *nvdcc_kobj;
static spinlock_t dcc_lock;
static struct list_head dcc_list;

static ssize_t sysfsdcc_show(struct kobject *kobj,
		struct kobj_attribute *attr, char *buf);

static ssize_t sysfsdcc_store(struct kobject *kobj,
		struct kobj_attribute *attr, const char *buf, size_t count);


static struct kobj_attribute nvdcc_attr =
		__ATTR(dcc0, 0222, sysfsdcc_show, sysfsdcc_store);

static int write_to_dcc(u32 c)
{
	volatile u32 dscr;

	/* Have we already determined that there is no debugger connected? */
	if (DebuggerConnected < 0)
	{
		return -ENXIO;
	}

	/* Read the DSCR. */
	asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (dscr) : : "cc");

	/* If DSCR Bit 29 (wDTRFull) is set there is data in the write
	 * register. If it stays there for than the DCC_TIMEOUT_US
	 * period, ignore this write and disable further DCC accesses. */
	if (dscr & CP14_DSCR_WDTRFULL)
	{
		ktime_t end = ktime_add_ns(ktime_get(), DCC_TIMEOUT_US * 1000);
		ktime_t now;

		for (;;)
		{
			/* Re-read the DSCR. */
			asm volatile ("mrc p14, 0, %0, c0, c1, 0" : "=r" (dscr) : : "cc");

			/* Previous data still there? */
			if (dscr & CP14_DSCR_WDTRFULL)
			{
				now = ktime_get();

				if (ktime_to_ns(now) >= ktime_to_ns(end))
				{
					goto fail;
				}
			}
			else
			{
				if (DebuggerConnected == 0) {
					/* Debugger connected */
					spin_lock(&dcc_lock);
					DebuggerConnected = 1;
					spin_unlock(&dcc_lock);
				}
				break;
			}
		}
	}

	// Write the data into the DCC output register
	asm volatile ("mcr p14, 0, %0, c0, c5, 0" : : "r" (c) : "cc");
	return 0;

fail:
	/* No debugged connected -- disable DCC */
	spin_lock(&dcc_lock);
	DebuggerConnected = -1;
	spin_unlock(&dcc_lock);
	return -ENXIO;
}


struct tegra_dcc_req {
	struct list_head node;

	const char *pBuf;
	unsigned int size;
};

struct dcc_action {
	struct tegra_dcc_req req;
	struct work_struct work;
	struct list_head node;
};


static void dcc_writer(struct work_struct *work)
{
	struct dcc_action *action = container_of(work, struct dcc_action, work);
	const char *p;

	DEBUG_DCC(("+dcc_writer\n"));

	spin_lock(&dcc_lock);
	list_del(&action->req.node);
	spin_unlock(&dcc_lock);

	p = action->req.pBuf;
	if (p)
		while ((p < &(action->req.pBuf[action->req.size])) && (*p))
			if (write_to_dcc(*p++))
				break;

	kfree(action->req.pBuf);
	kfree(action);

	DEBUG_DCC(("-dcc_writer\n"));
}

static ssize_t sysfsdcc_show(struct kobject *kobj,
		struct kobj_attribute *attr, char *buf)
{
	DEBUG_DCC(("!sysfsdcc_show\n"));
	return -EACCES;
}

static ssize_t sysfsdcc_store(struct kobject *kobj,
	struct kobj_attribute *attr, const char *buf, size_t count)
{
	struct dcc_action *action;
	char *pBuf;
	ssize_t ret = count;

	DEBUG_DCC(("+sysfsdcc_store: %p, %d\n", buf, count));

	if (!buf || !count) {
		ret = -EINVAL;
		goto fail;
	}

	pBuf = kmalloc(count+1, GFP_KERNEL);
	if (!pBuf) {
		pr_debug("%s: insufficient memory\n", __func__);
		ret = -ENOMEM;
		goto fail;
	}

	action = kzalloc(sizeof(*action), GFP_KERNEL);
	if (!action) {
		kfree(pBuf);
		pr_debug("%s: insufficient memory\n", __func__);
		ret = -ENOMEM;
		goto fail;
	}

	strncpy(pBuf, buf, count);
	pBuf[count] = '\0';
	action->req.pBuf = pBuf;
	action->req.size = count;

	INIT_WORK(&action->work, dcc_writer);

	spin_lock(&dcc_lock);
	list_add_tail(&action->req.node, &dcc_list);
	spin_unlock(&dcc_lock);

	/* DCC writes can only be performed from CPU0 */
	schedule_work_on(0, &action->work);

fail:
	DEBUG_DCC(("-sysfsdcc_store: %d\n", count));
	return ret;
}

static int __init sysfsdcc_init(void)
{
	spin_lock_init(&dcc_lock);
	INIT_LIST_HEAD(&dcc_list);

	DEBUG_DCC(("+sysfsdcc_init\n"));
	nvdcc_kobj = kobject_create_and_add("dcc", kernel_kobj);

	if (sysfs_create_file(nvdcc_kobj, &nvdcc_attr.attr))
	{
		DEBUG_DCC(("DCC: sysfs_create_file failed!\n"));
		return -ENXIO;
	}

	DEBUG_DCC(("-sysfsdcc_init\n"));
	return 0;
}

static void __exit sysfsdcc_exit(void)
{
	DEBUG_DCC(("+sysfsdcc_exit\n"));
	sysfs_remove_file(nvdcc_kobj, &nvdcc_attr.attr);
	kobject_del(nvdcc_kobj);
	DEBUG_DCC(("-sysfsdcc_exit\n"));
}

module_init(sysfsdcc_init);
module_exit(sysfsdcc_exit);
MODULE_LICENSE("GPL");