sunsab.c 29 KB

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