68328serial.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. /* 68328serial.c: Serial port driver for 68328 microcontroller
  2. *
  3. * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu>
  4. * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
  5. * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org>
  6. * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
  7. * Copyright (C) 2002-2003 David McCullough <davidm@snapgear.com>
  8. * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
  9. *
  10. * VZ Support/Fixes Evan Stawnyczy <e@lineo.ca>
  11. * Multiple UART support Daniel Potts <danielp@cse.unsw.edu.au>
  12. * Power management support Daniel Potts <danielp@cse.unsw.edu.au>
  13. * VZ Second Serial Port enable Phil Wilshire
  14. * 2.4/2.5 port David McCullough
  15. */
  16. #include <asm/dbg.h>
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/signal.h>
  20. #include <linux/sched.h>
  21. #include <linux/timer.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/tty.h>
  24. #include <linux/tty_flip.h>
  25. #include <linux/config.h>
  26. #include <linux/major.h>
  27. #include <linux/string.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/mm.h>
  30. #include <linux/kernel.h>
  31. #include <linux/console.h>
  32. #include <linux/reboot.h>
  33. #include <linux/keyboard.h>
  34. #include <linux/init.h>
  35. #include <linux/pm.h>
  36. #include <linux/pm_legacy.h>
  37. #include <linux/bitops.h>
  38. #include <linux/delay.h>
  39. #include <asm/io.h>
  40. #include <asm/irq.h>
  41. #include <asm/system.h>
  42. #include <asm/delay.h>
  43. #include <asm/uaccess.h>
  44. /* (es) */
  45. /* note: perhaps we can murge these files, so that you can just
  46. * define 1 of them, and they can sort that out for themselves
  47. */
  48. #if defined(CONFIG_M68EZ328)
  49. #include <asm/MC68EZ328.h>
  50. #else
  51. #if defined(CONFIG_M68VZ328)
  52. #include <asm/MC68VZ328.h>
  53. #else
  54. #include <asm/MC68328.h>
  55. #endif /* CONFIG_M68VZ328 */
  56. #endif /* CONFIG_M68EZ328 */
  57. #include "68328serial.h"
  58. /* Turn off usage of real serial interrupt code, to "support" Copilot */
  59. #ifdef CONFIG_XCOPILOT_BUGS
  60. #undef USE_INTS
  61. #else
  62. #define USE_INTS
  63. #endif
  64. static struct m68k_serial m68k_soft[NR_PORTS];
  65. struct m68k_serial *IRQ_ports[NR_IRQS];
  66. static unsigned int uart_irqs[NR_PORTS] = UART_IRQ_DEFNS;
  67. /* multiple ports are contiguous in memory */
  68. m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
  69. struct tty_struct m68k_ttys;
  70. struct m68k_serial *m68k_consinfo = 0;
  71. #define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
  72. #ifdef CONFIG_CONSOLE
  73. extern wait_queue_head_t keypress_wait;
  74. #endif
  75. struct tty_driver *serial_driver;
  76. /* serial subtype definitions */
  77. #define SERIAL_TYPE_NORMAL 1
  78. /* number of characters left in xmit buffer before we ask for more */
  79. #define WAKEUP_CHARS 256
  80. /* Debugging... DEBUG_INTR is bad to use when one of the zs
  81. * lines is your console ;(
  82. */
  83. #undef SERIAL_DEBUG_INTR
  84. #undef SERIAL_DEBUG_OPEN
  85. #undef SERIAL_DEBUG_FLOW
  86. #define RS_ISR_PASS_LIMIT 256
  87. #define _INLINE_ inline
  88. static void change_speed(struct m68k_serial *info);
  89. /*
  90. * Setup for console. Argument comes from the boot command line.
  91. */
  92. #if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ)
  93. #define CONSOLE_BAUD_RATE 115200
  94. #define DEFAULT_CBAUD B115200
  95. #else
  96. /* (es) */
  97. /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
  98. #ifdef CONFIG_M68VZ328
  99. #define CONSOLE_BAUD_RATE 19200
  100. #define DEFAULT_CBAUD B19200
  101. #endif
  102. /* (/es) */
  103. #endif
  104. #ifndef CONSOLE_BAUD_RATE
  105. #define CONSOLE_BAUD_RATE 9600
  106. #define DEFAULT_CBAUD B9600
  107. #endif
  108. static int m68328_console_initted = 0;
  109. static int m68328_console_baud = CONSOLE_BAUD_RATE;
  110. static int m68328_console_cbaud = DEFAULT_CBAUD;
  111. /*
  112. * tmp_buf is used as a temporary buffer by serial_write. We need to
  113. * lock it in case the memcpy_fromfs blocks while swapping in a page,
  114. * and some other program tries to do a serial write at the same time.
  115. * Since the lock will only come under contention when the system is
  116. * swapping and available memory is low, it makes sense to share one
  117. * buffer across all the serial ports, since it significantly saves
  118. * memory if large numbers of serial ports are open.
  119. */
  120. static unsigned char tmp_buf[SERIAL_XMIT_SIZE]; /* This is cheating */
  121. DECLARE_MUTEX(tmp_buf_sem);
  122. static inline int serial_paranoia_check(struct m68k_serial *info,
  123. char *name, const char *routine)
  124. {
  125. #ifdef SERIAL_PARANOIA_CHECK
  126. static const char *badmagic =
  127. "Warning: bad magic number for serial struct %s in %s\n";
  128. static const char *badinfo =
  129. "Warning: null m68k_serial for %s in %s\n";
  130. if (!info) {
  131. printk(badinfo, name, routine);
  132. return 1;
  133. }
  134. if (info->magic != SERIAL_MAGIC) {
  135. printk(badmagic, name, routine);
  136. return 1;
  137. }
  138. #endif
  139. return 0;
  140. }
  141. /*
  142. * This is used to figure out the divisor speeds and the timeouts
  143. */
  144. static int baud_table[] = {
  145. 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
  146. 9600, 19200, 38400, 57600, 115200, 0 };
  147. #define BAUD_TABLE_SIZE (sizeof(baud_table)/sizeof(baud_table[0]))
  148. /* Sets or clears DTR/RTS on the requested line */
  149. static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
  150. {
  151. if (set) {
  152. /* set the RTS/CTS line */
  153. } else {
  154. /* clear it */
  155. }
  156. return;
  157. }
  158. /* Utility routines */
  159. static inline int get_baud(struct m68k_serial *ss)
  160. {
  161. unsigned long result = 115200;
  162. unsigned short int baud = uart_addr[ss->line].ubaud;
  163. if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
  164. result >>= GET_FIELD(baud, UBAUD_DIVIDE);
  165. return result;
  166. }
  167. /*
  168. * ------------------------------------------------------------
  169. * rs_stop() and rs_start()
  170. *
  171. * This routines are called before setting or resetting tty->stopped.
  172. * They enable or disable transmitter interrupts, as necessary.
  173. * ------------------------------------------------------------
  174. */
  175. static void rs_stop(struct tty_struct *tty)
  176. {
  177. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  178. m68328_uart *uart = &uart_addr[info->line];
  179. unsigned long flags;
  180. if (serial_paranoia_check(info, tty->name, "rs_stop"))
  181. return;
  182. save_flags(flags); cli();
  183. uart->ustcnt &= ~USTCNT_TXEN;
  184. restore_flags(flags);
  185. }
  186. static void rs_put_char(char ch)
  187. {
  188. int flags, loops = 0;
  189. save_flags(flags); cli();
  190. while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
  191. loops++;
  192. udelay(5);
  193. }
  194. UTX_TXDATA = ch;
  195. udelay(5);
  196. restore_flags(flags);
  197. }
  198. static void rs_start(struct tty_struct *tty)
  199. {
  200. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  201. m68328_uart *uart = &uart_addr[info->line];
  202. unsigned long flags;
  203. if (serial_paranoia_check(info, tty->name, "rs_start"))
  204. return;
  205. save_flags(flags); cli();
  206. if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
  207. #ifdef USE_INTS
  208. uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
  209. #else
  210. uart->ustcnt |= USTCNT_TXEN;
  211. #endif
  212. }
  213. restore_flags(flags);
  214. }
  215. /* Drop into either the boot monitor or kadb upon receiving a break
  216. * from keyboard/console input.
  217. */
  218. static void batten_down_hatches(void)
  219. {
  220. /* Drop into the debugger */
  221. }
  222. static _INLINE_ void status_handle(struct m68k_serial *info, unsigned short status)
  223. {
  224. #if 0
  225. if(status & DCD) {
  226. if((info->tty->termios->c_cflag & CRTSCTS) &&
  227. ((info->curregs[3] & AUTO_ENAB)==0)) {
  228. info->curregs[3] |= AUTO_ENAB;
  229. info->pendregs[3] |= AUTO_ENAB;
  230. write_zsreg(info->m68k_channel, 3, info->curregs[3]);
  231. }
  232. } else {
  233. if((info->curregs[3] & AUTO_ENAB)) {
  234. info->curregs[3] &= ~AUTO_ENAB;
  235. info->pendregs[3] &= ~AUTO_ENAB;
  236. write_zsreg(info->m68k_channel, 3, info->curregs[3]);
  237. }
  238. }
  239. #endif
  240. /* If this is console input and this is a
  241. * 'break asserted' status change interrupt
  242. * see if we can drop into the debugger
  243. */
  244. if((status & URX_BREAK) && info->break_abort)
  245. batten_down_hatches();
  246. return;
  247. }
  248. static _INLINE_ void receive_chars(struct m68k_serial *info, struct pt_regs *regs, unsigned short rx)
  249. {
  250. struct tty_struct *tty = info->tty;
  251. m68328_uart *uart = &uart_addr[info->line];
  252. unsigned char ch, flag;
  253. /*
  254. * This do { } while() loop will get ALL chars out of Rx FIFO
  255. */
  256. #ifndef CONFIG_XCOPILOT_BUGS
  257. do {
  258. #endif
  259. ch = GET_FIELD(rx, URX_RXDATA);
  260. if(info->is_cons) {
  261. if(URX_BREAK & rx) { /* whee, break received */
  262. status_handle(info, rx);
  263. return;
  264. #ifdef CONFIG_MAGIC_SYSRQ
  265. } else if (ch == 0x10) { /* ^P */
  266. show_state();
  267. show_free_areas();
  268. show_buffers();
  269. /* show_net_buffers(); */
  270. return;
  271. } else if (ch == 0x12) { /* ^R */
  272. emergency_restart();
  273. return;
  274. #endif /* CONFIG_MAGIC_SYSRQ */
  275. }
  276. /* It is a 'keyboard interrupt' ;-) */
  277. #ifdef CONFIG_CONSOLE
  278. wake_up(&keypress_wait);
  279. #endif
  280. }
  281. if(!tty)
  282. goto clear_and_exit;
  283. /*
  284. * Make sure that we do not overflow the buffer
  285. */
  286. if (tty_request_buffer_room(tty, 1) == 0) {
  287. schedule_work(&tty->flip.work);
  288. return;
  289. }
  290. flag = TTY_NORMAL;
  291. if(rx & URX_PARITY_ERROR) {
  292. flag = TTY_PARITY;
  293. status_handle(info, rx);
  294. } else if(rx & URX_OVRUN) {
  295. flag = TTY_OVERRUN;
  296. status_handle(info, rx);
  297. } else if(rx & URX_FRAME_ERROR) {
  298. flag = TTY_FRAME;
  299. status_handle(info, rx);
  300. }
  301. tty_insert_flip_char(tty, ch, flag);
  302. #ifndef CONFIG_XCOPILOT_BUGS
  303. } while((rx = uart->urx.w) & URX_DATA_READY);
  304. #endif
  305. schedule_work(&tty->flip.work);
  306. clear_and_exit:
  307. return;
  308. }
  309. static _INLINE_ void transmit_chars(struct m68k_serial *info)
  310. {
  311. m68328_uart *uart = &uart_addr[info->line];
  312. if (info->x_char) {
  313. /* Send next char */
  314. uart->utx.b.txdata = info->x_char;
  315. info->x_char = 0;
  316. goto clear_and_return;
  317. }
  318. if((info->xmit_cnt <= 0) || info->tty->stopped) {
  319. /* That's peculiar... TX ints off */
  320. uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
  321. goto clear_and_return;
  322. }
  323. /* Send char */
  324. uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
  325. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  326. info->xmit_cnt--;
  327. if (info->xmit_cnt < WAKEUP_CHARS)
  328. schedule_work(&info->tqueue);
  329. if(info->xmit_cnt <= 0) {
  330. /* All done for now... TX ints off */
  331. uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
  332. goto clear_and_return;
  333. }
  334. clear_and_return:
  335. /* Clear interrupt (should be auto)*/
  336. return;
  337. }
  338. /*
  339. * This is the serial driver's generic interrupt routine
  340. */
  341. irqreturn_t rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  342. {
  343. struct m68k_serial * info;
  344. m68328_uart *uart;
  345. unsigned short rx;
  346. unsigned short tx;
  347. info = IRQ_ports[irq];
  348. if(!info)
  349. return IRQ_NONE;
  350. uart = &uart_addr[info->line];
  351. rx = uart->urx.w;
  352. #ifdef USE_INTS
  353. tx = uart->utx.w;
  354. if (rx & URX_DATA_READY) receive_chars(info, regs, rx);
  355. if (tx & UTX_TX_AVAIL) transmit_chars(info);
  356. #else
  357. receive_chars(info, regs, rx);
  358. #endif
  359. return IRQ_HANDLED;
  360. }
  361. static void do_softint(void *private)
  362. {
  363. struct m68k_serial *info = (struct m68k_serial *) private;
  364. struct tty_struct *tty;
  365. tty = info->tty;
  366. if (!tty)
  367. return;
  368. #if 0
  369. if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
  370. tty_wakeup(tty);
  371. }
  372. #endif
  373. }
  374. /*
  375. * This routine is called from the scheduler tqueue when the interrupt
  376. * routine has signalled that a hangup has occurred. The path of
  377. * hangup processing is:
  378. *
  379. * serial interrupt routine -> (scheduler tqueue) ->
  380. * do_serial_hangup() -> tty->hangup() -> rs_hangup()
  381. *
  382. */
  383. static void do_serial_hangup(void *private)
  384. {
  385. struct m68k_serial *info = (struct m68k_serial *) private;
  386. struct tty_struct *tty;
  387. tty = info->tty;
  388. if (!tty)
  389. return;
  390. tty_hangup(tty);
  391. }
  392. static int startup(struct m68k_serial * info)
  393. {
  394. m68328_uart *uart = &uart_addr[info->line];
  395. unsigned long flags;
  396. if (info->flags & S_INITIALIZED)
  397. return 0;
  398. if (!info->xmit_buf) {
  399. info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
  400. if (!info->xmit_buf)
  401. return -ENOMEM;
  402. }
  403. save_flags(flags); cli();
  404. /*
  405. * Clear the FIFO buffers and disable them
  406. * (they will be reenabled in change_speed())
  407. */
  408. uart->ustcnt = USTCNT_UEN;
  409. info->xmit_fifo_size = 1;
  410. uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
  411. (void)uart->urx.w;
  412. /*
  413. * Finally, enable sequencing and interrupts
  414. */
  415. #ifdef USE_INTS
  416. uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
  417. USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
  418. #else
  419. uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
  420. #endif
  421. if (info->tty)
  422. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  423. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  424. /*
  425. * and set the speed of the serial port
  426. */
  427. change_speed(info);
  428. info->flags |= S_INITIALIZED;
  429. restore_flags(flags);
  430. return 0;
  431. }
  432. /*
  433. * This routine will shutdown a serial port; interrupts are disabled, and
  434. * DTR is dropped if the hangup on close termio flag is on.
  435. */
  436. static void shutdown(struct m68k_serial * info)
  437. {
  438. m68328_uart *uart = &uart_addr[info->line];
  439. unsigned long flags;
  440. uart->ustcnt = 0; /* All off! */
  441. if (!(info->flags & S_INITIALIZED))
  442. return;
  443. save_flags(flags); cli(); /* Disable interrupts */
  444. if (info->xmit_buf) {
  445. free_page((unsigned long) info->xmit_buf);
  446. info->xmit_buf = 0;
  447. }
  448. if (info->tty)
  449. set_bit(TTY_IO_ERROR, &info->tty->flags);
  450. info->flags &= ~S_INITIALIZED;
  451. restore_flags(flags);
  452. }
  453. struct {
  454. int divisor, prescale;
  455. }
  456. #ifndef CONFIG_M68VZ328
  457. hw_baud_table[18] = {
  458. {0,0}, /* 0 */
  459. {0,0}, /* 50 */
  460. {0,0}, /* 75 */
  461. {0,0}, /* 110 */
  462. {0,0}, /* 134 */
  463. {0,0}, /* 150 */
  464. {0,0}, /* 200 */
  465. {7,0x26}, /* 300 */
  466. {6,0x26}, /* 600 */
  467. {5,0x26}, /* 1200 */
  468. {0,0}, /* 1800 */
  469. {4,0x26}, /* 2400 */
  470. {3,0x26}, /* 4800 */
  471. {2,0x26}, /* 9600 */
  472. {1,0x26}, /* 19200 */
  473. {0,0x26}, /* 38400 */
  474. {1,0x38}, /* 57600 */
  475. {0,0x38}, /* 115200 */
  476. };
  477. #else
  478. hw_baud_table[18] = {
  479. {0,0}, /* 0 */
  480. {0,0}, /* 50 */
  481. {0,0}, /* 75 */
  482. {0,0}, /* 110 */
  483. {0,0}, /* 134 */
  484. {0,0}, /* 150 */
  485. {0,0}, /* 200 */
  486. {0,0}, /* 300 */
  487. {7,0x26}, /* 600 */
  488. {6,0x26}, /* 1200 */
  489. {0,0}, /* 1800 */
  490. {5,0x26}, /* 2400 */
  491. {4,0x26}, /* 4800 */
  492. {3,0x26}, /* 9600 */
  493. {2,0x26}, /* 19200 */
  494. {1,0x26}, /* 38400 */
  495. {0,0x26}, /* 57600 */
  496. {1,0x38}, /* 115200 */
  497. };
  498. #endif
  499. /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
  500. /*
  501. * This routine is called to set the UART divisor registers to match
  502. * the specified baud rate for a serial port.
  503. */
  504. static void change_speed(struct m68k_serial *info)
  505. {
  506. m68328_uart *uart = &uart_addr[info->line];
  507. unsigned short port;
  508. unsigned short ustcnt;
  509. unsigned cflag;
  510. int i;
  511. if (!info->tty || !info->tty->termios)
  512. return;
  513. cflag = info->tty->termios->c_cflag;
  514. if (!(port = info->port))
  515. return;
  516. ustcnt = uart->ustcnt;
  517. uart->ustcnt = ustcnt & ~USTCNT_TXEN;
  518. i = cflag & CBAUD;
  519. if (i & CBAUDEX) {
  520. i = (i & ~CBAUDEX) + B38400;
  521. }
  522. info->baud = baud_table[i];
  523. uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
  524. PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
  525. ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
  526. if ((cflag & CSIZE) == CS8)
  527. ustcnt |= USTCNT_8_7;
  528. if (cflag & CSTOPB)
  529. ustcnt |= USTCNT_STOP;
  530. if (cflag & PARENB)
  531. ustcnt |= USTCNT_PARITYEN;
  532. if (cflag & PARODD)
  533. ustcnt |= USTCNT_ODD_EVEN;
  534. #ifdef CONFIG_SERIAL_68328_RTS_CTS
  535. if (cflag & CRTSCTS) {
  536. uart->utx.w &= ~ UTX_NOCTS;
  537. } else {
  538. uart->utx.w |= UTX_NOCTS;
  539. }
  540. #endif
  541. ustcnt |= USTCNT_TXEN;
  542. uart->ustcnt = ustcnt;
  543. return;
  544. }
  545. /*
  546. * Fair output driver allows a process to speak.
  547. */
  548. static void rs_fair_output(void)
  549. {
  550. int left; /* Output no more than that */
  551. unsigned long flags;
  552. struct m68k_serial *info = &m68k_soft[0];
  553. char c;
  554. if (info == 0) return;
  555. if (info->xmit_buf == 0) return;
  556. save_flags(flags); cli();
  557. left = info->xmit_cnt;
  558. while (left != 0) {
  559. c = info->xmit_buf[info->xmit_tail];
  560. info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
  561. info->xmit_cnt--;
  562. restore_flags(flags);
  563. rs_put_char(c);
  564. save_flags(flags); cli();
  565. left = min(info->xmit_cnt, left-1);
  566. }
  567. /* Last character is being transmitted now (hopefully). */
  568. udelay(5);
  569. restore_flags(flags);
  570. return;
  571. }
  572. /*
  573. * m68k_console_print is registered for printk.
  574. */
  575. void console_print_68328(const char *p)
  576. {
  577. char c;
  578. while((c=*(p++)) != 0) {
  579. if(c == '\n')
  580. rs_put_char('\r');
  581. rs_put_char(c);
  582. }
  583. /* Comment this if you want to have a strict interrupt-driven output */
  584. rs_fair_output();
  585. return;
  586. }
  587. static void rs_set_ldisc(struct tty_struct *tty)
  588. {
  589. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  590. if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
  591. return;
  592. info->is_cons = (tty->termios->c_line == N_TTY);
  593. printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
  594. }
  595. static void rs_flush_chars(struct tty_struct *tty)
  596. {
  597. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  598. m68328_uart *uart = &uart_addr[info->line];
  599. unsigned long flags;
  600. if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
  601. return;
  602. #ifndef USE_INTS
  603. for(;;) {
  604. #endif
  605. /* Enable transmitter */
  606. save_flags(flags); cli();
  607. if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  608. !info->xmit_buf) {
  609. restore_flags(flags);
  610. return;
  611. }
  612. #ifdef USE_INTS
  613. uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
  614. #else
  615. uart->ustcnt |= USTCNT_TXEN;
  616. #endif
  617. #ifdef USE_INTS
  618. if (uart->utx.w & UTX_TX_AVAIL) {
  619. #else
  620. if (1) {
  621. #endif
  622. /* Send char */
  623. uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
  624. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  625. info->xmit_cnt--;
  626. }
  627. #ifndef USE_INTS
  628. while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
  629. }
  630. #endif
  631. restore_flags(flags);
  632. }
  633. extern void console_printn(const char * b, int count);
  634. static int rs_write(struct tty_struct * tty,
  635. const unsigned char *buf, int count)
  636. {
  637. int c, total = 0;
  638. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  639. m68328_uart *uart = &uart_addr[info->line];
  640. unsigned long flags;
  641. if (serial_paranoia_check(info, tty->name, "rs_write"))
  642. return 0;
  643. if (!tty || !info->xmit_buf)
  644. return 0;
  645. save_flags(flags);
  646. while (1) {
  647. cli();
  648. c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  649. SERIAL_XMIT_SIZE - info->xmit_head));
  650. if (c <= 0)
  651. break;
  652. memcpy(info->xmit_buf + info->xmit_head, buf, c);
  653. info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
  654. info->xmit_cnt += c;
  655. restore_flags(flags);
  656. buf += c;
  657. count -= c;
  658. total += c;
  659. }
  660. if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
  661. /* Enable transmitter */
  662. cli();
  663. #ifndef USE_INTS
  664. while(info->xmit_cnt) {
  665. #endif
  666. uart->ustcnt |= USTCNT_TXEN;
  667. #ifdef USE_INTS
  668. uart->ustcnt |= USTCNT_TX_INTR_MASK;
  669. #else
  670. while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
  671. #endif
  672. if (uart->utx.w & UTX_TX_AVAIL) {
  673. uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
  674. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  675. info->xmit_cnt--;
  676. }
  677. #ifndef USE_INTS
  678. }
  679. #endif
  680. restore_flags(flags);
  681. }
  682. restore_flags(flags);
  683. return total;
  684. }
  685. static int rs_write_room(struct tty_struct *tty)
  686. {
  687. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  688. int ret;
  689. if (serial_paranoia_check(info, tty->name, "rs_write_room"))
  690. return 0;
  691. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  692. if (ret < 0)
  693. ret = 0;
  694. return ret;
  695. }
  696. static int rs_chars_in_buffer(struct tty_struct *tty)
  697. {
  698. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  699. if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
  700. return 0;
  701. return info->xmit_cnt;
  702. }
  703. static void rs_flush_buffer(struct tty_struct *tty)
  704. {
  705. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  706. if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
  707. return;
  708. cli();
  709. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  710. sti();
  711. tty_wakeup(tty);
  712. }
  713. /*
  714. * ------------------------------------------------------------
  715. * rs_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 rs_throttle(struct tty_struct * tty)
  722. {
  723. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  724. if (serial_paranoia_check(info, tty->name, "rs_throttle"))
  725. return;
  726. if (I_IXOFF(tty))
  727. info->x_char = STOP_CHAR(tty);
  728. /* Turn off RTS line (do this atomic) */
  729. }
  730. static void rs_unthrottle(struct tty_struct * tty)
  731. {
  732. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  733. if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
  734. return;
  735. if (I_IXOFF(tty)) {
  736. if (info->x_char)
  737. info->x_char = 0;
  738. else
  739. info->x_char = START_CHAR(tty);
  740. }
  741. /* Assert RTS line (do this atomic) */
  742. }
  743. /*
  744. * ------------------------------------------------------------
  745. * rs_ioctl() and friends
  746. * ------------------------------------------------------------
  747. */
  748. static int get_serial_info(struct m68k_serial * info,
  749. struct serial_struct * retinfo)
  750. {
  751. struct serial_struct tmp;
  752. if (!retinfo)
  753. return -EFAULT;
  754. memset(&tmp, 0, sizeof(tmp));
  755. tmp.type = info->type;
  756. tmp.line = info->line;
  757. tmp.port = info->port;
  758. tmp.irq = info->irq;
  759. tmp.flags = info->flags;
  760. tmp.baud_base = info->baud_base;
  761. tmp.close_delay = info->close_delay;
  762. tmp.closing_wait = info->closing_wait;
  763. tmp.custom_divisor = info->custom_divisor;
  764. copy_to_user(retinfo,&tmp,sizeof(*retinfo));
  765. return 0;
  766. }
  767. static int set_serial_info(struct m68k_serial * info,
  768. struct serial_struct * new_info)
  769. {
  770. struct serial_struct new_serial;
  771. struct m68k_serial old_info;
  772. int retval = 0;
  773. if (!new_info)
  774. return -EFAULT;
  775. copy_from_user(&new_serial,new_info,sizeof(new_serial));
  776. old_info = *info;
  777. if (!capable(CAP_SYS_ADMIN)) {
  778. if ((new_serial.baud_base != info->baud_base) ||
  779. (new_serial.type != info->type) ||
  780. (new_serial.close_delay != info->close_delay) ||
  781. ((new_serial.flags & ~S_USR_MASK) !=
  782. (info->flags & ~S_USR_MASK)))
  783. return -EPERM;
  784. info->flags = ((info->flags & ~S_USR_MASK) |
  785. (new_serial.flags & S_USR_MASK));
  786. info->custom_divisor = new_serial.custom_divisor;
  787. goto check_and_exit;
  788. }
  789. if (info->count > 1)
  790. return -EBUSY;
  791. /*
  792. * OK, past this point, all the error checking has been done.
  793. * At this point, we start making changes.....
  794. */
  795. info->baud_base = new_serial.baud_base;
  796. info->flags = ((info->flags & ~S_FLAGS) |
  797. (new_serial.flags & S_FLAGS));
  798. info->type = new_serial.type;
  799. info->close_delay = new_serial.close_delay;
  800. info->closing_wait = new_serial.closing_wait;
  801. check_and_exit:
  802. retval = startup(info);
  803. return retval;
  804. }
  805. /*
  806. * get_lsr_info - get line status register info
  807. *
  808. * Purpose: Let user call ioctl() to get info when the UART physically
  809. * is emptied. On bus types like RS485, the transmitter must
  810. * release the bus after transmitting. This must be done when
  811. * the transmit shift register is empty, not be done when the
  812. * transmit holding register is empty. This functionality
  813. * allows an RS485 driver to be written in user space.
  814. */
  815. static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
  816. {
  817. #ifdef CONFIG_SERIAL_68328_RTS_CTS
  818. m68328_uart *uart = &uart_addr[info->line];
  819. #endif
  820. unsigned char status;
  821. cli();
  822. #ifdef CONFIG_SERIAL_68328_RTS_CTS
  823. status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
  824. #else
  825. status = 0;
  826. #endif
  827. sti();
  828. put_user(status,value);
  829. return 0;
  830. }
  831. /*
  832. * This routine sends a break character out the serial port.
  833. */
  834. static void send_break(struct m68k_serial * info, unsigned int duration)
  835. {
  836. m68328_uart *uart = &uart_addr[info->line];
  837. unsigned long flags;
  838. if (!info->port)
  839. return;
  840. save_flags(flags);
  841. cli();
  842. #ifdef USE_INTS
  843. uart->utx.w |= UTX_SEND_BREAK;
  844. msleep_interruptible(duration);
  845. uart->utx.w &= ~UTX_SEND_BREAK;
  846. #endif
  847. restore_flags(flags);
  848. }
  849. static int rs_ioctl(struct tty_struct *tty, struct file * file,
  850. unsigned int cmd, unsigned long arg)
  851. {
  852. int error;
  853. struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
  854. int retval;
  855. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  856. return -ENODEV;
  857. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  858. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
  859. (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
  860. if (tty->flags & (1 << TTY_IO_ERROR))
  861. return -EIO;
  862. }
  863. switch (cmd) {
  864. case TCSBRK: /* SVID version: non-zero arg --> no break */
  865. retval = tty_check_change(tty);
  866. if (retval)
  867. return retval;
  868. tty_wait_until_sent(tty, 0);
  869. if (!arg)
  870. send_break(info, 250); /* 1/4 second */
  871. return 0;
  872. case TCSBRKP: /* support for POSIX tcsendbreak() */
  873. retval = tty_check_change(tty);
  874. if (retval)
  875. return retval;
  876. tty_wait_until_sent(tty, 0);
  877. send_break(info, arg ? arg*(100) : 250);
  878. return 0;
  879. case TIOCGSOFTCAR:
  880. error = put_user(C_CLOCAL(tty) ? 1 : 0,
  881. (unsigned long *) arg);
  882. if (error)
  883. return error;
  884. return 0;
  885. case TIOCSSOFTCAR:
  886. get_user(arg, (unsigned long *) arg);
  887. tty->termios->c_cflag =
  888. ((tty->termios->c_cflag & ~CLOCAL) |
  889. (arg ? CLOCAL : 0));
  890. return 0;
  891. case TIOCGSERIAL:
  892. if (access_ok(VERIFY_WRITE, (void *) arg,
  893. sizeof(struct serial_struct)))
  894. return get_serial_info(info,
  895. (struct serial_struct *) arg);
  896. return -EFAULT;
  897. case TIOCSSERIAL:
  898. return set_serial_info(info,
  899. (struct serial_struct *) arg);
  900. case TIOCSERGETLSR: /* Get line status register */
  901. if (access_ok(VERIFY_WRITE, (void *) arg,
  902. sizeof(unsigned int));
  903. return get_lsr_info(info, (unsigned int *) arg);
  904. return -EFAULT;
  905. case TIOCSERGSTRUCT:
  906. if (!access_ok(VERIFY_WRITE, (void *) arg,
  907. sizeof(struct m68k_serial)))
  908. return -EFAULT;
  909. copy_to_user((struct m68k_serial *) arg,
  910. info, sizeof(struct m68k_serial));
  911. return 0;
  912. default:
  913. return -ENOIOCTLCMD;
  914. }
  915. return 0;
  916. }
  917. static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
  918. {
  919. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  920. if (tty->termios->c_cflag == old_termios->c_cflag)
  921. return;
  922. change_speed(info);
  923. if ((old_termios->c_cflag & CRTSCTS) &&
  924. !(tty->termios->c_cflag & CRTSCTS)) {
  925. tty->hw_stopped = 0;
  926. rs_start(tty);
  927. }
  928. }
  929. /*
  930. * ------------------------------------------------------------
  931. * rs_close()
  932. *
  933. * This routine is called when the serial port gets closed. First, we
  934. * wait for the last remaining data to be sent. Then, we unlink its
  935. * S structure from the interrupt chain if necessary, and we free
  936. * that IRQ if nothing is left in the chain.
  937. * ------------------------------------------------------------
  938. */
  939. static void rs_close(struct tty_struct *tty, struct file * filp)
  940. {
  941. struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
  942. m68328_uart *uart = &uart_addr[info->line];
  943. unsigned long flags;
  944. if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
  945. return;
  946. save_flags(flags); cli();
  947. if (tty_hung_up_p(filp)) {
  948. restore_flags(flags);
  949. return;
  950. }
  951. if ((tty->count == 1) && (info->count != 1)) {
  952. /*
  953. * Uh, oh. tty->count is 1, which means that the tty
  954. * structure will be freed. Info->count should always
  955. * be one in these conditions. If it's greater than
  956. * one, we've got real problems, since it means the
  957. * serial port won't be shutdown.
  958. */
  959. printk("rs_close: bad serial port count; tty->count is 1, "
  960. "info->count is %d\n", info->count);
  961. info->count = 1;
  962. }
  963. if (--info->count < 0) {
  964. printk("rs_close: bad serial port count for ttyS%d: %d\n",
  965. info->line, info->count);
  966. info->count = 0;
  967. }
  968. if (info->count) {
  969. restore_flags(flags);
  970. return;
  971. }
  972. info->flags |= S_CLOSING;
  973. /*
  974. * Now we wait for the transmit buffer to clear; and we notify
  975. * the line discipline to only process XON/XOFF characters.
  976. */
  977. tty->closing = 1;
  978. if (info->closing_wait != S_CLOSING_WAIT_NONE)
  979. tty_wait_until_sent(tty, info->closing_wait);
  980. /*
  981. * At this point we stop accepting input. To do this, we
  982. * disable the receive line status interrupts, and tell the
  983. * interrupt driver to stop checking the data ready bit in the
  984. * line status register.
  985. */
  986. uart->ustcnt &= ~USTCNT_RXEN;
  987. uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
  988. shutdown(info);
  989. if (tty->driver->flush_buffer)
  990. tty->driver->flush_buffer(tty);
  991. tty_ldisc_flush(tty);
  992. tty->closing = 0;
  993. info->event = 0;
  994. info->tty = 0;
  995. #warning "This is not and has never been valid so fix it"
  996. #if 0
  997. if (tty->ldisc.num != ldiscs[N_TTY].num) {
  998. if (tty->ldisc.close)
  999. (tty->ldisc.close)(tty);
  1000. tty->ldisc = ldiscs[N_TTY];
  1001. tty->termios->c_line = N_TTY;
  1002. if (tty->ldisc.open)
  1003. (tty->ldisc.open)(tty);
  1004. }
  1005. #endif
  1006. if (info->blocked_open) {
  1007. if (info->close_delay) {
  1008. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  1009. }
  1010. wake_up_interruptible(&info->open_wait);
  1011. }
  1012. info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
  1013. wake_up_interruptible(&info->close_wait);
  1014. restore_flags(flags);
  1015. }
  1016. /*
  1017. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  1018. */
  1019. void rs_hangup(struct tty_struct *tty)
  1020. {
  1021. struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
  1022. if (serial_paranoia_check(info, tty->name, "rs_hangup"))
  1023. return;
  1024. rs_flush_buffer(tty);
  1025. shutdown(info);
  1026. info->event = 0;
  1027. info->count = 0;
  1028. info->flags &= ~S_NORMAL_ACTIVE;
  1029. info->tty = 0;
  1030. wake_up_interruptible(&info->open_wait);
  1031. }
  1032. /*
  1033. * ------------------------------------------------------------
  1034. * rs_open() and friends
  1035. * ------------------------------------------------------------
  1036. */
  1037. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  1038. struct m68k_serial *info)
  1039. {
  1040. DECLARE_WAITQUEUE(wait, current);
  1041. int retval;
  1042. int do_clocal = 0;
  1043. /*
  1044. * If the device is in the middle of being closed, then block
  1045. * until it's done, and then try again.
  1046. */
  1047. if (info->flags & S_CLOSING) {
  1048. interruptible_sleep_on(&info->close_wait);
  1049. #ifdef SERIAL_DO_RESTART
  1050. if (info->flags & S_HUP_NOTIFY)
  1051. return -EAGAIN;
  1052. else
  1053. return -ERESTARTSYS;
  1054. #else
  1055. return -EAGAIN;
  1056. #endif
  1057. }
  1058. /*
  1059. * If non-blocking mode is set, or the port is not enabled,
  1060. * then make the check up front and then exit.
  1061. */
  1062. if ((filp->f_flags & O_NONBLOCK) ||
  1063. (tty->flags & (1 << TTY_IO_ERROR))) {
  1064. info->flags |= S_NORMAL_ACTIVE;
  1065. return 0;
  1066. }
  1067. if (tty->termios->c_cflag & CLOCAL)
  1068. do_clocal = 1;
  1069. /*
  1070. * Block waiting for the carrier detect and the line to become
  1071. * free (i.e., not in use by the callout). While we are in
  1072. * this loop, info->count is dropped by one, so that
  1073. * rs_close() knows when to free things. We restore it upon
  1074. * exit, either normal or abnormal.
  1075. */
  1076. retval = 0;
  1077. add_wait_queue(&info->open_wait, &wait);
  1078. info->count--;
  1079. info->blocked_open++;
  1080. while (1) {
  1081. cli();
  1082. m68k_rtsdtr(info, 1);
  1083. sti();
  1084. current->state = TASK_INTERRUPTIBLE;
  1085. if (tty_hung_up_p(filp) ||
  1086. !(info->flags & S_INITIALIZED)) {
  1087. #ifdef SERIAL_DO_RESTART
  1088. if (info->flags & S_HUP_NOTIFY)
  1089. retval = -EAGAIN;
  1090. else
  1091. retval = -ERESTARTSYS;
  1092. #else
  1093. retval = -EAGAIN;
  1094. #endif
  1095. break;
  1096. }
  1097. if (!(info->flags & S_CLOSING) && do_clocal)
  1098. break;
  1099. if (signal_pending(current)) {
  1100. retval = -ERESTARTSYS;
  1101. break;
  1102. }
  1103. schedule();
  1104. }
  1105. current->state = TASK_RUNNING;
  1106. remove_wait_queue(&info->open_wait, &wait);
  1107. if (!tty_hung_up_p(filp))
  1108. info->count++;
  1109. info->blocked_open--;
  1110. if (retval)
  1111. return retval;
  1112. info->flags |= S_NORMAL_ACTIVE;
  1113. return 0;
  1114. }
  1115. /*
  1116. * This routine is called whenever a serial port is opened. It
  1117. * enables interrupts for a serial port, linking in its S structure into
  1118. * the IRQ chain. It also performs the serial-specific
  1119. * initialization for the tty structure.
  1120. */
  1121. int rs_open(struct tty_struct *tty, struct file * filp)
  1122. {
  1123. struct m68k_serial *info;
  1124. int retval, line;
  1125. line = tty->index;
  1126. if (line >= NR_PORTS || line < 0) /* we have exactly one */
  1127. return -ENODEV;
  1128. info = &m68k_soft[line];
  1129. if (serial_paranoia_check(info, tty->name, "rs_open"))
  1130. return -ENODEV;
  1131. info->count++;
  1132. tty->driver_data = info;
  1133. info->tty = tty;
  1134. /*
  1135. * Start up serial port
  1136. */
  1137. retval = startup(info);
  1138. if (retval)
  1139. return retval;
  1140. return block_til_ready(tty, filp, info);
  1141. }
  1142. /* Finally, routines used to initialize the serial driver. */
  1143. static void show_serial_version(void)
  1144. {
  1145. printk("MC68328 serial driver version 1.00\n");
  1146. }
  1147. #ifdef CONFIG_PM_LEGACY
  1148. /* Serial Power management
  1149. * The console (currently fixed at line 0) is a special case for power
  1150. * management because the kernel is so chatty. The console will be
  1151. * explicitly disabled my our power manager as the last minute, so we won't
  1152. * mess with it here.
  1153. */
  1154. static struct pm_dev *serial_pm[NR_PORTS];
  1155. static int serial_pm_callback(struct pm_dev *dev, pm_request_t request, void *data)
  1156. {
  1157. struct m68k_serial *info = (struct m68k_serial *)dev->data;
  1158. if(info == NULL)
  1159. return -1;
  1160. /* special case for line 0 - pm restores it */
  1161. if(info->line == 0)
  1162. return 0;
  1163. switch (request) {
  1164. case PM_SUSPEND:
  1165. shutdown(info);
  1166. break;
  1167. case PM_RESUME:
  1168. startup(info);
  1169. break;
  1170. }
  1171. return 0;
  1172. }
  1173. void shutdown_console(void)
  1174. {
  1175. struct m68k_serial *info = &m68k_soft[0];
  1176. /* HACK: wait a bit for any pending printk's to be dumped */
  1177. {
  1178. int i = 10000;
  1179. while(i--);
  1180. }
  1181. shutdown(info);
  1182. }
  1183. void startup_console(void)
  1184. {
  1185. struct m68k_serial *info = &m68k_soft[0];
  1186. startup(info);
  1187. }
  1188. #endif /* CONFIG_PM_LEGACY */
  1189. static struct tty_operations rs_ops = {
  1190. .open = rs_open,
  1191. .close = rs_close,
  1192. .write = rs_write,
  1193. .flush_chars = rs_flush_chars,
  1194. .write_room = rs_write_room,
  1195. .chars_in_buffer = rs_chars_in_buffer,
  1196. .flush_buffer = rs_flush_buffer,
  1197. .ioctl = rs_ioctl,
  1198. .throttle = rs_throttle,
  1199. .unthrottle = rs_unthrottle,
  1200. .set_termios = rs_set_termios,
  1201. .stop = rs_stop,
  1202. .start = rs_start,
  1203. .hangup = rs_hangup,
  1204. .set_ldisc = rs_set_ldisc,
  1205. };
  1206. /* rs_init inits the driver */
  1207. static int __init
  1208. rs68328_init(void)
  1209. {
  1210. int flags, i;
  1211. struct m68k_serial *info;
  1212. serial_driver = alloc_tty_driver(NR_PORTS);
  1213. if (!serial_driver)
  1214. return -ENOMEM;
  1215. show_serial_version();
  1216. /* Initialize the tty_driver structure */
  1217. /* SPARC: Not all of this is exactly right for us. */
  1218. serial_driver->name = "ttyS";
  1219. serial_driver->major = TTY_MAJOR;
  1220. serial_driver->minor_start = 64;
  1221. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1222. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  1223. serial_driver->init_termios = tty_std_termios;
  1224. serial_driver->init_termios.c_cflag =
  1225. m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
  1226. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  1227. tty_set_operations(serial_driver, &rs_ops);
  1228. if (tty_register_driver(serial_driver)) {
  1229. put_tty_driver(serial_driver);
  1230. printk(KERN_ERR "Couldn't register serial driver\n");
  1231. return -ENOMEM;
  1232. }
  1233. save_flags(flags); cli();
  1234. for(i=0;i<NR_PORTS;i++) {
  1235. info = &m68k_soft[i];
  1236. info->magic = SERIAL_MAGIC;
  1237. info->port = (int) &uart_addr[i];
  1238. info->tty = 0;
  1239. info->irq = uart_irqs[i];
  1240. info->custom_divisor = 16;
  1241. info->close_delay = 50;
  1242. info->closing_wait = 3000;
  1243. info->x_char = 0;
  1244. info->event = 0;
  1245. info->count = 0;
  1246. info->blocked_open = 0;
  1247. INIT_WORK(&info->tqueue, do_softint, info);
  1248. INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
  1249. init_waitqueue_head(&info->open_wait);
  1250. init_waitqueue_head(&info->close_wait);
  1251. info->line = i;
  1252. info->is_cons = 1; /* Means shortcuts work */
  1253. printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
  1254. info->port, info->irq);
  1255. printk(" is a builtin MC68328 UART\n");
  1256. IRQ_ports[info->irq] = info; /* waste of space */
  1257. #ifdef CONFIG_M68VZ328
  1258. if (i > 0 )
  1259. PJSEL &= 0xCF; /* PSW enable second port output */
  1260. #endif
  1261. if (request_irq(uart_irqs[i],
  1262. rs_interrupt,
  1263. IRQ_FLG_STD,
  1264. "M68328_UART", NULL))
  1265. panic("Unable to attach 68328 serial interrupt\n");
  1266. #ifdef CONFIG_PM_LEGACY
  1267. serial_pm[i] = pm_register(PM_SYS_DEV, PM_SYS_COM, serial_pm_callback);
  1268. if (serial_pm[i])
  1269. serial_pm[i]->data = info;
  1270. #endif
  1271. }
  1272. restore_flags(flags);
  1273. return 0;
  1274. }
  1275. module_init(rs68328_init);
  1276. static void m68328_set_baud(void)
  1277. {
  1278. unsigned short ustcnt;
  1279. int i;
  1280. ustcnt = USTCNT;
  1281. USTCNT = ustcnt & ~USTCNT_TXEN;
  1282. again:
  1283. for (i = 0; i < sizeof(baud_table) / sizeof(baud_table[0]); i++)
  1284. if (baud_table[i] == m68328_console_baud)
  1285. break;
  1286. if (i >= sizeof(baud_table) / sizeof(baud_table[0])) {
  1287. m68328_console_baud = 9600;
  1288. goto again;
  1289. }
  1290. UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
  1291. PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
  1292. ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
  1293. ustcnt |= USTCNT_8_7;
  1294. ustcnt |= USTCNT_TXEN;
  1295. USTCNT = ustcnt;
  1296. m68328_console_initted = 1;
  1297. return;
  1298. }
  1299. int m68328_console_setup(struct console *cp, char *arg)
  1300. {
  1301. int i, n = CONSOLE_BAUD_RATE;
  1302. if (!cp)
  1303. return(-1);
  1304. if (arg)
  1305. n = simple_strtoul(arg,NULL,0);
  1306. for (i = 0; i < BAUD_TABLE_SIZE; i++)
  1307. if (baud_table[i] == n)
  1308. break;
  1309. if (i < BAUD_TABLE_SIZE) {
  1310. m68328_console_baud = n;
  1311. m68328_console_cbaud = 0;
  1312. if (i > 15) {
  1313. m68328_console_cbaud |= CBAUDEX;
  1314. i -= 15;
  1315. }
  1316. m68328_console_cbaud |= i;
  1317. }
  1318. m68328_set_baud(); /* make sure baud rate changes */
  1319. return(0);
  1320. }
  1321. static struct tty_driver *m68328_console_device(struct console *c, int *index)
  1322. {
  1323. *index = c->index;
  1324. return serial_driver;
  1325. }
  1326. void m68328_console_write (struct console *co, const char *str,
  1327. unsigned int count)
  1328. {
  1329. if (!m68328_console_initted)
  1330. m68328_set_baud();
  1331. while (count--) {
  1332. if (*str == '\n')
  1333. rs_put_char('\r');
  1334. rs_put_char( *str++ );
  1335. }
  1336. }
  1337. static struct console m68328_driver = {
  1338. .name = "ttyS",
  1339. .write = m68328_console_write,
  1340. .device = m68328_console_device,
  1341. .setup = m68328_console_setup,
  1342. .flags = CON_PRINTBUFFER,
  1343. .index = -1,
  1344. };
  1345. static int __init m68328_console_init(void)
  1346. {
  1347. register_console(&m68328_driver);
  1348. return 0;
  1349. }
  1350. console_initcall(m68328_console_init);