1 \ *****************************************************************************
2 \ * Copyright (c) 2004, 2011 IBM Corporation
3 \ * All rights reserved.
4 \ * This program and the accompanying materials
5 \ * are made available under the terms of the BSD License
6 \ * which accompanies this distribution, and is available at
7 \ * http://www.opensource.org/licenses/bsd-license.php
10 \ * IBM Corporation - initial implementation
11 \ ****************************************************************************/
13 \ ----------------------------------------------------------
14 \ **************** PCI Helper functions *******************
15 \ ----------------------------------------------------------
17 \ convert an integer to string of len digits
18 : int2str ( int len -- str len ) swap s>d rot <# 0 ?DO # LOOP #> ;
20 \ convert addr to busnr
21 : pci-addr2bus ( addr -- busnr ) 10 rshift FF and ;
23 \ convert addr to devnr
24 : pci-addr2dev ( addr -- dev ) B rshift 1F and ;
26 \ convert addr to functionnumber
27 : pci-addr2fn ( addr -- dev ) 8 rshift 7 and ;
29 \ convert busnr devnr to addr
30 : pci-bus2addr ( busnr devnr -- addr ) B lshift swap 10 lshift + ;
32 \ print out a pci config addr
33 : pci-addr-out ( addr -- ) dup pci-addr2bus 2 0.r space FFFF and 4 0.r ;
35 \ Dump out the whole configspace
36 : pci-dump ( addr -- )
40 dup pci-addr-out space
47 \ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48 \ the following functions use l@ to fetch the data,
49 \ that's because the some pcie cores have probs with w@
50 \ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53 : pci-vendor@ ( addr -- id ) rtas-config-l@ FFFF and ;
56 : pci-device@ ( addr -- id ) rtas-config-l@ 10 rshift ;
59 : pci-status@ ( addr -- status ) 4 + rtas-config-l@ 10 rshift ;
62 : pci-revision@ ( addr -- id ) 8 + rtas-config-b@ ;
65 : pci-class@ ( addr -- class ) 8 + rtas-config-l@ 8 rshift ;
67 \ read Cache Line Size
68 : pci-cache@ ( addr -- size ) C + rtas-config-b@ ;
71 : pci-htype@ ( addr -- type ) E + rtas-config-b@ ;
74 : pci-sub-vendor@ ( addr -- sub-id ) 2C + rtas-config-l@ FFFF and ;
77 : pci-sub-device@ ( addr -- sub-id ) 2C + rtas-config-l@ 10 rshift FFFF and ;
80 : pci-interrupt@ ( addr -- interrupt ) 3D + rtas-config-b@ ;
83 : pci-min-grant@ ( addr -- min-gnt ) 3E + rtas-config-b@ ;
85 \ read Maximum Latency
86 : pci-max-lat@ ( addr -- max-lat ) 3F + rtas-config-b@ ;
88 \ Check if Capabilities are valid
89 : pci-capabilities? ( addr -- 0|1 ) pci-status@ 4 rshift 1 and ;
91 \ fetch the offset of the next capability
92 : pci-cap-next ( cap-addr -- next-cap-off ) rtas-config-b@ FC and ;
94 \ calc the address of the next capability
95 : pci-cap-next-addr ( cap-addr -- next-cap-addr ) 1+ dup pci-cap-next dup IF swap -100 and + ELSE nip THEN ;
98 \ Dump out all capabilities
99 : pci-cap-dump ( addr -- )
101 dup pci-capabilities? IF
103 pci-cap-next-addr dup 0<>
105 dup pci-addr-out s" : " type
106 dup rtas-config-b@ 2 0.r cr
110 s" capabilities not enabled!"
115 \ search the capability-list for this id
116 : pci-cap-find ( addr id -- capp-addr|0 )
117 swap dup pci-capabilities? IF
119 pci-cap-next-addr dup 0<> IF
120 dup rtas-config-b@ 2 pick =
131 \ check wether this device is a pci-express device
132 : pci-express? ( addr -- 0|1 ) 10 pci-cap-find 0<> ;
134 \ check wether this device is a pci-express device
135 : pci-x? ( addr -- 0|1 ) 07 pci-cap-find 0<> ;
137 \ check wether this device has extended config space
138 : pci-config-ext? ( addr -- 0|1 ) pci-express? ;
141 \ Disable Bus Master, Memory Space and I/O Space for this device
142 : pci-device-disable ( -- ) my-space 4 + dup rtas-config-l@ 7 invert and swap rtas-config-l! ;
145 : pci-master-enable ( -- ) my-space 4 + dup rtas-config-l@ 4 or swap rtas-config-l! ;
148 : pci-master-disable ( -- ) my-space 4 + dup rtas-config-l@ 4 invert and swap rtas-config-l! ;
150 \ Enable response to mem accesses of pci device
151 : pci-mem-enable ( -- ) my-space 4 + dup rtas-config-w@ 2 or swap rtas-config-w! ;
153 \ Enable response to I/O accesses of pci-device
154 : pci-io-enable ( -- ) my-space 4 + dup rtas-config-w@ 1 or swap rtas-config-w! ;
156 \ Enable Bus Master, I/O and mem access
157 : pci-enable ( -- ) my-space 4 + dup rtas-config-w@ 7 or swap rtas-config-w! ;
159 \ Enable #PERR and #SERR errors of pci-device
160 : pci-error-enable ( -- ) my-space 4 + dup rtas-config-w@ 140 or swap rtas-config-w! ;
162 \ prints out the ScanInformation about a device
163 \ char is a sign for device type e.g. D - device ; B - bridge
164 : pci-out ( addr char -- )
167 s" (" type emit s" ) : " type
168 dup pci-vendor@ 4 0.r space
174 \ set and fetch the interrupt Pin
175 : pci-irq-line@ ( addr -- irq-pin ) 3C + rtas-config-b@ ;
176 : pci-irq-line! ( pin addr -- ) 3C + rtas-config-b! ;
178 \ set and fetch primary bus number
179 : pci-bus-prim! ( nr addr -- ) 18 + dup rtas-config-l@ FFFFFF00 and rot + swap rtas-config-l! ;
180 : pci-bus-prim@ ( addr -- nr ) 18 + rtas-config-l@ FF and ;
182 \ set and fetch secondary bus number
183 : pci-bus-scnd! ( nr addr -- ) 18 + dup rtas-config-l@ FFFF00FF and rot 8 lshift + swap rtas-config-l! ;
184 : pci-bus-scnd@ ( addr -- nr ) 18 + rtas-config-l@ 8 rshift FF and ;
186 \ set and fetch subordinate bus number
187 : pci-bus-subo! ( nr addr -- ) 18 + dup rtas-config-l@ FF00FFFF and rot 10 lshift + swap rtas-config-l! ;
188 : pci-bus-subo@ ( addr -- nr ) 18 + rtas-config-l@ 10 rshift FF and ;
190 \ set and fetch primary, secondary and subordinate bus number
191 : pci-bus! ( subo scnd prim addr -- ) swap rot 8 lshift + rot 10 lshift + swap 18 + dup rtas-config-l@ FF000000 and rot + swap rtas-config-l! ;
192 : pci-bus@ ( addr -- subo scnd prim ) 18 + rtas-config-l@ dup 10 rshift FF and swap dup 8 rshift FF and swap FF and ;
194 \ Reset secondary Status
195 : pci-reset-2nd ( addr -- ) 1C + dup rtas-config-l@ FFFF0000 or swap rtas-config-l! ;