]>
Commit | Line | Data |
---|---|---|
dcd83aaf TT |
1 | /* ePAPR hypervisor byte channel device driver |
2 | * | |
3 | * Copyright 2009-2011 Freescale Semiconductor, Inc. | |
4 | * | |
5 | * Author: Timur Tabi <timur@freescale.com> | |
6 | * | |
7 | * This file is licensed under the terms of the GNU General Public License | |
8 | * version 2. This program is licensed "as is" without any warranty of any | |
9 | * kind, whether express or implied. | |
10 | * | |
11 | * This driver support three distinct interfaces, all of which are related to | |
12 | * ePAPR hypervisor byte channels. | |
13 | * | |
14 | * 1) An early-console (udbg) driver. This provides early console output | |
15 | * through a byte channel. The byte channel handle must be specified in a | |
16 | * Kconfig option. | |
17 | * | |
18 | * 2) A normal console driver. Output is sent to the byte channel designated | |
19 | * for stdout in the device tree. The console driver is for handling kernel | |
20 | * printk calls. | |
21 | * | |
22 | * 3) A tty driver, which is used to handle user-space input and output. The | |
23 | * byte channel used for the console is designated as the default tty. | |
24 | */ | |
25 | ||
26 | #include <linux/module.h> | |
27 | #include <linux/init.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/err.h> | |
30 | #include <linux/interrupt.h> | |
31 | #include <linux/fs.h> | |
32 | #include <linux/poll.h> | |
33 | #include <asm/epapr_hcalls.h> | |
34 | #include <linux/of.h> | |
5af50730 | 35 | #include <linux/of_irq.h> |
dcd83aaf TT |
36 | #include <linux/platform_device.h> |
37 | #include <linux/cdev.h> | |
38 | #include <linux/console.h> | |
39 | #include <linux/tty.h> | |
40 | #include <linux/tty_flip.h> | |
41 | #include <linux/circ_buf.h> | |
42 | #include <asm/udbg.h> | |
43 | ||
44 | /* The size of the transmit circular buffer. This must be a power of two. */ | |
45 | #define BUF_SIZE 2048 | |
46 | ||
47 | /* Per-byte channel private data */ | |
48 | struct ehv_bc_data { | |
49 | struct device *dev; | |
50 | struct tty_port port; | |
51 | uint32_t handle; | |
52 | unsigned int rx_irq; | |
53 | unsigned int tx_irq; | |
54 | ||
55 | spinlock_t lock; /* lock for transmit buffer */ | |
56 | unsigned char buf[BUF_SIZE]; /* transmit circular buffer */ | |
57 | unsigned int head; /* circular buffer head */ | |
58 | unsigned int tail; /* circular buffer tail */ | |
59 | ||
60 | int tx_irq_enabled; /* true == TX interrupt is enabled */ | |
61 | }; | |
62 | ||
63 | /* Array of byte channel objects */ | |
64 | static struct ehv_bc_data *bcs; | |
65 | ||
66 | /* Byte channel handle for stdout (and stdin), taken from device tree */ | |
67 | static unsigned int stdout_bc; | |
68 | ||
69 | /* Virtual IRQ for the byte channel handle for stdin, taken from device tree */ | |
70 | static unsigned int stdout_irq; | |
71 | ||
72 | /**************************** SUPPORT FUNCTIONS ****************************/ | |
73 | ||
74 | /* | |
75 | * Enable the transmit interrupt | |
76 | * | |
77 | * Unlike a serial device, byte channels have no mechanism for disabling their | |
78 | * own receive or transmit interrupts. To emulate that feature, we toggle | |
79 | * the IRQ in the kernel. | |
80 | * | |
81 | * We cannot just blindly call enable_irq() or disable_irq(), because these | |
82 | * calls are reference counted. This means that we cannot call enable_irq() | |
83 | * if interrupts are already enabled. This can happen in two situations: | |
84 | * | |
85 | * 1. The tty layer makes two back-to-back calls to ehv_bc_tty_write() | |
86 | * 2. A transmit interrupt occurs while executing ehv_bc_tx_dequeue() | |
87 | * | |
88 | * To work around this, we keep a flag to tell us if the IRQ is enabled or not. | |
89 | */ | |
90 | static void enable_tx_interrupt(struct ehv_bc_data *bc) | |
91 | { | |
92 | if (!bc->tx_irq_enabled) { | |
93 | enable_irq(bc->tx_irq); | |
94 | bc->tx_irq_enabled = 1; | |
95 | } | |
96 | } | |
97 | ||
98 | static void disable_tx_interrupt(struct ehv_bc_data *bc) | |
99 | { | |
100 | if (bc->tx_irq_enabled) { | |
101 | disable_irq_nosync(bc->tx_irq); | |
102 | bc->tx_irq_enabled = 0; | |
103 | } | |
104 | } | |
105 | ||
106 | /* | |
107 | * find the byte channel handle to use for the console | |
108 | * | |
109 | * The byte channel to be used for the console is specified via a "stdout" | |
110 | * property in the /chosen node. | |
dcd83aaf TT |
111 | */ |
112 | static int find_console_handle(void) | |
113 | { | |
a752ee56 | 114 | struct device_node *np = of_stdout; |
dcd83aaf TT |
115 | const char *sprop = NULL; |
116 | const uint32_t *iprop; | |
117 | ||
dcd83aaf TT |
118 | /* We don't care what the aliased node is actually called. We only |
119 | * care if it's compatible with "epapr,hv-byte-channel", because that | |
a752ee56 | 120 | * indicates that it's a byte channel node. |
dcd83aaf | 121 | */ |
a752ee56 | 122 | if (!np || !of_device_is_compatible(np, "epapr,hv-byte-channel")) |
dcd83aaf | 123 | return 0; |
dcd83aaf TT |
124 | |
125 | stdout_irq = irq_of_parse_and_map(np, 0); | |
126 | if (stdout_irq == NO_IRQ) { | |
a752ee56 | 127 | pr_err("ehv-bc: no 'interrupts' property in %s node\n", np->full_name); |
dcd83aaf TT |
128 | return 0; |
129 | } | |
130 | ||
131 | /* | |
132 | * The 'hv-handle' property contains the handle for this byte channel. | |
133 | */ | |
134 | iprop = of_get_property(np, "hv-handle", NULL); | |
135 | if (!iprop) { | |
136 | pr_err("ehv-bc: no 'hv-handle' property in %s node\n", | |
137 | np->name); | |
dcd83aaf TT |
138 | return 0; |
139 | } | |
140 | stdout_bc = be32_to_cpu(*iprop); | |
dcd83aaf TT |
141 | return 1; |
142 | } | |
143 | ||
144 | /*************************** EARLY CONSOLE DRIVER ***************************/ | |
145 | ||
146 | #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC | |
147 | ||
148 | /* | |
149 | * send a byte to a byte channel, wait if necessary | |
150 | * | |
151 | * This function sends a byte to a byte channel, and it waits and | |
152 | * retries if the byte channel is full. It returns if the character | |
153 | * has been sent, or if some error has occurred. | |
154 | * | |
155 | */ | |
156 | static void byte_channel_spin_send(const char data) | |
157 | { | |
158 | int ret, count; | |
159 | ||
160 | do { | |
161 | count = 1; | |
162 | ret = ev_byte_channel_send(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE, | |
163 | &count, &data); | |
164 | } while (ret == EV_EAGAIN); | |
165 | } | |
166 | ||
167 | /* | |
168 | * The udbg subsystem calls this function to display a single character. | |
169 | * We convert CR to a CR/LF. | |
170 | */ | |
171 | static void ehv_bc_udbg_putc(char c) | |
172 | { | |
173 | if (c == '\n') | |
174 | byte_channel_spin_send('\r'); | |
175 | ||
176 | byte_channel_spin_send(c); | |
177 | } | |
178 | ||
179 | /* | |
180 | * early console initialization | |
181 | * | |
182 | * PowerPC kernels support an early printk console, also known as udbg. | |
183 | * This function must be called via the ppc_md.init_early function pointer. | |
184 | * At this point, the device tree has been unflattened, so we can obtain the | |
185 | * byte channel handle for stdout. | |
186 | * | |
187 | * We only support displaying of characters (putc). We do not support | |
188 | * keyboard input. | |
189 | */ | |
190 | void __init udbg_init_ehv_bc(void) | |
191 | { | |
192 | unsigned int rx_count, tx_count; | |
193 | unsigned int ret; | |
194 | ||
dcd83aaf TT |
195 | /* Verify the byte channel handle */ |
196 | ret = ev_byte_channel_poll(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE, | |
197 | &rx_count, &tx_count); | |
198 | if (ret) | |
199 | return; | |
200 | ||
201 | udbg_putc = ehv_bc_udbg_putc; | |
202 | register_early_udbg_console(); | |
203 | ||
204 | udbg_printf("ehv-bc: early console using byte channel handle %u\n", | |
205 | CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE); | |
206 | } | |
207 | ||
208 | #endif | |
209 | ||
210 | /****************************** CONSOLE DRIVER ******************************/ | |
211 | ||
212 | static struct tty_driver *ehv_bc_driver; | |
213 | ||
214 | /* | |
215 | * Byte channel console sending worker function. | |
216 | * | |
217 | * For consoles, if the output buffer is full, we should just spin until it | |
218 | * clears. | |
219 | */ | |
220 | static int ehv_bc_console_byte_channel_send(unsigned int handle, const char *s, | |
221 | unsigned int count) | |
222 | { | |
223 | unsigned int len; | |
224 | int ret = 0; | |
225 | ||
226 | while (count) { | |
227 | len = min_t(unsigned int, count, EV_BYTE_CHANNEL_MAX_BYTES); | |
228 | do { | |
229 | ret = ev_byte_channel_send(handle, &len, s); | |
230 | } while (ret == EV_EAGAIN); | |
231 | count -= len; | |
232 | s += len; | |
233 | } | |
234 | ||
235 | return ret; | |
236 | } | |
237 | ||
238 | /* | |
239 | * write a string to the console | |
240 | * | |
241 | * This function gets called to write a string from the kernel, typically from | |
242 | * a printk(). This function spins until all data is written. | |
243 | * | |
244 | * We copy the data to a temporary buffer because we need to insert a \r in | |
245 | * front of every \n. It's more efficient to copy the data to the buffer than | |
246 | * it is to make multiple hcalls for each character or each newline. | |
247 | */ | |
248 | static void ehv_bc_console_write(struct console *co, const char *s, | |
249 | unsigned int count) | |
250 | { | |
dcd83aaf TT |
251 | char s2[EV_BYTE_CHANNEL_MAX_BYTES]; |
252 | unsigned int i, j = 0; | |
253 | char c; | |
254 | ||
255 | for (i = 0; i < count; i++) { | |
256 | c = *s++; | |
257 | ||
258 | if (c == '\n') | |
259 | s2[j++] = '\r'; | |
260 | ||
261 | s2[j++] = c; | |
262 | if (j >= (EV_BYTE_CHANNEL_MAX_BYTES - 1)) { | |
fd01a7a1 | 263 | if (ehv_bc_console_byte_channel_send(stdout_bc, s2, j)) |
dcd83aaf TT |
264 | return; |
265 | j = 0; | |
266 | } | |
267 | } | |
268 | ||
269 | if (j) | |
fd01a7a1 | 270 | ehv_bc_console_byte_channel_send(stdout_bc, s2, j); |
dcd83aaf TT |
271 | } |
272 | ||
273 | /* | |
274 | * When /dev/console is opened, the kernel iterates the console list looking | |
275 | * for one with ->device and then calls that method. On success, it expects | |
276 | * the passed-in int* to contain the minor number to use. | |
277 | */ | |
278 | static struct tty_driver *ehv_bc_console_device(struct console *co, int *index) | |
279 | { | |
280 | *index = co->index; | |
281 | ||
282 | return ehv_bc_driver; | |
283 | } | |
284 | ||
285 | static struct console ehv_bc_console = { | |
286 | .name = "ttyEHV", | |
287 | .write = ehv_bc_console_write, | |
288 | .device = ehv_bc_console_device, | |
289 | .flags = CON_PRINTBUFFER | CON_ENABLED, | |
290 | }; | |
291 | ||
292 | /* | |
293 | * Console initialization | |
294 | * | |
295 | * This is the first function that is called after the device tree is | |
296 | * available, so here is where we determine the byte channel handle and IRQ for | |
297 | * stdout/stdin, even though that information is used by the tty and character | |
298 | * drivers. | |
299 | */ | |
300 | static int __init ehv_bc_console_init(void) | |
301 | { | |
302 | if (!find_console_handle()) { | |
303 | pr_debug("ehv-bc: stdout is not a byte channel\n"); | |
304 | return -ENODEV; | |
305 | } | |
306 | ||
307 | #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC | |
308 | /* Print a friendly warning if the user chose the wrong byte channel | |
309 | * handle for udbg. | |
310 | */ | |
311 | if (stdout_bc != CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE) | |
312 | pr_warning("ehv-bc: udbg handle %u is not the stdout handle\n", | |
313 | CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE); | |
314 | #endif | |
315 | ||
dcd83aaf TT |
316 | /* add_preferred_console() must be called before register_console(), |
317 | otherwise it won't work. However, we don't want to enumerate all the | |
318 | byte channels here, either, since we only care about one. */ | |
319 | ||
320 | add_preferred_console(ehv_bc_console.name, ehv_bc_console.index, NULL); | |
321 | register_console(&ehv_bc_console); | |
322 | ||
323 | pr_info("ehv-bc: registered console driver for byte channel %u\n", | |
324 | stdout_bc); | |
325 | ||
326 | return 0; | |
327 | } | |
328 | console_initcall(ehv_bc_console_init); | |
329 | ||
330 | /******************************** TTY DRIVER ********************************/ | |
331 | ||
332 | /* | |
333 | * byte channel receive interupt handler | |
334 | * | |
335 | * This ISR is called whenever data is available on a byte channel. | |
336 | */ | |
337 | static irqreturn_t ehv_bc_tty_rx_isr(int irq, void *data) | |
338 | { | |
339 | struct ehv_bc_data *bc = data; | |
dcd83aaf TT |
340 | unsigned int rx_count, tx_count, len; |
341 | int count; | |
342 | char buffer[EV_BYTE_CHANNEL_MAX_BYTES]; | |
343 | int ret; | |
344 | ||
dcd83aaf TT |
345 | /* Find out how much data needs to be read, and then ask the TTY layer |
346 | * if it can handle that much. We want to ensure that every byte we | |
347 | * read from the byte channel will be accepted by the TTY layer. | |
348 | */ | |
349 | ev_byte_channel_poll(bc->handle, &rx_count, &tx_count); | |
227434f8 | 350 | count = tty_buffer_request_room(&bc->port, rx_count); |
dcd83aaf TT |
351 | |
352 | /* 'count' is the maximum amount of data the TTY layer can accept at | |
353 | * this time. However, during testing, I was never able to get 'count' | |
354 | * to be less than 'rx_count'. I'm not sure whether I'm calling it | |
355 | * correctly. | |
356 | */ | |
357 | ||
358 | while (count > 0) { | |
359 | len = min_t(unsigned int, count, sizeof(buffer)); | |
360 | ||
361 | /* Read some data from the byte channel. This function will | |
362 | * never return more than EV_BYTE_CHANNEL_MAX_BYTES bytes. | |
363 | */ | |
364 | ev_byte_channel_receive(bc->handle, &len, buffer); | |
365 | ||
366 | /* 'len' is now the amount of data that's been received. 'len' | |
367 | * can't be zero, and most likely it's equal to one. | |
368 | */ | |
369 | ||
370 | /* Pass the received data to the tty layer. */ | |
05c7cd39 | 371 | ret = tty_insert_flip_string(&bc->port, buffer, len); |
dcd83aaf TT |
372 | |
373 | /* 'ret' is the number of bytes that the TTY layer accepted. | |
374 | * If it's not equal to 'len', then it means the buffer is | |
375 | * full, which should never happen. If it does happen, we can | |
376 | * exit gracefully, but we drop the last 'len - ret' characters | |
377 | * that we read from the byte channel. | |
378 | */ | |
379 | if (ret != len) | |
380 | break; | |
381 | ||
382 | count -= len; | |
383 | } | |
384 | ||
385 | /* Tell the tty layer that we're done. */ | |
2e124b4a | 386 | tty_flip_buffer_push(&bc->port); |
dcd83aaf TT |
387 | |
388 | return IRQ_HANDLED; | |
389 | } | |
390 | ||
391 | /* | |
392 | * dequeue the transmit buffer to the hypervisor | |
393 | * | |
394 | * This function, which can be called in interrupt context, dequeues as much | |
395 | * data as possible from the transmit buffer to the byte channel. | |
396 | */ | |
397 | static void ehv_bc_tx_dequeue(struct ehv_bc_data *bc) | |
398 | { | |
399 | unsigned int count; | |
400 | unsigned int len, ret; | |
401 | unsigned long flags; | |
402 | ||
403 | do { | |
404 | spin_lock_irqsave(&bc->lock, flags); | |
405 | len = min_t(unsigned int, | |
406 | CIRC_CNT_TO_END(bc->head, bc->tail, BUF_SIZE), | |
407 | EV_BYTE_CHANNEL_MAX_BYTES); | |
408 | ||
409 | ret = ev_byte_channel_send(bc->handle, &len, bc->buf + bc->tail); | |
410 | ||
411 | /* 'len' is valid only if the return code is 0 or EV_EAGAIN */ | |
412 | if (!ret || (ret == EV_EAGAIN)) | |
413 | bc->tail = (bc->tail + len) & (BUF_SIZE - 1); | |
414 | ||
415 | count = CIRC_CNT(bc->head, bc->tail, BUF_SIZE); | |
416 | spin_unlock_irqrestore(&bc->lock, flags); | |
417 | } while (count && !ret); | |
418 | ||
419 | spin_lock_irqsave(&bc->lock, flags); | |
420 | if (CIRC_CNT(bc->head, bc->tail, BUF_SIZE)) | |
421 | /* | |
422 | * If we haven't emptied the buffer, then enable the TX IRQ. | |
423 | * We'll get an interrupt when there's more room in the | |
424 | * hypervisor's output buffer. | |
425 | */ | |
426 | enable_tx_interrupt(bc); | |
427 | else | |
428 | disable_tx_interrupt(bc); | |
429 | spin_unlock_irqrestore(&bc->lock, flags); | |
430 | } | |
431 | ||
432 | /* | |
433 | * byte channel transmit interupt handler | |
434 | * | |
435 | * This ISR is called whenever space becomes available for transmitting | |
436 | * characters on a byte channel. | |
437 | */ | |
438 | static irqreturn_t ehv_bc_tty_tx_isr(int irq, void *data) | |
439 | { | |
440 | struct ehv_bc_data *bc = data; | |
dcd83aaf TT |
441 | |
442 | ehv_bc_tx_dequeue(bc); | |
6aad04f2 | 443 | tty_port_tty_wakeup(&bc->port); |
dcd83aaf TT |
444 | |
445 | return IRQ_HANDLED; | |
446 | } | |
447 | ||
448 | /* | |
449 | * This function is called when the tty layer has data for us send. We store | |
450 | * the data first in a circular buffer, and then dequeue as much of that data | |
451 | * as possible. | |
452 | * | |
453 | * We don't need to worry about whether there is enough room in the buffer for | |
454 | * all the data. The purpose of ehv_bc_tty_write_room() is to tell the tty | |
455 | * layer how much data it can safely send to us. We guarantee that | |
456 | * ehv_bc_tty_write_room() will never lie, so the tty layer will never send us | |
457 | * too much data. | |
458 | */ | |
459 | static int ehv_bc_tty_write(struct tty_struct *ttys, const unsigned char *s, | |
460 | int count) | |
461 | { | |
462 | struct ehv_bc_data *bc = ttys->driver_data; | |
463 | unsigned long flags; | |
464 | unsigned int len; | |
465 | unsigned int written = 0; | |
466 | ||
467 | while (1) { | |
468 | spin_lock_irqsave(&bc->lock, flags); | |
469 | len = CIRC_SPACE_TO_END(bc->head, bc->tail, BUF_SIZE); | |
470 | if (count < len) | |
471 | len = count; | |
472 | if (len) { | |
473 | memcpy(bc->buf + bc->head, s, len); | |
474 | bc->head = (bc->head + len) & (BUF_SIZE - 1); | |
475 | } | |
476 | spin_unlock_irqrestore(&bc->lock, flags); | |
477 | if (!len) | |
478 | break; | |
479 | ||
480 | s += len; | |
481 | count -= len; | |
482 | written += len; | |
483 | } | |
484 | ||
485 | ehv_bc_tx_dequeue(bc); | |
486 | ||
487 | return written; | |
488 | } | |
489 | ||
490 | /* | |
491 | * This function can be called multiple times for a given tty_struct, which is | |
492 | * why we initialize bc->ttys in ehv_bc_tty_port_activate() instead. | |
493 | * | |
494 | * The tty layer will still call this function even if the device was not | |
495 | * registered (i.e. tty_register_device() was not called). This happens | |
496 | * because tty_register_device() is optional and some legacy drivers don't | |
497 | * use it. So we need to check for that. | |
498 | */ | |
499 | static int ehv_bc_tty_open(struct tty_struct *ttys, struct file *filp) | |
500 | { | |
501 | struct ehv_bc_data *bc = &bcs[ttys->index]; | |
502 | ||
503 | if (!bc->dev) | |
504 | return -ENODEV; | |
505 | ||
506 | return tty_port_open(&bc->port, ttys, filp); | |
507 | } | |
508 | ||
509 | /* | |
510 | * Amazingly, if ehv_bc_tty_open() returns an error code, the tty layer will | |
511 | * still call this function to close the tty device. So we can't assume that | |
512 | * the tty port has been initialized. | |
513 | */ | |
514 | static void ehv_bc_tty_close(struct tty_struct *ttys, struct file *filp) | |
515 | { | |
516 | struct ehv_bc_data *bc = &bcs[ttys->index]; | |
517 | ||
518 | if (bc->dev) | |
519 | tty_port_close(&bc->port, ttys, filp); | |
520 | } | |
521 | ||
522 | /* | |
523 | * Return the amount of space in the output buffer | |
524 | * | |
525 | * This is actually a contract between the driver and the tty layer outlining | |
526 | * how much write room the driver can guarantee will be sent OR BUFFERED. This | |
527 | * driver MUST honor the return value. | |
528 | */ | |
529 | static int ehv_bc_tty_write_room(struct tty_struct *ttys) | |
530 | { | |
531 | struct ehv_bc_data *bc = ttys->driver_data; | |
532 | unsigned long flags; | |
533 | int count; | |
534 | ||
535 | spin_lock_irqsave(&bc->lock, flags); | |
536 | count = CIRC_SPACE(bc->head, bc->tail, BUF_SIZE); | |
537 | spin_unlock_irqrestore(&bc->lock, flags); | |
538 | ||
539 | return count; | |
540 | } | |
541 | ||
542 | /* | |
543 | * Stop sending data to the tty layer | |
544 | * | |
545 | * This function is called when the tty layer's input buffers are getting full, | |
546 | * so the driver should stop sending it data. The easiest way to do this is to | |
547 | * disable the RX IRQ, which will prevent ehv_bc_tty_rx_isr() from being | |
548 | * called. | |
549 | * | |
550 | * The hypervisor will continue to queue up any incoming data. If there is any | |
551 | * data in the queue when the RX interrupt is enabled, we'll immediately get an | |
552 | * RX interrupt. | |
553 | */ | |
554 | static void ehv_bc_tty_throttle(struct tty_struct *ttys) | |
555 | { | |
556 | struct ehv_bc_data *bc = ttys->driver_data; | |
557 | ||
558 | disable_irq(bc->rx_irq); | |
559 | } | |
560 | ||
561 | /* | |
562 | * Resume sending data to the tty layer | |
563 | * | |
564 | * This function is called after previously calling ehv_bc_tty_throttle(). The | |
565 | * tty layer's input buffers now have more room, so the driver can resume | |
566 | * sending it data. | |
567 | */ | |
568 | static void ehv_bc_tty_unthrottle(struct tty_struct *ttys) | |
569 | { | |
570 | struct ehv_bc_data *bc = ttys->driver_data; | |
571 | ||
572 | /* If there is any data in the queue when the RX interrupt is enabled, | |
573 | * we'll immediately get an RX interrupt. | |
574 | */ | |
575 | enable_irq(bc->rx_irq); | |
576 | } | |
577 | ||
578 | static void ehv_bc_tty_hangup(struct tty_struct *ttys) | |
579 | { | |
580 | struct ehv_bc_data *bc = ttys->driver_data; | |
581 | ||
582 | ehv_bc_tx_dequeue(bc); | |
583 | tty_port_hangup(&bc->port); | |
584 | } | |
585 | ||
586 | /* | |
587 | * TTY driver operations | |
588 | * | |
589 | * If we could ask the hypervisor how much data is still in the TX buffer, or | |
590 | * at least how big the TX buffers are, then we could implement the | |
591 | * .wait_until_sent and .chars_in_buffer functions. | |
592 | */ | |
593 | static const struct tty_operations ehv_bc_ops = { | |
594 | .open = ehv_bc_tty_open, | |
595 | .close = ehv_bc_tty_close, | |
596 | .write = ehv_bc_tty_write, | |
597 | .write_room = ehv_bc_tty_write_room, | |
598 | .throttle = ehv_bc_tty_throttle, | |
599 | .unthrottle = ehv_bc_tty_unthrottle, | |
600 | .hangup = ehv_bc_tty_hangup, | |
601 | }; | |
602 | ||
603 | /* | |
604 | * initialize the TTY port | |
605 | * | |
606 | * This function will only be called once, no matter how many times | |
607 | * ehv_bc_tty_open() is called. That's why we register the ISR here, and also | |
608 | * why we initialize tty_struct-related variables here. | |
609 | */ | |
610 | static int ehv_bc_tty_port_activate(struct tty_port *port, | |
611 | struct tty_struct *ttys) | |
612 | { | |
613 | struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port); | |
614 | int ret; | |
615 | ||
616 | ttys->driver_data = bc; | |
617 | ||
618 | ret = request_irq(bc->rx_irq, ehv_bc_tty_rx_isr, 0, "ehv-bc", bc); | |
619 | if (ret < 0) { | |
620 | dev_err(bc->dev, "could not request rx irq %u (ret=%i)\n", | |
621 | bc->rx_irq, ret); | |
622 | return ret; | |
623 | } | |
624 | ||
625 | /* request_irq also enables the IRQ */ | |
626 | bc->tx_irq_enabled = 1; | |
627 | ||
628 | ret = request_irq(bc->tx_irq, ehv_bc_tty_tx_isr, 0, "ehv-bc", bc); | |
629 | if (ret < 0) { | |
630 | dev_err(bc->dev, "could not request tx irq %u (ret=%i)\n", | |
631 | bc->tx_irq, ret); | |
632 | free_irq(bc->rx_irq, bc); | |
633 | return ret; | |
634 | } | |
635 | ||
636 | /* The TX IRQ is enabled only when we can't write all the data to the | |
637 | * byte channel at once, so by default it's disabled. | |
638 | */ | |
639 | disable_tx_interrupt(bc); | |
640 | ||
641 | return 0; | |
642 | } | |
643 | ||
644 | static void ehv_bc_tty_port_shutdown(struct tty_port *port) | |
645 | { | |
646 | struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port); | |
647 | ||
648 | free_irq(bc->tx_irq, bc); | |
649 | free_irq(bc->rx_irq, bc); | |
650 | } | |
651 | ||
652 | static const struct tty_port_operations ehv_bc_tty_port_ops = { | |
653 | .activate = ehv_bc_tty_port_activate, | |
654 | .shutdown = ehv_bc_tty_port_shutdown, | |
655 | }; | |
656 | ||
9671f099 | 657 | static int ehv_bc_tty_probe(struct platform_device *pdev) |
dcd83aaf TT |
658 | { |
659 | struct device_node *np = pdev->dev.of_node; | |
660 | struct ehv_bc_data *bc; | |
661 | const uint32_t *iprop; | |
662 | unsigned int handle; | |
663 | int ret; | |
664 | static unsigned int index = 1; | |
665 | unsigned int i; | |
666 | ||
667 | iprop = of_get_property(np, "hv-handle", NULL); | |
668 | if (!iprop) { | |
669 | dev_err(&pdev->dev, "no 'hv-handle' property in %s node\n", | |
670 | np->name); | |
671 | return -ENODEV; | |
672 | } | |
673 | ||
674 | /* We already told the console layer that the index for the console | |
675 | * device is zero, so we need to make sure that we use that index when | |
676 | * we probe the console byte channel node. | |
677 | */ | |
678 | handle = be32_to_cpu(*iprop); | |
679 | i = (handle == stdout_bc) ? 0 : index++; | |
680 | bc = &bcs[i]; | |
681 | ||
682 | bc->handle = handle; | |
683 | bc->head = 0; | |
684 | bc->tail = 0; | |
685 | spin_lock_init(&bc->lock); | |
686 | ||
687 | bc->rx_irq = irq_of_parse_and_map(np, 0); | |
688 | bc->tx_irq = irq_of_parse_and_map(np, 1); | |
689 | if ((bc->rx_irq == NO_IRQ) || (bc->tx_irq == NO_IRQ)) { | |
690 | dev_err(&pdev->dev, "no 'interrupts' property in %s node\n", | |
691 | np->name); | |
692 | ret = -ENODEV; | |
693 | goto error; | |
694 | } | |
695 | ||
734cc178 JS |
696 | tty_port_init(&bc->port); |
697 | bc->port.ops = &ehv_bc_tty_port_ops; | |
698 | ||
699 | bc->dev = tty_port_register_device(&bc->port, ehv_bc_driver, i, | |
700 | &pdev->dev); | |
dcd83aaf TT |
701 | if (IS_ERR(bc->dev)) { |
702 | ret = PTR_ERR(bc->dev); | |
703 | dev_err(&pdev->dev, "could not register tty (ret=%i)\n", ret); | |
704 | goto error; | |
705 | } | |
706 | ||
dcd83aaf TT |
707 | dev_set_drvdata(&pdev->dev, bc); |
708 | ||
709 | dev_info(&pdev->dev, "registered /dev/%s%u for byte channel %u\n", | |
710 | ehv_bc_driver->name, i, bc->handle); | |
711 | ||
712 | return 0; | |
713 | ||
714 | error: | |
191c5f10 | 715 | tty_port_destroy(&bc->port); |
dcd83aaf TT |
716 | irq_dispose_mapping(bc->tx_irq); |
717 | irq_dispose_mapping(bc->rx_irq); | |
718 | ||
719 | memset(bc, 0, sizeof(struct ehv_bc_data)); | |
720 | return ret; | |
721 | } | |
722 | ||
723 | static int ehv_bc_tty_remove(struct platform_device *pdev) | |
724 | { | |
725 | struct ehv_bc_data *bc = dev_get_drvdata(&pdev->dev); | |
726 | ||
727 | tty_unregister_device(ehv_bc_driver, bc - bcs); | |
728 | ||
191c5f10 | 729 | tty_port_destroy(&bc->port); |
dcd83aaf TT |
730 | irq_dispose_mapping(bc->tx_irq); |
731 | irq_dispose_mapping(bc->rx_irq); | |
732 | ||
733 | return 0; | |
734 | } | |
735 | ||
736 | static const struct of_device_id ehv_bc_tty_of_ids[] = { | |
737 | { .compatible = "epapr,hv-byte-channel" }, | |
738 | {} | |
739 | }; | |
740 | ||
741 | static struct platform_driver ehv_bc_tty_driver = { | |
742 | .driver = { | |
743 | .owner = THIS_MODULE, | |
744 | .name = "ehv-bc", | |
745 | .of_match_table = ehv_bc_tty_of_ids, | |
746 | }, | |
747 | .probe = ehv_bc_tty_probe, | |
748 | .remove = ehv_bc_tty_remove, | |
749 | }; | |
750 | ||
751 | /** | |
752 | * ehv_bc_init - ePAPR hypervisor byte channel driver initialization | |
753 | * | |
754 | * This function is called when this module is loaded. | |
755 | */ | |
756 | static int __init ehv_bc_init(void) | |
757 | { | |
758 | struct device_node *np; | |
759 | unsigned int count = 0; /* Number of elements in bcs[] */ | |
760 | int ret; | |
761 | ||
762 | pr_info("ePAPR hypervisor byte channel driver\n"); | |
763 | ||
764 | /* Count the number of byte channels */ | |
765 | for_each_compatible_node(np, NULL, "epapr,hv-byte-channel") | |
766 | count++; | |
767 | ||
768 | if (!count) | |
769 | return -ENODEV; | |
770 | ||
771 | /* The array index of an element in bcs[] is the same as the tty index | |
772 | * for that element. If you know the address of an element in the | |
773 | * array, then you can use pointer math (e.g. "bc - bcs") to get its | |
774 | * tty index. | |
775 | */ | |
776 | bcs = kzalloc(count * sizeof(struct ehv_bc_data), GFP_KERNEL); | |
777 | if (!bcs) | |
778 | return -ENOMEM; | |
779 | ||
780 | ehv_bc_driver = alloc_tty_driver(count); | |
781 | if (!ehv_bc_driver) { | |
782 | ret = -ENOMEM; | |
783 | goto error; | |
784 | } | |
785 | ||
dcd83aaf TT |
786 | ehv_bc_driver->driver_name = "ehv-bc"; |
787 | ehv_bc_driver->name = ehv_bc_console.name; | |
788 | ehv_bc_driver->type = TTY_DRIVER_TYPE_CONSOLE; | |
789 | ehv_bc_driver->subtype = SYSTEM_TYPE_CONSOLE; | |
790 | ehv_bc_driver->init_termios = tty_std_termios; | |
791 | ehv_bc_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; | |
792 | tty_set_operations(ehv_bc_driver, &ehv_bc_ops); | |
793 | ||
794 | ret = tty_register_driver(ehv_bc_driver); | |
795 | if (ret) { | |
796 | pr_err("ehv-bc: could not register tty driver (ret=%i)\n", ret); | |
797 | goto error; | |
798 | } | |
799 | ||
800 | ret = platform_driver_register(&ehv_bc_tty_driver); | |
801 | if (ret) { | |
802 | pr_err("ehv-bc: could not register platform driver (ret=%i)\n", | |
803 | ret); | |
804 | goto error; | |
805 | } | |
806 | ||
807 | return 0; | |
808 | ||
809 | error: | |
810 | if (ehv_bc_driver) { | |
811 | tty_unregister_driver(ehv_bc_driver); | |
812 | put_tty_driver(ehv_bc_driver); | |
813 | } | |
814 | ||
815 | kfree(bcs); | |
816 | ||
817 | return ret; | |
818 | } | |
819 | ||
820 | ||
821 | /** | |
822 | * ehv_bc_exit - ePAPR hypervisor byte channel driver termination | |
823 | * | |
824 | * This function is called when this driver is unloaded. | |
825 | */ | |
826 | static void __exit ehv_bc_exit(void) | |
827 | { | |
df957d2b | 828 | platform_driver_unregister(&ehv_bc_tty_driver); |
dcd83aaf TT |
829 | tty_unregister_driver(ehv_bc_driver); |
830 | put_tty_driver(ehv_bc_driver); | |
831 | kfree(bcs); | |
832 | } | |
833 | ||
834 | module_init(ehv_bc_init); | |
835 | module_exit(ehv_bc_exit); | |
836 | ||
837 | MODULE_AUTHOR("Timur Tabi <timur@freescale.com>"); | |
838 | MODULE_DESCRIPTION("ePAPR hypervisor byte channel driver"); | |
839 | MODULE_LICENSE("GPL v2"); |