mcfserial.c 49 KB

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