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