sunzilog.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  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_port *
  272. sunzilog_receive_chars(struct uart_sunzilog_port *up,
  273. struct zilog_channel __iomem *channel)
  274. {
  275. struct tty_port *port = NULL;
  276. unsigned char ch, r1, flag;
  277. if (up->port.state != NULL) /* Unopened serial console */
  278. port = &up->port.state->port;
  279. for (;;) {
  280. r1 = read_zsreg(channel, R1);
  281. if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
  282. writeb(ERR_RES, &channel->control);
  283. ZSDELAY();
  284. ZS_WSYNC(channel);
  285. }
  286. ch = readb(&channel->control);
  287. ZSDELAY();
  288. /* This funny hack depends upon BRK_ABRT not interfering
  289. * with the other bits we care about in R1.
  290. */
  291. if (ch & BRK_ABRT)
  292. r1 |= BRK_ABRT;
  293. if (!(ch & Rx_CH_AV))
  294. break;
  295. ch = readb(&channel->data);
  296. ZSDELAY();
  297. ch &= up->parity_mask;
  298. if (unlikely(ZS_IS_KEYB(up)) || unlikely(ZS_IS_MOUSE(up))) {
  299. sunzilog_kbdms_receive_chars(up, ch, 0);
  300. continue;
  301. }
  302. /* A real serial line, record the character and status. */
  303. flag = TTY_NORMAL;
  304. up->port.icount.rx++;
  305. if (r1 & (BRK_ABRT | PAR_ERR | Rx_OVR | CRC_ERR)) {
  306. if (r1 & BRK_ABRT) {
  307. r1 &= ~(PAR_ERR | CRC_ERR);
  308. up->port.icount.brk++;
  309. if (uart_handle_break(&up->port))
  310. continue;
  311. }
  312. else if (r1 & PAR_ERR)
  313. up->port.icount.parity++;
  314. else if (r1 & CRC_ERR)
  315. up->port.icount.frame++;
  316. if (r1 & Rx_OVR)
  317. up->port.icount.overrun++;
  318. r1 &= up->port.read_status_mask;
  319. if (r1 & BRK_ABRT)
  320. flag = TTY_BREAK;
  321. else if (r1 & PAR_ERR)
  322. flag = TTY_PARITY;
  323. else if (r1 & CRC_ERR)
  324. flag = TTY_FRAME;
  325. }
  326. if (uart_handle_sysrq_char(&up->port, ch) || !port)
  327. continue;
  328. if (up->port.ignore_status_mask == 0xff ||
  329. (r1 & up->port.ignore_status_mask) == 0) {
  330. tty_insert_flip_char(port, ch, flag);
  331. }
  332. if (r1 & Rx_OVR)
  333. tty_insert_flip_char(port, 0, TTY_OVERRUN);
  334. }
  335. return port;
  336. }
  337. static void sunzilog_status_handle(struct uart_sunzilog_port *up,
  338. struct zilog_channel __iomem *channel)
  339. {
  340. unsigned char status;
  341. status = readb(&channel->control);
  342. ZSDELAY();
  343. writeb(RES_EXT_INT, &channel->control);
  344. ZSDELAY();
  345. ZS_WSYNC(channel);
  346. if (status & BRK_ABRT) {
  347. if (ZS_IS_MOUSE(up))
  348. sunzilog_kbdms_receive_chars(up, 0, 1);
  349. if (ZS_IS_CONS(up)) {
  350. /* Wait for BREAK to deassert to avoid potentially
  351. * confusing the PROM.
  352. */
  353. while (1) {
  354. status = readb(&channel->control);
  355. ZSDELAY();
  356. if (!(status & BRK_ABRT))
  357. break;
  358. }
  359. sun_do_break();
  360. return;
  361. }
  362. }
  363. if (ZS_WANTS_MODEM_STATUS(up)) {
  364. if (status & SYNC)
  365. up->port.icount.dsr++;
  366. /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
  367. * But it does not tell us which bit has changed, we have to keep
  368. * track of this ourselves.
  369. */
  370. if ((status ^ up->prev_status) ^ DCD)
  371. uart_handle_dcd_change(&up->port,
  372. (status & DCD));
  373. if ((status ^ up->prev_status) ^ CTS)
  374. uart_handle_cts_change(&up->port,
  375. (status & CTS));
  376. wake_up_interruptible(&up->port.state->port.delta_msr_wait);
  377. }
  378. up->prev_status = status;
  379. }
  380. static void sunzilog_transmit_chars(struct uart_sunzilog_port *up,
  381. struct zilog_channel __iomem *channel)
  382. {
  383. struct circ_buf *xmit;
  384. if (ZS_IS_CONS(up)) {
  385. unsigned char status = readb(&channel->control);
  386. ZSDELAY();
  387. /* TX still busy? Just wait for the next TX done interrupt.
  388. *
  389. * It can occur because of how we do serial console writes. It would
  390. * be nice to transmit console writes just like we normally would for
  391. * a TTY line. (ie. buffered and TX interrupt driven). That is not
  392. * easy because console writes cannot sleep. One solution might be
  393. * to poll on enough port->xmit space becoming free. -DaveM
  394. */
  395. if (!(status & Tx_BUF_EMP))
  396. return;
  397. }
  398. up->flags &= ~SUNZILOG_FLAG_TX_ACTIVE;
  399. if (ZS_REGS_HELD(up)) {
  400. __load_zsregs(channel, up->curregs);
  401. up->flags &= ~SUNZILOG_FLAG_REGS_HELD;
  402. }
  403. if (ZS_TX_STOPPED(up)) {
  404. up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
  405. goto ack_tx_int;
  406. }
  407. if (up->port.x_char) {
  408. up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
  409. writeb(up->port.x_char, &channel->data);
  410. ZSDELAY();
  411. ZS_WSYNC(channel);
  412. up->port.icount.tx++;
  413. up->port.x_char = 0;
  414. return;
  415. }
  416. if (up->port.state == NULL)
  417. goto ack_tx_int;
  418. xmit = &up->port.state->xmit;
  419. if (uart_circ_empty(xmit))
  420. goto ack_tx_int;
  421. if (uart_tx_stopped(&up->port))
  422. goto ack_tx_int;
  423. up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
  424. writeb(xmit->buf[xmit->tail], &channel->data);
  425. ZSDELAY();
  426. ZS_WSYNC(channel);
  427. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  428. up->port.icount.tx++;
  429. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  430. uart_write_wakeup(&up->port);
  431. return;
  432. ack_tx_int:
  433. writeb(RES_Tx_P, &channel->control);
  434. ZSDELAY();
  435. ZS_WSYNC(channel);
  436. }
  437. static irqreturn_t sunzilog_interrupt(int irq, void *dev_id)
  438. {
  439. struct uart_sunzilog_port *up = dev_id;
  440. while (up) {
  441. struct zilog_channel __iomem *channel
  442. = ZILOG_CHANNEL_FROM_PORT(&up->port);
  443. struct tty_port *port;
  444. unsigned char r3;
  445. spin_lock(&up->port.lock);
  446. r3 = read_zsreg(channel, R3);
  447. /* Channel A */
  448. port = NULL;
  449. if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
  450. writeb(RES_H_IUS, &channel->control);
  451. ZSDELAY();
  452. ZS_WSYNC(channel);
  453. if (r3 & CHARxIP)
  454. port = sunzilog_receive_chars(up, channel);
  455. if (r3 & CHAEXT)
  456. sunzilog_status_handle(up, channel);
  457. if (r3 & CHATxIP)
  458. sunzilog_transmit_chars(up, channel);
  459. }
  460. spin_unlock(&up->port.lock);
  461. if (port)
  462. tty_flip_buffer_push(port);
  463. /* Channel B */
  464. up = up->next;
  465. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  466. spin_lock(&up->port.lock);
  467. port = NULL;
  468. if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
  469. writeb(RES_H_IUS, &channel->control);
  470. ZSDELAY();
  471. ZS_WSYNC(channel);
  472. if (r3 & CHBRxIP)
  473. port = sunzilog_receive_chars(up, channel);
  474. if (r3 & CHBEXT)
  475. sunzilog_status_handle(up, channel);
  476. if (r3 & CHBTxIP)
  477. sunzilog_transmit_chars(up, channel);
  478. }
  479. spin_unlock(&up->port.lock);
  480. if (port)
  481. tty_flip_buffer_push(port);
  482. up = up->next;
  483. }
  484. return IRQ_HANDLED;
  485. }
  486. /* A convenient way to quickly get R0 status. The caller must _not_ hold the
  487. * port lock, it is acquired here.
  488. */
  489. static __inline__ unsigned char sunzilog_read_channel_status(struct uart_port *port)
  490. {
  491. struct zilog_channel __iomem *channel;
  492. unsigned char status;
  493. channel = ZILOG_CHANNEL_FROM_PORT(port);
  494. status = readb(&channel->control);
  495. ZSDELAY();
  496. return status;
  497. }
  498. /* The port lock is not held. */
  499. static unsigned int sunzilog_tx_empty(struct uart_port *port)
  500. {
  501. unsigned long flags;
  502. unsigned char status;
  503. unsigned int ret;
  504. spin_lock_irqsave(&port->lock, flags);
  505. status = sunzilog_read_channel_status(port);
  506. spin_unlock_irqrestore(&port->lock, flags);
  507. if (status & Tx_BUF_EMP)
  508. ret = TIOCSER_TEMT;
  509. else
  510. ret = 0;
  511. return ret;
  512. }
  513. /* The port lock is held and interrupts are disabled. */
  514. static unsigned int sunzilog_get_mctrl(struct uart_port *port)
  515. {
  516. unsigned char status;
  517. unsigned int ret;
  518. status = sunzilog_read_channel_status(port);
  519. ret = 0;
  520. if (status & DCD)
  521. ret |= TIOCM_CAR;
  522. if (status & SYNC)
  523. ret |= TIOCM_DSR;
  524. if (status & CTS)
  525. ret |= TIOCM_CTS;
  526. return ret;
  527. }
  528. /* The port lock is held and interrupts are disabled. */
  529. static void sunzilog_set_mctrl(struct uart_port *port, unsigned int mctrl)
  530. {
  531. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  532. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  533. unsigned char set_bits, clear_bits;
  534. set_bits = clear_bits = 0;
  535. if (mctrl & TIOCM_RTS)
  536. set_bits |= RTS;
  537. else
  538. clear_bits |= RTS;
  539. if (mctrl & TIOCM_DTR)
  540. set_bits |= DTR;
  541. else
  542. clear_bits |= DTR;
  543. /* NOTE: Not subject to 'transmitter active' rule. */
  544. up->curregs[R5] |= set_bits;
  545. up->curregs[R5] &= ~clear_bits;
  546. write_zsreg(channel, R5, up->curregs[R5]);
  547. }
  548. /* The port lock is held and interrupts are disabled. */
  549. static void sunzilog_stop_tx(struct uart_port *port)
  550. {
  551. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  552. up->flags |= SUNZILOG_FLAG_TX_STOPPED;
  553. }
  554. /* The port lock is held and interrupts are disabled. */
  555. static void sunzilog_start_tx(struct uart_port *port)
  556. {
  557. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  558. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  559. unsigned char status;
  560. up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
  561. up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
  562. status = readb(&channel->control);
  563. ZSDELAY();
  564. /* TX busy? Just wait for the TX done interrupt. */
  565. if (!(status & Tx_BUF_EMP))
  566. return;
  567. /* Send the first character to jump-start the TX done
  568. * IRQ sending engine.
  569. */
  570. if (port->x_char) {
  571. writeb(port->x_char, &channel->data);
  572. ZSDELAY();
  573. ZS_WSYNC(channel);
  574. port->icount.tx++;
  575. port->x_char = 0;
  576. } else {
  577. struct circ_buf *xmit = &port->state->xmit;
  578. if (uart_circ_empty(xmit))
  579. return;
  580. writeb(xmit->buf[xmit->tail], &channel->data);
  581. ZSDELAY();
  582. ZS_WSYNC(channel);
  583. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  584. port->icount.tx++;
  585. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  586. uart_write_wakeup(&up->port);
  587. }
  588. }
  589. /* The port lock is held. */
  590. static void sunzilog_stop_rx(struct uart_port *port)
  591. {
  592. struct uart_sunzilog_port *up = UART_ZILOG(port);
  593. struct zilog_channel __iomem *channel;
  594. if (ZS_IS_CONS(up))
  595. return;
  596. channel = ZILOG_CHANNEL_FROM_PORT(port);
  597. /* Disable all RX interrupts. */
  598. up->curregs[R1] &= ~RxINT_MASK;
  599. sunzilog_maybe_update_regs(up, channel);
  600. }
  601. /* The port lock is held. */
  602. static void sunzilog_enable_ms(struct uart_port *port)
  603. {
  604. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  605. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  606. unsigned char new_reg;
  607. new_reg = up->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
  608. if (new_reg != up->curregs[R15]) {
  609. up->curregs[R15] = new_reg;
  610. /* NOTE: Not subject to 'transmitter active' rule. */
  611. write_zsreg(channel, R15, up->curregs[R15] & ~WR7pEN);
  612. }
  613. }
  614. /* The port lock is not held. */
  615. static void sunzilog_break_ctl(struct uart_port *port, int break_state)
  616. {
  617. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  618. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  619. unsigned char set_bits, clear_bits, new_reg;
  620. unsigned long flags;
  621. set_bits = clear_bits = 0;
  622. if (break_state)
  623. set_bits |= SND_BRK;
  624. else
  625. clear_bits |= SND_BRK;
  626. spin_lock_irqsave(&port->lock, flags);
  627. new_reg = (up->curregs[R5] | set_bits) & ~clear_bits;
  628. if (new_reg != up->curregs[R5]) {
  629. up->curregs[R5] = new_reg;
  630. /* NOTE: Not subject to 'transmitter active' rule. */
  631. write_zsreg(channel, R5, up->curregs[R5]);
  632. }
  633. spin_unlock_irqrestore(&port->lock, flags);
  634. }
  635. static void __sunzilog_startup(struct uart_sunzilog_port *up)
  636. {
  637. struct zilog_channel __iomem *channel;
  638. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  639. up->prev_status = readb(&channel->control);
  640. /* Enable receiver and transmitter. */
  641. up->curregs[R3] |= RxENAB;
  642. up->curregs[R5] |= TxENAB;
  643. up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
  644. sunzilog_maybe_update_regs(up, channel);
  645. }
  646. static int sunzilog_startup(struct uart_port *port)
  647. {
  648. struct uart_sunzilog_port *up = UART_ZILOG(port);
  649. unsigned long flags;
  650. if (ZS_IS_CONS(up))
  651. return 0;
  652. spin_lock_irqsave(&port->lock, flags);
  653. __sunzilog_startup(up);
  654. spin_unlock_irqrestore(&port->lock, flags);
  655. return 0;
  656. }
  657. /*
  658. * The test for ZS_IS_CONS is explained by the following e-mail:
  659. *****
  660. * From: Russell King <rmk@arm.linux.org.uk>
  661. * Date: Sun, 8 Dec 2002 10:18:38 +0000
  662. *
  663. * On Sun, Dec 08, 2002 at 02:43:36AM -0500, Pete Zaitcev wrote:
  664. * > I boot my 2.5 boxes using "console=ttyS0,9600" argument,
  665. * > and I noticed that something is not right with reference
  666. * > counting in this case. It seems that when the console
  667. * > is open by kernel initially, this is not accounted
  668. * > as an open, and uart_startup is not called.
  669. *
  670. * That is correct. We are unable to call uart_startup when the serial
  671. * console is initialised because it may need to allocate memory (as
  672. * request_irq does) and the memory allocators may not have been
  673. * initialised.
  674. *
  675. * 1. initialise the port into a state where it can send characters in the
  676. * console write method.
  677. *
  678. * 2. don't do the actual hardware shutdown in your shutdown() method (but
  679. * do the normal software shutdown - ie, free irqs etc)
  680. *****
  681. */
  682. static void sunzilog_shutdown(struct uart_port *port)
  683. {
  684. struct uart_sunzilog_port *up = UART_ZILOG(port);
  685. struct zilog_channel __iomem *channel;
  686. unsigned long flags;
  687. if (ZS_IS_CONS(up))
  688. return;
  689. spin_lock_irqsave(&port->lock, flags);
  690. channel = ZILOG_CHANNEL_FROM_PORT(port);
  691. /* Disable receiver and transmitter. */
  692. up->curregs[R3] &= ~RxENAB;
  693. up->curregs[R5] &= ~TxENAB;
  694. /* Disable all interrupts and BRK assertion. */
  695. up->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
  696. up->curregs[R5] &= ~SND_BRK;
  697. sunzilog_maybe_update_regs(up, channel);
  698. spin_unlock_irqrestore(&port->lock, flags);
  699. }
  700. /* Shared by TTY driver and serial console setup. The port lock is held
  701. * and local interrupts are disabled.
  702. */
  703. static void
  704. sunzilog_convert_to_zs(struct uart_sunzilog_port *up, unsigned int cflag,
  705. unsigned int iflag, int brg)
  706. {
  707. up->curregs[R10] = NRZ;
  708. up->curregs[R11] = TCBR | RCBR;
  709. /* Program BAUD and clock source. */
  710. up->curregs[R4] &= ~XCLK_MASK;
  711. up->curregs[R4] |= X16CLK;
  712. up->curregs[R12] = brg & 0xff;
  713. up->curregs[R13] = (brg >> 8) & 0xff;
  714. up->curregs[R14] = BRSRC | BRENAB;
  715. /* Character size, stop bits, and parity. */
  716. up->curregs[R3] &= ~RxN_MASK;
  717. up->curregs[R5] &= ~TxN_MASK;
  718. switch (cflag & CSIZE) {
  719. case CS5:
  720. up->curregs[R3] |= Rx5;
  721. up->curregs[R5] |= Tx5;
  722. up->parity_mask = 0x1f;
  723. break;
  724. case CS6:
  725. up->curregs[R3] |= Rx6;
  726. up->curregs[R5] |= Tx6;
  727. up->parity_mask = 0x3f;
  728. break;
  729. case CS7:
  730. up->curregs[R3] |= Rx7;
  731. up->curregs[R5] |= Tx7;
  732. up->parity_mask = 0x7f;
  733. break;
  734. case CS8:
  735. default:
  736. up->curregs[R3] |= Rx8;
  737. up->curregs[R5] |= Tx8;
  738. up->parity_mask = 0xff;
  739. break;
  740. }
  741. up->curregs[R4] &= ~0x0c;
  742. if (cflag & CSTOPB)
  743. up->curregs[R4] |= SB2;
  744. else
  745. up->curregs[R4] |= SB1;
  746. if (cflag & PARENB)
  747. up->curregs[R4] |= PAR_ENAB;
  748. else
  749. up->curregs[R4] &= ~PAR_ENAB;
  750. if (!(cflag & PARODD))
  751. up->curregs[R4] |= PAR_EVEN;
  752. else
  753. up->curregs[R4] &= ~PAR_EVEN;
  754. up->port.read_status_mask = Rx_OVR;
  755. if (iflag & INPCK)
  756. up->port.read_status_mask |= CRC_ERR | PAR_ERR;
  757. if (iflag & (IGNBRK | BRKINT | PARMRK))
  758. up->port.read_status_mask |= BRK_ABRT;
  759. up->port.ignore_status_mask = 0;
  760. if (iflag & IGNPAR)
  761. up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
  762. if (iflag & IGNBRK) {
  763. up->port.ignore_status_mask |= BRK_ABRT;
  764. if (iflag & IGNPAR)
  765. up->port.ignore_status_mask |= Rx_OVR;
  766. }
  767. if ((cflag & CREAD) == 0)
  768. up->port.ignore_status_mask = 0xff;
  769. }
  770. /* The port lock is not held. */
  771. static void
  772. sunzilog_set_termios(struct uart_port *port, struct ktermios *termios,
  773. struct ktermios *old)
  774. {
  775. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  776. unsigned long flags;
  777. int baud, brg;
  778. baud = uart_get_baud_rate(port, termios, old, 1200, 76800);
  779. spin_lock_irqsave(&up->port.lock, flags);
  780. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  781. sunzilog_convert_to_zs(up, termios->c_cflag, termios->c_iflag, brg);
  782. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  783. up->flags |= SUNZILOG_FLAG_MODEM_STATUS;
  784. else
  785. up->flags &= ~SUNZILOG_FLAG_MODEM_STATUS;
  786. up->cflag = termios->c_cflag;
  787. sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));
  788. uart_update_timeout(port, termios->c_cflag, baud);
  789. spin_unlock_irqrestore(&up->port.lock, flags);
  790. }
  791. static const char *sunzilog_type(struct uart_port *port)
  792. {
  793. struct uart_sunzilog_port *up = UART_ZILOG(port);
  794. return (up->flags & SUNZILOG_FLAG_ESCC) ? "zs (ESCC)" : "zs";
  795. }
  796. /* We do not request/release mappings of the registers here, this
  797. * happens at early serial probe time.
  798. */
  799. static void sunzilog_release_port(struct uart_port *port)
  800. {
  801. }
  802. static int sunzilog_request_port(struct uart_port *port)
  803. {
  804. return 0;
  805. }
  806. /* These do not need to do anything interesting either. */
  807. static void sunzilog_config_port(struct uart_port *port, int flags)
  808. {
  809. }
  810. /* We do not support letting the user mess with the divisor, IRQ, etc. */
  811. static int sunzilog_verify_port(struct uart_port *port, struct serial_struct *ser)
  812. {
  813. return -EINVAL;
  814. }
  815. #ifdef CONFIG_CONSOLE_POLL
  816. static int sunzilog_get_poll_char(struct uart_port *port)
  817. {
  818. unsigned char ch, r1;
  819. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  820. struct zilog_channel __iomem *channel
  821. = ZILOG_CHANNEL_FROM_PORT(&up->port);
  822. r1 = read_zsreg(channel, R1);
  823. if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
  824. writeb(ERR_RES, &channel->control);
  825. ZSDELAY();
  826. ZS_WSYNC(channel);
  827. }
  828. ch = readb(&channel->control);
  829. ZSDELAY();
  830. /* This funny hack depends upon BRK_ABRT not interfering
  831. * with the other bits we care about in R1.
  832. */
  833. if (ch & BRK_ABRT)
  834. r1 |= BRK_ABRT;
  835. if (!(ch & Rx_CH_AV))
  836. return NO_POLL_CHAR;
  837. ch = readb(&channel->data);
  838. ZSDELAY();
  839. ch &= up->parity_mask;
  840. return ch;
  841. }
  842. static void sunzilog_put_poll_char(struct uart_port *port,
  843. unsigned char ch)
  844. {
  845. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *)port;
  846. sunzilog_putchar(&up->port, ch);
  847. }
  848. #endif /* CONFIG_CONSOLE_POLL */
  849. static struct uart_ops sunzilog_pops = {
  850. .tx_empty = sunzilog_tx_empty,
  851. .set_mctrl = sunzilog_set_mctrl,
  852. .get_mctrl = sunzilog_get_mctrl,
  853. .stop_tx = sunzilog_stop_tx,
  854. .start_tx = sunzilog_start_tx,
  855. .stop_rx = sunzilog_stop_rx,
  856. .enable_ms = sunzilog_enable_ms,
  857. .break_ctl = sunzilog_break_ctl,
  858. .startup = sunzilog_startup,
  859. .shutdown = sunzilog_shutdown,
  860. .set_termios = sunzilog_set_termios,
  861. .type = sunzilog_type,
  862. .release_port = sunzilog_release_port,
  863. .request_port = sunzilog_request_port,
  864. .config_port = sunzilog_config_port,
  865. .verify_port = sunzilog_verify_port,
  866. #ifdef CONFIG_CONSOLE_POLL
  867. .poll_get_char = sunzilog_get_poll_char,
  868. .poll_put_char = sunzilog_put_poll_char,
  869. #endif
  870. };
  871. static int uart_chip_count;
  872. static struct uart_sunzilog_port *sunzilog_port_table;
  873. static struct zilog_layout __iomem **sunzilog_chip_regs;
  874. static struct uart_sunzilog_port *sunzilog_irq_chain;
  875. static struct uart_driver sunzilog_reg = {
  876. .owner = THIS_MODULE,
  877. .driver_name = "sunzilog",
  878. .dev_name = "ttyS",
  879. .major = TTY_MAJOR,
  880. };
  881. static int __init sunzilog_alloc_tables(int num_sunzilog)
  882. {
  883. struct uart_sunzilog_port *up;
  884. unsigned long size;
  885. int num_channels = num_sunzilog * 2;
  886. int i;
  887. size = num_channels * sizeof(struct uart_sunzilog_port);
  888. sunzilog_port_table = kzalloc(size, GFP_KERNEL);
  889. if (!sunzilog_port_table)
  890. return -ENOMEM;
  891. for (i = 0; i < num_channels; i++) {
  892. up = &sunzilog_port_table[i];
  893. spin_lock_init(&up->port.lock);
  894. if (i == 0)
  895. sunzilog_irq_chain = up;
  896. if (i < num_channels - 1)
  897. up->next = up + 1;
  898. else
  899. up->next = NULL;
  900. }
  901. size = num_sunzilog * sizeof(struct zilog_layout __iomem *);
  902. sunzilog_chip_regs = kzalloc(size, GFP_KERNEL);
  903. if (!sunzilog_chip_regs) {
  904. kfree(sunzilog_port_table);
  905. sunzilog_irq_chain = NULL;
  906. return -ENOMEM;
  907. }
  908. return 0;
  909. }
  910. static void sunzilog_free_tables(void)
  911. {
  912. kfree(sunzilog_port_table);
  913. sunzilog_irq_chain = NULL;
  914. kfree(sunzilog_chip_regs);
  915. }
  916. #define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
  917. static void sunzilog_putchar(struct uart_port *port, int ch)
  918. {
  919. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  920. int loops = ZS_PUT_CHAR_MAX_DELAY;
  921. /* This is a timed polling loop so do not switch the explicit
  922. * udelay with ZSDELAY as that is a NOP on some platforms. -DaveM
  923. */
  924. do {
  925. unsigned char val = readb(&channel->control);
  926. if (val & Tx_BUF_EMP) {
  927. ZSDELAY();
  928. break;
  929. }
  930. udelay(5);
  931. } while (--loops);
  932. writeb(ch, &channel->data);
  933. ZSDELAY();
  934. ZS_WSYNC(channel);
  935. }
  936. #ifdef CONFIG_SERIO
  937. static DEFINE_SPINLOCK(sunzilog_serio_lock);
  938. static int sunzilog_serio_write(struct serio *serio, unsigned char ch)
  939. {
  940. struct uart_sunzilog_port *up = serio->port_data;
  941. unsigned long flags;
  942. spin_lock_irqsave(&sunzilog_serio_lock, flags);
  943. sunzilog_putchar(&up->port, ch);
  944. spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
  945. return 0;
  946. }
  947. static int sunzilog_serio_open(struct serio *serio)
  948. {
  949. struct uart_sunzilog_port *up = serio->port_data;
  950. unsigned long flags;
  951. int ret;
  952. spin_lock_irqsave(&sunzilog_serio_lock, flags);
  953. if (!up->serio_open) {
  954. up->serio_open = 1;
  955. ret = 0;
  956. } else
  957. ret = -EBUSY;
  958. spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
  959. return ret;
  960. }
  961. static void sunzilog_serio_close(struct serio *serio)
  962. {
  963. struct uart_sunzilog_port *up = serio->port_data;
  964. unsigned long flags;
  965. spin_lock_irqsave(&sunzilog_serio_lock, flags);
  966. up->serio_open = 0;
  967. spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
  968. }
  969. #endif /* CONFIG_SERIO */
  970. #ifdef CONFIG_SERIAL_SUNZILOG_CONSOLE
  971. static void
  972. sunzilog_console_write(struct console *con, const char *s, unsigned int count)
  973. {
  974. struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
  975. unsigned long flags;
  976. int locked = 1;
  977. if (up->port.sysrq || oops_in_progress)
  978. locked = spin_trylock_irqsave(&up->port.lock, flags);
  979. else
  980. spin_lock_irqsave(&up->port.lock, flags);
  981. uart_console_write(&up->port, s, count, sunzilog_putchar);
  982. udelay(2);
  983. if (locked)
  984. spin_unlock_irqrestore(&up->port.lock, flags);
  985. }
  986. static int __init sunzilog_console_setup(struct console *con, char *options)
  987. {
  988. struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
  989. unsigned long flags;
  990. int baud, brg;
  991. if (up->port.type != PORT_SUNZILOG)
  992. return -1;
  993. printk(KERN_INFO "Console: ttyS%d (SunZilog zs%d)\n",
  994. (sunzilog_reg.minor - 64) + con->index, con->index);
  995. /* Get firmware console settings. */
  996. sunserial_console_termios(con, up->port.dev->of_node);
  997. /* Firmware console speed is limited to 150-->38400 baud so
  998. * this hackish cflag thing is OK.
  999. */
  1000. switch (con->cflag & CBAUD) {
  1001. case B150: baud = 150; break;
  1002. case B300: baud = 300; break;
  1003. case B600: baud = 600; break;
  1004. case B1200: baud = 1200; break;
  1005. case B2400: baud = 2400; break;
  1006. case B4800: baud = 4800; break;
  1007. default: case B9600: baud = 9600; break;
  1008. case B19200: baud = 19200; break;
  1009. case B38400: baud = 38400; break;
  1010. }
  1011. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  1012. spin_lock_irqsave(&up->port.lock, flags);
  1013. up->curregs[R15] |= BRKIE;
  1014. sunzilog_convert_to_zs(up, con->cflag, 0, brg);
  1015. sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
  1016. __sunzilog_startup(up);
  1017. spin_unlock_irqrestore(&up->port.lock, flags);
  1018. return 0;
  1019. }
  1020. static struct console sunzilog_console_ops = {
  1021. .name = "ttyS",
  1022. .write = sunzilog_console_write,
  1023. .device = uart_console_device,
  1024. .setup = sunzilog_console_setup,
  1025. .flags = CON_PRINTBUFFER,
  1026. .index = -1,
  1027. .data = &sunzilog_reg,
  1028. };
  1029. static inline struct console *SUNZILOG_CONSOLE(void)
  1030. {
  1031. return &sunzilog_console_ops;
  1032. }
  1033. #else
  1034. #define SUNZILOG_CONSOLE() (NULL)
  1035. #endif
  1036. static void sunzilog_init_kbdms(struct uart_sunzilog_port *up)
  1037. {
  1038. int baud, brg;
  1039. if (up->flags & SUNZILOG_FLAG_CONS_KEYB) {
  1040. up->cflag = B1200 | CS8 | CLOCAL | CREAD;
  1041. baud = 1200;
  1042. } else {
  1043. up->cflag = B4800 | CS8 | CLOCAL | CREAD;
  1044. baud = 4800;
  1045. }
  1046. up->curregs[R15] |= BRKIE;
  1047. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  1048. sunzilog_convert_to_zs(up, up->cflag, 0, brg);
  1049. sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
  1050. __sunzilog_startup(up);
  1051. }
  1052. #ifdef CONFIG_SERIO
  1053. static void sunzilog_register_serio(struct uart_sunzilog_port *up)
  1054. {
  1055. struct serio *serio = &up->serio;
  1056. serio->port_data = up;
  1057. serio->id.type = SERIO_RS232;
  1058. if (up->flags & SUNZILOG_FLAG_CONS_KEYB) {
  1059. serio->id.proto = SERIO_SUNKBD;
  1060. strlcpy(serio->name, "zskbd", sizeof(serio->name));
  1061. } else {
  1062. serio->id.proto = SERIO_SUN;
  1063. serio->id.extra = 1;
  1064. strlcpy(serio->name, "zsms", sizeof(serio->name));
  1065. }
  1066. strlcpy(serio->phys,
  1067. ((up->flags & SUNZILOG_FLAG_CONS_KEYB) ?
  1068. "zs/serio0" : "zs/serio1"),
  1069. sizeof(serio->phys));
  1070. serio->write = sunzilog_serio_write;
  1071. serio->open = sunzilog_serio_open;
  1072. serio->close = sunzilog_serio_close;
  1073. serio->dev.parent = up->port.dev;
  1074. serio_register_port(serio);
  1075. }
  1076. #endif
  1077. static void sunzilog_init_hw(struct uart_sunzilog_port *up)
  1078. {
  1079. struct zilog_channel __iomem *channel;
  1080. unsigned long flags;
  1081. int baud, brg;
  1082. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  1083. spin_lock_irqsave(&up->port.lock, flags);
  1084. if (ZS_IS_CHANNEL_A(up)) {
  1085. write_zsreg(channel, R9, FHWRES);
  1086. ZSDELAY_LONG();
  1087. (void) read_zsreg(channel, R0);
  1088. }
  1089. if (up->flags & (SUNZILOG_FLAG_CONS_KEYB |
  1090. SUNZILOG_FLAG_CONS_MOUSE)) {
  1091. up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
  1092. up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
  1093. up->curregs[R3] = RxENAB | Rx8;
  1094. up->curregs[R5] = TxENAB | Tx8;
  1095. up->curregs[R6] = 0x00; /* SDLC Address */
  1096. up->curregs[R7] = 0x7E; /* SDLC Flag */
  1097. up->curregs[R9] = NV;
  1098. up->curregs[R7p] = 0x00;
  1099. sunzilog_init_kbdms(up);
  1100. /* Only enable interrupts if an ISR handler available */
  1101. if (up->flags & SUNZILOG_FLAG_ISR_HANDLER)
  1102. up->curregs[R9] |= MIE;
  1103. write_zsreg(channel, R9, up->curregs[R9]);
  1104. } else {
  1105. /* Normal serial TTY. */
  1106. up->parity_mask = 0xff;
  1107. up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
  1108. up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
  1109. up->curregs[R3] = RxENAB | Rx8;
  1110. up->curregs[R5] = TxENAB | Tx8;
  1111. up->curregs[R6] = 0x00; /* SDLC Address */
  1112. up->curregs[R7] = 0x7E; /* SDLC Flag */
  1113. up->curregs[R9] = NV;
  1114. up->curregs[R10] = NRZ;
  1115. up->curregs[R11] = TCBR | RCBR;
  1116. baud = 9600;
  1117. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  1118. up->curregs[R12] = (brg & 0xff);
  1119. up->curregs[R13] = (brg >> 8) & 0xff;
  1120. up->curregs[R14] = BRSRC | BRENAB;
  1121. up->curregs[R15] = FIFOEN; /* Use FIFO if on ESCC */
  1122. up->curregs[R7p] = TxFIFO_LVL | RxFIFO_LVL;
  1123. if (__load_zsregs(channel, up->curregs)) {
  1124. up->flags |= SUNZILOG_FLAG_ESCC;
  1125. }
  1126. /* Only enable interrupts if an ISR handler available */
  1127. if (up->flags & SUNZILOG_FLAG_ISR_HANDLER)
  1128. up->curregs[R9] |= MIE;
  1129. write_zsreg(channel, R9, up->curregs[R9]);
  1130. }
  1131. spin_unlock_irqrestore(&up->port.lock, flags);
  1132. #ifdef CONFIG_SERIO
  1133. if (up->flags & (SUNZILOG_FLAG_CONS_KEYB |
  1134. SUNZILOG_FLAG_CONS_MOUSE))
  1135. sunzilog_register_serio(up);
  1136. #endif
  1137. }
  1138. static int zilog_irq;
  1139. static int zs_probe(struct platform_device *op)
  1140. {
  1141. static int kbm_inst, uart_inst;
  1142. int inst;
  1143. struct uart_sunzilog_port *up;
  1144. struct zilog_layout __iomem *rp;
  1145. int keyboard_mouse = 0;
  1146. int err;
  1147. if (of_find_property(op->dev.of_node, "keyboard", NULL))
  1148. keyboard_mouse = 1;
  1149. /* uarts must come before keyboards/mice */
  1150. if (keyboard_mouse)
  1151. inst = uart_chip_count + kbm_inst;
  1152. else
  1153. inst = uart_inst;
  1154. sunzilog_chip_regs[inst] = of_ioremap(&op->resource[0], 0,
  1155. sizeof(struct zilog_layout),
  1156. "zs");
  1157. if (!sunzilog_chip_regs[inst])
  1158. return -ENOMEM;
  1159. rp = sunzilog_chip_regs[inst];
  1160. if (!zilog_irq)
  1161. zilog_irq = op->archdata.irqs[0];
  1162. up = &sunzilog_port_table[inst * 2];
  1163. /* Channel A */
  1164. up[0].port.mapbase = op->resource[0].start + 0x00;
  1165. up[0].port.membase = (void __iomem *) &rp->channelA;
  1166. up[0].port.iotype = UPIO_MEM;
  1167. up[0].port.irq = op->archdata.irqs[0];
  1168. up[0].port.uartclk = ZS_CLOCK;
  1169. up[0].port.fifosize = 1;
  1170. up[0].port.ops = &sunzilog_pops;
  1171. up[0].port.type = PORT_SUNZILOG;
  1172. up[0].port.flags = 0;
  1173. up[0].port.line = (inst * 2) + 0;
  1174. up[0].port.dev = &op->dev;
  1175. up[0].flags |= SUNZILOG_FLAG_IS_CHANNEL_A;
  1176. if (keyboard_mouse)
  1177. up[0].flags |= SUNZILOG_FLAG_CONS_KEYB;
  1178. sunzilog_init_hw(&up[0]);
  1179. /* Channel B */
  1180. up[1].port.mapbase = op->resource[0].start + 0x04;
  1181. up[1].port.membase = (void __iomem *) &rp->channelB;
  1182. up[1].port.iotype = UPIO_MEM;
  1183. up[1].port.irq = op->archdata.irqs[0];
  1184. up[1].port.uartclk = ZS_CLOCK;
  1185. up[1].port.fifosize = 1;
  1186. up[1].port.ops = &sunzilog_pops;
  1187. up[1].port.type = PORT_SUNZILOG;
  1188. up[1].port.flags = 0;
  1189. up[1].port.line = (inst * 2) + 1;
  1190. up[1].port.dev = &op->dev;
  1191. up[1].flags |= 0;
  1192. if (keyboard_mouse)
  1193. up[1].flags |= SUNZILOG_FLAG_CONS_MOUSE;
  1194. sunzilog_init_hw(&up[1]);
  1195. if (!keyboard_mouse) {
  1196. if (sunserial_console_match(SUNZILOG_CONSOLE(), op->dev.of_node,
  1197. &sunzilog_reg, up[0].port.line,
  1198. false))
  1199. up->flags |= SUNZILOG_FLAG_IS_CONS;
  1200. err = uart_add_one_port(&sunzilog_reg, &up[0].port);
  1201. if (err) {
  1202. of_iounmap(&op->resource[0],
  1203. rp, sizeof(struct zilog_layout));
  1204. return err;
  1205. }
  1206. if (sunserial_console_match(SUNZILOG_CONSOLE(), op->dev.of_node,
  1207. &sunzilog_reg, up[1].port.line,
  1208. false))
  1209. up->flags |= SUNZILOG_FLAG_IS_CONS;
  1210. err = uart_add_one_port(&sunzilog_reg, &up[1].port);
  1211. if (err) {
  1212. uart_remove_one_port(&sunzilog_reg, &up[0].port);
  1213. of_iounmap(&op->resource[0],
  1214. rp, sizeof(struct zilog_layout));
  1215. return err;
  1216. }
  1217. uart_inst++;
  1218. } else {
  1219. printk(KERN_INFO "%s: Keyboard at MMIO 0x%llx (irq = %d) "
  1220. "is a %s\n",
  1221. dev_name(&op->dev),
  1222. (unsigned long long) up[0].port.mapbase,
  1223. op->archdata.irqs[0], sunzilog_type(&up[0].port));
  1224. printk(KERN_INFO "%s: Mouse at MMIO 0x%llx (irq = %d) "
  1225. "is a %s\n",
  1226. dev_name(&op->dev),
  1227. (unsigned long long) up[1].port.mapbase,
  1228. op->archdata.irqs[0], sunzilog_type(&up[1].port));
  1229. kbm_inst++;
  1230. }
  1231. platform_set_drvdata(op, &up[0]);
  1232. return 0;
  1233. }
  1234. static void zs_remove_one(struct uart_sunzilog_port *up)
  1235. {
  1236. if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up)) {
  1237. #ifdef CONFIG_SERIO
  1238. serio_unregister_port(&up->serio);
  1239. #endif
  1240. } else
  1241. uart_remove_one_port(&sunzilog_reg, &up->port);
  1242. }
  1243. static int zs_remove(struct platform_device *op)
  1244. {
  1245. struct uart_sunzilog_port *up = platform_get_drvdata(op);
  1246. struct zilog_layout __iomem *regs;
  1247. zs_remove_one(&up[0]);
  1248. zs_remove_one(&up[1]);
  1249. regs = sunzilog_chip_regs[up[0].port.line / 2];
  1250. of_iounmap(&op->resource[0], regs, sizeof(struct zilog_layout));
  1251. return 0;
  1252. }
  1253. static const struct of_device_id zs_match[] = {
  1254. {
  1255. .name = "zs",
  1256. },
  1257. {},
  1258. };
  1259. MODULE_DEVICE_TABLE(of, zs_match);
  1260. static struct platform_driver zs_driver = {
  1261. .driver = {
  1262. .name = "zs",
  1263. .owner = THIS_MODULE,
  1264. .of_match_table = zs_match,
  1265. },
  1266. .probe = zs_probe,
  1267. .remove = zs_remove,
  1268. };
  1269. static int __init sunzilog_init(void)
  1270. {
  1271. struct device_node *dp;
  1272. int err;
  1273. int num_keybms = 0;
  1274. int num_sunzilog = 0;
  1275. for_each_node_by_name(dp, "zs") {
  1276. num_sunzilog++;
  1277. if (of_find_property(dp, "keyboard", NULL))
  1278. num_keybms++;
  1279. }
  1280. if (num_sunzilog) {
  1281. err = sunzilog_alloc_tables(num_sunzilog);
  1282. if (err)
  1283. goto out;
  1284. uart_chip_count = num_sunzilog - num_keybms;
  1285. err = sunserial_register_minors(&sunzilog_reg,
  1286. uart_chip_count * 2);
  1287. if (err)
  1288. goto out_free_tables;
  1289. }
  1290. err = platform_driver_register(&zs_driver);
  1291. if (err)
  1292. goto out_unregister_uart;
  1293. if (zilog_irq) {
  1294. struct uart_sunzilog_port *up = sunzilog_irq_chain;
  1295. err = request_irq(zilog_irq, sunzilog_interrupt, IRQF_SHARED,
  1296. "zs", sunzilog_irq_chain);
  1297. if (err)
  1298. goto out_unregister_driver;
  1299. /* Enable Interrupts */
  1300. while (up) {
  1301. struct zilog_channel __iomem *channel;
  1302. /* printk (KERN_INFO "Enable IRQ for ZILOG Hardware %p\n", up); */
  1303. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  1304. up->flags |= SUNZILOG_FLAG_ISR_HANDLER;
  1305. up->curregs[R9] |= MIE;
  1306. write_zsreg(channel, R9, up->curregs[R9]);
  1307. up = up->next;
  1308. }
  1309. }
  1310. out:
  1311. return err;
  1312. out_unregister_driver:
  1313. platform_driver_unregister(&zs_driver);
  1314. out_unregister_uart:
  1315. if (num_sunzilog) {
  1316. sunserial_unregister_minors(&sunzilog_reg, num_sunzilog);
  1317. sunzilog_reg.cons = NULL;
  1318. }
  1319. out_free_tables:
  1320. sunzilog_free_tables();
  1321. goto out;
  1322. }
  1323. static void __exit sunzilog_exit(void)
  1324. {
  1325. platform_driver_unregister(&zs_driver);
  1326. if (zilog_irq) {
  1327. struct uart_sunzilog_port *up = sunzilog_irq_chain;
  1328. /* Disable Interrupts */
  1329. while (up) {
  1330. struct zilog_channel __iomem *channel;
  1331. /* printk (KERN_INFO "Disable IRQ for ZILOG Hardware %p\n", up); */
  1332. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  1333. up->flags &= ~SUNZILOG_FLAG_ISR_HANDLER;
  1334. up->curregs[R9] &= ~MIE;
  1335. write_zsreg(channel, R9, up->curregs[R9]);
  1336. up = up->next;
  1337. }
  1338. free_irq(zilog_irq, sunzilog_irq_chain);
  1339. zilog_irq = 0;
  1340. }
  1341. if (sunzilog_reg.nr) {
  1342. sunserial_unregister_minors(&sunzilog_reg, sunzilog_reg.nr);
  1343. sunzilog_free_tables();
  1344. }
  1345. }
  1346. module_init(sunzilog_init);
  1347. module_exit(sunzilog_exit);
  1348. MODULE_AUTHOR("David S. Miller");
  1349. MODULE_DESCRIPTION("Sun Zilog serial port driver");
  1350. MODULE_VERSION("2.0");
  1351. MODULE_LICENSE("GPL");