xref: /openbmc/qemu/hw/ppc/pnv_i2c.c (revision 7e9595e41502960b3827abbd3dac282d93b65693)
1263b81eeSCédric Le Goater /*
2263b81eeSCédric Le Goater  * QEMU PowerPC PowerNV Processor I2C model
3263b81eeSCédric Le Goater  *
4263b81eeSCédric Le Goater  * Copyright (c) 2019-2023, IBM Corporation.
5263b81eeSCédric Le Goater  *
6263b81eeSCédric Le Goater  * SPDX-License-Identifier: GPL-2.0-or-later
7263b81eeSCédric Le Goater  */
8263b81eeSCédric Le Goater 
9263b81eeSCédric Le Goater #include "qemu/osdep.h"
10263b81eeSCédric Le Goater #include "qemu/module.h"
11263b81eeSCédric Le Goater #include "qemu/log.h"
12263b81eeSCédric Le Goater #include "sysemu/reset.h"
13263b81eeSCédric Le Goater 
14263b81eeSCédric Le Goater #include "hw/irq.h"
15263b81eeSCédric Le Goater #include "hw/qdev-properties.h"
16263b81eeSCédric Le Goater 
17263b81eeSCédric Le Goater #include "hw/ppc/pnv.h"
18263b81eeSCédric Le Goater #include "hw/ppc/pnv_chip.h"
19263b81eeSCédric Le Goater #include "hw/ppc/pnv_i2c.h"
20263b81eeSCédric Le Goater #include "hw/ppc/pnv_xscom.h"
21263b81eeSCédric Le Goater #include "hw/ppc/fdt.h"
22263b81eeSCédric Le Goater 
23263b81eeSCédric Le Goater #include <libfdt.h>
24263b81eeSCédric Le Goater 
254d2cd2d8SGlenn Miles #include "hw/i2c/pnv_i2c_regs.h"
26263b81eeSCédric Le Goater 
pnv_i2c_get_bus(PnvI2C * i2c)27263b81eeSCédric Le Goater static I2CBus *pnv_i2c_get_bus(PnvI2C *i2c)
28263b81eeSCédric Le Goater {
29263b81eeSCédric Le Goater     uint8_t port = GETFIELD(I2C_MODE_PORT_NUM, i2c->regs[I2C_MODE_REG]);
30263b81eeSCédric Le Goater 
31263b81eeSCédric Le Goater     if (port >= i2c->num_busses) {
32263b81eeSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "I2C: invalid bus number %d/%d\n", port,
33263b81eeSCédric Le Goater                       i2c->num_busses);
34263b81eeSCédric Le Goater         return NULL;
35263b81eeSCédric Le Goater     }
36263b81eeSCédric Le Goater     return i2c->busses[port];
37263b81eeSCédric Le Goater }
38263b81eeSCédric Le Goater 
pnv_i2c_update_irq(PnvI2C * i2c)39263b81eeSCédric Le Goater static void pnv_i2c_update_irq(PnvI2C *i2c)
40263b81eeSCédric Le Goater {
41263b81eeSCédric Le Goater     I2CBus *bus = pnv_i2c_get_bus(i2c);
42263b81eeSCédric Le Goater     bool recv = !!(i2c->regs[I2C_CMD_REG] & I2C_CMD_READ_NOT_WRITE);
43263b81eeSCédric Le Goater     uint16_t front_end = GETFIELD(I2C_RESIDUAL_FRONT_END,
44263b81eeSCédric Le Goater                                   i2c->regs[I2C_RESIDUAL_LEN_REG]);
45263b81eeSCédric Le Goater     uint16_t back_end = GETFIELD(I2C_RESIDUAL_BACK_END,
46263b81eeSCédric Le Goater                                  i2c->regs[I2C_RESIDUAL_LEN_REG]);
47263b81eeSCédric Le Goater     uint8_t fifo_count = GETFIELD(I2C_STAT_FIFO_ENTRY_COUNT,
48263b81eeSCédric Le Goater                                    i2c->regs[I2C_STAT_REG]);
49263b81eeSCédric Le Goater     uint8_t fifo_free = PNV_I2C_FIFO_SIZE - fifo_count;
50263b81eeSCédric Le Goater 
51263b81eeSCédric Le Goater     if (!bus) {
52263b81eeSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "I2C: invalid port\n");
53263b81eeSCédric Le Goater         return;
54263b81eeSCédric Le Goater     }
55263b81eeSCédric Le Goater 
56263b81eeSCédric Le Goater     if (i2c_bus_busy(bus)) {
57263b81eeSCédric Le Goater         i2c->regs[I2C_STAT_REG] &= ~I2C_STAT_DATA_REQ;
58263b81eeSCédric Le Goater 
59263b81eeSCédric Le Goater         if (recv) {
60263b81eeSCédric Le Goater             if (fifo_count >=
61263b81eeSCédric Le Goater                 GETFIELD(I2C_WATERMARK_HIGH, i2c->regs[I2C_WATERMARK_REG])) {
62263b81eeSCédric Le Goater                 i2c->regs[I2C_EXTD_STAT_REG] |= I2C_EXTD_STAT_HIGH_WATER;
63263b81eeSCédric Le Goater             } else {
64263b81eeSCédric Le Goater                 i2c->regs[I2C_EXTD_STAT_REG] &= ~I2C_EXTD_STAT_HIGH_WATER;
65263b81eeSCédric Le Goater             }
66263b81eeSCédric Le Goater 
67263b81eeSCédric Le Goater             if (((i2c->regs[I2C_EXTD_STAT_REG] & I2C_EXTD_STAT_HIGH_WATER) &&
68263b81eeSCédric Le Goater                  fifo_count != 0) || front_end == 0) {
69263b81eeSCédric Le Goater                 i2c->regs[I2C_STAT_REG] |= I2C_STAT_DATA_REQ;
70263b81eeSCédric Le Goater             }
71263b81eeSCédric Le Goater         } else {
72263b81eeSCédric Le Goater             if (fifo_count <=
73263b81eeSCédric Le Goater                 GETFIELD(I2C_WATERMARK_LOW, i2c->regs[I2C_WATERMARK_REG])) {
74263b81eeSCédric Le Goater                 i2c->regs[I2C_EXTD_STAT_REG] |= I2C_EXTD_STAT_LOW_WATER;
75263b81eeSCédric Le Goater             } else {
76263b81eeSCédric Le Goater                 i2c->regs[I2C_EXTD_STAT_REG] &= ~I2C_EXTD_STAT_LOW_WATER;
77263b81eeSCédric Le Goater             }
78263b81eeSCédric Le Goater 
79263b81eeSCédric Le Goater             if (back_end > 0 &&
80263b81eeSCédric Le Goater                 (fifo_free >= back_end ||
81263b81eeSCédric Le Goater                  (i2c->regs[I2C_EXTD_STAT_REG] & I2C_EXTD_STAT_LOW_WATER))) {
82263b81eeSCédric Le Goater                 i2c->regs[I2C_STAT_REG] |= I2C_STAT_DATA_REQ;
83263b81eeSCédric Le Goater             }
84263b81eeSCédric Le Goater         }
85263b81eeSCédric Le Goater 
86263b81eeSCédric Le Goater         if (back_end == 0 && front_end == 0) {
87263b81eeSCédric Le Goater             i2c->regs[I2C_STAT_REG] &= ~I2C_STAT_DATA_REQ;
88263b81eeSCédric Le Goater             i2c->regs[I2C_STAT_REG] |= I2C_STAT_CMD_COMP;
89263b81eeSCédric Le Goater 
90263b81eeSCédric Le Goater             if (i2c->regs[I2C_CMD_REG] & I2C_CMD_WITH_STOP) {
91263b81eeSCédric Le Goater                 i2c_end_transfer(bus);
92263b81eeSCédric Le Goater                 i2c->regs[I2C_EXTD_STAT_REG] &=
93263b81eeSCédric Le Goater                     ~(I2C_EXTD_STAT_I2C_BUSY | I2C_EXTD_STAT_SELF_BUSY);
94263b81eeSCédric Le Goater             }
95263b81eeSCédric Le Goater         } else {
96263b81eeSCédric Le Goater             i2c->regs[I2C_STAT_REG] &= ~I2C_STAT_CMD_COMP;
97263b81eeSCédric Le Goater         }
98263b81eeSCédric Le Goater     }
99263b81eeSCédric Le Goater 
100263b81eeSCédric Le Goater     /*
101263b81eeSCédric Le Goater      * Status and interrupt registers have nearly the same layout.
102263b81eeSCédric Le Goater      */
103263b81eeSCédric Le Goater     i2c->regs[I2C_INTR_RAW_COND_REG] = i2c->regs[I2C_STAT_REG] >> 16;
104263b81eeSCédric Le Goater     i2c->regs[I2C_INTR_COND_REG] =
105263b81eeSCédric Le Goater         i2c->regs[I2C_INTR_RAW_COND_REG] & i2c->regs[I2C_INTR_MASK_REG];
106263b81eeSCédric Le Goater 
107263b81eeSCédric Le Goater     qemu_set_irq(i2c->psi_irq, i2c->regs[I2C_INTR_COND_REG] != 0);
108263b81eeSCédric Le Goater }
109263b81eeSCédric Le Goater 
pnv_i2c_fifo_update_count(PnvI2C * i2c)110263b81eeSCédric Le Goater static void pnv_i2c_fifo_update_count(PnvI2C *i2c)
111263b81eeSCédric Le Goater {
112263b81eeSCédric Le Goater     uint64_t stat = i2c->regs[I2C_STAT_REG];
113263b81eeSCédric Le Goater 
114263b81eeSCédric Le Goater     i2c->regs[I2C_STAT_REG] = SETFIELD(I2C_STAT_FIFO_ENTRY_COUNT, stat,
115263b81eeSCédric Le Goater                                        fifo8_num_used(&i2c->fifo));
116263b81eeSCédric Le Goater }
117263b81eeSCédric Le Goater 
pnv_i2c_frontend_update(PnvI2C * i2c)118263b81eeSCédric Le Goater static void pnv_i2c_frontend_update(PnvI2C *i2c)
119263b81eeSCédric Le Goater {
120263b81eeSCédric Le Goater     uint64_t residual_end = i2c->regs[I2C_RESIDUAL_LEN_REG];
121263b81eeSCédric Le Goater     uint16_t front_end = GETFIELD(I2C_RESIDUAL_FRONT_END, residual_end);
122263b81eeSCédric Le Goater 
123263b81eeSCédric Le Goater     i2c->regs[I2C_RESIDUAL_LEN_REG] =
124263b81eeSCédric Le Goater         SETFIELD(I2C_RESIDUAL_FRONT_END, residual_end, front_end - 1);
125263b81eeSCédric Le Goater }
126263b81eeSCédric Le Goater 
pnv_i2c_fifo_flush(PnvI2C * i2c)127263b81eeSCédric Le Goater static void pnv_i2c_fifo_flush(PnvI2C *i2c)
128263b81eeSCédric Le Goater {
129263b81eeSCédric Le Goater     I2CBus *bus = pnv_i2c_get_bus(i2c);
130263b81eeSCédric Le Goater     uint8_t data;
131263b81eeSCédric Le Goater     int ret;
132263b81eeSCédric Le Goater 
133263b81eeSCédric Le Goater     if (!bus) {
134263b81eeSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "I2C: invalid port\n");
135263b81eeSCédric Le Goater         return;
136263b81eeSCédric Le Goater     }
137263b81eeSCédric Le Goater     if (!i2c_bus_busy(bus)) {
138263b81eeSCédric Le Goater         return;
139263b81eeSCédric Le Goater     }
140263b81eeSCédric Le Goater 
141263b81eeSCédric Le Goater     if (i2c->regs[I2C_CMD_REG] & I2C_CMD_READ_NOT_WRITE) {
142263b81eeSCédric Le Goater         if (fifo8_is_full(&i2c->fifo)) {
143263b81eeSCédric Le Goater             return;
144263b81eeSCédric Le Goater         }
145263b81eeSCédric Le Goater 
146263b81eeSCédric Le Goater         data = i2c_recv(bus);
147263b81eeSCédric Le Goater         fifo8_push(&i2c->fifo, data);
148263b81eeSCédric Le Goater     } else {
149263b81eeSCédric Le Goater         if (fifo8_is_empty(&i2c->fifo)) {
150263b81eeSCédric Le Goater             return;
151263b81eeSCédric Le Goater         }
152263b81eeSCédric Le Goater 
153263b81eeSCédric Le Goater         data = fifo8_pop(&i2c->fifo);
154263b81eeSCédric Le Goater         ret = i2c_send(bus, data);
155263b81eeSCédric Le Goater         if (ret) {
156263b81eeSCédric Le Goater             i2c->regs[I2C_STAT_REG] |= I2C_STAT_NACK_RCVD_ERR;
157263b81eeSCédric Le Goater             i2c_end_transfer(bus);
158263b81eeSCédric Le Goater         }
159263b81eeSCédric Le Goater     }
160263b81eeSCédric Le Goater 
161263b81eeSCédric Le Goater     pnv_i2c_fifo_update_count(i2c);
162263b81eeSCédric Le Goater     pnv_i2c_frontend_update(i2c);
163263b81eeSCédric Le Goater }
164263b81eeSCédric Le Goater 
pnv_i2c_handle_cmd(PnvI2C * i2c,uint64_t val)165263b81eeSCédric Le Goater static void pnv_i2c_handle_cmd(PnvI2C *i2c, uint64_t val)
166263b81eeSCédric Le Goater {
167263b81eeSCédric Le Goater     I2CBus *bus = pnv_i2c_get_bus(i2c);
168263b81eeSCédric Le Goater     uint8_t addr = GETFIELD(I2C_CMD_DEV_ADDR, val);
169263b81eeSCédric Le Goater     int recv = !!(val & I2C_CMD_READ_NOT_WRITE);
170263b81eeSCédric Le Goater     uint32_t len_bytes = GETFIELD(I2C_CMD_LEN_BYTES, val);
171263b81eeSCédric Le Goater 
172263b81eeSCédric Le Goater     if (!(val & I2C_CMD_WITH_START) && !(val & I2C_CMD_WITH_ADDR) &&
173263b81eeSCédric Le Goater         !(val & I2C_CMD_WITH_STOP) && !len_bytes) {
174263b81eeSCédric Le Goater         i2c->regs[I2C_STAT_REG] |= I2C_STAT_INVALID_CMD;
175263b81eeSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "I2C: invalid command 0x%"PRIx64"\n",
176263b81eeSCédric Le Goater                       val);
177263b81eeSCédric Le Goater         return;
178263b81eeSCédric Le Goater     }
179263b81eeSCédric Le Goater 
180263b81eeSCédric Le Goater     if (!(i2c->regs[I2C_STAT_REG] & I2C_STAT_CMD_COMP)) {
181263b81eeSCédric Le Goater         i2c->regs[I2C_STAT_REG] |= I2C_STAT_INVALID_CMD;
182263b81eeSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "I2C: command in progress\n");
183263b81eeSCédric Le Goater         return;
184263b81eeSCédric Le Goater     }
185263b81eeSCédric Le Goater 
186263b81eeSCédric Le Goater     if (!bus) {
187263b81eeSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "I2C: invalid port\n");
188263b81eeSCédric Le Goater         return;
189263b81eeSCédric Le Goater     }
190263b81eeSCédric Le Goater 
191263b81eeSCédric Le Goater     i2c->regs[I2C_RESIDUAL_LEN_REG] =
192263b81eeSCédric Le Goater         SETFIELD(I2C_RESIDUAL_FRONT_END, 0ull, len_bytes) |
193263b81eeSCédric Le Goater         SETFIELD(I2C_RESIDUAL_BACK_END, 0ull, len_bytes);
194263b81eeSCédric Le Goater 
195263b81eeSCédric Le Goater     if (val & I2C_CMD_WITH_START) {
196263b81eeSCédric Le Goater         if (i2c_start_transfer(bus, addr, recv)) {
197263b81eeSCédric Le Goater             i2c->regs[I2C_STAT_REG] |= I2C_STAT_NACK_RCVD_ERR;
198263b81eeSCédric Le Goater         } else {
199263b81eeSCédric Le Goater             i2c->regs[I2C_EXTD_STAT_REG] |=
200263b81eeSCédric Le Goater                 (I2C_EXTD_STAT_I2C_BUSY | I2C_EXTD_STAT_SELF_BUSY);
201263b81eeSCédric Le Goater             pnv_i2c_fifo_flush(i2c);
202263b81eeSCédric Le Goater         }
203263b81eeSCédric Le Goater     }
204263b81eeSCédric Le Goater }
205263b81eeSCédric Le Goater 
pnv_i2c_backend_update(PnvI2C * i2c)206263b81eeSCédric Le Goater static void pnv_i2c_backend_update(PnvI2C *i2c)
207263b81eeSCédric Le Goater {
208263b81eeSCédric Le Goater     uint64_t residual_end = i2c->regs[I2C_RESIDUAL_LEN_REG];
209263b81eeSCédric Le Goater     uint16_t back_end = GETFIELD(I2C_RESIDUAL_BACK_END, residual_end);
210263b81eeSCédric Le Goater 
211263b81eeSCédric Le Goater     if (!back_end) {
212263b81eeSCédric Le Goater         i2c->regs[I2C_STAT_REG] |= I2C_STAT_BKEND_ACCESS_ERR;
213263b81eeSCédric Le Goater         return;
214263b81eeSCédric Le Goater     }
215263b81eeSCédric Le Goater 
216263b81eeSCédric Le Goater     i2c->regs[I2C_RESIDUAL_LEN_REG] =
217263b81eeSCédric Le Goater         SETFIELD(I2C_RESIDUAL_BACK_END, residual_end, back_end - 1);
218263b81eeSCédric Le Goater }
219263b81eeSCédric Le Goater 
pnv_i2c_fifo_in(PnvI2C * i2c)220263b81eeSCédric Le Goater static void pnv_i2c_fifo_in(PnvI2C *i2c)
221263b81eeSCédric Le Goater {
222263b81eeSCédric Le Goater     uint8_t data = GETFIELD(I2C_FIFO, i2c->regs[I2C_FIFO_REG]);
223263b81eeSCédric Le Goater     I2CBus *bus = pnv_i2c_get_bus(i2c);
224263b81eeSCédric Le Goater 
225263b81eeSCédric Le Goater     if (!bus) {
226263b81eeSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "I2C: invalid port\n");
227263b81eeSCédric Le Goater         return;
228263b81eeSCédric Le Goater     }
229263b81eeSCédric Le Goater 
230263b81eeSCédric Le Goater     if (!i2c_bus_busy(bus)) {
231263b81eeSCédric Le Goater         i2c->regs[I2C_STAT_REG] |= I2C_STAT_INVALID_CMD;
232263b81eeSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "I2C: no command in progress\n");
233263b81eeSCédric Le Goater         return;
234263b81eeSCédric Le Goater     }
235263b81eeSCédric Le Goater 
236263b81eeSCédric Le Goater     if (i2c->regs[I2C_CMD_REG] & I2C_CMD_READ_NOT_WRITE) {
237263b81eeSCédric Le Goater         i2c->regs[I2C_STAT_REG] |= I2C_STAT_INVALID_CMD;
238263b81eeSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "I2C: read command in progress\n");
239263b81eeSCédric Le Goater         return;
240263b81eeSCédric Le Goater     }
241263b81eeSCédric Le Goater 
242263b81eeSCédric Le Goater     if (fifo8_is_full(&i2c->fifo)) {
243263b81eeSCédric Le Goater         if (!(i2c->regs[I2C_MODE_REG] & I2C_MODE_PACING_ALLOW)) {
244263b81eeSCédric Le Goater             i2c->regs[I2C_STAT_REG] |= I2C_STAT_BKEND_OVERRUN_ERR;
245263b81eeSCédric Le Goater         }
246263b81eeSCédric Le Goater         return;
247263b81eeSCédric Le Goater     }
248263b81eeSCédric Le Goater 
249263b81eeSCédric Le Goater     fifo8_push(&i2c->fifo, data);
250263b81eeSCédric Le Goater     pnv_i2c_fifo_update_count(i2c);
251263b81eeSCédric Le Goater     pnv_i2c_backend_update(i2c);
252263b81eeSCédric Le Goater     pnv_i2c_fifo_flush(i2c);
253263b81eeSCédric Le Goater }
254263b81eeSCédric Le Goater 
pnv_i2c_fifo_out(PnvI2C * i2c)255263b81eeSCédric Le Goater static void pnv_i2c_fifo_out(PnvI2C *i2c)
256263b81eeSCédric Le Goater {
257263b81eeSCédric Le Goater     uint8_t data;
258263b81eeSCédric Le Goater     I2CBus *bus = pnv_i2c_get_bus(i2c);
259263b81eeSCédric Le Goater 
260263b81eeSCédric Le Goater     if (!bus) {
261263b81eeSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "I2C: invalid port\n");
262263b81eeSCédric Le Goater         return;
263263b81eeSCédric Le Goater     }
264263b81eeSCédric Le Goater 
265263b81eeSCédric Le Goater     if (!i2c_bus_busy(bus)) {
266263b81eeSCédric Le Goater         i2c->regs[I2C_STAT_REG] |= I2C_STAT_INVALID_CMD;
267263b81eeSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "I2C: no command in progress\n");
268263b81eeSCédric Le Goater         return;
269263b81eeSCédric Le Goater     }
270263b81eeSCédric Le Goater 
271263b81eeSCédric Le Goater     if (!(i2c->regs[I2C_CMD_REG] & I2C_CMD_READ_NOT_WRITE)) {
272263b81eeSCédric Le Goater         i2c->regs[I2C_STAT_REG] |= I2C_STAT_INVALID_CMD;
273263b81eeSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "I2C: write command in progress\n");
274263b81eeSCédric Le Goater         return;
275263b81eeSCédric Le Goater     }
276263b81eeSCédric Le Goater 
277263b81eeSCédric Le Goater     if (fifo8_is_empty(&i2c->fifo)) {
278263b81eeSCédric Le Goater         if (!(i2c->regs[I2C_MODE_REG] & I2C_MODE_PACING_ALLOW)) {
279263b81eeSCédric Le Goater             i2c->regs[I2C_STAT_REG] |= I2C_STAT_BKEND_OVERRUN_ERR;
280263b81eeSCédric Le Goater         }
281263b81eeSCédric Le Goater         return;
282263b81eeSCédric Le Goater     }
283263b81eeSCédric Le Goater 
284263b81eeSCédric Le Goater     data = fifo8_pop(&i2c->fifo);
285263b81eeSCédric Le Goater 
286263b81eeSCédric Le Goater     i2c->regs[I2C_FIFO_REG] = SETFIELD(I2C_FIFO, 0ull, data);
287263b81eeSCédric Le Goater     pnv_i2c_fifo_update_count(i2c);
288263b81eeSCédric Le Goater     pnv_i2c_backend_update(i2c);
289263b81eeSCédric Le Goater }
290263b81eeSCédric Le Goater 
pnv_i2c_xscom_read(void * opaque,hwaddr addr,unsigned size)291263b81eeSCédric Le Goater static uint64_t pnv_i2c_xscom_read(void *opaque, hwaddr addr,
292263b81eeSCédric Le Goater                                    unsigned size)
293263b81eeSCédric Le Goater {
294263b81eeSCédric Le Goater     PnvI2C *i2c = PNV_I2C(opaque);
295263b81eeSCédric Le Goater     uint32_t offset = addr >> 3;
296263b81eeSCédric Le Goater     uint64_t val = -1;
297263b81eeSCédric Le Goater     int i;
298263b81eeSCédric Le Goater 
299263b81eeSCédric Le Goater     switch (offset) {
300263b81eeSCédric Le Goater     case I2C_STAT_REG:
301263b81eeSCédric Le Goater         val = i2c->regs[offset];
302263b81eeSCédric Le Goater         break;
303263b81eeSCédric Le Goater 
304263b81eeSCédric Le Goater     case I2C_FIFO_REG:
305263b81eeSCédric Le Goater         pnv_i2c_fifo_out(i2c);
306263b81eeSCédric Le Goater         val = i2c->regs[offset];
307263b81eeSCédric Le Goater         break;
308263b81eeSCédric Le Goater 
309263b81eeSCédric Le Goater     case I2C_PORT_BUSY_REG: /* compute busy bit for each port  */
310263b81eeSCédric Le Goater         val = 0;
311263b81eeSCédric Le Goater         for (i = 0; i < i2c->num_busses; i++) {
3128bc5ae04SCédric Le Goater             val |= (uint64_t)i2c_bus_busy(i2c->busses[i]) << i;
313263b81eeSCédric Le Goater         }
314263b81eeSCédric Le Goater         break;
315263b81eeSCédric Le Goater 
316263b81eeSCédric Le Goater     case I2C_CMD_REG:
317263b81eeSCédric Le Goater     case I2C_MODE_REG:
318263b81eeSCédric Le Goater     case I2C_WATERMARK_REG:
319263b81eeSCédric Le Goater     case I2C_INTR_MASK_REG:
320263b81eeSCédric Le Goater     case I2C_INTR_RAW_COND_REG:
321263b81eeSCédric Le Goater     case I2C_INTR_COND_REG:
322263b81eeSCédric Le Goater     case I2C_EXTD_STAT_REG:
323263b81eeSCédric Le Goater     case I2C_RESIDUAL_LEN_REG:
324263b81eeSCédric Le Goater         val = i2c->regs[offset];
325263b81eeSCédric Le Goater         break;
326263b81eeSCédric Le Goater     default:
327263b81eeSCédric Le Goater         i2c->regs[I2C_STAT_REG] |= I2C_STAT_INVALID_CMD;
328263b81eeSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "I2C: read at register: 0x%"
329263b81eeSCédric Le Goater                       HWADDR_PRIx "\n", addr >> 3);
330263b81eeSCédric Le Goater     }
331263b81eeSCédric Le Goater 
332263b81eeSCédric Le Goater     pnv_i2c_update_irq(i2c);
333263b81eeSCédric Le Goater 
334263b81eeSCédric Le Goater     return val;
335263b81eeSCédric Le Goater }
336263b81eeSCédric Le Goater 
pnv_i2c_reset(void * dev)337b664466dSGlenn Miles static void pnv_i2c_reset(void *dev)
338b664466dSGlenn Miles {
339b664466dSGlenn Miles     PnvI2C *i2c = PNV_I2C(dev);
340b664466dSGlenn Miles 
341b664466dSGlenn Miles     memset(i2c->regs, 0, sizeof(i2c->regs));
342b664466dSGlenn Miles 
343b664466dSGlenn Miles     i2c->regs[I2C_STAT_REG] =
344b664466dSGlenn Miles         SETFIELD(I2C_STAT_UPPER_THRS, 0ull, i2c->num_busses - 1) |
345b664466dSGlenn Miles         I2C_STAT_CMD_COMP | I2C_STAT_SCL_INPUT_LEVEL |
346b664466dSGlenn Miles         I2C_STAT_SDA_INPUT_LEVEL;
347b664466dSGlenn Miles     i2c->regs[I2C_EXTD_STAT_REG] =
348b664466dSGlenn Miles         SETFIELD(I2C_EXTD_STAT_FIFO_SIZE, 0ull, PNV_I2C_FIFO_SIZE) |
349b664466dSGlenn Miles         SETFIELD(I2C_EXTD_STAT_I2C_VERSION, 0ull, 23); /* last version */
350b664466dSGlenn Miles 
351b664466dSGlenn Miles     fifo8_reset(&i2c->fifo);
352b664466dSGlenn Miles }
353b664466dSGlenn Miles 
pnv_i2c_xscom_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)354263b81eeSCédric Le Goater static void pnv_i2c_xscom_write(void *opaque, hwaddr addr,
355263b81eeSCédric Le Goater                                 uint64_t val, unsigned size)
356263b81eeSCédric Le Goater {
357263b81eeSCédric Le Goater     PnvI2C *i2c = PNV_I2C(opaque);
358263b81eeSCédric Le Goater     uint32_t offset = addr >> 3;
359263b81eeSCédric Le Goater 
360263b81eeSCédric Le Goater     switch (offset) {
361263b81eeSCédric Le Goater     case I2C_MODE_REG:
362263b81eeSCédric Le Goater         {
363263b81eeSCédric Le Goater             i2c->regs[offset] = val;
364263b81eeSCédric Le Goater             I2CBus *bus = pnv_i2c_get_bus(i2c);
365263b81eeSCédric Le Goater             if (!bus) {
366263b81eeSCédric Le Goater                 qemu_log_mask(LOG_GUEST_ERROR, "I2C: invalid port\n");
367263b81eeSCédric Le Goater                 return;
368263b81eeSCédric Le Goater             }
369263b81eeSCédric Le Goater             if (i2c_bus_busy(bus)) {
370263b81eeSCédric Le Goater                 i2c->regs[I2C_STAT_REG] |= I2C_STAT_INVALID_CMD;
371263b81eeSCédric Le Goater                 qemu_log_mask(LOG_GUEST_ERROR, "I2C: command in progress\n");
372263b81eeSCédric Le Goater             }
373263b81eeSCédric Le Goater         }
374263b81eeSCédric Le Goater         break;
375263b81eeSCédric Le Goater 
376263b81eeSCédric Le Goater     case I2C_CMD_REG:
377263b81eeSCédric Le Goater         i2c->regs[offset] = val;
378263b81eeSCédric Le Goater         pnv_i2c_handle_cmd(i2c, val);
379263b81eeSCédric Le Goater         break;
380263b81eeSCédric Le Goater 
381263b81eeSCédric Le Goater     case I2C_FIFO_REG:
382263b81eeSCédric Le Goater         i2c->regs[offset] = val;
383263b81eeSCédric Le Goater         pnv_i2c_fifo_in(i2c);
384263b81eeSCédric Le Goater         break;
385263b81eeSCédric Le Goater 
386263b81eeSCédric Le Goater     case I2C_WATERMARK_REG:
387263b81eeSCédric Le Goater         i2c->regs[offset] = val;
388263b81eeSCédric Le Goater         break;
389263b81eeSCédric Le Goater 
390263b81eeSCédric Le Goater     case I2C_RESET_I2C_REG:
391b664466dSGlenn Miles         pnv_i2c_reset(i2c);
392263b81eeSCédric Le Goater         break;
393263b81eeSCédric Le Goater 
394263b81eeSCédric Le Goater     case I2C_RESET_ERRORS:
395263b81eeSCédric Le Goater         i2c->regs[I2C_STAT_REG] &= ~I2C_STAT_ANY_ERR;
396263b81eeSCédric Le Goater         i2c->regs[I2C_RESIDUAL_LEN_REG] = 0;
397263b81eeSCédric Le Goater         i2c->regs[I2C_EXTD_STAT_REG] &=
398263b81eeSCédric Le Goater             (I2C_EXTD_STAT_FIFO_SIZE | I2C_EXTD_STAT_I2C_VERSION);
399263b81eeSCédric Le Goater         fifo8_reset(&i2c->fifo);
400263b81eeSCédric Le Goater         break;
401263b81eeSCédric Le Goater 
402263b81eeSCédric Le Goater     case I2C_INTR_MASK_REG:
403263b81eeSCédric Le Goater         i2c->regs[offset] = val;
404263b81eeSCédric Le Goater         break;
405263b81eeSCédric Le Goater 
406263b81eeSCédric Le Goater     case I2C_INTR_MASK_OR_REG:
407263b81eeSCédric Le Goater         i2c->regs[I2C_INTR_MASK_REG] |= val;
408263b81eeSCédric Le Goater         break;
409263b81eeSCédric Le Goater 
410263b81eeSCédric Le Goater     case I2C_INTR_MASK_AND_REG:
411263b81eeSCédric Le Goater         i2c->regs[I2C_INTR_MASK_REG] &= val;
412263b81eeSCédric Le Goater         break;
413263b81eeSCédric Le Goater 
414263b81eeSCédric Le Goater     case I2C_PORT_BUSY_REG:
415263b81eeSCédric Le Goater     case I2C_SET_S_SCL_REG:
416263b81eeSCédric Le Goater     case I2C_RESET_S_SCL_REG:
417263b81eeSCédric Le Goater     case I2C_SET_S_SDA_REG:
418263b81eeSCédric Le Goater     case I2C_RESET_S_SDA_REG:
419263b81eeSCédric Le Goater         i2c->regs[offset] = val;
420263b81eeSCédric Le Goater         break;
421263b81eeSCédric Le Goater     default:
422263b81eeSCédric Le Goater         i2c->regs[I2C_STAT_REG] |= I2C_STAT_INVALID_CMD;
423263b81eeSCédric Le Goater         qemu_log_mask(LOG_GUEST_ERROR, "I2C: write at register: 0x%"
424263b81eeSCédric Le Goater                       HWADDR_PRIx " val=0x%"PRIx64"\n", addr >> 3, val);
425263b81eeSCédric Le Goater     }
426263b81eeSCédric Le Goater 
427263b81eeSCédric Le Goater     pnv_i2c_update_irq(i2c);
428263b81eeSCédric Le Goater }
429263b81eeSCédric Le Goater 
430263b81eeSCédric Le Goater static const MemoryRegionOps pnv_i2c_xscom_ops = {
431263b81eeSCédric Le Goater     .read = pnv_i2c_xscom_read,
432263b81eeSCédric Le Goater     .write = pnv_i2c_xscom_write,
433263b81eeSCédric Le Goater     .valid.min_access_size = 8,
434263b81eeSCédric Le Goater     .valid.max_access_size = 8,
435263b81eeSCédric Le Goater     .impl.min_access_size = 8,
436263b81eeSCédric Le Goater     .impl.max_access_size = 8,
437263b81eeSCédric Le Goater     .endianness = DEVICE_BIG_ENDIAN,
438263b81eeSCédric Le Goater };
439263b81eeSCédric Le Goater 
pnv_i2c_bus_dt_xscom(PnvI2C * i2c,void * fdt,int offset,int index)440263b81eeSCédric Le Goater static int pnv_i2c_bus_dt_xscom(PnvI2C *i2c, void *fdt,
441263b81eeSCédric Le Goater                                 int offset, int index)
442263b81eeSCédric Le Goater {
443263b81eeSCédric Le Goater     int i2c_bus_offset;
444263b81eeSCédric Le Goater     const char i2c_compat[] =
445263b81eeSCédric Le Goater         "ibm,opal-i2c\0ibm,power8-i2c-port\0ibm,power9-i2c-port";
446263b81eeSCédric Le Goater     g_autofree char *i2c_port_name = NULL;
447263b81eeSCédric Le Goater     g_autofree char *name = g_strdup_printf("i2c-bus@%x", index);
448263b81eeSCédric Le Goater 
449263b81eeSCédric Le Goater     i2c_bus_offset = fdt_add_subnode(fdt, offset, name);
450263b81eeSCédric Le Goater     _FDT(i2c_bus_offset);
451263b81eeSCédric Le Goater 
452263b81eeSCédric Le Goater     _FDT((fdt_setprop_cell(fdt, i2c_bus_offset, "reg", index)));
453263b81eeSCédric Le Goater     _FDT((fdt_setprop_cell(fdt, i2c_bus_offset, "#address-cells", 1)));
454263b81eeSCédric Le Goater     _FDT((fdt_setprop_cell(fdt, i2c_bus_offset, "#size-cells", 0)));
455263b81eeSCédric Le Goater     _FDT(fdt_setprop(fdt, i2c_bus_offset, "compatible", i2c_compat,
456263b81eeSCédric Le Goater                      sizeof(i2c_compat)));
457263b81eeSCédric Le Goater     _FDT((fdt_setprop_cell(fdt, i2c_bus_offset, "bus-frequency", 400000)));
458263b81eeSCédric Le Goater 
459263b81eeSCédric Le Goater     i2c_port_name = g_strdup_printf("p8_%08x_e%dp%d", i2c->chip->chip_id,
460263b81eeSCédric Le Goater                                     i2c->engine, index);
461263b81eeSCédric Le Goater     _FDT(fdt_setprop_string(fdt, i2c_bus_offset, "ibm,port-name",
462263b81eeSCédric Le Goater                             i2c_port_name));
463263b81eeSCédric Le Goater     return 0;
464263b81eeSCédric Le Goater }
465263b81eeSCédric Le Goater 
466263b81eeSCédric Le Goater #define XSCOM_BUS_FREQUENCY 466500000
467263b81eeSCédric Le Goater #define I2C_CLOCK_FREQUENCY (XSCOM_BUS_FREQUENCY / 4)
468263b81eeSCédric Le Goater 
pnv_i2c_dt_xscom(PnvXScomInterface * dev,void * fdt,int offset)469263b81eeSCédric Le Goater static int pnv_i2c_dt_xscom(PnvXScomInterface *dev, void *fdt,
470263b81eeSCédric Le Goater                             int offset)
471263b81eeSCédric Le Goater {
472263b81eeSCédric Le Goater     PnvI2C *i2c = PNV_I2C(dev);
473263b81eeSCédric Le Goater     int i2c_offset;
474263b81eeSCédric Le Goater     const char i2c_compat[] = "ibm,power8-i2cm\0ibm,power9-i2cm";
475263b81eeSCédric Le Goater     uint32_t i2c_pcba = PNV9_XSCOM_I2CM_BASE +
47647dfdd23SGlenn Miles         (i2c->engine - 1) * PNV9_XSCOM_I2CM_SIZE;
477263b81eeSCédric Le Goater     uint32_t reg[2] = {
478263b81eeSCédric Le Goater         cpu_to_be32(i2c_pcba),
479263b81eeSCédric Le Goater         cpu_to_be32(PNV9_XSCOM_I2CM_SIZE)
480263b81eeSCédric Le Goater     };
481263b81eeSCédric Le Goater     int i;
482263b81eeSCédric Le Goater     g_autofree char *name = g_strdup_printf("i2cm@%x", i2c_pcba);
483263b81eeSCédric Le Goater 
484263b81eeSCédric Le Goater     i2c_offset = fdt_add_subnode(fdt, offset, name);
485263b81eeSCédric Le Goater     _FDT(i2c_offset);
486263b81eeSCédric Le Goater 
487263b81eeSCédric Le Goater     _FDT(fdt_setprop(fdt, i2c_offset, "reg", reg, sizeof(reg)));
488263b81eeSCédric Le Goater 
489263b81eeSCédric Le Goater     _FDT((fdt_setprop_cell(fdt, i2c_offset, "#address-cells", 1)));
490263b81eeSCédric Le Goater     _FDT((fdt_setprop_cell(fdt, i2c_offset, "#size-cells", 0)));
491263b81eeSCédric Le Goater     _FDT(fdt_setprop(fdt, i2c_offset, "compatible", i2c_compat,
492263b81eeSCédric Le Goater                      sizeof(i2c_compat)));
493263b81eeSCédric Le Goater     _FDT((fdt_setprop_cell(fdt, i2c_offset, "chip-engine#", i2c->engine)));
494263b81eeSCédric Le Goater     _FDT((fdt_setprop_cell(fdt, i2c_offset, "clock-frequency",
495263b81eeSCédric Le Goater                            I2C_CLOCK_FREQUENCY)));
496263b81eeSCédric Le Goater 
497263b81eeSCédric Le Goater     for (i = 0; i < i2c->num_busses; i++) {
498263b81eeSCédric Le Goater         pnv_i2c_bus_dt_xscom(i2c, fdt, i2c_offset, i);
499263b81eeSCédric Le Goater     }
500263b81eeSCédric Le Goater     return 0;
501263b81eeSCédric Le Goater }
502263b81eeSCédric Le Goater 
pnv_i2c_sys_reset(void * dev)5037b85f008SGlenn Miles static void pnv_i2c_sys_reset(void *dev)
5047b85f008SGlenn Miles {
5057b85f008SGlenn Miles     int port;
5067b85f008SGlenn Miles     PnvI2C *i2c = PNV_I2C(dev);
5077b85f008SGlenn Miles 
5087b85f008SGlenn Miles     pnv_i2c_reset(dev);
5097b85f008SGlenn Miles 
5107b85f008SGlenn Miles     /* reset all buses connected to this i2c controller */
5117b85f008SGlenn Miles     for (port = 0; port < i2c->num_busses; port++) {
5127b85f008SGlenn Miles         bus_cold_reset(BUS(i2c->busses[port]));
5137b85f008SGlenn Miles     }
5147b85f008SGlenn Miles }
5157b85f008SGlenn Miles 
pnv_i2c_realize(DeviceState * dev,Error ** errp)516263b81eeSCédric Le Goater static void pnv_i2c_realize(DeviceState *dev, Error **errp)
517263b81eeSCédric Le Goater {
518263b81eeSCédric Le Goater     PnvI2C *i2c = PNV_I2C(dev);
519263b81eeSCédric Le Goater     int i;
520263b81eeSCédric Le Goater 
521263b81eeSCédric Le Goater     assert(i2c->chip);
522263b81eeSCédric Le Goater 
5238bc5ae04SCédric Le Goater     if (i2c->num_busses > PNV_I2C_MAX_BUSSES) {
5248bc5ae04SCédric Le Goater         error_setg(errp, "Invalid number of busses: %u", i2c->num_busses);
5258bc5ae04SCédric Le Goater         return;
5268bc5ae04SCédric Le Goater     }
5278bc5ae04SCédric Le Goater 
528263b81eeSCédric Le Goater     pnv_xscom_region_init(&i2c->xscom_regs, OBJECT(i2c), &pnv_i2c_xscom_ops,
529263b81eeSCédric Le Goater                           i2c, "xscom-i2c", PNV9_XSCOM_I2CM_SIZE);
530263b81eeSCédric Le Goater 
531263b81eeSCédric Le Goater     i2c->busses = g_new(I2CBus *, i2c->num_busses);
532263b81eeSCédric Le Goater     for (i = 0; i < i2c->num_busses; i++) {
533263b81eeSCédric Le Goater         char name[32];
534263b81eeSCédric Le Goater 
535263b81eeSCédric Le Goater         snprintf(name, sizeof(name), TYPE_PNV_I2C ".%d", i);
536263b81eeSCédric Le Goater         i2c->busses[i] = i2c_init_bus(dev, name);
537263b81eeSCédric Le Goater     }
538263b81eeSCédric Le Goater 
539263b81eeSCédric Le Goater     fifo8_create(&i2c->fifo, PNV_I2C_FIFO_SIZE);
540263b81eeSCédric Le Goater 
5417b85f008SGlenn Miles     qemu_register_reset(pnv_i2c_sys_reset, dev);
542263b81eeSCédric Le Goater 
543263b81eeSCédric Le Goater     qdev_init_gpio_out(DEVICE(dev), &i2c->psi_irq, 1);
544263b81eeSCédric Le Goater }
545263b81eeSCédric Le Goater 
546263b81eeSCédric Le Goater static Property pnv_i2c_properties[] = {
547263b81eeSCédric Le Goater     DEFINE_PROP_LINK("chip", PnvI2C, chip, TYPE_PNV_CHIP, PnvChip *),
548263b81eeSCédric Le Goater     DEFINE_PROP_UINT32("engine", PnvI2C, engine, 1),
549263b81eeSCédric Le Goater     DEFINE_PROP_UINT32("num-busses", PnvI2C, num_busses, 1),
550263b81eeSCédric Le Goater     DEFINE_PROP_END_OF_LIST(),
551263b81eeSCédric Le Goater };
552263b81eeSCédric Le Goater 
pnv_i2c_class_init(ObjectClass * klass,void * data)553263b81eeSCédric Le Goater static void pnv_i2c_class_init(ObjectClass *klass, void *data)
554263b81eeSCédric Le Goater {
555263b81eeSCédric Le Goater     DeviceClass *dc = DEVICE_CLASS(klass);
556263b81eeSCédric Le Goater     PnvXScomInterfaceClass *xscomc = PNV_XSCOM_INTERFACE_CLASS(klass);
557263b81eeSCédric Le Goater 
558263b81eeSCédric Le Goater     xscomc->dt_xscom = pnv_i2c_dt_xscom;
559263b81eeSCédric Le Goater 
560*5b2b9450SCédric Le Goater     /* Reason: This device is part of the CPU and cannot be used separately */
561*5b2b9450SCédric Le Goater     dc->user_creatable = false;
562*5b2b9450SCédric Le Goater 
563263b81eeSCédric Le Goater     dc->desc = "PowerNV I2C";
564263b81eeSCédric Le Goater     dc->realize = pnv_i2c_realize;
565263b81eeSCédric Le Goater     device_class_set_props(dc, pnv_i2c_properties);
566263b81eeSCédric Le Goater }
567263b81eeSCédric Le Goater 
568263b81eeSCédric Le Goater static const TypeInfo pnv_i2c_info = {
569263b81eeSCédric Le Goater     .name          = TYPE_PNV_I2C,
570263b81eeSCédric Le Goater     .parent        = TYPE_DEVICE,
571263b81eeSCédric Le Goater     .instance_size = sizeof(PnvI2C),
572263b81eeSCédric Le Goater     .class_init    = pnv_i2c_class_init,
573263b81eeSCédric Le Goater     .interfaces    = (InterfaceInfo[]) {
574263b81eeSCédric Le Goater         { TYPE_PNV_XSCOM_INTERFACE },
575263b81eeSCédric Le Goater         { }
576263b81eeSCédric Le Goater     }
577263b81eeSCédric Le Goater };
578263b81eeSCédric Le Goater 
pnv_i2c_register_types(void)579263b81eeSCédric Le Goater static void pnv_i2c_register_types(void)
580263b81eeSCédric Le Goater {
581263b81eeSCédric Le Goater     type_register_static(&pnv_i2c_info);
582263b81eeSCédric Le Goater }
583263b81eeSCédric Le Goater 
584263b81eeSCédric Le Goater type_init(pnv_i2c_register_types);
585