Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 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 | |
Mike Frysinger | 56578ab | 2009-06-22 18:31:10 +0100 | [diff] [blame] | 11 | #define DRV_NAME "bfin-jtag-comm" |
| 12 | #define DEV_NAME "ttyBFJC" |
| 13 | #define pr_fmt(fmt) DRV_NAME ": " fmt |
| 14 | |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 25 | #include <linux/tty.h> |
| 26 | #include <linux/tty_driver.h> |
| 27 | #include <linux/tty_flip.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 28 | #include <linux/atomic.h> |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 29 | |
Mike Frysinger | 56578ab | 2009-06-22 18:31:10 +0100 | [diff] [blame] | 30 | #define pr_init(fmt, args...) ({ static const __initconst char __fmt[] = fmt; printk(__fmt, ## args); }) |
| 31 | |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 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 | |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 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; |
Jiri Slaby | 560460b | 2012-04-02 13:54:19 +0200 | [diff] [blame] | 65 | static struct tty_port port; |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 66 | static volatile struct circ_buf bfin_jc_write_buf; |
| 67 | |
| 68 | static int |
| 69 | bfin_jc_emudat_manager(void *arg) |
| 70 | { |
| 71 | uint32_t inbound_len = 0, outbound_len = 0; |
| 72 | |
| 73 | while (!kthread_should_stop()) { |
Jiri Slaby | e63f9f7 | 2012-04-02 13:54:20 +0200 | [diff] [blame] | 74 | struct tty_struct *tty = tty_port_tty_get(&port); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 75 | /* no one left to give data to, so sleep */ |
Jiri Slaby | e63f9f7 | 2012-04-02 13:54:20 +0200 | [diff] [blame] | 76 | if (tty == NULL && circ_empty(&bfin_jc_write_buf)) { |
Mike Frysinger | 56578ab | 2009-06-22 18:31:10 +0100 | [diff] [blame] | 77 | pr_debug("waiting for readers\n"); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 78 | __set_current_state(TASK_UNINTERRUPTIBLE); |
| 79 | schedule(); |
Jiri Slaby | e63f9f7 | 2012-04-02 13:54:20 +0200 | [diff] [blame] | 80 | continue; |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* no data available, so just chill */ |
| 84 | if (!(bfin_read_DBGSTAT() & EMUDIF) && circ_empty(&bfin_jc_write_buf)) { |
Mike Frysinger | 56578ab | 2009-06-22 18:31:10 +0100 | [diff] [blame] | 85 | pr_debug("waiting for data (in_len = %i) (circ: %i %i)\n", |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 86 | inbound_len, bfin_jc_write_buf.tail, bfin_jc_write_buf.head); |
Jiri Slaby | e63f9f7 | 2012-04-02 13:54:20 +0200 | [diff] [blame] | 87 | tty_kref_put(tty); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 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) { |
Jiri Slaby | 2e124b4 | 2013-01-03 15:53:06 +0100 | [diff] [blame] | 97 | uint32_t emudat = bfin_read_emudat(); |
| 98 | if (inbound_len == 0) { |
| 99 | pr_debug("incoming length: 0x%08x\n", emudat); |
| 100 | inbound_len = emudat; |
| 101 | } else { |
| 102 | size_t num_chars = (4 <= inbound_len ? 4 : inbound_len); |
| 103 | pr_debug(" incoming data: 0x%08x (pushing %zu)\n", emudat, num_chars); |
| 104 | inbound_len -= num_chars; |
| 105 | tty_insert_flip_string(&port, (unsigned char *)&emudat, num_chars); |
| 106 | tty_flip_buffer_push(&port); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 107 | } |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* if outgoing data is ready, post it */ |
| 111 | if (!(bfin_read_DBGSTAT() & EMUDOF) && !circ_empty(&bfin_jc_write_buf)) { |
| 112 | if (outbound_len == 0) { |
| 113 | outbound_len = circ_cnt(&bfin_jc_write_buf); |
| 114 | bfin_write_emudat(outbound_len); |
Mike Frysinger | 56578ab | 2009-06-22 18:31:10 +0100 | [diff] [blame] | 115 | pr_debug("outgoing length: 0x%08x\n", outbound_len); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 116 | } else { |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 117 | int tail = bfin_jc_write_buf.tail; |
| 118 | size_t ate = (4 <= outbound_len ? 4 : outbound_len); |
| 119 | uint32_t emudat = |
| 120 | bfin_write_emudat_chars( |
| 121 | circ_byte(&bfin_jc_write_buf, tail + 0), |
| 122 | circ_byte(&bfin_jc_write_buf, tail + 1), |
| 123 | circ_byte(&bfin_jc_write_buf, tail + 2), |
| 124 | circ_byte(&bfin_jc_write_buf, tail + 3) |
| 125 | ); |
| 126 | bfin_jc_write_buf.tail += ate; |
| 127 | outbound_len -= ate; |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 128 | if (tty) |
| 129 | tty_wakeup(tty); |
Mike Frysinger | 56578ab | 2009-06-22 18:31:10 +0100 | [diff] [blame] | 130 | pr_debug(" outgoing data: 0x%08x (pushing %zu)\n", emudat, ate); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 131 | } |
| 132 | } |
Jiri Slaby | e63f9f7 | 2012-04-02 13:54:20 +0200 | [diff] [blame] | 133 | tty_kref_put(tty); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | __set_current_state(TASK_RUNNING); |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int |
| 141 | bfin_jc_open(struct tty_struct *tty, struct file *filp) |
| 142 | { |
Jiri Slaby | e63f9f7 | 2012-04-02 13:54:20 +0200 | [diff] [blame] | 143 | unsigned long flags; |
| 144 | |
| 145 | spin_lock_irqsave(&port.lock, flags); |
Jiri Slaby | 560460b | 2012-04-02 13:54:19 +0200 | [diff] [blame] | 146 | port.count++; |
Jiri Slaby | e63f9f7 | 2012-04-02 13:54:20 +0200 | [diff] [blame] | 147 | spin_unlock_irqrestore(&port.lock, flags); |
| 148 | tty_port_tty_set(&port, tty); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 149 | wake_up_process(bfin_jc_kthread); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static void |
| 154 | bfin_jc_close(struct tty_struct *tty, struct file *filp) |
| 155 | { |
Jiri Slaby | e63f9f7 | 2012-04-02 13:54:20 +0200 | [diff] [blame] | 156 | unsigned long flags; |
| 157 | bool last; |
| 158 | |
| 159 | spin_lock_irqsave(&port.lock, flags); |
| 160 | last = --port.count == 0; |
| 161 | spin_unlock_irqrestore(&port.lock, flags); |
| 162 | if (last) |
| 163 | tty_port_tty_set(&port, NULL); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 164 | wake_up_process(bfin_jc_kthread); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* XXX: we dont handle the put_char() case where we must handle count = 1 */ |
| 168 | static int |
| 169 | bfin_jc_circ_write(const unsigned char *buf, int count) |
| 170 | { |
| 171 | int i; |
| 172 | count = min(count, circ_free(&bfin_jc_write_buf)); |
Mike Frysinger | 56578ab | 2009-06-22 18:31:10 +0100 | [diff] [blame] | 173 | pr_debug("going to write chunk of %i bytes\n", count); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 174 | for (i = 0; i < count; ++i) |
| 175 | circ_byte(&bfin_jc_write_buf, bfin_jc_write_buf.head + i) = buf[i]; |
| 176 | bfin_jc_write_buf.head += i; |
| 177 | return i; |
| 178 | } |
| 179 | |
| 180 | #ifndef CONFIG_BFIN_JTAG_COMM_CONSOLE |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 181 | # define console_lock() |
| 182 | # define console_unlock() |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 183 | #endif |
| 184 | static int |
| 185 | bfin_jc_write(struct tty_struct *tty, const unsigned char *buf, int count) |
| 186 | { |
| 187 | int i; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 188 | console_lock(); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 189 | i = bfin_jc_circ_write(buf, count); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 190 | console_unlock(); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 191 | wake_up_process(bfin_jc_kthread); |
| 192 | return i; |
| 193 | } |
| 194 | |
| 195 | static void |
| 196 | bfin_jc_flush_chars(struct tty_struct *tty) |
| 197 | { |
| 198 | wake_up_process(bfin_jc_kthread); |
| 199 | } |
| 200 | |
| 201 | static int |
| 202 | bfin_jc_write_room(struct tty_struct *tty) |
| 203 | { |
| 204 | return circ_free(&bfin_jc_write_buf); |
| 205 | } |
| 206 | |
| 207 | static int |
| 208 | bfin_jc_chars_in_buffer(struct tty_struct *tty) |
| 209 | { |
| 210 | return circ_cnt(&bfin_jc_write_buf); |
| 211 | } |
| 212 | |
| 213 | static void |
| 214 | bfin_jc_wait_until_sent(struct tty_struct *tty, int timeout) |
| 215 | { |
| 216 | unsigned long expire = jiffies + timeout; |
| 217 | while (!circ_empty(&bfin_jc_write_buf)) { |
| 218 | if (signal_pending(current)) |
| 219 | break; |
| 220 | if (time_after(jiffies, expire)) |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | |
Alexey Dobriyan | 1cceefd | 2009-10-03 00:12:06 +0400 | [diff] [blame] | 225 | static const struct tty_operations bfin_jc_ops = { |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 226 | .open = bfin_jc_open, |
| 227 | .close = bfin_jc_close, |
| 228 | .write = bfin_jc_write, |
| 229 | /*.put_char = bfin_jc_put_char,*/ |
| 230 | .flush_chars = bfin_jc_flush_chars, |
| 231 | .write_room = bfin_jc_write_room, |
| 232 | .chars_in_buffer = bfin_jc_chars_in_buffer, |
| 233 | .wait_until_sent = bfin_jc_wait_until_sent, |
| 234 | }; |
| 235 | |
| 236 | static int __init bfin_jc_init(void) |
| 237 | { |
| 238 | int ret; |
| 239 | |
| 240 | bfin_jc_kthread = kthread_create(bfin_jc_emudat_manager, NULL, DRV_NAME); |
| 241 | if (IS_ERR(bfin_jc_kthread)) |
| 242 | return PTR_ERR(bfin_jc_kthread); |
| 243 | |
| 244 | ret = -ENOMEM; |
| 245 | |
| 246 | bfin_jc_write_buf.head = bfin_jc_write_buf.tail = 0; |
| 247 | bfin_jc_write_buf.buf = kmalloc(CIRC_SIZE, GFP_KERNEL); |
| 248 | if (!bfin_jc_write_buf.buf) |
Julia Lawall | d9d691f | 2011-03-23 16:42:56 -0700 | [diff] [blame] | 249 | goto err_buf; |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 250 | |
| 251 | bfin_jc_driver = alloc_tty_driver(1); |
| 252 | if (!bfin_jc_driver) |
Julia Lawall | d9d691f | 2011-03-23 16:42:56 -0700 | [diff] [blame] | 253 | goto err_driver; |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 254 | |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 255 | tty_port_init(&port); |
| 256 | |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 257 | bfin_jc_driver->driver_name = DRV_NAME; |
| 258 | bfin_jc_driver->name = DEV_NAME; |
| 259 | bfin_jc_driver->type = TTY_DRIVER_TYPE_SERIAL; |
| 260 | bfin_jc_driver->subtype = SERIAL_TYPE_NORMAL; |
| 261 | bfin_jc_driver->init_termios = tty_std_termios; |
| 262 | tty_set_operations(bfin_jc_driver, &bfin_jc_ops); |
Jiri Slaby | b19e2ca | 2012-08-07 21:47:51 +0200 | [diff] [blame] | 263 | tty_port_link_device(&port, bfin_jc_driver, 0); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 264 | |
| 265 | ret = tty_register_driver(bfin_jc_driver); |
| 266 | if (ret) |
| 267 | goto err; |
| 268 | |
| 269 | pr_init(KERN_INFO DRV_NAME ": initialized\n"); |
| 270 | |
| 271 | return 0; |
| 272 | |
| 273 | err: |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 274 | tty_port_destroy(&port); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 275 | put_tty_driver(bfin_jc_driver); |
Julia Lawall | d9d691f | 2011-03-23 16:42:56 -0700 | [diff] [blame] | 276 | err_driver: |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 277 | kfree(bfin_jc_write_buf.buf); |
Julia Lawall | d9d691f | 2011-03-23 16:42:56 -0700 | [diff] [blame] | 278 | err_buf: |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 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); |
Jiri Slaby | 191c5f1 | 2012-11-15 09:49:56 +0100 | [diff] [blame] | 290 | tty_port_destroy(&port); |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 291 | } |
| 292 | module_exit(bfin_jc_exit); |
| 293 | |
| 294 | #if defined(CONFIG_BFIN_JTAG_COMM_CONSOLE) || defined(CONFIG_EARLY_PRINTK) |
| 295 | static void |
| 296 | bfin_jc_straight_buffer_write(const char *buf, unsigned count) |
| 297 | { |
| 298 | unsigned ate = 0; |
| 299 | while (bfin_read_DBGSTAT() & EMUDOF) |
| 300 | continue; |
| 301 | bfin_write_emudat(count); |
| 302 | while (ate < count) { |
| 303 | while (bfin_read_DBGSTAT() & EMUDOF) |
| 304 | continue; |
| 305 | bfin_write_emudat_chars(buf[ate], buf[ate+1], buf[ate+2], buf[ate+3]); |
| 306 | ate += 4; |
| 307 | } |
| 308 | } |
| 309 | #endif |
| 310 | |
| 311 | #ifdef CONFIG_BFIN_JTAG_COMM_CONSOLE |
| 312 | static void |
| 313 | bfin_jc_console_write(struct console *co, const char *buf, unsigned count) |
| 314 | { |
| 315 | if (bfin_jc_kthread == NULL) |
| 316 | bfin_jc_straight_buffer_write(buf, count); |
| 317 | else |
| 318 | bfin_jc_circ_write(buf, count); |
| 319 | } |
| 320 | |
| 321 | static struct tty_driver * |
| 322 | bfin_jc_console_device(struct console *co, int *index) |
| 323 | { |
| 324 | *index = co->index; |
| 325 | return bfin_jc_driver; |
| 326 | } |
| 327 | |
| 328 | static struct console bfin_jc_console = { |
| 329 | .name = DEV_NAME, |
| 330 | .write = bfin_jc_console_write, |
| 331 | .device = bfin_jc_console_device, |
| 332 | .flags = CON_ANYTIME | CON_PRINTBUFFER, |
| 333 | .index = -1, |
| 334 | }; |
| 335 | |
| 336 | static int __init bfin_jc_console_init(void) |
| 337 | { |
| 338 | register_console(&bfin_jc_console); |
| 339 | return 0; |
| 340 | } |
| 341 | console_initcall(bfin_jc_console_init); |
| 342 | #endif |
| 343 | |
| 344 | #ifdef CONFIG_EARLY_PRINTK |
| 345 | static void __init |
| 346 | bfin_jc_early_write(struct console *co, const char *buf, unsigned int count) |
| 347 | { |
| 348 | bfin_jc_straight_buffer_write(buf, count); |
| 349 | } |
| 350 | |
Bartlomiej Zolnierkiewicz | f829452 | 2013-09-30 15:19:36 +0200 | [diff] [blame] | 351 | static struct console bfin_jc_early_console __initdata = { |
Mike Frysinger | 0b91421 | 2009-06-11 14:01:45 +0100 | [diff] [blame] | 352 | .name = "early_BFJC", |
| 353 | .write = bfin_jc_early_write, |
| 354 | .flags = CON_ANYTIME | CON_PRINTBUFFER, |
| 355 | .index = -1, |
| 356 | }; |
| 357 | |
| 358 | struct console * __init |
| 359 | bfin_jc_early_init(unsigned int port, unsigned int cflag) |
| 360 | { |
| 361 | return &bfin_jc_early_console; |
| 362 | } |
| 363 | #endif |
| 364 | |
| 365 | MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>"); |
| 366 | MODULE_DESCRIPTION("TTY over Blackfin JTAG Communication"); |
| 367 | MODULE_LICENSE("GPL"); |