blob: 011eb5322077827657146b940f57496a343c07c8 [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>
Alane0f682e02014-05-12 16:57:05 +010024#include <linux/goldfish.h>
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000025
Aleksandar Markovic2296eee2017-08-29 15:53:18 +020026/* Goldfish tty register's offsets */
27#define GOLDFISH_TTY_REG_BYTES_READY 0x04
28#define GOLDFISH_TTY_REG_CMD 0x08
29#define GOLDFISH_TTY_REG_DATA_PTR 0x10
30#define GOLDFISH_TTY_REG_DATA_LEN 0x14
31#define GOLDFISH_TTY_REG_DATA_PTR_HIGH 0x18
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000032
Aleksandar Markovic2296eee2017-08-29 15:53:18 +020033/* Goldfish tty commands */
34#define GOLDFISH_TTY_CMD_INT_DISABLE 0
35#define GOLDFISH_TTY_CMD_INT_ENABLE 1
36#define GOLDFISH_TTY_CMD_WRITE_BUFFER 2
37#define GOLDFISH_TTY_CMD_READ_BUFFER 3
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000038
39struct goldfish_tty {
40 struct tty_port port;
41 spinlock_t lock;
42 void __iomem *base;
43 u32 irq;
44 int opencount;
45 struct console console;
46};
47
48static DEFINE_MUTEX(goldfish_tty_lock);
49static struct tty_driver *goldfish_tty_driver;
50static u32 goldfish_tty_line_count = 8;
51static u32 goldfish_tty_current_line_count;
52static struct goldfish_tty *goldfish_ttys;
53
54static void goldfish_tty_do_write(int line, const char *buf, unsigned count)
55{
56 unsigned long irq_flags;
57 struct goldfish_tty *qtty = &goldfish_ttys[line];
58 void __iomem *base = qtty->base;
59 spin_lock_irqsave(&qtty->lock, irq_flags);
Aleksandar Markovic2296eee2017-08-29 15:53:18 +020060 gf_write_ptr(buf, base + GOLDFISH_TTY_REG_DATA_PTR,
61 base + GOLDFISH_TTY_REG_DATA_PTR_HIGH);
62 writel(count, base + GOLDFISH_TTY_REG_DATA_LEN);
63 writel(GOLDFISH_TTY_CMD_WRITE_BUFFER, base + GOLDFISH_TTY_REG_CMD);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000064 spin_unlock_irqrestore(&qtty->lock, irq_flags);
65}
66
67static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
68{
Greg Hackmann465893e2016-02-26 19:01:05 +000069 struct goldfish_tty *qtty = dev_id;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000070 void __iomem *base = qtty->base;
71 unsigned long irq_flags;
72 unsigned char *buf;
73 u32 count;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000074
Aleksandar Markovic2296eee2017-08-29 15:53:18 +020075 count = readl(base + GOLDFISH_TTY_REG_BYTES_READY);
Aland78055d2014-05-12 16:57:14 +010076 if (count == 0)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000077 return IRQ_NONE;
78
Alan Coxebcf0982013-01-25 15:05:30 +000079 count = tty_prepare_flip_string(&qtty->port, &buf, count);
80 spin_lock_irqsave(&qtty->lock, irq_flags);
Aleksandar Markovic2296eee2017-08-29 15:53:18 +020081 gf_write_ptr(buf, base + GOLDFISH_TTY_REG_DATA_PTR,
82 base + GOLDFISH_TTY_REG_DATA_PTR_HIGH);
83 writel(count, base + GOLDFISH_TTY_REG_DATA_LEN);
84 writel(GOLDFISH_TTY_CMD_READ_BUFFER, base + GOLDFISH_TTY_REG_CMD);
Alan Coxebcf0982013-01-25 15:05:30 +000085 spin_unlock_irqrestore(&qtty->lock, irq_flags);
86 tty_schedule_flip(&qtty->port);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000087 return IRQ_HANDLED;
88}
89
90static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
91{
Aland78055d2014-05-12 16:57:14 +010092 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
93 port);
Aleksandar Markovic2296eee2017-08-29 15:53:18 +020094 writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +000095 return 0;
96}
97
98static void goldfish_tty_shutdown(struct tty_port *port)
99{
Aland78055d2014-05-12 16:57:14 +0100100 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
101 port);
Aleksandar Markovic2296eee2017-08-29 15:53:18 +0200102 writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000103}
104
Aland78055d2014-05-12 16:57:14 +0100105static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000106{
107 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
108 return tty_port_open(&qtty->port, tty, filp);
109}
110
Aland78055d2014-05-12 16:57:14 +0100111static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000112{
113 tty_port_close(tty->port, tty, filp);
114}
115
116static void goldfish_tty_hangup(struct tty_struct *tty)
117{
118 tty_port_hangup(tty->port);
119}
120
Aland78055d2014-05-12 16:57:14 +0100121static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
122 int count)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000123{
124 goldfish_tty_do_write(tty->index, buf, count);
125 return count;
126}
127
128static int goldfish_tty_write_room(struct tty_struct *tty)
129{
130 return 0x10000;
131}
132
133static int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
134{
135 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
136 void __iomem *base = qtty->base;
Aleksandar Markovic2296eee2017-08-29 15:53:18 +0200137 return readl(base + GOLDFISH_TTY_REG_BYTES_READY);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000138}
139
Aland78055d2014-05-12 16:57:14 +0100140static void goldfish_tty_console_write(struct console *co, const char *b,
141 unsigned count)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000142{
143 goldfish_tty_do_write(co->index, b, count);
144}
145
Aland78055d2014-05-12 16:57:14 +0100146static struct tty_driver *goldfish_tty_console_device(struct console *c,
147 int *index)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000148{
149 *index = c->index;
150 return goldfish_tty_driver;
151}
152
153static int goldfish_tty_console_setup(struct console *co, char *options)
154{
Dan Carpenterfda2b412014-10-29 11:43:25 +0300155 if ((unsigned)co->index >= goldfish_tty_line_count)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000156 return -ENODEV;
Fabian Fredericka4dc9232014-09-28 20:10:17 +0200157 if (!goldfish_ttys[co->index].base)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000158 return -ENODEV;
159 return 0;
160}
161
Aya Mahfouz04b757d2015-12-15 01:53:36 +0200162static const struct tty_port_operations goldfish_port_ops = {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000163 .activate = goldfish_tty_activate,
164 .shutdown = goldfish_tty_shutdown
165};
166
Aland78055d2014-05-12 16:57:14 +0100167static const struct tty_operations goldfish_tty_ops = {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000168 .open = goldfish_tty_open,
169 .close = goldfish_tty_close,
170 .hangup = goldfish_tty_hangup,
171 .write = goldfish_tty_write,
172 .write_room = goldfish_tty_write_room,
173 .chars_in_buffer = goldfish_tty_chars_in_buffer,
174};
175
176static int goldfish_tty_create_driver(void)
177{
178 int ret;
179 struct tty_driver *tty;
180
Aland78055d2014-05-12 16:57:14 +0100181 goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) *
182 goldfish_tty_line_count, GFP_KERNEL);
183 if (goldfish_ttys == NULL) {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000184 ret = -ENOMEM;
185 goto err_alloc_goldfish_ttys_failed;
186 }
187 tty = alloc_tty_driver(goldfish_tty_line_count);
Aland78055d2014-05-12 16:57:14 +0100188 if (tty == NULL) {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000189 ret = -ENOMEM;
190 goto err_alloc_tty_driver_failed;
191 }
192 tty->driver_name = "goldfish";
193 tty->name = "ttyGF";
194 tty->type = TTY_DRIVER_TYPE_SERIAL;
195 tty->subtype = SERIAL_TYPE_NORMAL;
196 tty->init_termios = tty_std_termios;
Aland78055d2014-05-12 16:57:14 +0100197 tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
198 TTY_DRIVER_DYNAMIC_DEV;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000199 tty_set_operations(tty, &goldfish_tty_ops);
200 ret = tty_register_driver(tty);
Aland78055d2014-05-12 16:57:14 +0100201 if (ret)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000202 goto err_tty_register_driver_failed;
203
204 goldfish_tty_driver = tty;
205 return 0;
206
207err_tty_register_driver_failed:
208 put_tty_driver(tty);
209err_alloc_tty_driver_failed:
210 kfree(goldfish_ttys);
211 goldfish_ttys = NULL;
212err_alloc_goldfish_ttys_failed:
213 return ret;
214}
215
216static void goldfish_tty_delete_driver(void)
217{
218 tty_unregister_driver(goldfish_tty_driver);
219 put_tty_driver(goldfish_tty_driver);
220 goldfish_tty_driver = NULL;
221 kfree(goldfish_ttys);
222 goldfish_ttys = NULL;
223}
224
225static int goldfish_tty_probe(struct platform_device *pdev)
226{
227 struct goldfish_tty *qtty;
228 int ret = -EINVAL;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000229 struct resource *r;
230 struct device *ttydev;
231 void __iomem *base;
232 u32 irq;
Greg Hackmann465893e2016-02-26 19:01:05 +0000233 unsigned int line;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000234
235 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Aland78055d2014-05-12 16:57:14 +0100236 if (r == NULL)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000237 return -EINVAL;
238
239 base = ioremap(r->start, 0x1000);
240 if (base == NULL)
241 pr_err("goldfish_tty: unable to remap base\n");
242
243 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Aland78055d2014-05-12 16:57:14 +0100244 if (r == NULL)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000245 goto err_unmap;
246
247 irq = r->start;
248
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000249 mutex_lock(&goldfish_tty_lock);
Greg Hackmann465893e2016-02-26 19:01:05 +0000250
251 if (pdev->id == PLATFORM_DEVID_NONE)
252 line = goldfish_tty_current_line_count;
253 else
254 line = pdev->id;
255
256 if (line >= goldfish_tty_line_count)
257 goto err_create_driver_failed;
258
Aland78055d2014-05-12 16:57:14 +0100259 if (goldfish_tty_current_line_count == 0) {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000260 ret = goldfish_tty_create_driver();
Aland78055d2014-05-12 16:57:14 +0100261 if (ret)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000262 goto err_create_driver_failed;
263 }
264 goldfish_tty_current_line_count++;
265
Greg Hackmann465893e2016-02-26 19:01:05 +0000266 qtty = &goldfish_ttys[line];
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000267 spin_lock_init(&qtty->lock);
268 tty_port_init(&qtty->port);
269 qtty->port.ops = &goldfish_port_ops;
270 qtty->base = base;
271 qtty->irq = irq;
272
Aleksandar Markovic2296eee2017-08-29 15:53:18 +0200273 writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000274
Aland78055d2014-05-12 16:57:14 +0100275 ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
Greg Hackmann465893e2016-02-26 19:01:05 +0000276 "goldfish_tty", qtty);
Aland78055d2014-05-12 16:57:14 +0100277 if (ret)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000278 goto err_request_irq_failed;
279
280
281 ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
Greg Hackmann465893e2016-02-26 19:01:05 +0000282 line, &pdev->dev);
Aland78055d2014-05-12 16:57:14 +0100283 if (IS_ERR(ttydev)) {
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000284 ret = PTR_ERR(ttydev);
285 goto err_tty_register_device_failed;
286 }
287
288 strcpy(qtty->console.name, "ttyGF");
289 qtty->console.write = goldfish_tty_console_write;
290 qtty->console.device = goldfish_tty_console_device;
291 qtty->console.setup = goldfish_tty_console_setup;
292 qtty->console.flags = CON_PRINTBUFFER;
Greg Hackmann465893e2016-02-26 19:01:05 +0000293 qtty->console.index = line;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000294 register_console(&qtty->console);
Greg Hackmann465893e2016-02-26 19:01:05 +0000295 platform_set_drvdata(pdev, qtty);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000296
297 mutex_unlock(&goldfish_tty_lock);
298 return 0;
299
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000300err_tty_register_device_failed:
Christophe JAILLET1a5c2d12017-01-09 01:26:37 +0100301 free_irq(irq, qtty);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000302err_request_irq_failed:
303 goldfish_tty_current_line_count--;
Aland78055d2014-05-12 16:57:14 +0100304 if (goldfish_tty_current_line_count == 0)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000305 goldfish_tty_delete_driver();
306err_create_driver_failed:
307 mutex_unlock(&goldfish_tty_lock);
308err_unmap:
309 iounmap(base);
310 return ret;
311}
312
313static int goldfish_tty_remove(struct platform_device *pdev)
314{
Greg Hackmann465893e2016-02-26 19:01:05 +0000315 struct goldfish_tty *qtty = platform_get_drvdata(pdev);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000316
317 mutex_lock(&goldfish_tty_lock);
318
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000319 unregister_console(&qtty->console);
Greg Hackmann465893e2016-02-26 19:01:05 +0000320 tty_unregister_device(goldfish_tty_driver, qtty->console.index);
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000321 iounmap(qtty->base);
Fabian Fredericka4dc9232014-09-28 20:10:17 +0200322 qtty->base = NULL;
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000323 free_irq(qtty->irq, pdev);
324 goldfish_tty_current_line_count--;
Aland78055d2014-05-12 16:57:14 +0100325 if (goldfish_tty_current_line_count == 0)
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000326 goldfish_tty_delete_driver();
327 mutex_unlock(&goldfish_tty_lock);
328 return 0;
329}
330
Miodrag Dinic9b883ee2016-02-26 19:00:44 +0000331static const struct of_device_id goldfish_tty_of_match[] = {
332 { .compatible = "google,goldfish-tty", },
333 {},
334};
335
336MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
337
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000338static struct platform_driver goldfish_tty_platform_driver = {
339 .probe = goldfish_tty_probe,
340 .remove = goldfish_tty_remove,
341 .driver = {
Miodrag Dinic9b883ee2016-02-26 19:00:44 +0000342 .name = "goldfish_tty",
343 .of_match_table = goldfish_tty_of_match,
Arve Hjønnevåg666b7792013-01-21 23:38:47 +0000344 }
345};
346
347module_platform_driver(goldfish_tty_platform_driver);
348
349MODULE_LICENSE("GPL v2");