pmac_zilog.c 50 KB

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