sunzilog.c 44 KB

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