sunzilog.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  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))
  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. dev_set_drvdata(&op->dev, &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 = dev_get_drvdata(&op->dev);
  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. dev_set_drvdata(&op->dev, NULL);
  1254. return 0;
  1255. }
  1256. static const struct of_device_id zs_match[] = {
  1257. {
  1258. .name = "zs",
  1259. },
  1260. {},
  1261. };
  1262. MODULE_DEVICE_TABLE(of, zs_match);
  1263. static struct platform_driver zs_driver = {
  1264. .driver = {
  1265. .name = "zs",
  1266. .owner = THIS_MODULE,
  1267. .of_match_table = zs_match,
  1268. },
  1269. .probe = zs_probe,
  1270. .remove = zs_remove,
  1271. };
  1272. static int __init sunzilog_init(void)
  1273. {
  1274. struct device_node *dp;
  1275. int err;
  1276. int num_keybms = 0;
  1277. int num_sunzilog = 0;
  1278. for_each_node_by_name(dp, "zs") {
  1279. num_sunzilog++;
  1280. if (of_find_property(dp, "keyboard", NULL))
  1281. num_keybms++;
  1282. }
  1283. if (num_sunzilog) {
  1284. err = sunzilog_alloc_tables(num_sunzilog);
  1285. if (err)
  1286. goto out;
  1287. uart_chip_count = num_sunzilog - num_keybms;
  1288. err = sunserial_register_minors(&sunzilog_reg,
  1289. uart_chip_count * 2);
  1290. if (err)
  1291. goto out_free_tables;
  1292. }
  1293. err = platform_driver_register(&zs_driver);
  1294. if (err)
  1295. goto out_unregister_uart;
  1296. if (zilog_irq) {
  1297. struct uart_sunzilog_port *up = sunzilog_irq_chain;
  1298. err = request_irq(zilog_irq, sunzilog_interrupt, IRQF_SHARED,
  1299. "zs", sunzilog_irq_chain);
  1300. if (err)
  1301. goto out_unregister_driver;
  1302. /* Enable Interrupts */
  1303. while (up) {
  1304. struct zilog_channel __iomem *channel;
  1305. /* printk (KERN_INFO "Enable IRQ for ZILOG Hardware %p\n", up); */
  1306. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  1307. up->flags |= SUNZILOG_FLAG_ISR_HANDLER;
  1308. up->curregs[R9] |= MIE;
  1309. write_zsreg(channel, R9, up->curregs[R9]);
  1310. up = up->next;
  1311. }
  1312. }
  1313. out:
  1314. return err;
  1315. out_unregister_driver:
  1316. platform_driver_unregister(&zs_driver);
  1317. out_unregister_uart:
  1318. if (num_sunzilog) {
  1319. sunserial_unregister_minors(&sunzilog_reg, num_sunzilog);
  1320. sunzilog_reg.cons = NULL;
  1321. }
  1322. out_free_tables:
  1323. sunzilog_free_tables();
  1324. goto out;
  1325. }
  1326. static void __exit sunzilog_exit(void)
  1327. {
  1328. platform_driver_unregister(&zs_driver);
  1329. if (zilog_irq) {
  1330. struct uart_sunzilog_port *up = sunzilog_irq_chain;
  1331. /* Disable Interrupts */
  1332. while (up) {
  1333. struct zilog_channel __iomem *channel;
  1334. /* printk (KERN_INFO "Disable IRQ for ZILOG Hardware %p\n", up); */
  1335. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  1336. up->flags &= ~SUNZILOG_FLAG_ISR_HANDLER;
  1337. up->curregs[R9] &= ~MIE;
  1338. write_zsreg(channel, R9, up->curregs[R9]);
  1339. up = up->next;
  1340. }
  1341. free_irq(zilog_irq, sunzilog_irq_chain);
  1342. zilog_irq = 0;
  1343. }
  1344. if (sunzilog_reg.nr) {
  1345. sunserial_unregister_minors(&sunzilog_reg, sunzilog_reg.nr);
  1346. sunzilog_free_tables();
  1347. }
  1348. }
  1349. module_init(sunzilog_init);
  1350. module_exit(sunzilog_exit);
  1351. MODULE_AUTHOR("David S. Miller");
  1352. MODULE_DESCRIPTION("Sun Zilog serial port driver");
  1353. MODULE_VERSION("2.0");
  1354. MODULE_LICENSE("GPL");