mvebu-mbus.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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. static int mvebu_mbus_window_is_free(struct mvebu_mbus_state *mbus,
  163. const int win)
  164. {
  165. void __iomem *addr = mbus->mbuswins_base +
  166. mbus->soc->win_cfg_offset(win);
  167. u32 ctrl = readl(addr + WIN_CTRL_OFF);
  168. return !(ctrl & WIN_CTRL_ENABLE);
  169. }
  170. /*
  171. * Checks whether the given (base, base+size) area doesn't overlap an
  172. * existing region
  173. */
  174. static int mvebu_mbus_window_conflicts(struct mvebu_mbus_state *mbus,
  175. phys_addr_t base, size_t size,
  176. u8 target, u8 attr)
  177. {
  178. u64 end = (u64)base + size;
  179. int win;
  180. for (win = 0; win < mbus->soc->num_wins; win++) {
  181. u64 wbase, wend;
  182. u32 wsize;
  183. u8 wtarget, wattr;
  184. int enabled;
  185. mvebu_mbus_read_window(mbus, win,
  186. &enabled, &wbase, &wsize,
  187. &wtarget, &wattr, NULL);
  188. if (!enabled)
  189. continue;
  190. wend = wbase + wsize;
  191. /*
  192. * Check if the current window overlaps with the
  193. * proposed physical range
  194. */
  195. if ((u64)base < wend && end > wbase)
  196. return 0;
  197. }
  198. return 1;
  199. }
  200. static int mvebu_mbus_find_window(struct mvebu_mbus_state *mbus,
  201. phys_addr_t base, size_t size)
  202. {
  203. int win;
  204. for (win = 0; win < mbus->soc->num_wins; win++) {
  205. u64 wbase;
  206. u32 wsize;
  207. int enabled;
  208. mvebu_mbus_read_window(mbus, win,
  209. &enabled, &wbase, &wsize,
  210. NULL, NULL, NULL);
  211. if (!enabled)
  212. continue;
  213. if (base == wbase && size == wsize)
  214. return win;
  215. }
  216. return -ENODEV;
  217. }
  218. static int mvebu_mbus_setup_window(struct mvebu_mbus_state *mbus,
  219. int win, phys_addr_t base, size_t size,
  220. phys_addr_t remap, u8 target,
  221. u8 attr)
  222. {
  223. void __iomem *addr = mbus->mbuswins_base +
  224. mbus->soc->win_cfg_offset(win);
  225. u32 ctrl, remap_addr;
  226. if (!is_power_of_2(size)) {
  227. WARN(true, "Invalid MBus window size: 0x%zx\n", size);
  228. return -EINVAL;
  229. }
  230. if ((base & (phys_addr_t)(size - 1)) != 0) {
  231. WARN(true, "Invalid MBus base/size: %pa len 0x%zx\n", &base,
  232. size);
  233. return -EINVAL;
  234. }
  235. ctrl = ((size - 1) & WIN_CTRL_SIZE_MASK) |
  236. (attr << WIN_CTRL_ATTR_SHIFT) |
  237. (target << WIN_CTRL_TGT_SHIFT) |
  238. WIN_CTRL_ENABLE;
  239. writel(base & WIN_BASE_LOW, addr + WIN_BASE_OFF);
  240. writel(ctrl, addr + WIN_CTRL_OFF);
  241. if (win < mbus->soc->num_remappable_wins) {
  242. if (remap == MVEBU_MBUS_NO_REMAP)
  243. remap_addr = base;
  244. else
  245. remap_addr = remap;
  246. writel(remap_addr & WIN_REMAP_LOW, addr + WIN_REMAP_LO_OFF);
  247. writel(0, addr + WIN_REMAP_HI_OFF);
  248. }
  249. return 0;
  250. }
  251. static int mvebu_mbus_alloc_window(struct mvebu_mbus_state *mbus,
  252. phys_addr_t base, size_t size,
  253. phys_addr_t remap, u8 target,
  254. u8 attr)
  255. {
  256. int win;
  257. if (remap == MVEBU_MBUS_NO_REMAP) {
  258. for (win = mbus->soc->num_remappable_wins;
  259. win < mbus->soc->num_wins; win++)
  260. if (mvebu_mbus_window_is_free(mbus, win))
  261. return mvebu_mbus_setup_window(mbus, win, base,
  262. size, remap,
  263. target, attr);
  264. }
  265. for (win = 0; win < mbus->soc->num_wins; win++)
  266. if (mvebu_mbus_window_is_free(mbus, win))
  267. return mvebu_mbus_setup_window(mbus, win, base, size,
  268. remap, target, attr);
  269. return -ENOMEM;
  270. }
  271. /*
  272. * Debugfs debugging
  273. */
  274. /* Common function used for Dove, Kirkwood, Armada 370/XP and Orion 5x */
  275. static int mvebu_sdram_debug_show_orion(struct mvebu_mbus_state *mbus,
  276. struct seq_file *seq, void *v)
  277. {
  278. int i;
  279. for (i = 0; i < 4; i++) {
  280. u32 basereg = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
  281. u32 sizereg = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
  282. u64 base;
  283. u32 size;
  284. if (!(sizereg & DDR_SIZE_ENABLED)) {
  285. seq_printf(seq, "[%d] disabled\n", i);
  286. continue;
  287. }
  288. base = ((u64)basereg & DDR_BASE_CS_HIGH_MASK) << 32;
  289. base |= basereg & DDR_BASE_CS_LOW_MASK;
  290. size = (sizereg | ~DDR_SIZE_MASK);
  291. seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
  292. i, (unsigned long long)base,
  293. (unsigned long long)base + size + 1,
  294. (sizereg & DDR_SIZE_CS_MASK) >> DDR_SIZE_CS_SHIFT);
  295. }
  296. return 0;
  297. }
  298. /* Special function for Dove */
  299. static int mvebu_sdram_debug_show_dove(struct mvebu_mbus_state *mbus,
  300. struct seq_file *seq, void *v)
  301. {
  302. int i;
  303. for (i = 0; i < 2; i++) {
  304. u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
  305. u64 base;
  306. u32 size;
  307. if (!(map & 1)) {
  308. seq_printf(seq, "[%d] disabled\n", i);
  309. continue;
  310. }
  311. base = map & 0xff800000;
  312. size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
  313. seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
  314. i, (unsigned long long)base,
  315. (unsigned long long)base + size, i);
  316. }
  317. return 0;
  318. }
  319. static int mvebu_sdram_debug_show(struct seq_file *seq, void *v)
  320. {
  321. struct mvebu_mbus_state *mbus = &mbus_state;
  322. return mbus->soc->show_cpu_target(mbus, seq, v);
  323. }
  324. static int mvebu_sdram_debug_open(struct inode *inode, struct file *file)
  325. {
  326. return single_open(file, mvebu_sdram_debug_show, inode->i_private);
  327. }
  328. static const struct file_operations mvebu_sdram_debug_fops = {
  329. .open = mvebu_sdram_debug_open,
  330. .read = seq_read,
  331. .llseek = seq_lseek,
  332. .release = single_release,
  333. };
  334. static int mvebu_devs_debug_show(struct seq_file *seq, void *v)
  335. {
  336. struct mvebu_mbus_state *mbus = &mbus_state;
  337. int win;
  338. for (win = 0; win < mbus->soc->num_wins; win++) {
  339. u64 wbase, wremap;
  340. u32 wsize;
  341. u8 wtarget, wattr;
  342. int enabled;
  343. mvebu_mbus_read_window(mbus, win,
  344. &enabled, &wbase, &wsize,
  345. &wtarget, &wattr, &wremap);
  346. if (!enabled) {
  347. seq_printf(seq, "[%02d] disabled\n", win);
  348. continue;
  349. }
  350. seq_printf(seq, "[%02d] %016llx - %016llx : %04x:%04x",
  351. win, (unsigned long long)wbase,
  352. (unsigned long long)(wbase + wsize), wtarget, wattr);
  353. if (!is_power_of_2(wsize) ||
  354. ((wbase & (u64)(wsize - 1)) != 0))
  355. seq_puts(seq, " (Invalid base/size!!)");
  356. if (win < mbus->soc->num_remappable_wins) {
  357. seq_printf(seq, " (remap %016llx)\n",
  358. (unsigned long long)wremap);
  359. } else
  360. seq_printf(seq, "\n");
  361. }
  362. return 0;
  363. }
  364. static int mvebu_devs_debug_open(struct inode *inode, struct file *file)
  365. {
  366. return single_open(file, mvebu_devs_debug_show, inode->i_private);
  367. }
  368. static const struct file_operations mvebu_devs_debug_fops = {
  369. .open = mvebu_devs_debug_open,
  370. .read = seq_read,
  371. .llseek = seq_lseek,
  372. .release = single_release,
  373. };
  374. /*
  375. * SoC-specific functions and definitions
  376. */
  377. static unsigned int orion_mbus_win_offset(int win)
  378. {
  379. return win << 4;
  380. }
  381. static unsigned int armada_370_xp_mbus_win_offset(int win)
  382. {
  383. /* The register layout is a bit annoying and the below code
  384. * tries to cope with it.
  385. * - At offset 0x0, there are the registers for the first 8
  386. * windows, with 4 registers of 32 bits per window (ctrl,
  387. * base, remap low, remap high)
  388. * - Then at offset 0x80, there is a hole of 0x10 bytes for
  389. * the internal registers base address and internal units
  390. * sync barrier register.
  391. * - Then at offset 0x90, there the registers for 12
  392. * windows, with only 2 registers of 32 bits per window
  393. * (ctrl, base).
  394. */
  395. if (win < 8)
  396. return win << 4;
  397. else
  398. return 0x90 + ((win - 8) << 3);
  399. }
  400. static unsigned int mv78xx0_mbus_win_offset(int win)
  401. {
  402. if (win < 8)
  403. return win << 4;
  404. else
  405. return 0x900 + ((win - 8) << 4);
  406. }
  407. static void __init
  408. mvebu_mbus_default_setup_cpu_target(struct mvebu_mbus_state *mbus)
  409. {
  410. int i;
  411. int cs;
  412. mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
  413. for (i = 0, cs = 0; i < 4; i++) {
  414. u32 base = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
  415. u32 size = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
  416. /*
  417. * We only take care of entries for which the chip
  418. * select is enabled, and that don't have high base
  419. * address bits set (devices can only access the first
  420. * 32 bits of the memory).
  421. */
  422. if ((size & DDR_SIZE_ENABLED) &&
  423. !(base & DDR_BASE_CS_HIGH_MASK)) {
  424. struct mbus_dram_window *w;
  425. w = &mvebu_mbus_dram_info.cs[cs++];
  426. w->cs_index = i;
  427. w->mbus_attr = 0xf & ~(1 << i);
  428. if (mbus->hw_io_coherency)
  429. w->mbus_attr |= ATTR_HW_COHERENCY;
  430. w->base = base & DDR_BASE_CS_LOW_MASK;
  431. w->size = (size | ~DDR_SIZE_MASK) + 1;
  432. }
  433. }
  434. mvebu_mbus_dram_info.num_cs = cs;
  435. }
  436. static void __init
  437. mvebu_mbus_dove_setup_cpu_target(struct mvebu_mbus_state *mbus)
  438. {
  439. int i;
  440. int cs;
  441. mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
  442. for (i = 0, cs = 0; i < 2; i++) {
  443. u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
  444. /*
  445. * Chip select enabled?
  446. */
  447. if (map & 1) {
  448. struct mbus_dram_window *w;
  449. w = &mvebu_mbus_dram_info.cs[cs++];
  450. w->cs_index = i;
  451. w->mbus_attr = 0; /* CS address decoding done inside */
  452. /* the DDR controller, no need to */
  453. /* provide attributes */
  454. w->base = map & 0xff800000;
  455. w->size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
  456. }
  457. }
  458. mvebu_mbus_dram_info.num_cs = cs;
  459. }
  460. static const struct mvebu_mbus_soc_data armada_370_xp_mbus_data = {
  461. .num_wins = 20,
  462. .num_remappable_wins = 8,
  463. .win_cfg_offset = armada_370_xp_mbus_win_offset,
  464. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  465. .show_cpu_target = mvebu_sdram_debug_show_orion,
  466. };
  467. static const struct mvebu_mbus_soc_data kirkwood_mbus_data = {
  468. .num_wins = 8,
  469. .num_remappable_wins = 4,
  470. .win_cfg_offset = orion_mbus_win_offset,
  471. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  472. .show_cpu_target = mvebu_sdram_debug_show_orion,
  473. };
  474. static const struct mvebu_mbus_soc_data dove_mbus_data = {
  475. .num_wins = 8,
  476. .num_remappable_wins = 4,
  477. .win_cfg_offset = orion_mbus_win_offset,
  478. .setup_cpu_target = mvebu_mbus_dove_setup_cpu_target,
  479. .show_cpu_target = mvebu_sdram_debug_show_dove,
  480. };
  481. /*
  482. * Some variants of Orion5x have 4 remappable windows, some other have
  483. * only two of them.
  484. */
  485. static const struct mvebu_mbus_soc_data orion5x_4win_mbus_data = {
  486. .num_wins = 8,
  487. .num_remappable_wins = 4,
  488. .win_cfg_offset = orion_mbus_win_offset,
  489. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  490. .show_cpu_target = mvebu_sdram_debug_show_orion,
  491. };
  492. static const struct mvebu_mbus_soc_data orion5x_2win_mbus_data = {
  493. .num_wins = 8,
  494. .num_remappable_wins = 2,
  495. .win_cfg_offset = orion_mbus_win_offset,
  496. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  497. .show_cpu_target = mvebu_sdram_debug_show_orion,
  498. };
  499. static const struct mvebu_mbus_soc_data mv78xx0_mbus_data = {
  500. .num_wins = 14,
  501. .num_remappable_wins = 8,
  502. .win_cfg_offset = mv78xx0_mbus_win_offset,
  503. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  504. .show_cpu_target = mvebu_sdram_debug_show_orion,
  505. };
  506. static const struct of_device_id of_mvebu_mbus_ids[] = {
  507. { .compatible = "marvell,armada370-mbus",
  508. .data = &armada_370_xp_mbus_data, },
  509. { .compatible = "marvell,armadaxp-mbus",
  510. .data = &armada_370_xp_mbus_data, },
  511. { .compatible = "marvell,kirkwood-mbus",
  512. .data = &kirkwood_mbus_data, },
  513. { .compatible = "marvell,dove-mbus",
  514. .data = &dove_mbus_data, },
  515. { .compatible = "marvell,orion5x-88f5281-mbus",
  516. .data = &orion5x_4win_mbus_data, },
  517. { .compatible = "marvell,orion5x-88f5182-mbus",
  518. .data = &orion5x_2win_mbus_data, },
  519. { .compatible = "marvell,orion5x-88f5181-mbus",
  520. .data = &orion5x_2win_mbus_data, },
  521. { .compatible = "marvell,orion5x-88f6183-mbus",
  522. .data = &orion5x_4win_mbus_data, },
  523. { .compatible = "marvell,mv78xx0-mbus",
  524. .data = &mv78xx0_mbus_data, },
  525. { },
  526. };
  527. /*
  528. * Public API of the driver
  529. */
  530. int mvebu_mbus_add_window_remap_by_id(unsigned int target,
  531. unsigned int attribute,
  532. phys_addr_t base, size_t size,
  533. phys_addr_t remap)
  534. {
  535. struct mvebu_mbus_state *s = &mbus_state;
  536. if (!mvebu_mbus_window_conflicts(s, base, size, target, attribute)) {
  537. pr_err("cannot add window '%x:%x', conflicts with another window\n",
  538. target, attribute);
  539. return -EINVAL;
  540. }
  541. return mvebu_mbus_alloc_window(s, base, size, remap, target, attribute);
  542. }
  543. int mvebu_mbus_add_window_by_id(unsigned int target, unsigned int attribute,
  544. phys_addr_t base, size_t size)
  545. {
  546. return mvebu_mbus_add_window_remap_by_id(target, attribute, base,
  547. size, MVEBU_MBUS_NO_REMAP);
  548. }
  549. int mvebu_mbus_del_window(phys_addr_t base, size_t size)
  550. {
  551. int win;
  552. win = mvebu_mbus_find_window(&mbus_state, base, size);
  553. if (win < 0)
  554. return win;
  555. mvebu_mbus_disable_window(&mbus_state, win);
  556. return 0;
  557. }
  558. void mvebu_mbus_get_pcie_mem_aperture(struct resource *res)
  559. {
  560. if (!res)
  561. return;
  562. *res = mbus_state.pcie_mem_aperture;
  563. }
  564. void mvebu_mbus_get_pcie_io_aperture(struct resource *res)
  565. {
  566. if (!res)
  567. return;
  568. *res = mbus_state.pcie_io_aperture;
  569. }
  570. static __init int mvebu_mbus_debugfs_init(void)
  571. {
  572. struct mvebu_mbus_state *s = &mbus_state;
  573. /*
  574. * If no base has been initialized, doesn't make sense to
  575. * register the debugfs entries. We may be on a multiplatform
  576. * kernel that isn't running a Marvell EBU SoC.
  577. */
  578. if (!s->mbuswins_base)
  579. return 0;
  580. s->debugfs_root = debugfs_create_dir("mvebu-mbus", NULL);
  581. if (s->debugfs_root) {
  582. s->debugfs_sdram = debugfs_create_file("sdram", S_IRUGO,
  583. s->debugfs_root, NULL,
  584. &mvebu_sdram_debug_fops);
  585. s->debugfs_devs = debugfs_create_file("devices", S_IRUGO,
  586. s->debugfs_root, NULL,
  587. &mvebu_devs_debug_fops);
  588. }
  589. return 0;
  590. }
  591. fs_initcall(mvebu_mbus_debugfs_init);
  592. static int __init mvebu_mbus_common_init(struct mvebu_mbus_state *mbus,
  593. phys_addr_t mbuswins_phys_base,
  594. size_t mbuswins_size,
  595. phys_addr_t sdramwins_phys_base,
  596. size_t sdramwins_size)
  597. {
  598. int win;
  599. mbus->mbuswins_base = ioremap(mbuswins_phys_base, mbuswins_size);
  600. if (!mbus->mbuswins_base)
  601. return -ENOMEM;
  602. mbus->sdramwins_base = ioremap(sdramwins_phys_base, sdramwins_size);
  603. if (!mbus->sdramwins_base) {
  604. iounmap(mbus_state.mbuswins_base);
  605. return -ENOMEM;
  606. }
  607. for (win = 0; win < mbus->soc->num_wins; win++)
  608. mvebu_mbus_disable_window(mbus, win);
  609. mbus->soc->setup_cpu_target(mbus);
  610. return 0;
  611. }
  612. int __init mvebu_mbus_init(const char *soc, phys_addr_t mbuswins_phys_base,
  613. size_t mbuswins_size,
  614. phys_addr_t sdramwins_phys_base,
  615. size_t sdramwins_size)
  616. {
  617. const struct of_device_id *of_id;
  618. for (of_id = of_mvebu_mbus_ids; of_id->compatible[0]; of_id++)
  619. if (!strcmp(of_id->compatible, soc))
  620. break;
  621. if (!of_id->compatible[0]) {
  622. pr_err("could not find a matching SoC family\n");
  623. return -ENODEV;
  624. }
  625. mbus_state.soc = of_id->data;
  626. return mvebu_mbus_common_init(&mbus_state,
  627. mbuswins_phys_base,
  628. mbuswins_size,
  629. sdramwins_phys_base,
  630. sdramwins_size);
  631. }
  632. #ifdef CONFIG_OF
  633. /*
  634. * The window IDs in the ranges DT property have the following format:
  635. * - bits 28 to 31: MBus custom field
  636. * - bits 24 to 27: window target ID
  637. * - bits 16 to 23: window attribute ID
  638. * - bits 0 to 15: unused
  639. */
  640. #define CUSTOM(id) (((id) & 0xF0000000) >> 24)
  641. #define TARGET(id) (((id) & 0x0F000000) >> 24)
  642. #define ATTR(id) (((id) & 0x00FF0000) >> 16)
  643. static int __init mbus_dt_setup_win(struct mvebu_mbus_state *mbus,
  644. u32 base, u32 size,
  645. u8 target, u8 attr)
  646. {
  647. if (!mvebu_mbus_window_conflicts(mbus, base, size, target, attr)) {
  648. pr_err("cannot add window '%04x:%04x', conflicts with another window\n",
  649. target, attr);
  650. return -EBUSY;
  651. }
  652. if (mvebu_mbus_alloc_window(mbus, base, size, MVEBU_MBUS_NO_REMAP,
  653. target, attr)) {
  654. pr_err("cannot add window '%04x:%04x', too many windows\n",
  655. target, attr);
  656. return -ENOMEM;
  657. }
  658. return 0;
  659. }
  660. static int __init
  661. mbus_parse_ranges(struct device_node *node,
  662. int *addr_cells, int *c_addr_cells, int *c_size_cells,
  663. int *cell_count, const __be32 **ranges_start,
  664. const __be32 **ranges_end)
  665. {
  666. const __be32 *prop;
  667. int ranges_len, tuple_len;
  668. /* Allow a node with no 'ranges' property */
  669. *ranges_start = of_get_property(node, "ranges", &ranges_len);
  670. if (*ranges_start == NULL) {
  671. *addr_cells = *c_addr_cells = *c_size_cells = *cell_count = 0;
  672. *ranges_start = *ranges_end = NULL;
  673. return 0;
  674. }
  675. *ranges_end = *ranges_start + ranges_len / sizeof(__be32);
  676. *addr_cells = of_n_addr_cells(node);
  677. prop = of_get_property(node, "#address-cells", NULL);
  678. *c_addr_cells = be32_to_cpup(prop);
  679. prop = of_get_property(node, "#size-cells", NULL);
  680. *c_size_cells = be32_to_cpup(prop);
  681. *cell_count = *addr_cells + *c_addr_cells + *c_size_cells;
  682. tuple_len = (*cell_count) * sizeof(__be32);
  683. if (ranges_len % tuple_len) {
  684. pr_warn("malformed ranges entry '%s'\n", node->name);
  685. return -EINVAL;
  686. }
  687. return 0;
  688. }
  689. static int __init mbus_dt_setup(struct mvebu_mbus_state *mbus,
  690. struct device_node *np)
  691. {
  692. int addr_cells, c_addr_cells, c_size_cells;
  693. int i, ret, cell_count;
  694. const __be32 *r, *ranges_start, *ranges_end;
  695. ret = mbus_parse_ranges(np, &addr_cells, &c_addr_cells,
  696. &c_size_cells, &cell_count,
  697. &ranges_start, &ranges_end);
  698. if (ret < 0)
  699. return ret;
  700. for (i = 0, r = ranges_start; r < ranges_end; r += cell_count, i++) {
  701. u32 windowid, base, size;
  702. u8 target, attr;
  703. /*
  704. * An entry with a non-zero custom field do not
  705. * correspond to a static window, so skip it.
  706. */
  707. windowid = of_read_number(r, 1);
  708. if (CUSTOM(windowid))
  709. continue;
  710. target = TARGET(windowid);
  711. attr = ATTR(windowid);
  712. base = of_read_number(r + c_addr_cells, addr_cells);
  713. size = of_read_number(r + c_addr_cells + addr_cells,
  714. c_size_cells);
  715. ret = mbus_dt_setup_win(mbus, base, size, target, attr);
  716. if (ret < 0)
  717. return ret;
  718. }
  719. return 0;
  720. }
  721. static void __init mvebu_mbus_get_pcie_resources(struct device_node *np,
  722. struct resource *mem,
  723. struct resource *io)
  724. {
  725. u32 reg[2];
  726. int ret;
  727. /*
  728. * These are optional, so we make sure that resource_size(x) will
  729. * return 0.
  730. */
  731. memset(mem, 0, sizeof(struct resource));
  732. mem->end = -1;
  733. memset(io, 0, sizeof(struct resource));
  734. io->end = -1;
  735. ret = of_property_read_u32_array(np, "pcie-mem-aperture", reg, ARRAY_SIZE(reg));
  736. if (!ret) {
  737. mem->start = reg[0];
  738. mem->end = mem->start + reg[1] - 1;
  739. mem->flags = IORESOURCE_MEM;
  740. }
  741. ret = of_property_read_u32_array(np, "pcie-io-aperture", reg, ARRAY_SIZE(reg));
  742. if (!ret) {
  743. io->start = reg[0];
  744. io->end = io->start + reg[1] - 1;
  745. io->flags = IORESOURCE_IO;
  746. }
  747. }
  748. int __init mvebu_mbus_dt_init(bool is_coherent)
  749. {
  750. struct resource mbuswins_res, sdramwins_res;
  751. struct device_node *np, *controller;
  752. const struct of_device_id *of_id;
  753. const __be32 *prop;
  754. int ret;
  755. np = of_find_matching_node_and_match(NULL, of_mvebu_mbus_ids, &of_id);
  756. if (!np) {
  757. pr_err("could not find a matching SoC family\n");
  758. return -ENODEV;
  759. }
  760. mbus_state.soc = of_id->data;
  761. prop = of_get_property(np, "controller", NULL);
  762. if (!prop) {
  763. pr_err("required 'controller' property missing\n");
  764. return -EINVAL;
  765. }
  766. controller = of_find_node_by_phandle(be32_to_cpup(prop));
  767. if (!controller) {
  768. pr_err("could not find an 'mbus-controller' node\n");
  769. return -ENODEV;
  770. }
  771. if (of_address_to_resource(controller, 0, &mbuswins_res)) {
  772. pr_err("cannot get MBUS register address\n");
  773. return -EINVAL;
  774. }
  775. if (of_address_to_resource(controller, 1, &sdramwins_res)) {
  776. pr_err("cannot get SDRAM register address\n");
  777. return -EINVAL;
  778. }
  779. mbus_state.hw_io_coherency = is_coherent;
  780. /* Get optional pcie-{mem,io}-aperture properties */
  781. mvebu_mbus_get_pcie_resources(np, &mbus_state.pcie_mem_aperture,
  782. &mbus_state.pcie_io_aperture);
  783. ret = mvebu_mbus_common_init(&mbus_state,
  784. mbuswins_res.start,
  785. resource_size(&mbuswins_res),
  786. sdramwins_res.start,
  787. resource_size(&sdramwins_res));
  788. if (ret)
  789. return ret;
  790. /* Setup statically declared windows in the DT */
  791. return mbus_dt_setup(&mbus_state, np);
  792. }
  793. #endif