mn10300-serial.c 40 KB

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