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 53

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 35 nuubik
        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 18 nuubik
        lpc_wr <='0';
111
        r_lad <= (others=>'0');
112 19 nuubik
        cycle_type <= LPC_IO_W; --initial value 
113 17 nuubik
        r_addr <= (others=>'0');
114 18 nuubik
        r_cnt <= (others=>'0');
115 2 nuubik
   elsif lclk'event and lclk = '1' then  -- rising clock edge
116
    case CS is
117
      when RESETs => ----------------------------------------------------------
118
        lpc_wr <='0';
119
        lpc_val <='0';
120
        if lframe_n='0' then
121
          CS <= STARTs;
122
          r_lad <= lad_i;
123
        else
124
          CS <= RESETs;
125
        end if;
126
      when STARTs => ----------------------------------------------------------
127
        if lframe_n = '0' then
128 17 nuubik
                r_lad <= lad_i; -- latch lad state for next cycle
129
                CS <= STARTs;
130
        elsif r_lad = START_LPC then
131
              --must identify CYCTYPE
132
                  if lad_i(3 downto 1)="001" then --IO WRITE WILL HAPPEN
133
                    --next 4 states must be address states
134
                    CS<=ADDRs;
135
                                cycle_type <= LPC_IO_W;
136
                    r_cnt <= "000";
137
                  elsif lad_i(3 downto 1)="010"  and lena_mem_r='1' and lena_reads='1' then --MEM READ ALLOWED
138
                    CS<=ADDRs;
139
                                cycle_type <= LPC_MEM_R;
140
                    r_cnt <= "000";
141
                  else
142
                    CS<= RESETs;
143
                  end if;
144
        elsif r_lad = START_FW_READ then    --FW READ is always allowed
145
                        if lad_i = IDSEL_FW_BOOT and lena_reads='1'  then
146
                    CS<=ADDRs;
147
                                cycle_type <= LPC_FW_R;
148
                    r_cnt <= "000";
149
                        else
150
                                CS<= RESETs;
151
                    end if;
152 2 nuubik
        end if;
153
      when ADDRs => -----------------------------------------------------------
154 17 nuubik
       case cycle_type is
155
         when LPC_IO_W =>                   --IO write cycle
156 2 nuubik
          if r_cnt ="011" then
157
             if r_addr(11 downto 0)=x"008" and lad_i(3 downto 2)="00" then
158
              r_addr<= r_addr(27 downto 0)&lad_i;
159
              r_cnt <= "000";
160
              CS<=DATAs;
161
            elsif r_addr(11 downto 0)=x"008" and lad_i(3 downto 0)=x"8" then  --for debug switch
162
              r_addr<= r_addr(27 downto 0)&lad_i;
163
              r_cnt <= "000";
164
              CS<=DATAs;
165
            else
166
              --not for this device
167
               CS<=RESETs;
168
            end if;
169
          else
170
            r_addr<= r_addr(27 downto 0)&lad_i;
171 17 nuubik
            r_cnt<=r_cnt + 1;
172 2 nuubik
            CS<=ADDRs;
173
          end if;
174 17 nuubik
        when LPC_MEM_R =>                    --Memory read cycle
175 2 nuubik
          if r_cnt ="111" then
176
              r_addr<= r_addr(27 downto 0)&lad_i;
177
              r_cnt <= "000";
178
              lpc_wr <='0';             --memory read mus accure
179
              lpc_val <='1';
180
              data_valid <='0';
181
              CS<=TARs;
182
          else
183
            r_addr<= r_addr(27 downto 0)&lad_i;
184 17 nuubik
            r_cnt<=r_cnt + 1;
185 2 nuubik
            CS<=ADDRs;
186
          end if;
187 17 nuubik
                 when LPC_FW_R =>                    --Firmware read
188
          if r_cnt ="111" then
189
              --r_fw_msize <= lad_i; --8'th cycle on FW read is mem size
190
              r_cnt <= "000";
191
              lpc_wr <='0';             --memory read must accure
192
              lpc_val <='1';
193
              data_valid <='0';
194
                          if lad_i = MSIZE_FW_1B then
195
                                 CS<=TARs;
196
                          else
197
                     --over byte fw read not supported
198
                 CS<=RESETs;
199
                          end if;
200
          else
201
            r_addr<= r_addr(27 downto 0)&lad_i;  --28 bit address is given
202
            r_cnt<=r_cnt + 1;
203
            CS<=ADDRs;
204
          end if;
205
 
206 2 nuubik
         when others => null;
207 17 nuubik
        end case;
208 2 nuubik
      when DATAs => -----------------------------------------------------------
209 17 nuubik
       case cycle_type is
210 35 nuubik
        when LPC_IO_W =>              --IO write cycle              
211 2 nuubik
          if r_cnt ="001" then
