mvebu-mbus.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. /*
  2. * Address map functions for Marvell EBU SoCs (Kirkwood, Armada
  3. * 370/XP, Dove, Orion5x and MV78xx0)
  4. *
  5. * This file is licensed under the terms of the GNU General Public
  6. * License version 2. This program is licensed "as is" without any
  7. * warranty of any kind, whether express or implied.
  8. *
  9. * The Marvell EBU SoCs have a configurable physical address space:
  10. * the physical address at which certain devices (PCIe, NOR, NAND,
  11. * etc.) sit can be configured. The configuration takes place through
  12. * two sets of registers:
  13. *
  14. * - One to configure the access of the CPU to the devices. Depending
  15. * on the families, there are between 8 and 20 configurable windows,
  16. * each can be use to create a physical memory window that maps to a
  17. * specific device. Devices are identified by a tuple (target,
  18. * attribute).
  19. *
  20. * - One to configure the access to the CPU to the SDRAM. There are
  21. * either 2 (for Dove) or 4 (for other families) windows to map the
  22. * SDRAM into the physical address space.
  23. *
  24. * This driver:
  25. *
  26. * - Reads out the SDRAM address decoding windows at initialization
  27. * time, and fills the mvebu_mbus_dram_info structure with these
  28. * informations. The exported function mv_mbus_dram_info() allow
  29. * device drivers to get those informations related to the SDRAM
  30. * address decoding windows. This is because devices also have their
  31. * own windows (configured through registers that are part of each
  32. * device register space), and therefore the drivers for Marvell
  33. * devices have to configure those device -> SDRAM windows to ensure
  34. * that DMA works properly.
  35. *
  36. * - Provides an API for platform code or device drivers to
  37. * dynamically add or remove address decoding windows for the CPU ->
  38. * device accesses. This API is mvebu_mbus_add_window_by_id(),
  39. * mvebu_mbus_add_window_remap_by_id() and
  40. * mvebu_mbus_del_window().
  41. *
  42. * - Provides a debugfs interface in /sys/kernel/debug/mvebu-mbus/ to
  43. * see the list of CPU -> SDRAM windows and their configuration
  44. * (file 'sdram') and the list of CPU -> devices windows and their
  45. * configuration (file 'devices').
  46. */
  47. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  48. #include <linux/kernel.h>
  49. #include <linux/module.h>
  50. #include <linux/init.h>
  51. #include <linux/mbus.h>
  52. #include <linux/io.h>
  53. #include <linux/ioport.h>
  54. #include <linux/of.h>
  55. #include <linux/of_address.h>
  56. #include <linux/debugfs.h>
  57. #include <linux/log2.h>
  58. /*
  59. * DDR target is the same on all platforms.
  60. */
  61. #define TARGET_DDR 0
  62. /*
  63. * CPU Address Decode Windows registers
  64. */
  65. #define WIN_CTRL_OFF 0x0000
  66. #define WIN_CTRL_ENABLE BIT(0)
  67. #define WIN_CTRL_TGT_MASK 0xf0
  68. #define WIN_CTRL_TGT_SHIFT 4
  69. #define WIN_CTRL_ATTR_MASK 0xff00
  70. #define WIN_CTRL_ATTR_SHIFT 8
  71. #define WIN_CTRL_SIZE_MASK 0xffff0000
  72. #define WIN_CTRL_SIZE_SHIFT 16
  73. #define WIN_BASE_OFF 0x0004
  74. #define WIN_BASE_LOW 0xffff0000
  75. #define WIN_BASE_HIGH 0xf
  76. #define WIN_REMAP_LO_OFF 0x0008
  77. #define WIN_REMAP_LOW 0xffff0000
  78. #define WIN_REMAP_HI_OFF 0x000c
  79. #define ATTR_HW_COHERENCY (0x1 << 4)
  80. #define DDR_BASE_CS_OFF(n) (0x0000 + ((n) << 3))
  81. #define DDR_BASE_CS_HIGH_MASK 0xf
  82. #define DDR_BASE_CS_LOW_MASK 0xff000000
  83. #define DDR_SIZE_CS_OFF(n) (0x0004 + ((n) << 3))
  84. #define DDR_SIZE_ENABLED BIT(0)
  85. #define DDR_SIZE_CS_MASK 0x1c
  86. #define DDR_SIZE_CS_SHIFT 2
  87. #define DDR_SIZE_MASK 0xff000000
  88. #define DOVE_DDR_BASE_CS_OFF(n) ((n) << 4)
  89. struct mvebu_mbus_state;
  90. struct mvebu_mbus_soc_data {
  91. unsigned int num_wins;
  92. unsigned int num_remappable_wins;
  93. unsigned int (*win_cfg_offset)(const int win);
  94. void (*setup_cpu_target)(struct mvebu_mbus_state *s);
  95. int (*show_cpu_target)(struct mvebu_mbus_state *s,
  96. struct seq_file *seq, void *v);
  97. };
  98. struct mvebu_mbus_state {
  99. void __iomem *mbuswins_base;
  100. void __iomem *sdramwins_base;
  101. struct dentry *debugfs_root;
  102. struct dentry *debugfs_sdram;
  103. struct dentry *debugfs_devs;
  104. struct resource pcie_mem_aperture;
  105. struct resource pcie_io_aperture;
  106. const struct mvebu_mbus_soc_data *soc;
  107. int hw_io_coherency;
  108. };
  109. static struct mvebu_mbus_state mbus_state;
  110. static struct mbus_dram_target_info mvebu_mbus_dram_info;
  111. const struct mbus_dram_target_info *mv_mbus_dram_info(void)
  112. {
  113. return &mvebu_mbus_dram_info;
  114. }
  115. EXPORT_SYMBOL_GPL(mv_mbus_dram_info);
  116. /*
  117. * Functions to manipulate the address decoding windows
  118. */
  119. static void mvebu_mbus_read_window(struct mvebu_mbus_state *mbus,
  120. int win, int *enabled, u64 *base,
  121. u32 *size, u8 *target, u8 *attr,
  122. u64 *remap)
  123. {
  124. void __iomem *addr = mbus->mbuswins_base +
  125. mbus->soc->win_cfg_offset(win);
  126. u32 basereg = readl(addr + WIN_BASE_OFF);
  127. u32 ctrlreg = readl(addr + WIN_CTRL_OFF);
  128. if (!(ctrlreg & WIN_CTRL_ENABLE)) {
  129. *enabled = 0;
  130. return;
  131. }
  132. *enabled = 1;
  133. *base = ((u64)basereg & WIN_BASE_HIGH) << 32;
  134. *base |= (basereg & WIN_BASE_LOW);
  135. *size = (ctrlreg | ~WIN_CTRL_SIZE_MASK) + 1;
  136. if (target)
  137. *target = (ctrlreg & WIN_CTRL_TGT_MASK) >> WIN_CTRL_TGT_SHIFT;
  138. if (attr)
  139. *attr = (ctrlreg & WIN_CTRL_ATTR_MASK) >> WIN_CTRL_ATTR_SHIFT;
  140. if (remap) {
  141. if (win < mbus->soc->num_remappable_wins) {
  142. u32 remap_low = readl(addr + WIN_REMAP_LO_OFF);
  143. u32 remap_hi = readl(addr + WIN_REMAP_HI_OFF);
  144. *remap = ((u64)remap_hi << 32) | remap_low;
  145. } else
  146. *remap = 0;
  147. }
  148. }
  149. static void mvebu_mbus_disable_window(struct mvebu_mbus_state *mbus,
  150. int win)
  151. {
  152. void __iomem *addr;
  153. addr = mbus->mbuswins_base + mbus->soc->win_cfg_offset(win);
  154. writel(0, addr + WIN_BASE_OFF);
  155. writel(0, addr + WIN_CTRL_OFF);
  156. if (win < mbus->soc->num_remappable_wins) {
  157. writel(0, addr + WIN_REMAP_LO_OFF);
  158. writel(0, addr + WIN_REMAP_HI_OFF);
  159. }
  160. }
  161. /* Checks whether the given window number is available */
  162. /* On Armada XP, 375 and 38x the MBus window 13 has the remap
  163. * capability, like windows 0 to 7. However, the mvebu-mbus driver
  164. * isn't currently taking into account this special case, which means
  165. * that when window 13 is actually used, the remap registers are left
  166. * to 0, making the device using this MBus window unavailable. The
  167. * quick fix for stable is to not use window 13. A follow up patch
  168. * will correctly handle this window.
  169. */
  170. static int mvebu_mbus_window_is_free(struct mvebu_mbus_state *mbus,
  171. const int win)
  172. {
  173. void __iomem *addr = mbus->mbuswins_base +
  174. mbus->soc->win_cfg_offset(win);
  175. u32 ctrl = readl(addr + WIN_CTRL_OFF);
  176. if (win == 13)
  177. return false;
  178. return !(ctrl & WIN_CTRL_ENABLE);
  179. }
  180. /*
  181. * Checks whether the given (base, base+size) area doesn't overlap an
  182. * existing region
  183. */
  184. static int mvebu_mbus_window_conflicts(struct mvebu_mbus_state *mbus,
  185. phys_addr_t base, size_t size,
  186. u8 target, u8 attr)
  187. {
  188. u64 end = (u64)base + size;
  189. int win;
  190. for (win = 0; win < mbus->soc->num_wins; win++) {
  191. u64 wbase, wend;
  192. u32 wsize;
  193. u8 wtarget, wattr;
  194. int enabled;
  195. mvebu_mbus_read_window(mbus, win,
  196. &enabled, &wbase, &wsize,
  197. &wtarget, &wattr, NULL);
  198. if (!enabled)
  199. continue;
  200. wend = wbase + wsize;
  201. /*
  202. * Check if the current window overlaps with the
  203. * proposed physical range
  204. */
  205. if ((u64)base < wend && end > wbase)
  206. return 0;
  207. }
  208. return 1;
  209. }
  210. static int mvebu_mbus_find_window(struct mvebu_mbus_state *mbus,
  211. phys_addr_t base, size_t size)
  212. {
  213. int win;
  214. for (win = 0; win < mbus->soc->num_wins; win++) {
  215. u64 wbase;
  216. u32 wsize;
  217. int enabled;
  218. mvebu_mbus_read_window(mbus, win,
  219. &enabled, &wbase, &wsize,
  220. NULL, NULL, NULL);
  221. if (!enabled)
  222. continue;
  223. if (base == wbase && size == wsize)
  224. return win;
  225. }
  226. return -ENODEV;
  227. }
  228. static int mvebu_mbus_setup_window(struct mvebu_mbus_state *mbus,
  229. int win, phys_addr_t base, size_t size,
  230. phys_addr_t remap, u8 target,
  231. u8 attr)
  232. {
  233. void __iomem *addr = mbus->mbuswins_base +
  234. mbus->soc->win_cfg_offset(win);
  235. u32 ctrl, remap_addr;
  236. if (!is_power_of_2(size)) {
  237. WARN(true, "Invalid MBus window size: 0x%zx\n", size);
  238. return -EINVAL;
  239. }
  240. if ((base & (phys_addr_t)(size - 1)) != 0) {
  241. WARN(true, "Invalid MBus base/size: %pa len 0x%zx\n", &base,
  242. size);
  243. return -EINVAL;
  244. }
  245. ctrl = ((size - 1) & WIN_CTRL_SIZE_MASK) |
  246. (attr << WIN_CTRL_ATTR_SHIFT) |
  247. (target << WIN_CTRL_TGT_SHIFT) |
  248. WIN_CTRL_ENABLE;
  249. writel(base & WIN_BASE_LOW, addr + WIN_BASE_OFF);
  250. writel(ctrl, addr + WIN_CTRL_OFF);
  251. if (win < mbus->soc->num_remappable_wins) {
  252. if (remap == MVEBU_MBUS_NO_REMAP)
  253. remap_addr = base;
  254. else
  255. remap_addr = remap;
  256. writel(remap_addr & WIN_REMAP_LOW, addr + WIN_REMAP_LO_OFF);
  257. writel(0, addr + WIN_REMAP_HI_OFF);
  258. }
  259. return 0;
  260. }
  261. static int mvebu_mbus_alloc_window(struct mvebu_mbus_state *mbus,
  262. phys_addr_t base, size_t size,
  263. phys_addr_t remap, u8 target,
  264. u8 attr)
  265. {
  266. int win;
  267. if (remap == MVEBU_MBUS_NO_REMAP) {
  268. for (win = mbus->soc->num_remappable_wins;
  269. win < mbus->soc->num_wins; win++)
  270. if (mvebu_mbus_window_is_free(mbus, win))
  271. return mvebu_mbus_setup_window(mbus, win, base,
  272. size, remap,
  273. target, attr);
  274. }
  275. for (win = 0; win < mbus->soc->num_wins; win++)
  276. if (mvebu_mbus_window_is_free(mbus, win))
  277. return mvebu_mbus_setup_window(mbus, win, base, size,
  278. remap, target, attr);
  279. return -ENOMEM;
  280. }
  281. /*
  282. * Debugfs debugging
  283. */
  284. /* Common function used for Dove, Kirkwood, Armada 370/XP and Orion 5x */
  285. static int mvebu_sdram_debug_show_orion(struct mvebu_mbus_state *mbus,
  286. struct seq_file *seq, void *v)
  287. {
  288. int i;
  289. for (i = 0; i < 4; i++) {
  290. u32 basereg = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
  291. u32 sizereg = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
  292. u64 base;
  293. u32 size;
  294. if (!(sizereg & DDR_SIZE_ENABLED)) {
  295. seq_printf(seq, "[%d] disabled\n", i);
  296. continue;
  297. }
  298. base = ((u64)basereg & DDR_BASE_CS_HIGH_MASK) << 32;
  299. base |= basereg & DDR_BASE_CS_LOW_MASK;
  300. size = (sizereg | ~DDR_SIZE_MASK);
  301. seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
  302. i, (unsigned long long)base,
  303. (unsigned long long)base + size + 1,
  304. (sizereg & DDR_SIZE_CS_MASK) >> DDR_SIZE_CS_SHIFT);
  305. }
  306. return 0;
  307. }
  308. /* Special function for Dove */
  309. static int mvebu_sdram_debug_show_dove(struct mvebu_mbus_state *mbus,
  310. struct seq_file *seq, void *v)
  311. {
  312. int i;
  313. for (i = 0; i < 2; i++) {
  314. u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
  315. u64 base;
  316. u32 size;
  317. if (!(map & 1)) {
  318. seq_printf(seq, "[%d] disabled\n", i);
  319. continue;
  320. }
  321. base = map & 0xff800000;
  322. size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
  323. seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
  324. i, (unsigned long long)base,
  325. (unsigned long long)base + size, i);
  326. }
  327. return 0;
  328. }
  329. static int mvebu_sdram_debug_show(struct seq_file *seq, void *v)
  330. {
  331. struct mvebu_mbus_state *mbus = &mbus_state;
  332. return mbus->soc->show_cpu_target(mbus, seq, v);
  333. }
  334. static int mvebu_sdram_debug_open(struct inode *inode, struct file *file)
  335. {
  336. return single_open(file, mvebu_sdram_debug_show, inode->i_private);
  337. }
  338. static const struct file_operations mvebu_sdram_debug_fops = {
  339. .open = mvebu_sdram_debug_open,
  340. .read = seq_read,
  341. .llseek = seq_lseek,
  342. .release = single_release,
  343. };
  344. static int mvebu_devs_debug_show(struct seq_file *seq, void *v)
  345. {
  346. struct mvebu_mbus_state *mbus = &mbus_state;
  347. int win;
  348. for (win = 0; win < mbus->soc->num_wins; win++) {
  349. u64 wbase, wremap;
  350. u32 wsize;
  351. u8 wtarget, wattr;
  352. int enabled;
  353. mvebu_mbus_read_window(mbus, win,
  354. &enabled, &wbase, &wsize,
  355. &wtarget, &wattr, &wremap);
  356. if (!enabled) {
  357. seq_printf(seq, "[%02d] disabled\n", win);
  358. continue;
  359. }
  360. seq_printf(seq, "[%02d] %016llx - %016llx : %04x:%04x",
  361. win, (unsigned long long)wbase,
  362. (unsigned long long)(wbase + wsize), wtarget, wattr);
  363. if (!is_power_of_2(wsize) ||
  364. ((wbase & (u64)(wsize - 1)) != 0))
  365. seq_puts(seq, " (Invalid base/size!!)");
  366. if (win < mbus->soc->num_remappable_wins) {
  367. seq_printf(seq, " (remap %016llx)\n",
  368. (unsigned long long)wremap);
  369. } else
  370. seq_printf(seq, "\n");
  371. }
  372. return 0;
  373. }
  374. static int mvebu_devs_debug_open(struct inode *inode, struct file *file)
  375. {
  376. return single_open(file, mvebu_devs_debug_show, inode->i_private);
  377. }
  378. static const struct file_operations mvebu_devs_debug_fops = {
  379. .open = mvebu_devs_debug_open,
  380. .read = seq_read,
  381. .llseek = seq_lseek,
  382. .release = single_release,
  383. };
  384. /*
  385. * SoC-specific functions and definitions
  386. */
  387. static unsigned int orion_mbus_win_offset(int win)
  388. {
  389. return win << 4;
  390. }
  391. static unsigned int armada_370_xp_mbus_win_offset(int win)
  392. {
  393. /* The register layout is a bit annoying and the below code
  394. * tries to cope with it.
  395. * - At offset 0x0, there are the registers for the first 8
  396. * windows, with 4 registers of 32 bits per window (ctrl,
  397. * base, remap low, remap high)
  398. * - Then at offset 0x80, there is a hole of 0x10 bytes for
  399. * the internal registers base address and internal units
  400. * sync barrier register.
  401. * - Then at offset 0x90, there the registers for 12
  402. * windows, with only 2 registers of 32 bits per window
  403. * (ctrl, base).
  404. */
  405. if (win < 8)
  406. return win << 4;
  407. else
  408. return 0x90 + ((win - 8) << 3);
  409. }
  410. static unsigned int mv78xx0_mbus_win_offset(int win)
  411. {
  412. if (win < 8)
  413. return win << 4;
  414. else
  415. return 0x900 + ((win - 8) << 4);
  416. }
  417. static void __init
  418. mvebu_mbus_default_setup_cpu_target(struct mvebu_mbus_state *mbus)
  419. {
  420. int i;
  421. int cs;
  422. mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
  423. for (i = 0, cs = 0; i < 4; i++) {
  424. u32 base = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
  425. u32 size = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
  426. /*
  427. * We only take care of entries for which the chip
  428. * select is enabled, and that don't have high base
  429. * address bits set (devices can only access the first
  430. * 32 bits of the memory).
  431. */
  432. if ((size & DDR_SIZE_ENABLED) &&
  433. !(base & DDR_BASE_CS_HIGH_MASK)) {
  434. struct mbus_dram_window *w;
  435. w = &mvebu_mbus_dram_info.cs[cs++];
  436. w->cs_index = i;
  437. w->mbus_attr = 0xf & ~(1 << i);
  438. if (mbus->hw_io_coherency)
  439. w->mbus_attr |= ATTR_HW_COHERENCY;
  440. w->base = base & DDR_BASE_CS_LOW_MASK;
  441. w->size = (size | ~DDR_SIZE_MASK) + 1;
  442. }
  443. }
  444. mvebu_mbus_dram_info.num_cs = cs;
  445. }
  446. static void __init
  447. mvebu_mbus_dove_setup_cpu_target(struct mvebu_mbus_state *mbus)
  448. {
  449. int i;
  450. int cs;
  451. mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
  452. for (i = 0, cs = 0; i < 2; i++) {
  453. u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
  454. /*
  455. * Chip select enabled?
  456. */
  457. if (map & 1) {
  458. struct mbus_dram_window *w;
  459. w = &mvebu_mbus_dram_info.cs[cs++];
  460. w->cs_index = i;
  461. w->mbus_attr = 0; /* CS address decoding done inside */
  462. /* the DDR controller, no need to */
  463. /* provide attributes */
  464. w->base = map & 0xff800000;
  465. w->size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
  466. }
  467. }
  468. mvebu_mbus_dram_info.num_cs = cs;
  469. }
  470. static const struct mvebu_mbus_soc_data armada_370_xp_mbus_data = {
  471. .num_wins = 20,
  472. .num_remappable_wins = 8,
  473. .win_cfg_offset = armada_370_xp_mbus_win_offset,
  474. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  475. .show_cpu_target = mvebu_sdram_debug_show_orion,
  476. };
  477. static const struct mvebu_mbus_soc_data kirkwood_mbus_data = {
  478. .num_wins = 8,
  479. .num_remappable_wins = 4,
  480. .win_cfg_offset = orion_mbus_win_offset,
  481. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  482. .show_cpu_target = mvebu_sdram_debug_show_orion,
  483. };
  484. static const struct mvebu_mbus_soc_data dove_mbus_data = {
  485. .num_wins = 8,
  486. .num_remappable_wins = 4,
  487. .win_cfg_offset = orion_mbus_win_offset,
  488. .setup_cpu_target = mvebu_mbus_dove_setup_cpu_target,
  489. .show_cpu_target = mvebu_sdram_debug_show_dove,
  490. };
  491. /*
  492. * Some variants of Orion5x have 4 remappable windows, some other have
  493. * only two of them.
  494. */
  495. static const struct mvebu_mbus_soc_data orion5x_4win_mbus_data = {
  496. .num_wins = 8,
  497. .num_remappable_wins = 4,
  498. .win_cfg_offset = orion_mbus_win_offset,
  499. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  500. .show_cpu_target = mvebu_sdram_debug_show_orion,
  501. };
  502. static const struct mvebu_mbus_soc_data orion5x_2win_mbus_data = {
  503. .num_wins = 8,
  504. .num_remappable_wins = 2,
  505. .win_cfg_offset = orion_mbus_win_offset,
  506. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  507. .show_cpu_target = mvebu_sdram_debug_show_orion,
  508. };
  509. static const struct mvebu_mbus_soc_data mv78xx0_mbus_data = {
  510. .num_wins = 14,
  511. .num_remappable_wins = 8,
  512. .win_cfg_offset = mv78xx0_mbus_win_offset,
  513. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  514. .show_cpu_target = mvebu_sdram_debug_show_orion,
  515. };
  516. static const struct of_device_id of_mvebu_mbus_ids[] = {
  517. { .compatible = "marvell,armada370-mbus",
  518. .data = &armada_370_xp_mbus_data, },
  519. { .compatible = "marvell,armadaxp-mbus",
  520. .data = &armada_370_xp_mbus_data, },
  521. { .compatible = "marvell,kirkwood-mbus",
  522. .data = &kirkwood_mbus_data, },
  523. { .compatible = "marvell,dove-mbus",
  524. .data = &dove_mbus_data, },
  525. { .compatible = "marvell,orion5x-88f5281-mbus",
  526. .data = &orion5x_4win_mbus_data, },
  527. { .compatible = "marvell,orion5x-88f5182-mbus",
  528. .data = &orion5x_2win_mbus_data, },
  529. { .compatible = "marvell,orion5x-88f5181-mbus",
  530. .data = &orion5x_2win_mbus_data, },
  531. { .compatible = "marvell,orion5x-88f6183-mbus",
  532. .data = &orion5x_4win_mbus_data, },
  533. { .compatible = "marvell,mv78xx0-mbus",
  534. .data = &mv78xx0_mbus_data, },
  535. { },
  536. };
  537. /*
  538. * Public API of the driver
  539. */
  540. int mvebu_mbus_add_window_remap_by_id(unsigned int target,
  541. unsigned int attribute,
  542. phys_addr_t base, size_t size,
  543. phys_addr_t remap)
  544. {
  545. struct mvebu_mbus_state *s = &mbus_state;
  546. if (!mvebu_mbus_window_conflicts(s, base, size, target, attribute)) {
  547. pr_err("cannot add window '%x:%x', conflicts with another window\n",
  548. target, attribute);
  549. return -EINVAL;
  550. }
  551. return mvebu_mbus_alloc_window(s, base, size, remap, target, attribute);
  552. }
  553. int mvebu_mbus_add_window_by_id(unsigned int target, unsigned int attribute,
  554. phys_addr_t base, size_t size)
  555. {
  556. return mvebu_mbus_add_window_remap_by_id(target, attribute, base,
  557. size, MVEBU_MBUS_NO_REMAP);
  558. }
  559. int mvebu_mbus_del_window(phys_addr_t base, size_t size)
  560. {
  561. int win;
  562. win = mvebu_mbus_find_window(&mbus_state, base, size);
  563. if (win < 0)
  564. return win;
  565. mvebu_mbus_disable_window(&mbus_state, win);
  566. return 0;
  567. }
  568. void mvebu_mbus_get_pcie_mem_aperture(struct resource *res)
  569. {
  570. if (!res)
  571. return;
  572. *res = mbus_state.pcie_mem_aperture;
  573. }
  574. void mvebu_mbus_get_pcie_io_aperture(struct resource *res)
  575. {
  576. if (!res)
  577. return;
  578. *res = mbus_state.pcie_io_aperture;
  579. }
  580. static __init int mvebu_mbus_debugfs_init(void)
  581. {
  582. struct mvebu_mbus_state *s = &mbus_state;
  583. /*
  584. * If no base has been initialized, doesn't make sense to
  585. * register the debugfs entries. We may be on a multiplatform
  586. * kernel that isn't running a Marvell EBU SoC.
  587. */
  588. if (!s->mbuswins_base)
  589. return 0;
  590. s->debugfs_root = debugfs_create_dir("mvebu-mbus", NULL);
  591. if (s->debugfs_root) {
  592. s->debugfs_sdram = debugfs_create_file("sdram", S_IRUGO,
  593. s->debugfs_root, NULL,
  594. &mvebu_sdram_debug_fops);
  595. s->debugfs_devs = debugfs_create_file("devices", S_IRUGO,
  596. s->debugfs_root, NULL,
  597. &mvebu_devs_debug_fops);
  598. }
  599. return 0;
  600. }
  601. fs_initcall(mvebu_mbus_debugfs_init);
  602. static int __init mvebu_mbus_common_init(struct mvebu_mbus_state *mbus,
  603. phys_addr_t mbuswins_phys_base,
  604. size_t mbuswins_size,
  605. phys_addr_t sdramwins_phys_base,
  606. size_t sdramwins_size)
  607. {
  608. int win;
  609. mbus->mbuswins_base = ioremap(mbuswins_phys_base, mbuswins_size);
  610. if (!mbus->mbuswins_base)
  611. return -ENOMEM;
  612. mbus->sdramwins_base = ioremap(sdramwins_phys_base, sdramwins_size);
  613. if (!mbus->sdramwins_base) {
  614. iounmap(mbus_state.mbuswins_base);
  615. return -ENOMEM;
  616. }
  617. for (win = 0; win < mbus->soc->num_wins; win++)
  618. mvebu_mbus_disable_window(mbus, win);
  619. mbus->soc->setup_cpu_target(mbus);
  620. return 0;
  621. }
  622. int __init mvebu_mbus_init(const char *soc, phys_addr_t mbuswins_phys_base,
  623. size_t mbuswins_size,
  624. phys_addr_t sdramwins_phys_base,
  625. size_t sdramwins_size)
  626. {
  627. const struct of_device_id *of_id;
  628. for (of_id = of_mvebu_mbus_ids; of_id->compatible[0]; of_id++)
  629. if (!strcmp(of_id->compatible, soc))
  630. break;
  631. if (!of_id->compatible[0]) {
  632. pr_err("could not find a matching SoC family\n");
  633. return -ENODEV;
  634. }
  635. mbus_state.soc = of_id->data;
  636. return mvebu_mbus_common_init(&mbus_state,
  637. mbuswins_phys_base,
  638. mbuswins_size,
  639. sdramwins_phys_base,
  640. sdramwins_size);
  641. }
  642. #ifdef CONFIG_OF
  643. /*
  644. * The window IDs in the ranges DT property have the following format:
  645. * - bits 28 to 31: MBus custom field
  646. * - bits 24 to 27: window target ID
  647. * - bits 16 to 23: window attribute ID
  648. * - bits 0 to 15: unused
  649. */
  650. #define CUSTOM(id) (((id) & 0xF0000000) >> 24)
  651. #define TARGET(id) (((id) & 0x0F000000) >> 24)
  652. #define ATTR(id) (((id) & 0x00FF0000) >> 16)
  653. static int __init mbus_dt_setup_win(struct mvebu_mbus_state *mbus,
  654. u32 base, u32 size,
  655. u8 target, u8 attr)
  656. {
  657. if (!mvebu_mbus_window_conflicts(mbus, base, size, target, attr)) {
  658. pr_err("cannot add window '%04x:%04x', conflicts with another window\n",
  659. target, attr);
  660. return -EBUSY;
  661. }
  662. if (mvebu_mbus_alloc_window(mbus, base, size, MVEBU_MBUS_NO_REMAP,
  663. target, attr)) {
  664. pr_err("cannot add window '%04x:%04x', too many windows\n",
  665. target, attr);
  666. return -ENOMEM;
  667. }
  668. return 0;
  669. }
  670. static int __init
  671. mbus_parse_ranges(struct device_node *node,
  672. int *addr_cells, int *c_addr_cells, int *c_size_cells,
  673. int *cell_count, const __be32 **ranges_start,
  674. const __be32 **ranges_end)
  675. {
  676. const __be32 *prop;
  677. int ranges_len, tuple_len;
  678. /* Allow a node with no 'ranges' property */
  679. *ranges_start = of_get_property(node, "ranges", &ranges_len);
  680. if (*ranges_start == NULL) {
  681. *addr_cells = *c_addr_cells = *c_size_cells = *cell_count = 0;
  682. *ranges_start = *ranges_end = NULL;
  683. return 0;
  684. }
  685. *ranges_end = *ranges_start + ranges_len / sizeof(__be32);
  686. *addr_cells = of_n_addr_cells(node);
  687. prop = of_get_property(node, "#address-cells", NULL);
  688. *c_addr_cells = be32_to_cpup(prop);
  689. prop = of_get_property(node, "#size-cells", NULL);
  690. *c_size_cells = be32_to_cpup(prop);
  691. *cell_count = *addr_cells + *c_addr_cells + *c_size_cells;
  692. tuple_len = (*cell_count) * sizeof(__be32);
  693. if (ranges_len % tuple_len) {
  694. pr_warn("malformed ranges entry '%s'\n", node->name);
  695. return -EINVAL;
  696. }
  697. return 0;
  698. }
  699. static int __init mbus_dt_setup(struct mvebu_mbus_state *mbus,
  700. struct device_node *np)
  701. {
  702. int addr_cells, c_addr_cells, c_size_cells;
  703. int i, ret, cell_count;
  704. const __be32 *r, *ranges_start, *ranges_end;
  705. ret = mbus_parse_ranges(np, &addr_cells, &c_addr_cells,
  706. &c_size_cells, &cell_count,
  707. &ranges_start, &ranges_end);
  708. if (ret < 0)
  709. return ret;
  710. for (i = 0, r = ranges_start; r < ranges_end; r += cell_count, i++) {
  711. u32 windowid, base, size;
  712. u8 target, attr;
  713. /*
  714. * An entry with a non-zero custom field do not
  715. * correspond to a static window, so skip it.
  716. */
  717. windowid = of_read_number(r, 1);
  718. if (CUSTOM(windowid))
  719. continue;
  720. target = TARGET(windowid);
  721. attr = ATTR(windowid);
  722. base = of_read_number(r + c_addr_cells, addr_cells);
  723. size = of_read_number(r + c_addr_cells + addr_cells,
  724. c_size_cells);
  725. ret = mbus_dt_setup_win(mbus, base, size, target, attr);
  726. if (ret < 0)
  727. return ret;
  728. }
  729. return 0;
  730. }
  731. static void __init mvebu_mbus_get_pcie_resources(struct device_node *np,
  732. struct resource *mem,
  733. struct resource *io)
  734. {
  735. u32 reg[2];
  736. int ret;
  737. /*
  738. * These are optional, so we make sure that resource_size(x) will
  739. * return 0.
  740. */
  741. memset(mem, 0, sizeof(struct resource));
  742. mem->end = -1;
  743. memset(io, 0, sizeof(struct resource));
  744. io->end = -1;
  745. ret = of_property_read_u32_array(np, "pcie-mem-aperture", reg, ARRAY_SIZE(reg));
  746. if (!ret) {
  747. mem->start = reg[0];
  748. mem->end = mem->start + reg[1] - 1;
  749. mem->flags = IORESOURCE_MEM;
  750. }
  751. ret = of_property_read_u32_array(np, "pcie-io-aperture", reg, ARRAY_SIZE(reg));
  752. if (!ret) {
  753. io->start = reg[0];
  754. io->end = io->start + reg[1] - 1;
  755. io->flags = IORESOURCE_IO;
  756. }
  757. }
  758. int __init mvebu_mbus_dt_init(bool is_coherent)
  759. {
  760. struct resource mbuswins_res, sdramwins_res;
  761. struct device_node *np, *controller;
  762. const struct of_device_id *of_id;
  763. const __be32 *prop;
  764. int ret;
  765. np = of_find_matching_node_and_match(NULL, of_mvebu_mbus_ids, &of_id);
  766. if (!np) {
  767. pr_err("could not find a matching SoC family\n");
  768. return -ENODEV;
  769. }
  770. mbus_state.soc = of_id->data;
  771. prop = of_get_property(np, "controller", NULL);
  772. if (!prop) {
  773. pr_err("required 'controller' property missing\n");
  774. return -EINVAL;
  775. }
  776. controller = of_find_node_by_phandle(be32_to_cpup(prop));
  777. if (!controller) {
  778. pr_err("could not find an 'mbus-controller' node\n");
  779. return -ENODEV;
  780. }
  781. if (of_address_to_resource(controller, 0, &mbuswins_res)) {
  782. pr_err("cannot get MBUS register address\n");
  783. return -EINVAL;
  784. }
  785. if (of_address_to_resource(controller, 1, &sdramwins_res)) {
  786. pr_err("cannot get SDRAM register address\n");
  787. return -EINVAL;
  788. }
  789. mbus_state.hw_io_coherency = is_coherent;
  790. /* Get optional pcie-{mem,io}-aperture properties */
  791. mvebu_mbus_get_pcie_resources(np, &mbus_state.pcie_mem_aperture,
  792. &mbus_state.pcie_io_aperture);
  793. ret = mvebu_mbus_common_init(&mbus_state,
  794. mbuswins_res.start,
  795. resource_size(&mbuswins_res),
  796. sdramwins_res.start,
  797. resource_size(&sdramwins_res));
  798. if (ret)
  799. return ret;
  800. /* Setup statically declared windows in the DT */
  801. return mbus_dt_setup(&mbus_state, np);
  802. }
  803. #endif