1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include "hw.h"
23#include "omap.h"
24
25
26struct omap_mcspi_s {
27 qemu_irq irq;
28 int chnum;
29
30 uint32_t sysconfig;
31 uint32_t systest;
32 uint32_t irqst;
33 uint32_t irqen;
34 uint32_t wken;
35 uint32_t control;
36
37 struct omap_mcspi_ch_s {
38 qemu_irq txdrq;
39 qemu_irq rxdrq;
40 uint32_t (*txrx)(void *opaque, uint32_t, int);
41 void *opaque;
42
43 uint32_t tx;
44 uint32_t rx;
45
46 uint32_t config;
47 uint32_t status;
48 uint32_t control;
49 } ch[4];
50};
51
52static inline void omap_mcspi_interrupt_update(struct omap_mcspi_s *s)
53{
54 qemu_set_irq(s->irq, s->irqst & s->irqen);
55}
56
57static inline void omap_mcspi_dmarequest_update(struct omap_mcspi_ch_s *ch)
58{
59 qemu_set_irq(ch->txdrq,
60 (ch->control & 1) &&
61 (ch->config & (1 << 14)) &&
62 (ch->status & (1 << 1)) &&
63 ((ch->config >> 12) & 3) != 1);
64 qemu_set_irq(ch->rxdrq,
65 (ch->control & 1) &&
66 (ch->config & (1 << 15)) &&
67 (ch->status & (1 << 0)) &&
68 ((ch->config >> 12) & 3) != 2);
69}
70
71static void omap_mcspi_transfer_run(struct omap_mcspi_s *s, int chnum)
72{
73 struct omap_mcspi_ch_s *ch = s->ch + chnum;
74
75 if (!(ch->control & 1))
76 return;
77 if ((ch->status & (1 << 0)) &&
78 ((ch->config >> 12) & 3) != 2 &&
79 !(ch->config & (1 << 19)))
80 goto intr_update;
81 if ((ch->status & (1 << 1)) &&
82 ((ch->config >> 12) & 3) != 1)
83 goto intr_update;
84
85 if (!(s->control & 1) ||
86 (ch->config & (1 << 20))) {
87 if (ch->txrx)
88 ch->rx = ch->txrx(ch->opaque, ch->tx,
89 1 + (0x1f & (ch->config >> 7)));
90 }
91
92 ch->tx = 0;
93 ch->status |= 1 << 2;
94 ch->status |= 1 << 1;
95 if (((ch->config >> 12) & 3) != 2)
96 ch->status |= 1 << 0;
97
98intr_update:
99 if ((ch->status & (1 << 0)) &&
100 ((ch->config >> 12) & 3) != 2 &&
101 !(ch->config & (1 << 19)))
102 s->irqst |= 1 << (2 + 4 * chnum);
103 if ((ch->status & (1 << 1)) &&
104 ((ch->config >> 12) & 3) != 1)
105 s->irqst |= 1 << (0 + 4 * chnum);
106 omap_mcspi_interrupt_update(s);
107 omap_mcspi_dmarequest_update(ch);
108}
109
110void omap_mcspi_reset(struct omap_mcspi_s *s)
111{
112 int ch;
113
114 s->sysconfig = 0;
115 s->systest = 0;
116 s->irqst = 0;
117 s->irqen = 0;
118 s->wken = 0;
119 s->control = 4;
120
121 for (ch = 0; ch < 4; ch ++) {
122 s->ch[ch].config = 0x060000;
123 s->ch[ch].status = 2;
124 s->ch[ch].control = 0;
125
126 omap_mcspi_dmarequest_update(s->ch + ch);
127 }
128
129 omap_mcspi_interrupt_update(s);
130}
131
132static uint32_t omap_mcspi_read(void *opaque, target_phys_addr_t addr)
133{
134 struct omap_mcspi_s *s = (struct omap_mcspi_s *) opaque;
135 int ch = 0;
136 uint32_t ret;
137
138 switch (addr) {
139 case 0x00:
140 return 0x91;
141
142 case 0x10:
143 return s->sysconfig;
144
145 case 0x14:
146 return 1;
147
148 case 0x18:
149 return s->irqst;
150
151 case 0x1c:
152 return s->irqen;
153
154 case 0x20:
155 return s->wken;
156
157 case 0x24:
158 return s->systest;
159
160 case 0x28:
161 return s->control;
162
163 case 0x68: ch ++;
164 case 0x54: ch ++;
165 case 0x40: ch ++;
166 case 0x2c:
167 return s->ch[ch].config;
168
169 case 0x6c: ch ++;
170 case 0x58: ch ++;
171 case 0x44: ch ++;
172 case 0x30:
173 return s->ch[ch].status;
174
175 case 0x70: ch ++;
176 case 0x5c: ch ++;
177 case 0x48: ch ++;
178 case 0x34:
179 return s->ch[ch].control;
180
181 case 0x74: ch ++;
182 case 0x60: ch ++;
183 case 0x4c: ch ++;
184 case 0x38:
185 return s->ch[ch].tx;
186
187 case 0x78: ch ++;
188 case 0x64: ch ++;
189 case 0x50: ch ++;
190 case 0x3c:
191 s->ch[ch].status &= ~(1 << 0);
192 ret = s->ch[ch].rx;
193 omap_mcspi_transfer_run(s, ch);
194 return ret;
195 }
196
197 OMAP_BAD_REG(addr);
198 return 0;
199}
200
201static void omap_mcspi_write(void *opaque, target_phys_addr_t addr,
202 uint32_t value)
203{
204 struct omap_mcspi_s *s = (struct omap_mcspi_s *) opaque;
205 int ch = 0;
206
207 switch (addr) {
208 case 0x00:
209 case 0x14:
210 case 0x30:
211 case 0x3c:
212 case 0x44:
213 case 0x50:
214 case 0x58:
215 case 0x64:
216 case 0x6c:
217 case 0x78:
218 OMAP_RO_REG(addr);
219 return;
220
221 case 0x10:
222 if (value & (1 << 1))
223 omap_mcspi_reset(s);
224 s->sysconfig = value & 0x31d;
225 break;
226
227 case 0x18:
228 if (!((s->control & (1 << 3)) && (s->systest & (1 << 11)))) {
229 s->irqst &= ~value;
230 omap_mcspi_interrupt_update(s);
231 }
232 break;
233
234 case 0x1c:
235 s->irqen = value & 0x1777f;
236 omap_mcspi_interrupt_update(s);
237 break;
238
239 case 0x20:
240 s->wken = value & 1;
241 break;
242
243 case 0x24:
244 if (s->control & (1 << 3))
245 if (value & (1 << 11)) {
246 s->irqst |= 0x1777f;
247 omap_mcspi_interrupt_update(s);
248 }
249 s->systest = value & 0xfff;
250 break;
251
252 case 0x28:
253 if (value & (1 << 3))
254 if (s->systest & (1 << 11)) {
255 s->irqst |= 0x1777f;
256 omap_mcspi_interrupt_update(s);
257 }
258 s->control = value & 0xf;
259 break;
260
261 case 0x68: ch ++;
262 case 0x54: ch ++;
263 case 0x40: ch ++;
264 case 0x2c:
265 if ((value ^ s->ch[ch].config) & (3 << 14))
266 omap_mcspi_dmarequest_update(s->ch + ch);
267 if (((value >> 12) & 3) == 3)
268 fprintf(stderr, "%s: invalid TRM value (3)\n", __FUNCTION__);
269 if (((value >> 7) & 0x1f) < 3)
270 fprintf(stderr, "%s: invalid WL value (%i)\n",
271 __FUNCTION__, (value >> 7) & 0x1f);
272 s->ch[ch].config = value & 0x7fffff;
273 break;
274
275 case 0x70: ch ++;
276 case 0x5c: ch ++;
277 case 0x48: ch ++;
278 case 0x34:
279 if (value & ~s->ch[ch].control & 1) {
280 s->ch[ch].control |= 1;
281 omap_mcspi_transfer_run(s, ch);
282 } else
283 s->ch[ch].control = value & 1;
284 break;
285
286 case 0x74: ch ++;
287 case 0x60: ch ++;
288 case 0x4c: ch ++;
289 case 0x38:
290 s->ch[ch].tx = value;
291 s->ch[ch].status &= ~(1 << 1);
292 omap_mcspi_transfer_run(s, ch);
293 break;
294
295 default:
296 OMAP_BAD_REG(addr);
297 return;
298 }
299}
300
301static CPUReadMemoryFunc * const omap_mcspi_readfn[] = {
302 omap_badwidth_read32,
303 omap_badwidth_read32,
304 omap_mcspi_read,
305};
306
307static CPUWriteMemoryFunc * const omap_mcspi_writefn[] = {
308 omap_badwidth_write32,
309 omap_badwidth_write32,
310 omap_mcspi_write,
311};
312
313struct 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)
315{
316 int iomemtype;
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;
320
321 s->irq = irq;
322 s->chnum = chnum;
323 while (chnum --) {
324 ch->txdrq = *drq ++;
325 ch->rxdrq = *drq ++;
326 ch ++;
327 }
328 omap_mcspi_reset(s);
329
330 iomemtype = l4_register_io_memory(omap_mcspi_readfn,
331 omap_mcspi_writefn, s);
332 omap_l4_attach(ta, 0, iomemtype);
333
334 return s;
335}
336
337void omap_mcspi_attach(struct omap_mcspi_s *s,
338 uint32_t (*txrx)(void *opaque, uint32_t, int), void *opaque,
339 int chipselect)
340{
341 if (chipselect < 0 || chipselect >= s->chnum)
342 hw_error("%s: Bad chipselect %i\n", __FUNCTION__, chipselect);
343
344 s->ch[chipselect].txrx = txrx;
345 s->ch[chipselect].opaque = opaque;
346}
347