pmac_zilog.c 52 KB

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