sunsab.c 30 KB

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