1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2022 Schneider-Electric 4 * Author: Miquel Raynal <miquel.raynal@bootlin.com 5 * 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 6 17 #define RZN1_DMAMUX_MAX_LINES 64 18 #define RZN1_DMAMUX_LINES_PER_CTLR 16 19 20 struct rzn1_dmamux_data { 21 struct dma_router dmarouter; 22 DECLARE_BITMAP(used_chans, 2 * RZN1_DMAMUX_LINES_PER_CTLR); 23 }; 24 25 struct rzn1_dmamux_map { 26 unsigned int req_idx; 27 }; 28 29 static 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 41 static 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 ret = -EINVAL; 53 goto put_device; 54 } 55 56 map = kzalloc(sizeof(*map), GFP_KERNEL); 57 if (!map) { 58 ret = -ENOMEM; 59 goto put_device; 60 } 61 62 chan = dma_spec->args[0]; 63 map->req_idx = dma_spec->args[4]; 64 val = dma_spec->args[5]; 65 dma_spec->args_count -= 2; 66 67 if (chan >= RZN1_DMAMUX_LINES_PER_CTLR) { 68 dev_err(&pdev->dev, "Invalid DMA request line: %u\n", chan); 69 ret = -EINVAL; 70 goto free_map; 71 } 72 73 if (map->req_idx >= RZN1_DMAMUX_MAX_LINES || 74 (map->req_idx % RZN1_DMAMUX_LINES_PER_CTLR) != chan) { 75 dev_err(&pdev->dev, "Invalid MUX request line: %u\n", map->req_idx); 76 ret = -EINVAL; 77 goto free_map; 78 } 79 80 dmac_idx = map->req_idx >= RZN1_DMAMUX_LINES_PER_CTLR ? 1 : 0; 81 dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", dmac_idx); 82 if (!dma_spec->np) { 83 dev_err(&pdev->dev, "Can't get DMA master\n"); 84 ret = -EINVAL; 85 goto free_map; 86 } 87 88 dev_dbg(&pdev->dev, "Mapping DMAMUX request %u to DMAC%u request %u\n", 89 map->req_idx, dmac_idx, chan); 90 91 if (test_and_set_bit(map->req_idx, dmamux->used_chans)) { 92 ret = -EBUSY; 93 goto free_map; 94 } 95 96 mask = BIT(map->req_idx); 97 ret = r9a06g032_sysctrl_set_dmamux(mask, val ? mask : 0); 98 if (ret) 99 goto clear_bitmap; 100 101 put_device(&pdev->dev); 102 return map; 103 104 clear_bitmap: 105 clear_bit(map->req_idx, dmamux->used_chans); 106 free_map: 107 kfree(map); 108 put_device: 109 put_device(&pdev->dev); 110 111 return ERR_PTR(ret); 112 } 113 114 #ifdef CONFIG_OF 115 static const struct of_device_id rzn1_dmac_match[] = { 116 { .compatible = "renesas,rzn1-dma" }, 117 {} 118 }; 119 #endif 120 121 static int rzn1_dmamux_probe(struct platform_device *pdev) 122 { 123 struct device_node *mux_node = pdev->dev.of_node; 124 const struct of_device_id *match; 125 struct device_node *dmac_node; 126 struct rzn1_dmamux_data *dmamux; 127 128 dmamux = devm_kzalloc(&pdev->dev, sizeof(*dmamux), GFP_KERNEL); 129 if (!dmamux) 130 return -ENOMEM; 131 132 dmac_node = of_parse_phandle(mux_node, "dma-masters", 0); 133 if (!dmac_node) 134 return dev_err_probe(&pdev->dev, -ENODEV, "Can't get DMA master node\n"); 135 136 match = of_match_node(rzn1_dmac_match, dmac_node); 137 of_node_put(dmac_node); 138 if (!match) 139 return dev_err_probe(&pdev->dev, -EINVAL, "DMA master is not supported\n"); 140 141 dmamux->dmarouter.dev = &pdev->dev; 142 dmamux->dmarouter.route_free = rzn1_dmamux_free; 143 144 platform_set_drvdata(pdev, dmamux); 145 146 return of_dma_router_register(mux_node, rzn1_dmamux_route_allocate, 147 &dmamux->dmarouter); 148 } 149 150 static const struct of_device_id rzn1_dmamux_match[] = { 151 { .compatible = "renesas,rzn1-dmamux" }, 152 {} 153 }; 154 MODULE_DEVICE_TABLE(of, rzn1_dmamux_match); 155 156 static struct platform_driver rzn1_dmamux_driver = { 157 .driver = { 158 .name = "renesas,rzn1-dmamux", 159 .of_match_table = rzn1_dmamux_match, 160 }, 161 .probe = rzn1_dmamux_probe, 162 }; 163 module_platform_driver(rzn1_dmamux_driver); 164 165 MODULE_LICENSE("GPL"); 166 MODULE_AUTHOR("Miquel Raynal <miquel.raynal@bootlin.com"); 167 MODULE_DESCRIPTION("Renesas RZ/N1 DMAMUX driver"); 168