2 * Toshiba TC6393XB I/O Controller.
3 * Found in Sharp Zaurus SL-6000 (tosa) or some
4 * Toshiba e-Series PDAs.
6 * Most features are currently unsupported!!!
8 * This code is licensed under the GNU GPL v2.
15 #define IRQ_TC6393_NAND 0
16 #define IRQ_TC6393_MMC 1
17 #define IRQ_TC6393_OHCI 2
18 #define IRQ_TC6393_SERIAL 3
19 #define IRQ_TC6393_FB 4
21 #define TC6393XB_NR_IRQS 8
23 #define TC6393XB_GPIOS 16
25 #define SCR_REVID 0x08 /* b Revision ID */
26 #define SCR_ISR 0x50 /* b Interrupt Status */
27 #define SCR_IMR 0x52 /* b Interrupt Mask */
28 #define SCR_IRR 0x54 /* b Interrupt Routing */
29 #define SCR_GPER 0x60 /* w GP Enable */
30 #define SCR_GPI_SR(i) (0x64 + (i)) /* b3 GPI Status */
31 #define SCR_GPI_IMR(i) (0x68 + (i)) /* b3 GPI INT Mask */
32 #define SCR_GPI_EDER(i) (0x6c + (i)) /* b3 GPI Edge Detect Enable */
33 #define SCR_GPI_LIR(i) (0x70 + (i)) /* b3 GPI Level Invert */
34 #define SCR_GPO_DSR(i) (0x78 + (i)) /* b3 GPO Data Set */
35 #define SCR_GPO_DOECR(i) (0x7c + (i)) /* b3 GPO Data OE Control */
36 #define SCR_GP_IARCR(i) (0x80 + (i)) /* b3 GP Internal Active Register Control */
37 #define SCR_GP_IARLCR(i) (0x84 + (i)) /* b3 GP INTERNAL Active Register Level Control */
38 #define SCR_GPI_BCR(i) (0x88 + (i)) /* b3 GPI Buffer Control */
39 #define SCR_GPA_IARCR 0x8c /* w GPa Internal Active Register Control */
40 #define SCR_GPA_IARLCR 0x90 /* w GPa Internal Active Register Level Control */
41 #define SCR_GPA_BCR 0x94 /* w GPa Buffer Control */
42 #define SCR_CCR 0x98 /* w Clock Control */
43 #define SCR_PLL2CR 0x9a /* w PLL2 Control */
44 #define SCR_PLL1CR 0x9c /* l PLL1 Control */
45 #define SCR_DIARCR 0xa0 /* b Device Internal Active Register Control */
46 #define SCR_DBOCR 0xa1 /* b Device Buffer Off Control */
47 #define SCR_FER 0xe0 /* b Function Enable */
48 #define SCR_MCR 0xe4 /* w Mode Control */
49 #define SCR_CONFIG 0xfc /* b Configuration Control */
50 #define SCR_DEBUG 0xff /* b Debug */
52 #define NAND_CFG_COMMAND 0x04 /* w Command */
53 #define NAND_CFG_BASE 0x10 /* l Control Base Address */
54 #define NAND_CFG_INTP 0x3d /* b Interrupt Pin */
55 #define NAND_CFG_INTE 0x48 /* b Int Enable */
56 #define NAND_CFG_EC 0x4a /* b Event Control */
57 #define NAND_CFG_ICC 0x4c /* b Internal Clock Control */
58 #define NAND_CFG_ECCC 0x5b /* b ECC Control */
59 #define NAND_CFG_NFTC 0x60 /* b NAND Flash Transaction Control */
60 #define NAND_CFG_NFM 0x61 /* b NAND Flash Monitor */
61 #define NAND_CFG_NFPSC 0x62 /* b NAND Flash Power Supply Control */
62 #define NAND_CFG_NFDC 0x63 /* b NAND Flash Detect Control */
64 #define NAND_DATA 0x00 /* l Data */
65 #define NAND_MODE 0x04 /* b Mode */
66 #define NAND_STATUS 0x05 /* b Status */
67 #define NAND_ISR 0x06 /* b Interrupt Status */
68 #define NAND_IMR 0x07 /* b Interrupt Mask */
70 #define NAND_MODE_WP 0x80
71 #define NAND_MODE_CE 0x10
72 #define NAND_MODE_ALE 0x02
73 #define NAND_MODE_CLE 0x01
74 #define NAND_MODE_ECC_MASK 0x60
75 #define NAND_MODE_ECC_EN 0x20
76 #define NAND_MODE_ECC_READ 0x40
77 #define NAND_MODE_ECC_RST 0x60
80 target_phys_addr_t target_base
;
110 qemu_irq handler
[TC6393XB_GPIOS
];
120 struct nand_flash_s
*flash
;
121 struct ecc_state_s ecc
;
124 qemu_irq
*tc6393xb_gpio_in_get(struct tc6393xb_s
*s
)
129 static void tc6393xb_gpio_set(void *opaque
, int line
, int level
)
131 // struct tc6393xb_s *s = opaque;
133 if (line
> TC6393XB_GPIOS
) {
134 printf("%s: No GPIO pin %i\n", __FUNCTION__
, line
);
138 // FIXME: how does the chip reflect the GPIO input level change?
141 void tc6393xb_gpio_out_set(struct tc6393xb_s
*s
, int line
,
144 if (line
>= TC6393XB_GPIOS
) {
145 fprintf(stderr
, "TC6393xb: no GPIO pin %d\n", line
);
149 s
->handler
[line
] = handler
;
152 static void tc6393xb_gpio_handler_update(struct tc6393xb_s
*s
)
154 uint32_t level
, diff
;
157 level
= s
->gpio_level
& s
->gpio_dir
;
159 for (diff
= s
->prev_level
^ level
; diff
; diff
^= 1 << bit
) {
161 qemu_set_irq(s
->handler
[bit
], (level
>> bit
) & 1);
164 s
->prev_level
= level
;
167 static void tc6393xb_sub_irq(void *opaque
, int line
, int level
) {
168 struct tc6393xb_s
*s
= opaque
;
169 uint8_t isr
= s
->scr
.ISR
;
175 qemu_set_irq(s
->irq
, isr
& s
->scr
.IMR
);
178 #define SCR_REG_B(N) \
179 case SCR_ ##N: return s->scr.N
180 #define SCR_REG_W(N) \
181 case SCR_ ##N: return s->scr.N; \
182 case SCR_ ##N + 1: return s->scr.N >> 8;
183 #define SCR_REG_L(N) \
184 case SCR_ ##N: return s->scr.N; \
185 case SCR_ ##N + 1: return s->scr.N >> 8; \
186 case SCR_ ##N + 2: return s->scr.N >> 16; \
187 case SCR_ ##N + 3: return s->scr.N >> 24;
188 #define SCR_REG_A(N) \
189 case SCR_ ##N(0): return s->scr.N[0]; \
190 case SCR_ ##N(1): return s->scr.N[1]; \
191 case SCR_ ##N(2): return s->scr.N[2]
193 static uint32_t tc6393xb_scr_readb(struct tc6393xb_s
*s
, target_phys_addr_t addr
)
211 return (s
->gpio_level
>> ((addr
- SCR_GPO_DSR(0)) * 8)) & 0xff;
212 case SCR_GPO_DOECR(0):
213 case SCR_GPO_DOECR(1):
214 case SCR_GPO_DOECR(2):
215 return (s
->gpio_dir
>> ((addr
- SCR_GPO_DOECR(0)) * 8)) & 0xff;
217 SCR_REG_A(GP_IARLCR
);
219 SCR_REG_W(GPA_IARCR
);
220 SCR_REG_W(GPA_IARLCR
);
231 fprintf(stderr
, "tc6393xb_scr: unhandled read at %08x\n", (uint32_t) addr
);
239 #define SCR_REG_B(N) \
240 case SCR_ ##N: s->scr.N = value; return;
241 #define SCR_REG_W(N) \
242 case SCR_ ##N: s->scr.N = (s->scr.N & ~0xff) | (value & 0xff); return; \
243 case SCR_ ##N + 1: s->scr.N = (s->scr.N & 0xff) | (value << 8); return
244 #define SCR_REG_L(N) \
245 case SCR_ ##N: s->scr.N = (s->scr.N & ~0xff) | (value & 0xff); return; \
246 case SCR_ ##N + 1: s->scr.N = (s->scr.N & ~(0xff << 8)) | (value & (0xff << 8)); return; \
247 case SCR_ ##N + 2: s->scr.N = (s->scr.N & ~(0xff << 16)) | (value & (0xff << 16)); return; \
248 case SCR_ ##N + 3: s->scr.N = (s->scr.N & ~(0xff << 24)) | (value & (0xff << 24)); return;
249 #define SCR_REG_A(N) \
250 case SCR_ ##N(0): s->scr.N[0] = value; return; \
251 case SCR_ ##N(1): s->scr.N[1] = value; return; \
252 case SCR_ ##N(2): s->scr.N[2] = value; return
254 static void tc6393xb_scr_writeb(struct tc6393xb_s
*s
, target_phys_addr_t addr
, uint32_t value
)
268 s
->gpio_level
= (s
->gpio_level
& ~(0xff << ((addr
- SCR_GPO_DSR(0))*8))) | ((value
& 0xff) << ((addr
- SCR_GPO_DSR(0))*8));
269 tc6393xb_gpio_handler_update(s
);
271 case SCR_GPO_DOECR(0):
272 case SCR_GPO_DOECR(1):
273 case SCR_GPO_DOECR(2):
274 s
->gpio_dir
= (s
->gpio_dir
& ~(0xff << ((addr
- SCR_GPO_DOECR(0))*8))) | ((value
& 0xff) << ((addr
- SCR_GPO_DOECR(0))*8));
275 tc6393xb_gpio_handler_update(s
);
278 SCR_REG_A(GP_IARLCR
);
280 SCR_REG_W(GPA_IARCR
);
281 SCR_REG_W(GPA_IARLCR
);
292 fprintf(stderr
, "tc6393xb_scr: unhandled write at %08x: %02x\n",
293 (uint32_t) addr
, value
& 0xff);
300 static void tc6393xb_nand_irq(struct tc6393xb_s
*s
) {
301 qemu_set_irq(s
->sub_irqs
[IRQ_TC6393_NAND
],
302 (s
->nand
.imr
& 0x80) && (s
->nand
.imr
& s
->nand
.isr
));
305 static uint32_t tc6393xb_nand_cfg_readb(struct tc6393xb_s
*s
, target_phys_addr_t addr
) {
307 case NAND_CFG_COMMAND
:
308 return s
->nand_enable
? 2 : 0;
310 case NAND_CFG_BASE
+ 1:
311 case NAND_CFG_BASE
+ 2:
312 case NAND_CFG_BASE
+ 3:
313 return s
->nand_phys
>> (addr
- NAND_CFG_BASE
);
315 fprintf(stderr
, "tc6393xb_nand_cfg: unhandled read at %08x\n", (uint32_t) addr
);
318 static void tc6393xb_nand_cfg_writeb(struct tc6393xb_s
*s
, target_phys_addr_t addr
, uint32_t value
) {
320 case NAND_CFG_COMMAND
:
321 s
->nand_enable
= (value
& 0x2);
324 case NAND_CFG_BASE
+ 1:
325 case NAND_CFG_BASE
+ 2:
326 case NAND_CFG_BASE
+ 3:
327 s
->nand_phys
&= ~(0xff << ((addr
- NAND_CFG_BASE
) * 8));
328 s
->nand_phys
|= (value
& 0xff) << ((addr
- NAND_CFG_BASE
) * 8);
331 fprintf(stderr
, "tc6393xb_nand_cfg: unhandled write at %08x: %02x\n",
332 (uint32_t) addr
, value
& 0xff);
335 static uint32_t tc6393xb_nand_readb(struct tc6393xb_s
*s
, target_phys_addr_t addr
) {
341 return nand_getio(s
->flash
);
351 fprintf(stderr
, "tc6393xb_nand: unhandled read at %08x\n", (uint32_t) addr
);
354 static void tc6393xb_nand_writeb(struct tc6393xb_s
*s
, target_phys_addr_t addr
, uint32_t value
) {
355 // fprintf(stderr, "tc6393xb_nand: write at %08x: %02x\n",
356 // (uint32_t) addr, value & 0xff);
362 nand_setio(s
->flash
, value
);
364 tc6393xb_nand_irq(s
);
367 s
->nand
.mode
= value
;
368 nand_setpins(s
->flash
,
369 value
& NAND_MODE_CLE
,
370 value
& NAND_MODE_ALE
,
371 !(value
& NAND_MODE_CE
),
372 value
& NAND_MODE_WP
,
374 switch (value
& NAND_MODE_ECC_MASK
) {
375 case NAND_MODE_ECC_RST
:
378 case NAND_MODE_ECC_READ
:
381 case NAND_MODE_ECC_EN
:
387 tc6393xb_nand_irq(s
);
391 tc6393xb_nand_irq(s
);
394 fprintf(stderr
, "tc6393xb_nand: unhandled write at %08x: %02x\n",
395 (uint32_t) addr
, value
& 0xff);
398 static uint32_t tc6393xb_readb(void *opaque
, target_phys_addr_t addr
) {
399 struct tc6393xb_s
*s
= opaque
;
400 addr
-= s
->target_base
;
404 return tc6393xb_scr_readb(s
, addr
& 0xff);
406 return tc6393xb_nand_cfg_readb(s
, addr
& 0xff);
409 if ((addr
&~0xff) == s
->nand_phys
&& s
->nand_enable
) {
410 // return tc6393xb_nand_readb(s, addr & 0xff);
411 uint8_t d
= tc6393xb_nand_readb(s
, addr
& 0xff);
412 // fprintf(stderr, "tc6393xb_nand: read at %08x: %02hhx\n", (uint32_t) addr, d);
416 // fprintf(stderr, "tc6393xb: unhandled read at %08x\n", (uint32_t) addr);
420 static void tc6393xb_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t value
) {
421 struct tc6393xb_s
*s
= opaque
;
422 addr
-= s
->target_base
;
426 tc6393xb_scr_writeb(s
, addr
& 0xff, value
);
429 tc6393xb_nand_cfg_writeb(s
, addr
& 0xff, value
);
433 if ((addr
&~0xff) == s
->nand_phys
&& s
->nand_enable
)
434 tc6393xb_nand_writeb(s
, addr
& 0xff, value
);
436 fprintf(stderr
, "tc6393xb: unhandled write at %08x: %02x\n",
437 (uint32_t) addr
, value
& 0xff);
440 static uint32_t tc6393xb_readw(void *opaque
, target_phys_addr_t addr
)
442 return (tc6393xb_readb(opaque
, addr
) & 0xff) |
443 (tc6393xb_readb(opaque
, addr
+ 1) << 8);
446 static uint32_t tc6393xb_readl(void *opaque
, target_phys_addr_t addr
)
448 return (tc6393xb_readb(opaque
, addr
) & 0xff) |
449 ((tc6393xb_readb(opaque
, addr
+ 1) & 0xff) << 8) |
450 ((tc6393xb_readb(opaque
, addr
+ 2) & 0xff) << 16) |
451 ((tc6393xb_readb(opaque
, addr
+ 3) & 0xff) << 24);
454 static void tc6393xb_writew(void *opaque
, target_phys_addr_t addr
, uint32_t value
)
456 tc6393xb_writeb(opaque
, addr
, value
);
457 tc6393xb_writeb(opaque
, addr
+ 1, value
>> 8);
460 static void tc6393xb_writel(void *opaque
, target_phys_addr_t addr
, uint32_t value
)
462 tc6393xb_writeb(opaque
, addr
, value
);
463 tc6393xb_writeb(opaque
, addr
+ 1, value
>> 8);
464 tc6393xb_writeb(opaque
, addr
+ 2, value
>> 16);
465 tc6393xb_writeb(opaque
, addr
+ 3, value
>> 24);
468 struct tc6393xb_s
*tc6393xb_init(uint32_t base
, qemu_irq irq
)
471 struct tc6393xb_s
*s
;
472 CPUReadMemoryFunc
*tc6393xb_readfn
[] = {
477 CPUWriteMemoryFunc
*tc6393xb_writefn
[] = {
483 s
= (struct tc6393xb_s
*) qemu_mallocz(sizeof(struct tc6393xb_s
));
484 s
->target_base
= base
;
486 s
->gpio_in
= qemu_allocate_irqs(tc6393xb_gpio_set
, s
, TC6393XB_GPIOS
);
488 s
->sub_irqs
= qemu_allocate_irqs(tc6393xb_sub_irq
, s
, TC6393XB_NR_IRQS
);
490 s
->flash
= nand_init(NAND_MFR_TOSHIBA
, 0x76);
492 iomemtype
= cpu_register_io_memory(0, tc6393xb_readfn
,
493 tc6393xb_writefn
, s
);
494 cpu_register_physical_memory(s
->target_base
, 0x200000, iomemtype
);