Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / char / ipmi / kcs_bmc_aspeed.c
bloba140203c079bad7cbd52358985daf4963b7eabc1
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2015-2018, Intel Corporation.
4 */
6 #define pr_fmt(fmt) "aspeed-kcs-bmc: " fmt
8 #include <linux/atomic.h>
9 #include <linux/errno.h>
10 #include <linux/interrupt.h>
11 #include <linux/io.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_address.h>
16 #include <linux/platform_device.h>
17 #include <linux/poll.h>
18 #include <linux/regmap.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/timer.h>
23 #include "kcs_bmc.h"
26 #define DEVICE_NAME "ast-kcs-bmc"
28 #define KCS_CHANNEL_MAX 4
30 /* mapped to lpc-bmc@0 IO space */
31 #define LPC_HICR0 0x000
32 #define LPC_HICR0_LPC3E BIT(7)
33 #define LPC_HICR0_LPC2E BIT(6)
34 #define LPC_HICR0_LPC1E BIT(5)
35 #define LPC_HICR2 0x008
36 #define LPC_HICR2_IBFIF3 BIT(3)
37 #define LPC_HICR2_IBFIF2 BIT(2)
38 #define LPC_HICR2_IBFIF1 BIT(1)
39 #define LPC_HICR4 0x010
40 #define LPC_HICR4_LADR12AS BIT(7)
41 #define LPC_HICR4_KCSENBL BIT(2)
42 #define LPC_LADR3H 0x014
43 #define LPC_LADR3L 0x018
44 #define LPC_LADR12H 0x01C
45 #define LPC_LADR12L 0x020
46 #define LPC_IDR1 0x024
47 #define LPC_IDR2 0x028
48 #define LPC_IDR3 0x02C
49 #define LPC_ODR1 0x030
50 #define LPC_ODR2 0x034
51 #define LPC_ODR3 0x038
52 #define LPC_STR1 0x03C
53 #define LPC_STR2 0x040
54 #define LPC_STR3 0x044
56 /* mapped to lpc-host@80 IO space */
57 #define LPC_HICRB 0x080
58 #define LPC_HICRB_IBFIF4 BIT(1)
59 #define LPC_HICRB_LPC4E BIT(0)
60 #define LPC_LADR4 0x090
61 #define LPC_IDR4 0x094
62 #define LPC_ODR4 0x098
63 #define LPC_STR4 0x09C
65 struct aspeed_kcs_bmc {
66 struct regmap *map;
70 static u8 aspeed_kcs_inb(struct kcs_bmc *kcs_bmc, u32 reg)
72 struct aspeed_kcs_bmc *priv = kcs_bmc_priv(kcs_bmc);
73 u32 val = 0;
74 int rc;
76 rc = regmap_read(priv->map, reg, &val);
77 WARN(rc != 0, "regmap_read() failed: %d\n", rc);
79 return rc == 0 ? (u8) val : 0;
82 static void aspeed_kcs_outb(struct kcs_bmc *kcs_bmc, u32 reg, u8 data)
84 struct aspeed_kcs_bmc *priv = kcs_bmc_priv(kcs_bmc);
85 int rc;
87 rc = regmap_write(priv->map, reg, data);
88 WARN(rc != 0, "regmap_write() failed: %d\n", rc);
93 * AST_usrGuide_KCS.pdf
94 * 2. Background:
95 * we note D for Data, and C for Cmd/Status, default rules are
96 * A. KCS1 / KCS2 ( D / C:X / X+4 )
97 * D / C : CA0h / CA4h
98 * D / C : CA8h / CACh
99 * B. KCS3 ( D / C:XX2h / XX3h )
100 * D / C : CA2h / CA3h
101 * D / C : CB2h / CB3h
102 * C. KCS4
103 * D / C : CA4h / CA5h
105 static void aspeed_kcs_set_address(struct kcs_bmc *kcs_bmc, u16 addr)
107 struct aspeed_kcs_bmc *priv = kcs_bmc_priv(kcs_bmc);
109 switch (kcs_bmc->channel) {
110 case 1:
111 regmap_update_bits(priv->map, LPC_HICR4,
112 LPC_HICR4_LADR12AS, 0);
113 regmap_write(priv->map, LPC_LADR12H, addr >> 8);
114 regmap_write(priv->map, LPC_LADR12L, addr & 0xFF);
115 break;
117 case 2:
118 regmap_update_bits(priv->map, LPC_HICR4,
119 LPC_HICR4_LADR12AS, LPC_HICR4_LADR12AS);
120 regmap_write(priv->map, LPC_LADR12H, addr >> 8);
121 regmap_write(priv->map, LPC_LADR12L, addr & 0xFF);
122 break;
124 case 3:
125 regmap_write(priv->map, LPC_LADR3H, addr >> 8);
126 regmap_write(priv->map, LPC_LADR3L, addr & 0xFF);
127 break;
129 case 4:
130 regmap_write(priv->map, LPC_LADR4, ((addr + 1) << 16) |
131 addr);
132 break;
134 default:
135 break;
139 static void aspeed_kcs_enable_channel(struct kcs_bmc *kcs_bmc, bool enable)
141 struct aspeed_kcs_bmc *priv = kcs_bmc_priv(kcs_bmc);
143 switch (kcs_bmc->channel) {
144 case 1:
145 if (enable) {
146 regmap_update_bits(priv->map, LPC_HICR2,
147 LPC_HICR2_IBFIF1, LPC_HICR2_IBFIF1);
148 regmap_update_bits(priv->map, LPC_HICR0,
149 LPC_HICR0_LPC1E, LPC_HICR0_LPC1E);
150 } else {
151 regmap_update_bits(priv->map, LPC_HICR0,
152 LPC_HICR0_LPC1E, 0);
153 regmap_update_bits(priv->map, LPC_HICR2,
154 LPC_HICR2_IBFIF1, 0);
156 break;
158 case 2:
159 if (enable) {
160 regmap_update_bits(priv->map, LPC_HICR2,
161 LPC_HICR2_IBFIF2, LPC_HICR2_IBFIF2);
162 regmap_update_bits(priv->map, LPC_HICR0,
163 LPC_HICR0_LPC2E, LPC_HICR0_LPC2E);
164 } else {
165 regmap_update_bits(priv->map, LPC_HICR0,
166 LPC_HICR0_LPC2E, 0);
167 regmap_update_bits(priv->map, LPC_HICR2,
168 LPC_HICR2_IBFIF2, 0);
170 break;
172 case 3:
173 if (enable) {
174 regmap_update_bits(priv->map, LPC_HICR2,
175 LPC_HICR2_IBFIF3, LPC_HICR2_IBFIF3);
176 regmap_update_bits(priv->map, LPC_HICR0,
177 LPC_HICR0_LPC3E, LPC_HICR0_LPC3E);
178 regmap_update_bits(priv->map, LPC_HICR4,
179 LPC_HICR4_KCSENBL, LPC_HICR4_KCSENBL);
180 } else {
181 regmap_update_bits(priv->map, LPC_HICR0,
182 LPC_HICR0_LPC3E, 0);
183 regmap_update_bits(priv->map, LPC_HICR4,
184 LPC_HICR4_KCSENBL, 0);
185 regmap_update_bits(priv->map, LPC_HICR2,
186 LPC_HICR2_IBFIF3, 0);
188 break;
190 case 4:
191 if (enable)
192 regmap_update_bits(priv->map, LPC_HICRB,
193 LPC_HICRB_IBFIF4 | LPC_HICRB_LPC4E,
194 LPC_HICRB_IBFIF4 | LPC_HICRB_LPC4E);
195 else
196 regmap_update_bits(priv->map, LPC_HICRB,
197 LPC_HICRB_IBFIF4 | LPC_HICRB_LPC4E,
199 break;
201 default:
202 break;
206 static irqreturn_t aspeed_kcs_irq(int irq, void *arg)
208 struct kcs_bmc *kcs_bmc = arg;
210 if (!kcs_bmc_handle_event(kcs_bmc))
211 return IRQ_HANDLED;
213 return IRQ_NONE;
216 static int aspeed_kcs_config_irq(struct kcs_bmc *kcs_bmc,
217 struct platform_device *pdev)
219 struct device *dev = &pdev->dev;
220 int irq;
222 irq = platform_get_irq(pdev, 0);
223 if (irq < 0)
224 return irq;
226 return devm_request_irq(dev, irq, aspeed_kcs_irq, IRQF_SHARED,
227 dev_name(dev), kcs_bmc);
230 static const struct kcs_ioreg ast_kcs_bmc_ioregs[KCS_CHANNEL_MAX] = {
231 { .idr = LPC_IDR1, .odr = LPC_ODR1, .str = LPC_STR1 },
232 { .idr = LPC_IDR2, .odr = LPC_ODR2, .str = LPC_STR2 },
233 { .idr = LPC_IDR3, .odr = LPC_ODR3, .str = LPC_STR3 },
234 { .idr = LPC_IDR4, .odr = LPC_ODR4, .str = LPC_STR4 },
237 static struct kcs_bmc *aspeed_kcs_probe_of_v1(struct platform_device *pdev)
239 struct aspeed_kcs_bmc *priv;
240 struct device_node *np;
241 struct kcs_bmc *kcs;
242 u32 channel;
243 u32 slave;
244 int rc;
246 np = pdev->dev.of_node;
248 rc = of_property_read_u32(np, "kcs_chan", &channel);
249 if ((rc != 0) || (channel == 0 || channel > KCS_CHANNEL_MAX)) {
250 dev_err(&pdev->dev, "no valid 'kcs_chan' configured\n");
251 return ERR_PTR(-EINVAL);
254 kcs = kcs_bmc_alloc(&pdev->dev, sizeof(struct aspeed_kcs_bmc), channel);
255 if (!kcs)
256 return ERR_PTR(-ENOMEM);
258 priv = kcs_bmc_priv(kcs);
259 priv->map = syscon_node_to_regmap(pdev->dev.parent->of_node);
260 if (IS_ERR(priv->map)) {
261 dev_err(&pdev->dev, "Couldn't get regmap\n");
262 return ERR_PTR(-ENODEV);
265 rc = of_property_read_u32(np, "kcs_addr", &slave);
266 if (rc) {
267 dev_err(&pdev->dev, "no valid 'kcs_addr' configured\n");
268 return ERR_PTR(-EINVAL);
271 kcs->ioreg = ast_kcs_bmc_ioregs[channel - 1];
272 aspeed_kcs_set_address(kcs, slave);
274 return kcs;
277 static int aspeed_kcs_calculate_channel(const struct kcs_ioreg *regs)
279 int i;
281 for (i = 0; i < ARRAY_SIZE(ast_kcs_bmc_ioregs); i++) {
282 if (!memcmp(&ast_kcs_bmc_ioregs[i], regs, sizeof(*regs)))
283 return i + 1;
286 return -EINVAL;
289 static struct kcs_bmc *aspeed_kcs_probe_of_v2(struct platform_device *pdev)
291 struct aspeed_kcs_bmc *priv;
292 struct device_node *np;
293 struct kcs_ioreg ioreg;
294 struct kcs_bmc *kcs;
295 const __be32 *reg;
296 int channel;
297 u32 slave;
298 int rc;
300 np = pdev->dev.of_node;
302 /* Don't translate addresses, we want offsets for the regmaps */
303 reg = of_get_address(np, 0, NULL, NULL);
304 if (!reg)
305 return ERR_PTR(-EINVAL);
306 ioreg.idr = be32_to_cpup(reg);
308 reg = of_get_address(np, 1, NULL, NULL);
309 if (!reg)
310 return ERR_PTR(-EINVAL);
311 ioreg.odr = be32_to_cpup(reg);
313 reg = of_get_address(np, 2, NULL, NULL);
314 if (!reg)
315 return ERR_PTR(-EINVAL);
316 ioreg.str = be32_to_cpup(reg);
318 channel = aspeed_kcs_calculate_channel(&ioreg);
319 if (channel < 0)
320 return ERR_PTR(channel);
322 kcs = kcs_bmc_alloc(&pdev->dev, sizeof(struct aspeed_kcs_bmc), channel);
323 if (!kcs)
324 return ERR_PTR(-ENOMEM);
326 kcs->ioreg = ioreg;
328 priv = kcs_bmc_priv(kcs);
329 priv->map = syscon_node_to_regmap(pdev->dev.parent->of_node);
330 if (IS_ERR(priv->map)) {
331 dev_err(&pdev->dev, "Couldn't get regmap\n");
332 return ERR_PTR(-ENODEV);
335 rc = of_property_read_u32(np, "aspeed,lpc-io-reg", &slave);
336 if (rc)
337 return ERR_PTR(rc);
339 aspeed_kcs_set_address(kcs, slave);
341 return kcs;
344 static int aspeed_kcs_probe(struct platform_device *pdev)
346 struct device *dev = &pdev->dev;
347 struct kcs_bmc *kcs_bmc;
348 struct device_node *np;
349 int rc;
351 np = pdev->dev.of_node;
352 if (of_device_is_compatible(np, "aspeed,ast2400-kcs-bmc") ||
353 of_device_is_compatible(np, "aspeed,ast2500-kcs-bmc"))
354 kcs_bmc = aspeed_kcs_probe_of_v1(pdev);
355 else if (of_device_is_compatible(np, "aspeed,ast2400-kcs-bmc-v2") ||
356 of_device_is_compatible(np, "aspeed,ast2500-kcs-bmc-v2"))
357 kcs_bmc = aspeed_kcs_probe_of_v2(pdev);
358 else
359 return -EINVAL;
361 if (IS_ERR(kcs_bmc))
362 return PTR_ERR(kcs_bmc);
364 kcs_bmc->io_inputb = aspeed_kcs_inb;
365 kcs_bmc->io_outputb = aspeed_kcs_outb;
367 rc = aspeed_kcs_config_irq(kcs_bmc, pdev);
368 if (rc)
369 return rc;
371 dev_set_drvdata(dev, kcs_bmc);
373 aspeed_kcs_enable_channel(kcs_bmc, true);
375 rc = misc_register(&kcs_bmc->miscdev);
376 if (rc) {
377 dev_err(dev, "Unable to register device\n");
378 return rc;
381 dev_dbg(&pdev->dev,
382 "Probed KCS device %d (IDR=0x%x, ODR=0x%x, STR=0x%x)\n",
383 kcs_bmc->channel, kcs_bmc->ioreg.idr, kcs_bmc->ioreg.odr,
384 kcs_bmc->ioreg.str);
386 return 0;
389 static int aspeed_kcs_remove(struct platform_device *pdev)
391 struct kcs_bmc *kcs_bmc = dev_get_drvdata(&pdev->dev);
393 misc_deregister(&kcs_bmc->miscdev);
395 return 0;
398 static const struct of_device_id ast_kcs_bmc_match[] = {
399 { .compatible = "aspeed,ast2400-kcs-bmc" },
400 { .compatible = "aspeed,ast2500-kcs-bmc" },
401 { .compatible = "aspeed,ast2400-kcs-bmc-v2" },
402 { .compatible = "aspeed,ast2500-kcs-bmc-v2" },
405 MODULE_DEVICE_TABLE(of, ast_kcs_bmc_match);
407 static struct platform_driver ast_kcs_bmc_driver = {
408 .driver = {
409 .name = DEVICE_NAME,
410 .of_match_table = ast_kcs_bmc_match,
412 .probe = aspeed_kcs_probe,
413 .remove = aspeed_kcs_remove,
415 module_platform_driver(ast_kcs_bmc_driver);
417 MODULE_LICENSE("GPL v2");
418 MODULE_AUTHOR("Haiyue Wang <haiyue.wang@linux.intel.com>");
419 MODULE_DESCRIPTION("Aspeed device interface to the KCS BMC device");