blob: 8096fcbe2dc171d4742b561fdcaa0f60e1894547 [file] [log] [blame]
Mike Frysinger0b914212009-06-11 14:01:45 +01001/*
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
Mike Frysinger56578ab2009-06-22 18:31:10 +010011#define DRV_NAME "bfin-jtag-comm"
12#define DEV_NAME "ttyBFJC"
13#define pr_fmt(fmt) DRV_NAME ": " fmt
14
Mike Frysinger0b914212009-06-11 14:01:45 +010015#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Mike Frysinger0b914212009-06-11 14:01:45 +010025#include <linux/tty.h>
26#include <linux/tty_driver.h>
27#include <linux/tty_flip.h>
Arun Sharma600634972011-07-26 16:09:06 -070028#include <linux/atomic.h>
Mike Frysinger0b914212009-06-11 14:01:45 +010029
Mike Frysinger56578ab2009-06-22 18:31:10 +010030#define pr_init(fmt, args...) ({ static const __initconst char __fmt[] = fmt; printk(__fmt, ## args); })
31
Mike Frysinger0b914212009-06-11 14:01:45 +010032/* 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
Mike Frysinger0b914212009-06-11 14:01:45 +010038static inline uint32_t bfin_write_emudat(uint32_t emudat)
39{
40 __asm__ __volatile__("emudat = %0;" : : "d"(emudat));
41 return emudat;
42}
43
44static inline uint32_t bfin_read_emudat(void)
45{
46 uint32_t emudat;
47 __asm__ __volatile__("%0 = emudat;" : "=d"(emudat));
48 return emudat;
49}
50
51static 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
63static struct tty_driver *bfin_jc_driver;
64static struct task_struct *bfin_jc_kthread;
Jiri Slaby560460b2012-04-02 13:54:19 +020065static struct tty_port port;
Mike Frysinger0b914212009-06-11 14:01:45 +010066static volatile struct circ_buf bfin_jc_write_buf;
67
68static int
69bfin_jc_emudat_manager(void *arg)
70{
71 uint32_t inbound_len = 0, outbound_len = 0;
72
73 while (!kthread_should_stop()) {
Jiri Slabye63f9f72012-04-02 13:54:20 +020074 struct tty_struct *tty = tty_port_tty_get(&port);
Mike Frysinger0b914212009-06-11 14:01:45 +010075 /* no one left to give data to, so sleep */
Jiri Slabye63f9f72012-04-02 13:54:20 +020076 if (tty == NULL && circ_empty(&bfin_jc_write_buf)) {
Mike Frysinger56578ab2009-06-22 18:31:10 +010077 pr_debug("waiting for readers\n");
Mike Frysinger0b914212009-06-11 14:01:45 +010078 __set_current_state(TASK_UNINTERRUPTIBLE);
79 schedule();
80 __set_current_state(TASK_RUNNING);
Jiri Slabye63f9f72012-04-02 13:54:20 +020081 continue;
Mike Frysinger0b914212009-06-11 14:01:45 +010082 }
83
84 /* no data available, so just chill */
85 if (!(bfin_read_DBGSTAT() & EMUDIF) && circ_empty(&bfin_jc_write_buf)) {
Mike Frysinger56578ab2009-06-22 18:31:10 +010086 pr_debug("waiting for data (in_len = %i) (circ: %i %i)\n",
Mike Frysinger0b914212009-06-11 14:01:45 +010087 inbound_len, bfin_jc_write_buf.tail, bfin_jc_write_buf.head);
Jiri Slabye63f9f72012-04-02 13:54:20 +020088 tty_kref_put(tty);
Mike Frysinger0b914212009-06-11 14:01:45 +010089 if (inbound_len)
90 schedule();
91 else
92 schedule_timeout_interruptible(HZ);
93 continue;
94 }
95
96 /* if incoming data is ready, eat it */
97 if (bfin_read_DBGSTAT() & EMUDIF) {
Jiri Slaby2e124b42013-01-03 15:53:06 +010098 uint32_t emudat = bfin_read_emudat();
99 if (inbound_len == 0) {
100 pr_debug("incoming length: 0x%08x\n", emudat);
101 inbound_len = emudat;
102 } else {
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);
Mike Frysinger0b914212009-06-11 14:01:45 +0100108 }
Mike Frysinger0b914212009-06-11 14:01:45 +0100109 }
110
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);
Mike Frysinger56578ab2009-06-22 18:31:10 +0100116 pr_debug("outgoing length: 0x%08x\n", outbound_len);
Mike Frysinger0b914212009-06-11 14:01:45 +0100117 } else {
Mike Frysinger0b914212009-06-11 14:01:45 +0100118 int tail = bfin_jc_write_buf.tail;
119 size_t ate = (4 <= outbound_len ? 4 : outbound_len);
120 uint32_t emudat =
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)
126 );
127 bfin_jc_write_buf.tail += ate;
128 outbound_len -= ate;
Mike Frysinger0b914212009-06-11 14:01:45 +0100129 if (tty)
130 tty_wakeup(tty);
Mike Frysinger56578ab2009-06-22 18:31:10 +0100131 pr_debug(" outgoing data: 0x%08x (pushing %zu)\n", emudat, ate);
Mike Frysinger0b914212009-06-11 14:01:45 +0100132 }
133 }
Jiri Slabye63f9f72012-04-02 13:54:20 +0200134 tty_kref_put(tty);
Mike Frysinger0b914212009-06-11 14:01:45 +0100135 }
136
137 __set_current_state(TASK_RUNNING);
138 return 0;
139}
140
141static int
142bfin_jc_open(struct tty_struct *tty, struct file *filp)
143{
Jiri Slabye63f9f72012-04-02 13:54:20 +0200144 unsigned long flags;
145
146 spin_lock_irqsave(&port.lock, flags);
Jiri Slaby560460b2012-04-02 13:54:19 +0200147 port.count++;
Jiri Slabye63f9f72012-04-02 13:54:20 +0200148 spin_unlock_irqrestore(&port.lock, flags);
149 tty_port_tty_set(&port, tty);
Mike Frysinger0b914212009-06-11 14:01:45 +0100150 wake_up_process(bfin_jc_kthread);
Mike Frysinger0b914212009-06-11 14:01:45 +0100151 return 0;
152}
153
154static void
155bfin_jc_close(struct tty_struct *tty, struct file *filp)
156{
Jiri Slabye63f9f72012-04-02 13:54:20 +0200157 unsigned long flags;
158 bool last;
159
160 spin_lock_irqsave(&port.lock, flags);
161 last = --port.count == 0;
162 spin_unlock_irqrestore(&port.lock, flags);
163 if (last)
164 tty_port_tty_set(&port, NULL);
Mike Frysinger0b914212009-06-11 14:01:45 +0100165 wake_up_process(bfin_jc_kthread);
Mike Frysinger0b914212009-06-11 14:01:45 +0100166}
167
168/* XXX: we dont handle the put_char() case where we must handle count = 1 */
169static int
170bfin_jc_circ_write(const unsigned char *buf, int count)
171{
172 int i;
173 count = min(count, circ_free(&bfin_jc_write_buf));
Mike Frysinger56578ab2009-06-22 18:31:10 +0100174 pr_debug("going to write chunk of %i bytes\n", count);
Mike Frysinger0b914212009-06-11 14:01:45 +0100175 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;
178 return i;
179}
180
181#ifndef CONFIG_BFIN_JTAG_COMM_CONSOLE
Torben Hohnac751ef2011-01-25 15:07:35 -0800182# define console_lock()
183# define console_unlock()
Mike Frysinger0b914212009-06-11 14:01:45 +0100184#endif
185static int
186bfin_jc_write(struct tty_struct *tty, const unsigned char *buf, int count)
187{
188 int i;
Torben Hohnac751ef2011-01-25 15:07:35 -0800189 console_lock();
Mike Frysinger0b914212009-06-11 14:01:45 +0100190 i = bfin_jc_circ_write(buf, count);
Torben Hohnac751ef2011-01-25 15:07:35 -0800191 console_unlock();
Mike Frysinger0b914212009-06-11 14:01:45 +0100192 wake_up_process(bfin_jc_kthread);
193 return i;
194}
195
196static void
197bfin_jc_flush_chars(struct tty_struct *tty)
198{
199 wake_up_process(bfin_jc_kthread);
200}
201
202static int
203bfin_jc_write_room(struct tty_struct *tty)
204{
205 return circ_free(&bfin_jc_write_buf);
206}
207
208static int
209bfin_jc_chars_in_buffer(struct tty_struct *tty)
210{
211 return circ_cnt(&bfin_jc_write_buf);
212}
213
214static void
215bfin_jc_wait_until_sent(struct tty_struct *tty, int timeout)
216{
217 unsigned long expire = jiffies + timeout;
218 while (!circ_empty(&bfin_jc_write_buf)) {
219 if (signal_pending(current))
220 break;
221 if (time_after(jiffies, expire))
222 break;
223 }
224}
225
Alexey Dobriyan1cceefd2009-10-03 00:12:06 +0400226static const struct tty_operations bfin_jc_ops = {
Mike Frysinger0b914212009-06-11 14:01:45 +0100227 .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,
235};
236
237static int __init bfin_jc_init(void)
238{
239 int ret;
240
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);
244
245 ret = -ENOMEM;
246
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)
Julia Lawalld9d691f2011-03-23 16:42:56 -0700250 goto err_buf;
Mike Frysinger0b914212009-06-11 14:01:45 +0100251
252 bfin_jc_driver = alloc_tty_driver(1);
253 if (!bfin_jc_driver)
Julia Lawalld9d691f2011-03-23 16:42:56 -0700254 goto err_driver;
Mike Frysinger0b914212009-06-11 14:01:45 +0100255
Jiri Slaby191c5f12012-11-15 09:49:56 +0100256 tty_port_init(&port);
257
Mike Frysinger0b914212009-06-11 14:01:45 +0100258 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);
Jiri Slabyb19e2ca2012-08-07 21:47:51 +0200264 tty_port_link_device(&port, bfin_jc_driver, 0);
Mike Frysinger0b914212009-06-11 14:01:45 +0100265
266 ret = tty_register_driver(bfin_jc_driver);
267 if (ret)
268 goto err;
269
270 pr_init(KERN_INFO DRV_NAME ": initialized\n");
271
272 return 0;
273
274 err:
Jiri Slaby191c5f12012-11-15 09:49:56 +0100275 tty_port_destroy(&port);
Mike Frysinger0b914212009-06-11 14:01:45 +0100276 put_tty_driver(bfin_jc_driver);
Julia Lawalld9d691f2011-03-23 16:42:56 -0700277 err_driver:
Mike Frysinger0b914212009-06-11 14:01:45 +0100278 kfree(bfin_jc_write_buf.buf);
Julia Lawalld9d691f2011-03-23 16:42:56 -0700279 err_buf:
Mike Frysinger0b914212009-06-11 14:01:45 +0100280 kthread_stop(bfin_jc_kthread);
281 return ret;
282}
283module_init(bfin_jc_init);
284
285static void __exit bfin_jc_exit(void)
286{
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);
Jiri Slaby191c5f12012-11-15 09:49:56 +0100291 tty_port_destroy(&port);
Mike Frysinger0b914212009-06-11 14:01:45 +0100292}
293module_exit(bfin_jc_exit);
294
295#if defined(CONFIG_BFIN_JTAG_COMM_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
296static void
297bfin_jc_straight_buffer_write(const char *buf, unsigned count)
298{
299 unsigned ate = 0;
300 while (bfin_read_DBGSTAT() & EMUDOF)
301 continue;
302 bfin_write_emudat(count);
303 while (ate < count) {
304 while (bfin_read_DBGSTAT() & EMUDOF)
305 continue;
306 bfin_write_emudat_chars(buf[ate], buf[ate+1], buf[ate+2], buf[ate+3]);
307 ate += 4;
308 }
309}
310#endif
311
312#ifdef CONFIG_BFIN_JTAG_COMM_CONSOLE
313static void
314bfin_jc_console_write(struct console *co, const char *buf, unsigned count)
315{
316 if (bfin_jc_kthread == NULL)
317 bfin_jc_straight_buffer_write(buf, count);
318 else
319 bfin_jc_circ_write(buf, count);
320}
321
322static struct tty_driver *
323bfin_jc_console_device(struct console *co, int *index)
324{
325 *index = co->index;
326 return bfin_jc_driver;
327}
328
329static struct console bfin_jc_console = {
330 .name = DEV_NAME,
331 .write = bfin_jc_console_write,
332 .device = bfin_jc_console_device,
333 .flags = CON_ANYTIME | CON_PRINTBUFFER,
334 .index = -1,
335};
336
337static int __init bfin_jc_console_init(void)
338{
339 register_console(&bfin_jc_console);
340 return 0;
341}
342console_initcall(bfin_jc_console_init);
343#endif
344
345#ifdef CONFIG_EARLY_PRINTK
346static void __init
347bfin_jc_early_write(struct console *co, const char *buf, unsigned int count)
348{
349 bfin_jc_straight_buffer_write(buf, count);
350}
351
Bartlomiej Zolnierkiewiczf8294522013-09-30 15:19:36 +0200352static struct console bfin_jc_early_console __initdata = {
Mike Frysinger0b914212009-06-11 14:01:45 +0100353 .name = "early_BFJC",
354 .write = bfin_jc_early_write,
355 .flags = CON_ANYTIME | CON_PRINTBUFFER,
356 .index = -1,
357};
358
359struct console * __init
360bfin_jc_early_init(unsigned int port, unsigned int cflag)
361{
362 return &bfin_jc_early_console;
363}
364#endif
365
366MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
367MODULE_DESCRIPTION("TTY over Blackfin JTAG Communication");
368MODULE_LICENSE("GPL");