68328serial.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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->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->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->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->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->tty)
  382. clear_bit(TTY_IO_ERROR, &info->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->tty)
  409. set_bit(TTY_IO_ERROR, &info->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->tty || !info->tty->termios)
  472. return;
  473. cflag = info->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. struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
  816. int retval;
  817. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  818. return -ENODEV;
  819. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  820. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
  821. (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
  822. if (tty->flags & (1 << TTY_IO_ERROR))
  823. return -EIO;
  824. }
  825. switch (cmd) {
  826. case TCSBRK: /* SVID version: non-zero arg --> no break */
  827. retval = tty_check_change(tty);
  828. if (retval)
  829. return retval;
  830. tty_wait_until_sent(tty, 0);
  831. if (!arg)
  832. send_break(info, 250); /* 1/4 second */
  833. return 0;
  834. case TCSBRKP: /* support for POSIX tcsendbreak() */
  835. retval = tty_check_change(tty);
  836. if (retval)
  837. return retval;
  838. tty_wait_until_sent(tty, 0);
  839. send_break(info, arg ? arg*(100) : 250);
  840. return 0;
  841. case TIOCGSERIAL:
  842. return get_serial_info(info,
  843. (struct serial_struct *) arg);
  844. case TIOCSSERIAL:
  845. return set_serial_info(info,
  846. (struct serial_struct *) arg);
  847. case TIOCSERGETLSR: /* Get line status register */
  848. return get_lsr_info(info, (unsigned int *) arg);
  849. case TIOCSERGSTRUCT:
  850. if (copy_to_user((struct m68k_serial *) arg,
  851. info, sizeof(struct m68k_serial)))
  852. return -EFAULT;
  853. return 0;
  854. default:
  855. return -ENOIOCTLCMD;
  856. }
  857. return 0;
  858. }
  859. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  860. {
  861. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  862. change_speed(info);
  863. if ((old_termios->c_cflag & CRTSCTS) &&
  864. !(tty->termios->c_cflag & CRTSCTS)) {
  865. tty->hw_stopped = 0;
  866. rs_start(tty);
  867. }
  868. }
  869. /*
  870. * ------------------------------------------------------------
  871. * rs_close()
  872. *
  873. * This routine is called when the serial port gets closed. First, we
  874. * wait for the last remaining data to be sent. Then, we unlink its
  875. * S structure from the interrupt chain if necessary, and we free
  876. * that IRQ if nothing is left in the chain.
  877. * ------------------------------------------------------------
  878. */
  879. static void rs_close(struct tty_struct *tty, struct file * filp)
  880. {
  881. struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
  882. m68328_uart *uart = &uart_addr[info->line];
  883. unsigned long flags;
  884. if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
  885. return;
  886. local_irq_save(flags);
  887. if (tty_hung_up_p(filp)) {
  888. local_irq_restore(flags);
  889. return;
  890. }
  891. if ((tty->count == 1) && (info->count != 1)) {
  892. /*
  893. * Uh, oh. tty->count is 1, which means that the tty
  894. * structure will be freed. Info->count should always
  895. * be one in these conditions. If it's greater than
  896. * one, we've got real problems, since it means the
  897. * serial port won't be shutdown.
  898. */
  899. printk("rs_close: bad serial port count; tty->count is 1, "
  900. "info->count is %d\n", info->count);
  901. info->count = 1;
  902. }
  903. if (--info->count < 0) {
  904. printk("rs_close: bad serial port count for ttyS%d: %d\n",
  905. info->line, info->count);
  906. info->count = 0;
  907. }
  908. if (info->count) {
  909. local_irq_restore(flags);
  910. return;
  911. }
  912. info->flags |= S_CLOSING;
  913. /*
  914. * Now we wait for the transmit buffer to clear; and we notify
  915. * the line discipline to only process XON/XOFF characters.
  916. */
  917. tty->closing = 1;
  918. if (info->closing_wait != S_CLOSING_WAIT_NONE)
  919. tty_wait_until_sent(tty, info->closing_wait);
  920. /*
  921. * At this point we stop accepting input. To do this, we
  922. * disable the receive line status interrupts, and tell the
  923. * interrupt driver to stop checking the data ready bit in the
  924. * line status register.
  925. */
  926. uart->ustcnt &= ~USTCNT_RXEN;
  927. uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
  928. shutdown(info);
  929. rs_flush_buffer(tty);
  930. tty_ldisc_flush(tty);
  931. tty->closing = 0;
  932. info->event = 0;
  933. info->tty = NULL;
  934. #warning "This is not and has never been valid so fix it"
  935. #if 0
  936. if (tty->ldisc.num != ldiscs[N_TTY].num) {
  937. if (tty->ldisc.close)
  938. (tty->ldisc.close)(tty);
  939. tty->ldisc = ldiscs[N_TTY];
  940. tty->termios->c_line = N_TTY;
  941. if (tty->ldisc.open)
  942. (tty->ldisc.open)(tty);
  943. }
  944. #endif
  945. if (info->blocked_open) {
  946. if (info->close_delay) {
  947. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  948. }
  949. wake_up_interruptible(&info->open_wait);
  950. }
  951. info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
  952. wake_up_interruptible(&info->close_wait);
  953. local_irq_restore(flags);
  954. }
  955. /*
  956. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  957. */
  958. void rs_hangup(struct tty_struct *tty)
  959. {
  960. struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
  961. if (serial_paranoia_check(info, tty->name, "rs_hangup"))
  962. return;
  963. rs_flush_buffer(tty);
  964. shutdown(info);
  965. info->event = 0;
  966. info->count = 0;
  967. info->flags &= ~S_NORMAL_ACTIVE;
  968. info->tty = NULL;
  969. wake_up_interruptible(&info->open_wait);
  970. }
  971. /*
  972. * ------------------------------------------------------------
  973. * rs_open() and friends
  974. * ------------------------------------------------------------
  975. */
  976. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  977. struct m68k_serial *info)
  978. {
  979. DECLARE_WAITQUEUE(wait, current);
  980. int retval;
  981. int do_clocal = 0;
  982. /*
  983. * If the device is in the middle of being closed, then block
  984. * until it's done, and then try again.
  985. */
  986. if (info->flags & S_CLOSING) {
  987. interruptible_sleep_on(&info->close_wait);
  988. #ifdef SERIAL_DO_RESTART
  989. if (info->flags & S_HUP_NOTIFY)
  990. return -EAGAIN;
  991. else
  992. return -ERESTARTSYS;
  993. #else
  994. return -EAGAIN;
  995. #endif
  996. }
  997. /*
  998. * If non-blocking mode is set, or the port is not enabled,
  999. * then make the check up front and then exit.
  1000. */
  1001. if ((filp->f_flags & O_NONBLOCK) ||
  1002. (tty->flags & (1 << TTY_IO_ERROR))) {
  1003. info->flags |= S_NORMAL_ACTIVE;
  1004. return 0;
  1005. }
  1006. if (tty->termios->c_cflag & CLOCAL)
  1007. do_clocal = 1;
  1008. /*
  1009. * Block waiting for the carrier detect and the line to become
  1010. * free (i.e., not in use by the callout). While we are in
  1011. * this loop, info->count is dropped by one, so that
  1012. * rs_close() knows when to free things. We restore it upon
  1013. * exit, either normal or abnormal.
  1014. */
  1015. retval = 0;
  1016. add_wait_queue(&info->open_wait, &wait);
  1017. info->count--;
  1018. info->blocked_open++;
  1019. while (1) {
  1020. local_irq_disable();
  1021. m68k_rtsdtr(info, 1);
  1022. local_irq_enable();
  1023. current->state = TASK_INTERRUPTIBLE;
  1024. if (tty_hung_up_p(filp) ||
  1025. !(info->flags & S_INITIALIZED)) {
  1026. #ifdef SERIAL_DO_RESTART
  1027. if (info->flags & S_HUP_NOTIFY)
  1028. retval = -EAGAIN;
  1029. else
  1030. retval = -ERESTARTSYS;
  1031. #else
  1032. retval = -EAGAIN;
  1033. #endif
  1034. break;
  1035. }
  1036. if (!(info->flags & S_CLOSING) && do_clocal)
  1037. break;
  1038. if (signal_pending(current)) {
  1039. retval = -ERESTARTSYS;
  1040. break;
  1041. }
  1042. tty_unlock();
  1043. schedule();
  1044. tty_lock();
  1045. }
  1046. current->state = TASK_RUNNING;
  1047. remove_wait_queue(&info->open_wait, &wait);
  1048. if (!tty_hung_up_p(filp))
  1049. info->count++;
  1050. info->blocked_open--;
  1051. if (retval)
  1052. return retval;
  1053. info->flags |= S_NORMAL_ACTIVE;
  1054. return 0;
  1055. }
  1056. /*
  1057. * This routine is called whenever a serial port is opened. It
  1058. * enables interrupts for a serial port, linking in its S structure into
  1059. * the IRQ chain. It also performs the serial-specific
  1060. * initialization for the tty structure.
  1061. */
  1062. int rs_open(struct tty_struct *tty, struct file * filp)
  1063. {
  1064. struct m68k_serial *info;
  1065. int retval, line;
  1066. line = tty->index;
  1067. if (line >= NR_PORTS || line < 0) /* we have exactly one */
  1068. return -ENODEV;
  1069. info = &m68k_soft[line];
  1070. if (serial_paranoia_check(info, tty->name, "rs_open"))
  1071. return -ENODEV;
  1072. info->count++;
  1073. tty->driver_data = info;
  1074. info->tty = tty;
  1075. /*
  1076. * Start up serial port
  1077. */
  1078. retval = startup(info);
  1079. if (retval)
  1080. return retval;
  1081. return block_til_ready(tty, filp, info);
  1082. }
  1083. /* Finally, routines used to initialize the serial driver. */
  1084. static void show_serial_version(void)
  1085. {
  1086. printk("MC68328 serial driver version 1.00\n");
  1087. }
  1088. static const struct tty_operations rs_ops = {
  1089. .open = rs_open,
  1090. .close = rs_close,
  1091. .write = rs_write,
  1092. .flush_chars = rs_flush_chars,
  1093. .write_room = rs_write_room,
  1094. .chars_in_buffer = rs_chars_in_buffer,
  1095. .flush_buffer = rs_flush_buffer,
  1096. .ioctl = rs_ioctl,
  1097. .throttle = rs_throttle,
  1098. .unthrottle = rs_unthrottle,
  1099. .set_termios = rs_set_termios,
  1100. .stop = rs_stop,
  1101. .start = rs_start,
  1102. .hangup = rs_hangup,
  1103. .set_ldisc = rs_set_ldisc,
  1104. };
  1105. /* rs_init inits the driver */
  1106. static int __init
  1107. rs68328_init(void)
  1108. {
  1109. int flags, i;
  1110. struct m68k_serial *info;
  1111. serial_driver = alloc_tty_driver(NR_PORTS);
  1112. if (!serial_driver)
  1113. return -ENOMEM;
  1114. show_serial_version();
  1115. /* Initialize the tty_driver structure */
  1116. /* SPARC: Not all of this is exactly right for us. */
  1117. serial_driver->name = "ttyS";
  1118. serial_driver->major = TTY_MAJOR;
  1119. serial_driver->minor_start = 64;
  1120. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  1121. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  1122. serial_driver->init_termios = tty_std_termios;
  1123. serial_driver->init_termios.c_cflag =
  1124. m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
  1125. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  1126. tty_set_operations(serial_driver, &rs_ops);
  1127. if (tty_register_driver(serial_driver)) {
  1128. put_tty_driver(serial_driver);
  1129. printk(KERN_ERR "Couldn't register serial driver\n");
  1130. return -ENOMEM;
  1131. }
  1132. local_irq_save(flags);
  1133. for(i=0;i<NR_PORTS;i++) {
  1134. info = &m68k_soft[i];
  1135. info->magic = SERIAL_MAGIC;
  1136. info->port = (int) &uart_addr[i];
  1137. info->tty = NULL;
  1138. info->irq = uart_irqs[i];
  1139. info->custom_divisor = 16;
  1140. info->close_delay = 50;
  1141. info->closing_wait = 3000;
  1142. info->x_char = 0;
  1143. info->event = 0;
  1144. info->count = 0;
  1145. info->blocked_open = 0;
  1146. INIT_WORK(&info->tqueue, do_softint);
  1147. INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
  1148. init_waitqueue_head(&info->open_wait);
  1149. init_waitqueue_head(&info->close_wait);
  1150. info->line = i;
  1151. info->is_cons = 1; /* Means shortcuts work */
  1152. printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
  1153. info->port, info->irq);
  1154. printk(" is a builtin MC68328 UART\n");
  1155. #ifdef CONFIG_M68VZ328
  1156. if (i > 0 )
  1157. PJSEL &= 0xCF; /* PSW enable second port output */
  1158. #endif
  1159. if (request_irq(uart_irqs[i],
  1160. rs_interrupt,
  1161. IRQF_DISABLED,
  1162. "M68328_UART", info))
  1163. panic("Unable to attach 68328 serial interrupt\n");
  1164. }
  1165. local_irq_restore(flags);
  1166. return 0;
  1167. }
  1168. module_init(rs68328_init);
  1169. static void m68328_set_baud(void)
  1170. {
  1171. unsigned short ustcnt;
  1172. int i;
  1173. ustcnt = USTCNT;
  1174. USTCNT = ustcnt & ~USTCNT_TXEN;
  1175. again:
  1176. for (i = 0; i < ARRAY_SIZE(baud_table); i++)
  1177. if (baud_table[i] == m68328_console_baud)
  1178. break;
  1179. if (i >= ARRAY_SIZE(baud_table)) {
  1180. m68328_console_baud = 9600;
  1181. goto again;
  1182. }
  1183. UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
  1184. PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
  1185. ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
  1186. ustcnt |= USTCNT_8_7;
  1187. ustcnt |= USTCNT_TXEN;
  1188. USTCNT = ustcnt;
  1189. m68328_console_initted = 1;
  1190. return;
  1191. }
  1192. int m68328_console_setup(struct console *cp, char *arg)
  1193. {
  1194. int i, n = CONSOLE_BAUD_RATE;
  1195. if (!cp)
  1196. return(-1);
  1197. if (arg)
  1198. n = simple_strtoul(arg,NULL,0);
  1199. for (i = 0; i < ARRAY_SIZE(baud_table); i++)
  1200. if (baud_table[i] == n)
  1201. break;
  1202. if (i < ARRAY_SIZE(baud_table)) {
  1203. m68328_console_baud = n;
  1204. m68328_console_cbaud = 0;
  1205. if (i > 15) {
  1206. m68328_console_cbaud |= CBAUDEX;
  1207. i -= 15;
  1208. }
  1209. m68328_console_cbaud |= i;
  1210. }
  1211. m68328_set_baud(); /* make sure baud rate changes */
  1212. return(0);
  1213. }
  1214. static struct tty_driver *m68328_console_device(struct console *c, int *index)
  1215. {
  1216. *index = c->index;
  1217. return serial_driver;
  1218. }
  1219. void m68328_console_write (struct console *co, const char *str,
  1220. unsigned int count)
  1221. {
  1222. if (!m68328_console_initted)
  1223. m68328_set_baud();
  1224. while (count--) {
  1225. if (*str == '\n')
  1226. rs_put_char('\r');
  1227. rs_put_char( *str++ );
  1228. }
  1229. }
  1230. static struct console m68328_driver = {
  1231. .name = "ttyS",
  1232. .write = m68328_console_write,
  1233. .device = m68328_console_device,
  1234. .setup = m68328_console_setup,
  1235. .flags = CON_PRINTBUFFER,
  1236. .index = -1,
  1237. };
  1238. static int __init m68328_console_init(void)
  1239. {
  1240. register_console(&m68328_driver);
  1241. return 0;
  1242. }
  1243. console_initcall(m68328_console_init);