68328serial.c 34 KB

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