uarti8250.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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\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. uart->dev,
  158. uart->type,
  159. uart->ferr,
  160. uart->oerr,
  161. (msr & Cts) ? " cts": "",
  162. (msr & Dsr) ? " dsr": "",
  163. (msr & Dcd) ? " dcd": "",
  164. (msr & Ri) ? " ring": ""
  165. );
  166. n = readstr(offset, buf, n, p);
  167. free(p);
  168. return n;
  169. }
  170. static void
  171. i8250fifo(Uart* uart, int on)
  172. {
  173. int i;
  174. Ctlr *ctlr;
  175. /*
  176. * Toggle FIFOs:
  177. * if none, do nothing;
  178. * reset the Rx and Tx FIFOs;
  179. * empty the Rx buffer and clear any interrupt conditions;
  180. * if enabling, try to turn them on.
  181. */
  182. ctlr = uart->regs;
  183. ilock(ctlr);
  184. if(!ctlr->fifo){
  185. csr8w(ctlr, Fcr, FIFOtclr|FIFOrclr);
  186. for(i = 0; i < 16; i++){
  187. csr8r(ctlr, Iir);
  188. csr8r(ctlr, Rbr);
  189. }
  190. ctlr->fena = 0;
  191. if(on){
  192. csr8w(ctlr, Fcr, FIFO4|FIFOena);
  193. if(!(csr8r(ctlr, Iir) & Ife))
  194. ctlr->fifo = 1;
  195. ctlr->fena = 1;
  196. }
  197. }
  198. iunlock(ctlr);
  199. }
  200. static void
  201. i8250dtr(Uart* uart, int on)
  202. {
  203. Ctlr *ctlr;
  204. /*
  205. * Toggle DTR.
  206. */
  207. ctlr = uart->regs;
  208. if(on)
  209. ctlr->sticky[Mcr] |= Dtr;
  210. else
  211. ctlr->sticky[Mcr] &= ~Dtr;
  212. csr8w(ctlr, Mcr, 0);
  213. }
  214. static void
  215. i8250rts(Uart* uart, int on)
  216. {
  217. Ctlr *ctlr;
  218. /*
  219. * Toggle RTS.
  220. */
  221. ctlr = uart->regs;
  222. if(on)
  223. ctlr->sticky[Mcr] |= Rts;
  224. else
  225. ctlr->sticky[Mcr] &= ~Rts;
  226. csr8w(ctlr, Mcr, 0);
  227. }
  228. static void
  229. i8250modemctl(Uart* uart, int on)
  230. {
  231. Ctlr *ctlr;
  232. ctlr = uart->regs;
  233. ilock(&uart->tlock);
  234. if(on){
  235. ctlr->sticky[Ier] |= Ems;
  236. csr8w(ctlr, Ier, 0);
  237. uart->modem = 1;
  238. uart->cts = csr8r(ctlr, Msr) & Cts;
  239. }
  240. else{
  241. ctlr->sticky[Ier] &= ~Ems;
  242. csr8w(ctlr, Ier, 0);
  243. uart->modem = 0;
  244. uart->cts = 1;
  245. }
  246. iunlock(&uart->tlock);
  247. /* modem needs fifo */
  248. (*uart->phys->fifo)(uart, on);
  249. }
  250. static int
  251. i8250parity(Uart* uart, int parity)
  252. {
  253. int lcr;
  254. Ctlr *ctlr;
  255. ctlr = uart->regs;
  256. lcr = ctlr->sticky[Lcr] & ~(Eps|Pen);
  257. switch(parity){
  258. case 'e':
  259. lcr |= Eps|Pen;
  260. break;
  261. case 'o':
  262. lcr |= Pen;
  263. break;
  264. case 'n':
  265. default:
  266. break;
  267. }
  268. ctlr->sticky[Lcr] = lcr;
  269. csr8w(ctlr, Lcr, 0);
  270. uart->parity = parity;
  271. return 0;
  272. }
  273. static int
  274. i8250stop(Uart* uart, int stop)
  275. {
  276. int lcr;
  277. Ctlr *ctlr;
  278. ctlr = uart->regs;
  279. lcr = ctlr->sticky[Lcr] & ~Stb;
  280. switch(stop){
  281. case 1:
  282. break;
  283. case 2:
  284. lcr |= Stb;
  285. break;
  286. default:
  287. return -1;
  288. }
  289. ctlr->sticky[Lcr] = lcr;
  290. csr8w(ctlr, Lcr, 0);
  291. uart->stop = stop;
  292. return 0;
  293. }
  294. static int
  295. i8250bits(Uart* uart, int bits)
  296. {
  297. int lcr;
  298. Ctlr *ctlr;
  299. ctlr = uart->regs;
  300. lcr = ctlr->sticky[Lcr] & ~WlsMASK;
  301. switch(bits){
  302. case 5:
  303. lcr |= Wls5;
  304. break;
  305. case 6:
  306. lcr |= Wls6;
  307. break;
  308. case 7:
  309. lcr |= Wls7;
  310. break;
  311. case 8:
  312. lcr |= Wls8;
  313. break;
  314. default:
  315. return -1;
  316. }
  317. ctlr->sticky[Lcr] = lcr;
  318. csr8w(ctlr, Lcr, 0);
  319. uart->bits = bits;
  320. return 0;
  321. }
  322. static int
  323. i8250baud(Uart* uart, int baud)
  324. {
  325. ulong bgc;
  326. Ctlr *ctlr;
  327. /*
  328. * Set the Baud rate by calculating and setting the Baud rate
  329. * Generator Constant. This will work with fairly non-standard
  330. * Baud rates.
  331. */
  332. if(uart->freq == 0 || baud <= 0)
  333. return -1;
  334. bgc = (uart->freq+8*baud-1)/(16*baud);
  335. ctlr = uart->regs;
  336. csr8w(ctlr, Lcr, Dlab);
  337. outb(ctlr->io+Dlm, bgc>>8);
  338. outb(ctlr->io+Dll, bgc);
  339. csr8w(ctlr, Lcr, 0);
  340. uart->baud = baud;
  341. return 0;
  342. }
  343. static void
  344. i8250break(Uart* uart, int ms)
  345. {
  346. Ctlr *ctlr;
  347. /*
  348. * Send a break.
  349. */
  350. if(ms == 0)
  351. ms = 200;
  352. ctlr = uart->regs;
  353. csr8w(ctlr, Lcr, Brk);
  354. tsleep(&up->sleep, return0, 0, ms);
  355. csr8w(ctlr, Lcr, 0);
  356. }
  357. static void
  358. i8250kick(Uart* uart)
  359. {
  360. int i;
  361. Ctlr *ctlr;
  362. if(uart->cts == 0 || uart->blocked)
  363. return;
  364. /*
  365. * 128 here is an arbitrary limit to make sure
  366. * we don't stay in this loop too long. If the
  367. * chip's output queue is longer than 128, too
  368. * bad -- presotto
  369. */
  370. ctlr = uart->regs;
  371. for(i = 0; i < 128; i++){
  372. if(!(csr8r(ctlr, Lsr) & Thre))
  373. break;
  374. if(uart->op >= uart->oe && uartstageoutput(uart) == 0)
  375. break;
  376. outb(ctlr->io+Thr, *(uart->op++));
  377. }
  378. }
  379. static void
  380. i8250interrupt(Ureg*, void* arg)
  381. {
  382. Ctlr *ctlr;
  383. Uart *uart;
  384. int iir, lsr, old, r;
  385. uart = arg;
  386. ctlr = uart->regs;
  387. for(iir = csr8r(ctlr, Iir); !(iir & Ip); iir = csr8r(ctlr, Iir)){
  388. switch(iir & IirMASK){
  389. case Ims: /* Ms interrupt */
  390. r = csr8r(ctlr, Msr);
  391. if(r & Dcts){
  392. ilock(&uart->tlock);
  393. old = uart->cts;
  394. uart->cts = r & Cts;
  395. if(old == 0 && uart->cts)
  396. uart->ctsbackoff = 2;
  397. iunlock(&uart->tlock);
  398. }
  399. if(r & Ddsr){
  400. old = r & Dsr;
  401. if(uart->hup_dsr && uart->dsr && !old)
  402. uart->dohup = 1;
  403. uart->dsr = old;
  404. }
  405. if(r & Ddcd){
  406. old = r & Dcd;
  407. if(uart->hup_dcd && uart->dcd && !old)
  408. uart->dohup = 1;
  409. uart->dcd = old;
  410. }
  411. break;
  412. case Ithre: /* Thr Empty */
  413. uartkick(uart);
  414. break;
  415. case Irda: /* Received Data Available */
  416. case Ictoi: /* Character Time-out Indication */
  417. /*
  418. * Consume any received data.
  419. * If the received byte came in with a break,
  420. * parity or framing error, throw it away;
  421. * overrun is an indication that something has
  422. * already been tossed.
  423. */
  424. while((lsr = csr8r(ctlr, Lsr)) & Dr){
  425. if(lsr & Oe)
  426. uart->oerr++;
  427. if(lsr & Pe)
  428. uart->perr++;
  429. if(lsr & Fe)
  430. uart->ferr++;
  431. r = csr8r(ctlr, Rbr);
  432. if(!(lsr & (Bi|Fe|Pe)))
  433. uartrecv(uart, r);
  434. }
  435. break;
  436. default:
  437. iprint("weird uart interrupt 0x%2.2uX\n", iir);
  438. break;
  439. }
  440. }
  441. }
  442. static void
  443. i8250disable(Uart* uart)
  444. {
  445. Ctlr *ctlr;
  446. /*
  447. * Turn off DTR and RTS, disable interrupts and fifos.
  448. */
  449. (*uart->phys->dtr)(uart, 0);
  450. (*uart->phys->rts)(uart, 0);
  451. (*uart->phys->fifo)(uart, 0);
  452. ctlr = uart->regs;
  453. ctlr->sticky[Ier] = 0;
  454. csr8w(ctlr, Ier, 0);
  455. }
  456. static void
  457. i8250enable(Uart* uart, int ie)
  458. {
  459. Ctlr *ctlr;
  460. /*
  461. * Enable interrupts and turn on DTR and RTS.
  462. * Be careful if this is called to set up a polled serial line
  463. * early on not to try to enable interrupts as interrupt-
  464. * -enabling mechanisms might not be set up yet.
  465. */
  466. ctlr = uart->regs;
  467. if(ie){
  468. if(ctlr->iena == 0){
  469. intrenable(ctlr->irq, i8250interrupt, uart, ctlr->tbdf, uart->name);
  470. ctlr->iena = 1;
  471. }
  472. ctlr->sticky[Ier] = Ethre|Erda;
  473. ctlr->sticky[Mcr] |= Ie;
  474. }
  475. else{
  476. ctlr->sticky[Ier] = 0;
  477. ctlr->sticky[Mcr] = 0;
  478. }
  479. csr8w(ctlr, Ier, ctlr->sticky[Ier]);
  480. csr8w(ctlr, Mcr, ctlr->sticky[Mcr]);
  481. (*uart->phys->dtr)(uart, 1);
  482. (*uart->phys->rts)(uart, 1);
  483. }
  484. static Uart*
  485. i8250pnp(void)
  486. {
  487. return i8250uart;
  488. }
  489. static int
  490. i8250getc(Uart *uart)
  491. {
  492. Ctlr *ctlr;
  493. ctlr = uart->regs;
  494. while(!(csr8r(ctlr, Lsr)&Dr))
  495. delay(1);
  496. return csr8r(ctlr, Rbr);
  497. }
  498. static void
  499. i8250putc(Uart *uart, int c)
  500. {
  501. int i;
  502. Ctlr *ctlr;
  503. ctlr = uart->regs;
  504. for(i = 0; !(csr8r(ctlr, Lsr)&Thre) && i < 128; i++)
  505. delay(1);
  506. outb(ctlr->io+Thr, c);
  507. for(i = 0; !(csr8r(ctlr, Lsr)&Thre) && i < 128; i++)
  508. delay(1);
  509. }
  510. PhysUart i8250physuart = {
  511. .name = "i8250",
  512. .pnp = i8250pnp,
  513. .enable = i8250enable,
  514. .disable = i8250disable,
  515. .kick = i8250kick,
  516. .dobreak = i8250break,
  517. .baud = i8250baud,
  518. .bits = i8250bits,
  519. .stop = i8250stop,
  520. .parity = i8250parity,
  521. .modemctl = i8250modemctl,
  522. .rts = i8250rts,
  523. .dtr = i8250dtr,
  524. .status = i8250status,
  525. .fifo = i8250fifo,
  526. .getc = i8250getc,
  527. .putc = i8250putc,
  528. };
  529. void
  530. i8250console(void)
  531. {
  532. Uart *uart;
  533. int n;
  534. char *cmd, *p;
  535. if((p = getconf("console")) == nil)
  536. return;
  537. n = strtoul(p, &cmd, 0);
  538. if(p == cmd)
  539. return;
  540. switch(n){
  541. default:
  542. return;
  543. case 0:
  544. uart = &i8250uart[0];
  545. break;
  546. case 1:
  547. uart = &i8250uart[1];
  548. break;
  549. }
  550. uartctl(uart, "b9600 l8 pn s1");
  551. if(*cmd != '\0')
  552. uartctl(uart, cmd);
  553. (*uart->phys->enable)(uart, 0);
  554. consuart = uart;
  555. uart->console = 1;
  556. }