mvebu-mbus.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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(),
  39. * mvebu_mbus_add_window_remap_flags() and
  40. * mvebu_mbus_del_window(). Since the (target, attribute) values
  41. * differ from one SoC family to another, the API uses a 'const char
  42. * *' string to identify devices, and this driver is responsible for
  43. * knowing the mapping between the name of a device and its
  44. * corresponding (target, attribute) in the current SoC family.
  45. *
  46. * - Provides a debugfs interface in /sys/kernel/debug/mvebu-mbus/ to
  47. * see the list of CPU -> SDRAM windows and their configuration
  48. * (file 'sdram') and the list of CPU -> devices windows and their
  49. * configuration (file 'devices').
  50. */
  51. #include <linux/kernel.h>
  52. #include <linux/module.h>
  53. #include <linux/init.h>
  54. #include <linux/mbus.h>
  55. #include <linux/io.h>
  56. #include <linux/ioport.h>
  57. #include <linux/of.h>
  58. #include <linux/of_address.h>
  59. #include <linux/debugfs.h>
  60. /*
  61. * DDR target is the same on all platforms.
  62. */
  63. #define TARGET_DDR 0
  64. /*
  65. * CPU Address Decode Windows registers
  66. */
  67. #define WIN_CTRL_OFF 0x0000
  68. #define WIN_CTRL_ENABLE BIT(0)
  69. #define WIN_CTRL_TGT_MASK 0xf0
  70. #define WIN_CTRL_TGT_SHIFT 4
  71. #define WIN_CTRL_ATTR_MASK 0xff00
  72. #define WIN_CTRL_ATTR_SHIFT 8
  73. #define WIN_CTRL_SIZE_MASK 0xffff0000
  74. #define WIN_CTRL_SIZE_SHIFT 16
  75. #define WIN_BASE_OFF 0x0004
  76. #define WIN_BASE_LOW 0xffff0000
  77. #define WIN_BASE_HIGH 0xf
  78. #define WIN_REMAP_LO_OFF 0x0008
  79. #define WIN_REMAP_LOW 0xffff0000
  80. #define WIN_REMAP_HI_OFF 0x000c
  81. #define ATTR_HW_COHERENCY (0x1 << 4)
  82. #define DDR_BASE_CS_OFF(n) (0x0000 + ((n) << 3))
  83. #define DDR_BASE_CS_HIGH_MASK 0xf
  84. #define DDR_BASE_CS_LOW_MASK 0xff000000
  85. #define DDR_SIZE_CS_OFF(n) (0x0004 + ((n) << 3))
  86. #define DDR_SIZE_ENABLED BIT(0)
  87. #define DDR_SIZE_CS_MASK 0x1c
  88. #define DDR_SIZE_CS_SHIFT 2
  89. #define DDR_SIZE_MASK 0xff000000
  90. #define DOVE_DDR_BASE_CS_OFF(n) ((n) << 4)
  91. struct mvebu_mbus_mapping {
  92. const char *name;
  93. u8 target;
  94. u8 attr;
  95. u8 attrmask;
  96. };
  97. /*
  98. * Masks used for the 'attrmask' field of mvebu_mbus_mapping. They
  99. * allow to get the real attribute value, discarding the special bits
  100. * used to select a PCI MEM region or a PCI WA region. This allows the
  101. * debugfs code to reverse-match the name of a device from its
  102. * target/attr values.
  103. *
  104. * For all devices except PCI, all bits of 'attr' must be
  105. * considered. For most SoCs, only bit 3 should be ignored (it allows
  106. * to select between PCI MEM and PCI I/O). On Orion5x however, there
  107. * is the special bit 5 to select a PCI WA region.
  108. */
  109. #define MAPDEF_NOMASK 0xff
  110. #define MAPDEF_PCIMASK 0xf7
  111. #define MAPDEF_ORIONPCIMASK 0xd7
  112. /* Macro used to define one mvebu_mbus_mapping entry */
  113. #define MAPDEF(__n, __t, __a, __m) \
  114. { .name = __n, .target = __t, .attr = __a, .attrmask = __m }
  115. struct mvebu_mbus_state;
  116. struct mvebu_mbus_soc_data {
  117. unsigned int num_wins;
  118. unsigned int num_remappable_wins;
  119. unsigned int (*win_cfg_offset)(const int win);
  120. void (*setup_cpu_target)(struct mvebu_mbus_state *s);
  121. int (*show_cpu_target)(struct mvebu_mbus_state *s,
  122. struct seq_file *seq, void *v);
  123. const struct mvebu_mbus_mapping *map;
  124. };
  125. struct mvebu_mbus_state {
  126. void __iomem *mbuswins_base;
  127. void __iomem *sdramwins_base;
  128. struct dentry *debugfs_root;
  129. struct dentry *debugfs_sdram;
  130. struct dentry *debugfs_devs;
  131. const struct mvebu_mbus_soc_data *soc;
  132. int hw_io_coherency;
  133. };
  134. static struct mvebu_mbus_state mbus_state;
  135. static struct mbus_dram_target_info mvebu_mbus_dram_info;
  136. const struct mbus_dram_target_info *mv_mbus_dram_info(void)
  137. {
  138. return &mvebu_mbus_dram_info;
  139. }
  140. EXPORT_SYMBOL_GPL(mv_mbus_dram_info);
  141. /*
  142. * Functions to manipulate the address decoding windows
  143. */
  144. static void mvebu_mbus_read_window(struct mvebu_mbus_state *mbus,
  145. int win, int *enabled, u64 *base,
  146. u32 *size, u8 *target, u8 *attr,
  147. u64 *remap)
  148. {
  149. void __iomem *addr = mbus->mbuswins_base +
  150. mbus->soc->win_cfg_offset(win);
  151. u32 basereg = readl(addr + WIN_BASE_OFF);
  152. u32 ctrlreg = readl(addr + WIN_CTRL_OFF);
  153. if (!(ctrlreg & WIN_CTRL_ENABLE)) {
  154. *enabled = 0;
  155. return;
  156. }
  157. *enabled = 1;
  158. *base = ((u64)basereg & WIN_BASE_HIGH) << 32;
  159. *base |= (basereg & WIN_BASE_LOW);
  160. *size = (ctrlreg | ~WIN_CTRL_SIZE_MASK) + 1;
  161. if (target)
  162. *target = (ctrlreg & WIN_CTRL_TGT_MASK) >> WIN_CTRL_TGT_SHIFT;
  163. if (attr)
  164. *attr = (ctrlreg & WIN_CTRL_ATTR_MASK) >> WIN_CTRL_ATTR_SHIFT;
  165. if (remap) {
  166. if (win < mbus->soc->num_remappable_wins) {
  167. u32 remap_low = readl(addr + WIN_REMAP_LO_OFF);
  168. u32 remap_hi = readl(addr + WIN_REMAP_HI_OFF);
  169. *remap = ((u64)remap_hi << 32) | remap_low;
  170. } else
  171. *remap = 0;
  172. }
  173. }
  174. static void mvebu_mbus_disable_window(struct mvebu_mbus_state *mbus,
  175. int win)
  176. {
  177. void __iomem *addr;
  178. addr = mbus->mbuswins_base + mbus->soc->win_cfg_offset(win);
  179. writel(0, addr + WIN_BASE_OFF);
  180. writel(0, addr + WIN_CTRL_OFF);
  181. if (win < mbus->soc->num_remappable_wins) {
  182. writel(0, addr + WIN_REMAP_LO_OFF);
  183. writel(0, addr + WIN_REMAP_HI_OFF);
  184. }
  185. }
  186. /* Checks whether the given window number is available */
  187. static int mvebu_mbus_window_is_free(struct mvebu_mbus_state *mbus,
  188. const int win)
  189. {
  190. void __iomem *addr = mbus->mbuswins_base +
  191. mbus->soc->win_cfg_offset(win);
  192. u32 ctrl = readl(addr + WIN_CTRL_OFF);
  193. return !(ctrl & WIN_CTRL_ENABLE);
  194. }
  195. /*
  196. * Checks whether the given (base, base+size) area doesn't overlap an
  197. * existing region
  198. */
  199. static int mvebu_mbus_window_conflicts(struct mvebu_mbus_state *mbus,
  200. phys_addr_t base, size_t size,
  201. u8 target, u8 attr)
  202. {
  203. u64 end = (u64)base + size;
  204. int win;
  205. for (win = 0; win < mbus->soc->num_wins; win++) {
  206. u64 wbase, wend;
  207. u32 wsize;
  208. u8 wtarget, wattr;
  209. int enabled;
  210. mvebu_mbus_read_window(mbus, win,
  211. &enabled, &wbase, &wsize,
  212. &wtarget, &wattr, NULL);
  213. if (!enabled)
  214. continue;
  215. wend = wbase + wsize;
  216. /*
  217. * Check if the current window overlaps with the
  218. * proposed physical range
  219. */
  220. if ((u64)base < wend && end > wbase)
  221. return 0;
  222. /*
  223. * Check if target/attribute conflicts
  224. */
  225. if (target == wtarget && attr == wattr)
  226. return 0;
  227. }
  228. return 1;
  229. }
  230. static int mvebu_mbus_find_window(struct mvebu_mbus_state *mbus,
  231. phys_addr_t base, size_t size)
  232. {
  233. int win;
  234. for (win = 0; win < mbus->soc->num_wins; win++) {
  235. u64 wbase;
  236. u32 wsize;
  237. int enabled;
  238. mvebu_mbus_read_window(mbus, win,
  239. &enabled, &wbase, &wsize,
  240. NULL, NULL, NULL);
  241. if (!enabled)
  242. continue;
  243. if (base == wbase && size == wsize)
  244. return win;
  245. }
  246. return -ENODEV;
  247. }
  248. static int mvebu_mbus_setup_window(struct mvebu_mbus_state *mbus,
  249. int win, phys_addr_t base, size_t size,
  250. phys_addr_t remap, u8 target,
  251. u8 attr)
  252. {
  253. void __iomem *addr = mbus->mbuswins_base +
  254. mbus->soc->win_cfg_offset(win);
  255. u32 ctrl, remap_addr;
  256. ctrl = ((size - 1) & WIN_CTRL_SIZE_MASK) |
  257. (attr << WIN_CTRL_ATTR_SHIFT) |
  258. (target << WIN_CTRL_TGT_SHIFT) |
  259. WIN_CTRL_ENABLE;
  260. writel(base & WIN_BASE_LOW, addr + WIN_BASE_OFF);
  261. writel(ctrl, addr + WIN_CTRL_OFF);
  262. if (win < mbus->soc->num_remappable_wins) {
  263. if (remap == MVEBU_MBUS_NO_REMAP)
  264. remap_addr = base;
  265. else
  266. remap_addr = remap;
  267. writel(remap_addr & WIN_REMAP_LOW, addr + WIN_REMAP_LO_OFF);
  268. writel(0, addr + WIN_REMAP_HI_OFF);
  269. }
  270. return 0;
  271. }
  272. static int mvebu_mbus_alloc_window(struct mvebu_mbus_state *mbus,
  273. phys_addr_t base, size_t size,
  274. phys_addr_t remap, u8 target,
  275. u8 attr)
  276. {
  277. int win;
  278. if (remap == MVEBU_MBUS_NO_REMAP) {
  279. for (win = mbus->soc->num_remappable_wins;
  280. win < mbus->soc->num_wins; win++)
  281. if (mvebu_mbus_window_is_free(mbus, win))
  282. return mvebu_mbus_setup_window(mbus, win, base,
  283. size, remap,
  284. target, attr);
  285. }
  286. for (win = 0; win < mbus->soc->num_wins; win++)
  287. if (mvebu_mbus_window_is_free(mbus, win))
  288. return mvebu_mbus_setup_window(mbus, win, base, size,
  289. remap, target, attr);
  290. return -ENOMEM;
  291. }
  292. /*
  293. * Debugfs debugging
  294. */
  295. /* Common function used for Dove, Kirkwood, Armada 370/XP and Orion 5x */
  296. static int mvebu_sdram_debug_show_orion(struct mvebu_mbus_state *mbus,
  297. struct seq_file *seq, void *v)
  298. {
  299. int i;
  300. for (i = 0; i < 4; i++) {
  301. u32 basereg = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
  302. u32 sizereg = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
  303. u64 base;
  304. u32 size;
  305. if (!(sizereg & DDR_SIZE_ENABLED)) {
  306. seq_printf(seq, "[%d] disabled\n", i);
  307. continue;
  308. }
  309. base = ((u64)basereg & DDR_BASE_CS_HIGH_MASK) << 32;
  310. base |= basereg & DDR_BASE_CS_LOW_MASK;
  311. size = (sizereg | ~DDR_SIZE_MASK);
  312. seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
  313. i, (unsigned long long)base,
  314. (unsigned long long)base + size + 1,
  315. (sizereg & DDR_SIZE_CS_MASK) >> DDR_SIZE_CS_SHIFT);
  316. }
  317. return 0;
  318. }
  319. /* Special function for Dove */
  320. static int mvebu_sdram_debug_show_dove(struct mvebu_mbus_state *mbus,
  321. struct seq_file *seq, void *v)
  322. {
  323. int i;
  324. for (i = 0; i < 2; i++) {
  325. u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
  326. u64 base;
  327. u32 size;
  328. if (!(map & 1)) {
  329. seq_printf(seq, "[%d] disabled\n", i);
  330. continue;
  331. }
  332. base = map & 0xff800000;
  333. size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
  334. seq_printf(seq, "[%d] %016llx - %016llx : cs%d\n",
  335. i, (unsigned long long)base,
  336. (unsigned long long)base + size, i);
  337. }
  338. return 0;
  339. }
  340. static int mvebu_sdram_debug_show(struct seq_file *seq, void *v)
  341. {
  342. struct mvebu_mbus_state *mbus = &mbus_state;
  343. return mbus->soc->show_cpu_target(mbus, seq, v);
  344. }
  345. static int mvebu_sdram_debug_open(struct inode *inode, struct file *file)
  346. {
  347. return single_open(file, mvebu_sdram_debug_show, inode->i_private);
  348. }
  349. static const struct file_operations mvebu_sdram_debug_fops = {
  350. .open = mvebu_sdram_debug_open,
  351. .read = seq_read,
  352. .llseek = seq_lseek,
  353. .release = single_release,
  354. };
  355. static int mvebu_devs_debug_show(struct seq_file *seq, void *v)
  356. {
  357. struct mvebu_mbus_state *mbus = &mbus_state;
  358. int win;
  359. for (win = 0; win < mbus->soc->num_wins; win++) {
  360. u64 wbase, wremap;
  361. u32 wsize;
  362. u8 wtarget, wattr;
  363. int enabled, i;
  364. const char *name;
  365. mvebu_mbus_read_window(mbus, win,
  366. &enabled, &wbase, &wsize,
  367. &wtarget, &wattr, &wremap);
  368. if (!enabled) {
  369. seq_printf(seq, "[%02d] disabled\n", win);
  370. continue;
  371. }
  372. for (i = 0; mbus->soc->map[i].name; i++)
  373. if (mbus->soc->map[i].target == wtarget &&
  374. mbus->soc->map[i].attr ==
  375. (wattr & mbus->soc->map[i].attrmask))
  376. break;
  377. name = mbus->soc->map[i].name ?: "unknown";
  378. seq_printf(seq, "[%02d] %016llx - %016llx : %s",
  379. win, (unsigned long long)wbase,
  380. (unsigned long long)(wbase + wsize), name);
  381. if (win < mbus->soc->num_remappable_wins) {
  382. seq_printf(seq, " (remap %016llx)\n",
  383. (unsigned long long)wremap);
  384. } else
  385. seq_printf(seq, "\n");
  386. }
  387. return 0;
  388. }
  389. static int mvebu_devs_debug_open(struct inode *inode, struct file *file)
  390. {
  391. return single_open(file, mvebu_devs_debug_show, inode->i_private);
  392. }
  393. static const struct file_operations mvebu_devs_debug_fops = {
  394. .open = mvebu_devs_debug_open,
  395. .read = seq_read,
  396. .llseek = seq_lseek,
  397. .release = single_release,
  398. };
  399. /*
  400. * SoC-specific functions and definitions
  401. */
  402. static unsigned int orion_mbus_win_offset(int win)
  403. {
  404. return win << 4;
  405. }
  406. static unsigned int armada_370_xp_mbus_win_offset(int win)
  407. {
  408. /* The register layout is a bit annoying and the below code
  409. * tries to cope with it.
  410. * - At offset 0x0, there are the registers for the first 8
  411. * windows, with 4 registers of 32 bits per window (ctrl,
  412. * base, remap low, remap high)
  413. * - Then at offset 0x80, there is a hole of 0x10 bytes for
  414. * the internal registers base address and internal units
  415. * sync barrier register.
  416. * - Then at offset 0x90, there the registers for 12
  417. * windows, with only 2 registers of 32 bits per window
  418. * (ctrl, base).
  419. */
  420. if (win < 8)
  421. return win << 4;
  422. else
  423. return 0x90 + ((win - 8) << 3);
  424. }
  425. static unsigned int mv78xx0_mbus_win_offset(int win)
  426. {
  427. if (win < 8)
  428. return win << 4;
  429. else
  430. return 0x900 + ((win - 8) << 4);
  431. }
  432. static void __init
  433. mvebu_mbus_default_setup_cpu_target(struct mvebu_mbus_state *mbus)
  434. {
  435. int i;
  436. int cs;
  437. mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
  438. for (i = 0, cs = 0; i < 4; i++) {
  439. u32 base = readl(mbus->sdramwins_base + DDR_BASE_CS_OFF(i));
  440. u32 size = readl(mbus->sdramwins_base + DDR_SIZE_CS_OFF(i));
  441. /*
  442. * We only take care of entries for which the chip
  443. * select is enabled, and that don't have high base
  444. * address bits set (devices can only access the first
  445. * 32 bits of the memory).
  446. */
  447. if ((size & DDR_SIZE_ENABLED) &&
  448. !(base & DDR_BASE_CS_HIGH_MASK)) {
  449. struct mbus_dram_window *w;
  450. w = &mvebu_mbus_dram_info.cs[cs++];
  451. w->cs_index = i;
  452. w->mbus_attr = 0xf & ~(1 << i);
  453. if (mbus->hw_io_coherency)
  454. w->mbus_attr |= ATTR_HW_COHERENCY;
  455. w->base = base & DDR_BASE_CS_LOW_MASK;
  456. w->size = (size | ~DDR_SIZE_MASK) + 1;
  457. }
  458. }
  459. mvebu_mbus_dram_info.num_cs = cs;
  460. }
  461. static void __init
  462. mvebu_mbus_dove_setup_cpu_target(struct mvebu_mbus_state *mbus)
  463. {
  464. int i;
  465. int cs;
  466. mvebu_mbus_dram_info.mbus_dram_target_id = TARGET_DDR;
  467. for (i = 0, cs = 0; i < 2; i++) {
  468. u32 map = readl(mbus->sdramwins_base + DOVE_DDR_BASE_CS_OFF(i));
  469. /*
  470. * Chip select enabled?
  471. */
  472. if (map & 1) {
  473. struct mbus_dram_window *w;
  474. w = &mvebu_mbus_dram_info.cs[cs++];
  475. w->cs_index = i;
  476. w->mbus_attr = 0; /* CS address decoding done inside */
  477. /* the DDR controller, no need to */
  478. /* provide attributes */
  479. w->base = map & 0xff800000;
  480. w->size = 0x100000 << (((map & 0x000f0000) >> 16) - 4);
  481. }
  482. }
  483. mvebu_mbus_dram_info.num_cs = cs;
  484. }
  485. static const struct mvebu_mbus_mapping armada_370_map[] = {
  486. MAPDEF("bootrom", 1, 0xe0, MAPDEF_NOMASK),
  487. MAPDEF("devbus-boot", 1, 0x2f, MAPDEF_NOMASK),
  488. MAPDEF("devbus-cs0", 1, 0x3e, MAPDEF_NOMASK),
  489. MAPDEF("devbus-cs1", 1, 0x3d, MAPDEF_NOMASK),
  490. MAPDEF("devbus-cs2", 1, 0x3b, MAPDEF_NOMASK),
  491. MAPDEF("devbus-cs3", 1, 0x37, MAPDEF_NOMASK),
  492. MAPDEF("pcie0.0", 4, 0xe0, MAPDEF_PCIMASK),
  493. MAPDEF("pcie1.0", 8, 0xe0, MAPDEF_PCIMASK),
  494. {},
  495. };
  496. static const struct mvebu_mbus_soc_data armada_370_mbus_data = {
  497. .num_wins = 20,
  498. .num_remappable_wins = 8,
  499. .win_cfg_offset = armada_370_xp_mbus_win_offset,
  500. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  501. .show_cpu_target = mvebu_sdram_debug_show_orion,
  502. .map = armada_370_map,
  503. };
  504. static const struct mvebu_mbus_mapping armada_xp_map[] = {
  505. MAPDEF("bootrom", 1, 0x1d, MAPDEF_NOMASK),
  506. MAPDEF("devbus-boot", 1, 0x2f, MAPDEF_NOMASK),
  507. MAPDEF("devbus-cs0", 1, 0x3e, MAPDEF_NOMASK),
  508. MAPDEF("devbus-cs1", 1, 0x3d, MAPDEF_NOMASK),
  509. MAPDEF("devbus-cs2", 1, 0x3b, MAPDEF_NOMASK),
  510. MAPDEF("devbus-cs3", 1, 0x37, MAPDEF_NOMASK),
  511. MAPDEF("pcie0.0", 4, 0xe0, MAPDEF_PCIMASK),
  512. MAPDEF("pcie0.1", 4, 0xd0, MAPDEF_PCIMASK),
  513. MAPDEF("pcie0.2", 4, 0xb0, MAPDEF_PCIMASK),
  514. MAPDEF("pcie0.3", 4, 0x70, MAPDEF_PCIMASK),
  515. MAPDEF("pcie1.0", 8, 0xe0, MAPDEF_PCIMASK),
  516. MAPDEF("pcie1.1", 8, 0xd0, MAPDEF_PCIMASK),
  517. MAPDEF("pcie1.2", 8, 0xb0, MAPDEF_PCIMASK),
  518. MAPDEF("pcie1.3", 8, 0x70, MAPDEF_PCIMASK),
  519. MAPDEF("pcie2.0", 4, 0xf0, MAPDEF_PCIMASK),
  520. MAPDEF("pcie3.0", 8, 0xf0, MAPDEF_PCIMASK),
  521. {},
  522. };
  523. static const struct mvebu_mbus_soc_data armada_xp_mbus_data = {
  524. .num_wins = 20,
  525. .num_remappable_wins = 8,
  526. .win_cfg_offset = armada_370_xp_mbus_win_offset,
  527. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  528. .show_cpu_target = mvebu_sdram_debug_show_orion,
  529. .map = armada_xp_map,
  530. };
  531. static const struct mvebu_mbus_mapping kirkwood_map[] = {
  532. MAPDEF("pcie0.0", 4, 0xe0, MAPDEF_PCIMASK),
  533. MAPDEF("pcie1.0", 4, 0xd0, MAPDEF_PCIMASK),
  534. MAPDEF("sram", 3, 0x01, MAPDEF_NOMASK),
  535. MAPDEF("nand", 1, 0x2f, MAPDEF_NOMASK),
  536. {},
  537. };
  538. static const struct mvebu_mbus_soc_data kirkwood_mbus_data = {
  539. .num_wins = 8,
  540. .num_remappable_wins = 4,
  541. .win_cfg_offset = orion_mbus_win_offset,
  542. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  543. .show_cpu_target = mvebu_sdram_debug_show_orion,
  544. .map = kirkwood_map,
  545. };
  546. static const struct mvebu_mbus_mapping dove_map[] = {
  547. MAPDEF("pcie0.0", 0x4, 0xe0, MAPDEF_PCIMASK),
  548. MAPDEF("pcie1.0", 0x8, 0xe0, MAPDEF_PCIMASK),
  549. MAPDEF("cesa", 0x3, 0x01, MAPDEF_NOMASK),
  550. MAPDEF("bootrom", 0x1, 0xfd, MAPDEF_NOMASK),
  551. MAPDEF("scratchpad", 0xd, 0x0, MAPDEF_NOMASK),
  552. {},
  553. };
  554. static const struct mvebu_mbus_soc_data dove_mbus_data = {
  555. .num_wins = 8,
  556. .num_remappable_wins = 4,
  557. .win_cfg_offset = orion_mbus_win_offset,
  558. .setup_cpu_target = mvebu_mbus_dove_setup_cpu_target,
  559. .show_cpu_target = mvebu_sdram_debug_show_dove,
  560. .map = dove_map,
  561. };
  562. static const struct mvebu_mbus_mapping orion5x_map[] = {
  563. MAPDEF("pcie0.0", 4, 0x51, MAPDEF_ORIONPCIMASK),
  564. MAPDEF("pci0.0", 3, 0x51, MAPDEF_ORIONPCIMASK),
  565. MAPDEF("devbus-boot", 1, 0x0f, MAPDEF_NOMASK),
  566. MAPDEF("devbus-cs0", 1, 0x1e, MAPDEF_NOMASK),
  567. MAPDEF("devbus-cs1", 1, 0x1d, MAPDEF_NOMASK),
  568. MAPDEF("devbus-cs2", 1, 0x1b, MAPDEF_NOMASK),
  569. MAPDEF("sram", 0, 0x00, MAPDEF_NOMASK),
  570. {},
  571. };
  572. /*
  573. * Some variants of Orion5x have 4 remappable windows, some other have
  574. * only two of them.
  575. */
  576. static const struct mvebu_mbus_soc_data orion5x_4win_mbus_data = {
  577. .num_wins = 8,
  578. .num_remappable_wins = 4,
  579. .win_cfg_offset = orion_mbus_win_offset,
  580. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  581. .show_cpu_target = mvebu_sdram_debug_show_orion,
  582. .map = orion5x_map,
  583. };
  584. static const struct mvebu_mbus_soc_data orion5x_2win_mbus_data = {
  585. .num_wins = 8,
  586. .num_remappable_wins = 2,
  587. .win_cfg_offset = orion_mbus_win_offset,
  588. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  589. .show_cpu_target = mvebu_sdram_debug_show_orion,
  590. .map = orion5x_map,
  591. };
  592. static const struct mvebu_mbus_mapping mv78xx0_map[] = {
  593. MAPDEF("pcie0.0", 4, 0xe0, MAPDEF_PCIMASK),
  594. MAPDEF("pcie0.1", 4, 0xd0, MAPDEF_PCIMASK),
  595. MAPDEF("pcie0.2", 4, 0xb0, MAPDEF_PCIMASK),
  596. MAPDEF("pcie0.3", 4, 0x70, MAPDEF_PCIMASK),
  597. MAPDEF("pcie1.0", 8, 0xe0, MAPDEF_PCIMASK),
  598. MAPDEF("pcie1.1", 8, 0xd0, MAPDEF_PCIMASK),
  599. MAPDEF("pcie1.2", 8, 0xb0, MAPDEF_PCIMASK),
  600. MAPDEF("pcie1.3", 8, 0x70, MAPDEF_PCIMASK),
  601. MAPDEF("pcie2.0", 4, 0xf0, MAPDEF_PCIMASK),
  602. MAPDEF("pcie3.0", 8, 0xf0, MAPDEF_PCIMASK),
  603. {},
  604. };
  605. static const struct mvebu_mbus_soc_data mv78xx0_mbus_data = {
  606. .num_wins = 14,
  607. .num_remappable_wins = 8,
  608. .win_cfg_offset = mv78xx0_mbus_win_offset,
  609. .setup_cpu_target = mvebu_mbus_default_setup_cpu_target,
  610. .show_cpu_target = mvebu_sdram_debug_show_orion,
  611. .map = mv78xx0_map,
  612. };
  613. /*
  614. * The driver doesn't yet have a DT binding because the details of
  615. * this DT binding still need to be sorted out. However, as a
  616. * preparation, we already use of_device_id to match a SoC description
  617. * string against the SoC specific details of this driver.
  618. */
  619. static const struct of_device_id of_mvebu_mbus_ids[] = {
  620. { .compatible = "marvell,armada370-mbus",
  621. .data = &armada_370_mbus_data, },
  622. { .compatible = "marvell,armadaxp-mbus",
  623. .data = &armada_xp_mbus_data, },
  624. { .compatible = "marvell,kirkwood-mbus",
  625. .data = &kirkwood_mbus_data, },
  626. { .compatible = "marvell,dove-mbus",
  627. .data = &dove_mbus_data, },
  628. { .compatible = "marvell,orion5x-88f5281-mbus",
  629. .data = &orion5x_4win_mbus_data, },
  630. { .compatible = "marvell,orion5x-88f5182-mbus",
  631. .data = &orion5x_2win_mbus_data, },
  632. { .compatible = "marvell,orion5x-88f5181-mbus",
  633. .data = &orion5x_2win_mbus_data, },
  634. { .compatible = "marvell,orion5x-88f6183-mbus",
  635. .data = &orion5x_4win_mbus_data, },
  636. { .compatible = "marvell,mv78xx0-mbus",
  637. .data = &mv78xx0_mbus_data, },
  638. { },
  639. };
  640. /*
  641. * Public API of the driver
  642. */
  643. int mvebu_mbus_add_window_remap_flags(const char *devname, phys_addr_t base,
  644. size_t size, phys_addr_t remap,
  645. unsigned int flags)
  646. {
  647. struct mvebu_mbus_state *s = &mbus_state;
  648. u8 target, attr;
  649. int i;
  650. if (!s->soc->map)
  651. return -ENODEV;
  652. for (i = 0; s->soc->map[i].name; i++)
  653. if (!strcmp(s->soc->map[i].name, devname))
  654. break;
  655. if (!s->soc->map[i].name) {
  656. pr_err("mvebu-mbus: unknown device '%s'\n", devname);
  657. return -ENODEV;
  658. }
  659. target = s->soc->map[i].target;
  660. attr = s->soc->map[i].attr;
  661. if (flags == MVEBU_MBUS_PCI_MEM)
  662. attr |= 0x8;
  663. else if (flags == MVEBU_MBUS_PCI_WA)
  664. attr |= 0x28;
  665. if (!mvebu_mbus_window_conflicts(s, base, size, target, attr)) {
  666. pr_err("mvebu-mbus: cannot add window '%s', conflicts with another window\n",
  667. devname);
  668. return -EINVAL;
  669. }
  670. return mvebu_mbus_alloc_window(s, base, size, remap, target, attr);
  671. }
  672. int mvebu_mbus_add_window(const char *devname, phys_addr_t base, size_t size)
  673. {
  674. return mvebu_mbus_add_window_remap_flags(devname, base, size,
  675. MVEBU_MBUS_NO_REMAP, 0);
  676. }
  677. int mvebu_mbus_del_window(phys_addr_t base, size_t size)
  678. {
  679. int win;
  680. win = mvebu_mbus_find_window(&mbus_state, base, size);
  681. if (win < 0)
  682. return win;
  683. mvebu_mbus_disable_window(&mbus_state, win);
  684. return 0;
  685. }
  686. static __init int mvebu_mbus_debugfs_init(void)
  687. {
  688. struct mvebu_mbus_state *s = &mbus_state;
  689. /*
  690. * If no base has been initialized, doesn't make sense to
  691. * register the debugfs entries. We may be on a multiplatform
  692. * kernel that isn't running a Marvell EBU SoC.
  693. */
  694. if (!s->mbuswins_base)
  695. return 0;
  696. s->debugfs_root = debugfs_create_dir("mvebu-mbus", NULL);
  697. if (s->debugfs_root) {
  698. s->debugfs_sdram = debugfs_create_file("sdram", S_IRUGO,
  699. s->debugfs_root, NULL,
  700. &mvebu_sdram_debug_fops);
  701. s->debugfs_devs = debugfs_create_file("devices", S_IRUGO,
  702. s->debugfs_root, NULL,
  703. &mvebu_devs_debug_fops);
  704. }
  705. return 0;
  706. }
  707. fs_initcall(mvebu_mbus_debugfs_init);
  708. int __init mvebu_mbus_init(const char *soc, phys_addr_t mbuswins_phys_base,
  709. size_t mbuswins_size,
  710. phys_addr_t sdramwins_phys_base,
  711. size_t sdramwins_size)
  712. {
  713. struct mvebu_mbus_state *mbus = &mbus_state;
  714. const struct of_device_id *of_id;
  715. int win;
  716. for (of_id = of_mvebu_mbus_ids; of_id->compatible; of_id++)
  717. if (!strcmp(of_id->compatible, soc))
  718. break;
  719. if (!of_id->compatible) {
  720. pr_err("mvebu-mbus: could not find a matching SoC family\n");
  721. return -ENODEV;
  722. }
  723. mbus->soc = of_id->data;
  724. mbus->mbuswins_base = ioremap(mbuswins_phys_base, mbuswins_size);
  725. if (!mbus->mbuswins_base)
  726. return -ENOMEM;
  727. mbus->sdramwins_base = ioremap(sdramwins_phys_base, sdramwins_size);
  728. if (!mbus->sdramwins_base) {
  729. iounmap(mbus_state.mbuswins_base);
  730. return -ENOMEM;
  731. }
  732. if (of_find_compatible_node(NULL, NULL, "marvell,coherency-fabric"))
  733. mbus->hw_io_coherency = 1;
  734. for (win = 0; win < mbus->soc->num_wins; win++)
  735. mvebu_mbus_disable_window(mbus, win);
  736. mbus->soc->setup_cpu_target(mbus);
  737. return 0;
  738. }