1 // SPDX-License-Identifier: GPL-2.0+
3 * TTY over Blackfin JTAG Communication
5 * Copyright 2008-2009 Analog Devices Inc.
7 * Enter bugs at http://blackfin.uclinux.org/
10 #define DRV_NAME "bfin-jtag-comm"
11 #define DEV_NAME "ttyBFJC"
12 #define pr_fmt(fmt) DRV_NAME ": " fmt
14 #include <linux/circ_buf.h>
15 #include <linux/console.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/kernel.h>
19 #include <linux/kthread.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
24 #include <linux/tty.h>
25 #include <linux/tty_driver.h>
26 #include <linux/tty_flip.h>
27 #include <linux/atomic.h>
29 #define pr_init(fmt, args...) ({ static const __initconst char __fmt[] = fmt; printk(__fmt, ## args); })
31 /* See the Debug/Emulation chapter in the HRM */
32 #define EMUDOF 0x00000001 /* EMUDAT_OUT full & valid */
33 #define EMUDIF 0x00000002 /* EMUDAT_IN full & valid */
34 #define EMUDOOVF 0x00000004 /* EMUDAT_OUT overflow */
35 #define EMUDIOVF 0x00000008 /* EMUDAT_IN overflow */
37 static inline uint32_t bfin_write_emudat(uint32_t emudat
)
39 __asm__
__volatile__("emudat = %0;" : : "d"(emudat
));
43 static inline uint32_t bfin_read_emudat(void)
46 __asm__
__volatile__("%0 = emudat;" : "=d"(emudat
));
50 static inline uint32_t bfin_write_emudat_chars(char a
, char b
, char c
, char d
)
52 return bfin_write_emudat((a
<< 0) | (b
<< 8) | (c
<< 16) | (d
<< 24));
55 #define CIRC_SIZE 2048 /* see comment in tty_io.c:do_tty_write() */
56 #define CIRC_MASK (CIRC_SIZE - 1)
57 #define circ_empty(circ) ((circ)->head == (circ)->tail)
58 #define circ_free(circ) CIRC_SPACE((circ)->head, (circ)->tail, CIRC_SIZE)
59 #define circ_cnt(circ) CIRC_CNT((circ)->head, (circ)->tail, CIRC_SIZE)
60 #define circ_byte(circ, idx) ((circ)->buf[(idx) & CIRC_MASK])
62 static struct tty_driver
*bfin_jc_driver
;
63 static struct task_struct
*bfin_jc_kthread
;
64 static struct tty_port port
;
65 static volatile struct circ_buf bfin_jc_write_buf
;
68 bfin_jc_emudat_manager(void *arg
)
70 uint32_t inbound_len
= 0, outbound_len
= 0;
72 while (!kthread_should_stop()) {
73 struct tty_struct
*tty
= tty_port_tty_get(&port
);
74 /* no one left to give data to, so sleep */
75 if (tty
== NULL
&& circ_empty(&bfin_jc_write_buf
)) {
76 pr_debug("waiting for readers\n");
77 __set_current_state(TASK_UNINTERRUPTIBLE
);
82 /* no data available, so just chill */
83 if (!(bfin_read_DBGSTAT() & EMUDIF
) && circ_empty(&bfin_jc_write_buf
)) {
84 pr_debug("waiting for data (in_len = %i) (circ: %i %i)\n",
85 inbound_len
, bfin_jc_write_buf
.tail
, bfin_jc_write_buf
.head
);
90 schedule_timeout_interruptible(HZ
);
94 /* if incoming data is ready, eat it */
95 if (bfin_read_DBGSTAT() & EMUDIF
) {
96 uint32_t emudat
= bfin_read_emudat();
97 if (inbound_len
== 0) {
98 pr_debug("incoming length: 0x%08x\n", emudat
);
101 size_t num_chars
= (4 <= inbound_len
? 4 : inbound_len
);
102 pr_debug(" incoming data: 0x%08x (pushing %zu)\n", emudat
, num_chars
);
103 inbound_len
-= num_chars
;
104 tty_insert_flip_string(&port
, (unsigned char *)&emudat
, num_chars
);
105 tty_flip_buffer_push(&port
);
109 /* if outgoing data is ready, post it */
110 if (!(bfin_read_DBGSTAT() & EMUDOF
) && !circ_empty(&bfin_jc_write_buf
)) {
111 if (outbound_len
== 0) {
112 outbound_len
= circ_cnt(&bfin_jc_write_buf
);
113 bfin_write_emudat(outbound_len
);
114 pr_debug("outgoing length: 0x%08x\n", outbound_len
);
116 int tail
= bfin_jc_write_buf
.tail
;
117 size_t ate
= (4 <= outbound_len
? 4 : outbound_len
);
119 bfin_write_emudat_chars(
120 circ_byte(&bfin_jc_write_buf
, tail
+ 0),
121 circ_byte(&bfin_jc_write_buf
, tail
+ 1),
122 circ_byte(&bfin_jc_write_buf
, tail
+ 2),
123 circ_byte(&bfin_jc_write_buf
, tail
+ 3)
125 bfin_jc_write_buf
.tail
+= ate
;
129 pr_debug(" outgoing data: 0x%08x (pushing %zu)\n", emudat
, ate
);
135 __set_current_state(TASK_RUNNING
);
140 bfin_jc_open(struct tty_struct
*tty
, struct file
*filp
)
144 spin_lock_irqsave(&port
.lock
, flags
);
146 spin_unlock_irqrestore(&port
.lock
, flags
);
147 tty_port_tty_set(&port
, tty
);
148 wake_up_process(bfin_jc_kthread
);
153 bfin_jc_close(struct tty_struct
*tty
, struct file
*filp
)
158 spin_lock_irqsave(&port
.lock
, flags
);
159 last
= --port
.count
== 0;
160 spin_unlock_irqrestore(&port
.lock
, flags
);
162 tty_port_tty_set(&port
, NULL
);
163 wake_up_process(bfin_jc_kthread
);
166 /* XXX: we dont handle the put_char() case where we must handle count = 1 */
168 bfin_jc_circ_write(const unsigned char *buf
, int count
)
171 count
= min(count
, circ_free(&bfin_jc_write_buf
));
172 pr_debug("going to write chunk of %i bytes\n", count
);
173 for (i
= 0; i
< count
; ++i
)
174 circ_byte(&bfin_jc_write_buf
, bfin_jc_write_buf
.head
+ i
) = buf
[i
];
175 bfin_jc_write_buf
.head
+= i
;
179 #ifndef CONFIG_BFIN_JTAG_COMM_CONSOLE
180 # define console_lock()
181 # define console_unlock()
184 bfin_jc_write(struct tty_struct
*tty
, const unsigned char *buf
, int count
)
188 i
= bfin_jc_circ_write(buf
, count
);
190 wake_up_process(bfin_jc_kthread
);
195 bfin_jc_flush_chars(struct tty_struct
*tty
)
197 wake_up_process(bfin_jc_kthread
);
201 bfin_jc_write_room(struct tty_struct
*tty
)
203 return circ_free(&bfin_jc_write_buf
);
207 bfin_jc_chars_in_buffer(struct tty_struct
*tty
)
209 return circ_cnt(&bfin_jc_write_buf
);
212 static const struct tty_operations bfin_jc_ops
= {
213 .open
= bfin_jc_open
,
214 .close
= bfin_jc_close
,
215 .write
= bfin_jc_write
,
216 /*.put_char = bfin_jc_put_char,*/
217 .flush_chars
= bfin_jc_flush_chars
,
218 .write_room
= bfin_jc_write_room
,
219 .chars_in_buffer
= bfin_jc_chars_in_buffer
,
222 static int __init
bfin_jc_init(void)
226 bfin_jc_kthread
= kthread_create(bfin_jc_emudat_manager
, NULL
, DRV_NAME
);
227 if (IS_ERR(bfin_jc_kthread
))
228 return PTR_ERR(bfin_jc_kthread
);
232 bfin_jc_write_buf
.head
= bfin_jc_write_buf
.tail
= 0;
233 bfin_jc_write_buf
.buf
= kmalloc(CIRC_SIZE
, GFP_KERNEL
);
234 if (!bfin_jc_write_buf
.buf
)
237 bfin_jc_driver
= alloc_tty_driver(1);
241 tty_port_init(&port
);
243 bfin_jc_driver
->driver_name
= DRV_NAME
;
244 bfin_jc_driver
->name
= DEV_NAME
;
245 bfin_jc_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
246 bfin_jc_driver
->subtype
= SERIAL_TYPE_NORMAL
;
247 bfin_jc_driver
->init_termios
= tty_std_termios
;
248 tty_set_operations(bfin_jc_driver
, &bfin_jc_ops
);
249 tty_port_link_device(&port
, bfin_jc_driver
, 0);
251 ret
= tty_register_driver(bfin_jc_driver
);
255 pr_init(KERN_INFO DRV_NAME
": initialized\n");
260 tty_port_destroy(&port
);
261 put_tty_driver(bfin_jc_driver
);
263 kfree(bfin_jc_write_buf
.buf
);
265 kthread_stop(bfin_jc_kthread
);
268 module_init(bfin_jc_init
);
270 static void __exit
bfin_jc_exit(void)
272 kthread_stop(bfin_jc_kthread
);
273 kfree(bfin_jc_write_buf
.buf
);
274 tty_unregister_driver(bfin_jc_driver
);
275 put_tty_driver(bfin_jc_driver
);
276 tty_port_destroy(&port
);
278 module_exit(bfin_jc_exit
);
280 #if defined(CONFIG_BFIN_JTAG_COMM_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
282 bfin_jc_straight_buffer_write(const char *buf
, unsigned count
)
285 while (bfin_read_DBGSTAT() & EMUDOF
)
287 bfin_write_emudat(count
);
288 while (ate
< count
) {
289 while (bfin_read_DBGSTAT() & EMUDOF
)
291 bfin_write_emudat_chars(buf
[ate
], buf
[ate
+1], buf
[ate
+2], buf
[ate
+3]);
297 #ifdef CONFIG_BFIN_JTAG_COMM_CONSOLE
299 bfin_jc_console_write(struct console
*co
, const char *buf
, unsigned count
)
301 if (bfin_jc_kthread
== NULL
)
302 bfin_jc_straight_buffer_write(buf
, count
);
304 bfin_jc_circ_write(buf
, count
);
307 static struct tty_driver
*
308 bfin_jc_console_device(struct console
*co
, int *index
)
311 return bfin_jc_driver
;
314 static struct console bfin_jc_console
= {
316 .write
= bfin_jc_console_write
,
317 .device
= bfin_jc_console_device
,
318 .flags
= CON_ANYTIME
| CON_PRINTBUFFER
,
322 static int __init
bfin_jc_console_init(void)
324 register_console(&bfin_jc_console
);
327 console_initcall(bfin_jc_console_init
);
330 #ifdef CONFIG_EARLY_PRINTK
332 bfin_jc_early_write(struct console
*co
, const char *buf
, unsigned int count
)
334 bfin_jc_straight_buffer_write(buf
, count
);
337 static struct console bfin_jc_early_console __initdata
= {
338 .name
= "early_BFJC",
339 .write
= bfin_jc_early_write
,
340 .flags
= CON_ANYTIME
| CON_PRINTBUFFER
,
344 struct console
* __init
345 bfin_jc_early_init(unsigned int port
, unsigned int cflag
)
347 return &bfin_jc_early_console
;
351 MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
352 MODULE_DESCRIPTION("TTY over Blackfin JTAG Communication");
353 MODULE_LICENSE("GPL");