xref: /optee_os/core/drivers/stih_asc.c (revision 1bb929836182ecb96d2d9d268daa807c67596396)
1*1bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause
239e661bcSEtienne Carriere /*
339e661bcSEtienne Carriere  * Copyright (c) 2017, Linaro Limited
439e661bcSEtienne Carriere  * All rights reserved.
539e661bcSEtienne Carriere  *
639e661bcSEtienne Carriere  * Redistribution and use in source and binary forms, with or without
739e661bcSEtienne Carriere  * modification, are permitted provided that the following conditions are met:
839e661bcSEtienne Carriere  *
939e661bcSEtienne Carriere  * 1. Redistributions of source code must retain the above copyright notice,
1039e661bcSEtienne Carriere  * this list of conditions and the following disclaimer.
1139e661bcSEtienne Carriere  *
1239e661bcSEtienne Carriere  * 2. Redistributions in binary form must reproduce the above copyright notice,
1339e661bcSEtienne Carriere  * this list of conditions and the following disclaimer in the documentation
1439e661bcSEtienne Carriere  * and/or other materials provided with the distribution.
1539e661bcSEtienne Carriere  *
1639e661bcSEtienne Carriere  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1739e661bcSEtienne Carriere  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1839e661bcSEtienne Carriere  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1939e661bcSEtienne Carriere  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2039e661bcSEtienne Carriere  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2139e661bcSEtienne Carriere  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2239e661bcSEtienne Carriere  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2339e661bcSEtienne Carriere  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2439e661bcSEtienne Carriere  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2539e661bcSEtienne Carriere  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2639e661bcSEtienne Carriere  * POSSIBILITY OF SUCH DAMAGE.
2739e661bcSEtienne Carriere  */
2839e661bcSEtienne Carriere #include <drivers/stih_asc.h>
2939e661bcSEtienne Carriere #include <io.h>
300cb71d15SEtienne Carriere #include <keep.h>
3139e661bcSEtienne Carriere #include <util.h>
3239e661bcSEtienne Carriere 
3339e661bcSEtienne Carriere #define ASC_BAUDRATE		0x00
3439e661bcSEtienne Carriere #define ASC_TXBUFFER		0x04
3539e661bcSEtienne Carriere #define ASC_STATUS		0x14
3639e661bcSEtienne Carriere 
3739e661bcSEtienne Carriere #define ASC_STATUS_TX_EMPTY		BIT(1)
3839e661bcSEtienne Carriere #define ASC_STATUS_TX_HALF_EMPTY	BIT(2)
3939e661bcSEtienne Carriere 
400cb71d15SEtienne Carriere static vaddr_t chip_to_base(struct serial_chip *chip)
4139e661bcSEtienne Carriere {
420cb71d15SEtienne Carriere 	struct stih_asc_pd *pd =
430cb71d15SEtienne Carriere 		container_of(chip, struct stih_asc_pd, chip);
440cb71d15SEtienne Carriere 
450cb71d15SEtienne Carriere 	return io_pa_or_va(&pd->base);
460cb71d15SEtienne Carriere }
470cb71d15SEtienne Carriere 
480cb71d15SEtienne Carriere static void stih_asc_flush(struct serial_chip *chip)
490cb71d15SEtienne Carriere {
500cb71d15SEtienne Carriere 	vaddr_t base = chip_to_base(chip);
510cb71d15SEtienne Carriere 
5239e661bcSEtienne Carriere 	while (!(read32(base + ASC_STATUS) & ASC_STATUS_TX_EMPTY))
5339e661bcSEtienne Carriere 		;
5439e661bcSEtienne Carriere }
5539e661bcSEtienne Carriere 
560cb71d15SEtienne Carriere static void stih_asc_putc(struct serial_chip *chip, int ch)
5739e661bcSEtienne Carriere {
580cb71d15SEtienne Carriere 	vaddr_t base = chip_to_base(chip);
590cb71d15SEtienne Carriere 
6039e661bcSEtienne Carriere 	while (!(read32(base + ASC_STATUS) & ASC_STATUS_TX_HALF_EMPTY))
6139e661bcSEtienne Carriere 		;
6239e661bcSEtienne Carriere 
6339e661bcSEtienne Carriere 	write32(ch, base + ASC_TXBUFFER);
6439e661bcSEtienne Carriere }
6539e661bcSEtienne Carriere 
660cb71d15SEtienne Carriere static const struct serial_ops stih_asc_ops = {
670cb71d15SEtienne Carriere 	.flush = stih_asc_flush,
680cb71d15SEtienne Carriere 	.putc = stih_asc_putc,
690cb71d15SEtienne Carriere };
700cb71d15SEtienne Carriere KEEP_PAGER(stih_asc_ops);
710cb71d15SEtienne Carriere 
720cb71d15SEtienne Carriere void stih_asc_init(struct stih_asc_pd *pd, vaddr_t base)
7339e661bcSEtienne Carriere {
740cb71d15SEtienne Carriere 	pd->base.pa = base;
750cb71d15SEtienne Carriere 	pd->chip.ops = &stih_asc_ops;
7639e661bcSEtienne Carriere }
77