Debian Codegraph @

src/soc/qualcomm/common/spmi.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 #include <arch/mmio.h>
4 #include <commonlib/bsd/stdlib.h>
5 #include <soc/addressmap.h>
6 #include <soc/qcom_spmi.h>
7 #include <timer.h>
8
9 #define PPID_MASK (0xfffU << 8)
10
11 /* These are opcodes specific to this SPMI arbitrator, *not* SPMI commands. */
12 #define OPC_EXT_WRITEL 0
13 #define OPC_EXT_READL 1
14
15 #define ARB_STATUS_DONE BIT(0)
16 #define ARB_STATUS_FAILURE BIT(1)
17 #define ARB_STATUS_DENIED BIT(2)
18 #define ARB_STATUS_DROPPED BIT(3)
19
20 #define ERROR_APID_NOT_FOUND (-(int)BIT(8))
21 #define ERROR_TIMEOUT (-(int)BIT(9))
22
23 #define ARB_COMMAND_TIMEOUT_MS 100
24
25 // Individual register block per APID
26 struct qcom_spmi_regs {
27 uint32_t cmd;
28 uint32_t config;
29 uint32_t status;
30 uint32_t _reserved0;
31 uint32_t wdata0;
32 uint32_t wdata1;
33 uint32_t rdata0;
34 uint32_t rdata1;
35 uint8_t _reserved_empty_until_next_apid[SPMI_PMIC_ARB_CHANNEL_SIZE - 0x20];
36 };
37 check_member(qcom_spmi_regs, rdata1, 0x1c);
38 _Static_assert(sizeof(struct qcom_spmi_regs) == SPMI_PMIC_ARB_CHANNEL_SIZE,
39 "struct qcom_spmi_regs must be " STRINGIFY(SPMI_PMIC_ARB_CHANNEL_SIZE) " bytes per APID");
40
41 struct qcom_spmi {
42 struct qcom_spmi_regs *regs_per_apid; // indexed by APID
43 uint32_t *apid_map;
44 size_t num_apid;
45 };
46
47 struct qcom_spmi qcom_spmi = {
48 (void *) SPMI_PMIC_ARB_CHANNEL_BASE,
49 (void *) SPMI_PMIC_ARB_APID_MAP_BASE,
50 SPMI_PMIC_ARB_APID_COUNT
51 };
52
53 static struct qcom_spmi_regs *find_apid(uint32_t addr)
54 {
55 size_t i;
56
57 for (i = 0U; i < qcom_spmi.num_apid; i++) {
58 uint32_t reg = read32(&qcom_spmi.apid_map[i]);
59 if ((reg != 0U) && ((addr & PPID_MASK) == (reg & PPID_MASK)))
60 return &qcom_spmi.regs_per_apid[i];
61 }
62
63 return NULL;
64 }
65
66 static int wait_for_done(struct qcom_spmi_regs *regs)
67 {
68 struct stopwatch sw;
69
70 stopwatch_init_msecs_expire(&sw, ARB_COMMAND_TIMEOUT_MS);
71 while (!stopwatch_expired(&sw)) {
72 uint32_t status = read32(&regs->status);
73 if ((status & ARB_STATUS_DONE) != 0U) {
74 if ((status & ARB_STATUS_FAILURE) != 0U ||
75 (status & ARB_STATUS_DENIED) != 0U ||
76 (status & ARB_STATUS_DROPPED) != 0U)
77 return -(int)(status & 0xff);
78 return 0;
79 }
80 }
81 printk(BIOS_ERR, "ERROR: SPMI_ARB timeout!\n");
82 return ERROR_TIMEOUT;
83 }
84
85 static void arb_command(struct qcom_spmi_regs *regs, uint8_t opcode, uint32_t addr,
86 uint8_t bytes)
87 {
88 write32(&regs->cmd, (uint32_t)opcode << 27 |
89 (addr & 0xff) << 4 | (bytes - 1));
90 }
91
92 int spmi_read8(uint32_t addr)
93 {
94 struct qcom_spmi_regs *regs = find_apid(addr);
95
96 if (!regs)
97 return ERROR_APID_NOT_FOUND;
98
99 arb_command(regs, OPC_EXT_READL, addr, 1);
100
101 int ret = wait_for_done(regs);
102 if (ret != 0) {
103 printk(BIOS_ERR, "ERROR: SPMI_ARB read error [0x%x]: 0x%x\n", addr, ret);
104 return ret;
105 }
106
107 return read32(&regs->rdata0) & 0xff;
108 }
109
110 int spmi_write8(uint32_t addr, uint8_t data)
111 {
112 struct qcom_spmi_regs *regs = find_apid(addr);
113
114 if (!regs)
115 return ERROR_APID_NOT_FOUND;
116
117 write32(&regs->wdata0, data);
118 arb_command(regs, OPC_EXT_WRITEL, addr, 1);
119
120 int ret = wait_for_done(regs);
121 if (ret != 0) {
122 printk(BIOS_ERR, "ERROR: SPMI_ARB write error [0x%x] = 0x%x: 0x%x\n",
123 addr, data, ret);
124 }
125
126 return ret;
127 }