Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
/*
 * Copyright (c) 2023 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @brief UART driver for Intel FPGA UART Core IP
 * Reference : Embedded Peripherals IP User Guide : 11. UART Core
 * Limitations:
 * 1. User should consider to always use polling mode, as IP core does not have fifo.
 *    So IP can only send/receive 1 character at a time.
 * 2. CTS and RTS is purely software controlled. Assertion might not be on time.
 * 3. Full duplex mode is not supported.
 */

#define DT_DRV_COMPAT   altr_uart

#include <zephyr/kernel.h>
#include <zephyr/drivers/uart.h>

#include <zephyr/drivers/serial/uart_altera.h>

#ifdef CONFIG_UART_LINE_CTRL
	#ifndef CONFIG_UART_INTERRUPT_DRIVEN
		/* CTS and RTS is purely software controlled. */
		#error "uart_altera.c: Must enable UART_INTERRUPT_DRIVEN for line control!"
	#endif
#endif

/* register offsets */
#define ALTERA_AVALON_UART_OFFSET  (0x4)

#define ALTERA_AVALON_UART_RXDATA_REG_OFFSET    (0 * ALTERA_AVALON_UART_OFFSET)
#define ALTERA_AVALON_UART_TXDATA_REG_OFFSET    (1 * ALTERA_AVALON_UART_OFFSET)
#define ALTERA_AVALON_UART_STATUS_REG_OFFSET    (2 * ALTERA_AVALON_UART_OFFSET)
#define ALTERA_AVALON_UART_CONTROL_REG_OFFSET   (3 * ALTERA_AVALON_UART_OFFSET)
#define ALTERA_AVALON_UART_DIVISOR_REG_OFFSET   (4 * ALTERA_AVALON_UART_OFFSET)
#define ALTERA_AVALON_UART_EOP_REG_OFFSET       (5 * ALTERA_AVALON_UART_OFFSET)

/*status register mask */
#define ALTERA_AVALON_UART_STATUS_PE_MSK              (0x1)
#define ALTERA_AVALON_UART_STATUS_FE_MSK              (0x2)
#define ALTERA_AVALON_UART_STATUS_BRK_MSK             (0x4)
#define ALTERA_AVALON_UART_STATUS_ROE_MSK             (0x8)
#define ALTERA_AVALON_UART_STATUS_TMT_MSK             (0x20)
#define ALTERA_AVALON_UART_STATUS_TRDY_MSK            (0x40)
#define ALTERA_AVALON_UART_STATUS_RRDY_MSK            (0x80)
#define ALTERA_AVALON_UART_STATUS_DCTS_MSK            (0x400)
#define ALTERA_AVALON_UART_STATUS_CTS_MSK             (0x800)
#define ALTERA_AVALON_UART_STATUS_E_MSK               (0x100)
#define ALTERA_AVALON_UART_STATUS_EOP_MSK             (0x1000)

/* control register mask */
#define ALTERA_AVALON_UART_CONTROL_TMT_MSK             (0x20)
#define ALTERA_AVALON_UART_CONTROL_TRDY_MSK            (0x40)
#define ALTERA_AVALON_UART_CONTROL_RRDY_MSK            (0x80)
#define ALTERA_AVALON_UART_CONTROL_E_MSK               (0x100)
#define ALTERA_AVALON_UART_CONTROL_DCTS_MSK            (0x400)
#define ALTERA_AVALON_UART_CONTROL_RTS_MSK             (0x800)
#define ALTERA_AVALON_UART_CONTROL_EOP_MSK            (0x1000)

/* defined values */
#define UART_ALTERA_NO_ERROR (0u)
#define ALTERA_AVALON_UART_CLEAR_STATUS_VAL (0u)
#define ALTERA_AVALON_UART_PENDING_MASK  (ALTERA_AVALON_UART_STATUS_RRDY_MSK | \
			  ALTERA_AVALON_UART_STATUS_TRDY_MSK | ALTERA_AVALON_UART_STATUS_E_MSK | \
			  ALTERA_AVALON_UART_STATUS_EOP_MSK)

/***********************/
/* configuration flags */
/*
 * The value ALT_AVALON_UART_FB is a value set in the devices flag field to
 * indicate that the device has a fixed baud rate; i.e. if this flag is set
 * software can not control the baud rate of the device.
 */
#define ALT_AVALON_UART_FB 0x1

/*
 * The value ALT_AVALON_UART_FC is a value set in the device flag field to
 * indicate the device is using flow control, i.e. the driver must
 * throttle on transmit if the nCTS pin is low.
 */
