pmac_zilog.c 54 KB

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