1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015-2018, Intel Corporation.
6 #define pr_fmt(fmt) "aspeed-kcs-bmc: " fmt
8 #include <linux/atomic.h>
9 #include <linux/errno.h>
10 #include <linux/interrupt.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/module.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>
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
{
70 static u8
aspeed_kcs_inb(struct kcs_bmc
*kcs_bmc
, u32 reg
)
72 struct aspeed_kcs_bmc
*priv
= kcs_bmc_priv(kcs_bmc
);
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
);
87 rc
= regmap_write(priv
->map
, reg
, data
);
88 WARN(rc
!= 0, "regmap_write() failed: %d\n", rc
);
93 * AST_usrGuide_KCS.pdf
95 * we note D for Data, and C for Cmd/Status, default rules are
96 * A. KCS1 / KCS2 ( D / C:X / X+4 )
99 * B. KCS3 ( D / C:XX2h / XX3h )
100 * D / C : CA2h / CA3h
101 * D / C : CB2h / CB3h
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
) {
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);
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);
125 regmap_write(priv
->map
, LPC_LADR3H
, addr
>> 8);
126 regmap_write(priv
->map
, LPC_LADR3L
, addr
& 0xFF);
130 regmap_write(priv
->map
, LPC_LADR4
, ((addr
+ 1) << 16) |
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
) {
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
);
151 regmap_update_bits(priv
->map
, LPC_HICR0
,
153 regmap_update_bits(priv
->map
, LPC_HICR2
,
154 LPC_HICR2_IBFIF1
, 0);
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
);
165 regmap_update_bits(priv
->map
, LPC_HICR0
,
167 regmap_update_bits(priv
->map
, LPC_HICR2
,
168 LPC_HICR2_IBFIF2
, 0);
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
);
181 regmap_update_bits(priv
->map
, LPC_HICR0
,
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);
192 regmap_update_bits(priv
->map
, LPC_HICRB
,
193 LPC_HICRB_IBFIF4
| LPC_HICRB_LPC4E
,
194 LPC_HICRB_IBFIF4
| LPC_HICRB_LPC4E
);
196 regmap_update_bits(priv
->map
, LPC_HICRB
,
197 LPC_HICRB_IBFIF4
| LPC_HICRB_LPC4E
,
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
))
216 static int aspeed_kcs_config_irq(struct kcs_bmc
*kcs_bmc
,
217 struct platform_device
*pdev
)
219 struct device
*dev
= &pdev
->dev
;
222 irq
= platform_get_irq(pdev
, 0);
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
;
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
);
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
);
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
);
277 static int aspeed_kcs_calculate_channel(const struct kcs_ioreg
*regs
)
281 for (i
= 0; i
< ARRAY_SIZE(ast_kcs_bmc_ioregs
); i
++) {
282 if (!memcmp(&ast_kcs_bmc_ioregs
[i
], regs
, sizeof(*regs
)))
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
;
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
);
305 return ERR_PTR(-EINVAL
);
306 ioreg
.idr
= be32_to_cpup(reg
);
308 reg
= of_get_address(np
, 1, NULL
, NULL
);
310 return ERR_PTR(-EINVAL
);
311 ioreg
.odr
= be32_to_cpup(reg
);
313 reg
= of_get_address(np
, 2, NULL
, NULL
);
315 return ERR_PTR(-EINVAL
);
316 ioreg
.str
= be32_to_cpup(reg
);
318 channel
= aspeed_kcs_calculate_channel(&ioreg
);
320 return ERR_PTR(channel
);
322 kcs
= kcs_bmc_alloc(&pdev
->dev
, sizeof(struct aspeed_kcs_bmc
), channel
);
324 return ERR_PTR(-ENOMEM
);
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
);
339 aspeed_kcs_set_address(kcs
, slave
);
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
;
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
);
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
);
371 dev_set_drvdata(dev
, kcs_bmc
);
373 aspeed_kcs_enable_channel(kcs_bmc
, true);
375 rc
= misc_register(&kcs_bmc
->miscdev
);
377 dev_err(dev
, "Unable to register device\n");
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
,
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
);
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
= {
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");