blob: cc53a3919bea2d5b8184c80404be956faa993fe7 [file] [log] [blame]
developer4f0d2ba2023-08-21 17:33:25 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2023 MediaTek Inc.
4 *
5 * Author: Chris.Chou <chris.chou@mediatek.com>
6 * Ren-Ting Wang <ren-ting.wang@mediatek.com>
7 */
8
9#include <linux/module.h>
10#include <linux/netdevice.h>
11#include <linux/of.h>
12#include <linux/of_address.h>
13#include <linux/of_platform.h>
14#include <linux/platform_device.h>
15
16#include <mtk_eth_soc.h>
17#include <mtk_hnat/hnat.h>
18
19#include <crypto-eip/ddk/configs/cs_hwpal_ext.h>
20
21#include "crypto-eip/crypto-eip.h"
22#include "crypto-eip/ddk-wrapper.h"
23#include "crypto-eip/internal.h"
24
25#define DRIVER_AUTHOR "Ren-Ting Wang <ren-ting.wang@mediatek.com, " \
26 "Chris.Chou <chris.chou@mediatek.com"
27
28struct mtk_crypto mcrypto;
29struct device *crypto_dev;
30
31inline void crypto_eth_write(u32 reg, u32 val)
32{
33 writel(val, mcrypto.eth_base + reg);
34}
35
36static inline void crypto_eip_write(u32 reg, u32 val)
37{
38 writel(val, mcrypto.crypto_base + reg);
39}
40
41static inline void crypto_eip_set(u32 reg, u32 mask)
42{
43 setbits(mcrypto.crypto_base + reg, mask);
44}
45
46static inline void crypto_eip_clr(u32 reg, u32 mask)
47{
48 clrbits(mcrypto.crypto_base + reg, mask);
49}
50
51static inline void crypto_eip_rmw(u32 reg, u32 mask, u32 val)
52{
53 clrsetbits(mcrypto.crypto_base + reg, mask, val);
54}
55
56static inline u32 crypto_eip_read(u32 reg)
57{
58 return readl(mcrypto.crypto_base + reg);
59}
60
61static bool mtk_crypto_eip_offloadable(struct sk_buff *skb)
62{
63 /* TODO: check is esp */
64 return true;
65}
66
67static const struct xfrmdev_ops mtk_xfrmdev_ops = {
68 .xdo_dev_state_add = mtk_xfrm_offload_state_add,
69 .xdo_dev_state_delete = mtk_xfrm_offload_state_delete,
70 .xdo_dev_state_free = mtk_xfrm_offload_state_free,
71 .xdo_dev_offload_ok = mtk_xfrm_offload_ok,
72
73 /* Not support at v5.4*/
74 .xdo_dev_policy_add = mtk_xfrm_offload_policy_add,
75};
76
77static void mtk_crypto_xfrm_offload_deinit(struct mtk_eth *eth)
78{
79 int i;
80
81 mtk_crypto_offloadable = NULL;
82
83 for (i = 0; i < MTK_MAC_COUNT; i++) {
84 eth->netdev[i]->xfrmdev_ops = NULL;
85 eth->netdev[i]->features &= (~NETIF_F_HW_ESP);
86 eth->netdev[i]->hw_enc_features &= (~NETIF_F_HW_ESP);
87 rtnl_lock();
88 netdev_change_features(eth->netdev[i]);
89 rtnl_unlock();
90 }
91}
92
93static void mtk_crypto_xfrm_offload_init(struct mtk_eth *eth)
94{
95 int i;
96
97 for (i = 0; i < MTK_MAC_COUNT; i++) {
98 eth->netdev[i]->xfrmdev_ops = &mtk_xfrmdev_ops;
99 eth->netdev[i]->features |= NETIF_F_HW_ESP;
100 eth->netdev[i]->hw_enc_features |= NETIF_F_HW_ESP;
101 rtnl_lock();
102 netdev_change_features(eth->netdev[i]);
103 rtnl_unlock();
104 }
105
106 mtk_crypto_offloadable = mtk_crypto_eip_offloadable;
107}
108
109static int __init mtk_crypto_eth_dts_init(struct platform_device *pdev)
110{
111 struct platform_device *eth_pdev;
112 struct device_node *crypto_node;
113 struct device_node *eth_node;
114 struct resource res;
115 int ret = 0;
116
117 crypto_node = pdev->dev.of_node;
118
119 eth_node = of_parse_phandle(crypto_node, "eth", 0);
120 if (!eth_node)
121 return -ENODEV;
122
123 eth_pdev = of_find_device_by_node(eth_node);
124 if (!eth_pdev) {
125 ret = -ENODEV;
126 goto out;
127 }
128
129 if (!eth_pdev->dev.driver) {
130 ret = -EFAULT;
131 goto out;
132 }
133
134 if (of_address_to_resource(eth_node, 0, &res)) {
135 ret = -ENXIO;
136 goto out;
137 }
138
139 mcrypto.eth_base = devm_ioremap(&pdev->dev,
140 res.start, resource_size(&res));
141 if (!mcrypto.eth_base) {
142 ret = -ENOMEM;
143 goto out;
144 }
145
146 mcrypto.eth = platform_get_drvdata(eth_pdev);
147
148out:
149 of_node_put(eth_node);
150
151 return ret;
152}
153
154static int __init mtk_crypto_eip_dts_init(void)
155{
156 struct platform_device *crypto_pdev;
157 struct device_node *crypto_node;
158 struct resource res;
159 int ret;
160
161 crypto_node = of_find_compatible_node(NULL, NULL, HWPAL_PLATFORM_DEVICE_NAME);
162 if (!crypto_node)
163 return -ENODEV;
164
165 crypto_pdev = of_find_device_by_node(crypto_node);
166 if (!crypto_pdev) {
167 ret = -ENODEV;
168 goto out;
169 }
170
171 /* check crypto platform device is ready */
172 if (!crypto_pdev->dev.driver) {
173 ret = -EFAULT;
174 goto out;
175 }
176
177 if (of_address_to_resource(crypto_node, 0, &res)) {
178 ret = -ENXIO;
179 goto out;
180 }
181
182 mcrypto.crypto_base = devm_ioremap(&crypto_pdev->dev,
183 res.start, resource_size(&res));
184 if (!mcrypto.crypto_base) {
185 ret = -ENOMEM;
186 goto out;
187 }
188
189 ret = mtk_crypto_eth_dts_init(crypto_pdev);
190 if (ret)
191 goto out;
192
193 crypto_dev = &crypto_pdev->dev;
194
195out:
196 of_node_put(crypto_node);
197
198 return ret;
199}
200
201static int __init mtk_crypto_eip_hw_init(void)
202{
203 crypto_eip_write(EIP197_FORCE_CLK_ON, 0xffffffff);
204
205 crypto_eip_write(EIP197_FORCE_CLK_ON2, 0xffffffff);
206
207 /* TODO: adjust AXI burst? */
208
209 mtk_ddk_pec_init();
210
211 return 0;
212}
213
214static void __exit mtk_crypto_eip_hw_deinit(void)
215{
216 mtk_ddk_pec_deinit();
217
218 crypto_eip_write(EIP197_FORCE_CLK_ON, 0);
219
220 crypto_eip_write(EIP197_FORCE_CLK_ON2, 0);
221}
222
223static int __init mtk_crypto_eip_init(void)
224{
225 int ret;
226
227 ret = mtk_crypto_eip_dts_init();
228 if (ret) {
229 CRYPTO_ERR("crypto-eip dts init failed: %d\n", ret);
230 return ret;
231 }
232
233 ret = mtk_crypto_eip_hw_init();
234 if (ret) {
235 CRYPTO_ERR("crypto-eip hw init failed: %d\n", ret);
236 return ret;
237 }
238
239 mtk_crypto_xfrm_offload_init(mcrypto.eth);
240
241 CRYPTO_INFO("crypto-eip init done\n");
242
243 return ret;
244}
245
246static void __exit mtk_crypto_eip_exit(void)
247{
248 /* TODO: deactivate all tunnel */
249
250 mtk_crypto_xfrm_offload_deinit(mcrypto.eth);
251
252 mtk_crypto_eip_hw_deinit();
253}
254
255module_init(mtk_crypto_eip_init);
256module_exit(mtk_crypto_eip_exit);
257
258MODULE_LICENSE("GPL");
259MODULE_DESCRIPTION("MediaTek Crypto EIP Control Driver");
260MODULE_AUTHOR(DRIVER_AUTHOR);