sunzilog.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  1. /*
  2. * sunzilog.c
  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 David S. Miller (davem@redhat.com)
  14. */
  15. #include <linux/config.h>
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/errno.h>
  20. #include <linux/delay.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/major.h>
  24. #include <linux/string.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/ioport.h>
  27. #include <linux/slab.h>
  28. #include <linux/circ_buf.h>
  29. #include <linux/serial.h>
  30. #include <linux/sysrq.h>
  31. #include <linux/console.h>
  32. #include <linux/spinlock.h>
  33. #ifdef CONFIG_SERIO
  34. #include <linux/serio.h>
  35. #endif
  36. #include <linux/init.h>
  37. #include <asm/io.h>
  38. #include <asm/irq.h>
  39. #ifdef CONFIG_SPARC64
  40. #include <asm/fhc.h>
  41. #endif
  42. #include <asm/sbus.h>
  43. #if defined(CONFIG_SERIAL_SUNZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  44. #define SUPPORT_SYSRQ
  45. #endif
  46. #include <linux/serial_core.h>
  47. #include "suncore.h"
  48. #include "sunzilog.h"
  49. /* On 32-bit sparcs we need to delay after register accesses
  50. * to accommodate sun4 systems, but we do not need to flush writes.
  51. * On 64-bit sparc we only need to flush single writes to ensure
  52. * completion.
  53. */
  54. #ifndef CONFIG_SPARC64
  55. #define ZSDELAY() udelay(5)
  56. #define ZSDELAY_LONG() udelay(20)
  57. #define ZS_WSYNC(channel) do { } while (0)
  58. #else
  59. #define ZSDELAY()
  60. #define ZSDELAY_LONG()
  61. #define ZS_WSYNC(__channel) \
  62. sbus_readb(&((__channel)->control))
  63. #endif
  64. static int num_sunzilog;
  65. #define NUM_SUNZILOG num_sunzilog
  66. #define NUM_CHANNELS (NUM_SUNZILOG * 2)
  67. #define KEYBOARD_LINE 0x2
  68. #define MOUSE_LINE 0x3
  69. #define ZS_CLOCK 4915200 /* Zilog input clock rate. */
  70. #define ZS_CLOCK_DIVISOR 16 /* Divisor this driver uses. */
  71. /*
  72. * We wrap our port structure around the generic uart_port.
  73. */
  74. struct uart_sunzilog_port {
  75. struct uart_port port;
  76. /* IRQ servicing chain. */
  77. struct uart_sunzilog_port *next;
  78. /* Current values of Zilog write registers. */
  79. unsigned char curregs[NUM_ZSREGS];
  80. unsigned int flags;
  81. #define SUNZILOG_FLAG_CONS_KEYB 0x00000001
  82. #define SUNZILOG_FLAG_CONS_MOUSE 0x00000002
  83. #define SUNZILOG_FLAG_IS_CONS 0x00000004
  84. #define SUNZILOG_FLAG_IS_KGDB 0x00000008
  85. #define SUNZILOG_FLAG_MODEM_STATUS 0x00000010
  86. #define SUNZILOG_FLAG_IS_CHANNEL_A 0x00000020
  87. #define SUNZILOG_FLAG_REGS_HELD 0x00000040
  88. #define SUNZILOG_FLAG_TX_STOPPED 0x00000080
  89. #define SUNZILOG_FLAG_TX_ACTIVE 0x00000100
  90. unsigned int cflag;
  91. unsigned char parity_mask;
  92. unsigned char prev_status;
  93. #ifdef CONFIG_SERIO
  94. struct serio *serio;
  95. int serio_open;
  96. #endif
  97. };
  98. #define ZILOG_CHANNEL_FROM_PORT(PORT) ((struct zilog_channel __iomem *)((PORT)->membase))
  99. #define UART_ZILOG(PORT) ((struct uart_sunzilog_port *)(PORT))
  100. #define ZS_IS_KEYB(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_KEYB)
  101. #define ZS_IS_MOUSE(UP) ((UP)->flags & SUNZILOG_FLAG_CONS_MOUSE)
  102. #define ZS_IS_CONS(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CONS)
  103. #define ZS_IS_KGDB(UP) ((UP)->flags & SUNZILOG_FLAG_IS_KGDB)
  104. #define ZS_WANTS_MODEM_STATUS(UP) ((UP)->flags & SUNZILOG_FLAG_MODEM_STATUS)
  105. #define ZS_IS_CHANNEL_A(UP) ((UP)->flags & SUNZILOG_FLAG_IS_CHANNEL_A)
  106. #define ZS_REGS_HELD(UP) ((UP)->flags & SUNZILOG_FLAG_REGS_HELD)
  107. #define ZS_TX_STOPPED(UP) ((UP)->flags & SUNZILOG_FLAG_TX_STOPPED)
  108. #define ZS_TX_ACTIVE(UP) ((UP)->flags & SUNZILOG_FLAG_TX_ACTIVE)
  109. /* Reading and writing Zilog8530 registers. The delays are to make this
  110. * driver work on the Sun4 which needs a settling delay after each chip
  111. * register access, other machines handle this in hardware via auxiliary
  112. * flip-flops which implement the settle time we do in software.
  113. *
  114. * The port lock must be held and local IRQs must be disabled
  115. * when {read,write}_zsreg is invoked.
  116. */
  117. static unsigned char read_zsreg(struct zilog_channel __iomem *channel,
  118. unsigned char reg)
  119. {
  120. unsigned char retval;
  121. sbus_writeb(reg, &channel->control);
  122. ZSDELAY();
  123. retval = sbus_readb(&channel->control);
  124. ZSDELAY();
  125. return retval;
  126. }
  127. static void write_zsreg(struct zilog_channel __iomem *channel,
  128. unsigned char reg, unsigned char value)
  129. {
  130. sbus_writeb(reg, &channel->control);
  131. ZSDELAY();
  132. sbus_writeb(value, &channel->control);
  133. ZSDELAY();
  134. }
  135. static void sunzilog_clear_fifo(struct zilog_channel __iomem *channel)
  136. {
  137. int i;
  138. for (i = 0; i < 32; i++) {
  139. unsigned char regval;
  140. regval = sbus_readb(&channel->control);
  141. ZSDELAY();
  142. if (regval & Rx_CH_AV)
  143. break;
  144. regval = read_zsreg(channel, R1);
  145. sbus_readb(&channel->data);
  146. ZSDELAY();
  147. if (regval & (PAR_ERR | Rx_OVR | CRC_ERR)) {
  148. sbus_writeb(ERR_RES, &channel->control);
  149. ZSDELAY();
  150. ZS_WSYNC(channel);
  151. }
  152. }
  153. }
  154. /* This function must only be called when the TX is not busy. The UART
  155. * port lock must be held and local interrupts disabled.
  156. */
  157. static void __load_zsregs(struct zilog_channel __iomem *channel, unsigned char *regs)
  158. {
  159. int i;
  160. /* Let pending transmits finish. */
  161. for (i = 0; i < 1000; i++) {
  162. unsigned char stat = read_zsreg(channel, R1);
  163. if (stat & ALL_SNT)
  164. break;
  165. udelay(100);
  166. }
  167. sbus_writeb(ERR_RES, &channel->control);
  168. ZSDELAY();
  169. ZS_WSYNC(channel);
  170. sunzilog_clear_fifo(channel);
  171. /* Disable all interrupts. */
  172. write_zsreg(channel, R1,
  173. regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
  174. /* Set parity, sync config, stop bits, and clock divisor. */
  175. write_zsreg(channel, R4, regs[R4]);
  176. /* Set misc. TX/RX control bits. */
  177. write_zsreg(channel, R10, regs[R10]);
  178. /* Set TX/RX controls sans the enable bits. */
  179. write_zsreg(channel, R3, regs[R3] & ~RxENAB);
  180. write_zsreg(channel, R5, regs[R5] & ~TxENAB);
  181. /* Synchronous mode config. */
  182. write_zsreg(channel, R6, regs[R6]);
  183. write_zsreg(channel, R7, regs[R7]);
  184. /* Don't mess with the interrupt vector (R2, unused by us) and
  185. * master interrupt control (R9). We make sure this is setup
  186. * properly at probe time then never touch it again.
  187. */
  188. /* Disable baud generator. */
  189. write_zsreg(channel, R14, regs[R14] & ~BRENAB);
  190. /* Clock mode control. */
  191. write_zsreg(channel, R11, regs[R11]);
  192. /* Lower and upper byte of baud rate generator divisor. */
  193. write_zsreg(channel, R12, regs[R12]);
  194. write_zsreg(channel, R13, regs[R13]);
  195. /* Now rewrite R14, with BRENAB (if set). */
  196. write_zsreg(channel, R14, regs[R14]);
  197. /* External status interrupt control. */
  198. write_zsreg(channel, R15, regs[R15]);
  199. /* Reset external status interrupts. */
  200. write_zsreg(channel, R0, RES_EXT_INT);
  201. write_zsreg(channel, R0, RES_EXT_INT);
  202. /* Rewrite R3/R5, this time without enables masked. */
  203. write_zsreg(channel, R3, regs[R3]);
  204. write_zsreg(channel, R5, regs[R5]);
  205. /* Rewrite R1, this time without IRQ enabled masked. */
  206. write_zsreg(channel, R1, regs[R1]);
  207. }
  208. /* Reprogram the Zilog channel HW registers with the copies found in the
  209. * software state struct. If the transmitter is busy, we defer this update
  210. * until the next TX complete interrupt. Else, we do it right now.
  211. *
  212. * The UART port lock must be held and local interrupts disabled.
  213. */
  214. static void sunzilog_maybe_update_regs(struct uart_sunzilog_port *up,
  215. struct zilog_channel __iomem *channel)
  216. {
  217. if (!ZS_REGS_HELD(up)) {
  218. if (ZS_TX_ACTIVE(up)) {
  219. up->flags |= SUNZILOG_FLAG_REGS_HELD;
  220. } else {
  221. __load_zsregs(channel, up->curregs);
  222. }
  223. }
  224. }
  225. static void sunzilog_change_mouse_baud(struct uart_sunzilog_port *up)
  226. {
  227. unsigned int cur_cflag = up->cflag;
  228. int brg, new_baud;
  229. up->cflag &= ~CBAUD;
  230. up->cflag |= suncore_mouse_baud_cflag_next(cur_cflag, &new_baud);
  231. brg = BPS_TO_BRG(new_baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  232. up->curregs[R12] = (brg & 0xff);
  233. up->curregs[R13] = (brg >> 8) & 0xff;
  234. sunzilog_maybe_update_regs(up, ZILOG_CHANNEL_FROM_PORT(&up->port));
  235. }
  236. static void sunzilog_kbdms_receive_chars(struct uart_sunzilog_port *up,
  237. unsigned char ch, int is_break,
  238. struct pt_regs *regs)
  239. {
  240. if (ZS_IS_KEYB(up)) {
  241. /* Stop-A is handled by drivers/char/keyboard.c now. */
  242. #ifdef CONFIG_SERIO
  243. if (up->serio_open)
  244. serio_interrupt(up->serio, ch, 0, regs);
  245. #endif
  246. } else if (ZS_IS_MOUSE(up)) {
  247. int ret = suncore_mouse_baud_detection(ch, is_break);
  248. switch (ret) {
  249. case 2:
  250. sunzilog_change_mouse_baud(up);
  251. /* fallthru */
  252. case 1:
  253. break;
  254. case 0:
  255. #ifdef CONFIG_SERIO
  256. if (up->serio_open)
  257. serio_interrupt(up->serio, ch, 0, regs);
  258. #endif
  259. break;
  260. };
  261. }
  262. }
  263. static struct tty_struct *
  264. sunzilog_receive_chars(struct uart_sunzilog_port *up,
  265. struct zilog_channel __iomem *channel,
  266. struct pt_regs *regs)
  267. {
  268. struct tty_struct *tty;
  269. unsigned char ch, r1, flag;
  270. tty = NULL;
  271. if (up->port.info != NULL && /* Unopened serial console */
  272. up->port.info->tty != NULL) /* Keyboard || mouse */
  273. tty = up->port.info->tty;
  274. for (;;) {
  275. r1 = read_zsreg(channel, R1);
  276. if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
  277. sbus_writeb(ERR_RES, &channel->control);
  278. ZSDELAY();
  279. ZS_WSYNC(channel);
  280. }
  281. ch = sbus_readb(&channel->control);
  282. ZSDELAY();
  283. /* This funny hack depends upon BRK_ABRT not interfering
  284. * with the other bits we care about in R1.
  285. */
  286. if (ch & BRK_ABRT)
  287. r1 |= BRK_ABRT;
  288. if (!(ch & Rx_CH_AV))
  289. break;
  290. ch = sbus_readb(&channel->data);
  291. ZSDELAY();
  292. ch &= up->parity_mask;
  293. if (unlikely(ZS_IS_KEYB(up)) || unlikely(ZS_IS_MOUSE(up))) {
  294. sunzilog_kbdms_receive_chars(up, ch, 0, regs);
  295. continue;
  296. }
  297. if (tty == NULL) {
  298. uart_handle_sysrq_char(&up->port, ch, regs);
  299. continue;
  300. }
  301. /* A real serial line, record the character and status. */
  302. flag = TTY_NORMAL;
  303. up->port.icount.rx++;
  304. if (r1 & (BRK_ABRT | PAR_ERR | Rx_OVR | CRC_ERR)) {
  305. if (r1 & BRK_ABRT) {
  306. r1 &= ~(PAR_ERR | CRC_ERR);
  307. up->port.icount.brk++;
  308. if (uart_handle_break(&up->port))
  309. continue;
  310. }
  311. else if (r1 & PAR_ERR)
  312. up->port.icount.parity++;
  313. else if (r1 & CRC_ERR)
  314. up->port.icount.frame++;
  315. if (r1 & Rx_OVR)
  316. up->port.icount.overrun++;
  317. r1 &= up->port.read_status_mask;
  318. if (r1 & BRK_ABRT)
  319. flag = TTY_BREAK;
  320. else if (r1 & PAR_ERR)
  321. flag = TTY_PARITY;
  322. else if (r1 & CRC_ERR)
  323. flag = TTY_FRAME;
  324. }
  325. if (uart_handle_sysrq_char(&up->port, ch, regs))
  326. continue;
  327. if (up->port.ignore_status_mask == 0xff ||
  328. (r1 & up->port.ignore_status_mask) == 0) {
  329. tty_insert_flip_char(tty, ch, flag);
  330. }
  331. if (r1 & Rx_OVR)
  332. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  333. }
  334. return tty;
  335. }
  336. static void sunzilog_status_handle(struct uart_sunzilog_port *up,
  337. struct zilog_channel __iomem *channel,
  338. struct pt_regs *regs)
  339. {
  340. unsigned char status;
  341. status = sbus_readb(&channel->control);
  342. ZSDELAY();
  343. sbus_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, regs);
  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 = sbus_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.info->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 = sbus_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 becomming 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. sbus_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.info == NULL)
  417. goto ack_tx_int;
  418. xmit = &up->port.info->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. sbus_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. sbus_writeb(RES_Tx_P, &channel->control);
  434. ZSDELAY();
  435. ZS_WSYNC(channel);
  436. }
  437. static irqreturn_t sunzilog_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  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_struct *tty;
  444. unsigned char r3;
  445. spin_lock(&up->port.lock);
  446. r3 = read_zsreg(channel, R3);
  447. /* Channel A */
  448. tty = NULL;
  449. if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
  450. sbus_writeb(RES_H_IUS, &channel->control);
  451. ZSDELAY();
  452. ZS_WSYNC(channel);
  453. if (r3 & CHARxIP)
  454. tty = sunzilog_receive_chars(up, channel, regs);
  455. if (r3 & CHAEXT)
  456. sunzilog_status_handle(up, channel, regs);
  457. if (r3 & CHATxIP)
  458. sunzilog_transmit_chars(up, channel);
  459. }
  460. spin_unlock(&up->port.lock);
  461. if (tty)
  462. tty_flip_buffer_push(tty);
  463. /* Channel B */
  464. up = up->next;
  465. channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  466. spin_lock(&up->port.lock);
  467. tty = NULL;
  468. if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
  469. sbus_writeb(RES_H_IUS, &channel->control);
  470. ZSDELAY();
  471. ZS_WSYNC(channel);
  472. if (r3 & CHBRxIP)
  473. tty = sunzilog_receive_chars(up, channel, regs);
  474. if (r3 & CHBEXT)
  475. sunzilog_status_handle(up, channel, regs);
  476. if (r3 & CHBTxIP)
  477. sunzilog_transmit_chars(up, channel);
  478. }
  479. spin_unlock(&up->port.lock);
  480. if (tty)
  481. tty_flip_buffer_push(tty);
  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 = sbus_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 = sbus_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. sbus_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->info->xmit;
  578. sbus_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]);
  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 = sbus_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[3] &= ~RxN_MASK;
  715. up->curregs[5] &= ~TxN_MASK;
  716. switch (cflag & CSIZE) {
  717. case CS5:
  718. up->curregs[3] |= Rx5;
  719. up->curregs[5] |= Tx5;
  720. up->parity_mask = 0x1f;
  721. break;
  722. case CS6:
  723. up->curregs[3] |= Rx6;
  724. up->curregs[5] |= Tx6;
  725. up->parity_mask = 0x3f;
  726. break;
  727. case CS7:
  728. up->curregs[3] |= Rx7;
  729. up->curregs[5] |= Tx7;
  730. up->parity_mask = 0x7f;
  731. break;
  732. case CS8:
  733. default:
  734. up->curregs[3] |= Rx8;
  735. up->curregs[5] |= Tx8;
  736. up->parity_mask = 0xff;
  737. break;
  738. };
  739. up->curregs[4] &= ~0x0c;
  740. if (cflag & CSTOPB)
  741. up->curregs[4] |= SB2;
  742. else
  743. up->curregs[4] |= SB1;
  744. if (cflag & PARENB)
  745. up->curregs[4] |= PAR_ENAB;
  746. else
  747. up->curregs[4] &= ~PAR_ENAB;
  748. if (!(cflag & PARODD))
  749. up->curregs[4] |= PAR_EVEN;
  750. else
  751. up->curregs[4] &= ~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 termios *termios,
  771. struct termios *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. return "SunZilog";
  792. }
  793. /* We do not request/release mappings of the registers here, this
  794. * happens at early serial probe time.
  795. */
  796. static void sunzilog_release_port(struct uart_port *port)
  797. {
  798. }
  799. static int sunzilog_request_port(struct uart_port *port)
  800. {
  801. return 0;
  802. }
  803. /* These do not need to do anything interesting either. */
  804. static void sunzilog_config_port(struct uart_port *port, int flags)
  805. {
  806. }
  807. /* We do not support letting the user mess with the divisor, IRQ, etc. */
  808. static int sunzilog_verify_port(struct uart_port *port, struct serial_struct *ser)
  809. {
  810. return -EINVAL;
  811. }
  812. static struct uart_ops sunzilog_pops = {
  813. .tx_empty = sunzilog_tx_empty,
  814. .set_mctrl = sunzilog_set_mctrl,
  815. .get_mctrl = sunzilog_get_mctrl,
  816. .stop_tx = sunzilog_stop_tx,
  817. .start_tx = sunzilog_start_tx,
  818. .stop_rx = sunzilog_stop_rx,
  819. .enable_ms = sunzilog_enable_ms,
  820. .break_ctl = sunzilog_break_ctl,
  821. .startup = sunzilog_startup,
  822. .shutdown = sunzilog_shutdown,
  823. .set_termios = sunzilog_set_termios,
  824. .type = sunzilog_type,
  825. .release_port = sunzilog_release_port,
  826. .request_port = sunzilog_request_port,
  827. .config_port = sunzilog_config_port,
  828. .verify_port = sunzilog_verify_port,
  829. };
  830. static struct uart_sunzilog_port *sunzilog_port_table;
  831. static struct zilog_layout __iomem **sunzilog_chip_regs;
  832. static struct uart_sunzilog_port *sunzilog_irq_chain;
  833. static int zilog_irq = -1;
  834. static struct uart_driver sunzilog_reg = {
  835. .owner = THIS_MODULE,
  836. .driver_name = "ttyS",
  837. .dev_name = "ttyS",
  838. .major = TTY_MAJOR,
  839. };
  840. static void * __init alloc_one_table(unsigned long size)
  841. {
  842. void *ret;
  843. ret = kmalloc(size, GFP_KERNEL);
  844. if (ret != NULL)
  845. memset(ret, 0, size);
  846. return ret;
  847. }
  848. static void __init sunzilog_alloc_tables(void)
  849. {
  850. sunzilog_port_table =
  851. alloc_one_table(NUM_CHANNELS * sizeof(struct uart_sunzilog_port));
  852. sunzilog_chip_regs =
  853. alloc_one_table(NUM_SUNZILOG * sizeof(struct zilog_layout __iomem *));
  854. if (sunzilog_port_table == NULL || sunzilog_chip_regs == NULL) {
  855. prom_printf("SunZilog: Cannot allocate tables.\n");
  856. prom_halt();
  857. }
  858. }
  859. #ifdef CONFIG_SPARC64
  860. /* We used to attempt to use the address property of the Zilog device node
  861. * but that totally is not necessary on sparc64.
  862. */
  863. static struct zilog_layout __iomem * __init get_zs_sun4u(int chip, int zsnode)
  864. {
  865. void __iomem *mapped_addr;
  866. unsigned int sun4u_ino;
  867. struct sbus_bus *sbus = NULL;
  868. struct sbus_dev *sdev = NULL;
  869. int err;
  870. if (central_bus == NULL) {
  871. for_each_sbus(sbus) {
  872. for_each_sbusdev(sdev, sbus) {
  873. if (sdev->prom_node == zsnode)
  874. goto found;
  875. }
  876. }
  877. }
  878. found:
  879. if (sdev == NULL && central_bus == NULL) {
  880. prom_printf("SunZilog: sdev&&central == NULL for "
  881. "Zilog %d in get_zs_sun4u.\n", chip);
  882. prom_halt();
  883. }
  884. if (central_bus == NULL) {
  885. mapped_addr =
  886. sbus_ioremap(&sdev->resource[0], 0,
  887. PAGE_SIZE,
  888. "Zilog Registers");
  889. } else {
  890. struct linux_prom_registers zsregs[1];
  891. err = prom_getproperty(zsnode, "reg",
  892. (char *) &zsregs[0],
  893. sizeof(zsregs));
  894. if (err == -1) {
  895. prom_printf("SunZilog: Cannot map "
  896. "Zilog %d regs on "
  897. "central bus.\n", chip);
  898. prom_halt();
  899. }
  900. apply_fhc_ranges(central_bus->child,
  901. &zsregs[0], 1);
  902. apply_central_ranges(central_bus, &zsregs[0], 1);
  903. mapped_addr = (void __iomem *)
  904. ((((u64)zsregs[0].which_io)<<32UL) |
  905. ((u64)zsregs[0].phys_addr));
  906. }
  907. if (zilog_irq == -1) {
  908. if (central_bus) {
  909. unsigned long iclr, imap;
  910. iclr = central_bus->child->fhc_regs.uregs
  911. + FHC_UREGS_ICLR;
  912. imap = central_bus->child->fhc_regs.uregs
  913. + FHC_UREGS_IMAP;
  914. zilog_irq = build_irq(0, iclr, imap);
  915. } else {
  916. err = prom_getproperty(zsnode, "interrupts",
  917. (char *) &sun4u_ino,
  918. sizeof(sun4u_ino));
  919. zilog_irq = sbus_build_irq(sbus_root, sun4u_ino);
  920. }
  921. }
  922. return (struct zilog_layout __iomem *) mapped_addr;
  923. }
  924. #else /* CONFIG_SPARC64 */
  925. /*
  926. * XXX The sun4d case is utterly screwed: it tries to re-walk the tree
  927. * (for the 3rd time) in order to find bootbus and cpu. Streamline it.
  928. */
  929. static struct zilog_layout __iomem * __init get_zs_sun4cmd(int chip, int node)
  930. {
  931. struct linux_prom_irqs irq_info[2];
  932. void __iomem *mapped_addr = NULL;
  933. int zsnode, cpunode, bbnode;
  934. struct linux_prom_registers zsreg[4];
  935. struct resource res;
  936. if (sparc_cpu_model == sun4d) {
  937. int walk;
  938. zsnode = 0;
  939. bbnode = 0;
  940. cpunode = 0;
  941. for (walk = prom_getchild(prom_root_node);
  942. (walk = prom_searchsiblings(walk, "cpu-unit")) != 0;
  943. walk = prom_getsibling(walk)) {
  944. bbnode = prom_getchild(walk);
  945. if (bbnode &&
  946. (bbnode = prom_searchsiblings(bbnode, "bootbus"))) {
  947. if ((zsnode = prom_getchild(bbnode)) == node) {
  948. cpunode = walk;
  949. break;
  950. }
  951. }
  952. }
  953. if (!walk) {
  954. prom_printf("SunZilog: Cannot find the %d'th bootbus on sun4d.\n",
  955. (chip / 2));
  956. prom_halt();
  957. }
  958. if (prom_getproperty(zsnode, "reg",
  959. (char *) zsreg, sizeof(zsreg)) == -1) {
  960. prom_printf("SunZilog: Cannot map Zilog %d\n", chip);
  961. prom_halt();
  962. }
  963. /* XXX Looks like an off by one? */
  964. prom_apply_generic_ranges(bbnode, cpunode, zsreg, 1);
  965. res.start = zsreg[0].phys_addr;
  966. res.end = res.start + (8 - 1);
  967. res.flags = zsreg[0].which_io | IORESOURCE_IO;
  968. mapped_addr = sbus_ioremap(&res, 0, 8, "Zilog Serial");
  969. } else {
  970. zsnode = node;
  971. #if 0 /* XXX When was this used? */
  972. if (prom_getintdefault(zsnode, "slave", -1) != chipid) {
  973. zsnode = prom_getsibling(zsnode);
  974. continue;
  975. }
  976. #endif
  977. /*
  978. * "address" is only present on ports that OBP opened
  979. * (from Mitch Bradley's "Hitchhiker's Guide to OBP").
  980. * We do not use it.
  981. */
  982. if (prom_getproperty(zsnode, "reg",
  983. (char *) zsreg, sizeof(zsreg)) == -1) {
  984. prom_printf("SunZilog: Cannot map Zilog %d\n", chip);
  985. prom_halt();
  986. }
  987. if (sparc_cpu_model == sun4m) /* Crude. Pass parent. XXX */
  988. prom_apply_obio_ranges(zsreg, 1);
  989. res.start = zsreg[0].phys_addr;
  990. res.end = res.start + (8 - 1);
  991. res.flags = zsreg[0].which_io | IORESOURCE_IO;
  992. mapped_addr = sbus_ioremap(&res, 0, 8, "Zilog Serial");
  993. }
  994. if (prom_getproperty(zsnode, "intr",
  995. (char *) irq_info, sizeof(irq_info))
  996. % sizeof(struct linux_prom_irqs)) {
  997. prom_printf("SunZilog: Cannot get IRQ property for Zilog %d.\n",
  998. chip);
  999. prom_halt();
  1000. }
  1001. if (zilog_irq == -1) {
  1002. zilog_irq = irq_info[0].pri;
  1003. } else if (zilog_irq != irq_info[0].pri) {
  1004. /* XXX. Dumb. Should handle per-chip IRQ, for add-ons. */
  1005. prom_printf("SunZilog: Inconsistent IRQ layout for Zilog %d.\n",
  1006. chip);
  1007. prom_halt();
  1008. }
  1009. return (struct zilog_layout __iomem *) mapped_addr;
  1010. }
  1011. #endif /* !(CONFIG_SPARC64) */
  1012. /* Get the address of the registers for SunZilog instance CHIP. */
  1013. static struct zilog_layout __iomem * __init get_zs(int chip, int node)
  1014. {
  1015. if (chip < 0 || chip >= NUM_SUNZILOG) {
  1016. prom_printf("SunZilog: Illegal chip number %d in get_zs.\n", chip);
  1017. prom_halt();
  1018. }
  1019. #ifdef CONFIG_SPARC64
  1020. return get_zs_sun4u(chip, node);
  1021. #else
  1022. if (sparc_cpu_model == sun4) {
  1023. struct resource res;
  1024. /* Not probe-able, hard code it. */
  1025. switch (chip) {
  1026. case 0:
  1027. res.start = 0xf1000000;
  1028. break;
  1029. case 1:
  1030. res.start = 0xf0000000;
  1031. break;
  1032. };
  1033. zilog_irq = 12;
  1034. res.end = (res.start + (8 - 1));
  1035. res.flags = IORESOURCE_IO;
  1036. return sbus_ioremap(&res, 0, 8, "SunZilog");
  1037. }
  1038. return get_zs_sun4cmd(chip, node);
  1039. #endif
  1040. }
  1041. #define ZS_PUT_CHAR_MAX_DELAY 2000 /* 10 ms */
  1042. static void sunzilog_putchar(struct uart_port *port, int ch)
  1043. {
  1044. struct zilog_channel *channel = ZILOG_CHANNEL_FROM_PORT(port);
  1045. int loops = ZS_PUT_CHAR_MAX_DELAY;
  1046. /* This is a timed polling loop so do not switch the explicit
  1047. * udelay with ZSDELAY as that is a NOP on some platforms. -DaveM
  1048. */
  1049. do {
  1050. unsigned char val = sbus_readb(&channel->control);
  1051. if (val & Tx_BUF_EMP) {
  1052. ZSDELAY();
  1053. break;
  1054. }
  1055. udelay(5);
  1056. } while (--loops);
  1057. sbus_writeb(ch, &channel->data);
  1058. ZSDELAY();
  1059. ZS_WSYNC(channel);
  1060. }
  1061. #ifdef CONFIG_SERIO
  1062. static DEFINE_SPINLOCK(sunzilog_serio_lock);
  1063. static int sunzilog_serio_write(struct serio *serio, unsigned char ch)
  1064. {
  1065. struct uart_sunzilog_port *up = serio->port_data;
  1066. unsigned long flags;
  1067. spin_lock_irqsave(&sunzilog_serio_lock, flags);
  1068. sunzilog_putchar(&up->port, ch);
  1069. spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
  1070. return 0;
  1071. }
  1072. static int sunzilog_serio_open(struct serio *serio)
  1073. {
  1074. struct uart_sunzilog_port *up = serio->port_data;
  1075. unsigned long flags;
  1076. int ret;
  1077. spin_lock_irqsave(&sunzilog_serio_lock, flags);
  1078. if (!up->serio_open) {
  1079. up->serio_open = 1;
  1080. ret = 0;
  1081. } else
  1082. ret = -EBUSY;
  1083. spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
  1084. return ret;
  1085. }
  1086. static void sunzilog_serio_close(struct serio *serio)
  1087. {
  1088. struct uart_sunzilog_port *up = serio->port_data;
  1089. unsigned long flags;
  1090. spin_lock_irqsave(&sunzilog_serio_lock, flags);
  1091. up->serio_open = 0;
  1092. spin_unlock_irqrestore(&sunzilog_serio_lock, flags);
  1093. }
  1094. #endif /* CONFIG_SERIO */
  1095. #ifdef CONFIG_SERIAL_SUNZILOG_CONSOLE
  1096. static void
  1097. sunzilog_console_write(struct console *con, const char *s, unsigned int count)
  1098. {
  1099. struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
  1100. unsigned long flags;
  1101. spin_lock_irqsave(&up->port.lock, flags);
  1102. uart_console_write(&up->port, s, count, sunzilog_putchar);
  1103. udelay(2);
  1104. spin_unlock_irqrestore(&up->port.lock, flags);
  1105. }
  1106. static int __init sunzilog_console_setup(struct console *con, char *options)
  1107. {
  1108. struct uart_sunzilog_port *up = &sunzilog_port_table[con->index];
  1109. unsigned long flags;
  1110. int baud, brg;
  1111. printk(KERN_INFO "Console: ttyS%d (SunZilog zs%d)\n",
  1112. (sunzilog_reg.minor - 64) + con->index, con->index);
  1113. /* Get firmware console settings. */
  1114. sunserial_console_termios(con);
  1115. /* Firmware console speed is limited to 150-->38400 baud so
  1116. * this hackish cflag thing is OK.
  1117. */
  1118. switch (con->cflag & CBAUD) {
  1119. case B150: baud = 150; break;
  1120. case B300: baud = 300; break;
  1121. case B600: baud = 600; break;
  1122. case B1200: baud = 1200; break;
  1123. case B2400: baud = 2400; break;
  1124. case B4800: baud = 4800; break;
  1125. default: case B9600: baud = 9600; break;
  1126. case B19200: baud = 19200; break;
  1127. case B38400: baud = 38400; break;
  1128. };
  1129. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  1130. spin_lock_irqsave(&up->port.lock, flags);
  1131. up->curregs[R15] = BRKIE;
  1132. sunzilog_convert_to_zs(up, con->cflag, 0, brg);
  1133. sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
  1134. __sunzilog_startup(up);
  1135. spin_unlock_irqrestore(&up->port.lock, flags);
  1136. return 0;
  1137. }
  1138. static struct console sunzilog_console = {
  1139. .name = "ttyS",
  1140. .write = sunzilog_console_write,
  1141. .device = uart_console_device,
  1142. .setup = sunzilog_console_setup,
  1143. .flags = CON_PRINTBUFFER,
  1144. .index = -1,
  1145. .data = &sunzilog_reg,
  1146. };
  1147. static int __init sunzilog_console_init(void)
  1148. {
  1149. int i;
  1150. if (con_is_present())
  1151. return 0;
  1152. for (i = 0; i < NUM_CHANNELS; i++) {
  1153. int this_minor = sunzilog_reg.minor + i;
  1154. if ((this_minor - 64) == (serial_console - 1))
  1155. break;
  1156. }
  1157. if (i == NUM_CHANNELS)
  1158. return 0;
  1159. sunzilog_console.index = i;
  1160. sunzilog_port_table[i].flags |= SUNZILOG_FLAG_IS_CONS;
  1161. register_console(&sunzilog_console);
  1162. return 0;
  1163. }
  1164. static inline struct console *SUNZILOG_CONSOLE(void)
  1165. {
  1166. int i;
  1167. if (con_is_present())
  1168. return NULL;
  1169. for (i = 0; i < NUM_CHANNELS; i++) {
  1170. int this_minor = sunzilog_reg.minor + i;
  1171. if ((this_minor - 64) == (serial_console - 1))
  1172. break;
  1173. }
  1174. if (i == NUM_CHANNELS)
  1175. return NULL;
  1176. sunzilog_console.index = i;
  1177. sunzilog_port_table[i].flags |= SUNZILOG_FLAG_IS_CONS;
  1178. return &sunzilog_console;
  1179. }
  1180. #else
  1181. #define SUNZILOG_CONSOLE() (NULL)
  1182. #define sunzilog_console_init() do { } while (0)
  1183. #endif
  1184. /*
  1185. * We scan the PROM tree recursively. This is the most reliable way
  1186. * to find Zilog nodes on various platforms. However, we face an extreme
  1187. * shortage of kernel stack, so we must be very careful. To that end,
  1188. * we scan only to a certain depth, and we use a common property buffer
  1189. * in the scan structure.
  1190. */
  1191. #define ZS_PROPSIZE 128
  1192. #define ZS_SCAN_DEPTH 5
  1193. struct zs_probe_scan {
  1194. int depth;
  1195. void (*scanner)(struct zs_probe_scan *t, int node);
  1196. int devices;
  1197. char prop[ZS_PROPSIZE];
  1198. };
  1199. static int __inline__ sunzilog_node_ok(int node, const char *name, int len)
  1200. {
  1201. if (strncmp(name, "zs", len) == 0)
  1202. return 1;
  1203. /* Don't fold this procedure just yet. Compare to su_node_ok(). */
  1204. return 0;
  1205. }
  1206. static void __init sunzilog_scan(struct zs_probe_scan *t, int node)
  1207. {
  1208. int len;
  1209. for (; node != 0; node = prom_getsibling(node)) {
  1210. len = prom_getproperty(node, "name", t->prop, ZS_PROPSIZE);
  1211. if (len <= 1)
  1212. continue; /* Broken PROM node */
  1213. if (sunzilog_node_ok(node, t->prop, len)) {
  1214. (*t->scanner)(t, node);
  1215. } else {
  1216. if (t->depth < ZS_SCAN_DEPTH) {
  1217. t->depth++;
  1218. sunzilog_scan(t, prom_getchild(node));
  1219. --t->depth;
  1220. }
  1221. }
  1222. }
  1223. }
  1224. static void __init sunzilog_prepare(void)
  1225. {
  1226. struct uart_sunzilog_port *up;
  1227. struct zilog_layout __iomem *rp;
  1228. int channel, chip;
  1229. /*
  1230. * Temporary fix.
  1231. */
  1232. for (channel = 0; channel < NUM_CHANNELS; channel++)
  1233. spin_lock_init(&sunzilog_port_table[channel].port.lock);
  1234. sunzilog_irq_chain = up = &sunzilog_port_table[0];
  1235. for (channel = 0; channel < NUM_CHANNELS - 1; channel++)
  1236. up[channel].next = &up[channel + 1];
  1237. up[channel].next = NULL;
  1238. for (chip = 0; chip < NUM_SUNZILOG; chip++) {
  1239. rp = sunzilog_chip_regs[chip];
  1240. up[(chip * 2) + 0].port.membase = (void __iomem *)&rp->channelA;
  1241. up[(chip * 2) + 1].port.membase = (void __iomem *)&rp->channelB;
  1242. /* Channel A */
  1243. up[(chip * 2) + 0].port.iotype = UPIO_MEM;
  1244. up[(chip * 2) + 0].port.irq = zilog_irq;
  1245. up[(chip * 2) + 0].port.uartclk = ZS_CLOCK;
  1246. up[(chip * 2) + 0].port.fifosize = 1;
  1247. up[(chip * 2) + 0].port.ops = &sunzilog_pops;
  1248. up[(chip * 2) + 0].port.type = PORT_SUNZILOG;
  1249. up[(chip * 2) + 0].port.flags = 0;
  1250. up[(chip * 2) + 0].port.line = (chip * 2) + 0;
  1251. up[(chip * 2) + 0].flags |= SUNZILOG_FLAG_IS_CHANNEL_A;
  1252. /* Channel B */
  1253. up[(chip * 2) + 1].port.iotype = UPIO_MEM;
  1254. up[(chip * 2) + 1].port.irq = zilog_irq;
  1255. up[(chip * 2) + 1].port.uartclk = ZS_CLOCK;
  1256. up[(chip * 2) + 1].port.fifosize = 1;
  1257. up[(chip * 2) + 1].port.ops = &sunzilog_pops;
  1258. up[(chip * 2) + 1].port.type = PORT_SUNZILOG;
  1259. up[(chip * 2) + 1].port.flags = 0;
  1260. up[(chip * 2) + 1].port.line = (chip * 2) + 1;
  1261. up[(chip * 2) + 1].flags |= 0;
  1262. }
  1263. }
  1264. static void __init sunzilog_init_kbdms(struct uart_sunzilog_port *up, int channel)
  1265. {
  1266. int baud, brg;
  1267. if (channel == KEYBOARD_LINE) {
  1268. up->flags |= SUNZILOG_FLAG_CONS_KEYB;
  1269. up->cflag = B1200 | CS8 | CLOCAL | CREAD;
  1270. baud = 1200;
  1271. } else {
  1272. up->flags |= SUNZILOG_FLAG_CONS_MOUSE;
  1273. up->cflag = B4800 | CS8 | CLOCAL | CREAD;
  1274. baud = 4800;
  1275. }
  1276. printk(KERN_INFO "zs%d at 0x%p (irq = %d) is a SunZilog\n",
  1277. channel, up->port.membase, zilog_irq);
  1278. up->curregs[R15] = BRKIE;
  1279. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  1280. sunzilog_convert_to_zs(up, up->cflag, 0, brg);
  1281. sunzilog_set_mctrl(&up->port, TIOCM_DTR | TIOCM_RTS);
  1282. __sunzilog_startup(up);
  1283. }
  1284. #ifdef CONFIG_SERIO
  1285. static void __init sunzilog_register_serio(struct uart_sunzilog_port *up, int channel)
  1286. {
  1287. struct serio *serio;
  1288. up->serio = serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
  1289. if (serio) {
  1290. memset(serio, 0, sizeof(*serio));
  1291. serio->port_data = up;
  1292. serio->id.type = SERIO_RS232;
  1293. if (channel == KEYBOARD_LINE) {
  1294. serio->id.proto = SERIO_SUNKBD;
  1295. strlcpy(serio->name, "zskbd", sizeof(serio->name));
  1296. } else {
  1297. serio->id.proto = SERIO_SUN;
  1298. serio->id.extra = 1;
  1299. strlcpy(serio->name, "zsms", sizeof(serio->name));
  1300. }
  1301. strlcpy(serio->phys,
  1302. (channel == KEYBOARD_LINE ? "zs/serio0" : "zs/serio1"),
  1303. sizeof(serio->phys));
  1304. serio->write = sunzilog_serio_write;
  1305. serio->open = sunzilog_serio_open;
  1306. serio->close = sunzilog_serio_close;
  1307. serio_register_port(serio);
  1308. } else {
  1309. printk(KERN_WARNING "zs%d: not enough memory for serio port\n",
  1310. channel);
  1311. }
  1312. }
  1313. #endif
  1314. static void __init sunzilog_init_hw(void)
  1315. {
  1316. int i;
  1317. for (i = 0; i < NUM_CHANNELS; i++) {
  1318. struct uart_sunzilog_port *up = &sunzilog_port_table[i];
  1319. struct zilog_channel __iomem *channel = ZILOG_CHANNEL_FROM_PORT(&up->port);
  1320. unsigned long flags;
  1321. int baud, brg;
  1322. spin_lock_irqsave(&up->port.lock, flags);
  1323. if (ZS_IS_CHANNEL_A(up)) {
  1324. write_zsreg(channel, R9, FHWRES);
  1325. ZSDELAY_LONG();
  1326. (void) read_zsreg(channel, R0);
  1327. }
  1328. if (i == KEYBOARD_LINE || i == MOUSE_LINE) {
  1329. sunzilog_init_kbdms(up, i);
  1330. up->curregs[R9] |= (NV | MIE);
  1331. write_zsreg(channel, R9, up->curregs[R9]);
  1332. } else {
  1333. /* Normal serial TTY. */
  1334. up->parity_mask = 0xff;
  1335. up->curregs[R1] = EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB;
  1336. up->curregs[R4] = PAR_EVEN | X16CLK | SB1;
  1337. up->curregs[R3] = RxENAB | Rx8;
  1338. up->curregs[R5] = TxENAB | Tx8;
  1339. up->curregs[R9] = NV | MIE;
  1340. up->curregs[R10] = NRZ;
  1341. up->curregs[R11] = TCBR | RCBR;
  1342. baud = 9600;
  1343. brg = BPS_TO_BRG(baud, ZS_CLOCK / ZS_CLOCK_DIVISOR);
  1344. up->curregs[R12] = (brg & 0xff);
  1345. up->curregs[R13] = (brg >> 8) & 0xff;
  1346. up->curregs[R14] = BRSRC | BRENAB;
  1347. __load_zsregs(channel, up->curregs);
  1348. write_zsreg(channel, R9, up->curregs[R9]);
  1349. }
  1350. spin_unlock_irqrestore(&up->port.lock, flags);
  1351. #ifdef CONFIG_SERIO
  1352. if (i == KEYBOARD_LINE || i == MOUSE_LINE)
  1353. sunzilog_register_serio(up, i);
  1354. #endif
  1355. }
  1356. }
  1357. static struct zilog_layout __iomem * __init get_zs(int chip, int node);
  1358. static void __init sunzilog_scan_probe(struct zs_probe_scan *t, int node)
  1359. {
  1360. sunzilog_chip_regs[t->devices] = get_zs(t->devices, node);
  1361. t->devices++;
  1362. }
  1363. static int __init sunzilog_ports_init(void)
  1364. {
  1365. struct zs_probe_scan scan;
  1366. int ret;
  1367. int uart_count;
  1368. int i;
  1369. printk(KERN_DEBUG "SunZilog: %d chips.\n", NUM_SUNZILOG);
  1370. scan.scanner = sunzilog_scan_probe;
  1371. scan.depth = 0;
  1372. scan.devices = 0;
  1373. sunzilog_scan(&scan, prom_getchild(prom_root_node));
  1374. sunzilog_prepare();
  1375. if (request_irq(zilog_irq, sunzilog_interrupt, SA_SHIRQ,
  1376. "SunZilog", sunzilog_irq_chain)) {
  1377. prom_printf("SunZilog: Unable to register zs interrupt handler.\n");
  1378. prom_halt();
  1379. }
  1380. sunzilog_init_hw();
  1381. /* We can only init this once we have probed the Zilogs
  1382. * in the system. Do not count channels assigned to keyboards
  1383. * or mice when we are deciding how many ports to register.
  1384. */
  1385. uart_count = 0;
  1386. for (i = 0; i < NUM_CHANNELS; i++) {
  1387. struct uart_sunzilog_port *up = &sunzilog_port_table[i];
  1388. if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up))
  1389. continue;
  1390. uart_count++;
  1391. }
  1392. sunzilog_reg.nr = uart_count;
  1393. sunzilog_reg.minor = sunserial_current_minor;
  1394. ret = uart_register_driver(&sunzilog_reg);
  1395. if (ret == 0) {
  1396. sunzilog_reg.tty_driver->name_base = sunzilog_reg.minor - 64;
  1397. sunzilog_reg.cons = SUNZILOG_CONSOLE();
  1398. sunserial_current_minor += uart_count;
  1399. for (i = 0; i < NUM_CHANNELS; i++) {
  1400. struct uart_sunzilog_port *up = &sunzilog_port_table[i];
  1401. if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up))
  1402. continue;
  1403. if (uart_add_one_port(&sunzilog_reg, &up->port)) {
  1404. printk(KERN_ERR
  1405. "SunZilog: failed to add port zs%d\n", i);
  1406. }
  1407. }
  1408. }
  1409. return ret;
  1410. }
  1411. static void __init sunzilog_scan_count(struct zs_probe_scan *t, int node)
  1412. {
  1413. t->devices++;
  1414. }
  1415. static int __init sunzilog_ports_count(void)
  1416. {
  1417. struct zs_probe_scan scan;
  1418. /* Sun4 Zilog setup is hard coded, no probing to do. */
  1419. if (sparc_cpu_model == sun4)
  1420. return 2;
  1421. scan.scanner = sunzilog_scan_count;
  1422. scan.depth = 0;
  1423. scan.devices = 0;
  1424. sunzilog_scan(&scan, prom_getchild(prom_root_node));
  1425. return scan.devices;
  1426. }
  1427. static int __init sunzilog_init(void)
  1428. {
  1429. NUM_SUNZILOG = sunzilog_ports_count();
  1430. if (NUM_SUNZILOG == 0)
  1431. return -ENODEV;
  1432. sunzilog_alloc_tables();
  1433. sunzilog_ports_init();
  1434. return 0;
  1435. }
  1436. static void __exit sunzilog_exit(void)
  1437. {
  1438. int i;
  1439. for (i = 0; i < NUM_CHANNELS; i++) {
  1440. struct uart_sunzilog_port *up = &sunzilog_port_table[i];
  1441. if (ZS_IS_KEYB(up) || ZS_IS_MOUSE(up)) {
  1442. #ifdef CONFIG_SERIO
  1443. if (up->serio) {
  1444. serio_unregister_port(up->serio);
  1445. up->serio = NULL;
  1446. }
  1447. #endif
  1448. } else
  1449. uart_remove_one_port(&sunzilog_reg, &up->port);
  1450. }
  1451. uart_unregister_driver(&sunzilog_reg);
  1452. }
  1453. module_init(sunzilog_init);
  1454. module_exit(sunzilog_exit);
  1455. MODULE_AUTHOR("David S. Miller");
  1456. MODULE_DESCRIPTION("Sun Zilog serial port driver");
  1457. MODULE_LICENSE("GPL");