pmac_zilog.c 51 KB

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