68328serial.c 35 KB

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