68328serial.c 35 KB

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