sunzilog.c 41 KB

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