pmac_zilog.c 50 KB

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