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.
25 /* Multichannel SPI */
37 struct omap_mcspi_ch_s
{
40 uint32_t (*txrx
)(void *opaque
, uint32_t, int);
52 static inline void omap_mcspi_interrupt_update(struct omap_mcspi_s
*s
)
54 qemu_set_irq(s
->irq
, s
->irqst
& s
->irqen
);
57 static inline void omap_mcspi_dmarequest_update(struct omap_mcspi_ch_s
*ch
)
59 qemu_set_irq(ch
->txdrq
,
60 (ch
->control
& 1) && /* EN */
61 (ch
->config
& (1 << 14)) && /* DMAW */
62 (ch
->status
& (1 << 1)) && /* TXS */
63 ((ch
->config
>> 12) & 3) != 1); /* TRM */
64 qemu_set_irq(ch
->rxdrq
,
65 (ch
->control
& 1) && /* EN */
66 (ch
->config
& (1 << 15)) && /* DMAW */
67 (ch
->status
& (1 << 0)) && /* RXS */
68 ((ch
->config
>> 12) & 3) != 2); /* TRM */
71 static void omap_mcspi_transfer_run(struct omap_mcspi_s
*s
, int chnum
)
73 struct omap_mcspi_ch_s
*ch
= s
->ch
+ chnum
;
75 if (!(ch
->control
& 1)) /* EN */
77 if ((ch
->status
& (1 << 0)) && /* RXS */
78 ((ch
->config
>> 12) & 3) != 2 && /* TRM */
79 !(ch
->config
& (1 << 19))) /* TURBO */
81 if ((ch
->status
& (1 << 1)) && /* TXS */
82 ((ch
->config
>> 12) & 3) != 1) /* TRM */
85 if (!(s
->control
& 1) || /* SINGLE */
86 (ch
->config
& (1 << 20))) { /* FORCE */
88 ch
->rx
= ch
->txrx(ch
->opaque
, ch
->tx
, /* WL */
89 1 + (0x1f & (ch
->config
>> 7)));
93 ch
->status
|= 1 << 2; /* EOT */
94 ch
->status
|= 1 << 1; /* TXS */
95 if (((ch
->config
>> 12) & 3) != 2) /* TRM */
96 ch
->status
|= 1 << 0; /* RXS */
99 if ((ch
->status
& (1 << 0)) && /* RXS */
100 ((ch
->config
>> 12) & 3) != 2 && /* TRM */
101 !(ch
->config
& (1 << 19))) /* TURBO */
102 s
->irqst
|= 1 << (2 + 4 * chnum
); /* RX_FULL */
103 if ((ch
->status
& (1 << 1)) && /* TXS */
104 ((ch
->config
>> 12) & 3) != 1) /* TRM */
105 s
->irqst
|= 1 << (0 + 4 * chnum
); /* TX_EMPTY */
106 omap_mcspi_interrupt_update(s
);
107 omap_mcspi_dmarequest_update(ch
);
110 void omap_mcspi_reset(struct omap_mcspi_s
*s
)
121 for (ch
= 0; ch
< 4; ch
++) {
122 s
->ch
[ch
].config
= 0x060000;
123 s
->ch
[ch
].status
= 2; /* TXS */
124 s
->ch
[ch
].control
= 0;
126 omap_mcspi_dmarequest_update(s
->ch
+ ch
);
129 omap_mcspi_interrupt_update(s
);
132 static uint32_t omap_mcspi_read(void *opaque
, target_phys_addr_t addr
)
134 struct omap_mcspi_s
*s
= (struct omap_mcspi_s
*) opaque
;
139 case 0x00: /* MCSPI_REVISION */
142 case 0x10: /* MCSPI_SYSCONFIG */
145 case 0x14: /* MCSPI_SYSSTATUS */
146 return 1; /* RESETDONE */
148 case 0x18: /* MCSPI_IRQSTATUS */
151 case 0x1c: /* MCSPI_IRQENABLE */
154 case 0x20: /* MCSPI_WAKEUPENABLE */
157 case 0x24: /* MCSPI_SYST */
160 case 0x28: /* MCSPI_MODULCTRL */
166 case 0x2c: /* MCSPI_CHCONF */
167 return s
->ch
[ch
].config
;
172 case 0x30: /* MCSPI_CHSTAT */
173 return s
->ch
[ch
].status
;
178 case 0x34: /* MCSPI_CHCTRL */
179 return s
->ch
[ch
].control
;
184 case 0x38: /* MCSPI_TX */
190 case 0x3c: /* MCSPI_RX */
191 s
->ch
[ch
].status
&= ~(1 << 0); /* RXS */
193 omap_mcspi_transfer_run(s
, ch
);
201 static void omap_mcspi_write(void *opaque
, target_phys_addr_t addr
,
204 struct omap_mcspi_s
*s
= (struct omap_mcspi_s
*) opaque
;
208 case 0x00: /* MCSPI_REVISION */
209 case 0x14: /* MCSPI_SYSSTATUS */
210 case 0x30: /* MCSPI_CHSTAT0 */
211 case 0x3c: /* MCSPI_RX0 */
212 case 0x44: /* MCSPI_CHSTAT1 */
213 case 0x50: /* MCSPI_RX1 */
214 case 0x58: /* MCSPI_CHSTAT2 */
215 case 0x64: /* MCSPI_RX2 */
216 case 0x6c: /* MCSPI_CHSTAT3 */
217 case 0x78: /* MCSPI_RX3 */
221 case 0x10: /* MCSPI_SYSCONFIG */
222 if (value
& (1 << 1)) /* SOFTRESET */
224 s
->sysconfig
= value
& 0x31d;
227 case 0x18: /* MCSPI_IRQSTATUS */
228 if (!((s
->control
& (1 << 3)) && (s
->systest
& (1 << 11)))) {
230 omap_mcspi_interrupt_update(s
);
234 case 0x1c: /* MCSPI_IRQENABLE */
235 s
->irqen
= value
& 0x1777f;
236 omap_mcspi_interrupt_update(s
);
239 case 0x20: /* MCSPI_WAKEUPENABLE */
243 case 0x24: /* MCSPI_SYST */
244 if (s
->control
& (1 << 3)) /* SYSTEM_TEST */
245 if (value
& (1 << 11)) { /* SSB */
247 omap_mcspi_interrupt_update(s
);
249 s
->systest
= value
& 0xfff;
252 case 0x28: /* MCSPI_MODULCTRL */
253 if (value
& (1 << 3)) /* SYSTEM_TEST */
254 if (s
->systest
& (1 << 11)) { /* SSB */
256 omap_mcspi_interrupt_update(s
);
258 s
->control
= value
& 0xf;
264 case 0x2c: /* MCSPI_CHCONF */
265 if ((value
^ s
->ch
[ch
].config
) & (3 << 14)) /* DMAR | DMAW */
266 omap_mcspi_dmarequest_update(s
->ch
+ ch
);
267 if (((value
>> 12) & 3) == 3) /* TRM */
268 fprintf(stderr
, "%s: invalid TRM value (3)\n", __FUNCTION__
);
269 if (((value
>> 7) & 0x1f) < 3) /* WL */
270 fprintf(stderr
, "%s: invalid WL value (%i)\n",
271 __FUNCTION__
, (value
>> 7) & 0x1f);
272 s
->ch
[ch
].config
= value
& 0x7fffff;
278 case 0x34: /* MCSPI_CHCTRL */
279 if (value
& ~s
->ch
[ch
].control
& 1) { /* EN */
280 s
->ch
[ch
].control
|= 1;
281 omap_mcspi_transfer_run(s
, ch
);
283 s
->ch
[ch
].control
= value
& 1;
289 case 0x38: /* MCSPI_TX */
290 s
->ch
[ch
].tx
= value
;
291 s
->ch
[ch
].status
&= ~(1 << 1); /* TXS */
292 omap_mcspi_transfer_run(s
, ch
);
301 static CPUReadMemoryFunc
* const omap_mcspi_readfn
[] = {
302 omap_badwidth_read32
,
303 omap_badwidth_read32
,
307 static CPUWriteMemoryFunc
* const omap_mcspi_writefn
[] = {
308 omap_badwidth_write32
,
309 omap_badwidth_write32
,
313 struct omap_mcspi_s
*omap_mcspi_init(struct omap_target_agent_s
*ta
, int chnum
,
314 qemu_irq irq
, qemu_irq
*drq
, omap_clk fclk
, omap_clk iclk
)
317 struct omap_mcspi_s
*s
= (struct omap_mcspi_s
*)
318 g_malloc0(sizeof(struct omap_mcspi_s
));
319 struct omap_mcspi_ch_s
*ch
= s
->ch
;
330 iomemtype
= l4_register_io_memory(omap_mcspi_readfn
,
331 omap_mcspi_writefn
, s
);
332 omap_l4_attach(ta
, 0, iomemtype
);
337 void omap_mcspi_attach(struct omap_mcspi_s
*s
,
338 uint32_t (*txrx
)(void *opaque
, uint32_t, int), void *opaque
,
341 if (chipselect
< 0 || chipselect
>= s
->chnum
)
342 hw_error("%s: Bad chipselect %i\n", __FUNCTION__
, chipselect
);
344 s
->ch
[chipselect
].txrx
= txrx
;
345 s
->ch
[chipselect
].opaque
= opaque
;