zs.c 54 KB

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