68328serial.c 34 KB

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