d8c8a8157b96fe958e3a121591dcdc7d649e42a6
[linux-2.6.git] / drivers / serial / 8250_acpi.c
1 /*
2  * Copyright (c) 2002-2003 Matthew Wilcox for Hewlett-Packard
3  * Copyright (C) 2004 Hewlett-Packard Co
4  *      Bjorn Helgaas <bjorn.helgaas@hp.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11
12 #include <linux/acpi.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/tty.h>
16 #include <linux/serial.h>
17 #include <linux/serial_core.h>
18
19 #include <acpi/acpi_bus.h>
20
21 #include <asm/io.h>
22 #include <asm/serial.h>
23
24 struct serial_private {
25         int     line;
26         void    *iomem_base;
27 };
28
29 static acpi_status acpi_serial_mmio(struct serial_struct *req,
30                                     struct acpi_resource_address64 *addr)
31 {
32         unsigned long size;
33
34         size = addr->max_address_range - addr->min_address_range + 1;
35         req->iomap_base = addr->min_address_range;
36         req->iomem_base = ioremap(req->iomap_base, size);
37         if (!req->iomem_base) {
38                 printk(KERN_ERR "%s: couldn't ioremap 0x%lx-0x%lx\n",
39                         __FUNCTION__, req->iomap_base, req->iomap_base + size);
40                 return AE_ERROR;
41         }
42         req->io_type = SERIAL_IO_MEM;
43         return AE_OK;
44 }
45
46 static acpi_status acpi_serial_port(struct serial_struct *req,
47                                     struct acpi_resource_io *io)
48 {
49         if (io->range_length) {
50                 req->port = io->min_base_address;
51                 req->io_type = SERIAL_IO_PORT;
52         } else
53                 printk(KERN_ERR "%s: zero-length IO port range?\n", __FUNCTION__);
54         return AE_OK;
55 }
56
57 static acpi_status acpi_serial_ext_irq(struct serial_struct *req,
58                                        struct acpi_resource_ext_irq *ext_irq)
59 {
60         if (ext_irq->number_of_interrupts > 0) {
61 #ifdef CONFIG_IA64
62                 req->irq = acpi_register_irq(ext_irq->interrupts[0],
63                           ext_irq->active_high_low, ext_irq->edge_level);
64 #else
65                 req->irq = ext_irq->interrupts[0];
66 #endif
67         }
68         return AE_OK;
69 }
70
71 static acpi_status acpi_serial_irq(struct serial_struct *req,
72                                    struct acpi_resource_irq *irq)
73 {
74         if (irq->number_of_interrupts > 0) {
75 #ifdef CONFIG_IA64
76                 req->irq = acpi_register_irq(irq->interrupts[0],
77                           irq->active_high_low, irq->edge_level);
78 #else
79                 req->irq = irq->interrupts[0];
80 #endif
81         }
82         return AE_OK;
83 }
84
85 static acpi_status acpi_serial_resource(struct acpi_resource *res, void *data)
86 {
87         struct serial_struct *serial_req = (struct serial_struct *) data;
88         struct acpi_resource_address64 addr;
89         acpi_status status;
90
91         status = acpi_resource_to_address64(res, &addr);
92         if (ACPI_SUCCESS(status))
93                 return acpi_serial_mmio(serial_req, &addr);
94         else if (res->id == ACPI_RSTYPE_IO)
95                 return acpi_serial_port(serial_req, &res->data.io);
96         else if (res->id == ACPI_RSTYPE_EXT_IRQ)
97                 return acpi_serial_ext_irq(serial_req, &res->data.extended_irq);
98         else if (res->id == ACPI_RSTYPE_IRQ)
99                 return acpi_serial_irq(serial_req, &res->data.irq);
100         return AE_OK;
101 }
102
103 static int acpi_serial_add(struct acpi_device *device)
104 {
105         struct serial_private *priv;
106         acpi_status status;
107         struct serial_struct serial_req;
108         int result;
109
110         memset(&serial_req, 0, sizeof(serial_req));
111
112         priv = kmalloc(sizeof(struct serial_private), GFP_KERNEL);
113         if (!priv) {
114                 result = -ENOMEM;
115                 goto fail;
116         }
117         memset(priv, 0, sizeof(*priv));
118
119         status = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
120                                      acpi_serial_resource, &serial_req);
121         if (ACPI_FAILURE(status)) {
122                 result = -ENODEV;
123                 goto fail;
124         }
125
126         if (serial_req.iomem_base)
127                 priv->iomem_base = serial_req.iomem_base;
128         else if (!serial_req.port) {
129                 printk(KERN_ERR "%s: no iomem or port address in %s _CRS\n",
130                         __FUNCTION__, device->pnp.bus_id);
131                 result = -ENODEV;
132                 goto fail;
133         }
134
135         serial_req.baud_base = BASE_BAUD;
136         serial_req.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF | UPF_RESOURCES;
137
138         priv->line = register_serial(&serial_req);
139         if (priv->line < 0) {
140                 printk(KERN_WARNING "Couldn't register serial port %s: %d\n",
141                         device->pnp.bus_id, priv->line);
142                 result = -ENODEV;
143                 goto fail;
144         }
145
146         acpi_driver_data(device) = priv;
147         return 0;
148
149 fail:
150         if (serial_req.iomem_base)
151                 iounmap(serial_req.iomem_base);
152         kfree(priv);
153
154         return result;
155 }
156
157 static int acpi_serial_remove(struct acpi_device *device, int type)
158 {
159         struct serial_private *priv;
160
161         if (!device || !acpi_driver_data(device))
162                 return -EINVAL;
163
164         priv = acpi_driver_data(device);
165         unregister_serial(priv->line);
166         if (priv->iomem_base)
167                 iounmap(priv->iomem_base);
168         kfree(priv);
169
170         return 0;
171 }
172
173 static struct acpi_driver acpi_serial_driver = {
174         .name =         "serial",
175         .class =        "",
176         .ids =          "PNP0501",
177         .ops =  {
178                 .add =          acpi_serial_add,
179                 .remove =       acpi_serial_remove,
180         },
181 };
182
183 static int __init acpi_serial_init(void)
184 {
185         return acpi_bus_register_driver(&acpi_serial_driver);
186 }
187
188 static void __exit acpi_serial_exit(void)
189 {
190         acpi_bus_unregister_driver(&acpi_serial_driver);
191 }
192
193 module_init(acpi_serial_init);
194 module_exit(acpi_serial_exit);
195
196 MODULE_LICENSE("GPL");
197 MODULE_DESCRIPTION("Generic 8250/16x50 ACPI serial driver");