2 * Avanstar Xp pci uart driver
5 #include "../port/lib.h"
10 #include "../port/error.h"
15 typedef struct Ccb Ccb;
16 typedef struct Ctlr Ctlr;
17 typedef struct Gcb Gcb;
20 * Global Control Block.
21 * Service Request fields must be accessed using XCHG.
24 u16int gcw; /* Global Command Word */
25 u16int gsw; /* Global Status Word */
26 u16int gsr; /* Global Service Request */
27 u16int abs; /* Available Buffer Space */
28 u16int bt; /* Board Type */
29 u16int cpv; /* Control Program Version */
30 u16int ccbn; /* Ccb count */
31 u16int ccboff; /* Ccb offset */
32 u16int ccbsz; /* Ccb size */
33 u16int gcw2; /* Global Command Word 2 */
34 u16int gsw2; /* Global Status Word 2 */
35 u16int esr; /* Error Service Request */
36 u16int isr; /* Input Service Request */
37 u16int osr; /* Output Service Request */
38 u16int msr; /* Modem Service Request */
39 u16int csr; /* Command Service Request */
43 * Channel Control Block.
46 u16int br; /* Baud Rate */
47 u16int df; /* Data Format */
48 u16int lp; /* Line Protocol */
49 u16int ibs; /* Input Buffer Size */
50 u16int obs; /* Output Buffer Size */
51 u16int ibtr; /* Ib Trigger Rate */
52 u16int oblw; /* Ob Low Watermark */
53 u8int ixon[2]; /* IXON characters */
54 u16int ibhw; /* Ib High Watermark */
55 u16int iblw; /* Ib Low Watermark */
56 u16int cc; /* Channel Command */
57 u16int cs; /* Channel Status */
58 u16int ibsa; /* Ib Start Addr */
59 u16int ibea; /* Ib Ending Addr */
60 u16int obsa; /* Ob Start Addr */
61 u16int obea; /* Ob Ending Addr */
62 u16int ibwp; /* Ib write pointer (RO) */
63 u16int ibrp; /* Ib read pointer (R/W) */
64 u16int obwp; /* Ob write pointer (R/W) */
65 u16int obrp; /* Ob read pointer (RO) */
66 u16int ces; /* Communication Error Status */
67 u16int bcp; /* Bad Character Pointer */
68 u16int mc; /* Modem Control */
69 u16int ms; /* Modem Status */
70 u16int bs; /* Blocking Status */
71 u16int crf; /* Character Received Flag */
72 u8int ixoff[2]; /* IXOFF characters */
73 u16int cs2; /* Channel Status 2 */
74 u8int sec[2]; /* Strip/Error Characters */
83 Db5 = 0x0000, /* Data Bits - 5 bits/byte */
84 Db6 = 0x0001, /* 6 bits/byte */
85 Db7 = 0x0002, /* 7 bits/byte */
86 Db8 = 0x0003, /* 8 bits/byte */
88 Sb1 = 0x0000, /* 1 Stop Bit */
89 Sb2 = 0x0004, /* 2 Stop Bit */
91 Np = 0x0000, /* No Parity */
92 Op = 0x0008, /* Odd Parity */
93 Ep = 0x0010, /* Even Parity */
94 Mp = 0x0020, /* Mark Parity */
95 Sp = 0x0030, /* Space Parity */
97 Cmn = 0x0000, /* Channel Mode Normal */
98 Cme = 0x0040, /* CM Echo */
99 Cmll = 0x0080, /* CM Local Loopback */
100 Cmrl = 0x00C0, /* CM Remote Loopback */
104 Ixon = 0x0001, /* Obey IXON/IXOFF */
105 Ixany = 0x0002, /* Any character retarts Tx */
106 Ixgen = 0x0004, /* Generate IXON/IXOFF */
107 Cts = 0x0008, /* CTS controls Tx */
108 Dtr = 0x0010, /* Rx controls DTR */
109 ½d = 0x0020, /* RTS off during Tx */
110 Rts = 0x0040, /* generate RTS */
111 Emcs = 0x0080, /* Enable Modem Control */
112 Ecs = 0x1000, /* Enable Character Stripping */
113 Eia422 = 0x2000, /* EIA422 */
117 Ccu = 0x0001, /* Configure Channel and UART */
118 Cco = 0x0002, /* Configure Channel Only */
119 Fib = 0x0004, /* Flush Input Buffer */
120 Fob = 0x0008, /* Flush Output Buffer */
121 Er = 0x0010, /* Enable Receiver */
122 Dr = 0x0020, /* Disable Receiver */
123 Et = 0x0040, /* Enable Transmitter */
124 Dt = 0x0080, /* Disable Transmitter */
128 Oe = 0x0001, /* Overrun Error */
129 Pe = 0x0002, /* Parity Error */
130 Fe = 0x0004, /* Framing Error */
131 Br = 0x0008, /* Break Received */
135 Adtr = 0x0001, /* Assert DTR */
136 Arts = 0x0002, /* Assert RTS */
137 Ab = 0x0010, /* Assert BREAK */
141 Scts = 0x0001, /* Status od CTS */
142 Sdsr = 0x0002, /* Status of DSR */
143 Sri = 0x0004, /* Status of RI */
144 Sdcd = 0x0008, /* Status of DCD */
148 Rd = 0x0001, /* Receiver Disabled */
149 Td = 0x0002, /* Transmitter Disabled */
150 Tbxoff = 0x0004, /* Tx Blocked by XOFF */
151 Tbcts = 0x0008, /* Tx Blocked by CTS */
152 Rbxoff = 0x0010, /* Rx Blocked by XOFF */
153 Rbrts = 0x0020, /* Rx Blocked by RTS */
156 enum { /* Local Configuration */
160 Mb0 = 0x40, /* Mailbox 0 */
161 Ldb = 0x60, /* PCI to Local Doorbell */
162 Pdb = 0x64, /* Local to PCI Doorbell */
163 Ics = 0x68, /* Interrupt Control/Status */
164 Mcc = 0x6C, /* Misc. Command and Control */
168 Edcc = 1, /* exec. downloaded code cmd */
169 Aic = 0x10, /* adapter init'zed correctly */
170 Cpr = 1ul << 31, /* control program ready */
174 Rcr = 1ul << 29, /* reload config. reg.s */
175 Asr = 1ul << 30, /* pci adapter sw reset */
176 Lis = 1ul << 31, /* local init status */
179 typedef struct Cc Cc;
180 typedef struct Ccb Ccb;
181 typedef struct Ctlr Ctlr;
184 * Channel Control, one per uart.
185 * Devuart communicates via the PhysUart functions with
186 * a Uart* argument. Uart.regs is filled in by this driver
187 * to point to a Cc, and Cc.ctlr points to the Axp board
200 typedef struct Ctlr {
210 int im; /* interrupt mask */
214 #define csr32r(c, r) (*((c)->reg+((r)/4)))
215 #define csr32w(c, r, v) (*((c)->reg+((r)/4)) = (v))
217 static Ctlr* axpctlrhead;
218 static Ctlr* axpctlrtail;
220 extern PhysUart axpphysuart;
225 return !((Ccb*)ccb)->cc; /* hw sets ccb->cc to zero */
229 axpcc(Cc* cc, int cmd)
239 for(timeo = 0; timeo < 1000000; timeo++){
245 tsleep(cc, axpccdone, ccb, 1000);
249 print("%s: cmd %#ux didn't terminate: %#ux %#ux\n",
250 cc->name, cmd, ccb->cc, cs);
257 axpstatus(Uart* uart, void* buf, long n, long offset)
261 u16int bs, fstat, ms;
263 ccb = ((Cc*)(uart->regs))->ccb;
265 p = smalloc(READSTR);
271 "b%d c%d d%d e%d l%d m%d p%c r%d s%d i%d\n"
272 "dev(%d) type(%d) framing(%d) overruns(%d) "
273 "berr(%d) serr(%d)%s%s%s%s\n",
279 (fstat & DbMASK) + 5,
281 (fstat & PMASK) ? ((fstat & Ep) == Ep? 'e': 'o'): 'n',
282 (bs & Rbrts) ? 1 : 0,
283 (fstat & Sb2) ? 2 : 1,
292 (ms & Scts) ? " cts" : "",
293 (ms & Sdsr) ? " dsr" : "",
294 (ms & Sdcd) ? " dcd" : "",
295 (ms & Sri) ? " ring" : ""
297 n = readstr(offset, buf, n, p);
309 axpdtr(Uart* uart, int on)
314 ccb = ((Cc*)(uart->regs))->ccb;
325 * can be called from uartstageinput() during an input interrupt,
326 * with uart->rlock ilocked or the uart qlocked, sometimes both.
329 axprts(Uart* uart, int on)
334 ccb = ((Cc*)(uart->regs))->ccb;
345 axpmodemctl(Uart* uart, int on)
350 ccb = ((Cc*)(uart->regs))->ccb;
357 uart->cts = ccb->ms & Scts;
365 iunlock(&uart->tlock);
368 axpcc(uart->regs, Ccu);
372 axpparity(Uart* uart, int parity)
391 ccb = ((Cc*)(uart->regs))->ccb;
393 df = ccb->df & ~PMASK;
395 axpcc(uart->regs, Ccu);
401 axpstop(Uart* uart, int stop)
417 ccb = ((Cc*)(uart->regs))->ccb;
419 df = ccb->df & ~SbMASK;
421 axpcc(uart->regs, Ccu);
427 axpbits(Uart* uart, int bits)
433 if(bits < 0 || bits > 3)
436 ccb = ((Cc*)(uart->regs))->ccb;
438 df = ccb->df & ~DbMASK;
440 axpcc(uart->regs, Ccu);
446 axpbaud(Uart* uart, int baud)
452 * Set baud rate (high rates are special - only 16 bits).
458 ccb = ((Cc*)(uart->regs))->ccb;
473 * Set trigger level to about 50 per second.
476 i = (ccb->ibea - ccb->ibsa)/2;
480 axpcc(uart->regs, Ccu);
486 axpbreak(Uart* uart, int ms)
497 ccb = ((Cc*)(uart->regs))->ccb;
501 tsleep(&up->sleep, return0, 0, ms);
505 /* only called from interrupt service */
521 if(old == 0 && cc->cts)
527 if(cc->hup_dsr && cc->dsr && !old)
533 if(cc->hup_dcd && cc->dcd && !old)
539 /* called from uartkick() with uart->tlock ilocked */
545 uchar *ep, *mem, *rp, *wp, *bp;
547 if(uart->cts == 0 || uart->blocked)
553 mem = (uchar*)cc->ctlr->gcb;
554 bp = mem + ccb->obsa;
555 rp = mem + ccb->obrp;
556 wp = mem + ccb->obwp;
557 ep = mem + ccb->obea;
558 while(wp != rp-1 && (rp != bp || wp != ep)){
560 * if we've exhausted the uart's output buffer,
561 * ask for more from the output queue, and quit if there
564 if(uart->op >= uart->oe && uartstageoutput(uart) == 0)
566 *wp++ = *(uart->op++);
569 ccb->obwp = wp - mem;
573 /* only called from interrupt service */
578 uchar *ep, *mem, *rp, *wp;
582 mem = (uchar*)cc->ctlr->gcb;
583 rp = mem + ccb->ibrp;
584 wp = mem + ccb->ibwp;
585 ep = mem + ccb->ibea;
588 uartrecv(cc, *rp++); /* ilocks cc->tlock */
590 rp = mem + ccb->ibsa;
591 ccb->ibrp = rp - mem;
596 axpinterrupt(Ureg*, void* arg)
606 ics = csr32r(ctlr, Ics);
608 print("%s: unexpected interrupt %#ux\n", ctlr->name, ics);
609 if(!(ics & 0x00002000)) {
610 /* we get a steady stream of these on consoles */
611 // print("%s: non-doorbell interrupt\n", ctlr->name);
612 ctlr->gcb->gcw2 = 0x0001; /* set Gintack */
616 // while(work to do){
618 for(sr = xchgw(&ctlr->gcb->isr, 0); sr != 0; sr >>= 1){
624 for(sr = xchgw(&ctlr->gcb->osr, 0); sr != 0; sr >>= 1){
626 work++, uartkick(&cc->Uart);
630 for(sr = xchgw(&ctlr->gcb->csr, 0); sr != 0; sr >>= 1){
636 for(sr = xchgw(&ctlr->gcb->msr, 0); sr != 0; sr >>= 1){
642 for(sr = xchgw(&ctlr->gcb->esr, 0); sr != 0; sr >>= 1){
657 /* only meaningful if we don't share the irq */
659 print("%s: interrupt with no work\n", ctlr->name);
660 csr32w(ctlr, Pdb, 1); /* clear doorbell interrupt */
661 ctlr->gcb->gcw2 = 0x0001; /* set Gintack */
665 axpdisable(Uart* uart)
672 * Turn off DTR and RTS, disable interrupts.
674 (*uart->phys->dtr)(uart, 0);
675 (*uart->phys->rts)(uart, 0);
679 cc->ccb->lp = Emcs|lp;
680 axpcc(cc, Dt|Dr|Fob|Fib|Ccu);
683 * The Uart is qlocked.
686 ctlr->im &= ~(1<<cc->uartno);
688 intrdisable(ctlr->pcidev->intl, axpinterrupt, ctlr,
689 ctlr->pcidev->tbdf, ctlr->name);
693 axpenable(Uart* uart, int ie)
703 * Enable interrupts and turn on DTR and RTS.
704 * Be careful if this is called to set up a polled serial line
705 * early on not to try to enable interrupts as interrupt-
706 * -enabling mechanisms might not be set up yet.
710 * The Uart is qlocked.
713 intrenable(ctlr->pcidev->intl, axpinterrupt, ctlr,
714 ctlr->pcidev->tbdf, ctlr->name);
715 csr32w(ctlr, Ics, 0x00031F00);
716 csr32w(ctlr, Pdb, 1);
719 ctlr->im |= 1<<cc->uartno;
722 (*uart->phys->dtr)(uart, 1);
723 (*uart->phys->rts)(uart, 1);
726 * Make sure we control RTS, DTR and break.
729 cc->ccb->lp = Emcs|lp;
731 axpcc(cc, Et|Er|Ccu);
735 axpdealloc(Ctlr* ctlr)
739 for(i = 0; i < 16; i++){
740 if(ctlr->cc[i].name != nil)
741 free(ctlr->cc[i].name);
744 vunmap(ctlr->reg, ctlr->pcidev->mem[0].size);
746 vunmap(ctlr->mem, ctlr->pcidev->mem[2].size);
747 if(ctlr->name != nil)
755 axpalloc(int ctlrno, Pcidev* pcidev)
765 ctlr = malloc(sizeof(Ctlr));
767 print("uartaxp: can't allocate memory\n");
770 seprint(name, name+sizeof(name), "uartaxp%d", ctlrno);
771 kstrdup(&ctlr->name, name);
772 ctlr->pcidev = pcidev;
773 ctlr->ctlrno = ctlrno;
776 * Access to runtime registers.
778 bar = pcidev->mem[0].bar;
779 if((addr = vmap(bar & ~0x0F, pcidev->mem[0].size)) == 0){
780 print("%s: can't map registers at %#ux\n", ctlr->name, bar);
781 return axpdealloc(ctlr);
784 print("%s: port 0x%ux irq %d ", ctlr->name, bar, pcidev->intl);
787 * Local address space 0.
789 bar = pcidev->mem[2].bar;
790 if((addr = vmap(bar & ~0x0F, pcidev->mem[2].size)) == 0){
791 print("%s: can't map memory at %#ux\n", ctlr->name, bar);
792 return axpdealloc(ctlr);
795 ctlr->gcb = (Gcb*)(ctlr->mem+0x10000);
796 print("mem 0x%ux size %d: ", bar, pcidev->mem[2].size);
801 * Toggle the software reset and wait for
802 * the adapter local init status to indicate done.
804 * The two 'delay(100)'s below are important,
805 * without them the board seems to become confused
806 * (perhaps it needs some 'quiet time' because the
807 * timeout loops are not sufficient in themselves).
809 r = csr32r(ctlr, Mcc);
810 csr32w(ctlr, Mcc, r|Asr);
812 csr32w(ctlr, Mcc, r&~Asr);
815 for(timeo = 0; timeo < 100000; timeo++){
816 if(csr32r(ctlr, Mcc) & Lis)
820 if(!(csr32r(ctlr, Mcc) & Lis)){
821 print("%s: couldn't reset\n", ctlr->name);
822 return axpdealloc(ctlr);
824 print("downloading...");
826 * Copy the control programme to the card memory.
827 * The card's i960 control structures live at 0xD000.
829 if(sizeof(uartaxpcp) > 0xD000){
830 print("%s: control programme too big\n", ctlr->name);
831 return axpdealloc(ctlr);
833 /* TODO: is this right for more than 1 card? devastar does the same */
834 csr32w(ctlr, Remap, 0xA0000001);
835 for(i = 0; i < sizeof(uartaxpcp); i++)
836 ctlr->mem[i] = uartaxpcp[i];
838 * Execute downloaded code and wait for it
841 csr32w(ctlr, Mb0, Edcc);
843 /* the manual says to wait for Cpr for 1 second */
844 for(timeo = 0; timeo < 10000; timeo++){
845 if(csr32r(ctlr, Mb0) & Cpr)
849 if(!(csr32r(ctlr, Mb0) & Cpr)){
850 print("control programme not ready; Mb0 %#ux\n",
852 print("%s: distribution panel not connected or card not fully seated?\n",
855 return axpdealloc(ctlr);
860 if(ctlr->gcb->bt != 0x12 || n > 16){
861 print("%s: wrong board type %#ux, %d channels\n",
862 ctlr->name, ctlr->gcb->bt, ctlr->gcb->ccbn);
863 return axpdealloc(ctlr);
866 p = ((uchar*)ctlr->gcb) + ctlr->gcb->ccboff;
867 for(i = 0; i < n; i++){
870 p += ctlr->gcb->ccbsz;
874 cc->regs = cc; /* actually Uart->regs */
875 seprint(name, name+sizeof(name), "uartaxp%d%2.2d", ctlrno, i);
876 kstrdup(&cc->name, name);
882 cc->phys = &axpphysuart;
886 cc->next = &ctlr->cc[i+1];
888 ctlr->cc[n-1].next = nil;
891 if(axpctlrhead != nil)
892 axpctlrtail->next = ctlr;
905 Uart *head, *tail, *uart;
908 * Loop through all PCI devices looking for simple serial
909 * controllers (ccrb == 0x07) and configure the ones which
914 for(p = pcimatch(nil, 0, 0); p != nil; p = pcimatch(p, 0, 0)){
918 switch((p->did<<16)|p->vid){
921 case (0x6001<<16)|0x114F: /* AvanstarXp */
922 if((uart = axpalloc(ctlrno, p)) == nil)
931 for(tail = uart; tail->next != nil; tail = tail->next)
939 PhysUart axpphysuart = {
940 .name = "AvanstarXp",
943 .disable = axpdisable,
950 .modemctl = axpmodemctl,