uarti8250.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "../port/error.h"
  8. /*
  9. * 8250 UART and compatibles.
  10. */
  11. enum {
  12. Uart0 = 0x3F8, /* COM1 */
  13. Uart0IRQ = 4,
  14. Uart1 = 0x2F8, /* COM2 */
  15. Uart1IRQ = 3,
  16. UartFREQ = 1843200,
  17. };
  18. enum { /* I/O ports */
  19. Rbr = 0, /* Receiver Buffer (RO) */
  20. Thr = 0, /* Transmitter Holding (WO) */
  21. Ier = 1, /* Interrupt Enable */
  22. Iir = 2, /* Interrupt Identification (RO) */
  23. Fcr = 2, /* FIFO Control (WO) */
  24. Lcr = 3, /* Line Control */
  25. Mcr = 4, /* Modem Control */
  26. Lsr = 5, /* Line Status */
  27. Msr = 6, /* Modem Status */
  28. Scr = 7, /* Scratch Pad */
  29. Dll = 0, /* Divisor Latch LSB */
  30. Dlm = 1, /* Divisor Latch MSB */
  31. };
  32. enum { /* Ier */
  33. Erda = 0x01, /* Enable Received Data Available */
  34. Ethre = 0x02, /* Enable Thr Empty */
  35. Erls = 0x04, /* Enable Receiver Line Status */
  36. Ems = 0x08, /* Enable Modem Status */
  37. };
  38. enum { /* Iir */
  39. Ims = 0x00, /* Ms interrupt */
  40. Ip = 0x01, /* Interrupt Pending (not) */
  41. Ithre = 0x02, /* Thr Empty */
  42. Irda = 0x04, /* Received Data Available */
  43. Irls = 0x06, /* Receiver Line Status */
  44. Ictoi = 0x0C, /* Character Time-out Indication */
  45. IirMASK = 0x3F,
  46. Ife = 0xC0, /* FIFOs enabled */
  47. };
  48. enum { /* Fcr */
  49. FIFOena = 0x01, /* FIFO enable */
  50. FIFOrclr = 0x02, /* clear Rx FIFO */
  51. FIFOtclr = 0x04, /* clear Tx FIFO */
  52. FIFO1 = 0x00, /* Rx FIFO trigger level 1 byte */
  53. FIFO4 = 0x40, /* 4 bytes */
  54. FIFO8 = 0x80, /* 8 bytes */
  55. FIFO14 = 0xC0, /* 14 bytes */
  56. };
  57. enum { /* Lcr */
  58. Wls5 = 0x00, /* Word Length Select 5 bits/byte */
  59. Wls6 = 0x01, /* 6 bits/byte */
  60. Wls7 = 0x02, /* 7 bits/byte */
  61. Wls8 = 0x03, /* 8 bits/byte */
  62. WlsMASK = 0x03,
  63. Stb = 0x04, /* 2 stop bits */
  64. Pen = 0x08, /* Parity Enable */
  65. Eps = 0x10, /* Even Parity Select */
  66. Stp = 0x20, /* Stick Parity */
  67. Brk = 0x40, /* Break */
  68. Dlab = 0x80, /* Divisor Latch Access Bit */
  69. };
  70. enum { /* Mcr */
  71. Dtr = 0x01, /* Data Terminal Ready */
  72. Rts = 0x02, /* Ready To Send */
  73. Out1 = 0x04, /* no longer in use */
  74. Ie = 0x08, /* IRQ Enable */
  75. Dm = 0x10, /* Diagnostic Mode loopback */
  76. };
  77. enum { /* Lsr */
  78. Dr = 0x01, /* Data Ready */
  79. Oe = 0x02, /* Overrun Error */
  80. Pe = 0x04, /* Parity Error */
  81. Fe = 0x08, /* Framing Error */
  82. Bi = 0x10, /* Break Interrupt */
  83. Thre = 0x20, /* Thr Empty */
  84. Temt = 0x40, /* Tramsmitter Empty */
  85. FIFOerr = 0x80, /* error in receiver FIFO */
  86. };
  87. enum { /* Msr */
  88. Dcts = 0x01, /* Delta Cts */
  89. Ddsr = 0x02, /* Delta Dsr */
  90. Teri = 0x04, /* Trailing Edge of Ri */
  91. Ddcd = 0x08, /* Delta Dcd */
  92. Cts = 0x10, /* Clear To Send */
  93. Dsr = 0x20, /* Data Set Ready */
  94. Ri = 0x40, /* Ring Indicator */
  95. Dcd = 0x80, /* Data Set Ready */
  96. };
  97. typedef struct Ctlr {
  98. int io;
  99. int irq;
  100. int tbdf;
  101. int iena;
  102. uchar sticky[8];
  103. Lock;
  104. int fifo;
  105. int fena;
  106. } Ctlr;
  107. extern PhysUart i8250physuart;
  108. static Ctlr i8250ctlr[2] = {
  109. { .io = Uart0,
  110. .irq = Uart0IRQ,
  111. .tbdf = BUSUNKNOWN, },
  112. { .io = Uart1,
  113. .irq = Uart1IRQ,
  114. .tbdf = BUSUNKNOWN, },
  115. };
  116. static Uart i8250uart[2] = {
  117. { .regs = &i8250ctlr[0],
  118. .name = "COM1",
  119. .freq = UartFREQ,
  120. .phys = &i8250physuart,
  121. .special= 0,
  122. .next = &i8250uart[1], },
  123. { .regs = &i8250ctlr[1],
  124. .name = "COM2",
  125. .freq = UartFREQ,
  126. .phys = &i8250physuart,
  127. .special= 0,
  128. .next = nil, },
  129. };
  130. #define csr8r(c, r) inb((c)->io+(r))
  131. #define csr8w(c, r, v) outb((c)->io+(r), (c)->sticky[(r)]|(v))
  132. static long
  133. i8250status(Uart* uart, void* buf, long n, long offset)
  134. {
  135. char *p;
  136. Ctlr *ctlr;
  137. uchar ier, lcr, mcr, msr;
  138. ctlr = uart->regs;
  139. p = malloc(READSTR);
  140. mcr = ctlr->sticky[Mcr];
  141. msr = csr8r(ctlr, Msr);
  142. ier = ctlr->sticky[Ier];
  143. lcr = ctlr->sticky[Lcr];
  144. snprint(p, READSTR,
  145. "b%d c%d d%d e%d l%d m%d p%c r%d s%d i%d ier=%ux\n"
  146. "dev(%d) type(%d) framing(%d) overruns(%d)%s%s%s%s\n",
  147. uart->baud,
  148. uart->hup_dcd,
  149. (msr & Dsr) != 0,
  150. uart->hup_dsr,
  151. (lcr & WlsMASK) + 5,
  152. (ier & Ems) != 0,
  153. (lcr & Pen) ? ((lcr & Eps) ? 'e': 'o'): 'n',
  154. (mcr & Rts) != 0,
  155. (lcr & Stb) ? 2: 1,
  156. ctlr->fena,
  157. ier,
  158. uart->dev,
  159. uart->type,
  160. uart->ferr,
  161. uart->oerr,
  162. (msr & Cts) ? " cts": "",
  163. (msr & Dsr) ? " dsr": "",
  164. (msr & Dcd) ? " dcd": "",
  165. (msr & Ri) ? " ring": ""
  166. );
  167. n = readstr(offset, buf, n, p);
  168. free(p);
  169. return n;
  170. }
  171. static void
  172. i8250fifo(Uart* uart, int on)
  173. {
  174. int i;
  175. Ctlr *ctlr;
  176. /*
  177. * Toggle FIFOs:
  178. * if none, do nothing;
  179. * reset the Rx and Tx FIFOs;
  180. * empty the Rx buffer and clear any interrupt conditions;
  181. * if enabling, try to turn them on.
  182. */
  183. ctlr = uart->regs;
  184. ilock(ctlr);
  185. if(!ctlr->fifo){
  186. csr8w(ctlr, Fcr, FIFOtclr|FIFOrclr);
  187. for(i = 0; i < 16; i++){
  188. csr8r(ctlr, Iir);
  189. csr8r(ctlr, Rbr);
  190. }
  191. ctlr->fena = 0;
  192. if(on){
  193. csr8w(ctlr, Fcr, FIFO1|FIFOena);
  194. if(!(csr8r(ctlr, Iir) & Ife))
  195. ctlr->fifo = 1;
  196. ctlr->fena = 1;
  197. }
  198. }
  199. iunlock(ctlr);
  200. }
  201. static void
  202. i8250dtr(Uart* uart, int on)
  203. {
  204. Ctlr *ctlr;
  205. /*
  206. * Toggle DTR.
  207. */
  208. ctlr = uart->regs;
  209. if(on)
  210. ctlr->sticky[Mcr] |= Dtr;
  211. else
  212. ctlr->sticky[Mcr] &= ~Dtr;
  213. csr8w(ctlr, Mcr, 0);
  214. }
  215. static void
  216. i8250rts(Uart* uart, int on)
  217. {
  218. Ctlr *ctlr;
  219. /*
  220. * Toggle RTS.
  221. */
  222. ctlr = uart->regs;
  223. if(on)
  224. ctlr->sticky[Mcr] |= Rts;
  225. else
  226. ctlr->sticky[Mcr] &= ~Rts;
  227. csr8w(ctlr, Mcr, 0);
  228. }
  229. static void
  230. i8250modemctl(Uart* uart, int on)
  231. {
  232. Ctlr *ctlr;
  233. ctlr = uart->regs;
  234. ilock(&uart->tlock);
  235. if(on){
  236. ctlr->sticky[Ier] |= Ems;
  237. csr8w(ctlr, Ier, ctlr->sticky[Ier]);
  238. uart->modem = 1;
  239. uart->cts = csr8r(ctlr, Msr) & Cts;
  240. }
  241. else{
  242. ctlr->sticky[Ier] &= ~Ems;
  243. csr8w(ctlr, Ier, ctlr->sticky[Ier]);
  244. uart->modem = 0;
  245. uart->cts = 1;
  246. }
  247. iunlock(&uart->tlock);
  248. /* modem needs fifo */
  249. (*uart->phys->fifo)(uart, on);
  250. }
  251. static int
  252. i8250parity(Uart* uart, int parity)
  253. {
  254. int lcr;
  255. Ctlr *ctlr;
  256. ctlr = uart->regs;
  257. lcr = ctlr->sticky[Lcr] & ~(Eps|Pen);
  258. switch(parity){
  259. case 'e':
  260. lcr |= Eps|Pen;
  261. break;
  262. case 'o':
  263. lcr |= Pen;
  264. break;
  265. case 'n':
  266. default:
  267. break;
  268. }
  269. ctlr->sticky[Lcr] = lcr;
  270. csr8w(ctlr, Lcr, 0);
  271. uart->parity = parity;
  272. return 0;
  273. }
  274. static int
  275. i8250stop(Uart* uart, int stop)
  276. {
  277. int lcr;
  278. Ctlr *ctlr;
  279. ctlr = uart->regs;
  280. lcr = ctlr->sticky[Lcr] & ~Stb;
  281. switch(stop){
  282. case 1:
  283. break;
  284. case 2:
  285. lcr |= Stb;
  286. break;
  287. default:
  288. return -1;
  289. }
  290. ctlr->sticky[Lcr] = lcr;
  291. csr8w(ctlr, Lcr, 0);
  292. uart->stop = stop;
  293. return 0;
  294. }
  295. static int
  296. i8250bits(Uart* uart, int bits)
  297. {
  298. int lcr;
  299. Ctlr *ctlr;
  300. ctlr = uart->regs;
  301. lcr = ctlr->sticky[Lcr] & ~WlsMASK;
  302. switch(bits){
  303. case 5:
  304. lcr |= Wls5;
  305. break;
  306. case 6:
  307. lcr |= Wls6;
  308. break;
  309. case 7:
  310. lcr |= Wls7;
  311. break;
  312. case 8:
  313. lcr |= Wls8;
  314. break;
  315. default:
  316. return -1;
  317. }
  318. ctlr->sticky[Lcr] = lcr;
  319. csr8w(ctlr, Lcr, 0);
  320. uart->bits = bits;
  321. return 0;
  322. }
  323. static int
  324. i8250baud(Uart* uart, int baud)
  325. {
  326. ulong bgc;
  327. Ctlr *ctlr;
  328. /*
  329. * Set the Baud rate by calculating and setting the Baud rate
  330. * Generator Constant. This will work with fairly non-standard
  331. * Baud rates.
  332. */
  333. if(uart->freq == 0 || baud <= 0)
  334. return -1;
  335. bgc = (uart->freq+8*baud-1)/(16*baud);
  336. ctlr = uart->regs;
  337. csr8w(ctlr, Lcr, Dlab);
  338. outb(ctlr->io+Dlm, bgc>>8);
  339. outb(ctlr->io+Dll, bgc);
  340. csr8w(ctlr, Lcr, 0);
  341. uart->baud = baud;
  342. return 0;
  343. }
  344. static void
  345. i8250break(Uart* uart, int ms)
  346. {
  347. Ctlr *ctlr;
  348. /*
  349. * Send a break.
  350. */
  351. if(ms == 0)
  352. ms = 200;
  353. ctlr = uart->regs;
  354. csr8w(ctlr, Lcr, Brk);
  355. tsleep(&up->sleep, return0, 0, ms);
  356. csr8w(ctlr, Lcr, 0);
  357. }
  358. static void
  359. i8250kick(Uart* uart)
  360. {
  361. int i;
  362. Ctlr *ctlr;
  363. if(uart->cts == 0 || uart->blocked)
  364. return;
  365. /*
  366. * 128 here is an arbitrary limit to make sure
  367. * we don't stay in this loop too long. If the
  368. * chip's output queue is longer than 128, too
  369. * bad -- presotto
  370. */
  371. ctlr = uart->regs;
  372. for(i = 0; i < 128; i++){
  373. if(!(csr8r(ctlr, Lsr) & Thre))
  374. break;
  375. if(uart->op >= uart->oe && uartstageoutput(uart) == 0)
  376. break;
  377. outb(ctlr->io+Thr, *(uart->op++));
  378. }
  379. }
  380. static void
  381. i8250interrupt(Ureg*, void* arg)
  382. {
  383. Ctlr *ctlr;
  384. Uart *uart;
  385. int iir, lsr, old, r;
  386. uart = arg;
  387. ctlr = uart->regs;
  388. for(iir = csr8r(ctlr, Iir); !(iir & Ip); iir = csr8r(ctlr, Iir)){
  389. switch(iir & IirMASK){
  390. case Ims: /* Ms interrupt */
  391. r = csr8r(ctlr, Msr);
  392. if(r & Dcts){
  393. ilock(&uart->tlock);
  394. old = uart->cts;
  395. uart->cts = r & Cts;
  396. if(old == 0 && uart->cts)
  397. uart->ctsbackoff = 2;
  398. iunlock(&uart->tlock);
  399. }
  400. if(r & Ddsr){
  401. old = r & Dsr;
  402. if(uart->hup_dsr && uart->dsr && !old)
  403. uart->dohup = 1;
  404. uart->dsr = old;
  405. }
  406. if(r & Ddcd){
  407. old = r & Dcd;
  408. if(uart->hup_dcd && uart->dcd && !old)
  409. uart->dohup = 1;
  410. uart->dcd = old;
  411. }
  412. break;
  413. case Ithre: /* Thr Empty */
  414. uartkick(uart);
  415. break;
  416. case Irda: /* Received Data Available */
  417. case Ictoi: /* Character Time-out Indication */
  418. /*
  419. * Consume any received data.
  420. * If the received byte came in with a break,
  421. * parity or framing error, throw it away;
  422. * overrun is an indication that something has
  423. * already been tossed.
  424. */
  425. while((lsr = csr8r(ctlr, Lsr)) & Dr){
  426. if(lsr & Oe)
  427. uart->oerr++;
  428. if(lsr & Pe)
  429. uart->perr++;
  430. if(lsr & Fe)
  431. uart->ferr++;
  432. r = csr8r(ctlr, Rbr);
  433. if(!(lsr & (Bi|Fe|Pe)))
  434. uartrecv(uart, r);
  435. }
  436. break;
  437. default:
  438. iprint("weird uart interrupt 0x%2.2uX\n", iir);
  439. break;
  440. }
  441. }
  442. }
  443. static void
  444. i8250disable(Uart* uart)
  445. {
  446. Ctlr *ctlr;
  447. /*
  448. * Turn off DTR and RTS, disable interrupts and fifos.
  449. */
  450. (*uart->phys->dtr)(uart, 0);
  451. (*uart->phys->rts)(uart, 0);
  452. (*uart->phys->fifo)(uart, 0);
  453. ctlr = uart->regs;
  454. ctlr->sticky[Ier] = 0;
  455. csr8w(ctlr, Ier, ctlr->sticky[Ier]);
  456. if(ctlr->iena != 0){
  457. if(intrdisable(ctlr->irq, i8250interrupt, uart, ctlr->tbdf, uart->name) == 0)
  458. ctlr->iena = 0;
  459. }
  460. }
  461. static void
  462. i8250enable(Uart* uart, int ie)
  463. {
  464. Ctlr *ctlr;
  465. /*
  466. * Enable interrupts and turn on DTR and RTS.
  467. * Be careful if this is called to set up a polled serial line
  468. * early on not to try to enable interrupts as interrupt-
  469. * -enabling mechanisms might not be set up yet.
  470. */
  471. ctlr = uart->regs;
  472. if(ie){
  473. if(ctlr->iena == 0){
  474. intrenable(ctlr->irq, i8250interrupt, uart, ctlr->tbdf, uart->name);
  475. ctlr->iena = 1;
  476. }
  477. ctlr->sticky[Ier] = Ethre|Erda;
  478. ctlr->sticky[Mcr] |= Ie;
  479. }
  480. else{
  481. ctlr->sticky[Ier] = 0;
  482. ctlr->sticky[Mcr] = 0;
  483. }
  484. csr8w(ctlr, Ier, ctlr->sticky[Ier]);
  485. csr8w(ctlr, Mcr, ctlr->sticky[Mcr]);
  486. (*uart->phys->dtr)(uart, 1);
  487. (*uart->phys->rts)(uart, 1);
  488. /*
  489. * During startup, the i8259 interrupt controller is reset.
  490. * This may result in a lost interrupt from the i8250 uart.
  491. * The i8250 thinks the interrupt is still outstanding and does not
  492. * generate any further interrupts. The workaround is to call the
  493. * interrupt handler to clear any pending interrupt events.
  494. * Note: this must be done after setting Ier.
  495. */
  496. if(ie)
  497. i8250interrupt(nil, uart);
  498. }
  499. void*
  500. i8250alloc(int io, int irq, int tbdf)
  501. {
  502. Ctlr *ctlr;
  503. if((ctlr = malloc(sizeof(Ctlr))) != nil){
  504. ctlr->io = io;
  505. ctlr->irq = irq;
  506. ctlr->tbdf = tbdf;
  507. }
  508. return ctlr;
  509. }
  510. static Uart*
  511. i8250pnp(void)
  512. {
  513. return i8250uart;
  514. }
  515. static int
  516. i8250getc(Uart *uart)
  517. {
  518. Ctlr *ctlr;
  519. ctlr = uart->regs;
  520. while(!(csr8r(ctlr, Lsr)&Dr))
  521. delay(1);
  522. return csr8r(ctlr, Rbr);
  523. }
  524. static void
  525. i8250putc(Uart *uart, int c)
  526. {
  527. int i;
  528. Ctlr *ctlr;
  529. ctlr = uart->regs;
  530. for(i = 0; !(csr8r(ctlr, Lsr)&Thre) && i < 128; i++)
  531. delay(1);
  532. outb(ctlr->io+Thr, c);
  533. for(i = 0; !(csr8r(ctlr, Lsr)&Thre) && i < 128; i++)
  534. delay(1);
  535. }
  536. PhysUart i8250physuart = {
  537. .name = "i8250",
  538. .pnp = i8250pnp,
  539. .enable = i8250enable,
  540. .disable = i8250disable,
  541. .kick = i8250kick,
  542. .dobreak = i8250break,
  543. .baud = i8250baud,
  544. .bits = i8250bits,
  545. .stop = i8250stop,
  546. .parity = i8250parity,
  547. .modemctl = i8250modemctl,
  548. .rts = i8250rts,
  549. .dtr = i8250dtr,
  550. .status = i8250status,
  551. .fifo = i8250fifo,
  552. .getc = i8250getc,
  553. .putc = i8250putc,
  554. };
  555. void
  556. i8250console(void)
  557. {
  558. Uart *uart;
  559. int n;
  560. char *cmd, *p;
  561. if((p = getconf("console")) == nil)
  562. return;
  563. n = strtoul(p, &cmd, 0);
  564. if(p == cmd)
  565. return;
  566. switch(n){
  567. default:
  568. return;
  569. case 0:
  570. uart = &i8250uart[0];
  571. break;
  572. case 1:
  573. uart = &i8250uart[1];
  574. break;
  575. }
  576. uartctl(uart, "b9600 l8 pn s1");
  577. if(*cmd != '\0')
  578. uartctl(uart, cmd);
  579. (*uart->phys->enable)(uart, 0);
  580. consuart = uart;
  581. uart->console = 1;
  582. }
  583. void
  584. i8250mouse(char* which, int (*putc)(Queue*, int), int setb1200)
  585. {
  586. char *p;
  587. int port;
  588. port = strtol(which, &p, 0);
  589. if(p == which || port < 0 || port > 1)
  590. error(Ebadarg);
  591. uartmouse(&i8250uart[port], putc, setb1200);
  592. }
  593. void
  594. i8250setmouseputc(char* which, int (*putc)(Queue*, int))
  595. {
  596. char *p;
  597. int port;
  598. port = strtol(which, &p, 0);
  599. if(p == which || port < 0 || port > 1)
  600. error(Ebadarg);
  601. uartsetmouseputc(&i8250uart[port], putc);
  602. }