blob: 87b264457fb46902f633c953515f9b45bcf8796f [file] [log] [blame]
Girish Mahadevanebeed352016-11-23 10:59:29 -07001/*
2 * Copyright (c) 2017, The Linux foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/bitmap.h>
15#include <linux/bitops.h>
16#include <linux/debugfs.h>
17#include <linux/delay.h>
18#include <linux/console.h>
19#include <linux/io.h>
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/of_device.h>
23#include <linux/platform_device.h>
Karthikeyan Ramasubramanian9d88c722017-04-06 16:04:39 -060024#include <linux/pm_runtime.h>
Girish Mahadevanebeed352016-11-23 10:59:29 -070025#include <linux/qcom-geni-se.h>
26#include <linux/serial.h>
27#include <linux/serial_core.h>
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
30
31/* UART specific GENI registers */
32#define SE_UART_LOOPBACK_CFG (0x22C)
33#define SE_UART_TX_TRANS_CFG (0x25C)
34#define SE_UART_TX_WORD_LEN (0x268)
35#define SE_UART_TX_STOP_BIT_LEN (0x26C)
36#define SE_UART_TX_TRANS_LEN (0x270)
37#define SE_UART_RX_TRANS_CFG (0x280)
38#define SE_UART_RX_WORD_LEN (0x28C)
39#define SE_UART_RX_STALE_CNT (0x294)
40#define SE_UART_TX_PARITY_CFG (0x2A4)
41#define SE_UART_RX_PARITY_CFG (0x2A8)
42#define SE_UART_MANUAL_RFT (0x2AC)
43
44/* SE_UART_LOOPBACK_CFG */
45#define NO_LOOPBACK (0)
46#define TX_RX_LOOPBACK (0x1)
47#define CTS_RFR_LOOPBACK (0x2)
48#define CTSRFR_TXRX_LOOPBACK (0x3)
49
50/* SE_UART_TRANS_CFG */
51#define UART_TX_PAR_EN (BIT(0))
52#define UART_CTS_MASK (BIT(1))
53
54/* SE_UART_TX_WORD_LEN */
55#define TX_WORD_LEN_MSK (GENMASK(9, 0))
56
57/* SE_UART_TX_STOP_BIT_LEN */
58#define TX_STOP_BIT_LEN_MSK (GENMASK(23, 0))
59#define TX_STOP_BIT_LEN_1 (0)
60#define TX_STOP_BIT_LEN_1_5 (1)
61#define TX_STOP_BIT_LEN_2 (2)
62
63/* SE_UART_TX_TRANS_LEN */
64#define TX_TRANS_LEN_MSK (GENMASK(23, 0))
65
66/* SE_UART_RX_TRANS_CFG */
67#define UART_RX_INS_STATUS_BIT (BIT(2))
68#define UART_RX_PAR_EN (BIT(3))
69
70/* SE_UART_RX_WORD_LEN */
71#define RX_WORD_LEN_MASK (GENMASK(9, 0))
72
73/* SE_UART_RX_STALE_CNT */
74#define RX_STALE_CNT (GENMASK(23, 0))
75
76/* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
77#define PAR_CALC_EN (BIT(0))
78#define PAR_MODE_MSK (GENMASK(2, 1))
79#define PAR_MODE_SHFT (1)
80#define PAR_EVEN (0x00)
81#define PAR_ODD (0x01)
82#define PAR_SPACE (0x10)
83#define PAR_MARK (0x11)
84
85/* UART M_CMD OP codes */
86#define UART_START_TX (0x1)
87#define UART_START_BREAK (0x4)
88#define UART_STOP_BREAK (0x5)
89/* UART S_CMD OP codes */
90#define UART_START_READ (0x1)
91#define UART_PARAM (0x1)
92
93#define UART_OVERSAMPLING (32)
94#define STALE_TIMEOUT (16)
95#define GENI_UART_NR_PORTS (15)
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -060096#define DEF_FIFO_DEPTH_WORDS (16)
Girish Mahadevanebeed352016-11-23 10:59:29 -070097#define DEF_FIFO_WIDTH_BITS (32)
98
99struct msm_geni_serial_port {
100 struct uart_port uport;
101 char name[20];
102 unsigned int tx_fifo_depth;
103 unsigned int tx_fifo_width;
104 unsigned int rx_fifo_depth;
105 unsigned int tx_wm;
106 unsigned int rx_wm;
107 unsigned int rx_rfr;
108 int xfer_mode;
109 struct dentry *dbg;
110 bool port_setup;
111 unsigned int *rx_fifo;
112 int (*handle_rx)(struct uart_port *uport,
113 unsigned int rx_fifo_wc,
114 unsigned int rx_last_byte_valid,
115 unsigned int rx_last);
116 struct se_geni_rsc serial_rsc;
117 int loopback;
118};
119
120static const struct uart_ops msm_geni_serial_pops;
121static struct uart_driver msm_geni_console_driver;
122static struct uart_driver msm_geni_serial_hs_driver;
123static int handle_rx_console(struct uart_port *uport,
124 unsigned int rx_fifo_wc,
125 unsigned int rx_last_byte_valid,
126 unsigned int rx_last);
127static int handle_rx_hs(struct uart_port *uport,
128 unsigned int rx_fifo_wc,
129 unsigned int rx_last_byte_valid,
130 unsigned int rx_last);
131
132static atomic_t uart_line_id = ATOMIC_INIT(0);
133
134#define GET_DEV_PORT(uport) \
135 container_of(uport, struct msm_geni_serial_port, uport)
136
137static struct msm_geni_serial_port msm_geni_serial_ports[GENI_UART_NR_PORTS];
138
139static void msm_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
140{
141 if (cfg_flags & UART_CONFIG_TYPE)
142 uport->type = PORT_MSM;
143}
144
145static ssize_t msm_geni_serial_loopback_show(struct device *dev,
146 struct device_attribute *attr, char *buf)
147{
148 struct platform_device *pdev = to_platform_device(dev);
149 struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
150
151 return snprintf(buf, sizeof(int), "%d\n", port->loopback);
152}
153
154static ssize_t msm_geni_serial_loopback_store(struct device *dev,
155 struct device_attribute *attr, const char *buf,
156 size_t size)
157{
158 struct platform_device *pdev = to_platform_device(dev);
159 struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
160
161 if (kstrtoint(buf, 0, &port->loopback)) {
162 dev_err(dev, "Invalid input\n");
163 return -EINVAL;
164 }
165 return size;
166}
167
168static DEVICE_ATTR(loopback, 0644, msm_geni_serial_loopback_show,
169 msm_geni_serial_loopback_store);
170
171static void msm_geni_serial_set_mctrl(struct uart_port *port,
172 unsigned int mctrl)
173{
174}
175
176static const char *msm_geni_serial_get_type(struct uart_port *uport)
177{
178 return "MSM";
179}
180
181static struct msm_geni_serial_port *get_port_from_line(int line)
182{
183 if ((line < 0) || (line >= GENI_UART_NR_PORTS))
184 return ERR_PTR(-ENXIO);
185
186 return &msm_geni_serial_ports[line];
187}
188
189static int msm_geni_serial_power_on(struct uart_port *uport)
190{
191 int ret = 0;
192
193 ret = pm_runtime_get_sync(uport->dev);
194 if (ret < 0) {
195 dev_err(uport->dev, "%s: Failed (%d)", __func__, ret);
196 pm_runtime_put_noidle(uport->dev);
197 }
198 return ret;
199}
200
201static void msm_geni_serial_power_off(struct uart_port *uport)
202{
203 pm_runtime_mark_last_busy(uport->dev);
204 pm_runtime_put_autosuspend(uport->dev);
205}
206
207static int msm_geni_serial_poll_bit(struct uart_port *uport,
208 int offset, int bit_field)
209{
210 int iter = 0;
211 unsigned int reg;
212 bool met = false;
213
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600214 while (iter < 1000) {
215 reg = geni_read_reg_nolog(uport->membase, offset);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700216 if (reg & bit_field) {
217 met = true;
218 break;
219 }
220 udelay(10);
221 iter++;
222 }
223 return met;
224}
225
226static void msm_geni_serial_setup_tx(struct uart_port *uport,
227 unsigned int xmit_size)
228{
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600229 geni_write_reg_nolog(xmit_size, uport->membase, SE_UART_TX_TRANS_LEN);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700230 geni_setup_m_cmd(uport->membase, UART_START_TX, 0);
231 /*
232 * Writes to enable the primary sequencer should go through before
233 * exiting this function.
234 */
235 mb();
236}
237
238static void msm_geni_serial_poll_cancel_tx(struct uart_port *uport)
239{
240 int done = 0;
241 unsigned int irq_clear = M_CMD_DONE_EN;
242
243 done = msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
244 M_CMD_DONE_EN);
245 if (!done) {
246 geni_cancel_m_cmd(uport->membase);
247 irq_clear |= M_CMD_CANCEL_EN;
248 if (!msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
249 M_CMD_CANCEL_EN)) {
250 geni_abort_m_cmd(uport->membase);
251 irq_clear |= M_CMD_ABORT_EN;
252 msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
253 M_CMD_ABORT_EN);
254 }
255 }
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600256 geni_write_reg_nolog(irq_clear, uport->membase, SE_GENI_M_IRQ_CLEAR);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700257}
258
259#ifdef CONFIG_CONSOLE_POLL
260static int msm_geni_serial_get_char(struct uart_port *uport)
261{
262 unsigned int rx_fifo;
263 unsigned int m_irq_status;
264 unsigned int s_irq_status;
265
266 if (!(msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
267 M_SEC_IRQ_EN))) {
268 dev_err(uport->dev, "%s: Failed waiting for SE\n", __func__);
269 return -ENXIO;
270 }
271
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600272 m_irq_status = geni_read_reg_nolog(uport->membase,
273 SE_GENI_M_IRQ_STATUS);
274 s_irq_status = geni_read_reg_nolog(uport->membase,
275 SE_GENI_S_IRQ_STATUS);
276 geni_write_reg_nolog(m_irq_status, uport->membase, SE_GENI_M_IRQ_CLEAR);
277 geni_write_reg_nolog(s_irq_status, uport->membase, SE_GENI_S_IRQ_CLEAR);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700278
279 if (!(msm_geni_serial_poll_bit(uport, SE_GENI_RX_FIFO_STATUS,
280 RX_FIFO_WC_MSK))) {
281 dev_err(uport->dev, "%s: Failed waiting for Rx\n", __func__);
282 return -ENXIO;
283 }
284
285 /*
286 * Read the Rx FIFO only after clearing the interrupt registers and
287 * getting valid RX fifo status.
288 */
289 mb();
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600290 rx_fifo = geni_read_reg_nolog(uport->membase, SE_GENI_RX_FIFOn);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700291 rx_fifo &= 0xFF;
292 return rx_fifo;
293}
294
295static void msm_geni_serial_poll_put_char(struct uart_port *uport,
296 unsigned char c)
297{
298 int b = (int) c;
299 struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
300
301 se_config_packing(uport->membase, 8, 1, false);
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600302 geni_write_reg_nolog(port->tx_wm, uport->membase,
Girish Mahadevanebeed352016-11-23 10:59:29 -0700303 SE_GENI_TX_WATERMARK_REG);
304 msm_geni_serial_setup_tx(uport, 1);
305 if (!msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
306 M_TX_FIFO_WATERMARK_EN))
307 WARN_ON(1);
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600308 geni_write_reg_nolog(b, uport->membase, SE_GENI_TX_FIFOn);
309 geni_write_reg_nolog(M_TX_FIFO_WATERMARK_EN, uport->membase,
Girish Mahadevanebeed352016-11-23 10:59:29 -0700310 SE_GENI_M_IRQ_CLEAR);
311 /*
312 * Ensure FIFO write goes through before polling for status but.
313 */
314 mb();
315 msm_geni_serial_poll_cancel_tx(uport);
316}
317#endif
318
Girish Mahadevanf08b1102017-04-02 19:27:28 -0600319#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
Girish Mahadevanebeed352016-11-23 10:59:29 -0700320static void msm_geni_serial_wr_char(struct uart_port *uport, int ch)
321{
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600322 geni_write_reg_nolog(ch, uport->membase, SE_GENI_TX_FIFOn);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700323 /*
324 * Ensure FIFO write clear goes through before
325 * next iteration.
326 */
327 mb();
328
329}
330
331static void
332__msm_geni_serial_console_write(struct uart_port *uport, const char *s,
333 unsigned int count)
334{
335 struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
336 int new_line = 0;
337 int i;
338 int bytes_to_send = count;
339
340 for (i = 0; i < count; i++) {
341 if (s[i] == '\n')
342 new_line++;
343 }
344
345 bytes_to_send += new_line;
346 se_config_packing(uport->membase, 8, 1, false);
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600347 geni_write_reg_nolog(port->tx_wm, uport->membase,
Girish Mahadevanebeed352016-11-23 10:59:29 -0700348 SE_GENI_TX_WATERMARK_REG);
349 msm_geni_serial_setup_tx(uport, bytes_to_send);
350 i = 0;
351 while (i < count) {
352 u32 chars_to_write = 0;
353 u32 avail_fifo_bytes = (port->tx_fifo_depth - port->tx_wm);
354
355 while (!msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600356 M_TX_FIFO_WATERMARK_EN))
Girish Mahadevanebeed352016-11-23 10:59:29 -0700357 cpu_relax();
358 chars_to_write = min((unsigned int)(count - i),
359 avail_fifo_bytes);
360 if ((chars_to_write << 1) > avail_fifo_bytes)
361 chars_to_write = (avail_fifo_bytes >> 1);
362 uart_console_write(uport, (s + i), chars_to_write,
363 msm_geni_serial_wr_char);
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600364 geni_write_reg_nolog(M_TX_FIFO_WATERMARK_EN, uport->membase,
Girish Mahadevanebeed352016-11-23 10:59:29 -0700365 SE_GENI_M_IRQ_CLEAR);
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600366 /* Ensure this goes through before polling for WM IRQ again.*/
367 mb();
Girish Mahadevanebeed352016-11-23 10:59:29 -0700368 i += chars_to_write;
369 }
370 msm_geni_serial_poll_cancel_tx(uport);
371}
372
373static void msm_geni_serial_console_write(struct console *co, const char *s,
374 unsigned int count)
375{
376 struct uart_port *uport;
377 struct msm_geni_serial_port *port;
378
379 WARN_ON(co->index < 0 || co->index >= GENI_UART_NR_PORTS);
380
381 port = get_port_from_line(co->index);
382 if (IS_ERR_OR_NULL(port)) {
383 pr_err("%s:Invalid line %d\n", __func__, co->index);
384 return;
385 }
386
387 uport = &port->uport;
388 spin_lock(&uport->lock);
389 __msm_geni_serial_console_write(uport, s, count);
390 spin_unlock(&uport->lock);
391}
392
Girish Mahadevanf08b1102017-04-02 19:27:28 -0600393static int handle_rx_console(struct uart_port *uport,
394 unsigned int rx_fifo_wc,
395 unsigned int rx_last_byte_valid,
396 unsigned int rx_last)
397{
398 int i, c;
399 unsigned char *rx_char;
400 struct tty_port *tport;
401 struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
402
403 tport = &uport->state->port;
404
405 for (i = 0; i < rx_fifo_wc; i++) {
406 int bytes = 4;
407
408 *(msm_port->rx_fifo) =
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600409 geni_read_reg_nolog(uport->membase, SE_GENI_RX_FIFOn);
Girish Mahadevanf08b1102017-04-02 19:27:28 -0600410 rx_char = (unsigned char *)msm_port->rx_fifo;
411
412 if (i == (rx_fifo_wc - 1)) {
413 if (rx_last && rx_last_byte_valid)
414 bytes = rx_last_byte_valid;
415 }
416 for (c = 0; c < bytes; c++) {
417 char flag = TTY_NORMAL;
418 int sysrq;
419
420 uport->icount.rx++;
421 sysrq = uart_handle_sysrq_char(uport, rx_char[c]);
422 if (!sysrq)
423 tty_insert_flip_char(tport, rx_char[c], flag);
424 }
425 }
426 tty_flip_buffer_push(tport);
427 return 0;
428}
429#else
430static int handle_rx_console(struct uart_port *uport,
431 unsigned int rx_fifo_wc,
432 unsigned int rx_last_byte_valid,
433 unsigned int rx_last)
434{
435 return -EPERM;
436}
437
438#endif /* (CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)) */
439
Girish Mahadevanebeed352016-11-23 10:59:29 -0700440static void msm_geni_serial_start_tx(struct uart_port *uport)
441{
442 unsigned int geni_m_irq_en;
443 struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
444
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600445 geni_m_irq_en = geni_read_reg_nolog(uport->membase, SE_GENI_M_IRQ_EN);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700446 geni_m_irq_en |= M_TX_FIFO_WATERMARK_EN;
447
448 se_config_packing(uport->membase, 8, 4, false);
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600449 geni_write_reg_nolog(port->tx_wm, uport->membase,
450 SE_GENI_TX_WATERMARK_REG);
451 geni_write_reg_nolog(geni_m_irq_en, uport->membase, SE_GENI_M_IRQ_EN);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700452 /* Geni command setup/irq enables should complete before returning.*/
453 mb();
454}
455
456static void msm_geni_serial_stop_tx(struct uart_port *uport)
457{
458 unsigned int geni_m_irq_en;
459 unsigned int geni_status;
460
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600461 geni_m_irq_en = geni_read_reg_nolog(uport->membase, SE_GENI_M_IRQ_EN);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700462 geni_m_irq_en &= ~(M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN);
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600463 geni_write_reg_nolog(0, uport->membase, SE_GENI_TX_WATERMARK_REG);
464 geni_write_reg_nolog(geni_m_irq_en, uport->membase, SE_GENI_M_IRQ_EN);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700465
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600466 geni_status = geni_read_reg_nolog(uport->membase,
Girish Mahadevanebeed352016-11-23 10:59:29 -0700467 SE_GENI_STATUS);
468 /* Possible stop tx is called multiple times. */
469 if (!(geni_status & M_GENI_CMD_ACTIVE))
470 return;
471
472 geni_cancel_m_cmd(uport->membase);
473 if (!msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
474 M_CMD_CANCEL_EN)) {
475 geni_abort_m_cmd(uport->membase);
476 msm_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
477 M_CMD_ABORT_EN);
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600478 geni_write_reg_nolog(M_CMD_ABORT_EN, uport->membase,
Girish Mahadevanebeed352016-11-23 10:59:29 -0700479 SE_GENI_M_IRQ_CLEAR);
480 }
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600481 geni_write_reg_nolog(M_CMD_CANCEL_EN, uport, SE_GENI_M_IRQ_CLEAR);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700482}
483
484static void msm_geni_serial_start_rx(struct uart_port *uport)
485{
486 unsigned int geni_s_irq_en;
487 unsigned int geni_m_irq_en;
488
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600489 geni_s_irq_en = geni_read_reg_nolog(uport->membase,
Girish Mahadevanebeed352016-11-23 10:59:29 -0700490 SE_GENI_S_IRQ_EN);
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600491 geni_m_irq_en = geni_read_reg_nolog(uport->membase,
Girish Mahadevanebeed352016-11-23 10:59:29 -0700492 SE_GENI_M_IRQ_EN);
493 geni_s_irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
494 geni_m_irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
495
496 geni_setup_s_cmd(uport->membase, UART_START_READ, 0);
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600497 geni_write_reg_nolog(geni_s_irq_en, uport->membase, SE_GENI_S_IRQ_EN);
498 geni_write_reg_nolog(geni_m_irq_en, uport->membase, SE_GENI_M_IRQ_EN);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700499 /*
500 * Ensure the writes to the secondary sequencer and interrupt enables
501 * go through.
502 */
503 mb();
504}
505
506static void msm_geni_serial_stop_rx(struct uart_port *uport)
507{
508 unsigned int geni_s_irq_en;
509 unsigned int geni_m_irq_en;
510 unsigned int geni_status;
511
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600512 geni_s_irq_en = geni_read_reg_nolog(uport->membase,
Girish Mahadevanebeed352016-11-23 10:59:29 -0700513 SE_GENI_S_IRQ_EN);
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600514 geni_m_irq_en = geni_read_reg_nolog(uport->membase,
Girish Mahadevanebeed352016-11-23 10:59:29 -0700515 SE_GENI_M_IRQ_EN);
516 geni_s_irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
517 geni_m_irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
518
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600519 geni_write_reg_nolog(geni_s_irq_en, uport->membase, SE_GENI_S_IRQ_EN);
520 geni_write_reg_nolog(geni_m_irq_en, uport->membase, SE_GENI_M_IRQ_EN);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700521
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600522 geni_status = geni_read_reg_nolog(uport->membase, SE_GENI_STATUS);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700523 /* Possible stop rx is called multiple times. */
524 if (!(geni_status & S_GENI_CMD_ACTIVE))
525 return;
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600526 geni_write_reg_nolog(S_GENI_CMD_CANCEL, uport->membase,
Girish Mahadevanebeed352016-11-23 10:59:29 -0700527 SE_GENI_S_CMD_CTRL_REG);
528 if (!msm_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
529 S_CMD_CANCEL_EN))
530 WARN_ON(1);
531}
532
Girish Mahadevanebeed352016-11-23 10:59:29 -0700533static int handle_rx_hs(struct uart_port *uport,
534 unsigned int rx_fifo_wc,
535 unsigned int rx_last_byte_valid,
536 unsigned int rx_last)
537{
538 unsigned char *rx_char;
539 struct tty_port *tport;
540 struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
541 int ret;
542 int rx_bytes = 0;
543
544 rx_bytes = (msm_port->tx_fifo_width * (rx_fifo_wc - 1)) >> 3;
545 rx_bytes += ((rx_last && rx_last_byte_valid) ?
546 rx_last_byte_valid : msm_port->tx_fifo_width >> 3);
547
548 tport = &uport->state->port;
549 ioread32_rep((uport->membase + SE_GENI_RX_FIFOn), msm_port->rx_fifo,
550 rx_fifo_wc);
551
552 rx_char = (unsigned char *)msm_port->rx_fifo;
553 ret = tty_insert_flip_string(tport, rx_char, rx_bytes);
554 if (ret != rx_bytes) {
555 dev_err(uport->dev, "%s: ret %d rx_bytes %d\n", __func__,
556 ret, rx_bytes);
557 WARN_ON(1);
558 }
559 uport->icount.rx += ret;
560 tty_flip_buffer_push(tport);
561 return ret;
562}
563
564static int msm_geni_serial_handle_rx(struct uart_port *uport)
565{
566 int ret = 0;
567 unsigned int rx_fifo_status;
568 unsigned int rx_fifo_wc = 0;
569 unsigned int rx_last_byte_valid = 0;
570 unsigned int rx_last = 0;
571 struct tty_port *tport;
572 struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
573
574 tport = &uport->state->port;
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600575 rx_fifo_status = geni_read_reg_nolog(uport->membase,
Girish Mahadevanebeed352016-11-23 10:59:29 -0700576 SE_GENI_RX_FIFO_STATUS);
577 rx_fifo_wc = rx_fifo_status & RX_FIFO_WC_MSK;
578 rx_last_byte_valid = ((rx_fifo_status & RX_LAST_BYTE_VALID_MSK) >>
579 RX_LAST_BYTE_VALID_SHFT);
580 rx_last = rx_fifo_status & RX_LAST;
581 if (rx_fifo_wc)
582 msm_port->handle_rx(uport, rx_fifo_wc, rx_last_byte_valid,
583 rx_last);
584 return ret;
585}
586
587static int msm_geni_serial_handle_tx(struct uart_port *uport)
588{
589 int ret = 0;
590 struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
591 struct circ_buf *xmit = &uport->state->xmit;
592 unsigned int avail_fifo_bytes = 0;
593 unsigned int bytes_remaining = 0;
594 int i = 0;
595 unsigned int tx_fifo_status;
596 unsigned int xmit_size;
597 unsigned int fifo_width_bytes = msm_port->tx_fifo_width >> 3;
598
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600599 tx_fifo_status = geni_read_reg_nolog(uport->membase,
Girish Mahadevanebeed352016-11-23 10:59:29 -0700600 SE_GENI_TX_FIFO_STATUS);
601 if (uart_circ_empty(xmit) && !tx_fifo_status) {
602 msm_geni_serial_stop_tx(uport);
603 goto exit_handle_tx;
604 }
605
606 avail_fifo_bytes = (msm_port->tx_fifo_depth - msm_port->tx_wm) *
607 fifo_width_bytes;
608 xmit_size = uart_circ_chars_pending(xmit);
609 if (xmit_size > (UART_XMIT_SIZE - xmit->tail))
610 xmit_size = UART_XMIT_SIZE - xmit->tail;
611 if (xmit_size > avail_fifo_bytes)
612 xmit_size = avail_fifo_bytes;
613
614 if (!xmit_size)
615 goto exit_handle_tx;
616
617 msm_geni_serial_setup_tx(uport, xmit_size);
618
619 bytes_remaining = xmit_size;
620 while (i < xmit_size) {
621 unsigned int tx_bytes;
622 unsigned int buf = 0;
623 int c;
624
625 tx_bytes = ((bytes_remaining < fifo_width_bytes) ?
626 bytes_remaining : fifo_width_bytes);
627
628 for (c = 0; c < tx_bytes ; c++)
629 buf |= (xmit->buf[xmit->tail + c] << (c * 8));
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600630 geni_write_reg_nolog(buf, uport->membase, SE_GENI_TX_FIFOn);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700631 xmit->tail = (xmit->tail + tx_bytes) & (UART_XMIT_SIZE - 1);
632 i += tx_bytes;
633 uport->icount.tx += tx_bytes;
634 bytes_remaining -= tx_bytes;
635 /* Ensure FIFO write goes through */
636 wmb();
637 }
638 msm_geni_serial_poll_cancel_tx(uport);
639exit_handle_tx:
640 return ret;
641}
642
643static irqreturn_t msm_geni_serial_isr(int isr, void *dev)
644{
645 unsigned int m_irq_status;
646 unsigned int s_irq_status;
647 struct uart_port *uport = dev;
648 unsigned long flags;
649
650 spin_lock_irqsave(&uport->lock, flags);
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600651 m_irq_status = geni_read_reg_nolog(uport->membase,
652 SE_GENI_M_IRQ_STATUS);
653 s_irq_status = geni_read_reg_nolog(uport->membase,
654 SE_GENI_S_IRQ_STATUS);
655 geni_write_reg_nolog(m_irq_status, uport->membase, SE_GENI_M_IRQ_CLEAR);
656 geni_write_reg_nolog(s_irq_status, uport->membase, SE_GENI_S_IRQ_CLEAR);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700657
658 if ((m_irq_status & M_ILLEGAL_CMD_EN)) {
659 WARN_ON(1);
660 goto exit_geni_serial_isr;
661 }
662
663 if ((s_irq_status & S_RX_FIFO_WATERMARK_EN) ||
664 (s_irq_status & S_RX_FIFO_LAST_EN)) {
665 msm_geni_serial_handle_rx(uport);
666 }
667
668 if ((m_irq_status & M_TX_FIFO_WATERMARK_EN))
669 msm_geni_serial_handle_tx(uport);
670
671exit_geni_serial_isr:
672 spin_unlock_irqrestore(&uport->lock, flags);
673 return IRQ_HANDLED;
674}
675
676static int get_tx_fifo_size(struct msm_geni_serial_port *port)
677{
678 struct uart_port *uport;
679
680 if (!port)
681 return -ENODEV;
682
683 uport = &port->uport;
684 port->tx_fifo_depth = get_tx_fifo_depth(uport->membase);
685 if (!port->tx_fifo_depth) {
686 dev_err(uport->dev, "%s:Invalid TX FIFO depth read\n",
687 __func__);
688 return -ENXIO;
689 }
690
691 port->tx_fifo_width = get_tx_fifo_width(uport->membase);
692 if (!port->tx_fifo_width) {
693 dev_err(uport->dev, "%s:Invalid TX FIFO width read\n",
694 __func__);
695 return -ENXIO;
696 }
697
698 port->rx_fifo_depth = get_rx_fifo_depth(uport->membase);
699 if (!port->rx_fifo_depth) {
700 dev_err(uport->dev, "%s:Invalid RX FIFO depth read\n",
701 __func__);
702 return -ENXIO;
703 }
704
705 uport->fifosize =
706 ((port->tx_fifo_depth * port->tx_fifo_width) >> 3);
707 return 0;
708}
709
710static void set_rfr_wm(struct msm_geni_serial_port *port)
711{
712 /*
713 * Set RFR (Flow off) to FIFO_DEPTH - 2.
714 * RX WM level at 50% RX_FIFO_DEPTH.
715 * TX WM level at 10% TX_FIFO_DEPTH.
716 */
717 port->rx_rfr = port->rx_fifo_depth - 2;
718 port->rx_wm = port->rx_fifo_depth >> 1;
719 port->tx_wm = 2;
720}
721
722static void msm_geni_serial_shutdown(struct uart_port *uport)
723{
724 struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
725
726 msm_geni_serial_stop_tx(uport);
727 msm_geni_serial_stop_rx(uport);
728 disable_irq(uport->irq);
729 free_irq(uport->irq, msm_port);
730 if (uart_console(uport))
731 se_geni_resources_off(&msm_port->serial_rsc);
732 else
733 msm_geni_serial_power_off(uport);
734}
735
736static int msm_geni_serial_port_setup(struct uart_port *uport)
737{
738 int ret = 0;
739 struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
740
741 /* For now only assume FIFO mode. */
742 msm_port->xfer_mode = FIFO_MODE;
743 set_rfr_wm(msm_port);
744 ret = geni_se_init(uport->membase, msm_port->xfer_mode,
745 msm_port->rx_wm, msm_port->rx_rfr);
746 if (ret) {
747 dev_err(uport->dev, "%s: Fail\n", __func__);
748 goto exit_portsetup;
749 }
750
751 msm_port->port_setup = true;
752 /*
753 * Ensure Port setup related IO completes before returning to
754 * framework.
755 */
756 mb();
757exit_portsetup:
758 return ret;
759}
760
761static int msm_geni_serial_startup(struct uart_port *uport)
762{
763 int ret = 0;
764 struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
765
766 scnprintf(msm_port->name, sizeof(msm_port->name), "msm_serial_geni%d",
767 uport->line);
768
769 ret = request_irq(uport->irq, msm_geni_serial_isr, IRQF_TRIGGER_HIGH,
770 msm_port->name, msm_port);
771 if (unlikely(ret)) {
772 dev_err(uport->dev, "%s: Failed to get IRQ ret %d\n",
773 __func__, ret);
774 goto exit_startup;
775 }
776
777 if (likely(!uart_console(uport))) {
778 ret = msm_geni_serial_power_on(&msm_port->uport);
779 if (ret)
780 goto exit_startup;
781 }
782
783 if (!msm_port->port_setup) {
784 if (msm_geni_serial_port_setup(uport))
785 goto exit_startup;
786 }
787
788 get_tx_fifo_size(msm_port);
789 msm_geni_serial_start_rx(uport);
790 /*
791 * Ensure that all the port configuration writes complete
792 * before returning to the framework.
793 */
794 mb();
795exit_startup:
796 return ret;
797}
798
799static int get_dfs_index(unsigned long clk_freq, unsigned long *ser_clk)
800{
801 unsigned long root_freq[] = {19200000, 7372800, 64000000,
802 96000000, 100000000, 102400000, 128000000};
803 int i;
804 int match = -1;
805
806 for (i = 0; i < ARRAY_SIZE(root_freq); i++) {
807 if (clk_freq > root_freq[i])
808 continue;
809
810 if (!(root_freq[i] % clk_freq)) {
811 match = i;
812 break;
813 }
814 }
815 if (match != -1)
816 *ser_clk = root_freq[match];
817 return match;
818}
819
820static void geni_serial_write_term_regs(struct uart_port *uport, u32 loopback,
821 u32 tx_trans_cfg, u32 tx_parity_cfg, u32 rx_trans_cfg,
822 u32 rx_parity_cfg, u32 bits_per_char, u32 stop_bit_len,
823 u32 rxstale, u32 s_clk_cfg)
824{
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600825 geni_write_reg_nolog(loopback, uport->membase, SE_UART_LOOPBACK_CFG);
826 geni_write_reg_nolog(tx_trans_cfg, uport->membase,
827 SE_UART_TX_TRANS_CFG);
828 geni_write_reg_nolog(tx_parity_cfg, uport->membase,
829 SE_UART_TX_PARITY_CFG);
830 geni_write_reg_nolog(rx_trans_cfg, uport->membase,
831 SE_UART_RX_TRANS_CFG);
832 geni_write_reg_nolog(rx_parity_cfg, uport->membase,
833 SE_UART_RX_PARITY_CFG);
834 geni_write_reg_nolog(bits_per_char, uport->membase,
835 SE_UART_TX_WORD_LEN);
836 geni_write_reg_nolog(bits_per_char, uport->membase,
837 SE_UART_RX_WORD_LEN);
838 geni_write_reg_nolog(stop_bit_len, uport->membase,
839 SE_UART_TX_STOP_BIT_LEN);
840 geni_write_reg_nolog(rxstale, uport->membase, SE_UART_RX_STALE_CNT);
841 geni_write_reg_nolog(s_clk_cfg, uport->membase, GENI_SER_M_CLK_CFG);
842 geni_write_reg_nolog(s_clk_cfg, uport->membase, GENI_SER_S_CLK_CFG);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700843}
844
845static int get_clk_div_rate(unsigned int baud, unsigned long *desired_clk_rate)
846{
847 unsigned long ser_clk;
848 int dfs_index;
849 int clk_div = 0;
850
851 *desired_clk_rate = baud * UART_OVERSAMPLING;
852 dfs_index = get_dfs_index(*desired_clk_rate, &ser_clk);
853 if (dfs_index < 1) {
854 pr_err("%s: Can't find matching DFS entry for baud %d\n",
855 __func__, baud);
856 clk_div = -EINVAL;
857 goto exit_get_clk_div_rate;
858 }
859
860 clk_div = ser_clk / *desired_clk_rate;
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600861 *desired_clk_rate = ser_clk;
Girish Mahadevanebeed352016-11-23 10:59:29 -0700862exit_get_clk_div_rate:
863 return clk_div;
864}
865
866static void msm_geni_serial_set_termios(struct uart_port *uport,
867 struct ktermios *termios, struct ktermios *old)
868{
869 unsigned int baud;
870 unsigned int bits_per_char = 0;
871 unsigned int tx_trans_cfg;
872 unsigned int tx_parity_cfg;
873 unsigned int rx_trans_cfg;
874 unsigned int rx_parity_cfg;
875 unsigned int stop_bit_len;
876 unsigned int rxstale;
877 unsigned int clk_div;
Girish Mahadevan18a9fb02017-03-29 11:26:06 -0600878 unsigned long ser_clk_cfg = 0;
Girish Mahadevanebeed352016-11-23 10:59:29 -0700879 struct msm_geni_serial_port *port = GET_DEV_PORT(uport);
880 unsigned long clk_rate;
881
882 /* baud rate */
883 baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
884 clk_div = get_clk_div_rate(baud, &clk_rate);
885 if (clk_div <= 0)
886 goto exit_set_termios;
887
888 uport->uartclk = clk_rate;
889 clk_set_rate(port->serial_rsc.se_clk, clk_rate);
890 ser_clk_cfg |= SER_CLK_EN;
891 ser_clk_cfg |= (clk_div << CLK_DIV_SHFT);
892
893 /* parity */
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600894 tx_trans_cfg = geni_read_reg_nolog(uport->membase,
895 SE_UART_TX_TRANS_CFG);
896 tx_parity_cfg = geni_read_reg_nolog(uport->membase,
897 SE_UART_TX_PARITY_CFG);
898 rx_trans_cfg = geni_read_reg_nolog(uport->membase,
899 SE_UART_RX_TRANS_CFG);
900 rx_parity_cfg = geni_read_reg_nolog(uport->membase,
901 SE_UART_RX_PARITY_CFG);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700902 if (termios->c_cflag & PARENB) {
903 tx_trans_cfg |= UART_TX_PAR_EN;
904 rx_trans_cfg |= UART_RX_PAR_EN;
905 tx_parity_cfg |= PAR_CALC_EN;
906 rx_parity_cfg |= PAR_CALC_EN;
907 if (termios->c_cflag & PARODD) {
908 tx_parity_cfg |= PAR_ODD;
909 rx_parity_cfg |= PAR_ODD;
910 } else if (termios->c_cflag & CMSPAR) {
911 tx_parity_cfg |= PAR_SPACE;
912 rx_parity_cfg |= PAR_SPACE;
913 } else {
914 tx_parity_cfg |= PAR_EVEN;
915 rx_parity_cfg |= PAR_EVEN;
916 }
917 } else {
918 tx_trans_cfg &= ~UART_TX_PAR_EN;
919 rx_trans_cfg &= ~UART_RX_PAR_EN;
920 tx_parity_cfg &= ~PAR_CALC_EN;
921 rx_parity_cfg &= ~PAR_CALC_EN;
922 }
923
924 /* bits per char */
925 switch (termios->c_cflag & CSIZE) {
926 case CS5:
927 bits_per_char = 5;
928 break;
929 case CS6:
930 bits_per_char = 6;
931 break;
932 case CS7:
933 bits_per_char = 7;
934 break;
935 case CS8:
936 default:
937 bits_per_char = 8;
938 break;
939 }
940
941 /* stale timer, set this to 16 characters. */
942 rxstale = bits_per_char * STALE_TIMEOUT;
943
944 /* stop bits */
945 if (termios->c_cflag & CSTOPB)
946 stop_bit_len = TX_STOP_BIT_LEN_2;
947 else
948 stop_bit_len = TX_STOP_BIT_LEN_1;
949
950 /* flow control, clear the CTS_MASK bit if using flow control. */
951 if (termios->c_cflag & CRTSCTS)
952 tx_trans_cfg &= ~UART_CTS_MASK;
953 else
954 tx_trans_cfg |= UART_CTS_MASK;
955 /* status bits to ignore */
956
957 geni_serial_write_term_regs(uport, port->loopback, tx_trans_cfg,
958 tx_parity_cfg, rx_trans_cfg, rx_parity_cfg, bits_per_char,
959 stop_bit_len, rxstale, ser_clk_cfg);
960exit_set_termios:
961 return;
962
963}
964
965static unsigned int msm_geni_serial_tx_empty(struct uart_port *port)
966{
967 unsigned int tx_fifo_status;
968 unsigned int is_tx_empty = 1;
969
Girish Mahadevanbd9b44f2017-04-11 13:11:10 -0600970 tx_fifo_status = geni_read_reg_nolog(port->membase,
971 SE_GENI_TX_FIFO_STATUS);
Girish Mahadevanebeed352016-11-23 10:59:29 -0700972 if (tx_fifo_status)
973 is_tx_empty = 0;
974
975 return is_tx_empty;
976}
977
Girish Mahadevanf08b1102017-04-02 19:27:28 -0600978#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
Girish Mahadevanebeed352016-11-23 10:59:29 -0700979static int __init msm_geni_console_setup(struct console *co, char *options)
980{
981 struct uart_port *uport;
982 struct msm_geni_serial_port *dev_port;
983 int baud = 115200;
984 int bits = 8;
985 int parity = 'n';
986 int flow = 'n';
987 int ret = 0;
988
989 if (unlikely(co->index >= GENI_UART_NR_PORTS || co->index < 0))
990 return -ENXIO;
991
992 dev_port = get_port_from_line(co->index);
993 if (IS_ERR_OR_NULL(dev_port)) {
994 ret = PTR_ERR(dev_port);
995 pr_err("Invalid line %d(%d)\n", co->index, ret);
996 return ret;
997 }
998
999 uport = &dev_port->uport;
1000
1001 if (unlikely(!uport->membase))
1002 return -ENXIO;
1003
1004 if (se_geni_resources_on(&dev_port->serial_rsc))
1005 WARN_ON(1);
1006
1007 if (unlikely(get_se_proto(uport->membase) != UART)) {
1008 se_geni_resources_off(&dev_port->serial_rsc);
1009 return -ENXIO;
1010 }
1011
1012 if (!dev_port->port_setup)
1013 msm_geni_serial_port_setup(uport);
1014
1015 if (options)
1016 uart_parse_options(options, &baud, &parity, &bits, &flow);
1017
1018 return uart_set_options(uport, co, baud, parity, bits, flow);
1019}
1020
Girish Mahadevanf08b1102017-04-02 19:27:28 -06001021static void
1022msm_geni_serial_early_console_write(struct console *con, const char *s,
1023 unsigned int n)
Girish Mahadevanebeed352016-11-23 10:59:29 -07001024{
Girish Mahadevanf08b1102017-04-02 19:27:28 -06001025 struct earlycon_device *dev = con->data;
Girish Mahadevanebeed352016-11-23 10:59:29 -07001026
Girish Mahadevanf08b1102017-04-02 19:27:28 -06001027 __msm_geni_serial_console_write(&dev->port, s, n);
1028}
1029
1030static int __init
1031msm_geni_serial_earlycon_setup(struct earlycon_device *dev,
1032 const char *opt)
1033{
1034 struct uart_port *uport = &dev->port;
1035 int ret = 0;
1036 struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
1037 u32 tx_trans_cfg = 0;
1038 u32 tx_parity_cfg = 0;
1039 u32 rx_trans_cfg = 0;
1040 u32 rx_parity_cfg = 0;
1041 u32 stop_bit = 0;
1042 u32 rx_stale = 0;
1043 u32 bits_per_char = 0;
1044 u32 s_clk_cfg = 0;
1045 u32 baud = 115200;
1046 u32 clk_div;
1047 unsigned long clk_rate;
1048
1049 if (!uport->membase) {
1050 ret = -ENOMEM;
1051 goto exit_geni_serial_earlyconsetup;
1052 }
1053
1054 if (get_se_proto(uport->membase) != UART) {
1055 ret = -ENXIO;
1056 goto exit_geni_serial_earlyconsetup;
1057 }
1058
1059 msm_port->xfer_mode = FIFO_MODE;
1060 set_rfr_wm(msm_port);
1061 msm_port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1062 msm_port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1063 msm_port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1064 geni_se_init(uport->membase, msm_port->xfer_mode, msm_port->rx_wm,
1065 msm_port->rx_rfr);
1066 /*
1067 * Ignore Flow control.
1068 * Disable Tx Parity.
1069 * Don't check Parity during Rx.
1070 * Disable Rx Parity.
1071 * n = 8.
1072 * Stop bit = 0.
1073 * Stale timeout in bit-time (3 chars worth).
1074 */
1075 tx_trans_cfg |= UART_CTS_MASK;
1076 tx_parity_cfg = 0;
1077 rx_trans_cfg = 0;
1078 rx_parity_cfg = 0;
1079 bits_per_char = 0x8;
1080 stop_bit = 0;
1081 rx_stale = 0x18;
1082 clk_div = get_clk_div_rate(baud, &clk_rate);
1083 if (clk_div <= 0) {
1084 ret = -EINVAL;
1085 goto exit_geni_serial_earlyconsetup;
1086 }
1087
1088 s_clk_cfg |= SER_CLK_EN;
1089 s_clk_cfg |= (clk_div << CLK_DIV_SHFT);
1090
1091 geni_serial_write_term_regs(uport, 0, tx_trans_cfg,
1092 tx_parity_cfg, rx_trans_cfg, rx_parity_cfg, bits_per_char,
1093 stop_bit, rx_stale, s_clk_cfg);
1094
1095 dev->con->write = msm_geni_serial_early_console_write;
1096 dev->con->setup = NULL;
1097 /*
1098 * Ensure that the early console setup completes before
1099 * returning.
1100 */
1101 mb();
1102exit_geni_serial_earlyconsetup:
1103 return ret;
1104}
1105OF_EARLYCON_DECLARE(msm_geni_serial, "qcom,msm-geni-uart",
1106 msm_geni_serial_earlycon_setup);
1107
1108static int console_register(struct uart_driver *drv)
1109{
1110 return uart_register_driver(drv);
1111}
1112static void console_unregister(struct uart_driver *drv)
1113{
1114 uart_unregister_driver(drv);
Girish Mahadevanebeed352016-11-23 10:59:29 -07001115}
1116
1117static struct console cons_ops = {
1118 .name = "ttyMSM",
1119 .write = msm_geni_serial_console_write,
1120 .device = uart_console_device,
1121 .setup = msm_geni_console_setup,
1122 .flags = CON_PRINTBUFFER,
1123 .index = -1,
1124 .data = &msm_geni_console_driver,
1125};
1126
Girish Mahadevanf08b1102017-04-02 19:27:28 -06001127static struct uart_driver msm_geni_console_driver = {
1128 .owner = THIS_MODULE,
1129 .driver_name = "msm_geni_console",
1130 .dev_name = "ttyMSM",
1131 .nr = GENI_UART_NR_PORTS,
1132 .cons = &cons_ops,
1133};
1134#else
1135static int console_register(struct uart_driver *drv)
1136{
1137 return 0;
1138}
1139
1140static void console_unregister(struct uart_driver *drv)
1141{
1142}
1143#endif /* defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL) */
1144
1145static void msm_geni_serial_debug_init(struct uart_port *uport)
1146{
1147 struct msm_geni_serial_port *msm_port = GET_DEV_PORT(uport);
1148
1149 msm_port->dbg = debugfs_create_dir(dev_name(uport->dev), NULL);
1150 if (IS_ERR_OR_NULL(msm_port->dbg))
1151 dev_err(uport->dev, "Failed to create dbg dir\n");
1152}
1153
Girish Mahadevanebeed352016-11-23 10:59:29 -07001154static const struct uart_ops msm_geni_serial_pops = {
1155 .tx_empty = msm_geni_serial_tx_empty,
1156 .stop_tx = msm_geni_serial_stop_tx,
1157 .start_tx = msm_geni_serial_start_tx,
1158 .stop_rx = msm_geni_serial_stop_rx,
1159 .set_termios = msm_geni_serial_set_termios,
1160 .startup = msm_geni_serial_startup,
1161 .config_port = msm_geni_serial_config_port,
1162 .shutdown = msm_geni_serial_shutdown,
1163 .type = msm_geni_serial_get_type,
1164 .set_mctrl = msm_geni_serial_set_mctrl,
1165#ifdef CONFIG_CONSOLE_POLL
1166 .poll_get_char = msm_geni_serial_get_char,
1167 .poll_put_char = msm_geni_serial_poll_put_char,
1168#endif
1169};
1170
1171static const struct of_device_id msm_geni_device_tbl[] = {
Girish Mahadevanf08b1102017-04-02 19:27:28 -06001172#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
Girish Mahadevanebeed352016-11-23 10:59:29 -07001173 { .compatible = "qcom,msm-geni-console",
1174 .data = (void *)&msm_geni_console_driver},
Girish Mahadevanf08b1102017-04-02 19:27:28 -06001175#endif
Girish Mahadevanebeed352016-11-23 10:59:29 -07001176 { .compatible = "qcom,msm-geni-serial-hs",
1177 .data = (void *)&msm_geni_serial_hs_driver},
1178 {},
1179};
1180
1181static int msm_geni_serial_probe(struct platform_device *pdev)
1182{
1183 int ret = 0;
1184 int line;
1185 struct msm_geni_serial_port *dev_port;
1186 struct uart_port *uport;
1187 struct resource *res;
1188 struct uart_driver *drv;
1189 const struct of_device_id *id;
1190
1191 if (pdev->dev.of_node)
1192 line = of_alias_get_id(pdev->dev.of_node, "serial");
1193 else
1194 line = pdev->id;
1195
1196 if (line < 0)
1197 line = atomic_inc_return(&uart_line_id) - 1;
1198
1199 if ((line < 0) || (line >= GENI_UART_NR_PORTS))
1200 return -ENXIO;
1201
1202 id = of_match_device(msm_geni_device_tbl, &pdev->dev);
1203 if (id) {
1204 dev_dbg(&pdev->dev, "%s: %s\n", __func__, id->compatible);
1205 drv = (struct uart_driver *)id->data;
1206 } else {
1207 dev_err(&pdev->dev, "%s: No matching device found", __func__);
1208 return -ENODEV;
1209 }
1210
1211 dev_port = get_port_from_line(line);
1212 if (IS_ERR_OR_NULL(dev_port)) {
1213 ret = PTR_ERR(dev_port);
1214 dev_err(&pdev->dev, "Invalid line %d(%d)\n",
1215 line, ret);
1216 goto exit_geni_serial_probe;
1217 }
1218
1219 uport = &dev_port->uport;
1220
1221 /* Don't allow 2 drivers to access the same port */
1222 if (uport->private_data) {
1223 ret = -ENODEV;
1224 goto exit_geni_serial_probe;
1225 }
1226
1227 uport->dev = &pdev->dev;
1228 dev_port->serial_rsc.se_clk = devm_clk_get(&pdev->dev, "se-clk");
1229 if (IS_ERR(dev_port->serial_rsc.se_clk)) {
1230 ret = PTR_ERR(dev_port->serial_rsc.se_clk);
1231 dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
1232 goto exit_geni_serial_probe;
1233 }
1234
1235 dev_port->serial_rsc.m_ahb_clk = devm_clk_get(&pdev->dev, "m-ahb");
1236 if (IS_ERR(dev_port->serial_rsc.m_ahb_clk)) {
1237 ret = PTR_ERR(dev_port->serial_rsc.m_ahb_clk);
1238 dev_err(&pdev->dev, "Err getting M AHB clk %d\n", ret);
1239 goto exit_geni_serial_probe;
1240 }
1241
1242 dev_port->serial_rsc.s_ahb_clk = devm_clk_get(&pdev->dev, "s-ahb");
1243 if (IS_ERR(dev_port->serial_rsc.s_ahb_clk)) {
1244 ret = PTR_ERR(dev_port->serial_rsc.s_ahb_clk);
1245 dev_err(&pdev->dev, "Err getting S AHB clk %d\n", ret);
1246 goto exit_geni_serial_probe;
1247 }
1248
1249 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "se_phys");
1250 if (!res) {
1251 ret = -ENXIO;
1252 dev_err(&pdev->dev, "Err getting IO region\n");
1253 goto exit_geni_serial_probe;
1254 }
1255
1256 uport->mapbase = res->start;
1257 uport->membase = devm_ioremap(&pdev->dev, res->start,
1258 resource_size(res));
1259 if (!uport->membase) {
1260 ret = -ENOMEM;
1261 dev_err(&pdev->dev, "Err IO mapping serial iomem");
1262 goto exit_geni_serial_probe;
1263 }
1264
1265 dev_port->serial_rsc.geni_pinctrl = devm_pinctrl_get(&pdev->dev);
1266 if (IS_ERR_OR_NULL(dev_port->serial_rsc.geni_pinctrl)) {
1267 dev_err(&pdev->dev, "No pinctrl config specified!\n");
1268 ret = PTR_ERR(dev_port->serial_rsc.geni_pinctrl);
1269 goto exit_geni_serial_probe;
1270 }
1271 dev_port->serial_rsc.geni_gpio_active =
1272 pinctrl_lookup_state(dev_port->serial_rsc.geni_pinctrl,
1273 PINCTRL_DEFAULT);
1274 if (IS_ERR_OR_NULL(dev_port->serial_rsc.geni_gpio_active)) {
1275 dev_err(&pdev->dev, "No default config specified!\n");
1276 ret = PTR_ERR(dev_port->serial_rsc.geni_gpio_active);
1277 goto exit_geni_serial_probe;
1278 }
1279 dev_port->serial_rsc.geni_gpio_sleep =
1280 pinctrl_lookup_state(dev_port->serial_rsc.geni_pinctrl,
1281 PINCTRL_SLEEP);
1282 if (IS_ERR_OR_NULL(dev_port->serial_rsc.geni_gpio_sleep)) {
1283 dev_err(&pdev->dev, "No sleep config specified!\n");
1284 ret = PTR_ERR(dev_port->serial_rsc.geni_gpio_sleep);
1285 goto exit_geni_serial_probe;
1286 }
1287
Girish Mahadevanebeed352016-11-23 10:59:29 -07001288 dev_port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1289 dev_port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1290 dev_port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1291 uport->fifosize =
1292 ((dev_port->tx_fifo_depth * dev_port->tx_fifo_width) >> 3);
1293
1294 uport->irq = platform_get_irq(pdev, 0);
1295 if (uport->irq < 0) {
1296 ret = uport->irq;
1297 dev_err(&pdev->dev, "Failed to get IRQ %d\n", ret);
1298 goto exit_geni_serial_probe;
1299 }
1300
1301 uport->private_data = (void *)drv;
1302 platform_set_drvdata(pdev, dev_port);
1303 if (drv->cons) {
1304 dev_port->handle_rx = handle_rx_console;
1305 dev_port->rx_fifo = devm_kzalloc(uport->dev, sizeof(u32),
1306 GFP_KERNEL);
1307 } else {
1308 dev_port->handle_rx = handle_rx_hs;
1309 dev_port->rx_fifo = devm_kzalloc(uport->dev,
1310 sizeof(dev_port->rx_fifo_depth * sizeof(u32)),
1311 GFP_KERNEL);
1312 pm_runtime_set_autosuspend_delay(&pdev->dev, MSEC_PER_SEC);
1313 pm_runtime_use_autosuspend(&pdev->dev);
1314 pm_runtime_enable(&pdev->dev);
1315 }
1316
1317 dev_info(&pdev->dev, "Serial port%d added.FifoSize %d is_console%d\n",
1318 line, uport->fifosize, (drv->cons ? 1 : 0));
1319 device_create_file(uport->dev, &dev_attr_loopback);
1320 msm_geni_serial_debug_init(uport);
1321 dev_port->port_setup = false;
1322 return uart_add_one_port(drv, uport);
1323
1324exit_geni_serial_probe:
1325 return ret;
1326}
1327
1328static int msm_geni_serial_remove(struct platform_device *pdev)
1329{
1330 struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
1331 struct uart_driver *drv =
1332 (struct uart_driver *)port->uport.private_data;
1333
1334 uart_remove_one_port(drv, &port->uport);
1335 return 0;
1336}
1337
Girish Mahadevanebeed352016-11-23 10:59:29 -07001338
1339#ifdef CONFIG_PM
1340static int msm_geni_serial_runtime_suspend(struct device *dev)
1341{
1342 struct platform_device *pdev = to_platform_device(dev);
1343 struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
1344
1345 return se_geni_resources_off(&port->serial_rsc);
1346}
1347
1348static int msm_geni_serial_runtime_resume(struct device *dev)
1349{
1350 struct platform_device *pdev = to_platform_device(dev);
1351 struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
1352
1353 return se_geni_resources_on(&port->serial_rsc);
1354}
1355
1356static int msm_geni_serial_sys_suspend_noirq(struct device *dev)
1357{
1358 struct platform_device *pdev = to_platform_device(dev);
1359 struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
1360 struct uart_port *uport = &port->uport;
1361
1362 if (uart_console(uport)) {
1363 uart_suspend_port((struct uart_driver *)uport->private_data,
1364 uport);
1365 } else {
1366 if (!pm_runtime_status_suspended(dev)) {
1367 dev_info(dev, "%s: Is still active\n", __func__);
1368 return -EBUSY;
1369 }
1370 }
1371 return 0;
1372}
1373
1374static int msm_geni_serial_sys_resume_noirq(struct device *dev)
1375{
1376 struct platform_device *pdev = to_platform_device(dev);
1377 struct msm_geni_serial_port *port = platform_get_drvdata(pdev);
1378 struct uart_port *uport = &port->uport;
1379
1380 if (uart_console(uport))
1381 uart_resume_port((struct uart_driver *)uport->private_data,
1382 uport);
1383 return 0;
1384}
1385#else
1386static int msm_geni_serial_runtime_suspend(struct device *dev)
1387{
1388 return 0;
1389}
1390
1391static int msm_geni_serial_runtime_resume(struct device *dev)
1392{
1393 return 0;
1394}
1395
1396static int msm_geni_serial_sys_suspend_noirq(struct device *dev)
1397{
1398 return 0;
1399}
1400
1401static int msm_geni_serial_sys_resume_noirq(struct device *dev)
1402{
1403 return 0;
1404}
1405#endif
1406
1407static const struct dev_pm_ops msm_geni_serial_pm_ops = {
1408 .runtime_suspend = msm_geni_serial_runtime_suspend,
1409 .runtime_resume = msm_geni_serial_runtime_resume,
1410 .suspend_noirq = msm_geni_serial_sys_suspend_noirq,
1411 .resume_noirq = msm_geni_serial_sys_resume_noirq,
1412};
1413
1414static const struct of_device_id msm_geni_serial_match_table[] = {
1415 { .compatible = "qcom,msm-geni-uart"},
1416 {},
1417};
1418
1419static struct platform_driver msm_geni_serial_platform_driver = {
1420 .remove = msm_geni_serial_remove,
1421 .probe = msm_geni_serial_probe,
1422 .driver = {
1423 .name = "msm_geni_serial",
1424 .of_match_table = msm_geni_serial_match_table,
1425 .pm = &msm_geni_serial_pm_ops,
1426 },
1427};
1428
Girish Mahadevanebeed352016-11-23 10:59:29 -07001429
1430static struct uart_driver msm_geni_serial_hs_driver = {
1431 .owner = THIS_MODULE,
1432 .driver_name = "msm_geni_serial_hs",
1433 .dev_name = "ttyHS",
1434 .nr = GENI_UART_NR_PORTS,
1435};
1436
1437static int __init msm_geni_serial_init(void)
1438{
1439 int ret = 0;
1440 int i;
1441
1442 for (i = 0; i < GENI_UART_NR_PORTS; i++) {
1443 msm_geni_serial_ports[i].uport.iotype = UPIO_MEM;
1444 msm_geni_serial_ports[i].uport.ops = &msm_geni_serial_pops;
1445 msm_geni_serial_ports[i].uport.flags = UPF_BOOT_AUTOCONF;
1446 msm_geni_serial_ports[i].uport.line = i;
1447 }
1448
Girish Mahadevanf08b1102017-04-02 19:27:28 -06001449 ret = console_register(&msm_geni_console_driver);
Girish Mahadevanebeed352016-11-23 10:59:29 -07001450 if (ret)
1451 return ret;
1452
1453 ret = uart_register_driver(&msm_geni_serial_hs_driver);
1454 if (ret) {
1455 uart_unregister_driver(&msm_geni_console_driver);
1456 return ret;
1457 }
1458
1459 ret = platform_driver_register(&msm_geni_serial_platform_driver);
1460 if (ret) {
Girish Mahadevanf08b1102017-04-02 19:27:28 -06001461 console_unregister(&msm_geni_console_driver);
Girish Mahadevanebeed352016-11-23 10:59:29 -07001462 uart_unregister_driver(&msm_geni_serial_hs_driver);
1463 return ret;
1464 }
1465
1466 pr_info("%s: Driver initialized", __func__);
1467 return ret;
1468}
1469module_init(msm_geni_serial_init);
1470
1471static void __exit msm_geni_serial_exit(void)
1472{
1473 platform_driver_unregister(&msm_geni_serial_platform_driver);
1474 uart_unregister_driver(&msm_geni_serial_hs_driver);
Girish Mahadevanf08b1102017-04-02 19:27:28 -06001475 console_unregister(&msm_geni_console_driver);
Girish Mahadevanebeed352016-11-23 10:59:29 -07001476}
1477module_exit(msm_geni_serial_exit);
1478
1479MODULE_DESCRIPTION("Serial driver for GENI based QTI serial cores");
1480MODULE_LICENSE("GPL v2");
1481MODULE_ALIAS("tty:msm_geni_geni_serial");