zs.c 54 KB

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