zs.c 54 KB

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