]>
Commit | Line | Data |
---|---|---|
f3354b0e | 1 | /* |
2 | * TI OMAP general purpose memory controller emulation. | |
3 | * | |
4 | * Copyright (C) 2007-2009 Nokia Corporation | |
5 | * Original code written by Andrzej Zaborowski <[email protected]> | |
6 | * Enhancements for OMAP3 and NAND support written by Juha Riihimäki | |
7 | * | |
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. | |
12 | * | |
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. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
83c9f4ca | 21 | #include "hw/hw.h" |
0d09e41a PB |
22 | #include "hw/block/flash.h" |
23 | #include "hw/arm/omap.h" | |
022c62cb PB |
24 | #include "exec/memory.h" |
25 | #include "exec/address-spaces.h" | |
f3354b0e | 26 | |
27 | /* General-Purpose Memory Controller */ | |
28 | struct omap_gpmc_s { | |
29 | qemu_irq irq; | |
eee0a1c6 | 30 | qemu_irq drq; |
64066a8f | 31 | MemoryRegion iomem; |
856f2df7 | 32 | int accept_256; |
f3354b0e | 33 | |
7c470ff1 | 34 | uint8_t revision; |
f3354b0e | 35 | uint8_t sysconfig; |
36 | uint16_t irqst; | |
37 | uint16_t irqen; | |
d5c8cf99 | 38 | uint16_t lastirq; |
f3354b0e | 39 | uint16_t timeout; |
40 | uint16_t config; | |
f3354b0e | 41 | struct omap_gpmc_cs_file_s { |
42 | uint32_t config[7]; | |
64066a8f AK |
43 | MemoryRegion *iomem; |
44 | MemoryRegion container; | |
2a952feb PM |
45 | MemoryRegion nandiomem; |
46 | DeviceState *dev; | |
f3354b0e | 47 | } cs_file[8]; |
48 | int ecc_cs; | |
49 | int ecc_ptr; | |
50 | uint32_t ecc_cfg; | |
51 | ECCState ecc[9]; | |
ef20677c PM |
52 | struct prefetch { |
53 | uint32_t config1; /* GPMC_PREFETCH_CONFIG1 */ | |
54 | uint32_t transfercount; /* GPMC_PREFETCH_CONFIG2:TRANSFERCOUNT */ | |
55 | int startengine; /* GPMC_PREFETCH_CONTROL:STARTENGINE */ | |
56 | int fifopointer; /* GPMC_PREFETCH_STATUS:FIFOPOINTER */ | |
57 | int count; /* GPMC_PREFETCH_STATUS:COUNTVALUE */ | |
d5c8cf99 PM |
58 | MemoryRegion iomem; |
59 | uint8_t fifo[64]; | |
ef20677c | 60 | } prefetch; |
f3354b0e | 61 | }; |
62 | ||
2a952feb PM |
63 | #define OMAP_GPMC_8BIT 0 |
64 | #define OMAP_GPMC_16BIT 1 | |
65 | #define OMAP_GPMC_NOR 0 | |
66 | #define OMAP_GPMC_NAND 2 | |
67 | ||
68 | static int omap_gpmc_devtype(struct omap_gpmc_cs_file_s *f) | |
69 | { | |
70 | return (f->config[0] >> 10) & 3; | |
71 | } | |
72 | ||
73 | static int omap_gpmc_devsize(struct omap_gpmc_cs_file_s *f) | |
74 | { | |
75 | /* devsize field is really 2 bits but we ignore the high | |
76 | * bit to ensure consistent behaviour if the guest sets | |
77 | * it (values 2 and 3 are reserved in the TRM) | |
78 | */ | |
79 | return (f->config[0] >> 12) & 1; | |
80 | } | |
81 | ||
d5c8cf99 PM |
82 | /* Extract the chip-select value from the prefetch config1 register */ |
83 | static int prefetch_cs(uint32_t config1) | |
84 | { | |
85 | return (config1 >> 24) & 7; | |
86 | } | |
87 | ||
88 | static int prefetch_threshold(uint32_t config1) | |
89 | { | |
90 | return (config1 >> 8) & 0x7f; | |
91 | } | |
92 | ||
f3354b0e | 93 | static void omap_gpmc_int_update(struct omap_gpmc_s *s) |
94 | { | |
d5c8cf99 PM |
95 | /* The TRM is a bit unclear, but it seems to say that |
96 | * the TERMINALCOUNTSTATUS bit is set only on the | |
97 | * transition when the prefetch engine goes from | |
98 | * active to inactive, whereas the FIFOEVENTSTATUS | |
99 | * bit is held high as long as the fifo has at | |
100 | * least THRESHOLD bytes available. | |
101 | * So we do the latter here, but TERMINALCOUNTSTATUS | |
102 | * is set elsewhere. | |
103 | */ | |
104 | if (s->prefetch.fifopointer >= prefetch_threshold(s->prefetch.config1)) { | |
105 | s->irqst |= 1; | |
106 | } | |
107 | if ((s->irqen & s->irqst) != s->lastirq) { | |
108 | s->lastirq = s->irqen & s->irqst; | |
109 | qemu_set_irq(s->irq, s->lastirq); | |
110 | } | |
111 | } | |
112 | ||
113 | static void omap_gpmc_dma_update(struct omap_gpmc_s *s, int value) | |
114 | { | |
115 | if (s->prefetch.config1 & 4) { | |
116 | qemu_set_irq(s->drq, value); | |
117 | } | |
f3354b0e | 118 | } |
119 | ||
2a952feb PM |
120 | /* Access functions for when a NAND-like device is mapped into memory: |
121 | * all addresses in the region behave like accesses to the relevant | |
122 | * GPMC_NAND_DATA_i register (which is actually implemented to call these) | |
123 | */ | |
a8170e5e | 124 | static uint64_t omap_nand_read(void *opaque, hwaddr addr, |
2a952feb PM |
125 | unsigned size) |
126 | { | |
127 | struct omap_gpmc_cs_file_s *f = (struct omap_gpmc_cs_file_s *)opaque; | |
128 | uint64_t v; | |
129 | nand_setpins(f->dev, 0, 0, 0, 1, 0); | |
130 | switch (omap_gpmc_devsize(f)) { | |
131 | case OMAP_GPMC_8BIT: | |
132 | v = nand_getio(f->dev); | |
133 | if (size == 1) { | |
134 | return v; | |
135 | } | |
136 | v |= (nand_getio(f->dev) << 8); | |
137 | if (size == 2) { | |
138 | return v; | |
139 | } | |
140 | v |= (nand_getio(f->dev) << 16); | |
141 | v |= (nand_getio(f->dev) << 24); | |
142 | return v; | |
143 | case OMAP_GPMC_16BIT: | |
144 | v = nand_getio(f->dev); | |
145 | if (size == 1) { | |
146 | /* 8 bit read from 16 bit device : probably a guest bug */ | |
147 | return v & 0xff; | |
148 | } | |
149 | if (size == 2) { | |
150 | return v; | |
151 | } | |
152 | v |= (nand_getio(f->dev) << 16); | |
153 | return v; | |
154 | default: | |
155 | abort(); | |
156 | } | |
157 | } | |
158 | ||
159 | static void omap_nand_setio(DeviceState *dev, uint64_t value, | |
160 | int nandsize, int size) | |
161 | { | |
162 | /* Write the specified value to the NAND device, respecting | |
163 | * both size of the NAND device and size of the write access. | |
164 | */ | |
165 | switch (nandsize) { | |
166 | case OMAP_GPMC_8BIT: | |
167 | switch (size) { | |
168 | case 1: | |
169 | nand_setio(dev, value & 0xff); | |
170 | break; | |
171 | case 2: | |
172 | nand_setio(dev, value & 0xff); | |
173 | nand_setio(dev, (value >> 8) & 0xff); | |
174 | break; | |
175 | case 4: | |
176 | default: | |
177 | nand_setio(dev, value & 0xff); | |
178 | nand_setio(dev, (value >> 8) & 0xff); | |
179 | nand_setio(dev, (value >> 16) & 0xff); | |
180 | nand_setio(dev, (value >> 24) & 0xff); | |
181 | break; | |
182 | } | |
c0465d1a | 183 | break; |
2a952feb PM |
184 | case OMAP_GPMC_16BIT: |
185 | switch (size) { | |
186 | case 1: | |
187 | /* writing to a 16bit device with 8bit access is probably a guest | |
188 | * bug; pass the value through anyway. | |
189 | */ | |
190 | case 2: | |
191 | nand_setio(dev, value & 0xffff); | |
192 | break; | |
193 | case 4: | |
194 | default: | |
195 | nand_setio(dev, value & 0xffff); | |
196 | nand_setio(dev, (value >> 16) & 0xffff); | |
197 | break; | |
198 | } | |
c0465d1a | 199 | break; |
2a952feb PM |
200 | } |
201 | } | |
202 | ||
a8170e5e | 203 | static void omap_nand_write(void *opaque, hwaddr addr, |
2a952feb PM |
204 | uint64_t value, unsigned size) |
205 | { | |
206 | struct omap_gpmc_cs_file_s *f = (struct omap_gpmc_cs_file_s *)opaque; | |
207 | nand_setpins(f->dev, 0, 0, 0, 1, 0); | |
208 | omap_nand_setio(f->dev, value, omap_gpmc_devsize(f), size); | |
209 | } | |
210 | ||
211 | static const MemoryRegionOps omap_nand_ops = { | |
212 | .read = omap_nand_read, | |
213 | .write = omap_nand_write, | |
214 | .endianness = DEVICE_NATIVE_ENDIAN, | |
215 | }; | |
216 | ||
d5c8cf99 PM |
217 | static void fill_prefetch_fifo(struct omap_gpmc_s *s) |
218 | { | |
219 | /* Fill the prefetch FIFO by reading data from NAND. | |
220 | * We do this synchronously, unlike the hardware which | |
221 | * will do this asynchronously. We refill when the | |
222 | * FIFO has THRESHOLD bytes free, and we always refill | |
223 | * as much data as possible starting at the top end | |
224 | * of the FIFO. | |
225 | * (We have to refill at THRESHOLD rather than waiting | |
226 | * for the FIFO to empty to allow for the case where | |
227 | * the FIFO size isn't an exact multiple of THRESHOLD | |
228 | * and we're doing DMA transfers.) | |
229 | * This means we never need to handle wrap-around in | |
230 | * the fifo-reading code, and the next byte of data | |
231 | * to read is always fifo[63 - fifopointer]. | |
232 | */ | |
233 | int fptr; | |
234 | int cs = prefetch_cs(s->prefetch.config1); | |
235 | int is16bit = (((s->cs_file[cs].config[0] >> 12) & 3) != 0); | |
236 | int bytes; | |
237 | /* Don't believe the bit of the OMAP TRM that says that COUNTVALUE | |
238 | * and TRANSFERCOUNT are in units of 16 bit words for 16 bit NAND. | |
239 | * Instead believe the bit that says it is always a byte count. | |
240 | */ | |
241 | bytes = 64 - s->prefetch.fifopointer; | |
242 | if (bytes > s->prefetch.count) { | |
243 | bytes = s->prefetch.count; | |
244 | } | |
89f26e6b PM |
245 | if (is16bit) { |
246 | bytes &= ~1; | |
247 | } | |
248 | ||
d5c8cf99 PM |
249 | s->prefetch.count -= bytes; |
250 | s->prefetch.fifopointer += bytes; | |
251 | fptr = 64 - s->prefetch.fifopointer; | |
252 | /* Move the existing data in the FIFO so it sits just | |
253 | * before what we're about to read in | |
254 | */ | |
255 | while (fptr < (64 - bytes)) { | |
256 | s->prefetch.fifo[fptr] = s->prefetch.fifo[fptr + bytes]; | |
257 | fptr++; | |
258 | } | |
259 | while (fptr < 64) { | |
260 | if (is16bit) { | |
261 | uint32_t v = omap_nand_read(&s->cs_file[cs], 0, 2); | |
262 | s->prefetch.fifo[fptr++] = v & 0xff; | |
263 | s->prefetch.fifo[fptr++] = (v >> 8) & 0xff; | |
264 | } else { | |
265 | s->prefetch.fifo[fptr++] = omap_nand_read(&s->cs_file[cs], 0, 1); | |
266 | } | |
267 | } | |
268 | if (s->prefetch.startengine && (s->prefetch.count == 0)) { | |
269 | /* This was the final transfer: raise TERMINALCOUNTSTATUS */ | |
270 | s->irqst |= 2; | |
271 | s->prefetch.startengine = 0; | |
272 | } | |
273 | /* If there are any bytes in the FIFO at this point then | |
274 | * we must raise a DMA request (either this is a final part | |
275 | * transfer, or we filled the FIFO in which case we certainly | |
276 | * have THRESHOLD bytes available) | |
277 | */ | |
278 | if (s->prefetch.fifopointer != 0) { | |
279 | omap_gpmc_dma_update(s, 1); | |
280 | } | |
281 | omap_gpmc_int_update(s); | |
282 | } | |
283 | ||
284 | /* Access functions for a NAND-like device when the prefetch/postwrite | |
285 | * engine is enabled -- all addresses in the region behave alike: | |
286 | * data is read or written to the FIFO. | |
287 | */ | |
a8170e5e | 288 | static uint64_t omap_gpmc_prefetch_read(void *opaque, hwaddr addr, |
d5c8cf99 PM |
289 | unsigned size) |
290 | { | |
291 | struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque; | |
292 | uint32_t data; | |
293 | if (s->prefetch.config1 & 1) { | |
294 | /* The TRM doesn't define the behaviour if you read from the | |
295 | * FIFO when the prefetch engine is in write mode. We choose | |
296 | * to always return zero. | |
297 | */ | |
298 | return 0; | |
299 | } | |
300 | /* Note that trying to read an empty fifo repeats the last byte */ | |
301 | if (s->prefetch.fifopointer) { | |
302 | s->prefetch.fifopointer--; | |
303 | } | |
304 | data = s->prefetch.fifo[63 - s->prefetch.fifopointer]; | |
305 | if (s->prefetch.fifopointer == | |
306 | (64 - prefetch_threshold(s->prefetch.config1))) { | |
307 | /* We've drained THRESHOLD bytes now. So deassert the | |
308 | * DMA request, then refill the FIFO (which will probably | |
309 | * assert it again.) | |
310 | */ | |
311 | omap_gpmc_dma_update(s, 0); | |
312 | fill_prefetch_fifo(s); | |
313 | } | |
314 | omap_gpmc_int_update(s); | |
315 | return data; | |
316 | } | |
317 | ||
a8170e5e | 318 | static void omap_gpmc_prefetch_write(void *opaque, hwaddr addr, |
d5c8cf99 PM |
319 | uint64_t value, unsigned size) |
320 | { | |
321 | struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque; | |
322 | int cs = prefetch_cs(s->prefetch.config1); | |
323 | if ((s->prefetch.config1 & 1) == 0) { | |
324 | /* The TRM doesn't define the behaviour of writing to the | |
325 | * FIFO when the prefetch engine is in read mode. We | |
326 | * choose to ignore the write. | |
327 | */ | |
328 | return; | |
329 | } | |
330 | if (s->prefetch.count == 0) { | |
331 | /* The TRM doesn't define the behaviour of writing to the | |
332 | * FIFO if the transfer is complete. We choose to ignore. | |
333 | */ | |
334 | return; | |
335 | } | |
336 | /* The only reason we do any data buffering in postwrite | |
337 | * mode is if we are talking to a 16 bit NAND device, in | |
338 | * which case we need to buffer the first byte of the | |
339 | * 16 bit word until the other byte arrives. | |
340 | */ | |
341 | int is16bit = (((s->cs_file[cs].config[0] >> 12) & 3) != 0); | |
342 | if (is16bit) { | |
343 | /* fifopointer alternates between 64 (waiting for first | |
344 | * byte of word) and 63 (waiting for second byte) | |
345 | */ | |
346 | if (s->prefetch.fifopointer == 64) { | |
347 | s->prefetch.fifo[0] = value; | |
348 | s->prefetch.fifopointer--; | |
349 | } else { | |
350 | value = (value << 8) | s->prefetch.fifo[0]; | |
351 | omap_nand_write(&s->cs_file[cs], 0, value, 2); | |
352 | s->prefetch.count--; | |
353 | s->prefetch.fifopointer = 64; | |
354 | } | |
355 | } else { | |
356 | /* Just write the byte : fifopointer remains 64 at all times */ | |
357 | omap_nand_write(&s->cs_file[cs], 0, value, 1); | |
358 | s->prefetch.count--; | |
359 | } | |
360 | if (s->prefetch.count == 0) { | |
361 | /* Final transfer: raise TERMINALCOUNTSTATUS */ | |
362 | s->irqst |= 2; | |
363 | s->prefetch.startengine = 0; | |
364 | } | |
365 | omap_gpmc_int_update(s); | |
366 | } | |
367 | ||
368 | static const MemoryRegionOps omap_prefetch_ops = { | |
369 | .read = omap_gpmc_prefetch_read, | |
370 | .write = omap_gpmc_prefetch_write, | |
371 | .endianness = DEVICE_NATIVE_ENDIAN, | |
372 | .impl.min_access_size = 1, | |
373 | .impl.max_access_size = 1, | |
374 | }; | |
375 | ||
2a952feb PM |
376 | static MemoryRegion *omap_gpmc_cs_memregion(struct omap_gpmc_s *s, int cs) |
377 | { | |
378 | /* Return the MemoryRegion* to map/unmap for this chipselect */ | |
379 | struct omap_gpmc_cs_file_s *f = &s->cs_file[cs]; | |
380 | if (omap_gpmc_devtype(f) == OMAP_GPMC_NOR) { | |
381 | return f->iomem; | |
382 | } | |
d5c8cf99 PM |
383 | if ((s->prefetch.config1 & 0x80) && |
384 | (prefetch_cs(s->prefetch.config1) == cs)) { | |
385 | /* The prefetch engine is enabled for this CS: map the FIFO */ | |
386 | return &s->prefetch.iomem; | |
387 | } | |
2a952feb PM |
388 | return &f->nandiomem; |
389 | } | |
390 | ||
3387bf55 | 391 | static void omap_gpmc_cs_map(struct omap_gpmc_s *s, int cs) |
f3354b0e | 392 | { |
3387bf55 PM |
393 | struct omap_gpmc_cs_file_s *f = &s->cs_file[cs]; |
394 | uint32_t mask = (f->config[6] >> 8) & 0xf; | |
395 | uint32_t base = f->config[6] & 0x3f; | |
07bc2f80 PM |
396 | uint32_t size; |
397 | ||
2a952feb | 398 | if (!f->iomem && !f->dev) { |
07bc2f80 PM |
399 | return; |
400 | } | |
401 | ||
3387bf55 PM |
402 | if (!(f->config[6] & (1 << 6))) { |
403 | /* Do nothing unless CSVALID */ | |
404 | return; | |
405 | } | |
406 | ||
f3354b0e | 407 | /* TODO: check for overlapping regions and report access errors */ |
856f2df7 JR |
408 | if (mask != 0x8 && mask != 0xc && mask != 0xe && mask != 0xf |
409 | && !(s->accept_256 && !mask)) { | |
410 | fprintf(stderr, "%s: invalid chip-select mask address (0x%x)\n", | |
411 | __func__, mask); | |
f3354b0e | 412 | } |
413 | ||
07bc2f80 PM |
414 | base <<= 24; |
415 | size = (0x0fffffff & ~(mask << 24)) + 1; | |
f3354b0e | 416 | /* TODO: rather than setting the size of the mapping (which should be |
417 | * constant), the mask should cause wrapping of the address space, so | |
418 | * that the same memory becomes accessible at every <i>size</i> bytes | |
419 | * starting from <i>base</i>. */ | |
2c9b15ca | 420 | memory_region_init(&f->container, NULL, "omap-gpmc-file", size); |
2a952feb PM |
421 | memory_region_add_subregion(&f->container, 0, |
422 | omap_gpmc_cs_memregion(s, cs)); | |
07bc2f80 PM |
423 | memory_region_add_subregion(get_system_memory(), base, |
424 | &f->container); | |
f3354b0e | 425 | } |
426 | ||
3387bf55 | 427 | static void omap_gpmc_cs_unmap(struct omap_gpmc_s *s, int cs) |
f3354b0e | 428 | { |
3387bf55 PM |
429 | struct omap_gpmc_cs_file_s *f = &s->cs_file[cs]; |
430 | if (!(f->config[6] & (1 << 6))) { | |
431 | /* Do nothing unless CSVALID */ | |
432 | return; | |
433 | } | |
2a952feb | 434 | if (!f->iomem && !f->dev) { |
07bc2f80 | 435 | return; |
f3354b0e | 436 | } |
07bc2f80 | 437 | memory_region_del_subregion(get_system_memory(), &f->container); |
2a952feb | 438 | memory_region_del_subregion(&f->container, omap_gpmc_cs_memregion(s, cs)); |
d8d95814 | 439 | object_unparent(OBJECT(&f->container)); |
f3354b0e | 440 | } |
441 | ||
442 | void omap_gpmc_reset(struct omap_gpmc_s *s) | |
443 | { | |
444 | int i; | |
445 | ||
446 | s->sysconfig = 0; | |
447 | s->irqst = 0; | |
448 | s->irqen = 0; | |
449 | omap_gpmc_int_update(s); | |
0ec6dc73 PM |
450 | for (i = 0; i < 8; i++) { |
451 | /* This has to happen before we change any of the config | |
452 | * used to determine which memory regions are mapped or unmapped. | |
453 | */ | |
454 | omap_gpmc_cs_unmap(s, i); | |
455 | } | |
f3354b0e | 456 | s->timeout = 0; |
457 | s->config = 0xa00; | |
ef20677c PM |
458 | s->prefetch.config1 = 0x00004000; |
459 | s->prefetch.transfercount = 0x00000000; | |
460 | s->prefetch.startengine = 0; | |
461 | s->prefetch.fifopointer = 0; | |
462 | s->prefetch.count = 0; | |
f3354b0e | 463 | for (i = 0; i < 8; i ++) { |
f3354b0e | 464 | s->cs_file[i].config[1] = 0x101001; |
465 | s->cs_file[i].config[2] = 0x020201; | |
466 | s->cs_file[i].config[3] = 0x10031003; | |
467 | s->cs_file[i].config[4] = 0x10f1111; | |
468 | s->cs_file[i].config[5] = 0; | |
3387bf55 PM |
469 | s->cs_file[i].config[6] = 0xf00; |
470 | /* In theory we could probe attached devices for some CFG1 | |
471 | * bits here, but we just retain them across resets as they | |
472 | * were set initially by omap_gpmc_attach(). | |
473 | */ | |
474 | if (i == 0) { | |
475 | s->cs_file[i].config[0] &= 0x00433e00; | |
476 | s->cs_file[i].config[6] |= 1 << 6; /* CSVALID */ | |
477 | omap_gpmc_cs_map(s, i); | |
478 | } else { | |
479 | s->cs_file[i].config[0] &= 0x00403c00; | |
480 | } | |
f3354b0e | 481 | } |
f3354b0e | 482 | s->ecc_cs = 0; |
483 | s->ecc_ptr = 0; | |
484 | s->ecc_cfg = 0x3fcff000; | |
485 | for (i = 0; i < 9; i ++) | |
486 | ecc_reset(&s->ecc[i]); | |
487 | } | |
488 | ||
a8170e5e | 489 | static int gpmc_wordaccess_only(hwaddr addr) |
2a952feb PM |
490 | { |
491 | /* Return true if the register offset is to a register that | |
492 | * only permits word width accesses. | |
493 | * Non-word accesses are only OK for GPMC_NAND_DATA/ADDRESS/COMMAND | |
494 | * for any chipselect. | |
495 | */ | |
496 | if (addr >= 0x60 && addr <= 0x1d4) { | |
497 | int cs = (addr - 0x60) / 0x30; | |
498 | addr -= cs * 0x30; | |
499 | if (addr >= 0x7c && addr < 0x88) { | |
500 | /* GPMC_NAND_COMMAND, GPMC_NAND_ADDRESS, GPMC_NAND_DATA */ | |
501 | return 0; | |
502 | } | |
503 | } | |
504 | return 1; | |
505 | } | |
506 | ||
a8170e5e | 507 | static uint64_t omap_gpmc_read(void *opaque, hwaddr addr, |
64066a8f | 508 | unsigned size) |
f3354b0e | 509 | { |
510 | struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque; | |
511 | int cs; | |
512 | struct omap_gpmc_cs_file_s *f; | |
513 | ||
2a952feb | 514 | if (size != 4 && gpmc_wordaccess_only(addr)) { |
64066a8f AK |
515 | return omap_badwidth_read32(opaque, addr); |
516 | } | |
517 | ||
f3354b0e | 518 | switch (addr) { |
519 | case 0x000: /* GPMC_REVISION */ | |
7c470ff1 | 520 | return s->revision; |
f3354b0e | 521 | |
522 | case 0x010: /* GPMC_SYSCONFIG */ | |
523 | return s->sysconfig; | |
524 | ||
525 | case 0x014: /* GPMC_SYSSTATUS */ | |
526 | return 1; /* RESETDONE */ | |
527 | ||
528 | case 0x018: /* GPMC_IRQSTATUS */ | |
529 | return s->irqst; | |
530 | ||
531 | case 0x01c: /* GPMC_IRQENABLE */ | |
532 | return s->irqen; | |
533 | ||
534 | case 0x040: /* GPMC_TIMEOUT_CONTROL */ | |
535 | return s->timeout; | |
536 | ||
537 | case 0x044: /* GPMC_ERR_ADDRESS */ | |
538 | case 0x048: /* GPMC_ERR_TYPE */ | |
539 | return 0; | |
540 | ||
541 | case 0x050: /* GPMC_CONFIG */ | |
542 | return s->config; | |
543 | ||
544 | case 0x054: /* GPMC_STATUS */ | |
545 | return 0x001; | |
546 | ||
547 | case 0x060 ... 0x1d4: | |
548 | cs = (addr - 0x060) / 0x30; | |
549 | addr -= cs * 0x30; | |
550 | f = s->cs_file + cs; | |
551 | switch (addr) { | |
9ed3e1b1 PM |
552 | case 0x60: /* GPMC_CONFIG1 */ |
553 | return f->config[0]; | |
554 | case 0x64: /* GPMC_CONFIG2 */ | |
555 | return f->config[1]; | |
556 | case 0x68: /* GPMC_CONFIG3 */ | |
557 | return f->config[2]; | |
558 | case 0x6c: /* GPMC_CONFIG4 */ | |
559 | return f->config[3]; | |
560 | case 0x70: /* GPMC_CONFIG5 */ | |
561 | return f->config[4]; | |
562 | case 0x74: /* GPMC_CONFIG6 */ | |
563 | return f->config[5]; | |
564 | case 0x78: /* GPMC_CONFIG7 */ | |
565 | return f->config[6]; | |
2a952feb PM |
566 | case 0x84 ... 0x87: /* GPMC_NAND_DATA */ |
567 | if (omap_gpmc_devtype(f) == OMAP_GPMC_NAND) { | |
568 | return omap_nand_read(f, 0, size); | |
569 | } | |
9ed3e1b1 | 570 | return 0; |
f3354b0e | 571 | } |
572 | break; | |
573 | ||
574 | case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */ | |
ef20677c | 575 | return s->prefetch.config1; |
f3354b0e | 576 | case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */ |
ef20677c | 577 | return s->prefetch.transfercount; |
f3354b0e | 578 | case 0x1ec: /* GPMC_PREFETCH_CONTROL */ |
ef20677c | 579 | return s->prefetch.startengine; |
f3354b0e | 580 | case 0x1f0: /* GPMC_PREFETCH_STATUS */ |
71963455 PM |
581 | /* NB: The OMAP3 TRM is inconsistent about whether the GPMC |
582 | * FIFOTHRESHOLDSTATUS bit should be set when | |
583 | * FIFOPOINTER > FIFOTHRESHOLD or when it is >= FIFOTHRESHOLD. | |
584 | * Apparently the underlying functional spec from which the TRM was | |
585 | * created states that the behaviour is ">=", and this also | |
586 | * makes more conceptual sense. | |
587 | */ | |
ef20677c PM |
588 | return (s->prefetch.fifopointer << 24) | |
589 | ((s->prefetch.fifopointer >= | |
590 | ((s->prefetch.config1 >> 8) & 0x7f) ? 1 : 0) << 16) | | |
591 | s->prefetch.count; | |
f3354b0e | 592 | |
593 | case 0x1f4: /* GPMC_ECC_CONFIG */ | |
594 | return s->ecc_cs; | |
595 | case 0x1f8: /* GPMC_ECC_CONTROL */ | |
596 | return s->ecc_ptr; | |
597 | case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */ | |
598 | return s->ecc_cfg; | |
599 | case 0x200 ... 0x220: /* GPMC_ECC_RESULT */ | |
600 | cs = (addr & 0x1f) >> 2; | |
601 | /* TODO: check correctness */ | |
602 | return | |
603 | ((s->ecc[cs].cp & 0x07) << 0) | | |
604 | ((s->ecc[cs].cp & 0x38) << 13) | | |
605 | ((s->ecc[cs].lp[0] & 0x1ff) << 3) | | |
606 | ((s->ecc[cs].lp[1] & 0x1ff) << 19); | |
607 | ||
608 | case 0x230: /* GPMC_TESTMODE_CTRL */ | |
609 | return 0; | |
610 | case 0x234: /* GPMC_PSA_LSB */ | |
611 | case 0x238: /* GPMC_PSA_MSB */ | |
612 | return 0x00000000; | |
613 | } | |
614 | ||
615 | OMAP_BAD_REG(addr); | |
616 | return 0; | |
617 | } | |
618 | ||
a8170e5e | 619 | static void omap_gpmc_write(void *opaque, hwaddr addr, |
64066a8f | 620 | uint64_t value, unsigned size) |
f3354b0e | 621 | { |
622 | struct omap_gpmc_s *s = (struct omap_gpmc_s *) opaque; | |
623 | int cs; | |
624 | struct omap_gpmc_cs_file_s *f; | |
625 | ||
2a952feb | 626 | if (size != 4 && gpmc_wordaccess_only(addr)) { |
64066a8f AK |
627 | return omap_badwidth_write32(opaque, addr, value); |
628 | } | |
629 | ||
f3354b0e | 630 | switch (addr) { |
631 | case 0x000: /* GPMC_REVISION */ | |
632 | case 0x014: /* GPMC_SYSSTATUS */ | |
633 | case 0x054: /* GPMC_STATUS */ | |
634 | case 0x1f0: /* GPMC_PREFETCH_STATUS */ | |
635 | case 0x200 ... 0x220: /* GPMC_ECC_RESULT */ | |
636 | case 0x234: /* GPMC_PSA_LSB */ | |
637 | case 0x238: /* GPMC_PSA_MSB */ | |
638 | OMAP_RO_REG(addr); | |
639 | break; | |
640 | ||
641 | case 0x010: /* GPMC_SYSCONFIG */ | |
642 | if ((value >> 3) == 0x3) | |
64066a8f | 643 | fprintf(stderr, "%s: bad SDRAM idle mode %"PRIi64"\n", |
f3354b0e | 644 | __FUNCTION__, value >> 3); |
645 | if (value & 2) | |
646 | omap_gpmc_reset(s); | |
647 | s->sysconfig = value & 0x19; | |
648 | break; | |
649 | ||
650 | case 0x018: /* GPMC_IRQSTATUS */ | |
7e36b264 | 651 | s->irqst &= ~value; |
f3354b0e | 652 | omap_gpmc_int_update(s); |
653 | break; | |
654 | ||
655 | case 0x01c: /* GPMC_IRQENABLE */ | |
656 | s->irqen = value & 0xf03; | |
657 | omap_gpmc_int_update(s); | |
658 | break; | |
659 | ||
660 | case 0x040: /* GPMC_TIMEOUT_CONTROL */ | |
661 | s->timeout = value & 0x1ff1; | |
662 | break; | |
663 | ||
664 | case 0x044: /* GPMC_ERR_ADDRESS */ | |
665 | case 0x048: /* GPMC_ERR_TYPE */ | |
666 | break; | |
667 | ||
668 | case 0x050: /* GPMC_CONFIG */ | |
669 | s->config = value & 0xf13; | |
670 | break; | |
671 | ||
672 | case 0x060 ... 0x1d4: | |
673 | cs = (addr - 0x060) / 0x30; | |
674 | addr -= cs * 0x30; | |
675 | f = s->cs_file + cs; | |
676 | switch (addr) { | |
9ed3e1b1 PM |
677 | case 0x60: /* GPMC_CONFIG1 */ |
678 | f->config[0] = value & 0xffef3e13; | |
679 | break; | |
680 | case 0x64: /* GPMC_CONFIG2 */ | |
681 | f->config[1] = value & 0x001f1f8f; | |
682 | break; | |
683 | case 0x68: /* GPMC_CONFIG3 */ | |
684 | f->config[2] = value & 0x001f1f8f; | |
685 | break; | |
686 | case 0x6c: /* GPMC_CONFIG4 */ | |
687 | f->config[3] = value & 0x1f8f1f8f; | |
688 | break; | |
689 | case 0x70: /* GPMC_CONFIG5 */ | |
690 | f->config[4] = value & 0x0f1f1f1f; | |
691 | break; | |
692 | case 0x74: /* GPMC_CONFIG6 */ | |
693 | f->config[5] = value & 0x00000fcf; | |
694 | break; | |
695 | case 0x78: /* GPMC_CONFIG7 */ | |
696 | if ((f->config[6] ^ value) & 0xf7f) { | |
697 | omap_gpmc_cs_unmap(s, cs); | |
698 | f->config[6] = value & 0x00000f7f; | |
699 | omap_gpmc_cs_map(s, cs); | |
700 | } | |
701 | break; | |
2a952feb PM |
702 | case 0x7c ... 0x7f: /* GPMC_NAND_COMMAND */ |
703 | if (omap_gpmc_devtype(f) == OMAP_GPMC_NAND) { | |
704 | nand_setpins(f->dev, 1, 0, 0, 1, 0); /* CLE */ | |
705 | omap_nand_setio(f->dev, value, omap_gpmc_devsize(f), size); | |
706 | } | |
707 | break; | |
708 | case 0x80 ... 0x83: /* GPMC_NAND_ADDRESS */ | |
709 | if (omap_gpmc_devtype(f) == OMAP_GPMC_NAND) { | |
710 | nand_setpins(f->dev, 0, 1, 0, 1, 0); /* ALE */ | |
711 | omap_nand_setio(f->dev, value, omap_gpmc_devsize(f), size); | |
712 | } | |
713 | break; | |
714 | case 0x84 ... 0x87: /* GPMC_NAND_DATA */ | |
715 | if (omap_gpmc_devtype(f) == OMAP_GPMC_NAND) { | |
716 | omap_nand_write(f, 0, value, size); | |
717 | } | |
9ed3e1b1 | 718 | break; |
9ed3e1b1 PM |
719 | default: |
720 | goto bad_reg; | |
f3354b0e | 721 | } |
722 | break; | |
723 | ||
724 | case 0x1e0: /* GPMC_PREFETCH_CONFIG1 */ | |
d5c8cf99 | 725 | if (!s->prefetch.startengine) { |
0ec6dc73 | 726 | uint32_t newconfig1 = value & 0x7f8f7fbf; |
d5c8cf99 | 727 | uint32_t changed; |
0ec6dc73 | 728 | changed = newconfig1 ^ s->prefetch.config1; |
d5c8cf99 PM |
729 | if (changed & (0x80 | 0x7000000)) { |
730 | /* Turning the engine on or off, or mapping it somewhere else. | |
731 | * cs_map() and cs_unmap() check the prefetch config and | |
732 | * overall CSVALID bits, so it is sufficient to unmap-and-map | |
0ec6dc73 PM |
733 | * both the old cs and the new one. Note that we adhere to |
734 | * the "unmap/change config/map" order (and not unmap twice | |
735 | * if newcs == oldcs), otherwise we'll try to delete the wrong | |
736 | * memory region. | |
d5c8cf99 | 737 | */ |
0ec6dc73 PM |
738 | int oldcs = prefetch_cs(s->prefetch.config1); |
739 | int newcs = prefetch_cs(newconfig1); | |
d5c8cf99 | 740 | omap_gpmc_cs_unmap(s, oldcs); |
0ec6dc73 | 741 | if (oldcs != newcs) { |
d5c8cf99 | 742 | omap_gpmc_cs_unmap(s, newcs); |
0ec6dc73 PM |
743 | } |
744 | s->prefetch.config1 = newconfig1; | |
745 | omap_gpmc_cs_map(s, oldcs); | |
746 | if (oldcs != newcs) { | |
d5c8cf99 PM |
747 | omap_gpmc_cs_map(s, newcs); |
748 | } | |
0ec6dc73 PM |
749 | } else { |
750 | s->prefetch.config1 = newconfig1; | |
d5c8cf99 PM |
751 | } |
752 | } | |
f3354b0e | 753 | break; |
754 | ||
755 | case 0x1e4: /* GPMC_PREFETCH_CONFIG2 */ | |
d5c8cf99 PM |
756 | if (!s->prefetch.startengine) { |
757 | s->prefetch.transfercount = value & 0x3fff; | |
758 | } | |
f3354b0e | 759 | break; |
760 | ||
761 | case 0x1ec: /* GPMC_PREFETCH_CONTROL */ | |
d5c8cf99 PM |
762 | if (s->prefetch.startengine != (value & 1)) { |
763 | s->prefetch.startengine = value & 1; | |
764 | if (s->prefetch.startengine) { | |
765 | /* Prefetch engine start */ | |
766 | s->prefetch.count = s->prefetch.transfercount; | |
767 | if (s->prefetch.config1 & 1) { | |
768 | /* Write */ | |
769 | s->prefetch.fifopointer = 64; | |
770 | } else { | |
771 | /* Read */ | |
772 | s->prefetch.fifopointer = 0; | |
773 | fill_prefetch_fifo(s); | |
774 | } | |
ef20677c | 775 | } else { |
d5c8cf99 PM |
776 | /* Prefetch engine forcibly stopped. The TRM |
777 | * doesn't define the behaviour if you do this. | |
778 | * We clear the prefetch count, which means that | |
779 | * we permit no more writes, and don't read any | |
780 | * more data from NAND. The CPU can still drain | |
781 | * the FIFO of unread data. | |
782 | */ | |
783 | s->prefetch.count = 0; | |
ef20677c | 784 | } |
d5c8cf99 | 785 | omap_gpmc_int_update(s); |
f3354b0e | 786 | } |
f3354b0e | 787 | break; |
788 | ||
789 | case 0x1f4: /* GPMC_ECC_CONFIG */ | |
790 | s->ecc_cs = 0x8f; | |
791 | break; | |
792 | case 0x1f8: /* GPMC_ECC_CONTROL */ | |
793 | if (value & (1 << 8)) | |
794 | for (cs = 0; cs < 9; cs ++) | |
795 | ecc_reset(&s->ecc[cs]); | |
796 | s->ecc_ptr = value & 0xf; | |
797 | if (s->ecc_ptr == 0 || s->ecc_ptr > 9) { | |
798 | s->ecc_ptr = 0; | |
799 | s->ecc_cs &= ~1; | |
800 | } | |
801 | break; | |
802 | case 0x1fc: /* GPMC_ECC_SIZE_CONFIG */ | |
803 | s->ecc_cfg = value & 0x3fcff1ff; | |
804 | break; | |
805 | case 0x230: /* GPMC_TESTMODE_CTRL */ | |
806 | if (value & 7) | |
807 | fprintf(stderr, "%s: test mode enable attempt\n", __FUNCTION__); | |
808 | break; | |
809 | ||
810 | default: | |
811 | bad_reg: | |
812 | OMAP_BAD_REG(addr); | |
813 | return; | |
814 | } | |
815 | } | |
816 | ||
64066a8f AK |
817 | static const MemoryRegionOps omap_gpmc_ops = { |
818 | .read = omap_gpmc_read, | |
819 | .write = omap_gpmc_write, | |
820 | .endianness = DEVICE_NATIVE_ENDIAN, | |
f3354b0e | 821 | }; |
822 | ||
b5325c27 | 823 | struct omap_gpmc_s *omap_gpmc_init(struct omap_mpu_state_s *mpu, |
a8170e5e | 824 | hwaddr base, |
eee0a1c6 | 825 | qemu_irq irq, qemu_irq drq) |
f3354b0e | 826 | { |
2a952feb | 827 | int cs; |
f3354b0e | 828 | struct omap_gpmc_s *s = (struct omap_gpmc_s *) |
7267c094 | 829 | g_malloc0(sizeof(struct omap_gpmc_s)); |
f3354b0e | 830 | |
2c9b15ca | 831 | memory_region_init_io(&s->iomem, NULL, &omap_gpmc_ops, s, "omap-gpmc", 0x1000); |
64066a8f | 832 | memory_region_add_subregion(get_system_memory(), base, &s->iomem); |
f3354b0e | 833 | |
77c6c736 | 834 | s->irq = irq; |
eee0a1c6 | 835 | s->drq = drq; |
856f2df7 | 836 | s->accept_256 = cpu_is_omap3630(mpu); |
7c470ff1 | 837 | s->revision = cpu_class_omap3(mpu) ? 0x50 : 0x20; |
d5c8cf99 | 838 | s->lastirq = 0; |
07bc2f80 PM |
839 | omap_gpmc_reset(s); |
840 | ||
2a952feb PM |
841 | /* We have to register a different IO memory handler for each |
842 | * chip select region in case a NAND device is mapped there. We | |
843 | * make the region the worst-case size of 256MB and rely on the | |
844 | * container memory region in cs_map to chop it down to the actual | |
845 | * guest-requested size. | |
846 | */ | |
847 | for (cs = 0; cs < 8; cs++) { | |
2c9b15ca | 848 | memory_region_init_io(&s->cs_file[cs].nandiomem, NULL, |
2a952feb PM |
849 | &omap_nand_ops, |
850 | &s->cs_file[cs], | |
851 | "omap-nand", | |
852 | 256 * 1024 * 1024); | |
853 | } | |
d5c8cf99 | 854 | |
2c9b15ca | 855 | memory_region_init_io(&s->prefetch.iomem, NULL, &omap_prefetch_ops, s, |
d5c8cf99 | 856 | "omap-gpmc-prefetch", 256 * 1024 * 1024); |
f3354b0e | 857 | return s; |
858 | } | |
859 | ||
07bc2f80 | 860 | void omap_gpmc_attach(struct omap_gpmc_s *s, int cs, MemoryRegion *iomem) |
f3354b0e | 861 | { |
862 | struct omap_gpmc_cs_file_s *f; | |
07bc2f80 | 863 | assert(iomem); |
f3354b0e | 864 | |
865 | if (cs < 0 || cs >= 8) { | |
866 | fprintf(stderr, "%s: bad chip-select %i\n", __FUNCTION__, cs); | |
867 | exit(-1); | |
868 | } | |
869 | f = &s->cs_file[cs]; | |
870 | ||
3387bf55 | 871 | omap_gpmc_cs_unmap(s, cs); |
2a952feb | 872 | f->config[0] &= ~(0xf << 10); |
64066a8f | 873 | f->iomem = iomem; |
3387bf55 | 874 | omap_gpmc_cs_map(s, cs); |
f3354b0e | 875 | } |
2a952feb PM |
876 | |
877 | void omap_gpmc_attach_nand(struct omap_gpmc_s *s, int cs, DeviceState *nand) | |
878 | { | |
879 | struct omap_gpmc_cs_file_s *f; | |
880 | assert(nand); | |
881 | ||
882 | if (cs < 0 || cs >= 8) { | |
883 | fprintf(stderr, "%s: bad chip-select %i\n", __func__, cs); | |
884 | exit(-1); | |
885 | } | |
886 | f = &s->cs_file[cs]; | |
887 | ||
888 | omap_gpmc_cs_unmap(s, cs); | |
889 | f->config[0] &= ~(0xf << 10); | |
890 | f->config[0] |= (OMAP_GPMC_NAND << 10); | |
891 | f->dev = nand; | |
892 | if (nand_getbuswidth(f->dev) == 16) { | |
893 | f->config[0] |= OMAP_GPMC_16BIT << 12; | |
894 | } | |
895 | omap_gpmc_cs_map(s, cs); | |
896 | } |