1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /* Copyright 2017-2019 Qiang Yu <yuq825@gmail.com> */
3 
4 #include <linux/iopoll.h>
5 #include <linux/device.h>
6 
7 #include "lima_device.h"
8 #include "lima_l2_cache.h"
9 #include "lima_regs.h"
10 
11 #define l2_cache_write(reg, data) writel(data, ip->iomem + reg)
12 #define l2_cache_read(reg) readl(ip->iomem + reg)
13 
14 static int lima_l2_cache_wait_idle(struct lima_ip *ip)
15 {
16 	struct lima_device *dev = ip->dev;
17 	int err;
18 	u32 v;
19 
20 	err = readl_poll_timeout(ip->iomem + LIMA_L2_CACHE_STATUS, v,
21 				 !(v & LIMA_L2_CACHE_STATUS_COMMAND_BUSY),
22 				 0, 1000);
23 	if (err) {
24 		dev_err(dev->dev, "l2 cache wait command timeout\n");
25 		return err;
26 	}
27 	return 0;
28 }
29 
30 int lima_l2_cache_flush(struct lima_ip *ip)
31 {
32 	int ret;
33 
34 	spin_lock(&ip->data.lock);
35 	l2_cache_write(LIMA_L2_CACHE_COMMAND, LIMA_L2_CACHE_COMMAND_CLEAR_ALL);
36 	ret = lima_l2_cache_wait_idle(ip);
37 	spin_unlock(&ip->data.lock);
38 	return ret;
39 }
40 
41 int lima_l2_cache_init(struct lima_ip *ip)
42 {
43 	int i, err;
44 	u32 size;
45 	struct lima_device *dev = ip->dev;
46 
47 	/* l2_cache2 only exists when one of PP4-7 present */
48 	if (ip->id == lima_ip_l2_cache2) {
49 		for (i = lima_ip_pp4; i <= lima_ip_pp7; i++) {
50 			if (dev->ip[i].present)
51 				break;
52 		}
53 		if (i > lima_ip_pp7)
54 			return -ENODEV;
55 	}
56 
57 	spin_lock_init(&ip->data.lock);
58 
59 	size = l2_cache_read(LIMA_L2_CACHE_SIZE);
60 	dev_info(dev->dev, "l2 cache %uK, %u-way, %ubyte cache line, %ubit external bus\n",
61 		 1 << (((size >> 16) & 0xff) - 10),
62 		 1 << ((size >> 8) & 0xff),
63 		 1 << (size & 0xff),
64 		 1 << ((size >> 24) & 0xff));
65 
66 	err = lima_l2_cache_flush(ip);
67 	if (err)
68 		return err;
69 
70 	l2_cache_write(LIMA_L2_CACHE_ENABLE,
71 		       LIMA_L2_CACHE_ENABLE_ACCESS|LIMA_L2_CACHE_ENABLE_READ_ALLOCATE);
72 	l2_cache_write(LIMA_L2_CACHE_MAX_READS, 0x1c);
73 
74 	return 0;
75 }
76 
77 void lima_l2_cache_fini(struct lima_ip *ip)
78 {
79 
80 }
81