mcfserial.c 47 KB

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