]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/xillybus/xillybus_of.c
Merge tag 'microblaze-3.16-rc1' of git://git.monstr.eu/linux-2.6-microblaze into...
[karo-tx-linux.git] / drivers / staging / xillybus / xillybus_of.c
1 /*
2  * linux/drivers/misc/xillybus_of.c
3  *
4  * Copyright 2011 Xillybus Ltd, http://xillybus.com
5  *
6  * Driver for the Xillybus FPGA/host framework using Open Firmware.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the smems of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  */
12
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/platform_device.h>
17 #include <linux/of.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_address.h>
20 #include <linux/of_device.h>
21 #include <linux/of_platform.h>
22 #include <linux/err.h>
23 #include "xillybus.h"
24
25 MODULE_DESCRIPTION("Xillybus driver for Open Firmware");
26 MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
27 MODULE_VERSION("1.06");
28 MODULE_ALIAS("xillybus_of");
29 MODULE_LICENSE("GPL v2");
30
31 static const char xillyname[] = "xillybus_of";
32
33 /* Match table for of_platform binding */
34 static struct of_device_id xillybus_of_match[] = {
35         { .compatible = "xillybus,xillybus-1.00.a", },
36         { .compatible = "xlnx,xillybus-1.00.a", }, /* Deprecated */
37         {}
38 };
39
40 MODULE_DEVICE_TABLE(of, xillybus_of_match);
41
42 static void xilly_dma_sync_single_for_cpu_of(struct xilly_endpoint *ep,
43                                              dma_addr_t dma_handle,
44                                              size_t size,
45                                              int direction)
46 {
47         dma_sync_single_for_cpu(ep->dev, dma_handle, size, direction);
48 }
49
50 static void xilly_dma_sync_single_for_device_of(struct xilly_endpoint *ep,
51                                                 dma_addr_t dma_handle,
52                                                 size_t size,
53                                                 int direction)
54 {
55         dma_sync_single_for_device(ep->dev, dma_handle, size, direction);
56 }
57
58 static void xilly_dma_sync_single_nop(struct xilly_endpoint *ep,
59                                       dma_addr_t dma_handle,
60                                       size_t size,
61                                       int direction)
62 {
63 }
64
65 static dma_addr_t xilly_map_single_of(struct xilly_cleanup *mem,
66                                       struct xilly_endpoint *ep,
67                                       void *ptr,
68                                       size_t size,
69                                       int direction
70         )
71 {
72
73         dma_addr_t addr = 0;
74         struct xilly_dma *this;
75
76         this = kmalloc(sizeof(struct xilly_dma), GFP_KERNEL);
77         if (!this)
78                 return 0;
79
80         addr = dma_map_single(ep->dev, ptr, size, direction);
81         this->direction = direction;
82
83         if (dma_mapping_error(ep->dev, addr)) {
84                 kfree(this);
85                 return 0;
86         }
87
88         this->dma_addr = addr;
89         this->dev = ep->dev;
90         this->size = size;
91
92         list_add_tail(&this->node, &mem->to_unmap);
93
94         return addr;
95 }
96
97 static void xilly_unmap_single_of(struct xilly_dma *entry)
98 {
99         dma_unmap_single(entry->dev,
100                          entry->dma_addr,
101                          entry->size,
102                          entry->direction);
103 }
104
105 static struct xilly_endpoint_hardware of_hw = {
106         .owner = THIS_MODULE,
107         .hw_sync_sgl_for_cpu = xilly_dma_sync_single_for_cpu_of,
108         .hw_sync_sgl_for_device = xilly_dma_sync_single_for_device_of,
109         .map_single = xilly_map_single_of,
110         .unmap_single = xilly_unmap_single_of
111 };
112
113 static struct xilly_endpoint_hardware of_hw_coherent = {
114         .owner = THIS_MODULE,
115         .hw_sync_sgl_for_cpu = xilly_dma_sync_single_nop,
116         .hw_sync_sgl_for_device = xilly_dma_sync_single_nop,
117         .map_single = xilly_map_single_of,
118         .unmap_single = xilly_unmap_single_of
119 };
120
121 static int xilly_drv_probe(struct platform_device *op)
122 {
123         struct device *dev = &op->dev;
124         struct xilly_endpoint *endpoint;
125         int rc = 0;
126         int irq;
127         struct resource res;
128         struct xilly_endpoint_hardware *ephw = &of_hw;
129
130         if (of_property_read_bool(dev->of_node, "dma-coherent"))
131                 ephw = &of_hw_coherent;
132
133         endpoint = xillybus_init_endpoint(NULL, dev, ephw);
134
135         if (!endpoint)
136                 return -ENOMEM;
137
138         dev_set_drvdata(dev, endpoint);
139
140         rc = of_address_to_resource(dev->of_node, 0, &res);
141         if (rc) {
142                 dev_warn(endpoint->dev,
143                          "Failed to obtain device tree resource\n");
144                 return rc;
145         }
146
147         endpoint->registers = devm_ioremap_resource(dev, &res);
148
149         if (IS_ERR(endpoint->registers))
150                 return PTR_ERR(endpoint->registers);
151
152         irq = irq_of_parse_and_map(dev->of_node, 0);
153
154         rc = devm_request_irq(dev, irq, xillybus_isr, 0, xillyname, endpoint);
155
156         if (rc) {
157                 dev_err(endpoint->dev,
158                         "Failed to register IRQ handler. Aborting.\n");
159                 return -ENODEV;
160         }
161
162         rc = xillybus_endpoint_discovery(endpoint);
163
164         if (!rc)
165                 return 0;
166
167         xillybus_do_cleanup(&endpoint->cleanup, endpoint);
168
169         return rc;
170 }
171
172 static int xilly_drv_remove(struct platform_device *op)
173 {
174         struct device *dev = &op->dev;
175         struct xilly_endpoint *endpoint = dev_get_drvdata(dev);
176
177         xillybus_endpoint_remove(endpoint);
178
179         xillybus_do_cleanup(&endpoint->cleanup, endpoint);
180
181         return 0;
182 }
183
184 static struct platform_driver xillybus_platform_driver = {
185         .probe = xilly_drv_probe,
186         .remove = xilly_drv_remove,
187         .driver = {
188                 .name = xillyname,
189                 .owner = THIS_MODULE,
190                 .of_match_table = xillybus_of_match,
191         },
192 };
193
194 module_platform_driver(xillybus_platform_driver);