12f062c72Sths /* 22f062c72Sths * QEMU SCI/SCIF serial port emulation 32f062c72Sths * 42f062c72Sths * Copyright (c) 2007 Magnus Damm 52f062c72Sths * 62f062c72Sths * Based on serial.c - QEMU 16450 UART emulation 72f062c72Sths * Copyright (c) 2003-2004 Fabrice Bellard 82f062c72Sths * 92f062c72Sths * Permission is hereby granted, free of charge, to any person obtaining a copy 102f062c72Sths * of this software and associated documentation files (the "Software"), to deal 112f062c72Sths * in the Software without restriction, including without limitation the rights 122f062c72Sths * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 132f062c72Sths * copies of the Software, and to permit persons to whom the Software is 142f062c72Sths * furnished to do so, subject to the following conditions: 152f062c72Sths * 162f062c72Sths * The above copyright notice and this permission notice shall be included in 172f062c72Sths * all copies or substantial portions of the Software. 182f062c72Sths * 192f062c72Sths * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 202f062c72Sths * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 212f062c72Sths * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 222f062c72Sths * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 232f062c72Sths * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 242f062c72Sths * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 252f062c72Sths * THE SOFTWARE. 262f062c72Sths */ 27*0430891cSPeter Maydell #include "qemu/osdep.h" 2883c9f4caSPaolo Bonzini #include "hw/hw.h" 290d09e41aSPaolo Bonzini #include "hw/sh4/sh.h" 30dccfcd0eSPaolo Bonzini #include "sysemu/char.h" 31022c62cbSPaolo Bonzini #include "exec/address-spaces.h" 322f062c72Sths 332f062c72Sths //#define DEBUG_SERIAL 342f062c72Sths 352f062c72Sths #define SH_SERIAL_FLAG_TEND (1 << 0) 362f062c72Sths #define SH_SERIAL_FLAG_TDE (1 << 1) 372f062c72Sths #define SH_SERIAL_FLAG_RDF (1 << 2) 382f062c72Sths #define SH_SERIAL_FLAG_BRK (1 << 3) 392f062c72Sths #define SH_SERIAL_FLAG_DR (1 << 4) 402f062c72Sths 4163242a00Saurel32 #define SH_RX_FIFO_LENGTH (16) 4263242a00Saurel32 432f062c72Sths typedef struct { 449a9d0b81SBenoît Canet MemoryRegion iomem; 459a9d0b81SBenoît Canet MemoryRegion iomem_p4; 469a9d0b81SBenoît Canet MemoryRegion iomem_a7; 472f062c72Sths uint8_t smr; 482f062c72Sths uint8_t brr; 492f062c72Sths uint8_t scr; 502f062c72Sths uint8_t dr; /* ftdr / tdr */ 512f062c72Sths uint8_t sr; /* fsr / ssr */ 522f062c72Sths uint16_t fcr; 532f062c72Sths uint8_t sptr; 542f062c72Sths 5563242a00Saurel32 uint8_t rx_fifo[SH_RX_FIFO_LENGTH]; /* frdr / rdr */ 562f062c72Sths uint8_t rx_cnt; 5763242a00Saurel32 uint8_t rx_tail; 5863242a00Saurel32 uint8_t rx_head; 592f062c72Sths 602f062c72Sths int freq; 612f062c72Sths int feat; 622f062c72Sths int flags; 6363242a00Saurel32 int rtrg; 642f062c72Sths 652f062c72Sths CharDriverState *chr; 66bf5b7423Saurel32 674e7ed2d1Saurel32 qemu_irq eri; 684e7ed2d1Saurel32 qemu_irq rxi; 694e7ed2d1Saurel32 qemu_irq txi; 704e7ed2d1Saurel32 qemu_irq tei; 714e7ed2d1Saurel32 qemu_irq bri; 722f062c72Sths } sh_serial_state; 732f062c72Sths 7463242a00Saurel32 static void sh_serial_clear_fifo(sh_serial_state * s) 7563242a00Saurel32 { 7663242a00Saurel32 memset(s->rx_fifo, 0, SH_RX_FIFO_LENGTH); 7763242a00Saurel32 s->rx_cnt = 0; 7863242a00Saurel32 s->rx_head = 0; 7963242a00Saurel32 s->rx_tail = 0; 8063242a00Saurel32 } 8163242a00Saurel32 82a8170e5eSAvi Kivity static void sh_serial_write(void *opaque, hwaddr offs, 839a9d0b81SBenoît Canet uint64_t val, unsigned size) 842f062c72Sths { 852f062c72Sths sh_serial_state *s = opaque; 862f062c72Sths unsigned char ch; 872f062c72Sths 882f062c72Sths #ifdef DEBUG_SERIAL 898da3ff18Spbrook printf("sh_serial: write offs=0x%02x val=0x%02x\n", 908da3ff18Spbrook offs, val); 912f062c72Sths #endif 922f062c72Sths switch(offs) { 932f062c72Sths case 0x00: /* SMR */ 942f062c72Sths s->smr = val & ((s->feat & SH_SERIAL_FEAT_SCIF) ? 0x7b : 0xff); 952f062c72Sths return; 962f062c72Sths case 0x04: /* BRR */ 972f062c72Sths s->brr = val; 982f062c72Sths return; 992f062c72Sths case 0x08: /* SCR */ 10063242a00Saurel32 /* TODO : For SH7751, SCIF mask should be 0xfb. */ 101bf5b7423Saurel32 s->scr = val & ((s->feat & SH_SERIAL_FEAT_SCIF) ? 0xfa : 0xff); 1022f062c72Sths if (!(val & (1 << 5))) 1032f062c72Sths s->flags |= SH_SERIAL_FLAG_TEND; 104bf5b7423Saurel32 if ((s->feat & SH_SERIAL_FEAT_SCIF) && s->txi) { 1054e7ed2d1Saurel32 qemu_set_irq(s->txi, val & (1 << 7)); 106bf5b7423Saurel32 } 1074e7ed2d1Saurel32 if (!(val & (1 << 6))) { 1084e7ed2d1Saurel32 qemu_set_irq(s->rxi, 0); 10963242a00Saurel32 } 1102f062c72Sths return; 1112f062c72Sths case 0x0c: /* FTDR / TDR */ 1122f062c72Sths if (s->chr) { 1132f062c72Sths ch = val; 1142cc6e0a1SAnthony Liguori qemu_chr_fe_write(s->chr, &ch, 1); 1152f062c72Sths } 1162f062c72Sths s->dr = val; 1172f062c72Sths s->flags &= ~SH_SERIAL_FLAG_TDE; 1182f062c72Sths return; 1192f062c72Sths #if 0 1202f062c72Sths case 0x14: /* FRDR / RDR */ 1212f062c72Sths ret = 0; 1222f062c72Sths break; 1232f062c72Sths #endif 1242f062c72Sths } 1252f062c72Sths if (s->feat & SH_SERIAL_FEAT_SCIF) { 1262f062c72Sths switch(offs) { 1272f062c72Sths case 0x10: /* FSR */ 1282f062c72Sths if (!(val & (1 << 6))) 1292f062c72Sths s->flags &= ~SH_SERIAL_FLAG_TEND; 1302f062c72Sths if (!(val & (1 << 5))) 1312f062c72Sths s->flags &= ~SH_SERIAL_FLAG_TDE; 1322f062c72Sths if (!(val & (1 << 4))) 1332f062c72Sths s->flags &= ~SH_SERIAL_FLAG_BRK; 1342f062c72Sths if (!(val & (1 << 1))) 1352f062c72Sths s->flags &= ~SH_SERIAL_FLAG_RDF; 1362f062c72Sths if (!(val & (1 << 0))) 1372f062c72Sths s->flags &= ~SH_SERIAL_FLAG_DR; 13863242a00Saurel32 13963242a00Saurel32 if (!(val & (1 << 1)) || !(val & (1 << 0))) { 1404e7ed2d1Saurel32 if (s->rxi) { 1414e7ed2d1Saurel32 qemu_set_irq(s->rxi, 0); 14263242a00Saurel32 } 14363242a00Saurel32 } 1442f062c72Sths return; 1452f062c72Sths case 0x18: /* FCR */ 1462f062c72Sths s->fcr = val; 14763242a00Saurel32 switch ((val >> 6) & 3) { 14863242a00Saurel32 case 0: 14963242a00Saurel32 s->rtrg = 1; 15063242a00Saurel32 break; 15163242a00Saurel32 case 1: 15263242a00Saurel32 s->rtrg = 4; 15363242a00Saurel32 break; 15463242a00Saurel32 case 2: 15563242a00Saurel32 s->rtrg = 8; 15663242a00Saurel32 break; 15763242a00Saurel32 case 3: 15863242a00Saurel32 s->rtrg = 14; 15963242a00Saurel32 break; 16063242a00Saurel32 } 16163242a00Saurel32 if (val & (1 << 1)) { 16263242a00Saurel32 sh_serial_clear_fifo(s); 16363242a00Saurel32 s->sr &= ~(1 << 1); 16463242a00Saurel32 } 16563242a00Saurel32 1662f062c72Sths return; 1672f062c72Sths case 0x20: /* SPTR */ 16863242a00Saurel32 s->sptr = val & 0xf3; 1692f062c72Sths return; 1702f062c72Sths case 0x24: /* LSR */ 1712f062c72Sths return; 1722f062c72Sths } 1732f062c72Sths } 1742f062c72Sths else { 1752f062c72Sths switch(offs) { 176d1f193b0Saurel32 #if 0 1772f062c72Sths case 0x0c: 1782f062c72Sths ret = s->dr; 1792f062c72Sths break; 1802f062c72Sths case 0x10: 1812f062c72Sths ret = 0; 1822f062c72Sths break; 1832f062c72Sths #endif 184d1f193b0Saurel32 case 0x1c: 185d1f193b0Saurel32 s->sptr = val & 0x8f; 186d1f193b0Saurel32 return; 187d1f193b0Saurel32 } 1882f062c72Sths } 1892f062c72Sths 190c1950a4eSPeter Maydell fprintf(stderr, "sh_serial: unsupported write to 0x%02" 191a8170e5eSAvi Kivity HWADDR_PRIx "\n", offs); 19243dc2a64SBlue Swirl abort(); 1932f062c72Sths } 1942f062c72Sths 195a8170e5eSAvi Kivity static uint64_t sh_serial_read(void *opaque, hwaddr offs, 1969a9d0b81SBenoît Canet unsigned size) 1972f062c72Sths { 1982f062c72Sths sh_serial_state *s = opaque; 1992f062c72Sths uint32_t ret = ~0; 2002f062c72Sths 2012f062c72Sths #if 0 2022f062c72Sths switch(offs) { 2032f062c72Sths case 0x00: 2042f062c72Sths ret = s->smr; 2052f062c72Sths break; 2062f062c72Sths case 0x04: 2072f062c72Sths ret = s->brr; 2082f062c72Sths break; 2092f062c72Sths case 0x08: 2102f062c72Sths ret = s->scr; 2112f062c72Sths break; 2122f062c72Sths case 0x14: 2132f062c72Sths ret = 0; 2142f062c72Sths break; 2152f062c72Sths } 2162f062c72Sths #endif 2172f062c72Sths if (s->feat & SH_SERIAL_FEAT_SCIF) { 2182f062c72Sths switch(offs) { 219bf5b7423Saurel32 case 0x00: /* SMR */ 220bf5b7423Saurel32 ret = s->smr; 221bf5b7423Saurel32 break; 222bf5b7423Saurel32 case 0x08: /* SCR */ 223bf5b7423Saurel32 ret = s->scr; 224bf5b7423Saurel32 break; 2252f062c72Sths case 0x10: /* FSR */ 2262f062c72Sths ret = 0; 2272f062c72Sths if (s->flags & SH_SERIAL_FLAG_TEND) 2282f062c72Sths ret |= (1 << 6); 2292f062c72Sths if (s->flags & SH_SERIAL_FLAG_TDE) 2302f062c72Sths ret |= (1 << 5); 2312f062c72Sths if (s->flags & SH_SERIAL_FLAG_BRK) 2322f062c72Sths ret |= (1 << 4); 2332f062c72Sths if (s->flags & SH_SERIAL_FLAG_RDF) 2342f062c72Sths ret |= (1 << 1); 2352f062c72Sths if (s->flags & SH_SERIAL_FLAG_DR) 2362f062c72Sths ret |= (1 << 0); 2372f062c72Sths 2382f062c72Sths if (s->scr & (1 << 5)) 2392f062c72Sths s->flags |= SH_SERIAL_FLAG_TDE | SH_SERIAL_FLAG_TEND; 2402f062c72Sths 2412f062c72Sths break; 24263242a00Saurel32 case 0x14: 24363242a00Saurel32 if (s->rx_cnt > 0) { 24463242a00Saurel32 ret = s->rx_fifo[s->rx_tail++]; 24563242a00Saurel32 s->rx_cnt--; 24663242a00Saurel32 if (s->rx_tail == SH_RX_FIFO_LENGTH) 24763242a00Saurel32 s->rx_tail = 0; 24863242a00Saurel32 if (s->rx_cnt < s->rtrg) 24963242a00Saurel32 s->flags &= ~SH_SERIAL_FLAG_RDF; 25063242a00Saurel32 } 25163242a00Saurel32 break; 2522f062c72Sths case 0x18: 2532f062c72Sths ret = s->fcr; 2542f062c72Sths break; 2552f062c72Sths case 0x1c: 2562f062c72Sths ret = s->rx_cnt; 2572f062c72Sths break; 2582f062c72Sths case 0x20: 2592f062c72Sths ret = s->sptr; 2602f062c72Sths break; 2612f062c72Sths case 0x24: 2622f062c72Sths ret = 0; 2632f062c72Sths break; 2642f062c72Sths } 2652f062c72Sths } 2662f062c72Sths else { 2672f062c72Sths switch(offs) { 268d1f193b0Saurel32 #if 0 2692f062c72Sths case 0x0c: 2702f062c72Sths ret = s->dr; 2712f062c72Sths break; 2722f062c72Sths case 0x10: 2732f062c72Sths ret = 0; 2742f062c72Sths break; 27563242a00Saurel32 case 0x14: 27663242a00Saurel32 ret = s->rx_fifo[0]; 27763242a00Saurel32 break; 278d1f193b0Saurel32 #endif 2792f062c72Sths case 0x1c: 2802f062c72Sths ret = s->sptr; 2812f062c72Sths break; 2822f062c72Sths } 2832f062c72Sths } 2842f062c72Sths #ifdef DEBUG_SERIAL 2858da3ff18Spbrook printf("sh_serial: read offs=0x%02x val=0x%x\n", 2868da3ff18Spbrook offs, ret); 2872f062c72Sths #endif 2882f062c72Sths 2892f062c72Sths if (ret & ~((1 << 16) - 1)) { 290c1950a4eSPeter Maydell fprintf(stderr, "sh_serial: unsupported read from 0x%02" 291a8170e5eSAvi Kivity HWADDR_PRIx "\n", offs); 29243dc2a64SBlue Swirl abort(); 2932f062c72Sths } 2942f062c72Sths 2952f062c72Sths return ret; 2962f062c72Sths } 2972f062c72Sths 2982f062c72Sths static int sh_serial_can_receive(sh_serial_state *s) 2992f062c72Sths { 30063242a00Saurel32 return s->scr & (1 << 4); 3012f062c72Sths } 3022f062c72Sths 3032f062c72Sths static void sh_serial_receive_break(sh_serial_state *s) 3042f062c72Sths { 30563242a00Saurel32 if (s->feat & SH_SERIAL_FEAT_SCIF) 30663242a00Saurel32 s->sr |= (1 << 4); 3072f062c72Sths } 3082f062c72Sths 3092f062c72Sths static int sh_serial_can_receive1(void *opaque) 3102f062c72Sths { 3112f062c72Sths sh_serial_state *s = opaque; 3122f062c72Sths return sh_serial_can_receive(s); 3132f062c72Sths } 3142f062c72Sths 3152f062c72Sths static void sh_serial_receive1(void *opaque, const uint8_t *buf, int size) 3162f062c72Sths { 3172f062c72Sths sh_serial_state *s = opaque; 318b7d2b020SAurelien Jarno 319b7d2b020SAurelien Jarno if (s->feat & SH_SERIAL_FEAT_SCIF) { 320b7d2b020SAurelien Jarno int i; 321b7d2b020SAurelien Jarno for (i = 0; i < size; i++) { 322b7d2b020SAurelien Jarno if (s->rx_cnt < SH_RX_FIFO_LENGTH) { 323b7d2b020SAurelien Jarno s->rx_fifo[s->rx_head++] = buf[i]; 324b7d2b020SAurelien Jarno if (s->rx_head == SH_RX_FIFO_LENGTH) { 325b7d2b020SAurelien Jarno s->rx_head = 0; 326b7d2b020SAurelien Jarno } 327b7d2b020SAurelien Jarno s->rx_cnt++; 328b7d2b020SAurelien Jarno if (s->rx_cnt >= s->rtrg) { 329b7d2b020SAurelien Jarno s->flags |= SH_SERIAL_FLAG_RDF; 330b7d2b020SAurelien Jarno if (s->scr & (1 << 6) && s->rxi) { 331b7d2b020SAurelien Jarno qemu_set_irq(s->rxi, 1); 332b7d2b020SAurelien Jarno } 333b7d2b020SAurelien Jarno } 334b7d2b020SAurelien Jarno } 335b7d2b020SAurelien Jarno } 336b7d2b020SAurelien Jarno } else { 337b7d2b020SAurelien Jarno s->rx_fifo[0] = buf[0]; 338b7d2b020SAurelien Jarno } 3392f062c72Sths } 3402f062c72Sths 3412f062c72Sths static void sh_serial_event(void *opaque, int event) 3422f062c72Sths { 3432f062c72Sths sh_serial_state *s = opaque; 3442f062c72Sths if (event == CHR_EVENT_BREAK) 3452f062c72Sths sh_serial_receive_break(s); 3462f062c72Sths } 3472f062c72Sths 3489a9d0b81SBenoît Canet static const MemoryRegionOps sh_serial_ops = { 3499a9d0b81SBenoît Canet .read = sh_serial_read, 3509a9d0b81SBenoît Canet .write = sh_serial_write, 3519a9d0b81SBenoît Canet .endianness = DEVICE_NATIVE_ENDIAN, 3522f062c72Sths }; 3532f062c72Sths 3549a9d0b81SBenoît Canet void sh_serial_init(MemoryRegion *sysmem, 355a8170e5eSAvi Kivity hwaddr base, int feat, 356bf5b7423Saurel32 uint32_t freq, CharDriverState *chr, 3574e7ed2d1Saurel32 qemu_irq eri_source, 3584e7ed2d1Saurel32 qemu_irq rxi_source, 3594e7ed2d1Saurel32 qemu_irq txi_source, 3604e7ed2d1Saurel32 qemu_irq tei_source, 3614e7ed2d1Saurel32 qemu_irq bri_source) 3622f062c72Sths { 3632f062c72Sths sh_serial_state *s; 3642f062c72Sths 3657267c094SAnthony Liguori s = g_malloc0(sizeof(sh_serial_state)); 3662f062c72Sths 3672f062c72Sths s->feat = feat; 3682f062c72Sths s->flags = SH_SERIAL_FLAG_TEND | SH_SERIAL_FLAG_TDE; 36963242a00Saurel32 s->rtrg = 1; 3702f062c72Sths 3712f062c72Sths s->smr = 0; 3722f062c72Sths s->brr = 0xff; 373b7d35e65Sbalrog s->scr = 1 << 5; /* pretend that TX is enabled so early printk works */ 3742f062c72Sths s->sptr = 0; 3752f062c72Sths 3762f062c72Sths if (feat & SH_SERIAL_FEAT_SCIF) { 3772f062c72Sths s->fcr = 0; 3782f062c72Sths } 3792f062c72Sths else { 3802f062c72Sths s->dr = 0xff; 3812f062c72Sths } 3822f062c72Sths 38363242a00Saurel32 sh_serial_clear_fifo(s); 3842f062c72Sths 3852c9b15caSPaolo Bonzini memory_region_init_io(&s->iomem, NULL, &sh_serial_ops, s, 3869a9d0b81SBenoît Canet "serial", 0x100000000ULL); 3879a9d0b81SBenoît Canet 3882c9b15caSPaolo Bonzini memory_region_init_alias(&s->iomem_p4, NULL, "serial-p4", &s->iomem, 3899a9d0b81SBenoît Canet 0, 0x28); 3909a9d0b81SBenoît Canet memory_region_add_subregion(sysmem, P4ADDR(base), &s->iomem_p4); 3919a9d0b81SBenoît Canet 3922c9b15caSPaolo Bonzini memory_region_init_alias(&s->iomem_a7, NULL, "serial-a7", &s->iomem, 3939a9d0b81SBenoît Canet 0, 0x28); 3949a9d0b81SBenoît Canet memory_region_add_subregion(sysmem, A7ADDR(base), &s->iomem_a7); 3952f062c72Sths 3962f062c72Sths s->chr = chr; 3972f062c72Sths 398456d6069SHans de Goede if (chr) { 399456d6069SHans de Goede qemu_chr_fe_claim_no_fail(chr); 4002f062c72Sths qemu_chr_add_handlers(chr, sh_serial_can_receive1, sh_serial_receive1, 4012f062c72Sths sh_serial_event, s); 402456d6069SHans de Goede } 403bf5b7423Saurel32 404bf5b7423Saurel32 s->eri = eri_source; 405bf5b7423Saurel32 s->rxi = rxi_source; 406bf5b7423Saurel32 s->txi = txi_source; 407bf5b7423Saurel32 s->tei = tei_source; 408bf5b7423Saurel32 s->bri = bri_source; 4092f062c72Sths } 410