sunsab.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. /* sunsab.c: ASYNC Driver for the SIEMENS SAB82532 DUSCC.
  2. *
  3. * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
  4. * Copyright (C) 2002 David S. Miller (davem@redhat.com)
  5. *
  6. * Rewrote buffer handling to use CIRC(Circular Buffer) macros.
  7. * Maxim Krasnyanskiy <maxk@qualcomm.com>
  8. *
  9. * Fixed to use tty_get_baud_rate, and to allow for arbitrary baud
  10. * rates to be programmed into the UART. Also eliminated a lot of
  11. * duplicated code in the console setup.
  12. * Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
  13. *
  14. * Ported to new 2.5.x UART layer.
  15. * David S. Miller <davem@redhat.com>
  16. */
  17. #include <linux/config.h>
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/errno.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_flip.h>
  24. #include <linux/major.h>
  25. #include <linux/string.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/ioport.h>
  28. #include <linux/circ_buf.h>
  29. #include <linux/serial.h>
  30. #include <linux/sysrq.h>
  31. #include <linux/console.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/slab.h>
  34. #include <linux/delay.h>
  35. #include <linux/init.h>
  36. #include <asm/io.h>
  37. #include <asm/irq.h>
  38. #include <asm/oplib.h>
  39. #include <asm/ebus.h>
  40. #if defined(CONFIG_SERIAL_SUNZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  41. #define SUPPORT_SYSRQ
  42. #endif
  43. #include <linux/serial_core.h>
  44. #include "suncore.h"
  45. #include "sunsab.h"
  46. struct uart_sunsab_port {
  47. struct uart_port port; /* Generic UART port */
  48. union sab82532_async_regs __iomem *regs; /* Chip registers */
  49. unsigned long irqflags; /* IRQ state flags */
  50. int dsr; /* Current DSR state */
  51. unsigned int cec_timeout; /* Chip poll timeout... */
  52. unsigned int tec_timeout; /* likewise */
  53. unsigned char interrupt_mask0;/* ISR0 masking */
  54. unsigned char interrupt_mask1;/* ISR1 masking */
  55. unsigned char pvr_dtr_bit; /* Which PVR bit is DTR */
  56. unsigned char pvr_dsr_bit; /* Which PVR bit is DSR */
  57. int type; /* SAB82532 version */
  58. /* Setting configuration bits while the transmitter is active
  59. * can cause garbage characters to get emitted by the chip.
  60. * Therefore, we cache such writes here and do the real register
  61. * write the next time the transmitter becomes idle.
  62. */
  63. unsigned int cached_ebrg;
  64. unsigned char cached_mode;
  65. unsigned char cached_pvr;
  66. unsigned char cached_dafo;
  67. };
  68. /*
  69. * This assumes you have a 29.4912 MHz clock for your UART.
  70. */
  71. #define SAB_BASE_BAUD ( 29491200 / 16 )
  72. static char *sab82532_version[16] = {
  73. "V1.0", "V2.0", "V3.2", "V(0x03)",
  74. "V(0x04)", "V(0x05)", "V(0x06)", "V(0x07)",
  75. "V(0x08)", "V(0x09)", "V(0x0a)", "V(0x0b)",
  76. "V(0x0c)", "V(0x0d)", "V(0x0e)", "V(0x0f)"
  77. };
  78. #define SAB82532_MAX_TEC_TIMEOUT 200000 /* 1 character time (at 50 baud) */
  79. #define SAB82532_MAX_CEC_TIMEOUT 50000 /* 2.5 TX CLKs (at 50 baud) */
  80. #define SAB82532_RECV_FIFO_SIZE 32 /* Standard async fifo sizes */
  81. #define SAB82532_XMIT_FIFO_SIZE 32
  82. static __inline__ void sunsab_tec_wait(struct uart_sunsab_port *up)
  83. {
  84. int timeout = up->tec_timeout;
  85. while ((readb(&up->regs->r.star) & SAB82532_STAR_TEC) && --timeout)
  86. udelay(1);
  87. }
  88. static __inline__ void sunsab_cec_wait(struct uart_sunsab_port *up)
  89. {
  90. int timeout = up->cec_timeout;
  91. while ((readb(&up->regs->r.star) & SAB82532_STAR_CEC) && --timeout)
  92. udelay(1);
  93. }
  94. static struct tty_struct *
  95. receive_chars(struct uart_sunsab_port *up,
  96. union sab82532_irq_status *stat,
  97. struct pt_regs *regs)
  98. {
  99. struct tty_struct *tty = NULL;
  100. unsigned char buf[32];
  101. int saw_console_brk = 0;
  102. int free_fifo = 0;
  103. int count = 0;
  104. int i;
  105. if (up->port.info != NULL) /* Unopened serial console */
  106. tty = up->port.info->tty;
  107. /* Read number of BYTES (Character + Status) available. */
  108. if (stat->sreg.isr0 & SAB82532_ISR0_RPF) {
  109. count = SAB82532_RECV_FIFO_SIZE;
  110. free_fifo++;
  111. }
  112. if (stat->sreg.isr0 & SAB82532_ISR0_TCD) {
  113. count = readb(&up->regs->r.rbcl) & (SAB82532_RECV_FIFO_SIZE - 1);
  114. free_fifo++;
  115. }
  116. /* Issue a FIFO read command in case we where idle. */
  117. if (stat->sreg.isr0 & SAB82532_ISR0_TIME) {
  118. sunsab_cec_wait(up);
  119. writeb(SAB82532_CMDR_RFRD, &up->regs->w.cmdr);
  120. return tty;
  121. }
  122. if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
  123. free_fifo++;
  124. /* Read the FIFO. */
  125. for (i = 0; i < count; i++)
  126. buf[i] = readb(&up->regs->r.rfifo[i]);
  127. /* Issue Receive Message Complete command. */
  128. if (free_fifo) {
  129. sunsab_cec_wait(up);
  130. writeb(SAB82532_CMDR_RMC, &up->regs->w.cmdr);
  131. }
  132. /* Count may be zero for BRK, so we check for it here */
  133. if ((stat->sreg.isr1 & SAB82532_ISR1_BRK) &&
  134. (up->port.line == up->port.cons->index))
  135. saw_console_brk = 1;
  136. for (i = 0; i < count; i++) {
  137. unsigned char ch = buf[i], flag;
  138. if (tty == NULL) {
  139. uart_handle_sysrq_char(&up->port, ch, regs);
  140. continue;
  141. }
  142. flag = TTY_NORMAL;
  143. up->port.icount.rx++;
  144. if (unlikely(stat->sreg.isr0 & (SAB82532_ISR0_PERR |
  145. SAB82532_ISR0_FERR |
  146. SAB82532_ISR0_RFO)) ||
  147. unlikely(stat->sreg.isr1 & SAB82532_ISR1_BRK)) {
  148. /*
  149. * For statistics only
  150. */
  151. if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
  152. stat->sreg.isr0 &= ~(SAB82532_ISR0_PERR |
  153. SAB82532_ISR0_FERR);
  154. up->port.icount.brk++;
  155. /*
  156. * We do the SysRQ and SAK checking
  157. * here because otherwise the break
  158. * may get masked by ignore_status_mask
  159. * or read_status_mask.
  160. */
  161. if (uart_handle_break(&up->port))
  162. continue;
  163. } else if (stat->sreg.isr0 & SAB82532_ISR0_PERR)
  164. up->port.icount.parity++;
  165. else if (stat->sreg.isr0 & SAB82532_ISR0_FERR)
  166. up->port.icount.frame++;
  167. if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
  168. up->port.icount.overrun++;
  169. /*
  170. * Mask off conditions which should be ingored.
  171. */
  172. stat->sreg.isr0 &= (up->port.read_status_mask & 0xff);
  173. stat->sreg.isr1 &= ((up->port.read_status_mask >> 8) & 0xff);
  174. if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
  175. flag = TTY_BREAK;
  176. } else if (stat->sreg.isr0 & SAB82532_ISR0_PERR)
  177. flag = TTY_PARITY;
  178. else if (stat->sreg.isr0 & SAB82532_ISR0_FERR)
  179. flag = TTY_FRAME;
  180. }
  181. if (uart_handle_sysrq_char(&up->port, ch, regs))
  182. continue;
  183. if ((stat->sreg.isr0 & (up->port.ignore_status_mask & 0xff)) == 0 &&
  184. (stat->sreg.isr1 & ((up->port.ignore_status_mask >> 8) & 0xff)) == 0)
  185. tty_insert_flip_char(tty, ch, flag);
  186. if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
  187. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  188. }
  189. if (saw_console_brk)
  190. sun_do_break();
  191. return tty;
  192. }
  193. static void sunsab_stop_tx(struct uart_port *);
  194. static void sunsab_tx_idle(struct uart_sunsab_port *);
  195. static void transmit_chars(struct uart_sunsab_port *up,
  196. union sab82532_irq_status *stat)
  197. {
  198. struct circ_buf *xmit = &up->port.info->xmit;
  199. int i;
  200. if (stat->sreg.isr1 & SAB82532_ISR1_ALLS) {
  201. up->interrupt_mask1 |= SAB82532_IMR1_ALLS;
  202. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  203. set_bit(SAB82532_ALLS, &up->irqflags);
  204. }
  205. #if 0 /* bde@nwlink.com says this check causes problems */
  206. if (!(stat->sreg.isr1 & SAB82532_ISR1_XPR))
  207. return;
  208. #endif
  209. if (!(readb(&up->regs->r.star) & SAB82532_STAR_XFW))
  210. return;
  211. set_bit(SAB82532_XPR, &up->irqflags);
  212. sunsab_tx_idle(up);
  213. if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
  214. up->interrupt_mask1 |= SAB82532_IMR1_XPR;
  215. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  216. return;
  217. }
  218. up->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS|SAB82532_IMR1_XPR);
  219. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  220. clear_bit(SAB82532_ALLS, &up->irqflags);
  221. /* Stuff 32 bytes into Transmit FIFO. */
  222. clear_bit(SAB82532_XPR, &up->irqflags);
  223. for (i = 0; i < up->port.fifosize; i++) {
  224. writeb(xmit->buf[xmit->tail],
  225. &up->regs->w.xfifo[i]);
  226. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  227. up->port.icount.tx++;
  228. if (uart_circ_empty(xmit))
  229. break;
  230. }
  231. /* Issue a Transmit Frame command. */
  232. sunsab_cec_wait(up);
  233. writeb(SAB82532_CMDR_XF, &up->regs->w.cmdr);
  234. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  235. uart_write_wakeup(&up->port);
  236. if (uart_circ_empty(xmit))
  237. sunsab_stop_tx(&up->port);
  238. }
  239. static void check_status(struct uart_sunsab_port *up,
  240. union sab82532_irq_status *stat)
  241. {
  242. if (stat->sreg.isr0 & SAB82532_ISR0_CDSC)
  243. uart_handle_dcd_change(&up->port,
  244. !(readb(&up->regs->r.vstr) & SAB82532_VSTR_CD));
  245. if (stat->sreg.isr1 & SAB82532_ISR1_CSC)
  246. uart_handle_cts_change(&up->port,
  247. (readb(&up->regs->r.star) & SAB82532_STAR_CTS));
  248. if ((readb(&up->regs->r.pvr) & up->pvr_dsr_bit) ^ up->dsr) {
  249. up->dsr = (readb(&up->regs->r.pvr) & up->pvr_dsr_bit) ? 0 : 1;
  250. up->port.icount.dsr++;
  251. }
  252. wake_up_interruptible(&up->port.info->delta_msr_wait);
  253. }
  254. static irqreturn_t sunsab_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  255. {
  256. struct uart_sunsab_port *up = dev_id;
  257. struct tty_struct *tty;
  258. union sab82532_irq_status status;
  259. unsigned long flags;
  260. spin_lock_irqsave(&up->port.lock, flags);
  261. status.stat = 0;
  262. if (readb(&up->regs->r.gis) & SAB82532_GIS_ISA0)
  263. status.sreg.isr0 = readb(&up->regs->r.isr0);
  264. if (readb(&up->regs->r.gis) & SAB82532_GIS_ISA1)
  265. status.sreg.isr1 = readb(&up->regs->r.isr1);
  266. tty = NULL;
  267. if (status.stat) {
  268. if ((status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
  269. SAB82532_ISR0_RFO | SAB82532_ISR0_RPF)) ||
  270. (status.sreg.isr1 & SAB82532_ISR1_BRK))
  271. tty = receive_chars(up, &status, regs);
  272. if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
  273. (status.sreg.isr1 & SAB82532_ISR1_CSC))
  274. check_status(up, &status);
  275. if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
  276. transmit_chars(up, &status);
  277. }
  278. spin_unlock(&up->port.lock);
  279. if (tty)
  280. tty_flip_buffer_push(tty);
  281. up++;
  282. spin_lock(&up->port.lock);
  283. status.stat = 0;
  284. if (readb(&up->regs->r.gis) & SAB82532_GIS_ISB0)
  285. status.sreg.isr0 = readb(&up->regs->r.isr0);
  286. if (readb(&up->regs->r.gis) & SAB82532_GIS_ISB1)
  287. status.sreg.isr1 = readb(&up->regs->r.isr1);
  288. tty = NULL;
  289. if (status.stat) {
  290. if ((status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
  291. SAB82532_ISR0_RFO | SAB82532_ISR0_RPF)) ||
  292. (status.sreg.isr1 & SAB82532_ISR1_BRK))
  293. tty = receive_chars(up, &status, regs);
  294. if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
  295. (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
  296. check_status(up, &status);
  297. if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
  298. transmit_chars(up, &status);
  299. }
  300. spin_unlock_irqrestore(&up->port.lock, flags);
  301. if (tty)
  302. tty_flip_buffer_push(tty);
  303. return IRQ_HANDLED;
  304. }
  305. /* port->lock is not held. */
  306. static unsigned int sunsab_tx_empty(struct uart_port *port)
  307. {
  308. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  309. int ret;
  310. /* Do not need a lock for a state test like this. */
  311. if (test_bit(SAB82532_ALLS, &up->irqflags))
  312. ret = TIOCSER_TEMT;
  313. else
  314. ret = 0;
  315. return ret;
  316. }
  317. /* port->lock held by caller. */
  318. static void sunsab_set_mctrl(struct uart_port *port, unsigned int mctrl)
  319. {
  320. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  321. if (mctrl & TIOCM_RTS) {
  322. up->cached_mode &= ~SAB82532_MODE_FRTS;
  323. up->cached_mode |= SAB82532_MODE_RTS;
  324. } else {
  325. up->cached_mode |= (SAB82532_MODE_FRTS |
  326. SAB82532_MODE_RTS);
  327. }
  328. if (mctrl & TIOCM_DTR) {
  329. up->cached_pvr &= ~(up->pvr_dtr_bit);
  330. } else {
  331. up->cached_pvr |= up->pvr_dtr_bit;
  332. }
  333. set_bit(SAB82532_REGS_PENDING, &up->irqflags);
  334. if (test_bit(SAB82532_XPR, &up->irqflags))
  335. sunsab_tx_idle(up);
  336. }
  337. /* port->lock is held by caller and interrupts are disabled. */
  338. static unsigned int sunsab_get_mctrl(struct uart_port *port)
  339. {
  340. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  341. unsigned char val;
  342. unsigned int result;
  343. result = 0;
  344. val = readb(&up->regs->r.pvr);
  345. result |= (val & up->pvr_dsr_bit) ? 0 : TIOCM_DSR;
  346. val = readb(&up->regs->r.vstr);
  347. result |= (val & SAB82532_VSTR_CD) ? 0 : TIOCM_CAR;
  348. val = readb(&up->regs->r.star);
  349. result |= (val & SAB82532_STAR_CTS) ? TIOCM_CTS : 0;
  350. return result;
  351. }
  352. /* port->lock held by caller. */
  353. static void sunsab_stop_tx(struct uart_port *port)
  354. {
  355. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  356. up->interrupt_mask1 |= SAB82532_IMR1_XPR;
  357. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  358. }
  359. /* port->lock held by caller. */
  360. static void sunsab_tx_idle(struct uart_sunsab_port *up)
  361. {
  362. if (test_bit(SAB82532_REGS_PENDING, &up->irqflags)) {
  363. u8 tmp;
  364. clear_bit(SAB82532_REGS_PENDING, &up->irqflags);
  365. writeb(up->cached_mode, &up->regs->rw.mode);
  366. writeb(up->cached_pvr, &up->regs->rw.pvr);
  367. writeb(up->cached_dafo, &up->regs->w.dafo);
  368. writeb(up->cached_ebrg & 0xff, &up->regs->w.bgr);
  369. tmp = readb(&up->regs->rw.ccr2);
  370. tmp &= ~0xc0;
  371. tmp |= (up->cached_ebrg >> 2) & 0xc0;
  372. writeb(tmp, &up->regs->rw.ccr2);
  373. }
  374. }
  375. /* port->lock held by caller. */
  376. static void sunsab_start_tx(struct uart_port *port)
  377. {
  378. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  379. struct circ_buf *xmit = &up->port.info->xmit;
  380. int i;
  381. up->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS|SAB82532_IMR1_XPR);
  382. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  383. if (!test_bit(SAB82532_XPR, &up->irqflags))
  384. return;
  385. clear_bit(SAB82532_ALLS, &up->irqflags);
  386. clear_bit(SAB82532_XPR, &up->irqflags);
  387. for (i = 0; i < up->port.fifosize; i++) {
  388. writeb(xmit->buf[xmit->tail],
  389. &up->regs->w.xfifo[i]);
  390. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  391. up->port.icount.tx++;
  392. if (uart_circ_empty(xmit))
  393. break;
  394. }
  395. /* Issue a Transmit Frame command. */
  396. sunsab_cec_wait(up);
  397. writeb(SAB82532_CMDR_XF, &up->regs->w.cmdr);
  398. }
  399. /* port->lock is not held. */
  400. static void sunsab_send_xchar(struct uart_port *port, char ch)
  401. {
  402. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  403. unsigned long flags;
  404. spin_lock_irqsave(&up->port.lock, flags);
  405. sunsab_tec_wait(up);
  406. writeb(ch, &up->regs->w.tic);
  407. spin_unlock_irqrestore(&up->port.lock, flags);
  408. }
  409. /* port->lock held by caller. */
  410. static void sunsab_stop_rx(struct uart_port *port)
  411. {
  412. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  413. up->interrupt_mask0 |= SAB82532_ISR0_TCD;
  414. writeb(up->interrupt_mask1, &up->regs->w.imr0);
  415. }
  416. /* port->lock held by caller. */
  417. static void sunsab_enable_ms(struct uart_port *port)
  418. {
  419. /* For now we always receive these interrupts. */
  420. }
  421. /* port->lock is not held. */
  422. static void sunsab_break_ctl(struct uart_port *port, int break_state)
  423. {
  424. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  425. unsigned long flags;
  426. unsigned char val;
  427. spin_lock_irqsave(&up->port.lock, flags);
  428. val = up->cached_dafo;
  429. if (break_state)
  430. val |= SAB82532_DAFO_XBRK;
  431. else
  432. val &= ~SAB82532_DAFO_XBRK;
  433. up->cached_dafo = val;
  434. set_bit(SAB82532_REGS_PENDING, &up->irqflags);
  435. if (test_bit(SAB82532_XPR, &up->irqflags))
  436. sunsab_tx_idle(up);
  437. spin_unlock_irqrestore(&up->port.lock, flags);
  438. }
  439. /* port->lock is not held. */
  440. static int sunsab_startup(struct uart_port *port)
  441. {
  442. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  443. unsigned long flags;
  444. unsigned char tmp;
  445. spin_lock_irqsave(&up->port.lock, flags);
  446. /*
  447. * Wait for any commands or immediate characters
  448. */
  449. sunsab_cec_wait(up);
  450. sunsab_tec_wait(up);
  451. /*
  452. * Clear the FIFO buffers.
  453. */
  454. writeb(SAB82532_CMDR_RRES, &up->regs->w.cmdr);
  455. sunsab_cec_wait(up);
  456. writeb(SAB82532_CMDR_XRES, &up->regs->w.cmdr);
  457. /*
  458. * Clear the interrupt registers.
  459. */
  460. (void) readb(&up->regs->r.isr0);
  461. (void) readb(&up->regs->r.isr1);
  462. /*
  463. * Now, initialize the UART
  464. */
  465. writeb(0, &up->regs->w.ccr0); /* power-down */
  466. writeb(SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
  467. SAB82532_CCR0_SM_ASYNC, &up->regs->w.ccr0);
  468. writeb(SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7, &up->regs->w.ccr1);
  469. writeb(SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
  470. SAB82532_CCR2_TOE, &up->regs->w.ccr2);
  471. writeb(0, &up->regs->w.ccr3);
  472. writeb(SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG, &up->regs->w.ccr4);
  473. up->cached_mode = (SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
  474. SAB82532_MODE_RAC);
  475. writeb(up->cached_mode, &up->regs->w.mode);
  476. writeb(SAB82532_RFC_DPS|SAB82532_RFC_RFTH_32, &up->regs->w.rfc);
  477. tmp = readb(&up->regs->rw.ccr0);
  478. tmp |= SAB82532_CCR0_PU; /* power-up */
  479. writeb(tmp, &up->regs->rw.ccr0);
  480. /*
  481. * Finally, enable interrupts
  482. */
  483. up->interrupt_mask0 = (SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
  484. SAB82532_IMR0_PLLA);
  485. writeb(up->interrupt_mask0, &up->regs->w.imr0);
  486. up->interrupt_mask1 = (SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
  487. SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
  488. SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
  489. SAB82532_IMR1_XPR);
  490. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  491. set_bit(SAB82532_ALLS, &up->irqflags);
  492. set_bit(SAB82532_XPR, &up->irqflags);
  493. spin_unlock_irqrestore(&up->port.lock, flags);
  494. return 0;
  495. }
  496. /* port->lock is not held. */
  497. static void sunsab_shutdown(struct uart_port *port)
  498. {
  499. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  500. unsigned long flags;
  501. spin_lock_irqsave(&up->port.lock, flags);
  502. /* Disable Interrupts */
  503. up->interrupt_mask0 = 0xff;
  504. writeb(up->interrupt_mask0, &up->regs->w.imr0);
  505. up->interrupt_mask1 = 0xff;
  506. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  507. /* Disable break condition */
  508. up->cached_dafo = readb(&up->regs->rw.dafo);
  509. up->cached_dafo &= ~SAB82532_DAFO_XBRK;
  510. writeb(up->cached_dafo, &up->regs->rw.dafo);
  511. /* Disable Receiver */
  512. up->cached_mode &= ~SAB82532_MODE_RAC;
  513. writeb(up->cached_mode, &up->regs->rw.mode);
  514. /*
  515. * XXX FIXME
  516. *
  517. * If the chip is powered down here the system hangs/crashes during
  518. * reboot or shutdown. This needs to be investigated further,
  519. * similar behaviour occurs in 2.4 when the driver is configured
  520. * as a module only. One hint may be that data is sometimes
  521. * transmitted at 9600 baud during shutdown (regardless of the
  522. * speed the chip was configured for when the port was open).
  523. */
  524. #if 0
  525. /* Power Down */
  526. tmp = readb(&up->regs->rw.ccr0);
  527. tmp &= ~SAB82532_CCR0_PU;
  528. writeb(tmp, &up->regs->rw.ccr0);
  529. #endif
  530. spin_unlock_irqrestore(&up->port.lock, flags);
  531. }
  532. /*
  533. * This is used to figure out the divisor speeds.
  534. *
  535. * The formula is: Baud = SAB_BASE_BAUD / ((N + 1) * (1 << M)),
  536. *
  537. * with 0 <= N < 64 and 0 <= M < 16
  538. */
  539. static void calc_ebrg(int baud, int *n_ret, int *m_ret)
  540. {
  541. int n, m;
  542. if (baud == 0) {
  543. *n_ret = 0;
  544. *m_ret = 0;
  545. return;
  546. }
  547. /*
  548. * We scale numbers by 10 so that we get better accuracy
  549. * without having to use floating point. Here we increment m
  550. * until n is within the valid range.
  551. */
  552. n = (SAB_BASE_BAUD * 10) / baud;
  553. m = 0;
  554. while (n >= 640) {
  555. n = n / 2;
  556. m++;
  557. }
  558. n = (n+5) / 10;
  559. /*
  560. * We try very hard to avoid speeds with M == 0 since they may
  561. * not work correctly for XTAL frequences above 10 MHz.
  562. */
  563. if ((m == 0) && ((n & 1) == 0)) {
  564. n = n / 2;
  565. m++;
  566. }
  567. *n_ret = n - 1;
  568. *m_ret = m;
  569. }
  570. /* Internal routine, port->lock is held and local interrupts are disabled. */
  571. static void sunsab_convert_to_sab(struct uart_sunsab_port *up, unsigned int cflag,
  572. unsigned int iflag, unsigned int baud,
  573. unsigned int quot)
  574. {
  575. unsigned char dafo;
  576. int bits, n, m;
  577. /* Byte size and parity */
  578. switch (cflag & CSIZE) {
  579. case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
  580. case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
  581. case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
  582. case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
  583. /* Never happens, but GCC is too dumb to figure it out */
  584. default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
  585. }
  586. if (cflag & CSTOPB) {
  587. dafo |= SAB82532_DAFO_STOP;
  588. bits++;
  589. }
  590. if (cflag & PARENB) {
  591. dafo |= SAB82532_DAFO_PARE;
  592. bits++;
  593. }
  594. if (cflag & PARODD) {
  595. dafo |= SAB82532_DAFO_PAR_ODD;
  596. } else {
  597. dafo |= SAB82532_DAFO_PAR_EVEN;
  598. }
  599. up->cached_dafo = dafo;
  600. calc_ebrg(baud, &n, &m);
  601. up->cached_ebrg = n | (m << 6);
  602. up->tec_timeout = (10 * 1000000) / baud;
  603. up->cec_timeout = up->tec_timeout >> 2;
  604. /* CTS flow control flags */
  605. /* We encode read_status_mask and ignore_status_mask like so:
  606. *
  607. * ---------------------
  608. * | ... | ISR1 | ISR0 |
  609. * ---------------------
  610. * .. 15 8 7 0
  611. */
  612. up->port.read_status_mask = (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
  613. SAB82532_ISR0_RFO | SAB82532_ISR0_RPF |
  614. SAB82532_ISR0_CDSC);
  615. up->port.read_status_mask |= (SAB82532_ISR1_CSC |
  616. SAB82532_ISR1_ALLS |
  617. SAB82532_ISR1_XPR) << 8;
  618. if (iflag & INPCK)
  619. up->port.read_status_mask |= (SAB82532_ISR0_PERR |
  620. SAB82532_ISR0_FERR);
  621. if (iflag & (BRKINT | PARMRK))
  622. up->port.read_status_mask |= (SAB82532_ISR1_BRK << 8);
  623. /*
  624. * Characteres to ignore
  625. */
  626. up->port.ignore_status_mask = 0;
  627. if (iflag & IGNPAR)
  628. up->port.ignore_status_mask |= (SAB82532_ISR0_PERR |
  629. SAB82532_ISR0_FERR);
  630. if (iflag & IGNBRK) {
  631. up->port.ignore_status_mask |= (SAB82532_ISR1_BRK << 8);
  632. /*
  633. * If we're ignoring parity and break indicators,
  634. * ignore overruns too (for real raw support).
  635. */
  636. if (iflag & IGNPAR)
  637. up->port.ignore_status_mask |= SAB82532_ISR0_RFO;
  638. }
  639. /*
  640. * ignore all characters if CREAD is not set
  641. */
  642. if ((cflag & CREAD) == 0)
  643. up->port.ignore_status_mask |= (SAB82532_ISR0_RPF |
  644. SAB82532_ISR0_TCD);
  645. uart_update_timeout(&up->port, cflag,
  646. (up->port.uartclk / (16 * quot)));
  647. /* Now schedule a register update when the chip's
  648. * transmitter is idle.
  649. */
  650. up->cached_mode |= SAB82532_MODE_RAC;
  651. set_bit(SAB82532_REGS_PENDING, &up->irqflags);
  652. if (test_bit(SAB82532_XPR, &up->irqflags))
  653. sunsab_tx_idle(up);
  654. }
  655. /* port->lock is not held. */
  656. static void sunsab_set_termios(struct uart_port *port, struct termios *termios,
  657. struct termios *old)
  658. {
  659. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  660. unsigned long flags;
  661. unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
  662. unsigned int quot = uart_get_divisor(port, baud);
  663. spin_lock_irqsave(&up->port.lock, flags);
  664. sunsab_convert_to_sab(up, termios->c_cflag, termios->c_iflag, baud, quot);
  665. spin_unlock_irqrestore(&up->port.lock, flags);
  666. }
  667. static const char *sunsab_type(struct uart_port *port)
  668. {
  669. struct uart_sunsab_port *up = (void *)port;
  670. static char buf[36];
  671. sprintf(buf, "SAB82532 %s", sab82532_version[up->type]);
  672. return buf;
  673. }
  674. static void sunsab_release_port(struct uart_port *port)
  675. {
  676. }
  677. static int sunsab_request_port(struct uart_port *port)
  678. {
  679. return 0;
  680. }
  681. static void sunsab_config_port(struct uart_port *port, int flags)
  682. {
  683. }
  684. static int sunsab_verify_port(struct uart_port *port, struct serial_struct *ser)
  685. {
  686. return -EINVAL;
  687. }
  688. static struct uart_ops sunsab_pops = {
  689. .tx_empty = sunsab_tx_empty,
  690. .set_mctrl = sunsab_set_mctrl,
  691. .get_mctrl = sunsab_get_mctrl,
  692. .stop_tx = sunsab_stop_tx,
  693. .start_tx = sunsab_start_tx,
  694. .send_xchar = sunsab_send_xchar,
  695. .stop_rx = sunsab_stop_rx,
  696. .enable_ms = sunsab_enable_ms,
  697. .break_ctl = sunsab_break_ctl,
  698. .startup = sunsab_startup,
  699. .shutdown = sunsab_shutdown,
  700. .set_termios = sunsab_set_termios,
  701. .type = sunsab_type,
  702. .release_port = sunsab_release_port,
  703. .request_port = sunsab_request_port,
  704. .config_port = sunsab_config_port,
  705. .verify_port = sunsab_verify_port,
  706. };
  707. static struct uart_driver sunsab_reg = {
  708. .owner = THIS_MODULE,
  709. .driver_name = "serial",
  710. .devfs_name = "tts/",
  711. .dev_name = "ttyS",
  712. .major = TTY_MAJOR,
  713. };
  714. static struct uart_sunsab_port *sunsab_ports;
  715. static int num_channels;
  716. #ifdef CONFIG_SERIAL_SUNSAB_CONSOLE
  717. static void sunsab_console_putchar(struct uart_port *port, int c)
  718. {
  719. struct uart_sunsab_port *up = (struct uart_sunsab_port *)port;
  720. unsigned long flags;
  721. spin_lock_irqsave(&up->port.lock, flags);
  722. sunsab_tec_wait(up);
  723. writeb(c, &up->regs->w.tic);
  724. spin_unlock_irqrestore(&up->port.lock, flags);
  725. }
  726. static void sunsab_console_write(struct console *con, const char *s, unsigned n)
  727. {
  728. struct uart_sunsab_port *up = &sunsab_ports[con->index];
  729. uart_console_write(&up->port, s, n, sunsab_console_putchar);
  730. sunsab_tec_wait(up);
  731. }
  732. static int sunsab_console_setup(struct console *con, char *options)
  733. {
  734. struct uart_sunsab_port *up = &sunsab_ports[con->index];
  735. unsigned long flags;
  736. unsigned int baud, quot;
  737. printk("Console: ttyS%d (SAB82532)\n",
  738. (sunsab_reg.minor - 64) + con->index);
  739. sunserial_console_termios(con);
  740. switch (con->cflag & CBAUD) {
  741. case B150: baud = 150; break;
  742. case B300: baud = 300; break;
  743. case B600: baud = 600; break;
  744. case B1200: baud = 1200; break;
  745. case B2400: baud = 2400; break;
  746. case B4800: baud = 4800; break;
  747. default: case B9600: baud = 9600; break;
  748. case B19200: baud = 19200; break;
  749. case B38400: baud = 38400; break;
  750. case B57600: baud = 57600; break;
  751. case B115200: baud = 115200; break;
  752. case B230400: baud = 230400; break;
  753. case B460800: baud = 460800; break;
  754. };
  755. /*
  756. * Temporary fix.
  757. */
  758. spin_lock_init(&up->port.lock);
  759. /*
  760. * Initialize the hardware
  761. */
  762. sunsab_startup(&up->port);
  763. spin_lock_irqsave(&up->port.lock, flags);
  764. /*
  765. * Finally, enable interrupts
  766. */
  767. up->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
  768. SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
  769. writeb(up->interrupt_mask0, &up->regs->w.imr0);
  770. up->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
  771. SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
  772. SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
  773. SAB82532_IMR1_XPR;
  774. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  775. quot = uart_get_divisor(&up->port, baud);
  776. sunsab_convert_to_sab(up, con->cflag, 0, baud, quot);
  777. sunsab_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
  778. spin_unlock_irqrestore(&up->port.lock, flags);
  779. return 0;
  780. }
  781. static struct console sunsab_console = {
  782. .name = "ttyS",
  783. .write = sunsab_console_write,
  784. .device = uart_console_device,
  785. .setup = sunsab_console_setup,
  786. .flags = CON_PRINTBUFFER,
  787. .index = -1,
  788. .data = &sunsab_reg,
  789. };
  790. static inline struct console *SUNSAB_CONSOLE(void)
  791. {
  792. int i;
  793. if (con_is_present())
  794. return NULL;
  795. for (i = 0; i < num_channels; i++) {
  796. int this_minor = sunsab_reg.minor + i;
  797. if ((this_minor - 64) == (serial_console - 1))
  798. break;
  799. }
  800. if (i == num_channels)
  801. return NULL;
  802. sunsab_console.index = i;
  803. return &sunsab_console;
  804. }
  805. #else
  806. #define SUNSAB_CONSOLE() (NULL)
  807. #define sunsab_console_init() do { } while (0)
  808. #endif
  809. static void __init for_each_sab_edev(void (*callback)(struct linux_ebus_device *, void *), void *arg)
  810. {
  811. struct linux_ebus *ebus;
  812. struct linux_ebus_device *edev = NULL;
  813. for_each_ebus(ebus) {
  814. for_each_ebusdev(edev, ebus) {
  815. if (!strcmp(edev->prom_name, "se")) {
  816. callback(edev, arg);
  817. continue;
  818. } else if (!strcmp(edev->prom_name, "serial")) {
  819. char compat[32];
  820. int clen;
  821. /* On RIO this can be an SE, check it. We could
  822. * just check ebus->is_rio, but this is more portable.
  823. */
  824. clen = prom_getproperty(edev->prom_node, "compatible",
  825. compat, sizeof(compat));
  826. if (clen > 0) {
  827. if (strncmp(compat, "sab82532", 8) == 0) {
  828. callback(edev, arg);
  829. continue;
  830. }
  831. }
  832. }
  833. }
  834. }
  835. }
  836. static void __init sab_count_callback(struct linux_ebus_device *edev, void *arg)
  837. {
  838. int *count_p = arg;
  839. (*count_p)++;
  840. }
  841. static void __init sab_attach_callback(struct linux_ebus_device *edev, void *arg)
  842. {
  843. int *instance_p = arg;
  844. struct uart_sunsab_port *up;
  845. unsigned long regs, offset;
  846. int i;
  847. /* Note: ports are located in reverse order */
  848. regs = edev->resource[0].start;
  849. offset = sizeof(union sab82532_async_regs);
  850. for (i = 0; i < 2; i++) {
  851. up = &sunsab_ports[(*instance_p * 2) + 1 - i];
  852. memset(up, 0, sizeof(*up));
  853. up->regs = ioremap(regs + offset, sizeof(union sab82532_async_regs));
  854. up->port.irq = edev->irqs[0];
  855. up->port.fifosize = SAB82532_XMIT_FIFO_SIZE;
  856. up->port.mapbase = (unsigned long)up->regs;
  857. up->port.iotype = UPIO_MEM;
  858. writeb(SAB82532_IPC_IC_ACT_LOW, &up->regs->w.ipc);
  859. offset -= sizeof(union sab82532_async_regs);
  860. }
  861. (*instance_p)++;
  862. }
  863. static int __init probe_for_sabs(void)
  864. {
  865. int this_sab = 0;
  866. /* Find device instances. */
  867. for_each_sab_edev(&sab_count_callback, &this_sab);
  868. if (!this_sab)
  869. return -ENODEV;
  870. /* Allocate tables. */
  871. sunsab_ports = kmalloc(sizeof(struct uart_sunsab_port) * this_sab * 2,
  872. GFP_KERNEL);
  873. if (!sunsab_ports)
  874. return -ENOMEM;
  875. num_channels = this_sab * 2;
  876. this_sab = 0;
  877. for_each_sab_edev(&sab_attach_callback, &this_sab);
  878. return 0;
  879. }
  880. static void __init sunsab_init_hw(void)
  881. {
  882. int i;
  883. for (i = 0; i < num_channels; i++) {
  884. struct uart_sunsab_port *up = &sunsab_ports[i];
  885. up->port.line = i;
  886. up->port.ops = &sunsab_pops;
  887. up->port.type = PORT_SUNSAB;
  888. up->port.uartclk = SAB_BASE_BAUD;
  889. up->type = readb(&up->regs->r.vstr) & 0x0f;
  890. writeb(~((1 << 1) | (1 << 2) | (1 << 4)), &up->regs->w.pcr);
  891. writeb(0xff, &up->regs->w.pim);
  892. if (up->port.line == 0) {
  893. up->pvr_dsr_bit = (1 << 0);
  894. up->pvr_dtr_bit = (1 << 1);
  895. } else {
  896. up->pvr_dsr_bit = (1 << 3);
  897. up->pvr_dtr_bit = (1 << 2);
  898. }
  899. up->cached_pvr = (1 << 1) | (1 << 2) | (1 << 4);
  900. writeb(up->cached_pvr, &up->regs->w.pvr);
  901. up->cached_mode = readb(&up->regs->rw.mode);
  902. up->cached_mode |= SAB82532_MODE_FRTS;
  903. writeb(up->cached_mode, &up->regs->rw.mode);
  904. up->cached_mode |= SAB82532_MODE_RTS;
  905. writeb(up->cached_mode, &up->regs->rw.mode);
  906. up->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
  907. up->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
  908. if (!(up->port.line & 0x01)) {
  909. if (request_irq(up->port.irq, sunsab_interrupt,
  910. SA_SHIRQ, "serial(sab82532)", up)) {
  911. printk("sunsab%d: can't get IRQ %x\n",
  912. i, up->port.irq);
  913. continue;
  914. }
  915. }
  916. }
  917. }
  918. static int __init sunsab_init(void)
  919. {
  920. int ret = probe_for_sabs();
  921. int i;
  922. if (ret < 0)
  923. return ret;
  924. sunsab_init_hw();
  925. sunsab_reg.minor = sunserial_current_minor;
  926. sunsab_reg.nr = num_channels;
  927. ret = uart_register_driver(&sunsab_reg);
  928. if (ret < 0) {
  929. int i;
  930. for (i = 0; i < num_channels; i++) {
  931. struct uart_sunsab_port *up = &sunsab_ports[i];
  932. if (!(up->port.line & 0x01))
  933. free_irq(up->port.irq, up);
  934. iounmap(up->regs);
  935. }
  936. kfree(sunsab_ports);
  937. sunsab_ports = NULL;
  938. return ret;
  939. }
  940. sunsab_reg.tty_driver->name_base = sunsab_reg.minor - 64;
  941. sunsab_reg.cons = SUNSAB_CONSOLE();
  942. sunserial_current_minor += num_channels;
  943. for (i = 0; i < num_channels; i++) {
  944. struct uart_sunsab_port *up = &sunsab_ports[i];
  945. uart_add_one_port(&sunsab_reg, &up->port);
  946. }
  947. return 0;
  948. }
  949. static void __exit sunsab_exit(void)
  950. {
  951. int i;
  952. for (i = 0; i < num_channels; i++) {
  953. struct uart_sunsab_port *up = &sunsab_ports[i];
  954. uart_remove_one_port(&sunsab_reg, &up->port);
  955. if (!(up->port.line & 0x01))
  956. free_irq(up->port.irq, up);
  957. iounmap(up->regs);
  958. }
  959. sunserial_current_minor -= num_channels;
  960. uart_unregister_driver(&sunsab_reg);
  961. kfree(sunsab_ports);
  962. sunsab_ports = NULL;
  963. }
  964. module_init(sunsab_init);
  965. module_exit(sunsab_exit);
  966. MODULE_AUTHOR("Eddie C. Dost and David S. Miller");
  967. MODULE_DESCRIPTION("Sun SAB82532 serial port driver");
  968. MODULE_LICENSE("GPL");