sunzilog.c 41 KB

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