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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7813516 was 956d4281, checked in by Maurizio Lombardi <m.lombardi85@…>, 12 years ago

ns8250: fix cstyle and typos

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