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.
23 #include "qemu/osdep.h"
27 #include "hw/arm/omap.h"
29 /* Multichannel SPI */
42 struct omap_mcspi_ch_s
{
45 uint32_t (*txrx
)(void *opaque
, uint32_t, int);
57 static inline void omap_mcspi_interrupt_update(struct omap_mcspi_s
*s
)
59 qemu_set_irq(s
->irq
, s
->irqst
& s
->irqen
);
62 static inline void omap_mcspi_dmarequest_update(struct omap_mcspi_ch_s
*ch
)
64 qemu_set_irq(ch
->txdrq
,
65 (ch
->control
& 1) && /* EN */
66 (ch
->config
& (1 << 14)) && /* DMAW */
67 (ch
->status
& (1 << 1)) && /* TXS */
68 ((ch
->config
>> 12) & 3) != 1); /* TRM */
69 qemu_set_irq(ch
->rxdrq
,
70 (ch
->control
& 1) && /* EN */
71 (ch
->config
& (1 << 15)) && /* DMAW */
72 (ch
->status
& (1 << 0)) && /* RXS */
73 ((ch
->config
>> 12) & 3) != 2); /* TRM */
76 static void omap_mcspi_transfer_run(struct omap_mcspi_s
*s
, int chnum
)
78 struct omap_mcspi_ch_s
*ch
= s
->ch
+ chnum
;
80 if (!(ch
->control
& 1)) /* EN */
82 if ((ch
->status
& (1 << 0)) && /* RXS */
83 ((ch
->config
>> 12) & 3) != 2 && /* TRM */
84 !(ch
->config
& (1 << 19))) /* TURBO */
86 if ((ch
->status
& (1 << 1)) && /* TXS */
87 ((ch
->config
>> 12) & 3) != 1) /* TRM */
90 if (!(s
->control
& 1) || /* SINGLE */
91 (ch
->config
& (1 << 20))) { /* FORCE */
93 ch
->rx
= ch
->txrx(ch
->opaque
, ch
->tx
, /* WL */
94 1 + (0x1f & (ch
->config
>> 7)));
98 ch
->status
|= 1 << 2; /* EOT */
99 ch
->status
|= 1 << 1; /* TXS */
100 if (((ch
->config
>> 12) & 3) != 2) /* TRM */
101 ch
->status
|= 1 << 0; /* RXS */
104 if ((ch
->status
& (1 << 0)) && /* RXS */
105 ((ch
->config
>> 12) & 3) != 2 && /* TRM */
106 !(ch
->config
& (1 << 19))) /* TURBO */
107 s
->irqst
|= 1 << (2 + 4 * chnum
); /* RX_FULL */
108 if ((ch
->status
& (1 << 1)) && /* TXS */
109 ((ch
->config
>> 12) & 3) != 1) /* TRM */
110 s
->irqst
|= 1 << (0 + 4 * chnum
); /* TX_EMPTY */
111 omap_mcspi_interrupt_update(s
);
112 omap_mcspi_dmarequest_update(ch
);
115 void omap_mcspi_reset(struct omap_mcspi_s
*s
)
126 for (ch
= 0; ch
< 4; ch
++) {
127 s
->ch
[ch
].config
= 0x060000;
128 s
->ch
[ch
].status
= 2; /* TXS */
129 s
->ch
[ch
].control
= 0;
131 omap_mcspi_dmarequest_update(s
->ch
+ ch
);
134 omap_mcspi_interrupt_update(s
);
137 static uint64_t omap_mcspi_read(void *opaque
, hwaddr addr
,
140 struct omap_mcspi_s
*s
= (struct omap_mcspi_s
*) opaque
;
145 return omap_badwidth_read32(opaque
, addr
);
149 case 0x00: /* MCSPI_REVISION */
152 case 0x10: /* MCSPI_SYSCONFIG */
155 case 0x14: /* MCSPI_SYSSTATUS */
156 return 1; /* RESETDONE */
158 case 0x18: /* MCSPI_IRQSTATUS */
161 case 0x1c: /* MCSPI_IRQENABLE */
164 case 0x20: /* MCSPI_WAKEUPENABLE */
167 case 0x24: /* MCSPI_SYST */
170 case 0x28: /* MCSPI_MODULCTRL */
179 case 0x2c: /* MCSPI_CHCONF */
180 return s
->ch
[ch
].config
;
188 case 0x30: /* MCSPI_CHSTAT */
189 return s
->ch
[ch
].status
;
197 case 0x34: /* MCSPI_CHCTRL */
198 return s
->ch
[ch
].control
;
206 case 0x38: /* MCSPI_TX */
215 case 0x3c: /* MCSPI_RX */
216 s
->ch
[ch
].status
&= ~(1 << 0); /* RXS */
218 omap_mcspi_transfer_run(s
, ch
);
226 static void omap_mcspi_write(void *opaque
, hwaddr addr
,
227 uint64_t value
, unsigned size
)
229 struct omap_mcspi_s
*s
= (struct omap_mcspi_s
*) opaque
;
233 omap_badwidth_write32(opaque
, addr
, value
);
238 case 0x00: /* MCSPI_REVISION */
239 case 0x14: /* MCSPI_SYSSTATUS */
240 case 0x30: /* MCSPI_CHSTAT0 */
241 case 0x3c: /* MCSPI_RX0 */
242 case 0x44: /* MCSPI_CHSTAT1 */
243 case 0x50: /* MCSPI_RX1 */
244 case 0x58: /* MCSPI_CHSTAT2 */
245 case 0x64: /* MCSPI_RX2 */
246 case 0x6c: /* MCSPI_CHSTAT3 */
247 case 0x78: /* MCSPI_RX3 */
251 case 0x10: /* MCSPI_SYSCONFIG */
252 if (value
& (1 << 1)) /* SOFTRESET */
254 s
->sysconfig
= value
& 0x31d;
257 case 0x18: /* MCSPI_IRQSTATUS */
258 if (!((s
->control
& (1 << 3)) && (s
->systest
& (1 << 11)))) {
260 omap_mcspi_interrupt_update(s
);
264 case 0x1c: /* MCSPI_IRQENABLE */
265 s
->irqen
= value
& 0x1777f;
266 omap_mcspi_interrupt_update(s
);
269 case 0x20: /* MCSPI_WAKEUPENABLE */
273 case 0x24: /* MCSPI_SYST */
274 if (s
->control
& (1 << 3)) /* SYSTEM_TEST */
275 if (value
& (1 << 11)) { /* SSB */
277 omap_mcspi_interrupt_update(s
);
279 s
->systest
= value
& 0xfff;
282 case 0x28: /* MCSPI_MODULCTRL */
283 if (value
& (1 << 3)) /* SYSTEM_TEST */
284 if (s
->systest
& (1 << 11)) { /* SSB */
286 omap_mcspi_interrupt_update(s
);
288 s
->control
= value
& 0xf;
297 case 0x2c: /* MCSPI_CHCONF */
298 if ((value
^ s
->ch
[ch
].config
) & (3 << 14)) /* DMAR | DMAW */
299 omap_mcspi_dmarequest_update(s
->ch
+ ch
);
300 if (((value
>> 12) & 3) == 3) { /* TRM */
301 qemu_log_mask(LOG_GUEST_ERROR
, "%s: invalid TRM value (3)\n",
304 if (((value
>> 7) & 0x1f) < 3) { /* WL */
305 qemu_log_mask(LOG_GUEST_ERROR
,
306 "%s: invalid WL value (%" PRIx64
")\n",
307 __func__
, (value
>> 7) & 0x1f);
309 s
->ch
[ch
].config
= value
& 0x7fffff;
318 case 0x34: /* MCSPI_CHCTRL */
319 if (value
& ~s
->ch
[ch
].control
& 1) { /* EN */
320 s
->ch
[ch
].control
|= 1;
321 omap_mcspi_transfer_run(s
, ch
);
323 s
->ch
[ch
].control
= value
& 1;
332 case 0x38: /* MCSPI_TX */
333 s
->ch
[ch
].tx
= value
;
334 s
->ch
[ch
].status
&= ~(1 << 1); /* TXS */
335 omap_mcspi_transfer_run(s
, ch
);
344 static const MemoryRegionOps omap_mcspi_ops
= {
345 .read
= omap_mcspi_read
,
346 .write
= omap_mcspi_write
,
347 .endianness
= DEVICE_NATIVE_ENDIAN
,
350 struct omap_mcspi_s
*omap_mcspi_init(struct omap_target_agent_s
*ta
, int chnum
,
351 qemu_irq irq
, qemu_irq
*drq
, omap_clk fclk
, omap_clk iclk
)
353 struct omap_mcspi_s
*s
= g_new0(struct omap_mcspi_s
, 1);
354 struct omap_mcspi_ch_s
*ch
= s
->ch
;
365 memory_region_init_io(&s
->iomem
, NULL
, &omap_mcspi_ops
, s
, "omap.mcspi",
366 omap_l4_region_size(ta
, 0));
367 omap_l4_attach(ta
, 0, &s
->iomem
);
372 void omap_mcspi_attach(struct omap_mcspi_s
*s
,
373 uint32_t (*txrx
)(void *opaque
, uint32_t, int), void *opaque
,
376 if (chipselect
< 0 || chipselect
>= s
->chnum
)
377 hw_error("%s: Bad chipselect %i\n", __func__
, chipselect
);
379 s
->ch
[chipselect
].txrx
= txrx
;
380 s
->ch
[chipselect
].opaque
= opaque
;