men_z135_uart.c 21 KB

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