pmac_zilog.c 48 KB

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