rp2.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. * Driver for Comtrol RocketPort EXPRESS/INFINITY cards
  3. *
  4. * Copyright (C) 2012 Kevin Cernekee <cernekee@gmail.com>
  5. *
  6. * Inspired by, and loosely based on:
  7. *
  8. * ar933x_uart.c
  9. * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
  10. *
  11. * rocketport_infinity_express-linux-1.20.tar.gz
  12. * Copyright (C) 2004-2011 Comtrol, Inc.
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License version 2 as published
  16. * by the Free Software Foundation.
  17. */
  18. #include <linux/bitops.h>
  19. #include <linux/compiler.h>
  20. #include <linux/completion.h>
  21. #include <linux/console.h>
  22. #include <linux/delay.h>
  23. #include <linux/firmware.h>
  24. #include <linux/init.h>
  25. #include <linux/io.h>
  26. #include <linux/ioport.h>
  27. #include <linux/irq.h>
  28. #include <linux/kernel.h>
  29. #include <linux/log2.h>
  30. #include <linux/module.h>
  31. #include <linux/pci.h>
  32. #include <linux/serial.h>
  33. #include <linux/serial_core.h>
  34. #include <linux/slab.h>
  35. #include <linux/sysrq.h>
  36. #include <linux/tty.h>
  37. #include <linux/tty_flip.h>
  38. #include <linux/types.h>
  39. #define DRV_NAME "rp2"
  40. #define RP2_FW_NAME "/*(DEBLOBBED)*/"
  41. #define RP2_UCODE_BYTES 0x3f
  42. #define PORTS_PER_ASIC 16
  43. #define ALL_PORTS_MASK (BIT(PORTS_PER_ASIC) - 1)
  44. #define UART_CLOCK 44236800
  45. #define DEFAULT_BAUD_DIV (UART_CLOCK / (9600 * 16))
  46. #define FIFO_SIZE 512
  47. /* BAR0 registers */
  48. #define RP2_FPGA_CTL0 0x110
  49. #define RP2_FPGA_CTL1 0x11c
  50. #define RP2_IRQ_MASK 0x1ec
  51. #define RP2_IRQ_MASK_EN_m BIT(0)
  52. #define RP2_IRQ_STATUS 0x1f0
  53. /* BAR1 registers */
  54. #define RP2_ASIC_SPACING 0x1000
  55. #define RP2_ASIC_OFFSET(i) ((i) << ilog2(RP2_ASIC_SPACING))
  56. #define RP2_PORT_BASE 0x000
  57. #define RP2_PORT_SPACING 0x040
  58. #define RP2_UCODE_BASE 0x400
  59. #define RP2_UCODE_SPACING 0x80
  60. #define RP2_CLK_PRESCALER 0xc00
  61. #define RP2_CH_IRQ_STAT 0xc04
  62. #define RP2_CH_IRQ_MASK 0xc08
  63. #define RP2_ASIC_IRQ 0xd00
  64. #define RP2_ASIC_IRQ_EN_m BIT(20)
  65. #define RP2_GLOBAL_CMD 0xd0c
  66. #define RP2_ASIC_CFG 0xd04
  67. /* port registers */
  68. #define RP2_DATA_DWORD 0x000
  69. #define RP2_DATA_BYTE 0x008
  70. #define RP2_DATA_BYTE_ERR_PARITY_m BIT(8)
  71. #define RP2_DATA_BYTE_ERR_OVERRUN_m BIT(9)
  72. #define RP2_DATA_BYTE_ERR_FRAMING_m BIT(10)
  73. #define RP2_DATA_BYTE_BREAK_m BIT(11)
  74. /* This lets uart_insert_char() drop bytes received on a !CREAD port */
  75. #define RP2_DUMMY_READ BIT(16)
  76. #define RP2_DATA_BYTE_EXCEPTION_MASK (RP2_DATA_BYTE_ERR_PARITY_m | \
  77. RP2_DATA_BYTE_ERR_OVERRUN_m | \
  78. RP2_DATA_BYTE_ERR_FRAMING_m | \
  79. RP2_DATA_BYTE_BREAK_m)
  80. #define RP2_RX_FIFO_COUNT 0x00c
  81. #define RP2_TX_FIFO_COUNT 0x00e
  82. #define RP2_CHAN_STAT 0x010
  83. #define RP2_CHAN_STAT_RXDATA_m BIT(0)
  84. #define RP2_CHAN_STAT_DCD_m BIT(3)
  85. #define RP2_CHAN_STAT_DSR_m BIT(4)
  86. #define RP2_CHAN_STAT_CTS_m BIT(5)
  87. #define RP2_CHAN_STAT_RI_m BIT(6)
  88. #define RP2_CHAN_STAT_OVERRUN_m BIT(13)
  89. #define RP2_CHAN_STAT_DSR_CHANGED_m BIT(16)
  90. #define RP2_CHAN_STAT_CTS_CHANGED_m BIT(17)
  91. #define RP2_CHAN_STAT_CD_CHANGED_m BIT(18)
  92. #define RP2_CHAN_STAT_RI_CHANGED_m BIT(22)
  93. #define RP2_CHAN_STAT_TXEMPTY_m BIT(25)
  94. #define RP2_CHAN_STAT_MS_CHANGED_MASK (RP2_CHAN_STAT_DSR_CHANGED_m | \
  95. RP2_CHAN_STAT_CTS_CHANGED_m | \
  96. RP2_CHAN_STAT_CD_CHANGED_m | \
  97. RP2_CHAN_STAT_RI_CHANGED_m)
  98. #define RP2_TXRX_CTL 0x014
  99. #define RP2_TXRX_CTL_MSRIRQ_m BIT(0)
  100. #define RP2_TXRX_CTL_RXIRQ_m BIT(2)
  101. #define RP2_TXRX_CTL_RX_TRIG_s 3
  102. #define RP2_TXRX_CTL_RX_TRIG_m (0x3 << RP2_TXRX_CTL_RX_TRIG_s)
  103. #define RP2_TXRX_CTL_RX_TRIG_1 (0x1 << RP2_TXRX_CTL_RX_TRIG_s)
  104. #define RP2_TXRX_CTL_RX_TRIG_256 (0x2 << RP2_TXRX_CTL_RX_TRIG_s)
  105. #define RP2_TXRX_CTL_RX_TRIG_448 (0x3 << RP2_TXRX_CTL_RX_TRIG_s)
  106. #define RP2_TXRX_CTL_RX_EN_m BIT(5)
  107. #define RP2_TXRX_CTL_RTSFLOW_m BIT(6)
  108. #define RP2_TXRX_CTL_DTRFLOW_m BIT(7)
  109. #define RP2_TXRX_CTL_TX_TRIG_s 16
  110. #define RP2_TXRX_CTL_TX_TRIG_m (0x3 << RP2_TXRX_CTL_RX_TRIG_s)
  111. #define RP2_TXRX_CTL_DSRFLOW_m BIT(18)
  112. #define RP2_TXRX_CTL_TXIRQ_m BIT(19)
  113. #define RP2_TXRX_CTL_CTSFLOW_m BIT(23)
  114. #define RP2_TXRX_CTL_TX_EN_m BIT(24)
  115. #define RP2_TXRX_CTL_RTS_m BIT(25)
  116. #define RP2_TXRX_CTL_DTR_m BIT(26)
  117. #define RP2_TXRX_CTL_LOOP_m BIT(27)
  118. #define RP2_TXRX_CTL_BREAK_m BIT(28)
  119. #define RP2_TXRX_CTL_CMSPAR_m BIT(29)
  120. #define RP2_TXRX_CTL_nPARODD_m BIT(30)
  121. #define RP2_TXRX_CTL_PARENB_m BIT(31)
  122. #define RP2_UART_CTL 0x018
  123. #define RP2_UART_CTL_MODE_s 0
  124. #define RP2_UART_CTL_MODE_m (0x7 << RP2_UART_CTL_MODE_s)
  125. #define RP2_UART_CTL_MODE_rs232 (0x1 << RP2_UART_CTL_MODE_s)
  126. #define RP2_UART_CTL_FLUSH_RX_m BIT(3)
  127. #define RP2_UART_CTL_FLUSH_TX_m BIT(4)
  128. #define RP2_UART_CTL_RESET_CH_m BIT(5)
  129. #define RP2_UART_CTL_XMIT_EN_m BIT(6)
  130. #define RP2_UART_CTL_DATABITS_s 8
  131. #define RP2_UART_CTL_DATABITS_m (0x3 << RP2_UART_CTL_DATABITS_s)
  132. #define RP2_UART_CTL_DATABITS_8 (0x3 << RP2_UART_CTL_DATABITS_s)
  133. #define RP2_UART_CTL_DATABITS_7 (0x2 << RP2_UART_CTL_DATABITS_s)
  134. #define RP2_UART_CTL_DATABITS_6 (0x1 << RP2_UART_CTL_DATABITS_s)
  135. #define RP2_UART_CTL_DATABITS_5 (0x0 << RP2_UART_CTL_DATABITS_s)
  136. #define RP2_UART_CTL_STOPBITS_m BIT(10)
  137. #define RP2_BAUD 0x01c
  138. /* ucode registers */
  139. #define RP2_TX_SWFLOW 0x02
  140. #define RP2_TX_SWFLOW_ena 0x81
  141. #define RP2_TX_SWFLOW_dis 0x9d
  142. #define RP2_RX_SWFLOW 0x0c
  143. #define RP2_RX_SWFLOW_ena 0x81
  144. #define RP2_RX_SWFLOW_dis 0x8d
  145. #define RP2_RX_FIFO 0x37
  146. #define RP2_RX_FIFO_ena 0x08
  147. #define RP2_RX_FIFO_dis 0x81
  148. static struct uart_driver rp2_uart_driver = {
  149. .owner = THIS_MODULE,
  150. .driver_name = DRV_NAME,
  151. .dev_name = "ttyRP",
  152. .nr = CONFIG_SERIAL_RP2_NR_UARTS,
  153. };
  154. struct rp2_card;
  155. struct rp2_uart_port {
  156. struct uart_port port;
  157. int idx;
  158. int ignore_rx;
  159. struct rp2_card *card;
  160. void __iomem *asic_base;
  161. void __iomem *base;
  162. void __iomem *ucode;
  163. };
  164. struct rp2_card {
  165. struct pci_dev *pdev;
  166. struct rp2_uart_port *ports;
  167. int n_ports;
  168. int initialized_ports;
  169. int minor_start;
  170. int smpte;
  171. void __iomem *bar0;
  172. void __iomem *bar1;
  173. spinlock_t card_lock;
  174. struct completion fw_loaded;
  175. };
  176. #define RP_ID(prod) PCI_VDEVICE(RP, (prod))
  177. #define RP_CAP(ports, smpte) (((ports) << 8) | ((smpte) << 0))
  178. static inline void rp2_decode_cap(const struct pci_device_id *id,
  179. int *ports, int *smpte)
  180. {
  181. *ports = id->driver_data >> 8;
  182. *smpte = id->driver_data & 0xff;
  183. }
  184. static DEFINE_SPINLOCK(rp2_minor_lock);
  185. static int rp2_minor_next;
  186. static int rp2_alloc_ports(int n_ports)
  187. {
  188. int ret = -ENOSPC;
  189. spin_lock(&rp2_minor_lock);
  190. if (rp2_minor_next + n_ports <= CONFIG_SERIAL_RP2_NR_UARTS) {
  191. /* sorry, no support for hot unplugging individual cards */
  192. ret = rp2_minor_next;
  193. rp2_minor_next += n_ports;
  194. }
  195. spin_unlock(&rp2_minor_lock);
  196. return ret;
  197. }
  198. static inline struct rp2_uart_port *port_to_up(struct uart_port *port)
  199. {
  200. return container_of(port, struct rp2_uart_port, port);
  201. }
  202. static void rp2_rmw(struct rp2_uart_port *up, int reg,
  203. u32 clr_bits, u32 set_bits)
  204. {
  205. u32 tmp = readl(up->base + reg);
  206. tmp &= ~clr_bits;
  207. tmp |= set_bits;
  208. writel(tmp, up->base + reg);
  209. }
  210. static void rp2_rmw_clr(struct rp2_uart_port *up, int reg, u32 val)
  211. {
  212. rp2_rmw(up, reg, val, 0);
  213. }
  214. static void rp2_rmw_set(struct rp2_uart_port *up, int reg, u32 val)
  215. {
  216. rp2_rmw(up, reg, 0, val);
  217. }
  218. static void rp2_mask_ch_irq(struct rp2_uart_port *up, int ch_num,
  219. int is_enabled)
  220. {
  221. unsigned long flags, irq_mask;
  222. spin_lock_irqsave(&up->card->card_lock, flags);
  223. irq_mask = readl(up->asic_base + RP2_CH_IRQ_MASK);
  224. if (is_enabled)
  225. irq_mask &= ~BIT(ch_num);
  226. else
  227. irq_mask |= BIT(ch_num);
  228. writel(irq_mask, up->asic_base + RP2_CH_IRQ_MASK);
  229. spin_unlock_irqrestore(&up->card->card_lock, flags);
  230. }
  231. static unsigned int rp2_uart_tx_empty(struct uart_port *port)
  232. {
  233. struct rp2_uart_port *up = port_to_up(port);
  234. unsigned long tx_fifo_bytes, flags;
  235. /*
  236. * This should probably check the transmitter, not the FIFO.
  237. * But the TXEMPTY bit doesn't seem to work unless the TX IRQ is
  238. * enabled.
  239. */
  240. spin_lock_irqsave(&up->port.lock, flags);
  241. tx_fifo_bytes = readw(up->base + RP2_TX_FIFO_COUNT);
  242. spin_unlock_irqrestore(&up->port.lock, flags);
  243. return tx_fifo_bytes ? 0 : TIOCSER_TEMT;
  244. }
  245. static unsigned int rp2_uart_get_mctrl(struct uart_port *port)
  246. {
  247. struct rp2_uart_port *up = port_to_up(port);
  248. u32 status;
  249. status = readl(up->base + RP2_CHAN_STAT);
  250. return ((status & RP2_CHAN_STAT_DCD_m) ? TIOCM_CAR : 0) |
  251. ((status & RP2_CHAN_STAT_DSR_m) ? TIOCM_DSR : 0) |
  252. ((status & RP2_CHAN_STAT_CTS_m) ? TIOCM_CTS : 0) |
  253. ((status & RP2_CHAN_STAT_RI_m) ? TIOCM_RI : 0);
  254. }
  255. static void rp2_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  256. {
  257. rp2_rmw(port_to_up(port), RP2_TXRX_CTL,
  258. RP2_TXRX_CTL_DTR_m | RP2_TXRX_CTL_RTS_m | RP2_TXRX_CTL_LOOP_m,
  259. ((mctrl & TIOCM_DTR) ? RP2_TXRX_CTL_DTR_m : 0) |
  260. ((mctrl & TIOCM_RTS) ? RP2_TXRX_CTL_RTS_m : 0) |
  261. ((mctrl & TIOCM_LOOP) ? RP2_TXRX_CTL_LOOP_m : 0));
  262. }
  263. static void rp2_uart_start_tx(struct uart_port *port)
  264. {
  265. rp2_rmw_set(port_to_up(port), RP2_TXRX_CTL, RP2_TXRX_CTL_TXIRQ_m);
  266. }
  267. static void rp2_uart_stop_tx(struct uart_port *port)
  268. {
  269. rp2_rmw_clr(port_to_up(port), RP2_TXRX_CTL, RP2_TXRX_CTL_TXIRQ_m);
  270. }
  271. static void rp2_uart_stop_rx(struct uart_port *port)
  272. {
  273. rp2_rmw_clr(port_to_up(port), RP2_TXRX_CTL, RP2_TXRX_CTL_RXIRQ_m);
  274. }
  275. static void rp2_uart_break_ctl(struct uart_port *port, int break_state)
  276. {
  277. unsigned long flags;
  278. spin_lock_irqsave(&port->lock, flags);
  279. rp2_rmw(port_to_up(port), RP2_TXRX_CTL, RP2_TXRX_CTL_BREAK_m,
  280. break_state ? RP2_TXRX_CTL_BREAK_m : 0);
  281. spin_unlock_irqrestore(&port->lock, flags);
  282. }
  283. static void rp2_uart_enable_ms(struct uart_port *port)
  284. {
  285. rp2_rmw_set(port_to_up(port), RP2_TXRX_CTL, RP2_TXRX_CTL_MSRIRQ_m);
  286. }
  287. static void __rp2_uart_set_termios(struct rp2_uart_port *up,
  288. unsigned long cfl,
  289. unsigned long ifl,
  290. unsigned int baud_div)
  291. {
  292. /* baud rate divisor (calculated elsewhere). 0 = divide-by-1 */
  293. writew(baud_div - 1, up->base + RP2_BAUD);
  294. /* data bits and stop bits */
  295. rp2_rmw(up, RP2_UART_CTL,
  296. RP2_UART_CTL_STOPBITS_m | RP2_UART_CTL_DATABITS_m,
  297. ((cfl & CSTOPB) ? RP2_UART_CTL_STOPBITS_m : 0) |
  298. (((cfl & CSIZE) == CS8) ? RP2_UART_CTL_DATABITS_8 : 0) |
  299. (((cfl & CSIZE) == CS7) ? RP2_UART_CTL_DATABITS_7 : 0) |
  300. (((cfl & CSIZE) == CS6) ? RP2_UART_CTL_DATABITS_6 : 0) |
  301. (((cfl & CSIZE) == CS5) ? RP2_UART_CTL_DATABITS_5 : 0));
  302. /* parity and hardware flow control */
  303. rp2_rmw(up, RP2_TXRX_CTL,
  304. RP2_TXRX_CTL_PARENB_m | RP2_TXRX_CTL_nPARODD_m |
  305. RP2_TXRX_CTL_CMSPAR_m | RP2_TXRX_CTL_DTRFLOW_m |
  306. RP2_TXRX_CTL_DSRFLOW_m | RP2_TXRX_CTL_RTSFLOW_m |
  307. RP2_TXRX_CTL_CTSFLOW_m,
  308. ((cfl & PARENB) ? RP2_TXRX_CTL_PARENB_m : 0) |
  309. ((cfl & PARODD) ? 0 : RP2_TXRX_CTL_nPARODD_m) |
  310. ((cfl & CMSPAR) ? RP2_TXRX_CTL_CMSPAR_m : 0) |
  311. ((cfl & CRTSCTS) ? (RP2_TXRX_CTL_RTSFLOW_m |
  312. RP2_TXRX_CTL_CTSFLOW_m) : 0));
  313. /* XON/XOFF software flow control */
  314. writeb((ifl & IXON) ? RP2_TX_SWFLOW_ena : RP2_TX_SWFLOW_dis,
  315. up->ucode + RP2_TX_SWFLOW);
  316. writeb((ifl & IXOFF) ? RP2_RX_SWFLOW_ena : RP2_RX_SWFLOW_dis,
  317. up->ucode + RP2_RX_SWFLOW);
  318. }
  319. static void rp2_uart_set_termios(struct uart_port *port,
  320. struct ktermios *new,
  321. struct ktermios *old)
  322. {
  323. struct rp2_uart_port *up = port_to_up(port);
  324. unsigned long flags;
  325. unsigned int baud, baud_div;
  326. baud = uart_get_baud_rate(port, new, old, 0, port->uartclk / 16);
  327. baud_div = uart_get_divisor(port, baud);
  328. if (tty_termios_baud_rate(new))
  329. tty_termios_encode_baud_rate(new, baud, baud);
  330. spin_lock_irqsave(&port->lock, flags);
  331. /* ignore all characters if CREAD is not set */
  332. port->ignore_status_mask = (new->c_cflag & CREAD) ? 0 : RP2_DUMMY_READ;
  333. __rp2_uart_set_termios(up, new->c_cflag, new->c_iflag, baud_div);
  334. uart_update_timeout(port, new->c_cflag, baud);
  335. spin_unlock_irqrestore(&port->lock, flags);
  336. }
  337. static void rp2_rx_chars(struct rp2_uart_port *up)
  338. {
  339. u16 bytes = readw(up->base + RP2_RX_FIFO_COUNT);
  340. struct tty_port *port = &up->port.state->port;
  341. for (; bytes != 0; bytes--) {
  342. u32 byte = readw(up->base + RP2_DATA_BYTE) | RP2_DUMMY_READ;
  343. char ch = byte & 0xff;
  344. if (likely(!(byte & RP2_DATA_BYTE_EXCEPTION_MASK))) {
  345. if (!uart_handle_sysrq_char(&up->port, ch))
  346. uart_insert_char(&up->port, byte, 0, ch,
  347. TTY_NORMAL);
  348. } else {
  349. char flag = TTY_NORMAL;
  350. if (byte & RP2_DATA_BYTE_BREAK_m)
  351. flag = TTY_BREAK;
  352. else if (byte & RP2_DATA_BYTE_ERR_FRAMING_m)
  353. flag = TTY_FRAME;
  354. else if (byte & RP2_DATA_BYTE_ERR_PARITY_m)
  355. flag = TTY_PARITY;
  356. uart_insert_char(&up->port, byte,
  357. RP2_DATA_BYTE_ERR_OVERRUN_m, ch, flag);
  358. }
  359. up->port.icount.rx++;
  360. }
  361. spin_unlock(&up->port.lock);
  362. tty_flip_buffer_push(port);
  363. spin_lock(&up->port.lock);
  364. }
  365. static void rp2_tx_chars(struct rp2_uart_port *up)
  366. {
  367. u16 max_tx = FIFO_SIZE - readw(up->base + RP2_TX_FIFO_COUNT);
  368. struct circ_buf *xmit = &up->port.state->xmit;
  369. if (uart_tx_stopped(&up->port)) {
  370. rp2_uart_stop_tx(&up->port);
  371. return;
  372. }
  373. for (; max_tx != 0; max_tx--) {
  374. if (up->port.x_char) {
  375. writeb(up->port.x_char, up->base + RP2_DATA_BYTE);
  376. up->port.x_char = 0;
  377. up->port.icount.tx++;
  378. continue;
  379. }
  380. if (uart_circ_empty(xmit)) {
  381. rp2_uart_stop_tx(&up->port);
  382. break;
  383. }
  384. writeb(xmit->buf[xmit->tail], up->base + RP2_DATA_BYTE);
  385. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  386. up->port.icount.tx++;
  387. }
  388. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  389. uart_write_wakeup(&up->port);
  390. }
  391. static void rp2_ch_interrupt(struct rp2_uart_port *up)
  392. {
  393. u32 status;
  394. spin_lock(&up->port.lock);
  395. /*
  396. * The IRQ status bits are clear-on-write. Other status bits in
  397. * this register aren't, so it's harmless to write to them.
  398. */
  399. status = readl(up->base + RP2_CHAN_STAT);
  400. writel(status, up->base + RP2_CHAN_STAT);
  401. if (status & RP2_CHAN_STAT_RXDATA_m)
  402. rp2_rx_chars(up);
  403. if (status & RP2_CHAN_STAT_TXEMPTY_m)
  404. rp2_tx_chars(up);
  405. if (status & RP2_CHAN_STAT_MS_CHANGED_MASK)
  406. wake_up_interruptible(&up->port.state->port.delta_msr_wait);
  407. spin_unlock(&up->port.lock);
  408. }
  409. static int rp2_asic_interrupt(struct rp2_card *card, unsigned int asic_id)
  410. {
  411. void __iomem *base = card->bar1 + RP2_ASIC_OFFSET(asic_id);
  412. int ch, handled = 0;
  413. unsigned long status = readl(base + RP2_CH_IRQ_STAT) &
  414. ~readl(base + RP2_CH_IRQ_MASK);
  415. for_each_set_bit(ch, &status, PORTS_PER_ASIC) {
  416. rp2_ch_interrupt(&card->ports[ch]);
  417. handled++;
  418. }
  419. return handled;
  420. }
  421. static irqreturn_t rp2_uart_interrupt(int irq, void *dev_id)
  422. {
  423. struct rp2_card *card = dev_id;
  424. int handled;
  425. handled = rp2_asic_interrupt(card, 0);
  426. if (card->n_ports >= PORTS_PER_ASIC)
  427. handled += rp2_asic_interrupt(card, 1);
  428. return handled ? IRQ_HANDLED : IRQ_NONE;
  429. }
  430. static inline void rp2_flush_fifos(struct rp2_uart_port *up)
  431. {
  432. rp2_rmw_set(up, RP2_UART_CTL,
  433. RP2_UART_CTL_FLUSH_RX_m | RP2_UART_CTL_FLUSH_TX_m);
  434. readl(up->base + RP2_UART_CTL);
  435. udelay(10);
  436. rp2_rmw_clr(up, RP2_UART_CTL,
  437. RP2_UART_CTL_FLUSH_RX_m | RP2_UART_CTL_FLUSH_TX_m);
  438. }
  439. static int rp2_uart_startup(struct uart_port *port)
  440. {
  441. struct rp2_uart_port *up = port_to_up(port);
  442. rp2_flush_fifos(up);
  443. rp2_rmw(up, RP2_TXRX_CTL, RP2_TXRX_CTL_MSRIRQ_m, RP2_TXRX_CTL_RXIRQ_m);
  444. rp2_rmw(up, RP2_TXRX_CTL, RP2_TXRX_CTL_RX_TRIG_m,
  445. RP2_TXRX_CTL_RX_TRIG_1);
  446. rp2_rmw(up, RP2_CHAN_STAT, 0, 0);
  447. rp2_mask_ch_irq(up, up->idx, 1);
  448. return 0;
  449. }
  450. static void rp2_uart_shutdown(struct uart_port *port)
  451. {
  452. struct rp2_uart_port *up = port_to_up(port);
  453. unsigned long flags;
  454. rp2_uart_break_ctl(port, 0);
  455. spin_lock_irqsave(&port->lock, flags);
  456. rp2_mask_ch_irq(up, up->idx, 0);
  457. rp2_rmw(up, RP2_CHAN_STAT, 0, 0);
  458. spin_unlock_irqrestore(&port->lock, flags);
  459. }
  460. static const char *rp2_uart_type(struct uart_port *port)
  461. {
  462. return (port->type == PORT_RP2) ? "RocketPort 2 UART" : NULL;
  463. }
  464. static void rp2_uart_release_port(struct uart_port *port)
  465. {
  466. /* Nothing to release ... */
  467. }
  468. static int rp2_uart_request_port(struct uart_port *port)
  469. {
  470. /* UARTs always present */
  471. return 0;
  472. }
  473. static void rp2_uart_config_port(struct uart_port *port, int flags)
  474. {
  475. if (flags & UART_CONFIG_TYPE)
  476. port->type = PORT_RP2;
  477. }
  478. static int rp2_uart_verify_port(struct uart_port *port,
  479. struct serial_struct *ser)
  480. {
  481. if (ser->type != PORT_UNKNOWN && ser->type != PORT_RP2)
  482. return -EINVAL;
  483. return 0;
  484. }
  485. static const struct uart_ops rp2_uart_ops = {
  486. .tx_empty = rp2_uart_tx_empty,
  487. .set_mctrl = rp2_uart_set_mctrl,
  488. .get_mctrl = rp2_uart_get_mctrl,
  489. .stop_tx = rp2_uart_stop_tx,
  490. .start_tx = rp2_uart_start_tx,
  491. .stop_rx = rp2_uart_stop_rx,
  492. .enable_ms = rp2_uart_enable_ms,
  493. .break_ctl = rp2_uart_break_ctl,
  494. .startup = rp2_uart_startup,
  495. .shutdown = rp2_uart_shutdown,
  496. .set_termios = rp2_uart_set_termios,
  497. .type = rp2_uart_type,
  498. .release_port = rp2_uart_release_port,
  499. .request_port = rp2_uart_request_port,
  500. .config_port = rp2_uart_config_port,
  501. .verify_port = rp2_uart_verify_port,
  502. };
  503. static void rp2_reset_asic(struct rp2_card *card, unsigned int asic_id)
  504. {
  505. void __iomem *base = card->bar1 + RP2_ASIC_OFFSET(asic_id);
  506. u32 clk_cfg;
  507. writew(1, base + RP2_GLOBAL_CMD);
  508. readw(base + RP2_GLOBAL_CMD);
  509. msleep(100);
  510. writel(0, base + RP2_CLK_PRESCALER);
  511. /* TDM clock configuration */
  512. clk_cfg = readw(base + RP2_ASIC_CFG);
  513. clk_cfg = (clk_cfg & ~BIT(8)) | BIT(9);
  514. writew(clk_cfg, base + RP2_ASIC_CFG);
  515. /* IRQ routing */
  516. writel(ALL_PORTS_MASK, base + RP2_CH_IRQ_MASK);
  517. writel(RP2_ASIC_IRQ_EN_m, base + RP2_ASIC_IRQ);
  518. }
  519. static void rp2_init_card(struct rp2_card *card)
  520. {
  521. writel(4, card->bar0 + RP2_FPGA_CTL0);
  522. writel(0, card->bar0 + RP2_FPGA_CTL1);
  523. rp2_reset_asic(card, 0);
  524. if (card->n_ports >= PORTS_PER_ASIC)
  525. rp2_reset_asic(card, 1);
  526. writel(RP2_IRQ_MASK_EN_m, card->bar0 + RP2_IRQ_MASK);
  527. }
  528. static void rp2_init_port(struct rp2_uart_port *up, const struct firmware *fw)
  529. {
  530. int i;
  531. writel(RP2_UART_CTL_RESET_CH_m, up->base + RP2_UART_CTL);
  532. readl(up->base + RP2_UART_CTL);
  533. udelay(1);
  534. writel(0, up->base + RP2_TXRX_CTL);
  535. writel(0, up->base + RP2_UART_CTL);
  536. readl(up->base + RP2_UART_CTL);
  537. udelay(1);
  538. rp2_flush_fifos(up);
  539. for (i = 0; i < min_t(int, fw->size, RP2_UCODE_BYTES); i++)
  540. writeb(fw->data[i], up->ucode + i);
  541. __rp2_uart_set_termios(up, CS8 | CREAD | CLOCAL, 0, DEFAULT_BAUD_DIV);
  542. rp2_uart_set_mctrl(&up->port, 0);
  543. writeb(RP2_RX_FIFO_ena, up->ucode + RP2_RX_FIFO);
  544. rp2_rmw(up, RP2_UART_CTL, RP2_UART_CTL_MODE_m,
  545. RP2_UART_CTL_XMIT_EN_m | RP2_UART_CTL_MODE_rs232);
  546. rp2_rmw_set(up, RP2_TXRX_CTL,
  547. RP2_TXRX_CTL_TX_EN_m | RP2_TXRX_CTL_RX_EN_m);
  548. }
  549. static void rp2_remove_ports(struct rp2_card *card)
  550. {
  551. int i;
  552. for (i = 0; i < card->initialized_ports; i++)
  553. uart_remove_one_port(&rp2_uart_driver, &card->ports[i].port);
  554. card->initialized_ports = 0;
  555. }
  556. static void rp2_fw_cb(const struct firmware *fw, void *context)
  557. {
  558. struct rp2_card *card = context;
  559. resource_size_t phys_base;
  560. int i, rc = -ENOENT;
  561. if (!fw) {
  562. dev_err(&card->pdev->dev, "cannot find '%s' firmware image\n",
  563. RP2_FW_NAME);
  564. goto no_fw;
  565. }
  566. phys_base = pci_resource_start(card->pdev, 1);
  567. for (i = 0; i < card->n_ports; i++) {
  568. struct rp2_uart_port *rp = &card->ports[i];
  569. struct uart_port *p;
  570. int j = (unsigned)i % PORTS_PER_ASIC;
  571. rp->asic_base = card->bar1;
  572. rp->base = card->bar1 + RP2_PORT_BASE + j*RP2_PORT_SPACING;
  573. rp->ucode = card->bar1 + RP2_UCODE_BASE + j*RP2_UCODE_SPACING;
  574. rp->card = card;
  575. rp->idx = j;
  576. p = &rp->port;
  577. p->line = card->minor_start + i;
  578. p->dev = &card->pdev->dev;
  579. p->type = PORT_RP2;
  580. p->iotype = UPIO_MEM32;
  581. p->uartclk = UART_CLOCK;
  582. p->regshift = 2;
  583. p->fifosize = FIFO_SIZE;
  584. p->ops = &rp2_uart_ops;
  585. p->irq = card->pdev->irq;
  586. p->membase = rp->base;
  587. p->mapbase = phys_base + RP2_PORT_BASE + j*RP2_PORT_SPACING;
  588. if (i >= PORTS_PER_ASIC) {
  589. rp->asic_base += RP2_ASIC_SPACING;
  590. rp->base += RP2_ASIC_SPACING;
  591. rp->ucode += RP2_ASIC_SPACING;
  592. p->mapbase += RP2_ASIC_SPACING;
  593. }
  594. rp2_init_port(rp, fw);
  595. rc = uart_add_one_port(&rp2_uart_driver, p);
  596. if (rc) {
  597. dev_err(&card->pdev->dev,
  598. "error registering port %d: %d\n", i, rc);
  599. rp2_remove_ports(card);
  600. break;
  601. }
  602. card->initialized_ports++;
  603. }
  604. release_firmware(fw);
  605. no_fw:
  606. /*
  607. * rp2_fw_cb() is called from a workqueue long after rp2_probe()
  608. * has already returned success. So if something failed here,
  609. * we'll just leave the now-dormant device in place until somebody
  610. * unbinds it.
  611. */
  612. if (rc)
  613. dev_warn(&card->pdev->dev, "driver initialization failed\n");
  614. complete(&card->fw_loaded);
  615. }
  616. static int rp2_probe(struct pci_dev *pdev,
  617. const struct pci_device_id *id)
  618. {
  619. struct rp2_card *card;
  620. struct rp2_uart_port *ports;
  621. void __iomem * const *bars;
  622. int rc;
  623. card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL);
  624. if (!card)
  625. return -ENOMEM;
  626. pci_set_drvdata(pdev, card);
  627. spin_lock_init(&card->card_lock);
  628. init_completion(&card->fw_loaded);
  629. rc = pcim_enable_device(pdev);
  630. if (rc)
  631. return rc;
  632. rc = pcim_iomap_regions_request_all(pdev, 0x03, DRV_NAME);
  633. if (rc)
  634. return rc;
  635. bars = pcim_iomap_table(pdev);
  636. card->bar0 = bars[0];
  637. card->bar1 = bars[1];
  638. card->pdev = pdev;
  639. rp2_decode_cap(id, &card->n_ports, &card->smpte);
  640. dev_info(&pdev->dev, "found new card with %d ports\n", card->n_ports);
  641. card->minor_start = rp2_alloc_ports(card->n_ports);
  642. if (card->minor_start < 0) {
  643. dev_err(&pdev->dev,
  644. "too many ports (try increasing CONFIG_SERIAL_RP2_NR_UARTS)\n");
  645. return -EINVAL;
  646. }
  647. rp2_init_card(card);
  648. ports = devm_kzalloc(&pdev->dev, sizeof(*ports) * card->n_ports,
  649. GFP_KERNEL);
  650. if (!ports)
  651. return -ENOMEM;
  652. card->ports = ports;
  653. rc = devm_request_irq(&pdev->dev, pdev->irq, rp2_uart_interrupt,
  654. IRQF_SHARED, DRV_NAME, card);
  655. if (rc)
  656. return rc;
  657. /*
  658. * Only catastrophic errors (e.g. ENOMEM) are reported here.
  659. * If the FW image is missing, we'll find out in rp2_fw_cb()
  660. * and print an error message.
  661. */
  662. rc = reject_firmware_nowait(THIS_MODULE, 1, RP2_FW_NAME, &pdev->dev,
  663. GFP_KERNEL, card, rp2_fw_cb);
  664. if (rc)
  665. return rc;
  666. dev_dbg(&pdev->dev, "waiting for firmware blob...\n");
  667. return 0;
  668. }
  669. static void rp2_remove(struct pci_dev *pdev)
  670. {
  671. struct rp2_card *card = pci_get_drvdata(pdev);
  672. wait_for_completion(&card->fw_loaded);
  673. rp2_remove_ports(card);
  674. }
  675. static const struct pci_device_id rp2_pci_tbl[] = {
  676. /* RocketPort INFINITY cards */
  677. { RP_ID(0x0040), RP_CAP(8, 0) }, /* INF Octa, RJ45, selectable */
  678. { RP_ID(0x0041), RP_CAP(32, 0) }, /* INF 32, ext interface */
  679. { RP_ID(0x0042), RP_CAP(8, 0) }, /* INF Octa, ext interface */
  680. { RP_ID(0x0043), RP_CAP(16, 0) }, /* INF 16, ext interface */
  681. { RP_ID(0x0044), RP_CAP(4, 0) }, /* INF Quad, DB, selectable */
  682. { RP_ID(0x0045), RP_CAP(8, 0) }, /* INF Octa, DB, selectable */
  683. { RP_ID(0x0046), RP_CAP(4, 0) }, /* INF Quad, ext interface */
  684. { RP_ID(0x0047), RP_CAP(4, 0) }, /* INF Quad, RJ45 */
  685. { RP_ID(0x004a), RP_CAP(4, 0) }, /* INF Plus, Quad */
  686. { RP_ID(0x004b), RP_CAP(8, 0) }, /* INF Plus, Octa */
  687. { RP_ID(0x004c), RP_CAP(8, 0) }, /* INF III, Octa */
  688. { RP_ID(0x004d), RP_CAP(4, 0) }, /* INF III, Quad */
  689. { RP_ID(0x004e), RP_CAP(2, 0) }, /* INF Plus, 2, RS232 */
  690. { RP_ID(0x004f), RP_CAP(2, 1) }, /* INF Plus, 2, SMPTE */
  691. { RP_ID(0x0050), RP_CAP(4, 0) }, /* INF Plus, Quad, RJ45 */
  692. { RP_ID(0x0051), RP_CAP(8, 0) }, /* INF Plus, Octa, RJ45 */
  693. { RP_ID(0x0052), RP_CAP(8, 1) }, /* INF Octa, SMPTE */
  694. /* RocketPort EXPRESS cards */
  695. { RP_ID(0x0060), RP_CAP(8, 0) }, /* EXP Octa, RJ45, selectable */
  696. { RP_ID(0x0061), RP_CAP(32, 0) }, /* EXP 32, ext interface */
  697. { RP_ID(0x0062), RP_CAP(8, 0) }, /* EXP Octa, ext interface */
  698. { RP_ID(0x0063), RP_CAP(16, 0) }, /* EXP 16, ext interface */
  699. { RP_ID(0x0064), RP_CAP(4, 0) }, /* EXP Quad, DB, selectable */
  700. { RP_ID(0x0065), RP_CAP(8, 0) }, /* EXP Octa, DB, selectable */
  701. { RP_ID(0x0066), RP_CAP(4, 0) }, /* EXP Quad, ext interface */
  702. { RP_ID(0x0067), RP_CAP(4, 0) }, /* EXP Quad, RJ45 */
  703. { RP_ID(0x0068), RP_CAP(8, 0) }, /* EXP Octa, RJ11 */
  704. { RP_ID(0x0072), RP_CAP(8, 1) }, /* EXP Octa, SMPTE */
  705. { }
  706. };
  707. MODULE_DEVICE_TABLE(pci, rp2_pci_tbl);
  708. static struct pci_driver rp2_pci_driver = {
  709. .name = DRV_NAME,
  710. .id_table = rp2_pci_tbl,
  711. .probe = rp2_probe,
  712. .remove = rp2_remove,
  713. };
  714. static int __init rp2_uart_init(void)
  715. {
  716. int rc;
  717. rc = uart_register_driver(&rp2_uart_driver);
  718. if (rc)
  719. return rc;
  720. rc = pci_register_driver(&rp2_pci_driver);
  721. if (rc) {
  722. uart_unregister_driver(&rp2_uart_driver);
  723. return rc;
  724. }
  725. return 0;
  726. }
  727. static void __exit rp2_uart_exit(void)
  728. {
  729. pci_unregister_driver(&rp2_pci_driver);
  730. uart_unregister_driver(&rp2_uart_driver);
  731. }
  732. module_init(rp2_uart_init);
  733. module_exit(rp2_uart_exit);
  734. MODULE_DESCRIPTION("Comtrol RocketPort EXPRESS/INFINITY driver");
  735. MODULE_AUTHOR("Kevin Cernekee <cernekee@gmail.com>");
  736. MODULE_LICENSE("GPL v2");
  737. /*(DEBLOBBED)*/