OpenCores
URL https://opencores.org/ocsvn/usb_dongle_fpga/usb_dongle_fpga/trunk

Subversion Repositories usb_dongle_fpga

[/] [usb_dongle_fpga/] [trunk/] [src/] [lpc_proto/] [lpc_byte.vhd] - Blame information for rev 17

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 nuubik
------------------------------------------------------------------
2
-- Universal dongle board source code
3
-- 
4
-- Copyright (C) 2006 Artec Design <jyrit@artecdesign.ee>
5
-- 
6
-- This source code is free hardware; you can redistribute it and/or
7
-- modify it under the terms of the GNU Lesser General Public
8
-- License as published by the Free Software Foundation; either
9
-- version 2.1 of the License, or (at your option) any later version.
10
-- 
11
-- This source code is distributed in the hope that it will be useful,
12
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
-- Lesser General Public License for more details.
15
-- 
16
-- You should have received a copy of the GNU Lesser General Public
17
-- License along with this library; if not, write to the Free Software
18
-- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
-- 
20
-- 
21
-- The complete text of the GNU Lesser General Public License can be found in 
22
-- the file 'lesser.txt'.
23
 
24
 
25
library ieee;
26
use ieee.std_logic_1164.all;
27
use IEEE.std_logic_unsigned.all;
28
use IEEE.std_logic_arith.all;
29
 
30
 
31
entity lpc_iow is
32
  port (
33
     --system signals
34
    lreset_n   : in  std_logic;
35
    lclk       : in  std_logic;
36 17 nuubik
    lena_mem_r : in  std_logic;  --enable lpc regular memory read cycles also (default is only LPC firmware read)
37
        lena_reads : in  std_logic;  --enable read capabilities
38 2 nuubik
        --LPC bus from host
39
    lad_i      : in  std_logic_vector(3 downto 0);
40
    lad_o      : out std_logic_vector(3 downto 0);
41
    lad_oe     : out std_logic;
42
    lframe_n   : in  std_logic;
43
        --memory interface
44
    lpc_addr   : out std_logic_vector(23 downto 0); --shared address
45
    lpc_wr     : out std_logic;         --shared write not read
46
    lpc_data_i : in  std_logic_vector(7 downto 0);
47
    lpc_data_o : out std_logic_vector(7 downto 0);
48
    lpc_val    : out std_logic;
49
    lpc_ack    : in  std_logic
50
    );
51
end lpc_iow;
52
 
53
architecture rtl of lpc_iow is
54
type state is (RESETs,STARTs,ADDRs,TARs,SYNCs,DATAs,LOCAL_TARs);  -- simple LCP states
55 17 nuubik
type cycle is (LPC_IO_W,LPC_MEM_R,LPC_FW_R);  -- simple LPC bus cycle types
56
 
57 2 nuubik
signal CS : state;
58
signal r_lad   : std_logic_vector(3 downto 0);
59
signal r_addr  : std_logic_vector(31 downto 0);  --should consider saving max
60
                                                --adress 23 bits on flash
61
signal r_data  : std_logic_vector(7 downto 0);
62
signal r_cnt   : std_logic_vector(2 downto 0);
63 17 nuubik
signal cycle_type : cycle;
64
--signal r_fw_msize   : std_logic_vector(3 downto 0);
65
 
66
 
67 2 nuubik
signal data_valid : std_logic;
68 17 nuubik
 
69
signal lad_rising_o : std_logic_vector(3 downto 0);
70
signal lad_rising_oe : std_logic;
71
 
72
constant START_FW_READ : std_logic_vector(3 downto 0):="1101";
73
constant START_LPC     : std_logic_vector(3 downto 0):="0000";
74
constant IDSEL_FW_BOOT : std_logic_vector(3 downto 0):="0000";  --0000 is boot device on ThinCan
75
constant MSIZE_FW_1B   : std_logic_vector(3 downto 0):="0000";  --0000 is 1 byte read
76
constant SYNC_OK       : std_logic_vector(3 downto 0):="0000";  --sync done
77
constant SYNC_WAIT     : std_logic_vector(3 downto 0):="0101";  --sync wait device holds the bus
78
constant SYNC_LWAIT    : std_logic_vector(3 downto 0):="0110";  --sync long wait expected device holds the bus
79
constant TAR_OK            : std_logic_vector(3 downto 0):="1111";  --accepted tar constant for master and slave
80
 
81
 
82
 
83
 
84 2 nuubik
begin  -- rtl
85
 
86 17 nuubik
lad_o<= lad_rising_o;
87
lad_oe <= lad_rising_oe;
88
 
89
 
90 2 nuubik
 
91
--Pass the whole LPC address to the system
92
lpc_addr <= r_addr(23 downto 0);
93
lpc_data_o<= r_data;
94
 
95
 
96 17 nuubik
 
