18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2015-2018, Intel Corporation.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "aspeed-kcs-bmc: " fmt
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/atomic.h>
98c2ecf20Sopenharmony_ci#include <linux/errno.h>
108c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
118c2ecf20Sopenharmony_ci#include <linux/io.h>
128c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/of.h>
158c2ecf20Sopenharmony_ci#include <linux/of_address.h>
168c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
178c2ecf20Sopenharmony_ci#include <linux/poll.h>
188c2ecf20Sopenharmony_ci#include <linux/regmap.h>
198c2ecf20Sopenharmony_ci#include <linux/sched.h>
208c2ecf20Sopenharmony_ci#include <linux/slab.h>
218c2ecf20Sopenharmony_ci#include <linux/timer.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include "kcs_bmc.h"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define DEVICE_NAME     "ast-kcs-bmc"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define KCS_CHANNEL_MAX     4
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* mapped to lpc-bmc@0 IO space */
318c2ecf20Sopenharmony_ci#define LPC_HICR0            0x000
328c2ecf20Sopenharmony_ci#define     LPC_HICR0_LPC3E          BIT(7)
338c2ecf20Sopenharmony_ci#define     LPC_HICR0_LPC2E          BIT(6)
348c2ecf20Sopenharmony_ci#define     LPC_HICR0_LPC1E          BIT(5)
358c2ecf20Sopenharmony_ci#define LPC_HICR2            0x008
368c2ecf20Sopenharmony_ci#define     LPC_HICR2_IBFIF3         BIT(3)
378c2ecf20Sopenharmony_ci#define     LPC_HICR2_IBFIF2         BIT(2)
388c2ecf20Sopenharmony_ci#define     LPC_HICR2_IBFIF1         BIT(1)
398c2ecf20Sopenharmony_ci#define LPC_HICR4            0x010
408c2ecf20Sopenharmony_ci#define     LPC_HICR4_LADR12AS       BIT(7)
418c2ecf20Sopenharmony_ci#define     LPC_HICR4_KCSENBL        BIT(2)
428c2ecf20Sopenharmony_ci#define LPC_LADR3H           0x014
438c2ecf20Sopenharmony_ci#define LPC_LADR3L           0x018
448c2ecf20Sopenharmony_ci#define LPC_LADR12H          0x01C
458c2ecf20Sopenharmony_ci#define LPC_LADR12L          0x020
468c2ecf20Sopenharmony_ci#define LPC_IDR1             0x024
478c2ecf20Sopenharmony_ci#define LPC_IDR2             0x028
488c2ecf20Sopenharmony_ci#define LPC_IDR3             0x02C
498c2ecf20Sopenharmony_ci#define LPC_ODR1             0x030
508c2ecf20Sopenharmony_ci#define LPC_ODR2             0x034
518c2ecf20Sopenharmony_ci#define LPC_ODR3             0x038
528c2ecf20Sopenharmony_ci#define LPC_STR1             0x03C
538c2ecf20Sopenharmony_ci#define LPC_STR2             0x040
548c2ecf20Sopenharmony_ci#define LPC_STR3             0x044
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* mapped to lpc-host@80 IO space */
578c2ecf20Sopenharmony_ci#define LPC_HICRB            0x080
588c2ecf20Sopenharmony_ci#define     LPC_HICRB_IBFIF4         BIT(1)
598c2ecf20Sopenharmony_ci#define     LPC_HICRB_LPC4E          BIT(0)
608c2ecf20Sopenharmony_ci#define LPC_LADR4            0x090
618c2ecf20Sopenharmony_ci#define LPC_IDR4             0x094
628c2ecf20Sopenharmony_ci#define LPC_ODR4             0x098
638c2ecf20Sopenharmony_ci#define LPC_STR4             0x09C
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistruct aspeed_kcs_bmc {
668c2ecf20Sopenharmony_ci	struct regmap *map;
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic u8 aspeed_kcs_inb(struct kcs_bmc *kcs_bmc, u32 reg)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct aspeed_kcs_bmc *priv = kcs_bmc_priv(kcs_bmc);
738c2ecf20Sopenharmony_ci	u32 val = 0;
748c2ecf20Sopenharmony_ci	int rc;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	rc = regmap_read(priv->map, reg, &val);
778c2ecf20Sopenharmony_ci	WARN(rc != 0, "regmap_read() failed: %d\n", rc);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	return rc == 0 ? (u8) val : 0;
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic void aspeed_kcs_outb(struct kcs_bmc *kcs_bmc, u32 reg, u8 data)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	struct aspeed_kcs_bmc *priv = kcs_bmc_priv(kcs_bmc);
858c2ecf20Sopenharmony_ci	int rc;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	rc = regmap_write(priv->map, reg, data);
888c2ecf20Sopenharmony_ci	WARN(rc != 0, "regmap_write() failed: %d\n", rc);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/*
938c2ecf20Sopenharmony_ci * AST_usrGuide_KCS.pdf
948c2ecf20Sopenharmony_ci * 2. Background:
958c2ecf20Sopenharmony_ci *   we note D for Data, and C for Cmd/Status, default rules are
968c2ecf20Sopenharmony_ci *     A. KCS1 / KCS2 ( D / C:X / X+4 )
978c2ecf20Sopenharmony_ci *        D / C : CA0h / CA4h
988c2ecf20Sopenharmony_ci *        D / C : CA8h / CACh
998c2ecf20Sopenharmony_ci *     B. KCS3 ( D / C:XX2h / XX3h )
1008c2ecf20Sopenharmony_ci *        D / C : CA2h / CA3h
1018c2ecf20Sopenharmony_ci *        D / C : CB2h / CB3h
1028c2ecf20Sopenharmony_ci *     C. KCS4
1038c2ecf20Sopenharmony_ci *        D / C : CA4h / CA5h
1048c2ecf20Sopenharmony_ci */
1058c2ecf20Sopenharmony_cistatic void aspeed_kcs_set_address(struct kcs_bmc *kcs_bmc, u16 addr)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	struct aspeed_kcs_bmc *priv = kcs_bmc_priv(kcs_bmc);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	switch (kcs_bmc->channel) {
1108c2ecf20Sopenharmony_ci	case 1:
1118c2ecf20Sopenharmony_ci		regmap_update_bits(priv->map, LPC_HICR4,
1128c2ecf20Sopenharmony_ci				LPC_HICR4_LADR12AS, 0);
1138c2ecf20Sopenharmony_ci		regmap_write(priv->map, LPC_LADR12H, addr >> 8);
1148c2ecf20Sopenharmony_ci		regmap_write(priv->map, LPC_LADR12L, addr & 0xFF);
1158c2ecf20Sopenharmony_ci		break;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	case 2:
1188c2ecf20Sopenharmony_ci		regmap_update_bits(priv->map, LPC_HICR4,
1198c2ecf20Sopenharmony_ci				LPC_HICR4_LADR12AS, LPC_HICR4_LADR12AS);
1208c2ecf20Sopenharmony_ci		regmap_write(priv->map, LPC_LADR12H, addr >> 8);
1218c2ecf20Sopenharmony_ci		regmap_write(priv->map, LPC_LADR12L, addr & 0xFF);
1228c2ecf20Sopenharmony_ci		break;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	case 3:
1258c2ecf20Sopenharmony_ci		regmap_write(priv->map, LPC_LADR3H, addr >> 8);
1268c2ecf20Sopenharmony_ci		regmap_write(priv->map, LPC_LADR3L, addr & 0xFF);
1278c2ecf20Sopenharmony_ci		break;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	case 4:
1308c2ecf20Sopenharmony_ci		regmap_write(priv->map, LPC_LADR4, ((addr + 1) << 16) |
1318c2ecf20Sopenharmony_ci			addr);
1328c2ecf20Sopenharmony_ci		break;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	default:
1358c2ecf20Sopenharmony_ci		break;
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic void aspeed_kcs_enable_channel(struct kcs_bmc *kcs_bmc, bool enable)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	struct aspeed_kcs_bmc *priv = kcs_bmc_priv(kcs_bmc);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	switch (kcs_bmc->channel) {
1448c2ecf20Sopenharmony_ci	case 1:
1458c2ecf20Sopenharmony_ci		if (enable) {
1468c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICR2,
1478c2ecf20Sopenharmony_ci					LPC_HICR2_IBFIF1, LPC_HICR2_IBFIF1);
1488c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICR0,
1498c2ecf20Sopenharmony_ci					LPC_HICR0_LPC1E, LPC_HICR0_LPC1E);
1508c2ecf20Sopenharmony_ci		} else {
1518c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICR0,
1528c2ecf20Sopenharmony_ci					LPC_HICR0_LPC1E, 0);
1538c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICR2,
1548c2ecf20Sopenharmony_ci					LPC_HICR2_IBFIF1, 0);
1558c2ecf20Sopenharmony_ci		}
1568c2ecf20Sopenharmony_ci		break;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	case 2:
1598c2ecf20Sopenharmony_ci		if (enable) {
1608c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICR2,
1618c2ecf20Sopenharmony_ci					LPC_HICR2_IBFIF2, LPC_HICR2_IBFIF2);
1628c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICR0,
1638c2ecf20Sopenharmony_ci					LPC_HICR0_LPC2E, LPC_HICR0_LPC2E);
1648c2ecf20Sopenharmony_ci		} else {
1658c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICR0,
1668c2ecf20Sopenharmony_ci					LPC_HICR0_LPC2E, 0);
1678c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICR2,
1688c2ecf20Sopenharmony_ci					LPC_HICR2_IBFIF2, 0);
1698c2ecf20Sopenharmony_ci		}
1708c2ecf20Sopenharmony_ci		break;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	case 3:
1738c2ecf20Sopenharmony_ci		if (enable) {
1748c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICR2,
1758c2ecf20Sopenharmony_ci					LPC_HICR2_IBFIF3, LPC_HICR2_IBFIF3);
1768c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICR0,
1778c2ecf20Sopenharmony_ci					LPC_HICR0_LPC3E, LPC_HICR0_LPC3E);
1788c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICR4,
1798c2ecf20Sopenharmony_ci					LPC_HICR4_KCSENBL, LPC_HICR4_KCSENBL);
1808c2ecf20Sopenharmony_ci		} else {
1818c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICR0,
1828c2ecf20Sopenharmony_ci					LPC_HICR0_LPC3E, 0);
1838c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICR4,
1848c2ecf20Sopenharmony_ci					LPC_HICR4_KCSENBL, 0);
1858c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICR2,
1868c2ecf20Sopenharmony_ci					LPC_HICR2_IBFIF3, 0);
1878c2ecf20Sopenharmony_ci		}
1888c2ecf20Sopenharmony_ci		break;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	case 4:
1918c2ecf20Sopenharmony_ci		if (enable)
1928c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICRB,
1938c2ecf20Sopenharmony_ci					LPC_HICRB_IBFIF4 | LPC_HICRB_LPC4E,
1948c2ecf20Sopenharmony_ci					LPC_HICRB_IBFIF4 | LPC_HICRB_LPC4E);
1958c2ecf20Sopenharmony_ci		else
1968c2ecf20Sopenharmony_ci			regmap_update_bits(priv->map, LPC_HICRB,
1978c2ecf20Sopenharmony_ci					LPC_HICRB_IBFIF4 | LPC_HICRB_LPC4E,
1988c2ecf20Sopenharmony_ci					0);
1998c2ecf20Sopenharmony_ci		break;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	default:
2028c2ecf20Sopenharmony_ci		break;
2038c2ecf20Sopenharmony_ci	}
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic irqreturn_t aspeed_kcs_irq(int irq, void *arg)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	struct kcs_bmc *kcs_bmc = arg;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	if (!kcs_bmc_handle_event(kcs_bmc))
2118c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	return IRQ_NONE;
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cistatic int aspeed_kcs_config_irq(struct kcs_bmc *kcs_bmc,
2178c2ecf20Sopenharmony_ci			struct platform_device *pdev)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
2208c2ecf20Sopenharmony_ci	int irq;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
2238c2ecf20Sopenharmony_ci	if (irq < 0)
2248c2ecf20Sopenharmony_ci		return irq;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	return devm_request_irq(dev, irq, aspeed_kcs_irq, IRQF_SHARED,
2278c2ecf20Sopenharmony_ci				dev_name(dev), kcs_bmc);
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic const struct kcs_ioreg ast_kcs_bmc_ioregs[KCS_CHANNEL_MAX] = {
2318c2ecf20Sopenharmony_ci	{ .idr = LPC_IDR1, .odr = LPC_ODR1, .str = LPC_STR1 },
2328c2ecf20Sopenharmony_ci	{ .idr = LPC_IDR2, .odr = LPC_ODR2, .str = LPC_STR2 },
2338c2ecf20Sopenharmony_ci	{ .idr = LPC_IDR3, .odr = LPC_ODR3, .str = LPC_STR3 },
2348c2ecf20Sopenharmony_ci	{ .idr = LPC_IDR4, .odr = LPC_ODR4, .str = LPC_STR4 },
2358c2ecf20Sopenharmony_ci};
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic struct kcs_bmc *aspeed_kcs_probe_of_v1(struct platform_device *pdev)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	struct aspeed_kcs_bmc *priv;
2408c2ecf20Sopenharmony_ci	struct device_node *np;
2418c2ecf20Sopenharmony_ci	struct kcs_bmc *kcs;
2428c2ecf20Sopenharmony_ci	u32 channel;
2438c2ecf20Sopenharmony_ci	u32 slave;
2448c2ecf20Sopenharmony_ci	int rc;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	np = pdev->dev.of_node;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	rc = of_property_read_u32(np, "kcs_chan", &channel);
2498c2ecf20Sopenharmony_ci	if ((rc != 0) || (channel == 0 || channel > KCS_CHANNEL_MAX)) {
2508c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "no valid 'kcs_chan' configured\n");
2518c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
2528c2ecf20Sopenharmony_ci	}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	kcs = kcs_bmc_alloc(&pdev->dev, sizeof(struct aspeed_kcs_bmc), channel);
2558c2ecf20Sopenharmony_ci	if (!kcs)
2568c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	priv = kcs_bmc_priv(kcs);
2598c2ecf20Sopenharmony_ci	priv->map = syscon_node_to_regmap(pdev->dev.parent->of_node);
2608c2ecf20Sopenharmony_ci	if (IS_ERR(priv->map)) {
2618c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Couldn't get regmap\n");
2628c2ecf20Sopenharmony_ci		return ERR_PTR(-ENODEV);
2638c2ecf20Sopenharmony_ci	}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	rc = of_property_read_u32(np, "kcs_addr", &slave);
2668c2ecf20Sopenharmony_ci	if (rc) {
2678c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "no valid 'kcs_addr' configured\n");
2688c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
2698c2ecf20Sopenharmony_ci	}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	kcs->ioreg = ast_kcs_bmc_ioregs[channel - 1];
2728c2ecf20Sopenharmony_ci	aspeed_kcs_set_address(kcs, slave);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	return kcs;
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic int aspeed_kcs_calculate_channel(const struct kcs_ioreg *regs)
2788c2ecf20Sopenharmony_ci{
2798c2ecf20Sopenharmony_ci	int i;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ast_kcs_bmc_ioregs); i++) {
2828c2ecf20Sopenharmony_ci		if (!memcmp(&ast_kcs_bmc_ioregs[i], regs, sizeof(*regs)))
2838c2ecf20Sopenharmony_ci			return i + 1;
2848c2ecf20Sopenharmony_ci	}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	return -EINVAL;
2878c2ecf20Sopenharmony_ci}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_cistatic struct kcs_bmc *aspeed_kcs_probe_of_v2(struct platform_device *pdev)
2908c2ecf20Sopenharmony_ci{
2918c2ecf20Sopenharmony_ci	struct aspeed_kcs_bmc *priv;
2928c2ecf20Sopenharmony_ci	struct device_node *np;
2938c2ecf20Sopenharmony_ci	struct kcs_ioreg ioreg;
2948c2ecf20Sopenharmony_ci	struct kcs_bmc *kcs;
2958c2ecf20Sopenharmony_ci	const __be32 *reg;
2968c2ecf20Sopenharmony_ci	int channel;
2978c2ecf20Sopenharmony_ci	u32 slave;
2988c2ecf20Sopenharmony_ci	int rc;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	np = pdev->dev.of_node;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	/* Don't translate addresses, we want offsets for the regmaps */
3038c2ecf20Sopenharmony_ci	reg = of_get_address(np, 0, NULL, NULL);
3048c2ecf20Sopenharmony_ci	if (!reg)
3058c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
3068c2ecf20Sopenharmony_ci	ioreg.idr = be32_to_cpup(reg);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	reg = of_get_address(np, 1, NULL, NULL);
3098c2ecf20Sopenharmony_ci	if (!reg)
3108c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
3118c2ecf20Sopenharmony_ci	ioreg.odr = be32_to_cpup(reg);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	reg = of_get_address(np, 2, NULL, NULL);
3148c2ecf20Sopenharmony_ci	if (!reg)
3158c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
3168c2ecf20Sopenharmony_ci	ioreg.str = be32_to_cpup(reg);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	channel = aspeed_kcs_calculate_channel(&ioreg);
3198c2ecf20Sopenharmony_ci	if (channel < 0)
3208c2ecf20Sopenharmony_ci		return ERR_PTR(channel);
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	kcs = kcs_bmc_alloc(&pdev->dev, sizeof(struct aspeed_kcs_bmc), channel);
3238c2ecf20Sopenharmony_ci	if (!kcs)
3248c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	kcs->ioreg = ioreg;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	priv = kcs_bmc_priv(kcs);
3298c2ecf20Sopenharmony_ci	priv->map = syscon_node_to_regmap(pdev->dev.parent->of_node);
3308c2ecf20Sopenharmony_ci	if (IS_ERR(priv->map)) {
3318c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Couldn't get regmap\n");
3328c2ecf20Sopenharmony_ci		return ERR_PTR(-ENODEV);
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	rc = of_property_read_u32(np, "aspeed,lpc-io-reg", &slave);
3368c2ecf20Sopenharmony_ci	if (rc)
3378c2ecf20Sopenharmony_ci		return ERR_PTR(rc);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	aspeed_kcs_set_address(kcs, slave);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	return kcs;
3428c2ecf20Sopenharmony_ci}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_cistatic int aspeed_kcs_probe(struct platform_device *pdev)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
3478c2ecf20Sopenharmony_ci	struct kcs_bmc *kcs_bmc;
3488c2ecf20Sopenharmony_ci	struct device_node *np;
3498c2ecf20Sopenharmony_ci	int rc;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	np = pdev->dev.of_node;
3528c2ecf20Sopenharmony_ci	if (of_device_is_compatible(np, "aspeed,ast2400-kcs-bmc") ||
3538c2ecf20Sopenharmony_ci			of_device_is_compatible(np, "aspeed,ast2500-kcs-bmc"))
3548c2ecf20Sopenharmony_ci		kcs_bmc = aspeed_kcs_probe_of_v1(pdev);
3558c2ecf20Sopenharmony_ci	else if (of_device_is_compatible(np, "aspeed,ast2400-kcs-bmc-v2") ||
3568c2ecf20Sopenharmony_ci			of_device_is_compatible(np, "aspeed,ast2500-kcs-bmc-v2"))
3578c2ecf20Sopenharmony_ci		kcs_bmc = aspeed_kcs_probe_of_v2(pdev);
3588c2ecf20Sopenharmony_ci	else
3598c2ecf20Sopenharmony_ci		return -EINVAL;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	if (IS_ERR(kcs_bmc))
3628c2ecf20Sopenharmony_ci		return PTR_ERR(kcs_bmc);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	kcs_bmc->io_inputb = aspeed_kcs_inb;
3658c2ecf20Sopenharmony_ci	kcs_bmc->io_outputb = aspeed_kcs_outb;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	rc = aspeed_kcs_config_irq(kcs_bmc, pdev);
3688c2ecf20Sopenharmony_ci	if (rc)
3698c2ecf20Sopenharmony_ci		return rc;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	dev_set_drvdata(dev, kcs_bmc);
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	aspeed_kcs_enable_channel(kcs_bmc, true);
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	rc = misc_register(&kcs_bmc->miscdev);
3768c2ecf20Sopenharmony_ci	if (rc) {
3778c2ecf20Sopenharmony_ci		dev_err(dev, "Unable to register device\n");
3788c2ecf20Sopenharmony_ci		return rc;
3798c2ecf20Sopenharmony_ci	}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	dev_dbg(&pdev->dev,
3828c2ecf20Sopenharmony_ci		"Probed KCS device %d (IDR=0x%x, ODR=0x%x, STR=0x%x)\n",
3838c2ecf20Sopenharmony_ci		kcs_bmc->channel, kcs_bmc->ioreg.idr, kcs_bmc->ioreg.odr,
3848c2ecf20Sopenharmony_ci		kcs_bmc->ioreg.str);
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	return 0;
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_cistatic int aspeed_kcs_remove(struct platform_device *pdev)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	struct kcs_bmc *kcs_bmc = dev_get_drvdata(&pdev->dev);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	misc_deregister(&kcs_bmc->miscdev);
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	return 0;
3968c2ecf20Sopenharmony_ci}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_cistatic const struct of_device_id ast_kcs_bmc_match[] = {
3998c2ecf20Sopenharmony_ci	{ .compatible = "aspeed,ast2400-kcs-bmc" },
4008c2ecf20Sopenharmony_ci	{ .compatible = "aspeed,ast2500-kcs-bmc" },
4018c2ecf20Sopenharmony_ci	{ .compatible = "aspeed,ast2400-kcs-bmc-v2" },
4028c2ecf20Sopenharmony_ci	{ .compatible = "aspeed,ast2500-kcs-bmc-v2" },
4038c2ecf20Sopenharmony_ci	{ }
4048c2ecf20Sopenharmony_ci};
4058c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ast_kcs_bmc_match);
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistatic struct platform_driver ast_kcs_bmc_driver = {
4088c2ecf20Sopenharmony_ci	.driver = {
4098c2ecf20Sopenharmony_ci		.name           = DEVICE_NAME,
4108c2ecf20Sopenharmony_ci		.of_match_table = ast_kcs_bmc_match,
4118c2ecf20Sopenharmony_ci	},
4128c2ecf20Sopenharmony_ci	.probe  = aspeed_kcs_probe,
4138c2ecf20Sopenharmony_ci	.remove = aspeed_kcs_remove,
4148c2ecf20Sopenharmony_ci};
4158c2ecf20Sopenharmony_cimodule_platform_driver(ast_kcs_bmc_driver);
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
4188c2ecf20Sopenharmony_ciMODULE_AUTHOR("Haiyue Wang <haiyue.wang@linux.intel.com>");
4198c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Aspeed device interface to the KCS BMC device");
420