#define ALT_AVALON_UART_FC 0x2

/* end of configuration flags */
/******************************/

/* device data */
struct uart_altera_device_data {
	struct uart_config uart_cfg; /* stores uart config from device tree*/
	struct k_spinlock lock;
	uint32_t status_act; /* stores value of status register. */
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	uart_irq_callback_user_data_t cb;  /**< Callback function pointer */
	void *cb_data;  /**< Callback function arg */
#ifdef CONFIG_UART_ALTERA_EOP
	uint8_t set_eop_cb;
	uart_irq_callback_user_data_t cb_eop;  /**< Callback function pointer */
	void *cb_data_eop;  /**< Callback function arg */
#endif /* CONFIG_UART_ALTERA_EOP */
#ifdef CONFIG_UART_ALTERA_LINE_CTRL_WORKAROUND
	uint8_t dcts_rising;
#endif /*CONFIG_UART_ALTERA_LINE_CTRL_WORKAROUND*/
	uint32_t control_val; /* stores value to set control register. */
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
};

/*
 * device config:
 * stores data that cannot be changed during run time.
 */
struct uart_altera_device_config {
	mm_reg_t base;
	uint32_t flags;              /* refer to configuration flags */
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	uart_irq_config_func_t  irq_config_func;
	unsigned int irq_num;
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
};

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
/**
 * function prototypes
 */
static int uart_altera_irq_update(const struct device *dev);
static int uart_altera_irq_tx_ready(const struct device *dev);
static int uart_altera_irq_rx_ready(const struct device *dev);
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */

/**
 * @brief Poll the device for input.
 *
 * This is a non-blocking function.
 *
 * @param dev UART device instance
 * @param p_char Pointer to character
 *
 * @return 0 if a character arrived, -1 if input buffer is empty.
 * -EINVAL if p_char is null pointer
 */
static int uart_altera_poll_in(const struct device *dev, unsigned char *p_char)
{
	const struct uart_altera_device_config *config = dev->config;
	struct uart_altera_device_data *data = dev->data;
	int ret_val = -1;
	uint32_t status;

	/* generate fatal error if CONFIG_ASSERT is enabled. */
	__ASSERT(p_char != NULL, "p_char is null pointer!");

	/* Stop, if p_char is null pointer */
	if (p_char == NULL) {
		return -EINVAL;
	}

	k_spinlock_key_t key = k_spin_lock(&data->lock);

	/* check if received character is ready.*/
	status = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET);
	if (status & ALTERA_AVALON_UART_STATUS_RRDY_MSK) {
		/* got a character */
		*p_char = sys_read32(config->base + ALTERA_AVALON_UART_RXDATA_REG_OFFSET);
		ret_val = 0;
	}

	k_spin_unlock(&data->lock, key);

	return ret_val;
}

/**
 * @brief Output a character in polled mode.
 *
 * This function will block until transmitter is ready.
 * Then, a character will be transmitted.
 *
 * @param dev UART device instance
 * @param c Character to send
 */
static void uart_altera_poll_out(const struct device *dev, unsigned char c)
{
	const struct uart_altera_device_config *config = dev->config;
	struct uart_altera_device_data *data = dev->data;
	uint32_t status;

	k_spinlock_key_t key = k_spin_lock(&data->lock);

	do {
		/* wait until uart is free to transmit.*/
		status = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET);
	} while ((status & ALTERA_AVALON_UART_STATUS_TRDY_MSK) == 0);

	sys_write32(c, config->base + ALTERA_AVALON_UART_TXDATA_REG_OFFSET);

	k_spin_unlock(&data->lock, key);
}

/**
 * @brief Initialise an instance of the driver
 *
 * This function initialise the interrupt configuration for the driver.
 *
 * @param dev UART device instance
 *
 * @return 0 to indicate success.
 */
static int uart_altera_init(const struct device *dev)
{
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	struct uart_altera_device_data *data = dev->data;
	const struct uart_altera_device_config *config = dev->config;
	/* clear status to ensure, that interrupts are not triggered due to old status. */
	sys_write32(ALTERA_AVALON_UART_CLEAR_STATUS_VAL, config->base
				+ ALTERA_AVALON_UART_STATUS_REG_OFFSET);

	/*
	 * Enable hardware interrupt.
	 * The corresponding csr from IP still needs to be set,
	 * so that the IP generates interrupt signal.
	 */
	config->irq_config_func(dev);

#ifdef CONFIG_UART_LINE_CTRL
	/* Enable DCTS interrupt. */
	data->control_val = ALTERA_AVALON_UART_CONTROL_DCTS_MSK;
#endif /* CONFIG_UART_LINE_CTRL */

	sys_write32(data->control_val, config->base + ALTERA_AVALON_UART_CONTROL_REG_OFFSET);

#endif /* CONFIG_UART_INTERRUPT_DRIVEN */

	return 0;
}

