197fb5e8dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
245fcac1aSArnd Bergmann /* Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
345fcac1aSArnd Bergmann * Copyright (c) 2010, Google Inc.
445fcac1aSArnd Bergmann *
545fcac1aSArnd Bergmann * Original authors: Code Aurora Forum
645fcac1aSArnd Bergmann *
745fcac1aSArnd Bergmann * Author: Dima Zavin <dima@android.com>
845fcac1aSArnd Bergmann * - Largely rewritten from original to not be an i2c driver.
945fcac1aSArnd Bergmann */
1045fcac1aSArnd Bergmann
1145fcac1aSArnd Bergmann #define pr_fmt(fmt) "%s: " fmt, __func__
1245fcac1aSArnd Bergmann
1345fcac1aSArnd Bergmann #include <linux/delay.h>
1445fcac1aSArnd Bergmann #include <linux/err.h>
1545fcac1aSArnd Bergmann #include <linux/io.h>
1645fcac1aSArnd Bergmann #include <linux/kernel.h>
17dc0c386eSRob Herring #include <linux/module.h>
18dc0c386eSRob Herring #include <linux/of.h>
19dc0c386eSRob Herring #include <linux/of_platform.h>
2045fcac1aSArnd Bergmann #include <linux/platform_device.h>
2145fcac1aSArnd Bergmann #include <linux/slab.h>
2245fcac1aSArnd Bergmann #include <linux/ssbi.h>
2345fcac1aSArnd Bergmann
2445fcac1aSArnd Bergmann /* SSBI 2.0 controller registers */
2545fcac1aSArnd Bergmann #define SSBI2_CMD 0x0008
2645fcac1aSArnd Bergmann #define SSBI2_RD 0x0010
2745fcac1aSArnd Bergmann #define SSBI2_STATUS 0x0014
2845fcac1aSArnd Bergmann #define SSBI2_MODE2 0x001C
2945fcac1aSArnd Bergmann
3045fcac1aSArnd Bergmann /* SSBI_CMD fields */
3145fcac1aSArnd Bergmann #define SSBI_CMD_RDWRN (1 << 24)
3245fcac1aSArnd Bergmann
3345fcac1aSArnd Bergmann /* SSBI_STATUS fields */
3445fcac1aSArnd Bergmann #define SSBI_STATUS_RD_READY (1 << 2)
3545fcac1aSArnd Bergmann #define SSBI_STATUS_READY (1 << 1)
3645fcac1aSArnd Bergmann #define SSBI_STATUS_MCHN_BUSY (1 << 0)
3745fcac1aSArnd Bergmann
3845fcac1aSArnd Bergmann /* SSBI_MODE2 fields */
3945fcac1aSArnd Bergmann #define SSBI_MODE2_REG_ADDR_15_8_SHFT 0x04
4045fcac1aSArnd Bergmann #define SSBI_MODE2_REG_ADDR_15_8_MASK (0x7f << SSBI_MODE2_REG_ADDR_15_8_SHFT)
4145fcac1aSArnd Bergmann
4245fcac1aSArnd Bergmann #define SET_SSBI_MODE2_REG_ADDR_15_8(MD, AD) \
4345fcac1aSArnd Bergmann (((MD) & 0x0F) | ((((AD) >> 8) << SSBI_MODE2_REG_ADDR_15_8_SHFT) & \
4445fcac1aSArnd Bergmann SSBI_MODE2_REG_ADDR_15_8_MASK))
4545fcac1aSArnd Bergmann
4645fcac1aSArnd Bergmann /* SSBI PMIC Arbiter command registers */
4745fcac1aSArnd Bergmann #define SSBI_PA_CMD 0x0000
4845fcac1aSArnd Bergmann #define SSBI_PA_RD_STATUS 0x0004
4945fcac1aSArnd Bergmann
5045fcac1aSArnd Bergmann /* SSBI_PA_CMD fields */
5145fcac1aSArnd Bergmann #define SSBI_PA_CMD_RDWRN (1 << 24)
5245fcac1aSArnd Bergmann #define SSBI_PA_CMD_ADDR_MASK 0x7fff /* REG_ADDR_7_0, REG_ADDR_8_14*/
5345fcac1aSArnd Bergmann
5445fcac1aSArnd Bergmann /* SSBI_PA_RD_STATUS fields */
5545fcac1aSArnd Bergmann #define SSBI_PA_RD_STATUS_TRANS_DONE (1 << 27)
5645fcac1aSArnd Bergmann #define SSBI_PA_RD_STATUS_TRANS_DENIED (1 << 26)
5745fcac1aSArnd Bergmann
5845fcac1aSArnd Bergmann #define SSBI_TIMEOUT_US 100
5945fcac1aSArnd Bergmann
60bae911a0SStephen Boyd enum ssbi_controller_type {
61bae911a0SStephen Boyd MSM_SBI_CTRL_SSBI = 0,
62bae911a0SStephen Boyd MSM_SBI_CTRL_SSBI2,
63bae911a0SStephen Boyd MSM_SBI_CTRL_PMIC_ARBITER,
64bae911a0SStephen Boyd };
65bae911a0SStephen Boyd
6645fcac1aSArnd Bergmann struct ssbi {
6745fcac1aSArnd Bergmann void __iomem *base;
6845fcac1aSArnd Bergmann spinlock_t lock;
6945fcac1aSArnd Bergmann enum ssbi_controller_type controller_type;
7045fcac1aSArnd Bergmann int (*read)(struct ssbi *, u16 addr, u8 *buf, int len);
715eec14ccSStephen Boyd int (*write)(struct ssbi *, u16 addr, const u8 *buf, int len);
7245fcac1aSArnd Bergmann };
7345fcac1aSArnd Bergmann
ssbi_readl(struct ssbi * ssbi,u32 reg)7445fcac1aSArnd Bergmann static inline u32 ssbi_readl(struct ssbi *ssbi, u32 reg)
7545fcac1aSArnd Bergmann {
7645fcac1aSArnd Bergmann return readl(ssbi->base + reg);
7745fcac1aSArnd Bergmann }
7845fcac1aSArnd Bergmann
ssbi_writel(struct ssbi * ssbi,u32 val,u32 reg)7945fcac1aSArnd Bergmann static inline void ssbi_writel(struct ssbi *ssbi, u32 val, u32 reg)
8045fcac1aSArnd Bergmann {
8145fcac1aSArnd Bergmann writel(val, ssbi->base + reg);
8245fcac1aSArnd Bergmann }
8345fcac1aSArnd Bergmann
8445fcac1aSArnd Bergmann /*
8545fcac1aSArnd Bergmann * Via private exchange with one of the original authors, the hardware
8645fcac1aSArnd Bergmann * should generally finish a transaction in about 5us. The worst
8745fcac1aSArnd Bergmann * case, is when using the arbiter and both other CPUs have just
8845fcac1aSArnd Bergmann * started trying to use the SSBI bus will result in a time of about
8945fcac1aSArnd Bergmann * 20us. It should never take longer than this.
9045fcac1aSArnd Bergmann *
9145fcac1aSArnd Bergmann * As such, this wait merely spins, with a udelay.
9245fcac1aSArnd Bergmann */
ssbi_wait_mask(struct ssbi * ssbi,u32 set_mask,u32 clr_mask)9345fcac1aSArnd Bergmann static int ssbi_wait_mask(struct ssbi *ssbi, u32 set_mask, u32 clr_mask)
9445fcac1aSArnd Bergmann {
9545fcac1aSArnd Bergmann u32 timeout = SSBI_TIMEOUT_US;
9645fcac1aSArnd Bergmann u32 val;
9745fcac1aSArnd Bergmann
9845fcac1aSArnd Bergmann while (timeout--) {
9945fcac1aSArnd Bergmann val = ssbi_readl(ssbi, SSBI2_STATUS);
10045fcac1aSArnd Bergmann if (((val & set_mask) == set_mask) && ((val & clr_mask) == 0))
10145fcac1aSArnd Bergmann return 0;
10245fcac1aSArnd Bergmann udelay(1);
10345fcac1aSArnd Bergmann }
10445fcac1aSArnd Bergmann
10545fcac1aSArnd Bergmann return -ETIMEDOUT;
10645fcac1aSArnd Bergmann }
10745fcac1aSArnd Bergmann
10845fcac1aSArnd Bergmann static int
ssbi_read_bytes(struct ssbi * ssbi,u16 addr,u8 * buf,int len)10945fcac1aSArnd Bergmann ssbi_read_bytes(struct ssbi *ssbi, u16 addr, u8 *buf, int len)
11045fcac1aSArnd Bergmann {
11145fcac1aSArnd Bergmann u32 cmd = SSBI_CMD_RDWRN | ((addr & 0xff) << 16);
11245fcac1aSArnd Bergmann int ret = 0;
11345fcac1aSArnd Bergmann
11445fcac1aSArnd Bergmann if (ssbi->controller_type == MSM_SBI_CTRL_SSBI2) {
11545fcac1aSArnd Bergmann u32 mode2 = ssbi_readl(ssbi, SSBI2_MODE2);
11645fcac1aSArnd Bergmann mode2 = SET_SSBI_MODE2_REG_ADDR_15_8(mode2, addr);
11745fcac1aSArnd Bergmann ssbi_writel(ssbi, mode2, SSBI2_MODE2);
11845fcac1aSArnd Bergmann }
11945fcac1aSArnd Bergmann
12045fcac1aSArnd Bergmann while (len) {
12145fcac1aSArnd Bergmann ret = ssbi_wait_mask(ssbi, SSBI_STATUS_READY, 0);
12245fcac1aSArnd Bergmann if (ret)
12345fcac1aSArnd Bergmann goto err;
12445fcac1aSArnd Bergmann
12545fcac1aSArnd Bergmann ssbi_writel(ssbi, cmd, SSBI2_CMD);
12645fcac1aSArnd Bergmann ret = ssbi_wait_mask(ssbi, SSBI_STATUS_RD_READY, 0);
12745fcac1aSArnd Bergmann if (ret)
12845fcac1aSArnd Bergmann goto err;
12945fcac1aSArnd Bergmann *buf++ = ssbi_readl(ssbi, SSBI2_RD) & 0xff;
13045fcac1aSArnd Bergmann len--;
13145fcac1aSArnd Bergmann }
13245fcac1aSArnd Bergmann
13345fcac1aSArnd Bergmann err:
13445fcac1aSArnd Bergmann return ret;
13545fcac1aSArnd Bergmann }
13645fcac1aSArnd Bergmann
13745fcac1aSArnd Bergmann static int
ssbi_write_bytes(struct ssbi * ssbi,u16 addr,const u8 * buf,int len)1385eec14ccSStephen Boyd ssbi_write_bytes(struct ssbi *ssbi, u16 addr, const u8 *buf, int len)
13945fcac1aSArnd Bergmann {
14045fcac1aSArnd Bergmann int ret = 0;
14145fcac1aSArnd Bergmann
14245fcac1aSArnd Bergmann if (ssbi->controller_type == MSM_SBI_CTRL_SSBI2) {
14345fcac1aSArnd Bergmann u32 mode2 = ssbi_readl(ssbi, SSBI2_MODE2);
14445fcac1aSArnd Bergmann mode2 = SET_SSBI_MODE2_REG_ADDR_15_8(mode2, addr);
14545fcac1aSArnd Bergmann ssbi_writel(ssbi, mode2, SSBI2_MODE2);
14645fcac1aSArnd Bergmann }
14745fcac1aSArnd Bergmann
14845fcac1aSArnd Bergmann while (len) {
14945fcac1aSArnd Bergmann ret = ssbi_wait_mask(ssbi, SSBI_STATUS_READY, 0);
15045fcac1aSArnd Bergmann if (ret)
15145fcac1aSArnd Bergmann goto err;
15245fcac1aSArnd Bergmann
15345fcac1aSArnd Bergmann ssbi_writel(ssbi, ((addr & 0xff) << 16) | *buf, SSBI2_CMD);
15445fcac1aSArnd Bergmann ret = ssbi_wait_mask(ssbi, 0, SSBI_STATUS_MCHN_BUSY);
15545fcac1aSArnd Bergmann if (ret)
15645fcac1aSArnd Bergmann goto err;
15745fcac1aSArnd Bergmann buf++;
15845fcac1aSArnd Bergmann len--;
15945fcac1aSArnd Bergmann }
16045fcac1aSArnd Bergmann
16145fcac1aSArnd Bergmann err:
16245fcac1aSArnd Bergmann return ret;
16345fcac1aSArnd Bergmann }
16445fcac1aSArnd Bergmann
16545fcac1aSArnd Bergmann /*
16645fcac1aSArnd Bergmann * See ssbi_wait_mask for an explanation of the time and the
16745fcac1aSArnd Bergmann * busywait.
16845fcac1aSArnd Bergmann */
16945fcac1aSArnd Bergmann static inline int
ssbi_pa_transfer(struct ssbi * ssbi,u32 cmd,u8 * data)17045fcac1aSArnd Bergmann ssbi_pa_transfer(struct ssbi *ssbi, u32 cmd, u8 *data)
17145fcac1aSArnd Bergmann {
17245fcac1aSArnd Bergmann u32 timeout = SSBI_TIMEOUT_US;
17345fcac1aSArnd Bergmann u32 rd_status = 0;
17445fcac1aSArnd Bergmann
17545fcac1aSArnd Bergmann ssbi_writel(ssbi, cmd, SSBI_PA_CMD);
17645fcac1aSArnd Bergmann
17745fcac1aSArnd Bergmann while (timeout--) {
17845fcac1aSArnd Bergmann rd_status = ssbi_readl(ssbi, SSBI_PA_RD_STATUS);
17945fcac1aSArnd Bergmann
18045fcac1aSArnd Bergmann if (rd_status & SSBI_PA_RD_STATUS_TRANS_DENIED)
18145fcac1aSArnd Bergmann return -EPERM;
18245fcac1aSArnd Bergmann
18345fcac1aSArnd Bergmann if (rd_status & SSBI_PA_RD_STATUS_TRANS_DONE) {
18445fcac1aSArnd Bergmann if (data)
18545fcac1aSArnd Bergmann *data = rd_status & 0xff;
18645fcac1aSArnd Bergmann return 0;
18745fcac1aSArnd Bergmann }
18845fcac1aSArnd Bergmann udelay(1);
18945fcac1aSArnd Bergmann }
19045fcac1aSArnd Bergmann
19145fcac1aSArnd Bergmann return -ETIMEDOUT;
19245fcac1aSArnd Bergmann }
19345fcac1aSArnd Bergmann
19445fcac1aSArnd Bergmann static int
ssbi_pa_read_bytes(struct ssbi * ssbi,u16 addr,u8 * buf,int len)19545fcac1aSArnd Bergmann ssbi_pa_read_bytes(struct ssbi *ssbi, u16 addr, u8 *buf, int len)
19645fcac1aSArnd Bergmann {
19745fcac1aSArnd Bergmann u32 cmd;
19845fcac1aSArnd Bergmann int ret = 0;
19945fcac1aSArnd Bergmann
20045fcac1aSArnd Bergmann cmd = SSBI_PA_CMD_RDWRN | (addr & SSBI_PA_CMD_ADDR_MASK) << 8;
20145fcac1aSArnd Bergmann
20245fcac1aSArnd Bergmann while (len) {
20345fcac1aSArnd Bergmann ret = ssbi_pa_transfer(ssbi, cmd, buf);
20445fcac1aSArnd Bergmann if (ret)
20545fcac1aSArnd Bergmann goto err;
20645fcac1aSArnd Bergmann buf++;
20745fcac1aSArnd Bergmann len--;
20845fcac1aSArnd Bergmann }
20945fcac1aSArnd Bergmann
21045fcac1aSArnd Bergmann err:
21145fcac1aSArnd Bergmann return ret;
21245fcac1aSArnd Bergmann }
21345fcac1aSArnd Bergmann
21445fcac1aSArnd Bergmann static int
ssbi_pa_write_bytes(struct ssbi * ssbi,u16 addr,const u8 * buf,int len)2155eec14ccSStephen Boyd ssbi_pa_write_bytes(struct ssbi *ssbi, u16 addr, const u8 *buf, int len)
21645fcac1aSArnd Bergmann {
21745fcac1aSArnd Bergmann u32 cmd;
21845fcac1aSArnd Bergmann int ret = 0;
21945fcac1aSArnd Bergmann
22045fcac1aSArnd Bergmann while (len) {
22145fcac1aSArnd Bergmann cmd = (addr & SSBI_PA_CMD_ADDR_MASK) << 8 | *buf;
22245fcac1aSArnd Bergmann ret = ssbi_pa_transfer(ssbi, cmd, NULL);
22345fcac1aSArnd Bergmann if (ret)
22445fcac1aSArnd Bergmann goto err;
22545fcac1aSArnd Bergmann buf++;
22645fcac1aSArnd Bergmann len--;
22745fcac1aSArnd Bergmann }
22845fcac1aSArnd Bergmann
22945fcac1aSArnd Bergmann err:
23045fcac1aSArnd Bergmann return ret;
23145fcac1aSArnd Bergmann }
23245fcac1aSArnd Bergmann
ssbi_read(struct device * dev,u16 addr,u8 * buf,int len)23345fcac1aSArnd Bergmann int ssbi_read(struct device *dev, u16 addr, u8 *buf, int len)
23445fcac1aSArnd Bergmann {
235ed835136SKefeng Wang struct ssbi *ssbi = dev_get_drvdata(dev);
23645fcac1aSArnd Bergmann unsigned long flags;
23745fcac1aSArnd Bergmann int ret;
23845fcac1aSArnd Bergmann
23945fcac1aSArnd Bergmann spin_lock_irqsave(&ssbi->lock, flags);
24045fcac1aSArnd Bergmann ret = ssbi->read(ssbi, addr, buf, len);
24145fcac1aSArnd Bergmann spin_unlock_irqrestore(&ssbi->lock, flags);
24245fcac1aSArnd Bergmann
24345fcac1aSArnd Bergmann return ret;
24445fcac1aSArnd Bergmann }
24545fcac1aSArnd Bergmann EXPORT_SYMBOL_GPL(ssbi_read);
24645fcac1aSArnd Bergmann
ssbi_write(struct device * dev,u16 addr,const u8 * buf,int len)2475eec14ccSStephen Boyd int ssbi_write(struct device *dev, u16 addr, const u8 *buf, int len)
24845fcac1aSArnd Bergmann {
249ed835136SKefeng Wang struct ssbi *ssbi = dev_get_drvdata(dev);
25045fcac1aSArnd Bergmann unsigned long flags;
25145fcac1aSArnd Bergmann int ret;
25245fcac1aSArnd Bergmann
25345fcac1aSArnd Bergmann spin_lock_irqsave(&ssbi->lock, flags);
25445fcac1aSArnd Bergmann ret = ssbi->write(ssbi, addr, buf, len);
25545fcac1aSArnd Bergmann spin_unlock_irqrestore(&ssbi->lock, flags);
25645fcac1aSArnd Bergmann
25745fcac1aSArnd Bergmann return ret;
25845fcac1aSArnd Bergmann }
25945fcac1aSArnd Bergmann EXPORT_SYMBOL_GPL(ssbi_write);
26045fcac1aSArnd Bergmann
ssbi_probe(struct platform_device * pdev)26145fcac1aSArnd Bergmann static int ssbi_probe(struct platform_device *pdev)
26245fcac1aSArnd Bergmann {
26345fcac1aSArnd Bergmann struct device_node *np = pdev->dev.of_node;
26445fcac1aSArnd Bergmann struct ssbi *ssbi;
26545fcac1aSArnd Bergmann const char *type;
26645fcac1aSArnd Bergmann
267e5784388SStephen Boyd ssbi = devm_kzalloc(&pdev->dev, sizeof(*ssbi), GFP_KERNEL);
268e5784388SStephen Boyd if (!ssbi)
26945fcac1aSArnd Bergmann return -ENOMEM;
27045fcac1aSArnd Bergmann
2710479ed2cSYe Xingchen ssbi->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
272e5784388SStephen Boyd if (IS_ERR(ssbi->base))
273e5784388SStephen Boyd return PTR_ERR(ssbi->base);
27445fcac1aSArnd Bergmann
27545fcac1aSArnd Bergmann platform_set_drvdata(pdev, ssbi);
27645fcac1aSArnd Bergmann
27745fcac1aSArnd Bergmann type = of_get_property(np, "qcom,controller-type", NULL);
27845fcac1aSArnd Bergmann if (type == NULL) {
279e5784388SStephen Boyd dev_err(&pdev->dev, "Missing qcom,controller-type property\n");
280e5784388SStephen Boyd return -EINVAL;
28145fcac1aSArnd Bergmann }
28245fcac1aSArnd Bergmann dev_info(&pdev->dev, "SSBI controller type: '%s'\n", type);
28345fcac1aSArnd Bergmann if (strcmp(type, "ssbi") == 0)
28445fcac1aSArnd Bergmann ssbi->controller_type = MSM_SBI_CTRL_SSBI;
28545fcac1aSArnd Bergmann else if (strcmp(type, "ssbi2") == 0)
28645fcac1aSArnd Bergmann ssbi->controller_type = MSM_SBI_CTRL_SSBI2;
28745fcac1aSArnd Bergmann else if (strcmp(type, "pmic-arbiter") == 0)
28845fcac1aSArnd Bergmann ssbi->controller_type = MSM_SBI_CTRL_PMIC_ARBITER;
28945fcac1aSArnd Bergmann else {
290e5784388SStephen Boyd dev_err(&pdev->dev, "Unknown qcom,controller-type\n");
291e5784388SStephen Boyd return -EINVAL;
29245fcac1aSArnd Bergmann }
29345fcac1aSArnd Bergmann
29445fcac1aSArnd Bergmann if (ssbi->controller_type == MSM_SBI_CTRL_PMIC_ARBITER) {
29545fcac1aSArnd Bergmann ssbi->read = ssbi_pa_read_bytes;
29645fcac1aSArnd Bergmann ssbi->write = ssbi_pa_write_bytes;
29745fcac1aSArnd Bergmann } else {
29845fcac1aSArnd Bergmann ssbi->read = ssbi_read_bytes;
29945fcac1aSArnd Bergmann ssbi->write = ssbi_write_bytes;
30045fcac1aSArnd Bergmann }
30145fcac1aSArnd Bergmann
30245fcac1aSArnd Bergmann spin_lock_init(&ssbi->lock);
30345fcac1aSArnd Bergmann
304f38aa351SBenjamin Gaignard return devm_of_platform_populate(&pdev->dev);
30545fcac1aSArnd Bergmann }
30645fcac1aSArnd Bergmann
30712eda2a2SStephen Boyd static const struct of_device_id ssbi_match_table[] = {
30845fcac1aSArnd Bergmann { .compatible = "qcom,ssbi" },
30945fcac1aSArnd Bergmann {}
31045fcac1aSArnd Bergmann };
3116378c1e5SStephen Boyd MODULE_DEVICE_TABLE(of, ssbi_match_table);
31245fcac1aSArnd Bergmann
31345fcac1aSArnd Bergmann static struct platform_driver ssbi_driver = {
31445fcac1aSArnd Bergmann .probe = ssbi_probe,
31545fcac1aSArnd Bergmann .driver = {
31645fcac1aSArnd Bergmann .name = "ssbi",
31745fcac1aSArnd Bergmann .of_match_table = ssbi_match_table,
31845fcac1aSArnd Bergmann },
31945fcac1aSArnd Bergmann };
320e5784388SStephen Boyd module_platform_driver(ssbi_driver);
32145fcac1aSArnd Bergmann
322*5fed47abSJeff Johnson MODULE_DESCRIPTION("Qualcomm Single-wire Serial Bus Interface (SSBI) driver");
32345fcac1aSArnd Bergmann MODULE_LICENSE("GPL v2");
32445fcac1aSArnd Bergmann MODULE_VERSION("1.0");
32545fcac1aSArnd Bergmann MODULE_ALIAS("platform:ssbi");
32645fcac1aSArnd Bergmann MODULE_AUTHOR("Dima Zavin <dima@android.com>");
327