addr-map.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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)
  156. || (dev == MV88F5182_DEV_ID && win < 2)
  157. || (dev == MV88F5181_DEV_ID && win < 2))
  158. return 1;
  159. return 0;
  160. }
  161. void __init orion_setup_cpu_win(enum orion_target target, u32 base, u32 size, int remap)
  162. {
  163. u32 win, attr, ctrl;
  164. switch (target) {
  165. case ORION_PCIE_IO:
  166. target = TARGET_PCIE;
  167. attr = ATTR_PCIE_IO;
  168. win = CPU_WIN_PCIE_IO;
  169. break;
  170. case ORION_PCI_IO:
  171. target = TARGET_PCI;
  172. attr = ATTR_PCI_IO;
  173. win = CPU_WIN_PCI_IO;
  174. break;
  175. case ORION_PCIE_MEM:
  176. target = TARGET_PCIE;
  177. attr = ATTR_PCIE_MEM;
  178. win = CPU_WIN_PCIE_MEM;
  179. break;
  180. case ORION_PCI_MEM:
  181. target = TARGET_PCI;
  182. attr = ATTR_PCI_MEM;
  183. win = CPU_WIN_PCI_MEM;
  184. break;
  185. case ORION_DEV_BOOT:
  186. target = TARGET_DEV_BUS;
  187. attr = ATTR_DEV_BOOT;
  188. win = CPU_WIN_DEV_BOOT;
  189. break;
  190. case ORION_DEV0:
  191. target = TARGET_DEV_BUS;
  192. attr = ATTR_DEV_CS0;
  193. win = CPU_WIN_DEV_CS0;
  194. break;
  195. case ORION_DEV1:
  196. target = TARGET_DEV_BUS;
  197. attr = ATTR_DEV_CS1;
  198. win = CPU_WIN_DEV_CS1;
  199. break;
  200. case ORION_DEV2:
  201. target = TARGET_DEV_BUS;
  202. attr = ATTR_DEV_CS2;
  203. win = CPU_WIN_DEV_CS2;
  204. break;
  205. case ORION_DDR:
  206. case ORION_REGS:
  207. /*
  208. * Must be mapped by bootloader.
  209. */
  210. default:
  211. target = attr = win = -1;
  212. BUG();
  213. }
  214. base &= 0xffff0000;
  215. ctrl = (((size - 1) & 0xffff0000) | (attr << 8) |
  216. (target << 4) | WIN_EN);
  217. orion_write(CPU_WIN_BASE(win), base);
  218. orion_write(CPU_WIN_CTRL(win), ctrl);
  219. if (orion_cpu_win_can_remap(win)) {
  220. if (remap >= 0) {
  221. orion_write(CPU_WIN_REMAP_LO(win), remap & 0xffff0000);
  222. orion_write(CPU_WIN_REMAP_HI(win), 0);
  223. } else {
  224. orion_write(CPU_WIN_REMAP_LO(win), base);
  225. orion_write(CPU_WIN_REMAP_HI(win), 0);
  226. }
  227. }
  228. }
  229. void __init orion_setup_cpu_wins(void)
  230. {
  231. int i;
  232. /*
  233. * First, disable and clear windows
  234. */
  235. for (i = 0; i < CPU_MAX_WIN; i++) {
  236. orion_write(CPU_WIN_BASE(i), 0);
  237. orion_write(CPU_WIN_CTRL(i), 0);
  238. if (orion_cpu_win_can_remap(i)) {
  239. orion_write(CPU_WIN_REMAP_LO(i), 0);
  240. orion_write(CPU_WIN_REMAP_HI(i), 0);
  241. }
  242. }
  243. /*
  244. * Setup windows for PCI+PCIe IO+MEM space.
  245. */
  246. orion_setup_cpu_win(ORION_PCIE_IO, ORION_PCIE_IO_PHYS_BASE,
  247. ORION_PCIE_IO_SIZE, ORION_PCIE_IO_BUS_BASE);
  248. orion_setup_cpu_win(ORION_PCI_IO, ORION_PCI_IO_PHYS_BASE,
  249. ORION_PCI_IO_SIZE, ORION_PCI_IO_BUS_BASE);
  250. orion_setup_cpu_win(ORION_PCIE_MEM, ORION_PCIE_MEM_PHYS_BASE,
  251. ORION_PCIE_MEM_SIZE, -1);
  252. orion_setup_cpu_win(ORION_PCI_MEM, ORION_PCI_MEM_PHYS_BASE,
  253. ORION_PCI_MEM_SIZE, -1);
  254. }
  255. /*
  256. * Setup PCIE BARs and Address Decode Wins:
  257. * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
  258. * WIN[0-3] -> DRAM bank[0-3]
  259. */
  260. void __init orion_setup_pcie_wins(void)
  261. {
  262. u32 base, size, i;
  263. /*
  264. * First, disable and clear BARs and windows
  265. */
  266. for (i = 1; i < PCIE_MAX_BARS; i++) {
  267. orion_write(PCIE_BAR_CTRL(i), 0);
  268. orion_write(PCIE_BAR_LO(i), 0);
  269. orion_write(PCIE_BAR_HI(i), 0);
  270. }
  271. for (i = 0; i < PCIE_MAX_WINS; i++) {
  272. orion_write(PCIE_WIN_CTRL(i), 0);
  273. orion_write(PCIE_WIN_BASE(i), 0);
  274. orion_write(PCIE_WIN_REMAP(i), 0);
  275. }
  276. /*
  277. * Setup windows for DDR banks. Count total DDR size on the fly.
  278. */
  279. base = DDR_REG_TO_BASE(orion_read(DDR_BASE_CS(0)));
  280. size = 0;
  281. for (i = 0; i < DDR_MAX_CS; i++) {
  282. u32 bank_base, bank_size;
  283. bank_size = orion_read(DDR_SIZE_CS(i));
  284. bank_base = orion_read(DDR_BASE_CS(i));
  285. if (bank_size & DDR_BANK_EN) {
  286. bank_size = DDR_REG_TO_SIZE(bank_size);
  287. bank_base = DDR_REG_TO_BASE(bank_base);
  288. orion_write(PCIE_WIN_BASE(i), bank_base & 0xffff0000);
  289. orion_write(PCIE_WIN_REMAP(i), 0);
  290. orion_write(PCIE_WIN_CTRL(i),
  291. ((bank_size-1) & 0xffff0000) |
  292. (ATTR_DDR_CS(i) << 8) |
  293. (TARGET_DDR << 4) |
  294. (PCIE_DRAM_BAR << 1) | WIN_EN);
  295. size += bank_size;
  296. }
  297. }
  298. /*
  299. * Setup BAR[1] to all DRAM banks
  300. */
  301. orion_write(PCIE_BAR_LO(PCIE_DRAM_BAR), base & 0xffff0000);
  302. orion_write(PCIE_BAR_HI(PCIE_DRAM_BAR), 0);
  303. orion_write(PCIE_BAR_CTRL(PCIE_DRAM_BAR),
  304. ((size - 1) & 0xffff0000) | WIN_EN);
  305. }
  306. void __init orion_setup_pci_wins(void)
  307. {
  308. u32 base, size, i;
  309. /*
  310. * First, disable windows
  311. */
  312. orion_write(PCI_BAR_ENABLE, 0xffffffff);
  313. /*
  314. * Setup windows for DDR banks.
  315. */
  316. for (i = 0; i < DDR_MAX_CS; i++) {
  317. base = orion_read(DDR_BASE_CS(i));
  318. size = orion_read(DDR_SIZE_CS(i));
  319. if (size & DDR_BANK_EN) {
  320. u32 bus, dev, func, reg, val;
  321. size = DDR_REG_TO_SIZE(size);
  322. base = DDR_REG_TO_BASE(base);
  323. bus = orion_pci_local_bus_nr();
  324. dev = orion_pci_local_dev_nr();
  325. func = PCI_CONF_FUNC_BAR_CS(i);
  326. reg = PCI_CONF_REG_BAR_LO_CS(i);
  327. orion_pci_hw_rd_conf(bus, dev, func, reg, 4, &val);
  328. orion_pci_hw_wr_conf(bus, dev, func, reg, 4,
  329. (base & 0xfffff000) | (val & 0xfff));
  330. reg = PCI_CONF_REG_BAR_HI_CS(i);
  331. orion_pci_hw_wr_conf(bus, dev, func, reg, 4, 0);
  332. orion_write(PCI_BAR_SIZE_DDR_CS(i),
  333. (size - 1) & 0xfffff000);
  334. orion_write(PCI_BAR_REMAP_DDR_CS(i),
  335. base & 0xfffff000);
  336. orion_clrbits(PCI_BAR_ENABLE, (1 << i));
  337. }
  338. }
  339. /*
  340. * Disable automatic update of address remaping when writing to BARs
  341. */
  342. orion_setbits(PCI_ADDR_DECODE_CTRL, 1);
  343. }
  344. void __init orion_setup_usb_wins(void)
  345. {
  346. int i;
  347. u32 usb_if, dev, rev;
  348. u32 max_usb_if = 1;
  349. orion_pcie_id(&dev, &rev);
  350. if (dev == MV88F5182_DEV_ID)
  351. max_usb_if = 2;
  352. for (usb_if = 0; usb_if < max_usb_if; usb_if++) {
  353. /*
  354. * First, disable and clear windows
  355. */
  356. for (i = 0; i < USB_MAX_WIN; i++) {
  357. orion_write(USB_WIN_BASE(usb_if, i), 0);
  358. orion_write(USB_WIN_CTRL(usb_if, i), 0);
  359. }
  360. /*
  361. * Setup windows for DDR banks.
  362. */
  363. for (i = 0; i < DDR_MAX_CS; i++) {
  364. u32 base, size;
  365. size = orion_read(DDR_SIZE_CS(i));
  366. base = orion_read(DDR_BASE_CS(i));
  367. if (size & DDR_BANK_EN) {
  368. base = DDR_REG_TO_BASE(base);
  369. size = DDR_REG_TO_SIZE(size);
  370. orion_write(USB_WIN_CTRL(usb_if, i),
  371. ((size-1) & 0xffff0000) |
  372. (ATTR_DDR_CS(i) << 8) |
  373. (TARGET_DDR << 4) | WIN_EN);
  374. orion_write(USB_WIN_BASE(usb_if, i),
  375. base & 0xffff0000);
  376. }
  377. }
  378. }
  379. }
  380. void __init orion_setup_eth_wins(void)
  381. {
  382. int i;
  383. /*
  384. * First, disable and clear windows
  385. */
  386. for (i = 0; i < ETH_MAX_WIN; i++) {
  387. orion_write(ETH_WIN_BASE(i), 0);
  388. orion_write(ETH_WIN_SIZE(i), 0);
  389. orion_setbits(ETH_WIN_EN, 1 << i);
  390. orion_clrbits(ETH_WIN_PROT, 0x3 << (i * 2));
  391. if (i < ETH_MAX_REMAP_WIN)
  392. orion_write(ETH_WIN_REMAP(i), 0);
  393. }
  394. /*
  395. * Setup windows for DDR banks.
  396. */
  397. for (i = 0; i < DDR_MAX_CS; i++) {
  398. u32 base, size;
  399. size = orion_read(DDR_SIZE_CS(i));
  400. base = orion_read(DDR_BASE_CS(i));
  401. if (size & DDR_BANK_EN) {
  402. base = DDR_REG_TO_BASE(base);
  403. size = DDR_REG_TO_SIZE(size);
  404. orion_write(ETH_WIN_SIZE(i), (size-1) & 0xffff0000);
  405. orion_write(ETH_WIN_BASE(i), (base & 0xffff0000) |
  406. (ATTR_DDR_CS(i) << 8) |
  407. TARGET_DDR);
  408. orion_clrbits(ETH_WIN_EN, 1 << i);
  409. orion_setbits(ETH_WIN_PROT, 0x3 << (i * 2));
  410. }
  411. }
  412. }
  413. void __init orion_setup_sata_wins(void)
  414. {
  415. int i;
  416. /*
  417. * First, disable and clear windows
  418. */
  419. for (i = 0; i < SATA_MAX_WIN; i++) {
  420. orion_write(SATA_WIN_BASE(i), 0);
  421. orion_write(SATA_WIN_CTRL(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(SATA_WIN_CTRL(i),
  434. ((size-1) & 0xffff0000) |
  435. (ATTR_DDR_CS(i) << 8) |
  436. (TARGET_DDR << 4) | WIN_EN);
  437. orion_write(SATA_WIN_BASE(i),
  438. base & 0xffff0000);
  439. }
  440. }
  441. }