sunsab.c 29 KB

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