sunzilog.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  1. /*
  2. * sunzilog.c
  3. *
  4. * Driver for Zilog serial chips found on Sun workstations and
  5. * servers. This driver could actually be made more generic.
  6. *
  7. * This is based on the old drivers/sbus/char/zs.c code. A lot
  8. * of code has been simply moved over directly from there but
  9. * much has been rewritten. Credits therefore go out to Eddie
  10. * C. Dost, Pete Zaitcev, Ted Ts'o and Alex Buell for their
  11. * work there.
  12. *
  13. * Copyright (C) 2002 David S. Miller (davem@redhat.com)
  14. */
  15. #include <linux/config.h>
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/errno.h>
  20. #include <linux/delay.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/major.h>
  24. #include <linux/string.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/ioport.h>
  27. #include <linux/slab.h>
  28. #include <linux/circ_buf.h>
  29. #include <linux/serial.h>
  30. #include <linux/sysrq.h>
  31. #include <linux/console.h>
  32. #include <linux/spinlock.h>
  33. #ifdef CONFIG_SERIO
  34. #include <linux/serio.h>
  35. #endif
  36. #include <linux/init.h>
  37. #include <asm/io.h>
  38. #include <asm/irq.h>
  39. #ifdef CONFIG_SPARC64
  40. #include <asm/fhc.h>
  41. #endif
  42. #include <asm/sbus.h>
  43. #if defined(CONFIG_SERIAL_SUNZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  44. #define SUPPORT_SYSRQ
  45. #endif
  46. #include <linux/serial_core.h>
  47. #include "suncore.h"
  48. #include "sunzilog.h"
  49. /* On 32-bit sparcs we need to delay after register accesses
  50. * to accommodate sun4 systems, but we do not need to flush writes.
  51. * On 64-bit sparc we only need to flush single writes to ensure
  52. * completion.
  53. */
  54. #ifndef CONFIG_SPARC64
  55. #define ZSDELAY() udelay(5)
  56. #define ZSDELAY_LONG() udelay(20)
  57. #define ZS_WSYNC(channel) do { } while (0)
  58. #else
  59. #define ZSDELAY()
  60. #define ZSDELAY_LONG()
  61. #define ZS_WSYNC(__channel) \
  62. sbus_readb(&((__channel)->control))
  63. #endif
  64. static int num_sunzilog;
  65. #define NUM_SUNZILOG num_sunzilog
  66. #define NUM_CHANNELS (NUM_SUNZILOG * 2)
  67. #define KEYBOARD_LINE 0x2
  68. #define MOUSE_LINE 0x3
  69. #define ZS_CLOCK 4915200 /* Zilog input clock rate. */
  70. #define ZS_CLOCK_DIVISOR 16 /* Divisor this driver uses. */
  71. /*
  72. * We wrap our port structure around the generic uart_port.
  73. */
  74. struct uart_sunzilog_port {
  75. struct uart_port port;
  76. /* IRQ servicing chain. */
  77. struct uart_sunzilog_port *next;
  78. /* Current values of Zilog write registers. */
  79. unsigned char curregs[NUM_ZSREGS];
  80. unsigned int flags;
  81. #define SUNZILOG_FLAG_CONS_KEYB 0x00000001
  82. #define SUNZILOG_FLAG_CONS_MOUSE 0x00000002
  83. #define SUNZILOG_FLAG_IS_CONS 0x00000004
  84. #define SUNZILOG_FLAG_IS_KGDB 0x00000008
  85. #define SUNZILOG_FLAG_MODEM_STATUS 0x00000010
  86. #define SUNZILOG_FLAG_IS_CHANNEL_A 0x00000020
  87. #define SUNZILOG_FLAG_REGS_HELD 0x00000040
  88. #define SUNZILOG_FLAG_TX_STOPPED 0x00000080
  89. #define SUNZILOG_FLAG_TX_ACTIVE 0x00000100
  90. unsigned int cflag;
  91. unsigned char parity_mask;
  92. unsigned char prev_status;
  93. #ifdef CONFIG_SERIO
  94. struct serio *serio;
  95. int serio_open;
  96. #endif
  97. };
  98. #define ZILOG_CHANNEL_FROM_PORT(PORT) ((struct zilog_channel __iomem *)((PORT)->membase))
  99. #define UART_ZILOG(PORT) ((struct uart_sunzilog_port *)(PORT))
  100. #define ZS_IS_KEYB(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_KEYB)
  101. #define ZS_IS_MOUSE(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_MOUSE)
  102. #define ZS_IS_CONS(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CONS)
  103. #define ZS_IS_KGDB(UP) ((UP)->flags & SUNZILOG_FLAG_IS_KGDB)
  104. #define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & SUNZILOG_FLAG_MODEM_STATUS)
  105. #define ZS_IS_CHANNEL_A(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CHANNEL_A)
  106. #define ZS_REGS_HELD(UP) ((UP)->flags & SUNZILOG_FLAG_REGS_HELD)
  107. #define ZS_TX_STOPPED(UP) ((UP)->flags & SUNZILOG_FLAG_TX_STOPPED)
  108. #define ZS_TX_ACTIVE(UP) ((UP)->flags & SUNZILOG_FLAG_TX_ACTIVE)
  109. /* Reading and writing Zilog8530 registers. The delays are to make this
  110. * driver work on the Sun4 which needs a settling delay after each chip
  111. * register access, other machines handle this in hardware via auxiliary
  112. * flip-flops which implement the settle time we do in software.
  113. *
  114. * The port lock must be held and local IRQs must be disabled
  115. * when {read,write}_zsreg is invoked.
  116. */
  117. static unsigned char read_zsreg(struct zilog_channel __iomem *channel,
  118. unsigned char reg)
  119. {
  120. unsigned char retval;
  121. sbus_writeb(reg, &channel->control);
  122. ZSDELAY();
  123. retval = sbus_readb(&channel->control);
  124. ZSDELAY();
  125. return retval;
  126. }
  127. static void write_zsreg(struct zilog_channel __iomem *channel,
  128. unsigned char reg, unsigned char value)
  129. {
  130. sbus_writeb(reg, &channel->control);
  131. ZSDELAY();
  132. sbus_writeb(value, &channel->control);
  133. ZSDELAY();
  134. }
  135. static void sunzilog_clear_fifo(struct zilog_channel __iomem *channel)
  136. {
  137. int i;
  138. for (i = 0; i < 32; i++) {
  139. unsigned char regval;
  140. regval = sbus_readb(&channel->control);
  141. ZSDELAY();
  142. if (regval & Rx_CH_AV)
  143. break;
  144. regval = read_zsreg(channel, R1);
  145. sbus_readb(&channel->data);
  146. ZSDELAY();
  147. if (regval & (PAR_ERR | Rx_OVR | CRC_ERR)) {
  148. sbus_writeb(ERR_RES, &channel->control);
  149. ZSDELAY();
  150. ZS_WSYNC(channel);
  151. }
  152. }
  153. }
  154. /* This function must only be called when the TX is not busy. The UART
  155. * port lock must be held and local interrupts disabled.
  156. */
  157. static void __load_zsregs(struct zilog_channel __iomem *channel, unsigned char *regs)
  158. {
  159. int i;
  160. /* Let pending transmits finish. */
  161. for (i = 0; i < 1000; i++) {
  162. unsigned char stat = read_zsreg(channel, R1);
  163. if (stat & ALL_SNT)
  164. break;
  165. udelay(100);
  166. }
  167. sbus_writeb(ERR_RES, &channel->control);
  168. ZSDELAY();
  169. ZS_WSYNC(channel);
  170. sunzilog_clear_fifo(channel);
  171. /* Disable all interrupts. */
  172. write_zsreg(channel, R1,
  173. regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
  174. /* Set parity, sync config, stop bits, and clock divisor. */
  175. write_zsreg(channel, R4, regs[R4]);
  176. /* Set misc. TX/RX control bits. */
  177. write_zsreg(channel, R10, regs[R10]);
  178. /* Set TX/RX controls sans the enable bits. */
  179. write_zsreg(channel, R3, regs[R3] & ~RxENAB);
  180. write_zsreg(channel, R5, regs[R5] & ~TxENAB);
  181. /* Synchronous mode config. */
  182. write_zsreg(channel, R6, regs[R6]);
  183. write_zsreg(channel, R7, regs[R7]);
  184. /* Don't mess with the interrupt vector (R2, unused by us) and
  185. * master interrupt control (R9). We make sure this is setup
  186. * properly at probe time then never touch it again.
  187. */
  188. /* Disable baud generator. */
  189. write_zsreg(channel, R14, regs[R14] & ~BRENAB);
  190. /* Clock mode control. */
  191. write_zsreg(channel, R11, regs[R11]);
  192. /* Lower and upper byte of baud rate generator divisor. */
  193. write_zsreg(channel, R12, regs[R12]);
  194. write_zsreg(channel, R13, regs[R13]);
  195. /* Now rewrite R14, with BRENAB (if set). */
  196. write_zsreg(channel, R14, regs[R14]);
  197. /* External status interrupt control. */
  198. write_zsreg(channel, R15, regs[R15]);
  199. /* Reset external status interrupts. */
  200. write_zsreg(channel, R0, RES_EXT_INT);
  201. write_zsreg(channel, R0, RES_EXT_INT);
  202. /* Rewrite R3/R5, this time without enables masked. */
  203. write_zsreg(channel, R3, regs[R3]);
  204. write_zsreg(channel, R5, regs[R5]);
  205. /* Rewrite R1, this time without IRQ enabled masked. */
  206. write_zsreg(channel, R1, regs[R1]);
  207. }
  208. /* Reprogram the Zilog channel HW registers with the copies found in the
  209. * software state struct. If the transmitter is busy, we defer this update
  210. * until the next TX complete interrupt. Else, we do it right now.
  211. *
  212. * The UART port lock must be held and local interrupts disabled.
  213. */
  214. static void sunzilog_maybe_update_regs(struct uart_sunzilog_port *up,
  215. struct zilog_channel __iomem *channel)
  216. {
  217. if (!ZS_REGS_HELD(up)) {
  218. if (ZS_TX_ACTIVE(up)) {
  219. up->flags |= SUNZILOG_FLAG_REGS_HELD;
  220. } else {
  221. __load_zsregs(channel, up->curregs);
  222. }
  223. }
  224. }
  225. static void sunzilog_change_mouse_baud(struct uart_sunzilog_port *up)
  226. {
  227. unsigned int cur_cflag = up->cflag;
  228. int brg, new_baud;
  229. up->cflag &= ~CBAUD;
  230. up->cflag |= suncore_mouse_baud_cflag_next(cur_cflag, &new_baud);
  231. brg = BPS_TO_BRG(new_baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  232. up->curregs[R12] = (brg & 0xff);
  233. up->curregs[R13] = (brg >> 8) & 0xff;
  234. sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(&up->port));
  235. }
  236. static void sunzilog_kbdms_receive_chars(struct uart_sunzilog_port *up,
  237. unsigned char ch, int is_break,
  238. struct pt_regs *regs)
  239. {
  240. if (ZS_IS_KEYB(up)) {
  241. /* Stop-A is handled by drivers/char/keyboard.c now. */
  242. #ifdef CONFIG_SERIO
  243. if (up->serio_open)
  244. serio_interrupt(up->serio, ch, 0, regs);
  245. #endif
  246. } else if (ZS_IS_MOUSE(up)) {
  247. int ret = suncore_mouse_baud_detection(ch, is_break);
  248. switch (ret) {
  249. case 2:
  250. sunzilog_change_mouse_baud(up);
  251. /* fallthru */
  252. case 1:
  253. break;
  254. case 0:
  255. #ifdef CONFIG_SERIO
  256. if (up->serio_open)
  257. serio_interrupt(up->serio, ch, 0, regs);
  258. #endif
  259. break;
  260. };
  261. }
  262. }
  263. static struct tty_struct *
  264. sunzilog_receive_chars(struct uart_sunzilog_port *up,
  265. struct zilog_channel __iomem *channel,
  266. struct pt_regs *regs)
  267. {
  268. struct tty_struct *tty;
  269. unsigned char ch, r1;
  270. tty = NULL;
  271. if (up->port.info != NULL && /* Unopened serial console */
  272. up->port.info->tty != NULL) /* Keyboard || mouse */
  273. tty = up->port.info->tty;
  274. for (;;) {
  275. r1 = read_zsreg(channel, R1);
  276. if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
  277. sbus_writeb(ERR_RES, &channel->control);
  278. ZSDELAY();
  279. ZS_WSYNC(channel);
  280. }
  281. ch = sbus_readb(&channel->control);
  282. ZSDELAY();
  283. /* This funny hack depends upon BRK_ABRT not interfering
  284. * with the other bits we care about in R1.
  285. */
  286. if (ch & BRK_ABRT)
  287. r1 |= BRK_ABRT;
  288. if (!(ch & Rx_CH_AV))
  289. break;
  290. ch = sbus_readb(&channel->data);
  291. ZSDELAY();
  292. ch &= up->parity_mask;
  293. if (unlikely(ZS_IS_KEYB(up)) || unlikely(ZS_IS_MOUSE(up))) {
  294. sunzilog_kbdms_receive_chars(up, ch, 0, regs);
  295. continue;
  296. }
  297. if (tty == NULL) {
  298. uart_handle_sysrq_char(&up->port, ch, regs);
  299. continue;
  300. }
  301. if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
  302. tty->flip.work.func((void *)tty);
  303. /*
  304. * The 8250 bails out of the loop here,
  305. * but we need to read everything, or die.
  306. */
  307. if (tty->flip.count >= TTY_FLIPBUF_SIZE)
  308. continue;
  309. }
  310. /* A real serial line, record the character and status. */
  311. *tty->flip.char_buf_ptr = ch;
  312. *tty->flip.flag_buf_ptr = TTY_NORMAL;
  313. up->port.icount.rx++;
  314. if (r1 & (BRK_ABRT | PAR_ERR | Rx_OVR | CRC_ERR)) {
  315. if (r1 & BRK_ABRT) {
  316. r1 &= ~(PAR_ERR | CRC_ERR);
  317. up->port.icount.brk++;
  318. if (uart_handle_break(&up->port))
  319. continue;
  320. }
  321. else if (r1 & PAR_ERR)
  322. up->port.icount.parity++;
  323. else if (r1 & CRC_ERR)
  324. up->port.icount.frame++;
  325. if (r1 & Rx_OVR)
  326. up->port.icount.overrun++;
  327. r1 &= up->port.read_status_mask;
  328. if (r1 & BRK_ABRT)
  329. *tty->flip.flag_buf_ptr = TTY_BREAK;
  330. else if (r1 & PAR_ERR)
  331. *tty->flip.flag_buf_ptr = TTY_PARITY;
  332. else if (r1 & CRC_ERR)
  333. *tty->flip.flag_buf_ptr = TTY_FRAME;
  334. }
  335. if (uart_handle_sysrq_char(&up->port, ch, regs))
  336. continue;
  337. if (up->port.ignore_status_mask == 0xff ||
  338. (r1 & up->port.ignore_status_mask) == 0) {
  339. tty->flip.flag_buf_ptr++;
  340. tty->flip.char_buf_ptr++;
  341. tty->flip.count++;
  342. }
  343. if ((r1 & Rx_OVR) &&
  344. tty->flip.count < TTY_FLIPBUF_SIZE) {
  345. *tty->flip.flag_buf_ptr = TTY_OVERRUN;
  346. tty->flip.flag_buf_ptr++;
  347. tty->flip.char_buf_ptr++;
  348. tty->flip.count++;
  349. }
  350. }
  351. return tty;
  352. }
  353. static void sunzilog_status_handle(struct uart_sunzilog_port *up,
  354. struct zilog_channel __iomem *channel,
  355. struct pt_regs *regs)
  356. {
  357. unsigned char status;
  358. status = sbus_readb(&channel->control);
  359. ZSDELAY();
  360. sbus_writeb(RES_EXT_INT, &channel->control);
  361. ZSDELAY();
  362. ZS_WSYNC(channel);
  363. if (status & BRK_ABRT) {
  364. if (ZS_IS_MOUSE(up))
  365. sunzilog_kbdms_receive_chars(up, 0, 1, regs);
  366. if (ZS_IS_CONS(up)) {
  367. /* Wait for BREAK to deassert to avoid potentially
  368. * confusing the PROM.
  369. */
  370. while (1) {
  371. status = sbus_readb(&channel->control);
  372. ZSDELAY();
  373. if (!(status & BRK_ABRT))
  374. break;
  375. }
  376. sun_do_break();
  377. return;
  378. }
  379. }
  380. if (ZS_WANTS_MODEM_STATUS(up)) {
  381. if (status & SYNC)
  382. up->port.icount.dsr++;
  383. /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
  384. * But it does not tell us which bit has changed, we have to keep
  385. * track of this ourselves.
  386. */
  387. if ((status ^ up->prev_status) ^ DCD)
  388. uart_handle_dcd_change(&up->port,
  389. (status & DCD));
  390. if ((status ^ up->prev_status) ^ CTS)
  391. uart_handle_cts_change(&up->port,
  392. (status & CTS));
  393. wake_up_interruptible(&up->port.info->delta_msr_wait);
  394. }
  395. up->prev_status = status;
  396. }
  397. static void sunzilog_transmit_chars(struct uart_sunzilog_port *up,
  398. struct zilog_channel __iomem *channel)
  399. {
  400. struct circ_buf *xmit;
  401. if (ZS_IS_CONS(up)) {
  402. unsigned char status = sbus_readb(&channel->control);
  403. ZSDELAY();
  404. /* TX still busy? Just wait for the next TX done interrupt.
  405. *
  406. * It can occur because of how we do serial console writes. It would
  407. * be nice to transmit console writes just like we normally would for
  408. * a TTY line. (ie. buffered and TX interrupt driven). That is not
  409. * easy because console writes cannot sleep. One solution might be
  410. * to poll on enough port->xmit space becomming free. -DaveM
  411. */
  412. if (!(status & Tx_BUF_EMP))
  413. return;
  414. }
  415. up->flags &= ~SUNZILOG_FLAG_TX_ACTIVE;
  416. if (ZS_REGS_HELD(up)) {
  417. __load_zsregs(channel, up->curregs);
  418. up->flags &= ~SUNZILOG_FLAG_REGS_HELD;
  419. }
  420. if (ZS_TX_STOPPED(up)) {
  421. up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
  422. goto ack_tx_int;
  423. }
  424. if (up->port.x_char) {
  425. up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
  426. sbus_writeb(up->port.x_char, &channel->data);
  427. ZSDELAY();
  428. ZS_WSYNC(channel);
  429. up->port.icount.tx++;
  430. up->port.x_char = 0;
  431. return;
  432. }
  433. if (up->port.info == NULL)
  434. goto ack_tx_int;
  435. xmit = &up->port.info->xmit;
  436. if (uart_circ_empty(xmit))
  437. goto ack_tx_int;
  438. if (uart_tx_stopped(&up->port))
  439. goto ack_tx_int;
  440. up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
  441. sbus_writeb(xmit->buf[xmit->tail], &channel->data);
  442. ZSDELAY();
  443. ZS_WSYNC(channel);
  444. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  445. up->port.icount.tx++;
  446. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  447. uart_write_wakeup(&up->port);
  448. return;
  449. ack_tx_int:
  450. sbus_writeb(RES_Tx_P, &channel->control);
  451. ZSDELAY();
  452. ZS_WSYNC(channel);
  453. }
  454. static irqreturn_t sunzilog_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  455. {
  456. struct uart_sunzilog_port *up = dev_id;
  457. while (up) {
  458. struct zilog_channel __iomem *channel
  459. = ZILOG_CHANNEL_FROM_PORT(&up->port);
  460. struct tty_struct *tty;
  461. unsigned char r3;
  462. spin_lock(&up->port.lock);
  463. r3 = read_zsreg(channel, R3);
  464. /* Channel A */
  465. tty = NULL;
  466. if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
  467. sbus_writeb(RES_H_IUS, &channel->control);
  468. ZSDELAY();
  469. ZS_WSYNC(channel);
  470. if (r3 & CHARxIP)
  471. tty = sunzilog_receive_chars(up, channel, regs);
  472. if (r3 & CHAEXT)
  473. sunzilog_status_handle(up, channel, regs);
  474. if (r3 & CHATxIP)
  475. sunzilog_transmit_chars(up, channel);
  476. }
  477. spin_unlock(&up->port.lock);
  478. if (tty)
  479. tty_flip_buffer_push(tty);
  480. /* Channel B */
  481. up = up->next;
  482. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  483. spin_lock(&up->port.lock);
  484. tty = NULL;
  485. if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
  486. sbus_writeb(RES_H_IUS, &channel->control);
  487. ZSDELAY();
  488. ZS_WSYNC(channel);
  489. if (r3 & CHBRxIP)
  490. tty = sunzilog_receive_chars(up, channel, regs);
  491. if (r3 & CHBEXT)
  492. sunzilog_status_handle(up, channel, regs);
  493. if (r3 & CHBTxIP)
  494. sunzilog_transmit_chars(up, channel);
  495. }
  496. spin_unlock(&up->port.lock);
  497. if (tty)
  498. tty_flip_buffer_push(tty);
  499. up = up->next;
  500. }
  501. return IRQ_HANDLED;
  502. }
  503. /* A convenient way to quickly get R0 status. The caller must _not_ hold the
  504. * port lock, it is acquired here.
  505. */
  506. static __inline__ unsigned char sunzilog_read_channel_status(struct uart_port *port)
  507. {
  508. struct zilog_channel __iomem *channel;
  509. unsigned char status;
  510. channel = ZILOG_CHANNEL_FROM_PORT(port);
  511. status = sbus_readb(&channel->control);
  512. ZSDELAY();
  513. return status;
  514. }
  515. /* The port lock is not held. */
  516. static unsigned int sunzilog_tx_empty(struct uart_port *port)
  517. {
  518. unsigned long flags;
  519. unsigned char status;
  520. unsigned int ret;
  521. spin_lock_irqsave(&port->lock, flags);
  522. status = sunzilog_read_channel_status(port);
  523. spin_unlock_irqrestore(&port->lock, flags);
  524. if (status & Tx_BUF_EMP)
  525. ret = TIOCSER_TEMT;
  526. else
  527. ret = 0;
  528. return ret;
  529. }
  530. /* The port lock is held and interrupts are disabled. */
  531. static unsigned int sunzilog_get_mctrl(struct uart_port *port)
  532. {
  533. unsigned char status;
  534. unsigned int ret;
  535. status = sunzilog_read_channel_status(port);
  536. ret = 0;
  537. if (status & DCD)
  538. ret |= TIOCM_CAR;
  539. if (status & SYNC)
  540. ret |= TIOCM_DSR;
  541. if (status & CTS)
  542. ret |= TIOCM_CTS;
  543. return ret;
  544. }
  545. /* The port lock is held and interrupts are disabled. */
  546. static void sunzilog_set_mctrl(struct uart_port *port, unsigned int mctrl)
  547. {
  548. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  549. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  550. unsigned char set_bits, clear_bits;
  551. set_bits = clear_bits = 0;
  552. if (mctrl & TIOCM_RTS)
  553. set_bits |= RTS;
  554. else
  555. clear_bits |= RTS;
  556. if (mctrl & TIOCM_DTR)
  557. set_bits |= DTR;
  558. else
  559. clear_bits |= DTR;
  560. /* NOTE: Not subject to 'transmitter active' rule. */
  561. up->curregs[R5] |= set_bits;
  562. up->curregs[R5] &= ~clear_bits;
  563. write_zsreg(channel, R5, up->curregs[R5]);
  564. }
  565. /* The port lock is held and interrupts are disabled. */
  566. static void sunzilog_stop_tx(struct uart_port *port)
  567. {
  568. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  569. up->flags |= SUNZILOG_FLAG_TX_STOPPED;
  570. }
  571. /* The port lock is held and interrupts are disabled. */
  572. static void sunzilog_start_tx(struct uart_port *port)
  573. {
  574. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  575. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  576. unsigned char status;
  577. up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
  578. up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
  579. status = sbus_readb(&channel->control);
  580. ZSDELAY();
  581. /* TX busy? Just wait for the TX done interrupt. */
  582. if (!(status & Tx_BUF_EMP))
  583. return;
  584. /* Send the first character to jump-start the TX done
  585. * IRQ sending engine.
  586. */
  587. if (port->x_char) {
  588. sbus_writeb(port->x_char, &channel->data);
  589. ZSDELAY();
  590. ZS_WSYNC(channel);
  591. port->icount.tx++;
  592. port->x_char = 0;
  593. } else {
  594. struct circ_buf *xmit = &port->info->xmit;
  595. sbus_writeb(xmit->buf[xmit->tail], &channel->data);
  596. ZSDELAY();
  597. ZS_WSYNC(channel);
  598. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  599. port->icount.tx++;
  600. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  601. uart_write_wakeup(&up->port);
  602. }
  603. }
  604. /* The port lock is held. */
  605. static void sunzilog_stop_rx(struct uart_port *port)
  606. {
  607. struct uart_sunzilog_port *up = UART_ZILOG(port);
  608. struct zilog_channel __iomem *channel;
  609. if (ZS_IS_CONS(up))
  610. return;
  611. channel = ZILOG_CHANNEL_FROM_PORT(port);
  612. /* Disable all RX interrupts. */
  613. up->curregs[R1] &= ~RxINT_MASK;
  614. sunzilog_maybe_update_regs(up, channel);
  615. }
  616. /* The port lock is held. */
  617. static void sunzilog_enable_ms(struct uart_port *port)
  618. {
  619. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  620. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  621. unsigned char new_reg;
  622. new_reg = up->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
  623. if (new_reg != up->curregs[R15]) {
  624. up->curregs[R15] = new_reg;
  625. /* NOTE: Not subject to 'transmitter active' rule. */
  626. write_zsreg(channel, R15, up->curregs[R15]);
  627. }
  628. }
  629. /* The port lock is not held. */
  630. static void sunzilog_break_ctl(struct uart_port *port, int break_state)
  631. {
  632. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  633. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  634. unsigned char set_bits, clear_bits, new_reg;
  635. unsigned long flags;
  636. set_bits = clear_bits = 0;
  637. if (break_state)
  638. set_bits |= SND_BRK;
  639. else
  640. clear_bits |= SND_BRK;
  641. spin_lock_irqsave(&port->lock, flags);
  642. new_reg = (up->curregs[R5] | set_bits) & ~clear_bits;
  643. if (new_reg != up->curregs[R5]) {
  644. up->curregs[R5] = new_reg;
  645. /* NOTE: Not subject to 'transmitter active' rule. */
  646. write_zsreg(channel, R5, up->curregs[R5]);
  647. }
  648. spin_unlock_irqrestore(&port->lock, flags);
  649. }
  650. static void __sunzilog_startup(struct uart_sunzilog_port *up)
  651. {
  652. struct zilog_channel __iomem *channel;
  653. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  654. up->prev_status = sbus_readb(&channel->control);
  655. /* Enable receiver and transmitter. */
  656. up->curregs[R3] |= RxENAB;
  657. up->curregs[R5] |= TxENAB;
  658. up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
  659. sunzilog_maybe_update_regs(up, channel);
  660. }
  661. static int sunzilog_startup(struct uart_port *port)
  662. {
  663. struct uart_sunzilog_port *up = UART_ZILOG(port);
  664. unsigned long flags;
  665. if (ZS_IS_CONS(up))
  666. return 0;
  667. spin_lock_irqsave(&port->lock, flags);
  668. __sunzilog_startup(up);
  669. spin_unlock_irqrestore(&port->lock, flags);
  670. return 0;
  671. }
  672. /*
  673. * The test for ZS_IS_CONS is explained by the following e-mail:
  674. *****
  675. * From: Russell King <rmk@arm.linux.org.uk>
  676. * Date: Sun, 8 Dec 2002 10:18:38 +0000
  677. *
  678. * On Sun, Dec 08, 2002 at 02:43:36AM -0500, Pete Zaitcev wrote:
  679. * > I boot my 2.5 boxes using "console=ttyS0,9600" argument,
  680. * > and I noticed that something is not right with reference
  681. * > counting in this case. It seems that when the console
  682. * > is open by kernel initially, this is not accounted
  683. * > as an open, and uart_startup is not called.
  684. *
  685. * That is correct. We are unable to call uart_startup when the serial
  686. * console is initialised because it may need to allocate memory (as
  687. * request_irq does) and the memory allocators may not have been
  688. * initialised.
  689. *
  690. * 1. initialise the port into a state where it can send characters in the
  691. * console write method.
  692. *
  693. * 2. don't do the actual hardware shutdown in your shutdown() method (but
  694. * do the normal software shutdown - ie, free irqs etc)
  695. *****
  696. */
  697. static void sunzilog_shutdown(struct uart_port *port)
  698. {
  699. struct uart_sunzilog_port *up = UART_ZILOG(port);
  700. struct zilog_channel __iomem *channel;
  701. unsigned long flags;
  702. if (ZS_IS_CONS(up))
  703. return;
  704. spin_lock_irqsave(&port->lock, flags);
  705. channel = ZILOG_CHANNEL_FROM_PORT(port);
  706. /* Disable receiver and transmitter. */
  707. up->curregs[R3] &= ~RxENAB;
  708. up->curregs[R5] &= ~TxENAB;
  709. /* Disable all interrupts and BRK assertion. */
  710. up->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
  711. up->curregs[R5] &= ~SND_BRK;
  712. sunzilog_maybe_update_regs(up, channel);
  713. spin_unlock_irqrestore(&port->lock, flags);
  714. }
  715. /* Shared by TTY driver and serial console setup. The port lock is held
  716. * and local interrupts are disabled.
  717. */
  718. static void
  719. sunzilog_convert_to_zs(struct uart_sunzilog_port *up, unsigned int cflag,
  720. unsigned int iflag, int brg)
  721. {
  722. up->curregs[R10] = NRZ;
  723. up->curregs[R11] = TCBR | RCBR;
  724. /* Program BAUD and clock source. */
  725. up->curregs[R4] &= ~XCLK_MASK;
  726. up->curregs[R4] |= X16CLK;
  727. up->curregs[R12] = brg & 0xff;
  728. up->curregs[R13] = (brg >> 8) & 0xff;
  729. up->curregs[R14] = BRSRC | BRENAB;
  730. /* Character size, stop bits, and parity. */
  731. up->curregs[3] &= ~RxN_MASK;
  732. up->curregs[5] &= ~TxN_MASK;
  733. switch (cflag & CSIZE) {
  734. case CS5:
  735. up->curregs[3] |= Rx5;
  736. up->curregs[5] |= Tx5;
  737. up->parity_mask = 0x1f;
  738. break;
  739. case CS6:
  740. up->curregs[3] |= Rx6;
  741. up->curregs[5] |= Tx6;
  742. up->parity_mask = 0x3f;
  743. break;
  744. case CS7:
  745. up->curregs[3] |= Rx7;
  746. up->curregs[5] |= Tx7;
  747. up->parity_mask = 0x7f;
  748. break;
  749. case CS8:
  750. default:
  751. up->curregs[3] |= Rx8;
  752. up->curregs[5] |= Tx8;
  753. up->parity_mask = 0xff;
  754. break;
  755. };
  756. up->curregs[4] &= ~0x0c;
  757. if (cflag & CSTOPB)
  758. up->curregs[4] |= SB2;
  759. else
  760. up->curregs[4] |= SB1;
  761. if (cflag & PARENB)
  762. up->curregs[4] |= PAR_ENAB;
  763. else
  764. up->curregs[4] &= ~PAR_ENAB;
  765. if (!(cflag & PARODD))
  766. up->curregs[4] |= PAR_EVEN;
  767. else
  768. up->curregs[4] &= ~PAR_EVEN;
  769. up->port.read_status_mask = Rx_OVR;
  770. if (iflag & INPCK)
  771. up->port.read_status_mask |= CRC_ERR | PAR_ERR;
  772. if (iflag & (BRKINT | PARMRK))
  773. up->port.read_status_mask |= BRK_ABRT;
  774. up->port.ignore_status_mask = 0;
  775. if (iflag & IGNPAR)
  776. up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
  777. if (iflag & IGNBRK) {
  778. up->port.ignore_status_mask |= BRK_ABRT;
  779. if (iflag & IGNPAR)
  780. up->port.ignore_status_mask |= Rx_OVR;
  781. }
  782. if ((cflag & CREAD) == 0)
  783. up->port.ignore_status_mask = 0xff;
  784. }
  785. /* The port lock is not held. */
  786. static void
  787. sunzilog_set_termios(struct uart_port *port, struct termios *termios,
  788. struct termios *old)
  789. {
  790. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  791. unsigned long flags;
  792. int baud, brg;
  793. baud = uart_get_baud_rate(port, termios, old, 1200, 76800);
  794. spin_lock_irqsave(&up->port.lock, flags);
  795. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  796. sunzilog_convert_to_zs(up, termios->c_cflag, termios->c_iflag, brg);
  797. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  798. up->flags |= SUNZILOG_FLAG_MODEM_STATUS;
  799. else
  800. up->flags &= ~SUNZILOG_FLAG_MODEM_STATUS;
  801. up->cflag = termios->c_cflag;
  802. sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));
  803. uart_update_timeout(port, termios->c_cflag, baud);
  804. spin_unlock_irqrestore(&up->port.lock, flags);
  805. }
  806. static const char *sunzilog_type(struct uart_port *port)
  807. {
  808. return "SunZilog";
  809. }
  810. /* We do not request/release mappings of the registers here, this
  811. * happens at early serial probe time.
  812. */
  813. static void sunzilog_release_port(struct uart_port *port)
  814. {
  815. }
  816. static int sunzilog_request_port(struct uart_port *port)
  817. {
  818. return 0;
  819. }
  820. /* These do not need to do anything interesting either. */
  821. static void sunzilog_config_port(struct uart_port *port, int flags)
  822. {
  823. }
  824. /* We do not support letting the user mess with the divisor, IRQ, etc. */
  825. static int sunzilog_verify_port(struct uart_port *port, struct serial_struct *ser)
  826. {
  827. return -EINVAL;
  828. }
  829. static struct uart_ops sunzilog_pops = {
  830. .tx_empty = sunzilog_tx_empty,
  831. .set_mctrl = sunzilog_set_mctrl,
  832. .get_mctrl = sunzilog_get_mctrl,
  833. .stop_tx = sunzilog_stop_tx,
  834. .start_tx = sunzilog_start_tx,
  835. .stop_rx = sunzilog_stop_rx,
  836. .enable_ms = sunzilog_enable_ms,
  837. .break_ctl = sunzilog_break_ctl,
  838. .startup = sunzilog_startup,
  839. .shutdown = sunzilog_shutdown,
  840. .set_termios = sunzilog_set_termios,
  841. .type = sunzilog_type,
  842. .release_port = sunzilog_release_port,
  843. .request_port = sunzilog_request_port,
  844. .config_port = sunzilog_config_port,
  845. .verify_port = sunzilog_verify_port,
  846. };
  847. static struct uart_sunzilog_port *sunzilog_port_table;
  848. static struct zilog_layout __iomem **sunzilog_chip_regs;
  849. static struct uart_sunzilog_port *sunzilog_irq_chain;
  850. static int zilog_irq = -1;
  851. static struct uart_driver sunzilog_reg = {
  852. .owner = THIS_MODULE,
  853. .driver_name = "ttyS",
  854. .devfs_name = "tts/",
  855. .dev_name = "ttyS",
  856. .major = TTY_MAJOR,
  857. };
  858. static void * __init alloc_one_table(unsigned long size)
  859. {
  860. void *ret;
  861. ret = kmalloc(size, GFP_KERNEL);
  862. if (ret != NULL)
  863. memset(ret, 0, size);
  864. return ret;
  865. }
  866. static void __init sunzilog_alloc_tables(void)
  867. {
  868. sunzilog_port_table =
  869. alloc_one_table(NUM_CHANNELS * sizeof(struct uart_sunzilog_port));
  870. sunzilog_chip_regs =
  871. alloc_one_table(NUM_SUNZILOG * sizeof(struct zilog_layout __iomem *));
  872. if (sunzilog_port_table == NULL || sunzilog_chip_regs == NULL) {
  873. prom_printf("SunZilog: Cannot allocate tables.\n");
  874. prom_halt();
  875. }
  876. }
  877. #ifdef CONFIG_SPARC64
  878. /* We used to attempt to use the address property of the Zilog device node
  879. * but that totally is not necessary on sparc64.
  880. */
  881. static struct zilog_layout __iomem * __init get_zs_sun4u(int chip, int zsnode)
  882. {
  883. void __iomem *mapped_addr;
  884. unsigned int sun4u_ino;
  885. struct sbus_bus *sbus = NULL;
  886. struct sbus_dev *sdev = NULL;
  887. int err;
  888. if (central_bus == NULL) {
  889. for_each_sbus(sbus) {
  890. for_each_sbusdev(sdev, sbus) {
  891. if (sdev->prom_node == zsnode)
  892. goto found;
  893. }
  894. }
  895. }
  896. found:
  897. if (sdev == NULL && central_bus == NULL) {
  898. prom_printf("SunZilog: sdev&&central == NULL for "
  899. "Zilog %d in get_zs_sun4u.\n", chip);
  900. prom_halt();
  901. }
  902. if (central_bus == NULL) {
  903. mapped_addr =
  904. sbus_ioremap(&sdev->resource[0], 0,
  905. PAGE_SIZE,
  906. "Zilog Registers");
  907. } else {
  908. struct linux_prom_registers zsregs[1];
  909. err = prom_getproperty(zsnode, "reg",
  910. (char *) &zsregs[0],
  911. sizeof(zsregs));
  912. if (err == -1) {
  913. prom_printf("SunZilog: Cannot map "
  914. "Zilog %d regs on "
  915. "central bus.\n", chip);
  916. prom_halt();
  917. }
  918. apply_fhc_ranges(central_bus->child,
  919. &zsregs[0], 1);
  920. apply_central_ranges(central_bus, &zsregs[0], 1);
  921. mapped_addr = (void __iomem *)
  922. ((((u64)zsregs[0].which_io)<<32UL) |
  923. ((u64)zsregs[0].phys_addr));
  924. }
  925. if (zilog_irq == -1) {
  926. if (central_bus) {
  927. unsigned long iclr, imap;
  928. iclr = central_bus->child->fhc_regs.uregs
  929. + FHC_UREGS_ICLR;
  930. imap = central_bus->child->fhc_regs.uregs
  931. + FHC_UREGS_IMAP;
  932. zilog_irq = build_irq(12, 0, iclr, imap);
  933. } else {
  934. err = prom_getproperty(zsnode, "interrupts",
  935. (char *) &sun4u_ino,
  936. sizeof(sun4u_ino));
  937. zilog_irq = sbus_build_irq(sbus_root, sun4u_ino);
  938. }
  939. }
  940. return (struct zilog_layout __iomem *) mapped_addr;
  941. }
  942. #else /* CONFIG_SPARC64 */
  943. /*
  944. * XXX The sun4d case is utterly screwed: it tries to re-walk the tree
  945. * (for the 3rd time) in order to find bootbus and cpu. Streamline it.
  946. */
  947. static struct zilog_layout __iomem * __init get_zs_sun4cmd(int chip, int node)
  948. {
  949. struct linux_prom_irqs irq_info[2];
  950. void __iomem *mapped_addr = NULL;
  951. int zsnode, cpunode, bbnode;
  952. struct linux_prom_registers zsreg[4];
  953. struct resource res;
  954. if (sparc_cpu_model == sun4d) {
  955. int walk;
  956. zsnode = 0;
  957. bbnode = 0;
  958. cpunode = 0;
  959. for (walk = prom_getchild(prom_root_node);
  960. (walk = prom_searchsiblings(walk, "cpu-unit")) != 0;
  961. walk = prom_getsibling(walk)) {
  962. bbnode = prom_getchild(walk);
  963. if (bbnode &&
  964. (bbnode = prom_searchsiblings(bbnode, "bootbus"))) {
  965. if ((zsnode = prom_getchild(bbnode)) == node) {
  966. cpunode = walk;
  967. break;
  968. }
  969. }
  970. }
  971. if (!walk) {
  972. prom_printf("SunZilog: Cannot find the %d'th bootbus on sun4d.\n",
  973. (chip / 2));
  974. prom_halt();
  975. }
  976. if (prom_getproperty(zsnode, "reg",
  977. (char *) zsreg, sizeof(zsreg)) == -1) {
  978. prom_printf("SunZilog: Cannot map Zilog %d\n", chip);
  979. prom_halt();
  980. }
  981. /* XXX Looks like an off by one? */
  982. prom_apply_generic_ranges(bbnode, cpunode, zsreg, 1);
  983. res.start = zsreg[0].phys_addr;
  984. res.end = res.start + (8 - 1);
  985. res.flags = zsreg[0].which_io | IORESOURCE_IO;
  986. mapped_addr = sbus_ioremap(&res, 0, 8, "Zilog Serial");
  987. } else {
  988. zsnode = node;
  989. #if 0 /* XXX When was this used? */
  990. if (prom_getintdefault(zsnode, "slave", -1) != chipid) {
  991. zsnode = prom_getsibling(zsnode);
  992. continue;
  993. }
  994. #endif
  995. /*
  996. * "address" is only present on ports that OBP opened
  997. * (from Mitch Bradley's "Hitchhiker's Guide to OBP").
  998. * We do not use it.
  999. */
  1000. if (prom_getproperty(zsnode, "reg",
  1001. (char *) zsreg, sizeof(zsreg)) == -1) {
  1002. prom_printf("SunZilog: Cannot map Zilog %d\n", chip);
  1003. prom_halt();
  1004. }
  1005. if (sparc_cpu_model == sun4m) /* Crude. Pass parent. XXX */
  1006. prom_apply_obio_ranges(zsreg, 1);
  1007. res.start = zsreg[0].phys_addr;
  1008. res.end = res.start + (8 - 1);
  1009. res.flags = zsreg[0].which_io | IORESOURCE_IO;
  1010. mapped_addr = sbus_ioremap(&res, 0, 8, "Zilog Serial");
  1011. }
  1012. if (prom_getproperty(zsnode, "intr",
  1013. (char *) irq_info, sizeof(irq_info))
  1014. % sizeof(struct linux_prom_irqs)) {
  1015. prom_printf("SunZilog: Cannot get IRQ property for Zilog %d.\n",
  1016. chip);
  1017. prom_halt();
  1018. }
  1019. if (zilog_irq == -1) {
  1020. zilog_irq = irq_info[0].pri;
  1021. } else if (zilog_irq != irq_info[0].pri) {
  1022. /* XXX. Dumb. Should handle per-chip IRQ, for add-ons. */
  1023. prom_printf("SunZilog: Inconsistent IRQ layout for Zilog %d.\n",
  1024. chip);
  1025. prom_halt();
  1026. }
  1027. return (struct zilog_layout __iomem *) mapped_addr;
  1028. }
  1029. #endif /* !(CONFIG_SPARC64) */
  1030. /* Get the address of the registers for SunZilog instance CHIP. */
  1031. static struct zilog_layout __iomem * __init get_zs(int chip, int node)
  1032. {
  1033. if (chip < 0 || chip >= NUM_SUNZILOG) {
  1034. prom_printf("SunZilog: Illegal chip number %d in get_zs.\n", chip);
  1035. prom_halt();
  1036. }
  1037. #ifdef CONFIG_SPARC64
  1038. return get_zs_sun4u(chip, node);
  1039. #else
  1040. if (sparc_cpu_model == sun4) {
  1041. struct resource res;
  1042. /* Not probe-able, hard code it. */
  1043. switch (chip) {
  1044. case 0:
  1045. res.start = 0xf1000000;
  1046. break;
  1047. case 1:
  1048. res.start = 0xf0000000;
  1049. break;
  1050. };
  1051. zilog_irq = 12;
  1052. res.end = (res.start + (8 - 1));
  1053. res.flags = IORESOURCE_IO;
  1054. return sbus_ioremap(&res, 0, 8, "SunZilog");
  1055. }
  1056. return get_zs_sun4cmd(chip, node);
  1057. #endif
  1058. }
  1059. #define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
  1060. static void sunzilog_put_char(struct zilog_channel __iomem *channel, unsigned char ch)
  1061. {
  1062. int loops = ZS_PUT_CHAR_MAX_DELAY;
  1063. /* This is a timed polling loop so do not switch the explicit
  1064. * udelay with ZSDELAY as that is a NOP on some platforms. -DaveM
  1065. */
  1066. do {
  1067. unsigned char val = sbus_readb(&channel->control);
  1068. if (val & Tx_BUF_EMP) {
  1069. ZSDELAY();
  1070. break;
  1071. }
  1072. udelay(5);
  1073. } while (--loops);
  1074. sbus_writeb(ch, &channel->data);
  1075. ZSDELAY();
  1076. ZS_WSYNC(channel);
  1077. }
  1078. #ifdef CONFIG_SERIO
  1079. static DEFINE_SPINLOCK(sunzilog_serio_lock);
  1080. static int sunzilog_serio_write(struct serio *serio, unsigned char ch)
  1081. {
  1082. struct uart_sunzilog_port *up = serio->port_data;
  1083. unsigned long flags;
  1084. spin_lock_irqsave(&sunzilog_serio_lock, flags);
  1085. sunzilog_put_char(ZILOG_CHANNEL_FROM_PORT(&up->port), ch);
  1086. spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
  1087. return 0;
  1088. }
  1089. static int sunzilog_serio_open(struct serio *serio)
  1090. {
  1091. struct uart_sunzilog_port *up = serio->port_data;
  1092. unsigned long flags;
  1093. int ret;
  1094. spin_lock_irqsave(&sunzilog_serio_lock, flags);
  1095. if (!up->serio_open) {
  1096. up->serio_open = 1;
  1097. ret = 0;
  1098. } else
  1099. ret = -EBUSY;
  1100. spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
  1101. return ret;
  1102. }
  1103. static void sunzilog_serio_close(struct serio *serio)
  1104. {
  1105. struct uart_sunzilog_port *up = serio->port_data;
  1106. unsigned long flags;
  1107. spin_lock_irqsave(&sunzilog_serio_lock, flags);
  1108. up->serio_open = 0;
  1109. spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
  1110. }
  1111. #endif /* CONFIG_SERIO */
  1112. #ifdef CONFIG_SERIAL_SUNZILOG_CONSOLE
  1113. static void
  1114. sunzilog_console_write(struct console *con, const char *s, unsigned int count)
  1115. {
  1116. struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
  1117. struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  1118. unsigned long flags;
  1119. int i;
  1120. spin_lock_irqsave(&up->port.lock, flags);
  1121. for (i = 0; i < count; i++, s++) {
  1122. sunzilog_put_char(channel, *s);
  1123. if (*s == 10)
  1124. sunzilog_put_char(channel, 13);
  1125. }
  1126. udelay(2);
  1127. spin_unlock_irqrestore(&up->port.lock, flags);
  1128. }
  1129. static int __init sunzilog_console_setup(struct console *con, char *options)
  1130. {
  1131. struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
  1132. unsigned long flags;
  1133. int baud, brg;
  1134. printk(KERN_INFO "Console: ttyS%d (SunZilog zs%d)\n",
  1135. (sunzilog_reg.minor - 64) + con->index, con->index);
  1136. /* Get firmware console settings. */
  1137. sunserial_console_termios(con);
  1138. /* Firmware console speed is limited to 150-->38400 baud so
  1139. * this hackish cflag thing is OK.
  1140. */
  1141. switch (con->cflag & CBAUD) {
  1142. case B150: baud = 150; break;
  1143. case B300: baud = 300; break;
  1144. case B600: baud = 600; break;
  1145. case B1200: baud = 1200; break;
  1146. case B2400: baud = 2400; break;
  1147. case B4800: baud = 4800; break;
  1148. default: case B9600: baud = 9600; break;
  1149. case B19200: baud = 19200; break;
  1150. case B38400: baud = 38400; break;
  1151. };
  1152. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  1153. spin_lock_irqsave(&up->port.lock, flags);
  1154. up->curregs[R15] = BRKIE;
  1155. sunzilog_convert_to_zs(up, con->cflag, 0, brg);
  1156. sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
  1157. __sunzilog_startup(up);
  1158. spin_unlock_irqrestore(&up->port.lock, flags);
  1159. return 0;
  1160. }
  1161. static struct console sunzilog_console = {
  1162. .name = "ttyS",
  1163. .write = sunzilog_console_write,
  1164. .device = uart_console_device,
  1165. .setup = sunzilog_console_setup,
  1166. .flags = CON_PRINTBUFFER,
  1167. .index = -1,
  1168. .data = &sunzilog_reg,
  1169. };
  1170. #define SUNZILOG_CONSOLE (&sunzilog_console)
  1171. static int __init sunzilog_console_init(void)
  1172. {
  1173. int i;
  1174. if (con_is_present())
  1175. return 0;
  1176. for (i = 0; i < NUM_CHANNELS; i++) {
  1177. int this_minor = sunzilog_reg.minor + i;
  1178. if ((this_minor - 64) == (serial_console - 1))
  1179. break;
  1180. }
  1181. if (i == NUM_CHANNELS)
  1182. return 0;
  1183. sunzilog_console.index = i;
  1184. sunzilog_port_table[i].flags |= SUNZILOG_FLAG_IS_CONS;
  1185. register_console(&sunzilog_console);
  1186. return 0;
  1187. }
  1188. #else
  1189. #define SUNZILOG_CONSOLE (NULL)
  1190. #define sunzilog_console_init() do { } while (0)
  1191. #endif
  1192. /*
  1193. * We scan the PROM tree recursively. This is the most reliable way
  1194. * to find Zilog nodes on various platforms. However, we face an extreme
  1195. * shortage of kernel stack, so we must be very careful. To that end,
  1196. * we scan only to a certain depth, and we use a common property buffer
  1197. * in the scan structure.
  1198. */
  1199. #define ZS_PROPSIZE 128
  1200. #define ZS_SCAN_DEPTH 5
  1201. struct zs_probe_scan {
  1202. int depth;
  1203. void (*scanner)(struct zs_probe_scan *t, int node);
  1204. int devices;
  1205. char prop[ZS_PROPSIZE];
  1206. };
  1207. static int __inline__ sunzilog_node_ok(int node, const char *name, int len)
  1208. {
  1209. if (strncmp(name, "zs", len) == 0)
  1210. return 1;
  1211. /* Don't fold this procedure just yet. Compare to su_node_ok(). */
  1212. return 0;
  1213. }
  1214. static void __init sunzilog_scan(struct zs_probe_scan *t, int node)
  1215. {
  1216. int len;
  1217. for (; node != 0; node = prom_getsibling(node)) {
  1218. len = prom_getproperty(node, "name", t->prop, ZS_PROPSIZE);
  1219. if (len <= 1)
  1220. continue; /* Broken PROM node */
  1221. if (sunzilog_node_ok(node, t->prop, len)) {
  1222. (*t->scanner)(t, node);
  1223. } else {
  1224. if (t->depth < ZS_SCAN_DEPTH) {
  1225. t->depth++;
  1226. sunzilog_scan(t, prom_getchild(node));
  1227. --t->depth;
  1228. }
  1229. }
  1230. }
  1231. }
  1232. static void __init sunzilog_prepare(void)
  1233. {
  1234. struct uart_sunzilog_port *up;
  1235. struct zilog_layout __iomem *rp;
  1236. int channel, chip;
  1237. /*
  1238. * Temporary fix.
  1239. */
  1240. for (channel = 0; channel < NUM_CHANNELS; channel++)
  1241. spin_lock_init(&sunzilog_port_table[channel].port.lock);
  1242. sunzilog_irq_chain = up = &sunzilog_port_table[0];
  1243. for (channel = 0; channel < NUM_CHANNELS - 1; channel++)
  1244. up[channel].next = &up[channel + 1];
  1245. up[channel].next = NULL;
  1246. for (chip = 0; chip < NUM_SUNZILOG; chip++) {
  1247. rp = sunzilog_chip_regs[chip];
  1248. up[(chip * 2) + 0].port.membase = (void __iomem *)&rp->channelA;
  1249. up[(chip * 2) + 1].port.membase = (void __iomem *)&rp->channelB;
  1250. /* Channel A */
  1251. up[(chip * 2) + 0].port.iotype = SERIAL_IO_MEM;
  1252. up[(chip * 2) + 0].port.irq = zilog_irq;
  1253. up[(chip * 2) + 0].port.uartclk = ZS_CLOCK;
  1254. up[(chip * 2) + 0].port.fifosize = 1;
  1255. up[(chip * 2) + 0].port.ops = &sunzilog_pops;
  1256. up[(chip * 2) + 0].port.type = PORT_SUNZILOG;
  1257. up[(chip * 2) + 0].port.flags = 0;
  1258. up[(chip * 2) + 0].port.line = (chip * 2) + 0;
  1259. up[(chip * 2) + 0].flags |= SUNZILOG_FLAG_IS_CHANNEL_A;
  1260. /* Channel B */
  1261. up[(chip * 2) + 1].port.iotype = SERIAL_IO_MEM;
  1262. up[(chip * 2) + 1].port.irq = zilog_irq;
  1263. up[(chip * 2) + 1].port.uartclk = ZS_CLOCK;
  1264. up[(chip * 2) + 1].port.fifosize = 1;
  1265. up[(chip * 2) + 1].port.ops = &sunzilog_pops;
  1266. up[(chip * 2) + 1].port.type = PORT_SUNZILOG;
  1267. up[(chip * 2) + 1].port.flags = 0;
  1268. up[(chip * 2) + 1].port.line = (chip * 2) + 1;
  1269. up[(chip * 2) + 1].flags |= 0;
  1270. }
  1271. }
  1272. static void __init sunzilog_init_kbdms(struct uart_sunzilog_port *up, int channel)
  1273. {
  1274. int baud, brg;
  1275. if (channel == KEYBOARD_LINE) {
  1276. up->flags |= SUNZILOG_FLAG_CONS_KEYB;
  1277. up->cflag = B1200 | CS8 | CLOCAL | CREAD;
  1278. baud = 1200;
  1279. } else {
  1280. up->flags |= SUNZILOG_FLAG_CONS_MOUSE;
  1281. up->cflag = B4800 | CS8 | CLOCAL | CREAD;
  1282. baud = 4800;
  1283. }
  1284. printk(KERN_INFO "zs%d at 0x%p (irq = %s) is a SunZilog\n",
  1285. channel, up->port.membase, __irq_itoa(zilog_irq));
  1286. up->curregs[R15] = BRKIE;
  1287. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  1288. sunzilog_convert_to_zs(up, up->cflag, 0, brg);
  1289. sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
  1290. __sunzilog_startup(up);
  1291. }
  1292. #ifdef CONFIG_SERIO
  1293. static void __init sunzilog_register_serio(struct uart_sunzilog_port *up, int channel)
  1294. {
  1295. struct serio *serio;
  1296. up->serio = serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
  1297. if (serio) {
  1298. memset(serio, 0, sizeof(*serio));
  1299. serio->port_data = up;
  1300. serio->id.type = SERIO_RS232;
  1301. if (channel == KEYBOARD_LINE) {
  1302. serio->id.proto = SERIO_SUNKBD;
  1303. strlcpy(serio->name, "zskbd", sizeof(serio->name));
  1304. } else {
  1305. serio->id.proto = SERIO_SUN;
  1306. serio->id.extra = 1;
  1307. strlcpy(serio->name, "zsms", sizeof(serio->name));
  1308. }
  1309. strlcpy(serio->phys,
  1310. (channel == KEYBOARD_LINE ? "zs/serio0" : "zs/serio1"),
  1311. sizeof(serio->phys));
  1312. serio->write = sunzilog_serio_write;
  1313. serio->open = sunzilog_serio_open;
  1314. serio->close = sunzilog_serio_close;
  1315. serio_register_port(serio);
  1316. } else {
  1317. printk(KERN_WARNING "zs%d: not enough memory for serio port\n",
  1318. channel);
  1319. }
  1320. }
  1321. #endif
  1322. static void __init sunzilog_init_hw(void)
  1323. {
  1324. int i;
  1325. for (i = 0; i < NUM_CHANNELS; i++) {
  1326. struct uart_sunzilog_port *up = &sunzilog_port_table[i];
  1327. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  1328. unsigned long flags;
  1329. int baud, brg;
  1330. spin_lock_irqsave(&up->port.lock, flags);
  1331. if (ZS_IS_CHANNEL_A(up)) {
  1332. write_zsreg(channel, R9, FHWRES);
  1333. ZSDELAY_LONG();
  1334. (void) read_zsreg(channel, R0);
  1335. }
  1336. if (i == KEYBOARD_LINE || i == MOUSE_LINE) {
  1337. sunzilog_init_kbdms(up, i);
  1338. up->curregs[R9] |= (NV | MIE);
  1339. write_zsreg(channel, R9, up->curregs[R9]);
  1340. } else {
  1341. /* Normal serial TTY. */
  1342. up->parity_mask = 0xff;
  1343. up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
  1344. up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
  1345. up->curregs[R3] = RxENAB | Rx8;
  1346. up->curregs[R5] = TxENAB | Tx8;
  1347. up->curregs[R9] = NV | MIE;
  1348. up->curregs[R10] = NRZ;
  1349. up->curregs[R11] = TCBR | RCBR;
  1350. baud = 9600;
  1351. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  1352. up->curregs[R12] = (brg & 0xff);
  1353. up->curregs[R13] = (brg >> 8) & 0xff;
  1354. up->curregs[R14] = BRSRC | BRENAB;
  1355. __load_zsregs(channel, up->curregs);
  1356. write_zsreg(channel, R9, up->curregs[R9]);
  1357. }
  1358. spin_unlock_irqrestore(&up->port.lock, flags);
  1359. #ifdef CONFIG_SERIO
  1360. if (i == KEYBOARD_LINE || i == MOUSE_LINE)
  1361. sunzilog_register_serio(up, i);
  1362. #endif
  1363. }
  1364. }
  1365. static struct zilog_layout __iomem * __init get_zs(int chip, int node);
  1366. static void __init sunzilog_scan_probe(struct zs_probe_scan *t, int node)
  1367. {
  1368. sunzilog_chip_regs[t->devices] = get_zs(t->devices, node);
  1369. t->devices++;
  1370. }
  1371. static int __init sunzilog_ports_init(void)
  1372. {
  1373. struct zs_probe_scan scan;
  1374. int ret;
  1375. int uart_count;
  1376. int i;
  1377. printk(KERN_DEBUG "SunZilog: %d chips.\n", NUM_SUNZILOG);
  1378. scan.scanner = sunzilog_scan_probe;
  1379. scan.depth = 0;
  1380. scan.devices = 0;
  1381. sunzilog_scan(&scan, prom_getchild(prom_root_node));
  1382. sunzilog_prepare();
  1383. if (request_irq(zilog_irq, sunzilog_interrupt, SA_SHIRQ,
  1384. "SunZilog", sunzilog_irq_chain)) {
  1385. prom_printf("SunZilog: Unable to register zs interrupt handler.\n");
  1386. prom_halt();
  1387. }
  1388. sunzilog_init_hw();
  1389. /* We can only init this once we have probed the Zilogs
  1390. * in the system. Do not count channels assigned to keyboards
  1391. * or mice when we are deciding how many ports to register.
  1392. */
  1393. uart_count = 0;
  1394. for (i = 0; i < NUM_CHANNELS; i++) {
  1395. struct uart_sunzilog_port *up = &sunzilog_port_table[i];
  1396. if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up))
  1397. continue;
  1398. uart_count++;
  1399. }
  1400. sunzilog_reg.nr = uart_count;
  1401. sunzilog_reg.cons = SUNZILOG_CONSOLE;
  1402. sunzilog_reg.minor = sunserial_current_minor;
  1403. sunserial_current_minor += uart_count;
  1404. ret = uart_register_driver(&sunzilog_reg);
  1405. if (ret == 0) {
  1406. sunzilog_console_init();
  1407. for (i = 0; i < NUM_CHANNELS; i++) {
  1408. struct uart_sunzilog_port *up = &sunzilog_port_table[i];
  1409. if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up))
  1410. continue;
  1411. if (uart_add_one_port(&sunzilog_reg, &up->port)) {
  1412. printk(KERN_ERR
  1413. "SunZilog: failed to add port zs%d\n", i);
  1414. }
  1415. }
  1416. }
  1417. return ret;
  1418. }
  1419. static void __init sunzilog_scan_count(struct zs_probe_scan *t, int node)
  1420. {
  1421. t->devices++;
  1422. }
  1423. static int __init sunzilog_ports_count(void)
  1424. {
  1425. struct zs_probe_scan scan;
  1426. /* Sun4 Zilog setup is hard coded, no probing to do. */
  1427. if (sparc_cpu_model == sun4)
  1428. return 2;
  1429. scan.scanner = sunzilog_scan_count;
  1430. scan.depth = 0;
  1431. scan.devices = 0;
  1432. sunzilog_scan(&scan, prom_getchild(prom_root_node));
  1433. return scan.devices;
  1434. }
  1435. static int __init sunzilog_init(void)
  1436. {
  1437. NUM_SUNZILOG = sunzilog_ports_count();
  1438. if (NUM_SUNZILOG == 0)
  1439. return -ENODEV;
  1440. sunzilog_alloc_tables();
  1441. sunzilog_ports_init();
  1442. return 0;
  1443. }
  1444. static void __exit sunzilog_exit(void)
  1445. {
  1446. int i;
  1447. for (i = 0; i < NUM_CHANNELS; i++) {
  1448. struct uart_sunzilog_port *up = &sunzilog_port_table[i];
  1449. if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up)) {
  1450. #ifdef CONFIG_SERIO
  1451. if (up->serio) {
  1452. serio_unregister_port(up->serio);
  1453. up->serio = NULL;
  1454. }
  1455. #endif
  1456. } else
  1457. uart_remove_one_port(&sunzilog_reg, &up->port);
  1458. }
  1459. uart_unregister_driver(&sunzilog_reg);
  1460. }
  1461. module_init(sunzilog_init);
  1462. module_exit(sunzilog_exit);
  1463. MODULE_AUTHOR("David S. Miller");
  1464. MODULE_DESCRIPTION("Sun Zilog serial port driver");
  1465. MODULE_LICENSE("GPL");