]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/tty/goldfish.c
tty: goldfish: Use streaming DMA for r/w operations on Ranchu platforms
[mirror_ubuntu-jammy-kernel.git] / drivers / tty / goldfish.c
CommitLineData
666b7793
AH
1/*
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>
666b7793
AH
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>
e0f682e0 24#include <linux/goldfish.h>
7157d2be
MD
25#include <linux/mm.h>
26#include <linux/dma-mapping.h>
666b7793 27
2296eee7
AM
28/* Goldfish tty register's offsets */
29#define GOLDFISH_TTY_REG_BYTES_READY 0x04
30#define GOLDFISH_TTY_REG_CMD 0x08
31#define GOLDFISH_TTY_REG_DATA_PTR 0x10
32#define GOLDFISH_TTY_REG_DATA_LEN 0x14
33#define GOLDFISH_TTY_REG_DATA_PTR_HIGH 0x18
7157d2be 34#define GOLDFISH_TTY_REG_VERSION 0x20
2296eee7
AM
35
36/* Goldfish tty commands */
37#define GOLDFISH_TTY_CMD_INT_DISABLE 0
38#define GOLDFISH_TTY_CMD_INT_ENABLE 1
39#define GOLDFISH_TTY_CMD_WRITE_BUFFER 2
40#define GOLDFISH_TTY_CMD_READ_BUFFER 3
666b7793
AH
41
42struct goldfish_tty {
43 struct tty_port port;
44 spinlock_t lock;
45 void __iomem *base;
46 u32 irq;
47 int opencount;
48 struct console console;
7157d2be
MD
49 u32 version;
50 struct device *dev;
666b7793
AH
51};
52
53static DEFINE_MUTEX(goldfish_tty_lock);
54static struct tty_driver *goldfish_tty_driver;
55static u32 goldfish_tty_line_count = 8;
56static u32 goldfish_tty_current_line_count;
57static struct goldfish_tty *goldfish_ttys;
58
7157d2be
MD
59static void do_rw_io(struct goldfish_tty *qtty,
60 unsigned long address,
61 unsigned int count,
62 int is_write)
666b7793
AH
63{
64 unsigned long irq_flags;
666b7793 65 void __iomem *base = qtty->base;
7157d2be 66
666b7793 67 spin_lock_irqsave(&qtty->lock, irq_flags);
7157d2be 68 gf_write_ptr((void *)address, base + GOLDFISH_TTY_REG_DATA_PTR,
2296eee7
AM
69 base + GOLDFISH_TTY_REG_DATA_PTR_HIGH);
70 writel(count, base + GOLDFISH_TTY_REG_DATA_LEN);
7157d2be
MD
71
72 if (is_write)
73 writel(GOLDFISH_TTY_CMD_WRITE_BUFFER,
74 base + GOLDFISH_TTY_REG_CMD);
75 else
76 writel(GOLDFISH_TTY_CMD_READ_BUFFER,
77 base + GOLDFISH_TTY_REG_CMD);
78
666b7793
AH
79 spin_unlock_irqrestore(&qtty->lock, irq_flags);
80}
81
7157d2be
MD
82static void goldfish_tty_rw(struct goldfish_tty *qtty,
83 unsigned long addr,
84 unsigned int count,
85 int is_write)
86{
87 dma_addr_t dma_handle;
88 enum dma_data_direction dma_dir;
89
90 dma_dir = (is_write ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
91 if (qtty->version > 0) {
92 /*
93 * Goldfish TTY for Ranchu platform uses
94 * physical addresses and DMA for read/write operations
95 */
96 unsigned long addr_end = addr + count;
97
98 while (addr < addr_end) {
99 unsigned long pg_end = (addr & PAGE_MASK) + PAGE_SIZE;
100 unsigned long next =
101 pg_end < addr_end ? pg_end : addr_end;
102 unsigned long avail = next - addr;
103
104 /*
105 * Map the buffer's virtual address to the DMA address
106 * so the buffer can be accessed by the device.
107 */
108 dma_handle = dma_map_single(qtty->dev, (void *)addr,
109 avail, dma_dir);
110
111 if (dma_mapping_error(qtty->dev, dma_handle)) {
112 dev_err(qtty->dev, "tty: DMA mapping error.\n");
113 return;
114 }
115 do_rw_io(qtty, dma_handle, avail, is_write);
116
117 /*
118 * Unmap the previously mapped region after
119 * the completion of the read/write operation.
120 */
121 dma_unmap_single(qtty->dev, dma_handle, avail, dma_dir);
122
123 addr += avail;
124 }
125 } else {
126 /*
127 * Old style Goldfish TTY used on the Goldfish platform
128 * uses virtual addresses.
129 */
130 do_rw_io(qtty, addr, count, is_write);
131 }
132}
133
134static void goldfish_tty_do_write(int line, const char *buf,
135 unsigned int count)
136{
137 struct goldfish_tty *qtty = &goldfish_ttys[line];
138 unsigned long address = (unsigned long)(void *)buf;
139
140 goldfish_tty_rw(qtty, address, count, 1);
141}
142
666b7793
AH
143static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id)
144{
465893e1 145 struct goldfish_tty *qtty = dev_id;
666b7793 146 void __iomem *base = qtty->base;
7157d2be 147 unsigned long address;
666b7793
AH
148 unsigned char *buf;
149 u32 count;
666b7793 150
2296eee7 151 count = readl(base + GOLDFISH_TTY_REG_BYTES_READY);
d78055dc 152 if (count == 0)
666b7793
AH
153 return IRQ_NONE;
154
ebcf0981 155 count = tty_prepare_flip_string(&qtty->port, &buf, count);
7157d2be
MD
156
157 address = (unsigned long)(void *)buf;
158 goldfish_tty_rw(qtty, address, count, 0);
159
ebcf0981 160 tty_schedule_flip(&qtty->port);
666b7793
AH
161 return IRQ_HANDLED;
162}
163
164static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty)
165{
d78055dc
A
166 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
167 port);
2296eee7 168 writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
666b7793
AH
169 return 0;
170}
171
172static void goldfish_tty_shutdown(struct tty_port *port)
173{
d78055dc
A
174 struct goldfish_tty *qtty = container_of(port, struct goldfish_tty,
175 port);
2296eee7 176 writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_REG_CMD);
666b7793
AH
177}
178
d78055dc 179static int goldfish_tty_open(struct tty_struct *tty, struct file *filp)
666b7793
AH
180{
181 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
182 return tty_port_open(&qtty->port, tty, filp);
183}
184
d78055dc 185static void goldfish_tty_close(struct tty_struct *tty, struct file *filp)
666b7793
AH
186{
187 tty_port_close(tty->port, tty, filp);
188}
189
190static void goldfish_tty_hangup(struct tty_struct *tty)
191{
192 tty_port_hangup(tty->port);
193}
194
d78055dc
A
195static int goldfish_tty_write(struct tty_struct *tty, const unsigned char *buf,
196 int count)
666b7793
AH
197{
198 goldfish_tty_do_write(tty->index, buf, count);
199 return count;
200}
201
202static int goldfish_tty_write_room(struct tty_struct *tty)
203{
204 return 0x10000;
205}
206
207static int goldfish_tty_chars_in_buffer(struct tty_struct *tty)
208{
209 struct goldfish_tty *qtty = &goldfish_ttys[tty->index];
210 void __iomem *base = qtty->base;
2296eee7 211 return readl(base + GOLDFISH_TTY_REG_BYTES_READY);
666b7793
AH
212}
213
d78055dc
A
214static void goldfish_tty_console_write(struct console *co, const char *b,
215 unsigned count)
666b7793
AH
216{
217 goldfish_tty_do_write(co->index, b, count);
218}
219
d78055dc
A
220static struct tty_driver *goldfish_tty_console_device(struct console *c,
221 int *index)
666b7793
AH
222{
223 *index = c->index;
224 return goldfish_tty_driver;
225}
226
227static int goldfish_tty_console_setup(struct console *co, char *options)
228{
fda2b418 229 if ((unsigned)co->index >= goldfish_tty_line_count)
666b7793 230 return -ENODEV;
a4dc9236 231 if (!goldfish_ttys[co->index].base)
666b7793
AH
232 return -ENODEV;
233 return 0;
234}
235
04b757df 236static const struct tty_port_operations goldfish_port_ops = {
666b7793
AH
237 .activate = goldfish_tty_activate,
238 .shutdown = goldfish_tty_shutdown
239};
240
d78055dc 241static const struct tty_operations goldfish_tty_ops = {
666b7793
AH
242 .open = goldfish_tty_open,
243 .close = goldfish_tty_close,
244 .hangup = goldfish_tty_hangup,
245 .write = goldfish_tty_write,
246 .write_room = goldfish_tty_write_room,
247 .chars_in_buffer = goldfish_tty_chars_in_buffer,
248};
249
250static int goldfish_tty_create_driver(void)
251{
252 int ret;
253 struct tty_driver *tty;
254
d78055dc
A
255 goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) *
256 goldfish_tty_line_count, GFP_KERNEL);
257 if (goldfish_ttys == NULL) {
666b7793
AH
258 ret = -ENOMEM;
259 goto err_alloc_goldfish_ttys_failed;
260 }
261 tty = alloc_tty_driver(goldfish_tty_line_count);
d78055dc 262 if (tty == NULL) {
666b7793
AH
263 ret = -ENOMEM;
264 goto err_alloc_tty_driver_failed;
265 }
266 tty->driver_name = "goldfish";
267 tty->name = "ttyGF";
268 tty->type = TTY_DRIVER_TYPE_SERIAL;
269 tty->subtype = SERIAL_TYPE_NORMAL;
270 tty->init_termios = tty_std_termios;
d78055dc
A
271 tty->flags = TTY_DRIVER_RESET_TERMIOS | TTY_DRIVER_REAL_RAW |
272 TTY_DRIVER_DYNAMIC_DEV;
666b7793
AH
273 tty_set_operations(tty, &goldfish_tty_ops);
274 ret = tty_register_driver(tty);
d78055dc 275 if (ret)
666b7793
AH
276 goto err_tty_register_driver_failed;
277
278 goldfish_tty_driver = tty;
279 return 0;
280
281err_tty_register_driver_failed:
282 put_tty_driver(tty);
283err_alloc_tty_driver_failed:
284 kfree(goldfish_ttys);
285 goldfish_ttys = NULL;
286err_alloc_goldfish_ttys_failed:
287 return ret;
288}
289
290static void goldfish_tty_delete_driver(void)
291{
292 tty_unregister_driver(goldfish_tty_driver);
293 put_tty_driver(goldfish_tty_driver);
294 goldfish_tty_driver = NULL;
295 kfree(goldfish_ttys);
296 goldfish_ttys = NULL;
297}
298
299static int goldfish_tty_probe(struct platform_device *pdev)
300{
301 struct goldfish_tty *qtty;
7157d2be 302 int ret = -ENODEV;
666b7793
AH
303 struct resource *r;
304 struct device *ttydev;
305 void __iomem *base;
306 u32 irq;
465893e1 307 unsigned int line;
666b7793
AH
308
309 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
7157d2be
MD
310 if (!r) {
311 pr_err("goldfish_tty: No MEM resource available!\n");
312 return -ENOMEM;
313 }
666b7793
AH
314
315 base = ioremap(r->start, 0x1000);
7157d2be
MD
316 if (!base) {
317 pr_err("goldfish_tty: Unable to ioremap base!\n");
318 return -ENOMEM;
319 }
666b7793
AH
320
321 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
7157d2be
MD
322 if (!r) {
323 pr_err("goldfish_tty: No IRQ resource available!\n");
666b7793 324 goto err_unmap;
7157d2be 325 }
666b7793
AH
326
327 irq = r->start;
328
666b7793 329 mutex_lock(&goldfish_tty_lock);
465893e1
GH
330
331 if (pdev->id == PLATFORM_DEVID_NONE)
332 line = goldfish_tty_current_line_count;
333 else
334 line = pdev->id;
335
7157d2be
MD
336 if (line >= goldfish_tty_line_count) {
337 pr_err("goldfish_tty: Reached maximum tty number of %d.\n",
338 goldfish_tty_current_line_count);
339 ret = -ENOMEM;
340 goto err_unlock;
341 }
465893e1 342
d78055dc 343 if (goldfish_tty_current_line_count == 0) {
666b7793 344 ret = goldfish_tty_create_driver();
d78055dc 345 if (ret)
7157d2be 346 goto err_unlock;
666b7793
AH
347 }
348 goldfish_tty_current_line_count++;
349
465893e1 350 qtty = &goldfish_ttys[line];
666b7793
AH
351 spin_lock_init(&qtty->lock);
352 tty_port_init(&qtty->port);
353 qtty->port.ops = &goldfish_port_ops;
354 qtty->base = base;
355 qtty->irq = irq;
7157d2be
MD
356 qtty->dev = &pdev->dev;
357
358 /*
359 * Goldfish TTY device used by the Goldfish emulator
360 * should identify itself with 0, forcing the driver
361 * to use virtual addresses. Goldfish TTY device
362 * on Ranchu emulator (qemu2) returns 1 here and
363 * driver will use physical addresses.
364 */
365 qtty->version = readl(base + GOLDFISH_TTY_REG_VERSION);
366
367 /*
368 * Goldfish TTY device on Ranchu emulator (qemu2)
369 * will use DMA for read/write IO operations.
370 */
371 if (qtty->version > 0) {
372 /*
373 * Initialize dma_mask to 32-bits.
374 */
375 if (!pdev->dev.dma_mask)
376 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
377 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
378 if (ret) {
379 dev_err(&pdev->dev, "No suitable DMA available.\n");
380 goto err_dec_line_count;
381 }
382 }
666b7793 383
2296eee7 384 writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_REG_CMD);
666b7793 385
d78055dc 386 ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED,
7157d2be
MD
387 "goldfish_tty", qtty);
388 if (ret) {
389 pr_err("goldfish_tty: No IRQ available!\n");
390 goto err_dec_line_count;
391 }
666b7793
AH
392
393 ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver,
7157d2be 394 line, &pdev->dev);
d78055dc 395 if (IS_ERR(ttydev)) {
666b7793
AH
396 ret = PTR_ERR(ttydev);
397 goto err_tty_register_device_failed;
398 }
399
400 strcpy(qtty->console.name, "ttyGF");
401 qtty->console.write = goldfish_tty_console_write;
402 qtty->console.device = goldfish_tty_console_device;
403 qtty->console.setup = goldfish_tty_console_setup;
404 qtty->console.flags = CON_PRINTBUFFER;
465893e1 405 qtty->console.index = line;
666b7793 406 register_console(&qtty->console);
465893e1 407 platform_set_drvdata(pdev, qtty);
666b7793
AH
408
409 mutex_unlock(&goldfish_tty_lock);
410 return 0;
411
666b7793 412err_tty_register_device_failed:
1a5c2d1d 413 free_irq(irq, qtty);
7157d2be 414err_dec_line_count:
666b7793 415 goldfish_tty_current_line_count--;
d78055dc 416 if (goldfish_tty_current_line_count == 0)
666b7793 417 goldfish_tty_delete_driver();
7157d2be 418err_unlock:
666b7793
AH
419 mutex_unlock(&goldfish_tty_lock);
420err_unmap:
421 iounmap(base);
422 return ret;
423}
424
425static int goldfish_tty_remove(struct platform_device *pdev)
426{
465893e1 427 struct goldfish_tty *qtty = platform_get_drvdata(pdev);
666b7793
AH
428
429 mutex_lock(&goldfish_tty_lock);
430
666b7793 431 unregister_console(&qtty->console);
465893e1 432 tty_unregister_device(goldfish_tty_driver, qtty->console.index);
666b7793 433 iounmap(qtty->base);
a4dc9236 434 qtty->base = NULL;
666b7793
AH
435 free_irq(qtty->irq, pdev);
436 goldfish_tty_current_line_count--;
d78055dc 437 if (goldfish_tty_current_line_count == 0)
666b7793
AH
438 goldfish_tty_delete_driver();
439 mutex_unlock(&goldfish_tty_lock);
440 return 0;
441}
442
9b883eea
MD
443static const struct of_device_id goldfish_tty_of_match[] = {
444 { .compatible = "google,goldfish-tty", },
445 {},
446};
447
448MODULE_DEVICE_TABLE(of, goldfish_tty_of_match);
449
666b7793
AH
450static struct platform_driver goldfish_tty_platform_driver = {
451 .probe = goldfish_tty_probe,
452 .remove = goldfish_tty_remove,
453 .driver = {
9b883eea
MD
454 .name = "goldfish_tty",
455 .of_match_table = goldfish_tty_of_match,
666b7793
AH
456 }
457};
458
459module_platform_driver(goldfish_tty_platform_driver);
460
461MODULE_LICENSE("GPL v2");