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, flag;
  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. /* A real serial line, record the character and status. */
  302. flag = TTY_NORMAL;
  303. up->port.icount.rx++;
  304. if (r1 & (BRK_ABRT | PAR_ERR | Rx_OVR | CRC_ERR)) {
  305. if (r1 & BRK_ABRT) {
  306. r1 &= ~(PAR_ERR | CRC_ERR);
  307. up->port.icount.brk++;
  308. if (uart_handle_break(&up->port))
  309. continue;
  310. }
  311. else if (r1 & PAR_ERR)
  312. up->port.icount.parity++;
  313. else if (r1 & CRC_ERR)
  314. up->port.icount.frame++;
  315. if (r1 & Rx_OVR)
  316. up->port.icount.overrun++;
  317. r1 &= up->port.read_status_mask;
  318. if (r1 & BRK_ABRT)
  319. flag = TTY_BREAK;
  320. else if (r1 & PAR_ERR)
  321. flag = TTY_PARITY;
  322. else if (r1 & CRC_ERR)
  323. flag = TTY_FRAME;
  324. }
  325. if (uart_handle_sysrq_char(&up->port, ch, regs))
  326. continue;
  327. if (up->port.ignore_status_mask == 0xff ||
  328. (r1 & up->port.ignore_status_mask) == 0) {
  329. tty_insert_flip_char(tty, ch, flag);
  330. }
  331. if (r1 & Rx_OVR)
  332. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  333. }
  334. return tty;
  335. }
  336. static void sunzilog_status_handle(struct uart_sunzilog_port *up,
  337. struct zilog_channel __iomem *channel,
  338. struct pt_regs *regs)
  339. {
  340. unsigned char status;
  341. status = sbus_readb(&channel->control);
  342. ZSDELAY();
  343. sbus_writeb(RES_EXT_INT, &channel->control);
  344. ZSDELAY();
  345. ZS_WSYNC(channel);
  346. if (status & BRK_ABRT) {
  347. if (ZS_IS_MOUSE(up))
  348. sunzilog_kbdms_receive_chars(up, 0, 1, regs);
  349. if (ZS_IS_CONS(up)) {
  350. /* Wait for BREAK to deassert to avoid potentially
  351. * confusing the PROM.
  352. */
  353. while (1) {
  354. status = sbus_readb(&channel->control);
  355. ZSDELAY();
  356. if (!(status & BRK_ABRT))
  357. break;
  358. }
  359. sun_do_break();
  360. return;
  361. }
  362. }
  363. if (ZS_WANTS_MODEM_STATUS(up)) {
  364. if (status & SYNC)
  365. up->port.icount.dsr++;
  366. /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
  367. * But it does not tell us which bit has changed, we have to keep
  368. * track of this ourselves.
  369. */
  370. if ((status ^ up->prev_status) ^ DCD)
  371. uart_handle_dcd_change(&up->port,
  372. (status & DCD));
  373. if ((status ^ up->prev_status) ^ CTS)
  374. uart_handle_cts_change(&up->port,
  375. (status & CTS));
  376. wake_up_interruptible(&up->port.info->delta_msr_wait);
  377. }
  378. up->prev_status = status;
  379. }
  380. static void sunzilog_transmit_chars(struct uart_sunzilog_port *up,
  381. struct zilog_channel __iomem *channel)
  382. {
  383. struct circ_buf *xmit;
  384. if (ZS_IS_CONS(up)) {
  385. unsigned char status = sbus_readb(&channel->control);
  386. ZSDELAY();
  387. /* TX still busy? Just wait for the next TX done interrupt.
  388. *
  389. * It can occur because of how we do serial console writes. It would
  390. * be nice to transmit console writes just like we normally would for
  391. * a TTY line. (ie. buffered and TX interrupt driven). That is not
  392. * easy because console writes cannot sleep. One solution might be
  393. * to poll on enough port->xmit space becomming free. -DaveM
  394. */
  395. if (!(status & Tx_BUF_EMP))
  396. return;
  397. }
  398. up->flags &= ~SUNZILOG_FLAG_TX_ACTIVE;
  399. if (ZS_REGS_HELD(up)) {
  400. __load_zsregs(channel, up->curregs);
  401. up->flags &= ~SUNZILOG_FLAG_REGS_HELD;
  402. }
  403. if (ZS_TX_STOPPED(up)) {
  404. up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
  405. goto ack_tx_int;
  406. }
  407. if (up->port.x_char) {
  408. up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
  409. sbus_writeb(up->port.x_char, &channel->data);
  410. ZSDELAY();
  411. ZS_WSYNC(channel);
  412. up->port.icount.tx++;
  413. up->port.x_char = 0;
  414. return;
  415. }
  416. if (up->port.info == NULL)
  417. goto ack_tx_int;
  418. xmit = &up->port.info->xmit;
  419. if (uart_circ_empty(xmit))
  420. goto ack_tx_int;
  421. if (uart_tx_stopped(&up->port))
  422. goto ack_tx_int;
  423. up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
  424. sbus_writeb(xmit->buf[xmit->tail], &channel->data);
  425. ZSDELAY();
  426. ZS_WSYNC(channel);
  427. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  428. up->port.icount.tx++;
  429. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  430. uart_write_wakeup(&up->port);
  431. return;
  432. ack_tx_int:
  433. sbus_writeb(RES_Tx_P, &channel->control);
  434. ZSDELAY();
  435. ZS_WSYNC(channel);
  436. }
  437. static irqreturn_t sunzilog_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  438. {
  439. struct uart_sunzilog_port *up = dev_id;
  440. while (up) {
  441. struct zilog_channel __iomem *channel
  442. = ZILOG_CHANNEL_FROM_PORT(&up->port);
  443. struct tty_struct *tty;
  444. unsigned char r3;
  445. spin_lock(&up->port.lock);
  446. r3 = read_zsreg(channel, R3);
  447. /* Channel A */
  448. tty = NULL;
  449. if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
  450. sbus_writeb(RES_H_IUS, &channel->control);
  451. ZSDELAY();
  452. ZS_WSYNC(channel);
  453. if (r3 & CHARxIP)
  454. tty = sunzilog_receive_chars(up, channel, regs);
  455. if (r3 & CHAEXT)
  456. sunzilog_status_handle(up, channel, regs);
  457. if (r3 & CHATxIP)
  458. sunzilog_transmit_chars(up, channel);
  459. }
  460. spin_unlock(&up->port.lock);
  461. if (tty)
  462. tty_flip_buffer_push(tty);
  463. /* Channel B */
  464. up = up->next;
  465. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  466. spin_lock(&up->port.lock);
  467. tty = NULL;
  468. if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
  469. sbus_writeb(RES_H_IUS, &channel->control);
  470. ZSDELAY();
  471. ZS_WSYNC(channel);
  472. if (r3 & CHBRxIP)
  473. tty = sunzilog_receive_chars(up, channel, regs);
  474. if (r3 & CHBEXT)
  475. sunzilog_status_handle(up, channel, regs);
  476. if (r3 & CHBTxIP)
  477. sunzilog_transmit_chars(up, channel);
  478. }
  479. spin_unlock(&up->port.lock);
  480. if (tty)
  481. tty_flip_buffer_push(tty);
  482. up = up->next;
  483. }
  484. return IRQ_HANDLED;
  485. }
  486. /* A convenient way to quickly get R0 status. The caller must _not_ hold the
  487. * port lock, it is acquired here.
  488. */
  489. static __inline__ unsigned char sunzilog_read_channel_status(struct uart_port *port)
  490. {
  491. struct zilog_channel __iomem *channel;
  492. unsigned char status;
  493. channel = ZILOG_CHANNEL_FROM_PORT(port);
  494. status = sbus_readb(&channel->control);
  495. ZSDELAY();
  496. return status;
  497. }
  498. /* The port lock is not held. */
  499. static unsigned int sunzilog_tx_empty(struct uart_port *port)
  500. {
  501. unsigned long flags;
  502. unsigned char status;
  503. unsigned int ret;
  504. spin_lock_irqsave(&port->lock, flags);
  505. status = sunzilog_read_channel_status(port);
  506. spin_unlock_irqrestore(&port->lock, flags);
  507. if (status & Tx_BUF_EMP)
  508. ret = TIOCSER_TEMT;
  509. else
  510. ret = 0;
  511. return ret;
  512. }
  513. /* The port lock is held and interrupts are disabled. */
  514. static unsigned int sunzilog_get_mctrl(struct uart_port *port)
  515. {
  516. unsigned char status;
  517. unsigned int ret;
  518. status = sunzilog_read_channel_status(port);
  519. ret = 0;
  520. if (status & DCD)
  521. ret |= TIOCM_CAR;
  522. if (status & SYNC)
  523. ret |= TIOCM_DSR;
  524. if (status & CTS)
  525. ret |= TIOCM_CTS;
  526. return ret;
  527. }
  528. /* The port lock is held and interrupts are disabled. */
  529. static void sunzilog_set_mctrl(struct uart_port *port, unsigned int mctrl)
  530. {
  531. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  532. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  533. unsigned char set_bits, clear_bits;
  534. set_bits = clear_bits = 0;
  535. if (mctrl & TIOCM_RTS)
  536. set_bits |= RTS;
  537. else
  538. clear_bits |= RTS;
  539. if (mctrl & TIOCM_DTR)
  540. set_bits |= DTR;
  541. else
  542. clear_bits |= DTR;
  543. /* NOTE: Not subject to 'transmitter active' rule. */
  544. up->curregs[R5] |= set_bits;
  545. up->curregs[R5] &= ~clear_bits;
  546. write_zsreg(channel, R5, up->curregs[R5]);
  547. }
  548. /* The port lock is held and interrupts are disabled. */
  549. static void sunzilog_stop_tx(struct uart_port *port)
  550. {
  551. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  552. up->flags |= SUNZILOG_FLAG_TX_STOPPED;
  553. }
  554. /* The port lock is held and interrupts are disabled. */
  555. static void sunzilog_start_tx(struct uart_port *port)
  556. {
  557. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  558. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  559. unsigned char status;
  560. up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
  561. up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
  562. status = sbus_readb(&channel->control);
  563. ZSDELAY();
  564. /* TX busy? Just wait for the TX done interrupt. */
  565. if (!(status & Tx_BUF_EMP))
  566. return;
  567. /* Send the first character to jump-start the TX done
  568. * IRQ sending engine.
  569. */
  570. if (port->x_char) {
  571. sbus_writeb(port->x_char, &channel->data);
  572. ZSDELAY();
  573. ZS_WSYNC(channel);
  574. port->icount.tx++;
  575. port->x_char = 0;
  576. } else {
  577. struct circ_buf *xmit = &port->info->xmit;
  578. sbus_writeb(xmit->buf[xmit->tail], &channel->data);
  579. ZSDELAY();
  580. ZS_WSYNC(channel);
  581. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  582. port->icount.tx++;
  583. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  584. uart_write_wakeup(&up->port);
  585. }
  586. }
  587. /* The port lock is held. */
  588. static void sunzilog_stop_rx(struct uart_port *port)
  589. {
  590. struct uart_sunzilog_port *up = UART_ZILOG(port);
  591. struct zilog_channel __iomem *channel;
  592. if (ZS_IS_CONS(up))
  593. return;
  594. channel = ZILOG_CHANNEL_FROM_PORT(port);
  595. /* Disable all RX interrupts. */
  596. up->curregs[R1] &= ~RxINT_MASK;
  597. sunzilog_maybe_update_regs(up, channel);
  598. }
  599. /* The port lock is held. */
  600. static void sunzilog_enable_ms(struct uart_port *port)
  601. {
  602. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  603. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  604. unsigned char new_reg;
  605. new_reg = up->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
  606. if (new_reg != up->curregs[R15]) {
  607. up->curregs[R15] = new_reg;
  608. /* NOTE: Not subject to 'transmitter active' rule. */
  609. write_zsreg(channel, R15, up->curregs[R15]);
  610. }
  611. }
  612. /* The port lock is not held. */
  613. static void sunzilog_break_ctl(struct uart_port *port, int break_state)
  614. {
  615. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  616. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  617. unsigned char set_bits, clear_bits, new_reg;
  618. unsigned long flags;
  619. set_bits = clear_bits = 0;
  620. if (break_state)
  621. set_bits |= SND_BRK;
  622. else
  623. clear_bits |= SND_BRK;
  624. spin_lock_irqsave(&port->lock, flags);
  625. new_reg = (up->curregs[R5] | set_bits) & ~clear_bits;
  626. if (new_reg != up->curregs[R5]) {
  627. up->curregs[R5] = new_reg;
  628. /* NOTE: Not subject to 'transmitter active' rule. */
  629. write_zsreg(channel, R5, up->curregs[R5]);
  630. }
  631. spin_unlock_irqrestore(&port->lock, flags);
  632. }
  633. static void __sunzilog_startup(struct uart_sunzilog_port *up)
  634. {
  635. struct zilog_channel __iomem *channel;
  636. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  637. up->prev_status = sbus_readb(&channel->control);
  638. /* Enable receiver and transmitter. */
  639. up->curregs[R3] |= RxENAB;
  640. up->curregs[R5] |= TxENAB;
  641. up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
  642. sunzilog_maybe_update_regs(up, channel);
  643. }
  644. static int sunzilog_startup(struct uart_port *port)
  645. {
  646. struct uart_sunzilog_port *up = UART_ZILOG(port);
  647. unsigned long flags;
  648. if (ZS_IS_CONS(up))
  649. return 0;
  650. spin_lock_irqsave(&port->lock, flags);
  651. __sunzilog_startup(up);
  652. spin_unlock_irqrestore(&port->lock, flags);
  653. return 0;
  654. }
  655. /*
  656. * The test for ZS_IS_CONS is explained by the following e-mail:
  657. *****
  658. * From: Russell King <rmk@arm.linux.org.uk>
  659. * Date: Sun, 8 Dec 2002 10:18:38 +0000
  660. *
  661. * On Sun, Dec 08, 2002 at 02:43:36AM -0500, Pete Zaitcev wrote:
  662. * > I boot my 2.5 boxes using "console=ttyS0,9600" argument,
  663. * > and I noticed that something is not right with reference
  664. * > counting in this case. It seems that when the console
  665. * > is open by kernel initially, this is not accounted
  666. * > as an open, and uart_startup is not called.
  667. *
  668. * That is correct. We are unable to call uart_startup when the serial
  669. * console is initialised because it may need to allocate memory (as
  670. * request_irq does) and the memory allocators may not have been
  671. * initialised.
  672. *
  673. * 1. initialise the port into a state where it can send characters in the
  674. * console write method.
  675. *
  676. * 2. don't do the actual hardware shutdown in your shutdown() method (but
  677. * do the normal software shutdown - ie, free irqs etc)
  678. *****
  679. */
  680. static void sunzilog_shutdown(struct uart_port *port)
  681. {
  682. struct uart_sunzilog_port *up = UART_ZILOG(port);
  683. struct zilog_channel __iomem *channel;
  684. unsigned long flags;
  685. if (ZS_IS_CONS(up))
  686. return;
  687. spin_lock_irqsave(&port->lock, flags);
  688. channel = ZILOG_CHANNEL_FROM_PORT(port);
  689. /* Disable receiver and transmitter. */
  690. up->curregs[R3] &= ~RxENAB;
  691. up->curregs[R5] &= ~TxENAB;
  692. /* Disable all interrupts and BRK assertion. */
  693. up->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
  694. up->curregs[R5] &= ~SND_BRK;
  695. sunzilog_maybe_update_regs(up, channel);
  696. spin_unlock_irqrestore(&port->lock, flags);
  697. }
  698. /* Shared by TTY driver and serial console setup. The port lock is held
  699. * and local interrupts are disabled.
  700. */
  701. static void
  702. sunzilog_convert_to_zs(struct uart_sunzilog_port *up, unsigned int cflag,
  703. unsigned int iflag, int brg)
  704. {
  705. up->curregs[R10] = NRZ;
  706. up->curregs[R11] = TCBR | RCBR;
  707. /* Program BAUD and clock source. */
  708. up->curregs[R4] &= ~XCLK_MASK;
  709. up->curregs[R4] |= X16CLK;
  710. up->curregs[R12] = brg & 0xff;
  711. up->curregs[R13] = (brg >> 8) & 0xff;
  712. up->curregs[R14] = BRSRC | BRENAB;
  713. /* Character size, stop bits, and parity. */
  714. up->curregs[3] &= ~RxN_MASK;
  715. up->curregs[5] &= ~TxN_MASK;
  716. switch (cflag & CSIZE) {
  717. case CS5:
  718. up->curregs[3] |= Rx5;
  719. up->curregs[5] |= Tx5;
  720. up->parity_mask = 0x1f;
  721. break;
  722. case CS6:
  723. up->curregs[3] |= Rx6;
  724. up->curregs[5] |= Tx6;
  725. up->parity_mask = 0x3f;
  726. break;
  727. case CS7:
  728. up->curregs[3] |= Rx7;
  729. up->curregs[5] |= Tx7;
  730. up->parity_mask = 0x7f;
  731. break;
  732. case CS8:
  733. default:
  734. up->curregs[3] |= Rx8;
  735. up->curregs[5] |= Tx8;
  736. up->parity_mask = 0xff;
  737. break;
  738. };
  739. up->curregs[4] &= ~0x0c;
  740. if (cflag & CSTOPB)
  741. up->curregs[4] |= SB2;
  742. else
  743. up->curregs[4] |= SB1;
  744. if (cflag & PARENB)
  745. up->curregs[4] |= PAR_ENAB;
  746. else
  747. up->curregs[4] &= ~PAR_ENAB;
  748. if (!(cflag & PARODD))
  749. up->curregs[4] |= PAR_EVEN;
  750. else
  751. up->curregs[4] &= ~PAR_EVEN;
  752. up->port.read_status_mask = Rx_OVR;
  753. if (iflag & INPCK)
  754. up->port.read_status_mask |= CRC_ERR | PAR_ERR;
  755. if (iflag & (BRKINT | PARMRK))
  756. up->port.read_status_mask |= BRK_ABRT;
  757. up->port.ignore_status_mask = 0;
  758. if (iflag & IGNPAR)
  759. up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
  760. if (iflag & IGNBRK) {
  761. up->port.ignore_status_mask |= BRK_ABRT;
  762. if (iflag & IGNPAR)
  763. up->port.ignore_status_mask |= Rx_OVR;
  764. }
  765. if ((cflag & CREAD) == 0)
  766. up->port.ignore_status_mask = 0xff;
  767. }
  768. /* The port lock is not held. */
  769. static void
  770. sunzilog_set_termios(struct uart_port *port, struct termios *termios,
  771. struct termios *old)
  772. {
  773. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  774. unsigned long flags;
  775. int baud, brg;
  776. baud = uart_get_baud_rate(port, termios, old, 1200, 76800);
  777. spin_lock_irqsave(&up->port.lock, flags);
  778. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  779. sunzilog_convert_to_zs(up, termios->c_cflag, termios->c_iflag, brg);
  780. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  781. up->flags |= SUNZILOG_FLAG_MODEM_STATUS;
  782. else
  783. up->flags &= ~SUNZILOG_FLAG_MODEM_STATUS;
  784. up->cflag = termios->c_cflag;
  785. sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));
  786. uart_update_timeout(port, termios->c_cflag, baud);
  787. spin_unlock_irqrestore(&up->port.lock, flags);
  788. }
  789. static const char *sunzilog_type(struct uart_port *port)
  790. {
  791. return "SunZilog";
  792. }
  793. /* We do not request/release mappings of the registers here, this
  794. * happens at early serial probe time.
  795. */
  796. static void sunzilog_release_port(struct uart_port *port)
  797. {
  798. }
  799. static int sunzilog_request_port(struct uart_port *port)
  800. {
  801. return 0;
  802. }
  803. /* These do not need to do anything interesting either. */
  804. static void sunzilog_config_port(struct uart_port *port, int flags)
  805. {
  806. }
  807. /* We do not support letting the user mess with the divisor, IRQ, etc. */
  808. static int sunzilog_verify_port(struct uart_port *port, struct serial_struct *ser)
  809. {
  810. return -EINVAL;
  811. }
  812. static struct uart_ops sunzilog_pops = {
  813. .tx_empty = sunzilog_tx_empty,
  814. .set_mctrl = sunzilog_set_mctrl,
  815. .get_mctrl = sunzilog_get_mctrl,
  816. .stop_tx = sunzilog_stop_tx,
  817. .start_tx = sunzilog_start_tx,
  818. .stop_rx = sunzilog_stop_rx,
  819. .enable_ms = sunzilog_enable_ms,
  820. .break_ctl = sunzilog_break_ctl,
  821. .startup = sunzilog_startup,
  822. .shutdown = sunzilog_shutdown,
  823. .set_termios = sunzilog_set_termios,
  824. .type = sunzilog_type,
  825. .release_port = sunzilog_release_port,
  826. .request_port = sunzilog_request_port,
  827. .config_port = sunzilog_config_port,
  828. .verify_port = sunzilog_verify_port,
  829. };
  830. static struct uart_sunzilog_port *sunzilog_port_table;
  831. static struct zilog_layout __iomem **sunzilog_chip_regs;
  832. static struct uart_sunzilog_port *sunzilog_irq_chain;
  833. static int zilog_irq = -1;
  834. static struct uart_driver sunzilog_reg = {
  835. .owner = THIS_MODULE,
  836. .driver_name = "ttyS",
  837. .devfs_name = "tts/",
  838. .dev_name = "ttyS",
  839. .major = TTY_MAJOR,
  840. };
  841. static void * __init alloc_one_table(unsigned long size)
  842. {
  843. void *ret;
  844. ret = kmalloc(size, GFP_KERNEL);
  845. if (ret != NULL)
  846. memset(ret, 0, size);
  847. return ret;
  848. }
  849. static void __init sunzilog_alloc_tables(void)
  850. {
  851. sunzilog_port_table =
  852. alloc_one_table(NUM_CHANNELS * sizeof(struct uart_sunzilog_port));
  853. sunzilog_chip_regs =
  854. alloc_one_table(NUM_SUNZILOG * sizeof(struct zilog_layout __iomem *));
  855. if (sunzilog_port_table == NULL || sunzilog_chip_regs == NULL) {
  856. prom_printf("SunZilog: Cannot allocate tables.\n");
  857. prom_halt();
  858. }
  859. }
  860. #ifdef CONFIG_SPARC64
  861. /* We used to attempt to use the address property of the Zilog device node
  862. * but that totally is not necessary on sparc64.
  863. */
  864. static struct zilog_layout __iomem * __init get_zs_sun4u(int chip, int zsnode)
  865. {
  866. void __iomem *mapped_addr;
  867. unsigned int sun4u_ino;
  868. struct sbus_bus *sbus = NULL;
  869. struct sbus_dev *sdev = NULL;
  870. int err;
  871. if (central_bus == NULL) {
  872. for_each_sbus(sbus) {
  873. for_each_sbusdev(sdev, sbus) {
  874. if (sdev->prom_node == zsnode)
  875. goto found;
  876. }
  877. }
  878. }
  879. found:
  880. if (sdev == NULL && central_bus == NULL) {
  881. prom_printf("SunZilog: sdev&&central == NULL for "
  882. "Zilog %d in get_zs_sun4u.\n", chip);
  883. prom_halt();
  884. }
  885. if (central_bus == NULL) {
  886. mapped_addr =
  887. sbus_ioremap(&sdev->resource[0], 0,
  888. PAGE_SIZE,
  889. "Zilog Registers");
  890. } else {
  891. struct linux_prom_registers zsregs[1];
  892. err = prom_getproperty(zsnode, "reg",
  893. (char *) &zsregs[0],
  894. sizeof(zsregs));
  895. if (err == -1) {
  896. prom_printf("SunZilog: Cannot map "
  897. "Zilog %d regs on "
  898. "central bus.\n", chip);
  899. prom_halt();
  900. }
  901. apply_fhc_ranges(central_bus->child,
  902. &zsregs[0], 1);
  903. apply_central_ranges(central_bus, &zsregs[0], 1);
  904. mapped_addr = (void __iomem *)
  905. ((((u64)zsregs[0].which_io)<<32UL) |
  906. ((u64)zsregs[0].phys_addr));
  907. }
  908. if (zilog_irq == -1) {
  909. if (central_bus) {
  910. unsigned long iclr, imap;
  911. iclr = central_bus->child->fhc_regs.uregs
  912. + FHC_UREGS_ICLR;
  913. imap = central_bus->child->fhc_regs.uregs
  914. + FHC_UREGS_IMAP;
  915. zilog_irq = build_irq(12, 0, iclr, imap);
  916. } else {
  917. err = prom_getproperty(zsnode, "interrupts",
  918. (char *) &sun4u_ino,
  919. sizeof(sun4u_ino));
  920. zilog_irq = sbus_build_irq(sbus_root, sun4u_ino);
  921. }
  922. }
  923. return (struct zilog_layout __iomem *) mapped_addr;
  924. }
  925. #else /* CONFIG_SPARC64 */
  926. /*
  927. * XXX The sun4d case is utterly screwed: it tries to re-walk the tree
  928. * (for the 3rd time) in order to find bootbus and cpu. Streamline it.
  929. */
  930. static struct zilog_layout __iomem * __init get_zs_sun4cmd(int chip, int node)
  931. {
  932. struct linux_prom_irqs irq_info[2];
  933. void __iomem *mapped_addr = NULL;
  934. int zsnode, cpunode, bbnode;
  935. struct linux_prom_registers zsreg[4];
  936. struct resource res;
  937. if (sparc_cpu_model == sun4d) {
  938. int walk;
  939. zsnode = 0;
  940. bbnode = 0;
  941. cpunode = 0;
  942. for (walk = prom_getchild(prom_root_node);
  943. (walk = prom_searchsiblings(walk, "cpu-unit")) != 0;
  944. walk = prom_getsibling(walk)) {
  945. bbnode = prom_getchild(walk);
  946. if (bbnode &&
  947. (bbnode = prom_searchsiblings(bbnode, "bootbus"))) {
  948. if ((zsnode = prom_getchild(bbnode)) == node) {
  949. cpunode = walk;
  950. break;
  951. }
  952. }
  953. }
  954. if (!walk) {
  955. prom_printf("SunZilog: Cannot find the %d'th bootbus on sun4d.\n",
  956. (chip / 2));
  957. prom_halt();
  958. }
  959. if (prom_getproperty(zsnode, "reg",
  960. (char *) zsreg, sizeof(zsreg)) == -1) {
  961. prom_printf("SunZilog: Cannot map Zilog %d\n", chip);
  962. prom_halt();
  963. }
  964. /* XXX Looks like an off by one? */
  965. prom_apply_generic_ranges(bbnode, cpunode, zsreg, 1);
  966. res.start = zsreg[0].phys_addr;
  967. res.end = res.start + (8 - 1);
  968. res.flags = zsreg[0].which_io | IORESOURCE_IO;
  969. mapped_addr = sbus_ioremap(&res, 0, 8, "Zilog Serial");
  970. } else {
  971. zsnode = node;
  972. #if 0 /* XXX When was this used? */
  973. if (prom_getintdefault(zsnode, "slave", -1) != chipid) {
  974. zsnode = prom_getsibling(zsnode);
  975. continue;
  976. }
  977. #endif
  978. /*
  979. * "address" is only present on ports that OBP opened
  980. * (from Mitch Bradley's "Hitchhiker's Guide to OBP").
  981. * We do not use it.
  982. */
  983. if (prom_getproperty(zsnode, "reg",
  984. (char *) zsreg, sizeof(zsreg)) == -1) {
  985. prom_printf("SunZilog: Cannot map Zilog %d\n", chip);
  986. prom_halt();
  987. }
  988. if (sparc_cpu_model == sun4m) /* Crude. Pass parent. XXX */
  989. prom_apply_obio_ranges(zsreg, 1);
  990. res.start = zsreg[0].phys_addr;
  991. res.end = res.start + (8 - 1);
  992. res.flags = zsreg[0].which_io | IORESOURCE_IO;
  993. mapped_addr = sbus_ioremap(&res, 0, 8, "Zilog Serial");
  994. }
  995. if (prom_getproperty(zsnode, "intr",
  996. (char *) irq_info, sizeof(irq_info))
  997. % sizeof(struct linux_prom_irqs)) {
  998. prom_printf("SunZilog: Cannot get IRQ property for Zilog %d.\n",
  999. chip);
  1000. prom_halt();
  1001. }
  1002. if (zilog_irq == -1) {
  1003. zilog_irq = irq_info[0].pri;
  1004. } else if (zilog_irq != irq_info[0].pri) {
  1005. /* XXX. Dumb. Should handle per-chip IRQ, for add-ons. */
  1006. prom_printf("SunZilog: Inconsistent IRQ layout for Zilog %d.\n",
  1007. chip);
  1008. prom_halt();
  1009. }
  1010. return (struct zilog_layout __iomem *) mapped_addr;
  1011. }
  1012. #endif /* !(CONFIG_SPARC64) */
  1013. /* Get the address of the registers for SunZilog instance CHIP. */
  1014. static struct zilog_layout __iomem * __init get_zs(int chip, int node)
  1015. {
  1016. if (chip < 0 || chip >= NUM_SUNZILOG) {
  1017. prom_printf("SunZilog: Illegal chip number %d in get_zs.\n", chip);
  1018. prom_halt();
  1019. }
  1020. #ifdef CONFIG_SPARC64
  1021. return get_zs_sun4u(chip, node);
  1022. #else
  1023. if (sparc_cpu_model == sun4) {
  1024. struct resource res;
  1025. /* Not probe-able, hard code it. */
  1026. switch (chip) {
  1027. case 0:
  1028. res.start = 0xf1000000;
  1029. break;
  1030. case 1:
  1031. res.start = 0xf0000000;
  1032. break;
  1033. };
  1034. zilog_irq = 12;
  1035. res.end = (res.start + (8 - 1));
  1036. res.flags = IORESOURCE_IO;
  1037. return sbus_ioremap(&res, 0, 8, "SunZilog");
  1038. }
  1039. return get_zs_sun4cmd(chip, node);
  1040. #endif
  1041. }
  1042. #define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
  1043. static void sunzilog_putchar(struct uart_port *port, int ch)
  1044. {
  1045. struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
  1046. int loops = ZS_PUT_CHAR_MAX_DELAY;
  1047. /* This is a timed polling loop so do not switch the explicit
  1048. * udelay with ZSDELAY as that is a NOP on some platforms. -DaveM
  1049. */
  1050. do {
  1051. unsigned char val = sbus_readb(&channel->control);
  1052. if (val & Tx_BUF_EMP) {
  1053. ZSDELAY();
  1054. break;
  1055. }
  1056. udelay(5);
  1057. } while (--loops);
  1058. sbus_writeb(ch, &channel->data);
  1059. ZSDELAY();
  1060. ZS_WSYNC(channel);
  1061. }
  1062. #ifdef CONFIG_SERIO
  1063. static DEFINE_SPINLOCK(sunzilog_serio_lock);
  1064. static int sunzilog_serio_write(struct serio *serio, unsigned char ch)
  1065. {
  1066. struct uart_sunzilog_port *up = serio->port_data;
  1067. unsigned long flags;
  1068. spin_lock_irqsave(&sunzilog_serio_lock, flags);
  1069. sunzilog_putchar(&up->port, ch);
  1070. spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
  1071. return 0;
  1072. }
  1073. static int sunzilog_serio_open(struct serio *serio)
  1074. {
  1075. struct uart_sunzilog_port *up = serio->port_data;
  1076. unsigned long flags;
  1077. int ret;
  1078. spin_lock_irqsave(&sunzilog_serio_lock, flags);
  1079. if (!up->serio_open) {
  1080. up->serio_open = 1;
  1081. ret = 0;
  1082. } else
  1083. ret = -EBUSY;
  1084. spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
  1085. return ret;
  1086. }
  1087. static void sunzilog_serio_close(struct serio *serio)
  1088. {
  1089. struct uart_sunzilog_port *up = serio->port_data;
  1090. unsigned long flags;
  1091. spin_lock_irqsave(&sunzilog_serio_lock, flags);
  1092. up->serio_open = 0;
  1093. spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
  1094. }
  1095. #endif /* CONFIG_SERIO */
  1096. #ifdef CONFIG_SERIAL_SUNZILOG_CONSOLE
  1097. static void
  1098. sunzilog_console_write(struct console *con, const char *s, unsigned int count)
  1099. {
  1100. struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
  1101. unsigned long flags;
  1102. spin_lock_irqsave(&up->port.lock, flags);
  1103. uart_console_write(&up->port, s, count, sunzilog_putchar);
  1104. udelay(2);
  1105. spin_unlock_irqrestore(&up->port.lock, flags);
  1106. }
  1107. static int __init sunzilog_console_setup(struct console *con, char *options)
  1108. {
  1109. struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
  1110. unsigned long flags;
  1111. int baud, brg;
  1112. printk(KERN_INFO "Console: ttyS%d (SunZilog zs%d)\n",
  1113. (sunzilog_reg.minor - 64) + con->index, con->index);
  1114. /* Get firmware console settings. */
  1115. sunserial_console_termios(con);
  1116. /* Firmware console speed is limited to 150-->38400 baud so
  1117. * this hackish cflag thing is OK.
  1118. */
  1119. switch (con->cflag & CBAUD) {
  1120. case B150: baud = 150; break;
  1121. case B300: baud = 300; break;
  1122. case B600: baud = 600; break;
  1123. case B1200: baud = 1200; break;
  1124. case B2400: baud = 2400; break;
  1125. case B4800: baud = 4800; break;
  1126. default: case B9600: baud = 9600; break;
  1127. case B19200: baud = 19200; break;
  1128. case B38400: baud = 38400; break;
  1129. };
  1130. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  1131. spin_lock_irqsave(&up->port.lock, flags);
  1132. up->curregs[R15] = BRKIE;
  1133. sunzilog_convert_to_zs(up, con->cflag, 0, brg);
  1134. sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
  1135. __sunzilog_startup(up);
  1136. spin_unlock_irqrestore(&up->port.lock, flags);
  1137. return 0;
  1138. }
  1139. static struct console sunzilog_console = {
  1140. .name = "ttyS",
  1141. .write = sunzilog_console_write,
  1142. .device = uart_console_device,
  1143. .setup = sunzilog_console_setup,
  1144. .flags = CON_PRINTBUFFER,
  1145. .index = -1,
  1146. .data = &sunzilog_reg,
  1147. };
  1148. static int __init sunzilog_console_init(void)
  1149. {
  1150. int i;
  1151. if (con_is_present())
  1152. return 0;
  1153. for (i = 0; i < NUM_CHANNELS; i++) {
  1154. int this_minor = sunzilog_reg.minor + i;
  1155. if ((this_minor - 64) == (serial_console - 1))
  1156. break;
  1157. }
  1158. if (i == NUM_CHANNELS)
  1159. return 0;
  1160. sunzilog_console.index = i;
  1161. sunzilog_port_table[i].flags |= SUNZILOG_FLAG_IS_CONS;
  1162. register_console(&sunzilog_console);
  1163. return 0;
  1164. }
  1165. static inline struct console *SUNZILOG_CONSOLE(void)
  1166. {
  1167. int i;
  1168. if (con_is_present())
  1169. return NULL;
  1170. for (i = 0; i < NUM_CHANNELS; i++) {
  1171. int this_minor = sunzilog_reg.minor + i;
  1172. if ((this_minor - 64) == (serial_console - 1))
  1173. break;
  1174. }
  1175. if (i == NUM_CHANNELS)
  1176. return NULL;
  1177. sunzilog_console.index = i;
  1178. sunzilog_port_table[i].flags |= SUNZILOG_FLAG_IS_CONS;
  1179. return &sunzilog_console;
  1180. }
  1181. #else
  1182. #define SUNZILOG_CONSOLE() (NULL)
  1183. #define sunzilog_console_init() do { } while (0)
  1184. #endif
  1185. /*
  1186. * We scan the PROM tree recursively. This is the most reliable way
  1187. * to find Zilog nodes on various platforms. However, we face an extreme
  1188. * shortage of kernel stack, so we must be very careful. To that end,
  1189. * we scan only to a certain depth, and we use a common property buffer
  1190. * in the scan structure.
  1191. */
  1192. #define ZS_PROPSIZE 128
  1193. #define ZS_SCAN_DEPTH 5
  1194. struct zs_probe_scan {
  1195. int depth;
  1196. void (*scanner)(struct zs_probe_scan *t, int node);
  1197. int devices;
  1198. char prop[ZS_PROPSIZE];
  1199. };
  1200. static int __inline__ sunzilog_node_ok(int node, const char *name, int len)
  1201. {
  1202. if (strncmp(name, "zs", len) == 0)
  1203. return 1;
  1204. /* Don't fold this procedure just yet. Compare to su_node_ok(). */
  1205. return 0;
  1206. }
  1207. static void __init sunzilog_scan(struct zs_probe_scan *t, int node)
  1208. {
  1209. int len;
  1210. for (; node != 0; node = prom_getsibling(node)) {
  1211. len = prom_getproperty(node, "name", t->prop, ZS_PROPSIZE);
  1212. if (len <= 1)
  1213. continue; /* Broken PROM node */
  1214. if (sunzilog_node_ok(node, t->prop, len)) {
  1215. (*t->scanner)(t, node);
  1216. } else {
  1217. if (t->depth < ZS_SCAN_DEPTH) {
  1218. t->depth++;
  1219. sunzilog_scan(t, prom_getchild(node));
  1220. --t->depth;
  1221. }
  1222. }
  1223. }
  1224. }
  1225. static void __init sunzilog_prepare(void)
  1226. {
  1227. struct uart_sunzilog_port *up;
  1228. struct zilog_layout __iomem *rp;
  1229. int channel, chip;
  1230. /*
  1231. * Temporary fix.
  1232. */
  1233. for (channel = 0; channel < NUM_CHANNELS; channel++)
  1234. spin_lock_init(&sunzilog_port_table[channel].port.lock);
  1235. sunzilog_irq_chain = up = &sunzilog_port_table[0];
  1236. for (channel = 0; channel < NUM_CHANNELS - 1; channel++)
  1237. up[channel].next = &up[channel + 1];
  1238. up[channel].next = NULL;
  1239. for (chip = 0; chip < NUM_SUNZILOG; chip++) {
  1240. rp = sunzilog_chip_regs[chip];
  1241. up[(chip * 2) + 0].port.membase = (void __iomem *)&rp->channelA;
  1242. up[(chip * 2) + 1].port.membase = (void __iomem *)&rp->channelB;
  1243. /* Channel A */
  1244. up[(chip * 2) + 0].port.iotype = UPIO_MEM;
  1245. up[(chip * 2) + 0].port.irq = zilog_irq;
  1246. up[(chip * 2) + 0].port.uartclk = ZS_CLOCK;
  1247. up[(chip * 2) + 0].port.fifosize = 1;
  1248. up[(chip * 2) + 0].port.ops = &sunzilog_pops;
  1249. up[(chip * 2) + 0].port.type = PORT_SUNZILOG;
  1250. up[(chip * 2) + 0].port.flags = 0;
  1251. up[(chip * 2) + 0].port.line = (chip * 2) + 0;
  1252. up[(chip * 2) + 0].flags |= SUNZILOG_FLAG_IS_CHANNEL_A;
  1253. /* Channel B */
  1254. up[(chip * 2) + 1].port.iotype = UPIO_MEM;
  1255. up[(chip * 2) + 1].port.irq = zilog_irq;
  1256. up[(chip * 2) + 1].port.uartclk = ZS_CLOCK;
  1257. up[(chip * 2) + 1].port.fifosize = 1;
  1258. up[(chip * 2) + 1].port.ops = &sunzilog_pops;
  1259. up[(chip * 2) + 1].port.type = PORT_SUNZILOG;
  1260. up[(chip * 2) + 1].port.flags = 0;
  1261. up[(chip * 2) + 1].port.line = (chip * 2) + 1;
  1262. up[(chip * 2) + 1].flags |= 0;
  1263. }
  1264. }
  1265. static void __init sunzilog_init_kbdms(struct uart_sunzilog_port *up, int channel)
  1266. {
  1267. int baud, brg;
  1268. if (channel == KEYBOARD_LINE) {
  1269. up->flags |= SUNZILOG_FLAG_CONS_KEYB;
  1270. up->cflag = B1200 | CS8 | CLOCAL | CREAD;
  1271. baud = 1200;
  1272. } else {
  1273. up->flags |= SUNZILOG_FLAG_CONS_MOUSE;
  1274. up->cflag = B4800 | CS8 | CLOCAL | CREAD;
  1275. baud = 4800;
  1276. }
  1277. printk(KERN_INFO "zs%d at 0x%p (irq = %s) is a SunZilog\n",
  1278. channel, up->port.membase, __irq_itoa(zilog_irq));
  1279. up->curregs[R15] = BRKIE;
  1280. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  1281. sunzilog_convert_to_zs(up, up->cflag, 0, brg);
  1282. sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
  1283. __sunzilog_startup(up);
  1284. }
  1285. #ifdef CONFIG_SERIO
  1286. static void __init sunzilog_register_serio(struct uart_sunzilog_port *up, int channel)
  1287. {
  1288. struct serio *serio;
  1289. up->serio = serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
  1290. if (serio) {
  1291. memset(serio, 0, sizeof(*serio));
  1292. serio->port_data = up;
  1293. serio->id.type = SERIO_RS232;
  1294. if (channel == KEYBOARD_LINE) {
  1295. serio->id.proto = SERIO_SUNKBD;
  1296. strlcpy(serio->name, "zskbd", sizeof(serio->name));
  1297. } else {
  1298. serio->id.proto = SERIO_SUN;
  1299. serio->id.extra = 1;
  1300. strlcpy(serio->name, "zsms", sizeof(serio->name));
  1301. }
  1302. strlcpy(serio->phys,
  1303. (channel == KEYBOARD_LINE ? "zs/serio0" : "zs/serio1"),
  1304. sizeof(serio->phys));
  1305. serio->write = sunzilog_serio_write;
  1306. serio->open = sunzilog_serio_open;
  1307. serio->close = sunzilog_serio_close;
  1308. serio_register_port(serio);
  1309. } else {
  1310. printk(KERN_WARNING "zs%d: not enough memory for serio port\n",
  1311. channel);
  1312. }
  1313. }
  1314. #endif
  1315. static void __init sunzilog_init_hw(void)
  1316. {
  1317. int i;
  1318. for (i = 0; i < NUM_CHANNELS; i++) {
  1319. struct uart_sunzilog_port *up = &sunzilog_port_table[i];
  1320. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  1321. unsigned long flags;
  1322. int baud, brg;
  1323. spin_lock_irqsave(&up->port.lock, flags);
  1324. if (ZS_IS_CHANNEL_A(up)) {
  1325. write_zsreg(channel, R9, FHWRES);
  1326. ZSDELAY_LONG();
  1327. (void) read_zsreg(channel, R0);
  1328. }
  1329. if (i == KEYBOARD_LINE || i == MOUSE_LINE) {
  1330. sunzilog_init_kbdms(up, i);
  1331. up->curregs[R9] |= (NV | MIE);
  1332. write_zsreg(channel, R9, up->curregs[R9]);
  1333. } else {
  1334. /* Normal serial TTY. */
  1335. up->parity_mask = 0xff;
  1336. up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
  1337. up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
  1338. up->curregs[R3] = RxENAB | Rx8;
  1339. up->curregs[R5] = TxENAB | Tx8;
  1340. up->curregs[R9] = NV | MIE;
  1341. up->curregs[R10] = NRZ;
  1342. up->curregs[R11] = TCBR | RCBR;
  1343. baud = 9600;
  1344. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  1345. up->curregs[R12] = (brg & 0xff);
  1346. up->curregs[R13] = (brg >> 8) & 0xff;
  1347. up->curregs[R14] = BRSRC | BRENAB;
  1348. __load_zsregs(channel, up->curregs);
  1349. write_zsreg(channel, R9, up->curregs[R9]);
  1350. }
  1351. spin_unlock_irqrestore(&up->port.lock, flags);
  1352. #ifdef CONFIG_SERIO
  1353. if (i == KEYBOARD_LINE || i == MOUSE_LINE)
  1354. sunzilog_register_serio(up, i);
  1355. #endif
  1356. }
  1357. }
  1358. static struct zilog_layout __iomem * __init get_zs(int chip, int node);
  1359. static void __init sunzilog_scan_probe(struct zs_probe_scan *t, int node)
  1360. {
  1361. sunzilog_chip_regs[t->devices] = get_zs(t->devices, node);
  1362. t->devices++;
  1363. }
  1364. static int __init sunzilog_ports_init(void)
  1365. {
  1366. struct zs_probe_scan scan;
  1367. int ret;
  1368. int uart_count;
  1369. int i;
  1370. printk(KERN_DEBUG "SunZilog: %d chips.\n", NUM_SUNZILOG);
  1371. scan.scanner = sunzilog_scan_probe;
  1372. scan.depth = 0;
  1373. scan.devices = 0;
  1374. sunzilog_scan(&scan, prom_getchild(prom_root_node));
  1375. sunzilog_prepare();
  1376. if (request_irq(zilog_irq, sunzilog_interrupt, SA_SHIRQ,
  1377. "SunZilog", sunzilog_irq_chain)) {
  1378. prom_printf("SunZilog: Unable to register zs interrupt handler.\n");
  1379. prom_halt();
  1380. }
  1381. sunzilog_init_hw();
  1382. /* We can only init this once we have probed the Zilogs
  1383. * in the system. Do not count channels assigned to keyboards
  1384. * or mice when we are deciding how many ports to register.
  1385. */
  1386. uart_count = 0;
  1387. for (i = 0; i < NUM_CHANNELS; i++) {
  1388. struct uart_sunzilog_port *up = &sunzilog_port_table[i];
  1389. if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up))
  1390. continue;
  1391. uart_count++;
  1392. }
  1393. sunzilog_reg.nr = uart_count;
  1394. sunzilog_reg.minor = sunserial_current_minor;
  1395. ret = uart_register_driver(&sunzilog_reg);
  1396. if (ret == 0) {
  1397. sunzilog_reg.tty_driver->name_base = sunzilog_reg.minor - 64;
  1398. sunzilog_reg.cons = SUNZILOG_CONSOLE();
  1399. sunserial_current_minor += uart_count;
  1400. for (i = 0; i < NUM_CHANNELS; i++) {
  1401. struct uart_sunzilog_port *up = &sunzilog_port_table[i];
  1402. if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up))
  1403. continue;
  1404. if (uart_add_one_port(&sunzilog_reg, &up->port)) {
  1405. printk(KERN_ERR
  1406. "SunZilog: failed to add port zs%d\n", i);
  1407. }
  1408. }
  1409. }
  1410. return ret;
  1411. }
  1412. static void __init sunzilog_scan_count(struct zs_probe_scan *t, int node)
  1413. {
  1414. t->devices++;
  1415. }
  1416. static int __init sunzilog_ports_count(void)
  1417. {
  1418. struct zs_probe_scan scan;
  1419. /* Sun4 Zilog setup is hard coded, no probing to do. */
  1420. if (sparc_cpu_model == sun4)
  1421. return 2;
  1422. scan.scanner = sunzilog_scan_count;
  1423. scan.depth = 0;
  1424. scan.devices = 0;
  1425. sunzilog_scan(&scan, prom_getchild(prom_root_node));
  1426. return scan.devices;
  1427. }
  1428. static int __init sunzilog_init(void)
  1429. {
  1430. NUM_SUNZILOG = sunzilog_ports_count();
  1431. if (NUM_SUNZILOG == 0)
  1432. return -ENODEV;
  1433. sunzilog_alloc_tables();
  1434. sunzilog_ports_init();
  1435. return 0;
  1436. }
  1437. static void __exit sunzilog_exit(void)
  1438. {
  1439. int i;
  1440. for (i = 0; i < NUM_CHANNELS; i++) {
  1441. struct uart_sunzilog_port *up = &sunzilog_port_table[i];
  1442. if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up)) {
  1443. #ifdef CONFIG_SERIO
  1444. if (up->serio) {
  1445. serio_unregister_port(up->serio);
  1446. up->serio = NULL;
  1447. }
  1448. #endif
  1449. } else
  1450. uart_remove_one_port(&sunzilog_reg, &up->port);
  1451. }
  1452. uart_unregister_driver(&sunzilog_reg);
  1453. }
  1454. module_init(sunzilog_init);
  1455. module_exit(sunzilog_exit);
  1456. MODULE_AUTHOR("David S. Miller");
  1457. MODULE_DESCRIPTION("Sun Zilog serial port driver");
  1458. MODULE_LICENSE("GPL");