1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/drivers/misc/xillybus_of.c
4  *
5  * Copyright 2011 Xillybus Ltd, http://xillybus.com
6  *
7  * Driver for the Xillybus FPGA/host framework using Open Firmware.
8  */
9 
10 #include <linux/module.h>
11 #include <linux/device.h>
12 #include <linux/slab.h>
13 #include <linux/platform_device.h>
14 #include <linux/of.h>
15 #include <linux/err.h>
16 #include "xillybus.h"
17 
18 MODULE_DESCRIPTION("Xillybus driver for Open Firmware");
19 MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
20 MODULE_VERSION("1.06");
21 MODULE_ALIAS("xillybus_of");
22 MODULE_LICENSE("GPL v2");
23 
24 static const char xillyname[] = "xillybus_of";
25 
26 /* Match table for of_platform binding */
27 static const struct of_device_id xillybus_of_match[] = {
28 	{ .compatible = "xillybus,xillybus-1.00.a", },
29 	{ .compatible = "xlnx,xillybus-1.00.a", }, /* Deprecated */
30 	{}
31 };
32 
33 MODULE_DEVICE_TABLE(of, xillybus_of_match);
34 
35 static void xilly_dma_sync_single_for_cpu_of(struct xilly_endpoint *ep,
36 					     dma_addr_t dma_handle,
37 					     size_t size,
38 					     int direction)
39 {
40 	dma_sync_single_for_cpu(ep->dev, dma_handle, size, direction);
41 }
42 
43 static void xilly_dma_sync_single_for_device_of(struct xilly_endpoint *ep,
44 						dma_addr_t dma_handle,
45 						size_t size,
46 						int direction)
47 {
48 	dma_sync_single_for_device(ep->dev, dma_handle, size, direction);
49 }
50 
51 static void xilly_dma_sync_single_nop(struct xilly_endpoint *ep,
52 				      dma_addr_t dma_handle,
53 				      size_t size,
54 				      int direction)
55 {
56 }
57 
58 static void xilly_of_unmap(void *ptr)
59 {
60 	struct xilly_mapping *data = ptr;
61 
62 	dma_unmap_single(data->device, data->dma_addr,
63 			 data->size, data->direction);
64 
65 	kfree(ptr);
66 }
67 
68 static int xilly_map_single_of(struct xilly_endpoint *ep,
69 			       void *ptr,
70 			       size_t size,
71 			       int direction,
72 			       dma_addr_t *ret_dma_handle
73 	)
74 {
75 	dma_addr_t addr;
76 	struct xilly_mapping *this;
77 
78 	this = kzalloc(sizeof(*this), GFP_KERNEL);
79 	if (!this)
80 		return -ENOMEM;
81 
82 	addr = dma_map_single(ep->dev, ptr, size, direction);
83 
84 	if (dma_mapping_error(ep->dev, addr)) {
85 		kfree(this);
86 		return -ENODEV;
87 	}
88 
89 	this->device = ep->dev;
90 	this->dma_addr = addr;
91 	this->size = size;
92 	this->direction = direction;
93 
94 	*ret_dma_handle = addr;
95 
96 	return devm_add_action_or_reset(ep->dev, xilly_of_unmap, this);
97 }
98 
99 static struct xilly_endpoint_hardware of_hw = {
100 	.owner = THIS_MODULE,
101 	.hw_sync_sgl_for_cpu = xilly_dma_sync_single_for_cpu_of,
102 	.hw_sync_sgl_for_device = xilly_dma_sync_single_for_device_of,
103 	.map_single = xilly_map_single_of,
104 };
105 
106 static struct xilly_endpoint_hardware of_hw_coherent = {
107 	.owner = THIS_MODULE,
108 	.hw_sync_sgl_for_cpu = xilly_dma_sync_single_nop,
109 	.hw_sync_sgl_for_device = xilly_dma_sync_single_nop,
110 	.map_single = xilly_map_single_of,
111 };
112 
113 static int xilly_drv_probe(struct platform_device *op)
114 {
115 	struct device *dev = &op->dev;
116 	struct xilly_endpoint *endpoint;
117 	int rc;
118 	int irq;
119 	struct resource *res;
120 	struct xilly_endpoint_hardware *ephw = &of_hw;
121 
122 	if (of_property_read_bool(dev->of_node, "dma-coherent"))
123 		ephw = &of_hw_coherent;
124 
125 	endpoint = xillybus_init_endpoint(NULL, dev, ephw);
126 
127 	if (!endpoint)
128 		return -ENOMEM;
129 
130 	dev_set_drvdata(dev, endpoint);
131 
132 	res = platform_get_resource(op, IORESOURCE_MEM, 0);
133 	endpoint->registers = devm_ioremap_resource(dev, res);
134 
135 	if (IS_ERR(endpoint->registers))
136 		return PTR_ERR(endpoint->registers);
137 
138 	irq = platform_get_irq(op, 0);
139 
140 	rc = devm_request_irq(dev, irq, xillybus_isr, 0, xillyname, endpoint);
141 
142 	if (rc) {
143 		dev_err(endpoint->dev,
144 			"Failed to register IRQ handler. Aborting.\n");
145 		return -ENODEV;
146 	}
147 
148 	return xillybus_endpoint_discovery(endpoint);
149 }
150 
151 static int xilly_drv_remove(struct platform_device *op)
152 {
153 	struct device *dev = &op->dev;
154 	struct xilly_endpoint *endpoint = dev_get_drvdata(dev);
155 
156 	xillybus_endpoint_remove(endpoint);
157 
158 	return 0;
159 }
160 
161 static struct platform_driver xillybus_platform_driver = {
162 	.probe = xilly_drv_probe,
163 	.remove = xilly_drv_remove,
164 	.driver = {
165 		.name = xillyname,
166 		.of_match_table = xillybus_of_match,
167 	},
168 };
169 
170 module_platform_driver(xillybus_platform_driver);
171