sunzilog.c 41 KB

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