97 2 nuubik
 
98 17 nuubik
-- purpose: LPC IO write/LPC MEM read/LPC FW read  handler
99 2 nuubik
-- type   : sequential
100
-- inputs : lclk, lreset_n
101
-- outputs: 
102
LPC: process (lclk, lreset_n)
103
begin  -- process LPC
104
  if lreset_n = '0' then                -- asynchronous reset (active low)
105
    CS<= RESETs;
106 17 nuubik
    lad_rising_oe<='0';
107 2 nuubik
    data_valid <='1';
108 17 nuubik
    lad_rising_o<="0000";
109 2 nuubik
    lpc_val <='0';
110 17 nuubik
        r_addr <= (others=>'0');
111 2 nuubik
   elsif lclk'event and lclk = '1' then  -- rising clock edge
112
    case CS is
113
      when RESETs => ----------------------------------------------------------
114
        lpc_wr <='0';
115
        lpc_val <='0';
116
        if lframe_n='0' then
117
          CS <= STARTs;
118
          r_lad <= lad_i;
119
        else
120
          CS <= RESETs;
121
        end if;
122
      when STARTs => ----------------------------------------------------------
123
        if lframe_n = '0' then
124 17 nuubik
                r_lad <= lad_i; -- latch lad state for next cycle
125
                CS <= STARTs;
126
        elsif r_lad = START_LPC then
127
              --must identify CYCTYPE
128
                  if lad_i(3 downto 1)="001" then --IO WRITE WILL HAPPEN
129
                    --next 4 states must be address states
130
                    CS<=ADDRs;
131
                                cycle_type <= LPC_IO_W;
132
                    r_cnt <= "000";
133
                  elsif lad_i(3 downto 1)="010"  and lena_mem_r='1' and lena_reads='1' then --MEM READ ALLOWED
134
                    CS<=ADDRs;
135
                                cycle_type <= LPC_MEM_R;
136
                    r_cnt <= "000";
137
                  else
138
                    CS<= RESETs;
139
                  end if;
140
        elsif r_lad = START_FW_READ then    --FW READ is always allowed
141
                        if lad_i = IDSEL_FW_BOOT and lena_reads='1'  then
142
                    CS<=ADDRs;
143
                                cycle_type <= LPC_FW_R;
144
                    r_cnt <= "000";
145
                        else
146
                                CS<= RESETs;
147
                    end if;
148 2 nuubik
        end if;
149
      when ADDRs => -----------------------------------------------------------
150 17 nuubik
       case cycle_type is
151
         when LPC_IO_W =>                   --IO write cycle
152 2 nuubik
          if r_cnt ="011" then
153
             if r_addr(11 downto 0)=x"008" and lad_i(3 downto 2)="00" then
154
              r_addr<= r_addr(27 downto 0)&lad_i;
155
              r_cnt <= "000";
156
              CS<=DATAs;
157
            elsif r_addr(11 downto 0)=x"008" and lad_i(3 downto 0)=x"8" then  --for debug switch
158
              r_addr<= r_addr(27 downto 0)&lad_i;
159
              r_cnt <= "000";
160
              CS<=DATAs;
161
            else
162
              --not for this device
163
               CS<=RESETs;
164
            end if;
165
          else
166
            r_addr<= r_addr(27 downto 0)&lad_i;
167 17 nuubik
            r_cnt<=r_cnt + 1;
168 2 nuubik
            CS<=ADDRs;
169
          end if;
170 17 nuubik
        when LPC_MEM_R =>                    --Memory read cycle
171 2 nuubik
          if r_cnt ="111" then
172
              r_addr<= r_addr(27 downto 0)&lad_i;
173
              r_cnt <= "000";
174
              lpc_wr <='0';             --memory read mus accure
175
              lpc_val <='1';
176
              data_valid <='0';
177
              CS<=TARs;
178
          else
179
            r_addr<= r_addr(27 downto 0)&lad_i;
180 17 nuubik
            r_cnt<=r_cnt + 1;
181 2 nuubik
            CS<=ADDRs;
182
          end if;
183 17 nuubik
                 when LPC_FW_R =>                    --Firmware read
184
          if r_cnt ="111" then
185
              --r_fw_msize <= lad_i; --8'th cycle on FW read is mem size
186
              r_cnt <= "000";
187
              lpc_wr <='0';             --memory read must accure
188
              lpc_val <='1';
189
              data_valid <='0';
190
                          if lad_i = MSIZE_FW_1B then
191
                                 CS<=TARs;
192
                          else
193
                     --over byte fw read not supported
194
                 CS<=RESETs;
195
                          end if;
196
          else
197
            r_addr<= r_addr(27 downto 0)&lad_i;  --28 bit address is given
198
            r_cnt<=r_cnt + 1;
199
            CS<=ADDRs;