/**
 * @brief Check if an error was received
 * If error is received, it will be mapped to uart_rx_stop_reason.
 * This function should be called after irq_update.
 * If interrupt driven API is not enabled,
 * this function will read and clear the status register.
 *
 * @param dev UART device struct
 *
 * @return UART_ERROR_OVERRUN, UART_ERROR_PARITY, UART_ERROR_FRAMING,
 * UART_BREAK if an error was detected, 0 otherwise.
 */
static int uart_altera_err_check(const struct device *dev)
{
	struct uart_altera_device_data *data = dev->data;
	int err = UART_ALTERA_NO_ERROR;

#ifndef CONFIG_UART_INTERRUPT_DRIVEN
	const struct uart_altera_device_config *config = dev->config;
	k_spinlock_key_t key = k_spin_lock(&data->lock);

	data->status_act = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET);
#endif

	if (data->status_act & ALTERA_AVALON_UART_STATUS_E_MSK) {
		if (data->status_act & ALTERA_AVALON_UART_STATUS_PE_MSK) {
			err |= UART_ERROR_PARITY;
		}

		if (data->status_act & ALTERA_AVALON_UART_STATUS_FE_MSK) {
			err |= UART_ERROR_FRAMING;
		}

		if (data->status_act & ALTERA_AVALON_UART_STATUS_BRK_MSK) {
			err |= UART_BREAK;
		}

		if (data->status_act & ALTERA_AVALON_UART_STATUS_ROE_MSK) {
			err |= UART_ERROR_OVERRUN;
		}
	}

#ifndef CONFIG_UART_INTERRUPT_DRIVEN
	/* clear status */
	sys_write32(ALTERA_AVALON_UART_CLEAR_STATUS_VAL, config->base
	+ ALTERA_AVALON_UART_STATUS_REG_OFFSET);
	k_spin_unlock(&data->lock, key);
#endif

	return err;
}

#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
/***
 * @brief helper function to check, if the configuration is support.
 * @param  cfg_stored : The original configuration.
 * @param  cfg_in     : The input configuration.
 * @return true if only baudrate is changed. otherwise false.
 */
static bool uart_altera_check_configuration(const struct uart_config *cfg_stored,
				   const struct uart_config *cfg_in)
{
	bool ret_val = false;

	if ((cfg_stored->parity       == cfg_in->parity)
		&& (cfg_stored->stop_bits == cfg_in->stop_bits)
		&& (cfg_stored->data_bits == cfg_in->data_bits)
		&& (cfg_stored->flow_ctrl == cfg_in->flow_ctrl)) {
		ret_val = true;
	}

	return ret_val;
}

/**
 * @brief Set UART configuration using data from *cfg_in.
 *
 * @param dev UART : Device struct
 * @param cfg_in   : The input configuration.
 *
 * @return 0 if success, -ENOTSUP, if input from cfg_in is not configurable.
 * -EINVAL if cfg_in is null pointer
 */
static int uart_altera_configure(const struct device *dev,
				   const struct uart_config *cfg_in)
{
	const struct uart_altera_device_config *config = dev->config;
	struct uart_altera_device_data * const data = dev->data;
	struct uart_config * const cfg_stored = &data->uart_cfg;
	uint32_t divisor_val;
	int ret_val;

	/* generate fatal error if CONFIG_ASSERT is enabled. */
	__ASSERT(cfg_in != NULL, "cfg_in is null pointer!");

	/* Stop, if cfg_in is null pointer */
	if (cfg_in == NULL) {
		return -EINVAL;
	}

	/* check if configuration is supported. */
	if (uart_altera_check_configuration(cfg_stored, cfg_in)
		&& !(config->flags & ALT_AVALON_UART_FB)) {
		/* calculate and set baudrate. */
		divisor_val = (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC/cfg_in->baudrate) - 1;
		sys_write32(divisor_val, config->base + ALTERA_AVALON_UART_DIVISOR_REG_OFFSET);

		/* update stored data. */
		cfg_stored->baudrate = cfg_in->baudrate;
		ret_val = 0;
	} else {
		/* return not supported */
		ret_val = -ENOTSUP;
	}

	return ret_val;
}

