sunzilog.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  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 = (struct uart_sunzilog_port *) port;
  532. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  533. unsigned char set_bits, clear_bits;
  534. set_bits = clear_bits = 0;
  535. if (mctrl & TIOCM_RTS)
  536. set_bits |= RTS;
  537. else
  538. clear_bits |= RTS;
  539. if (mctrl & TIOCM_DTR)
  540. set_bits |= DTR;
  541. else
  542. clear_bits |= DTR;
  543. /* NOTE: Not subject to 'transmitter active' rule. */
  544. up->curregs[R5] |= set_bits;
  545. up->curregs[R5] &= ~clear_bits;
  546. write_zsreg(channel, R5, up->curregs[R5]);
  547. }
  548. /* The port lock is held and interrupts are disabled. */
  549. static void sunzilog_stop_tx(struct uart_port *port)
  550. {
  551. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  552. up->flags |= SUNZILOG_FLAG_TX_STOPPED;
  553. }
  554. /* The port lock is held and interrupts are disabled. */
  555. static void sunzilog_start_tx(struct uart_port *port)
  556. {
  557. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  558. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  559. unsigned char status;
  560. up->flags |= SUNZILOG_FLAG_TX_ACTIVE;
  561. up->flags &= ~SUNZILOG_FLAG_TX_STOPPED;
  562. status = readb(&channel->control);
  563. ZSDELAY();
  564. /* TX busy? Just wait for the TX done interrupt. */
  565. if (!(status & Tx_BUF_EMP))
  566. return;
  567. /* Send the first character to jump-start the TX done
  568. * IRQ sending engine.
  569. */
  570. if (port->x_char) {
  571. writeb(port->x_char, &channel->data);
  572. ZSDELAY();
  573. ZS_WSYNC(channel);
  574. port->icount.tx++;
  575. port->x_char = 0;
  576. } else {
  577. struct circ_buf *xmit = &port->state->xmit;
  578. writeb(xmit->buf[xmit->tail], &channel->data);
  579. ZSDELAY();
  580. ZS_WSYNC(channel);
  581. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  582. port->icount.tx++;
  583. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  584. uart_write_wakeup(&up->port);
  585. }
  586. }
  587. /* The port lock is held. */
  588. static void sunzilog_stop_rx(struct uart_port *port)
  589. {
  590. struct uart_sunzilog_port *up = UART_ZILOG(port);
  591. struct zilog_channel __iomem *channel;
  592. if (ZS_IS_CONS(up))
  593. return;
  594. channel = ZILOG_CHANNEL_FROM_PORT(port);
  595. /* Disable all RX interrupts. */
  596. up->curregs[R1] &= ~RxINT_MASK;
  597. sunzilog_maybe_update_regs(up, channel);
  598. }
  599. /* The port lock is held. */
  600. static void sunzilog_enable_ms(struct uart_port *port)
  601. {
  602. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  603. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  604. unsigned char new_reg;
  605. new_reg = up->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
  606. if (new_reg != up->curregs[R15]) {
  607. up->curregs[R15] = new_reg;
  608. /* NOTE: Not subject to 'transmitter active' rule. */
  609. write_zsreg(channel, R15, up->curregs[R15] & ~WR7pEN);
  610. }
  611. }
  612. /* The port lock is not held. */
  613. static void sunzilog_break_ctl(struct uart_port *port, int break_state)
  614. {
  615. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  616. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  617. unsigned char set_bits, clear_bits, new_reg;
  618. unsigned long flags;
  619. set_bits = clear_bits = 0;
  620. if (break_state)
  621. set_bits |= SND_BRK;
  622. else
  623. clear_bits |= SND_BRK;
  624. spin_lock_irqsave(&port->lock, flags);
  625. new_reg = (up->curregs[R5] | set_bits) & ~clear_bits;
  626. if (new_reg != up->curregs[R5]) {
  627. up->curregs[R5] = new_reg;
  628. /* NOTE: Not subject to 'transmitter active' rule. */
  629. write_zsreg(channel, R5, up->curregs[R5]);
  630. }
  631. spin_unlock_irqrestore(&port->lock, flags);
  632. }
  633. static void __sunzilog_startup(struct uart_sunzilog_port *up)
  634. {
  635. struct zilog_channel __iomem *channel;
  636. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  637. up->prev_status = readb(&channel->control);
  638. /* Enable receiver and transmitter. */
  639. up->curregs[R3] |= RxENAB;
  640. up->curregs[R5] |= TxENAB;
  641. up->curregs[R1] |= EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
  642. sunzilog_maybe_update_regs(up, channel);
  643. }
  644. static int sunzilog_startup(struct uart_port *port)
  645. {
  646. struct uart_sunzilog_port *up = UART_ZILOG(port);
  647. unsigned long flags;
  648. if (ZS_IS_CONS(up))
  649. return 0;
  650. spin_lock_irqsave(&port->lock, flags);
  651. __sunzilog_startup(up);
  652. spin_unlock_irqrestore(&port->lock, flags);
  653. return 0;
  654. }
  655. /*
  656. * The test for ZS_IS_CONS is explained by the following e-mail:
  657. *****
  658. * From: Russell King <rmk@arm.linux.org.uk>
  659. * Date: Sun, 8 Dec 2002 10:18:38 +0000
  660. *
  661. * On Sun, Dec 08, 2002 at 02:43:36AM -0500, Pete Zaitcev wrote:
  662. * > I boot my 2.5 boxes using "console=ttyS0,9600" argument,
  663. * > and I noticed that something is not right with reference
  664. * > counting in this case. It seems that when the console
  665. * > is open by kernel initially, this is not accounted
  666. * > as an open, and uart_startup is not called.
  667. *
  668. * That is correct. We are unable to call uart_startup when the serial
  669. * console is initialised because it may need to allocate memory (as
  670. * request_irq does) and the memory allocators may not have been
  671. * initialised.
  672. *
  673. * 1. initialise the port into a state where it can send characters in the
  674. * console write method.
  675. *
  676. * 2. don't do the actual hardware shutdown in your shutdown() method (but
  677. * do the normal software shutdown - ie, free irqs etc)
  678. *****
  679. */
  680. static void sunzilog_shutdown(struct uart_port *port)
  681. {
  682. struct uart_sunzilog_port *up = UART_ZILOG(port);
  683. struct zilog_channel __iomem *channel;
  684. unsigned long flags;
  685. if (ZS_IS_CONS(up))
  686. return;
  687. spin_lock_irqsave(&port->lock, flags);
  688. channel = ZILOG_CHANNEL_FROM_PORT(port);
  689. /* Disable receiver and transmitter. */
  690. up->curregs[R3] &= ~RxENAB;
  691. up->curregs[R5] &= ~TxENAB;
  692. /* Disable all interrupts and BRK assertion. */
  693. up->curregs[R1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
  694. up->curregs[R5] &= ~SND_BRK;
  695. sunzilog_maybe_update_regs(up, channel);
  696. spin_unlock_irqrestore(&port->lock, flags);
  697. }
  698. /* Shared by TTY driver and serial console setup. The port lock is held
  699. * and local interrupts are disabled.
  700. */
  701. static void
  702. sunzilog_convert_to_zs(struct uart_sunzilog_port *up, unsigned int cflag,
  703. unsigned int iflag, int brg)
  704. {
  705. up->curregs[R10] = NRZ;
  706. up->curregs[R11] = TCBR | RCBR;
  707. /* Program BAUD and clock source. */
  708. up->curregs[R4] &= ~XCLK_MASK;
  709. up->curregs[R4] |= X16CLK;
  710. up->curregs[R12] = brg & 0xff;
  711. up->curregs[R13] = (brg >> 8) & 0xff;
  712. up->curregs[R14] = BRSRC | BRENAB;
  713. /* Character size, stop bits, and parity. */
  714. up->curregs[R3] &= ~RxN_MASK;
  715. up->curregs[R5] &= ~TxN_MASK;
  716. switch (cflag & CSIZE) {
  717. case CS5:
  718. up->curregs[R3] |= Rx5;
  719. up->curregs[R5] |= Tx5;
  720. up->parity_mask = 0x1f;
  721. break;
  722. case CS6:
  723. up->curregs[R3] |= Rx6;
  724. up->curregs[R5] |= Tx6;
  725. up->parity_mask = 0x3f;
  726. break;
  727. case CS7:
  728. up->curregs[R3] |= Rx7;
  729. up->curregs[R5] |= Tx7;
  730. up->parity_mask = 0x7f;
  731. break;
  732. case CS8:
  733. default:
  734. up->curregs[R3] |= Rx8;
  735. up->curregs[R5] |= Tx8;
  736. up->parity_mask = 0xff;
  737. break;
  738. };
  739. up->curregs[R4] &= ~0x0c;
  740. if (cflag & CSTOPB)
  741. up->curregs[R4] |= SB2;
  742. else
  743. up->curregs[R4] |= SB1;
  744. if (cflag & PARENB)
  745. up->curregs[R4] |= PAR_ENAB;
  746. else
  747. up->curregs[R4] &= ~PAR_ENAB;
  748. if (!(cflag & PARODD))
  749. up->curregs[R4] |= PAR_EVEN;
  750. else
  751. up->curregs[R4] &= ~PAR_EVEN;
  752. up->port.read_status_mask = Rx_OVR;
  753. if (iflag & INPCK)
  754. up->port.read_status_mask |= CRC_ERR | PAR_ERR;
  755. if (iflag & (BRKINT | PARMRK))
  756. up->port.read_status_mask |= BRK_ABRT;
  757. up->port.ignore_status_mask = 0;
  758. if (iflag & IGNPAR)
  759. up->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
  760. if (iflag & IGNBRK) {
  761. up->port.ignore_status_mask |= BRK_ABRT;
  762. if (iflag & IGNPAR)
  763. up->port.ignore_status_mask |= Rx_OVR;
  764. }
  765. if ((cflag & CREAD) == 0)
  766. up->port.ignore_status_mask = 0xff;
  767. }
  768. /* The port lock is not held. */
  769. static void
  770. sunzilog_set_termios(struct uart_port *port, struct ktermios *termios,
  771. struct ktermios *old)
  772. {
  773. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  774. unsigned long flags;
  775. int baud, brg;
  776. baud = uart_get_baud_rate(port, termios, old, 1200, 76800);
  777. spin_lock_irqsave(&up->port.lock, flags);
  778. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  779. sunzilog_convert_to_zs(up, termios->c_cflag, termios->c_iflag, brg);
  780. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  781. up->flags |= SUNZILOG_FLAG_MODEM_STATUS;
  782. else
  783. up->flags &= ~SUNZILOG_FLAG_MODEM_STATUS;
  784. up->cflag = termios->c_cflag;
  785. sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(port));
  786. uart_update_timeout(port, termios->c_cflag, baud);
  787. spin_unlock_irqrestore(&up->port.lock, flags);
  788. }
  789. static const char *sunzilog_type(struct uart_port *port)
  790. {
  791. struct uart_sunzilog_port *up = UART_ZILOG(port);
  792. return (up->flags & SUNZILOG_FLAG_ESCC) ? "zs (ESCC)" : "zs";
  793. }
  794. /* We do not request/release mappings of the registers here, this
  795. * happens at early serial probe time.
  796. */
  797. static void sunzilog_release_port(struct uart_port *port)
  798. {
  799. }
  800. static int sunzilog_request_port(struct uart_port *port)
  801. {
  802. return 0;
  803. }
  804. /* These do not need to do anything interesting either. */
  805. static void sunzilog_config_port(struct uart_port *port, int flags)
  806. {
  807. }
  808. /* We do not support letting the user mess with the divisor, IRQ, etc. */
  809. static int sunzilog_verify_port(struct uart_port *port, struct serial_struct *ser)
  810. {
  811. return -EINVAL;
  812. }
  813. #ifdef CONFIG_CONSOLE_POLL
  814. static int sunzilog_get_poll_char(struct uart_port *port)
  815. {
  816. unsigned char ch, r1;
  817. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *) port;
  818. struct zilog_channel __iomem *channel
  819. = ZILOG_CHANNEL_FROM_PORT(&up->port);
  820. r1 = read_zsreg(channel, R1);
  821. if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
  822. writeb(ERR_RES, &channel->control);
  823. ZSDELAY();
  824. ZS_WSYNC(channel);
  825. }
  826. ch = readb(&channel->control);
  827. ZSDELAY();
  828. /* This funny hack depends upon BRK_ABRT not interfering
  829. * with the other bits we care about in R1.
  830. */
  831. if (ch & BRK_ABRT)
  832. r1 |= BRK_ABRT;
  833. if (!(ch & Rx_CH_AV))
  834. return NO_POLL_CHAR;
  835. ch = readb(&channel->data);
  836. ZSDELAY();
  837. ch &= up->parity_mask;
  838. return ch;
  839. }
  840. static void sunzilog_put_poll_char(struct uart_port *port,
  841. unsigned char ch)
  842. {
  843. struct uart_sunzilog_port *up = (struct uart_sunzilog_port *)port;
  844. sunzilog_putchar(&up->port, ch);
  845. }
  846. #endif /* CONFIG_CONSOLE_POLL */
  847. static struct uart_ops sunzilog_pops = {
  848. .tx_empty = sunzilog_tx_empty,
  849. .set_mctrl = sunzilog_set_mctrl,
  850. .get_mctrl = sunzilog_get_mctrl,
  851. .stop_tx = sunzilog_stop_tx,
  852. .start_tx = sunzilog_start_tx,
  853. .stop_rx = sunzilog_stop_rx,
  854. .enable_ms = sunzilog_enable_ms,
  855. .break_ctl = sunzilog_break_ctl,
  856. .startup = sunzilog_startup,
  857. .shutdown = sunzilog_shutdown,
  858. .set_termios = sunzilog_set_termios,
  859. .type = sunzilog_type,
  860. .release_port = sunzilog_release_port,
  861. .request_port = sunzilog_request_port,
  862. .config_port = sunzilog_config_port,
  863. .verify_port = sunzilog_verify_port,
  864. #ifdef CONFIG_CONSOLE_POLL
  865. .poll_get_char = sunzilog_get_poll_char,
  866. .poll_put_char = sunzilog_put_poll_char,
  867. #endif
  868. };
  869. static int uart_chip_count;
  870. static struct uart_sunzilog_port *sunzilog_port_table;
  871. static struct zilog_layout __iomem **sunzilog_chip_regs;
  872. static struct uart_sunzilog_port *sunzilog_irq_chain;
  873. static struct uart_driver sunzilog_reg = {
  874. .owner = THIS_MODULE,
  875. .driver_name = "sunzilog",
  876. .dev_name = "ttyS",
  877. .major = TTY_MAJOR,
  878. };
  879. static int __init sunzilog_alloc_tables(int num_sunzilog)
  880. {
  881. struct uart_sunzilog_port *up;
  882. unsigned long size;
  883. int num_channels = num_sunzilog * 2;
  884. int i;
  885. size = num_channels * sizeof(struct uart_sunzilog_port);
  886. sunzilog_port_table = kzalloc(size, GFP_KERNEL);
  887. if (!sunzilog_port_table)
  888. return -ENOMEM;
  889. for (i = 0; i < num_channels; i++) {
  890. up = &sunzilog_port_table[i];
  891. spin_lock_init(&up->port.lock);
  892. if (i == 0)
  893. sunzilog_irq_chain = up;
  894. if (i < num_channels - 1)
  895. up->next = up + 1;
  896. else
  897. up->next = NULL;
  898. }
  899. size = num_sunzilog * sizeof(struct zilog_layout __iomem *);
  900. sunzilog_chip_regs = kzalloc(size, GFP_KERNEL);
  901. if (!sunzilog_chip_regs) {
  902. kfree(sunzilog_port_table);
  903. sunzilog_irq_chain = NULL;
  904. return -ENOMEM;
  905. }
  906. return 0;
  907. }
  908. static void sunzilog_free_tables(void)
  909. {
  910. kfree(sunzilog_port_table);
  911. sunzilog_irq_chain = NULL;
  912. kfree(sunzilog_chip_regs);
  913. }
  914. #define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
  915. static void sunzilog_putchar(struct uart_port *port, int ch)
  916. {
  917. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(port);
  918. int loops = ZS_PUT_CHAR_MAX_DELAY;
  919. /* This is a timed polling loop so do not switch the explicit
  920. * udelay with ZSDELAY as that is a NOP on some platforms. -DaveM
  921. */
  922. do {
  923. unsigned char val = readb(&channel->control);
  924. if (val & Tx_BUF_EMP) {
  925. ZSDELAY();
  926. break;
  927. }
  928. udelay(5);
  929. } while (--loops);
  930. writeb(ch, &channel->data);
  931. ZSDELAY();
  932. ZS_WSYNC(channel);
  933. }
  934. #ifdef CONFIG_SERIO
  935. static DEFINE_SPINLOCK(sunzilog_serio_lock);
  936. static int sunzilog_serio_write(struct serio *serio, unsigned char ch)
  937. {
  938. struct uart_sunzilog_port *up = serio->port_data;
  939. unsigned long flags;
  940. spin_lock_irqsave(&sunzilog_serio_lock, flags);
  941. sunzilog_putchar(&up->port, ch);
  942. spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
  943. return 0;
  944. }
  945. static int sunzilog_serio_open(struct serio *serio)
  946. {
  947. struct uart_sunzilog_port *up = serio->port_data;
  948. unsigned long flags;
  949. int ret;
  950. spin_lock_irqsave(&sunzilog_serio_lock, flags);
  951. if (!up->serio_open) {
  952. up->serio_open = 1;
  953. ret = 0;
  954. } else
  955. ret = -EBUSY;
  956. spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
  957. return ret;
  958. }
  959. static void sunzilog_serio_close(struct serio *serio)
  960. {
  961. struct uart_sunzilog_port *up = serio->port_data;
  962. unsigned long flags;
  963. spin_lock_irqsave(&sunzilog_serio_lock, flags);
  964. up->serio_open = 0;
  965. spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
  966. }
  967. #endif /* CONFIG_SERIO */
  968. #ifdef CONFIG_SERIAL_SUNZILOG_CONSOLE
  969. static void
  970. sunzilog_console_write(struct console *con, const char *s, unsigned int count)
  971. {
  972. struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
  973. unsigned long flags;
  974. int locked = 1;
  975. local_irq_save(flags);
  976. if (up->port.sysrq) {
  977. locked = 0;
  978. } else if (oops_in_progress) {
  979. locked = spin_trylock(&up->port.lock);
  980. } else
  981. spin_lock(&up->port.lock);
  982. uart_console_write(&up->port, s, count, sunzilog_putchar);
  983. udelay(2);
  984. if (locked)
  985. spin_unlock(&up->port.lock);
  986. local_irq_restore(flags);
  987. }
  988. static int __init sunzilog_console_setup(struct console *con, char *options)
  989. {
  990. struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
  991. unsigned long flags;
  992. int baud, brg;
  993. if (up->port.type != PORT_SUNZILOG)
  994. return -1;
  995. printk(KERN_INFO "Console: ttyS%d (SunZilog zs%d)\n",
  996. (sunzilog_reg.minor - 64) + con->index, con->index);
  997. /* Get firmware console settings. */
  998. sunserial_console_termios(con, up->port.dev->of_node);
  999. /* Firmware console speed is limited to 150-->38400 baud so
  1000. * this hackish cflag thing is OK.
  1001. */
  1002. switch (con->cflag & CBAUD) {
  1003. case B150: baud = 150; break;
  1004. case B300: baud = 300; break;
  1005. case B600: baud = 600; break;
  1006. case B1200: baud = 1200; break;
  1007. case B2400: baud = 2400; break;
  1008. case B4800: baud = 4800; break;
  1009. default: case B9600: baud = 9600; break;
  1010. case B19200: baud = 19200; break;
  1011. case B38400: baud = 38400; break;
  1012. };
  1013. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  1014. spin_lock_irqsave(&up->port.lock, flags);
  1015. up->curregs[R15] |= BRKIE;
  1016. sunzilog_convert_to_zs(up, con->cflag, 0, brg);
  1017. sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
  1018. __sunzilog_startup(up);
  1019. spin_unlock_irqrestore(&up->port.lock, flags);
  1020. return 0;
  1021. }
  1022. static struct console sunzilog_console_ops = {
  1023. .name = "ttyS",
  1024. .write = sunzilog_console_write,
  1025. .device = uart_console_device,
  1026. .setup = sunzilog_console_setup,
  1027. .flags = CON_PRINTBUFFER,
  1028. .index = -1,
  1029. .data = &sunzilog_reg,
  1030. };
  1031. static inline struct console *SUNZILOG_CONSOLE(void)
  1032. {
  1033. return &sunzilog_console_ops;
  1034. }
  1035. #else
  1036. #define SUNZILOG_CONSOLE() (NULL)
  1037. #endif
  1038. static void sunzilog_init_kbdms(struct uart_sunzilog_port *up)
  1039. {
  1040. int baud, brg;
  1041. if (up->flags & SUNZILOG_FLAG_CONS_KEYB) {
  1042. up->cflag = B1200 | CS8 | CLOCAL | CREAD;
  1043. baud = 1200;
  1044. } else {
  1045. up->cflag = B4800 | CS8 | CLOCAL | CREAD;
  1046. baud = 4800;
  1047. }
  1048. up->curregs[R15] |= BRKIE;
  1049. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  1050. sunzilog_convert_to_zs(up, up->cflag, 0, brg);
  1051. sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
  1052. __sunzilog_startup(up);
  1053. }
  1054. #ifdef CONFIG_SERIO
  1055. static void sunzilog_register_serio(struct uart_sunzilog_port *up)
  1056. {
  1057. struct serio *serio = &up->serio;
  1058. serio->port_data = up;
  1059. serio->id.type = SERIO_RS232;
  1060. if (up->flags & SUNZILOG_FLAG_CONS_KEYB) {
  1061. serio->id.proto = SERIO_SUNKBD;
  1062. strlcpy(serio->name, "zskbd", sizeof(serio->name));
  1063. } else {
  1064. serio->id.proto = SERIO_SUN;
  1065. serio->id.extra = 1;
  1066. strlcpy(serio->name, "zsms", sizeof(serio->name));
  1067. }
  1068. strlcpy(serio->phys,
  1069. ((up->flags & SUNZILOG_FLAG_CONS_KEYB) ?
  1070. "zs/serio0" : "zs/serio1"),
  1071. sizeof(serio->phys));
  1072. serio->write = sunzilog_serio_write;
  1073. serio->open = sunzilog_serio_open;
  1074. serio->close = sunzilog_serio_close;
  1075. serio->dev.parent = up->port.dev;
  1076. serio_register_port(serio);
  1077. }
  1078. #endif
  1079. static void sunzilog_init_hw(struct uart_sunzilog_port *up)
  1080. {
  1081. struct zilog_channel __iomem *channel;
  1082. unsigned long flags;
  1083. int baud, brg;
  1084. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  1085. spin_lock_irqsave(&up->port.lock, flags);
  1086. if (ZS_IS_CHANNEL_A(up)) {
  1087. write_zsreg(channel, R9, FHWRES);
  1088. ZSDELAY_LONG();
  1089. (void) read_zsreg(channel, R0);
  1090. }
  1091. if (up->flags & (SUNZILOG_FLAG_CONS_KEYB |
  1092. SUNZILOG_FLAG_CONS_MOUSE)) {
  1093. up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
  1094. up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
  1095. up->curregs[R3] = RxENAB | Rx8;
  1096. up->curregs[R5] = TxENAB | Tx8;
  1097. up->curregs[R6] = 0x00; /* SDLC Address */
  1098. up->curregs[R7] = 0x7E; /* SDLC Flag */
  1099. up->curregs[R9] = NV;
  1100. up->curregs[R7p] = 0x00;
  1101. sunzilog_init_kbdms(up);
  1102. /* Only enable interrupts if an ISR handler available */
  1103. if (up->flags & SUNZILOG_FLAG_ISR_HANDLER)
  1104. up->curregs[R9] |= MIE;
  1105. write_zsreg(channel, R9, up->curregs[R9]);
  1106. } else {
  1107. /* Normal serial TTY. */
  1108. up->parity_mask = 0xff;
  1109. up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
  1110. up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
  1111. up->curregs[R3] = RxENAB | Rx8;
  1112. up->curregs[R5] = TxENAB | Tx8;
  1113. up->curregs[R6] = 0x00; /* SDLC Address */
  1114. up->curregs[R7] = 0x7E; /* SDLC Flag */
  1115. up->curregs[R9] = NV;
  1116. up->curregs[R10] = NRZ;
  1117. up->curregs[R11] = TCBR | RCBR;
  1118. baud = 9600;
  1119. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  1120. up->curregs[R12] = (brg & 0xff);
  1121. up->curregs[R13] = (brg >> 8) & 0xff;
  1122. up->curregs[R14] = BRSRC | BRENAB;
  1123. up->curregs[R15] = FIFOEN; /* Use FIFO if on ESCC */
  1124. up->curregs[R7p] = TxFIFO_LVL | RxFIFO_LVL;
  1125. if (__load_zsregs(channel, up->curregs)) {
  1126. up->flags |= SUNZILOG_FLAG_ESCC;
  1127. }
  1128. /* Only enable interrupts if an ISR handler available */
  1129. if (up->flags & SUNZILOG_FLAG_ISR_HANDLER)
  1130. up->curregs[R9] |= MIE;
  1131. write_zsreg(channel, R9, up->curregs[R9]);
  1132. }
  1133. spin_unlock_irqrestore(&up->port.lock, flags);
  1134. #ifdef CONFIG_SERIO
  1135. if (up->flags & (SUNZILOG_FLAG_CONS_KEYB |
  1136. SUNZILOG_FLAG_CONS_MOUSE))
  1137. sunzilog_register_serio(up);
  1138. #endif
  1139. }
  1140. static int zilog_irq;
  1141. static int zs_probe(struct platform_device *op)
  1142. {
  1143. static int kbm_inst, uart_inst;
  1144. int inst;
  1145. struct uart_sunzilog_port *up;
  1146. struct zilog_layout __iomem *rp;
  1147. int keyboard_mouse = 0;
  1148. int err;
  1149. if (of_find_property(op->dev.of_node, "keyboard", NULL))
  1150. keyboard_mouse = 1;
  1151. /* uarts must come before keyboards/mice */
  1152. if (keyboard_mouse)
  1153. inst = uart_chip_count + kbm_inst;
  1154. else
  1155. inst = uart_inst;
  1156. sunzilog_chip_regs[inst] = of_ioremap(&op->resource[0], 0,
  1157. sizeof(struct zilog_layout),
  1158. "zs");
  1159. if (!sunzilog_chip_regs[inst])
  1160. return -ENOMEM;
  1161. rp = sunzilog_chip_regs[inst];
  1162. if (!zilog_irq)
  1163. zilog_irq = op->archdata.irqs[0];
  1164. up = &sunzilog_port_table[inst * 2];
  1165. /* Channel A */
  1166. up[0].port.mapbase = op->resource[0].start + 0x00;
  1167. up[0].port.membase = (void __iomem *) &rp->channelA;
  1168. up[0].port.iotype = UPIO_MEM;
  1169. up[0].port.irq = op->archdata.irqs[0];
  1170. up[0].port.uartclk = ZS_CLOCK;
  1171. up[0].port.fifosize = 1;
  1172. up[0].port.ops = &sunzilog_pops;
  1173. up[0].port.type = PORT_SUNZILOG;
  1174. up[0].port.flags = 0;
  1175. up[0].port.line = (inst * 2) + 0;
  1176. up[0].port.dev = &op->dev;
  1177. up[0].flags |= SUNZILOG_FLAG_IS_CHANNEL_A;
  1178. if (keyboard_mouse)
  1179. up[0].flags |= SUNZILOG_FLAG_CONS_KEYB;
  1180. sunzilog_init_hw(&up[0]);
  1181. /* Channel B */
  1182. up[1].port.mapbase = op->resource[0].start + 0x04;
  1183. up[1].port.membase = (void __iomem *) &rp->channelB;
  1184. up[1].port.iotype = UPIO_MEM;
  1185. up[1].port.irq = op->archdata.irqs[0];
  1186. up[1].port.uartclk = ZS_CLOCK;
  1187. up[1].port.fifosize = 1;
  1188. up[1].port.ops = &sunzilog_pops;
  1189. up[1].port.type = PORT_SUNZILOG;
  1190. up[1].port.flags = 0;
  1191. up[1].port.line = (inst * 2) + 1;
  1192. up[1].port.dev = &op->dev;
  1193. up[1].flags |= 0;
  1194. if (keyboard_mouse)
  1195. up[1].flags |= SUNZILOG_FLAG_CONS_MOUSE;
  1196. sunzilog_init_hw(&up[1]);
  1197. if (!keyboard_mouse) {
  1198. if (sunserial_console_match(SUNZILOG_CONSOLE(), op->dev.of_node,
  1199. &sunzilog_reg, up[0].port.line,
  1200. false))
  1201. up->flags |= SUNZILOG_FLAG_IS_CONS;
  1202. err = uart_add_one_port(&sunzilog_reg, &up[0].port);
  1203. if (err) {
  1204. of_iounmap(&op->resource[0],
  1205. rp, sizeof(struct zilog_layout));
  1206. return err;
  1207. }
  1208. if (sunserial_console_match(SUNZILOG_CONSOLE(), op->dev.of_node,
  1209. &sunzilog_reg, up[1].port.line,
  1210. false))
  1211. up->flags |= SUNZILOG_FLAG_IS_CONS;
  1212. err = uart_add_one_port(&sunzilog_reg, &up[1].port);
  1213. if (err) {
  1214. uart_remove_one_port(&sunzilog_reg, &up[0].port);
  1215. of_iounmap(&op->resource[0],
  1216. rp, sizeof(struct zilog_layout));
  1217. return err;
  1218. }
  1219. uart_inst++;
  1220. } else {
  1221. printk(KERN_INFO "%s: Keyboard at MMIO 0x%llx (irq = %d) "
  1222. "is a %s\n",
  1223. dev_name(&op->dev),
  1224. (unsigned long long) up[0].port.mapbase,
  1225. op->archdata.irqs[0], sunzilog_type(&up[0].port));
  1226. printk(KERN_INFO "%s: Mouse at MMIO 0x%llx (irq = %d) "
  1227. "is a %s\n",
  1228. dev_name(&op->dev),
  1229. (unsigned long long) up[1].port.mapbase,
  1230. op->archdata.irqs[0], sunzilog_type(&up[1].port));
  1231. kbm_inst++;
  1232. }
  1233. platform_set_drvdata(op, &up[0]);
  1234. return 0;
  1235. }
  1236. static void zs_remove_one(struct uart_sunzilog_port *up)
  1237. {
  1238. if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up)) {
  1239. #ifdef CONFIG_SERIO
  1240. serio_unregister_port(&up->serio);
  1241. #endif
  1242. } else
  1243. uart_remove_one_port(&sunzilog_reg, &up->port);
  1244. }
  1245. static int zs_remove(struct platform_device *op)
  1246. {
  1247. struct uart_sunzilog_port *up = platform_get_drvdata(op);
  1248. struct zilog_layout __iomem *regs;
  1249. zs_remove_one(&up[0]);
  1250. zs_remove_one(&up[1]);
  1251. regs = sunzilog_chip_regs[up[0].port.line / 2];
  1252. of_iounmap(&op->resource[0], regs, sizeof(struct zilog_layout));
  1253. return 0;
  1254. }
  1255. static const struct of_device_id zs_match[] = {
  1256. {
  1257. .name = "zs",
  1258. },
  1259. {},
  1260. };
  1261. MODULE_DEVICE_TABLE(of, zs_match);
  1262. static struct platform_driver zs_driver = {
  1263. .driver = {
  1264. .name = "zs",
  1265. .owner = THIS_MODULE,
  1266. .of_match_table = zs_match,
  1267. },
  1268. .probe = zs_probe,
  1269. .remove = zs_remove,
  1270. };
  1271. static int __init sunzilog_init(void)
  1272. {
  1273. struct device_node *dp;
  1274. int err;
  1275. int num_keybms = 0;
  1276. int num_sunzilog = 0;
  1277. for_each_node_by_name(dp, "zs") {
  1278. num_sunzilog++;
  1279. if (of_find_property(dp, "keyboard", NULL))
  1280. num_keybms++;
  1281. }
  1282. if (num_sunzilog) {
  1283. err = sunzilog_alloc_tables(num_sunzilog);
  1284. if (err)
  1285. goto out;
  1286. uart_chip_count = num_sunzilog - num_keybms;
  1287. err = sunserial_register_minors(&sunzilog_reg,
  1288. uart_chip_count * 2);
  1289. if (err)
  1290. goto out_free_tables;
  1291. }
  1292. err = platform_driver_register(&zs_driver);
  1293. if (err)
  1294. goto out_unregister_uart;
  1295. if (zilog_irq) {
  1296. struct uart_sunzilog_port *up = sunzilog_irq_chain;
  1297. err = request_irq(zilog_irq, sunzilog_interrupt, IRQF_SHARED,
  1298. "zs", sunzilog_irq_chain);
  1299. if (err)
  1300. goto out_unregister_driver;
  1301. /* Enable Interrupts */
  1302. while (up) {
  1303. struct zilog_channel __iomem *channel;
  1304. /* printk (KERN_INFO "Enable IRQ for ZILOG Hardware %p\n", up); */
  1305. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  1306. up->flags |= SUNZILOG_FLAG_ISR_HANDLER;
  1307. up->curregs[R9] |= MIE;
  1308. write_zsreg(channel, R9, up->curregs[R9]);
  1309. up = up->next;
  1310. }
  1311. }
  1312. out:
  1313. return err;
  1314. out_unregister_driver:
  1315. platform_driver_unregister(&zs_driver);
  1316. out_unregister_uart:
  1317. if (num_sunzilog) {
  1318. sunserial_unregister_minors(&sunzilog_reg, num_sunzilog);
  1319. sunzilog_reg.cons = NULL;
  1320. }
  1321. out_free_tables:
  1322. sunzilog_free_tables();
  1323. goto out;
  1324. }
  1325. static void __exit sunzilog_exit(void)
  1326. {
  1327. platform_driver_unregister(&zs_driver);
  1328. if (zilog_irq) {
  1329. struct uart_sunzilog_port *up = sunzilog_irq_chain;
  1330. /* Disable Interrupts */
  1331. while (up) {
  1332. struct zilog_channel __iomem *channel;
  1333. /* printk (KERN_INFO "Disable IRQ for ZILOG Hardware %p\n", up); */
  1334. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  1335. up->flags &= ~SUNZILOG_FLAG_ISR_HANDLER;
  1336. up->curregs[R9] &= ~MIE;
  1337. write_zsreg(channel, R9, up->curregs[R9]);
  1338. up = up->next;
  1339. }
  1340. free_irq(zilog_irq, sunzilog_irq_chain);
  1341. zilog_irq = 0;
  1342. }
  1343. if (sunzilog_reg.nr) {
  1344. sunserial_unregister_minors(&sunzilog_reg, sunzilog_reg.nr);
  1345. sunzilog_free_tables();
  1346. }
  1347. }
  1348. module_init(sunzilog_init);
  1349. module_exit(sunzilog_exit);
  1350. MODULE_AUTHOR("David S. Miller");
  1351. MODULE_DESCRIPTION("Sun Zilog serial port driver");
  1352. MODULE_VERSION("2.0");
  1353. MODULE_LICENSE("GPL");