sunsab.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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. };
  59. /*
  60. * This assumes you have a 29.4912 MHz clock for your UART.
  61. */
  62. #define SAB_BASE_BAUD ( 29491200 / 16 )
  63. static char *sab82532_version[16] = {
  64. "V1.0", "V2.0", "V3.2", "V(0x03)",
  65. "V(0x04)", "V(0x05)", "V(0x06)", "V(0x07)",
  66. "V(0x08)", "V(0x09)", "V(0x0a)", "V(0x0b)",
  67. "V(0x0c)", "V(0x0d)", "V(0x0e)", "V(0x0f)"
  68. };
  69. #define SAB82532_MAX_TEC_TIMEOUT 200000 /* 1 character time (at 50 baud) */
  70. #define SAB82532_MAX_CEC_TIMEOUT 50000 /* 2.5 TX CLKs (at 50 baud) */
  71. #define SAB82532_RECV_FIFO_SIZE 32 /* Standard async fifo sizes */
  72. #define SAB82532_XMIT_FIFO_SIZE 32
  73. static __inline__ void sunsab_tec_wait(struct uart_sunsab_port *up)
  74. {
  75. int timeout = up->tec_timeout;
  76. while ((readb(&up->regs->r.star) & SAB82532_STAR_TEC) && --timeout)
  77. udelay(1);
  78. }
  79. static __inline__ void sunsab_cec_wait(struct uart_sunsab_port *up)
  80. {
  81. int timeout = up->cec_timeout;
  82. while ((readb(&up->regs->r.star) & SAB82532_STAR_CEC) && --timeout)
  83. udelay(1);
  84. }
  85. static struct tty_struct *
  86. receive_chars(struct uart_sunsab_port *up,
  87. union sab82532_irq_status *stat,
  88. struct pt_regs *regs)
  89. {
  90. struct tty_struct *tty = NULL;
  91. unsigned char buf[32];
  92. int saw_console_brk = 0;
  93. int free_fifo = 0;
  94. int count = 0;
  95. int i;
  96. if (up->port.info != NULL) /* Unopened serial console */
  97. tty = up->port.info->tty;
  98. /* Read number of BYTES (Character + Status) available. */
  99. if (stat->sreg.isr0 & SAB82532_ISR0_RPF) {
  100. count = SAB82532_RECV_FIFO_SIZE;
  101. free_fifo++;
  102. }
  103. if (stat->sreg.isr0 & SAB82532_ISR0_TCD) {
  104. count = readb(&up->regs->r.rbcl) & (SAB82532_RECV_FIFO_SIZE - 1);
  105. free_fifo++;
  106. }
  107. /* Issue a FIFO read command in case we where idle. */
  108. if (stat->sreg.isr0 & SAB82532_ISR0_TIME) {
  109. sunsab_cec_wait(up);
  110. writeb(SAB82532_CMDR_RFRD, &up->regs->w.cmdr);
  111. return tty;
  112. }
  113. if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
  114. free_fifo++;
  115. /* Read the FIFO. */
  116. for (i = 0; i < count; i++)
  117. buf[i] = readb(&up->regs->r.rfifo[i]);
  118. /* Issue Receive Message Complete command. */
  119. if (free_fifo) {
  120. sunsab_cec_wait(up);
  121. writeb(SAB82532_CMDR_RMC, &up->regs->w.cmdr);
  122. }
  123. /* Count may be zero for BRK, so we check for it here */
  124. if ((stat->sreg.isr1 & SAB82532_ISR1_BRK) &&
  125. (up->port.line == up->port.cons->index))
  126. saw_console_brk = 1;
  127. for (i = 0; i < count; i++) {
  128. unsigned char ch = buf[i];
  129. if (tty == NULL) {
  130. uart_handle_sysrq_char(&up->port, ch, regs);
  131. continue;
  132. }
  133. if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
  134. tty->flip.work.func((void *)tty);
  135. if (tty->flip.count >= TTY_FLIPBUF_SIZE)
  136. return tty; // if TTY_DONT_FLIP is set
  137. }
  138. *tty->flip.char_buf_ptr = ch;
  139. *tty->flip.flag_buf_ptr = TTY_NORMAL;
  140. up->port.icount.rx++;
  141. if (unlikely(stat->sreg.isr0 & (SAB82532_ISR0_PERR |
  142. SAB82532_ISR0_FERR |
  143. SAB82532_ISR0_RFO)) ||
  144. unlikely(stat->sreg.isr1 & SAB82532_ISR1_BRK)) {
  145. /*
  146. * For statistics only
  147. */
  148. if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
  149. stat->sreg.isr0 &= ~(SAB82532_ISR0_PERR |
  150. SAB82532_ISR0_FERR);
  151. up->port.icount.brk++;
  152. /*
  153. * We do the SysRQ and SAK checking
  154. * here because otherwise the break
  155. * may get masked by ignore_status_mask
  156. * or read_status_mask.
  157. */
  158. if (uart_handle_break(&up->port))
  159. continue;
  160. } else if (stat->sreg.isr0 & SAB82532_ISR0_PERR)
  161. up->port.icount.parity++;
  162. else if (stat->sreg.isr0 & SAB82532_ISR0_FERR)
  163. up->port.icount.frame++;
  164. if (stat->sreg.isr0 & SAB82532_ISR0_RFO)
  165. up->port.icount.overrun++;
  166. /*
  167. * Mask off conditions which should be ingored.
  168. */
  169. stat->sreg.isr0 &= (up->port.read_status_mask & 0xff);
  170. stat->sreg.isr1 &= ((up->port.read_status_mask >> 8) & 0xff);
  171. if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
  172. *tty->flip.flag_buf_ptr = TTY_BREAK;
  173. } else if (stat->sreg.isr0 & SAB82532_ISR0_PERR)
  174. *tty->flip.flag_buf_ptr = TTY_PARITY;
  175. else if (stat->sreg.isr0 & SAB82532_ISR0_FERR)
  176. *tty->flip.flag_buf_ptr = TTY_FRAME;
  177. }
  178. if (uart_handle_sysrq_char(&up->port, ch, regs))
  179. continue;
  180. if ((stat->sreg.isr0 & (up->port.ignore_status_mask & 0xff)) == 0 &&
  181. (stat->sreg.isr1 & ((up->port.ignore_status_mask >> 8) & 0xff)) == 0){
  182. tty->flip.flag_buf_ptr++;
  183. tty->flip.char_buf_ptr++;
  184. tty->flip.count++;
  185. }
  186. if ((stat->sreg.isr0 & SAB82532_ISR0_RFO) &&
  187. tty->flip.count < TTY_FLIPBUF_SIZE) {
  188. /*
  189. * Overrun is special, since it's reported
  190. * immediately, and doesn't affect the current
  191. * character.
  192. */
  193. *tty->flip.flag_buf_ptr = TTY_OVERRUN;
  194. tty->flip.flag_buf_ptr++;
  195. tty->flip.char_buf_ptr++;
  196. tty->flip.count++;
  197. }
  198. }
  199. if (saw_console_brk)
  200. sun_do_break();
  201. return tty;
  202. }
  203. static void sunsab_stop_tx(struct uart_port *, unsigned int);
  204. static void transmit_chars(struct uart_sunsab_port *up,
  205. union sab82532_irq_status *stat)
  206. {
  207. struct circ_buf *xmit = &up->port.info->xmit;
  208. int i;
  209. if (stat->sreg.isr1 & SAB82532_ISR1_ALLS) {
  210. up->interrupt_mask1 |= SAB82532_IMR1_ALLS;
  211. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  212. set_bit(SAB82532_ALLS, &up->irqflags);
  213. }
  214. #if 0 /* bde@nwlink.com says this check causes problems */
  215. if (!(stat->sreg.isr1 & SAB82532_ISR1_XPR))
  216. return;
  217. #endif
  218. if (!(readb(&up->regs->r.star) & SAB82532_STAR_XFW))
  219. return;
  220. set_bit(SAB82532_XPR, &up->irqflags);
  221. if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
  222. up->interrupt_mask1 |= SAB82532_IMR1_XPR;
  223. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  224. uart_write_wakeup(&up->port);
  225. return;
  226. }
  227. up->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS|SAB82532_IMR1_XPR);
  228. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  229. clear_bit(SAB82532_ALLS, &up->irqflags);
  230. /* Stuff 32 bytes into Transmit FIFO. */
  231. clear_bit(SAB82532_XPR, &up->irqflags);
  232. for (i = 0; i < up->port.fifosize; i++) {
  233. writeb(xmit->buf[xmit->tail],
  234. &up->regs->w.xfifo[i]);
  235. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  236. up->port.icount.tx++;
  237. if (uart_circ_empty(xmit))
  238. break;
  239. }
  240. /* Issue a Transmit Frame command. */
  241. sunsab_cec_wait(up);
  242. writeb(SAB82532_CMDR_XF, &up->regs->w.cmdr);
  243. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  244. uart_write_wakeup(&up->port);
  245. if (uart_circ_empty(xmit))
  246. sunsab_stop_tx(&up->port, 0);
  247. }
  248. static void check_status(struct uart_sunsab_port *up,
  249. union sab82532_irq_status *stat)
  250. {
  251. if (stat->sreg.isr0 & SAB82532_ISR0_CDSC)
  252. uart_handle_dcd_change(&up->port,
  253. !(readb(&up->regs->r.vstr) & SAB82532_VSTR_CD));
  254. if (stat->sreg.isr1 & SAB82532_ISR1_CSC)
  255. uart_handle_cts_change(&up->port,
  256. (readb(&up->regs->r.star) & SAB82532_STAR_CTS));
  257. if ((readb(&up->regs->r.pvr) & up->pvr_dsr_bit) ^ up->dsr) {
  258. up->dsr = (readb(&up->regs->r.pvr) & up->pvr_dsr_bit) ? 0 : 1;
  259. up->port.icount.dsr++;
  260. }
  261. wake_up_interruptible(&up->port.info->delta_msr_wait);
  262. }
  263. static irqreturn_t sunsab_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  264. {
  265. struct uart_sunsab_port *up = dev_id;
  266. struct tty_struct *tty;
  267. union sab82532_irq_status status;
  268. unsigned long flags;
  269. spin_lock_irqsave(&up->port.lock, flags);
  270. status.stat = 0;
  271. if (readb(&up->regs->r.gis) & SAB82532_GIS_ISA0)
  272. status.sreg.isr0 = readb(&up->regs->r.isr0);
  273. if (readb(&up->regs->r.gis) & SAB82532_GIS_ISA1)
  274. status.sreg.isr1 = readb(&up->regs->r.isr1);
  275. tty = NULL;
  276. if (status.stat) {
  277. if ((status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
  278. SAB82532_ISR0_RFO | SAB82532_ISR0_RPF)) ||
  279. (status.sreg.isr1 & SAB82532_ISR1_BRK))
  280. tty = receive_chars(up, &status, regs);
  281. if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
  282. (status.sreg.isr1 & SAB82532_ISR1_CSC))
  283. check_status(up, &status);
  284. if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
  285. transmit_chars(up, &status);
  286. }
  287. spin_unlock(&up->port.lock);
  288. if (tty)
  289. tty_flip_buffer_push(tty);
  290. up++;
  291. spin_lock(&up->port.lock);
  292. status.stat = 0;
  293. if (readb(&up->regs->r.gis) & SAB82532_GIS_ISB0)
  294. status.sreg.isr0 = readb(&up->regs->r.isr0);
  295. if (readb(&up->regs->r.gis) & SAB82532_GIS_ISB1)
  296. status.sreg.isr1 = readb(&up->regs->r.isr1);
  297. tty = NULL;
  298. if (status.stat) {
  299. if ((status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
  300. SAB82532_ISR0_RFO | SAB82532_ISR0_RPF)) ||
  301. (status.sreg.isr1 & SAB82532_ISR1_BRK))
  302. tty = receive_chars(up, &status, regs);
  303. if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
  304. (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
  305. check_status(up, &status);
  306. if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
  307. transmit_chars(up, &status);
  308. }
  309. spin_unlock_irqrestore(&up->port.lock, flags);
  310. if (tty)
  311. tty_flip_buffer_push(tty);
  312. return IRQ_HANDLED;
  313. }
  314. /* port->lock is not held. */
  315. static unsigned int sunsab_tx_empty(struct uart_port *port)
  316. {
  317. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  318. int ret;
  319. /* Do not need a lock for a state test like this. */
  320. if (test_bit(SAB82532_ALLS, &up->irqflags))
  321. ret = TIOCSER_TEMT;
  322. else
  323. ret = 0;
  324. return ret;
  325. }
  326. /* port->lock held by caller. */
  327. static void sunsab_set_mctrl(struct uart_port *port, unsigned int mctrl)
  328. {
  329. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  330. if (mctrl & TIOCM_RTS) {
  331. writeb(readb(&up->regs->rw.mode) & ~SAB82532_MODE_FRTS,
  332. &up->regs->rw.mode);
  333. writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_RTS,
  334. &up->regs->rw.mode);
  335. } else {
  336. writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_FRTS,
  337. &up->regs->rw.mode);
  338. writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_RTS,
  339. &up->regs->rw.mode);
  340. }
  341. if (mctrl & TIOCM_DTR) {
  342. writeb(readb(&up->regs->rw.pvr) & ~(up->pvr_dtr_bit), &up->regs->rw.pvr);
  343. } else {
  344. writeb(readb(&up->regs->rw.pvr) | up->pvr_dtr_bit, &up->regs->rw.pvr);
  345. }
  346. }
  347. /* port->lock is not held. */
  348. static unsigned int sunsab_get_mctrl(struct uart_port *port)
  349. {
  350. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  351. unsigned long flags;
  352. unsigned char val;
  353. unsigned int result;
  354. result = 0;
  355. spin_lock_irqsave(&up->port.lock, flags);
  356. val = readb(&up->regs->r.pvr);
  357. result |= (val & up->pvr_dsr_bit) ? 0 : TIOCM_DSR;
  358. val = readb(&up->regs->r.vstr);
  359. result |= (val & SAB82532_VSTR_CD) ? 0 : TIOCM_CAR;
  360. val = readb(&up->regs->r.star);
  361. result |= (val & SAB82532_STAR_CTS) ? TIOCM_CTS : 0;
  362. spin_unlock_irqrestore(&up->port.lock, flags);
  363. return result;
  364. }
  365. /* port->lock held by caller. */
  366. static void sunsab_stop_tx(struct uart_port *port, unsigned int tty_stop)
  367. {
  368. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  369. up->interrupt_mask1 |= SAB82532_IMR1_XPR;
  370. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  371. }
  372. /* port->lock held by caller. */
  373. static void sunsab_start_tx(struct uart_port *port, unsigned int tty_start)
  374. {
  375. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  376. struct circ_buf *xmit = &up->port.info->xmit;
  377. int i;
  378. up->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS|SAB82532_IMR1_XPR);
  379. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  380. if (!test_bit(SAB82532_XPR, &up->irqflags))
  381. return;
  382. clear_bit(SAB82532_ALLS, &up->irqflags);
  383. clear_bit(SAB82532_XPR, &up->irqflags);
  384. for (i = 0; i < up->port.fifosize; i++) {
  385. writeb(xmit->buf[xmit->tail],
  386. &up->regs->w.xfifo[i]);
  387. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  388. up->port.icount.tx++;
  389. if (uart_circ_empty(xmit))
  390. break;
  391. }
  392. /* Issue a Transmit Frame command. */
  393. sunsab_cec_wait(up);
  394. writeb(SAB82532_CMDR_XF, &up->regs->w.cmdr);
  395. }
  396. /* port->lock is not held. */
  397. static void sunsab_send_xchar(struct uart_port *port, char ch)
  398. {
  399. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  400. unsigned long flags;
  401. spin_lock_irqsave(&up->port.lock, flags);
  402. sunsab_tec_wait(up);
  403. writeb(ch, &up->regs->w.tic);
  404. spin_unlock_irqrestore(&up->port.lock, flags);
  405. }
  406. /* port->lock held by caller. */
  407. static void sunsab_stop_rx(struct uart_port *port)
  408. {
  409. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  410. up->interrupt_mask0 |= SAB82532_ISR0_TCD;
  411. writeb(up->interrupt_mask1, &up->regs->w.imr0);
  412. }
  413. /* port->lock held by caller. */
  414. static void sunsab_enable_ms(struct uart_port *port)
  415. {
  416. /* For now we always receive these interrupts. */
  417. }
  418. /* port->lock is not held. */
  419. static void sunsab_break_ctl(struct uart_port *port, int break_state)
  420. {
  421. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  422. unsigned long flags;
  423. unsigned char val;
  424. spin_lock_irqsave(&up->port.lock, flags);
  425. val = readb(&up->regs->rw.dafo);
  426. if (break_state)
  427. val |= SAB82532_DAFO_XBRK;
  428. else
  429. val &= ~SAB82532_DAFO_XBRK;
  430. writeb(val, &up->regs->rw.dafo);
  431. spin_unlock_irqrestore(&up->port.lock, flags);
  432. }
  433. /* port->lock is not held. */
  434. static int sunsab_startup(struct uart_port *port)
  435. {
  436. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  437. unsigned long flags;
  438. unsigned char tmp;
  439. spin_lock_irqsave(&up->port.lock, flags);
  440. /*
  441. * Wait for any commands or immediate characters
  442. */
  443. sunsab_cec_wait(up);
  444. sunsab_tec_wait(up);
  445. /*
  446. * Clear the FIFO buffers.
  447. */
  448. writeb(SAB82532_CMDR_RRES, &up->regs->w.cmdr);
  449. sunsab_cec_wait(up);
  450. writeb(SAB82532_CMDR_XRES, &up->regs->w.cmdr);
  451. /*
  452. * Clear the interrupt registers.
  453. */
  454. (void) readb(&up->regs->r.isr0);
  455. (void) readb(&up->regs->r.isr1);
  456. /*
  457. * Now, initialize the UART
  458. */
  459. writeb(0, &up->regs->w.ccr0); /* power-down */
  460. writeb(SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
  461. SAB82532_CCR0_SM_ASYNC, &up->regs->w.ccr0);
  462. writeb(SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7, &up->regs->w.ccr1);
  463. writeb(SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
  464. SAB82532_CCR2_TOE, &up->regs->w.ccr2);
  465. writeb(0, &up->regs->w.ccr3);
  466. writeb(SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG, &up->regs->w.ccr4);
  467. writeb(SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
  468. SAB82532_MODE_RAC, &up->regs->w.mode);
  469. writeb(SAB82532_RFC_DPS|SAB82532_RFC_RFTH_32, &up->regs->w.rfc);
  470. tmp = readb(&up->regs->rw.ccr0);
  471. tmp |= SAB82532_CCR0_PU; /* power-up */
  472. writeb(tmp, &up->regs->rw.ccr0);
  473. /*
  474. * Finally, enable interrupts
  475. */
  476. up->interrupt_mask0 = (SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
  477. SAB82532_IMR0_PLLA);
  478. writeb(up->interrupt_mask0, &up->regs->w.imr0);
  479. up->interrupt_mask1 = (SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
  480. SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
  481. SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
  482. SAB82532_IMR1_XPR);
  483. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  484. set_bit(SAB82532_ALLS, &up->irqflags);
  485. set_bit(SAB82532_XPR, &up->irqflags);
  486. spin_unlock_irqrestore(&up->port.lock, flags);
  487. return 0;
  488. }
  489. /* port->lock is not held. */
  490. static void sunsab_shutdown(struct uart_port *port)
  491. {
  492. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  493. unsigned long flags;
  494. unsigned char tmp;
  495. spin_lock_irqsave(&up->port.lock, flags);
  496. /* Disable Interrupts */
  497. up->interrupt_mask0 = 0xff;
  498. writeb(up->interrupt_mask0, &up->regs->w.imr0);
  499. up->interrupt_mask1 = 0xff;
  500. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  501. /* Disable break condition */
  502. tmp = readb(&up->regs->rw.dafo);
  503. tmp &= ~SAB82532_DAFO_XBRK;
  504. writeb(tmp, &up->regs->rw.dafo);
  505. /* Disable Receiver */
  506. tmp = readb(&up->regs->rw.mode);
  507. tmp &= ~SAB82532_MODE_RAC;
  508. writeb(tmp, &up->regs->rw.mode);
  509. /*
  510. * XXX FIXME
  511. *
  512. * If the chip is powered down here the system hangs/crashes during
  513. * reboot or shutdown. This needs to be investigated further,
  514. * similar behaviour occurs in 2.4 when the driver is configured
  515. * as a module only. One hint may be that data is sometimes
  516. * transmitted at 9600 baud during shutdown (regardless of the
  517. * speed the chip was configured for when the port was open).
  518. */
  519. #if 0
  520. /* Power Down */
  521. tmp = readb(&up->regs->rw.ccr0);
  522. tmp &= ~SAB82532_CCR0_PU;
  523. writeb(tmp, &up->regs->rw.ccr0);
  524. #endif
  525. spin_unlock_irqrestore(&up->port.lock, flags);
  526. }
  527. /*
  528. * This is used to figure out the divisor speeds.
  529. *
  530. * The formula is: Baud = SAB_BASE_BAUD / ((N + 1) * (1 << M)),
  531. *
  532. * with 0 <= N < 64 and 0 <= M < 16
  533. */
  534. static void calc_ebrg(int baud, int *n_ret, int *m_ret)
  535. {
  536. int n, m;
  537. if (baud == 0) {
  538. *n_ret = 0;
  539. *m_ret = 0;
  540. return;
  541. }
  542. /*
  543. * We scale numbers by 10 so that we get better accuracy
  544. * without having to use floating point. Here we increment m
  545. * until n is within the valid range.
  546. */
  547. n = (SAB_BASE_BAUD * 10) / baud;
  548. m = 0;
  549. while (n >= 640) {
  550. n = n / 2;
  551. m++;
  552. }
  553. n = (n+5) / 10;
  554. /*
  555. * We try very hard to avoid speeds with M == 0 since they may
  556. * not work correctly for XTAL frequences above 10 MHz.
  557. */
  558. if ((m == 0) && ((n & 1) == 0)) {
  559. n = n / 2;
  560. m++;
  561. }
  562. *n_ret = n - 1;
  563. *m_ret = m;
  564. }
  565. /* Internal routine, port->lock is held and local interrupts are disabled. */
  566. static void sunsab_convert_to_sab(struct uart_sunsab_port *up, unsigned int cflag,
  567. unsigned int iflag, unsigned int baud,
  568. unsigned int quot)
  569. {
  570. unsigned int ebrg;
  571. unsigned char dafo;
  572. int bits, n, m;
  573. /* Byte size and parity */
  574. switch (cflag & CSIZE) {
  575. case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
  576. case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
  577. case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
  578. case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
  579. /* Never happens, but GCC is too dumb to figure it out */
  580. default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
  581. }
  582. if (cflag & CSTOPB) {
  583. dafo |= SAB82532_DAFO_STOP;
  584. bits++;
  585. }
  586. if (cflag & PARENB) {
  587. dafo |= SAB82532_DAFO_PARE;
  588. bits++;
  589. }
  590. if (cflag & PARODD) {
  591. dafo |= SAB82532_DAFO_PAR_ODD;
  592. } else {
  593. dafo |= SAB82532_DAFO_PAR_EVEN;
  594. }
  595. calc_ebrg(baud, &n, &m);
  596. ebrg = n | (m << 6);
  597. up->tec_timeout = (10 * 1000000) / baud;
  598. up->cec_timeout = up->tec_timeout >> 2;
  599. /* CTS flow control flags */
  600. /* We encode read_status_mask and ignore_status_mask like so:
  601. *
  602. * ---------------------
  603. * | ... | ISR1 | ISR0 |
  604. * ---------------------
  605. * .. 15 8 7 0
  606. */
  607. up->port.read_status_mask = (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
  608. SAB82532_ISR0_RFO | SAB82532_ISR0_RPF |
  609. SAB82532_ISR0_CDSC);
  610. up->port.read_status_mask |= (SAB82532_ISR1_CSC |
  611. SAB82532_ISR1_ALLS |
  612. SAB82532_ISR1_XPR) << 8;
  613. if (iflag & INPCK)
  614. up->port.read_status_mask |= (SAB82532_ISR0_PERR |
  615. SAB82532_ISR0_FERR);
  616. if (iflag & (BRKINT | PARMRK))
  617. up->port.read_status_mask |= (SAB82532_ISR1_BRK << 8);
  618. /*
  619. * Characteres to ignore
  620. */
  621. up->port.ignore_status_mask = 0;
  622. if (iflag & IGNPAR)
  623. up->port.ignore_status_mask |= (SAB82532_ISR0_PERR |
  624. SAB82532_ISR0_FERR);
  625. if (iflag & IGNBRK) {
  626. up->port.ignore_status_mask |= (SAB82532_ISR1_BRK << 8);
  627. /*
  628. * If we're ignoring parity and break indicators,
  629. * ignore overruns too (for real raw support).
  630. */
  631. if (iflag & IGNPAR)
  632. up->port.ignore_status_mask |= SAB82532_ISR0_RFO;
  633. }
  634. /*
  635. * ignore all characters if CREAD is not set
  636. */
  637. if ((cflag & CREAD) == 0)
  638. up->port.ignore_status_mask |= (SAB82532_ISR0_RPF |
  639. SAB82532_ISR0_TCD);
  640. uart_update_timeout(&up->port, cflag,
  641. (up->port.uartclk / (16 * quot)));
  642. /* Now bang the new settings into the chip. */
  643. sunsab_cec_wait(up);
  644. sunsab_tec_wait(up);
  645. writeb(dafo, &up->regs->w.dafo);
  646. writeb(ebrg & 0xff, &up->regs->w.bgr);
  647. writeb((readb(&up->regs->rw.ccr2) & ~0xc0) | ((ebrg >> 2) & 0xc0),
  648. &up->regs->rw.ccr2);
  649. writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_RAC, &up->regs->rw.mode);
  650. }
  651. /* port->lock is not held. */
  652. static void sunsab_set_termios(struct uart_port *port, struct termios *termios,
  653. struct termios *old)
  654. {
  655. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  656. unsigned long flags;
  657. unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
  658. unsigned int quot = uart_get_divisor(port, baud);
  659. spin_lock_irqsave(&up->port.lock, flags);
  660. sunsab_convert_to_sab(up, termios->c_cflag, termios->c_iflag, baud, quot);
  661. spin_unlock_irqrestore(&up->port.lock, flags);
  662. }
  663. static const char *sunsab_type(struct uart_port *port)
  664. {
  665. struct uart_sunsab_port *up = (void *)port;
  666. static char buf[36];
  667. sprintf(buf, "SAB82532 %s", sab82532_version[up->type]);
  668. return buf;
  669. }
  670. static void sunsab_release_port(struct uart_port *port)
  671. {
  672. }
  673. static int sunsab_request_port(struct uart_port *port)
  674. {
  675. return 0;
  676. }
  677. static void sunsab_config_port(struct uart_port *port, int flags)
  678. {
  679. }
  680. static int sunsab_verify_port(struct uart_port *port, struct serial_struct *ser)
  681. {
  682. return -EINVAL;
  683. }
  684. static struct uart_ops sunsab_pops = {
  685. .tx_empty = sunsab_tx_empty,
  686. .set_mctrl = sunsab_set_mctrl,
  687. .get_mctrl = sunsab_get_mctrl,
  688. .stop_tx = sunsab_stop_tx,
  689. .start_tx = sunsab_start_tx,
  690. .send_xchar = sunsab_send_xchar,
  691. .stop_rx = sunsab_stop_rx,
  692. .enable_ms = sunsab_enable_ms,
  693. .break_ctl = sunsab_break_ctl,
  694. .startup = sunsab_startup,
  695. .shutdown = sunsab_shutdown,
  696. .set_termios = sunsab_set_termios,
  697. .type = sunsab_type,
  698. .release_port = sunsab_release_port,
  699. .request_port = sunsab_request_port,
  700. .config_port = sunsab_config_port,
  701. .verify_port = sunsab_verify_port,
  702. };
  703. static struct uart_driver sunsab_reg = {
  704. .owner = THIS_MODULE,
  705. .driver_name = "serial",
  706. .devfs_name = "tts/",
  707. .dev_name = "ttyS",
  708. .major = TTY_MAJOR,
  709. };
  710. static struct uart_sunsab_port *sunsab_ports;
  711. static int num_channels;
  712. #ifdef CONFIG_SERIAL_SUNSAB_CONSOLE
  713. static __inline__ void sunsab_console_putchar(struct uart_sunsab_port *up, char c)
  714. {
  715. unsigned long flags;
  716. spin_lock_irqsave(&up->port.lock, flags);
  717. sunsab_tec_wait(up);
  718. writeb(c, &up->regs->w.tic);
  719. spin_unlock_irqrestore(&up->port.lock, flags);
  720. }
  721. static void sunsab_console_write(struct console *con, const char *s, unsigned n)
  722. {
  723. struct uart_sunsab_port *up = &sunsab_ports[con->index];
  724. int i;
  725. for (i = 0; i < n; i++) {
  726. if (*s == '\n')
  727. sunsab_console_putchar(up, '\r');
  728. sunsab_console_putchar(up, *s++);
  729. }
  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. /* Firmware console speed is limited to 150-->38400 baud so
  741. * this hackish cflag thing is OK.
  742. */
  743. switch (con->cflag & CBAUD) {
  744. case B150: baud = 150; break;
  745. case B300: baud = 300; break;
  746. case B600: baud = 600; break;
  747. case B1200: baud = 1200; break;
  748. case B2400: baud = 2400; break;
  749. case B4800: baud = 4800; break;
  750. default: case B9600: baud = 9600; break;
  751. case B19200: baud = 19200; break;
  752. case B38400: baud = 38400; break;
  753. };
  754. /*
  755. * Temporary fix.
  756. */
  757. spin_lock_init(&up->port.lock);
  758. /*
  759. * Initialize the hardware
  760. */
  761. sunsab_startup(&up->port);
  762. spin_lock_irqsave(&up->port.lock, flags);
  763. /*
  764. * Finally, enable interrupts
  765. */
  766. up->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
  767. SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
  768. writeb(up->interrupt_mask0, &up->regs->w.imr0);
  769. up->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
  770. SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
  771. SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
  772. SAB82532_IMR1_XPR;
  773. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  774. quot = uart_get_divisor(&up->port, baud);
  775. sunsab_convert_to_sab(up, con->cflag, 0, baud, quot);
  776. sunsab_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
  777. spin_unlock_irqrestore(&up->port.lock, flags);
  778. return 0;
  779. }
  780. static struct console sunsab_console = {
  781. .name = "ttyS",
  782. .write = sunsab_console_write,
  783. .device = uart_console_device,
  784. .setup = sunsab_console_setup,
  785. .flags = CON_PRINTBUFFER,
  786. .index = -1,
  787. .data = &sunsab_reg,
  788. };
  789. #define SUNSAB_CONSOLE (&sunsab_console)
  790. static void __init sunsab_console_init(void)
  791. {
  792. int i;
  793. if (con_is_present())
  794. return;
  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;
  802. sunsab_console.index = i;
  803. register_console(&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 = SERIAL_IO_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. writeb((1 << 1) | (1 << 2) | (1 << 4), &up->regs->w.pvr);
  900. writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_FRTS,
  901. &up->regs->rw.mode);
  902. writeb(readb(&up->regs->rw.mode) | SAB82532_MODE_RTS,
  903. &up->regs->rw.mode);
  904. up->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
  905. up->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
  906. if (!(up->port.line & 0x01)) {
  907. if (request_irq(up->port.irq, sunsab_interrupt,
  908. SA_SHIRQ, "serial(sab82532)", up)) {
  909. printk("sunsab%d: can't get IRQ %x\n",
  910. i, up->port.irq);
  911. continue;
  912. }
  913. }
  914. }
  915. }
  916. static int __init sunsab_init(void)
  917. {
  918. int ret = probe_for_sabs();
  919. int i;
  920. if (ret < 0)
  921. return ret;
  922. sunsab_init_hw();
  923. sunsab_reg.minor = sunserial_current_minor;
  924. sunsab_reg.nr = num_channels;
  925. sunsab_reg.cons = SUNSAB_CONSOLE;
  926. ret = uart_register_driver(&sunsab_reg);
  927. if (ret < 0) {
  928. int i;
  929. for (i = 0; i < num_channels; i++) {
  930. struct uart_sunsab_port *up = &sunsab_ports[i];
  931. if (!(up->port.line & 0x01))
  932. free_irq(up->port.irq, up);
  933. iounmap(up->regs);
  934. }
  935. kfree(sunsab_ports);
  936. sunsab_ports = NULL;
  937. return ret;
  938. }
  939. sunserial_current_minor += num_channels;
  940. sunsab_console_init();
  941. for (i = 0; i < num_channels; i++) {
  942. struct uart_sunsab_port *up = &sunsab_ports[i];
  943. uart_add_one_port(&sunsab_reg, &up->port);
  944. }
  945. return 0;
  946. }
  947. static void __exit sunsab_exit(void)
  948. {
  949. int i;
  950. for (i = 0; i < num_channels; i++) {
  951. struct uart_sunsab_port *up = &sunsab_ports[i];
  952. uart_remove_one_port(&sunsab_reg, &up->port);
  953. if (!(up->port.line & 0x01))
  954. free_irq(up->port.irq, up);
  955. iounmap(up->regs);
  956. }
  957. sunserial_current_minor -= num_channels;
  958. uart_unregister_driver(&sunsab_reg);
  959. kfree(sunsab_ports);
  960. sunsab_ports = NULL;
  961. }
  962. module_init(sunsab_init);
  963. module_exit(sunsab_exit);
  964. MODULE_AUTHOR("Eddie C. Dost and David S. Miller");
  965. MODULE_DESCRIPTION("Sun SAB82532 serial port driver");
  966. MODULE_LICENSE("GPL");