addr-map.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * arch/arm/mach-orion/addr-map.c
  3. *
  4. * Address map functions for Marvell Orion System On Chip
  5. *
  6. * Maintainer: Tzachi Perelstein <tzachi@marvell.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <asm/hardware.h>
  15. #include "common.h"
  16. /*
  17. * The Orion has fully programable address map. There's a separate address
  18. * map for each of the device _master_ interfaces, e.g. CPU, PCI, PCIE, USB,
  19. * Gigabit Ethernet, DMA/XOR engines, etc. Each interface has its own
  20. * address decode windows that allow it to access any of the Orion resources.
  21. *
  22. * CPU address decoding --
  23. * Linux assumes that it is the boot loader that already setup the access to
  24. * DDR and internal registers.
  25. * Setup access to PCI and PCI-E IO/MEM space is issued by core.c.
  26. * Setup access to various devices located on the device bus interface (e.g.
  27. * flashes, RTC, etc) should be issued by machine-setup.c according to
  28. * specific board population (by using orion_setup_cpu_win()).
  29. *
  30. * Non-CPU Masters address decoding --
  31. * Unlike the CPU, we setup the access from Orion's master interfaces to DDR
  32. * banks only (the typical use case).
  33. * Setup access for each master to DDR is issued by common.c.
  34. *
  35. * Note: although orion_setbits() and orion_clrbits() are not atomic
  36. * no locking is necessary here since code in this file is only called
  37. * at boot time when there is no concurrency issues.
  38. */
  39. /*
  40. * Generic Address Decode Windows bit settings
  41. */
  42. #define TARGET_DDR 0
  43. #define TARGET_PCI 3
  44. #define TARGET_PCIE 4
  45. #define TARGET_DEV_BUS 1
  46. #define ATTR_DDR_CS(n) (((n) ==0) ? 0xe : \
  47. ((n) == 1) ? 0xd : \
  48. ((n) == 2) ? 0xb : \
  49. ((n) == 3) ? 0x7 : 0xf)
  50. #define ATTR_PCIE_MEM 0x59
  51. #define ATTR_PCIE_IO 0x51
  52. #define ATTR_PCI_MEM 0x59
  53. #define ATTR_PCI_IO 0x51
  54. #define ATTR_DEV_CS0 0x1e
  55. #define ATTR_DEV_CS1 0x1d
  56. #define ATTR_DEV_CS2 0x1b
  57. #define ATTR_DEV_BOOT 0xf
  58. #define WIN_EN 1
  59. /*
  60. * Helpers to get DDR banks info
  61. */
  62. #define DDR_BASE_CS(n) ORION_DDR_REG(0x1500 + ((n) * 8))
  63. #define DDR_SIZE_CS(n) ORION_DDR_REG(0x1504 + ((n) * 8))
  64. #define DDR_MAX_CS 4
  65. #define DDR_REG_TO_SIZE(reg) (((reg) | 0xffffff) + 1)
  66. #define DDR_REG_TO_BASE(reg) ((reg) & 0xff000000)
  67. #define DDR_BANK_EN 1
  68. /*
  69. * CPU Address Decode Windows registers
  70. */
  71. #define CPU_WIN_CTRL(n) ORION_BRIDGE_REG(0x000 | ((n) << 4))
  72. #define CPU_WIN_BASE(n) ORION_BRIDGE_REG(0x004 | ((n) << 4))
  73. #define CPU_WIN_REMAP_LO(n) ORION_BRIDGE_REG(0x008 | ((n) << 4))
  74. #define CPU_WIN_REMAP_HI(n) ORION_BRIDGE_REG(0x00c | ((n) << 4))
  75. #define CPU_MAX_WIN 8
  76. /*
  77. * Use this CPU address decode windows allocation
  78. */
  79. #define CPU_WIN_PCIE_IO 0
  80. #define CPU_WIN_PCI_IO 1
  81. #define CPU_WIN_PCIE_MEM 2
  82. #define CPU_WIN_PCI_MEM 3
  83. #define CPU_WIN_DEV_BOOT 4
  84. #define CPU_WIN_DEV_CS0 5
  85. #define CPU_WIN_DEV_CS1 6
  86. #define CPU_WIN_DEV_CS2 7
  87. /*
  88. * PCIE Address Decode Windows registers
  89. */
  90. #define PCIE_BAR_CTRL(n) ORION_PCIE_REG(0x1804 + ((n - 1) * 4))
  91. #define PCIE_BAR_LO(n) ORION_PCIE_REG(0x0010 + ((n) * 8))
  92. #define PCIE_BAR_HI(n) ORION_PCIE_REG(0x0014 + ((n) * 8))
  93. #define PCIE_WIN_CTRL(n) ORION_PCIE_REG(0x1820 + ((n) << 4))
  94. #define PCIE_WIN_BASE(n) ORION_PCIE_REG(0x1824 + ((n) << 4))
  95. #define PCIE_WIN_REMAP(n) ORION_PCIE_REG(0x182c + ((n) << 4))
  96. #define PCIE_DEFWIN_CTRL ORION_PCIE_REG(0x18b0)
  97. #define PCIE_EXPROM_WIN_CTRL ORION_PCIE_REG(0x18c0)
  98. #define PCIE_EXPROM_WIN_REMP ORION_PCIE_REG(0x18c4)
  99. #define PCIE_MAX_BARS 3
  100. #define PCIE_MAX_WINS 5
  101. /*
  102. * Use PCIE BAR '1' for all DDR banks
  103. */
  104. #define PCIE_DRAM_BAR 1
  105. /*
  106. * PCI Address Decode Windows registers
  107. */
  108. #define PCI_BAR_SIZE_DDR_CS(n) (((n) == 0) ? ORION_PCI_REG(0xc08) : \
  109. ((n) == 1) ? ORION_PCI_REG(0xd08) : \
  110. ((n) == 2) ? ORION_PCI_REG(0xc0c) : \
  111. ((n) == 3) ? ORION_PCI_REG(0xd0c) : 0)
  112. #define PCI_BAR_REMAP_DDR_CS(n) (((n) ==0) ? ORION_PCI_REG(0xc48) : \
  113. ((n) == 1) ? ORION_PCI_REG(0xd48) : \
  114. ((n) == 2) ? ORION_PCI_REG(0xc4c) : \
  115. ((n) == 3) ? ORION_PCI_REG(0xd4c) : 0)
  116. #define PCI_BAR_ENABLE ORION_PCI_REG(0xc3c)
  117. #define PCI_CTRL_BASE_LO(n) ORION_PCI_REG(0x1e00 | ((n) << 4))
  118. #define PCI_CTRL_BASE_HI(n) ORION_PCI_REG(0x1e04 | ((n) << 4))
  119. #define PCI_CTRL_SIZE(n) ORION_PCI_REG(0x1e08 | ((n) << 4))
  120. #define PCI_ADDR_DECODE_CTRL ORION_PCI_REG(0xd3c)
  121. /*
  122. * PCI configuration heleprs for BAR settings
  123. */
  124. #define PCI_CONF_FUNC_BAR_CS(n) ((n) >> 1)
  125. #define PCI_CONF_REG_BAR_LO_CS(n) (((n) & 1) ? 0x18 : 0x10)
  126. #define PCI_CONF_REG_BAR_HI_CS(n) (((n) & 1) ? 0x1c : 0x14)
  127. /*
  128. * Gigabit Ethernet Address Decode Windows registers
  129. */
  130. #define ETH_WIN_BASE(win) ORION_ETH_REG(0x200 + ((win) * 8))
  131. #define ETH_WIN_SIZE(win) ORION_ETH_REG(0x204 + ((win) * 8))
  132. #define ETH_WIN_REMAP(win) ORION_ETH_REG(0x280 + ((win) * 4))
  133. #define ETH_WIN_EN ORION_ETH_REG(0x290)
  134. #define ETH_WIN_PROT ORION_ETH_REG(0x294)
  135. #define ETH_MAX_WIN 6
  136. #define ETH_MAX_REMAP_WIN 4
  137. /*
  138. * USB Address Decode Windows registers
  139. */
  140. #define USB_WIN_CTRL(i, w) ((i == 0) ? ORION_USB0_REG(0x320 + ((w) << 4)) \
  141. : ORION_USB1_REG(0x320 + ((w) << 4)))
  142. #define USB_WIN_BASE(i, w) ((i == 0) ? ORION_USB0_REG(0x324 + ((w) << 4)) \
  143. : ORION_USB1_REG(0x324 + ((w) << 4)))
  144. #define USB_MAX_WIN 4
  145. /*
  146. * SATA Address Decode Windows registers
  147. */
  148. #define SATA_WIN_CTRL(win) ORION_SATA_REG(0x30 + ((win) * 0x10))
  149. #define SATA_WIN_BASE(win) ORION_SATA_REG(0x34 + ((win) * 0x10))
  150. #define SATA_MAX_WIN 4
  151. static int __init orion_cpu_win_can_remap(u32 win)
  152. {
  153. u32 dev, rev;
  154. orion_pcie_id(&dev, &rev);
  155. if ((dev == MV88F5281_DEV_ID && win < 4) || (dev == MV88F5182_DEV_ID && win < 2))
  156. return 1;
  157. return 0;
  158. }
  159. void __init orion_setup_cpu_win(enum orion_target target, u32 base, u32 size, int remap)
  160. {
  161. u32 win, attr, ctrl;
  162. switch (target) {
  163. case ORION_PCIE_IO:
  164. target = TARGET_PCIE;
  165. attr = ATTR_PCIE_IO;
  166. win = CPU_WIN_PCIE_IO;
  167. break;
  168. case ORION_PCI_IO:
  169. target = TARGET_PCI;
  170. attr = ATTR_PCI_IO;
  171. win = CPU_WIN_PCI_IO;
  172. break;
  173. case ORION_PCIE_MEM:
  174. target = TARGET_PCIE;
  175. attr = ATTR_PCIE_MEM;
  176. win = CPU_WIN_PCIE_MEM;
  177. break;
  178. case ORION_PCI_MEM:
  179. target = TARGET_PCI;
  180. attr = ATTR_PCI_MEM;
  181. win = CPU_WIN_PCI_MEM;
  182. break;
  183. case ORION_DEV_BOOT:
  184. target = TARGET_DEV_BUS;
  185. attr = ATTR_DEV_BOOT;
  186. win = CPU_WIN_DEV_BOOT;
  187. break;
  188. case ORION_DEV0:
  189. target = TARGET_DEV_BUS;
  190. attr = ATTR_DEV_CS0;
  191. win = CPU_WIN_DEV_CS0;
  192. break;
  193. case ORION_DEV1:
  194. target = TARGET_DEV_BUS;
  195. attr = ATTR_DEV_CS1;
  196. win = CPU_WIN_DEV_CS1;
  197. break;
  198. case ORION_DEV2:
  199. target = TARGET_DEV_BUS;
  200. attr = ATTR_DEV_CS2;
  201. win = CPU_WIN_DEV_CS2;
  202. break;
  203. case ORION_DDR:
  204. case ORION_REGS:
  205. /*
  206. * Must be mapped by bootloader.
  207. */
  208. default:
  209. target = attr = win = -1;
  210. BUG();
  211. }
  212. base &= 0xffff0000;
  213. ctrl = (((size - 1) & 0xffff0000) | (attr << 8) |
  214. (target << 4) | WIN_EN);
  215. orion_write(CPU_WIN_BASE(win), base);
  216. orion_write(CPU_WIN_CTRL(win), ctrl);
  217. if (orion_cpu_win_can_remap(win)) {
  218. if (remap >= 0) {
  219. orion_write(CPU_WIN_REMAP_LO(win), remap & 0xffff0000);
  220. orion_write(CPU_WIN_REMAP_HI(win), 0);
  221. } else {
  222. orion_write(CPU_WIN_REMAP_LO(win), base);
  223. orion_write(CPU_WIN_REMAP_HI(win), 0);
  224. }
  225. }
  226. }
  227. void __init orion_setup_cpu_wins(void)
  228. {
  229. int i;
  230. /*
  231. * First, disable and clear windows
  232. */
  233. for (i = 0; i < CPU_MAX_WIN; i++) {
  234. orion_write(CPU_WIN_BASE(i), 0);
  235. orion_write(CPU_WIN_CTRL(i), 0);
  236. if (orion_cpu_win_can_remap(i)) {
  237. orion_write(CPU_WIN_REMAP_LO(i), 0);
  238. orion_write(CPU_WIN_REMAP_HI(i), 0);
  239. }
  240. }
  241. /*
  242. * Setup windows for PCI+PCIE IO+MAM space
  243. */
  244. orion_setup_cpu_win(ORION_PCIE_IO, ORION_PCIE_IO_BASE,
  245. ORION_PCIE_IO_SIZE, ORION_PCIE_IO_REMAP);
  246. orion_setup_cpu_win(ORION_PCI_IO, ORION_PCI_IO_BASE,
  247. ORION_PCI_IO_SIZE, ORION_PCI_IO_REMAP);
  248. orion_setup_cpu_win(ORION_PCIE_MEM, ORION_PCIE_MEM_BASE,
  249. ORION_PCIE_MEM_SIZE, -1);
  250. orion_setup_cpu_win(ORION_PCI_MEM, ORION_PCI_MEM_BASE,
  251. ORION_PCI_MEM_SIZE, -1);
  252. }
  253. /*
  254. * Setup PCIE BARs and Address Decode Wins:
  255. * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
  256. * WIN[0-3] -> DRAM bank[0-3]
  257. */
  258. void __init orion_setup_pcie_wins(void)
  259. {
  260. u32 base, size, i;
  261. /*
  262. * First, disable and clear BARs and windows
  263. */
  264. for (i = 1; i < PCIE_MAX_BARS; i++) {
  265. orion_write(PCIE_BAR_CTRL(i), 0);
  266. orion_write(PCIE_BAR_LO(i), 0);
  267. orion_write(PCIE_BAR_HI(i), 0);
  268. }
  269. for (i = 0; i < PCIE_MAX_WINS; i++) {
  270. orion_write(PCIE_WIN_CTRL(i), 0);
  271. orion_write(PCIE_WIN_BASE(i), 0);
  272. orion_write(PCIE_WIN_REMAP(i), 0);
  273. }
  274. /*
  275. * Setup windows for DDR banks. Count total DDR size on the fly.
  276. */
  277. base = DDR_REG_TO_BASE(orion_read(DDR_BASE_CS(0)));
  278. size = 0;
  279. for (i = 0; i < DDR_MAX_CS; i++) {
  280. u32 bank_base, bank_size;
  281. bank_size = orion_read(DDR_SIZE_CS(i));
  282. bank_base = orion_read(DDR_BASE_CS(i));
  283. if (bank_size & DDR_BANK_EN) {
  284. bank_size = DDR_REG_TO_SIZE(bank_size);
  285. bank_base = DDR_REG_TO_BASE(bank_base);
  286. orion_write(PCIE_WIN_BASE(i), bank_base & 0xffff0000);
  287. orion_write(PCIE_WIN_REMAP(i), 0);
  288. orion_write(PCIE_WIN_CTRL(i),
  289. ((bank_size-1) & 0xffff0000) |
  290. (ATTR_DDR_CS(i) << 8) |
  291. (TARGET_DDR << 4) |
  292. (PCIE_DRAM_BAR << 1) | WIN_EN);
  293. size += bank_size;
  294. }
  295. }
  296. /*
  297. * Setup BAR[1] to all DRAM banks
  298. */
  299. orion_write(PCIE_BAR_LO(PCIE_DRAM_BAR), base & 0xffff0000);
  300. orion_write(PCIE_BAR_HI(PCIE_DRAM_BAR), 0);
  301. orion_write(PCIE_BAR_CTRL(PCIE_DRAM_BAR),
  302. ((size - 1) & 0xffff0000) | WIN_EN);
  303. }
  304. void __init orion_setup_pci_wins(void)
  305. {
  306. u32 base, size, i;
  307. /*
  308. * First, disable windows
  309. */
  310. orion_write(PCI_BAR_ENABLE, 0xffffffff);
  311. /*
  312. * Setup windows for DDR banks.
  313. */
  314. for (i = 0; i < DDR_MAX_CS; i++) {
  315. base = orion_read(DDR_BASE_CS(i));
  316. size = orion_read(DDR_SIZE_CS(i));
  317. if (size & DDR_BANK_EN) {
  318. u32 bus, dev, func, reg, val;
  319. size = DDR_REG_TO_SIZE(size);
  320. base = DDR_REG_TO_BASE(base);
  321. bus = orion_pci_local_bus_nr();
  322. dev = orion_pci_local_dev_nr();
  323. func = PCI_CONF_FUNC_BAR_CS(i);
  324. reg = PCI_CONF_REG_BAR_LO_CS(i);
  325. orion_pci_hw_rd_conf(bus, dev, func, reg, 4, &val);
  326. orion_pci_hw_wr_conf(bus, dev, func, reg, 4,
  327. (base & 0xfffff000) | (val & 0xfff));
  328. reg = PCI_CONF_REG_BAR_HI_CS(i);
  329. orion_pci_hw_wr_conf(bus, dev, func, reg, 4, 0);
  330. orion_write(PCI_BAR_SIZE_DDR_CS(i),
  331. (size - 1) & 0xfffff000);
  332. orion_write(PCI_BAR_REMAP_DDR_CS(i),
  333. base & 0xfffff000);
  334. orion_clrbits(PCI_BAR_ENABLE, (1 << i));
  335. }
  336. }
  337. /*
  338. * Disable automatic update of address remaping when writing to BARs
  339. */
  340. orion_setbits(PCI_ADDR_DECODE_CTRL, 1);
  341. }
  342. void __init orion_setup_usb_wins(void)
  343. {
  344. int i;
  345. u32 usb_if, dev, rev;
  346. u32 max_usb_if = 1;
  347. orion_pcie_id(&dev, &rev);
  348. if (dev == MV88F5182_DEV_ID)
  349. max_usb_if = 2;
  350. for (usb_if = 0; usb_if < max_usb_if; usb_if++) {
  351. /*
  352. * First, disable and clear windows
  353. */
  354. for (i = 0; i < USB_MAX_WIN; i++) {
  355. orion_write(USB_WIN_BASE(usb_if, i), 0);
  356. orion_write(USB_WIN_CTRL(usb_if, i), 0);
  357. }
  358. /*
  359. * Setup windows for DDR banks.
  360. */
  361. for (i = 0; i < DDR_MAX_CS; i++) {
  362. u32 base, size;
  363. size = orion_read(DDR_SIZE_CS(i));
  364. base = orion_read(DDR_BASE_CS(i));
  365. if (size & DDR_BANK_EN) {
  366. base = DDR_REG_TO_BASE(base);
  367. size = DDR_REG_TO_SIZE(size);
  368. orion_write(USB_WIN_CTRL(usb_if, i),
  369. ((size-1) & 0xffff0000) |
  370. (ATTR_DDR_CS(i) << 8) |
  371. (TARGET_DDR << 4) | WIN_EN);
  372. orion_write(USB_WIN_BASE(usb_if, i),
  373. base & 0xffff0000);
  374. }
  375. }
  376. }
  377. }
  378. void __init orion_setup_eth_wins(void)
  379. {
  380. int i;
  381. /*
  382. * First, disable and clear windows
  383. */
  384. for (i = 0; i < ETH_MAX_WIN; i++) {
  385. orion_write(ETH_WIN_BASE(i), 0);
  386. orion_write(ETH_WIN_SIZE(i), 0);
  387. orion_setbits(ETH_WIN_EN, 1 << i);
  388. orion_clrbits(ETH_WIN_PROT, 0x3 << (i * 2));
  389. if (i < ETH_MAX_REMAP_WIN)
  390. orion_write(ETH_WIN_REMAP(i), 0);
  391. }
  392. /*
  393. * Setup windows for DDR banks.
  394. */
  395. for (i = 0; i < DDR_MAX_CS; i++) {
  396. u32 base, size;
  397. size = orion_read(DDR_SIZE_CS(i));
  398. base = orion_read(DDR_BASE_CS(i));
  399. if (size & DDR_BANK_EN) {
  400. base = DDR_REG_TO_BASE(base);
  401. size = DDR_REG_TO_SIZE(size);
  402. orion_write(ETH_WIN_SIZE(i), (size-1) & 0xffff0000);
  403. orion_write(ETH_WIN_BASE(i), (base & 0xffff0000) |
  404. (ATTR_DDR_CS(i) << 8) |
  405. TARGET_DDR);
  406. orion_clrbits(ETH_WIN_EN, 1 << i);
  407. orion_setbits(ETH_WIN_PROT, 0x3 << (i * 2));
  408. }
  409. }
  410. }
  411. void __init orion_setup_sata_wins(void)
  412. {
  413. int i;
  414. /*
  415. * First, disable and clear windows
  416. */
  417. for (i = 0; i < SATA_MAX_WIN; i++) {
  418. orion_write(SATA_WIN_BASE(i), 0);
  419. orion_write(SATA_WIN_CTRL(i), 0);
  420. }
  421. /*
  422. * Setup windows for DDR banks.
  423. */
  424. for (i = 0; i < DDR_MAX_CS; i++) {
  425. u32 base, size;
  426. size = orion_read(DDR_SIZE_CS(i));
  427. base = orion_read(DDR_BASE_CS(i));
  428. if (size & DDR_BANK_EN) {
  429. base = DDR_REG_TO_BASE(base);
  430. size = DDR_REG_TO_SIZE(size);
  431. orion_write(SATA_WIN_CTRL(i),
  432. ((size-1) & 0xffff0000) |
  433. (ATTR_DDR_CS(i) << 8) |
  434. (TARGET_DDR << 4) | WIN_EN);
  435. orion_write(SATA_WIN_BASE(i),
  436. base & 0xffff0000);
  437. }
  438. }
  439. }