2 * QEMU JZ Soc emulation
4 * Copyright (c) 2009 yajin (yajin@vm-kernel.org)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * The emulation target is pavo demo board.
28 * http://www.ingenic.cn/eng/productServ/kfyd/Hardware/pffaqQuestionContent.aspx?Category=2&Question=3
35 #include "qemu-timer.h"
36 #include "qemu-char.h"
39 #include "audio/audio.h"
45 #define DEBUG /*global debug on/off */
47 #define DEBUG_CPM (1<<0x0)
48 #define DEBUG_EMC (1<<0x1)
49 #define DEBUG_GPIO (1<<0x2)
50 #define DEBUG_RTC (1<<0x3)
51 #define DEBUG_TCU (1<<0x4)
52 #define DEBUG_LCDC (1<<0x5)
53 #define DEBUG_DMA (1<<0x6)
54 #define DEBUG_FLAG DEBUG_RTC //(DEBUG_CPM|DEBUG_EMC|DEBUG_GPIO \
55 // | DEBUG_RTC | DEBUG_TCU | DEBUG_LCDC | DEBUG_DMA)
56 //DEBUG_TCU// (DEBUG_CPM|DEBUG_EMC|DEBUG_GPIO
57 // | DEBUG_RTC | DEBUG_TCU | DEBUG_LCDC | DEBUG_DMA)
62 static void debug_init(void)
64 fp
= fopen("jz4740.txt", "w+");
67 fprintf(stderr
, "can not open jz4740.txt \n");
71 static void debug_out(uint32_t flag
, const char *format
, ...)
76 if (flag
& DEBUG_FLAG
)
79 vfprintf(fp
, format
, ap
);
86 static void debug_init(void)
89 static void debug_out(uint32_t flag
, const char *format
, ...)
94 uint32_t jz4740_badwidth_read8(void *opaque
, target_phys_addr_t addr
)
99 cpu_physical_memory_read(addr
, (void *) &ret
, 1);
103 void jz4740_badwidth_write8(void *opaque
, target_phys_addr_t addr
,
106 uint8_t val8
= value
;
109 cpu_physical_memory_write(addr
, (void *) &val8
, 1);
112 uint32_t jz4740_badwidth_read16(void *opaque
, target_phys_addr_t addr
)
115 JZ4740_16B_REG(addr
);
116 cpu_physical_memory_read(addr
, (void *) &ret
, 2);
120 void jz4740_badwidth_write16(void *opaque
, target_phys_addr_t addr
,
123 uint16_t val16
= value
;
125 JZ4740_16B_REG(addr
);
126 cpu_physical_memory_write(addr
, (void *) &val16
, 2);
129 uint32_t jz4740_badwidth_read32(void *opaque
, target_phys_addr_t addr
)
133 JZ4740_32B_REG(addr
);
134 cpu_physical_memory_read(addr
, (void *) &ret
, 4);
138 void jz4740_badwidth_write32(void *opaque
, target_phys_addr_t addr
,
141 JZ4740_32B_REG(addr
);
142 cpu_physical_memory_write(addr
, (void *) &value
, 4);
146 /*clock reset and power control*/
149 target_phys_addr_t base
;
150 struct jz_state_s
*soc
;
166 static void jz4740_dump_clocks(jz_clk parent
)
170 debug_out(DEBUG_CPM
, "clock %s rate %d \n", i
->name
, i
->rate
);
171 for (i
= i
->child1
; i
; i
= i
->sibling
)
172 jz4740_dump_clocks(i
);
175 static inline void jz4740_cpccr_update(struct jz4740_cpm_s
*s
,
178 uint32_t ldiv
, mdiv
, pdiv
, hdiv
, cdiv
, udiv
;
179 uint32_t div_table
[10] = {
180 1, 2, 3, 4, 6, 8, 12, 16, 24, 32
183 if (unlikely(new_value
== s
->cpccr
))
186 if (new_value
& CPM_CPCCR_PCS
)
187 jz_clk_setrate(jz_findclk(s
->soc
, "pll_divider"), 1, 1);
189 jz_clk_setrate(jz_findclk(s
->soc
, "pll_divider"), 2, 1);
192 ldiv
= (new_value
& CPM_CPCCR_LDIV_MASK
) >> CPM_CPCCR_LDIV_BIT
;
195 mdiv
= div_table
[(new_value
& CPM_CPCCR_MDIV_MASK
) >> CPM_CPCCR_MDIV_BIT
];
196 pdiv
= div_table
[(new_value
& CPM_CPCCR_PDIV_MASK
) >> CPM_CPCCR_PDIV_BIT
];
197 hdiv
= div_table
[(new_value
& CPM_CPCCR_HDIV_MASK
) >> CPM_CPCCR_HDIV_BIT
];
198 cdiv
= div_table
[(new_value
& CPM_CPCCR_CDIV_MASK
) >> CPM_CPCCR_CDIV_BIT
];
199 udiv
= div_table
[(new_value
& CPM_CPCCR_UDIV_MASK
) >> CPM_CPCCR_UDIV_BIT
];
201 jz_clk_setrate(jz_findclk(s
->soc
, "ldclk"), ldiv
, 1);
202 jz_clk_setrate(jz_findclk(s
->soc
, "mclk"), mdiv
, 1);
203 jz_clk_setrate(jz_findclk(s
->soc
, "pclk"), pdiv
, 1);
204 jz_clk_setrate(jz_findclk(s
->soc
, "hclk"), hdiv
, 1);
205 jz_clk_setrate(jz_findclk(s
->soc
, "cclk"), cdiv
, 1);
206 jz_clk_setrate(jz_findclk(s
->soc
, "usbclk"), udiv
, 1);
208 if (new_value
& CPM_CPCCR_UCS
)
209 jz_clk_reparent(jz_findclk(s
->soc
, "usbclk"),
210 jz_findclk(s
->soc
, "pll_divider"));
212 jz_clk_reparent(jz_findclk(s
->soc
, "usbclk"),
213 jz_findclk(s
->soc
, "osc_extal"));
215 if (new_value
& CPM_CPCCR_I2CS
)
216 jz_clk_reparent(jz_findclk(s
->soc
, "i2sclk"),
217 jz_findclk(s
->soc
, "pll_divider"));
219 jz_clk_reparent(jz_findclk(s
->soc
, "i2sclk"),
220 jz_findclk(s
->soc
, "osc_extal"));
222 s
->cpccr
= new_value
;
224 debug_out(DEBUG_CPM
, "write to cpccr 0x%x\n", new_value
);
226 jz4740_dump_clocks(jz_findclk(s
->soc
, "osc_extal"));
230 static inline void jz4740_cppcr_update(struct jz4740_cpm_s
*s
,
233 uint32_t pllm
, plln
, pllod
, pllbp
, pllen
;
239 pllen
= new_value
& CPM_CPPCR_PLLEN
;
240 pllbp
= new_value
& CPM_CPPCR_PLLBP
;
241 if ((!pllen
) || (pllen
&& pllbp
))
243 jz_clk_setrate(jz_findclk(s
->soc
, "pll_output"), 1, 1);
244 debug_out(DEBUG_CPM
, "pll is bypassed \n");
245 s
->cppcr
= new_value
| CPM_CPPCR_PLLS
;
250 pllm
= (new_value
& CPM_CPPCR_PLLM_MASK
) >> CPM_CPPCR_PLLM_BIT
;
251 plln
= (new_value
& CPM_CPPCR_PLLN_MASK
) >> CPM_CPPCR_PLLN_BIT
;
252 pllod
= (new_value
& CPM_CPPCR_PLLOD_MASK
) >> CPM_CPPCR_PLLOD_BIT
;
253 jz_clk_setrate(jz_findclk(s
->soc
, "pll_output"), (plln
+ 2) * pll0
[pllod
],
256 s
->cppcr
= new_value
;
258 debug_out(DEBUG_CPM
, "write to cppcr 0x%x\n", new_value
);
259 jz4740_dump_clocks(jz_findclk(s
->soc
, "osc_extal"));
263 static inline void jz4740_i2scdr_update(struct jz4740_cpm_s
*s
,
268 i2scdr
= new_value
& CPM_I2SCDR_I2SDIV_MASK
;
269 if (unlikely(i2scdr
== s
->i2scdr
))
273 jz_clk_setrate(jz_findclk(s
->soc
, "i2sclk"), i2scdr
+ 1, 1);
277 debug_out(DEBUG_CPM
, "write to i2scdr 0x%x\n", new_value
);
278 jz4740_dump_clocks(jz_findclk(s
->soc
, "osc_extal"));
282 static inline void jz4740_lpcdr_update(struct jz4740_cpm_s
*s
,
287 ipcdr
= new_value
& CPM_LPCDR_PIXDIV_MASK
;
292 static inline void jz4740_msccdr_update(struct jz4740_cpm_s
*s
,
297 msccdr
= new_value
& CPM_MSCCDR_MSCDIV_MASK
;
299 if (unlikely(msccdr
== s
->msccdr
))
303 jz_clk_setrate(jz_findclk(s
->soc
, "mscclk"), msccdr
+ 1, 1);
307 debug_out(DEBUG_CPM
, "write to msccdr 0x%x\n", new_value
);
308 jz4740_dump_clocks(jz_findclk(s
->soc
, "osc_extal"));
312 static inline void jz4740_uhccdr_update(struct jz4740_cpm_s
*s
,
317 uhccdr
= new_value
& 0xf;
322 static void jz4740_cpm_write(void *opaque
, target_phys_addr_t addr
,
325 struct jz4740_cpm_s
*s
= (struct jz4740_cpm_s
*) opaque
;
327 debug_out(DEBUG_CPM
, "write to cpm addr %x value 0x%x\n", addr
, value
);
332 jz4740_cpccr_update(s
, value
);
335 s
->lcr
= value
& 0xff;
338 s
->clkgr
= value
& 0xffff;
341 s
->scr
= value
& 0xffff;
344 jz4740_cppcr_update(s
, value
);
347 jz4740_i2scdr_update(s
, value
);
350 jz4740_lpcdr_update(s
, value
);
353 jz4740_msccdr_update(s
, value
);
356 jz4740_uhccdr_update(s
, value
);
359 s
->uhctst
= value
& 0x3f;
362 s
->ssicdr
= value
& 0xf;
365 cpu_abort(s
->soc
->env
,
366 "jz4740_cpm_write undefined addr " JZ_FMT_plx
367 " value %x \n", addr
, value
);
373 static uint32_t jz474_cpm_read(void *opaque
, target_phys_addr_t addr
)
375 struct jz4740_cpm_s
*s
= (struct jz4740_cpm_s
*) opaque
;
402 cpu_abort(s
->soc
->env
,
403 "jz474_cpm_read undefined addr " JZ_FMT_plx
" \n", addr
);
410 static CPUReadMemoryFunc
*jz4740_cpm_readfn
[] = {
411 jz4740_badwidth_read32
,
412 jz4740_badwidth_read32
,
416 static CPUWriteMemoryFunc
*jz4740_cpm_writefn
[] = {
417 jz4740_badwidth_write32
,
418 jz4740_badwidth_write32
,
422 static void jz4740_cpm_reset(struct jz4740_cpm_s
*s
)
424 s
->cpccr
= 0x42040000;
425 s
->cppcr
= 0x28080011;
426 s
->i2scdr
= 0x00000004;
427 s
->lpcdr
= 0x00000004;
428 s
->msccdr
= 0x00000004;
429 s
->uhccdr
= 0x00000004;
431 s
->ssicdr
= 0x00000004;
438 static struct jz4740_cpm_s
*jz4740_cpm_init(struct jz_state_s
*soc
)
441 struct jz4740_cpm_s
*s
= (struct jz4740_cpm_s
*) qemu_mallocz(sizeof(*s
));
442 s
->base
= JZ4740_PHYS_BASE(JZ4740_CPM_BASE
);
448 cpu_register_io_memory(0, jz4740_cpm_readfn
, jz4740_cpm_writefn
, s
);
449 cpu_register_physical_memory(s
->base
, 0x00001000, iomemtype
);
454 /* JZ4740 interrupt controller
455 * It issues INT2 to MIPS
461 target_phys_addr_t base
;
462 struct jz_state_s
*soc
;
471 static uint32_t jz4740_intc_read(void *opaque
, target_phys_addr_t addr
)
473 struct jz4740_intc_s
*s
= (struct jz4740_intc_s
*) opaque
;
488 cpu_abort(s
->soc
->env
,
489 "jz4740_intc_read undefined addr " JZ_FMT_plx
" \n", addr
);
495 static void jz4740_intc_write(void *opaque
, target_phys_addr_t addr
,
498 struct jz4740_intc_s
*s
= (struct jz4740_intc_s
*) opaque
;
516 qemu_set_irq(s
->parent_irq
, 0);
519 cpu_abort(s
->soc
->env
,
520 "jz4740_intc_write undefined addr " JZ_FMT_plx
521 " value %x \n", addr
, value
);
526 static CPUReadMemoryFunc
*jz4740_intc_readfn
[] = {
527 jz4740_badwidth_read32
,
528 jz4740_badwidth_read32
,
532 static CPUWriteMemoryFunc
*jz4740_intc_writefn
[] = {
533 jz4740_badwidth_write32
,
534 jz4740_badwidth_write32
,
538 static void jz4740_intc_reset(struct jz4740_intc_s
*s
)
541 s
->icmr
= 0xffffffff;
545 static void jz4740_set_irq(void *opaque
, int irq
, int level
)
547 struct jz4740_intc_s
*s
= (struct jz4740_intc_s
*) opaque
;
548 uint32_t irq_mask
= 1 << irq
;
554 s
->icpr
&= ~irq_mask
;
555 if (!(s
->icmr
& irq_mask
))
558 qemu_set_irq(s
->parent_irq
, 1);
564 static qemu_irq
*jz4740_intc_init(struct jz_state_s
*soc
, qemu_irq parent_irq
)
567 struct jz4740_intc_s
*s
= (struct jz4740_intc_s
*) qemu_mallocz(sizeof(*s
));
568 s
->base
= JZ4740_PHYS_BASE(JZ4740_INTC_BASE
);
569 s
->parent_irq
= parent_irq
;
572 jz4740_intc_reset(s
);
575 cpu_register_io_memory(0, jz4740_intc_readfn
, jz4740_intc_writefn
, s
);
576 cpu_register_physical_memory(s
->base
, 0x00001000, iomemtype
);
577 return qemu_allocate_irqs(jz4740_set_irq
, s
, 32);
580 /*external memory controller*/
584 target_phys_addr_t base
;
585 struct jz_state_s
*soc
;
588 uint32_t smcr1
; /*0x13010014 */
589 uint32_t smcr2
; /*0x13010018 */
590 uint32_t smcr3
; /*0x1301001c */
591 uint32_t smcr4
; /*0x13010020 */
592 uint32_t sacr1
; /*0x13010034 */
593 uint32_t sacr2
; /*0x13010038 */
594 uint32_t sacr3
; /*0x1301003c */
595 uint32_t sacr4
; /*0x13010040 */
597 uint32_t nfcsr
; /*0x13010050 */
598 uint32_t nfeccr
; /*0x13010100 */
599 uint32_t nfecc
; /*0x13010104 */
600 uint32_t nfpar0
; /*0x13010108 */
601 uint32_t nfpar1
; /*0x1301010c */
602 uint32_t nfpar2
; /*0x13010110 */
603 uint32_t nfints
; /*0x13010114 */
604 uint32_t nfinte
; /*0x13010118 */
605 uint32_t nferr0
; /*0x1301011c */
606 uint32_t nferr1
; /*0x13010120 */
607 uint32_t nferr2
; /*0x13010124 */
608 uint32_t nferr3
; /*0x13010128 */
610 uint32_t dmcr
; /*0x13010080 */
611 uint32_t rtcsr
; /*0x13010084 */
612 uint32_t rtcnt
; /*0x13010088 */
613 uint32_t rtcor
; /*0x1301008c */
614 uint32_t dmar
; /*0x13010090 */
615 uint32_t sdmr
; /*0x1301a000 */
620 static void jz4740_emc_reset(struct jz4740_emc_s
*s
)
622 s
->smcr1
= 0xfff7700;
623 s
->smcr2
= 0xfff7700;
624 s
->smcr3
= 0xfff7700;
625 s
->smcr4
= 0xfff7700;
653 static uint32_t jz4740_emc_read8(void *opaque
, target_phys_addr_t addr
)
655 struct jz4740_emc_s
*s
= (struct jz4740_emc_s
*) opaque
;
663 return (s
->nfpar0
>> ((addr
- 0x108) * 8)) & 0xff;
668 return (s
->nfpar1
>> ((addr
- 0x10c) * 8)) & 0xff;
673 return (s
->nfpar2
>> ((addr
- 0x110) * 8)) & 0xff;
678 return (s
->sdmr
>> ((addr
- 0xa000) * 8)) & 0xff;
680 cpu_abort(s
->soc
->env
,
681 "jz4740_emc_read8 undefined addr " JZ_FMT_plx
" \n", addr
);
688 static uint32_t jz4740_emc_read16(void *opaque
, target_phys_addr_t addr
)
690 struct jz4740_emc_s
*s
= (struct jz4740_emc_s
*) opaque
;
696 return (s
->nfpar0
>> ((addr
- 0x108) * 8)) & 0xffff;
699 return (s
->nfpar1
>> ((addr
- 0x10c) * 8)) & 0xffff;
702 return (s
->nfpar2
>> ((addr
- 0x110) * 8)) & 0xffff;
705 return (s
->nferr0
>> ((addr
- 0x11c) * 8)) & 0xffff;
708 return (s
->nferr1
>> ((addr
- 0x120) * 8)) & 0xffff;
711 return (s
->nferr2
>> ((addr
- 0x124) * 8)) & 0xffff;
714 return (s
->nferr3
>> ((addr
- 0x128) * 8)) & 0xffff;
716 cpu_abort(s
->soc
->env
,
717 "jz4740_emc_read16 undefined addr " JZ_FMT_plx
" \n", addr
);
722 static uint32_t jz4740_emc_read32(void *opaque
, target_phys_addr_t addr
)
724 struct jz4740_emc_s
*s
= (struct jz4740_emc_s
*) opaque
;
775 cpu_abort(s
->soc
->env
,
776 "jz4740_emc_read32 undefined addr " JZ_FMT_plx
" \n", addr
);
781 static void jz4740_emc_write8(void *opaque
, target_phys_addr_t addr
,
784 struct jz4740_emc_s
*s
= (struct jz4740_emc_s
*) opaque
;
786 debug_out(DEBUG_EMC
, "jz4740_emc_write8 addr %x value %x\n", addr
, value
);
794 s
->nfpar0
|= (value
& 0xff) << ((addr
- 0x108) * 8);
800 s
->nfpar1
|= (value
& 0xff) << ((addr
- 0x10c) * 8);
806 s
->nfpar2
|= (value
& 0xff) << ((addr
- 0x110) * 8);
808 case 0xa000 ... 0xa3ff:
811 cpu_abort(s
->soc
->env
,
812 "jz4740_emc_write8 undefined addr " JZ_FMT_plx
813 " value %x \n", addr
, value
);
816 static void jz4740_emc_write16(void *opaque
, target_phys_addr_t addr
,
819 struct jz4740_emc_s
*s
= (struct jz4740_emc_s
*) opaque
;
821 debug_out(DEBUG_EMC
, "jz4740_emc_write16 addr %x value %x\n", addr
, value
);
826 s
->nfpar0
|= (value
& 0xffff) << ((addr
- 0x108) * 8);
830 s
->nfpar1
|= (value
& 0xffff) << ((addr
- 0x10c) * 8);
834 s
->nfpar2
|= (value
& 0xffff) << ((addr
- 0x110) * 8);
838 s
->rtcsr
|= (value
& 0xffff) << ((addr
- 0x84) * 8);
842 s
->rtcnt
|= (value
& 0xffff) << ((addr
- 0x88) * 8);
845 s
->rtcor
|= (value
& 0xffff) << ((addr
- 0x8c) * 8);
848 cpu_abort(s
->soc
->env
,
849 "jz4740_emc_write16 undefined addr " JZ_FMT_plx
850 " value %x \n", addr
, value
);
854 static void jz4740_emc_upate_interrupt(struct jz4740_emc_s
*s
)
856 qemu_set_irq(s
->irq
, s
->nfints
& s
->nfinte
);
859 static void jz4740_emc_write32(void *opaque
, target_phys_addr_t addr
,
862 struct jz4740_emc_s
*s
= (struct jz4740_emc_s
*) opaque
;
864 debug_out(DEBUG_EMC
, "jz4740_emc_write32 addr %x value %x\n", addr
, value
);
878 s
->smcr1
= value
& 0xfff77cf;
881 s
->smcr2
= value
& 0xfff77cf;
884 s
->smcr3
= value
& 0xfff77cf;
887 s
->smcr4
= value
& 0xfff77cf;
890 s
->sacr1
= value
& 0xffff;
893 s
->sacr2
= value
& 0xffff;
896 s
->sacr3
= value
& 0xffff;
899 s
->sacr4
= value
& 0xffff;
902 s
->nfcsr
= value
& 0xffff;
905 s
->nfeccr
= value
& 0x1f;
920 /*TODO: Real RS error correction */
923 if ((s
->nfeccr
& 0x10) && (!(s
->nfeccr
& 0x8)))
935 s
->nfpar0
= 0xffffffff; /*fake value. for debug */
936 s
->nfpar1
= 0xffffffff; /*fake value */
937 s
->nfpar2
= 0xff; /*fake value */
944 jz4740_emc_upate_interrupt(s
);
953 s
->nfpar2
= value
& 0xff;
956 s
->nfints
= value
& 0x1fffffff;
957 jz4740_emc_upate_interrupt(s
);
960 s
->nfinte
= value
& 0x1f;
961 jz4740_emc_upate_interrupt(s
);
964 s
->dmcr
= value
& 0x9fbeff7f;
967 s
->dmar
= value
& 0xffff;
970 cpu_abort(s
->soc
->env
,
971 "jz4740_emc_write32 undefined addr " JZ_FMT_plx
972 " value %x \n", addr
, value
);
978 static CPUReadMemoryFunc
*jz4740_emc_readfn
[] = {
984 static CPUWriteMemoryFunc
*jz4740_emc_writefn
[] = {
991 static struct jz4740_emc_s
*jz4740_emc_init(struct jz_state_s
*soc
,
995 struct jz4740_emc_s
*s
= (struct jz4740_emc_s
*) qemu_mallocz(sizeof(*s
));
996 s
->base
= JZ4740_PHYS_BASE(JZ4740_EMC_BASE
);
1000 jz4740_emc_reset(s
);
1003 cpu_register_io_memory(0, jz4740_emc_readfn
, jz4740_emc_writefn
, s
);
1004 cpu_register_physical_memory(s
->base
, 0x00010000, iomemtype
);
1009 struct jz4740_gpio_s
1012 target_phys_addr_t base
;
1013 struct jz_state_s
*soc
;
1026 static void jz4740_gpio_reset(struct jz4740_gpio_s
*s
)
1028 memset(s
->papin
, 0x0, sizeof(s
->papin
));
1029 memset(s
->padat
, 0x0, sizeof(s
->padat
));
1030 memset(s
->paim
, 0xffffffff, sizeof(s
->paim
));
1031 memset(s
->pape
, 0x0, sizeof(s
->pape
));
1032 memset(s
->pafun
, 0x0, sizeof(s
->pafun
));
1033 memset(s
->pasel
, 0x0, sizeof(s
->pasel
));
1034 memset(s
->padir
, 0x0, sizeof(s
->padir
));
1035 memset(s
->patrg
, 0x0, sizeof(s
->patrg
));
1036 memset(s
->paflg
, 0x0, sizeof(s
->paflg
));
1039 static uint32_t jz4740_gpio_read(void *opaque
, target_phys_addr_t addr
)
1041 struct jz4740_gpio_s
*s
= (struct jz4740_gpio_s
*) opaque
;
1043 debug_out(DEBUG_GPIO
, "jz4740_gpio_read addr %x\n", addr
);
1107 JZ4740_WO_REG(addr
);
1114 group
= (addr
- 0x0) / 0x100;
1117 /*GPIO(C) PIN 30 -> NAND FLASH R/B. */
1118 /*FOR NAND FLASH.PIN 30 ----|_____|------ */
1119 s
->papin
[2] &= 0x40000000;
1121 s
->papin
[2] &= ~0x40000000;
1123 s
->papin
[2] |= 0x40000000;
1125 return s
->papin
[group
];
1130 group
= (addr
- 0x10) / 0x100;
1131 return s
->padat
[group
];
1136 group
= (addr
- 0x20) / 0x100;
1137 return s
->paim
[group
];
1142 group
= (addr
- 0x30) / 0x100;
1143 return s
->pape
[group
];
1148 group
= (addr
- 0x40) / 0x100;
1149 return s
->pafun
[group
];
1154 group
= (addr
- 0x50) / 0x100;
1155 return s
->pasel
[group
];
1160 group
= (addr
- 0x60) / 0x100;
1161 return s
->padir
[group
];
1166 group
= (addr
- 0x70) / 0x100;
1167 return s
->patrg
[group
];
1172 group
= (addr
- 0x80) / 0x100;
1173 return s
->paflg
[group
];
1175 cpu_abort(s
->soc
->env
,
1176 "jz4740_gpio_read undefined addr " JZ_FMT_plx
" \n", addr
);
1181 static void jz4740_gpio_write(void *opaque
, target_phys_addr_t addr
,
1184 struct jz4740_gpio_s
*s
= (struct jz4740_gpio_s
*) opaque
;
1187 debug_out(DEBUG_GPIO
, "jz4740_gpio_write addr %x value %x\n", addr
, value
);
1227 JZ4740_RO_REG(addr
);
1233 group
= (addr
- 0x14) / 0x100;
1234 s
->padat
[group
] = value
;
1240 group
= (addr
- 0x18) / 0x100;
1241 s
->padat
[group
] &= ~value
;
1247 group
= (addr
- 0x24) / 0x100;
1248 s
->paim
[group
] = value
;
1254 group
= (addr
- 0x28) / 0x100;
1255 s
->paim
[group
] &= ~value
;
1261 group
= (addr
- 0x34) / 0x100;
1262 s
->pape
[group
] = value
;
1268 group
= (addr
- 0x38) / 0x100;
1269 s
->pape
[group
] &= ~value
;
1275 group
= (addr
- 0x44) / 0x100;
1276 s
->pafun
[group
] = value
;
1282 group
= (addr
- 0x48) / 0x100;
1283 s
->pafun
[group
] &= ~value
;
1289 group
= (addr
- 0x54) / 0x100;
1290 s
->pasel
[group
] = value
;
1296 group
= (addr
- 0x58) / 0x100;
1297 s
->pasel
[group
] &= ~value
;
1303 group
= (addr
- 0x64) / 0x100;
1304 s
->padir
[group
] = value
;
1310 group
= (addr
- 0x68) / 0x100;
1311 s
->padir
[group
] &= ~value
;
1317 group
= (addr
- 0x74) / 0x100;
1318 s
->patrg
[group
] = value
;
1324 group
= (addr
- 0x78) / 0x100;
1325 s
->patrg
[group
] &= ~value
;
1331 group
= (addr
- 0x74) / 0x100;
1332 s
->paflg
[group
] &= ~value
;
1335 cpu_abort(s
->soc
->env
,
1336 "jz4740_gpio_write undefined addr " JZ_FMT_plx
1337 " value %x \n", addr
, value
);
1345 static CPUReadMemoryFunc
*jz4740_gpio_readfn
[] = {
1346 jz4740_badwidth_read32
,
1347 jz4740_badwidth_read32
,
1351 static CPUWriteMemoryFunc
*jz4740_gpio_writefn
[] = {
1352 jz4740_badwidth_write32
,
1353 jz4740_badwidth_write32
,
1357 static struct jz4740_gpio_s
*jz4740_gpio_init(struct jz_state_s
*soc
,
1361 struct jz4740_gpio_s
*s
= (struct jz4740_gpio_s
*) qemu_mallocz(sizeof(*s
));
1362 s
->base
= JZ4740_PHYS_BASE(JZ4740_GPIO_BASE
);
1366 jz4740_gpio_reset(s
);
1369 cpu_register_io_memory(0, jz4740_gpio_readfn
, jz4740_gpio_writefn
, s
);
1370 cpu_register_physical_memory(s
->base
, 0x00010000, iomemtype
);
1378 target_phys_addr_t base
;
1379 struct jz_state_s
*soc
;
1401 static void jz4740_rtc_update_interrupt(struct jz4740_rtc_s
*s
)
1403 /* if (((s->rtcsr & 0x40) && (s->rtcsr & 0x20))
1404 || ((s->rtcsr & 0x10) && (s->rtcsr & 0x8)))
1405 qemu_set_irq(s->irq, 1);*/
1407 // qemu_set_irq(s->irq, 0);
1410 static inline void jz4740_rtc_start(struct jz4740_rtc_s
*s
)
1412 s
->next
= +qemu_get_clock(rt_clock
);
1413 qemu_mod_timer(s
->hz_tm
, s
->next
);
1416 static inline void jz4740_rtc_stop(struct jz4740_rtc_s
*s
)
1418 qemu_del_timer(s
->hz_tm
);
1419 s
->next
= -qemu_get_clock(rt_clock
);
1424 static void jz4740_rtc_hz(void *opaque
)
1426 struct jz4740_rtc_s
*s
= (struct jz4740_rtc_s
*) opaque
;
1429 qemu_mod_timer(s
->hz_tm
, s
->next
);
1436 if (s
->rtcsr
== s
->rtcsar
)
1439 jz4740_rtc_update_interrupt(s
);
1443 static void jz4740_rtc_reset(struct jz4740_rtc_s
*s
)
1449 /*Maybe rtcsr need to be saved to file */
1451 //s->sec_offset = 0;
1452 //qemu_get_timedate(&s->tm, s->sec_offset);
1453 jz4740_rtc_start(s
);
1457 static uint32_t jz4740_rtc_read(void *opaque
, target_phys_addr_t addr
)
1459 struct jz4740_rtc_s
*s
= (struct jz4740_rtc_s
*) opaque
;
1461 debug_out(DEBUG_RTC
, "jz4740_rtc_read addr %x\n", addr
);
1465 return s
->rtccr
| 0x80;
1485 cpu_abort(s
->soc
->env
,
1486 "jz4740_rtc_read undefined addr " JZ_FMT_plx
"\n", addr
);
1492 static void jz4740_rtc_write(void *opaque
, target_phys_addr_t addr
,
1495 struct jz4740_rtc_s
*s
= (struct jz4740_rtc_s
*) opaque
;
1497 debug_out(DEBUG_RTC
, "jz4740_rtc_write addr %x value %x\n", addr
, value
);
1502 s
->rtccr
= value
& 0x2d;
1509 //jz4740_rtc_start(s);
1510 jz4740_rtc_update_interrupt(s
);
1517 //s->sec_offset = qemu_timedate_diff(&s->tm);
1523 s
->rtcgr
= value
& 0x13ffffff;
1526 s
->hcr
= value
& 0x1;
1529 s
->hwfcr
= value
& 0xffe0;
1532 s
->hrcr
= value
& 0xfe0;
1535 s
->hwcr
= value
& 0x1;
1538 s
->hwrsr
= value
& 0x33;
1544 cpu_abort(s
->soc
->env
,
1545 "jz4740_rtc_write undefined addr " JZ_FMT_plx
1546 " value %x \n", addr
, value
);
1551 static CPUReadMemoryFunc
*jz4740_rtc_readfn
[] = {
1552 jz4740_badwidth_read32
,
1553 jz4740_badwidth_read32
,
1557 static CPUWriteMemoryFunc
*jz4740_rtc_writefn
[] = {
1558 jz4740_badwidth_write32
,
1559 jz4740_badwidth_write32
,
1563 static struct jz4740_rtc_s
*jz4740_rtc_init(struct jz_state_s
*soc
,
1567 struct jz4740_rtc_s
*s
= (struct jz4740_rtc_s
*) qemu_mallocz(sizeof(*s
));
1568 s
->base
= JZ4740_PHYS_BASE(JZ4740_RTC_BASE
);
1572 s
->hz_tm
= qemu_new_timer(rt_clock
, jz4740_rtc_hz
, s
);
1574 jz4740_rtc_reset(s
);
1577 cpu_register_io_memory(0, jz4740_rtc_readfn
, jz4740_rtc_writefn
, s
);
1578 cpu_register_physical_memory(s
->base
, 0x00001000, iomemtype
);
1588 target_phys_addr_t base
;
1589 struct jz_state_s
*soc
;
1592 QEMUTimer
*half_timer
[8];
1593 QEMUTimer
*full_timer
[8];
1606 uint32_t prescale
[8];
1610 static void jz4740_tcu_update_interrupt(struct jz4740_tcu_s
*s
)
1612 //printf("s->tfr %x s->tmr %x \n",s->tfr,s->tmr);
1613 if (((s
->tfr
& 0x1) & (~(s
->tmr
& 0x1)))
1614 || ((s
->tfr
& 0x10000) & (~(s
->tmr
& 0x10000))))
1616 qemu_set_irq(s
->tcu_irq0
, 1);
1619 qemu_set_irq(s
->tcu_irq0
, 0);
1621 if (((s
->tfr
& 0x2) & (~(s
->tmr
& 0x2)))
1622 || ((s
->tfr
& 0x20000) & (~(s
->tmr
& 0x20000))))
1624 qemu_set_irq(s
->tcu_irq1
, 1);
1627 qemu_set_irq(s
->tcu_irq1
, 0);
1629 if (((s
->tfr
& 0xfc) & (~(s
->tmr
& 0xfc)))
1630 || ((s
->tfr
& 0xfc0000) & (~(s
->tmr
& 0xfc0000))))
1632 qemu_set_irq(s
->tcu_irq2
, 1);
1635 qemu_set_irq(s
->tcu_irq2
, 0);
1641 #include "mips_jz_glue.h"
1643 #include "mips_jz_glue.h"
1645 #include "mips_jz_glue.h"
1647 #include "mips_jz_glue.h"
1649 #include "mips_jz_glue.h"
1651 #include "mips_jz_glue.h"
1653 #include "mips_jz_glue.h"
1655 #include "mips_jz_glue.h"
1658 #define jz4740_tcu_start(s) do { \
1659 jz4740_tcu_start_half0(s); \
1660 jz4740_tcu_start_full0(s); \
1661 jz4740_tcu_start_half1(s); \
1662 jz4740_tcu_start_full1(s); \
1663 jz4740_tcu_start_half2(s); \
1664 jz4740_tcu_start_full2(s); \
1665 jz4740_tcu_start_half3(s); \
1666 jz4740_tcu_start_full3(s); \
1667 jz4740_tcu_start_half4(s); \
1668 jz4740_tcu_start_full4(s); \
1669 jz4740_tcu_start_half5(s); \
1670 jz4740_tcu_start_full5(s); \
1671 jz4740_tcu_start_half6(s); \
1672 jz4740_tcu_start_full6(s); \
1673 jz4740_tcu_start_half7(s); \
1674 jz4740_tcu_start_full7(s); \
1677 static void jz4740_tcu_if_reset(struct jz4740_tcu_s
*s
)
1685 for (i
= 0; i
< 8; i
++)
1687 s
->tdfr
[i
] = 0xffff;
1688 s
->tdhr
[i
] = 0x8000;
1691 s
->half_timer
[i
] = NULL
;
1692 s
->full_timer
[i
] = NULL
;
1696 static void jz4740_tcu_if_write8(void *opaque
, target_phys_addr_t addr
,
1699 struct jz4740_tcu_s
*s
= (struct jz4740_tcu_s
*) opaque
;
1701 debug_out(DEBUG_TCU
, "jz4740_tcu_if_write8 addr %x value %x\n", addr
,
1707 s
->ter
|= (value
& 0xff);
1708 jz4740_tcu_start(s
);
1711 s
->ter
&= ~(value
& 0xff);
1712 jz4740_tcu_start(s
);
1715 cpu_abort(s
->soc
->env
,
1716 "jz4740_tcu_if_write8 undefined addr " JZ_FMT_plx
1717 " value %x \n", addr
, value
);
1722 static void jz4740_tcu_if_write32(void *opaque
, target_phys_addr_t addr
,
1725 struct jz4740_tcu_s
*s
= (struct jz4740_tcu_s
*) opaque
;
1727 debug_out(DEBUG_TCU
, "jz4740_tcu_if_write32 addr %x value %x\n", addr
,
1730 fprintf(fp
, "jz4740_tcu_if_write32 addr %x value %x\n", addr
, value
);
1734 s
->tsr
|= (value
& 0x100ff);
1735 jz4740_tcu_start(s
);
1738 s
->tsr
&= ~(value
& 0x100ff);
1739 jz4740_tcu_start(s
);
1742 s
->tfr
|= (value
& 0xff00ff);
1745 s
->tfr
&= ~(value
& 0xff00ff);
1748 s
->tmr
|= (value
& 0xff00ff);
1749 jz4740_tcu_update_interrupt(s
);
1752 s
->tmr
&= ~(value
& 0xff00ff);
1753 jz4740_tcu_update_interrupt(s
);
1756 cpu_abort(s
->soc
->env
,
1757 "jz4740_tcu_if_write32 undefined addr " JZ_FMT_plx
1758 " value %x \n", addr
, value
);
1763 static uint32_t jz4740_tcu_if_read8(void *opaque
, target_phys_addr_t addr
)
1765 struct jz4740_tcu_s
*s
= (struct jz4740_tcu_s
*) opaque
;
1767 debug_out(DEBUG_TCU
, "jz4740_tcu_if_read8 addr %x\n", addr
);
1774 cpu_abort(s
->soc
->env
,
1775 "jz4740_tcu_if_read8 undefined addr " JZ_FMT_plx
"\n", addr
);
1780 static uint32_t jz4740_tcu_if_read32(void *opaque
, target_phys_addr_t addr
)
1782 struct jz4740_tcu_s
*s
= (struct jz4740_tcu_s
*) opaque
;
1784 debug_out(DEBUG_TCU
, "jz4740_tcu_if_read32 addr %x\n", addr
);
1795 cpu_abort(s
->soc
->env
,
1796 "jz4740_tcu_if_read32 undefined addr " JZ_FMT_plx
"\n", addr
);
1803 static CPUReadMemoryFunc
*jz4740_tcu_if_readfn
[] = {
1804 jz4740_tcu_if_read8
,
1805 jz4740_badwidth_read32
,
1806 jz4740_tcu_if_read32
,
1809 static CPUWriteMemoryFunc
*jz4740_tcu_if_writefn
[] = {
1810 jz4740_tcu_if_write8
,
1811 jz4740_badwidth_write32
,
1812 jz4740_tcu_if_write32
,
1815 static struct jz4740_tcu_s
*jz4740_tcu_if_init(struct jz_state_s
*soc
,
1823 struct jz4740_tcu_s
*s
= (struct jz4740_tcu_s
*) qemu_mallocz(sizeof(*s
));
1824 s
->base
= JZ4740_PHYS_BASE(JZ4740_TCU_BASE
);
1826 s
->tcu_irq0
= tcu_irq0
;
1827 s
->tcu_irq1
= tcu_irq1
;
1828 s
->tcu_irq2
= tcu_irq2
;
1830 jz4740_tcu_if_reset(s
);
1833 cpu_register_io_memory(0, jz4740_tcu_if_readfn
, jz4740_tcu_if_writefn
,
1835 cpu_register_physical_memory(s
->base
, 0x00000040, iomemtype
);
1840 static void jz4740_tcu_init(struct jz_state_s
*soc
,
1841 struct jz4740_tcu_s
*s
, int timer_index
)
1843 switch (timer_index
)
1846 jz4740_tcu_init0(soc
, s
);
1849 jz4740_tcu_init1(soc
, s
);
1852 jz4740_tcu_init2(soc
, s
);
1855 jz4740_tcu_init3(soc
, s
);
1858 jz4740_tcu_init4(soc
, s
);
1861 jz4740_tcu_init5(soc
, s
);
1864 jz4740_tcu_init6(soc
, s
);
1867 jz4740_tcu_init7(soc
, s
);
1870 cpu_abort(s
->soc
->env
,
1871 "jz4740_tcu_init undefined timer %x \n", timer_index
);
1875 typedef void (*jz4740_lcd_fn_t
) (uint8_t * d
, const uint8_t * s
, int width
,
1876 const uint16_t * pal
);
1877 struct jz_fb_descriptor
1879 uint32_t fdadr
; /* Frame descriptor address register */
1880 uint32_t fsadr
; /* Frame source address register */
1881 uint32_t fidr
; /* Frame ID register */
1882 uint32_t ldcmd
; /* Command register */
1885 struct jz4740_lcdc_s
1889 target_phys_addr_t base
;
1890 struct jz_state_s
*soc
;
1892 DisplayState
*state
;
1893 QEMUConsole
*console
;
1894 jz4740_lcd_fn_t
*line_fn_tab
;
1895 jz4740_lcd_fn_t line_fn
;
1924 uint32_t bpp
; /*bit per second */
1925 uint16_t palette
[256];
1926 uint32_t invalidate
;
1931 static const int jz4740_lcd_bpp
[0x6] = {
1932 1, 2, 4, 8, 16, 32 /*4740 uses 32 bit for 24bpp */
1935 static void jz4740_lcdc_reset(struct jz4740_lcdc_s
*s
)
1940 static uint32_t jz4740_lcdc_read(void *opaque
, target_phys_addr_t addr
)
1942 struct jz4740_lcdc_s
*s
= (struct jz4740_lcdc_s
*) opaque
;
1944 debug_out(DEBUG_LCDC
, "jz4740_lcdc_read addr %x \n", addr
);
1990 cpu_abort(s
->soc
->env
,
1991 "jz4740_lcdc_read undefined addr " JZ_FMT_plx
" \n", addr
);
1997 static void jz4740_lcdc_write(void *opaque
, target_phys_addr_t addr
,
2000 struct jz4740_lcdc_s
*s
= (struct jz4740_lcdc_s
*) opaque
;
2002 debug_out(DEBUG_LCDC
, "jz4740_lcdc_write addr %x value %x\n", addr
, value
);
2009 JZ4740_RO_REG(addr
);
2012 s
->lcdcfg
= value
& 0x80ffffbf;
2015 s
->lcdvsync
= value
& 0x7ff07ff;
2018 s
->lcdhsync
= value
& 0x7ff07ff;
2021 s
->lcdvat
= value
& 0x7ff07ff;
2024 s
->lcddah
= value
& 0x7ff07ff;
2025 s
->width
= (value
& 0x7ff) - ((value
>> 16) & 0x7ff);
2028 s
->height
= (value
& 0x7ff) - ((value
>> 16) & 0x7ff);
2029 s
->lcddav
= value
& 0x7ff07ff;
2032 s
->lcdps
= value
& 0x7ff07ff;
2035 s
->lcdcls
= value
& 0x7ff07ff;
2038 s
->lcdspl
= value
& 0x7ff07ff;
2041 s
->lcdrev
= value
& 0x7ff0000;
2044 s
->lcdctrl
= value
& 0x3fff3fff;
2045 s
->ena
= (value
& 0x8) >> 3;
2046 s
->dis
= (value
& 0x10) >> 4;
2047 s
->bpp
= jz4740_lcd_bpp
[value
& 0x7];
2050 fprintf(stderr
, "bpp =1 is not supported\n");
2053 s
->line_fn
= s
->line_fn_tab
[value
& 0x7];
2056 s
->lcdstate
= value
& 0xbf;
2068 cpu_abort(s
->soc
->env
,
2069 "jz4740_lcdc_write undefined addr " JZ_FMT_plx
" value %x \n",
2075 static CPUReadMemoryFunc
*jz4740_lcdc_readfn
[] = {
2076 jz4740_badwidth_read32
,
2077 jz4740_badwidth_read32
,
2081 static CPUWriteMemoryFunc
*jz4740_lcdc_writefn
[] = {
2082 jz4740_badwidth_write32
,
2083 jz4740_badwidth_write32
,
2087 #include "pixel_ops.h"
2088 #define JZ4740_LCD_PANEL
2090 #include "mips_jz_glue.h"
2092 #include "mips_jz_glue.h"
2094 #include "mips_jz_glue.h"
2096 #include "mips_jz_glue.h"
2098 #include "mips_jz_glue.h"
2099 #undef JZ4740_LCD_PANEL
2101 static void *jz4740_lcd_get_buffer(struct jz4740_lcdc_s
*s
,
2102 target_phys_addr_t addr
)
2106 pd
= cpu_get_physical_page_desc(addr
);
2107 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
)
2109 cpu_abort(cpu_single_env
, "%s: framebuffer outside RAM!\n",
2112 return phys_ram_base
+
2113 (pd
& TARGET_PAGE_MASK
) + (addr
& ~TARGET_PAGE_MASK
);
2116 static void jz4740_lcd_update_display(void *opaque
)
2118 struct jz4740_lcdc_s
*s
= (struct jz4740_lcdc_s
*) opaque
;
2120 uint8_t *src
, *dest
;
2121 struct jz_fb_descriptor
*fb_des
;
2135 fb_des
= (struct jz_fb_descriptor
*) jz4740_lcd_get_buffer(s
, s
->lcdda0
);
2136 s
->lcdda0
= fb_des
->fdadr
;
2137 s
->lcdsa0
= fb_des
->fsadr
;
2138 s
->lcdfid0
= fb_des
->fidr
;
2139 s
->lcdcmd0
= fb_des
->ldcmd
;
2141 src
= (uint8_t *) jz4740_lcd_get_buffer(s
, fb_des
->fsadr
);
2142 if (s
->lcdcmd0
& (0x1 << 28))
2145 memcpy(s
->palette
, src
, sizeof(s
->palette
));
2151 if (s
->width
!= ds_get_width(s
->state
) ||
2152 s
->height
!= ds_get_height(s
->state
))
2154 qemu_console_resize(s
->console
, s
->width
, s
->height
);
2158 step
= (s
->width
* s
->bpp
) >> 3;
2159 dest
= ds_get_data(s
->state
);
2160 linesize
= ds_get_linesize(s
->state
);
2162 //printf("s->width %d s->height %d s->bpp %d linesize %d \n",s->width,s->height ,s->bpp,linesize);
2164 for (y
= 0; y
< s
->height
; y
++)
2166 s
->line_fn(dest
, src
, s
->width
, s
->palette
);
2167 //memcpy(dest,src,step);
2173 dpy_update(s
->state
, 0, 0, s
->width
, s
->height
);
2174 s
->lcdstate
|= 0x20;
2175 if ((s
->lcdcmd0
& 0x40000000) && (!(s
->lcdctrl
& 0x2000)))
2176 qemu_set_irq(s
->irq
, 1);
2179 static inline void jz4740_lcd_invalidate_display(void *opaque
)
2181 struct jz4740_lcdc_s
*s
= (struct jz4740_lcdc_s
*) opaque
;
2185 static struct jz4740_lcdc_s
*jz4740_lcdc_init(struct jz_state_s
*soc
,
2186 qemu_irq irq
, DisplayState
* ds
)
2190 struct jz4740_lcdc_s
*s
= (struct jz4740_lcdc_s
*) qemu_mallocz(sizeof(*s
));
2191 s
->base
= JZ4740_PHYS_BASE(JZ4740_LCD_BASE
);
2197 jz4740_lcdc_reset(s
);
2200 cpu_register_io_memory(0, jz4740_lcdc_readfn
, jz4740_lcdc_writefn
, s
);
2201 cpu_register_physical_memory(s
->base
, 0x10000, iomemtype
);
2203 s
->console
= graphic_console_init(s
->state
, jz4740_lcd_update_display
,
2204 jz4740_lcd_invalidate_display
,
2206 switch (ds_get_bits_per_pixel(s
->state
))
2209 s
->line_fn_tab
= qemu_mallocz(sizeof(jz4740_lcd_fn_t
) * 6);
2212 s
->line_fn_tab
= jz4740_lcd_draw_fn_8
;
2215 s
->line_fn_tab
= jz4740_lcd_draw_fn_15
;
2218 s
->line_fn_tab
= jz4740_lcd_draw_fn_16
;
2221 s
->line_fn_tab
= jz4740_lcd_draw_fn_24
;
2224 s
->line_fn_tab
= jz4740_lcd_draw_fn_32
;
2227 fprintf(stderr
, "%s: Bad color depth\n", __FUNCTION__
);
2235 #define JZ4740_DMA_NUM 6
2240 target_phys_addr_t base
;
2241 struct jz_state_s
*soc
;
2248 uint32_t dsa
[JZ4740_DMA_NUM
];
2249 uint32_t dta
[JZ4740_DMA_NUM
];
2250 uint32_t dtc
[JZ4740_DMA_NUM
];
2251 uint32_t drs
[JZ4740_DMA_NUM
];
2252 uint32_t dcs
[JZ4740_DMA_NUM
];
2253 uint32_t dcm
[JZ4740_DMA_NUM
];
2254 uint32_t dda
[JZ4740_DMA_NUM
];
2258 struct jz4740_desc_s
2260 uint32_t dcmd
; /* DCMD value for the current transfer */
2261 uint32_t dsadr
; /* DSAR value for the current transfer */
2262 uint32_t dtadr
; /* DTAR value for the current transfer */
2263 uint32_t ddadr
; /* Points to the next descriptor + transfer count */
2266 static inline void jz4740_dma_transfer(struct jz4740_dma_s
*s
,
2267 target_phys_addr_t src
,
2268 target_phys_addr_t dest
, uint32_t len
)
2270 uint32_t pd_src
, pd_dest
;
2273 pd_src
= cpu_get_physical_page_desc(src
);
2274 if ((pd_src
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
)
2276 cpu_abort(cpu_single_env
, "%s: DMA source address %x outside RAM!\n",
2279 sr
= phys_ram_base
+
2280 (pd_src
& TARGET_PAGE_MASK
) + (src
& ~TARGET_PAGE_MASK
);
2282 pd_dest
= cpu_get_physical_page_desc(dest
);
2283 if ((pd_dest
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
)
2285 cpu_abort(cpu_single_env
,
2286 "%s: DMA destination address %x outside RAM!\n",
2287 __FUNCTION__
, dest
);
2289 de
= phys_ram_base
+
2290 (pd_dest
& TARGET_PAGE_MASK
) + (dest
& ~TARGET_PAGE_MASK
);
2292 memcpy(de
, sr
, len
);
2295 static inline uint32_t jz4740_dma_unit_size(struct jz4740_dma_s
*s
,
2298 switch ((cmd
& 0x700) >> 8)
2315 /*No-descriptor transfer*/
2316 static inline void jz4740_dma_ndrun(struct jz4740_dma_s
*s
, int channel
)
2320 len
= jz4740_dma_unit_size(s
, s
->dcs
[channel
]) * s
->dtc
[channel
];
2322 jz4740_dma_transfer(s
, s
->dsa
[channel
], s
->dta
[channel
], len
);
2324 /*finish dma transfer */
2325 s
->dtc
[channel
] = 0;
2327 s
->dirqp
|= 1 << channel
;
2329 /*some cleanup work */
2330 /*clean AR TT GLOBAL AR */
2331 s
->dcs
[channel
] &= 0xffffffe7;
2332 s
->dmac
&= 0xfffffffb;
2334 if (s
->dcm
[channel
] & 0x2)
2335 qemu_set_irq(s
->irq
, 1);
2338 /*descriptor transfer */
2339 static inline void jz4740_dma_drun(struct jz4740_dma_s
*s
, int channel
)
2341 struct jz4740_desc_s
*desc
;
2342 target_phys_addr_t desc_phy
;
2345 desc_phy
= s
->dda
[channel
];
2347 cpu_abort(s
->soc
->env
,
2348 "jz4740_dma_drun descriptor address " JZ_FMT_plx
2349 " must be 4 bytes aligned \n", desc_phy
);
2351 pd
= cpu_get_physical_page_desc(desc_phy
);
2352 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
)
2353 cpu_abort(cpu_single_env
,
2354 "%s: DMA descriptor address " JZ_FMT_plx
" outside RAM!\n",
2355 __FUNCTION__
, desc_phy
);
2357 desc
= (struct jz4740_desc_s
*) (phys_ram_base
+
2358 (pd
& TARGET_PAGE_MASK
) +
2359 (desc_phy
& ~TARGET_PAGE_MASK
));
2362 cpu_abort(cpu_single_env
,
2363 "%s: DMA descriptor " JZ_FMT_plx
" is NULL!\n", __FUNCTION__
,
2368 if ((desc
->dcmd
& 0x8) && (!(desc
->dcmd
& 0x10)))
2370 /*Stop DMA and set DCSN.INV=1 */
2371 s
->dcs
[channel
] |= 1 << 6;
2374 jz4740_dma_transfer(s
, desc
->dtadr
, desc
->dsadr
,
2375 (desc
->ddadr
& 0xffffff) *
2376 jz4740_dma_unit_size(s
, desc
->dcmd
));
2378 if ((desc
->dcmd
) & (1 << 3))
2380 desc
->dcmd
&= ~(1 << 4);
2381 if (desc
->dcmd
& 0x1)
2383 s
->dcs
[channel
] |= 0x2;
2386 s
->dcs
[channel
] |= 0x8;
2388 if (desc
->dcmd
& 0x2)
2389 qemu_set_irq(s
->irq
, 1);
2391 if ((desc
->dcmd
) & 0x1)
2393 /*fetch next descriptor */
2394 desc_phy
= s
->dda
[channel
] & 0xfffff000;
2395 desc_phy
+= (desc
->dtadr
& 0xff000000) >> 24;
2396 pd
= cpu_get_physical_page_desc(desc_phy
);
2397 if ((pd
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
)
2398 cpu_abort(cpu_single_env
,
2399 "%s: DMA descriptor address %x outside RAM!\n",
2400 __FUNCTION__
, desc_phy
);
2402 desc
= (struct jz4740_desc_s
*) (phys_ram_base
+
2403 (pd
& TARGET_PAGE_MASK
)
2406 ~TARGET_PAGE_MASK
));
2408 cpu_abort(cpu_single_env
,
2409 "%s: DMA descriptor %x is NULL!\n",
2410 __FUNCTION__
, (uint32_t) desc
);
2417 static void jz4740_dma_en_channel(struct jz4740_dma_s
*s
, int channel
)
2421 if (s
->dcs
[channel
] & (1 << 31))
2424 jz4740_dma_ndrun(s
, channel
);
2429 static inline void jz4740_dma_en_global(struct jz4740_dma_s
*s
)
2432 for (channel
= 0; channel
< JZ4740_DMA_NUM
; channel
++)
2434 jz4740_dma_en_channel(s
, channel
);
2438 static inline void jz4740_dma_en_dbn(struct jz4740_dma_s
*s
, int channel
)
2440 if ((s
->dmac
& 0x1) && (s
->dcs
[channel
] & (1 << 31)))
2442 jz4740_dma_drun(s
, channel
);
2446 static void jz4740_dma_reset(struct jz4740_dma_s
*s
)
2451 static uint32_t jz4740_dma_read(void *opaque
, target_phys_addr_t addr
)
2453 struct jz4740_dma_s
*s
= (struct jz4740_dma_s
*) opaque
;
2456 debug_out(DEBUG_DMA
, "jz4740_dma_read addr %x \n", addr
);
2471 channel
= (addr
- 0x0) / 0x20;
2472 return s
->dsa
[channel
];
2479 channel
= (addr
- 0x4) / 0x20;
2480 return s
->dta
[channel
];
2487 channel
= (addr
- 0x8) / 0x20;
2488 return s
->dtc
[channel
];
2495 channel
= (addr
- 0xc) / 0x20;
2496 return s
->drs
[channel
];
2503 channel
= (addr
- 0x10) / 0x20;
2504 return s
->dcs
[channel
];
2511 channel
= (addr
- 0x14) / 0x20;
2512 return s
->dcm
[channel
];
2519 channel
= (addr
- 0x18) / 0x20;
2520 return s
->dda
[channel
];
2522 cpu_abort(s
->soc
->env
,
2523 "jz4740_dma_read undefined addr " JZ_FMT_plx
" \n", addr
);
2528 static void jz4740_dma_write(void *opaque
, target_phys_addr_t addr
,
2531 struct jz4740_dma_s
*s
= (struct jz4740_dma_s
*) opaque
;
2534 debug_out(DEBUG_DMA
, "jz4740_dma_write addr %x value %x \n", addr
, value
);
2538 JZ4740_RO_REG(addr
);
2541 s
->dmac
= value
& 0x30d;
2543 jz4740_dma_en_global(s
);
2547 s
->ddr
= value
& 0xff;
2548 for (channel
= 0; channel
< JZ4740_DMA_NUM
; channel
++)
2550 if (s
->ddr
& (1 << channel
))
2552 jz4740_dma_en_dbn(s
, channel
);
2563 channel
= (addr
- 0x0) / 0x20;
2564 s
->dsa
[channel
] = value
;
2572 channel
= (addr
- 0x4) / 0x20;
2573 s
->dta
[channel
] = value
;
2581 channel
= (addr
- 0x8) / 0x20;
2582 s
->dtc
[channel
] = value
;
2590 channel
= (addr
- 0xc) / 0x20;
2591 s
->drs
[channel
] = value
& 0x10;
2592 if (s
->drs
[channel
] != 0x8)
2594 fprintf(stderr
, "Only auto request is supproted \n");
2603 channel
= (addr
- 0x10) / 0x20;
2604 s
->dcs
[channel
] = value
& 0x80ff005f;
2605 if (s
->dcs
[channel
] & 0x1)
2606 jz4740_dma_en_channel(s
, channel
);
2614 channel
= (addr
- 0x14) / 0x20;
2615 s
->dcm
[channel
] = value
& 0xcff79f;
2623 channel
= (addr
- 0x18) / 0x20;
2624 s
->dda
[channel
] = 0xfffffff0;
2627 cpu_abort(s
->soc
->env
,
2628 "jz4740_dma_read undefined addr " JZ_FMT_plx
" \n", addr
);
2633 static CPUReadMemoryFunc
*jz4740_dma_readfn
[] = {
2634 jz4740_badwidth_read32
,
2635 jz4740_badwidth_read32
,
2639 static CPUWriteMemoryFunc
*jz4740_dma_writefn
[] = {
2640 jz4740_badwidth_write32
,
2641 jz4740_badwidth_write32
,
2646 static struct jz4740_dma_s
*jz4740_dma_init(struct jz_state_s
*soc
,
2650 struct jz4740_dma_s
*s
= (struct jz4740_dma_s
*) qemu_mallocz(sizeof(*s
));
2651 s
->base
= JZ4740_PHYS_BASE(JZ4740_DMAC_BASE
);
2655 jz4740_dma_reset(s
);
2658 cpu_register_io_memory(0, jz4740_dma_readfn
, jz4740_dma_writefn
, s
);
2659 cpu_register_physical_memory(s
->base
, 0x00010000, iomemtype
);
2664 static void jz4740_cpu_reset(void *opaque
)
2666 fprintf(stderr
, "%s: UNIMPLEMENTED!", __FUNCTION__
);
2669 struct jz_state_s
*jz4740_init(unsigned long sdram_size
,
2670 uint32_t osc_extal_freq
, DisplayState
* ds
)
2672 struct jz_state_s
*s
= (struct jz_state_s
*)
2673 qemu_mallocz(sizeof(struct jz_state_s
));
2674 ram_addr_t sram_base
, sdram_base
;
2677 s
->mpu_model
= jz4740
;
2678 s
->env
= cpu_init("jz4740");
2682 fprintf(stderr
, "Unable to find CPU definition\n");
2687 qemu_register_reset(jz4740_cpu_reset
, s
->env
);
2689 s
->sdram_size
= sdram_size
;
2690 s
->sram_size
= JZ4740_SRAM_SIZE
;
2693 jz_clk_init(s
, osc_extal_freq
);
2695 /*map sram to 0x80000000 and sdram to 0x80004000 */
2696 sram_base
= qemu_ram_alloc(s
->sram_size
);
2697 cpu_register_physical_memory(0x0, s
->sram_size
, (sram_base
| IO_MEM_RAM
));
2698 sdram_base
= qemu_ram_alloc(s
->sdram_size
);
2699 cpu_register_physical_memory(JZ4740_SRAM_SIZE
, s
->sdram_size
,
2700 (sdram_base
| IO_MEM_RAM
));
2702 /* Init internal devices */
2703 cpu_mips_irq_init_cpu(s
->env
);
2704 cpu_mips_clock_init(s
->env
);
2708 jz_clk_init(s
, osc_extal_freq
);
2710 intc
= jz4740_intc_init(s
, s
->env
->irq
[2]);
2711 s
->cpm
= jz4740_cpm_init(s
);
2712 s
->emc
= jz4740_emc_init(s
, intc
[2]);
2713 s
->gpio
= jz4740_gpio_init(s
, intc
[25]);
2714 s
->rtc
= jz4740_rtc_init(s
, intc
[15]);
2715 s
->tcu
= jz4740_tcu_if_init(s
, intc
[23], intc
[22], intc
[21]);
2716 jz4740_tcu_init(s
, s
->tcu
, 0);
2717 s
->lcdc
= jz4740_lcdc_init(s
, intc
[30], ds
);
2718 s
->dma
= jz4740_dma_init(s
, intc
[20]);
2721 serial_mm_init(0x10030000, 2, intc
[9], 57600, serial_hds
[0], 1);