drivers/tty/hvc/hvc_tile.c: use PTR_ERR_OR_ZERO
[linux-2.6/btrfs-unstable.git] / drivers / tty / hvc / hvc_tile.c
blobaf4e3d1bac2a30fc85aba9a15ceb6a801b9c0720
1 /*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
14 * Tilera TILE Processor hypervisor console
17 #include <linux/console.h>
18 #include <linux/delay.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/irq.h>
23 #include <linux/moduleparam.h>
24 #include <linux/platform_device.h>
25 #include <linux/types.h>
27 #include <asm/setup.h>
28 #include <arch/sim_def.h>
30 #include <hv/hypervisor.h>
32 #include "hvc_console.h"
34 static int use_sim_console;
35 static int __init sim_console(char *str)
37 use_sim_console = 1;
38 return 0;
40 early_param("sim_console", sim_console);
42 int tile_console_write(const char *buf, int count)
44 if (unlikely(use_sim_console)) {
45 int i;
46 for (i = 0; i < count; ++i)
47 __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_PUTC |
48 (buf[i] << _SIM_CONTROL_OPERATOR_BITS));
49 __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_PUTC |
50 (SIM_PUTC_FLUSH_BINARY <<
51 _SIM_CONTROL_OPERATOR_BITS));
52 return 0;
53 } else {
54 return hv_console_write((HV_VirtAddr)buf, count);
58 static int hvc_tile_put_chars(uint32_t vt, const char *buf, int count)
60 return tile_console_write(buf, count);
63 static int hvc_tile_get_chars(uint32_t vt, char *buf, int count)
65 int i, c;
67 for (i = 0; i < count; ++i) {
68 c = hv_console_read_if_ready();
69 if (c < 0)
70 break;
71 buf[i] = c;
74 return i;
77 #ifdef __tilegx__
79 * IRQ based callbacks.
81 static int hvc_tile_notifier_add_irq(struct hvc_struct *hp, int irq)
83 int rc;
84 int cpu = raw_smp_processor_id(); /* Choose an arbitrary cpu */
85 HV_Coord coord = { .x = cpu_x(cpu), .y = cpu_y(cpu) };
87 rc = notifier_add_irq(hp, irq);
88 if (rc)
89 return rc;
92 * Request that the hypervisor start sending us interrupts.
93 * If the hypervisor returns an error, we still return 0, so that
94 * we can fall back to polling.
96 if (hv_console_set_ipi(KERNEL_PL, irq, coord) < 0)
97 notifier_del_irq(hp, irq);
99 return 0;
102 static void hvc_tile_notifier_del_irq(struct hvc_struct *hp, int irq)
104 HV_Coord coord = { 0, 0 };
106 /* Tell the hypervisor to stop sending us interrupts. */
107 hv_console_set_ipi(KERNEL_PL, -1, coord);
109 notifier_del_irq(hp, irq);
112 static void hvc_tile_notifier_hangup_irq(struct hvc_struct *hp, int irq)
114 hvc_tile_notifier_del_irq(hp, irq);
116 #endif
118 static const struct hv_ops hvc_tile_get_put_ops = {
119 .get_chars = hvc_tile_get_chars,
120 .put_chars = hvc_tile_put_chars,
121 #ifdef __tilegx__
122 .notifier_add = hvc_tile_notifier_add_irq,
123 .notifier_del = hvc_tile_notifier_del_irq,
124 .notifier_hangup = hvc_tile_notifier_hangup_irq,
125 #endif
129 #ifdef __tilegx__
130 static int hvc_tile_probe(struct platform_device *pdev)
132 struct hvc_struct *hp;
133 int tile_hvc_irq;
135 /* Create our IRQ and register it. */
136 tile_hvc_irq = create_irq();
137 if (tile_hvc_irq < 0)
138 return -ENXIO;
140 tile_irq_activate(tile_hvc_irq, TILE_IRQ_PERCPU);
141 hp = hvc_alloc(0, tile_hvc_irq, &hvc_tile_get_put_ops, 128);
142 if (IS_ERR(hp)) {
143 destroy_irq(tile_hvc_irq);
144 return PTR_ERR(hp);
146 dev_set_drvdata(&pdev->dev, hp);
148 return 0;
151 static int hvc_tile_remove(struct platform_device *pdev)
153 int rc;
154 struct hvc_struct *hp = dev_get_drvdata(&pdev->dev);
156 rc = hvc_remove(hp);
157 if (rc == 0)
158 destroy_irq(hp->data);
160 return rc;
163 static void hvc_tile_shutdown(struct platform_device *pdev)
165 struct hvc_struct *hp = dev_get_drvdata(&pdev->dev);
167 hvc_tile_notifier_del_irq(hp, hp->data);
170 static struct platform_device hvc_tile_pdev = {
171 .name = "hvc-tile",
172 .id = 0,
175 static struct platform_driver hvc_tile_driver = {
176 .probe = hvc_tile_probe,
177 .remove = hvc_tile_remove,
178 .shutdown = hvc_tile_shutdown,
179 .driver = {
180 .name = "hvc-tile",
181 .owner = THIS_MODULE,
184 #endif
186 static int __init hvc_tile_console_init(void)
188 hvc_instantiate(0, 0, &hvc_tile_get_put_ops);
189 add_preferred_console("hvc", 0, NULL);
190 return 0;
192 console_initcall(hvc_tile_console_init);
194 static int __init hvc_tile_init(void)
196 #ifndef __tilegx__
197 struct hvc_struct *hp;
198 hp = hvc_alloc(0, 0, &hvc_tile_get_put_ops, 128);
199 return PTR_ERR_OR_ZERO(hp);
200 #else
201 platform_device_register(&hvc_tile_pdev);
202 return platform_driver_register(&hvc_tile_driver);
203 #endif
205 device_initcall(hvc_tile_init);