18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2016 Chen-Yu Tsai. All rights reserved.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/clk.h>
78c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
88c2ecf20Sopenharmony_ci#include <linux/of_address.h>
98c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include "ccu_common.h"
128c2ecf20Sopenharmony_ci#include "ccu_gate.h"
138c2ecf20Sopenharmony_ci#include "ccu_reset.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include "ccu-sun9i-a80-usb.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic const struct clk_parent_data clk_parent_hosc[] = {
188c2ecf20Sopenharmony_ci	{ .fw_name = "hosc" },
198c2ecf20Sopenharmony_ci};
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic const struct clk_parent_data clk_parent_bus[] = {
228c2ecf20Sopenharmony_ci	{ .fw_name = "bus" },
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(bus_hci0_clk, "bus-hci0", clk_parent_bus, 0x0, BIT(1), 0);
268c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(usb_ohci0_clk, "usb-ohci0", clk_parent_hosc, 0x0, BIT(2), 0);
278c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(bus_hci1_clk, "bus-hci1", clk_parent_bus, 0x0, BIT(3), 0);
288c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(bus_hci2_clk, "bus-hci2", clk_parent_bus, 0x0, BIT(5), 0);
298c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(usb_ohci2_clk, "usb-ohci2", clk_parent_hosc, 0x0, BIT(6), 0);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(usb0_phy_clk, "usb0-phy", clk_parent_hosc, 0x4, BIT(1), 0);
328c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(usb1_hsic_clk, "usb1-hsic", clk_parent_hosc, 0x4, BIT(2), 0);
338c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(usb1_phy_clk, "usb1-phy", clk_parent_hosc, 0x4, BIT(3), 0);
348c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(usb2_hsic_clk, "usb2-hsic", clk_parent_hosc, 0x4, BIT(4), 0);
358c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(usb2_phy_clk, "usb2-phy", clk_parent_hosc, 0x4, BIT(5), 0);
368c2ecf20Sopenharmony_cistatic SUNXI_CCU_GATE_DATA(usb_hsic_clk, "usb-hsic", clk_parent_hosc, 0x4, BIT(10), 0);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic struct ccu_common *sun9i_a80_usb_clks[] = {
398c2ecf20Sopenharmony_ci	&bus_hci0_clk.common,
408c2ecf20Sopenharmony_ci	&usb_ohci0_clk.common,
418c2ecf20Sopenharmony_ci	&bus_hci1_clk.common,
428c2ecf20Sopenharmony_ci	&bus_hci2_clk.common,
438c2ecf20Sopenharmony_ci	&usb_ohci2_clk.common,
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	&usb0_phy_clk.common,
468c2ecf20Sopenharmony_ci	&usb1_hsic_clk.common,
478c2ecf20Sopenharmony_ci	&usb1_phy_clk.common,
488c2ecf20Sopenharmony_ci	&usb2_hsic_clk.common,
498c2ecf20Sopenharmony_ci	&usb2_phy_clk.common,
508c2ecf20Sopenharmony_ci	&usb_hsic_clk.common,
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic struct clk_hw_onecell_data sun9i_a80_usb_hw_clks = {
548c2ecf20Sopenharmony_ci	.hws	= {
558c2ecf20Sopenharmony_ci		[CLK_BUS_HCI0]	= &bus_hci0_clk.common.hw,
568c2ecf20Sopenharmony_ci		[CLK_USB_OHCI0]	= &usb_ohci0_clk.common.hw,
578c2ecf20Sopenharmony_ci		[CLK_BUS_HCI1]	= &bus_hci1_clk.common.hw,
588c2ecf20Sopenharmony_ci		[CLK_BUS_HCI2]	= &bus_hci2_clk.common.hw,
598c2ecf20Sopenharmony_ci		[CLK_USB_OHCI2]	= &usb_ohci2_clk.common.hw,
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci		[CLK_USB0_PHY]	= &usb0_phy_clk.common.hw,
628c2ecf20Sopenharmony_ci		[CLK_USB1_HSIC]	= &usb1_hsic_clk.common.hw,
638c2ecf20Sopenharmony_ci		[CLK_USB1_PHY]	= &usb1_phy_clk.common.hw,
648c2ecf20Sopenharmony_ci		[CLK_USB2_HSIC]	= &usb2_hsic_clk.common.hw,
658c2ecf20Sopenharmony_ci		[CLK_USB2_PHY]	= &usb2_phy_clk.common.hw,
668c2ecf20Sopenharmony_ci		[CLK_USB_HSIC]	= &usb_hsic_clk.common.hw,
678c2ecf20Sopenharmony_ci	},
688c2ecf20Sopenharmony_ci	.num	= CLK_NUMBER,
698c2ecf20Sopenharmony_ci};
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic struct ccu_reset_map sun9i_a80_usb_resets[] = {
728c2ecf20Sopenharmony_ci	[RST_USB0_HCI]		= { 0x0, BIT(17) },
738c2ecf20Sopenharmony_ci	[RST_USB1_HCI]		= { 0x0, BIT(18) },
748c2ecf20Sopenharmony_ci	[RST_USB2_HCI]		= { 0x0, BIT(19) },
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	[RST_USB0_PHY]		= { 0x4, BIT(17) },
778c2ecf20Sopenharmony_ci	[RST_USB1_HSIC]		= { 0x4, BIT(18) },
788c2ecf20Sopenharmony_ci	[RST_USB1_PHY]		= { 0x4, BIT(19) },
798c2ecf20Sopenharmony_ci	[RST_USB2_HSIC]		= { 0x4, BIT(20) },
808c2ecf20Sopenharmony_ci	[RST_USB2_PHY]		= { 0x4, BIT(21) },
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic const struct sunxi_ccu_desc sun9i_a80_usb_clk_desc = {
848c2ecf20Sopenharmony_ci	.ccu_clks	= sun9i_a80_usb_clks,
858c2ecf20Sopenharmony_ci	.num_ccu_clks	= ARRAY_SIZE(sun9i_a80_usb_clks),
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	.hw_clks	= &sun9i_a80_usb_hw_clks,
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	.resets		= sun9i_a80_usb_resets,
908c2ecf20Sopenharmony_ci	.num_resets	= ARRAY_SIZE(sun9i_a80_usb_resets),
918c2ecf20Sopenharmony_ci};
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic int sun9i_a80_usb_clk_probe(struct platform_device *pdev)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	struct resource *res;
968c2ecf20Sopenharmony_ci	struct clk *bus_clk;
978c2ecf20Sopenharmony_ci	void __iomem *reg;
988c2ecf20Sopenharmony_ci	int ret;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1018c2ecf20Sopenharmony_ci	reg = devm_ioremap_resource(&pdev->dev, res);
1028c2ecf20Sopenharmony_ci	if (IS_ERR(reg))
1038c2ecf20Sopenharmony_ci		return PTR_ERR(reg);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	bus_clk = devm_clk_get(&pdev->dev, "bus");
1068c2ecf20Sopenharmony_ci	if (IS_ERR(bus_clk)) {
1078c2ecf20Sopenharmony_ci		ret = PTR_ERR(bus_clk);
1088c2ecf20Sopenharmony_ci		if (ret != -EPROBE_DEFER)
1098c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "Couldn't get bus clk: %d\n", ret);
1108c2ecf20Sopenharmony_ci		return ret;
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/* The bus clock needs to be enabled for us to access the registers */
1148c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(bus_clk);
1158c2ecf20Sopenharmony_ci	if (ret) {
1168c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Couldn't enable bus clk: %d\n", ret);
1178c2ecf20Sopenharmony_ci		return ret;
1188c2ecf20Sopenharmony_ci	}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	ret = sunxi_ccu_probe(pdev->dev.of_node, reg,
1218c2ecf20Sopenharmony_ci			      &sun9i_a80_usb_clk_desc);
1228c2ecf20Sopenharmony_ci	if (ret)
1238c2ecf20Sopenharmony_ci		goto err_disable_clk;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	return 0;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cierr_disable_clk:
1288c2ecf20Sopenharmony_ci	clk_disable_unprepare(bus_clk);
1298c2ecf20Sopenharmony_ci	return ret;
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic const struct of_device_id sun9i_a80_usb_clk_ids[] = {
1338c2ecf20Sopenharmony_ci	{ .compatible = "allwinner,sun9i-a80-usb-clks" },
1348c2ecf20Sopenharmony_ci	{ }
1358c2ecf20Sopenharmony_ci};
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic struct platform_driver sun9i_a80_usb_clk_driver = {
1388c2ecf20Sopenharmony_ci	.probe	= sun9i_a80_usb_clk_probe,
1398c2ecf20Sopenharmony_ci	.driver	= {
1408c2ecf20Sopenharmony_ci		.name	= "sun9i-a80-usb-clks",
1418c2ecf20Sopenharmony_ci		.of_match_table	= sun9i_a80_usb_clk_ids,
1428c2ecf20Sopenharmony_ci	},
1438c2ecf20Sopenharmony_ci};
1448c2ecf20Sopenharmony_cibuiltin_platform_driver(sun9i_a80_usb_clk_driver);
145