161 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2/*3 * Copyright (C) 2022 Schneider-Electric4 * Author: Miquel Raynal <miquel.raynal@bootlin.com5 * Based on TI crossbar driver written by Peter Ujfalusi <peter.ujfalusi@ti.com>6 */7#include <linux/bitops.h>8#include <linux/of.h>9#include <linux/of_dma.h>10#include <linux/of_platform.h>11#include <linux/platform_device.h>12#include <linux/slab.h>13#include <linux/soc/renesas/r9a06g032-sysctrl.h>14#include <linux/types.h>15 16#define RNZ1_DMAMUX_NCELLS 617#define RZN1_DMAMUX_MAX_LINES 6418#define RZN1_DMAMUX_LINES_PER_CTLR 1619 20struct rzn1_dmamux_data {21 struct dma_router dmarouter;22 DECLARE_BITMAP(used_chans, 2 * RZN1_DMAMUX_LINES_PER_CTLR);23};24 25struct rzn1_dmamux_map {26 unsigned int req_idx;27};28 29static void rzn1_dmamux_free(struct device *dev, void *route_data)30{31 struct rzn1_dmamux_data *dmamux = dev_get_drvdata(dev);32 struct rzn1_dmamux_map *map = route_data;33 34 dev_dbg(dev, "Unmapping DMAMUX request %u\n", map->req_idx);35 36 clear_bit(map->req_idx, dmamux->used_chans);37 38 kfree(map);39}40 41static void *rzn1_dmamux_route_allocate(struct of_phandle_args *dma_spec,42 struct of_dma *ofdma)43{44 struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);45 struct rzn1_dmamux_data *dmamux = platform_get_drvdata(pdev);46 struct rzn1_dmamux_map *map;47 unsigned int dmac_idx, chan, val;48 u32 mask;49 int ret;50 51 if (dma_spec->args_count != RNZ1_DMAMUX_NCELLS)52 return ERR_PTR(-EINVAL);53 54 map = kzalloc(sizeof(*map), GFP_KERNEL);55 if (!map)56 return ERR_PTR(-ENOMEM);57 58 chan = dma_spec->args[0];59 map->req_idx = dma_spec->args[4];60 val = dma_spec->args[5];61 dma_spec->args_count -= 2;62 63 if (chan >= RZN1_DMAMUX_LINES_PER_CTLR) {64 dev_err(&pdev->dev, "Invalid DMA request line: %u\n", chan);65 ret = -EINVAL;66 goto free_map;67 }68 69 if (map->req_idx >= RZN1_DMAMUX_MAX_LINES ||70 (map->req_idx % RZN1_DMAMUX_LINES_PER_CTLR) != chan) {71 dev_err(&pdev->dev, "Invalid MUX request line: %u\n", map->req_idx);72 ret = -EINVAL;73 goto free_map;74 }75 76 dmac_idx = map->req_idx >= RZN1_DMAMUX_LINES_PER_CTLR ? 1 : 0;77 dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", dmac_idx);78 if (!dma_spec->np) {79 dev_err(&pdev->dev, "Can't get DMA master\n");80 ret = -EINVAL;81 goto free_map;82 }83 84 dev_dbg(&pdev->dev, "Mapping DMAMUX request %u to DMAC%u request %u\n",85 map->req_idx, dmac_idx, chan);86 87 if (test_and_set_bit(map->req_idx, dmamux->used_chans)) {88 ret = -EBUSY;89 goto free_map;90 }91 92 mask = BIT(map->req_idx);93 ret = r9a06g032_sysctrl_set_dmamux(mask, val ? mask : 0);94 if (ret)95 goto clear_bitmap;96 97 return map;98 99clear_bitmap:100 clear_bit(map->req_idx, dmamux->used_chans);101free_map:102 kfree(map);103 104 return ERR_PTR(ret);105}106 107#ifdef CONFIG_OF108static const struct of_device_id rzn1_dmac_match[] = {109 { .compatible = "renesas,rzn1-dma" },110 {}111};112#endif113 114static int rzn1_dmamux_probe(struct platform_device *pdev)115{116 struct device_node *mux_node = pdev->dev.of_node;117 const struct of_device_id *match;118 struct device_node *dmac_node;119 struct rzn1_dmamux_data *dmamux;120 121 dmamux = devm_kzalloc(&pdev->dev, sizeof(*dmamux), GFP_KERNEL);122 if (!dmamux)123 return -ENOMEM;124 125 dmac_node = of_parse_phandle(mux_node, "dma-masters", 0);126 if (!dmac_node)127 return dev_err_probe(&pdev->dev, -ENODEV, "Can't get DMA master node\n");128 129 match = of_match_node(rzn1_dmac_match, dmac_node);130 of_node_put(dmac_node);131 if (!match)132 return dev_err_probe(&pdev->dev, -EINVAL, "DMA master is not supported\n");133 134 dmamux->dmarouter.dev = &pdev->dev;135 dmamux->dmarouter.route_free = rzn1_dmamux_free;136 137 platform_set_drvdata(pdev, dmamux);138 139 return of_dma_router_register(mux_node, rzn1_dmamux_route_allocate,140 &dmamux->dmarouter);141}142 143static const struct of_device_id rzn1_dmamux_match[] = {144 { .compatible = "renesas,rzn1-dmamux" },145 {}146};147MODULE_DEVICE_TABLE(of, rzn1_dmamux_match);148 149static struct platform_driver rzn1_dmamux_driver = {150 .driver = {151 .name = "renesas,rzn1-dmamux",152 .of_match_table = rzn1_dmamux_match,153 },154 .probe = rzn1_dmamux_probe,155};156module_platform_driver(rzn1_dmamux_driver);157 158MODULE_LICENSE("GPL");159MODULE_AUTHOR("Miquel Raynal <miquel.raynal@bootlin.com");160MODULE_DESCRIPTION("Renesas RZ/N1 DMAMUX driver");161