xref: /openbmc/linux/drivers/usb/serial/xr_serial.c (revision 974e2f6a)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * MaxLinear/Exar USB to Serial driver
4  *
5  * Copyright (c) 2020 Manivannan Sadhasivam <mani@kernel.org>
6  * Copyright (c) 2021 Johan Hovold <johan@kernel.org>
7  *
8  * Based on the initial driver written by Patong Yang:
9  *
10  *   https://lore.kernel.org/r/20180404070634.nhspvmxcjwfgjkcv@advantechmxl-desktop
11  *
12  *   Copyright (c) 2018 Patong Yang <patong.mxl@gmail.com>
13  */
14 
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/tty.h>
19 #include <linux/usb.h>
20 #include <linux/usb/cdc.h>
21 #include <linux/usb/serial.h>
22 
23 struct xr_txrx_clk_mask {
24 	u16 tx;
25 	u16 rx0;
26 	u16 rx1;
27 };
28 
29 #define XR_INT_OSC_HZ			48000000U
30 #define XR21V141X_MIN_SPEED		46U
31 #define XR21V141X_MAX_SPEED		XR_INT_OSC_HZ
32 
33 /* XR21V141X register blocks */
34 #define XR21V141X_UART_REG_BLOCK	0
35 #define XR21V141X_UM_REG_BLOCK		4
36 #define XR21V141X_UART_CUSTOM_BLOCK	0x66
37 
38 /* XR21V141X UART registers */
39 #define XR21V141X_CLOCK_DIVISOR_0	0x04
40 #define XR21V141X_CLOCK_DIVISOR_1	0x05
41 #define XR21V141X_CLOCK_DIVISOR_2	0x06
42 #define XR21V141X_TX_CLOCK_MASK_0	0x07
43 #define XR21V141X_TX_CLOCK_MASK_1	0x08
44 #define XR21V141X_RX_CLOCK_MASK_0	0x09
45 #define XR21V141X_RX_CLOCK_MASK_1	0x0a
46 #define XR21V141X_REG_FORMAT		0x0b
47 
48 /* XR21V141X UART Manager registers */
49 #define XR21V141X_UM_FIFO_ENABLE_REG	0x10
50 #define XR21V141X_UM_ENABLE_TX_FIFO	0x01
51 #define XR21V141X_UM_ENABLE_RX_FIFO	0x02
52 
53 #define XR21V141X_UM_RX_FIFO_RESET	0x18
54 #define XR21V141X_UM_TX_FIFO_RESET	0x1c
55 
56 #define XR_UART_ENABLE_TX		0x1
57 #define XR_UART_ENABLE_RX		0x2
58 
59 #define XR_GPIO_RI			BIT(0)
60 #define XR_GPIO_CD			BIT(1)
61 #define XR_GPIO_DSR			BIT(2)
62 #define XR_GPIO_DTR			BIT(3)
63 #define XR_GPIO_CTS			BIT(4)
64 #define XR_GPIO_RTS			BIT(5)
65 #define XR_GPIO_CLK			BIT(6)
66 #define XR_GPIO_XEN			BIT(7)
67 #define XR_GPIO_TXT			BIT(8)
68 #define XR_GPIO_RXT			BIT(9)
69 
70 #define XR_UART_DATA_MASK		GENMASK(3, 0)
71 #define XR_UART_DATA_7			0x7
72 #define XR_UART_DATA_8			0x8
73 
74 #define XR_UART_PARITY_MASK		GENMASK(6, 4)
75 #define XR_UART_PARITY_SHIFT		4
76 #define XR_UART_PARITY_NONE		(0x0 << XR_UART_PARITY_SHIFT)
77 #define XR_UART_PARITY_ODD		(0x1 << XR_UART_PARITY_SHIFT)
78 #define XR_UART_PARITY_EVEN		(0x2 <<	XR_UART_PARITY_SHIFT)
79 #define XR_UART_PARITY_MARK		(0x3 << XR_UART_PARITY_SHIFT)
80 #define XR_UART_PARITY_SPACE		(0x4 << XR_UART_PARITY_SHIFT)
81 
82 #define XR_UART_STOP_MASK		BIT(7)
83 #define XR_UART_STOP_SHIFT		7
84 #define XR_UART_STOP_1			(0x0 << XR_UART_STOP_SHIFT)
85 #define XR_UART_STOP_2			(0x1 << XR_UART_STOP_SHIFT)
86 
87 #define XR_UART_FLOW_MODE_NONE		0x0
88 #define XR_UART_FLOW_MODE_HW		0x1
89 #define XR_UART_FLOW_MODE_SW		0x2
90 
91 #define XR_GPIO_MODE_SEL_MASK		GENMASK(2, 0)
92 #define XR_GPIO_MODE_SEL_RTS_CTS	0x1
93 #define XR_GPIO_MODE_SEL_DTR_DSR	0x2
94 #define XR_GPIO_MODE_SEL_RS485		0x3
95 #define XR_GPIO_MODE_SEL_RS485_ADDR	0x4
96 #define XR_GPIO_MODE_RS485_TX_H		0x8
97 #define XR_GPIO_MODE_TX_TOGGLE		0x100
98 #define XR_GPIO_MODE_RX_TOGGLE		0x200
99 
100 #define XR_FIFO_RESET			0x1
101 
102 #define XR_CUSTOM_DRIVER_ACTIVE		0x1
103 
104 static int xr21v141x_uart_enable(struct usb_serial_port *port);
105 static int xr21v141x_uart_disable(struct usb_serial_port *port);
106 static int xr21v141x_fifo_reset(struct usb_serial_port *port);
107 static void xr21v141x_set_line_settings(struct tty_struct *tty,
108 					struct usb_serial_port *port,
109 					const struct ktermios *old_termios);
110 
111 struct xr_type {
112 	int reg_width;
113 	u8 reg_recipient;
114 	u8 set_reg;
115 	u8 get_reg;
116 
117 	u16 uart_enable;
118 	u16 flow_control;
119 	u16 xon_char;
120 	u16 xoff_char;
121 	u16 tx_break;
122 	u16 gpio_mode;
123 	u16 gpio_direction;
124 	u16 gpio_set;
125 	u16 gpio_clear;
126 	u16 gpio_status;
127 	u16 tx_fifo_reset;
128 	u16 rx_fifo_reset;
129 	u16 custom_driver;
130 
131 	bool have_5_6_bit_mode;
132 	bool have_xmit_toggle;
133 
134 	int (*enable)(struct usb_serial_port *port);
135 	int (*disable)(struct usb_serial_port *port);
136 	int (*fifo_reset)(struct usb_serial_port *port);
137 	void (*set_line_settings)(struct tty_struct *tty,
138 				  struct usb_serial_port *port,
139 				  const struct ktermios *old_termios);
140 };
141 
142 enum xr_type_id {
143 	XR21V141X,
144 	XR21B142X,
145 	XR21B1411,
146 	XR2280X,
147 	XR_TYPE_COUNT,
148 };
149 
150 static const struct xr_type xr_types[] = {
151 	[XR21V141X] = {
152 		.reg_width	= 8,
153 		.reg_recipient	= USB_RECIP_DEVICE,
154 		.set_reg	= 0x00,
155 		.get_reg	= 0x01,
156 
157 		.uart_enable	= 0x03,
158 		.flow_control	= 0x0c,
159 		.xon_char	= 0x10,
160 		.xoff_char	= 0x11,
161 		.tx_break	= 0x14,
162 		.gpio_mode	= 0x1a,
163 		.gpio_direction	= 0x1b,
164 		.gpio_set	= 0x1d,
165 		.gpio_clear	= 0x1e,
166 		.gpio_status	= 0x1f,
167 
168 		.enable			= xr21v141x_uart_enable,
169 		.disable		= xr21v141x_uart_disable,
170 		.fifo_reset		= xr21v141x_fifo_reset,
171 		.set_line_settings	= xr21v141x_set_line_settings,
172 	},
173 	[XR21B142X] = {
174 		.reg_width	= 16,
175 		.reg_recipient	= USB_RECIP_INTERFACE,
176 		.set_reg	= 0x00,
177 		.get_reg	= 0x00,
178 
179 		.uart_enable	= 0x00,
180 		.flow_control	= 0x06,
181 		.xon_char	= 0x07,
182 		.xoff_char	= 0x08,
183 		.tx_break	= 0x0a,
184 		.gpio_mode	= 0x0c,
185 		.gpio_direction	= 0x0d,
186 		.gpio_set	= 0x0e,
187 		.gpio_clear	= 0x0f,
188 		.gpio_status	= 0x10,
189 		.tx_fifo_reset	= 0x40,
190 		.rx_fifo_reset	= 0x43,
191 		.custom_driver	= 0x60,
192 
193 		.have_5_6_bit_mode	= true,
194 		.have_xmit_toggle	= true,
195 	},
196 	[XR21B1411] = {
197 		.reg_width	= 12,
198 		.reg_recipient	= USB_RECIP_DEVICE,
199 		.set_reg	= 0x00,
200 		.get_reg	= 0x01,
201 
202 		.uart_enable	= 0xc00,
203 		.flow_control	= 0xc06,
204 		.xon_char	= 0xc07,
205 		.xoff_char	= 0xc08,
206 		.tx_break	= 0xc0a,
207 		.gpio_mode	= 0xc0c,
208 		.gpio_direction	= 0xc0d,
209 		.gpio_set	= 0xc0e,
210 		.gpio_clear	= 0xc0f,
211 		.gpio_status	= 0xc10,
212 		.tx_fifo_reset	= 0xc80,
213 		.rx_fifo_reset	= 0xcc0,
214 		.custom_driver	= 0x20d,
215 	},
216 	[XR2280X] = {
217 		.reg_width	= 16,
218 		.reg_recipient	= USB_RECIP_DEVICE,
219 		.set_reg	= 0x05,
220 		.get_reg	= 0x05,
221 
222 		.uart_enable	= 0x40,
223 		.flow_control	= 0x46,
224 		.xon_char	= 0x47,
225 		.xoff_char	= 0x48,
226 		.tx_break	= 0x4a,
227 		.gpio_mode	= 0x4c,
228 		.gpio_direction	= 0x4d,
229 		.gpio_set	= 0x4e,
230 		.gpio_clear	= 0x4f,
231 		.gpio_status	= 0x50,
232 		.tx_fifo_reset	= 0x60,
233 		.rx_fifo_reset	= 0x63,
234 		.custom_driver	= 0x81,
235 	},
236 };
237 
238 struct xr_data {
239 	const struct xr_type *type;
240 	u8 channel;			/* zero-based index or interface number */
241 	struct serial_rs485 rs485;
242 };
243 
xr_set_reg(struct usb_serial_port * port,u8 channel,u16 reg,u16 val)244 static int xr_set_reg(struct usb_serial_port *port, u8 channel, u16 reg, u16 val)
245 {
246 	struct xr_data *data = usb_get_serial_port_data(port);
247 	const struct xr_type *type = data->type;
248 	struct usb_serial *serial = port->serial;
249 	int ret;
250 
251 	ret = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
252 			type->set_reg,
253 			USB_DIR_OUT | USB_TYPE_VENDOR | type->reg_recipient,
254 			val, (channel << 8) | reg, NULL, 0,
255 			USB_CTRL_SET_TIMEOUT);
256 	if (ret < 0) {
257 		dev_err(&port->dev, "Failed to set reg 0x%02x: %d\n", reg, ret);
258 		return ret;
259 	}
260 
261 	return 0;
262 }
263 
xr_get_reg(struct usb_serial_port * port,u8 channel,u16 reg,u16 * val)264 static int xr_get_reg(struct usb_serial_port *port, u8 channel, u16 reg, u16 *val)
265 {
266 	struct xr_data *data = usb_get_serial_port_data(port);
267 	const struct xr_type *type = data->type;
268 	struct usb_serial *serial = port->serial;
269 	u8 *dmabuf;
270 	int ret, len;
271 
272 	if (type->reg_width == 8)
273 		len = 1;
274 	else
275 		len = 2;
276 
277 	dmabuf = kmalloc(len, GFP_KERNEL);
278 	if (!dmabuf)
279 		return -ENOMEM;
280 
281 	ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
282 			type->get_reg,
283 			USB_DIR_IN | USB_TYPE_VENDOR | type->reg_recipient,
284 			0, (channel << 8) | reg, dmabuf, len,
285 			USB_CTRL_GET_TIMEOUT);
286 	if (ret == len) {
287 		if (len == 2)
288 			*val = le16_to_cpup((__le16 *)dmabuf);
289 		else
290 			*val = *dmabuf;
291 		ret = 0;
292 	} else {
293 		dev_err(&port->dev, "Failed to get reg 0x%02x: %d\n", reg, ret);
294 		if (ret >= 0)
295 			ret = -EIO;
296 	}
297 
298 	kfree(dmabuf);
299 
300 	return ret;
301 }
302 
xr_set_reg_uart(struct usb_serial_port * port,u16 reg,u16 val)303 static int xr_set_reg_uart(struct usb_serial_port *port, u16 reg, u16 val)
304 {
305 	struct xr_data *data = usb_get_serial_port_data(port);
306 
307 	return xr_set_reg(port, data->channel, reg, val);
308 }
309 
xr_get_reg_uart(struct usb_serial_port * port,u16 reg,u16 * val)310 static int xr_get_reg_uart(struct usb_serial_port *port, u16 reg, u16 *val)
311 {
312 	struct xr_data *data = usb_get_serial_port_data(port);
313 
314 	return xr_get_reg(port, data->channel, reg, val);
315 }
316 
xr_set_reg_um(struct usb_serial_port * port,u8 reg_base,u8 val)317 static int xr_set_reg_um(struct usb_serial_port *port, u8 reg_base, u8 val)
318 {
319 	struct xr_data *data = usb_get_serial_port_data(port);
320 	u8 reg;
321 
322 	reg = reg_base + data->channel;
323 
324 	return xr_set_reg(port, XR21V141X_UM_REG_BLOCK, reg, val);
325 }
326 
__xr_uart_enable(struct usb_serial_port * port)327 static int __xr_uart_enable(struct usb_serial_port *port)
328 {
329 	struct xr_data *data = usb_get_serial_port_data(port);
330 
331 	return xr_set_reg_uart(port, data->type->uart_enable,
332 			XR_UART_ENABLE_TX | XR_UART_ENABLE_RX);
333 }
334 
__xr_uart_disable(struct usb_serial_port * port)335 static int __xr_uart_disable(struct usb_serial_port *port)
336 {
337 	struct xr_data *data = usb_get_serial_port_data(port);
338 
339 	return xr_set_reg_uart(port, data->type->uart_enable, 0);
340 }
341 
342 /*
343  * According to datasheet, below is the recommended sequence for enabling UART
344  * module in XR21V141X:
345  *
346  * Enable Tx FIFO
347  * Enable Tx and Rx
348  * Enable Rx FIFO
349  */
xr21v141x_uart_enable(struct usb_serial_port * port)350 static int xr21v141x_uart_enable(struct usb_serial_port *port)
351 {
352 	int ret;
353 
354 	ret = xr_set_reg_um(port, XR21V141X_UM_FIFO_ENABLE_REG,
355 			    XR21V141X_UM_ENABLE_TX_FIFO);
356 	if (ret)
357 		return ret;
358 
359 	ret = __xr_uart_enable(port);
360 	if (ret)
361 		return ret;
362 
363 	ret = xr_set_reg_um(port, XR21V141X_UM_FIFO_ENABLE_REG,
364 			    XR21V141X_UM_ENABLE_TX_FIFO | XR21V141X_UM_ENABLE_RX_FIFO);
365 	if (ret)
366 		__xr_uart_disable(port);
367 
368 	return ret;
369 }
370 
xr21v141x_uart_disable(struct usb_serial_port * port)371 static int xr21v141x_uart_disable(struct usb_serial_port *port)
372 {
373 	int ret;
374 
375 	ret = __xr_uart_disable(port);
376 	if (ret)
377 		return ret;
378 
379 	ret = xr_set_reg_um(port, XR21V141X_UM_FIFO_ENABLE_REG, 0);
380 
381 	return ret;
382 }
383 
xr_uart_enable(struct usb_serial_port * port)384 static int xr_uart_enable(struct usb_serial_port *port)
385 {
386 	struct xr_data *data = usb_get_serial_port_data(port);
387 
388 	if (data->type->enable)
389 		return data->type->enable(port);
390 
391 	return __xr_uart_enable(port);
392 }
393 
xr_uart_disable(struct usb_serial_port * port)394 static int xr_uart_disable(struct usb_serial_port *port)
395 {
396 	struct xr_data *data = usb_get_serial_port_data(port);
397 
398 	if (data->type->disable)
399 		return data->type->disable(port);
400 
401 	return __xr_uart_disable(port);
402 }
403 
xr21v141x_fifo_reset(struct usb_serial_port * port)404 static int xr21v141x_fifo_reset(struct usb_serial_port *port)
405 {
406 	int ret;
407 
408 	ret = xr_set_reg_um(port, XR21V141X_UM_TX_FIFO_RESET, XR_FIFO_RESET);
409 	if (ret)
410 		return ret;
411 
412 	ret = xr_set_reg_um(port, XR21V141X_UM_RX_FIFO_RESET, XR_FIFO_RESET);
413 	if (ret)
414 		return ret;
415 
416 	return 0;
417 }
418 
xr_fifo_reset(struct usb_serial_port * port)419 static int xr_fifo_reset(struct usb_serial_port *port)
420 {
421 	struct xr_data *data = usb_get_serial_port_data(port);
422 	int ret;
423 
424 	if (data->type->fifo_reset)
425 		return data->type->fifo_reset(port);
426 
427 	ret = xr_set_reg_uart(port, data->type->tx_fifo_reset, XR_FIFO_RESET);
428 	if (ret)
429 		return ret;
430 
431 	ret = xr_set_reg_uart(port, data->type->rx_fifo_reset, XR_FIFO_RESET);
432 	if (ret)
433 		return ret;
434 
435 	return 0;
436 }
437 
xr_tiocmget(struct tty_struct * tty)438 static int xr_tiocmget(struct tty_struct *tty)
439 {
440 	struct usb_serial_port *port = tty->driver_data;
441 	struct xr_data *data = usb_get_serial_port_data(port);
442 	u16 status;
443 	int ret;
444 
445 	ret = xr_get_reg_uart(port, data->type->gpio_status, &status);
446 	if (ret)
447 		return ret;
448 
449 	/*
450 	 * Modem control pins are active low, so reading '0' means it is active
451 	 * and '1' means not active.
452 	 */
453 	ret = ((status & XR_GPIO_DTR) ? 0 : TIOCM_DTR) |
454 	      ((status & XR_GPIO_RTS) ? 0 : TIOCM_RTS) |
455 	      ((status & XR_GPIO_CTS) ? 0 : TIOCM_CTS) |
456 	      ((status & XR_GPIO_DSR) ? 0 : TIOCM_DSR) |
457 	      ((status & XR_GPIO_RI) ? 0 : TIOCM_RI) |
458 	      ((status & XR_GPIO_CD) ? 0 : TIOCM_CD);
459 
460 	return ret;
461 }
462 
xr_tiocmset_port(struct usb_serial_port * port,unsigned int set,unsigned int clear)463 static int xr_tiocmset_port(struct usb_serial_port *port,
464 			    unsigned int set, unsigned int clear)
465 {
466 	struct xr_data *data = usb_get_serial_port_data(port);
467 	const struct xr_type *type = data->type;
468 	u16 gpio_set = 0;
469 	u16 gpio_clr = 0;
470 	int ret = 0;
471 
472 	/* Modem control pins are active low, so set & clr are swapped */
473 	if (set & TIOCM_RTS)
474 		gpio_clr |= XR_GPIO_RTS;
475 	if (set & TIOCM_DTR)
476 		gpio_clr |= XR_GPIO_DTR;
477 	if (clear & TIOCM_RTS)
478 		gpio_set |= XR_GPIO_RTS;
479 	if (clear & TIOCM_DTR)
480 		gpio_set |= XR_GPIO_DTR;
481 
482 	/* Writing '0' to gpio_{set/clr} bits has no effect, so no need to do */
483 	if (gpio_clr)
484 		ret = xr_set_reg_uart(port, type->gpio_clear, gpio_clr);
485 
486 	if (gpio_set)
487 		ret = xr_set_reg_uart(port, type->gpio_set, gpio_set);
488 
489 	return ret;
490 }
491 
xr_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)492 static int xr_tiocmset(struct tty_struct *tty,
493 		       unsigned int set, unsigned int clear)
494 {
495 	struct usb_serial_port *port = tty->driver_data;
496 
497 	return xr_tiocmset_port(port, set, clear);
498 }
499 
xr_dtr_rts(struct usb_serial_port * port,int on)500 static void xr_dtr_rts(struct usb_serial_port *port, int on)
501 {
502 	if (on)
503 		xr_tiocmset_port(port, TIOCM_DTR | TIOCM_RTS, 0);
504 	else
505 		xr_tiocmset_port(port, 0, TIOCM_DTR | TIOCM_RTS);
506 }
507 
xr_break_ctl(struct tty_struct * tty,int break_state)508 static int xr_break_ctl(struct tty_struct *tty, int break_state)
509 {
510 	struct usb_serial_port *port = tty->driver_data;
511 	struct xr_data *data = usb_get_serial_port_data(port);
512 	const struct xr_type *type = data->type;
513 	u16 state;
514 
515 	if (break_state == 0)
516 		state = 0;
517 	else
518 		state = GENMASK(type->reg_width - 1, 0);
519 
520 	dev_dbg(&port->dev, "Turning break %s\n", state == 0 ? "off" : "on");
521 
522 	return xr_set_reg_uart(port, type->tx_break, state);
523 }
524 
525 /* Tx and Rx clock mask values obtained from section 3.3.4 of datasheet */
526 static const struct xr_txrx_clk_mask xr21v141x_txrx_clk_masks[] = {
527 	{ 0x000, 0x000, 0x000 },
528 	{ 0x000, 0x000, 0x000 },
529 	{ 0x100, 0x000, 0x100 },
530 	{ 0x020, 0x400, 0x020 },
531 	{ 0x010, 0x100, 0x010 },
532 	{ 0x208, 0x040, 0x208 },
533 	{ 0x104, 0x820, 0x108 },
534 	{ 0x844, 0x210, 0x884 },
535 	{ 0x444, 0x110, 0x444 },
536 	{ 0x122, 0x888, 0x224 },
537 	{ 0x912, 0x448, 0x924 },
538 	{ 0x492, 0x248, 0x492 },
539 	{ 0x252, 0x928, 0x292 },
540 	{ 0x94a, 0x4a4, 0xa52 },
541 	{ 0x52a, 0xaa4, 0x54a },
542 	{ 0xaaa, 0x954, 0x4aa },
543 	{ 0xaaa, 0x554, 0xaaa },
544 	{ 0x555, 0xad4, 0x5aa },
545 	{ 0xb55, 0xab4, 0x55a },
546 	{ 0x6b5, 0x5ac, 0xb56 },
547 	{ 0x5b5, 0xd6c, 0x6d6 },
548 	{ 0xb6d, 0xb6a, 0xdb6 },
549 	{ 0x76d, 0x6da, 0xbb6 },
550 	{ 0xedd, 0xdda, 0x76e },
551 	{ 0xddd, 0xbba, 0xeee },
552 	{ 0x7bb, 0xf7a, 0xdde },
553 	{ 0xf7b, 0xef6, 0x7de },
554 	{ 0xdf7, 0xbf6, 0xf7e },
555 	{ 0x7f7, 0xfee, 0xefe },
556 	{ 0xfdf, 0xfbe, 0x7fe },
557 	{ 0xf7f, 0xefe, 0xffe },
558 	{ 0xfff, 0xffe, 0xffd },
559 };
560 
xr21v141x_set_baudrate(struct tty_struct * tty,struct usb_serial_port * port)561 static int xr21v141x_set_baudrate(struct tty_struct *tty, struct usb_serial_port *port)
562 {
563 	u32 divisor, baud, idx;
564 	u16 tx_mask, rx_mask;
565 	int ret;
566 
567 	baud = tty->termios.c_ospeed;
568 	if (!baud)
569 		return 0;
570 
571 	baud = clamp(baud, XR21V141X_MIN_SPEED, XR21V141X_MAX_SPEED);
572 	divisor = XR_INT_OSC_HZ / baud;
573 	idx = ((32 * XR_INT_OSC_HZ) / baud) & 0x1f;
574 	tx_mask = xr21v141x_txrx_clk_masks[idx].tx;
575 
576 	if (divisor & 0x01)
577 		rx_mask = xr21v141x_txrx_clk_masks[idx].rx1;
578 	else
579 		rx_mask = xr21v141x_txrx_clk_masks[idx].rx0;
580 
581 	dev_dbg(&port->dev, "Setting baud rate: %u\n", baud);
582 	/*
583 	 * XR21V141X uses fractional baud rate generator with 48MHz internal
584 	 * oscillator and 19-bit programmable divisor. So theoretically it can
585 	 * generate most commonly used baud rates with high accuracy.
586 	 */
587 	ret = xr_set_reg_uart(port, XR21V141X_CLOCK_DIVISOR_0,
588 			      divisor & 0xff);
589 	if (ret)
590 		return ret;
591 
592 	ret = xr_set_reg_uart(port, XR21V141X_CLOCK_DIVISOR_1,
593 			      (divisor >>  8) & 0xff);
594 	if (ret)
595 		return ret;
596 
597 	ret = xr_set_reg_uart(port, XR21V141X_CLOCK_DIVISOR_2,
598 			      (divisor >> 16) & 0xff);
599 	if (ret)
600 		return ret;
601 
602 	ret = xr_set_reg_uart(port, XR21V141X_TX_CLOCK_MASK_0,
603 			      tx_mask & 0xff);
604 	if (ret)
605 		return ret;
606 
607 	ret = xr_set_reg_uart(port, XR21V141X_TX_CLOCK_MASK_1,
608 			      (tx_mask >>  8) & 0xff);
609 	if (ret)
610 		return ret;
611 
612 	ret = xr_set_reg_uart(port, XR21V141X_RX_CLOCK_MASK_0,
613 			      rx_mask & 0xff);
614 	if (ret)
615 		return ret;
616 
617 	ret = xr_set_reg_uart(port, XR21V141X_RX_CLOCK_MASK_1,
618 			      (rx_mask >>  8) & 0xff);
619 	if (ret)
620 		return ret;
621 
622 	tty_encode_baud_rate(tty, baud, baud);
623 
624 	return 0;
625 }
626 
xr_set_flow_mode(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)627 static void xr_set_flow_mode(struct tty_struct *tty,
628 		             struct usb_serial_port *port,
629 		             const struct ktermios *old_termios)
630 {
631 	struct xr_data *data = usb_get_serial_port_data(port);
632 	const struct xr_type *type = data->type;
633 	u16 flow, gpio_mode;
634 	bool rs485_enabled;
635 	int ret;
636 
637 	ret = xr_get_reg_uart(port, type->gpio_mode, &gpio_mode);
638 	if (ret)
639 		return;
640 
641 	/*
642 	 * According to the datasheets, the UART needs to be disabled while
643 	 * writing to the FLOW_CONTROL register (XR21V141X), or any register
644 	 * but GPIO_SET, GPIO_CLEAR, TX_BREAK and ERROR_STATUS (XR21B142X).
645 	 */
646 	xr_uart_disable(port);
647 
648 	/* Set GPIO mode for controlling the pins manually by default. */
649 	gpio_mode &= ~XR_GPIO_MODE_SEL_MASK;
650 
651 	rs485_enabled = !!(data->rs485.flags & SER_RS485_ENABLED);
652 	if (rs485_enabled) {
653 		dev_dbg(&port->dev, "Enabling RS-485\n");
654 		gpio_mode |= XR_GPIO_MODE_SEL_RS485;
655 		if (data->rs485.flags & SER_RS485_RTS_ON_SEND)
656 			gpio_mode &= ~XR_GPIO_MODE_RS485_TX_H;
657 		else
658 			gpio_mode |= XR_GPIO_MODE_RS485_TX_H;
659 	}
660 
661 	if (C_CRTSCTS(tty) && C_BAUD(tty) != B0 && !rs485_enabled) {
662 		dev_dbg(&port->dev, "Enabling hardware flow ctrl\n");
663 		gpio_mode |= XR_GPIO_MODE_SEL_RTS_CTS;
664 		flow = XR_UART_FLOW_MODE_HW;
665 	} else if (I_IXON(tty)) {
666 		u8 start_char = START_CHAR(tty);
667 		u8 stop_char = STOP_CHAR(tty);
668 
669 		dev_dbg(&port->dev, "Enabling sw flow ctrl\n");
670 		flow = XR_UART_FLOW_MODE_SW;
671 
672 		xr_set_reg_uart(port, type->xon_char, start_char);
673 		xr_set_reg_uart(port, type->xoff_char, stop_char);
674 	} else {
675 		dev_dbg(&port->dev, "Disabling flow ctrl\n");
676 		flow = XR_UART_FLOW_MODE_NONE;
677 	}
678 
679 	xr_set_reg_uart(port, type->flow_control, flow);
680 	xr_set_reg_uart(port, type->gpio_mode, gpio_mode);
681 
682 	xr_uart_enable(port);
683 
684 	if (C_BAUD(tty) == B0)
685 		xr_dtr_rts(port, 0);
686 	else if (old_termios && (old_termios->c_cflag & CBAUD) == B0)
687 		xr_dtr_rts(port, 1);
688 }
689 
xr21v141x_set_line_settings(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)690 static void xr21v141x_set_line_settings(struct tty_struct *tty,
691 				        struct usb_serial_port *port,
692 				        const struct ktermios *old_termios)
693 {
694 	struct ktermios *termios = &tty->termios;
695 	u8 bits = 0;
696 	int ret;
697 
698 	if (!old_termios || (tty->termios.c_ospeed != old_termios->c_ospeed))
699 		xr21v141x_set_baudrate(tty, port);
700 
701 	switch (C_CSIZE(tty)) {
702 	case CS5:
703 	case CS6:
704 		/* CS5 and CS6 are not supported, so just restore old setting */
705 		termios->c_cflag &= ~CSIZE;
706 		if (old_termios)
707 			termios->c_cflag |= old_termios->c_cflag & CSIZE;
708 		else
709 			termios->c_cflag |= CS8;
710 
711 		if (C_CSIZE(tty) == CS7)
712 			bits |= XR_UART_DATA_7;
713 		else
714 			bits |= XR_UART_DATA_8;
715 		break;
716 	case CS7:
717 		bits |= XR_UART_DATA_7;
718 		break;
719 	case CS8:
720 	default:
721 		bits |= XR_UART_DATA_8;
722 		break;
723 	}
724 
725 	if (C_PARENB(tty)) {
726 		if (C_CMSPAR(tty)) {
727 			if (C_PARODD(tty))
728 				bits |= XR_UART_PARITY_MARK;
729 			else
730 				bits |= XR_UART_PARITY_SPACE;
731 		} else {
732 			if (C_PARODD(tty))
733 				bits |= XR_UART_PARITY_ODD;
734 			else
735 				bits |= XR_UART_PARITY_EVEN;
736 		}
737 	}
738 
739 	if (C_CSTOPB(tty))
740 		bits |= XR_UART_STOP_2;
741 	else
742 		bits |= XR_UART_STOP_1;
743 
744 	ret = xr_set_reg_uart(port, XR21V141X_REG_FORMAT, bits);
745 	if (ret)
746 		return;
747 }
748 
xr_cdc_set_line_coding(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)749 static void xr_cdc_set_line_coding(struct tty_struct *tty,
750 				   struct usb_serial_port *port,
751 				   const struct ktermios *old_termios)
752 {
753 	struct xr_data *data = usb_get_serial_port_data(port);
754 	struct usb_host_interface *alt = port->serial->interface->cur_altsetting;
755 	struct usb_device *udev = port->serial->dev;
756 	struct usb_cdc_line_coding *lc;
757 	int ret;
758 
759 	lc = kzalloc(sizeof(*lc), GFP_KERNEL);
760 	if (!lc)
761 		return;
762 
763 	if (tty->termios.c_ospeed)
764 		lc->dwDTERate = cpu_to_le32(tty->termios.c_ospeed);
765 	else
766 		lc->dwDTERate = cpu_to_le32(9600);
767 
768 	if (C_CSTOPB(tty))
769 		lc->bCharFormat = USB_CDC_2_STOP_BITS;
770 	else
771 		lc->bCharFormat = USB_CDC_1_STOP_BITS;
772 
773 	if (C_PARENB(tty)) {
774 		if (C_CMSPAR(tty)) {
775 			if (C_PARODD(tty))
776 				lc->bParityType = USB_CDC_MARK_PARITY;
777 			else
778 				lc->bParityType = USB_CDC_SPACE_PARITY;
779 		} else {
780 			if (C_PARODD(tty))
781 				lc->bParityType = USB_CDC_ODD_PARITY;
782 			else
783 				lc->bParityType = USB_CDC_EVEN_PARITY;
784 		}
785 	} else {
786 		lc->bParityType = USB_CDC_NO_PARITY;
787 	}
788 
789 	if (!data->type->have_5_6_bit_mode &&
790 			(C_CSIZE(tty) == CS5 || C_CSIZE(tty) == CS6)) {
791 		tty->termios.c_cflag &= ~CSIZE;
792 		if (old_termios)
793 			tty->termios.c_cflag |= old_termios->c_cflag & CSIZE;
794 		else
795 			tty->termios.c_cflag |= CS8;
796 	}
797 
798 	switch (C_CSIZE(tty)) {
799 	case CS5:
800 		lc->bDataBits = 5;
801 		break;
802 	case CS6:
803 		lc->bDataBits = 6;
804 		break;
805 	case CS7:
806 		lc->bDataBits = 7;
807 		break;
808 	case CS8:
809 	default:
810 		lc->bDataBits = 8;
811 		break;
812 	}
813 
814 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
815 			USB_CDC_REQ_SET_LINE_CODING,
816 			USB_TYPE_CLASS | USB_RECIP_INTERFACE,
817 			0, alt->desc.bInterfaceNumber,
818 			lc, sizeof(*lc), USB_CTRL_SET_TIMEOUT);
819 	if (ret < 0)
820 		dev_err(&port->dev, "Failed to set line coding: %d\n", ret);
821 
822 	kfree(lc);
823 }
824 
xr_sanitize_serial_rs485(struct serial_rs485 * rs485)825 static void xr_sanitize_serial_rs485(struct serial_rs485 *rs485)
826 {
827 	if (!(rs485->flags & SER_RS485_ENABLED)) {
828 		memset(rs485, 0, sizeof(*rs485));
829 		return;
830 	}
831 
832 	/* RTS always toggles after TX */
833 	if (rs485->flags & SER_RS485_RTS_ON_SEND)
834 		rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
835 	else
836 		rs485->flags |= SER_RS485_RTS_AFTER_SEND;
837 
838 	/* Only the flags are implemented at the moment */
839 	rs485->flags &= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND |
840 			SER_RS485_RTS_AFTER_SEND;
841 	rs485->delay_rts_before_send = 0;
842 	rs485->delay_rts_after_send = 0;
843 	memset(rs485->padding, 0, sizeof(rs485->padding));
844 }
845 
xr_get_rs485_config(struct tty_struct * tty,struct serial_rs485 __user * argp)846 static int xr_get_rs485_config(struct tty_struct *tty,
847 			       struct serial_rs485 __user *argp)
848 {
849 	struct usb_serial_port *port = tty->driver_data;
850 	struct xr_data *data = usb_get_serial_port_data(port);
851 
852 	down_read(&tty->termios_rwsem);
853 	if (copy_to_user(argp, &data->rs485, sizeof(data->rs485))) {
854 		up_read(&tty->termios_rwsem);
855 		return -EFAULT;
856 	}
857 	up_read(&tty->termios_rwsem);
858 
859 	return 0;
860 }
861 
xr_set_rs485_config(struct tty_struct * tty,struct serial_rs485 __user * argp)862 static int xr_set_rs485_config(struct tty_struct *tty,
863 			       struct serial_rs485 __user *argp)
864 {
865 	struct usb_serial_port *port = tty->driver_data;
866 	struct xr_data *data = usb_get_serial_port_data(port);
867 	struct serial_rs485 rs485;
868 
869 	if (copy_from_user(&rs485, argp, sizeof(rs485)))
870 		return -EFAULT;
871 	xr_sanitize_serial_rs485(&rs485);
872 
873 	down_write(&tty->termios_rwsem);
874 	data->rs485 = rs485;
875 	xr_set_flow_mode(tty, port, NULL);
876 	up_write(&tty->termios_rwsem);
877 
878 	if (copy_to_user(argp, &rs485, sizeof(rs485)))
879 		return -EFAULT;
880 
881 	return 0;
882 }
883 
xr_ioctl(struct tty_struct * tty,unsigned int cmd,unsigned long arg)884 static int xr_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
885 {
886 	void __user *argp = (void __user *)arg;
887 
888 	switch (cmd) {
889 	case TIOCGRS485:
890 		return xr_get_rs485_config(tty, argp);
891 	case TIOCSRS485:
892 		return xr_set_rs485_config(tty, argp);
893 	}
894 
895 	return -ENOIOCTLCMD;
896 }
897 
xr_set_termios(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)898 static void xr_set_termios(struct tty_struct *tty,
899 			   struct usb_serial_port *port,
900 			   const struct ktermios *old_termios)
901 {
902 	struct xr_data *data = usb_get_serial_port_data(port);
903 
904 	/*
905 	 * XR21V141X does not have a CUSTOM_DRIVER flag and always enters CDC
906 	 * mode upon receiving CDC requests.
907 	 */
908 	if (data->type->set_line_settings)
909 		data->type->set_line_settings(tty, port, old_termios);
910 	else
911 		xr_cdc_set_line_coding(tty, port, old_termios);
912 
913 	xr_set_flow_mode(tty, port, old_termios);
914 }
915 
xr_open(struct tty_struct * tty,struct usb_serial_port * port)916 static int xr_open(struct tty_struct *tty, struct usb_serial_port *port)
917 {
918 	int ret;
919 
920 	ret = xr_fifo_reset(port);
921 	if (ret)
922 		return ret;
923 
924 	ret = xr_uart_enable(port);
925 	if (ret) {
926 		dev_err(&port->dev, "Failed to enable UART\n");
927 		return ret;
928 	}
929 
930 	/* Setup termios */
931 	if (tty)
932 		xr_set_termios(tty, port, NULL);
933 
934 	ret = usb_serial_generic_open(tty, port);
935 	if (ret) {
936 		xr_uart_disable(port);
937 		return ret;
938 	}
939 
940 	return 0;
941 }
942 
xr_close(struct usb_serial_port * port)943 static void xr_close(struct usb_serial_port *port)
944 {
945 	usb_serial_generic_close(port);
946 
947 	xr_uart_disable(port);
948 }
949 
xr_probe(struct usb_serial * serial,const struct usb_device_id * id)950 static int xr_probe(struct usb_serial *serial, const struct usb_device_id *id)
951 {
952 	struct usb_interface *control = serial->interface;
953 	struct usb_host_interface *alt = control->cur_altsetting;
954 	struct usb_cdc_parsed_header hdrs;
955 	struct usb_cdc_union_desc *desc;
956 	struct usb_interface *data;
957 	int ret;
958 
959 	ret = cdc_parse_cdc_header(&hdrs, control, alt->extra, alt->extralen);
960 	if (ret < 0)
961 		return -ENODEV;
962 
963 	desc = hdrs.usb_cdc_union_desc;
964 	if (!desc)
965 		return -ENODEV;
966 
967 	data = usb_ifnum_to_if(serial->dev, desc->bSlaveInterface0);
968 	if (!data)
969 		return -ENODEV;
970 
971 	ret = usb_serial_claim_interface(serial, data);
972 	if (ret)
973 		return ret;
974 
975 	usb_set_serial_data(serial, (void *)id->driver_info);
976 
977 	return 0;
978 }
979 
xr_gpio_init(struct usb_serial_port * port,const struct xr_type * type)980 static int xr_gpio_init(struct usb_serial_port *port, const struct xr_type *type)
981 {
982 	u16 mask, mode;
983 	int ret;
984 
985 	/*
986 	 * Configure all pins as GPIO except for Receive and Transmit Toggle.
987 	 */
988 	mode = 0;
989 	if (type->have_xmit_toggle)
990 		mode |= XR_GPIO_MODE_RX_TOGGLE | XR_GPIO_MODE_TX_TOGGLE;
991 
992 	ret = xr_set_reg_uart(port, type->gpio_mode, mode);
993 	if (ret)
994 		return ret;
995 
996 	/*
997 	 * Configure DTR and RTS as outputs and make sure they are deasserted
998 	 * (active low), and configure RI, CD, DSR and CTS as inputs.
999 	 */
1000 	mask = XR_GPIO_DTR | XR_GPIO_RTS;
1001 	ret = xr_set_reg_uart(port, type->gpio_direction, mask);
1002 	if (ret)
1003 		return ret;
1004 
1005 	ret = xr_set_reg_uart(port, type->gpio_set, mask);
1006 	if (ret)
1007 		return ret;
1008 
1009 	return 0;
1010 }
1011 
xr_port_probe(struct usb_serial_port * port)1012 static int xr_port_probe(struct usb_serial_port *port)
1013 {
1014 	struct usb_interface_descriptor *desc;
1015 	const struct xr_type *type;
1016 	struct xr_data *data;
1017 	enum xr_type_id type_id;
1018 	int ret;
1019 
1020 	type_id = (int)(unsigned long)usb_get_serial_data(port->serial);
1021 	type = &xr_types[type_id];
1022 
1023 	data = kzalloc(sizeof(*data), GFP_KERNEL);
1024 	if (!data)
1025 		return -ENOMEM;
1026 
1027 	data->type = type;
1028 
1029 	desc = &port->serial->interface->cur_altsetting->desc;
1030 	if (type_id == XR21V141X)
1031 		data->channel = desc->bInterfaceNumber / 2;
1032 	else
1033 		data->channel = desc->bInterfaceNumber;
1034 
1035 	usb_set_serial_port_data(port, data);
1036 
1037 	if (type->custom_driver) {
1038 		ret = xr_set_reg_uart(port, type->custom_driver,
1039 				XR_CUSTOM_DRIVER_ACTIVE);
1040 		if (ret)
1041 			goto err_free;
1042 	}
1043 
1044 	ret = xr_gpio_init(port, type);
1045 	if (ret)
1046 		goto err_free;
1047 
1048 	return 0;
1049 
1050 err_free:
1051 	kfree(data);
1052 
1053 	return ret;
1054 }
1055 
xr_port_remove(struct usb_serial_port * port)1056 static void xr_port_remove(struct usb_serial_port *port)
1057 {
1058 	struct xr_data *data = usb_get_serial_port_data(port);
1059 
1060 	kfree(data);
1061 }
1062 
1063 #define XR_DEVICE(vid, pid, type)					\
1064 	USB_DEVICE_INTERFACE_CLASS((vid), (pid), USB_CLASS_COMM),	\
1065 	.driver_info = (type)
1066 
1067 static const struct usb_device_id id_table[] = {
1068 	{ XR_DEVICE(0x04e2, 0x1400, XR2280X) },
1069 	{ XR_DEVICE(0x04e2, 0x1401, XR2280X) },
1070 	{ XR_DEVICE(0x04e2, 0x1402, XR2280X) },
1071 	{ XR_DEVICE(0x04e2, 0x1403, XR2280X) },
1072 	{ XR_DEVICE(0x04e2, 0x1410, XR21V141X) },
1073 	{ XR_DEVICE(0x04e2, 0x1411, XR21B1411) },
1074 	{ XR_DEVICE(0x04e2, 0x1412, XR21V141X) },
1075 	{ XR_DEVICE(0x04e2, 0x1414, XR21V141X) },
1076 	{ XR_DEVICE(0x04e2, 0x1420, XR21B142X) },
1077 	{ XR_DEVICE(0x04e2, 0x1422, XR21B142X) },
1078 	{ XR_DEVICE(0x04e2, 0x1424, XR21B142X) },
1079 	{ }
1080 };
1081 MODULE_DEVICE_TABLE(usb, id_table);
1082 
1083 static struct usb_serial_driver xr_device = {
1084 	.driver = {
1085 		.owner = THIS_MODULE,
1086 		.name =	"xr_serial",
1087 	},
1088 	.id_table		= id_table,
1089 	.num_ports		= 1,
1090 	.probe			= xr_probe,
1091 	.port_probe		= xr_port_probe,
1092 	.port_remove		= xr_port_remove,
1093 	.open			= xr_open,
1094 	.close			= xr_close,
1095 	.break_ctl		= xr_break_ctl,
1096 	.set_termios		= xr_set_termios,
1097 	.tiocmget		= xr_tiocmget,
1098 	.tiocmset		= xr_tiocmset,
1099 	.ioctl			= xr_ioctl,
1100 	.dtr_rts		= xr_dtr_rts
1101 };
1102 
1103 static struct usb_serial_driver * const serial_drivers[] = {
1104 	&xr_device, NULL
1105 };
1106 
1107 module_usb_serial_driver(serial_drivers, id_table);
1108 
1109 MODULE_AUTHOR("Manivannan Sadhasivam <mani@kernel.org>");
1110 MODULE_DESCRIPTION("MaxLinear/Exar USB to Serial driver");
1111 MODULE_LICENSE("GPL");
1112