sunzilog.c 37 KB

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