mcfserial.c 49 KB

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