mn10300-serial.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  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 *port = &port->uart.state->port;
  484. struct tty_struct *tty = port->tty;
  485. unsigned ix;
  486. int count;
  487. u8 st, ch, push, status, overrun;
  488. _enter("%s", port->name);
  489. push = 0;
  490. count = CIRC_CNT(port->rx_inp, port->rx_outp, MNSC_BUFFER_SIZE);
  491. count = tty_buffer_request_room(port, count);
  492. if (count == 0) {
  493. if (!tty->low_latency)
  494. tty_flip_buffer_push(tty);
  495. return;
  496. }
  497. try_again:
  498. /* pull chars out of the hat */
  499. ix = ACCESS_ONCE(port->rx_outp);
  500. if (CIRC_CNT(port->rx_inp, ix, MNSC_BUFFER_SIZE) == 0) {
  501. if (push && !tty->low_latency)
  502. tty_flip_buffer_push(tty);
  503. return;
  504. }
  505. smp_read_barrier_depends();
  506. ch = port->rx_buffer[ix++];
  507. st = port->rx_buffer[ix++];
  508. smp_mb();
  509. port->rx_outp = ix & (MNSC_BUFFER_SIZE - 1);
  510. port->uart.icount.rx++;
  511. st &= SC01STR_FEF | SC01STR_PEF | SC01STR_OEF;
  512. status = 0;
  513. overrun = 0;
  514. /* the UART doesn't detect BREAK, so we have to do that ourselves
  515. * - it starts as a framing error on a NUL character
  516. * - then we count another two NUL characters before issuing TTY_BREAK
  517. * - then we end on a normal char or one that has all the bottom bits
  518. * zero and the top bits set
  519. */
  520. switch (port->rx_brk) {
  521. case 0:
  522. /* not breaking at the moment */
  523. break;
  524. case 1:
  525. if (st & SC01STR_FEF && ch == 0) {
  526. port->rx_brk = 2;
  527. goto try_again;
  528. }
  529. goto not_break;
  530. case 2:
  531. if (st & SC01STR_FEF && ch == 0) {
  532. port->rx_brk = 3;
  533. _proto("Rx Break Detected");
  534. icount->brk++;
  535. if (uart_handle_break(&port->uart))
  536. goto ignore_char;
  537. status |= 1 << TTY_BREAK;
  538. goto insert;
  539. }
  540. goto not_break;
  541. default:
  542. if (st & (SC01STR_FEF | SC01STR_PEF | SC01STR_OEF))
  543. goto try_again; /* still breaking */
  544. port->rx_brk = 0; /* end of the break */
  545. switch (ch) {
  546. case 0xFF:
  547. case 0xFE:
  548. case 0xFC:
  549. case 0xF8:
  550. case 0xF0:
  551. case 0xE0:
  552. case 0xC0:
  553. case 0x80:
  554. case 0x00:
  555. /* discard char at probable break end */
  556. goto try_again;
  557. }
  558. break;
  559. }
  560. process_errors:
  561. /* handle framing error */
  562. if (st & SC01STR_FEF) {
  563. if (ch == 0) {
  564. /* framing error with NUL char is probably a BREAK */
  565. port->rx_brk = 1;
  566. goto try_again;
  567. }
  568. _proto("Rx Framing Error");
  569. icount->frame++;
  570. status |= 1 << TTY_FRAME;
  571. }
  572. /* handle parity error */
  573. if (st & SC01STR_PEF) {
  574. _proto("Rx Parity Error");
  575. icount->parity++;
  576. status = TTY_PARITY;
  577. }
  578. /* handle normal char */
  579. if (status == 0) {
  580. if (uart_handle_sysrq_char(&port->uart, ch))
  581. goto ignore_char;
  582. status = (1 << TTY_NORMAL);
  583. }
  584. /* handle overrun error */
  585. if (st & SC01STR_OEF) {
  586. if (port->rx_brk)
  587. goto try_again;
  588. _proto("Rx Overrun Error");
  589. icount->overrun++;
  590. overrun = 1;
  591. }
  592. insert:
  593. status &= port->uart.read_status_mask;
  594. if (!overrun && !(status & port->uart.ignore_status_mask)) {
  595. int flag;
  596. if (status & (1 << TTY_BREAK))
  597. flag = TTY_BREAK;
  598. else if (status & (1 << TTY_PARITY))
  599. flag = TTY_PARITY;
  600. else if (status & (1 << TTY_FRAME))
  601. flag = TTY_FRAME;
  602. else
  603. flag = TTY_NORMAL;
  604. tty_insert_flip_char(port, ch, flag);
  605. }
  606. /* overrun is special, since it's reported immediately, and doesn't
  607. * affect the current character
  608. */
  609. if (overrun)
  610. tty_insert_flip_char(port, 0, TTY_OVERRUN);
  611. count--;
  612. if (count <= 0) {
  613. if (!tty->low_latency)
  614. tty_flip_buffer_push(tty);
  615. return;
  616. }
  617. ignore_char:
  618. push = 1;
  619. goto try_again;
  620. not_break:
  621. port->rx_brk = 0;
  622. goto process_errors;
  623. }
  624. /*
  625. * handle an interrupt from the serial transmission "virtual DMA" driver
  626. * - note: the interrupt routine will disable its own interrupts when the Tx
  627. * buffer is empty
  628. */
  629. static void mn10300_serial_transmit_interrupt(struct mn10300_serial_port *port)
  630. {
  631. _enter("%s", port->name);
  632. if (!port->uart.state || !port->uart.state->port.tty) {
  633. mn10300_serial_dis_tx_intr(port);
  634. return;
  635. }
  636. if (uart_tx_stopped(&port->uart) ||
  637. uart_circ_empty(&port->uart.state->xmit))
  638. mn10300_serial_dis_tx_intr(port);
  639. if (uart_circ_chars_pending(&port->uart.state->xmit) < WAKEUP_CHARS)
  640. uart_write_wakeup(&port->uart);
  641. }
  642. /*
  643. * deal with a change in the status of the CTS line
  644. */
  645. static void mn10300_serial_cts_changed(struct mn10300_serial_port *port, u8 st)
  646. {
  647. u16 ctr;
  648. port->tx_cts = st;
  649. port->uart.icount.cts++;
  650. /* flip the CTS state selector flag to interrupt when it changes
  651. * back */
  652. ctr = *port->_control;
  653. ctr ^= SC2CTR_TWS;
  654. *port->_control = ctr;
  655. uart_handle_cts_change(&port->uart, st & SC2STR_CTS);
  656. wake_up_interruptible(&port->uart.state->port.delta_msr_wait);
  657. }
  658. /*
  659. * handle a virtual interrupt generated by the lower level "virtual DMA"
  660. * routines (irq is the baud timer interrupt)
  661. */
  662. static irqreturn_t mn10300_serial_interrupt(int irq, void *dev_id)
  663. {
  664. struct mn10300_serial_port *port = dev_id;
  665. u8 st;
  666. spin_lock(&port->uart.lock);
  667. if (port->intr_flags) {
  668. _debug("INT %s: %x", port->name, port->intr_flags);
  669. if (mask_test_and_clear(&port->intr_flags, MNSCx_RX_AVAIL))
  670. mn10300_serial_receive_interrupt(port);
  671. if (mask_test_and_clear(&port->intr_flags,
  672. MNSCx_TX_SPACE | MNSCx_TX_EMPTY))
  673. mn10300_serial_transmit_interrupt(port);
  674. }
  675. /* the only modem control line amongst the whole lot is CTS on
  676. * serial port 2 */
  677. if (port->type == PORT_MN10300_CTS) {
  678. st = *port->_status;
  679. if ((port->tx_cts ^ st) & SC2STR_CTS)
  680. mn10300_serial_cts_changed(port, st);
  681. }
  682. spin_unlock(&port->uart.lock);
  683. return IRQ_HANDLED;
  684. }
  685. /*
  686. * return indication of whether the hardware transmit buffer is empty
  687. */
  688. static unsigned int mn10300_serial_tx_empty(struct uart_port *_port)
  689. {
  690. struct mn10300_serial_port *port =
  691. container_of(_port, struct mn10300_serial_port, uart);
  692. _enter("%s", port->name);
  693. return (*port->_status & (SC01STR_TXF | SC01STR_TBF)) ?
  694. 0 : TIOCSER_TEMT;
  695. }
  696. /*
  697. * set the modem control lines (we don't have any)
  698. */
  699. static void mn10300_serial_set_mctrl(struct uart_port *_port,
  700. unsigned int mctrl)
  701. {
  702. struct mn10300_serial_port *port __attribute__ ((unused)) =
  703. container_of(_port, struct mn10300_serial_port, uart);
  704. _enter("%s,%x", port->name, mctrl);
  705. }
  706. /*
  707. * get the modem control line statuses
  708. */
  709. static unsigned int mn10300_serial_get_mctrl(struct uart_port *_port)
  710. {
  711. struct mn10300_serial_port *port =
  712. container_of(_port, struct mn10300_serial_port, uart);
  713. _enter("%s", port->name);
  714. if (port->type == PORT_MN10300_CTS && !(*port->_status & SC2STR_CTS))
  715. return TIOCM_CAR | TIOCM_DSR;
  716. return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
  717. }
  718. /*
  719. * stop transmitting characters
  720. */
  721. static void mn10300_serial_stop_tx(struct uart_port *_port)
  722. {
  723. struct mn10300_serial_port *port =
  724. container_of(_port, struct mn10300_serial_port, uart);
  725. _enter("%s", port->name);
  726. /* disable the virtual DMA */
  727. mn10300_serial_dis_tx_intr(port);
  728. }
  729. /*
  730. * start transmitting characters
  731. * - jump-start transmission if it has stalled
  732. * - enable the serial Tx interrupt (used by the virtual DMA controller)
  733. * - force an interrupt to happen if necessary
  734. */
  735. static void mn10300_serial_start_tx(struct uart_port *_port)
  736. {
  737. struct mn10300_serial_port *port =
  738. container_of(_port, struct mn10300_serial_port, uart);
  739. _enter("%s{%lu}",
  740. port->name,
  741. CIRC_CNT(&port->uart.state->xmit.head,
  742. &port->uart.state->xmit.tail,
  743. UART_XMIT_SIZE));
  744. /* kick the virtual DMA controller */
  745. mn10300_serial_en_tx_intr(port);
  746. _debug("CTR=%04hx ICR=%02hx STR=%04x TMD=%02hx TBR=%04hx ICR=%04hx",
  747. *port->_control, *port->_intr, *port->_status,
  748. *port->_tmxmd,
  749. (port->div_timer == MNSCx_DIV_TIMER_8BIT) ?
  750. *(volatile u8 *)port->_tmxbr : *port->_tmxbr,
  751. *port->tx_icr);
  752. }
  753. /*
  754. * transmit a high-priority XON/XOFF character
  755. */
  756. static void mn10300_serial_send_xchar(struct uart_port *_port, char ch)
  757. {
  758. struct mn10300_serial_port *port =
  759. container_of(_port, struct mn10300_serial_port, uart);
  760. unsigned long flags;
  761. _enter("%s,%02x", port->name, ch);
  762. if (likely(port->gdbstub)) {
  763. port->tx_xchar = ch;
  764. if (ch) {
  765. spin_lock_irqsave(&port->uart.lock, flags);
  766. mn10300_serial_en_tx_intr(port);
  767. spin_unlock_irqrestore(&port->uart.lock, flags);
  768. }
  769. }
  770. }
  771. /*
  772. * stop receiving characters
  773. * - called whilst the port is being closed
  774. */
  775. static void mn10300_serial_stop_rx(struct uart_port *_port)
  776. {
  777. struct mn10300_serial_port *port =
  778. container_of(_port, struct mn10300_serial_port, uart);
  779. u16 ctr;
  780. _enter("%s", port->name);
  781. ctr = *port->_control;
  782. ctr &= ~SC01CTR_RXE;
  783. *port->_control = ctr;
  784. mn10300_serial_dis_rx_intr(port);
  785. }
  786. /*
  787. * enable modem status interrupts
  788. */
  789. static void mn10300_serial_enable_ms(struct uart_port *_port)
  790. {
  791. struct mn10300_serial_port *port =
  792. container_of(_port, struct mn10300_serial_port, uart);
  793. u16 ctr, cts;
  794. _enter("%s", port->name);
  795. if (port->type == PORT_MN10300_CTS) {
  796. /* want to interrupt when CTS goes low if CTS is now high and
  797. * vice versa
  798. */
  799. port->tx_cts = *port->_status;
  800. cts = (port->tx_cts & SC2STR_CTS) ?
  801. SC2CTR_TWE : SC2CTR_TWE | SC2CTR_TWS;
  802. ctr = *port->_control;
  803. ctr &= ~SC2CTR_TWS;
  804. ctr |= cts;
  805. *port->_control = ctr;
  806. mn10300_serial_en_tx_intr(port);
  807. }
  808. }
  809. /*
  810. * transmit or cease transmitting a break signal
  811. */
  812. static void mn10300_serial_break_ctl(struct uart_port *_port, int ctl)
  813. {
  814. struct mn10300_serial_port *port =
  815. container_of(_port, struct mn10300_serial_port, uart);
  816. unsigned long flags;
  817. _enter("%s,%d", port->name, ctl);
  818. spin_lock_irqsave(&port->uart.lock, flags);
  819. if (ctl) {
  820. /* tell the virtual DMA handler to assert BREAK */
  821. port->tx_flags |= MNSCx_TX_BREAK;
  822. mn10300_serial_en_tx_intr(port);
  823. } else {
  824. port->tx_flags &= ~MNSCx_TX_BREAK;
  825. *port->_control &= ~SC01CTR_BKE;
  826. mn10300_serial_en_tx_intr(port);
  827. }
  828. spin_unlock_irqrestore(&port->uart.lock, flags);
  829. }
  830. /*
  831. * grab the interrupts and enable the port for reception
  832. */
  833. static int mn10300_serial_startup(struct uart_port *_port)
  834. {
  835. struct mn10300_serial_port *port =
  836. container_of(_port, struct mn10300_serial_port, uart);
  837. struct mn10300_serial_int *pint;
  838. _enter("%s{%d}", port->name, port->gdbstub);
  839. if (unlikely(port->gdbstub))
  840. return -EBUSY;
  841. /* allocate an Rx buffer for the virtual DMA handler */
  842. port->rx_buffer = kmalloc(MNSC_BUFFER_SIZE, GFP_KERNEL);
  843. if (!port->rx_buffer)
  844. return -ENOMEM;
  845. port->rx_inp = port->rx_outp = 0;
  846. port->tx_flags = 0;
  847. /* finally, enable the device */
  848. *port->_intr = SC01ICR_TI;
  849. *port->_control |= SC01CTR_TXE | SC01CTR_RXE;
  850. pint = &mn10300_serial_int_tbl[port->rx_irq];
  851. pint->port = port;
  852. pint->vdma = mn10300_serial_vdma_rx_handler;
  853. pint = &mn10300_serial_int_tbl[port->tx_irq];
  854. pint->port = port;
  855. pint->vdma = mn10300_serial_vdma_tx_handler;
  856. irq_set_chip(port->rx_irq, &mn10300_serial_low_pic);
  857. irq_set_chip(port->tx_irq, &mn10300_serial_low_pic);
  858. irq_set_chip(port->tm_irq, &mn10300_serial_pic);
  859. if (request_irq(port->rx_irq, mn10300_serial_interrupt,
  860. IRQF_DISABLED | IRQF_NOBALANCING,
  861. port->rx_name, port) < 0)
  862. goto error;
  863. if (request_irq(port->tx_irq, mn10300_serial_interrupt,
  864. IRQF_DISABLED | IRQF_NOBALANCING,
  865. port->tx_name, port) < 0)
  866. goto error2;
  867. if (request_irq(port->tm_irq, mn10300_serial_interrupt,
  868. IRQF_DISABLED | IRQF_NOBALANCING,
  869. port->tm_name, port) < 0)
  870. goto error3;
  871. mn10300_serial_mask_ack(port->tm_irq);
  872. return 0;
  873. error3:
  874. free_irq(port->tx_irq, port);
  875. error2:
  876. free_irq(port->rx_irq, port);
  877. error:
  878. kfree(port->rx_buffer);
  879. port->rx_buffer = NULL;
  880. return -EBUSY;
  881. }
  882. /*
  883. * shutdown the port and release interrupts
  884. */
  885. static void mn10300_serial_shutdown(struct uart_port *_port)
  886. {
  887. unsigned long flags;
  888. u16 x;
  889. struct mn10300_serial_port *port =
  890. container_of(_port, struct mn10300_serial_port, uart);
  891. _enter("%s", port->name);
  892. spin_lock_irqsave(&_port->lock, flags);
  893. mn10300_serial_dis_tx_intr(port);
  894. *port->rx_icr = NUM2GxICR_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL);
  895. x = *port->rx_icr;
  896. port->tx_flags = 0;
  897. spin_unlock_irqrestore(&_port->lock, flags);
  898. /* disable the serial port and its baud rate timer */
  899. *port->_control &= ~(SC01CTR_TXE | SC01CTR_RXE | SC01CTR_BKE);
  900. *port->_tmxmd = 0;
  901. if (port->rx_buffer) {
  902. void *buf = port->rx_buffer;
  903. port->rx_buffer = NULL;
  904. kfree(buf);
  905. }
  906. /* disable all intrs */
  907. free_irq(port->tm_irq, port);
  908. free_irq(port->rx_irq, port);
  909. free_irq(port->tx_irq, port);
  910. mn10300_serial_int_tbl[port->tx_irq].port = NULL;
  911. mn10300_serial_int_tbl[port->rx_irq].port = NULL;
  912. }
  913. /*
  914. * this routine is called to set the UART divisor registers to match the
  915. * specified baud rate for a serial port.
  916. */
  917. static void mn10300_serial_change_speed(struct mn10300_serial_port *port,
  918. struct ktermios *new,
  919. struct ktermios *old)
  920. {
  921. unsigned long flags;
  922. unsigned long ioclk = port->ioclk;
  923. unsigned cflag;
  924. int baud, bits, xdiv, tmp;
  925. u16 tmxbr, scxctr;
  926. u8 tmxmd, battempt;
  927. u8 div_timer = port->div_timer;
  928. _enter("%s{%lu}", port->name, ioclk);
  929. /* byte size and parity */
  930. cflag = new->c_cflag;
  931. switch (cflag & CSIZE) {
  932. case CS7: scxctr = SC01CTR_CLN_7BIT; bits = 9; break;
  933. case CS8: scxctr = SC01CTR_CLN_8BIT; bits = 10; break;
  934. default: scxctr = SC01CTR_CLN_8BIT; bits = 10; break;
  935. }
  936. if (cflag & CSTOPB) {
  937. scxctr |= SC01CTR_STB_2BIT;
  938. bits++;
  939. }
  940. if (cflag & PARENB) {
  941. bits++;
  942. if (cflag & PARODD)
  943. scxctr |= SC01CTR_PB_ODD;
  944. #ifdef CMSPAR
  945. else if (cflag & CMSPAR)
  946. scxctr |= SC01CTR_PB_FIXED0;
  947. #endif
  948. else
  949. scxctr |= SC01CTR_PB_EVEN;
  950. }
  951. /* Determine divisor based on baud rate */
  952. battempt = 0;
  953. switch (port->uart.line) {
  954. #ifdef CONFIG_MN10300_TTYSM0
  955. case 0: /* ttySM0 */
  956. #if defined(CONFIG_MN10300_TTYSM0_TIMER8)
  957. scxctr |= SC0CTR_CK_TM8UFLOW_8;
  958. #elif defined(CONFIG_MN10300_TTYSM0_TIMER0)
  959. scxctr |= SC0CTR_CK_TM0UFLOW_8;
  960. #elif defined(CONFIG_MN10300_TTYSM0_TIMER2)
  961. scxctr |= SC0CTR_CK_TM2UFLOW_8;
  962. #else
  963. #error "Unknown config for ttySM0"
  964. #endif
  965. break;
  966. #endif /* CONFIG_MN10300_TTYSM0 */
  967. #ifdef CONFIG_MN10300_TTYSM1
  968. case 1: /* ttySM1 */
  969. #if defined(CONFIG_AM33_2) || defined(CONFIG_AM33_3)
  970. #if defined(CONFIG_MN10300_TTYSM1_TIMER9)
  971. scxctr |= SC1CTR_CK_TM9UFLOW_8;
  972. #elif defined(CONFIG_MN10300_TTYSM1_TIMER3)
  973. scxctr |= SC1CTR_CK_TM3UFLOW_8;
  974. #else
  975. #error "Unknown config for ttySM1"
  976. #endif
  977. #else /* CONFIG_AM33_2 || CONFIG_AM33_3 */
  978. #if defined(CONFIG_MN10300_TTYSM1_TIMER12)
  979. scxctr |= SC1CTR_CK_TM12UFLOW_8;
  980. #else
  981. #error "Unknown config for ttySM1"
  982. #endif
  983. #endif /* CONFIG_AM33_2 || CONFIG_AM33_3 */
  984. break;
  985. #endif /* CONFIG_MN10300_TTYSM1 */
  986. #ifdef CONFIG_MN10300_TTYSM2
  987. case 2: /* ttySM2 */
  988. #if defined(CONFIG_AM33_2)
  989. #if defined(CONFIG_MN10300_TTYSM2_TIMER10)
  990. scxctr |= SC2CTR_CK_TM10UFLOW;
  991. #else
  992. #error "Unknown config for ttySM2"
  993. #endif
  994. #else /* CONFIG_AM33_2 */
  995. #if defined(CONFIG_MN10300_TTYSM2_TIMER9)
  996. scxctr |= SC2CTR_CK_TM9UFLOW_8;
  997. #elif defined(CONFIG_MN10300_TTYSM2_TIMER1)
  998. scxctr |= SC2CTR_CK_TM1UFLOW_8;
  999. #elif defined(CONFIG_MN10300_TTYSM2_TIMER3)
  1000. scxctr |= SC2CTR_CK_TM3UFLOW_8;
  1001. #else
  1002. #error "Unknown config for ttySM2"
  1003. #endif
  1004. #endif /* CONFIG_AM33_2 */
  1005. break;
  1006. #endif /* CONFIG_MN10300_TTYSM2 */
  1007. default:
  1008. break;
  1009. }
  1010. try_alternative:
  1011. baud = uart_get_baud_rate(&port->uart, new, old, 0,
  1012. port->ioclk / 8);
  1013. _debug("ALT %d [baud %d]", battempt, baud);
  1014. if (!baud)
  1015. baud = 9600; /* B0 transition handled in rs_set_termios */
  1016. xdiv = 1;
  1017. if (baud == 134) {
  1018. baud = 269; /* 134 is really 134.5 */
  1019. xdiv = 2;
  1020. }
  1021. if (baud == 38400 &&
  1022. (port->uart.flags & UPF_SPD_MASK) == UPF_SPD_CUST
  1023. ) {
  1024. _debug("CUSTOM %u", port->uart.custom_divisor);
  1025. if (div_timer == MNSCx_DIV_TIMER_16BIT) {
  1026. if (port->uart.custom_divisor <= 65535) {
  1027. tmxmd = TM8MD_SRC_IOCLK;
  1028. tmxbr = port->uart.custom_divisor;
  1029. port->uart.uartclk = ioclk;
  1030. goto timer_okay;
  1031. }
  1032. if (port->uart.custom_divisor / 8 <= 65535) {
  1033. tmxmd = TM8MD_SRC_IOCLK_8;
  1034. tmxbr = port->uart.custom_divisor / 8;
  1035. port->uart.custom_divisor = tmxbr * 8;
  1036. port->uart.uartclk = ioclk / 8;
  1037. goto timer_okay;
  1038. }
  1039. if (port->uart.custom_divisor / 32 <= 65535) {
  1040. tmxmd = TM8MD_SRC_IOCLK_32;
  1041. tmxbr = port->uart.custom_divisor / 32;
  1042. port->uart.custom_divisor = tmxbr * 32;
  1043. port->uart.uartclk = ioclk / 32;
  1044. goto timer_okay;
  1045. }
  1046. } else if (div_timer == MNSCx_DIV_TIMER_8BIT) {
  1047. if (port->uart.custom_divisor <= 255) {
  1048. tmxmd = TM2MD_SRC_IOCLK;
  1049. tmxbr = port->uart.custom_divisor;
  1050. port->uart.uartclk = ioclk;
  1051. goto timer_okay;
  1052. }
  1053. if (port->uart.custom_divisor / 8 <= 255) {
  1054. tmxmd = TM2MD_SRC_IOCLK_8;
  1055. tmxbr = port->uart.custom_divisor / 8;
  1056. port->uart.custom_divisor = tmxbr * 8;
  1057. port->uart.uartclk = ioclk / 8;
  1058. goto timer_okay;
  1059. }
  1060. if (port->uart.custom_divisor / 32 <= 255) {
  1061. tmxmd = TM2MD_SRC_IOCLK_32;
  1062. tmxbr = port->uart.custom_divisor / 32;
  1063. port->uart.custom_divisor = tmxbr * 32;
  1064. port->uart.uartclk = ioclk / 32;
  1065. goto timer_okay;
  1066. }
  1067. }
  1068. }
  1069. switch (div_timer) {
  1070. case MNSCx_DIV_TIMER_16BIT:
  1071. port->uart.uartclk = ioclk;
  1072. tmxmd = TM8MD_SRC_IOCLK;
  1073. tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
  1074. if (tmp > 0 && tmp <= 65535)
  1075. goto timer_okay;
  1076. port->uart.uartclk = ioclk / 8;
  1077. tmxmd = TM8MD_SRC_IOCLK_8;
  1078. tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
  1079. if (tmp > 0 && tmp <= 65535)
  1080. goto timer_okay;
  1081. port->uart.uartclk = ioclk / 32;
  1082. tmxmd = TM8MD_SRC_IOCLK_32;
  1083. tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
  1084. if (tmp > 0 && tmp <= 65535)
  1085. goto timer_okay;
  1086. break;
  1087. case MNSCx_DIV_TIMER_8BIT:
  1088. port->uart.uartclk = ioclk;
  1089. tmxmd = TM2MD_SRC_IOCLK;
  1090. tmxbr = tmp = (ioclk / (baud * xdiv) + 4) / 8 - 1;
  1091. if (tmp > 0 && tmp <= 255)
  1092. goto timer_okay;
  1093. port->uart.uartclk = ioclk / 8;
  1094. tmxmd = TM2MD_SRC_IOCLK_8;
  1095. tmxbr = tmp = (ioclk / (baud * 8 * xdiv) + 4) / 8 - 1;
  1096. if (tmp > 0 && tmp <= 255)
  1097. goto timer_okay;
  1098. port->uart.uartclk = ioclk / 32;
  1099. tmxmd = TM2MD_SRC_IOCLK_32;
  1100. tmxbr = tmp = (ioclk / (baud * 32 * xdiv) + 4) / 8 - 1;
  1101. if (tmp > 0 && tmp <= 255)
  1102. goto timer_okay;
  1103. break;
  1104. default:
  1105. BUG();
  1106. return;
  1107. }
  1108. /* refuse to change to a baud rate we can't support */
  1109. _debug("CAN'T SUPPORT");
  1110. switch (battempt) {
  1111. case 0:
  1112. if (old) {
  1113. new->c_cflag &= ~CBAUD;
  1114. new->c_cflag |= (old->c_cflag & CBAUD);
  1115. battempt = 1;
  1116. goto try_alternative;
  1117. }
  1118. case 1:
  1119. /* as a last resort, if the quotient is zero, default to 9600
  1120. * bps */
  1121. new->c_cflag &= ~CBAUD;
  1122. new->c_cflag |= B9600;
  1123. battempt = 2;
  1124. goto try_alternative;
  1125. default:
  1126. /* hmmm... can't seem to support 9600 either
  1127. * - we could try iterating through the speeds we know about to
  1128. * find the lowest
  1129. */
  1130. new->c_cflag &= ~CBAUD;
  1131. new->c_cflag |= B0;
  1132. if (div_timer == MNSCx_DIV_TIMER_16BIT)
  1133. tmxmd = TM8MD_SRC_IOCLK_32;
  1134. else if (div_timer == MNSCx_DIV_TIMER_8BIT)
  1135. tmxmd = TM2MD_SRC_IOCLK_32;
  1136. tmxbr = 1;
  1137. port->uart.uartclk = ioclk / 32;
  1138. break;
  1139. }
  1140. timer_okay:
  1141. _debug("UARTCLK: %u / %hu", port->uart.uartclk, tmxbr);
  1142. /* make the changes */
  1143. spin_lock_irqsave(&port->uart.lock, flags);
  1144. uart_update_timeout(&port->uart, new->c_cflag, baud);
  1145. /* set the timer to produce the required baud rate */
  1146. switch (div_timer) {
  1147. case MNSCx_DIV_TIMER_16BIT:
  1148. *port->_tmxmd = 0;
  1149. *port->_tmxbr = tmxbr;
  1150. *port->_tmxmd = TM8MD_INIT_COUNTER;
  1151. *port->_tmxmd = tmxmd | TM8MD_COUNT_ENABLE;
  1152. break;
  1153. case MNSCx_DIV_TIMER_8BIT:
  1154. *port->_tmxmd = 0;
  1155. *(volatile u8 *) port->_tmxbr = (u8) tmxbr;
  1156. *port->_tmxmd = TM2MD_INIT_COUNTER;
  1157. *port->_tmxmd = tmxmd | TM2MD_COUNT_ENABLE;
  1158. break;
  1159. }
  1160. /* CTS flow control flag and modem status interrupts */
  1161. scxctr &= ~(SC2CTR_TWE | SC2CTR_TWS);
  1162. if (port->type == PORT_MN10300_CTS && cflag & CRTSCTS) {
  1163. /* want to interrupt when CTS goes low if CTS is now
  1164. * high and vice versa
  1165. */
  1166. port->tx_cts = *port->_status;
  1167. if (port->tx_cts & SC2STR_CTS)
  1168. scxctr |= SC2CTR_TWE;
  1169. else
  1170. scxctr |= SC2CTR_TWE | SC2CTR_TWS;
  1171. }
  1172. /* set up parity check flag */
  1173. port->uart.read_status_mask = (1 << TTY_NORMAL) | (1 << TTY_OVERRUN);
  1174. if (new->c_iflag & INPCK)
  1175. port->uart.read_status_mask |=
  1176. (1 << TTY_PARITY) | (1 << TTY_FRAME);
  1177. if (new->c_iflag & (BRKINT | PARMRK))
  1178. port->uart.read_status_mask |= (1 << TTY_BREAK);
  1179. /* characters to ignore */
  1180. port->uart.ignore_status_mask = 0;
  1181. if (new->c_iflag & IGNPAR)
  1182. port->uart.ignore_status_mask |=
  1183. (1 << TTY_PARITY) | (1 << TTY_FRAME);
  1184. if (new->c_iflag & IGNBRK) {
  1185. port->uart.ignore_status_mask |= (1 << TTY_BREAK);
  1186. /*
  1187. * If we're ignoring parity and break indicators,
  1188. * ignore overruns to (for real raw support).
  1189. */
  1190. if (new->c_iflag & IGNPAR)
  1191. port->uart.ignore_status_mask |= (1 << TTY_OVERRUN);
  1192. }
  1193. /* Ignore all characters if CREAD is not set */
  1194. if ((new->c_cflag & CREAD) == 0)
  1195. port->uart.ignore_status_mask |= (1 << TTY_NORMAL);
  1196. scxctr |= SC01CTR_TXE | SC01CTR_RXE;
  1197. scxctr |= *port->_control & SC01CTR_BKE;
  1198. *port->_control = scxctr;
  1199. spin_unlock_irqrestore(&port->uart.lock, flags);
  1200. }
  1201. /*
  1202. * set the terminal I/O parameters
  1203. */
  1204. static void mn10300_serial_set_termios(struct uart_port *_port,
  1205. struct ktermios *new,
  1206. struct ktermios *old)
  1207. {
  1208. struct mn10300_serial_port *port =
  1209. container_of(_port, struct mn10300_serial_port, uart);
  1210. _enter("%s,%p,%p", port->name, new, old);
  1211. mn10300_serial_change_speed(port, new, old);
  1212. /* handle turning off CRTSCTS */
  1213. if (!(new->c_cflag & CRTSCTS)) {
  1214. u16 ctr = *port->_control;
  1215. ctr &= ~SC2CTR_TWE;
  1216. *port->_control = ctr;
  1217. }
  1218. /* change Transfer bit-order (LSB/MSB) */
  1219. if (new->c_cflag & CODMSB)
  1220. *port->_control |= SC01CTR_OD_MSBFIRST; /* MSB MODE */
  1221. else
  1222. *port->_control &= ~SC01CTR_OD_MSBFIRST; /* LSB MODE */
  1223. }
  1224. /*
  1225. * return description of port type
  1226. */
  1227. static const char *mn10300_serial_type(struct uart_port *_port)
  1228. {
  1229. struct mn10300_serial_port *port =
  1230. container_of(_port, struct mn10300_serial_port, uart);
  1231. if (port->uart.type == PORT_MN10300_CTS)
  1232. return "MN10300 SIF_CTS";
  1233. return "MN10300 SIF";
  1234. }
  1235. /*
  1236. * release I/O and memory regions in use by port
  1237. */
  1238. static void mn10300_serial_release_port(struct uart_port *_port)
  1239. {
  1240. struct mn10300_serial_port *port =
  1241. container_of(_port, struct mn10300_serial_port, uart);
  1242. _enter("%s", port->name);
  1243. release_mem_region((unsigned long) port->_iobase, 16);
  1244. }
  1245. /*
  1246. * request I/O and memory regions for port
  1247. */
  1248. static int mn10300_serial_request_port(struct uart_port *_port)
  1249. {
  1250. struct mn10300_serial_port *port =
  1251. container_of(_port, struct mn10300_serial_port, uart);
  1252. _enter("%s", port->name);
  1253. request_mem_region((unsigned long) port->_iobase, 16, port->name);
  1254. return 0;
  1255. }
  1256. /*
  1257. * configure the type and reserve the ports
  1258. */
  1259. static void mn10300_serial_config_port(struct uart_port *_port, int type)
  1260. {
  1261. struct mn10300_serial_port *port =
  1262. container_of(_port, struct mn10300_serial_port, uart);
  1263. _enter("%s", port->name);
  1264. port->uart.type = PORT_MN10300;
  1265. if (port->options & MNSCx_OPT_CTS)
  1266. port->uart.type = PORT_MN10300_CTS;
  1267. mn10300_serial_request_port(_port);
  1268. }
  1269. /*
  1270. * verify serial parameters are suitable for this port type
  1271. */
  1272. static int mn10300_serial_verify_port(struct uart_port *_port,
  1273. struct serial_struct *ss)
  1274. {
  1275. struct mn10300_serial_port *port =
  1276. container_of(_port, struct mn10300_serial_port, uart);
  1277. void *mapbase = (void *) (unsigned long) port->uart.mapbase;
  1278. _enter("%s", port->name);
  1279. /* these things may not be changed */
  1280. if (ss->irq != port->uart.irq ||
  1281. ss->port != port->uart.iobase ||
  1282. ss->io_type != port->uart.iotype ||
  1283. ss->iomem_base != mapbase ||
  1284. ss->iomem_reg_shift != port->uart.regshift ||
  1285. ss->hub6 != port->uart.hub6 ||
  1286. ss->xmit_fifo_size != port->uart.fifosize)
  1287. return -EINVAL;
  1288. /* type may be changed on a port that supports CTS */
  1289. if (ss->type != port->uart.type) {
  1290. if (!(port->options & MNSCx_OPT_CTS))
  1291. return -EINVAL;
  1292. if (ss->type != PORT_MN10300 &&
  1293. ss->type != PORT_MN10300_CTS)
  1294. return -EINVAL;
  1295. }
  1296. return 0;
  1297. }
  1298. /*
  1299. * initialise the MN10300 on-chip UARTs
  1300. */
  1301. static int __init mn10300_serial_init(void)
  1302. {
  1303. struct mn10300_serial_port *port;
  1304. int ret, i;
  1305. printk(KERN_INFO "%s version %s (%s)\n",
  1306. serial_name, serial_version, serial_revdate);
  1307. #if defined(CONFIG_MN10300_TTYSM2) && defined(CONFIG_AM33_2)
  1308. {
  1309. int tmp;
  1310. SC2TIM = 8; /* make the baud base of timer 2 IOCLK/8 */
  1311. tmp = SC2TIM;
  1312. }
  1313. #endif
  1314. set_intr_stub(NUM2EXCEP_IRQ_LEVEL(CONFIG_MN10300_SERIAL_IRQ_LEVEL),
  1315. mn10300_serial_vdma_interrupt);
  1316. ret = uart_register_driver(&mn10300_serial_driver);
  1317. if (!ret) {
  1318. for (i = 0 ; i < NR_PORTS ; i++) {
  1319. port = mn10300_serial_ports[i];
  1320. if (!port || port->gdbstub)
  1321. continue;
  1322. switch (port->clock_src) {
  1323. case MNSCx_CLOCK_SRC_IOCLK:
  1324. port->ioclk = MN10300_IOCLK;
  1325. break;
  1326. #ifdef MN10300_IOBCLK
  1327. case MNSCx_CLOCK_SRC_IOBCLK:
  1328. port->ioclk = MN10300_IOBCLK;
  1329. break;
  1330. #endif
  1331. default:
  1332. BUG();
  1333. }
  1334. ret = uart_add_one_port(&mn10300_serial_driver,
  1335. &port->uart);
  1336. if (ret < 0) {
  1337. _debug("ERROR %d", -ret);
  1338. break;
  1339. }
  1340. }
  1341. if (ret)
  1342. uart_unregister_driver(&mn10300_serial_driver);
  1343. }
  1344. return ret;
  1345. }
  1346. __initcall(mn10300_serial_init);
  1347. #ifdef CONFIG_MN10300_TTYSM_CONSOLE
  1348. /*
  1349. * print a string to the serial port without disturbing the real user of the
  1350. * port too much
  1351. * - the console must be locked by the caller
  1352. */
  1353. static void mn10300_serial_console_write(struct console *co,
  1354. const char *s, unsigned count)
  1355. {
  1356. struct mn10300_serial_port *port;
  1357. unsigned i;
  1358. u16 scxctr;
  1359. u8 tmxmd;
  1360. unsigned long flags;
  1361. int locked = 1;
  1362. port = mn10300_serial_ports[co->index];
  1363. local_irq_save(flags);
  1364. if (port->uart.sysrq) {
  1365. /* mn10300_serial_interrupt() already took the lock */
  1366. locked = 0;
  1367. } else if (oops_in_progress) {
  1368. locked = spin_trylock(&port->uart.lock);
  1369. } else
  1370. spin_lock(&port->uart.lock);
  1371. /* firstly hijack the serial port from the "virtual DMA" controller */
  1372. mn10300_serial_dis_tx_intr(port);
  1373. /* the transmitter may be disabled */
  1374. scxctr = *port->_control;
  1375. if (!(scxctr & SC01CTR_TXE)) {
  1376. /* restart the UART clock */
  1377. tmxmd = *port->_tmxmd;
  1378. switch (port->div_timer) {
  1379. case MNSCx_DIV_TIMER_16BIT:
  1380. *port->_tmxmd = 0;
  1381. *port->_tmxmd = TM8MD_INIT_COUNTER;
  1382. *port->_tmxmd = tmxmd | TM8MD_COUNT_ENABLE;
  1383. break;
  1384. case MNSCx_DIV_TIMER_8BIT:
  1385. *port->_tmxmd = 0;
  1386. *port->_tmxmd = TM2MD_INIT_COUNTER;
  1387. *port->_tmxmd = tmxmd | TM2MD_COUNT_ENABLE;
  1388. break;
  1389. }
  1390. /* enable the transmitter */
  1391. *port->_control = (scxctr & ~SC01CTR_BKE) | SC01CTR_TXE;
  1392. } else if (scxctr & SC01CTR_BKE) {
  1393. /* stop transmitting BREAK */
  1394. *port->_control = (scxctr & ~SC01CTR_BKE);
  1395. }
  1396. /* send the chars into the serial port (with LF -> LFCR conversion) */
  1397. for (i = 0; i < count; i++) {
  1398. char ch = *s++;
  1399. while (*port->_status & SC01STR_TBF)
  1400. continue;
  1401. *port->_txb = ch;
  1402. if (ch == 0x0a) {
  1403. while (*port->_status & SC01STR_TBF)
  1404. continue;
  1405. *port->_txb = 0xd;
  1406. }
  1407. }
  1408. /* can't let the transmitter be turned off if it's actually
  1409. * transmitting */
  1410. while (*port->_status & (SC01STR_TXF | SC01STR_TBF))
  1411. continue;
  1412. /* disable the transmitter if we re-enabled it */
  1413. if (!(scxctr & SC01CTR_TXE))
  1414. *port->_control = scxctr;
  1415. mn10300_serial_en_tx_intr(port);
  1416. if (locked)
  1417. spin_unlock(&port->uart.lock);
  1418. local_irq_restore(flags);
  1419. }
  1420. /*
  1421. * set up a serial port as a console
  1422. * - construct a cflag setting for the first rs_open()
  1423. * - initialize the serial port
  1424. * - return non-zero if we didn't find a serial port.
  1425. */
  1426. static int __init mn10300_serial_console_setup(struct console *co,
  1427. char *options)
  1428. {
  1429. struct mn10300_serial_port *port;
  1430. int i, parity = 'n', baud = 9600, bits = 8, flow = 0;
  1431. for (i = 0 ; i < NR_PORTS ; i++) {
  1432. port = mn10300_serial_ports[i];
  1433. if (port && !port->gdbstub && port->uart.line == co->index)
  1434. goto found_device;
  1435. }
  1436. return -ENODEV;
  1437. found_device:
  1438. switch (port->clock_src) {
  1439. case MNSCx_CLOCK_SRC_IOCLK:
  1440. port->ioclk = MN10300_IOCLK;
  1441. break;
  1442. #ifdef MN10300_IOBCLK
  1443. case MNSCx_CLOCK_SRC_IOBCLK:
  1444. port->ioclk = MN10300_IOBCLK;
  1445. break;
  1446. #endif
  1447. default:
  1448. BUG();
  1449. }
  1450. if (options)
  1451. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1452. return uart_set_options(&port->uart, co, baud, parity, bits, flow);
  1453. }
  1454. /*
  1455. * register console
  1456. */
  1457. static int __init mn10300_serial_console_init(void)
  1458. {
  1459. register_console(&mn10300_serial_console);
  1460. return 0;
  1461. }
  1462. console_initcall(mn10300_serial_console_init);
  1463. #endif
  1464. #ifdef CONFIG_CONSOLE_POLL
  1465. /*
  1466. * Polled character reception for the kernel debugger
  1467. */
  1468. static int mn10300_serial_poll_get_char(struct uart_port *_port)
  1469. {
  1470. struct mn10300_serial_port *port =
  1471. container_of(_port, struct mn10300_serial_port, uart);
  1472. unsigned ix;
  1473. u8 st, ch;
  1474. _enter("%s", port->name);
  1475. if (mn10300_serial_int_tbl[port->rx_irq].port != NULL) {
  1476. do {
  1477. /* pull chars out of the hat */
  1478. ix = ACCESS_ONCE(port->rx_outp);
  1479. if (CIRC_CNT(port->rx_inp, ix, MNSC_BUFFER_SIZE) == 0)
  1480. return NO_POLL_CHAR;
  1481. smp_read_barrier_depends();
  1482. ch = port->rx_buffer[ix++];
  1483. st = port->rx_buffer[ix++];
  1484. smp_mb();
  1485. port->rx_outp = ix & (MNSC_BUFFER_SIZE - 1);
  1486. } while (st & (SC01STR_FEF | SC01STR_PEF | SC01STR_OEF));
  1487. } else {
  1488. do {
  1489. st = *port->_status;
  1490. if (st & (SC01STR_FEF | SC01STR_PEF | SC01STR_OEF))
  1491. continue;
  1492. } while (!(st & SC01STR_RBF));
  1493. ch = *port->_rxb;
  1494. }
  1495. return ch;
  1496. }
  1497. /*
  1498. * Polled character transmission for the kernel debugger
  1499. */
  1500. static void mn10300_serial_poll_put_char(struct uart_port *_port,
  1501. unsigned char ch)
  1502. {
  1503. struct mn10300_serial_port *port =
  1504. container_of(_port, struct mn10300_serial_port, uart);
  1505. u8 intr, tmp;
  1506. /* wait for the transmitter to finish anything it might be doing (and
  1507. * this includes the virtual DMA handler, so it might take a while) */
  1508. while (*port->_status & (SC01STR_TBF | SC01STR_TXF))
  1509. continue;
  1510. /* disable the Tx ready interrupt */
  1511. intr = *port->_intr;
  1512. *port->_intr = intr & ~SC01ICR_TI;
  1513. tmp = *port->_intr;
  1514. if (ch == 0x0a) {
  1515. *port->_txb = 0x0d;
  1516. while (*port->_status & SC01STR_TBF)
  1517. continue;
  1518. }
  1519. *port->_txb = ch;
  1520. while (*port->_status & SC01STR_TBF)
  1521. continue;
  1522. /* restore the Tx interrupt flag */
  1523. *port->_intr = intr;
  1524. tmp = *port->_intr;
  1525. }
  1526. #endif /* CONFIG_CONSOLE_POLL */