200
          end if;
201
 
202 2 nuubik
         when others => null;
203 17 nuubik
        end case;
204 2 nuubik
      when DATAs => -----------------------------------------------------------
205 17 nuubik
       case cycle_type is
206
        when LPC_IO_W =>                   --IO write cycle              
207 2 nuubik
          if r_cnt ="001" then
208
            r_data <= r_data(3 downto 0)&lad_i;
209
            r_cnt <= "000";
210
            lpc_wr <='1';             --IO write must accure
211
            lpc_val <='1';
212
            CS <= TARs;
213
          else
214
            r_data <= r_data(3 downto 0)&lad_i;
215 17 nuubik
            r_cnt<=r_cnt + 1;
216 2 nuubik
            CS <= DATAs;
217
          end if;
218 17 nuubik
        when LPC_MEM_R | LPC_FW_R =>                    --Memory/FW read cycle
219 2 nuubik
          if r_cnt ="001" then
220 17 nuubik
            lad_rising_o<= r_data(7 downto 4);
221 2 nuubik
            r_cnt <= "000";
222
            CS <= LOCAL_TARs;
223
          else
224 17 nuubik
            lad_rising_o<= r_data(3 downto 0);
225
            r_cnt<=r_cnt + 1;
226 2 nuubik
            CS <= DATAs;
227
          end if;
228 17 nuubik
       when others => null;
229 2 nuubik
       end case;
230
      when TARs => ------------------------------------------------------------
231 17 nuubik
          if cycle_type /= LPC_IO_W and lpc_ack='1' and r_cnt ="001" then --if mem_read or fr_read
232 2 nuubik
            r_data <= lpc_data_i;
233
            lpc_val <='0';
234
            data_valid <='1';
235
                        CS<= SYNCs;
236
                        r_cnt <= "000";
237
                  elsif lpc_ack='1' and r_cnt ="001" then
238 17 nuubik
                    lad_rising_o<=SYNC_OK;              --added to avoid trouble as SYNC is OK allready
239 2 nuubik
                        lpc_val <='0';
240
                        CS<= SYNCs;
241
                        r_cnt <= "000";
242
          end if;
243
 
244
          if r_cnt ="001" then
245
                          if lpc_ack='0' then
246 17 nuubik
                                lad_rising_o <= SYNC_LWAIT;              --added to avoid trouble                               
247 2 nuubik
                          end if;
248 17 nuubik
            lad_rising_oe<='1';
249
          elsif lad_i = TAR_OK then
250
            r_cnt<=r_cnt + 1;
251
            lad_rising_oe<='1';
252
            lad_rising_o <= TAR_OK;              --drive to F on the bus
253 2 nuubik
            CS <= TARs;
254
          else
255
            CS <= RESETs; --some error in protocol master must drive lad to "1111" on 1st TAR
256
          end if;
257
      when SYNCs => -----------------------------------------------------------
258 17 nuubik
       case cycle_type is
259
        when LPC_IO_W =>                   --IO write cycle   
260 2 nuubik
          -- just passing r_lad on bus again
261 17 nuubik
          lad_rising_o<= TAR_OK;
262 2 nuubik
          CS <= LOCAL_TARs;
263 17 nuubik
        when LPC_MEM_R | LPC_FW_R =>                    --Memory/FW read cycle
264 2 nuubik
          if data_valid ='1' then
265 17 nuubik
            lad_rising_o<=SYNC_OK;
266 2 nuubik
            CS <= DATAs;
267
          else
268
            if lpc_ack='1' then
269
              r_data <= lpc_data_i;
270
              data_valid <= '1';
271 17 nuubik
              lad_rising_o<=SYNC_OK;           --SYNC ok now                            
272 2 nuubik
              lpc_val <='0';
273
              CS <= DATAs;
274
            end if;
275
          end if;
276
         when others => null;
277
        end case;
278
      when LOCAL_TARs => ------------------------------------------------------
279 17 nuubik
       case cycle_type is
280
        when LPC_IO_W =>                   --IO write cycle   
281 2 nuubik
            lpc_wr <='0';
282 17 nuubik
            lad_rising_oe <='0';
283 2 nuubik
            CS <= RESETs;
284 17 nuubik
        when LPC_MEM_R | LPC_FW_R =>                    --Memory read cycle
285 2 nuubik
          if r_cnt ="000" then
286 17 nuubik
            lad_rising_o<= TAR_OK;
287
            r_cnt <= r_cnt + 1;
288 2 nuubik
          else
289 17 nuubik
            lad_rising_oe <= '0';
290 2 nuubik
            r_cnt <="000";
291
            CS <= RESETs;
292
          end if;
293
        when others => null;
294
       end case;
295
    end case; -----------------------------------------------------------------
296
  end if;
297
end process LPC;
298
 
299
end rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.