212 35 nuubik
            r_data <= lad_i&r_data(7 downto 4); --LSB first from io cycle
213 2 nuubik
            r_cnt <= "000";
214
            lpc_wr <='1';             --IO write must accure
215
            lpc_val <='1';
216
            CS <= TARs;
217
          else
218 35 nuubik
            r_data <= lad_i&r_data(7 downto 4); --LSB first from io cycle
219 17 nuubik
            r_cnt<=r_cnt + 1;
220 2 nuubik
            CS <= DATAs;
221
          end if;
222 17 nuubik
        when LPC_MEM_R | LPC_FW_R =>                    --Memory/FW read cycle
223 2 nuubik
          if r_cnt ="001" then
224 17 nuubik
            lad_rising_o<= r_data(7 downto 4);
225 2 nuubik
            r_cnt <= "000";
226
            CS <= LOCAL_TARs;
227
          else
228 17 nuubik
            lad_rising_o<= r_data(3 downto 0);
229
            r_cnt<=r_cnt + 1;
230 2 nuubik
            CS <= DATAs;
231
          end if;
232 17 nuubik
       when others => null;
233 2 nuubik
       end case;
234
      when TARs => ------------------------------------------------------------
235 20 nuubik
        if cycle_type /= LPC_IO_W and lpc_ack='1' and r_cnt ="001" then --if mem_read or fr_read
236 2 nuubik
            r_data <= lpc_data_i;
237
            lpc_val <='0';
238
            data_valid <='1';
239
                        CS<= SYNCs;
240
                        r_cnt <= "000";
241
                  elsif lpc_ack='1' and r_cnt ="001" then
242 17 nuubik
                    lad_rising_o<=SYNC_OK;              --added to avoid trouble as SYNC is OK allready
243 2 nuubik
                        lpc_val <='0';
244
                        CS<= SYNCs;
245
                        r_cnt <= "000";
246
          end if;
247
 
248
          if r_cnt ="001" then
249
                          if lpc_ack='0' then
250 17 nuubik
                                lad_rising_o <= SYNC_LWAIT;              --added to avoid trouble                               
251 2 nuubik
                          end if;
252 20 nuubik
           lad_rising_oe<='1';
253 17 nuubik
          elsif lad_i = TAR_OK then
254
            r_cnt<=r_cnt + 1;
255 20 nuubik
            --lad_rising_oe<='1'; --BUG fix by LPC stanard TAR cycle part 2 must be tri-stated by host and device
256 17 nuubik
            lad_rising_o <= TAR_OK;              --drive to F on the bus
257 2 nuubik
            CS <= TARs;
258
          else
259
            CS <= RESETs; --some error in protocol master must drive lad to "1111" on 1st TAR
260
          end if;
261
      when SYNCs => -----------------------------------------------------------
262 17 nuubik
       case cycle_type is
263
        when LPC_IO_W =>                   --IO write cycle   
264 2 nuubik
          -- just passing r_lad on bus again
265 17 nuubik
          lad_rising_o<= TAR_OK;
266 2 nuubik
          CS <= LOCAL_TARs;
267 17 nuubik
        when LPC_MEM_R | LPC_FW_R =>                    --Memory/FW read cycle
268 2 nuubik
          if data_valid ='1' then
269 17 nuubik
            lad_rising_o<=SYNC_OK;
270 2 nuubik
            CS <= DATAs;
271
          else
272
            if lpc_ack='1' then
273
              r_data <= lpc_data_i;
274
              data_valid <= '1';
275 17 nuubik
              lad_rising_o<=SYNC_OK;           --SYNC ok now                            
276 2 nuubik
              lpc_val <='0';
277
              CS <= DATAs;
278
            end if;
279
          end if;
280
         when others => null;
281
        end case;
282
      when LOCAL_TARs => ------------------------------------------------------
283 17 nuubik
       case cycle_type is
284
        when LPC_IO_W =>                   --IO write cycle   
285 2 nuubik
            lpc_wr <='0';
286 17 nuubik
            lad_rising_oe <='0';
287 2 nuubik
            CS <= RESETs;
288 17 nuubik
        when LPC_MEM_R | LPC_FW_R =>                    --Memory read cycle
289 2 nuubik
          if r_cnt ="000" then
290 17 nuubik
            lad_rising_o<= TAR_OK;
291
            r_cnt <= r_cnt + 1;
292 2 nuubik
          else
293 17 nuubik
            lad_rising_oe <= '0';
294 2 nuubik
            r_cnt <="000";
295
            CS <= RESETs;
296
          end if;
297
        when others => null;
298
       end case;
299
    end case; -----------------------------------------------------------------
300
  end if;
301
end process LPC;
302
 
303
end rtl;

powered by: WebSVN 2.1.0

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