mcfserial.c 48 KB

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