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