68328serial.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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 <linux/module.h>
  17. #include <linux/errno.h>
  18. #include <linux/serial.h>
  19. #include <linux/signal.h>
  20. #include <linux/sched.h>
  21. #include <linux/timer.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/tty.h>
  24. #include <linux/tty_flip.h>
  25. #include <linux/major.h>
  26. #include <linux/string.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/mm.h>
  29. #include <linux/kernel.h>
  30. #include <linux/console.h>
  31. #include <linux/reboot.h>
  32. #include <linux/keyboard.h>
  33. #include <linux/init.h>
  34. #include <linux/pm.h>
  35. #include <linux/bitops.h>
  36. #include <linux/delay.h>
  37. #include <linux/gfp.h>
  38. #include <asm/io.h>
  39. #include <asm/irq.h>
  40. #include <asm/delay.h>
  41. #include <asm/uaccess.h>
  42. /* (es) */
  43. /* note: perhaps we can murge these files, so that you can just
  44. * define 1 of them, and they can sort that out for themselves
  45. */
  46. #if defined(CONFIG_M68EZ328)
  47. #include <asm/MC68EZ328.h>
  48. #else
  49. #if defined(CONFIG_M68VZ328)
  50. #include <asm/MC68VZ328.h>
  51. #else
  52. #include <asm/MC68328.h>
  53. #endif /* CONFIG_M68VZ328 */
  54. #endif /* CONFIG_M68EZ328 */
  55. /* Turn off usage of real serial interrupt code, to "support" Copilot */
  56. #ifdef CONFIG_XCOPILOT_BUGS
  57. #undef USE_INTS
  58. #else
  59. #define USE_INTS
  60. #endif
  61. /*
  62. * I believe this is the optimal setting that reduces the number of interrupts.
  63. * At high speeds the output might become a little "bursted" (use USTCNT_TXHE
  64. * if that bothers you), but in most cases it will not, since we try to
  65. * transmit characters every time rs_interrupt is called. Thus, quite often
  66. * you'll see that a receive interrupt occures before the transmit one.
  67. * -- Vladimir Gurevich
  68. */
  69. #define USTCNT_TX_INTR_MASK (USTCNT_TXEE)
  70. /*
  71. * 68328 and 68EZ328 UARTS are a little bit different. EZ328 has special
  72. * "Old data interrupt" which occures whenever the data stay in the FIFO
  73. * longer than 30 bits time. This allows us to use FIFO without compromising
  74. * latency. '328 does not have this feature and without the real 328-based
  75. * board I would assume that RXRE is the safest setting.
  76. *
  77. * For EZ328 I use RXHE (Half empty) interrupt to reduce the number of
  78. * interrupts. RXFE (receive queue full) causes the system to lose data
  79. * at least at 115200 baud
  80. *
  81. * If your board is busy doing other stuff, you might consider to use
  82. * RXRE (data ready intrrupt) instead.
  83. *
  84. * The other option is to make these INTR masks run-time configurable, so
  85. * that people can dynamically adapt them according to the current usage.
  86. * -- Vladimir Gurevich
  87. */
  88. /* (es) */
  89. #if defined(CONFIG_M68EZ328) || defined(CONFIG_M68VZ328)
  90. #define USTCNT_RX_INTR_MASK (USTCNT_RXHE | USTCNT_ODEN)
  91. #elif defined(CONFIG_M68328)
  92. #define USTCNT_RX_INTR_MASK (USTCNT_RXRE)
  93. #else
  94. #error Please, define the Rx interrupt events for your CPU
  95. #endif
  96. /* (/es) */
  97. /*
  98. * This is our internal structure for each serial port's state.
  99. */
  100. struct m68k_serial {
  101. struct tty_port tport;
  102. char is_cons; /* Is this our console. */
  103. int magic;
  104. int baud_base;
  105. int port;
  106. int irq;
  107. int type; /* UART type */
  108. int custom_divisor;
  109. int x_char; /* xon/xoff character */
  110. int line;
  111. unsigned char *xmit_buf;
  112. int xmit_head;
  113. int xmit_tail;
  114. int xmit_cnt;
  115. };
  116. #define SERIAL_MAGIC 0x5301
  117. /*
  118. * Define the number of ports supported and their irqs.
  119. */
  120. #define NR_PORTS 1
  121. static struct m68k_serial m68k_soft[NR_PORTS];
  122. static unsigned int uart_irqs[NR_PORTS] = { UART_IRQ_NUM };
  123. /* multiple ports are contiguous in memory */
  124. m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
  125. struct tty_driver *serial_driver;
  126. static void change_speed(struct m68k_serial *info, struct tty_struct *tty);
  127. /*
  128. * Setup for console. Argument comes from the boot command line.
  129. */
  130. /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
  131. #ifdef CONFIG_M68VZ328
  132. #define CONSOLE_BAUD_RATE 19200
  133. #define DEFAULT_CBAUD B19200
  134. #endif
  135. #ifndef CONSOLE_BAUD_RATE
  136. #define CONSOLE_BAUD_RATE 9600
  137. #define DEFAULT_CBAUD B9600
  138. #endif
  139. static int m68328_console_initted = 0;
  140. static int m68328_console_baud = CONSOLE_BAUD_RATE;
  141. static int m68328_console_cbaud = DEFAULT_CBAUD;
  142. static inline int serial_paranoia_check(struct m68k_serial *info,
  143. char *name, const char *routine)
  144. {
  145. #ifdef SERIAL_PARANOIA_CHECK
  146. static const char *badmagic =
  147. "Warning: bad magic number for serial struct %s in %s\n";
  148. static const char *badinfo =
  149. "Warning: null m68k_serial for %s in %s\n";
  150. if (!info) {
  151. printk(badinfo, name, routine);
  152. return 1;
  153. }
  154. if (info->magic != SERIAL_MAGIC) {
  155. printk(badmagic, name, routine);
  156. return 1;
  157. }
  158. #endif
  159. return 0;
  160. }
  161. /*
  162. * This is used to figure out the divisor speeds and the timeouts
  163. */
  164. static int baud_table[] = {
  165. 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
  166. 9600, 19200, 38400, 57600, 115200, 0 };
  167. /* Utility routines */
  168. static inline int get_baud(struct m68k_serial *ss)
  169. {
  170. unsigned long result = 115200;
  171. unsigned short int baud = uart_addr[ss->line].ubaud;
  172. if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
  173. result >>= GET_FIELD(baud, UBAUD_DIVIDE);
  174. return result;
  175. }
  176. /*
  177. * ------------------------------------------------------------
  178. * rs_stop() and rs_start()
  179. *
  180. * This routines are called before setting or resetting tty->stopped.
  181. * They enable or disable transmitter interrupts, as necessary.
  182. * ------------------------------------------------------------
  183. */
  184. static void rs_stop(struct tty_struct *tty)
  185. {
  186. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  187. m68328_uart *uart = &uart_addr[info->line];
  188. unsigned long flags;
  189. if (serial_paranoia_check(info, tty->name, "rs_stop"))
  190. return;
  191. local_irq_save(flags);
  192. uart->ustcnt &= ~USTCNT_TXEN;
  193. local_irq_restore(flags);
  194. }
  195. static int rs_put_char(char ch)
  196. {
  197. unsigned long flags;
  198. int loops = 0;
  199. local_irq_save(flags);
  200. while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
  201. loops++;
  202. udelay(5);
  203. }
  204. UTX_TXDATA = ch;
  205. udelay(5);
  206. local_irq_restore(flags);
  207. return 1;
  208. }
  209. static void rs_start(struct tty_struct *tty)
  210. {
  211. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  212. m68328_uart *uart = &uart_addr[info->line];
  213. unsigned long flags;
  214. if (serial_paranoia_check(info, tty->name, "rs_start"))
  215. return;
  216. local_irq_save(flags);
  217. if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
  218. #ifdef USE_INTS
  219. uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
  220. #else
  221. uart->ustcnt |= USTCNT_TXEN;
  222. #endif
  223. }
  224. local_irq_restore(flags);
  225. }
  226. static void receive_chars(struct m68k_serial *info, unsigned short rx)
  227. {
  228. m68328_uart *uart = &uart_addr[info->line];
  229. unsigned char ch, flag;
  230. /*
  231. * This do { } while() loop will get ALL chars out of Rx FIFO
  232. */
  233. #ifndef CONFIG_XCOPILOT_BUGS
  234. do {
  235. #endif
  236. ch = GET_FIELD(rx, URX_RXDATA);
  237. if(info->is_cons) {
  238. if(URX_BREAK & rx) { /* whee, break received */
  239. return;
  240. #ifdef CONFIG_MAGIC_SYSRQ
  241. } else if (ch == 0x10) { /* ^P */
  242. show_state();
  243. show_free_areas(0);
  244. show_buffers();
  245. /* show_net_buffers(); */
  246. return;
  247. } else if (ch == 0x12) { /* ^R */
  248. emergency_restart();
  249. return;
  250. #endif /* CONFIG_MAGIC_SYSRQ */
  251. }
  252. }
  253. flag = TTY_NORMAL;
  254. if (rx & URX_PARITY_ERROR)
  255. flag = TTY_PARITY;
  256. else if (rx & URX_OVRUN)
  257. flag = TTY_OVERRUN;
  258. else if (rx & URX_FRAME_ERROR)
  259. flag = TTY_FRAME;
  260. tty_insert_flip_char(&info->tport, ch, flag);
  261. #ifndef CONFIG_XCOPILOT_BUGS
  262. } while((rx = uart->urx.w) & URX_DATA_READY);
  263. #endif
  264. tty_schedule_flip(&info->tport);
  265. }
  266. static void transmit_chars(struct m68k_serial *info, struct tty_struct *tty)
  267. {
  268. m68328_uart *uart = &uart_addr[info->line];
  269. if (info->x_char) {
  270. /* Send next char */
  271. uart->utx.b.txdata = info->x_char;
  272. info->x_char = 0;
  273. goto clear_and_return;
  274. }
  275. if ((info->xmit_cnt <= 0) || !tty || tty->stopped) {
  276. /* That's peculiar... TX ints off */
  277. uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
  278. goto clear_and_return;
  279. }
  280. /* Send char */
  281. uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
  282. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  283. info->xmit_cnt--;
  284. if(info->xmit_cnt <= 0) {
  285. /* All done for now... TX ints off */
  286. uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
  287. goto clear_and_return;
  288. }
  289. clear_and_return:
  290. /* Clear interrupt (should be auto)*/
  291. return;
  292. }
  293. /*
  294. * This is the serial driver's generic interrupt routine
  295. */
  296. irqreturn_t rs_interrupt(int irq, void *dev_id)
  297. {
  298. struct m68k_serial *info = dev_id;
  299. struct tty_struct *tty = tty_port_tty_get(&info->tport);
  300. m68328_uart *uart;
  301. unsigned short rx;
  302. unsigned short tx;
  303. uart = &uart_addr[info->line];
  304. rx = uart->urx.w;
  305. #ifdef USE_INTS
  306. tx = uart->utx.w;
  307. if (rx & URX_DATA_READY)
  308. receive_chars(info, rx);
  309. if (tx & UTX_TX_AVAIL)
  310. transmit_chars(info, tty);
  311. #else
  312. receive_chars(info, rx);
  313. #endif
  314. tty_kref_put(tty);
  315. return IRQ_HANDLED;
  316. }
  317. static int startup(struct m68k_serial *info, struct tty_struct *tty)
  318. {
  319. m68328_uart *uart = &uart_addr[info->line];
  320. unsigned long flags;
  321. if (info->tport.flags & ASYNC_INITIALIZED)
  322. return 0;
  323. if (!info->xmit_buf) {
  324. info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
  325. if (!info->xmit_buf)
  326. return -ENOMEM;
  327. }
  328. local_irq_save(flags);
  329. /*
  330. * Clear the FIFO buffers and disable them
  331. * (they will be reenabled in change_speed())
  332. */
  333. uart->ustcnt = USTCNT_UEN;
  334. uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
  335. (void)uart->urx.w;
  336. /*
  337. * Finally, enable sequencing and interrupts
  338. */
  339. #ifdef USE_INTS
  340. uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
  341. USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
  342. #else
  343. uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
  344. #endif
  345. if (tty)
  346. clear_bit(TTY_IO_ERROR, &tty->flags);
  347. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  348. /*
  349. * and set the speed of the serial port
  350. */
  351. change_speed(info, tty);
  352. info->tport.flags |= ASYNC_INITIALIZED;
  353. local_irq_restore(flags);
  354. return 0;
  355. }
  356. /*
  357. * This routine will shutdown a serial port; interrupts are disabled, and
  358. * DTR is dropped if the hangup on close termio flag is on.
  359. */
  360. static void shutdown(struct m68k_serial *info, struct tty_struct *tty)
  361. {
  362. m68328_uart *uart = &uart_addr[info->line];
  363. unsigned long flags;
  364. uart->ustcnt = 0; /* All off! */
  365. if (!(info->tport.flags & ASYNC_INITIALIZED))
  366. return;
  367. local_irq_save(flags);
  368. if (info->xmit_buf) {
  369. free_page((unsigned long) info->xmit_buf);
  370. info->xmit_buf = 0;
  371. }
  372. if (tty)
  373. set_bit(TTY_IO_ERROR, &tty->flags);
  374. info->tport.flags &= ~ASYNC_INITIALIZED;
  375. local_irq_restore(flags);
  376. }
  377. struct {
  378. int divisor, prescale;
  379. }
  380. #ifndef CONFIG_M68VZ328
  381. hw_baud_table[18] = {
  382. {0,0}, /* 0 */
  383. {0,0}, /* 50 */
  384. {0,0}, /* 75 */
  385. {0,0}, /* 110 */
  386. {0,0}, /* 134 */
  387. {0,0}, /* 150 */
  388. {0,0}, /* 200 */
  389. {7,0x26}, /* 300 */
  390. {6,0x26}, /* 600 */
  391. {5,0x26}, /* 1200 */
  392. {0,0}, /* 1800 */
  393. {4,0x26}, /* 2400 */
  394. {3,0x26}, /* 4800 */
  395. {2,0x26}, /* 9600 */
  396. {1,0x26}, /* 19200 */
  397. {0,0x26}, /* 38400 */
  398. {1,0x38}, /* 57600 */
  399. {0,0x38}, /* 115200 */
  400. };
  401. #else
  402. hw_baud_table[18] = {
  403. {0,0}, /* 0 */
  404. {0,0}, /* 50 */
  405. {0,0}, /* 75 */
  406. {0,0}, /* 110 */
  407. {0,0}, /* 134 */
  408. {0,0}, /* 150 */
  409. {0,0}, /* 200 */
  410. {0,0}, /* 300 */
  411. {7,0x26}, /* 600 */
  412. {6,0x26}, /* 1200 */
  413. {0,0}, /* 1800 */
  414. {5,0x26}, /* 2400 */
  415. {4,0x26}, /* 4800 */
  416. {3,0x26}, /* 9600 */
  417. {2,0x26}, /* 19200 */
  418. {1,0x26}, /* 38400 */
  419. {0,0x26}, /* 57600 */
  420. {1,0x38}, /* 115200 */
  421. };
  422. #endif
  423. /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
  424. /*
  425. * This routine is called to set the UART divisor registers to match
  426. * the specified baud rate for a serial port.
  427. */
  428. static void change_speed(struct m68k_serial *info, struct tty_struct *tty)
  429. {
  430. m68328_uart *uart = &uart_addr[info->line];
  431. unsigned short port;
  432. unsigned short ustcnt;
  433. unsigned cflag;
  434. int i;
  435. cflag = tty->termios.c_cflag;
  436. port = info->port;
  437. if (!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 == NULL) return;
  477. if (info->xmit_buf == NULL) 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. local_irq_restore(flags);
  912. }
  913. /*
  914. * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
  915. */
  916. void rs_hangup(struct tty_struct *tty)
  917. {
  918. struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
  919. if (serial_paranoia_check(info, tty->name, "rs_hangup"))
  920. return;
  921. rs_flush_buffer(tty);
  922. shutdown(info, tty);
  923. info->tport.count = 0;
  924. info->tport.flags &= ~ASYNC_NORMAL_ACTIVE;
  925. tty_port_tty_set(&info->tport, NULL);
  926. wake_up_interruptible(&info->tport.open_wait);
  927. }
  928. /*
  929. * This routine is called whenever a serial port is opened. It
  930. * enables interrupts for a serial port, linking in its S structure into
  931. * the IRQ chain. It also performs the serial-specific
  932. * initialization for the tty structure.
  933. */
  934. int rs_open(struct tty_struct *tty, struct file * filp)
  935. {
  936. struct m68k_serial *info;
  937. int retval;
  938. info = &m68k_soft[tty->index];
  939. if (serial_paranoia_check(info, tty->name, "rs_open"))
  940. return -ENODEV;
  941. info->tport.count++;
  942. tty->driver_data = info;
  943. tty_port_tty_set(&info->tport, tty);
  944. /*
  945. * Start up serial port
  946. */
  947. retval = startup(info, tty);
  948. if (retval)
  949. return retval;
  950. return tty_port_block_til_ready(&info->tport, tty, filp);
  951. }
  952. /* Finally, routines used to initialize the serial driver. */
  953. static void show_serial_version(void)
  954. {
  955. printk("MC68328 serial driver version 1.00\n");
  956. }
  957. static const struct tty_operations rs_ops = {
  958. .open = rs_open,
  959. .close = rs_close,
  960. .write = rs_write,
  961. .flush_chars = rs_flush_chars,
  962. .write_room = rs_write_room,
  963. .chars_in_buffer = rs_chars_in_buffer,
  964. .flush_buffer = rs_flush_buffer,
  965. .ioctl = rs_ioctl,
  966. .throttle = rs_throttle,
  967. .unthrottle = rs_unthrottle,
  968. .set_termios = rs_set_termios,
  969. .stop = rs_stop,
  970. .start = rs_start,
  971. .hangup = rs_hangup,
  972. .set_ldisc = rs_set_ldisc,
  973. };
  974. static const struct tty_port_operations rs_port_ops = {
  975. };
  976. /* rs_init inits the driver */
  977. static int __init
  978. rs68328_init(void)
  979. {
  980. unsigned long flags;
  981. int i;
  982. struct m68k_serial *info;
  983. serial_driver = alloc_tty_driver(NR_PORTS);
  984. if (!serial_driver)
  985. return -ENOMEM;
  986. show_serial_version();
  987. /* Initialize the tty_driver structure */
  988. /* SPARC: Not all of this is exactly right for us. */
  989. serial_driver->name = "ttyS";
  990. serial_driver->major = TTY_MAJOR;
  991. serial_driver->minor_start = 64;
  992. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  993. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  994. serial_driver->init_termios = tty_std_termios;
  995. serial_driver->init_termios.c_cflag =
  996. m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
  997. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  998. tty_set_operations(serial_driver, &rs_ops);
  999. local_irq_save(flags);
  1000. for(i=0;i<NR_PORTS;i++) {
  1001. info = &m68k_soft[i];
  1002. tty_port_init(&info->tport);
  1003. info->tport.ops = &rs_port_ops;
  1004. info->magic = SERIAL_MAGIC;
  1005. info->port = (int) &uart_addr[i];
  1006. info->irq = uart_irqs[i];
  1007. info->custom_divisor = 16;
  1008. info->x_char = 0;
  1009. info->line = i;
  1010. info->is_cons = 1; /* Means shortcuts work */
  1011. printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
  1012. info->port, info->irq);
  1013. printk(" is a builtin MC68328 UART\n");
  1014. #ifdef CONFIG_M68VZ328
  1015. if (i > 0 )
  1016. PJSEL &= 0xCF; /* PSW enable second port output */
  1017. #endif
  1018. if (request_irq(uart_irqs[i],
  1019. rs_interrupt,
  1020. 0,
  1021. "M68328_UART", info))
  1022. panic("Unable to attach 68328 serial interrupt\n");
  1023. tty_port_link_device(&info->tport, serial_driver, i);
  1024. }
  1025. local_irq_restore(flags);
  1026. if (tty_register_driver(serial_driver)) {
  1027. put_tty_driver(serial_driver);
  1028. for (i = 0; i < NR_PORTS; i++)
  1029. tty_port_destroy(&m68k_soft[i].tport);
  1030. printk(KERN_ERR "Couldn't register serial driver\n");
  1031. return -ENOMEM;
  1032. }
  1033. return 0;
  1034. }
  1035. module_init(rs68328_init);
  1036. static void m68328_set_baud(void)
  1037. {
  1038. unsigned short ustcnt;
  1039. int i;
  1040. ustcnt = USTCNT;
  1041. USTCNT = ustcnt & ~USTCNT_TXEN;
  1042. again:
  1043. for (i = 0; i < ARRAY_SIZE(baud_table); i++)
  1044. if (baud_table[i] == m68328_console_baud)
  1045. break;
  1046. if (i >= ARRAY_SIZE(baud_table)) {
  1047. m68328_console_baud = 9600;
  1048. goto again;
  1049. }
  1050. UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
  1051. PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
  1052. ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
  1053. ustcnt |= USTCNT_8_7;
  1054. ustcnt |= USTCNT_TXEN;
  1055. USTCNT = ustcnt;
  1056. m68328_console_initted = 1;
  1057. return;
  1058. }
  1059. int m68328_console_setup(struct console *cp, char *arg)
  1060. {
  1061. int i, n = CONSOLE_BAUD_RATE;
  1062. if (!cp)
  1063. return(-1);
  1064. if (arg)
  1065. n = simple_strtoul(arg,NULL,0);
  1066. for (i = 0; i < ARRAY_SIZE(baud_table); i++)
  1067. if (baud_table[i] == n)
  1068. break;
  1069. if (i < ARRAY_SIZE(baud_table)) {
  1070. m68328_console_baud = n;
  1071. m68328_console_cbaud = 0;
  1072. if (i > 15) {
  1073. m68328_console_cbaud |= CBAUDEX;
  1074. i -= 15;
  1075. }
  1076. m68328_console_cbaud |= i;
  1077. }
  1078. m68328_set_baud(); /* make sure baud rate changes */
  1079. return(0);
  1080. }
  1081. static struct tty_driver *m68328_console_device(struct console *c, int *index)
  1082. {
  1083. *index = c->index;
  1084. return serial_driver;
  1085. }
  1086. void m68328_console_write (struct console *co, const char *str,
  1087. unsigned int count)
  1088. {
  1089. if (!m68328_console_initted)
  1090. m68328_set_baud();
  1091. while (count--) {
  1092. if (*str == '\n')
  1093. rs_put_char('\r');
  1094. rs_put_char( *str++ );
  1095. }
  1096. }
  1097. static struct console m68328_driver = {
  1098. .name = "ttyS",
  1099. .write = m68328_console_write,
  1100. .device = m68328_console_device,
  1101. .setup = m68328_console_setup,
  1102. .flags = CON_PRINTBUFFER,
  1103. .index = -1,
  1104. };
  1105. static int __init m68328_console_init(void)
  1106. {
  1107. register_console(&m68328_driver);
  1108. return 0;
  1109. }
  1110. console_initcall(m68328_console_init);