mcfserial.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925
  1. /*
  2. * mcfserial.c -- serial driver for ColdFire internal UARTS.
  3. *
  4. * Copyright (C) 1999-2003 Greg Ungerer <gerg@snapgear.com>
  5. * Copyright (c) 2000-2001 Lineo, Inc. <www.lineo.com>
  6. * Copyright (C) 2001-2002 SnapGear Inc. <www.snapgear.com>
  7. *
  8. * Based on code from 68332serial.c which was:
  9. *
  10. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  11. * Copyright (C) 1998 TSHG
  12. * Copyright (c) 1999 Rt-Control Inc. <jeff@uclinux.org>
  13. *
  14. * Changes:
  15. * 08/07/2003 Daniele Bellucci <bellucda@tiscali.it>
  16. * some cleanups in mcfrs_write.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/errno.h>
  21. #include <linux/signal.h>
  22. #include <linux/sched.h>
  23. #include <linux/timer.h>
  24. #include <linux/wait.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/tty.h>
  27. #include <linux/tty_flip.h>
  28. #include <linux/string.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/mm.h>
  31. #include <linux/kernel.h>
  32. #include <linux/serial.h>
  33. #include <linux/serialP.h>
  34. #include <linux/console.h>
  35. #include <linux/init.h>
  36. #include <linux/bitops.h>
  37. #include <linux/delay.h>
  38. #include <asm/io.h>
  39. #include <asm/irq.h>
  40. #include <asm/system.h>
  41. #include <asm/semaphore.h>
  42. #include <asm/delay.h>
  43. #include <asm/coldfire.h>
  44. #include <asm/mcfsim.h>
  45. #include <asm/mcfuart.h>
  46. #include <asm/nettel.h>
  47. #include <asm/uaccess.h>
  48. #include "mcfserial.h"
  49. struct timer_list mcfrs_timer_struct;
  50. /*
  51. * Default console baud rate, we use this as the default
  52. * for all ports so init can just open /dev/console and
  53. * keep going. Perhaps one day the cflag settings for the
  54. * console can be used instead.
  55. */
  56. #if defined(CONFIG_HW_FEITH)
  57. #define CONSOLE_BAUD_RATE 38400
  58. #define DEFAULT_CBAUD B38400
  59. #elif defined(CONFIG_MOD5272) || defined(CONFIG_M5208EVB) || defined(CONFIG_M5329EVB)
  60. #define CONSOLE_BAUD_RATE 115200
  61. #define DEFAULT_CBAUD B115200
  62. #elif defined(CONFIG_ARNEWSH) || defined(CONFIG_FREESCALE) || \
  63. defined(CONFIG_senTec) || defined(CONFIG_SNEHA) || defined(CONFIG_AVNET)
  64. #define CONSOLE_BAUD_RATE 19200
  65. #define DEFAULT_CBAUD B19200
  66. #endif
  67. #ifndef CONSOLE_BAUD_RATE
  68. #define CONSOLE_BAUD_RATE 9600
  69. #define DEFAULT_CBAUD B9600
  70. #endif
  71. int mcfrs_console_inited = 0;
  72. int mcfrs_console_port = -1;
  73. int mcfrs_console_baud = CONSOLE_BAUD_RATE;
  74. int mcfrs_console_cbaud = DEFAULT_CBAUD;
  75. /*
  76. * Driver data structures.
  77. */
  78. static struct tty_driver *mcfrs_serial_driver;
  79. /* number of characters left in xmit buffer before we ask for more */
  80. #define WAKEUP_CHARS 256
  81. /* Debugging...
  82. */
  83. #undef SERIAL_DEBUG_OPEN
  84. #undef SERIAL_DEBUG_FLOW
  85. #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
  86. defined(CONFIG_M520x) || defined(CONFIG_M532x)
  87. #define IRQBASE (MCFINT_VECBASE+MCFINT_UART0)
  88. #else
  89. #define IRQBASE 73
  90. #endif
  91. /*
  92. * Configuration table, UARTs to look for at startup.
  93. */
  94. static struct mcf_serial mcfrs_table[] = {
  95. { /* ttyS0 */
  96. .magic = 0,
  97. .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
  98. .irq = IRQBASE,
  99. .flags = ASYNC_BOOT_AUTOCONF,
  100. },
  101. { /* ttyS1 */
  102. .magic = 0,
  103. .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
  104. .irq = IRQBASE+1,
  105. .flags = ASYNC_BOOT_AUTOCONF,
  106. },
  107. };
  108. #define NR_PORTS (sizeof(mcfrs_table) / sizeof(struct mcf_serial))
  109. /*
  110. * This is used to figure out the divisor speeds and the timeouts.
  111. */
  112. static int mcfrs_baud_table[] = {
  113. 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
  114. 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
  115. };
  116. #define MCFRS_BAUD_TABLE_SIZE \
  117. (sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
  118. #ifdef CONFIG_MAGIC_SYSRQ
  119. /*
  120. * Magic system request keys. Used for debugging...
  121. */
  122. extern int magic_sysrq_key(int ch);
  123. #endif
  124. /*
  125. * Forware declarations...
  126. */
  127. static void mcfrs_change_speed(struct mcf_serial *info);
  128. static void mcfrs_wait_until_sent(struct tty_struct *tty, int timeout);
  129. static inline int serial_paranoia_check(struct mcf_serial *info,
  130. char *name, const char *routine)
  131. {
  132. #ifdef SERIAL_PARANOIA_CHECK
  133. static const char badmagic[] =
  134. "MCFRS(warning): bad magic number for serial struct %s in %s\n";
  135. static const char badinfo[] =
  136. "MCFRS(warning): null mcf_serial for %s in %s\n";
  137. if (!info) {
  138. printk(badinfo, name, routine);
  139. return 1;
  140. }
  141. if (info->magic != SERIAL_MAGIC) {
  142. printk(badmagic, name, routine);
  143. return 1;
  144. }
  145. #endif
  146. return 0;
  147. }
  148. /*
  149. * Sets or clears DTR and RTS on the requested line.
  150. */
  151. static void mcfrs_setsignals(struct mcf_serial *info, int dtr, int rts)
  152. {
  153. volatile unsigned char *uartp;
  154. unsigned long flags;
  155. #if 0
  156. printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
  157. __FILE__, __LINE__, info, dtr, rts);
  158. #endif
  159. local_irq_save(flags);
  160. if (dtr >= 0) {
  161. #ifdef MCFPP_DTR0
  162. if (info->line)
  163. mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
  164. else
  165. mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
  166. #endif
  167. }
  168. if (rts >= 0) {
  169. uartp = info->addr;
  170. if (rts) {
  171. info->sigs |= TIOCM_RTS;
  172. uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
  173. } else {
  174. info->sigs &= ~TIOCM_RTS;
  175. uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
  176. }
  177. }
  178. local_irq_restore(flags);
  179. return;
  180. }
  181. /*
  182. * Gets values of serial signals.
  183. */
  184. static int mcfrs_getsignals(struct mcf_serial *info)
  185. {
  186. volatile unsigned char *uartp;
  187. unsigned long flags;
  188. int sigs;
  189. #if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
  190. unsigned short ppdata;
  191. #endif
  192. #if 0
  193. printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__, __LINE__);
  194. #endif
  195. local_irq_save(flags);
  196. uartp = info->addr;
  197. sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
  198. sigs |= (info->sigs & TIOCM_RTS);
  199. #ifdef MCFPP_DCD0
  200. {
  201. unsigned int ppdata;
  202. ppdata = mcf_getppdata();
  203. if (info->line == 0) {
  204. sigs |= (ppdata & MCFPP_DCD0) ? 0 : TIOCM_CD;
  205. sigs |= (ppdata & MCFPP_DTR0) ? 0 : TIOCM_DTR;
  206. } else if (info->line == 1) {
  207. sigs |= (ppdata & MCFPP_DCD1) ? 0 : TIOCM_CD;
  208. sigs |= (ppdata & MCFPP_DTR1) ? 0 : TIOCM_DTR;
  209. }
  210. }
  211. #endif
  212. local_irq_restore(flags);
  213. return(sigs);
  214. }
  215. /*
  216. * ------------------------------------------------------------
  217. * mcfrs_stop() and mcfrs_start()
  218. *
  219. * This routines are called before setting or resetting tty->stopped.
  220. * They enable or disable transmitter interrupts, as necessary.
  221. * ------------------------------------------------------------
  222. */
  223. static void mcfrs_stop(struct tty_struct *tty)
  224. {
  225. volatile unsigned char *uartp;
  226. struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
  227. unsigned long flags;
  228. if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
  229. return;
  230. local_irq_save(flags);
  231. uartp = info->addr;
  232. info->imr &= ~MCFUART_UIR_TXREADY;
  233. uartp[MCFUART_UIMR] = info->imr;
  234. local_irq_restore(flags);
  235. }
  236. static void mcfrs_start(struct tty_struct *tty)
  237. {
  238. volatile unsigned char *uartp;
  239. struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
  240. unsigned long flags;
  241. if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
  242. return;
  243. local_irq_save(flags);
  244. if (info->xmit_cnt && info->xmit_buf) {
  245. uartp = info->addr;
  246. info->imr |= MCFUART_UIR_TXREADY;
  247. uartp[MCFUART_UIMR] = info->imr;
  248. }
  249. local_irq_restore(flags);
  250. }
  251. /*
  252. * ----------------------------------------------------------------------
  253. *
  254. * Here starts the interrupt handling routines. All of the following
  255. * subroutines are declared as inline and are folded into
  256. * mcfrs_interrupt(). They were separated out for readability's sake.
  257. *
  258. * Note: mcfrs_interrupt() is a "fast" interrupt, which means that it
  259. * runs with interrupts turned off. People who may want to modify
  260. * mcfrs_interrupt() should try to keep the interrupt handler as fast as
  261. * possible. After you are done making modifications, it is not a bad
  262. * idea to do:
  263. *
  264. * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
  265. *
  266. * and look at the resulting assemble code in serial.s.
  267. *
  268. * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
  269. * -----------------------------------------------------------------------
  270. */
  271. static inline void receive_chars(struct mcf_serial *info)
  272. {
  273. volatile unsigned char *uartp;
  274. struct tty_struct *tty = info->tty;
  275. unsigned char status, ch, flag;
  276. if (!tty)
  277. return;
  278. uartp = info->addr;
  279. while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
  280. ch = uartp[MCFUART_URB];
  281. info->stats.rx++;
  282. #ifdef CONFIG_MAGIC_SYSRQ
  283. if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
  284. if (magic_sysrq_key(ch))
  285. continue;
  286. }
  287. #endif
  288. flag = TTY_NORMAL;
  289. if (status & MCFUART_USR_RXERR) {
  290. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
  291. if (status & MCFUART_USR_RXBREAK) {
  292. info->stats.rxbreak++;
  293. flag = TTY_BREAK;
  294. } else if (status & MCFUART_USR_RXPARITY) {
  295. info->stats.rxparity++;
  296. flag = TTY_PARITY;
  297. } else if (status & MCFUART_USR_RXOVERRUN) {
  298. info->stats.rxoverrun++;
  299. flag = TTY_OVERRUN;
  300. } else if (status & MCFUART_USR_RXFRAMING) {
  301. info->stats.rxframing++;
  302. flag = TTY_FRAME;
  303. }
  304. }
  305. tty_insert_flip_char(tty, ch, flag);
  306. }
  307. tty_schedule_flip(tty);
  308. return;
  309. }
  310. static inline void transmit_chars(struct mcf_serial *info)
  311. {
  312. volatile unsigned char *uartp;
  313. uartp = info->addr;
  314. if (info->x_char) {
  315. /* Send special char - probably flow control */
  316. uartp[MCFUART_UTB] = info->x_char;
  317. info->x_char = 0;
  318. info->stats.tx++;
  319. }
  320. if ((info->xmit_cnt <= 0) || info->tty->stopped) {
  321. info->imr &= ~MCFUART_UIR_TXREADY;
  322. uartp[MCFUART_UIMR] = info->imr;
  323. return;
  324. }
  325. while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
  326. uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
  327. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  328. info->stats.tx++;
  329. if (--info->xmit_cnt <= 0)
  330. break;
  331. }
  332. if (info->xmit_cnt < WAKEUP_CHARS)
  333. schedule_work(&info->tqueue);
  334. return;
  335. }
  336. /*
  337. * This is the serial driver's generic interrupt routine
  338. */
  339. irqreturn_t mcfrs_interrupt(int irq, void *dev_id)
  340. {
  341. struct mcf_serial *info;
  342. unsigned char isr;
  343. info = &mcfrs_table[(irq - IRQBASE)];
  344. isr = info->addr[MCFUART_UISR] & info->imr;
  345. if (isr & MCFUART_UIR_RXREADY)
  346. receive_chars(info);
  347. if (isr & MCFUART_UIR_TXREADY)
  348. transmit_chars(info);
  349. return IRQ_HANDLED;
  350. }
  351. /*
  352. * -------------------------------------------------------------------
  353. * Here ends the serial interrupt routines.
  354. * -------------------------------------------------------------------
  355. */
  356. static void mcfrs_offintr(void *private)
  357. {
  358. struct mcf_serial *info = (struct mcf_serial *) private;
  359. struct tty_struct *tty;
  360. tty = info->tty;
  361. if (!tty)
  362. return;
  363. tty_wakeup(tty);
  364. }
  365. /*
  366. * Change of state on a DCD line.
  367. */
  368. void mcfrs_modem_change(struct mcf_serial *info, int dcd)
  369. {
  370. if (info->count == 0)
  371. return;
  372. if (info->flags & ASYNC_CHECK_CD) {
  373. if (dcd)
  374. wake_up_interruptible(&info->open_wait);
  375. else
  376. schedule_work(&info->tqueue_hangup);
  377. }
  378. }
  379. #ifdef MCFPP_DCD0
  380. unsigned short mcfrs_ppstatus;
  381. /*
  382. * This subroutine is called when the RS_TIMER goes off. It is used
  383. * to monitor the state of the DCD lines - since they have no edge
  384. * sensors and interrupt generators.
  385. */
  386. static void mcfrs_timer(void)
  387. {
  388. unsigned int ppstatus, dcdval, i;
  389. ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
  390. if (ppstatus != mcfrs_ppstatus) {
  391. for (i = 0; (i < 2); i++) {
  392. dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
  393. if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
  394. mcfrs_modem_change(&mcfrs_table[i],
  395. ((ppstatus & dcdval) ? 0 : 1));
  396. }
  397. }
  398. }
  399. mcfrs_ppstatus = ppstatus;
  400. /* Re-arm timer */
  401. mcfrs_timer_struct.expires = jiffies + HZ/25;
  402. add_timer(&mcfrs_timer_struct);
  403. }
  404. #endif /* MCFPP_DCD0 */
  405. /*
  406. * This routine is called from the scheduler tqueue when the interrupt
  407. * routine has signalled that a hangup has occurred. The path of
  408. * hangup processing is:
  409. *
  410. * serial interrupt routine -> (scheduler tqueue) ->
  411. * do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
  412. *
  413. */
  414. static void do_serial_hangup(void *private)
  415. {
  416. struct mcf_serial *info = (struct mcf_serial *) private;
  417. struct tty_struct *tty;
  418. tty = info->tty;
  419. if (!tty)
  420. return;
  421. tty_hangup(tty);
  422. }
  423. static int startup(struct mcf_serial * info)
  424. {
  425. volatile unsigned char *uartp;
  426. unsigned long flags;
  427. if (info->flags & ASYNC_INITIALIZED)
  428. return 0;
  429. if (!info->xmit_buf) {
  430. info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
  431. if (!info->xmit_buf)
  432. return -ENOMEM;
  433. }
  434. local_irq_save(flags);
  435. #ifdef SERIAL_DEBUG_OPEN
  436. printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
  437. #endif
  438. /*
  439. * Reset UART, get it into known state...
  440. */
  441. uartp = info->addr;
  442. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
  443. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
  444. mcfrs_setsignals(info, 1, 1);
  445. if (info->tty)
  446. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  447. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  448. /*
  449. * and set the speed of the serial port
  450. */
  451. mcfrs_change_speed(info);
  452. /*
  453. * Lastly enable the UART transmitter and receiver, and
  454. * interrupt enables.
  455. */
  456. info->imr = MCFUART_UIR_RXREADY;
  457. uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
  458. uartp[MCFUART_UIMR] = info->imr;
  459. info->flags |= ASYNC_INITIALIZED;
  460. local_irq_restore(flags);
  461. return 0;
  462. }
  463. /*
  464. * This routine will shutdown a serial port; interrupts are disabled, and
  465. * DTR is dropped if the hangup on close termio flag is on.
  466. */
  467. static void shutdown(struct mcf_serial * info)
  468. {
  469. volatile unsigned char *uartp;
  470. unsigned long flags;
  471. if (!(info->flags & ASYNC_INITIALIZED))
  472. return;
  473. #ifdef SERIAL_DEBUG_OPEN
  474. printk("Shutting down serial port %d (irq %d)....\n", info->line,
  475. info->irq);
  476. #endif
  477. local_irq_save(flags);
  478. uartp = info->addr;
  479. uartp[MCFUART_UIMR] = 0; /* mask all interrupts */
  480. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
  481. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
  482. if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
  483. mcfrs_setsignals(info, 0, 0);
  484. if (info->xmit_buf) {
  485. free_page((unsigned long) info->xmit_buf);
  486. info->xmit_buf = 0;
  487. }
  488. if (info->tty)
  489. set_bit(TTY_IO_ERROR, &info->tty->flags);
  490. info->flags &= ~ASYNC_INITIALIZED;
  491. local_irq_restore(flags);
  492. }
  493. /*
  494. * This routine is called to set the UART divisor registers to match
  495. * the specified baud rate for a serial port.
  496. */
  497. static void mcfrs_change_speed(struct mcf_serial *info)
  498. {
  499. volatile unsigned char *uartp;
  500. unsigned int baudclk, cflag;
  501. unsigned long flags;
  502. unsigned char mr1, mr2;
  503. int i;
  504. #ifdef CONFIG_M5272
  505. unsigned int fraction;
  506. #endif
  507. if (!info->tty || !info->tty->termios)
  508. return;
  509. cflag = info->tty->termios->c_cflag;
  510. if (info->addr == 0)
  511. return;
  512. #if 0
  513. printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
  514. #endif
  515. i = cflag & CBAUD;
  516. if (i & CBAUDEX) {
  517. i &= ~CBAUDEX;
  518. if (i < 1 || i > 4)
  519. info->tty->termios->c_cflag &= ~CBAUDEX;
  520. else
  521. i += 15;
  522. }
  523. if (i == 0) {
  524. mcfrs_setsignals(info, 0, -1);
  525. return;
  526. }
  527. /* compute the baudrate clock */
  528. #ifdef CONFIG_M5272
  529. /*
  530. * For the MCF5272, also compute the baudrate fraction.
  531. */
  532. baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
  533. fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
  534. fraction *= 16;
  535. fraction /= (32 * mcfrs_baud_table[i]);
  536. #else
  537. baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
  538. #endif
  539. info->baud = mcfrs_baud_table[i];
  540. mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
  541. mr2 = 0;
  542. switch (cflag & CSIZE) {
  543. case CS5: mr1 |= MCFUART_MR1_CS5; break;
  544. case CS6: mr1 |= MCFUART_MR1_CS6; break;
  545. case CS7: mr1 |= MCFUART_MR1_CS7; break;
  546. case CS8:
  547. default: mr1 |= MCFUART_MR1_CS8; break;
  548. }
  549. if (cflag & PARENB) {
  550. if (cflag & CMSPAR) {
  551. if (cflag & PARODD)
  552. mr1 |= MCFUART_MR1_PARITYMARK;
  553. else
  554. mr1 |= MCFUART_MR1_PARITYSPACE;
  555. } else {
  556. if (cflag & PARODD)
  557. mr1 |= MCFUART_MR1_PARITYODD;
  558. else
  559. mr1 |= MCFUART_MR1_PARITYEVEN;
  560. }
  561. } else {
  562. mr1 |= MCFUART_MR1_PARITYNONE;
  563. }
  564. if (cflag & CSTOPB)
  565. mr2 |= MCFUART_MR2_STOP2;
  566. else
  567. mr2 |= MCFUART_MR2_STOP1;
  568. if (cflag & CRTSCTS) {
  569. mr1 |= MCFUART_MR1_RXRTS;
  570. mr2 |= MCFUART_MR2_TXCTS;
  571. }
  572. if (cflag & CLOCAL)
  573. info->flags &= ~ASYNC_CHECK_CD;
  574. else
  575. info->flags |= ASYNC_CHECK_CD;
  576. uartp = info->addr;
  577. local_irq_save(flags);
  578. #if 0
  579. printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
  580. mr1, mr2, baudclk);
  581. #endif
  582. /*
  583. Note: pg 12-16 of MCF5206e User's Manual states that a
  584. software reset should be performed prior to changing
  585. UMR1,2, UCSR, UACR, bit 7
  586. */
  587. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
  588. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
  589. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
  590. uartp[MCFUART_UMR] = mr1;
  591. uartp[MCFUART_UMR] = mr2;
  592. uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8; /* set msb byte */
  593. uartp[MCFUART_UBG2] = (baudclk & 0xff); /* set lsb byte */
  594. #ifdef CONFIG_M5272
  595. uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
  596. #endif
  597. uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
  598. uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
  599. mcfrs_setsignals(info, 1, -1);
  600. local_irq_restore(flags);
  601. return;
  602. }
  603. static void mcfrs_flush_chars(struct tty_struct *tty)
  604. {
  605. volatile unsigned char *uartp;
  606. struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
  607. unsigned long flags;
  608. if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
  609. return;
  610. uartp = (volatile unsigned char *) info->addr;
  611. /*
  612. * re-enable receiver interrupt
  613. */
  614. local_irq_save(flags);
  615. if ((!(info->imr & MCFUART_UIR_RXREADY)) &&
  616. (info->flags & ASYNC_INITIALIZED) ) {
  617. info->imr |= MCFUART_UIR_RXREADY;
  618. uartp[MCFUART_UIMR] = info->imr;
  619. }
  620. local_irq_restore(flags);
  621. if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  622. !info->xmit_buf)
  623. return;
  624. /* Enable transmitter */
  625. local_irq_save(flags);
  626. info->imr |= MCFUART_UIR_TXREADY;
  627. uartp[MCFUART_UIMR] = info->imr;
  628. local_irq_restore(flags);
  629. }
  630. static int mcfrs_write(struct tty_struct * tty,
  631. const unsigned char *buf, int count)
  632. {
  633. volatile unsigned char *uartp;
  634. struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
  635. unsigned long flags;
  636. int c, total = 0;
  637. #if 0
  638. printk("%s(%d): mcfrs_write(tty=%x,buf=%x,count=%d)\n",
  639. __FILE__, __LINE__, (int)tty, (int)buf, count);
  640. #endif
  641. if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
  642. return 0;
  643. if (!tty || !info->xmit_buf)
  644. return 0;
  645. local_save_flags(flags);
  646. while (1) {
  647. local_irq_disable();
  648. c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
  649. ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
  650. local_irq_restore(flags);
  651. if (c <= 0)
  652. break;
  653. memcpy(info->xmit_buf + info->xmit_head, buf, c);
  654. local_irq_disable();
  655. info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
  656. info->xmit_cnt += c;
  657. local_irq_restore(flags);
  658. buf += c;
  659. count -= c;
  660. total += c;
  661. }
  662. local_irq_disable();
  663. uartp = info->addr;
  664. info->imr |= MCFUART_UIR_TXREADY;
  665. uartp[MCFUART_UIMR] = info->imr;
  666. local_irq_restore(flags);
  667. return total;
  668. }
  669. static int mcfrs_write_room(struct tty_struct *tty)
  670. {
  671. struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
  672. int ret;
  673. if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
  674. return 0;
  675. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  676. if (ret < 0)
  677. ret = 0;
  678. return ret;
  679. }
  680. static int mcfrs_chars_in_buffer(struct tty_struct *tty)
  681. {
  682. struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
  683. if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
  684. return 0;
  685. return info->xmit_cnt;
  686. }
  687. static void mcfrs_flush_buffer(struct tty_struct *tty)
  688. {
  689. struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
  690. unsigned long flags;
  691. if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
  692. return;
  693. local_irq_save(flags);
  694. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  695. local_irq_restore(flags);
  696. tty_wakeup(tty);
  697. }
  698. /*
  699. * ------------------------------------------------------------
  700. * mcfrs_throttle()
  701. *
  702. * This routine is called by the upper-layer tty layer to signal that
  703. * incoming characters should be throttled.
  704. * ------------------------------------------------------------
  705. */
  706. static void mcfrs_throttle(struct tty_struct * tty)
  707. {
  708. struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
  709. #ifdef SERIAL_DEBUG_THROTTLE
  710. char buf[64];
  711. printk("throttle %s: %d....\n", _tty_name(tty, buf),
  712. tty->ldisc.chars_in_buffer(tty));
  713. #endif
  714. if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
  715. return;
  716. if (I_IXOFF(tty))
  717. info->x_char = STOP_CHAR(tty);
  718. /* Turn off RTS line (do this atomic) */
  719. }
  720. static void mcfrs_unthrottle(struct tty_struct * tty)
  721. {
  722. struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
  723. #ifdef SERIAL_DEBUG_THROTTLE
  724. char buf[64];
  725. printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
  726. tty->ldisc.chars_in_buffer(tty));
  727. #endif
  728. if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
  729. return;
  730. if (I_IXOFF(tty)) {
  731. if (info->x_char)
  732. info->x_char = 0;
  733. else
  734. info->x_char = START_CHAR(tty);
  735. }
  736. /* Assert RTS line (do this atomic) */
  737. }
  738. /*
  739. * ------------------------------------------------------------
  740. * mcfrs_ioctl() and friends
  741. * ------------------------------------------------------------
  742. */
  743. static int get_serial_info(struct mcf_serial * info,
  744. struct serial_struct * retinfo)
  745. {
  746. struct serial_struct tmp;
  747. if (!retinfo)
  748. return -EFAULT;
  749. memset(&tmp, 0, sizeof(tmp));
  750. tmp.type = info->type;
  751. tmp.line = info->line;
  752. tmp.port = (unsigned int) info->addr;
  753. tmp.irq = info->irq;
  754. tmp.flags = info->flags;
  755. tmp.baud_base = info->baud_base;
  756. tmp.close_delay = info->close_delay;
  757. tmp.closing_wait = info->closing_wait;
  758. tmp.custom_divisor = info->custom_divisor;
  759. return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
  760. }
  761. static int set_serial_info(struct mcf_serial * info,
  762. struct serial_struct * new_info)
  763. {
  764. struct serial_struct new_serial;
  765. struct mcf_serial old_info;
  766. int retval = 0;
  767. if (!new_info)
  768. return -EFAULT;
  769. if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
  770. return -EFAULT;
  771. old_info = *info;
  772. if (!capable(CAP_SYS_ADMIN)) {
  773. if ((new_serial.baud_base != info->baud_base) ||
  774. (new_serial.type != info->type) ||
  775. (new_serial.close_delay != info->close_delay) ||
  776. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  777. (info->flags & ~ASYNC_USR_MASK)))
  778. return -EPERM;
  779. info->flags = ((info->flags & ~ASYNC_USR_MASK) |
  780. (new_serial.flags & ASYNC_USR_MASK));
  781. info->custom_divisor = new_serial.custom_divisor;
  782. goto check_and_exit;
  783. }
  784. if (info->count > 1)
  785. return -EBUSY;
  786. /*
  787. * OK, past this point, all the error checking has been done.
  788. * At this point, we start making changes.....
  789. */
  790. info->baud_base = new_serial.baud_base;
  791. info->flags = ((info->flags & ~ASYNC_FLAGS) |
  792. (new_serial.flags & ASYNC_FLAGS));
  793. info->type = new_serial.type;
  794. info->close_delay = new_serial.close_delay;
  795. info->closing_wait = new_serial.closing_wait;
  796. check_and_exit:
  797. retval = startup(info);
  798. return retval;
  799. }
  800. /*
  801. * get_lsr_info - get line status register info
  802. *
  803. * Purpose: Let user call ioctl() to get info when the UART physically
  804. * is emptied. On bus types like RS485, the transmitter must
  805. * release the bus after transmitting. This must be done when
  806. * the transmit shift register is empty, not be done when the
  807. * transmit holding register is empty. This functionality
  808. * allows an RS485 driver to be written in user space.
  809. */
  810. static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
  811. {
  812. volatile unsigned char *uartp;
  813. unsigned long flags;
  814. unsigned char status;
  815. local_irq_save(flags);
  816. uartp = info->addr;
  817. status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
  818. local_irq_restore(flags);
  819. return put_user(status,value);
  820. }
  821. /*
  822. * This routine sends a break character out the serial port.
  823. */
  824. static void send_break( struct mcf_serial * info, int duration)
  825. {
  826. volatile unsigned char *uartp;
  827. unsigned long flags;
  828. if (!info->addr)
  829. return;
  830. set_current_state(TASK_INTERRUPTIBLE);
  831. uartp = info->addr;
  832. local_irq_save(flags);
  833. uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
  834. schedule_timeout(duration);
  835. uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
  836. local_irq_restore(flags);
  837. }
  838. static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
  839. {
  840. struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
  841. if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
  842. return -ENODEV;
  843. if (tty->flags & (1 << TTY_IO_ERROR))
  844. return -EIO;
  845. return mcfrs_getsignals(info);
  846. }
  847. static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
  848. unsigned int set, unsigned int clear)
  849. {
  850. struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
  851. int rts = -1, dtr = -1;
  852. if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
  853. return -ENODEV;
  854. if (tty->flags & (1 << TTY_IO_ERROR))
  855. return -EIO;
  856. if (set & TIOCM_RTS)
  857. rts = 1;
  858. if (set & TIOCM_DTR)
  859. dtr = 1;
  860. if (clear & TIOCM_RTS)
  861. rts = 0;
  862. if (clear & TIOCM_DTR)
  863. dtr = 0;
  864. mcfrs_setsignals(info, dtr, rts);
  865. return 0;
  866. }
  867. static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
  868. unsigned int cmd, unsigned long arg)
  869. {
  870. struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
  871. int retval, error;
  872. if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
  873. return -ENODEV;
  874. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  875. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
  876. (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
  877. if (tty->flags & (1 << TTY_IO_ERROR))
  878. return -EIO;
  879. }
  880. switch (cmd) {
  881. case TCSBRK: /* SVID version: non-zero arg --> no break */
  882. retval = tty_check_change(tty);
  883. if (retval)
  884. return retval;
  885. tty_wait_until_sent(tty, 0);
  886. if (!arg)
  887. send_break(info, HZ/4); /* 1/4 second */
  888. return 0;
  889. case TCSBRKP: /* support for POSIX tcsendbreak() */
  890. retval = tty_check_change(tty);
  891. if (retval)
  892. return retval;
  893. tty_wait_until_sent(tty, 0);
  894. send_break(info, arg ? arg*(HZ/10) : HZ/4);
  895. return 0;
  896. case TIOCGSOFTCAR:
  897. error = put_user(C_CLOCAL(tty) ? 1 : 0,
  898. (unsigned long *) arg);
  899. if (error)
  900. return error;
  901. return 0;
  902. case TIOCSSOFTCAR:
  903. get_user(arg, (unsigned long *) arg);
  904. tty->termios->c_cflag =
  905. ((tty->termios->c_cflag & ~CLOCAL) |
  906. (arg ? CLOCAL : 0));
  907. return 0;
  908. case TIOCGSERIAL:
  909. if (access_ok(VERIFY_WRITE, (void *) arg,
  910. sizeof(struct serial_struct)))
  911. return get_serial_info(info,
  912. (struct serial_struct *) arg);
  913. return -EFAULT;
  914. case TIOCSSERIAL:
  915. return set_serial_info(info,
  916. (struct serial_struct *) arg);
  917. case TIOCSERGETLSR: /* Get line status register */
  918. if (access_ok(VERIFY_WRITE, (void *) arg,
  919. sizeof(unsigned int)))
  920. return get_lsr_info(info, (unsigned int *) arg);
  921. return -EFAULT;
  922. case TIOCSERGSTRUCT:
  923. error = copy_to_user((struct mcf_serial *) arg,
  924. info, sizeof(struct mcf_serial));
  925. if (error)
  926. return -EFAULT;
  927. return 0;
  928. #ifdef TIOCSET422
  929. case TIOCSET422: {
  930. unsigned int val;
  931. get_user(val, (unsigned int *) arg);
  932. mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
  933. break;
  934. }
  935. case TIOCGET422: {
  936. unsigned int val;
  937. val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
  938. put_user(val, (unsigned int *) arg);
  939. break;
  940. }
  941. #endif
  942. default:
  943. return -ENOIOCTLCMD;
  944. }
  945. return 0;
  946. }
  947. static void mcfrs_set_termios(struct tty_struct *tty, struct termios *old_termios)
  948. {
  949. struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
  950. if (tty->termios->c_cflag == old_termios->c_cflag)
  951. return;
  952. mcfrs_change_speed(info);
  953. if ((old_termios->c_cflag & CRTSCTS) &&
  954. !(tty->termios->c_cflag & CRTSCTS)) {
  955. tty->hw_stopped = 0;
  956. mcfrs_setsignals(info, -1, 1);
  957. #if 0
  958. mcfrs_start(tty);
  959. #endif
  960. }
  961. }
  962. /*
  963. * ------------------------------------------------------------
  964. * mcfrs_close()
  965. *
  966. * This routine is called when the serial port gets closed. First, we
  967. * wait for the last remaining data to be sent. Then, we unlink its
  968. * S structure from the interrupt chain if necessary, and we free
  969. * that IRQ if nothing is left in the chain.
  970. * ------------------------------------------------------------
  971. */
  972. static void mcfrs_close(struct tty_struct *tty, struct file * filp)
  973. {
  974. volatile unsigned char *uartp;
  975. struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
  976. unsigned long flags;
  977. if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
  978. return;
  979. local_irq_save(flags);
  980. if (tty_hung_up_p(filp)) {
  981. local_irq_restore(flags);
  982. return;
  983. }
  984. #ifdef SERIAL_DEBUG_OPEN
  985. printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
  986. #endif
  987. if ((tty->count == 1) && (info->count != 1)) {
  988. /*
  989. * Uh, oh. tty->count is 1, which means that the tty
  990. * structure will be freed. Info->count should always
  991. * be one in these conditions. If it's greater than
  992. * one, we've got real problems, since it means the
  993. * serial port won't be shutdown.
  994. */
  995. printk("MCFRS: bad serial port count; tty->count is 1, "
  996. "info->count is %d\n", info->count);
  997. info->count = 1;
  998. }
  999. if (--info->count < 0) {
  1000. printk("MCFRS: bad serial port count for ttyS%d: %d\n",
  1001. info->line, info->count);
  1002. info->count = 0;
  1003. }
  1004. if (info->count) {
  1005. local_irq_restore(flags);
  1006. return;
  1007. }
  1008. info->flags |= ASYNC_CLOSING;
  1009. /*
  1010. * Now we wait for the transmit buffer to clear; and we notify
  1011. * the line discipline to only process XON/XOFF characters.
  1012. */
  1013. tty->closing = 1;
  1014. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  1015. tty_wait_until_sent(tty, info->closing_wait);
  1016. /*
  1017. * At this point we stop accepting input. To do this, we
  1018. * disable the receive line status interrupts, and tell the
  1019. * interrupt driver to stop checking the data ready bit in the
  1020. * line status register.
  1021. */
  1022. info->imr &= ~MCFUART_UIR_RXREADY;
  1023. uartp = info->addr;
  1024. uartp[MCFUART_UIMR] = info->imr;
  1025. #if 0
  1026. /* FIXME: do we need to keep this enabled for console?? */
  1027. if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
  1028. /* Do not disable the UART */ ;
  1029. } else
  1030. #endif
  1031. shutdown(info);
  1032. if (tty->driver->flush_buffer)
  1033. tty->driver->flush_buffer(tty);
  1034. tty_ldisc_flush(tty);
  1035. tty->closing = 0;
  1036. info->event = 0;
  1037. info->tty = 0;
  1038. #if 0
  1039. if (tty->ldisc.num != ldiscs[N_TTY].num) {
  1040. if (tty->ldisc.close)
  1041. (tty->ldisc.close)(tty);
  1042. tty->ldisc = ldiscs[N_TTY];
  1043. tty->termios->c_line = N_TTY;
  1044. if (tty->ldisc.open)
  1045. (tty->ldisc.open)(tty);
  1046. }
  1047. #endif
  1048. if (info->blocked_open) {
  1049. if (info->close_delay) {
  1050. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  1051. }
  1052. wake_up_interruptible(&info->open_wait);
  1053. }
  1054. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  1055. wake_up_interruptible(&info->close_wait);
  1056. local_irq_restore(flags);
  1057. }
  1058. /*
  1059. * mcfrs_wait_until_sent() --- wait until the transmitter is empty
  1060. */
  1061. static void
  1062. mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
  1063. {
  1064. #ifdef CONFIG_M5272
  1065. #define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
  1066. struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
  1067. volatile unsigned char *uartp;
  1068. unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
  1069. if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
  1070. return;
  1071. orig_jiffies = jiffies;
  1072. /*
  1073. * Set the check interval to be 1/5 of the approximate time
  1074. * to send the entire fifo, and make it at least 1. The check
  1075. * interval should also be less than the timeout.
  1076. *
  1077. * Note: we have to use pretty tight timings here to satisfy
  1078. * the NIST-PCTS.
  1079. */
  1080. fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
  1081. char_time = fifo_time / 5;
  1082. if (char_time == 0)
  1083. char_time = 1;
  1084. if (timeout && timeout < char_time)
  1085. char_time = timeout;
  1086. /*
  1087. * Clamp the timeout period at 2 * the time to empty the
  1088. * fifo. Just to be safe, set the minimum at .5 seconds.
  1089. */
  1090. fifo_time *= 2;
  1091. if (fifo_time < (HZ/2))
  1092. fifo_time = HZ/2;
  1093. if (!timeout || timeout > fifo_time)
  1094. timeout = fifo_time;
  1095. /*
  1096. * Account for the number of bytes in the UART
  1097. * transmitter FIFO plus any byte being shifted out.
  1098. */
  1099. uartp = (volatile unsigned char *) info->addr;
  1100. for (;;) {
  1101. fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
  1102. if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
  1103. MCFUART_USR_TXEMPTY)) ==
  1104. MCFUART_USR_TXREADY)
  1105. fifo_cnt++;
  1106. if (fifo_cnt == 0)
  1107. break;
  1108. msleep_interruptible(jiffies_to_msecs(char_time));
  1109. if (signal_pending(current))
  1110. break;
  1111. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1112. break;
  1113. }
  1114. #else
  1115. /*
  1116. * For the other coldfire models, assume all data has been sent
  1117. */
  1118. #endif
  1119. }
  1120. /*
  1121. * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
  1122. */
  1123. void mcfrs_hangup(struct tty_struct *tty)
  1124. {
  1125. struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
  1126. if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
  1127. return;
  1128. mcfrs_flush_buffer(tty);
  1129. shutdown(info);
  1130. info->event = 0;
  1131. info->count = 0;
  1132. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  1133. info->tty = 0;
  1134. wake_up_interruptible(&info->open_wait);
  1135. }
  1136. /*
  1137. * ------------------------------------------------------------
  1138. * mcfrs_open() and friends
  1139. * ------------------------------------------------------------
  1140. */
  1141. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  1142. struct mcf_serial *info)
  1143. {
  1144. DECLARE_WAITQUEUE(wait, current);
  1145. int retval;
  1146. int do_clocal = 0;
  1147. /*
  1148. * If the device is in the middle of being closed, then block
  1149. * until it's done, and then try again.
  1150. */
  1151. if (info->flags & ASYNC_CLOSING) {
  1152. interruptible_sleep_on(&info->close_wait);
  1153. #ifdef SERIAL_DO_RESTART
  1154. if (info->flags & ASYNC_HUP_NOTIFY)
  1155. return -EAGAIN;
  1156. else
  1157. return -ERESTARTSYS;
  1158. #else
  1159. return -EAGAIN;
  1160. #endif
  1161. }
  1162. /*
  1163. * If non-blocking mode is set, or the port is not enabled,
  1164. * then make the check up front and then exit.
  1165. */
  1166. if ((filp->f_flags & O_NONBLOCK) ||
  1167. (tty->flags & (1 << TTY_IO_ERROR))) {
  1168. info->flags |= ASYNC_NORMAL_ACTIVE;
  1169. return 0;
  1170. }
  1171. if (tty->termios->c_cflag & CLOCAL)
  1172. do_clocal = 1;
  1173. /*
  1174. * Block waiting for the carrier detect and the line to become
  1175. * free (i.e., not in use by the callout). While we are in
  1176. * this loop, info->count is dropped by one, so that
  1177. * mcfrs_close() knows when to free things. We restore it upon
  1178. * exit, either normal or abnormal.
  1179. */
  1180. retval = 0;
  1181. add_wait_queue(&info->open_wait, &wait);
  1182. #ifdef SERIAL_DEBUG_OPEN
  1183. printk("block_til_ready before block: ttyS%d, count = %d\n",
  1184. info->line, info->count);
  1185. #endif
  1186. info->count--;
  1187. info->blocked_open++;
  1188. while (1) {
  1189. local_irq_disable();
  1190. mcfrs_setsignals(info, 1, 1);
  1191. local_irq_enable();
  1192. current->state = TASK_INTERRUPTIBLE;
  1193. if (tty_hung_up_p(filp) ||
  1194. !(info->flags & ASYNC_INITIALIZED)) {
  1195. #ifdef SERIAL_DO_RESTART
  1196. if (info->flags & ASYNC_HUP_NOTIFY)
  1197. retval = -EAGAIN;
  1198. else
  1199. retval = -ERESTARTSYS;
  1200. #else
  1201. retval = -EAGAIN;
  1202. #endif
  1203. break;
  1204. }
  1205. if (!(info->flags & ASYNC_CLOSING) &&
  1206. (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
  1207. break;
  1208. if (signal_pending(current)) {
  1209. retval = -ERESTARTSYS;
  1210. break;
  1211. }
  1212. #ifdef SERIAL_DEBUG_OPEN
  1213. printk("block_til_ready blocking: ttyS%d, count = %d\n",
  1214. info->line, info->count);
  1215. #endif
  1216. schedule();
  1217. }
  1218. current->state = TASK_RUNNING;
  1219. remove_wait_queue(&info->open_wait, &wait);
  1220. if (!tty_hung_up_p(filp))
  1221. info->count++;
  1222. info->blocked_open--;
  1223. #ifdef SERIAL_DEBUG_OPEN
  1224. printk("block_til_ready after blocking: ttyS%d, count = %d\n",
  1225. info->line, info->count);
  1226. #endif
  1227. if (retval)
  1228. return retval;
  1229. info->flags |= ASYNC_NORMAL_ACTIVE;
  1230. return 0;
  1231. }
  1232. /*
  1233. * This routine is called whenever a serial port is opened. It
  1234. * enables interrupts for a serial port, linking in its structure into
  1235. * the IRQ chain. It also performs the serial-specific
  1236. * initialization for the tty structure.
  1237. */
  1238. int mcfrs_open(struct tty_struct *tty, struct file * filp)
  1239. {
  1240. struct mcf_serial *info;
  1241. int retval, line;
  1242. line = tty->index;
  1243. if ((line < 0) || (line >= NR_PORTS))
  1244. return -ENODEV;
  1245. info = mcfrs_table + line;
  1246. if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
  1247. return -ENODEV;
  1248. #ifdef SERIAL_DEBUG_OPEN
  1249. printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
  1250. #endif
  1251. info->count++;
  1252. tty->driver_data = info;
  1253. info->tty = tty;
  1254. /*
  1255. * Start up serial port
  1256. */
  1257. retval = startup(info);
  1258. if (retval)
  1259. return retval;
  1260. retval = block_til_ready(tty, filp, info);
  1261. if (retval) {
  1262. #ifdef SERIAL_DEBUG_OPEN
  1263. printk("mcfrs_open returning after block_til_ready with %d\n",
  1264. retval);
  1265. #endif
  1266. return retval;
  1267. }
  1268. #ifdef SERIAL_DEBUG_OPEN
  1269. printk("mcfrs_open %s successful...\n", tty->name);
  1270. #endif
  1271. return 0;
  1272. }
  1273. /*
  1274. * Based on the line number set up the internal interrupt stuff.
  1275. */
  1276. static void mcfrs_irqinit(struct mcf_serial *info)
  1277. {
  1278. #if defined(CONFIG_M5272)
  1279. volatile unsigned long *icrp;
  1280. volatile unsigned long *portp;
  1281. volatile unsigned char *uartp;
  1282. uartp = info->addr;
  1283. icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
  1284. switch (info->line) {
  1285. case 0:
  1286. *icrp = 0xe0000000;
  1287. break;
  1288. case 1:
  1289. *icrp = 0x0e000000;
  1290. break;
  1291. default:
  1292. printk("MCFRS: don't know how to handle UART %d interrupt?\n",
  1293. info->line);
  1294. return;
  1295. }
  1296. /* Enable the output lines for the serial ports */
  1297. portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
  1298. *portp = (*portp & ~0x000000ff) | 0x00000055;
  1299. portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
  1300. *portp = (*portp & ~0x000003fc) | 0x000002a8;
  1301. #elif defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x)
  1302. volatile unsigned char *icrp, *uartp;
  1303. volatile unsigned long *imrp;
  1304. uartp = info->addr;
  1305. icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
  1306. MCFINTC_ICR0 + MCFINT_UART0 + info->line);
  1307. *icrp = 0x30 + info->line; /* level 6, line based priority */
  1308. imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
  1309. MCFINTC_IMRL);
  1310. *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
  1311. #elif defined(CONFIG_M520x)
  1312. volatile unsigned char *icrp, *uartp;
  1313. volatile unsigned long *imrp;
  1314. uartp = info->addr;
  1315. icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
  1316. MCFINTC_ICR0 + MCFINT_UART0 + info->line);
  1317. *icrp = 0x03;
  1318. imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
  1319. MCFINTC_IMRL);
  1320. *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
  1321. if (info->line < 2) {
  1322. unsigned short *uart_par;
  1323. uart_par = (unsigned short *)(MCF_IPSBAR + MCF_GPIO_PAR_UART);
  1324. if (info->line == 0)
  1325. *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD0
  1326. | MCF_GPIO_PAR_UART_PAR_URXD0;
  1327. else if (info->line == 1)
  1328. *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD1
  1329. | MCF_GPIO_PAR_UART_PAR_URXD1;
  1330. } else if (info->line == 2) {
  1331. unsigned char *feci2c_par;
  1332. feci2c_par = (unsigned char *)(MCF_IPSBAR + MCF_GPIO_PAR_FECI2C);
  1333. *feci2c_par &= ~0x0F;
  1334. *feci2c_par |= MCF_GPIO_PAR_FECI2C_PAR_SCL_UTXD2
  1335. | MCF_GPIO_PAR_FECI2C_PAR_SDA_URXD2;
  1336. }
  1337. #elif defined(CONFIG_M532x)
  1338. volatile unsigned char *uartp;
  1339. uartp = info->addr;
  1340. switch (info->line) {
  1341. case 0:
  1342. MCF_INTC0_ICR26 = 0x3;
  1343. MCF_INTC0_CIMR = 26;
  1344. /* GPIO initialization */
  1345. MCF_GPIO_PAR_UART |= 0x000F;
  1346. break;
  1347. case 1:
  1348. MCF_INTC0_ICR27 = 0x3;
  1349. MCF_INTC0_CIMR = 27;
  1350. /* GPIO initialization */
  1351. MCF_GPIO_PAR_UART |= 0x0FF0;
  1352. break;
  1353. case 2:
  1354. MCF_INTC0_ICR28 = 0x3;
  1355. MCF_INTC0_CIMR = 28;
  1356. /* GPIOs also must be initalized, depends on board */
  1357. break;
  1358. }
  1359. #else
  1360. volatile unsigned char *icrp, *uartp;
  1361. switch (info->line) {
  1362. case 0:
  1363. icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
  1364. *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
  1365. MCFSIM_ICR_PRI1;
  1366. mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
  1367. break;
  1368. case 1:
  1369. icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
  1370. *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
  1371. MCFSIM_ICR_PRI2;
  1372. mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
  1373. break;
  1374. default:
  1375. printk("MCFRS: don't know how to handle UART %d interrupt?\n",
  1376. info->line);
  1377. return;
  1378. }
  1379. uartp = info->addr;
  1380. uartp[MCFUART_UIVR] = info->irq;
  1381. #endif
  1382. /* Clear mask, so no surprise interrupts. */
  1383. uartp[MCFUART_UIMR] = 0;
  1384. if (request_irq(info->irq, mcfrs_interrupt, IRQF_DISABLED,
  1385. "ColdFire UART", NULL)) {
  1386. printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
  1387. "vector=%d\n", info->line, info->irq);
  1388. }
  1389. return;
  1390. }
  1391. char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
  1392. /*
  1393. * Serial stats reporting...
  1394. */
  1395. int mcfrs_readproc(char *page, char **start, off_t off, int count,
  1396. int *eof, void *data)
  1397. {
  1398. struct mcf_serial *info;
  1399. char str[20];
  1400. int len, sigs, i;
  1401. len = sprintf(page, mcfrs_drivername);
  1402. for (i = 0; (i < NR_PORTS); i++) {
  1403. info = &mcfrs_table[i];
  1404. len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
  1405. i, (unsigned int) info->addr, info->irq, info->baud);
  1406. if (info->stats.rx || info->stats.tx)
  1407. len += sprintf((page + len), "tx:%d rx:%d ",
  1408. info->stats.tx, info->stats.rx);
  1409. if (info->stats.rxframing)
  1410. len += sprintf((page + len), "fe:%d ",
  1411. info->stats.rxframing);
  1412. if (info->stats.rxparity)
  1413. len += sprintf((page + len), "pe:%d ",
  1414. info->stats.rxparity);
  1415. if (info->stats.rxbreak)
  1416. len += sprintf((page + len), "brk:%d ",
  1417. info->stats.rxbreak);
  1418. if (info->stats.rxoverrun)
  1419. len += sprintf((page + len), "oe:%d ",
  1420. info->stats.rxoverrun);
  1421. str[0] = str[1] = 0;
  1422. if ((sigs = mcfrs_getsignals(info))) {
  1423. if (sigs & TIOCM_RTS)
  1424. strcat(str, "|RTS");
  1425. if (sigs & TIOCM_CTS)
  1426. strcat(str, "|CTS");
  1427. if (sigs & TIOCM_DTR)
  1428. strcat(str, "|DTR");
  1429. if (sigs & TIOCM_CD)
  1430. strcat(str, "|CD");
  1431. }
  1432. len += sprintf((page + len), "%s\n", &str[1]);
  1433. }
  1434. return(len);
  1435. }
  1436. /* Finally, routines used to initialize the serial driver. */
  1437. static void show_serial_version(void)
  1438. {
  1439. printk(mcfrs_drivername);
  1440. }
  1441. static const struct tty_operations mcfrs_ops = {
  1442. .open = mcfrs_open,
  1443. .close = mcfrs_close,
  1444. .write = mcfrs_write,
  1445. .flush_chars = mcfrs_flush_chars,
  1446. .write_room = mcfrs_write_room,
  1447. .chars_in_buffer = mcfrs_chars_in_buffer,
  1448. .flush_buffer = mcfrs_flush_buffer,
  1449. .ioctl = mcfrs_ioctl,
  1450. .throttle = mcfrs_throttle,
  1451. .unthrottle = mcfrs_unthrottle,
  1452. .set_termios = mcfrs_set_termios,
  1453. .stop = mcfrs_stop,
  1454. .start = mcfrs_start,
  1455. .hangup = mcfrs_hangup,
  1456. .read_proc = mcfrs_readproc,
  1457. .wait_until_sent = mcfrs_wait_until_sent,
  1458. .tiocmget = mcfrs_tiocmget,
  1459. .tiocmset = mcfrs_tiocmset,
  1460. };
  1461. /* mcfrs_init inits the driver */
  1462. static int __init
  1463. mcfrs_init(void)
  1464. {
  1465. struct mcf_serial *info;
  1466. unsigned long flags;
  1467. int i;
  1468. /* Setup base handler, and timer table. */
  1469. #ifdef MCFPP_DCD0
  1470. init_timer(&mcfrs_timer_struct);
  1471. mcfrs_timer_struct.function = mcfrs_timer;
  1472. mcfrs_timer_struct.data = 0;
  1473. mcfrs_timer_struct.expires = jiffies + HZ/25;
  1474. add_timer(&mcfrs_timer_struct);
  1475. mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
  1476. #endif
  1477. mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
  1478. if (!mcfrs_serial_driver)
  1479. return -ENOMEM;
  1480. show_serial_version();
  1481. /* Initialize the tty_driver structure */
  1482. mcfrs_serial_driver->owner = THIS_MODULE;
  1483. mcfrs_serial_driver->name = "ttyS";
  1484. mcfrs_serial_driver->driver_name = "serial";
  1485. mcfrs_serial_driver->major = TTY_MAJOR;
  1486. mcfrs_serial_driver->minor_start = 64;
  1487. mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1488. mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
  1489. mcfrs_serial_driver->init_termios = tty_std_termios;
  1490. mcfrs_serial_driver->init_termios.c_cflag =
  1491. mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
  1492. mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
  1493. tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
  1494. if (tty_register_driver(mcfrs_serial_driver)) {
  1495. printk("MCFRS: Couldn't register serial driver\n");
  1496. put_tty_driver(mcfrs_serial_driver);
  1497. return(-EBUSY);
  1498. }
  1499. local_irq_save(flags);
  1500. /*
  1501. * Configure all the attached serial ports.
  1502. */
  1503. for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
  1504. info->magic = SERIAL_MAGIC;
  1505. info->line = i;
  1506. info->tty = 0;
  1507. info->custom_divisor = 16;
  1508. info->close_delay = 50;
  1509. info->closing_wait = 3000;
  1510. info->x_char = 0;
  1511. info->event = 0;
  1512. info->count = 0;
  1513. info->blocked_open = 0;
  1514. INIT_WORK(&info->tqueue, mcfrs_offintr, info);
  1515. INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
  1516. init_waitqueue_head(&info->open_wait);
  1517. init_waitqueue_head(&info->close_wait);
  1518. info->imr = 0;
  1519. mcfrs_setsignals(info, 0, 0);
  1520. mcfrs_irqinit(info);
  1521. printk("ttyS%d at 0x%04x (irq = %d)", info->line,
  1522. (unsigned int) info->addr, info->irq);
  1523. printk(" is a builtin ColdFire UART\n");
  1524. }
  1525. local_irq_restore(flags);
  1526. return 0;
  1527. }
  1528. module_init(mcfrs_init);
  1529. /****************************************************************************/
  1530. /* Serial Console */
  1531. /****************************************************************************/
  1532. /*
  1533. * Quick and dirty UART initialization, for console output.
  1534. */
  1535. void mcfrs_init_console(void)
  1536. {
  1537. volatile unsigned char *uartp;
  1538. unsigned int clk;
  1539. /*
  1540. * Reset UART, get it into known state...
  1541. */
  1542. uartp = (volatile unsigned char *) (MCF_MBAR +
  1543. (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
  1544. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
  1545. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
  1546. uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
  1547. /*
  1548. * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
  1549. */
  1550. uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
  1551. uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
  1552. clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
  1553. uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8; /* set msb baud */
  1554. uartp[MCFUART_UBG2] = (clk & 0xff); /* set lsb baud */
  1555. uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
  1556. uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
  1557. mcfrs_console_inited++;
  1558. return;
  1559. }
  1560. /*
  1561. * Setup for console. Argument comes from the boot command line.
  1562. */
  1563. int mcfrs_console_setup(struct console *cp, char *arg)
  1564. {
  1565. int i, n = CONSOLE_BAUD_RATE;
  1566. if (!cp)
  1567. return(-1);
  1568. if (!strncmp(cp->name, "ttyS", 4))
  1569. mcfrs_console_port = cp->index;
  1570. else if (!strncmp(cp->name, "cua", 3))
  1571. mcfrs_console_port = cp->index;
  1572. else
  1573. return(-1);
  1574. if (arg)
  1575. n = simple_strtoul(arg,NULL,0);
  1576. for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
  1577. if (mcfrs_baud_table[i] == n)
  1578. break;
  1579. if (i < MCFRS_BAUD_TABLE_SIZE) {
  1580. mcfrs_console_baud = n;
  1581. mcfrs_console_cbaud = 0;
  1582. if (i > 15) {
  1583. mcfrs_console_cbaud |= CBAUDEX;
  1584. i -= 15;
  1585. }
  1586. mcfrs_console_cbaud |= i;
  1587. }
  1588. mcfrs_init_console(); /* make sure baud rate changes */
  1589. return(0);
  1590. }
  1591. static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
  1592. {
  1593. *index = c->index;
  1594. return mcfrs_serial_driver;
  1595. }
  1596. /*
  1597. * Output a single character, using UART polled mode.
  1598. * This is used for console output.
  1599. */
  1600. void mcfrs_put_char(char ch)
  1601. {
  1602. volatile unsigned char *uartp;
  1603. unsigned long flags;
  1604. int i;
  1605. uartp = (volatile unsigned char *) (MCF_MBAR +
  1606. (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
  1607. local_irq_save(flags);
  1608. for (i = 0; (i < 0x10000); i++) {
  1609. if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
  1610. break;
  1611. }
  1612. if (i < 0x10000) {
  1613. uartp[MCFUART_UTB] = ch;
  1614. for (i = 0; (i < 0x10000); i++)
  1615. if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
  1616. break;
  1617. }
  1618. if (i >= 0x10000)
  1619. mcfrs_init_console(); /* try and get it back */
  1620. local_irq_restore(flags);
  1621. return;
  1622. }
  1623. /*
  1624. * rs_console_write is registered for printk output.
  1625. */
  1626. void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
  1627. {
  1628. if (!mcfrs_console_inited)
  1629. mcfrs_init_console();
  1630. while (len-- > 0) {
  1631. if (*p == '\n')
  1632. mcfrs_put_char('\r');
  1633. mcfrs_put_char(*p++);
  1634. }
  1635. }
  1636. /*
  1637. * declare our consoles
  1638. */
  1639. struct console mcfrs_console = {
  1640. .name = "ttyS",
  1641. .write = mcfrs_console_write,
  1642. .device = mcfrs_console_device,
  1643. .setup = mcfrs_console_setup,
  1644. .flags = CON_PRINTBUFFER,
  1645. .index = -1,
  1646. };
  1647. static int __init mcfrs_console_init(void)
  1648. {
  1649. register_console(&mcfrs_console);
  1650. return 0;
  1651. }
  1652. console_initcall(mcfrs_console_init);
  1653. /****************************************************************************/