mcfserial.c 47 KB

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