]>
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> | |
60063497 | 28 | #include <linux/atomic.h> |
0b914218 | 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; | |
560460b8 | 65 | static struct tty_port port; |
0b914218 MF |
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()) { | |
e63f9f74 | 74 | struct tty_struct *tty = tty_port_tty_get(&port); |
0b914218 | 75 | /* no one left to give data to, so sleep */ |
e63f9f74 | 76 | if (tty == NULL && circ_empty(&bfin_jc_write_buf)) { |
56578abf | 77 | pr_debug("waiting for readers\n"); |
0b914218 MF |
78 | __set_current_state(TASK_UNINTERRUPTIBLE); |
79 | schedule(); | |
80 | __set_current_state(TASK_RUNNING); | |
e63f9f74 | 81 | continue; |
0b914218 MF |
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 | 87 | inbound_len, bfin_jc_write_buf.tail, bfin_jc_write_buf.head); |
e63f9f74 | 88 | tty_kref_put(tty); |
0b914218 MF |
89 | 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) { | |
0b914218 MF |
98 | if (tty != NULL) { |
99 | uint32_t emudat = bfin_read_emudat(); | |
100 | if (inbound_len == 0) { | |
56578abf | 101 | pr_debug("incoming length: 0x%08x\n", emudat); |
0b914218 MF |
102 | inbound_len = emudat; |
103 | } else { | |
104 | size_t num_chars = (4 <= inbound_len ? 4 : inbound_len); | |
56578abf | 105 | pr_debug(" incoming data: 0x%08x (pushing %zu)\n", emudat, num_chars); |
0b914218 MF |
106 | inbound_len -= num_chars; |
107 | tty_insert_flip_string(tty, (unsigned char *)&emudat, num_chars); | |
108 | tty_flip_buffer_push(tty); | |
109 | } | |
110 | } | |
0b914218 MF |
111 | } |
112 | ||
113 | /* if outgoing data is ready, post it */ | |
114 | if (!(bfin_read_DBGSTAT() & EMUDOF) && !circ_empty(&bfin_jc_write_buf)) { | |
115 | if (outbound_len == 0) { | |
116 | outbound_len = circ_cnt(&bfin_jc_write_buf); | |
117 | bfin_write_emudat(outbound_len); | |
56578abf | 118 | pr_debug("outgoing length: 0x%08x\n", outbound_len); |
0b914218 | 119 | } else { |
0b914218 MF |
120 | int tail = bfin_jc_write_buf.tail; |
121 | size_t ate = (4 <= outbound_len ? 4 : outbound_len); | |
122 | uint32_t emudat = | |
123 | bfin_write_emudat_chars( | |
124 | circ_byte(&bfin_jc_write_buf, tail + 0), | |
125 | circ_byte(&bfin_jc_write_buf, tail + 1), | |
126 | circ_byte(&bfin_jc_write_buf, tail + 2), | |
127 | circ_byte(&bfin_jc_write_buf, tail + 3) | |
128 | ); | |
129 | bfin_jc_write_buf.tail += ate; | |
130 | outbound_len -= ate; | |
0b914218 MF |
131 | if (tty) |
132 | tty_wakeup(tty); | |
56578abf | 133 | pr_debug(" outgoing data: 0x%08x (pushing %zu)\n", emudat, ate); |
0b914218 MF |
134 | } |
135 | } | |
e63f9f74 | 136 | tty_kref_put(tty); |
0b914218 MF |
137 | } |
138 | ||
139 | __set_current_state(TASK_RUNNING); | |
140 | return 0; | |
141 | } | |
142 | ||
143 | static int | |
144 | bfin_jc_open(struct tty_struct *tty, struct file *filp) | |
145 | { | |
e63f9f74 JS |
146 | unsigned long flags; |
147 | ||
148 | spin_lock_irqsave(&port.lock, flags); | |
560460b8 | 149 | port.count++; |
e63f9f74 JS |
150 | spin_unlock_irqrestore(&port.lock, flags); |
151 | tty_port_tty_set(&port, tty); | |
0b914218 | 152 | wake_up_process(bfin_jc_kthread); |
0b914218 MF |
153 | return 0; |
154 | } | |
155 | ||
156 | static void | |
157 | bfin_jc_close(struct tty_struct *tty, struct file *filp) | |
158 | { | |
e63f9f74 JS |
159 | unsigned long flags; |
160 | bool last; | |
161 | ||
162 | spin_lock_irqsave(&port.lock, flags); | |
163 | last = --port.count == 0; | |
164 | spin_unlock_irqrestore(&port.lock, flags); | |
165 | if (last) | |
166 | tty_port_tty_set(&port, NULL); | |
0b914218 | 167 | wake_up_process(bfin_jc_kthread); |
0b914218 MF |
168 | } |
169 | ||
170 | /* XXX: we dont handle the put_char() case where we must handle count = 1 */ | |
171 | static int | |
172 | bfin_jc_circ_write(const unsigned char *buf, int count) | |
173 | { | |
174 | int i; | |
175 | count = min(count, circ_free(&bfin_jc_write_buf)); | |
56578abf | 176 | pr_debug("going to write chunk of %i bytes\n", count); |
0b914218 MF |
177 | for (i = 0; i < count; ++i) |
178 | circ_byte(&bfin_jc_write_buf, bfin_jc_write_buf.head + i) = buf[i]; | |
179 | bfin_jc_write_buf.head += i; | |
180 | return i; | |
181 | } | |
182 | ||
183 | #ifndef CONFIG_BFIN_JTAG_COMM_CONSOLE | |
ac751efa TH |
184 | # define console_lock() |
185 | # define console_unlock() | |
0b914218 MF |
186 | #endif |
187 | static int | |
188 | bfin_jc_write(struct tty_struct *tty, const unsigned char *buf, int count) | |
189 | { | |
190 | int i; | |
ac751efa | 191 | console_lock(); |
0b914218 | 192 | i = bfin_jc_circ_write(buf, count); |
ac751efa | 193 | console_unlock(); |
0b914218 MF |
194 | wake_up_process(bfin_jc_kthread); |
195 | return i; | |
196 | } | |
197 | ||
198 | static void | |
199 | bfin_jc_flush_chars(struct tty_struct *tty) | |
200 | { | |
201 | wake_up_process(bfin_jc_kthread); | |
202 | } | |
203 | ||
204 | static int | |
205 | bfin_jc_write_room(struct tty_struct *tty) | |
206 | { | |
207 | return circ_free(&bfin_jc_write_buf); | |
208 | } | |
209 | ||
210 | static int | |
211 | bfin_jc_chars_in_buffer(struct tty_struct *tty) | |
212 | { | |
213 | return circ_cnt(&bfin_jc_write_buf); | |
214 | } | |
215 | ||
216 | static void | |
217 | bfin_jc_wait_until_sent(struct tty_struct *tty, int timeout) | |
218 | { | |
219 | unsigned long expire = jiffies + timeout; | |
220 | while (!circ_empty(&bfin_jc_write_buf)) { | |
221 | if (signal_pending(current)) | |
222 | break; | |
223 | if (time_after(jiffies, expire)) | |
224 | break; | |
225 | } | |
226 | } | |
227 | ||
1cceefd3 | 228 | static const struct tty_operations bfin_jc_ops = { |
0b914218 MF |
229 | .open = bfin_jc_open, |
230 | .close = bfin_jc_close, | |
231 | .write = bfin_jc_write, | |
232 | /*.put_char = bfin_jc_put_char,*/ | |
233 | .flush_chars = bfin_jc_flush_chars, | |
234 | .write_room = bfin_jc_write_room, | |
235 | .chars_in_buffer = bfin_jc_chars_in_buffer, | |
236 | .wait_until_sent = bfin_jc_wait_until_sent, | |
237 | }; | |
238 | ||
239 | static int __init bfin_jc_init(void) | |
240 | { | |
241 | int ret; | |
242 | ||
560460b8 JS |
243 | tty_port_init(&port); |
244 | ||
0b914218 MF |
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) | |
d9d691f5 | 254 | goto err_buf; |
0b914218 MF |
255 | |
256 | bfin_jc_driver = alloc_tty_driver(1); | |
257 | if (!bfin_jc_driver) | |
d9d691f5 | 258 | goto err_driver; |
0b914218 | 259 | |
0b914218 MF |
260 | bfin_jc_driver->driver_name = DRV_NAME; |
261 | bfin_jc_driver->name = DEV_NAME; | |
262 | bfin_jc_driver->type = TTY_DRIVER_TYPE_SERIAL; | |
263 | bfin_jc_driver->subtype = SERIAL_TYPE_NORMAL; | |
264 | bfin_jc_driver->init_termios = tty_std_termios; | |
265 | tty_set_operations(bfin_jc_driver, &bfin_jc_ops); | |
266 | ||
267 | ret = tty_register_driver(bfin_jc_driver); | |
268 | if (ret) | |
269 | goto err; | |
270 | ||
271 | pr_init(KERN_INFO DRV_NAME ": initialized\n"); | |
272 | ||
273 | return 0; | |
274 | ||
275 | err: | |
276 | put_tty_driver(bfin_jc_driver); | |
d9d691f5 | 277 | err_driver: |
0b914218 | 278 | kfree(bfin_jc_write_buf.buf); |
d9d691f5 | 279 | err_buf: |
0b914218 MF |
280 | kthread_stop(bfin_jc_kthread); |
281 | return ret; | |
282 | } | |
283 | module_init(bfin_jc_init); | |
284 | ||
285 | static 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); | |
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 | ||
351 | static struct __initdata console bfin_jc_early_console = { | |
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 <[email protected]>"); | |
366 | MODULE_DESCRIPTION("TTY over Blackfin JTAG Communication"); | |
367 | MODULE_LICENSE("GPL"); |