mn10300-serial.c 42 KB

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