sunsab.c 30 KB

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