68328serial.c 35 KB

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