ambapp.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* Gaisler AMBA Plug&Play bus scanning. Functions
  2. * ending on _nomem is inteded to be used only during
  3. * initialization, only registers are used (no ram).
  4. *
  5. * (C) Copyright 2007
  6. * Daniel Hellstrom, Gaisler Research, daniel@gaisler.com
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <ambapp.h>
  29. static int ambapp_apb_scan(unsigned int vendor, /* Plug&Play Vendor ID */
  30. unsigned int driver, /* Plug&Play Device ID */
  31. ambapp_apbdev * dev, /* Result(s) is placed here */
  32. int index, /* Index of device to start copying Plug&Play
  33. * info into dev
  34. */
  35. int max_cnt /* Maximal count that dev can hold, if dev
  36. * is NULL function will stop scanning after
  37. * max_cnt devices are found.
  38. */
  39. )
  40. {
  41. int i, cnt = 0;
  42. unsigned int apbmst_base;
  43. ambapp_ahbdev apbmst;
  44. apbctrl_pp_dev *apb;
  45. if (max_cnt == 0)
  46. return 0;
  47. /* Get AMBA APB Master */
  48. if (ambapp_ahbslv_first(VENDOR_GAISLER, GAISLER_APBMST, &apbmst) != 1) {
  49. return 0;
  50. }
  51. /* Get APB CTRL Plug&Play info area */
  52. apbmst_base = apbmst.address[0] & LEON3_IO_AREA;
  53. apb = (apbctrl_pp_dev *) (apbmst_base | LEON3_CONF_AREA);
  54. for (i = 0; i < LEON3_APB_SLAVES; i++) {
  55. if ((amba_vendor(apb->conf) == vendor) &&
  56. (amba_device(apb->conf) == driver) && ((index < 0)
  57. || (index-- == 0))) {
  58. /* Convert Plug&Play info into a more readable format */
  59. cnt++;
  60. if (dev) {
  61. dev->irq = amba_irq(apb->conf);
  62. dev->ver = amba_ver(apb->conf);
  63. dev->address =
  64. (apbmst_base |
  65. (((apb->
  66. bar & 0xfff00000) >> 12))) & (((apb->
  67. bar &
  68. 0x0000fff0)
  69. << 4) |
  70. 0xfff00000);
  71. dev++;
  72. }
  73. /* found max devices? */
  74. if (cnt >= max_cnt)
  75. return cnt;
  76. }
  77. /* Get next Plug&Play entry */
  78. apb++;
  79. }
  80. return cnt;
  81. }
  82. unsigned int ambapp_apb_next_nomem(register unsigned int vendor, /* Plug&Play Vendor ID */
  83. register unsigned int driver, /* Plug&Play Device ID */
  84. register int index)
  85. {
  86. register int i;
  87. register ahbctrl_pp_dev *apbmst;
  88. register apbctrl_pp_dev *apb;
  89. register unsigned int apbmst_base;
  90. /* APBMST is a AHB Slave */
  91. apbmst = ambapp_ahb_next_nomem(VENDOR_GAISLER, GAISLER_APBMST, 1, 0);
  92. if (!apbmst)
  93. return 0;
  94. apbmst_base = amba_membar_start(apbmst->bars[0]);
  95. if (amba_membar_type(apbmst->bars[0]) == AMBA_TYPE_AHBIO)
  96. apbmst_base = AMBA_TYPE_AHBIO_ADDR(apbmst_base);
  97. apbmst_base &= LEON3_IO_AREA;
  98. /* Find the vendor/driver device on the first APB bus */
  99. apb = (apbctrl_pp_dev *) (apbmst_base | LEON3_CONF_AREA);
  100. for (i = 0; i < LEON3_APB_SLAVES; i++) {
  101. if ((amba_vendor(apb->conf) == vendor) &&
  102. (amba_device(apb->conf) == driver) && ((index < 0)
  103. || (index-- == 0))) {
  104. /* Convert Plug&Play info info a more readable format */
  105. return (apbmst_base | (((apb->bar & 0xfff00000) >> 12)))
  106. & (((apb->bar & 0x0000fff0) << 4) | 0xfff00000);
  107. }
  108. /* Get next Plug&Play entry */
  109. apb++;
  110. }
  111. return 0;
  112. }
  113. /****************************** APB SLAVES ******************************/
  114. int ambapp_apb_count(unsigned int vendor, unsigned int driver)
  115. {
  116. return ambapp_apb_scan(vendor, driver, NULL, 0, LEON3_APB_SLAVES);
  117. }
  118. int ambapp_apb_first(unsigned int vendor,
  119. unsigned int driver, ambapp_apbdev * dev)
  120. {
  121. return ambapp_apb_scan(vendor, driver, dev, 0, 1);
  122. }
  123. int ambapp_apb_next(unsigned int vendor,
  124. unsigned int driver, ambapp_apbdev * dev, int index)
  125. {
  126. return ambapp_apb_scan(vendor, driver, dev, index, 1);
  127. }
  128. int ambapp_apbs_first(unsigned int vendor,
  129. unsigned int driver, ambapp_apbdev * dev, int max_cnt)
  130. {
  131. return ambapp_apb_scan(vendor, driver, dev, 0, max_cnt);
  132. }
  133. enum {
  134. AHB_SCAN_MASTER = 0,
  135. AHB_SCAN_SLAVE = 1
  136. };
  137. /* Scan AMBA Plug&Play bus for AMBA AHB Masters or AHB Slaves
  138. * for a certain matching Vendor and Device ID.
  139. *
  140. * Return number of devices found.
  141. *
  142. * Compact edition...
  143. */
  144. static int ambapp_ahb_scan(unsigned int vendor, /* Plug&Play Vendor ID */
  145. unsigned int driver, /* Plug&Play Device ID */
  146. ambapp_ahbdev * dev, /* Result(s) is placed here */
  147. int index, /* Index of device to start copying Plug&Play
  148. * info into dev
  149. */
  150. int max_cnt, /* Maximal count that dev can hold, if dev
  151. * is NULL function will stop scanning after
  152. * max_cnt devices are found.
  153. */
  154. int type /* Selectes what type of devices to scan.
  155. * 0=AHB Masters
  156. * 1=AHB Slaves
  157. */
  158. )
  159. {
  160. int i, j, cnt = 0, max_pp_devs;
  161. unsigned int addr;
  162. ahbctrl_info *info = (ahbctrl_info *) (LEON3_IO_AREA | LEON3_CONF_AREA);
  163. ahbctrl_pp_dev *ahb;
  164. if (max_cnt == 0)
  165. return 0;
  166. if (type == 0) {
  167. max_pp_devs = LEON3_AHB_MASTERS;
  168. ahb = info->masters;
  169. } else {
  170. max_pp_devs = LEON3_AHB_SLAVES;
  171. ahb = info->slaves;
  172. }
  173. for (i = 0; i < max_pp_devs; i++) {
  174. if ((amba_vendor(ahb->conf) == vendor) &&
  175. (amba_device(ahb->conf) == driver) &&
  176. ((index < 0) || (index-- == 0))) {
  177. /* Convert Plug&Play info info a more readable format */
  178. cnt++;
  179. if (dev) {
  180. dev->irq = amba_irq(ahb->conf);
  181. dev->ver = amba_ver(ahb->conf);
  182. dev->userdef[0] = ahb->userdef[0];
  183. dev->userdef[1] = ahb->userdef[1];
  184. dev->userdef[2] = ahb->userdef[2];
  185. for (j = 0; j < 4; j++) {
  186. addr = amba_membar_start(ahb->bars[j]);
  187. if (amba_membar_type(ahb->bars[j]) ==
  188. AMBA_TYPE_AHBIO)
  189. addr =
  190. AMBA_TYPE_AHBIO_ADDR(addr);
  191. dev->address[j] = addr;
  192. }
  193. dev++;
  194. }
  195. /* found max devices? */
  196. if (cnt >= max_cnt)
  197. return cnt;
  198. }
  199. /* Get next Plug&Play entry */
  200. ahb++;
  201. }
  202. return cnt;
  203. }
  204. unsigned int ambapp_ahb_get_info(ahbctrl_pp_dev * ahb, int info)
  205. {
  206. register unsigned int ret;
  207. if (!ahb)
  208. return 0;
  209. switch (info) {
  210. default:
  211. info = 0;
  212. case 0:
  213. case 1:
  214. case 2:
  215. case 3:
  216. /* Get Address from PnP Info */
  217. ret = amba_membar_start(ahb->bars[info]);
  218. if (amba_membar_type(ahb->bars[info]) == AMBA_TYPE_AHBIO)
  219. ret = AMBA_TYPE_AHBIO_ADDR(ret);
  220. return ret;
  221. }
  222. return 0;
  223. }
  224. ahbctrl_pp_dev *ambapp_ahb_next_nomem(register unsigned int vendor, /* Plug&Play Vendor ID */
  225. register unsigned int driver, /* Plug&Play Device ID */
  226. register unsigned int opts, /* 1=slave, 0=master */
  227. register int index)
  228. {
  229. register ahbctrl_pp_dev *ahb;
  230. register ahbctrl_info *info =
  231. (ahbctrl_info *) (LEON3_IO_AREA | LEON3_CONF_AREA);
  232. register int i;
  233. register int max_pp_devs;
  234. if (opts == 0) {
  235. max_pp_devs = LEON3_AHB_MASTERS;
  236. ahb = info->masters;
  237. } else {
  238. max_pp_devs = LEON3_AHB_SLAVES;
  239. ahb = info->slaves;
  240. }
  241. for (i = 0; i < max_pp_devs; i++) {
  242. if ((amba_vendor(ahb->conf) == vendor) &&
  243. (amba_device(ahb->conf) == driver) &&
  244. ((index < 0) || (index-- == 0))) {
  245. /* Convert Plug&Play info info a more readable format */
  246. return ahb;
  247. }
  248. /* Get next Plug&Play entry */
  249. ahb++;
  250. }
  251. return 0;
  252. }
  253. /****************************** AHB MASTERS ******************************/
  254. int ambapp_ahbmst_count(unsigned int vendor, unsigned int driver)
  255. {
  256. /* Get number of devices of this vendor&device ID */
  257. return ambapp_ahb_scan(vendor, driver, NULL, 0, LEON3_AHB_MASTERS,
  258. AHB_SCAN_MASTER);
  259. }
  260. int ambapp_ahbmst_first(unsigned int vendor, unsigned int driver,
  261. ambapp_ahbdev * dev)
  262. {
  263. /* find first device of this */
  264. return ambapp_ahb_scan(vendor, driver, dev, 0, 1, AHB_SCAN_MASTER);
  265. }
  266. int ambapp_ahbmst_next(unsigned int vendor,
  267. unsigned int driver, ambapp_ahbdev * dev, int index)
  268. {
  269. /* find first device of this */
  270. return ambapp_ahb_scan(vendor, driver, dev, index, 1, AHB_SCAN_MASTER);
  271. }
  272. int ambapp_ahbmsts_first(unsigned int vendor,
  273. unsigned int driver, ambapp_ahbdev * dev, int max_cnt)
  274. {
  275. /* find first device of this */
  276. return ambapp_ahb_scan(vendor, driver, dev, 0, max_cnt,
  277. AHB_SCAN_MASTER);
  278. }
  279. /****************************** AHB SLAVES ******************************/
  280. int ambapp_ahbslv_count(unsigned int vendor, unsigned int driver)
  281. {
  282. /* Get number of devices of this vendor&device ID */
  283. return ambapp_ahb_scan(vendor, driver, NULL, 0, LEON3_AHB_SLAVES,
  284. AHB_SCAN_SLAVE);
  285. }
  286. int ambapp_ahbslv_first(unsigned int vendor, unsigned int driver,
  287. ambapp_ahbdev * dev)
  288. {
  289. /* find first device of this */
  290. return ambapp_ahb_scan(vendor, driver, dev, 0, 1, AHB_SCAN_SLAVE);
  291. }
  292. int ambapp_ahbslv_next(unsigned int vendor,
  293. unsigned int driver, ambapp_ahbdev * dev, int index)
  294. {
  295. /* find first device of this */
  296. return ambapp_ahb_scan(vendor, driver, dev, index, 1, AHB_SCAN_SLAVE);
  297. }
  298. int ambapp_ahbslvs_first(unsigned int vendor,
  299. unsigned int driver, ambapp_ahbdev * dev, int max_cnt)
  300. {
  301. /* find first device of this */
  302. return ambapp_ahb_scan(vendor, driver, dev, 0, max_cnt, AHB_SCAN_SLAVE);
  303. }