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. if (!(port = info->port))
  437. return;
  438. ustcnt = uart->ustcnt;
  439. uart->ustcnt = ustcnt & ~USTCNT_TXEN;
  440. i = cflag & CBAUD;
  441. if (i & CBAUDEX) {
  442. i = (i & ~CBAUDEX) + B38400;
  443. }
  444. uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
  445. PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
  446. ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
  447. if ((cflag & CSIZE) == CS8)
  448. ustcnt |= USTCNT_8_7;
  449. if (cflag & CSTOPB)
  450. ustcnt |= USTCNT_STOP;
  451. if (cflag & PARENB)
  452. ustcnt |= USTCNT_PARITYEN;
  453. if (cflag & PARODD)
  454. ustcnt |= USTCNT_ODD_EVEN;
  455. #ifdef CONFIG_SERIAL_68328_RTS_CTS
  456. if (cflag & CRTSCTS) {
  457. uart->utx.w &= ~ UTX_NOCTS;
  458. } else {
  459. uart->utx.w |= UTX_NOCTS;
  460. }
  461. #endif
  462. ustcnt |= USTCNT_TXEN;
  463. uart->ustcnt = ustcnt;
  464. return;
  465. }
  466. /*
  467. * Fair output driver allows a process to speak.
  468. */
  469. static void rs_fair_output(void)
  470. {
  471. int left; /* Output no more than that */
  472. unsigned long flags;
  473. struct m68k_serial *info = &m68k_soft[0];
  474. char c;
  475. if (info == 0) return;
  476. if (info->xmit_buf == 0) return;
  477. local_irq_save(flags);
  478. left = info->xmit_cnt;
  479. while (left != 0) {
  480. c = info->xmit_buf[info->xmit_tail];
  481. info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
  482. info->xmit_cnt--;
  483. local_irq_restore(flags);
  484. rs_put_char(c);
  485. local_irq_save(flags);
  486. left = min(info->xmit_cnt, left-1);
  487. }
  488. /* Last character is being transmitted now (hopefully). */
  489. udelay(5);
  490. local_irq_restore(flags);
  491. return;
  492. }
  493. /*
  494. * m68k_console_print is registered for printk.
  495. */
  496. void console_print_68328(const char *p)
  497. {
  498. char c;
  499. while((c=*(p++)) != 0) {
  500. if(c == '\n')
  501. rs_put_char('\r');
  502. rs_put_char(c);
  503. }
  504. /* Comment this if you want to have a strict interrupt-driven output */
  505. rs_fair_output();
  506. return;
  507. }
  508. static void rs_set_ldisc(struct tty_struct *tty)
  509. {
  510. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  511. if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
  512. return;
  513. info->is_cons = (tty->termios.c_line == N_TTY);
  514. printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
  515. }
  516. static void rs_flush_chars(struct tty_struct *tty)
  517. {
  518. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  519. m68328_uart *uart = &uart_addr[info->line];
  520. unsigned long flags;
  521. if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
  522. return;
  523. #ifndef USE_INTS
  524. for(;;) {
  525. #endif
  526. /* Enable transmitter */
  527. local_irq_save(flags);
  528. if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf) {
  529. local_irq_restore(flags);
  530. return;
  531. }
  532. #ifdef USE_INTS
  533. uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
  534. #else
  535. uart->ustcnt |= USTCNT_TXEN;
  536. #endif
  537. #ifdef USE_INTS
  538. if (uart->utx.w & UTX_TX_AVAIL) {
  539. #else
  540. if (1) {
  541. #endif
  542. /* Send char */
  543. uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
  544. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  545. info->xmit_cnt--;
  546. }
  547. #ifndef USE_INTS
  548. while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
  549. }
  550. #endif
  551. local_irq_restore(flags);
  552. }
  553. extern void console_printn(const char * b, int count);
  554. static int rs_write(struct tty_struct * tty,
  555. const unsigned char *buf, int count)
  556. {
  557. int c, total = 0;
  558. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  559. m68328_uart *uart = &uart_addr[info->line];
  560. unsigned long flags;
  561. if (serial_paranoia_check(info, tty->name, "rs_write"))
  562. return 0;
  563. if (!tty || !info->xmit_buf)
  564. return 0;
  565. local_save_flags(flags);
  566. while (1) {
  567. local_irq_disable();
  568. c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  569. SERIAL_XMIT_SIZE - info->xmit_head));
  570. local_irq_restore(flags);
  571. if (c <= 0)
  572. break;
  573. memcpy(info->xmit_buf + info->xmit_head, buf, c);
  574. local_irq_disable();
  575. info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
  576. info->xmit_cnt += c;
  577. local_irq_restore(flags);
  578. buf += c;
  579. count -= c;
  580. total += c;
  581. }
  582. if (info->xmit_cnt && !tty->stopped) {
  583. /* Enable transmitter */
  584. local_irq_disable();
  585. #ifndef USE_INTS
  586. while(info->xmit_cnt) {
  587. #endif
  588. uart->ustcnt |= USTCNT_TXEN;
  589. #ifdef USE_INTS
  590. uart->ustcnt |= USTCNT_TX_INTR_MASK;
  591. #else
  592. while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
  593. #endif
  594. if (uart->utx.w & UTX_TX_AVAIL) {
  595. uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
  596. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  597. info->xmit_cnt--;
  598. }
  599. #ifndef USE_INTS
  600. }
  601. #endif
  602. local_irq_restore(flags);
  603. }
  604. return total;
  605. }
  606. static int rs_write_room(struct tty_struct *tty)
  607. {
  608. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  609. int ret;
  610. if (serial_paranoia_check(info, tty->name, "rs_write_room"))
  611. return 0;
  612. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  613. if (ret < 0)
  614. ret = 0;
  615. return ret;
  616. }
  617. static int rs_chars_in_buffer(struct tty_struct *tty)
  618. {
  619. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  620. if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
  621. return 0;
  622. return info->xmit_cnt;
  623. }
  624. static void rs_flush_buffer(struct tty_struct *tty)
  625. {
  626. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  627. unsigned long flags;
  628. if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
  629. return;
  630. local_irq_save(flags);
  631. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  632. local_irq_restore(flags);
  633. tty_wakeup(tty);
  634. }
  635. /*
  636. * ------------------------------------------------------------
  637. * rs_throttle()
  638. *
  639. * This routine is called by the upper-layer tty layer to signal that
  640. * incoming characters should be throttled.
  641. * ------------------------------------------------------------
  642. */
  643. static void rs_throttle(struct tty_struct * tty)
  644. {
  645. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  646. if (serial_paranoia_check(info, tty->name, "rs_throttle"))
  647. return;
  648. if (I_IXOFF(tty))
  649. info->x_char = STOP_CHAR(tty);
  650. /* Turn off RTS line (do this atomic) */
  651. }
  652. static void rs_unthrottle(struct tty_struct * tty)
  653. {
  654. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  655. if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
  656. return;
  657. if (I_IXOFF(tty)) {
  658. if (info->x_char)
  659. info->x_char = 0;
  660. else
  661. info->x_char = START_CHAR(tty);
  662. }
  663. /* Assert RTS line (do this atomic) */
  664. }
  665. /*
  666. * ------------------------------------------------------------
  667. * rs_ioctl() and friends
  668. * ------------------------------------------------------------
  669. */
  670. static int get_serial_info(struct m68k_serial * info,
  671. struct serial_struct * retinfo)
  672. {
  673. struct serial_struct tmp;
  674. if (!retinfo)
  675. return -EFAULT;
  676. memset(&tmp, 0, sizeof(tmp));
  677. tmp.type = info->type;
  678. tmp.line = info->line;
  679. tmp.port = info->port;
  680. tmp.irq = info->irq;
  681. tmp.flags = info->tport.flags;
  682. tmp.baud_base = info->baud_base;
  683. tmp.close_delay = info->tport.close_delay;
  684. tmp.closing_wait = info->tport.closing_wait;
  685. tmp.custom_divisor = info->custom_divisor;
  686. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  687. return -EFAULT;
  688. return 0;
  689. }
  690. static int set_serial_info(struct m68k_serial *info, struct tty_struct *tty,
  691. struct serial_struct * new_info)
  692. {
  693. struct tty_port *port = &info->tport;
  694. struct serial_struct new_serial;
  695. struct m68k_serial old_info;
  696. int retval = 0;
  697. if (!new_info)
  698. return -EFAULT;
  699. if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  700. return -EFAULT;
  701. old_info = *info;
  702. if (!capable(CAP_SYS_ADMIN)) {
  703. if ((new_serial.baud_base != info->baud_base) ||
  704. (new_serial.type != info->type) ||
  705. (new_serial.close_delay != port->close_delay) ||
  706. ((new_serial.flags & ~ASYNC_USR_MASK) !=
  707. (port->flags & ~ASYNC_USR_MASK)))
  708. return -EPERM;
  709. port->flags = ((port->flags & ~ASYNC_USR_MASK) |
  710. (new_serial.flags & ASYNC_USR_MASK));
  711. info->custom_divisor = new_serial.custom_divisor;
  712. goto check_and_exit;
  713. }
  714. if (port->count > 1)
  715. return -EBUSY;
  716. /*
  717. * OK, past this point, all the error checking has been done.
  718. * At this point, we start making changes.....
  719. */
  720. info->baud_base = new_serial.baud_base;
  721. port->flags = ((port->flags & ~ASYNC_FLAGS) |
  722. (new_serial.flags & ASYNC_FLAGS));
  723. info->type = new_serial.type;
  724. port->close_delay = new_serial.close_delay;
  725. port->closing_wait = new_serial.closing_wait;
  726. check_and_exit:
  727. retval = startup(info, tty);
  728. return retval;
  729. }
  730. /*
  731. * get_lsr_info - get line status register info
  732. *
  733. * Purpose: Let user call ioctl() to get info when the UART physically
  734. * is emptied. On bus types like RS485, the transmitter must
  735. * release the bus after transmitting. This must be done when
  736. * the transmit shift register is empty, not be done when the
  737. * transmit holding register is empty. This functionality
  738. * allows an RS485 driver to be written in user space.
  739. */
  740. static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
  741. {
  742. #ifdef CONFIG_SERIAL_68328_RTS_CTS
  743. m68328_uart *uart = &uart_addr[info->line];
  744. #endif
  745. unsigned char status;
  746. unsigned long flags;
  747. local_irq_save(flags);
  748. #ifdef CONFIG_SERIAL_68328_RTS_CTS
  749. status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
  750. #else
  751. status = 0;
  752. #endif
  753. local_irq_restore(flags);
  754. return put_user(status, value);
  755. }
  756. /*
  757. * This routine sends a break character out the serial port.
  758. */
  759. static void send_break(struct m68k_serial * info, unsigned int duration)
  760. {
  761. m68328_uart *uart = &uart_addr[info->line];
  762. unsigned long flags;
  763. if (!info->port)
  764. return;
  765. local_irq_save(flags);
  766. #ifdef USE_INTS
  767. uart->utx.w |= UTX_SEND_BREAK;
  768. msleep_interruptible(duration);
  769. uart->utx.w &= ~UTX_SEND_BREAK;
  770. #endif
  771. local_irq_restore(flags);
  772. }
  773. static int rs_ioctl(struct tty_struct *tty,
  774. unsigned int cmd, unsigned long arg)
  775. {
  776. struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
  777. int retval;
  778. if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
  779. return -ENODEV;
  780. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  781. (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
  782. (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
  783. if (tty->flags & (1 << TTY_IO_ERROR))
  784. return -EIO;
  785. }
  786. switch (cmd) {
  787. case TCSBRK: /* SVID version: non-zero arg --> no break */
  788. retval = tty_check_change(tty);
  789. if (retval)
  790. return retval;
  791. tty_wait_until_sent(tty, 0);
  792. if (!arg)
  793. send_break(info, 250); /* 1/4 second */
  794. return 0;
  795. case TCSBRKP: /* support for POSIX tcsendbreak() */
  796. retval = tty_check_change(tty);
  797. if (retval)
  798. return retval;
  799. tty_wait_until_sent(tty, 0);
  800. send_break(info, arg ? arg*(100) : 250);
  801. return 0;
  802. case TIOCGSERIAL:
  803. return get_serial_info(info,
  804. (struct serial_struct *) arg);
  805. case TIOCSSERIAL:
  806. return set_serial_info(info, tty,
  807. (struct serial_struct *) arg);
  808. case TIOCSERGETLSR: /* Get line status register */
  809. return get_lsr_info(info, (unsigned int *) arg);
  810. case TIOCSERGSTRUCT:
  811. if (copy_to_user((struct m68k_serial *) arg,
  812. info, sizeof(struct m68k_serial)))
  813. return -EFAULT;
  814. return 0;
  815. default:
  816. return -ENOIOCTLCMD;
  817. }
  818. return 0;
  819. }
  820. static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  821. {
  822. struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
  823. change_speed(info, tty);
  824. if ((old_termios->c_cflag & CRTSCTS) &&
  825. !(tty->termios.c_cflag & CRTSCTS))
  826. rs_start(tty);
  827. }
  828. /*
  829. * ------------------------------------------------------------
  830. * rs_close()
  831. *
  832. * This routine is called when the serial port gets closed. First, we
  833. * wait for the last remaining data to be sent. Then, we unlink its
  834. * S structure from the interrupt chain if necessary, and we free
  835. * that IRQ if nothing is left in the chain.
  836. * ------------------------------------------------------------
  837. */
  838. static void rs_close(struct tty_struct *tty, struct file * filp)
  839. {
  840. struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
  841. struct tty_port *port = &info->tport;
  842. m68328_uart *uart = &uart_addr[info->line];
  843. unsigned long flags;
  844. if (serial_paranoia_check(info, tty->name, "rs_close"))
  845. return;
  846. local_irq_save(flags);
  847. if (tty_hung_up_p(filp)) {
  848. local_irq_restore(flags);
  849. return;
  850. }
  851. if ((tty->count == 1) && (port->count != 1)) {
  852. /*
  853. * Uh, oh. tty->count is 1, which means that the tty
  854. * structure will be freed. Info->count should always
  855. * be one in these conditions. If it's greater than
  856. * one, we've got real problems, since it means the
  857. * serial port won't be shutdown.
  858. */
  859. printk("rs_close: bad serial port count; tty->count is 1, "
  860. "port->count is %d\n", port->count);
  861. port->count = 1;
  862. }
  863. if (--port->count < 0) {
  864. printk("rs_close: bad serial port count for ttyS%d: %d\n",
  865. info->line, port->count);
  866. port->count = 0;
  867. }
  868. if (port->count) {
  869. local_irq_restore(flags);
  870. return;
  871. }
  872. port->flags |= ASYNC_CLOSING;
  873. /*
  874. * Now we wait for the transmit buffer to clear; and we notify
  875. * the line discipline to only process XON/XOFF characters.
  876. */
  877. tty->closing = 1;
  878. if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  879. tty_wait_until_sent(tty, port->closing_wait);
  880. /*
  881. * At this point we stop accepting input. To do this, we
  882. * disable the receive line status interrupts, and tell the
  883. * interrupt driver to stop checking the data ready bit in the
  884. * line status register.
  885. */
  886. uart->ustcnt &= ~USTCNT_RXEN;
  887. uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
  888. shutdown(info, tty);
  889. rs_flush_buffer(tty);
  890. tty_ldisc_flush(tty);
  891. tty->closing = 0;
  892. tty_port_tty_set(&info->tport, NULL);
  893. #warning "This is not and has never been valid so fix it"
  894. #if 0
  895. if (tty->ldisc.num != ldiscs[N_TTY].num) {
  896. if (tty->ldisc.close)
  897. (tty->ldisc.close)(tty);
  898. tty->ldisc = ldiscs[N_TTY];
  899. tty->termios.c_line = N_TTY;
  900. if (tty->ldisc.open)
  901. (tty->ldisc.open)(tty);
  902. }
  903. #endif
  904. if (port->blocked_open) {
  905. if (port->close_delay)
  906. msleep_interruptible(jiffies_to_msecs(port->close_delay));
  907. wake_up_interruptible(&port->open_wait);
  908. }
  909. port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  910. wake_up_interruptible(&port->close_wait);
  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);