/**
 * @brief Get UART configuration and stores in *cfg_out.
 *
 * @param dev UART : Device struct
 * @param cfg_out   : The output configuration.
 *
 * @return 0 if success.
 * -EINVAL if cfg_out is null pointer
 */
static int uart_altera_config_get(const struct device *dev,
				struct uart_config *cfg_out)
{
	const struct uart_altera_device_data *data = dev->data;

	/* generate fatal error if CONFIG_ASSERT is enabled. */
	__ASSERT(cfg_out != NULL, "cfg_out is null pointer!");

	/* Stop, if cfg_out is null pointer */
	if (cfg_out == NULL) {
		return -EINVAL;
	}

	*cfg_out = data->uart_cfg;
	return 0;
}
#endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
/**
 * @brief Fill FIFO with data
 * This function is expected to be called from UART interrupt handler (ISR),
 * if uart_irq_tx_ready() returns true. This function does not block!
 * IP has no fifo. Hence only 1 data can be sent at a time!
 *
 * @param dev UART device struct
 * @param tx_data Data to transmit
 * @param size Number of bytes to send (unused)
 *
 * @return Number of bytes sent
 */
static int uart_altera_fifo_fill(const struct device *dev,
				  const uint8_t *tx_data,
				  int size)
{
	ARG_UNUSED(size);

	const struct uart_altera_device_config *config = dev->config;
	struct uart_altera_device_data *data = dev->data;
	int ret_val;

	/* generate fatal error if CONFIG_ASSERT is enabled. */
	__ASSERT(tx_data != NULL, "tx_data is null pointer!");

	/* Stop, if tx_data is null pointer */
	if (tx_data == NULL) {
		return 0;
	}

	k_spinlock_key_t key = k_spin_lock(&data->lock);

	if (data->status_act & ALTERA_AVALON_UART_STATUS_TRDY_MSK) {
		sys_write32(*tx_data, config->base + ALTERA_AVALON_UART_TXDATA_REG_OFFSET);
		ret_val = 1;
		/* function may be called in a loop. update the actual status! */
		data->status_act = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET);
	} else {
		ret_val = 0;
	}

#ifdef CONFIG_UART_ALTERA_LINE_CTRL_WORKAROUND
	/* clear and CTS rising edge! */
	data->dcts_rising = 0;
#endif /* CONFIG_UART_ALTERA_LINE_CTRL_WORKAROUND */

	k_spin_unlock(&data->lock, key);

	return ret_val;
}

/**
 * @brief Read data from FIFO
 * This function is expected to be called from UART interrupt handler (ISR),
 * if uart_irq_rx_ready() returns true.
 * IP has no fifo. Hence only 1 data can be read at a time!
 *
 * @param dev UART device struct
 * @param rx_data Data container
 * @param size Container size
 *
 * @return Number of bytes read
 */
static int uart_altera_fifo_read(const struct device *dev, uint8_t *rx_data,
				  const int size)
{
	ARG_UNUSED(size);

	const struct uart_altera_device_config *config = dev->config;
	struct uart_altera_device_data *data = dev->data;
	int ret_val;

	/* generate fatal error if CONFIG_ASSERT is enabled. */
	__ASSERT(rx_data != NULL, "rx_data is null pointer!");

	/* Stop, if rx_data is null pointer */
	if (rx_data == NULL) {
		return 0;
	}

	k_spinlock_key_t key = k_spin_lock(&data->lock);

	if (data->status_act & ALTERA_AVALON_UART_STATUS_RRDY_MSK) {
		*rx_data = sys_read32(config->base + ALTERA_AVALON_UART_RXDATA_REG_OFFSET);
		ret_val = 1;

		/* function may be called in a loop. update the actual status! */
		data->status_act = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET);
	} else {
		ret_val = 0;
	}

#ifdef CONFIG_UART_ALTERA_LINE_CTRL_WORKAROUND
	/* assert RTS as soon as rx data is read, as IP has no fifo. */
	data->status_act = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET);
	if (((data->status_act & ALTERA_AVALON_UART_STATUS_RRDY_MSK) == 0)
	  && (data->status_act & ALTERA_AVALON_UART_STATUS_CTS_MSK)) {
		data->control_val |= ALTERA_AVALON_UART_CONTROL_RTS_MSK;
		sys_write32(data->control_val, config->base
					+ ALTERA_AVALON_UART_CONTROL_REG_OFFSET);
	}
#endif /* CONFIG_UART_ALTERA_LINE_CTRL_WORKAROUND */

	k_spin_unlock(&data->lock, key);

	return ret_val;
}

