|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +// Copyright (c) 2015-2018, Intel Corporation. |
| 3 | + |
| 4 | +#define pr_fmt(fmt) "aspeed-kcs-bmc: " fmt |
| 5 | + |
| 6 | +#include <linux/atomic.h> |
| 7 | +#include <linux/errno.h> |
| 8 | +#include <linux/interrupt.h> |
| 9 | +#include <linux/io.h> |
| 10 | +#include <linux/mfd/syscon.h> |
| 11 | +#include <linux/module.h> |
| 12 | +#include <linux/of.h> |
| 13 | +#include <linux/platform_device.h> |
| 14 | +#include <linux/poll.h> |
| 15 | +#include <linux/regmap.h> |
| 16 | +#include <linux/sched.h> |
| 17 | +#include <linux/slab.h> |
| 18 | +#include <linux/timer.h> |
| 19 | + |
| 20 | +#include "kcs_bmc.h" |
| 21 | + |
| 22 | + |
| 23 | +#define DEVICE_NAME "ast-kcs-bmc" |
| 24 | + |
| 25 | +#define KCS_CHANNEL_MAX 4 |
| 26 | + |
| 27 | +/* mapped to lpc-bmc@0 IO space */ |
| 28 | +#define LPC_HICR0 0x000 |
| 29 | +#define LPC_HICR0_LPC3E BIT(7) |
| 30 | +#define LPC_HICR0_LPC2E BIT(6) |
| 31 | +#define LPC_HICR0_LPC1E BIT(5) |
| 32 | +#define LPC_HICR2 0x008 |
| 33 | +#define LPC_HICR2_IBFIF3 BIT(3) |
| 34 | +#define LPC_HICR2_IBFIF2 BIT(2) |
| 35 | +#define LPC_HICR2_IBFIF1 BIT(1) |
| 36 | +#define LPC_HICR4 0x010 |
| 37 | +#define LPC_HICR4_LADR12AS BIT(7) |
| 38 | +#define LPC_HICR4_KCSENBL BIT(2) |
| 39 | +#define LPC_LADR3H 0x014 |
| 40 | +#define LPC_LADR3L 0x018 |
| 41 | +#define LPC_LADR12H 0x01C |
| 42 | +#define LPC_LADR12L 0x020 |
| 43 | +#define LPC_IDR1 0x024 |
| 44 | +#define LPC_IDR2 0x028 |
| 45 | +#define LPC_IDR3 0x02C |
| 46 | +#define LPC_ODR1 0x030 |
| 47 | +#define LPC_ODR2 0x034 |
| 48 | +#define LPC_ODR3 0x038 |
| 49 | +#define LPC_STR1 0x03C |
| 50 | +#define LPC_STR2 0x040 |
| 51 | +#define LPC_STR3 0x044 |
| 52 | + |
| 53 | +/* mapped to lpc-host@80 IO space */ |
| 54 | +#define LPC_HICRB 0x080 |
| 55 | +#define LPC_HICRB_IBFIF4 BIT(1) |
| 56 | +#define LPC_HICRB_LPC4E BIT(0) |
| 57 | +#define LPC_LADR4 0x090 |
| 58 | +#define LPC_IDR4 0x094 |
| 59 | +#define LPC_ODR4 0x098 |
| 60 | +#define LPC_STR4 0x09C |
| 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 int aspeed_kcs_probe(struct platform_device *pdev) |
| 235 | +{ |
| 236 | + struct device *dev = &pdev->dev; |
| 237 | + struct aspeed_kcs_bmc *priv; |
| 238 | + struct kcs_bmc *kcs_bmc; |
| 239 | + u32 chan, addr; |
| 240 | + int rc; |
| 241 | + |
| 242 | + rc = of_property_read_u32(dev->of_node, "kcs_chan", &chan); |
| 243 | + if ((rc != 0) || (chan == 0 || chan > KCS_CHANNEL_MAX)) { |
| 244 | + dev_err(dev, "no valid 'kcs_chan' configured\n"); |
| 245 | + return -ENODEV; |
| 246 | + } |
| 247 | + |
| 248 | + rc = of_property_read_u32(dev->of_node, "kcs_addr", &addr); |
| 249 | + if (rc) { |
| 250 | + dev_err(dev, "no valid 'kcs_addr' configured\n"); |
| 251 | + return -ENODEV; |
| 252 | + } |
| 253 | + |
| 254 | + kcs_bmc = kcs_bmc_alloc(dev, sizeof(*priv), chan); |
| 255 | + if (!kcs_bmc) |
| 256 | + return -ENOMEM; |
| 257 | + |
| 258 | + priv = kcs_bmc_priv(kcs_bmc); |
| 259 | + priv->map = syscon_node_to_regmap(dev->parent->of_node); |
| 260 | + if (IS_ERR(priv->map)) { |
| 261 | + dev_err(dev, "Couldn't get regmap\n"); |
| 262 | + return -ENODEV; |
| 263 | + } |
| 264 | + |
| 265 | + kcs_bmc->ioreg = ast_kcs_bmc_ioregs[chan - 1]; |
| 266 | + kcs_bmc->io_inputb = aspeed_kcs_inb; |
| 267 | + kcs_bmc->io_outputb = aspeed_kcs_outb; |
| 268 | + |
| 269 | + dev_set_drvdata(dev, kcs_bmc); |
| 270 | + |
| 271 | + aspeed_kcs_set_address(kcs_bmc, addr); |
| 272 | + aspeed_kcs_enable_channel(kcs_bmc, true); |
| 273 | + rc = aspeed_kcs_config_irq(kcs_bmc, pdev); |
| 274 | + if (rc) |
| 275 | + return rc; |
| 276 | + |
| 277 | + rc = misc_register(&kcs_bmc->miscdev); |
| 278 | + if (rc) { |
| 279 | + dev_err(dev, "Unable to register device\n"); |
| 280 | + return rc; |
| 281 | + } |
| 282 | + |
| 283 | + pr_info("channel=%u addr=0x%x idr=0x%x odr=0x%x str=0x%x\n", |
| 284 | + chan, addr, |
| 285 | + kcs_bmc->ioreg.idr, kcs_bmc->ioreg.odr, kcs_bmc->ioreg.str); |
| 286 | + |
| 287 | + return 0; |
| 288 | +} |
| 289 | + |
| 290 | +static int aspeed_kcs_remove(struct platform_device *pdev) |
| 291 | +{ |
| 292 | + struct kcs_bmc *kcs_bmc = dev_get_drvdata(&pdev->dev); |
| 293 | + |
| 294 | + misc_deregister(&kcs_bmc->miscdev); |
| 295 | + |
| 296 | + return 0; |
| 297 | +} |
| 298 | + |
| 299 | +static const struct of_device_id ast_kcs_bmc_match[] = { |
| 300 | + { .compatible = "aspeed,ast2400-kcs-bmc" }, |
| 301 | + { .compatible = "aspeed,ast2500-kcs-bmc" }, |
| 302 | + { } |
| 303 | +}; |
| 304 | + |
| 305 | +static struct platform_driver ast_kcs_bmc_driver = { |
| 306 | + .driver = { |
| 307 | + .name = DEVICE_NAME, |
| 308 | + .of_match_table = ast_kcs_bmc_match, |
| 309 | + }, |
| 310 | + .probe = aspeed_kcs_probe, |
| 311 | + .remove = aspeed_kcs_remove, |
| 312 | +}; |
| 313 | + |
| 314 | +module_platform_driver(ast_kcs_bmc_driver); |
| 315 | + |
| 316 | +MODULE_DEVICE_TABLE(of, ast_kcs_bmc_match); |
| 317 | +MODULE_LICENSE("GPL v2"); |
| 318 | +MODULE_AUTHOR("Haiyue Wang <haiyue.wang@linux.intel.com>"); |
| 319 | +MODULE_DESCRIPTION("Aspeed device interface to the KCS BMC device"); |
0 commit comments