68328serial.c 36 KB

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