pmac_zilog.c 50 KB

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