68328serial.c 32 KB

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