source: mainline/uspace/drv/char/ns8250/ns8250.c@ 0f79283b

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

Let leaf drivers enable/disable/clear interrupts via hw_res instead of directly using irc.

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