/**
 * @brief Enable TX interrupt
 *
 * @param dev UART device struct
 */
static void uart_altera_irq_tx_enable(const struct device *dev)
{
	struct uart_altera_device_data *data = dev->data;
	const struct uart_altera_device_config *config = dev->config;

	k_spinlock_key_t key = k_spin_lock(&data->lock);

	data->control_val |= ALTERA_AVALON_UART_CONTROL_TRDY_MSK;

#ifdef CONFIG_UART_LINE_CTRL
	/* also enable RTS, if flow control is enabled. */
	data->control_val |= ALTERA_AVALON_UART_CONTROL_RTS_MSK;
#endif

	sys_write32(data->control_val, config->base + ALTERA_AVALON_UART_CONTROL_REG_OFFSET);

	k_spin_unlock(&data->lock, key);
}

/**
 * @brief Disable TX interrupt
 *
 * @param dev UART device struct
 */
static void uart_altera_irq_tx_disable(const struct device *dev)
{
	struct uart_altera_device_data *data = dev->data;
	const struct uart_altera_device_config *config = dev->config;

	k_spinlock_key_t key = k_spin_lock(&data->lock);

	data->control_val &= ~ALTERA_AVALON_UART_CONTROL_TRDY_MSK;

#ifdef CONFIG_UART_LINE_CTRL
	/* also disable RTS, if flow control is enabled. */
	data->control_val &= ~ALTERA_AVALON_UART_CONTROL_RTS_MSK;
#endif

	sys_write32(data->control_val, config->base + ALTERA_AVALON_UART_CONTROL_REG_OFFSET);

	k_spin_unlock(&data->lock, key);
}

/**
 * @brief Check if UART TX buffer can accept a new char.
 *
 * @param dev UART device struct
 *
 * @return 1 if TX interrupt is enabled and at least one char can be written to UART.
 *         0 if device is not ready to write a new byte.
 */
static int uart_altera_irq_tx_ready(const struct device *dev)
{
	struct uart_altera_device_data *data = dev->data;
	int ret_val = 0;

	k_spinlock_key_t key = k_spin_lock(&data->lock);

	/* if TX interrupt is enabled */
	if (data->control_val & ALTERA_AVALON_UART_CONTROL_TRDY_MSK) {
		/* IP core does not have fifo. Wait until tx data is completely shifted. */
		if (data->status_act & ALTERA_AVALON_UART_STATUS_TMT_MSK) {
			ret_val = 1;
		}
	}

#ifdef CONFIG_UART_LINE_CTRL
	/* if flow control is enabled, set tx not ready, if CTS is low. */
	if ((data->status_act & ALTERA_AVALON_UART_STATUS_CTS_MSK) == 0) {
		ret_val = 0;
	}
#ifdef CONFIG_UART_ALTERA_LINE_CTRL_WORKAROUND
	if (data->dcts_rising == 0) {
		ret_val = 0;
	}
#endif /* CONFIG_UART_ALTERA_LINE_CTRL_WORKAROUND */

#endif /* CONFIG_UART_LINE_CTRL */

	k_spin_unlock(&data->lock, key);

	return ret_val;
}

/**
 * @brief Check if nothing remains to be transmitted
 *
 * @param dev UART device struct
 *
 * @return 1 if nothing remains to be transmitted, 0 otherwise
 */
static int uart_altera_irq_tx_complete(const struct device *dev)
{
	struct uart_altera_device_data *data = dev->data;
	int ret_val = 0;

	k_spinlock_key_t key = k_spin_lock(&data->lock);

	if (data->status_act & ALTERA_AVALON_UART_STATUS_TMT_MSK) {
		ret_val = 1;
	}

	k_spin_unlock(&data->lock, key);

	return ret_val;
}

/**
 * @brief Enable RX interrupt in
 *
 * @param dev UART device struct
 */
static void uart_altera_irq_rx_enable(const struct device *dev)
{
	struct uart_altera_device_data *data = dev->data;
	const struct uart_altera_device_config *config = dev->config;

	k_spinlock_key_t key = k_spin_lock(&data->lock);

	data->control_val |= ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
	sys_write32(data->control_val, config->base + ALTERA_AVALON_UART_CONTROL_REG_OFFSET);

	k_spin_unlock(&data->lock, key);
}

/**
 * @brief Disable RX interrupt
 *
 * @param dev UART device struct
 */
