sunzilog.c 39 KB

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