zs.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204
  1. /*
  2. * decserial.c: Serial port driver for IOASIC DECstations.
  3. *
  4. * Derived from drivers/sbus/char/sunserial.c by Paul Mackerras.
  5. * Derived from drivers/macintosh/macserial.c by Harald Koerfgen.
  6. *
  7. * DECstation changes
  8. * Copyright (C) 1998-2000 Harald Koerfgen
  9. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Maciej W. Rozycki
  10. *
  11. * For the rest of the code the original Copyright applies:
  12. * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
  13. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  14. *
  15. *
  16. * Note: for IOASIC systems the wiring is as follows:
  17. *
  18. * mouse/keyboard:
  19. * DIN-7 MJ-4 signal SCC
  20. * 2 1 TxD <- A.TxD
  21. * 3 4 RxD -> A.RxD
  22. *
  23. * EIA-232/EIA-423:
  24. * DB-25 MMJ-6 signal SCC
  25. * 2 2 TxD <- B.TxD
  26. * 3 5 RxD -> B.RxD
  27. * 4 RTS <- ~A.RTS
  28. * 5 CTS -> ~B.CTS
  29. * 6 6 DSR -> ~A.SYNC
  30. * 8 CD -> ~B.DCD
  31. * 12 DSRS(DCE) -> ~A.CTS (*)
  32. * 15 TxC -> B.TxC
  33. * 17 RxC -> B.RxC
  34. * 20 1 DTR <- ~A.DTR
  35. * 22 RI -> ~A.DCD
  36. * 23 DSRS(DTE) <- ~B.RTS
  37. *
  38. * (*) EIA-232 defines the signal at this pin to be SCD, while DSRS(DCE)
  39. * is shared with DSRS(DTE) at pin 23.
  40. */
  41. #include <linux/errno.h>
  42. #include <linux/signal.h>
  43. #include <linux/sched.h>
  44. #include <linux/timer.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/tty.h>
  47. #include <linux/tty_flip.h>
  48. #include <linux/major.h>
  49. #include <linux/string.h>
  50. #include <linux/fcntl.h>
  51. #include <linux/mm.h>
  52. #include <linux/kernel.h>
  53. #include <linux/delay.h>
  54. #include <linux/init.h>
  55. #include <linux/ioport.h>
  56. #include <linux/spinlock.h>
  57. #ifdef CONFIG_SERIAL_DEC_CONSOLE
  58. #include <linux/console.h>
  59. #endif
  60. #include <asm/io.h>
  61. #include <asm/pgtable.h>
  62. #include <asm/irq.h>
  63. #include <asm/system.h>
  64. #include <asm/bootinfo.h>
  65. #include <asm/dec/interrupts.h>
  66. #include <asm/dec/ioasic_addrs.h>
  67. #include <asm/dec/machtype.h>
  68. #include <asm/dec/serial.h>
  69. #include <asm/dec/system.h>
  70. #include <asm/dec/tc.h>
  71. #ifdef CONFIG_KGDB
  72. #include <asm/kgdb.h>
  73. #endif
  74. #ifdef CONFIG_MAGIC_SYSRQ
  75. #include <linux/sysrq.h>
  76. #endif
  77. #include "zs.h"
  78. /*
  79. * It would be nice to dynamically allocate everything that
  80. * depends on NUM_SERIAL, so we could support any number of
  81. * Z8530s, but for now...
  82. */
  83. #define NUM_SERIAL 2 /* Max number of ZS chips supported */
  84. #define NUM_CHANNELS (NUM_SERIAL * 2) /* 2 channels per chip */
  85. #define CHANNEL_A_NR (zs_parms->channel_a_offset > zs_parms->channel_b_offset)
  86. /* Number of channel A in the chip */
  87. #define ZS_CHAN_IO_SIZE 8
  88. #define ZS_CLOCK 7372800 /* Z8530 RTxC input clock rate */
  89. #define RECOVERY_DELAY udelay(2)
  90. struct zs_parms {
  91. unsigned long scc0;
  92. unsigned long scc1;
  93. int channel_a_offset;
  94. int channel_b_offset;
  95. int irq0;
  96. int irq1;
  97. int clock;
  98. };
  99. static struct zs_parms *zs_parms;
  100. #ifdef CONFIG_MACH_DECSTATION
  101. static struct zs_parms ds_parms = {
  102. scc0 : IOASIC_SCC0,
  103. scc1 : IOASIC_SCC1,
  104. channel_a_offset : 1,
  105. channel_b_offset : 9,
  106. irq0 : -1,
  107. irq1 : -1,
  108. clock : ZS_CLOCK
  109. };
  110. #endif
  111. #ifdef CONFIG_MACH_DECSTATION
  112. #define DS_BUS_PRESENT (IOASIC)
  113. #else
  114. #define DS_BUS_PRESENT 0
  115. #endif
  116. #define BUS_PRESENT (DS_BUS_PRESENT)
  117. DEFINE_SPINLOCK(zs_lock);
  118. struct dec_zschannel zs_channels[NUM_CHANNELS];
  119. struct dec_serial zs_soft[NUM_CHANNELS];
  120. int zs_channels_found;
  121. struct dec_serial *zs_chain; /* list of all channels */
  122. struct tty_struct zs_ttys[NUM_CHANNELS];
  123. #ifdef CONFIG_SERIAL_DEC_CONSOLE
  124. static struct console sercons;
  125. #endif
  126. #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
  127. !defined(MODULE)
  128. static unsigned long break_pressed; /* break, really ... */
  129. #endif
  130. static unsigned char zs_init_regs[16] __initdata = {
  131. 0, /* write 0 */
  132. 0, /* write 1 */
  133. 0, /* write 2 */
  134. 0, /* write 3 */
  135. (X16CLK), /* write 4 */
  136. 0, /* write 5 */
  137. 0, 0, 0, /* write 6, 7, 8 */
  138. (MIE | DLC | NV), /* write 9 */
  139. (NRZ), /* write 10 */
  140. (TCBR | RCBR), /* write 11 */
  141. 0, 0, /* BRG time constant, write 12 + 13 */
  142. (BRSRC | BRENABL), /* write 14 */
  143. 0 /* write 15 */
  144. };
  145. static struct tty_driver *serial_driver;
  146. /* serial subtype definitions */
  147. #define SERIAL_TYPE_NORMAL 1
  148. /* number of characters left in xmit buffer before we ask for more */
  149. #define WAKEUP_CHARS 256
  150. /*
  151. * Debugging.
  152. */
  153. #undef SERIAL_DEBUG_OPEN
  154. #undef SERIAL_DEBUG_FLOW
  155. #undef SERIAL_DEBUG_THROTTLE
  156. #undef SERIAL_PARANOIA_CHECK
  157. #undef ZS_DEBUG_REGS
  158. #ifdef SERIAL_DEBUG_THROTTLE
  159. #define _tty_name(tty,buf) tty_name(tty,buf)
  160. #endif
  161. #define RS_STROBE_TIME 10
  162. #define RS_ISR_PASS_LIMIT 256
  163. static void probe_sccs(void);
  164. static void change_speed(struct dec_serial *info);
  165. static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
  166. static inline int serial_paranoia_check(struct dec_serial *info,
  167. char *name, const char *routine)
  168. {
  169. #ifdef SERIAL_PARANOIA_CHECK
  170. static const char *badmagic =
  171. "Warning: bad magic number for serial struct %s in %s\n";
  172. static const char *badinfo =
  173. "Warning: null mac_serial for %s in %s\n";
  174. if (!info) {
  175. printk(badinfo, name, routine);
  176. return 1;
  177. }
  178. if (info->magic != SERIAL_MAGIC) {
  179. printk(badmagic, name, routine);
  180. return 1;
  181. }
  182. #endif
  183. return 0;
  184. }
  185. /*
  186. * This is used to figure out the divisor speeds and the timeouts
  187. */
  188. static int baud_table[] = {
  189. 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
  190. 9600, 19200, 38400, 57600, 115200, 0 };
  191. /*
  192. * Reading and writing Z8530 registers.
  193. */
  194. static inline unsigned char read_zsreg(struct dec_zschannel *channel,
  195. unsigned char reg)
  196. {
  197. unsigned char retval;
  198. if (reg != 0) {
  199. *channel->control = reg & 0xf;
  200. fast_iob(); RECOVERY_DELAY;
  201. }
  202. retval = *channel->control;
  203. RECOVERY_DELAY;
  204. return retval;
  205. }
  206. static inline void write_zsreg(struct dec_zschannel *channel,
  207. unsigned char reg, unsigned char value)
  208. {
  209. if (reg != 0) {
  210. *channel->control = reg & 0xf;
  211. fast_iob(); RECOVERY_DELAY;
  212. }
  213. *channel->control = value;
  214. fast_iob(); RECOVERY_DELAY;
  215. return;
  216. }
  217. static inline unsigned char read_zsdata(struct dec_zschannel *channel)
  218. {
  219. unsigned char retval;
  220. retval = *channel->data;
  221. RECOVERY_DELAY;
  222. return retval;
  223. }
  224. static inline void write_zsdata(struct dec_zschannel *channel,
  225. unsigned char value)
  226. {
  227. *channel->data = value;
  228. fast_iob(); RECOVERY_DELAY;
  229. return;
  230. }
  231. static inline void load_zsregs(struct dec_zschannel *channel,
  232. unsigned char *regs)
  233. {
  234. /* ZS_CLEARERR(channel);
  235. ZS_CLEARFIFO(channel); */
  236. /* Load 'em up */
  237. write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
  238. write_zsreg(channel, R5, regs[R5] & ~TxENAB);
  239. write_zsreg(channel, R4, regs[R4]);
  240. write_zsreg(channel, R9, regs[R9]);
  241. write_zsreg(channel, R1, regs[R1]);
  242. write_zsreg(channel, R2, regs[R2]);
  243. write_zsreg(channel, R10, regs[R10]);
  244. write_zsreg(channel, R11, regs[R11]);
  245. write_zsreg(channel, R12, regs[R12]);
  246. write_zsreg(channel, R13, regs[R13]);
  247. write_zsreg(channel, R14, regs[R14]);
  248. write_zsreg(channel, R15, regs[R15]);
  249. write_zsreg(channel, R3, regs[R3]);
  250. write_zsreg(channel, R5, regs[R5]);
  251. return;
  252. }
  253. /* Sets or clears DTR/RTS on the requested line */
  254. static inline void zs_rtsdtr(struct dec_serial *info, int which, int set)
  255. {
  256. unsigned long flags;
  257. spin_lock_irqsave(&zs_lock, flags);
  258. if (info->zs_channel != info->zs_chan_a) {
  259. if (set) {
  260. info->zs_chan_a->curregs[5] |= (which & (RTS | DTR));
  261. } else {
  262. info->zs_chan_a->curregs[5] &= ~(which & (RTS | DTR));
  263. }
  264. write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
  265. }
  266. spin_unlock_irqrestore(&zs_lock, flags);
  267. }
  268. /* Utility routines for the Zilog */
  269. static inline int get_zsbaud(struct dec_serial *ss)
  270. {
  271. struct dec_zschannel *channel = ss->zs_channel;
  272. int brg;
  273. /* The baud rate is split up between two 8-bit registers in
  274. * what is termed 'BRG time constant' format in my docs for
  275. * the chip, it is a function of the clk rate the chip is
  276. * receiving which happens to be constant.
  277. */
  278. brg = (read_zsreg(channel, 13) << 8);
  279. brg |= read_zsreg(channel, 12);
  280. return BRG_TO_BPS(brg, (zs_parms->clock/(ss->clk_divisor)));
  281. }
  282. /* On receive, this clears errors and the receiver interrupts */
  283. static inline void rs_recv_clear(struct dec_zschannel *zsc)
  284. {
  285. write_zsreg(zsc, 0, ERR_RES);
  286. write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
  287. }
  288. /*
  289. * ----------------------------------------------------------------------
  290. *
  291. * Here starts the interrupt handling routines. All of the following
  292. * subroutines are declared as inline and are folded into
  293. * rs_interrupt(). They were separated out for readability's sake.
  294. *
  295. * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
  296. * -----------------------------------------------------------------------
  297. */
  298. /*
  299. * This routine is used by the interrupt handler to schedule
  300. * processing in the software interrupt portion of the driver.
  301. */
  302. static void rs_sched_event(struct dec_serial *info, int event)
  303. {
  304. info->event |= 1 << event;
  305. tasklet_schedule(&info->tlet);
  306. }
  307. static void receive_chars(struct dec_serial *info)
  308. {
  309. struct tty_struct *tty = info->tty;
  310. unsigned char ch, stat, flag;
  311. while ((read_zsreg(info->zs_channel, R0) & Rx_CH_AV) != 0) {
  312. stat = read_zsreg(info->zs_channel, R1);
  313. ch = read_zsdata(info->zs_channel);
  314. if (!tty && (!info->hook || !info->hook->rx_char))
  315. continue;
  316. flag = TTY_NORMAL;
  317. if (info->tty_break) {
  318. info->tty_break = 0;
  319. flag = TTY_BREAK;
  320. if (info->flags & ZILOG_SAK)
  321. do_SAK(tty);
  322. /* Ignore the null char got when BREAK is removed. */
  323. if (ch == 0)
  324. continue;
  325. } else {
  326. if (stat & Rx_OVR) {
  327. flag = TTY_OVERRUN;
  328. } else if (stat & FRM_ERR) {
  329. flag = TTY_FRAME;
  330. } else if (stat & PAR_ERR) {
  331. flag = TTY_PARITY;
  332. }
  333. if (flag != TTY_NORMAL)
  334. /* reset the error indication */
  335. write_zsreg(info->zs_channel, R0, ERR_RES);
  336. }
  337. #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
  338. !defined(MODULE)
  339. if (break_pressed && info->line == sercons.index) {
  340. /* Ignore the null char got when BREAK is removed. */
  341. if (ch == 0)
  342. continue;
  343. if (time_before(jiffies, break_pressed + HZ * 5)) {
  344. handle_sysrq(ch, NULL);
  345. break_pressed = 0;
  346. continue;
  347. }
  348. break_pressed = 0;
  349. }
  350. #endif
  351. if (info->hook && info->hook->rx_char) {
  352. (*info->hook->rx_char)(ch, flag);
  353. return;
  354. }
  355. tty_insert_flip_char(tty, ch, flag);
  356. }
  357. if (tty)
  358. tty_flip_buffer_push(tty);
  359. }
  360. static void transmit_chars(struct dec_serial *info)
  361. {
  362. if ((read_zsreg(info->zs_channel, R0) & Tx_BUF_EMP) == 0)
  363. return;
  364. info->tx_active = 0;
  365. if (info->x_char) {
  366. /* Send next char */
  367. write_zsdata(info->zs_channel, info->x_char);
  368. info->x_char = 0;
  369. info->tx_active = 1;
  370. return;
  371. }
  372. if ((info->xmit_cnt <= 0) || (info->tty && info->tty->stopped)
  373. || info->tx_stopped) {
  374. write_zsreg(info->zs_channel, R0, RES_Tx_P);
  375. return;
  376. }
  377. /* Send char */
  378. write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
  379. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  380. info->xmit_cnt--;
  381. info->tx_active = 1;
  382. if (info->xmit_cnt < WAKEUP_CHARS)
  383. rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
  384. }
  385. static void status_handle(struct dec_serial *info)
  386. {
  387. unsigned char stat;
  388. /* Get status from Read Register 0 */
  389. stat = read_zsreg(info->zs_channel, R0);
  390. if ((stat & BRK_ABRT) && !(info->read_reg_zero & BRK_ABRT)) {
  391. #if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && \
  392. !defined(MODULE)
  393. if (info->line == sercons.index) {
  394. if (!break_pressed)
  395. break_pressed = jiffies;
  396. } else
  397. #endif
  398. info->tty_break = 1;
  399. }
  400. if (info->zs_channel != info->zs_chan_a) {
  401. /* Check for DCD transitions */
  402. if (info->tty && !C_CLOCAL(info->tty) &&
  403. ((stat ^ info->read_reg_zero) & DCD) != 0 ) {
  404. if (stat & DCD) {
  405. wake_up_interruptible(&info->open_wait);
  406. } else {
  407. tty_hangup(info->tty);
  408. }
  409. }
  410. /* Check for CTS transitions */
  411. if (info->tty && C_CRTSCTS(info->tty)) {
  412. if ((stat & CTS) != 0) {
  413. if (info->tx_stopped) {
  414. info->tx_stopped = 0;
  415. if (!info->tx_active)
  416. transmit_chars(info);
  417. }
  418. } else {
  419. info->tx_stopped = 1;
  420. }
  421. }
  422. }
  423. /* Clear status condition... */
  424. write_zsreg(info->zs_channel, R0, RES_EXT_INT);
  425. info->read_reg_zero = stat;
  426. }
  427. /*
  428. * This is the serial driver's generic interrupt routine
  429. */
  430. static irqreturn_t rs_interrupt(int irq, void *dev_id)
  431. {
  432. struct dec_serial *info = (struct dec_serial *) dev_id;
  433. irqreturn_t status = IRQ_NONE;
  434. unsigned char zs_intreg;
  435. int shift;
  436. /* NOTE: The read register 3, which holds the irq status,
  437. * does so for both channels on each chip. Although
  438. * the status value itself must be read from the A
  439. * channel and is only valid when read from channel A.
  440. * Yes... broken hardware...
  441. */
  442. #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
  443. if (info->zs_chan_a == info->zs_channel)
  444. shift = 3; /* Channel A */
  445. else
  446. shift = 0; /* Channel B */
  447. for (;;) {
  448. zs_intreg = read_zsreg(info->zs_chan_a, R3) >> shift;
  449. if ((zs_intreg & CHAN_IRQMASK) == 0)
  450. break;
  451. status = IRQ_HANDLED;
  452. if (zs_intreg & CHBRxIP) {
  453. receive_chars(info);
  454. }
  455. if (zs_intreg & CHBTxIP) {
  456. transmit_chars(info);
  457. }
  458. if (zs_intreg & CHBEXT) {
  459. status_handle(info);
  460. }
  461. }
  462. /* Why do we need this ? */
  463. write_zsreg(info->zs_channel, 0, RES_H_IUS);
  464. return status;
  465. }
  466. #ifdef ZS_DEBUG_REGS
  467. void zs_dump (void) {
  468. int i, j;
  469. for (i = 0; i < zs_channels_found; i++) {
  470. struct dec_zschannel *ch = &zs_channels[i];
  471. if ((long)ch->control == UNI_IO_BASE+UNI_SCC1A_CTRL) {
  472. for (j = 0; j < 15; j++) {
  473. printk("W%d = 0x%x\t",
  474. j, (int)ch->curregs[j]);
  475. }
  476. for (j = 0; j < 15; j++) {
  477. printk("R%d = 0x%x\t",
  478. j, (int)read_zsreg(ch,j));
  479. }
  480. printk("\n\n");
  481. }
  482. }
  483. }
  484. #endif
  485. /*
  486. * -------------------------------------------------------------------
  487. * Here ends the serial interrupt routines.
  488. * -------------------------------------------------------------------
  489. */
  490. /*
  491. * ------------------------------------------------------------
  492. * rs_stop() and rs_start()
  493. *
  494. * This routines are called before setting or resetting tty->stopped.
  495. * ------------------------------------------------------------
  496. */
  497. static void rs_stop(struct tty_struct *tty)
  498. {
  499. struct dec_serial *info = (struct dec_serial *)tty->driver_data;
  500. unsigned long flags;
  501. if (serial_paranoia_check(info, tty->name, "rs_stop"))
  502. return;
  503. #if 1
  504. spin_lock_irqsave(&zs_lock, flags);
  505. if (info->zs_channel->curregs[5] & TxENAB) {
  506. info->zs_channel->curregs[5] &= ~TxENAB;
  507. write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
  508. }
  509. spin_unlock_irqrestore(&zs_lock, flags);
  510. #endif
  511. }
  512. static void rs_start(struct tty_struct *tty)
  513. {
  514. struct dec_serial *info = (struct dec_serial *)tty->driver_data;
  515. unsigned long flags;
  516. if (serial_paranoia_check(info, tty->name, "rs_start"))
  517. return;
  518. spin_lock_irqsave(&zs_lock, flags);
  519. #if 1
  520. if (info->xmit_cnt && info->xmit_buf && !(info->zs_channel->curregs[5] & TxENAB)) {
  521. info->zs_channel->curregs[5] |= TxENAB;
  522. write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
  523. }
  524. #else
  525. if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
  526. transmit_chars(info);
  527. }
  528. #endif
  529. spin_unlock_irqrestore(&zs_lock, flags);
  530. }
  531. /*
  532. * This routine is used to handle the "bottom half" processing for the
  533. * serial driver, known also the "software interrupt" processing.
  534. * This processing is done at the kernel interrupt level, after the
  535. * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
  536. * is where time-consuming activities which can not be done in the
  537. * interrupt driver proper are done; the interrupt driver schedules
  538. * them using rs_sched_event(), and they get done here.
  539. */
  540. static void do_softint(unsigned long private_)
  541. {
  542. struct dec_serial *info = (struct dec_serial *) private_;
  543. struct tty_struct *tty;
  544. tty = info->tty;
  545. if (!tty)
  546. return;
  547. if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
  548. tty_wakeup(tty);
  549. }
  550. static int zs_startup(struct dec_serial * info)
  551. {
  552. unsigned long flags;
  553. if (info->flags & ZILOG_INITIALIZED)
  554. return 0;
  555. if (!info->xmit_buf) {
  556. info->xmit_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
  557. if (!info->xmit_buf)
  558. return -ENOMEM;
  559. }
  560. spin_lock_irqsave(&zs_lock, flags);
  561. #ifdef SERIAL_DEBUG_OPEN
  562. printk("starting up ttyS%d (irq %d)...", info->line, info->irq);
  563. #endif
  564. /*
  565. * Clear the receive FIFO.
  566. */
  567. ZS_CLEARFIFO(info->zs_channel);
  568. info->xmit_fifo_size = 1;
  569. /*
  570. * Clear the interrupt registers.
  571. */
  572. write_zsreg(info->zs_channel, R0, ERR_RES);
  573. write_zsreg(info->zs_channel, R0, RES_H_IUS);
  574. /*
  575. * Set the speed of the serial port
  576. */
  577. change_speed(info);
  578. /*
  579. * Turn on RTS and DTR.
  580. */
  581. zs_rtsdtr(info, RTS | DTR, 1);
  582. /*
  583. * Finally, enable sequencing and interrupts
  584. */
  585. info->zs_channel->curregs[R1] &= ~RxINT_MASK;
  586. info->zs_channel->curregs[R1] |= (RxINT_ALL | TxINT_ENAB |
  587. EXT_INT_ENAB);
  588. info->zs_channel->curregs[R3] |= RxENABLE;
  589. info->zs_channel->curregs[R5] |= TxENAB;
  590. info->zs_channel->curregs[R15] |= (DCDIE | CTSIE | TxUIE | BRKIE);
  591. write_zsreg(info->zs_channel, R1, info->zs_channel->curregs[R1]);
  592. write_zsreg(info->zs_channel, R3, info->zs_channel->curregs[R3]);
  593. write_zsreg(info->zs_channel, R5, info->zs_channel->curregs[R5]);
  594. write_zsreg(info->zs_channel, R15, info->zs_channel->curregs[R15]);
  595. /*
  596. * And clear the interrupt registers again for luck.
  597. */
  598. write_zsreg(info->zs_channel, R0, ERR_RES);
  599. write_zsreg(info->zs_channel, R0, RES_H_IUS);
  600. /* Save the current value of RR0 */
  601. info->read_reg_zero = read_zsreg(info->zs_channel, R0);
  602. if (info->tty)
  603. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  604. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  605. info->flags |= ZILOG_INITIALIZED;
  606. spin_unlock_irqrestore(&zs_lock, flags);
  607. return 0;
  608. }
  609. /*
  610. * This routine will shutdown a serial port; interrupts are disabled, and
  611. * DTR is dropped if the hangup on close termio flag is on.
  612. */
  613. static void shutdown(struct dec_serial * info)
  614. {
  615. unsigned long flags;
  616. if (!(info->flags & ZILOG_INITIALIZED))
  617. return;
  618. #ifdef SERIAL_DEBUG_OPEN
  619. printk("Shutting down serial port %d (irq %d)....", info->line,
  620. info->irq);
  621. #endif
  622. spin_lock_irqsave(&zs_lock, flags);
  623. if (info->xmit_buf) {
  624. free_page((unsigned long) info->xmit_buf);
  625. info->xmit_buf = 0;
  626. }
  627. info->zs_channel->curregs[1] = 0;
  628. write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]); /* no interrupts */
  629. info->zs_channel->curregs[3] &= ~RxENABLE;
  630. write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
  631. info->zs_channel->curregs[5] &= ~TxENAB;
  632. write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
  633. if (!info->tty || C_HUPCL(info->tty)) {
  634. zs_rtsdtr(info, RTS | DTR, 0);
  635. }
  636. if (info->tty)
  637. set_bit(TTY_IO_ERROR, &info->tty->flags);
  638. info->flags &= ~ZILOG_INITIALIZED;
  639. spin_unlock_irqrestore(&zs_lock, flags);
  640. }
  641. /*
  642. * This routine is called to set the UART divisor registers to match
  643. * the specified baud rate for a serial port.
  644. */
  645. static void change_speed(struct dec_serial *info)
  646. {
  647. unsigned cflag;
  648. int i;
  649. int brg, bits;
  650. unsigned long flags;
  651. if (!info->hook) {
  652. if (!info->tty || !info->tty->termios)
  653. return;
  654. cflag = info->tty->termios->c_cflag;
  655. if (!info->port)
  656. return;
  657. } else {
  658. cflag = info->hook->cflags;
  659. }
  660. i = cflag & CBAUD;
  661. if (i & CBAUDEX) {
  662. i &= ~CBAUDEX;
  663. if (i < 1 || i > 2) {
  664. if (!info->hook)
  665. info->tty->termios->c_cflag &= ~CBAUDEX;
  666. else
  667. info->hook->cflags &= ~CBAUDEX;
  668. } else
  669. i += 15;
  670. }
  671. spin_lock_irqsave(&zs_lock, flags);
  672. info->zs_baud = baud_table[i];
  673. if (info->zs_baud) {
  674. brg = BPS_TO_BRG(info->zs_baud, zs_parms->clock/info->clk_divisor);
  675. info->zs_channel->curregs[12] = (brg & 255);
  676. info->zs_channel->curregs[13] = ((brg >> 8) & 255);
  677. zs_rtsdtr(info, DTR, 1);
  678. } else {
  679. zs_rtsdtr(info, RTS | DTR, 0);
  680. return;
  681. }
  682. /* byte size and parity */
  683. info->zs_channel->curregs[3] &= ~RxNBITS_MASK;
  684. info->zs_channel->curregs[5] &= ~TxNBITS_MASK;
  685. switch (cflag & CSIZE) {
  686. case CS5:
  687. bits = 7;
  688. info->zs_channel->curregs[3] |= Rx5;
  689. info->zs_channel->curregs[5] |= Tx5;
  690. break;
  691. case CS6:
  692. bits = 8;
  693. info->zs_channel->curregs[3] |= Rx6;
  694. info->zs_channel->curregs[5] |= Tx6;
  695. break;
  696. case CS7:
  697. bits = 9;
  698. info->zs_channel->curregs[3] |= Rx7;
  699. info->zs_channel->curregs[5] |= Tx7;
  700. break;
  701. case CS8:
  702. default: /* defaults to 8 bits */
  703. bits = 10;
  704. info->zs_channel->curregs[3] |= Rx8;
  705. info->zs_channel->curregs[5] |= Tx8;
  706. break;
  707. }
  708. info->timeout = ((info->xmit_fifo_size*HZ*bits) / info->zs_baud);
  709. info->timeout += HZ/50; /* Add .02 seconds of slop */
  710. info->zs_channel->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
  711. if (cflag & CSTOPB) {
  712. info->zs_channel->curregs[4] |= SB2;
  713. } else {
  714. info->zs_channel->curregs[4] |= SB1;
  715. }
  716. if (cflag & PARENB) {
  717. info->zs_channel->curregs[4] |= PAR_ENA;
  718. }
  719. if (!(cflag & PARODD)) {
  720. info->zs_channel->curregs[4] |= PAR_EVEN;
  721. }
  722. if (!(cflag & CLOCAL)) {
  723. if (!(info->zs_channel->curregs[15] & DCDIE))
  724. info->read_reg_zero = read_zsreg(info->zs_channel, 0);
  725. info->zs_channel->curregs[15] |= DCDIE;
  726. } else
  727. info->zs_channel->curregs[15] &= ~DCDIE;
  728. if (cflag & CRTSCTS) {
  729. info->zs_channel->curregs[15] |= CTSIE;
  730. if ((read_zsreg(info->zs_channel, 0) & CTS) == 0)
  731. info->tx_stopped = 1;
  732. } else {
  733. info->zs_channel->curregs[15] &= ~CTSIE;
  734. info->tx_stopped = 0;
  735. }
  736. /* Load up the new values */
  737. load_zsregs(info->zs_channel, info->zs_channel->curregs);
  738. spin_unlock_irqrestore(&zs_lock, flags);
  739. }
  740. static void rs_flush_chars(struct tty_struct *tty)
  741. {
  742. struct dec_serial *info = (struct dec_serial *)tty->driver_data;
  743. unsigned long flags;
  744. if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
  745. return;
  746. if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
  747. !info->xmit_buf)
  748. return;
  749. /* Enable transmitter */
  750. spin_lock_irqsave(&zs_lock, flags);
  751. transmit_chars(info);
  752. spin_unlock_irqrestore(&zs_lock, flags);
  753. }
  754. static int rs_write(struct tty_struct * tty,
  755. const unsigned char *buf, int count)
  756. {
  757. int c, total = 0;
  758. struct dec_serial *info = (struct dec_serial *)tty->driver_data;
  759. unsigned long flags;
  760. if (serial_paranoia_check(info, tty->name, "rs_write"))
  761. return 0;
  762. if (!tty || !info->xmit_buf)
  763. return 0;
  764. while (1) {
  765. spin_lock_irqsave(&zs_lock, flags);
  766. c = min(count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  767. SERIAL_XMIT_SIZE - info->xmit_head));
  768. if (c <= 0)
  769. break;
  770. memcpy(info->xmit_buf + info->xmit_head, buf, c);
  771. info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
  772. info->xmit_cnt += c;
  773. spin_unlock_irqrestore(&zs_lock, flags);
  774. buf += c;
  775. count -= c;
  776. total += c;
  777. }
  778. if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
  779. && !info->tx_active)
  780. transmit_chars(info);
  781. spin_unlock_irqrestore(&zs_lock, flags);
  782. return total;
  783. }
  784. static int rs_write_room(struct tty_struct *tty)
  785. {
  786. struct dec_serial *info = (struct dec_serial *)tty->driver_data;
  787. int ret;
  788. if (serial_paranoia_check(info, tty->name, "rs_write_room"))
  789. return 0;
  790. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  791. if (ret < 0)
  792. ret = 0;
  793. return ret;
  794. }
  795. static int rs_chars_in_buffer(struct tty_struct *tty)
  796. {
  797. struct dec_serial *info = (struct dec_serial *)tty->driver_data;
  798. if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
  799. return 0;
  800. return info->xmit_cnt;
  801. }
  802. static void rs_flush_buffer(struct tty_struct *tty)
  803. {
  804. struct dec_serial *info = (struct dec_serial *)tty->driver_data;
  805. if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
  806. return;
  807. spin_lock_irq(&zs_lock);
  808. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  809. spin_unlock_irq(&zs_lock);
  810. tty_wakeup(tty);
  811. }
  812. /*
  813. * ------------------------------------------------------------
  814. * rs_throttle()
  815. *
  816. * This routine is called by the upper-layer tty layer to signal that
  817. * incoming characters should be throttled.
  818. * ------------------------------------------------------------
  819. */
  820. static void rs_throttle(struct tty_struct * tty)
  821. {
  822. struct dec_serial *info = (struct dec_serial *)tty->driver_data;
  823. unsigned long flags;
  824. #ifdef SERIAL_DEBUG_THROTTLE
  825. char buf[64];
  826. printk("throttle %s: %d....\n", _tty_name(tty, buf),
  827. tty->ldisc.chars_in_buffer(tty));
  828. #endif
  829. if (serial_paranoia_check(info, tty->name, "rs_throttle"))
  830. return;
  831. if (I_IXOFF(tty)) {
  832. spin_lock_irqsave(&zs_lock, flags);
  833. info->x_char = STOP_CHAR(tty);
  834. if (!info->tx_active)
  835. transmit_chars(info);
  836. spin_unlock_irqrestore(&zs_lock, flags);
  837. }
  838. if (C_CRTSCTS(tty)) {
  839. zs_rtsdtr(info, RTS, 0);
  840. }
  841. }
  842. static void rs_unthrottle(struct tty_struct * tty)
  843. {
  844. struct dec_serial *info = (struct dec_serial *)tty->driver_data;
  845. unsigned long flags;
  846. #ifdef SERIAL_DEBUG_THROTTLE
  847. char buf[64];
  848. printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
  849. tty->ldisc.chars_in_buffer(tty));
  850. #endif
  851. if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
  852. return;
  853. if (I_IXOFF(tty)) {
  854. spin_lock_irqsave(&zs_lock, flags);
  855. if (info->x_char)
  856. info->x_char = 0;
  857. else {
  858. info->x_char = START_CHAR(tty);
  859. if (!info->tx_active)
  860. transmit_chars(info);
  861. }
  862. spin_unlock_irqrestore(&zs_lock, flags);
  863. }
  864. if (C_CRTSCTS(tty)) {
  865. zs_rtsdtr(info, RTS, 1);
  866. }
  867. }
  868. /*
  869. * ------------------------------------------------------------
  870. * rs_ioctl() and friends
  871. * ------------------------------------------------------------
  872. */
  873. static int get_serial_info(struct dec_serial * info,
  874. struct serial_struct * retinfo)
  875. {
  876. struct serial_struct tmp;
  877. if (!retinfo)
  878. return -EFAULT;
  879. memset(&tmp, 0, sizeof(tmp));
  880. tmp.type = info->type;
  881. tmp.line = info->line;
  882. tmp.port = info->port;
  883. tmp.irq = info->irq;
  884. tmp.flags = info->flags;
  885. tmp.baud_base = info->baud_base;
  886. tmp.close_delay = info->close_delay;
  887. tmp.closing_wait = info->closing_wait;
  888. tmp.custom_divisor = info->custom_divisor;
  889. return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
  890. }
  891. static int set_serial_info(struct dec_serial * info,
  892. struct serial_struct * new_info)
  893. {
  894. struct serial_struct new_serial;
  895. struct dec_serial old_info;
  896. int retval = 0;
  897. if (!new_info)
  898. return -EFAULT;
  899. copy_from_user(&new_serial,new_info,sizeof(new_serial));
  900. old_info = *info;
  901. if (!capable(CAP_SYS_ADMIN)) {
  902. if ((new_serial.baud_base != info->baud_base) ||
  903. (new_serial.type != info->type) ||
  904. (new_serial.close_delay != info->close_delay) ||
  905. ((new_serial.flags & ~ZILOG_USR_MASK) !=
  906. (info->flags & ~ZILOG_USR_MASK)))
  907. return -EPERM;
  908. info->flags = ((info->flags & ~ZILOG_USR_MASK) |
  909. (new_serial.flags & ZILOG_USR_MASK));
  910. info->custom_divisor = new_serial.custom_divisor;
  911. goto check_and_exit;
  912. }
  913. if (info->count > 1)
  914. return -EBUSY;
  915. /*
  916. * OK, past this point, all the error checking has been done.
  917. * At this point, we start making changes.....
  918. */
  919. info->baud_base = new_serial.baud_base;
  920. info->flags = ((info->flags & ~ZILOG_FLAGS) |
  921. (new_serial.flags & ZILOG_FLAGS));
  922. info->type = new_serial.type;
  923. info->close_delay = new_serial.close_delay;
  924. info->closing_wait = new_serial.closing_wait;
  925. check_and_exit:
  926. retval = zs_startup(info);
  927. return retval;
  928. }
  929. /*
  930. * get_lsr_info - get line status register info
  931. *
  932. * Purpose: Let user call ioctl() to get info when the UART physically
  933. * is emptied. On bus types like RS485, the transmitter must
  934. * release the bus after transmitting. This must be done when
  935. * the transmit shift register is empty, not be done when the
  936. * transmit holding register is empty. This functionality
  937. * allows an RS485 driver to be written in user space.
  938. */
  939. static int get_lsr_info(struct dec_serial * info, unsigned int *value)
  940. {
  941. unsigned char status;
  942. spin_lock(&zs_lock);
  943. status = read_zsreg(info->zs_channel, 0);
  944. spin_unlock_irq(&zs_lock);
  945. put_user(status,value);
  946. return 0;
  947. }
  948. static int rs_tiocmget(struct tty_struct *tty, struct file *file)
  949. {
  950. struct dec_serial * info = (struct dec_serial *)tty->driver_data;
  951. unsigned char control, status_a, status_b;
  952. unsigned int result;
  953. if (info->hook)
  954. return -ENODEV;
  955. if (serial_paranoia_check(info, tty->name, __FUNCTION__))
  956. return -ENODEV;
  957. if (tty->flags & (1 << TTY_IO_ERROR))
  958. return -EIO;
  959. if (info->zs_channel == info->zs_chan_a)
  960. result = 0;
  961. else {
  962. spin_lock(&zs_lock);
  963. control = info->zs_chan_a->curregs[5];
  964. status_a = read_zsreg(info->zs_chan_a, 0);
  965. status_b = read_zsreg(info->zs_channel, 0);
  966. spin_unlock_irq(&zs_lock);
  967. result = ((control & RTS) ? TIOCM_RTS: 0)
  968. | ((control & DTR) ? TIOCM_DTR: 0)
  969. | ((status_b & DCD) ? TIOCM_CAR: 0)
  970. | ((status_a & DCD) ? TIOCM_RNG: 0)
  971. | ((status_a & SYNC_HUNT) ? TIOCM_DSR: 0)
  972. | ((status_b & CTS) ? TIOCM_CTS: 0);
  973. }
  974. return result;
  975. }
  976. static int rs_tiocmset(struct tty_struct *tty, struct file *file,
  977. unsigned int set, unsigned int clear)
  978. {
  979. struct dec_serial * info = (struct dec_serial *)tty->driver_data;
  980. if (info->hook)
  981. return -ENODEV;
  982. if (serial_paranoia_check(info, tty->name, __FUNCTION__))
  983. return -ENODEV;
  984. if (tty->flags & (1 << TTY_IO_ERROR))
  985. return -EIO;
  986. if (info->zs_channel == info->zs_chan_a)
  987. return 0;
  988. spin_lock(&zs_lock);
  989. if (set & TIOCM_RTS)
  990. info->zs_chan_a->curregs[5] |= RTS;
  991. if (set & TIOCM_DTR)
  992. info->zs_chan_a->curregs[5] |= DTR;
  993. if (clear & TIOCM_RTS)
  994. info->zs_chan_a->curregs[5] &= ~RTS;
  995. if (clear & TIOCM_DTR)
  996. info->zs_chan_a->curregs[5] &= ~DTR;
  997. write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
  998. spin_unlock_irq(&zs_lock);
  999. return 0;
  1000. }
  1001. /*
  1002. * rs_break - turn transmit break condition on/off
  1003. */
  1004. static void rs_break(struct tty_struct *tty, int break_state)
  1005. {
  1006. struct dec_serial *info = (struct dec_serial *) tty->driver_data;
  1007. unsigned long flags;
  1008. if (serial_paranoia_check(info, tty->name, "rs_break"))
  1009. return;
  1010. if (!info->port)
  1011. return;
  1012. spin_lock_irqsave(&zs_lock, flags);
  1013. if (break_state == -1)
  1014. info->zs_channel->curregs[5] |= SND_BRK;
  1015. else
  1016. info->zs_channel->curregs[5] &= ~SND_BRK;
  1017. write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
  1018. spin_unlock_irqrestore(&zs_lock, flags);
  1019. }
  1020. static int rs_ioctl(struct tty_struct *tty, struct file * file,
  1021. unsigned int cmd, unsigned long arg)
  1022. {
  1023. struct dec_serial * info = (struct dec_serial *)tty->driver_data;
  1024. if (info->hook)
  1025. return -ENODEV;
  1026. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  1027. return -ENODEV;
  1028. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1029. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
  1030. (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
  1031. if (tty->flags & (1 << TTY_IO_ERROR))
  1032. return -EIO;
  1033. }
  1034. switch (cmd) {
  1035. case TIOCGSERIAL:
  1036. if (!access_ok(VERIFY_WRITE, (void *)arg,
  1037. sizeof(struct serial_struct)))
  1038. return -EFAULT;
  1039. return get_serial_info(info, (struct serial_struct *)arg);
  1040. case TIOCSSERIAL:
  1041. return set_serial_info(info, (struct serial_struct *)arg);
  1042. case TIOCSERGETLSR: /* Get line status register */
  1043. if (!access_ok(VERIFY_WRITE, (void *)arg,
  1044. sizeof(unsigned int)))
  1045. return -EFAULT;
  1046. return get_lsr_info(info, (unsigned int *)arg);
  1047. case TIOCSERGSTRUCT:
  1048. if (!access_ok(VERIFY_WRITE, (void *)arg,
  1049. sizeof(struct dec_serial)))
  1050. return -EFAULT;
  1051. copy_from_user((struct dec_serial *)arg, info,
  1052. sizeof(struct dec_serial));
  1053. return 0;
  1054. default:
  1055. return -ENOIOCTLCMD;
  1056. }
  1057. return 0;
  1058. }
  1059. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  1060. {
  1061. struct dec_serial *info = (struct dec_serial *)tty->driver_data;
  1062. int was_stopped;
  1063. if (tty->termios->c_cflag == old_termios->c_cflag)
  1064. return;
  1065. was_stopped = info->tx_stopped;
  1066. change_speed(info);
  1067. if (was_stopped && !info->tx_stopped)
  1068. rs_start(tty);
  1069. }
  1070. /*
  1071. * ------------------------------------------------------------
  1072. * rs_close()
  1073. *
  1074. * This routine is called when the serial port gets closed.
  1075. * Wait for the last remaining data to be sent.
  1076. * ------------------------------------------------------------
  1077. */
  1078. static void rs_close(struct tty_struct *tty, struct file * filp)
  1079. {
  1080. struct dec_serial * info = (struct dec_serial *)tty->driver_data;
  1081. unsigned long flags;
  1082. if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
  1083. return;
  1084. spin_lock_irqsave(&zs_lock, flags);
  1085. if (tty_hung_up_p(filp)) {
  1086. spin_unlock_irqrestore(&zs_lock, flags);
  1087. return;
  1088. }
  1089. #ifdef SERIAL_DEBUG_OPEN
  1090. printk("rs_close ttyS%d, count = %d\n", info->line, info->count);
  1091. #endif
  1092. if ((tty->count == 1) && (info->count != 1)) {
  1093. /*
  1094. * Uh, oh. tty->count is 1, which means that the tty
  1095. * structure will be freed. Info->count should always
  1096. * be one in these conditions. If it's greater than
  1097. * one, we've got real problems, since it means the
  1098. * serial port won't be shutdown.
  1099. */
  1100. printk("rs_close: bad serial port count; tty->count is 1, "
  1101. "info->count is %d\n", info->count);
  1102. info->count = 1;
  1103. }
  1104. if (--info->count < 0) {
  1105. printk("rs_close: bad serial port count for ttyS%d: %d\n",
  1106. info->line, info->count);
  1107. info->count = 0;
  1108. }
  1109. if (info->count) {
  1110. spin_unlock_irqrestore(&zs_lock, flags);
  1111. return;
  1112. }
  1113. info->flags |= ZILOG_CLOSING;
  1114. /*
  1115. * Now we wait for the transmit buffer to clear; and we notify
  1116. * the line discipline to only process XON/XOFF characters.
  1117. */
  1118. tty->closing = 1;
  1119. if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
  1120. tty_wait_until_sent(tty, info->closing_wait);
  1121. /*
  1122. * At this point we stop accepting input. To do this, we
  1123. * disable the receiver and receive interrupts.
  1124. */
  1125. info->zs_channel->curregs[3] &= ~RxENABLE;
  1126. write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
  1127. info->zs_channel->curregs[1] = 0; /* disable any rx ints */
  1128. write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
  1129. ZS_CLEARFIFO(info->zs_channel);
  1130. if (info->flags & ZILOG_INITIALIZED) {
  1131. /*
  1132. * Before we drop DTR, make sure the SCC transmitter
  1133. * has completely drained.
  1134. */
  1135. rs_wait_until_sent(tty, info->timeout);
  1136. }
  1137. shutdown(info);
  1138. if (tty->driver->flush_buffer)
  1139. tty->driver->flush_buffer(tty);
  1140. tty_ldisc_flush(tty);
  1141. tty->closing = 0;
  1142. info->event = 0;
  1143. info->tty = 0;
  1144. if (info->blocked_open) {
  1145. if (info->close_delay) {
  1146. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  1147. }
  1148. wake_up_interruptible(&info->open_wait);
  1149. }
  1150. info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CLOSING);
  1151. wake_up_interruptible(&info->close_wait);
  1152. spin_unlock_irqrestore(&zs_lock, flags);
  1153. }
  1154. /*
  1155. * rs_wait_until_sent() --- wait until the transmitter is empty
  1156. */
  1157. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  1158. {
  1159. struct dec_serial *info = (struct dec_serial *) tty->driver_data;
  1160. unsigned long orig_jiffies;
  1161. int char_time;
  1162. if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
  1163. return;
  1164. orig_jiffies = jiffies;
  1165. /*
  1166. * Set the check interval to be 1/5 of the estimated time to
  1167. * send a single character, and make it at least 1. The check
  1168. * interval should also be less than the timeout.
  1169. */
  1170. char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
  1171. char_time = char_time / 5;
  1172. if (char_time == 0)
  1173. char_time = 1;
  1174. if (timeout)
  1175. char_time = min(char_time, timeout);
  1176. while ((read_zsreg(info->zs_channel, 1) & Tx_BUF_EMP) == 0) {
  1177. msleep_interruptible(jiffies_to_msecs(char_time));
  1178. if (signal_pending(current))
  1179. break;
  1180. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1181. break;
  1182. }
  1183. current->state = TASK_RUNNING;
  1184. }
  1185. /*
  1186. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  1187. */
  1188. static void rs_hangup(struct tty_struct *tty)
  1189. {
  1190. struct dec_serial * info = (struct dec_serial *)tty->driver_data;
  1191. if (serial_paranoia_check(info, tty->name, "rs_hangup"))
  1192. return;
  1193. rs_flush_buffer(tty);
  1194. shutdown(info);
  1195. info->event = 0;
  1196. info->count = 0;
  1197. info->flags &= ~ZILOG_NORMAL_ACTIVE;
  1198. info->tty = 0;
  1199. wake_up_interruptible(&info->open_wait);
  1200. }
  1201. /*
  1202. * ------------------------------------------------------------
  1203. * rs_open() and friends
  1204. * ------------------------------------------------------------
  1205. */
  1206. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  1207. struct dec_serial *info)
  1208. {
  1209. DECLARE_WAITQUEUE(wait, current);
  1210. int retval;
  1211. int do_clocal = 0;
  1212. /*
  1213. * If the device is in the middle of being closed, then block
  1214. * until it's done, and then try again.
  1215. */
  1216. if (info->flags & ZILOG_CLOSING) {
  1217. interruptible_sleep_on(&info->close_wait);
  1218. #ifdef SERIAL_DO_RESTART
  1219. return ((info->flags & ZILOG_HUP_NOTIFY) ?
  1220. -EAGAIN : -ERESTARTSYS);
  1221. #else
  1222. return -EAGAIN;
  1223. #endif
  1224. }
  1225. /*
  1226. * If non-blocking mode is set, or the port is not enabled,
  1227. * then make the check up front and then exit.
  1228. */
  1229. if ((filp->f_flags & O_NONBLOCK) ||
  1230. (tty->flags & (1 << TTY_IO_ERROR))) {
  1231. info->flags |= ZILOG_NORMAL_ACTIVE;
  1232. return 0;
  1233. }
  1234. if (tty->termios->c_cflag & CLOCAL)
  1235. do_clocal = 1;
  1236. /*
  1237. * Block waiting for the carrier detect and the line to become
  1238. * free (i.e., not in use by the callout). While we are in
  1239. * this loop, info->count is dropped by one, so that
  1240. * rs_close() knows when to free things. We restore it upon
  1241. * exit, either normal or abnormal.
  1242. */
  1243. retval = 0;
  1244. add_wait_queue(&info->open_wait, &wait);
  1245. #ifdef SERIAL_DEBUG_OPEN
  1246. printk("block_til_ready before block: ttyS%d, count = %d\n",
  1247. info->line, info->count);
  1248. #endif
  1249. spin_lock(&zs_lock);
  1250. if (!tty_hung_up_p(filp))
  1251. info->count--;
  1252. spin_unlock_irq(&zs_lock);
  1253. info->blocked_open++;
  1254. while (1) {
  1255. spin_lock(&zs_lock);
  1256. if (tty->termios->c_cflag & CBAUD)
  1257. zs_rtsdtr(info, RTS | DTR, 1);
  1258. spin_unlock_irq(&zs_lock);
  1259. set_current_state(TASK_INTERRUPTIBLE);
  1260. if (tty_hung_up_p(filp) ||
  1261. !(info->flags & ZILOG_INITIALIZED)) {
  1262. #ifdef SERIAL_DO_RESTART
  1263. if (info->flags & ZILOG_HUP_NOTIFY)
  1264. retval = -EAGAIN;
  1265. else
  1266. retval = -ERESTARTSYS;
  1267. #else
  1268. retval = -EAGAIN;
  1269. #endif
  1270. break;
  1271. }
  1272. if (!(info->flags & ZILOG_CLOSING) &&
  1273. (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
  1274. break;
  1275. if (signal_pending(current)) {
  1276. retval = -ERESTARTSYS;
  1277. break;
  1278. }
  1279. #ifdef SERIAL_DEBUG_OPEN
  1280. printk("block_til_ready blocking: ttyS%d, count = %d\n",
  1281. info->line, info->count);
  1282. #endif
  1283. schedule();
  1284. }
  1285. current->state = TASK_RUNNING;
  1286. remove_wait_queue(&info->open_wait, &wait);
  1287. if (!tty_hung_up_p(filp))
  1288. info->count++;
  1289. info->blocked_open--;
  1290. #ifdef SERIAL_DEBUG_OPEN
  1291. printk("block_til_ready after blocking: ttyS%d, count = %d\n",
  1292. info->line, info->count);
  1293. #endif
  1294. if (retval)
  1295. return retval;
  1296. info->flags |= ZILOG_NORMAL_ACTIVE;
  1297. return 0;
  1298. }
  1299. /*
  1300. * This routine is called whenever a serial port is opened. It
  1301. * enables interrupts for a serial port, linking in its ZILOG structure into
  1302. * the IRQ chain. It also performs the serial-specific
  1303. * initialization for the tty structure.
  1304. */
  1305. static int rs_open(struct tty_struct *tty, struct file * filp)
  1306. {
  1307. struct dec_serial *info;
  1308. int retval, line;
  1309. line = tty->index;
  1310. if ((line < 0) || (line >= zs_channels_found))
  1311. return -ENODEV;
  1312. info = zs_soft + line;
  1313. if (info->hook)
  1314. return -ENODEV;
  1315. if (serial_paranoia_check(info, tty->name, "rs_open"))
  1316. return -ENODEV;
  1317. #ifdef SERIAL_DEBUG_OPEN
  1318. printk("rs_open %s, count = %d\n", tty->name, info->count);
  1319. #endif
  1320. info->count++;
  1321. tty->driver_data = info;
  1322. info->tty = tty;
  1323. /*
  1324. * If the port is the middle of closing, bail out now
  1325. */
  1326. if (tty_hung_up_p(filp) ||
  1327. (info->flags & ZILOG_CLOSING)) {
  1328. if (info->flags & ZILOG_CLOSING)
  1329. interruptible_sleep_on(&info->close_wait);
  1330. #ifdef SERIAL_DO_RESTART
  1331. return ((info->flags & ZILOG_HUP_NOTIFY) ?
  1332. -EAGAIN : -ERESTARTSYS);
  1333. #else
  1334. return -EAGAIN;
  1335. #endif
  1336. }
  1337. /*
  1338. * Start up serial port
  1339. */
  1340. retval = zs_startup(info);
  1341. if (retval)
  1342. return retval;
  1343. retval = block_til_ready(tty, filp, info);
  1344. if (retval) {
  1345. #ifdef SERIAL_DEBUG_OPEN
  1346. printk("rs_open returning after block_til_ready with %d\n",
  1347. retval);
  1348. #endif
  1349. return retval;
  1350. }
  1351. #ifdef CONFIG_SERIAL_DEC_CONSOLE
  1352. if (sercons.cflag && sercons.index == line) {
  1353. tty->termios->c_cflag = sercons.cflag;
  1354. sercons.cflag = 0;
  1355. change_speed(info);
  1356. }
  1357. #endif
  1358. #ifdef SERIAL_DEBUG_OPEN
  1359. printk("rs_open %s successful...", tty->name);
  1360. #endif
  1361. /* tty->low_latency = 1; */
  1362. return 0;
  1363. }
  1364. /* Finally, routines used to initialize the serial driver. */
  1365. static void __init show_serial_version(void)
  1366. {
  1367. printk("DECstation Z8530 serial driver version 0.09\n");
  1368. }
  1369. /* Initialize Z8530s zs_channels
  1370. */
  1371. static void __init probe_sccs(void)
  1372. {
  1373. struct dec_serial **pp;
  1374. int i, n, n_chips = 0, n_channels, chip, channel;
  1375. unsigned long flags;
  1376. /*
  1377. * did we get here by accident?
  1378. */
  1379. if(!BUS_PRESENT) {
  1380. printk("Not on JUNKIO machine, skipping probe_sccs\n");
  1381. return;
  1382. }
  1383. switch(mips_machtype) {
  1384. #ifdef CONFIG_MACH_DECSTATION
  1385. case MACH_DS5000_2X0:
  1386. case MACH_DS5900:
  1387. n_chips = 2;
  1388. zs_parms = &ds_parms;
  1389. zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
  1390. zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
  1391. break;
  1392. case MACH_DS5000_1XX:
  1393. n_chips = 2;
  1394. zs_parms = &ds_parms;
  1395. zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
  1396. zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
  1397. break;
  1398. case MACH_DS5000_XX:
  1399. n_chips = 1;
  1400. zs_parms = &ds_parms;
  1401. zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
  1402. break;
  1403. #endif
  1404. default:
  1405. panic("zs: unsupported bus");
  1406. }
  1407. if (!zs_parms)
  1408. panic("zs: uninitialized parms");
  1409. pp = &zs_chain;
  1410. n_channels = 0;
  1411. for (chip = 0; chip < n_chips; chip++) {
  1412. for (channel = 0; channel <= 1; channel++) {
  1413. /*
  1414. * The sccs reside on the high byte of the 16 bit IOBUS
  1415. */
  1416. zs_channels[n_channels].control =
  1417. (volatile void *)CKSEG1ADDR(dec_kn_slot_base +
  1418. (0 == chip ? zs_parms->scc0 : zs_parms->scc1) +
  1419. (0 == channel ? zs_parms->channel_a_offset :
  1420. zs_parms->channel_b_offset));
  1421. zs_channels[n_channels].data =
  1422. zs_channels[n_channels].control + 4;
  1423. #ifndef CONFIG_SERIAL_DEC_CONSOLE
  1424. /*
  1425. * We're called early and memory managment isn't up, yet.
  1426. * Thus request_region would fail.
  1427. */
  1428. if (!request_region((unsigned long)
  1429. zs_channels[n_channels].control,
  1430. ZS_CHAN_IO_SIZE, "SCC"))
  1431. panic("SCC I/O region is not free");
  1432. #endif
  1433. zs_soft[n_channels].zs_channel = &zs_channels[n_channels];
  1434. /* HACK alert! */
  1435. if (!(chip & 1))
  1436. zs_soft[n_channels].irq = zs_parms->irq0;
  1437. else
  1438. zs_soft[n_channels].irq = zs_parms->irq1;
  1439. /*
  1440. * Identification of channel A. Location of channel A
  1441. * inside chip depends on mapping of internal address
  1442. * the chip decodes channels by.
  1443. * CHANNEL_A_NR returns either 0 (in case of
  1444. * DECstations) or 1 (in case of Baget).
  1445. */
  1446. if (CHANNEL_A_NR == channel)
  1447. zs_soft[n_channels].zs_chan_a =
  1448. &zs_channels[n_channels+1-2*CHANNEL_A_NR];
  1449. else
  1450. zs_soft[n_channels].zs_chan_a =
  1451. &zs_channels[n_channels];
  1452. *pp = &zs_soft[n_channels];
  1453. pp = &zs_soft[n_channels].zs_next;
  1454. n_channels++;
  1455. }
  1456. }
  1457. *pp = 0;
  1458. zs_channels_found = n_channels;
  1459. for (n = 0; n < zs_channels_found; n++) {
  1460. for (i = 0; i < 16; i++) {
  1461. zs_soft[n].zs_channel->curregs[i] = zs_init_regs[i];
  1462. }
  1463. }
  1464. spin_lock_irqsave(&zs_lock, flags);
  1465. for (n = 0; n < zs_channels_found; n++) {
  1466. if (n % 2 == 0) {
  1467. write_zsreg(zs_soft[n].zs_chan_a, R9, FHWRES);
  1468. udelay(10);
  1469. write_zsreg(zs_soft[n].zs_chan_a, R9, 0);
  1470. }
  1471. load_zsregs(zs_soft[n].zs_channel,
  1472. zs_soft[n].zs_channel->curregs);
  1473. }
  1474. spin_unlock_irqrestore(&zs_lock, flags);
  1475. }
  1476. static const struct tty_operations serial_ops = {
  1477. .open = rs_open,
  1478. .close = rs_close,
  1479. .write = rs_write,
  1480. .flush_chars = rs_flush_chars,
  1481. .write_room = rs_write_room,
  1482. .chars_in_buffer = rs_chars_in_buffer,
  1483. .flush_buffer = rs_flush_buffer,
  1484. .ioctl = rs_ioctl,
  1485. .throttle = rs_throttle,
  1486. .unthrottle = rs_unthrottle,
  1487. .set_termios = rs_set_termios,
  1488. .stop = rs_stop,
  1489. .start = rs_start,
  1490. .hangup = rs_hangup,
  1491. .break_ctl = rs_break,
  1492. .wait_until_sent = rs_wait_until_sent,
  1493. .tiocmget = rs_tiocmget,
  1494. .tiocmset = rs_tiocmset,
  1495. };
  1496. /* zs_init inits the driver */
  1497. int __init zs_init(void)
  1498. {
  1499. int channel, i;
  1500. struct dec_serial *info;
  1501. if(!BUS_PRESENT)
  1502. return -ENODEV;
  1503. /* Find out how many Z8530 SCCs we have */
  1504. if (zs_chain == 0)
  1505. probe_sccs();
  1506. serial_driver = alloc_tty_driver(zs_channels_found);
  1507. if (!serial_driver)
  1508. return -ENOMEM;
  1509. show_serial_version();
  1510. /* Initialize the tty_driver structure */
  1511. /* Not all of this is exactly right for us. */
  1512. serial_driver->owner = THIS_MODULE;
  1513. serial_driver->name = "ttyS";
  1514. serial_driver->major = TTY_MAJOR;
  1515. serial_driver->minor_start = 64;
  1516. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1517. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  1518. serial_driver->init_termios = tty_std_termios;
  1519. serial_driver->init_termios.c_cflag =
  1520. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1521. serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  1522. tty_set_operations(serial_driver, &serial_ops);
  1523. if (tty_register_driver(serial_driver))
  1524. panic("Couldn't register serial driver");
  1525. for (info = zs_chain, i = 0; info; info = info->zs_next, i++) {
  1526. /* Needed before interrupts are enabled. */
  1527. info->tty = 0;
  1528. info->x_char = 0;
  1529. if (info->hook && info->hook->init_info) {
  1530. (*info->hook->init_info)(info);
  1531. continue;
  1532. }
  1533. info->magic = SERIAL_MAGIC;
  1534. info->port = (int) info->zs_channel->control;
  1535. info->line = i;
  1536. info->custom_divisor = 16;
  1537. info->close_delay = 50;
  1538. info->closing_wait = 3000;
  1539. info->event = 0;
  1540. info->count = 0;
  1541. info->blocked_open = 0;
  1542. tasklet_init(&info->tlet, do_softint, (unsigned long)info);
  1543. init_waitqueue_head(&info->open_wait);
  1544. init_waitqueue_head(&info->close_wait);
  1545. printk("ttyS%02d at 0x%08x (irq = %d) is a Z85C30 SCC\n",
  1546. info->line, info->port, info->irq);
  1547. tty_register_device(serial_driver, info->line, NULL);
  1548. }
  1549. for (channel = 0; channel < zs_channels_found; ++channel) {
  1550. zs_soft[channel].clk_divisor = 16;
  1551. zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
  1552. if (request_irq(zs_soft[channel].irq, rs_interrupt, IRQF_SHARED,
  1553. "scc", &zs_soft[channel]))
  1554. printk(KERN_ERR "decserial: can't get irq %d\n",
  1555. zs_soft[channel].irq);
  1556. if (zs_soft[channel].hook) {
  1557. zs_startup(&zs_soft[channel]);
  1558. if (zs_soft[channel].hook->init_channel)
  1559. (*zs_soft[channel].hook->init_channel)
  1560. (&zs_soft[channel]);
  1561. }
  1562. }
  1563. return 0;
  1564. }
  1565. /*
  1566. * polling I/O routines
  1567. */
  1568. static int zs_poll_tx_char(void *handle, unsigned char ch)
  1569. {
  1570. struct dec_serial *info = handle;
  1571. struct dec_zschannel *chan = info->zs_channel;
  1572. int ret;
  1573. if(chan) {
  1574. int loops = 10000;
  1575. while (loops && !(read_zsreg(chan, 0) & Tx_BUF_EMP))
  1576. loops--;
  1577. if (loops) {
  1578. write_zsdata(chan, ch);
  1579. ret = 0;
  1580. } else
  1581. ret = -EAGAIN;
  1582. return ret;
  1583. } else
  1584. return -ENODEV;
  1585. }
  1586. static int zs_poll_rx_char(void *handle)
  1587. {
  1588. struct dec_serial *info = handle;
  1589. struct dec_zschannel *chan = info->zs_channel;
  1590. int ret;
  1591. if(chan) {
  1592. int loops = 10000;
  1593. while (loops && !(read_zsreg(chan, 0) & Rx_CH_AV))
  1594. loops--;
  1595. if (loops)
  1596. ret = read_zsdata(chan);
  1597. else
  1598. ret = -EAGAIN;
  1599. return ret;
  1600. } else
  1601. return -ENODEV;
  1602. }
  1603. int register_zs_hook(unsigned int channel, struct dec_serial_hook *hook)
  1604. {
  1605. struct dec_serial *info = &zs_soft[channel];
  1606. if (info->hook) {
  1607. printk("%s: line %d has already a hook registered\n",
  1608. __FUNCTION__, channel);
  1609. return 0;
  1610. } else {
  1611. hook->poll_rx_char = zs_poll_rx_char;
  1612. hook->poll_tx_char = zs_poll_tx_char;
  1613. info->hook = hook;
  1614. return 1;
  1615. }
  1616. }
  1617. int unregister_zs_hook(unsigned int channel)
  1618. {
  1619. struct dec_serial *info = &zs_soft[channel];
  1620. if (info->hook) {
  1621. info->hook = NULL;
  1622. return 1;
  1623. } else {
  1624. printk("%s: trying to unregister hook on line %d,"
  1625. " but none is registered\n", __FUNCTION__, channel);
  1626. return 0;
  1627. }
  1628. }
  1629. /*
  1630. * ------------------------------------------------------------
  1631. * Serial console driver
  1632. * ------------------------------------------------------------
  1633. */
  1634. #ifdef CONFIG_SERIAL_DEC_CONSOLE
  1635. /*
  1636. * Print a string to the serial port trying not to disturb
  1637. * any possible real use of the port...
  1638. */
  1639. static void serial_console_write(struct console *co, const char *s,
  1640. unsigned count)
  1641. {
  1642. struct dec_serial *info;
  1643. int i;
  1644. info = zs_soft + co->index;
  1645. for (i = 0; i < count; i++, s++) {
  1646. if(*s == '\n')
  1647. zs_poll_tx_char(info, '\r');
  1648. zs_poll_tx_char(info, *s);
  1649. }
  1650. }
  1651. static struct tty_driver *serial_console_device(struct console *c, int *index)
  1652. {
  1653. *index = c->index;
  1654. return serial_driver;
  1655. }
  1656. /*
  1657. * Setup initial baud/bits/parity. We do two things here:
  1658. * - construct a cflag setting for the first rs_open()
  1659. * - initialize the serial port
  1660. * Return non-zero if we didn't find a serial port.
  1661. */
  1662. static int __init serial_console_setup(struct console *co, char *options)
  1663. {
  1664. struct dec_serial *info;
  1665. int baud = 9600;
  1666. int bits = 8;
  1667. int parity = 'n';
  1668. int cflag = CREAD | HUPCL | CLOCAL;
  1669. int clk_divisor = 16;
  1670. int brg;
  1671. char *s;
  1672. unsigned long flags;
  1673. if(!BUS_PRESENT)
  1674. return -ENODEV;
  1675. info = zs_soft + co->index;
  1676. if (zs_chain == 0)
  1677. probe_sccs();
  1678. info->is_cons = 1;
  1679. if (options) {
  1680. baud = simple_strtoul(options, NULL, 10);
  1681. s = options;
  1682. while(*s >= '0' && *s <= '9')
  1683. s++;
  1684. if (*s)
  1685. parity = *s++;
  1686. if (*s)
  1687. bits = *s - '0';
  1688. }
  1689. /*
  1690. * Now construct a cflag setting.
  1691. */
  1692. switch(baud) {
  1693. case 1200:
  1694. cflag |= B1200;
  1695. break;
  1696. case 2400:
  1697. cflag |= B2400;
  1698. break;
  1699. case 4800:
  1700. cflag |= B4800;
  1701. break;
  1702. case 19200:
  1703. cflag |= B19200;
  1704. break;
  1705. case 38400:
  1706. cflag |= B38400;
  1707. break;
  1708. case 57600:
  1709. cflag |= B57600;
  1710. break;
  1711. case 115200:
  1712. cflag |= B115200;
  1713. break;
  1714. case 9600:
  1715. default:
  1716. cflag |= B9600;
  1717. /*
  1718. * Set this to a sane value to prevent a divide error.
  1719. */
  1720. baud = 9600;
  1721. break;
  1722. }
  1723. switch(bits) {
  1724. case 7:
  1725. cflag |= CS7;
  1726. break;
  1727. default:
  1728. case 8:
  1729. cflag |= CS8;
  1730. break;
  1731. }
  1732. switch(parity) {
  1733. case 'o': case 'O':
  1734. cflag |= PARODD;
  1735. break;
  1736. case 'e': case 'E':
  1737. cflag |= PARENB;
  1738. break;
  1739. }
  1740. co->cflag = cflag;
  1741. spin_lock_irqsave(&zs_lock, flags);
  1742. /*
  1743. * Set up the baud rate generator.
  1744. */
  1745. brg = BPS_TO_BRG(baud, zs_parms->clock / clk_divisor);
  1746. info->zs_channel->curregs[R12] = (brg & 255);
  1747. info->zs_channel->curregs[R13] = ((brg >> 8) & 255);
  1748. /*
  1749. * Set byte size and parity.
  1750. */
  1751. if (bits == 7) {
  1752. info->zs_channel->curregs[R3] |= Rx7;
  1753. info->zs_channel->curregs[R5] |= Tx7;
  1754. } else {
  1755. info->zs_channel->curregs[R3] |= Rx8;
  1756. info->zs_channel->curregs[R5] |= Tx8;
  1757. }
  1758. if (cflag & PARENB) {
  1759. info->zs_channel->curregs[R4] |= PAR_ENA;
  1760. }
  1761. if (!(cflag & PARODD)) {
  1762. info->zs_channel->curregs[R4] |= PAR_EVEN;
  1763. }
  1764. info->zs_channel->curregs[R4] |= SB1;
  1765. /*
  1766. * Turn on RTS and DTR.
  1767. */
  1768. zs_rtsdtr(info, RTS | DTR, 1);
  1769. /*
  1770. * Finally, enable sequencing.
  1771. */
  1772. info->zs_channel->curregs[R3] |= RxENABLE;
  1773. info->zs_channel->curregs[R5] |= TxENAB;
  1774. /*
  1775. * Clear the interrupt registers.
  1776. */
  1777. write_zsreg(info->zs_channel, R0, ERR_RES);
  1778. write_zsreg(info->zs_channel, R0, RES_H_IUS);
  1779. /*
  1780. * Load up the new values.
  1781. */
  1782. load_zsregs(info->zs_channel, info->zs_channel->curregs);
  1783. /* Save the current value of RR0 */
  1784. info->read_reg_zero = read_zsreg(info->zs_channel, R0);
  1785. zs_soft[co->index].clk_divisor = clk_divisor;
  1786. zs_soft[co->index].zs_baud = get_zsbaud(&zs_soft[co->index]);
  1787. spin_unlock_irqrestore(&zs_lock, flags);
  1788. return 0;
  1789. }
  1790. static struct console sercons = {
  1791. .name = "ttyS",
  1792. .write = serial_console_write,
  1793. .device = serial_console_device,
  1794. .setup = serial_console_setup,
  1795. .flags = CON_PRINTBUFFER,
  1796. .index = -1,
  1797. };
  1798. /*
  1799. * Register console.
  1800. */
  1801. void __init zs_serial_console_init(void)
  1802. {
  1803. register_console(&sercons);
  1804. }
  1805. #endif /* ifdef CONFIG_SERIAL_DEC_CONSOLE */
  1806. #ifdef CONFIG_KGDB
  1807. struct dec_zschannel *zs_kgdbchan;
  1808. static unsigned char scc_inittab[] = {
  1809. 9, 0x80, /* reset A side (CHRA) */
  1810. 13, 0, /* set baud rate divisor */
  1811. 12, 1,
  1812. 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
  1813. 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
  1814. 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
  1815. 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
  1816. 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
  1817. };
  1818. /* These are for receiving and sending characters under the kgdb
  1819. * source level kernel debugger.
  1820. */
  1821. void putDebugChar(char kgdb_char)
  1822. {
  1823. struct dec_zschannel *chan = zs_kgdbchan;
  1824. while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
  1825. RECOVERY_DELAY;
  1826. write_zsdata(chan, kgdb_char);
  1827. }
  1828. char getDebugChar(void)
  1829. {
  1830. struct dec_zschannel *chan = zs_kgdbchan;
  1831. while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
  1832. eieio(); /*barrier();*/
  1833. return read_zsdata(chan);
  1834. }
  1835. void kgdb_interruptible(int yes)
  1836. {
  1837. struct dec_zschannel *chan = zs_kgdbchan;
  1838. int one, nine;
  1839. nine = read_zsreg(chan, 9);
  1840. if (yes == 1) {
  1841. one = EXT_INT_ENAB|RxINT_ALL;
  1842. nine |= MIE;
  1843. printk("turning serial ints on\n");
  1844. } else {
  1845. one = RxINT_DISAB;
  1846. nine &= ~MIE;
  1847. printk("turning serial ints off\n");
  1848. }
  1849. write_zsreg(chan, 1, one);
  1850. write_zsreg(chan, 9, nine);
  1851. }
  1852. static int kgdbhook_init_channel(void *handle)
  1853. {
  1854. return 0;
  1855. }
  1856. static void kgdbhook_init_info(void *handle)
  1857. {
  1858. }
  1859. static void kgdbhook_rx_char(void *handle, unsigned char ch, unsigned char fl)
  1860. {
  1861. struct dec_serial *info = handle;
  1862. if (fl != TTY_NORMAL)
  1863. return;
  1864. if (ch == 0x03 || ch == '$')
  1865. breakpoint();
  1866. }
  1867. /* This sets up the serial port we're using, and turns on
  1868. * interrupts for that channel, so kgdb is usable once we're done.
  1869. */
  1870. static inline void kgdb_chaninit(struct dec_zschannel *ms, int intson, int bps)
  1871. {
  1872. int brg;
  1873. int i, x;
  1874. volatile char *sccc = ms->control;
  1875. brg = BPS_TO_BRG(bps, zs_parms->clock/16);
  1876. printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
  1877. for (i = 20000; i != 0; --i) {
  1878. x = *sccc; eieio();
  1879. }
  1880. for (i = 0; i < sizeof(scc_inittab); ++i) {
  1881. write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
  1882. i++;
  1883. }
  1884. }
  1885. /* This is called at boot time to prime the kgdb serial debugging
  1886. * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
  1887. * for /dev/ttyb which is determined in setup_arch() from the
  1888. * boot command line flags.
  1889. */
  1890. struct dec_serial_hook zs_kgdbhook = {
  1891. .init_channel = kgdbhook_init_channel,
  1892. .init_info = kgdbhook_init_info,
  1893. .rx_char = kgdbhook_rx_char,
  1894. .cflags = B38400 | CS8 | CLOCAL,
  1895. }
  1896. void __init zs_kgdb_hook(int tty_num)
  1897. {
  1898. /* Find out how many Z8530 SCCs we have */
  1899. if (zs_chain == 0)
  1900. probe_sccs();
  1901. zs_soft[tty_num].zs_channel = &zs_channels[tty_num];
  1902. zs_kgdbchan = zs_soft[tty_num].zs_channel;
  1903. zs_soft[tty_num].change_needed = 0;
  1904. zs_soft[tty_num].clk_divisor = 16;
  1905. zs_soft[tty_num].zs_baud = 38400;
  1906. zs_soft[tty_num].hook = &zs_kgdbhook; /* This runs kgdb */
  1907. /* Turn on transmitter/receiver at 8-bits/char */
  1908. kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
  1909. printk("KGDB: on channel %d initialized\n", tty_num);
  1910. set_debug_traps(); /* init stub */
  1911. }
  1912. #endif /* ifdef CONFIG_KGDB */