xref: /qemu/hw/char/sh_serial.c (revision 4d43a603c71d0eb92534bc82b72933f329d8a64c)
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  */
270430891cSPeter Maydell #include "qemu/osdep.h"
2883c9f4caSPaolo Bonzini #include "hw/hw.h"
290d09e41aSPaolo Bonzini #include "hw/sh4/sh.h"
30*4d43a603SMarc-André Lureau #include "chardev/char-fe.h"
31022c62cbSPaolo Bonzini #include "exec/address-spaces.h"
3232a6ebecSMarc-André Lureau #include "qapi/error.h"
332f062c72Sths 
342f062c72Sths //#define DEBUG_SERIAL
352f062c72Sths 
362f062c72Sths #define SH_SERIAL_FLAG_TEND (1 << 0)
372f062c72Sths #define SH_SERIAL_FLAG_TDE  (1 << 1)
382f062c72Sths #define SH_SERIAL_FLAG_RDF  (1 << 2)
392f062c72Sths #define SH_SERIAL_FLAG_BRK  (1 << 3)
402f062c72Sths #define SH_SERIAL_FLAG_DR   (1 << 4)
412f062c72Sths 
4263242a00Saurel32 #define SH_RX_FIFO_LENGTH (16)
4363242a00Saurel32 
442f062c72Sths typedef struct {
459a9d0b81SBenoît Canet     MemoryRegion iomem;
469a9d0b81SBenoît Canet     MemoryRegion iomem_p4;
479a9d0b81SBenoît Canet     MemoryRegion iomem_a7;
482f062c72Sths     uint8_t smr;
492f062c72Sths     uint8_t brr;
502f062c72Sths     uint8_t scr;
512f062c72Sths     uint8_t dr; /* ftdr / tdr */
522f062c72Sths     uint8_t sr; /* fsr / ssr */
532f062c72Sths     uint16_t fcr;
542f062c72Sths     uint8_t sptr;
552f062c72Sths 
5663242a00Saurel32     uint8_t rx_fifo[SH_RX_FIFO_LENGTH]; /* frdr / rdr */
572f062c72Sths     uint8_t rx_cnt;
5863242a00Saurel32     uint8_t rx_tail;
5963242a00Saurel32     uint8_t rx_head;
602f062c72Sths 
612f062c72Sths     int freq;
622f062c72Sths     int feat;
632f062c72Sths     int flags;
6463242a00Saurel32     int rtrg;
652f062c72Sths 
6632a6ebecSMarc-André Lureau     CharBackend chr;
67bf5b7423Saurel32 
684e7ed2d1Saurel32     qemu_irq eri;
694e7ed2d1Saurel32     qemu_irq rxi;
704e7ed2d1Saurel32     qemu_irq txi;
714e7ed2d1Saurel32     qemu_irq tei;
724e7ed2d1Saurel32     qemu_irq bri;
732f062c72Sths } sh_serial_state;
742f062c72Sths 
7563242a00Saurel32 static void sh_serial_clear_fifo(sh_serial_state * s)
7663242a00Saurel32 {
7763242a00Saurel32     memset(s->rx_fifo, 0, SH_RX_FIFO_LENGTH);
7863242a00Saurel32     s->rx_cnt = 0;
7963242a00Saurel32     s->rx_head = 0;
8063242a00Saurel32     s->rx_tail = 0;
8163242a00Saurel32 }
8263242a00Saurel32 
83a8170e5eSAvi Kivity static void sh_serial_write(void *opaque, hwaddr offs,
849a9d0b81SBenoît Canet                             uint64_t val, unsigned size)
852f062c72Sths {
862f062c72Sths     sh_serial_state *s = opaque;
872f062c72Sths     unsigned char ch;
882f062c72Sths 
892f062c72Sths #ifdef DEBUG_SERIAL
908da3ff18Spbrook     printf("sh_serial: write offs=0x%02x val=0x%02x\n",
918da3ff18Spbrook 	   offs, val);
922f062c72Sths #endif
932f062c72Sths     switch(offs) {
942f062c72Sths     case 0x00: /* SMR */
952f062c72Sths         s->smr = val & ((s->feat & SH_SERIAL_FEAT_SCIF) ? 0x7b : 0xff);
962f062c72Sths         return;
972f062c72Sths     case 0x04: /* BRR */
982f062c72Sths         s->brr = val;
992f062c72Sths 	return;
1002f062c72Sths     case 0x08: /* SCR */
10163242a00Saurel32         /* TODO : For SH7751, SCIF mask should be 0xfb. */
102bf5b7423Saurel32         s->scr = val & ((s->feat & SH_SERIAL_FEAT_SCIF) ? 0xfa : 0xff);
1032f062c72Sths         if (!(val & (1 << 5)))
1042f062c72Sths             s->flags |= SH_SERIAL_FLAG_TEND;
105bf5b7423Saurel32         if ((s->feat & SH_SERIAL_FEAT_SCIF) && s->txi) {
1064e7ed2d1Saurel32 	    qemu_set_irq(s->txi, val & (1 << 7));
107bf5b7423Saurel32         }
1084e7ed2d1Saurel32         if (!(val & (1 << 6))) {
1094e7ed2d1Saurel32 	    qemu_set_irq(s->rxi, 0);
11063242a00Saurel32         }
1112f062c72Sths         return;
1122f062c72Sths     case 0x0c: /* FTDR / TDR */
1135345fdb4SMarc-André Lureau         if (qemu_chr_fe_get_driver(&s->chr)) {
1142f062c72Sths             ch = val;
1156ab3fc32SDaniel P. Berrange             /* XXX this blocks entire thread. Rewrite to use
1166ab3fc32SDaniel P. Berrange              * qemu_chr_fe_write and background I/O callbacks */
1175345fdb4SMarc-André Lureau             qemu_chr_fe_write_all(&s->chr, &ch, 1);
1182f062c72Sths 	}
1192f062c72Sths 	s->dr = val;
1202f062c72Sths 	s->flags &= ~SH_SERIAL_FLAG_TDE;
1212f062c72Sths         return;
1222f062c72Sths #if 0
1232f062c72Sths     case 0x14: /* FRDR / RDR */
1242f062c72Sths         ret = 0;
1252f062c72Sths         break;
1262f062c72Sths #endif
1272f062c72Sths     }
1282f062c72Sths     if (s->feat & SH_SERIAL_FEAT_SCIF) {
1292f062c72Sths         switch(offs) {
1302f062c72Sths         case 0x10: /* FSR */
1312f062c72Sths             if (!(val & (1 << 6)))
1322f062c72Sths                 s->flags &= ~SH_SERIAL_FLAG_TEND;
1332f062c72Sths             if (!(val & (1 << 5)))
1342f062c72Sths                 s->flags &= ~SH_SERIAL_FLAG_TDE;
1352f062c72Sths             if (!(val & (1 << 4)))
1362f062c72Sths                 s->flags &= ~SH_SERIAL_FLAG_BRK;
1372f062c72Sths             if (!(val & (1 << 1)))
1382f062c72Sths                 s->flags &= ~SH_SERIAL_FLAG_RDF;
1392f062c72Sths             if (!(val & (1 << 0)))
1402f062c72Sths                 s->flags &= ~SH_SERIAL_FLAG_DR;
14163242a00Saurel32 
14263242a00Saurel32             if (!(val & (1 << 1)) || !(val & (1 << 0))) {
1434e7ed2d1Saurel32                 if (s->rxi) {
1444e7ed2d1Saurel32                     qemu_set_irq(s->rxi, 0);
14563242a00Saurel32                 }
14663242a00Saurel32             }
1472f062c72Sths             return;
1482f062c72Sths         case 0x18: /* FCR */
1492f062c72Sths             s->fcr = val;
15063242a00Saurel32             switch ((val >> 6) & 3) {
15163242a00Saurel32             case 0:
15263242a00Saurel32                 s->rtrg = 1;
15363242a00Saurel32                 break;
15463242a00Saurel32             case 1:
15563242a00Saurel32                 s->rtrg = 4;
15663242a00Saurel32                 break;
15763242a00Saurel32             case 2:
15863242a00Saurel32                 s->rtrg = 8;
15963242a00Saurel32                 break;
16063242a00Saurel32             case 3:
16163242a00Saurel32                 s->rtrg = 14;
16263242a00Saurel32                 break;
16363242a00Saurel32             }
16463242a00Saurel32             if (val & (1 << 1)) {
16563242a00Saurel32                 sh_serial_clear_fifo(s);
16663242a00Saurel32                 s->sr &= ~(1 << 1);
16763242a00Saurel32             }
16863242a00Saurel32 
1692f062c72Sths             return;
1702f062c72Sths         case 0x20: /* SPTR */
17163242a00Saurel32             s->sptr = val & 0xf3;
1722f062c72Sths             return;
1732f062c72Sths         case 0x24: /* LSR */
1742f062c72Sths             return;
1752f062c72Sths         }
1762f062c72Sths     }
1772f062c72Sths     else {
1782f062c72Sths         switch(offs) {
179d1f193b0Saurel32 #if 0
1802f062c72Sths         case 0x0c:
1812f062c72Sths             ret = s->dr;
1822f062c72Sths             break;
1832f062c72Sths         case 0x10:
1842f062c72Sths             ret = 0;
1852f062c72Sths             break;
1862f062c72Sths #endif
187d1f193b0Saurel32         case 0x1c:
188d1f193b0Saurel32             s->sptr = val & 0x8f;
189d1f193b0Saurel32             return;
190d1f193b0Saurel32         }
1912f062c72Sths     }
1922f062c72Sths 
193c1950a4eSPeter Maydell     fprintf(stderr, "sh_serial: unsupported write to 0x%02"
194a8170e5eSAvi Kivity             HWADDR_PRIx "\n", offs);
19543dc2a64SBlue Swirl     abort();
1962f062c72Sths }
1972f062c72Sths 
198a8170e5eSAvi Kivity static uint64_t sh_serial_read(void *opaque, hwaddr offs,
1999a9d0b81SBenoît Canet                                unsigned size)
2002f062c72Sths {
2012f062c72Sths     sh_serial_state *s = opaque;
2022f062c72Sths     uint32_t ret = ~0;
2032f062c72Sths 
2042f062c72Sths #if 0
2052f062c72Sths     switch(offs) {
2062f062c72Sths     case 0x00:
2072f062c72Sths         ret = s->smr;
2082f062c72Sths         break;
2092f062c72Sths     case 0x04:
2102f062c72Sths         ret = s->brr;
2112f062c72Sths 	break;
2122f062c72Sths     case 0x08:
2132f062c72Sths         ret = s->scr;
2142f062c72Sths         break;
2152f062c72Sths     case 0x14:
2162f062c72Sths         ret = 0;
2172f062c72Sths         break;
2182f062c72Sths     }
2192f062c72Sths #endif
2202f062c72Sths     if (s->feat & SH_SERIAL_FEAT_SCIF) {
2212f062c72Sths         switch(offs) {
222bf5b7423Saurel32         case 0x00: /* SMR */
223bf5b7423Saurel32             ret = s->smr;
224bf5b7423Saurel32             break;
225bf5b7423Saurel32         case 0x08: /* SCR */
226bf5b7423Saurel32             ret = s->scr;
227bf5b7423Saurel32             break;
2282f062c72Sths         case 0x10: /* FSR */
2292f062c72Sths             ret = 0;
2302f062c72Sths             if (s->flags & SH_SERIAL_FLAG_TEND)
2312f062c72Sths                 ret |= (1 << 6);
2322f062c72Sths             if (s->flags & SH_SERIAL_FLAG_TDE)
2332f062c72Sths                 ret |= (1 << 5);
2342f062c72Sths             if (s->flags & SH_SERIAL_FLAG_BRK)
2352f062c72Sths                 ret |= (1 << 4);
2362f062c72Sths             if (s->flags & SH_SERIAL_FLAG_RDF)
2372f062c72Sths                 ret |= (1 << 1);
2382f062c72Sths             if (s->flags & SH_SERIAL_FLAG_DR)
2392f062c72Sths                 ret |= (1 << 0);
2402f062c72Sths 
2412f062c72Sths             if (s->scr & (1 << 5))
2422f062c72Sths                 s->flags |= SH_SERIAL_FLAG_TDE | SH_SERIAL_FLAG_TEND;
2432f062c72Sths 
2442f062c72Sths             break;
24563242a00Saurel32         case 0x14:
24663242a00Saurel32             if (s->rx_cnt > 0) {
24763242a00Saurel32                 ret = s->rx_fifo[s->rx_tail++];
24863242a00Saurel32                 s->rx_cnt--;
24963242a00Saurel32                 if (s->rx_tail == SH_RX_FIFO_LENGTH)
25063242a00Saurel32                     s->rx_tail = 0;
25163242a00Saurel32                 if (s->rx_cnt < s->rtrg)
25263242a00Saurel32                     s->flags &= ~SH_SERIAL_FLAG_RDF;
25363242a00Saurel32             }
25463242a00Saurel32             break;
2552f062c72Sths         case 0x18:
2562f062c72Sths             ret = s->fcr;
2572f062c72Sths             break;
2582f062c72Sths         case 0x1c:
2592f062c72Sths             ret = s->rx_cnt;
2602f062c72Sths             break;
2612f062c72Sths         case 0x20:
2622f062c72Sths             ret = s->sptr;
2632f062c72Sths             break;
2642f062c72Sths         case 0x24:
2652f062c72Sths             ret = 0;
2662f062c72Sths             break;
2672f062c72Sths         }
2682f062c72Sths     }
2692f062c72Sths     else {
2702f062c72Sths         switch(offs) {
271d1f193b0Saurel32 #if 0
2722f062c72Sths         case 0x0c:
2732f062c72Sths             ret = s->dr;
2742f062c72Sths             break;
2752f062c72Sths         case 0x10:
2762f062c72Sths             ret = 0;
2772f062c72Sths             break;
27863242a00Saurel32         case 0x14:
27963242a00Saurel32             ret = s->rx_fifo[0];
28063242a00Saurel32             break;
281d1f193b0Saurel32 #endif
2822f062c72Sths         case 0x1c:
2832f062c72Sths             ret = s->sptr;
2842f062c72Sths             break;
2852f062c72Sths         }
2862f062c72Sths     }
2872f062c72Sths #ifdef DEBUG_SERIAL
2888da3ff18Spbrook     printf("sh_serial: read offs=0x%02x val=0x%x\n",
2898da3ff18Spbrook 	   offs, ret);
2902f062c72Sths #endif
2912f062c72Sths 
2922f062c72Sths     if (ret & ~((1 << 16) - 1)) {
293c1950a4eSPeter Maydell         fprintf(stderr, "sh_serial: unsupported read from 0x%02"
294a8170e5eSAvi Kivity                 HWADDR_PRIx "\n", offs);
29543dc2a64SBlue Swirl         abort();
2962f062c72Sths     }
2972f062c72Sths 
2982f062c72Sths     return ret;
2992f062c72Sths }
3002f062c72Sths 
3012f062c72Sths static int sh_serial_can_receive(sh_serial_state *s)
3022f062c72Sths {
30363242a00Saurel32     return s->scr & (1 << 4);
3042f062c72Sths }
3052f062c72Sths 
3062f062c72Sths static void sh_serial_receive_break(sh_serial_state *s)
3072f062c72Sths {
30863242a00Saurel32     if (s->feat & SH_SERIAL_FEAT_SCIF)
30963242a00Saurel32         s->sr |= (1 << 4);
3102f062c72Sths }
3112f062c72Sths 
3122f062c72Sths static int sh_serial_can_receive1(void *opaque)
3132f062c72Sths {
3142f062c72Sths     sh_serial_state *s = opaque;
3152f062c72Sths     return sh_serial_can_receive(s);
3162f062c72Sths }
3172f062c72Sths 
3182f062c72Sths static void sh_serial_receive1(void *opaque, const uint8_t *buf, int size)
3192f062c72Sths {
3202f062c72Sths     sh_serial_state *s = opaque;
321b7d2b020SAurelien Jarno 
322b7d2b020SAurelien Jarno     if (s->feat & SH_SERIAL_FEAT_SCIF) {
323b7d2b020SAurelien Jarno         int i;
324b7d2b020SAurelien Jarno         for (i = 0; i < size; i++) {
325b7d2b020SAurelien Jarno             if (s->rx_cnt < SH_RX_FIFO_LENGTH) {
326b7d2b020SAurelien Jarno                 s->rx_fifo[s->rx_head++] = buf[i];
327b7d2b020SAurelien Jarno                 if (s->rx_head == SH_RX_FIFO_LENGTH) {
328b7d2b020SAurelien Jarno                     s->rx_head = 0;
329b7d2b020SAurelien Jarno                 }
330b7d2b020SAurelien Jarno                 s->rx_cnt++;
331b7d2b020SAurelien Jarno                 if (s->rx_cnt >= s->rtrg) {
332b7d2b020SAurelien Jarno                     s->flags |= SH_SERIAL_FLAG_RDF;
333b7d2b020SAurelien Jarno                     if (s->scr & (1 << 6) && s->rxi) {
334b7d2b020SAurelien Jarno                         qemu_set_irq(s->rxi, 1);
335b7d2b020SAurelien Jarno                     }
336b7d2b020SAurelien Jarno                 }
337b7d2b020SAurelien Jarno             }
338b7d2b020SAurelien Jarno         }
339b7d2b020SAurelien Jarno     } else {
340b7d2b020SAurelien Jarno         s->rx_fifo[0] = buf[0];
341b7d2b020SAurelien Jarno     }
3422f062c72Sths }
3432f062c72Sths 
3442f062c72Sths static void sh_serial_event(void *opaque, int event)
3452f062c72Sths {
3462f062c72Sths     sh_serial_state *s = opaque;
3472f062c72Sths     if (event == CHR_EVENT_BREAK)
3482f062c72Sths         sh_serial_receive_break(s);
3492f062c72Sths }
3502f062c72Sths 
3519a9d0b81SBenoît Canet static const MemoryRegionOps sh_serial_ops = {
3529a9d0b81SBenoît Canet     .read = sh_serial_read,
3539a9d0b81SBenoît Canet     .write = sh_serial_write,
3549a9d0b81SBenoît Canet     .endianness = DEVICE_NATIVE_ENDIAN,
3552f062c72Sths };
3562f062c72Sths 
3579a9d0b81SBenoît Canet void sh_serial_init(MemoryRegion *sysmem,
358a8170e5eSAvi Kivity                     hwaddr base, int feat,
3590ec7b3e7SMarc-André Lureau                     uint32_t freq, Chardev *chr,
3604e7ed2d1Saurel32                     qemu_irq eri_source,
3614e7ed2d1Saurel32                     qemu_irq rxi_source,
3624e7ed2d1Saurel32                     qemu_irq txi_source,
3634e7ed2d1Saurel32                     qemu_irq tei_source,
3644e7ed2d1Saurel32                     qemu_irq bri_source)
3652f062c72Sths {
3662f062c72Sths     sh_serial_state *s;
3672f062c72Sths 
3687267c094SAnthony Liguori     s = g_malloc0(sizeof(sh_serial_state));
3692f062c72Sths 
3702f062c72Sths     s->feat = feat;
3712f062c72Sths     s->flags = SH_SERIAL_FLAG_TEND | SH_SERIAL_FLAG_TDE;
37263242a00Saurel32     s->rtrg = 1;
3732f062c72Sths 
3742f062c72Sths     s->smr = 0;
3752f062c72Sths     s->brr = 0xff;
376b7d35e65Sbalrog     s->scr = 1 << 5; /* pretend that TX is enabled so early printk works */
3772f062c72Sths     s->sptr = 0;
3782f062c72Sths 
3792f062c72Sths     if (feat & SH_SERIAL_FEAT_SCIF) {
3802f062c72Sths         s->fcr = 0;
3812f062c72Sths     }
3822f062c72Sths     else {
3832f062c72Sths         s->dr = 0xff;
3842f062c72Sths     }
3852f062c72Sths 
38663242a00Saurel32     sh_serial_clear_fifo(s);
3872f062c72Sths 
3882c9b15caSPaolo Bonzini     memory_region_init_io(&s->iomem, NULL, &sh_serial_ops, s,
3899a9d0b81SBenoît Canet                           "serial", 0x100000000ULL);
3909a9d0b81SBenoît Canet 
3912c9b15caSPaolo Bonzini     memory_region_init_alias(&s->iomem_p4, NULL, "serial-p4", &s->iomem,
3929a9d0b81SBenoît Canet                              0, 0x28);
3939a9d0b81SBenoît Canet     memory_region_add_subregion(sysmem, P4ADDR(base), &s->iomem_p4);
3949a9d0b81SBenoît Canet 
3952c9b15caSPaolo Bonzini     memory_region_init_alias(&s->iomem_a7, NULL, "serial-a7", &s->iomem,
3969a9d0b81SBenoît Canet                              0, 0x28);
3979a9d0b81SBenoît Canet     memory_region_add_subregion(sysmem, A7ADDR(base), &s->iomem_a7);
3982f062c72Sths 
399456d6069SHans de Goede     if (chr) {
40032a6ebecSMarc-André Lureau         qemu_chr_fe_init(&s->chr, chr, &error_abort);
4015345fdb4SMarc-André Lureau         qemu_chr_fe_set_handlers(&s->chr, sh_serial_can_receive1,
4025345fdb4SMarc-André Lureau                                  sh_serial_receive1,
40339ab61c6SMarc-André Lureau                                  sh_serial_event, s, NULL, true);
404456d6069SHans de Goede     }
405bf5b7423Saurel32 
406bf5b7423Saurel32     s->eri = eri_source;
407bf5b7423Saurel32     s->rxi = rxi_source;
408bf5b7423Saurel32     s->txi = txi_source;
409bf5b7423Saurel32     s->tei = tei_source;
410bf5b7423Saurel32     s->bri = bri_source;
4112f062c72Sths }
412