sunzilog.c 39 KB

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