Contiki 3.x
msg-bus.c
1 /*
2  * Copyright (C) 2015-2016, Intel Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "msg-bus.h"
32 #include "pci.h"
33 #include "syscalls.h"
34 
35 PROT_DOMAINS_ALLOC(dom_client_data_t, quarkX1000_msg_bus);
36 
37 /** Message bus control register */
38 #define MCR_PCI_REG_ADDR 0xD0
39 /** Message data register */
40 #define MDR_PCI_REG_ADDR 0xD4
41 /** Message control register extension */
42 #define MCRX_PCI_REG_ADDR 0xD8
43 
44 typedef union mcr {
45  struct {
46  uint32_t : 4;
47  uint32_t byte_en : 4;
48  uint32_t reg_off : 8;
49  uint32_t port : 8;
50  uint32_t opcode : 8;
51  };
52  uint32_t raw;
53 } mcr_t;
54 
55 typedef union mcrx {
56  struct {
57  uint32_t : 8;
58  uint32_t reg_off : 24;
59  };
60  uint32_t raw;
61 } mcrx_t;
62 
63 /*---------------------------------------------------------------------------*/
64 static void
65 request_op(uint8_t port, uint32_t reg_off, uint8_t opcode)
66 {
67  pci_config_addr_t pci_addr = { .raw = 0 };
68  mcr_t mcr = { .raw = 0 };
69  mcrx_t mcrx = { .raw = 0 };
70 
71  pci_addr.reg_off = MCR_PCI_REG_ADDR;
72  mcr.opcode = opcode;
73  mcr.byte_en = 0xF;
74  mcr.port = port;
75  mcr.reg_off = reg_off & 0xFF;
76  pci_config_write(pci_addr, mcr.raw);
77 
78  pci_addr.reg_off = MCRX_PCI_REG_ADDR;
79  mcrx.reg_off = reg_off >> 8;
80  pci_config_write(pci_addr, mcrx.raw);
81 }
82 /*---------------------------------------------------------------------------*/
83 /**
84  * \brief Read from a message bus register.
85  * \param port Port of message bus register to be read.
86  * \param reg_off Register/offset identifier of message bus register to read.
87  * \param val Storage location for value that has been read.
88  */
89 SYSCALLS_DEFINE_SINGLETON(quarkX1000_msg_bus_read,
90  quarkX1000_msg_bus,
91  uint8_t port,
92  uint32_t reg_off,
93  uint32_t *val)
94 {
95  uint32_t *loc_val;
96  pci_config_addr_t pci_addr = { .raw = 0 };
97 
98  PROT_DOMAINS_VALIDATE_PTR(loc_val, val, sizeof(*val));
99 
100  request_op(port, reg_off, 0x10);
101 
102  pci_addr.reg_off = MDR_PCI_REG_ADDR;
103  *loc_val = pci_config_read(pci_addr);
104 }
105 /*---------------------------------------------------------------------------*/
106 /**
107  * \brief Write to a message bus register.
108  * \param port Port of message bus register to be written.
109  * \param reg_off Register/offset identifier of message bus register to write.
110  * \param val Value to write.
111  */
112 SYSCALLS_DEFINE_SINGLETON(quarkX1000_msg_bus_write,
113  quarkX1000_msg_bus,
114  uint8_t port,
115  uint32_t reg_off,
116  uint32_t val)
117 {
118  pci_config_addr_t pci_addr = { .raw = 0 };
119 
120  pci_addr.reg_off = MDR_PCI_REG_ADDR;
121  pci_config_write(pci_addr, val);
122 
123  request_op(port, reg_off, 0x11);
124 }
125 /*---------------------------------------------------------------------------*/
126 void
127 quarkX1000_msg_bus_init(void)
128 {
129  PROT_DOMAINS_INIT_ID(quarkX1000_msg_bus);
130  prot_domains_reg(&quarkX1000_msg_bus, 0, 0, 0, 0, true);
131  SYSCALLS_INIT(quarkX1000_msg_bus_read);
132  SYSCALLS_AUTHZ(quarkX1000_msg_bus_read, quarkX1000_msg_bus);
133  SYSCALLS_INIT(quarkX1000_msg_bus_write);
134  SYSCALLS_AUTHZ(quarkX1000_msg_bus_write, quarkX1000_msg_bus);
135 }
136 /*---------------------------------------------------------------------------*/
137 void
138 quarkX1000_msg_bus_lock(void)
139 {
140  SYSCALLS_DEAUTHZ(quarkX1000_msg_bus_read, quarkX1000_msg_bus);
141  SYSCALLS_DEAUTHZ(quarkX1000_msg_bus_write, quarkX1000_msg_bus);
142 }
143 /*---------------------------------------------------------------------------*/
PCI configuration address.
Definition: pci.h:85
Data associated with each protection domain that is owned by clients of that domain and used to ident...
Definition: prot-domains.h:247
uint32_t reg_off
Register/offset number.
Definition: pci.h:88