jsm_cls.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2003 Digi International (www.digi.com)
  4. * Scott H Kilau <Scott_Kilau at digi dot com>
  5. *
  6. * NOTE TO LINUX KERNEL HACKERS: DO NOT REFORMAT THIS CODE!
  7. *
  8. * This is shared code between Digi's CVS archive and the
  9. * Linux Kernel sources.
  10. * Changing the source just for reformatting needlessly breaks
  11. * our CVS diff history.
  12. *
  13. * Send any bug fixes/changes to: Eng.Linux at digi dot com.
  14. * Thank you.
  15. *
  16. */
  17. #include <linux/delay.h> /* For udelay */
  18. #include <linux/io.h> /* For read[bwl]/write[bwl] */
  19. #include <linux/serial.h> /* For struct async_serial */
  20. #include <linux/serial_reg.h> /* For the various UART offsets */
  21. #include <linux/pci.h>
  22. #include <linux/tty.h>
  23. #include "jsm.h" /* Driver main header file */
  24. static struct {
  25. unsigned int rate;
  26. unsigned int cflag;
  27. } baud_rates[] = {
  28. { 921600, B921600 },
  29. { 460800, B460800 },
  30. { 230400, B230400 },
  31. { 115200, B115200 },
  32. { 57600, B57600 },
  33. { 38400, B38400 },
  34. { 19200, B19200 },
  35. { 9600, B9600 },
  36. { 4800, B4800 },
  37. { 2400, B2400 },
  38. { 1200, B1200 },
  39. { 600, B600 },
  40. { 300, B300 },
  41. { 200, B200 },
  42. { 150, B150 },
  43. { 134, B134 },
  44. { 110, B110 },
  45. { 75, B75 },
  46. { 50, B50 },
  47. };
  48. static void cls_set_cts_flow_control(struct jsm_channel *ch)
  49. {
  50. u8 lcrb = readb(&ch->ch_cls_uart->lcr);
  51. u8 ier = readb(&ch->ch_cls_uart->ier);
  52. u8 isr_fcr = 0;
  53. /*
  54. * The Enhanced Register Set may only be accessed when
  55. * the Line Control Register is set to 0xBFh.
  56. */
  57. writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
  58. isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
  59. /* Turn on CTS flow control, turn off IXON flow control */
  60. isr_fcr |= (UART_EXAR654_EFR_ECB | UART_EXAR654_EFR_CTSDSR);
  61. isr_fcr &= ~(UART_EXAR654_EFR_IXON);
  62. writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
  63. /* Write old LCR value back out, which turns enhanced access off */
  64. writeb(lcrb, &ch->ch_cls_uart->lcr);
  65. /*
  66. * Enable interrupts for CTS flow, turn off interrupts for
  67. * received XOFF chars
  68. */
  69. ier |= (UART_EXAR654_IER_CTSDSR);
  70. ier &= ~(UART_EXAR654_IER_XOFF);
  71. writeb(ier, &ch->ch_cls_uart->ier);
  72. /* Set the usual FIFO values */
  73. writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
  74. writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_56 |
  75. UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
  76. &ch->ch_cls_uart->isr_fcr);
  77. ch->ch_t_tlevel = 16;
  78. }
  79. static void cls_set_ixon_flow_control(struct jsm_channel *ch)
  80. {
  81. u8 lcrb = readb(&ch->ch_cls_uart->lcr);
  82. u8 ier = readb(&ch->ch_cls_uart->ier);
  83. u8 isr_fcr = 0;
  84. /*
  85. * The Enhanced Register Set may only be accessed when
  86. * the Line Control Register is set to 0xBFh.
  87. */
  88. writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
  89. isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
  90. /* Turn on IXON flow control, turn off CTS flow control */
  91. isr_fcr |= (UART_EXAR654_EFR_ECB | UART_EXAR654_EFR_IXON);
  92. isr_fcr &= ~(UART_EXAR654_EFR_CTSDSR);
  93. writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
  94. /* Now set our current start/stop chars while in enhanced mode */
  95. writeb(ch->ch_startc, &ch->ch_cls_uart->mcr);
  96. writeb(0, &ch->ch_cls_uart->lsr);
  97. writeb(ch->ch_stopc, &ch->ch_cls_uart->msr);
  98. writeb(0, &ch->ch_cls_uart->spr);
  99. /* Write old LCR value back out, which turns enhanced access off */
  100. writeb(lcrb, &ch->ch_cls_uart->lcr);
  101. /*
  102. * Disable interrupts for CTS flow, turn on interrupts for
  103. * received XOFF chars
  104. */
  105. ier &= ~(UART_EXAR654_IER_CTSDSR);
  106. ier |= (UART_EXAR654_IER_XOFF);
  107. writeb(ier, &ch->ch_cls_uart->ier);
  108. /* Set the usual FIFO values */
  109. writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
  110. writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_16 |
  111. UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
  112. &ch->ch_cls_uart->isr_fcr);
  113. }
  114. static void cls_set_no_output_flow_control(struct jsm_channel *ch)
  115. {
  116. u8 lcrb = readb(&ch->ch_cls_uart->lcr);
  117. u8 ier = readb(&ch->ch_cls_uart->ier);
  118. u8 isr_fcr = 0;
  119. /*
  120. * The Enhanced Register Set may only be accessed when
  121. * the Line Control Register is set to 0xBFh.
  122. */
  123. writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
  124. isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
  125. /* Turn off IXON flow control, turn off CTS flow control */
  126. isr_fcr |= (UART_EXAR654_EFR_ECB);
  127. isr_fcr &= ~(UART_EXAR654_EFR_CTSDSR | UART_EXAR654_EFR_IXON);
  128. writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
  129. /* Write old LCR value back out, which turns enhanced access off */
  130. writeb(lcrb, &ch->ch_cls_uart->lcr);
  131. /*
  132. * Disable interrupts for CTS flow, turn off interrupts for
  133. * received XOFF chars
  134. */
  135. ier &= ~(UART_EXAR654_IER_CTSDSR);
  136. ier &= ~(UART_EXAR654_IER_XOFF);
  137. writeb(ier, &ch->ch_cls_uart->ier);
  138. /* Set the usual FIFO values */
  139. writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
  140. writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_16 |
  141. UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
  142. &ch->ch_cls_uart->isr_fcr);
  143. ch->ch_r_watermark = 0;
  144. ch->ch_t_tlevel = 16;
  145. ch->ch_r_tlevel = 16;
  146. }
  147. static void cls_set_rts_flow_control(struct jsm_channel *ch)
  148. {
  149. u8 lcrb = readb(&ch->ch_cls_uart->lcr);
  150. u8 ier = readb(&ch->ch_cls_uart->ier);
  151. u8 isr_fcr = 0;
  152. /*
  153. * The Enhanced Register Set may only be accessed when
  154. * the Line Control Register is set to 0xBFh.
  155. */
  156. writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
  157. isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
  158. /* Turn on RTS flow control, turn off IXOFF flow control */
  159. isr_fcr |= (UART_EXAR654_EFR_ECB | UART_EXAR654_EFR_RTSDTR);
  160. isr_fcr &= ~(UART_EXAR654_EFR_IXOFF);
  161. writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
  162. /* Write old LCR value back out, which turns enhanced access off */
  163. writeb(lcrb, &ch->ch_cls_uart->lcr);
  164. /* Enable interrupts for RTS flow */
  165. ier |= (UART_EXAR654_IER_RTSDTR);
  166. writeb(ier, &ch->ch_cls_uart->ier);
  167. /* Set the usual FIFO values */
  168. writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
  169. writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_56 |
  170. UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
  171. &ch->ch_cls_uart->isr_fcr);
  172. ch->ch_r_watermark = 4;
  173. ch->ch_r_tlevel = 8;
  174. }
  175. static void cls_set_ixoff_flow_control(struct jsm_channel *ch)
  176. {
  177. u8 lcrb = readb(&ch->ch_cls_uart->lcr);
  178. u8 ier = readb(&ch->ch_cls_uart->ier);
  179. u8 isr_fcr = 0;
  180. /*
  181. * The Enhanced Register Set may only be accessed when
  182. * the Line Control Register is set to 0xBFh.
  183. */
  184. writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
  185. isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
  186. /* Turn on IXOFF flow control, turn off RTS flow control */
  187. isr_fcr |= (UART_EXAR654_EFR_ECB | UART_EXAR654_EFR_IXOFF);
  188. isr_fcr &= ~(UART_EXAR654_EFR_RTSDTR);
  189. writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
  190. /* Now set our current start/stop chars while in enhanced mode */
  191. writeb(ch->ch_startc, &ch->ch_cls_uart->mcr);
  192. writeb(0, &ch->ch_cls_uart->lsr);
  193. writeb(ch->ch_stopc, &ch->ch_cls_uart->msr);
  194. writeb(0, &ch->ch_cls_uart->spr);
  195. /* Write old LCR value back out, which turns enhanced access off */
  196. writeb(lcrb, &ch->ch_cls_uart->lcr);
  197. /* Disable interrupts for RTS flow */
  198. ier &= ~(UART_EXAR654_IER_RTSDTR);
  199. writeb(ier, &ch->ch_cls_uart->ier);
  200. /* Set the usual FIFO values */
  201. writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
  202. writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_16 |
  203. UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
  204. &ch->ch_cls_uart->isr_fcr);
  205. }
  206. static void cls_set_no_input_flow_control(struct jsm_channel *ch)
  207. {
  208. u8 lcrb = readb(&ch->ch_cls_uart->lcr);
  209. u8 ier = readb(&ch->ch_cls_uart->ier);
  210. u8 isr_fcr = 0;
  211. /*
  212. * The Enhanced Register Set may only be accessed when
  213. * the Line Control Register is set to 0xBFh.
  214. */
  215. writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
  216. isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
  217. /* Turn off IXOFF flow control, turn off RTS flow control */
  218. isr_fcr |= (UART_EXAR654_EFR_ECB);
  219. isr_fcr &= ~(UART_EXAR654_EFR_RTSDTR | UART_EXAR654_EFR_IXOFF);
  220. writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
  221. /* Write old LCR value back out, which turns enhanced access off */
  222. writeb(lcrb, &ch->ch_cls_uart->lcr);
  223. /* Disable interrupts for RTS flow */
  224. ier &= ~(UART_EXAR654_IER_RTSDTR);
  225. writeb(ier, &ch->ch_cls_uart->ier);
  226. /* Set the usual FIFO values */
  227. writeb((UART_FCR_ENABLE_FIFO), &ch->ch_cls_uart->isr_fcr);
  228. writeb((UART_FCR_ENABLE_FIFO | UART_16654_FCR_RXTRIGGER_16 |
  229. UART_16654_FCR_TXTRIGGER_16 | UART_FCR_CLEAR_RCVR),
  230. &ch->ch_cls_uart->isr_fcr);
  231. ch->ch_t_tlevel = 16;
  232. ch->ch_r_tlevel = 16;
  233. }
  234. /*
  235. * cls_clear_break.
  236. * Determines whether its time to shut off break condition.
  237. *
  238. * No locks are assumed to be held when calling this function.
  239. * channel lock is held and released in this function.
  240. */
  241. static void cls_clear_break(struct jsm_channel *ch)
  242. {
  243. unsigned long lock_flags;
  244. spin_lock_irqsave(&ch->ch_lock, lock_flags);
  245. /* Turn break off, and unset some variables */
  246. if (ch->ch_flags & CH_BREAK_SENDING) {
  247. u8 temp = readb(&ch->ch_cls_uart->lcr);
  248. writeb((temp & ~UART_LCR_SBC), &ch->ch_cls_uart->lcr);
  249. ch->ch_flags &= ~(CH_BREAK_SENDING);
  250. jsm_dbg(IOCTL, &ch->ch_bd->pci_dev,
  251. "clear break Finishing UART_LCR_SBC! finished: %lx\n",
  252. jiffies);
  253. }
  254. spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
  255. }
  256. static void cls_disable_receiver(struct jsm_channel *ch)
  257. {
  258. u8 tmp = readb(&ch->ch_cls_uart->ier);
  259. tmp &= ~(UART_IER_RDI);
  260. writeb(tmp, &ch->ch_cls_uart->ier);
  261. }
  262. static void cls_enable_receiver(struct jsm_channel *ch)
  263. {
  264. u8 tmp = readb(&ch->ch_cls_uart->ier);
  265. tmp |= (UART_IER_RDI);
  266. writeb(tmp, &ch->ch_cls_uart->ier);
  267. }
  268. /* Make the UART raise any of the output signals we want up */
  269. static void cls_assert_modem_signals(struct jsm_channel *ch)
  270. {
  271. if (!ch)
  272. return;
  273. writeb(ch->ch_mostat, &ch->ch_cls_uart->mcr);
  274. }
  275. static void cls_copy_data_from_uart_to_queue(struct jsm_channel *ch)
  276. {
  277. int qleft = 0;
  278. u8 linestatus;
  279. u8 error_mask = 0;
  280. u16 head;
  281. u16 tail;
  282. unsigned long flags;
  283. if (!ch)
  284. return;
  285. spin_lock_irqsave(&ch->ch_lock, flags);
  286. /* cache head and tail of queue */
  287. head = ch->ch_r_head & RQUEUEMASK;
  288. tail = ch->ch_r_tail & RQUEUEMASK;
  289. ch->ch_cached_lsr = 0;
  290. /* Store how much space we have left in the queue */
  291. qleft = tail - head - 1;
  292. if (qleft < 0)
  293. qleft += RQUEUEMASK + 1;
  294. /*
  295. * Create a mask to determine whether we should
  296. * insert the character (if any) into our queue.
  297. */
  298. if (ch->ch_c_iflag & IGNBRK)
  299. error_mask |= UART_LSR_BI;
  300. while (1) {
  301. /*
  302. * Grab the linestatus register, we need to
  303. * check to see if there is any data to read
  304. */
  305. linestatus = readb(&ch->ch_cls_uart->lsr);
  306. /* Break out if there is no data to fetch */
  307. if (!(linestatus & UART_LSR_DR))
  308. break;
  309. /*
  310. * Discard character if we are ignoring the error mask
  311. * which in this case is the break signal.
  312. */
  313. if (linestatus & error_mask) {
  314. linestatus = 0;
  315. readb(&ch->ch_cls_uart->txrx);
  316. continue;
  317. }
  318. /*
  319. * If our queue is full, we have no choice but to drop some
  320. * data. The assumption is that HWFLOW or SWFLOW should have
  321. * stopped things way way before we got to this point.
  322. *
  323. * I decided that I wanted to ditch the oldest data first,
  324. * I hope thats okay with everyone? Yes? Good.
  325. */
  326. while (qleft < 1) {
  327. tail = (tail + 1) & RQUEUEMASK;
  328. ch->ch_r_tail = tail;
  329. ch->ch_err_overrun++;
  330. qleft++;
  331. }
  332. ch->ch_equeue[head] = linestatus & (UART_LSR_BI | UART_LSR_PE
  333. | UART_LSR_FE);
  334. ch->ch_rqueue[head] = readb(&ch->ch_cls_uart->txrx);
  335. qleft--;
  336. if (ch->ch_equeue[head] & UART_LSR_PE)
  337. ch->ch_err_parity++;
  338. if (ch->ch_equeue[head] & UART_LSR_BI)
  339. ch->ch_err_break++;
  340. if (ch->ch_equeue[head] & UART_LSR_FE)
  341. ch->ch_err_frame++;
  342. /* Add to, and flip head if needed */
  343. head = (head + 1) & RQUEUEMASK;
  344. ch->ch_rxcount++;
  345. }
  346. /*
  347. * Write new final heads to channel structure.
  348. */
  349. ch->ch_r_head = head & RQUEUEMASK;
  350. ch->ch_e_head = head & EQUEUEMASK;
  351. spin_unlock_irqrestore(&ch->ch_lock, flags);
  352. }
  353. static void cls_copy_data_from_queue_to_uart(struct jsm_channel *ch)
  354. {
  355. u16 tail;
  356. int n;
  357. int qlen;
  358. u32 len_written = 0;
  359. struct circ_buf *circ;
  360. if (!ch)
  361. return;
  362. circ = &ch->uart_port.state->xmit;
  363. /* No data to write to the UART */
  364. if (uart_circ_empty(circ))
  365. return;
  366. /* If port is "stopped", don't send any data to the UART */
  367. if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_BREAK_SENDING))
  368. return;
  369. /* We have to do it this way, because of the EXAR TXFIFO count bug. */
  370. if (!(ch->ch_flags & (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM)))
  371. return;
  372. n = 32;
  373. /* cache tail of queue */
  374. tail = circ->tail & (UART_XMIT_SIZE - 1);
  375. qlen = uart_circ_chars_pending(circ);
  376. /* Find minimum of the FIFO space, versus queue length */
  377. n = min(n, qlen);
  378. while (n > 0) {
  379. writeb(circ->buf[tail], &ch->ch_cls_uart->txrx);
  380. tail = (tail + 1) & (UART_XMIT_SIZE - 1);
  381. n--;
  382. ch->ch_txcount++;
  383. len_written++;
  384. }
  385. /* Update the final tail */
  386. circ->tail = tail & (UART_XMIT_SIZE - 1);
  387. if (len_written > ch->ch_t_tlevel)
  388. ch->ch_flags &= ~(CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
  389. if (uart_circ_empty(circ))
  390. uart_write_wakeup(&ch->uart_port);
  391. }
  392. static void cls_parse_modem(struct jsm_channel *ch, u8 signals)
  393. {
  394. u8 msignals = signals;
  395. jsm_dbg(MSIGS, &ch->ch_bd->pci_dev,
  396. "neo_parse_modem: port: %d msignals: %x\n",
  397. ch->ch_portnum, msignals);
  398. /*
  399. * Scrub off lower bits.
  400. * They signify delta's, which I don't care about
  401. * Keep DDCD and DDSR though
  402. */
  403. msignals &= 0xf8;
  404. if (msignals & UART_MSR_DDCD)
  405. uart_handle_dcd_change(&ch->uart_port, msignals & UART_MSR_DCD);
  406. if (msignals & UART_MSR_DDSR)
  407. uart_handle_dcd_change(&ch->uart_port, msignals & UART_MSR_CTS);
  408. if (msignals & UART_MSR_DCD)
  409. ch->ch_mistat |= UART_MSR_DCD;
  410. else
  411. ch->ch_mistat &= ~UART_MSR_DCD;
  412. if (msignals & UART_MSR_DSR)
  413. ch->ch_mistat |= UART_MSR_DSR;
  414. else
  415. ch->ch_mistat &= ~UART_MSR_DSR;
  416. if (msignals & UART_MSR_RI)
  417. ch->ch_mistat |= UART_MSR_RI;
  418. else
  419. ch->ch_mistat &= ~UART_MSR_RI;
  420. if (msignals & UART_MSR_CTS)
  421. ch->ch_mistat |= UART_MSR_CTS;
  422. else
  423. ch->ch_mistat &= ~UART_MSR_CTS;
  424. jsm_dbg(MSIGS, &ch->ch_bd->pci_dev,
  425. "Port: %d DTR: %d RTS: %d CTS: %d DSR: %d " "RI: %d CD: %d\n",
  426. ch->ch_portnum,
  427. !!((ch->ch_mistat | ch->ch_mostat) & UART_MCR_DTR),
  428. !!((ch->ch_mistat | ch->ch_mostat) & UART_MCR_RTS),
  429. !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_CTS),
  430. !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_DSR),
  431. !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_RI),
  432. !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_DCD));
  433. }
  434. /* Parse the ISR register for the specific port */
  435. static inline void cls_parse_isr(struct jsm_board *brd, uint port)
  436. {
  437. struct jsm_channel *ch;
  438. u8 isr = 0;
  439. unsigned long flags;
  440. /*
  441. * No need to verify board pointer, it was already
  442. * verified in the interrupt routine.
  443. */
  444. if (port >= brd->nasync)
  445. return;
  446. ch = brd->channels[port];
  447. if (!ch)
  448. return;
  449. /* Here we try to figure out what caused the interrupt to happen */
  450. while (1) {
  451. isr = readb(&ch->ch_cls_uart->isr_fcr);
  452. /* Bail if no pending interrupt on port */
  453. if (isr & UART_IIR_NO_INT)
  454. break;
  455. /* Receive Interrupt pending */
  456. if (isr & (UART_IIR_RDI | UART_IIR_RDI_TIMEOUT)) {
  457. /* Read data from uart -> queue */
  458. cls_copy_data_from_uart_to_queue(ch);
  459. jsm_check_queue_flow_control(ch);
  460. }
  461. /* Transmit Hold register empty pending */
  462. if (isr & UART_IIR_THRI) {
  463. /* Transfer data (if any) from Write Queue -> UART. */
  464. spin_lock_irqsave(&ch->ch_lock, flags);
  465. ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
  466. spin_unlock_irqrestore(&ch->ch_lock, flags);
  467. cls_copy_data_from_queue_to_uart(ch);
  468. }
  469. /*
  470. * CTS/RTS change of state:
  471. * Don't need to do anything, the cls_parse_modem
  472. * below will grab the updated modem signals.
  473. */
  474. /* Parse any modem signal changes */
  475. cls_parse_modem(ch, readb(&ch->ch_cls_uart->msr));
  476. }
  477. }
  478. /* Channel lock MUST be held before calling this function! */
  479. static void cls_flush_uart_write(struct jsm_channel *ch)
  480. {
  481. u8 tmp = 0;
  482. u8 i = 0;
  483. if (!ch)
  484. return;
  485. writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_XMIT),
  486. &ch->ch_cls_uart->isr_fcr);
  487. for (i = 0; i < 10; i++) {
  488. /* Check to see if the UART feels it completely flushed FIFO */
  489. tmp = readb(&ch->ch_cls_uart->isr_fcr);
  490. if (tmp & UART_FCR_CLEAR_XMIT) {
  491. jsm_dbg(IOCTL, &ch->ch_bd->pci_dev,
  492. "Still flushing TX UART... i: %d\n", i);
  493. udelay(10);
  494. } else
  495. break;
  496. }
  497. ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
  498. }
  499. /* Channel lock MUST be held before calling this function! */
  500. static void cls_flush_uart_read(struct jsm_channel *ch)
  501. {
  502. if (!ch)
  503. return;
  504. /*
  505. * For complete POSIX compatibility, we should be purging the
  506. * read FIFO in the UART here.
  507. *
  508. * However, clearing the read FIFO (UART_FCR_CLEAR_RCVR) also
  509. * incorrectly flushes write data as well as just basically trashing the
  510. * FIFO.
  511. *
  512. * Presumably, this is a bug in this UART.
  513. */
  514. udelay(10);
  515. }
  516. static void cls_send_start_character(struct jsm_channel *ch)
  517. {
  518. if (!ch)
  519. return;
  520. if (ch->ch_startc != __DISABLED_CHAR) {
  521. ch->ch_xon_sends++;
  522. writeb(ch->ch_startc, &ch->ch_cls_uart->txrx);
  523. }
  524. }
  525. static void cls_send_stop_character(struct jsm_channel *ch)
  526. {
  527. if (!ch)
  528. return;
  529. if (ch->ch_stopc != __DISABLED_CHAR) {
  530. ch->ch_xoff_sends++;
  531. writeb(ch->ch_stopc, &ch->ch_cls_uart->txrx);
  532. }
  533. }
  534. /*
  535. * cls_param()
  536. * Send any/all changes to the line to the UART.
  537. */
  538. static void cls_param(struct jsm_channel *ch)
  539. {
  540. u8 lcr = 0;
  541. u8 uart_lcr = 0;
  542. u8 ier = 0;
  543. u32 baud = 9600;
  544. int quot = 0;
  545. struct jsm_board *bd;
  546. int i;
  547. unsigned int cflag;
  548. bd = ch->ch_bd;
  549. if (!bd)
  550. return;
  551. /*
  552. * If baud rate is zero, flush queues, and set mval to drop DTR.
  553. */
  554. if ((ch->ch_c_cflag & CBAUD) == B0) {
  555. ch->ch_r_head = 0;
  556. ch->ch_r_tail = 0;
  557. ch->ch_e_head = 0;
  558. ch->ch_e_tail = 0;
  559. cls_flush_uart_write(ch);
  560. cls_flush_uart_read(ch);
  561. /* The baudrate is B0 so all modem lines are to be dropped. */
  562. ch->ch_flags |= (CH_BAUD0);
  563. ch->ch_mostat &= ~(UART_MCR_RTS | UART_MCR_DTR);
  564. cls_assert_modem_signals(ch);
  565. return;
  566. }
  567. cflag = C_BAUD(ch->uart_port.state->port.tty);
  568. baud = 9600;
  569. for (i = 0; i < ARRAY_SIZE(baud_rates); i++) {
  570. if (baud_rates[i].cflag == cflag) {
  571. baud = baud_rates[i].rate;
  572. break;
  573. }
  574. }
  575. if (ch->ch_flags & CH_BAUD0)
  576. ch->ch_flags &= ~(CH_BAUD0);
  577. if (ch->ch_c_cflag & PARENB)
  578. lcr |= UART_LCR_PARITY;
  579. if (!(ch->ch_c_cflag & PARODD))
  580. lcr |= UART_LCR_EPAR;
  581. if (ch->ch_c_cflag & CMSPAR)
  582. lcr |= UART_LCR_SPAR;
  583. if (ch->ch_c_cflag & CSTOPB)
  584. lcr |= UART_LCR_STOP;
  585. lcr |= UART_LCR_WLEN(tty_get_char_size(ch->ch_c_cflag));
  586. ier = readb(&ch->ch_cls_uart->ier);
  587. uart_lcr = readb(&ch->ch_cls_uart->lcr);
  588. quot = ch->ch_bd->bd_dividend / baud;
  589. if (quot != 0) {
  590. writeb(UART_LCR_DLAB, &ch->ch_cls_uart->lcr);
  591. writeb((quot & 0xff), &ch->ch_cls_uart->txrx);
  592. writeb((quot >> 8), &ch->ch_cls_uart->ier);
  593. writeb(lcr, &ch->ch_cls_uart->lcr);
  594. }
  595. if (uart_lcr != lcr)
  596. writeb(lcr, &ch->ch_cls_uart->lcr);
  597. if (ch->ch_c_cflag & CREAD)
  598. ier |= (UART_IER_RDI | UART_IER_RLSI);
  599. ier |= (UART_IER_THRI | UART_IER_MSI);
  600. writeb(ier, &ch->ch_cls_uart->ier);
  601. if (ch->ch_c_cflag & CRTSCTS)
  602. cls_set_cts_flow_control(ch);
  603. else if (ch->ch_c_iflag & IXON) {
  604. /*
  605. * If start/stop is set to disable,
  606. * then we should disable flow control.
  607. */
  608. if ((ch->ch_startc == __DISABLED_CHAR) ||
  609. (ch->ch_stopc == __DISABLED_CHAR))
  610. cls_set_no_output_flow_control(ch);
  611. else
  612. cls_set_ixon_flow_control(ch);
  613. } else
  614. cls_set_no_output_flow_control(ch);
  615. if (ch->ch_c_cflag & CRTSCTS)
  616. cls_set_rts_flow_control(ch);
  617. else if (ch->ch_c_iflag & IXOFF) {
  618. /*
  619. * If start/stop is set to disable,
  620. * then we should disable flow control.
  621. */
  622. if ((ch->ch_startc == __DISABLED_CHAR) ||
  623. (ch->ch_stopc == __DISABLED_CHAR))
  624. cls_set_no_input_flow_control(ch);
  625. else
  626. cls_set_ixoff_flow_control(ch);
  627. } else
  628. cls_set_no_input_flow_control(ch);
  629. cls_assert_modem_signals(ch);
  630. /* get current status of the modem signals now */
  631. cls_parse_modem(ch, readb(&ch->ch_cls_uart->msr));
  632. }
  633. /*
  634. * cls_intr()
  635. *
  636. * Classic specific interrupt handler.
  637. */
  638. static irqreturn_t cls_intr(int irq, void *voidbrd)
  639. {
  640. struct jsm_board *brd = voidbrd;
  641. unsigned long lock_flags;
  642. unsigned char uart_poll;
  643. uint i = 0;
  644. /* Lock out the slow poller from running on this board. */
  645. spin_lock_irqsave(&brd->bd_intr_lock, lock_flags);
  646. /*
  647. * Check the board's global interrupt offset to see if we
  648. * acctually do have an interrupt pending on us.
  649. */
  650. uart_poll = readb(brd->re_map_membase + UART_CLASSIC_POLL_ADDR_OFFSET);
  651. jsm_dbg(INTR, &brd->pci_dev, "%s:%d uart_poll: %x\n",
  652. __FILE__, __LINE__, uart_poll);
  653. if (!uart_poll) {
  654. jsm_dbg(INTR, &brd->pci_dev,
  655. "Kernel interrupted to me, but no pending interrupts...\n");
  656. spin_unlock_irqrestore(&brd->bd_intr_lock, lock_flags);
  657. return IRQ_NONE;
  658. }
  659. /* At this point, we have at least SOMETHING to service, dig further. */
  660. /* Parse each port to find out what caused the interrupt */
  661. for (i = 0; i < brd->nasync; i++)
  662. cls_parse_isr(brd, i);
  663. spin_unlock_irqrestore(&brd->bd_intr_lock, lock_flags);
  664. return IRQ_HANDLED;
  665. }
  666. /* Inits UART */
  667. static void cls_uart_init(struct jsm_channel *ch)
  668. {
  669. unsigned char lcrb = readb(&ch->ch_cls_uart->lcr);
  670. unsigned char isr_fcr = 0;
  671. writeb(0, &ch->ch_cls_uart->ier);
  672. /*
  673. * The Enhanced Register Set may only be accessed when
  674. * the Line Control Register is set to 0xBFh.
  675. */
  676. writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
  677. isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
  678. /* Turn on Enhanced/Extended controls */
  679. isr_fcr |= (UART_EXAR654_EFR_ECB);
  680. writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
  681. /* Write old LCR value back out, which turns enhanced access off */
  682. writeb(lcrb, &ch->ch_cls_uart->lcr);
  683. /* Clear out UART and FIFO */
  684. readb(&ch->ch_cls_uart->txrx);
  685. writeb((UART_FCR_ENABLE_FIFO|UART_FCR_CLEAR_RCVR|UART_FCR_CLEAR_XMIT),
  686. &ch->ch_cls_uart->isr_fcr);
  687. udelay(10);
  688. ch->ch_flags |= (CH_FIFO_ENABLED | CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
  689. readb(&ch->ch_cls_uart->lsr);
  690. readb(&ch->ch_cls_uart->msr);
  691. }
  692. /*
  693. * Turns off UART.
  694. */
  695. static void cls_uart_off(struct jsm_channel *ch)
  696. {
  697. /* Stop all interrupts from accurring. */
  698. writeb(0, &ch->ch_cls_uart->ier);
  699. }
  700. /*
  701. * cls_get_uarts_bytes_left.
  702. * Returns 0 is nothing left in the FIFO, returns 1 otherwise.
  703. *
  704. * The channel lock MUST be held by the calling function.
  705. */
  706. static u32 cls_get_uart_bytes_left(struct jsm_channel *ch)
  707. {
  708. u8 left = 0;
  709. u8 lsr = readb(&ch->ch_cls_uart->lsr);
  710. /* Determine whether the Transmitter is empty or not */
  711. if (!(lsr & UART_LSR_TEMT))
  712. left = 1;
  713. else {
  714. ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
  715. left = 0;
  716. }
  717. return left;
  718. }
  719. /*
  720. * cls_send_break.
  721. * Starts sending a break thru the UART.
  722. *
  723. * The channel lock MUST be held by the calling function.
  724. */
  725. static void cls_send_break(struct jsm_channel *ch)
  726. {
  727. /* Tell the UART to start sending the break */
  728. if (!(ch->ch_flags & CH_BREAK_SENDING)) {
  729. u8 temp = readb(&ch->ch_cls_uart->lcr);
  730. writeb((temp | UART_LCR_SBC), &ch->ch_cls_uart->lcr);
  731. ch->ch_flags |= (CH_BREAK_SENDING);
  732. }
  733. }
  734. /*
  735. * cls_send_immediate_char.
  736. * Sends a specific character as soon as possible to the UART,
  737. * jumping over any bytes that might be in the write queue.
  738. *
  739. * The channel lock MUST be held by the calling function.
  740. */
  741. static void cls_send_immediate_char(struct jsm_channel *ch, unsigned char c)
  742. {
  743. writeb(c, &ch->ch_cls_uart->txrx);
  744. }
  745. struct board_ops jsm_cls_ops = {
  746. .intr = cls_intr,
  747. .uart_init = cls_uart_init,
  748. .uart_off = cls_uart_off,
  749. .param = cls_param,
  750. .assert_modem_signals = cls_assert_modem_signals,
  751. .flush_uart_write = cls_flush_uart_write,
  752. .flush_uart_read = cls_flush_uart_read,
  753. .disable_receiver = cls_disable_receiver,
  754. .enable_receiver = cls_enable_receiver,
  755. .send_break = cls_send_break,
  756. .clear_break = cls_clear_break,
  757. .send_start_character = cls_send_start_character,
  758. .send_stop_character = cls_send_stop_character,
  759. .copy_data_from_queue_to_uart = cls_copy_data_from_queue_to_uart,
  760. .get_uart_bytes_left = cls_get_uart_bytes_left,
  761. .send_immediate_char = cls_send_immediate_char
  762. };