pmac_zilog.c 50 KB

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