sunzilog.c 37 KB

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