mn10300-serial.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  1. /* MN10300 On-chip serial port UART driver
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. static const char serial_name[] = "MN10300 Serial driver";
  12. static const char serial_version[] = "mn10300_serial-1.0";
  13. static const char serial_revdate[] = "2007-11-06";
  14. #if defined(CONFIG_MN10300_TTYSM_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  15. #define SUPPORT_SYSRQ
  16. #endif
  17. #include <linux/module.h>
  18. #include <linux/serial.h>
  19. #include <linux/circ_buf.h>
  20. #include <linux/errno.h>
  21. #include <linux/signal.h>
  22. #include <linux/sched.h>
  23. #include <linux/timer.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/tty.h>
  26. #include <linux/tty_flip.h>
  27. #include <linux/major.h>
  28. #include <linux/string.h>
  29. #include <linux/ioport.h>
  30. #include <linux/mm.h>
  31. #include <linux/slab.h>
  32. #include <linux/init.h>
  33. #include <linux/console.h>
  34. #include <linux/sysrq.h>
  35. #include <asm/system.h>
  36. #include <asm/io.h>
  37. #include <asm/irq.h>
  38. #include <asm/bitops.h>
  39. #include <asm/serial-regs.h>
  40. #include <unit/timex.h>
  41. #include "mn10300-serial.h"
  42. #ifdef CONFIG_SMP
  43. #undef GxICR
  44. #define GxICR(X) CROSS_GxICR(X, 0)
  45. #endif /* CONFIG_SMP */
  46. #define kenter(FMT, ...) \
  47. printk(KERN_DEBUG "-->%s(" FMT ")\n", __func__, ##__VA_ARGS__)
  48. #define _enter(FMT, ...) \
  49. no_printk(KERN_DEBUG "-->%s(" FMT ")\n", __func__, ##__VA_ARGS__)
  50. #define kdebug(FMT, ...) \
  51. printk(KERN_DEBUG "--- " FMT "\n", ##__VA_ARGS__)
  52. #define _debug(FMT, ...) \
  53. no_printk(KERN_DEBUG "--- " FMT "\n", ##__VA_ARGS__)
  54. #define kproto(FMT, ...) \
  55. printk(KERN_DEBUG "### MNSERIAL " FMT " ###\n", ##__VA_ARGS__)
  56. #define _proto(FMT, ...) \
  57. no_printk(KERN_DEBUG "### MNSERIAL " FMT " ###\n", ##__VA_ARGS__)
  58. #ifndef CODMSB
  59. /* c_cflag bit meaning */
  60. #define CODMSB 004000000000 /* change Transfer bit-order */
  61. #endif
  62. #define NR_UARTS 3
  63. #ifdef CONFIG_MN10300_TTYSM_CONSOLE
  64. static void mn10300_serial_console_write(struct console *co,
  65. const char *s, unsigned count);
  66. static int __init mn10300_serial_console_setup(struct console *co,
  67. char *options);
  68. static struct uart_driver mn10300_serial_driver;
  69. static struct console mn10300_serial_console = {
  70. .name = "ttySM",
  71. .write = mn10300_serial_console_write,
  72. .device = uart_console_device,
  73. .setup = mn10300_serial_console_setup,
  74. .flags = CON_PRINTBUFFER,
  75. .index = -1,
  76. .data = &mn10300_serial_driver,
  77. };
  78. #endif
  79. static struct uart_driver mn10300_serial_driver = {
  80. .owner = NULL,
  81. .driver_name = "mn10300-serial",
  82. .dev_name = "ttySM",
  83. .major = TTY_MAJOR,
  84. .minor = 128,
  85. .nr = NR_UARTS,
  86. #ifdef CONFIG_MN10300_TTYSM_CONSOLE
  87. .cons = &mn10300_serial_console,
  88. #endif
  89. };
  90. static unsigned int mn10300_serial_tx_empty(struct uart_port *);
  91. static void mn10300_serial_set_mctrl(struct uart_port *, unsigned int mctrl);
  92. static unsigned int mn10300_serial_get_mctrl(struct uart_port *);
  93. static void mn10300_serial_stop_tx(struct uart_port *);
  94. static void mn10300_serial_start_tx(struct uart_port *);
  95. static void mn10300_serial_send_xchar(struct uart_port *, char ch);
  96. static void mn10300_serial_stop_rx(struct uart_port *);
  97. static void mn10300_serial_enable_ms(struct uart_port *);
  98. static void mn10300_serial_break_ctl(struct uart_port *, int ctl);
  99. static int mn10300_serial_startup(struct uart_port *);
  100. static void mn10300_serial_shutdown(struct uart_port *);
  101. static void mn10300_serial_set_termios(struct uart_port *,
  102. struct ktermios *new,
  103. struct ktermios *old);
  104. static const char *mn10300_serial_type(struct uart_port *);
  105. static void mn10300_serial_release_port(struct uart_port *);
  106. static int mn10300_serial_request_port(struct uart_port *);
  107. static void mn10300_serial_config_port(struct uart_port *, int);
  108. static int mn10300_serial_verify_port(struct uart_port *,
  109. struct serial_struct *);
  110. #ifdef CONFIG_CONSOLE_POLL
  111. static void mn10300_serial_poll_put_char(struct uart_port *, unsigned char);
  112. static int mn10300_serial_poll_get_char(struct uart_port *);
  113. #endif
  114. static const struct uart_ops mn10300_serial_ops = {
  115. .tx_empty = mn10300_serial_tx_empty,
  116. .set_mctrl = mn10300_serial_set_mctrl,
  117. .get_mctrl = mn10300_serial_get_mctrl,
  118. .stop_tx = mn10300_serial_stop_tx,
  119. .start_tx = mn10300_serial_start_tx,
  120. .send_xchar = mn10300_serial_send_xchar,
  121. .stop_rx = mn10300_serial_stop_rx,
  122. .enable_ms = mn10300_serial_enable_ms,
  123. .break_ctl = mn10300_serial_break_ctl,
  124. .startup = mn10300_serial_startup,
  125. .shutdown = mn10300_serial_shutdown,
  126. .set_termios = mn10300_serial_set_termios,
  127. .type = mn10300_serial_type,
  128. .release_port = mn10300_serial_release_port,
  129. .request_port = mn10300_serial_request_port,
  130. .config_port = mn10300_serial_config_port,
  131. .verify_port = mn10300_serial_verify_port,
  132. #ifdef CONFIG_CONSOLE_POLL
  133. .poll_put_char = mn10300_serial_poll_put_char,
  134. .poll_get_char = mn10300_serial_poll_get_char,
  135. #endif
  136. };
  137. static irqreturn_t mn10300_serial_interrupt(int irq, void *dev_id);
  138. /*
  139. * the first on-chip serial port: ttySM0 (aka SIF0)
  140. */
  141. #ifdef CONFIG_MN10300_TTYSM0
  142. struct mn10300_serial_port mn10300_serial_port_sif0 = {
  143. .uart.ops = &mn10300_serial_ops,
  144. .uart.membase = (void __iomem *) &SC0CTR,
  145. .uart.mapbase = (unsigned long) &SC0CTR,
  146. .uart.iotype = UPIO_MEM,
  147. .uart.irq = 0,
  148. .uart.uartclk = 0, /* MN10300_IOCLK, */
  149. .uart.fifosize = 1,
  150. .uart.flags = UPF_BOOT_AUTOCONF,
  151. .uart.line = 0,
  152. .uart.type = PORT_MN10300,
  153. .uart.lock =
  154. __SPIN_LOCK_UNLOCKED(mn10300_serial_port_sif0.uart.lock),
  155. .name = "ttySM0",
  156. ._iobase = &SC0CTR,
  157. ._control = &SC0CTR,
  158. ._status = (volatile u8 *)&SC0STR,
  159. ._intr = &SC0ICR,
  160. ._rxb = &SC0RXB,
  161. ._txb = &SC0TXB,
  162. .rx_name = "ttySM0:Rx",
  163. .tx_name = "ttySM0:Tx",
  164. #if defined(CONFIG_MN10300_TTYSM0_TIMER8)
  165. .tm_name = "ttySM0:Timer8",
  166. ._tmxmd = &TM8MD,
  167. ._tmxbr = &TM8BR,
  168. ._tmicr = &TM8ICR,
  169. .tm_irq = TM8IRQ,
  170. .div_timer = MNSCx_DIV_TIMER_16BIT,
  171. #elif defined(CONFIG_MN10300_TTYSM0_TIMER0)
  172. .tm_name = "ttySM0:Timer0",
  173. ._tmxmd = &TM0MD,
  174. ._tmxbr = (volatile u16 *)&TM0BR,
  175. ._tmicr = &TM0ICR,
  176. .tm_irq = TM0IRQ,
  177. .div_timer = MNSCx_DIV_TIMER_8BIT,
  178. #elif defined(CONFIG_MN10300_TTYSM0_TIMER2)
  179. .tm_name = "ttySM0:Timer2",
  180. ._tmxmd = &TM2MD,
  181. ._tmxbr = (volatile u16 *)&TM2BR,
  182. ._tmicr = &TM2ICR,
  183. .tm_irq = TM2IRQ,
  184. .div_timer = MNSCx_DIV_TIMER_8BIT,
  185. #else
  186. #error "Unknown config for ttySM0"
  187. #endif
  188. .rx_irq = SC0RXIRQ,
  189. .tx_irq = SC0TXIRQ,
  190. .rx_icr = &GxICR(SC0RXIRQ),
  191. .tx_icr = &GxICR(SC0TXIRQ),
  192. .clock_src = MNSCx_CLOCK_SRC_IOCLK,
  193. .options = 0,
  194. #ifdef CONFIG_GDBSTUB_ON_TTYSM0
  195. .gdbstub = 1,
  196. #endif
  197. };
  198. #endif /* CONFIG_MN10300_TTYSM0 */
  199. /*
  200. * the second on-chip serial port: ttySM1 (aka SIF1)
  201. */
  202. #ifdef CONFIG_MN10300_TTYSM1
  203. struct mn10300_serial_port mn10300_serial_port_sif1 = {
  204. .uart.ops = &mn10300_serial_ops,
  205. .uart.membase = (void __iomem *) &SC1CTR,
  206. .uart.mapbase = (unsigned long) &SC1CTR,
  207. .uart.iotype = UPIO_MEM,
  208. .uart.irq = 0,
  209. .uart.uartclk = 0, /* MN10300_IOCLK, */
  210. .uart.fifosize = 1,
  211. .uart.flags = UPF_BOOT_AUTOCONF,
  212. .uart.line = 1,
  213. .uart.type = PORT_MN10300,
  214. .uart.lock =
  215. __SPIN_LOCK_UNLOCKED(mn10300_serial_port_sif1.uart.lock),
  216. .name = "ttySM1",
  217. ._iobase = &SC1CTR,
  218. ._control = &SC1CTR,
  219. ._status = (volatile u8 *)&SC1STR,
  220. ._intr = &SC1ICR,
  221. ._rxb = &SC1RXB,
  222. ._txb = &SC1TXB,
  223. .rx_name = "ttySM1:Rx",
  224. .tx_name = "ttySM1:Tx",
  225. #if defined(CONFIG_MN10300_TTYSM1_TIMER9)
  226. .tm_name = "ttySM1:Timer9",
  227. ._tmxmd = &TM9MD,
  228. ._tmxbr = &TM9BR,
  229. ._tmicr = &TM9ICR,
  230. .tm_irq = TM9IRQ,
  231. .div_timer = MNSCx_DIV_TIMER_16BIT,
  232. #elif defined(CONFIG_MN10300_TTYSM1_TIMER3)
  233. .tm_name = "ttySM1:Timer3",
  234. ._tmxmd = &TM3MD,
  235. ._tmxbr = (volatile u16 *)&TM3BR,
  236. ._tmicr = &TM3ICR,
  237. .tm_irq = TM3IRQ,
  238. .div_timer = MNSCx_DIV_TIMER_8BIT,
  239. #elif defined(CONFIG_MN10300_TTYSM1_TIMER12)
  240. .tm_name = "ttySM1/Timer12",
  241. ._tmxmd = &TM12MD,
  242. ._tmxbr = &TM12BR,
  243. ._tmicr = &TM12ICR,
  244. .tm_irq = TM12IRQ,
  245. .div_timer = MNSCx_DIV_TIMER_16BIT,
  246. #else
  247. #error "Unknown config for ttySM1"
  248. #endif
  249. .rx_irq = SC1RXIRQ,
  250. .tx_irq = SC1TXIRQ,
  251. .rx_icr = &GxICR(SC1RXIRQ),
  252. .tx_icr = &GxICR(SC1TXIRQ),
  253. .clock_src = MNSCx_CLOCK_SRC_IOCLK,
  254. .options = 0,
  255. #ifdef CONFIG_GDBSTUB_ON_TTYSM1
  256. .gdbstub = 1,
  257. #endif
  258. };
  259. #endif /* CONFIG_MN10300_TTYSM1 */
  260. /*
  261. * the third on-chip serial port: ttySM2 (aka SIF2)
  262. */
  263. #ifdef CONFIG_MN10300_TTYSM2
  264. struct mn10300_serial_port mn10300_serial_port_sif2 = {
  265. .uart.ops = &mn10300_serial_ops,
  266. .uart.membase = (void __iomem *) &SC2CTR,
  267. .uart.mapbase = (unsigned long) &SC2CTR,
  268. .uart.iotype = UPIO_MEM,
  269. .uart.irq = 0,
  270. .uart.uartclk = 0, /* MN10300_IOCLK, */
  271. .uart.fifosize = 1,
  272. .uart.flags = UPF_BOOT_AUTOCONF,
  273. .uart.line = 2,
  274. #ifdef CONFIG_MN10300_TTYSM2_CTS
  275. .uart.type = PORT_MN10300_CTS,
  276. #else
  277. .uart.type = PORT_MN10300,
  278. #endif
  279. .uart.lock =
  280. __SPIN_LOCK_UNLOCKED(mn10300_serial_port_sif2.uart.lock),
  281. .name = "ttySM2",
  282. ._iobase = &SC2CTR,
  283. ._control = &SC2CTR,
  284. ._status = (volatile u8 *)&SC2STR,
  285. ._intr = &SC2ICR,
  286. ._rxb = &SC2RXB,
  287. ._txb = &SC2TXB,
  288. .rx_name = "ttySM2:Rx",
  289. .tx_name = "ttySM2:Tx",
  290. #if defined(CONFIG_MN10300_TTYSM2_TIMER10)
  291. .tm_name = "ttySM2/Timer10",
  292. ._tmxmd = &TM10MD,
  293. ._tmxbr = &TM10BR,
  294. ._tmicr = &TM10ICR,
  295. .tm_irq = TM10IRQ,
  296. .div_timer = MNSCx_DIV_TIMER_16BIT,
  297. #elif defined(CONFIG_MN10300_TTYSM2_TIMER9)
  298. .tm_name = "ttySM2/Timer9",
  299. ._tmxmd = &TM9MD,
  300. ._tmxbr = &TM9BR,
  301. ._tmicr = &TM9ICR,
  302. .tm_irq = TM9IRQ,
  303. .div_timer = MNSCx_DIV_TIMER_16BIT,
  304. #elif defined(CONFIG_MN10300_TTYSM2_TIMER1)
  305. .tm_name = "ttySM2/Timer1",
  306. ._tmxmd = &TM1MD,
  307. ._tmxbr = (volatile u16 *)&TM1BR,
  308. ._tmicr = &TM1ICR,
  309. .tm_irq = TM1IRQ,
  310. .div_timer = MNSCx_DIV_TIMER_8BIT,
  311. #elif defined(CONFIG_MN10300_TTYSM2_TIMER3)
  312. .tm_name = "ttySM2/Timer3",
  313. ._tmxmd = &TM3MD,
  314. ._tmxbr = (volatile u16 *)&TM3BR,
  315. ._tmicr = &TM3ICR,
  316. .tm_irq = TM3IRQ,
  317. .div_timer = MNSCx_DIV_TIMER_8BIT,
  318. #else
  319. #error "Unknown config for ttySM2"
  320. #endif
  321. .rx_irq = SC2RXIRQ,
  322. .tx_irq = SC2TXIRQ,
  323. .rx_icr = &GxICR(SC2RXIRQ),
  324. .tx_icr = &GxICR(SC2TXIRQ),
  325. .clock_src = MNSCx_CLOCK_SRC_IOCLK,
  326. #ifdef CONFIG_MN10300_TTYSM2_CTS
  327. .options = MNSCx_OPT_CTS,
  328. #else
  329. .options = 0,
  330. #endif
  331. #ifdef CONFIG_GDBSTUB_ON_TTYSM2
  332. .gdbstub = 1,
  333. #endif
  334. };
  335. #endif /* CONFIG_MN10300_TTYSM2 */
  336. /*
  337. * list of available serial ports
  338. */
  339. struct mn10300_serial_port *mn10300_serial_ports[NR_UARTS + 1] = {
  340. #ifdef CONFIG_MN10300_TTYSM0
  341. [0] = &mn10300_serial_port_sif0,
  342. #endif
  343. #ifdef CONFIG_MN10300_TTYSM1
  344. [1] = &mn10300_serial_port_sif1,
  345. #endif
  346. #ifdef CONFIG_MN10300_TTYSM2
  347. [2] = &mn10300_serial_port_sif2,
  348. #endif
  349. [NR_UARTS] = NULL,
  350. };
  351. /*
  352. * we abuse the serial ports' baud timers' interrupt lines to get the ability
  353. * to deliver interrupts to userspace as we use the ports' interrupt lines to
  354. * do virtual DMA on account of the ports having no hardware FIFOs
  355. *
  356. * we can generate an interrupt manually in the assembly stubs by writing to
  357. * the enable and detect bits in the interrupt control register, so all we need
  358. * to do here is disable the interrupt line
  359. *
  360. * note that we can't just leave the line enabled as the baud rate timer *also*
  361. * generates interrupts
  362. */
  363. static void mn10300_serial_mask_ack(unsigned int irq)
  364. {
  365. unsigned long flags;
  366. u16 tmp;
  367. flags = arch_local_cli_save();
  368. GxICR(irq) = GxICR_LEVEL_6;
  369. tmp = GxICR(irq); /* flush write buffer */
  370. arch_local_irq_restore(flags);
  371. }
  372. static void mn10300_serial_chip_mask_ack(struct irq_data *d)
  373. {
  374. mn10300_serial_mask_ack(d->irq);
  375. }
  376. static void mn10300_serial_nop(struct irq_data *d)
  377. {
  378. }
  379. static struct irq_chip mn10300_serial_pic = {
  380. .name = "mnserial",
  381. .irq_ack = mn10300_serial_chip_mask_ack,
  382. .irq_mask = mn10300_serial_chip_mask_ack,
  383. .irq_mask_ack = mn10300_serial_chip_mask_ack,
  384. .irq_unmask = mn10300_serial_nop,
  385. };
  386. /*
  387. * serial virtual DMA interrupt jump table
  388. */
  389. struct mn10300_serial_int mn10300_serial_int_tbl[NR_IRQS];
  390. static void mn10300_serial_dis_tx_intr(struct mn10300_serial_port *port)
  391. {
  392. unsigned long flags;
  393. u16 x;
  394. flags = arch_local_cli_save();
  395. *port->tx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
  396. x = *port->tx_icr;
  397. arch_local_irq_restore(flags);
  398. }
  399. static void mn10300_serial_en_tx_intr(struct mn10300_serial_port *port)
  400. {
  401. unsigned long flags;
  402. u16 x;
  403. flags = arch_local_cli_save();
  404. *port->tx_icr =
  405. NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL) | GxICR_ENABLE;
  406. x = *port->tx_icr;
  407. arch_local_irq_restore(flags);
  408. }
  409. static void mn10300_serial_dis_rx_intr(struct mn10300_serial_port *port)
  410. {
  411. unsigned long flags;
  412. u16 x;
  413. flags = arch_local_cli_save();
  414. *port->rx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
  415. x = *port->rx_icr;
  416. arch_local_irq_restore(flags);
  417. }
  418. /*
  419. * multi-bit equivalent of test_and_clear_bit()
  420. */
  421. static int mask_test_and_clear(volatile u8 *ptr, u8 mask)
  422. {
  423. u32 epsw;
  424. asm volatile(" bclr %1,(%2) \n"
  425. " mov epsw,%0 \n"
  426. : "=d"(epsw) : "d"(mask), "a"(ptr)
  427. : "cc", "memory");
  428. return !(epsw & EPSW_FLAG_Z);
  429. }
  430. /*
  431. * receive chars from the ring buffer for this serial port
  432. * - must do break detection here (not done in the UART)
  433. */
  434. static void mn10300_serial_receive_interrupt(struct mn10300_serial_port *port)
  435. {
  436. struct uart_icount *icount = &port->uart.icount;
  437. struct tty_struct *tty = port->uart.state->port.tty;
  438. unsigned ix;
  439. int count;
  440. u8 st, ch, push, status, overrun;
  441. _enter("%s", port->name);
  442. push = 0;
  443. count = CIRC_CNT(port->rx_inp, port->rx_outp, MNSC_BUFFER_SIZE);
  444. count = tty_buffer_request_room(tty, count);
  445. if (count == 0) {
  446. if (!tty->low_latency)
  447. tty_flip_buffer_push(tty);
  448. return;
  449. }
  450. try_again:
  451. /* pull chars out of the hat */
  452. ix = port->rx_outp;
  453. if (ix == port->rx_inp) {
  454. if (push && !tty->low_latency)
  455. tty_flip_buffer_push(tty);
  456. return;
  457. }
  458. ch = port->rx_buffer[ix++];
  459. st = port->rx_buffer[ix++];
  460. smp_rmb();
  461. port->rx_outp = ix & (MNSC_BUFFER_SIZE - 1);
  462. port->uart.icount.rx++;
  463. st &= SC01STR_FEF | SC01STR_PEF | SC01STR_OEF;
  464. status = 0;
  465. overrun = 0;
  466. /* the UART doesn't detect BREAK, so we have to do that ourselves
  467. * - it starts as a framing error on a NUL character
  468. * - then we count another two NUL characters before issuing TTY_BREAK
  469. * - then we end on a normal char or one that has all the bottom bits
  470. * zero and the top bits set
  471. */
  472. switch (port->rx_brk) {
  473. case 0:
  474. /* not breaking at the moment */
  475. break;
  476. case 1:
  477. if (st & SC01STR_FEF && ch == 0) {
  478. port->rx_brk = 2;
  479. goto try_again;
  480. }
  481. goto not_break;
  482. case 2:
  483. if (st & SC01STR_FEF && ch == 0) {
  484. port->rx_brk = 3;
  485. _proto("Rx Break Detected");
  486. icount->brk++;
  487. if (uart_handle_break(&port->uart))
  488. goto ignore_char;
  489. status |= 1 << TTY_BREAK;
  490. goto insert;
  491. }
  492. goto not_break;
  493. default:
  494. if (st & (SC01STR_FEF | SC01STR_PEF | SC01STR_OEF))
  495. goto try_again; /* still breaking */
  496. port->rx_brk = 0; /* end of the break */
  497. switch (ch) {
  498. case 0xFF:
  499. case 0xFE:
  500. case 0xFC:
  501. case 0xF8:
  502. case 0xF0:
  503. case 0xE0:
  504. case 0xC0:
  505. case 0x80:
  506. case 0x00:
  507. /* discard char at probable break end */
  508. goto try_again;
  509. }
  510. break;
  511. }
  512. process_errors:
  513. /* handle framing error */
  514. if (st & SC01STR_FEF) {
  515. if (ch == 0) {
  516. /* framing error with NUL char is probably a BREAK */
  517. port->rx_brk = 1;
  518. goto try_again;
  519. }
  520. _proto("Rx Framing Error");
  521. icount->frame++;
  522. status |= 1 << TTY_FRAME;
  523. }
  524. /* handle parity error */
  525. if (st & SC01STR_PEF) {
  526. _proto("Rx Parity Error");
  527. icount->parity++;
  528. status = TTY_PARITY;
  529. }
  530. /* handle normal char */
  531. if (status == 0) {
  532. if (uart_handle_sysrq_char(&port->uart, ch))
  533. goto ignore_char;
  534. status = (1 << TTY_NORMAL);
  535. }
  536. /* handle overrun error */
  537. if (st & SC01STR_OEF) {
  538. if (port->rx_brk)
  539. goto try_again;
  540. _proto("Rx Overrun Error");
  541. icount->overrun++;
  542. overrun = 1;
  543. }
  544. insert:
  545. status &= port->uart.read_status_mask;
  546. if (!overrun && !(status & port->uart.ignore_status_mask)) {
  547. int flag;
  548. if (status & (1 << TTY_BREAK))
  549. flag = TTY_BREAK;
  550. else if (status & (1 << TTY_PARITY))
  551. flag = TTY_PARITY;
  552. else if (status & (1 << TTY_FRAME))
  553. flag = TTY_FRAME;
  554. else
  555. flag = TTY_NORMAL;
  556. tty_insert_flip_char(tty, ch, flag);
  557. }
  558. /* overrun is special, since it's reported immediately, and doesn't
  559. * affect the current character
  560. */
  561. if (overrun)
  562. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  563. count--;
  564. if (count <= 0) {
  565. if (!tty->low_latency)
  566. tty_flip_buffer_push(tty);
  567. return;
  568. }
  569. ignore_char:
  570. push = 1;
  571. goto try_again;
  572. not_break:
  573. port->rx_brk = 0;
  574. goto process_errors;
  575. }
  576. /*
  577. * handle an interrupt from the serial transmission "virtual DMA" driver
  578. * - note: the interrupt routine will disable its own interrupts when the Tx
  579. * buffer is empty
  580. */
  581. static void mn10300_serial_transmit_interrupt(struct mn10300_serial_port *port)
  582. {
  583. _enter("%s", port->name);
  584. if (!port->uart.state || !port->uart.state->port.tty) {
  585. mn10300_serial_dis_tx_intr(port);
  586. return;
  587. }
  588. if (uart_tx_stopped(&port->uart) ||
  589. uart_circ_empty(&port->uart.state->xmit))
  590. mn10300_serial_dis_tx_intr(port);
  591. if (uart_circ_chars_pending(&port->uart.state->xmit) < WAKEUP_CHARS)
  592. uart_write_wakeup(&port->uart);
  593. }
  594. /*
  595. * deal with a change in the status of the CTS line
  596. */
  597. static void mn10300_serial_cts_changed(struct mn10300_serial_port *port, u8 st)
  598. {
  599. u16 ctr;
  600. port->tx_cts = st;
  601. port->uart.icount.cts++;
  602. /* flip the CTS state selector flag to interrupt when it changes
  603. * back */
  604. ctr = *port->_control;
  605. ctr ^= SC2CTR_TWS;
  606. *port->_control = ctr;
  607. uart_handle_cts_change(&port->uart, st & SC2STR_CTS);
  608. wake_up_interruptible(&port->uart.state->port.delta_msr_wait);
  609. }
  610. /*
  611. * handle a virtual interrupt generated by the lower level "virtual DMA"
  612. * routines (irq is the baud timer interrupt)
  613. */
  614. static irqreturn_t mn10300_serial_interrupt(int irq, void *dev_id)
  615. {
  616. struct mn10300_serial_port *port = dev_id;
  617. u8 st;
  618. spin_lock(&port->uart.lock);
  619. if (port->intr_flags) {
  620. _debug("INT %s: %x", port->name, port->intr_flags);
  621. if (mask_test_and_clear(&port->intr_flags, MNSCx_RX_AVAIL))
  622. mn10300_serial_receive_interrupt(port);
  623. if (mask_test_and_clear(&port->intr_flags,
  624. MNSCx_TX_SPACE | MNSCx_TX_EMPTY))
  625. mn10300_serial_transmit_interrupt(port);
  626. }
  627. /* the only modem control line amongst the whole lot is CTS on
  628. * serial port 2 */
  629. if (port->type == PORT_MN10300_CTS) {
  630. st = *port->_status;
  631. if ((port->tx_cts ^ st) & SC2STR_CTS)
  632. mn10300_serial_cts_changed(port, st);
  633. }
  634. spin_unlock(&port->uart.lock);
  635. return IRQ_HANDLED;
  636. }
  637. /*
  638. * return indication of whether the hardware transmit buffer is empty
  639. */
  640. static unsigned int mn10300_serial_tx_empty(struct uart_port *_port)
  641. {
  642. struct mn10300_serial_port *port =
  643. container_of(_port, struct mn10300_serial_port, uart);
  644. _enter("%s", port->name);
  645. return (*port->_status & (SC01STR_TXF | SC01STR_TBF)) ?
  646. 0 : TIOCSER_TEMT;
  647. }
  648. /*
  649. * set the modem control lines (we don't have any)
  650. */
  651. static void mn10300_serial_set_mctrl(struct uart_port *_port,
  652. unsigned int mctrl)
  653. {
  654. struct mn10300_serial_port *port __attribute__ ((unused)) =
  655. container_of(_port, struct mn10300_serial_port, uart);
  656. _enter("%s,%x", port->name, mctrl);
  657. }
  658. /*
  659. * get the modem control line statuses
  660. */
  661. static unsigned int mn10300_serial_get_mctrl(struct uart_port *_port)
  662. {
  663. struct mn10300_serial_port *port =
  664. container_of(_port, struct mn10300_serial_port, uart);
  665. _enter("%s", port->name);
  666. if (port->type == PORT_MN10300_CTS && !(*port->_status & SC2STR_CTS))
  667. return TIOCM_CAR | TIOCM_DSR;
  668. return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
  669. }
  670. /*
  671. * stop transmitting characters
  672. */
  673. static void mn10300_serial_stop_tx(struct uart_port *_port)
  674. {
  675. struct mn10300_serial_port *port =
  676. container_of(_port, struct mn10300_serial_port, uart);
  677. _enter("%s", port->name);
  678. /* disable the virtual DMA */
  679. mn10300_serial_dis_tx_intr(port);
  680. }
  681. /*
  682. * start transmitting characters
  683. * - jump-start transmission if it has stalled
  684. * - enable the serial Tx interrupt (used by the virtual DMA controller)
  685. * - force an interrupt to happen if necessary
  686. */
  687. static void mn10300_serial_start_tx(struct uart_port *_port)
  688. {
  689. struct mn10300_serial_port *port =
  690. container_of(_port, struct mn10300_serial_port, uart);
  691. u16 x;
  692. _enter("%s{%lu}",
  693. port->name,
  694. CIRC_CNT(&port->uart.state->xmit.head,
  695. &port->uart.state->xmit.tail,
  696. UART_XMIT_SIZE));
  697. /* kick the virtual DMA controller */
  698. arch_local_cli();
  699. x = *port->tx_icr;
  700. x |= GxICR_ENABLE;
  701. if (*port->_status & SC01STR_TBF)
  702. x &= ~(GxICR_REQUEST | GxICR_DETECT);
  703. else
  704. x |= GxICR_REQUEST | GxICR_DETECT;
  705. _debug("CTR=%04hx ICR=%02hx STR=%04x TMD=%02hx TBR=%04hx ICR=%04hx",
  706. *port->_control, *port->_intr, *port->_status,
  707. *port->_tmxmd,
  708. (port->div_timer == MNSCx_DIV_TIMER_8BIT) ?
  709. *(volatile u8 *)port->_tmxbr : *port->_tmxbr,
  710. *port->tx_icr);
  711. *port->tx_icr = x;
  712. x = *port->tx_icr;
  713. arch_local_sti();
  714. }
  715. /*
  716. * transmit a high-priority XON/XOFF character
  717. */
  718. static void mn10300_serial_send_xchar(struct uart_port *_port, char ch)
  719. {
  720. struct mn10300_serial_port *port =
  721. container_of(_port, struct mn10300_serial_port, uart);
  722. _enter("%s,%02x", port->name, ch);
  723. if (likely(port->gdbstub)) {
  724. port->tx_xchar = ch;
  725. if (ch)
  726. mn10300_serial_en_tx_intr(port);
  727. }
  728. }
  729. /*
  730. * stop receiving characters
  731. * - called whilst the port is being closed
  732. */
  733. static void mn10300_serial_stop_rx(struct uart_port *_port)
  734. {
  735. struct mn10300_serial_port *port =
  736. container_of(_port, struct mn10300_serial_port, uart);
  737. u16 ctr;
  738. _enter("%s", port->name);
  739. ctr = *port->_control;
  740. ctr &= ~SC01CTR_RXE;
  741. *port->_control = ctr;
  742. mn10300_serial_dis_rx_intr(port);
  743. }
  744. /*
  745. * enable modem status interrupts
  746. */
  747. static void mn10300_serial_enable_ms(struct uart_port *_port)
  748. {
  749. struct mn10300_serial_port *port =
  750. container_of(_port, struct mn10300_serial_port, uart);
  751. u16 ctr, cts;
  752. _enter("%s", port->name);
  753. if (port->type == PORT_MN10300_CTS) {
  754. /* want to interrupt when CTS goes low if CTS is now high and
  755. * vice versa
  756. */
  757. port->tx_cts = *port->_status;
  758. cts = (port->tx_cts & SC2STR_CTS) ?
  759. SC2CTR_TWE : SC2CTR_TWE | SC2CTR_TWS;
  760. ctr = *port->_control;
  761. ctr &= ~SC2CTR_TWS;
  762. ctr |= cts;
  763. *port->_control = ctr;
  764. mn10300_serial_en_tx_intr(port);
  765. }
  766. }
  767. /*
  768. * transmit or cease transmitting a break signal
  769. */
  770. static void mn10300_serial_break_ctl(struct uart_port *_port, int ctl)
  771. {
  772. struct mn10300_serial_port *port =
  773. container_of(_port, struct mn10300_serial_port, uart);
  774. _enter("%s,%d", port->name, ctl);
  775. if (ctl) {
  776. /* tell the virtual DMA handler to assert BREAK */
  777. port->tx_break = 1;
  778. mn10300_serial_en_tx_intr(port);
  779. } else {
  780. port->tx_break = 0;
  781. *port->_control &= ~SC01CTR_BKE;
  782. mn10300_serial_en_tx_intr(port);
  783. }
  784. }
  785. /*
  786. * grab the interrupts and enable the port for reception
  787. */
  788. static int mn10300_serial_startup(struct uart_port *_port)
  789. {
  790. struct mn10300_serial_port *port =
  791. container_of(_port, struct mn10300_serial_port, uart);
  792. struct mn10300_serial_int *pint;
  793. _enter("%s{%d}", port->name, port->gdbstub);
  794. if (unlikely(port->gdbstub))
  795. return -EBUSY;
  796. /* allocate an Rx buffer for the virtual DMA handler */
  797. port->rx_buffer = kmalloc(MNSC_BUFFER_SIZE, GFP_KERNEL);
  798. if (!port->rx_buffer)
  799. return -ENOMEM;
  800. port->rx_inp = port->rx_outp = 0;
  801. /* finally, enable the device */
  802. *port->_intr = SC01ICR_TI;
  803. *port->_control |= SC01CTR_TXE | SC01CTR_RXE;
  804. pint = &mn10300_serial_int_tbl[port->rx_irq];
  805. pint->port = port;
  806. pint->vdma = mn10300_serial_vdma_rx_handler;
  807. pint = &mn10300_serial_int_tbl[port->tx_irq];
  808. pint->port = port;
  809. pint->vdma = mn10300_serial_vdma_tx_handler;
  810. set_intr_level(port->rx_irq,
  811. NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL));
  812. set_intr_level(port->tx_irq,
  813. NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL));
  814. irq_set_chip(port->tm_irq, &mn10300_serial_pic);
  815. if (request_irq(port->rx_irq, mn10300_serial_interrupt,
  816. IRQF_DISABLED, port->rx_name, port) < 0)
  817. goto error;
  818. if (request_irq(port->tx_irq, mn10300_serial_interrupt,
  819. IRQF_DISABLED, port->tx_name, port) < 0)
  820. goto error2;
  821. if (request_irq(port->tm_irq, mn10300_serial_interrupt,
  822. IRQF_DISABLED, port->tm_name, port) < 0)
  823. goto error3;
  824. mn10300_serial_mask_ack(port->tm_irq);
  825. return 0;
  826. error3:
  827. free_irq(port->tx_irq, port);
  828. error2:
  829. free_irq(port->rx_irq, port);
  830. error:
  831. kfree(port->rx_buffer);
  832. port->rx_buffer = NULL;
  833. return -EBUSY;
  834. }
  835. /*
  836. * shutdown the port and release interrupts
  837. */
  838. static void mn10300_serial_shutdown(struct uart_port *_port)
  839. {
  840. u16 x;
  841. struct mn10300_serial_port *port =
  842. container_of(_port, struct mn10300_serial_port, uart);
  843. _enter("%s", port->name);
  844. /* disable the serial port and its baud rate timer */
  845. port->tx_break = 0;
  846. *port->_control &= ~(SC01CTR_TXE | SC01CTR_RXE | SC01CTR_BKE);
  847. *port->_tmxmd = 0;
  848. if (port->rx_buffer) {
  849. void *buf = port->rx_buffer;
  850. port->rx_buffer = NULL;
  851. kfree(buf);
  852. }
  853. /* disable all intrs */
  854. free_irq(port->tm_irq, port);
  855. free_irq(port->rx_irq, port);
  856. free_irq(port->tx_irq, port);
  857. arch_local_cli();
  858. *port->rx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
  859. x = *port->rx_icr;
  860. *port->tx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
  861. x = *port->tx_icr;
  862. arch_local_sti();
  863. }
  864. /*
  865. * this routine is called to set the UART divisor registers to match the
  866. * specified baud rate for a serial port.
  867. */
  868. static void mn10300_serial_change_speed(struct mn10300_serial_port *port,
  869. struct ktermios *new,
  870. struct ktermios *old)
  871. {
  872. unsigned long flags;
  873. unsigned long ioclk = port->ioclk;
  874. unsigned cflag;
  875. int baud, bits, xdiv, tmp;
  876. u16 tmxbr, scxctr;
  877. u8 tmxmd, battempt;
  878. u8 div_timer = port->div_timer;
  879. _enter("%s{%lu}", port->name, ioclk);
  880. /* byte size and parity */
  881. cflag = new->c_cflag;
  882. switch (cflag & CSIZE) {
  883. case CS7: scxctr = SC01CTR_CLN_7BIT; bits = 9; break;
  884. case CS8: scxctr = SC01CTR_CLN_8BIT; bits = 10; break;
  885. default: scxctr = SC01CTR_CLN_8BIT; bits = 10; break;
  886. }
  887. if (cflag & CSTOPB) {
  888. scxctr |= SC01CTR_STB_2BIT;
  889. bits++;
  890. }
  891. if (cflag & PARENB) {
  892. bits++;
  893. if (cflag & PARODD)
  894. scxctr |= SC01CTR_PB_ODD;
  895. #ifdef CMSPAR
  896. else if (cflag & CMSPAR)
  897. scxctr |= SC01CTR_PB_FIXED0;
  898. #endif
  899. else
  900. scxctr |= SC01CTR_PB_EVEN;
  901. }
  902. /* Determine divisor based on baud rate */
  903. battempt = 0;
  904. switch (port->uart.line) {
  905. #ifdef CONFIG_MN10300_TTYSM0
  906. case 0: /* ttySM0 */
  907. #if defined(CONFIG_MN10300_TTYSM0_TIMER8)
  908. scxctr |= SC0CTR_CK_TM8UFLOW_8;
  909. #elif defined(CONFIG_MN10300_TTYSM0_TIMER0)
  910. scxctr |= SC0CTR_CK_TM0UFLOW_8;
  911. #elif defined(CONFIG_MN10300_TTYSM0_TIMER2)
  912. scxctr |= SC0CTR_CK_TM2UFLOW_8;
  913. #else
  914. #error "Unknown config for ttySM0"
  915. #endif
  916. break;
  917. #endif /* CONFIG_MN10300_TTYSM0 */
  918. #ifdef CONFIG_MN10300_TTYSM1
  919. case 1: /* ttySM1 */
  920. #if defined(CONFIG_AM33_2) || defined(CONFIG_AM33_3)
  921. #if defined(CONFIG_MN10300_TTYSM1_TIMER9)
  922. scxctr |= SC1CTR_CK_TM9UFLOW_8;
  923. #elif defined(CONFIG_MN10300_TTYSM1_TIMER3)
  924. scxctr |= SC1CTR_CK_TM3UFLOW_8;
  925. #else
  926. #error "Unknown config for ttySM1"
  927. #endif
  928. #else /* CONFIG_AM33_2 || CONFIG_AM33_3 */
  929. #if defined(CONFIG_MN10300_TTYSM1_TIMER12)
  930. scxctr |= SC1CTR_CK_TM12UFLOW_8;
  931. #else
  932. #error "Unknown config for ttySM1"
  933. #endif
  934. #endif /* CONFIG_AM33_2 || CONFIG_AM33_3 */
  935. break;
  936. #endif /* CONFIG_MN10300_TTYSM1 */
  937. #ifdef CONFIG_MN10300_TTYSM2
  938. case 2: /* ttySM2 */
  939. #if defined(CONFIG_AM33_2)
  940. #if defined(CONFIG_MN10300_TTYSM2_TIMER10)
  941. scxctr |= SC2CTR_CK_TM10UFLOW;
  942. #else
  943. #error "Unknown config for ttySM2"
  944. #endif
  945. #else /* CONFIG_AM33_2 */
  946. #if defined(CONFIG_MN10300_TTYSM2_TIMER9)
  947. scxctr |= SC2CTR_CK_TM9UFLOW_8;
  948. #elif defined(CONFIG_MN10300_TTYSM2_TIMER1)
  949. scxctr |= SC2CTR_CK_TM1UFLOW_8;
  950. #elif defined(CONFIG_MN10300_TTYSM2_TIMER3)
  951. scxctr |= SC2CTR_CK_TM3UFLOW_8;
  952. #else
  953. #error "Unknown config for ttySM2"
  954. #endif
  955. #endif /* CONFIG_AM33_2 */
  956. break;
  957. #endif /* CONFIG_MN10300_TTYSM2 */
  958. default:
  959. break;
  960. }
  961. try_alternative:
  962. baud = uart_get_baud_rate(&port->uart, new, old, 0,
  963. port->ioclk / 8);
  964. _debug("ALT %d [baud %d]", battempt, baud);
  965. if (!baud)
  966. baud = 9600; /* B0 transition handled in rs_set_termios */
  967. xdiv = 1;
  968. if (baud == 134) {
  969. baud = 269; /* 134 is really 134.5 */
  970. xdiv = 2;
  971. }
  972. if (baud == 38400 &&
  973. (port->uart.flags & UPF_SPD_MASK) == UPF_SPD_CUST
  974. ) {
  975. _debug("CUSTOM %u", port->uart.custom_divisor);
  976. if (div_timer == MNSCx_DIV_TIMER_16BIT) {
  977. if (port->uart.custom_divisor <= 65535) {
  978. tmxmd = TM8MD_SRC_IOCLK;
  979. tmxbr = port->uart.custom_divisor;
  980. port->uart.uartclk = ioclk;
  981. goto timer_okay;
  982. }
  983. if (port->uart.custom_divisor / 8 <= 65535) {
  984. tmxmd = TM8MD_SRC_IOCLK_8;
  985. tmxbr = port->uart.custom_divisor / 8;
  986. port->uart.custom_divisor = tmxbr * 8;
  987. port->uart.uartclk = ioclk / 8;
  988. goto timer_okay;
  989. }
  990. if (port->uart.custom_divisor / 32 <= 65535) {
  991. tmxmd = TM8MD_SRC_IOCLK_32;
  992. tmxbr = port->uart.custom_divisor / 32;
  993. port->uart.custom_divisor = tmxbr * 32;
  994. port->uart.uartclk = ioclk / 32;
  995. goto timer_okay;
  996. }
  997. } else if (div_timer == MNSCx_DIV_TIMER_8BIT) {
  998. if (port->uart.custom_divisor <= 255) {
  999. tmxmd = TM2MD_SRC_IOCLK;
  1000. tmxbr = port->uart.custom_divisor;
  1001. port->uart.uartclk = ioclk;
  1002. goto timer_okay;
  1003. }
  1004. if (port->uart.custom_divisor / 8 <= 255) {
  1005. tmxmd = TM2MD_SRC_IOCLK_8;
  1006. tmxbr = port->uart.custom_divisor / 8;
  1007. port->uart.custom_divisor = tmxbr * 8;
  1008. port->uart.uartclk = ioclk / 8;
  1009. goto timer_okay;
  1010. }
  1011. if (port->uart.custom_divisor / 32 <= 255) {
  1012. tmxmd = TM2MD_SRC_IOCLK_32;
  1013. tmxbr = port->uart.custom_divisor / 32;
  1014. port->uart.custom_divisor = tmxbr * 32;
  1015. port->uart.uartclk = ioclk / 32;
  1016. goto timer_okay;
  1017. }
  1018. }
  1019. }
  1020. switch (div_timer) {
  1021. case MNSCx_DIV_TIMER_16BIT:
  1022. port->uart.uartclk = ioclk;
  1023. tmxmd = TM8MD_SRC_IOCLK;
  1024. tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
  1025. if (tmp > 0 && tmp <= 65535)
  1026. goto timer_okay;
  1027. port->uart.uartclk = ioclk / 8;
  1028. tmxmd = TM8MD_SRC_IOCLK_8;
  1029. tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
  1030. if (tmp > 0 && tmp <= 65535)
  1031. goto timer_okay;
  1032. port->uart.uartclk = ioclk / 32;
  1033. tmxmd = TM8MD_SRC_IOCLK_32;
  1034. tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
  1035. if (tmp > 0 && tmp <= 65535)
  1036. goto timer_okay;
  1037. break;
  1038. case MNSCx_DIV_TIMER_8BIT:
  1039. port->uart.uartclk = ioclk;
  1040. tmxmd = TM2MD_SRC_IOCLK;
  1041. tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
  1042. if (tmp > 0 && tmp <= 255)
  1043. goto timer_okay;
  1044. port->uart.uartclk = ioclk / 8;
  1045. tmxmd = TM2MD_SRC_IOCLK_8;
  1046. tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
  1047. if (tmp > 0 && tmp <= 255)
  1048. goto timer_okay;
  1049. port->uart.uartclk = ioclk / 32;
  1050. tmxmd = TM2MD_SRC_IOCLK_32;
  1051. tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
  1052. if (tmp > 0 && tmp <= 255)
  1053. goto timer_okay;
  1054. break;
  1055. default:
  1056. BUG();
  1057. return;
  1058. }
  1059. /* refuse to change to a baud rate we can't support */
  1060. _debug("CAN'T SUPPORT");
  1061. switch (battempt) {
  1062. case 0:
  1063. if (old) {
  1064. new->c_cflag &= ~CBAUD;
  1065. new->c_cflag |= (old->c_cflag & CBAUD);
  1066. battempt = 1;
  1067. goto try_alternative;
  1068. }
  1069. case 1:
  1070. /* as a last resort, if the quotient is zero, default to 9600
  1071. * bps */
  1072. new->c_cflag &= ~CBAUD;
  1073. new->c_cflag |= B9600;
  1074. battempt = 2;
  1075. goto try_alternative;
  1076. default:
  1077. /* hmmm... can't seem to support 9600 either
  1078. * - we could try iterating through the speeds we know about to
  1079. * find the lowest
  1080. */
  1081. new->c_cflag &= ~CBAUD;
  1082. new->c_cflag |= B0;
  1083. if (div_timer == MNSCx_DIV_TIMER_16BIT)
  1084. tmxmd = TM8MD_SRC_IOCLK_32;
  1085. else if (div_timer == MNSCx_DIV_TIMER_8BIT)
  1086. tmxmd = TM2MD_SRC_IOCLK_32;
  1087. tmxbr = 1;
  1088. port->uart.uartclk = ioclk / 32;
  1089. break;
  1090. }
  1091. timer_okay:
  1092. _debug("UARTCLK: %u / %hu", port->uart.uartclk, tmxbr);
  1093. /* make the changes */
  1094. spin_lock_irqsave(&port->uart.lock, flags);
  1095. uart_update_timeout(&port->uart, new->c_cflag, baud);
  1096. /* set the timer to produce the required baud rate */
  1097. switch (div_timer) {
  1098. case MNSCx_DIV_TIMER_16BIT:
  1099. *port->_tmxmd = 0;
  1100. *port->_tmxbr = tmxbr;
  1101. *port->_tmxmd = TM8MD_INIT_COUNTER;
  1102. *port->_tmxmd = tmxmd | TM8MD_COUNT_ENABLE;
  1103. break;
  1104. case MNSCx_DIV_TIMER_8BIT:
  1105. *port->_tmxmd = 0;
  1106. *(volatile u8 *) port->_tmxbr = (u8) tmxbr;
  1107. *port->_tmxmd = TM2MD_INIT_COUNTER;
  1108. *port->_tmxmd = tmxmd | TM2MD_COUNT_ENABLE;
  1109. break;
  1110. }
  1111. /* CTS flow control flag and modem status interrupts */
  1112. scxctr &= ~(SC2CTR_TWE | SC2CTR_TWS);
  1113. if (port->type == PORT_MN10300_CTS && cflag & CRTSCTS) {
  1114. /* want to interrupt when CTS goes low if CTS is now
  1115. * high and vice versa
  1116. */
  1117. port->tx_cts = *port->_status;
  1118. if (port->tx_cts & SC2STR_CTS)
  1119. scxctr |= SC2CTR_TWE;
  1120. else
  1121. scxctr |= SC2CTR_TWE | SC2CTR_TWS;
  1122. }
  1123. /* set up parity check flag */
  1124. port->uart.read_status_mask = (1 << TTY_NORMAL) | (1 << TTY_OVERRUN);
  1125. if (new->c_iflag & INPCK)
  1126. port->uart.read_status_mask |=
  1127. (1 << TTY_PARITY) | (1 << TTY_FRAME);
  1128. if (new->c_iflag & (BRKINT | PARMRK))
  1129. port->uart.read_status_mask |= (1 << TTY_BREAK);
  1130. /* characters to ignore */
  1131. port->uart.ignore_status_mask = 0;
  1132. if (new->c_iflag & IGNPAR)
  1133. port->uart.ignore_status_mask |=
  1134. (1 << TTY_PARITY) | (1 << TTY_FRAME);
  1135. if (new->c_iflag & IGNBRK) {
  1136. port->uart.ignore_status_mask |= (1 << TTY_BREAK);
  1137. /*
  1138. * If we're ignoring parity and break indicators,
  1139. * ignore overruns to (for real raw support).
  1140. */
  1141. if (new->c_iflag & IGNPAR)
  1142. port->uart.ignore_status_mask |= (1 << TTY_OVERRUN);
  1143. }
  1144. /* Ignore all characters if CREAD is not set */
  1145. if ((new->c_cflag & CREAD) == 0)
  1146. port->uart.ignore_status_mask |= (1 << TTY_NORMAL);
  1147. scxctr |= *port->_control & (SC01CTR_TXE | SC01CTR_RXE | SC01CTR_BKE);
  1148. *port->_control = scxctr;
  1149. spin_unlock_irqrestore(&port->uart.lock, flags);
  1150. }
  1151. /*
  1152. * set the terminal I/O parameters
  1153. */
  1154. static void mn10300_serial_set_termios(struct uart_port *_port,
  1155. struct ktermios *new,
  1156. struct ktermios *old)
  1157. {
  1158. struct mn10300_serial_port *port =
  1159. container_of(_port, struct mn10300_serial_port, uart);
  1160. _enter("%s,%p,%p", port->name, new, old);
  1161. mn10300_serial_change_speed(port, new, old);
  1162. /* handle turning off CRTSCTS */
  1163. if (!(new->c_cflag & CRTSCTS)) {
  1164. u16 ctr = *port->_control;
  1165. ctr &= ~SC2CTR_TWE;
  1166. *port->_control = ctr;
  1167. }
  1168. /* change Transfer bit-order (LSB/MSB) */
  1169. if (new->c_cflag & CODMSB)
  1170. *port->_control |= SC01CTR_OD_MSBFIRST; /* MSB MODE */
  1171. else
  1172. *port->_control &= ~SC01CTR_OD_MSBFIRST; /* LSB MODE */
  1173. }
  1174. /*
  1175. * return description of port type
  1176. */
  1177. static const char *mn10300_serial_type(struct uart_port *_port)
  1178. {
  1179. struct mn10300_serial_port *port =
  1180. container_of(_port, struct mn10300_serial_port, uart);
  1181. if (port->uart.type == PORT_MN10300_CTS)
  1182. return "MN10300 SIF_CTS";
  1183. return "MN10300 SIF";
  1184. }
  1185. /*
  1186. * release I/O and memory regions in use by port
  1187. */
  1188. static void mn10300_serial_release_port(struct uart_port *_port)
  1189. {
  1190. struct mn10300_serial_port *port =
  1191. container_of(_port, struct mn10300_serial_port, uart);
  1192. _enter("%s", port->name);
  1193. release_mem_region((unsigned long) port->_iobase, 16);
  1194. }
  1195. /*
  1196. * request I/O and memory regions for port
  1197. */
  1198. static int mn10300_serial_request_port(struct uart_port *_port)
  1199. {
  1200. struct mn10300_serial_port *port =
  1201. container_of(_port, struct mn10300_serial_port, uart);
  1202. _enter("%s", port->name);
  1203. request_mem_region((unsigned long) port->_iobase, 16, port->name);
  1204. return 0;
  1205. }
  1206. /*
  1207. * configure the type and reserve the ports
  1208. */
  1209. static void mn10300_serial_config_port(struct uart_port *_port, int type)
  1210. {
  1211. struct mn10300_serial_port *port =
  1212. container_of(_port, struct mn10300_serial_port, uart);
  1213. _enter("%s", port->name);
  1214. port->uart.type = PORT_MN10300;
  1215. if (port->options & MNSCx_OPT_CTS)
  1216. port->uart.type = PORT_MN10300_CTS;
  1217. mn10300_serial_request_port(_port);
  1218. }
  1219. /*
  1220. * verify serial parameters are suitable for this port type
  1221. */
  1222. static int mn10300_serial_verify_port(struct uart_port *_port,
  1223. struct serial_struct *ss)
  1224. {
  1225. struct mn10300_serial_port *port =
  1226. container_of(_port, struct mn10300_serial_port, uart);
  1227. void *mapbase = (void *) (unsigned long) port->uart.mapbase;
  1228. _enter("%s", port->name);
  1229. /* these things may not be changed */
  1230. if (ss->irq != port->uart.irq ||
  1231. ss->port != port->uart.iobase ||
  1232. ss->io_type != port->uart.iotype ||
  1233. ss->iomem_base != mapbase ||
  1234. ss->iomem_reg_shift != port->uart.regshift ||
  1235. ss->hub6 != port->uart.hub6 ||
  1236. ss->xmit_fifo_size != port->uart.fifosize)
  1237. return -EINVAL;
  1238. /* type may be changed on a port that supports CTS */
  1239. if (ss->type != port->uart.type) {
  1240. if (!(port->options & MNSCx_OPT_CTS))
  1241. return -EINVAL;
  1242. if (ss->type != PORT_MN10300 &&
  1243. ss->type != PORT_MN10300_CTS)
  1244. return -EINVAL;
  1245. }
  1246. return 0;
  1247. }
  1248. /*
  1249. * initialise the MN10300 on-chip UARTs
  1250. */
  1251. static int __init mn10300_serial_init(void)
  1252. {
  1253. struct mn10300_serial_port *port;
  1254. int ret, i;
  1255. printk(KERN_INFO "%s version %s (%s)\n",
  1256. serial_name, serial_version, serial_revdate);
  1257. #if defined(CONFIG_MN10300_TTYSM2) && defined(CONFIG_AM33_2)
  1258. {
  1259. int tmp;
  1260. SC2TIM = 8; /* make the baud base of timer 2 IOCLK/8 */
  1261. tmp = SC2TIM;
  1262. }
  1263. #endif
  1264. set_intr_stub(NUM2EXCEP_IRQ_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL),
  1265. mn10300_serial_vdma_interrupt);
  1266. ret = uart_register_driver(&mn10300_serial_driver);
  1267. if (!ret) {
  1268. for (i = 0 ; i < NR_PORTS ; i++) {
  1269. port = mn10300_serial_ports[i];
  1270. if (!port || port->gdbstub)
  1271. continue;
  1272. switch (port->clock_src) {
  1273. case MNSCx_CLOCK_SRC_IOCLK:
  1274. port->ioclk = MN10300_IOCLK;
  1275. break;
  1276. #ifdef MN10300_IOBCLK
  1277. case MNSCx_CLOCK_SRC_IOBCLK:
  1278. port->ioclk = MN10300_IOBCLK;
  1279. break;
  1280. #endif
  1281. default:
  1282. BUG();
  1283. }
  1284. ret = uart_add_one_port(&mn10300_serial_driver,
  1285. &port->uart);
  1286. if (ret < 0) {
  1287. _debug("ERROR %d", -ret);
  1288. break;
  1289. }
  1290. }
  1291. if (ret)
  1292. uart_unregister_driver(&mn10300_serial_driver);
  1293. }
  1294. return ret;
  1295. }
  1296. __initcall(mn10300_serial_init);
  1297. #ifdef CONFIG_MN10300_TTYSM_CONSOLE
  1298. /*
  1299. * print a string to the serial port without disturbing the real user of the
  1300. * port too much
  1301. * - the console must be locked by the caller
  1302. */
  1303. static void mn10300_serial_console_write(struct console *co,
  1304. const char *s, unsigned count)
  1305. {
  1306. struct mn10300_serial_port *port;
  1307. unsigned i;
  1308. u16 scxctr, txicr, tmp;
  1309. u8 tmxmd;
  1310. port = mn10300_serial_ports[co->index];
  1311. /* firstly hijack the serial port from the "virtual DMA" controller */
  1312. arch_local_cli();
  1313. txicr = *port->tx_icr;
  1314. *port->tx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
  1315. tmp = *port->tx_icr;
  1316. arch_local_sti();
  1317. /* the transmitter may be disabled */
  1318. scxctr = *port->_control;
  1319. if (!(scxctr & SC01CTR_TXE)) {
  1320. /* restart the UART clock */
  1321. tmxmd = *port->_tmxmd;
  1322. switch (port->div_timer) {
  1323. case MNSCx_DIV_TIMER_16BIT:
  1324. *port->_tmxmd = 0;
  1325. *port->_tmxmd = TM8MD_INIT_COUNTER;
  1326. *port->_tmxmd = tmxmd | TM8MD_COUNT_ENABLE;
  1327. break;
  1328. case MNSCx_DIV_TIMER_8BIT:
  1329. *port->_tmxmd = 0;
  1330. *port->_tmxmd = TM2MD_INIT_COUNTER;
  1331. *port->_tmxmd = tmxmd | TM2MD_COUNT_ENABLE;
  1332. break;
  1333. }
  1334. /* enable the transmitter */
  1335. *port->_control = (scxctr & ~SC01CTR_BKE) | SC01CTR_TXE;
  1336. } else if (scxctr & SC01CTR_BKE) {
  1337. /* stop transmitting BREAK */
  1338. *port->_control = (scxctr & ~SC01CTR_BKE);
  1339. }
  1340. /* send the chars into the serial port (with LF -> LFCR conversion) */
  1341. for (i = 0; i < count; i++) {
  1342. char ch = *s++;
  1343. while (*port->_status & SC01STR_TBF)
  1344. continue;
  1345. *(u8 *) port->_txb = ch;
  1346. if (ch == 0x0a) {
  1347. while (*port->_status & SC01STR_TBF)
  1348. continue;
  1349. *(u8 *) port->_txb = 0xd;
  1350. }
  1351. }
  1352. /* can't let the transmitter be turned off if it's actually
  1353. * transmitting */
  1354. while (*port->_status & (SC01STR_TXF | SC01STR_TBF))
  1355. continue;
  1356. /* disable the transmitter if we re-enabled it */
  1357. if (!(scxctr & SC01CTR_TXE))
  1358. *port->_control = scxctr;
  1359. arch_local_cli();
  1360. *port->tx_icr = txicr;
  1361. tmp = *port->tx_icr;
  1362. arch_local_sti();
  1363. }
  1364. /*
  1365. * set up a serial port as a console
  1366. * - construct a cflag setting for the first rs_open()
  1367. * - initialize the serial port
  1368. * - return non-zero if we didn't find a serial port.
  1369. */
  1370. static int __init mn10300_serial_console_setup(struct console *co,
  1371. char *options)
  1372. {
  1373. struct mn10300_serial_port *port;
  1374. int i, parity = 'n', baud = 9600, bits = 8, flow = 0;
  1375. for (i = 0 ; i < NR_PORTS ; i++) {
  1376. port = mn10300_serial_ports[i];
  1377. if (port && !port->gdbstub && port->uart.line == co->index)
  1378. goto found_device;
  1379. }
  1380. return -ENODEV;
  1381. found_device:
  1382. switch (port->clock_src) {
  1383. case MNSCx_CLOCK_SRC_IOCLK:
  1384. port->ioclk = MN10300_IOCLK;
  1385. break;
  1386. #ifdef MN10300_IOBCLK
  1387. case MNSCx_CLOCK_SRC_IOBCLK:
  1388. port->ioclk = MN10300_IOBCLK;
  1389. break;
  1390. #endif
  1391. default:
  1392. BUG();
  1393. }
  1394. if (options)
  1395. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1396. return uart_set_options(&port->uart, co, baud, parity, bits, flow);
  1397. }
  1398. /*
  1399. * register console
  1400. */
  1401. static int __init mn10300_serial_console_init(void)
  1402. {
  1403. register_console(&mn10300_serial_console);
  1404. return 0;
  1405. }
  1406. console_initcall(mn10300_serial_console_init);
  1407. #endif
  1408. #ifdef CONFIG_CONSOLE_POLL
  1409. /*
  1410. * Polled character reception for the kernel debugger
  1411. */
  1412. static int mn10300_serial_poll_get_char(struct uart_port *_port)
  1413. {
  1414. struct mn10300_serial_port *port =
  1415. container_of(_port, struct mn10300_serial_port, uart);
  1416. unsigned ix;
  1417. u8 st, ch;
  1418. _enter("%s", port->name);
  1419. do {
  1420. /* pull chars out of the hat */
  1421. ix = port->rx_outp;
  1422. if (ix == port->rx_inp)
  1423. return NO_POLL_CHAR;
  1424. ch = port->rx_buffer[ix++];
  1425. st = port->rx_buffer[ix++];
  1426. smp_rmb();
  1427. port->rx_outp = ix & (MNSC_BUFFER_SIZE - 1);
  1428. } while (st & (SC01STR_FEF | SC01STR_PEF | SC01STR_OEF));
  1429. return ch;
  1430. }
  1431. /*
  1432. * Polled character transmission for the kernel debugger
  1433. */
  1434. static void mn10300_serial_poll_put_char(struct uart_port *_port,
  1435. unsigned char ch)
  1436. {
  1437. struct mn10300_serial_port *port =
  1438. container_of(_port, struct mn10300_serial_port, uart);
  1439. u8 intr, tmp;
  1440. /* wait for the transmitter to finish anything it might be doing (and
  1441. * this includes the virtual DMA handler, so it might take a while) */
  1442. while (*port->_status & (SC01STR_TBF | SC01STR_TXF))
  1443. continue;
  1444. /* disable the Tx ready interrupt */
  1445. intr = *port->_intr;
  1446. *port->_intr = intr & ~SC01ICR_TI;
  1447. tmp = *port->_intr;
  1448. if (ch == 0x0a) {
  1449. *(u8 *) port->_txb = 0x0d;
  1450. while (*port->_status & SC01STR_TBF)
  1451. continue;
  1452. }
  1453. *(u8 *) port->_txb = ch;
  1454. while (*port->_status & SC01STR_TBF)
  1455. continue;
  1456. /* restore the Tx interrupt flag */
  1457. *port->_intr = intr;
  1458. tmp = *port->_intr;
  1459. }
  1460. #endif /* CONFIG_CONSOLE_POLL */