mbus.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * Address map functions for Marvell EBU SoCs (Kirkwood, Armada
  3. * 370/XP, Dove, Orion5x and MV78xx0)
  4. *
  5. * Ported from the Barebox version to U-Boot by:
  6. * Stefan Roese <sr@denx.de>
  7. *
  8. * The Barebox version is:
  9. * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
  10. *
  11. * based on mbus driver from Linux
  12. * (C) Copyright 2008 Marvell Semiconductor
  13. *
  14. * SPDX-License-Identifier: GPL-2.0
  15. *
  16. * The Marvell EBU SoCs have a configurable physical address space:
  17. * the physical address at which certain devices (PCIe, NOR, NAND,
  18. * etc.) sit can be configured. The configuration takes place through
  19. * two sets of registers:
  20. *
  21. * - One to configure the access of the CPU to the devices. Depending
  22. * on the families, there are between 8 and 20 configurable windows,
  23. * each can be use to create a physical memory window that maps to a
  24. * specific device. Devices are identified by a tuple (target,
  25. * attribute).
  26. *
  27. * - One to configure the access to the CPU to the SDRAM. There are
  28. * either 2 (for Dove) or 4 (for other families) windows to map the
  29. * SDRAM into the physical address space.
  30. *
  31. * This driver:
  32. *
  33. * - Reads out the SDRAM address decoding windows at initialization
  34. * time, and fills the mbus_dram_info structure with these
  35. * informations. The exported function mv_mbus_dram_info() allow
  36. * device drivers to get those informations related to the SDRAM
  37. * address decoding windows. This is because devices also have their
  38. * own windows (configured through registers that are part of each
  39. * device register space), and therefore the drivers for Marvell
  40. * devices have to configure those device -> SDRAM windows to ensure
  41. * that DMA works properly.
  42. *
  43. * - Provides an API for platform code or device drivers to
  44. * dynamically add or remove address decoding windows for the CPU ->
  45. * device accesses. This API is mvebu_mbus_add_window_by_id(),
  46. * mvebu_mbus_add_window_remap_by_id() and
  47. * mvebu_mbus_del_window().
  48. */
  49. #include <common.h>
  50. #include <linux/errno.h>
  51. #include <asm/io.h>
  52. #include <asm/arch/cpu.h>
  53. #include <asm/arch/soc.h>
  54. #include <linux/log2.h>
  55. #include <linux/mbus.h>
  56. /* DDR target is the same on all platforms */
  57. #define TARGET_DDR 0
  58. /* CPU Address Decode Windows registers */
  59. #define WIN_CTRL_OFF 0x0000
  60. #define WIN_CTRL_ENABLE BIT(0)
  61. #define WIN_CTRL_TGT_MASK 0xf0
  62. #define WIN_CTRL_TGT_SHIFT 4
  63. #define WIN_CTRL_ATTR_MASK 0xff00
  64. #define WIN_CTRL_ATTR_SHIFT 8
  65. #define WIN_CTRL_SIZE_MASK 0xffff0000
  66. #define WIN_CTRL_SIZE_SHIFT 16
  67. #define WIN_BASE_OFF 0x0004
  68. #define WIN_BASE_LOW 0xffff0000
  69. #define WIN_BASE_HIGH 0xf
  70. #define WIN_REMAP_LO_OFF 0x0008
  71. #define WIN_REMAP_LOW 0xffff0000
  72. #define WIN_REMAP_HI_OFF 0x000c
  73. #define ATTR_HW_COHERENCY (0x1 << 4)
  74. #define DDR_BASE_CS_OFF(n) (0x0000 + ((n) << 3))
  75. #define DDR_BASE_CS_HIGH_MASK 0xf
  76. #define DDR_BASE_CS_LOW_MASK 0xff000000
  77. #define DDR_SIZE_CS_OFF(n) (0x0004 + ((n) << 3))
  78. #define DDR_SIZE_ENABLED BIT(0)
  79. #define DDR_SIZE_CS_MASK 0x1c
  80. #define DDR_SIZE_CS_SHIFT 2
  81. #define DDR_SIZE_MASK 0xff000000
  82. #define DOVE_DDR_BASE_CS_OFF(n) ((n) << 4)
  83. struct mvebu_mbus_state;
  84. struct mvebu_mbus_soc_data {
  85. unsigned int num_wins;
  86. unsigned int num_remappable_wins;
  87. unsigned int (*win_cfg_offset)(const int win);
  88. void (*setup_cpu_target)(struct mvebu_mbus_state *s);
  89. };
  90. struct mvebu_mbus_state mbus_state
  91. __attribute__ ((section(".data")));
  92. static struct mbus_dram_target_info mbus_dram_info
  93. __attribute__ ((section(".data")));
  94. /*
  95. * Functions to manipulate the address decoding windows
  96. */
  97. static void mvebu_mbus_read_window(struct mvebu_mbus_state *mbus,
  98. int win, int *enabled, u64 *base,
  99. u32 *size, u8 *target, u8 *attr,
  100. u64 *remap)
  101. {
  102. void __iomem *addr = mbus->mbuswins_base +
  103. mbus->soc->win_cfg_offset(win);
  104. u32 basereg = readl(addr + WIN_BASE_OFF);
  105. u32 ctrlreg = readl(addr + WIN_CTRL_OFF);
  106. if (!(ctrlreg & WIN_CTRL_ENABLE)) {
  107. *enabled = 0;
  108. return;
  109. }
  110. *enabled = 1;
  111. *base = ((u64)basereg & WIN_BASE_HIGH) << 32;
  112. *base |= (basereg & WIN_BASE_LOW);
  113. *size = (ctrlreg | ~WIN_CTRL_SIZE_MASK) + 1;
  114. if (target)
  115. *target = (ctrlreg & WIN_CTRL_TGT_MASK) >> WIN_CTRL_TGT_SHIFT;
  116. if (attr)
  117. *attr = (ctrlreg & WIN_CTRL_ATTR_MASK) >> WIN_CTRL_ATTR_SHIFT;
  118. if (remap) {
  119. if (win < mbus->soc->num_remappable_wins) {
  120. u32 remap_low = readl(addr + WIN_REMAP_LO_OFF);
  121. u32 remap_hi = readl(addr + WIN_REMAP_HI_OFF);
  122. *remap = ((u64)remap_hi << 32) | remap_low;
  123. } else {
  124. *remap = 0;
  125. }
  126. }
  127. }
  128. static void mvebu_mbus_disable_window(struct mvebu_mbus_state *mbus,
  129. int win)
  130. {
  131. void __iomem *addr;
  132. addr = mbus->mbuswins_base + mbus->soc->win_cfg_offset(win);
  133. writel(0, addr + WIN_BASE_OFF);
  134. writel(0, addr + WIN_CTRL_OFF);
  135. if (win < mbus->soc->num_remappable_wins) {
  136. writel(0, addr + WIN_REMAP_LO_OFF);
  137. writel(0, addr + WIN_REMAP_HI_OFF);
  138. }
  139. }
  140. /* Checks whether the given window number is available */
  141. static int mvebu_mbus_window_is_free(struct mvebu_mbus_state *mbus,
  142. const int win)
  143. {
  144. void __iomem *addr = mbus->mbuswins_base +
  145. mbus->soc->win_cfg_offset(win);
  146. u32 ctrl = readl(addr + WIN_CTRL_OFF);
  147. return !(ctrl & WIN_CTRL_ENABLE);
  148. }
  149. /*
  150. * Checks whether the given (base, base+size) area doesn't overlap an
  151. * existing region
  152. */
  153. static int mvebu_mbus_window_conflicts(struct mvebu_mbus_state *mbus,
  154. phys_addr_t base, size_t size,
  155. u8 target, u8 attr)
  156. {
  157. u64 end = (u64)base + size;
  158. int win;
  159. for (win = 0; win < mbus->soc->num_wins; win++) {
  160. u64 wbase, wend;
  161. u32 wsize;
  162. u8 wtarget, wattr;
  163. int enabled;
  164. mvebu_mbus_read_window(mbus, win,
  165. &enabled, &wbase, &wsize,
  166. &wtarget, &wattr, NULL);
  167. if (!enabled)
  168. continue;
  169. wend = wbase + wsize;
  170. /*
  171. * Check if the current window overlaps with the
  172. * proposed physical range
  173. */
  174. if ((u64)base < wend && end > wbase)
  175. return 0;
  176. /*
  177. * Check if target/attribute conflicts
  178. */
  179. if (target == wtarget && attr == wattr)
  180. return 0;
  181. }
  182. return 1;
  183. }
  184. static int mvebu_mbus_find_window(struct mvebu_mbus_state *mbus,
  185. phys_addr_t base, size_t size)
  186. {
  187. int win;
  188. for (win = 0; win < mbus->soc->num_wins; win++) {
  189. u64 wbase;
  190. u32 wsize;
  191. int enabled;
  192. mvebu_mbus_read_window(mbus, win,
  193. &enabled, &wbase, &wsize,
  194. NULL, NULL, NULL);
  195. if (!enabled)
  196. continue;
  197. if (base == wbase && size == wsize)
  198. return win;
  199. }
  200. return -ENODEV;
  201. }
  202. static int mvebu_mbus_setup_window(struct mvebu_mbus_state *mbus,
  203. int win, phys_addr_t base, size_t size,
  204. phys_addr_t remap, u8 target,
  205. u8 attr)
  206. {
  207. void __iomem *addr = mbus->mbuswins_base +
  208. mbus->soc->win_cfg_offset(win);
  209. u32 ctrl, remap_addr;
  210. ctrl = ((size - 1) & WIN_CTRL_SIZE_MASK) |
  211. (attr << WIN_CTRL_ATTR_SHIFT) |
  212. (target << WIN_CTRL_TGT_SHIFT) |
  213. WIN_CTRL_ENABLE;
  214. writel(base & WIN_BASE_LOW, addr + WIN_BASE_OFF);
  215. writel(ctrl, addr + WIN_CTRL_OFF);
  216. if (win < mbus->soc->num_remappable_wins) {
  217. if (remap == MVEBU_MBUS_NO_REMAP)
  218. remap_addr = base;
  219. else
  220. remap_addr = remap;
  221. writel(remap_addr & WIN_REMAP_LOW, addr + WIN_REMAP_LO_OFF);
  222. writel(0, addr + WIN_REMAP_HI_OFF);
  223. }
  224. return 0;
  225. }
  226. static int mvebu_mbus_alloc_window(struct mvebu_mbus_state *mbus,
  227. phys_addr_t base, size_t size,
  228. phys_addr_t remap, u8 target,
  229. u8 attr)
  230. {
  231. int win;
  232. if (remap == MVEBU_MBUS_NO_REMAP) {
  233. for (win = mbus->soc->num_remappable_wins;
  234. win < mbus->soc->num_wins; win++)
  235. if (mvebu_mbus_window_is_free(mbus, win))
  236. return mvebu_mbus_setup_window(mbus, win, base,
  237. size, remap,
  238. target, attr);
  239. }
  240. for (win = 0; win < mbus->soc->num_wins; win++)
  241. if (mvebu_mbus_window_is_free(mbus, win))
  242. return mvebu_mbus_setup_window(mbus, win, base, size,
  243. remap, target, attr);
  244. return -ENOMEM;
  245. }
  246. /*
  247. * SoC-specific functions and definitions
  248. */
  249. static unsigned int armada_370_xp_mbus_win_offset(int win)
  250. {
  251. /* The register layout is a bit annoying and the below code
  252. * tries to cope with it.
  253. * - At offset 0x0, there are the registers for the first 8
  254. * windows, with 4 registers of 32 bits per window (ctrl,
  255. * base, remap low, remap high)
  256. * - Then at offset 0x80, there is a hole of 0x10 bytes for
  257. * the internal registers base address and internal units
  258. * sync barrier register.
  259. * - Then at offset 0x90, there the registers for 12
  260. * windows, with only 2 registers of 32 bits per window
  261. * (ctrl, base).
  262. */
  263. if (win < 8)
  264. return win << 4;
  265. else
  266. return 0x90 + ((win - 8) << 3);
  267. }
  268. static unsigned int orion5x_mbus_win_offset(int win)
  269. {
  270. return win << 4;
  271. }
  272. static void mvebu_mbus_default_setup_cpu_target(struct mvebu_mbus_state *mbus)
  273. {
  274. int i;
  275. int cs;
  276. mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
  277. for (i = 0, cs = 0; i < 4; i++) {
  278. u32 base = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
  279. u32 size = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
  280. /*
  281. * We only take care of entries for which the chip
  282. * select is enabled, and that don't have high base
  283. * address bits set (devices can only access the first
  284. * 32 bits of the memory).
  285. */
  286. if ((size & DDR_SIZE_ENABLED) &&
  287. !(base & DDR_BASE_CS_HIGH_MASK)) {
  288. struct mbus_dram_window *w;
  289. w = &mbus_dram_info.cs[cs++];
  290. w->cs_index = i;
  291. w->mbus_attr = 0xf & ~(1 << i);
  292. w->base = base & DDR_BASE_CS_LOW_MASK;
  293. w->size = (size | ~DDR_SIZE_MASK) + 1;
  294. }
  295. }
  296. mbus_dram_info.num_cs = cs;
  297. }
  298. static const struct mvebu_mbus_soc_data
  299. armada_370_xp_mbus_data __maybe_unused = {
  300. .num_wins = 20,
  301. .num_remappable_wins = 8,
  302. .win_cfg_offset = armada_370_xp_mbus_win_offset,
  303. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  304. };
  305. static const struct mvebu_mbus_soc_data
  306. kirkwood_mbus_data __maybe_unused = {
  307. .num_wins = 8,
  308. .num_remappable_wins = 4,
  309. .win_cfg_offset = orion5x_mbus_win_offset,
  310. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  311. };
  312. /*
  313. * Public API of the driver
  314. */
  315. const struct mbus_dram_target_info *mvebu_mbus_dram_info(void)
  316. {
  317. return &mbus_dram_info;
  318. }
  319. int mvebu_mbus_add_window_remap_by_id(unsigned int target,
  320. unsigned int attribute,
  321. phys_addr_t base, size_t size,
  322. phys_addr_t remap)
  323. {
  324. struct mvebu_mbus_state *s = &mbus_state;
  325. if (!mvebu_mbus_window_conflicts(s, base, size, target, attribute)) {
  326. printf("Cannot add window '%x:%x', conflicts with another window\n",
  327. target, attribute);
  328. return -EINVAL;
  329. }
  330. return mvebu_mbus_alloc_window(s, base, size, remap, target, attribute);
  331. }
  332. int mvebu_mbus_add_window_by_id(unsigned int target, unsigned int attribute,
  333. phys_addr_t base, size_t size)
  334. {
  335. return mvebu_mbus_add_window_remap_by_id(target, attribute, base,
  336. size, MVEBU_MBUS_NO_REMAP);
  337. }
  338. int mvebu_mbus_del_window(phys_addr_t base, size_t size)
  339. {
  340. int win;
  341. win = mvebu_mbus_find_window(&mbus_state, base, size);
  342. if (win < 0)
  343. return win;
  344. mvebu_mbus_disable_window(&mbus_state, win);
  345. return 0;
  346. }
  347. static void mvebu_mbus_get_lowest_base(struct mvebu_mbus_state *mbus,
  348. phys_addr_t *base)
  349. {
  350. int win;
  351. *base = 0xffffffff;
  352. for (win = 0; win < mbus->soc->num_wins; win++) {
  353. u64 wbase;
  354. u32 wsize;
  355. u8 wtarget, wattr;
  356. int enabled;
  357. mvebu_mbus_read_window(mbus, win,
  358. &enabled, &wbase, &wsize,
  359. &wtarget, &wattr, NULL);
  360. if (!enabled)
  361. continue;
  362. if (wbase < *base)
  363. *base = wbase;
  364. }
  365. }
  366. static void mvebu_config_mbus_bridge(struct mvebu_mbus_state *mbus)
  367. {
  368. phys_addr_t base;
  369. u32 val;
  370. u32 size;
  371. /* Set MBUS bridge base/ctrl */
  372. mvebu_mbus_get_lowest_base(&mbus_state, &base);
  373. size = 0xffffffff - base + 1;
  374. if (!is_power_of_2(size)) {
  375. /* Round up to next power of 2 */
  376. size = 1 << (ffs(base) + 1);
  377. base = 0xffffffff - size + 1;
  378. }
  379. /* Now write base and size */
  380. writel(base, MBUS_BRIDGE_WIN_BASE_REG);
  381. /* Align window size to 64KiB */
  382. val = (size / (64 << 10)) - 1;
  383. writel((val << 16) | 0x1, MBUS_BRIDGE_WIN_CTRL_REG);
  384. }
  385. int mbus_dt_setup_win(struct mvebu_mbus_state *mbus,
  386. u32 base, u32 size, u8 target, u8 attr)
  387. {
  388. if (!mvebu_mbus_window_conflicts(mbus, base, size, target, attr)) {
  389. printf("Cannot add window '%04x:%04x', conflicts with another window\n",
  390. target, attr);
  391. return -EBUSY;
  392. }
  393. /*
  394. * In U-Boot we first try to add the mbus window to the remap windows.
  395. * If this fails, lets try to add the windows to the non-remap windows.
  396. */
  397. if (mvebu_mbus_alloc_window(mbus, base, size, base, target, attr)) {
  398. if (mvebu_mbus_alloc_window(mbus, base, size,
  399. MVEBU_MBUS_NO_REMAP, target, attr))
  400. return -ENOMEM;
  401. }
  402. /*
  403. * Re-configure the mbus bridge registers each time this function
  404. * is called. Since it may get called from the board code in
  405. * later boot stages as well.
  406. */
  407. mvebu_config_mbus_bridge(mbus);
  408. return 0;
  409. }
  410. int mvebu_mbus_probe(struct mbus_win windows[], int count)
  411. {
  412. int win;
  413. int ret;
  414. int i;
  415. #if defined(CONFIG_KIRKWOOD)
  416. mbus_state.soc = &kirkwood_mbus_data;
  417. #endif
  418. #if defined(CONFIG_ARCH_MVEBU)
  419. mbus_state.soc = &armada_370_xp_mbus_data;
  420. #endif
  421. mbus_state.mbuswins_base = (void __iomem *)MVEBU_CPU_WIN_BASE;
  422. mbus_state.sdramwins_base = (void __iomem *)MVEBU_SDRAM_BASE;
  423. for (win = 0; win < mbus_state.soc->num_wins; win++)
  424. mvebu_mbus_disable_window(&mbus_state, win);
  425. mbus_state.soc->setup_cpu_target(&mbus_state);
  426. /* Setup statically declared windows in the DT */
  427. for (i = 0; i < count; i++) {
  428. u32 base, size;
  429. u8 target, attr;
  430. target = windows[i].target;
  431. attr = windows[i].attr;
  432. base = windows[i].base;
  433. size = windows[i].size;
  434. ret = mbus_dt_setup_win(&mbus_state, base, size, target, attr);
  435. if (ret < 0)
  436. return ret;
  437. }
  438. return 0;
  439. }