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>
23 #include <linux/module.h>
26 GOLDFISH_TTY_PUT_CHAR
= 0x00,
27 GOLDFISH_TTY_BYTES_READY
= 0x04,
28 GOLDFISH_TTY_CMD
= 0x08,
30 GOLDFISH_TTY_DATA_PTR
= 0x10,
31 GOLDFISH_TTY_DATA_LEN
= 0x14,
33 GOLDFISH_TTY_CMD_INT_DISABLE
= 0,
34 GOLDFISH_TTY_CMD_INT_ENABLE
= 1,
35 GOLDFISH_TTY_CMD_WRITE_BUFFER
= 2,
36 GOLDFISH_TTY_CMD_READ_BUFFER
= 3,
45 struct console console
;
48 static DEFINE_MUTEX(goldfish_tty_lock
);
49 static struct tty_driver
*goldfish_tty_driver
;
50 static u32 goldfish_tty_line_count
= 8;
51 static u32 goldfish_tty_current_line_count
;
52 static struct goldfish_tty
*goldfish_ttys
;
54 static void goldfish_tty_do_write(int line
, const char *buf
, unsigned count
)
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
);
60 writel((u32
)buf
, base
+ GOLDFISH_TTY_DATA_PTR
);
61 writel(count
, base
+ GOLDFISH_TTY_DATA_LEN
);
62 writel(GOLDFISH_TTY_CMD_WRITE_BUFFER
, base
+ GOLDFISH_TTY_CMD
);
63 spin_unlock_irqrestore(&qtty
->lock
, irq_flags
);
66 static irqreturn_t
goldfish_tty_interrupt(int irq
, void *dev_id
)
68 struct platform_device
*pdev
= dev_id
;
69 struct goldfish_tty
*qtty
= &goldfish_ttys
[pdev
->id
];
70 void __iomem
*base
= qtty
->base
;
71 unsigned long irq_flags
;
75 count
= readl(base
+ GOLDFISH_TTY_BYTES_READY
);
79 count
= tty_prepare_flip_string(&qtty
->port
, &buf
, count
);
80 spin_lock_irqsave(&qtty
->lock
, irq_flags
);
81 writel((u32
)buf
, base
+ GOLDFISH_TTY_DATA_PTR
);
82 writel(count
, base
+ GOLDFISH_TTY_DATA_LEN
);
83 writel(GOLDFISH_TTY_CMD_READ_BUFFER
, base
+ GOLDFISH_TTY_CMD
);
84 spin_unlock_irqrestore(&qtty
->lock
, irq_flags
);
85 tty_schedule_flip(&qtty
->port
);
89 static int goldfish_tty_activate(struct tty_port
*port
, struct tty_struct
*tty
)
91 struct goldfish_tty
*qtty
= container_of(port
, struct goldfish_tty
, port
);
92 writel(GOLDFISH_TTY_CMD_INT_ENABLE
, qtty
->base
+ GOLDFISH_TTY_CMD
);
96 static void goldfish_tty_shutdown(struct tty_port
*port
)
98 struct goldfish_tty
*qtty
= container_of(port
, struct goldfish_tty
, port
);
99 writel(GOLDFISH_TTY_CMD_INT_DISABLE
, qtty
->base
+ GOLDFISH_TTY_CMD
);
102 static int goldfish_tty_open(struct tty_struct
* tty
, struct file
* filp
)
104 struct goldfish_tty
*qtty
= &goldfish_ttys
[tty
->index
];
105 return tty_port_open(&qtty
->port
, tty
, filp
);
108 static void goldfish_tty_close(struct tty_struct
* tty
, struct file
* filp
)
110 tty_port_close(tty
->port
, tty
, filp
);
113 static void goldfish_tty_hangup(struct tty_struct
*tty
)
115 tty_port_hangup(tty
->port
);
118 static int goldfish_tty_write(struct tty_struct
* tty
, const unsigned char *buf
, int count
)
120 goldfish_tty_do_write(tty
->index
, buf
, count
);
124 static int goldfish_tty_write_room(struct tty_struct
*tty
)
129 static int goldfish_tty_chars_in_buffer(struct tty_struct
*tty
)
131 struct goldfish_tty
*qtty
= &goldfish_ttys
[tty
->index
];
132 void __iomem
*base
= qtty
->base
;
133 return readl(base
+ GOLDFISH_TTY_BYTES_READY
);
136 static void goldfish_tty_console_write(struct console
*co
, const char *b
, unsigned count
)
138 goldfish_tty_do_write(co
->index
, b
, count
);
141 static struct tty_driver
*goldfish_tty_console_device(struct console
*c
, int *index
)
144 return goldfish_tty_driver
;
147 static int goldfish_tty_console_setup(struct console
*co
, char *options
)
149 if((unsigned)co
->index
> goldfish_tty_line_count
)
151 if(goldfish_ttys
[co
->index
].base
== 0)
156 static struct tty_port_operations goldfish_port_ops
= {
157 .activate
= goldfish_tty_activate
,
158 .shutdown
= goldfish_tty_shutdown
161 static struct tty_operations goldfish_tty_ops
= {
162 .open
= goldfish_tty_open
,
163 .close
= goldfish_tty_close
,
164 .hangup
= goldfish_tty_hangup
,
165 .write
= goldfish_tty_write
,
166 .write_room
= goldfish_tty_write_room
,
167 .chars_in_buffer
= goldfish_tty_chars_in_buffer
,
170 static int goldfish_tty_create_driver(void)
173 struct tty_driver
*tty
;
175 goldfish_ttys
= kzalloc(sizeof(*goldfish_ttys
) * goldfish_tty_line_count
, GFP_KERNEL
);
176 if(goldfish_ttys
== NULL
) {
178 goto err_alloc_goldfish_ttys_failed
;
180 tty
= alloc_tty_driver(goldfish_tty_line_count
);
183 goto err_alloc_tty_driver_failed
;
185 tty
->driver_name
= "goldfish";
187 tty
->type
= TTY_DRIVER_TYPE_SERIAL
;
188 tty
->subtype
= SERIAL_TYPE_NORMAL
;
189 tty
->init_termios
= tty_std_termios
;
190 tty
->flags
= TTY_DRIVER_RESET_TERMIOS
| TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
191 tty_set_operations(tty
, &goldfish_tty_ops
);
192 ret
= tty_register_driver(tty
);
194 goto err_tty_register_driver_failed
;
196 goldfish_tty_driver
= tty
;
199 err_tty_register_driver_failed
:
201 err_alloc_tty_driver_failed
:
202 kfree(goldfish_ttys
);
203 goldfish_ttys
= NULL
;
204 err_alloc_goldfish_ttys_failed
:
208 static void goldfish_tty_delete_driver(void)
210 tty_unregister_driver(goldfish_tty_driver
);
211 put_tty_driver(goldfish_tty_driver
);
212 goldfish_tty_driver
= NULL
;
213 kfree(goldfish_ttys
);
214 goldfish_ttys
= NULL
;
217 static int goldfish_tty_probe(struct platform_device
*pdev
)
219 struct goldfish_tty
*qtty
;
223 struct device
*ttydev
;
227 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
231 base
= ioremap(r
->start
, 0x1000);
233 pr_err("goldfish_tty: unable to remap base\n");
235 r
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
241 if(pdev
->id
>= goldfish_tty_line_count
)
244 mutex_lock(&goldfish_tty_lock
);
245 if(goldfish_tty_current_line_count
== 0) {
246 ret
= goldfish_tty_create_driver();
248 goto err_create_driver_failed
;
250 goldfish_tty_current_line_count
++;
252 qtty
= &goldfish_ttys
[pdev
->id
];
253 spin_lock_init(&qtty
->lock
);
254 tty_port_init(&qtty
->port
);
255 qtty
->port
.ops
= &goldfish_port_ops
;
259 writel(GOLDFISH_TTY_CMD_INT_DISABLE
, base
+ GOLDFISH_TTY_CMD
);
261 ret
= request_irq(irq
, goldfish_tty_interrupt
, IRQF_SHARED
, "goldfish_tty", pdev
);
263 goto err_request_irq_failed
;
266 ttydev
= tty_port_register_device(&qtty
->port
, goldfish_tty_driver
,
267 pdev
->id
, &pdev
->dev
);
269 ret
= PTR_ERR(ttydev
);
270 goto err_tty_register_device_failed
;
273 strcpy(qtty
->console
.name
, "ttyGF");
274 qtty
->console
.write
= goldfish_tty_console_write
;
275 qtty
->console
.device
= goldfish_tty_console_device
;
276 qtty
->console
.setup
= goldfish_tty_console_setup
;
277 qtty
->console
.flags
= CON_PRINTBUFFER
;
278 qtty
->console
.index
= pdev
->id
;
279 register_console(&qtty
->console
);
281 mutex_unlock(&goldfish_tty_lock
);
284 tty_unregister_device(goldfish_tty_driver
, i
);
285 err_tty_register_device_failed
:
287 err_request_irq_failed
:
288 goldfish_tty_current_line_count
--;
289 if(goldfish_tty_current_line_count
== 0)
290 goldfish_tty_delete_driver();
291 err_create_driver_failed
:
292 mutex_unlock(&goldfish_tty_lock
);
298 static int goldfish_tty_remove(struct platform_device
*pdev
)
300 struct goldfish_tty
*qtty
;
302 mutex_lock(&goldfish_tty_lock
);
304 qtty
= &goldfish_ttys
[pdev
->id
];
305 unregister_console(&qtty
->console
);
306 tty_unregister_device(goldfish_tty_driver
, pdev
->id
);
309 free_irq(qtty
->irq
, pdev
);
310 goldfish_tty_current_line_count
--;
311 if(goldfish_tty_current_line_count
== 0)
312 goldfish_tty_delete_driver();
313 mutex_unlock(&goldfish_tty_lock
);
317 static struct platform_driver goldfish_tty_platform_driver
= {
318 .probe
= goldfish_tty_probe
,
319 .remove
= goldfish_tty_remove
,
321 .name
= "goldfish_tty"
325 module_platform_driver(goldfish_tty_platform_driver
);
327 MODULE_LICENSE("GPL v2");