sunsab.c 30 KB

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