xref: /OK3568_Linux_fs/u-boot/drivers/misc/rockchip_decompress.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier:     GPL-2.0+
2 /*
3  * Copyright (C) 2019 Rockchip Electronics Co., Ltd
4  */
5 #include <common.h>
6 #include <asm/io.h>
7 #include <clk.h>
8 #include <dm.h>
9 #include <linux/bitops.h>
10 #include <misc.h>
11 #include <irq-generic.h>
12 #include <reset.h>
13 
14 DECLARE_GLOBAL_DATA_PTR;
15 
16 #define DECOM_CTRL		0x0
17 #define DECOM_ENR		0x4
18 #define DECOM_RADDR		0x8
19 #define DECOM_WADDR		0xc
20 #define DECOM_UDDSL		0x10
21 #define DECOM_UDDSH		0x14
22 #define DECOM_TXTHR		0x18
23 #define DECOM_RXTHR		0x1c
24 #define DECOM_SLEN		0x20
25 #define DECOM_STAT		0x24
26 #define DECOM_ISR		0x28
27 #define DECOM_IEN		0x2c
28 #define DECOM_AXI_STAT		0x30
29 #define DECOM_TSIZEL		0x34
30 #define DECOM_TSIZEH		0x38
31 #define DECOM_MGNUM		0x3c
32 #define DECOM_FRAME		0x40
33 #define DECOM_DICTID		0x44
34 #define DECOM_CSL		0x48
35 #define DECOM_CSH		0x4c
36 #define DECOM_LMTSL             0x50
37 #define DECOM_LMTSH             0x54
38 
39 #define LZ4_HEAD_CSUM_CHECK_EN	BIT(1)
40 #define LZ4_BLOCK_CSUM_CHECK_EN	BIT(2)
41 #define LZ4_CONT_CSUM_CHECK_EN	BIT(3)
42 
43 #define DSOLIEN			BIT(19)
44 #define ZDICTEIEN		BIT(18)
45 #define GCMEIEN			BIT(17)
46 #define GIDEIEN			BIT(16)
47 #define CCCEIEN			BIT(15)
48 #define BCCEIEN			BIT(14)
49 #define HCCEIEN			BIT(13)
50 #define CSEIEN			BIT(12)
51 #define DICTEIEN		BIT(11)
52 #define VNEIEN			BIT(10)
53 #define WNEIEN			BIT(9)
54 #define RDCEIEN			BIT(8)
55 #define WRCEIEN			BIT(7)
56 #define DISEIEN			BIT(6)
57 #define LENEIEN			BIT(5)
58 #define LITEIEN			BIT(4)
59 #define SQMEIEN			BIT(3)
60 #define SLCIEN			BIT(2)
61 #define HDEIEN			BIT(1)
62 #define DSIEN			BIT(0)
63 
64 #define DECOM_STOP		BIT(0)
65 #define DECOM_COMPLETE		BIT(0)
66 #define DECOM_GZIP_MODE		BIT(4)
67 #define DECOM_ZLIB_MODE		BIT(5)
68 #define DECOM_DEFLATE_MODE	BIT(0)
69 #define DECOM_AXI_IDLE		BIT(4)
70 #define DECOM_LZ4_MODE		0
71 
72 #define DECOM_ENABLE		0x1
73 #define DECOM_DISABLE		0x0
74 
75 #define DECOM_IRQ		0xffff /* fixme */
76 
77 #define DECOM_INT_MASK \
78 	(DSOLIEN | ZDICTEIEN | GCMEIEN | GIDEIEN | \
79 	CCCEIEN | BCCEIEN | HCCEIEN | CSEIEN | \
80 	DICTEIEN | VNEIEN | WNEIEN | RDCEIEN | WRCEIEN | \
81 	DISEIEN | LENEIEN | LITEIEN | SQMEIEN | SLCIEN | \
82 	HDEIEN | DSIEN)
83 
84 #define DCLK_DECOM		400 * 1000 * 1000
85 
86 struct rockchip_decom_priv {
87 	struct reset_ctl rst;
88 	void __iomem *base;
89 	bool idle_check_once;
90 	bool done;
91 	struct clk dclk;
92 	int cached; /* 1: access the data through dcache; 0: no dcache */
93 };
94 
rockchip_decom_start(struct udevice * dev,void * buf)95 static int rockchip_decom_start(struct udevice *dev, void *buf)
96 {
97 	struct rockchip_decom_priv *priv = dev_get_priv(dev);
98 	struct decom_param *param = (struct decom_param *)buf;
99 	unsigned int limit_lo = param->size_dst & 0xffffffff;
100 	unsigned int limit_hi = param->size_dst >> 32;
101 
102 #if CONFIG_IS_ENABLED(DM_RESET)
103 	reset_assert(&priv->rst);
104 	udelay(10);
105 	reset_deassert(&priv->rst);
106 #endif
107 	/*
108 	 * Purpose:
109 	 *    src: clean dcache to get the real compressed data from ddr.
110 	 *    dst: invalidate dcache.
111 	 *
112 	 * flush_dcache_all() operating on set/way is faster than
113 	 * flush_cache() and invalidate_dcache_range() operating
114 	 * on virtual address.
115 	 */
116 	if (!priv->cached)
117 		flush_dcache_all();
118 
119 	priv->done = false;
120 
121 	if (param->mode == DECOM_LZ4)
122 		writel(LZ4_CONT_CSUM_CHECK_EN |
123 		       LZ4_HEAD_CSUM_CHECK_EN |
124 		       LZ4_BLOCK_CSUM_CHECK_EN |
125 		       DECOM_LZ4_MODE,
126 		       priv->base + DECOM_CTRL);
127 	else if (param->mode == DECOM_GZIP)
128 		writel(DECOM_DEFLATE_MODE | DECOM_GZIP_MODE,
129 		       priv->base + DECOM_CTRL);
130 	else if (param->mode == DECOM_ZLIB)
131 		writel(DECOM_DEFLATE_MODE | DECOM_ZLIB_MODE,
132 		       priv->base + DECOM_CTRL);
133 
134 	writel(param->addr_src, priv->base + DECOM_RADDR);
135 	writel(param->addr_dst, priv->base + DECOM_WADDR);
136 
137 	writel(limit_lo, priv->base + DECOM_LMTSL);
138 	writel(limit_hi, priv->base + DECOM_LMTSH);
139 
140 	writel(DECOM_ENABLE, priv->base + DECOM_ENR);
141 
142 	priv->idle_check_once = true;
143 
144 	return 0;
145 }
146 
rockchip_decom_stop(struct udevice * dev)147 static int rockchip_decom_stop(struct udevice *dev)
148 {
149 	struct rockchip_decom_priv *priv = dev_get_priv(dev);
150 
151 	writel(DECOM_DISABLE, priv->base + DECOM_ENR);
152 
153 	return 0;
154 }
155 
156 /* Caller must call this function to check if decompress done */
rockchip_decom_done_poll(struct udevice * dev)157 static int rockchip_decom_done_poll(struct udevice *dev)
158 {
159 	struct rockchip_decom_priv *priv = dev_get_priv(dev);
160 
161 	/*
162 	 * Test the decom is idle first time.
163 	 */
164 	if (!priv->idle_check_once)
165 		return !(readl(priv->base + DECOM_AXI_STAT) & DECOM_AXI_IDLE);
166 
167 	return !(readl(priv->base + DECOM_STAT) & DECOM_COMPLETE);
168 }
169 
rockchip_decom_capability(u32 * buf)170 static int rockchip_decom_capability(u32 *buf)
171 {
172 	*buf = DECOM_GZIP | DECOM_LZ4;
173 
174 	return 0;
175 }
176 
rockchip_decom_data_size(struct udevice * dev,u64 * buf)177 static int rockchip_decom_data_size(struct udevice *dev, u64 *buf)
178 {
179 	struct rockchip_decom_priv *priv = dev_get_priv(dev);
180 	struct decom_param *param = (struct decom_param *)buf;
181 	u32 sizel, sizeh;
182 
183 	sizel = readl(priv->base + DECOM_TSIZEL);
184 	sizeh = readl(priv->base + DECOM_TSIZEH);
185 	param->size_dst = sizel | ((u64)sizeh << 32);
186 
187 	return 0;
188 }
189 
190 /* Caller must fill in param @buf which represent struct decom_param */
rockchip_decom_ioctl(struct udevice * dev,unsigned long request,void * buf)191 static int rockchip_decom_ioctl(struct udevice *dev, unsigned long request,
192 				void *buf)
193 {
194 	int ret = -EINVAL;
195 
196 	switch (request) {
197 	case IOCTL_REQ_START:
198 		ret = rockchip_decom_start(dev, buf);
199 		break;
200 	case IOCTL_REQ_POLL:
201 		ret = rockchip_decom_done_poll(dev);
202 		break;
203 	case IOCTL_REQ_STOP:
204 		ret = rockchip_decom_stop(dev);
205 		break;
206 	case IOCTL_REQ_CAPABILITY:
207 		ret = rockchip_decom_capability(buf);
208 		break;
209 	case IOCTL_REQ_DATA_SIZE:
210 		ret = rockchip_decom_data_size(dev, buf);
211 		break;
212 	default:
213 		printf("Unsupported ioctl: %ld\n", (ulong)request);
214 		break;
215 	}
216 
217 	return ret;
218 }
219 
220 static const struct misc_ops rockchip_decom_ops = {
221 	.ioctl = rockchip_decom_ioctl,
222 };
223 
rockchip_decom_ofdata_to_platdata(struct udevice * dev)224 static int rockchip_decom_ofdata_to_platdata(struct udevice *dev)
225 {
226 	struct rockchip_decom_priv *priv = dev_get_priv(dev);
227 
228 	priv->base = dev_read_addr_ptr(dev);
229 	if (!priv->base)
230 		return -ENOENT;
231 
232 	priv->cached = dev_read_u32_default(dev, "data-cached", 0);
233 
234 	return 0;
235 }
236 
rockchip_decom_probe(struct udevice * dev)237 static int rockchip_decom_probe(struct udevice *dev)
238 {
239 	struct rockchip_decom_priv *priv = dev_get_priv(dev);
240 	int ret;
241 
242 #if CONFIG_IS_ENABLED(DM_RESET)
243 	ret = reset_get_by_name(dev, "dresetn", &priv->rst);
244 	if (ret) {
245 		debug("reset_get_by_name() failed: %d\n", ret);
246 		return ret;
247 	}
248 #endif
249 
250 	ret = clk_get_by_index(dev, 1, &priv->dclk);
251 	if (ret < 0)
252 		return ret;
253 
254 	ret = clk_set_rate(&priv->dclk, DCLK_DECOM);
255 	if (ret < 0)
256 		return ret;
257 
258 	return 0;
259 }
260 
261 static const struct udevice_id rockchip_decom_ids[] = {
262 	{ .compatible = "rockchip,hw-decompress" },
263 	{}
264 };
265 
266 U_BOOT_DRIVER(rockchip_hw_decompress) = {
267 	.name = "rockchip_hw_decompress",
268 	.id = UCLASS_MISC,
269 	.of_match = rockchip_decom_ids,
270 	.probe = rockchip_decom_probe,
271 	.ofdata_to_platdata = rockchip_decom_ofdata_to_platdata,
272 	.priv_auto_alloc_size = sizeof(struct rockchip_decom_priv),
273 	.ops = &rockchip_decom_ops,
274 };
275