68328serial.c 34 KB

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