blob: 3fc912373adf236c6d7a4060608ccc8c78617fd5 [file] [log] [blame]
Arve Hjønnevåg666b7792013-01-21 23:38:47 +00001/*
2 * Copyright (C) 2007 Google, Inc.
3 * Copyright (C) 2012 Intel, Inc.
4 *
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.
8 *
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.
13 *
14 */
15
16#include <linux/console.h>
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000017#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>
Alane0f682e2014-05-12 16:57:05 +010024#include <linux/goldfish.h>
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000025
26enum {
27 GOLDFISH_TTY_PUT_CHAR = 0x00,
28 GOLDFISH_TTY_BYTES_READY = 0x04,
29 GOLDFISH_TTY_CMD = 0x08,
30
31 GOLDFISH_TTY_DATA_PTR = 0x10,
32 GOLDFISH_TTY_DATA_LEN = 0x14,
Jun Tianb8658bc2014-05-12 16:55:26 +010033 GOLDFISH_TTY_DATA_PTR_HIGH = 0x18,
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000034
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,
39};
40
41struct 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;
48};
49
50static DEFINE_MUTEX(goldfish_tty_lock);
51static struct tty_driver *goldfish_tty_driver;
52static u32 goldfish_tty_line_count = 8;
53static u32 goldfish_tty_current_line_count;
54static struct goldfish_tty *goldfish_ttys;
55
56static void goldfish_tty_do_write(int line, const char *buf, unsigned count)
57{
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);
Peter Senna Tschudin07d783f2015-05-19 11:44:46 +020062 gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR,
Alane0f682e2014-05-12 16:57:05 +010063 base + GOLDFISH_TTY_DATA_PTR_HIGH);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000064 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);
67}
68
69static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
70{
Greg Hackmann465893e2016-02-26 19:01:05 +000071 struct goldfish_tty *qtty = dev_id;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000072 void __iomem *base = qtty->base;
73 unsigned long irq_flags;
74 unsigned char *buf;
75 u32 count;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000076
77 count = readl(base + GOLDFISH_TTY_BYTES_READY);
Aland78055d2014-05-12 16:57:14 +010078 if (count == 0)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000079 return IRQ_NONE;
80
Alan Coxebcf0982013-01-25 15:05:30 +000081 count = tty_prepare_flip_string(&qtty->port, &buf, count);
82 spin_lock_irqsave(&qtty->lock, irq_flags);
Peter Senna Tschudin07d783f2015-05-19 11:44:46 +020083 gf_write_ptr(buf, base + GOLDFISH_TTY_DATA_PTR,
Alane0f682e2014-05-12 16:57:05 +010084 base + GOLDFISH_TTY_DATA_PTR_HIGH);
Alan Coxebcf0982013-01-25 15:05:30 +000085 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);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000089 return IRQ_HANDLED;
90}
91
92static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
93{
Aland78055d2014-05-12 16:57:14 +010094 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
95 port);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000096 writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_CMD);
97 return 0;
98}
99
100static void goldfish_tty_shutdown(struct tty_port *port)
101{
Aland78055d2014-05-12 16:57:14 +0100102 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
103 port);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000104 writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_CMD);
105}
106
Aland78055d2014-05-12 16:57:14 +0100107static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000108{
109 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
110 return tty_port_open(&qtty->port, tty, filp);
111}
112
Aland78055d2014-05-12 16:57:14 +0100113static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000114{
115 tty_port_close(tty->port, tty, filp);
116}
117
118static void goldfish_tty_hangup(struct tty_struct *tty)
119{
120 tty_port_hangup(tty->port);
121}
122
Aland78055d2014-05-12 16:57:14 +0100123static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
124 int count)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000125{
126 goldfish_tty_do_write(tty->index, buf, count);
127 return count;
128}
129
130static int goldfish_tty_write_room(struct tty_struct *tty)
131{
132 return 0x10000;
133}
134
135static int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
136{
137 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
138 void __iomem *base = qtty->base;
139 return readl(base + GOLDFISH_TTY_BYTES_READY);
140}
141
Aland78055d2014-05-12 16:57:14 +0100142static void goldfish_tty_console_write(struct console *co, const char *b,
143 unsigned count)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000144{
145 goldfish_tty_do_write(co->index, b, count);
146}
147
Aland78055d2014-05-12 16:57:14 +0100148static struct tty_driver *goldfish_tty_console_device(struct console *c,
149 int *index)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000150{
151 *index = c->index;
152 return goldfish_tty_driver;
153}
154
155static int goldfish_tty_console_setup(struct console *co, char *options)
156{
Dan Carpenterfda2b412014-10-29 11:43:25 +0300157 if ((unsigned)co->index >= goldfish_tty_line_count)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000158 return -ENODEV;
Fabian Fredericka4dc9232014-09-28 20:10:17 +0200159 if (!goldfish_ttys[co->index].base)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000160 return -ENODEV;
161 return 0;
162}
163
Aya Mahfouz04b757d2015-12-15 01:53:36 +0200164static const struct tty_port_operations goldfish_port_ops = {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000165 .activate = goldfish_tty_activate,
166 .shutdown = goldfish_tty_shutdown
167};
168
Aland78055d2014-05-12 16:57:14 +0100169static const struct tty_operations goldfish_tty_ops = {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000170 .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,
176};
177
178static int goldfish_tty_create_driver(void)
179{
180 int ret;
181 struct tty_driver *tty;
182
Aland78055d2014-05-12 16:57:14 +0100183 goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) *
184 goldfish_tty_line_count, GFP_KERNEL);
185 if (goldfish_ttys == NULL) {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000186 ret = -ENOMEM;
187 goto err_alloc_goldfish_ttys_failed;
188 }
189 tty = alloc_tty_driver(goldfish_tty_line_count);
Aland78055d2014-05-12 16:57:14 +0100190 if (tty == NULL) {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000191 ret = -ENOMEM;
192 goto err_alloc_tty_driver_failed;
193 }
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;
Aland78055d2014-05-12 16:57:14 +0100199 tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
200 TTY_DRIVER_DYNAMIC_DEV;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000201 tty_set_operations(tty, &goldfish_tty_ops);
202 ret = tty_register_driver(tty);
Aland78055d2014-05-12 16:57:14 +0100203 if (ret)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000204 goto err_tty_register_driver_failed;
205
206 goldfish_tty_driver = tty;
207 return 0;
208
209err_tty_register_driver_failed:
210 put_tty_driver(tty);
211err_alloc_tty_driver_failed:
212 kfree(goldfish_ttys);
213 goldfish_ttys = NULL;
214err_alloc_goldfish_ttys_failed:
215 return ret;
216}
217
218static void goldfish_tty_delete_driver(void)
219{
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;
225}
226
227static int goldfish_tty_probe(struct platform_device *pdev)
228{
229 struct goldfish_tty *qtty;
230 int ret = -EINVAL;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000231 struct resource *r;
232 struct device *ttydev;
233 void __iomem *base;
234 u32 irq;
Greg Hackmann465893e2016-02-26 19:01:05 +0000235 unsigned int line;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000236
237 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Aland78055d2014-05-12 16:57:14 +0100238 if (r == NULL)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000239 return -EINVAL;
240
241 base = ioremap(r->start, 0x1000);
242 if (base == NULL)
243 pr_err("goldfish_tty: unable to remap base\n");
244
245 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Aland78055d2014-05-12 16:57:14 +0100246 if (r == NULL)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000247 goto err_unmap;
248
249 irq = r->start;
250
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000251 mutex_lock(&goldfish_tty_lock);
Greg Hackmann465893e2016-02-26 19:01:05 +0000252
253 if (pdev->id == PLATFORM_DEVID_NONE)
254 line = goldfish_tty_current_line_count;
255 else
256 line = pdev->id;
257
258 if (line >= goldfish_tty_line_count)
259 goto err_create_driver_failed;
260
Aland78055d2014-05-12 16:57:14 +0100261 if (goldfish_tty_current_line_count == 0) {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000262 ret = goldfish_tty_create_driver();
Aland78055d2014-05-12 16:57:14 +0100263 if (ret)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000264 goto err_create_driver_failed;
265 }
266 goldfish_tty_current_line_count++;
267
Greg Hackmann465893e2016-02-26 19:01:05 +0000268 qtty = &goldfish_ttys[line];
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000269 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;
274
275 writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_CMD);
276
Aland78055d2014-05-12 16:57:14 +0100277 ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
Greg Hackmann465893e2016-02-26 19:01:05 +0000278 "goldfish_tty", qtty);
Aland78055d2014-05-12 16:57:14 +0100279 if (ret)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000280 goto err_request_irq_failed;
281
282
283 ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
Greg Hackmann465893e2016-02-26 19:01:05 +0000284 line, &pdev->dev);
Aland78055d2014-05-12 16:57:14 +0100285 if (IS_ERR(ttydev)) {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000286 ret = PTR_ERR(ttydev);
287 goto err_tty_register_device_failed;
288 }
289
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;
Greg Hackmann465893e2016-02-26 19:01:05 +0000295 qtty->console.index = line;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000296 register_console(&qtty->console);
Greg Hackmann465893e2016-02-26 19:01:05 +0000297 platform_set_drvdata(pdev, qtty);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000298
299 mutex_unlock(&goldfish_tty_lock);
300 return 0;
301
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000302err_tty_register_device_failed:
303 free_irq(irq, pdev);
304err_request_irq_failed:
305 goldfish_tty_current_line_count--;
Aland78055d2014-05-12 16:57:14 +0100306 if (goldfish_tty_current_line_count == 0)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000307 goldfish_tty_delete_driver();
308err_create_driver_failed:
309 mutex_unlock(&goldfish_tty_lock);
310err_unmap:
311 iounmap(base);
312 return ret;
313}
314
315static int goldfish_tty_remove(struct platform_device *pdev)
316{
Greg Hackmann465893e2016-02-26 19:01:05 +0000317 struct goldfish_tty *qtty = platform_get_drvdata(pdev);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000318
319 mutex_lock(&goldfish_tty_lock);
320
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000321 unregister_console(&qtty->console);
Greg Hackmann465893e2016-02-26 19:01:05 +0000322 tty_unregister_device(goldfish_tty_driver, qtty->console.index);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000323 iounmap(qtty->base);
Fabian Fredericka4dc9232014-09-28 20:10:17 +0200324 qtty->base = NULL;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000325 free_irq(qtty->irq, pdev);
326 goldfish_tty_current_line_count--;
Aland78055d2014-05-12 16:57:14 +0100327 if (goldfish_tty_current_line_count == 0)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000328 goldfish_tty_delete_driver();
329 mutex_unlock(&goldfish_tty_lock);
330 return 0;
331}
332
Miodrag Dinic9b883ee2016-02-26 19:00:44 +0000333static const struct of_device_id goldfish_tty_of_match[] = {
334 { .compatible = "google,goldfish-tty", },
335 {},
336};
337
338MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
339
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000340static struct platform_driver goldfish_tty_platform_driver = {
341 .probe = goldfish_tty_probe,
342 .remove = goldfish_tty_remove,
343 .driver = {
Miodrag Dinic9b883ee2016-02-26 19:00:44 +0000344 .name = "goldfish_tty",
345 .of_match_table = goldfish_tty_of_match,
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000346 }
347};
348
349module_platform_driver(goldfish_tty_platform_driver);
350
351MODULE_LICENSE("GPL v2");