static void uart_altera_irq_rx_disable(const struct device *dev)
{
	struct uart_altera_device_data *data = dev->data;
	const struct uart_altera_device_config *config = dev->config;

	k_spinlock_key_t key = k_spin_lock(&data->lock);

	data->control_val &= ~ALTERA_AVALON_UART_CONTROL_RRDY_MSK;
	sys_write32(data->control_val, config->base + ALTERA_AVALON_UART_CONTROL_REG_OFFSET);

	k_spin_unlock(&data->lock, key);
}

/**
 * @brief Check if Rx IRQ has been raised
 *
 * @param dev UART device struct
 *
 * @return 1 if an IRQ is ready, 0 otherwise
 */
static int uart_altera_irq_rx_ready(const struct device *dev)
{
	struct uart_altera_device_data *data = dev->data;
	int ret_val = 0;

	k_spinlock_key_t key = k_spin_lock(&data->lock);

	/* if RX interrupt is enabled */
	if (data->control_val & ALTERA_AVALON_UART_CONTROL_RRDY_MSK) {
		/* check for space in rx data register */
		if (data->status_act & ALTERA_AVALON_UART_STATUS_RRDY_MSK) {
			ret_val = 1;
		}
	}

	k_spin_unlock(&data->lock, key);

	return ret_val;
}

/**
 * @brief This function will cache the status register.
 *
 * @param dev UART device struct
 *
 * @return 1 for success.
 */
static int uart_altera_irq_update(const struct device *dev)
{
	struct uart_altera_device_data *data = dev->data;
	const struct uart_altera_device_config *config = dev->config;

	k_spinlock_key_t key = k_spin_lock(&data->lock);

	data->status_act = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET);

	k_spin_unlock(&data->lock, key);

	return 1;
}

/**
 * @brief Check if any IRQ is pending
 *
 * @param dev UART device struct
 *
 * @return 1 if an IRQ is pending, 0 otherwise
 */
static int uart_altera_irq_is_pending(const struct device *dev)
{
	struct uart_altera_device_data *data = dev->data;
	int ret_val = 0;

	k_spinlock_key_t key = k_spin_lock(&data->lock);

	if (data->status_act & data->control_val & ALTERA_AVALON_UART_PENDING_MASK) {
		ret_val = 1;
	}

	k_spin_unlock(&data->lock, key);

	return ret_val;
}

/**
 * @brief Set the callback function pointer for IRQ.
 *
 * @param dev UART device struct
 * @param cb Callback function pointer.
 * @param cb_data Data to pass to callback function.
 */
static void uart_altera_irq_callback_set(const struct device *dev,
					  uart_irq_callback_user_data_t cb,
					  void *cb_data)
{
	struct uart_altera_device_data *data = dev->data;

	/* generate fatal error if CONFIG_ASSERT is enabled. */
	__ASSERT(cb != NULL, "uart_irq_callback_user_data_t cb is null pointer!");

	k_spinlock_key_t key = k_spin_lock(&data->lock);

#ifdef CONFIG_UART_ALTERA_EOP
	if (data->set_eop_cb) {
		data->cb_eop = cb;
		data->cb_data_eop = cb_data;
		data->set_eop_cb = 0;
	} else {
		data->cb = cb;
		data->cb_data = cb_data;
	}
#else
	data->cb = cb;
	data->cb_data = cb_data;
#endif /* CONFIG_UART_ALTERA_EOP */

	k_spin_unlock(&data->lock, key);
}

#ifdef CONFIG_UART_LINE_CTRL
/**
 * @brief DCTS Interrupt service routine.
 *
 * Handles assertion and deassettion of CTS/RTS stignal
 *
 * @param dev Pointer to UART device struct
 */
static void uart_altera_dcts_isr(const struct device *dev)
{
	struct uart_altera_device_data *data = dev->data;
	const struct uart_altera_device_config *config = dev->config;

	k_spinlock_key_t key = k_spin_lock(&data->lock);

	/* Assume that user follows zephyr requirement and update status in their call back. */
	if (data->status_act & ALTERA_AVALON_UART_STATUS_CTS_MSK) {
#ifdef CONFIG_UART_ALTERA_LINE_CTRL_WORKAROUND
		data->dcts_rising = 1;
#endif /* CONFIG_UART_ALTERA_LINE_CTRL_WORKAROUND */

		/* check if device is ready to receive character */
		if ((data->status_act & ALTERA_AVALON_UART_STATUS_RRDY_MSK) == 0) {
			/* Assert RTS to inform other UART. */
			data->control_val |= ALTERA_AVALON_UART_CONTROL_RTS_MSK;
			sys_write32(data->control_val, config->base
						+ ALTERA_AVALON_UART_CONTROL_REG_OFFSET);
		}
	} else {
		/* other UART deasserts RTS */
		if (data->status_act & ALTERA_AVALON_UART_STATUS_TMT_MSK) {
			/* only deasserts if not transmitting. */
			data->control_val &= ~ALTERA_AVALON_UART_CONTROL_RTS_MSK;
			sys_write32(data->control_val, config->base
						+ ALTERA_AVALON_UART_CONTROL_REG_OFFSET);
		}
	}

	k_spin_unlock(&data->lock, key);
}
#endif /* CONFIG_UART_LINE_CTRL */

