men_z135_uart.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /*
  2. * MEN 16z135 High Speed UART
  3. *
  4. * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de)
  5. * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; version 2 of the License.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ":" fmt
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/serial_core.h>
  16. #include <linux/ioport.h>
  17. #include <linux/io.h>
  18. #include <linux/tty_flip.h>
  19. #include <linux/bitops.h>
  20. #include <linux/mcb.h>
  21. #define MEN_Z135_MAX_PORTS 12
  22. #define MEN_Z135_BASECLK 29491200
  23. #define MEN_Z135_FIFO_SIZE 1024
  24. #define MEN_Z135_FIFO_WATERMARK 1020
  25. #define MEN_Z135_STAT_REG 0x0
  26. #define MEN_Z135_RX_RAM 0x4
  27. #define MEN_Z135_TX_RAM 0x400
  28. #define MEN_Z135_RX_CTRL 0x800
  29. #define MEN_Z135_TX_CTRL 0x804
  30. #define MEN_Z135_CONF_REG 0x808
  31. #define MEN_Z135_UART_FREQ 0x80c
  32. #define MEN_Z135_BAUD_REG 0x810
  33. #define MEN_Z135_TIMEOUT 0x814
  34. #define IRQ_ID(x) ((x) & 0x1f)
  35. #define MEN_Z135_IER_RXCIEN BIT(0) /* RX Space IRQ */
  36. #define MEN_Z135_IER_TXCIEN BIT(1) /* TX Space IRQ */
  37. #define MEN_Z135_IER_RLSIEN BIT(2) /* Receiver Line Status IRQ */
  38. #define MEN_Z135_IER_MSIEN BIT(3) /* Modem Status IRQ */
  39. #define MEN_Z135_ALL_IRQS (MEN_Z135_IER_RXCIEN \
  40. | MEN_Z135_IER_RLSIEN \
  41. | MEN_Z135_IER_MSIEN \
  42. | MEN_Z135_IER_TXCIEN)
  43. #define MEN_Z135_MCR_DTR BIT(24)
  44. #define MEN_Z135_MCR_RTS BIT(25)
  45. #define MEN_Z135_MCR_OUT1 BIT(26)
  46. #define MEN_Z135_MCR_OUT2 BIT(27)
  47. #define MEN_Z135_MCR_LOOP BIT(28)
  48. #define MEN_Z135_MCR_RCFC BIT(29)
  49. #define MEN_Z135_MSR_DCTS BIT(0)
  50. #define MEN_Z135_MSR_DDSR BIT(1)
  51. #define MEN_Z135_MSR_DRI BIT(2)
  52. #define MEN_Z135_MSR_DDCD BIT(3)
  53. #define MEN_Z135_MSR_CTS BIT(4)
  54. #define MEN_Z135_MSR_DSR BIT(5)
  55. #define MEN_Z135_MSR_RI BIT(6)
  56. #define MEN_Z135_MSR_DCD BIT(7)
  57. #define MEN_Z135_LCR_SHIFT 8 /* LCR shift mask */
  58. #define MEN_Z135_WL5 0 /* CS5 */
  59. #define MEN_Z135_WL6 1 /* CS6 */
  60. #define MEN_Z135_WL7 2 /* CS7 */
  61. #define MEN_Z135_WL8 3 /* CS8 */
  62. #define MEN_Z135_STB_SHIFT 2 /* Stopbits */
  63. #define MEN_Z135_NSTB1 0
  64. #define MEN_Z135_NSTB2 1
  65. #define MEN_Z135_PEN_SHIFT 3 /* Parity enable */
  66. #define MEN_Z135_PAR_DIS 0
  67. #define MEN_Z135_PAR_ENA 1
  68. #define MEN_Z135_PTY_SHIFT 4 /* Parity type */
  69. #define MEN_Z135_PTY_ODD 0
  70. #define MEN_Z135_PTY_EVN 1
  71. #define MEN_Z135_LSR_DR BIT(0)
  72. #define MEN_Z135_LSR_OE BIT(1)
  73. #define MEN_Z135_LSR_PE BIT(2)
  74. #define MEN_Z135_LSR_FE BIT(3)
  75. #define MEN_Z135_LSR_BI BIT(4)
  76. #define MEN_Z135_LSR_THEP BIT(5)
  77. #define MEN_Z135_LSR_TEXP BIT(6)
  78. #define MEN_Z135_LSR_RXFIFOERR BIT(7)
  79. #define MEN_Z135_IRQ_ID_RLS BIT(0)
  80. #define MEN_Z135_IRQ_ID_RDA BIT(1)
  81. #define MEN_Z135_IRQ_ID_CTI BIT(2)
  82. #define MEN_Z135_IRQ_ID_TSA BIT(3)
  83. #define MEN_Z135_IRQ_ID_MST BIT(4)
  84. #define LCR(x) (((x) >> MEN_Z135_LCR_SHIFT) & 0xff)
  85. #define BYTES_TO_ALIGN(x) ((x) & 0x3)
  86. static int line;
  87. static int txlvl = 5;
  88. module_param(txlvl, int, S_IRUGO);
  89. MODULE_PARM_DESC(txlvl, "TX IRQ trigger level 0-7, default 5 (128 byte)");
  90. static int rxlvl = 6;
  91. module_param(rxlvl, int, S_IRUGO);
  92. MODULE_PARM_DESC(rxlvl, "RX IRQ trigger level 0-7, default 6 (256 byte)");
  93. static int align;
  94. module_param(align, int, S_IRUGO);
  95. MODULE_PARM_DESC(align, "Keep hardware FIFO write pointer aligned, default 0");
  96. static uint rx_timeout;
  97. module_param(rx_timeout, uint, S_IRUGO);
  98. MODULE_PARM_DESC(rx_timeout, "RX timeout. "
  99. "Timeout in seconds = (timeout_reg * baud_reg * 4) / freq_reg");
  100. struct men_z135_port {
  101. struct uart_port port;
  102. struct mcb_device *mdev;
  103. struct resource *mem;
  104. unsigned char *rxbuf;
  105. u32 stat_reg;
  106. spinlock_t lock;
  107. bool automode;
  108. };
  109. #define to_men_z135(port) container_of((port), struct men_z135_port, port)
  110. /**
  111. * men_z135_reg_set() - Set value in register
  112. * @uart: The UART port
  113. * @addr: Register address
  114. * @val: value to set
  115. */
  116. static inline void men_z135_reg_set(struct men_z135_port *uart,
  117. u32 addr, u32 val)
  118. {
  119. struct uart_port *port = &uart->port;
  120. unsigned long flags;
  121. u32 reg;
  122. spin_lock_irqsave(&uart->lock, flags);
  123. reg = ioread32(port->membase + addr);
  124. reg |= val;
  125. iowrite32(reg, port->membase + addr);
  126. spin_unlock_irqrestore(&uart->lock, flags);
  127. }
  128. /**
  129. * men_z135_reg_clr() - Unset value in register
  130. * @uart: The UART port
  131. * @addr: Register address
  132. * @val: value to clear
  133. */
  134. static void men_z135_reg_clr(struct men_z135_port *uart,
  135. u32 addr, u32 val)
  136. {
  137. struct uart_port *port = &uart->port;
  138. unsigned long flags;
  139. u32 reg;
  140. spin_lock_irqsave(&uart->lock, flags);
  141. reg = ioread32(port->membase + addr);
  142. reg &= ~val;
  143. iowrite32(reg, port->membase + addr);
  144. spin_unlock_irqrestore(&uart->lock, flags);
  145. }
  146. /**
  147. * men_z135_handle_modem_status() - Handle change of modem status
  148. * @port: The UART port
  149. *
  150. * Handle change of modem status register. This is done by reading the "delta"
  151. * versions of DCD (Data Carrier Detect) and CTS (Clear To Send).
  152. */
  153. static void men_z135_handle_modem_status(struct men_z135_port *uart)
  154. {
  155. u8 msr;
  156. msr = (uart->stat_reg >> 8) & 0xff;
  157. if (msr & MEN_Z135_MSR_DDCD)
  158. uart_handle_dcd_change(&uart->port,
  159. msr & MEN_Z135_MSR_DCD);
  160. if (msr & MEN_Z135_MSR_DCTS)
  161. uart_handle_cts_change(&uart->port,
  162. msr & MEN_Z135_MSR_CTS);
  163. }
  164. static void men_z135_handle_lsr(struct men_z135_port *uart)
  165. {
  166. struct uart_port *port = &uart->port;
  167. u8 lsr;
  168. lsr = (uart->stat_reg >> 16) & 0xff;
  169. if (lsr & MEN_Z135_LSR_OE)
  170. port->icount.overrun++;
  171. if (lsr & MEN_Z135_LSR_PE)
  172. port->icount.parity++;
  173. if (lsr & MEN_Z135_LSR_FE)
  174. port->icount.frame++;
  175. if (lsr & MEN_Z135_LSR_BI) {
  176. port->icount.brk++;
  177. uart_handle_break(port);
  178. }
  179. }
  180. /**
  181. * get_rx_fifo_content() - Get the number of bytes in RX FIFO
  182. * @uart: The UART port
  183. *
  184. * Read RXC register from hardware and return current FIFO fill size.
  185. */
  186. static u16 get_rx_fifo_content(struct men_z135_port *uart)
  187. {
  188. struct uart_port *port = &uart->port;
  189. u32 stat_reg;
  190. u16 rxc;
  191. u8 rxc_lo;
  192. u8 rxc_hi;
  193. stat_reg = ioread32(port->membase + MEN_Z135_STAT_REG);
  194. rxc_lo = stat_reg >> 24;
  195. rxc_hi = (stat_reg & 0xC0) >> 6;
  196. rxc = rxc_lo | (rxc_hi << 8);
  197. return rxc;
  198. }
  199. /**
  200. * men_z135_handle_rx() - RX tasklet routine
  201. * @arg: Pointer to struct men_z135_port
  202. *
  203. * Copy from RX FIFO and acknowledge number of bytes copied.
  204. */
  205. static void men_z135_handle_rx(struct men_z135_port *uart)
  206. {
  207. struct uart_port *port = &uart->port;
  208. struct tty_port *tport = &port->state->port;
  209. int copied;
  210. u16 size;
  211. int room;
  212. size = get_rx_fifo_content(uart);
  213. if (size == 0)
  214. return;
  215. /* Avoid accidently accessing TX FIFO instead of RX FIFO. Last
  216. * longword in RX FIFO cannot be read.(0x004-0x3FF)
  217. */
  218. if (size > MEN_Z135_FIFO_WATERMARK)
  219. size = MEN_Z135_FIFO_WATERMARK;
  220. room = tty_buffer_request_room(tport, size);
  221. if (room != size)
  222. dev_warn(&uart->mdev->dev,
  223. "Not enough room in flip buffer, truncating to %d\n",
  224. room);
  225. if (room == 0)
  226. return;
  227. memcpy_fromio(uart->rxbuf, port->membase + MEN_Z135_RX_RAM, room);
  228. /* Be sure to first copy all data and then acknowledge it */
  229. mb();
  230. iowrite32(room, port->membase + MEN_Z135_RX_CTRL);
  231. copied = tty_insert_flip_string(tport, uart->rxbuf, room);
  232. if (copied != room)
  233. dev_warn(&uart->mdev->dev,
  234. "Only copied %d instead of %d bytes\n",
  235. copied, room);
  236. port->icount.rx += copied;
  237. tty_flip_buffer_push(tport);
  238. }
  239. /**
  240. * men_z135_handle_tx() - TX tasklet routine
  241. * @arg: Pointer to struct men_z135_port
  242. *
  243. */
  244. static void men_z135_handle_tx(struct men_z135_port *uart)
  245. {
  246. struct uart_port *port = &uart->port;
  247. struct circ_buf *xmit = &port->state->xmit;
  248. u32 txc;
  249. u32 wptr;
  250. int qlen;
  251. int n;
  252. int txfree;
  253. int head;
  254. int tail;
  255. int s;
  256. if (uart_circ_empty(xmit))
  257. goto out;
  258. if (uart_tx_stopped(port))
  259. goto out;
  260. if (port->x_char)
  261. goto out;
  262. /* calculate bytes to copy */
  263. qlen = uart_circ_chars_pending(xmit);
  264. if (qlen <= 0)
  265. goto out;
  266. wptr = ioread32(port->membase + MEN_Z135_TX_CTRL);
  267. txc = (wptr >> 16) & 0x3ff;
  268. wptr &= 0x3ff;
  269. if (txc > MEN_Z135_FIFO_WATERMARK)
  270. txc = MEN_Z135_FIFO_WATERMARK;
  271. txfree = MEN_Z135_FIFO_WATERMARK - txc;
  272. if (txfree <= 0) {
  273. dev_err(&uart->mdev->dev,
  274. "Not enough room in TX FIFO have %d, need %d\n",
  275. txfree, qlen);
  276. goto irq_en;
  277. }
  278. /* if we're not aligned, it's better to copy only 1 or 2 bytes and
  279. * then the rest.
  280. */
  281. if (align && qlen >= 3 && BYTES_TO_ALIGN(wptr))
  282. n = 4 - BYTES_TO_ALIGN(wptr);
  283. else if (qlen > txfree)
  284. n = txfree;
  285. else
  286. n = qlen;
  287. if (n <= 0)
  288. goto irq_en;
  289. head = xmit->head & (UART_XMIT_SIZE - 1);
  290. tail = xmit->tail & (UART_XMIT_SIZE - 1);
  291. s = ((head >= tail) ? head : UART_XMIT_SIZE) - tail;
  292. n = min(n, s);
  293. memcpy_toio(port->membase + MEN_Z135_TX_RAM, &xmit->buf[xmit->tail], n);
  294. xmit->tail = (xmit->tail + n) & (UART_XMIT_SIZE - 1);
  295. mmiowb();
  296. iowrite32(n & 0x3ff, port->membase + MEN_Z135_TX_CTRL);
  297. port->icount.tx += n;
  298. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  299. uart_write_wakeup(port);
  300. irq_en:
  301. if (!uart_circ_empty(xmit))
  302. men_z135_reg_set(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_TXCIEN);
  303. else
  304. men_z135_reg_clr(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_TXCIEN);
  305. out:
  306. return;
  307. }
  308. /**
  309. * men_z135_intr() - Handle legacy IRQs
  310. * @irq: The IRQ number
  311. * @data: Pointer to UART port
  312. *
  313. * Check IIR register to find the cause of the interrupt and handle it.
  314. * It is possible that multiple interrupts reason bits are set and reading
  315. * the IIR is a destructive read, so we always need to check for all possible
  316. * interrupts and handle them.
  317. */
  318. static irqreturn_t men_z135_intr(int irq, void *data)
  319. {
  320. struct men_z135_port *uart = (struct men_z135_port *)data;
  321. struct uart_port *port = &uart->port;
  322. bool handled = false;
  323. int irq_id;
  324. uart->stat_reg = ioread32(port->membase + MEN_Z135_STAT_REG);
  325. irq_id = IRQ_ID(uart->stat_reg);
  326. if (!irq_id)
  327. goto out;
  328. spin_lock(&port->lock);
  329. /* It's save to write to IIR[7:6] RXC[9:8] */
  330. iowrite8(irq_id, port->membase + MEN_Z135_STAT_REG);
  331. if (irq_id & MEN_Z135_IRQ_ID_RLS) {
  332. men_z135_handle_lsr(uart);
  333. handled = true;
  334. }
  335. if (irq_id & (MEN_Z135_IRQ_ID_RDA | MEN_Z135_IRQ_ID_CTI)) {
  336. if (irq_id & MEN_Z135_IRQ_ID_CTI)
  337. dev_dbg(&uart->mdev->dev, "Character Timeout Indication\n");
  338. men_z135_handle_rx(uart);
  339. handled = true;
  340. }
  341. if (irq_id & MEN_Z135_IRQ_ID_TSA) {
  342. men_z135_handle_tx(uart);
  343. handled = true;
  344. }
  345. if (irq_id & MEN_Z135_IRQ_ID_MST) {
  346. men_z135_handle_modem_status(uart);
  347. handled = true;
  348. }
  349. spin_unlock(&port->lock);
  350. out:
  351. return IRQ_RETVAL(handled);
  352. }
  353. /**
  354. * men_z135_request_irq() - Request IRQ for 16z135 core
  355. * @uart: z135 private uart port structure
  356. *
  357. * Request an IRQ for 16z135 to use. First try using MSI, if it fails
  358. * fall back to using legacy interrupts.
  359. */
  360. static int men_z135_request_irq(struct men_z135_port *uart)
  361. {
  362. struct device *dev = &uart->mdev->dev;
  363. struct uart_port *port = &uart->port;
  364. int err = 0;
  365. err = request_irq(port->irq, men_z135_intr, IRQF_SHARED,
  366. "men_z135_intr", uart);
  367. if (err)
  368. dev_err(dev, "Error %d getting interrupt\n", err);
  369. return err;
  370. }
  371. /**
  372. * men_z135_tx_empty() - Handle tx_empty call
  373. * @port: The UART port
  374. *
  375. * This function tests whether the TX FIFO and shifter for the port
  376. * described by @port is empty.
  377. */
  378. static unsigned int men_z135_tx_empty(struct uart_port *port)
  379. {
  380. u32 wptr;
  381. u16 txc;
  382. wptr = ioread32(port->membase + MEN_Z135_TX_CTRL);
  383. txc = (wptr >> 16) & 0x3ff;
  384. if (txc == 0)
  385. return TIOCSER_TEMT;
  386. else
  387. return 0;
  388. }
  389. /**
  390. * men_z135_set_mctrl() - Set modem control lines
  391. * @port: The UART port
  392. * @mctrl: The modem control lines
  393. *
  394. * This function sets the modem control lines for a port described by @port
  395. * to the state described by @mctrl
  396. */
  397. static void men_z135_set_mctrl(struct uart_port *port, unsigned int mctrl)
  398. {
  399. u32 old;
  400. u32 conf_reg;
  401. conf_reg = old = ioread32(port->membase + MEN_Z135_CONF_REG);
  402. if (mctrl & TIOCM_RTS)
  403. conf_reg |= MEN_Z135_MCR_RTS;
  404. else
  405. conf_reg &= ~MEN_Z135_MCR_RTS;
  406. if (mctrl & TIOCM_DTR)
  407. conf_reg |= MEN_Z135_MCR_DTR;
  408. else
  409. conf_reg &= ~MEN_Z135_MCR_DTR;
  410. if (mctrl & TIOCM_OUT1)
  411. conf_reg |= MEN_Z135_MCR_OUT1;
  412. else
  413. conf_reg &= ~MEN_Z135_MCR_OUT1;
  414. if (mctrl & TIOCM_OUT2)
  415. conf_reg |= MEN_Z135_MCR_OUT2;
  416. else
  417. conf_reg &= ~MEN_Z135_MCR_OUT2;
  418. if (mctrl & TIOCM_LOOP)
  419. conf_reg |= MEN_Z135_MCR_LOOP;
  420. else
  421. conf_reg &= ~MEN_Z135_MCR_LOOP;
  422. if (conf_reg != old)
  423. iowrite32(conf_reg, port->membase + MEN_Z135_CONF_REG);
  424. }
  425. /**
  426. * men_z135_get_mctrl() - Get modem control lines
  427. * @port: The UART port
  428. *
  429. * Retruns the current state of modem control inputs.
  430. */
  431. static unsigned int men_z135_get_mctrl(struct uart_port *port)
  432. {
  433. unsigned int mctrl = 0;
  434. u8 msr;
  435. msr = ioread8(port->membase + MEN_Z135_STAT_REG + 1);
  436. if (msr & MEN_Z135_MSR_CTS)
  437. mctrl |= TIOCM_CTS;
  438. if (msr & MEN_Z135_MSR_DSR)
  439. mctrl |= TIOCM_DSR;
  440. if (msr & MEN_Z135_MSR_RI)
  441. mctrl |= TIOCM_RI;
  442. if (msr & MEN_Z135_MSR_DCD)
  443. mctrl |= TIOCM_CAR;
  444. return mctrl;
  445. }
  446. /**
  447. * men_z135_stop_tx() - Stop transmitting characters
  448. * @port: The UART port
  449. *
  450. * Stop transmitting characters. This might be due to CTS line becomming
  451. * inactive or the tty layer indicating we want to stop transmission due to
  452. * an XOFF character.
  453. */
  454. static void men_z135_stop_tx(struct uart_port *port)
  455. {
  456. struct men_z135_port *uart = to_men_z135(port);
  457. men_z135_reg_clr(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_TXCIEN);
  458. }
  459. /*
  460. * men_z135_disable_ms() - Disable Modem Status
  461. * port: The UART port
  462. *
  463. * Enable Modem Status IRQ.
  464. */
  465. static void men_z135_disable_ms(struct uart_port *port)
  466. {
  467. struct men_z135_port *uart = to_men_z135(port);
  468. men_z135_reg_clr(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_MSIEN);
  469. }
  470. /**
  471. * men_z135_start_tx() - Start transmitting characters
  472. * @port: The UART port
  473. *
  474. * Start transmitting character. This actually doesn't transmit anything, but
  475. * fires off the TX tasklet.
  476. */
  477. static void men_z135_start_tx(struct uart_port *port)
  478. {
  479. struct men_z135_port *uart = to_men_z135(port);
  480. if (uart->automode)
  481. men_z135_disable_ms(port);
  482. men_z135_handle_tx(uart);
  483. }
  484. /**
  485. * men_z135_stop_rx() - Stop receiving characters
  486. * @port: The UART port
  487. *
  488. * Stop receiving characters; the port is in the process of being closed.
  489. */
  490. static void men_z135_stop_rx(struct uart_port *port)
  491. {
  492. struct men_z135_port *uart = to_men_z135(port);
  493. men_z135_reg_clr(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_RXCIEN);
  494. }
  495. /**
  496. * men_z135_enable_ms() - Enable Modem Status
  497. * port:
  498. *
  499. * Enable Modem Status IRQ.
  500. */
  501. static void men_z135_enable_ms(struct uart_port *port)
  502. {
  503. struct men_z135_port *uart = to_men_z135(port);
  504. men_z135_reg_set(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_MSIEN);
  505. }
  506. static int men_z135_startup(struct uart_port *port)
  507. {
  508. struct men_z135_port *uart = to_men_z135(port);
  509. int err;
  510. u32 conf_reg = 0;
  511. err = men_z135_request_irq(uart);
  512. if (err)
  513. return -ENODEV;
  514. conf_reg = ioread32(port->membase + MEN_Z135_CONF_REG);
  515. /* Activate all but TX space available IRQ */
  516. conf_reg |= MEN_Z135_ALL_IRQS & ~MEN_Z135_IER_TXCIEN;
  517. conf_reg &= ~(0xff << 16);
  518. conf_reg |= (txlvl << 16);
  519. conf_reg |= (rxlvl << 20);
  520. iowrite32(conf_reg, port->membase + MEN_Z135_CONF_REG);
  521. if (rx_timeout)
  522. iowrite32(rx_timeout, port->membase + MEN_Z135_TIMEOUT);
  523. return 0;
  524. }
  525. static void men_z135_shutdown(struct uart_port *port)
  526. {
  527. struct men_z135_port *uart = to_men_z135(port);
  528. u32 conf_reg = 0;
  529. conf_reg |= MEN_Z135_ALL_IRQS;
  530. men_z135_reg_clr(uart, MEN_Z135_CONF_REG, conf_reg);
  531. free_irq(uart->port.irq, uart);
  532. }
  533. static void men_z135_set_termios(struct uart_port *port,
  534. struct ktermios *termios,
  535. struct ktermios *old)
  536. {
  537. struct men_z135_port *uart = to_men_z135(port);
  538. unsigned int baud;
  539. u32 conf_reg;
  540. u32 bd_reg;
  541. u32 uart_freq;
  542. u8 lcr;
  543. conf_reg = ioread32(port->membase + MEN_Z135_CONF_REG);
  544. lcr = LCR(conf_reg);
  545. /* byte size */
  546. switch (termios->c_cflag & CSIZE) {
  547. case CS5:
  548. lcr |= MEN_Z135_WL5;
  549. break;
  550. case CS6:
  551. lcr |= MEN_Z135_WL6;
  552. break;
  553. case CS7:
  554. lcr |= MEN_Z135_WL7;
  555. break;
  556. case CS8:
  557. lcr |= MEN_Z135_WL8;
  558. break;
  559. }
  560. /* stop bits */
  561. if (termios->c_cflag & CSTOPB)
  562. lcr |= MEN_Z135_NSTB2 << MEN_Z135_STB_SHIFT;
  563. /* parity */
  564. if (termios->c_cflag & PARENB) {
  565. lcr |= MEN_Z135_PAR_ENA << MEN_Z135_PEN_SHIFT;
  566. if (termios->c_cflag & PARODD)
  567. lcr |= MEN_Z135_PTY_ODD << MEN_Z135_PTY_SHIFT;
  568. else
  569. lcr |= MEN_Z135_PTY_EVN << MEN_Z135_PTY_SHIFT;
  570. } else
  571. lcr |= MEN_Z135_PAR_DIS << MEN_Z135_PEN_SHIFT;
  572. conf_reg |= MEN_Z135_IER_MSIEN;
  573. if (termios->c_cflag & CRTSCTS) {
  574. conf_reg |= MEN_Z135_MCR_RCFC;
  575. uart->automode = true;
  576. termios->c_cflag &= ~CLOCAL;
  577. } else {
  578. conf_reg &= ~MEN_Z135_MCR_RCFC;
  579. uart->automode = false;
  580. }
  581. termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
  582. conf_reg |= lcr << MEN_Z135_LCR_SHIFT;
  583. iowrite32(conf_reg, port->membase + MEN_Z135_CONF_REG);
  584. uart_freq = ioread32(port->membase + MEN_Z135_UART_FREQ);
  585. if (uart_freq == 0)
  586. uart_freq = MEN_Z135_BASECLK;
  587. baud = uart_get_baud_rate(port, termios, old, 0, uart_freq / 16);
  588. spin_lock_irq(&port->lock);
  589. if (tty_termios_baud_rate(termios))
  590. tty_termios_encode_baud_rate(termios, baud, baud);
  591. bd_reg = uart_freq / (4 * baud);
  592. iowrite32(bd_reg, port->membase + MEN_Z135_BAUD_REG);
  593. uart_update_timeout(port, termios->c_cflag, baud);
  594. spin_unlock_irq(&port->lock);
  595. }
  596. static const char *men_z135_type(struct uart_port *port)
  597. {
  598. return KBUILD_MODNAME;
  599. }
  600. static void men_z135_release_port(struct uart_port *port)
  601. {
  602. struct men_z135_port *uart = to_men_z135(port);
  603. iounmap(port->membase);
  604. port->membase = NULL;
  605. mcb_release_mem(uart->mem);
  606. }
  607. static int men_z135_request_port(struct uart_port *port)
  608. {
  609. struct men_z135_port *uart = to_men_z135(port);
  610. struct mcb_device *mdev = uart->mdev;
  611. struct resource *mem;
  612. mem = mcb_request_mem(uart->mdev, dev_name(&mdev->dev));
  613. if (IS_ERR(mem))
  614. return PTR_ERR(mem);
  615. port->mapbase = mem->start;
  616. uart->mem = mem;
  617. port->membase = ioremap(mem->start, resource_size(mem));
  618. if (port->membase == NULL) {
  619. mcb_release_mem(mem);
  620. return -ENOMEM;
  621. }
  622. return 0;
  623. }
  624. static void men_z135_config_port(struct uart_port *port, int type)
  625. {
  626. port->type = PORT_MEN_Z135;
  627. men_z135_request_port(port);
  628. }
  629. static int men_z135_verify_port(struct uart_port *port,
  630. struct serial_struct *serinfo)
  631. {
  632. return -EINVAL;
  633. }
  634. static const struct uart_ops men_z135_ops = {
  635. .tx_empty = men_z135_tx_empty,
  636. .set_mctrl = men_z135_set_mctrl,
  637. .get_mctrl = men_z135_get_mctrl,
  638. .stop_tx = men_z135_stop_tx,
  639. .start_tx = men_z135_start_tx,
  640. .stop_rx = men_z135_stop_rx,
  641. .enable_ms = men_z135_enable_ms,
  642. .startup = men_z135_startup,
  643. .shutdown = men_z135_shutdown,
  644. .set_termios = men_z135_set_termios,
  645. .type = men_z135_type,
  646. .release_port = men_z135_release_port,
  647. .request_port = men_z135_request_port,
  648. .config_port = men_z135_config_port,
  649. .verify_port = men_z135_verify_port,
  650. };
  651. static struct uart_driver men_z135_driver = {
  652. .owner = THIS_MODULE,
  653. .driver_name = KBUILD_MODNAME,
  654. .dev_name = "ttyHSU",
  655. .major = 0,
  656. .minor = 0,
  657. .nr = MEN_Z135_MAX_PORTS,
  658. };
  659. /**
  660. * men_z135_probe() - Probe a z135 instance
  661. * @mdev: The MCB device
  662. * @id: The MCB device ID
  663. *
  664. * men_z135_probe does the basic setup of hardware resources and registers the
  665. * new uart port to the tty layer.
  666. */
  667. static int men_z135_probe(struct mcb_device *mdev,
  668. const struct mcb_device_id *id)
  669. {
  670. struct men_z135_port *uart;
  671. struct resource *mem;
  672. struct device *dev;
  673. int err;
  674. dev = &mdev->dev;
  675. uart = devm_kzalloc(dev, sizeof(struct men_z135_port), GFP_KERNEL);
  676. if (!uart)
  677. return -ENOMEM;
  678. uart->rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  679. if (!uart->rxbuf)
  680. return -ENOMEM;
  681. mem = &mdev->mem;
  682. mcb_set_drvdata(mdev, uart);
  683. uart->port.uartclk = MEN_Z135_BASECLK * 16;
  684. uart->port.fifosize = MEN_Z135_FIFO_SIZE;
  685. uart->port.iotype = UPIO_MEM;
  686. uart->port.ops = &men_z135_ops;
  687. uart->port.irq = mcb_get_irq(mdev);
  688. uart->port.iotype = UPIO_MEM;
  689. uart->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
  690. uart->port.line = line++;
  691. uart->port.dev = dev;
  692. uart->port.type = PORT_MEN_Z135;
  693. uart->port.mapbase = mem->start;
  694. uart->port.membase = NULL;
  695. uart->mdev = mdev;
  696. spin_lock_init(&uart->lock);
  697. err = uart_add_one_port(&men_z135_driver, &uart->port);
  698. if (err)
  699. goto err;
  700. return 0;
  701. err:
  702. free_page((unsigned long) uart->rxbuf);
  703. dev_err(dev, "Failed to add UART: %d\n", err);
  704. return err;
  705. }
  706. /**
  707. * men_z135_remove() - Remove a z135 instance from the system
  708. *
  709. * @mdev: The MCB device
  710. */
  711. static void men_z135_remove(struct mcb_device *mdev)
  712. {
  713. struct men_z135_port *uart = mcb_get_drvdata(mdev);
  714. line--;
  715. uart_remove_one_port(&men_z135_driver, &uart->port);
  716. free_page((unsigned long) uart->rxbuf);
  717. }
  718. static const struct mcb_device_id men_z135_ids[] = {
  719. { .device = 0x87 },
  720. { }
  721. };
  722. MODULE_DEVICE_TABLE(mcb, men_z135_ids);
  723. static struct mcb_driver mcb_driver = {
  724. .driver = {
  725. .name = "z135-uart",
  726. .owner = THIS_MODULE,
  727. },
  728. .probe = men_z135_probe,
  729. .remove = men_z135_remove,
  730. .id_table = men_z135_ids,
  731. };
  732. /**
  733. * men_z135_init() - Driver Registration Routine
  734. *
  735. * men_z135_init is the first routine called when the driver is loaded. All it
  736. * does is register with the legacy MEN Chameleon subsystem.
  737. */
  738. static int __init men_z135_init(void)
  739. {
  740. int err;
  741. err = uart_register_driver(&men_z135_driver);
  742. if (err) {
  743. pr_err("Failed to register UART: %d\n", err);
  744. return err;
  745. }
  746. err = mcb_register_driver(&mcb_driver);
  747. if (err) {
  748. pr_err("Failed to register MCB driver: %d\n", err);
  749. uart_unregister_driver(&men_z135_driver);
  750. return err;
  751. }
  752. return 0;
  753. }
  754. module_init(men_z135_init);
  755. /**
  756. * men_z135_exit() - Driver Exit Routine
  757. *
  758. * men_z135_exit is called just before the driver is removed from memory.
  759. */
  760. static void __exit men_z135_exit(void)
  761. {
  762. mcb_unregister_driver(&mcb_driver);
  763. uart_unregister_driver(&men_z135_driver);
  764. }
  765. module_exit(men_z135_exit);
  766. MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
  767. MODULE_LICENSE("GPL v2");
  768. MODULE_DESCRIPTION("MEN 16z135 High Speed UART");
  769. MODULE_ALIAS("mcb:16z135");