xref: /OK3568_Linux_fs/kernel/drivers/soc/rockchip/rockchip_thunderboot_mmc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2020 Rockchip Electronics Co., Ltd.
4  */
5 #include <linux/iopoll.h>
6 #include <linux/kernel.h>
7 #include <linux/kthread.h>
8 #include <linux/mm.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/of_address.h>
12 #include <linux/of_device.h>
13 #include <linux/platform_device.h>
14 #include <linux/soc/rockchip/rockchip_decompress.h>
15 #include <linux/soc/rockchip/rockchip_thunderboot_crypto.h>
16 
17 #define SDMMC_RINTSTS		0x044
18 #define SDMMC_STATUS		0x048
19 #define SDMMC_IDSTS		0x08c
20 #define SDMMC_INTR_ERROR	0xB7C2
21 
rk_tb_mmc_thread(void * p)22 static int rk_tb_mmc_thread(void *p)
23 {
24 	int ret = 0;
25 	struct platform_device *pdev = p;
26 	void __iomem *regs;
27 	struct resource *res;
28 	struct device_node *rds, *rdd, *dma;
29 	struct device *dev = &pdev->dev;
30 	u32 status;
31 
32 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
33 	regs = ioremap(res->start, resource_size(res));
34 	if (!regs) {
35 		dev_err(dev, "ioremap failed for resource %pR\n", res);
36 		return -ENOMEM;
37 	}
38 
39 	rds = of_parse_phandle(dev->of_node, "memory-region-src", 0);
40 	rdd = of_parse_phandle(dev->of_node, "memory-region-dst", 0);
41 	dma = of_parse_phandle(dev->of_node, "memory-region-idmac", 0);
42 
43 	if (readl_poll_timeout(regs + SDMMC_STATUS, status,
44 			       !(status & (BIT(10) | GENMASK(7, 4))), 100,
45 			       500 * USEC_PER_MSEC))
46 		dev_err(dev, "Controller is occupied!\n");
47 
48 	if (readl_poll_timeout(regs + SDMMC_IDSTS, status,
49 			       !(status & GENMASK(16, 13)), 100,
50 			       500 * USEC_PER_MSEC))
51 		dev_err(dev, "DMA is still running!\n");
52 
53 	status = readl_relaxed(regs + SDMMC_RINTSTS);
54 	if (status & SDMMC_INTR_ERROR) {
55 		dev_err(dev, "SDMMC_INTR_ERROR status: 0x%08x\n", status);
56 		goto out;
57 	}
58 
59 	/* Parse ramdisk addr and help start decompressing */
60 	if (rds && rdd) {
61 		struct resource src, dst;
62 		u32 rdk_size = 0;
63 		const u32 *digest_org;
64 
65 		if (of_address_to_resource(rds, 0, &src) >= 0 &&
66 		    of_address_to_resource(rdd, 0, &dst) >= 0) {
67 			if (IS_ENABLED(CONFIG_ROCKCHIP_THUNDER_BOOT_CRYPTO)) {
68 				of_property_read_u32(rds, "size", &rdk_size);
69 				digest_org = of_get_property(rds->child, "value", NULL);
70 				if (digest_org && rdk_size)
71 					rk_tb_sha256((dma_addr_t)src.start, rdk_size,
72 						     (void *)digest_org);
73 			}
74 			/*
75 			 * Decompress HW driver will free reserved area of
76 			 * memory-region-src.
77 			 */
78 			ret = rk_decom_start(GZIP_MOD, src.start,
79 					     dst.start,
80 					     resource_size(&dst));
81 			if (ret < 0)
82 				dev_err(dev, "failed to start decom\n");
83 		}
84 	}
85 
86 	/* Release idmac descriptor */
87 	if (dma) {
88 		struct resource idmac;
89 
90 		ret = of_address_to_resource(dma, 0, &idmac);
91 		if (ret >= 0)
92 			free_reserved_area(phys_to_virt(idmac.start),
93 					   phys_to_virt(idmac.start) + resource_size(&idmac),
94 					   -1, "memory-region-idmac");
95 	}
96 
97 out:
98 	of_node_put(rds);
99 	of_node_put(rdd);
100 	of_node_put(dma);
101 	iounmap(regs);
102 
103 	return 0;
104 }
105 
rk_tb_mmc_probe(struct platform_device * pdev)106 static int __init rk_tb_mmc_probe(struct platform_device *pdev)
107 {
108 	int ret = 0;
109 	struct task_struct *tsk;
110 
111 	tsk = kthread_run(rk_tb_mmc_thread, pdev, "tb_mmc");
112 	if (IS_ERR(tsk)) {
113 		ret = PTR_ERR(tsk);
114 		dev_err(&pdev->dev, "start thread failed (%d)\n", ret);
115 	}
116 
117 	return ret;
118 }
119 
120 #ifdef CONFIG_OF
121 static const struct of_device_id rk_tb_mmc_dt_match[] = {
122 	{ .compatible = "rockchip,thunder-boot-mmc" },
123 	{},
124 };
125 #endif
126 
127 static struct platform_driver rk_tb_mmc_driver = {
128 	.driver		= {
129 		.name	= "rockchip_thunder_boot_mmc",
130 		.of_match_table = rk_tb_mmc_dt_match,
131 	},
132 };
133 
rk_tb_mmc_init(void)134 static int __init rk_tb_mmc_init(void)
135 {
136 	struct device_node *node;
137 
138 	node = of_find_matching_node(NULL, rk_tb_mmc_dt_match);
139 	if (node) {
140 		of_platform_device_create(node, NULL, NULL);
141 		of_node_put(node);
142 		return platform_driver_probe(&rk_tb_mmc_driver, rk_tb_mmc_probe);
143 	}
144 
145 	return 0;
146 }
147 
148 pure_initcall(rk_tb_mmc_init);
149