sunsab.c 29 KB

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