pmac_zilog.c 50 KB

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