1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2015-2018, Intel Corporation.
4  */
5 
6 #define pr_fmt(fmt) "aspeed-kcs-bmc: " fmt
7 
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>
22 
23 #include "kcs_bmc.h"
24 
25 
26 #define DEVICE_NAME     "ast-kcs-bmc"
27 
28 #define KCS_CHANNEL_MAX     4
29 
30 #define LPC_HICR0            0x000
31 #define     LPC_HICR0_LPC3E          BIT(7)
32 #define     LPC_HICR0_LPC2E          BIT(6)
33 #define     LPC_HICR0_LPC1E          BIT(5)
34 #define LPC_HICR2            0x008
35 #define     LPC_HICR2_IBFIF3         BIT(3)
36 #define     LPC_HICR2_IBFIF2         BIT(2)
37 #define     LPC_HICR2_IBFIF1         BIT(1)
38 #define LPC_HICR4            0x010
39 #define     LPC_HICR4_LADR12AS       BIT(7)
40 #define     LPC_HICR4_KCSENBL        BIT(2)
41 #define LPC_LADR3H           0x014
42 #define LPC_LADR3L           0x018
43 #define LPC_LADR12H          0x01C
44 #define LPC_LADR12L          0x020
45 #define LPC_IDR1             0x024
46 #define LPC_IDR2             0x028
47 #define LPC_IDR3             0x02C
48 #define LPC_ODR1             0x030
49 #define LPC_ODR2             0x034
50 #define LPC_ODR3             0x038
51 #define LPC_STR1             0x03C
52 #define LPC_STR2             0x040
53 #define LPC_STR3             0x044
54 #define LPC_HICRB            0x100
55 #define     LPC_HICRB_IBFIF4         BIT(1)
56 #define     LPC_HICRB_LPC4E          BIT(0)
57 #define LPC_LADR4            0x110
58 #define LPC_IDR4             0x114
59 #define LPC_ODR4             0x118
60 #define LPC_STR4             0x11C
61 
62 struct aspeed_kcs_bmc {
63 	struct regmap *map;
64 };
65 
66 
67 static u8 aspeed_kcs_inb(struct kcs_bmc *kcs_bmc, u32 reg)
68 {
69 	struct aspeed_kcs_bmc *priv = kcs_bmc_priv(kcs_bmc);
70 	u32 val = 0;
71 	int rc;
72 
73 	rc = regmap_read(priv->map, reg, &val);
74 	WARN(rc != 0, "regmap_read() failed: %d\n", rc);
75 
76 	return rc == 0 ? (u8) val : 0;
77 }
78 
79 static void aspeed_kcs_outb(struct kcs_bmc *kcs_bmc, u32 reg, u8 data)
80 {
81 	struct aspeed_kcs_bmc *priv = kcs_bmc_priv(kcs_bmc);
82 	int rc;
83 
84 	rc = regmap_write(priv->map, reg, data);
85 	WARN(rc != 0, "regmap_write() failed: %d\n", rc);
86 }
87 
88 
89 /*
90  * AST_usrGuide_KCS.pdf
91  * 2. Background:
92  *   we note D for Data, and C for Cmd/Status, default rules are
93  *     A. KCS1 / KCS2 ( D / C:X / X+4 )
94  *        D / C : CA0h / CA4h
95  *        D / C : CA8h / CACh
96  *     B. KCS3 ( D / C:XX2h / XX3h )
97  *        D / C : CA2h / CA3h
98  *        D / C : CB2h / CB3h
99  *     C. KCS4
100  *        D / C : CA4h / CA5h
101  */
102 static void aspeed_kcs_set_address(struct kcs_bmc *kcs_bmc, u16 addr)
103 {
104 	struct aspeed_kcs_bmc *priv = kcs_bmc_priv(kcs_bmc);
105 
106 	switch (kcs_bmc->channel) {
107 	case 1:
108 		regmap_update_bits(priv->map, LPC_HICR4,
109 				LPC_HICR4_LADR12AS, 0);
110 		regmap_write(priv->map, LPC_LADR12H, addr >> 8);
111 		regmap_write(priv->map, LPC_LADR12L, addr & 0xFF);
112 		break;
113 
114 	case 2:
115 		regmap_update_bits(priv->map, LPC_HICR4,
116 				LPC_HICR4_LADR12AS, LPC_HICR4_LADR12AS);
117 		regmap_write(priv->map, LPC_LADR12H, addr >> 8);
118 		regmap_write(priv->map, LPC_LADR12L, addr & 0xFF);
119 		break;
120 
121 	case 3:
122 		regmap_write(priv->map, LPC_LADR3H, addr >> 8);
123 		regmap_write(priv->map, LPC_LADR3L, addr & 0xFF);
124 		break;
125 
126 	case 4:
127 		regmap_write(priv->map, LPC_LADR4, ((addr + 1) << 16) |
128 			addr);
129 		break;
130 
131 	default:
132 		break;
133 	}
134 }
135 
136 static void aspeed_kcs_enable_channel(struct kcs_bmc *kcs_bmc, bool enable)
137 {
138 	struct aspeed_kcs_bmc *priv = kcs_bmc_priv(kcs_bmc);
139 
140 	switch (kcs_bmc->channel) {
141 	case 1:
142 		if (enable) {
143 			regmap_update_bits(priv->map, LPC_HICR2,
144 					LPC_HICR2_IBFIF1, LPC_HICR2_IBFIF1);
145 			regmap_update_bits(priv->map, LPC_HICR0,
146 					LPC_HICR0_LPC1E, LPC_HICR0_LPC1E);
147 		} else {
148 			regmap_update_bits(priv->map, LPC_HICR0,
149 					LPC_HICR0_LPC1E, 0);
150 			regmap_update_bits(priv->map, LPC_HICR2,
151 					LPC_HICR2_IBFIF1, 0);
152 		}
153 		break;
154 
155 	case 2:
156 		if (enable) {
157 			regmap_update_bits(priv->map, LPC_HICR2,
158 					LPC_HICR2_IBFIF2, LPC_HICR2_IBFIF2);
159 			regmap_update_bits(priv->map, LPC_HICR0,
160 					LPC_HICR0_LPC2E, LPC_HICR0_LPC2E);
161 		} else {
162 			regmap_update_bits(priv->map, LPC_HICR0,
163 					LPC_HICR0_LPC2E, 0);
164 			regmap_update_bits(priv->map, LPC_HICR2,
165 					LPC_HICR2_IBFIF2, 0);
166 		}
167 		break;
168 
169 	case 3:
170 		if (enable) {
171 			regmap_update_bits(priv->map, LPC_HICR2,
172 					LPC_HICR2_IBFIF3, LPC_HICR2_IBFIF3);
173 			regmap_update_bits(priv->map, LPC_HICR0,
174 					LPC_HICR0_LPC3E, LPC_HICR0_LPC3E);
175 			regmap_update_bits(priv->map, LPC_HICR4,
176 					LPC_HICR4_KCSENBL, LPC_HICR4_KCSENBL);
177 		} else {
178 			regmap_update_bits(priv->map, LPC_HICR0,
179 					LPC_HICR0_LPC3E, 0);
180 			regmap_update_bits(priv->map, LPC_HICR4,
181 					LPC_HICR4_KCSENBL, 0);
182 			regmap_update_bits(priv->map, LPC_HICR2,
183 					LPC_HICR2_IBFIF3, 0);
184 		}
185 		break;
186 
187 	case 4:
188 		if (enable)
189 			regmap_update_bits(priv->map, LPC_HICRB,
190 					LPC_HICRB_IBFIF4 | LPC_HICRB_LPC4E,
191 					LPC_HICRB_IBFIF4 | LPC_HICRB_LPC4E);
192 		else
193 			regmap_update_bits(priv->map, LPC_HICRB,
194 					LPC_HICRB_IBFIF4 | LPC_HICRB_LPC4E,
195 					0);
196 		break;
197 
198 	default:
199 		break;
200 	}
201 }
202 
203 static irqreturn_t aspeed_kcs_irq(int irq, void *arg)
204 {
205 	struct kcs_bmc *kcs_bmc = arg;
206 
207 	if (!kcs_bmc_handle_event(kcs_bmc))
208 		return IRQ_HANDLED;
209 
210 	return IRQ_NONE;
211 }
212 
213 static int aspeed_kcs_config_irq(struct kcs_bmc *kcs_bmc,
214 			struct platform_device *pdev)
215 {
216 	struct device *dev = &pdev->dev;
217 	int irq;
218 
219 	irq = platform_get_irq(pdev, 0);
220 	if (irq < 0)
221 		return irq;
222 
223 	return devm_request_irq(dev, irq, aspeed_kcs_irq, IRQF_SHARED,
224 				dev_name(dev), kcs_bmc);
225 }
226 
227 static const struct kcs_ioreg ast_kcs_bmc_ioregs[KCS_CHANNEL_MAX] = {
228 	{ .idr = LPC_IDR1, .odr = LPC_ODR1, .str = LPC_STR1 },
229 	{ .idr = LPC_IDR2, .odr = LPC_ODR2, .str = LPC_STR2 },
230 	{ .idr = LPC_IDR3, .odr = LPC_ODR3, .str = LPC_STR3 },
231 	{ .idr = LPC_IDR4, .odr = LPC_ODR4, .str = LPC_STR4 },
232 };
233 
234 static struct kcs_bmc *aspeed_kcs_probe_of_v1(struct platform_device *pdev)
235 {
236 	struct aspeed_kcs_bmc *priv;
237 	struct device_node *np;
238 	struct kcs_bmc *kcs;
239 	u32 channel;
240 	u32 slave;
241 	int rc;
242 
243 	np = pdev->dev.of_node;
244 
245 	rc = of_property_read_u32(np, "kcs_chan", &channel);
246 	if ((rc != 0) || (channel == 0 || channel > KCS_CHANNEL_MAX)) {
247 		dev_err(&pdev->dev, "no valid 'kcs_chan' configured\n");
248 		return ERR_PTR(-EINVAL);
249 	}
250 
251 	kcs = kcs_bmc_alloc(&pdev->dev, sizeof(struct aspeed_kcs_bmc), channel);
252 	if (!kcs)
253 		return ERR_PTR(-ENOMEM);
254 
255 	priv = kcs_bmc_priv(kcs);
256 	priv->map = syscon_node_to_regmap(pdev->dev.parent->of_node);
257 	if (IS_ERR(priv->map)) {
258 		dev_err(&pdev->dev, "Couldn't get regmap\n");
259 		return ERR_PTR(-ENODEV);
260 	}
261 
262 	rc = of_property_read_u32(np, "kcs_addr", &slave);
263 	if (rc) {
264 		dev_err(&pdev->dev, "no valid 'kcs_addr' configured\n");
265 		return ERR_PTR(-EINVAL);
266 	}
267 
268 	kcs->ioreg = ast_kcs_bmc_ioregs[channel - 1];
269 	aspeed_kcs_set_address(kcs, slave);
270 
271 	return kcs;
272 }
273 
274 static int aspeed_kcs_calculate_channel(const struct kcs_ioreg *regs)
275 {
276 	int i;
277 
278 	for (i = 0; i < ARRAY_SIZE(ast_kcs_bmc_ioregs); i++) {
279 		if (!memcmp(&ast_kcs_bmc_ioregs[i], regs, sizeof(*regs)))
280 			return i + 1;
281 	}
282 
283 	return -EINVAL;
284 }
285 
286 static struct kcs_bmc *aspeed_kcs_probe_of_v2(struct platform_device *pdev)
287 {
288 	struct aspeed_kcs_bmc *priv;
289 	struct device_node *np;
290 	struct kcs_ioreg ioreg;
291 	struct kcs_bmc *kcs;
292 	const __be32 *reg;
293 	int channel;
294 	u32 slave;
295 	int rc;
296 
297 	np = pdev->dev.of_node;
298 
299 	/* Don't translate addresses, we want offsets for the regmaps */
300 	reg = of_get_address(np, 0, NULL, NULL);
301 	if (!reg)
302 		return ERR_PTR(-EINVAL);
303 	ioreg.idr = be32_to_cpup(reg);
304 
305 	reg = of_get_address(np, 1, NULL, NULL);
306 	if (!reg)
307 		return ERR_PTR(-EINVAL);
308 	ioreg.odr = be32_to_cpup(reg);
309 
310 	reg = of_get_address(np, 2, NULL, NULL);
311 	if (!reg)
312 		return ERR_PTR(-EINVAL);
313 	ioreg.str = be32_to_cpup(reg);
314 
315 	channel = aspeed_kcs_calculate_channel(&ioreg);
316 	if (channel < 0)
317 		return ERR_PTR(channel);
318 
319 	kcs = kcs_bmc_alloc(&pdev->dev, sizeof(struct aspeed_kcs_bmc), channel);
320 	if (!kcs)
321 		return ERR_PTR(-ENOMEM);
322 
323 	kcs->ioreg = ioreg;
324 
325 	priv = kcs_bmc_priv(kcs);
326 	priv->map = syscon_node_to_regmap(pdev->dev.parent->of_node);
327 	if (IS_ERR(priv->map)) {
328 		dev_err(&pdev->dev, "Couldn't get regmap\n");
329 		return ERR_PTR(-ENODEV);
330 	}
331 
332 	rc = of_property_read_u32(np, "aspeed,lpc-io-reg", &slave);
333 	if (rc)
334 		return ERR_PTR(rc);
335 
336 	aspeed_kcs_set_address(kcs, slave);
337 
338 	return kcs;
339 }
340 
341 static int aspeed_kcs_probe(struct platform_device *pdev)
342 {
343 	struct device *dev = &pdev->dev;
344 	struct kcs_bmc *kcs_bmc;
345 	struct device_node *np;
346 	int rc;
347 
348 	np = dev->of_node->parent;
349 	if (!of_device_is_compatible(np, "aspeed,ast2400-lpc-v2") &&
350 	    !of_device_is_compatible(np, "aspeed,ast2500-lpc-v2") &&
351 	    !of_device_is_compatible(np, "aspeed,ast2600-lpc-v2")) {
352 		dev_err(dev, "unsupported LPC device binding\n");
353 		return -ENODEV;
354 	}
355 
356 	np = dev->of_node;
357 	if (of_device_is_compatible(np, "aspeed,ast2400-kcs-bmc") ||
358 	    of_device_is_compatible(np, "aspeed,ast2500-kcs-bmc"))
359 		kcs_bmc = aspeed_kcs_probe_of_v1(pdev);
360 	else if (of_device_is_compatible(np, "aspeed,ast2400-kcs-bmc-v2") ||
361 		 of_device_is_compatible(np, "aspeed,ast2500-kcs-bmc-v2"))
362 		kcs_bmc = aspeed_kcs_probe_of_v2(pdev);
363 	else
364 		return -EINVAL;
365 
366 	if (IS_ERR(kcs_bmc))
367 		return PTR_ERR(kcs_bmc);
368 
369 	kcs_bmc->io_inputb = aspeed_kcs_inb;
370 	kcs_bmc->io_outputb = aspeed_kcs_outb;
371 
372 	rc = aspeed_kcs_config_irq(kcs_bmc, pdev);
373 	if (rc)
374 		return rc;
375 
376 	dev_set_drvdata(dev, kcs_bmc);
377 
378 	aspeed_kcs_enable_channel(kcs_bmc, true);
379 
380 	rc = misc_register(&kcs_bmc->miscdev);
381 	if (rc) {
382 		dev_err(dev, "Unable to register device\n");
383 		return rc;
384 	}
385 
386 	dev_dbg(&pdev->dev,
387 		"Probed KCS device %d (IDR=0x%x, ODR=0x%x, STR=0x%x)\n",
388 		kcs_bmc->channel, kcs_bmc->ioreg.idr, kcs_bmc->ioreg.odr,
389 		kcs_bmc->ioreg.str);
390 
391 	return 0;
392 }
393 
394 static int aspeed_kcs_remove(struct platform_device *pdev)
395 {
396 	struct kcs_bmc *kcs_bmc = dev_get_drvdata(&pdev->dev);
397 
398 	misc_deregister(&kcs_bmc->miscdev);
399 
400 	return 0;
401 }
402 
403 static const struct of_device_id ast_kcs_bmc_match[] = {
404 	{ .compatible = "aspeed,ast2400-kcs-bmc" },
405 	{ .compatible = "aspeed,ast2500-kcs-bmc" },
406 	{ .compatible = "aspeed,ast2400-kcs-bmc-v2" },
407 	{ .compatible = "aspeed,ast2500-kcs-bmc-v2" },
408 	{ }
409 };
410 MODULE_DEVICE_TABLE(of, ast_kcs_bmc_match);
411 
412 static struct platform_driver ast_kcs_bmc_driver = {
413 	.driver = {
414 		.name           = DEVICE_NAME,
415 		.of_match_table = ast_kcs_bmc_match,
416 	},
417 	.probe  = aspeed_kcs_probe,
418 	.remove = aspeed_kcs_remove,
419 };
420 module_platform_driver(ast_kcs_bmc_driver);
421 
422 MODULE_LICENSE("GPL v2");
423 MODULE_AUTHOR("Haiyue Wang <haiyue.wang@linux.intel.com>");
424 MODULE_DESCRIPTION("Aspeed device interface to the KCS BMC device");
425