blob: 4d6625ccb48f2f75c838919e34986b402acd8ef1 [file] [log] [blame]
Eli Billauer48bae052013-06-24 18:55:47 +03001/*
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>
Eli Billauer92674622014-05-11 19:53:46 +030018#include <linux/err.h>
Eli Billauer48bae052013-06-24 18:55:47 +030019#include "xillybus.h"
20
21MODULE_DESCRIPTION("Xillybus driver for Open Firmware");
22MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
23MODULE_VERSION("1.06");
24MODULE_ALIAS("xillybus_of");
25MODULE_LICENSE("GPL v2");
26
Eli Billauere71042f2013-07-27 00:24:00 +030027static const char xillyname[] = "xillybus_of";
28
Eli Billauer48bae052013-06-24 18:55:47 +030029/* Match table for of_platform binding */
Fabian Frederickda2ff522015-03-16 20:17:13 +010030static const struct of_device_id xillybus_of_match[] = {
Eli Billauer24b33c92013-12-30 23:16:24 +020031 { .compatible = "xillybus,xillybus-1.00.a", },
32 { .compatible = "xlnx,xillybus-1.00.a", }, /* Deprecated */
Eli Billauer48bae052013-06-24 18:55:47 +030033 {}
34};
35
36MODULE_DEVICE_TABLE(of, xillybus_of_match);
37
38static void xilly_dma_sync_single_for_cpu_of(struct xilly_endpoint *ep,
39 dma_addr_t dma_handle,
40 size_t size,
41 int direction)
42{
43 dma_sync_single_for_cpu(ep->dev, dma_handle, size, direction);
44}
45
46static void xilly_dma_sync_single_for_device_of(struct xilly_endpoint *ep,
47 dma_addr_t dma_handle,
48 size_t size,
49 int direction)
50{
51 dma_sync_single_for_device(ep->dev, dma_handle, size, direction);
52}
53
Eli Billauercc212552013-12-30 23:16:25 +020054static void xilly_dma_sync_single_nop(struct xilly_endpoint *ep,
55 dma_addr_t dma_handle,
56 size_t size,
57 int direction)
58{
59}
60
Eli Billauer525be902014-06-21 14:07:12 +030061static void xilly_of_unmap(void *ptr)
62{
63 struct xilly_mapping *data = ptr;
64
65 dma_unmap_single(data->device, data->dma_addr,
66 data->size, data->direction);
67
68 kfree(ptr);
69}
70
71static int xilly_map_single_of(struct xilly_endpoint *ep,
72 void *ptr,
73 size_t size,
74 int direction,
75 dma_addr_t *ret_dma_handle
Eli Billauer48bae052013-06-24 18:55:47 +030076 )
77{
Eli Billauer525be902014-06-21 14:07:12 +030078 dma_addr_t addr;
79 struct xilly_mapping *this;
Eli Billauer48bae052013-06-24 18:55:47 +030080
Eli Billauer525be902014-06-21 14:07:12 +030081 this = kzalloc(sizeof(*this), GFP_KERNEL);
Eli Billauer48bae052013-06-24 18:55:47 +030082 if (!this)
Eli Billauer525be902014-06-21 14:07:12 +030083 return -ENOMEM;
Eli Billauer48bae052013-06-24 18:55:47 +030084
85 addr = dma_map_single(ep->dev, ptr, size, direction);
Eli Billauer48bae052013-06-24 18:55:47 +030086
87 if (dma_mapping_error(ep->dev, addr)) {
88 kfree(this);
Eli Billauer525be902014-06-21 14:07:12 +030089 return -ENODEV;
Eli Billauer48bae052013-06-24 18:55:47 +030090 }
91
Eli Billauer525be902014-06-21 14:07:12 +030092 this->device = ep->dev;
Eli Billauer48bae052013-06-24 18:55:47 +030093 this->dma_addr = addr;
Eli Billauer48bae052013-06-24 18:55:47 +030094 this->size = size;
Eli Billauer525be902014-06-21 14:07:12 +030095 this->direction = direction;
Eli Billauer48bae052013-06-24 18:55:47 +030096
Eli Billauer525be902014-06-21 14:07:12 +030097 *ret_dma_handle = addr;
Eli Billauer48bae052013-06-24 18:55:47 +030098
Sudip Mukherjeebd83a4a2016-04-30 17:13:20 +010099 return devm_add_action_or_reset(ep->dev, xilly_of_unmap, this);
Eli Billauer48bae052013-06-24 18:55:47 +0300100}
101
102static struct xilly_endpoint_hardware of_hw = {
103 .owner = THIS_MODULE,
Eli Billauer7ee9ded2013-07-31 11:22:43 +0300104 .hw_sync_sgl_for_cpu = xilly_dma_sync_single_for_cpu_of,
105 .hw_sync_sgl_for_device = xilly_dma_sync_single_for_device_of,
Eli Billauer48bae052013-06-24 18:55:47 +0300106 .map_single = xilly_map_single_of,
Eli Billauer48bae052013-06-24 18:55:47 +0300107};
108
Eli Billauercc212552013-12-30 23:16:25 +0200109static struct xilly_endpoint_hardware of_hw_coherent = {
110 .owner = THIS_MODULE,
111 .hw_sync_sgl_for_cpu = xilly_dma_sync_single_nop,
112 .hw_sync_sgl_for_device = xilly_dma_sync_single_nop,
113 .map_single = xilly_map_single_of,
Eli Billauercc212552013-12-30 23:16:25 +0200114};
115
Eli Billauer48bae052013-06-24 18:55:47 +0300116static int xilly_drv_probe(struct platform_device *op)
117{
118 struct device *dev = &op->dev;
119 struct xilly_endpoint *endpoint;
Eli Billauer40931bb2014-09-04 17:47:48 +0300120 int rc;
Eli Billauer48bae052013-06-24 18:55:47 +0300121 int irq;
Rob Herring74d83a52018-01-04 16:45:39 -0600122 struct resource *res;
Eli Billauercc212552013-12-30 23:16:25 +0200123 struct xilly_endpoint_hardware *ephw = &of_hw;
Eli Billauer48bae052013-06-24 18:55:47 +0300124
Eli Billauercc212552013-12-30 23:16:25 +0200125 if (of_property_read_bool(dev->of_node, "dma-coherent"))
126 ephw = &of_hw_coherent;
127
128 endpoint = xillybus_init_endpoint(NULL, dev, ephw);
Eli Billauer48bae052013-06-24 18:55:47 +0300129
130 if (!endpoint)
131 return -ENOMEM;
132
133 dev_set_drvdata(dev, endpoint);
134
Rob Herring74d83a52018-01-04 16:45:39 -0600135 res = platform_get_resource(op, IORESOURCE_MEM, 0);
136 endpoint->registers = devm_ioremap_resource(dev, res);
Eli Billauer48bae052013-06-24 18:55:47 +0300137
Eli Billauer92674622014-05-11 19:53:46 +0300138 if (IS_ERR(endpoint->registers))
139 return PTR_ERR(endpoint->registers);
Eli Billauer48bae052013-06-24 18:55:47 +0300140
Rob Herring74d83a52018-01-04 16:45:39 -0600141 irq = platform_get_irq(op, 0);
Eli Billauer48bae052013-06-24 18:55:47 +0300142
Eli Billauer92674622014-05-11 19:53:46 +0300143 rc = devm_request_irq(dev, irq, xillybus_isr, 0, xillyname, endpoint);
Eli Billauer48bae052013-06-24 18:55:47 +0300144
145 if (rc) {
Eli Billauer1a2f9a92013-10-19 01:02:27 +0300146 dev_err(endpoint->dev,
147 "Failed to register IRQ handler. Aborting.\n");
Eli Billauer92674622014-05-11 19:53:46 +0300148 return -ENODEV;
Eli Billauer48bae052013-06-24 18:55:47 +0300149 }
150
Eli Billauer525be902014-06-21 14:07:12 +0300151 return xillybus_endpoint_discovery(endpoint);
Eli Billauer48bae052013-06-24 18:55:47 +0300152}
153
154static int xilly_drv_remove(struct platform_device *op)
155{
156 struct device *dev = &op->dev;
157 struct xilly_endpoint *endpoint = dev_get_drvdata(dev);
Eli Billauer48bae052013-06-24 18:55:47 +0300158
159 xillybus_endpoint_remove(endpoint);
160
Eli Billauer48bae052013-06-24 18:55:47 +0300161 return 0;
162}
163
164static struct platform_driver xillybus_platform_driver = {
165 .probe = xilly_drv_probe,
166 .remove = xilly_drv_remove,
167 .driver = {
168 .name = xillyname,
Eli Billauer48bae052013-06-24 18:55:47 +0300169 .of_match_table = xillybus_of_match,
170 },
171};
172
Sachin Kamat7a620a62013-10-10 09:30:04 +0530173module_platform_driver(xillybus_platform_driver);