addr-map.c 14 KB

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