mn10300-serial.c 44 KB

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