162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/* Copyright (C) 2020 Maxime Ripard <maxime@cerno.tech> */
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci#include <linux/device.h>
562306a36Sopenharmony_ci#include <linux/dma-map-ops.h>
662306a36Sopenharmony_ci#include <linux/init.h>
762306a36Sopenharmony_ci#include <linux/notifier.h>
862306a36Sopenharmony_ci#include <linux/of.h>
962306a36Sopenharmony_ci#include <linux/platform_device.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_cistatic const char * const sunxi_mbus_devices[] = {
1262306a36Sopenharmony_ci	/*
1362306a36Sopenharmony_ci	 * The display engine virtual devices are not strictly speaking
1462306a36Sopenharmony_ci	 * connected to the MBUS, but since DRM will perform all the
1562306a36Sopenharmony_ci	 * memory allocations and DMA operations through that device, we
1662306a36Sopenharmony_ci	 * need to have the quirk on those devices too.
1762306a36Sopenharmony_ci	 */
1862306a36Sopenharmony_ci	"allwinner,sun4i-a10-display-engine",
1962306a36Sopenharmony_ci	"allwinner,sun5i-a10s-display-engine",
2062306a36Sopenharmony_ci	"allwinner,sun5i-a13-display-engine",
2162306a36Sopenharmony_ci	"allwinner,sun6i-a31-display-engine",
2262306a36Sopenharmony_ci	"allwinner,sun6i-a31s-display-engine",
2362306a36Sopenharmony_ci	"allwinner,sun7i-a20-display-engine",
2462306a36Sopenharmony_ci	"allwinner,sun8i-a23-display-engine",
2562306a36Sopenharmony_ci	"allwinner,sun8i-a33-display-engine",
2662306a36Sopenharmony_ci	"allwinner,sun9i-a80-display-engine",
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci	/*
2962306a36Sopenharmony_ci	 * And now we have the regular devices connected to the MBUS
3062306a36Sopenharmony_ci	 * (that we know of).
3162306a36Sopenharmony_ci	 */
3262306a36Sopenharmony_ci	"allwinner,sun4i-a10-csi1",
3362306a36Sopenharmony_ci	"allwinner,sun4i-a10-display-backend",
3462306a36Sopenharmony_ci	"allwinner,sun4i-a10-display-frontend",
3562306a36Sopenharmony_ci	"allwinner,sun4i-a10-video-engine",
3662306a36Sopenharmony_ci	"allwinner,sun5i-a13-display-backend",
3762306a36Sopenharmony_ci	"allwinner,sun5i-a13-video-engine",
3862306a36Sopenharmony_ci	"allwinner,sun6i-a31-csi",
3962306a36Sopenharmony_ci	"allwinner,sun6i-a31-display-backend",
4062306a36Sopenharmony_ci	"allwinner,sun7i-a20-csi0",
4162306a36Sopenharmony_ci	"allwinner,sun7i-a20-display-backend",
4262306a36Sopenharmony_ci	"allwinner,sun7i-a20-display-frontend",
4362306a36Sopenharmony_ci	"allwinner,sun7i-a20-video-engine",
4462306a36Sopenharmony_ci	"allwinner,sun8i-a23-display-backend",
4562306a36Sopenharmony_ci	"allwinner,sun8i-a23-display-frontend",
4662306a36Sopenharmony_ci	"allwinner,sun8i-a33-display-backend",
4762306a36Sopenharmony_ci	"allwinner,sun8i-a33-display-frontend",
4862306a36Sopenharmony_ci	"allwinner,sun8i-a33-video-engine",
4962306a36Sopenharmony_ci	"allwinner,sun8i-a83t-csi",
5062306a36Sopenharmony_ci	"allwinner,sun8i-h3-csi",
5162306a36Sopenharmony_ci	"allwinner,sun8i-h3-video-engine",
5262306a36Sopenharmony_ci	"allwinner,sun8i-v3s-csi",
5362306a36Sopenharmony_ci	"allwinner,sun9i-a80-display-backend",
5462306a36Sopenharmony_ci	"allwinner,sun50i-a64-csi",
5562306a36Sopenharmony_ci	"allwinner,sun50i-a64-video-engine",
5662306a36Sopenharmony_ci	"allwinner,sun50i-h5-video-engine",
5762306a36Sopenharmony_ci	NULL,
5862306a36Sopenharmony_ci};
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cistatic int sunxi_mbus_notifier(struct notifier_block *nb,
6162306a36Sopenharmony_ci			       unsigned long event, void *__dev)
6262306a36Sopenharmony_ci{
6362306a36Sopenharmony_ci	struct device *dev = __dev;
6462306a36Sopenharmony_ci	int ret;
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	if (event != BUS_NOTIFY_ADD_DEVICE)
6762306a36Sopenharmony_ci		return NOTIFY_DONE;
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	/*
7062306a36Sopenharmony_ci	 * Only the devices that need a large memory bandwidth do DMA
7162306a36Sopenharmony_ci	 * directly over the memory bus (called MBUS), instead of going
7262306a36Sopenharmony_ci	 * through the regular system bus.
7362306a36Sopenharmony_ci	 */
7462306a36Sopenharmony_ci	if (!of_device_compatible_match(dev->of_node, sunxi_mbus_devices))
7562306a36Sopenharmony_ci		return NOTIFY_DONE;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	/*
7862306a36Sopenharmony_ci	 * Devices with an interconnects property have the MBUS
7962306a36Sopenharmony_ci	 * relationship described in their DT and dealt with by
8062306a36Sopenharmony_ci	 * of_dma_configure, so we can just skip them.
8162306a36Sopenharmony_ci	 *
8262306a36Sopenharmony_ci	 * Older DTs or SoCs who are not clearly understood need to set
8362306a36Sopenharmony_ci	 * that DMA offset though.
8462306a36Sopenharmony_ci	 */
8562306a36Sopenharmony_ci	if (of_property_present(dev->of_node, "interconnects"))
8662306a36Sopenharmony_ci		return NOTIFY_DONE;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	ret = dma_direct_set_offset(dev, PHYS_OFFSET, 0, SZ_4G);
8962306a36Sopenharmony_ci	if (ret)
9062306a36Sopenharmony_ci		dev_err(dev, "Couldn't setup our DMA offset: %d\n", ret);
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	return NOTIFY_DONE;
9362306a36Sopenharmony_ci}
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_cistatic struct notifier_block sunxi_mbus_nb = {
9662306a36Sopenharmony_ci	.notifier_call = sunxi_mbus_notifier,
9762306a36Sopenharmony_ci};
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_cistatic const char * const sunxi_mbus_platforms[] __initconst = {
10062306a36Sopenharmony_ci	"allwinner,sun4i-a10",
10162306a36Sopenharmony_ci	"allwinner,sun5i-a10s",
10262306a36Sopenharmony_ci	"allwinner,sun5i-a13",
10362306a36Sopenharmony_ci	"allwinner,sun6i-a31",
10462306a36Sopenharmony_ci	"allwinner,sun7i-a20",
10562306a36Sopenharmony_ci	"allwinner,sun8i-a23",
10662306a36Sopenharmony_ci	"allwinner,sun8i-a33",
10762306a36Sopenharmony_ci	"allwinner,sun8i-a83t",
10862306a36Sopenharmony_ci	"allwinner,sun8i-h3",
10962306a36Sopenharmony_ci	"allwinner,sun8i-r40",
11062306a36Sopenharmony_ci	"allwinner,sun8i-v3",
11162306a36Sopenharmony_ci	"allwinner,sun8i-v3s",
11262306a36Sopenharmony_ci	"allwinner,sun9i-a80",
11362306a36Sopenharmony_ci	"allwinner,sun50i-a64",
11462306a36Sopenharmony_ci	"allwinner,sun50i-h5",
11562306a36Sopenharmony_ci	"nextthing,gr8",
11662306a36Sopenharmony_ci	NULL,
11762306a36Sopenharmony_ci};
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_cistatic int __init sunxi_mbus_init(void)
12062306a36Sopenharmony_ci{
12162306a36Sopenharmony_ci	if (!of_device_compatible_match(of_root, sunxi_mbus_platforms))
12262306a36Sopenharmony_ci		return 0;
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	bus_register_notifier(&platform_bus_type, &sunxi_mbus_nb);
12562306a36Sopenharmony_ci	return 0;
12662306a36Sopenharmony_ci}
12762306a36Sopenharmony_ciarch_initcall(sunxi_mbus_init);
128