zs.c 55 KB

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