pmac_zilog.c 54 KB

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