68328serial.c 37 KB

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