68328serial.c 32 KB

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