sunsab.c 29 KB

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