68328serial.c 37 KB

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