men_z135_uart.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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 MEN_Z135_MEM_SIZE 0x818
  35. #define IRQ_ID(x) ((x) & 0x1f)
  36. #define MEN_Z135_IER_RXCIEN BIT(0) /* RX Space IRQ */
  37. #define MEN_Z135_IER_TXCIEN BIT(1) /* TX Space IRQ */
  38. #define MEN_Z135_IER_RLSIEN BIT(2) /* Receiver Line Status IRQ */
  39. #define MEN_Z135_IER_MSIEN BIT(3) /* Modem Status IRQ */
  40. #define MEN_Z135_ALL_IRQS (MEN_Z135_IER_RXCIEN \
  41. | MEN_Z135_IER_RLSIEN \
  42. | MEN_Z135_IER_MSIEN \
  43. | MEN_Z135_IER_TXCIEN)
  44. #define MEN_Z135_MCR_DTR BIT(24)
  45. #define MEN_Z135_MCR_RTS BIT(25)
  46. #define MEN_Z135_MCR_OUT1 BIT(26)
  47. #define MEN_Z135_MCR_OUT2 BIT(27)
  48. #define MEN_Z135_MCR_LOOP BIT(28)
  49. #define MEN_Z135_MCR_RCFC BIT(29)
  50. #define MEN_Z135_MSR_DCTS BIT(0)
  51. #define MEN_Z135_MSR_DDSR BIT(1)
  52. #define MEN_Z135_MSR_DRI BIT(2)
  53. #define MEN_Z135_MSR_DDCD BIT(3)
  54. #define MEN_Z135_MSR_CTS BIT(4)
  55. #define MEN_Z135_MSR_DSR BIT(5)
  56. #define MEN_Z135_MSR_RI BIT(6)
  57. #define MEN_Z135_MSR_DCD BIT(7)
  58. #define MEN_Z135_LCR_SHIFT 8 /* LCR shift mask */
  59. #define MEN_Z135_WL5 0 /* CS5 */
  60. #define MEN_Z135_WL6 1 /* CS6 */
  61. #define MEN_Z135_WL7 2 /* CS7 */
  62. #define MEN_Z135_WL8 3 /* CS8 */
  63. #define MEN_Z135_STB_SHIFT 2 /* Stopbits */
  64. #define MEN_Z135_NSTB1 0
  65. #define MEN_Z135_NSTB2 1
  66. #define MEN_Z135_PEN_SHIFT 3 /* Parity enable */
  67. #define MEN_Z135_PAR_DIS 0
  68. #define MEN_Z135_PAR_ENA 1
  69. #define MEN_Z135_PTY_SHIFT 4 /* Parity type */
  70. #define MEN_Z135_PTY_ODD 0
  71. #define MEN_Z135_PTY_EVN 1
  72. #define MEN_Z135_LSR_DR BIT(0)
  73. #define MEN_Z135_LSR_OE BIT(1)
  74. #define MEN_Z135_LSR_PE BIT(2)
  75. #define MEN_Z135_LSR_FE BIT(3)
  76. #define MEN_Z135_LSR_BI BIT(4)
  77. #define MEN_Z135_LSR_THEP BIT(5)
  78. #define MEN_Z135_LSR_TEXP BIT(6)
  79. #define MEN_Z135_LSR_RXFIFOERR BIT(7)
  80. #define MEN_Z135_IRQ_ID_RLS BIT(0)
  81. #define MEN_Z135_IRQ_ID_RDA BIT(1)
  82. #define MEN_Z135_IRQ_ID_CTI BIT(2)
  83. #define MEN_Z135_IRQ_ID_TSA BIT(3)
  84. #define MEN_Z135_IRQ_ID_MST BIT(4)
  85. #define LCR(x) (((x) >> MEN_Z135_LCR_SHIFT) & 0xff)
  86. #define BYTES_TO_ALIGN(x) ((x) & 0x3)
  87. static int line;
  88. static int txlvl = 5;
  89. module_param(txlvl, int, S_IRUGO);
  90. MODULE_PARM_DESC(txlvl, "TX IRQ trigger level 0-7, default 5 (128 byte)");
  91. static int rxlvl = 6;
  92. module_param(rxlvl, int, S_IRUGO);
  93. MODULE_PARM_DESC(rxlvl, "RX IRQ trigger level 0-7, default 6 (256 byte)");
  94. static int align;
  95. module_param(align, int, S_IRUGO);
  96. MODULE_PARM_DESC(align, "Keep hardware FIFO write pointer aligned, default 0");
  97. static uint rx_timeout;
  98. module_param(rx_timeout, uint, S_IRUGO);
  99. MODULE_PARM_DESC(rx_timeout, "RX timeout. "
  100. "Timeout in seconds = (timeout_reg * baud_reg * 4) / freq_reg");
  101. struct men_z135_port {
  102. struct uart_port port;
  103. struct mcb_device *mdev;
  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 inline 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. unsigned long flags;
  324. int irq_id;
  325. uart->stat_reg = ioread32(port->membase + MEN_Z135_STAT_REG);
  326. irq_id = IRQ_ID(uart->stat_reg);
  327. if (!irq_id)
  328. goto out;
  329. spin_lock_irqsave(&port->lock, flags);
  330. /* It's save to write to IIR[7:6] RXC[9:8] */
  331. iowrite8(irq_id, port->membase + MEN_Z135_STAT_REG);
  332. if (irq_id & MEN_Z135_IRQ_ID_RLS) {
  333. men_z135_handle_lsr(uart);
  334. handled = true;
  335. }
  336. if (irq_id & (MEN_Z135_IRQ_ID_RDA | MEN_Z135_IRQ_ID_CTI)) {
  337. if (irq_id & MEN_Z135_IRQ_ID_CTI)
  338. dev_dbg(&uart->mdev->dev, "Character Timeout Indication\n");
  339. men_z135_handle_rx(uart);
  340. handled = true;
  341. }
  342. if (irq_id & MEN_Z135_IRQ_ID_TSA) {
  343. men_z135_handle_tx(uart);
  344. handled = true;
  345. }
  346. if (irq_id & MEN_Z135_IRQ_ID_MST) {
  347. men_z135_handle_modem_status(uart);
  348. handled = true;
  349. }
  350. spin_unlock_irqrestore(&port->lock, flags);
  351. out:
  352. return IRQ_RETVAL(handled);
  353. }
  354. /**
  355. * men_z135_request_irq() - Request IRQ for 16z135 core
  356. * @uart: z135 private uart port structure
  357. *
  358. * Request an IRQ for 16z135 to use. First try using MSI, if it fails
  359. * fall back to using legacy interrupts.
  360. */
  361. static int men_z135_request_irq(struct men_z135_port *uart)
  362. {
  363. struct device *dev = &uart->mdev->dev;
  364. struct uart_port *port = &uart->port;
  365. int err = 0;
  366. err = request_irq(port->irq, men_z135_intr, IRQF_SHARED,
  367. "men_z135_intr", uart);
  368. if (err)
  369. dev_err(dev, "Error %d getting interrupt\n", err);
  370. return err;
  371. }
  372. /**
  373. * men_z135_tx_empty() - Handle tx_empty call
  374. * @port: The UART port
  375. *
  376. * This function tests whether the TX FIFO and shifter for the port
  377. * described by @port is empty.
  378. */
  379. static unsigned int men_z135_tx_empty(struct uart_port *port)
  380. {
  381. u32 wptr;
  382. u16 txc;
  383. wptr = ioread32(port->membase + MEN_Z135_TX_CTRL);
  384. txc = (wptr >> 16) & 0x3ff;
  385. if (txc == 0)
  386. return TIOCSER_TEMT;
  387. else
  388. return 0;
  389. }
  390. /**
  391. * men_z135_set_mctrl() - Set modem control lines
  392. * @port: The UART port
  393. * @mctrl: The modem control lines
  394. *
  395. * This function sets the modem control lines for a port described by @port
  396. * to the state described by @mctrl
  397. */
  398. static void men_z135_set_mctrl(struct uart_port *port, unsigned int mctrl)
  399. {
  400. u32 old;
  401. u32 conf_reg;
  402. conf_reg = old = ioread32(port->membase + MEN_Z135_CONF_REG);
  403. if (mctrl & TIOCM_RTS)
  404. conf_reg |= MEN_Z135_MCR_RTS;
  405. else
  406. conf_reg &= ~MEN_Z135_MCR_RTS;
  407. if (mctrl & TIOCM_DTR)
  408. conf_reg |= MEN_Z135_MCR_DTR;
  409. else
  410. conf_reg &= ~MEN_Z135_MCR_DTR;
  411. if (mctrl & TIOCM_OUT1)
  412. conf_reg |= MEN_Z135_MCR_OUT1;
  413. else
  414. conf_reg &= ~MEN_Z135_MCR_OUT1;
  415. if (mctrl & TIOCM_OUT2)
  416. conf_reg |= MEN_Z135_MCR_OUT2;
  417. else
  418. conf_reg &= ~MEN_Z135_MCR_OUT2;
  419. if (mctrl & TIOCM_LOOP)
  420. conf_reg |= MEN_Z135_MCR_LOOP;
  421. else
  422. conf_reg &= ~MEN_Z135_MCR_LOOP;
  423. if (conf_reg != old)
  424. iowrite32(conf_reg, port->membase + MEN_Z135_CONF_REG);
  425. }
  426. /**
  427. * men_z135_get_mctrl() - Get modem control lines
  428. * @port: The UART port
  429. *
  430. * Retruns the current state of modem control inputs.
  431. */
  432. static unsigned int men_z135_get_mctrl(struct uart_port *port)
  433. {
  434. unsigned int mctrl = 0;
  435. u8 msr;
  436. msr = ioread8(port->membase + MEN_Z135_STAT_REG + 1);
  437. if (msr & MEN_Z135_MSR_CTS)
  438. mctrl |= TIOCM_CTS;
  439. if (msr & MEN_Z135_MSR_DSR)
  440. mctrl |= TIOCM_DSR;
  441. if (msr & MEN_Z135_MSR_RI)
  442. mctrl |= TIOCM_RI;
  443. if (msr & MEN_Z135_MSR_DCD)
  444. mctrl |= TIOCM_CAR;
  445. return mctrl;
  446. }
  447. /**
  448. * men_z135_stop_tx() - Stop transmitting characters
  449. * @port: The UART port
  450. *
  451. * Stop transmitting characters. This might be due to CTS line becomming
  452. * inactive or the tty layer indicating we want to stop transmission due to
  453. * an XOFF character.
  454. */
  455. static void men_z135_stop_tx(struct uart_port *port)
  456. {
  457. struct men_z135_port *uart = to_men_z135(port);
  458. men_z135_reg_clr(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_TXCIEN);
  459. }
  460. /*
  461. * men_z135_disable_ms() - Disable Modem Status
  462. * port: The UART port
  463. *
  464. * Enable Modem Status IRQ.
  465. */
  466. static void men_z135_disable_ms(struct uart_port *port)
  467. {
  468. struct men_z135_port *uart = to_men_z135(port);
  469. men_z135_reg_clr(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_MSIEN);
  470. }
  471. /**
  472. * men_z135_start_tx() - Start transmitting characters
  473. * @port: The UART port
  474. *
  475. * Start transmitting character. This actually doesn't transmit anything, but
  476. * fires off the TX tasklet.
  477. */
  478. static void men_z135_start_tx(struct uart_port *port)
  479. {
  480. struct men_z135_port *uart = to_men_z135(port);
  481. if (uart->automode)
  482. men_z135_disable_ms(port);
  483. men_z135_handle_tx(uart);
  484. }
  485. /**
  486. * men_z135_stop_rx() - Stop receiving characters
  487. * @port: The UART port
  488. *
  489. * Stop receiving characters; the port is in the process of being closed.
  490. */
  491. static void men_z135_stop_rx(struct uart_port *port)
  492. {
  493. struct men_z135_port *uart = to_men_z135(port);
  494. men_z135_reg_clr(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_RXCIEN);
  495. }
  496. /**
  497. * men_z135_enable_ms() - Enable Modem Status
  498. * port:
  499. *
  500. * Enable Modem Status IRQ.
  501. */
  502. static void men_z135_enable_ms(struct uart_port *port)
  503. {
  504. struct men_z135_port *uart = to_men_z135(port);
  505. men_z135_reg_set(uart, MEN_Z135_CONF_REG, MEN_Z135_IER_MSIEN);
  506. }
  507. static int men_z135_startup(struct uart_port *port)
  508. {
  509. struct men_z135_port *uart = to_men_z135(port);
  510. int err;
  511. u32 conf_reg = 0;
  512. err = men_z135_request_irq(uart);
  513. if (err)
  514. return -ENODEV;
  515. conf_reg = ioread32(port->membase + MEN_Z135_CONF_REG);
  516. /* Activate all but TX space available IRQ */
  517. conf_reg |= MEN_Z135_ALL_IRQS & ~MEN_Z135_IER_TXCIEN;
  518. conf_reg &= ~(0xff << 16);
  519. conf_reg |= (txlvl << 16);
  520. conf_reg |= (rxlvl << 20);
  521. iowrite32(conf_reg, port->membase + MEN_Z135_CONF_REG);
  522. if (rx_timeout)
  523. iowrite32(rx_timeout, port->membase + MEN_Z135_TIMEOUT);
  524. return 0;
  525. }
  526. static void men_z135_shutdown(struct uart_port *port)
  527. {
  528. struct men_z135_port *uart = to_men_z135(port);
  529. u32 conf_reg = 0;
  530. conf_reg |= MEN_Z135_ALL_IRQS;
  531. men_z135_reg_clr(uart, MEN_Z135_CONF_REG, conf_reg);
  532. free_irq(uart->port.irq, uart);
  533. }
  534. static void men_z135_set_termios(struct uart_port *port,
  535. struct ktermios *termios,
  536. struct ktermios *old)
  537. {
  538. struct men_z135_port *uart = to_men_z135(port);
  539. unsigned int baud;
  540. u32 conf_reg;
  541. u32 bd_reg;
  542. u32 uart_freq;
  543. u8 lcr;
  544. conf_reg = ioread32(port->membase + MEN_Z135_CONF_REG);
  545. lcr = LCR(conf_reg);
  546. /* byte size */
  547. switch (termios->c_cflag & CSIZE) {
  548. case CS5:
  549. lcr |= MEN_Z135_WL5;
  550. break;
  551. case CS6:
  552. lcr |= MEN_Z135_WL6;
  553. break;
  554. case CS7:
  555. lcr |= MEN_Z135_WL7;
  556. break;
  557. case CS8:
  558. lcr |= MEN_Z135_WL8;
  559. break;
  560. }
  561. /* stop bits */
  562. if (termios->c_cflag & CSTOPB)
  563. lcr |= MEN_Z135_NSTB2 << MEN_Z135_STB_SHIFT;
  564. /* parity */
  565. if (termios->c_cflag & PARENB) {
  566. lcr |= MEN_Z135_PAR_ENA << MEN_Z135_PEN_SHIFT;
  567. if (termios->c_cflag & PARODD)
  568. lcr |= MEN_Z135_PTY_ODD << MEN_Z135_PTY_SHIFT;
  569. else
  570. lcr |= MEN_Z135_PTY_EVN << MEN_Z135_PTY_SHIFT;
  571. } else
  572. lcr |= MEN_Z135_PAR_DIS << MEN_Z135_PEN_SHIFT;
  573. conf_reg |= MEN_Z135_IER_MSIEN;
  574. if (termios->c_cflag & CRTSCTS) {
  575. conf_reg |= MEN_Z135_MCR_RCFC;
  576. uart->automode = true;
  577. termios->c_cflag &= ~CLOCAL;
  578. } else {
  579. conf_reg &= ~MEN_Z135_MCR_RCFC;
  580. uart->automode = false;
  581. }
  582. termios->c_cflag &= ~CMSPAR; /* Mark/Space parity is not supported */
  583. conf_reg |= lcr << MEN_Z135_LCR_SHIFT;
  584. iowrite32(conf_reg, port->membase + MEN_Z135_CONF_REG);
  585. uart_freq = ioread32(port->membase + MEN_Z135_UART_FREQ);
  586. if (uart_freq == 0)
  587. uart_freq = MEN_Z135_BASECLK;
  588. baud = uart_get_baud_rate(port, termios, old, 0, uart_freq / 16);
  589. spin_lock(&port->lock);
  590. if (tty_termios_baud_rate(termios))
  591. tty_termios_encode_baud_rate(termios, baud, baud);
  592. bd_reg = uart_freq / (4 * baud);
  593. iowrite32(bd_reg, port->membase + MEN_Z135_BAUD_REG);
  594. uart_update_timeout(port, termios->c_cflag, baud);
  595. spin_unlock(&port->lock);
  596. }
  597. static const char *men_z135_type(struct uart_port *port)
  598. {
  599. return KBUILD_MODNAME;
  600. }
  601. static void men_z135_release_port(struct uart_port *port)
  602. {
  603. iounmap(port->membase);
  604. port->membase = NULL;
  605. release_mem_region(port->mapbase, MEN_Z135_MEM_SIZE);
  606. }
  607. static int men_z135_request_port(struct uart_port *port)
  608. {
  609. int size = MEN_Z135_MEM_SIZE;
  610. if (!request_mem_region(port->mapbase, size, "men_z135_port"))
  611. return -EBUSY;
  612. port->membase = ioremap(port->mapbase, MEN_Z135_MEM_SIZE);
  613. if (port->membase == NULL) {
  614. release_mem_region(port->mapbase, MEN_Z135_MEM_SIZE);
  615. return -ENOMEM;
  616. }
  617. return 0;
  618. }
  619. static void men_z135_config_port(struct uart_port *port, int type)
  620. {
  621. port->type = PORT_MEN_Z135;
  622. men_z135_request_port(port);
  623. }
  624. static int men_z135_verify_port(struct uart_port *port,
  625. struct serial_struct *serinfo)
  626. {
  627. return -EINVAL;
  628. }
  629. static struct uart_ops men_z135_ops = {
  630. .tx_empty = men_z135_tx_empty,
  631. .set_mctrl = men_z135_set_mctrl,
  632. .get_mctrl = men_z135_get_mctrl,
  633. .stop_tx = men_z135_stop_tx,
  634. .start_tx = men_z135_start_tx,
  635. .stop_rx = men_z135_stop_rx,
  636. .enable_ms = men_z135_enable_ms,
  637. .startup = men_z135_startup,
  638. .shutdown = men_z135_shutdown,
  639. .set_termios = men_z135_set_termios,
  640. .type = men_z135_type,
  641. .release_port = men_z135_release_port,
  642. .request_port = men_z135_request_port,
  643. .config_port = men_z135_config_port,
  644. .verify_port = men_z135_verify_port,
  645. };
  646. static struct uart_driver men_z135_driver = {
  647. .owner = THIS_MODULE,
  648. .driver_name = KBUILD_MODNAME,
  649. .dev_name = "ttyHSU",
  650. .major = 0,
  651. .minor = 0,
  652. .nr = MEN_Z135_MAX_PORTS,
  653. };
  654. /**
  655. * men_z135_probe() - Probe a z135 instance
  656. * @mdev: The MCB device
  657. * @id: The MCB device ID
  658. *
  659. * men_z135_probe does the basic setup of hardware resources and registers the
  660. * new uart port to the tty layer.
  661. */
  662. static int men_z135_probe(struct mcb_device *mdev,
  663. const struct mcb_device_id *id)
  664. {
  665. struct men_z135_port *uart;
  666. struct resource *mem;
  667. struct device *dev;
  668. int err;
  669. dev = &mdev->dev;
  670. uart = devm_kzalloc(dev, sizeof(struct men_z135_port), GFP_KERNEL);
  671. if (!uart)
  672. return -ENOMEM;
  673. uart->rxbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
  674. if (!uart->rxbuf)
  675. return -ENOMEM;
  676. mem = &mdev->mem;
  677. mcb_set_drvdata(mdev, uart);
  678. uart->port.uartclk = MEN_Z135_BASECLK * 16;
  679. uart->port.fifosize = MEN_Z135_FIFO_SIZE;
  680. uart->port.iotype = UPIO_MEM;
  681. uart->port.ops = &men_z135_ops;
  682. uart->port.irq = mcb_get_irq(mdev);
  683. uart->port.iotype = UPIO_MEM;
  684. uart->port.flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
  685. uart->port.line = line++;
  686. uart->port.dev = dev;
  687. uart->port.type = PORT_MEN_Z135;
  688. uart->port.mapbase = mem->start;
  689. uart->port.membase = NULL;
  690. uart->mdev = mdev;
  691. spin_lock_init(&uart->port.lock);
  692. spin_lock_init(&uart->lock);
  693. err = uart_add_one_port(&men_z135_driver, &uart->port);
  694. if (err)
  695. goto err;
  696. return 0;
  697. err:
  698. free_page((unsigned long) uart->rxbuf);
  699. dev_err(dev, "Failed to add UART: %d\n", err);
  700. return err;
  701. }
  702. /**
  703. * men_z135_remove() - Remove a z135 instance from the system
  704. *
  705. * @mdev: The MCB device
  706. */
  707. static void men_z135_remove(struct mcb_device *mdev)
  708. {
  709. struct men_z135_port *uart = mcb_get_drvdata(mdev);
  710. line--;
  711. uart_remove_one_port(&men_z135_driver, &uart->port);
  712. free_page((unsigned long) uart->rxbuf);
  713. }
  714. static const struct mcb_device_id men_z135_ids[] = {
  715. { .device = 0x87 },
  716. { }
  717. };
  718. MODULE_DEVICE_TABLE(mcb, men_z135_ids);
  719. static struct mcb_driver mcb_driver = {
  720. .driver = {
  721. .name = "z135-uart",
  722. .owner = THIS_MODULE,
  723. },
  724. .probe = men_z135_probe,
  725. .remove = men_z135_remove,
  726. .id_table = men_z135_ids,
  727. };
  728. /**
  729. * men_z135_init() - Driver Registration Routine
  730. *
  731. * men_z135_init is the first routine called when the driver is loaded. All it
  732. * does is register with the legacy MEN Chameleon subsystem.
  733. */
  734. static int __init men_z135_init(void)
  735. {
  736. int err;
  737. err = uart_register_driver(&men_z135_driver);
  738. if (err) {
  739. pr_err("Failed to register UART: %d\n", err);
  740. return err;
  741. }
  742. err = mcb_register_driver(&mcb_driver);
  743. if (err) {
  744. pr_err("Failed to register MCB driver: %d\n", err);
  745. uart_unregister_driver(&men_z135_driver);
  746. return err;
  747. }
  748. return 0;
  749. }
  750. module_init(men_z135_init);
  751. /**
  752. * men_z135_exit() - Driver Exit Routine
  753. *
  754. * men_z135_exit is called just before the driver is removed from memory.
  755. */
  756. static void __exit men_z135_exit(void)
  757. {
  758. mcb_unregister_driver(&mcb_driver);
  759. uart_unregister_driver(&men_z135_driver);
  760. }
  761. module_exit(men_z135_exit);
  762. MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
  763. MODULE_LICENSE("GPL v2");
  764. MODULE_DESCRIPTION("MEN 16z135 High Speed UART");
  765. MODULE_ALIAS("mcb:16z135");