pmac_zilog.c 54 KB

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