1*3e16184aSTomer Maimon // SPDX-License-Identifier: GPL-2.0 2*3e16184aSTomer Maimon // Copyright (c) 2019 Nuvoton Technology corporation 3*3e16184aSTomer Maimon 4*3e16184aSTomer Maimon #include <linux/bitfield.h> 5*3e16184aSTomer Maimon #include <linux/clk.h> 6*3e16184aSTomer Maimon #include <linux/interrupt.h> 7*3e16184aSTomer Maimon #include <linux/jiffies.h> 8*3e16184aSTomer Maimon #include <linux/module.h> 9*3e16184aSTomer Maimon #include <linux/of.h> 10*3e16184aSTomer Maimon #include <linux/peci.h> 11*3e16184aSTomer Maimon #include <linux/platform_device.h> 12*3e16184aSTomer Maimon #include <linux/regmap.h> 13*3e16184aSTomer Maimon #include <linux/reset.h> 14*3e16184aSTomer Maimon 15*3e16184aSTomer Maimon /* NPCM GCR module */ 16*3e16184aSTomer Maimon #define NPCM_INTCR3_OFFSET 0x9C 17*3e16184aSTomer Maimon #define NPCM_INTCR3_PECIVSEL BIT(19) 18*3e16184aSTomer Maimon 19*3e16184aSTomer Maimon /* NPCM PECI Registers */ 20*3e16184aSTomer Maimon #define NPCM_PECI_CTL_STS 0x00 21*3e16184aSTomer Maimon #define NPCM_PECI_RD_LENGTH 0x04 22*3e16184aSTomer Maimon #define NPCM_PECI_ADDR 0x08 23*3e16184aSTomer Maimon #define NPCM_PECI_CMD 0x0C 24*3e16184aSTomer Maimon #define NPCM_PECI_CTL2 0x10 25*3e16184aSTomer Maimon #define NPCM_PECI_WR_LENGTH 0x1C 26*3e16184aSTomer Maimon #define NPCM_PECI_PDDR 0x2C 27*3e16184aSTomer Maimon #define NPCM_PECI_DAT_INOUT(n) (0x100 + ((n) * 4)) 28*3e16184aSTomer Maimon 29*3e16184aSTomer Maimon #define NPCM_PECI_MAX_REG 0x200 30*3e16184aSTomer Maimon 31*3e16184aSTomer Maimon /* NPCM_PECI_CTL_STS - 0x00 : Control Register */ 32*3e16184aSTomer Maimon #define NPCM_PECI_CTRL_DONE_INT_EN BIT(6) 33*3e16184aSTomer Maimon #define NPCM_PECI_CTRL_ABRT_ERR BIT(4) 34*3e16184aSTomer Maimon #define NPCM_PECI_CTRL_CRC_ERR BIT(3) 35*3e16184aSTomer Maimon #define NPCM_PECI_CTRL_DONE BIT(1) 36*3e16184aSTomer Maimon #define NPCM_PECI_CTRL_START_BUSY BIT(0) 37*3e16184aSTomer Maimon 38*3e16184aSTomer Maimon /* NPCM_PECI_RD_LENGTH - 0x04 : Command Register */ 39*3e16184aSTomer Maimon #define NPCM_PECI_RD_LEN_MASK GENMASK(6, 0) 40*3e16184aSTomer Maimon 41*3e16184aSTomer Maimon /* NPCM_PECI_CMD - 0x10 : Command Register */ 42*3e16184aSTomer Maimon #define NPCM_PECI_CTL2_MASK GENMASK(7, 6) 43*3e16184aSTomer Maimon 44*3e16184aSTomer Maimon /* NPCM_PECI_WR_LENGTH - 0x1C : Command Register */ 45*3e16184aSTomer Maimon #define NPCM_PECI_WR_LEN_MASK GENMASK(6, 0) 46*3e16184aSTomer Maimon 47*3e16184aSTomer Maimon /* NPCM_PECI_PDDR - 0x2C : Command Register */ 48*3e16184aSTomer Maimon #define NPCM_PECI_PDDR_MASK GENMASK(4, 0) 49*3e16184aSTomer Maimon 50*3e16184aSTomer Maimon #define NPCM_PECI_INT_MASK (NPCM_PECI_CTRL_ABRT_ERR | \ 51*3e16184aSTomer Maimon NPCM_PECI_CTRL_CRC_ERR | \ 52*3e16184aSTomer Maimon NPCM_PECI_CTRL_DONE) 53*3e16184aSTomer Maimon 54*3e16184aSTomer Maimon #define NPCM_PECI_IDLE_CHECK_TIMEOUT_USEC (50 * USEC_PER_MSEC) 55*3e16184aSTomer Maimon #define NPCM_PECI_IDLE_CHECK_INTERVAL_USEC (10 * USEC_PER_MSEC) 56*3e16184aSTomer Maimon #define NPCM_PECI_CMD_TIMEOUT_MS_DEFAULT 1000 57*3e16184aSTomer Maimon #define NPCM_PECI_CMD_TIMEOUT_MS_MAX 60000 58*3e16184aSTomer Maimon #define NPCM_PECI_HOST_NEG_BIT_RATE_DEFAULT 15 59*3e16184aSTomer Maimon #define NPCM_PECI_PULL_DOWN_DEFAULT 0 60*3e16184aSTomer Maimon 61*3e16184aSTomer Maimon struct npcm_peci { 62*3e16184aSTomer Maimon u32 cmd_timeout_ms; 63*3e16184aSTomer Maimon struct completion xfer_complete; 64*3e16184aSTomer Maimon struct regmap *regmap; 65*3e16184aSTomer Maimon u32 status; 66*3e16184aSTomer Maimon spinlock_t lock; /* to sync completion status handling */ 67*3e16184aSTomer Maimon struct peci_controller *controller; 68*3e16184aSTomer Maimon struct device *dev; 69*3e16184aSTomer Maimon struct clk *clk; 70*3e16184aSTomer Maimon int irq; 71*3e16184aSTomer Maimon }; 72*3e16184aSTomer Maimon 73*3e16184aSTomer Maimon static int npcm_peci_xfer(struct peci_controller *controller, u8 addr, struct peci_request *req) 74*3e16184aSTomer Maimon { 75*3e16184aSTomer Maimon struct npcm_peci *priv = dev_get_drvdata(controller->dev.parent); 76*3e16184aSTomer Maimon unsigned long timeout = msecs_to_jiffies(priv->cmd_timeout_ms); 77*3e16184aSTomer Maimon unsigned int msg_rd; 78*3e16184aSTomer Maimon u32 cmd_sts; 79*3e16184aSTomer Maimon int i, ret; 80*3e16184aSTomer Maimon 81*3e16184aSTomer Maimon /* Check command sts and bus idle state */ 82*3e16184aSTomer Maimon ret = regmap_read_poll_timeout(priv->regmap, NPCM_PECI_CTL_STS, cmd_sts, 83*3e16184aSTomer Maimon !(cmd_sts & NPCM_PECI_CTRL_START_BUSY), 84*3e16184aSTomer Maimon NPCM_PECI_IDLE_CHECK_INTERVAL_USEC, 85*3e16184aSTomer Maimon NPCM_PECI_IDLE_CHECK_TIMEOUT_USEC); 86*3e16184aSTomer Maimon if (ret) 87*3e16184aSTomer Maimon return ret; /* -ETIMEDOUT */ 88*3e16184aSTomer Maimon 89*3e16184aSTomer Maimon spin_lock_irq(&priv->lock); 90*3e16184aSTomer Maimon reinit_completion(&priv->xfer_complete); 91*3e16184aSTomer Maimon 92*3e16184aSTomer Maimon regmap_write(priv->regmap, NPCM_PECI_ADDR, addr); 93*3e16184aSTomer Maimon regmap_write(priv->regmap, NPCM_PECI_RD_LENGTH, NPCM_PECI_WR_LEN_MASK & req->rx.len); 94*3e16184aSTomer Maimon regmap_write(priv->regmap, NPCM_PECI_WR_LENGTH, NPCM_PECI_WR_LEN_MASK & req->tx.len); 95*3e16184aSTomer Maimon 96*3e16184aSTomer Maimon if (req->tx.len) { 97*3e16184aSTomer Maimon regmap_write(priv->regmap, NPCM_PECI_CMD, req->tx.buf[0]); 98*3e16184aSTomer Maimon 99*3e16184aSTomer Maimon for (i = 0; i < (req->tx.len - 1); i++) 100*3e16184aSTomer Maimon regmap_write(priv->regmap, NPCM_PECI_DAT_INOUT(i), req->tx.buf[i + 1]); 101*3e16184aSTomer Maimon } 102*3e16184aSTomer Maimon 103*3e16184aSTomer Maimon #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) 104*3e16184aSTomer Maimon dev_dbg(priv->dev, "addr : %#02x, tx.len : %#02x, rx.len : %#02x\n", 105*3e16184aSTomer Maimon addr, req->tx.len, req->rx.len); 106*3e16184aSTomer Maimon print_hex_dump_bytes("TX : ", DUMP_PREFIX_NONE, req->tx.buf, req->tx.len); 107*3e16184aSTomer Maimon #endif 108*3e16184aSTomer Maimon 109*3e16184aSTomer Maimon priv->status = 0; 110*3e16184aSTomer Maimon regmap_update_bits(priv->regmap, NPCM_PECI_CTL_STS, NPCM_PECI_CTRL_START_BUSY, 111*3e16184aSTomer Maimon NPCM_PECI_CTRL_START_BUSY); 112*3e16184aSTomer Maimon 113*3e16184aSTomer Maimon spin_unlock_irq(&priv->lock); 114*3e16184aSTomer Maimon 115*3e16184aSTomer Maimon ret = wait_for_completion_interruptible_timeout(&priv->xfer_complete, timeout); 116*3e16184aSTomer Maimon if (ret < 0) 117*3e16184aSTomer Maimon return ret; 118*3e16184aSTomer Maimon 119*3e16184aSTomer Maimon if (ret == 0) { 120*3e16184aSTomer Maimon dev_dbg(priv->dev, "timeout waiting for a response\n"); 121*3e16184aSTomer Maimon return -ETIMEDOUT; 122*3e16184aSTomer Maimon } 123*3e16184aSTomer Maimon 124*3e16184aSTomer Maimon spin_lock_irq(&priv->lock); 125*3e16184aSTomer Maimon 126*3e16184aSTomer Maimon if (priv->status != NPCM_PECI_CTRL_DONE) { 127*3e16184aSTomer Maimon spin_unlock_irq(&priv->lock); 128*3e16184aSTomer Maimon dev_dbg(priv->dev, "no valid response, status: %#02x\n", priv->status); 129*3e16184aSTomer Maimon return -EIO; 130*3e16184aSTomer Maimon } 131*3e16184aSTomer Maimon 132*3e16184aSTomer Maimon regmap_write(priv->regmap, NPCM_PECI_CMD, 0); 133*3e16184aSTomer Maimon 134*3e16184aSTomer Maimon for (i = 0; i < req->rx.len; i++) { 135*3e16184aSTomer Maimon regmap_read(priv->regmap, NPCM_PECI_DAT_INOUT(i), &msg_rd); 136*3e16184aSTomer Maimon req->rx.buf[i] = (u8)msg_rd; 137*3e16184aSTomer Maimon } 138*3e16184aSTomer Maimon 139*3e16184aSTomer Maimon spin_unlock_irq(&priv->lock); 140*3e16184aSTomer Maimon 141*3e16184aSTomer Maimon #if IS_ENABLED(CONFIG_DYNAMIC_DEBUG) 142*3e16184aSTomer Maimon print_hex_dump_bytes("RX : ", DUMP_PREFIX_NONE, req->rx.buf, req->rx.len); 143*3e16184aSTomer Maimon #endif 144*3e16184aSTomer Maimon return 0; 145*3e16184aSTomer Maimon } 146*3e16184aSTomer Maimon 147*3e16184aSTomer Maimon static irqreturn_t npcm_peci_irq_handler(int irq, void *arg) 148*3e16184aSTomer Maimon { 149*3e16184aSTomer Maimon struct npcm_peci *priv = arg; 150*3e16184aSTomer Maimon u32 status_ack = 0; 151*3e16184aSTomer Maimon u32 status; 152*3e16184aSTomer Maimon 153*3e16184aSTomer Maimon spin_lock(&priv->lock); 154*3e16184aSTomer Maimon regmap_read(priv->regmap, NPCM_PECI_CTL_STS, &status); 155*3e16184aSTomer Maimon priv->status |= (status & NPCM_PECI_INT_MASK); 156*3e16184aSTomer Maimon 157*3e16184aSTomer Maimon if (status & NPCM_PECI_CTRL_CRC_ERR) 158*3e16184aSTomer Maimon status_ack |= NPCM_PECI_CTRL_CRC_ERR; 159*3e16184aSTomer Maimon 160*3e16184aSTomer Maimon if (status & NPCM_PECI_CTRL_ABRT_ERR) 161*3e16184aSTomer Maimon status_ack |= NPCM_PECI_CTRL_ABRT_ERR; 162*3e16184aSTomer Maimon 163*3e16184aSTomer Maimon /* 164*3e16184aSTomer Maimon * All commands should be ended up with a NPCM_PECI_CTRL_DONE 165*3e16184aSTomer Maimon * bit set even in an error case. 166*3e16184aSTomer Maimon */ 167*3e16184aSTomer Maimon if (status & NPCM_PECI_CTRL_DONE) { 168*3e16184aSTomer Maimon status_ack |= NPCM_PECI_CTRL_DONE; 169*3e16184aSTomer Maimon complete(&priv->xfer_complete); 170*3e16184aSTomer Maimon } 171*3e16184aSTomer Maimon 172*3e16184aSTomer Maimon regmap_write_bits(priv->regmap, NPCM_PECI_CTL_STS, NPCM_PECI_INT_MASK, status_ack); 173*3e16184aSTomer Maimon 174*3e16184aSTomer Maimon spin_unlock(&priv->lock); 175*3e16184aSTomer Maimon return IRQ_HANDLED; 176*3e16184aSTomer Maimon } 177*3e16184aSTomer Maimon 178*3e16184aSTomer Maimon static int npcm_peci_init_ctrl(struct npcm_peci *priv) 179*3e16184aSTomer Maimon { 180*3e16184aSTomer Maimon u32 cmd_sts; 181*3e16184aSTomer Maimon int ret; 182*3e16184aSTomer Maimon 183*3e16184aSTomer Maimon priv->clk = devm_clk_get_enabled(priv->dev, NULL); 184*3e16184aSTomer Maimon if (IS_ERR(priv->clk)) { 185*3e16184aSTomer Maimon dev_err(priv->dev, "failed to get ref clock\n"); 186*3e16184aSTomer Maimon return PTR_ERR(priv->clk); 187*3e16184aSTomer Maimon } 188*3e16184aSTomer Maimon 189*3e16184aSTomer Maimon ret = device_property_read_u32(priv->dev, "cmd-timeout-ms", &priv->cmd_timeout_ms); 190*3e16184aSTomer Maimon if (ret) { 191*3e16184aSTomer Maimon priv->cmd_timeout_ms = NPCM_PECI_CMD_TIMEOUT_MS_DEFAULT; 192*3e16184aSTomer Maimon } else if (priv->cmd_timeout_ms > NPCM_PECI_CMD_TIMEOUT_MS_MAX || 193*3e16184aSTomer Maimon priv->cmd_timeout_ms == 0) { 194*3e16184aSTomer Maimon dev_warn(priv->dev, "invalid cmd-timeout-ms: %u, falling back to: %u\n", 195*3e16184aSTomer Maimon priv->cmd_timeout_ms, NPCM_PECI_CMD_TIMEOUT_MS_DEFAULT); 196*3e16184aSTomer Maimon 197*3e16184aSTomer Maimon priv->cmd_timeout_ms = NPCM_PECI_CMD_TIMEOUT_MS_DEFAULT; 198*3e16184aSTomer Maimon } 199*3e16184aSTomer Maimon 200*3e16184aSTomer Maimon regmap_update_bits(priv->regmap, NPCM_PECI_CTL2, NPCM_PECI_CTL2_MASK, 201*3e16184aSTomer Maimon NPCM_PECI_PULL_DOWN_DEFAULT << 6); 202*3e16184aSTomer Maimon 203*3e16184aSTomer Maimon regmap_update_bits(priv->regmap, NPCM_PECI_PDDR, NPCM_PECI_PDDR_MASK, 204*3e16184aSTomer Maimon NPCM_PECI_HOST_NEG_BIT_RATE_DEFAULT); 205*3e16184aSTomer Maimon 206*3e16184aSTomer Maimon ret = regmap_read_poll_timeout(priv->regmap, NPCM_PECI_CTL_STS, cmd_sts, 207*3e16184aSTomer Maimon !(cmd_sts & NPCM_PECI_CTRL_START_BUSY), 208*3e16184aSTomer Maimon NPCM_PECI_IDLE_CHECK_INTERVAL_USEC, 209*3e16184aSTomer Maimon NPCM_PECI_IDLE_CHECK_TIMEOUT_USEC); 210*3e16184aSTomer Maimon if (ret) 211*3e16184aSTomer Maimon return ret; /* -ETIMEDOUT */ 212*3e16184aSTomer Maimon 213*3e16184aSTomer Maimon /* PECI interrupt enable */ 214*3e16184aSTomer Maimon regmap_update_bits(priv->regmap, NPCM_PECI_CTL_STS, NPCM_PECI_CTRL_DONE_INT_EN, 215*3e16184aSTomer Maimon NPCM_PECI_CTRL_DONE_INT_EN); 216*3e16184aSTomer Maimon 217*3e16184aSTomer Maimon return 0; 218*3e16184aSTomer Maimon } 219*3e16184aSTomer Maimon 220*3e16184aSTomer Maimon static const struct regmap_config npcm_peci_regmap_config = { 221*3e16184aSTomer Maimon .reg_bits = 8, 222*3e16184aSTomer Maimon .val_bits = 8, 223*3e16184aSTomer Maimon .max_register = NPCM_PECI_MAX_REG, 224*3e16184aSTomer Maimon .fast_io = true, 225*3e16184aSTomer Maimon }; 226*3e16184aSTomer Maimon 227*3e16184aSTomer Maimon static struct peci_controller_ops npcm_ops = { 228*3e16184aSTomer Maimon .xfer = npcm_peci_xfer, 229*3e16184aSTomer Maimon }; 230*3e16184aSTomer Maimon 231*3e16184aSTomer Maimon static int npcm_peci_probe(struct platform_device *pdev) 232*3e16184aSTomer Maimon { 233*3e16184aSTomer Maimon struct peci_controller *controller; 234*3e16184aSTomer Maimon struct npcm_peci *priv; 235*3e16184aSTomer Maimon void __iomem *base; 236*3e16184aSTomer Maimon int ret; 237*3e16184aSTomer Maimon 238*3e16184aSTomer Maimon priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); 239*3e16184aSTomer Maimon if (!priv) 240*3e16184aSTomer Maimon return -ENOMEM; 241*3e16184aSTomer Maimon 242*3e16184aSTomer Maimon priv->dev = &pdev->dev; 243*3e16184aSTomer Maimon dev_set_drvdata(&pdev->dev, priv); 244*3e16184aSTomer Maimon 245*3e16184aSTomer Maimon base = devm_platform_ioremap_resource(pdev, 0); 246*3e16184aSTomer Maimon if (IS_ERR(base)) 247*3e16184aSTomer Maimon return PTR_ERR(base); 248*3e16184aSTomer Maimon 249*3e16184aSTomer Maimon priv->regmap = devm_regmap_init_mmio(&pdev->dev, base, &npcm_peci_regmap_config); 250*3e16184aSTomer Maimon if (IS_ERR(priv->regmap)) 251*3e16184aSTomer Maimon return PTR_ERR(priv->regmap); 252*3e16184aSTomer Maimon 253*3e16184aSTomer Maimon priv->irq = platform_get_irq(pdev, 0); 254*3e16184aSTomer Maimon if (priv->irq < 0) 255*3e16184aSTomer Maimon return priv->irq; 256*3e16184aSTomer Maimon 257*3e16184aSTomer Maimon ret = devm_request_irq(&pdev->dev, priv->irq, npcm_peci_irq_handler, 258*3e16184aSTomer Maimon 0, "peci-npcm-irq", priv); 259*3e16184aSTomer Maimon if (ret) 260*3e16184aSTomer Maimon return ret; 261*3e16184aSTomer Maimon 262*3e16184aSTomer Maimon init_completion(&priv->xfer_complete); 263*3e16184aSTomer Maimon spin_lock_init(&priv->lock); 264*3e16184aSTomer Maimon 265*3e16184aSTomer Maimon ret = npcm_peci_init_ctrl(priv); 266*3e16184aSTomer Maimon if (ret) 267*3e16184aSTomer Maimon return ret; 268*3e16184aSTomer Maimon 269*3e16184aSTomer Maimon controller = devm_peci_controller_add(priv->dev, &npcm_ops); 270*3e16184aSTomer Maimon if (IS_ERR(controller)) 271*3e16184aSTomer Maimon return dev_err_probe(priv->dev, PTR_ERR(controller), 272*3e16184aSTomer Maimon "failed to add npcm peci controller\n"); 273*3e16184aSTomer Maimon 274*3e16184aSTomer Maimon priv->controller = controller; 275*3e16184aSTomer Maimon 276*3e16184aSTomer Maimon return 0; 277*3e16184aSTomer Maimon } 278*3e16184aSTomer Maimon 279*3e16184aSTomer Maimon static const struct of_device_id npcm_peci_of_table[] = { 280*3e16184aSTomer Maimon { .compatible = "nuvoton,npcm750-peci", }, 281*3e16184aSTomer Maimon { .compatible = "nuvoton,npcm845-peci", }, 282*3e16184aSTomer Maimon { } 283*3e16184aSTomer Maimon }; 284*3e16184aSTomer Maimon MODULE_DEVICE_TABLE(of, npcm_peci_of_table); 285*3e16184aSTomer Maimon 286*3e16184aSTomer Maimon static struct platform_driver npcm_peci_driver = { 287*3e16184aSTomer Maimon .probe = npcm_peci_probe, 288*3e16184aSTomer Maimon .driver = { 289*3e16184aSTomer Maimon .name = KBUILD_MODNAME, 290*3e16184aSTomer Maimon .of_match_table = npcm_peci_of_table, 291*3e16184aSTomer Maimon }, 292*3e16184aSTomer Maimon }; 293*3e16184aSTomer Maimon module_platform_driver(npcm_peci_driver); 294*3e16184aSTomer Maimon 295*3e16184aSTomer Maimon MODULE_AUTHOR("Tomer Maimon <tomer.maimon@nuvoton.com>"); 296*3e16184aSTomer Maimon MODULE_DESCRIPTION("NPCM PECI driver"); 297*3e16184aSTomer Maimon MODULE_LICENSE("GPL"); 298*3e16184aSTomer Maimon MODULE_IMPORT_NS(PECI); 299