jsm_cls.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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 = 0;
  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. /* Get our cached LSR */
  290. linestatus = ch->ch_cached_lsr;
  291. ch->ch_cached_lsr = 0;
  292. /* Store how much space we have left in the queue */
  293. qleft = tail - head - 1;
  294. if (qleft < 0)
  295. qleft += RQUEUEMASK + 1;
  296. /*
  297. * Create a mask to determine whether we should
  298. * insert the character (if any) into our queue.
  299. */
  300. if (ch->ch_c_iflag & IGNBRK)
  301. error_mask |= UART_LSR_BI;
  302. while (1) {
  303. /*
  304. * Grab the linestatus register, we need to
  305. * check to see if there is any data to read
  306. */
  307. linestatus = readb(&ch->ch_cls_uart->lsr);
  308. /* Break out if there is no data to fetch */
  309. if (!(linestatus & UART_LSR_DR))
  310. break;
  311. /*
  312. * Discard character if we are ignoring the error mask
  313. * which in this case is the break signal.
  314. */
  315. if (linestatus & error_mask) {
  316. u8 discard;
  317. linestatus = 0;
  318. discard = readb(&ch->ch_cls_uart->txrx);
  319. continue;
  320. }
  321. /*
  322. * If our queue is full, we have no choice but to drop some
  323. * data. The assumption is that HWFLOW or SWFLOW should have
  324. * stopped things way way before we got to this point.
  325. *
  326. * I decided that I wanted to ditch the oldest data first,
  327. * I hope thats okay with everyone? Yes? Good.
  328. */
  329. while (qleft < 1) {
  330. tail = (tail + 1) & RQUEUEMASK;
  331. ch->ch_r_tail = tail;
  332. ch->ch_err_overrun++;
  333. qleft++;
  334. }
  335. ch->ch_equeue[head] = linestatus & (UART_LSR_BI | UART_LSR_PE
  336. | UART_LSR_FE);
  337. ch->ch_rqueue[head] = readb(&ch->ch_cls_uart->txrx);
  338. qleft--;
  339. if (ch->ch_equeue[head] & UART_LSR_PE)
  340. ch->ch_err_parity++;
  341. if (ch->ch_equeue[head] & UART_LSR_BI)
  342. ch->ch_err_break++;
  343. if (ch->ch_equeue[head] & UART_LSR_FE)
  344. ch->ch_err_frame++;
  345. /* Add to, and flip head if needed */
  346. head = (head + 1) & RQUEUEMASK;
  347. ch->ch_rxcount++;
  348. }
  349. /*
  350. * Write new final heads to channel structure.
  351. */
  352. ch->ch_r_head = head & RQUEUEMASK;
  353. ch->ch_e_head = head & EQUEUEMASK;
  354. spin_unlock_irqrestore(&ch->ch_lock, flags);
  355. }
  356. static void cls_copy_data_from_queue_to_uart(struct jsm_channel *ch)
  357. {
  358. u16 tail;
  359. int n;
  360. int qlen;
  361. u32 len_written = 0;
  362. struct circ_buf *circ;
  363. if (!ch)
  364. return;
  365. circ = &ch->uart_port.state->xmit;
  366. /* No data to write to the UART */
  367. if (uart_circ_empty(circ))
  368. return;
  369. /* If port is "stopped", don't send any data to the UART */
  370. if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_BREAK_SENDING))
  371. return;
  372. /* We have to do it this way, because of the EXAR TXFIFO count bug. */
  373. if (!(ch->ch_flags & (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM)))
  374. return;
  375. n = 32;
  376. /* cache tail of queue */
  377. tail = circ->tail & (UART_XMIT_SIZE - 1);
  378. qlen = uart_circ_chars_pending(circ);
  379. /* Find minimum of the FIFO space, versus queue length */
  380. n = min(n, qlen);
  381. while (n > 0) {
  382. writeb(circ->buf[tail], &ch->ch_cls_uart->txrx);
  383. tail = (tail + 1) & (UART_XMIT_SIZE - 1);
  384. n--;
  385. ch->ch_txcount++;
  386. len_written++;
  387. }
  388. /* Update the final tail */
  389. circ->tail = tail & (UART_XMIT_SIZE - 1);
  390. if (len_written > ch->ch_t_tlevel)
  391. ch->ch_flags &= ~(CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
  392. if (uart_circ_empty(circ))
  393. uart_write_wakeup(&ch->uart_port);
  394. }
  395. static void cls_parse_modem(struct jsm_channel *ch, u8 signals)
  396. {
  397. u8 msignals = signals;
  398. jsm_dbg(MSIGS, &ch->ch_bd->pci_dev,
  399. "neo_parse_modem: port: %d msignals: %x\n",
  400. ch->ch_portnum, msignals);
  401. /*
  402. * Scrub off lower bits.
  403. * They signify delta's, which I don't care about
  404. * Keep DDCD and DDSR though
  405. */
  406. msignals &= 0xf8;
  407. if (msignals & UART_MSR_DDCD)
  408. uart_handle_dcd_change(&ch->uart_port, msignals & UART_MSR_DCD);
  409. if (msignals & UART_MSR_DDSR)
  410. uart_handle_dcd_change(&ch->uart_port, msignals & UART_MSR_CTS);
  411. if (msignals & UART_MSR_DCD)
  412. ch->ch_mistat |= UART_MSR_DCD;
  413. else
  414. ch->ch_mistat &= ~UART_MSR_DCD;
  415. if (msignals & UART_MSR_DSR)
  416. ch->ch_mistat |= UART_MSR_DSR;
  417. else
  418. ch->ch_mistat &= ~UART_MSR_DSR;
  419. if (msignals & UART_MSR_RI)
  420. ch->ch_mistat |= UART_MSR_RI;
  421. else
  422. ch->ch_mistat &= ~UART_MSR_RI;
  423. if (msignals & UART_MSR_CTS)
  424. ch->ch_mistat |= UART_MSR_CTS;
  425. else
  426. ch->ch_mistat &= ~UART_MSR_CTS;
  427. jsm_dbg(MSIGS, &ch->ch_bd->pci_dev,
  428. "Port: %d DTR: %d RTS: %d CTS: %d DSR: %d " "RI: %d CD: %d\n",
  429. ch->ch_portnum,
  430. !!((ch->ch_mistat | ch->ch_mostat) & UART_MCR_DTR),
  431. !!((ch->ch_mistat | ch->ch_mostat) & UART_MCR_RTS),
  432. !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_CTS),
  433. !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_DSR),
  434. !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_RI),
  435. !!((ch->ch_mistat | ch->ch_mostat) & UART_MSR_DCD));
  436. }
  437. /* Parse the ISR register for the specific port */
  438. static inline void cls_parse_isr(struct jsm_board *brd, uint port)
  439. {
  440. struct jsm_channel *ch;
  441. u8 isr = 0;
  442. unsigned long flags;
  443. /*
  444. * No need to verify board pointer, it was already
  445. * verified in the interrupt routine.
  446. */
  447. if (port >= brd->nasync)
  448. return;
  449. ch = brd->channels[port];
  450. if (!ch)
  451. return;
  452. /* Here we try to figure out what caused the interrupt to happen */
  453. while (1) {
  454. isr = readb(&ch->ch_cls_uart->isr_fcr);
  455. /* Bail if no pending interrupt on port */
  456. if (isr & UART_IIR_NO_INT)
  457. break;
  458. /* Receive Interrupt pending */
  459. if (isr & (UART_IIR_RDI | UART_IIR_RDI_TIMEOUT)) {
  460. /* Read data from uart -> queue */
  461. cls_copy_data_from_uart_to_queue(ch);
  462. jsm_check_queue_flow_control(ch);
  463. }
  464. /* Transmit Hold register empty pending */
  465. if (isr & UART_IIR_THRI) {
  466. /* Transfer data (if any) from Write Queue -> UART. */
  467. spin_lock_irqsave(&ch->ch_lock, flags);
  468. ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
  469. spin_unlock_irqrestore(&ch->ch_lock, flags);
  470. cls_copy_data_from_queue_to_uart(ch);
  471. }
  472. /*
  473. * CTS/RTS change of state:
  474. * Don't need to do anything, the cls_parse_modem
  475. * below will grab the updated modem signals.
  476. */
  477. /* Parse any modem signal changes */
  478. cls_parse_modem(ch, readb(&ch->ch_cls_uart->msr));
  479. }
  480. }
  481. /* Channel lock MUST be held before calling this function! */
  482. static void cls_flush_uart_write(struct jsm_channel *ch)
  483. {
  484. u8 tmp = 0;
  485. u8 i = 0;
  486. if (!ch)
  487. return;
  488. writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_XMIT),
  489. &ch->ch_cls_uart->isr_fcr);
  490. for (i = 0; i < 10; i++) {
  491. /* Check to see if the UART feels it completely flushed FIFO */
  492. tmp = readb(&ch->ch_cls_uart->isr_fcr);
  493. if (tmp & UART_FCR_CLEAR_XMIT) {
  494. jsm_dbg(IOCTL, &ch->ch_bd->pci_dev,
  495. "Still flushing TX UART... i: %d\n", i);
  496. udelay(10);
  497. } else
  498. break;
  499. }
  500. ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
  501. }
  502. /* Channel lock MUST be held before calling this function! */
  503. static void cls_flush_uart_read(struct jsm_channel *ch)
  504. {
  505. if (!ch)
  506. return;
  507. /*
  508. * For complete POSIX compatibility, we should be purging the
  509. * read FIFO in the UART here.
  510. *
  511. * However, clearing the read FIFO (UART_FCR_CLEAR_RCVR) also
  512. * incorrectly flushes write data as well as just basically trashing the
  513. * FIFO.
  514. *
  515. * Presumably, this is a bug in this UART.
  516. */
  517. udelay(10);
  518. }
  519. static void cls_send_start_character(struct jsm_channel *ch)
  520. {
  521. if (!ch)
  522. return;
  523. if (ch->ch_startc != __DISABLED_CHAR) {
  524. ch->ch_xon_sends++;
  525. writeb(ch->ch_startc, &ch->ch_cls_uart->txrx);
  526. }
  527. }
  528. static void cls_send_stop_character(struct jsm_channel *ch)
  529. {
  530. if (!ch)
  531. return;
  532. if (ch->ch_stopc != __DISABLED_CHAR) {
  533. ch->ch_xoff_sends++;
  534. writeb(ch->ch_stopc, &ch->ch_cls_uart->txrx);
  535. }
  536. }
  537. /*
  538. * cls_param()
  539. * Send any/all changes to the line to the UART.
  540. */
  541. static void cls_param(struct jsm_channel *ch)
  542. {
  543. u8 lcr = 0;
  544. u8 uart_lcr = 0;
  545. u8 ier = 0;
  546. u32 baud = 9600;
  547. int quot = 0;
  548. struct jsm_board *bd;
  549. int i;
  550. unsigned int cflag;
  551. bd = ch->ch_bd;
  552. if (!bd)
  553. return;
  554. /*
  555. * If baud rate is zero, flush queues, and set mval to drop DTR.
  556. */
  557. if ((ch->ch_c_cflag & (CBAUD)) == 0) {
  558. ch->ch_r_head = 0;
  559. ch->ch_r_tail = 0;
  560. ch->ch_e_head = 0;
  561. ch->ch_e_tail = 0;
  562. cls_flush_uart_write(ch);
  563. cls_flush_uart_read(ch);
  564. /* The baudrate is B0 so all modem lines are to be dropped. */
  565. ch->ch_flags |= (CH_BAUD0);
  566. ch->ch_mostat &= ~(UART_MCR_RTS | UART_MCR_DTR);
  567. cls_assert_modem_signals(ch);
  568. return;
  569. }
  570. cflag = C_BAUD(ch->uart_port.state->port.tty);
  571. baud = 9600;
  572. for (i = 0; i < ARRAY_SIZE(baud_rates); i++) {
  573. if (baud_rates[i].cflag == cflag) {
  574. baud = baud_rates[i].rate;
  575. break;
  576. }
  577. }
  578. if (ch->ch_flags & CH_BAUD0)
  579. ch->ch_flags &= ~(CH_BAUD0);
  580. if (ch->ch_c_cflag & PARENB)
  581. lcr |= UART_LCR_PARITY;
  582. if (!(ch->ch_c_cflag & PARODD))
  583. lcr |= UART_LCR_EPAR;
  584. /*
  585. * Not all platforms support mark/space parity,
  586. * so this will hide behind an ifdef.
  587. */
  588. #ifdef CMSPAR
  589. if (ch->ch_c_cflag & CMSPAR)
  590. lcr |= UART_LCR_SPAR;
  591. #endif
  592. if (ch->ch_c_cflag & CSTOPB)
  593. lcr |= UART_LCR_STOP;
  594. switch (ch->ch_c_cflag & CSIZE) {
  595. case CS5:
  596. lcr |= UART_LCR_WLEN5;
  597. break;
  598. case CS6:
  599. lcr |= UART_LCR_WLEN6;
  600. break;
  601. case CS7:
  602. lcr |= UART_LCR_WLEN7;
  603. break;
  604. case CS8:
  605. default:
  606. lcr |= UART_LCR_WLEN8;
  607. break;
  608. }
  609. ier = readb(&ch->ch_cls_uart->ier);
  610. uart_lcr = readb(&ch->ch_cls_uart->lcr);
  611. quot = ch->ch_bd->bd_dividend / baud;
  612. if (quot != 0) {
  613. writeb(UART_LCR_DLAB, &ch->ch_cls_uart->lcr);
  614. writeb((quot & 0xff), &ch->ch_cls_uart->txrx);
  615. writeb((quot >> 8), &ch->ch_cls_uart->ier);
  616. writeb(lcr, &ch->ch_cls_uart->lcr);
  617. }
  618. if (uart_lcr != lcr)
  619. writeb(lcr, &ch->ch_cls_uart->lcr);
  620. if (ch->ch_c_cflag & CREAD)
  621. ier |= (UART_IER_RDI | UART_IER_RLSI);
  622. ier |= (UART_IER_THRI | UART_IER_MSI);
  623. writeb(ier, &ch->ch_cls_uart->ier);
  624. if (ch->ch_c_cflag & CRTSCTS)
  625. cls_set_cts_flow_control(ch);
  626. else if (ch->ch_c_iflag & IXON) {
  627. /*
  628. * If start/stop is set to disable,
  629. * then we should disable flow control.
  630. */
  631. if ((ch->ch_startc == __DISABLED_CHAR) ||
  632. (ch->ch_stopc == __DISABLED_CHAR))
  633. cls_set_no_output_flow_control(ch);
  634. else
  635. cls_set_ixon_flow_control(ch);
  636. } else
  637. cls_set_no_output_flow_control(ch);
  638. if (ch->ch_c_cflag & CRTSCTS)
  639. cls_set_rts_flow_control(ch);
  640. else if (ch->ch_c_iflag & IXOFF) {
  641. /*
  642. * If start/stop is set to disable,
  643. * then we should disable flow control.
  644. */
  645. if ((ch->ch_startc == __DISABLED_CHAR) ||
  646. (ch->ch_stopc == __DISABLED_CHAR))
  647. cls_set_no_input_flow_control(ch);
  648. else
  649. cls_set_ixoff_flow_control(ch);
  650. } else
  651. cls_set_no_input_flow_control(ch);
  652. cls_assert_modem_signals(ch);
  653. /* get current status of the modem signals now */
  654. cls_parse_modem(ch, readb(&ch->ch_cls_uart->msr));
  655. }
  656. /*
  657. * cls_intr()
  658. *
  659. * Classic specific interrupt handler.
  660. */
  661. static irqreturn_t cls_intr(int irq, void *voidbrd)
  662. {
  663. struct jsm_board *brd = voidbrd;
  664. unsigned long lock_flags;
  665. unsigned char uart_poll;
  666. uint i = 0;
  667. /* Lock out the slow poller from running on this board. */
  668. spin_lock_irqsave(&brd->bd_intr_lock, lock_flags);
  669. /*
  670. * Check the board's global interrupt offset to see if we
  671. * acctually do have an interrupt pending on us.
  672. */
  673. uart_poll = readb(brd->re_map_membase + UART_CLASSIC_POLL_ADDR_OFFSET);
  674. jsm_dbg(INTR, &brd->pci_dev, "%s:%d uart_poll: %x\n",
  675. __FILE__, __LINE__, uart_poll);
  676. if (!uart_poll) {
  677. jsm_dbg(INTR, &brd->pci_dev,
  678. "Kernel interrupted to me, but no pending interrupts...\n");
  679. spin_unlock_irqrestore(&brd->bd_intr_lock, lock_flags);
  680. return IRQ_NONE;
  681. }
  682. /* At this point, we have at least SOMETHING to service, dig further. */
  683. /* Parse each port to find out what caused the interrupt */
  684. for (i = 0; i < brd->nasync; i++)
  685. cls_parse_isr(brd, i);
  686. spin_unlock_irqrestore(&brd->bd_intr_lock, lock_flags);
  687. return IRQ_HANDLED;
  688. }
  689. /* Inits UART */
  690. static void cls_uart_init(struct jsm_channel *ch)
  691. {
  692. unsigned char lcrb = readb(&ch->ch_cls_uart->lcr);
  693. unsigned char isr_fcr = 0;
  694. writeb(0, &ch->ch_cls_uart->ier);
  695. /*
  696. * The Enhanced Register Set may only be accessed when
  697. * the Line Control Register is set to 0xBFh.
  698. */
  699. writeb(UART_EXAR654_ENHANCED_REGISTER_SET, &ch->ch_cls_uart->lcr);
  700. isr_fcr = readb(&ch->ch_cls_uart->isr_fcr);
  701. /* Turn on Enhanced/Extended controls */
  702. isr_fcr |= (UART_EXAR654_EFR_ECB);
  703. writeb(isr_fcr, &ch->ch_cls_uart->isr_fcr);
  704. /* Write old LCR value back out, which turns enhanced access off */
  705. writeb(lcrb, &ch->ch_cls_uart->lcr);
  706. /* Clear out UART and FIFO */
  707. readb(&ch->ch_cls_uart->txrx);
  708. writeb((UART_FCR_ENABLE_FIFO|UART_FCR_CLEAR_RCVR|UART_FCR_CLEAR_XMIT),
  709. &ch->ch_cls_uart->isr_fcr);
  710. udelay(10);
  711. ch->ch_flags |= (CH_FIFO_ENABLED | CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
  712. readb(&ch->ch_cls_uart->lsr);
  713. readb(&ch->ch_cls_uart->msr);
  714. }
  715. /*
  716. * Turns off UART.
  717. */
  718. static void cls_uart_off(struct jsm_channel *ch)
  719. {
  720. /* Stop all interrupts from accurring. */
  721. writeb(0, &ch->ch_cls_uart->ier);
  722. }
  723. /*
  724. * cls_get_uarts_bytes_left.
  725. * Returns 0 is nothing left in the FIFO, returns 1 otherwise.
  726. *
  727. * The channel lock MUST be held by the calling function.
  728. */
  729. static u32 cls_get_uart_bytes_left(struct jsm_channel *ch)
  730. {
  731. u8 left = 0;
  732. u8 lsr = readb(&ch->ch_cls_uart->lsr);
  733. /* Determine whether the Transmitter is empty or not */
  734. if (!(lsr & UART_LSR_TEMT))
  735. left = 1;
  736. else {
  737. ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
  738. left = 0;
  739. }
  740. return left;
  741. }
  742. /*
  743. * cls_send_break.
  744. * Starts sending a break thru the UART.
  745. *
  746. * The channel lock MUST be held by the calling function.
  747. */
  748. static void cls_send_break(struct jsm_channel *ch)
  749. {
  750. /* Tell the UART to start sending the break */
  751. if (!(ch->ch_flags & CH_BREAK_SENDING)) {
  752. u8 temp = readb(&ch->ch_cls_uart->lcr);
  753. writeb((temp | UART_LCR_SBC), &ch->ch_cls_uart->lcr);
  754. ch->ch_flags |= (CH_BREAK_SENDING);
  755. }
  756. }
  757. /*
  758. * cls_send_immediate_char.
  759. * Sends a specific character as soon as possible to the UART,
  760. * jumping over any bytes that might be in the write queue.
  761. *
  762. * The channel lock MUST be held by the calling function.
  763. */
  764. static void cls_send_immediate_char(struct jsm_channel *ch, unsigned char c)
  765. {
  766. writeb(c, &ch->ch_cls_uart->txrx);
  767. }
  768. struct board_ops jsm_cls_ops = {
  769. .intr = cls_intr,
  770. .uart_init = cls_uart_init,
  771. .uart_off = cls_uart_off,
  772. .param = cls_param,
  773. .assert_modem_signals = cls_assert_modem_signals,
  774. .flush_uart_write = cls_flush_uart_write,
  775. .flush_uart_read = cls_flush_uart_read,
  776. .disable_receiver = cls_disable_receiver,
  777. .enable_receiver = cls_enable_receiver,
  778. .send_break = cls_send_break,
  779. .clear_break = cls_clear_break,
  780. .send_start_character = cls_send_start_character,
  781. .send_stop_character = cls_send_stop_character,
  782. .copy_data_from_queue_to_uart = cls_copy_data_from_queue_to_uart,
  783. .get_uart_bytes_left = cls_get_uart_bytes_left,
  784. .send_immediate_char = cls_send_immediate_char
  785. };