sunsab.c 30 KB

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