pmac_zilog.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for PowerMac Z85c30 based ESCC cell found in the
  4. * "macio" ASICs of various PowerMac models
  5. *
  6. * Copyright (C) 2003 Ben. Herrenschmidt (benh@kernel.crashing.org)
  7. *
  8. * Derived from drivers/macintosh/macserial.c by Paul Mackerras
  9. * and drivers/serial/sunzilog.c by David S. Miller
  10. *
  11. * Hrm... actually, I ripped most of sunzilog (Thanks David !) and
  12. * adapted special tweaks needed for us. I don't think it's worth
  13. * merging back those though. The DMA code still has to get in
  14. * and once done, I expect that driver to remain fairly stable in
  15. * the long term, unless we change the driver model again...
  16. *
  17. * 2004-08-06 Harald Welte <laforge@gnumonks.org>
  18. * - Enable BREAK interrupt
  19. * - Add support for sysreq
  20. *
  21. * TODO: - Add DMA support
  22. * - Defer port shutdown to a few seconds after close
  23. * - maybe put something right into uap->clk_divisor
  24. */
  25. #undef DEBUG
  26. #undef DEBUG_HARD
  27. #undef USE_CTRL_O_SYSRQ
  28. #include <linux/module.h>
  29. #include <linux/tty.h>
  30. #include <linux/tty_flip.h>
  31. #include <linux/major.h>
  32. #include <linux/string.h>
  33. #include <linux/fcntl.h>
  34. #include <linux/mm.h>
  35. #include <linux/kernel.h>
  36. #include <linux/delay.h>
  37. #include <linux/init.h>
  38. #include <linux/console.h>
  39. #include <linux/adb.h>
  40. #include <linux/pmu.h>
  41. #include <linux/bitops.h>
  42. #include <linux/sysrq.h>
  43. #include <linux/mutex.h>
  44. #include <linux/of_address.h>
  45. #include <linux/of_irq.h>
  46. #include <asm/sections.h>
  47. #include <asm/io.h>
  48. #include <asm/irq.h>
  49. #ifdef CONFIG_PPC_PMAC
  50. #include <asm/prom.h>
  51. #include <asm/machdep.h>
  52. #include <asm/pmac_feature.h>
  53. #include <asm/dbdma.h>
  54. #include <asm/macio.h>
  55. #else
  56. #include <linux/platform_device.h>
  57. #define of_machine_is_compatible(x) (0)
  58. #endif
  59. #if defined (CONFIG_SERIAL_PMACZILOG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  60. #define SUPPORT_SYSRQ
  61. #endif
  62. #include <linux/serial.h>
  63. #include <linux/serial_core.h>
  64. #include "pmac_zilog.h"
  65. /* Not yet implemented */
  66. #undef HAS_DBDMA
  67. static char version[] __initdata = "pmac_zilog: 0.6 (Benjamin Herrenschmidt <benh@kernel.crashing.org>)";
  68. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  69. MODULE_DESCRIPTION("Driver for the Mac and PowerMac serial ports.");
  70. MODULE_LICENSE("GPL");
  71. #ifdef CONFIG_SERIAL_PMACZILOG_TTYS
  72. #define PMACZILOG_MAJOR TTY_MAJOR
  73. #define PMACZILOG_MINOR 64
  74. #define PMACZILOG_NAME "ttyS"
  75. #else
  76. #define PMACZILOG_MAJOR 204
  77. #define PMACZILOG_MINOR 192
  78. #define PMACZILOG_NAME "ttyPZ"
  79. #endif
  80. #define pmz_debug(fmt, arg...) pr_debug("ttyPZ%d: " fmt, uap->port.line, ## arg)
  81. #define pmz_error(fmt, arg...) pr_err("ttyPZ%d: " fmt, uap->port.line, ## arg)
  82. #define pmz_info(fmt, arg...) pr_info("ttyPZ%d: " fmt, uap->port.line, ## arg)
  83. /*
  84. * For the sake of early serial console, we can do a pre-probe
  85. * (optional) of the ports at rather early boot time.
  86. */
  87. static struct uart_pmac_port pmz_ports[MAX_ZS_PORTS];
  88. static int pmz_ports_count;
  89. static struct uart_driver pmz_uart_reg = {
  90. .owner = THIS_MODULE,
  91. .driver_name = PMACZILOG_NAME,
  92. .dev_name = PMACZILOG_NAME,
  93. .major = PMACZILOG_MAJOR,
  94. .minor = PMACZILOG_MINOR,
  95. };
  96. /*
  97. * Load all registers to reprogram the port
  98. * This function must only be called when the TX is not busy. The UART
  99. * port lock must be held and local interrupts disabled.
  100. */
  101. static void pmz_load_zsregs(struct uart_pmac_port *uap, u8 *regs)
  102. {
  103. int i;
  104. /* Let pending transmits finish. */
  105. for (i = 0; i < 1000; i++) {
  106. unsigned char stat = read_zsreg(uap, R1);
  107. if (stat & ALL_SNT)
  108. break;
  109. udelay(100);
  110. }
  111. ZS_CLEARERR(uap);
  112. zssync(uap);
  113. ZS_CLEARFIFO(uap);
  114. zssync(uap);
  115. ZS_CLEARERR(uap);
  116. /* Disable all interrupts. */
  117. write_zsreg(uap, R1,
  118. regs[R1] & ~(RxINT_MASK | TxINT_ENAB | EXT_INT_ENAB));
  119. /* Set parity, sync config, stop bits, and clock divisor. */
  120. write_zsreg(uap, R4, regs[R4]);
  121. /* Set misc. TX/RX control bits. */
  122. write_zsreg(uap, R10, regs[R10]);
  123. /* Set TX/RX controls sans the enable bits. */
  124. write_zsreg(uap, R3, regs[R3] & ~RxENABLE);
  125. write_zsreg(uap, R5, regs[R5] & ~TxENABLE);
  126. /* now set R7 "prime" on ESCC */
  127. write_zsreg(uap, R15, regs[R15] | EN85C30);
  128. write_zsreg(uap, R7, regs[R7P]);
  129. /* make sure we use R7 "non-prime" on ESCC */
  130. write_zsreg(uap, R15, regs[R15] & ~EN85C30);
  131. /* Synchronous mode config. */
  132. write_zsreg(uap, R6, regs[R6]);
  133. write_zsreg(uap, R7, regs[R7]);
  134. /* Disable baud generator. */
  135. write_zsreg(uap, R14, regs[R14] & ~BRENAB);
  136. /* Clock mode control. */
  137. write_zsreg(uap, R11, regs[R11]);
  138. /* Lower and upper byte of baud rate generator divisor. */
  139. write_zsreg(uap, R12, regs[R12]);
  140. write_zsreg(uap, R13, regs[R13]);
  141. /* Now rewrite R14, with BRENAB (if set). */
  142. write_zsreg(uap, R14, regs[R14]);
  143. /* Reset external status interrupts. */
  144. write_zsreg(uap, R0, RES_EXT_INT);
  145. write_zsreg(uap, R0, RES_EXT_INT);
  146. /* Rewrite R3/R5, this time without enables masked. */
  147. write_zsreg(uap, R3, regs[R3]);
  148. write_zsreg(uap, R5, regs[R5]);
  149. /* Rewrite R1, this time without IRQ enabled masked. */
  150. write_zsreg(uap, R1, regs[R1]);
  151. /* Enable interrupts */
  152. write_zsreg(uap, R9, regs[R9]);
  153. }
  154. /*
  155. * We do like sunzilog to avoid disrupting pending Tx
  156. * Reprogram the Zilog channel HW registers with the copies found in the
  157. * software state struct. If the transmitter is busy, we defer this update
  158. * until the next TX complete interrupt. Else, we do it right now.
  159. *
  160. * The UART port lock must be held and local interrupts disabled.
  161. */
  162. static void pmz_maybe_update_regs(struct uart_pmac_port *uap)
  163. {
  164. if (!ZS_REGS_HELD(uap)) {
  165. if (ZS_TX_ACTIVE(uap)) {
  166. uap->flags |= PMACZILOG_FLAG_REGS_HELD;
  167. } else {
  168. pmz_debug("pmz: maybe_update_regs: updating\n");
  169. pmz_load_zsregs(uap, uap->curregs);
  170. }
  171. }
  172. }
  173. static void pmz_interrupt_control(struct uart_pmac_port *uap, int enable)
  174. {
  175. if (enable) {
  176. uap->curregs[1] |= INT_ALL_Rx | TxINT_ENAB;
  177. if (!ZS_IS_EXTCLK(uap))
  178. uap->curregs[1] |= EXT_INT_ENAB;
  179. } else {
  180. uap->curregs[1] &= ~(EXT_INT_ENAB | TxINT_ENAB | RxINT_MASK);
  181. }
  182. write_zsreg(uap, R1, uap->curregs[1]);
  183. }
  184. static bool pmz_receive_chars(struct uart_pmac_port *uap)
  185. {
  186. struct tty_port *port;
  187. unsigned char ch, r1, drop, flag;
  188. int loops = 0;
  189. /* Sanity check, make sure the old bug is no longer happening */
  190. if (uap->port.state == NULL) {
  191. WARN_ON(1);
  192. (void)read_zsdata(uap);
  193. return false;
  194. }
  195. port = &uap->port.state->port;
  196. while (1) {
  197. drop = 0;
  198. r1 = read_zsreg(uap, R1);
  199. ch = read_zsdata(uap);
  200. if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR)) {
  201. write_zsreg(uap, R0, ERR_RES);
  202. zssync(uap);
  203. }
  204. ch &= uap->parity_mask;
  205. if (ch == 0 && uap->flags & PMACZILOG_FLAG_BREAK) {
  206. uap->flags &= ~PMACZILOG_FLAG_BREAK;
  207. }
  208. #if defined(CONFIG_MAGIC_SYSRQ) && defined(CONFIG_SERIAL_CORE_CONSOLE)
  209. #ifdef USE_CTRL_O_SYSRQ
  210. /* Handle the SysRq ^O Hack */
  211. if (ch == '\x0f') {
  212. uap->port.sysrq = jiffies + HZ*5;
  213. goto next_char;
  214. }
  215. #endif /* USE_CTRL_O_SYSRQ */
  216. if (uap->port.sysrq) {
  217. int swallow;
  218. spin_unlock(&uap->port.lock);
  219. swallow = uart_handle_sysrq_char(&uap->port, ch);
  220. spin_lock(&uap->port.lock);
  221. if (swallow)
  222. goto next_char;
  223. }
  224. #endif /* CONFIG_MAGIC_SYSRQ && CONFIG_SERIAL_CORE_CONSOLE */
  225. /* A real serial line, record the character and status. */
  226. if (drop)
  227. goto next_char;
  228. flag = TTY_NORMAL;
  229. uap->port.icount.rx++;
  230. if (r1 & (PAR_ERR | Rx_OVR | CRC_ERR | BRK_ABRT)) {
  231. if (r1 & BRK_ABRT) {
  232. pmz_debug("pmz: got break !\n");
  233. r1 &= ~(PAR_ERR | CRC_ERR);
  234. uap->port.icount.brk++;
  235. if (uart_handle_break(&uap->port))
  236. goto next_char;
  237. }
  238. else if (r1 & PAR_ERR)
  239. uap->port.icount.parity++;
  240. else if (r1 & CRC_ERR)
  241. uap->port.icount.frame++;
  242. if (r1 & Rx_OVR)
  243. uap->port.icount.overrun++;
  244. r1 &= uap->port.read_status_mask;
  245. if (r1 & BRK_ABRT)
  246. flag = TTY_BREAK;
  247. else if (r1 & PAR_ERR)
  248. flag = TTY_PARITY;
  249. else if (r1 & CRC_ERR)
  250. flag = TTY_FRAME;
  251. }
  252. if (uap->port.ignore_status_mask == 0xff ||
  253. (r1 & uap->port.ignore_status_mask) == 0) {
  254. tty_insert_flip_char(port, ch, flag);
  255. }
  256. if (r1 & Rx_OVR)
  257. tty_insert_flip_char(port, 0, TTY_OVERRUN);
  258. next_char:
  259. /* We can get stuck in an infinite loop getting char 0 when the
  260. * line is in a wrong HW state, we break that here.
  261. * When that happens, I disable the receive side of the driver.
  262. * Note that what I've been experiencing is a real irq loop where
  263. * I'm getting flooded regardless of the actual port speed.
  264. * Something strange is going on with the HW
  265. */
  266. if ((++loops) > 1000)
  267. goto flood;
  268. ch = read_zsreg(uap, R0);
  269. if (!(ch & Rx_CH_AV))
  270. break;
  271. }
  272. return true;
  273. flood:
  274. pmz_interrupt_control(uap, 0);
  275. pmz_error("pmz: rx irq flood !\n");
  276. return true;
  277. }
  278. static void pmz_status_handle(struct uart_pmac_port *uap)
  279. {
  280. unsigned char status;
  281. status = read_zsreg(uap, R0);
  282. write_zsreg(uap, R0, RES_EXT_INT);
  283. zssync(uap);
  284. if (ZS_IS_OPEN(uap) && ZS_WANTS_MODEM_STATUS(uap)) {
  285. if (status & SYNC_HUNT)
  286. uap->port.icount.dsr++;
  287. /* The Zilog just gives us an interrupt when DCD/CTS/etc. change.
  288. * But it does not tell us which bit has changed, we have to keep
  289. * track of this ourselves.
  290. * The CTS input is inverted for some reason. -- paulus
  291. */
  292. if ((status ^ uap->prev_status) & DCD)
  293. uart_handle_dcd_change(&uap->port,
  294. (status & DCD));
  295. if ((status ^ uap->prev_status) & CTS)
  296. uart_handle_cts_change(&uap->port,
  297. !(status & CTS));
  298. wake_up_interruptible(&uap->port.state->port.delta_msr_wait);
  299. }
  300. if (status & BRK_ABRT)
  301. uap->flags |= PMACZILOG_FLAG_BREAK;
  302. uap->prev_status = status;
  303. }
  304. static void pmz_transmit_chars(struct uart_pmac_port *uap)
  305. {
  306. struct circ_buf *xmit;
  307. if (ZS_IS_CONS(uap)) {
  308. unsigned char status = read_zsreg(uap, R0);
  309. /* TX still busy? Just wait for the next TX done interrupt.
  310. *
  311. * It can occur because of how we do serial console writes. It would
  312. * be nice to transmit console writes just like we normally would for
  313. * a TTY line. (ie. buffered and TX interrupt driven). That is not
  314. * easy because console writes cannot sleep. One solution might be
  315. * to poll on enough port->xmit space becoming free. -DaveM
  316. */
  317. if (!(status & Tx_BUF_EMP))
  318. return;
  319. }
  320. uap->flags &= ~PMACZILOG_FLAG_TX_ACTIVE;
  321. if (ZS_REGS_HELD(uap)) {
  322. pmz_load_zsregs(uap, uap->curregs);
  323. uap->flags &= ~PMACZILOG_FLAG_REGS_HELD;
  324. }
  325. if (ZS_TX_STOPPED(uap)) {
  326. uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
  327. goto ack_tx_int;
  328. }
  329. /* Under some circumstances, we see interrupts reported for
  330. * a closed channel. The interrupt mask in R1 is clear, but
  331. * R3 still signals the interrupts and we see them when taking
  332. * an interrupt for the other channel (this could be a qemu
  333. * bug but since the ESCC doc doesn't specify precsiely whether
  334. * R3 interrup status bits are masked by R1 interrupt enable
  335. * bits, better safe than sorry). --BenH.
  336. */
  337. if (!ZS_IS_OPEN(uap))
  338. goto ack_tx_int;
  339. if (uap->port.x_char) {
  340. uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
  341. write_zsdata(uap, uap->port.x_char);
  342. zssync(uap);
  343. uap->port.icount.tx++;
  344. uap->port.x_char = 0;
  345. return;
  346. }
  347. if (uap->port.state == NULL)
  348. goto ack_tx_int;
  349. xmit = &uap->port.state->xmit;
  350. if (uart_circ_empty(xmit)) {
  351. uart_write_wakeup(&uap->port);
  352. goto ack_tx_int;
  353. }
  354. if (uart_tx_stopped(&uap->port))
  355. goto ack_tx_int;
  356. uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
  357. write_zsdata(uap, xmit->buf[xmit->tail]);
  358. zssync(uap);
  359. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  360. uap->port.icount.tx++;
  361. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  362. uart_write_wakeup(&uap->port);
  363. return;
  364. ack_tx_int:
  365. write_zsreg(uap, R0, RES_Tx_P);
  366. zssync(uap);
  367. }
  368. /* Hrm... we register that twice, fixme later.... */
  369. static irqreturn_t pmz_interrupt(int irq, void *dev_id)
  370. {
  371. struct uart_pmac_port *uap = dev_id;
  372. struct uart_pmac_port *uap_a;
  373. struct uart_pmac_port *uap_b;
  374. int rc = IRQ_NONE;
  375. bool push;
  376. u8 r3;
  377. uap_a = pmz_get_port_A(uap);
  378. uap_b = uap_a->mate;
  379. spin_lock(&uap_a->port.lock);
  380. r3 = read_zsreg(uap_a, R3);
  381. #ifdef DEBUG_HARD
  382. pmz_debug("irq, r3: %x\n", r3);
  383. #endif
  384. /* Channel A */
  385. push = false;
  386. if (r3 & (CHAEXT | CHATxIP | CHARxIP)) {
  387. if (!ZS_IS_OPEN(uap_a)) {
  388. pmz_debug("ChanA interrupt while not open !\n");
  389. goto skip_a;
  390. }
  391. write_zsreg(uap_a, R0, RES_H_IUS);
  392. zssync(uap_a);
  393. if (r3 & CHAEXT)
  394. pmz_status_handle(uap_a);
  395. if (r3 & CHARxIP)
  396. push = pmz_receive_chars(uap_a);
  397. if (r3 & CHATxIP)
  398. pmz_transmit_chars(uap_a);
  399. rc = IRQ_HANDLED;
  400. }
  401. skip_a:
  402. spin_unlock(&uap_a->port.lock);
  403. if (push)
  404. tty_flip_buffer_push(&uap->port.state->port);
  405. if (!uap_b)
  406. goto out;
  407. spin_lock(&uap_b->port.lock);
  408. push = false;
  409. if (r3 & (CHBEXT | CHBTxIP | CHBRxIP)) {
  410. if (!ZS_IS_OPEN(uap_b)) {
  411. pmz_debug("ChanB interrupt while not open !\n");
  412. goto skip_b;
  413. }
  414. write_zsreg(uap_b, R0, RES_H_IUS);
  415. zssync(uap_b);
  416. if (r3 & CHBEXT)
  417. pmz_status_handle(uap_b);
  418. if (r3 & CHBRxIP)
  419. push = pmz_receive_chars(uap_b);
  420. if (r3 & CHBTxIP)
  421. pmz_transmit_chars(uap_b);
  422. rc = IRQ_HANDLED;
  423. }
  424. skip_b:
  425. spin_unlock(&uap_b->port.lock);
  426. if (push)
  427. tty_flip_buffer_push(&uap->port.state->port);
  428. out:
  429. return rc;
  430. }
  431. /*
  432. * Peek the status register, lock not held by caller
  433. */
  434. static inline u8 pmz_peek_status(struct uart_pmac_port *uap)
  435. {
  436. unsigned long flags;
  437. u8 status;
  438. spin_lock_irqsave(&uap->port.lock, flags);
  439. status = read_zsreg(uap, R0);
  440. spin_unlock_irqrestore(&uap->port.lock, flags);
  441. return status;
  442. }
  443. /*
  444. * Check if transmitter is empty
  445. * The port lock is not held.
  446. */
  447. static unsigned int pmz_tx_empty(struct uart_port *port)
  448. {
  449. unsigned char status;
  450. status = pmz_peek_status(to_pmz(port));
  451. if (status & Tx_BUF_EMP)
  452. return TIOCSER_TEMT;
  453. return 0;
  454. }
  455. /*
  456. * Set Modem Control (RTS & DTR) bits
  457. * The port lock is held and interrupts are disabled.
  458. * Note: Shall we really filter out RTS on external ports or
  459. * should that be dealt at higher level only ?
  460. */
  461. static void pmz_set_mctrl(struct uart_port *port, unsigned int mctrl)
  462. {
  463. struct uart_pmac_port *uap = to_pmz(port);
  464. unsigned char set_bits, clear_bits;
  465. /* Do nothing for irda for now... */
  466. if (ZS_IS_IRDA(uap))
  467. return;
  468. /* We get called during boot with a port not up yet */
  469. if (!(ZS_IS_OPEN(uap) || ZS_IS_CONS(uap)))
  470. return;
  471. set_bits = clear_bits = 0;
  472. if (ZS_IS_INTMODEM(uap)) {
  473. if (mctrl & TIOCM_RTS)
  474. set_bits |= RTS;
  475. else
  476. clear_bits |= RTS;
  477. }
  478. if (mctrl & TIOCM_DTR)
  479. set_bits |= DTR;
  480. else
  481. clear_bits |= DTR;
  482. /* NOTE: Not subject to 'transmitter active' rule. */
  483. uap->curregs[R5] |= set_bits;
  484. uap->curregs[R5] &= ~clear_bits;
  485. write_zsreg(uap, R5, uap->curregs[R5]);
  486. pmz_debug("pmz_set_mctrl: set bits: %x, clear bits: %x -> %x\n",
  487. set_bits, clear_bits, uap->curregs[R5]);
  488. zssync(uap);
  489. }
  490. /*
  491. * Get Modem Control bits (only the input ones, the core will
  492. * or that with a cached value of the control ones)
  493. * The port lock is held and interrupts are disabled.
  494. */
  495. static unsigned int pmz_get_mctrl(struct uart_port *port)
  496. {
  497. struct uart_pmac_port *uap = to_pmz(port);
  498. unsigned char status;
  499. unsigned int ret;
  500. status = read_zsreg(uap, R0);
  501. ret = 0;
  502. if (status & DCD)
  503. ret |= TIOCM_CAR;
  504. if (status & SYNC_HUNT)
  505. ret |= TIOCM_DSR;
  506. if (!(status & CTS))
  507. ret |= TIOCM_CTS;
  508. return ret;
  509. }
  510. /*
  511. * Stop TX side. Dealt like sunzilog at next Tx interrupt,
  512. * though for DMA, we will have to do a bit more.
  513. * The port lock is held and interrupts are disabled.
  514. */
  515. static void pmz_stop_tx(struct uart_port *port)
  516. {
  517. to_pmz(port)->flags |= PMACZILOG_FLAG_TX_STOPPED;
  518. }
  519. /*
  520. * Kick the Tx side.
  521. * The port lock is held and interrupts are disabled.
  522. */
  523. static void pmz_start_tx(struct uart_port *port)
  524. {
  525. struct uart_pmac_port *uap = to_pmz(port);
  526. unsigned char status;
  527. pmz_debug("pmz: start_tx()\n");
  528. uap->flags |= PMACZILOG_FLAG_TX_ACTIVE;
  529. uap->flags &= ~PMACZILOG_FLAG_TX_STOPPED;
  530. status = read_zsreg(uap, R0);
  531. /* TX busy? Just wait for the TX done interrupt. */
  532. if (!(status & Tx_BUF_EMP))
  533. return;
  534. /* Send the first character to jump-start the TX done
  535. * IRQ sending engine.
  536. */
  537. if (port->x_char) {
  538. write_zsdata(uap, port->x_char);
  539. zssync(uap);
  540. port->icount.tx++;
  541. port->x_char = 0;
  542. } else {
  543. struct circ_buf *xmit = &port->state->xmit;
  544. if (uart_circ_empty(xmit))
  545. goto out;
  546. write_zsdata(uap, xmit->buf[xmit->tail]);
  547. zssync(uap);
  548. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  549. port->icount.tx++;
  550. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  551. uart_write_wakeup(&uap->port);
  552. }
  553. out:
  554. pmz_debug("pmz: start_tx() done.\n");
  555. }
  556. /*
  557. * Stop Rx side, basically disable emitting of
  558. * Rx interrupts on the port. We don't disable the rx
  559. * side of the chip proper though
  560. * The port lock is held.
  561. */
  562. static void pmz_stop_rx(struct uart_port *port)
  563. {
  564. struct uart_pmac_port *uap = to_pmz(port);
  565. pmz_debug("pmz: stop_rx()()\n");
  566. /* Disable all RX interrupts. */
  567. uap->curregs[R1] &= ~RxINT_MASK;
  568. pmz_maybe_update_regs(uap);
  569. pmz_debug("pmz: stop_rx() done.\n");
  570. }
  571. /*
  572. * Enable modem status change interrupts
  573. * The port lock is held.
  574. */
  575. static void pmz_enable_ms(struct uart_port *port)
  576. {
  577. struct uart_pmac_port *uap = to_pmz(port);
  578. unsigned char new_reg;
  579. if (ZS_IS_IRDA(uap))
  580. return;
  581. new_reg = uap->curregs[R15] | (DCDIE | SYNCIE | CTSIE);
  582. if (new_reg != uap->curregs[R15]) {
  583. uap->curregs[R15] = new_reg;
  584. /* NOTE: Not subject to 'transmitter active' rule. */
  585. write_zsreg(uap, R15, uap->curregs[R15]);
  586. }
  587. }
  588. /*
  589. * Control break state emission
  590. * The port lock is not held.
  591. */
  592. static void pmz_break_ctl(struct uart_port *port, int break_state)
  593. {
  594. struct uart_pmac_port *uap = to_pmz(port);
  595. unsigned char set_bits, clear_bits, new_reg;
  596. unsigned long flags;
  597. set_bits = clear_bits = 0;
  598. if (break_state)
  599. set_bits |= SND_BRK;
  600. else
  601. clear_bits |= SND_BRK;
  602. spin_lock_irqsave(&port->lock, flags);
  603. new_reg = (uap->curregs[R5] | set_bits) & ~clear_bits;
  604. if (new_reg != uap->curregs[R5]) {
  605. uap->curregs[R5] = new_reg;
  606. write_zsreg(uap, R5, uap->curregs[R5]);
  607. }
  608. spin_unlock_irqrestore(&port->lock, flags);
  609. }
  610. #ifdef CONFIG_PPC_PMAC
  611. /*
  612. * Turn power on or off to the SCC and associated stuff
  613. * (port drivers, modem, IR port, etc.)
  614. * Returns the number of milliseconds we should wait before
  615. * trying to use the port.
  616. */
  617. static int pmz_set_scc_power(struct uart_pmac_port *uap, int state)
  618. {
  619. int delay = 0;
  620. int rc;
  621. if (state) {
  622. rc = pmac_call_feature(
  623. PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 1);
  624. pmz_debug("port power on result: %d\n", rc);
  625. if (ZS_IS_INTMODEM(uap)) {
  626. rc = pmac_call_feature(
  627. PMAC_FTR_MODEM_ENABLE, uap->node, 0, 1);
  628. delay = 2500; /* wait for 2.5s before using */
  629. pmz_debug("modem power result: %d\n", rc);
  630. }
  631. } else {
  632. /* TODO: Make that depend on a timer, don't power down
  633. * immediately
  634. */
  635. if (ZS_IS_INTMODEM(uap)) {
  636. rc = pmac_call_feature(
  637. PMAC_FTR_MODEM_ENABLE, uap->node, 0, 0);
  638. pmz_debug("port power off result: %d\n", rc);
  639. }
  640. pmac_call_feature(PMAC_FTR_SCC_ENABLE, uap->node, uap->port_type, 0);
  641. }
  642. return delay;
  643. }
  644. #else
  645. static int pmz_set_scc_power(struct uart_pmac_port *uap, int state)
  646. {
  647. return 0;
  648. }
  649. #endif /* !CONFIG_PPC_PMAC */
  650. /*
  651. * FixZeroBug....Works around a bug in the SCC receiving channel.
  652. * Inspired from Darwin code, 15 Sept. 2000 -DanM
  653. *
  654. * The following sequence prevents a problem that is seen with O'Hare ASICs
  655. * (most versions -- also with some Heathrow and Hydra ASICs) where a zero
  656. * at the input to the receiver becomes 'stuck' and locks up the receiver.
  657. * This problem can occur as a result of a zero bit at the receiver input
  658. * coincident with any of the following events:
  659. *
  660. * The SCC is initialized (hardware or software).
  661. * A framing error is detected.
  662. * The clocking option changes from synchronous or X1 asynchronous
  663. * clocking to X16, X32, or X64 asynchronous clocking.
  664. * The decoding mode is changed among NRZ, NRZI, FM0, or FM1.
  665. *
  666. * This workaround attempts to recover from the lockup condition by placing
  667. * the SCC in synchronous loopback mode with a fast clock before programming
  668. * any of the asynchronous modes.
  669. */
  670. static void pmz_fix_zero_bug_scc(struct uart_pmac_port *uap)
  671. {
  672. write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
  673. zssync(uap);
  674. udelay(10);
  675. write_zsreg(uap, 9, (ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB) | NV);
  676. zssync(uap);
  677. write_zsreg(uap, 4, X1CLK | MONSYNC);
  678. write_zsreg(uap, 3, Rx8);
  679. write_zsreg(uap, 5, Tx8 | RTS);
  680. write_zsreg(uap, 9, NV); /* Didn't we already do this? */
  681. write_zsreg(uap, 11, RCBR | TCBR);
  682. write_zsreg(uap, 12, 0);
  683. write_zsreg(uap, 13, 0);
  684. write_zsreg(uap, 14, (LOOPBAK | BRSRC));
  685. write_zsreg(uap, 14, (LOOPBAK | BRSRC | BRENAB));
  686. write_zsreg(uap, 3, Rx8 | RxENABLE);
  687. write_zsreg(uap, 0, RES_EXT_INT);
  688. write_zsreg(uap, 0, RES_EXT_INT);
  689. write_zsreg(uap, 0, RES_EXT_INT); /* to kill some time */
  690. /* The channel should be OK now, but it is probably receiving
  691. * loopback garbage.
  692. * Switch to asynchronous mode, disable the receiver,
  693. * and discard everything in the receive buffer.
  694. */
  695. write_zsreg(uap, 9, NV);
  696. write_zsreg(uap, 4, X16CLK | SB_MASK);
  697. write_zsreg(uap, 3, Rx8);
  698. while (read_zsreg(uap, 0) & Rx_CH_AV) {
  699. (void)read_zsreg(uap, 8);
  700. write_zsreg(uap, 0, RES_EXT_INT);
  701. write_zsreg(uap, 0, ERR_RES);
  702. }
  703. }
  704. /*
  705. * Real startup routine, powers up the hardware and sets up
  706. * the SCC. Returns a delay in ms where you need to wait before
  707. * actually using the port, this is typically the internal modem
  708. * powerup delay. This routine expect the lock to be taken.
  709. */
  710. static int __pmz_startup(struct uart_pmac_port *uap)
  711. {
  712. int pwr_delay = 0;
  713. memset(&uap->curregs, 0, sizeof(uap->curregs));
  714. /* Power up the SCC & underlying hardware (modem/irda) */
  715. pwr_delay = pmz_set_scc_power(uap, 1);
  716. /* Nice buggy HW ... */
  717. pmz_fix_zero_bug_scc(uap);
  718. /* Reset the channel */
  719. uap->curregs[R9] = 0;
  720. write_zsreg(uap, 9, ZS_IS_CHANNEL_A(uap) ? CHRA : CHRB);
  721. zssync(uap);
  722. udelay(10);
  723. write_zsreg(uap, 9, 0);
  724. zssync(uap);
  725. /* Clear the interrupt registers */
  726. write_zsreg(uap, R1, 0);
  727. write_zsreg(uap, R0, ERR_RES);
  728. write_zsreg(uap, R0, ERR_RES);
  729. write_zsreg(uap, R0, RES_H_IUS);
  730. write_zsreg(uap, R0, RES_H_IUS);
  731. /* Setup some valid baud rate */
  732. uap->curregs[R4] = X16CLK | SB1;
  733. uap->curregs[R3] = Rx8;
  734. uap->curregs[R5] = Tx8 | RTS;
  735. if (!ZS_IS_IRDA(uap))
  736. uap->curregs[R5] |= DTR;
  737. uap->curregs[R12] = 0;
  738. uap->curregs[R13] = 0;
  739. uap->curregs[R14] = BRENAB;
  740. /* Clear handshaking, enable BREAK interrupts */
  741. uap->curregs[R15] = BRKIE;
  742. /* Master interrupt enable */
  743. uap->curregs[R9] |= NV | MIE;
  744. pmz_load_zsregs(uap, uap->curregs);
  745. /* Enable receiver and transmitter. */
  746. write_zsreg(uap, R3, uap->curregs[R3] |= RxENABLE);
  747. write_zsreg(uap, R5, uap->curregs[R5] |= TxENABLE);
  748. /* Remember status for DCD/CTS changes */
  749. uap->prev_status = read_zsreg(uap, R0);
  750. return pwr_delay;
  751. }
  752. static void pmz_irda_reset(struct uart_pmac_port *uap)
  753. {
  754. unsigned long flags;
  755. spin_lock_irqsave(&uap->port.lock, flags);
  756. uap->curregs[R5] |= DTR;
  757. write_zsreg(uap, R5, uap->curregs[R5]);
  758. zssync(uap);
  759. spin_unlock_irqrestore(&uap->port.lock, flags);
  760. msleep(110);
  761. spin_lock_irqsave(&uap->port.lock, flags);
  762. uap->curregs[R5] &= ~DTR;
  763. write_zsreg(uap, R5, uap->curregs[R5]);
  764. zssync(uap);
  765. spin_unlock_irqrestore(&uap->port.lock, flags);
  766. msleep(10);
  767. }
  768. /*
  769. * This is the "normal" startup routine, using the above one
  770. * wrapped with the lock and doing a schedule delay
  771. */
  772. static int pmz_startup(struct uart_port *port)
  773. {
  774. struct uart_pmac_port *uap = to_pmz(port);
  775. unsigned long flags;
  776. int pwr_delay = 0;
  777. pmz_debug("pmz: startup()\n");
  778. uap->flags |= PMACZILOG_FLAG_IS_OPEN;
  779. /* A console is never powered down. Else, power up and
  780. * initialize the chip
  781. */
  782. if (!ZS_IS_CONS(uap)) {
  783. spin_lock_irqsave(&port->lock, flags);
  784. pwr_delay = __pmz_startup(uap);
  785. spin_unlock_irqrestore(&port->lock, flags);
  786. }
  787. sprintf(uap->irq_name, PMACZILOG_NAME"%d", uap->port.line);
  788. if (request_irq(uap->port.irq, pmz_interrupt, IRQF_SHARED,
  789. uap->irq_name, uap)) {
  790. pmz_error("Unable to register zs interrupt handler.\n");
  791. pmz_set_scc_power(uap, 0);
  792. return -ENXIO;
  793. }
  794. /* Right now, we deal with delay by blocking here, I'll be
  795. * smarter later on
  796. */
  797. if (pwr_delay != 0) {
  798. pmz_debug("pmz: delaying %d ms\n", pwr_delay);
  799. msleep(pwr_delay);
  800. }
  801. /* IrDA reset is done now */
  802. if (ZS_IS_IRDA(uap))
  803. pmz_irda_reset(uap);
  804. /* Enable interrupt requests for the channel */
  805. spin_lock_irqsave(&port->lock, flags);
  806. pmz_interrupt_control(uap, 1);
  807. spin_unlock_irqrestore(&port->lock, flags);
  808. pmz_debug("pmz: startup() done.\n");
  809. return 0;
  810. }
  811. static void pmz_shutdown(struct uart_port *port)
  812. {
  813. struct uart_pmac_port *uap = to_pmz(port);
  814. unsigned long flags;
  815. pmz_debug("pmz: shutdown()\n");
  816. spin_lock_irqsave(&port->lock, flags);
  817. /* Disable interrupt requests for the channel */
  818. pmz_interrupt_control(uap, 0);
  819. if (!ZS_IS_CONS(uap)) {
  820. /* Disable receiver and transmitter */
  821. uap->curregs[R3] &= ~RxENABLE;
  822. uap->curregs[R5] &= ~TxENABLE;
  823. /* Disable break assertion */
  824. uap->curregs[R5] &= ~SND_BRK;
  825. pmz_maybe_update_regs(uap);
  826. }
  827. spin_unlock_irqrestore(&port->lock, flags);
  828. /* Release interrupt handler */
  829. free_irq(uap->port.irq, uap);
  830. spin_lock_irqsave(&port->lock, flags);
  831. uap->flags &= ~PMACZILOG_FLAG_IS_OPEN;
  832. if (!ZS_IS_CONS(uap))
  833. pmz_set_scc_power(uap, 0); /* Shut the chip down */
  834. spin_unlock_irqrestore(&port->lock, flags);
  835. pmz_debug("pmz: shutdown() done.\n");
  836. }
  837. /* Shared by TTY driver and serial console setup. The port lock is held
  838. * and local interrupts are disabled.
  839. */
  840. static void pmz_convert_to_zs(struct uart_pmac_port *uap, unsigned int cflag,
  841. unsigned int iflag, unsigned long baud)
  842. {
  843. int brg;
  844. /* Switch to external clocking for IrDA high clock rates. That
  845. * code could be re-used for Midi interfaces with different
  846. * multipliers
  847. */
  848. if (baud >= 115200 && ZS_IS_IRDA(uap)) {
  849. uap->curregs[R4] = X1CLK;
  850. uap->curregs[R11] = RCTRxCP | TCTRxCP;
  851. uap->curregs[R14] = 0; /* BRG off */
  852. uap->curregs[R12] = 0;
  853. uap->curregs[R13] = 0;
  854. uap->flags |= PMACZILOG_FLAG_IS_EXTCLK;
  855. } else {
  856. switch (baud) {
  857. case ZS_CLOCK/16: /* 230400 */
  858. uap->curregs[R4] = X16CLK;
  859. uap->curregs[R11] = 0;
  860. uap->curregs[R14] = 0;
  861. break;
  862. case ZS_CLOCK/32: /* 115200 */
  863. uap->curregs[R4] = X32CLK;
  864. uap->curregs[R11] = 0;
  865. uap->curregs[R14] = 0;
  866. break;
  867. default:
  868. uap->curregs[R4] = X16CLK;
  869. uap->curregs[R11] = TCBR | RCBR;
  870. brg = BPS_TO_BRG(baud, ZS_CLOCK / 16);
  871. uap->curregs[R12] = (brg & 255);
  872. uap->curregs[R13] = ((brg >> 8) & 255);
  873. uap->curregs[R14] = BRENAB;
  874. }
  875. uap->flags &= ~PMACZILOG_FLAG_IS_EXTCLK;
  876. }
  877. /* Character size, stop bits, and parity. */
  878. uap->curregs[3] &= ~RxN_MASK;
  879. uap->curregs[5] &= ~TxN_MASK;
  880. switch (cflag & CSIZE) {
  881. case CS5:
  882. uap->curregs[3] |= Rx5;
  883. uap->curregs[5] |= Tx5;
  884. uap->parity_mask = 0x1f;
  885. break;
  886. case CS6:
  887. uap->curregs[3] |= Rx6;
  888. uap->curregs[5] |= Tx6;
  889. uap->parity_mask = 0x3f;
  890. break;
  891. case CS7:
  892. uap->curregs[3] |= Rx7;
  893. uap->curregs[5] |= Tx7;
  894. uap->parity_mask = 0x7f;
  895. break;
  896. case CS8:
  897. default:
  898. uap->curregs[3] |= Rx8;
  899. uap->curregs[5] |= Tx8;
  900. uap->parity_mask = 0xff;
  901. break;
  902. }
  903. uap->curregs[4] &= ~(SB_MASK);
  904. if (cflag & CSTOPB)
  905. uap->curregs[4] |= SB2;
  906. else
  907. uap->curregs[4] |= SB1;
  908. if (cflag & PARENB)
  909. uap->curregs[4] |= PAR_ENAB;
  910. else
  911. uap->curregs[4] &= ~PAR_ENAB;
  912. if (!(cflag & PARODD))
  913. uap->curregs[4] |= PAR_EVEN;
  914. else
  915. uap->curregs[4] &= ~PAR_EVEN;
  916. uap->port.read_status_mask = Rx_OVR;
  917. if (iflag & INPCK)
  918. uap->port.read_status_mask |= CRC_ERR | PAR_ERR;
  919. if (iflag & (IGNBRK | BRKINT | PARMRK))
  920. uap->port.read_status_mask |= BRK_ABRT;
  921. uap->port.ignore_status_mask = 0;
  922. if (iflag & IGNPAR)
  923. uap->port.ignore_status_mask |= CRC_ERR | PAR_ERR;
  924. if (iflag & IGNBRK) {
  925. uap->port.ignore_status_mask |= BRK_ABRT;
  926. if (iflag & IGNPAR)
  927. uap->port.ignore_status_mask |= Rx_OVR;
  928. }
  929. if ((cflag & CREAD) == 0)
  930. uap->port.ignore_status_mask = 0xff;
  931. }
  932. /*
  933. * Set the irda codec on the imac to the specified baud rate.
  934. */
  935. static void pmz_irda_setup(struct uart_pmac_port *uap, unsigned long *baud)
  936. {
  937. u8 cmdbyte;
  938. int t, version;
  939. switch (*baud) {
  940. /* SIR modes */
  941. case 2400:
  942. cmdbyte = 0x53;
  943. break;
  944. case 4800:
  945. cmdbyte = 0x52;
  946. break;
  947. case 9600:
  948. cmdbyte = 0x51;
  949. break;
  950. case 19200:
  951. cmdbyte = 0x50;
  952. break;
  953. case 38400:
  954. cmdbyte = 0x4f;
  955. break;
  956. case 57600:
  957. cmdbyte = 0x4e;
  958. break;
  959. case 115200:
  960. cmdbyte = 0x4d;
  961. break;
  962. /* The FIR modes aren't really supported at this point, how
  963. * do we select the speed ? via the FCR on KeyLargo ?
  964. */
  965. case 1152000:
  966. cmdbyte = 0;
  967. break;
  968. case 4000000:
  969. cmdbyte = 0;
  970. break;
  971. default: /* 9600 */
  972. cmdbyte = 0x51;
  973. *baud = 9600;
  974. break;
  975. }
  976. /* Wait for transmitter to drain */
  977. t = 10000;
  978. while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0
  979. || (read_zsreg(uap, R1) & ALL_SNT) == 0) {
  980. if (--t <= 0) {
  981. pmz_error("transmitter didn't drain\n");
  982. return;
  983. }
  984. udelay(10);
  985. }
  986. /* Drain the receiver too */
  987. t = 100;
  988. (void)read_zsdata(uap);
  989. (void)read_zsdata(uap);
  990. (void)read_zsdata(uap);
  991. mdelay(10);
  992. while (read_zsreg(uap, R0) & Rx_CH_AV) {
  993. read_zsdata(uap);
  994. mdelay(10);
  995. if (--t <= 0) {
  996. pmz_error("receiver didn't drain\n");
  997. return;
  998. }
  999. }
  1000. /* Switch to command mode */
  1001. uap->curregs[R5] |= DTR;
  1002. write_zsreg(uap, R5, uap->curregs[R5]);
  1003. zssync(uap);
  1004. mdelay(1);
  1005. /* Switch SCC to 19200 */
  1006. pmz_convert_to_zs(uap, CS8, 0, 19200);
  1007. pmz_load_zsregs(uap, uap->curregs);
  1008. mdelay(1);
  1009. /* Write get_version command byte */
  1010. write_zsdata(uap, 1);
  1011. t = 5000;
  1012. while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
  1013. if (--t <= 0) {
  1014. pmz_error("irda_setup timed out on get_version byte\n");
  1015. goto out;
  1016. }
  1017. udelay(10);
  1018. }
  1019. version = read_zsdata(uap);
  1020. if (version < 4) {
  1021. pmz_info("IrDA: dongle version %d not supported\n", version);
  1022. goto out;
  1023. }
  1024. /* Send speed mode */
  1025. write_zsdata(uap, cmdbyte);
  1026. t = 5000;
  1027. while ((read_zsreg(uap, R0) & Rx_CH_AV) == 0) {
  1028. if (--t <= 0) {
  1029. pmz_error("irda_setup timed out on speed mode byte\n");
  1030. goto out;
  1031. }
  1032. udelay(10);
  1033. }
  1034. t = read_zsdata(uap);
  1035. if (t != cmdbyte)
  1036. pmz_error("irda_setup speed mode byte = %x (%x)\n", t, cmdbyte);
  1037. pmz_info("IrDA setup for %ld bps, dongle version: %d\n",
  1038. *baud, version);
  1039. (void)read_zsdata(uap);
  1040. (void)read_zsdata(uap);
  1041. (void)read_zsdata(uap);
  1042. out:
  1043. /* Switch back to data mode */
  1044. uap->curregs[R5] &= ~DTR;
  1045. write_zsreg(uap, R5, uap->curregs[R5]);
  1046. zssync(uap);
  1047. (void)read_zsdata(uap);
  1048. (void)read_zsdata(uap);
  1049. (void)read_zsdata(uap);
  1050. }
  1051. static void __pmz_set_termios(struct uart_port *port, struct ktermios *termios,
  1052. struct ktermios *old)
  1053. {
  1054. struct uart_pmac_port *uap = to_pmz(port);
  1055. unsigned long baud;
  1056. pmz_debug("pmz: set_termios()\n");
  1057. memcpy(&uap->termios_cache, termios, sizeof(struct ktermios));
  1058. /* XXX Check which revs of machines actually allow 1 and 4Mb speeds
  1059. * on the IR dongle. Note that the IRTTY driver currently doesn't know
  1060. * about the FIR mode and high speed modes. So these are unused. For
  1061. * implementing proper support for these, we should probably add some
  1062. * DMA as well, at least on the Rx side, which isn't a simple thing
  1063. * at this point.
  1064. */
  1065. if (ZS_IS_IRDA(uap)) {
  1066. /* Calc baud rate */
  1067. baud = uart_get_baud_rate(port, termios, old, 1200, 4000000);
  1068. pmz_debug("pmz: switch IRDA to %ld bauds\n", baud);
  1069. /* Cet the irda codec to the right rate */
  1070. pmz_irda_setup(uap, &baud);
  1071. /* Set final baud rate */
  1072. pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
  1073. pmz_load_zsregs(uap, uap->curregs);
  1074. zssync(uap);
  1075. } else {
  1076. baud = uart_get_baud_rate(port, termios, old, 1200, 230400);
  1077. pmz_convert_to_zs(uap, termios->c_cflag, termios->c_iflag, baud);
  1078. /* Make sure modem status interrupts are correctly configured */
  1079. if (UART_ENABLE_MS(&uap->port, termios->c_cflag)) {
  1080. uap->curregs[R15] |= DCDIE | SYNCIE | CTSIE;
  1081. uap->flags |= PMACZILOG_FLAG_MODEM_STATUS;
  1082. } else {
  1083. uap->curregs[R15] &= ~(DCDIE | SYNCIE | CTSIE);
  1084. uap->flags &= ~PMACZILOG_FLAG_MODEM_STATUS;
  1085. }
  1086. /* Load registers to the chip */
  1087. pmz_maybe_update_regs(uap);
  1088. }
  1089. uart_update_timeout(port, termios->c_cflag, baud);
  1090. pmz_debug("pmz: set_termios() done.\n");
  1091. }
  1092. /* The port lock is not held. */
  1093. static void pmz_set_termios(struct uart_port *port, struct ktermios *termios,
  1094. struct ktermios *old)
  1095. {
  1096. struct uart_pmac_port *uap = to_pmz(port);
  1097. unsigned long flags;
  1098. spin_lock_irqsave(&port->lock, flags);
  1099. /* Disable IRQs on the port */
  1100. pmz_interrupt_control(uap, 0);
  1101. /* Setup new port configuration */
  1102. __pmz_set_termios(port, termios, old);
  1103. /* Re-enable IRQs on the port */
  1104. if (ZS_IS_OPEN(uap))
  1105. pmz_interrupt_control(uap, 1);
  1106. spin_unlock_irqrestore(&port->lock, flags);
  1107. }
  1108. static const char *pmz_type(struct uart_port *port)
  1109. {
  1110. struct uart_pmac_port *uap = to_pmz(port);
  1111. if (ZS_IS_IRDA(uap))
  1112. return "Z85c30 ESCC - Infrared port";
  1113. else if (ZS_IS_INTMODEM(uap))
  1114. return "Z85c30 ESCC - Internal modem";
  1115. return "Z85c30 ESCC - Serial port";
  1116. }
  1117. /* We do not request/release mappings of the registers here, this
  1118. * happens at early serial probe time.
  1119. */
  1120. static void pmz_release_port(struct uart_port *port)
  1121. {
  1122. }
  1123. static int pmz_request_port(struct uart_port *port)
  1124. {
  1125. return 0;
  1126. }
  1127. /* These do not need to do anything interesting either. */
  1128. static void pmz_config_port(struct uart_port *port, int flags)
  1129. {
  1130. }
  1131. /* We do not support letting the user mess with the divisor, IRQ, etc. */
  1132. static int pmz_verify_port(struct uart_port *port, struct serial_struct *ser)
  1133. {
  1134. return -EINVAL;
  1135. }
  1136. #ifdef CONFIG_CONSOLE_POLL
  1137. static int pmz_poll_get_char(struct uart_port *port)
  1138. {
  1139. struct uart_pmac_port *uap =
  1140. container_of(port, struct uart_pmac_port, port);
  1141. int tries = 2;
  1142. while (tries) {
  1143. if ((read_zsreg(uap, R0) & Rx_CH_AV) != 0)
  1144. return read_zsdata(uap);
  1145. if (tries--)
  1146. udelay(5);
  1147. }
  1148. return NO_POLL_CHAR;
  1149. }
  1150. static void pmz_poll_put_char(struct uart_port *port, unsigned char c)
  1151. {
  1152. struct uart_pmac_port *uap =
  1153. container_of(port, struct uart_pmac_port, port);
  1154. /* Wait for the transmit buffer to empty. */
  1155. while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0)
  1156. udelay(5);
  1157. write_zsdata(uap, c);
  1158. }
  1159. #endif /* CONFIG_CONSOLE_POLL */
  1160. static const struct uart_ops pmz_pops = {
  1161. .tx_empty = pmz_tx_empty,
  1162. .set_mctrl = pmz_set_mctrl,
  1163. .get_mctrl = pmz_get_mctrl,
  1164. .stop_tx = pmz_stop_tx,
  1165. .start_tx = pmz_start_tx,
  1166. .stop_rx = pmz_stop_rx,
  1167. .enable_ms = pmz_enable_ms,
  1168. .break_ctl = pmz_break_ctl,
  1169. .startup = pmz_startup,
  1170. .shutdown = pmz_shutdown,
  1171. .set_termios = pmz_set_termios,
  1172. .type = pmz_type,
  1173. .release_port = pmz_release_port,
  1174. .request_port = pmz_request_port,
  1175. .config_port = pmz_config_port,
  1176. .verify_port = pmz_verify_port,
  1177. #ifdef CONFIG_CONSOLE_POLL
  1178. .poll_get_char = pmz_poll_get_char,
  1179. .poll_put_char = pmz_poll_put_char,
  1180. #endif
  1181. };
  1182. #ifdef CONFIG_PPC_PMAC
  1183. /*
  1184. * Setup one port structure after probing, HW is down at this point,
  1185. * Unlike sunzilog, we don't need to pre-init the spinlock as we don't
  1186. * register our console before uart_add_one_port() is called
  1187. */
  1188. static int __init pmz_init_port(struct uart_pmac_port *uap)
  1189. {
  1190. struct device_node *np = uap->node;
  1191. const char *conn;
  1192. const struct slot_names_prop {
  1193. int count;
  1194. char name[1];
  1195. } *slots;
  1196. int len;
  1197. struct resource r_ports, r_rxdma, r_txdma;
  1198. /*
  1199. * Request & map chip registers
  1200. */
  1201. if (of_address_to_resource(np, 0, &r_ports))
  1202. return -ENODEV;
  1203. uap->port.mapbase = r_ports.start;
  1204. uap->port.membase = ioremap(uap->port.mapbase, 0x1000);
  1205. uap->control_reg = uap->port.membase;
  1206. uap->data_reg = uap->control_reg + 0x10;
  1207. /*
  1208. * Request & map DBDMA registers
  1209. */
  1210. #ifdef HAS_DBDMA
  1211. if (of_address_to_resource(np, 1, &r_txdma) == 0 &&
  1212. of_address_to_resource(np, 2, &r_rxdma) == 0)
  1213. uap->flags |= PMACZILOG_FLAG_HAS_DMA;
  1214. #else
  1215. memset(&r_txdma, 0, sizeof(struct resource));
  1216. memset(&r_rxdma, 0, sizeof(struct resource));
  1217. #endif
  1218. if (ZS_HAS_DMA(uap)) {
  1219. uap->tx_dma_regs = ioremap(r_txdma.start, 0x100);
  1220. if (uap->tx_dma_regs == NULL) {
  1221. uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
  1222. goto no_dma;
  1223. }
  1224. uap->rx_dma_regs = ioremap(r_rxdma.start, 0x100);
  1225. if (uap->rx_dma_regs == NULL) {
  1226. iounmap(uap->tx_dma_regs);
  1227. uap->tx_dma_regs = NULL;
  1228. uap->flags &= ~PMACZILOG_FLAG_HAS_DMA;
  1229. goto no_dma;
  1230. }
  1231. uap->tx_dma_irq = irq_of_parse_and_map(np, 1);
  1232. uap->rx_dma_irq = irq_of_parse_and_map(np, 2);
  1233. }
  1234. no_dma:
  1235. /*
  1236. * Detect port type
  1237. */
  1238. if (of_device_is_compatible(np, "cobalt"))
  1239. uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
  1240. conn = of_get_property(np, "AAPL,connector", &len);
  1241. if (conn && (strcmp(conn, "infrared") == 0))
  1242. uap->flags |= PMACZILOG_FLAG_IS_IRDA;
  1243. uap->port_type = PMAC_SCC_ASYNC;
  1244. /* 1999 Powerbook G3 has slot-names property instead */
  1245. slots = of_get_property(np, "slot-names", &len);
  1246. if (slots && slots->count > 0) {
  1247. if (strcmp(slots->name, "IrDA") == 0)
  1248. uap->flags |= PMACZILOG_FLAG_IS_IRDA;
  1249. else if (strcmp(slots->name, "Modem") == 0)
  1250. uap->flags |= PMACZILOG_FLAG_IS_INTMODEM;
  1251. }
  1252. if (ZS_IS_IRDA(uap))
  1253. uap->port_type = PMAC_SCC_IRDA;
  1254. if (ZS_IS_INTMODEM(uap)) {
  1255. struct device_node* i2c_modem =
  1256. of_find_node_by_name(NULL, "i2c-modem");
  1257. if (i2c_modem) {
  1258. const char* mid =
  1259. of_get_property(i2c_modem, "modem-id", NULL);
  1260. if (mid) switch(*mid) {
  1261. case 0x04 :
  1262. case 0x05 :
  1263. case 0x07 :
  1264. case 0x08 :
  1265. case 0x0b :
  1266. case 0x0c :
  1267. uap->port_type = PMAC_SCC_I2S1;
  1268. }
  1269. printk(KERN_INFO "pmac_zilog: i2c-modem detected, id: %d\n",
  1270. mid ? (*mid) : 0);
  1271. of_node_put(i2c_modem);
  1272. } else {
  1273. printk(KERN_INFO "pmac_zilog: serial modem detected\n");
  1274. }
  1275. }
  1276. /*
  1277. * Init remaining bits of "port" structure
  1278. */
  1279. uap->port.iotype = UPIO_MEM;
  1280. uap->port.irq = irq_of_parse_and_map(np, 0);
  1281. uap->port.uartclk = ZS_CLOCK;
  1282. uap->port.fifosize = 1;
  1283. uap->port.ops = &pmz_pops;
  1284. uap->port.type = PORT_PMAC_ZILOG;
  1285. uap->port.flags = 0;
  1286. /*
  1287. * Fixup for the port on Gatwick for which the device-tree has
  1288. * missing interrupts. Normally, the macio_dev would contain
  1289. * fixed up interrupt info, but we use the device-tree directly
  1290. * here due to early probing so we need the fixup too.
  1291. */
  1292. if (uap->port.irq == 0 &&
  1293. np->parent && np->parent->parent &&
  1294. of_device_is_compatible(np->parent->parent, "gatwick")) {
  1295. /* IRQs on gatwick are offset by 64 */
  1296. uap->port.irq = irq_create_mapping(NULL, 64 + 15);
  1297. uap->tx_dma_irq = irq_create_mapping(NULL, 64 + 4);
  1298. uap->rx_dma_irq = irq_create_mapping(NULL, 64 + 5);
  1299. }
  1300. /* Setup some valid baud rate information in the register
  1301. * shadows so we don't write crap there before baud rate is
  1302. * first initialized.
  1303. */
  1304. pmz_convert_to_zs(uap, CS8, 0, 9600);
  1305. return 0;
  1306. }
  1307. /*
  1308. * Get rid of a port on module removal
  1309. */
  1310. static void pmz_dispose_port(struct uart_pmac_port *uap)
  1311. {
  1312. struct device_node *np;
  1313. np = uap->node;
  1314. iounmap(uap->rx_dma_regs);
  1315. iounmap(uap->tx_dma_regs);
  1316. iounmap(uap->control_reg);
  1317. uap->node = NULL;
  1318. of_node_put(np);
  1319. memset(uap, 0, sizeof(struct uart_pmac_port));
  1320. }
  1321. /*
  1322. * Called upon match with an escc node in the device-tree.
  1323. */
  1324. static int pmz_attach(struct macio_dev *mdev, const struct of_device_id *match)
  1325. {
  1326. struct uart_pmac_port *uap;
  1327. int i;
  1328. /* Iterate the pmz_ports array to find a matching entry
  1329. */
  1330. for (i = 0; i < MAX_ZS_PORTS; i++)
  1331. if (pmz_ports[i].node == mdev->ofdev.dev.of_node)
  1332. break;
  1333. if (i >= MAX_ZS_PORTS)
  1334. return -ENODEV;
  1335. uap = &pmz_ports[i];
  1336. uap->dev = mdev;
  1337. uap->port.dev = &mdev->ofdev.dev;
  1338. dev_set_drvdata(&mdev->ofdev.dev, uap);
  1339. /* We still activate the port even when failing to request resources
  1340. * to work around bugs in ancient Apple device-trees
  1341. */
  1342. if (macio_request_resources(uap->dev, "pmac_zilog"))
  1343. printk(KERN_WARNING "%pOFn: Failed to request resource"
  1344. ", port still active\n",
  1345. uap->node);
  1346. else
  1347. uap->flags |= PMACZILOG_FLAG_RSRC_REQUESTED;
  1348. return uart_add_one_port(&pmz_uart_reg, &uap->port);
  1349. }
  1350. /*
  1351. * That one should not be called, macio isn't really a hotswap device,
  1352. * we don't expect one of those serial ports to go away...
  1353. */
  1354. static int pmz_detach(struct macio_dev *mdev)
  1355. {
  1356. struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
  1357. if (!uap)
  1358. return -ENODEV;
  1359. uart_remove_one_port(&pmz_uart_reg, &uap->port);
  1360. if (uap->flags & PMACZILOG_FLAG_RSRC_REQUESTED) {
  1361. macio_release_resources(uap->dev);
  1362. uap->flags &= ~PMACZILOG_FLAG_RSRC_REQUESTED;
  1363. }
  1364. dev_set_drvdata(&mdev->ofdev.dev, NULL);
  1365. uap->dev = NULL;
  1366. uap->port.dev = NULL;
  1367. return 0;
  1368. }
  1369. static int pmz_suspend(struct macio_dev *mdev, pm_message_t pm_state)
  1370. {
  1371. struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
  1372. if (uap == NULL) {
  1373. printk("HRM... pmz_suspend with NULL uap\n");
  1374. return 0;
  1375. }
  1376. uart_suspend_port(&pmz_uart_reg, &uap->port);
  1377. return 0;
  1378. }
  1379. static int pmz_resume(struct macio_dev *mdev)
  1380. {
  1381. struct uart_pmac_port *uap = dev_get_drvdata(&mdev->ofdev.dev);
  1382. if (uap == NULL)
  1383. return 0;
  1384. uart_resume_port(&pmz_uart_reg, &uap->port);
  1385. return 0;
  1386. }
  1387. /*
  1388. * Probe all ports in the system and build the ports array, we register
  1389. * with the serial layer later, so we get a proper struct device which
  1390. * allows the tty to attach properly. This is later than it used to be
  1391. * but the tty layer really wants it that way.
  1392. */
  1393. static int __init pmz_probe(void)
  1394. {
  1395. struct device_node *node_p, *node_a, *node_b, *np;
  1396. int count = 0;
  1397. int rc;
  1398. /*
  1399. * Find all escc chips in the system
  1400. */
  1401. for_each_node_by_name(node_p, "escc") {
  1402. /*
  1403. * First get channel A/B node pointers
  1404. *
  1405. * TODO: Add routines with proper locking to do that...
  1406. */
  1407. node_a = node_b = NULL;
  1408. for (np = NULL; (np = of_get_next_child(node_p, np)) != NULL;) {
  1409. if (of_node_name_prefix(np, "ch-a"))
  1410. node_a = of_node_get(np);
  1411. else if (of_node_name_prefix(np, "ch-b"))
  1412. node_b = of_node_get(np);
  1413. }
  1414. if (!node_a && !node_b) {
  1415. of_node_put(node_a);
  1416. of_node_put(node_b);
  1417. printk(KERN_ERR "pmac_zilog: missing node %c for escc %pOF\n",
  1418. (!node_a) ? 'a' : 'b', node_p);
  1419. continue;
  1420. }
  1421. /*
  1422. * Fill basic fields in the port structures
  1423. */
  1424. if (node_b != NULL) {
  1425. pmz_ports[count].mate = &pmz_ports[count+1];
  1426. pmz_ports[count+1].mate = &pmz_ports[count];
  1427. }
  1428. pmz_ports[count].flags = PMACZILOG_FLAG_IS_CHANNEL_A;
  1429. pmz_ports[count].node = node_a;
  1430. pmz_ports[count+1].node = node_b;
  1431. pmz_ports[count].port.line = count;
  1432. pmz_ports[count+1].port.line = count+1;
  1433. /*
  1434. * Setup the ports for real
  1435. */
  1436. rc = pmz_init_port(&pmz_ports[count]);
  1437. if (rc == 0 && node_b != NULL)
  1438. rc = pmz_init_port(&pmz_ports[count+1]);
  1439. if (rc != 0) {
  1440. of_node_put(node_a);
  1441. of_node_put(node_b);
  1442. memset(&pmz_ports[count], 0, sizeof(struct uart_pmac_port));
  1443. memset(&pmz_ports[count+1], 0, sizeof(struct uart_pmac_port));
  1444. continue;
  1445. }
  1446. count += 2;
  1447. }
  1448. pmz_ports_count = count;
  1449. return 0;
  1450. }
  1451. #else
  1452. extern struct platform_device scc_a_pdev, scc_b_pdev;
  1453. static int __init pmz_init_port(struct uart_pmac_port *uap)
  1454. {
  1455. struct resource *r_ports;
  1456. int irq;
  1457. r_ports = platform_get_resource(uap->pdev, IORESOURCE_MEM, 0);
  1458. irq = platform_get_irq(uap->pdev, 0);
  1459. if (!r_ports || irq <= 0)
  1460. return -ENODEV;
  1461. uap->port.mapbase = r_ports->start;
  1462. uap->port.membase = (unsigned char __iomem *) r_ports->start;
  1463. uap->port.iotype = UPIO_MEM;
  1464. uap->port.irq = irq;
  1465. uap->port.uartclk = ZS_CLOCK;
  1466. uap->port.fifosize = 1;
  1467. uap->port.ops = &pmz_pops;
  1468. uap->port.type = PORT_PMAC_ZILOG;
  1469. uap->port.flags = 0;
  1470. uap->control_reg = uap->port.membase;
  1471. uap->data_reg = uap->control_reg + 4;
  1472. uap->port_type = 0;
  1473. pmz_convert_to_zs(uap, CS8, 0, 9600);
  1474. return 0;
  1475. }
  1476. static int __init pmz_probe(void)
  1477. {
  1478. int err;
  1479. pmz_ports_count = 0;
  1480. pmz_ports[0].port.line = 0;
  1481. pmz_ports[0].flags = PMACZILOG_FLAG_IS_CHANNEL_A;
  1482. pmz_ports[0].pdev = &scc_a_pdev;
  1483. err = pmz_init_port(&pmz_ports[0]);
  1484. if (err)
  1485. return err;
  1486. pmz_ports_count++;
  1487. pmz_ports[0].mate = &pmz_ports[1];
  1488. pmz_ports[1].mate = &pmz_ports[0];
  1489. pmz_ports[1].port.line = 1;
  1490. pmz_ports[1].flags = 0;
  1491. pmz_ports[1].pdev = &scc_b_pdev;
  1492. err = pmz_init_port(&pmz_ports[1]);
  1493. if (err)
  1494. return err;
  1495. pmz_ports_count++;
  1496. return 0;
  1497. }
  1498. static void pmz_dispose_port(struct uart_pmac_port *uap)
  1499. {
  1500. memset(uap, 0, sizeof(struct uart_pmac_port));
  1501. }
  1502. static int __init pmz_attach(struct platform_device *pdev)
  1503. {
  1504. struct uart_pmac_port *uap;
  1505. int i;
  1506. /* Iterate the pmz_ports array to find a matching entry */
  1507. for (i = 0; i < pmz_ports_count; i++)
  1508. if (pmz_ports[i].pdev == pdev)
  1509. break;
  1510. if (i >= pmz_ports_count)
  1511. return -ENODEV;
  1512. uap = &pmz_ports[i];
  1513. uap->port.dev = &pdev->dev;
  1514. platform_set_drvdata(pdev, uap);
  1515. return uart_add_one_port(&pmz_uart_reg, &uap->port);
  1516. }
  1517. static int __exit pmz_detach(struct platform_device *pdev)
  1518. {
  1519. struct uart_pmac_port *uap = platform_get_drvdata(pdev);
  1520. if (!uap)
  1521. return -ENODEV;
  1522. uart_remove_one_port(&pmz_uart_reg, &uap->port);
  1523. uap->port.dev = NULL;
  1524. return 0;
  1525. }
  1526. #endif /* !CONFIG_PPC_PMAC */
  1527. #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
  1528. static void pmz_console_write(struct console *con, const char *s, unsigned int count);
  1529. static int __init pmz_console_setup(struct console *co, char *options);
  1530. static struct console pmz_console = {
  1531. .name = PMACZILOG_NAME,
  1532. .write = pmz_console_write,
  1533. .device = uart_console_device,
  1534. .setup = pmz_console_setup,
  1535. .flags = CON_PRINTBUFFER,
  1536. .index = -1,
  1537. .data = &pmz_uart_reg,
  1538. };
  1539. #define PMACZILOG_CONSOLE &pmz_console
  1540. #else /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
  1541. #define PMACZILOG_CONSOLE (NULL)
  1542. #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
  1543. /*
  1544. * Register the driver, console driver and ports with the serial
  1545. * core
  1546. */
  1547. static int __init pmz_register(void)
  1548. {
  1549. pmz_uart_reg.nr = pmz_ports_count;
  1550. pmz_uart_reg.cons = PMACZILOG_CONSOLE;
  1551. /*
  1552. * Register this driver with the serial core
  1553. */
  1554. return uart_register_driver(&pmz_uart_reg);
  1555. }
  1556. #ifdef CONFIG_PPC_PMAC
  1557. static const struct of_device_id pmz_match[] =
  1558. {
  1559. {
  1560. .name = "ch-a",
  1561. },
  1562. {
  1563. .name = "ch-b",
  1564. },
  1565. {},
  1566. };
  1567. MODULE_DEVICE_TABLE (of, pmz_match);
  1568. static struct macio_driver pmz_driver = {
  1569. .driver = {
  1570. .name = "pmac_zilog",
  1571. .owner = THIS_MODULE,
  1572. .of_match_table = pmz_match,
  1573. },
  1574. .probe = pmz_attach,
  1575. .remove = pmz_detach,
  1576. .suspend = pmz_suspend,
  1577. .resume = pmz_resume,
  1578. };
  1579. #else
  1580. static struct platform_driver pmz_driver = {
  1581. .remove = __exit_p(pmz_detach),
  1582. .driver = {
  1583. .name = "scc",
  1584. },
  1585. };
  1586. #endif /* !CONFIG_PPC_PMAC */
  1587. static int __init init_pmz(void)
  1588. {
  1589. int rc, i;
  1590. printk(KERN_INFO "%s\n", version);
  1591. /*
  1592. * First, we need to do a direct OF-based probe pass. We
  1593. * do that because we want serial console up before the
  1594. * macio stuffs calls us back, and since that makes it
  1595. * easier to pass the proper number of channels to
  1596. * uart_register_driver()
  1597. */
  1598. if (pmz_ports_count == 0)
  1599. pmz_probe();
  1600. /*
  1601. * Bail early if no port found
  1602. */
  1603. if (pmz_ports_count == 0)
  1604. return -ENODEV;
  1605. /*
  1606. * Now we register with the serial layer
  1607. */
  1608. rc = pmz_register();
  1609. if (rc) {
  1610. printk(KERN_ERR
  1611. "pmac_zilog: Error registering serial device, disabling pmac_zilog.\n"
  1612. "pmac_zilog: Did another serial driver already claim the minors?\n");
  1613. /* effectively "pmz_unprobe()" */
  1614. for (i=0; i < pmz_ports_count; i++)
  1615. pmz_dispose_port(&pmz_ports[i]);
  1616. return rc;
  1617. }
  1618. /*
  1619. * Then we register the macio driver itself
  1620. */
  1621. #ifdef CONFIG_PPC_PMAC
  1622. return macio_register_driver(&pmz_driver);
  1623. #else
  1624. return platform_driver_probe(&pmz_driver, pmz_attach);
  1625. #endif
  1626. }
  1627. static void __exit exit_pmz(void)
  1628. {
  1629. int i;
  1630. #ifdef CONFIG_PPC_PMAC
  1631. /* Get rid of macio-driver (detach from macio) */
  1632. macio_unregister_driver(&pmz_driver);
  1633. #else
  1634. platform_driver_unregister(&pmz_driver);
  1635. #endif
  1636. for (i = 0; i < pmz_ports_count; i++) {
  1637. struct uart_pmac_port *uport = &pmz_ports[i];
  1638. #ifdef CONFIG_PPC_PMAC
  1639. if (uport->node != NULL)
  1640. pmz_dispose_port(uport);
  1641. #else
  1642. if (uport->pdev != NULL)
  1643. pmz_dispose_port(uport);
  1644. #endif
  1645. }
  1646. /* Unregister UART driver */
  1647. uart_unregister_driver(&pmz_uart_reg);
  1648. }
  1649. #ifdef CONFIG_SERIAL_PMACZILOG_CONSOLE
  1650. static void pmz_console_putchar(struct uart_port *port, int ch)
  1651. {
  1652. struct uart_pmac_port *uap =
  1653. container_of(port, struct uart_pmac_port, port);
  1654. /* Wait for the transmit buffer to empty. */
  1655. while ((read_zsreg(uap, R0) & Tx_BUF_EMP) == 0)
  1656. udelay(5);
  1657. write_zsdata(uap, ch);
  1658. }
  1659. /*
  1660. * Print a string to the serial port trying not to disturb
  1661. * any possible real use of the port...
  1662. */
  1663. static void pmz_console_write(struct console *con, const char *s, unsigned int count)
  1664. {
  1665. struct uart_pmac_port *uap = &pmz_ports[con->index];
  1666. unsigned long flags;
  1667. spin_lock_irqsave(&uap->port.lock, flags);
  1668. /* Turn of interrupts and enable the transmitter. */
  1669. write_zsreg(uap, R1, uap->curregs[1] & ~TxINT_ENAB);
  1670. write_zsreg(uap, R5, uap->curregs[5] | TxENABLE | RTS | DTR);
  1671. uart_console_write(&uap->port, s, count, pmz_console_putchar);
  1672. /* Restore the values in the registers. */
  1673. write_zsreg(uap, R1, uap->curregs[1]);
  1674. /* Don't disable the transmitter. */
  1675. spin_unlock_irqrestore(&uap->port.lock, flags);
  1676. }
  1677. /*
  1678. * Setup the serial console
  1679. */
  1680. static int __init pmz_console_setup(struct console *co, char *options)
  1681. {
  1682. struct uart_pmac_port *uap;
  1683. struct uart_port *port;
  1684. int baud = 38400;
  1685. int bits = 8;
  1686. int parity = 'n';
  1687. int flow = 'n';
  1688. unsigned long pwr_delay;
  1689. /*
  1690. * XServe's default to 57600 bps
  1691. */
  1692. if (of_machine_is_compatible("RackMac1,1")
  1693. || of_machine_is_compatible("RackMac1,2")
  1694. || of_machine_is_compatible("MacRISC4"))
  1695. baud = 57600;
  1696. /*
  1697. * Check whether an invalid uart number has been specified, and
  1698. * if so, search for the first available port that does have
  1699. * console support.
  1700. */
  1701. if (co->index >= pmz_ports_count)
  1702. co->index = 0;
  1703. uap = &pmz_ports[co->index];
  1704. #ifdef CONFIG_PPC_PMAC
  1705. if (uap->node == NULL)
  1706. return -ENODEV;
  1707. #else
  1708. if (uap->pdev == NULL)
  1709. return -ENODEV;
  1710. #endif
  1711. port = &uap->port;
  1712. /*
  1713. * Mark port as beeing a console
  1714. */
  1715. uap->flags |= PMACZILOG_FLAG_IS_CONS;
  1716. /*
  1717. * Temporary fix for uart layer who didn't setup the spinlock yet
  1718. */
  1719. spin_lock_init(&port->lock);
  1720. /*
  1721. * Enable the hardware
  1722. */
  1723. pwr_delay = __pmz_startup(uap);
  1724. if (pwr_delay)
  1725. mdelay(pwr_delay);
  1726. if (options)
  1727. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1728. return uart_set_options(port, co, baud, parity, bits, flow);
  1729. }
  1730. static int __init pmz_console_init(void)
  1731. {
  1732. /* Probe ports */
  1733. pmz_probe();
  1734. if (pmz_ports_count == 0)
  1735. return -ENODEV;
  1736. /* TODO: Autoprobe console based on OF */
  1737. /* pmz_console.index = i; */
  1738. register_console(&pmz_console);
  1739. return 0;
  1740. }
  1741. console_initcall(pmz_console_init);
  1742. #endif /* CONFIG_SERIAL_PMACZILOG_CONSOLE */
  1743. module_init(init_pmz);
  1744. module_exit(exit_pmz);