forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-02-19 1c055e55a242a33e574e48be530e06770a210dcd
kernel/drivers/tty/serial/atmel_serial.c
....@@ -20,20 +20,18 @@
2020 #include <linux/platform_device.h>
2121 #include <linux/of.h>
2222 #include <linux/of_device.h>
23
-#include <linux/of_gpio.h>
2423 #include <linux/dma-mapping.h>
2524 #include <linux/dmaengine.h>
2625 #include <linux/atmel_pdc.h>
2726 #include <linux/uaccess.h>
2827 #include <linux/platform_data/atmel.h>
2928 #include <linux/timer.h>
30
-#include <linux/gpio.h>
31
-#include <linux/gpio/consumer.h>
3229 #include <linux/err.h>
3330 #include <linux/irq.h>
3431 #include <linux/suspend.h>
3532 #include <linux/mm.h>
3633
34
+#include <asm/div64.h>
3735 #include <asm/io.h>
3836 #include <asm/ioctls.h>
3937
....@@ -49,10 +47,6 @@
4947 */
5048 #define ATMEL_RTS_HIGH_OFFSET 16
5149 #define ATMEL_RTS_LOW_OFFSET 20
52
-
53
-#if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
54
-#define SUPPORT_SYSRQ
55
-#endif
5650
5751 #include <linux/serial_core.h>
5852
....@@ -147,6 +141,8 @@
147141 struct circ_buf rx_ring;
148142
149143 struct mctrl_gpios *gpios;
144
+ u32 backup_mode; /* MR saved during iso7816 operations */
145
+ u32 backup_brgr; /* BRGR saved during iso7816 operations */
150146 unsigned int tx_done_mask;
151147 u32 fifo_size;
152148 u32 rts_high;
....@@ -164,6 +160,10 @@
164160 spinlock_t lock_suspended;
165161
166162 bool hd_start_rx; /* can start RX during half-duplex operation */
163
+
164
+ /* ISO7816 */
165
+ unsigned int fidi_min;
166
+ unsigned int fidi_max;
167167
168168 #ifdef CONFIG_PM
169169 struct {
....@@ -189,14 +189,9 @@
189189 static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
190190 static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
191191
192
-#ifdef SUPPORT_SYSRQ
193
-static struct console atmel_console;
194
-#endif
195
-
196192 #if defined(CONFIG_OF)
197193 static const struct of_device_id atmel_serial_dt_ids[] = {
198
- { .compatible = "atmel,at91rm9200-usart" },
199
- { .compatible = "atmel,at91sam9260-usart" },
194
+ { .compatible = "atmel,at91rm9200-usart-serial" },
200195 { /* sentinel */ }
201196 };
202197 #endif
....@@ -229,8 +224,9 @@
229224
230225 static inline int atmel_uart_is_half_duplex(struct uart_port *port)
231226 {
232
- return (port->rs485.flags & SER_RS485_ENABLED) &&
233
- !(port->rs485.flags & SER_RS485_RX_DURING_TX);
227
+ return ((port->rs485.flags & SER_RS485_ENABLED) &&
228
+ !(port->rs485.flags & SER_RS485_RX_DURING_TX)) ||
229
+ (port->iso7816.flags & SER_ISO7816_ENABLED);
234230 }
235231
236232 #ifdef CONFIG_SERIAL_ATMEL_PDC
....@@ -287,50 +283,6 @@
287283 tasklet_schedule(t);
288284 }
289285
290
-static unsigned int atmel_get_lines_status(struct uart_port *port)
291
-{
292
- struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
293
- unsigned int status, ret = 0;
294
-
295
- status = atmel_uart_readl(port, ATMEL_US_CSR);
296
-
297
- mctrl_gpio_get(atmel_port->gpios, &ret);
298
-
299
- if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
300
- UART_GPIO_CTS))) {
301
- if (ret & TIOCM_CTS)
302
- status &= ~ATMEL_US_CTS;
303
- else
304
- status |= ATMEL_US_CTS;
305
- }
306
-
307
- if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
308
- UART_GPIO_DSR))) {
309
- if (ret & TIOCM_DSR)
310
- status &= ~ATMEL_US_DSR;
311
- else
312
- status |= ATMEL_US_DSR;
313
- }
314
-
315
- if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
316
- UART_GPIO_RI))) {
317
- if (ret & TIOCM_RI)
318
- status &= ~ATMEL_US_RI;
319
- else
320
- status |= ATMEL_US_RI;
321
- }
322
-
323
- if (!IS_ERR_OR_NULL(mctrl_gpio_to_gpiod(atmel_port->gpios,
324
- UART_GPIO_DCD))) {
325
- if (ret & TIOCM_CD)
326
- status &= ~ATMEL_US_DCD;
327
- else
328
- status |= ATMEL_US_DCD;
329
- }
330
-
331
- return status;
332
-}
333
-
334286 /* Enable or disable the rs485 support */
335287 static int atmel_config_rs485(struct uart_port *port,
336288 struct serial_rs485 *rs485conf)
....@@ -343,16 +295,16 @@
343295
344296 mode = atmel_uart_readl(port, ATMEL_US_MR);
345297
346
- /* Resetting serial mode to RS232 (0x0) */
347
- mode &= ~ATMEL_US_USMODE;
348
-
349
- port->rs485 = *rs485conf;
350
-
351298 if (rs485conf->flags & SER_RS485_ENABLED) {
352299 dev_dbg(port->dev, "Setting UART to RS485\n");
353
- atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
300
+ if (rs485conf->flags & SER_RS485_RX_DURING_TX)
301
+ atmel_port->tx_done_mask = ATMEL_US_TXRDY;
302
+ else
303
+ atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
304
+
354305 atmel_uart_writel(port, ATMEL_US_TTGR,
355306 rs485conf->delay_rts_after_send);
307
+ mode &= ~ATMEL_US_USMODE;
356308 mode |= ATMEL_US_USMODE_RS485;
357309 } else {
358310 dev_dbg(port->dev, "Setting UART to RS232\n");
....@@ -368,6 +320,127 @@
368320 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
369321
370322 return 0;
323
+}
324
+
325
+static unsigned int atmel_calc_cd(struct uart_port *port,
326
+ struct serial_iso7816 *iso7816conf)
327
+{
328
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
329
+ unsigned int cd;
330
+ u64 mck_rate;
331
+
332
+ mck_rate = (u64)clk_get_rate(atmel_port->clk);
333
+ do_div(mck_rate, iso7816conf->clk);
334
+ cd = mck_rate;
335
+ return cd;
336
+}
337
+
338
+static unsigned int atmel_calc_fidi(struct uart_port *port,
339
+ struct serial_iso7816 *iso7816conf)
340
+{
341
+ u64 fidi = 0;
342
+
343
+ if (iso7816conf->sc_fi && iso7816conf->sc_di) {
344
+ fidi = (u64)iso7816conf->sc_fi;
345
+ do_div(fidi, iso7816conf->sc_di);
346
+ }
347
+ return (u32)fidi;
348
+}
349
+
350
+/* Enable or disable the iso7816 support */
351
+/* Called with interrupts disabled */
352
+static int atmel_config_iso7816(struct uart_port *port,
353
+ struct serial_iso7816 *iso7816conf)
354
+{
355
+ struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
356
+ unsigned int mode;
357
+ unsigned int cd, fidi;
358
+ int ret = 0;
359
+
360
+ /* Disable interrupts */
361
+ atmel_uart_writel(port, ATMEL_US_IDR, atmel_port->tx_done_mask);
362
+
363
+ mode = atmel_uart_readl(port, ATMEL_US_MR);
364
+
365
+ if (iso7816conf->flags & SER_ISO7816_ENABLED) {
366
+ mode &= ~ATMEL_US_USMODE;
367
+
368
+ if (iso7816conf->tg > 255) {
369
+ dev_err(port->dev, "ISO7816: Timeguard exceeding 255\n");
370
+ memset(iso7816conf, 0, sizeof(struct serial_iso7816));
371
+ ret = -EINVAL;
372
+ goto err_out;
373
+ }
374
+
375
+ if ((iso7816conf->flags & SER_ISO7816_T_PARAM)
376
+ == SER_ISO7816_T(0)) {
377
+ mode |= ATMEL_US_USMODE_ISO7816_T0 | ATMEL_US_DSNACK;
378
+ } else if ((iso7816conf->flags & SER_ISO7816_T_PARAM)
379
+ == SER_ISO7816_T(1)) {
380
+ mode |= ATMEL_US_USMODE_ISO7816_T1 | ATMEL_US_INACK;
381
+ } else {
382
+ dev_err(port->dev, "ISO7816: Type not supported\n");
383
+ memset(iso7816conf, 0, sizeof(struct serial_iso7816));
384
+ ret = -EINVAL;
385
+ goto err_out;
386
+ }
387
+
388
+ mode &= ~(ATMEL_US_USCLKS | ATMEL_US_NBSTOP | ATMEL_US_PAR);
389
+
390
+ /* select mck clock, and output */
391
+ mode |= ATMEL_US_USCLKS_MCK | ATMEL_US_CLKO;
392
+ /* set parity for normal/inverse mode + max iterations */
393
+ mode |= ATMEL_US_PAR_EVEN | ATMEL_US_NBSTOP_1 | ATMEL_US_MAX_ITER(3);
394
+
395
+ cd = atmel_calc_cd(port, iso7816conf);
396
+ fidi = atmel_calc_fidi(port, iso7816conf);
397
+ if (fidi == 0) {
398
+ dev_warn(port->dev, "ISO7816 fidi = 0, Generator generates no signal\n");
399
+ } else if (fidi < atmel_port->fidi_min
400
+ || fidi > atmel_port->fidi_max) {
401
+ dev_err(port->dev, "ISO7816 fidi = %u, value not supported\n", fidi);
402
+ memset(iso7816conf, 0, sizeof(struct serial_iso7816));
403
+ ret = -EINVAL;
404
+ goto err_out;
405
+ }
406
+
407
+ if (!(port->iso7816.flags & SER_ISO7816_ENABLED)) {
408
+ /* port not yet in iso7816 mode: store configuration */
409
+ atmel_port->backup_mode = atmel_uart_readl(port, ATMEL_US_MR);
410
+ atmel_port->backup_brgr = atmel_uart_readl(port, ATMEL_US_BRGR);
411
+ }
412
+
413
+ atmel_uart_writel(port, ATMEL_US_TTGR, iso7816conf->tg);
414
+ atmel_uart_writel(port, ATMEL_US_BRGR, cd);
415
+ atmel_uart_writel(port, ATMEL_US_FIDI, fidi);
416
+
417
+ atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXDIS | ATMEL_US_RXEN);
418
+ atmel_port->tx_done_mask = ATMEL_US_TXEMPTY | ATMEL_US_NACK | ATMEL_US_ITERATION;
419
+ } else {
420
+ dev_dbg(port->dev, "Setting UART back to RS232\n");
421
+ /* back to last RS232 settings */
422
+ mode = atmel_port->backup_mode;
423
+ memset(iso7816conf, 0, sizeof(struct serial_iso7816));
424
+ atmel_uart_writel(port, ATMEL_US_TTGR, 0);
425
+ atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->backup_brgr);
426
+ atmel_uart_writel(port, ATMEL_US_FIDI, 0x174);
427
+
428
+ if (atmel_use_pdc_tx(port))
429
+ atmel_port->tx_done_mask = ATMEL_US_ENDTX |
430
+ ATMEL_US_TXBUFE;
431
+ else
432
+ atmel_port->tx_done_mask = ATMEL_US_TXRDY;
433
+ }
434
+
435
+ port->iso7816 = *iso7816conf;
436
+
437
+ atmel_uart_writel(port, ATMEL_US_MR, mode);
438
+
439
+err_out:
440
+ /* Enable interrupts */
441
+ atmel_uart_writel(port, ATMEL_US_IER, atmel_port->tx_done_mask);
442
+
443
+ return ret;
371444 }
372445
373446 /*
....@@ -748,7 +821,7 @@
748821 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
749822
750823 if (port->x_char &&
751
- (atmel_uart_readl(port, ATMEL_US_CSR) & atmel_port->tx_done_mask)) {
824
+ (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY)) {
752825 atmel_uart_write_char(port, port->x_char);
753826 port->icount.tx++;
754827 port->x_char = 0;
....@@ -756,8 +829,7 @@
756829 if (uart_circ_empty(xmit) || uart_tx_stopped(port))
757830 return;
758831
759
- while (atmel_uart_readl(port, ATMEL_US_CSR) &
760
- atmel_port->tx_done_mask) {
832
+ while (atmel_uart_readl(port, ATMEL_US_CSR) & ATMEL_US_TXRDY) {
761833 atmel_uart_write_char(port, xmit->buf[xmit->tail]);
762834 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
763835 port->icount.tx++;
....@@ -768,10 +840,20 @@
768840 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
769841 uart_write_wakeup(port);
770842
771
- if (!uart_circ_empty(xmit))
843
+ if (!uart_circ_empty(xmit)) {
844
+ /* we still have characters to transmit, so we should continue
845
+ * transmitting them when TX is ready, regardless of
846
+ * mode or duplexity
847
+ */
848
+ atmel_port->tx_done_mask |= ATMEL_US_TXRDY;
849
+
772850 /* Enable interrupts */
773851 atmel_uart_writel(port, ATMEL_US_IER,
774852 atmel_port->tx_done_mask);
853
+ } else {
854
+ if (atmel_uart_is_half_duplex(port))
855
+ atmel_port->tx_done_mask &= ~ATMEL_US_TXRDY;
856
+ }
775857 }
776858
777859 static void atmel_complete_tx_dma(void *arg)
....@@ -791,11 +873,11 @@
791873
792874 port->icount.tx += atmel_port->tx_len;
793875
794
- spin_lock_irq(&atmel_port->lock_tx);
876
+ spin_lock(&atmel_port->lock_tx);
795877 async_tx_ack(atmel_port->desc_tx);
796878 atmel_port->cookie_tx = -EINVAL;
797879 atmel_port->desc_tx = NULL;
798
- spin_unlock_irq(&atmel_port->lock_tx);
880
+ spin_unlock(&atmel_port->lock_tx);
799881
800882 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
801883 uart_write_wakeup(port);
....@@ -934,6 +1016,7 @@
9341016 static int atmel_prepare_tx_dma(struct uart_port *port)
9351017 {
9361018 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1019
+ struct device *mfd_dev = port->dev->parent;
9371020 dma_cap_mask_t mask;
9381021 struct dma_slave_config config;
9391022 int ret, nent;
....@@ -941,7 +1024,7 @@
9411024 dma_cap_zero(mask);
9421025 dma_cap_set(DMA_SLAVE, mask);
9431026
944
- atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
1027
+ atmel_port->chan_tx = dma_request_slave_channel(mfd_dev, "tx");
9451028 if (atmel_port->chan_tx == NULL)
9461029 goto chan_err;
9471030 dev_info(port->dev, "using %s for tx DMA transfers\n",
....@@ -990,7 +1073,7 @@
9901073
9911074 chan_err:
9921075 dev_err(port->dev, "TX channel not available, switch to pio\n");
993
- atmel_port->use_dma_tx = 0;
1076
+ atmel_port->use_dma_tx = false;
9941077 if (atmel_port->chan_tx)
9951078 atmel_release_tx_dma(port);
9961079 return -EINVAL;
....@@ -1112,6 +1195,7 @@
11121195 static int atmel_prepare_rx_dma(struct uart_port *port)
11131196 {
11141197 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1198
+ struct device *mfd_dev = port->dev->parent;
11151199 struct dma_async_tx_descriptor *desc;
11161200 dma_cap_mask_t mask;
11171201 struct dma_slave_config config;
....@@ -1123,7 +1207,7 @@
11231207 dma_cap_zero(mask);
11241208 dma_cap_set(DMA_CYCLIC, mask);
11251209
1126
- atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
1210
+ atmel_port->chan_rx = dma_request_slave_channel(mfd_dev, "rx");
11271211 if (atmel_port->chan_rx == NULL)
11281212 goto chan_err;
11291213 dev_info(port->dev, "using %s for rx DMA transfers\n",
....@@ -1195,7 +1279,7 @@
11951279
11961280 chan_err:
11971281 dev_err(port->dev, "RX channel not available, switch to pio\n");
1198
- atmel_port->use_dma_rx = 0;
1282
+ atmel_port->use_dma_rx = false;
11991283 if (atmel_port->chan_rx)
12001284 atmel_release_rx_dma(port);
12011285 return -EINVAL;
....@@ -1321,6 +1405,9 @@
13211405 wake_up_interruptible(&port->state->port.delta_msr_wait);
13221406 }
13231407 }
1408
+
1409
+ if (pending & (ATMEL_US_NACK | ATMEL_US_ITERATION))
1410
+ dev_dbg(port->dev, "ISO7816 ERROR (0x%08x)\n", pending);
13241411 }
13251412
13261413 /*
....@@ -1335,7 +1422,7 @@
13351422 spin_lock(&atmel_port->lock_suspended);
13361423
13371424 do {
1338
- status = atmel_get_lines_status(port);
1425
+ status = atmel_uart_readl(port, ATMEL_US_CSR);
13391426 mask = atmel_uart_readl(port, ATMEL_US_IMR);
13401427 pending = status & mask;
13411428 if (!pending)
....@@ -1619,7 +1706,7 @@
16191706 DMA_FROM_DEVICE);
16201707 kfree(atmel_port->pdc_rx[0].buf);
16211708 }
1622
- atmel_port->use_pdc_rx = 0;
1709
+ atmel_port->use_pdc_rx = false;
16231710 return -ENOMEM;
16241711 }
16251712 pdc->dma_addr = dma_map_single(port->dev,
....@@ -1645,10 +1732,11 @@
16451732 /*
16461733 * tasklet handling tty stuff outside the interrupt handler.
16471734 */
1648
-static void atmel_tasklet_rx_func(unsigned long data)
1735
+static void atmel_tasklet_rx_func(struct tasklet_struct *t)
16491736 {
1650
- struct uart_port *port = (struct uart_port *)data;
1651
- struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1737
+ struct atmel_uart_port *atmel_port = from_tasklet(atmel_port, t,
1738
+ tasklet_rx);
1739
+ struct uart_port *port = &atmel_port->uart;
16521740
16531741 /* The interrupt handler does not take the lock */
16541742 spin_lock(&port->lock);
....@@ -1656,10 +1744,11 @@
16561744 spin_unlock(&port->lock);
16571745 }
16581746
1659
-static void atmel_tasklet_tx_func(unsigned long data)
1747
+static void atmel_tasklet_tx_func(struct tasklet_struct *t)
16601748 {
1661
- struct uart_port *port = (struct uart_port *)data;
1662
- struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
1749
+ struct atmel_uart_port *atmel_port = from_tasklet(atmel_port, t,
1750
+ tasklet_tx);
1751
+ struct uart_port *port = &atmel_port->uart;
16631752
16641753 /* The interrupt handler does not take the lock */
16651754 spin_lock(&port->lock);
....@@ -1765,6 +1854,22 @@
17651854 atmel_port->has_frac_baudrate = true;
17661855 atmel_port->has_hw_timer = true;
17671856 atmel_port->rtor = ATMEL_US_RTOR;
1857
+ version = atmel_uart_readl(port, ATMEL_US_VERSION);
1858
+ switch (version) {
1859
+ case 0x814: /* sama5d2 */
1860
+ fallthrough;
1861
+ case 0x701: /* sama5d4 */
1862
+ atmel_port->fidi_min = 3;
1863
+ atmel_port->fidi_max = 65535;
1864
+ break;
1865
+ case 0x502: /* sam9x5, sama5d3 */
1866
+ atmel_port->fidi_min = 3;
1867
+ atmel_port->fidi_max = 2047;
1868
+ break;
1869
+ default:
1870
+ atmel_port->fidi_min = 1;
1871
+ atmel_port->fidi_max = 2047;
1872
+ }
17681873 } else if (name == dbgu_uart) {
17691874 dev_dbg(port->dev, "Dbgu or uart without hw timer\n");
17701875 } else {
....@@ -1818,10 +1923,8 @@
18181923 }
18191924
18201925 atomic_set(&atmel_port->tasklet_shutdown, 0);
1821
- tasklet_init(&atmel_port->tasklet_rx, atmel_tasklet_rx_func,
1822
- (unsigned long)port);
1823
- tasklet_init(&atmel_port->tasklet_tx, atmel_tasklet_tx_func,
1824
- (unsigned long)port);
1926
+ tasklet_setup(&atmel_port->tasklet_rx, atmel_tasklet_rx_func);
1927
+ tasklet_setup(&atmel_port->tasklet_tx, atmel_tasklet_tx_func);
18251928
18261929 /*
18271930 * Initialize DMA (if necessary)
....@@ -1868,7 +1971,7 @@
18681971 }
18691972
18701973 /* Save current CSR for comparison in atmel_tasklet_func() */
1871
- atmel_port->irq_status_prev = atmel_get_lines_status(port);
1974
+ atmel_port->irq_status_prev = atmel_uart_readl(port, ATMEL_US_CSR);
18721975
18731976 /*
18741977 * Finally, enable the serial port
....@@ -2138,6 +2241,17 @@
21382241 atmel_uart_writel(port, ATMEL_US_TTGR,
21392242 port->rs485.delay_rts_after_send);
21402243 mode |= ATMEL_US_USMODE_RS485;
2244
+ } else if (port->iso7816.flags & SER_ISO7816_ENABLED) {
2245
+ atmel_uart_writel(port, ATMEL_US_TTGR, port->iso7816.tg);
2246
+ /* select mck clock, and output */
2247
+ mode |= ATMEL_US_USCLKS_MCK | ATMEL_US_CLKO;
2248
+ /* set max iterations */
2249
+ mode |= ATMEL_US_MAX_ITER(3);
2250
+ if ((port->iso7816.flags & SER_ISO7816_T_PARAM)
2251
+ == SER_ISO7816_T(0))
2252
+ mode |= ATMEL_US_USMODE_ISO7816_T0;
2253
+ else
2254
+ mode |= ATMEL_US_USMODE_ISO7816_T1;
21412255 } else if (termios->c_cflag & CRTSCTS) {
21422256 /* RS232 with hardware handshake (RTS/CTS) */
21432257 if (atmel_use_fifo(port) &&
....@@ -2193,7 +2307,8 @@
21932307 }
21942308 quot = cd | fp << ATMEL_US_FP_OFFSET;
21952309
2196
- atmel_uart_writel(port, ATMEL_US_BRGR, quot);
2310
+ if (!(port->iso7816.flags & SER_ISO7816_ENABLED))
2311
+ atmel_uart_writel(port, ATMEL_US_BRGR, quot);
21972312
21982313 /* set the mode, clock divisor, parity, stop bits and data size */
21992314 atmel_uart_writel(port, ATMEL_US_MR, mode);
....@@ -2262,8 +2377,8 @@
22622377 */
22632378 static void atmel_release_port(struct uart_port *port)
22642379 {
2265
- struct platform_device *pdev = to_platform_device(port->dev);
2266
- int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2380
+ struct platform_device *mpdev = to_platform_device(port->dev->parent);
2381
+ int size = resource_size(mpdev->resource);
22672382
22682383 release_mem_region(port->mapbase, size);
22692384
....@@ -2278,8 +2393,8 @@
22782393 */
22792394 static int atmel_request_port(struct uart_port *port)
22802395 {
2281
- struct platform_device *pdev = to_platform_device(port->dev);
2282
- int size = pdev->resource[0].end - pdev->resource[0].start + 1;
2396
+ struct platform_device *mpdev = to_platform_device(port->dev->parent);
2397
+ int size = resource_size(mpdev->resource);
22832398
22842399 if (!request_mem_region(port->mapbase, size, "atmel_serial"))
22852400 return -EBUSY;
....@@ -2381,27 +2496,31 @@
23812496 {
23822497 int ret;
23832498 struct uart_port *port = &atmel_port->uart;
2499
+ struct platform_device *mpdev = to_platform_device(pdev->dev.parent);
23842500
23852501 atmel_init_property(atmel_port, pdev);
23862502 atmel_set_ops(port);
2387
-
2388
- uart_get_rs485_mode(&pdev->dev, &port->rs485);
23892503
23902504 port->iotype = UPIO_MEM;
23912505 port->flags = UPF_BOOT_AUTOCONF | UPF_IOREMAP;
23922506 port->ops = &atmel_pops;
23932507 port->fifosize = 1;
23942508 port->dev = &pdev->dev;
2395
- port->mapbase = pdev->resource[0].start;
2396
- port->irq = pdev->resource[1].start;
2509
+ port->mapbase = mpdev->resource[0].start;
2510
+ port->irq = mpdev->resource[1].start;
23972511 port->rs485_config = atmel_config_rs485;
2398
- port->membase = NULL;
2512
+ port->iso7816_config = atmel_config_iso7816;
2513
+ port->membase = NULL;
23992514
24002515 memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
24012516
2517
+ ret = uart_get_rs485_mode(port);
2518
+ if (ret)
2519
+ return ret;
2520
+
24022521 /* for console, the clock could already be configured */
24032522 if (!atmel_port->clk) {
2404
- atmel_port->clk = clk_get(&pdev->dev, "usart");
2523
+ atmel_port->clk = clk_get(&mpdev->dev, "usart");
24052524 if (IS_ERR(atmel_port->clk)) {
24062525 ret = PTR_ERR(atmel_port->clk);
24072526 atmel_port->clk = NULL;
....@@ -2418,8 +2537,11 @@
24182537 /* only enable clock when USART is in use */
24192538 }
24202539
2421
- /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
2422
- if (port->rs485.flags & SER_RS485_ENABLED)
2540
+ /*
2541
+ * Use TXEMPTY for interrupt when rs485 or ISO7816 else TXRDY or
2542
+ * ENDTX|TXBUFE
2543
+ */
2544
+ if (atmel_uart_is_half_duplex(port))
24232545 atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
24242546 else if (atmel_use_pdc_tx(port)) {
24252547 port->fifosize = PDC_BUFFER_SIZE;
....@@ -2511,13 +2633,7 @@
25112633 else if (mr == ATMEL_US_PAR_ODD)
25122634 *parity = 'o';
25132635
2514
- /*
2515
- * The serial core only rounds down when matching this to a
2516
- * supported baud rate. Make sure we don't end up slightly
2517
- * lower than one of those, as it would make us fall through
2518
- * to a much lower baud rate than we really want.
2519
- */
2520
- *baud = port->uartclk / (16 * (quot - 1));
2636
+ *baud = port->uartclk / (16 * quot);
25212637 }
25222638
25232639 static int __init atmel_console_setup(struct console *co, char *options)
....@@ -2566,18 +2682,8 @@
25662682
25672683 #define ATMEL_CONSOLE_DEVICE (&atmel_console)
25682684
2569
-static inline bool atmel_is_console_port(struct uart_port *port)
2570
-{
2571
- return port->cons && port->cons->index == port->line;
2572
-}
2573
-
25742685 #else
25752686 #define ATMEL_CONSOLE_DEVICE NULL
2576
-
2577
-static inline bool atmel_is_console_port(struct uart_port *port)
2578
-{
2579
- return false;
2580
-}
25812687 #endif
25822688
25832689 static struct uart_driver atmel_uart = {
....@@ -2606,14 +2712,14 @@
26062712 struct uart_port *port = platform_get_drvdata(pdev);
26072713 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
26082714
2609
- if (atmel_is_console_port(port) && console_suspend_enabled) {
2715
+ if (uart_console(port) && console_suspend_enabled) {
26102716 /* Drain the TX shifter */
26112717 while (!(atmel_uart_readl(port, ATMEL_US_CSR) &
26122718 ATMEL_US_TXEMPTY))
26132719 cpu_relax();
26142720 }
26152721
2616
- if (atmel_is_console_port(port) && !console_suspend_enabled) {
2722
+ if (uart_console(port) && !console_suspend_enabled) {
26172723 /* Cache register values as we won't get a full shutdown/startup
26182724 * cycle
26192725 */
....@@ -2649,7 +2755,7 @@
26492755 struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
26502756 unsigned long flags;
26512757
2652
- if (atmel_is_console_port(port) && !console_suspend_enabled) {
2758
+ if (uart_console(port) && !console_suspend_enabled) {
26532759 atmel_uart_writel(port, ATMEL_US_MR, atmel_port->cache.mr);
26542760 atmel_uart_writel(port, ATMEL_US_IER, atmel_port->cache.imr);
26552761 atmel_uart_writel(port, ATMEL_US_BRGR, atmel_port->cache.brgr);
....@@ -2734,12 +2840,21 @@
27342840 static int atmel_serial_probe(struct platform_device *pdev)
27352841 {
27362842 struct atmel_uart_port *atmel_port;
2737
- struct device_node *np = pdev->dev.of_node;
2843
+ struct device_node *np = pdev->dev.parent->of_node;
27382844 void *data;
2739
- int ret = -ENODEV;
2845
+ int ret;
27402846 bool rs485_enabled;
27412847
27422848 BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
2849
+
2850
+ /*
2851
+ * In device tree there is no node with "atmel,at91rm9200-usart-serial"
2852
+ * as compatible string. This driver is probed by at91-usart mfd driver
2853
+ * which is just a wrapper over the atmel_serial driver and
2854
+ * spi-at91-usart driver. All attributes needed by this driver are
2855
+ * found in of_node of parent.
2856
+ */
2857
+ pdev->dev.of_node = np;
27432858
27442859 ret = of_alias_get_id(np, "serial");
27452860 if (ret < 0)
....@@ -2761,6 +2876,7 @@
27612876 atmel_port = &atmel_ports[ret];
27622877 atmel_port->backup_imr = 0;
27632878 atmel_port->uart.line = ret;
2879
+ atmel_port->uart.has_sysrq = IS_ENABLED(CONFIG_SERIAL_ATMEL_CONSOLE);
27642880 atmel_serial_probe_fifos(atmel_port, pdev);
27652881
27662882 atomic_set(&atmel_port->tasklet_shutdown, 0);
....@@ -2793,7 +2909,7 @@
27932909 goto err_add_port;
27942910
27952911 #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
2796
- if (atmel_is_console_port(&atmel_port->uart)
2912
+ if (uart_console(&atmel_port->uart)
27972913 && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
27982914 /*
27992915 * The serial core enabled the clock for us, so undo
....@@ -2836,7 +2952,7 @@
28362952 kfree(atmel_port->rx_ring.buf);
28372953 atmel_port->rx_ring.buf = NULL;
28382954 err_alloc_ring:
2839
- if (!atmel_is_console_port(&atmel_port->uart)) {
2955
+ if (!uart_console(&atmel_port->uart)) {
28402956 clk_put(atmel_port->clk);
28412957 atmel_port->clk = NULL;
28422958 }
....@@ -2876,6 +2992,7 @@
28762992
28772993 clk_put(atmel_port->clk);
28782994 atmel_port->clk = NULL;
2995
+ pdev->dev.of_node = NULL;
28792996
28802997 return ret;
28812998 }
....@@ -2886,7 +3003,7 @@
28863003 .suspend = atmel_serial_suspend,
28873004 .resume = atmel_serial_resume,
28883005 .driver = {
2889
- .name = "atmel_usart",
3006
+ .name = "atmel_usart_serial",
28903007 .of_match_table = of_match_ptr(atmel_serial_dt_ids),
28913008 },
28923009 };