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