]>
Commit | Line | Data |
---|---|---|
0b914218 MF |
1 | /* |
2 | * TTY over Blackfin JTAG Communication | |
3 | * | |
4 | * Copyright 2008-2009 Analog Devices Inc. | |
5 | * | |
6 | * Enter bugs at http://blackfin.uclinux.org/ | |
7 | * | |
8 | * Licensed under the GPL-2 or later. | |
9 | */ | |
10 | ||
56578abf MF |
11 | #define DRV_NAME "bfin-jtag-comm" |
12 | #define DEV_NAME "ttyBFJC" | |
13 | #define pr_fmt(fmt) DRV_NAME ": " fmt | |
14 | ||
0b914218 MF |
15 | #include <linux/circ_buf.h> |
16 | #include <linux/console.h> | |
17 | #include <linux/delay.h> | |
18 | #include <linux/err.h> | |
19 | #include <linux/kernel.h> | |
20 | #include <linux/kthread.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/mutex.h> | |
23 | #include <linux/sched.h> | |
5a0e3ad6 | 24 | #include <linux/slab.h> |
0b914218 MF |
25 | #include <linux/tty.h> |
26 | #include <linux/tty_driver.h> | |
27 | #include <linux/tty_flip.h> | |
28 | #include <asm/atomic.h> | |
29 | ||
56578abf MF |
30 | #define pr_init(fmt, args...) ({ static const __initconst char __fmt[] = fmt; printk(__fmt, ## args); }) |
31 | ||
0b914218 MF |
32 | /* See the Debug/Emulation chapter in the HRM */ |
33 | #define EMUDOF 0x00000001 /* EMUDAT_OUT full & valid */ | |
34 | #define EMUDIF 0x00000002 /* EMUDAT_IN full & valid */ | |
35 | #define EMUDOOVF 0x00000004 /* EMUDAT_OUT overflow */ | |
36 | #define EMUDIOVF 0x00000008 /* EMUDAT_IN overflow */ | |
37 | ||
0b914218 MF |
38 | static inline uint32_t bfin_write_emudat(uint32_t emudat) |
39 | { | |
40 | __asm__ __volatile__("emudat = %0;" : : "d"(emudat)); | |
41 | return emudat; | |
42 | } | |
43 | ||
44 | static inline uint32_t bfin_read_emudat(void) | |
45 | { | |
46 | uint32_t emudat; | |
47 | __asm__ __volatile__("%0 = emudat;" : "=d"(emudat)); | |
48 | return emudat; | |
49 | } | |
50 | ||
51 | static inline uint32_t bfin_write_emudat_chars(char a, char b, char c, char d) | |
52 | { | |
53 | return bfin_write_emudat((a << 0) | (b << 8) | (c << 16) | (d << 24)); | |
54 | } | |
55 | ||
56 | #define CIRC_SIZE 2048 /* see comment in tty_io.c:do_tty_write() */ | |
57 | #define CIRC_MASK (CIRC_SIZE - 1) | |
58 | #define circ_empty(circ) ((circ)->head == (circ)->tail) | |
59 | #define circ_free(circ) CIRC_SPACE((circ)->head, (circ)->tail, CIRC_SIZE) | |
60 | #define circ_cnt(circ) CIRC_CNT((circ)->head, (circ)->tail, CIRC_SIZE) | |
61 | #define circ_byte(circ, idx) ((circ)->buf[(idx) & CIRC_MASK]) | |
62 | ||
63 | static struct tty_driver *bfin_jc_driver; | |
64 | static struct task_struct *bfin_jc_kthread; | |
65 | static struct tty_struct * volatile bfin_jc_tty; | |
66 | static unsigned long bfin_jc_count; | |
67 | static DEFINE_MUTEX(bfin_jc_tty_mutex); | |
68 | static volatile struct circ_buf bfin_jc_write_buf; | |
69 | ||
70 | static int | |
71 | bfin_jc_emudat_manager(void *arg) | |
72 | { | |
73 | uint32_t inbound_len = 0, outbound_len = 0; | |
74 | ||
75 | while (!kthread_should_stop()) { | |
76 | /* no one left to give data to, so sleep */ | |
77 | if (bfin_jc_tty == NULL && circ_empty(&bfin_jc_write_buf)) { | |
56578abf | 78 | pr_debug("waiting for readers\n"); |
0b914218 MF |
79 | __set_current_state(TASK_UNINTERRUPTIBLE); |
80 | schedule(); | |
81 | __set_current_state(TASK_RUNNING); | |
82 | } | |
83 | ||
84 | /* no data available, so just chill */ | |
85 | if (!(bfin_read_DBGSTAT() & EMUDIF) && circ_empty(&bfin_jc_write_buf)) { | |
56578abf | 86 | pr_debug("waiting for data (in_len = %i) (circ: %i %i)\n", |
0b914218 MF |
87 | inbound_len, bfin_jc_write_buf.tail, bfin_jc_write_buf.head); |
88 | if (inbound_len) | |
89 | schedule(); | |
90 | else | |
91 | schedule_timeout_interruptible(HZ); | |
92 | continue; | |
93 | } | |
94 | ||
95 | /* if incoming data is ready, eat it */ | |
96 | if (bfin_read_DBGSTAT() & EMUDIF) { | |
97 | struct tty_struct *tty; | |
98 | mutex_lock(&bfin_jc_tty_mutex); | |
99 | tty = (struct tty_struct *)bfin_jc_tty; | |
100 | if (tty != NULL) { | |
101 | uint32_t emudat = bfin_read_emudat(); | |
102 | if (inbound_len == 0) { | |
56578abf | 103 | pr_debug("incoming length: 0x%08x\n", emudat); |
0b914218 MF |
104 | inbound_len = emudat; |
105 | } else { | |
106 | size_t num_chars = (4 <= inbound_len ? 4 : inbound_len); | |
56578abf | 107 | pr_debug(" incoming data: 0x%08x (pushing %zu)\n", emudat, num_chars); |
0b914218 MF |
108 | inbound_len -= num_chars; |
109 | tty_insert_flip_string(tty, (unsigned char *)&emudat, num_chars); | |
110 | tty_flip_buffer_push(tty); | |
111 | } | |
112 | } | |
113 | mutex_unlock(&bfin_jc_tty_mutex); | |
114 | } | |
115 | ||
116 | /* if outgoing data is ready, post it */ | |
117 | if (!(bfin_read_DBGSTAT() & EMUDOF) && !circ_empty(&bfin_jc_write_buf)) { | |
118 | if (outbound_len == 0) { | |
119 | outbound_len = circ_cnt(&bfin_jc_write_buf); | |
120 | bfin_write_emudat(outbound_len); | |
56578abf | 121 | pr_debug("outgoing length: 0x%08x\n", outbound_len); |
0b914218 MF |
122 | } else { |
123 | struct tty_struct *tty; | |
124 | int tail = bfin_jc_write_buf.tail; | |
125 | size_t ate = (4 <= outbound_len ? 4 : outbound_len); | |
126 | uint32_t emudat = | |
127 | bfin_write_emudat_chars( | |
128 | circ_byte(&bfin_jc_write_buf, tail + 0), | |
129 | circ_byte(&bfin_jc_write_buf, tail + 1), | |
130 | circ_byte(&bfin_jc_write_buf, tail + 2), | |
131 | circ_byte(&bfin_jc_write_buf, tail + 3) | |
132 | ); | |
133 | bfin_jc_write_buf.tail += ate; | |
134 | outbound_len -= ate; | |
135 | mutex_lock(&bfin_jc_tty_mutex); | |
136 | tty = (struct tty_struct *)bfin_jc_tty; | |
137 | if (tty) | |
138 | tty_wakeup(tty); | |
139 | mutex_unlock(&bfin_jc_tty_mutex); | |
56578abf | 140 | pr_debug(" outgoing data: 0x%08x (pushing %zu)\n", emudat, ate); |
0b914218 MF |
141 | } |
142 | } | |
143 | } | |
144 | ||
145 | __set_current_state(TASK_RUNNING); | |
146 | return 0; | |
147 | } | |
148 | ||
149 | static int | |
150 | bfin_jc_open(struct tty_struct *tty, struct file *filp) | |
151 | { | |
152 | mutex_lock(&bfin_jc_tty_mutex); | |
56578abf | 153 | pr_debug("open %lu\n", bfin_jc_count); |
0b914218 MF |
154 | ++bfin_jc_count; |
155 | bfin_jc_tty = tty; | |
156 | wake_up_process(bfin_jc_kthread); | |
157 | mutex_unlock(&bfin_jc_tty_mutex); | |
158 | return 0; | |
159 | } | |
160 | ||
161 | static void | |
162 | bfin_jc_close(struct tty_struct *tty, struct file *filp) | |
163 | { | |
164 | mutex_lock(&bfin_jc_tty_mutex); | |
56578abf | 165 | pr_debug("close %lu\n", bfin_jc_count); |
0b914218 MF |
166 | if (--bfin_jc_count == 0) |
167 | bfin_jc_tty = NULL; | |
168 | wake_up_process(bfin_jc_kthread); | |
169 | mutex_unlock(&bfin_jc_tty_mutex); | |
170 | } | |
171 | ||
172 | /* XXX: we dont handle the put_char() case where we must handle count = 1 */ | |
173 | static int | |
174 | bfin_jc_circ_write(const unsigned char *buf, int count) | |
175 | { | |
176 | int i; | |
177 | count = min(count, circ_free(&bfin_jc_write_buf)); | |
56578abf | 178 | pr_debug("going to write chunk of %i bytes\n", count); |
0b914218 MF |
179 | for (i = 0; i < count; ++i) |
180 | circ_byte(&bfin_jc_write_buf, bfin_jc_write_buf.head + i) = buf[i]; | |
181 | bfin_jc_write_buf.head += i; | |
182 | return i; | |
183 | } | |
184 | ||
185 | #ifndef CONFIG_BFIN_JTAG_COMM_CONSOLE | |
186 | # define acquire_console_sem() | |
187 | # define release_console_sem() | |
188 | #endif | |
189 | static int | |
190 | bfin_jc_write(struct tty_struct *tty, const unsigned char *buf, int count) | |
191 | { | |
192 | int i; | |
193 | acquire_console_sem(); | |
194 | i = bfin_jc_circ_write(buf, count); | |
195 | release_console_sem(); | |
196 | wake_up_process(bfin_jc_kthread); | |
197 | return i; | |
198 | } | |
199 | ||
200 | static void | |
201 | bfin_jc_flush_chars(struct tty_struct *tty) | |
202 | { | |
203 | wake_up_process(bfin_jc_kthread); | |
204 | } | |
205 | ||
206 | static int | |
207 | bfin_jc_write_room(struct tty_struct *tty) | |
208 | { | |
209 | return circ_free(&bfin_jc_write_buf); | |
210 | } | |
211 | ||
212 | static int | |
213 | bfin_jc_chars_in_buffer(struct tty_struct *tty) | |
214 | { | |
215 | return circ_cnt(&bfin_jc_write_buf); | |
216 | } | |
217 | ||
218 | static void | |
219 | bfin_jc_wait_until_sent(struct tty_struct *tty, int timeout) | |
220 | { | |
221 | unsigned long expire = jiffies + timeout; | |
222 | while (!circ_empty(&bfin_jc_write_buf)) { | |
223 | if (signal_pending(current)) | |
224 | break; | |
225 | if (time_after(jiffies, expire)) | |
226 | break; | |
227 | } | |
228 | } | |
229 | ||
1cceefd3 | 230 | static const struct tty_operations bfin_jc_ops = { |
0b914218 MF |
231 | .open = bfin_jc_open, |
232 | .close = bfin_jc_close, | |
233 | .write = bfin_jc_write, | |
234 | /*.put_char = bfin_jc_put_char,*/ | |
235 | .flush_chars = bfin_jc_flush_chars, | |
236 | .write_room = bfin_jc_write_room, | |
237 | .chars_in_buffer = bfin_jc_chars_in_buffer, | |
238 | .wait_until_sent = bfin_jc_wait_until_sent, | |
239 | }; | |
240 | ||
241 | static int __init bfin_jc_init(void) | |
242 | { | |
243 | int ret; | |
244 | ||
245 | bfin_jc_kthread = kthread_create(bfin_jc_emudat_manager, NULL, DRV_NAME); | |
246 | if (IS_ERR(bfin_jc_kthread)) | |
247 | return PTR_ERR(bfin_jc_kthread); | |
248 | ||
249 | ret = -ENOMEM; | |
250 | ||
251 | bfin_jc_write_buf.head = bfin_jc_write_buf.tail = 0; | |
252 | bfin_jc_write_buf.buf = kmalloc(CIRC_SIZE, GFP_KERNEL); | |
253 | if (!bfin_jc_write_buf.buf) | |
254 | goto err; | |
255 | ||
256 | bfin_jc_driver = alloc_tty_driver(1); | |
257 | if (!bfin_jc_driver) | |
258 | goto err; | |
259 | ||
260 | bfin_jc_driver->owner = THIS_MODULE; | |
261 | bfin_jc_driver->driver_name = DRV_NAME; | |
262 | bfin_jc_driver->name = DEV_NAME; | |
263 | bfin_jc_driver->type = TTY_DRIVER_TYPE_SERIAL; | |
264 | bfin_jc_driver->subtype = SERIAL_TYPE_NORMAL; | |
265 | bfin_jc_driver->init_termios = tty_std_termios; | |
266 | tty_set_operations(bfin_jc_driver, &bfin_jc_ops); | |
267 | ||
268 | ret = tty_register_driver(bfin_jc_driver); | |
269 | if (ret) | |
270 | goto err; | |
271 | ||
272 | pr_init(KERN_INFO DRV_NAME ": initialized\n"); | |
273 | ||
274 | return 0; | |
275 | ||
276 | err: | |
277 | put_tty_driver(bfin_jc_driver); | |
278 | kfree(bfin_jc_write_buf.buf); | |
279 | kthread_stop(bfin_jc_kthread); | |
280 | return ret; | |
281 | } | |
282 | module_init(bfin_jc_init); | |
283 | ||
284 | static void __exit bfin_jc_exit(void) | |
285 | { | |
286 | kthread_stop(bfin_jc_kthread); | |
287 | kfree(bfin_jc_write_buf.buf); | |
288 | tty_unregister_driver(bfin_jc_driver); | |
289 | put_tty_driver(bfin_jc_driver); | |
290 | } | |
291 | module_exit(bfin_jc_exit); | |
292 | ||
293 | #if defined(CONFIG_BFIN_JTAG_COMM_CONSOLE) || defined(CONFIG_EARLY_PRINTK) | |
294 | static void | |
295 | bfin_jc_straight_buffer_write(const char *buf, unsigned count) | |
296 | { | |
297 | unsigned ate = 0; | |
298 | while (bfin_read_DBGSTAT() & EMUDOF) | |
299 | continue; | |
300 | bfin_write_emudat(count); | |
301 | while (ate < count) { | |
302 | while (bfin_read_DBGSTAT() & EMUDOF) | |
303 | continue; | |
304 | bfin_write_emudat_chars(buf[ate], buf[ate+1], buf[ate+2], buf[ate+3]); | |
305 | ate += 4; | |
306 | } | |
307 | } | |
308 | #endif | |
309 | ||
310 | #ifdef CONFIG_BFIN_JTAG_COMM_CONSOLE | |
311 | static void | |
312 | bfin_jc_console_write(struct console *co, const char *buf, unsigned count) | |
313 | { | |
314 | if (bfin_jc_kthread == NULL) | |
315 | bfin_jc_straight_buffer_write(buf, count); | |
316 | else | |
317 | bfin_jc_circ_write(buf, count); | |
318 | } | |
319 | ||
320 | static struct tty_driver * | |
321 | bfin_jc_console_device(struct console *co, int *index) | |
322 | { | |
323 | *index = co->index; | |
324 | return bfin_jc_driver; | |
325 | } | |
326 | ||
327 | static struct console bfin_jc_console = { | |
328 | .name = DEV_NAME, | |
329 | .write = bfin_jc_console_write, | |
330 | .device = bfin_jc_console_device, | |
331 | .flags = CON_ANYTIME | CON_PRINTBUFFER, | |
332 | .index = -1, | |
333 | }; | |
334 | ||
335 | static int __init bfin_jc_console_init(void) | |
336 | { | |
337 | register_console(&bfin_jc_console); | |
338 | return 0; | |
339 | } | |
340 | console_initcall(bfin_jc_console_init); | |
341 | #endif | |
342 | ||
343 | #ifdef CONFIG_EARLY_PRINTK | |
344 | static void __init | |
345 | bfin_jc_early_write(struct console *co, const char *buf, unsigned int count) | |
346 | { | |
347 | bfin_jc_straight_buffer_write(buf, count); | |
348 | } | |
349 | ||
350 | static struct __initdata console bfin_jc_early_console = { | |
351 | .name = "early_BFJC", | |
352 | .write = bfin_jc_early_write, | |
353 | .flags = CON_ANYTIME | CON_PRINTBUFFER, | |
354 | .index = -1, | |
355 | }; | |
356 | ||
357 | struct console * __init | |
358 | bfin_jc_early_init(unsigned int port, unsigned int cflag) | |
359 | { | |
360 | return &bfin_jc_early_console; | |
361 | } | |
362 | #endif | |
363 | ||
364 | MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>"); | |
365 | MODULE_DESCRIPTION("TTY over Blackfin JTAG Communication"); | |
366 | MODULE_LICENSE("GPL"); |