source: mainline/uspace/drv/char/ns8250/ns8250.c

Last change on this file was b9cc81c6, checked in by Jiri Svoboda <jiri@…>, 5 months ago

Implement quiesce in NS8250 and PC-LPT.

  • Property mode set to 100644
File size: 29.0 KB
Line 
1/*
2 * Copyright (c) 2025 Jiri Svoboda
3 * Copyright (c) 2010 Lenka Trochtova
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/**
31 * @addtogroup ns8250
32 * @{
33 */
34
35/** @file
36 */
37
38#include <assert.h>
39#include <stdio.h>
40#include <errno.h>
41#include <str_error.h>
42#include <stdbool.h>
43#include <fibril_synch.h>
44#include <stdlib.h>
45#include <str.h>
46#include <ctype.h>
47#include <macros.h>
48#include <stdlib.h>
49#include <dirent.h>
50#include <ddi.h>
51
52#include <ddf/driver.h>
53#include <ddf/interrupt.h>
54#include <ddf/log.h>
55#include <io/chardev_srv.h>
56
57#include <device/hw_res.h>
58#include <ipc/serial_ctl.h>
59
60#include "cyclic_buffer.h"
61
62#define NAME "ns8250"
63
64#define REG_COUNT 7
65#define MAX_BAUD_RATE 115200
66#define DLAB_MASK (1 << 7)
67
68/** Interrupt Enable Register definition. */
69#define NS8250_IER_RXREADY (1 << 0)
70#define NS8250_IER_THRE (1 << 1)
71#define NS8250_IER_RXSTATUS (1 << 2)
72#define NS8250_IER_MODEM_STATUS (1 << 3)
73
74/** Interrupt ID Register definition. */
75#define NS8250_IID_ACTIVE (1 << 0)
76#define NS8250_IID_CAUSE_MASK 0x0e
77#define NS8250_IID_CAUSE_RXSTATUS 0x06
78
79/** FIFO Control Register definition. */
80#define NS8250_FCR_FIFOENABLE (1 << 0)
81#define NS8250_FCR_RXFIFORESET (1 << 1)
82#define NS8250_FCR_TXFIFORESET (1 << 2)
83#define NS8250_FCR_DMAMODE (1 << 3)
84#define NS8250_FCR_RXTRIGGERLOW (1 << 6)
85#define NS8250_FCR_RXTRIGGERHI (1 << 7)
86
87/** Line Control Register definition. */
88#define NS8250_LCR_STOPBITS (1 << 2)
89#define NS8250_LCR_PARITY (1 << 3)
90#define NS8250_LCR_SENDBREAK (1 << 6)
91#define NS8250_LCR_DLAB (1 << 7)
92
93/** Modem Control Register definition. */
94#define NS8250_MCR_DTR (1 << 0)
95#define NS8250_MCR_RTS (1 << 1)
96#define NS8250_MCR_OUT1 (1 << 2)
97#define NS8250_MCR_OUT2 (1 << 3)
98#define NS8250_MCR_LOOPBACK (1 << 4)
99#define NS8250_MCR_ALL (0x1f)
100
101/** Line Status Register definition. */
102#define NS8250_LSR_RXREADY (1 << 0)
103#define NS8250_LSR_OE (1 << 1)
104#define NS8250_LSR_PE (1 << 2)
105#define NS8250_LSR_FE (1 << 3)
106#define NS8250_LSR_BREAK (1 << 4)
107#define NS8250_LSR_THRE (1 << 5)
108#define NS8250_LSR_TSE (1 << 6)
109
110/** Modem Status Register definition. */
111#define NS8250_MSR_DELTACTS (1 << 0)
112#define NS8250_MSR_DELTADSR (1 << 1)
113#define NS8250_MSR_RITRAILING (1 << 2)
114#define NS8250_MSR_DELTADCD (1 << 3)
115#define NS8250_MSR_CTS (1 << 4)
116#define NS8250_MSR_DSR (1 << 5)
117#define NS8250_MSR_RI (1 << 6)
118#define NS8250_MSR_DCD (1 << 7)
119#define NS8250_MSR_SIGNALS (NS8250_MSR_CTS | NS8250_MSR_DSR \
120 | NS8250_MSR_RI | NS8250_MSR_DCD)
121
122/** The number of bits of one data unit send by the serial port. */
123typedef enum {
124 WORD_LENGTH_5,
125 WORD_LENGTH_6,
126 WORD_LENGTH_7,
127 WORD_LENGTH_8
128} word_length_t;
129
130/** The number of stop bits used by the serial port. */
131typedef enum {
132 /** Use one stop bit. */
133 ONE_STOP_BIT,
134 /** 1.5 stop bits for word length 5, 2 stop bits otherwise. */
135 TWO_STOP_BITS
136} stop_bit_t;
137
138/** 8250 UART registers layout. */
139typedef struct {
140 ioport8_t data; /**< Data register. */
141 ioport8_t ier; /**< Interrupt Enable Reg. */
142 ioport8_t iid; /**< Interrupt ID Reg. */
143 ioport8_t lcr; /**< Line Control Reg. */
144 ioport8_t mcr; /**< Modem Control Reg. */
145 ioport8_t lsr; /**< Line Status Reg. */
146 ioport8_t msr; /**< Modem Status Reg. */
147} ns8250_regs_t;
148
149/** The driver data for the serial port devices. */
150typedef struct ns8250 {
151 /** DDF device node */
152 ddf_dev_t *dev;
153 /** DDF function node */
154 ddf_fun_t *fun;
155 /** Character device service */
156 chardev_srvs_t cds;
157 /** Parent session */
158 async_sess_t *parent_sess;
159 /** I/O registers */
160 ns8250_regs_t *regs;
161 /** Are there any clients connected to the device? */
162 unsigned client_connections;
163 /** The irq assigned to this device. */
164 int irq;
165 /** IRQ capability handle */
166 cap_irq_handle_t irq_handle;
167 /** The base i/o address of the devices registers. */
168 uintptr_t io_addr;
169 /** The i/o port used to access the serial ports registers. */
170 ioport8_t *port;
171 /** The buffer for incoming data. */
172 cyclic_buffer_t input_buffer;
173 /** The fibril mutex for synchronizing the access to the device. */
174 fibril_mutex_t mutex;
175 /** Indicates that some data has become available */
176 fibril_condvar_t input_buffer_available;
177 /** True if device is removed. */
178 bool removed;
179} ns8250_t;
180
181/** Obtain soft-state structure from device node */
182static ns8250_t *dev_ns8250(ddf_dev_t *dev)
183{
184 return ddf_dev_data_get(dev);
185}
186
187/** Obtain soft-state structure from function node */
188static ns8250_t *fun_ns8250(ddf_fun_t *fun)
189{
190 return dev_ns8250(ddf_fun_get_dev(fun));
191}
192
193/** Obtain soft-state structure from chardev srv */
194static ns8250_t *srv_ns8250(chardev_srv_t *srv)
195{
196 return (ns8250_t *)srv->srvs->sarg;
197}
198
199/** Find out if there is some incoming data available on the serial port.
200 *
201 * @param port The base address of the serial port device's ports.
202 * @return True if there are data waiting to be read, false
203 * otherwise.
204 */
205static bool ns8250_received(ns8250_regs_t *regs)
206{
207 return (pio_read_8(&regs->lsr) & NS8250_LSR_RXREADY) != 0;
208}
209
210/** Read one byte from the serial port.
211 *
212 * @param port The base address of the serial port device's ports.
213 * @return The data read.
214 */
215static uint8_t ns8250_read_8(ns8250_regs_t *regs)
216{
217 return pio_read_8(&regs->data);
218}
219
220/** Find out wheter it is possible to send data.
221 *
222 * @param port The base address of the serial port device's ports.
223 */
224static bool is_transmit_empty(ns8250_regs_t *regs)
225{
226 return (pio_read_8(&regs->lsr) & NS8250_LSR_THRE) != 0;
227}
228
229/** Write one character on the serial port.
230 *
231 * @param port The base address of the serial port device's ports.
232 * @param c The character to be written to the serial port device.
233 */
234static void ns8250_write_8(ns8250_regs_t *regs, uint8_t c)
235{
236 while (!is_transmit_empty(regs))
237 ;
238
239 pio_write_8(&regs->data, c);
240}
241
242/** Read data from the serial port device.
243 *
244 * @param srv Server-side connection data
245 * @param buf The output buffer for read data.
246 * @param count The number of bytes to be read.
247 * @param nread Place to store number of bytes actually read
248 * @param flags @c chardev_f_nonblock not to block waiting for data
249 * even if no data is available
250 *
251 * @return EOK on success or non-zero error code
252 */
253static errno_t ns8250_read(chardev_srv_t *srv, void *buf, size_t count, size_t *nread,
254 chardev_flags_t flags)
255{
256 ns8250_t *ns = srv_ns8250(srv);
257 char *bp = (char *) buf;
258 size_t pos = 0;
259
260 if (count == 0) {
261 *nread = 0;
262 return EOK;
263 }
264
265 fibril_mutex_lock(&ns->mutex);
266 while ((flags & chardev_f_none) == 0 &&
267 buf_is_empty(&ns->input_buffer))
268 fibril_condvar_wait(&ns->input_buffer_available, &ns->mutex);
269 while (!buf_is_empty(&ns->input_buffer) && pos < count) {
270 bp[pos] = (char)buf_pop_front(&ns->input_buffer);
271 pos++;
272 }
273 fibril_mutex_unlock(&ns->mutex);
274
275 *nread = pos;
276 return EOK;
277}
278
279/** Write a character to the serial port.
280 *
281 * @param ns Serial port device
282 * @param c The character to be written
283 */
284static inline void ns8250_putchar(ns8250_t *ns, uint8_t c)
285{
286 fibril_mutex_lock(&ns->mutex);
287 ns8250_write_8(ns->regs, c);
288 fibril_mutex_unlock(&ns->mutex);
289}
290
291/** Write data to the serial port.
292 *
293 * @param srv Server-side connection data
294 * @param buf The data to be written
295 * @param count The number of bytes to be written
296 * @param nwritten Place to store number of bytes successfully written
297 * @return EOK on success or non-zero error code
298 */
299static errno_t ns8250_write(chardev_srv_t *srv, const void *buf, size_t count,
300 size_t *nwritten)
301{
302 ns8250_t *ns = srv_ns8250(srv);
303 size_t idx;
304 uint8_t *bp = (uint8_t *) buf;
305
306 for (idx = 0; idx < count; idx++)
307 ns8250_putchar(ns, bp[idx]);
308
309 *nwritten = count;
310 return EOK;
311}
312
313static errno_t ns8250_open(chardev_srvs_t *, chardev_srv_t *);
314static errno_t ns8250_close(chardev_srv_t *);
315static void ns8250_default_handler(chardev_srv_t *, ipc_call_t *);
316
317/** The character interface's callbacks. */
318static chardev_ops_t ns8250_chardev_ops = {
319 .open = ns8250_open,
320 .close = ns8250_close,
321 .read = ns8250_read,
322 .write = ns8250_write,
323 .def_handler = ns8250_default_handler
324};
325
326static void ns8250_char_conn(ipc_call_t *, void *);
327
328static errno_t ns8250_dev_add(ddf_dev_t *dev);
329static errno_t ns8250_dev_quiesce(ddf_dev_t *dev);
330static errno_t ns8250_dev_remove(ddf_dev_t *dev);
331
332/** The serial port device driver's standard operations. */
333static driver_ops_t ns8250_ops = {
334 .dev_add = &ns8250_dev_add,
335 .dev_remove = &ns8250_dev_remove,
336 .dev_quiesce = &ns8250_dev_quiesce
337};
338
339/** The serial port device driver structure. */
340static driver_t ns8250_driver = {
341 .name = NAME,
342 .driver_ops = &ns8250_ops
343};
344
345/** Clean up the serial port soft-state
346 *
347 * @param ns Serial port device
348 */
349static void ns8250_dev_cleanup(ns8250_t *ns)
350{
351}
352
353/** Enable the i/o ports of the device.
354 *
355 * @param ns Serial port device
356 * @return True on success, false otherwise
357 */
358static bool ns8250_pio_enable(ns8250_t *ns)
359{
360 ddf_msg(LVL_DEBUG, "ns8250_pio_enable %s", ddf_dev_get_name(ns->dev));
361
362 /* Gain control over port's registers. */
363 if (pio_enable((void *) ns->io_addr, REG_COUNT,
364 (void **) &ns->port)) {
365 ddf_msg(LVL_ERROR, "Cannot map the port %#" PRIxn
366 " for device %s.", ns->io_addr, ddf_dev_get_name(ns->dev));
367 return false;
368 }
369
370 ns->regs = (ns8250_regs_t *)ns->port;
371
372 return true;
373}
374
375/** Probe the serial port device for its presence.
376 *
377 * @param ns Serial port device
378 * @return True if the device is present, false otherwise
379 */
380static bool ns8250_dev_probe(ns8250_t *ns)
381{
382 ddf_msg(LVL_DEBUG, "ns8250_dev_probe %s", ddf_dev_get_name(ns->dev));
383
384 bool res = true;
385 uint8_t olddata;
386
387 olddata = pio_read_8(&ns->regs->mcr);
388
389 pio_write_8(&ns->regs->mcr, NS8250_MCR_LOOPBACK);
390 if (pio_read_8(&ns->regs->msr) & NS8250_MSR_SIGNALS)
391 res = false;
392
393 pio_write_8(&ns->regs->mcr, NS8250_MCR_ALL);
394 if ((pio_read_8(&ns->regs->msr) & NS8250_MSR_SIGNALS) !=
395 NS8250_MSR_SIGNALS)
396 res = false;
397
398 pio_write_8(&ns->regs->mcr, olddata);
399
400 if (!res) {
401 ddf_msg(LVL_DEBUG, "Device %s is not present.",
402 ddf_dev_get_name(ns->dev));
403 }
404
405 return res;
406}
407
408/** Initialize serial port device.
409 *
410 * @param ns Serial port device
411 * @return Zero on success, error number otherwise
412 */
413static errno_t ns8250_dev_initialize(ns8250_t *ns)
414{
415 errno_t ret = EOK;
416
417 ddf_msg(LVL_DEBUG, "ns8250_dev_initialize %s", ddf_dev_get_name(ns->dev));
418
419 hw_resource_list_t hw_resources;
420 memset(&hw_resources, 0, sizeof(hw_resource_list_t));
421
422 /* Get hw resources. */
423 ret = hw_res_get_resource_list(ns->parent_sess, &hw_resources);
424 if (ret != EOK) {
425 ddf_msg(LVL_ERROR, "Failed to get HW resources for device "
426 "%s.", ddf_dev_get_name(ns->dev));
427 goto failed;
428 }
429
430 size_t i;
431 hw_resource_t *res;
432 bool irq = false;
433 bool ioport = false;
434
435 for (i = 0; i < hw_resources.count; i++) {
436 res = &hw_resources.resources[i];
437 switch (res->type) {
438 case INTERRUPT:
439 ns->irq = res->res.interrupt.irq;
440 irq = true;
441 ddf_msg(LVL_NOTE, "Device %s was assigned irq = 0x%x.",
442 ddf_dev_get_name(ns->dev), ns->irq);
443 break;
444
445 case IO_RANGE:
446 ns->io_addr = res->res.io_range.address;
447 if (res->res.io_range.size < REG_COUNT) {
448 ddf_msg(LVL_ERROR, "I/O range assigned to "
449 "device %s is too small.", ddf_dev_get_name(ns->dev));
450 ret = ELIMIT;
451 goto failed;
452 }
453 ioport = true;
454 ddf_msg(LVL_NOTE, "Device %s was assigned I/O address = "
455 "0x%#" PRIxn ".", ddf_dev_get_name(ns->dev), ns->io_addr);
456 break;
457
458 default:
459 break;
460 }
461 }
462
463 if (!irq || !ioport) {
464 ddf_msg(LVL_ERROR, "Missing HW resource(s) for device %s.",
465 ddf_dev_get_name(ns->dev));
466 ret = ENOENT;
467 goto failed;
468 }
469
470 hw_res_clean_resource_list(&hw_resources);
471 return ret;
472
473failed:
474 ns8250_dev_cleanup(ns);
475 hw_res_clean_resource_list(&hw_resources);
476 return ret;
477}
478
479/** Enable interrupts on the serial port device.
480 *
481 * Interrupt when data is received
482 *
483 * @param port The base address of the serial port device's ports.
484 */
485static inline void ns8250_port_interrupts_enable(ns8250_regs_t *regs)
486{
487 /* Interrupt when data received. */
488 pio_write_8(&regs->ier, NS8250_IER_RXREADY | NS8250_IER_RXSTATUS);
489 pio_write_8(&regs->mcr, NS8250_MCR_DTR | NS8250_MCR_RTS |
490 NS8250_MCR_OUT2);
491}
492
493/** Disable interrupts on the serial port device.
494 *
495 * @param port The base address of the serial port device's ports
496 */
497static inline void ns8250_port_interrupts_disable(ns8250_regs_t *regs)
498{
499 pio_write_8(&regs->ier, 0x0); /* Disable all interrupts. */
500}
501
502/** Enable interrupts for the serial port device.
503 *
504 * @param ns Serial port device
505 * @return Zero on success, error number otherwise
506 */
507static errno_t ns8250_interrupt_enable(ns8250_t *ns)
508{
509 /* Enable interrupt using IRC service. */
510 errno_t rc = hw_res_enable_interrupt(ns->parent_sess, ns->irq);
511 if (rc != EOK)
512 return EIO;
513
514 /* Read LSR to clear possible previous LSR interrupt */
515 pio_read_8(&ns->regs->lsr);
516
517 /* Enable interrupt on the serial port. */
518 ns8250_port_interrupts_enable(ns->regs);
519
520 return EOK;
521}
522
523/** Set Divisor Latch Access Bit.
524 *
525 * When the Divisor Latch Access Bit is set, it is possible to set baud rate of
526 * the serial port device.
527 *
528 * @param port The base address of the serial port device's ports.
529 */
530static inline void enable_dlab(ns8250_regs_t *regs)
531{
532 uint8_t val = pio_read_8(&regs->lcr);
533 pio_write_8(&regs->lcr, val | NS8250_LCR_DLAB);
534}
535
536/** Clear Divisor Latch Access Bit.
537 *
538 * @param port The base address of the serial port device's ports.
539 */
540static inline void clear_dlab(ns8250_regs_t *regs)
541{
542 uint8_t val = pio_read_8(&regs->lcr);
543 pio_write_8(&regs->lcr, val & (~NS8250_LCR_DLAB));
544}
545
546/** Set baud rate of the serial communication on the serial device.
547 *
548 * @param port The base address of the serial port device's ports.
549 * @param baud_rate The baud rate to be used by the device.
550 * @return Zero on success, error number otherwise (EINVAL
551 * if the specified baud_rate is not valid).
552 */
553static errno_t ns8250_port_set_baud_rate(ns8250_regs_t *regs, unsigned int baud_rate)
554{
555 uint16_t divisor;
556 uint8_t div_low, div_high;
557
558 if (baud_rate < 50 || MAX_BAUD_RATE % baud_rate != 0) {
559 ddf_msg(LVL_ERROR, "Invalid baud rate %d requested.",
560 baud_rate);
561 return EINVAL;
562 }
563
564 divisor = MAX_BAUD_RATE / baud_rate;
565 div_low = (uint8_t)divisor;
566 div_high = (uint8_t)(divisor >> 8);
567
568 /* Enable DLAB to be able to access baud rate divisor. */
569 enable_dlab(regs);
570
571 /* Set divisor low byte. */
572 pio_write_8(&regs->data, div_low);
573 /* Set divisor high byte. */
574 pio_write_8(&regs->ier, div_high);
575
576 clear_dlab(regs);
577
578 return EOK;
579}
580
581/** Get baud rate used by the serial port device.
582 *
583 * @param port The base address of the serial port device's ports.
584 * @param baud_rate The ouput parameter to which the baud rate is stored.
585 */
586static unsigned int ns8250_port_get_baud_rate(ns8250_regs_t *regs)
587{
588 uint16_t divisor;
589 uint8_t div_low, div_high;
590
591 /* Enable DLAB to be able to access baud rate divisor. */
592 enable_dlab(regs);
593
594 /* Get divisor low byte. */
595 div_low = pio_read_8(&regs->data);
596 /* Get divisor high byte. */
597 div_high = pio_read_8(&regs->ier);
598
599 clear_dlab(regs);
600
601 divisor = (div_high << 8) | div_low;
602 return MAX_BAUD_RATE / divisor;
603}
604
605/** Get the parameters of the serial communication set on the serial port
606 * device.
607 *
608 * @param parity The parity used.
609 * @param word_length The length of one data unit in bits.
610 * @param stop_bits The number of stop bits used (one or two).
611 */
612static void ns8250_port_get_com_props(ns8250_regs_t *regs, unsigned int *parity,
613 unsigned int *word_length, unsigned int *stop_bits)
614{
615 uint8_t val;
616
617 val = pio_read_8(&regs->lcr);
618 *parity = ((val >> NS8250_LCR_PARITY) & 7);
619
620 /* Silence warnings */
621 *word_length = 0;
622
623 switch (val & 3) {
624 case WORD_LENGTH_5:
625 *word_length = 5;
626 break;
627 case WORD_LENGTH_6:
628 *word_length = 6;
629 break;
630 case WORD_LENGTH_7:
631 *word_length = 7;
632 break;
633 case WORD_LENGTH_8:
634 *word_length = 8;
635 break;
636 }
637
638 if ((val >> NS8250_LCR_STOPBITS) & 1)
639 *stop_bits = 2;
640 else
641 *stop_bits = 1;
642}
643
644/** Set the parameters of the serial communication on the serial port device.
645 *
646 * @param parity The parity to be used.
647 * @param word_length The length of one data unit in bits.
648 * @param stop_bits The number of stop bits used (one or two).
649 * @return Zero on success, EINVAL if some of the specified values
650 * is invalid.
651 */
652static errno_t ns8250_port_set_com_props(ns8250_regs_t *regs, unsigned int parity,
653 unsigned int word_length, unsigned int stop_bits)
654{
655 uint8_t val;
656
657 switch (word_length) {
658 case 5:
659 val = WORD_LENGTH_5;
660 break;
661 case 6:
662 val = WORD_LENGTH_6;
663 break;
664 case 7:
665 val = WORD_LENGTH_7;
666 break;
667 case 8:
668 val = WORD_LENGTH_8;
669 break;
670 default:
671 return EINVAL;
672 }
673
674 switch (stop_bits) {
675 case 1:
676 val |= ONE_STOP_BIT << NS8250_LCR_STOPBITS;
677 break;
678 case 2:
679 val |= TWO_STOP_BITS << NS8250_LCR_STOPBITS;
680 break;
681 default:
682 return EINVAL;
683 }
684
685 switch (parity) {
686 case SERIAL_NO_PARITY:
687 case SERIAL_ODD_PARITY:
688 case SERIAL_EVEN_PARITY:
689 case SERIAL_MARK_PARITY:
690 case SERIAL_SPACE_PARITY:
691 val |= parity << NS8250_LCR_PARITY;
692 break;
693 default:
694 return EINVAL;
695 }
696
697 pio_write_8(&regs->lcr, val);
698
699 return EOK;
700}
701
702/** Initialize the serial port device.
703 *
704 * Set the default parameters of the serial communication.
705 *
706 * @param ns Serial port device
707 */
708static void ns8250_initialize_port(ns8250_t *ns)
709{
710 /* Disable interrupts. */
711 ns8250_port_interrupts_disable(ns->regs);
712 /* Set baud rate. */
713 ns8250_port_set_baud_rate(ns->regs, 38400);
714 /* 8 bits, no parity, two stop bits. */
715 ns8250_port_set_com_props(ns->regs, SERIAL_NO_PARITY, 8, 2);
716 /*
717 * Enable FIFO, clear them, with 4-byte threshold for greater
718 * reliability.
719 */
720 pio_write_8(&ns->regs->iid, NS8250_FCR_FIFOENABLE |
721 NS8250_FCR_RXFIFORESET | NS8250_FCR_TXFIFORESET |
722 NS8250_FCR_RXTRIGGERLOW);
723 /*
724 * RTS/DSR set (Request to Send and Data Terminal Ready lines enabled),
725 * Aux Output2 set - needed for interrupts.
726 */
727 pio_write_8(&ns->regs->mcr, NS8250_MCR_DTR | NS8250_MCR_RTS |
728 NS8250_MCR_OUT2);
729}
730
731/** Deinitialize the serial port device.
732 *
733 * @param ns Serial port device
734 */
735static void ns8250_port_cleanup(ns8250_t *ns)
736{
737 /* Disable FIFO */
738 pio_write_8(&ns->regs->iid, 0x00);
739 /* Disable DTR, RTS, OUT1, OUT2 (int. enable) */
740 pio_write_8(&ns->regs->mcr, 0x00);
741 /* Disable all interrupts from the port */
742 ns8250_port_interrupts_disable(ns->regs);
743}
744
745/** Read the data from the serial port device and store them to the input
746 * buffer.
747 *
748 * @param ns Serial port device
749 */
750static void ns8250_read_from_device(ns8250_t *ns)
751{
752 ns8250_regs_t *regs = ns->regs;
753 bool cont = true;
754
755 fibril_mutex_lock(&ns->mutex);
756 while (cont) {
757 cont = ns8250_received(regs);
758 if (cont) {
759 uint8_t val = ns8250_read_8(regs);
760
761 if (ns->client_connections > 0) {
762 bool buf_was_empty = buf_is_empty(&ns->input_buffer);
763 if (!buf_push_back(&ns->input_buffer, val)) {
764 ddf_msg(LVL_WARN, "Buffer overflow on "
765 "%s.", ddf_dev_get_name(ns->dev));
766 break;
767 } else {
768 ddf_msg(LVL_DEBUG2, "Character %c saved "
769 "to the buffer of %s.",
770 val, ddf_dev_get_name(ns->dev));
771 if (buf_was_empty)
772 fibril_condvar_broadcast(&ns->input_buffer_available);
773 }
774 }
775 }
776 }
777 fibril_mutex_unlock(&ns->mutex);
778 fibril_yield();
779}
780
781/** The interrupt handler.
782 *
783 * The serial port is initialized to interrupt when some data come or line
784 * status register changes, so the interrupt is handled by reading the incoming
785 * data and reading the line status register.
786 *
787 * @pram icall IRQ event notificatoin
788 * @param arg Argument (ns8250_t *)
789 */
790static inline void ns8250_interrupt_handler(ipc_call_t *icall, void *arg)
791{
792 ns8250_t *ns = (ns8250_t *)arg;
793 uint8_t iir = pio_read_8(&ns->regs->iid);
794 if ((iir & NS8250_IID_CAUSE_MASK) == NS8250_IID_CAUSE_RXSTATUS) {
795 uint8_t lsr = pio_read_8(&ns->regs->lsr);
796 if (lsr & NS8250_LSR_OE) {
797 ddf_msg(LVL_WARN, "Overrun error on %s", ddf_dev_get_name(ns->dev));
798 }
799 }
800
801 ns8250_read_from_device(ns);
802 hw_res_clear_interrupt(ns->parent_sess, ns->irq);
803}
804
805/** Register the interrupt handler for the device.
806 *
807 * @param ns Serial port device
808 */
809static inline errno_t ns8250_register_interrupt_handler(ns8250_t *ns,
810 cap_irq_handle_t *ihandle)
811{
812 return register_interrupt_handler(ns->dev, ns->irq,
813 ns8250_interrupt_handler, (void *)ns, NULL, ihandle);
814}
815
816/** Unregister the interrupt handler for the device.
817 *
818 * @param ns Serial port device
819 */
820static inline errno_t ns8250_unregister_interrupt_handler(ns8250_t *ns)
821{
822 return unregister_interrupt_handler(ns->dev, ns->irq_handle);
823}
824
825/** The dev_add callback method of the serial port driver.
826 *
827 * Probe and initialize the newly added device.
828 *
829 * @param dev The serial port device.
830 */
831static errno_t ns8250_dev_add(ddf_dev_t *dev)
832{
833 ns8250_t *ns = NULL;
834 ddf_fun_t *fun = NULL;
835 bool need_cleanup = false;
836 bool need_unreg_intr_handler = false;
837 bool bound = false;
838 errno_t rc;
839
840 ddf_msg(LVL_DEBUG, "ns8250_dev_add %s (handle = %d)",
841 ddf_dev_get_name(dev), (int) ddf_dev_get_handle(dev));
842
843 /* Allocate soft-state for the device */
844 ns = ddf_dev_data_alloc(dev, sizeof(ns8250_t));
845 if (ns == NULL) {
846 rc = ENOMEM;
847 goto fail;
848 }
849
850 fibril_mutex_initialize(&ns->mutex);
851 fibril_condvar_initialize(&ns->input_buffer_available);
852 ns->dev = dev;
853
854 ns->parent_sess = ddf_dev_parent_sess_get(ns->dev);
855 if (ns->parent_sess == NULL) {
856 ddf_msg(LVL_ERROR, "Failed to connect to parent driver of "
857 "device %s.", ddf_dev_get_name(ns->dev));
858 rc = EIO;
859 goto fail;
860 }
861
862 rc = ns8250_dev_initialize(ns);
863 if (rc != EOK)
864 goto fail;
865
866 need_cleanup = true;
867
868 if (!ns8250_pio_enable(ns)) {
869 rc = EADDRNOTAVAIL;
870 goto fail;
871 }
872
873 /* Find out whether the device is present. */
874 if (!ns8250_dev_probe(ns)) {
875 rc = ENOENT;
876 goto fail;
877 }
878
879 /* Serial port initialization (baud rate etc.). */
880 ns8250_initialize_port(ns);
881
882 /* Register interrupt handler. */
883 ns->irq_handle = CAP_NIL;
884 rc = ns8250_register_interrupt_handler(ns, &ns->irq_handle);
885 if (rc != EOK) {
886 ddf_msg(LVL_ERROR, "Failed to register interrupt handler.");
887 rc = EADDRNOTAVAIL;
888 goto fail;
889 }
890 need_unreg_intr_handler = true;
891
892 /* Enable interrupt. */
893 rc = ns8250_interrupt_enable(ns);
894 if (rc != EOK) {
895 ddf_msg(LVL_ERROR, "Failed to enable the interrupt. Error code = "
896 "%s.", str_error_name(rc));
897 goto fail;
898 }
899
900 fun = ddf_fun_create(dev, fun_exposed, "a");
901 if (fun == NULL) {
902 ddf_msg(LVL_ERROR, "Failed creating function.");
903 goto fail;
904 }
905
906 ddf_fun_set_conn_handler(fun, ns8250_char_conn);
907
908 chardev_srvs_init(&ns->cds);
909 ns->cds.ops = &ns8250_chardev_ops;
910 ns->cds.sarg = ns;
911
912 rc = ddf_fun_bind(fun);
913 if (rc != EOK) {
914 ddf_msg(LVL_ERROR, "Failed binding function.");
915 goto fail;
916 }
917
918 bound = true;
919 ns->fun = fun;
920
921 rc = ddf_fun_add_to_category(fun, "serial");
922 if (rc != EOK) {
923 ddf_msg(LVL_ERROR, "Error adding function to category 'serial'.");
924 goto fail;
925 }
926
927 ddf_msg(LVL_NOTE, "Device %s successfully initialized.",
928 ddf_dev_get_name(dev));
929
930 return EOK;
931fail:
932 if (bound)
933 ddf_fun_unbind(fun);
934 if (fun != NULL)
935 ddf_fun_destroy(fun);
936 if (need_unreg_intr_handler)
937 ns8250_unregister_interrupt_handler(ns);
938 if (need_cleanup)
939 ns8250_dev_cleanup(ns);
940 return rc;
941}
942
943static errno_t ns8250_dev_remove(ddf_dev_t *dev)
944{
945 ns8250_t *ns = dev_ns8250(dev);
946 errno_t rc;
947
948 fibril_mutex_lock(&ns->mutex);
949 if (ns->client_connections > 0) {
950 fibril_mutex_unlock(&ns->mutex);
951 return EBUSY;
952 }
953 ns->removed = true;
954 fibril_mutex_unlock(&ns->mutex);
955
956 rc = ddf_fun_unbind(ns->fun);
957 if (rc != EOK) {
958 ddf_msg(LVL_ERROR, "Failed to unbind function.");
959 return rc;
960 }
961
962 ddf_fun_destroy(ns->fun);
963
964 ns8250_port_cleanup(ns);
965 ns8250_unregister_interrupt_handler(ns);
966 ns8250_dev_cleanup(ns);
967 return EOK;
968}
969
970static errno_t ns8250_dev_quiesce(ddf_dev_t *dev)
971{
972 ns8250_t *ns = dev_ns8250(dev);
973
974 ns8250_port_interrupts_disable(ns->regs);
975 return EOK;
976}
977
978/** Open the device.
979 *
980 * This is a callback function called when a client tries to connect to the
981 * device.
982 *
983 * @param srvs Service structure
984 * @param srv Server-side connection structure
985 */
986static errno_t ns8250_open(chardev_srvs_t *srvs, chardev_srv_t *srv)
987{
988 ns8250_t *ns = srv_ns8250(srv);
989 errno_t res;
990
991 fibril_mutex_lock(&ns->mutex);
992 if (ns->removed) {
993 res = ENXIO;
994 } else {
995 res = EOK;
996 ns->client_connections++;
997 }
998 fibril_mutex_unlock(&ns->mutex);
999
1000 return res;
1001}
1002
1003/** Close the device.
1004 *
1005 * This is a callback function called when a client tries to disconnect from
1006 * the device.
1007 *
1008 * @param srv Server-side connection structure
1009 */
1010static errno_t ns8250_close(chardev_srv_t *srv)
1011{
1012 ns8250_t *data = srv_ns8250(srv);
1013
1014 fibril_mutex_lock(&data->mutex);
1015
1016 assert(data->client_connections > 0);
1017
1018 if (!(--data->client_connections))
1019 buf_clear(&data->input_buffer);
1020
1021 fibril_mutex_unlock(&data->mutex);
1022
1023 return EOK;
1024}
1025
1026/** Get parameters of the serial communication which are set to the specified
1027 * device.
1028 *
1029 * @param dev The serial port device.
1030 * @param baud_rate The baud rate used by the device.
1031 * @param parity The type of parity used by the device.
1032 * @param word_length The size of one data unit in bits.
1033 * @param stop_bits The number of stop bits used.
1034 */
1035static void ns8250_get_props(ddf_dev_t *dev, unsigned int *baud_rate,
1036 unsigned int *parity, unsigned int *word_length, unsigned int *stop_bits)
1037{
1038 ns8250_t *data = dev_ns8250(dev);
1039 ns8250_regs_t *regs = data->regs;
1040
1041 fibril_mutex_lock(&data->mutex);
1042 ns8250_port_interrupts_disable(regs);
1043 *baud_rate = ns8250_port_get_baud_rate(regs);
1044 ns8250_port_get_com_props(regs, parity, word_length, stop_bits);
1045 ns8250_port_interrupts_enable(regs);
1046 fibril_mutex_unlock(&data->mutex);
1047
1048 ddf_msg(LVL_DEBUG, "ns8250_get_props: baud rate %d, parity 0x%x, word "
1049 "length %d, stop bits %d", *baud_rate, *parity, *word_length,
1050 *stop_bits);
1051}
1052
1053/** Set parameters of the serial communication to the specified serial port
1054 * device.
1055 *
1056 * @param dev The serial port device.
1057 * @param baud_rate The baud rate to be used by the device.
1058 * @param parity The type of parity to be used by the device.
1059 * @param word_length The size of one data unit in bits.
1060 * @param stop_bits The number of stop bits to be used.
1061 */
1062static errno_t ns8250_set_props(ddf_dev_t *dev, unsigned int baud_rate,
1063 unsigned int parity, unsigned int word_length, unsigned int stop_bits)
1064{
1065 ddf_msg(LVL_DEBUG, "ns8250_set_props: baud rate %d, parity 0x%x, word "
1066 "length %d, stop bits %d", baud_rate, parity, word_length,
1067 stop_bits);
1068
1069 ns8250_t *data = dev_ns8250(dev);
1070 ns8250_regs_t *regs = data->regs;
1071 errno_t ret;
1072
1073 fibril_mutex_lock(&data->mutex);
1074 ns8250_port_interrupts_disable(regs);
1075 ret = ns8250_port_set_baud_rate(regs, baud_rate);
1076 if (ret == EOK)
1077 ret = ns8250_port_set_com_props(regs, parity, word_length, stop_bits);
1078 ns8250_port_interrupts_enable(regs);
1079 fibril_mutex_unlock(&data->mutex);
1080
1081 return ret;
1082}
1083
1084/** Default handler for client requests which are not handled by the standard
1085 * interfaces.
1086 *
1087 * Configure the parameters of the serial communication.
1088 */
1089static void ns8250_default_handler(chardev_srv_t *srv, ipc_call_t *call)
1090{
1091 ns8250_t *ns8250 = srv_ns8250(srv);
1092 sysarg_t method = ipc_get_imethod(call);
1093 errno_t ret;
1094 unsigned int baud_rate, parity, word_length, stop_bits;
1095
1096 switch (method) {
1097 case SERIAL_GET_COM_PROPS:
1098 ns8250_get_props(ns8250->dev, &baud_rate, &parity, &word_length,
1099 &stop_bits);
1100 async_answer_4(call, EOK, baud_rate, parity, word_length,
1101 stop_bits);
1102 break;
1103
1104 case SERIAL_SET_COM_PROPS:
1105 baud_rate = ipc_get_arg1(call);
1106 parity = ipc_get_arg2(call);
1107 word_length = ipc_get_arg3(call);
1108 stop_bits = ipc_get_arg4(call);
1109 ret = ns8250_set_props(ns8250->dev, baud_rate, parity, word_length,
1110 stop_bits);
1111 async_answer_0(call, ret);
1112 break;
1113
1114 default:
1115 async_answer_0(call, ENOTSUP);
1116 }
1117}
1118
1119void ns8250_char_conn(ipc_call_t *icall, void *arg)
1120{
1121 ns8250_t *ns8250 = fun_ns8250((ddf_fun_t *)arg);
1122
1123 chardev_conn(icall, &ns8250->cds);
1124}
1125
1126/** Initialize the serial port driver.
1127 *
1128 * Initialize device operations structures with callback methods for handling
1129 * client requests to the serial port devices.
1130 */
1131static void ns8250_init(void)
1132{
1133 ddf_log_init(NAME);
1134}
1135
1136int main(int argc, char *argv[])
1137{
1138 printf(NAME ": HelenOS serial port driver\n");
1139 ns8250_init();
1140 return ddf_driver_main(&ns8250_driver);
1141}
1142
1143/**
1144 * @}
1145 */
Note: See TracBrowser for help on using the repository browser.