mbus.c 13 KB

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