68328serial.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  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/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/segment.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;
  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. machine_restart(NULL);
  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->flip.count >= TTY_FLIPBUF_SIZE) {
  287. schedule_work(&tty->flip.work);
  288. return;
  289. }
  290. if(rx & URX_PARITY_ERROR) {
  291. *tty->flip.flag_buf_ptr++ = TTY_PARITY;
  292. status_handle(info, rx);
  293. } else if(rx & URX_OVRUN) {
  294. *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
  295. status_handle(info, rx);
  296. } else if(rx & URX_FRAME_ERROR) {
  297. *tty->flip.flag_buf_ptr++ = TTY_FRAME;
  298. status_handle(info, rx);
  299. } else {
  300. *tty->flip.flag_buf_ptr++ = 0; /* XXX */
  301. }
  302. *tty->flip.char_buf_ptr++ = ch;
  303. tty->flip.count++;
  304. #ifndef CONFIG_XCOPILOT_BUGS
  305. } while((rx = uart->urx.w) & URX_DATA_READY);
  306. #endif
  307. schedule_work(&tty->flip.work);
  308. clear_and_exit:
  309. return;
  310. }
  311. static _INLINE_ void transmit_chars(struct m68k_serial *info)
  312. {
  313. m68328_uart *uart = &uart_addr[info->line];
  314. if (info->x_char) {
  315. /* Send next char */
  316. uart->utx.b.txdata = info->x_char;
  317. info->x_char = 0;
  318. goto clear_and_return;
  319. }
  320. if((info->xmit_cnt <= 0) || info->tty->stopped) {
  321. /* That's peculiar... TX ints off */
  322. uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
  323. goto clear_and_return;
  324. }
  325. /* Send char */
  326. uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
  327. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  328. info->xmit_cnt--;
  329. if (info->xmit_cnt < WAKEUP_CHARS)
  330. schedule_work(&info->tqueue);
  331. if(info->xmit_cnt <= 0) {
  332. /* All done for now... TX ints off */
  333. uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
  334. goto clear_and_return;
  335. }
  336. clear_and_return:
  337. /* Clear interrupt (should be auto)*/
  338. return;
  339. }
  340. /*
  341. * This is the serial driver's generic interrupt routine
  342. */
  343. irqreturn_t rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
  344. {
  345. struct m68k_serial * info;
  346. m68328_uart *uart;
  347. unsigned short rx;
  348. unsigned short tx;
  349. info = IRQ_ports[irq];
  350. if(!info)
  351. return IRQ_NONE;
  352. uart = &uart_addr[info->line];
  353. rx = uart->urx.w;
  354. #ifdef USE_INTS
  355. tx = uart->utx.w;
  356. if (rx & URX_DATA_READY) receive_chars(info, regs, rx);
  357. if (tx & UTX_TX_AVAIL) transmit_chars(info);
  358. #else
  359. receive_chars(info, regs, rx);
  360. #endif
  361. return IRQ_HANDLED;
  362. }
  363. static void do_softint(void *private)
  364. {
  365. struct m68k_serial *info = (struct m68k_serial *) private;
  366. struct tty_struct *tty;
  367. tty = info->tty;
  368. if (!tty)
  369. return;
  370. #if 0
  371. if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
  372. tty_wakeup(tty);
  373. }
  374. #endif
  375. }
  376. /*
  377. * This routine is called from the scheduler tqueue when the interrupt
  378. * routine has signalled that a hangup has occurred. The path of
  379. * hangup processing is:
  380. *
  381. * serial interrupt routine -> (scheduler tqueue) ->
  382. * do_serial_hangup() -> tty->hangup() -> rs_hangup()
  383. *
  384. */
  385. static void do_serial_hangup(void *private)
  386. {
  387. struct m68k_serial *info = (struct m68k_serial *) private;
  388. struct tty_struct *tty;
  389. tty = info->tty;
  390. if (!tty)
  391. return;
  392. tty_hangup(tty);
  393. }
  394. static int startup(struct m68k_serial * info)
  395. {
  396. m68328_uart *uart = &uart_addr[info->line];
  397. unsigned long flags;
  398. if (info->flags & S_INITIALIZED)
  399. return 0;
  400. if (!info->xmit_buf) {
  401. info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
  402. if (!info->xmit_buf)
  403. return -ENOMEM;
  404. }
  405. save_flags(flags); cli();
  406. /*
  407. * Clear the FIFO buffers and disable them
  408. * (they will be reenabled in change_speed())
  409. */
  410. uart->ustcnt = USTCNT_UEN;
  411. info->xmit_fifo_size = 1;
  412. uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
  413. (void)uart->urx.w;
  414. /*
  415. * Finally, enable sequencing and interrupts
  416. */
  417. #ifdef USE_INTS
  418. uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
  419. USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
  420. #else
  421. uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
  422. #endif
  423. if (info->tty)
  424. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  425. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  426. /*
  427. * and set the speed of the serial port
  428. */
  429. change_speed(info);
  430. info->flags |= S_INITIALIZED;
  431. restore_flags(flags);
  432. return 0;
  433. }
  434. /*
  435. * This routine will shutdown a serial port; interrupts are disabled, and
  436. * DTR is dropped if the hangup on close termio flag is on.
  437. */
  438. static void shutdown(struct m68k_serial * info)
  439. {
  440. m68328_uart *uart = &uart_addr[info->line];
  441. unsigned long flags;
  442. uart->ustcnt = 0; /* All off! */
  443. if (!(info->flags & S_INITIALIZED))
  444. return;
  445. save_flags(flags); cli(); /* Disable interrupts */
  446. if (info->xmit_buf) {
  447. free_page((unsigned long) info->xmit_buf);
  448. info->xmit_buf = 0;
  449. }
  450. if (info->tty)
  451. set_bit(TTY_IO_ERROR, &info->tty->flags);
  452. info->flags &= ~S_INITIALIZED;
  453. restore_flags(flags);
  454. }
  455. struct {
  456. int divisor, prescale;
  457. }
  458. #ifndef CONFIG_M68VZ328
  459. hw_baud_table[18] = {
  460. {0,0}, /* 0 */
  461. {0,0}, /* 50 */
  462. {0,0}, /* 75 */
  463. {0,0}, /* 110 */
  464. {0,0}, /* 134 */
  465. {0,0}, /* 150 */
  466. {0,0}, /* 200 */
  467. {7,0x26}, /* 300 */
  468. {6,0x26}, /* 600 */
  469. {5,0x26}, /* 1200 */
  470. {0,0}, /* 1800 */
  471. {4,0x26}, /* 2400 */
  472. {3,0x26}, /* 4800 */
  473. {2,0x26}, /* 9600 */
  474. {1,0x26}, /* 19200 */
  475. {0,0x26}, /* 38400 */
  476. {1,0x38}, /* 57600 */
  477. {0,0x38}, /* 115200 */
  478. };
  479. #else
  480. hw_baud_table[18] = {
  481. {0,0}, /* 0 */
  482. {0,0}, /* 50 */
  483. {0,0}, /* 75 */
  484. {0,0}, /* 110 */
  485. {0,0}, /* 134 */
  486. {0,0}, /* 150 */
  487. {0,0}, /* 200 */
  488. {0,0}, /* 300 */
  489. {7,0x26}, /* 600 */
  490. {6,0x26}, /* 1200 */
  491. {0,0}, /* 1800 */
  492. {5,0x26}, /* 2400 */
  493. {4,0x26}, /* 4800 */
  494. {3,0x26}, /* 9600 */
  495. {2,0x26}, /* 19200 */
  496. {1,0x26}, /* 38400 */
  497. {0,0x26}, /* 57600 */
  498. {1,0x38}, /* 115200 */
  499. };
  500. #endif
  501. /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
  502. /*
  503. * This routine is called to set the UART divisor registers to match
  504. * the specified baud rate for a serial port.
  505. */
  506. static void change_speed(struct m68k_serial *info)
  507. {
  508. m68328_uart *uart = &uart_addr[info->line];
  509. unsigned short port;
  510. unsigned short ustcnt;
  511. unsigned cflag;
  512. int i;
  513. if (!info->tty || !info->tty->termios)
  514. return;
  515. cflag = info->tty->termios->c_cflag;
  516. if (!(port = info->port))
  517. return;
  518. ustcnt = uart->ustcnt;
  519. uart->ustcnt = ustcnt & ~USTCNT_TXEN;
  520. i = cflag & CBAUD;
  521. if (i & CBAUDEX) {
  522. i = (i & ~CBAUDEX) + B38400;
  523. }
  524. info->baud = baud_table[i];
  525. uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
  526. PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
  527. ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
  528. if ((cflag & CSIZE) == CS8)
  529. ustcnt |= USTCNT_8_7;
  530. if (cflag & CSTOPB)
  531. ustcnt |= USTCNT_STOP;
  532. if (cflag & PARENB)
  533. ustcnt |= USTCNT_PARITYEN;
  534. if (cflag & PARODD)
  535. ustcnt |= USTCNT_ODD_EVEN;
  536. #ifdef CONFIG_SERIAL_68328_RTS_CTS
  537. if (cflag & CRTSCTS) {
  538. uart->utx.w &= ~ UTX_NOCTS;
  539. } else {
  540. uart->utx.w |= UTX_NOCTS;
  541. }
  542. #endif
  543. ustcnt |= USTCNT_TXEN;
  544. uart->ustcnt = ustcnt;
  545. return;
  546. }
  547. /*
  548. * Fair output driver allows a process to speak.
  549. */
  550. static void rs_fair_output(void)
  551. {
  552. int left; /* Output no more than that */
  553. unsigned long flags;
  554. struct m68k_serial *info = &m68k_soft[0];
  555. char c;
  556. if (info == 0) return;
  557. if (info->xmit_buf == 0) return;
  558. save_flags(flags); cli();
  559. left = info->xmit_cnt;
  560. while (left != 0) {
  561. c = info->xmit_buf[info->xmit_tail];
  562. info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
  563. info->xmit_cnt--;
  564. restore_flags(flags);
  565. rs_put_char(c);
  566. save_flags(flags); cli();
  567. left = min(info->xmit_cnt, left-1);
  568. }
  569. /* Last character is being transmitted now (hopefully). */
  570. udelay(5);
  571. restore_flags(flags);
  572. return;
  573. }
  574. /*
  575. * m68k_console_print is registered for printk.
  576. */
  577. void console_print_68328(const char *p)
  578. {
  579. char c;
  580. while((c=*(p++)) != 0) {
  581. if(c == '\n')
  582. rs_put_char('\r');
  583. rs_put_char(c);
  584. }
  585. /* Comment this if you want to have a strict interrupt-driven output */
  586. rs_fair_output();
  587. return;
  588. }
  589. static void rs_set_ldisc(struct tty_struct *tty)
  590. {
  591. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  592. if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
  593. return;
  594. info->is_cons = (tty->termios->c_line == N_TTY);
  595. printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
  596. }
  597. static void rs_flush_chars(struct tty_struct *tty)
  598. {
  599. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  600. m68328_uart *uart = &uart_addr[info->line];
  601. unsigned long flags;
  602. if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
  603. return;
  604. #ifndef USE_INTS
  605. for(;;) {
  606. #endif
  607. /* Enable transmitter */
  608. save_flags(flags); cli();
  609. if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  610. !info->xmit_buf) {
  611. restore_flags(flags);
  612. return;
  613. }
  614. #ifdef USE_INTS
  615. uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
  616. #else
  617. uart->ustcnt |= USTCNT_TXEN;
  618. #endif
  619. #ifdef USE_INTS
  620. if (uart->utx.w & UTX_TX_AVAIL) {
  621. #else
  622. if (1) {
  623. #endif
  624. /* Send char */
  625. uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
  626. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  627. info->xmit_cnt--;
  628. }
  629. #ifndef USE_INTS
  630. while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
  631. }
  632. #endif
  633. restore_flags(flags);
  634. }
  635. extern void console_printn(const char * b, int count);
  636. static int rs_write(struct tty_struct * tty,
  637. const unsigned char *buf, int count)
  638. {
  639. int c, total = 0;
  640. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  641. m68328_uart *uart = &uart_addr[info->line];
  642. unsigned long flags;
  643. if (serial_paranoia_check(info, tty->name, "rs_write"))
  644. return 0;
  645. if (!tty || !info->xmit_buf)
  646. return 0;
  647. save_flags(flags);
  648. while (1) {
  649. cli();
  650. c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  651. SERIAL_XMIT_SIZE - info->xmit_head));
  652. if (c <= 0)
  653. break;
  654. memcpy(info->xmit_buf + info->xmit_head, buf, c);
  655. info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
  656. info->xmit_cnt += c;
  657. restore_flags(flags);
  658. buf += c;
  659. count -= c;
  660. total += c;
  661. }
  662. if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
  663. /* Enable transmitter */
  664. cli();
  665. #ifndef USE_INTS
  666. while(info->xmit_cnt) {
  667. #endif
  668. uart->ustcnt |= USTCNT_TXEN;
  669. #ifdef USE_INTS
  670. uart->ustcnt |= USTCNT_TX_INTR_MASK;
  671. #else
  672. while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
  673. #endif
  674. if (uart->utx.w & UTX_TX_AVAIL) {
  675. uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
  676. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  677. info->xmit_cnt--;
  678. }
  679. #ifndef USE_INTS
  680. }
  681. #endif
  682. restore_flags(flags);
  683. }
  684. restore_flags(flags);
  685. return total;
  686. }
  687. static int rs_write_room(struct tty_struct *tty)
  688. {
  689. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  690. int ret;
  691. if (serial_paranoia_check(info, tty->name, "rs_write_room"))
  692. return 0;
  693. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  694. if (ret < 0)
  695. ret = 0;
  696. return ret;
  697. }
  698. static int rs_chars_in_buffer(struct tty_struct *tty)
  699. {
  700. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  701. if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
  702. return 0;
  703. return info->xmit_cnt;
  704. }
  705. static void rs_flush_buffer(struct tty_struct *tty)
  706. {
  707. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  708. if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
  709. return;
  710. cli();
  711. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  712. sti();
  713. tty_wakeup(tty);
  714. }
  715. /*
  716. * ------------------------------------------------------------
  717. * rs_throttle()
  718. *
  719. * This routine is called by the upper-layer tty layer to signal that
  720. * incoming characters should be throttled.
  721. * ------------------------------------------------------------
  722. */
  723. static void rs_throttle(struct tty_struct * tty)
  724. {
  725. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  726. if (serial_paranoia_check(info, tty->name, "rs_throttle"))
  727. return;
  728. if (I_IXOFF(tty))
  729. info->x_char = STOP_CHAR(tty);
  730. /* Turn off RTS line (do this atomic) */
  731. }
  732. static void rs_unthrottle(struct tty_struct * tty)
  733. {
  734. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  735. if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
  736. return;
  737. if (I_IXOFF(tty)) {
  738. if (info->x_char)
  739. info->x_char = 0;
  740. else
  741. info->x_char = START_CHAR(tty);
  742. }
  743. /* Assert RTS line (do this atomic) */
  744. }
  745. /*
  746. * ------------------------------------------------------------
  747. * rs_ioctl() and friends
  748. * ------------------------------------------------------------
  749. */
  750. static int get_serial_info(struct m68k_serial * info,
  751. struct serial_struct * retinfo)
  752. {
  753. struct serial_struct tmp;
  754. if (!retinfo)
  755. return -EFAULT;
  756. memset(&tmp, 0, sizeof(tmp));
  757. tmp.type = info->type;
  758. tmp.line = info->line;
  759. tmp.port = info->port;
  760. tmp.irq = info->irq;
  761. tmp.flags = info->flags;
  762. tmp.baud_base = info->baud_base;
  763. tmp.close_delay = info->close_delay;
  764. tmp.closing_wait = info->closing_wait;
  765. tmp.custom_divisor = info->custom_divisor;
  766. copy_to_user(retinfo,&tmp,sizeof(*retinfo));
  767. return 0;
  768. }
  769. static int set_serial_info(struct m68k_serial * info,
  770. struct serial_struct * new_info)
  771. {
  772. struct serial_struct new_serial;
  773. struct m68k_serial old_info;
  774. int retval = 0;
  775. if (!new_info)
  776. return -EFAULT;
  777. copy_from_user(&new_serial,new_info,sizeof(new_serial));
  778. old_info = *info;
  779. if (!capable(CAP_SYS_ADMIN)) {
  780. if ((new_serial.baud_base != info->baud_base) ||
  781. (new_serial.type != info->type) ||
  782. (new_serial.close_delay != info->close_delay) ||
  783. ((new_serial.flags & ~S_USR_MASK) !=
  784. (info->flags & ~S_USR_MASK)))
  785. return -EPERM;
  786. info->flags = ((info->flags & ~S_USR_MASK) |
  787. (new_serial.flags & S_USR_MASK));
  788. info->custom_divisor = new_serial.custom_divisor;
  789. goto check_and_exit;
  790. }
  791. if (info->count > 1)
  792. return -EBUSY;
  793. /*
  794. * OK, past this point, all the error checking has been done.
  795. * At this point, we start making changes.....
  796. */
  797. info->baud_base = new_serial.baud_base;
  798. info->flags = ((info->flags & ~S_FLAGS) |
  799. (new_serial.flags & S_FLAGS));
  800. info->type = new_serial.type;
  801. info->close_delay = new_serial.close_delay;
  802. info->closing_wait = new_serial.closing_wait;
  803. check_and_exit:
  804. retval = startup(info);
  805. return retval;
  806. }
  807. /*
  808. * get_lsr_info - get line status register info
  809. *
  810. * Purpose: Let user call ioctl() to get info when the UART physically
  811. * is emptied. On bus types like RS485, the transmitter must
  812. * release the bus after transmitting. This must be done when
  813. * the transmit shift register is empty, not be done when the
  814. * transmit holding register is empty. This functionality
  815. * allows an RS485 driver to be written in user space.
  816. */
  817. static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
  818. {
  819. #ifdef CONFIG_SERIAL_68328_RTS_CTS
  820. m68328_uart *uart = &uart_addr[info->line];
  821. #endif
  822. unsigned char status;
  823. cli();
  824. #ifdef CONFIG_SERIAL_68328_RTS_CTS
  825. status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
  826. #else
  827. status = 0;
  828. #endif
  829. sti();
  830. put_user(status,value);
  831. return 0;
  832. }
  833. /*
  834. * This routine sends a break character out the serial port.
  835. */
  836. static void send_break(struct m68k_serial * info, unsigned int duration)
  837. {
  838. m68328_uart *uart = &uart_addr[info->line];
  839. unsigned long flags;
  840. if (!info->port)
  841. return;
  842. save_flags(flags);
  843. cli();
  844. #ifdef USE_INTS
  845. uart->utx.w |= UTX_SEND_BREAK;
  846. msleep_interruptible(duration);
  847. uart->utx.w &= ~UTX_SEND_BREAK;
  848. #endif
  849. restore_flags(flags);
  850. }
  851. static int rs_ioctl(struct tty_struct *tty, struct file * file,
  852. unsigned int cmd, unsigned long arg)
  853. {
  854. int error;
  855. struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
  856. int retval;
  857. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  858. return -ENODEV;
  859. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  860. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
  861. (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
  862. if (tty->flags & (1 << TTY_IO_ERROR))
  863. return -EIO;
  864. }
  865. switch (cmd) {
  866. case TCSBRK: /* SVID version: non-zero arg --> no break */
  867. retval = tty_check_change(tty);
  868. if (retval)
  869. return retval;
  870. tty_wait_until_sent(tty, 0);
  871. if (!arg)
  872. send_break(info, 250); /* 1/4 second */
  873. return 0;
  874. case TCSBRKP: /* support for POSIX tcsendbreak() */
  875. retval = tty_check_change(tty);
  876. if (retval)
  877. return retval;
  878. tty_wait_until_sent(tty, 0);
  879. send_break(info, arg ? arg*(100) : 250);
  880. return 0;
  881. case TIOCGSOFTCAR:
  882. error = put_user(C_CLOCAL(tty) ? 1 : 0,
  883. (unsigned long *) arg);
  884. if (error)
  885. return error;
  886. return 0;
  887. case TIOCSSOFTCAR:
  888. get_user(arg, (unsigned long *) arg);
  889. tty->termios->c_cflag =
  890. ((tty->termios->c_cflag & ~CLOCAL) |
  891. (arg ? CLOCAL : 0));
  892. return 0;
  893. case TIOCGSERIAL:
  894. if (access_ok(VERIFY_WRITE, (void *) arg,
  895. sizeof(struct serial_struct)))
  896. return get_serial_info(info,
  897. (struct serial_struct *) arg);
  898. return -EFAULT;
  899. case TIOCSSERIAL:
  900. return set_serial_info(info,
  901. (struct serial_struct *) arg);
  902. case TIOCSERGETLSR: /* Get line status register */
  903. if (access_ok(VERIFY_WRITE, (void *) arg,
  904. sizeof(unsigned int));
  905. return get_lsr_info(info, (unsigned int *) arg);
  906. return -EFAULT;
  907. case TIOCSERGSTRUCT:
  908. if (!access_ok(VERIFY_WRITE, (void *) arg,
  909. sizeof(struct m68k_serial)))
  910. return -EFAULT;
  911. copy_to_user((struct m68k_serial *) arg,
  912. info, sizeof(struct m68k_serial));
  913. return 0;
  914. default:
  915. return -ENOIOCTLCMD;
  916. }
  917. return 0;
  918. }
  919. static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
  920. {
  921. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  922. if (tty->termios->c_cflag == old_termios->c_cflag)
  923. return;
  924. change_speed(info);
  925. if ((old_termios->c_cflag & CRTSCTS) &&
  926. !(tty->termios->c_cflag & CRTSCTS)) {
  927. tty->hw_stopped = 0;
  928. rs_start(tty);
  929. }
  930. }
  931. /*
  932. * ------------------------------------------------------------
  933. * rs_close()
  934. *
  935. * This routine is called when the serial port gets closed. First, we
  936. * wait for the last remaining data to be sent. Then, we unlink its
  937. * S structure from the interrupt chain if necessary, and we free
  938. * that IRQ if nothing is left in the chain.
  939. * ------------------------------------------------------------
  940. */
  941. static void rs_close(struct tty_struct *tty, struct file * filp)
  942. {
  943. struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
  944. m68328_uart *uart = &uart_addr[info->line];
  945. unsigned long flags;
  946. if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
  947. return;
  948. save_flags(flags); cli();
  949. if (tty_hung_up_p(filp)) {
  950. restore_flags(flags);
  951. return;
  952. }
  953. if ((tty->count == 1) && (info->count != 1)) {
  954. /*
  955. * Uh, oh. tty->count is 1, which means that the tty
  956. * structure will be freed. Info->count should always
  957. * be one in these conditions. If it's greater than
  958. * one, we've got real problems, since it means the
  959. * serial port won't be shutdown.
  960. */
  961. printk("rs_close: bad serial port count; tty->count is 1, "
  962. "info->count is %d\n", info->count);
  963. info->count = 1;
  964. }
  965. if (--info->count < 0) {
  966. printk("rs_close: bad serial port count for ttyS%d: %d\n",
  967. info->line, info->count);
  968. info->count = 0;
  969. }
  970. if (info->count) {
  971. restore_flags(flags);
  972. return;
  973. }
  974. info->flags |= S_CLOSING;
  975. /*
  976. * Now we wait for the transmit buffer to clear; and we notify
  977. * the line discipline to only process XON/XOFF characters.
  978. */
  979. tty->closing = 1;
  980. if (info->closing_wait != S_CLOSING_WAIT_NONE)
  981. tty_wait_until_sent(tty, info->closing_wait);
  982. /*
  983. * At this point we stop accepting input. To do this, we
  984. * disable the receive line status interrupts, and tell the
  985. * interrupt driver to stop checking the data ready bit in the
  986. * line status register.
  987. */
  988. uart->ustcnt &= ~USTCNT_RXEN;
  989. uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
  990. shutdown(info);
  991. if (tty->driver->flush_buffer)
  992. tty->driver->flush_buffer(tty);
  993. tty_ldisc_flush(tty);
  994. tty->closing = 0;
  995. info->event = 0;
  996. info->tty = 0;
  997. #warning "This is not and has never been valid so fix it"
  998. #if 0
  999. if (tty->ldisc.num != ldiscs[N_TTY].num) {
  1000. if (tty->ldisc.close)
  1001. (tty->ldisc.close)(tty);
  1002. tty->ldisc = ldiscs[N_TTY];
  1003. tty->termios->c_line = N_TTY;
  1004. if (tty->ldisc.open)
  1005. (tty->ldisc.open)(tty);
  1006. }
  1007. #endif
  1008. if (info->blocked_open) {
  1009. if (info->close_delay) {
  1010. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  1011. }
  1012. wake_up_interruptible(&info->open_wait);
  1013. }
  1014. info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
  1015. wake_up_interruptible(&info->close_wait);
  1016. restore_flags(flags);
  1017. }
  1018. /*
  1019. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  1020. */
  1021. void rs_hangup(struct tty_struct *tty)
  1022. {
  1023. struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
  1024. if (serial_paranoia_check(info, tty->name, "rs_hangup"))
  1025. return;
  1026. rs_flush_buffer(tty);
  1027. shutdown(info);
  1028. info->event = 0;
  1029. info->count = 0;
  1030. info->flags &= ~S_NORMAL_ACTIVE;
  1031. info->tty = 0;
  1032. wake_up_interruptible(&info->open_wait);
  1033. }
  1034. /*
  1035. * ------------------------------------------------------------
  1036. * rs_open() and friends
  1037. * ------------------------------------------------------------
  1038. */
  1039. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  1040. struct m68k_serial *info)
  1041. {
  1042. DECLARE_WAITQUEUE(wait, current);
  1043. int retval;
  1044. int do_clocal = 0;
  1045. /*
  1046. * If the device is in the middle of being closed, then block
  1047. * until it's done, and then try again.
  1048. */
  1049. if (info->flags & S_CLOSING) {
  1050. interruptible_sleep_on(&info->close_wait);
  1051. #ifdef SERIAL_DO_RESTART
  1052. if (info->flags & S_HUP_NOTIFY)
  1053. return -EAGAIN;
  1054. else
  1055. return -ERESTARTSYS;
  1056. #else
  1057. return -EAGAIN;
  1058. #endif
  1059. }
  1060. /*
  1061. * If non-blocking mode is set, or the port is not enabled,
  1062. * then make the check up front and then exit.
  1063. */
  1064. if ((filp->f_flags & O_NONBLOCK) ||
  1065. (tty->flags & (1 << TTY_IO_ERROR))) {
  1066. info->flags |= S_NORMAL_ACTIVE;
  1067. return 0;
  1068. }
  1069. if (tty->termios->c_cflag & CLOCAL)
  1070. do_clocal = 1;
  1071. /*
  1072. * Block waiting for the carrier detect and the line to become
  1073. * free (i.e., not in use by the callout). While we are in
  1074. * this loop, info->count is dropped by one, so that
  1075. * rs_close() knows when to free things. We restore it upon
  1076. * exit, either normal or abnormal.
  1077. */
  1078. retval = 0;
  1079. add_wait_queue(&info->open_wait, &wait);
  1080. info->count--;
  1081. info->blocked_open++;
  1082. while (1) {
  1083. cli();
  1084. m68k_rtsdtr(info, 1);
  1085. sti();
  1086. current->state = TASK_INTERRUPTIBLE;
  1087. if (tty_hung_up_p(filp) ||
  1088. !(info->flags & S_INITIALIZED)) {
  1089. #ifdef SERIAL_DO_RESTART
  1090. if (info->flags & S_HUP_NOTIFY)
  1091. retval = -EAGAIN;
  1092. else
  1093. retval = -ERESTARTSYS;
  1094. #else
  1095. retval = -EAGAIN;
  1096. #endif
  1097. break;
  1098. }
  1099. if (!(info->flags & S_CLOSING) && do_clocal)
  1100. break;
  1101. if (signal_pending(current)) {
  1102. retval = -ERESTARTSYS;
  1103. break;
  1104. }
  1105. schedule();
  1106. }
  1107. current->state = TASK_RUNNING;
  1108. remove_wait_queue(&info->open_wait, &wait);
  1109. if (!tty_hung_up_p(filp))
  1110. info->count++;
  1111. info->blocked_open--;
  1112. if (retval)
  1113. return retval;
  1114. info->flags |= S_NORMAL_ACTIVE;
  1115. return 0;
  1116. }
  1117. /*
  1118. * This routine is called whenever a serial port is opened. It
  1119. * enables interrupts for a serial port, linking in its S structure into
  1120. * the IRQ chain. It also performs the serial-specific
  1121. * initialization for the tty structure.
  1122. */
  1123. int rs_open(struct tty_struct *tty, struct file * filp)
  1124. {
  1125. struct m68k_serial *info;
  1126. int retval, line;
  1127. line = tty->index;
  1128. if (line >= NR_PORTS || line < 0) /* we have exactly one */
  1129. return -ENODEV;
  1130. info = &m68k_soft[line];
  1131. if (serial_paranoia_check(info, tty->name, "rs_open"))
  1132. return -ENODEV;
  1133. info->count++;
  1134. tty->driver_data = info;
  1135. info->tty = tty;
  1136. /*
  1137. * Start up serial port
  1138. */
  1139. retval = startup(info);
  1140. if (retval)
  1141. return retval;
  1142. return block_til_ready(tty, filp, info);
  1143. }
  1144. /* Finally, routines used to initialize the serial driver. */
  1145. static void show_serial_version(void)
  1146. {
  1147. printk("MC68328 serial driver version 1.00\n");
  1148. }
  1149. #ifdef CONFIG_PM
  1150. /* Serial Power management
  1151. * The console (currently fixed at line 0) is a special case for power
  1152. * management because the kernel is so chatty. The console will be
  1153. * explicitly disabled my our power manager as the last minute, so we won't
  1154. * mess with it here.
  1155. */
  1156. static struct pm_dev *serial_pm[NR_PORTS];
  1157. static int serial_pm_callback(struct pm_dev *dev, pm_request_t request, void *data)
  1158. {
  1159. struct m68k_serial *info = (struct m68k_serial *)dev->data;
  1160. if(info == NULL)
  1161. return -1;
  1162. /* special case for line 0 - pm restores it */
  1163. if(info->line == 0)
  1164. return 0;
  1165. switch (request) {
  1166. case PM_SUSPEND:
  1167. shutdown(info);
  1168. break;
  1169. case PM_RESUME:
  1170. startup(info);
  1171. break;
  1172. }
  1173. return 0;
  1174. }
  1175. void shutdown_console(void)
  1176. {
  1177. struct m68k_serial *info = &m68k_soft[0];
  1178. /* HACK: wait a bit for any pending printk's to be dumped */
  1179. {
  1180. int i = 10000;
  1181. while(i--);
  1182. }
  1183. shutdown(info);
  1184. }
  1185. void startup_console(void)
  1186. {
  1187. struct m68k_serial *info = &m68k_soft[0];
  1188. startup(info);
  1189. }
  1190. #endif
  1191. static struct tty_operations rs_ops = {
  1192. .open = rs_open,
  1193. .close = rs_close,
  1194. .write = rs_write,
  1195. .flush_chars = rs_flush_chars,
  1196. .write_room = rs_write_room,
  1197. .chars_in_buffer = rs_chars_in_buffer,
  1198. .flush_buffer = rs_flush_buffer,
  1199. .ioctl = rs_ioctl,
  1200. .throttle = rs_throttle,
  1201. .unthrottle = rs_unthrottle,
  1202. .set_termios = rs_set_termios,
  1203. .stop = rs_stop,
  1204. .start = rs_start,
  1205. .hangup = rs_hangup,
  1206. .set_ldisc = rs_set_ldisc,
  1207. };
  1208. /* rs_init inits the driver */
  1209. static int __init
  1210. rs68328_init(void)
  1211. {
  1212. int flags, i;
  1213. struct m68k_serial *info;
  1214. serial_driver = alloc_tty_driver(NR_PORTS);
  1215. if (!serial_driver)
  1216. return -ENOMEM;
  1217. show_serial_version();
  1218. /* Initialize the tty_driver structure */
  1219. /* SPARC: Not all of this is exactly right for us. */
  1220. serial_driver->name = "ttyS";
  1221. serial_driver->major = TTY_MAJOR;
  1222. serial_driver->minor_start = 64;
  1223. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1224. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  1225. serial_driver->init_termios = tty_std_termios;
  1226. serial_driver->init_termios.c_cflag =
  1227. m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
  1228. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  1229. tty_set_operations(serial_driver, &rs_ops);
  1230. if (tty_register_driver(serial_driver)) {
  1231. put_tty_driver(serial_driver);
  1232. printk(KERN_ERR "Couldn't register serial driver\n");
  1233. return -ENOMEM;
  1234. }
  1235. save_flags(flags); cli();
  1236. for(i=0;i<NR_PORTS;i++) {
  1237. info = &m68k_soft[i];
  1238. info->magic = SERIAL_MAGIC;
  1239. info->port = (int) &uart_addr[i];
  1240. info->tty = 0;
  1241. info->irq = uart_irqs[i];
  1242. info->custom_divisor = 16;
  1243. info->close_delay = 50;
  1244. info->closing_wait = 3000;
  1245. info->x_char = 0;
  1246. info->event = 0;
  1247. info->count = 0;
  1248. info->blocked_open = 0;
  1249. INIT_WORK(&info->tqueue, do_softint, info);
  1250. INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
  1251. init_waitqueue_head(&info->open_wait);
  1252. init_waitqueue_head(&info->close_wait);
  1253. info->line = i;
  1254. info->is_cons = 1; /* Means shortcuts work */
  1255. printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
  1256. info->port, info->irq);
  1257. printk(" is a builtin MC68328 UART\n");
  1258. IRQ_ports[info->irq] = info; /* waste of space */
  1259. #ifdef CONFIG_M68VZ328
  1260. if (i > 0 )
  1261. PJSEL &= 0xCF; /* PSW enable second port output */
  1262. #endif
  1263. if (request_irq(uart_irqs[i],
  1264. rs_interrupt,
  1265. IRQ_FLG_STD,
  1266. "M68328_UART", NULL))
  1267. panic("Unable to attach 68328 serial interrupt\n");
  1268. #ifdef CONFIG_PM
  1269. serial_pm[i] = pm_register(PM_SYS_DEV, PM_SYS_COM, serial_pm_callback);
  1270. if (serial_pm[i])
  1271. serial_pm[i]->data = info;
  1272. #endif
  1273. }
  1274. restore_flags(flags);
  1275. return 0;
  1276. }
  1277. module_init(rs68328_init);
  1278. static void m68328_set_baud(void)
  1279. {
  1280. unsigned short ustcnt;
  1281. int i;
  1282. ustcnt = USTCNT;
  1283. USTCNT = ustcnt & ~USTCNT_TXEN;
  1284. again:
  1285. for (i = 0; i < sizeof(baud_table) / sizeof(baud_table[0]); i++)
  1286. if (baud_table[i] == m68328_console_baud)
  1287. break;
  1288. if (i >= sizeof(baud_table) / sizeof(baud_table[0])) {
  1289. m68328_console_baud = 9600;
  1290. goto again;
  1291. }
  1292. UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
  1293. PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
  1294. ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
  1295. ustcnt |= USTCNT_8_7;
  1296. ustcnt |= USTCNT_TXEN;
  1297. USTCNT = ustcnt;
  1298. m68328_console_initted = 1;
  1299. return;
  1300. }
  1301. int m68328_console_setup(struct console *cp, char *arg)
  1302. {
  1303. int i, n = CONSOLE_BAUD_RATE;
  1304. if (!cp)
  1305. return(-1);
  1306. if (arg)
  1307. n = simple_strtoul(arg,NULL,0);
  1308. for (i = 0; i < BAUD_TABLE_SIZE; i++)
  1309. if (baud_table[i] == n)
  1310. break;
  1311. if (i < BAUD_TABLE_SIZE) {
  1312. m68328_console_baud = n;
  1313. m68328_console_cbaud = 0;
  1314. if (i > 15) {
  1315. m68328_console_cbaud |= CBAUDEX;
  1316. i -= 15;
  1317. }
  1318. m68328_console_cbaud |= i;
  1319. }
  1320. m68328_set_baud(); /* make sure baud rate changes */
  1321. return(0);
  1322. }
  1323. static struct tty_driver *m68328_console_device(struct console *c, int *index)
  1324. {
  1325. *index = c->index;
  1326. return serial_driver;
  1327. }
  1328. void m68328_console_write (struct console *co, const char *str,
  1329. unsigned int count)
  1330. {
  1331. if (!m68328_console_initted)
  1332. m68328_set_baud();
  1333. while (count--) {
  1334. if (*str == '\n')
  1335. rs_put_char('\r');
  1336. rs_put_char( *str++ );
  1337. }
  1338. }
  1339. static struct console m68328_driver = {
  1340. .name = "ttyS",
  1341. .write = m68328_console_write,
  1342. .device = m68328_console_device,
  1343. .setup = m68328_console_setup,
  1344. .flags = CON_PRINTBUFFER,
  1345. .index = -1,
  1346. };
  1347. static int __init m68328_console_init(void)
  1348. {
  1349. register_console(&m68328_driver);
  1350. return 0;
  1351. }
  1352. console_initcall(m68328_console_init);