68328serial.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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 || !info->xmit_buf) {
  530. local_irq_restore(flags);
  531. return;
  532. }
  533. #ifdef USE_INTS
  534. uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
  535. #else
  536. uart->ustcnt |= USTCNT_TXEN;
  537. #endif
  538. #ifdef USE_INTS
  539. if (uart->utx.w & UTX_TX_AVAIL) {
  540. #else
  541. if (1) {
  542. #endif
  543. /* Send char */
  544. uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
  545. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  546. info->xmit_cnt--;
  547. }
  548. #ifndef USE_INTS
  549. while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
  550. }
  551. #endif
  552. local_irq_restore(flags);
  553. }
  554. extern void console_printn(const char * b, int count);
  555. static int rs_write(struct tty_struct * tty,
  556. const unsigned char *buf, int count)
  557. {
  558. int c, total = 0;
  559. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  560. m68328_uart *uart = &uart_addr[info->line];
  561. unsigned long flags;
  562. if (serial_paranoia_check(info, tty->name, "rs_write"))
  563. return 0;
  564. if (!tty || !info->xmit_buf)
  565. return 0;
  566. local_save_flags(flags);
  567. while (1) {
  568. local_irq_disable();
  569. c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  570. SERIAL_XMIT_SIZE - info->xmit_head));
  571. local_irq_restore(flags);
  572. if (c <= 0)
  573. break;
  574. memcpy(info->xmit_buf + info->xmit_head, buf, c);
  575. local_irq_disable();
  576. info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
  577. info->xmit_cnt += c;
  578. local_irq_restore(flags);
  579. buf += c;
  580. count -= c;
  581. total += c;
  582. }
  583. if (info->xmit_cnt && !tty->stopped) {
  584. /* Enable transmitter */
  585. local_irq_disable();
  586. #ifndef USE_INTS
  587. while(info->xmit_cnt) {
  588. #endif
  589. uart->ustcnt |= USTCNT_TXEN;
  590. #ifdef USE_INTS
  591. uart->ustcnt |= USTCNT_TX_INTR_MASK;
  592. #else
  593. while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
  594. #endif
  595. if (uart->utx.w & UTX_TX_AVAIL) {
  596. uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
  597. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  598. info->xmit_cnt--;
  599. }
  600. #ifndef USE_INTS
  601. }
  602. #endif
  603. local_irq_restore(flags);
  604. }
  605. return total;
  606. }
  607. static int rs_write_room(struct tty_struct *tty)
  608. {
  609. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  610. int ret;
  611. if (serial_paranoia_check(info, tty->name, "rs_write_room"))
  612. return 0;
  613. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  614. if (ret < 0)
  615. ret = 0;
  616. return ret;
  617. }
  618. static int rs_chars_in_buffer(struct tty_struct *tty)
  619. {
  620. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  621. if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
  622. return 0;
  623. return info->xmit_cnt;
  624. }
  625. static void rs_flush_buffer(struct tty_struct *tty)
  626. {
  627. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  628. unsigned long flags;
  629. if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
  630. return;
  631. local_irq_save(flags);
  632. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  633. local_irq_restore(flags);
  634. tty_wakeup(tty);
  635. }
  636. /*
  637. * ------------------------------------------------------------
  638. * rs_throttle()
  639. *
  640. * This routine is called by the upper-layer tty layer to signal that
  641. * incoming characters should be throttled.
  642. * ------------------------------------------------------------
  643. */
  644. static void rs_throttle(struct tty_struct * tty)
  645. {
  646. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  647. if (serial_paranoia_check(info, tty->name, "rs_throttle"))
  648. return;
  649. if (I_IXOFF(tty))
  650. info->x_char = STOP_CHAR(tty);
  651. /* Turn off RTS line (do this atomic) */
  652. }
  653. static void rs_unthrottle(struct tty_struct * tty)
  654. {
  655. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  656. if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
  657. return;
  658. if (I_IXOFF(tty)) {
  659. if (info->x_char)
  660. info->x_char = 0;
  661. else
  662. info->x_char = START_CHAR(tty);
  663. }
  664. /* Assert RTS line (do this atomic) */
  665. }
  666. /*
  667. * ------------------------------------------------------------
  668. * rs_ioctl() and friends
  669. * ------------------------------------------------------------
  670. */
  671. static int get_serial_info(struct m68k_serial * info,
  672. struct serial_struct * retinfo)
  673. {
  674. struct serial_struct tmp;
  675. if (!retinfo)
  676. return -EFAULT;
  677. memset(&tmp, 0, sizeof(tmp));
  678. tmp.type = info->type;
  679. tmp.line = info->line;
  680. tmp.port = info->port;
  681. tmp.irq = info->irq;
  682. tmp.flags = info->tport.flags;
  683. tmp.baud_base = info->baud_base;
  684. tmp.close_delay = info->tport.close_delay;
  685. tmp.closing_wait = info->tport.closing_wait;
  686. tmp.custom_divisor = info->custom_divisor;
  687. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  688. return -EFAULT;
  689. return 0;
  690. }
  691. static int set_serial_info(struct m68k_serial *info, struct tty_struct *tty,
  692. struct serial_struct * new_info)
  693. {
  694. struct tty_port *port = &info->tport;
  695. struct serial_struct new_serial;
  696. struct m68k_serial old_info;
  697. int retval = 0;
  698. if (!new_info)
  699. return -EFAULT;
  700. if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  701. return -EFAULT;
  702. old_info = *info;
  703. if (!capable(CAP_SYS_ADMIN)) {
  704. if ((new_serial.baud_base != info->baud_base) ||
  705. (new_serial.type != info->type) ||
  706. (new_serial.close_delay != port->close_delay) ||
  707. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  708. (port->flags & ~ASYNC_USR_MASK)))
  709. return -EPERM;
  710. port->flags = ((port->flags & ~ASYNC_USR_MASK) |
  711. (new_serial.flags & ASYNC_USR_MASK));
  712. info->custom_divisor = new_serial.custom_divisor;
  713. goto check_and_exit;
  714. }
  715. if (port->count > 1)
  716. return -EBUSY;
  717. /*
  718. * OK, past this point, all the error checking has been done.
  719. * At this point, we start making changes.....
  720. */
  721. info->baud_base = new_serial.baud_base;
  722. port->flags = ((port->flags & ~ASYNC_FLAGS) |
  723. (new_serial.flags & ASYNC_FLAGS));
  724. info->type = new_serial.type;
  725. port->close_delay = new_serial.close_delay;
  726. port->closing_wait = new_serial.closing_wait;
  727. check_and_exit:
  728. retval = startup(info, tty);
  729. return retval;
  730. }
  731. /*
  732. * get_lsr_info - get line status register info
  733. *
  734. * Purpose: Let user call ioctl() to get info when the UART physically
  735. * is emptied. On bus types like RS485, the transmitter must
  736. * release the bus after transmitting. This must be done when
  737. * the transmit shift register is empty, not be done when the
  738. * transmit holding register is empty. This functionality
  739. * allows an RS485 driver to be written in user space.
  740. */
  741. static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
  742. {
  743. #ifdef CONFIG_SERIAL_68328_RTS_CTS
  744. m68328_uart *uart = &uart_addr[info->line];
  745. #endif
  746. unsigned char status;
  747. unsigned long flags;
  748. local_irq_save(flags);
  749. #ifdef CONFIG_SERIAL_68328_RTS_CTS
  750. status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
  751. #else
  752. status = 0;
  753. #endif
  754. local_irq_restore(flags);
  755. return put_user(status, value);
  756. }
  757. /*
  758. * This routine sends a break character out the serial port.
  759. */
  760. static void send_break(struct m68k_serial * info, unsigned int duration)
  761. {
  762. m68328_uart *uart = &uart_addr[info->line];
  763. unsigned long flags;
  764. if (!info->port)
  765. return;
  766. local_irq_save(flags);
  767. #ifdef USE_INTS
  768. uart->utx.w |= UTX_SEND_BREAK;
  769. msleep_interruptible(duration);
  770. uart->utx.w &= ~UTX_SEND_BREAK;
  771. #endif
  772. local_irq_restore(flags);
  773. }
  774. static int rs_ioctl(struct tty_struct *tty,
  775. unsigned int cmd, unsigned long arg)
  776. {
  777. struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
  778. int retval;
  779. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  780. return -ENODEV;
  781. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  782. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
  783. (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
  784. if (tty->flags & (1 << TTY_IO_ERROR))
  785. return -EIO;
  786. }
  787. switch (cmd) {
  788. case TCSBRK: /* SVID version: non-zero arg --> no break */
  789. retval = tty_check_change(tty);
  790. if (retval)
  791. return retval;
  792. tty_wait_until_sent(tty, 0);
  793. if (!arg)
  794. send_break(info, 250); /* 1/4 second */
  795. return 0;
  796. case TCSBRKP: /* support for POSIX tcsendbreak() */
  797. retval = tty_check_change(tty);
  798. if (retval)
  799. return retval;
  800. tty_wait_until_sent(tty, 0);
  801. send_break(info, arg ? arg*(100) : 250);
  802. return 0;
  803. case TIOCGSERIAL:
  804. return get_serial_info(info,
  805. (struct serial_struct *) arg);
  806. case TIOCSSERIAL:
  807. return set_serial_info(info, tty,
  808. (struct serial_struct *) arg);
  809. case TIOCSERGETLSR: /* Get line status register */
  810. return get_lsr_info(info, (unsigned int *) arg);
  811. case TIOCSERGSTRUCT:
  812. if (copy_to_user((struct m68k_serial *) arg,
  813. info, sizeof(struct m68k_serial)))
  814. return -EFAULT;
  815. return 0;
  816. default:
  817. return -ENOIOCTLCMD;
  818. }
  819. return 0;
  820. }
  821. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  822. {
  823. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  824. change_speed(info, tty);
  825. if ((old_termios->c_cflag & CRTSCTS) &&
  826. !(tty->termios.c_cflag & CRTSCTS))
  827. rs_start(tty);
  828. }
  829. /*
  830. * ------------------------------------------------------------
  831. * rs_close()
  832. *
  833. * This routine is called when the serial port gets closed. First, we
  834. * wait for the last remaining data to be sent. Then, we unlink its
  835. * S structure from the interrupt chain if necessary, and we free
  836. * that IRQ if nothing is left in the chain.
  837. * ------------------------------------------------------------
  838. */
  839. static void rs_close(struct tty_struct *tty, struct file * filp)
  840. {
  841. struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
  842. struct tty_port *port = &info->tport;
  843. m68328_uart *uart = &uart_addr[info->line];
  844. unsigned long flags;
  845. if (serial_paranoia_check(info, tty->name, "rs_close"))
  846. return;
  847. local_irq_save(flags);
  848. if (tty_hung_up_p(filp)) {
  849. local_irq_restore(flags);
  850. return;
  851. }
  852. if ((tty->count == 1) && (port->count != 1)) {
  853. /*
  854. * Uh, oh. tty->count is 1, which means that the tty
  855. * structure will be freed. Info->count should always
  856. * be one in these conditions. If it's greater than
  857. * one, we've got real problems, since it means the
  858. * serial port won't be shutdown.
  859. */
  860. printk("rs_close: bad serial port count; tty->count is 1, "
  861. "port->count is %d\n", port->count);
  862. port->count = 1;
  863. }
  864. if (--port->count < 0) {
  865. printk("rs_close: bad serial port count for ttyS%d: %d\n",
  866. info->line, port->count);
  867. port->count = 0;
  868. }
  869. if (port->count) {
  870. local_irq_restore(flags);
  871. return;
  872. }
  873. port->flags |= ASYNC_CLOSING;
  874. /*
  875. * Now we wait for the transmit buffer to clear; and we notify
  876. * the line discipline to only process XON/XOFF characters.
  877. */
  878. tty->closing = 1;
  879. if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  880. tty_wait_until_sent(tty, port->closing_wait);
  881. /*
  882. * At this point we stop accepting input. To do this, we
  883. * disable the receive line status interrupts, and tell the
  884. * interrupt driver to stop checking the data ready bit in the
  885. * line status register.
  886. */
  887. uart->ustcnt &= ~USTCNT_RXEN;
  888. uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
  889. shutdown(info, tty);
  890. rs_flush_buffer(tty);
  891. tty_ldisc_flush(tty);
  892. tty->closing = 0;
  893. tty_port_tty_set(&info->tport, NULL);
  894. #warning "This is not and has never been valid so fix it"
  895. #if 0
  896. if (tty->ldisc.num != ldiscs[N_TTY].num) {
  897. if (tty->ldisc.close)
  898. (tty->ldisc.close)(tty);
  899. tty->ldisc = ldiscs[N_TTY];
  900. tty->termios.c_line = N_TTY;
  901. if (tty->ldisc.open)
  902. (tty->ldisc.open)(tty);
  903. }
  904. #endif
  905. if (port->blocked_open) {
  906. if (port->close_delay)
  907. msleep_interruptible(jiffies_to_msecs(port->close_delay));
  908. wake_up_interruptible(&port->open_wait);
  909. }
  910. port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  911. wake_up_interruptible(&port->close_wait);
  912. local_irq_restore(flags);
  913. }
  914. /*
  915. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  916. */
  917. void rs_hangup(struct tty_struct *tty)
  918. {
  919. struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
  920. if (serial_paranoia_check(info, tty->name, "rs_hangup"))
  921. return;
  922. rs_flush_buffer(tty);
  923. shutdown(info, tty);
  924. info->tport.count = 0;
  925. info->tport.flags &= ~ASYNC_NORMAL_ACTIVE;
  926. tty_port_tty_set(&info->tport, NULL);
  927. wake_up_interruptible(&info->tport.open_wait);
  928. }
  929. /*
  930. * This routine is called whenever a serial port is opened. It
  931. * enables interrupts for a serial port, linking in its S structure into
  932. * the IRQ chain. It also performs the serial-specific
  933. * initialization for the tty structure.
  934. */
  935. int rs_open(struct tty_struct *tty, struct file * filp)
  936. {
  937. struct m68k_serial *info;
  938. int retval;
  939. info = &m68k_soft[tty->index];
  940. if (serial_paranoia_check(info, tty->name, "rs_open"))
  941. return -ENODEV;
  942. info->tport.count++;
  943. tty->driver_data = info;
  944. tty_port_tty_set(&info->tport, tty);
  945. /*
  946. * Start up serial port
  947. */
  948. retval = startup(info, tty);
  949. if (retval)
  950. return retval;
  951. return tty_port_block_til_ready(&info->tport, tty, filp);
  952. }
  953. /* Finally, routines used to initialize the serial driver. */
  954. static void show_serial_version(void)
  955. {
  956. printk("MC68328 serial driver version 1.00\n");
  957. }
  958. static const struct tty_operations rs_ops = {
  959. .open = rs_open,
  960. .close = rs_close,
  961. .write = rs_write,
  962. .flush_chars = rs_flush_chars,
  963. .write_room = rs_write_room,
  964. .chars_in_buffer = rs_chars_in_buffer,
  965. .flush_buffer = rs_flush_buffer,
  966. .ioctl = rs_ioctl,
  967. .throttle = rs_throttle,
  968. .unthrottle = rs_unthrottle,
  969. .set_termios = rs_set_termios,
  970. .stop = rs_stop,
  971. .start = rs_start,
  972. .hangup = rs_hangup,
  973. .set_ldisc = rs_set_ldisc,
  974. };
  975. static const struct tty_port_operations rs_port_ops = {
  976. };
  977. /* rs_init inits the driver */
  978. static int __init
  979. rs68328_init(void)
  980. {
  981. unsigned long flags;
  982. int i;
  983. struct m68k_serial *info;
  984. serial_driver = alloc_tty_driver(NR_PORTS);
  985. if (!serial_driver)
  986. return -ENOMEM;
  987. show_serial_version();
  988. /* Initialize the tty_driver structure */
  989. /* SPARC: Not all of this is exactly right for us. */
  990. serial_driver->name = "ttyS";
  991. serial_driver->major = TTY_MAJOR;
  992. serial_driver->minor_start = 64;
  993. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  994. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  995. serial_driver->init_termios = tty_std_termios;
  996. serial_driver->init_termios.c_cflag =
  997. m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
  998. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  999. tty_set_operations(serial_driver, &rs_ops);
  1000. local_irq_save(flags);
  1001. for(i=0;i<NR_PORTS;i++) {
  1002. info = &m68k_soft[i];
  1003. tty_port_init(&info->tport);
  1004. info->tport.ops = &rs_port_ops;
  1005. info->magic = SERIAL_MAGIC;
  1006. info->port = (int) &uart_addr[i];
  1007. info->irq = uart_irqs[i];
  1008. info->custom_divisor = 16;
  1009. info->x_char = 0;
  1010. info->line = i;
  1011. info->is_cons = 1; /* Means shortcuts work */
  1012. printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
  1013. info->port, info->irq);
  1014. printk(" is a builtin MC68328 UART\n");
  1015. #ifdef CONFIG_M68VZ328
  1016. if (i > 0 )
  1017. PJSEL &= 0xCF; /* PSW enable second port output */
  1018. #endif
  1019. if (request_irq(uart_irqs[i],
  1020. rs_interrupt,
  1021. 0,
  1022. "M68328_UART", info))
  1023. panic("Unable to attach 68328 serial interrupt\n");
  1024. tty_port_link_device(&info->tport, serial_driver, i);
  1025. }
  1026. local_irq_restore(flags);
  1027. if (tty_register_driver(serial_driver)) {
  1028. put_tty_driver(serial_driver);
  1029. for (i = 0; i < NR_PORTS; i++)
  1030. tty_port_destroy(&m68k_soft[i].tport);
  1031. printk(KERN_ERR "Couldn't register serial driver\n");
  1032. return -ENOMEM;
  1033. }
  1034. return 0;
  1035. }
  1036. module_init(rs68328_init);
  1037. static void m68328_set_baud(void)
  1038. {
  1039. unsigned short ustcnt;
  1040. int i;
  1041. ustcnt = USTCNT;
  1042. USTCNT = ustcnt & ~USTCNT_TXEN;
  1043. again:
  1044. for (i = 0; i < ARRAY_SIZE(baud_table); i++)
  1045. if (baud_table[i] == m68328_console_baud)
  1046. break;
  1047. if (i >= ARRAY_SIZE(baud_table)) {
  1048. m68328_console_baud = 9600;
  1049. goto again;
  1050. }
  1051. UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
  1052. PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
  1053. ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
  1054. ustcnt |= USTCNT_8_7;
  1055. ustcnt |= USTCNT_TXEN;
  1056. USTCNT = ustcnt;
  1057. m68328_console_initted = 1;
  1058. return;
  1059. }
  1060. int m68328_console_setup(struct console *cp, char *arg)
  1061. {
  1062. int i, n = CONSOLE_BAUD_RATE;
  1063. if (!cp)
  1064. return(-1);
  1065. if (arg)
  1066. n = simple_strtoul(arg,NULL,0);
  1067. for (i = 0; i < ARRAY_SIZE(baud_table); i++)
  1068. if (baud_table[i] == n)
  1069. break;
  1070. if (i < ARRAY_SIZE(baud_table)) {
  1071. m68328_console_baud = n;
  1072. m68328_console_cbaud = 0;
  1073. if (i > 15) {
  1074. m68328_console_cbaud |= CBAUDEX;
  1075. i -= 15;
  1076. }
  1077. m68328_console_cbaud |= i;
  1078. }
  1079. m68328_set_baud(); /* make sure baud rate changes */
  1080. return(0);
  1081. }
  1082. static struct tty_driver *m68328_console_device(struct console *c, int *index)
  1083. {
  1084. *index = c->index;
  1085. return serial_driver;
  1086. }
  1087. void m68328_console_write (struct console *co, const char *str,
  1088. unsigned int count)
  1089. {
  1090. if (!m68328_console_initted)
  1091. m68328_set_baud();
  1092. while (count--) {
  1093. if (*str == '\n')
  1094. rs_put_char('\r');
  1095. rs_put_char( *str++ );
  1096. }
  1097. }
  1098. static struct console m68328_driver = {
  1099. .name = "ttyS",
  1100. .write = m68328_console_write,
  1101. .device = m68328_console_device,
  1102. .setup = m68328_console_setup,
  1103. .flags = CON_PRINTBUFFER,
  1104. .index = -1,
  1105. };
  1106. static int __init m68328_console_init(void)
  1107. {
  1108. register_console(&m68328_driver);
  1109. return 0;
  1110. }
  1111. console_initcall(m68328_console_init);