68328serial.c 35 KB

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