| .. | .. |
|---|
| 248 | 248 | struct max310x_devtype { |
|---|
| 249 | 249 | char name[9]; |
|---|
| 250 | 250 | int nr; |
|---|
| 251 | + u8 mode1; |
|---|
| 251 | 252 | int (*detect)(struct device *); |
|---|
| 252 | 253 | void (*power)(struct uart_port *, int); |
|---|
| 253 | 254 | }; |
|---|
| .. | .. |
|---|
| 257 | 258 | struct work_struct tx_work; |
|---|
| 258 | 259 | struct work_struct md_work; |
|---|
| 259 | 260 | struct work_struct rs_work; |
|---|
| 261 | + |
|---|
| 262 | + u8 wr_header; |
|---|
| 263 | + u8 rd_header; |
|---|
| 264 | + u8 rx_buf[MAX310X_FIFO_SIZE]; |
|---|
| 260 | 265 | }; |
|---|
| 266 | +#define to_max310x_port(_port) \ |
|---|
| 267 | + container_of(_port, struct max310x_one, port) |
|---|
| 261 | 268 | |
|---|
| 262 | 269 | struct max310x_port { |
|---|
| 263 | 270 | struct max310x_devtype *devtype; |
|---|
| 264 | 271 | struct regmap *regmap; |
|---|
| 265 | | - struct mutex mutex; |
|---|
| 266 | 272 | struct clk *clk; |
|---|
| 267 | 273 | #ifdef CONFIG_GPIOLIB |
|---|
| 268 | 274 | struct gpio_chip gpio; |
|---|
| .. | .. |
|---|
| 410 | 416 | static const struct max310x_devtype max3107_devtype = { |
|---|
| 411 | 417 | .name = "MAX3107", |
|---|
| 412 | 418 | .nr = 1, |
|---|
| 419 | + .mode1 = MAX310X_MODE1_AUTOSLEEP_BIT | MAX310X_MODE1_IRQSEL_BIT, |
|---|
| 413 | 420 | .detect = max3107_detect, |
|---|
| 414 | 421 | .power = max310x_power, |
|---|
| 415 | 422 | }; |
|---|
| .. | .. |
|---|
| 417 | 424 | static const struct max310x_devtype max3108_devtype = { |
|---|
| 418 | 425 | .name = "MAX3108", |
|---|
| 419 | 426 | .nr = 1, |
|---|
| 427 | + .mode1 = MAX310X_MODE1_AUTOSLEEP_BIT, |
|---|
| 420 | 428 | .detect = max3108_detect, |
|---|
| 421 | 429 | .power = max310x_power, |
|---|
| 422 | 430 | }; |
|---|
| .. | .. |
|---|
| 424 | 432 | static const struct max310x_devtype max3109_devtype = { |
|---|
| 425 | 433 | .name = "MAX3109", |
|---|
| 426 | 434 | .nr = 2, |
|---|
| 435 | + .mode1 = MAX310X_MODE1_AUTOSLEEP_BIT, |
|---|
| 427 | 436 | .detect = max3109_detect, |
|---|
| 428 | 437 | .power = max310x_power, |
|---|
| 429 | 438 | }; |
|---|
| .. | .. |
|---|
| 431 | 440 | static const struct max310x_devtype max14830_devtype = { |
|---|
| 432 | 441 | .name = "MAX14830", |
|---|
| 433 | 442 | .nr = 4, |
|---|
| 443 | + .mode1 = MAX310X_MODE1_IRQSEL_BIT, |
|---|
| 434 | 444 | .detect = max14830_detect, |
|---|
| 435 | 445 | .power = max14830_power, |
|---|
| 436 | 446 | }; |
|---|
| .. | .. |
|---|
| 613 | 623 | |
|---|
| 614 | 624 | static void max310x_batch_write(struct uart_port *port, u8 *txbuf, unsigned int len) |
|---|
| 615 | 625 | { |
|---|
| 616 | | - u8 header[] = { (port->iobase + MAX310X_THR_REG) | MAX310X_WRITE_BIT }; |
|---|
| 626 | + struct max310x_one *one = to_max310x_port(port); |
|---|
| 617 | 627 | struct spi_transfer xfer[] = { |
|---|
| 618 | 628 | { |
|---|
| 619 | | - .tx_buf = &header, |
|---|
| 620 | | - .len = sizeof(header), |
|---|
| 629 | + .tx_buf = &one->wr_header, |
|---|
| 630 | + .len = sizeof(one->wr_header), |
|---|
| 621 | 631 | }, { |
|---|
| 622 | 632 | .tx_buf = txbuf, |
|---|
| 623 | 633 | .len = len, |
|---|
| .. | .. |
|---|
| 628 | 638 | |
|---|
| 629 | 639 | static void max310x_batch_read(struct uart_port *port, u8 *rxbuf, unsigned int len) |
|---|
| 630 | 640 | { |
|---|
| 631 | | - u8 header[] = { port->iobase + MAX310X_RHR_REG }; |
|---|
| 641 | + struct max310x_one *one = to_max310x_port(port); |
|---|
| 632 | 642 | struct spi_transfer xfer[] = { |
|---|
| 633 | 643 | { |
|---|
| 634 | | - .tx_buf = &header, |
|---|
| 635 | | - .len = sizeof(header), |
|---|
| 644 | + .tx_buf = &one->rd_header, |
|---|
| 645 | + .len = sizeof(one->rd_header), |
|---|
| 636 | 646 | }, { |
|---|
| 637 | 647 | .rx_buf = rxbuf, |
|---|
| 638 | 648 | .len = len, |
|---|
| .. | .. |
|---|
| 643 | 653 | |
|---|
| 644 | 654 | static void max310x_handle_rx(struct uart_port *port, unsigned int rxlen) |
|---|
| 645 | 655 | { |
|---|
| 656 | + struct max310x_one *one = to_max310x_port(port); |
|---|
| 646 | 657 | unsigned int sts, ch, flag, i; |
|---|
| 647 | | - u8 buf[MAX310X_FIFO_SIZE]; |
|---|
| 648 | 658 | |
|---|
| 649 | 659 | if (port->read_status_mask == MAX310X_LSR_RXOVR_BIT) { |
|---|
| 650 | 660 | /* We are just reading, happily ignoring any error conditions. |
|---|
| .. | .. |
|---|
| 659 | 669 | * */ |
|---|
| 660 | 670 | |
|---|
| 661 | 671 | sts = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG); |
|---|
| 662 | | - max310x_batch_read(port, buf, rxlen); |
|---|
| 672 | + max310x_batch_read(port, one->rx_buf, rxlen); |
|---|
| 663 | 673 | |
|---|
| 664 | 674 | port->icount.rx += rxlen; |
|---|
| 665 | 675 | flag = TTY_NORMAL; |
|---|
| .. | .. |
|---|
| 670 | 680 | port->icount.overrun++; |
|---|
| 671 | 681 | } |
|---|
| 672 | 682 | |
|---|
| 673 | | - for (i = 0; i < rxlen; ++i) { |
|---|
| 674 | | - uart_insert_char(port, sts, MAX310X_LSR_RXOVR_BIT, buf[i], flag); |
|---|
| 675 | | - } |
|---|
| 683 | + for (i = 0; i < (rxlen - 1); ++i) |
|---|
| 684 | + uart_insert_char(port, sts, 0, one->rx_buf[i], flag); |
|---|
| 685 | + |
|---|
| 686 | + /* |
|---|
| 687 | + * Handle the overrun case for the last character only, since |
|---|
| 688 | + * the RxFIFO overflow happens after it is pushed to the FIFO |
|---|
| 689 | + * tail. |
|---|
| 690 | + */ |
|---|
| 691 | + uart_insert_char(port, sts, MAX310X_LSR_RXOVR_BIT, |
|---|
| 692 | + one->rx_buf[rxlen-1], flag); |
|---|
| 676 | 693 | |
|---|
| 677 | 694 | } else { |
|---|
| 678 | 695 | if (unlikely(rxlen >= port->fifosize)) { |
|---|
| .. | .. |
|---|
| 772 | 789 | |
|---|
| 773 | 790 | static void max310x_start_tx(struct uart_port *port) |
|---|
| 774 | 791 | { |
|---|
| 775 | | - struct max310x_one *one = container_of(port, struct max310x_one, port); |
|---|
| 792 | + struct max310x_one *one = to_max310x_port(port); |
|---|
| 776 | 793 | |
|---|
| 777 | | - if (!work_pending(&one->tx_work)) |
|---|
| 778 | | - schedule_work(&one->tx_work); |
|---|
| 794 | + schedule_work(&one->tx_work); |
|---|
| 779 | 795 | } |
|---|
| 780 | 796 | |
|---|
| 781 | 797 | static irqreturn_t max310x_port_irq(struct max310x_port *s, int portno) |
|---|
| .. | .. |
|---|
| 832 | 848 | return IRQ_RETVAL(handled); |
|---|
| 833 | 849 | } |
|---|
| 834 | 850 | |
|---|
| 835 | | -static void max310x_wq_proc(struct work_struct *ws) |
|---|
| 851 | +static void max310x_tx_proc(struct work_struct *ws) |
|---|
| 836 | 852 | { |
|---|
| 837 | 853 | struct max310x_one *one = container_of(ws, struct max310x_one, tx_work); |
|---|
| 838 | | - struct max310x_port *s = dev_get_drvdata(one->port.dev); |
|---|
| 839 | 854 | |
|---|
| 840 | | - mutex_lock(&s->mutex); |
|---|
| 841 | 855 | max310x_handle_tx(&one->port); |
|---|
| 842 | | - mutex_unlock(&s->mutex); |
|---|
| 843 | 856 | } |
|---|
| 844 | 857 | |
|---|
| 845 | 858 | static unsigned int max310x_tx_empty(struct uart_port *port) |
|---|
| .. | .. |
|---|
| 869 | 882 | |
|---|
| 870 | 883 | static void max310x_set_mctrl(struct uart_port *port, unsigned int mctrl) |
|---|
| 871 | 884 | { |
|---|
| 872 | | - struct max310x_one *one = container_of(port, struct max310x_one, port); |
|---|
| 885 | + struct max310x_one *one = to_max310x_port(port); |
|---|
| 873 | 886 | |
|---|
| 874 | 887 | schedule_work(&one->md_work); |
|---|
| 875 | 888 | } |
|---|
| .. | .. |
|---|
| 942 | 955 | /* Configure flow control */ |
|---|
| 943 | 956 | max310x_port_write(port, MAX310X_XON1_REG, termios->c_cc[VSTART]); |
|---|
| 944 | 957 | max310x_port_write(port, MAX310X_XOFF1_REG, termios->c_cc[VSTOP]); |
|---|
| 945 | | - if (termios->c_cflag & CRTSCTS) |
|---|
| 958 | + |
|---|
| 959 | + /* Disable transmitter before enabling AutoCTS or auto transmitter |
|---|
| 960 | + * flow control |
|---|
| 961 | + */ |
|---|
| 962 | + if (termios->c_cflag & CRTSCTS || termios->c_iflag & IXOFF) { |
|---|
| 963 | + max310x_port_update(port, MAX310X_MODE1_REG, |
|---|
| 964 | + MAX310X_MODE1_TXDIS_BIT, |
|---|
| 965 | + MAX310X_MODE1_TXDIS_BIT); |
|---|
| 966 | + } |
|---|
| 967 | + |
|---|
| 968 | + port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS | UPSTAT_AUTOXOFF); |
|---|
| 969 | + |
|---|
| 970 | + if (termios->c_cflag & CRTSCTS) { |
|---|
| 971 | + /* Enable AUTORTS and AUTOCTS */ |
|---|
| 972 | + port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS; |
|---|
| 946 | 973 | flow |= MAX310X_FLOWCTRL_AUTOCTS_BIT | |
|---|
| 947 | 974 | MAX310X_FLOWCTRL_AUTORTS_BIT; |
|---|
| 975 | + } |
|---|
| 948 | 976 | if (termios->c_iflag & IXON) |
|---|
| 949 | 977 | flow |= MAX310X_FLOWCTRL_SWFLOW3_BIT | |
|---|
| 950 | 978 | MAX310X_FLOWCTRL_SWFLOWEN_BIT; |
|---|
| 951 | | - if (termios->c_iflag & IXOFF) |
|---|
| 979 | + if (termios->c_iflag & IXOFF) { |
|---|
| 980 | + port->status |= UPSTAT_AUTOXOFF; |
|---|
| 952 | 981 | flow |= MAX310X_FLOWCTRL_SWFLOW1_BIT | |
|---|
| 953 | 982 | MAX310X_FLOWCTRL_SWFLOWEN_BIT; |
|---|
| 983 | + } |
|---|
| 954 | 984 | max310x_port_write(port, MAX310X_FLOWCTRL_REG, flow); |
|---|
| 985 | + |
|---|
| 986 | + /* Enable transmitter after disabling AutoCTS and auto transmitter |
|---|
| 987 | + * flow control |
|---|
| 988 | + */ |
|---|
| 989 | + if (!(termios->c_cflag & CRTSCTS) && !(termios->c_iflag & IXOFF)) { |
|---|
| 990 | + max310x_port_update(port, MAX310X_MODE1_REG, |
|---|
| 991 | + MAX310X_MODE1_TXDIS_BIT, |
|---|
| 992 | + 0); |
|---|
| 993 | + } |
|---|
| 955 | 994 | |
|---|
| 956 | 995 | /* Get baud rate generator configuration */ |
|---|
| 957 | 996 | baud = uart_get_baud_rate(port, termios, old, |
|---|
| .. | .. |
|---|
| 968 | 1007 | static void max310x_rs_proc(struct work_struct *ws) |
|---|
| 969 | 1008 | { |
|---|
| 970 | 1009 | struct max310x_one *one = container_of(ws, struct max310x_one, rs_work); |
|---|
| 971 | | - unsigned int val; |
|---|
| 1010 | + unsigned int delay, mode1 = 0, mode2 = 0; |
|---|
| 972 | 1011 | |
|---|
| 973 | | - val = (one->port.rs485.delay_rts_before_send << 4) | |
|---|
| 1012 | + delay = (one->port.rs485.delay_rts_before_send << 4) | |
|---|
| 974 | 1013 | one->port.rs485.delay_rts_after_send; |
|---|
| 975 | | - max310x_port_write(&one->port, MAX310X_HDPIXDELAY_REG, val); |
|---|
| 1014 | + max310x_port_write(&one->port, MAX310X_HDPIXDELAY_REG, delay); |
|---|
| 976 | 1015 | |
|---|
| 977 | 1016 | if (one->port.rs485.flags & SER_RS485_ENABLED) { |
|---|
| 978 | | - max310x_port_update(&one->port, MAX310X_MODE1_REG, |
|---|
| 979 | | - MAX310X_MODE1_TRNSCVCTRL_BIT, |
|---|
| 980 | | - MAX310X_MODE1_TRNSCVCTRL_BIT); |
|---|
| 981 | | - max310x_port_update(&one->port, MAX310X_MODE2_REG, |
|---|
| 982 | | - MAX310X_MODE2_ECHOSUPR_BIT, |
|---|
| 983 | | - MAX310X_MODE2_ECHOSUPR_BIT); |
|---|
| 984 | | - } else { |
|---|
| 985 | | - max310x_port_update(&one->port, MAX310X_MODE1_REG, |
|---|
| 986 | | - MAX310X_MODE1_TRNSCVCTRL_BIT, 0); |
|---|
| 987 | | - max310x_port_update(&one->port, MAX310X_MODE2_REG, |
|---|
| 988 | | - MAX310X_MODE2_ECHOSUPR_BIT, 0); |
|---|
| 1017 | + mode1 = MAX310X_MODE1_TRNSCVCTRL_BIT; |
|---|
| 1018 | + |
|---|
| 1019 | + if (!(one->port.rs485.flags & SER_RS485_RX_DURING_TX)) |
|---|
| 1020 | + mode2 = MAX310X_MODE2_ECHOSUPR_BIT; |
|---|
| 989 | 1021 | } |
|---|
| 1022 | + |
|---|
| 1023 | + max310x_port_update(&one->port, MAX310X_MODE1_REG, |
|---|
| 1024 | + MAX310X_MODE1_TRNSCVCTRL_BIT, mode1); |
|---|
| 1025 | + max310x_port_update(&one->port, MAX310X_MODE2_REG, |
|---|
| 1026 | + MAX310X_MODE2_ECHOSUPR_BIT, mode2); |
|---|
| 990 | 1027 | } |
|---|
| 991 | 1028 | |
|---|
| 992 | 1029 | static int max310x_rs485_config(struct uart_port *port, |
|---|
| 993 | 1030 | struct serial_rs485 *rs485) |
|---|
| 994 | 1031 | { |
|---|
| 995 | | - struct max310x_one *one = container_of(port, struct max310x_one, port); |
|---|
| 1032 | + struct max310x_one *one = to_max310x_port(port); |
|---|
| 996 | 1033 | |
|---|
| 997 | 1034 | if ((rs485->delay_rts_before_send > 0x0f) || |
|---|
| 998 | 1035 | (rs485->delay_rts_after_send > 0x0f)) |
|---|
| 999 | 1036 | return -ERANGE; |
|---|
| 1000 | 1037 | |
|---|
| 1001 | | - rs485->flags &= SER_RS485_RTS_ON_SEND | SER_RS485_ENABLED; |
|---|
| 1038 | + rs485->flags &= SER_RS485_RTS_ON_SEND | SER_RS485_RX_DURING_TX | |
|---|
| 1039 | + SER_RS485_ENABLED; |
|---|
| 1002 | 1040 | memset(rs485->padding, 0, sizeof(rs485->padding)); |
|---|
| 1003 | 1041 | port->rs485 = *rs485; |
|---|
| 1004 | 1042 | |
|---|
| .. | .. |
|---|
| 1023 | 1061 | max310x_port_write(port, MAX310X_MODE2_REG, val); |
|---|
| 1024 | 1062 | max310x_port_update(port, MAX310X_MODE2_REG, |
|---|
| 1025 | 1063 | MAX310X_MODE2_FIFORST_BIT, 0); |
|---|
| 1064 | + |
|---|
| 1065 | + /* Configure mode1/mode2 to have rs485/rs232 enabled at startup */ |
|---|
| 1066 | + val = (clamp(port->rs485.delay_rts_before_send, 0U, 15U) << 4) | |
|---|
| 1067 | + clamp(port->rs485.delay_rts_after_send, 0U, 15U); |
|---|
| 1068 | + max310x_port_write(port, MAX310X_HDPIXDELAY_REG, val); |
|---|
| 1069 | + |
|---|
| 1070 | + if (port->rs485.flags & SER_RS485_ENABLED) { |
|---|
| 1071 | + max310x_port_update(port, MAX310X_MODE1_REG, |
|---|
| 1072 | + MAX310X_MODE1_TRNSCVCTRL_BIT, |
|---|
| 1073 | + MAX310X_MODE1_TRNSCVCTRL_BIT); |
|---|
| 1074 | + |
|---|
| 1075 | + if (!(port->rs485.flags & SER_RS485_RX_DURING_TX)) |
|---|
| 1076 | + max310x_port_update(port, MAX310X_MODE2_REG, |
|---|
| 1077 | + MAX310X_MODE2_ECHOSUPR_BIT, |
|---|
| 1078 | + MAX310X_MODE2_ECHOSUPR_BIT); |
|---|
| 1079 | + } |
|---|
| 1026 | 1080 | |
|---|
| 1027 | 1081 | /* Configure flow control levels */ |
|---|
| 1028 | 1082 | /* Flow control halt level 96, resume level 48 */ |
|---|
| .. | .. |
|---|
| 1208 | 1262 | return PTR_ERR(regmap); |
|---|
| 1209 | 1263 | |
|---|
| 1210 | 1264 | /* Alloc port structure */ |
|---|
| 1211 | | - s = devm_kzalloc(dev, sizeof(*s) + |
|---|
| 1212 | | - sizeof(struct max310x_one) * devtype->nr, GFP_KERNEL); |
|---|
| 1265 | + s = devm_kzalloc(dev, struct_size(s, p, devtype->nr), GFP_KERNEL); |
|---|
| 1213 | 1266 | if (!s) { |
|---|
| 1214 | 1267 | dev_err(dev, "Error allocating port structure\n"); |
|---|
| 1215 | 1268 | return -ENOMEM; |
|---|
| .. | .. |
|---|
| 1269 | 1322 | MAX310X_BRGDIVLSB_REG + offs, &ret); |
|---|
| 1270 | 1323 | } while (ret != 0x01); |
|---|
| 1271 | 1324 | |
|---|
| 1272 | | - regmap_update_bits(s->regmap, MAX310X_MODE1_REG + offs, |
|---|
| 1273 | | - MAX310X_MODE1_AUTOSLEEP_BIT, |
|---|
| 1274 | | - MAX310X_MODE1_AUTOSLEEP_BIT); |
|---|
| 1325 | + regmap_write(s->regmap, MAX310X_MODE1_REG + offs, |
|---|
| 1326 | + devtype->mode1); |
|---|
| 1275 | 1327 | } |
|---|
| 1276 | 1328 | |
|---|
| 1277 | 1329 | uartclk = max310x_set_ref_clk(dev, s, freq, xtal); |
|---|
| 1278 | 1330 | dev_dbg(dev, "Reference clock set to %i Hz\n", uartclk); |
|---|
| 1279 | | - |
|---|
| 1280 | | - mutex_init(&s->mutex); |
|---|
| 1281 | 1331 | |
|---|
| 1282 | 1332 | for (i = 0; i < devtype->nr; i++) { |
|---|
| 1283 | 1333 | unsigned int line; |
|---|
| .. | .. |
|---|
| 1305 | 1355 | max310x_port_write(&s->p[i].port, MAX310X_IRQEN_REG, 0); |
|---|
| 1306 | 1356 | /* Clear IRQ status register */ |
|---|
| 1307 | 1357 | max310x_port_read(&s->p[i].port, MAX310X_IRQSTS_REG); |
|---|
| 1308 | | - /* Enable IRQ pin */ |
|---|
| 1309 | | - max310x_port_update(&s->p[i].port, MAX310X_MODE1_REG, |
|---|
| 1310 | | - MAX310X_MODE1_IRQSEL_BIT, |
|---|
| 1311 | | - MAX310X_MODE1_IRQSEL_BIT); |
|---|
| 1312 | 1358 | /* Initialize queue for start TX */ |
|---|
| 1313 | | - INIT_WORK(&s->p[i].tx_work, max310x_wq_proc); |
|---|
| 1359 | + INIT_WORK(&s->p[i].tx_work, max310x_tx_proc); |
|---|
| 1314 | 1360 | /* Initialize queue for changing LOOPBACK mode */ |
|---|
| 1315 | 1361 | INIT_WORK(&s->p[i].md_work, max310x_md_proc); |
|---|
| 1316 | 1362 | /* Initialize queue for changing RS485 mode */ |
|---|
| 1317 | 1363 | INIT_WORK(&s->p[i].rs_work, max310x_rs_proc); |
|---|
| 1364 | + /* Initialize SPI-transfer buffers */ |
|---|
| 1365 | + s->p[i].wr_header = (s->p[i].port.iobase + MAX310X_THR_REG) | |
|---|
| 1366 | + MAX310X_WRITE_BIT; |
|---|
| 1367 | + s->p[i].rd_header = (s->p[i].port.iobase + MAX310X_RHR_REG); |
|---|
| 1318 | 1368 | |
|---|
| 1319 | 1369 | /* Register port */ |
|---|
| 1320 | 1370 | ret = uart_add_one_port(&max310x_uart, &s->p[i].port); |
|---|
| .. | .. |
|---|
| 1362 | 1412 | } |
|---|
| 1363 | 1413 | } |
|---|
| 1364 | 1414 | |
|---|
| 1365 | | - mutex_destroy(&s->mutex); |
|---|
| 1366 | | - |
|---|
| 1367 | 1415 | out_clk: |
|---|
| 1368 | 1416 | clk_disable_unprepare(s->clk); |
|---|
| 1369 | 1417 | |
|---|
| .. | .. |
|---|
| 1384 | 1432 | s->devtype->power(&s->p[i].port, 0); |
|---|
| 1385 | 1433 | } |
|---|
| 1386 | 1434 | |
|---|
| 1387 | | - mutex_destroy(&s->mutex); |
|---|
| 1388 | 1435 | clk_disable_unprepare(s->clk); |
|---|
| 1389 | 1436 | |
|---|
| 1390 | 1437 | return 0; |
|---|