68328serial.c 37 KB

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