xref: /openbmc/linux/drivers/dma/lpc18xx-dmamux.c (revision ae213c44)
1 /*
2  * DMA Router driver for LPC18xx/43xx DMA MUX
3  *
4  * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
5  *
6  * Based on TI DMA Crossbar driver by:
7  *   Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
8  *   Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  */
15 
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/of_device.h>
20 #include <linux/of_dma.h>
21 #include <linux/regmap.h>
22 #include <linux/spinlock.h>
23 
24 /* CREG register offset and macros for mux manipulation */
25 #define LPC18XX_CREG_DMAMUX		0x11c
26 #define LPC18XX_DMAMUX_VAL(v, n)	((v) << (n * 2))
27 #define LPC18XX_DMAMUX_MASK(n)		(0x3 << (n * 2))
28 #define LPC18XX_DMAMUX_MAX_VAL		0x3
29 
30 struct lpc18xx_dmamux {
31 	u32 value;
32 	bool busy;
33 };
34 
35 struct lpc18xx_dmamux_data {
36 	struct dma_router dmarouter;
37 	struct lpc18xx_dmamux *muxes;
38 	u32 dma_master_requests;
39 	u32 dma_mux_requests;
40 	struct regmap *reg;
41 	spinlock_t lock;
42 };
43 
44 static void lpc18xx_dmamux_free(struct device *dev, void *route_data)
45 {
46 	struct lpc18xx_dmamux_data *dmamux = dev_get_drvdata(dev);
47 	struct lpc18xx_dmamux *mux = route_data;
48 	unsigned long flags;
49 
50 	spin_lock_irqsave(&dmamux->lock, flags);
51 	mux->busy = false;
52 	spin_unlock_irqrestore(&dmamux->lock, flags);
53 }
54 
55 static void *lpc18xx_dmamux_reserve(struct of_phandle_args *dma_spec,
56 				    struct of_dma *ofdma)
57 {
58 	struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);
59 	struct lpc18xx_dmamux_data *dmamux = platform_get_drvdata(pdev);
60 	unsigned long flags;
61 	unsigned mux;
62 
63 	if (dma_spec->args_count != 3) {
64 		dev_err(&pdev->dev, "invalid number of dma mux args\n");
65 		return ERR_PTR(-EINVAL);
66 	}
67 
68 	mux = dma_spec->args[0];
69 	if (mux >= dmamux->dma_master_requests) {
70 		dev_err(&pdev->dev, "invalid mux number: %d\n",
71 			dma_spec->args[0]);
72 		return ERR_PTR(-EINVAL);
73 	}
74 
75 	if (dma_spec->args[1] > LPC18XX_DMAMUX_MAX_VAL) {
76 		dev_err(&pdev->dev, "invalid dma mux value: %d\n",
77 			dma_spec->args[1]);
78 		return ERR_PTR(-EINVAL);
79 	}
80 
81 	/* The of_node_put() will be done in the core for the node */
82 	dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
83 	if (!dma_spec->np) {
84 		dev_err(&pdev->dev, "can't get dma master\n");
85 		return ERR_PTR(-EINVAL);
86 	}
87 
88 	spin_lock_irqsave(&dmamux->lock, flags);
89 	if (dmamux->muxes[mux].busy) {
90 		spin_unlock_irqrestore(&dmamux->lock, flags);
91 		dev_err(&pdev->dev, "dma request %u busy with %u.%u\n",
92 			mux, mux, dmamux->muxes[mux].value);
93 		of_node_put(dma_spec->np);
94 		return ERR_PTR(-EBUSY);
95 	}
96 
97 	dmamux->muxes[mux].busy = true;
98 	dmamux->muxes[mux].value = dma_spec->args[1];
99 
100 	regmap_update_bits(dmamux->reg, LPC18XX_CREG_DMAMUX,
101 			   LPC18XX_DMAMUX_MASK(mux),
102 			   LPC18XX_DMAMUX_VAL(dmamux->muxes[mux].value, mux));
103 	spin_unlock_irqrestore(&dmamux->lock, flags);
104 
105 	dma_spec->args[1] = dma_spec->args[2];
106 	dma_spec->args_count = 2;
107 
108 	dev_dbg(&pdev->dev, "mapping dmamux %u.%u to dma request %u\n", mux,
109 		dmamux->muxes[mux].value, mux);
110 
111 	return &dmamux->muxes[mux];
112 }
113 
114 static int lpc18xx_dmamux_probe(struct platform_device *pdev)
115 {
116 	struct device_node *dma_np, *np = pdev->dev.of_node;
117 	struct lpc18xx_dmamux_data *dmamux;
118 	int ret;
119 
120 	dmamux = devm_kzalloc(&pdev->dev, sizeof(*dmamux), GFP_KERNEL);
121 	if (!dmamux)
122 		return -ENOMEM;
123 
124 	dmamux->reg = syscon_regmap_lookup_by_compatible("nxp,lpc1850-creg");
125 	if (IS_ERR(dmamux->reg)) {
126 		dev_err(&pdev->dev, "syscon lookup failed\n");
127 		return PTR_ERR(dmamux->reg);
128 	}
129 
130 	ret = of_property_read_u32(np, "dma-requests",
131 				   &dmamux->dma_mux_requests);
132 	if (ret) {
133 		dev_err(&pdev->dev, "missing dma-requests property\n");
134 		return ret;
135 	}
136 
137 	dma_np = of_parse_phandle(np, "dma-masters", 0);
138 	if (!dma_np) {
139 		dev_err(&pdev->dev, "can't get dma master\n");
140 		return -ENODEV;
141 	}
142 
143 	ret = of_property_read_u32(dma_np, "dma-requests",
144 				   &dmamux->dma_master_requests);
145 	of_node_put(dma_np);
146 	if (ret) {
147 		dev_err(&pdev->dev, "missing master dma-requests property\n");
148 		return ret;
149 	}
150 
151 	dmamux->muxes = devm_kcalloc(&pdev->dev, dmamux->dma_master_requests,
152 				     sizeof(struct lpc18xx_dmamux),
153 				     GFP_KERNEL);
154 	if (!dmamux->muxes)
155 		return -ENOMEM;
156 
157 	spin_lock_init(&dmamux->lock);
158 	platform_set_drvdata(pdev, dmamux);
159 	dmamux->dmarouter.dev = &pdev->dev;
160 	dmamux->dmarouter.route_free = lpc18xx_dmamux_free;
161 
162 	return of_dma_router_register(np, lpc18xx_dmamux_reserve,
163 				      &dmamux->dmarouter);
164 }
165 
166 static const struct of_device_id lpc18xx_dmamux_match[] = {
167 	{ .compatible = "nxp,lpc1850-dmamux" },
168 	{},
169 };
170 
171 static struct platform_driver lpc18xx_dmamux_driver = {
172 	.probe	= lpc18xx_dmamux_probe,
173 	.driver = {
174 		.name = "lpc18xx-dmamux",
175 		.of_match_table = lpc18xx_dmamux_match,
176 	},
177 };
178 
179 static int __init lpc18xx_dmamux_init(void)
180 {
181 	return platform_driver_register(&lpc18xx_dmamux_driver);
182 }
183 arch_initcall(lpc18xx_dmamux_init);
184