mvebu-mbus.c 24 KB

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