kernel: bump 5.4 to 5.4.106
[openwrt/openwrt.git] / target / linux / layerscape / patches-5.4 / 802-can-0027-can-flexcan-add-CAN-wakeup-function-for-i.MX8.patch
1 From 140de383d9d26e8be300b7ba86d56b47898cd8c9 Mon Sep 17 00:00:00 2001
2 From: Joakim Zhang <qiangqing.zhang@nxp.com>
3 Date: Tue, 30 Jul 2019 18:01:11 +0800
4 Subject: [PATCH] can: flexcan: add CAN wakeup function for i.MX8
5
6 The System Controller Firmware (SCFW) is a low-level system function
7 which runs on a dedicated Cortex-M core to provide power, clock, and
8 resource management. It exists on some i.MX8 processors. e.g. i.MX8QM
9 (QM, QP), and i.MX8QX (QXP, DX).
10
11 SCU driver manages the IPC interface between host CPU and the
12 SCU firmware running on M4.
13
14 For i.MX8, stop mode request is controlled by System Controller Unit(SCU)
15 firmware.
16
17 Signed-off-by: Joakim Zhang <qiangqing.zhang@nxp.com>
18 ---
19 drivers/net/can/flexcan.c | 99 ++++++++++++++++++++++++++++++++++++++++-------
20 1 file changed, 86 insertions(+), 13 deletions(-)
21
22 --- a/drivers/net/can/flexcan.c
23 +++ b/drivers/net/can/flexcan.c
24 @@ -29,6 +29,11 @@
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/regmap.h>
27
28 +#ifdef CONFIG_IMX_SCU_SOC
29 +#include <linux/firmware/imx/sci.h>
30 +#include <dt-bindings/firmware/imx/rsrc.h>
31 +#endif
32 +
33 #define DRV_NAME "flexcan"
34
35 /* 8 for RX fifo and 2 error handling */
36 @@ -223,6 +228,7 @@
37 #define FLEXCAN_QUIRK_DEFAULT_BIG_ENDIAN BIT(7) /* default to BE register access */
38 #define FLEXCAN_QUIRK_SETUP_STOP_MODE BIT(8) /* Setup stop mode to support wakeup */
39 #define FLEXCAN_QUIRK_TIMESTAMP_SUPPORT_FD BIT(9) /* Use timestamp then support can fd mode */
40 +#define FLEXCAN_QUIRK_USE_SCFW BIT(10) /* Use System Controller Firmware */
41
42 /* Structure of the message buffer */
43 struct flexcan_mb {
44 @@ -323,6 +329,11 @@ struct flexcan_priv {
45 struct regulator *reg_xceiver;
46 struct flexcan_stop_mode stm;
47
48 +#ifdef CONFIG_IMX_SCU_SOC
49 + /* IPC handle when enable stop mode by System Controller firmware(scfw) */
50 + struct imx_sc_ipc *sc_ipc_handle;
51 +#endif
52 +
53 /* Read and Write APIs */
54 u32 (*read)(void __iomem *addr);
55 void (*write)(u32 val, void __iomem *addr);
56 @@ -352,7 +363,8 @@ static const struct flexcan_devtype_data
57 static struct flexcan_devtype_data fsl_imx8qm_devtype_data = {
58 .quirks = FLEXCAN_QUIRK_DISABLE_RXFG | FLEXCAN_QUIRK_ENABLE_EACEN_RRS |
59 FLEXCAN_QUIRK_USE_OFF_TIMESTAMP | FLEXCAN_QUIRK_BROKEN_PERR_STATE |
60 - FLEXCAN_QUIRK_TIMESTAMP_SUPPORT_FD,
61 + FLEXCAN_QUIRK_TIMESTAMP_SUPPORT_FD | FLEXCAN_QUIRK_SETUP_STOP_MODE |
62 + FLEXCAN_QUIRK_USE_SCFW,
63 };
64
65 static const struct flexcan_devtype_data fsl_vf610_devtype_data = {
66 @@ -503,6 +515,32 @@ static void flexcan_enable_wakeup_irq(st
67 priv->write(reg_mcr, &regs->mcr);
68 }
69
70 +#ifdef CONFIG_IMX_SCU_SOC
71 +static void flexcan_stop_mode_enable_scfw(struct flexcan_priv *priv, bool enabled)
72 +{
73 + struct device_node *np = priv->dev->of_node;
74 + u32 rsrc_id, val;
75 + int idx;
76 +
77 + idx = of_alias_get_id(np, "can");
78 + if (idx == 0)
79 + rsrc_id = IMX_SC_R_CAN_0;
80 + else if (idx == 1)
81 + rsrc_id = IMX_SC_R_CAN_1;
82 + else
83 + rsrc_id = IMX_SC_R_CAN_2;
84 +
85 + val = enabled ? 1 : 0;
86 + /* stop mode request */
87 + imx_sc_misc_set_control(priv->sc_ipc_handle, rsrc_id, IMX_SC_C_IPG_STOP, val);
88 +}
89 +#else
90 +static int flexcan_stop_mode_enable_scfw(struct flexcan_priv *priv, bool enabled)
91 +{
92 + return 0;
93 +}
94 +#endif
95 +
96 static inline int flexcan_enter_stop_mode(struct flexcan_priv *priv)
97 {
98 struct flexcan_regs __iomem *regs = priv->regs;
99 @@ -512,9 +550,12 @@ static inline int flexcan_enter_stop_mod
100 reg_mcr |= FLEXCAN_MCR_SLF_WAK;
101 priv->write(reg_mcr, &regs->mcr);
102
103 - /* enable stop request */
104 - regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
105 - 1 << priv->stm.req_bit, 1 << priv->stm.req_bit);
106 + /* enable stop request */
107 + if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_SCFW)
108 + flexcan_stop_mode_enable_scfw(priv, true);
109 + else
110 + regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
111 + 1 << priv->stm.req_bit, 1 << priv->stm.req_bit);
112
113 return flexcan_low_power_enter_ack(priv);
114 }
115 @@ -525,8 +566,11 @@ static inline int flexcan_exit_stop_mode
116 u32 reg_mcr;
117
118 /* remove stop request */
119 - regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
120 - 1 << priv->stm.req_bit, 0);
121 + if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_SCFW)
122 + flexcan_stop_mode_enable_scfw(priv, false);
123 + else
124 + regmap_update_bits(priv->stm.gpr, priv->stm.req_gpr,
125 + 1 << priv->stm.req_bit, 0);
126
127
128 reg_mcr = priv->read(&regs->mcr);
129 @@ -1776,11 +1820,6 @@ static int flexcan_setup_stop_mode(struc
130 gpr_np->full_name, priv->stm.req_gpr, priv->stm.req_bit,
131 priv->stm.ack_gpr, priv->stm.ack_bit);
132
133 - device_set_wakeup_capable(&pdev->dev, true);
134 -
135 - if (of_property_read_bool(np, "wakeup-source"))
136 - device_set_wakeup_enable(&pdev->dev, true);
137 -
138 return 0;
139
140 out_put_node:
141 @@ -1788,6 +1827,30 @@ out_put_node:
142 return ret;
143 }
144
145 +#ifdef CONFIG_IMX_SCU_SOC
146 +static int flexcan_setup_stop_mode_scfw(struct platform_device *pdev)
147 +{
148 + struct net_device *dev = platform_get_drvdata(pdev);
149 + struct flexcan_priv *priv;
150 + int ret;
151 +
152 + priv = netdev_priv(dev);
153 +
154 + ret = imx_scu_get_handle(&(priv->sc_ipc_handle));
155 + if (ret < 0) {
156 + dev_err(&pdev->dev, "get ipc handle used by SCU failed\n");
157 + return ret;
158 + }
159 +
160 + return 0;
161 +}
162 +#else
163 +static int flexcan_setup_stop_mode_scfw(struct platform_device *pdev)
164 +{
165 + return 0;
166 +}
167 +#endif
168 +
169 static const struct of_device_id flexcan_of_match[] = {
170 { .compatible = "fsl,imx8qm-flexcan", .data = &fsl_imx8qm_devtype_data, },
171 { .compatible = "fsl,imx6q-flexcan", .data = &fsl_imx6q_devtype_data, },
172 @@ -1930,9 +1993,19 @@ static int flexcan_probe(struct platform
173 devm_can_led_init(dev);
174
175 if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE) {
176 - err = flexcan_setup_stop_mode(pdev);
177 - if (err)
178 + if (priv->devtype_data->quirks & FLEXCAN_QUIRK_USE_SCFW)
179 + err = flexcan_setup_stop_mode_scfw(pdev);
180 + else
181 + err = flexcan_setup_stop_mode(pdev);
182 +
183 + if (err) {
184 dev_dbg(&pdev->dev, "failed to setup stop-mode\n");
185 + } else {
186 + device_set_wakeup_capable(&pdev->dev, true);
187 +
188 + if (of_property_read_bool(pdev->dev.of_node, "wakeup-source"))
189 + device_set_wakeup_enable(&pdev->dev, true);
190 + }
191 }
192
193 return 0;