pmac_zilog.c 47 KB

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