zs.c 54 KB

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