sunzilog.c 41 KB

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