clk-bcm63xx-gate.c 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
// SPDX-License-Identifier: GPL-2.0

#include <linux/clk-provider.h>
#include <linux/init.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>

struct clk_bcm63xx_table_entry {
	const char * const name;
	u8 bit;
	unsigned long flags;
};

struct clk_bcm63xx_hw {
	void __iomem *regs;
	spinlock_t lock;

	struct clk_hw_onecell_data data;
};

static const struct clk_bcm63xx_table_entry bcm3368_clocks[] = {
	{ .name = "mac", .bit = 3, },
	{ .name = "tc", .bit = 5, },
	{ .name = "us_top", .bit = 6, },
	{ .name = "ds_top", .bit = 7, },
	{ .name = "acm", .bit = 8, },
	{ .name = "spi", .bit = 9, },
	{ .name = "usbs", .bit = 10, },
	{ .name = "bmu", .bit = 11, },
	{ .name = "pcm", .bit = 12, },
	{ .name = "ntp", .bit = 13, },
	{ .name = "acp_b", .bit = 14, },
	{ .name = "acp_a", .bit = 15, },
	{ .name = "emusb", .bit = 17, },
	{ .name = "enet0", .bit = 18, },
	{ .name = "enet1", .bit = 19, },
	{ .name = "usbsu", .bit = 20, },
	{ .name = "ephy", .bit = 21, },
	{ },
};

static const struct clk_bcm63xx_table_entry bcm6328_clocks[] = {
	{ .name = "phy_mips", .bit = 0, },
	{ .name = "adsl_qproc", .bit = 1, },
	{ .name = "adsl_afe", .bit = 2, },
	{ .name = "adsl", .bit = 3, },
	{ .name = "mips", .bit = 4, .flags = CLK_IS_CRITICAL, },
	{ .name = "sar", .bit = 5, },
	{ .name = "pcm", .bit = 6, },
	{ .name = "usbd", .bit = 7, },
	{ .name = "usbh", .bit = 8, },
	{ .name = "hsspi", .bit = 9, },
	{ .name = "pcie", .bit = 10, },
	{ .name = "robosw", .bit = 11, },
	{ },
};

static const struct clk_bcm63xx_table_entry bcm6358_clocks[] = {
	{ .name = "enet", .bit = 4, },
	{ .name = "adslphy", .bit = 5, },
	{ .name = "pcm", .bit = 8, },
	{ .name = "spi", .bit = 9, },
	{ .name = "usbs", .bit = 10, },
	{ .name = "sar", .bit = 11, },
	{ .name = "emusb", .bit = 17, },
	{ .name = "enet0", .bit = 18, },
	{ .name = "enet1", .bit = 19, },
	{ .name = "usbsu", .bit = 20, },
	{ .name = "ephy", .bit = 21, },
	{ },
};

static const struct clk_bcm63xx_table_entry bcm6362_clocks[] = {
	{ .name = "adsl_qproc", .bit = 1, },
	{ .name = "adsl_afe", .bit = 2, },
	{ .name = "adsl", .bit = 3, },
	{ .name = "mips", .bit = 4, .flags = CLK_IS_CRITICAL, },
	{ .name = "wlan_ocp", .bit = 5, },
	{ .name = "swpkt_usb", .bit = 7, },
	{ .name = "swpkt_sar", .bit = 8, },
	{ .name = "sar", .bit = 9, },
	{ .name = "robosw", .bit = 10, },
	{ .name = "pcm", .bit = 11, },
	{ .name = "usbd", .bit = 12, },
	{ .name = "usbh", .bit = 13, },
	{ .name = "ipsec", .bit = 14, },
	{ .name = "spi", .bit = 15, },
	{ .name = "hsspi", .bit = 16, },
	{ .name = "pcie", .bit = 17, },
	{ .name = "fap", .bit = 18, },
	{ .name = "phymips", .bit = 19, },
	{ .name = "nand", .bit = 20, },
	{ },
};

static const struct clk_bcm63xx_table_entry bcm6368_clocks[] = {
	{ .name = "vdsl_qproc", .bit = 2, },
	{ .name = "vdsl_afe", .bit = 3, },
	{ .name = "vdsl_bonding", .bit = 4, },
	{ .name = "vdsl", .bit = 5, },
	{ .name = "phymips", .bit = 6, },
	{ .name = "swpkt_usb", .bit = 7, },
	{ .name = "swpkt_sar", .bit = 8, },
	{ .name = "spi", .bit = 9, },
	{ .name = "usbd", .bit = 10, },
	{ .name = "sar", .bit = 11, },
	{ .name = "robosw", .bit = 12, },
	{ .name = "utopia", .bit = 13, },
	{ .name = "pcm", .bit = 14, },
	{ .name = "usbh", .bit = 15, },
	{ .name = "disable_gless", .bit = 16, },
	{ .name = "nand", .bit = 17, },
	{ .name = "ipsec", .bit = 18, },
	{ },
};

