2 * TI OMAP processor's Multichannel SPI emulation.
4 * Copyright (C) 2007-2009 Nokia Corporation
6 * Original code for OMAP2 by Andrzej Zaborowski <andrew@openedhand.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 or
11 * (at your option) any later version of the License.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "qemu/osdep.h"
25 #include "hw/arm/omap.h"
27 /* Multichannel SPI */
40 struct omap_mcspi_ch_s
{
43 uint32_t (*txrx
)(void *opaque
, uint32_t, int);
55 static inline void omap_mcspi_interrupt_update(struct omap_mcspi_s
*s
)
57 qemu_set_irq(s
->irq
, s
->irqst
& s
->irqen
);
60 static inline void omap_mcspi_dmarequest_update(struct omap_mcspi_ch_s
*ch
)
62 qemu_set_irq(ch
->txdrq
,
63 (ch
->control
& 1) && /* EN */
64 (ch
->config
& (1 << 14)) && /* DMAW */
65 (ch
->status
& (1 << 1)) && /* TXS */
66 ((ch
->config
>> 12) & 3) != 1); /* TRM */
67 qemu_set_irq(ch
->rxdrq
,
68 (ch
->control
& 1) && /* EN */
69 (ch
->config
& (1 << 15)) && /* DMAW */
70 (ch
->status
& (1 << 0)) && /* RXS */
71 ((ch
->config
>> 12) & 3) != 2); /* TRM */
74 static void omap_mcspi_transfer_run(struct omap_mcspi_s
*s
, int chnum
)
76 struct omap_mcspi_ch_s
*ch
= s
->ch
+ chnum
;
78 if (!(ch
->control
& 1)) /* EN */
80 if ((ch
->status
& (1 << 0)) && /* RXS */
81 ((ch
->config
>> 12) & 3) != 2 && /* TRM */
82 !(ch
->config
& (1 << 19))) /* TURBO */
84 if ((ch
->status
& (1 << 1)) && /* TXS */
85 ((ch
->config
>> 12) & 3) != 1) /* TRM */
88 if (!(s
->control
& 1) || /* SINGLE */
89 (ch
->config
& (1 << 20))) { /* FORCE */
91 ch
->rx
= ch
->txrx(ch
->opaque
, ch
->tx
, /* WL */
92 1 + (0x1f & (ch
->config
>> 7)));
96 ch
->status
|= 1 << 2; /* EOT */
97 ch
->status
|= 1 << 1; /* TXS */
98 if (((ch
->config
>> 12) & 3) != 2) /* TRM */
99 ch
->status
|= 1 << 0; /* RXS */
102 if ((ch
->status
& (1 << 0)) && /* RXS */
103 ((ch
->config
>> 12) & 3) != 2 && /* TRM */
104 !(ch
->config
& (1 << 19))) /* TURBO */
105 s
->irqst
|= 1 << (2 + 4 * chnum
); /* RX_FULL */
106 if ((ch
->status
& (1 << 1)) && /* TXS */
107 ((ch
->config
>> 12) & 3) != 1) /* TRM */
108 s
->irqst
|= 1 << (0 + 4 * chnum
); /* TX_EMPTY */
109 omap_mcspi_interrupt_update(s
);
110 omap_mcspi_dmarequest_update(ch
);
113 void omap_mcspi_reset(struct omap_mcspi_s
*s
)
124 for (ch
= 0; ch
< 4; ch
++) {
125 s
->ch
[ch
].config
= 0x060000;
126 s
->ch
[ch
].status
= 2; /* TXS */
127 s
->ch
[ch
].control
= 0;
129 omap_mcspi_dmarequest_update(s
->ch
+ ch
);
132 omap_mcspi_interrupt_update(s
);
135 static uint64_t omap_mcspi_read(void *opaque
, hwaddr addr
,
138 struct omap_mcspi_s
*s
= (struct omap_mcspi_s
*) opaque
;
143 return omap_badwidth_read32(opaque
, addr
);
147 case 0x00: /* MCSPI_REVISION */
150 case 0x10: /* MCSPI_SYSCONFIG */
153 case 0x14: /* MCSPI_SYSSTATUS */
154 return 1; /* RESETDONE */
156 case 0x18: /* MCSPI_IRQSTATUS */
159 case 0x1c: /* MCSPI_IRQENABLE */
162 case 0x20: /* MCSPI_WAKEUPENABLE */
165 case 0x24: /* MCSPI_SYST */
168 case 0x28: /* MCSPI_MODULCTRL */
177 case 0x2c: /* MCSPI_CHCONF */
178 return s
->ch
[ch
].config
;
186 case 0x30: /* MCSPI_CHSTAT */
187 return s
->ch
[ch
].status
;
195 case 0x34: /* MCSPI_CHCTRL */
196 return s
->ch
[ch
].control
;
204 case 0x38: /* MCSPI_TX */
213 case 0x3c: /* MCSPI_RX */
214 s
->ch
[ch
].status
&= ~(1 << 0); /* RXS */
216 omap_mcspi_transfer_run(s
, ch
);
224 static void omap_mcspi_write(void *opaque
, hwaddr addr
,
225 uint64_t value
, unsigned size
)
227 struct omap_mcspi_s
*s
= (struct omap_mcspi_s
*) opaque
;
231 omap_badwidth_write32(opaque
, addr
, value
);
236 case 0x00: /* MCSPI_REVISION */
237 case 0x14: /* MCSPI_SYSSTATUS */
238 case 0x30: /* MCSPI_CHSTAT0 */
239 case 0x3c: /* MCSPI_RX0 */
240 case 0x44: /* MCSPI_CHSTAT1 */
241 case 0x50: /* MCSPI_RX1 */
242 case 0x58: /* MCSPI_CHSTAT2 */
243 case 0x64: /* MCSPI_RX2 */
244 case 0x6c: /* MCSPI_CHSTAT3 */
245 case 0x78: /* MCSPI_RX3 */
249 case 0x10: /* MCSPI_SYSCONFIG */
250 if (value
& (1 << 1)) /* SOFTRESET */
252 s
->sysconfig
= value
& 0x31d;
255 case 0x18: /* MCSPI_IRQSTATUS */
256 if (!((s
->control
& (1 << 3)) && (s
->systest
& (1 << 11)))) {
258 omap_mcspi_interrupt_update(s
);
262 case 0x1c: /* MCSPI_IRQENABLE */
263 s
->irqen
= value
& 0x1777f;
264 omap_mcspi_interrupt_update(s
);
267 case 0x20: /* MCSPI_WAKEUPENABLE */
271 case 0x24: /* MCSPI_SYST */
272 if (s
->control
& (1 << 3)) /* SYSTEM_TEST */
273 if (value
& (1 << 11)) { /* SSB */
275 omap_mcspi_interrupt_update(s
);
277 s
->systest
= value
& 0xfff;
280 case 0x28: /* MCSPI_MODULCTRL */
281 if (value
& (1 << 3)) /* SYSTEM_TEST */
282 if (s
->systest
& (1 << 11)) { /* SSB */
284 omap_mcspi_interrupt_update(s
);
286 s
->control
= value
& 0xf;
295 case 0x2c: /* MCSPI_CHCONF */
296 if ((value
^ s
->ch
[ch
].config
) & (3 << 14)) /* DMAR | DMAW */
297 omap_mcspi_dmarequest_update(s
->ch
+ ch
);
298 if (((value
>> 12) & 3) == 3) { /* TRM */
299 qemu_log_mask(LOG_GUEST_ERROR
, "%s: invalid TRM value (3)\n",
302 if (((value
>> 7) & 0x1f) < 3) { /* WL */
303 qemu_log_mask(LOG_GUEST_ERROR
,
304 "%s: invalid WL value (%" PRIx64
")\n",
305 __func__
, (value
>> 7) & 0x1f);
307 s
->ch
[ch
].config
= value
& 0x7fffff;
316 case 0x34: /* MCSPI_CHCTRL */
317 if (value
& ~s
->ch
[ch
].control
& 1) { /* EN */
318 s
->ch
[ch
].control
|= 1;
319 omap_mcspi_transfer_run(s
, ch
);
321 s
->ch
[ch
].control
= value
& 1;
330 case 0x38: /* MCSPI_TX */
331 s
->ch
[ch
].tx
= value
;
332 s
->ch
[ch
].status
&= ~(1 << 1); /* TXS */
333 omap_mcspi_transfer_run(s
, ch
);
342 static const MemoryRegionOps omap_mcspi_ops
= {
343 .read
= omap_mcspi_read
,
344 .write
= omap_mcspi_write
,
345 .endianness
= DEVICE_NATIVE_ENDIAN
,
348 struct omap_mcspi_s
*omap_mcspi_init(struct omap_target_agent_s
*ta
, int chnum
,
349 qemu_irq irq
, qemu_irq
*drq
, omap_clk fclk
, omap_clk iclk
)
351 struct omap_mcspi_s
*s
= g_new0(struct omap_mcspi_s
, 1);
352 struct omap_mcspi_ch_s
*ch
= s
->ch
;
363 memory_region_init_io(&s
->iomem
, NULL
, &omap_mcspi_ops
, s
, "omap.mcspi",
364 omap_l4_region_size(ta
, 0));
365 omap_l4_attach(ta
, 0, &s
->iomem
);
370 void omap_mcspi_attach(struct omap_mcspi_s
*s
,
371 uint32_t (*txrx
)(void *opaque
, uint32_t, int), void *opaque
,
374 if (chipselect
< 0 || chipselect
>= s
->chnum
)
375 hw_error("%s: Bad chipselect %i\n", __func__
, chipselect
);
377 s
->ch
[chipselect
].txrx
= txrx
;
378 s
->ch
[chipselect
].opaque
= opaque
;