/**
 * @brief Interrupt service routine.
 *
 * This simply calls the callback function, if one exists.
 *
 * @param dev Pointer to UART device struct
 *
 */
static void uart_altera_isr(const struct device *dev)
{
	struct uart_altera_device_data *data = dev->data;
	const struct uart_altera_device_config *config = dev->config;

	uart_irq_callback_user_data_t callback = data->cb;

	/* Pre ISR */
#ifdef CONFIG_UART_ALTERA_LINE_CTRL_WORKAROUND
	/* deassert RTS as soon as rx data is received, as IP has no fifo. */
	data->status_act = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET);
	if (data->status_act & ALTERA_AVALON_UART_STATUS_RRDY_MSK) {
		data->control_val &= ~ALTERA_AVALON_UART_CONTROL_RTS_MSK;
		sys_write32(data->control_val, config->base
					+ ALTERA_AVALON_UART_CONTROL_REG_OFFSET);
	}
#endif /* CONFIG_UART_ALTERA_LINE_CTRL_WORKAROUND */

	if (callback) {
		callback(dev, data->cb_data);
	}

	/* Post ISR */
#if CONFIG_UART_ALTERA_EOP
	data->status_act = sys_read32(config->base + ALTERA_AVALON_UART_STATUS_REG_OFFSET);

	if (data->status_act & ALTERA_AVALON_UART_STATUS_EOP_MSK) {
		callback = data->cb_eop;
		if (callback) {
			callback(dev, data->cb_data_eop);
		}
	}
#endif /* CONFIG_UART_ALTERA_EOP */

#ifdef CONFIG_UART_LINE_CTRL
	/* handles RTS/CTS signal */
	if (data->status_act & ALTERA_AVALON_UART_STATUS_DCTS_MSK) {
		uart_altera_dcts_isr(dev);
	}
#endif

	/* clear status after all interrupts are handled. */
	sys_write32(ALTERA_AVALON_UART_CLEAR_STATUS_VAL, config->base
				+ ALTERA_AVALON_UART_STATUS_REG_OFFSET);
}

#ifdef CONFIG_UART_DRV_CMD
/**
 * @brief Send extra command to driver
 *
 * @param dev UART device struct
 * @param cmd Command to driver
 * @param p Parameter to the command
 *
 * @return 0 if successful, failed otherwise
 */
static int uart_altera_drv_cmd(const struct device *dev, uint32_t cmd,
				uint32_t p)
{
	struct uart_altera_device_data *data = dev->data;
#if CONFIG_UART_ALTERA_EOP
	const struct uart_altera_device_config *config = dev->config;
#endif
	int ret_val = -ENOTSUP;
	k_spinlock_key_t key = k_spin_lock(&data->lock);

	switch (cmd) {
#if CONFIG_UART_ALTERA_EOP
	case CMD_ENABLE_EOP:
		/* enable EOP interrupt */
		data->control_val |= ALTERA_AVALON_UART_CONTROL_EOP_MSK;
		sys_write32(data->control_val, config->base
					+ ALTERA_AVALON_UART_CONTROL_REG_OFFSET);

		/* set EOP character */
		sys_write32((uint8_t) p, config->base + ALTERA_AVALON_UART_EOP_REG_OFFSET);

		/* after this, user needs to call uart_irq_callback_set
		 * to set data->cb_eop and data->cb_data_eop!
		 */
		data->set_eop_cb = 1;
		ret_val = 0;
		break;

	case CMD_DISABLE_EOP:
		/* Disable EOP interrupt */
		data->control_val &= ~ALTERA_AVALON_UART_CONTROL_EOP_MSK;
		sys_write32(data->control_val, config->base
					+ ALTERA_AVALON_UART_CONTROL_REG_OFFSET);

		/* clear call back */
		data->cb_eop = NULL;
		data->cb_data_eop = NULL;
		ret_val = 0;
		break;
#endif /* CONFIG_UART_ALTERA_EOP */
	default:
		ret_val = -ENOTSUP;
		break;
	};

	k_spin_unlock(&data->lock, key);

	return ret_val;
}

