2 * TTY over Blackfin JTAG Communication
4 * Copyright 2008-2009 Analog Devices Inc.
6 * Enter bugs at http://blackfin.uclinux.org/
8 * Licensed under the GPL-2 or later.
11 #define DRV_NAME "bfin-jtag-comm"
12 #define DEV_NAME "ttyBFJC"
13 #define pr_fmt(fmt) DRV_NAME ": " fmt
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>
24 #include <linux/slab.h>
25 #include <linux/tty.h>
26 #include <linux/tty_driver.h>
27 #include <linux/tty_flip.h>
28 #include <linux/atomic.h>
30 #define pr_init(fmt, args...) ({ static const __initconst char __fmt[] = fmt; printk(__fmt, ## args); })
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 */
38 static inline uint32_t bfin_write_emudat(uint32_t emudat
)
40 __asm__
__volatile__("emudat = %0;" : : "d"(emudat
));
44 static inline uint32_t bfin_read_emudat(void)
47 __asm__
__volatile__("%0 = emudat;" : "=d"(emudat
));
51 static inline uint32_t bfin_write_emudat_chars(char a
, char b
, char c
, char d
)
53 return bfin_write_emudat((a
<< 0) | (b
<< 8) | (c
<< 16) | (d
<< 24));
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])
63 static struct tty_driver
*bfin_jc_driver
;
64 static struct task_struct
*bfin_jc_kthread
;
65 static struct tty_port port
;
66 static volatile struct circ_buf bfin_jc_write_buf
;
69 bfin_jc_emudat_manager(void *arg
)
71 uint32_t inbound_len
= 0, outbound_len
= 0;
73 while (!kthread_should_stop()) {
74 struct tty_struct
*tty
= tty_port_tty_get(&port
);
75 /* no one left to give data to, so sleep */
76 if (tty
== NULL
&& circ_empty(&bfin_jc_write_buf
)) {
77 pr_debug("waiting for readers\n");
78 __set_current_state(TASK_UNINTERRUPTIBLE
);
80 __set_current_state(TASK_RUNNING
);
84 /* no data available, so just chill */
85 if (!(bfin_read_DBGSTAT() & EMUDIF
) && circ_empty(&bfin_jc_write_buf
)) {
86 pr_debug("waiting for data (in_len = %i) (circ: %i %i)\n",
87 inbound_len
, bfin_jc_write_buf
.tail
, bfin_jc_write_buf
.head
);
92 schedule_timeout_interruptible(HZ
);
96 /* if incoming data is ready, eat it */
97 if (bfin_read_DBGSTAT() & EMUDIF
) {
98 uint32_t emudat
= bfin_read_emudat();
99 if (inbound_len
== 0) {
100 pr_debug("incoming length: 0x%08x\n", emudat
);
101 inbound_len
= emudat
;
103 size_t num_chars
= (4 <= inbound_len
? 4 : inbound_len
);
104 pr_debug(" incoming data: 0x%08x (pushing %zu)\n", emudat
, num_chars
);
105 inbound_len
-= num_chars
;
106 tty_insert_flip_string(&port
, (unsigned char *)&emudat
, num_chars
);
107 tty_flip_buffer_push(&port
);
111 /* if outgoing data is ready, post it */
112 if (!(bfin_read_DBGSTAT() & EMUDOF
) && !circ_empty(&bfin_jc_write_buf
)) {
113 if (outbound_len
== 0) {
114 outbound_len
= circ_cnt(&bfin_jc_write_buf
);
115 bfin_write_emudat(outbound_len
);
116 pr_debug("outgoing length: 0x%08x\n", outbound_len
);
118 int tail
= bfin_jc_write_buf
.tail
;
119 size_t ate
= (4 <= outbound_len
? 4 : outbound_len
);
121 bfin_write_emudat_chars(
122 circ_byte(&bfin_jc_write_buf
, tail
+ 0),
123 circ_byte(&bfin_jc_write_buf
, tail
+ 1),
124 circ_byte(&bfin_jc_write_buf
, tail
+ 2),
125 circ_byte(&bfin_jc_write_buf
, tail
+ 3)
127 bfin_jc_write_buf
.tail
+= ate
;
131 pr_debug(" outgoing data: 0x%08x (pushing %zu)\n", emudat
, ate
);
137 __set_current_state(TASK_RUNNING
);
142 bfin_jc_open(struct tty_struct
*tty
, struct file
*filp
)
146 spin_lock_irqsave(&port
.lock
, flags
);
148 spin_unlock_irqrestore(&port
.lock
, flags
);
149 tty_port_tty_set(&port
, tty
);
150 wake_up_process(bfin_jc_kthread
);
155 bfin_jc_close(struct tty_struct
*tty
, struct file
*filp
)
160 spin_lock_irqsave(&port
.lock
, flags
);
161 last
= --port
.count
== 0;
162 spin_unlock_irqrestore(&port
.lock
, flags
);
164 tty_port_tty_set(&port
, NULL
);
165 wake_up_process(bfin_jc_kthread
);
168 /* XXX: we dont handle the put_char() case where we must handle count = 1 */
170 bfin_jc_circ_write(const unsigned char *buf
, int count
)
173 count
= min(count
, circ_free(&bfin_jc_write_buf
));
174 pr_debug("going to write chunk of %i bytes\n", count
);
175 for (i
= 0; i
< count
; ++i
)
176 circ_byte(&bfin_jc_write_buf
, bfin_jc_write_buf
.head
+ i
) = buf
[i
];
177 bfin_jc_write_buf
.head
+= i
;
181 #ifndef CONFIG_BFIN_JTAG_COMM_CONSOLE
182 # define console_lock()
183 # define console_unlock()
186 bfin_jc_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
)
190 i
= bfin_jc_circ_write(buf
, count
);
192 wake_up_process(bfin_jc_kthread
);
197 bfin_jc_flush_chars(struct tty_struct
*tty
)
199 wake_up_process(bfin_jc_kthread
);
203 bfin_jc_write_room(struct tty_struct
*tty
)
205 return circ_free(&bfin_jc_write_buf
);
209 bfin_jc_chars_in_buffer(struct tty_struct
*tty
)
211 return circ_cnt(&bfin_jc_write_buf
);
215 bfin_jc_wait_until_sent(struct tty_struct
*tty
, int timeout
)
217 unsigned long expire
= jiffies
+ timeout
;
218 while (!circ_empty(&bfin_jc_write_buf
)) {
219 if (signal_pending(current
))
221 if (time_after(jiffies
, expire
))
226 static const struct tty_operations bfin_jc_ops
= {
227 .open
= bfin_jc_open
,
228 .close
= bfin_jc_close
,
229 .write
= bfin_jc_write
,
230 /*.put_char = bfin_jc_put_char,*/
231 .flush_chars
= bfin_jc_flush_chars
,
232 .write_room
= bfin_jc_write_room
,
233 .chars_in_buffer
= bfin_jc_chars_in_buffer
,
234 .wait_until_sent
= bfin_jc_wait_until_sent
,
237 static int __init
bfin_jc_init(void)
241 bfin_jc_kthread
= kthread_create(bfin_jc_emudat_manager
, NULL
, DRV_NAME
);
242 if (IS_ERR(bfin_jc_kthread
))
243 return PTR_ERR(bfin_jc_kthread
);
247 bfin_jc_write_buf
.head
= bfin_jc_write_buf
.tail
= 0;
248 bfin_jc_write_buf
.buf
= kmalloc(CIRC_SIZE
, GFP_KERNEL
);
249 if (!bfin_jc_write_buf
.buf
)
252 bfin_jc_driver
= alloc_tty_driver(1);
256 tty_port_init(&port
);
258 bfin_jc_driver
->driver_name
= DRV_NAME
;
259 bfin_jc_driver
->name
= DEV_NAME
;
260 bfin_jc_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
261 bfin_jc_driver
->subtype
= SERIAL_TYPE_NORMAL
;
262 bfin_jc_driver
->init_termios
= tty_std_termios
;
263 tty_set_operations(bfin_jc_driver
, &bfin_jc_ops
);
264 tty_port_link_device(&port
, bfin_jc_driver
, 0);
266 ret
= tty_register_driver(bfin_jc_driver
);
270 pr_init(KERN_INFO DRV_NAME
": initialized\n");
275 tty_port_destroy(&port
);
276 put_tty_driver(bfin_jc_driver
);
278 kfree(bfin_jc_write_buf
.buf
);
280 kthread_stop(bfin_jc_kthread
);
283 module_init(bfin_jc_init
);
285 static void __exit
bfin_jc_exit(void)
287 kthread_stop(bfin_jc_kthread
);
288 kfree(bfin_jc_write_buf
.buf
);
289 tty_unregister_driver(bfin_jc_driver
);
290 put_tty_driver(bfin_jc_driver
);
291 tty_port_destroy(&port
);
293 module_exit(bfin_jc_exit
);
295 #if defined(CONFIG_BFIN_JTAG_COMM_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
297 bfin_jc_straight_buffer_write(const char *buf
, unsigned count
)
300 while (bfin_read_DBGSTAT() & EMUDOF
)
302 bfin_write_emudat(count
);
303 while (ate
< count
) {
304 while (bfin_read_DBGSTAT() & EMUDOF
)
306 bfin_write_emudat_chars(buf
[ate
], buf
[ate
+1], buf
[ate
+2], buf
[ate
+3]);
312 #ifdef CONFIG_BFIN_JTAG_COMM_CONSOLE
314 bfin_jc_console_write(struct console
*co
, const char *buf
, unsigned count
)
316 if (bfin_jc_kthread
== NULL
)
317 bfin_jc_straight_buffer_write(buf
, count
);
319 bfin_jc_circ_write(buf
, count
);
322 static struct tty_driver
*
323 bfin_jc_console_device(struct console
*co
, int *index
)
326 return bfin_jc_driver
;
329 static struct console bfin_jc_console
= {
331 .write
= bfin_jc_console_write
,
332 .device
= bfin_jc_console_device
,
333 .flags
= CON_ANYTIME
| CON_PRINTBUFFER
,
337 static int __init
bfin_jc_console_init(void)
339 register_console(&bfin_jc_console
);
342 console_initcall(bfin_jc_console_init
);
345 #ifdef CONFIG_EARLY_PRINTK
347 bfin_jc_early_write(struct console
*co
, const char *buf
, unsigned int count
)
349 bfin_jc_straight_buffer_write(buf
, count
);
352 static struct __initdata console bfin_jc_early_console
= {
353 .name
= "early_BFJC",
354 .write
= bfin_jc_early_write
,
355 .flags
= CON_ANYTIME
| CON_PRINTBUFFER
,
359 struct console
* __init
360 bfin_jc_early_init(unsigned int port
, unsigned int cflag
)
362 return &bfin_jc_early_console
;
366 MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
367 MODULE_DESCRIPTION("TTY over Blackfin JTAG Communication");
368 MODULE_LICENSE("GPL");