mcfserial.c 48 KB

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