xref: /openbmc/qemu/hw/ssi/allwinner-a10-spi.c (revision f774a677507966222624a9b2859f06ede7608100)
1*8d3dfb62SStrahinja Jankovic /*
2*8d3dfb62SStrahinja Jankovic  *  Allwinner SPI Bus Serial Interface Emulation
3*8d3dfb62SStrahinja Jankovic  *
4*8d3dfb62SStrahinja Jankovic  *  Copyright (C) 2024 Strahinja Jankovic <strahinja.p.jankovic@gmail.com>
5*8d3dfb62SStrahinja Jankovic  *
6*8d3dfb62SStrahinja Jankovic  *  This program is free software; you can redistribute it and/or modify it
7*8d3dfb62SStrahinja Jankovic  *  under the terms of the GNU General Public License as published by the
8*8d3dfb62SStrahinja Jankovic  *  Free Software Foundation; either version 2 of the License, or
9*8d3dfb62SStrahinja Jankovic  *  (at your option) any later version.
10*8d3dfb62SStrahinja Jankovic  *
11*8d3dfb62SStrahinja Jankovic  *  This program is distributed in the hope that it will be useful, but WITHOUT
12*8d3dfb62SStrahinja Jankovic  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*8d3dfb62SStrahinja Jankovic  *  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14*8d3dfb62SStrahinja Jankovic  *  for more details.
15*8d3dfb62SStrahinja Jankovic  *
16*8d3dfb62SStrahinja Jankovic  *  You should have received a copy of the GNU General Public License along
17*8d3dfb62SStrahinja Jankovic  *  with this program; if not, see <http://www.gnu.org/licenses/>.
18*8d3dfb62SStrahinja Jankovic  *
19*8d3dfb62SStrahinja Jankovic  * SPDX-License-Identifier: GPL-2.0-or-later
20*8d3dfb62SStrahinja Jankovic  */
21*8d3dfb62SStrahinja Jankovic 
22*8d3dfb62SStrahinja Jankovic #include "qemu/osdep.h"
23*8d3dfb62SStrahinja Jankovic #include "hw/irq.h"
24*8d3dfb62SStrahinja Jankovic #include "hw/ssi/allwinner-a10-spi.h"
25*8d3dfb62SStrahinja Jankovic #include "migration/vmstate.h"
26*8d3dfb62SStrahinja Jankovic #include "qemu/log.h"
27*8d3dfb62SStrahinja Jankovic #include "qemu/module.h"
28*8d3dfb62SStrahinja Jankovic #include "trace.h"
29*8d3dfb62SStrahinja Jankovic 
30*8d3dfb62SStrahinja Jankovic /* Allwinner SPI memory map */
31*8d3dfb62SStrahinja Jankovic #define SPI_RXDATA_REG   0x00 /* receive data register */
32*8d3dfb62SStrahinja Jankovic #define SPI_TXDATA_REG   0x04 /* transmit data register */
33*8d3dfb62SStrahinja Jankovic #define SPI_CTL_REG      0x08 /* control register */
34*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_REG   0x0c /* interrupt control register */
35*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_REG  0x10 /* interrupt status register */
36*8d3dfb62SStrahinja Jankovic #define SPI_DMACTL_REG   0x14 /* DMA control register */
37*8d3dfb62SStrahinja Jankovic #define SPI_WAIT_REG     0x18 /* wait clock counter register */
38*8d3dfb62SStrahinja Jankovic #define SPI_CCTL_REG     0x1c /* clock rate control register */
39*8d3dfb62SStrahinja Jankovic #define SPI_BC_REG       0x20 /* burst control register */
40*8d3dfb62SStrahinja Jankovic #define SPI_TC_REG       0x24 /* transmit counter register */
41*8d3dfb62SStrahinja Jankovic #define SPI_FIFO_STA_REG 0x28 /* FIFO status register */
42*8d3dfb62SStrahinja Jankovic 
43*8d3dfb62SStrahinja Jankovic /* Data register */
44*8d3dfb62SStrahinja Jankovic #define SPI_DATA_RESET 0
45*8d3dfb62SStrahinja Jankovic 
46*8d3dfb62SStrahinja Jankovic /* Control register */
47*8d3dfb62SStrahinja Jankovic #define SPI_CTL_SDC      (1 << 19)
48*8d3dfb62SStrahinja Jankovic #define SPI_CTL_TP_EN    (1 << 18)
49*8d3dfb62SStrahinja Jankovic #define SPI_CTL_SS_LEVEL (1 << 17)
50*8d3dfb62SStrahinja Jankovic #define SPI_CTL_SS_CTRL  (1 << 16)
51*8d3dfb62SStrahinja Jankovic #define SPI_CTL_DHB      (1 << 15)
52*8d3dfb62SStrahinja Jankovic #define SPI_CTL_DDB      (1 << 14)
53*8d3dfb62SStrahinja Jankovic #define SPI_CTL_SS       (3 << 12)
54*8d3dfb62SStrahinja Jankovic #define SPI_CTL_SS_SHIFT 12
55*8d3dfb62SStrahinja Jankovic #define SPI_CTL_RPSM     (1 << 11)
56*8d3dfb62SStrahinja Jankovic #define SPI_CTL_XCH      (1 << 10)
57*8d3dfb62SStrahinja Jankovic #define SPI_CTL_RF_RST   (1 << 9)
58*8d3dfb62SStrahinja Jankovic #define SPI_CTL_TF_RST   (1 << 8)
59*8d3dfb62SStrahinja Jankovic #define SPI_CTL_SSCTL    (1 << 7)
60*8d3dfb62SStrahinja Jankovic #define SPI_CTL_LMTF     (1 << 6)
61*8d3dfb62SStrahinja Jankovic #define SPI_CTL_DMAMC    (1 << 5)
62*8d3dfb62SStrahinja Jankovic #define SPI_CTL_SSPOL    (1 << 4)
63*8d3dfb62SStrahinja Jankovic #define SPI_CTL_POL      (1 << 3)
64*8d3dfb62SStrahinja Jankovic #define SPI_CTL_PHA      (1 << 2)
65*8d3dfb62SStrahinja Jankovic #define SPI_CTL_MODE     (1 << 1)
66*8d3dfb62SStrahinja Jankovic #define SPI_CTL_EN       (1 << 0)
67*8d3dfb62SStrahinja Jankovic #define SPI_CTL_MASK     0xFFFFFu
68*8d3dfb62SStrahinja Jankovic #define SPI_CTL_RESET    0x0002001Cu
69*8d3dfb62SStrahinja Jankovic 
70*8d3dfb62SStrahinja Jankovic /* Interrupt control register */
71*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_SS_INT_EN          (1 << 17)
72*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_TX_INT_EN          (1 << 16)
73*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_TF_UR_INT_EN       (1 << 14)
74*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_TF_OF_INT_EN       (1 << 13)
75*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_TF_E34_INT_EN      (1 << 12)
76*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_TF_E14_INT_EN      (1 << 11)
77*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_TF_FL_INT_EN       (1 << 10)
78*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_TF_HALF_EMP_INT_EN (1 << 9)
79*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_TF_EMP_INT_EN      (1 << 8)
80*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_RF_UR_INT_EN       (1 << 6)
81*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_RF_OF_INT_EN       (1 << 5)
82*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_RF_E34_INT_EN      (1 << 4)
83*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_RF_E14_INT_EN      (1 << 3)
84*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_RF_FU_INT_EN       (1 << 2)
85*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_RF_HALF_FU_INT_EN  (1 << 1)
86*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_RF_RDY_INT_EN      (1 << 0)
87*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_MASK               0x37F7Fu
88*8d3dfb62SStrahinja Jankovic #define SPI_INTCTL_RESET              0
89*8d3dfb62SStrahinja Jankovic 
90*8d3dfb62SStrahinja Jankovic /* Interrupt status register */
91*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_INT_CBF (1 << 31)
92*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_SSI     (1 << 17)
93*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_TC      (1 << 16)
94*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_TU      (1 << 14)
95*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_TO      (1 << 13)
96*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_TE34    (1 << 12)
97*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_TE14    (1 << 11)
98*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_TF      (1 << 10)
99*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_THE     (1 << 9)
100*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_TE      (1 << 8)
101*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_RU      (1 << 6)
102*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_RO      (1 << 5)
103*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_RF34    (1 << 4)
104*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_RF14    (1 << 3)
105*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_RF      (1 << 2)
106*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_RHF     (1 << 1)
107*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_RR      (1 << 0)
108*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_MASK    0x80037F7Fu
109*8d3dfb62SStrahinja Jankovic #define SPI_INT_STA_RESET   0x00001B00u
110*8d3dfb62SStrahinja Jankovic 
111*8d3dfb62SStrahinja Jankovic /* DMA control register - not implemented */
112*8d3dfb62SStrahinja Jankovic #define SPI_DMACTL_RESET 0
113*8d3dfb62SStrahinja Jankovic 
114*8d3dfb62SStrahinja Jankovic /* Wait clock register */
115*8d3dfb62SStrahinja Jankovic #define SPI_WAIT_REG_WCC_MASK 0xFFFFu
116*8d3dfb62SStrahinja Jankovic #define SPI_WAIT_RESET        0
117*8d3dfb62SStrahinja Jankovic 
118*8d3dfb62SStrahinja Jankovic /* Clock control register - not implemented */
119*8d3dfb62SStrahinja Jankovic #define SPI_CCTL_RESET 2
120*8d3dfb62SStrahinja Jankovic 
121*8d3dfb62SStrahinja Jankovic /* Burst count register */
122*8d3dfb62SStrahinja Jankovic #define SPI_BC_BC_MASK 0xFFFFFFu
123*8d3dfb62SStrahinja Jankovic #define SPI_BC_RESET   0
124*8d3dfb62SStrahinja Jankovic 
125*8d3dfb62SStrahinja Jankovic /* Transmi counter register */
126*8d3dfb62SStrahinja Jankovic #define SPI_TC_WTC_MASK 0xFFFFFFu
127*8d3dfb62SStrahinja Jankovic #define SPI_TC_RESET    0
128*8d3dfb62SStrahinja Jankovic 
129*8d3dfb62SStrahinja Jankovic /* FIFO status register */
130*8d3dfb62SStrahinja Jankovic #define SPI_FIFO_STA_CNT_MASK     0x7F
131*8d3dfb62SStrahinja Jankovic #define SPI_FIFO_STA_TF_CNT_SHIFT 16
132*8d3dfb62SStrahinja Jankovic #define SPI_FIFO_STA_RF_CNT_SHIFT 0
133*8d3dfb62SStrahinja Jankovic #define SPI_FIFO_STA_RESET        0
134*8d3dfb62SStrahinja Jankovic 
135*8d3dfb62SStrahinja Jankovic #define REG_INDEX(offset)         (offset / sizeof(uint32_t))
136*8d3dfb62SStrahinja Jankovic 
137*8d3dfb62SStrahinja Jankovic 
allwinner_a10_spi_get_regname(unsigned offset)138*8d3dfb62SStrahinja Jankovic static const char *allwinner_a10_spi_get_regname(unsigned offset)
139*8d3dfb62SStrahinja Jankovic {
140*8d3dfb62SStrahinja Jankovic     switch (offset) {
141*8d3dfb62SStrahinja Jankovic     case SPI_RXDATA_REG:
142*8d3dfb62SStrahinja Jankovic         return "RXDATA";
143*8d3dfb62SStrahinja Jankovic     case SPI_TXDATA_REG:
144*8d3dfb62SStrahinja Jankovic         return "TXDATA";
145*8d3dfb62SStrahinja Jankovic     case SPI_CTL_REG:
146*8d3dfb62SStrahinja Jankovic         return "CTL";
147*8d3dfb62SStrahinja Jankovic     case SPI_INTCTL_REG:
148*8d3dfb62SStrahinja Jankovic         return "INTCTL";
149*8d3dfb62SStrahinja Jankovic     case SPI_INT_STA_REG:
150*8d3dfb62SStrahinja Jankovic         return "INT_STA";
151*8d3dfb62SStrahinja Jankovic     case SPI_DMACTL_REG:
152*8d3dfb62SStrahinja Jankovic         return "DMACTL";
153*8d3dfb62SStrahinja Jankovic     case SPI_WAIT_REG:
154*8d3dfb62SStrahinja Jankovic         return "WAIT";
155*8d3dfb62SStrahinja Jankovic     case SPI_CCTL_REG:
156*8d3dfb62SStrahinja Jankovic         return "CCTL";
157*8d3dfb62SStrahinja Jankovic     case SPI_BC_REG:
158*8d3dfb62SStrahinja Jankovic         return "BC";
159*8d3dfb62SStrahinja Jankovic     case SPI_TC_REG:
160*8d3dfb62SStrahinja Jankovic         return "TC";
161*8d3dfb62SStrahinja Jankovic     case SPI_FIFO_STA_REG:
162*8d3dfb62SStrahinja Jankovic         return "FIFO_STA";
163*8d3dfb62SStrahinja Jankovic     default:
164*8d3dfb62SStrahinja Jankovic         return "[?]";
165*8d3dfb62SStrahinja Jankovic     }
166*8d3dfb62SStrahinja Jankovic }
167*8d3dfb62SStrahinja Jankovic 
allwinner_a10_spi_is_enabled(AWA10SPIState * s)168*8d3dfb62SStrahinja Jankovic static bool allwinner_a10_spi_is_enabled(AWA10SPIState *s)
169*8d3dfb62SStrahinja Jankovic {
170*8d3dfb62SStrahinja Jankovic     return s->regs[REG_INDEX(SPI_CTL_REG)] & SPI_CTL_EN;
171*8d3dfb62SStrahinja Jankovic }
172*8d3dfb62SStrahinja Jankovic 
allwinner_a10_spi_txfifo_reset(AWA10SPIState * s)173*8d3dfb62SStrahinja Jankovic static void allwinner_a10_spi_txfifo_reset(AWA10SPIState *s)
174*8d3dfb62SStrahinja Jankovic {
175*8d3dfb62SStrahinja Jankovic     fifo8_reset(&s->tx_fifo);
176*8d3dfb62SStrahinja Jankovic     s->regs[REG_INDEX(SPI_INT_STA_REG)] |= (SPI_INT_STA_TE | SPI_INT_STA_TE14 |
177*8d3dfb62SStrahinja Jankovic                                             SPI_INT_STA_THE | SPI_INT_STA_TE34);
178*8d3dfb62SStrahinja Jankovic     s->regs[REG_INDEX(SPI_INT_STA_REG)] &= ~(SPI_INT_STA_TU | SPI_INT_STA_TO);
179*8d3dfb62SStrahinja Jankovic }
180*8d3dfb62SStrahinja Jankovic 
allwinner_a10_spi_rxfifo_reset(AWA10SPIState * s)181*8d3dfb62SStrahinja Jankovic static void allwinner_a10_spi_rxfifo_reset(AWA10SPIState *s)
182*8d3dfb62SStrahinja Jankovic {
183*8d3dfb62SStrahinja Jankovic     fifo8_reset(&s->rx_fifo);
184*8d3dfb62SStrahinja Jankovic     s->regs[REG_INDEX(SPI_INT_STA_REG)] &=
185*8d3dfb62SStrahinja Jankovic         ~(SPI_INT_STA_RU | SPI_INT_STA_RO | SPI_INT_STA_RF | SPI_INT_STA_RR |
186*8d3dfb62SStrahinja Jankovic           SPI_INT_STA_RHF | SPI_INT_STA_RF14 | SPI_INT_STA_RF34);
187*8d3dfb62SStrahinja Jankovic }
188*8d3dfb62SStrahinja Jankovic 
allwinner_a10_spi_selected_channel(AWA10SPIState * s)189*8d3dfb62SStrahinja Jankovic static uint8_t allwinner_a10_spi_selected_channel(AWA10SPIState *s)
190*8d3dfb62SStrahinja Jankovic {
191*8d3dfb62SStrahinja Jankovic     return (s->regs[REG_INDEX(SPI_CTL_REG)] & SPI_CTL_SS) >> SPI_CTL_SS_SHIFT;
192*8d3dfb62SStrahinja Jankovic }
193*8d3dfb62SStrahinja Jankovic 
allwinner_a10_spi_reset_hold(Object * obj,ResetType type)194*8d3dfb62SStrahinja Jankovic static void allwinner_a10_spi_reset_hold(Object *obj, ResetType type)
195*8d3dfb62SStrahinja Jankovic {
196*8d3dfb62SStrahinja Jankovic     AWA10SPIState *s = AW_A10_SPI(obj);
197*8d3dfb62SStrahinja Jankovic 
198*8d3dfb62SStrahinja Jankovic     s->regs[REG_INDEX(SPI_RXDATA_REG)] = SPI_DATA_RESET;
199*8d3dfb62SStrahinja Jankovic     s->regs[REG_INDEX(SPI_TXDATA_REG)] = SPI_DATA_RESET;
200*8d3dfb62SStrahinja Jankovic     s->regs[REG_INDEX(SPI_CTL_REG)] = SPI_CTL_RESET;
201*8d3dfb62SStrahinja Jankovic     s->regs[REG_INDEX(SPI_INTCTL_REG)] = SPI_INTCTL_RESET;
202*8d3dfb62SStrahinja Jankovic     s->regs[REG_INDEX(SPI_INT_STA_REG)] = SPI_INT_STA_RESET;
203*8d3dfb62SStrahinja Jankovic     s->regs[REG_INDEX(SPI_DMACTL_REG)] = SPI_DMACTL_RESET;
204*8d3dfb62SStrahinja Jankovic     s->regs[REG_INDEX(SPI_WAIT_REG)] = SPI_WAIT_RESET;
205*8d3dfb62SStrahinja Jankovic     s->regs[REG_INDEX(SPI_CCTL_REG)] = SPI_CCTL_RESET;
206*8d3dfb62SStrahinja Jankovic     s->regs[REG_INDEX(SPI_BC_REG)] = SPI_BC_RESET;
207*8d3dfb62SStrahinja Jankovic     s->regs[REG_INDEX(SPI_TC_REG)] = SPI_TC_RESET;
208*8d3dfb62SStrahinja Jankovic     s->regs[REG_INDEX(SPI_FIFO_STA_REG)] = SPI_FIFO_STA_RESET;
209*8d3dfb62SStrahinja Jankovic 
210*8d3dfb62SStrahinja Jankovic     allwinner_a10_spi_txfifo_reset(s);
211*8d3dfb62SStrahinja Jankovic     allwinner_a10_spi_rxfifo_reset(s);
212*8d3dfb62SStrahinja Jankovic }
213*8d3dfb62SStrahinja Jankovic 
allwinner_a10_spi_update_irq(AWA10SPIState * s)214*8d3dfb62SStrahinja Jankovic static void allwinner_a10_spi_update_irq(AWA10SPIState *s)
215*8d3dfb62SStrahinja Jankovic {
216*8d3dfb62SStrahinja Jankovic     bool level;
217*8d3dfb62SStrahinja Jankovic 
218*8d3dfb62SStrahinja Jankovic     if (fifo8_is_empty(&s->rx_fifo)) {
219*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] &= ~SPI_INT_STA_RR;
220*8d3dfb62SStrahinja Jankovic     } else {
221*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] |= SPI_INT_STA_RR;
222*8d3dfb62SStrahinja Jankovic     }
223*8d3dfb62SStrahinja Jankovic 
224*8d3dfb62SStrahinja Jankovic     if (fifo8_num_used(&s->rx_fifo) >= (AW_A10_SPI_FIFO_SIZE >> 2)) {
225*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] |= SPI_INT_STA_RF14;
226*8d3dfb62SStrahinja Jankovic     } else {
227*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] &= ~SPI_INT_STA_RF14;
228*8d3dfb62SStrahinja Jankovic     }
229*8d3dfb62SStrahinja Jankovic 
230*8d3dfb62SStrahinja Jankovic     if (fifo8_num_used(&s->rx_fifo) >= (AW_A10_SPI_FIFO_SIZE >> 1)) {
231*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] |= SPI_INT_STA_RHF;
232*8d3dfb62SStrahinja Jankovic     } else {
233*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] &= ~SPI_INT_STA_RHF;
234*8d3dfb62SStrahinja Jankovic     }
235*8d3dfb62SStrahinja Jankovic 
236*8d3dfb62SStrahinja Jankovic     if (fifo8_num_free(&s->rx_fifo) <= (AW_A10_SPI_FIFO_SIZE >> 2)) {
237*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] |= SPI_INT_STA_RF34;
238*8d3dfb62SStrahinja Jankovic     } else {
239*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] &= ~SPI_INT_STA_RF34;
240*8d3dfb62SStrahinja Jankovic     }
241*8d3dfb62SStrahinja Jankovic 
242*8d3dfb62SStrahinja Jankovic     if (fifo8_is_full(&s->rx_fifo)) {
243*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] |= SPI_INT_STA_RF;
244*8d3dfb62SStrahinja Jankovic     } else {
245*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] &= ~SPI_INT_STA_RF;
246*8d3dfb62SStrahinja Jankovic     }
247*8d3dfb62SStrahinja Jankovic 
248*8d3dfb62SStrahinja Jankovic     if (fifo8_is_empty(&s->tx_fifo)) {
249*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] |= SPI_INT_STA_TE;
250*8d3dfb62SStrahinja Jankovic     } else {
251*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] &= ~SPI_INT_STA_TE;
252*8d3dfb62SStrahinja Jankovic     }
253*8d3dfb62SStrahinja Jankovic 
254*8d3dfb62SStrahinja Jankovic     if (fifo8_num_free(&s->tx_fifo) >= (AW_A10_SPI_FIFO_SIZE >> 2)) {
255*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] |= SPI_INT_STA_TE14;
256*8d3dfb62SStrahinja Jankovic     } else {
257*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] &= ~SPI_INT_STA_TE14;
258*8d3dfb62SStrahinja Jankovic     }
259*8d3dfb62SStrahinja Jankovic 
260*8d3dfb62SStrahinja Jankovic     if (fifo8_num_free(&s->tx_fifo) >= (AW_A10_SPI_FIFO_SIZE >> 1)) {
261*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] |= SPI_INT_STA_THE;
262*8d3dfb62SStrahinja Jankovic     } else {
263*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] &= ~SPI_INT_STA_THE;
264*8d3dfb62SStrahinja Jankovic     }
265*8d3dfb62SStrahinja Jankovic 
266*8d3dfb62SStrahinja Jankovic     if (fifo8_num_used(&s->tx_fifo) <= (AW_A10_SPI_FIFO_SIZE >> 2)) {
267*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] |= SPI_INT_STA_TE34;
268*8d3dfb62SStrahinja Jankovic     } else {
269*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] &= ~SPI_INT_STA_TE34;
270*8d3dfb62SStrahinja Jankovic     }
271*8d3dfb62SStrahinja Jankovic 
272*8d3dfb62SStrahinja Jankovic     if (fifo8_is_full(&s->rx_fifo)) {
273*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] |= SPI_INT_STA_TF;
274*8d3dfb62SStrahinja Jankovic     } else {
275*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] &= ~SPI_INT_STA_TF;
276*8d3dfb62SStrahinja Jankovic     }
277*8d3dfb62SStrahinja Jankovic 
278*8d3dfb62SStrahinja Jankovic     level = (s->regs[REG_INDEX(SPI_INT_STA_REG)] &
279*8d3dfb62SStrahinja Jankovic              s->regs[REG_INDEX(SPI_INTCTL_REG)]) != 0;
280*8d3dfb62SStrahinja Jankovic 
281*8d3dfb62SStrahinja Jankovic     qemu_set_irq(s->irq, level);
282*8d3dfb62SStrahinja Jankovic 
283*8d3dfb62SStrahinja Jankovic     trace_allwinner_a10_spi_update_irq(level);
284*8d3dfb62SStrahinja Jankovic }
285*8d3dfb62SStrahinja Jankovic 
allwinner_a10_spi_flush_txfifo(AWA10SPIState * s)286*8d3dfb62SStrahinja Jankovic static void allwinner_a10_spi_flush_txfifo(AWA10SPIState *s)
287*8d3dfb62SStrahinja Jankovic {
288*8d3dfb62SStrahinja Jankovic     uint32_t burst_count = s->regs[REG_INDEX(SPI_BC_REG)];
289*8d3dfb62SStrahinja Jankovic     uint32_t tx_burst = s->regs[REG_INDEX(SPI_TC_REG)];
290*8d3dfb62SStrahinja Jankovic     trace_allwinner_a10_spi_burst_length(tx_burst);
291*8d3dfb62SStrahinja Jankovic 
292*8d3dfb62SStrahinja Jankovic     trace_allwinner_a10_spi_flush_txfifo_begin(fifo8_num_used(&s->tx_fifo),
293*8d3dfb62SStrahinja Jankovic                                                fifo8_num_used(&s->rx_fifo));
294*8d3dfb62SStrahinja Jankovic 
295*8d3dfb62SStrahinja Jankovic     while (!fifo8_is_empty(&s->tx_fifo)) {
296*8d3dfb62SStrahinja Jankovic         uint8_t tx = fifo8_pop(&s->tx_fifo);
297*8d3dfb62SStrahinja Jankovic         uint8_t rx = 0;
298*8d3dfb62SStrahinja Jankovic         bool fill_rx = true;
299*8d3dfb62SStrahinja Jankovic 
300*8d3dfb62SStrahinja Jankovic         trace_allwinner_a10_spi_tx(tx);
301*8d3dfb62SStrahinja Jankovic 
302*8d3dfb62SStrahinja Jankovic         /* Write one byte at a time */
303*8d3dfb62SStrahinja Jankovic         rx = ssi_transfer(s->bus, tx);
304*8d3dfb62SStrahinja Jankovic 
305*8d3dfb62SStrahinja Jankovic         trace_allwinner_a10_spi_rx(rx);
306*8d3dfb62SStrahinja Jankovic 
307*8d3dfb62SStrahinja Jankovic         /* Check DHB here to determine if RX bytes should be stored */
308*8d3dfb62SStrahinja Jankovic         if (s->regs[REG_INDEX(SPI_CTL_REG)] & SPI_CTL_DHB) {
309*8d3dfb62SStrahinja Jankovic             /* Store rx bytes only after WTC transfers */
310*8d3dfb62SStrahinja Jankovic             if (tx_burst > 0u) {
311*8d3dfb62SStrahinja Jankovic                 fill_rx = false;
312*8d3dfb62SStrahinja Jankovic                 tx_burst--;
313*8d3dfb62SStrahinja Jankovic             }
314*8d3dfb62SStrahinja Jankovic         }
315*8d3dfb62SStrahinja Jankovic 
316*8d3dfb62SStrahinja Jankovic         if (fill_rx) {
317*8d3dfb62SStrahinja Jankovic             if (fifo8_is_full(&s->rx_fifo)) {
318*8d3dfb62SStrahinja Jankovic                 s->regs[REG_INDEX(SPI_INT_STA_REG)] |= SPI_INT_STA_RF;
319*8d3dfb62SStrahinja Jankovic             } else {
320*8d3dfb62SStrahinja Jankovic                 fifo8_push(&s->rx_fifo, rx);
321*8d3dfb62SStrahinja Jankovic             }
322*8d3dfb62SStrahinja Jankovic         }
323*8d3dfb62SStrahinja Jankovic 
324*8d3dfb62SStrahinja Jankovic         allwinner_a10_spi_update_irq(s);
325*8d3dfb62SStrahinja Jankovic 
326*8d3dfb62SStrahinja Jankovic         burst_count--;
327*8d3dfb62SStrahinja Jankovic 
328*8d3dfb62SStrahinja Jankovic         if (burst_count == 0) {
329*8d3dfb62SStrahinja Jankovic             s->regs[REG_INDEX(SPI_INT_STA_REG)] |= SPI_INT_STA_TC;
330*8d3dfb62SStrahinja Jankovic             s->regs[REG_INDEX(SPI_CTL_REG)] &= ~SPI_CTL_XCH;
331*8d3dfb62SStrahinja Jankovic             break;
332*8d3dfb62SStrahinja Jankovic         }
333*8d3dfb62SStrahinja Jankovic     }
334*8d3dfb62SStrahinja Jankovic 
335*8d3dfb62SStrahinja Jankovic     if (fifo8_is_empty(&s->tx_fifo)) {
336*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] |= SPI_INT_STA_TC;
337*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_CTL_REG)] &= ~SPI_CTL_XCH;
338*8d3dfb62SStrahinja Jankovic     }
339*8d3dfb62SStrahinja Jankovic 
340*8d3dfb62SStrahinja Jankovic     trace_allwinner_a10_spi_flush_txfifo_end(fifo8_num_used(&s->tx_fifo),
341*8d3dfb62SStrahinja Jankovic                                              fifo8_num_used(&s->rx_fifo));
342*8d3dfb62SStrahinja Jankovic }
343*8d3dfb62SStrahinja Jankovic 
allwinner_a10_spi_read(void * opaque,hwaddr offset,unsigned size)344*8d3dfb62SStrahinja Jankovic static uint64_t allwinner_a10_spi_read(void *opaque, hwaddr offset,
345*8d3dfb62SStrahinja Jankovic                                        unsigned size)
346*8d3dfb62SStrahinja Jankovic {
347*8d3dfb62SStrahinja Jankovic     uint32_t value = 0;
348*8d3dfb62SStrahinja Jankovic     AWA10SPIState *s = opaque;
349*8d3dfb62SStrahinja Jankovic     uint32_t index = offset >> 2;
350*8d3dfb62SStrahinja Jankovic 
351*8d3dfb62SStrahinja Jankovic     if (offset > SPI_FIFO_STA_REG) {
352*8d3dfb62SStrahinja Jankovic         qemu_log_mask(LOG_GUEST_ERROR,
353*8d3dfb62SStrahinja Jankovic                       "[%s]%s: Bad register at offset 0x%" HWADDR_PRIx "\n",
354*8d3dfb62SStrahinja Jankovic                       TYPE_AW_A10_SPI, __func__, offset);
355*8d3dfb62SStrahinja Jankovic         return 0;
356*8d3dfb62SStrahinja Jankovic     }
357*8d3dfb62SStrahinja Jankovic 
358*8d3dfb62SStrahinja Jankovic     value = s->regs[index];
359*8d3dfb62SStrahinja Jankovic 
360*8d3dfb62SStrahinja Jankovic     if (allwinner_a10_spi_is_enabled(s)) {
361*8d3dfb62SStrahinja Jankovic         switch (offset) {
362*8d3dfb62SStrahinja Jankovic         case SPI_RXDATA_REG:
363*8d3dfb62SStrahinja Jankovic             if (fifo8_is_empty(&s->rx_fifo)) {
364*8d3dfb62SStrahinja Jankovic                 /* value is undefined */
365*8d3dfb62SStrahinja Jankovic                 value = 0xdeadbeef;
366*8d3dfb62SStrahinja Jankovic             } else {
367*8d3dfb62SStrahinja Jankovic                 /* read from the RX FIFO */
368*8d3dfb62SStrahinja Jankovic                 value = fifo8_pop(&s->rx_fifo);
369*8d3dfb62SStrahinja Jankovic             }
370*8d3dfb62SStrahinja Jankovic             break;
371*8d3dfb62SStrahinja Jankovic         case SPI_TXDATA_REG:
372*8d3dfb62SStrahinja Jankovic             qemu_log_mask(LOG_GUEST_ERROR,
373*8d3dfb62SStrahinja Jankovic                           "[%s]%s: Trying to read from TX FIFO\n",
374*8d3dfb62SStrahinja Jankovic                           TYPE_AW_A10_SPI, __func__);
375*8d3dfb62SStrahinja Jankovic 
376*8d3dfb62SStrahinja Jankovic             /* Reading from TXDATA gives 0 */
377*8d3dfb62SStrahinja Jankovic             break;
378*8d3dfb62SStrahinja Jankovic         case SPI_FIFO_STA_REG:
379*8d3dfb62SStrahinja Jankovic             /* Read current tx/rx fifo data count */
380*8d3dfb62SStrahinja Jankovic             value = fifo8_num_used(&s->tx_fifo) << SPI_FIFO_STA_TF_CNT_SHIFT |
381*8d3dfb62SStrahinja Jankovic                     fifo8_num_used(&s->rx_fifo) << SPI_FIFO_STA_RF_CNT_SHIFT;
382*8d3dfb62SStrahinja Jankovic             break;
383*8d3dfb62SStrahinja Jankovic         case SPI_CTL_REG:
384*8d3dfb62SStrahinja Jankovic         case SPI_INTCTL_REG:
385*8d3dfb62SStrahinja Jankovic         case SPI_INT_STA_REG:
386*8d3dfb62SStrahinja Jankovic         case SPI_DMACTL_REG:
387*8d3dfb62SStrahinja Jankovic         case SPI_WAIT_REG:
388*8d3dfb62SStrahinja Jankovic         case SPI_CCTL_REG:
389*8d3dfb62SStrahinja Jankovic         case SPI_BC_REG:
390*8d3dfb62SStrahinja Jankovic         case SPI_TC_REG:
391*8d3dfb62SStrahinja Jankovic             break;
392*8d3dfb62SStrahinja Jankovic         default:
393*8d3dfb62SStrahinja Jankovic             qemu_log_mask(LOG_GUEST_ERROR,
394*8d3dfb62SStrahinja Jankovic                     "%s: bad offset 0x%x\n", __func__,
395*8d3dfb62SStrahinja Jankovic                     (uint32_t)offset);
396*8d3dfb62SStrahinja Jankovic             break;
397*8d3dfb62SStrahinja Jankovic         }
398*8d3dfb62SStrahinja Jankovic 
399*8d3dfb62SStrahinja Jankovic         allwinner_a10_spi_update_irq(s);
400*8d3dfb62SStrahinja Jankovic     }
401*8d3dfb62SStrahinja Jankovic     trace_allwinner_a10_spi_read(allwinner_a10_spi_get_regname(offset), value);
402*8d3dfb62SStrahinja Jankovic 
403*8d3dfb62SStrahinja Jankovic     return value;
404*8d3dfb62SStrahinja Jankovic }
405*8d3dfb62SStrahinja Jankovic 
allwinner_a10_spi_update_cs_level(AWA10SPIState * s,int cs_line_nr)406*8d3dfb62SStrahinja Jankovic static bool allwinner_a10_spi_update_cs_level(AWA10SPIState *s, int cs_line_nr)
407*8d3dfb62SStrahinja Jankovic {
408*8d3dfb62SStrahinja Jankovic     if (cs_line_nr == allwinner_a10_spi_selected_channel(s)) {
409*8d3dfb62SStrahinja Jankovic         return (s->regs[REG_INDEX(SPI_CTL_REG)] & SPI_CTL_SS_LEVEL) != 0;
410*8d3dfb62SStrahinja Jankovic     } else {
411*8d3dfb62SStrahinja Jankovic         return (s->regs[REG_INDEX(SPI_CTL_REG)] & SPI_CTL_SSPOL) != 0;
412*8d3dfb62SStrahinja Jankovic     }
413*8d3dfb62SStrahinja Jankovic }
414*8d3dfb62SStrahinja Jankovic 
allwinner_a10_spi_write(void * opaque,hwaddr offset,uint64_t value,unsigned size)415*8d3dfb62SStrahinja Jankovic static void allwinner_a10_spi_write(void *opaque, hwaddr offset, uint64_t value,
416*8d3dfb62SStrahinja Jankovic                                     unsigned size)
417*8d3dfb62SStrahinja Jankovic {
418*8d3dfb62SStrahinja Jankovic     AWA10SPIState *s = opaque;
419*8d3dfb62SStrahinja Jankovic     uint32_t index = offset >> 2;
420*8d3dfb62SStrahinja Jankovic     int i = 0;
421*8d3dfb62SStrahinja Jankovic 
422*8d3dfb62SStrahinja Jankovic     if (offset > SPI_FIFO_STA_REG) {
423*8d3dfb62SStrahinja Jankovic         qemu_log_mask(LOG_GUEST_ERROR,
424*8d3dfb62SStrahinja Jankovic                       "[%s]%s: Bad register at offset 0x%" HWADDR_PRIx "\n",
425*8d3dfb62SStrahinja Jankovic                       TYPE_AW_A10_SPI, __func__, offset);
426*8d3dfb62SStrahinja Jankovic         return;
427*8d3dfb62SStrahinja Jankovic     }
428*8d3dfb62SStrahinja Jankovic 
429*8d3dfb62SStrahinja Jankovic     trace_allwinner_a10_spi_write(allwinner_a10_spi_get_regname(offset),
430*8d3dfb62SStrahinja Jankovic                                   (uint32_t)value);
431*8d3dfb62SStrahinja Jankovic 
432*8d3dfb62SStrahinja Jankovic     if (!allwinner_a10_spi_is_enabled(s)) {
433*8d3dfb62SStrahinja Jankovic         /* Block is disabled */
434*8d3dfb62SStrahinja Jankovic         if (offset != SPI_CTL_REG) {
435*8d3dfb62SStrahinja Jankovic             /* Ignore access */
436*8d3dfb62SStrahinja Jankovic             return;
437*8d3dfb62SStrahinja Jankovic         }
438*8d3dfb62SStrahinja Jankovic     }
439*8d3dfb62SStrahinja Jankovic 
440*8d3dfb62SStrahinja Jankovic     switch (offset) {
441*8d3dfb62SStrahinja Jankovic     case SPI_RXDATA_REG:
442*8d3dfb62SStrahinja Jankovic         qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Trying to write to RX FIFO\n",
443*8d3dfb62SStrahinja Jankovic                       TYPE_AW_A10_SPI, __func__);
444*8d3dfb62SStrahinja Jankovic         break;
445*8d3dfb62SStrahinja Jankovic     case SPI_TXDATA_REG:
446*8d3dfb62SStrahinja Jankovic         if (fifo8_is_full(&s->tx_fifo)) {
447*8d3dfb62SStrahinja Jankovic             /* Ignore writes if queue is full */
448*8d3dfb62SStrahinja Jankovic             break;
449*8d3dfb62SStrahinja Jankovic         }
450*8d3dfb62SStrahinja Jankovic 
451*8d3dfb62SStrahinja Jankovic         fifo8_push(&s->tx_fifo, (uint8_t)value);
452*8d3dfb62SStrahinja Jankovic 
453*8d3dfb62SStrahinja Jankovic         break;
454*8d3dfb62SStrahinja Jankovic     case SPI_INT_STA_REG:
455*8d3dfb62SStrahinja Jankovic         /* Handle W1C bits - everything except SPI_INT_STA_INT_CBF. */
456*8d3dfb62SStrahinja Jankovic         value &= ~SPI_INT_STA_INT_CBF;
457*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_INT_STA_REG)] &= ~(value & SPI_INT_STA_MASK);
458*8d3dfb62SStrahinja Jankovic         break;
459*8d3dfb62SStrahinja Jankovic     case SPI_CTL_REG:
460*8d3dfb62SStrahinja Jankovic         s->regs[REG_INDEX(SPI_CTL_REG)] = value;
461*8d3dfb62SStrahinja Jankovic 
462*8d3dfb62SStrahinja Jankovic         for (i = 0; i < AW_A10_SPI_CS_LINES_NR; i++) {
463*8d3dfb62SStrahinja Jankovic             qemu_set_irq(
464*8d3dfb62SStrahinja Jankovic                 s->cs_lines[i],
465*8d3dfb62SStrahinja Jankovic                 allwinner_a10_spi_update_cs_level(s, i));
466*8d3dfb62SStrahinja Jankovic         }
467*8d3dfb62SStrahinja Jankovic 
468*8d3dfb62SStrahinja Jankovic         if (s->regs[REG_INDEX(SPI_CTL_REG)] & SPI_CTL_XCH) {
469*8d3dfb62SStrahinja Jankovic             /* Request to start emitting */
470*8d3dfb62SStrahinja Jankovic             allwinner_a10_spi_flush_txfifo(s);
471*8d3dfb62SStrahinja Jankovic         }
472*8d3dfb62SStrahinja Jankovic         if (s->regs[REG_INDEX(SPI_CTL_REG)] & SPI_CTL_TF_RST) {
473*8d3dfb62SStrahinja Jankovic             allwinner_a10_spi_txfifo_reset(s);
474*8d3dfb62SStrahinja Jankovic             s->regs[REG_INDEX(SPI_CTL_REG)] &= ~SPI_CTL_TF_RST;
475*8d3dfb62SStrahinja Jankovic         }
476*8d3dfb62SStrahinja Jankovic         if (s->regs[REG_INDEX(SPI_CTL_REG)] & SPI_CTL_RF_RST) {
477*8d3dfb62SStrahinja Jankovic             allwinner_a10_spi_rxfifo_reset(s);
478*8d3dfb62SStrahinja Jankovic             s->regs[REG_INDEX(SPI_CTL_REG)] &= ~SPI_CTL_RF_RST;
479*8d3dfb62SStrahinja Jankovic         }
480*8d3dfb62SStrahinja Jankovic         break;
481*8d3dfb62SStrahinja Jankovic     case SPI_INTCTL_REG:
482*8d3dfb62SStrahinja Jankovic     case SPI_DMACTL_REG:
483*8d3dfb62SStrahinja Jankovic     case SPI_WAIT_REG:
484*8d3dfb62SStrahinja Jankovic     case SPI_CCTL_REG:
485*8d3dfb62SStrahinja Jankovic     case SPI_BC_REG:
486*8d3dfb62SStrahinja Jankovic     case SPI_TC_REG:
487*8d3dfb62SStrahinja Jankovic     case SPI_FIFO_STA_REG:
488*8d3dfb62SStrahinja Jankovic         s->regs[index] = value;
489*8d3dfb62SStrahinja Jankovic         break;
490*8d3dfb62SStrahinja Jankovic     default:
491*8d3dfb62SStrahinja Jankovic         qemu_log_mask(LOG_GUEST_ERROR,
492*8d3dfb62SStrahinja Jankovic             "%s: bad offset 0x%x\n", __func__,
493*8d3dfb62SStrahinja Jankovic             (uint32_t)offset);
494*8d3dfb62SStrahinja Jankovic         break;
495*8d3dfb62SStrahinja Jankovic     }
496*8d3dfb62SStrahinja Jankovic 
497*8d3dfb62SStrahinja Jankovic     allwinner_a10_spi_update_irq(s);
498*8d3dfb62SStrahinja Jankovic }
499*8d3dfb62SStrahinja Jankovic 
500*8d3dfb62SStrahinja Jankovic static const MemoryRegionOps allwinner_a10_spi_ops = {
501*8d3dfb62SStrahinja Jankovic     .read = allwinner_a10_spi_read,
502*8d3dfb62SStrahinja Jankovic     .write = allwinner_a10_spi_write,
503*8d3dfb62SStrahinja Jankovic     .valid.min_access_size = 1,
504*8d3dfb62SStrahinja Jankovic     .valid.max_access_size = 4,
505*8d3dfb62SStrahinja Jankovic     .endianness = DEVICE_NATIVE_ENDIAN,
506*8d3dfb62SStrahinja Jankovic };
507*8d3dfb62SStrahinja Jankovic 
508*8d3dfb62SStrahinja Jankovic static const VMStateDescription allwinner_a10_spi_vmstate = {
509*8d3dfb62SStrahinja Jankovic     .name = TYPE_AW_A10_SPI,
510*8d3dfb62SStrahinja Jankovic     .version_id = 1,
511*8d3dfb62SStrahinja Jankovic     .minimum_version_id = 1,
512*8d3dfb62SStrahinja Jankovic     .fields = (const VMStateField[]) {
513*8d3dfb62SStrahinja Jankovic         VMSTATE_FIFO8(tx_fifo, AWA10SPIState),
514*8d3dfb62SStrahinja Jankovic         VMSTATE_FIFO8(rx_fifo, AWA10SPIState),
515*8d3dfb62SStrahinja Jankovic         VMSTATE_UINT32_ARRAY(regs, AWA10SPIState, AW_A10_SPI_REGS_NUM),
516*8d3dfb62SStrahinja Jankovic         VMSTATE_END_OF_LIST()
517*8d3dfb62SStrahinja Jankovic     }
518*8d3dfb62SStrahinja Jankovic };
519*8d3dfb62SStrahinja Jankovic 
allwinner_a10_spi_realize(DeviceState * dev,Error ** errp)520*8d3dfb62SStrahinja Jankovic static void allwinner_a10_spi_realize(DeviceState *dev, Error **errp)
521*8d3dfb62SStrahinja Jankovic {
522*8d3dfb62SStrahinja Jankovic     AWA10SPIState *s = AW_A10_SPI(dev);
523*8d3dfb62SStrahinja Jankovic     int i = 0;
524*8d3dfb62SStrahinja Jankovic 
525*8d3dfb62SStrahinja Jankovic     memory_region_init_io(&s->iomem, OBJECT(s), &allwinner_a10_spi_ops, s,
526*8d3dfb62SStrahinja Jankovic                           TYPE_AW_A10_SPI, AW_A10_SPI_IOSIZE);
527*8d3dfb62SStrahinja Jankovic     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
528*8d3dfb62SStrahinja Jankovic     sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq);
529*8d3dfb62SStrahinja Jankovic 
530*8d3dfb62SStrahinja Jankovic     s->bus = ssi_create_bus(dev, "spi");
531*8d3dfb62SStrahinja Jankovic     for (i = 0; i < AW_A10_SPI_CS_LINES_NR; i++) {
532*8d3dfb62SStrahinja Jankovic         sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->cs_lines[i]);
533*8d3dfb62SStrahinja Jankovic     }
534*8d3dfb62SStrahinja Jankovic     fifo8_create(&s->tx_fifo, AW_A10_SPI_FIFO_SIZE);
535*8d3dfb62SStrahinja Jankovic     fifo8_create(&s->rx_fifo, AW_A10_SPI_FIFO_SIZE);
536*8d3dfb62SStrahinja Jankovic }
537*8d3dfb62SStrahinja Jankovic 
allwinner_a10_spi_class_init(ObjectClass * klass,void * data)538*8d3dfb62SStrahinja Jankovic static void allwinner_a10_spi_class_init(ObjectClass *klass, void *data)
539*8d3dfb62SStrahinja Jankovic {
540*8d3dfb62SStrahinja Jankovic     DeviceClass *dc = DEVICE_CLASS(klass);
541*8d3dfb62SStrahinja Jankovic     ResettableClass *rc = RESETTABLE_CLASS(klass);
542*8d3dfb62SStrahinja Jankovic 
543*8d3dfb62SStrahinja Jankovic     rc->phases.hold = allwinner_a10_spi_reset_hold;
544*8d3dfb62SStrahinja Jankovic     dc->vmsd = &allwinner_a10_spi_vmstate;
545*8d3dfb62SStrahinja Jankovic     dc->realize = allwinner_a10_spi_realize;
546*8d3dfb62SStrahinja Jankovic     dc->desc = "Allwinner A10 SPI Controller";
547*8d3dfb62SStrahinja Jankovic }
548*8d3dfb62SStrahinja Jankovic 
549*8d3dfb62SStrahinja Jankovic static const TypeInfo allwinner_a10_spi_type_info = {
550*8d3dfb62SStrahinja Jankovic     .name = TYPE_AW_A10_SPI,
551*8d3dfb62SStrahinja Jankovic     .parent = TYPE_SYS_BUS_DEVICE,
552*8d3dfb62SStrahinja Jankovic     .instance_size = sizeof(AWA10SPIState),
553*8d3dfb62SStrahinja Jankovic     .class_init = allwinner_a10_spi_class_init,
554*8d3dfb62SStrahinja Jankovic };
555*8d3dfb62SStrahinja Jankovic 
allwinner_a10_spi_register_types(void)556*8d3dfb62SStrahinja Jankovic static void allwinner_a10_spi_register_types(void)
557*8d3dfb62SStrahinja Jankovic {
558*8d3dfb62SStrahinja Jankovic     type_register_static(&allwinner_a10_spi_type_info);
559*8d3dfb62SStrahinja Jankovic }
560*8d3dfb62SStrahinja Jankovic 
561*8d3dfb62SStrahinja Jankovic type_init(allwinner_a10_spi_register_types)
562