sunsab.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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. up->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS|SAB82532_IMR1_XPR);
  356. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  357. if (!test_bit(SAB82532_XPR, &up->irqflags))
  358. return;
  359. clear_bit(SAB82532_ALLS, &up->irqflags);
  360. clear_bit(SAB82532_XPR, &up->irqflags);
  361. for (i = 0; i < up->port.fifosize; i++) {
  362. writeb(xmit->buf[xmit->tail],
  363. &up->regs->w.xfifo[i]);
  364. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  365. up->port.icount.tx++;
  366. if (uart_circ_empty(xmit))
  367. break;
  368. }
  369. /* Issue a Transmit Frame command. */
  370. sunsab_cec_wait(up);
  371. writeb(SAB82532_CMDR_XF, &up->regs->w.cmdr);
  372. }
  373. /* port->lock is not held. */
  374. static void sunsab_send_xchar(struct uart_port *port, char ch)
  375. {
  376. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  377. unsigned long flags;
  378. spin_lock_irqsave(&up->port.lock, flags);
  379. sunsab_tec_wait(up);
  380. writeb(ch, &up->regs->w.tic);
  381. spin_unlock_irqrestore(&up->port.lock, flags);
  382. }
  383. /* port->lock held by caller. */
  384. static void sunsab_stop_rx(struct uart_port *port)
  385. {
  386. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  387. up->interrupt_mask0 |= SAB82532_IMR0_TCD;
  388. writeb(up->interrupt_mask1, &up->regs->w.imr0);
  389. }
  390. /* port->lock is not held. */
  391. static void sunsab_break_ctl(struct uart_port *port, int break_state)
  392. {
  393. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  394. unsigned long flags;
  395. unsigned char val;
  396. spin_lock_irqsave(&up->port.lock, flags);
  397. val = up->cached_dafo;
  398. if (break_state)
  399. val |= SAB82532_DAFO_XBRK;
  400. else
  401. val &= ~SAB82532_DAFO_XBRK;
  402. up->cached_dafo = val;
  403. set_bit(SAB82532_REGS_PENDING, &up->irqflags);
  404. if (test_bit(SAB82532_XPR, &up->irqflags))
  405. sunsab_tx_idle(up);
  406. spin_unlock_irqrestore(&up->port.lock, flags);
  407. }
  408. /* port->lock is not held. */
  409. static int sunsab_startup(struct uart_port *port)
  410. {
  411. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  412. unsigned long flags;
  413. unsigned char tmp;
  414. int err = request_irq(up->port.irq, sunsab_interrupt,
  415. IRQF_SHARED, "sab", up);
  416. if (err)
  417. return err;
  418. spin_lock_irqsave(&up->port.lock, flags);
  419. /*
  420. * Wait for any commands or immediate characters
  421. */
  422. sunsab_cec_wait(up);
  423. sunsab_tec_wait(up);
  424. /*
  425. * Clear the FIFO buffers.
  426. */
  427. writeb(SAB82532_CMDR_RRES, &up->regs->w.cmdr);
  428. sunsab_cec_wait(up);
  429. writeb(SAB82532_CMDR_XRES, &up->regs->w.cmdr);
  430. /*
  431. * Clear the interrupt registers.
  432. */
  433. (void) readb(&up->regs->r.isr0);
  434. (void) readb(&up->regs->r.isr1);
  435. /*
  436. * Now, initialize the UART
  437. */
  438. writeb(0, &up->regs->w.ccr0); /* power-down */
  439. writeb(SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
  440. SAB82532_CCR0_SM_ASYNC, &up->regs->w.ccr0);
  441. writeb(SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7, &up->regs->w.ccr1);
  442. writeb(SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
  443. SAB82532_CCR2_TOE, &up->regs->w.ccr2);
  444. writeb(0, &up->regs->w.ccr3);
  445. writeb(SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG, &up->regs->w.ccr4);
  446. up->cached_mode = (SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
  447. SAB82532_MODE_RAC);
  448. writeb(up->cached_mode, &up->regs->w.mode);
  449. writeb(SAB82532_RFC_DPS|SAB82532_RFC_RFTH_32, &up->regs->w.rfc);
  450. tmp = readb(&up->regs->rw.ccr0);
  451. tmp |= SAB82532_CCR0_PU; /* power-up */
  452. writeb(tmp, &up->regs->rw.ccr0);
  453. /*
  454. * Finally, enable interrupts
  455. */
  456. up->interrupt_mask0 = (SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
  457. SAB82532_IMR0_PLLA);
  458. writeb(up->interrupt_mask0, &up->regs->w.imr0);
  459. up->interrupt_mask1 = (SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
  460. SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
  461. SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
  462. SAB82532_IMR1_XPR);
  463. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  464. set_bit(SAB82532_ALLS, &up->irqflags);
  465. set_bit(SAB82532_XPR, &up->irqflags);
  466. spin_unlock_irqrestore(&up->port.lock, flags);
  467. return 0;
  468. }
  469. /* port->lock is not held. */
  470. static void sunsab_shutdown(struct uart_port *port)
  471. {
  472. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  473. unsigned long flags;
  474. spin_lock_irqsave(&up->port.lock, flags);
  475. /* Disable Interrupts */
  476. up->interrupt_mask0 = 0xff;
  477. writeb(up->interrupt_mask0, &up->regs->w.imr0);
  478. up->interrupt_mask1 = 0xff;
  479. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  480. /* Disable break condition */
  481. up->cached_dafo = readb(&up->regs->rw.dafo);
  482. up->cached_dafo &= ~SAB82532_DAFO_XBRK;
  483. writeb(up->cached_dafo, &up->regs->rw.dafo);
  484. /* Disable Receiver */
  485. up->cached_mode &= ~SAB82532_MODE_RAC;
  486. writeb(up->cached_mode, &up->regs->rw.mode);
  487. /*
  488. * XXX FIXME
  489. *
  490. * If the chip is powered down here the system hangs/crashes during
  491. * reboot or shutdown. This needs to be investigated further,
  492. * similar behaviour occurs in 2.4 when the driver is configured
  493. * as a module only. One hint may be that data is sometimes
  494. * transmitted at 9600 baud during shutdown (regardless of the
  495. * speed the chip was configured for when the port was open).
  496. */
  497. #if 0
  498. /* Power Down */
  499. tmp = readb(&up->regs->rw.ccr0);
  500. tmp &= ~SAB82532_CCR0_PU;
  501. writeb(tmp, &up->regs->rw.ccr0);
  502. #endif
  503. spin_unlock_irqrestore(&up->port.lock, flags);
  504. free_irq(up->port.irq, up);
  505. }
  506. /*
  507. * This is used to figure out the divisor speeds.
  508. *
  509. * The formula is: Baud = SAB_BASE_BAUD / ((N + 1) * (1 << M)),
  510. *
  511. * with 0 <= N < 64 and 0 <= M < 16
  512. */
  513. static void calc_ebrg(int baud, int *n_ret, int *m_ret)
  514. {
  515. int n, m;
  516. if (baud == 0) {
  517. *n_ret = 0;
  518. *m_ret = 0;
  519. return;
  520. }
  521. /*
  522. * We scale numbers by 10 so that we get better accuracy
  523. * without having to use floating point. Here we increment m
  524. * until n is within the valid range.
  525. */
  526. n = (SAB_BASE_BAUD * 10) / baud;
  527. m = 0;
  528. while (n >= 640) {
  529. n = n / 2;
  530. m++;
  531. }
  532. n = (n+5) / 10;
  533. /*
  534. * We try very hard to avoid speeds with M == 0 since they may
  535. * not work correctly for XTAL frequences above 10 MHz.
  536. */
  537. if ((m == 0) && ((n & 1) == 0)) {
  538. n = n / 2;
  539. m++;
  540. }
  541. *n_ret = n - 1;
  542. *m_ret = m;
  543. }
  544. /* Internal routine, port->lock is held and local interrupts are disabled. */
  545. static void sunsab_convert_to_sab(struct uart_sunsab_port *up, unsigned int cflag,
  546. unsigned int iflag, unsigned int baud,
  547. unsigned int quot)
  548. {
  549. unsigned char dafo;
  550. int bits, n, m;
  551. /* Byte size and parity */
  552. switch (cflag & CSIZE) {
  553. case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
  554. case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
  555. case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
  556. case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
  557. /* Never happens, but GCC is too dumb to figure it out */
  558. default: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
  559. }
  560. if (cflag & CSTOPB) {
  561. dafo |= SAB82532_DAFO_STOP;
  562. bits++;
  563. }
  564. if (cflag & PARENB) {
  565. dafo |= SAB82532_DAFO_PARE;
  566. bits++;
  567. }
  568. if (cflag & PARODD) {
  569. dafo |= SAB82532_DAFO_PAR_ODD;
  570. } else {
  571. dafo |= SAB82532_DAFO_PAR_EVEN;
  572. }
  573. up->cached_dafo = dafo;
  574. calc_ebrg(baud, &n, &m);
  575. up->cached_ebrg = n | (m << 6);
  576. up->tec_timeout = (10 * 1000000) / baud;
  577. up->cec_timeout = up->tec_timeout >> 2;
  578. /* CTS flow control flags */
  579. /* We encode read_status_mask and ignore_status_mask like so:
  580. *
  581. * ---------------------
  582. * | ... | ISR1 | ISR0 |
  583. * ---------------------
  584. * .. 15 8 7 0
  585. */
  586. up->port.read_status_mask = (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
  587. SAB82532_ISR0_RFO | SAB82532_ISR0_RPF |
  588. SAB82532_ISR0_CDSC);
  589. up->port.read_status_mask |= (SAB82532_ISR1_CSC |
  590. SAB82532_ISR1_ALLS |
  591. SAB82532_ISR1_XPR) << 8;
  592. if (iflag & INPCK)
  593. up->port.read_status_mask |= (SAB82532_ISR0_PERR |
  594. SAB82532_ISR0_FERR);
  595. if (iflag & (IGNBRK | BRKINT | PARMRK))
  596. up->port.read_status_mask |= (SAB82532_ISR1_BRK << 8);
  597. /*
  598. * Characteres to ignore
  599. */
  600. up->port.ignore_status_mask = 0;
  601. if (iflag & IGNPAR)
  602. up->port.ignore_status_mask |= (SAB82532_ISR0_PERR |
  603. SAB82532_ISR0_FERR);
  604. if (iflag & IGNBRK) {
  605. up->port.ignore_status_mask |= (SAB82532_ISR1_BRK << 8);
  606. /*
  607. * If we're ignoring parity and break indicators,
  608. * ignore overruns too (for real raw support).
  609. */
  610. if (iflag & IGNPAR)
  611. up->port.ignore_status_mask |= SAB82532_ISR0_RFO;
  612. }
  613. /*
  614. * ignore all characters if CREAD is not set
  615. */
  616. if ((cflag & CREAD) == 0)
  617. up->port.ignore_status_mask |= (SAB82532_ISR0_RPF |
  618. SAB82532_ISR0_TCD);
  619. uart_update_timeout(&up->port, cflag,
  620. (up->port.uartclk / (16 * quot)));
  621. /* Now schedule a register update when the chip's
  622. * transmitter is idle.
  623. */
  624. up->cached_mode |= SAB82532_MODE_RAC;
  625. set_bit(SAB82532_REGS_PENDING, &up->irqflags);
  626. if (test_bit(SAB82532_XPR, &up->irqflags))
  627. sunsab_tx_idle(up);
  628. }
  629. /* port->lock is not held. */
  630. static void sunsab_set_termios(struct uart_port *port, struct ktermios *termios,
  631. struct ktermios *old)
  632. {
  633. struct uart_sunsab_port *up = (struct uart_sunsab_port *) port;
  634. unsigned long flags;
  635. unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
  636. unsigned int quot = uart_get_divisor(port, baud);
  637. spin_lock_irqsave(&up->port.lock, flags);
  638. sunsab_convert_to_sab(up, termios->c_cflag, termios->c_iflag, baud, quot);
  639. spin_unlock_irqrestore(&up->port.lock, flags);
  640. }
  641. static const char *sunsab_type(struct uart_port *port)
  642. {
  643. struct uart_sunsab_port *up = (void *)port;
  644. static char buf[36];
  645. sprintf(buf, "SAB82532 %s", sab82532_version[up->type]);
  646. return buf;
  647. }
  648. static void sunsab_release_port(struct uart_port *port)
  649. {
  650. }
  651. static int sunsab_request_port(struct uart_port *port)
  652. {
  653. return 0;
  654. }
  655. static void sunsab_config_port(struct uart_port *port, int flags)
  656. {
  657. }
  658. static int sunsab_verify_port(struct uart_port *port, struct serial_struct *ser)
  659. {
  660. return -EINVAL;
  661. }
  662. static struct uart_ops sunsab_pops = {
  663. .tx_empty = sunsab_tx_empty,
  664. .set_mctrl = sunsab_set_mctrl,
  665. .get_mctrl = sunsab_get_mctrl,
  666. .stop_tx = sunsab_stop_tx,
  667. .start_tx = sunsab_start_tx,
  668. .send_xchar = sunsab_send_xchar,
  669. .stop_rx = sunsab_stop_rx,
  670. .break_ctl = sunsab_break_ctl,
  671. .startup = sunsab_startup,
  672. .shutdown = sunsab_shutdown,
  673. .set_termios = sunsab_set_termios,
  674. .type = sunsab_type,
  675. .release_port = sunsab_release_port,
  676. .request_port = sunsab_request_port,
  677. .config_port = sunsab_config_port,
  678. .verify_port = sunsab_verify_port,
  679. };
  680. static struct uart_driver sunsab_reg = {
  681. .owner = THIS_MODULE,
  682. .driver_name = "sunsab",
  683. .dev_name = "ttyS",
  684. .major = TTY_MAJOR,
  685. };
  686. static struct uart_sunsab_port *sunsab_ports;
  687. #ifdef CONFIG_SERIAL_SUNSAB_CONSOLE
  688. static void sunsab_console_putchar(struct uart_port *port, int c)
  689. {
  690. struct uart_sunsab_port *up = (struct uart_sunsab_port *)port;
  691. sunsab_tec_wait(up);
  692. writeb(c, &up->regs->w.tic);
  693. }
  694. static void sunsab_console_write(struct console *con, const char *s, unsigned n)
  695. {
  696. struct uart_sunsab_port *up = &sunsab_ports[con->index];
  697. unsigned long flags;
  698. int locked = 1;
  699. if (up->port.sysrq || oops_in_progress)
  700. locked = spin_trylock_irqsave(&up->port.lock, flags);
  701. else
  702. spin_lock_irqsave(&up->port.lock, flags);
  703. uart_console_write(&up->port, s, n, sunsab_console_putchar);
  704. sunsab_tec_wait(up);
  705. if (locked)
  706. spin_unlock_irqrestore(&up->port.lock, flags);
  707. }
  708. static int sunsab_console_setup(struct console *con, char *options)
  709. {
  710. struct uart_sunsab_port *up = &sunsab_ports[con->index];
  711. unsigned long flags;
  712. unsigned int baud, quot;
  713. /*
  714. * The console framework calls us for each and every port
  715. * registered. Defer the console setup until the requested
  716. * port has been properly discovered. A bit of a hack,
  717. * though...
  718. */
  719. if (up->port.type != PORT_SUNSAB)
  720. return -1;
  721. printk("Console: ttyS%d (SAB82532)\n",
  722. (sunsab_reg.minor - 64) + con->index);
  723. sunserial_console_termios(con, up->port.dev->of_node);
  724. switch (con->cflag & CBAUD) {
  725. case B150: baud = 150; break;
  726. case B300: baud = 300; break;
  727. case B600: baud = 600; break;
  728. case B1200: baud = 1200; break;
  729. case B2400: baud = 2400; break;
  730. case B4800: baud = 4800; break;
  731. default: case B9600: baud = 9600; break;
  732. case B19200: baud = 19200; break;
  733. case B38400: baud = 38400; break;
  734. case B57600: baud = 57600; break;
  735. case B115200: baud = 115200; break;
  736. case B230400: baud = 230400; break;
  737. case B460800: baud = 460800; break;
  738. }
  739. /*
  740. * Temporary fix.
  741. */
  742. spin_lock_init(&up->port.lock);
  743. /*
  744. * Initialize the hardware
  745. */
  746. sunsab_startup(&up->port);
  747. spin_lock_irqsave(&up->port.lock, flags);
  748. /*
  749. * Finally, enable interrupts
  750. */
  751. up->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
  752. SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
  753. writeb(up->interrupt_mask0, &up->regs->w.imr0);
  754. up->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
  755. SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
  756. SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
  757. SAB82532_IMR1_XPR;
  758. writeb(up->interrupt_mask1, &up->regs->w.imr1);
  759. quot = uart_get_divisor(&up->port, baud);
  760. sunsab_convert_to_sab(up, con->cflag, 0, baud, quot);
  761. sunsab_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
  762. spin_unlock_irqrestore(&up->port.lock, flags);
  763. return 0;
  764. }
  765. static struct console sunsab_console = {
  766. .name = "ttyS",
  767. .write = sunsab_console_write,
  768. .device = uart_console_device,
  769. .setup = sunsab_console_setup,
  770. .flags = CON_PRINTBUFFER,
  771. .index = -1,
  772. .data = &sunsab_reg,
  773. };
  774. static inline struct console *SUNSAB_CONSOLE(void)
  775. {
  776. return &sunsab_console;
  777. }
  778. #else
  779. #define SUNSAB_CONSOLE() (NULL)
  780. #define sunsab_console_init() do { } while (0)
  781. #endif
  782. static int sunsab_init_one(struct uart_sunsab_port *up,
  783. struct platform_device *op,
  784. unsigned long offset,
  785. int line)
  786. {
  787. up->port.line = line;
  788. up->port.dev = &op->dev;
  789. up->port.mapbase = op->resource[0].start + offset;
  790. up->port.membase = of_ioremap(&op->resource[0], offset,
  791. sizeof(union sab82532_async_regs),
  792. "sab");
  793. if (!up->port.membase)
  794. return -ENOMEM;
  795. up->regs = (union sab82532_async_regs __iomem *) up->port.membase;
  796. up->port.irq = op->archdata.irqs[0];
  797. up->port.fifosize = SAB82532_XMIT_FIFO_SIZE;
  798. up->port.iotype = UPIO_MEM;
  799. writeb(SAB82532_IPC_IC_ACT_LOW, &up->regs->w.ipc);
  800. up->port.ops = &sunsab_pops;
  801. up->port.type = PORT_SUNSAB;
  802. up->port.uartclk = SAB_BASE_BAUD;
  803. up->type = readb(&up->regs->r.vstr) & 0x0f;
  804. writeb(~((1 << 1) | (1 << 2) | (1 << 4)), &up->regs->w.pcr);
  805. writeb(0xff, &up->regs->w.pim);
  806. if ((up->port.line & 0x1) == 0) {
  807. up->pvr_dsr_bit = (1 << 0);
  808. up->pvr_dtr_bit = (1 << 1);
  809. up->gis_shift = 2;
  810. } else {
  811. up->pvr_dsr_bit = (1 << 3);
  812. up->pvr_dtr_bit = (1 << 2);
  813. up->gis_shift = 0;
  814. }
  815. up->cached_pvr = (1 << 1) | (1 << 2) | (1 << 4);
  816. writeb(up->cached_pvr, &up->regs->w.pvr);
  817. up->cached_mode = readb(&up->regs->rw.mode);
  818. up->cached_mode |= SAB82532_MODE_FRTS;
  819. writeb(up->cached_mode, &up->regs->rw.mode);
  820. up->cached_mode |= SAB82532_MODE_RTS;
  821. writeb(up->cached_mode, &up->regs->rw.mode);
  822. up->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
  823. up->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
  824. return 0;
  825. }
  826. static int sab_probe(struct platform_device *op)
  827. {
  828. static int inst;
  829. struct uart_sunsab_port *up;
  830. int err;
  831. up = &sunsab_ports[inst * 2];
  832. err = sunsab_init_one(&up[0], op,
  833. 0,
  834. (inst * 2) + 0);
  835. if (err)
  836. goto out;
  837. err = sunsab_init_one(&up[1], op,
  838. sizeof(union sab82532_async_regs),
  839. (inst * 2) + 1);
  840. if (err)
  841. goto out1;
  842. sunserial_console_match(SUNSAB_CONSOLE(), op->dev.of_node,
  843. &sunsab_reg, up[0].port.line,
  844. false);
  845. sunserial_console_match(SUNSAB_CONSOLE(), op->dev.of_node,
  846. &sunsab_reg, up[1].port.line,
  847. false);
  848. err = uart_add_one_port(&sunsab_reg, &up[0].port);
  849. if (err)
  850. goto out2;
  851. err = uart_add_one_port(&sunsab_reg, &up[1].port);
  852. if (err)
  853. goto out3;
  854. platform_set_drvdata(op, &up[0]);
  855. inst++;
  856. return 0;
  857. out3:
  858. uart_remove_one_port(&sunsab_reg, &up[0].port);
  859. out2:
  860. of_iounmap(&op->resource[0],
  861. up[1].port.membase,
  862. sizeof(union sab82532_async_regs));
  863. out1:
  864. of_iounmap(&op->resource[0],
  865. up[0].port.membase,
  866. sizeof(union sab82532_async_regs));
  867. out:
  868. return err;
  869. }
  870. static int sab_remove(struct platform_device *op)
  871. {
  872. struct uart_sunsab_port *up = platform_get_drvdata(op);
  873. uart_remove_one_port(&sunsab_reg, &up[1].port);
  874. uart_remove_one_port(&sunsab_reg, &up[0].port);
  875. of_iounmap(&op->resource[0],
  876. up[1].port.membase,
  877. sizeof(union sab82532_async_regs));
  878. of_iounmap(&op->resource[0],
  879. up[0].port.membase,
  880. sizeof(union sab82532_async_regs));
  881. return 0;
  882. }
  883. static const struct of_device_id sab_match[] = {
  884. {
  885. .name = "se",
  886. },
  887. {
  888. .name = "serial",
  889. .compatible = "sab82532",
  890. },
  891. {},
  892. };
  893. MODULE_DEVICE_TABLE(of, sab_match);
  894. static struct platform_driver sab_driver = {
  895. .driver = {
  896. .name = "sab",
  897. .owner = THIS_MODULE,
  898. .of_match_table = sab_match,
  899. },
  900. .probe = sab_probe,
  901. .remove = sab_remove,
  902. };
  903. static int __init sunsab_init(void)
  904. {
  905. struct device_node *dp;
  906. int err;
  907. int num_channels = 0;
  908. for_each_node_by_name(dp, "se")
  909. num_channels += 2;
  910. for_each_node_by_name(dp, "serial") {
  911. if (of_device_is_compatible(dp, "sab82532"))
  912. num_channels += 2;
  913. }
  914. if (num_channels) {
  915. sunsab_ports = kzalloc(sizeof(struct uart_sunsab_port) *
  916. num_channels, GFP_KERNEL);
  917. if (!sunsab_ports)
  918. return -ENOMEM;
  919. err = sunserial_register_minors(&sunsab_reg, num_channels);
  920. if (err) {
  921. kfree(sunsab_ports);
  922. sunsab_ports = NULL;
  923. return err;
  924. }
  925. }
  926. return platform_driver_register(&sab_driver);
  927. }
  928. static void __exit sunsab_exit(void)
  929. {
  930. platform_driver_unregister(&sab_driver);
  931. if (sunsab_reg.nr) {
  932. sunserial_unregister_minors(&sunsab_reg, sunsab_reg.nr);
  933. }
  934. kfree(sunsab_ports);
  935. sunsab_ports = NULL;
  936. }
  937. module_init(sunsab_init);
  938. module_exit(sunsab_exit);
  939. MODULE_AUTHOR("Eddie C. Dost and David S. Miller");
  940. MODULE_DESCRIPTION("Sun SAB82532 serial port driver");
  941. MODULE_LICENSE("GPL");