]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/tty/bfin_jtag_comm.c
x86/speculation: Move arch_smt_update() call to after mitigation decisions
[mirror_ubuntu-bionic-kernel.git] / drivers / tty / bfin_jtag_comm.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * TTY over Blackfin JTAG Communication
4 *
5 * Copyright 2008-2009 Analog Devices Inc.
6 *
7 * Enter bugs at http://blackfin.uclinux.org/
8 */
9
10 #define DRV_NAME "bfin-jtag-comm"
11 #define DEV_NAME "ttyBFJC"
12 #define pr_fmt(fmt) DRV_NAME ": " fmt
13
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>
28
29 #define pr_init(fmt, args...) ({ static const __initconst char __fmt[] = fmt; printk(__fmt, ## args); })
30
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 */
36
37 static inline uint32_t bfin_write_emudat(uint32_t emudat)
38 {
39 __asm__ __volatile__("emudat = %0;" : : "d"(emudat));
40 return emudat;
41 }
42
43 static inline uint32_t bfin_read_emudat(void)
44 {
45 uint32_t emudat;
46 __asm__ __volatile__("%0 = emudat;" : "=d"(emudat));
47 return emudat;
48 }
49
50 static inline uint32_t bfin_write_emudat_chars(char a, char b, char c, char d)
51 {
52 return bfin_write_emudat((a << 0) | (b << 8) | (c << 16) | (d << 24));
53 }
54
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])
61
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;
66
67 static int
68 bfin_jc_emudat_manager(void *arg)
69 {
70 uint32_t inbound_len = 0, outbound_len = 0;
71
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);
78 schedule();
79 continue;
80 }
81
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);
86 tty_kref_put(tty);
87 if (inbound_len)
88 schedule();
89 else
90 schedule_timeout_interruptible(HZ);
91 continue;
92 }
93
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);
99 inbound_len = emudat;
100 } else {
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);
106 }
107 }
108
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);
115 } else {
116 int tail = bfin_jc_write_buf.tail;
117 size_t ate = (4 <= outbound_len ? 4 : outbound_len);
118 uint32_t emudat =
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)
124 );
125 bfin_jc_write_buf.tail += ate;
126 outbound_len -= ate;
127 if (tty)
128 tty_wakeup(tty);
129 pr_debug(" outgoing data: 0x%08x (pushing %zu)\n", emudat, ate);
130 }
131 }
132 tty_kref_put(tty);
133 }
134
135 __set_current_state(TASK_RUNNING);
136 return 0;
137 }
138
139 static int
140 bfin_jc_open(struct tty_struct *tty, struct file *filp)
141 {
142 unsigned long flags;
143
144 spin_lock_irqsave(&port.lock, flags);
145 port.count++;
146 spin_unlock_irqrestore(&port.lock, flags);
147 tty_port_tty_set(&port, tty);
148 wake_up_process(bfin_jc_kthread);
149 return 0;
150 }
151
152 static void
153 bfin_jc_close(struct tty_struct *tty, struct file *filp)
154 {
155 unsigned long flags;
156 bool last;
157
158 spin_lock_irqsave(&port.lock, flags);
159 last = --port.count == 0;
160 spin_unlock_irqrestore(&port.lock, flags);
161 if (last)
162 tty_port_tty_set(&port, NULL);
163 wake_up_process(bfin_jc_kthread);
164 }
165
166 /* XXX: we dont handle the put_char() case where we must handle count = 1 */
167 static int
168 bfin_jc_circ_write(const unsigned char *buf, int count)
169 {
170 int i;
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;
176 return i;
177 }
178
179 #ifndef CONFIG_BFIN_JTAG_COMM_CONSOLE
180 # define console_lock()
181 # define console_unlock()
182 #endif
183 static int
184 bfin_jc_write(struct tty_struct *tty, const unsigned char *buf, int count)
185 {
186 int i;
187 console_lock();
188 i = bfin_jc_circ_write(buf, count);
189 console_unlock();
190 wake_up_process(bfin_jc_kthread);
191 return i;
192 }
193
194 static void
195 bfin_jc_flush_chars(struct tty_struct *tty)
196 {
197 wake_up_process(bfin_jc_kthread);
198 }
199
200 static int
201 bfin_jc_write_room(struct tty_struct *tty)
202 {
203 return circ_free(&bfin_jc_write_buf);
204 }
205
206 static int
207 bfin_jc_chars_in_buffer(struct tty_struct *tty)
208 {
209 return circ_cnt(&bfin_jc_write_buf);
210 }
211
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,
220 };
221
222 static int __init bfin_jc_init(void)
223 {
224 int ret;
225
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);
229
230 ret = -ENOMEM;
231
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)
235 goto err_buf;
236
237 bfin_jc_driver = alloc_tty_driver(1);
238 if (!bfin_jc_driver)
239 goto err_driver;
240
241 tty_port_init(&port);
242
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);
250
251 ret = tty_register_driver(bfin_jc_driver);
252 if (ret)
253 goto err;
254
255 pr_init(KERN_INFO DRV_NAME ": initialized\n");
256
257 return 0;
258
259 err:
260 tty_port_destroy(&port);
261 put_tty_driver(bfin_jc_driver);
262 err_driver:
263 kfree(bfin_jc_write_buf.buf);
264 err_buf:
265 kthread_stop(bfin_jc_kthread);
266 return ret;
267 }
268 module_init(bfin_jc_init);
269
270 static void __exit bfin_jc_exit(void)
271 {
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);
277 }
278 module_exit(bfin_jc_exit);
279
280 #if defined(CONFIG_BFIN_JTAG_COMM_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
281 static void
282 bfin_jc_straight_buffer_write(const char *buf, unsigned count)
283 {
284 unsigned ate = 0;
285 while (bfin_read_DBGSTAT() & EMUDOF)
286 continue;
287 bfin_write_emudat(count);
288 while (ate < count) {
289 while (bfin_read_DBGSTAT() & EMUDOF)
290 continue;
291 bfin_write_emudat_chars(buf[ate], buf[ate+1], buf[ate+2], buf[ate+3]);
292 ate += 4;
293 }
294 }
295 #endif
296
297 #ifdef CONFIG_BFIN_JTAG_COMM_CONSOLE
298 static void
299 bfin_jc_console_write(struct console *co, const char *buf, unsigned count)
300 {
301 if (bfin_jc_kthread == NULL)
302 bfin_jc_straight_buffer_write(buf, count);
303 else
304 bfin_jc_circ_write(buf, count);
305 }
306
307 static struct tty_driver *
308 bfin_jc_console_device(struct console *co, int *index)
309 {
310 *index = co->index;
311 return bfin_jc_driver;
312 }
313
314 static struct console bfin_jc_console = {
315 .name = DEV_NAME,
316 .write = bfin_jc_console_write,
317 .device = bfin_jc_console_device,
318 .flags = CON_ANYTIME | CON_PRINTBUFFER,
319 .index = -1,
320 };
321
322 static int __init bfin_jc_console_init(void)
323 {
324 register_console(&bfin_jc_console);
325 return 0;
326 }
327 console_initcall(bfin_jc_console_init);
328 #endif
329
330 #ifdef CONFIG_EARLY_PRINTK
331 static void __init
332 bfin_jc_early_write(struct console *co, const char *buf, unsigned int count)
333 {
334 bfin_jc_straight_buffer_write(buf, count);
335 }
336
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,
341 .index = -1,
342 };
343
344 struct console * __init
345 bfin_jc_early_init(unsigned int port, unsigned int cflag)
346 {
347 return &bfin_jc_early_console;
348 }
349 #endif
350
351 MODULE_AUTHOR("Mike Frysinger <vapier@gentoo.org>");
352 MODULE_DESCRIPTION("TTY over Blackfin JTAG Communication");
353 MODULE_LICENSE("GPL");