ocf: update to version 20120127
[15.05/openwrt.git] / target / linux / generic / files / crypto / ocf / kirkwood / mvHal / mv_hal / pex / mvVrtBrgPex.c
1 /*******************************************************************************\r
2 Copyright (C) Marvell International Ltd. and its affiliates\r
3 \r
4 This software file (the "File") is owned and distributed by Marvell \r
5 International Ltd. and/or its affiliates ("Marvell") under the following\r
6 alternative licensing terms.  Once you have made an election to distribute the\r
7 File under one of the following license alternatives, please (i) delete this\r
8 introductory statement regarding license alternatives, (ii) delete the two\r
9 license alternatives that you have not elected to use and (iii) preserve the\r
10 Marvell copyright notice above.\r
11 \r
12 ********************************************************************************\r
13 Marvell Commercial License Option\r
14 \r
15 If you received this File from Marvell and you have entered into a commercial\r
16 license agreement (a "Commercial License") with Marvell, the File is licensed\r
17 to you under the terms of the applicable Commercial License.\r
18 \r
19 ********************************************************************************\r
20 Marvell GPL License Option\r
21 \r
22 If you received this File from Marvell, you may opt to use, redistribute and/or \r
23 modify this File in accordance with the terms and conditions of the General \r
24 Public License Version 2, June 1991 (the "GPL License"), a copy of which is \r
25 available along with the File in the license.txt file or by writing to the Free \r
26 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 or \r
27 on the worldwide web at http://www.gnu.org/licenses/gpl.txt. \r
28 \r
29 THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE IMPLIED \r
30 WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY \r
31 DISCLAIMED.  The GPL License provides additional details about this warranty \r
32 disclaimer.\r
33 ********************************************************************************\r
34 Marvell BSD License Option\r
35 \r
36 If you received this File from Marvell, you may opt to use, redistribute and/or \r
37 modify this File under the following licensing terms. \r
38 Redistribution and use in source and binary forms, with or without modification, \r
39 are permitted provided that the following conditions are met:\r
40 \r
41     *   Redistributions of source code must retain the above copyright notice,\r
42             this list of conditions and the following disclaimer. \r
43 \r
44     *   Redistributions in binary form must reproduce the above copyright\r
45         notice, this list of conditions and the following disclaimer in the\r
46         documentation and/or other materials provided with the distribution. \r
47 \r
48     *   Neither the name of Marvell nor the names of its contributors may be \r
49         used to endorse or promote products derived from this software without \r
50         specific prior written permission. \r
51     \r
52 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND \r
53 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED \r
54 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE \r
55 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR \r
56 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES \r
57 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; \r
58 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
59 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
60 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS \r
61 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
62 \r
63 *******************************************************************************/\r
64 \r
65 #include "mvPex.h"\r
66 \r
67 //#define MV_DEBUG\r
68 /* defines  */       \r
69 #ifdef MV_DEBUG         \r
70         #define DB(x)   x\r
71 #else                \r
72         #define DB(x)    \r
73 #endif               \r
74 \r
75 /* locals */         \r
76 typedef struct\r
77 {\r
78         MV_U32 data;\r
79         MV_U32 mask;\r
80 }PEX_HEADER_DATA;\r
81 \r
82 /* local function forwad decleration */\r
83 MV_U32 mvPexHwConfigRead (MV_U32 pexIf, MV_U32 bus, MV_U32 dev, MV_U32 func, \r
84                         MV_U32 regOff);\r
85 MV_STATUS mvPexHwConfigWrite(MV_U32 pexIf, MV_U32 bus, MV_U32 dev, \r
86                            MV_U32 func, MV_U32 regOff, MV_U32 data);\r
87 void resetPexConfig(MV_U32 pexIf, MV_U32 bus, MV_U32 dev);\r
88 \r
89 \r
90 PEX_HEADER_DATA configHdr[16] = \r
91 {\r
92 {0x888811ab, 0x00000000}, /*[device ID, vendor ID] */\r
93 {0x00100007, 0x0000ffff}, /*[status register, command register] */  \r
94 {0x0604000e, 0x00000000}, /*[programming interface, sub class code, class code, revision ID] */\r
95 {0x00010008, 0x00000000},  /*[BIST, header type, latency time, cache line] */\r
96 {0x00000000, 0x00000000},  /*[base address 0] */             \r
97 {0x00000000, 0x00000000},  /*[base address 1] */             \r
98 {0x00000000, 0x00ffffff},  /*[secondary latency timersubordinate bus number, secondary bus number, primary bus number] */         \r
99 {0x0000f101, 0x00000000},  /*[secondary status ,IO limit, IO base] */\r
100 {0x9ff0a000, 0x00000000},  /*[memory limit, memory base] */                \r
101 {0x0001fff1, 0x00000000},  /*[prefetch memory limit, prefetch memory base] */       \r
102 {0xffffffff, 0x00000000},  /*[prefetch memory base upper] */ \r
103 {0x00000000, 0x00000000},  /*[prefetch memory limit upper] */\r
104 {0xeffff000, 0x00000000},  /*[IO limit upper 16 bits, IO base upper 16 bits] */      \r
105 {0x00000000, 0x00000000},  /*[reserved, capability pointer] */ \r
106 {0x00000000, 0x00000000},  /*[expansion ROM base address] */ \r
107 {0x00000000, 0x000000FF},  /*[bridge control, interrupt pin, interrupt line] */             \r
108 };\r
109 \r
110 \r
111 #define HEADER_WRITE(data, offset) configHdr[offset/4].data = ((configHdr[offset/4].data & ~configHdr[offset/4].mask) | \\r
112                                                                                                                                 (data & configHdr[offset/4].mask))\r
113 #define HEADER_READ(offset) configHdr[offset/4].data\r
114 \r
115 /*******************************************************************************\r
116 * mvVrtBrgPexInit - Initialize PEX interfaces\r
117 *\r
118 * DESCRIPTION:\r
119 *\r
120 * This function is responsible of intialization of the Pex Interface , It \r
121 * configure the Pex Bars and Windows in the following manner:\r
122 *\r
123 *  Assumptions : \r
124 *                               Bar0 is always internal registers bar\r
125 *                           Bar1 is always the DRAM bar\r
126 *                               Bar2 is always the Device bar\r
127 *\r
128 *  1) Sets the Internal registers bar base by obtaining the base from\r
129 *         the CPU Interface\r
130 *  2) Sets the DRAM bar base and size by getting the base and size from\r
131 *     the CPU Interface when the size is the sum of all enabled DRAM \r
132 *         chip selects and the base is the base of CS0 .\r
133 *  3) Sets the Device bar base and size by getting these values from the \r
134 *     CPU Interface when the base is the base of the lowest base of the\r
135 *     Device chip selects, and the \r
136 *\r
137 *\r
138 * INPUT:\r
139 *\r
140 *       pexIf   -  PEX interface number.\r
141 *\r
142 *\r
143 * OUTPUT:\r
144 *       None.\r
145 *\r
146 * RETURN:\r
147 *       MV_OK if function success otherwise MV_ERROR or MV_BAD_PARAM\r
148 *\r
149 *******************************************************************************/\r
150 MV_STATUS mvPexVrtBrgInit(MV_U32 pexIf)\r
151 {\r
152         /* reset PEX tree to recover previous U-boot/Boot configurations */\r
153         MV_U32 localBus = mvPexLocalBusNumGet(pexIf);\r
154 \r
155 \r
156         resetPexConfig(pexIf, localBus, 1);\r
157         return MV_OK;\r
158 }\r
159 \r
160 \r
161 MV_U32 mvPexVrtBrgConfigRead (MV_U32 pexIf, MV_U32 bus, MV_U32 dev, MV_U32 func, \r
162                         MV_U32 regOff)\r
163 {\r
164         \r
165         MV_U32 localBus = mvPexLocalBusNumGet(pexIf);\r
166         MV_U32 localDev = mvPexLocalDevNumGet(pexIf);\r
167         MV_U32 val;  \r
168         if(bus == localBus)\r
169         {\r
170                 if(dev > 1)\r
171                 {\r
172 /* on the local device allow only device #0 & #1 */\r
173                         return 0xffffffff;\r
174                 }\r
175                 else\r
176                 if (dev == localDev)\r
177                 {\r
178                         /* read the memory controller registers */\r
179                         return mvPexHwConfigRead (pexIf, bus, dev, func, regOff);\r
180                 }\r
181                 else\r
182                 {\r
183                         /* access the virtual brg header */\r
184                         return HEADER_READ(regOff);\r
185                 }\r
186         }\r
187         else\r
188         if(bus == (localBus + 1))\r
189         {\r
190                 /* access the device behind the virtual bridge */\r
191                 if((dev == localDev) || (dev > 1))\r
192                 {\r
193                         return 0xffffffff;\r
194                 }\r
195                 else\r
196                 {\r
197                         /* access the device behind the virtual bridge, in this case \r
198                         *  change the bus number to the local bus number in order to \r
199                         *  generate type 0 config cycle\r
200                         */                      \r
201                         mvPexLocalBusNumSet(pexIf, bus);\r
202                         mvPexLocalDevNumSet(pexIf, 1);\r
203                         val = mvPexHwConfigRead (pexIf, bus, 0, func, regOff);\r
204                         mvPexLocalBusNumSet(pexIf, localBus);\r
205                         mvPexLocalDevNumSet(pexIf, localDev);\r
206                         return val;\r
207                 }\r
208         }\r
209         /* for all other devices use the HW function to get the \r
210         *  requested registers\r
211         */\r
212         mvPexLocalDevNumSet(pexIf, 1);\r
213         val = mvPexHwConfigRead (pexIf, bus, dev, func, regOff);\r
214         mvPexLocalDevNumSet(pexIf, localDev);   \r
215         return val;\r
216 }\r
217 \r
218 \r
219 MV_STATUS mvPexVrtBrgConfigWrite(MV_U32 pexIf, MV_U32 bus, MV_U32 dev, \r
220                            MV_U32 func, MV_U32 regOff, MV_U32 data)\r
221 {\r
222         MV_U32 localBus = mvPexLocalBusNumGet(pexIf);\r
223         MV_U32 localDev = mvPexLocalDevNumGet(pexIf);\r
224         MV_STATUS       status;\r
225 \r
226         if(bus == localBus)\r
227         {\r
228                 if(dev > 1)\r
229                 {\r
230                         /* on the local device allow only device #0 & #1 */\r
231                         return MV_ERROR;\r
232                 }\r
233                 else\r
234                 if (dev == localDev)\r
235                 {\r
236                         /* read the memory controller registers */\r
237                         return mvPexHwConfigWrite (pexIf, bus, dev, func, regOff, data);                        \r
238                 }\r
239                 else\r
240                 {\r
241                         /* access the virtual brg header */\r
242                         HEADER_WRITE(data, regOff);\r
243                         return MV_OK;\r
244                 }\r
245         }\r
246         else\r
247         if(bus == (localBus + 1))\r
248         {\r
249                 /* access the device behind the virtual bridge */\r
250                 if((dev == localDev) || (dev > 1))\r
251                 {\r
252                         return MV_ERROR;\r
253                 }\r
254                 else\r
255                 {\r
256                         /* access the device behind the virtual bridge, in this case \r
257                         *  change the bus number to the local bus number in order to \r
258                         *  generate type 0 config cycle\r
259                         */\r
260                         //return mvPexHwConfigWrite (pexIf, localBus, dev, func, regOff, data);\r
261                         mvPexLocalBusNumSet(pexIf, bus);\r
262                         mvPexLocalDevNumSet(pexIf, 1);\r
263                         status = mvPexHwConfigWrite (pexIf, bus, 0, func, regOff, data);\r
264                         mvPexLocalBusNumSet(pexIf, localBus);\r
265                         mvPexLocalDevNumSet(pexIf, localDev);\r
266                         return status;\r
267 \r
268                 }\r
269         }\r
270         /* for all other devices use the HW function to get the \r
271         *  requested registers\r
272         */\r
273         mvPexLocalDevNumSet(pexIf, 1);\r
274         status = mvPexHwConfigWrite (pexIf, bus, dev, func, regOff, data);\r
275         mvPexLocalDevNumSet(pexIf, localDev);\r
276         return status;\r
277 }\r
278 \r
279 \r
280 \r
281 \r
282 void resetPexConfig(MV_U32 pexIf, MV_U32 bus, MV_U32 dev)\r
283 {\r
284         MV_U32 tData;\r
285         MV_U32 i;\r
286 \r
287         /* restore the PEX configuration to initialization state */\r
288         /* in case PEX P2P call recursive and reset config */\r
289         tData = mvPexHwConfigRead (pexIf, bus, dev, 0x0, 0x0);\r
290         if(tData != 0xffffffff)\r
291         {\r
292                 /* agent had been found - check whether P2P */\r
293                 tData = mvPexHwConfigRead (pexIf, bus, dev, 0x0, 0x8);\r
294                 if((tData & 0xffff0000) == 0x06040000)\r
295                 {/* P2P */\r
296                         /* get the sec bus and the subordinate */\r
297                         MV_U32 secBus;  \r
298                         tData = mvPexHwConfigRead (pexIf, bus, dev, 0x0, 0x18);\r
299                         secBus = ((tData >> 8) & 0xff);\r
300                         /* now scan on sec bus */\r
301                         for(i = 0;i < 0xff;i++)\r
302                         {\r
303                                 resetPexConfig(pexIf, secBus, i);\r
304                         }\r
305                         /* now reset this device */\r
306                         DB(mvOsPrintf("Reset bus %d dev %d\n", bus, dev));\r
307                         mvPexHwConfigWrite(pexIf, bus, dev, 0x0, 0x18, 0x0); \r
308                         DB(mvOsPrintf("Reset bus %d dev %d\n", bus, dev));\r
309                 }\r
310         }\r
311 }\r
312 \r
313 \r