zs.c 54 KB

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