source: mainline/uspace/drv/char/ns8250/ns8250.c@ 9bd4615

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9bd4615 was ebc9c2c, checked in by Jiri Svoboda <jiri@…>, 11 years ago

Other clients should also use the IRC client stub.

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