mn10300-serial.c 40 KB

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