addr-map.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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) (((n) < 5) ? \
  94. ORION_PCIE_REG(0x1820 + ((n) << 4)) : \
  95. ORION_PCIE_REG(0x1880))
  96. #define PCIE_WIN_BASE(n) (((n) < 5) ? \
  97. ORION_PCIE_REG(0x1824 + ((n) << 4)) : \
  98. ORION_PCIE_REG(0x1884))
  99. #define PCIE_WIN_REMAP(n) (((n) < 5) ? \
  100. ORION_PCIE_REG(0x182c + ((n) << 4)) : \
  101. ORION_PCIE_REG(0x188c))
  102. #define PCIE_DEFWIN_CTRL ORION_PCIE_REG(0x18b0)
  103. #define PCIE_EXPROM_WIN_CTRL ORION_PCIE_REG(0x18c0)
  104. #define PCIE_EXPROM_WIN_REMP ORION_PCIE_REG(0x18c4)
  105. #define PCIE_MAX_BARS 3
  106. #define PCIE_MAX_WINS 6
  107. /*
  108. * Use PCIE BAR '1' for all DDR banks
  109. */
  110. #define PCIE_DRAM_BAR 1
  111. /*
  112. * PCI Address Decode Windows registers
  113. */
  114. #define PCI_BAR_SIZE_DDR_CS(n) (((n) == 0) ? ORION_PCI_REG(0xc08) : \
  115. ((n) == 1) ? ORION_PCI_REG(0xd08) : \
  116. ((n) == 2) ? ORION_PCI_REG(0xc0c) : \
  117. ((n) == 3) ? ORION_PCI_REG(0xd0c) : 0)
  118. #define PCI_BAR_REMAP_DDR_CS(n) (((n) ==0) ? ORION_PCI_REG(0xc48) : \
  119. ((n) == 1) ? ORION_PCI_REG(0xd48) : \
  120. ((n) == 2) ? ORION_PCI_REG(0xc4c) : \
  121. ((n) == 3) ? ORION_PCI_REG(0xd4c) : 0)
  122. #define PCI_BAR_ENABLE ORION_PCI_REG(0xc3c)
  123. #define PCI_CTRL_BASE_LO(n) ORION_PCI_REG(0x1e00 | ((n) << 4))
  124. #define PCI_CTRL_BASE_HI(n) ORION_PCI_REG(0x1e04 | ((n) << 4))
  125. #define PCI_CTRL_SIZE(n) ORION_PCI_REG(0x1e08 | ((n) << 4))
  126. #define PCI_ADDR_DECODE_CTRL ORION_PCI_REG(0xd3c)
  127. /*
  128. * PCI configuration heleprs for BAR settings
  129. */
  130. #define PCI_CONF_FUNC_BAR_CS(n) ((n) >> 1)
  131. #define PCI_CONF_REG_BAR_LO_CS(n) (((n) & 1) ? 0x18 : 0x10)
  132. #define PCI_CONF_REG_BAR_HI_CS(n) (((n) & 1) ? 0x1c : 0x14)
  133. /*
  134. * Gigabit Ethernet Address Decode Windows registers
  135. */
  136. #define ETH_WIN_BASE(win) ORION_ETH_REG(0x200 + ((win) * 8))
  137. #define ETH_WIN_SIZE(win) ORION_ETH_REG(0x204 + ((win) * 8))
  138. #define ETH_WIN_REMAP(win) ORION_ETH_REG(0x280 + ((win) * 4))
  139. #define ETH_WIN_EN ORION_ETH_REG(0x290)
  140. #define ETH_WIN_PROT ORION_ETH_REG(0x294)
  141. #define ETH_MAX_WIN 6
  142. #define ETH_MAX_REMAP_WIN 4
  143. /*
  144. * USB Address Decode Windows registers
  145. */
  146. #define USB_WIN_CTRL(i, w) ((i == 0) ? ORION_USB0_REG(0x320 + ((w) << 4)) \
  147. : ORION_USB1_REG(0x320 + ((w) << 4)))
  148. #define USB_WIN_BASE(i, w) ((i == 0) ? ORION_USB0_REG(0x324 + ((w) << 4)) \
  149. : ORION_USB1_REG(0x324 + ((w) << 4)))
  150. #define USB_MAX_WIN 4
  151. /*
  152. * SATA Address Decode Windows registers
  153. */
  154. #define SATA_WIN_CTRL(win) ORION_SATA_REG(0x30 + ((win) * 0x10))
  155. #define SATA_WIN_BASE(win) ORION_SATA_REG(0x34 + ((win) * 0x10))
  156. #define SATA_MAX_WIN 4
  157. static int __init orion_cpu_win_can_remap(u32 win)
  158. {
  159. u32 dev, rev;
  160. orion_pcie_id(&dev, &rev);
  161. if ((dev == MV88F5281_DEV_ID && win < 4)
  162. || (dev == MV88F5182_DEV_ID && win < 2)
  163. || (dev == MV88F5181_DEV_ID && win < 2))
  164. return 1;
  165. return 0;
  166. }
  167. void __init orion_setup_cpu_win(enum orion_target target, u32 base, u32 size, int remap)
  168. {
  169. u32 win, attr, ctrl;
  170. switch (target) {
  171. case ORION_PCIE_IO:
  172. target = TARGET_PCIE;
  173. attr = ATTR_PCIE_IO;
  174. win = CPU_WIN_PCIE_IO;
  175. break;
  176. case ORION_PCI_IO:
  177. target = TARGET_PCI;
  178. attr = ATTR_PCI_IO;
  179. win = CPU_WIN_PCI_IO;
  180. break;
  181. case ORION_PCIE_MEM:
  182. target = TARGET_PCIE;
  183. attr = ATTR_PCIE_MEM;
  184. win = CPU_WIN_PCIE_MEM;
  185. break;
  186. case ORION_PCI_MEM:
  187. target = TARGET_PCI;
  188. attr = ATTR_PCI_MEM;
  189. win = CPU_WIN_PCI_MEM;
  190. break;
  191. case ORION_DEV_BOOT:
  192. target = TARGET_DEV_BUS;
  193. attr = ATTR_DEV_BOOT;
  194. win = CPU_WIN_DEV_BOOT;
  195. break;
  196. case ORION_DEV0:
  197. target = TARGET_DEV_BUS;
  198. attr = ATTR_DEV_CS0;
  199. win = CPU_WIN_DEV_CS0;
  200. break;
  201. case ORION_DEV1:
  202. target = TARGET_DEV_BUS;
  203. attr = ATTR_DEV_CS1;
  204. win = CPU_WIN_DEV_CS1;
  205. break;
  206. case ORION_DEV2:
  207. target = TARGET_DEV_BUS;
  208. attr = ATTR_DEV_CS2;
  209. win = CPU_WIN_DEV_CS2;
  210. break;
  211. case ORION_DDR:
  212. case ORION_REGS:
  213. /*
  214. * Must be mapped by bootloader.
  215. */
  216. default:
  217. target = attr = win = -1;
  218. BUG();
  219. }
  220. base &= 0xffff0000;
  221. ctrl = (((size - 1) & 0xffff0000) | (attr << 8) |
  222. (target << 4) | WIN_EN);
  223. orion_write(CPU_WIN_BASE(win), base);
  224. orion_write(CPU_WIN_CTRL(win), ctrl);
  225. if (orion_cpu_win_can_remap(win)) {
  226. if (remap >= 0) {
  227. orion_write(CPU_WIN_REMAP_LO(win), remap & 0xffff0000);
  228. orion_write(CPU_WIN_REMAP_HI(win), 0);
  229. } else {
  230. orion_write(CPU_WIN_REMAP_LO(win), base);
  231. orion_write(CPU_WIN_REMAP_HI(win), 0);
  232. }
  233. }
  234. }
  235. void __init orion_setup_cpu_wins(void)
  236. {
  237. int i;
  238. /*
  239. * First, disable and clear windows
  240. */
  241. for (i = 0; i < CPU_MAX_WIN; i++) {
  242. orion_write(CPU_WIN_BASE(i), 0);
  243. orion_write(CPU_WIN_CTRL(i), 0);
  244. if (orion_cpu_win_can_remap(i)) {
  245. orion_write(CPU_WIN_REMAP_LO(i), 0);
  246. orion_write(CPU_WIN_REMAP_HI(i), 0);
  247. }
  248. }
  249. /*
  250. * Setup windows for PCI+PCIe IO+MEM space.
  251. */
  252. orion_setup_cpu_win(ORION_PCIE_IO, ORION_PCIE_IO_PHYS_BASE,
  253. ORION_PCIE_IO_SIZE, ORION_PCIE_IO_BUS_BASE);
  254. orion_setup_cpu_win(ORION_PCI_IO, ORION_PCI_IO_PHYS_BASE,
  255. ORION_PCI_IO_SIZE, ORION_PCI_IO_BUS_BASE);
  256. orion_setup_cpu_win(ORION_PCIE_MEM, ORION_PCIE_MEM_PHYS_BASE,
  257. ORION_PCIE_MEM_SIZE, -1);
  258. orion_setup_cpu_win(ORION_PCI_MEM, ORION_PCI_MEM_PHYS_BASE,
  259. ORION_PCI_MEM_SIZE, -1);
  260. }
  261. /*
  262. * Setup PCIE BARs and Address Decode Wins:
  263. * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
  264. * WIN[0-3] -> DRAM bank[0-3]
  265. */
  266. void __init orion_setup_pcie_wins(void)
  267. {
  268. u32 base, size, i;
  269. /*
  270. * First, disable and clear BARs and windows
  271. */
  272. for (i = 1; i < PCIE_MAX_BARS; i++) {
  273. orion_write(PCIE_BAR_CTRL(i), 0);
  274. orion_write(PCIE_BAR_LO(i), 0);
  275. orion_write(PCIE_BAR_HI(i), 0);
  276. }
  277. for (i = 0; i < PCIE_MAX_WINS; i++) {
  278. orion_write(PCIE_WIN_CTRL(i), 0);
  279. orion_write(PCIE_WIN_BASE(i), 0);
  280. orion_write(PCIE_WIN_REMAP(i), 0);
  281. }
  282. /*
  283. * Setup windows for DDR banks. Count total DDR size on the fly.
  284. */
  285. base = DDR_REG_TO_BASE(orion_read(DDR_BASE_CS(0)));
  286. size = 0;
  287. for (i = 0; i < DDR_MAX_CS; i++) {
  288. u32 bank_base, bank_size;
  289. bank_size = orion_read(DDR_SIZE_CS(i));
  290. bank_base = orion_read(DDR_BASE_CS(i));
  291. if (bank_size & DDR_BANK_EN) {
  292. bank_size = DDR_REG_TO_SIZE(bank_size);
  293. bank_base = DDR_REG_TO_BASE(bank_base);
  294. orion_write(PCIE_WIN_BASE(i), bank_base & 0xffff0000);
  295. orion_write(PCIE_WIN_REMAP(i), 0);
  296. orion_write(PCIE_WIN_CTRL(i),
  297. ((bank_size-1) & 0xffff0000) |
  298. (ATTR_DDR_CS(i) << 8) |
  299. (TARGET_DDR << 4) |
  300. (PCIE_DRAM_BAR << 1) | WIN_EN);
  301. size += bank_size;
  302. }
  303. }
  304. /*
  305. * Setup BAR[1] to all DRAM banks
  306. */
  307. orion_write(PCIE_BAR_LO(PCIE_DRAM_BAR), base & 0xffff0000);
  308. orion_write(PCIE_BAR_HI(PCIE_DRAM_BAR), 0);
  309. orion_write(PCIE_BAR_CTRL(PCIE_DRAM_BAR),
  310. ((size - 1) & 0xffff0000) | WIN_EN);
  311. }
  312. void __init orion_setup_pci_wins(void)
  313. {
  314. u32 base, size, i;
  315. /*
  316. * First, disable windows
  317. */
  318. orion_write(PCI_BAR_ENABLE, 0xffffffff);
  319. /*
  320. * Setup windows for DDR banks.
  321. */
  322. for (i = 0; i < DDR_MAX_CS; i++) {
  323. base = orion_read(DDR_BASE_CS(i));
  324. size = orion_read(DDR_SIZE_CS(i));
  325. if (size & DDR_BANK_EN) {
  326. u32 bus, dev, func, reg, val;
  327. size = DDR_REG_TO_SIZE(size);
  328. base = DDR_REG_TO_BASE(base);
  329. bus = orion_pci_local_bus_nr();
  330. dev = orion_pci_local_dev_nr();
  331. func = PCI_CONF_FUNC_BAR_CS(i);
  332. reg = PCI_CONF_REG_BAR_LO_CS(i);
  333. orion_pci_hw_rd_conf(bus, dev, func, reg, 4, &val);
  334. orion_pci_hw_wr_conf(bus, dev, func, reg, 4,
  335. (base & 0xfffff000) | (val & 0xfff));
  336. reg = PCI_CONF_REG_BAR_HI_CS(i);
  337. orion_pci_hw_wr_conf(bus, dev, func, reg, 4, 0);
  338. orion_write(PCI_BAR_SIZE_DDR_CS(i),
  339. (size - 1) & 0xfffff000);
  340. orion_write(PCI_BAR_REMAP_DDR_CS(i),
  341. base & 0xfffff000);
  342. orion_clrbits(PCI_BAR_ENABLE, (1 << i));
  343. }
  344. }
  345. /*
  346. * Disable automatic update of address remaping when writing to BARs
  347. */
  348. orion_setbits(PCI_ADDR_DECODE_CTRL, 1);
  349. }
  350. void __init orion_setup_usb_wins(void)
  351. {
  352. int i;
  353. u32 usb_if, dev, rev;
  354. u32 max_usb_if = 1;
  355. orion_pcie_id(&dev, &rev);
  356. if (dev == MV88F5182_DEV_ID)
  357. max_usb_if = 2;
  358. for (usb_if = 0; usb_if < max_usb_if; usb_if++) {
  359. /*
  360. * First, disable and clear windows
  361. */
  362. for (i = 0; i < USB_MAX_WIN; i++) {
  363. orion_write(USB_WIN_BASE(usb_if, i), 0);
  364. orion_write(USB_WIN_CTRL(usb_if, i), 0);
  365. }
  366. /*
  367. * Setup windows for DDR banks.
  368. */
  369. for (i = 0; i < DDR_MAX_CS; i++) {
  370. u32 base, size;
  371. size = orion_read(DDR_SIZE_CS(i));
  372. base = orion_read(DDR_BASE_CS(i));
  373. if (size & DDR_BANK_EN) {
  374. base = DDR_REG_TO_BASE(base);
  375. size = DDR_REG_TO_SIZE(size);
  376. orion_write(USB_WIN_CTRL(usb_if, i),
  377. ((size-1) & 0xffff0000) |
  378. (ATTR_DDR_CS(i) << 8) |
  379. (TARGET_DDR << 4) | WIN_EN);
  380. orion_write(USB_WIN_BASE(usb_if, i),
  381. base & 0xffff0000);
  382. }
  383. }
  384. }
  385. }
  386. void __init orion_setup_eth_wins(void)
  387. {
  388. int i;
  389. /*
  390. * First, disable and clear windows
  391. */
  392. for (i = 0; i < ETH_MAX_WIN; i++) {
  393. orion_write(ETH_WIN_BASE(i), 0);
  394. orion_write(ETH_WIN_SIZE(i), 0);
  395. orion_setbits(ETH_WIN_EN, 1 << i);
  396. orion_clrbits(ETH_WIN_PROT, 0x3 << (i * 2));
  397. if (i < ETH_MAX_REMAP_WIN)
  398. orion_write(ETH_WIN_REMAP(i), 0);
  399. }
  400. /*
  401. * Setup windows for DDR banks.
  402. */
  403. for (i = 0; i < DDR_MAX_CS; i++) {
  404. u32 base, size;
  405. size = orion_read(DDR_SIZE_CS(i));
  406. base = orion_read(DDR_BASE_CS(i));
  407. if (size & DDR_BANK_EN) {
  408. base = DDR_REG_TO_BASE(base);
  409. size = DDR_REG_TO_SIZE(size);
  410. orion_write(ETH_WIN_SIZE(i), (size-1) & 0xffff0000);
  411. orion_write(ETH_WIN_BASE(i), (base & 0xffff0000) |
  412. (ATTR_DDR_CS(i) << 8) |
  413. TARGET_DDR);
  414. orion_clrbits(ETH_WIN_EN, 1 << i);
  415. orion_setbits(ETH_WIN_PROT, 0x3 << (i * 2));
  416. }
  417. }
  418. }
  419. void __init orion_setup_sata_wins(void)
  420. {
  421. int i;
  422. /*
  423. * First, disable and clear windows
  424. */
  425. for (i = 0; i < SATA_MAX_WIN; i++) {
  426. orion_write(SATA_WIN_BASE(i), 0);
  427. orion_write(SATA_WIN_CTRL(i), 0);
  428. }
  429. /*
  430. * Setup windows for DDR banks.
  431. */
  432. for (i = 0; i < DDR_MAX_CS; i++) {
  433. u32 base, size;
  434. size = orion_read(DDR_SIZE_CS(i));
  435. base = orion_read(DDR_BASE_CS(i));
  436. if (size & DDR_BANK_EN) {
  437. base = DDR_REG_TO_BASE(base);
  438. size = DDR_REG_TO_SIZE(size);
  439. orion_write(SATA_WIN_CTRL(i),
  440. ((size-1) & 0xffff0000) |
  441. (ATTR_DDR_CS(i) << 8) |
  442. (TARGET_DDR << 4) | WIN_EN);
  443. orion_write(SATA_WIN_BASE(i),
  444. base & 0xffff0000);
  445. }
  446. }
  447. }