68328serial.c 37 KB

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