68328serial.c 35 KB

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