ladybird/Kernel/Devices/SerialDevice.h
Liav A 05ba034000 Kernel: Introduce the IOWindow class
This class is intended to replace all IOAddress usages in the Kernel
codebase altogether. The idea is to ensure IO can be done in
arch-specific manner that is determined mostly in compile-time, but to
still be able to use most of the Kernel code in non-x86 builds. Specific
devices that rely on x86-specific IO instructions are already placed in
the Arch/x86 directory and are omitted for non-x86 builds.

The reason this works so well is the fact that x86 IO space acts in a
similar fashion to the traditional memory space being available in most
CPU architectures - the x86 IO space is essentially just an array of
bytes like the physical memory address space, but requires x86 IO
instructions to load and store data. Therefore, many devices allow host
software to interact with the hardware registers in both ways, with a
noticeable trend even in the modern x86 hardware to move away from the
old x86 IO space to exclusively using memory-mapped IO.

Therefore, the IOWindow class encapsulates both methods for x86 builds.
The idea is to allow PCI devices to be used in either way in x86 builds,
so when trying to map an IOWindow on a PCI BAR, the Kernel will try to
find the proper method being declared with the PCI BAR flags.
For old PCI hardware on non-x86 builds this might turn into a problem as
we can't use port mapped IO, so the Kernel will gracefully fail with
ENOTSUP error code if that's the case, as there's really nothing we can
do within such case.

For general IO, the read{8,16,32} and write{8,16,32} methods are
available as a convenient API for other places in the Kernel. There are
simply no direct 64-bit IO API methods yet, as it's not needed right now
and is not considered to be Arch-agnostic too - the x86 IO space doesn't
support generating 64 bit cycle on IO bus and instead requires two 2
32-bit accesses. If for whatever reason it appears to be necessary to do
IO in such manner, it could probably be added with some neat tricks to
do so. It is recommended to use Memory::TypedMapping struct if direct 64
bit IO is actually needed.
2022-09-23 17:22:15 +01:00

137 lines
3.7 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <Kernel/Devices/CharacterDevice.h>
#include <Kernel/IOWindow.h>
namespace Kernel {
class SerialDevice final : public CharacterDevice {
friend class DeviceManagement;
public:
static NonnullLockRefPtr<SerialDevice> must_create(size_t com_number);
virtual ~SerialDevice() override;
// ^CharacterDevice
virtual bool can_read(OpenFileDescription const&, u64) const override;
virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
virtual bool can_write(OpenFileDescription const&, u64) const override;
virtual ErrorOr<size_t> write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t) override;
void put_char(char);
enum InterruptEnable {
LowPowerMode = 0x01 << 5,
SleepMode = 0x01 << 4,
ModemStatusInterrupt = 0x01 << 3,
ReceiverLineStatusInterrupt = 0x01 << 2,
TransmitterHoldingRegisterEmptyInterrupt = 0x01 << 1,
ReceivedDataAvailableInterrupt = 0x01 << 0
};
enum Baud {
Baud50 = 2304,
Baud110 = 1047,
Baud220 = 524,
Baud300 = 384,
Baud600 = 192,
Baud1200 = 96,
Baud2400 = 48,
Baud4800 = 24,
Baud9600 = 12,
Baud19200 = 6,
Baud38400 = 3,
Baud57600 = 2,
Baud115200 = 1
};
enum ParitySelect {
None = 0x00 << 3,
Odd = 0x01 << 3,
Even = 0x03 << 3,
Mark = 0x05 << 3,
Space = 0x07 << 3
};
enum StopBits {
One = 0x00 << 2,
Two = 0x01 << 2
};
enum WordLength {
FiveBits = 0x00,
SixBits = 0x01,
SevenBits = 0x02,
EightBits = 0x03
};
enum FIFOControl {
EnableFIFO = 0x01 << 0,
ClearReceiveFIFO = 0x01 << 1,
ClearTransmitFIFO = 0x01 << 2,
Enable64ByteFIFO = 0x01 << 5,
TriggerLevel1 = 0x00 << 6,
TriggerLevel2 = 0x01 << 6,
TriggerLevel3 = 0x02 << 6,
TriggerLevel4 = 0x03 << 6
};
enum ModemControl {
AutoflowControlEnabled = 0x01 << 5,
LoopbackMode = 0x01 << 4,
AuxiliaryOutput2 = 0x01 << 3,
AuxiliaryOutput1 = 0x01 << 2,
RequestToSend = 0x01 << 1,
DataTerminalReady = 0x01 << 0
};
enum LineStatus {
ErrorInReceivedFIFO = 0x01 << 7,
EmptyDataHoldingRegisters = 0x01 << 6,
EmptyTransmitterHoldingRegister = 0x01 << 5,
BreakInterrupt = 0x01 << 4,
FramingError = 0x01 << 3,
ParityError = 0x01 << 2,
OverrunError = 0x01 << 1,
DataReady = 0x01 << 0
};
private:
SerialDevice(NonnullOwnPtr<IOWindow> registers_io_window, unsigned minor);
friend class PCISerialDevice;
// ^CharacterDevice
virtual StringView class_name() const override { return "SerialDevice"sv; }
void initialize();
void set_interrupts(bool interrupt_enable);
void set_baud(Baud);
void set_fifo_control(u8 fifo_control);
void set_line_control(ParitySelect, StopBits, WordLength);
void set_break_enable(bool break_enable);
void set_modem_control(u8 modem_control);
u8 get_line_status() const;
mutable NonnullOwnPtr<IOWindow> m_registers_io_window;
bool m_interrupt_enable { false };
u8 m_fifo_control { 0 };
Baud m_baud { Baud38400 };
ParitySelect m_parity_select { None };
StopBits m_stop_bits { One };
WordLength m_word_length { EightBits };
bool m_break_enable { false };
u8 m_modem_control { 0 };
bool m_last_put_char_was_carriage_return { false };
Spinlock m_serial_lock { LockRank::None };
};
}