2 * Copyright (C) 2007 Google, Inc.
3 * Copyright (C) 2012 Intel, Inc.
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <linux/console.h>
17 #include <linux/init.h>
18 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
20 #include <linux/tty.h>
21 #include <linux/tty_flip.h>
22 #include <linux/slab.h>
24 #include <linux/module.h>
27 GOLDFISH_TTY_PUT_CHAR
= 0x00,
28 GOLDFISH_TTY_BYTES_READY
= 0x04,
29 GOLDFISH_TTY_CMD
= 0x08,
31 GOLDFISH_TTY_DATA_PTR
= 0x10,
32 GOLDFISH_TTY_DATA_LEN
= 0x14,
34 GOLDFISH_TTY_CMD_INT_DISABLE
= 0,
35 GOLDFISH_TTY_CMD_INT_ENABLE
= 1,
36 GOLDFISH_TTY_CMD_WRITE_BUFFER
= 2,
37 GOLDFISH_TTY_CMD_READ_BUFFER
= 3,
46 struct console console
;
49 static DEFINE_MUTEX(goldfish_tty_lock
);
50 static struct tty_driver
*goldfish_tty_driver
;
51 static u32 goldfish_tty_line_count
= 8;
52 static u32 goldfish_tty_current_line_count
;
53 static struct goldfish_tty
*goldfish_ttys
;
55 static void goldfish_tty_do_write(int line
, const char *buf
, unsigned count
)
57 unsigned long irq_flags
;
58 struct goldfish_tty
*qtty
= &goldfish_ttys
[line
];
59 void __iomem
*base
= qtty
->base
;
60 spin_lock_irqsave(&qtty
->lock
, irq_flags
);
61 writel((u32
)buf
, base
+ GOLDFISH_TTY_DATA_PTR
);
62 writel(count
, base
+ GOLDFISH_TTY_DATA_LEN
);
63 writel(GOLDFISH_TTY_CMD_WRITE_BUFFER
, base
+ GOLDFISH_TTY_CMD
);
64 spin_unlock_irqrestore(&qtty
->lock
, irq_flags
);
67 static irqreturn_t
goldfish_tty_interrupt(int irq
, void *dev_id
)
69 struct platform_device
*pdev
= dev_id
;
70 struct goldfish_tty
*qtty
= &goldfish_ttys
[pdev
->id
];
71 void __iomem
*base
= qtty
->base
;
72 unsigned long irq_flags
;
76 count
= readl(base
+ GOLDFISH_TTY_BYTES_READY
);
80 count
= tty_prepare_flip_string(&qtty
->port
, &buf
, count
);
81 spin_lock_irqsave(&qtty
->lock
, irq_flags
);
82 writel((u32
)buf
, base
+ GOLDFISH_TTY_DATA_PTR
);
83 writel(count
, base
+ GOLDFISH_TTY_DATA_LEN
);
84 writel(GOLDFISH_TTY_CMD_READ_BUFFER
, base
+ GOLDFISH_TTY_CMD
);
85 spin_unlock_irqrestore(&qtty
->lock
, irq_flags
);
86 tty_schedule_flip(&qtty
->port
);
90 static int goldfish_tty_activate(struct tty_port
*port
, struct tty_struct
*tty
)
92 struct goldfish_tty
*qtty
= container_of(port
, struct goldfish_tty
, port
);
93 writel(GOLDFISH_TTY_CMD_INT_ENABLE
, qtty
->base
+ GOLDFISH_TTY_CMD
);
97 static void goldfish_tty_shutdown(struct tty_port
*port
)
99 struct goldfish_tty
*qtty
= container_of(port
, struct goldfish_tty
, port
);
100 writel(GOLDFISH_TTY_CMD_INT_DISABLE
, qtty
->base
+ GOLDFISH_TTY_CMD
);
103 static int goldfish_tty_open(struct tty_struct
* tty
, struct file
* filp
)
105 struct goldfish_tty
*qtty
= &goldfish_ttys
[tty
->index
];
106 return tty_port_open(&qtty
->port
, tty
, filp
);
109 static void goldfish_tty_close(struct tty_struct
* tty
, struct file
* filp
)
111 tty_port_close(tty
->port
, tty
, filp
);
114 static void goldfish_tty_hangup(struct tty_struct
*tty
)
116 tty_port_hangup(tty
->port
);
119 static int goldfish_tty_write(struct tty_struct
* tty
, const unsigned char *buf
, int count
)
121 goldfish_tty_do_write(tty
->index
, buf
, count
);
125 static int goldfish_tty_write_room(struct tty_struct
*tty
)
130 static int goldfish_tty_chars_in_buffer(struct tty_struct
*tty
)
132 struct goldfish_tty
*qtty
= &goldfish_ttys
[tty
->index
];
133 void __iomem
*base
= qtty
->base
;
134 return readl(base
+ GOLDFISH_TTY_BYTES_READY
);
137 static void goldfish_tty_console_write(struct console
*co
, const char *b
, unsigned count
)
139 goldfish_tty_do_write(co
->index
, b
, count
);
142 static struct tty_driver
*goldfish_tty_console_device(struct console
*c
, int *index
)
145 return goldfish_tty_driver
;
148 static int goldfish_tty_console_setup(struct console
*co
, char *options
)
150 if((unsigned)co
->index
> goldfish_tty_line_count
)
152 if(goldfish_ttys
[co
->index
].base
== 0)
157 static struct tty_port_operations goldfish_port_ops
= {
158 .activate
= goldfish_tty_activate
,
159 .shutdown
= goldfish_tty_shutdown
162 static struct tty_operations goldfish_tty_ops
= {
163 .open
= goldfish_tty_open
,
164 .close
= goldfish_tty_close
,
165 .hangup
= goldfish_tty_hangup
,
166 .write
= goldfish_tty_write
,
167 .write_room
= goldfish_tty_write_room
,
168 .chars_in_buffer
= goldfish_tty_chars_in_buffer
,
171 static int goldfish_tty_create_driver(void)
174 struct tty_driver
*tty
;
176 goldfish_ttys
= kzalloc(sizeof(*goldfish_ttys
) * goldfish_tty_line_count
, GFP_KERNEL
);
177 if(goldfish_ttys
== NULL
) {
179 goto err_alloc_goldfish_ttys_failed
;
181 tty
= alloc_tty_driver(goldfish_tty_line_count
);
184 goto err_alloc_tty_driver_failed
;
186 tty
->driver_name
= "goldfish";
188 tty
->type
= TTY_DRIVER_TYPE_SERIAL
;
189 tty
->subtype
= SERIAL_TYPE_NORMAL
;
190 tty
->init_termios
= tty_std_termios
;
191 tty
->flags
= TTY_DRIVER_RESET_TERMIOS
| TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
192 tty_set_operations(tty
, &goldfish_tty_ops
);
193 ret
= tty_register_driver(tty
);
195 goto err_tty_register_driver_failed
;
197 goldfish_tty_driver
= tty
;
200 err_tty_register_driver_failed
:
202 err_alloc_tty_driver_failed
:
203 kfree(goldfish_ttys
);
204 goldfish_ttys
= NULL
;
205 err_alloc_goldfish_ttys_failed
:
209 static void goldfish_tty_delete_driver(void)
211 tty_unregister_driver(goldfish_tty_driver
);
212 put_tty_driver(goldfish_tty_driver
);
213 goldfish_tty_driver
= NULL
;
214 kfree(goldfish_ttys
);
215 goldfish_ttys
= NULL
;
218 static int goldfish_tty_probe(struct platform_device
*pdev
)
220 struct goldfish_tty
*qtty
;
224 struct device
*ttydev
;
228 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
232 base
= ioremap(r
->start
, 0x1000);
234 pr_err("goldfish_tty: unable to remap base\n");
236 r
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
242 if(pdev
->id
>= goldfish_tty_line_count
)
245 mutex_lock(&goldfish_tty_lock
);
246 if(goldfish_tty_current_line_count
== 0) {
247 ret
= goldfish_tty_create_driver();
249 goto err_create_driver_failed
;
251 goldfish_tty_current_line_count
++;
253 qtty
= &goldfish_ttys
[pdev
->id
];
254 spin_lock_init(&qtty
->lock
);
255 tty_port_init(&qtty
->port
);
256 qtty
->port
.ops
= &goldfish_port_ops
;
260 writel(GOLDFISH_TTY_CMD_INT_DISABLE
, base
+ GOLDFISH_TTY_CMD
);
262 ret
= request_irq(irq
, goldfish_tty_interrupt
, IRQF_SHARED
, "goldfish_tty", pdev
);
264 goto err_request_irq_failed
;
267 ttydev
= tty_port_register_device(&qtty
->port
, goldfish_tty_driver
,
268 pdev
->id
, &pdev
->dev
);
270 ret
= PTR_ERR(ttydev
);
271 goto err_tty_register_device_failed
;
274 strcpy(qtty
->console
.name
, "ttyGF");
275 qtty
->console
.write
= goldfish_tty_console_write
;
276 qtty
->console
.device
= goldfish_tty_console_device
;
277 qtty
->console
.setup
= goldfish_tty_console_setup
;
278 qtty
->console
.flags
= CON_PRINTBUFFER
;
279 qtty
->console
.index
= pdev
->id
;
280 register_console(&qtty
->console
);
282 mutex_unlock(&goldfish_tty_lock
);
285 tty_unregister_device(goldfish_tty_driver
, i
);
286 err_tty_register_device_failed
:
288 err_request_irq_failed
:
289 goldfish_tty_current_line_count
--;
290 if(goldfish_tty_current_line_count
== 0)
291 goldfish_tty_delete_driver();
292 err_create_driver_failed
:
293 mutex_unlock(&goldfish_tty_lock
);
299 static int goldfish_tty_remove(struct platform_device
*pdev
)
301 struct goldfish_tty
*qtty
;
303 mutex_lock(&goldfish_tty_lock
);
305 qtty
= &goldfish_ttys
[pdev
->id
];
306 unregister_console(&qtty
->console
);
307 tty_unregister_device(goldfish_tty_driver
, pdev
->id
);
310 free_irq(qtty
->irq
, pdev
);
311 goldfish_tty_current_line_count
--;
312 if(goldfish_tty_current_line_count
== 0)
313 goldfish_tty_delete_driver();
314 mutex_unlock(&goldfish_tty_lock
);
318 static struct platform_driver goldfish_tty_platform_driver
= {
319 .probe
= goldfish_tty_probe
,
320 .remove
= goldfish_tty_remove
,
322 .name
= "goldfish_tty"
326 module_platform_driver(goldfish_tty_platform_driver
);
328 MODULE_LICENSE("GPL v2");