kernel/printk: Convert to hotplug state machine
[linux/fpc-iii.git] / drivers / tty / goldfish.c
blob3fc912373adf236c6d7a4060608ccc8c78617fd5
1 /*
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/interrupt.h>
18 #include <linux/platform_device.h>
19 #include <linux/tty.h>
20 #include <linux/tty_flip.h>
21 #include <linux/slab.h>
22 #include <linux/io.h>
23 #include <linux/module.h>
24 #include <linux/goldfish.h>
26 enum {
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,
33 GOLDFISH_TTY_DATA_PTR_HIGH = 0x18,
35 GOLDFISH_TTY_CMD_INT_DISABLE = 0,
36 GOLDFISH_TTY_CMD_INT_ENABLE = 1,
37 GOLDFISH_TTY_CMD_WRITE_BUFFER = 2,
38 GOLDFISH_TTY_CMD_READ_BUFFER = 3,
41 struct goldfish_tty {
42 struct tty_port port;
43 spinlock_t lock;
44 void __iomem *base;
45 u32 irq;
46 int opencount;
47 struct console console;
50 static DEFINE_MUTEX(goldfish_tty_lock);
51 static struct tty_driver *goldfish_tty_driver;
52 static u32 goldfish_tty_line_count = 8;
53 static u32 goldfish_tty_current_line_count;
54 static struct goldfish_tty *goldfish_ttys;
56 static void goldfish_tty_do_write(int line, const char *buf, unsigned count)
58 unsigned long irq_flags;
59 struct goldfish_tty *qtty = &goldfish_ttys[line];
60 void __iomem *base = qtty->base;
61 spin_lock_irqsave(&qtty->lock, irq_flags);
62 gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR,
63 base + GOLDFISH_TTY_DATA_PTR_HIGH);
64 writel(count, base + GOLDFISH_TTY_DATA_LEN);
65 writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_CMD);
66 spin_unlock_irqrestore(&qtty->lock, irq_flags);
69 static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
71 struct goldfish_tty *qtty = dev_id;
72 void __iomem *base = qtty->base;
73 unsigned long irq_flags;
74 unsigned char *buf;
75 u32 count;
77 count = readl(base + GOLDFISH_TTY_BYTES_READY);
78 if (count == 0)
79 return IRQ_NONE;
81 count = tty_prepare_flip_string(&qtty->port, &buf, count);
82 spin_lock_irqsave(&qtty->lock, irq_flags);
83 gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR,
84 base + GOLDFISH_TTY_DATA_PTR_HIGH);
85 writel(count, base + GOLDFISH_TTY_DATA_LEN);
86 writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_CMD);
87 spin_unlock_irqrestore(&qtty->lock, irq_flags);
88 tty_schedule_flip(&qtty->port);
89 return IRQ_HANDLED;
92 static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
94 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
95 port);
96 writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_CMD);
97 return 0;
100 static void goldfish_tty_shutdown(struct tty_port *port)
102 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
103 port);
104 writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_CMD);
107 static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
109 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
110 return tty_port_open(&qtty->port, tty, filp);
113 static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
115 tty_port_close(tty->port, tty, filp);
118 static void goldfish_tty_hangup(struct tty_struct *tty)
120 tty_port_hangup(tty->port);
123 static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
124 int count)
126 goldfish_tty_do_write(tty->index, buf, count);
127 return count;
130 static int goldfish_tty_write_room(struct tty_struct *tty)
132 return 0x10000;
135 static int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
137 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
138 void __iomem *base = qtty->base;
139 return readl(base + GOLDFISH_TTY_BYTES_READY);
142 static void goldfish_tty_console_write(struct console *co, const char *b,
143 unsigned count)
145 goldfish_tty_do_write(co->index, b, count);
148 static struct tty_driver *goldfish_tty_console_device(struct console *c,
149 int *index)
151 *index = c->index;
152 return goldfish_tty_driver;
155 static int goldfish_tty_console_setup(struct console *co, char *options)
157 if ((unsigned)co->index >= goldfish_tty_line_count)
158 return -ENODEV;
159 if (!goldfish_ttys[co->index].base)
160 return -ENODEV;
161 return 0;
164 static const struct tty_port_operations goldfish_port_ops = {
165 .activate = goldfish_tty_activate,
166 .shutdown = goldfish_tty_shutdown
169 static const struct tty_operations goldfish_tty_ops = {
170 .open = goldfish_tty_open,
171 .close = goldfish_tty_close,
172 .hangup = goldfish_tty_hangup,
173 .write = goldfish_tty_write,
174 .write_room = goldfish_tty_write_room,
175 .chars_in_buffer = goldfish_tty_chars_in_buffer,
178 static int goldfish_tty_create_driver(void)
180 int ret;
181 struct tty_driver *tty;
183 goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) *
184 goldfish_tty_line_count, GFP_KERNEL);
185 if (goldfish_ttys == NULL) {
186 ret = -ENOMEM;
187 goto err_alloc_goldfish_ttys_failed;
189 tty = alloc_tty_driver(goldfish_tty_line_count);
190 if (tty == NULL) {
191 ret = -ENOMEM;
192 goto err_alloc_tty_driver_failed;
194 tty->driver_name = "goldfish";
195 tty->name = "ttyGF";
196 tty->type = TTY_DRIVER_TYPE_SERIAL;
197 tty->subtype = SERIAL_TYPE_NORMAL;
198 tty->init_termios = tty_std_termios;
199 tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
200 TTY_DRIVER_DYNAMIC_DEV;
201 tty_set_operations(tty, &goldfish_tty_ops);
202 ret = tty_register_driver(tty);
203 if (ret)
204 goto err_tty_register_driver_failed;
206 goldfish_tty_driver = tty;
207 return 0;
209 err_tty_register_driver_failed:
210 put_tty_driver(tty);
211 err_alloc_tty_driver_failed:
212 kfree(goldfish_ttys);
213 goldfish_ttys = NULL;
214 err_alloc_goldfish_ttys_failed:
215 return ret;
218 static void goldfish_tty_delete_driver(void)
220 tty_unregister_driver(goldfish_tty_driver);
221 put_tty_driver(goldfish_tty_driver);
222 goldfish_tty_driver = NULL;
223 kfree(goldfish_ttys);
224 goldfish_ttys = NULL;
227 static int goldfish_tty_probe(struct platform_device *pdev)
229 struct goldfish_tty *qtty;
230 int ret = -EINVAL;
231 struct resource *r;
232 struct device *ttydev;
233 void __iomem *base;
234 u32 irq;
235 unsigned int line;
237 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
238 if (r == NULL)
239 return -EINVAL;
241 base = ioremap(r->start, 0x1000);
242 if (base == NULL)
243 pr_err("goldfish_tty: unable to remap base\n");
245 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
246 if (r == NULL)
247 goto err_unmap;
249 irq = r->start;
251 mutex_lock(&goldfish_tty_lock);
253 if (pdev->id == PLATFORM_DEVID_NONE)
254 line = goldfish_tty_current_line_count;
255 else
256 line = pdev->id;
258 if (line >= goldfish_tty_line_count)
259 goto err_create_driver_failed;
261 if (goldfish_tty_current_line_count == 0) {
262 ret = goldfish_tty_create_driver();
263 if (ret)
264 goto err_create_driver_failed;
266 goldfish_tty_current_line_count++;
268 qtty = &goldfish_ttys[line];
269 spin_lock_init(&qtty->lock);
270 tty_port_init(&qtty->port);
271 qtty->port.ops = &goldfish_port_ops;
272 qtty->base = base;
273 qtty->irq = irq;
275 writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_CMD);
277 ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
278 "goldfish_tty", qtty);
279 if (ret)
280 goto err_request_irq_failed;
283 ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
284 line, &pdev->dev);
285 if (IS_ERR(ttydev)) {
286 ret = PTR_ERR(ttydev);
287 goto err_tty_register_device_failed;
290 strcpy(qtty->console.name, "ttyGF");
291 qtty->console.write = goldfish_tty_console_write;
292 qtty->console.device = goldfish_tty_console_device;
293 qtty->console.setup = goldfish_tty_console_setup;
294 qtty->console.flags = CON_PRINTBUFFER;
295 qtty->console.index = line;
296 register_console(&qtty->console);
297 platform_set_drvdata(pdev, qtty);
299 mutex_unlock(&goldfish_tty_lock);
300 return 0;
302 err_tty_register_device_failed:
303 free_irq(irq, pdev);
304 err_request_irq_failed:
305 goldfish_tty_current_line_count--;
306 if (goldfish_tty_current_line_count == 0)
307 goldfish_tty_delete_driver();
308 err_create_driver_failed:
309 mutex_unlock(&goldfish_tty_lock);
310 err_unmap:
311 iounmap(base);
312 return ret;
315 static int goldfish_tty_remove(struct platform_device *pdev)
317 struct goldfish_tty *qtty = platform_get_drvdata(pdev);
319 mutex_lock(&goldfish_tty_lock);
321 unregister_console(&qtty->console);
322 tty_unregister_device(goldfish_tty_driver, qtty->console.index);
323 iounmap(qtty->base);
324 qtty->base = NULL;
325 free_irq(qtty->irq, pdev);
326 goldfish_tty_current_line_count--;
327 if (goldfish_tty_current_line_count == 0)
328 goldfish_tty_delete_driver();
329 mutex_unlock(&goldfish_tty_lock);
330 return 0;
333 static const struct of_device_id goldfish_tty_of_match[] = {
334 { .compatible = "google,goldfish-tty", },
338 MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
340 static struct platform_driver goldfish_tty_platform_driver = {
341 .probe = goldfish_tty_probe,
342 .remove = goldfish_tty_remove,
343 .driver = {
344 .name = "goldfish_tty",
345 .of_match_table = goldfish_tty_of_match,
349 module_platform_driver(goldfish_tty_platform_driver);
351 MODULE_LICENSE("GPL v2");