68328serial.c 34 KB

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