zs.c 54 KB

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