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 *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 */