sunsab.c 29 KB

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