sunzilog.c 39 KB

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