static const struct clk_bcm63xx_table_entry bcm63268_clocks[] = {
	{ .name = "disable_gless", .bit = 0, },
	{ .name = "vdsl_qproc", .bit = 1, },
	{ .name = "vdsl_afe", .bit = 2, },
	{ .name = "vdsl", .bit = 3, },
	{ .name = "mips", .bit = 4, .flags = CLK_IS_CRITICAL, },
	{ .name = "wlan_ocp", .bit = 5, },
	{ .name = "dect", .bit = 6, },
	{ .name = "fap0", .bit = 7, },
	{ .name = "fap1", .bit = 8, },
	{ .name = "sar", .bit = 9, },
	{ .name = "robosw", .bit = 10, },
	{ .name = "pcm", .bit = 11, },
	{ .name = "usbd", .bit = 12, },
	{ .name = "usbh", .bit = 13, },
	{ .name = "ipsec", .bit = 14, },
	{ .name = "spi", .bit = 15, },
	{ .name = "hsspi", .bit = 16, },
	{ .name = "pcie", .bit = 17, },
	{ .name = "phymips", .bit = 18, },
	{ .name = "gmac", .bit = 19, },
	{ .name = "nand", .bit = 20, },
	{ .name = "tbus", .bit = 27, },
	{ .name = "robosw250", .bit = 31, },
	{ },
};

static int clk_bcm63xx_probe(struct platform_device *pdev)
{
	const struct clk_bcm63xx_table_entry *entry, *table;
	struct clk_bcm63xx_hw *hw;
	struct resource *r;
	u8 maxbit = 0;
	int i, ret;

	table = of_device_get_match_data(&pdev->dev);
	if (!table)
		return -EINVAL;

	for (entry = table; entry->name; entry++)
		maxbit = max_t(u8, maxbit, entry->bit);

	hw = devm_kzalloc(&pdev->dev, struct_size(hw, data.hws, maxbit),
			  GFP_KERNEL);
	if (!hw)
		return -ENOMEM;

	platform_set_drvdata(pdev, hw);

	spin_lock_init(&hw->lock);

	hw->data.num = maxbit;
	for (i = 0; i < maxbit; i++)
		hw->data.hws[i] = ERR_PTR(-ENODEV);

	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	hw->regs = devm_ioremap_resource(&pdev->dev, r);
	if (IS_ERR(hw->regs))
		return PTR_ERR(hw->regs);

	for (entry = table; entry->name; entry++) {
		struct clk_hw *clk;

		clk = clk_hw_register_gate(&pdev->dev, entry->name, NULL,
					   entry->flags, hw->regs, entry->bit,
					   CLK_GATE_BIG_ENDIAN, &hw->lock);
		if (IS_ERR(clk)) {
			ret = PTR_ERR(clk);
			goto out_err;
		}

		hw->data.hws[entry->bit] = clk;
	}

	ret = of_clk_add_hw_provider(pdev->dev.of_node, of_clk_hw_onecell_get,
				     &hw->data);
	if (!ret)
		return 0;
out_err:
	for (i = 0; i < hw->data.num; i++) {
		if (!IS_ERR(hw->data.hws[i]))
			clk_hw_unregister_gate(hw->data.hws[i]);
	}

	return ret;
}

static int clk_bcm63xx_remove(struct platform_device *pdev)
{
	struct clk_bcm63xx_hw *hw = platform_get_drvdata(pdev);
	int i;

	of_clk_del_provider(pdev->dev.of_node);

	for (i = 0; i < hw->data.num; i++) {
		if (!IS_ERR(hw->data.hws[i]))
			clk_hw_unregister_gate(hw->data.hws[i]);
	}

	return 0;
}

static const struct of_device_id clk_bcm63xx_dt_ids[] = {
	{ .compatible = "brcm,bcm3368-clocks", .data = &bcm3368_clocks, },
	{ .compatible = "brcm,bcm6328-clocks", .data = &bcm6328_clocks, },
	{ .compatible = "brcm,bcm6358-clocks", .data = &bcm6358_clocks, },
	{ .compatible = "brcm,bcm6362-clocks", .data = &bcm6362_clocks, },
	{ .compatible = "brcm,bcm6368-clocks", .data = &bcm6368_clocks, },
	{ .compatible = "brcm,bcm63268-clocks", .data = &bcm63268_clocks, },
	{ }
};

static struct platform_driver clk_bcm63xx = {
	.probe = clk_bcm63xx_probe,
	.remove = clk_bcm63xx_remove,
	.driver = {
		.name = "bcm63xx-clock",
		.of_match_table = clk_bcm63xx_dt_ids,
	},
};
builtin_platform_driver(clk_bcm63xx);