#endif /* CONFIG_UART_DRV_CMD */

#endif /* CONFIG_UART_INTERRUPT_DRIVEN */

static const struct uart_driver_api uart_altera_driver_api = {
	.poll_in = uart_altera_poll_in,
	.poll_out = uart_altera_poll_out,
	.err_check = uart_altera_err_check,
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
	.configure = uart_altera_configure,
	.config_get = uart_altera_config_get,
#endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	.fifo_fill = uart_altera_fifo_fill,
	.fifo_read = uart_altera_fifo_read,
	.irq_tx_enable = uart_altera_irq_tx_enable,
	.irq_tx_disable = uart_altera_irq_tx_disable,
	.irq_tx_ready = uart_altera_irq_tx_ready,
	.irq_tx_complete = uart_altera_irq_tx_complete,
	.irq_rx_enable = uart_altera_irq_rx_enable,
	.irq_rx_disable = uart_altera_irq_rx_disable,
	.irq_rx_ready = uart_altera_irq_rx_ready,
	.irq_is_pending = uart_altera_irq_is_pending,
	.irq_update = uart_altera_irq_update,
	.irq_callback_set = uart_altera_irq_callback_set,
#endif

#ifdef CONFIG_UART_DRV_CMD
	.drv_cmd = uart_altera_drv_cmd,
#endif
};

#ifdef CONFIG_UART_INTERRUPT_DRIVEN

#define UART_ALTERA_IRQ_CONFIG_FUNC(n)                                    \
	static void uart_altera_irq_config_func_##n(const struct device *dev) \
	{                                                                     \
		IRQ_CONNECT(DT_INST_IRQN(n),                                      \
				DT_INST_IRQ(n, priority),                                 \
				uart_altera_isr,                                          \
				DEVICE_DT_INST_GET(n), 0);		                          \
		                                                                  \
		irq_enable(DT_INST_IRQN(n));                                      \
	}

#define UART_ALTERA_IRQ_CONFIG_INIT(n)                                    \
	.irq_config_func = uart_altera_irq_config_func_##n,                   \
	.irq_num = DT_INST_IRQN(n),

#else

#define UART_ALTERA_IRQ_CONFIG_FUNC(n)
#define UART_ALTERA_IRQ_CONFIG_INIT(n)

#endif /* CONFIG_UART_INTERRUPT_DRIVEN */

#define UART_ALTERA_DEVICE_INIT(n)                                        \
UART_ALTERA_IRQ_CONFIG_FUNC(n)                                            \
static struct uart_altera_device_data uart_altera_dev_data_##n = {        \
	.uart_cfg =                                                           \
	{                                                                     \
			.baudrate = DT_INST_PROP(n, current_speed),                   \
			.parity = DT_INST_ENUM_IDX_OR(n, parity,                      \
						 UART_CFG_PARITY_NONE),                           \
			.stop_bits = DT_INST_ENUM_IDX_OR(n, stop_bits,                \
						 UART_CFG_STOP_BITS_1),                           \
			.data_bits = DT_INST_ENUM_IDX_OR(n, data_bits,                \
						 UART_CFG_DATA_BITS_8),                           \
			.flow_ctrl = DT_INST_PROP(n, hw_flow_control) ?               \
				UART_CFG_FLOW_CTRL_RTS_CTS :                              \
				UART_CFG_FLOW_CTRL_NONE,                                  \
	},                                                                    \
};                                                                        \
	                                                                      \
static const struct uart_altera_device_config uart_altera_dev_cfg_##n = { \
	.base = DT_INST_REG_ADDR(n),                                          \
	.flags = ((DT_INST_PROP(n, fixed_baudrate)?ALT_AVALON_UART_FB:0)      \
			  |(DT_INST_PROP(n, hw_flow_control)?ALT_AVALON_UART_FC:0)),  \
	UART_ALTERA_IRQ_CONFIG_INIT(n)                                        \
};                                                                        \
	                                                                      \
DEVICE_DT_INST_DEFINE(n,                                                  \
			  uart_altera_init,                                           \
			  NULL,                                                       \
			  &uart_altera_dev_data_##n,                                  \
			  &uart_altera_dev_cfg_##n,                                   \
			  PRE_KERNEL_1,                                               \
			  CONFIG_SERIAL_INIT_PRIORITY,                                \
			  &uart_altera_driver_api);

DT_INST_FOREACH_STATUS_OKAY(UART_ALTERA_DEVICE_INIT)