pmac_zilog.c 53 KB

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