mcfserial.c 48 KB

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