uarti8250.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*
  2. * omap35 8250-like UART
  3. *
  4. * we ignore the first 2 uarts on the omap35 (see below) and use the
  5. * third one but call it 0.
  6. */
  7. #include "u.h"
  8. #include "../port/lib.h"
  9. #include "mem.h"
  10. #include "dat.h"
  11. #include "fns.h"
  12. enum { /* registers */
  13. Rbr = 0, /* Receiver Buffer (RO) */
  14. Thr = 0, /* Transmitter Holding (WO) */
  15. Ier = 1, /* Interrupt Enable */
  16. Iir = 2, /* Interrupt Identification (RO) */
  17. Fcr = 2, /* FIFO Control (WO) */
  18. Lcr = 3, /* Line Control */
  19. Mcr = 4, /* Modem Control */
  20. Lsr = 5, /* Line Status */
  21. Msr = 6, /* Modem Status */
  22. Scr = 7, /* Scratch Pad */
  23. Mdr = 8, /* Mode Def'n (omap rw) */
  24. // Usr = 31, /* Uart Status Register; missing in omap? */
  25. Dll = 0, /* Divisor Latch LSB */
  26. Dlm = 1, /* Divisor Latch MSB */
  27. };
  28. enum { /* Usr */
  29. Busy = 0x01,
  30. };
  31. enum { /* Ier */
  32. Erda = 0x01, /* Enable Received Data Available */
  33. Ethre = 0x02, /* Enable Thr Empty */
  34. Erls = 0x04, /* Enable Receiver Line Status */
  35. Ems = 0x08, /* Enable Modem Status */
  36. };
  37. enum { /* Iir */
  38. Ims = 0x00, /* Ms interrupt */
  39. Ip = 0x01, /* Interrupt Pending (not) */
  40. Ithre = 0x02, /* Thr Empty */
  41. Irda = 0x04, /* Received Data Available */
  42. Irls = 0x06, /* Receiver Line Status */
  43. Ictoi = 0x0C, /* Character Time-out Indication */
  44. IirMASK = 0x3F,
  45. Ifena = 0xC0, /* FIFOs enabled */
  46. };
  47. enum { /* Fcr */
  48. FIFOena = 0x01, /* FIFO enable */
  49. FIFOrclr = 0x02, /* clear Rx FIFO */
  50. FIFOtclr = 0x04, /* clear Tx FIFO */
  51. // FIFOdma = 0x08,
  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 (cd_sts_ch on omap) */
  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, /* Transmitter 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, /* Carrier Detect */
  96. };
  97. enum { /* Mdr */
  98. Modemask = 7,
  99. Modeuart = 0,
  100. };
  101. typedef struct Ctlr {
  102. u32int* io;
  103. int irq;
  104. int tbdf;
  105. int iena;
  106. int poll;
  107. uchar sticky[Scr+1];
  108. Lock;
  109. int hasfifo;
  110. int checkfifo;
  111. int fena;
  112. } Ctlr;
  113. extern PhysUart i8250physuart;
  114. static Ctlr i8250ctlr[] = {
  115. { .io = (u32int*)PHYSCONS, /* UART3 in TI terminology */
  116. .irq = 74,
  117. .tbdf = -1,
  118. .poll = 0, },
  119. /* these exist, but I don't think they're connected to anything external */
  120. //{ .io = (u32int*)PHYSUART0,
  121. // .irq = 72,
  122. // .tbdf = -1,
  123. // .poll = 0, },
  124. //
  125. //{ .io = (u32int*)PHYSUART1,
  126. // .irq = 73,
  127. // .tbdf = -1,
  128. // .poll = 0, },
  129. };
  130. static Uart i8250uart[] = {
  131. { .regs = &i8250ctlr[0], /* not [2] */
  132. .name = "COM3",
  133. .freq = 3686000, /* Not used, we use the global i8250freq */
  134. .phys = &i8250physuart,
  135. .console= 1,
  136. .next = nil, },
  137. /* these exist, but I don't think they're connected to anything external */
  138. //{ .regs = &i8250ctlr[0],
  139. // .name = "COM1",
  140. // .freq = 3686000, /* Not used, we use the global i8250freq */
  141. // .phys = &i8250physuart,
  142. // .special= 0,
  143. // .next = &i8250uart[1], },
  144. //
  145. //{ .regs = &i8250ctlr[1],
  146. // .name = "COM2",
  147. // .freq = 3686000, /* Not used, we use the global i8250freq */
  148. // .phys = &i8250physuart,
  149. // .special= 0,
  150. // .next = &i8250uart[2], },
  151. };
  152. #define csr8r(c, r) ((c)->io[r])
  153. #define csr8w(c, r, v) ((c)->io[r] = (c)->sticky[r] | (v), coherence())
  154. #define csr8o(c, r, v) ((c)->io[r] = (v), coherence())
  155. static long
  156. i8250status(Uart* uart, void* buf, long n, long offset)
  157. {
  158. char *p;
  159. Ctlr *ctlr;
  160. uchar ier, lcr, mcr, msr;
  161. ctlr = uart->regs;
  162. p = malloc(READSTR);
  163. mcr = ctlr->sticky[Mcr];
  164. msr = csr8r(ctlr, Msr);
  165. ier = ctlr->sticky[Ier];
  166. lcr = ctlr->sticky[Lcr];
  167. snprint(p, READSTR,
  168. "b%d c%d d%d e%d l%d m%d p%c r%d s%d i%d\n"
  169. "dev(%d) type(%d) framing(%d) overruns(%d) "
  170. "berr(%d) serr(%d)%s%s%s%s\n",
  171. uart->baud,
  172. uart->hup_dcd,
  173. (msr & Dsr) != 0,
  174. uart->hup_dsr,
  175. (lcr & WlsMASK) + 5,
  176. (ier & Ems) != 0,
  177. (lcr & Pen) ? ((lcr & Eps) ? 'e': 'o'): 'n',
  178. (mcr & Rts) != 0,
  179. (lcr & Stb) ? 2: 1,
  180. ctlr->fena,
  181. uart->dev,
  182. uart->type,
  183. uart->ferr,
  184. uart->oerr,
  185. uart->berr,
  186. uart->serr,
  187. (msr & Cts) ? " cts": "",
  188. (msr & Dsr) ? " dsr": "",
  189. (msr & Dcd) ? " dcd": "",
  190. (msr & Ri) ? " ring": ""
  191. );
  192. n = readstr(offset, buf, n, p);
  193. free(p);
  194. return n;
  195. }
  196. static void
  197. i8250fifo(Uart* uart, int level)
  198. {
  199. Ctlr *ctlr;
  200. ctlr = uart->regs;
  201. if(ctlr->hasfifo == 0)
  202. return;
  203. /*
  204. * Changing the FIFOena bit in Fcr flushes data
  205. * from both receive and transmit FIFOs; there's
  206. * no easy way to guarantee not losing data on
  207. * the receive side, but it's possible to wait until
  208. * the transmitter is really empty.
  209. */
  210. ilock(ctlr);
  211. while(!(csr8r(ctlr, Lsr) & Temt))
  212. ;
  213. /*
  214. * Set the trigger level, default is the max.
  215. * value.
  216. * Some UARTs require FIFOena to be set before
  217. * other bits can take effect, so set it twice.
  218. */
  219. ctlr->fena = level;
  220. switch(level){
  221. case 0:
  222. break;
  223. case 1:
  224. level = FIFO1|FIFOena;
  225. break;
  226. case 4:
  227. level = FIFO4|FIFOena;
  228. break;
  229. case 8:
  230. level = FIFO8|FIFOena;
  231. break;
  232. default:
  233. level = FIFO14|FIFOena;
  234. break;
  235. }
  236. csr8w(ctlr, Fcr, level);
  237. csr8w(ctlr, Fcr, level);
  238. iunlock(ctlr);
  239. }
  240. static void
  241. i8250dtr(Uart* uart, int on)
  242. {
  243. Ctlr *ctlr;
  244. /*
  245. * Toggle DTR.
  246. */
  247. ctlr = uart->regs;
  248. if(on)
  249. ctlr->sticky[Mcr] |= Dtr;
  250. else
  251. ctlr->sticky[Mcr] &= ~Dtr;
  252. csr8w(ctlr, Mcr, 0);
  253. }
  254. static void
  255. i8250rts(Uart* uart, int on)
  256. {
  257. Ctlr *ctlr;
  258. /*
  259. * Toggle RTS.
  260. */
  261. ctlr = uart->regs;
  262. if(on)
  263. ctlr->sticky[Mcr] |= Rts;
  264. else
  265. ctlr->sticky[Mcr] &= ~Rts;
  266. csr8w(ctlr, Mcr, 0);
  267. }
  268. static void
  269. i8250modemctl(Uart* uart, int on)
  270. {
  271. Ctlr *ctlr;
  272. ctlr = uart->regs;
  273. ilock(&uart->tlock);
  274. if(on){
  275. ctlr->sticky[Ier] |= Ems;
  276. csr8w(ctlr, Ier, 0);
  277. uart->modem = 1;
  278. uart->cts = csr8r(ctlr, Msr) & Cts;
  279. }
  280. else{
  281. ctlr->sticky[Ier] &= ~Ems;
  282. csr8w(ctlr, Ier, 0);
  283. uart->modem = 0;
  284. uart->cts = 1;
  285. }
  286. iunlock(&uart->tlock);
  287. /* modem needs fifo */
  288. (*uart->phys->fifo)(uart, on);
  289. }
  290. static int
  291. i8250parity(Uart* uart, int parity)
  292. {
  293. int lcr;
  294. Ctlr *ctlr;
  295. ctlr = uart->regs;
  296. lcr = ctlr->sticky[Lcr] & ~(Eps|Pen);
  297. switch(parity){
  298. case 'e':
  299. lcr |= Eps|Pen;
  300. break;
  301. case 'o':
  302. lcr |= Pen;
  303. break;
  304. case 'n':
  305. break;
  306. default:
  307. return -1;
  308. }
  309. ctlr->sticky[Lcr] = lcr;
  310. csr8w(ctlr, Lcr, 0);
  311. uart->parity = parity;
  312. return 0;
  313. }
  314. static int
  315. i8250stop(Uart* uart, int stop)
  316. {
  317. int lcr;
  318. Ctlr *ctlr;
  319. ctlr = uart->regs;
  320. lcr = ctlr->sticky[Lcr] & ~Stb;
  321. switch(stop){
  322. case 1:
  323. break;
  324. case 2:
  325. lcr |= Stb;
  326. break;
  327. default:
  328. return -1;
  329. }
  330. ctlr->sticky[Lcr] = lcr;
  331. csr8w(ctlr, Lcr, 0);
  332. uart->stop = stop;
  333. return 0;
  334. }
  335. static int
  336. i8250bits(Uart* uart, int bits)
  337. {
  338. int lcr;
  339. Ctlr *ctlr;
  340. ctlr = uart->regs;
  341. lcr = ctlr->sticky[Lcr] & ~WlsMASK;
  342. switch(bits){
  343. case 5:
  344. lcr |= Wls5;
  345. break;
  346. case 6:
  347. lcr |= Wls6;
  348. break;
  349. case 7:
  350. lcr |= Wls7;
  351. break;
  352. case 8:
  353. lcr |= Wls8;
  354. break;
  355. default:
  356. return -1;
  357. }
  358. ctlr->sticky[Lcr] = lcr;
  359. csr8w(ctlr, Lcr, 0);
  360. uart->bits = bits;
  361. return 0;
  362. }
  363. static int
  364. i8250baud(Uart* uart, int baud)
  365. {
  366. #ifdef notdef /* don't change the speed */
  367. ulong bgc;
  368. Ctlr *ctlr;
  369. extern int i8250freq; /* In the config file */
  370. /*
  371. * Set the Baud rate by calculating and setting the Baud rate
  372. * Generator Constant. This will work with fairly non-standard
  373. * Baud rates.
  374. */
  375. if(i8250freq == 0 || baud <= 0)
  376. return -1;
  377. bgc = (i8250freq+8*baud-1)/(16*baud);
  378. ctlr = uart->regs;
  379. while(csr8r(ctlr, Usr) & Busy)
  380. delay(1);
  381. csr8w(ctlr, Lcr, Dlab); /* begin kludge */
  382. csr8o(ctlr, Dlm, bgc>>8);
  383. csr8o(ctlr, Dll, bgc);
  384. csr8w(ctlr, Lcr, 0);
  385. #endif
  386. uart->baud = baud;
  387. return 0;
  388. }
  389. static void
  390. i8250break(Uart* uart, int ms)
  391. {
  392. Ctlr *ctlr;
  393. if (up == nil)
  394. panic("i8250break: nil up");
  395. /*
  396. * Send a break.
  397. */
  398. if(ms <= 0)
  399. ms = 200;
  400. ctlr = uart->regs;
  401. csr8w(ctlr, Lcr, Brk);
  402. tsleep(&up->sleep, return0, 0, ms);
  403. csr8w(ctlr, Lcr, 0);
  404. }
  405. static void
  406. emptyoutstage(Uart *uart, int n)
  407. {
  408. _uartputs((char *)uart->op, n);
  409. uart->op = uart->oe = uart->ostage;
  410. }
  411. static void
  412. i8250kick(Uart* uart)
  413. {
  414. int i;
  415. Ctlr *ctlr;
  416. if(/* uart->cts == 0 || */ uart->blocked)
  417. return;
  418. if(!normalprint) { /* early */
  419. if (uart->op < uart->oe)
  420. emptyoutstage(uart, uart->oe - uart->op);
  421. while ((i = uartstageoutput(uart)) > 0)
  422. emptyoutstage(uart, i);
  423. return;
  424. }
  425. /* nothing more to send? then disable xmit intr */
  426. ctlr = uart->regs;
  427. if (uart->op >= uart->oe && qlen(uart->oq) == 0 &&
  428. csr8r(ctlr, Lsr) & Temt) {
  429. ctlr->sticky[Ier] &= ~Ethre;
  430. csr8w(ctlr, Ier, 0);
  431. return;
  432. }
  433. /*
  434. * 128 here is an arbitrary limit to make sure
  435. * we don't stay in this loop too long. If the
  436. * chip's output queue is longer than 128, too
  437. * bad -- presotto
  438. */
  439. for(i = 0; i < 128; i++){
  440. if(!(csr8r(ctlr, Lsr) & Thre))
  441. break;
  442. if(uart->op >= uart->oe && uartstageoutput(uart) == 0)
  443. break;
  444. csr8o(ctlr, Thr, *uart->op++); /* start tx */
  445. ctlr->sticky[Ier] |= Ethre;
  446. csr8w(ctlr, Ier, 0); /* intr when done */
  447. }
  448. }
  449. void
  450. serialkick(void)
  451. {
  452. uartkick(&i8250uart[CONSOLE]);
  453. }
  454. static void
  455. i8250interrupt(Ureg*, void* arg)
  456. {
  457. Ctlr *ctlr;
  458. Uart *uart;
  459. int iir, lsr, old, r;
  460. uart = arg;
  461. ctlr = uart->regs;
  462. for(iir = csr8r(ctlr, Iir); !(iir & Ip); iir = csr8r(ctlr, Iir)){
  463. switch(iir & IirMASK){
  464. case Ims: /* Ms interrupt */
  465. r = csr8r(ctlr, Msr);
  466. if(r & Dcts){
  467. ilock(&uart->tlock);
  468. old = uart->cts;
  469. uart->cts = r & Cts;
  470. if(old == 0 && uart->cts)
  471. uart->ctsbackoff = 2;
  472. iunlock(&uart->tlock);
  473. }
  474. if(r & Ddsr){
  475. old = r & Dsr;
  476. if(uart->hup_dsr && uart->dsr && !old)
  477. uart->dohup = 1;
  478. uart->dsr = old;
  479. }
  480. if(r & Ddcd){
  481. old = r & Dcd;
  482. if(uart->hup_dcd && uart->dcd && !old)
  483. uart->dohup = 1;
  484. uart->dcd = old;
  485. }
  486. break;
  487. case Ithre: /* Thr Empty */
  488. uartkick(uart);
  489. break;
  490. case Irda: /* Received Data Available */
  491. case Irls: /* Receiver Line Status */
  492. case Ictoi: /* Character Time-out Indication */
  493. /*
  494. * Consume any received data.
  495. * If the received byte came in with a break,
  496. * parity or framing error, throw it away;
  497. * overrun is an indication that something has
  498. * already been tossed.
  499. */
  500. while((lsr = csr8r(ctlr, Lsr)) & Dr){
  501. if(lsr & (FIFOerr|Oe))
  502. uart->oerr++;
  503. if(lsr & Pe)
  504. uart->perr++;
  505. if(lsr & Fe)
  506. uart->ferr++;
  507. r = csr8r(ctlr, Rbr);
  508. if(!(lsr & (Bi|Fe|Pe)))
  509. uartrecv(uart, r);
  510. }
  511. break;
  512. default:
  513. iprint("weird uart interrupt type %#2.2uX\n", iir);
  514. break;
  515. }
  516. }
  517. }
  518. static void
  519. i8250disable(Uart* uart)
  520. {
  521. Ctlr *ctlr;
  522. /*
  523. * Turn off DTR and RTS, disable interrupts and fifos.
  524. */
  525. (*uart->phys->dtr)(uart, 0);
  526. (*uart->phys->rts)(uart, 0);
  527. (*uart->phys->fifo)(uart, 0);
  528. ctlr = uart->regs;
  529. ctlr->sticky[Ier] = 0;
  530. csr8w(ctlr, Ier, 0);
  531. if(ctlr->iena != 0){
  532. if(irqdisable(ctlr->irq, i8250interrupt, uart, uart->name) == 0)
  533. ctlr->iena = 0;
  534. }
  535. }
  536. static void
  537. i8250enable(Uart* uart, int ie)
  538. {
  539. int mode;
  540. Ctlr *ctlr;
  541. if (up == nil)
  542. return; /* too soon */
  543. ctlr = uart->regs;
  544. /* omap only: set uart/irda/cir mode to uart */
  545. mode = csr8r(ctlr, Mdr);
  546. csr8o(ctlr, Mdr, (mode & ~Modemask) | Modeuart);
  547. ctlr->sticky[Lcr] = Wls8; /* no parity */
  548. csr8w(ctlr, Lcr, 0);
  549. /*
  550. * Check if there is a FIFO.
  551. * Changing the FIFOena bit in Fcr flushes data
  552. * from both receive and transmit FIFOs; there's
  553. * no easy way to guarantee not losing data on
  554. * the receive side, but it's possible to wait until
  555. * the transmitter is really empty.
  556. * Also, reading the Iir outwith i8250interrupt()
  557. * can be dangerous, but this should only happen
  558. * once, before interrupts are enabled.
  559. */
  560. ilock(ctlr);
  561. if(!ctlr->checkfifo){
  562. /*
  563. * Wait until the transmitter is really empty.
  564. */
  565. while(!(csr8r(ctlr, Lsr) & Temt))
  566. ;
  567. csr8w(ctlr, Fcr, FIFOena);
  568. if(csr8r(ctlr, Iir) & Ifena)
  569. ctlr->hasfifo = 1;
  570. csr8w(ctlr, Fcr, 0);
  571. ctlr->checkfifo = 1;
  572. }
  573. iunlock(ctlr);
  574. /*
  575. * Enable interrupts and turn on DTR and RTS.
  576. * Be careful if this is called to set up a polled serial line
  577. * early on not to try to enable interrupts as interrupt-
  578. * -enabling mechanisms might not be set up yet.
  579. */
  580. if(ie){
  581. if(ctlr->iena == 0 && !ctlr->poll){
  582. irqenable(ctlr->irq, i8250interrupt, uart, uart->name);
  583. ctlr->iena = 1;
  584. }
  585. ctlr->sticky[Ier] = Erda;
  586. // ctlr->sticky[Mcr] |= Ie; /* not on omap */
  587. ctlr->sticky[Mcr] = 0;
  588. }
  589. else{
  590. ctlr->sticky[Ier] = 0;
  591. ctlr->sticky[Mcr] = 0;
  592. }
  593. csr8w(ctlr, Ier, 0);
  594. csr8w(ctlr, Mcr, 0);
  595. (*uart->phys->dtr)(uart, 1);
  596. (*uart->phys->rts)(uart, 1);
  597. /*
  598. * During startup, the i8259 interrupt controller is reset.
  599. * This may result in a lost interrupt from the i8250 uart.
  600. * The i8250 thinks the interrupt is still outstanding and does not
  601. * generate any further interrupts. The workaround is to call the
  602. * interrupt handler to clear any pending interrupt events.
  603. * Note: this must be done after setting Ier.
  604. */
  605. if(ie)
  606. i8250interrupt(nil, uart);
  607. }
  608. static Uart*
  609. i8250pnp(void)
  610. {
  611. return i8250uart;
  612. }
  613. static int
  614. i8250getc(Uart* uart)
  615. {
  616. Ctlr *ctlr;
  617. ctlr = uart->regs;
  618. while(!(csr8r(ctlr, Lsr) & Dr))
  619. delay(1);
  620. return csr8r(ctlr, Rbr);
  621. }
  622. static void
  623. i8250putc(Uart* uart, int c)
  624. {
  625. int i;
  626. Ctlr *ctlr;
  627. if (!normalprint) { /* too early; use brute force */
  628. int s = splhi();
  629. while (!(((ulong *)PHYSCONS)[Lsr] & Thre))
  630. ;
  631. ((ulong *)PHYSCONS)[Thr] = c;
  632. coherence();
  633. splx(s);
  634. return;
  635. }
  636. ctlr = uart->regs;
  637. for(i = 0; !(csr8r(ctlr, Lsr) & Thre) && i < 128; i++)
  638. delay(1);
  639. csr8o(ctlr, Thr, (uchar)c);
  640. for(i = 0; !(csr8r(ctlr, Lsr) & Thre) && i < 128; i++)
  641. delay(1);
  642. }
  643. void
  644. serialputc(int c)
  645. {
  646. i8250putc(&i8250uart[CONSOLE], c);
  647. }
  648. void
  649. serialputs(char* s, int n)
  650. {
  651. _uartputs(s, n);
  652. }
  653. #ifdef notdef
  654. static void
  655. i8250poll(Uart* uart)
  656. {
  657. Ctlr *ctlr;
  658. /*
  659. * If PhysUart has a non-nil .poll member, this
  660. * routine will be called from the uartclock timer.
  661. * If the Ctlr .poll member is non-zero, when the
  662. * Uart is enabled interrupts will not be enabled
  663. * and the result is polled input and output.
  664. * Not very useful here, but ports to new hardware
  665. * or simulators can use this to get serial I/O
  666. * without setting up the interrupt mechanism.
  667. */
  668. ctlr = uart->regs;
  669. if(ctlr->iena || !ctlr->poll)
  670. return;
  671. i8250interrupt(nil, uart);
  672. }
  673. #endif
  674. PhysUart i8250physuart = {
  675. .name = "i8250",
  676. .pnp = i8250pnp,
  677. .enable = i8250enable,
  678. .disable = i8250disable,
  679. .kick = i8250kick,
  680. .dobreak = i8250break,
  681. .baud = i8250baud,
  682. .bits = i8250bits,
  683. .stop = i8250stop,
  684. .parity = i8250parity,
  685. .modemctl = i8250modemctl,
  686. .rts = i8250rts,
  687. .dtr = i8250dtr,
  688. .status = i8250status,
  689. .fifo = i8250fifo,
  690. .getc = i8250getc,
  691. .putc = i8250putc,
  692. // .poll = i8250poll, /* only in 9k, not 9 */
  693. };
  694. static void
  695. i8250dumpregs(Ctlr* ctlr)
  696. {
  697. int dlm, dll;
  698. int _uartprint(char*, ...);
  699. csr8w(ctlr, Lcr, Dlab);
  700. dlm = csr8r(ctlr, Dlm);
  701. dll = csr8r(ctlr, Dll);
  702. csr8w(ctlr, Lcr, 0);
  703. _uartprint("dlm %#ux dll %#ux\n", dlm, dll);
  704. }
  705. Uart* uartenable(Uart *p);
  706. /* must call this from a process's context */
  707. int
  708. i8250console(void)
  709. {
  710. Uart *uart = &i8250uart[CONSOLE];
  711. if (up == nil)
  712. return -1; /* too early */
  713. if(uartenable(uart) != nil /* && uart->console */){
  714. // iprint("i8250console: enabling console uart\n");
  715. kbdq = uart->iq;
  716. serialoq = uart->oq;
  717. uart->putc = kbdcr2nl;
  718. uart->opens++;
  719. consuart = uart;
  720. }
  721. uartctl(uart, "b115200 l8 pn r1 s1 i1");
  722. return 0;
  723. }
  724. void
  725. _uartputs(char* s, int n)
  726. {
  727. char *e;
  728. for(e = s+n; s < e; s++){
  729. if(*s == '\n')
  730. i8250putc(&i8250uart[CONSOLE], '\r');
  731. i8250putc(&i8250uart[CONSOLE], *s);
  732. }
  733. }
  734. int
  735. _uartprint(char* fmt, ...)
  736. {
  737. int n;
  738. va_list arg;
  739. char buf[PRINTSIZE];
  740. va_start(arg, fmt);
  741. n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
  742. va_end(arg);
  743. _uartputs(buf, n);
  744. return n;
  745. }