Commit | Line | Data |
---|---|---|
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> | |
24 | ||
25 | enum { | |
26 | GOLDFISH_TTY_PUT_CHAR = 0x00, | |
27 | GOLDFISH_TTY_BYTES_READY = 0x04, | |
28 | GOLDFISH_TTY_CMD = 0x08, | |
29 | ||
30 | GOLDFISH_TTY_DATA_PTR = 0x10, | |
31 | GOLDFISH_TTY_DATA_LEN = 0x14, | |
32 | ||
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, | |
37 | }; | |
38 | ||
39 | struct 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 | ||
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; | |
53 | ||
54 | static 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); | |
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); | |
64 | } | |
65 | ||
66 | static irqreturn_t goldfish_tty_interrupt(int irq, void *dev_id) | |
67 | { | |
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; | |
72 | unsigned char *buf; | |
73 | u32 count; | |
666b7793 AH |
74 | |
75 | count = readl(base + GOLDFISH_TTY_BYTES_READY); | |
76 | if(count == 0) | |
77 | return IRQ_NONE; | |
78 | ||
ebcf0981 AC |
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); | |
666b7793 AH |
86 | return IRQ_HANDLED; |
87 | } | |
88 | ||
89 | static int goldfish_tty_activate(struct tty_port *port, struct tty_struct *tty) | |
90 | { | |
91 | struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, port); | |
92 | writel(GOLDFISH_TTY_CMD_INT_ENABLE, qtty->base + GOLDFISH_TTY_CMD); | |
93 | return 0; | |
94 | } | |
95 | ||
96 | static void goldfish_tty_shutdown(struct tty_port *port) | |
97 | { | |
98 | struct goldfish_tty *qtty = container_of(port, struct goldfish_tty, port); | |
99 | writel(GOLDFISH_TTY_CMD_INT_DISABLE, qtty->base + GOLDFISH_TTY_CMD); | |
100 | } | |
101 | ||
102 | static int goldfish_tty_open(struct tty_struct * tty, struct file * filp) | |
103 | { | |
104 | struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; | |
105 | return tty_port_open(&qtty->port, tty, filp); | |
106 | } | |
107 | ||
108 | static void goldfish_tty_close(struct tty_struct * tty, struct file * filp) | |
109 | { | |
110 | tty_port_close(tty->port, tty, filp); | |
111 | } | |
112 | ||
113 | static void goldfish_tty_hangup(struct tty_struct *tty) | |
114 | { | |
115 | tty_port_hangup(tty->port); | |
116 | } | |
117 | ||
118 | static int goldfish_tty_write(struct tty_struct * tty, const unsigned char *buf, int count) | |
119 | { | |
120 | goldfish_tty_do_write(tty->index, buf, count); | |
121 | return count; | |
122 | } | |
123 | ||
124 | static int goldfish_tty_write_room(struct tty_struct *tty) | |
125 | { | |
126 | return 0x10000; | |
127 | } | |
128 | ||
129 | static int goldfish_tty_chars_in_buffer(struct tty_struct *tty) | |
130 | { | |
131 | struct goldfish_tty *qtty = &goldfish_ttys[tty->index]; | |
132 | void __iomem *base = qtty->base; | |
133 | return readl(base + GOLDFISH_TTY_BYTES_READY); | |
134 | } | |
135 | ||
136 | static void goldfish_tty_console_write(struct console *co, const char *b, unsigned count) | |
137 | { | |
138 | goldfish_tty_do_write(co->index, b, count); | |
139 | } | |
140 | ||
141 | static struct tty_driver *goldfish_tty_console_device(struct console *c, int *index) | |
142 | { | |
143 | *index = c->index; | |
144 | return goldfish_tty_driver; | |
145 | } | |
146 | ||
147 | static int goldfish_tty_console_setup(struct console *co, char *options) | |
148 | { | |
149 | if((unsigned)co->index > goldfish_tty_line_count) | |
150 | return -ENODEV; | |
151 | if(goldfish_ttys[co->index].base == 0) | |
152 | return -ENODEV; | |
153 | return 0; | |
154 | } | |
155 | ||
156 | static struct tty_port_operations goldfish_port_ops = { | |
157 | .activate = goldfish_tty_activate, | |
158 | .shutdown = goldfish_tty_shutdown | |
159 | }; | |
160 | ||
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, | |
168 | }; | |
169 | ||
170 | static int goldfish_tty_create_driver(void) | |
171 | { | |
172 | int ret; | |
173 | struct tty_driver *tty; | |
174 | ||
175 | goldfish_ttys = kzalloc(sizeof(*goldfish_ttys) * goldfish_tty_line_count, GFP_KERNEL); | |
176 | if(goldfish_ttys == NULL) { | |
177 | ret = -ENOMEM; | |
178 | goto err_alloc_goldfish_ttys_failed; | |
179 | } | |
180 | tty = alloc_tty_driver(goldfish_tty_line_count); | |
181 | if(tty == NULL) { | |
182 | ret = -ENOMEM; | |
183 | goto err_alloc_tty_driver_failed; | |
184 | } | |
185 | tty->driver_name = "goldfish"; | |
186 | tty->name = "ttyGF"; | |
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); | |
193 | if(ret) | |
194 | goto err_tty_register_driver_failed; | |
195 | ||
196 | goldfish_tty_driver = tty; | |
197 | return 0; | |
198 | ||
199 | err_tty_register_driver_failed: | |
200 | put_tty_driver(tty); | |
201 | err_alloc_tty_driver_failed: | |
202 | kfree(goldfish_ttys); | |
203 | goldfish_ttys = NULL; | |
204 | err_alloc_goldfish_ttys_failed: | |
205 | return ret; | |
206 | } | |
207 | ||
208 | static void goldfish_tty_delete_driver(void) | |
209 | { | |
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; | |
215 | } | |
216 | ||
217 | static int goldfish_tty_probe(struct platform_device *pdev) | |
218 | { | |
219 | struct goldfish_tty *qtty; | |
220 | int ret = -EINVAL; | |
221 | int i; | |
222 | struct resource *r; | |
223 | struct device *ttydev; | |
224 | void __iomem *base; | |
225 | u32 irq; | |
226 | ||
227 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
228 | if(r == NULL) | |
229 | return -EINVAL; | |
230 | ||
231 | base = ioremap(r->start, 0x1000); | |
232 | if (base == NULL) | |
233 | pr_err("goldfish_tty: unable to remap base\n"); | |
234 | ||
235 | r = platform_get_resource(pdev, IORESOURCE_IRQ, 0); | |
236 | if(r == NULL) | |
237 | goto err_unmap; | |
238 | ||
239 | irq = r->start; | |
240 | ||
241 | if(pdev->id >= goldfish_tty_line_count) | |
242 | goto err_unmap; | |
243 | ||
244 | mutex_lock(&goldfish_tty_lock); | |
245 | if(goldfish_tty_current_line_count == 0) { | |
246 | ret = goldfish_tty_create_driver(); | |
247 | if(ret) | |
248 | goto err_create_driver_failed; | |
249 | } | |
250 | goldfish_tty_current_line_count++; | |
251 | ||
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; | |
256 | qtty->base = base; | |
257 | qtty->irq = irq; | |
258 | ||
259 | writel(GOLDFISH_TTY_CMD_INT_DISABLE, base + GOLDFISH_TTY_CMD); | |
260 | ||
261 | ret = request_irq(irq, goldfish_tty_interrupt, IRQF_SHARED, "goldfish_tty", pdev); | |
262 | if(ret) | |
263 | goto err_request_irq_failed; | |
264 | ||
265 | ||
266 | ttydev = tty_port_register_device(&qtty->port, goldfish_tty_driver, | |
267 | pdev->id, &pdev->dev); | |
268 | if(IS_ERR(ttydev)) { | |
269 | ret = PTR_ERR(ttydev); | |
270 | goto err_tty_register_device_failed; | |
271 | } | |
272 | ||
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); | |
280 | ||
281 | mutex_unlock(&goldfish_tty_lock); | |
282 | return 0; | |
283 | ||
284 | tty_unregister_device(goldfish_tty_driver, i); | |
285 | err_tty_register_device_failed: | |
286 | free_irq(irq, pdev); | |
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); | |
293 | err_unmap: | |
294 | iounmap(base); | |
295 | return ret; | |
296 | } | |
297 | ||
298 | static int goldfish_tty_remove(struct platform_device *pdev) | |
299 | { | |
300 | struct goldfish_tty *qtty; | |
301 | ||
302 | mutex_lock(&goldfish_tty_lock); | |
303 | ||
304 | qtty = &goldfish_ttys[pdev->id]; | |
305 | unregister_console(&qtty->console); | |
306 | tty_unregister_device(goldfish_tty_driver, pdev->id); | |
307 | iounmap(qtty->base); | |
308 | qtty->base = 0; | |
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); | |
314 | return 0; | |
315 | } | |
316 | ||
317 | static struct platform_driver goldfish_tty_platform_driver = { | |
318 | .probe = goldfish_tty_probe, | |
319 | .remove = goldfish_tty_remove, | |
320 | .driver = { | |
321 | .name = "goldfish_tty" | |
322 | } | |
323 | }; | |
324 | ||
325 | module_platform_driver(goldfish_tty_platform_driver); | |
326 | ||
327 | MODULE_LICENSE("GPL v2"); |