pmac_zilog.c 50 KB

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