sunsab.c 29 KB

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