xref: /openbmc/qemu/hw/i2c/aspeed_i2c.c (revision b03ec4ff0621cc981238ca8531cd102eebd4da89)
1 /*
2  * ARM Aspeed I2C controller
3  *
4  * Copyright (C) 2016 IBM Corp.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #include "qemu/osdep.h"
22 #include "hw/sysbus.h"
23 #include "migration/vmstate.h"
24 #include "qemu/cutils.h"
25 #include "qemu/log.h"
26 #include "qemu/module.h"
27 #include "qemu/error-report.h"
28 #include "qapi/error.h"
29 #include "hw/i2c/aspeed_i2c.h"
30 #include "hw/irq.h"
31 #include "hw/qdev-properties.h"
32 #include "hw/registerfields.h"
33 #include "trace.h"
34 
35 static inline void aspeed_i2c_bus_raise_interrupt(AspeedI2CBus *bus)
36 {
37     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
38     uint32_t reg_intr_sts = aspeed_i2c_bus_intr_sts_offset(bus);
39     uint32_t intr_ctrl_reg = aspeed_i2c_bus_intr_ctrl_offset(bus);
40     bool raise_irq;
41 
42     if (trace_event_get_state_backends(TRACE_ASPEED_I2C_BUS_RAISE_INTERRUPT)) {
43         g_autofree char *buf = g_strdup_printf("%s%s%s%s%s%s",
44                aspeed_i2c_bus_pkt_mode_en(bus) &&
45                ARRAY_FIELD_EX32(bus->regs, I2CM_INTR_STS, PKT_CMD_DONE) ?
46                                                "pktdone|" : "",
47                SHARED_ARRAY_FIELD_EX32(bus->regs, reg_intr_sts, TX_NAK) ?
48                                                "nak|" : "",
49                SHARED_ARRAY_FIELD_EX32(bus->regs, reg_intr_sts, TX_ACK) ?
50                                                "ack|" : "",
51                SHARED_ARRAY_FIELD_EX32(bus->regs, reg_intr_sts, RX_DONE) ?
52                                                "done|" : "",
53                SHARED_ARRAY_FIELD_EX32(bus->regs, reg_intr_sts, NORMAL_STOP) ?
54                                                "normal|" : "",
55                SHARED_ARRAY_FIELD_EX32(bus->regs, reg_intr_sts, ABNORMAL) ?
56                                                "abnormal"  : "");
57 
58            trace_aspeed_i2c_bus_raise_interrupt(bus->regs[reg_intr_sts], buf);
59     }
60 
61     raise_irq = bus->regs[reg_intr_sts] & bus->regs[intr_ctrl_reg];
62 
63     /* In packet mode we don't mask off INTR_STS */
64     if (!aspeed_i2c_bus_pkt_mode_en(bus)) {
65         bus->regs[reg_intr_sts] &= bus->regs[intr_ctrl_reg];
66     }
67 
68     if (raise_irq) {
69         bus->controller->intr_status |= 1 << bus->id;
70         qemu_irq_raise(aic->bus_get_irq(bus));
71     }
72 }
73 
74 static uint64_t aspeed_i2c_bus_old_read(AspeedI2CBus *bus, hwaddr offset,
75                                         unsigned size)
76 {
77     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
78     uint64_t value = bus->regs[offset / sizeof(*bus->regs)];
79 
80     switch (offset) {
81     case A_I2CD_FUN_CTRL:
82     case A_I2CD_AC_TIMING1:
83     case A_I2CD_AC_TIMING2:
84     case A_I2CD_INTR_CTRL:
85     case A_I2CD_INTR_STS:
86     case A_I2CD_POOL_CTRL:
87     case A_I2CD_BYTE_BUF:
88         /* Value is already set, don't do anything. */
89         break;
90     case A_I2CD_CMD:
91         value = SHARED_FIELD_DP32(value, BUS_BUSY_STS, i2c_bus_busy(bus->bus));
92         break;
93     case A_I2CD_DMA_ADDR:
94         if (!aic->has_dma) {
95             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
96             value = -1;
97         }
98         break;
99     case A_I2CD_DMA_LEN:
100         if (!aic->has_dma) {
101             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
102             value = -1;
103         }
104         break;
105 
106     default:
107         qemu_log_mask(LOG_GUEST_ERROR,
108                       "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, offset);
109         value = -1;
110         break;
111     }
112 
113     trace_aspeed_i2c_bus_read(bus->id, offset, size, value);
114     return value;
115 }
116 
117 static uint64_t aspeed_i2c_bus_new_read(AspeedI2CBus *bus, hwaddr offset,
118                                         unsigned size)
119 {
120     uint64_t value = bus->regs[offset / sizeof(*bus->regs)];
121 
122     switch (offset) {
123     case A_I2CC_FUN_CTRL:
124     case A_I2CC_AC_TIMING:
125     case A_I2CC_POOL_CTRL:
126     case A_I2CM_INTR_CTRL:
127     case A_I2CM_INTR_STS:
128     case A_I2CC_MS_TXRX_BYTE_BUF:
129     case A_I2CM_DMA_LEN:
130     case A_I2CM_DMA_TX_ADDR:
131     case A_I2CM_DMA_RX_ADDR:
132     case A_I2CM_DMA_LEN_STS:
133     case A_I2CC_DMA_ADDR:
134     case A_I2CC_DMA_LEN:
135         /* Value is already set, don't do anything. */
136         break;
137     case A_I2CM_CMD:
138         value = SHARED_FIELD_DP32(value, BUS_BUSY_STS, i2c_bus_busy(bus->bus));
139         break;
140     default:
141         qemu_log_mask(LOG_GUEST_ERROR,
142                       "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, offset);
143         value = -1;
144         break;
145     }
146 
147     trace_aspeed_i2c_bus_read(bus->id, offset, size, value);
148     return value;
149 }
150 
151 static uint64_t aspeed_i2c_bus_read(void *opaque, hwaddr offset,
152                                     unsigned size)
153 {
154     AspeedI2CBus *bus = opaque;
155     if (aspeed_i2c_is_new_mode(bus->controller)) {
156         return aspeed_i2c_bus_new_read(bus, offset, size);
157     }
158     return aspeed_i2c_bus_old_read(bus, offset, size);
159 }
160 
161 static void aspeed_i2c_set_state(AspeedI2CBus *bus, uint8_t state)
162 {
163     if (aspeed_i2c_is_new_mode(bus->controller)) {
164         SHARED_ARRAY_FIELD_DP32(bus->regs, R_I2CC_MS_TXRX_BYTE_BUF, TX_STATE,
165                                 state);
166     } else {
167         SHARED_ARRAY_FIELD_DP32(bus->regs, R_I2CD_CMD, TX_STATE, state);
168     }
169 }
170 
171 static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus)
172 {
173     if (aspeed_i2c_is_new_mode(bus->controller)) {
174         return SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CC_MS_TXRX_BYTE_BUF,
175                                        TX_STATE);
176     }
177     return SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CD_CMD, TX_STATE);
178 }
179 
180 static int aspeed_i2c_dma_read(AspeedI2CBus *bus, uint8_t *data)
181 {
182     MemTxResult result;
183     AspeedI2CState *s = bus->controller;
184     uint32_t reg_dma_addr = aspeed_i2c_bus_dma_addr_offset(bus);
185     uint32_t reg_dma_len = aspeed_i2c_bus_dma_len_offset(bus);
186 
187     result = address_space_read(&s->dram_as, bus->regs[reg_dma_addr],
188                                 MEMTXATTRS_UNSPECIFIED, data, 1);
189     if (result != MEMTX_OK) {
190         qemu_log_mask(LOG_GUEST_ERROR, "%s: DRAM read failed @%08x\n",
191                       __func__, bus->regs[reg_dma_addr]);
192         return -1;
193     }
194 
195     bus->regs[reg_dma_addr]++;
196     bus->regs[reg_dma_len]--;
197     return 0;
198 }
199 
200 static int aspeed_i2c_bus_send(AspeedI2CBus *bus, uint8_t pool_start)
201 {
202     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
203     int ret = -1;
204     int i;
205     uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
206     uint32_t reg_pool_ctrl = aspeed_i2c_bus_pool_ctrl_offset(bus);
207     uint32_t reg_byte_buf = aspeed_i2c_bus_byte_buf_offset(bus);
208     uint32_t reg_dma_len = aspeed_i2c_bus_dma_len_offset(bus);
209     int pool_tx_count = SHARED_ARRAY_FIELD_EX32(bus->regs, reg_pool_ctrl,
210                                                 TX_COUNT);
211 
212     if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_BUFF_EN)) {
213         for (i = pool_start; i < pool_tx_count; i++) {
214             uint8_t *pool_base = aic->bus_pool_base(bus);
215 
216             trace_aspeed_i2c_bus_send("BUF", i + 1, pool_tx_count,
217                                       pool_base[i]);
218             ret = i2c_send(bus->bus, pool_base[i]);
219             if (ret) {
220                 break;
221             }
222         }
223         SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, TX_BUFF_EN, 0);
224     } else if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_DMA_EN)) {
225         /* In new mode, clear how many bytes we TXed */
226         if (aspeed_i2c_is_new_mode(bus->controller)) {
227             ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN_STS, TX_LEN, 0);
228         }
229         while (bus->regs[reg_dma_len]) {
230             uint8_t data;
231             aspeed_i2c_dma_read(bus, &data);
232             trace_aspeed_i2c_bus_send("DMA", bus->regs[reg_dma_len],
233                                       bus->regs[reg_dma_len], data);
234             ret = i2c_send(bus->bus, data);
235             if (ret) {
236                 break;
237             }
238             /* In new mode, keep track of how many bytes we TXed */
239             if (aspeed_i2c_is_new_mode(bus->controller)) {
240                 ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN_STS, TX_LEN,
241                                  ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN_STS,
242                                                   TX_LEN) + 1);
243             }
244         }
245         SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, TX_DMA_EN, 0);
246     } else {
247         trace_aspeed_i2c_bus_send("BYTE", pool_start, 1,
248                                   bus->regs[reg_byte_buf]);
249         ret = i2c_send(bus->bus, bus->regs[reg_byte_buf]);
250     }
251 
252     return ret;
253 }
254 
255 static void aspeed_i2c_bus_recv(AspeedI2CBus *bus)
256 {
257     AspeedI2CState *s = bus->controller;
258     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
259     uint8_t data;
260     int i;
261     uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
262     uint32_t reg_pool_ctrl = aspeed_i2c_bus_pool_ctrl_offset(bus);
263     uint32_t reg_byte_buf = aspeed_i2c_bus_byte_buf_offset(bus);
264     uint32_t reg_dma_len = aspeed_i2c_bus_dma_len_offset(bus);
265     uint32_t reg_dma_addr = aspeed_i2c_bus_dma_addr_offset(bus);
266     int pool_rx_count = SHARED_ARRAY_FIELD_EX32(bus->regs, reg_pool_ctrl,
267                                                 RX_COUNT);
268 
269     if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_BUFF_EN)) {
270         uint8_t *pool_base = aic->bus_pool_base(bus);
271 
272         for (i = 0; i < pool_rx_count; i++) {
273             pool_base[i] = i2c_recv(bus->bus);
274             trace_aspeed_i2c_bus_recv("BUF", i + 1, pool_rx_count,
275                                       pool_base[i]);
276         }
277 
278         /* Update RX count */
279         SHARED_ARRAY_FIELD_DP32(bus->regs, reg_pool_ctrl, RX_COUNT, i & 0xff);
280         SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, RX_BUFF_EN, 0);
281     } else if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_DMA_EN)) {
282         uint8_t data;
283         /* In new mode, clear how many bytes we RXed */
284         if (aspeed_i2c_is_new_mode(bus->controller)) {
285             ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN_STS, RX_LEN, 0);
286         }
287 
288         while (bus->regs[reg_dma_len]) {
289             MemTxResult result;
290 
291             data = i2c_recv(bus->bus);
292             trace_aspeed_i2c_bus_recv("DMA", bus->regs[reg_dma_len],
293                                       bus->regs[reg_dma_len], data);
294             result = address_space_write(&s->dram_as, bus->regs[reg_dma_addr],
295                                          MEMTXATTRS_UNSPECIFIED, &data, 1);
296             if (result != MEMTX_OK) {
297                 qemu_log_mask(LOG_GUEST_ERROR, "%s: DRAM write failed @%08x\n",
298                               __func__, bus->regs[reg_dma_addr]);
299                 return;
300             }
301             bus->regs[reg_dma_addr]++;
302             bus->regs[reg_dma_len]--;
303             /* In new mode, keep track of how many bytes we RXed */
304             if (aspeed_i2c_is_new_mode(bus->controller)) {
305                 ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN_STS, RX_LEN,
306                                  ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN_STS,
307                                                   RX_LEN) + 1);
308             }
309         }
310         SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, RX_DMA_EN, 0);
311     } else {
312         data = i2c_recv(bus->bus);
313         trace_aspeed_i2c_bus_recv("BYTE", 1, 1, bus->regs[reg_byte_buf]);
314         SHARED_ARRAY_FIELD_DP32(bus->regs, reg_byte_buf, RX_BUF, data);
315     }
316 }
317 
318 static void aspeed_i2c_handle_rx_cmd(AspeedI2CBus *bus)
319 {
320     uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
321     uint32_t reg_intr_sts = aspeed_i2c_bus_intr_sts_offset(bus);
322 
323     aspeed_i2c_set_state(bus, I2CD_MRXD);
324     aspeed_i2c_bus_recv(bus);
325     SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, RX_DONE, 1);
326     if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_S_RX_CMD_LAST)) {
327         i2c_nack(bus->bus);
328     }
329     SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_RX_CMD, 0);
330     SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_S_RX_CMD_LAST, 0);
331     aspeed_i2c_set_state(bus, I2CD_MACTIVE);
332 }
333 
334 static uint8_t aspeed_i2c_get_addr(AspeedI2CBus *bus)
335 {
336     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
337     uint32_t reg_byte_buf = aspeed_i2c_bus_byte_buf_offset(bus);
338     uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
339 
340     if (aspeed_i2c_bus_pkt_mode_en(bus)) {
341         return (ARRAY_FIELD_EX32(bus->regs, I2CM_CMD, PKT_DEV_ADDR) << 1) |
342                 SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_RX_CMD);
343     }
344     if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_BUFF_EN)) {
345         uint8_t *pool_base = aic->bus_pool_base(bus);
346 
347         return pool_base[0];
348     } else if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_DMA_EN)) {
349         uint8_t data;
350 
351         aspeed_i2c_dma_read(bus, &data);
352         return data;
353     } else {
354         return bus->regs[reg_byte_buf];
355     }
356 }
357 
358 static bool aspeed_i2c_check_sram(AspeedI2CBus *bus)
359 {
360     AspeedI2CState *s = bus->controller;
361     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
362     uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
363     bool dma_en = SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_DMA_EN)  ||
364                   SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_DMA_EN)  ||
365                   SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_BUFF_EN) ||
366                   SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_BUFF_EN);
367     if (!aic->check_sram) {
368         return true;
369     }
370 
371     /*
372      * AST2500: SRAM must be enabled before using the Buffer Pool or
373      * DMA mode.
374      */
375     if (!FIELD_EX32(s->ctrl_global, I2C_CTRL_GLOBAL, SRAM_EN) && dma_en) {
376         qemu_log_mask(LOG_GUEST_ERROR, "%s: SRAM is not enabled\n", __func__);
377         return false;
378     }
379 
380     return true;
381 }
382 
383 static void aspeed_i2c_bus_cmd_dump(AspeedI2CBus *bus)
384 {
385     g_autofree char *cmd_flags = NULL;
386     uint32_t count;
387     uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
388     uint32_t reg_pool_ctrl = aspeed_i2c_bus_pool_ctrl_offset(bus);
389     uint32_t reg_intr_sts = aspeed_i2c_bus_intr_sts_offset(bus);
390     uint32_t reg_dma_len = aspeed_i2c_bus_dma_len_offset(bus);
391     if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_BUFF_EN)) {
392         count = SHARED_ARRAY_FIELD_EX32(bus->regs, reg_pool_ctrl, TX_COUNT);
393     } else if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_DMA_EN)) {
394         count = bus->regs[reg_dma_len];
395     } else { /* BYTE mode */
396         count = 1;
397     }
398 
399     cmd_flags = g_strdup_printf("%s%s%s%s%s%s%s%s%s",
400     SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_START_CMD) ? "start|" : "",
401     SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_DMA_EN) ? "rxdma|" : "",
402     SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_DMA_EN) ? "txdma|" : "",
403     SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_BUFF_EN) ? "rxbuf|" : "",
404     SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_BUFF_EN) ? "txbuf|" : "",
405     SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_TX_CMD) ? "tx|" : "",
406     SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_RX_CMD) ? "rx|" : "",
407     SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_S_RX_CMD_LAST) ? "last|" : "",
408     SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_STOP_CMD) ? "stop|" : "");
409 
410     trace_aspeed_i2c_bus_cmd(bus->regs[reg_cmd], cmd_flags, count,
411                              bus->regs[reg_intr_sts]);
412 }
413 
414 /*
415  * The state machine needs some refinement. It is only used to track
416  * invalid STOP commands for the moment.
417  */
418 static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value)
419 {
420     uint8_t pool_start = 0;
421     uint32_t reg_intr_sts = aspeed_i2c_bus_intr_sts_offset(bus);
422     uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
423     uint32_t reg_pool_ctrl = aspeed_i2c_bus_pool_ctrl_offset(bus);
424     uint32_t reg_dma_len = aspeed_i2c_bus_dma_len_offset(bus);
425 
426     if (!aspeed_i2c_check_sram(bus)) {
427         return;
428     }
429 
430     if (trace_event_get_state_backends(TRACE_ASPEED_I2C_BUS_CMD)) {
431         aspeed_i2c_bus_cmd_dump(bus);
432     }
433 
434     if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_START_CMD)) {
435         uint8_t state = aspeed_i2c_get_state(bus) & I2CD_MACTIVE ?
436             I2CD_MSTARTR : I2CD_MSTART;
437         uint8_t addr;
438 
439         aspeed_i2c_set_state(bus, state);
440 
441         addr = aspeed_i2c_get_addr(bus);
442         if (i2c_start_transfer(bus->bus, extract32(addr, 1, 7),
443                                extract32(addr, 0, 1))) {
444             SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, TX_NAK, 1);
445             if (aspeed_i2c_bus_pkt_mode_en(bus)) {
446                 ARRAY_FIELD_DP32(bus->regs, I2CM_INTR_STS, PKT_CMD_FAIL, 1);
447             }
448         } else {
449             /* START doesn't set TX_ACK in packet mode */
450             if (!aspeed_i2c_bus_pkt_mode_en(bus)) {
451                 SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, TX_ACK, 1);
452             }
453         }
454 
455         SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_START_CMD, 0);
456 
457         /*
458          * The START command is also a TX command, as the slave
459          * address is sent on the bus. Drop the TX flag if nothing
460          * else needs to be sent in this sequence.
461          */
462         if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_BUFF_EN)) {
463             if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_pool_ctrl, TX_COUNT)
464                 == 1) {
465                 SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_TX_CMD, 0);
466             } else {
467                 /*
468                  * Increase the start index in the TX pool buffer to
469                  * skip the address byte.
470                  */
471                 pool_start++;
472             }
473         } else if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_DMA_EN)) {
474             if (bus->regs[reg_dma_len] == 0) {
475                 SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_TX_CMD, 0);
476             }
477         } else {
478             SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_TX_CMD, 0);
479         }
480 
481         /* No slave found */
482         if (!i2c_bus_busy(bus->bus)) {
483             if (aspeed_i2c_bus_pkt_mode_en(bus)) {
484                 ARRAY_FIELD_DP32(bus->regs, I2CM_INTR_STS, PKT_CMD_FAIL, 1);
485                 ARRAY_FIELD_DP32(bus->regs, I2CM_INTR_STS, PKT_CMD_DONE, 1);
486             }
487             return;
488         }
489         aspeed_i2c_set_state(bus, I2CD_MACTIVE);
490     }
491 
492     if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_TX_CMD)) {
493         aspeed_i2c_set_state(bus, I2CD_MTXD);
494         if (aspeed_i2c_bus_send(bus, pool_start)) {
495             SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, TX_NAK, 1);
496             i2c_end_transfer(bus->bus);
497         } else {
498             SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, TX_ACK, 1);
499         }
500         SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_TX_CMD, 0);
501         aspeed_i2c_set_state(bus, I2CD_MACTIVE);
502     }
503 
504     if ((SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_RX_CMD) ||
505          SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_S_RX_CMD_LAST)) &&
506         !SHARED_ARRAY_FIELD_EX32(bus->regs, reg_intr_sts, RX_DONE)) {
507         aspeed_i2c_handle_rx_cmd(bus);
508     }
509 
510     if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_STOP_CMD)) {
511         if (!(aspeed_i2c_get_state(bus) & I2CD_MACTIVE)) {
512             qemu_log_mask(LOG_GUEST_ERROR, "%s: abnormal stop\n", __func__);
513             SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, ABNORMAL, 1);
514             if (aspeed_i2c_bus_pkt_mode_en(bus)) {
515                 ARRAY_FIELD_DP32(bus->regs, I2CM_INTR_STS, PKT_CMD_FAIL, 1);
516             }
517         } else {
518             aspeed_i2c_set_state(bus, I2CD_MSTOP);
519             i2c_end_transfer(bus->bus);
520             SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, NORMAL_STOP, 1);
521         }
522         SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_STOP_CMD, 0);
523         aspeed_i2c_set_state(bus, I2CD_IDLE);
524     }
525 
526     if (aspeed_i2c_bus_pkt_mode_en(bus)) {
527         ARRAY_FIELD_DP32(bus->regs, I2CM_INTR_STS, PKT_CMD_DONE, 1);
528     }
529 }
530 
531 static void aspeed_i2c_bus_new_write(AspeedI2CBus *bus, hwaddr offset,
532                                      uint64_t value, unsigned size)
533 {
534     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
535     bool handle_rx;
536     bool w1t;
537 
538     trace_aspeed_i2c_bus_write(bus->id, offset, size, value);
539 
540     switch (offset) {
541     case A_I2CC_FUN_CTRL:
542         if (SHARED_FIELD_EX32(value, SLAVE_EN)) {
543             qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
544                           __func__);
545             break;
546         }
547         bus->regs[R_I2CD_FUN_CTRL] = value & 0x007dc3ff;
548         break;
549     case A_I2CC_AC_TIMING:
550         bus->regs[R_I2CC_AC_TIMING] = value & 0x1ffff0ff;
551         break;
552     case A_I2CC_MS_TXRX_BYTE_BUF:
553         SHARED_ARRAY_FIELD_DP32(bus->regs, R_I2CC_MS_TXRX_BYTE_BUF, TX_BUF,
554                                 value);
555         break;
556     case A_I2CC_POOL_CTRL:
557         bus->regs[R_I2CC_POOL_CTRL] &= ~0xffffff;
558         bus->regs[R_I2CC_POOL_CTRL] |= (value & 0xffffff);
559         break;
560     case A_I2CM_INTR_CTRL:
561         bus->regs[R_I2CM_INTR_CTRL] = value & 0x0007f07f;
562         break;
563     case A_I2CM_INTR_STS:
564         handle_rx = SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CM_INTR_STS, RX_DONE)
565                     && SHARED_FIELD_EX32(value, RX_DONE);
566 
567         /* In packet mode, clearing PKT_CMD_DONE clears other interrupts. */
568         if (aspeed_i2c_bus_pkt_mode_en(bus) &&
569            FIELD_EX32(value, I2CM_INTR_STS, PKT_CMD_DONE)) {
570             bus->regs[R_I2CM_INTR_STS] &= 0xf0001000;
571             if (!bus->regs[R_I2CM_INTR_STS]) {
572                 bus->controller->intr_status &= ~(1 << bus->id);
573                 qemu_irq_lower(aic->bus_get_irq(bus));
574             }
575             break;
576         }
577         bus->regs[R_I2CM_INTR_STS] &= ~(value & 0xf007f07f);
578         if (!bus->regs[R_I2CM_INTR_STS]) {
579             bus->controller->intr_status &= ~(1 << bus->id);
580             qemu_irq_lower(aic->bus_get_irq(bus));
581         }
582         if (handle_rx && (SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CM_CMD,
583                                                   M_RX_CMD) ||
584                           SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CM_CMD,
585                                                   M_S_RX_CMD_LAST))) {
586             aspeed_i2c_handle_rx_cmd(bus);
587             aspeed_i2c_bus_raise_interrupt(bus);
588         }
589         break;
590     case A_I2CM_CMD:
591         if (!aspeed_i2c_bus_is_enabled(bus)) {
592             break;
593         }
594 
595         if (!aspeed_i2c_bus_is_master(bus)) {
596             qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
597                           __func__);
598             break;
599         }
600 
601         if (!aic->has_dma &&
602             (SHARED_FIELD_EX32(value, RX_DMA_EN) ||
603              SHARED_FIELD_EX32(value, TX_DMA_EN))) {
604             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
605             break;
606         }
607 
608         if (bus->regs[R_I2CM_INTR_STS] & 0xffff0000) {
609             qemu_log_mask(LOG_UNIMP, "%s: Packet mode is not implemented\n",
610                           __func__);
611             break;
612         }
613 
614         value &= 0xff0ffbfb;
615         if (ARRAY_FIELD_EX32(bus->regs, I2CM_CMD, W1_CTRL)) {
616             bus->regs[R_I2CM_CMD] |= value;
617         } else {
618             bus->regs[R_I2CM_CMD] = value;
619         }
620 
621         aspeed_i2c_bus_handle_cmd(bus, value);
622         aspeed_i2c_bus_raise_interrupt(bus);
623         break;
624     case A_I2CM_DMA_TX_ADDR:
625         bus->regs[R_I2CM_DMA_TX_ADDR] = FIELD_EX32(value, I2CM_DMA_TX_ADDR,
626                                                    ADDR);
627         bus->regs[R_I2CC_DMA_ADDR] = FIELD_EX32(value, I2CM_DMA_TX_ADDR, ADDR);
628         bus->regs[R_I2CC_DMA_LEN] = ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN,
629                                                      TX_BUF_LEN) + 1;
630         break;
631     case A_I2CM_DMA_RX_ADDR:
632         bus->regs[R_I2CM_DMA_RX_ADDR] = FIELD_EX32(value, I2CM_DMA_RX_ADDR,
633                                                    ADDR);
634         bus->regs[R_I2CC_DMA_ADDR] = FIELD_EX32(value, I2CM_DMA_RX_ADDR, ADDR);
635         bus->regs[R_I2CC_DMA_LEN] = ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN,
636                                                      RX_BUF_LEN) + 1;
637         break;
638     case A_I2CM_DMA_LEN:
639         w1t = ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN, RX_BUF_LEN_W1T) ||
640                    ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN, TX_BUF_LEN_W1T);
641         /* If none of the w1t bits are set, just write to the reg as normal. */
642         if (!w1t) {
643             bus->regs[R_I2CM_DMA_LEN] = value;
644             break;
645         }
646         if (ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN, RX_BUF_LEN_W1T)) {
647             ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN, RX_BUF_LEN,
648                              FIELD_EX32(value, I2CM_DMA_LEN, RX_BUF_LEN));
649         }
650         if (ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN, TX_BUF_LEN_W1T)) {
651             ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN, TX_BUF_LEN,
652                              FIELD_EX32(value, I2CM_DMA_LEN, TX_BUF_LEN));
653         }
654         break;
655     case A_I2CM_DMA_LEN_STS:
656         /* Writes clear to 0 */
657         bus->regs[R_I2CM_DMA_LEN_STS] = 0;
658         break;
659     case A_I2CC_DMA_ADDR:
660     case A_I2CC_DMA_LEN:
661         /* RO */
662         break;
663     case A_I2CS_DMA_LEN_STS:
664     case A_I2CS_DMA_TX_ADDR:
665     case A_I2CS_DMA_RX_ADDR:
666     case A_I2CS_DEV_ADDR:
667     case A_I2CS_INTR_CTRL:
668     case A_I2CS_INTR_STS:
669     case A_I2CS_CMD:
670     case A_I2CS_DMA_LEN:
671         qemu_log_mask(LOG_UNIMP, "%s: Slave mode is not implemented\n",
672                       __func__);
673         break;
674     default:
675         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
676                       __func__, offset);
677     }
678 }
679 
680 static void aspeed_i2c_bus_old_write(AspeedI2CBus *bus, hwaddr offset,
681                                      uint64_t value, unsigned size)
682 {
683     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
684     bool handle_rx;
685 
686     trace_aspeed_i2c_bus_write(bus->id, offset, size, value);
687 
688     switch (offset) {
689     case A_I2CD_FUN_CTRL:
690         if (SHARED_FIELD_EX32(value, SLAVE_EN)) {
691             qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
692                           __func__);
693             break;
694         }
695         bus->regs[R_I2CD_FUN_CTRL] = value & 0x0071C3FF;
696         break;
697     case A_I2CD_AC_TIMING1:
698         bus->regs[R_I2CD_AC_TIMING1] = value & 0xFFFFF0F;
699         break;
700     case A_I2CD_AC_TIMING2:
701         bus->regs[R_I2CD_AC_TIMING2] = value & 0x7;
702         break;
703     case A_I2CD_INTR_CTRL:
704         bus->regs[R_I2CD_INTR_CTRL] = value & 0x7FFF;
705         break;
706     case A_I2CD_INTR_STS:
707         handle_rx = SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CD_INTR_STS, RX_DONE)
708                     && SHARED_FIELD_EX32(value, RX_DONE);
709         bus->regs[R_I2CD_INTR_STS] &= ~(value & 0x7FFF);
710         if (!bus->regs[R_I2CD_INTR_STS]) {
711             bus->controller->intr_status &= ~(1 << bus->id);
712             qemu_irq_lower(aic->bus_get_irq(bus));
713         }
714         if (handle_rx && (SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CD_CMD,
715                                                   M_RX_CMD) ||
716                       SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CD_CMD,
717                                               M_S_RX_CMD_LAST))) {
718             aspeed_i2c_handle_rx_cmd(bus);
719             aspeed_i2c_bus_raise_interrupt(bus);
720         }
721         break;
722     case A_I2CD_DEV_ADDR:
723         qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
724                       __func__);
725         break;
726     case A_I2CD_POOL_CTRL:
727         bus->regs[R_I2CD_POOL_CTRL] &= ~0xffffff;
728         bus->regs[R_I2CD_POOL_CTRL] |= (value & 0xffffff);
729         break;
730 
731     case A_I2CD_BYTE_BUF:
732         SHARED_ARRAY_FIELD_DP32(bus->regs, R_I2CD_BYTE_BUF, TX_BUF, value);
733         break;
734     case A_I2CD_CMD:
735         if (!aspeed_i2c_bus_is_enabled(bus)) {
736             break;
737         }
738 
739         if (!aspeed_i2c_bus_is_master(bus)) {
740             qemu_log_mask(LOG_UNIMP, "%s: slave mode not implemented\n",
741                           __func__);
742             break;
743         }
744 
745         if (!aic->has_dma &&
746             (SHARED_FIELD_EX32(value, RX_DMA_EN) ||
747              SHARED_FIELD_EX32(value, TX_DMA_EN))) {
748             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
749             break;
750         }
751 
752         bus->regs[R_I2CD_CMD] &= ~0xFFFF;
753         bus->regs[R_I2CD_CMD] |= value & 0xFFFF;
754 
755         aspeed_i2c_bus_handle_cmd(bus, value);
756         aspeed_i2c_bus_raise_interrupt(bus);
757         break;
758     case A_I2CD_DMA_ADDR:
759         if (!aic->has_dma) {
760             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
761             break;
762         }
763 
764         bus->regs[R_I2CD_DMA_ADDR] = value & 0x3ffffffc;
765         break;
766 
767     case A_I2CD_DMA_LEN:
768         if (!aic->has_dma) {
769             qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n",  __func__);
770             break;
771         }
772 
773         bus->regs[R_I2CD_DMA_LEN] = value & 0xfff;
774         if (!bus->regs[R_I2CD_DMA_LEN]) {
775             qemu_log_mask(LOG_UNIMP, "%s: invalid DMA length\n",  __func__);
776         }
777         break;
778 
779     default:
780         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
781                       __func__, offset);
782     }
783 }
784 
785 static void aspeed_i2c_bus_write(void *opaque, hwaddr offset,
786                                      uint64_t value, unsigned size)
787 {
788     AspeedI2CBus *bus = opaque;
789     if (aspeed_i2c_is_new_mode(bus->controller)) {
790         aspeed_i2c_bus_new_write(bus, offset, value, size);
791     } else {
792         aspeed_i2c_bus_old_write(bus, offset, value, size);
793     }
794 }
795 
796 static uint64_t aspeed_i2c_ctrl_read(void *opaque, hwaddr offset,
797                                    unsigned size)
798 {
799     AspeedI2CState *s = opaque;
800 
801     switch (offset) {
802     case A_I2C_CTRL_STATUS:
803         return s->intr_status;
804     case A_I2C_CTRL_GLOBAL:
805         return s->ctrl_global;
806     case A_I2C_CTRL_NEW_CLK_DIVIDER:
807         if (aspeed_i2c_is_new_mode(s)) {
808             return s->new_clk_divider;
809         }
810         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
811                       __func__, offset);
812         break;
813     default:
814         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
815                       __func__, offset);
816         break;
817     }
818 
819     return -1;
820 }
821 
822 static void aspeed_i2c_ctrl_write(void *opaque, hwaddr offset,
823                                   uint64_t value, unsigned size)
824 {
825     AspeedI2CState *s = opaque;
826 
827     switch (offset) {
828     case A_I2C_CTRL_GLOBAL:
829         s->ctrl_global = value;
830         break;
831     case A_I2C_CTRL_NEW_CLK_DIVIDER:
832         if (aspeed_i2c_is_new_mode(s)) {
833             s->new_clk_divider = value;
834         } else {
835             qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx
836                           "\n", __func__, offset);
837         }
838         break;
839     case A_I2C_CTRL_STATUS:
840     default:
841         qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
842                       __func__, offset);
843         break;
844     }
845 }
846 
847 static const MemoryRegionOps aspeed_i2c_bus_ops = {
848     .read = aspeed_i2c_bus_read,
849     .write = aspeed_i2c_bus_write,
850     .endianness = DEVICE_LITTLE_ENDIAN,
851 };
852 
853 static const MemoryRegionOps aspeed_i2c_ctrl_ops = {
854     .read = aspeed_i2c_ctrl_read,
855     .write = aspeed_i2c_ctrl_write,
856     .endianness = DEVICE_LITTLE_ENDIAN,
857 };
858 
859 static uint64_t aspeed_i2c_pool_read(void *opaque, hwaddr offset,
860                                      unsigned size)
861 {
862     AspeedI2CState *s = opaque;
863     uint64_t ret = 0;
864     int i;
865 
866     for (i = 0; i < size; i++) {
867         ret |= (uint64_t) s->pool[offset + i] << (8 * i);
868     }
869 
870     return ret;
871 }
872 
873 static void aspeed_i2c_pool_write(void *opaque, hwaddr offset,
874                                   uint64_t value, unsigned size)
875 {
876     AspeedI2CState *s = opaque;
877     int i;
878 
879     for (i = 0; i < size; i++) {
880         s->pool[offset + i] = (value >> (8 * i)) & 0xFF;
881     }
882 }
883 
884 static const MemoryRegionOps aspeed_i2c_pool_ops = {
885     .read = aspeed_i2c_pool_read,
886     .write = aspeed_i2c_pool_write,
887     .endianness = DEVICE_LITTLE_ENDIAN,
888     .valid = {
889         .min_access_size = 1,
890         .max_access_size = 4,
891     },
892 };
893 
894 static const VMStateDescription aspeed_i2c_bus_vmstate = {
895     .name = TYPE_ASPEED_I2C,
896     .version_id = 5,
897     .minimum_version_id = 5,
898     .fields = (VMStateField[]) {
899         VMSTATE_UINT32_ARRAY(regs, AspeedI2CBus, ASPEED_I2C_NEW_NUM_REG),
900         VMSTATE_END_OF_LIST()
901     }
902 };
903 
904 static const VMStateDescription aspeed_i2c_vmstate = {
905     .name = TYPE_ASPEED_I2C,
906     .version_id = 2,
907     .minimum_version_id = 2,
908     .fields = (VMStateField[]) {
909         VMSTATE_UINT32(intr_status, AspeedI2CState),
910         VMSTATE_STRUCT_ARRAY(busses, AspeedI2CState,
911                              ASPEED_I2C_NR_BUSSES, 1, aspeed_i2c_bus_vmstate,
912                              AspeedI2CBus),
913         VMSTATE_UINT8_ARRAY(pool, AspeedI2CState, ASPEED_I2C_MAX_POOL_SIZE),
914         VMSTATE_END_OF_LIST()
915     }
916 };
917 
918 static void aspeed_i2c_reset(DeviceState *dev)
919 {
920     AspeedI2CState *s = ASPEED_I2C(dev);
921 
922     s->intr_status = 0;
923 }
924 
925 static void aspeed_i2c_instance_init(Object *obj)
926 {
927     AspeedI2CState *s = ASPEED_I2C(obj);
928     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
929     int i;
930 
931     for (i = 0; i < aic->num_busses; i++) {
932         object_initialize_child(obj, "bus[*]", &s->busses[i],
933                                 TYPE_ASPEED_I2C_BUS);
934     }
935 }
936 
937 /*
938  * Address Definitions (AST2400 and AST2500)
939  *
940  *   0x000 ... 0x03F: Global Register
941  *   0x040 ... 0x07F: Device 1
942  *   0x080 ... 0x0BF: Device 2
943  *   0x0C0 ... 0x0FF: Device 3
944  *   0x100 ... 0x13F: Device 4
945  *   0x140 ... 0x17F: Device 5
946  *   0x180 ... 0x1BF: Device 6
947  *   0x1C0 ... 0x1FF: Device 7
948  *   0x200 ... 0x2FF: Buffer Pool  (unused in linux driver)
949  *   0x300 ... 0x33F: Device 8
950  *   0x340 ... 0x37F: Device 9
951  *   0x380 ... 0x3BF: Device 10
952  *   0x3C0 ... 0x3FF: Device 11
953  *   0x400 ... 0x43F: Device 12
954  *   0x440 ... 0x47F: Device 13
955  *   0x480 ... 0x4BF: Device 14
956  *   0x800 ... 0xFFF: Buffer Pool  (unused in linux driver)
957  */
958 static void aspeed_i2c_realize(DeviceState *dev, Error **errp)
959 {
960     int i;
961     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
962     AspeedI2CState *s = ASPEED_I2C(dev);
963     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
964 
965     sysbus_init_irq(sbd, &s->irq);
966     memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_i2c_ctrl_ops, s,
967                           "aspeed.i2c", 0x1000);
968     sysbus_init_mmio(sbd, &s->iomem);
969 
970     for (i = 0; i < aic->num_busses; i++) {
971         Object *bus = OBJECT(&s->busses[i]);
972         int offset = i < aic->gap ? 1 : 5;
973 
974         if (!object_property_set_link(bus, "controller", OBJECT(s), errp)) {
975             return;
976         }
977 
978         if (!object_property_set_uint(bus, "bus-id", i, errp)) {
979             return;
980         }
981 
982         if (!sysbus_realize(SYS_BUS_DEVICE(bus), errp)) {
983             return;
984         }
985 
986         memory_region_add_subregion(&s->iomem, aic->reg_size * (i + offset),
987                                     &s->busses[i].mr);
988     }
989 
990     memory_region_init_io(&s->pool_iomem, OBJECT(s), &aspeed_i2c_pool_ops, s,
991                           "aspeed.i2c-pool", aic->pool_size);
992     memory_region_add_subregion(&s->iomem, aic->pool_base, &s->pool_iomem);
993 
994     if (aic->has_dma) {
995         if (!s->dram_mr) {
996             error_setg(errp, TYPE_ASPEED_I2C ": 'dram' link not set");
997             return;
998         }
999 
1000         address_space_init(&s->dram_as, s->dram_mr,
1001                            TYPE_ASPEED_I2C "-dma-dram");
1002     }
1003 }
1004 
1005 static Property aspeed_i2c_properties[] = {
1006     DEFINE_PROP_LINK("dram", AspeedI2CState, dram_mr,
1007                      TYPE_MEMORY_REGION, MemoryRegion *),
1008     DEFINE_PROP_END_OF_LIST(),
1009 };
1010 
1011 static void aspeed_i2c_class_init(ObjectClass *klass, void *data)
1012 {
1013     DeviceClass *dc = DEVICE_CLASS(klass);
1014 
1015     dc->vmsd = &aspeed_i2c_vmstate;
1016     dc->reset = aspeed_i2c_reset;
1017     device_class_set_props(dc, aspeed_i2c_properties);
1018     dc->realize = aspeed_i2c_realize;
1019     dc->desc = "Aspeed I2C Controller";
1020 }
1021 
1022 static const TypeInfo aspeed_i2c_info = {
1023     .name          = TYPE_ASPEED_I2C,
1024     .parent        = TYPE_SYS_BUS_DEVICE,
1025     .instance_init = aspeed_i2c_instance_init,
1026     .instance_size = sizeof(AspeedI2CState),
1027     .class_init    = aspeed_i2c_class_init,
1028     .class_size = sizeof(AspeedI2CClass),
1029     .abstract   = true,
1030 };
1031 
1032 static void aspeed_i2c_bus_reset(DeviceState *dev)
1033 {
1034     AspeedI2CBus *s = ASPEED_I2C_BUS(dev);
1035 
1036     memset(s->regs, 0, sizeof(s->regs));
1037     i2c_end_transfer(s->bus);
1038 }
1039 
1040 static void aspeed_i2c_bus_realize(DeviceState *dev, Error **errp)
1041 {
1042     AspeedI2CBus *s = ASPEED_I2C_BUS(dev);
1043     AspeedI2CClass *aic;
1044     g_autofree char *name = g_strdup_printf(TYPE_ASPEED_I2C_BUS ".%d", s->id);
1045 
1046     if (!s->controller) {
1047         error_setg(errp, TYPE_ASPEED_I2C_BUS ": 'controller' link not set");
1048         return;
1049     }
1050 
1051     aic = ASPEED_I2C_GET_CLASS(s->controller);
1052 
1053     sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq);
1054 
1055     s->bus = i2c_init_bus(dev, name);
1056 
1057     memory_region_init_io(&s->mr, OBJECT(s), &aspeed_i2c_bus_ops,
1058                           s, name, aic->reg_size);
1059     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mr);
1060 }
1061 
1062 static Property aspeed_i2c_bus_properties[] = {
1063     DEFINE_PROP_UINT8("bus-id", AspeedI2CBus, id, 0),
1064     DEFINE_PROP_LINK("controller", AspeedI2CBus, controller, TYPE_ASPEED_I2C,
1065                      AspeedI2CState *),
1066     DEFINE_PROP_END_OF_LIST(),
1067 };
1068 
1069 static void aspeed_i2c_bus_class_init(ObjectClass *klass, void *data)
1070 {
1071     DeviceClass *dc = DEVICE_CLASS(klass);
1072 
1073     dc->desc = "Aspeed I2C Bus";
1074     dc->realize = aspeed_i2c_bus_realize;
1075     dc->reset = aspeed_i2c_bus_reset;
1076     device_class_set_props(dc, aspeed_i2c_bus_properties);
1077 }
1078 
1079 static const TypeInfo aspeed_i2c_bus_info = {
1080     .name           = TYPE_ASPEED_I2C_BUS,
1081     .parent         = TYPE_SYS_BUS_DEVICE,
1082     .instance_size  = sizeof(AspeedI2CBus),
1083     .class_init     = aspeed_i2c_bus_class_init,
1084 };
1085 
1086 static qemu_irq aspeed_2400_i2c_bus_get_irq(AspeedI2CBus *bus)
1087 {
1088     return bus->controller->irq;
1089 }
1090 
1091 static uint8_t *aspeed_2400_i2c_bus_pool_base(AspeedI2CBus *bus)
1092 {
1093     uint8_t *pool_page =
1094         &bus->controller->pool[ARRAY_FIELD_EX32(bus->regs, I2CD_FUN_CTRL,
1095                                                 POOL_PAGE_SEL) * 0x100];
1096 
1097     return &pool_page[ARRAY_FIELD_EX32(bus->regs, I2CD_POOL_CTRL, OFFSET)];
1098 }
1099 
1100 static void aspeed_2400_i2c_class_init(ObjectClass *klass, void *data)
1101 {
1102     DeviceClass *dc = DEVICE_CLASS(klass);
1103     AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
1104 
1105     dc->desc = "ASPEED 2400 I2C Controller";
1106 
1107     aic->num_busses = 14;
1108     aic->reg_size = 0x40;
1109     aic->gap = 7;
1110     aic->bus_get_irq = aspeed_2400_i2c_bus_get_irq;
1111     aic->pool_size = 0x800;
1112     aic->pool_base = 0x800;
1113     aic->bus_pool_base = aspeed_2400_i2c_bus_pool_base;
1114 }
1115 
1116 static const TypeInfo aspeed_2400_i2c_info = {
1117     .name = TYPE_ASPEED_2400_I2C,
1118     .parent = TYPE_ASPEED_I2C,
1119     .class_init = aspeed_2400_i2c_class_init,
1120 };
1121 
1122 static qemu_irq aspeed_2500_i2c_bus_get_irq(AspeedI2CBus *bus)
1123 {
1124     return bus->controller->irq;
1125 }
1126 
1127 static uint8_t *aspeed_2500_i2c_bus_pool_base(AspeedI2CBus *bus)
1128 {
1129     return &bus->controller->pool[bus->id * 0x10];
1130 }
1131 
1132 static void aspeed_2500_i2c_class_init(ObjectClass *klass, void *data)
1133 {
1134     DeviceClass *dc = DEVICE_CLASS(klass);
1135     AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
1136 
1137     dc->desc = "ASPEED 2500 I2C Controller";
1138 
1139     aic->num_busses = 14;
1140     aic->reg_size = 0x40;
1141     aic->gap = 7;
1142     aic->bus_get_irq = aspeed_2500_i2c_bus_get_irq;
1143     aic->pool_size = 0x100;
1144     aic->pool_base = 0x200;
1145     aic->bus_pool_base = aspeed_2500_i2c_bus_pool_base;
1146     aic->check_sram = true;
1147     aic->has_dma = true;
1148 }
1149 
1150 static const TypeInfo aspeed_2500_i2c_info = {
1151     .name = TYPE_ASPEED_2500_I2C,
1152     .parent = TYPE_ASPEED_I2C,
1153     .class_init = aspeed_2500_i2c_class_init,
1154 };
1155 
1156 static qemu_irq aspeed_2600_i2c_bus_get_irq(AspeedI2CBus *bus)
1157 {
1158     return bus->irq;
1159 }
1160 
1161 static uint8_t *aspeed_2600_i2c_bus_pool_base(AspeedI2CBus *bus)
1162 {
1163    return &bus->controller->pool[bus->id * 0x20];
1164 }
1165 
1166 static void aspeed_2600_i2c_class_init(ObjectClass *klass, void *data)
1167 {
1168     DeviceClass *dc = DEVICE_CLASS(klass);
1169     AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
1170 
1171     dc->desc = "ASPEED 2600 I2C Controller";
1172 
1173     aic->num_busses = 16;
1174     aic->reg_size = 0x80;
1175     aic->gap = -1; /* no gap */
1176     aic->bus_get_irq = aspeed_2600_i2c_bus_get_irq;
1177     aic->pool_size = 0x200;
1178     aic->pool_base = 0xC00;
1179     aic->bus_pool_base = aspeed_2600_i2c_bus_pool_base;
1180     aic->has_dma = true;
1181 }
1182 
1183 static const TypeInfo aspeed_2600_i2c_info = {
1184     .name = TYPE_ASPEED_2600_I2C,
1185     .parent = TYPE_ASPEED_I2C,
1186     .class_init = aspeed_2600_i2c_class_init,
1187 };
1188 
1189 static void aspeed_1030_i2c_class_init(ObjectClass *klass, void *data)
1190 {
1191     DeviceClass *dc = DEVICE_CLASS(klass);
1192     AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
1193 
1194     dc->desc = "ASPEED 1030 I2C Controller";
1195 
1196     aic->num_busses = 14;
1197     aic->reg_size = 0x80;
1198     aic->gap = -1; /* no gap */
1199     aic->bus_get_irq = aspeed_2600_i2c_bus_get_irq;
1200     aic->pool_size = 0x200;
1201     aic->pool_base = 0xC00;
1202     aic->bus_pool_base = aspeed_2600_i2c_bus_pool_base;
1203     aic->has_dma = true;
1204 }
1205 
1206 static const TypeInfo aspeed_1030_i2c_info = {
1207     .name = TYPE_ASPEED_1030_I2C,
1208     .parent = TYPE_ASPEED_I2C,
1209     .class_init = aspeed_1030_i2c_class_init,
1210 };
1211 
1212 static void aspeed_i2c_register_types(void)
1213 {
1214     type_register_static(&aspeed_i2c_bus_info);
1215     type_register_static(&aspeed_i2c_info);
1216     type_register_static(&aspeed_2400_i2c_info);
1217     type_register_static(&aspeed_2500_i2c_info);
1218     type_register_static(&aspeed_2600_i2c_info);
1219     type_register_static(&aspeed_1030_i2c_info);
1220 }
1221 
1222 type_init(aspeed_i2c_register_types)
1223 
1224 
1225 I2CBus *aspeed_i2c_get_bus(AspeedI2CState *s, int busnr)
1226 {
1227     AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
1228     I2CBus *bus = NULL;
1229 
1230     if (busnr >= 0 && busnr < aic->num_busses) {
1231         bus = s->busses[busnr].bus;
1232     }
1233 
1234     return bus;
1235 }
1236