68328serial.c 35 KB

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